vhost.h 7.5 KB

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