vhost.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. enum {
  16. /* Enough place for all fragments, head, and virtio net header. */
  17. VHOST_NET_MAX_SG = MAX_SKB_FRAGS + 2,
  18. };
  19. struct vhost_work;
  20. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  21. struct vhost_work {
  22. struct list_head node;
  23. vhost_work_fn_t fn;
  24. wait_queue_head_t done;
  25. int flushing;
  26. unsigned queue_seq;
  27. unsigned done_seq;
  28. };
  29. /* Poll a file (eventfd or socket) */
  30. /* Note: there's nothing vhost specific about this structure. */
  31. struct vhost_poll {
  32. poll_table table;
  33. wait_queue_head_t *wqh;
  34. wait_queue_t wait;
  35. struct vhost_work work;
  36. unsigned long mask;
  37. struct vhost_dev *dev;
  38. };
  39. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  40. unsigned long mask, struct vhost_dev *dev);
  41. void vhost_poll_start(struct vhost_poll *poll, struct file *file);
  42. void vhost_poll_stop(struct vhost_poll *poll);
  43. void vhost_poll_flush(struct vhost_poll *poll);
  44. void vhost_poll_queue(struct vhost_poll *poll);
  45. struct vhost_log {
  46. u64 addr;
  47. u64 len;
  48. };
  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. /* Log writes to used structure. */
  76. bool log_used;
  77. u64 log_addr;
  78. struct iovec indirect[VHOST_NET_MAX_SG];
  79. struct iovec iov[VHOST_NET_MAX_SG];
  80. struct iovec hdr[VHOST_NET_MAX_SG];
  81. size_t vhost_hlen;
  82. size_t sock_hlen;
  83. struct vring_used_elem heads[VHOST_NET_MAX_SG];
  84. /* We use a kind of RCU to access private pointer.
  85. * All readers access it from worker, which makes it possible to
  86. * flush the vhost_work instead of synchronize_rcu. Therefore readers do
  87. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  88. * vhost_work execution acts instead of rcu_read_lock() and the end of
  89. * vhost_work execution acts instead of rcu_read_lock().
  90. * Writers use virtqueue mutex. */
  91. void *private_data;
  92. /* Log write descriptors */
  93. void __user *log_base;
  94. struct vhost_log log[VHOST_NET_MAX_SG];
  95. };
  96. struct vhost_dev {
  97. /* Readers use RCU to access memory table pointer
  98. * log base pointer and features.
  99. * Writers use mutex below.*/
  100. struct vhost_memory *memory;
  101. struct mm_struct *mm;
  102. struct mutex mutex;
  103. unsigned acked_features;
  104. struct vhost_virtqueue *vqs;
  105. int nvqs;
  106. struct file *log_file;
  107. struct eventfd_ctx *log_ctx;
  108. spinlock_t work_lock;
  109. struct list_head work_list;
  110. struct task_struct *worker;
  111. };
  112. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
  113. long vhost_dev_check_owner(struct vhost_dev *);
  114. long vhost_dev_reset_owner(struct vhost_dev *);
  115. void vhost_dev_cleanup(struct vhost_dev *);
  116. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
  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_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  125. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  126. unsigned count);
  127. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  128. unsigned int id, int len);
  129. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  130. struct vring_used_elem *heads, unsigned count);
  131. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  132. void vhost_disable_notify(struct vhost_virtqueue *);
  133. bool vhost_enable_notify(struct vhost_virtqueue *);
  134. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  135. unsigned int log_num, u64 len);
  136. #define vq_err(vq, fmt, ...) do { \
  137. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  138. if ((vq)->error_ctx) \
  139. eventfd_signal((vq)->error_ctx, 1);\
  140. } while (0)
  141. enum {
  142. VHOST_FEATURES = (1 << VIRTIO_F_NOTIFY_ON_EMPTY) |
  143. (1 << VIRTIO_RING_F_INDIRECT_DESC) |
  144. (1 << VHOST_F_LOG_ALL) |
  145. (1 << VHOST_NET_F_VIRTIO_NET_HDR) |
  146. (1 << VIRTIO_NET_F_MRG_RXBUF),
  147. };
  148. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  149. {
  150. unsigned acked_features = rcu_dereference(dev->acked_features);
  151. return acked_features & (1 << bit);
  152. }
  153. #endif