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
33typedef enum VhostSetConfigType {
34 VHOST_SET_CONFIG_TYPE_MASTER = 0,
35 VHOST_SET_CONFIG_TYPE_MIGRATION = 1,
36} VhostSetConfigType;
37
38
39
40
41#define VHOST_USER_MAX_CONFIG_SIZE 256
42
43enum VhostUserProtocolFeature {
44 VHOST_USER_PROTOCOL_F_MQ = 0,
45 VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
46 VHOST_USER_PROTOCOL_F_RARP = 2,
47 VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
48 VHOST_USER_PROTOCOL_F_NET_MTU = 4,
49 VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5,
50 VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
51 VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
52 VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
53 VHOST_USER_PROTOCOL_F_CONFIG = 9,
54 VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
55 VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
56
57 VHOST_USER_PROTOCOL_F_MAX
58};
59
60#define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
61
62typedef enum VhostUserRequest {
63 VHOST_USER_NONE = 0,
64 VHOST_USER_GET_FEATURES = 1,
65 VHOST_USER_SET_FEATURES = 2,
66 VHOST_USER_SET_OWNER = 3,
67 VHOST_USER_RESET_OWNER = 4,
68 VHOST_USER_SET_MEM_TABLE = 5,
69 VHOST_USER_SET_LOG_BASE = 6,
70 VHOST_USER_SET_LOG_FD = 7,
71 VHOST_USER_SET_VRING_NUM = 8,
72 VHOST_USER_SET_VRING_ADDR = 9,
73 VHOST_USER_SET_VRING_BASE = 10,
74 VHOST_USER_GET_VRING_BASE = 11,
75 VHOST_USER_SET_VRING_KICK = 12,
76 VHOST_USER_SET_VRING_CALL = 13,
77 VHOST_USER_SET_VRING_ERR = 14,
78 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
79 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
80 VHOST_USER_GET_QUEUE_NUM = 17,
81 VHOST_USER_SET_VRING_ENABLE = 18,
82 VHOST_USER_SEND_RARP = 19,
83 VHOST_USER_NET_SET_MTU = 20,
84 VHOST_USER_SET_SLAVE_REQ_FD = 21,
85 VHOST_USER_IOTLB_MSG = 22,
86 VHOST_USER_SET_VRING_ENDIAN = 23,
87 VHOST_USER_GET_CONFIG = 24,
88 VHOST_USER_SET_CONFIG = 25,
89 VHOST_USER_CREATE_CRYPTO_SESSION = 26,
90 VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
91 VHOST_USER_POSTCOPY_ADVISE = 28,
92 VHOST_USER_POSTCOPY_LISTEN = 29,
93 VHOST_USER_POSTCOPY_END = 30,
94 VHOST_USER_MAX
95} VhostUserRequest;
96
97typedef enum VhostUserSlaveRequest {
98 VHOST_USER_SLAVE_NONE = 0,
99 VHOST_USER_SLAVE_IOTLB_MSG = 1,
100 VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
101 VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
102 VHOST_USER_SLAVE_MAX
103} VhostUserSlaveRequest;
104
105typedef struct VhostUserMemoryRegion {
106 uint64_t guest_phys_addr;
107 uint64_t memory_size;
108 uint64_t userspace_addr;
109 uint64_t mmap_offset;
110} VhostUserMemoryRegion;
111
112typedef struct VhostUserMemory {
113 uint32_t nregions;
114 uint32_t padding;
115 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
116} VhostUserMemory;
117
118typedef struct VhostUserLog {
119 uint64_t mmap_size;
120 uint64_t mmap_offset;
121} VhostUserLog;
122
123typedef struct VhostUserConfig {
124 uint32_t offset;
125 uint32_t size;
126 uint32_t flags;
127 uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
128} VhostUserConfig;
129
130static VhostUserConfig c __attribute__ ((unused));
131#define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
132 + sizeof(c.size) \
133 + sizeof(c.flags))
134
135typedef struct VhostUserVringArea {
136 uint64_t u64;
137 uint64_t size;
138 uint64_t offset;
139} VhostUserVringArea;
140
141#if defined(_WIN32)
142# define VU_PACKED __attribute__((gcc_struct, packed))
143#else
144# define VU_PACKED __attribute__((packed))
145#endif
146
147typedef struct VhostUserMsg {
148 VhostUserRequest request;
149
150#define VHOST_USER_VERSION_MASK (0x3)
151#define VHOST_USER_REPLY_MASK (0x1 << 2)
152#define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
153 uint32_t flags;
154 uint32_t size;
155
156 union {
157#define VHOST_USER_VRING_IDX_MASK (0xff)
158#define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
159 uint64_t u64;
160 struct vhost_vring_state state;
161 struct vhost_vring_addr addr;
162 VhostUserMemory memory;
163 VhostUserLog log;
164 VhostUserConfig config;
165 VhostUserVringArea area;
166 } payload;
167
168 int fds[VHOST_MEMORY_MAX_NREGIONS];
169 int fd_num;
170 uint8_t *data;
171} VU_PACKED VhostUserMsg;
172
173typedef struct VuDevRegion {
174
175 uint64_t gpa;
176
177 uint64_t size;
178
179 uint64_t qva;
180
181 uint64_t mmap_offset;
182
183 uint64_t mmap_addr;
184} VuDevRegion;
185
186typedef struct VuDev VuDev;
187
188typedef uint64_t (*vu_get_features_cb) (VuDev *dev);
189typedef void (*vu_set_features_cb) (VuDev *dev, uint64_t features);
190typedef int (*vu_process_msg_cb) (VuDev *dev, VhostUserMsg *vmsg,
191 int *do_reply);
192typedef void (*vu_queue_set_started_cb) (VuDev *dev, int qidx, bool started);
193typedef bool (*vu_queue_is_processed_in_order_cb) (VuDev *dev, int qidx);
194typedef int (*vu_get_config_cb) (VuDev *dev, uint8_t *config, uint32_t len);
195typedef int (*vu_set_config_cb) (VuDev *dev, const uint8_t *data,
196 uint32_t offset, uint32_t size,
197 uint32_t flags);
198
199typedef struct VuDevIface {
200
201 vu_get_features_cb get_features;
202
203 vu_set_features_cb set_features;
204
205
206 vu_get_features_cb get_protocol_features;
207
208 vu_set_features_cb set_protocol_features;
209
210
211 vu_process_msg_cb process_msg;
212
213 vu_queue_set_started_cb queue_set_started;
214
215
216
217
218
219 vu_queue_is_processed_in_order_cb queue_is_processed_in_order;
220
221 vu_get_config_cb get_config;
222
223 vu_set_config_cb set_config;
224} VuDevIface;
225
226typedef void (*vu_queue_handler_cb) (VuDev *dev, int qidx);
227
228typedef struct VuRing {
229 unsigned int num;
230 struct vring_desc *desc;
231 struct vring_avail *avail;
232 struct vring_used *used;
233 uint64_t log_guest_addr;
234 uint32_t flags;
235} VuRing;
236
237typedef struct VuVirtq {
238 VuRing vring;
239
240
241 uint16_t last_avail_idx;
242
243
244 uint16_t shadow_avail_idx;
245
246 uint16_t used_idx;
247
248
249 uint16_t signalled_used;
250
251
252 bool signalled_used_valid;
253
254
255 bool notification;
256
257 int inuse;
258
259 vu_queue_handler_cb handler;
260
261 int call_fd;
262 int kick_fd;
263 int err_fd;
264 unsigned int enable;
265 bool started;
266} VuVirtq;
267
268enum VuWatchCondtion {
269 VU_WATCH_IN = POLLIN,
270 VU_WATCH_OUT = POLLOUT,
271 VU_WATCH_PRI = POLLPRI,
272 VU_WATCH_ERR = POLLERR,
273 VU_WATCH_HUP = POLLHUP,
274};
275
276typedef void (*vu_panic_cb) (VuDev *dev, const char *err);
277typedef void (*vu_watch_cb) (VuDev *dev, int condition, void *data);
278typedef void (*vu_set_watch_cb) (VuDev *dev, int fd, int condition,
279 vu_watch_cb cb, void *data);
280typedef void (*vu_remove_watch_cb) (VuDev *dev, int fd);
281
282struct VuDev {
283 int sock;
284 uint32_t nregions;
285 VuDevRegion regions[VHOST_MEMORY_MAX_NREGIONS];
286 VuVirtq vq[VHOST_MAX_NR_VIRTQUEUE];
287 int log_call_fd;
288 int slave_fd;
289 uint64_t log_size;
290 uint8_t *log_table;
291 uint64_t features;
292 uint64_t protocol_features;
293 bool broken;
294
295
296
297 vu_set_watch_cb set_watch;
298
299
300 vu_remove_watch_cb remove_watch;
301
302
303
304 vu_panic_cb panic;
305 const VuDevIface *iface;
306
307
308 int postcopy_ufd;
309 bool postcopy_listening;
310};
311
312typedef struct VuVirtqElement {
313 unsigned int index;
314 unsigned int out_num;
315 unsigned int in_num;
316 struct iovec *in_sg;
317 struct iovec *out_sg;
318} VuVirtqElement;
319
320
321
322
323
324
325
326
327
328
329
330
331void vu_init(VuDev *dev,
332 int socket,
333 vu_panic_cb panic,
334 vu_set_watch_cb set_watch,
335 vu_remove_watch_cb remove_watch,
336 const VuDevIface *iface);
337
338
339
340
341
342
343
344
345void vu_deinit(VuDev *dev);
346
347
348
349
350
351
352
353
354
355bool vu_dispatch(VuDev *dev);
356
357
358
359
360
361
362
363
364
365void *vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr);
366
367
368
369
370
371
372
373
374VuVirtq *vu_get_queue(VuDev *dev, int qidx);
375
376
377
378
379
380
381
382
383
384
385
386void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
387 vu_queue_handler_cb handler);
388
389
390
391
392
393
394
395
396
397
398
399
400
401bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
402 int size, int offset);
403
404
405
406
407
408
409
410
411
412void vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable);
413
414
415
416
417
418
419
420
421bool vu_queue_enabled(VuDev *dev, VuVirtq *vq);
422
423
424
425
426
427
428
429
430bool vu_queue_started(const VuDev *dev, const VuVirtq *vq);
431
432
433
434
435
436
437
438
439bool vu_queue_empty(VuDev *dev, VuVirtq *vq);
440
441
442
443
444
445
446
447
448void vu_queue_notify(VuDev *dev, VuVirtq *vq);
449
450
451
452
453
454
455
456
457
458
459void *vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz);
460
461
462
463
464
465
466
467
468
469
470
471
472
473bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num);
474
475
476
477
478
479
480
481
482
483
484
485void vu_queue_fill(VuDev *dev, VuVirtq *vq,
486 const VuVirtqElement *elem,
487 unsigned int len, unsigned int idx);
488
489
490
491
492
493
494
495
496
497
498void vu_queue_push(VuDev *dev, VuVirtq *vq,
499 const VuVirtqElement *elem, unsigned int len);
500
501
502
503
504
505
506
507
508
509
510void vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int num);
511
512
513
514
515
516
517
518
519
520
521
522
523void vu_queue_get_avail_bytes(VuDev *vdev, VuVirtq *vq, unsigned int *in_bytes,
524 unsigned int *out_bytes,
525 unsigned max_in_bytes, unsigned max_out_bytes);
526
527
528
529
530
531
532
533
534
535
536bool vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
537 unsigned int out_bytes);
538
539#endif
540