vhost.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* Log writes to used structure. */
  72. bool log_used;
  73. u64 log_addr;
  74. struct iovec iov[UIO_MAXIOV];
  75. /* hdr is used to store the virtio header.
  76. * Since each iovec has >= 1 byte length, we never need more than
  77. * header length entries to store the header. */
  78. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  79. struct iovec *indirect;
  80. size_t vhost_hlen;
  81. size_t sock_hlen;
  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_check_owner(struct vhost_dev *);
  113. long vhost_dev_reset_owner(struct vhost_dev *);
  114. void vhost_dev_cleanup(struct vhost_dev *);
  115. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
  116. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  117. int vhost_log_access_ok(struct vhost_dev *);
  118. int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
  119. struct iovec iov[], unsigned int iov_count,
  120. unsigned int *out_num, unsigned int *in_num,
  121. struct vhost_log *log, unsigned int *log_num);
  122. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  123. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  124. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  125. unsigned count);
  126. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  127. unsigned int id, int len);
  128. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  129. struct vring_used_elem *heads, unsigned count);
  130. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  131. void vhost_disable_notify(struct vhost_virtqueue *);
  132. bool vhost_enable_notify(struct vhost_virtqueue *);
  133. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  134. unsigned int log_num, u64 len);
  135. #define vq_err(vq, fmt, ...) do { \
  136. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  137. if ((vq)->error_ctx) \
  138. eventfd_signal((vq)->error_ctx, 1);\
  139. } while (0)
  140. enum {
  141. VHOST_FEATURES = (1 << VIRTIO_F_NOTIFY_ON_EMPTY) |
  142. (1 << VIRTIO_RING_F_INDIRECT_DESC) |
  143. (1 << VHOST_F_LOG_ALL) |
  144. (1 << VHOST_NET_F_VIRTIO_NET_HDR) |
  145. (1 << VIRTIO_NET_F_MRG_RXBUF),
  146. };
  147. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  148. {
  149. unsigned acked_features;
  150. /* TODO: check that we are running from vhost_worker or dev mutex is
  151. * held? */
  152. acked_features = rcu_dereference_index_check(dev->acked_features, 1);
  153. return acked_features & (1 << bit);
  154. }
  155. #endif