Use TC for linux-based traffic management

Source: Internet
Author: User
Use TC to implement linux-based traffic management-general Linux technology-Linux programming and kernel information. The following is a detailed description. Red Hat Linux 7.3 kernel 2.4.18 or above.
The external address eth0: 192.168.1.3 is bound to eth0 on the server.
Eth1 is bound to the internal address eth1: 1 172.17.1.1
Eth1: 2 172.18.1.1
Eth1: 3 172.19.1.1
  
The function to be implemented now is to limit the entire exit to 512 kbit (upload traffic), 172.17 download traffic to 512 Kbit, and 172.18 network segment to 128 kbit, the CIDR block of 172.19 is limited to 3 Mbit.
The method is as follows:
  
First, bind the corresponding address to implement route settings, using iptables.
  
# Iptables? A input-F
# Iptables-A output-F
# Iptables-A forward-F
# Echo 1>/proc/sys/net/ipv4/ip_forward
# Forwarding permitted
# Iptables-A input? J accept
# Iptables-A output-j accept
# Iptables-A forward-j accept
# Iptables-t nat-a postrouting-s 172.17.0.0/16-j MASQUERADE
# Iptables-t nat-a postrouting-s 172.18.0.0/16-j MASQUERADE
# Iptables-t nat-a postrouting-s 172.19.0.0/16-j MASQUERADE
  
# Mask IP addresses so that packets of internal hosts can be connected to the outside world through the server.
  
Manage traffic
  
# Tc qdisc add dev eth0 root tbf rate 512 k lantency 50 ms burst1540
  
# Use the TokenBucket Filter (tbf) to Filter the queue on the eth0 Nic. the outbound traffic is limited to 512 kbit, the delay is 50 ms, and the burst data is 1540. The specified rate value is the limited bandwidth.
Continue to limit eth1.
  
# Tc qdisc add dev eth1 root handle 1:0 cbq bandwidth 100 Mbit avpkt 1000 cell 8
  
# Create a queue and specify the NIC as a m Nic. This is irrelevant to the traffic limit and is used for computing.
  
# Tc class add dev eth1 parent 1:0 classid cbq bandwidth 100 Mbit rate 5 Mbit weight 6 Mbit prio 8 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
  
Create a root category. The bandwidth is limited to 5 Mbit and other bandwidths cannot be borrowed.
The parameters following Prio are priority values, indicating the order in which data packets are processed.
  
# Tc class add dev eth1 parent classid cbq bandwidth 100 Mbit rate 512 kbit weight 5 Mbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
  
Under the same class, the bandwidth limit of is 512 kbit, And the borrow bandwidth is not allowed for 172.17 network segments.
  
# Tc class add dev eth1 parent classid cbq bandwidth 100 Mbit rate 128 kbit weight 5 Mbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
  
Under the same class, you can create a class with a bandwidth limit of 128 kbit at. You cannot borrow the bandwidth for the 172.18 CIDR block.
  
# Tc class add dev eth1 parent classid cbq bandwidth 100 Mbit rate 3 Mbit weight 10 Mbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
  
Under the same class, create a class with a bandwidth limit of 3 Mbit at and do not allow the borrow bandwidth for the 172.19 CIDR block.
  
# Tc qdisc add dev eth1 parent :3 handle 30: sfq
# Tc qdisc add dev eth1 parent :4 handle 40: sfq
# Tc qdisc add dev eth1 parent :5 handle 50: sfq
  
Under each category, create a queue and use sfq (Stochastic Fareness Queueing) to then fair the queue.
  
# Tc filter add dev eth1 parent 1:0 protocol ip prio 1 u32 match ip dst 172.17.0.0/16 flowid :3
# Tc filter add dev eth1 parent 1:0 protocol ip prio 1 u32 match ip dst 172.18.0.0/16 flowid :4
# Tc filter add dev eth1 parent 1:0 protocol ip prio 1 u32 match ip dst 172.19.0.0/16 flowid :5
  
Use the u32 filter to classify the destination address and match the created queue.
  
Through the above method, simple traffic control is implemented to limit the upload traffic at the exit and the download traffic at the internal port.
  
The download speed of 172.18.1.2 is 128 kbit, And the download speed is 13-16.3kB.
The download speed of 172.19.1.2 is limited to 3 Mbit, And the download speed reaches 180-500kB.
Note: If the data packet does not match any rule, the rule set by root is sent.
  
It is found that the limit error of 3 M bandwidth is relatively large.
  
The above tests are based on a single machine and are not fully loaded.
  
The above articles are written in a rush. The TC filter involves a lot of things. There are many methods that can be implemented, either based on IP addresses or ports.

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.