原文:https://www.cyberciti.biz/faq/linux-traffic-shaping-using-tc-to-control-http-traffic/
I‘ve 10Mbps server port dedicated to our small business server. The server also act as a backup DNS server and I’d like to slow down outbound traffic on port 80. How do I limit bandwidth allocation to http service 5Mbps (burst to 8Mbps) at peak times so that DNS and other service will not go down due to heavy activity under a Linux operating systems?
You need use the tc command which can slow down traffic for given port and services on servers and it is called traffic shaping:
When traffic is shaped, its rate of transmission is under control, in other words you apply some sort of bandwidth allocation for each port or or so called Linux services. Shaping occurs on egress.
You can only apply traffic shaping to outgoing or forwarding traffic i.e. you do not have any control for incoming traffic to server. However, tc can do policing controls for arriving traffic. Policing thus occurs on ingress. This FAQ only deals with traffic shaping.
Token Bucket (TB)A token bucket is nothing but a common algorithm used to control the amount of data that is injected into a network, allowing for bursts of data to be sent. It is used for network traffic shaping or rate limiting. With token bucket you can define the maximum rate of traffic allowed on an interface at a given moment in time.
tokens/sec | | | | Bucket to | | to hold b tokens +======+=====+ | | | \|/ Packets | +============+ stream | ---> | token wait | ---> Remove token ---> eth0 | +============+The TB filter puts tokens into the bucket at a certain rate.
Each token is permission for the source to send a specific number of bits into the network.
Bucket can hold b tokens as per shaping rules.
Kernel can send packet if you’ve a token else traffic need to wait.
How Do I Use tc command?WARNING! These examples requires good understanding of TCP/IP and other networking concepts. All new user should try out examples in test environment.
tc command is by default installed on my Linux distributions. To list existing rules, enter:
# tc -s qdisc ls dev eth0
Sample outputs:
First, send ping request to cyberciti.biz from your Local Linux workstation and note down ping time, enter:
# ping cyberciti.biz
Sample outputs:
Type the following tc command to slow down traffic by 200 ms:
# tc qdisc add dev eth0 root netem delay 200ms
Now, send ping requests again:
# ping cyberciti.biz
Sample outputs:
To list current rules, enter:
# tc -s qdisc ls dev eth0
Sample outputs:
To delete all rules, enter:
# tc qdisc del dev eth0 root
# tc -s qdisc ls dev eth0