xref: /openbmc/linux/include/uapi/linux/vhost.h (revision c48300c9)
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 
14607ca46eSDavid Howells #include <linux/types.h>
15607ca46eSDavid Howells #include <linux/compiler.h>
16607ca46eSDavid Howells #include <linux/ioctl.h>
17607ca46eSDavid Howells #include <linux/virtio_config.h>
18607ca46eSDavid Howells #include <linux/virtio_ring.h>
19607ca46eSDavid Howells 
20607ca46eSDavid Howells struct vhost_vring_state {
21607ca46eSDavid Howells 	unsigned int index;
22607ca46eSDavid Howells 	unsigned int num;
23607ca46eSDavid Howells };
24607ca46eSDavid Howells 
25607ca46eSDavid Howells struct vhost_vring_file {
26607ca46eSDavid Howells 	unsigned int index;
27607ca46eSDavid Howells 	int fd; /* Pass -1 to unbind from file. */
28607ca46eSDavid Howells 
29607ca46eSDavid Howells };
30607ca46eSDavid Howells 
31607ca46eSDavid Howells struct vhost_vring_addr {
32607ca46eSDavid Howells 	unsigned int index;
33607ca46eSDavid Howells 	/* Option flags. */
34607ca46eSDavid Howells 	unsigned int flags;
35607ca46eSDavid Howells 	/* Flag values: */
36607ca46eSDavid Howells 	/* Whether log address is valid. If set enables logging. */
37607ca46eSDavid Howells #define VHOST_VRING_F_LOG 0
38607ca46eSDavid Howells 
39607ca46eSDavid Howells 	/* Start of array of descriptors (virtually contiguous) */
40607ca46eSDavid Howells 	__u64 desc_user_addr;
41607ca46eSDavid Howells 	/* Used structure address. Must be 32 bit aligned */
42607ca46eSDavid Howells 	__u64 used_user_addr;
43607ca46eSDavid Howells 	/* Available structure address. Must be 16 bit aligned */
44607ca46eSDavid Howells 	__u64 avail_user_addr;
45607ca46eSDavid Howells 	/* Logging support. */
46607ca46eSDavid Howells 	/* Log writes to used structure, at offset calculated from specified
47607ca46eSDavid Howells 	 * address. Address must be 32 bit aligned. */
48607ca46eSDavid Howells 	__u64 log_guest_addr;
49607ca46eSDavid Howells };
50607ca46eSDavid Howells 
516b1e6cc7SJason Wang /* no alignment requirement */
526b1e6cc7SJason Wang struct vhost_iotlb_msg {
536b1e6cc7SJason Wang 	__u64 iova;
546b1e6cc7SJason Wang 	__u64 size;
556b1e6cc7SJason Wang 	__u64 uaddr;
566b1e6cc7SJason Wang #define VHOST_ACCESS_RO      0x1
576b1e6cc7SJason Wang #define VHOST_ACCESS_WO      0x2
586b1e6cc7SJason Wang #define VHOST_ACCESS_RW      0x3
596b1e6cc7SJason Wang 	__u8 perm;
606b1e6cc7SJason Wang #define VHOST_IOTLB_MISS           1
616b1e6cc7SJason Wang #define VHOST_IOTLB_UPDATE         2
626b1e6cc7SJason Wang #define VHOST_IOTLB_INVALIDATE     3
636b1e6cc7SJason Wang #define VHOST_IOTLB_ACCESS_FAIL    4
646b1e6cc7SJason Wang 	__u8 type;
656b1e6cc7SJason Wang };
666b1e6cc7SJason Wang 
676b1e6cc7SJason Wang #define VHOST_IOTLB_MSG 0x1
68429711aeSJason Wang #define VHOST_IOTLB_MSG_V2 0x2
696b1e6cc7SJason Wang 
706b1e6cc7SJason Wang struct vhost_msg {
716b1e6cc7SJason Wang 	int type;
726b1e6cc7SJason Wang 	union {
736b1e6cc7SJason Wang 		struct vhost_iotlb_msg iotlb;
746b1e6cc7SJason Wang 		__u8 padding[64];
756b1e6cc7SJason Wang 	};
766b1e6cc7SJason Wang };
776b1e6cc7SJason Wang 
78429711aeSJason Wang struct vhost_msg_v2 {
79429711aeSJason Wang 	__u32 type;
80429711aeSJason Wang 	__u32 reserved;
81429711aeSJason Wang 	union {
82429711aeSJason Wang 		struct vhost_iotlb_msg iotlb;
83429711aeSJason Wang 		__u8 padding[64];
84429711aeSJason Wang 	};
85429711aeSJason Wang };
86429711aeSJason Wang 
87607ca46eSDavid Howells struct vhost_memory_region {
88607ca46eSDavid Howells 	__u64 guest_phys_addr;
89607ca46eSDavid Howells 	__u64 memory_size; /* bytes */
90607ca46eSDavid Howells 	__u64 userspace_addr;
91607ca46eSDavid Howells 	__u64 flags_padding; /* No flags are currently specified. */
92607ca46eSDavid Howells };
93607ca46eSDavid Howells 
94607ca46eSDavid Howells /* All region addresses and sizes must be 4K aligned. */
95607ca46eSDavid Howells #define VHOST_PAGE_SIZE 0x1000
96607ca46eSDavid Howells 
97607ca46eSDavid Howells struct vhost_memory {
98607ca46eSDavid Howells 	__u32 nregions;
99607ca46eSDavid Howells 	__u32 padding;
100607ca46eSDavid Howells 	struct vhost_memory_region regions[0];
101607ca46eSDavid Howells };
102607ca46eSDavid Howells 
103607ca46eSDavid Howells /* ioctls */
104607ca46eSDavid Howells 
105607ca46eSDavid Howells #define VHOST_VIRTIO 0xAF
106607ca46eSDavid Howells 
107607ca46eSDavid Howells /* Features bitmask for forward compatibility.  Transport bits are used for
108607ca46eSDavid Howells  * vhost specific features. */
109607ca46eSDavid Howells #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
110607ca46eSDavid Howells #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
111607ca46eSDavid Howells 
112607ca46eSDavid Howells /* Set current process as the (exclusive) owner of this file descriptor.  This
113607ca46eSDavid Howells  * must be called before any other vhost command.  Further calls to
114607ca46eSDavid Howells  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
115607ca46eSDavid Howells #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
116607ca46eSDavid Howells /* Give up ownership, and reset the device to default values.
117607ca46eSDavid Howells  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
118607ca46eSDavid Howells #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
119607ca46eSDavid Howells 
120607ca46eSDavid Howells /* Set up/modify memory layout */
121607ca46eSDavid Howells #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
122607ca46eSDavid Howells 
123607ca46eSDavid Howells /* Write logging setup. */
124607ca46eSDavid Howells /* Memory writes can optionally be logged by setting bit at an offset
125607ca46eSDavid Howells  * (calculated from the physical address) from specified log base.
126607ca46eSDavid Howells  * The bit is set using an atomic 32 bit operation. */
127607ca46eSDavid Howells /* Set base address for logging. */
128607ca46eSDavid Howells #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
129607ca46eSDavid Howells /* Specify an eventfd file descriptor to signal on log write. */
130607ca46eSDavid Howells #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
131607ca46eSDavid Howells 
132607ca46eSDavid Howells /* Ring setup. */
133607ca46eSDavid Howells /* Set number of descriptors in ring. This parameter can not
134607ca46eSDavid Howells  * be modified while ring is running (bound to a device). */
135607ca46eSDavid Howells #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
136607ca46eSDavid Howells /* Set addresses for the ring. */
137607ca46eSDavid Howells #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
138607ca46eSDavid Howells /* Base value where queue looks for available descriptors */
139607ca46eSDavid Howells #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
140607ca46eSDavid Howells /* Get accessor: reads index, writes value in num */
141607ca46eSDavid Howells #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
142607ca46eSDavid Howells 
1432751c988SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
1442751c988SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
1452751c988SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
1462751c988SGreg Kurz  * returns -EBUSY.
1472751c988SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
1482751c988SGreg Kurz  * set.
1492751c988SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
1502751c988SGreg Kurz  * support SET also support GET.
1512751c988SGreg Kurz  */
1522751c988SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
1532751c988SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
1542751c988SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
1552751c988SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
1562751c988SGreg Kurz 
157607ca46eSDavid Howells /* The following ioctls use eventfd file descriptors to signal and poll
158607ca46eSDavid Howells  * for events. */
159607ca46eSDavid Howells 
160607ca46eSDavid Howells /* Set eventfd to poll for added buffers */
161607ca46eSDavid Howells #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
162607ca46eSDavid Howells /* Set eventfd to signal when buffers have beed used */
163607ca46eSDavid Howells #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
164607ca46eSDavid Howells /* Set eventfd to signal an error */
165607ca46eSDavid Howells #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
16603088137SJason Wang /* Set busy loop timeout (in us) */
16703088137SJason Wang #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
16803088137SJason Wang 					 struct vhost_vring_state)
16903088137SJason Wang /* Get busy loop timeout (in us) */
17003088137SJason Wang #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
17103088137SJason Wang 					 struct vhost_vring_state)
172607ca46eSDavid Howells 
173429711aeSJason Wang /* Set or get vhost backend capability */
174429711aeSJason Wang 
175429711aeSJason Wang /* Use message type V2 */
176429711aeSJason Wang #define VHOST_BACKEND_F_IOTLB_MSG_V2 0x1
177429711aeSJason Wang 
178429711aeSJason Wang #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
179c48300c9SGleb Fotengauer-Malinovskiy #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
180429711aeSJason Wang 
181607ca46eSDavid Howells /* VHOST_NET specific defines */
182607ca46eSDavid Howells 
183607ca46eSDavid Howells /* Attach virtio net ring to a raw socket, or tap device.
184607ca46eSDavid Howells  * The socket must be already bound to an ethernet device, this device will be
185607ca46eSDavid Howells  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
186607ca46eSDavid Howells  * device.  This can be used to stop the ring (e.g. for migration). */
187607ca46eSDavid Howells #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
188607ca46eSDavid Howells 
189607ca46eSDavid Howells /* Feature bits */
190607ca46eSDavid Howells /* Log all write descriptors. Can be changed while device is active. */
191607ca46eSDavid Howells #define VHOST_F_LOG_ALL 26
192607ca46eSDavid Howells /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
193607ca46eSDavid Howells #define VHOST_NET_F_VIRTIO_NET_HDR 27
194607ca46eSDavid Howells 
1955012a3a3SMichael S. Tsirkin /* VHOST_SCSI specific definitions */
1965012a3a3SMichael S. Tsirkin 
1975012a3a3SMichael S. Tsirkin /*
1985012a3a3SMichael S. Tsirkin  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
1995012a3a3SMichael S. Tsirkin  *
2005012a3a3SMichael S. Tsirkin  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
2015012a3a3SMichael S. Tsirkin  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
2025012a3a3SMichael S. Tsirkin  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
2035012a3a3SMichael S. Tsirkin  *            All the targets under vhost_wwpn can be seen and used by guset.
2045012a3a3SMichael S. Tsirkin  */
2055012a3a3SMichael S. Tsirkin 
2065012a3a3SMichael S. Tsirkin #define VHOST_SCSI_ABI_VERSION	1
2075012a3a3SMichael S. Tsirkin 
2085012a3a3SMichael S. Tsirkin struct vhost_scsi_target {
2095012a3a3SMichael S. Tsirkin 	int abi_version;
2105012a3a3SMichael S. Tsirkin 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
2115012a3a3SMichael S. Tsirkin 	unsigned short vhost_tpgt;
2125012a3a3SMichael S. Tsirkin 	unsigned short reserved;
2135012a3a3SMichael S. Tsirkin };
2145012a3a3SMichael S. Tsirkin 
2155012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
2165012a3a3SMichael S. Tsirkin #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
2175012a3a3SMichael S. Tsirkin /* Changing this breaks userspace. */
2185012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
2195012a3a3SMichael S. Tsirkin /* Set and get the events missed flag */
2205012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
2215012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
2225012a3a3SMichael S. Tsirkin 
223433fc58eSAsias He /* VHOST_VSOCK specific defines */
224433fc58eSAsias He 
225433fc58eSAsias He #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
226433fc58eSAsias He #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
227433fc58eSAsias He 
228607ca46eSDavid Howells #endif
229