xref: /linux/tools/testing/selftests/net/forwarding/tc_tunnel_key.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="tunnel_key_nofrag_test"
5
6NUM_NETIFS=4
7source tc_common.sh
8source lib.sh
9
10tcflags="skip_hw"
11
12h1_create()
13{
14	simple_if_init $h1 192.0.2.1/24
15	forwarding_enable
16	mtu_set $h1 1500
17	tunnel_create h1-et vxlan 192.0.2.1 192.0.2.2 dev $h1 dstport 0 external
18	tc qdisc add dev h1-et clsact
19	mtu_set h1-et 1230
20	mtu_restore $h1
21	mtu_set $h1 1000
22}
23
24h1_destroy()
25{
26	tc qdisc del dev h1-et clsact
27	tunnel_destroy h1-et
28	forwarding_restore
29	mtu_restore $h1
30	simple_if_fini $h1 192.0.2.1/24
31}
32
33h2_create()
34{
35	simple_if_init $h2 192.0.2.2/24
36}
37
38h2_destroy()
39{
40	simple_if_fini $h2 192.0.2.2/24
41}
42
43switch_create()
44{
45	simple_if_init $swp1 192.0.2.2/24
46	tc qdisc add dev $swp1 clsact
47	simple_if_init $swp2 192.0.2.1/24
48}
49
50switch_destroy()
51{
52	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
55}
56
57setup_prepare()
58{
59	h1=${NETIFS[p1]}
60	swp1=${NETIFS[p2]}
61
62	swp2=${NETIFS[p3]}
63	h2=${NETIFS[p4]}
64
65	h1mac=$(mac_get $h1)
66	h2mac=$(mac_get $h2)
67
68	swp1origmac=$(mac_get $swp1)
69	swp2origmac=$(mac_get $swp2)
70	ip link set $swp1 address $h2mac
71	ip link set $swp2 address $h1mac
72
73	vrf_prepare
74
75	h1_create
76	h2_create
77	switch_create
78
79	if ! tc action add action tunnel_key help 2>&1 | grep -q nofrag; then
80		log_test "SKIP: iproute doesn't support nofrag"
81		exit $ksft_skip
82	fi
83}
84
85cleanup()
86{
87	pre_cleanup
88
89	switch_destroy
90	h2_destroy
91	h1_destroy
92
93	vrf_cleanup
94
95	ip link set $swp2 address $swp2origmac
96	ip link set $swp1 address $swp1origmac
97}
98
99tunnel_key_nofrag_test()
100{
101	RET=0
102	local i
103
104	tc filter add dev $swp1 ingress protocol ip pref 100 handle 100 \
105		flower src_ip 192.0.2.1 dst_ip 192.0.2.2 ip_proto udp \
106		ip_flags nofrag action drop
107	tc filter add dev $swp1 ingress protocol ip pref 101 handle 101 \
108		flower src_ip 192.0.2.1 dst_ip 192.0.2.2 ip_proto udp \
109		ip_flags firstfrag action drop
110	tc filter add dev $swp1 ingress protocol ip pref 102 handle 102 \
111		flower src_ip 192.0.2.1 dst_ip 192.0.2.2 ip_proto udp \
112		ip_flags nofirstfrag action drop
113
114	# test 'nofrag' set
115	tc filter add dev h1-et egress protocol all pref 1 handle 1 matchall $tcflags \
116		action tunnel_key set src_ip 192.0.2.1 dst_ip 192.0.2.2 id 42 nofrag index 10
117	$MZ h1-et -c 1 -p 930 -a 00:aa:bb:cc:dd:ee -b 00:ee:dd:cc:bb:aa -t ip -q
118	tc_check_packets "dev $swp1 ingress" 100 1
119	check_err $? "packet smaller than MTU was not tunneled"
120
121	$MZ h1-et -c 1 -p 931 -a 00:aa:bb:cc:dd:ee -b 00:ee:dd:cc:bb:aa -t ip -q
122	tc_check_packets "dev $swp1 ingress" 100 1
123	check_err $? "packet bigger than MTU matched nofrag (nofrag was set)"
124	tc_check_packets "dev $swp1 ingress" 101 0
125	check_err $? "packet bigger than MTU matched firstfrag (nofrag was set)"
126	tc_check_packets "dev $swp1 ingress" 102 0
127	check_err $? "packet bigger than MTU matched nofirstfrag (nofrag was set)"
128
129	# test 'nofrag' cleared
130	tc actions change action tunnel_key set src_ip 192.0.2.1 dst_ip 192.0.2.2 id 42 index 10
131	$MZ h1-et -c 1 -p 931 -a 00:aa:bb:cc:dd:ee -b 00:ee:dd:cc:bb:aa -t ip -q
132	tc_check_packets "dev $swp1  ingress" 100 1
133	check_err $? "packet bigger than MTU matched nofrag (nofrag was unset)"
134	tc_check_packets "dev $swp1  ingress" 101 1
135	check_err $? "packet bigger than MTU didn't match firstfrag (nofrag was unset) "
136	tc_check_packets "dev $swp1 ingress" 102 1
137	check_err $? "packet bigger than MTU didn't match nofirstfrag (nofrag was unset) "
138
139	for i in 100 101 102; do
140		tc filter del dev $swp1 ingress protocol ip pref $i handle $i flower
141	done
142	tc filter del dev h1-et egress pref 1 handle 1 matchall
143
144	log_test "tunnel_key nofrag ($tcflags)"
145}
146
147trap cleanup EXIT
148
149setup_prepare
150setup_wait
151
152tests_run
153
154tc_offload_check
155if [[ $? -ne 0 ]]; then
156	log_info "Could not test offloaded functionality"
157else
158	tcflags="skip_sw"
159	tests_run
160fi
161
162exit $EXIT_STATUS
163