Networking
Network tools
How to check the claims in the networking pages with ping, mtr, ss and tcpdump, including how to read a handshake in a capture and what each timestamp means.
Networking advice is easy to repeat and easy to get wrong. A few command-line tools let you check the claims yourself. Use ping for the round-trip floor, mtr for the path, ss for live connection state, and tcpdump for the packets.
Concept
Before you start: ICMP is the protocol behind ping and traceroute. It is separate from TCP and UDP, and many networks treat it differently from application traffic.
ping: the round-trip floor
ping sends ICMP echo requests and reports RTT. It is the fastest way to learn the RTT floor to a host and whether packets are being lost.
$ ping -c 4 example.com
64 bytes from 93.184.215.14: icmp_seq=1 ttl=56 time=78.2 ms
...
4 packets transmitted, 4 received, 0% packet loss
rtt min/avg/max/mdev = 77.9/78.3/78.9/0.4 msMany cloud load balancers drop ICMP, so no reply does not always mean the host is down.
traceroute and mtr: the path
traceroute sends packets with TTL 1, 2, 3 and so on. Each router that decrements the TTL to zero sends back an ICMP "time exceeded" message, which reveals it. mtr does the same thing continuously and shows loss and latency for each hop.
When you read mtr, look for the hop where latency jumps and stays high. One hop showing loss while later hops do not is usually a router that deprioritizes ICMP. That is not a real problem.
ss: live socket state
ss (the modern replacement for netstat) lists sockets. It is the quickest way to see what is open right now.
$ ss -tan state time-wait | wc -l # count TIME_WAIT sockets
$ ss -tni dst 10.0.4.12 # kernel's live view of each connection
ESTAB 0 0 10.0.1.5:51234 10.0.4.12:5432
cubic wscale:7,7 rto:204 rtt:1.2/0.3 mss:1448 cwnd:10 bytes_acked:5821 retrans:0/0The -ti view includes RTT, the congestion window (cwnd) and retransmission counts. A rising retrans count points to packet loss on that path.
tcpdump: the packets
tcpdump captures packets, and Wireshark shows them visually. Here is a capture of a handshake and a plain-HTTP request:
$ sudo tcpdump -i any -nn 'tcp port 80 and host 93.184.215.14'
10:01:00.000 IP 10.0.1.5.51234 > 93.184.215.14.80: Flags [S], seq 1000, win 64240, options [mss 1460,sackOK,TS,nop,wscale 7]
10:01:00.078 IP 93.184.215.14.80 > 10.0.1.5.51234: Flags [S.], seq 5000, ack 1001, win 65535, options [mss 1460,...]
10:01:00.078 IP 10.0.1.5.51234 > 93.184.215.14.80: Flags [.], ack 5001
10:01:00.079 IP 10.0.1.5.51234 > 93.184.215.14.80: Flags [P.], seq 1001:1078, ack 5001 (GET / HTTP/1.1)
10:01:00.157 IP 93.184.215.14.80 > 10.0.1.5.51234: Flags [.], seq 5001:6449, ack 1078Read it line by line. [S] is the SYN. [S.] is the SYN-ACK, 78 ms later, which is the RTT. [.] is the ACK. [P.] pushes the 77-byte request. The first byte of the response arrives one more RTT later. The handshake's cost is visible in the timestamps.
Save captures with -w capture.pcap and open them in Wireshark. Its "Statistics → TCP Stream Graphs → Time Sequence" view shows slow start as a staircase.
Walking through it: a checking routine
When someone says "the network is slow", run these in order:
pingthe host to get the RTT floor. If the floor is high, round trips are the cost.mtrto the host, to see whether a hop in the middle is losing packets.ss -tnion the client to seecwnd, RTT andretransfor live connections.tcpdumpa single connection if the numbers still do not explain the delay.
Common mistakes
- Reading ICMP loss as real loss. Routers often deprioritize ICMP. Check application-level loss too.
- Running
tcpdumpon a busy host without a filter. Filter by host and port, or the capture floods the disk. - Assuming one
pingtells you the whole story. Run it several times, and look at the variance too.
Check yourself
- In the capture above, how do you know the RTT is about 78 ms? The gap between the SYN at
10:01:00.000and the SYN-ACK at10:01:00.078. - What does a rising
retranscount inss -tisuggest? Packet loss on that path, which triggers retransmissions.
Key takeaways
pinggives the RTT floor.mtrshows where on the path the time or loss sits.ss -tnishows livecwnd, RTT and retransmits for each connection.tcpdumpcaptures packets, and the timestamps between SYN and SYN-ACK measure the RTT directly.- Check your claims with these tools before changing configuration.
Further learning
Revisit networking basics for the layers and vocabulary these tools display, or go to TCP handshake and reliability to see each handshake step in context.
Related
Networking: IP, TCP and UDP
Packets, addresses, ports, and the handshakes and congestion control that decide how fast your bytes arrive. Explains why a new connection is slow and how to check it on the wire.
TCP handshake and reliability
How TCP sets up a connection with a three-way handshake, numbers every byte, recovers lost packets, and closes cleanly, along with the costs each step adds.
Congestion control and BDP
Why a new TCP connection starts slow, how slow start works with a 100 KB example, how CUBIC and BBR differ, and how the bandwidth-delay product tells you how much data must be in flight.