xref: /linux/tools/testing/selftests/net/hsr/hsr_ping.sh (revision b50ecc5aca4d18f1f0c4942f5c797bc85edef144)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ipv6=true
5
6source ./hsr_common.sh
7
8optstring="h4"
9usage() {
10	echo "Usage: $0 [OPTION]"
11	echo -e "\t-4: IPv4 only: disable IPv6 tests (default: test both IPv4 and IPv6)"
12}
13
14while getopts "$optstring" option;do
15	case "$option" in
16	"h")
17		usage $0
18		exit 0
19		;;
20	"4")
21		ipv6=false
22		;;
23	"?")
24		usage $0
25		exit 1
26		;;
27esac
28done
29
30do_complete_ping_test()
31{
32	echo "INFO: Initial validation ping."
33	# Each node has to be able each one.
34	do_ping "$ns1" 100.64.0.2
35	do_ping "$ns2" 100.64.0.1
36	do_ping "$ns3" 100.64.0.1
37	stop_if_error "Initial validation failed."
38
39	do_ping "$ns1" 100.64.0.3
40	do_ping "$ns2" 100.64.0.3
41	do_ping "$ns3" 100.64.0.2
42
43	do_ping "$ns1" dead:beef:1::2
44	do_ping "$ns1" dead:beef:1::3
45	do_ping "$ns2" dead:beef:1::1
46	do_ping "$ns2" dead:beef:1::2
47	do_ping "$ns3" dead:beef:1::1
48	do_ping "$ns3" dead:beef:1::2
49
50	stop_if_error "Initial validation failed."
51
52# Wait until supervisor all supervision frames have been processed and the node
53# entries have been merged. Otherwise duplicate frames will be observed which is
54# valid at this stage.
55	WAIT=5
56	while [ ${WAIT} -gt 0 ]
57	do
58		grep 00:00:00:00:00:00 /sys/kernel/debug/hsr/hsr*/node_table
59		if [ $? -ne 0 ]
60		then
61			break
62		fi
63		sleep 1
64		let "WAIT = WAIT - 1"
65	done
66
67# Just a safety delay in case the above check didn't handle it.
68	sleep 1
69
70	echo "INFO: Longer ping test."
71	do_ping_long "$ns1" 100.64.0.2
72	do_ping_long "$ns1" dead:beef:1::2
73	do_ping_long "$ns1" 100.64.0.3
74	do_ping_long "$ns1" dead:beef:1::3
75
76	stop_if_error "Longer ping test failed."
77
78	do_ping_long "$ns2" 100.64.0.1
79	do_ping_long "$ns2" dead:beef:1::1
80	do_ping_long "$ns2" 100.64.0.3
81	do_ping_long "$ns2" dead:beef:1::2
82	stop_if_error "Longer ping test failed."
83
84	do_ping_long "$ns3" 100.64.0.1
85	do_ping_long "$ns3" dead:beef:1::1
86	do_ping_long "$ns3" 100.64.0.2
87	do_ping_long "$ns3" dead:beef:1::2
88	stop_if_error "Longer ping test failed."
89
90	echo "INFO: Cutting one link."
91	do_ping_long "$ns1" 100.64.0.3 &
92
93	sleep 3
94	ip -net "$ns3" link set ns3eth1 down
95	wait
96
97	ip -net "$ns3" link set ns3eth1 up
98
99	stop_if_error "Failed with one link down."
100
101	echo "INFO: Delay the link and drop a few packages."
102	tc -net "$ns3" qdisc add dev ns3eth1 root netem delay 50ms
103	tc -net "$ns2" qdisc add dev ns2eth1 root netem delay 5ms loss 25%
104
105	do_ping_long "$ns1" 100.64.0.2
106	do_ping_long "$ns1" 100.64.0.3
107
108	stop_if_error "Failed with delay and packetloss."
109
110	do_ping_long "$ns2" 100.64.0.1
111	do_ping_long "$ns2" 100.64.0.3
112
113	stop_if_error "Failed with delay and packetloss."
114
115	do_ping_long "$ns3" 100.64.0.1
116	do_ping_long "$ns3" 100.64.0.2
117	stop_if_error "Failed with delay and packetloss."
118
119	echo "INFO: All good."
120}
121
122setup_hsr_interfaces()
123{
124	local HSRv="$1"
125
126	echo "INFO: preparing interfaces for HSRv${HSRv}."
127# Three HSR nodes. Each node has one link to each of its neighbour, two links in total.
128#
129#    ns1eth1 ----- ns2eth1
130#      hsr1         hsr2
131#    ns1eth2       ns2eth2
132#       |            |
133#    ns3eth1      ns3eth2
134#           \    /
135#            hsr3
136#
137	# Interfaces
138	ip link add ns1eth1 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
139	ip link add ns1eth2 netns "$ns1" type veth peer name ns3eth1 netns "$ns3"
140	ip link add ns3eth2 netns "$ns3" type veth peer name ns2eth2 netns "$ns2"
141
142	# HSRv0/1
143	ip -net "$ns1" link add name hsr1 type hsr slave1 ns1eth1 slave2 ns1eth2 supervision 45 version $HSRv proto 0
144	ip -net "$ns2" link add name hsr2 type hsr slave1 ns2eth1 slave2 ns2eth2 supervision 45 version $HSRv proto 0
145	ip -net "$ns3" link add name hsr3 type hsr slave1 ns3eth1 slave2 ns3eth2 supervision 45 version $HSRv proto 0
146
147	# IP for HSR
148	ip -net "$ns1" addr add 100.64.0.1/24 dev hsr1
149	ip -net "$ns1" addr add dead:beef:1::1/64 dev hsr1 nodad
150	ip -net "$ns2" addr add 100.64.0.2/24 dev hsr2
151	ip -net "$ns2" addr add dead:beef:1::2/64 dev hsr2 nodad
152	ip -net "$ns3" addr add 100.64.0.3/24 dev hsr3
153	ip -net "$ns3" addr add dead:beef:1::3/64 dev hsr3 nodad
154
155	ip -net "$ns1" link set address 00:11:22:00:01:01 dev ns1eth1
156	ip -net "$ns1" link set address 00:11:22:00:01:02 dev ns1eth2
157
158	ip -net "$ns2" link set address 00:11:22:00:02:01 dev ns2eth1
159	ip -net "$ns2" link set address 00:11:22:00:02:02 dev ns2eth2
160
161	ip -net "$ns3" link set address 00:11:22:00:03:01 dev ns3eth1
162	ip -net "$ns3" link set address 00:11:22:00:03:02 dev ns3eth2
163
164	# All Links up
165	ip -net "$ns1" link set ns1eth1 up
166	ip -net "$ns1" link set ns1eth2 up
167	ip -net "$ns1" link set hsr1 up
168
169	ip -net "$ns2" link set ns2eth1 up
170	ip -net "$ns2" link set ns2eth2 up
171	ip -net "$ns2" link set hsr2 up
172
173	ip -net "$ns3" link set ns3eth1 up
174	ip -net "$ns3" link set ns3eth2 up
175	ip -net "$ns3" link set hsr3 up
176}
177
178setup_vlan_interfaces() {
179	ip -net "$ns1" link add link hsr1 name hsr1.2 type vlan id 2
180	ip -net "$ns1" link add link hsr1 name hsr1.3 type vlan id 3
181	ip -net "$ns1" link add link hsr1 name hsr1.4 type vlan id 4
182	ip -net "$ns1" link add link hsr1 name hsr1.5 type vlan id 5
183
184	ip -net "$ns2" link add link hsr2 name hsr2.2 type vlan id 2
185	ip -net "$ns2" link add link hsr2 name hsr2.3 type vlan id 3
186	ip -net "$ns2" link add link hsr2 name hsr2.4 type vlan id 4
187	ip -net "$ns2" link add link hsr2 name hsr2.5 type vlan id 5
188
189	ip -net "$ns3" link add link hsr3 name hsr3.2 type vlan id 2
190	ip -net "$ns3" link add link hsr3 name hsr3.3 type vlan id 3
191	ip -net "$ns3" link add link hsr3 name hsr3.4 type vlan id 4
192	ip -net "$ns3" link add link hsr3 name hsr3.5 type vlan id 5
193
194	ip -net "$ns1" addr add 100.64.2.1/24 dev hsr1.2
195	ip -net "$ns1" addr add 100.64.3.1/24 dev hsr1.3
196	ip -net "$ns1" addr add 100.64.4.1/24 dev hsr1.4
197	ip -net "$ns1" addr add 100.64.5.1/24 dev hsr1.5
198
199	ip -net "$ns2" addr add 100.64.2.2/24 dev hsr2.2
200	ip -net "$ns2" addr add 100.64.3.2/24 dev hsr2.3
201	ip -net "$ns2" addr add 100.64.4.2/24 dev hsr2.4
202	ip -net "$ns2" addr add 100.64.5.2/24 dev hsr2.5
203
204	ip -net "$ns3" addr add 100.64.2.3/24 dev hsr3.2
205	ip -net "$ns3" addr add 100.64.3.3/24 dev hsr3.3
206	ip -net "$ns3" addr add 100.64.4.3/24 dev hsr3.4
207	ip -net "$ns3" addr add 100.64.5.3/24 dev hsr3.5
208
209	ip -net "$ns1" link set dev hsr1.2 up
210	ip -net "$ns1" link set dev hsr1.3 up
211	ip -net "$ns1" link set dev hsr1.4 up
212	ip -net "$ns1" link set dev hsr1.5 up
213
214	ip -net "$ns2" link set dev hsr2.2 up
215	ip -net "$ns2" link set dev hsr2.3 up
216	ip -net "$ns2" link set dev hsr2.4 up
217	ip -net "$ns2" link set dev hsr2.5 up
218
219	ip -net "$ns3" link set dev hsr3.2 up
220	ip -net "$ns3" link set dev hsr3.3 up
221	ip -net "$ns3" link set dev hsr3.4 up
222	ip -net "$ns3" link set dev hsr3.5 up
223
224}
225
226hsr_vlan_ping() {
227	do_ping "$ns1" 100.64.2.2
228	do_ping "$ns1" 100.64.3.2
229	do_ping "$ns1" 100.64.4.2
230	do_ping "$ns1" 100.64.5.2
231
232	do_ping "$ns1" 100.64.2.3
233	do_ping "$ns1" 100.64.3.3
234	do_ping "$ns1" 100.64.4.3
235	do_ping "$ns1" 100.64.5.3
236
237	do_ping "$ns2" 100.64.2.1
238	do_ping "$ns2" 100.64.3.1
239	do_ping "$ns2" 100.64.4.1
240	do_ping "$ns2" 100.64.5.1
241
242	do_ping "$ns2" 100.64.2.3
243	do_ping "$ns2" 100.64.3.3
244	do_ping "$ns2" 100.64.4.3
245	do_ping "$ns2" 100.64.5.3
246
247	do_ping "$ns3" 100.64.2.1
248	do_ping "$ns3" 100.64.3.1
249	do_ping "$ns3" 100.64.4.1
250	do_ping "$ns3" 100.64.5.1
251
252	do_ping "$ns3" 100.64.2.2
253	do_ping "$ns3" 100.64.3.2
254	do_ping "$ns3" 100.64.4.2
255	do_ping "$ns3" 100.64.5.2
256}
257
258run_vlan_tests() {
259	vlan_challenged_hsr1=$(ip net exec "$ns1" ethtool -k hsr1 | grep "vlan-challenged" | awk '{print $2}')
260	vlan_challenged_hsr2=$(ip net exec "$ns2" ethtool -k hsr2 | grep "vlan-challenged" | awk '{print $2}')
261	vlan_challenged_hsr3=$(ip net exec "$ns3" ethtool -k hsr3 | grep "vlan-challenged" | awk '{print $2}')
262
263	if [[ "$vlan_challenged_hsr1" = "off" || "$vlan_challenged_hsr2" = "off" || "$vlan_challenged_hsr3" = "off" ]]; then
264		echo "INFO: Running VLAN tests"
265		setup_vlan_interfaces
266		hsr_vlan_ping
267	else
268		echo "INFO: Not Running VLAN tests as the device does not support VLAN"
269	fi
270}
271
272check_prerequisites
273setup_ns ns1 ns2 ns3
274
275trap cleanup_all_ns EXIT
276
277setup_hsr_interfaces 0
278do_complete_ping_test
279
280run_vlan_tests
281
282setup_ns ns1 ns2 ns3
283
284setup_hsr_interfaces 1
285do_complete_ping_test
286
287run_vlan_tests
288
289exit $ret
290