vhost.h 4.5 KB

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