4 #include <linux/eventfd.h>
5 #include <linux/vhost.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>
17 * For transmit, used buffer len is unused; we override it to track buffer
18 * status internally; used for zerocopy tx only.
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
25 #define VHOST_DMA_CLEAR_LEN 0
30 typedef void (*vhost_work_fn_t)(struct vhost_work *work);
33 struct list_head node;
35 wait_queue_head_t done;
41 /* Poll a file (eventfd or socket) */
42 /* Note: there's nothing vhost specific about this structure. */
45 wait_queue_head_t *wqh;
47 struct vhost_work work;
49 struct vhost_dev *dev;
52 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
53 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
55 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
56 unsigned long mask, struct vhost_dev *dev);
57 void vhost_poll_start(struct vhost_poll *poll, struct file *file);
58 void vhost_poll_stop(struct vhost_poll *poll);
59 void vhost_poll_flush(struct vhost_poll *poll);
60 void vhost_poll_queue(struct vhost_poll *poll);
67 struct vhost_virtqueue;
69 struct vhost_ubuf_ref {
71 wait_queue_head_t wait;
72 struct vhost_virtqueue *vq;
75 struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy);
76 void vhost_ubuf_put(struct vhost_ubuf_ref *);
77 void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *);
79 /* The virtqueue structure describes a queue attached to a device. */
80 struct vhost_virtqueue {
81 struct vhost_dev *dev;
83 /* The actual ring of buffers. */
86 struct vring_desc __user *desc;
87 struct vring_avail __user *avail;
88 struct vring_used __user *used;
92 struct eventfd_ctx *call_ctx;
93 struct eventfd_ctx *error_ctx;
94 struct eventfd_ctx *log_ctx;
96 struct vhost_poll poll;
98 /* The routine to call when the Guest pings us, or timeout. */
99 vhost_work_fn_t handle_kick;
101 /* Last available index we saw. */
104 /* Caches available index value from user. */
107 /* Last index we used. */
113 /* Last used index value we have signalled on */
116 /* Last used index value we have signalled on */
117 bool signalled_used_valid;
119 /* Log writes to used structure. */
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;
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 */
146 /* first used idx for DMA done zerocopy buffers */
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;
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;
162 unsigned acked_features;
163 struct vhost_virtqueue *vqs;
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;
172 long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
173 long vhost_dev_check_owner(struct vhost_dev *);
174 long vhost_dev_reset_owner(struct vhost_dev *);
175 void vhost_dev_cleanup(struct vhost_dev *, bool locked);
176 long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
177 int vhost_vq_access_ok(struct vhost_virtqueue *vq);
178 int vhost_log_access_ok(struct vhost_dev *);
180 int 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);
184 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
186 int vhost_init_used(struct vhost_virtqueue *);
187 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
188 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
190 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
191 unsigned int id, int len);
192 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
193 struct vring_used_elem *heads, unsigned count);
194 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
195 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
196 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
198 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
199 unsigned int log_num, u64 len);
200 void vhost_zerocopy_callback(struct ubuf_info *, bool);
201 int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq);
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);\
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),
219 static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
221 unsigned acked_features;
223 /* TODO: check that we are running from vhost_worker or dev mutex is
225 acked_features = rcu_dereference_index_check(dev->acked_features, 1);
226 return acked_features & (1 << bit);
229 void vhost_enable_zcopy(int vq);