summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/hsr/link_faults.sh
blob: be526281571c5aa7950a50b5066edd6cdf8689ed (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# shellcheck disable=SC2329

source ../lib.sh

ALL_TESTS="
	test_clean_hsrv0
	test_cut_link_hsrv0
	test_packet_loss_hsrv0
	test_high_packet_loss_hsrv0
	test_reordering_hsrv0

	test_clean_hsrv1
	test_cut_link_hsrv1
	test_packet_loss_hsrv1
	test_high_packet_loss_hsrv1
	test_reordering_hsrv1

	test_clean_prp
	test_cut_link_prp
	test_packet_loss_prp
	test_high_packet_loss_prp
	test_reordering_prp
"

# The tests are running ping for 5sec with a relatively short interval in
# different scenarios with faulty links (cut links, packet loss, delay,
# reordering) that should be recoverable by HSR/PRP. The ping interval (10ms)
# is short enough that the base delay (50ms) leads to a queue in the netem
# qdiscs which is needed for reordering.

setup_hsr_topo()
{
	# Three HSR nodes in a ring, every node has a LAN A interface connected
	# to the LAN B interface of the next node.
	#
	#    node1            node2
	#
	#     vethA -------- vethB
	#   hsr1                 hsr2
	#     vethB          vethA
	#         \          /
	#         vethA  vethB
	#             hsr3
	#
	#            node3

	local ver="$1"

	setup_ns node1 node2 node3

	# veth links
	# shellcheck disable=SC2154 # variables assigned by setup_ns
	ip link add vethA netns "$node1" type veth peer name vethB netns "$node2"
	# shellcheck disable=SC2154 # variables assigned by setup_ns
	ip link add vethA netns "$node2" type veth peer name vethB netns "$node3"
	ip link add vethA netns "$node3" type veth peer name vethB netns "$node1"

	# MAC addresses (not needed for HSR operation, but helps with debugging)
	ip -net "$node1" link set address 00:11:22:00:01:01 dev vethA
	ip -net "$node1" link set address 00:11:22:00:01:02 dev vethB

	ip -net "$node2" link set address 00:11:22:00:02:01 dev vethA
	ip -net "$node2" link set address 00:11:22:00:02:02 dev vethB

	ip -net "$node3" link set address 00:11:22:00:03:01 dev vethA
	ip -net "$node3" link set address 00:11:22:00:03:02 dev vethB

	# HSR interfaces
	ip -net "$node1" link add name hsr1 type hsr proto 0 version "$ver" \
		slave1 vethA slave2 vethB supervision 45
	ip -net "$node2" link add name hsr2 type hsr proto 0 version "$ver" \
		slave1 vethA slave2 vethB supervision 45
	ip -net "$node3" link add name hsr3 type hsr proto 0 version "$ver" \
		slave1 vethA slave2 vethB supervision 45

	# IP addresses
	ip -net "$node1" addr add 100.64.0.1/24 dev hsr1
	ip -net "$node2" addr add 100.64.0.2/24 dev hsr2
	ip -net "$node3" addr add 100.64.0.3/24 dev hsr3

	# Set all links up
	ip -net "$node1" link set vethA up
	ip -net "$node1" link set vethB up
	ip -net "$node1" link set hsr1 up

	ip -net "$node2" link set vethA up
	ip -net "$node2" link set vethB up
	ip -net "$node2" link set hsr2 up

	ip -net "$node3" link set vethA up
	ip -net "$node3" link set vethB up
	ip -net "$node3" link set hsr3 up
}

setup_prp_topo()
{
	# Two PRP nodes, connected by two links (treated as LAN A and LAN B).
	#
	#       vethA ----- vethA
	#     prp1             prp2
	#       vethB ----- vethB
	#
	#     node1           node2

	setup_ns node1 node2

	# veth links
	ip link add vethA netns "$node1" type veth peer name vethA netns "$node2"
	ip link add vethB netns "$node1" type veth peer name vethB netns "$node2"

	# MAC addresses will be copied from LAN A interface
	ip -net "$node1" link set address 00:11:22:00:00:01 dev vethA
	ip -net "$node2" link set address 00:11:22:00:00:02 dev vethA

	# PRP interfaces
	ip -net "$node1" link add name prp1 type hsr \
		slave1 vethA slave2 vethB supervision 45 proto 1
	ip -net "$node2" link add name prp2 type hsr \
		slave1 vethA slave2 vethB supervision 45 proto 1

	# IP addresses
	ip -net "$node1" addr add 100.64.0.1/24 dev prp1
	ip -net "$node2" addr add 100.64.0.2/24 dev prp2

	# All links up
	ip -net "$node1" link set vethA up
	ip -net "$node1" link set vethB up
	ip -net "$node1" link set prp1 up

	ip -net "$node2" link set vethA up
	ip -net "$node2" link set vethB up
	ip -net "$node2" link set prp2 up
}

wait_for_hsr_node_table()
{
	log_info "Wait for node table entries to be merged."
	WAIT=5
	while [ "${WAIT}" -gt 0 ]; do
		nts=$(cat /sys/kernel/debug/hsr/hsr*/node_table)

		# We need entries in the node tables, and they need to be merged
		if (echo "$nts" | grep -qE "^([0-9a-f]{2}:){5}") && \
		    ! (echo "$nts" | grep -q "00:00:00:00:00:00"); then
			return
		fi

		sleep 1
		((WAIT--))
	done
	check_err 1 "Failed to wait for merged node table entries"
}

setup_topo()
{
	local proto="$1"

	if [ "$proto" = "HSRv0" ]; then
		setup_hsr_topo 0
		wait_for_hsr_node_table
	elif [ "$proto" = "HSRv1" ]; then
		setup_hsr_topo 1
		wait_for_hsr_node_table
	elif [ "$proto" = "PRP" ]; then
		setup_prp_topo
	else
		check_err 1 "Unknown protocol (${proto})"
	fi
}

check_ping()
{
	local node="$1"
	local dst="$2"
	local accepted_dups="$3"
	local ping_args="-q -i 0.01 -c 400"

	log_info "Running ping $node -> $dst"
	# shellcheck disable=SC2086
	output=$(ip netns exec "$node" ping $ping_args "$dst" | \
		grep "packets transmitted")
	log_info "$output"

	dups=0
	loss=0

	if [[ "$output" =~ \+([0-9]+)" duplicates" ]]; then
		dups="${BASH_REMATCH[1]}"
	fi
	if [[ "$output" =~ ([0-9\.]+\%)" packet loss" ]]; then
		loss="${BASH_REMATCH[1]}"
	fi

	if [ "$dups" -gt "$accepted_dups" ]; then
		check_err 1 "Unexpected duplicate packets (${dups})"
	fi
	if [ "$loss" != "0%" ]; then
		check_err 1 "Unexpected packet loss (${loss})"
	fi
}

test_clean()
{
	local proto="$1"

	RET=0
	tname="${FUNCNAME[0]} - ${proto}"

	setup_topo "$proto"
	if ((RET != ksft_pass)); then
		log_test "${tname} setup"
		return
	fi

	check_ping "$node1" "100.64.0.2" 0

	log_test "${tname}"
}

test_clean_hsrv0()
{
	test_clean "HSRv0"
}

test_clean_hsrv1()
{
	test_clean "HSRv1"
}

test_clean_prp()
{
	test_clean "PRP"
}

test_cut_link()
{
	local proto="$1"

	RET=0
	tname="${FUNCNAME[0]} - ${proto}"

	setup_topo "$proto"
	if ((RET != ksft_pass)); then
		log_test "${tname} setup"
		return
	fi

	# Cutting link from subshell, so check_ping can run in the normal shell
	# with access to global variables from the test harness.
	(
		sleep 2
		log_info "Cutting link"
		ip -net "$node1" link set vethB down
	) &
	check_ping "$node1" "100.64.0.2" 0

	wait
	log_test "${tname}"
}


test_cut_link_hsrv0()
{
	test_cut_link "HSRv0"
}

test_cut_link_hsrv1()
{
	test_cut_link "HSRv1"
}

test_cut_link_prp()
{
	test_cut_link "PRP"
}

test_packet_loss()
{
	local proto="$1"
	local loss="$2"

	RET=0
	tname="${FUNCNAME[0]} - ${proto}, ${loss}"

	setup_topo "$proto"
	if ((RET != ksft_pass)); then
		log_test "${tname} setup"
		return
	fi

	# Packet loss with lower delay makes sure the packets on the lossy link
	# arrive first.
	tc -net "$node1" qdisc add dev vethA root netem delay 50ms
	tc -net "$node1" qdisc add dev vethB root netem delay 20ms loss "$loss"

	check_ping "$node1" "100.64.0.2" 40

	log_test "${tname}"
}

test_packet_loss_hsrv0()
{
	test_packet_loss "HSRv0" "20%"
}

test_packet_loss_hsrv1()
{
	test_packet_loss "HSRv1" "20%"
}

test_packet_loss_prp()
{
	test_packet_loss "PRP" "20%"
}

test_high_packet_loss_hsrv0()
{
	test_packet_loss "HSRv0" "80%"
}

test_high_packet_loss_hsrv1()
{
	test_packet_loss "HSRv1" "80%"
}

test_high_packet_loss_prp()
{
	test_packet_loss "PRP" "80%"
}

test_reordering()
{
	local proto="$1"

	RET=0
	tname="${FUNCNAME[0]} - ${proto}"

	setup_topo "$proto"
	if ((RET != ksft_pass)); then
		log_test "${tname} setup"
		return
	fi

	tc -net "$node1" qdisc add dev vethA root netem delay 50ms
	tc -net "$node1" qdisc add dev vethB root netem delay 50ms reorder 20%

	check_ping "$node1" "100.64.0.2" 40

	log_test "${tname}"
}

test_reordering_hsrv0()
{
	test_reordering "HSRv0"
}

test_reordering_hsrv1()
{
	test_reordering "HSRv1"
}

test_reordering_prp()
{
	test_reordering "PRP"
}

cleanup()
{
	cleanup_all_ns
}

trap cleanup EXIT

tests_run

exit $EXIT_STATUS