-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdisc_setup_routing_device.bash
executable file
·192 lines (148 loc) · 6.55 KB
/
qdisc_setup_routing_device.bash
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
#!/usr/bin/bash
device=$1
device_count=$2
bandwidth="950mbit"
if [[ -n $3 ]]; then
bandwidth=$3
fi
if [[ -n $4 ]]; then
qdisc_to_set=$4
fi
subnet_octet_a=172
subnet_octet_b=16
vlan_start=100
vlan_addition=50
#------------------------------
# Find the default route interface
#ip route show default
#default via 172.16.50.1 dev wlp0s20f3 proto dhcp src 172.16.50.140 metric 600
default_route_line=$(ip route show default)
#echo "default_route_line:${default_route_line}"
regex="dev\s+(\S+)"
if [[ ${default_route_line} =~ ${regex} ]]; then
default_route_interface="${BASH_REMATCH[1]}"
fi
echo "default_route_interface:${default_route_interface}"
nic="${default_route_interface}"
# sub interface names are apparently limited to 11 chars
vlan_dev=${nic:0:11}
#-----------------------------------------------
# system setup
echo sysctl -w net.ipv4.ip_local_port_range="1025 65534"
sysctl -w net.ipv4.ip_local_port_range="1025 65534"
echo sysctl -w net.ipv4.tcp_timestamps=1
sysctl -w net.ipv4.tcp_timestamps=1
echo sysctl net.ipv4.ip_forward=1
sysctl net.ipv4.ip_forward=1
echo sysctl -w net.core.default_qdisc="pfifo_fast"
sysctl -w net.core.default_qdisc="pfifo_fast"
qdiscs=(noqueue pfifo_fast fq fq_codel cake20 cake40)
namespaces=()
qdisc_count=0
for qdisc in "${qdiscs[@]}"; do
qdisc_count=$((qdisc_count + 1))
if [[ -n ${qdisc_to_set} ]]; then
if [[ ${qdisc} != "${qdisc_to_set}" ]]; then
continue
fi
fi
namespace=$(((device_count * vlan_start) + qdisc_count))
namespaces+=("${namespace}")
vlan="${namespace}"
octet_a="${subnet_octet_a}"
octet_b=$((subnet_octet_b + device_count))
octet_c=$((qdisc_count))
#-----------------------------------------------
echo "### Creating network namespace:network${namespace}"
echo ip netns add network"${namespace}"
ip netns add network"${namespace}"
echo ip netns exec network"${namespace}" sysctl -w net.ipv4.ip_forward=1
ip netns exec network"${namespace}" sysctl -w net.ipv4.ip_forward=1
nets=(x y)
net_count=0
for _ in "${nets[@]}"; do
net_count=$((net_count + 1))
if [[ ${net_count} == 2 ]]; then
vlan=$((vlan + vlan_addition))
octet_c=$((octet_c + vlan_addition))
fi
octet_d=1
#echo "device:$device qdisc:$qdisc vlan:$vlan octet_a:$octet_a octet_b:$octet_b octet_c:$octet_c octet_d:$octet_d"
echo "#----------------------------------------------- ${device} ${qdisc}"
echo "device:${device} qdisc:${qdisc} vlan:${vlan} ${octet_a}.${octet_b}.${octet_c}.${octet_d}/24"
echo "### Creating vlan interface"
echo ip link add link "${nic}" name "${vlan_dev}"."${vlan}" type vlan id "${vlan}"
ip link add link "${nic}" name "${vlan_dev}"."${vlan}" type vlan id "${vlan}"
echo "### Moving vlan interface to network namespace"
echo ip link set dev "${vlan_dev}"."${vlan}" netns network"${namespace}"
ip link set dev "${vlan_dev}"."${vlan}" netns network"${namespace}"
#----------------------
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/linux-traffic-control_configuring-and-managing-networking
echo "### Configure cake qdisc"
case ${qdisc} in
noqueue)
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root noqueue
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root noqueue
;;
pfifo_fast)
# https://www.man7.org/linux/man-pages/man8/tc-pfifo_fast.8.html
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root pfifo_fast
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root pfifo_fast
;;
fq)
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root fq
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root fq
;;
fq_codel)
# https://www.man7.org/linux/man-pages/man8/tc-fq_codel.8.html
# https://linuxnet-qos.readthedocs.io/en/latest/qdiscs/fq_codel.html
# flows default is 1024
# memory_limit default is 32MB
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root fq_codel flows 4096 memory_limit 64MB
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root fq_codel flows 4096 memory_limit 64MB
;;
cake20)
# https://www.man7.org/linux/man-pages/man8/tc-cake.8.html
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root cake ether-vlan bandwidth "${bandwidth}" rtt 20ms ack-filter
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root cake ether-vlan bandwidth "${bandwidth}" rtt 20ms ack-filter
;;
cake40)
# https://www.man7.org/linux/man-pages/man8/tc-cake.8.html
echo ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root cake ether-vlan bandwidth "${bandwidth}" rtt 40ms ack-filter
ip netns exec network"${namespace}" tc qdisc replace dev "${vlan_dev}"."${vlan}" root cake ether-vlan bandwidth "${bandwidth}" rtt 40ms ack-filter
;;
*)
echo "unsupport qdisc"
;;
esac
echo ip netns exec network"${namespace}" tc -s qdisc ls dev "${vlan_dev}"."${vlan}"
ip netns exec network"${namespace}" tc -s qdisc ls dev "${vlan_dev}"."${vlan}"
echo "### Configure ip address on vlan interface"
echo ip netns exec network"${namespace}" ip address add "${octet_a}"."${octet_b}"."${octet_c}"."${octet_d}"/24 dev "${vlan_dev}"."${vlan}"
ip netns exec network"${namespace}" ip address add "${octet_a}"."${octet_b}"."${octet_c}"."${octet_d}"/24 dev "${vlan_dev}"."${vlan}"
echo "### Bring interface up"
echo ip netns exec network"${namespace}" ip link set dev "${vlan_dev}"."${vlan}" up
ip netns exec network"${namespace}" ip link set dev "${vlan_dev}"."${vlan}" up
echo "######### -----------------------"
echo ip netns exec network"${namespace}" ping -c 3 -w 1 "${octet_a}.${octet_b}.${octet_c}".10
done
echo "######### -----------------------"
echo ip netns exec network"${namespace}" ip link show
ip netns exec network"${namespace}" ip link show
echo "####"
echo ip netns exec network"${namespace}" ip addr show
ip netns exec network"${namespace}" ip addr show
echo "####"
echo ip netns exec network"${namespace}" ip route show
ip netns exec network"${namespace}" ip route show
done
echo "######### -----------------------"
echo find /run/netns/
find /run/netns/
echo find /run/netns/ | wc -l || true
find /run/netns/ | wc -l || true
echo "######### -----------------------"
for ns in "${namespaces[@]}"; do
echo ip netns exec network"${ns}" ip route show
ip netns exec network"${ns}" ip route show
done