On Linux Traffic Queuing Discipline attached to a NIC can be used to shape the outgoing bandwidth. By default, Linux uses pfifo_fast as the queuing discipline. Use the following command to verify the setting on your network card
# tc qdisc show dev eth0 qdisc pfifo_fast 0: root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
Measuring the default bandwidth
I am using iperf tool to measure the bandwidth between my VirtualBox instance (10.0.2.15) and my Desktop (192.168.90.4) acting as the iperf server.
Start the iperf server with the following command
The client can then connect using the following command
iperf -c <server address>
The following figure shows the bandwidth available with default queue setting
Limiting Traffic with TC
We will use Token Bucket Filter to throttle the outgoing traffic. The following command sets an egress rate of 1024kbit at a latency of 50ms and a burst rate of 1540
# tc qdisc add dev eth0 root tbf rate 1024kbit latency 50ms burst 1540
Use the tc qdisc show command to verify the setting
# tc qdisc show dev eth0 tc qdisc add dev eth0 root tbf rate 1024kbit latency 50ms burst 1540
Verifying the result
I measured the bandwidth again to make sure the new queuing configuration is working and sure enough, the result from iperf confirmed it.
The following command shows the detailed statistics of the queuing discipline
Decreasing the latency number leads to packet drops, follow figure captures the result after latency was dropped to 1ms.
Providing a big burst buffer defeats the rate limiting
Both the parameters needs to be chosen properly to avoid packet loss and spike in traffic beyond the rate limit