2015-04-02 48 views
1

我正在寻找模拟主机上不同端口上运行的一组服务的延迟。我想模拟对不同服务的不同延迟,可能有很多希望没有任何限制的给定主机。tc:将多个netem连接到接口

我发现这样做的唯一方法是使用prio qdisc。这里有一个例子:

IF=eth0 
tc qdisc add dev $IF root handle 1: prio bands 16 priomap 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

# this loop is only a sample to make the example runnable; values will vary in practice 
for handle in {2..16}; do 
# add a delay for the service on port 20000 + $handle 
tc qdisc add dev $IF handle $handle: parent 1:$handle netem delay 1000ms # example; this will vary in practice 
# add a filter to send the traffic to the correct netem 
tc filter add dev $IF pref $handle protocol ip u32 match ip sport $((20000 + $handle)) 0xffff flowid 1:$handle 
done 

如果你运行上面的命令,你会发现,处理11-16没有得到建立和失败,出现错误。

注意:这是上面的撤消。

IF=eth0 
for handle in {2..16}; do 
tc filter del dev $IF pref $handle 
tc qdisc del dev $IF handle $handle: parent 1:$handle netem delay 1000ms 
done 
tc qdisc del dev $IF root 

有没有办法给接口添加10个以上的netems?

回答

1

解决它使用HTB和类:

IF=eth0 
tc qdisc add dev $IF root handle 1: htb 
tc class add dev $IF parent 1: classid 1:1 htb rate 1000Mbps 

for handle in {2..32}; do 
tc class add dev $IF parent 1:1 classid 1:$handle htb rate 1000Mbps 
tc qdisc add dev $IF handle $handle: parent 1:$handle netem delay 1000ms 
tc filter add dev $IF pref $handle protocol ip u32 match ip sport $((20000 + $handle)) 0xffff flowid 1:$handle 
done