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