xref: /openbmc/linux/include/uapi/linux/vhost.h (revision 6b1e6cc7)
1607ca46eSDavid Howells #ifndef _LINUX_VHOST_H
2607ca46eSDavid Howells #define _LINUX_VHOST_H
3607ca46eSDavid Howells /* Userspace interface for in-kernel virtio accelerators. */
4607ca46eSDavid Howells 
5607ca46eSDavid Howells /* vhost is used to reduce the number of system calls involved in virtio.
6607ca46eSDavid Howells  *
7607ca46eSDavid Howells  * Existing virtio net code is used in the guest without modification.
8607ca46eSDavid Howells  *
9607ca46eSDavid Howells  * This header includes interface used by userspace hypervisor for
10607ca46eSDavid Howells  * device configuration.
11607ca46eSDavid Howells  */
12607ca46eSDavid Howells 
13607ca46eSDavid Howells #include <linux/types.h>
14607ca46eSDavid Howells #include <linux/compiler.h>
15607ca46eSDavid Howells #include <linux/ioctl.h>
16607ca46eSDavid Howells #include <linux/virtio_config.h>
17607ca46eSDavid Howells #include <linux/virtio_ring.h>
18607ca46eSDavid Howells 
19607ca46eSDavid Howells struct vhost_vring_state {
20607ca46eSDavid Howells 	unsigned int index;
21607ca46eSDavid Howells 	unsigned int num;
22607ca46eSDavid Howells };
23607ca46eSDavid Howells 
24607ca46eSDavid Howells struct vhost_vring_file {
25607ca46eSDavid Howells 	unsigned int index;
26607ca46eSDavid Howells 	int fd; /* Pass -1 to unbind from file. */
27607ca46eSDavid Howells 
28607ca46eSDavid Howells };
29607ca46eSDavid Howells 
30607ca46eSDavid Howells struct vhost_vring_addr {
31607ca46eSDavid Howells 	unsigned int index;
32607ca46eSDavid Howells 	/* Option flags. */
33607ca46eSDavid Howells 	unsigned int flags;
34607ca46eSDavid Howells 	/* Flag values: */
35607ca46eSDavid Howells 	/* Whether log address is valid. If set enables logging. */
36607ca46eSDavid Howells #define VHOST_VRING_F_LOG 0
37607ca46eSDavid Howells 
38607ca46eSDavid Howells 	/* Start of array of descriptors (virtually contiguous) */
39607ca46eSDavid Howells 	__u64 desc_user_addr;
40607ca46eSDavid Howells 	/* Used structure address. Must be 32 bit aligned */
41607ca46eSDavid Howells 	__u64 used_user_addr;
42607ca46eSDavid Howells 	/* Available structure address. Must be 16 bit aligned */
43607ca46eSDavid Howells 	__u64 avail_user_addr;
44607ca46eSDavid Howells 	/* Logging support. */
45607ca46eSDavid Howells 	/* Log writes to used structure, at offset calculated from specified
46607ca46eSDavid Howells 	 * address. Address must be 32 bit aligned. */
47607ca46eSDavid Howells 	__u64 log_guest_addr;
48607ca46eSDavid Howells };
49607ca46eSDavid Howells 
506b1e6cc7SJason Wang /* no alignment requirement */
516b1e6cc7SJason Wang struct vhost_iotlb_msg {
526b1e6cc7SJason Wang 	__u64 iova;
536b1e6cc7SJason Wang 	__u64 size;
546b1e6cc7SJason Wang 	__u64 uaddr;
556b1e6cc7SJason Wang #define VHOST_ACCESS_RO      0x1
566b1e6cc7SJason Wang #define VHOST_ACCESS_WO      0x2
576b1e6cc7SJason Wang #define VHOST_ACCESS_RW      0x3
586b1e6cc7SJason Wang 	__u8 perm;
596b1e6cc7SJason Wang #define VHOST_IOTLB_MISS           1
606b1e6cc7SJason Wang #define VHOST_IOTLB_UPDATE         2
616b1e6cc7SJason Wang #define VHOST_IOTLB_INVALIDATE     3
626b1e6cc7SJason Wang #define VHOST_IOTLB_ACCESS_FAIL    4
636b1e6cc7SJason Wang 	__u8 type;
646b1e6cc7SJason Wang };
656b1e6cc7SJason Wang 
666b1e6cc7SJason Wang #define VHOST_IOTLB_MSG 0x1
676b1e6cc7SJason Wang 
686b1e6cc7SJason Wang struct vhost_msg {
696b1e6cc7SJason Wang 	int type;
706b1e6cc7SJason Wang 	union {
716b1e6cc7SJason Wang 		struct vhost_iotlb_msg iotlb;
726b1e6cc7SJason Wang 		__u8 padding[64];
736b1e6cc7SJason Wang 	};
746b1e6cc7SJason Wang };
756b1e6cc7SJason Wang 
76607ca46eSDavid Howells struct vhost_memory_region {
77607ca46eSDavid Howells 	__u64 guest_phys_addr;
78607ca46eSDavid Howells 	__u64 memory_size; /* bytes */
79607ca46eSDavid Howells 	__u64 userspace_addr;
80607ca46eSDavid Howells 	__u64 flags_padding; /* No flags are currently specified. */
81607ca46eSDavid Howells };
82607ca46eSDavid Howells 
83607ca46eSDavid Howells /* All region addresses and sizes must be 4K aligned. */
84607ca46eSDavid Howells #define VHOST_PAGE_SIZE 0x1000
85607ca46eSDavid Howells 
86607ca46eSDavid Howells struct vhost_memory {
87607ca46eSDavid Howells 	__u32 nregions;
88607ca46eSDavid Howells 	__u32 padding;
89607ca46eSDavid Howells 	struct vhost_memory_region regions[0];
90607ca46eSDavid Howells };
91607ca46eSDavid Howells 
92607ca46eSDavid Howells /* ioctls */
93607ca46eSDavid Howells 
94607ca46eSDavid Howells #define VHOST_VIRTIO 0xAF
95607ca46eSDavid Howells 
96607ca46eSDavid Howells /* Features bitmask for forward compatibility.  Transport bits are used for
97607ca46eSDavid Howells  * vhost specific features. */
98607ca46eSDavid Howells #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
99607ca46eSDavid Howells #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
100607ca46eSDavid Howells 
101607ca46eSDavid Howells /* Set current process as the (exclusive) owner of this file descriptor.  This
102607ca46eSDavid Howells  * must be called before any other vhost command.  Further calls to
103607ca46eSDavid Howells  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
104607ca46eSDavid Howells #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
105607ca46eSDavid Howells /* Give up ownership, and reset the device to default values.
106607ca46eSDavid Howells  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
107607ca46eSDavid Howells #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
108607ca46eSDavid Howells 
109607ca46eSDavid Howells /* Set up/modify memory layout */
110607ca46eSDavid Howells #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
111607ca46eSDavid Howells 
112607ca46eSDavid Howells /* Write logging setup. */
113607ca46eSDavid Howells /* Memory writes can optionally be logged by setting bit at an offset
114607ca46eSDavid Howells  * (calculated from the physical address) from specified log base.
115607ca46eSDavid Howells  * The bit is set using an atomic 32 bit operation. */
116607ca46eSDavid Howells /* Set base address for logging. */
117607ca46eSDavid Howells #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
118607ca46eSDavid Howells /* Specify an eventfd file descriptor to signal on log write. */
119607ca46eSDavid Howells #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
120607ca46eSDavid Howells 
121607ca46eSDavid Howells /* Ring setup. */
122607ca46eSDavid Howells /* Set number of descriptors in ring. This parameter can not
123607ca46eSDavid Howells  * be modified while ring is running (bound to a device). */
124607ca46eSDavid Howells #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
125607ca46eSDavid Howells /* Set addresses for the ring. */
126607ca46eSDavid Howells #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
127607ca46eSDavid Howells /* Base value where queue looks for available descriptors */
128607ca46eSDavid Howells #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
129607ca46eSDavid Howells /* Get accessor: reads index, writes value in num */
130607ca46eSDavid Howells #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
131607ca46eSDavid Howells 
1322751c988SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
1332751c988SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
1342751c988SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
1352751c988SGreg Kurz  * returns -EBUSY.
1362751c988SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
1372751c988SGreg Kurz  * set.
1382751c988SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
1392751c988SGreg Kurz  * support SET also support GET.
1402751c988SGreg Kurz  */
1412751c988SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
1422751c988SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
1432751c988SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
1442751c988SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
1452751c988SGreg Kurz 
146607ca46eSDavid Howells /* The following ioctls use eventfd file descriptors to signal and poll
147607ca46eSDavid Howells  * for events. */
148607ca46eSDavid Howells 
149607ca46eSDavid Howells /* Set eventfd to poll for added buffers */
150607ca46eSDavid Howells #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
151607ca46eSDavid Howells /* Set eventfd to signal when buffers have beed used */
152607ca46eSDavid Howells #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
153607ca46eSDavid Howells /* Set eventfd to signal an error */
154607ca46eSDavid Howells #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
15503088137SJason Wang /* Set busy loop timeout (in us) */
15603088137SJason Wang #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
15703088137SJason Wang 					 struct vhost_vring_state)
15803088137SJason Wang /* Get busy loop timeout (in us) */
15903088137SJason Wang #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
16003088137SJason Wang 					 struct vhost_vring_state)
161607ca46eSDavid Howells 
162607ca46eSDavid Howells /* VHOST_NET specific defines */
163607ca46eSDavid Howells 
164607ca46eSDavid Howells /* Attach virtio net ring to a raw socket, or tap device.
165607ca46eSDavid Howells  * The socket must be already bound to an ethernet device, this device will be
166607ca46eSDavid Howells  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
167607ca46eSDavid Howells  * device.  This can be used to stop the ring (e.g. for migration). */
168607ca46eSDavid Howells #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
169607ca46eSDavid Howells 
170607ca46eSDavid Howells /* Feature bits */
171607ca46eSDavid Howells /* Log all write descriptors. Can be changed while device is active. */
172607ca46eSDavid Howells #define VHOST_F_LOG_ALL 26
173607ca46eSDavid Howells /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
174607ca46eSDavid Howells #define VHOST_NET_F_VIRTIO_NET_HDR 27
1756b1e6cc7SJason Wang /* Vhost have device IOTLB */
1766b1e6cc7SJason Wang #define VHOST_F_DEVICE_IOTLB 63
177607ca46eSDavid Howells 
1785012a3a3SMichael S. Tsirkin /* VHOST_SCSI specific definitions */
1795012a3a3SMichael S. Tsirkin 
1805012a3a3SMichael S. Tsirkin /*
1815012a3a3SMichael S. Tsirkin  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
1825012a3a3SMichael S. Tsirkin  *
1835012a3a3SMichael S. Tsirkin  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
1845012a3a3SMichael S. Tsirkin  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
1855012a3a3SMichael S. Tsirkin  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
1865012a3a3SMichael S. Tsirkin  *            All the targets under vhost_wwpn can be seen and used by guset.
1875012a3a3SMichael S. Tsirkin  */
1885012a3a3SMichael S. Tsirkin 
1895012a3a3SMichael S. Tsirkin #define VHOST_SCSI_ABI_VERSION	1
1905012a3a3SMichael S. Tsirkin 
1915012a3a3SMichael S. Tsirkin struct vhost_scsi_target {
1925012a3a3SMichael S. Tsirkin 	int abi_version;
1935012a3a3SMichael S. Tsirkin 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
1945012a3a3SMichael S. Tsirkin 	unsigned short vhost_tpgt;
1955012a3a3SMichael S. Tsirkin 	unsigned short reserved;
1965012a3a3SMichael S. Tsirkin };
1975012a3a3SMichael S. Tsirkin 
1985012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
1995012a3a3SMichael S. Tsirkin #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
2005012a3a3SMichael S. Tsirkin /* Changing this breaks userspace. */
2015012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
2025012a3a3SMichael S. Tsirkin /* Set and get the events missed flag */
2035012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
2045012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
2055012a3a3SMichael S. Tsirkin 
206433fc58eSAsias He /* VHOST_VSOCK specific defines */
207433fc58eSAsias He 
208433fc58eSAsias He #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
209433fc58eSAsias He #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
210433fc58eSAsias He 
211607ca46eSDavid Howells #endif
212