1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test for "tc action mirred egress mirror" when the underlay route points at a
5# bridge device with vlan filtering (802.1q), and the egress device is a team
6# device.
7#
8# +----------------------+                             +----------------------+
9# | H1                   |                             |                   H2 |
10# |     + $h1.333        |                             |        $h1.555 +     |
11# |     | 192.0.2.1/28   |                             |  192.0.2.18/28 |     |
12# +-----|----------------+                             +----------------|-----+
13#       |                               $h1                             |
14#       +--------------------------------+------------------------------+
15#                                        |
16# +--------------------------------------|------------------------------------+
17# | SW                                   o---> mirror                         |
18# |                                      |                                    |
19# |     +--------------------------------+------------------------------+     |
20# |     |                              $swp1                            |     |
21# |     + $swp1.333                                           $swp1.555 +     |
22# |       192.0.2.2/28                                    192.0.2.17/28       |
23# |                                                                           |
24# | +-----------------------------------------------------------------------+ |
25# | |                        BR1 (802.1q)                                   | |
26# | |     + lag (team)       192.0.2.129/28                                 | |
27# | |    / \                 2001:db8:2::1/64                               | |
28# | +---/---\---------------------------------------------------------------+ |
29# |    /     \                                                            ^   |
30# |   |       \                                        + gt4 (gretap)     |   |
31# |   |        \                                         loc=192.0.2.129  |   |
32# |   |         \                                        rem=192.0.2.130 -+   |
33# |   |          \                                       ttl=100              |
34# |   |           \                                      tos=inherit          |
35# |   |            \                                                          |
36# |   |             \_________________________________                        |
37# |   |                                               \                       |
38# |   + $swp3                                          + $swp4                |
39# +---|------------------------------------------------|----------------------+
40#     |                                                |
41# +---|----------------------+                     +---|----------------------+
42# |   + $h3               H3 |                     |   + $h4               H4 |
43# |     192.0.2.130/28       |                     |     192.0.2.130/28       |
44# |     2001:db8:2::2/64     |                     |     2001:db8:2::2/64     |
45# +--------------------------+                     +--------------------------+
46
47ALL_TESTS="
48	test_mirror_gretap_first
49	test_mirror_gretap_second
50"
51
52NUM_NETIFS=6
53source lib.sh
54source mirror_lib.sh
55source mirror_gre_lib.sh
56
57require_command $ARPING
58
59vlan_host_create()
60{
61	local if_name=$1; shift
62	local vid=$1; shift
63	local vrf_name=$1; shift
64	local ips=("${@}")
65
66	vrf_create $vrf_name
67	ip link set dev $vrf_name up
68	vlan_create $if_name $vid $vrf_name "${ips[@]}"
69}
70
71vlan_host_destroy()
72{
73	local if_name=$1; shift
74	local vid=$1; shift
75	local vrf_name=$1; shift
76
77	vlan_destroy $if_name $vid
78	ip link set dev $vrf_name down
79	vrf_destroy $vrf_name
80}
81
82h1_create()
83{
84	vlan_host_create $h1 333 vrf-h1 192.0.2.1/28
85	ip -4 route add 192.0.2.16/28 vrf vrf-h1 nexthop via 192.0.2.2
86}
87
88h1_destroy()
89{
90	ip -4 route del 192.0.2.16/28 vrf vrf-h1
91	vlan_host_destroy $h1 333 vrf-h1
92}
93
94h2_create()
95{
96	vlan_host_create $h1 555 vrf-h2 192.0.2.18/28
97	ip -4 route add 192.0.2.0/28 vrf vrf-h2 nexthop via 192.0.2.17
98}
99
100h2_destroy()
101{
102	ip -4 route del 192.0.2.0/28 vrf vrf-h2
103	vlan_host_destroy $h1 555 vrf-h2
104}
105
106h3_create()
107{
108	simple_if_init $h3 192.0.2.130/28
109	tc qdisc add dev $h3 clsact
110}
111
112h3_destroy()
113{
114	tc qdisc del dev $h3 clsact
115	simple_if_fini $h3 192.0.2.130/28
116}
117
118h4_create()
119{
120	simple_if_init $h4 192.0.2.130/28
121	tc qdisc add dev $h4 clsact
122}
123
124h4_destroy()
125{
126	tc qdisc del dev $h4 clsact
127	simple_if_fini $h4 192.0.2.130/28
128}
129
130switch_create()
131{
132	ip link set dev $swp1 up
133	tc qdisc add dev $swp1 clsact
134	vlan_create $swp1 333 "" 192.0.2.2/28
135	vlan_create $swp1 555 "" 192.0.2.17/28
136
137	tunnel_create gt4 gretap 192.0.2.129 192.0.2.130 \
138		      ttl 100 tos inherit
139
140	ip link set dev $swp3 up
141	ip link set dev $swp4 up
142
143	ip link add name br1 type bridge vlan_filtering 1
144	ip link set dev br1 up
145	__addr_add_del br1 add 192.0.2.129/32
146	ip -4 route add 192.0.2.130/32 dev br1
147
148	team_create lag loadbalance $swp3 $swp4
149	ip link set dev lag master br1
150}
151
152switch_destroy()
153{
154	ip link set dev lag nomaster
155	team_destroy lag
156
157	ip -4 route del 192.0.2.130/32 dev br1
158	__addr_add_del br1 del 192.0.2.129/32
159	ip link set dev br1 down
160	ip link del dev br1
161
162	ip link set dev $swp4 down
163	ip link set dev $swp3 down
164
165	tunnel_destroy gt4
166
167	vlan_destroy $swp1 555
168	vlan_destroy $swp1 333
169	tc qdisc del dev $swp1 clsact
170	ip link set dev $swp1 down
171}
172
173setup_prepare()
174{
175	h1=${NETIFS[p1]}
176	swp1=${NETIFS[p2]}
177
178	swp3=${NETIFS[p3]}
179	h3=${NETIFS[p4]}
180
181	swp4=${NETIFS[p5]}
182	h4=${NETIFS[p6]}
183
184	vrf_prepare
185
186	ip link set dev $h1 up
187	h1_create
188	h2_create
189	h3_create
190	h4_create
191	switch_create
192
193	forwarding_enable
194
195	trap_install $h3 ingress
196	trap_install $h4 ingress
197}
198
199cleanup()
200{
201	pre_cleanup
202
203	trap_uninstall $h4 ingress
204	trap_uninstall $h3 ingress
205
206	forwarding_restore
207
208	switch_destroy
209	h4_destroy
210	h3_destroy
211	h2_destroy
212	h1_destroy
213	ip link set dev $h1 down
214
215	vrf_cleanup
216}
217
218test_lag_slave()
219{
220	local host_dev=$1; shift
221	local up_dev=$1; shift
222	local down_dev=$1; shift
223	local what=$1; shift
224
225	RET=0
226
227	tc filter add dev $swp1 ingress pref 999 \
228		proto 802.1q flower vlan_ethtype arp $tcflags \
229		action pass
230	mirror_install $swp1 ingress gt4 \
231		"proto 802.1q flower vlan_id 333 $tcflags"
232
233	# Test connectivity through $up_dev when $down_dev is set down.
234	ip link set dev $down_dev down
235	ip neigh flush dev br1
236	setup_wait_dev $up_dev
237	setup_wait_dev $host_dev
238	$ARPING -I br1 192.0.2.130 -qfc 1
239	sleep 2
240	mirror_test vrf-h1 192.0.2.1 192.0.2.18 $host_dev 1 10
241
242	# Test lack of connectivity when both slaves are down.
243	ip link set dev $up_dev down
244	sleep 2
245	mirror_test vrf-h1 192.0.2.1 192.0.2.18 $h3 1 0
246	mirror_test vrf-h1 192.0.2.1 192.0.2.18 $h4 1 0
247
248	ip link set dev $up_dev up
249	ip link set dev $down_dev up
250	mirror_uninstall $swp1 ingress
251	tc filter del dev $swp1 ingress pref 999
252
253	log_test "$what ($tcflags)"
254}
255
256test_mirror_gretap_first()
257{
258	test_lag_slave $h3 $swp3 $swp4 "mirror to gretap: LAG first slave"
259}
260
261test_mirror_gretap_second()
262{
263	test_lag_slave $h4 $swp4 $swp3 "mirror to gretap: LAG second slave"
264}
265
266test_all()
267{
268	slow_path_trap_install $swp1 ingress
269	slow_path_trap_install $swp1 egress
270
271	tests_run
272
273	slow_path_trap_uninstall $swp1 egress
274	slow_path_trap_uninstall $swp1 ingress
275}
276
277trap cleanup EXIT
278
279setup_prepare
280setup_wait
281
282tcflags="skip_hw"
283test_all
284
285if ! tc_offload_check; then
286	echo "WARN: Could not test offloaded functionality"
287else
288	tcflags="skip_sw"
289	test_all
290fi
291
292exit $EXIT_STATUS
293