|
From: | John G Johnson |
Subject: | Re: [PATCH] introduce VFIO-over-socket protocol specificaion |
Date: | Fri, 7 Aug 2020 09:52:21 -0700 |
Updated doc to address comments. Many changes are typos, but some are more more substantive. JJ Version title is a hyperlink the versioning section Rewrote concurrency section to be less concurrent Removed disconnection recovery section - disconnect now causes client device reset Message type is now enumerated in flags to distinguish it from message attributes argsz fields use VFIO definitions instead of being reserved sys/vfio.h -> linux/vfio.h
vfio-user.diff
Description: Binary data
vfio-user.rst
Description: Binary data
Table of Contents
vfio-user is a protocol that allows a device to be emulated in a separate process outside of a Virtual Machine Monitor (VMM). vfio-user devices consist of a generic VFIO device type, living inside the VMM, which we call the client, and the core device implementation, living outside the VMM, which we call the server.
The Linux VFIO ioctl interface been chosen as the base for this protocol for the following reasons:
Note
In a proof of concept implementation it has been demonstrated that using VFIO over a UNIX domain socket is a viable option. vfio-user is designed with QEMU in mind, however it could be used by other client applications. The vfio-user protocol does not require that QEMU's VFIO client implementation is used in QEMU.
None of the VFIO kernel modules are required for supporting the protocol, neither in the client nor the server, only the source header files are used.
The main idea is to allow a virtual device to function in a separate process in the same host over a UNIX domain socket. A UNIX domain socket (AF_UNIX) is chosen because file descriptors can be trivially sent over it, which in turn allows:
Other socket types could be used which allow the server to run in a separate guest in the same host (AF_VSOCK) or remotely (AF_INET). Theoretically the underlying transport does not necessarily have to be a socket, however we do not examine such alternatives. In this protocol version we focus on using a UNIX domain socket and introduce basic support for the other two types of sockets without considering performance implications.
While passing of file descriptors is desirable for performance reasons, it is not necessary neither for the client nor for the server to support it in order to implement the protocol. There is always an in-band, message-passing fall back mechanism.
VFIO is a framework that allows a physical device to be securely passed through to a user space process; the device-specific kernel driver does not drive the device at all. Typically, the user space process is a VMM and the device is passed through to it in order to achieve high performance. VFIO provides an API and the required functionality in the kernel. QEMU has adopted VFIO to allow a guest to directly access physical devices, instead of emulating them in software.
vfio-user reuses the core VFIO concepts defined in its API, but implements them as messages to be sent over a socket. It does not change the kernel-based VFIO in any way, in fact none of the VFIO kernel modules need to be loaded to use vfio-user. It is also possible for the client to concurrently use the current kernel-based VFIO for one device, and vfio-user for another device.
A device under VFIO presents a standard interface to the user process. Many of the VFIO operations in the existing interface use the ioctl() system call, and references to the existing interface are called the ioctl() implementation in this document.
The following sections describe the set of messages that implement the VFIO interface over a socket. In many cases, the messages are direct translations of data structures used in the ioctl() implementation. Messages derived from ioctl()s will have a name derived from the ioctl() command name. E.g., the VFIO_GET_INFO ioctl() command becomes a VFIO_USER_GET_INFO message. The purpose of this reuse is to share as much code as feasible with the ioctl() implementation.
After the client connects to the server, the initial server message is VFIO_USER_VERSION to propose a protocol version and set of capabilities to apply to the session. The client replies with a compatible version and set of capabilities it supports, or closes the connection if it cannot support the advertised version.
The client uses VFIO_USER_DMA_MAP and VFIO_USER_DMA_UNMAP messages to inform the server of the valid DMA ranges that the server can access on behalf of a device. DMA memory may be accessed by the server via VFIO_USER_DMA_READ and VFIO_USER_DMA_WRITE messages over the socket.
An optimization for server access to client memory is for the client to provide file descriptors the server can mmap() to directly access client memory. Note that mmap() privileges cannot be revoked by the client, therefore file descriptors should only be exported in environments where the client trusts the server not to corrupt guest memory.
The client uses a VFIO_USER_DEVICE_GET_INFO message to query the server for information about the device. This information includes:
The client uses VFIO_USER_DEVICE_GET_REGION_INFO messages to query the server for information about the device's memory regions. This information describes:
When a region can be mapped by the client, the server provides a file descriptor which the client can mmap(). The server is responsible for polling for client updates to memory mapped regions.
Some regions have additional capabilities that cannot be described adequately by the region info data structure. These capabilities are returned in the region info reply in a list similar to PCI capabilities in a PCI device's configuration space.
A region can be memory-mappable in whole or in part. When only a subset of a region can be mapped by the client, a VFIO_REGION_INFO_CAP_SPARSE_MMAP capability is included in the region info reply. This capability describes which portions can be mapped by the client.
Note
For example, in a virtual NVMe controller, sparse regions can be used so that accesses to the NVMe registers (found in the beginning of BAR0) are trapped (an infrequent event), while allowing direct access to the doorbells (an extremely frequent event as every I/O submission requires a write to BAR0), found right after the NVMe registers in BAR0.
The client uses VFIO_USER_DEVICE_GET_IRQ_INFO messages to query the server for the device's interrupt types. The interrupt types are specific to the bus the device is attached to, and the client is expected to know the capabilities of each interrupt type. The server can signal an interrupt either with VFIO_USER_VM_INTERRUPT messages over the socket, or can directly inject interrupts into the guest via an event file descriptor. The client configures how the server signals an interrupt with VFIO_USER_SET_IRQS messages.
When the guest executes load or store operations to device memory, the client forwards these operations to the server with VFIO_USER_REGION_READ or VFIO_USER_REGION_WRITE messages. The server will reply with data from the device on read operations or an acknowledgement on write operations.
When a device performs DMA accesses to guest memory, the server will forward them to the client with VFIO_USER_DMA_READ and VFIO_USER_DMA_WRITE messages. These messages can only be used to access guest memory the client has configured into the server.
To distinguish from the base VFIO symbols, all vfio-user symbols are prefixed with vfio_user or VFIO_USER. In revision 0.1, all data is in the little-endian format, although this may be relaxed in future revision in cases where the client and server are both big-endian. The messages are formatted for seamless reuse of the native VFIO structs.
A server can serve:
The current protocol specification requires a dedicated socket per client/server connection. It is a server-side implementation detail whether a single server handles multiple virtual devices from the same or multiple clients. The location of the socket is implementation-specific. Multiplexing clients, devices, and servers over the same socket is not supported in this version of the protocol.
For AF_UNIX, we rely on OS mandatory access controls on the socket files, therefore it is up to the management layer to set up the socket as required. Socket types than span guests or hosts will require a proper authentication mechanism. Defining that mechanism is deferred to a future version of the protocol.
A client may pipeline multiple commands without waiting for previous command replies. The server will process commands in the order they are received. A consequence of this is if a client issues a command with the No_reply bit, then subseqently issues a command without No_reply, the older command will have been processed before the reply to the younger command is sent by the server. The client must be aware of the device's capability to process concurrent commands if pipelining is used. For example, pipelining allows multiple client threads to concurently access device memory; the client must ensure these acceses obey device semantics.
An example is a frame buffer device, where the device may allow concurrent access to different areas of video memory, but may have indeterminate behavior if concurrent acceses are performed to command or status registers.
The server and the client can disconnect from each other, either intentionally or unexpectedly. Both the client and the server need to know how to handle such events.
A server disconnecting from the client may indicate that:
It is impossible for the client to know whether or not a failure is intermittent or innocuous and should be retried, therefore the client should reset the VFIO device when it detects the socket has been disconnected. Error recovery will be driven by the guest's device error handling behavior.
The client disconnecting from the server primarily means that the client has exited. Currently, this means that the guest is shut down so the device is no longer needed therefore the server can automatically exit. However, there can be cases where a client disconnection should not result in a server exit:
Therefore in order for the protocol to be forward compatible the server should take no action when the client disconnects. If anything happens to the client the control stack will know about it and can clean up resources accordingly.
A future version of the protocol will support client live migration. This action will require the socket to be quiesced before it is disconnected, This mechanism will be defined when live migration support is added.
A failed command is a command that has been successfully sent and has been responded to with an error code. Failure to send the command in the first place (e.g. because the socket is disconnected) is a different type of error examined earlier in the disconnect section.
Note
QEMU's VFIO retries certain operations if they fail. While this makes sense for real HW, we don't know for sure whether it makes sense for virtual devices.
Defining a retry and timeout scheme is deferred to a future version of the protocol.
The following table lists the VFIO message command IDs, and whether the message command is sent from the client or the server.
Name | Command | Request Direction |
---|---|---|
VFIO_USER_VERSION | 1 | server -> client |
VFIO_USER_DMA_MAP | 2 | client -> server |
VFIO_USER_DMA_UNMAP | 3 | client -> server |
VFIO_USER_DEVICE_GET_INFO | 4 | client -> server |
VFIO_USER_DEVICE_GET_REGION_INFO | 5 | client -> server |
VFIO_USER_DEVICE_GET_IRQ_INFO | 6 | client -> server |
VFIO_USER_DEVICE_SET_IRQS | 7 | client -> server |
VFIO_USER_REGION_READ | 8 | client -> server |
VFIO_USER_REGION_WRITE | 9 | client -> server |
VFIO_USER_DMA_READ | 10 | server -> client |
VFIO_USER_DMA_WRITE | 11 | server -> client |
VFIO_USER_VM_INTERRUPT | 12 | server -> client |
VFIO_USER_DEVICE_RESET | 13 | client -> server |
Note
Some VFIO defines cannot be reused since their values are architecture-specific (e.g. VFIO_IOMMU_MAP_DMA).
All messages, both command messages and reply messages, are preceded by a header that contains basic information about the message. The header is followed by message-specific data described in the sections below.
Name | Offset | Size | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
Message ID | 0 | 2 | ||||||||
Command | 2 | 2 | ||||||||
Message size | 4 | 4 | ||||||||
Flags | 8 | 4 | ||||||||
|
||||||||||
Errno | 12 | 4 | ||||||||
<message data> | 16 | variable |
Each command message in Commands must be replied to with a reply message, unless the message sets the No_Reply bit. The reply consists of the header with the Reply bit set, plus any additional data.
Name | Value |
---|---|
Message ID | <ID> |
Command | 1 |
Message size | 16 + version length |
Flags | Reply bit set in reply |
Errno | 0/errno |
Version | JSON byte array |
This is the initial message sent by the server after the socket connection is established. The version is in JSON format, and the following objects must be included:
Name | Type | Description |
---|---|---|
version | object | {"major": <number>, "minor": <number>} Version supported by the sender, e.g. "0.1". |
capabilities | array | Reserved. Can be omitted for v0.1, otherwise must be empty. |
Upon accepting a connection, the server must send a VFIO_USER_VERSION message proposing a protocol version and a set of capabilities. The client compares these with the versions and capabilities it supports and sends a VFIO_USER_VERSION reply according to the following rules.
The protocol major version will only change when incompatible protocol changes are made, such as changing the message format. The minor version may change when compatible changes are made, such as adding new messages or capabilities, Both the client and server must support all minor versions less than the maximum minor version it supports. E.g., an implementation that supports version 1.3 must also support 1.0 through 1.2.
When making a change to this specification, the protocol version number must be included in the form "added in version X.Y"
Name | Value |
---|---|
Message ID | <ID> |
Command | MAP=2, UNMAP=3 |
Message size | 16 + table size |
Flags | Reply bit set in reply |
Errno | 0/errno |
Table | array of table entries |
This command message is sent by the client to the server to inform it of the memory regions the server can access. It must be sent before the server can perform any DMA to the client. It is normally sent directly after the version handshake is completed, but may also occur when memory is added to or subtracted from the client, or if the client uses a vIOMMU. If the client does not expect the server to perform DMA then it does not need to send to the server VFIO_USER_DMA_MAP and VFIO_USER_DMA_UNMAP commands. If the server does not need to perform DMA the then it can ignore such commands but it must still reply to them. The table is an array of the following structure. This structure is 32 bytes in size, so the message size is: 16 + (# of table entries * 32).
Name | Offset | Size | ||||
---|---|---|---|---|---|---|
Address | 0 | 8 | ||||
Size | 8 | 8 | ||||
Offset | 16 | 8 | ||||
Protections | 24 | 4 | ||||
Flags | 28 | 4 | ||||
|
If a DMA region being added can be directly mapped by the server, an array of file descriptors must be sent as part of the message meta-data. Each region entry must have a corresponding file descriptor. On AF_UNIX sockets, the file descriptors must be passed as SCM_RIGHTS type ancillary data. Otherwise, if a DMA region cannot be directly mapped by the server, it can be accessed by the server using VFIO_USER_DMA_READ and VFIO_USER_DMA_WRITE messages, explained in Read and Write Operations. A command to map over an existing region must be failed by the server with EEXIST set in error field in the reply.
Upon receiving a VFIO_USER_DMA_UNMAP command, if the file descriptor is mapped then the server must release all references to that DMA region before replying, which includes potentially in flight DMA transactions. Removing a portion of a DMA region is possible.
Name | Value |
---|---|
Message ID | <ID> |
Command | 4 |
Message size | 16 in command, 32 in reply |
Flags | Reply bit set in reply |
Errno | 0/errno |
Device info | VFIO device info |
This command message is sent by the client to the server to query for basic information about the device. Only the message header is needed in the command message. The VFIO device info structure is defined in <linux/vfio.h> (struct vfio_device_info).
Name | Offset | Size | ||||||
---|---|---|---|---|---|---|---|---|
argsz | 16 | 4 | ||||||
flags | 20 | 4 | ||||||
|
||||||||
num_regions | 24 | 4 | ||||||
num_irqs | 28 | 4 |
This version of the protocol only supports PCI devices. Additional devices may be supported in future versions.
Name | Value |
---|---|
Message ID | <ID> |
Command | 5 |
Message size | 48 + any caps |
Flags | Reply bit set in reply |
Errno | 0/errno |
Region info | VFIO region info |
This command message is sent by the client to the server to query for information about device memory regions. The VFIO region info structure is defined in <linux/vfio.h> (struct vfio_region_info). Since the client does not know the size of the capabilities, the size of the reply it should expect is 48 plus any capabilities whose size is indicated in the size field of the reply header.
Name | Offset | Size | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
argsz | 16 | 4 | ||||||||||
flags | 20 | 4 | ||||||||||
|
||||||||||||
index | 24 | 4 | ||||||||||
cap_offset | 28 | 4 | ||||||||||
size | 32 | 8 | ||||||||||
offset | 40 | 8 |
The VFIO region information can also include a capabilities list. This list is similar to a PCI capability list - each entry has a common header that identifies a capability and where the next capability in the list can be found. The VFIO capability header format is defined in <linux/vfio.h> (struct vfio_info_cap_header).
Name | Offset | Size |
---|---|---|
id | 0 | 2 |
version | 2 | 2 |
next | 4 | 4 |
Name | Value |
---|---|
id | VFIO_REGION_INFO_CAP_SPARSE_MMAP |
version | 0x1 |
next | <next> |
sparse mmap info | VFIO region info sparse mmap |
The only capability supported in this version of the protocol is for sparse mmap. This capability is defined when only a subrange of the region supports direct access by the client via mmap(). The VFIO sparse mmap area is defined in <linux/vfio.h> (struct vfio_region_sparse_mmap_area).
Name | Offset | Size |
---|---|---|
nr_areas | 0 | 4 |
reserved | 4 | 4 |
offset | 8 | 8 |
size | 16 | 9 |
... |
The VFIO sparse mmap area is defined in <linux/vfio.h> (struct vfio_region_info_cap_sparse_mmap).
Name | Value |
---|---|
Message ID | <ID> |
Command | 6 |
Message size | 32 |
Flags | Reply bit set in reply |
Errno | 0/errno |
IRQ info | VFIO IRQ info |
This command message is sent by the client to the server to query for information about device interrupt types. The VFIO IRQ info structure is defined in <linux/vfio.h> (struct vfio_irq_info).
Name | Offset | Size | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
argsz | 16 | 4 | ||||||||||
flags | 20 | 4 | ||||||||||
|
||||||||||||
index | 24 | 4 | ||||||||||
count | 28 | 4 |
Name | Value |
---|---|
Message ID | <ID> |
Command | 7 |
Message size | 36 + any data |
Flags | Reply bit set in reply |
Errno | 0/errno |
IRQ set | VFIO IRQ set |
This command message is sent by the client to the server to set actions for device interrupt types. The VFIO IRQ set structure is defined in <linux/vfio.h> (struct vfio_irq_set).
Name | Offset | Size | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
argsz | 16 | 4 | ||||||||||||||
flags | 20 | 4 | ||||||||||||||
|
||||||||||||||||
index | 24 | 4 | ||||||||||||||
start | 28 | 4 | ||||||||||||||
count | 32 | 4 | ||||||||||||||
data | 36 | variable |
Not all interrupt types support every combination of data and action flags. The client must know the capabilities of the device and IRQ index before it sends a VFIO_USER_DEVICE_SET_IRQ message.
Not all I/O operations between the client and server can be done via direct access of memory mapped with an mmap() call. In these cases, the client and server use messages sent over the socket. It is expected that these operations will have lower performance than direct access.
The client can access server memory with VFIO_USER_REGION_READ and VFIO_USER_REGION_WRITE commands. These share a common data structure that appears after the message header.
Name | Offset | Size |
---|---|---|
Offset | 16 | 8 |
Region | 24 | 4 |
Count | 28 | 4 |
Data | 32 | variable |
The server can access client memory with VFIO_USER_DMA_READ and VFIO_USER_DMA_WRITE messages. These also share a common data structure that appears after the message header.
Name | Offset | Size |
---|---|---|
Address | 16 | 8 |
Count | 24 | 4 |
Data | 28 | variable |
Name | Value |
---|---|
Message ID | <ID> |
Command | 8 |
Message size | 32 + data size |
Flags | Reply bit set in reply |
Errno | 0/errno |
Read info | REGION read/write data |
This command message is sent from the client to the server to read from server memory. In the command messages, there is no data, and the count is the amount of data to be read. The reply message must include the data read, and its count field is the amount of data read.
Name | Value |
---|---|
Message ID | <ID> |
Command | 9 |
Message size | 32 + data size |
Flags | Reply bit set in reply |
Errno | 0/errno |
Write info | REGION read/write data |
This command message is sent from the client to the server to write to server memory. The command message must contain the data to be written, and its count field must contain the amount of write data. The count field in the reply message must be zero.
Name | Value |
---|---|
Message ID | <ID> |
Command | 10 |
Message size | 28 + data size |
Flags | Reply bit set in reply |
Errno | 0/errno |
DMA info | DMA read/write data |
This command message is sent from the server to the client to read from client memory. In the command message, there is no data, and the count must will be the amount of data to be read. The reply message must include the data read, and its count field must be the amount of data read.
Name | Value |
---|---|
Message ID | <ID> |
Command | 11 |
Message size | 28 + data size |
Flags | Reply bit set in reply |
Errno | 0/errno |
DMA info | DMA read/write data |
This command message is sent from the server to the client to write to server memory. The command message must contain the data to be written, and its count field must contain the amount of write data. The count field in the reply message must be zero.
Name | Value |
---|---|
Message ID | <ID> |
Command | 12 |
Message size | 24 |
Flags | Reply bit set in reply |
Errno | 0/errno |
Interrupt info | <interrupt> |
This command message is sent from the server to the client to signal the device has raised an interrupt.
Name | Offset | Size |
---|---|---|
Index | 16 | 4 |
Sub-index | 20 | 4 |
Name | Value |
---|---|
Message ID | <ID> |
Command | 13 |
Message size | 16 |
Flags | Reply bit set in reply |
Errno | 0/errno |
This command message is sent from the client to the server to reset the device.
The following VFIO commands do not have an equivalent vfio-user command:
However, once support for live migration for VFIO devices is finalized some of the above commands may have to be handled by the client in their corresponding vfio-user form. This will be addressed in a future protocol version.
Currently live migration is not supported for devices passed through via VFIO, therefore it is not supported for vfio-user, either. This is being actively worked on in the Add migration support for VFIO devices (v25) patch series.
The current VFIO implementation includes group and container idioms that describe how a device relates to the host IOMMU. In the vfio-user implementation, the IOMMU is implemented in SW by the client, and is not visible to the server. The simplest idea would be that the client put each device into its own group and container.
vfio-user backend program conventions are based on the vhost-user ones.
[Prev in Thread] | Current Thread | [Next in Thread] |