Use of TC and Netem queues under Linux

Source: Internet
Author: User

A: Overview:

The flow controller (TC) in a Linux system is mainly to establish a queue at the output port for flow control.

TC is a tool that can classify a packet according to its characteristics, and can provide different bandwidths for various types of data, thus controlling their transmission speed. TC is part of the IPROUTE2, integrated in the 2.2 and above versions of the kernel, and can also work with various architectures in the Linux kernel, such as NetFilter Netem.


II: Components of TC

The TC is mainly composed of 3 components of the queue stipulation (QDISC), class and filter (filter), which are generally used in the circle to denote the queue, and the class is represented by a rectangle:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/9E/wKiom1RU0lKAEIb9AAFPHLlaSng302.jpg "title=" 1.png " alt= "Wkiom1ru0lkaeib9aafphllasng302.jpg"/>


1:QDISC:TC's core components, also known as queues, are an algorithm for managing network card input and outputting data to determine how packets are sent.

Queue rules can be divided into two categories:

(1) non-classified Qdisc: The internal does not contain a configurable subclass, the packet into the queue is not differentiated treatment, but only to rearrange the packet, delay sending or discard, mainly: Pfifo-fast. TBF. SFQ, etc.

(2) Classification Queue provisions: The internal can contain one or more sub-class, the use of filters to classify the packet, and then to the corresponding subclass processing, classification queue rules have CBQ,HTB, etc.


2: Class:

is the category of data, a variety of data through the filter classification, and finally put into the queue of the class to queue.

If a class has no subclasses, then this class is called the Leaf class, otherwise it becomes an inner class. 1:1 and 1:12 are internal classes, others are leaf classes, and leaf classes have a queue that is responsible for sending data for this class, and the Qdisc can be categorized, such as 1:10 there is a classification of the queue rules. The class's queue rules are usually referred to as Leaf Qdisc in TC (only the leaf class has a queue rule).


3: Filter

is a few rules, according to these rules to classify the packet, the filter can belong to the queue rules, can also belong to the internal class, if you need to classify the leaf class, it must be the filter and the leaf category of the classification queue rules, and can not be associated with the leaf class.

The most common is the U32 filter, which consists of a filter and an action, which the selector uses to match the packet and perform the action once the match is successful.


Three: The structure of the TC

is started with a root qdisc, if the root qdisc is not categorized by the queue rules, then it does not have subclasses, it is not possible to include other sub-objects, there will be no filter associated with it, when sending data, packets into the queue queued, and then according to the processing method of the queue to send the data packets.

The Qdisc of a taxonomy contains one or more classes, and each class can contain a queue stipulation or contain several subclasses, which can contain categorical or unclassified queue rules, so recursively, forming a tree.


Handle Number: Qdisc and classes are identified with a handle, and must be unique in a tree, each handle consists of a primary number and a secondary number Qdisc number must be 0 (0 can usually be omitted)

The handle to the root Qdisc is 1:1:0. The primary number of the handle to the class is the same as its parent (the parents or parent qdisc), such as the main number of class 1:1, which is the same as the primary number that contains his queue 1:1:10 and 1:11 are the same as the primary number of their parent class 1:1 and 1.

When you create a new class, the default is a non-categorical queue with a pfifo_fast type, and when a subclass is added, this type of QDISC is deleted, so the non-leaf class is not queue-defined, and the packet ends up queued to the queue of the leaf class.

If a class has subclasses, then these subclasses are allowed to compete for the bandwidth of the parent class, but the classes that are defined as the parents are not allowed to compete with each other's bandwidth.


Four: How the TC works:

For the Internet, everything is a packet, and the control network is actually manipulating the packet, manipulating how it is generated, routed, transmitted, fragmented, and so on. TC control when the packet leaves the system, between the IP layer and the network card tampering, in fact, is responsible for passing the packet to the physical layer is the TC module, which means that in the system kernel, TC as the dispatcher of the packet is always working, even when you do not want to use him, in general, TC maintenance A FIFO data queue.

When a packet is queued, it first invokes the filter specified by the root queue, and the packet is given to a class according to the rules defined by the filter, and if the class is not a leaf class, the filter of the class definition will be called further classified, and if the class does not define a filter, it will be handled by the default class containing his queue rules. If the class that receives the packet is a leaf class, the packet will go into the queue of the leaf class, and it should be noted that the filter can only give the packet to a class, and the class will queue the packet in its own queue instead of directly to a queue rule.

Accept the packet from the data interface, after the traffic limit, discard the non-compliant packet, and then enter the multi-channel allocator to determine: if the destination of the receiving packet is this host, then the package is sent to the book processing, otherwise forwarding, will accept the packet to the forwarding block processing. The forwarding block also receives packets generated from the upper layer of the host. The forwarding block determines the next hop of the processed package by looking at the routing table, and then sorts the packages so that they are routed to the output interface.

The TC of Linux is mainly processed and implemented when the output interface is arranged.




V: TC Command


1:add command: Add a Qdisc, class, or filter to a node. When adding, you need to pass an ancestor as a parameter, pass the parameter can use ID also can pass the device root directly, if build a qdisc or filter, can use handle to name, if build a class, use class identifier to name.


2:remove: Deletes the qdisc specified by a handle, the root Qdisc can also be deleted, all subclasses of the deleted Qdisc, and filters attached to each class are automatically deleted.


3:change: Modify some items in an alternative way, the handle and ancestor cannot be modified, and the change and add syntax are the same.


4:replace: Delete/Add to an existing node near atomic operation, if the node does not exist, this command will establish a node.


5:link: Applies only to Qdisc, instead of an existing node



TC Qdisc [add|change|replace|link] Dev Dev [parent qdisc-id |root] [handle Qdisc-id] Qdisc [qdisc specific parameters]


TC class [Add|change|replace] Dev Dev parent qdisc-id [classid Class-id] qdisc [qdisc specific parameters]


TC Filter [add|change|replace] Dev Dev [parent qdisc-id|root] Protocol protocol Prio Priority FilterType [FilterType Speci FIC parameters] Flowid Flow-id


TC [-s|-d] qdisc show [Dev Dev]


TC [-s|-d] class show Dev Dev


TC Filter Show Dev Dev


Six: TBF queue: (token bucket queue)


TC Qdisc Add TBF limit BYTES burst BYTES rate KBPS [MTU BYTES] [peakrate KBPS] [latency time] [overhead BYTES] [linklayer TYPE]

Rate is the first token bucket's fill speed

Peakrate is the fill rate for the second token bucket

Peakrate>rate


Burst is the size of the first token bucket

MTU is the size of the second token bucket

Burst>mtu


If the token in the token bucket is not enough, the packet will have to wait for a certain time, the time is controlled by the latency parameter, if the wait time exceeds latency, then the packet will be discarded


The limit parameter is a setting that allows the maximum number of data to wait in the queue

Latency=max ((Limit-burst)/rate, (LIMIT-MTU)/peakrate);

Burst should be greater than MTU and rate


Overhead represents the encapsulation overhead of the ADSL network for packets

The linklayer specifies the type of link, which can be Ethernet or ATM or ADSL

Both the ATM and ADSL header overhead are 5 bytes.



Seven:netem module

Netem provides the function of network policy, which can approach the nature of variable delay, packet loss, data packet copy and reordering of WAN.

The Netem is included in the Iproute2 in the kernel, and the Netem module is called by the TC control.

The Netem consists of two parts:

(1)qdisc kernel module: integrated into the kernel

(2) Configure the command line function of the module: part of the Iproute2

Both use the NetLink socket practice, and the command lines its request into a format that is decoded by the kernel.



1: Analog delay:

Example: #tc qdisc add dev eth0 root netem delay 100ms

All packets passed through the ETH device are delayed by 100ms


#tc Qdisc Change dev eth0 root netem delay 100ms 10ms

Indicates that the delay is random and the delay range is 100+_10ms


#tc Qdisc Change dev eth0 root netem delay 100ms 10ms 25%

Indicates that the delay range is: 100+_10ms*25%


2: Delay Distribution:

In the actual network, the delay is not evenly distributed, more like the normal distribution


#tc Qdisc Change dev eth0 root netem delay 100ms 20ms distribution Normal


Note: Allocations (such as Normal,pareto,paretonormal) are iproute2 compiled and placed under/USR/LIB/TC, so you can compile your own distribution


3: Packet loss


Configurable minimum packet loss rate of 232=0.000 000 232%


#tc Qdisc Change dev eth0 root netem loss 0.1%

Represents a 0.1% random packet drop


#tc Qdisc Change dev eth0 root netem loss 0.3% 25%

Causes 0.3% loss of packet rate

25% refers to correlations, which can be used to simulate sudden loss of packets


4: Packet Copy

#tc Qdisc Change dev eth0 root netem duplicate 1%



5: Packet Conflict error:

The resulting noise in the actual network causes the packet to fail and can be simulated with the corrupt option module


#tc Qdisc Change dev eth0 root netem corrupt 0.1%

can cause 0.1% errors.


6: Packet reordering


#tc Qdisc Change dev eth0 root netem gap 5 delay 10ms

Only 5 of multiple packets are not delayed, others are delayed by 10ms



#tc Qdisc Change dev eth0 root netem delay 10ms Recorder 25% 50%


25% of packets are sent immediately, other packets are delayed by 10ms

Here 50% refers to the correlation of 25% packets

In addition, different random delays can cause reordering



In the construction of packet loss, the linear correlation coefficient is used to set the distribution of packet loss, if 0, the packet loss is random: from 0-1, the distribution of packet drops tends to converge. For example, set the packet loss rate is 10%, the linear correlation coefficient is 1, it means that every 100 packets lost 10, but the 10 packets are very concentrated (it is possible 20 consecutive packets lost 10), it should be noted that the actual drop effect is not an exact number, more is a statistic.


This article is from the "Professor" blog, please be sure to keep this source http://professor.blog.51cto.com/996189/1570778

Use of TC and Netem queues under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.