How to use the command 'tc' (with examples)
- Linux
- December 17, 2024
The ’tc’ command is a utility in Linux that allows system administrators to configure network traffic control settings. This command provides the tools for simulating various network conditions, managing bandwidth allocations, and monitoring active traffic control policies on network interfaces. It is particularly useful for network testing and management, allowing you to impose limitations or simulate conditions that can occur in real-world scenarios.
Use case 1: Add constant network delay to outbound packages
Code:
tc qdisc add dev eth0 root netem delay 100ms
Motivation:
In real-world network scenarios, varying amounts of latency can affect how applications perform. Adding a constant network delay can help simulate the conditions of networks like satellite or long-distance terrestrial networks, where consistent delays are common. This is invaluable for developers and testers to ensure applications perform optimally under these conditions.
Explanation:
tc
: Invokes the traffic control tool.qdisc
: Stands for “queueing discipline,” which is a method for traffic control.add
: Adds a new rule or configuration to the specified network interface.dev eth0
: Specifies the network device, witheth0
being a common default network interface name.root
: Indicates that the rule is applied to the root of the queuing discipline hierarchy for the specified device.netem
: Refers to the networking emulation capability oftc
, allowing for simulation of network actions like delay.delay 100ms
: Specifies a constant delay of 100 milliseconds for outbound packets.
Example Output:
After running the command, there isn’t a typical command-line output; however, network packets sent through eth0
will experience a 100ms delay.
Use case 2: Add normal distributed network delay to outbound packages
Code:
tc qdisc add dev eth0 root netem delay 100ms 10ms
Motivation:
Not all network delays are constant; they can vary in a more natural, Gaussian manner. Adding a normally distributed network delay helps simulate real-world network conditions like those experienced on mobile or wireless networks where environmental factors cause latency to fluctuate.
Explanation:
- All parameters as explained in Use Case 1 apply here, with two additions:
delay 100ms
: Specifies the mean (average) delay.10ms
: Indicates the standard deviation of delay, making the delay fluctuate around the mean by this amount.
Example Output:
As with the constant delay, no direct output is produced. Network packets will have an average delay of 100ms with a standard deviation of 10ms, creating a more realistic network simulation.
Use case 3: Add package corruption/loss/duplication to a portion of packages
Code:
tc qdisc add dev eth0 root netem loss 5%
Motivation:
In many networks, especially over long distances or unstable connections, packets can be lost, corrupted, or duplicated. Simulating these conditions can help developers optimize their apps for resilience and fault tolerance.
Explanation:
loss 5%
: This example shows adding a 5% packet loss on outbound traffic. The parameter specifies the percentage of packets that will be randomly dropped.
Example Output:
No visible command-line output; however, approximately 5 out of every 100 packets sent will be randomly dropped, mimicking an error-prone network.
Use case 4: Limit bandwidth, burst rate and max latency
Code:
tc qdisc add dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms
Motivation:
Network bandwidth can vary due to many factors. By limiting the bandwidth and configuring burst and latency, one can evaluate how applications will behave under constrained network environments similar to limited broadband or throttled connections.
Explanation:
tbf
: Token Bucket Filter, a network traffic control algorithm for traffic shaping.rate 10mbit
: Sets the bandwidth limit to 10 megabits per second.burst 32kbit
: Allows bursts up to 32 kilobits to accommodate short bursts of traffic.latency 400ms
: Defines the maximum delay before a packet is discarded.
Example Output:
The command doesn’t produce a visual output. However, the network traffic from eth0
will be limited to the parameters defined, which results in shaping the outgoing traffic to fit within these constraints.
Use case 5: Show active traffic control policies
Code:
tc qdisc show dev eth0
Motivation:
It’s important for network administrators to view current traffic configurations to ensure that rules are applied correctly, confirm network settings, and assist with debugging or performance tuning.
Explanation:
show
: Displays active configurations or policies on the specified device.
Example Output:
A display listing of current traffic control conditions set on eth0
, providing information on any delays, losses, or bandwidth restrictions currently active.
Use case 6: Delete all traffic control rules
Code:
tc qdisc del dev eth0
Motivation:
To revert to default network settings, it’s often necessary to remove all custom traffic control rules. This is useful when the network conditions need to be reset after testing or debugging is complete.
Explanation:
del
: Removes all queuing disciplines from the specified device, essentially clearing the configuration.
Example Output:
No output appears directly. However, any rules previously applied to eth0
are removed, and traffic will proceed without manipulation.
Use case 7: Change traffic control rule
Code:
tc qdisc change dev eth0 root netem delay 50ms
Motivation:
Instead of adding/removing rules, sometimes modifying existing traffic configurations is more efficient. Changing the settings on the fly helps in iterating quickly through different testing scenarios.
Explanation:
change
: Modifies an existing queuing discipline on the specified device.delay 50ms
: Updates the existing delay to a new value (50ms).
Example Output:
No command-line output is given. The existing delay network configuration on eth0
is modified to the new value of 50ms.
Conclusion:
The ’tc’ command is a versatile tool for managing network traffic, making it possible to simulate a wide array of real-world conditions and control bandwidth allocations efficiently. From adding delays or errors to limiting bandwidth, ’tc’ provides strong support for network testing, management, and optimization tasks.