vhost.h 6.9 KB

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