1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
10 * Interface to vhost-user
15 #include <sys/eventfd.h>
17 #include <rte_memory.h>
18 #include <rte_mempool.h>
24 /* These are not C++-aware. */
25 #include <linux/vhost.h>
26 #include <linux/virtio_ring.h>
27 #include <linux/virtio_net.h>
29 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
30 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
31 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
32 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
33 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
34 /* support mbuf with external buffer attached */
35 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
36 /* support only linear buffers (no chained mbufs) */
37 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
39 /** Protocol features. */
40 #ifndef VHOST_USER_PROTOCOL_F_MQ
41 #define VHOST_USER_PROTOCOL_F_MQ 0
44 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
45 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
48 #ifndef VHOST_USER_PROTOCOL_F_RARP
49 #define VHOST_USER_PROTOCOL_F_RARP 2
52 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
53 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
56 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
57 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
60 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
61 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
64 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
65 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
68 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
69 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
72 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
73 #define VHOST_USER_PROTOCOL_F_CONFIG 9
76 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
77 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
80 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
81 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
84 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
85 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
88 /** Indicate whether protocol features negotiation is supported. */
89 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
90 #define VHOST_USER_F_PROTOCOL_FEATURES 30
93 struct rte_vdpa_device;
96 * Information relating to memory regions including offsets to
97 * addresses in QEMUs memory file.
99 struct rte_vhost_mem_region {
100 uint64_t guest_phys_addr;
101 uint64_t guest_user_addr;
102 uint64_t host_user_addr;
110 * Memory structure includes region and mapping information.
112 struct rte_vhost_memory {
114 struct rte_vhost_mem_region regions[];
117 struct rte_vhost_inflight_desc_split {
124 struct rte_vhost_inflight_info_split {
128 uint16_t last_inflight_io;
130 struct rte_vhost_inflight_desc_split desc[0];
133 struct rte_vhost_inflight_desc_packed {
146 struct rte_vhost_inflight_info_packed {
151 uint16_t old_free_head;
153 uint16_t old_used_idx;
154 uint8_t used_wrap_counter;
155 uint8_t old_used_wrap_counter;
157 struct rte_vhost_inflight_desc_packed desc[0];
160 struct rte_vhost_resubmit_desc {
165 struct rte_vhost_resubmit_info {
166 struct rte_vhost_resubmit_desc *resubmit_list;
167 uint16_t resubmit_num;
170 struct rte_vhost_ring_inflight {
172 struct rte_vhost_inflight_info_split *inflight_split;
173 struct rte_vhost_inflight_info_packed *inflight_packed;
176 struct rte_vhost_resubmit_info *resubmit_inflight;
179 struct rte_vhost_vring {
181 struct vring_desc *desc;
182 struct vring_packed_desc *desc_packed;
185 struct vring_avail *avail;
186 struct vring_packed_desc_event *driver_event;
189 struct vring_used *used;
190 struct vring_packed_desc_event *device_event;
192 uint64_t log_guest_addr;
194 /** Deprecated, use rte_vhost_vring_call() instead. */
202 * Possible results of the vhost user message handling callbacks
204 enum rte_vhost_msg_result {
205 /* Message handling failed */
206 RTE_VHOST_MSG_RESULT_ERR = -1,
207 /* Message handling successful */
208 RTE_VHOST_MSG_RESULT_OK = 0,
209 /* Message handling successful and reply prepared */
210 RTE_VHOST_MSG_RESULT_REPLY = 1,
211 /* Message not handled */
212 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
216 * Function prototype for the vhost backend to handle specific vhost user
224 * RTE_VHOST_MSG_RESULT_OK on success,
225 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
226 * RTE_VHOST_MSG_RESULT_ERR on failure,
227 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
229 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
232 * Optional vhost user message handlers.
234 struct rte_vhost_user_extern_ops {
235 /* Called prior to the master message handling. */
236 rte_vhost_msg_handle pre_msg_handle;
237 /* Called after the master message handling. */
238 rte_vhost_msg_handle post_msg_handle;
242 * Device and vring operations.
244 struct vhost_device_ops {
245 int (*new_device)(int vid); /**< Add device. */
246 void (*destroy_device)(int vid); /**< Remove device. */
248 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
251 * Features could be changed after the feature negotiation.
252 * For example, VHOST_F_LOG_ALL will be set/cleared at the
253 * start/end of live migration, respectively. This callback
254 * is used to inform the application on such change.
256 int (*features_changed)(int vid, uint64_t features);
258 int (*new_connection)(int vid);
259 void (*destroy_connection)(int vid);
262 * This callback gets called each time a guest gets notified
263 * about waiting packets. This is the interrupt handling through
264 * the eventfd_write(callfd), which can be used for counting these
267 void (*guest_notified)(int vid);
269 void *reserved[1]; /**< Reserved for future extension */
273 * Convert guest physical address to host virtual address
275 * This function is deprecated because unsafe.
276 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
277 * guest physical ranges are fully and contiguously mapped into
278 * process virtual address space.
281 * the guest memory regions
283 * the guest physical address for querying
285 * the host virtual address on success, 0 on failure
288 static __rte_always_inline uint64_t
289 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
291 struct rte_vhost_mem_region *reg;
294 for (i = 0; i < mem->nregions; i++) {
295 reg = &mem->regions[i];
296 if (gpa >= reg->guest_phys_addr &&
297 gpa < reg->guest_phys_addr + reg->size) {
298 return gpa - reg->guest_phys_addr +
307 * Convert guest physical address to host virtual address safely
309 * This variant of rte_vhost_gpa_to_vva() takes care all the
310 * requested length is mapped and contiguous in process address
314 * the guest memory regions
316 * the guest physical address for querying
318 * the size of the requested area to map, updated with actual size mapped
320 * the host virtual address on success, 0 on failure
323 static __rte_always_inline uint64_t
324 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
325 uint64_t gpa, uint64_t *len)
327 struct rte_vhost_mem_region *r;
330 for (i = 0; i < mem->nregions; i++) {
331 r = &mem->regions[i];
332 if (gpa >= r->guest_phys_addr &&
333 gpa < r->guest_phys_addr + r->size) {
335 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
336 *len = r->guest_phys_addr + r->size - gpa;
338 return gpa - r->guest_phys_addr +
347 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
350 * Log the memory write start with given address.
352 * This function only need be invoked when the live migration starts.
353 * Therefore, we won't need call it at all in the most of time. For
354 * making the performance impact be minimum, it's suggested to do a
355 * check before calling it:
357 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
358 * rte_vhost_log_write(vid, addr, len);
363 * the starting address for write (in guest physical address space)
365 * the length to write
367 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
370 * Log the used ring update start at given offset.
372 * Same as rte_vhost_log_write, it's suggested to do a check before
375 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
376 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
383 * the offset inside the used ring
385 * the length to write
387 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
388 uint64_t offset, uint64_t len);
390 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
393 * Register vhost driver. path could be different for multiple
396 int rte_vhost_driver_register(const char *path, uint64_t flags);
398 /* Unregister vhost driver. This is only meaningful to vhost user. */
399 int rte_vhost_driver_unregister(const char *path);
402 * Set the vdpa device id, enforce single connection per socket
405 * The vhost-user socket file path
407 * vDPA device pointer
409 * 0 on success, -1 on failure
413 rte_vhost_driver_attach_vdpa_device(const char *path,
414 struct rte_vdpa_device *dev);
417 * Unset the vdpa device id
420 * The vhost-user socket file path
422 * 0 on success, -1 on failure
426 rte_vhost_driver_detach_vdpa_device(const char *path);
432 * The vhost-user socket file path
434 * vDPA device pointer, NULL on failure
437 struct rte_vdpa_device *
438 rte_vhost_driver_get_vdpa_device(const char *path);
441 * Set the feature bits the vhost-user driver supports.
444 * The vhost-user socket file path
448 * 0 on success, -1 on failure
450 int rte_vhost_driver_set_features(const char *path, uint64_t features);
453 * Enable vhost-user driver features.
456 * - the param features should be a subset of the feature bits provided
457 * by rte_vhost_driver_set_features().
458 * - it must be invoked before vhost-user negotiation starts.
461 * The vhost-user socket file path
465 * 0 on success, -1 on failure
467 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
470 * Disable vhost-user driver features.
472 * The two notes at rte_vhost_driver_enable_features() also apply here.
475 * The vhost-user socket file path
477 * Features to disable
479 * 0 on success, -1 on failure
481 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
484 * Get the feature bits before feature negotiation.
487 * The vhost-user socket file path
489 * A pointer to store the queried feature bits
491 * 0 on success, -1 on failure
493 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
496 * Set the protocol feature bits before feature negotiation.
499 * The vhost-user socket file path
500 * @param protocol_features
501 * Supported protocol features
503 * 0 on success, -1 on failure
507 rte_vhost_driver_set_protocol_features(const char *path,
508 uint64_t protocol_features);
511 * Get the protocol feature bits before feature negotiation.
514 * The vhost-user socket file path
515 * @param protocol_features
516 * A pointer to store the queried protocol feature bits
518 * 0 on success, -1 on failure
522 rte_vhost_driver_get_protocol_features(const char *path,
523 uint64_t *protocol_features);
526 * Get the queue number bits before feature negotiation.
529 * The vhost-user socket file path
531 * A pointer to store the queried queue number bits
533 * 0 on success, -1 on failure
537 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
540 * Get the feature bits after negotiation
545 * A pointer to store the queried feature bits
547 * 0 on success, -1 on failure
549 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
551 /* Register callbacks. */
552 int rte_vhost_driver_callback_register(const char *path,
553 struct vhost_device_ops const * const ops);
557 * Start the vhost-user driver.
559 * This function triggers the vhost-user negotiation.
562 * The vhost-user socket file path
564 * 0 on success, -1 on failure
566 int rte_vhost_driver_start(const char *path);
569 * Get the MTU value of the device if set in QEMU.
572 * virtio-net device ID
574 * The variable to store the MTU value
578 * -EAGAIN: device not yet started
579 * -ENOTSUP: device does not support MTU feature
581 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
584 * Get the numa node from which the virtio net device's memory
591 * The numa node, -1 on failure
593 int rte_vhost_get_numa_node(int vid);
597 * Get the number of queues the device supports.
599 * Note this function is deprecated, as it returns a queue pair number,
600 * which is vhost specific. Instead, rte_vhost_get_vring_num should
607 * The number of queues, 0 on failure
610 uint32_t rte_vhost_get_queue_num(int vid);
613 * Get the number of vrings the device supports.
619 * The number of vrings, 0 on failure
621 uint16_t rte_vhost_get_vring_num(int vid);
624 * Get the virtio net device's ifname, which is the vhost-user socket
630 * The buffer to stored the queried ifname
635 * 0 on success, -1 on failure
637 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
640 * Get how many avail entries are left in the queue
648 * num of avail entries left
650 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
655 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
656 * be received from the physical port or from another virtual device. A packet
657 * count is returned to indicate the number of packets that were successfully
658 * added to the RX queue.
662 * virtio queue index in mq case
664 * array to contain packets to be enqueued
666 * packets num to be enqueued
668 * num of packets enqueued
670 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
671 struct rte_mbuf **pkts, uint16_t count);
674 * This function gets guest buffers from the virtio device TX virtqueue,
675 * construct host mbufs, copies guest buffer content to host mbufs and
676 * store them in pkts to be processed.
680 * virtio queue index in mq case
682 * mbuf_pool where host mbuf is allocated.
684 * array to contain packets to be dequeued
686 * packets num to be dequeued
688 * num of packets dequeued
690 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
691 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
694 * Get guest mem table: a list of memory regions.
696 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
697 * guest memory regions. Application should free it at destroy_device()
703 * To store the returned mem regions
705 * 0 on success, -1 on failure
707 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
710 * Get guest vring info, including the vring address, vring size, etc.
717 * the structure to hold the requested vring info
719 * 0 on success, -1 on failure
721 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
722 struct rte_vhost_vring *vring);
725 * Get guest inflight vring info, including inflight ring and resubmit list.
732 * the structure to hold the requested inflight vring info
734 * 0 on success, -1 on failure
738 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
739 struct rte_vhost_ring_inflight *vring);
742 * Set split inflight descriptor.
744 * This function save descriptors that has been comsumed in available
752 * inflight entry index
754 * 0 on success, -1 on failure
758 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
762 * Set packed inflight descriptor and get corresponding inflight entry
764 * This function save descriptors that has been comsumed
771 * head of descriptors
773 * last of descriptors
774 * @param inflight_entry
775 * corresponding inflight entry
777 * 0 on success, -1 on failure
781 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
782 uint16_t head, uint16_t last, uint16_t *inflight_entry);
785 * Save the head of list that the last batch of used descriptors.
792 * descriptor entry index
794 * 0 on success, -1 on failure
798 rte_vhost_set_last_inflight_io_split(int vid,
799 uint16_t vring_idx, uint16_t idx);
802 * Update the inflight free_head, used_idx and used_wrap_counter.
804 * This function will update status first before updating descriptors
812 * head of descriptors
814 * 0 on success, -1 on failure
818 rte_vhost_set_last_inflight_io_packed(int vid,
819 uint16_t vring_idx, uint16_t head);
822 * Clear the split inflight status.
828 * @param last_used_idx
829 * last used idx of used ring
831 * inflight entry index
833 * 0 on success, -1 on failure
837 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
838 uint16_t last_used_idx, uint16_t idx);
841 * Clear the packed inflight status.
848 * inflight entry index
850 * 0 on success, -1 on failure
854 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
858 * Notify the guest that used descriptors have been added to the vring. This
859 * function acts as a memory barrier.
866 * 0 on success, -1 on failure
868 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
871 * Get vhost RX queue avail count.
876 * virtio queue index in mq case
878 * num of desc available
880 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
883 * Get log base and log size of the vhost device
892 * 0 on success, -1 on failure
896 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
899 * Get last_avail/used_idx of the vhost virtqueue
905 * @param last_avail_idx
906 * vhost last_avail_idx to get
907 * @param last_used_idx
908 * vhost last_used_idx to get
910 * 0 on success, -1 on failure
914 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
915 uint16_t *last_avail_idx, uint16_t *last_used_idx);
918 * Get last_avail/last_used of the vhost virtqueue
920 * This function is designed for the reconnection and it's specific for
921 * the packed ring as we can get the two parameters from the inflight
928 * @param last_avail_idx
929 * vhost last_avail_idx to get
930 * @param last_used_idx
931 * vhost last_used_idx to get
933 * 0 on success, -1 on failure
937 rte_vhost_get_vring_base_from_inflight(int vid,
938 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
941 * Set last_avail/used_idx of the vhost virtqueue
947 * @param last_avail_idx
948 * last_avail_idx to set
949 * @param last_used_idx
950 * last_used_idx to set
952 * 0 on success, -1 on failure
956 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
957 uint16_t last_avail_idx, uint16_t last_used_idx);
960 * Register external message handling callbacks
965 * virtio external callbacks to register
967 * additional context passed to the callbacks
969 * 0 on success, -1 on failure
973 rte_vhost_extern_callback_register(int vid,
974 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
977 * Get vdpa device id for vhost device.
982 * vDPA device pointer on success, NULL on failure
985 struct rte_vdpa_device *
986 rte_vhost_get_vdpa_device(int vid);
989 * Notify the guest that should get virtio configuration space from backend.
994 * wait for the master response the status of this operation
996 * 0 on success, < 0 on failure
1000 rte_vhost_slave_config_change(int vid, bool need_reply);
1006 #endif /* _RTE_VHOST_H_ */