xref: /openbmc/linux/include/uapi/linux/vhost.h (revision ddc141e5)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _LINUX_VHOST_H
3 #define _LINUX_VHOST_H
4 /* Userspace interface for in-kernel virtio accelerators. */
5 
6 /* vhost is used to reduce the number of system calls involved in virtio.
7  *
8  * Existing virtio net code is used in the guest without modification.
9  *
10  * This header includes interface used by userspace hypervisor for
11  * device configuration.
12  */
13 
14 #include <linux/types.h>
15 #include <linux/compiler.h>
16 #include <linux/ioctl.h>
17 #include <linux/virtio_config.h>
18 #include <linux/virtio_ring.h>
19 
20 struct vhost_vring_state {
21 	unsigned int index;
22 	unsigned int num;
23 };
24 
25 struct vhost_vring_file {
26 	unsigned int index;
27 	int fd; /* Pass -1 to unbind from file. */
28 
29 };
30 
31 struct vhost_vring_addr {
32 	unsigned int index;
33 	/* Option flags. */
34 	unsigned int flags;
35 	/* Flag values: */
36 	/* Whether log address is valid. If set enables logging. */
37 #define VHOST_VRING_F_LOG 0
38 
39 	/* Start of array of descriptors (virtually contiguous) */
40 	__u64 desc_user_addr;
41 	/* Used structure address. Must be 32 bit aligned */
42 	__u64 used_user_addr;
43 	/* Available structure address. Must be 16 bit aligned */
44 	__u64 avail_user_addr;
45 	/* Logging support. */
46 	/* Log writes to used structure, at offset calculated from specified
47 	 * address. Address must be 32 bit aligned. */
48 	__u64 log_guest_addr;
49 };
50 
51 /* no alignment requirement */
52 struct vhost_iotlb_msg {
53 	__u64 iova;
54 	__u64 size;
55 	__u64 uaddr;
56 #define VHOST_ACCESS_RO      0x1
57 #define VHOST_ACCESS_WO      0x2
58 #define VHOST_ACCESS_RW      0x3
59 	__u8 perm;
60 #define VHOST_IOTLB_MISS           1
61 #define VHOST_IOTLB_UPDATE         2
62 #define VHOST_IOTLB_INVALIDATE     3
63 #define VHOST_IOTLB_ACCESS_FAIL    4
64 	__u8 type;
65 };
66 
67 #define VHOST_IOTLB_MSG 0x1
68 
69 struct vhost_msg {
70 	int type;
71 	union {
72 		struct vhost_iotlb_msg iotlb;
73 		__u8 padding[64];
74 	};
75 };
76 
77 struct vhost_memory_region {
78 	__u64 guest_phys_addr;
79 	__u64 memory_size; /* bytes */
80 	__u64 userspace_addr;
81 	__u64 flags_padding; /* No flags are currently specified. */
82 };
83 
84 /* All region addresses and sizes must be 4K aligned. */
85 #define VHOST_PAGE_SIZE 0x1000
86 
87 struct vhost_memory {
88 	__u32 nregions;
89 	__u32 padding;
90 	struct vhost_memory_region regions[0];
91 };
92 
93 /* ioctls */
94 
95 #define VHOST_VIRTIO 0xAF
96 
97 /* Features bitmask for forward compatibility.  Transport bits are used for
98  * vhost specific features. */
99 #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
100 #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
101 
102 /* Set current process as the (exclusive) owner of this file descriptor.  This
103  * must be called before any other vhost command.  Further calls to
104  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
105 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
106 /* Give up ownership, and reset the device to default values.
107  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
108 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
109 
110 /* Set up/modify memory layout */
111 #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
112 
113 /* Write logging setup. */
114 /* Memory writes can optionally be logged by setting bit at an offset
115  * (calculated from the physical address) from specified log base.
116  * The bit is set using an atomic 32 bit operation. */
117 /* Set base address for logging. */
118 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
119 /* Specify an eventfd file descriptor to signal on log write. */
120 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
121 
122 /* Ring setup. */
123 /* Set number of descriptors in ring. This parameter can not
124  * be modified while ring is running (bound to a device). */
125 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
126 /* Set addresses for the ring. */
127 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
128 /* Base value where queue looks for available descriptors */
129 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
130 /* Get accessor: reads index, writes value in num */
131 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
132 
133 /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
134  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
135  * The byte order cannot be changed while the device is active: trying to do so
136  * returns -EBUSY.
137  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
138  * set.
139  * Not all kernel configurations support this ioctl, but all configurations that
140  * support SET also support GET.
141  */
142 #define VHOST_VRING_LITTLE_ENDIAN 0
143 #define VHOST_VRING_BIG_ENDIAN 1
144 #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
145 #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
146 
147 /* The following ioctls use eventfd file descriptors to signal and poll
148  * for events. */
149 
150 /* Set eventfd to poll for added buffers */
151 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
152 /* Set eventfd to signal when buffers have beed used */
153 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
154 /* Set eventfd to signal an error */
155 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
156 /* Set busy loop timeout (in us) */
157 #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
158 					 struct vhost_vring_state)
159 /* Get busy loop timeout (in us) */
160 #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
161 					 struct vhost_vring_state)
162 
163 /* VHOST_NET specific defines */
164 
165 /* Attach virtio net ring to a raw socket, or tap device.
166  * The socket must be already bound to an ethernet device, this device will be
167  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
168  * device.  This can be used to stop the ring (e.g. for migration). */
169 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
170 
171 /* Feature bits */
172 /* Log all write descriptors. Can be changed while device is active. */
173 #define VHOST_F_LOG_ALL 26
174 /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
175 #define VHOST_NET_F_VIRTIO_NET_HDR 27
176 
177 /* VHOST_SCSI specific definitions */
178 
179 /*
180  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
181  *
182  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
183  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
184  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
185  *            All the targets under vhost_wwpn can be seen and used by guset.
186  */
187 
188 #define VHOST_SCSI_ABI_VERSION	1
189 
190 struct vhost_scsi_target {
191 	int abi_version;
192 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
193 	unsigned short vhost_tpgt;
194 	unsigned short reserved;
195 };
196 
197 #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
198 #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
199 /* Changing this breaks userspace. */
200 #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
201 /* Set and get the events missed flag */
202 #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
203 #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
204 
205 /* VHOST_VSOCK specific defines */
206 
207 #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
208 #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
209 
210 #endif
211