xref: /openbmc/linux/include/uapi/linux/vhost.h (revision 6f52b16c)
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
686b1e6cc7SJason Wang 
696b1e6cc7SJason Wang struct vhost_msg {
706b1e6cc7SJason Wang 	int type;
716b1e6cc7SJason Wang 	union {
726b1e6cc7SJason Wang 		struct vhost_iotlb_msg iotlb;
736b1e6cc7SJason Wang 		__u8 padding[64];
746b1e6cc7SJason Wang 	};
756b1e6cc7SJason Wang };
766b1e6cc7SJason Wang 
77607ca46eSDavid Howells struct vhost_memory_region {
78607ca46eSDavid Howells 	__u64 guest_phys_addr;
79607ca46eSDavid Howells 	__u64 memory_size; /* bytes */
80607ca46eSDavid Howells 	__u64 userspace_addr;
81607ca46eSDavid Howells 	__u64 flags_padding; /* No flags are currently specified. */
82607ca46eSDavid Howells };
83607ca46eSDavid Howells 
84607ca46eSDavid Howells /* All region addresses and sizes must be 4K aligned. */
85607ca46eSDavid Howells #define VHOST_PAGE_SIZE 0x1000
86607ca46eSDavid Howells 
87607ca46eSDavid Howells struct vhost_memory {
88607ca46eSDavid Howells 	__u32 nregions;
89607ca46eSDavid Howells 	__u32 padding;
90607ca46eSDavid Howells 	struct vhost_memory_region regions[0];
91607ca46eSDavid Howells };
92607ca46eSDavid Howells 
93607ca46eSDavid Howells /* ioctls */
94607ca46eSDavid Howells 
95607ca46eSDavid Howells #define VHOST_VIRTIO 0xAF
96607ca46eSDavid Howells 
97607ca46eSDavid Howells /* Features bitmask for forward compatibility.  Transport bits are used for
98607ca46eSDavid Howells  * vhost specific features. */
99607ca46eSDavid Howells #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
100607ca46eSDavid Howells #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
101607ca46eSDavid Howells 
102607ca46eSDavid Howells /* Set current process as the (exclusive) owner of this file descriptor.  This
103607ca46eSDavid Howells  * must be called before any other vhost command.  Further calls to
104607ca46eSDavid Howells  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
105607ca46eSDavid Howells #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
106607ca46eSDavid Howells /* Give up ownership, and reset the device to default values.
107607ca46eSDavid Howells  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
108607ca46eSDavid Howells #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
109607ca46eSDavid Howells 
110607ca46eSDavid Howells /* Set up/modify memory layout */
111607ca46eSDavid Howells #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
112607ca46eSDavid Howells 
113607ca46eSDavid Howells /* Write logging setup. */
114607ca46eSDavid Howells /* Memory writes can optionally be logged by setting bit at an offset
115607ca46eSDavid Howells  * (calculated from the physical address) from specified log base.
116607ca46eSDavid Howells  * The bit is set using an atomic 32 bit operation. */
117607ca46eSDavid Howells /* Set base address for logging. */
118607ca46eSDavid Howells #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
119607ca46eSDavid Howells /* Specify an eventfd file descriptor to signal on log write. */
120607ca46eSDavid Howells #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
121607ca46eSDavid Howells 
122607ca46eSDavid Howells /* Ring setup. */
123607ca46eSDavid Howells /* Set number of descriptors in ring. This parameter can not
124607ca46eSDavid Howells  * be modified while ring is running (bound to a device). */
125607ca46eSDavid Howells #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
126607ca46eSDavid Howells /* Set addresses for the ring. */
127607ca46eSDavid Howells #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
128607ca46eSDavid Howells /* Base value where queue looks for available descriptors */
129607ca46eSDavid Howells #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
130607ca46eSDavid Howells /* Get accessor: reads index, writes value in num */
131607ca46eSDavid Howells #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
132607ca46eSDavid Howells 
1332751c988SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
1342751c988SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
1352751c988SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
1362751c988SGreg Kurz  * returns -EBUSY.
1372751c988SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
1382751c988SGreg Kurz  * set.
1392751c988SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
1402751c988SGreg Kurz  * support SET also support GET.
1412751c988SGreg Kurz  */
1422751c988SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
1432751c988SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
1442751c988SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
1452751c988SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
1462751c988SGreg Kurz 
147607ca46eSDavid Howells /* The following ioctls use eventfd file descriptors to signal and poll
148607ca46eSDavid Howells  * for events. */
149607ca46eSDavid Howells 
150607ca46eSDavid Howells /* Set eventfd to poll for added buffers */
151607ca46eSDavid Howells #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
152607ca46eSDavid Howells /* Set eventfd to signal when buffers have beed used */
153607ca46eSDavid Howells #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
154607ca46eSDavid Howells /* Set eventfd to signal an error */
155607ca46eSDavid Howells #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
15603088137SJason Wang /* Set busy loop timeout (in us) */
15703088137SJason Wang #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
15803088137SJason Wang 					 struct vhost_vring_state)
15903088137SJason Wang /* Get busy loop timeout (in us) */
16003088137SJason Wang #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
16103088137SJason Wang 					 struct vhost_vring_state)
162607ca46eSDavid Howells 
163607ca46eSDavid Howells /* VHOST_NET specific defines */
164607ca46eSDavid Howells 
165607ca46eSDavid Howells /* Attach virtio net ring to a raw socket, or tap device.
166607ca46eSDavid Howells  * The socket must be already bound to an ethernet device, this device will be
167607ca46eSDavid Howells  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
168607ca46eSDavid Howells  * device.  This can be used to stop the ring (e.g. for migration). */
169607ca46eSDavid Howells #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
170607ca46eSDavid Howells 
171607ca46eSDavid Howells /* Feature bits */
172607ca46eSDavid Howells /* Log all write descriptors. Can be changed while device is active. */
173607ca46eSDavid Howells #define VHOST_F_LOG_ALL 26
174607ca46eSDavid Howells /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
175607ca46eSDavid Howells #define VHOST_NET_F_VIRTIO_NET_HDR 27
176607ca46eSDavid Howells 
1775012a3a3SMichael S. Tsirkin /* VHOST_SCSI specific definitions */
1785012a3a3SMichael S. Tsirkin 
1795012a3a3SMichael S. Tsirkin /*
1805012a3a3SMichael S. Tsirkin  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
1815012a3a3SMichael S. Tsirkin  *
1825012a3a3SMichael S. Tsirkin  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
1835012a3a3SMichael S. Tsirkin  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
1845012a3a3SMichael S. Tsirkin  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
1855012a3a3SMichael S. Tsirkin  *            All the targets under vhost_wwpn can be seen and used by guset.
1865012a3a3SMichael S. Tsirkin  */
1875012a3a3SMichael S. Tsirkin 
1885012a3a3SMichael S. Tsirkin #define VHOST_SCSI_ABI_VERSION	1
1895012a3a3SMichael S. Tsirkin 
1905012a3a3SMichael S. Tsirkin struct vhost_scsi_target {
1915012a3a3SMichael S. Tsirkin 	int abi_version;
1925012a3a3SMichael S. Tsirkin 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
1935012a3a3SMichael S. Tsirkin 	unsigned short vhost_tpgt;
1945012a3a3SMichael S. Tsirkin 	unsigned short reserved;
1955012a3a3SMichael S. Tsirkin };
1965012a3a3SMichael S. Tsirkin 
1975012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
1985012a3a3SMichael S. Tsirkin #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
1995012a3a3SMichael S. Tsirkin /* Changing this breaks userspace. */
2005012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
2015012a3a3SMichael S. Tsirkin /* Set and get the events missed flag */
2025012a3a3SMichael S. Tsirkin #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
2035012a3a3SMichael S. Tsirkin #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
2045012a3a3SMichael S. Tsirkin 
205433fc58eSAsias He /* VHOST_VSOCK specific defines */
206433fc58eSAsias He 
207433fc58eSAsias He #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
208433fc58eSAsias He #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
209433fc58eSAsias He 
210607ca46eSDavid Howells #endif
211