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"
16 simple_if_init $h1 192.0.2.1/24
17 tc qdisc add dev $h1 clsact
22 tc qdisc del dev $h1 clsact
23 simple_if_fini $h1 192.0.2.1/24
28 simple_if_init $h2 192.0.2.2/24
29 tc qdisc add dev $h2 clsact
34 tc qdisc del dev $h2 clsact
35 simple_if_fini $h2 192.0.2.2/24
40 simple_if_init $swp1 192.0.2.2/24
41 tc qdisc add dev $swp1 clsact
43 simple_if_init $swp2 192.0.2.1/24
48 simple_if_fini $swp2 192.0.2.1/24
50 tc qdisc del dev $swp1 clsact
51 simple_if_fini $swp1 192.0.2.2/24
59 local classifier_args=$4
63 tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
64 dst_ip 192.0.2.2 action drop
66 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
69 tc_check_packets "dev $h2 ingress" 101 1
70 check_fail $? "Matched without redirect rule inserted"
72 tc filter add dev $swp1 ingress protocol $protocol pref 1 handle 101 \
73 $classifier $tcflags $classifier_args \
74 action mirred egress $action dev $swp2
76 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
79 tc_check_packets "dev $h2 ingress" 101 1
80 check_err $? "Did not match incoming $action packet"
82 tc filter del dev $swp1 ingress protocol $protocol pref 1 handle 101 \
84 tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
86 log_test "mirred egress $classifier $action ($tcflags)"
89 gact_drop_and_ok_test()
93 tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
94 $tcflags dst_ip 192.0.2.2 action drop
96 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
99 tc_check_packets "dev $swp1 ingress" 102 1
100 check_err $? "Packet was not dropped"
102 tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
103 $tcflags dst_ip 192.0.2.2 action ok
105 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
108 tc_check_packets "dev $swp1 ingress" 101 1
109 check_err $? "Did not see passed packet"
111 tc_check_packets "dev $swp1 ingress" 102 2
112 check_fail $? "Packet was dropped and it should not reach here"
114 tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
115 tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
117 log_test "gact drop and ok ($tcflags)"
124 if [[ "$tcflags" != "skip_sw" ]]; then
128 tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
129 skip_hw dst_ip 192.0.2.2 action drop
130 tc filter add dev $swp1 ingress protocol ip pref 3 handle 103 flower \
131 $tcflags dst_ip 192.0.2.2 action mirred egress redirect \
134 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
137 tc_check_packets "dev $swp1 ingress" 101 1
138 check_fail $? "Saw packet without trap rule inserted"
140 tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
141 $tcflags dst_ip 192.0.2.2 action trap
143 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
146 tc_check_packets "dev $swp1 ingress" 102 1
147 check_err $? "Packet was not trapped"
149 tc_check_packets "dev $swp1 ingress" 101 1
150 check_err $? "Did not see trapped packet"
152 tc filter del dev $swp1 ingress protocol ip pref 3 handle 103 flower
153 tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
154 tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
156 log_test "trap ($tcflags)"
159 mirred_egress_to_ingress_test()
163 tc filter add dev $h1 protocol ip pref 100 handle 100 egress flower \
164 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 8 action \
165 ct commit nat src addr 192.0.2.2 pipe \
167 ct commit nat dst addr 192.0.2.1 pipe \
168 mirred ingress redirect dev $h1
170 tc filter add dev $swp1 protocol ip pref 11 handle 111 ingress flower \
171 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 8 action drop
172 tc filter add dev $swp1 protocol ip pref 12 handle 112 ingress flower \
173 ip_proto icmp src_ip 192.0.2.1 dst_ip 192.0.2.2 type 0 action pass
175 $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
176 -t icmp "ping,id=42,seq=10" -q
178 tc_check_packets "dev $h1 egress" 100 1
179 check_err $? "didn't mirror first packet"
181 tc_check_packets "dev $swp1 ingress" 111 1
182 check_fail $? "didn't redirect first packet"
183 tc_check_packets "dev $swp1 ingress" 112 1
184 check_err $? "didn't receive reply to first packet"
186 ping 192.0.2.2 -I$h1 -c1 -w1 -q 1>/dev/null 2>&1
188 tc_check_packets "dev $h1 egress" 100 2
189 check_err $? "didn't mirror second packet"
190 tc_check_packets "dev $swp1 ingress" 111 1
191 check_fail $? "didn't redirect second packet"
192 tc_check_packets "dev $swp1 ingress" 112 2
193 check_err $? "didn't receive reply to second packet"
195 tc filter del dev $h1 egress protocol ip pref 100 handle 100 flower
196 tc filter del dev $swp1 ingress protocol ip pref 11 handle 111 flower
197 tc filter del dev $swp1 ingress protocol ip pref 12 handle 112 flower
199 log_test "mirred_egress_to_ingress ($tcflags)"
202 mirred_egress_to_ingress_tcp_test()
204 local tmpfile=$(mktemp) tmpfile1=$(mktemp)
207 dd conv=sparse status=none if=/dev/zero bs=1M count=2 of=$tmpfile
208 tc filter add dev $h1 protocol ip pref 100 handle 100 egress flower \
209 $tcflags ip_proto tcp src_ip 192.0.2.1 dst_ip 192.0.2.2 \
210 action ct commit nat src addr 192.0.2.2 pipe \
211 action ct clear pipe \
212 action ct commit nat dst addr 192.0.2.1 pipe \
213 action ct clear pipe \
214 action skbedit ptype host pipe \
215 action mirred ingress redirect dev $h1
216 tc filter add dev $h1 protocol ip pref 101 handle 101 egress flower \
217 $tcflags ip_proto icmp \
218 action mirred ingress redirect dev $h1
219 tc filter add dev $h1 protocol ip pref 102 handle 102 ingress flower \
223 ip vrf exec v$h1 nc --recv-only -w10 -l -p 12345 -o $tmpfile1 &
225 ip vrf exec v$h1 nc -w1 --send-only 192.0.2.2 12345 <$tmpfile
227 cmp -s $tmpfile $tmpfile1
228 check_err $? "server output check failed"
230 $MZ $h1 -c 10 -p 64 -a $h1mac -b $h1mac -A 192.0.2.1 -B 192.0.2.1 \
231 -t icmp "ping,id=42,seq=5" -q
232 tc_check_packets "dev $h1 egress" 101 10
233 check_err $? "didn't mirred redirect ICMP"
234 tc_check_packets "dev $h1 ingress" 102 10
235 check_err $? "didn't drop mirred ICMP"
236 local overlimits=$(tc_rule_stats_get ${h1} 101 egress .overlimits)
237 test ${overlimits} = 10
238 check_err $? "wrong overlimits, expected 10 got ${overlimits}"
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 $tmpfile $tmpfile1
245 log_test "mirred_egress_to_ingress_tcp ($tcflags)"
259 swp1origmac=$(mac_get $swp1)
260 swp2origmac=$(mac_get $swp2)
261 ip link set $swp1 address $h2mac
262 ip link set $swp2 address $h1mac
281 ip link set $swp2 address $swp2origmac
282 ip link set $swp1 address $swp1origmac
285 mirred_egress_redirect_test()
287 mirred_egress_test "redirect" "ip" "flower" "dst_ip 192.0.2.2"
290 mirred_egress_mirror_test()
292 mirred_egress_test "mirror" "ip" "flower" "dst_ip 192.0.2.2"
295 matchall_mirred_egress_mirror_test()
297 mirred_egress_test "mirror" "all" "matchall" ""
308 if [[ $? -ne 0 ]]; then
309 log_info "Could not test offloaded functionality"