1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Topology for Bond mode 1,5,6 testing
5#
6#  +-------------------------+
7#  |          bond0          |  Server
8#  |            +            |  192.0.2.1/24
9#  |      eth0  |  eth1      |  2001:db8::1/24
10#  |        +---+---+        |
11#  |        |       |        |
12#  +-------------------------+
13#           |       |
14#  +-------------------------+
15#  |        |       |        |
16#  |    +---+-------+---+    |  Gateway
17#  |    |      br0      |    |  192.0.2.254/24
18#  |    +-------+-------+    |  2001:db8::254/24
19#  |            |            |
20#  +-------------------------+
21#               |
22#  +-------------------------+
23#  |            |            |  Client
24#  |            +            |  192.0.2.10/24
25#  |          eth0           |  2001:db8::10/24
26#  +-------------------------+
27
28REQUIRE_MZ=no
29NUM_NETIFS=0
30lib_dir=$(dirname "$0")
31source ${lib_dir}/net_forwarding_lib.sh
32
33s_ns="s-$(mktemp -u XXXXXX)"
34c_ns="c-$(mktemp -u XXXXXX)"
35g_ns="g-$(mktemp -u XXXXXX)"
36s_ip4="192.0.2.1"
37c_ip4="192.0.2.10"
38g_ip4="192.0.2.254"
39s_ip6="2001:db8::1"
40c_ip6="2001:db8::10"
41g_ip6="2001:db8::254"
42
43gateway_create()
44{
45	ip netns add ${g_ns}
46	ip -n ${g_ns} link add br0 type bridge
47	ip -n ${g_ns} link set br0 up
48	ip -n ${g_ns} addr add ${g_ip4}/24 dev br0
49	ip -n ${g_ns} addr add ${g_ip6}/24 dev br0
50}
51
52gateway_destroy()
53{
54	ip -n ${g_ns} link del br0
55	ip netns del ${g_ns}
56}
57
58server_create()
59{
60	ip netns add ${s_ns}
61	ip -n ${s_ns} link add bond0 type bond mode active-backup miimon 100
62
63	for i in $(seq 0 1); do
64		ip -n ${s_ns} link add eth${i} type veth peer name s${i} netns ${g_ns}
65
66		ip -n ${g_ns} link set s${i} up
67		ip -n ${g_ns} link set s${i} master br0
68		ip -n ${s_ns} link set eth${i} master bond0
69
70		tc -n ${g_ns} qdisc add dev s${i} clsact
71	done
72
73	ip -n ${s_ns} link set bond0 up
74	ip -n ${s_ns} addr add ${s_ip4}/24 dev bond0
75	ip -n ${s_ns} addr add ${s_ip6}/24 dev bond0
76	sleep 2
77}
78
79# Reset bond with new mode and options
80bond_reset()
81{
82	# Count the eth link number in real-time as this function
83	# maybe called from other topologies.
84	local link_num=$(ip -n ${s_ns} -br link show | grep -c "^eth")
85	local param="$1"
86	link_num=$((link_num -1))
87
88	ip -n ${s_ns} link set bond0 down
89	ip -n ${s_ns} link del bond0
90
91	ip -n ${s_ns} link add bond0 type bond $param
92	for i in $(seq 0 ${link_num}); do
93		ip -n ${s_ns} link set eth$i master bond0
94	done
95
96	ip -n ${s_ns} link set bond0 up
97	ip -n ${s_ns} addr add ${s_ip4}/24 dev bond0
98	ip -n ${s_ns} addr add ${s_ip6}/24 dev bond0
99	sleep 2
100}
101
102server_destroy()
103{
104	# Count the eth link number in real-time as this function
105	# maybe called from other topologies.
106	local link_num=$(ip -n ${s_ns} -br link show | grep -c "^eth")
107	link_num=$((link_num -1))
108	for i in $(seq 0 ${link_num}); do
109		ip -n ${s_ns} link del eth${i}
110	done
111	ip netns del ${s_ns}
112}
113
114client_create()
115{
116	ip netns add ${c_ns}
117	ip -n ${c_ns} link add eth0 type veth peer name c0 netns ${g_ns}
118
119	ip -n ${g_ns} link set c0 up
120	ip -n ${g_ns} link set c0 master br0
121
122	ip -n ${c_ns} link set eth0 up
123	ip -n ${c_ns} addr add ${c_ip4}/24 dev eth0
124	ip -n ${c_ns} addr add ${c_ip6}/24 dev eth0
125}
126
127client_destroy()
128{
129	ip -n ${c_ns} link del eth0
130	ip netns del ${c_ns}
131}
132
133setup_prepare()
134{
135	gateway_create
136	server_create
137	client_create
138}
139
140cleanup()
141{
142	pre_cleanup
143
144	client_destroy
145	server_destroy
146	gateway_destroy
147}
148
149bond_check_connection()
150{
151	local msg=${1:-"check connection"}
152
153	sleep 2
154	ip netns exec ${s_ns} ping ${c_ip4} -c5 -i 0.1 &>/dev/null
155	check_err $? "${msg}: ping failed"
156	ip netns exec ${s_ns} ping6 ${c_ip6} -c5 -i 0.1 &>/dev/null
157	check_err $? "${msg}: ping6 failed"
158}
159