Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

blackhole_dev: convert self-test to KUnit

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>
Link: https://patch.msgid.link/20250208-blackholedev-kunit-convert-v2-1-182db9bd56ec@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Tamir Duberstein and committed by
Jakub Kicinski
b341f6fd b6df0523

+29 -53
+11 -9
lib/Kconfig.debug
··· 2557 2557 2558 2558 If unsure, say N. 2559 2559 2560 - config TEST_BLACKHOLE_DEV 2561 - tristate "Test blackhole netdev functionality" 2562 - depends on m && NET 2563 - help 2564 - This builds the "test_blackhole_dev" module that validates the 2565 - data path through this blackhole netdev. 2566 - 2567 - If unsure, say N. 2568 - 2569 2560 config FIND_BIT_BENCHMARK 2570 2561 tristate "Test find_bit functions" 2571 2562 help ··· 2878 2887 This builds the "usercopy_kunit" module that runs sanity checks 2879 2888 on the copy_to/from_user infrastructure, making sure basic 2880 2889 user/kernel boundary testing is working. 2890 + 2891 + config BLACKHOLE_DEV_KUNIT_TEST 2892 + tristate "Test blackhole netdev functionality" if !KUNIT_ALL_TESTS 2893 + depends on NET 2894 + depends on KUNIT 2895 + default KUNIT_ALL_TESTS 2896 + help 2897 + This builds the "blackhole_dev_kunit" module that validates the 2898 + data path through this blackhole netdev. 2899 + 2900 + If unsure, say N. 2881 2901 2882 2902 config TEST_UDELAY 2883 2903 tristate "udelay test driver"
+1 -1
lib/Makefile
··· 102 102 obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o 103 103 obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o 104 104 obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o 105 - obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o 106 105 obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o 107 106 obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o 108 107 obj-$(CONFIG_TEST_HMM) += test_hmm.o ··· 392 393 obj-$(CONFIG_CRC_KUNIT_TEST) += crc_kunit.o 393 394 obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o 394 395 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o 396 + obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o 395 397 396 398 obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o 397 399
+16 -31
lib/test_blackhole_dev.c lib/blackhole_dev_kunit.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* 3 - * This module tests the blackhole_dev that is created during the 3 + * This tests the blackhole_dev that is created during the 4 4 * net subsystem initialization. The test this module performs is 5 5 * by injecting an skb into the stack with skb->dev as the 6 6 * blackhole_dev and expects kernel to behave in a sane manner ··· 9 9 * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com> 10 10 */ 11 11 12 - #include <linux/init.h> 12 + #include <kunit/test.h> 13 13 #include <linux/module.h> 14 - #include <linux/printk.h> 15 14 #include <linux/skbuff.h> 16 15 #include <linux/netdevice.h> 17 16 #include <linux/udp.h> ··· 24 25 25 26 #define UDP_PORT 1234 26 27 27 - static int __init test_blackholedev_init(void) 28 + static void test_blackholedev(struct kunit *test) 28 29 { 29 30 struct ipv6hdr *ip6h; 30 31 struct sk_buff *skb; 31 32 struct udphdr *uh; 32 33 int data_len; 33 - int ret; 34 34 35 35 skb = alloc_skb(SKB_SIZE, GFP_KERNEL); 36 - if (!skb) 37 - return -ENOMEM; 36 + KUNIT_ASSERT_NOT_NULL(test, skb); 38 37 39 38 /* Reserve head-room for the headers */ 40 39 skb_reserve(skb, HEAD_SIZE); ··· 52 55 ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr)); 53 56 skb_set_network_header(skb, 0); 54 57 ip6h->hop_limit = 32; 55 - ip6h->payload_len = data_len + sizeof(struct udphdr); 58 + ip6h->payload_len = htons(data_len + sizeof(struct udphdr)); 56 59 ip6h->nexthdr = IPPROTO_UDP; 57 60 ip6h->saddr = in6addr_loopback; 58 61 ip6h->daddr = in6addr_loopback; ··· 65 68 skb->dev = blackhole_netdev; 66 69 67 70 /* Now attempt to send the packet */ 68 - ret = dev_queue_xmit(skb); 69 - 70 - switch (ret) { 71 - case NET_XMIT_SUCCESS: 72 - pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n"); 73 - break; 74 - case NET_XMIT_DROP: 75 - pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n"); 76 - break; 77 - case NET_XMIT_CN: 78 - pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n"); 79 - break; 80 - default: 81 - pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret); 82 - } 83 - 84 - return 0; 71 + KUNIT_EXPECT_EQ(test, dev_queue_xmit(skb), NET_XMIT_SUCCESS); 85 72 } 86 73 87 - static void __exit test_blackholedev_exit(void) 88 - { 89 - pr_warn("test_blackholedev module terminating.\n"); 90 - } 74 + static struct kunit_case blackholedev_cases[] = { 75 + KUNIT_CASE(test_blackholedev), 76 + {}, 77 + }; 91 78 92 - module_init(test_blackholedev_init); 93 - module_exit(test_blackholedev_exit); 79 + static struct kunit_suite blackholedev_suite = { 80 + .name = "blackholedev", 81 + .test_cases = blackholedev_cases, 82 + }; 83 + 84 + kunit_test_suite(blackholedev_suite); 94 85 95 86 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>"); 96 87 MODULE_DESCRIPTION("module test of the blackhole_dev");
+1 -1
tools/testing/selftests/net/Makefile
··· 7 7 CFLAGS += -I../ 8 8 9 9 TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \ 10 - rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh 10 + rtnetlink.sh xfrm_policy.sh 11 11 TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh 12 12 TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh 13 13 TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh
-11
tools/testing/selftests/net/test_blackhole_dev.sh
··· 1 - #!/bin/sh 2 - # SPDX-License-Identifier: GPL-2.0 3 - # Runs blackhole-dev test using blackhole-dev kernel module 4 - 5 - if /sbin/modprobe -q test_blackhole_dev ; then 6 - /sbin/modprobe -q -r test_blackhole_dev; 7 - echo "test_blackhole_dev: ok"; 8 - else 9 - echo "test_blackhole_dev: [FAIL]"; 10 - exit 1; 11 - fi