


























Side note: Using a secondary network interface is recommended since the following commands could make a remote machine unreachable.
This a blog post about the basics of netem and tc on how to modify the outgoing traffic. You could modify the incoming traffic with an Intermediate Functional Block pseudo-device in Linux, but I am not too familiar with it and is out of scope for now.
There are various reasons why you want to modify the traffic between devices. The last time we had to ensure that a streaming server in Frankfurt could handle incoming video streams with a high latency over an unreliable connection from the US. The other time we had to provide proof that some SAP modules can't handle the additional latency of a VPN and that the problem is on their side and not ours.
This post tries to provide enough information to help you troubleshoot various problems quickly and simulate certain scenarios.
tc and netem provide are variety of options to shape the outgoing traffic.
Those options can be combined and will cover most of the cases.
tc stands for 'traffic control' and, as the name implies, is used to configure the traffic control of the Linux kernel and is part of the iproute2 package. Netem stands for 'network emulator' and is controlled by the tc command.
You can check quickly whether tc is available by typing tc -V.
kuser@pleasejustwork:~$ tc -V
tc utility, iproute2-5.15.0, libbpf 0.5.0
tc -p qdisc ls dev eth0kuser@pleasejustwork:~$ sudo tc -p qdisc ls dev eth0
qdisc netem 8001: root refcnt 2 limit 1000 delay 100ms 50ms
tc qdisc del dev eth0 rootSide note: the following options are temporary and don't survive a reboot!
A breakdown of a common tc command can be found in the first example below.
Unfortunately, it is not that easy to limit the applied options to a specific IP or port. It is possible, but outside the scope of this basic guide. To avoid problems, it is therefore recommended to use a secondary network interface.
I might rework this section at some point. For further reading, feel free to check the official documentation for the filters.
Almost every 'nenum' option can have one or more parameters. I thought it would make sense to show you the available units before we start with the practical part.
bit - Bits per secondkbit - Kilobits per secondmbit - Megabits per secondgbit - Gigabits per secondtbit - Terabits per secondbps - Bytes per secondkbps - Kilobytes per secondmbps - Megabytes per secondgbps - Gigabytes per secondtbps - Terabytes per secondus - Microsecondsms - Millisecondss - SecondsI am going to explain the syntax in the first scenario.
As a reference, this is the normal ICMP request/ ping over a separate interface.
kuser@pleasejustwork:~$ ping -c 10 -I eth0 10.10.22.1
PING 10.10.22.1 (10.10.22.1) from 10.10.22.51 eth0: 56(84) bytes of data.
64 bytes from 10.10.22.1: icmp_seq=1 ttl=255 time=0.458 ms
64 bytes from 10.10.22.1: icmp_seq=2 ttl=255 time=0.520 ms
64 bytes from 10.10.22.1: icmp_seq=3 ttl=255 time=0.453 ms
64 bytes from 10.10.22.1: icmp_seq=4 ttl=255 time=0.420 ms
64 bytes from 10.10.22.1: icmp_seq=5 ttl=255 time=0.513 ms
64 bytes from 10.10.22.1: icmp_seq=6 ttl=255 time=0.412 ms
64 bytes from 10.10.22.1: icmp_seq=7 ttl=255 time=0.550 ms
64 bytes from 10.10.22.1: icmp_seq=8 ttl=255 time=0.548 ms
64 bytes from 10.10.22.1: icmp_seq=9 ttl=255 time=0.402 ms
64 bytes from 10.10.22.1: icmp_seq=10 ttl=255 time=0.376 ms
--- 10.10.22.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9202ms
rtt min/avg/max/mdev = 0.376/0.465/0.550/0.060 ms
The netem latency will be added to the normal latency of the connection.
DELAY := delay TIME [ JITTER [ CORRELATION ]]
sudo tc qdisc add dev eth0 root netem delay 100mssudo # run command as sudotc # command stands for 'traffic control'qdisc # stands for 'Queue discipline'add|change|del # is the action that tc should perform to a optiondev eth0 # choosing the network interfaceroot # qdiscs IDnetem delay 100ms # 'netem' option + parameterResults
kuser@pleasejustwork:~$ sudo tc qdisc add dev eth0 root netem delay 100ms
[sudo] password for kuser:
kuser@pleasejustwork:~$ ping -c 10 -I eth0 10.10.22.1
PING 10.10.22.1 (10.10.22.1) from 10.10.22.51 eth0: 56(84) bytes of data.
64 bytes from 10.10.22.1: icmp_seq=1 ttl=255 time=101 ms
64 bytes from 10.10.22.1: icmp_seq=2 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=3 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=4 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=5 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=6 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=7 ttl=255 time=101 ms
64 bytes from 10.10.22.1: icmp_seq=8 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=9 ttl=255 time=100 ms
64 bytes from 10.10.22.1: icmp_seq=10 ttl=255 time=100 ms
--- 10.10.22.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9013ms
rtt min/avg/max/mdev = 100.416/100.466/100.586/0.050 ms
To remove this tc rule, send the same command again, but replace add with del.
sudo tc qdisc del dev eth0 root netem delay 100ms
If you want to add more Jitter - or in other words - variance in latency, add another parameter at the end. This is a plus/minus value.
sudo tc qdisc change dev eth0 root netem delay 100ms 50ms
Results
kuser@pleasejustwork:~$ ping -c 10 -I eth0 10.10.22.1
PING 10.10.22.1 (10.10.22.1) from 10.10.22.51 eth0: 56(84) bytes of data.
64 bytes from 10.10.22.1: icmp_seq=1 ttl=255 time=105 ms
64 bytes from 10.10.22.1: icmp_seq=2 ttl=255 time=88.6 ms
64 bytes from 10.10.22.1: icmp_seq=3 ttl=255 time=108 ms
64 bytes from 10.10.22.1: icmp_seq=4 ttl=255 time=109 ms
64 bytes from 10.10.22.1: icmp_seq=5 ttl=255 time=130 ms
64 bytes from 10.10.22.1: icmp_seq=6 ttl=255 time=54.5 ms
64 bytes from 10.10.22.1: icmp_seq=7 ttl=255 time=141 ms
64 bytes from 10.10.22.1: icmp_seq=8 ttl=255 time=102 ms
64 bytes from 10.10.22.1: icmp_seq=9 ttl=255 time=124 ms
64 bytes from 10.10.22.1: icmp_seq=10 ttl=255 time=146 ms
--- 10.10.22.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9011ms
rtt min/avg/max/mdev = 54.495/110.797/145.590/25.366 ms
The added latency will be in a range from 50-150ms from now on.
sudo tc qdisc change dev eth0 root netem duplicate 1%There are various reasons for packet loss: an unreliable network connection, network congestion, bugs, and so on.
sudo tc qdisc add dev eth0 root netem loss 20%Results
kuser@pleasejustwork:~$ ping -c 10 -I eth0 10.10.22.1
PING 10.10.22.1 (10.10.22.1) from 10.10.22.51 eth0: 56(84) bytes of data.
64 bytes from 10.10.22.1: icmp_seq=1 ttl=255 time=0.833 ms
64 bytes from 10.10.22.1: icmp_seq=2 ttl=255 time=0.414 ms
64 bytes from 10.10.22.1: icmp_seq=3 ttl=255 time=0.576 ms
64 bytes from 10.10.22.1: icmp_seq=4 ttl=255 time=0.443 ms
64 bytes from 10.10.22.1: icmp_seq=5 ttl=255 time=0.449 ms
64 bytes from 10.10.22.1: icmp_seq=6 ttl=255 time=0.510 ms
64 bytes from 10.10.22.1: icmp_seq=8 ttl=255 time=0.515 ms
64 bytes from 10.10.22.1: icmp_seq=10 ttl=255 time=0.302 ms
--- 10.10.22.1 ping statistics ---
10 packets transmitted, 8 received, 20% packet loss, time 9221ms
rtt min/avg/max/mdev = 0.302/0.505/0.833/0.145 ms
Introduced an error at a random position of the packet.
sudo tc qdisc change dev eth0 root netem corrupt 10%
As mentioned before, there are more advanced options, but this blog post should cover the basics.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。