玩转Openvwitch第七站:Port和QoS

对于控制一台机器的QoS,分两个方向,一个是入方向,一个是出方向。

e9dda0c938134de9c3b52c96cadf0cae.png

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

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

18a7a204946ee3d1d53f6c3a46783c53.png

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

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

3e0ed2baeb91faf1422541672e5686da.png

a777511a2aa8ccdd2e90d69d6b2e737a.png

823ab256f9b0c69f565c36d20460156f.png

d2dbcf269d2508ea160ed4ecb02e79ab.png

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

ec568a1e7435ee41625168678770303d.png

另外一种称为SFQ, Stochastic Fair Queuing

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

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

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

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

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

023d732a68c751e68cc77c48a4e84109.png

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

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

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

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

93ad96210e2a178cc22a238b64bb2705.png

另外一大类是Classful Queuing Disciplines。

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

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

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

Rate:设定的发送速度。

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

a83474be3d54ce67b51357ac86439779.png

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树。

20f66cc6bbca6aa7a49a7e99740028c5.png

创建一个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

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

bbe17dc8a56cc2cad1470494a595eac8.png

5b22c161e853cccdc559a2281f74c655.png

怎么解释这个图呢?

时间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设定

8936bbb2540eb5f44ca601c4b9680a1f.png

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

39b3b0c111ccdd78c5cb63f95c97e979.png

c79228b809498e623723af8acb6c04bc.png

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

2c85703bf4512339d874eba652828ba4.png

设置 一下first_if

ovs-vsctl set Interface first_if ingress_policing_rate=100000

ovs-vsctl set Interface first_if ingress_policing_burst=10000

1749ce6b1e709e829a5199f88719bd9b.png

清理现场

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"

89022c5556a957e093eb78dbc89e81d4.png

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

2c0bbac5fa0a593cec3d2a05cf52fa97.png

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

cd8784df01897b2e0f2502265caa03d9.png

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

430b7f226103d779c95319c2561c3ca7.png

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

3ab11612e19c89c5151c04ef15025737.png

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

33ecb3ad885d6d2db3f0ccf31ae532f2.png

清理环境

33220e2fd8a3dd757c21d96b2b883c73.png

9e7bf85a9721336c1182b5f056826b74.png

729362f10252a8f87461555dab88a310.png

22a53df88e20fc0dfc315dd18aa14ffc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

popsuper1982

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值