vhost.h 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef _VHOST_H
#define _VHOST_H

#include <linux/eventfd.h>
#include <linux/vhost.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/file.h>
#include <linux/skbuff.h>
#include <linux/uio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>
14
#include <asm/atomic.h>
15

16 17 18 19 20
/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
 * done */
#define VHOST_DMA_DONE_LEN	1
#define VHOST_DMA_CLEAR_LEN	0

21 22
struct vhost_device;

23 24 25 26 27 28 29 30 31 32 33 34
struct vhost_work;
typedef void (*vhost_work_fn_t)(struct vhost_work *work);

struct vhost_work {
	struct list_head	  node;
	vhost_work_fn_t		  fn;
	wait_queue_head_t	  done;
	int			  flushing;
	unsigned		  queue_seq;
	unsigned		  done_seq;
};

35 36 37 38 39 40
/* Poll a file (eventfd or socket) */
/* Note: there's nothing vhost specific about this structure. */
struct vhost_poll {
	poll_table                table;
	wait_queue_head_t        *wqh;
	wait_queue_t              wait;
41
	struct vhost_work	  work;
42
	unsigned long		  mask;
43
	struct vhost_dev	 *dev;
44 45
};

46 47
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
		     unsigned long mask, struct vhost_dev *dev);
48 49 50 51 52 53 54 55 56 57
void vhost_poll_start(struct vhost_poll *poll, struct file *file);
void vhost_poll_stop(struct vhost_poll *poll);
void vhost_poll_flush(struct vhost_poll *poll);
void vhost_poll_queue(struct vhost_poll *poll);

struct vhost_log {
	u64 addr;
	u64 len;
};

58 59 60 61 62 63 64 65 66 67 68 69
struct vhost_virtqueue;

struct vhost_ubuf_ref {
	struct kref kref;
	wait_queue_head_t wait;
	struct vhost_virtqueue *vq;
};

struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *, bool zcopy);
void vhost_ubuf_put(struct vhost_ubuf_ref *);
void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *);

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* The virtqueue structure describes a queue attached to a device. */
struct vhost_virtqueue {
	struct vhost_dev *dev;

	/* The actual ring of buffers. */
	struct mutex mutex;
	unsigned int num;
	struct vring_desc __user *desc;
	struct vring_avail __user *avail;
	struct vring_used __user *used;
	struct file *kick;
	struct file *call;
	struct file *error;
	struct eventfd_ctx *call_ctx;
	struct eventfd_ctx *error_ctx;
	struct eventfd_ctx *log_ctx;

	struct vhost_poll poll;

	/* The routine to call when the Guest pings us, or timeout. */
90
	vhost_work_fn_t handle_kick;
91 92 93 94 95 96 97 98 99 100 101 102 103

	/* Last available index we saw. */
	u16 last_avail_idx;

	/* Caches available index value from user. */
	u16 avail_idx;

	/* Last index we used. */
	u16 last_used_idx;

	/* Used flags */
	u16 used_flags;

104 105 106 107 108 109
	/* Last used index value we have signalled on */
	u16 signalled_used;

	/* Last used index value we have signalled on */
	bool signalled_used_valid;

110 111 112 113
	/* Log writes to used structure. */
	bool log_used;
	u64 log_addr;

J
Jason Wang 已提交
114 115 116 117 118 119
	struct iovec iov[UIO_MAXIOV];
	/* hdr is used to store the virtio header.
	 * Since each iovec has >= 1 byte length, we never need more than
	 * header length entries to store the header. */
	struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
	struct iovec *indirect;
120 121
	size_t vhost_hlen;
	size_t sock_hlen;
J
Jason Wang 已提交
122
	struct vring_used_elem *heads;
123
	/* We use a kind of RCU to access private pointer.
124 125
	 * All readers access it from worker, which makes it possible to
	 * flush the vhost_work instead of synchronize_rcu. Therefore readers do
126
	 * not need to call rcu_read_lock/rcu_read_unlock: the beginning of
127
	 * vhost_work execution acts instead of rcu_read_lock() and the end of
J
Jason Wang 已提交
128
	 * vhost_work execution acts instead of rcu_read_unlock().
129
	 * Writers use virtqueue mutex. */
130
	void __rcu *private_data;
131 132
	/* Log write descriptors */
	void __user *log_base;
J
Jason Wang 已提交
133
	struct vhost_log *log;
134 135 136 137 138 139 140 141 142 143
	/* vhost zerocopy support fields below: */
	/* last used idx for outstanding DMA zerocopy buffers */
	int upend_idx;
	/* first used idx for DMA done zerocopy buffers */
	int done_idx;
	/* an array of userspace buffers info */
	struct ubuf_info *ubuf_info;
	/* Reference counting for outstanding ubufs.
	 * Protected by vq mutex. Writers must also take device mutex. */
	struct vhost_ubuf_ref *ubufs;
144 145 146 147 148 149
};

struct vhost_dev {
	/* Readers use RCU to access memory table pointer
	 * log base pointer and features.
	 * Writers use mutex below.*/
150
	struct vhost_memory __rcu *memory;
151 152 153 154 155 156 157
	struct mm_struct *mm;
	struct mutex mutex;
	unsigned acked_features;
	struct vhost_virtqueue *vqs;
	int nvqs;
	struct file *log_file;
	struct eventfd_ctx *log_ctx;
158 159 160
	spinlock_t work_lock;
	struct list_head work_list;
	struct task_struct *worker;
161 162 163 164 165 166 167 168 169 170
};

long vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue *vqs, int nvqs);
long vhost_dev_check_owner(struct vhost_dev *);
long vhost_dev_reset_owner(struct vhost_dev *);
void vhost_dev_cleanup(struct vhost_dev *);
long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
int vhost_vq_access_ok(struct vhost_virtqueue *vq);
int vhost_log_access_ok(struct vhost_dev *);

171 172 173 174
int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
		      struct iovec iov[], unsigned int iov_count,
		      unsigned int *out_num, unsigned int *in_num,
		      struct vhost_log *log, unsigned int *log_num);
175
void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
176

177
int vhost_init_used(struct vhost_virtqueue *);
178
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
179 180
int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
		     unsigned count);
181
void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
182 183 184 185
			       unsigned int id, int len);
void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
			       struct vring_used_elem *heads, unsigned count);
void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
186 187
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
188 189 190

int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
		    unsigned int log_num, u64 len);
191 192
void vhost_zerocopy_callback(void *arg);
int vhost_zerocopy_signal_used(struct vhost_virtqueue *vq);
193 194 195 196 197 198 199 200

#define vq_err(vq, fmt, ...) do {                                  \
		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
		if ((vq)->error_ctx)                               \
				eventfd_signal((vq)->error_ctx, 1);\
	} while (0)

enum {
201 202 203 204 205 206
	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
			 (1ULL << VHOST_F_LOG_ALL) |
			 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
			 (1ULL << VIRTIO_NET_F_MRG_RXBUF),
207 208 209 210
};

static inline int vhost_has_feature(struct vhost_dev *dev, int bit)
{
211 212
	unsigned acked_features;

213 214 215
	/* TODO: check that we are running from vhost_worker or dev mutex is
	 * held? */
	acked_features = rcu_dereference_index_check(dev->acked_features, 1);
216 217 218
	return acked_features & (1 << bit);
}

219 220
void vhost_enable_zcopy(int vq);

221
#endif
新手
引导
客服 返回
顶部