diff mbox series

[v2] blackhole_dev: convert self-test to KUnit

Message ID 20250208-blackholedev-kunit-convert-v2-1-182db9bd56ec@gmail.com (mailing list archive)
State Accepted
Commit b341f6fd45abb188653d9e0a2816bc53d64278b1
Delegated to: Netdev Maintainers
Headers show
Series [v2] blackhole_dev: convert self-test to KUnit | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success Errors and warnings before: 26 (+1) this patch: 26 (+1)
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 48 this patch: 48
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest fail Script test_blackhole_dev.sh not found in tools/testing/selftests/net/Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 11 this patch: 10
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: please write a help paragraph that fully describes the config symbol
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2025-02-11--00-00 (tests: 889)

Commit Message

Tamir Duberstein Feb. 8, 2025, 7:26 p.m. UTC
Convert this very simple smoke test to a KUnit test.

Add a missing `htons` call that was spotted[0] by kernel test robot
<lkp@intel.com> after initial conversion to KUnit.

Link: https://lore.kernel.org/oe-kbuild-all/202502090223.qCYMBjWT-lkp@intel.com/ [0]
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
I tested this using:

$ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 --kconfig_add CONFIG_NET=y blackholedev
---
Changes in v2:
- Add missing `htons` call. (kernel test robot <lkp@intel.com>)
- Link to v1: https://lore.kernel.org/r/20250207-blackholedev-kunit-convert-v1-1-8ef0dc1ff881@gmail.com
---
 lib/Kconfig.debug                                  | 20 ++++-----
 lib/Makefile                                       |  2 +-
 ...{test_blackhole_dev.c => blackhole_dev_kunit.c} | 47 ++++++++--------------
 tools/testing/selftests/net/Makefile               |  2 +-
 tools/testing/selftests/net/test_blackhole_dev.sh  | 11 -----
 5 files changed, 29 insertions(+), 53 deletions(-)


---
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20250207-blackholedev-kunit-convert-9a52a1a1a032

Best regards,

Comments

Tamir Duberstein Feb. 10, 2025, 3:58 p.m. UTC | #1
On Sat, Feb 8, 2025 at 2:26 PM Tamir Duberstein <tamird@gmail.com> wrote:
>
> Convert this very simple smoke test to a KUnit test.
>
> Add a missing `htons` call that was spotted[0] by kernel test robot
> <lkp@intel.com> after initial conversion to KUnit.
>
> Link: https://lore.kernel.org/oe-kbuild-all/202502090223.qCYMBjWT-lkp@intel.com/ [0]
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> I tested this using:
>
> $ tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 --kconfig_add CONFIG_NET=y blackholedev
> ---
> Changes in v2:
> - Add missing `htons` call. (kernel test robot <lkp@intel.com>)
> - Link to v1: https://lore.kernel.org/r/20250207-blackholedev-kunit-convert-v1-1-8ef0dc1ff881@gmail.com
> ---
>  lib/Kconfig.debug                                  | 20 ++++-----
>  lib/Makefile                                       |  2 +-
>  ...{test_blackhole_dev.c => blackhole_dev_kunit.c} | 47 ++++++++--------------
>  tools/testing/selftests/net/Makefile               |  2 +-
>  tools/testing/selftests/net/test_blackhole_dev.sh  | 11 -----
>  5 files changed, 29 insertions(+), 53 deletions(-)
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 1af972a92d06..238321830993 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2557,15 +2557,6 @@ config TEST_BPF
>
>           If unsure, say N.
>
> -config TEST_BLACKHOLE_DEV
> -       tristate "Test blackhole netdev functionality"
> -       depends on m && NET
> -       help
> -         This builds the "test_blackhole_dev" module that validates the
> -         data path through this blackhole netdev.
> -
> -         If unsure, say N.
> -
>  config FIND_BIT_BENCHMARK
>         tristate "Test find_bit functions"
>         help
> @@ -2888,6 +2879,17 @@ config USERCOPY_KUNIT_TEST
>           on the copy_to/from_user infrastructure, making sure basic
>           user/kernel boundary testing is working.
>
> +config BLACKHOLE_DEV_KUNIT_TEST
> +       tristate "Test blackhole netdev functionality" if !KUNIT_ALL_TESTS
> +       depends on NET
> +       depends on KUNIT
> +       default KUNIT_ALL_TESTS
> +       help
> +         This builds the "blackhole_dev_kunit" module that validates the
> +         data path through this blackhole netdev.
> +
> +         If unsure, say N.
> +
>  config TEST_UDELAY
>         tristate "udelay test driver"
>         help
> diff --git a/lib/Makefile b/lib/Makefile
> index d5cfc7afbbb8..19ff6993c2bc 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -102,7 +102,6 @@ obj-$(CONFIG_TEST_RUNTIME) += tests/
>  obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
>  obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
>  obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
> -obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
>  obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
>  obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
>  obj-$(CONFIG_TEST_HMM) += test_hmm.o
> @@ -393,6 +392,7 @@ obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
>  obj-$(CONFIG_CRC_KUNIT_TEST) += crc_kunit.o
>  obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
>  obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
> +obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o
>
>  obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
>
> diff --git a/lib/test_blackhole_dev.c b/lib/blackhole_dev_kunit.c
> similarity index 68%
> rename from lib/test_blackhole_dev.c
> rename to lib/blackhole_dev_kunit.c
> index ec290ac2a0d9..06834ab35f43 100644
> --- a/lib/test_blackhole_dev.c
> +++ b/lib/blackhole_dev_kunit.c
> @@ -1,6 +1,6 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /*
> - * This module tests the blackhole_dev that is created during the
> + * This tests the blackhole_dev that is created during the
>   * net subsystem initialization. The test this module performs is
>   * by injecting an skb into the stack with skb->dev as the
>   * blackhole_dev and expects kernel to behave in a sane manner
> @@ -9,9 +9,8 @@
>   * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com>
>   */
>
> -#include <linux/init.h>
> +#include <kunit/test.h>
>  #include <linux/module.h>
> -#include <linux/printk.h>
>  #include <linux/skbuff.h>
>  #include <linux/netdevice.h>
>  #include <linux/udp.h>
> @@ -25,17 +24,15 @@
>
>  #define UDP_PORT 1234
>
> -static int __init test_blackholedev_init(void)
> +static void test_blackholedev(struct kunit *test)
>  {
>         struct ipv6hdr *ip6h;
>         struct sk_buff *skb;
>         struct udphdr *uh;
>         int data_len;
> -       int ret;
>
>         skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
> -       if (!skb)
> -               return -ENOMEM;
> +       KUNIT_ASSERT_NOT_NULL(test, skb);
>
>         /* Reserve head-room for the headers */
>         skb_reserve(skb, HEAD_SIZE);
> @@ -55,7 +52,7 @@ static int __init test_blackholedev_init(void)
>         ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
>         skb_set_network_header(skb, 0);
>         ip6h->hop_limit = 32;
> -       ip6h->payload_len = data_len + sizeof(struct udphdr);
> +       ip6h->payload_len = htons(data_len + sizeof(struct udphdr));
>         ip6h->nexthdr = IPPROTO_UDP;
>         ip6h->saddr = in6addr_loopback;
>         ip6h->daddr = in6addr_loopback;
> @@ -68,32 +65,20 @@ static int __init test_blackholedev_init(void)
>         skb->dev = blackhole_netdev;
>
>         /* Now attempt to send the packet */
> -       ret = dev_queue_xmit(skb);
> -
> -       switch (ret) {
> -       case NET_XMIT_SUCCESS:
> -               pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n");
> -               break;
> -       case NET_XMIT_DROP:
> -               pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n");
> -               break;
> -       case NET_XMIT_CN:
> -               pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n");
> -               break;
> -       default:
> -               pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret);
> -       }
> -
> -       return 0;
> +       KUNIT_EXPECT_EQ(test, dev_queue_xmit(skb), NET_XMIT_SUCCESS);
>  }
>
> -static void __exit test_blackholedev_exit(void)
> -{
> -       pr_warn("test_blackholedev module terminating.\n");
> -}
> +static struct kunit_case blackholedev_cases[] = {
> +       KUNIT_CASE(test_blackholedev),
> +       {},
> +};
> +
> +static struct kunit_suite blackholedev_suite = {
> +       .name = "blackholedev",
> +       .test_cases = blackholedev_cases,
> +};
>
> -module_init(test_blackholedev_init);
> -module_exit(test_blackholedev_exit);
> +kunit_test_suite(blackholedev_suite);
>
>  MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
>  MODULE_DESCRIPTION("module test of the blackhole_dev");
> diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
> index 73ee88d6b043..afa4bcdc5833 100644
> --- a/tools/testing/selftests/net/Makefile
> +++ b/tools/testing/selftests/net/Makefile
> @@ -7,7 +7,7 @@ CFLAGS += -I../../../../usr/include/ $(KHDR_INCLUDES)
>  CFLAGS += -I../
>
>  TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
> -             rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh
> +             rtnetlink.sh xfrm_policy.sh
>  TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh
>  TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh
>  TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh
> diff --git a/tools/testing/selftests/net/test_blackhole_dev.sh b/tools/testing/selftests/net/test_blackhole_dev.sh
> deleted file mode 100755
> index 3119b80e711f..000000000000
> --- a/tools/testing/selftests/net/test_blackhole_dev.sh
> +++ /dev/null
> @@ -1,11 +0,0 @@
> -#!/bin/sh
> -# SPDX-License-Identifier: GPL-2.0
> -# Runs blackhole-dev test using blackhole-dev kernel module
> -
> -if /sbin/modprobe -q test_blackhole_dev ; then
> -       /sbin/modprobe -q -r test_blackhole_dev;
> -       echo "test_blackhole_dev: ok";
> -else
> -       echo "test_blackhole_dev: [FAIL]";
> -       exit 1;
> -fi
>
> ---
> base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
> change-id: 20250207-blackholedev-kunit-convert-9a52a1a1a032
>
> Best regards,
> --
> Tamir Duberstein <tamird@gmail.com>

Adding David.
patchwork-bot+netdevbpf@kernel.org Feb. 12, 2025, 12:30 a.m. UTC | #2
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Sat, 08 Feb 2025 14:26:43 -0500 you wrote:
> Convert this very simple smoke test to a KUnit test.
> 
> Add a missing `htons` call that was spotted[0] by kernel test robot
> <lkp@intel.com> after initial conversion to KUnit.
> 
> Link: https://lore.kernel.org/oe-kbuild-all/202502090223.qCYMBjWT-lkp@intel.com/ [0]
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> 
> [...]

Here is the summary with links:
  - [v2] blackhole_dev: convert self-test to KUnit
    https://git.kernel.org/netdev/net-next/c/b341f6fd45ab

You are awesome, thank you!
diff mbox series

Patch

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1af972a92d06..238321830993 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2557,15 +2557,6 @@  config TEST_BPF
 
 	  If unsure, say N.
 
-config TEST_BLACKHOLE_DEV
-	tristate "Test blackhole netdev functionality"
-	depends on m && NET
-	help
-	  This builds the "test_blackhole_dev" module that validates the
-	  data path through this blackhole netdev.
-
-	  If unsure, say N.
-
 config FIND_BIT_BENCHMARK
 	tristate "Test find_bit functions"
 	help
@@ -2888,6 +2879,17 @@  config USERCOPY_KUNIT_TEST
 	  on the copy_to/from_user infrastructure, making sure basic
 	  user/kernel boundary testing is working.
 
+config BLACKHOLE_DEV_KUNIT_TEST
+	tristate "Test blackhole netdev functionality" if !KUNIT_ALL_TESTS
+	depends on NET
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the "blackhole_dev_kunit" module that validates the
+	  data path through this blackhole netdev.
+
+	  If unsure, say N.
+
 config TEST_UDELAY
 	tristate "udelay test driver"
 	help
diff --git a/lib/Makefile b/lib/Makefile
index d5cfc7afbbb8..19ff6993c2bc 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -102,7 +102,6 @@  obj-$(CONFIG_TEST_RUNTIME) += tests/
 obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
 obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
 obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
-obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
 obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
 obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
 obj-$(CONFIG_TEST_HMM) += test_hmm.o
@@ -393,6 +392,7 @@  obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
 obj-$(CONFIG_CRC_KUNIT_TEST) += crc_kunit.o
 obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
+obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o
 
 obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
 
diff --git a/lib/test_blackhole_dev.c b/lib/blackhole_dev_kunit.c
similarity index 68%
rename from lib/test_blackhole_dev.c
rename to lib/blackhole_dev_kunit.c
index ec290ac2a0d9..06834ab35f43 100644
--- a/lib/test_blackhole_dev.c
+++ b/lib/blackhole_dev_kunit.c
@@ -1,6 +1,6 @@ 
 // SPDX-License-Identifier: GPL-2.0
 /*
- * This module tests the blackhole_dev that is created during the
+ * This tests the blackhole_dev that is created during the
  * net subsystem initialization. The test this module performs is
  * by injecting an skb into the stack with skb->dev as the
  * blackhole_dev and expects kernel to behave in a sane manner
@@ -9,9 +9,8 @@ 
  * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com>
  */
 
-#include <linux/init.h>
+#include <kunit/test.h>
 #include <linux/module.h>
-#include <linux/printk.h>
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
 #include <linux/udp.h>
@@ -25,17 +24,15 @@ 
 
 #define UDP_PORT 1234
 
-static int __init test_blackholedev_init(void)
+static void test_blackholedev(struct kunit *test)
 {
 	struct ipv6hdr *ip6h;
 	struct sk_buff *skb;
 	struct udphdr *uh;
 	int data_len;
-	int ret;
 
 	skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
-	if (!skb)
-		return -ENOMEM;
+	KUNIT_ASSERT_NOT_NULL(test, skb);
 
 	/* Reserve head-room for the headers */
 	skb_reserve(skb, HEAD_SIZE);
@@ -55,7 +52,7 @@  static int __init test_blackholedev_init(void)
 	ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
 	skb_set_network_header(skb, 0);
 	ip6h->hop_limit = 32;
-	ip6h->payload_len = data_len + sizeof(struct udphdr);
+	ip6h->payload_len = htons(data_len + sizeof(struct udphdr));
 	ip6h->nexthdr = IPPROTO_UDP;
 	ip6h->saddr = in6addr_loopback;
 	ip6h->daddr = in6addr_loopback;
@@ -68,32 +65,20 @@  static int __init test_blackholedev_init(void)
 	skb->dev = blackhole_netdev;
 
 	/* Now attempt to send the packet */
-	ret = dev_queue_xmit(skb);
-
-	switch (ret) {
-	case NET_XMIT_SUCCESS:
-		pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n");
-		break;
-	case NET_XMIT_DROP:
-		pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n");
-		break;
-	case NET_XMIT_CN:
-		pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n");
-		break;
-	default:
-		pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret);
-	}
-
-	return 0;
+	KUNIT_EXPECT_EQ(test, dev_queue_xmit(skb), NET_XMIT_SUCCESS);
 }
 
-static void __exit test_blackholedev_exit(void)
-{
-	pr_warn("test_blackholedev module terminating.\n");
-}
+static struct kunit_case blackholedev_cases[] = {
+	KUNIT_CASE(test_blackholedev),
+	{},
+};
+
+static struct kunit_suite blackholedev_suite = {
+	.name = "blackholedev",
+	.test_cases = blackholedev_cases,
+};
 
-module_init(test_blackholedev_init);
-module_exit(test_blackholedev_exit);
+kunit_test_suite(blackholedev_suite);
 
 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
 MODULE_DESCRIPTION("module test of the blackhole_dev");
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 73ee88d6b043..afa4bcdc5833 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -7,7 +7,7 @@  CFLAGS += -I../../../../usr/include/ $(KHDR_INCLUDES)
 CFLAGS += -I../
 
 TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
-	      rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh
+	      rtnetlink.sh xfrm_policy.sh
 TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh
 TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh
 TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh
diff --git a/tools/testing/selftests/net/test_blackhole_dev.sh b/tools/testing/selftests/net/test_blackhole_dev.sh
deleted file mode 100755
index 3119b80e711f..000000000000
--- a/tools/testing/selftests/net/test_blackhole_dev.sh
+++ /dev/null
@@ -1,11 +0,0 @@ 
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-# Runs blackhole-dev test using blackhole-dev kernel module
-
-if /sbin/modprobe -q test_blackhole_dev ; then
-	/sbin/modprobe -q -r test_blackhole_dev;
-	echo "test_blackhole_dev: ok";
-else
-	echo "test_blackhole_dev: [FAIL]";
-	exit 1;
-fi