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