Using TC to control network traffic in Linux

Source: Internet
Author: User
Article title: using TC to control network traffic in Linux. Linux is a technology channel of the IT lab in China. Some basic categories including desktop applications, Linux system management, kernel research, embedded systems, and open source are well known. at the beginning of the Internet, colleges and research institutions communicate with each other, there is no consideration or design for network traffic control. The principle of IP protocol is to serve all data streams as well as possible, and different data streams are equal. However, many years of practice show that this principle is not the most ideal, and some data streams should be taken special care of. for example, interactive data streams with remote logon should have a higher priority than data downloads.

This possibility exists when different policies are adopted for different data streams. Furthermore, with the development and development of research, different management modes have been proposed. IETF has released several standards, such as Integrated Services and differentiated Services. In fact, the Linux kernel has implemented the traffic control function since. This document introduces the concept of traffic control in Linux, describes how to use the TC tool for traffic control, and provides several representative examples.

1. Related concepts

It can be seen that the packet group is received from the input Nic (entry) and searched through the route to determine whether the packet is sent to the local machine or needs to be forwarded. If it is sent to the local machine, it is directly submitted to the upper-layer protocol, such as TCP. if it is forwarded, it will be issued from the output Nic (exit. Network traffic is usually controlled at the output Nic. Although traffic control can be performed at the entrance of the router, Linux also has related functions, but in general, because we cannot control the devices outside of our network, it is relatively difficult to control the traffic at the entrance. This article will focus on traffic control at the exit. A basic concept of traffic control is the queue (Qdisc). Each NIC is associated with a queue (Qdisc). whenever the kernel needs to send packets from the NIC, the packet group is first added to the queue configured by the NIC, which determines the sending sequence of the packet group. Therefore, all traffic control occurs in the queue. for detailed flowchart, see.
 

Flowchart of packets in Linux

Some queues are very simple, and they implement a first-come-first-served policy for packet groups. In some queues, the functions are complex. different packet groups are queued and classified, and packet groups in the queue are sent in different order according to different principles. To implement such a function, these complex queues need to use different filters to divide packet groups into different classes ). These complex queues are called ClassfuI queues. Generally, to achieve powerful traffic control, classifier queues are essential. Therefore, class and Filter are two important basic concepts of traffic control. The following is an example of a classifier queue.
 

Multi-category queue

It can be seen that the CIass and Filter are both internal structures of queues, and the categorized queues can contain multiple categories. at the same time, A category can further contain sub-queues or sub-categories. All incoming packet groups can be placed in different sub-queues or sub-categories based on different principles, and so on. A Filter is a tool used by a queue to classify data packets. it determines the category of a data packet to be allocated.


II. use TC

In Linux, traffic control is implemented through the TC tool. Generally, you need to perform the following steps to configure traffic control for the NIC:

◆ Configure a queue for the NIC;

◆ Create a category on the queue;

◆ Create sub-queues and sub-categories as needed;

◆ Create a filter for each category.

In Linux, you can configure many types of queues, such as CBQ and HTB. CBQ is complex and hard to understand. HTB (HierarchicaIToken Bucket) is a classifier queue. compared with other complex queue types, HTB provides powerful functions, simple configuration, and easy to use. In TC, a handle like "major: minor" is used to identify the queue and category. both major and minor are numbers.

For a queue, minor is always 0, that is, in the form of "major: 0". It can also be abbreviated as "major: for example, queue 1:0 can be abbreviated as 1 :. It must be noted that major must be unique among all queues of a Nic. For a category, its major must be the same as its parent category or the major of its parent queue, minor must be unique within a queue (because the category must be included in a queue ). For example, if Queue 2 contains two classes, the handles of these two classes must be in the form of 2: x, and their x cannot be the same, such as and.

Next, we will describe the use of TC based on the requirements of the HTB queue. Assume that the eth0 egress has a bandwidth of 100 mbit/s and is allocated to WWW, email, and Telnet. the bandwidth allocated to WWW is 40 Mbit/s, the bandwidth allocated to Email is 40 Mbit/s, and the bandwidth allocated to Telnet is 20 Mbit/S. 3.

Note that the following abbreviations are used in TC to indicate the corresponding bandwidth:

◆ Kbps kiIobytes per second, that is, "kilobytes per second;

◆ Mbps megabytes per second, that is, "megabytes per second,

◆ Kbit kilobits per second, that is, "kilobytes per second;

◆ Mbit megabits per second, that is, "megabits per second.


3. create an HTB queue

The TC commands related to the queue are generally in the form:

# Tc qdisc [add | change | replace | link] dev DEV [parent qdisk-id | root] [handle qdisc-id] qdisc [qdisc specific parameters]

First, configure an HTB queue for eth0, and run the following command:

# Tc qdisc add dev eth0 root handle 1: htb default 11

Here, "add" indicates that you want to add, "dev eth0 indicates that the NIC to be operated is eth0. "Root indicates that eth0 is added as a root queue for the NIC. "Handle 1: indicates that the queue handle is 1 :. "Htb indicates that the queue to be added is an HTB queue. At the end of the command, "default 11 is a queue parameter specific to htb, meaning that all unclassified traffic will be allocated to class.

[1] [2] Next page

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.