玩转Openvwitch第七站:Port和QoS

以下内容已屏蔽图片优化访问速度
对于控制一台机器的QoS,分两个方向,一个是入方向,一个是出方向。


[IMG]


其实能控制的只有出方向,通过Shaping,将出的流量控制成自己想要的模样。而进入的方向是无法控制的,只能通过Policy将包丢弃。


OpenVwitch中有关QoS的表结构如下:


[IMG]


我们可以通过TC控制网络的QoS,主要通过队列的方式。


第一大类称为Classless Queuing Disciplines,默认为pfifo_fast,是一种不把网络包分类的一种技术。


[IMG]


[IMG]


[IMG]


[IMG]


pfifo_fast根据网络包中的TOS对应的数字,在TOS的priomap中查看对应的Band,不同的Band对应的不同的队列。



[IMG]


另外一种称为SFQ, Stochastic Fair Queuing


有很多的FIFO的队列,TCP Session或者UDP stream会被分配到某个队列。包会RoundRobin的从各个队列中取出发送。


这样不会一个Session占据所有的流量。


但不是每一个Session都有一个队列,而是有一个Hash算法,将大量的Session分配到有限的队列中。


这样两个Session会共享一个队列,也有可能互相影响。


Hash函数会经常改变,从而session不会总是相互影响。


[IMG]


还有一种是TBF, Token Bucket Filter。两个概念Tokens and buckets,所有的包排成队列进行发送,但不是到了队头就能发送,而是需要拿到Token才能发送。


Token根据设定的速度rate生成,所以即便队列很长,也是按照rate进行发送的。


当没有包在队列中的时候,Token还是以既定的速度生成,但是不是无限累积的,而是放满了buckets为止,篮子的大小常用burst/buffer/maxburst来设定。


Buckets会避免下面的情况:当长时间没有包发送的时候,积累了大量的Token,突然来了大量的包,每个都能得到Token,造成瞬间流量大增。


[IMG]


另外一大类是Classful Queuing Disciplines。


其中典型的为HTB, Hierarchical Token Bucket。


Shaping:仅仅发生在叶子节点,依赖于其他的Queue。


Borrowing: 当网络资源空闲的时候,借点过来为我所用。


Rate:设定的发送速度。


Ceil:最大的速度,和rate之间的差是最多能向别人借多少。


[IMG]


type  of class
class  state
HTB  internal state
action  taken
leaf
< rate
HTB_CAN_SEND
Leaf  class will dequeue queued bytes up to available tokens  (no more than burst packets)
leaf
> rate,  < ceil
HTB_MAY_BORROW
Leaf  class will attempt to borrow tokens/ctokens from parent class. If tokens are  available, they will be lent in quantum increments and the leaf  class will dequeue up to cburst bytes
leaf
> ceil
HTB_CANT_SEND
No  packets will be dequeued. This will cause packet delay and will increase  latency to meet the desired rate.
inner,  root
< rate
HTB_CAN_SEND
Inner  class will lend tokens to children.
inner,  root
> rate,  < ceil
HTB_MAY_BORROW
Inner  class will attempt to borrow tokens/ctokens from parent class, lending them to  competing children in quantum increments per request.
inner,  root
> ceil
HTB_CANT_SEND
Inner  class will not attempt to borrow from its parent and will not lend tokens/ctokens  to children classes.


接下来举个具体的例子,通过TC构建一个如下的HTB树。


[IMG]


创建一个HTB的qdisc在eth0上,句柄为1:,default 12表示默认发送给1:12。


tc qdisc add dev eth0 root handle 1: htb default 12


创建一个root class,然后创建几个子class。


同一个root class下的子类可以相互借流量,如果直接不在qdisc下面创建一个root class,而是直接创建三个class,他们之间是不能相互借流量的。


tc class add dev eth0 parent 1: classid 1:1 htb rate 100kbps ceil 100kbps
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 30kbps ceil 100kbps
tc class add dev eth0 parent 1:1 classid 1:11 htb rate 10kbps ceil 100kbps
tc class add dev eth0 parent 1:1 classid 1:12 htb rate 60kbps ceil 100kbps


创建叶子qdisc,分别为fifo和sfq。


tc qdisc add dev eth0 parent 1:10 handle 20: pfifo limit 5
tc qdisc add dev eth0 parent 1:11 handle 30: pfifo limit 5
tc qdisc add dev eth0 parent 1:12 handle 40: sfq perturb 10


设定规则:从1.2.3.4来的,发送给port 80的包,从1:10走;其他从1.2.3.4发送来的包从1:11走;其他的走默认。


tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 1.2.3.4 match ip dport 80 0xffff flowid 1:10
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 1.2.3.4 flowid 1:11


例如对于如下的这棵树,如果将流量打满,则会出现下面的图。


[IMG]


[IMG]


怎么解释这个图呢?


时间0的时候,0,1,2都以90k的速度发送数据,在时间3的时候,将0的发送停止,红色的线归零,剩余的流量按照比例分给了蓝色的和绿色的线。


在时间6的时候,将0的发送重启为90k,则蓝色和绿色的流量返还给红色的流量。


在时间9的时候,将1的发送停止,绿色的流量为零,剩余的流量按照比例分给了蓝色和红色。


在时间12,将1的发送恢复,红色和蓝色返还流量。


在时间15,将2的发送停止,蓝色流量为零,剩余的流量按照比例分给红色和绿色。


在时间19,将1的发送停止,绿色的流量为零,所有的流量都归了红色。


那Openvswitch如何控制QoS呢?


Openvswitch支持两种:


Ingress policy


ovs-vsctl set Interface tap0 ingress_policing_rate=100000
ovs-vsctl set Interface tap0 ingress_policing_burst=10000


Egress shaping: Port QoS policy 仅支持HTB
在port上可以创建QoS
一个QoS可以有多个Queue
规则通过Flow设定


[IMG]


接下来,我们根据如下的拓扑图,测试QoS。



[IMG]


[IMG]


在什么都没有配置的时候,测试一下速度,从192.168.100.100 netperf 192.168.100.103


[IMG]


设置 一下first_if


ovs-vsctl set Interface first_if ingress_policing_rate=100000
ovs-vsctl set Interface first_if ingress_policing_burst=10000


[IMG]


清理现场


ovs-vsctl set Interface first_if ingress_policing_burst=0
ovs-vsctl set Interface first_if ingress_policing_rate=0
ovs-vsctl list Interface first_if


添加QoS


ovs-vsctl set port first_br qos=@newqos -- --id=@newqos create qos type=linux-htb other-config:max-rate=10000000 queues=0=@q0,1=@q1,2=@q2 -- --id=@q0 create queue other-config:min-rate=3000000 other-config:max-rate=10000000 -- --id=@q1 create queue other-config:min-rate=1000000 other-config:max-rate=10000000 -- --id=@q2 create queue other-config:min-rate=6000000 other-config:max-rate=10000000


添加Flow(first_br是ubuntu_br上的port 5)


ovs-ofctl add-flow ubuntu_br "in_port=6 nw_src=192.168.100.100 actions=enqueue:5:0"
ovs-ofctl add-flow ubuntu_br "in_port=7 nw_src=192.168.100.101 actions=enqueue:5:1"
ovs-ofctl add-flow ubuntu_br "in_port=8 nw_src=192.168.100.102 actions=enqueue:5:2"


[IMG]


单独测试从192.168.100.100,192.168.100.101,192.168.100.102到192.168.100.103


[IMG]


如果三个一起测试,发现是按照比例3:1:6进行的


[IMG]


如果Instance01和Instance02一起,则3:1


[IMG]


如果Instance01和Instance03一起,则1:2


[IMG]


如果Instance02和Instance03一起,则1:6


[IMG]


清理环境


[IMG]


[IMG]


[IMG]


[IMG]
学会这18个工具,你一定能真正理解如何监控网络带宽! 地下代孕链曝光:能站着挣的钱,就别躺着 吴秀波事件,你的精力分配决定了你的层次! 爆刷8.4亿!酷了几十年Supreme,却被大爷大妈玩成“土包子”! 金错刀X诺贝尔奖得主X巴勒斯坦首富X犹太教领袖X创业教父9天8夜考察以色列“二十”大爆品,探索犹太商道的逻辑
好看吗?
总执行时间0.07462072372436523,文章查询时间0.04800581932067871,分类查询时间0.009908914566040039,其他脚本0.0003247261047363281,模板渲染0.016381263732910156