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 enum 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,
42 VHOST_USER_PROTOCOL_F_MAX
45 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
47 typedef enum VhostUserRequest
{
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,
75 typedef struct VhostUserMemoryRegion
{
76 uint64_t guest_phys_addr
;
78 uint64_t userspace_addr
;
80 } VhostUserMemoryRegion
;
82 typedef struct VhostUserMemory
{
85 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
88 typedef struct VhostUserLog
{
94 # define VU_PACKED __attribute__((gcc_struct, packed))
96 # define VU_PACKED __attribute__((packed))
99 typedef struct VhostUserMsg
{
100 VhostUserRequest request
;
102 #define VHOST_USER_VERSION_MASK (0x3)
103 #define VHOST_USER_REPLY_MASK (0x1 << 2)
105 uint32_t size
; /* the following payload size */
108 #define VHOST_USER_VRING_IDX_MASK (0xff)
109 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
111 struct vhost_vring_state state
;
112 struct vhost_vring_addr addr
;
113 VhostUserMemory memory
;
117 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
120 } VU_PACKED VhostUserMsg
;
122 typedef struct VuDevRegion
{
123 /* Guest Physical address. */
125 /* Memory region size. */
127 /* QEMU virtual address (userspace). */
129 /* Starting offset in our mmaped space. */
130 uint64_t mmap_offset
;
131 /* Start address of mmaped space. */
135 typedef struct VuDev VuDev
;
137 typedef uint64_t (*vu_get_features_cb
) (VuDev
*dev
);
138 typedef void (*vu_set_features_cb
) (VuDev
*dev
, uint64_t features
);
139 typedef int (*vu_process_msg_cb
) (VuDev
*dev
, VhostUserMsg
*vmsg
,
141 typedef void (*vu_queue_set_started_cb
) (VuDev
*dev
, int qidx
, bool started
);
142 typedef bool (*vu_queue_is_processed_in_order_cb
) (VuDev
*dev
, int qidx
);
144 typedef struct VuDevIface
{
145 /* called by VHOST_USER_GET_FEATURES to get the features bitmask */
146 vu_get_features_cb get_features
;
147 /* enable vhost implementation features */
148 vu_set_features_cb set_features
;
149 /* get the protocol feature bitmask from the underlying vhost
151 vu_get_features_cb get_protocol_features
;
152 /* enable protocol features in the underlying vhost implementation. */
153 vu_set_features_cb set_protocol_features
;
154 /* process_msg is called for each vhost-user message received */
155 /* skip libvhost-user processing if return value != 0 */
156 vu_process_msg_cb process_msg
;
157 /* tells when queues can be processed */
158 vu_queue_set_started_cb queue_set_started
;
160 * If the queue is processed in order, in which case it will be
161 * resumed to vring.used->idx. This can help to support resuming
162 * on unmanaged exit/crash.
164 vu_queue_is_processed_in_order_cb queue_is_processed_in_order
;
167 typedef void (*vu_queue_handler_cb
) (VuDev
*dev
, int qidx
);
169 typedef struct VuRing
{
171 struct vring_desc
*desc
;
172 struct vring_avail
*avail
;
173 struct vring_used
*used
;
174 uint64_t log_guest_addr
;
178 typedef struct VuVirtq
{
181 /* Next head to pop */
182 uint16_t last_avail_idx
;
184 /* Last avail_idx read from VQ. */
185 uint16_t shadow_avail_idx
;
189 /* Last used index value we have signalled on */
190 uint16_t signalled_used
;
192 /* Last used index value we have signalled on */
193 bool signalled_used_valid
;
195 /* Notification enabled? */
200 vu_queue_handler_cb handler
;
209 enum 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
,
217 typedef void (*vu_panic_cb
) (VuDev
*dev
, const char *err
);
218 typedef void (*vu_watch_cb
) (VuDev
*dev
, int condition
, void *data
);
219 typedef void (*vu_set_watch_cb
) (VuDev
*dev
, int fd
, int condition
,
220 vu_watch_cb cb
, void *data
);
221 typedef void (*vu_remove_watch_cb
) (VuDev
*dev
, int fd
);
226 VuDevRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
227 VuVirtq vq
[VHOST_MAX_NR_VIRTQUEUE
];
233 uint64_t protocol_features
;
236 /* @set_watch: add or update the given fd to the watch set,
237 * call cb when condition is met */
238 vu_set_watch_cb set_watch
;
240 /* @remove_watch: remove the given fd from the watch set */
241 vu_remove_watch_cb remove_watch
;
243 /* @panic: encountered an unrecoverable error, you may try to
246 const VuDevIface
*iface
;
249 typedef struct VuVirtqElement
{
251 unsigned int out_num
;
254 struct iovec
*out_sg
;
259 * @dev: a VuDev context
260 * @socket: the socket connected to vhost-user master
261 * @panic: a panic callback
262 * @set_watch: a set_watch callback
263 * @remove_watch: a remove_watch callback
264 * @iface: a VuDevIface structure with vhost-user device callbacks
266 * Intializes a VuDev vhost-user context.
268 void vu_init(VuDev
*dev
,
271 vu_set_watch_cb set_watch
,
272 vu_remove_watch_cb remove_watch
,
273 const VuDevIface
*iface
);
278 * @dev: a VuDev context
280 * Cleans up the VuDev context
282 void vu_deinit(VuDev
*dev
);
286 * @dev: a VuDev context
288 * Process one vhost-user message.
290 * Returns: TRUE on success, FALSE on failure.
292 bool vu_dispatch(VuDev
*dev
);
296 * @dev: a VuDev context
297 * @guest_addr: guest address
299 * Translate a guest address to a pointer. Returns NULL on failure.
301 void *vu_gpa_to_va(VuDev
*dev
, uint64_t guest_addr
);
305 * @dev: a VuDev context
308 * Returns the queue number @qidx.
310 VuVirtq
*vu_get_queue(VuDev
*dev
, int qidx
);
313 * vu_set_queue_handler:
314 * @dev: a VuDev context
315 * @vq: a VuVirtq queue
316 * @handler: the queue handler callback
318 * Set the queue handler. This function may be called several times
319 * for the same queue. If called with NULL @handler, the handler is
322 void vu_set_queue_handler(VuDev
*dev
, VuVirtq
*vq
,
323 vu_queue_handler_cb handler
);
327 * vu_queue_set_notification:
328 * @dev: a VuDev context
329 * @vq: a VuVirtq queue
332 * Set whether the queue notifies (via event index or interrupt)
334 void vu_queue_set_notification(VuDev
*dev
, VuVirtq
*vq
, int enable
);
338 * @dev: a VuDev context
339 * @vq: a VuVirtq queue
341 * Returns: whether the queue is enabled.
343 bool vu_queue_enabled(VuDev
*dev
, VuVirtq
*vq
);
347 * @dev: a VuDev context
348 * @vq: a VuVirtq queue
350 * Returns: whether the queue is started.
352 bool vu_queue_started(const VuDev
*dev
, const VuVirtq
*vq
);
356 * @dev: a VuDev context
357 * @vq: a VuVirtq queue
359 * Returns: true if the queue is empty or not ready.
361 bool vu_queue_empty(VuDev
*dev
, VuVirtq
*vq
);
365 * @dev: a VuDev context
366 * @vq: a VuVirtq queue
368 * Request to notify the queue via callfd (skipped if unnecessary)
370 void vu_queue_notify(VuDev
*dev
, VuVirtq
*vq
);
374 * @dev: a VuDev context
375 * @vq: a VuVirtq queue
376 * @sz: the size of struct to return (must be >= VuVirtqElement)
378 * Returns: a VuVirtqElement filled from the queue or NULL. The
379 * returned element must be free()-d by the caller.
381 void *vu_queue_pop(VuDev
*dev
, VuVirtq
*vq
, size_t sz
);
385 * @dev: a VuDev context
386 * @vq: a VuVirtq queue
387 * @num: number of elements to push back
389 * Pretend that elements weren't popped from the virtqueue. The next
390 * virtqueue_pop() will refetch the oldest element.
392 * Returns: true on success, false if @num is greater than the number of in use
395 bool vu_queue_rewind(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
);
399 * @dev: a VuDev context
400 * @vq: a VuVirtq queue
401 * @elem: a VuVirtqElement
402 * @len: length in bytes to write
403 * @idx: optional offset for the used ring index (0 in general)
405 * Fill the used ring with @elem element.
407 void vu_queue_fill(VuDev
*dev
, VuVirtq
*vq
,
408 const VuVirtqElement
*elem
,
409 unsigned int len
, unsigned int idx
);
413 * @dev: a VuDev context
414 * @vq: a VuVirtq queue
415 * @elem: a VuVirtqElement
416 * @len: length in bytes to write
418 * Helper that combines vu_queue_fill() with a vu_queue_flush().
420 void vu_queue_push(VuDev
*dev
, VuVirtq
*vq
,
421 const VuVirtqElement
*elem
, unsigned int len
);
425 * @dev: a VuDev context
426 * @vq: a VuVirtq queue
427 * @num: number of elements to flush
429 * Mark the last number of elements as done (used.idx is updated by
432 void vu_queue_flush(VuDev
*dev
, VuVirtq
*vq
, unsigned int num
);
435 * vu_queue_get_avail_bytes:
436 * @dev: a VuDev context
437 * @vq: a VuVirtq queue
438 * @in_bytes: in bytes
439 * @out_bytes: out bytes
440 * @max_in_bytes: stop counting after max_in_bytes
441 * @max_out_bytes: stop counting after max_out_bytes
443 * Count the number of available bytes, up to max_in_bytes/max_out_bytes.
445 void 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
);
450 * vu_queue_avail_bytes:
451 * @dev: a VuDev context
452 * @vq: a VuVirtq queue
453 * @in_bytes: expected in bytes
454 * @out_bytes: expected out bytes
456 * Returns: true if in_bytes <= in_total && out_bytes <= out_total
458 bool vu_queue_avail_bytes(VuDev
*dev
, VuVirtq
*vq
, unsigned int in_bytes
,
459 unsigned int out_bytes
);
461 #endif /* LIBVHOST_USER_H */