Hello everyone, my name is Sasha, and I lead the backend testing team at FunCorp. Like many others, we have implemented a service-oriented architecture. On one hand, this simplifies work because each service is easier to test individually, but on the other hand, there is a need to test the interaction between services, which often occurs over the network.
In this article, I will discuss two utilities that can be used to test basic scenarios that describe application behavior when network issues arise.

Simulating Network Problems
Typically, software is tested on test servers with a good internet connection. In the harsh conditions of production, things may not be as smooth, so sometimes it is necessary to check programs in poor connection conditions. On Linux, the utility that can help simulate such conditions is tc.
tc (short for Traffic Control) allows you to configure the transmission of network packets within the system. This utility has extensive capabilities; you can read more about them . Here, I will only cover a few of them: we are interested in traffic scheduling, for which we use qdisc, and since we need to emulate an unstable network, we will use the classless qdisc .
Let's start an echo server on the machine (I used ):
ncat -l 127.0.0.1 12345 -k -c 'xargs -n1 -i echo "Response: {}"'
To detail all the timestamps at each step of the client-server interaction, I wrote a simple Python script that sends a request Test to our echo server.
Client Source Code
#!/bin/python
import socket
import time
HOST = '127.0.0.1'
PORT = 12345
BUFFER_SIZE = 1024
MESSAGE = "Testn"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t1 = time.time()
print "[time before connection: %.5f]" % t1
s.connect((HOST, PORT))
print "[time after connection, before sending: %.5f]" % time.time()
s.send(MESSAGE)
print "[time after sending, before receiving: %.5f]" % time.time()
data = s.recv(BUFFER_SIZE)
print "[time after receiving, before closing: %.5f]" % time.time()
s.close()
t2 = time.time()
print "[time after closing: %.5f]" % t2
print "[total duration: %.5f]" % (t2 - t1)
print data
Let's run it and observe the traffic on the interface lo and port 12345:
[user@host ~]# python client.py
[time before connection: 1578652979.44837]
[time after connection, before sending: 1578652979.44889]
[time after sending, before receiving: 1578652979.44894]
[time after receiving, before closing: 1578652979.45922]
[time after closing: 1578652979.45928]
[total duration: 0.01091]
Response: Test
Traffic Dump
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
10:42:59.448601 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [S], seq 3383332866, win 43690, options [mss 65495,sackOK,TS val 606325685 ecr 0,nop,wscale 7], length 0
10:42:59.448612 IP 127.0.0.1.12345 > 127.0.0.1.54054: Flags [S.], seq 2584700178, ack 3383332867, win 43690, options [mss 65495,sackOK,TS val 606325685 ecr 606325685,nop,wscale 7], length 0
10:42:59.448622 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 606325685 ecr 606325685], length 0
10:42:59.448923 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 606325685 ecr 606325685], length 5
10:42:59.448930 IP 127.0.0.1.12345 > 127.0.0.1.54054: Flags [.], ack 6, win 342, options [nop,nop,TS val 606325685 ecr 606325685], length 0
10:42:59.459118 IP 127.0.0.1.12345 > 127.0.0.1.54054: Flags [P.], seq 1:15, ack 6, win 342, options [nop,nop,TS val 606325696 ecr 606325685], length 14
10:42:59.459213 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [.], ack 15, win 342, options [nop,nop,TS val 606325696 ecr 606325696], length 0
10:42:59.459268 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [F.], seq 6, ack 15, win 342, options [nop,nop,TS val 606325696 ecr 606325696], length 0
10:42:59.460184 IP 127.0.0.1.12345 > 127.0.0.1.54054: Flags [F.], seq 15, ack 7, win 342, options [nop,nop,TS val 606325697 ecr 606325696], length 0
10:42:59.460196 IP 127.0.0.1.54054 > 127.0.0.1.12345: Flags [.], ack 16, win 342, options [nop,nop,TS val 606325697 ecr 606325697], length 0
Everything is standard: three-way handshake, PSH/ACK and ACK in response twice — this is the exchange of request and response between the client and server, and twice FIN/ACK and ACK — signaling the termination of the connection.
Packet delay
Now let's set a delay of 500 milliseconds:
tc qdisc add dev lo root netem delay 500ms
We run the client and see that the script now runs for 2 seconds:
[user@host ~]# ./client.py
[time before connection: 1578662612.71044]
[time after connection, before sending: 1578662613.71059]
[time after sending, before receiving: 1578662613.71065]
[time after receiving, before closing: 1578662614.72011]
[time after closing: 1578662614.72019]
[total duration: 2.00974]
Response: Test
What's in the traffic? Let’s take a look:
Traffic Dump
13:23:33.210520 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [S], seq 1720950927, win 43690, options [mss 65495,sackOK,TS val 615958947 ecr 0,nop,wscale 7], length 0
13:23:33.710554 IP 127.0.0.1.12345 > 127.0.0.1.58694: Flags [S.], seq 1801168125, ack 1720950928, win 43690, options [mss 65495,sackOK,TS val 615959447 ecr 615958947,nop,wscale 7], length 0
13:23:34.210590 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 615959947 ecr 615959447], length 0
13:23:34.210657 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 615959947 ecr 615959447], length 5
13:23:34.710680 IP 127.0.0.1.12345 > 127.0.0.1.58694: Flags [.], ack 6, win 342, options [nop,nop,TS val 615960447 ecr 615959947], length 0
13:23:34.719371 IP 127.0.0.1.12345 > 127.0.0.1.58694: Flags [P.], seq 1:15, ack 6, win 342, options [nop,nop,TS val 615960456 ecr 615959947], length 14
13:23:35.220106 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [.], ack 15, win 342, options [nop,nop,TS val 615960957 ecr 615960456], length 0
13:23:35.220188 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [F.], seq 6, ack 15, win 342, options [nop,nop,TS val 615960957 ecr 615960456], length 0
13:23:35.720994 IP 127.0.0.1.12345 > 127.0.0.1.58694: Flags [F.], seq 15, ack 7, win 342, options [nop,nop,TS val 615961457 ecr 615960957], length 0
13:23:36.221025 IP 127.0.0.1.58694 > 127.0.0.1.12345: Flags [.], ack 16, win 342, options [nop,nop,TS val 615961957 ecr 615961457], length 0
It can be seen that there was an expected half-second lag in the interaction between the client and the server. Much more interesting is how the system behaves when the lag is longer: the kernel starts to resend some TCP packets. Let's change the delay to 1 second and observe the traffic (I won't show the client's output, which is the expected 4 seconds in total duration):
tc qdisc change dev lo root netem delay 1s
Traffic Dump
13:29:07.709981 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [S], seq 283338334, win 43690, options [mss 65495,sackOK,TS val 616292946 ecr 0,nop,wscale 7], length 0
13:29:08.710018 IP 127.0.0.1.12345 > 127.0.0.1.39306: Flags [S.], seq 3514208179, ack 283338335, win 43690, options [mss 65495,sackOK,TS val 616293946 ecr 616292946,nop,wscale 7], length 0
13:29:08.711094 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [S], seq 283338334, win 43690, options [mss 65495,sackOK,TS val 616293948 ecr 0,nop,wscale 7], length 0
13:29:09.710048 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 616294946 ecr 616293946], length 0
13:29:09.710152 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 616294947 ecr 616293946], length 5
13:29:09.711120 IP 127.0.0.1.12345 > 127.0.0.1.39306: Flags [S.], seq 3514208179, ack 283338335, win 43690, options [mss 65495,sackOK,TS val 616294948 ecr 616292946,nop,wscale 7], length 0
13:29:10.710173 IP 127.0.0.1.12345 > 127.0.0.1.39306: Flags [.], ack 6, win 342, options [nop,nop,TS val 616295947 ecr 616294947], length 0
13:29:10.711140 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 616295948 ecr 616293946], length 0
13:29:10.714782 IP 127.0.0.1.12345 > 127.0.0.1.39306: Flags [P.], seq 1:15, ack 6, win 342, options [nop,nop,TS val 616295951 ecr 616294947], length 14
13:29:11.714819 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [.], ack 15, win 342, options [nop,nop,TS val 616296951 ecr 616295951], length 0
13:29:11.714893 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [F.], seq 6, ack 15, win 342, options [nop,nop,TS val 616296951 ecr 616295951], length 0
13:29:12.715562 IP 127.0.0.1.12345 > 127.0.0.1.39306: Flags [F.], seq 15, ack 7, win 342, options [nop,nop,TS val 616297952 ecr 616296951], length 0
13:29:13.715596 IP 127.0.0.1.39306 > 127.0.0.1.12345: Flags [.], ack 16, win 342, options [nop,nop,TS val 616298952 ecr 616297952], length 0
It is clear that the client sent a SYN packet twice, while the server responded with SYN/ACK twice.
In addition to a constant value, you can specify deviation, distribution function, and correlation (with the value from the previous packet) for the delay. This is done as follows:
tc qdisc change dev lo root netem delay 500ms 400ms 50 distribution normal
Here we set a delay ranging from 100 to 900 milliseconds, the values will be chosen according to a normal distribution, and there will be a 50 percent correlation with the delay value of the previous packet.
You might have noticed that I used add, and then change. The meaning of these commands is obvious, so I will just add that there is also del, which can be used to remove the configuration.
Packet loss
Now let's try to simulate packet loss. As stated in the documentation, this can be achieved in three ways: randomly losing packets with a certain probability, using a Markov chain of 2, 3, or 4 states to calculate packet loss, or employing the Elliott-Gilbert model. In this article, I will discuss the first (simplest and most straightforward) method, while information about the others can be found in the documentation. .
Let's simulate a 50% packet loss with a 25% correlation:
tc qdisc add dev lo root netem loss 50% 25%
Unfortunately, tcpdump won't visually demonstrate packet loss; we can only hypothesize that it actually works. We can confirm this through the increased and unstable script runtime. client.py (it may execute instantly or take up to 20 seconds), as well as the increased number of retransmitted packets:
[user@host ~]# netstat -s | grep retransmited; sleep 10; netstat -s | grep retransmited
17147 segments retransmited
17185 segments retransmited
Adding noise to packets
In addition to packet loss, we can simulate their corruption: noise will appear at a random position in the packet. Let's simulate packet corruption with a 50% probability and without correlation:
tc qdisc change dev lo root netem corrupt 50%
We run the client script (it's nothing interesting, but it ran for 2 seconds), and we check the traffic:
Traffic Dump
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
10:20:54.812434 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [S], seq 2023663770, win 43690, options [mss 65495,sackOK,TS val 1037001049 ecr 0,nop,wscale 7], length 0
10:20:54.812449 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [S.], seq 2104268044, ack 2023663771, win 43690, options [mss 65495,sackOK,TS val 1037001049 ecr 1037001049,nop,wscale 7], length 0
10:20:54.812458 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 1037001049 ecr 1037001049], length 0
10:20:54.812509 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1037001049 ecr 1037001049], length 5
10:20:55.013093 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1037001250 ecr 1037001049], length 5
10:20:55.013122 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [.], ack 6, win 342, options [nop,nop,TS val 1037001250 ecr 1037001250], length 0
10:20:55.014681 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [P.], seq 1:15, ack 6, win 342, options [nop,nop,TS val 1037001251 ecr 1037001250], length 14
10:20:55.014745 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [.], ack 15, win 340, options [nop,nop,TS val 1037001251 ecr 1037001251], length 0
10:20:55.014823 IP 127.0.0.1.43666 > 127.0.0.5.12345: Flags [F.], seq 2023663776, ack 2104268059, win 342, options [nop,nop,TS val 1037001251 ecr 1037001251], length 0
10:20:55.214088 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [P.], seq 1:15, ack 6, win 342, options [nop,unknown-65 0x0a3dcf62eb3d,[bad opt]
10:20:55.416087 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [F.], seq 6, ack 15, win 342, options [nop,nop,TS val 1037001653 ecr 1037001251], length 0
10:20:55.416804 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [F.], seq 15, ack 7, win 342, options [nop,nop,TS val 1037001653 ecr 1037001653], length 0
10:20:55.416818 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [.], ack 16, win 343, options [nop,nop,TS val 1037001653 ecr 1037001653], length 0
10:20:56.147086 IP 127.0.0.1.12345 > 127.0.0.1.43666: Flags [F.], seq 15, ack 7, win 342, options [nop,nop,TS val 1037002384 ecr 1037001653], length 0
10:20:56.147101 IP 127.0.0.1.43666 > 127.0.0.1.12345: Flags [.], ack 16, win 342, options [nop,nop,TS val 1037002384 ecr 1037001653], length 0
It is clear that some packets were sent multiple times and there is one packet with corrupted metadata: options [nop,unknown-65 0x0a3dcf62eb3d,[bad opt]>. But the main thing is that everything eventually worked correctly — TCP handled its task.
Packet duplication
What else can be done with netem? Например, сымитировать ситуацию, обратную потере пакетов, — дубликацию пакетов. Эта команда также принимает 2 аргумента: вероятность и корреляцию.
tc qdisc change dev lo root netem duplicate 50% 25%
Changing packet order
Packets can be shuffled in two ways.
In the first case, part of the packets is sent immediately, while the rest is sent with a specified delay. Example from the documentation:
tc qdisc change dev lo root netem delay 10ms reorder 25% 50%
With a probability of 25% (and a correlation of 50%), a packet will be sent immediately, while the others will be sent with a 10-millisecond delay.
The second way is when every N-th packet is sent instantly with a specified probability (and correlation), while the others are sent with a specified delay. Example from the documentation:
tc qdisc change dev lo root netem delay 10ms reorder 25% 50% gap 5
Every fifth packet will be sent without delay with a probability of 25%.
Changing bandwidth
Usually, everywhere it is referred to as , but you can also change the bandwidth of the interface using netem :
tc qdisc change dev lo root netem rate 56kbit
This command will make trips over localhost as painful as surfing the internet with a dial-up modem. In addition to setting the bitrate, you can also emulate the model of the data link layer protocol: set the overhead for the packet, the cell size, and the overhead for the cell. For example, this can simulate and a bitrate of 56 kbit/sec:
tc qdisc change dev lo root netem rate 56kbit 0 48 5
Simulating connection timeout
Another important point in the test plan during software acceptance is timeouts. This is important because in distributed systems, when one of the services is disconnected, the others must timely failover to others or return an error to the client, and they should not just hang while waiting for a response or establishing a connection.
There are several ways to do this: for example, use a mock that does not respond at all, or attach to the process using a debugger, set a breakpoint at the right place, and stop the process execution (this is probably the most twisted way). But one of the most obvious methods is to firewall ports or hosts. This will help us. .
For demonstration, we will firewall port 12345 and run our client script. We can firewall outgoing packets on this port at the sender or incoming ones at the receiver. In my examples, incoming packets will be firewalled (using chain INPUT and option —dport). Such packets can be dropped, rejected, or rejected with TCP flag RST; we can also use ICMP host unreachable (the default behavior is actually icmp-port-unreachable, and there is also the possibility to send in response icmp-net-unreachable, icmp-proto-unreachable, icmp-net-prohibited and icmp-host-prohibited).
DROP
With a DROP rule in place, packets will simply 'disappear.'
iptables -A INPUT -p tcp --dport 12345 -j DROP
We start the client and see that it hangs at the connection stage to the server. Let's check the traffic:
Traffic Dump
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
08:28:20.213506 IP 127.0.0.1.32856 > 127.0.0.1.12345: Flags [S], seq 3019694933, win 43690, options [mss 65495,sackOK,TS val 1203046450 ecr 0,nop,wscale 7], length 0
08:28:21.215086 IP 127.0.0.1.32856 > 127.0.0.1.12345: Flags [S], seq 3019694933, win 43690, options [mss 65495,sackOK,TS val 1203047452 ecr 0,nop,wscale 7], length 0
08:28:23.219092 IP 127.0.0.1.32856 > 127.0.0.1.12345: Flags [S], seq 3019694933, win 43690, options [mss 65495,sackOK,TS val 1203049456 ecr 0,nop,wscale 7], length 0
08:28:27.227087 IP 127.0.0.1.32856 > 127.0.0.1.12345: Flags [S], seq 3019694933, win 43690, options [mss 65495,sackOK,TS val 1203053464 ecr 0,nop,wscale 7], length 0
08:28:35.235102 IP 127.0.0.1.32856 > 127.0.0.1.12345: Flags [S], seq 3019694933, win 43690, options [mss 65495,sackOK,TS val 1203061472 ecr 0,nop,wscale 7], length 0
It is clear that the client is sending SYN packets with an exponentially increasing timeout. Here we found a small bug in the client: it needs to use the method settimeout(), to limit the time during which the client will attempt to connect to the server.
We immediately remove the rule:
iptables -D INPUT -p tcp --dport 12345 -j DROPYou can remove all rules at once:
iptables -F
If you are using Docker and you need to firewall all traffic going to a container, you can do it as follows:
iptables -I DOCKER-USER -p tcp -d CONTAINER_IP -j DROP
REJECT
Now we will add a similar rule, but with REJECT:
iptables -A INPUT -p tcp --dport 12345 -j REJECT
The client terminates after one second with the error [Errno 111] Connection refused. Let's check the ICMP traffic:
[user@host ~]# tcpdump -i lo -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
08:45:32.871414 IP 127.0.0.1 > 127.0.0.1: ICMP 127.0.0.1 tcp port 12345 unreachable, length 68
08:45:33.873097 IP 127.0.0.1 > 127.0.0.1: ICMP 127.0.0.1 tcp port 12345 unreachable, length 68
It is evident that the client received port unreachable twice and then terminated with an error.
REJECT with tcp-reset
Let's try adding the option —reject-with tcp-reset:
iptables -A INPUT -p tcp --dport 12345 -j REJECT --reject-with tcp-reset
In this case, the client immediately exits with an error because it received an RST packet on the very first request:
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
09:02:52.766175 IP 127.0.0.1.60658 > 127.0.0.1.12345: Flags [S], seq 1889460883, win 43690, options [mss 65495,sackOK,TS val 1205119003 ecr 0,nop,wscale 7], length 0
09:02:52.766184 IP 127.0.0.1.12345 > 127.0.0.1.60658: Flags [R.], seq 0, ack 1889460884, win 0, length 0
REJECT with icmp-host-unreachable
Let's try another option for using REJECT:
iptables -A INPUT -p tcp --dport 12345 -j REJECT --reject-with icmp-host-unreachable
The client terminates after one second with the error [Errno 113] No route to host, in the ICMP traffic we see ICMP host 127.0.0.1 unreachable.
You can also try other REJECT parameters, but I will stop here 🙂
Simulating request timeout
Another situation is when the client was able to connect to the server but cannot send it a request. How can we filter packets so that filtering doesn't start immediately? If you look at the traffic of any communication between the client and the server, you will notice that only SYN and ACK flags are used when establishing a connection, but in the last packet of the request, there will be a PSH flag. It is set automatically to avoid buffering. This information can be used to create a filter: it will allow all packets except those that contain the PSH flag. Thus, the connection will be established, but the client will not be able to send data to the server.
DROP
For DROP, the command will look as follows:
iptables -A INPUT -p tcp --tcp-flags PSH PSH --dport 12345 -j DROP
We start the client and observe the traffic:
Traffic Dump
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
10:02:47.549498 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [S], seq 2166014137, win 43690, options [mss 65495,sackOK,TS val 1208713786 ecr 0,nop,wscale 7], length 0
10:02:47.549510 IP 127.0.0.1.12345 > 127.0.0.1.49594: Flags [S.], seq 2341799088, ack 2166014138, win 43690, options [mss 65495,sackOK,TS val 1208713786 ecr 1208713786,nop,wscale 7], length 0
10:02:47.549520 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 1208713786 ecr 1208713786], length 0
10:02:47.549568 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1208713786 ecr 1208713786], length 5
10:02:47.750084 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1208713987 ecr 1208713786], length 5
10:02:47.951088 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1208714188 ecr 1208713786], length 5
10:02:48.354089 IP 127.0.0.1.49594 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1208714591 ecr 1208713786], length 5
We can see that the connection is established, but the client cannot send data to the server.
REJECT
In this case, the behavior will be the same: the client will not be able to send a request but will receive ICMP 127.0.0.1 tcp port 12345 unreachable and will increase the time between retransmitting the request exponentially. The command looks like this:
iptables -A INPUT -p tcp --tcp-flags PSH PSH --dport 12345 -j REJECT
REJECT with tcp-reset
The command looks as follows:
iptables -A INPUT -p tcp --tcp-flags PSH PSH --dport 12345 -j REJECT --reject-with tcp-reset
We already know that when using —reject-with tcp-reset the client will receive an RST packet in response, so we can predict the behavior: receiving an RST packet when the connection is established means an unexpected socket closure from the other side, which means the client should receive Connection reset by peer. We run our script and confirm this. Here’s how the traffic will look:
Traffic Dump
[user@host ~]# tcpdump -i lo -nn port 12345
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
10:22:14.186269 IP 127.0.0.1.52536 > 127.0.0.1.12345: Flags [S], seq 2615137531, win 43690, options [mss 65495,sackOK,TS val 1209880423 ecr 0,nop,wscale 7], length 0
10:22:14.186284 IP 127.0.0.1.12345 > 127.0.0.1.52536: Flags [S.], seq 3999904809, ack 2615137532, win 43690, options [mss 65495,sackOK,TS val 1209880423 ecr 1209880423,nop,wscale 7], length 0
10:22:14.186293 IP 127.0.0.1.52536 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 1209880423 ecr 1209880423], length 0
10:22:14.186338 IP 127.0.0.1.52536 > 127.0.0.1.12345: Flags [P.], seq 1:6, ack 1, win 342, options [nop,nop,TS val 1209880423 ecr 1209880423], length 5
10:22:14.186344 IP 127.0.0.1.12345 > 127.0.0.1.52536: Flags [R], seq 3999904810, win 0, length 0
REJECT with icmp-host-unreachable
I think everyone can see how the command will look 🙂 The client's behavior in this case will differ slightly from what it was with a simple REJECT: the client will not increase the timeout between attempts to retransmit the packet.
[user@host ~]# tcpdump -i lo -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
10:29:56.149202 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:56.349107 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:56.549117 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:56.750125 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:56.951130 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:57.152107 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
10:29:57.353115 IP 127.0.0.1 > 127.0.0.1: ICMP host 127.0.0.1 unreachable, length 65
Output
It's not necessary to write a mock to check the interaction of the service with a stalled client or server; sometimes it's enough to use standard utilities available in Linux.
The utilities discussed in the article have even more capabilities than described, so you can come up with your own ways to use them. Personally, I always find enough in what I've written (in fact, even less). If you use these or similar utilities for testing in your company, please share how you do it. If not, I hope your software will become more robust if you choose to test it under network issue conditions using the suggested methods.
Source: habr.com
