How to add latency and bandwidth-limit interface using tc

tctraffic-shaping

I know I can add latency to an interface using this command:

# tc qdisc add dev eth0 root netem delay 50ms

In addition to latency, I want to limit the bandwidth of this interface to 100kbps.

How can I do this?

I've seen some examples of how to limit the bandwidth. For example:

# tc qdisc add dev eth0 root handle 1: cbq avpkt 1000 bandwidth 10mbit 

# tc class add dev eth0 parent 1: classid 1:1 cbq rate 512kbit \
  allot 1500 prio 5 bounded isolated 

# tc filter add dev eth0 parent 1: protocol ip prio 16 u32 

But if I try to run these commands after setting up the latency delay, I get errors that look like this:

RTNETLINK answers: File exists

For example:

# tc qdisc add dev eth0 root netem delay 97ms
# tc qdisc add dev eth0 root handle 1: cbq avpkt 1000 bandwidth 10kbit
RTNETLINK answers: File exists

I really want to be able to do both: Limit the bandwidth AND introduce a latency delay. Does anyone know how to accomplish this?

Best Answer

In your example, you are trying to add two root qdiscs on the egress. The second command fails because you already have a root qdisc, and only one is allowed. Instead, create the root qdisc, then a child class for it, and then add your second qdisc as a child of the class. For example:

tc qdisc add dev eth0 root handle 1:0 cbq avpkt 1000 bandwidth 10kbit
tc class add dev eth0 parent 1:0 classid 1:1 cbq rate 512kbit \
    allot 1500 prio 5 bounded isolated
tc qdisc add dev eth0 parent 1:1 netem delay 97ms

In this example, we assigned the root qdisc the handle 1:0. We then specified the class as a child of 1:0 and gave it the handle 1:1. We then added the second qdisc as a child of 1:1.

After you execute these commands, running tc qdisc should show both the cbq qdisc and the netem qdisc. The Linux Advanced Routing & Traffic Control HOWTO provides more information about qdiscs.

Related Topic