1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test sends traffic from H1 to H2. Either on ingress of $swp1, or on egress of $swp2, the
5# traffic is acted upon by a pedit action. An ingress filter installed on $h2 verifies that the
6# packet looks like expected.
7#
8# +----------------------+                             +----------------------+
9# | H1                   |                             |                   H2 |
10# |    + $h1             |                             |            $h2 +     |
11# |    | 192.0.2.1/28    |                             |   192.0.2.2/28 |     |
12# +----|-----------------+                             +----------------|-----+
13#      |                                                                |
14# +----|----------------------------------------------------------------|-----+
15# | SW |                                                                |     |
16# |  +-|----------------------------------------------------------------|-+   |
17# |  | + $swp1                       BR                           $swp2 + |   |
18# |  +--------------------------------------------------------------------+   |
19# +---------------------------------------------------------------------------+
20
21ALL_TESTS="
22	ping_ipv4
23	test_udp_sport
24	test_udp_dport
25	test_tcp_sport
26	test_tcp_dport
27"
28
29NUM_NETIFS=4
30source lib.sh
31source tc_common.sh
32
33: ${HIT_TIMEOUT:=2000} # ms
34
35h1_create()
36{
37	simple_if_init $h1 192.0.2.1/28 2001:db8:1::1/64
38}
39
40h1_destroy()
41{
42	simple_if_fini $h1 192.0.2.1/28 2001:db8:1::1/64
43}
44
45h2_create()
46{
47	simple_if_init $h2 192.0.2.2/28 2001:db8:1::2/64
48	tc qdisc add dev $h2 clsact
49}
50
51h2_destroy()
52{
53	tc qdisc del dev $h2 clsact
54	simple_if_fini $h2 192.0.2.2/28 2001:db8:1::2/64
55}
56
57switch_create()
58{
59	ip link add name br1 up type bridge vlan_filtering 1
60	ip link set dev $swp1 master br1
61	ip link set dev $swp1 up
62	ip link set dev $swp2 master br1
63	ip link set dev $swp2 up
64
65	tc qdisc add dev $swp1 clsact
66	tc qdisc add dev $swp2 clsact
67}
68
69switch_destroy()
70{
71	tc qdisc del dev $swp2 clsact
72	tc qdisc del dev $swp1 clsact
73
74	ip link set dev $swp2 nomaster
75	ip link set dev $swp1 nomaster
76	ip link del dev br1
77}
78
79setup_prepare()
80{
81	h1=${NETIFS[p1]}
82	swp1=${NETIFS[p2]}
83
84	swp2=${NETIFS[p3]}
85	h2=${NETIFS[p4]}
86
87	h2mac=$(mac_get $h2)
88
89	vrf_prepare
90	h1_create
91	h2_create
92	switch_create
93}
94
95cleanup()
96{
97	pre_cleanup
98
99	switch_destroy
100	h2_destroy
101	h1_destroy
102	vrf_cleanup
103}
104
105ping_ipv4()
106{
107	ping_test $h1 192.0.2.2
108}
109
110ping_ipv6()
111{
112	ping6_test $h1 2001:db8:1::2
113}
114
115do_test_pedit_l4port_one()
116{
117	local pedit_locus=$1; shift
118	local pedit_prot=$1; shift
119	local pedit_action=$1; shift
120	local match_prot=$1; shift
121	local match_flower=$1; shift
122	local mz_flags=$1; shift
123	local saddr=$1; shift
124	local daddr=$1; shift
125
126	tc filter add $pedit_locus handle 101 pref 1 \
127	   flower action pedit ex munge $pedit_action
128	tc filter add dev $h2 ingress handle 101 pref 1 prot $match_prot \
129	   flower skip_hw $match_flower action pass
130
131	RET=0
132
133	$MZ $mz_flags $h1 -c 10 -d 20msec -p 100 \
134	    -a own -b $h2mac -q -t $pedit_prot sp=54321,dp=12345
135
136	local pkts
137	pkts=$(busywait "$TC_HIT_TIMEOUT" until_counter_is ">= 10" \
138			tc_rule_handle_stats_get "dev $h2 ingress" 101)
139	check_err $? "Expected to get 10 packets, but got $pkts."
140
141	pkts=$(tc_rule_handle_stats_get "$pedit_locus" 101)
142	((pkts >= 10))
143	check_err $? "Expected to get 10 packets on pedit rule, but got $pkts."
144
145	log_test "$pedit_locus pedit $pedit_action"
146
147	tc filter del dev $h2 ingress pref 1
148	tc filter del $pedit_locus pref 1
149}
150
151do_test_pedit_l4port()
152{
153	local locus=$1; shift
154	local prot=$1; shift
155	local pedit_port=$1; shift
156	local flower_port=$1; shift
157	local port
158
159	for port in 1 11111 65535; do
160		do_test_pedit_l4port_one "$locus" "$prot"			\
161					 "$prot $pedit_port set $port"		\
162					 ip "ip_proto $prot $flower_port $port"	\
163					 "-A 192.0.2.1 -B 192.0.2.2"
164	done
165}
166
167test_udp_sport()
168{
169	do_test_pedit_l4port "dev $swp1 ingress" udp sport src_port
170	do_test_pedit_l4port "dev $swp2 egress"  udp sport src_port
171}
172
173test_udp_dport()
174{
175	do_test_pedit_l4port "dev $swp1 ingress" udp dport dst_port
176	do_test_pedit_l4port "dev $swp2 egress"  udp dport dst_port
177}
178
179test_tcp_sport()
180{
181	do_test_pedit_l4port "dev $swp1 ingress" tcp sport src_port
182	do_test_pedit_l4port "dev $swp2 egress"  tcp sport src_port
183}
184
185test_tcp_dport()
186{
187	do_test_pedit_l4port "dev $swp1 ingress" tcp dport dst_port
188	do_test_pedit_l4port "dev $swp2 egress"  tcp dport dst_port
189}
190
191trap cleanup EXIT
192
193setup_prepare
194setup_wait
195
196tests_run
197
198exit $EXIT_STATUS
199