skbuff.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * KUnit resource management helpers for SKBs (skbuff).
  4. *
  5. * Copyright (C) 2023 Intel Corporation
  6. */
  7. #ifndef _KUNIT_SKBUFF_H
  8. #define _KUNIT_SKBUFF_H
  9. #include <kunit/resource.h>
  10. #include <linux/skbuff.h>
  11. static void kunit_action_kfree_skb(void *p)
  12. {
  13. kfree_skb((struct sk_buff *)p);
  14. }
  15. /**
  16. * kunit_zalloc_skb() - Allocate and initialize a resource managed skb.
  17. * @test: The test case to which the skb belongs
  18. * @len: size to allocate
  19. *
  20. * Allocate a new struct sk_buff with GFP_KERNEL, zero fill the give length
  21. * and add it as a resource to the kunit test for automatic cleanup.
  22. *
  23. * Returns: newly allocated SKB, or %NULL on error
  24. */
  25. static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len,
  26. gfp_t gfp)
  27. {
  28. struct sk_buff *res = alloc_skb(len, gfp);
  29. if (!res || skb_pad(res, len))
  30. return NULL;
  31. if (kunit_add_action_or_reset(test, kunit_action_kfree_skb, res))
  32. return NULL;
  33. return res;
  34. }
  35. /**
  36. * kunit_kfree_skb() - Like kfree_skb except for allocations managed by KUnit.
  37. * @test: The test case to which the resource belongs.
  38. * @skb: The SKB to free.
  39. */
  40. static inline void kunit_kfree_skb(struct kunit *test, struct sk_buff *skb)
  41. {
  42. if (!skb)
  43. return;
  44. kunit_release_action(test, kunit_action_kfree_skb, (void *)skb);
  45. }
  46. #endif /* _KUNIT_SKBUFF_H */