liveBook · Manning

Network stack

As explained in Latency

A network stack is the software and hardware subsystem that processes network packets across protocol layers between a physical network interface and applications.

A network stack mediates data flow between applications and a physical network. Its main parts are the network interface card (NIC) device driver, protocol implementations, and application-visible socket API. Because packet processing happens within a node, the stack can be a significant source of intranode latency. section 3.3.1 section 3.3

A message crosses several protocol layers. Hardware commonly handles Ethernet, the OS kernel handles TCP/IP, and the application handles a higher-level protocol such as HTTP. Moving between those layers is work; on a multicore system, colocating protocol handlers can reduce latency in some cases. section 3.3.1

Trace a message in both directions before treating a socket call as the whole network operation:

  • On receipt, packets enter a NIC receive queue. The kernel driver manages the NIC and notifies the OS of available data; Linux polls for packets rather than raising an interrupt for every arrival. section 3.3.1
  • On sending, an application creates a socket, establishes a TCP connection with connect() when needed, and passes its message to the OS with a call such as sendmsg(). The OS may queue it and transmit it asynchronously; Nagle’s algorithm may also defer sending when enabled. section 7.5.3
  • On receiving, recvmsg() or a similar call checks kernel data structures for arrived messages. OS background processing handles packets that can arrive at any time. section 7.5.3

Sending an HTTP request

The application produces the HTTP request in userspace and hands it to the OS through a socket call. The OS performs lower-level TCP processing. A return from sendmsg() therefore does not mean that all network-stack transmission work is finished; some of that work may still interfere with the application thread. section 7.5.3

Figure 3.3 Packets traversing the network stack
Figure 3.4: Packets traversing the network stack.

Follow the packet path from the NIC through the stack toward the application to locate where receive-side processing occurs. section 3.3.1

How Latency covers it

How deep it goes
Traces NIC queues, driver notification, protocol responsibilities, and socket calls, while leaving detailed hardware notification mechanics aside.
Examples
Uses an HTTP request, a NIC-to-application packet-path figure, and connect(), sendmsg(), and recvmsg() calls.
What it assumes
Defines NICs and sockets while using HTTP and TCP/IP as concrete protocol examples.
What you can do after
Diagnose/debug with it by tracing where packet processing and deferred transmission can add work to a low-latency application.
How it connects
Links packet processing to intranode latency, multicore colocation, protocol layers, and asynchronous transmission.

Also explained in

Compare how 2 books explain network stack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.