skbuff.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @gfp: allocation flags
  20. *
  21. * Allocate a new struct sk_buff with gfp flags, zero fill the given length
  22. * and add it as a resource to the kunit test for automatic cleanup.
  23. *
  24. * Returns: newly allocated SKB, or %NULL on error
  25. */
  26. static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len,
  27. gfp_t gfp)
  28. {
  29. struct sk_buff *res = alloc_skb(len, gfp);
  30. if (!res || skb_pad(res, len))
  31. return NULL;
  32. if (kunit_add_action_or_reset(test, kunit_action_kfree_skb, res))
  33. return NULL;
  34. return res;
  35. }
  36. /**
  37. * kunit_kfree_skb() - Like kfree_skb except for allocations managed by KUnit.
  38. * @test: The test case to which the resource belongs.
  39. * @skb: The SKB to free.
  40. */
  41. static inline void kunit_kfree_skb(struct kunit *test, struct sk_buff *skb)
  42. {
  43. if (!skb)
  44. return;
  45. kunit_release_action(test, kunit_action_kfree_skb, (void *)skb);
  46. }
  47. #endif /* _KUNIT_SKBUFF_H */