xref: /freebsd/tests/sys/net/bpf/bpf.sh (revision 8ccc0d235c226d84112561d453c49904398d085c)
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	epair=$(vnet_mkepair)
73	ifconfig ${epair}a inet 192.0.2.1/24 up
74	vnet_mkjail alcatraz ${epair}b
75	jexec alcatraz ifconfig ${epair}b inet 192.0.2.2/24 up
76
77	in=$(pwd)/$(mktemp in.pcap.XXXXXXXXXX)
78	in2=$(pwd)/$(mktemp in2.pcap.XXXXXXXXXX)
79	out=$(pwd)/$(mktemp out.pcap.XXXXXXXXXX)
80
81	# write dump on jail side, with "in" direction
82	jexec alcatraz $(atf_get_srcdir)/pcap-test \
83	    capture epair0b $in 3 in > out & pid=$!
84	while ! jexec alcatraz netstat -B | grep -q epair0b.*pcap-test; do
85		sleep 0.01;
86	done
87	atf_check -s exit:0 -o ignore ping -c 3 -i 0.1 192.0.2.2
88	atf_check -s exit:0 sh -c "wait $pid; exit $?"
89	atf_check -s exit:0 -o empty cat out
90
91	# inject dump on host side, recording on both sides
92	jexec alcatraz $(atf_get_srcdir)/pcap-test \
93	    capture epair0b $in2 3 in > jout & jpid=$!
94	while ! jexec alcatraz netstat -B | grep -q epair0b.*pcap-test; do
95		sleep 0.01;
96	done
97	$(atf_get_srcdir)/pcap-test \
98	    capture epair0a $out 3 out > hout & hpid=$!
99	while ! netstat -B | grep -q epair0a.*pcap-test; do
100		sleep 0.01;
101	done
102	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
103	    inject epair0a $in 3
104	atf_check -s exit:0 sh -c "wait $jpid; exit $?"
105	atf_check -s exit:0 -o empty cat jout
106	atf_check -s exit:0 sh -c "wait $hpid; exit $?"
107	atf_check -s exit:0 -o empty cat hout
108
109	# all 3 dumps should be equal
110	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
111	    compare $in $out
112	atf_check -s exit:0 -o empty -e empty $(atf_get_srcdir)/pcap-test \
113	    compare $in $in2
114}
115inject_cleanup()
116{
117	vnet_cleanup
118}
119
120atf_init_test_cases()
121{
122	atf_add_test_case "multi_read"
123	atf_add_test_case "inject"
124}
125