About tc in linux

Source: Internet
Author: User
Tc introduction under linux-Linux Enterprise Application-Linux server application information. The following is a detailed description. Linux supports QOS from kernel 2.1.105. However, you need to recompile the kernel. When running 'make config', set EXPERIMENTAL _ OPTIONS to 'y' and set Class Based Queueing (CBQ), Token Bucket Flow, and Traffic Shapers to 'y ', run 'make dep; make clean; make bzilo' to generate a new kernel.
In Linux, the traffic controller (TC) is mainly used to set up a queue at the output port for traffic control. The control method is based on routing, that is, traffic control based on the destination IP address or network number of the destination subnet. The basic function modules of the traffic controller TC are queue, classification, and filter. The queues supported In the Linux Kernel include Class Based Queue, Token Bucket Flow, CSZ, First In First Out, Priority, TEQL, SFQ, ATM, and RED. The Queue and Classification discussed here are Based on CBQ (Class Based Queue), while the filter is Based on routing (Route.
You can configure and use the traffic controller TC in the following aspects: Establish a queue, establish a classification, create a filter, and establish a route, in addition, you also need to monitor existing queues, categories, filters, and routes.
The basic steps are as follows:
1) bind a CBQ queue to network physical devices (such as the ethernet card eth0;
2) create a category on the queue;
3) create a routing-based filter for each category;
4) Finally, work with the filter to create a specific route table.

Assume a simple Environment
  
The IP address of the Ethernet Card (eth0) on the traffic controller is 192.168.1.66, and a CBQ queue is created on it. Assume that the average packet size is 1000 bytes, the packet interval sending unit is 8 bytes, and the maximum number of packets that can receive conflicting messages is 20 bytes.
If three types of traffic need to be controlled:
1) it is sent to host 1 and its IP address is 192.168.1.24. The traffic bandwidth is controlled at 8 Mbit with a priority of 2;
2) It is sent to host 2 and its IP address is 192.168.1.26. The traffic bandwidth is controlled at 1 Mbit with a priority of 1;
3) it is sent to subnet 1. Its subnet number is 192.168.1.0, and its subnet mask is 255.255.255.0. The traffic bandwidth is controlled at 1 Mbit and the priority is 6.

1. Create a queue
Generally, only one queue is required for one Nic.

Bind a cbq queue to the network physical device eth0 with the serial number of 1:0. The actual bandwidth of the network physical device eth0 is 10 Mbit, and the average package size is 1000 bytes; the size of the packet interval sending unit is 8 bytes, and the minimum transfer package size is 64 bytes.
· Tc qdisc add dev eth0 root handle 1: cbq bandwidth 10 Mbit avpkt 1000 cell 8 mpu 64

2. Create a category
The classification is based on the queue. Generally, you need to create a root category for a queue, and then create a subcategory on it. For a classification, the classification takes effect in the order of serial numbers, and smaller serial numbers take precedence. Once a data packet is sent through a classification matching rule, the subsequent classification does not take effect.

1) create a root category:, assign a bandwidth of 10 Mbit, and set the priority to 8.
· Tc class add dev eth0 parent 1:0 classid 1:1 cbq bandwidth 10 Mbit rate 10 Mbit maxburst 20 allot 1514 prio 8 avpkt 1000 cell 8 weight 1 Mbit

The maximum available bandwidth of this queue is 10 Mbit, the actually allocated bandwidth is 10 Mbit, and the maximum number of packets that can receive conflicting messages is 20 bytes; the maximum transmission unit and MAC header size are 1514 bytes, the priority is 8, the average packet size is 1000 bytes, and the packet interval sending unit size is 8 bytes, the weighted rate corresponding to the actual bandwidth is 1 Mbit.

2) create a category of, its parent category is, the allocated bandwidth is 8 Mbit, and the priority level is 2.
· Tc class add dev eth0 parent classid cbq bandwidth 10 Mbit rate 8 Mbit maxburst 20 allot 1514 prio 2 avpkt 1000 cell 8 weight 800 Kbit split 1:0 bounded

The maximum available bandwidth of this queue is 10 Mbit, the actually allocated bandwidth is 8 Mbit, and the maximum number of packets that can receive conflicting messages is 20 bytes; the maximum transmission unit and MAC header size are 1514 bytes, the priority is 1, the average packet size is 1000 bytes, and the packet interval sending unit size is 8 bytes, the weighted rate corresponding to the actual bandwidth is 800 Kbit, the separation point of the classification is, And the unused bandwidth cannot be borrowed.

3) create a category of, its parent category is, the allocated bandwidth is 1 Mbit, and the priority is 1.
· Tc class add dev eth0 parent classid cbq bandwidth 10 Mbit rate 1 Mbit maxburst 20 allot 1514 prio 1 avpkt 1000 cell 8 weight 100 Kbit split 1:0

The maximum available bandwidth of this queue is 10 Mbit, the actually allocated bandwidth is 1 Mbit, and the maximum number of packets that can receive conflicting messages is 20 bytes; the maximum transmission unit and MAC header size are 1514 bytes, the priority is 2, the average packet size is 1000 bytes, and the packet interval sending unit size is 8 bytes, the weighted rate corresponding to the actual bandwidth is 100 Kbit, and the separation point of the classification is.

4) create a category of, its parent category is, the allocated bandwidth is 1 Mbit, and the priority is 6.
· Tc class add dev eth0 parent classid cbq bandwidth 10 Mbit rate 1 Mbit maxburst 20 allot 1514 prio 6 avpkt 1000 cell 8 weight 100 Kbit split 1:0

The maximum available bandwidth of this queue is 10 Mbit, the actually allocated bandwidth is 64 Kbit, and the maximum number of packets that can receive conflicting messages is 20 bytes; the maximum transmission unit and MAC header size are 1514 bytes, the priority is 1, the average packet size is 1000 bytes, and the packet interval sending unit size is 8 bytes, the weighted rate corresponding to the actual bandwidth is 100 Kbit, and the separation point of the classification is.

3. Create a filter
Filters mainly serve classification. Generally, you only need to provide a filter for the root category and then provide route ing for each subcategory.

1) apply the route classifier to the root of the cbq queue. The parent classifier number is 1:0. The filtering protocol is ip, the priority is 100, and the filter is based on the route table.
· Tc filter add dev eth0 parent 1:0 protocol ip prio 100 route

2) create a route ing classification
· Tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 2 flowid
· Tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 3 flowid
· Tc filter add dev eth0 parent 1:0 protocol ip prio 100 route to 4 flowid

4. Create a route
This route maps to the previously created route.
1) data packets sent to host 192.168.1.24 are forwarded by Classification 2 (Classification 2 at a rate of 8 Mbit)
· Ip route add 192.168.1.24 dev eth0 via 192.168.1.66 realm 2

2) packets sent to host 192.168.1.30 are forwarded by Category 3 (rate of Category 3: 1 Mbit)
· Ip route add 192.168.1.30 dev eth0 via 192.168.1.66 realm 3

3) data packets sent to the subnet 192.168.1.0/24 are forwarded by Category 4 (the rate of Category 4 is 1 Mbit)
· Ip route add 192.168.1.0/24 dev eth0 via 192.168.1.66 realm 4

Note: We recommend that you use IP host address traffic control restrictions for the CIDR blocks directly connected by the traffic controller. Do not use subnet traffic control restrictions. If you need to use the subnet traffic control limit for the directly connected subnet, you must delete the original route established by the system before establishing the subnet route ing.

5. Monitoring
This module monitors the status of existing queues, categories, filters, and routes.
1) display the queue status

Simply display the queue status of the specified device (eth0 here)
· Tc qdisc ls dev eth0
Qdisc cbq 1: rate 10 Mbit (bounded, isolated) prio no-transmit

Displays the queue status of the specified device (eth0 here) in detail.
· Tc-s qdisc ls dev eth0
Qdisc cbq 1: rate 10 Mbit (bounded, isolated) prio no-transmit
Sent 7646731 bytes 13232 pkts (dropped 0, overlimits 0)
Borrowed 0 overactions 0 avgidle 31 undertime 0

It mainly shows that 13232 data packets are sent through the queue, the data traffic is 7646731 bytes, the number of discarded packets is 0, and the number of packets exceeding the rate limit is 0.

2) display the category status

Simple display of the classification status of the specified device (eth0 here)
· Tc class ls dev eth0
Class cbq 1: root rate 10 Mbit (bounded, isolated) prio no-transmit
Class cbq parent 1: rate 10 Mbit prio no-transmit # no-transmit indicates the priority is 8
Class cbq parent rate 8 Mbit prio 2
Class cbq parent rate 1 Mbit prio 1
Class cbq parent rate 1 Mbit prio 6

Detailed display of the category of the specified device (eth0 here)
· Tc-s class ls dev eth0
Class cbq 1: root rate 10 Mbit (bounded, isolated) prio no-transmit
Sent 17725304 bytes 32088 pkts (dropped 0, overlimits 0)
Borrowed 0 overactions 0 avgidle 31 undertime 0
Class cbq 1:1 parent 1: rate 10 Mbit prio no-transmit
Sent 16627774 bytes 28884 pkts (dropped 0, overlimits 0)
Borrowed 16163 overactions 0 avgid le 587 undertime 0
Class cbq parent rate 8 Mbit prio 2
Sent 628829 bytes 3130 pkts (dropped 0, overlimits 0)
Borrowed 0 overactions 0 avgid le 4137 undertime 0
Class cbq parent rate 1 Mbit prio 1
Sent 0 bytes 0 pkts (dropped 0, overlimits 0)
Borrowed 0 overactions 0 avgid le 159654 undertime 0
Class cbq parent rate 1 Mbit prio 6
Sent 5552879 bytes 8076 pkts (dropped 0, overlimits 0)
Borrowed 3797 overactions 0 avgid le 159557 undertime 0

The data packets sent by different categories, data traffic, number of dropped packets, number of packets exceeding the rate limit, and so on are displayed. Among them, the root category (class cbq 1:0) should be in a similar situation as the queue.
For example, the classification class cbq sends 8076 data packets at, the data traffic is 5552879 bytes, the number of discarded packets is 0, and the number of packets exceeding the rate limit is 0.

Display filter status
· Tc-s filter ls dev eth0
Filter parent 1: protocol ip pref 100 route
Filter parent 1: protocol ip pref 100 route fh 0xffff0002 flowid to 2
Filter parent 1: protocol ip pref 100 route fh 0xffff0003 flowid :3 to 3
Filter parent 1: protocol ip pref 100 route fh 0xffff0004 flowid :4 to 4

Here, flowid indicates the classification class cbq, And to 2 indicates sending through Route 2.

Displays the status of an existing route
· Ip route
192.168.1.66 dev eth0 scope link
192.168.1.24 via 192.168.1.66 dev eth0 realm 2
202.102.24.216 dev ppp0 proto kernel scope link src 202.102.76.5
192.168.1.30 via 192.168.1.66 dev eth0 realm 3
192.168.1.0/24 via 192.168.1.66 dev eth0 realm 4
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.66
172.16.1.0/24 via 192.168.1.66 dev eth0 scope link
127.0.0.0/8 dev lo scope link
Default via 202.102.24.216 dev ppp0
Default via 192.168.1.254 dev eth0

As shown above, the display line with realm at the end is a valid routing filter.

6. Maintenance
It mainly includes adding, modifying, and deleting queues, categories, filters, and routes.
The Add action is generally performed in the order of "queue-> category-> filter-> route". There is no requirement for modifying the action; the deletion is performed in the order of "route-> filter-> category-> queue.

1) queue Maintenance
Generally, for a traffic controller, a queue is configured for each Ethernet card at the factory hour. Generally, no additional, modification, or deletion actions are required for the queue.

2) classification Maintenance

Add
The add action is implemented using the tc class add command, as shown in the preceding figure.

Modify
The modify action is implemented using the tc class change command as follows:
· Tc class change dev eth0 parent classid cbq bandwidth 10 Mbit rate 7 Mbit maxburst 20 allot 1514 prio 2 avpkt 1000 cell 8 weight 700 Kbit split 1:0 bounded
The bounded command should be used with caution. Once added, the bounded command can only be deleted and then added.

Delete
The Delete action can be performed only when the category does not work. Once data is sent through the category, it cannot be deleted. Therefore, you must use the shell file to modify and restart to delete the file.

3) filter Maintenance

Add
The add action is implemented using the tc filter add command, as shown in the preceding figure.

Modify
The modify action is implemented using the tc filter change Command, as shown below:
· Tc filter change dev eth0 parent 1:0 protocol ip prio 100 route to 10 flowid

Delete
The Delete action is implemented using the tc filter del command, as shown below:
· Tc filter del dev eth0 parent 1:0 protocol ip prio 100 route to 10

4) Maintenance of one-to-one ing routing with Filters

Add
The add action is implemented using the ip route add command, as shown in the preceding figure.

Modify
The modification action is implemented by the ip route change command as follows:
· Ip route change 192.168.1.30 dev eth0 via 192.168.1.66 realm 8

Delete
The Delete action is implemented using the ip route del command as follows:
· Ip route del 192.168.1.30 dev eth0 via 192.168.1.66 realm 8
· Ip route del 192.168.1.0/24 dev eth0 via 192.168.1.66 realm 4
Related Article

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.