Technical Blog

Linux networking internals: sk_buff, qdiscs and the packet path

The NIC driver has come up a few times in this series already. It's the part of the kernel that actually talks to the card, and it's where a received packet first turns into an sk_buff.

The NIC driver#

The driver runs in kernel space and has three main jobs. It allocates the RX and TX descriptor rings and the buffers they point to. It initializes the NIC, which mostly means writing the NIC's registers so the card knows where those rings are in host memory, and turning on queues, MAC filters and offloads. And it handles the interrupts for both receive and transmit, usually with one MSI-X vector per queue.

If you want to read a real driver, Intel's ixgbe in drivers/net/ethernet/intel/ixgbe is a good one to start with. ethtool -i eth0 tells you which driver an interface is using.

net_device#

struct net_device is how the kernel represents a network interface. The driver allocates one, fills it in and registers it, and that's when the interface shows up on the system. Almost everything ip link prints (name, MTU, MAC address, stats) comes from it, and so do the offload flags you see in ethtool -k. It also holds the driver's table of operations, which is how the rest of the kernel calls into the driver:

static const struct net_device_ops ixgbe_netdev_ops = {
	.ndo_open		= ixgbe_open,		/* ip link set eth0 up   */
	.ndo_stop		= ixgbe_close,		/* ip link set eth0 down */
	.ndo_start_xmit		= ixgbe_xmit_frame,	/* hand a packet to the NIC */
	/* ... */
};

sk_buff#

The sk_buff struct with head, data, tail and end pointers into a buffer laid out as headroom, data, tailroom and skb_shared_info with page fragments and a frag_list
The sk_buff layout, from the kernel's skbuff docs.

struct sk_buff, or skb, is the one most people have heard of. An skb represents a single packet and travels with it through every layer of the stack.

The skb itself holds metadata and pointers into a separate data buffer (head, data, tail and end), and the packet bytes sit between data and tail. There's some headroom at the front, so when you send, TCP, IP and then Ethernet can each add their header in front of the payload without copying anything. Receive works the other way around, with each layer stripping its header and passing the skb up. The skb_shared_info at the end of the buffer lists extra page fragments, which scatter-gather and GRO depend on.

Most layers just read or change the skb they're given. A few need their own copy. tcpdump gets a clone that shares the data, and TCP sends a clone down the stack and keeps the original in case it has to retransmit.

For received packets, the skb is created by the driver. By the time its NAPI poll runs (see part 1), the NIC has already DMA'd the packet into a buffer. The driver wraps an skb around that buffer (or copies small packets into a new one), sets the protocol from the Ethernet header and hands the skb to the stack. All of this happens in the softirq, after the hard interrupt handler has already returned.

Queueing disciplines#

Transmit pipeline: App, then QDISC, then the driver queue, IRQ, DMA and transmit
The qdisc sits between the stack and the driver queue.

Qdiscs are part of Linux traffic control (tc), and you can think of them as policies for sending packets. Once the stack knows which interface a packet is going out on, the packet is queued on that interface's qdisc, and the qdisc decides what the driver gets next.

The simplest qdisc is a plain FIFO. The old default, pfifo_fast, is basically three FIFOs. Most distros use fq_codel by default now, and on a multi-queue NIC you'll usually see mq at the root with one child per TX queue. netem is really useful for testing because it can add delay and packet loss.

tc qdisc show dev eth0
tc -s qdisc show dev eth0          # with counters: sent, dropped, backlog
sysctl net.core.default_qdisc

# lab only: add 50 ms of delay, then take it off again
tc qdisc add dev eth0 root netem delay 50ms
tc qdisc del dev eth0 root

Qdiscs are mostly about transmit. On the receive side there's only an ingress hook, for filtering and policing.

The full path#

Side-by-side transmit and receive paths. Transmit: application, socket layer, network stack, qdisc, driver queue, TX ring, signal the NIC, DMA, NIC. Receive: NIC, RX ring, DMA, IRQ/NAPI, RSS/RPS, network stack, socket layer, application
Transmit on the left going down, receive on the right going up.

Sending starts when the application calls send() on a socket. The data goes down through TCP and IP (netfilter's output hooks are on the way), an skb gets created with the payload in its buffer, and the packet is queued on the interface's qdisc. When the driver has room, the qdisc dequeues the packet and the driver fills in TX descriptors that point at the data. Then it writes the NIC's tail register to let the card know there's something to send. That's a simple register write, not an interrupt. The NIC DMAs the packet out of host memory and onto the wire, and once it reports that it's done, the driver frees the skb.

Kernel-space structures between the NIC and the stack: packet buffers, TX and RX rings, IRQ handler, poll list and softirq
The receive side goes through every box here.

Receiving goes the other way. The NIC takes a descriptor from the RX ring, DMAs the packet into its buffer and raises an interrupt. The driver masks the interrupt and puts the queue on the poll list, and the NET_RX softirq runs the driver's poll function, which builds skbs and passes them through GRO. RPS or RFS from part 2 might move the packet to another CPU here. Then it goes up through IP and TCP or UDP to the socket's receive queue, and the waiting process wakes up and copies the data out with recv().

All of that happens for every single packet. A 1 GB download with a normal 1500-byte MTU is around 700,000 packets, and that's before any firewall rules on the host. This is why offloads like TSO and GRO matter so much. They let the stack deal with a few big skbs instead of lots of small ones (ethtool -k eth0 shows which offloads are on).

In the last part we'll look at small packets and VMs, where this per-packet cost really hurts, and at the ways to get the host kernel out of the path.