aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/net/forwarding/bridge_fdb_learning_limit.sh
blob: 0760a34b7114618d29d518f4c9699da0a232a70f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# ShellCheck incorrectly believes that most of the code here is unreachable
# because it's invoked by variable name following ALL_TESTS.
#
# shellcheck disable=SC2317

ALL_TESTS="check_accounting check_limit"
NUM_NETIFS=6
source lib.sh

TEST_MAC_BASE=de:ad:be:ef:42:

NUM_PKTS=16
FDB_LIMIT=8

FDB_TYPES=(
	# name		is counted?	overrides learned?
	'learned	1		0'
	'static		0		1'
	'user		0		1'
	'extern_learn	0		1'
	'local		0		1'
)

mac()
{
	printf "${TEST_MAC_BASE}%02x" "$1"
}

H1_DEFAULT_MAC=$(mac 42)

switch_create()
{
	ip link add dev br0 type bridge

	ip link set dev "$swp1" master br0
	ip link set dev "$swp2" master br0
	# swp3 is used to add local MACs, so do not add it to the bridge yet.

	# swp2 is only used for replying when learning on swp1, its MAC should not be learned.
	ip link set dev "$swp2" type bridge_slave learning off

	ip link set dev br0 up

	ip link set dev "$swp1" up
	ip link set dev "$swp2" up
	ip link set dev "$swp3" up
}

switch_destroy()
{
	ip link set dev "$swp3" down
	ip link set dev "$swp2" down
	ip link set dev "$swp1" down

	ip link del dev br0
}

h_create()
{
	ip link set "$h1" addr "$H1_DEFAULT_MAC"

	simple_if_init "$h1" 192.0.2.1/24
	simple_if_init "$h2" 192.0.2.2/24
}

h_destroy()
{
	simple_if_fini "$h1" 192.0.2.1/24
	simple_if_fini "$h2" 192.0.2.2/24
}

setup_prepare()
{
	h1=${NETIFS[p1]}
	swp1=${NETIFS[p2]}

	h2=${NETIFS[p3]}
	swp2=${NETIFS[p4]}

	swp3=${NETIFS[p6]}

	vrf_prepare

	h_create

	switch_create
}

cleanup()
{
	pre_cleanup

	switch_destroy

	h_destroy

	vrf_cleanup
}

fdb_get_n_learned()
{
	ip -d -j link show dev br0 type bridge | \
		jq '.[]["linkinfo"]["info_data"]["fdb_n_learned"]'
}

fdb_get_n_mac()
{
	local mac=${1}

	bridge -j fdb show br br0 | \
		jq "map(select(.mac == \"${mac}\" and (has(\"vlan\") | not))) | length"
}

fdb_fill_learned()
{
	local i

	for i in $(seq 1 "$NUM_PKTS"); do
		fdb_add learned "$(mac "$i")"
	done
}

fdb_reset()
{
	bridge fdb flush dev br0

	# Keep the default MAC address of h1 in the table. We set it to a different one when
	# testing dynamic learning.
	bridge fdb add "$H1_DEFAULT_MAC" dev "$swp1" master static use
}

fdb_add()
{
	local type=$1 mac=$2

	case "$type" in
		learned)
			ip link set "$h1" addr "$mac"
			# Wait for a reply so we implicitly wait until after the forwarding
			# code finished and the FDB entry was created.
			PING_COUNT=1 ping_do "$h1" 192.0.2.2
			check_err $? "Failed to ping another bridge port"
			ip link set "$h1" addr "$H1_DEFAULT_MAC"
			;;
		local)
			ip link set dev "$swp3" addr "$mac" && ip link set "$swp3" master br0
			;;
		static)
			bridge fdb replace "$mac" dev "$swp1" master static
			;;
		user)
			bridge fdb replace "$mac" dev "$swp1" master static use
			;;
		extern_learn)
			bridge fdb replace "$mac" dev "$swp1" master extern_learn
			;;
	esac

	check_err $? "Failed to add a FDB entry of type ${type}"
}

fdb_del()
{
	local type=$1 mac=$2

	case "$type" in
		local)
			ip link set "$swp3" nomaster
			;;
		*)
			bridge fdb del "$mac" dev "$swp1" master
			;;
	esac

	check_err $? "Failed to remove a FDB entry of type ${type}"
}

check_accounting_one_type()
{
	local type=$1 is_counted=$2 overrides_learned=$3
	shift 3
	RET=0

	fdb_reset
	fdb_add "$type" "$(mac 0)"
	learned=$(fdb_get_n_learned)
	[ "$learned" -ne "$is_counted" ]
	check_fail $? "Inserted FDB type ${type}: Expected the count ${is_counted}, but got ${learned}"

	fdb_del "$type" "$(mac 0)"
	learned=$(fdb_get_n_learned)
	[ "$learned" -ne 0 ]
	check_fail $? "Removed FDB type ${type}: Expected the count 0, but got ${learned}"

	if [ "$overrides_learned" -eq 1 ]; then
		fdb_reset
		fdb_add learned "$(mac 0)"
		fdb_add "$type" "$(mac 0)"
		learned=$(fdb_get_n_learned)
		[ "$learned" -ne "$is_counted" ]
		check_fail $? "Set a learned entry to FDB type ${type}: Expected the count ${is_counted}, but got ${learned}"
		fdb_del "$type" "$(mac 0)"
	fi

	log_test "FDB accounting interacting with FDB type ${type}"
}

check_accounting()
{
	local type_args learned
	RET=0

	fdb_reset
	learned=$(fdb_get_n_learned)
	[ "$learned" -ne 0 ]
	check_fail $? "Flushed the FDB table: Expected the count 0, but got ${learned}"

	fdb_fill_learned
	sleep 1

	learned=$(fdb_get_n_learned)
	[ "$learned" -ne "$NUM_PKTS" ]
	check_fail $? "Filled the FDB table: Expected the count ${NUM_PKTS}, but got ${learned}"

	log_test "FDB accounting"

	for type_args in "${FDB_TYPES[@]}"; do
		# This is intentional use of word splitting.
		# shellcheck disable=SC2086
		check_accounting_one_type $type_args
	done
}

check_limit_one_type()
{
	local type=$1 is_counted=$2
	local n_mac expected=$((1 - is_counted))
	RET=0

	fdb_reset
	fdb_fill_learned

	fdb_add "$type" "$(mac 0)"
	n_mac=$(fdb_get_n_mac "$(mac 0)")
	[ "$n_mac" -ne "$expected" ]
	check_fail $? "Inserted FDB type ${type} at limit: Expected the count ${expected}, but got ${n_mac}"

	log_test "FDB limits interacting with FDB type ${type}"
}

check_limit()
{
	local learned
	RET=0

	ip link set br0 type bridge fdb_max_learned "$FDB_LIMIT"

	fdb_reset
	fdb_fill_learned

	learned=$(fdb_get_n_learned)
	[ "$learned" -ne "$FDB_LIMIT" ]
	check_fail $? "Filled the limited FDB table: Expected the count ${FDB_LIMIT}, but got ${learned}"

	log_test "FDB limits"

	for type_args in "${FDB_TYPES[@]}"; do
		# This is intentional use of word splitting.
		# shellcheck disable=SC2086
		check_limit_one_type $type_args
	done
}

trap cleanup EXIT

setup_prepare

tests_run

exit $EXIT_STATUS