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);
135
136typedef struct VuDevIface {
137
138 vu_get_features_cb get_features;
139
140 vu_set_features_cb set_features;
141
142
143 vu_get_features_cb get_protocol_features;
144
145 vu_set_features_cb set_protocol_features;
146
147
148 vu_process_msg_cb process_msg;
149
150 vu_queue_set_started_cb queue_set_started;
151} VuDevIface;
152
153typedef void (*vu_queue_handler_cb) (VuDev *dev, int qidx);
154
155typedef struct VuRing {
156 unsigned int num;
157 struct vring_desc *desc;
158 struct vring_avail *avail;
159 struct vring_used *used;
160 uint64_t log_guest_addr;
161 uint32_t flags;
162} VuRing;
163
164typedef struct VuVirtq {
165 VuRing vring;
166
167
168 uint16_t last_avail_idx;
169
170
171 uint16_t shadow_avail_idx;
172
173 uint16_t used_idx;
174
175
176 uint16_t signalled_used;
177
178
179 bool signalled_used_valid;
180
181
182 bool notification;
183
184 int inuse;
185
186 vu_queue_handler_cb handler;
187
188 int call_fd;
189 int kick_fd;
190 int err_fd;
191 unsigned int enable;
192 bool started;
193} VuVirtq;
194
195enum VuWatchCondtion {
196 VU_WATCH_IN = POLLIN,
197 VU_WATCH_OUT = POLLOUT,
198 VU_WATCH_PRI = POLLPRI,
199 VU_WATCH_ERR = POLLERR,
200 VU_WATCH_HUP = POLLHUP,
201};
202
203typedef void (*vu_panic_cb) (VuDev *dev, const char *err);
204typedef void (*vu_watch_cb) (VuDev *dev, int condition, void *data);
205typedef void (*vu_set_watch_cb) (VuDev *dev, int fd, int condition,
206 vu_watch_cb cb, void *data);
207typedef void (*vu_remove_watch_cb) (VuDev *dev, int fd);
208
209struct VuDev {
210 int sock;
211 uint32_t nregions;
212 VuDevRegion regions[VHOST_MEMORY_MAX_NREGIONS];
213 VuVirtq vq[VHOST_MAX_NR_VIRTQUEUE];
214 int log_call_fd;
215 uint64_t log_size;
216 uint8_t *log_table;
217 uint64_t features;
218 uint64_t protocol_features;
219 bool broken;
220
221
222
223 vu_set_watch_cb set_watch;
224
225
226 vu_remove_watch_cb remove_watch;
227
228
229
230 vu_panic_cb panic;
231 const VuDevIface *iface;
232};
233
234typedef struct VuVirtqElement {
235 unsigned int index;
236 unsigned int out_num;
237 unsigned int in_num;
238 struct iovec *in_sg;
239 struct iovec *out_sg;
240} VuVirtqElement;
241
242
243
244
245
246
247
248
249
250
251
252
253void vu_init(VuDev *dev,
254 int socket,
255 vu_panic_cb panic,
256 vu_set_watch_cb set_watch,
257 vu_remove_watch_cb remove_watch,
258 const VuDevIface *iface);
259
260
261
262
263
264
265
266
267void vu_deinit(VuDev *dev);
268
269
270
271
272
273
274
275
276
277bool vu_dispatch(VuDev *dev);
278
279
280
281
282
283
284
285
286void *vu_gpa_to_va(VuDev *dev, uint64_t guest_addr);
287
288
289
290
291
292
293
294
295VuVirtq *vu_get_queue(VuDev *dev, int qidx);
296
297
298
299
300
301
302
303
304
305
306
307void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
308 vu_queue_handler_cb handler);
309
310
311
312
313
314
315
316
317
318
319void vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable);
320
321
322
323
324
325
326
327
328bool vu_queue_enabled(VuDev *dev, VuVirtq *vq);
329
330
331
332
333
334
335
336
337bool vu_queue_empty(VuDev *dev, VuVirtq *vq);
338
339
340
341
342
343
344
345
346void vu_queue_notify(VuDev *dev, VuVirtq *vq);
347
348
349
350
351
352
353
354
355
356void *vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz);
357
358
359
360
361
362
363
364
365
366
367
368
369
370bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num);
371
372
373
374
375
376
377
378
379
380
381
382void vu_queue_fill(VuDev *dev, VuVirtq *vq,
383 const VuVirtqElement *elem,
384 unsigned int len, unsigned int idx);
385
386
387
388
389
390
391
392
393
394
395void vu_queue_push(VuDev *dev, VuVirtq *vq,
396 const VuVirtqElement *elem, unsigned int len);
397
398
399
400
401
402
403
404
405
406
407void vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int num);
408
409
410
411
412
413
414
415
416
417
418
419
420void vu_queue_get_avail_bytes(VuDev *vdev, VuVirtq *vq, unsigned int *in_bytes,
421 unsigned int *out_bytes,
422 unsigned max_in_bytes, unsigned max_out_bytes);
423
424
425
426
427
428
429
430
431
432
433bool vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
434 unsigned int out_bytes);
435
436#endif
437