1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NAMESPACES=""
5
6# Test that a link aggregation device (bonding, team) removes the hardware
7# addresses that it adds on its underlying devices.
8test_LAG_cleanup()
9{
10	local driver=$1
11	local mode=$2
12	local ucaddr="02:00:00:12:34:56"
13	local addr6="fe80::78:9abc/64"
14	local mcaddr="33:33:ff:78:9a:bc"
15	local name
16
17	ip link add dummy1 type dummy
18	ip link add dummy2 type dummy
19	if [ "$driver" = "bonding" ]; then
20		name="bond1"
21		ip link add "$name" up type bond mode "$mode"
22		ip link set dev dummy1 master "$name"
23		ip link set dev dummy2 master "$name"
24	elif [ "$driver" = "team" ]; then
25		name="team0"
26		teamd -d -c '
27			{
28				"device": "'"$name"'",
29				"runner": {
30					"name": "'"$mode"'"
31				},
32				"ports": {
33					"dummy1":
34						{},
35					"dummy2":
36						{}
37				}
38			}
39		'
40		ip link set dev "$name" up
41	else
42		check_err 1
43		log_test test_LAG_cleanup ": unknown driver \"$driver\""
44		return
45	fi
46
47	# Used to test dev->uc handling
48	ip link add mv0 link "$name" up address "$ucaddr" type macvlan
49	# Used to test dev->mc handling
50	ip address add "$addr6" dev "$name"
51	ip link set dev "$name" down
52	ip link del "$name"
53
54	not grep_bridge_fdb "$ucaddr" bridge fdb show >/dev/null
55	check_err $? "macvlan unicast address still present on a slave"
56
57	not grep_bridge_fdb "$mcaddr" bridge fdb show >/dev/null
58	check_err $? "IPv6 solicited-node multicast mac address still present on a slave"
59
60	cleanup
61
62	log_test "$driver cleanup mode $mode"
63}
64
65# Build a generic 2 node net namespace with 2 connections
66# between the namespaces
67#
68#  +-----------+       +-----------+
69#  | node1     |       | node2     |
70#  |           |       |           |
71#  |           |       |           |
72#  |      eth0 +-------+ eth0      |
73#  |           |       |           |
74#  |      eth1 +-------+ eth1      |
75#  |           |       |           |
76#  +-----------+       +-----------+
77lag_setup2x2()
78{
79	local state=${1:-down}
80	local namespaces="lag_node1 lag_node2"
81
82	# create namespaces
83	for n in ${namespaces}; do
84		ip netns add ${n}
85	done
86
87	# wire up namespaces
88	ip link add name lag1 type veth peer name lag1-end
89	ip link set dev lag1 netns lag_node1 $state name eth0
90	ip link set dev lag1-end netns lag_node2 $state name eth0
91
92	ip link add name lag1 type veth peer name lag1-end
93	ip link set dev lag1 netns lag_node1 $state name eth1
94	ip link set dev lag1-end netns lag_node2 $state name eth1
95
96	NAMESPACES="${namespaces}"
97}
98
99# cleanup all lag related namespaces and remove the bonding module
100lag_cleanup()
101{
102	for n in ${NAMESPACES}; do
103		ip netns delete ${n} >/dev/null 2>&1 || true
104	done
105	modprobe -r bonding
106}
107
108SWITCH="lag_node1"
109CLIENT="lag_node2"
110CLIENTIP="172.20.2.1"
111SWITCHIP="172.20.2.2"
112
113lag_setup_network()
114{
115	lag_setup2x2 "down"
116
117	# create switch
118	ip netns exec ${SWITCH} ip link add br0 up type bridge
119	ip netns exec ${SWITCH} ip link set eth0 master br0 up
120	ip netns exec ${SWITCH} ip link set eth1 master br0 up
121	ip netns exec ${SWITCH} ip addr add ${SWITCHIP}/24 dev br0
122}
123
124lag_reset_network()
125{
126	ip netns exec ${CLIENT} ip link del bond0
127	ip netns exec ${SWITCH} ip link set eth0 up
128	ip netns exec ${SWITCH} ip link set eth1 up
129}
130
131create_bond()
132{
133	# create client
134	ip netns exec ${CLIENT} ip link set eth0 down
135	ip netns exec ${CLIENT} ip link set eth1 down
136
137	ip netns exec ${CLIENT} ip link add bond0 type bond $@
138	ip netns exec ${CLIENT} ip link set eth0 master bond0
139	ip netns exec ${CLIENT} ip link set eth1 master bond0
140	ip netns exec ${CLIENT} ip link set bond0 up
141	ip netns exec ${CLIENT} ip addr add ${CLIENTIP}/24 dev bond0
142}
143
144test_bond_recovery()
145{
146	RET=0
147
148	create_bond $@
149
150	# verify connectivity
151	ip netns exec ${CLIENT} ping ${SWITCHIP} -c 2 >/dev/null 2>&1
152	check_err $? "No connectivity"
153
154	# force the links of the bond down
155	ip netns exec ${SWITCH} ip link set eth0 down
156	sleep 2
157	ip netns exec ${SWITCH} ip link set eth0 up
158	ip netns exec ${SWITCH} ip link set eth1 down
159
160	# re-verify connectivity
161	ip netns exec ${CLIENT} ping ${SWITCHIP} -c 2 >/dev/null 2>&1
162
163	local rc=$?
164	check_err $rc "Bond failed to recover"
165	log_test "$1 ($2) bond recovery"
166	lag_reset_network
167}
168