Talk about traffic shaping network communication

Previous two articles "to talk about the network communication ACK, NACK and REX" and "talk FEC basic network communication" describes the retransmission packet loss and network communication theory and methods of FEC, they are remedial measures in case of network packet loss occurs, the paper is further forward, how to introduce traffic shaping technology, network as much as possible to avoid the occurrence of packet loss.

Speaking from traffic jam

v2-dbd54149c8a9baf674a0237ea5623f5d_hd.png

There are many reasons for traffic jams, let's talk about this diagram: Suppose a stock Road highway is 3, that is, every time a vehicle can simultaneously enter the parallel road is three, then, if a certain time period while trying to enter the road of vehicles more than three, then inevitably occur due to inadequate bearing capacity of the road to give the "traffic jam."


v2-e3fd9186c204581fb255c15e64838d65_hd.png

Similarly network transmission, network bandwidth is assumed 2Mbps, if the packet network to the second forced upon> 2Mbps, the network channel will not stand. However, its manifestations and that the road is not the same place: beyond the carrying capacity of the network packets may be card / router / switch to lose, that is, we often say that the loss (loss).


For network transmission, the cost of packet loss is very high, because after some important packet loss, is the need to "retransmission", and "retransmission" too much back and forth repeatedly to increase the data transfer delay, a further deterioration of the network load will ultimately greatly reduces the transmission efficiency.


Therefore, we need to reduce the "loss" is produced fundamentally as far as possible, in simple terms, is the amount of data sent to the network transmission within the control unit time, as smooth as possible and do not exceed the carrying capacity of the network bandwidth.

controlled object

If it wants to control the amount of data sent to the network transmission, you have to first find how data is generated, and is sent to the network through which links.

v2-189f08aa04249095d2b2e66e089ec583_hd.png

As shown, transmission of audio and video data, "source", nothing less than the local audio and video files, the microphone collected the audio stream, video stream captured by the camera, to capture the desktop screen flow, etc., are encoded and compressed packet processing, then through the "transmission module" is fed to the network.

设置和修改采集的配置(如:分辨率、帧率)、编码器的配置(如:GOP 间隔、码率)等,是可以减少实时产生的总数据量的,但是数据的产生并不是 “平滑” 的,特别是视频流/屏幕流,画面的突变,会带来数据量的突变,因此,送入到 “发送模块” 的数据量,也并不会总是 “平滑” 的。


从上面的 “堵车” 理论,为了避免丢包,我们需要尽可能地将数据 “平滑” 地送入网络中,因此,“流量整形” 在此派上了用场,它作用于 “发送模块”,目标是调整数据传输的平均速率,防止突发性的流量暴增导致网络拥塞和丢包。

流量整形

如何平输入和输出,一个最容易想到方法,就是增加 “缓冲”,让输入的数据先进入 “缓冲区” ,然后用恒定的 “速率” 从缓冲区取数据输出。这种方法称之为 “漏桶算法”。

漏桶算法(Leaky Bucket)

v2-da28afda819a75f0cd11aa4c5e05512b_hd.png

如图,使用一个 packet buffer(漏桶),把所有输入的 packet 缓存起来。


设置一个目标的输出码率(比如:3Mbps),固定的时间间隔(比如:10ms),读取 packet buffer(漏桶)中固定数量的 packet(如:3Mbit * 10ms / 1000 = 0.03Mbit)进行网络发送。注:如果某时刻缓冲区没有数据,则不用发送了。


漏桶算法的缺点

漏桶算法有一个明显的缺点,因为非常精准的网络带宽无法预判,那么假设你设置了一个比较小的目标码率(如 3Mbps),可能小于真实的网络带宽(如 10Mbps),这时,如果业务上产生了一些突发的流量,真实的网络带宽本可以允许更快地完成发送,但经过了漏桶算法后,依然会以恒定的目标码率慢慢地发送。所以说,漏桶算法无法充分用满网络资源来降低传输延时。


解决方案有 2 个:

  1. 先慢启动,然后将漏桶的目标码率持续上探,直到出现网络恶化(如:丢包增多)后再降下来,如此反复,维持一个动态平衡,使得漏桶算法的目标码率持续无限逼近网络的承载能力

  2. 改进漏桶算法,允许其在执行过程中,偶尔出现一些超过预设平均值的突发传输能力,用于应对业务上的流量突发,即:令牌桶算法


Token bucket algorithm (Token Bucket)

v2-c4cda3c67c50d6e4f01f6179dafe3991_hd.png

Token bucket algorithm on the basis of the leaky bucket algorithm, a proposed improvement is the addition of the "token" and "token bucket."


"Token" represents the number of bytes allowed transmissions, we fixed time intervals (for example: 10ms) is generated and fed into a "token" to "token bucket", "token bucket" setting a "token" the maximum number of (full consumption would be lost without the new token), thus allowing a maximum number of bytes transmitted spurt = M x B


A sending module, a fixed time interval t ms to read the packet buffer, the read byte number X must be close to but less than equal to the number of bytes permitted transmission "token bucket" current (ie: "token bucket" in the remaining "token number" x B). Further, the number of bytes transmitted over, then delete "token bucket" in the number corresponding to a "token."


The key point of this method to solve unexpected traffic where it?


In that the "token" is accumulated, the buffer may not have the front N ms data burst, then, a "token" is still generated, and is accumulated in the "token bucket" buffer once the projections increase the amount of data, you can quickly consumed in a short time. Of course, in order to prevent breakthrough network carrying capacity and packet loss, "token bucket" maximum "number of tokens" accordingly we made some restrictions.

summary

About traffic shaping network traffic in a brief introduction here, "leaky bucket" and "token bucket algorithm" in fact, has used in many places, such as limiting the service side of the downgrade, such as dropped frames smooth audio and video, etc. and, of course, there are also various improvement strategies based on these algorithms, here is not introduced one by one, we welcome the exchange of letters [email protected] in addition, I also welcome attention Sina microblogging @ Lu _ Chun or micro-channel public number @Jhuster get the latest articles and information.


Guess you like

Origin blog.51cto.com/ticktick/2473071