Skip to content

Commit 509e56b

Browse files
Mahesh Bandewardavem330
authored andcommitted
blackhole_dev: add a selftest
Since this is not really a device with all capabilities, this test ensures that it has *enough* to make it through the data path without causing unwanted side-effects (read crash!). Signed-off-by: Mahesh Bandewar <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8d7017f commit 509e56b

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

lib/Kconfig.debug

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,15 @@ config TEST_BPF
19091909

19101910
If unsure, say N.
19111911

1912+
config TEST_BLACKHOLE_DEV
1913+
tristate "Test blackhole netdev functionality"
1914+
depends on m && NET
1915+
help
1916+
This builds the "test_blackhole_dev" module that validates the
1917+
data path through this blackhole netdev.
1918+
1919+
If unsure, say N.
1920+
19121921
config FIND_BIT_BENCHMARK
19131922
tristate "Test find_bit functions"
19141923
help

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
9191
obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
9292
obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
9393
obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o
94+
obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
9495

9596
obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/
9697

lib/test_blackhole_dev.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* This module tests the blackhole_dev that is created during the
4+
* net subsystem initialization. The test this module performs is
5+
* by injecting an skb into the stack with skb->dev as the
6+
* blackhole_dev and expects kernel to behave in a sane manner
7+
* (in other words, *not crash*)!
8+
*
9+
* Copyright (c) 2018, Mahesh Bandewar <[email protected]>
10+
*/
11+
12+
#include <linux/init.h>
13+
#include <linux/module.h>
14+
#include <linux/printk.h>
15+
#include <linux/skbuff.h>
16+
#include <linux/netdevice.h>
17+
#include <linux/udp.h>
18+
#include <linux/ipv6.h>
19+
20+
#include <net/dst.h>
21+
22+
#define SKB_SIZE 256
23+
#define HEAD_SIZE (14+40+8) /* Ether + IPv6 + UDP */
24+
#define TAIL_SIZE 32 /* random tail-room */
25+
26+
#define UDP_PORT 1234
27+
28+
static int __init test_blackholedev_init(void)
29+
{
30+
struct ipv6hdr *ip6h;
31+
struct sk_buff *skb;
32+
struct ethhdr *ethh;
33+
struct udphdr *uh;
34+
int data_len;
35+
int ret;
36+
37+
skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
38+
if (!skb)
39+
return -ENOMEM;
40+
41+
/* Reserve head-room for the headers */
42+
skb_reserve(skb, HEAD_SIZE);
43+
44+
/* Add data to the skb */
45+
data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE);
46+
memset(__skb_put(skb, data_len), 0xf, data_len);
47+
48+
/* Add protocol data */
49+
/* (Transport) UDP */
50+
uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
51+
skb_set_transport_header(skb, 0);
52+
uh->source = uh->dest = htons(UDP_PORT);
53+
uh->len = htons(data_len);
54+
uh->check = 0;
55+
/* (Network) IPv6 */
56+
ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
57+
skb_set_network_header(skb, 0);
58+
ip6h->hop_limit = 32;
59+
ip6h->payload_len = data_len + sizeof(struct udphdr);
60+
ip6h->nexthdr = IPPROTO_UDP;
61+
ip6h->saddr = in6addr_loopback;
62+
ip6h->daddr = in6addr_loopback;
63+
/* Ether */
64+
ethh = (struct ethhdr *)skb_push(skb, sizeof(struct ethhdr));
65+
skb_set_mac_header(skb, 0);
66+
67+
skb->protocol = htons(ETH_P_IPV6);
68+
skb->pkt_type = PACKET_HOST;
69+
skb->dev = blackhole_netdev;
70+
71+
/* Now attempt to send the packet */
72+
ret = dev_queue_xmit(skb);
73+
74+
switch (ret) {
75+
case NET_XMIT_SUCCESS:
76+
pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n");
77+
break;
78+
case NET_XMIT_DROP:
79+
pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n");
80+
break;
81+
case NET_XMIT_CN:
82+
pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n");
83+
break;
84+
default:
85+
pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret);
86+
}
87+
88+
return 0;
89+
}
90+
91+
static void __exit test_blackholedev_exit(void)
92+
{
93+
pr_warn("test_blackholedev module terminating.\n");
94+
}
95+
96+
module_init(test_blackholedev_init);
97+
module_exit(test_blackholedev_exit);
98+
99+
MODULE_AUTHOR("Mahesh Bandewar <[email protected]>");
100+
MODULE_LICENSE("GPL");

tools/testing/selftests/net/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CFLAGS = -Wall -Wl,--no-as-needed -O2 -g
55
CFLAGS += -I../../../../usr/include/
66

77
TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
8-
rtnetlink.sh xfrm_policy.sh
8+
rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh
99
TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh
1010
TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh
1111
TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh

tools/testing/selftests/net/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ CONFIG_NFT_CHAIN_NAT_IPV6=m
2727
CONFIG_NFT_CHAIN_NAT_IPV4=m
2828
CONFIG_NET_SCH_FQ=m
2929
CONFIG_NET_SCH_ETF=m
30+
CONFIG_TEST_BLACKHOLE_DEV=m
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)