TC
is a linux command using which you could manipulate traffic control settings.
In Linux, TC command
is used to Show or Manipulate traffic control settings
.
Why do you need to add small delay to a specific site? Think about this scenario:
- In Production site, you are getting some TimeoutExceptions and during that Exception your application is behaving abnormally.
- There are some other threads which may generate wrong data or empty data in case of TimeoutException.
- Now, if you want to debug your application then it’s absolutely not possible to mess with production environment until you find a solution.
- During this case, you can use your local environment or development environment for your testing.
- In local environment, you can run your Java application and once it’s started successfully, you could use
TC command
to generate delay and eventually TimeoutException.
Do you have below questions?
Then you are at right place 🙂
- How to simulate a delay to a certain IP over as certain port?
- Delaying network traffic to a specific IP
- linux – Simulate network latency on specific port using tc
- tc: Adding simulated
network latency
to your Linux server - How to remove tc delay?
- netem delay to specific ip and port
Here is a TC Command
Just put below all lines to file crunchify_tc_command.sh
.
#!/bin/bash tc qdisc del dev eth0 root tc qdisc add dev eth0 root handle 1: prio tc qdisc add dev eth0 parent 1:3 handle 30: tbf rate 20kbit buffer 1600 limit 3000 tc qdisc add dev eth0 parent 30:1 handle 31: netem delay 30000ms 10ms distribution normal tc filter add dev eth0 protocol ip parent 1:0 prio 3 u32 match ip dst 172.217.4.174/32 flowid 1:3
- 1st line: It will
delete
the rootqdisc
, it will be substituted by apfifo_fast
one . - 2nd line: Will substitute the pfifo_fast qdisc with the
prio
one. By default, the prio queue has 3 bands (0, 1, 2). - 4th line: Network Emulation (
netem
) provides the building blocks to create an impairment node. - 5th link: will match for specific IP address (172.217.4.174) and will introduce delay of
30000 milliseconds
.
Here 172.217.4.174
is an IP address of google.com
in my case. Just keep adding last line
with different IP address
if you have more than one IP address.
How to verify if TC command is working or not?
Some time back I’ve written a Java program to generate TimeoutException manually in java. You could use the same program.
Here are the steps:
- Change readTimeout to 5000 milliseconds
- Search for line
connection.setReadTimeout(10);
and change value from 10 to 5000connection.setReadTimeout(5000);
- Search for line
- Run the program
- You shouldn’t see socketTimeoutError anymore, that means you are getting result from google.com
- Next steps is to runcrunchify_tc_command.sh command
- bash-3.2$
sh crunchify_tc_command.sh
- bash-3.2$
- As you see in above script, we have added connect delay to 30 seconds
- As in Java Program we have 5 seconds timeout, your program will now start failing with
java.net.SocketTimeoutException: Read timed out
error.
How to undo TC command effect?
Just use below command to revert back shell script file changes.
tc qdisc del dev eth0 root
Hope this trick works well for you if you want to generate a delay connecting to any IP address for your project.