/drivers/vhost/vhost.h

http://github.com/mirrors/linux · C Header · 313 lines · 236 code · 42 blank · 35 comment · 3 complexity · 592bfa1a9331ca9749ae8a9b5e05b37a MD5 · raw file

  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _VHOST_H
  3. #define _VHOST_H
  4. #include <linux/eventfd.h>
  5. #include <linux/vhost.h>
  6. #include <linux/mm.h>
  7. #include <linux/mutex.h>
  8. #include <linux/poll.h>
  9. #include <linux/file.h>
  10. #include <linux/uio.h>
  11. #include <linux/virtio_config.h>
  12. #include <linux/virtio_ring.h>
  13. #include <linux/atomic.h>
  14. #include <linux/vhost_iotlb.h>
  15. struct vhost_work;
  16. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  17. #define VHOST_WORK_QUEUED 1
  18. struct vhost_work {
  19. struct llist_node node;
  20. vhost_work_fn_t fn;
  21. unsigned long flags;
  22. };
  23. /* Poll a file (eventfd or socket) */
  24. /* Note: there's nothing vhost specific about this structure. */
  25. struct vhost_poll {
  26. poll_table table;
  27. wait_queue_head_t *wqh;
  28. wait_queue_entry_t wait;
  29. struct vhost_work work;
  30. __poll_t mask;
  31. struct vhost_dev *dev;
  32. };
  33. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  34. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  35. bool vhost_has_work(struct vhost_dev *dev);
  36. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  37. __poll_t 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, unsigned int ioctl, void __user *argp);
  44. struct vhost_log {
  45. u64 addr;
  46. u64 len;
  47. };
  48. enum vhost_uaddr_type {
  49. VHOST_ADDR_DESC = 0,
  50. VHOST_ADDR_AVAIL = 1,
  51. VHOST_ADDR_USED = 2,
  52. VHOST_NUM_ADDRS = 3,
  53. };
  54. /* The virtqueue structure describes a queue attached to a device. */
  55. struct vhost_virtqueue {
  56. struct vhost_dev *dev;
  57. /* The actual ring of buffers. */
  58. struct mutex mutex;
  59. unsigned int num;
  60. struct vring_desc __user *desc;
  61. struct vring_avail __user *avail;
  62. struct vring_used __user *used;
  63. const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
  64. struct file *kick;
  65. struct eventfd_ctx *call_ctx;
  66. struct eventfd_ctx *error_ctx;
  67. struct eventfd_ctx *log_ctx;
  68. struct vhost_poll poll;
  69. /* The routine to call when the Guest pings us, or timeout. */
  70. vhost_work_fn_t handle_kick;
  71. /* Last available index we saw. */
  72. u16 last_avail_idx;
  73. /* Caches available index value from user. */
  74. u16 avail_idx;
  75. /* Last index we used. */
  76. u16 last_used_idx;
  77. /* Used flags */
  78. u16 used_flags;
  79. /* Last used index value we have signalled on */
  80. u16 signalled_used;
  81. /* Last used index value we have signalled on */
  82. bool signalled_used_valid;
  83. /* Log writes to used structure. */
  84. bool log_used;
  85. u64 log_addr;
  86. struct iovec iov[UIO_MAXIOV];
  87. struct iovec iotlb_iov[64];
  88. struct iovec *indirect;
  89. struct vring_used_elem *heads;
  90. /* Protected by virtqueue mutex. */
  91. struct vhost_iotlb *umem;
  92. struct vhost_iotlb *iotlb;
  93. void *private_data;
  94. u64 acked_features;
  95. u64 acked_backend_features;
  96. /* Log write descriptors */
  97. void __user *log_base;
  98. struct vhost_log *log;
  99. /* Ring endianness. Defaults to legacy native endianness.
  100. * Set to true when starting a modern virtio device. */
  101. bool is_le;
  102. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  103. /* Ring endianness requested by userspace for cross-endian support. */
  104. bool user_be;
  105. #endif
  106. u32 busyloop_timeout;
  107. };
  108. struct vhost_msg_node {
  109. union {
  110. struct vhost_msg msg;
  111. struct vhost_msg_v2 msg_v2;
  112. };
  113. struct vhost_virtqueue *vq;
  114. struct list_head node;
  115. };
  116. struct vhost_dev {
  117. struct mm_struct *mm;
  118. struct mutex mutex;
  119. struct vhost_virtqueue **vqs;
  120. int nvqs;
  121. struct eventfd_ctx *log_ctx;
  122. struct llist_head work_list;
  123. struct task_struct *worker;
  124. struct vhost_iotlb *umem;
  125. struct vhost_iotlb *iotlb;
  126. spinlock_t iotlb_lock;
  127. struct list_head read_list;
  128. struct list_head pending_list;
  129. wait_queue_head_t wait;
  130. int iov_limit;
  131. int weight;
  132. int byte_weight;
  133. u64 kcov_handle;
  134. int (*msg_handler)(struct vhost_dev *dev,
  135. struct vhost_iotlb_msg *msg);
  136. };
  137. bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
  138. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
  139. int nvqs, int iov_limit, int weight, int byte_weight,
  140. int (*msg_handler)(struct vhost_dev *dev,
  141. struct vhost_iotlb_msg *msg));
  142. long vhost_dev_set_owner(struct vhost_dev *dev);
  143. bool vhost_dev_has_owner(struct vhost_dev *dev);
  144. long vhost_dev_check_owner(struct vhost_dev *);
  145. struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
  146. void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
  147. void vhost_dev_cleanup(struct vhost_dev *);
  148. void vhost_dev_stop(struct vhost_dev *);
  149. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  150. long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
  151. bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
  152. bool vhost_log_access_ok(struct vhost_dev *);
  153. int vhost_get_vq_desc(struct vhost_virtqueue *,
  154. struct iovec iov[], unsigned int iov_count,
  155. unsigned int *out_num, unsigned int *in_num,
  156. struct vhost_log *log, unsigned int *log_num);
  157. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  158. int vhost_vq_init_access(struct vhost_virtqueue *);
  159. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  160. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  161. unsigned count);
  162. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  163. unsigned int id, int len);
  164. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  165. struct vring_used_elem *heads, unsigned count);
  166. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  167. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  168. bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
  169. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  170. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  171. unsigned int log_num, u64 len,
  172. struct iovec *iov, int count);
  173. int vq_meta_prefetch(struct vhost_virtqueue *vq);
  174. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
  175. void vhost_enqueue_msg(struct vhost_dev *dev,
  176. struct list_head *head,
  177. struct vhost_msg_node *node);
  178. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  179. struct list_head *head);
  180. __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  181. poll_table *wait);
  182. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  183. int noblock);
  184. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  185. struct iov_iter *from);
  186. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  187. void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
  188. struct vhost_iotlb_map *map);
  189. #define vq_err(vq, fmt, ...) do { \
  190. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  191. if ((vq)->error_ctx) \
  192. eventfd_signal((vq)->error_ctx, 1);\
  193. } while (0)
  194. enum {
  195. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  196. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  197. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  198. (1ULL << VHOST_F_LOG_ALL) |
  199. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  200. (1ULL << VIRTIO_F_VERSION_1)
  201. };
  202. /**
  203. * vhost_vq_set_backend - Set backend.
  204. *
  205. * @vq Virtqueue.
  206. * @private_data The private data.
  207. *
  208. * Context: Need to call with vq->mutex acquired.
  209. */
  210. static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
  211. void *private_data)
  212. {
  213. vq->private_data = private_data;
  214. }
  215. /**
  216. * vhost_vq_get_backend - Get backend.
  217. *
  218. * @vq Virtqueue.
  219. *
  220. * Context: Need to call with vq->mutex acquired.
  221. * Return: Private data previously set with vhost_vq_set_backend.
  222. */
  223. static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
  224. {
  225. return vq->private_data;
  226. }
  227. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  228. {
  229. return vq->acked_features & (1ULL << bit);
  230. }
  231. static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
  232. {
  233. return vq->acked_backend_features & (1ULL << bit);
  234. }
  235. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  236. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  237. {
  238. return vq->is_le;
  239. }
  240. #else
  241. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  242. {
  243. return virtio_legacy_is_little_endian() || vq->is_le;
  244. }
  245. #endif
  246. /* Memory accessors */
  247. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  248. {
  249. return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
  250. }
  251. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  252. {
  253. return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
  254. }
  255. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  256. {
  257. return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
  258. }
  259. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  260. {
  261. return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
  262. }
  263. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  264. {
  265. return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
  266. }
  267. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  268. {
  269. return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
  270. }
  271. #endif