Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / net / forwarding / tc_actions.sh
blobea89e558672db0af1e64c8761f7879c649642104
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 ALL_TESTS="gact_drop_and_ok_test mirred_egress_redirect_test \
5 mirred_egress_mirror_test matchall_mirred_egress_mirror_test \
6 gact_trap_test mirred_egress_to_ingress_test \
7 mirred_egress_to_ingress_tcp_test \
8 ingress_2nd_vlan_push egress_2nd_vlan_push"
9 NUM_NETIFS=4
10 source tc_common.sh
11 source lib.sh
13 require_command ncat
15 tcflags="skip_hw"
17 h1_create()
19 simple_if_init $h1 192.0.2.1/24
20 tc qdisc add dev $h1 clsact
23 h1_destroy()
25 tc qdisc del dev $h1 clsact
26 simple_if_fini $h1 192.0.2.1/24
29 h2_create()
31 simple_if_init $h2 192.0.2.2/24
32 tc qdisc add dev $h2 clsact
35 h2_destroy()
37 tc qdisc del dev $h2 clsact
38 simple_if_fini $h2 192.0.2.2/24
41 switch_create()
43 simple_if_init $swp1 192.0.2.2/24
44 tc qdisc add dev $swp1 clsact
46 simple_if_init $swp2 192.0.2.1/24
49 switch_destroy()
51 simple_if_fini $swp2 192.0.2.1/24
53 tc qdisc del dev $swp1 clsact
54 simple_if_fini $swp1 192.0.2.2/24
57 mirred_egress_test()
59 local action=$1
60 local protocol=$2
61 local classifier=$3
62 local classifier_args=$4
64 RET=0
66 tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
67 dst_ip 192.0.2.2 action drop
69 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
70 -t ip -q
72 tc_check_packets "dev $h2 ingress" 101 1
73 check_fail $? "Matched without redirect rule inserted"
75 tc filter add dev $swp1 ingress protocol $protocol pref 1 handle 101 \
76 $classifier $tcflags $classifier_args \
77 action mirred egress $action dev $swp2
79 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
80 -t ip -q
82 tc_check_packets "dev $h2 ingress" 101 1
83 check_err $? "Did not match incoming $action packet"
85 tc filter del dev $swp1 ingress protocol $protocol pref 1 handle 101 \
86 $classifier
87 tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
89 log_test "mirred egress $classifier $action ($tcflags)"
92 gact_drop_and_ok_test()
94 RET=0
96 tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
97 $tcflags dst_ip 192.0.2.2 action drop
99 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
100 -t ip -q
102 tc_check_packets "dev $swp1 ingress" 102 1
103 check_err $? "Packet was not dropped"
105 tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
106 $tcflags dst_ip 192.0.2.2 action ok
108 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
109 -t ip -q
111 tc_check_packets "dev $swp1 ingress" 101 1
112 check_err $? "Did not see passed packet"
114 tc_check_packets "dev $swp1 ingress" 102 2
115 check_fail $? "Packet was dropped and it should not reach here"
117 tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
118 tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
120 log_test "gact drop and ok ($tcflags)"
123 gact_trap_test()
125 RET=0
127 if [[ "$tcflags" != "skip_sw" ]]; then
128 return 0;
131 tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
132 skip_hw dst_ip 192.0.2.2 action drop
133 tc filter add dev $swp1 ingress protocol ip pref 3 handle 103 flower \
134 $tcflags dst_ip 192.0.2.2 action mirred egress redirect \
135 dev $swp2
137 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
138 -t ip -q
140 tc_check_packets "dev $swp1 ingress" 101 1
141 check_fail $? "Saw packet without trap rule inserted"
143 tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
144 $tcflags dst_ip 192.0.2.2 action trap
146 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
147 -t ip -q
149 tc_check_packets "dev $swp1 ingress" 102 1
150 check_err $? "Packet was not trapped"
152 tc_check_packets "dev $swp1 ingress" 101 1
153 check_err $? "Did not see trapped packet"
155 tc filter del dev $swp1 ingress protocol ip pref 3 handle 103 flower
156 tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
157 tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
159 log_test "trap ($tcflags)"
162 mirred_egress_to_ingress_test()
164 RET=0
166 tc filter add dev $h1 protocol ip pref 100 handle 100 egress flower \
167 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 8 action \
168 ct commit nat src addr 192.0.2.2 pipe \
169 ct clear pipe \
170 ct commit nat dst addr 192.0.2.1 pipe \
171 mirred ingress redirect dev $h1
173 tc filter add dev $swp1 protocol ip pref 11 handle 111 ingress flower \
174 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 8 action drop
175 tc filter add dev $swp1 protocol ip pref 12 handle 112 ingress flower \
176 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 0 action pass
178 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
179 -t icmp "ping,id=42,seq=10" -q
181 tc_check_packets "dev $h1 egress" 100 1
182 check_err $? "didn't mirror first packet"
184 tc_check_packets "dev $swp1 ingress" 111 1
185 check_fail $? "didn't redirect first packet"
186 tc_check_packets "dev $swp1 ingress" 112 1
187 check_err $? "didn't receive reply to first packet"
189 ping 192.0.2.2 -I$h1 -c1 -w1 -q 1>/dev/null 2>&1
191 tc_check_packets "dev $h1 egress" 100 2
192 check_err $? "didn't mirror second packet"
193 tc_check_packets "dev $swp1 ingress" 111 1
194 check_fail $? "didn't redirect second packet"
195 tc_check_packets "dev $swp1 ingress" 112 2
196 check_err $? "didn't receive reply to second packet"
198 tc filter del dev $h1 egress protocol ip pref 100 handle 100 flower
199 tc filter del dev $swp1 ingress protocol ip pref 11 handle 111 flower
200 tc filter del dev $swp1 ingress protocol ip pref 12 handle 112 flower
202 log_test "mirred_egress_to_ingress ($tcflags)"
205 mirred_egress_to_ingress_tcp_test()
207 mirred_e2i_tf1=$(mktemp) mirred_e2i_tf2=$(mktemp)
209 RET=0
210 dd conv=sparse status=none if=/dev/zero bs=1M count=2 of=$mirred_e2i_tf1
211 tc filter add dev $h1 protocol ip pref 100 handle 100 egress flower \
212 $tcflags ip_proto tcp src_ip 192.0.2.1 dst_ip 192.0.2.2 \
213 action ct commit nat src addr 192.0.2.2 pipe \
214 action ct clear pipe \
215 action ct commit nat dst addr 192.0.2.1 pipe \
216 action ct clear pipe \
217 action skbedit ptype host pipe \
218 action mirred ingress redirect dev $h1
219 tc filter add dev $h1 protocol ip pref 101 handle 101 egress flower \
220 $tcflags ip_proto icmp \
221 action mirred ingress redirect dev $h1
222 tc filter add dev $h1 protocol ip pref 102 handle 102 ingress flower \
223 ip_proto icmp \
224 action drop
226 ip vrf exec v$h1 ncat --recv-only -w10 -l -p 12345 -o $mirred_e2i_tf2 &
227 local rpid=$!
228 ip vrf exec v$h1 ncat -w1 --send-only 192.0.2.2 12345 <$mirred_e2i_tf1
229 wait -n $rpid
230 cmp -s $mirred_e2i_tf1 $mirred_e2i_tf2
231 check_err $? "server output check failed"
233 $MZ $h1 -c 10 -p 64 -a $h1mac -b $h1mac -A 192.0.2.1 -B 192.0.2.1 \
234 -t icmp "ping,id=42,seq=5" -q
235 tc_check_packets "dev $h1 egress" 101 10
236 check_err $? "didn't mirred redirect ICMP"
237 tc_check_packets "dev $h1 ingress" 102 10
238 check_err $? "didn't drop mirred ICMP"
240 tc filter del dev $h1 egress protocol ip pref 100 handle 100 flower
241 tc filter del dev $h1 egress protocol ip pref 101 handle 101 flower
242 tc filter del dev $h1 ingress protocol ip pref 102 handle 102 flower
244 rm -f $mirred_e2i_tf1 $mirred_e2i_tf2
245 log_test "mirred_egress_to_ingress_tcp ($tcflags)"
248 ingress_2nd_vlan_push()
250 tc filter add dev $swp1 ingress pref 20 chain 0 handle 20 flower \
251 $tcflags num_of_vlans 1 \
252 action vlan push id 100 protocol 0x8100 action goto chain 5
253 tc filter add dev $swp1 ingress pref 30 chain 5 handle 30 flower \
254 $tcflags num_of_vlans 2 \
255 cvlan_ethtype 0x800 action pass
257 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
258 -t ip -Q 10 -q
260 tc_check_packets "dev $swp1 ingress" 30 1
261 check_err $? "No double-vlan packets received"
263 tc filter del dev $swp1 ingress pref 20 chain 0 handle 20 flower
264 tc filter del dev $swp1 ingress pref 30 chain 5 handle 30 flower
266 log_test "ingress_2nd_vlan_push ($tcflags)"
269 egress_2nd_vlan_push()
271 tc filter add dev $h1 egress pref 20 chain 0 handle 20 flower \
272 $tcflags num_of_vlans 0 \
273 action vlan push id 10 protocol 0x8100 \
274 pipe action vlan push id 100 protocol 0x8100 action goto chain 5
275 tc filter add dev $h1 egress pref 30 chain 5 handle 30 flower \
276 $tcflags num_of_vlans 2 \
277 cvlan_ethtype 0x800 action pass
279 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
280 -t ip -q
282 tc_check_packets "dev $h1 egress" 30 1
283 check_err $? "No double-vlan packets received"
285 tc filter del dev $h1 egress pref 20 chain 0 handle 20 flower
286 tc filter del dev $h1 egress pref 30 chain 5 handle 30 flower
288 log_test "egress_2nd_vlan_push ($tcflags)"
291 setup_prepare()
293 h1=${NETIFS[p1]}
294 swp1=${NETIFS[p2]}
296 swp2=${NETIFS[p3]}
297 h2=${NETIFS[p4]}
299 h1mac=$(mac_get $h1)
300 h2mac=$(mac_get $h2)
302 swp1origmac=$(mac_get $swp1)
303 swp2origmac=$(mac_get $swp2)
304 ip link set $swp1 address $h2mac
305 ip link set $swp2 address $h1mac
307 vrf_prepare
309 h1_create
310 h2_create
311 switch_create
314 cleanup()
316 local tf
318 pre_cleanup
320 switch_destroy
321 h2_destroy
322 h1_destroy
324 vrf_cleanup
326 ip link set $swp2 address $swp2origmac
327 ip link set $swp1 address $swp1origmac
329 for tf in $mirred_e2i_tf1 $mirred_e2i_tf2; do rm -f $tf; done
332 mirred_egress_redirect_test()
334 mirred_egress_test "redirect" "ip" "flower" "dst_ip 192.0.2.2"
337 mirred_egress_mirror_test()
339 mirred_egress_test "mirror" "ip" "flower" "dst_ip 192.0.2.2"
342 matchall_mirred_egress_mirror_test()
344 mirred_egress_test "mirror" "all" "matchall" ""
347 trap cleanup EXIT
349 setup_prepare
350 setup_wait
352 tests_run
354 tc_offload_check
355 if [[ $? -ne 0 ]]; then
356 log_info "Could not test offloaded functionality"
357 else
358 tcflags="skip_sw"
359 tests_run
362 exit $EXIT_STATUS