Skip to content
systemcoding.dev
Kernels

Linux Network Stack Path From NIC to Socket

Follow a received packet through Linux 6.12 from the NIC's DMA ring, the interrupt, NAPI, GRO, IP and TCP to the socket, with the counters for each step.

6 min read
Summarize in AI
Flat illustration of network packets crossing between two hosts, with the Linux, Rust and Wireshark logos.

A packet arrives at a network card and, microseconds later, a program's recv() returns the bytes. Between those two events the packet crosses about a dozen layers of kernel code, and every performance and loss problem on a Linux host sits at one of them. Knowing the order, which function owns each step and which counter measures it turns "packets are dropped somewhere" into a question with an address. This post follows one TCP packet received on Linux 6.12 from the NIC's ring buffer to the socket, names the functions, and shows the commands that read the counters at each stage. The data structure that carries the packet is covered in the sk_buff post, and the driver that starts it all in the minimal network driver.

The flow is documented in the kernel's networking documentation, with NAPI in its own page.

Step 1: the NIC and its ring

During setup the driver allocates a receive ring: an array of descriptors in memory, each pointing at a buffer. It gives the ring's address to the NIC. When a frame arrives from the wire, the NIC writes it by DMA into the next free buffer, then updates the descriptor with the length and status. No CPU is involved yet, and the NIC has no idea about IP or TCP. A frame that arrives when the ring has no free descriptors is dropped by the hardware, and the card counts it.

The size of the ring is a setting you can inspect and change:

Shell
ethtool -g enp0s31f6
Text
Ring parameters for enp0s31f6:
Pre-set maximums:
RX:		4096
RX Mini:	n/a
RX Jumbo:	n/a
TX:		4096
Current hardware settings:
RX:		256
RX Mini:	n/a
RX Jumbo:	n/a
TX:		256

A larger ring absorbs bursts at the cost of memory and latency.

Step 2: the interrupt

After DMA, the NIC raises an interrupt, usually a message-signaled one to a specific CPU, as in the MSI post. The driver's interrupt handler is deliberately tiny. It does not process the packet. It disables further receive interrupts for that queue and schedules NAPI, with napi_schedule(), then returns. You can see the interrupts, one row per queue, spread over CPUs:

Shell
grep enp0s31f6 /proc/interrupts
Text
 134:   1829301         0         0         0  IR-PCI-MSI 524288-edge      enp0s31f6

Disabling the interrupt while processing is the point of NAPI. Under a flood of packets, an interrupt per packet would consume the CPU entirely, a condition called receive livelock. NAPI switches from interrupts to polling when traffic is heavy.

Step 3: softirq and the NAPI poll

napi_schedule() raises the network receive softirq, NET_RX_SOFTIRQ, which runs soon afterward on the same CPU, either on return from the interrupt or in a ksoftirqd thread if the system is busy. The softirq calls the driver's poll function with a budget, typically 64 packets, and the function does the real work:

  1. Read descriptors the NIC has completed.

  2. Build a struct sk_buff for each frame, wrapping the DMA buffer or copying it.

  3. Hand each one up with napi_gro_receive().

  4. Allocate fresh buffers to refill the ring.

  5. If fewer packets than the budget were waiting, call napi_complete_done() and re-enable the interrupt. Otherwise stay in polling mode.

The per-CPU softirq counts show how much work happens here:

Shell
grep NET_RX /proc/softirqs
Text
NET_RX:     482913      39201      40117      38544

Step 4: GRO

napi_gro_receive() is the generic receive offload layer. It tries to merge consecutive packets of the same flow, say eight TCP segments of one connection, into one large sk_buff, so everything above it handles one big packet and not eight small ones. Merging is a software step that costs little and saves a lot of per-packet work in the stack, which is why the same stack that struggles at 100,000 packets a second handles 10 Gbit/s of bulk traffic. Packets that cannot be merged, such as ones with different flags or from different flows, pass through.

Step 5: into the protocol stack

GRO passes packets to netif_receive_skb() and finally to __netif_receive_skb_core(), the point where the packet leaves the device layer. It looks at the EtherType in the Ethernet header, now stored in skb->protocol, and delivers the packet to the registered handler for it. Taps (like tcpdump) also get a copy here, and a bridge or VLAN device may intercept the packet. For IPv4, the handler is ip_rcv().

Step 6: IP

ip_rcv() checks the IP header: the version, the header length, the checksum and the total length. A packet with a bad header is dropped and counted in InHdrErrors. If the packet passes, it goes through the netfilter PREROUTING hook, where iptables or nftables rules can drop it or rewrite its address, and then a routing lookup decides what happens next. For a packet addressed to this host, ip_local_deliver() strips the IP header, reassembles fragments if needed, runs the INPUT hook and looks at the protocol field to choose a transport handler. For a packet addressed elsewhere, forwarding takes over, if it is enabled.

The IP counters are in /proc/net/snmp:

Shell
nstat -az IpInReceives IpInDelivers IpInHdrErrors
Text
#kernel
IpInReceives                    2187456            0.0
IpInHdrErrors                   0                  0.0
IpInDelivers                    2187301            0.0

The gap between IpInReceives and IpInDelivers is packets that were forwarded, dropped or consumed elsewhere.

Step 7: TCP

The IP layer calls tcp_v4_rcv(). It finds the socket for the connection using the four-tuple of source and destination address and port, in a hash table. If no socket matches, the stack answers with a reset. If it does, TCP verifies the checksum, then processes the segment: it checks sequence numbers, handles acknowledgments, updates the congestion window and, for data, queues it in order with tcp_data_queue(). Out-of-order segments are held until the gap is filled.

The TCP counters live in nstat as well. A rise in TcpExtTCPBacklogDrop or TcpExtListenOverflows points at a socket that cannot keep up or an accept queue that is full:

Shell
nstat -az TcpExtListenOverflows TcpExtTCPBacklogDrop
Text
#kernel
TcpExtListenOverflows           0                  0.0
TcpExtTCPBacklogDrop            0                  0.0

Step 8: the socket

In-order data lands in the socket's receive queue. The kernel then wakes whatever is waiting: a thread blocked in recv(), or a thread in epoll_wait() that is watching the descriptor, through sock_def_readable(). When the thread runs, tcp_recvmsg() copies the bytes from the sk_buff to the user buffer, which is the one copy of the payload on the receive path, and frees the buffer. The size the socket may hold is bounded by SO_RCVBUF and the net.ipv4.tcp_rmem limits, and a program that reads too slowly lets the queue fill, the advertised window shrink and the sender slow down.

The path in one table

StageFunction or objectWhere it shows up
NIC ringDMA descriptorsethtool -S, rx_missed_errors
Interruptdriver handler, napi_schedule()/proc/interrupts
Pollnapi_poll, driver poll function/proc/softirqs, /proc/net/softnet_stat
GROnapi_gro_receive()ethtool -S rx_gro style counters
Device to protocol__netif_receive_skb_core()tcpdump sees packets here
IPip_rcv(), ip_local_deliver()nstat Ip*
TCPtcp_v4_rcv(), tcp_data_queue()nstat Tcp*, ss -ti
Socketreceive queue, tcp_recvmsg()ss -m, /proc/net/sockstat

The softnet_stat file deserves a note, since it holds the packet drops from step 3. Its second column counts packets dropped because the per-CPU backlog was full, and its third counts times the poll loop ran out of budget or time:

Shell
cat /proc/net/softnet_stat
Text
0028b3a1 00000000 00000004 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

All values are in hexadecimal. A non-zero second column means the stack dropped packets before the protocol layers, and the fix is more CPUs on the receive path or a bigger budget.

Also inNetworking
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.