How to use the command 'tc' (with examples)

How to use the command 'tc' (with examples)

The ’tc’ command is used to show and manipulate traffic control settings in Linux. It allows you to control network traffic by adding delays, introducing package corruption/loss/duplication, limiting bandwidth, and more.

Use case 1: Add constant network delay to outbound packages

Code:

tc qdisc add dev eth0 root netem delay delay_in_millisecondsms

Motivation: You may want to add a constant network delay to outbound packages to simulate network conditions for testing purposes or to troubleshoot network performance issues.

Explanation:

  • qdisc: Specifies the queuing discipline. In this case, we are using the ’netem’ queuing discipline.
  • add: Indicates that we want to add a new rule.
  • dev eth0: Specifies the network interface where the rule will be applied.
  • root: Indicates that the rule should be attached to the root qdisc.
  • netem: The specific rule type for introducing network emulation.
  • delay: Specifies the delay to be added to outbound packages in milliseconds.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that the rule has been added.

Use case 2: Add normal distributed network delay to outbound packages

Code:

tc qdisc add dev eth0 root netem delay mean_delay_msms delay_std_msms

Motivation: By adding a normal distributed network delay to outbound packages, you can simulate realistic network conditions for testing or analysis purposes.

Explanation:

  • mean_delay_ms: Specifies the mean delay to be added to outbound packages in milliseconds.
  • delay_std_ms: Specifies the standard deviation for the delay distribution in milliseconds.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that the rule has been added.

Use case 3: Add package corruption/loss/duplication to a portion of packages

Code:

tc qdisc add dev eth0 root netem corruption|loss|duplication effect_percentage%

Motivation: Introducing package corruption, loss, or duplication allows you to test the resilience and reliability of your network applications under different network conditions.

Explanation:

  • corruption|loss|duplication: Specifies the type of network effect to be applied. Choose one option: ‘corruption’ for introducing packet corruption, ’loss’ for packet loss, or ‘duplication’ for packet duplication.
  • effect_percentage: Specifies the percentage of packages to which the effect should be applied.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that the rule has been added.

Use case 4: Limit bandwidth, burst rate, and max latency

Code:

tc qdisc add dev eth0 root tbf rate max_bandwidth_mbmbit burst max_burst_rate_kbkbit latency max_latency_before_drop_msms

Motivation: Limiting bandwidth, burst rate, and maximum latency can help optimize network usage and prioritize traffic in scenarios where limited resources are available.

Explanation:

  • tbf: Specifies the queuing discipline as ’tbf’, which stands for token bucket filter.
  • rate: Specifies the maximum bandwidth allowed in megabits per second.
  • burst: Specifies the maximum burst rate allowed in kilobytes.
  • latency: Specifies the maximum latency before dropping packets in milliseconds.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that the rule has been added.

Use case 5: Show active traffic control policies

Code:

tc qdisc show dev eth0

Motivation: By showing the active traffic control policies, you can easily monitor and verify the current network settings on a specific network interface.

Explanation:

  • show: Indicates that we want to display the active traffic control policies.
  • dev eth0: Specifies the network interface for which we want to show the traffic control policies.

Example output:

qdisc netem 8001: root refcnt 2 limit 1000 delay 100.0ms

Use case 6: Delete all traffic control rules

Code:

tc qdisc del dev eth0

Motivation: Deleting all traffic control rules allows you to reset the network settings and remove any customization or restrictions that have been applied.

Explanation:

  • del: Indicates that we want to delete the traffic control rules.
  • dev eth0: Specifies the network interface from which we want to delete the traffic control rules.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that all traffic control rules have been deleted.

Use case 7: Change traffic control rule

Code:

tc qdisc change dev eth0 root netem policy policy_parameters

Motivation: Changing a traffic control rule allows you to modify the network settings and adapt them to specific requirements or network conditions.

Explanation:

  • change: Indicates that we want to change an existing traffic control rule.
  • dev eth0: Specifies the network interface where the rule is applied.
  • root: Indicates that the rule is attached to the root qdisc.
  • netem: The specific rule type for introducing network emulation.
  • policy: Specifies the policy name or parameters to be changed.

Example output: No output is generated if the command is successful. You can use tc qdisc show dev eth0 to verify that the rule has been changed.

Conclusion:

The ’tc’ command is a powerful tool for managing and controlling network traffic in Linux. From adding network delays to introducing package corruption or limiting bandwidth, ’tc’ provides a wide range of capabilities for network testing, troubleshooting, and optimization. Understanding the syntax and use cases of ’tc’ can greatly enhance your ability to manage and analyze network traffic effectively.

Tags :

Related Posts

Clear Command (with examples)

Clear Command (with examples)

The “clear” command is used to clear the screen of the terminal.

Read More
Terminal Based Typing Test (with examples)

Terminal Based Typing Test (with examples)

Introduction In the world of technology, typing skills are highly valued and essential.

Read More
How to use the command 'alien' (with examples)

How to use the command 'alien' (with examples)

Alien is a command-line tool that can be used to convert different installation packages to other formats.

Read More