vhost.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* The virtqueue structure describes a queue attached to a device. */
  48. struct vhost_virtqueue {
  49. struct vhost_dev *dev;
  50. /* The actual ring of buffers. */
  51. struct mutex mutex;
  52. unsigned int num;
  53. struct vring_desc __user *desc;
  54. struct vring_avail __user *avail;
  55. struct vring_used __user *used;
  56. struct file *kick;
  57. struct file *call;
  58. struct file *error;
  59. struct eventfd_ctx *call_ctx;
  60. struct eventfd_ctx *error_ctx;
  61. struct eventfd_ctx *log_ctx;
  62. struct vhost_poll poll;
  63. /* The routine to call when the Guest pings us, or timeout. */
  64. vhost_work_fn_t handle_kick;
  65. /* Last available index we saw. */
  66. u16 last_avail_idx;
  67. /* Caches available index value from user. */
  68. u16 avail_idx;
  69. /* Last index we used. */
  70. u16 last_used_idx;
  71. /* Used flags */
  72. u16 used_flags;
  73. /* Last used index value we have signalled on */
  74. u16 signalled_used;
  75. /* Last used index value we have signalled on */
  76. bool signalled_used_valid;
  77. /* Log writes to used structure. */
  78. bool log_used;
  79. u64 log_addr;
  80. struct iovec iov[UIO_MAXIOV];
  81. struct iovec *indirect;
  82. struct vring_used_elem *heads;
  83. /* We use a kind of RCU to access private pointer.
  84. * All readers access it from worker, which makes it possible to
  85. * flush the vhost_work instead of synchronize_rcu. Therefore readers do
  86. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  87. * vhost_work execution acts instead of rcu_read_lock() and the end of
  88. * vhost_work execution acts instead of rcu_read_unlock().
  89. * Writers use virtqueue mutex. */
  90. void __rcu *private_data;
  91. /* Log write descriptors */
  92. void __user *log_base;
  93. struct vhost_log *log;
  94. };
  95. struct vhost_dev {
  96. /* Readers use RCU to access memory table pointer
  97. * log base pointer and features.
  98. * Writers use mutex below.*/
  99. struct vhost_memory __rcu *memory;
  100. struct mm_struct *mm;
  101. struct mutex mutex;
  102. unsigned acked_features;
  103. struct vhost_virtqueue **vqs;
  104. int nvqs;
  105. struct file *log_file;
  106. struct eventfd_ctx *log_ctx;
  107. spinlock_t work_lock;
  108. struct list_head work_list;
  109. struct task_struct *worker;
  110. };
  111. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
  112. long vhost_dev_set_owner(struct vhost_dev *dev);
  113. long vhost_dev_check_owner(struct vhost_dev *);
  114. struct vhost_memory *vhost_dev_reset_owner_prepare(void);
  115. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_memory *);
  116. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  117. void vhost_dev_stop(struct vhost_dev *);
  118. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  119. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  120. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  121. int vhost_log_access_ok(struct vhost_dev *);
  122. int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
  123. struct iovec iov[], unsigned int iov_count,
  124. unsigned int *out_num, unsigned int *in_num,
  125. struct vhost_log *log, unsigned int *log_num);
  126. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  127. int vhost_init_used(struct vhost_virtqueue *);
  128. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  129. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  130. unsigned count);
  131. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  132. unsigned int id, int len);
  133. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  134. struct vring_used_elem *heads, unsigned count);
  135. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  136. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  137. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  138. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  139. unsigned int log_num, u64 len);
  140. #define vq_err(vq, fmt, ...) do { \
  141. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  142. if ((vq)->error_ctx) \
  143. eventfd_signal((vq)->error_ctx, 1);\
  144. } while (0)
  145. enum {
  146. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  147. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  148. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  149. (1ULL << VHOST_F_LOG_ALL),
  150. };
  151. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  152. {
  153. unsigned acked_features;
  154. /* TODO: check that we are running from vhost_worker or dev mutex is
  155. * held? */
  156. acked_features = rcu_dereference_index_check(dev->acked_features, 1);
  157. return acked_features & (1 << bit);
  158. }
  159. #endif