1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# This test is for checking the FIB offload API on top of mlxsw.
5
6lib_dir=$(dirname $0)/../../../net/forwarding
7
8ALL_TESTS="
9	ipv4_identical_routes
10	ipv4_tos
11	ipv4_metric
12	ipv4_replace
13	ipv4_delete
14	ipv4_plen
15	ipv4_replay
16	ipv4_flush
17	ipv4_local_replace
18	ipv6_add
19	ipv6_metric
20	ipv6_append_single
21	ipv6_replace_single
22	ipv6_metric_multipath
23	ipv6_append_multipath
24	ipv6_replace_multipath
25	ipv6_append_multipath_to_single
26	ipv6_delete_single
27	ipv6_delete_multipath
28	ipv6_replay_single
29	ipv6_replay_multipath
30	ipv6_local_replace
31"
32NUM_NETIFS=0
33source $lib_dir/lib.sh
34source $lib_dir/devlink_lib.sh
35source $lib_dir/fib_offload_lib.sh
36
37ipv4_identical_routes()
38{
39	fib_ipv4_identical_routes_test "testns1"
40}
41
42ipv4_tos()
43{
44	fib_ipv4_tos_test "testns1"
45}
46
47ipv4_metric()
48{
49	fib_ipv4_metric_test "testns1"
50}
51
52ipv4_replace()
53{
54	fib_ipv4_replace_test "testns1"
55}
56
57ipv4_delete()
58{
59	fib_ipv4_delete_test "testns1"
60}
61
62ipv4_plen()
63{
64	fib_ipv4_plen_test "testns1"
65}
66
67ipv4_replay_metric()
68{
69	fib_ipv4_replay_metric_test "testns1" "$DEVLINK_DEV"
70}
71
72ipv4_replay_tos()
73{
74	fib_ipv4_replay_tos_test "testns1" "$DEVLINK_DEV"
75}
76
77ipv4_replay_plen()
78{
79	fib_ipv4_replay_plen_test "testns1" "$DEVLINK_DEV"
80}
81
82ipv4_replay()
83{
84	ipv4_replay_metric
85	ipv4_replay_tos
86	ipv4_replay_plen
87}
88
89ipv4_flush()
90{
91	fib_ipv4_flush_test "testns1"
92}
93
94ipv4_local_replace()
95{
96	local ns="testns1"
97
98	RET=0
99
100	ip -n $ns link add name dummy1 type dummy
101	ip -n $ns link set dev dummy1 up
102
103	ip -n $ns route add table local 192.0.2.1/32 dev dummy1
104	fib4_trap_check $ns "table local 192.0.2.1/32 dev dummy1" false
105	check_err $? "Local table route not in hardware when should"
106
107	ip -n $ns route add table main 192.0.2.1/32 dev dummy1
108	fib4_trap_check $ns "table main 192.0.2.1/32 dev dummy1" true
109	check_err $? "Main table route in hardware when should not"
110
111	fib4_trap_check $ns "table local 192.0.2.1/32 dev dummy1" false
112	check_err $? "Local table route was replaced when should not"
113
114	# Test that local routes can replace routes in main table.
115	ip -n $ns route add table main 192.0.2.2/32 dev dummy1
116	fib4_trap_check $ns "table main 192.0.2.2/32 dev dummy1" false
117	check_err $? "Main table route not in hardware when should"
118
119	ip -n $ns route add table local 192.0.2.2/32 dev dummy1
120	fib4_trap_check $ns "table local 192.0.2.2/32 dev dummy1" false
121	check_err $? "Local table route did not replace route in main table when should"
122
123	fib4_trap_check $ns "table main 192.0.2.2/32 dev dummy1" true
124	check_err $? "Main table route was not replaced when should"
125
126	log_test "IPv4 local table route replacement"
127
128	ip -n $ns link del dev dummy1
129}
130
131ipv6_add()
132{
133	fib_ipv6_add_test "testns1"
134}
135
136ipv6_metric()
137{
138	fib_ipv6_metric_test "testns1"
139}
140
141ipv6_append_single()
142{
143	fib_ipv6_append_single_test "testns1"
144}
145
146ipv6_replace_single()
147{
148	fib_ipv6_replace_single_test "testns1"
149}
150
151ipv6_metric_multipath()
152{
153	fib_ipv6_metric_multipath_test "testns1"
154}
155
156ipv6_append_multipath()
157{
158	fib_ipv6_append_multipath_test "testns1"
159}
160
161ipv6_replace_multipath()
162{
163	fib_ipv6_replace_multipath_test "testns1"
164}
165
166ipv6_append_multipath_to_single()
167{
168	fib_ipv6_append_multipath_to_single_test "testns1"
169}
170
171ipv6_delete_single()
172{
173	fib_ipv6_delete_single_test "testns1"
174}
175
176ipv6_delete_multipath()
177{
178	fib_ipv6_delete_multipath_test "testns1"
179}
180
181ipv6_replay_single()
182{
183	fib_ipv6_replay_single_test "testns1" "$DEVLINK_DEV"
184}
185
186ipv6_replay_multipath()
187{
188	fib_ipv6_replay_multipath_test "testns1" "$DEVLINK_DEV"
189}
190
191ipv6_local_replace()
192{
193	local ns="testns1"
194
195	RET=0
196
197	ip -n $ns link add name dummy1 type dummy
198	ip -n $ns link set dev dummy1 up
199
200	ip -n $ns route add table local 2001:db8:1::1/128 dev dummy1
201	fib6_trap_check $ns "table local 2001:db8:1::1/128 dev dummy1" false
202	check_err $? "Local table route not in hardware when should"
203
204	ip -n $ns route add table main 2001:db8:1::1/128 dev dummy1
205	fib6_trap_check $ns "table main 2001:db8:1::1/128 dev dummy1" true
206	check_err $? "Main table route in hardware when should not"
207
208	fib6_trap_check $ns "table local 2001:db8:1::1/128 dev dummy1" false
209	check_err $? "Local table route was replaced when should not"
210
211	# Test that local routes can replace routes in main table.
212	ip -n $ns route add table main 2001:db8:1::2/128 dev dummy1
213	fib6_trap_check $ns "table main 2001:db8:1::2/128 dev dummy1" false
214	check_err $? "Main table route not in hardware when should"
215
216	ip -n $ns route add table local 2001:db8:1::2/128 dev dummy1
217	fib6_trap_check $ns "table local 2001:db8:1::2/128 dev dummy1" false
218	check_err $? "Local route route did not replace route in main table when should"
219
220	fib6_trap_check $ns "table main 2001:db8:1::2/128 dev dummy1" true
221	check_err $? "Main table route was not replaced when should"
222
223	log_test "IPv6 local table route replacement"
224
225	ip -n $ns link del dev dummy1
226}
227
228fib_notify_on_flag_change_set()
229{
230	local notify=$1; shift
231
232	ip netns exec testns1 sysctl -qw net.ipv4.fib_notify_on_flag_change=$notify
233	ip netns exec testns1 sysctl -qw net.ipv6.fib_notify_on_flag_change=$notify
234
235	log_info "Set fib_notify_on_flag_change to $notify"
236}
237
238setup_prepare()
239{
240	ip netns add testns1
241	if [ $? -ne 0 ]; then
242		echo "Failed to add netns \"testns1\""
243		exit 1
244	fi
245
246	devlink dev reload $DEVLINK_DEV netns testns1
247	if [ $? -ne 0 ]; then
248		echo "Failed to reload into netns \"testns1\""
249		exit 1
250	fi
251}
252
253cleanup()
254{
255	pre_cleanup
256	devlink -N testns1 dev reload $DEVLINK_DEV netns $$
257	ip netns del testns1
258}
259
260trap cleanup EXIT
261
262setup_prepare
263
264fib_notify_on_flag_change_set 1
265tests_run
266
267fib_notify_on_flag_change_set 0
268tests_run
269
270exit $EXIT_STATUS
271