vhost.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_device;
  14. struct vhost_work;
  15. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  16. struct vhost_work {
  17. struct list_head node;
  18. vhost_work_fn_t fn;
  19. wait_queue_head_t done;
  20. int flushing;
  21. unsigned queue_seq;
  22. unsigned done_seq;
  23. };
  24. /* Poll a file (eventfd or socket) */
  25. /* Note: there's nothing vhost specific about this structure. */
  26. struct vhost_poll {
  27. poll_table table;
  28. wait_queue_head_t *wqh;
  29. wait_queue_t wait;
  30. struct vhost_work work;
  31. unsigned long mask;
  32. struct vhost_dev *dev;
  33. };
  34. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  35. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  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. struct vhost_log {
  43. u64 addr;
  44. u64 len;
  45. };
  46. struct vhost_virtqueue;
  47. struct vhost_ubuf_ref {
  48. struct kref kref;
  49. wait_queue_head_t wait;
  50. struct vhost_virtqueue *vq;
  51. };
  52. struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy);
  53. void vhost_ubuf_put(struct vhost_ubuf_ref *);
  54. void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *);
  55. struct ubuf_info;
  56. /* The virtqueue structure describes a queue attached to a device. */
  57. struct vhost_virtqueue {
  58. struct vhost_dev *dev;
  59. /* The actual ring of buffers. */
  60. struct mutex mutex;
  61. unsigned int num;
  62. struct vring_desc __user *desc;
  63. struct vring_avail __user *avail;
  64. struct vring_used __user *used;
  65. struct file *kick;
  66. struct file *call;
  67. struct file *error;
  68. struct eventfd_ctx *call_ctx;
  69. struct eventfd_ctx *error_ctx;
  70. struct eventfd_ctx *log_ctx;
  71. struct vhost_poll poll;
  72. /* The routine to call when the Guest pings us, or timeout. */
  73. vhost_work_fn_t handle_kick;
  74. /* Last available index we saw. */
  75. u16 last_avail_idx;
  76. /* Caches available index value from user. */
  77. u16 avail_idx;
  78. /* Last index we used. */
  79. u16 last_used_idx;
  80. /* Used flags */
  81. u16 used_flags;
  82. /* Last used index value we have signalled on */
  83. u16 signalled_used;
  84. /* Last used index value we have signalled on */
  85. bool signalled_used_valid;
  86. /* Log writes to used structure. */
  87. bool log_used;
  88. u64 log_addr;
  89. struct iovec iov[UIO_MAXIOV];
  90. /* hdr is used to store the virtio header.
  91. * Since each iovec has >= 1 byte length, we never need more than
  92. * header length entries to store the header. */
  93. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  94. struct iovec *indirect;
  95. size_t vhost_hlen;
  96. size_t sock_hlen;
  97. struct vring_used_elem *heads;
  98. /* We use a kind of RCU to access private pointer.
  99. * All readers access it from worker, which makes it possible to
  100. * flush the vhost_work instead of synchronize_rcu. Therefore readers do
  101. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  102. * vhost_work execution acts instead of rcu_read_lock() and the end of
  103. * vhost_work execution acts instead of rcu_read_unlock().
  104. * Writers use virtqueue mutex. */
  105. void __rcu *private_data;
  106. /* Log write descriptors */
  107. void __user *log_base;
  108. struct vhost_log *log;
  109. /* vhost zerocopy support fields below: */
  110. /* last used idx for outstanding DMA zerocopy buffers */
  111. int upend_idx;
  112. /* first used idx for DMA done zerocopy buffers */
  113. int done_idx;
  114. /* an array of userspace buffers info */
  115. struct ubuf_info *ubuf_info;
  116. /* Reference counting for outstanding ubufs.
  117. * Protected by vq mutex. Writers must also take device mutex. */
  118. struct vhost_ubuf_ref *ubufs;
  119. };
  120. struct vhost_dev {
  121. /* Readers use RCU to access memory table pointer
  122. * log base pointer and features.
  123. * Writers use mutex below.*/
  124. struct vhost_memory __rcu *memory;
  125. struct mm_struct *mm;
  126. struct mutex mutex;
  127. unsigned acked_features;
  128. struct vhost_virtqueue *vqs;
  129. int nvqs;
  130. struct file *log_file;
  131. struct eventfd_ctx *log_ctx;
  132. spinlock_t work_lock;
  133. struct list_head work_list;
  134. struct task_struct *worker;
  135. };
  136. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
  137. long vhost_dev_check_owner(struct vhost_dev *);
  138. long vhost_dev_reset_owner(struct vhost_dev *);
  139. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  140. void vhost_dev_stop(struct vhost_dev *);
  141. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  142. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  143. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  144. int vhost_log_access_ok(struct vhost_dev *);
  145. int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
  146. struct iovec iov[], unsigned int iov_count,
  147. unsigned int *out_num, unsigned int *in_num,
  148. struct vhost_log *log, unsigned int *log_num);
  149. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  150. int vhost_init_used(struct vhost_virtqueue *);
  151. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  152. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  153. unsigned count);
  154. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  155. unsigned int id, int len);
  156. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  157. struct vring_used_elem *heads, unsigned count);
  158. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  159. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  160. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  161. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  162. unsigned int log_num, u64 len);
  163. #define vq_err(vq, fmt, ...) do { \
  164. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  165. if ((vq)->error_ctx) \
  166. eventfd_signal((vq)->error_ctx, 1);\
  167. } while (0)
  168. enum {
  169. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  170. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  171. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  172. (1ULL << VHOST_F_LOG_ALL),
  173. VHOST_NET_FEATURES = VHOST_FEATURES |
  174. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  175. (1ULL << VIRTIO_NET_F_MRG_RXBUF),
  176. };
  177. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  178. {
  179. unsigned acked_features;
  180. /* TODO: check that we are running from vhost_worker or dev mutex is
  181. * held? */
  182. acked_features = rcu_dereference_index_check(dev->acked_features, 1);
  183. return acked_features & (1 << bit);
  184. }
  185. void vhost_enable_zcopy(int vq);
  186. #endif