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