vhost.h 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#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/uio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>
A
Arun Sharma 已提交
13
#include <linux/atomic.h>
14

15 16 17 18 19 20 21 22 23 24 25 26
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;
};

27 28 29 30 31 32
/* 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;
33
	struct vhost_work	  work;
34
	unsigned long		  mask;
35
	struct vhost_dev	 *dev;
36 37
};

38 39 40
void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);

41 42
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
		     unsigned long mask, struct vhost_dev *dev);
43
int vhost_poll_start(struct vhost_poll *poll, struct file *file);
44 45 46
void vhost_poll_stop(struct vhost_poll *poll);
void vhost_poll_flush(struct vhost_poll *poll);
void vhost_poll_queue(struct vhost_poll *poll);
A
Asias He 已提交
47 48
void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

struct vhost_log {
	u64 addr;
	u64 len;
};

/* 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. */
75
	vhost_work_fn_t handle_kick;
76 77 78 79 80 81 82 83 84 85 86 87 88

	/* 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;

M
Michael S. Tsirkin 已提交
89 90 91 92 93 94
	/* Last used index value we have signalled on */
	u16 signalled_used;

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

95 96 97 98
	/* Log writes to used structure. */
	bool log_used;
	u64 log_addr;

J
Jason Wang 已提交
99 100 101
	struct iovec iov[UIO_MAXIOV];
	struct iovec *indirect;
	struct vring_used_elem *heads;
A
Asias He 已提交
102
	/* Protected by virtqueue mutex. */
103
	struct vhost_memory *memory;
A
Asias He 已提交
104
	void *private_data;
M
Michael S. Tsirkin 已提交
105
	u64 acked_features;
106 107
	/* Log write descriptors */
	void __user *log_base;
J
Jason Wang 已提交
108
	struct vhost_log *log;
109 110 111 112 113 114 115 116

	/* Ring endianness. Defaults to legacy native endianness.
	 * Set to true when starting a modern virtio device. */
	bool is_le;
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
	/* Ring endianness requested by userspace for cross-endian support. */
	bool user_be;
#endif
117 118 119
};

struct vhost_dev {
120
	struct vhost_memory *memory;
121 122
	struct mm_struct *mm;
	struct mutex mutex;
123
	struct vhost_virtqueue **vqs;
124 125 126
	int nvqs;
	struct file *log_file;
	struct eventfd_ctx *log_ctx;
127 128 129
	spinlock_t work_lock;
	struct list_head work_list;
	struct task_struct *worker;
130 131
};

Z
Zhi Yong Wu 已提交
132
void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
A
Asias He 已提交
133
long vhost_dev_set_owner(struct vhost_dev *dev);
134
bool vhost_dev_has_owner(struct vhost_dev *dev);
135
long vhost_dev_check_owner(struct vhost_dev *);
136 137
struct vhost_memory *vhost_dev_reset_owner_prepare(void);
void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_memory *);
138
void vhost_dev_cleanup(struct vhost_dev *, bool locked);
139
void vhost_dev_stop(struct vhost_dev *);
140 141
long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
142 143 144
int vhost_vq_access_ok(struct vhost_virtqueue *vq);
int vhost_log_access_ok(struct vhost_dev *);

145
int vhost_get_vq_desc(struct vhost_virtqueue *,
146 147 148
		      struct iovec iov[], unsigned int iov_count,
		      unsigned int *out_num, unsigned int *in_num,
		      struct vhost_log *log, unsigned int *log_num);
149
void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
150

151
int vhost_init_used(struct vhost_virtqueue *);
152
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
153 154
int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
		     unsigned count);
155
void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
156 157 158 159
			       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 *);
M
Michael S. Tsirkin 已提交
160 161
void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
162 163 164 165 166 167 168 169 170 171 172

int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
		    unsigned int log_num, u64 len);

#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 {
M
Michael S. Tsirkin 已提交
173 174 175
	VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
			 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
			 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
176
			 (1ULL << VHOST_F_LOG_ALL),
177 178
};

M
Michael S. Tsirkin 已提交
179
static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
180
{
M
Michael S. Tsirkin 已提交
181
	return vq->acked_features & (1ULL << bit);
182
}
183

184 185
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
{
186
	return vq->is_le;
187 188
}

189 190 191
/* Memory accessors */
static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
{
192
	return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
193 194 195 196
}

static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
{
197
	return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
198 199 200 201
}

static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
{
202
	return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
203 204 205 206
}

static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
{
207
	return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
208 209 210 211
}

static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
{
212
	return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
213 214 215 216
}

static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
{
217
	return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
218
}
219
#endif