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
8 #ifndef _KUNIT_SKBUFF_H
9 #define _KUNIT_SKBUFF_H
10
11 #include <kunit/resource.h>
12 #include <linux/skbuff.h>
13
kunit_action_kfree_skb(void * p)14 static void kunit_action_kfree_skb(void *p)
15 {
16 kfree_skb((struct sk_buff *)p);
17 }
18
19 /**
20 * kunit_zalloc_skb() - Allocate and initialize a resource managed skb.
21 * @test: The test case to which the skb belongs
22 * @len: size to allocate
23 * @gfp: allocation flags
24 *
25 * Allocate a new struct sk_buff with gfp flags, zero fill the given length
26 * and add it as a resource to the kunit test for automatic cleanup.
27 *
28 * Returns: newly allocated SKB, or %NULL on error
29 */
kunit_zalloc_skb(struct kunit * test,int len,gfp_t gfp)30 static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len,
31 gfp_t gfp)
32 {
33 struct sk_buff *res = alloc_skb(len, gfp);
34
35 if (!res || skb_pad(res, len))
36 return NULL;
37
38 if (kunit_add_action_or_reset(test, kunit_action_kfree_skb, res))
39 return NULL;
40
41 return res;
42 }
43
44 /**
45 * kunit_kfree_skb() - Like kfree_skb except for allocations managed by KUnit.
46 * @test: The test case to which the resource belongs.
47 * @skb: The SKB to free.
48 */
kunit_kfree_skb(struct kunit * test,struct sk_buff * skb)49 static inline void kunit_kfree_skb(struct kunit *test, struct sk_buff *skb)
50 {
51 if (!skb)
52 return;
53
54 kunit_release_action(test, kunit_action_kfree_skb, (void *)skb);
55 }
56
57 #endif /* _KUNIT_SKBUFF_H */
58