vhost.h revision 70e4cb9aafb3caa9e5eb25614a5bc40b0ffa9ffd
1#ifndef _VHOST_H
2#define _VHOST_H
3
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/skbuff.h>
11#include <linux/uio.h>
12#include <linux/virtio_config.h>
13#include <linux/virtio_ring.h>
14#include <linux/atomic.h>
15
16/*
17 * For transmit, used buffer len is unused; we override it to track buffer
18 * status internally; used for zerocopy tx only.
19 */
20/* Lower device DMA done */
21#define VHOST_DMA_DONE_LEN	2
22/* Lower device DMA in progress */
23#define VHOST_DMA_IN_PROGRESS	1
24/* Buffer unused */
25#define VHOST_DMA_CLEAR_LEN	0
26
27struct vhost_device;
28
29struct vhost_work;
30typedef void (*vhost_work_fn_t)(struct vhost_work *work);
31
32struct vhost_work {
33	struct list_head	  node;
34	vhost_work_fn_t		  fn;
35	wait_queue_head_t	  done;
36	int			  flushing;
37	unsigned		  queue_seq;
38	unsigned		  done_seq;
39};
40
41/* Poll a file (eventfd or socket) */
42/* Note: there's nothing vhost specific about this structure. */
43struct vhost_poll {
44	poll_table                table;
45	wait_queue_head_t        *wqh;
46	wait_queue_t              wait;
47	struct vhost_work	  work;
48	unsigned long		  mask;
49	struct vhost_dev	 *dev;
50};
51
52void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
53void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
54
55void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
56		     unsigned long mask, struct vhost_dev *dev);
57void vhost_poll_start(struct vhost_poll *poll, struct file *file);
58void vhost_poll_stop(struct vhost_poll *poll);
59void vhost_poll_flush(struct vhost_poll *poll);
60void vhost_poll_queue(struct vhost_poll *poll);
61
62struct vhost_log {
63	u64 addr;
64	u64 len;
65};
66
67struct vhost_virtqueue;
68
69struct vhost_ubuf_ref {
70	struct kref kref;
71	wait_queue_head_t wait;
72	struct vhost_virtqueue *vq;
73};
74
75struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy);
76void vhost_ubuf_put(struct vhost_ubuf_ref *);
77void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *);
78
79/* The virtqueue structure describes a queue attached to a device. */
80struct vhost_virtqueue {
81	struct vhost_dev *dev;
82
83	/* The actual ring of buffers. */
84	struct mutex mutex;
85	unsigned int num;
86	struct vring_desc __user *desc;
87	struct vring_avail __user *avail;
88	struct vring_used __user *used;
89	struct file *kick;
90	struct file *call;
91	struct file *error;
92	struct eventfd_ctx *call_ctx;
93	struct eventfd_ctx *error_ctx;
94	struct eventfd_ctx *log_ctx;
95
96	struct vhost_poll poll;
97
98	/* The routine to call when the Guest pings us, or timeout. */
99	vhost_work_fn_t handle_kick;
100
101	/* Last available index we saw. */
102	u16 last_avail_idx;
103
104	/* Caches available index value from user. */
105	u16 avail_idx;
106
107	/* Last index we used. */
108	u16 last_used_idx;
109
110	/* Used flags */
111	u16 used_flags;
112
113	/* Last used index value we have signalled on */
114	u16 signalled_used;
115
116	/* Last used index value we have signalled on */
117	bool signalled_used_valid;
118
119	/* Log writes to used structure. */
120	bool log_used;
121	u64 log_addr;
122
123	struct iovec iov[UIO_MAXIOV];
124	/* hdr is used to store the virtio header.
125	 * Since each iovec has >= 1 byte length, we never need more than
126	 * header length entries to store the header. */
127	struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
128	struct iovec *indirect;
129	size_t vhost_hlen;
130	size_t sock_hlen;
131	struct vring_used_elem *heads;
132	/* We use a kind of RCU to access private pointer.
133	 * All readers access it from worker, which makes it possible to
134	 * flush the vhost_work instead of synchronize_rcu. Therefore readers do
135	 * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
136	 * vhost_work execution acts instead of rcu_read_lock() and the end of
137	 * vhost_work execution acts instead of rcu_read_unlock().
138	 * Writers use virtqueue mutex. */
139	void __rcu *private_data;
140	/* Log write descriptors */
141	void __user *log_base;
142	struct vhost_log *log;
143	/* vhost zerocopy support fields below: */
144	/* last used idx for outstanding DMA zerocopy buffers */
145	int upend_idx;
146	/* first used idx for DMA done zerocopy buffers */
147	int done_idx;
148	/* an array of userspace buffers info */
149	struct ubuf_info *ubuf_info;
150	/* Reference counting for outstanding ubufs.
151	 * Protected by vq mutex. Writers must also take device mutex. */
152	struct vhost_ubuf_ref *ubufs;
153};
154
155struct vhost_dev {
156	/* Readers use RCU to access memory table pointer
157	 * log base pointer and features.
158	 * Writers use mutex below.*/
159	struct vhost_memory __rcu *memory;
160	struct mm_struct *mm;
161	struct mutex mutex;
162	unsigned acked_features;
163	struct vhost_virtqueue *vqs;
164	int nvqs;
165	struct file *log_file;
166	struct eventfd_ctx *log_ctx;
167	spinlock_t work_lock;
168	struct list_head work_list;
169	struct task_struct *worker;
170};
171
172long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
173long vhost_dev_check_owner(struct vhost_dev *);
174long vhost_dev_reset_owner(struct vhost_dev *);
175void vhost_dev_cleanup(struct vhost_dev *, bool locked);
176long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
177int vhost_vq_access_ok(struct vhost_virtqueue *vq);
178int vhost_log_access_ok(struct vhost_dev *);
179
180int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
181		      struct iovec iov[], unsigned int iov_count,
182		      unsigned int *out_num, unsigned int *in_num,
183		      struct vhost_log *log, unsigned int *log_num);
184void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
185
186int vhost_init_used(struct vhost_virtqueue *);
187int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
188int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
189		     unsigned count);
190void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
191			       unsigned int id, int len);
192void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
193			       struct vring_used_elem *heads, unsigned count);
194void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
195void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
196bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
197
198int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
199		    unsigned int log_num, u64 len);
200void vhost_zerocopy_callback(struct ubuf_info *, bool);
201int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq);
202
203#define vq_err(vq, fmt, ...) do {                                  \
204		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
205		if ((vq)->error_ctx)                               \
206				eventfd_signal((vq)->error_ctx, 1);\
207	} while (0)
208
209enum {
210	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
211			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
212			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
213			 (1ULL << VHOST_F_LOG_ALL),
214	VHOST_NET_FEATURES = VHOST_FEATURES |
215			 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
216			 (1ULL << VIRTIO_NET_F_MRG_RXBUF),
217};
218
219static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
220{
221	unsigned acked_features;
222
223	/* TODO: check that we are running from vhost_worker or dev mutex is
224	 * held? */
225	acked_features = rcu_dereference_index_check(dev->acked_features, 1);
226	return acked_features & (1 << bit);
227}
228
229void vhost_enable_zcopy(int vq);
230
231#endif
232