xref: /linux/tools/testing/selftests/net/forwarding/mirror_lib.sh (revision d361d78fe2cc8a7a60e8c71825f84e70d34938af)
1# SPDX-License-Identifier: GPL-2.0
2
3mirror_install()
4{
5	local from_dev=$1; shift
6	local direction=$1; shift
7	local to_dev=$1; shift
8	local filter=$1; shift
9
10	tc filter add dev $from_dev $direction \
11	   pref 1000 $filter \
12	   action mirred egress mirror dev $to_dev
13}
14
15mirror_uninstall()
16{
17	local from_dev=$1; shift
18	local direction=$1; shift
19
20	tc filter del dev $swp1 $direction pref 1000
21}
22
23is_ipv6()
24{
25	local addr=$1; shift
26
27	[[ -z ${addr//[0-9a-fA-F:]/} ]]
28}
29
30mirror_test()
31{
32	local vrf_name=$1; shift
33	local sip=$1; shift
34	local dip=$1; shift
35	local dev=$1; shift
36	local pref=$1; shift
37	local expect=$1; shift
38
39	if is_ipv6 $dip; then
40		local proto=-6
41		local type="icmp6 type=128" # Echo request.
42	else
43		local proto=
44		local type="icmp echoreq"
45	fi
46
47	if [[ -z ${expect//[[:digit:]]/} ]]; then
48		expect="== $expect"
49	fi
50
51	local t0=$(tc_rule_stats_get $dev $pref)
52	$MZ $proto $vrf_name ${sip:+-A $sip} -B $dip -a own -b bc -q \
53	    -c 10 -d 100msec -t $type
54	sleep 0.5
55	local t1=$(tc_rule_stats_get $dev $pref)
56	local delta=$((t1 - t0))
57	((delta $expect))
58	check_err $? "Expected to capture $expect packets, got $delta."
59}
60
61do_test_span_dir_ips()
62{
63	local expect=$1; shift
64	local dev=$1; shift
65	local ip1=$1; shift
66	local ip2=$1; shift
67	local forward_type=${1-8}; shift
68	local backward_type=${1-0}; shift
69
70	icmp_capture_install $dev "type $forward_type"
71	mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
72	icmp_capture_uninstall $dev
73
74	icmp_capture_install $dev "type $backward_type"
75	mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
76	icmp_capture_uninstall $dev
77}
78
79quick_test_span_dir_ips()
80{
81	local dev=$1; shift
82	local ip1=$1; shift
83	local ip2=$1; shift
84	local forward_type=${1-8}; shift
85	local backward_type=${1-0}; shift
86
87	do_test_span_dir_ips 10 "$dev" "$ip1" "$ip2" \
88			     "$forward_type" "$backward_type"
89}
90
91fail_test_span_dir_ips()
92{
93	local dev=$1; shift
94	local direction=$1; shift
95	local ip1=$1; shift
96	local ip2=$1; shift
97
98	do_test_span_dir_ips 0 "$dev" "$direction" "$ip1" "$ip2"
99}
100
101test_span_dir_ips()
102{
103	local dev=$1; shift
104	local forward_type=$1; shift
105	local backward_type=$1; shift
106	local ip1=$1; shift
107	local ip2=$1; shift
108
109	quick_test_span_dir_ips "$dev" "$ip1" "$ip2" \
110				"$forward_type" "$backward_type"
111
112	icmp_capture_install $dev "type $forward_type"
113	mirror_test v$h1 $ip1 $ip2 $dev 100 10
114	icmp_capture_uninstall $dev
115
116	icmp_capture_install $dev "type $backward_type"
117	mirror_test v$h2 $ip2 $ip1 $dev 100 10
118	icmp_capture_uninstall $dev
119}
120
121fail_test_span_dir()
122{
123	local dev=$1; shift
124	local direction=$1; shift
125
126	fail_test_span_dir_ips "$dev" "$direction" 192.0.2.1 192.0.2.2
127}
128
129test_span_dir()
130{
131	local dev=$1; shift
132	local forward_type=$1; shift
133	local backward_type=$1; shift
134
135	test_span_dir_ips "$dev" "$forward_type" "$backward_type" \
136			  192.0.2.1 192.0.2.2
137}
138
139do_test_span_vlan_dir_ips()
140{
141	local expect=$1; shift
142	local dev=$1; shift
143	local vid=$1; shift
144	local ul_proto=$1; shift
145	local ip1=$1; shift
146	local ip2=$1; shift
147
148	# Install the capture as skip_hw to avoid double-counting of packets.
149	# The traffic is meant for local box anyway, so will be trapped to
150	# kernel.
151	vlan_capture_install $dev "skip_hw vlan_id $vid vlan_ethtype $ul_proto"
152	mirror_test v$h1 $ip1 $ip2 $dev 100 "$expect"
153	mirror_test v$h2 $ip2 $ip1 $dev 100 "$expect"
154	vlan_capture_uninstall $dev
155}
156
157quick_test_span_vlan_dir_ips()
158{
159	local dev=$1; shift
160	local vid=$1; shift
161	local ul_proto=$1; shift
162	local ip1=$1; shift
163	local ip2=$1; shift
164
165	do_test_span_vlan_dir_ips '>= 10' "$dev" "$vid" "$ul_proto" \
166				  "$ip1" "$ip2"
167}
168
169fail_test_span_vlan_dir_ips()
170{
171	local dev=$1; shift
172	local vid=$1; shift
173	local ul_proto=$1; shift
174	local ip1=$1; shift
175	local ip2=$1; shift
176
177	do_test_span_vlan_dir_ips 0 "$dev" "$vid" "$ul_proto" "$ip1" "$ip2"
178}
179
180quick_test_span_vlan_dir()
181{
182	local dev=$1; shift
183	local vid=$1; shift
184	local ul_proto=$1; shift
185
186	quick_test_span_vlan_dir_ips "$dev" "$vid" "$ul_proto" \
187				     192.0.2.1 192.0.2.2
188}
189
190fail_test_span_vlan_dir()
191{
192	local dev=$1; shift
193	local vid=$1; shift
194	local ul_proto=$1; shift
195
196	fail_test_span_vlan_dir_ips "$dev" "$vid" "$ul_proto" \
197				    192.0.2.1 192.0.2.2
198}
199