1*51b24e34SJan Kiszka #ifndef _LINUX_VHOST_H
2*51b24e34SJan Kiszka #define _LINUX_VHOST_H
3*51b24e34SJan Kiszka /* Userspace interface for in-kernel virtio accelerators. */
4*51b24e34SJan Kiszka 
5*51b24e34SJan Kiszka /* vhost is used to reduce the number of system calls involved in virtio.
6*51b24e34SJan Kiszka  *
7*51b24e34SJan Kiszka  * Existing virtio net code is used in the guest without modification.
8*51b24e34SJan Kiszka  *
9*51b24e34SJan Kiszka  * This header includes interface used by userspace hypervisor for
10*51b24e34SJan Kiszka  * device configuration.
11*51b24e34SJan Kiszka  */
12*51b24e34SJan Kiszka 
13*51b24e34SJan Kiszka #include <linux/types.h>
14*51b24e34SJan Kiszka 
15*51b24e34SJan Kiszka #include <linux/ioctl.h>
16*51b24e34SJan Kiszka #include <linux/virtio_config.h>
17*51b24e34SJan Kiszka #include <linux/virtio_ring.h>
18*51b24e34SJan Kiszka 
19*51b24e34SJan Kiszka struct vhost_vring_state {
20*51b24e34SJan Kiszka 	unsigned int index;
21*51b24e34SJan Kiszka 	unsigned int num;
22*51b24e34SJan Kiszka };
23*51b24e34SJan Kiszka 
24*51b24e34SJan Kiszka struct vhost_vring_file {
25*51b24e34SJan Kiszka 	unsigned int index;
26*51b24e34SJan Kiszka 	int fd; /* Pass -1 to unbind from file. */
27*51b24e34SJan Kiszka 
28*51b24e34SJan Kiszka };
29*51b24e34SJan Kiszka 
30*51b24e34SJan Kiszka struct vhost_vring_addr {
31*51b24e34SJan Kiszka 	unsigned int index;
32*51b24e34SJan Kiszka 	/* Option flags. */
33*51b24e34SJan Kiszka 	unsigned int flags;
34*51b24e34SJan Kiszka 	/* Flag values: */
35*51b24e34SJan Kiszka 	/* Whether log address is valid. If set enables logging. */
36*51b24e34SJan Kiszka #define VHOST_VRING_F_LOG 0
37*51b24e34SJan Kiszka 
38*51b24e34SJan Kiszka 	/* Start of array of descriptors (virtually contiguous) */
39*51b24e34SJan Kiszka 	__u64 desc_user_addr;
40*51b24e34SJan Kiszka 	/* Used structure address. Must be 32 bit aligned */
41*51b24e34SJan Kiszka 	__u64 used_user_addr;
42*51b24e34SJan Kiszka 	/* Available structure address. Must be 16 bit aligned */
43*51b24e34SJan Kiszka 	__u64 avail_user_addr;
44*51b24e34SJan Kiszka 	/* Logging support. */
45*51b24e34SJan Kiszka 	/* Log writes to used structure, at offset calculated from specified
46*51b24e34SJan Kiszka 	 * address. Address must be 32 bit aligned. */
47*51b24e34SJan Kiszka 	__u64 log_guest_addr;
48*51b24e34SJan Kiszka };
49*51b24e34SJan Kiszka 
50*51b24e34SJan Kiszka struct vhost_memory_region {
51*51b24e34SJan Kiszka 	__u64 guest_phys_addr;
52*51b24e34SJan Kiszka 	__u64 memory_size; /* bytes */
53*51b24e34SJan Kiszka 	__u64 userspace_addr;
54*51b24e34SJan Kiszka 	__u64 flags_padding; /* No flags are currently specified. */
55*51b24e34SJan Kiszka };
56*51b24e34SJan Kiszka 
57*51b24e34SJan Kiszka /* All region addresses and sizes must be 4K aligned. */
58*51b24e34SJan Kiszka #define VHOST_PAGE_SIZE 0x1000
59*51b24e34SJan Kiszka 
60*51b24e34SJan Kiszka struct vhost_memory {
61*51b24e34SJan Kiszka 	__u32 nregions;
62*51b24e34SJan Kiszka 	__u32 padding;
63*51b24e34SJan Kiszka 	struct vhost_memory_region regions[0];
64*51b24e34SJan Kiszka };
65*51b24e34SJan Kiszka 
66*51b24e34SJan Kiszka /* ioctls */
67*51b24e34SJan Kiszka 
68*51b24e34SJan Kiszka #define VHOST_VIRTIO 0xAF
69*51b24e34SJan Kiszka 
70*51b24e34SJan Kiszka /* Features bitmask for forward compatibility.  Transport bits are used for
71*51b24e34SJan Kiszka  * vhost specific features. */
72*51b24e34SJan Kiszka #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
73*51b24e34SJan Kiszka #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
74*51b24e34SJan Kiszka 
75*51b24e34SJan Kiszka /* Set current process as the (exclusive) owner of this file descriptor.  This
76*51b24e34SJan Kiszka  * must be called before any other vhost command.  Further calls to
77*51b24e34SJan Kiszka  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
78*51b24e34SJan Kiszka #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
79*51b24e34SJan Kiszka /* Give up ownership, and reset the device to default values.
80*51b24e34SJan Kiszka  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
81*51b24e34SJan Kiszka #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
82*51b24e34SJan Kiszka 
83*51b24e34SJan Kiszka /* Set up/modify memory layout */
84*51b24e34SJan Kiszka #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
85*51b24e34SJan Kiszka 
86*51b24e34SJan Kiszka /* Write logging setup. */
87*51b24e34SJan Kiszka /* Memory writes can optionally be logged by setting bit at an offset
88*51b24e34SJan Kiszka  * (calculated from the physical address) from specified log base.
89*51b24e34SJan Kiszka  * The bit is set using an atomic 32 bit operation. */
90*51b24e34SJan Kiszka /* Set base address for logging. */
91*51b24e34SJan Kiszka #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
92*51b24e34SJan Kiszka /* Specify an eventfd file descriptor to signal on log write. */
93*51b24e34SJan Kiszka #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
94*51b24e34SJan Kiszka 
95*51b24e34SJan Kiszka /* Ring setup. */
96*51b24e34SJan Kiszka /* Set number of descriptors in ring. This parameter can not
97*51b24e34SJan Kiszka  * be modified while ring is running (bound to a device). */
98*51b24e34SJan Kiszka #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
99*51b24e34SJan Kiszka /* Set addresses for the ring. */
100*51b24e34SJan Kiszka #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
101*51b24e34SJan Kiszka /* Base value where queue looks for available descriptors */
102*51b24e34SJan Kiszka #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
103*51b24e34SJan Kiszka /* Get accessor: reads index, writes value in num */
104*51b24e34SJan Kiszka #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
105*51b24e34SJan Kiszka 
106*51b24e34SJan Kiszka /* The following ioctls use eventfd file descriptors to signal and poll
107*51b24e34SJan Kiszka  * for events. */
108*51b24e34SJan Kiszka 
109*51b24e34SJan Kiszka /* Set eventfd to poll for added buffers */
110*51b24e34SJan Kiszka #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
111*51b24e34SJan Kiszka /* Set eventfd to signal when buffers have beed used */
112*51b24e34SJan Kiszka #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
113*51b24e34SJan Kiszka /* Set eventfd to signal an error */
114*51b24e34SJan Kiszka #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
115*51b24e34SJan Kiszka 
116*51b24e34SJan Kiszka /* VHOST_NET specific defines */
117*51b24e34SJan Kiszka 
118*51b24e34SJan Kiszka /* Attach virtio net ring to a raw socket, or tap device.
119*51b24e34SJan Kiszka  * The socket must be already bound to an ethernet device, this device will be
120*51b24e34SJan Kiszka  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
121*51b24e34SJan Kiszka  * device.  This can be used to stop the ring (e.g. for migration). */
122*51b24e34SJan Kiszka #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
123*51b24e34SJan Kiszka 
124*51b24e34SJan Kiszka /* Feature bits */
125*51b24e34SJan Kiszka /* Log all write descriptors. Can be changed while device is active. */
126*51b24e34SJan Kiszka #define VHOST_F_LOG_ALL 26
127*51b24e34SJan Kiszka /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
128*51b24e34SJan Kiszka #define VHOST_NET_F_VIRTIO_NET_HDR 27
129*51b24e34SJan Kiszka 
130*51b24e34SJan Kiszka #endif
131