forked from mirrors/linux
		
	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 <maheshb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									8d7017fd62
								
							
						
					
					
						commit
						509e56b37c
					
				
					 6 changed files with 123 additions and 1 deletions
				
			
		|  | @ -1909,6 +1909,15 @@ config TEST_BPF | ||||||
| 
 | 
 | ||||||
| 	  If unsure, say N. | 	  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 | config FIND_BIT_BENCHMARK | ||||||
| 	tristate "Test find_bit functions" | 	tristate "Test find_bit functions" | ||||||
| 	help | 	help | ||||||
|  |  | ||||||
|  | @ -91,6 +91,7 @@ obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o | ||||||
| obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o | obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o | ||||||
| obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o | obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o | ||||||
| obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o | obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o | ||||||
|  | obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o | ||||||
| 
 | 
 | ||||||
| obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/ | obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/ | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										100
									
								
								lib/test_blackhole_dev.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								lib/test_blackhole_dev.c
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,100 @@ | ||||||
|  | // SPDX-License-Identifier: GPL-2.0
 | ||||||
|  | /*
 | ||||||
|  |  * This module 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 | ||||||
|  |  * (in other words, *not crash*)! | ||||||
|  |  * | ||||||
|  |  * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com> | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | #include <linux/init.h> | ||||||
|  | #include <linux/module.h> | ||||||
|  | #include <linux/printk.h> | ||||||
|  | #include <linux/skbuff.h> | ||||||
|  | #include <linux/netdevice.h> | ||||||
|  | #include <linux/udp.h> | ||||||
|  | #include <linux/ipv6.h> | ||||||
|  | 
 | ||||||
|  | #include <net/dst.h> | ||||||
|  | 
 | ||||||
|  | #define SKB_SIZE  256 | ||||||
|  | #define HEAD_SIZE (14+40+8)	/* Ether + IPv6 + UDP */ | ||||||
|  | #define TAIL_SIZE 32		/* random tail-room */ | ||||||
|  | 
 | ||||||
|  | #define UDP_PORT 1234 | ||||||
|  | 
 | ||||||
|  | static int __init test_blackholedev_init(void) | ||||||
|  | { | ||||||
|  | 	struct ipv6hdr *ip6h; | ||||||
|  | 	struct sk_buff *skb; | ||||||
|  | 	struct ethhdr *ethh; | ||||||
|  | 	struct udphdr *uh; | ||||||
|  | 	int data_len; | ||||||
|  | 	int ret; | ||||||
|  | 
 | ||||||
|  | 	skb = alloc_skb(SKB_SIZE, GFP_KERNEL); | ||||||
|  | 	if (!skb) | ||||||
|  | 		return -ENOMEM; | ||||||
|  | 
 | ||||||
|  | 	/* Reserve head-room for the headers */ | ||||||
|  | 	skb_reserve(skb, HEAD_SIZE); | ||||||
|  | 
 | ||||||
|  | 	/* Add data to the skb */ | ||||||
|  | 	data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE); | ||||||
|  | 	memset(__skb_put(skb, data_len), 0xf, data_len); | ||||||
|  | 
 | ||||||
|  | 	/* Add protocol data */ | ||||||
|  | 	/* (Transport) UDP */ | ||||||
|  | 	uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr)); | ||||||
|  | 	skb_set_transport_header(skb, 0); | ||||||
|  | 	uh->source = uh->dest = htons(UDP_PORT); | ||||||
|  | 	uh->len = htons(data_len); | ||||||
|  | 	uh->check = 0; | ||||||
|  | 	/* (Network) IPv6 */ | ||||||
|  | 	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->nexthdr = IPPROTO_UDP; | ||||||
|  | 	ip6h->saddr = in6addr_loopback; | ||||||
|  | 	ip6h->daddr = in6addr_loopback; | ||||||
|  | 	/* Ether */ | ||||||
|  | 	ethh = (struct ethhdr *)skb_push(skb, sizeof(struct ethhdr)); | ||||||
|  | 	skb_set_mac_header(skb, 0); | ||||||
|  | 
 | ||||||
|  | 	skb->protocol = htons(ETH_P_IPV6); | ||||||
|  | 	skb->pkt_type = PACKET_HOST; | ||||||
|  | 	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; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static void __exit test_blackholedev_exit(void) | ||||||
|  | { | ||||||
|  | 	pr_warn("test_blackholedev module terminating.\n"); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module_init(test_blackholedev_init); | ||||||
|  | module_exit(test_blackholedev_exit); | ||||||
|  | 
 | ||||||
|  | MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>"); | ||||||
|  | MODULE_LICENSE("GPL"); | ||||||
|  | @ -5,7 +5,7 @@ CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g | ||||||
| CFLAGS += -I../../../../usr/include/ | CFLAGS += -I../../../../usr/include/ | ||||||
| 
 | 
 | ||||||
| TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
 | TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
 | ||||||
| 	      rtnetlink.sh xfrm_policy.sh | 	      rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh | ||||||
| TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.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 += 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 | TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh | ||||||
|  |  | ||||||
|  | @ -27,3 +27,4 @@ CONFIG_NFT_CHAIN_NAT_IPV6=m | ||||||
| CONFIG_NFT_CHAIN_NAT_IPV4=m | CONFIG_NFT_CHAIN_NAT_IPV4=m | ||||||
| CONFIG_NET_SCH_FQ=m | CONFIG_NET_SCH_FQ=m | ||||||
| CONFIG_NET_SCH_ETF=m | CONFIG_NET_SCH_ETF=m | ||||||
|  | CONFIG_TEST_BLACKHOLE_DEV=m | ||||||
|  |  | ||||||
							
								
								
									
										11
									
								
								tools/testing/selftests/net/test_blackhole_dev.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										11
									
								
								tools/testing/selftests/net/test_blackhole_dev.sh
									
									
									
									
									
										Executable file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | #!/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 | ||||||
		Loading…
	
		Reference in a new issue
	
	 Mahesh Bandewar
						Mahesh Bandewar