Skip to content
Linux system performance · Chapter 13 of 21 Day 11: Network I – throughput, latency and lost packets

Day 11: Network I – throughput, latency and lost packets

Goal: know the network stack roughly, read the counters for throughput and errors, look at a single TCP connection from the inside, understand why latency limits throughput, and prove packet loss on the path · Duration: about 3 hours · Prerequisite: Day 0–10

Where we start

perf1 and last1 reach each other by name – even after a reboot (Day 0, Drill 1, Step 5). No fault is set up on last1 yet.

getent hosts asks the system’s name resolution:

# on perf1
getent hosts last1

Expected:

172.16.0.228    last1

tc qdisc show shows the queueing discipline of a network card (more on that shortly). ssh last1 BEFEHL runs the command on last1. It is best to set up the ssh access from perf1 to last1 once with ssh-copy-id last1; alternatively, run the commands for last1 in a separate terminal.

# on perf1
ssh last1 tc qdisc show dev eth0

Expected:

qdisc fq_codel 0: root refcnt 2 limit 10240p flows 1024 quantum 1514 target 5ms interval 100ms memory_limit 32Mb ecn drop_batch 64 

fq_codel is Ubuntu’s default queue. If it says netem, a fault is still active – sudo tc qdisc del dev eth0 root on last1 removes it.

Introduction

Network problems have a bad reputation because they are hard to pin down. An application is slow, and everyone points at the network – the network team measures the line and finds nothing. Yet from the host you can see a surprising amount: how many packets had to be resent, how large a connection’s send window is, how long a connection setup takes, whether a buffer is too small.

Today you learn this view from the host. First the counters – they tell you whether anything goes wrong at all. Then you look into a single TCP connection and see what it is failing at right now. With a kernel tool (tc netem) you build delay and packet loss between perf1 and last1 and measure the damage they do. Two results surprise almost everyone: 20 milliseconds of delay cost a single data stream 85 % of its throughput, and one percent of lost packets makes a download thirty times slower.

Today you learn:

  • to roughly follow a packet’s path through the network stack,
  • to read throughput and error counters with ip -s link, sar -n and nstat,
  • to look into a TCP connection with ss -ti: RTT, congestion window, limits,
  • to understand and measure the bandwidth-delay product,
  • to create delay and loss with tc netem,
  • to find retransmits on the sender side, also with tcpretrans.bt,
  • to measure the duration of connection setup with tcpconnlat.

Background

The picture for today: registered letters with return receipts

Think of a TCP connection as an exchange of letters with return receipts. perf1 sends numbered letters, last1 sends back a receipt for each letter.

  • The postal transit time there and back is the RTT.
  • How many letters may be on the way without a receipt is decided by the sender – that is the congestion window. The receiver also only has a mailbox of a certain size – that is the receive window.
  • If the post is slow, a lot must be on the way at the same time so that the mailbox is never empty. How much is the bandwidth-delay product. If the mailbox is too small, the fastest post is of no use.
  • If a receipt does not come, the sender sends the letter again – a retransmit. Only the sender knows about it; the receiver at most notices a gap in the numbers.
  • And the sender draws a conclusion from it: “The post is overloaded” – and to be safe sends far fewer letters at the same time. With regular loss it never gets going again.
  • tc netem is a postman you deliberately let dawdle or lose letters.

The terms

Term Meaning in the picture
RTT (round trip time) time for a packet there and the acknowledgment back postal transit time there and back
ACK the receiver’s acknowledgment return receipt
congestion window (cwnd) this much may be on the way unacknowledged, estimated by the sender letters without a receipt
receive window (rwnd, snd_wnd) this much the receiver can buffer right now size of the mailbox
BDP throughput × RTT: the window needed for full throughput letters that must be on the way
retransmit segment sent again letter sent again
qdisc queueing discipline in front of the network card (fq_codel, netem) the mail room in the building

Remember: A data stream manages at most window ÷ RTT. A longer transit time needs a larger mailbox – otherwise the mailbox slows you down, not the line.

A packet’s path

When a program sends data over a TCP connection, it writes it into the socket’s send buffer. The kernel’s TCP part splits it into segments, numbers them and sends them as soon as the congestion window (how much may be on the way unacknowledged) and the other side’s receive window allow. Below that come IP, the queueing discipline (qdisc, for example fq_codel), the driver and the network card. On the receiver side it goes the other way: network card, interrupt, softirq (Day 2), IP, TCP, receive buffer – until the program reads the data.

For each received segment the receiver sends an acknowledgment (ACK). If it does not arrive, the sender sends the segment again – a retransmit. Only the sender sees it: the receiver at most notices a gap.

The bandwidth-delay product

A TCP connection can send at most as much data per round trip as its window allows. So the throughput is at most window size ÷ RTT. Conversely, for a desired throughput you need a window of throughput × RTT – the bandwidth-delay product (BDP):

Throughput RTT Window needed
10 Gbit/s 0.5 ms (same data center) 625 KB
10 Gbit/s 20 ms (another city) 25 MB
1 Gbit/s 100 ms (another continent) 12.5 MB

Linux adjusts the buffers automatically (autotuning), but only up to the maximum values in net.ipv4.tcp_rmem and net.ipv4.tcp_wmem – on Ubuntu 6 MiB for receiving and 4 MiB for sending. Over long distances a single data stream is therefore often limited by the buffers, not by the line.

Packet loss

If a packet is lost, TCP must resend it and – this is the expensive part – assumes that the network is congested. The congestion control algorithm (on Ubuntu cubic) roughly halves the congestion window and slowly works its way back up. With regular loss the connection never really gets going. Rule of thumb: the achievable throughput falls roughly with the square root of the loss rate – at 1 % loss it is only a fraction of what the line can do.

tc netem

tc (traffic control) controls the queueing disciplines of a network card. The discipline netem (network emulator) is built for testing: it can delay, drop, duplicate or reorder outgoing packets. With it you build a path between two VMs like one over the internet – without any network hardware.

Drill 1: The counters

Where you are: on perf1.

What you have afterwards: seen the error counters of the network card and of TCP in the normal state.

Step 1 – The network card, totals since boot.

# on perf1
ip -s link show eth0
  • ip -s link show eth0 shows the interface with statistics (-s): bytes, packets, errors and dropped packets, separately for receiving (RX) and sending (TX). They are totals since boot.

Expected:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether bc:24:11:52:2f:dd brd ff:ff:ff:ff:ff:ff
    RX:   bytes packets errors dropped  missed   mcast
    13670583796  726133      0    2915       0       0
    TX:   bytes packets errors dropped carrier collsns
    49362619625  933472      0       0       0       0
    altname enp0s18

Step 2 – The same errors as a rate.

# on perf1
sar -n EDEV 1 1
  • sar -n EDEV shows network errors as a rate per second – that is what counts right now. 1 1 means one measurement over one second.

Expected (excerpt – the header and the lines for lo and eth0):

14:27:54        IFACE   rxerr/s   txerr/s    coll/s  rxdrop/s  txdrop/s  txcarr/s  rxfram/s  rxfifo/s  txfifo/s
14:27:55           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14:27:55         eth0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

ip -s link reports 2,915 dropped received packets since boot. Sounds worrying – but sar -n EDEV shows rxdrop/s = 0: nothing is being dropped right now. Totals say that something happened, rates say whether it is happening now. Where old drops came from often cannot be found out afterwards – the sar recording (sar -n EDEV -s …) helps.

Step 3 – The TCP counters. nstat reads the network stack counters from /proc/net/snmp and /proc/net/netstat. After the options you write the names of the counters you want to see:

# on perf1
nstat -az IpInReceives TcpActiveOpens TcpPassiveOpens TcpRetransSegs TcpExtTCPTimeouts TcpExtListenDrops
  • Without options, nstat shows the change since the last call; -a shows the totals since boot, -z also counters with the value 0.

Expected (your numbers are different; in the lab after building the whole course):

#kernel
IpInReceives                    5415248            0.0
TcpActiveOpens                  79                 0.0
TcpPassiveOpens                 616364             0.0
TcpRetransSegs                  380                0.0
TcpExtListenDrops               1343               0.0
TcpExtTCPTimeouts               56                 0.0

The first line #kernel only says that the values come from the kernel. The order is nstat’s, not the one in which you typed the names.

Counter Value Meaning
TcpActiveOpens / TcpPassiveOpens 79 / 616,364 connections opened or accepted since boot – the many accepted ones come from the load tests on Day 12
TcpRetransSegs 380 segments resent since boot – little with over 5 million received packets
TcpExtTCPTimeouts 56 the acknowledgment stayed away so long that TCP had to resend after a timeout; each costs at least 200 ms
TcpExtListenDrops 1,343 connections a server could not accept – the topic of Day 12, created there on purpose in the lab

On a fresh VM all values are small; what matters is knowing them before something goes wrong.

Remember: Totals say that something happened. Rates say whether it is happening now. For troubleshooting, the rate counts.

Save your progress? Sign up for free

Drill 2: A TCP connection from the inside

Where you are: in three terminals – one on last1, two on perf1.

What you have afterwards: measured the throughput between the VMs and looked into the running connection.

Step 1 – iperf3 as a service on last1.

# on last1
iperf3 -s -D
  • -s server, -D (daemon) lets it run in the background until you stop it; unlike -1 (Day 0) it accepts any number of measurements.

pgrep -a shows running processes with their command line:

# on last1
pgrep -a iperf3

Expected:

31704 iperf3 -s -D

Step 2 – A starting point for the counters. nstat -n sets the starting point without printing anything; the next call then shows only what has changed since:

# on perf1, terminal 1
nstat -n

Step 3 – Measure for 20 seconds.

# on perf1, terminal 1
iperf3 -c last1 -t 20
  • -c last1 client, connects to last1; -t 20 20 seconds.

Step 4 – Look inside. After about eight seconds in the second terminal:

# on perf1, terminal 2
ss -tin dst 172.16.0.228:5201
  • ss -tin lists TCP connections (-t) with internal details (-i) and numbers instead of names (-n). dst 172.16.0.228:5201 filters by the remote end.

Expected (the first connection cut off on the right):

State Recv-Q Send-Q  Local Address:Port  Peer Address:PortProcess
ESTAB 0      0        172.16.0.126:37270 172.16.0.228:5201
     cubic wscale:7,7 rto:202 rtt:1.738/0.715 ato:40 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:10 …
ESTAB 0      2639480  172.16.0.126:37276 172.16.0.228:5201
     cubic wscale:7,7 rto:202 rtt:1.559/0.435 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:3095 ssthresh:501 bytes_sent:7206489765 bytes_retrans:7104 bytes_acked:7205440102 segs_out:4978980 segs_in:33325 data_segs_out:4978978 send 22997100706bps lastrcv:7964 pacing_rate 29768535544bps delivery_rate 5663009232bps delivered:4978259 busy:7940ms rwnd_limited:1988ms(25.0%) sndbuf_limited:173ms(2.2%) unacked:720 retrans:0/5 dsack_dups:5 rcv_space:14480 rcv_ssthresh:64088 notsent:2215232 minrtt:0.146 snd_wnd:1935744

There are two connections: iperf3 uses a small one for control (cwnd:10, hardly any data) and one for the measurement. The measurement connection shows the most important values of a TCP connection:

Field Value Meaning
cubic – the congestion control algorithm
rtt:1.559/0.435 1.6 ms smoothed RTT in ms and its variation; minrtt:0.146 is the smallest value – under load the RTT grows because packets pile up in buffers
cwnd:3095 3,095 congestion window in segments: 3,095 × 1,448 bytes ≈ 4.5 MB may be on the way unacknowledged
delivery_rate ≈ 5.7 Gbit/s most recently measured throughput
rwnd_limited:1988ms(25.0%) 25 % this long the other side’s mailbox was full
sndbuf_limited 2.2 % this long the own send buffer was full
Send-Q 2639480 2.6 MB bytes in the send buffer, not yet acknowledged
bytes_retrans:7104, retrans:0/5 5 segments resent – visible as retransmits in Step 5

rwnd_limited and sndbuf_limited tell you who is slowing things down.

Step 5 – The result. When iperf3 in terminal 1 is done:

Expected (terminal 1, excerpt – the two summary lines):

[  5]   0.00-20.00  sec  17.1 GBytes  7.36 Gbits/sec    7             sender
[  5]   0.00-20.00  sec  17.1 GBytes  7.35 Gbits/sec                  receiver

The 7 in the sender line is the retransmits.

Step 6 – What happened in the counters? nstat without -a shows the change since nstat -n; it leaves out counters that have not changed:

# on perf1
nstat TcpOutSegs TcpRetransSegs

Expected:

#kernel
TcpOutSegs                      12708954           0.0
TcpRetransSegs                  7                  0.0

12.7 million segments sent, 7 of them resent – a share of 0.00006 %. 7.4 Gbit/s with almost no retransmits – that is the baseline from Day 0 (there 7.62 Gbit/s and 1 retransmit), now with a look inside. If TcpRetransSegs had not increased, nstat would leave out the line entirely.

Remember: ss -ti is the X-ray of a connection: RTT, window, throughput – and with rwnd_limited/sndbuf_limited the name of whoever is slowing things down.

Save your progress? Sign up for free

Drill 3: Latency limits throughput

Where you are: in three terminals – last1 and twice perf1.

What you have afterwards: measured how 20 ms of delay slows down a single data stream, and seen the remedy with larger buffers.

Step 1 – Add 20 ms of delay.

# on last1
sudo tc qdisc add dev eth0 root netem delay 20ms
Option Meaning
qdisc add dev eth0 create a queueing discipline on eth0
root at the root – it replaces fq_codel
netem delay 20ms hold every outgoing packet for 20 ms

Because last1 delays all replies to perf1, the RTT between the two rises by 20 ms – the postman dawdles. Your SSH session to last1 also becomes somewhat more sluggish.

# on last1
tc qdisc show dev eth0

Expected:

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

Step 2 – The RTT. ping -q only prints the summary:

# on perf1
ping -c 5 -q last1

Expected (excerpt – the last line):

rtt min/avg/max/mdev = 20.478/20.659/20.968/0.169 ms

Step 3 – One data stream.

# on perf1
iperf3 -c last1 -t 15

Expected (excerpt – the sender and receiver lines):

[  5]   0.00-15.00  sec  2.32 GBytes  1.33 Gbits/sec    0             sender
[  5]   0.00-15.02  sec  2.32 GBytes  1.33 Gbits/sec                  receiver

Step 4 – Four data streams. -P 4 opens four parallel streams; SUM is their total:

# on perf1
iperf3 -c last1 -t 15 -P 4

Expected (excerpt – the SUM lines at the end):

[SUM]   0.00-15.00  sec  8.52 GBytes  4.88 Gbits/sec    0             sender
[SUM]   0.00-15.02  sec  8.52 GBytes  4.87 Gbits/sec                  receiver

One data stream: 1.33 Gbit/s instead of about 8 – without a single retransmit. Four streams: 4.88 Gbit/s. So no packet is lost, and the line could do more. Who is slowing things down?

Step 5 – How large may the buffers get? sysctl reads kernel settings; the three numbers are the minimum, start value and maximum of the buffers in bytes:

# on perf1
sysctl net.ipv4.tcp_rmem net.ipv4.tcp_wmem

Expected:

net.ipv4.tcp_rmem = 4096    131072  6291456
net.ipv4.tcp_wmem = 4096    16384   4194304

Step 6 – Look into the connection. A twelve-second measurement; this time you do not need the summary:

# on perf1, terminal 1
iperf3 -c last1 -t 12 > /dev/null

After about six seconds in the second terminal:

# on perf1, terminal 2
ss -tin dst 172.16.0.228:5201

You again see two connections. Read the measurement connection – the one without cwnd:10.

Expected (excerpt – only these fields from the long line of the measurement connection):

rtt:21.113/0.309
cwnd:5832
rwnd_limited:2171ms(36.7%)
sndbuf_limited:2258ms(38.1%)
snd_wnd:3779200

The connection was limited 37 % of the time by the receive window and 38 % by its own send buffer. last1’s receive window (snd_wnd) is 3.8 MB – the mailbox. Do the math: 3.8 MB ÷ 21 ms ≈ 180 MB/s ≈ 1.4 Gbit/s – exactly the measured throughput. For 8 Gbit/s at 21 ms you would need a window of 21 MB (BDP).

Step 7 – Larger buffers. Raise the maximum to 32 MiB on both sides. last1 receives, so tcp_rmem counts there:

# on last1
sudo sysctl -w net.ipv4.tcp_rmem="4096 131072 33554432"

Expected:

net.ipv4.tcp_rmem = 4096 131072 33554432

perf1 sends, so tcp_wmem counts there:

# on perf1
sudo sysctl -w net.ipv4.tcp_wmem="4096 16384 33554432"

Expected:

net.ipv4.tcp_wmem = 4096 16384 33554432

sysctl -w sets a value until the next reboot (Day 15).

# on perf1
iperf3 -c last1 -t 15

Expected (excerpt – the sender and receiver lines):

[  5]   0.00-15.00  sec  11.8 GBytes  6.78 Gbits/sec    0             sender
[  5]   0.00-15.02  sec  11.8 GBytes  6.77 Gbits/sec                  receiver

6.78 Gbit/s with a single stream – five times as much as with the default values. For connections over long distances (replication to a second data center, remote backups) this is one of the most effective settings of all. Permanently it belongs in a file under /etc/sysctl.d/ – more on Day 15.

Step 8 – Back to normal.

# on last1
sudo sysctl -w net.ipv4.tcp_rmem="4096 131072 6291456"
# on last1
sudo tc qdisc del dev eth0 root
# on last1
tc qdisc show dev eth0

Expected: qdisc fq_codel 0: root … again.

# on perf1
sudo sysctl -w net.ipv4.tcp_wmem="4096 16384 4194304"

Remember: Slow throughput without a single retransmit usually means: window too small for the RTT. ss -ti shows it with rwnd_limited and sndbuf_limited, larger buffers fix it.

If it doesn’t work

  • Error: Exclusivity flag on, cannot modify. with tc qdisc add: There is already a netem discipline. Change it with tc qdisc change … or remove it first with tc qdisc del dev eth0 root.
  • The SSH session to last1 hangs: You set a very high loss rate or delay. Log in through the hypervisor console and remove the discipline.

Save your progress? Sign up for free

Drill 4: How long does connection setup take?

Where you are: in three terminals – last1 and twice perf1.

What you have afterwards: a small web server on last1 and the duration of every connection setup from perf1 to it.

Step 1 – A directory for the web server.

# on last1
mkdir -p ~/ablage
# on last1
cd ~/ablage

Step 2 – A large file. head -c 300M takes the first 300 MB from the random source, > writes them into the file:

# on last1
head -c 300M /dev/urandom > bericht.bin

Step 3 – The web server.

# on last1
setsid python3 -m http.server 8080 > /dev/null 2>&1 &
  • python3 -m http.server 8080 starts a simple web server on port 8080 that serves the current directory.
  • setsid detaches it from your terminal so that it keeps running when you log out; & starts it in the background; > /dev/null 2>&1 discards its messages.

Step 4 – Is it listening? ss -tln shows listening TCP sockets (-l); the filter sport = :8080 only the one on port 8080:

# on last1
ss -tln sport = :8080

Expected:

State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      5            0.0.0.0:8080      0.0.0.0:*          

Step 5 – Measure. In the first terminal on perf1 you start the measurement; wait for the header line (about 20 seconds):

# on perf1, terminal 1
sudo tcpconnlat-bpfcc

In the second terminal you fetch the start page – five times in a row (arrow up and Enter):

# on perf1, terminal 2
curl -s -o /dev/null http://last1:8080/
  • curl -s -o /dev/null fetches the page silently (-s) and discards it (-o /dev/null).

Expected (terminal 1; stop with Ctrl+C):

PID     COMM         IP SADDR            DADDR            DPORT LAT(ms)
78418   curl         4  172.16.0.126     172.16.0.228     8080  0.67
78421   curl         4  172.16.0.126     172.16.0.228     8080  0.76
78424   curl         4  172.16.0.126     172.16.0.228     8080  0.65
78427   curl         4  172.16.0.126     172.16.0.228     8080  0.83
78430   curl         4  172.16.0.126     172.16.0.228     8080  0.77

One line per curl.

tcpconnlat measures the time from sending the connection request (SYN) to the reply (SYN-ACK) – that is, one RTT plus the time the server needs to reply. Below one millisecond, as expected. On an overloaded server whose queue for new connections is full, these values rise to seconds – Day 12.

Remember: Connection setup costs one RTT. tcpconnlat shows it per connection – values far above the RTT mean: the server is not accepting fast enough.

Save your progress? Sign up for free

Diagnosis task

Since this morning the reports from the file server (last1) take “forever” to load. A report is 300 MB. Yesterday it took one second. According to its administrator the file server is “completely relaxed”, and so is perf1.

Setup. Leave the web server from Drill 4 running and set up a fault on last1 whose command you only read in the solution:

# on last1
sudo tc qdisc add dev eth0 root netem delay 2ms loss 1%

Task (20 minutes): How long does the download take now? Find the cause – first only looking from perf1, then looking from last1. Where is the trace visible, where not?

Solution path

Step 1 – Measure the symptom. Run the download three times:

# on perf1
curl -s -o /dev/null -w "%{time_total} s, %{speed_download} B/s\n" http://last1:8080/bericht.bin
  • -w prints values after the download: %{time_total} the total time, %{speed_download} the speed in bytes per second; \n ends the line.

Expected (three runs):

12.522407 s, 25120793 B/s
14.341391 s, 21934608 B/s
12.867527 s, 24447028 B/s

12.5 to 14.3 seconds, 22 to 25 MB/s.

Step 2 – The view from perf1, the receiver: ping. -i 0.2 sends a packet every 0.2 seconds, -q only shows the summary:

# on perf1
ping -c 50 -i 0.2 -q last1

Expected (excerpt – the last two lines):

50 packets transmitted, 50 received, 0% packet loss, time 9850ms
rtt min/avg/max/mdev = 2.478/2.748/4.084/0.307 ms

Step 3 – The view from perf1: TCP counters. Set the starting point:

# on perf1
nstat -n

Download once:

# on perf1
curl -s -o /dev/null http://last1:8080/bericht.bin

And look at the change:

# on perf1
nstat TcpRetransSegs TcpInSegs

Expected:

#kernel
TcpInSegs                       15663              0.0

Two traces and a gap:

  • ping has an RTT of 2.7 ms instead of 0.6 ms (Day 0). This time none of the 50 packets was lost – at 1 % loss that is chance; in another lab run one was missing. So a single ping run proves neither loss nor its absence.
  • TcpRetransSegs does not appear at all. perf1 only receives – and retransmits are counted by the sender, just as only the sender knows that they sent a letter twice. sar -n ETCP on perf1 also shows 0 for retrans/s. If you only look at the receiver, you find nothing.

Step 4 – The view from last1, the sender. Start a download on perf1 (curl -s -o /dev/null http://last1:8080/bericht.bin) and meanwhile look at last1:

# on last1
sar -n ETCP 1 3
  • ETCP are the TCP errors: among others retrans/s, resent segments per second (third number column).

Expected (excerpt – without the first line with kernel and date):

23:34:29     atmptf/s  estres/s retrans/s isegerr/s   orsts/s
23:34:30         0.00      0.00    211.00      0.00      0.00
23:34:31         0.00      0.00    119.00      0.00      0.00
23:34:32         0.00      0.00     90.00      0.00      0.00
Average:         0.00      0.00    140.00      0.00      0.00

sport = :8080 filters connections with source port 8080 – those of the web server:

# on last1
ss -ti sport = :8080

Expected:

State Recv-Q Send-Q  Local Address:Port     Peer Address:Port Process
ESTAB 0      1342296  172.16.0.228:http-alt 172.16.0.126:56082
     cubic wscale:7,7 rto:203 rtt:2.748/0.145 ato:40 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:40 ssthresh:32 bytes_sent:148715599 bytes_retrans:1475512 bytes_acked:147182167 bytes_received:84 segs_out:102707 segs_in:3027 data_segs_out:102706 data_segs_in:1 send 169Mbps lastrcv:6577 pacing_rate 202Mbps delivery_rate 178Mbps delivered:101648 busy:6570ms rwnd_limited:2ms(0.0%) unacked:40 retrans:0/1019 rcv_space:14480 rcv_ssthresh:64088 notsent:1284376 minrtt:2 snd_wnd:4792320

Without -n, ss shows port names: http-alt is the name for port 8080.

Now it is there: 90 to 211 retransmits per second, and the connection resent 1,019 segments in a little over six seconds (retrans: current/total), almost 1.5 MB (bytes_retrans). Its congestion window is tiny at 40 segments – for comparison: 3,095 in Drill 2. delivery_rate 178 Mbit/s instead of several Gbit/s. Every loss makes cubic shrink the window, and at 1 % loss it never comes back up.

Step 5 – See every single retransmit. The bcc tool tcpretrans-bpfcc cannot be compiled on Ubuntu 24.04 with kernel 6.8 (error: use of undeclared identifier 'BPF_CGROUP_UNIX_CONNECT' – the headers of bcc 0.29 do not match the kernel headers). The bpftrace version works:

# on last1
sudo timeout -s INT 20 tcpretrans.bt

Expected (excerpt – the first lines):

Attaching 3 probes...
Tracing tcp retransmits. Hit Ctrl-C to end.
TIME     PID               LADDR:LPORT           RADDR:RPORT  STATE
14:26:24 16         172.16.0.228:8080     172.16.0.126:39226  ESTABLISHED
14:26:24 1381       172.16.0.228:8080     172.16.0.126:39226  ESTABLISHED
14:26:24 1381       172.16.0.228:8080     172.16.0.126:39226  ESTABLISHED
14:26:24 1381       172.16.0.228:8080     172.16.0.126:39226  ESTABLISHED

Each line is a resent segment, with connection and state. The PID is the process that happened to be running when the kernel resent – often the web server, sometimes a kernel thread (16).

Step 6 – The cause. Where are packets lost? The network card and driver on last1 show no errors. But the queueing discipline:

# on last1
tc -s qdisc show dev eth0
  • -s shows the statistics of the discipline.

Expected:

qdisc netem 8003: root refcnt 2 limit 1000 delay 2ms loss 1%
 Sent 1469853996 bytes 970963 pkt (dropped 687, overlimits 0 requeues 0) 
 backlog 60560b 3p requeues 0

There it is: a netem discipline drops 1 % of all outgoing packets (dropped 687) and delays them by 2 ms. In real life this place would hold a faulty line, an overloaded switch port or a misconfigured network device – and tc -s qdisc would show nothing. Then the search continues with ping, mtr and the error counters of the network devices. The evidence from the host stays the same: retransmits on the sender, a small congestion window, increased RTT and occasional loss with ping.

Step 7 – Fix and verify.

# on last1
sudo tc qdisc del dev eth0 root

Run the download three times again:

# on perf1
curl -s -o /dev/null -w "%{time_total} s, %{speed_download} B/s\n" http://last1:8080/bericht.bin

Expected (three runs):

0.682037 s, 461225417 B/s
0.642708 s, 489449018 B/s
0.617156 s, 509713589 B/s

0.6 to 0.7 seconds instead of 12.5 to 14.3 – about 20 times faster. One percent of lost packets had slowed the download by 95 %. (In another lab run the first download was slower because last1 first had to read the file from its disk again.)

Step 8 – Clean up. Stop the web server. The square brackets in the pattern prevent pkill -f from matching its own command line – a trick you will meet again on Day 13:

# on last1
pkill -f "[h]ttp.server 8080"

-x means: the process name must be exactly this:

# on last1
pkill -x iperf3
# on last1
rm -f ~/ablage/bericht.bin

Remember: Only the sender sees retransmits. If you only look at the receiver during a slow download, you find nothing – look at the side that sends the data.

If it doesn’t work

  • curl: (7) Failed to connect to last1 port 8080: The web server is not running (any more). Start it as in Drill 4.
  • The download is fast despite the fault: The discipline was set up on perf1 instead of last1 – then it only delays perf1’s small acknowledgments. Check with tc qdisc show dev eth0 on both VMs.

Challenge

Measure the throughput with iperf3 -R -c last1 -t 15 at 0.1 %, 1 % and 5 % loss (netem loss …) and plot the values against the loss rate. How does the throughput behave?

Solution path: -R reverses the direction: last1 sends, perf1 receives – so the loss on last1 affects the data. Between the measurements you change the rate with sudo tc qdisc change dev eth0 root netem delay 2ms loss 0.1% (and so on). In the author’s lab, 15 seconds each, with 2 ms delay:

Loss Throughput Retransmits
0 % 8.34 Gbit/s 0
0.1 % 992 Mbit/s 1,214
1 % 230 Mbit/s 2,540
5 % 19.1 Mbit/s 1,257

The throughput does not fall in proportion to the loss rate but much more steeply: even one per mille costs 88 %, and at 5 % practically nothing is left. That fewer retransmits are counted at 5 % than at 1 % is because much less is sent in the same time. That is the square root rule from the background – and the reason why network teams should never play down “only one percent loss”. Remove the discipline afterwards.

Save your progress? Sign up for free

Checkpoint

# on perf1
ssh last1 tc qdisc show dev eth0

Expected: a line that starts with qdisc fq_codel.

sysctl -n only prints the value, without the name:

# on perf1
sysctl -n net.ipv4.tcp_wmem

Expected: 4096 16384 4194304 – the default value.

# on perf1
ssh last1 sysctl -n net.ipv4.tcp_rmem

Expected: 4096 131072 6291456 – the default value.

# on perf1
ssh last1 pgrep -c iperf3

Expected: 0.

# on perf1
ssh last1 pgrep -fc "[h]ttp.server"

Expected: 0.

Test yourself

  1. ip -s link reports 2,915 dropped packets. Is that a problem?
  2. A single data stream only manages 1.3 Gbit/s over a 20 ms RTT, without retransmits. Why, and which field in ss -ti shows it?
  3. How large must the window be for 8 Gbit/s at a 21 ms RTT?
  4. A download is slow. On the receiver, nstat shows not a single retransmit. Is the network clean then?
  5. Why does even 1 % packet loss cost so much throughput?

Answers:

  1. Not necessarily – it is a total since boot. Whether something is being dropped right now is shown by the rate (sar -n EDEV, rxdrop/s).
  2. The window is too small for the RTT (BDP). rwnd_limited and sndbuf_limited show how long the receive window or the send buffer was the limit.
  3. Throughput × RTT: 1 GB/s × 0.021 s ≈ 21 MB.
  4. No. Retransmits are counted by the sender – look at last1 (sar -n ETCP, ss -ti, tcpretrans.bt).
  5. TCP takes every loss as congestion and shrinks the congestion window; with regular loss it never comes back up (in the lab cwnd:40 instead of 3,095).

Interview Note

“An application loads data over a 10 Gbit/s line from another data center and only manages 1 Gbit/s. What could be the reason?” – Bandwidth-delay product, TCP buffers (tcp_rmem/tcp_wmem), parallel streams as a workaround, packet loss and retransmits on the sender side, and ss -ti with rwnd_limited/sndbuf_limited as the tool that tells you exactly who is slowing things down. If you can bring that together, you can prove network problems from the host instead of guessing.

Enterprise Note

Retransmits per second (TcpRetransSegs as a rate) belong in the monitoring of every server that delivers data – not only of the network devices. An increase shows problems on the path long before users report them. For paths between data centers it is worth tuning the TCP buffers to the BDP on purpose and documenting it; for replication and backups over long distances this is often more effective than a more expensive line. And anyone who uses netem for testing should never forget it on production systems – tc -s qdisc belongs in every network checklist.

Summary

  • Counters: ip -s link (totals), sar -n DEV/EDEV (rates), nstat (TCP stack, with -n as a difference). Totals say that, rates whether right now.
  • ss -tin shows per connection the RTT, congestion window, delivery_rate and, with rwnd_limited and sndbuf_limited, who is slowing things down.
  • BDP: window ≥ throughput × RTT. With 20 ms delay one stream fell from about 8 to 1.33 Gbit/s; with 32 MiB buffers it rose to 6.78 Gbit/s.
  • tc netem creates delay and loss on outgoing packets; tc -s qdisc shows them.
  • The sender sees retransmits. 1 % loss made a 300 MB download about 20 times slower.
  • tcpretrans-bpfcc cannot be compiled on kernel 6.8; tcpretrans.bt works. tcpconnlat-bpfcc measures connection setup.

Commands at a glance

Command What it does Parameters
ip -s link show eth0 interface statistics (totals) -s statistics
sar -n EDEV 1 1 network errors per second –
nstat [-n] [-az] [ZÄHLER …] network stack counters -n only starting point, -a totals, -z also zeros
ss -tin [dst ADR:PORT \| sport = :PORT] TCP connections with internals -t TCP, -i details, -n numbers
ss -tln sport = :PORT listening TCP socket -l listening
iperf3 -s -D / iperf3 -c HOST [-P 4] [-R] measure throughput -D daemon, -P streams, -R reversed
tc qdisc add/change/del dev eth0 root netem delay 20ms loss 1% create delay and loss delay, loss
tc -s qdisc show dev eth0 queueing discipline with statistics -s statistics
sysctl -w net.ipv4.tcp_rmem="…" set TCP buffers min, start, max in bytes
ping -c N [-i 0.2] -q HOST RTT and loss -q only summary
tcpconnlat-bpfcc duration of connection setup –
tcpretrans.bt show every retransmit –
curl -s -o /dev/null -w "%{time_total} …" URL measure a download -w output format
python3 -m http.server 8080 simple web server port

Contents

More