4 * Copyright (c) 2016 Red Hat, Inc.
7 * Victor Kaplansky <victork@redhat.com>
8 * Marc-André Lureau <mlureau@redhat.com>
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.
14 #ifndef LIBVHOST_USER_H
15 #define LIBVHOST_USER_H
21 #include <linux/vhost.h>
22 #include "standard-headers/linux/virtio_ring.h"
24 /* Based on qemu/hw/virtio/vhost-user.c */
25 #define VHOST_USER_F_PROTOCOL_FEATURES 30
26 #define VHOST_LOG_PAGE 4096
28 #define VHOST_MAX_NR_VIRTQUEUE 8
29 #define VIRTQUEUE_MAX_SIZE 1024
31 #define VHOST_MEMORY_MAX_NREGIONS 8
33 typedef enum VhostSetConfigType
{
34 VHOST_SET_CONFIG_TYPE_MASTER
= 0,
35 VHOST_SET_CONFIG_TYPE_MIGRATION
= 1,
39 * Maximum size of virtio device config space
41 #define VHOST_USER_MAX_CONFIG_SIZE 256
43 enum 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,
57 VHOST_USER_PROTOCOL_F_MAX
60 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
62 typedef enum VhostUserRequest
{
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,
97 typedef 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,
103 } VhostUserSlaveRequest
;
105 typedef 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
;
112 typedef struct VhostUserMemory
{
115 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
118 typedef struct VhostUserLog
{
120 uint64_t mmap_offset
;
123 typedef struct VhostUserConfig
{
127 uint8_t region
[VHOST_USER_MAX_CONFIG_SIZE
];
130 static VhostUserConfig c
__attribute__ ((unused
));
131 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
135 typedef struct VhostUserVringArea
{
139 } VhostUserVringArea
;
142 # define VU_PACKED __attribute__((gcc_struct, packed))
144 # define VU_PACKED __attribute__((packed))
147 typedef struct VhostUserMsg
{
148 VhostUserRequest request
;
150 #define VHOST_USER_VERSION_MASK (0x3)
151 #define VHOST_USER_REPLY_MASK (0x1 << 2)
152 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
154 uint32_t size
; /* the following payload size */
157 #define VHOST_USER_VRING_IDX_MASK (0xff)
158 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
160 struct vhost_vring_state state
;
161 struct vhost_vring_addr addr
;
162 VhostUserMemory memory
;
164 VhostUserConfig config
;
165 VhostUserVringArea area
;
168 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
171 } VU_PACKED VhostUserMsg
;
173 typedef struct VuDevRegion
{
174 /* Guest Physical address. */
176 /* Memory region size. */
178 /* QEMU virtual address (userspace). */
180 /* Starting offset in our mmaped space. */
181 uint64_t mmap_offset
;
182 /* Start address of mmaped space. */
186 typedef struct VuDev VuDev
;
188 typedef uint64_t (*vu_get_features_cb
) (VuDev
*dev
);
189 typedef void (*vu_set_features_cb
) (VuDev
*dev
, uint64_t features
);
190 typedef int (*vu_process_msg_cb
) (VuDev
*dev
, VhostUserMsg
*vmsg
,
192 typedef void (*vu_queue_set_started_cb
) (VuDev
*dev
, int qidx
, bool started
);
193 typedef bool (*vu_queue_is_processed_in_order_cb
) (VuDev
*dev
, int qidx
);
194 typedef int (*vu_get_config_cb
) (VuDev
*dev
, uint8_t *config
, uint32_t len
);
195 typedef int (*vu_set_config_cb
) (VuDev
*dev
, const uint8_t *data
,
196 uint32_t offset
, uint32_t size
,
199 typedef struct VuDevIface
{
200 /* called by VHOST_USER_GET_FEATURES to get the features bitmask */
201 vu_get_features_cb get_features
;
202 /* enable vhost implementation features */
203 vu_set_features_cb set_features
;
204 /* get the protocol feature bitmask from the underlying vhost
206 vu_get_features_cb get_protocol_features
;
207 /* enable protocol features in the underlying vhost implementation. */
208 vu_set_features_cb set_protocol_features
;
209 /* process_msg is called for each vhost-user message received */
210 /* skip libvhost-user processing if return value != 0 */
211 vu_process_msg_cb process_msg
;
212 /* tells when queues can be processed */
213 vu_queue_set_started_cb queue_set_started
;
215 * If the queue is processed in order, in which case it will be
216 * resumed to vring.used->idx. This can help to support resuming
217 * on unmanaged exit/crash.
219 vu_queue_is_processed_in_order_cb queue_is_processed_in_order
;
220 /* get the config space of the device */
221 vu_get_config_cb get_config
;
222 /* set the config space of the device */
223 vu_set_config_cb set_config
;
226 typedef void (*vu_queue_handler_cb
) (VuDev
*dev
, int qidx
);
228 typedef struct VuRing
{
230 struct vring_desc
*desc
;
231 struct vring_avail
*avail
;
232 struct vring_used
*used
;
233 uint64_t log_guest_addr
;
237 typedef struct VuVirtq
{
240 /* Next head to pop */
241 uint16_t last_avail_idx
;
243 /* Last avail_idx read from VQ. */
244 uint16_t shadow_avail_idx
;
248 /* Last used index value we have signalled on */
249 uint16_t signalled_used
;
251 /* Last used index value we have signalled on */
252 bool signalled_used_valid
;
254 /* Notification enabled? */
259 vu_queue_handler_cb handler
;
268 enum 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
,
276 typedef void (*vu_panic_cb
) (VuDev
*dev
, const char *err
);
277 typedef void (*vu_watch_cb
) (VuDev
*dev
, int condition
, void *data
);
278 typedef void (*vu_set_watch_cb
) (VuDev
*dev
, int fd
, int condition
,
279 vu_watch_cb cb
, void *data
);
280 typedef void (*vu_remove_watch_cb
) (VuDev
*dev
, int fd
);
285 VuDevRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
286 VuVirtq vq
[VHOST_MAX_NR_VIRTQUEUE
];
292 uint64_t protocol_features
;
295 /* @set_watch: add or update the given fd to the watch set,
296 * call cb when condition is met */
297 vu_set_watch_cb set_watch
;
299 /* @remove_watch: remove the given fd from the watch set */
300 vu_remove_watch_cb remove_watch
;
302 /* @panic: encountered an unrecoverable error, you may try to
305 const VuDevIface
*iface
;
309 bool postcopy_listening
;
312 typedef struct VuVirtqElement
{
314 unsigned int out_num
;
317 struct iovec
*out_sg
;
322 * @dev: a VuDev context
323 * @socket: the socket connected to vhost-user master
324 * @panic: a panic callback
325 * @set_watch: a set_watch callback
326 * @remove_watch: a remove_watch callback
327 * @iface: a VuDevIface structure with vhost-user device callbacks
329 * Intializes a VuDev vhost-user context.
331 void vu_init(VuDev
*dev
,
334 vu_set_watch_cb set_watch
,
335 vu_remove_watch_cb remove_watch
,
336 const VuDevIface
*iface
);
341 * @dev: a VuDev context
343 * Cleans up the VuDev context
345 void vu_deinit(VuDev
*dev
);
349 * @dev: a VuDev context
351 * Process one vhost-user message.
353 * Returns: TRUE on success, FALSE on failure.
355 bool vu_dispatch(VuDev
*dev
);
359 * @dev: a VuDev context
360 * @plen: guest memory size
361 * @guest_addr: guest address
363 * Translate a guest address to a pointer. Returns NULL on failure.
365 void *vu_gpa_to_va(VuDev
*dev
, uint64_t *plen
, uint64_t guest_addr
);
369 * @dev: a VuDev context
372 * Returns the queue number @qidx.
374 VuVirtq
*vu_get_queue(VuDev
*dev
, int qidx
);
377 * vu_set_queue_handler:
378 * @dev: a VuDev context
379 * @vq: a VuVirtq queue
380 * @handler: the queue handler callback
382 * Set the queue handler. This function may be called several times
383 * for the same queue. If called with NULL @handler, the handler is
386 void vu_set_queue_handler(VuDev
*dev
, VuVirtq
*vq
,
387 vu_queue_handler_cb handler
);
390 * vu_set_queue_host_notifier:
391 * @dev: a VuDev context
392 * @vq: a VuVirtq queue
393 * @fd: a file descriptor
394 * @size: host page size
395 * @offset: notifier offset in @fd file
397 * Set queue's host notifier. This function may be called several
398 * times for the same queue. If called with -1 @fd, the notifier
401 bool vu_set_queue_host_notifier(VuDev
*dev
, VuVirtq
*vq
, int fd
,
402 int size
, int offset
);
405 * vu_queue_set_notification:
406 * @dev: a VuDev context
407 * @vq: a VuVirtq queue
410 * Set whether the queue notifies (via event index or interrupt)
412 void vu_queue_set_notification(VuDev
*dev
, VuVirtq
*vq
, int enable
);
416 * @dev: a VuDev context
417 * @vq: a VuVirtq queue
419 * Returns: whether the queue is enabled.
421 bool vu_queue_enabled(VuDev
*dev
, VuVirtq
*vq
);
425 * @dev: a VuDev context
426 * @vq: a VuVirtq queue
428 * Returns: whether the queue is started.
430 bool vu_queue_started(const VuDev
*dev
, const VuVirtq
*vq
);
434 * @dev: a VuDev context
435 * @vq: a VuVirtq queue
437 * Returns: true if the queue is empty or not ready.
439 bool vu_queue_empty(VuDev
*dev
, VuVirtq
*vq
);
443 * @dev: a VuDev context
444 * @vq: a VuVirtq queue
446 * Request to notify the queue via callfd (skipped if unnecessary)
448 void vu_queue_notify(VuDev
*dev
, VuVirtq
*vq
);
452 * @dev: a VuDev context
453 * @vq: a VuVirtq queue
454 * @sz: the size of struct to return (must be >= VuVirtqElement)
456 * Returns: a VuVirtqElement filled from the queue or NULL. The
457 * returned element must be free()-d by the caller.
459 void *vu_queue_pop(VuDev
*dev
, VuVirtq
*vq
, size_t sz
);
463 * @dev: a VuDev context
464 * @vq: a VuVirtq queue
465 * @num: number of elements to push back
467 * Pretend that elements weren't popped from the virtqueue. The next
468 * virtqueue_pop() will refetch the oldest element.
470 * Returns: true on success, false if @num is greater than the number of in use
473 bool vu_queue_rewind(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
);
477 * @dev: a VuDev context
478 * @vq: a VuVirtq queue
479 * @elem: a VuVirtqElement
480 * @len: length in bytes to write
481 * @idx: optional offset for the used ring index (0 in general)
483 * Fill the used ring with @elem element.
485 void vu_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
486 const VuVirtqElement
*elem
,
487 unsigned int len
, unsigned int idx
);
491 * @dev: a VuDev context
492 * @vq: a VuVirtq queue
493 * @elem: a VuVirtqElement
494 * @len: length in bytes to write
496 * Helper that combines vu_queue_fill() with a vu_queue_flush().
498 void vu_queue_push(VuDev
*dev
, VuVirtq
*vq
,
499 const VuVirtqElement
*elem
, unsigned int len
);
503 * @dev: a VuDev context
504 * @vq: a VuVirtq queue
505 * @num: number of elements to flush
507 * Mark the last number of elements as done (used.idx is updated by
510 void vu_queue_flush(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
);
513 * vu_queue_get_avail_bytes:
514 * @dev: a VuDev context
515 * @vq: a VuVirtq queue
516 * @in_bytes: in bytes
517 * @out_bytes: out bytes
518 * @max_in_bytes: stop counting after max_in_bytes
519 * @max_out_bytes: stop counting after max_out_bytes
521 * Count the number of available bytes, up to max_in_bytes/max_out_bytes.
523 void 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
);
528 * vu_queue_avail_bytes:
529 * @dev: a VuDev context
530 * @vq: a VuVirtq queue
531 * @in_bytes: expected in bytes
532 * @out_bytes: expected out bytes
534 * Returns: true if in_bytes <= in_total && out_bytes <= out_total
536 bool vu_queue_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int in_bytes
,
537 unsigned int out_bytes
);
539 #endif /* LIBVHOST_USER_H */