1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Topology for Bond mode 1,5,6 testing
5#
6#  +-------------------------------------+
7#  |                bond0                |
8#  |                  +                  |  Server
9#  |      eth0        | eth1   eth2      |  192.0.2.1/24
10#  |        +-------------------+        |  2001:db8::1/24
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
28s_ns="s-$(mktemp -u XXXXXX)"
29c_ns="c-$(mktemp -u XXXXXX)"
30g_ns="g-$(mktemp -u XXXXXX)"
31s_ip4="192.0.2.1"
32c_ip4="192.0.2.10"
33g_ip4="192.0.2.254"
34s_ip6="2001:db8::1"
35c_ip6="2001:db8::10"
36g_ip6="2001:db8::254"
37
38gateway_create()
39{
40	ip netns add ${g_ns}
41	ip -n ${g_ns} link add br0 type bridge
42	ip -n ${g_ns} link set br0 up
43	ip -n ${g_ns} addr add ${g_ip4}/24 dev br0
44	ip -n ${g_ns} addr add ${g_ip6}/24 dev br0
45}
46
47gateway_destroy()
48{
49	ip -n ${g_ns} link del br0
50	ip netns del ${g_ns}
51}
52
53server_create()
54{
55	ip netns add ${s_ns}
56	ip -n ${s_ns} link add bond0 type bond mode active-backup miimon 100
57
58	for i in $(seq 0 2); do
59		ip -n ${s_ns} link add eth${i} type veth peer name s${i} netns ${g_ns}
60
61		ip -n ${g_ns} link set s${i} up
62		ip -n ${g_ns} link set s${i} master br0
63		ip -n ${s_ns} link set eth${i} master bond0
64
65		tc -n ${g_ns} qdisc add dev s${i} clsact
66	done
67
68	ip -n ${s_ns} link set bond0 up
69	ip -n ${s_ns} addr add ${s_ip4}/24 dev bond0
70	ip -n ${s_ns} addr add ${s_ip6}/24 dev bond0
71	sleep 2
72}
73
74# Reset bond with new mode and options
75bond_reset()
76{
77	local param="$1"
78
79	ip -n ${s_ns} link set bond0 down
80	ip -n ${s_ns} link del bond0
81
82	ip -n ${s_ns} link add bond0 type bond $param
83	for i in $(seq 0 2); do
84		ip -n ${s_ns} link set eth$i master bond0
85	done
86
87	ip -n ${s_ns} link set bond0 up
88	ip -n ${s_ns} addr add ${s_ip4}/24 dev bond0
89	ip -n ${s_ns} addr add ${s_ip6}/24 dev bond0
90	sleep 2
91}
92
93server_destroy()
94{
95	for i in $(seq 0 2); do
96		ip -n ${s_ns} link del eth${i}
97	done
98	ip netns del ${s_ns}
99}
100
101client_create()
102{
103	ip netns add ${c_ns}
104	ip -n ${c_ns} link add eth0 type veth peer name c0 netns ${g_ns}
105
106	ip -n ${g_ns} link set c0 up
107	ip -n ${g_ns} link set c0 master br0
108
109	ip -n ${c_ns} link set eth0 up
110	ip -n ${c_ns} addr add ${c_ip4}/24 dev eth0
111	ip -n ${c_ns} addr add ${c_ip6}/24 dev eth0
112}
113
114client_destroy()
115{
116	ip -n ${c_ns} link del eth0
117	ip netns del ${c_ns}
118}
119
120setup_prepare()
121{
122	gateway_create
123	server_create
124	client_create
125}
126
127cleanup()
128{
129	pre_cleanup
130
131	client_destroy
132	server_destroy
133	gateway_destroy
134}
135
136bond_check_connection()
137{
138	local msg=${1:-"check connection"}
139
140	sleep 2
141	ip netns exec ${s_ns} ping ${c_ip4} -c5 -i 0.1 &>/dev/null
142	check_err $? "${msg}: ping failed"
143	ip netns exec ${s_ns} ping6 ${c_ip6} -c5 -i 0.1 &>/dev/null
144	check_err $? "${msg}: ping6 failed"
145}
146