vhost.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <asm/atomic.h>
  14. struct vhost_device;
  15. struct vhost_work;
  16. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  17. struct vhost_work {
  18. struct list_head node;
  19. vhost_work_fn_t fn;
  20. wait_queue_head_t done;
  21. int flushing;
  22. unsigned queue_seq;
  23. unsigned done_seq;
  24. };
  25. /* Poll a file (eventfd or socket) */
  26. /* Note: there's nothing vhost specific about this structure. */
  27. struct vhost_poll {
  28. poll_table table;
  29. wait_queue_head_t *wqh;
  30. wait_queue_t wait;
  31. struct vhost_work work;
  32. unsigned long mask;
  33. struct vhost_dev *dev;
  34. };
  35. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  36. unsigned long mask, struct vhost_dev *dev);
  37. void vhost_poll_start(struct vhost_poll *poll, struct file *file);
  38. void vhost_poll_stop(struct vhost_poll *poll);
  39. void vhost_poll_flush(struct vhost_poll *poll);
  40. void vhost_poll_queue(struct vhost_poll *poll);
  41. struct vhost_log {
  42. u64 addr;
  43. u64 len;
  44. };
  45. /* The virtqueue structure describes a queue attached to a device. */
  46. struct vhost_virtqueue {
  47. struct vhost_dev *dev;
  48. /* The actual ring of buffers. */
  49. struct mutex mutex;
  50. unsigned int num;
  51. struct vring_desc __user *desc;
  52. struct vring_avail __user *avail;
  53. struct vring_used __user *used;
  54. struct file *kick;
  55. struct file *call;
  56. struct file *error;
  57. struct eventfd_ctx *call_ctx;
  58. struct eventfd_ctx *error_ctx;
  59. struct eventfd_ctx *log_ctx;
  60. struct vhost_poll poll;
  61. /* The routine to call when the Guest pings us, or timeout. */
  62. vhost_work_fn_t handle_kick;
  63. /* Last available index we saw. */
  64. u16 last_avail_idx;
  65. /* Caches available index value from user. */
  66. u16 avail_idx;
  67. /* Last index we used. */
  68. u16 last_used_idx;
  69. /* Used flags */
  70. u16 used_flags;
  71. /* Last used index value we have signalled on */
  72. u16 signalled_used;
  73. /* Last used index value we have signalled on */
  74. bool signalled_used_valid;
  75. /* Log writes to used structure. */
  76. bool log_used;
  77. u64 log_addr;
  78. struct iovec iov[UIO_MAXIOV];
  79. /* hdr is used to store the virtio header.
  80. * Since each iovec has >= 1 byte length, we never need more than
  81. * header length entries to store the header. */
  82. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  83. struct iovec *indirect;
  84. size_t vhost_hlen;
  85. size_t sock_hlen;
  86. struct vring_used_elem *heads;
  87. /* We use a kind of RCU to access private pointer.
  88. * All readers access it from worker, which makes it possible to
  89. * flush the vhost_work instead of synchronize_rcu. Therefore readers do
  90. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  91. * vhost_work execution acts instead of rcu_read_lock() and the end of
  92. * vhost_work execution acts instead of rcu_read_unlock().
  93. * Writers use virtqueue mutex. */
  94. void __rcu *private_data;
  95. /* Log write descriptors */
  96. void __user *log_base;
  97. struct vhost_log *log;
  98. };
  99. struct vhost_dev {
  100. /* Readers use RCU to access memory table pointer
  101. * log base pointer and features.
  102. * Writers use mutex below.*/
  103. struct vhost_memory __rcu *memory;
  104. struct mm_struct *mm;
  105. struct mutex mutex;
  106. unsigned acked_features;
  107. struct vhost_virtqueue *vqs;
  108. int nvqs;
  109. struct file *log_file;
  110. struct eventfd_ctx *log_ctx;
  111. spinlock_t work_lock;
  112. struct list_head work_list;
  113. struct task_struct *worker;
  114. };
  115. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
  116. long vhost_dev_check_owner(struct vhost_dev *);
  117. long vhost_dev_reset_owner(struct vhost_dev *);
  118. void vhost_dev_cleanup(struct vhost_dev *);
  119. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
  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_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  128. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  129. unsigned count);
  130. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  131. unsigned int id, int len);
  132. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  133. struct vring_used_elem *heads, unsigned count);
  134. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  135. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  136. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  137. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  138. unsigned int log_num, u64 len);
  139. #define vq_err(vq, fmt, ...) do { \
  140. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  141. if ((vq)->error_ctx) \
  142. eventfd_signal((vq)->error_ctx, 1);\
  143. } while (0)
  144. enum {
  145. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  146. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  147. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  148. (1ULL << VHOST_F_LOG_ALL) |
  149. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  150. (1ULL << VIRTIO_NET_F_MRG_RXBUF),
  151. };
  152. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  153. {
  154. unsigned acked_features;
  155. /* TODO: check that we are running from vhost_worker or dev mutex is
  156. * held? */
  157. acked_features = rcu_dereference_index_check(dev->acked_features, 1);
  158. return acked_features & (1 << bit);
  159. }
  160. #endif