vhost.h 5.6 KB

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