TT Lab
Blog Get started
Learn Learning paths Courses

Linux Network Diagnosis

Diagnosing Sockets and Ports

Continue in TT Lab

Goal

You read the binding scope and waiting queue of listening sockets through two paths, ss and /proc/net/tcp, and use that reading to tell apart the different causes of "Connection refused" and "the connection hangs".

Why it matters

Most reports of "curl works inside the server but not from outside" are a binding problem, not a firewall problem. A socket bound to 127.0.0.1:8080 accepts only connections from the same host. And on a listening socket, Send-Q is the backlog maximum and Recv-Q is the current length of the queue waiting for accept; it is often misread because it means something different from its meaning on an established connection.

The two incidents at the end put this table reading to real use. The "Connection refused" a client receives does not differ by a single character whether nobody is listening, it listens only on another address, or it listens on another port. "It hangs" and "Address already in use" are also easy to blame on TIME-WAIT, but the real answer lies in the owner and queue length of a single listening socket line. Even if the management tool says running, the kernel's socket table is the truth.

Steps

You work in the /root/net directory.

  1. Start an HTTP server on port 8080 bound only to the loopback address, in the background, and write its PID to /root/net/http_pid.txt.
  2. Start an HTTP server on port 8081 bound to all addresses and write its PID to /root/net/all_pid.txt. 8080 must remain loopback-only.
  3. Write the binding scopes of the two ports to /root/net/binding.txt as two lines, 8080=<loopback|all> and 8081=<loopback|all>.
  4. Start a process that opens one connection to 8081 and keeps it without closing, and write its PID to /root/net/client_pid.txt.
  5. Without using ss, find the line for the 8080 listening socket in /proc/net/tcp, and write that line's local_address value (address:port, in hexadecimal exactly as written in the file) to /root/net/hexport.txt.
  6. Write the maximum backlog of the 8080 listening socket to /root/net/backlog.txt, as a number only.
  7. Incident incident ln-sock-refused: three APIs all give Connection refused when connected to via the Pod address. Fix all three and report the cause for each service.
  8. Incident incident ln-sock-stuck: connections to report hang, and notify 2.0 will not start. Bring report back without a restart, and report the causes of both.

Notes

Start a server bound only to loopback

Start one HTTP server on port 8080 bound only to the loopback address (127.0.0.1), in the background, and write its PID to /root/net/http_pid.txt. You may use the HTTP server from Python's standard library.

Which address to listen on is chosen when you start the server; the help (-h) of python3's http.server module has that option. After starting it, check that the Local Address column in the list of listening sockets is 127.0.0.1:8080.

Start one bound to all addresses

Start another HTTP server on port 8081 bound to all addresses (0.0.0.0), in the background, and write its PID to /root/net/all_pid.txt. 8080 must remain loopback-only.

If you bind to 0.0.0.0, it accepts connections arriving on any interface. Be careful not to restart 8080.

Record the binding scopes

Write the binding scopes of the two ports to /root/net/binding.txt as the following two lines. The value is loopback or all.

Look at the Local Address column of the listening sockets. 127.0.0.1 is loopback, and 0.0.0.0 (or *) is all addresses.

Hold a connection

Start a process that opens one connection to 8081 and keeps it without closing, and write its PID to /root/net/client_pid.txt.

When the process that opened the connection ends, the connection closes too. Leave in the background a small client that opens the connection and then sleeps for a long time (python3's socket module is enough). Check with the list of sockets in the ESTAB state.

Read /proc/net/tcp directly

Without using ss, find the line for the 8080 listening socket in /proc/net/tcp, and write that line's local_address value (address:port, in hexadecimal exactly as written in the file) to /root/net/hexport.txt.

This file writes not only the port but also the address in hexadecimal. The port is in the order people read it, but an IPv4 address is written with its byte order reversed (little-endian). First convert 8080 to hexadecimal, then pick the line whose state field is LISTEN (0A).

Read the backlog maximum

Write the maximum backlog of the 8080 listening socket to /root/net/backlog.txt, as a number only.

On a listening socket, Recv-Q and Send-Q mean something different from an established connection. Recall from the reading which of the two is "the number waiting now" and which is "the upper limit".

Incident: three APIs give the same Connection refused

After running incident ln-sock-refused, fix pay-api (9101), auth-api (9102), and cart-api (9103) so that they all answer when connected to via the Pod address (eth0's IPv4). Keep your fixes in /etc/ln-svc/<name>.conf so they persist across restarts, and start the services with ln-svc so the management tool knows the actual processes. In /root/incident/ln-sock-refused/report.txt, write three lines, one <name>: <code> per service. Candidate codes: backlog-full (the waiting queue is full), firewall (packets are blocked along the way), loopback-only (bound only to 127.0.0.1), not-listening (nobody is listening), wrong-port (listening on another port).

Direction: the RST the client receives is the same for all three. The difference is only in the server-side socket table, so do not trust the management tool's running; ask the kernel directly. Tools: ss (with the option that shows listening sockets together with their owner processes), ip, ps, curl. Concretely: for each port, hold up against each other the address, port, and owner of the listening socket, who the number the management tool trusts actually is, and the port table in the operations documentation and each configuration. After restarting, also check that no old process remains.

Incident: a hanging connection and a new version that will not start

After running incident ln-sock-stuck, fix two things. Make report (9401) answer requests again as the very same process, without a restart, and make notify (9402) 2.0 answer on 9402 (started with ln-svc). In /root/incident/ln-sock-stuck/report.txt, write two lines, report: <code> and notify: <code>. Candidate codes: backlog-full (the server is not taking connections, so the waiting queue filled up), firewall (packets are dropped along the way), orphan-holder (a previous process still holds the port), time-wait (TIME-WAIT of closed connections blocks the port), wrong-port (listening on another port).

Direction: "it hangs" and "already in use" are answered by different fields of a single listening socket line. Also remember that TIME-WAIT is a different line from LISTEN. Tools: ss, ps, /proc, curl -w. Concretely: look at the Recv-Q and Send-Q of the listening socket (their meaning on a listening socket), the rx_queue of that port's LISTEN line in /proc/net/tcp, the State and PPid in /proc//status of the PID holding that port, and which version the path in that process's command line belongs to. The last line of the logs (/var/log/ln-svc/) is evidence too.