vhost.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  40. unsigned long mask, struct vhost_dev *dev);
  41. void vhost_poll_start(struct vhost_poll *poll, struct file *file);
  42. void vhost_poll_stop(struct vhost_poll *poll);
  43. void vhost_poll_flush(struct vhost_poll *poll);
  44. void vhost_poll_queue(struct vhost_poll *poll);
  45. struct vhost_log {
  46. u64 addr;
  47. u64 len;
  48. };
  49. struct vhost_virtqueue;
  50. struct vhost_ubuf_ref {
  51. struct kref kref;
  52. wait_queue_head_t wait;
  53. struct vhost_virtqueue *vq;
  54. };
  55. struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy);
  56. void vhost_ubuf_put(struct vhost_ubuf_ref *);
  57. void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *);
  58. /* The virtqueue structure describes a queue attached to a device. */
  59. struct vhost_virtqueue {
  60. struct vhost_dev *dev;
  61. /* The actual ring of buffers. */
  62. struct mutex mutex;
  63. unsigned int num;
  64. struct vring_desc __user *desc;
  65. struct vring_avail __user *avail;
  66. struct vring_used __user *used;
  67. struct file *kick;
  68. struct file *call;
  69. struct file *error;
  70. struct eventfd_ctx *call_ctx;
  71. struct eventfd_ctx *error_ctx;
  72. struct eventfd_ctx *log_ctx;
  73. struct vhost_poll poll;
  74. /* The routine to call when the Guest pings us, or timeout. */
  75. vhost_work_fn_t handle_kick;
  76. /* Last available index we saw. */
  77. u16 last_avail_idx;
  78. /* Caches available index value from user. */
  79. u16 avail_idx;
  80. /* Last index we used. */
  81. u16 last_used_idx;
  82. /* Used flags */
  83. u16 used_flags;
  84. /* Last used index value we have signalled on */
  85. u16 signalled_used;
  86. /* Last used index value we have signalled on */
  87. bool signalled_used_valid;
  88. /* Log writes to used structure. */
  89. bool log_used;
  90. u64 log_addr;
  91. struct iovec iov[UIO_MAXIOV];
  92. /* hdr is used to store the virtio header.
  93. * Since each iovec has >= 1 byte length, we never need more than
  94. * header length entries to store the header. */
  95. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  96. struct iovec *indirect;
  97. size_t vhost_hlen;
  98. size_t sock_hlen;
  99. struct vring_used_elem *heads;
  100. /* We use a kind of RCU to access private pointer.
  101. * All readers access it from worker, which makes it possible to
  102. * flush the vhost_work instead of synchronize_rcu. Therefore readers do
  103. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  104. * vhost_work execution acts instead of rcu_read_lock() and the end of
  105. * vhost_work execution acts instead of rcu_read_unlock().
  106. * Writers use virtqueue mutex. */
  107. void __rcu *private_data;
  108. /* Log write descriptors */
  109. void __user *log_base;
  110. struct vhost_log *log;
  111. /* vhost zerocopy support fields below: */
  112. /* last used idx for outstanding DMA zerocopy buffers */
  113. int upend_idx;
  114. /* first used idx for DMA done zerocopy buffers */
  115. int done_idx;
  116. /* an array of userspace buffers info */
  117. struct ubuf_info *ubuf_info;
  118. /* Reference counting for outstanding ubufs.
  119. * Protected by vq mutex. Writers must also take device mutex. */
  120. struct vhost_ubuf_ref *ubufs;
  121. };
  122. struct vhost_dev {
  123. /* Readers use RCU to access memory table pointer
  124. * log base pointer and features.
  125. * Writers use mutex below.*/
  126. struct vhost_memory __rcu *memory;
  127. struct mm_struct *mm;
  128. struct mutex mutex;
  129. unsigned acked_features;
  130. struct vhost_virtqueue *vqs;
  131. int nvqs;
  132. struct file *log_file;
  133. struct eventfd_ctx *log_ctx;
  134. spinlock_t work_lock;
  135. struct list_head work_list;
  136. struct task_struct *worker;
  137. };
  138. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
  139. long vhost_dev_check_owner(struct vhost_dev *);
  140. long vhost_dev_reset_owner(struct vhost_dev *);
  141. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  142. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
  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. void vhost_zerocopy_callback(struct ubuf_info *);
  164. int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq);
  165. #define vq_err(vq, fmt, ...) do { \
  166. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  167. if ((vq)->error_ctx) \
  168. eventfd_signal((vq)->error_ctx, 1);\
  169. } while (0)
  170. enum {
  171. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  172. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  173. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  174. (1ULL << VHOST_F_LOG_ALL) |
  175. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  176. (1ULL << VIRTIO_NET_F_MRG_RXBUF),
  177. };
  178. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  179. {
  180. unsigned acked_features;
  181. /* TODO: check that we are running from vhost_worker or dev mutex is
  182. * held? */
  183. acked_features = rcu_dereference_index_check(dev->acked_features, 1);
  184. return acked_features & (1 << bit);
  185. }
  186. void vhost_enable_zcopy(int vq);
  187. #endif