
Recall first from Config space and virtqueue creation
- virtio_cread and virtio_cwrite use the device layout and delegate configuration access to transport operations.
- A configuration generation check detects changes during a read that needs a consistent snapshot.
- The transport creates the virtqueue and programs its ring addresses for the device.
How virtio-blk submits and completes a request
4 min readThe last lesson covered config space and virtqueues. This lesson answers the two most important drivers: blk-mq requests for disks and NAPI for the network.
A disk read can finish on the backend while the requesting program is still waiting. Linux needs both the data and the result of the operation, and it needs to associate that result with the original request even when several requests are active. The Linux blk-mq documentation explains how the block layer dispatches and tracks concurrent requests. The virtio-blk driver connects that request lifecycle to virtqueue submission and completion.
the Linux block layer's multiqueue mechanism for dispatching block requests to a driver and tracking them until they complete gives a block device a request queue. It uses software submission contexts and hardware dispatch contexts, where “hardware” names the driver-facing context even for a virtual disk. Virtio-blk maps its dispatch contexts to request virtqueues. When blk-mq dispatches an ordinary read or write, it calls the driver's queue_rq operation, implemented by virtio_queue_rq. At that point the block request already identifies the operation, the disk range and the memory carrying the data.
The request has driver-private state in a virtio_blk_req. For a read or write, the driver offers three kinds of buffer in the descriptor chain. The device reads the request header, which includes the operation type and starting sector. It reads the data buffers for a disk write or fills them for a disk read. In either case it writes a final status byte. The driver describes the payload with a scatter-gather list, so a single request can refer to several memory segments. The device sees the buffers described by the chain, while the driver keeps its private request state in the guest.
virtio_queue_rq starts the request in blk-mq and attempts to add the descriptor chain to the selected virtqueue. If it cannot add the request because the queue has run out of space, it reports a resource condition to blk-mq so dispatch can retry. Successful submission leaves the request outstanding. The driver can batch submissions and check whether a kick is needed at the end of the batch. One notification can therefore cover several new requests.
For a split virtqueue, the device finishes the operation, writes the status, and publishes a used-ring entry. In the interrupt-driven path, the completion callback drains available completions with virtqueue_get_buf. The virtqueue code returns the token the driver supplied when it added the chain, which lets the driver recover the original block request. The driver interprets the status and completes the request through blk-mq. A used entry means the device has finished with the chain. The status tells Linux whether the operation succeeded.
For a successful read, the device has already put the bytes in the request's data buffers by the time the driver reclaims the chain. For a failed read, the driver reports the error through the same completion path. A write completion returns ownership of the submitted buffers and reports the device result. A successful ordinary write may still be in a backend write cache, with durability depending on the device's cache behavior and the block layer's flush handling.
How virtio-net transmits and receives packets
3 min readThe transmit path begins when the Linux networking stack calls the driver's transmit operation with an sk_buff, often called an skb. In virtio-net, the ndo_start_xmit operation is start_xmit. The driver uses the skb's TX queue mapping to select a transmit virtqueue, prepares a virtio-net header, and describes the header and packet data as device-readable buffers. The header can carry checksum and segmentation information when the corresponding offloads are enabled. The skb may describe packet bytes in several fragments, which the driver can submit as a scatter-gather list.
The submit path calls virtqueue_add_outbuf. Here “out” describes the guest-to-device direction. Adding the buffers publishes work for the device. The driver also checks whether to kick the device, and it can defer that check when the stack indicates more packets are coming in the current batch. When a kick is required, the driver notifies the backend so it can process the available buffers. Until the device returns a used entry, the guest keeps the submitted packet memory available. Reclaiming that entry lets the driver release the skb. TX completion means the backend has finished with the guest buffers. Delivery to a remote application requires the rest of the network path to succeed.
On receive, the guest supplies writable buffers in advance. The backend takes an available RX buffer, fills it with a virtio-net header and incoming frame data, and returns it as used. With mergeable receive buffers negotiated, one packet can occupy more than one posted buffer. The virtio-net header tells the driver how many buffers belong to that packet. The driver uses the completed lengths and header information to construct the receive skb and pass it to the guest network stack. It replenishes the receive queue as buffers are consumed. Without enough posted buffers, the backend can run out of space to deliver incoming frames to that queue.
the Linux networking mechanism that schedules a driver's poll callback to process network events under a work budget controls this receive work. A receive virtqueue callback schedules NAPI and suppresses further queue callbacks while polling is scheduled. The RX poll callback in virtio-net is virtnet_poll. It takes used buffers from the receive virtqueue, processes packets up to its budget, and can hand received skbs to the stack through NAPI's generic receive offload path. The budget counts packet work, so a packet split across multiple buffers does not count as a separate packet for every buffer.
When the poll uses its budget, NAPI can schedule more polling. When the work drains and polling completes, the driver re-enables callbacks and checks for completions that arrived during the transition. That check matters because a packet can arrive after the last attempt to fetch a used buffer and before callbacks are re-enabled. An interrupt can initiate a poll which processes many packets. Consequently, the interrupt count and packet count over a test interval can differ substantially.
The two directions also start from different conditions. On TX there is already a packet when start_xmit runs. On RX the memory must already be available when a packet reaches the backend. A transmit performance problem can involve delayed reclamation of submitted buffers. A receive performance problem can involve buffer replenishment or the CPU time available for virtnet_poll and the receive stack.
What queue count and EVENT_IDX change
4 min readA virtio-net queue pair contains one RX virtqueue and one TX virtqueue. With multiqueue, a device can expose several pairs, and Linux can activate a supported number of them. Applications still use one guest network interface. The networking stack can submit traffic on different TX queues, and different RX queues have their own NAPI polling contexts. Parallel processing depends on where traffic is assigned and which CPUs run the guest and backend work. Extra active queues can leave throughput unchanged when traffic remains concentrated on one queue or when another resource is already saturated.
Going deeperReceive steering, XDP and the host worker
Receive-side scaling (RSS) is a receive steering mechanism. When the virtio RSS feature is negotiated and the device is configured for RSS, the backend hashes selected packet fields and uses an indirection table to choose an RX queue. For TCP, the addresses and ports can distinguish flows. Packets in one flow generally keep the same queue while the RSS configuration is stable. Several flows can exercise several queues, although collisions are possible. A device can support multiqueue without exposing the virtio RSS feature, so the active channel count alone gives no evidence that RSS is in use.
Virtio-net also supports native XDP on compatible configurations. The receive processing path can run an attached XDP program on packet data before normal skb delivery. The program can pass or drop a packet, transmit it back through the interface, or request a redirect. XDP transmit may use dedicated TX queues when the configuration has enough queues. Keep the XDP configuration fixed for throughput comparisons because it can change both RX processing and queue use.
For a QEMU device backed by vhost_net, the guest still uses virtio-net with virtnet_poll. On the host, vhost worker processing consumes guest TX descriptors and sends the packet data to the TAP backend. In the reverse direction, host-side processing uses the guest's posted RX buffers to deliver packets from TAP and publishes completions. The guest NAPI context and the host worker run on their respective sides of the same data queues. Queue pairs do not imply a dedicated host CPU for each pair or a fixed correspondence between host CPUs and guest vCPUs.
With split virtqueues, negotiated VIRTIO_RING_F_EVENT_IDX lets each side specify a ring index at which it wants a notification from the other. The guest can use an event index when asking for completion notifications, and the backend can use one when asking for kicks as new buffers become available. With the feature disabled, split rings still support notification suppression through ring flags. NAPI polling and TX batching also still operate. An off/on comparison tests how the event-index mechanism affects this workload. Its throughput effect can be small, and throughput alone cannot establish the number of kicks or interrupts.
For the measurement, use a dedicated guest NIC on a host TAP link, with iperf3 serving on the host and the client inside the guest. The QEMU device that already boots the guest can stay in place. Add the lab NIC using these network arguments, with tap-lab already created as a multiqueue TAP interface and accessible to the QEMU user.
-netdev tap,id=lab,ifname=tap-lab,script=no,downscript=no,queues=4,vhost=on
-device virtio-net-pci,netdev=lab,mq=on,vectors=10,event_idx=on,packed=off
Each line is a pair of arguments to add to the same QEMU invocation. This example provisions four queue pairs and provides ten MSI-X vectors for the eight data virtqueues plus control and configuration. Keep the guest's vCPU count fixed, preferably at four or more for this comparison. Give the host TAP and guest lab NIC addresses on the same unused subnet. The server and client must use those addresses so the test crosses the intended NIC. vhost=on requires host access to /dev/vhost-net. Keep that setting fixed for the whole comparison.
In the guest, use ethtool -l "$IFACE" to see the maximum and current combined channel counts, where IFACE is the lab interface name. Use sudo ethtool -L "$IFACE" combined 1 or sudo ethtool -L "$IFACE" combined 4 and check the current count after each change. To vary EVENT_IDX, shut down the guest and relaunch QEMU with event_idx=off in place of event_idx=on. The property controls whether the device offers the feature at negotiation. Record the launch setting for each run and check the channel count again after boot.
Start the host server with iperf3 -s -B "$HOST_IP", where HOST_IP is the address on the TAP link. In the guest, run iperf3 -c "$HOST_IP" -P 4 -t 20 -R with the same host address. -R sends the test data from host to guest so the measured traffic exercises guest RX. -P 4 uses four TCP streams and gives the backend multiple flows to distribute when its receive steering supports that. Use -P 1 as a comparison with a single flow. Record receiver throughput and repeat each condition before comparing medians. Keep the backend, vCPU count, offloads, duration and traffic direction fixed across the four queue-count and EVENT_IDX combinations. An observation worth explaining is whether adding queues helps more with several flows than with one, and whether that difference persists across repeated runs.
The MMIO and PCI transport drivers lesson follows these requests and packets down to the code that delivers queue notifications and completion interrupts for each transport.
Key takeaways
- A virtio-blk read gives the device writable data buffers, and a write gives it readable data buffers. Both have a device-written status.
- virtio-net submits outbound packets on TX queues and posts writable buffers in advance on RX queues.
- virtnet_poll drains completed RX buffers under a NAPI budget.
- Multiqueue provides parallel queues, RSS steers receive flows, and EVENT_IDX changes notification decisions.
Reflect
- Explain to a colleague, in two sentences: trace a blk-mq request from queue_rq through used-ring completion.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationMulti-Queue Block IO Queueing Mechanism (blk-mq)
- Kernel documentationWriting Virtio Drivers
- Kernel documentationNAPI
- Kernel documentationScaling in the Linux Networking Stack
Official documentation first. Links checked when the lesson was published.