virtio.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _virtio:
  3. ===============
  4. Virtio on Linux
  5. ===============
  6. Introduction
  7. ============
  8. Virtio is an open standard that defines a protocol for communication
  9. between drivers and devices of different types, see Chapter 5 ("Device
  10. Types") of the virtio spec (`[1]`_). Originally developed as a standard
  11. for paravirtualized devices implemented by a hypervisor, it can be used
  12. to interface any compliant device (real or emulated) with a driver.
  13. For illustrative purposes, this document will focus on the common case
  14. of a Linux kernel running in a virtual machine and using paravirtualized
  15. devices provided by the hypervisor, which exposes them as virtio devices
  16. via standard mechanisms such as PCI.
  17. Device - Driver communication: virtqueues
  18. =========================================
  19. Although the virtio devices are really an abstraction layer in the
  20. hypervisor, they're exposed to the guest as if they are physical devices
  21. using a specific transport method -- PCI, MMIO or CCW -- that is
  22. orthogonal to the device itself. The virtio spec defines these transport
  23. methods in detail, including device discovery, capabilities and
  24. interrupt handling.
  25. The communication between the driver in the guest OS and the device in
  26. the hypervisor is done through shared memory (that's what makes virtio
  27. devices so efficient) using specialized data structures called
  28. virtqueues, which are actually ring buffers [#f1]_ of buffer descriptors
  29. similar to the ones used in a network device:
  30. .. kernel-doc:: include/uapi/linux/virtio_ring.h
  31. :identifiers: struct vring_desc
  32. All the buffers the descriptors point to are allocated by the guest and
  33. used by the host either for reading or for writing but not for both.
  34. Refer to Chapter 2.5 ("Virtqueues") of the virtio spec (`[1]`_) for the
  35. reference definitions of virtqueues and "Virtqueues and virtio ring: How
  36. the data travels" blog post (`[2]`_) for an illustrated overview of how
  37. the host device and the guest driver communicate.
  38. The :c:type:`vring_virtqueue` struct models a virtqueue, including the
  39. ring buffers and management data. Embedded in this struct is the
  40. :c:type:`virtqueue` struct, which is the data structure that's
  41. ultimately used by virtio drivers:
  42. .. kernel-doc:: include/linux/virtio.h
  43. :identifiers: struct virtqueue
  44. The callback function pointed by this struct is triggered when the
  45. device has consumed the buffers provided by the driver. More
  46. specifically, the trigger will be an interrupt issued by the hypervisor
  47. (see vring_interrupt()). Interrupt request handlers are registered for
  48. a virtqueue during the virtqueue setup process (transport-specific).
  49. .. kernel-doc:: drivers/virtio/virtio_ring.c
  50. :identifiers: vring_interrupt
  51. Device discovery and probing
  52. ============================
  53. In the kernel, the virtio core contains the virtio bus driver and
  54. transport-specific drivers like `virtio-pci` and `virtio-mmio`. Then
  55. there are individual virtio drivers for specific device types that are
  56. registered to the virtio bus driver.
  57. How a virtio device is found and configured by the kernel depends on how
  58. the hypervisor defines it. Taking the `QEMU virtio-console
  59. <https://gitlab.com/qemu-project/qemu/-/blob/master/hw/char/virtio-console.c>`__
  60. device as an example. When using PCI as a transport method, the device
  61. will present itself on the PCI bus with vendor 0x1af4 (Red Hat, Inc.)
  62. and device id 0x1003 (virtio console), as defined in the spec, so the
  63. kernel will detect it as it would do with any other PCI device.
  64. During the PCI enumeration process, if a device is found to match the
  65. virtio-pci driver (according to the virtio-pci device table, any PCI
  66. device with vendor id = 0x1af4)::
  67. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  68. static const struct pci_device_id virtio_pci_id_table[] = {
  69. { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
  70. { 0 }
  71. };
  72. then the virtio-pci driver is probed and, if the probing goes well, the
  73. device is registered to the virtio bus::
  74. static int virtio_pci_probe(struct pci_dev *pci_dev,
  75. const struct pci_device_id *id)
  76. {
  77. ...
  78. if (force_legacy) {
  79. rc = virtio_pci_legacy_probe(vp_dev);
  80. /* Also try modern mode if we can't map BAR0 (no IO space). */
  81. if (rc == -ENODEV || rc == -ENOMEM)
  82. rc = virtio_pci_modern_probe(vp_dev);
  83. if (rc)
  84. goto err_probe;
  85. } else {
  86. rc = virtio_pci_modern_probe(vp_dev);
  87. if (rc == -ENODEV)
  88. rc = virtio_pci_legacy_probe(vp_dev);
  89. if (rc)
  90. goto err_probe;
  91. }
  92. ...
  93. rc = register_virtio_device(&vp_dev->vdev);
  94. When the device is registered to the virtio bus the kernel will look
  95. for a driver in the bus that can handle the device and call that
  96. driver's ``probe`` method.
  97. At this point, the virtqueues will be allocated and configured by
  98. calling the appropriate ``virtio_find`` helper function, such as
  99. virtio_find_single_vq() or virtio_find_vqs(), which will end up calling
  100. a transport-specific ``find_vqs`` method.
  101. References
  102. ==========
  103. _`[1]` Virtio Spec v1.2:
  104. https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html
  105. .. Check for later versions of the spec as well.
  106. _`[2]` Virtqueues and virtio ring: How the data travels
  107. https://www.redhat.com/en/blog/virtqueues-and-virtio-ring-how-data-travels
  108. .. rubric:: Footnotes
  109. .. [#f1] that's why they may be also referred to as virtrings.