Traffic control with tc

Vicente González Ruiz

January 31, 2024

Contents

 1 Controlling the latency
 2 Controlling the bit-rate
 References

1 Controlling the latency

See tc [1].

  1. Check the current configuration:

    tc qdisc show dev lo

    The output should be something like: “qdisc noqueue 0: root refcnt 2

  2. A link locally using netem:

    sudo tc qdisc add dev lo root netem delay <average_delay_in_miliseconds>ms <maximum_average_deviation_in_miliseconds>ms <Pearson_correlation_coefficient_expressed_as_a_percentage>% distribution <uniform|normal|pareto|paretonormal>

    where:

    qdisc:

    Use the default FIFO Queueing DISCipline for the outgoing traffic.

    add:

    Add a new traffic control rule.

    dev lo:

    The device affected by the rule. lo means loopback.

    root:

    The rule will be applied to all the outbound traffic (it’s the root rule of the possible tree of rules).

    netem:

    Use the network emulator to emulate a WAN.

    Example:

    1. Add the following rule:

      sudo tc qdisc add dev lo root netem delay 100ms 10ms 25% distribution normal
    2. Check that the rule has been installed with the command:

      tc qdisc show dev lo

      that should output (or something very similar): “ qdisc netem 8009: root refcnt 2 limit 1000 delay 100ms 10ms 25%

  3. Delete the tc rule with:

    sudo tc qdisc delete dev lo root netem delay <average_dalay_in_miliseconds>ms <maximum_average_deviation_in_miliseconds>ms <Pearson_correlation_coefficient_expressed_as_a_percentage>% distribution <uniform|normal|pareto|paretonormal>
  4. It is possible to change a working rule with:

    sudo tc qdisc change dev lo root netem delay <average_dalay_in_miliseconds>ms <maximum_average_deviation_in_miliseconds>ms <Pearson_correlation_coefficient_expressed_as_a_percentage>% distribution <uniform|normal|pareto|paretonormal>
  5. Lossing packets. For example, a packet loss ratio of \(10\%\) can be configured with tc by running:

    sudo tc qdisc add dev lo root netem loss 10%

2 Controlling the bit-rate

A bit-rate value can be simulated (locally) with:

  sudo tc qdisc add dev lo root handle 1: tbf rate <bit-rate_in_kbps>kbit burst 32kbit limit 32kbit 
  sudo tc qdisc add dev lo parent 1:1 handle 10: netem delay <average_delay_in_miliseconds>ms <maximum_average_deviation_in_miliseconds>ms <Pearson_correlation_coefficient_expressed_as_a_percentage>% distribution <uniform|normal|pareto|paretonormal>

Examples:

  1. Check the current configuration:

       tc qdisc show dev lo

    The output should be something like:

       qdisc noqueue 0: root refcnt 2
  2. Define the rule for bit-rate control (example for \(200\) kbps):

       sudo tc qdisc add dev lo root handle 1: tbf rate 200kbit burst 32kbit limit 32kbit
  3. Define the rule for latency control (example for \(100\) ms, \(10\) ms of jitter, and with the next random transmission depending on \(25\)% on the last):

       sudo tc qdisc add dev lo parent 1:1 handle 10: netem delay 100ms 10ms 25% distribution normal
  4. After adding these rules, this should be the configuration:

       tc qdisc show dev lo

    The output should be something like:

       qdisc tbf 1: root refcnt 2 rate 200Kbit burst 4Kb lat 0us 
       qdisc netem 10: parent 1:1 limit 1000 delay 100ms  10ms 25%

Recall to delete the rules after they are unnecessary (example to delete the previous rules):

  sudo tc qdisc delete dev lo parent 1:1 handle 10: netem delay 100ms 10ms 25% distribution normal 
  sudo tc qdisc delete dev lo root handle 1: tbf rate 200kbit burst 32kbit limit 32kbit

[1]   Bert Hubert, Thomas Graf, Greg Maxwell, Remco van Mook, Martijn van Oosterhout, Paul B. Schroeder, Jasper Spaans, and Pedro Larroy. Linux Advanced Routing & Traffic Control. Publisher: Bert Humbert et al., 2012.