CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 4 of 6 · 3 min read

Virtio in a real guest

Think of a house whose utility meters sit in a row by the door: water, gas, electricity. Each has a plate that says what it is and who reads it. The analogy stops in one place: a meter shows one number, and a virtio device has queues, a configuration area and feature bits.

In plain words: Linux lists virtio devices under /sys/bus/virtio/devices. Each has a number for its type, which the virtio standard assigns (1 network, 2 block, 4 random numbers, 5 memory balloon, 19 vsock), and a driver bound to it. The program below lists them.

A worked example.

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static const char *kind(unsigned id) {
    switch (id) {
    case 1: return "network";
    case 2: return "block";
    case 4: return "random numbers";
    case 5: return "memory balloon";
    case 19: return "vsock";
    default: return "other";
    }
}

int main(void) {
    const char *dir = "/sys/bus/virtio/devices";
    DIR *d = opendir(dir);
    if (!d) { puts("no virtio devices: not a virtio guest"); return 0; }
    struct dirent *e;
    while ((e = readdir(d))) {
        if (strncmp(e->d_name, "virtio", 6)) continue;
        char path[512], link[256] = "", drv[64] = "-";
        unsigned id = 0;
        snprintf(path, sizeof path, "%s/%s/device", dir, e->d_name);
        FILE *f = fopen(path, "r");
        if (f) { fscanf(f, "%x", &id); fclose(f); }
        snprintf(path, sizeof path, "%s/%s/driver", dir, e->d_name);
        ssize_t n = readlink(path, link, sizeof link - 1);
        if (n > 0) { link[n] = 0; strncpy(drv, strrchr(link, '/') + 1, sizeof drv - 1); }
        printf("%-8s type %-2u %-15s driver %s\n", e->d_name, id, kind(id), drv);
    }
    closedir(d);
    return 0;
}

Output on the test machine:

virtio0  type 5  memory balloon  driver virtio_balloon
virtio1  type 2  block           driver virtio_blk
virtio2  type 2  block           driver virtio_blk
virtio3  type 2  block           driver virtio_blk
virtio4  type 2  block           driver virtio_blk
virtio5  type 2  block           driver virtio_blk
virtio6  type 2  block           driver virtio_blk
virtio7  type 1  network         driver virtio_net
virtio8  type 19 vsock           driver vmw_vsock_virtio_transport
virtio9  type 4  random numbers  driver virtio_rng

The machine has six virtual disks (vda to vdf), one network card, a balloon (page 2), a random-number source and a vsock device, which is a channel between guest and host without a network. Every device is virtio, so none of them imitates a real card. Each queue also has its own interrupt line, which /proc/interrupts shows as names like virtio1-req.0 (the request queue of disk 1), and the lines for the disks were counted on different processors, so the queues spread the work across the two vCPUs.

The key lines. The type number lets one guest driver set serve every hypervisor that follows the standard, which is why the same guest image runs on several clouds.

Virtio

Virtio is a standard for paravirtual devices. It defines device types (network, block, balloon and others), a common transport and the virtqueue mechanism, so that one guest driver works with any hypervisor that implements the standard.

Example: type 2 is a block device, and its driver is virtio_blk.

Check yourself: 1. What does the type number in /sys/bus/virtio/devices/*/device tell? 2. How many virtual disks does the test machine have? 3. What is vsock for?