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