LinuxCommandLibrary

tc

Traffic control: shaping and prioritizing network traffic

TLDR

Add constant network delay to outbound packages

$ sudo tc [[q|qdisc]] [[a|add]] dev [eth0] root netem delay [delay_in_milliseconds]ms
copy

Add normal distributed network delay to outbound packages
$ sudo tc [[q|qdisc]] [[a|add]] dev [eth0] root netem delay [mean_delay_ms]ms [delay_std_ms]ms
copy

Add package corruption/loss/duplication to a portion of packages
$ sudo tc [[q|qdisc]] [[a|add]] dev [eth0] root netem [corruption|loss|duplication] [effect_percentage]%
copy

Limit bandwidth, burst rate and max latency
$ sudo tc [[q|qdisc]] [[a|add]] dev eth0 root tbf rate [max_bandwidth_mb]mbit burst [max_burst_rate_kb]kbit latency [max_latency_before_drop_ms]ms
copy

Show active traffic control policies
$ tc [[q|qdisc]] [[s|show]] dev [eth0]
copy

Delete all traffic control rules
$ sudo tc [[q|qdisc]] [[d|delete]] dev [eth0]
copy

Change traffic control rule
$ sudo tc [[q|qdisc]] [[c|change]] dev [eth0] root netem [policy] [policy_parameters]
copy

SYNOPSIS

tc qdisc [add | change | replace | link | delete ] dev dev [parent qdisc-id | root] [handle qdisc-id] qdisc-type [qdisc-parameters]
tc class [add | change | replace | delete] dev dev parent qdisc-id classid class-id class-type [class-parameters]
tc filter [add | change | replace | delete] dev dev parent qdisc-id protocol protocol [prio priority] filter-type [filter-parameters] flowid flow-id

PARAMETERS

qdisc
    Specifies that you are working with a queuing discipline (qdisc).

class
    Specifies that you are working with a class within a qdisc.

filter
    Specifies that you are working with a filter to classify traffic.

add
    Adds a new qdisc, class, or filter.

change
    Modifies an existing qdisc, class, or filter.

replace
    Replaces an existing qdisc, class, or filter.

link
    Links a qdisc to a network interface.

delete
    Deletes an existing qdisc, class, or filter.

dev dev
    Specifies the network interface (e.g., eth0, wlan0).

parent qdisc-id
    Specifies the parent qdisc or class ID.

root
    Specifies that the qdisc is the root qdisc of the interface.

handle qdisc-id
    Assigns a handle to the qdisc for referencing it later.

classid class-id
    Assigns a class ID to a class.

qdisc-type
    Specifies the type of qdisc (e.g., htb, sfq, tbf).

class-type
    Specifies the type of class (e.g., htb).

protocol protocol
    Specifies the protocol for the filter (e.g., ip, arp).

prio priority
    Assigns a priority to the filter.

filter-type
    Specifies the type of filter (e.g., u32, fw).

flowid flow-id
    Specifies the flowid to which packets matching the filter are directed.

DESCRIPTION

The tc command in Linux is a powerful utility used to configure the Traffic Control (QoS) facilities provided by the Linux kernel.
It allows administrators to shape network traffic by prioritizing, delaying, policing, or dropping packets based on various criteria. This is achieved through complex queuing disciplines (qdiscs), classes, filters, and actions.
tc can be used to improve network performance under heavy load, guarantee bandwidth for critical applications, and limit bandwidth usage for less important traffic. It is a complex tool, requiring a good understanding of networking concepts and traffic shaping techniques. Common use cases include prioritizing VoIP traffic, limiting bandwidth for peer-to-peer applications, and implementing fair queuing algorithms. Configuration is typically persistent between reboots, requiring careful planning and testing. Incorrect configuration can severely impact network performance.
tc operates primarily on network interfaces to control the flow of packets at the kernel level.

CAVEATS

Incorrect tc configurations can severely degrade network performance and even lead to network outages. Testing and careful planning are essential. Different qdiscs, classes and filters have vastly different parameters that must be configured correctly. Traffic control is applied only on egress traffic by default.

COMMON <B><I>QDISCS</I></B>

HTB (Hierarchical Token Bucket): A classful qdisc that allows you to create a hierarchy of classes for prioritizing traffic.
SFQ (Stochastic Fairness Queuing): A classless qdisc that fairly distributes bandwidth among different flows.
TBF (Token Bucket Filter): A classless qdisc that limits the rate of traffic.

U32 FILTER

The u32 filter is a powerful, yet complex filter that allows you to match packets based on almost any field in the packet header. It requires a deep understanding of packet structures and bitwise operations.

HISTORY

The tc command and the associated Traffic Control subsystem have been a part of the Linux kernel for many years, evolving alongside advancements in networking technology. Initial development focused on providing basic queuing disciplines and traffic shaping capabilities. Over time, more sophisticated algorithms and features were added to support various network traffic management requirements. Development is ongoing, as new qdiscs and filtering mechanisms are implemented to address the increasing complexity of modern networks.

SEE ALSO

ip(8), iptables(8)

Copied to clipboard