1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test traffic distribution when a wECMP route forwards traffic to two GRE
5# tunnels.
6#
7# +-------------------------+
8# | H1                      |
9# |               $h1 +     |
10# |      192.0.2.1/28 |     |
11# |  2001:db8:1::1/64 |     |
12# +-------------------|-----+
13#                     |
14# +-------------------|------------------------+
15# | SW1               |                        |
16# |              $ol1 +                        |
17# |      192.0.2.2/28                          |
18# |  2001:db8:1::2/64                          |
19# |                                            |
20# |  + g1a (gre)          + g1b (gre)          |
21# |    loc=192.0.2.65       loc=192.0.2.81     |
22# |    rem=192.0.2.66 --.   rem=192.0.2.82 --. |
23# |    tos=inherit      |   tos=inherit      | |
24# |  .------------------'                    | |
25# |  |                    .------------------' |
26# |  v                    v                    |
27# |  + $ul1.111 (vlan)    + $ul1.222 (vlan)    |
28# |  | 192.0.2.129/28     | 192.0.2.145/28     |
29# |   \                  /                     |
30# |    \________________/                      |
31# |            |                               |
32# |            + $ul1                          |
33# +------------|-------------------------------+
34#              |
35# +------------|-------------------------------+
36# | SW2        + $ul2                          |
37# |     _______|________                       |
38# |    /                \                      |
39# |   /                  \                     |
40# |  + $ul2.111 (vlan)    + $ul2.222 (vlan)    |
41# |  ^ 192.0.2.130/28     ^ 192.0.2.146/28     |
42# |  |                    |                    |
43# |  |                    '------------------. |
44# |  '------------------.                    | |
45# |  + g2a (gre)        | + g2b (gre)        | |
46# |    loc=192.0.2.66   |   loc=192.0.2.82   | |
47# |    rem=192.0.2.65 --'   rem=192.0.2.81 --' |
48# |    tos=inherit          tos=inherit        |
49# |                                            |
50# |              $ol2 +                        |
51# |     192.0.2.17/28 |                        |
52# |  2001:db8:2::1/64 |                        |
53# +-------------------|------------------------+
54#                     |
55# +-------------------|-----+
56# | H2                |     |
57# |               $h2 +     |
58# |     192.0.2.18/28       |
59# |  2001:db8:2::2/64       |
60# +-------------------------+
61
62ALL_TESTS="
63	ping_ipv4
64	ping_ipv6
65	multipath_ipv4
66	multipath_ipv6
67"
68
69NUM_NETIFS=6
70source lib.sh
71
72h1_create()
73{
74	simple_if_init $h1 192.0.2.1/28 2001:db8:1::1/64
75	ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
76	ip route add vrf v$h1 2001:db8:2::/64 via 2001:db8:1::2
77}
78
79h1_destroy()
80{
81	ip route del vrf v$h1 2001:db8:2::/64 via 2001:db8:1::2
82	ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
83	simple_if_fini $h1 192.0.2.1/28
84}
85
86sw1_create()
87{
88	simple_if_init $ol1 192.0.2.2/28 2001:db8:1::2/64
89	__simple_if_init $ul1 v$ol1
90	vlan_create $ul1 111 v$ol1 192.0.2.129/28
91	vlan_create $ul1 222 v$ol1 192.0.2.145/28
92
93	tunnel_create g1a gre 192.0.2.65 192.0.2.66 tos inherit dev v$ol1
94	__simple_if_init g1a v$ol1 192.0.2.65/32
95	ip route add vrf v$ol1 192.0.2.66/32 via 192.0.2.130
96
97	tunnel_create g1b gre 192.0.2.81 192.0.2.82 tos inherit dev v$ol1
98	__simple_if_init g1b v$ol1 192.0.2.81/32
99	ip route add vrf v$ol1 192.0.2.82/32 via 192.0.2.146
100
101	ip -6 nexthop add id 101 dev g1a
102	ip -6 nexthop add id 102 dev g1b
103	ip nexthop add id 103 group 101/102
104
105	ip route add vrf v$ol1 192.0.2.16/28 nhid 103
106	ip route add vrf v$ol1 2001:db8:2::/64 nhid 103
107}
108
109sw1_destroy()
110{
111	ip route del vrf v$ol1 2001:db8:2::/64
112	ip route del vrf v$ol1 192.0.2.16/28
113
114	ip nexthop del id 103
115	ip -6 nexthop del id 102
116	ip -6 nexthop del id 101
117
118	ip route del vrf v$ol1 192.0.2.82/32 via 192.0.2.146
119	__simple_if_fini g1b 192.0.2.81/32
120	tunnel_destroy g1b
121
122	ip route del vrf v$ol1 192.0.2.66/32 via 192.0.2.130
123	__simple_if_fini g1a 192.0.2.65/32
124	tunnel_destroy g1a
125
126	vlan_destroy $ul1 222
127	vlan_destroy $ul1 111
128	__simple_if_fini $ul1
129	simple_if_fini $ol1 192.0.2.2/28 2001:db8:1::2/64
130}
131
132sw2_create()
133{
134	simple_if_init $ol2 192.0.2.17/28 2001:db8:2::1/64
135	__simple_if_init $ul2 v$ol2
136	vlan_create $ul2 111 v$ol2 192.0.2.130/28
137	vlan_create $ul2 222 v$ol2 192.0.2.146/28
138
139	tunnel_create g2a gre 192.0.2.66 192.0.2.65 tos inherit dev v$ol2
140	__simple_if_init g2a v$ol2 192.0.2.66/32
141	ip route add vrf v$ol2 192.0.2.65/32 via 192.0.2.129
142
143	tunnel_create g2b gre 192.0.2.82 192.0.2.81 tos inherit dev v$ol2
144	__simple_if_init g2b v$ol2 192.0.2.82/32
145	ip route add vrf v$ol2 192.0.2.81/32 via 192.0.2.145
146
147	ip -6 nexthop add id 201 dev g2a
148	ip -6 nexthop add id 202 dev g2b
149	ip nexthop add id 203 group 201/202
150
151	ip route add vrf v$ol2 192.0.2.0/28 nhid 203
152	ip route add vrf v$ol2 2001:db8:1::/64 nhid 203
153
154	tc qdisc add dev $ul2 clsact
155	tc filter add dev $ul2 ingress pref 111 prot 802.1Q \
156	   flower vlan_id 111 action pass
157	tc filter add dev $ul2 ingress pref 222 prot 802.1Q \
158	   flower vlan_id 222 action pass
159}
160
161sw2_destroy()
162{
163	tc qdisc del dev $ul2 clsact
164
165	ip route del vrf v$ol2 2001:db8:1::/64
166	ip route del vrf v$ol2 192.0.2.0/28
167
168	ip nexthop del id 203
169	ip -6 nexthop del id 202
170	ip -6 nexthop del id 201
171
172	ip route del vrf v$ol2 192.0.2.81/32 via 192.0.2.145
173	__simple_if_fini g2b 192.0.2.82/32
174	tunnel_destroy g2b
175
176	ip route del vrf v$ol2 192.0.2.65/32 via 192.0.2.129
177	__simple_if_fini g2a 192.0.2.66/32
178	tunnel_destroy g2a
179
180	vlan_destroy $ul2 222
181	vlan_destroy $ul2 111
182	__simple_if_fini $ul2
183	simple_if_fini $ol2 192.0.2.17/28 2001:db8:2::1/64
184}
185
186h2_create()
187{
188	simple_if_init $h2 192.0.2.18/28 2001:db8:2::2/64
189	ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
190	ip route add vrf v$h2 2001:db8:1::/64 via 2001:db8:2::1
191}
192
193h2_destroy()
194{
195	ip route del vrf v$h2 2001:db8:1::/64 via 2001:db8:2::1
196	ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
197	simple_if_fini $h2 192.0.2.18/28 2001:db8:2::2/64
198}
199
200setup_prepare()
201{
202	h1=${NETIFS[p1]}
203	ol1=${NETIFS[p2]}
204
205	ul1=${NETIFS[p3]}
206	ul2=${NETIFS[p4]}
207
208	ol2=${NETIFS[p5]}
209	h2=${NETIFS[p6]}
210
211	vrf_prepare
212	h1_create
213	sw1_create
214	sw2_create
215	h2_create
216
217	forwarding_enable
218}
219
220cleanup()
221{
222	pre_cleanup
223
224	forwarding_restore
225
226	h2_destroy
227	sw2_destroy
228	sw1_destroy
229	h1_destroy
230	vrf_cleanup
231}
232
233multipath4_test()
234{
235	local what=$1; shift
236	local weight1=$1; shift
237	local weight2=$1; shift
238
239	sysctl_set net.ipv4.fib_multipath_hash_policy 1
240	ip nexthop replace id 103 group 101,$weight1/102,$weight2
241
242	local t0_111=$(tc_rule_stats_get $ul2 111 ingress)
243	local t0_222=$(tc_rule_stats_get $ul2 222 ingress)
244
245	ip vrf exec v$h1 \
246	   $MZ $h1 -q -p 64 -A 192.0.2.1 -B 192.0.2.18 \
247	       -d $MZ_DELAY -t udp "sp=1024,dp=0-32768"
248
249	local t1_111=$(tc_rule_stats_get $ul2 111 ingress)
250	local t1_222=$(tc_rule_stats_get $ul2 222 ingress)
251
252	local d111=$((t1_111 - t0_111))
253	local d222=$((t1_222 - t0_222))
254	multipath_eval "$what" $weight1 $weight2 $d111 $d222
255
256	ip nexthop replace id 103 group 101/102
257	sysctl_restore net.ipv4.fib_multipath_hash_policy
258}
259
260multipath6_test()
261{
262	local what=$1; shift
263	local weight1=$1; shift
264	local weight2=$1; shift
265
266	sysctl_set net.ipv6.fib_multipath_hash_policy 1
267	ip nexthop replace id 103 group 101,$weight1/102,$weight2
268
269	local t0_111=$(tc_rule_stats_get $ul2 111 ingress)
270	local t0_222=$(tc_rule_stats_get $ul2 222 ingress)
271
272	ip vrf exec v$h1 \
273		$MZ $h1 -6 -q -p 64 -A 2001:db8:1::1 -B 2001:db8:2::2 \
274		-d $MZ_DELAY -t udp "sp=1024,dp=0-32768"
275
276	local t1_111=$(tc_rule_stats_get $ul2 111 ingress)
277	local t1_222=$(tc_rule_stats_get $ul2 222 ingress)
278
279	local d111=$((t1_111 - t0_111))
280	local d222=$((t1_222 - t0_222))
281	multipath_eval "$what" $weight1 $weight2 $d111 $d222
282
283	ip nexthop replace id 103 group 101/102
284	sysctl_restore net.ipv6.fib_multipath_hash_policy
285}
286
287ping_ipv4()
288{
289	ping_test $h1 192.0.2.18
290}
291
292ping_ipv6()
293{
294	ping6_test $h1 2001:db8:2::2
295}
296
297multipath_ipv4()
298{
299	log_info "Running IPv4 multipath tests"
300	multipath4_test "ECMP" 1 1
301	multipath4_test "Weighted MP 2:1" 2 1
302	multipath4_test "Weighted MP 11:45" 11 45
303}
304
305multipath_ipv6()
306{
307	log_info "Running IPv6 multipath tests"
308	multipath6_test "ECMP" 1 1
309	multipath6_test "Weighted MP 2:1" 2 1
310	multipath6_test "Weighted MP 11:45" 11 45
311}
312
313trap cleanup EXIT
314
315setup_prepare
316setup_wait
317tests_run
318
319exit $EXIT_STATUS
320