xref: /src/tests/sys/net/bpf/bpf.sh (revision 1abb62867f2a36acb689fdfed62af00b78d8f324)
1##
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2025 Rubicon Communications, LLC ("Netgate")
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27. $(atf_get_srcdir)/../../common/vnet.subr
28
29atf_test_case "multi_read" "cleanup"
30multi_read_head()
31{
32	atf_set descr 'Test multiple readers on /dev/bpf'
33	atf_set require.user root
34}
35multi_read_body()
36{
37	vnet_init
38
39	epair=$(vnet_mkepair)
40	ifconfig ${epair}a inet 192.0.2.1/24 up
41
42	vnet_mkjail alcatraz ${epair}b
43	jexec alcatraz ifconfig ${epair}b inet 192.0.2.2/24 up
44
45	atf_check -s exit:0 -o ignore \
46	    ping -c 1 192.0.2.2
47
48	# Start a multi-thread (or multi-process) read on bpf
49	$(atf_get_srcdir)/bpf_multi_read ${epair}a &
50
51	# Generate traffic
52	ping -f 192.0.2.2 >/dev/null 2>&1 &
53
54	# Now let this run for 10 seconds
55	sleep 10
56}
57multi_read_cleanup()
58{
59	vnet_cleanup
60}
61
62atf_test_case "inject" "cleanup"
63inject_head()
64{
65	atf_set descr 'Catch packets, re-inject and check'
66	atf_set require.user root
67}
68inject_body()
69{
70	vnet_init
71
72	if sysctl -q kern.features.rss >/dev/null; then
73		atf_skip "This test is flaky with RSS"
74	fi
75
76	epair=$(vnet_mkepair)
77	ifconfig ${epair}a inet 192.0.2.1/24 up
78	vnet_mkjail alcatraz ${epair}b
79	jexec alcatraz ifconfig ${epair}b inet 192.0.2.2/24 up
80
81	in=$(pwd)/$(mktemp in.pcap.XXXXXXXXXX)
82	in2=$(pwd)/$(mktemp in2.pcap.XXXXXXXXXX)
83	out=$(pwd)/$(mktemp out.pcap.XXXXXXXXXX)
84
85	# write dump on jail side, with "in" direction
86	jexec alcatraz $(atf_get_srcdir)/pcap-test \
87	    capture epair0b $in 3 in > out & pid=$!
88	while ! jexec alcatraz netstat -B | grep -q epair0b.*pcap-test; do
89		sleep 0.01;
90	done
91	atf_check -s exit:0 -o ignore ping -c 3 -i 0.1 192.0.2.2
92	atf_check -s exit:0 sh -c "wait $pid; exit $?"
93	atf_check -s exit:0 -o empty cat out
94
95	# inject dump on host side, recording on both sides
96	jexec alcatraz $(atf_get_srcdir)/pcap-test \
97	    capture epair0b $in2 3 in > jout & jpid=$!
98	while ! jexec alcatraz netstat -B | grep -q epair0b.*pcap-test; do
99		sleep 0.01;
100	done
101	$(atf_get_srcdir)/pcap-test \
102	    capture epair0a $out 3 out > hout & hpid=$!
103	while ! netstat -B | grep -q epair0a.*pcap-test; do
104		sleep 0.01;
105	done
106	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
107	    inject epair0a $in 3
108	atf_check -s exit:0 sh -c "wait $jpid; exit $?"
109	atf_check -s exit:0 -o empty cat jout
110	atf_check -s exit:0 sh -c "wait $hpid; exit $?"
111	atf_check -s exit:0 -o empty cat hout
112
113	# all 3 dumps should be equal
114	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
115	    compare $in $out
116	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
117	    compare $in $in2
118}
119inject_cleanup()
120{
121	vnet_cleanup
122}
123
124atf_init_test_cases()
125{
126	atf_add_test_case "multi_read"
127	atf_add_test_case "inject"
128}
129