1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef LIBVHOST_USER_H
15#define LIBVHOST_USER_H
16
17#include <stdint.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <sys/poll.h>
21#include <linux/vhost.h>
22#include "standard-headers/linux/virtio_ring.h"
23
24
25#define VHOST_USER_F_PROTOCOL_FEATURES 30
26#define VHOST_LOG_PAGE 4096
27
28#define VHOST_MAX_NR_VIRTQUEUE 8
29#define VIRTQUEUE_MAX_SIZE 1024
30
31#define VHOST_MEMORY_MAX_NREGIONS 8
32
33enum VhostUserProtocolFeature {
34 VHOST_USER_PROTOCOL_F_MQ = 0,
35 VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
36 VHOST_USER_PROTOCOL_F_RARP = 2,
37
38 VHOST_USER_PROTOCOL_F_MAX
39};
40
41#define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
42
43typedef enum VhostUserRequest {
44 VHOST_USER_NONE = 0,
45 VHOST_USER_GET_FEATURES = 1,
46 VHOST_USER_SET_FEATURES = 2,
47 VHOST_USER_SET_OWNER = 3,
48 VHOST_USER_RESET_OWNER = 4,
49 VHOST_USER_SET_MEM_TABLE = 5,
50 VHOST_USER_SET_LOG_BASE = 6,
51 VHOST_USER_SET_LOG_FD = 7,
52 VHOST_USER_SET_VRING_NUM = 8,
53 VHOST_USER_SET_VRING_ADDR = 9,
54 VHOST_USER_SET_VRING_BASE = 10,
55 VHOST_USER_GET_VRING_BASE = 11,
56 VHOST_USER_SET_VRING_KICK = 12,
57 VHOST_USER_SET_VRING_CALL = 13,
58 VHOST_USER_SET_VRING_ERR = 14,
59 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
60 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
61 VHOST_USER_GET_QUEUE_NUM = 17,
62 VHOST_USER_SET_VRING_ENABLE = 18,
63 VHOST_USER_SEND_RARP = 19,
64 VHOST_USER_INPUT_GET_CONFIG = 20,
65 VHOST_USER_MAX
66} VhostUserRequest;
67
68typedef struct VhostUserMemoryRegion {
69 uint64_t guest_phys_addr;
70 uint64_t memory_size;
71 uint64_t userspace_addr;
72 uint64_t mmap_offset;
73} VhostUserMemoryRegion;
74
75typedef struct VhostUserMemory {
76 uint32_t nregions;
77 uint32_t padding;
78 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
79} VhostUserMemory;
80
81typedef struct VhostUserLog {
82 uint64_t mmap_size;
83 uint64_t mmap_offset;
84} VhostUserLog;
85
86#if defined(_WIN32)
87# define VU_PACKED __attribute__((gcc_struct, packed))
88#else
89# define VU_PACKED __attribute__((packed))
90#endif
91
92typedef struct VhostUserMsg {
93 VhostUserRequest request;
94
95#define VHOST_USER_VERSION_MASK (0x3)
96#define VHOST_USER_REPLY_MASK (0x1 << 2)
97 uint32_t flags;
98 uint32_t size;
99
100 union {
101#define VHOST_USER_VRING_IDX_MASK (0xff)
102#define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
103 uint64_t u64;
104 struct vhost_vring_state state;
105 struct vhost_vring_addr addr;
106 VhostUserMemory memory;
107 VhostUserLog log;
108 } payload;
109
110 int fds[VHOST_MEMORY_MAX_NREGIONS];
111 int fd_num;
112 uint8_t *data;
113} VU_PACKED VhostUserMsg;
114
115typedef struct VuDevRegion {
116
117 uint64_t gpa;
118
119 uint64_t size;
120
121 uint64_t qva;
122
123 uint64_t mmap_offset;
124
125 uint64_t mmap_addr;
126} VuDevRegion;
127
128typedef struct VuDev VuDev;
129
130typedef uint64_t (*vu_get_features_cb) (VuDev *dev);
131typedef void (*vu_set_features_cb) (VuDev *dev, uint64_t features);
132typedef int (*vu_process_msg_cb) (VuDev *dev, VhostUserMsg *vmsg,
133 int *do_reply);
134typedef void (*vu_queue_set_started_cb) (VuDev *dev, int qidx, bool started);
135typedef bool (*vu_queue_is_processed_in_order_cb) (VuDev *dev, int qidx);
136
137typedef struct VuDevIface {
138
139 vu_get_features_cb get_features;
140
141 vu_set_features_cb set_features;
142
143
144 vu_get_features_cb get_protocol_features;
145
146 vu_set_features_cb set_protocol_features;
147
148
149 vu_process_msg_cb process_msg;
150
151 vu_queue_set_started_cb queue_set_started;
152
153
154
155
156
157 vu_queue_is_processed_in_order_cb queue_is_processed_in_order;
158} VuDevIface;
159
160typedef void (*vu_queue_handler_cb) (VuDev *dev, int qidx);
161
162typedef struct VuRing {
163 unsigned int num;
164 struct vring_desc *desc;
165 struct vring_avail *avail;
166 struct vring_used *used;
167 uint64_t log_guest_addr;
168 uint32_t flags;
169} VuRing;
170
171typedef struct VuVirtq {
172 VuRing vring;
173
174
175 uint16_t last_avail_idx;
176
177
178 uint16_t shadow_avail_idx;
179
180 uint16_t used_idx;
181
182
183 uint16_t signalled_used;
184
185
186 bool signalled_used_valid;
187
188
189 bool notification;
190
191 int inuse;
192
193 vu_queue_handler_cb handler;
194
195 int call_fd;
196 int kick_fd;
197 int err_fd;
198 unsigned int enable;
199 bool started;
200} VuVirtq;
201
202enum VuWatchCondtion {
203 VU_WATCH_IN = POLLIN,
204 VU_WATCH_OUT = POLLOUT,
205 VU_WATCH_PRI = POLLPRI,
206 VU_WATCH_ERR = POLLERR,
207 VU_WATCH_HUP = POLLHUP,
208};
209
210typedef void (*vu_panic_cb) (VuDev *dev, const char *err);
211typedef void (*vu_watch_cb) (VuDev *dev, int condition, void *data);
212typedef void (*vu_set_watch_cb) (VuDev *dev, int fd, int condition,
213 vu_watch_cb cb, void *data);
214typedef void (*vu_remove_watch_cb) (VuDev *dev, int fd);
215
216struct VuDev {
217 int sock;
218 uint32_t nregions;
219 VuDevRegion regions[VHOST_MEMORY_MAX_NREGIONS];
220 VuVirtq vq[VHOST_MAX_NR_VIRTQUEUE];
221 int log_call_fd;
222 uint64_t log_size;
223 uint8_t *log_table;
224 uint64_t features;
225 uint64_t protocol_features;
226 bool broken;
227
228
229
230 vu_set_watch_cb set_watch;
231
232
233 vu_remove_watch_cb remove_watch;
234
235
236
237 vu_panic_cb panic;
238 const VuDevIface *iface;
239};
240
241typedef struct VuVirtqElement {
242 unsigned int index;
243 unsigned int out_num;
244 unsigned int in_num;
245 struct iovec *in_sg;
246 struct iovec *out_sg;
247} VuVirtqElement;
248
249
250
251
252
253
254
255
256
257
258
259
260void vu_init(VuDev *dev,
261 int socket,
262 vu_panic_cb panic,
263 vu_set_watch_cb set_watch,
264 vu_remove_watch_cb remove_watch,
265 const VuDevIface *iface);
266
267
268
269
270
271
272
273
274void vu_deinit(VuDev *dev);
275
276
277
278
279
280
281
282
283
284bool vu_dispatch(VuDev *dev);
285
286
287
288
289
290
291
292
293void *vu_gpa_to_va(VuDev *dev, uint64_t guest_addr);
294
295
296
297
298
299
300
301
302VuVirtq *vu_get_queue(VuDev *dev, int qidx);
303
304
305
306
307
308
309
310
311
312
313
314void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
315 vu_queue_handler_cb handler);
316
317
318
319
320
321
322
323
324
325
326void vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable);
327
328
329
330
331
332
333
334
335bool vu_queue_enabled(VuDev *dev, VuVirtq *vq);
336
337
338
339
340
341
342
343
344bool vu_queue_empty(VuDev *dev, VuVirtq *vq);
345
346
347
348
349
350
351
352
353void vu_queue_notify(VuDev *dev, VuVirtq *vq);
354
355
356
357
358
359
360
361
362
363void *vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz);
364
365
366
367
368
369
370
371
372
373
374
375
376
377bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num);
378
379
380
381
382
383
384
385
386
387
388
389void vu_queue_fill(VuDev *dev, VuVirtq *vq,
390 const VuVirtqElement *elem,
391 unsigned int len, unsigned int idx);
392
393
394
395
396
397
398
399
400
401
402void vu_queue_push(VuDev *dev, VuVirtq *vq,
403 const VuVirtqElement *elem, unsigned int len);
404
405
406
407
408
409
410
411
412
413
414void vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int num);
415
416
417
418
419
420
421
422
423
424
425
426
427void vu_queue_get_avail_bytes(VuDev *vdev, VuVirtq *vq, unsigned int *in_bytes,
428 unsigned int *out_bytes,
429 unsigned max_in_bytes, unsigned max_out_bytes);
430
431
432
433
434
435
436
437
438
439
440bool vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
441 unsigned int out_bytes);
442
443#endif
444