Packet Delivery: Socket to Local Link Explained
In this article
Network packet delivery from a socket to the local link is the process by which an application's data is transformed into frames on a physical network cable or wireless channel. It involves the transport, network, and data-link layers working together, with the operating system kernel performing the heavy lifting. This article explains each step, the buffers involved, and common pitfalls that affect performance.
What Happens Inside the Kernel
When an application calls send() (or write() on a socket), the data does not immediately go to the network card. The call copies the user-space buffer into a kernel-space socket buffer. The kernel then appends protocol headers: TCP or UDP header, IP header, and finally the Ethernet (or Wi-Fi) header. This is called the "packet journey" and involves several queues:
- Socket send buffer: holds data waiting for the transport protocol.
- IP layer: fragments or segments the data, adds IP headers, and routes it.
- Link layer (driver): adds the frame header and trailer, and hands it to the NIC.
- NIC transmit queue: the hardware ring buffer that actually sends bits.
The process is asynchronous: send() returns as soon as the data is copied to the socket buffer (for TCP, it returns when the data is acknowledged, but not necessarily transmitted). The kernel's softirq mechanism handles the actual transmission later, so the application does not block on every byte.
From Socket Buffer to Network Card
After the IP layer decides the next-hop interface, it calls the link-layer driver. The driver builds a frame (e.g., Ethernet II) by prepending the destination MAC address (from the ARP cache or neighbor table) and the source MAC. It also appends a frame check sequence (FCS) for error detection.
The frame is placed into a ring buffer (often called a DMA ring) that the network card reads via Direct Memory Access. The card then serializes the bits onto the wire, applying line coding (e.g., 8b/10b for Gigabit Ethernet). For Wi-Fi, additional steps include encryption (WPA2/3) and CSMA/CA channel access.
Key point: the kernel does not copy the data again between IP and link layers if it can avoid it. Modern NICs use checksum offload and TCP segmentation offload (TSO) so the card computes checksums and splits large TCP segments into smaller frames, reducing CPU load.
TCP vs. UDP Behavior
The delivery path differs significantly between the two main transport protocols:
- TCP: provides a byte stream.
send()may buffer data and send it in segments sized by the MSS (maximum segment size, typically 1460 bytes for Ethernet). The kernel may delay ACKs and use Nagle's algorithm to coalesce small writes. - UDP: each
sendto()becomes one datagram. The kernel adds a UDP header and hands it to IP as a single unit. If the datagram is larger than the MTU, IP fragmentation occurs (unless PMTUD is used).
For real-time applications, UDP is often chosen because it avoids retransmission delays, but the application must handle loss and ordering itself.
Common Performance Bottlenecks
Even with a fast NIC, packet delivery can be slow due to kernel overhead. The most common issues are:
- Interrupt storms: every packet generates an interrupt, which can saturate the CPU. Modern drivers use NAPI (New API) to poll the NIC in batches, but misconfiguration can cause high CPU usage.
- Socket buffer limits: if the send buffer is full,
send()blocks or returns EAGAIN for non-blocking sockets. IncreasingSO_SNDBUFcan help but does not fix a slow receiver. - TCP small-packet problem: sending many small writes (e.g., 1 byte each) causes header overhead and Nagle delays. Use
TCP_NODELAYor batch writes. - LRO/GRO: Large Receive Offload and Generic Receive Offload combine multiple packets into one larger buffer, reducing CPU but increasing latency. For low-latency apps, disable these.
How to Choose the Right Socket Options
Application developers can tune delivery with socket options. The most relevant are:
SO_SNDBUFandSO_RCVBUF: set buffer sizes. Larger buffers reduce drops but increase latency.TCP_NODELAY: disables Nagle's algorithm, sending small segments immediately. Useful for interactive protocols.SO_PRIORITY: sets the socket priority, which the kernel maps to the 802.1p priority in the VLAN tag (if configured).SO_BUSY_POLL: for low-latency, the application can poll the NIC instead of sleeping on interrupts.
For high-throughput servers, consider using sendmmsg() to send multiple packets in one syscall, or io_uring for async I/O. These reduce syscall overhead.
Real-World Example: Sending a 1 MB File
Suppose an application sends 1 MB of data over TCP on Ethernet. The kernel will:
- Copy the data into the socket buffer (maybe 16 KB chunks).
- Segment into ~1460-byte MSS chunks, adding TCP and IP headers (20+20 bytes).
- Pass each segment to the link layer, which adds a 14-byte Ethernet header and 4-byte FCS.
- The NIC transmits ~715 frames (1 MB / 1460).
With TSO enabled, the kernel passes a large buffer (up to 64 KB) to the NIC, which splits it into frames. This reduces CPU usage dramatically. The receiver reassembles the segments in the kernel before delivering to the application's receive buffer.
Frequently Asked Questions
Why does my packet get dropped before reaching the wire?
Drops can happen at several points: the socket buffer is full (application not reading fast enough), the IP layer discards due to no route, or the NIC ring buffer is full (often due to interrupt throttling). Check netstat -s for "send buffer full" or "drop" counters.
What is the difference between a packet and a frame?
A packet is the PDU (protocol data unit) at the network layer (IP). A frame is the PDU at the link layer (Ethernet). The frame encapsulates the packet by adding the header and trailer. The terms are often used interchangeably, but technically a frame is what actually goes on the wire.
Does the kernel copy data twice?
Traditionally, yes: once from user space to kernel, and once from kernel to the NIC. With zero-copy networking (e.g., sendfile() or io_uring with registered buffers), the first copy is avoided. The NIC still reads from kernel memory via DMA, so there is no second CPU copy.
Why does Wi-Fi have higher latency than Ethernet?
Wi-Fi adds channel access (CSMA/CA), retransmissions, and encryption. The link layer must wait for a clear channel, and each frame requires an ACK. This adds several milliseconds compared to Ethernet's sub-millisecond wire time.
How does the kernel decide the destination MAC address?
It uses the ARP cache (IPv4) or neighbor discovery (IPv6). If the entry is missing, it sends an ARP request broadcast. The reply updates the cache, and the frame is then sent. This is why the first packet to a new host has higher latency.