diff mbox series

[3/3] kunit: add a convenience allocation wrapper for SKBs

Message ID 20230329172311.71861-3-benjamin@sipsolutions.net (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series [1/3] kunit: add parameter generation macro using description from array | expand

Commit Message

Benjamin Berg March 29, 2023, 5:23 p.m. UTC
From: Benjamin Berg <benjamin.berg@intel.com>

This simplifies the use of SKBs in tests by avoiding the need for
error checking.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
---
 include/kunit/skbuff.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 include/kunit/skbuff.h
diff mbox series

Patch

diff --git a/include/kunit/skbuff.h b/include/kunit/skbuff.h
new file mode 100644
index 000000000000..46dcb00af655
--- /dev/null
+++ b/include/kunit/skbuff.h
@@ -0,0 +1,38 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit struct sk_buff helpers.
+ *
+ * Copyright (C) 2023 Intel Corporation
+ */
+
+#ifndef _KUNIT_SKBUFF_H
+#define _KUNIT_SKBUFF_H
+
+#include <kunit/test.h>
+#include <linux/skbuff.h>
+
+/**
+ * kunit_zalloc_skb() - Allocate and initialize a resource managed skb.
+ * @test: The test case to which the skb belongs
+ * @len: size to allocate
+ * @gfp: allocation mask
+ *
+ * Allocate a new struct sk_buff, zero fill the give length and add it as a
+ * resource to the kunit test for automatic cleanup.
+ *
+ * The function will not return in case of an allocation error.
+ */
+static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len,
+					       gfp_t gfp)
+{
+	struct sk_buff *res = alloc_skb(len, gfp);
+
+	KUNIT_ASSERT_NOT_NULL(test, res);
+	KUNIT_ASSERT_EQ(test, skb_pad(res, len), 0);
+
+	kunit_add_cleanup(test, (kunit_cleanup_t) kfree_skb, res, gfp);
+
+	return res;
+}
+
+#endif /* _KUNIT_SKBUFF_H */