xref: /openbmc/linux/include/uapi/linux/vhost.h (revision c1ecd8e9)
16f52b16cSGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2607ca46eSDavid Howells #ifndef _LINUX_VHOST_H
3607ca46eSDavid Howells #define _LINUX_VHOST_H
4607ca46eSDavid Howells /* Userspace interface for in-kernel virtio accelerators. */
5607ca46eSDavid Howells 
6607ca46eSDavid Howells /* vhost is used to reduce the number of system calls involved in virtio.
7607ca46eSDavid Howells  *
8607ca46eSDavid Howells  * Existing virtio net code is used in the guest without modification.
9607ca46eSDavid Howells  *
10607ca46eSDavid Howells  * This header includes interface used by userspace hypervisor for
11607ca46eSDavid Howells  * device configuration.
12607ca46eSDavid Howells  */
13607ca46eSDavid Howells 
144b867132SPaolo Bonzini #include <linux/vhost_types.h>
15607ca46eSDavid Howells #include <linux/types.h>
16607ca46eSDavid Howells #include <linux/ioctl.h>
17607ca46eSDavid Howells 
18776f3950SZhu Lingshan #define VHOST_FILE_UNBIND -1
19776f3950SZhu Lingshan 
20607ca46eSDavid Howells /* ioctls */
21607ca46eSDavid Howells 
22607ca46eSDavid Howells #define VHOST_VIRTIO 0xAF
23607ca46eSDavid Howells 
24607ca46eSDavid Howells /* Features bitmask for forward compatibility.  Transport bits are used for
25607ca46eSDavid Howells  * vhost specific features. */
26607ca46eSDavid Howells #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
27607ca46eSDavid Howells #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
28607ca46eSDavid Howells 
29607ca46eSDavid Howells /* Set current process as the (exclusive) owner of this file descriptor.  This
30607ca46eSDavid Howells  * must be called before any other vhost command.  Further calls to
31607ca46eSDavid Howells  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
32607ca46eSDavid Howells #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
33607ca46eSDavid Howells /* Give up ownership, and reset the device to default values.
34607ca46eSDavid Howells  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
35607ca46eSDavid Howells #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
36607ca46eSDavid Howells 
37607ca46eSDavid Howells /* Set up/modify memory layout */
38607ca46eSDavid Howells #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
39607ca46eSDavid Howells 
40607ca46eSDavid Howells /* Write logging setup. */
41607ca46eSDavid Howells /* Memory writes can optionally be logged by setting bit at an offset
42607ca46eSDavid Howells  * (calculated from the physical address) from specified log base.
43607ca46eSDavid Howells  * The bit is set using an atomic 32 bit operation. */
44607ca46eSDavid Howells /* Set base address for logging. */
45607ca46eSDavid Howells #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
46607ca46eSDavid Howells /* Specify an eventfd file descriptor to signal on log write. */
47607ca46eSDavid Howells #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
48*c1ecd8e9SMike Christie /* By default, a device gets one vhost_worker that its virtqueues share. This
49*c1ecd8e9SMike Christie  * command allows the owner of the device to create an additional vhost_worker
50*c1ecd8e9SMike Christie  * for the device. It can later be bound to 1 or more of its virtqueues using
51*c1ecd8e9SMike Christie  * the VHOST_ATTACH_VRING_WORKER command.
52*c1ecd8e9SMike Christie  *
53*c1ecd8e9SMike Christie  * This must be called after VHOST_SET_OWNER and the caller must be the owner
54*c1ecd8e9SMike Christie  * of the device. The new thread will inherit caller's cgroups and namespaces,
55*c1ecd8e9SMike Christie  * and will share the caller's memory space. The new thread will also be
56*c1ecd8e9SMike Christie  * counted against the caller's RLIMIT_NPROC value.
57*c1ecd8e9SMike Christie  *
58*c1ecd8e9SMike Christie  * The worker's ID used in other commands will be returned in
59*c1ecd8e9SMike Christie  * vhost_worker_state.
60*c1ecd8e9SMike Christie  */
61*c1ecd8e9SMike Christie #define VHOST_NEW_WORKER _IOR(VHOST_VIRTIO, 0x8, struct vhost_worker_state)
62*c1ecd8e9SMike Christie /* Free a worker created with VHOST_NEW_WORKER if it's not attached to any
63*c1ecd8e9SMike Christie  * virtqueue. If userspace is not able to call this for workers its created,
64*c1ecd8e9SMike Christie  * the kernel will free all the device's workers when the device is closed.
65*c1ecd8e9SMike Christie  */
66*c1ecd8e9SMike Christie #define VHOST_FREE_WORKER _IOW(VHOST_VIRTIO, 0x9, struct vhost_worker_state)
67607ca46eSDavid Howells 
68607ca46eSDavid Howells /* Ring setup. */
69607ca46eSDavid Howells /* Set number of descriptors in ring. This parameter can not
70607ca46eSDavid Howells  * be modified while ring is running (bound to a device). */
71607ca46eSDavid Howells #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
72607ca46eSDavid Howells /* Set addresses for the ring. */
73607ca46eSDavid Howells #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
74607ca46eSDavid Howells /* Base value where queue looks for available descriptors */
75607ca46eSDavid Howells #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
76607ca46eSDavid Howells /* Get accessor: reads index, writes value in num */
77607ca46eSDavid Howells #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
78607ca46eSDavid Howells 
792751c988SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
802751c988SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
812751c988SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
822751c988SGreg Kurz  * returns -EBUSY.
832751c988SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
842751c988SGreg Kurz  * set.
852751c988SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
862751c988SGreg Kurz  * support SET also support GET.
872751c988SGreg Kurz  */
882751c988SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
892751c988SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
902751c988SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
912751c988SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
92*c1ecd8e9SMike Christie /* Attach a vhost_worker created with VHOST_NEW_WORKER to one of the device's
93*c1ecd8e9SMike Christie  * virtqueues. This must be done before VHOST_SET_VRING_KICK and the driver
94*c1ecd8e9SMike Christie  * specific ioctl to activate the virtqueue (VHOST_SCSI_SET_ENDPOINT,
95*c1ecd8e9SMike Christie  * VHOST_NET_SET_BACKEND, VHOST_VSOCK_SET_RUNNING) has been run.
96*c1ecd8e9SMike Christie  *
97*c1ecd8e9SMike Christie  * This will replace the virtqueue's existing worker. If the replaced worker
98*c1ecd8e9SMike Christie  * is no longer attached to any virtqueues, it can be freed with
99*c1ecd8e9SMike Christie  * VHOST_FREE_WORKER.
100*c1ecd8e9SMike Christie  */
101*c1ecd8e9SMike Christie #define VHOST_ATTACH_VRING_WORKER _IOW(VHOST_VIRTIO, 0x15,		\
102*c1ecd8e9SMike Christie 				       struct vhost_vring_worker)
103*c1ecd8e9SMike Christie /* Return the vring worker's ID */
104*c1ecd8e9SMike Christie #define VHOST_GET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x16,		\
105*c1ecd8e9SMike Christie 				     struct vhost_vring_worker)
1062751c988SGreg Kurz 
107607ca46eSDavid Howells /* The following ioctls use eventfd file descriptors to signal and poll
108607ca46eSDavid Howells  * for events. */
109607ca46eSDavid Howells 
110607ca46eSDavid Howells /* Set eventfd to poll for added buffers */
111607ca46eSDavid Howells #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
112607ca46eSDavid Howells /* Set eventfd to signal when buffers have beed used */
113607ca46eSDavid Howells #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
114607ca46eSDavid Howells /* Set eventfd to signal an error */
115607ca46eSDavid Howells #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
11603088137SJason Wang /* Set busy loop timeout (in us) */
11703088137SJason Wang #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
11803088137SJason Wang 					 struct vhost_vring_state)
11903088137SJason Wang /* Get busy loop timeout (in us) */
12003088137SJason Wang #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
12103088137SJason Wang 					 struct vhost_vring_state)
122607ca46eSDavid Howells 
123429711aeSJason Wang /* Set or get vhost backend capability */
124429711aeSJason Wang 
125429711aeSJason Wang #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
126c48300c9SGleb Fotengauer-Malinovskiy #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
127429711aeSJason Wang 
128607ca46eSDavid Howells /* VHOST_NET specific defines */
129607ca46eSDavid Howells 
130607ca46eSDavid Howells /* Attach virtio net ring to a raw socket, or tap device.
131607ca46eSDavid Howells  * The socket must be already bound to an ethernet device, this device will be
132607ca46eSDavid Howells  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
133607ca46eSDavid Howells  * device.  This can be used to stop the ring (e.g. for migration). */
134607ca46eSDavid Howells #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
135607ca46eSDavid Howells 
1364b867132SPaolo Bonzini /* VHOST_SCSI specific defines */
1375012a3a3SMichael S. Tsirkin 
1385012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
1395012a3a3SMichael S. Tsirkin #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
1405012a3a3SMichael S. Tsirkin /* Changing this breaks userspace. */
1415012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
1425012a3a3SMichael S. Tsirkin /* Set and get the events missed flag */
1435012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
1445012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
1455012a3a3SMichael S. Tsirkin 
146433fc58eSAsias He /* VHOST_VSOCK specific defines */
147433fc58eSAsias He 
148433fc58eSAsias He #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
149433fc58eSAsias He #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
150433fc58eSAsias He 
1514c8cf318STiwei Bie /* VHOST_VDPA specific defines */
1524c8cf318STiwei Bie 
1534c8cf318STiwei Bie /* Get the device id. The device ids follow the same definition of
1544c8cf318STiwei Bie  * the device id defined in virtio-spec.
1554c8cf318STiwei Bie  */
1564c8cf318STiwei Bie #define VHOST_VDPA_GET_DEVICE_ID	_IOR(VHOST_VIRTIO, 0x70, __u32)
1574c8cf318STiwei Bie /* Get and set the status. The status bits follow the same definition
1584c8cf318STiwei Bie  * of the device status defined in virtio-spec.
1594c8cf318STiwei Bie  */
1604c8cf318STiwei Bie #define VHOST_VDPA_GET_STATUS		_IOR(VHOST_VIRTIO, 0x71, __u8)
1614c8cf318STiwei Bie #define VHOST_VDPA_SET_STATUS		_IOW(VHOST_VIRTIO, 0x72, __u8)
1624c8cf318STiwei Bie /* Get and set the device config. The device config follows the same
1634c8cf318STiwei Bie  * definition of the device config defined in virtio-spec.
1644c8cf318STiwei Bie  */
1654c8cf318STiwei Bie #define VHOST_VDPA_GET_CONFIG		_IOR(VHOST_VIRTIO, 0x73, \
1664c8cf318STiwei Bie 					     struct vhost_vdpa_config)
1674c8cf318STiwei Bie #define VHOST_VDPA_SET_CONFIG		_IOW(VHOST_VIRTIO, 0x74, \
1684c8cf318STiwei Bie 					     struct vhost_vdpa_config)
1694c8cf318STiwei Bie /* Enable/disable the ring. */
1704c8cf318STiwei Bie #define VHOST_VDPA_SET_VRING_ENABLE	_IOW(VHOST_VIRTIO, 0x75, \
1714c8cf318STiwei Bie 					     struct vhost_vring_state)
1724c8cf318STiwei Bie /* Get the max ring size. */
1734c8cf318STiwei Bie #define VHOST_VDPA_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
1744c8cf318STiwei Bie 
175776f3950SZhu Lingshan /* Set event fd for config interrupt*/
176776f3950SZhu Lingshan #define VHOST_VDPA_SET_CONFIG_CALL	_IOW(VHOST_VIRTIO, 0x77, int)
1771b48dc03SJason Wang 
1781b48dc03SJason Wang /* Get the valid iova range */
1791b48dc03SJason Wang #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
1801b48dc03SJason Wang 					     struct vhost_vdpa_iova_range)
181a61280ddSLongpeng /* Get the config size */
182a61280ddSLongpeng #define VHOST_VDPA_GET_CONFIG_SIZE	_IOR(VHOST_VIRTIO, 0x79, __u32)
183a61280ddSLongpeng 
184b04d910aSLongpeng /* Get the count of all virtqueues */
185b04d910aSLongpeng #define VHOST_VDPA_GET_VQS_COUNT	_IOR(VHOST_VIRTIO, 0x80, __u32)
186b04d910aSLongpeng 
1873ace88bdSGautam Dawar /* Get the number of virtqueue groups. */
1883ace88bdSGautam Dawar #define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x81, __u32)
1893ace88bdSGautam Dawar 
190a0c95f20SGautam Dawar /* Get the number of address spaces. */
191a0c95f20SGautam Dawar #define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
1922d1fcb77SGautam Dawar 
1932d1fcb77SGautam Dawar /* Get the group for a virtqueue: read index, write group in num,
1942d1fcb77SGautam Dawar  * The virtqueue index is stored in the index field of
1952d1fcb77SGautam Dawar  * vhost_vring_state. The group for this specific virtqueue is
1962d1fcb77SGautam Dawar  * returned via num field of vhost_vring_state.
1972d1fcb77SGautam Dawar  */
1982d1fcb77SGautam Dawar #define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
1992d1fcb77SGautam Dawar 					      struct vhost_vring_state)
20084d7c8fdSGautam Dawar /* Set the ASID for a virtqueue group. The group index is stored in
20184d7c8fdSGautam Dawar  * the index field of vhost_vring_state, the ASID associated with this
20284d7c8fdSGautam Dawar  * group is stored at num field of vhost_vring_state.
20384d7c8fdSGautam Dawar  */
20484d7c8fdSGautam Dawar #define VHOST_VDPA_SET_GROUP_ASID	_IOW(VHOST_VIRTIO, 0x7C, \
20584d7c8fdSGautam Dawar 					     struct vhost_vring_state)
20684d7c8fdSGautam Dawar 
207f345a014SEugenio Pérez /* Suspend a device so it does not process virtqueue requests anymore
208f345a014SEugenio Pérez  *
209f345a014SEugenio Pérez  * After the return of ioctl the device must preserve all the necessary state
210f345a014SEugenio Pérez  * (the virtqueue vring base plus the possible device specific states) that is
211f345a014SEugenio Pérez  * required for restoring in the future. The device must not change its
212f345a014SEugenio Pérez  * configuration after that point.
213f345a014SEugenio Pérez  */
214f345a014SEugenio Pérez #define VHOST_VDPA_SUSPEND		_IO(VHOST_VIRTIO, 0x7D)
215f345a014SEugenio Pérez 
2163b688d7aSSebastien Boeuf /* Resume a device so it can resume processing virtqueue requests
2173b688d7aSSebastien Boeuf  *
2183b688d7aSSebastien Boeuf  * After the return of this ioctl the device will have restored all the
2193b688d7aSSebastien Boeuf  * necessary states and it is fully operational to continue processing the
2203b688d7aSSebastien Boeuf  * virtqueue descriptors.
2213b688d7aSSebastien Boeuf  */
2223b688d7aSSebastien Boeuf #define VHOST_VDPA_RESUME		_IO(VHOST_VIRTIO, 0x7E)
2233b688d7aSSebastien Boeuf 
224607ca46eSDavid Howells #endif
225