vhost.h 6.8 KB

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