我一直在尝试在正在进行的学术项目中执行基于 VLAN 的速率限制。该项目使用 Openflow(带有 Beacon 控制器),并使用 Mininet 模拟网络。该网络由 OpenVSwitches 组成。
对于我的项目,我需要对网络中每个 VLAN 的流量进行速率限制。我为此选择了 tc(带 htb),并且已经尝试了很多天来让它工作。这些是我想使用 tc 实现的目标:1) 对所有具有特定数据速率的 VLAN 进行速率限制。2) 确保一旦设置了速率限制,所有使用交换机之间物理链路的 VLAN 都获得相同(或几乎相同)的最大数据速率。
因此,如果我将每个 VLAN 上的数据速率限制设置为 100 Mbps,并且有 5 个 VLAN 使用物理链路,我希望所有这些 VLAN 都不超过 100Mbps,同时每个 VLAN 的最大数据速率约为 100 Mbps。
我已经为这个拓扑编写了脚本:
s2---s4
|
s6---s8
|
s10---s12
这是脚本:
#!/bin/bash
# delete prev:
tc qdisc del dev s10-eth2 root
tc qdisc del dev s2-eth3 root
tc qdisc del dev s6-eth2 root
tc qdisc del dev s6-eth3 root
tc qdisc del dev s6-eth4 root
tc qdisc del dev s8-eth2 root
# create "roots":
tc qdisc add dev s10-eth2 root handle 1: htb default 1000
tc class add dev s10-eth2 parent 1: classid 1:1 htb rate 1000Mbit ceil 1000Mbit
tc qdisc add dev s2-eth3 root handle 2: htb default 1001
tc class add dev s2-eth3 parent 2: classid 2:1 htb rate 1000Mbit ceil 1000Mbit
tc qdisc add dev s6-eth2 root handle 3: htb default 1002
tc class add dev s6-eth2 parent 3: classid 3:1 htb rate 1000Mbit ceil 1000Mbit
tc qdisc add dev s6-eth3 root handle 4: htb default 1003
tc class add dev s6-eth3 parent 4: classid 4:1 htb rate 1000Mbit ceil 1000Mbit
tc qdisc add dev s6-eth4 root handle 5: htb default 1004
tc class add dev s6-eth4 parent 5: classid 5:1 htb rate 1000Mbit ceil 1000Mbit
tc qdisc add dev s8-eth2 root handle 6: htb default 1005
tc class add dev s8-eth2 parent 6: classid 6:1 htb rate 1000Mbit ceil 1000Mbit
# classes for all vlans (ie: VLAN ids 541, 542 and 543) on each interface:
tc class add dev s10-eth2 parent 1:1 classid 1:541 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s10-eth2 parent 1:1 classid 1:542 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s10-eth2 parent 1:1 classid 1:543 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s2-eth3 parent 2:1 classid 2:542 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s6-eth2 parent 3:1 classid 3:542 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s6-eth3 parent 4:1 classid 4:543 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s6-eth4 parent 5:1 classid 5:541 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s6-eth4 parent 5:1 classid 5:542 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s6-eth4 parent 5:1 classid 5:543 htb rate 100Mbit ceil 100Mbit prio 1
tc class add dev s8-eth2 parent 6:1 classid 6:543 htb rate 100Mbit ceil 100Mbit prio 1
# parent filter:
tc filter add dev s10-eth2 parent 1: prio 1 protocol 802.1q u32
tc filter add dev s2-eth3 parent 2: prio 1 protocol 802.1q u32
tc filter add dev s6-eth2 parent 3: prio 1 protocol 802.1q u32
tc filter add dev s6-eth3 parent 4: prio 1 protocol 802.1q u32
tc filter add dev s6-eth4 parent 5: prio 1 protocol 802.1q u32
tc filter add dev s8-eth2 parent 6: prio 1 protocol 802.1q u32
# filter for each VLAN (with VLAN ids 541, 542 and 543):
tc filter add dev s10-eth2 parent 1: prio 1 protocol 802.1q u32 match u16 0x021D 0x0fff at -4 flowid 1:541
tc filter add dev s10-eth2 parent 1: prio 1 protocol 802.1q u32 match u16 0x021E 0x0fff at -4 flowid 1:542
tc filter add dev s10-eth2 parent 1: prio 1 protocol 802.1q u32 match u16 0x021F 0x0fff at -4 flowid 1:543
tc filter add dev s2-eth3 parent 2: prio 1 protocol 802.1q u32 match u16 0x021E 0x0fff at -4 flowid 2:542
tc filter add dev s6-eth2 parent 3: prio 1 protocol 802.1q u32 match u16 0x021E 0x0fff at -4 flowid 3:542
tc filter add dev s6-eth3 parent 4: prio 1 protocol 802.1q u32 match u16 0x021F 0x0fff at -4 flowid 4:543
tc filter add dev s6-eth4 parent 5: prio 1 protocol 802.1q u32 match u16 0x021D 0x0fff at -4 flowid 5:541
tc filter add dev s6-eth4 parent 5: prio 1 protocol 802.1q u32 match u16 0x021E 0x0fff at -4 flowid 5:542
tc filter add dev s6-eth4 parent 5: prio 1 protocol 802.1q u32 match u16 0x021F 0x0fff at -4 flowid 5:543
tc filter add dev s8-eth2 parent 6: prio 1 protocol 802.1q u32 match u16 0x021F 0x0fff at -4 flowid 6:543
执行此脚本并进行传输后,统计数据如下:
qdisc htb 1: root refcnt 2 r2q 10 default 1000 direct_packets_stat 82134
Sent 5425380 bytes 82134 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
qdisc htb 2: root refcnt 2 r2q 10 default 1001 direct_packets_stat 306284
Sent 462713848 bytes 306284 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
qdisc htb 3: root refcnt 2 r2q 10 default 1002 direct_packets_stat 62794
Sent 4144482 bytes 62795 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
qdisc htb 4: root refcnt 2 r2q 10 default 1003 direct_packets_stat 0
Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
qdisc htb 5: root refcnt 2 r2q 10 default 1004 direct_packets_stat 831269
Sent 1257477000 bytes 831270 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
qdisc htb 6: root refcnt 2 r2q 10 default 1005 direct_packets_stat 0
Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
我正在尝试使用此脚本在所有 VLAN 上设置 100 Mbps 的数据速率限制。速率限制根本不起作用,事实上它们变得混乱。我的每台主机实现的数据速率都超过 300+ Mbps,这比我设置的要高得多,而且使用 VLAN 的所有数据流都没有统一的数据速率。
如果有人能帮助我修正并运行此脚本,那就太好了。谢谢。
答案1
我编写了一个脚本,用于对通过网桥的不同 VLAN 上的流量进行整形。我希望这对那些正在寻找解决方案的人有用。
#!/bin/bash
## in this example I use vlan 2001-2004, which are hex equivalents of 0x07D1 -0x07D4
# internet facing interface
INET=eth2
# downlink facing interface
DOWN=eth3
### configuring download rules:
tc qdisc del dev $DOWN root
tc qdisc replace dev $DOWN root handle 1: htb
tc class replace dev $DOWN parent 1: classid 1:2001 htb rate 11mbit
tc class replace dev $DOWN parent 1: classid 1:2002 htb rate 21mbit
tc class replace dev $DOWN parent 1: classid 1:2003 htb rate 31mbit
tc class replace dev $DOWN parent 1: classid 1:2004 htb rate 41mbit
tc filter replace dev $DOWN protocol all parent 1: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D1)" flowid 1:2001
tc filter replace dev $DOWN protocol all parent 1: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D2)" flowid 1:2002
tc filter replace dev $DOWN protocol all parent 1: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D3)" flowid 1:2003
tc filter replace dev $DOWN protocol all parent 1: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D4)" flowid 1:2004
printf "########## Printout for DOWNLOAD: ###################\n"
printf "\n"
printf "$(tc filter show dev $DOWN)\n"
printf "$(tc class show dev $DOWN)\n"
printf "$(tc qdisc show dev $DOWN)\n"
printf "\n"
### configuring upload rules:
tc qdisc del dev $INET root
tc qdisc replace dev $INET root handle 2: htb
tc class replace dev $INET parent 2: classid 2:2001 htb rate 11mbit
tc class replace dev $INET parent 2: classid 2:2002 htb rate 21mbit
tc class replace dev $INET parent 2: classid 2:2003 htb rate 31mbit
tc class replace dev $INET parent 2: classid 2:2004 htb rate 41mbit
tc filter replace dev $INET protocol all parent 2: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D1)" flowid 2:2001
tc filter replace dev $INET protocol all parent 2: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D2)" flowid 2:2002
tc filter replace dev $INET protocol all parent 2: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D3)" flowid 2:2003
tc filter replace dev $INET protocol all parent 2: prio 100 basic match "meta(vlan mask 0xfff eq 0x07D4)" flowid 2:2004
printf "########## Printout for UPLOAD: #####################\n"
printf "\n"
printf "$(tc filter show dev $INET)\n"
printf "$(tc class show dev $INET)\n"
printf "$(tc qdisc show dev $INET)\n"
printf "\n"
exit 0