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