1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ROUTER_NUM_NETIFS=4
5: ${TIMEOUT:=20000} # ms
6
7router_h1_create()
8{
9	simple_if_init $h1 192.0.1.1/24
10}
11
12router_h1_destroy()
13{
14	simple_if_fini $h1 192.0.1.1/24
15}
16
17router_h2_create()
18{
19	simple_if_init $h2 192.0.2.1/24
20	tc qdisc add dev $h2 handle ffff: ingress
21}
22
23router_h2_destroy()
24{
25	tc qdisc del dev $h2 handle ffff: ingress
26	simple_if_fini $h2 192.0.2.1/24
27}
28
29router_create()
30{
31	ip link set dev $rp1 up
32	ip link set dev $rp2 up
33
34	ip address add 192.0.1.2/24 dev $rp1
35	ip address add 192.0.2.2/24 dev $rp2
36}
37
38router_destroy()
39{
40	ip address del 192.0.2.2/24 dev $rp2
41	ip address del 192.0.1.2/24 dev $rp1
42
43	ip link set dev $rp2 down
44	ip link set dev $rp1 down
45}
46
47router_setup_prepare()
48{
49	h1=${NETIFS[p1]}
50	rp1=${NETIFS[p2]}
51
52	rp2=${NETIFS[p3]}
53	h2=${NETIFS[p4]}
54
55	h1mac=$(mac_get $h1)
56	rp1mac=$(mac_get $rp1)
57
58	vrf_prepare
59
60	router_h1_create
61	router_h2_create
62
63	router_create
64}
65
66wait_for_routes()
67{
68	local t0=$1; shift
69	local route_count=$1; shift
70
71	local t1=$(ip route | grep -o 'offload' | wc -l)
72	local delta=$((t1 - t0))
73	echo $delta
74	[[ $delta -ge $route_count ]]
75}
76
77router_routes_create()
78{
79	local route_count=$1
80	local count=0
81
82	ROUTE_FILE="$(mktemp)"
83
84	for i in {0..255}
85	do
86		for j in {0..255}
87		do
88			for k in {0..255}
89			do
90				if [[ $count -eq $route_count ]]; then
91					break 3
92				fi
93
94				echo route add 193.${i}.${j}.${k}/32 dev $rp2 \
95					>> $ROUTE_FILE
96				((count++))
97			done
98		done
99	done
100
101	ip -b $ROUTE_FILE &> /dev/null
102}
103
104router_routes_destroy()
105{
106	if [[ -v ROUTE_FILE ]]; then
107		rm -f $ROUTE_FILE
108	fi
109}
110
111router_test()
112{
113	local route_count=$1
114	local should_fail=$2
115	local delta
116
117	RET=0
118
119	local t0=$(ip route | grep -o 'offload' | wc -l)
120	router_routes_create $route_count
121	delta=$(busywait "$TIMEOUT" wait_for_routes $t0 $route_count)
122
123	check_err_fail $should_fail $? "Offload routes: Expected $route_count, got $delta."
124	if [[ $RET -ne 0 ]] || [[ $should_fail -eq 1 ]]; then
125		return
126	fi
127
128	router_routes_destroy
129}
130
131router_cleanup()
132{
133	pre_cleanup
134
135	router_routes_destroy
136	router_destroy
137
138	router_h2_destroy
139	router_h1_destroy
140
141	vrf_cleanup
142}
143