vhost.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef _VHOST_H
  2. #define _VHOST_H
  3. #include <linux/eventfd.h>
  4. #include <linux/vhost.h>
  5. #include <linux/mm.h>
  6. #include <linux/mutex.h>
  7. #include <linux/poll.h>
  8. #include <linux/file.h>
  9. #include <linux/uio.h>
  10. #include <linux/virtio_config.h>
  11. #include <linux/virtio_ring.h>
  12. #include <linux/atomic.h>
  13. struct vhost_work;
  14. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  15. struct vhost_work {
  16. struct list_head node;
  17. vhost_work_fn_t fn;
  18. wait_queue_head_t done;
  19. int flushing;
  20. unsigned queue_seq;
  21. unsigned done_seq;
  22. };
  23. /* Poll a file (eventfd or socket) */
  24. /* Note: there's nothing vhost specific about this structure. */
  25. struct vhost_poll {
  26. poll_table table;
  27. wait_queue_head_t *wqh;
  28. wait_queue_t wait;
  29. struct vhost_work work;
  30. unsigned long mask;
  31. struct vhost_dev *dev;
  32. };
  33. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  34. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  35. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  36. unsigned long mask, struct vhost_dev *dev);
  37. int vhost_poll_start(struct vhost_poll *poll, struct file *file);
  38. void vhost_poll_stop(struct vhost_poll *poll);
  39. void vhost_poll_flush(struct vhost_poll *poll);
  40. void vhost_poll_queue(struct vhost_poll *poll);
  41. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
  42. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  43. struct vhost_log {
  44. u64 addr;
  45. u64 len;
  46. };
  47. /* The virtqueue structure describes a queue attached to a device. */
  48. struct vhost_virtqueue {
  49. struct vhost_dev *dev;
  50. /* The actual ring of buffers. */
  51. struct mutex mutex;
  52. unsigned int num;
  53. struct vring_desc __user *desc;
  54. struct vring_avail __user *avail;
  55. struct vring_used __user *used;
  56. struct file *kick;
  57. struct file *call;
  58. struct file *error;
  59. struct eventfd_ctx *call_ctx;
  60. struct eventfd_ctx *error_ctx;
  61. struct eventfd_ctx *log_ctx;
  62. struct vhost_poll poll;
  63. /* The routine to call when the Guest pings us, or timeout. */
  64. vhost_work_fn_t handle_kick;
  65. /* Last available index we saw. */
  66. u16 last_avail_idx;
  67. /* Caches available index value from user. */
  68. u16 avail_idx;
  69. /* Last index we used. */
  70. u16 last_used_idx;
  71. /* Used flags */
  72. u16 used_flags;
  73. /* Last used index value we have signalled on */
  74. u16 signalled_used;
  75. /* Last used index value we have signalled on */
  76. bool signalled_used_valid;
  77. /* Log writes to used structure. */
  78. bool log_used;
  79. u64 log_addr;
  80. struct iovec iov[UIO_MAXIOV];
  81. struct iovec *indirect;
  82. struct vring_used_elem *heads;
  83. /* Protected by virtqueue mutex. */
  84. struct vhost_memory *memory;
  85. void *private_data;
  86. u64 acked_features;
  87. /* Log write descriptors */
  88. void __user *log_base;
  89. struct vhost_log *log;
  90. /* Ring endianness. Defaults to legacy native endianness.
  91. * Set to true when starting a modern virtio device. */
  92. bool is_le;
  93. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  94. /* Ring endianness requested by userspace for cross-endian support. */
  95. bool user_be;
  96. #endif
  97. };
  98. struct vhost_dev {
  99. struct vhost_memory *memory;
  100. struct mm_struct *mm;
  101. struct mutex mutex;
  102. struct vhost_virtqueue **vqs;
  103. int nvqs;
  104. struct file *log_file;
  105. struct eventfd_ctx *log_ctx;
  106. spinlock_t work_lock;
  107. struct list_head work_list;
  108. struct task_struct *worker;
  109. };
  110. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
  111. long vhost_dev_set_owner(struct vhost_dev *dev);
  112. bool vhost_dev_has_owner(struct vhost_dev *dev);
  113. long vhost_dev_check_owner(struct vhost_dev *);
  114. struct vhost_memory *vhost_dev_reset_owner_prepare(void);
  115. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_memory *);
  116. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  117. void vhost_dev_stop(struct vhost_dev *);
  118. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  119. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  120. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  121. int vhost_log_access_ok(struct vhost_dev *);
  122. int vhost_get_vq_desc(struct vhost_virtqueue *,
  123. struct iovec iov[], unsigned int iov_count,
  124. unsigned int *out_num, unsigned int *in_num,
  125. struct vhost_log *log, unsigned int *log_num);
  126. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  127. int vhost_init_used(struct vhost_virtqueue *);
  128. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  129. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  130. unsigned count);
  131. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  132. unsigned int id, int len);
  133. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  134. struct vring_used_elem *heads, unsigned count);
  135. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  136. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  137. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  138. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  139. unsigned int log_num, u64 len);
  140. #define vq_err(vq, fmt, ...) do { \
  141. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  142. if ((vq)->error_ctx) \
  143. eventfd_signal((vq)->error_ctx, 1);\
  144. } while (0)
  145. enum {
  146. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  147. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  148. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  149. (1ULL << VHOST_F_LOG_ALL) |
  150. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  151. (1ULL << VIRTIO_F_VERSION_1)
  152. };
  153. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  154. {
  155. return vq->acked_features & (1ULL << bit);
  156. }
  157. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  158. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  159. {
  160. return vq->is_le;
  161. }
  162. #else
  163. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  164. {
  165. return virtio_legacy_is_little_endian() || vq->is_le;
  166. }
  167. #endif
  168. /* Memory accessors */
  169. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  170. {
  171. return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
  172. }
  173. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  174. {
  175. return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
  176. }
  177. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  178. {
  179. return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
  180. }
  181. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  182. {
  183. return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
  184. }
  185. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  186. {
  187. return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
  188. }
  189. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  190. {
  191. return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
  192. }
  193. #endif