1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
10 * Interface to vhost-user
14 #include <sys/eventfd.h>
16 #include <rte_memory.h>
17 #include <rte_mempool.h>
23 /* These are not C++-aware. */
24 #include <linux/vhost.h>
25 #include <linux/virtio_ring.h>
26 #include <linux/virtio_net.h>
28 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
29 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
30 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
31 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
32 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
34 /** Protocol features. */
35 #ifndef VHOST_USER_PROTOCOL_F_MQ
36 #define VHOST_USER_PROTOCOL_F_MQ 0
39 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
40 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
43 #ifndef VHOST_USER_PROTOCOL_F_RARP
44 #define VHOST_USER_PROTOCOL_F_RARP 2
47 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
48 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
51 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
52 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
55 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
56 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
59 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
60 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
63 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
64 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
67 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
68 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
71 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
72 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
75 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
76 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
79 /** Indicate whether protocol features negotiation is supported. */
80 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
81 #define VHOST_USER_F_PROTOCOL_FEATURES 30
85 * Information relating to memory regions including offsets to
86 * addresses in QEMUs memory file.
88 struct rte_vhost_mem_region {
89 uint64_t guest_phys_addr;
90 uint64_t guest_user_addr;
91 uint64_t host_user_addr;
99 * Memory structure includes region and mapping information.
101 struct rte_vhost_memory {
103 struct rte_vhost_mem_region regions[];
106 struct rte_vhost_inflight_desc_split {
113 struct rte_vhost_inflight_info_split {
117 uint16_t last_inflight_io;
119 struct rte_vhost_inflight_desc_split desc[0];
122 struct rte_vhost_inflight_desc_packed {
135 struct rte_vhost_inflight_info_packed {
140 uint16_t old_free_head;
142 uint16_t old_used_idx;
143 uint8_t used_wrap_counter;
144 uint8_t old_used_wrap_counter;
146 struct rte_vhost_inflight_desc_packed desc[0];
149 struct rte_vhost_vring {
151 struct vring_desc *desc;
152 struct vring_packed_desc *desc_packed;
155 struct vring_avail *avail;
156 struct vring_packed_desc_event *driver_event;
159 struct vring_used *used;
160 struct vring_packed_desc_event *device_event;
162 uint64_t log_guest_addr;
164 /** Deprecated, use rte_vhost_vring_call() instead. */
172 * Possible results of the vhost user message handling callbacks
174 enum rte_vhost_msg_result {
175 /* Message handling failed */
176 RTE_VHOST_MSG_RESULT_ERR = -1,
177 /* Message handling successful */
178 RTE_VHOST_MSG_RESULT_OK = 0,
179 /* Message handling successful and reply prepared */
180 RTE_VHOST_MSG_RESULT_REPLY = 1,
181 /* Message not handled */
182 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
186 * Function prototype for the vhost backend to handle specific vhost user
194 * RTE_VHOST_MSG_RESULT_OK on success,
195 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
196 * RTE_VHOST_MSG_RESULT_ERR on failure,
197 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
199 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
202 * Optional vhost user message handlers.
204 struct rte_vhost_user_extern_ops {
205 /* Called prior to the master message handling. */
206 rte_vhost_msg_handle pre_msg_handle;
207 /* Called after the master message handling. */
208 rte_vhost_msg_handle post_msg_handle;
212 * Device and vring operations.
214 struct vhost_device_ops {
215 int (*new_device)(int vid); /**< Add device. */
216 void (*destroy_device)(int vid); /**< Remove device. */
218 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
221 * Features could be changed after the feature negotiation.
222 * For example, VHOST_F_LOG_ALL will be set/cleared at the
223 * start/end of live migration, respectively. This callback
224 * is used to inform the application on such change.
226 int (*features_changed)(int vid, uint64_t features);
228 int (*new_connection)(int vid);
229 void (*destroy_connection)(int vid);
232 * This callback gets called each time a guest gets notified
233 * about waiting packets. This is the interrupt handling trough
234 * the eventfd_write(callfd), which can be used for counting these
237 void (*guest_notified)(int vid);
239 void *reserved[1]; /**< Reserved for future extension */
243 * Convert guest physical address to host virtual address
245 * This function is deprecated because unsafe.
246 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
247 * guest physical ranges are fully and contiguously mapped into
248 * process virtual address space.
251 * the guest memory regions
253 * the guest physical address for querying
255 * the host virtual address on success, 0 on failure
258 static __rte_always_inline uint64_t
259 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
261 struct rte_vhost_mem_region *reg;
264 for (i = 0; i < mem->nregions; i++) {
265 reg = &mem->regions[i];
266 if (gpa >= reg->guest_phys_addr &&
267 gpa < reg->guest_phys_addr + reg->size) {
268 return gpa - reg->guest_phys_addr +
277 * Convert guest physical address to host virtual address safely
279 * This variant of rte_vhost_gpa_to_vva() takes care all the
280 * requested length is mapped and contiguous in process address
284 * the guest memory regions
286 * the guest physical address for querying
288 * the size of the requested area to map, updated with actual size mapped
290 * the host virtual address on success, 0 on failure
293 static __rte_always_inline uint64_t
294 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
295 uint64_t gpa, uint64_t *len)
297 struct rte_vhost_mem_region *r;
300 for (i = 0; i < mem->nregions; i++) {
301 r = &mem->regions[i];
302 if (gpa >= r->guest_phys_addr &&
303 gpa < r->guest_phys_addr + r->size) {
305 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
306 *len = r->guest_phys_addr + r->size - gpa;
308 return gpa - r->guest_phys_addr +
317 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
320 * Log the memory write start with given address.
322 * This function only need be invoked when the live migration starts.
323 * Therefore, we won't need call it at all in the most of time. For
324 * making the performance impact be minimum, it's suggested to do a
325 * check before calling it:
327 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
328 * rte_vhost_log_write(vid, addr, len);
333 * the starting address for write (in guest physical address space)
335 * the length to write
337 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
340 * Log the used ring update start at given offset.
342 * Same as rte_vhost_log_write, it's suggested to do a check before
345 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
346 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
353 * the offset inside the used ring
355 * the length to write
357 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
358 uint64_t offset, uint64_t len);
360 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
363 * Register vhost driver. path could be different for multiple
366 int rte_vhost_driver_register(const char *path, uint64_t flags);
368 /* Unregister vhost driver. This is only meaningful to vhost user. */
369 int rte_vhost_driver_unregister(const char *path);
372 * Set the vdpa device id, enforce single connection per socket
375 * The vhost-user socket file path
379 * 0 on success, -1 on failure
383 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
386 * Unset the vdpa device id
389 * The vhost-user socket file path
391 * 0 on success, -1 on failure
395 rte_vhost_driver_detach_vdpa_device(const char *path);
401 * The vhost-user socket file path
403 * Device id, -1 on failure
407 rte_vhost_driver_get_vdpa_device_id(const char *path);
410 * Set the feature bits the vhost-user driver supports.
413 * The vhost-user socket file path
417 * 0 on success, -1 on failure
419 int rte_vhost_driver_set_features(const char *path, uint64_t features);
422 * Enable vhost-user driver features.
425 * - the param features should be a subset of the feature bits provided
426 * by rte_vhost_driver_set_features().
427 * - it must be invoked before vhost-user negotiation starts.
430 * The vhost-user socket file path
434 * 0 on success, -1 on failure
436 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
439 * Disable vhost-user driver features.
441 * The two notes at rte_vhost_driver_enable_features() also apply here.
444 * The vhost-user socket file path
446 * Features to disable
448 * 0 on success, -1 on failure
450 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
453 * Get the feature bits before feature negotiation.
456 * The vhost-user socket file path
458 * A pointer to store the queried feature bits
460 * 0 on success, -1 on failure
462 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
465 * Set the protocol feature bits before feature negotiation.
468 * The vhost-user socket file path
469 * @param protocol_features
470 * Supported protocol features
472 * 0 on success, -1 on failure
476 rte_vhost_driver_set_protocol_features(const char *path,
477 uint64_t protocol_features);
480 * Get the protocol feature bits before feature negotiation.
483 * The vhost-user socket file path
484 * @param protocol_features
485 * A pointer to store the queried protocol feature bits
487 * 0 on success, -1 on failure
491 rte_vhost_driver_get_protocol_features(const char *path,
492 uint64_t *protocol_features);
495 * Get the queue number bits before feature negotiation.
498 * The vhost-user socket file path
500 * A pointer to store the queried queue number bits
502 * 0 on success, -1 on failure
506 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
509 * Get the feature bits after negotiation
514 * A pointer to store the queried feature bits
516 * 0 on success, -1 on failure
518 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
520 /* Register callbacks. */
521 int rte_vhost_driver_callback_register(const char *path,
522 struct vhost_device_ops const * const ops);
526 * Start the vhost-user driver.
528 * This function triggers the vhost-user negotiation.
531 * The vhost-user socket file path
533 * 0 on success, -1 on failure
535 int rte_vhost_driver_start(const char *path);
538 * Get the MTU value of the device if set in QEMU.
541 * virtio-net device ID
543 * The variable to store the MTU value
547 * -EAGAIN: device not yet started
548 * -ENOTSUP: device does not support MTU feature
550 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
553 * Get the numa node from which the virtio net device's memory
560 * The numa node, -1 on failure
562 int rte_vhost_get_numa_node(int vid);
566 * Get the number of queues the device supports.
568 * Note this function is deprecated, as it returns a queue pair number,
569 * which is vhost specific. Instead, rte_vhost_get_vring_num should
576 * The number of queues, 0 on failure
579 uint32_t rte_vhost_get_queue_num(int vid);
582 * Get the number of vrings the device supports.
588 * The number of vrings, 0 on failure
590 uint16_t rte_vhost_get_vring_num(int vid);
593 * Get the virtio net device's ifname, which is the vhost-user socket
599 * The buffer to stored the queried ifname
604 * 0 on success, -1 on failure
606 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
609 * Get how many avail entries are left in the queue
617 * num of avail entries left
619 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
624 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
625 * be received from the physical port or from another virtual device. A packet
626 * count is returned to indicate the number of packets that were successfully
627 * added to the RX queue.
631 * virtio queue index in mq case
633 * array to contain packets to be enqueued
635 * packets num to be enqueued
637 * num of packets enqueued
639 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
640 struct rte_mbuf **pkts, uint16_t count);
643 * This function gets guest buffers from the virtio device TX virtqueue,
644 * construct host mbufs, copies guest buffer content to host mbufs and
645 * store them in pkts to be processed.
649 * virtio queue index in mq case
651 * mbuf_pool where host mbuf is allocated.
653 * array to contain packets to be dequeued
655 * packets num to be dequeued
657 * num of packets dequeued
659 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
660 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
663 * Get guest mem table: a list of memory regions.
665 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
666 * guest memory regions. Application should free it at destroy_device()
672 * To store the returned mem regions
674 * 0 on success, -1 on failure
676 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
679 * Get guest vring info, including the vring address, vring size, etc.
686 * the structure to hold the requested vring info
688 * 0 on success, -1 on failure
690 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
691 struct rte_vhost_vring *vring);
694 * Notify the guest that used descriptors have been added to the vring. This
695 * function acts as a memory barrier.
702 * 0 on success, -1 on failure
704 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
707 * Get vhost RX queue avail count.
712 * virtio queue index in mq case
714 * num of desc available
716 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
719 * Get log base and log size of the vhost device
728 * 0 on success, -1 on failure
732 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
735 * Get last_avail/used_idx of the vhost virtqueue
741 * @param last_avail_idx
742 * vhost last_avail_idx to get
743 * @param last_used_idx
744 * vhost last_used_idx to get
746 * 0 on success, -1 on failure
750 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
751 uint16_t *last_avail_idx, uint16_t *last_used_idx);
754 * Set last_avail/used_idx of the vhost virtqueue
760 * @param last_avail_idx
761 * last_avail_idx to set
762 * @param last_used_idx
763 * last_used_idx to set
765 * 0 on success, -1 on failure
769 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
770 uint16_t last_avail_idx, uint16_t last_used_idx);
773 * Register external message handling callbacks
778 * virtio external callbacks to register
780 * additional context passed to the callbacks
782 * 0 on success, -1 on failure
786 rte_vhost_extern_callback_register(int vid,
787 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
790 * Get vdpa device id for vhost device.
799 rte_vhost_get_vdpa_device_id(int vid);
805 #endif /* _RTE_VHOST_H_ */