xref: /qemu/subprojects/libvhost-user/libvhost-user.h (revision 8f5a4cfc7ed9e06e07fdd8e8fdf50ef3ea783f63)
1 /*
2  * Vhost User library
3  *
4  * Copyright (c) 2016 Red Hat, Inc.
5  *
6  * Authors:
7  *  Victor Kaplansky <victork@redhat.com>
8  *  Marc-André Lureau <mlureau@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or
11  * later.  See the COPYING file in the top-level directory.
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 <poll.h>
21 #include <linux/vhost.h>
22 #include <pthread.h>
23 #include "standard-headers/linux/virtio_ring.h"
24 
25 /* Based on qemu/hw/virtio/vhost-user.c */
26 #define VHOST_USER_F_PROTOCOL_FEATURES 30
27 #define VHOST_LOG_PAGE 4096
28 
29 #define VIRTQUEUE_MAX_SIZE 1024
30 
31 #define VHOST_MEMORY_BASELINE_NREGIONS 8
32 
33 /*
34  * vhost in the kernel usually supports 509 mem slots. 509 used to be the
35  * KVM limit, it supported 512, but 3 were used for internal purposes. This
36  * limit is sufficient to support many DIMMs and virtio-mem in
37  * "dynamic-memslots" mode.
38  */
39 #define VHOST_USER_MAX_RAM_SLOTS 509
40 
41 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
42 
43 typedef enum VhostSetConfigType {
44     VHOST_SET_CONFIG_TYPE_FRONTEND = 0,
45     VHOST_SET_CONFIG_TYPE_MIGRATION = 1,
46 } VhostSetConfigType;
47 
48 /*
49  * Maximum size of virtio device config space
50  */
51 #define VHOST_USER_MAX_CONFIG_SIZE 256
52 
53 enum VhostUserProtocolFeature {
54     VHOST_USER_PROTOCOL_F_MQ = 0,
55     VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
56     VHOST_USER_PROTOCOL_F_RARP = 2,
57     VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
58     VHOST_USER_PROTOCOL_F_NET_MTU = 4,
59     VHOST_USER_PROTOCOL_F_BACKEND_REQ = 5,
60     VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
61     VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
62     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
63     VHOST_USER_PROTOCOL_F_CONFIG = 9,
64     VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD = 10,
65     VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
66     VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12,
67     VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS = 14,
68     VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15,
69     /* Feature 16 is reserved for VHOST_USER_PROTOCOL_F_STATUS. */
70     /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */
71     VHOST_USER_PROTOCOL_F_SHARED_OBJECT = 18,
72     VHOST_USER_PROTOCOL_F_MAX
73 };
74 
75 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
76 
77 typedef enum VhostUserRequest {
78     VHOST_USER_NONE = 0,
79     VHOST_USER_GET_FEATURES = 1,
80     VHOST_USER_SET_FEATURES = 2,
81     VHOST_USER_SET_OWNER = 3,
82     VHOST_USER_RESET_OWNER = 4,
83     VHOST_USER_SET_MEM_TABLE = 5,
84     VHOST_USER_SET_LOG_BASE = 6,
85     VHOST_USER_SET_LOG_FD = 7,
86     VHOST_USER_SET_VRING_NUM = 8,
87     VHOST_USER_SET_VRING_ADDR = 9,
88     VHOST_USER_SET_VRING_BASE = 10,
89     VHOST_USER_GET_VRING_BASE = 11,
90     VHOST_USER_SET_VRING_KICK = 12,
91     VHOST_USER_SET_VRING_CALL = 13,
92     VHOST_USER_SET_VRING_ERR = 14,
93     VHOST_USER_GET_PROTOCOL_FEATURES = 15,
94     VHOST_USER_SET_PROTOCOL_FEATURES = 16,
95     VHOST_USER_GET_QUEUE_NUM = 17,
96     VHOST_USER_SET_VRING_ENABLE = 18,
97     VHOST_USER_SEND_RARP = 19,
98     VHOST_USER_NET_SET_MTU = 20,
99     VHOST_USER_SET_BACKEND_REQ_FD = 21,
100     VHOST_USER_IOTLB_MSG = 22,
101     VHOST_USER_SET_VRING_ENDIAN = 23,
102     VHOST_USER_GET_CONFIG = 24,
103     VHOST_USER_SET_CONFIG = 25,
104     VHOST_USER_CREATE_CRYPTO_SESSION = 26,
105     VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
106     VHOST_USER_POSTCOPY_ADVISE  = 28,
107     VHOST_USER_POSTCOPY_LISTEN  = 29,
108     VHOST_USER_POSTCOPY_END     = 30,
109     VHOST_USER_GET_INFLIGHT_FD = 31,
110     VHOST_USER_SET_INFLIGHT_FD = 32,
111     VHOST_USER_GPU_SET_SOCKET = 33,
112     VHOST_USER_VRING_KICK = 35,
113     VHOST_USER_GET_MAX_MEM_SLOTS = 36,
114     VHOST_USER_ADD_MEM_REG = 37,
115     VHOST_USER_REM_MEM_REG = 38,
116     VHOST_USER_GET_SHARED_OBJECT = 41,
117     VHOST_USER_MAX
118 } VhostUserRequest;
119 
120 typedef enum VhostUserBackendRequest {
121     VHOST_USER_BACKEND_NONE = 0,
122     VHOST_USER_BACKEND_IOTLB_MSG = 1,
123     VHOST_USER_BACKEND_CONFIG_CHANGE_MSG = 2,
124     VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG = 3,
125     VHOST_USER_BACKEND_VRING_CALL = 4,
126     VHOST_USER_BACKEND_VRING_ERR = 5,
127     VHOST_USER_BACKEND_SHARED_OBJECT_ADD = 6,
128     VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE = 7,
129     VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP = 8,
130     VHOST_USER_BACKEND_MAX
131 }  VhostUserBackendRequest;
132 
133 typedef struct VhostUserMemoryRegion {
134     uint64_t guest_phys_addr;
135     uint64_t memory_size;
136     uint64_t userspace_addr;
137     uint64_t mmap_offset;
138 } VhostUserMemoryRegion;
139 
140 #define VHOST_USER_MEM_REG_SIZE (sizeof(VhostUserMemoryRegion))
141 
142 typedef struct VhostUserMemory {
143     uint32_t nregions;
144     uint32_t padding;
145     VhostUserMemoryRegion regions[VHOST_MEMORY_BASELINE_NREGIONS];
146 } VhostUserMemory;
147 
148 typedef struct VhostUserMemRegMsg {
149     uint64_t padding;
150     VhostUserMemoryRegion region;
151 } VhostUserMemRegMsg;
152 
153 typedef struct VhostUserLog {
154     uint64_t mmap_size;
155     uint64_t mmap_offset;
156 } VhostUserLog;
157 
158 typedef struct VhostUserConfig {
159     uint32_t offset;
160     uint32_t size;
161     uint32_t flags;
162     uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
163 } VhostUserConfig;
164 
165 static VhostUserConfig c __attribute__ ((unused));
166 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
167                                    + sizeof(c.size) \
168                                    + sizeof(c.flags))
169 
170 typedef struct VhostUserVringArea {
171     uint64_t u64;
172     uint64_t size;
173     uint64_t offset;
174 } VhostUserVringArea;
175 
176 typedef struct VhostUserInflight {
177     uint64_t mmap_size;
178     uint64_t mmap_offset;
179     uint16_t num_queues;
180     uint16_t queue_size;
181 } VhostUserInflight;
182 
183 #define UUID_LEN 16
184 
185 typedef struct VhostUserShared {
186     unsigned char uuid[UUID_LEN];
187 } VhostUserShared;
188 
189 #define VU_PACKED __attribute__((packed))
190 
191 typedef struct VhostUserMsg {
192     int request;
193 
194 #define VHOST_USER_VERSION_MASK     (0x3)
195 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
196 #define VHOST_USER_NEED_REPLY_MASK  (0x1 << 3)
197     uint32_t flags;
198     uint32_t size; /* the following payload size */
199 
200     union {
201 #define VHOST_USER_VRING_IDX_MASK   (0xff)
202 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
203         uint64_t u64;
204         struct vhost_vring_state state;
205         struct vhost_vring_addr addr;
206         VhostUserMemory memory;
207         VhostUserMemRegMsg memreg;
208         VhostUserLog log;
209         VhostUserConfig config;
210         VhostUserVringArea area;
211         VhostUserInflight inflight;
212         VhostUserShared object;
213     } payload;
214 
215     int fds[VHOST_MEMORY_BASELINE_NREGIONS];
216     int fd_num;
217     uint8_t *data;
218 } VU_PACKED VhostUserMsg;
219 
220 typedef struct VuDevRegion {
221     /* Guest Physical address. */
222     uint64_t gpa;
223     /* Memory region size. */
224     uint64_t size;
225     /* QEMU virtual address (userspace). */
226     uint64_t qva;
227     /* Starting offset in our mmaped space. */
228     uint64_t mmap_offset;
229     /* Start address of mmaped space. */
230     uint64_t mmap_addr;
231 } VuDevRegion;
232 
233 typedef struct VuDev VuDev;
234 
235 typedef uint64_t (*vu_get_features_cb) (VuDev *dev);
236 typedef void (*vu_set_features_cb) (VuDev *dev, uint64_t features);
237 typedef int (*vu_process_msg_cb) (VuDev *dev, VhostUserMsg *vmsg,
238                                   int *do_reply);
239 typedef bool (*vu_read_msg_cb) (VuDev *dev, int sock, VhostUserMsg *vmsg);
240 typedef void (*vu_queue_set_started_cb) (VuDev *dev, int qidx, bool started);
241 typedef bool (*vu_queue_is_processed_in_order_cb) (VuDev *dev, int qidx);
242 typedef int (*vu_get_config_cb) (VuDev *dev, uint8_t *config, uint32_t len);
243 typedef int (*vu_set_config_cb) (VuDev *dev, const uint8_t *data,
244                                  uint32_t offset, uint32_t size,
245                                  uint32_t flags);
246 typedef int (*vu_get_shared_object_cb) (VuDev *dev, const unsigned char *uuid);
247 
248 typedef struct VuDevIface {
249     /* called by VHOST_USER_GET_FEATURES to get the features bitmask */
250     vu_get_features_cb get_features;
251     /* enable vhost implementation features */
252     vu_set_features_cb set_features;
253     /* get the protocol feature bitmask from the underlying vhost
254      * implementation */
255     vu_get_features_cb get_protocol_features;
256     /* enable protocol features in the underlying vhost implementation. */
257     vu_set_features_cb set_protocol_features;
258     /* process_msg is called for each vhost-user message received */
259     /* skip libvhost-user processing if return value != 0 */
260     vu_process_msg_cb process_msg;
261     /* tells when queues can be processed */
262     vu_queue_set_started_cb queue_set_started;
263     /*
264      * If the queue is processed in order, in which case it will be
265      * resumed to vring.used->idx. This can help to support resuming
266      * on unmanaged exit/crash.
267      */
268     vu_queue_is_processed_in_order_cb queue_is_processed_in_order;
269     /* get the config space of the device */
270     vu_get_config_cb get_config;
271     /* set the config space of the device */
272     vu_set_config_cb set_config;
273     /* get virtio shared object from the underlying vhost implementation. */
274     vu_get_shared_object_cb get_shared_object;
275 } VuDevIface;
276 
277 typedef void (*vu_queue_handler_cb) (VuDev *dev, int qidx);
278 
279 typedef struct VuRing {
280     unsigned int num;
281     struct vring_desc *desc;
282     struct vring_avail *avail;
283     struct vring_used *used;
284     uint64_t log_guest_addr;
285     uint32_t flags;
286 } VuRing;
287 
288 typedef struct VuDescStateSplit {
289     /* Indicate whether this descriptor is inflight or not.
290      * Only available for head-descriptor. */
291     uint8_t inflight;
292 
293     /* Padding */
294     uint8_t padding[5];
295 
296     /* Maintain a list for the last batch of used descriptors.
297      * Only available when batching is used for submitting */
298     uint16_t next;
299 
300     /* Used to preserve the order of fetching available descriptors.
301      * Only available for head-descriptor. */
302     uint64_t counter;
303 } VuDescStateSplit;
304 
305 typedef struct VuVirtqInflight {
306     /* The feature flags of this region. Now it's initialized to 0. */
307     uint64_t features;
308 
309     /* The version of this region. It's 1 currently.
310      * Zero value indicates a vm reset happened. */
311     uint16_t version;
312 
313     /*
314      * The size of VuDescStateSplit array. It's equal to the virtqueue size.
315      * Backend could get it from queue size field of VhostUserInflight.
316      */
317     uint16_t desc_num;
318 
319     /* The head of list that track the last batch of used descriptors. */
320     uint16_t last_batch_head;
321 
322     /* Storing the idx value of used ring */
323     uint16_t used_idx;
324 
325     /* Used to track the state of each descriptor in descriptor table */
326     VuDescStateSplit desc[];
327 } VuVirtqInflight;
328 
329 typedef struct VuVirtqInflightDesc {
330     uint16_t index;
331     uint64_t counter;
332 } VuVirtqInflightDesc;
333 
334 typedef struct VuVirtq {
335     VuRing vring;
336 
337     VuVirtqInflight *inflight;
338 
339     VuVirtqInflightDesc *resubmit_list;
340 
341     uint16_t resubmit_num;
342 
343     uint64_t counter;
344 
345     /* Next head to pop */
346     uint16_t last_avail_idx;
347 
348     /* Last avail_idx read from VQ. */
349     uint16_t shadow_avail_idx;
350 
351     uint16_t used_idx;
352 
353     /* Last used index value we have signalled on */
354     uint16_t signalled_used;
355 
356     /* Last used index value we have signalled on */
357     bool signalled_used_valid;
358 
359     /* Notification enabled? */
360     bool notification;
361 
362     unsigned int inuse;
363 
364     vu_queue_handler_cb handler;
365 
366     int call_fd;
367     int kick_fd;
368     int err_fd;
369     unsigned int enable;
370     bool started;
371 
372     /* Guest addresses of our ring */
373     struct vhost_vring_addr vra;
374 } VuVirtq;
375 
376 enum VuWatchCondtion {
377     VU_WATCH_IN = POLLIN,
378     VU_WATCH_OUT = POLLOUT,
379     VU_WATCH_PRI = POLLPRI,
380     VU_WATCH_ERR = POLLERR,
381     VU_WATCH_HUP = POLLHUP,
382 };
383 
384 typedef void (*vu_panic_cb) (VuDev *dev, const char *err);
385 typedef void (*vu_watch_cb) (VuDev *dev, int condition, void *data);
386 typedef void (*vu_set_watch_cb) (VuDev *dev, int fd, int condition,
387                                  vu_watch_cb cb, void *data);
388 typedef void (*vu_remove_watch_cb) (VuDev *dev, int fd);
389 
390 typedef struct VuDevInflightInfo {
391     int fd;
392     void *addr;
393     uint64_t size;
394 } VuDevInflightInfo;
395 
396 struct VuDev {
397     int sock;
398     uint32_t nregions;
399     VuDevRegion *regions;
400     VuVirtq *vq;
401     VuDevInflightInfo inflight_info;
402     int log_call_fd;
403     /* Must be held while using backend_fd */
404     pthread_mutex_t backend_mutex;
405     int backend_fd;
406     uint64_t log_size;
407     uint8_t *log_table;
408     uint64_t features;
409     uint64_t protocol_features;
410     bool broken;
411     uint16_t max_queues;
412 
413     /*
414      * @read_msg: custom method to read vhost-user message
415      *
416      * Read data from vhost_user socket fd and fill up
417      * the passed VhostUserMsg *vmsg struct.
418      *
419      * If reading fails, it should close the received set of file
420      * descriptors as socket message's auxiliary data.
421      *
422      * For the details, please refer to vu_message_read in libvhost-user.c
423      * which will be used by default if not custom method is provided when
424      * calling vu_init
425      *
426      * Returns: true if vhost-user message successfully received,
427      *          otherwise return false.
428      *
429      */
430     vu_read_msg_cb read_msg;
431 
432     /*
433      * @set_watch: add or update the given fd to the watch set,
434      * call cb when condition is met.
435      */
436     vu_set_watch_cb set_watch;
437 
438     /* @remove_watch: remove the given fd from the watch set */
439     vu_remove_watch_cb remove_watch;
440 
441     /*
442      * @panic: encountered an unrecoverable error, you may try to re-initialize
443      */
444     vu_panic_cb panic;
445     const VuDevIface *iface;
446 
447     /* Postcopy data */
448     int postcopy_ufd;
449     bool postcopy_listening;
450 };
451 
452 typedef struct VuVirtqElement {
453     unsigned int index;
454     unsigned int out_num;
455     unsigned int in_num;
456     struct iovec *in_sg;
457     struct iovec *out_sg;
458 } VuVirtqElement;
459 
460 /**
461  * vu_init:
462  * @dev: a VuDev context
463  * @max_queues: maximum number of virtqueues
464  * @socket: the socket connected to vhost-user frontend
465  * @panic: a panic callback
466  * @set_watch: a set_watch callback
467  * @remove_watch: a remove_watch callback
468  * @iface: a VuDevIface structure with vhost-user device callbacks
469  *
470  * Initializes a VuDev vhost-user context.
471  *
472  * Returns: true on success, false on failure.
473  **/
474 bool vu_init(VuDev *dev,
475              uint16_t max_queues,
476              int socket,
477              vu_panic_cb panic,
478              vu_read_msg_cb read_msg,
479              vu_set_watch_cb set_watch,
480              vu_remove_watch_cb remove_watch,
481              const VuDevIface *iface);
482 
483 
484 /**
485  * vu_deinit:
486  * @dev: a VuDev context
487  *
488  * Cleans up the VuDev context
489  */
490 void vu_deinit(VuDev *dev);
491 
492 
493 /**
494  * vu_request_to_string: return string for vhost message request
495  * @req: VhostUserMsg request
496  *
497  * Returns a const string, do not free.
498  */
499 const char *vu_request_to_string(unsigned int req);
500 
501 /**
502  * vu_dispatch:
503  * @dev: a VuDev context
504  *
505  * Process one vhost-user message.
506  *
507  * Returns: TRUE on success, FALSE on failure.
508  */
509 bool vu_dispatch(VuDev *dev);
510 
511 /**
512  * vu_gpa_to_va:
513  * @dev: a VuDev context
514  * @plen: guest memory size
515  * @guest_addr: guest address
516  *
517  * Translate a guest address to a pointer. Returns NULL on failure.
518  */
519 void *vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr);
520 
521 /**
522  * vu_get_queue:
523  * @dev: a VuDev context
524  * @qidx: queue index
525  *
526  * Returns the queue number @qidx.
527  */
528 VuVirtq *vu_get_queue(VuDev *dev, int qidx);
529 
530 /**
531  * vu_set_queue_handler:
532  * @dev: a VuDev context
533  * @vq: a VuVirtq queue
534  * @handler: the queue handler callback
535  *
536  * Set the queue handler. This function may be called several times
537  * for the same queue. If called with NULL @handler, the handler is
538  * removed.
539  */
540 void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
541                           vu_queue_handler_cb handler);
542 
543 /**
544  * vu_set_queue_host_notifier:
545  * @dev: a VuDev context
546  * @vq: a VuVirtq queue
547  * @fd: a file descriptor
548  * @size: host page size
549  * @offset: notifier offset in @fd file
550  *
551  * Set queue's host notifier. This function may be called several
552  * times for the same queue. If called with -1 @fd, the notifier
553  * is removed.
554  */
555 bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
556                                 int size, int offset);
557 
558 /**
559  * vu_lookup_shared_object:
560  * @dev: a VuDev context
561  * @uuid: UUID of the shared object
562  * @dmabuf_fd: output dma-buf file descriptor
563  *
564  * Lookup for a virtio shared object (i.e., dma-buf fd) associated with the
565  * received UUID. Result, if found, is stored in the dmabuf_fd argument.
566  *
567  * Returns: whether the virtio object was found.
568  */
569 bool vu_lookup_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN],
570                              int *dmabuf_fd);
571 
572 /**
573  * vu_add_shared_object:
574  * @dev: a VuDev context
575  * @uuid: UUID of the shared object
576  *
577  * Registers this back-end as the exporter for the object associated with
578  * the received UUID.
579  *
580  * Returns: TRUE on success, FALSE on failure.
581  */
582 bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
583 
584 /**
585  * vu_rm_shared_object:
586  * @dev: a VuDev context
587  * @uuid: UUID of the shared object
588  *
589  * Removes a shared object entry (i.e., back-end entry) associated with the
590  * received UUID key from the hash table.
591  *
592  * Returns: TRUE on success, FALSE on failure.
593  */
594 bool vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
595 
596 /**
597  * vu_queue_set_notification:
598  * @dev: a VuDev context
599  * @vq: a VuVirtq queue
600  * @enable: state
601  *
602  * Set whether the queue notifies (via event index or interrupt)
603  */
604 void vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable);
605 
606 /**
607  * vu_queue_enabled:
608  * @dev: a VuDev context
609  * @vq: a VuVirtq queue
610  *
611  * Returns: whether the queue is enabled.
612  */
613 bool vu_queue_enabled(VuDev *dev, VuVirtq *vq);
614 
615 /**
616  * vu_queue_started:
617  * @dev: a VuDev context
618  * @vq: a VuVirtq queue
619  *
620  * Returns: whether the queue is started.
621  */
622 bool vu_queue_started(const VuDev *dev, const VuVirtq *vq);
623 
624 /**
625  * vu_queue_empty:
626  * @dev: a VuDev context
627  * @vq: a VuVirtq queue
628  *
629  * Returns: true if the queue is empty or not ready.
630  */
631 bool vu_queue_empty(VuDev *dev, VuVirtq *vq);
632 
633 /**
634  * vu_queue_notify:
635  * @dev: a VuDev context
636  * @vq: a VuVirtq queue
637  *
638  * Request to notify the queue via callfd (skipped if unnecessary)
639  */
640 void vu_queue_notify(VuDev *dev, VuVirtq *vq);
641 
642 void vu_config_change_msg(VuDev *dev);
643 
644 /**
645  * vu_queue_notify_sync:
646  * @dev: a VuDev context
647  * @vq: a VuVirtq queue
648  *
649  * Request to notify the queue via callfd (skipped if unnecessary)
650  * or sync message if possible.
651  */
652 void vu_queue_notify_sync(VuDev *dev, VuVirtq *vq);
653 
654 /**
655  * vu_queue_pop:
656  * @dev: a VuDev context
657  * @vq: a VuVirtq queue
658  * @sz: the size of struct to return (must be >= VuVirtqElement)
659  *
660  * Returns: a VuVirtqElement filled from the queue or NULL. The
661  * returned element must be free()-d by the caller.
662  */
663 void *vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz);
664 
665 
666 /**
667  * vu_queue_unpop:
668  * @dev: a VuDev context
669  * @vq: a VuVirtq queue
670  * @elem: The #VuVirtqElement
671  * @len: number of bytes written
672  *
673  * Pretend the most recent element wasn't popped from the virtqueue.  The next
674  * call to vu_queue_pop() will refetch the element.
675  */
676 void vu_queue_unpop(VuDev *dev, VuVirtq *vq, VuVirtqElement *elem,
677                     size_t len);
678 
679 /**
680  * vu_queue_rewind:
681  * @dev: a VuDev context
682  * @vq: a VuVirtq queue
683  * @num: number of elements to push back
684  *
685  * Pretend that elements weren't popped from the virtqueue.  The next
686  * virtqueue_pop() will refetch the oldest element.
687  *
688  * Returns: true on success, false if @num is greater than the number of in use
689  * elements.
690  */
691 bool vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num);
692 
693 /**
694  * vu_queue_fill:
695  * @dev: a VuDev context
696  * @vq: a VuVirtq queue
697  * @elem: a VuVirtqElement
698  * @len: length in bytes to write
699  * @idx: optional offset for the used ring index (0 in general)
700  *
701  * Fill the used ring with @elem element.
702  */
703 void vu_queue_fill(VuDev *dev, VuVirtq *vq,
704                    const VuVirtqElement *elem,
705                    unsigned int len, unsigned int idx);
706 
707 /**
708  * vu_queue_push:
709  * @dev: a VuDev context
710  * @vq: a VuVirtq queue
711  * @elem: a VuVirtqElement
712  * @len: length in bytes to write
713  *
714  * Helper that combines vu_queue_fill() with a vu_queue_flush().
715  */
716 void vu_queue_push(VuDev *dev, VuVirtq *vq,
717                    const VuVirtqElement *elem, unsigned int len);
718 
719 /**
720  * vu_queue_flush:
721  * @dev: a VuDev context
722  * @vq: a VuVirtq queue
723  * @num: number of elements to flush
724  *
725  * Mark the last number of elements as done (used.idx is updated by
726  * num elements).
727 */
728 void vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int num);
729 
730 /**
731  * vu_queue_get_avail_bytes:
732  * @dev: a VuDev context
733  * @vq: a VuVirtq queue
734  * @in_bytes: in bytes
735  * @out_bytes: out bytes
736  * @max_in_bytes: stop counting after max_in_bytes
737  * @max_out_bytes: stop counting after max_out_bytes
738  *
739  * Count the number of available bytes, up to max_in_bytes/max_out_bytes.
740  */
741 void vu_queue_get_avail_bytes(VuDev *vdev, VuVirtq *vq, unsigned int *in_bytes,
742                               unsigned int *out_bytes,
743                               unsigned max_in_bytes, unsigned max_out_bytes);
744 
745 /**
746  * vu_queue_avail_bytes:
747  * @dev: a VuDev context
748  * @vq: a VuVirtq queue
749  * @in_bytes: expected in bytes
750  * @out_bytes: expected out bytes
751  *
752  * Returns: true if in_bytes <= in_total && out_bytes <= out_total
753  */
754 bool vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
755                           unsigned int out_bytes);
756 
757 #endif /* LIBVHOST_USER_H */
758