vhost.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/workqueue.h>
  8. #include <linux/poll.h>
  9. #include <linux/file.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/uio.h>
  12. #include <linux/virtio_config.h>
  13. #include <linux/virtio_ring.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. /* Poll a file (eventfd or socket) */
  20. /* Note: there's nothing vhost specific about this structure. */
  21. struct vhost_poll {
  22. poll_table table;
  23. wait_queue_head_t *wqh;
  24. wait_queue_t wait;
  25. /* struct which will handle all actual work. */
  26. struct work_struct work;
  27. unsigned long mask;
  28. };
  29. void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
  30. unsigned long mask);
  31. void vhost_poll_start(struct vhost_poll *poll, struct file *file);
  32. void vhost_poll_stop(struct vhost_poll *poll);
  33. void vhost_poll_flush(struct vhost_poll *poll);
  34. void vhost_poll_queue(struct vhost_poll *poll);
  35. struct vhost_log {
  36. u64 addr;
  37. u64 len;
  38. };
  39. /* The virtqueue structure describes a queue attached to a device. */
  40. struct vhost_virtqueue {
  41. struct vhost_dev *dev;
  42. /* The actual ring of buffers. */
  43. struct mutex mutex;
  44. unsigned int num;
  45. struct vring_desc __user *desc;
  46. struct vring_avail __user *avail;
  47. struct vring_used __user *used;
  48. struct file *kick;
  49. struct file *call;
  50. struct file *error;
  51. struct eventfd_ctx *call_ctx;
  52. struct eventfd_ctx *error_ctx;
  53. struct eventfd_ctx *log_ctx;
  54. struct vhost_poll poll;
  55. /* The routine to call when the Guest pings us, or timeout. */
  56. work_func_t handle_kick;
  57. /* Last available index we saw. */
  58. u16 last_avail_idx;
  59. /* Caches available index value from user. */
  60. u16 avail_idx;
  61. /* Last index we used. */
  62. u16 last_used_idx;
  63. /* Used flags */
  64. u16 used_flags;
  65. /* Log writes to used structure. */
  66. bool log_used;
  67. u64 log_addr;
  68. struct iovec indirect[VHOST_NET_MAX_SG];
  69. struct iovec iov[VHOST_NET_MAX_SG];
  70. struct iovec hdr[VHOST_NET_MAX_SG];
  71. size_t hdr_size;
  72. /* We use a kind of RCU to access private pointer.
  73. * All readers access it from workqueue, which makes it possible to
  74. * flush the workqueue instead of synchronize_rcu. Therefore readers do
  75. * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
  76. * work item execution acts instead of rcu_read_lock() and the end of
  77. * work item execution acts instead of rcu_read_lock().
  78. * Writers use virtqueue mutex. */
  79. void *private_data;
  80. /* Log write descriptors */
  81. void __user *log_base;
  82. struct vhost_log log[VHOST_NET_MAX_SG];
  83. };
  84. struct vhost_dev {
  85. /* Readers use RCU to access memory table pointer
  86. * log base pointer and features.
  87. * Writers use mutex below.*/
  88. struct vhost_memory *memory;
  89. struct mm_struct *mm;
  90. struct mutex mutex;
  91. unsigned acked_features;
  92. struct vhost_virtqueue *vqs;
  93. int nvqs;
  94. struct file *log_file;
  95. struct eventfd_ctx *log_ctx;
  96. };
  97. long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
  98. long vhost_dev_check_owner(struct vhost_dev *);
  99. long vhost_dev_reset_owner(struct vhost_dev *);
  100. void vhost_dev_cleanup(struct vhost_dev *);
  101. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
  102. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  103. int vhost_log_access_ok(struct vhost_dev *);
  104. unsigned vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
  105. struct iovec iov[], unsigned int iov_count,
  106. unsigned int *out_num, unsigned int *in_num,
  107. struct vhost_log *log, unsigned int *log_num);
  108. void vhost_discard_vq_desc(struct vhost_virtqueue *);
  109. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  110. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  111. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  112. unsigned int head, int len);
  113. void vhost_disable_notify(struct vhost_virtqueue *);
  114. bool vhost_enable_notify(struct vhost_virtqueue *);
  115. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  116. unsigned int log_num, u64 len);
  117. int vhost_init(void);
  118. void vhost_cleanup(void);
  119. #define vq_err(vq, fmt, ...) do { \
  120. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  121. if ((vq)->error_ctx) \
  122. eventfd_signal((vq)->error_ctx, 1);\
  123. } while (0)
  124. enum {
  125. VHOST_FEATURES = (1 << VIRTIO_F_NOTIFY_ON_EMPTY) |
  126. (1 << VIRTIO_RING_F_INDIRECT_DESC) |
  127. (1 << VHOST_F_LOG_ALL) |
  128. (1 << VHOST_NET_F_VIRTIO_NET_HDR),
  129. };
  130. static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
  131. {
  132. unsigned acked_features = rcu_dereference(dev->acked_features);
  133. return acked_features & (1 << bit);
  134. }
  135. #endif