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_resubmit_desc {
154 struct rte_vhost_resubmit_info {
155 struct rte_vhost_resubmit_desc *resubmit_list;
156 uint16_t resubmit_num;
159 struct rte_vhost_ring_inflight {
161 struct rte_vhost_inflight_info_split *inflight_split;
162 struct rte_vhost_inflight_info_packed *inflight_packed;
165 struct rte_vhost_resubmit_info *resubmit_inflight;
168 struct rte_vhost_vring {
170 struct vring_desc *desc;
171 struct vring_packed_desc *desc_packed;
174 struct vring_avail *avail;
175 struct vring_packed_desc_event *driver_event;
178 struct vring_used *used;
179 struct vring_packed_desc_event *device_event;
181 uint64_t log_guest_addr;
183 /** Deprecated, use rte_vhost_vring_call() instead. */
191 * Possible results of the vhost user message handling callbacks
193 enum rte_vhost_msg_result {
194 /* Message handling failed */
195 RTE_VHOST_MSG_RESULT_ERR = -1,
196 /* Message handling successful */
197 RTE_VHOST_MSG_RESULT_OK = 0,
198 /* Message handling successful and reply prepared */
199 RTE_VHOST_MSG_RESULT_REPLY = 1,
200 /* Message not handled */
201 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
205 * Function prototype for the vhost backend to handle specific vhost user
213 * RTE_VHOST_MSG_RESULT_OK on success,
214 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
215 * RTE_VHOST_MSG_RESULT_ERR on failure,
216 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
218 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
221 * Optional vhost user message handlers.
223 struct rte_vhost_user_extern_ops {
224 /* Called prior to the master message handling. */
225 rte_vhost_msg_handle pre_msg_handle;
226 /* Called after the master message handling. */
227 rte_vhost_msg_handle post_msg_handle;
231 * Device and vring operations.
233 struct vhost_device_ops {
234 int (*new_device)(int vid); /**< Add device. */
235 void (*destroy_device)(int vid); /**< Remove device. */
237 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
240 * Features could be changed after the feature negotiation.
241 * For example, VHOST_F_LOG_ALL will be set/cleared at the
242 * start/end of live migration, respectively. This callback
243 * is used to inform the application on such change.
245 int (*features_changed)(int vid, uint64_t features);
247 int (*new_connection)(int vid);
248 void (*destroy_connection)(int vid);
251 * This callback gets called each time a guest gets notified
252 * about waiting packets. This is the interrupt handling trough
253 * the eventfd_write(callfd), which can be used for counting these
256 void (*guest_notified)(int vid);
258 void *reserved[1]; /**< Reserved for future extension */
262 * Convert guest physical address to host virtual address
264 * This function is deprecated because unsafe.
265 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
266 * guest physical ranges are fully and contiguously mapped into
267 * process virtual address space.
270 * the guest memory regions
272 * the guest physical address for querying
274 * the host virtual address on success, 0 on failure
277 static __rte_always_inline uint64_t
278 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
280 struct rte_vhost_mem_region *reg;
283 for (i = 0; i < mem->nregions; i++) {
284 reg = &mem->regions[i];
285 if (gpa >= reg->guest_phys_addr &&
286 gpa < reg->guest_phys_addr + reg->size) {
287 return gpa - reg->guest_phys_addr +
296 * Convert guest physical address to host virtual address safely
298 * This variant of rte_vhost_gpa_to_vva() takes care all the
299 * requested length is mapped and contiguous in process address
303 * the guest memory regions
305 * the guest physical address for querying
307 * the size of the requested area to map, updated with actual size mapped
309 * the host virtual address on success, 0 on failure
312 static __rte_always_inline uint64_t
313 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
314 uint64_t gpa, uint64_t *len)
316 struct rte_vhost_mem_region *r;
319 for (i = 0; i < mem->nregions; i++) {
320 r = &mem->regions[i];
321 if (gpa >= r->guest_phys_addr &&
322 gpa < r->guest_phys_addr + r->size) {
324 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
325 *len = r->guest_phys_addr + r->size - gpa;
327 return gpa - r->guest_phys_addr +
336 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
339 * Log the memory write start with given address.
341 * This function only need be invoked when the live migration starts.
342 * Therefore, we won't need call it at all in the most of time. For
343 * making the performance impact be minimum, it's suggested to do a
344 * check before calling it:
346 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
347 * rte_vhost_log_write(vid, addr, len);
352 * the starting address for write (in guest physical address space)
354 * the length to write
356 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
359 * Log the used ring update start at given offset.
361 * Same as rte_vhost_log_write, it's suggested to do a check before
364 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
365 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
372 * the offset inside the used ring
374 * the length to write
376 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
377 uint64_t offset, uint64_t len);
379 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
382 * Register vhost driver. path could be different for multiple
385 int rte_vhost_driver_register(const char *path, uint64_t flags);
387 /* Unregister vhost driver. This is only meaningful to vhost user. */
388 int rte_vhost_driver_unregister(const char *path);
391 * Set the vdpa device id, enforce single connection per socket
394 * The vhost-user socket file path
398 * 0 on success, -1 on failure
402 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
405 * Unset the vdpa device id
408 * The vhost-user socket file path
410 * 0 on success, -1 on failure
414 rte_vhost_driver_detach_vdpa_device(const char *path);
420 * The vhost-user socket file path
422 * Device id, -1 on failure
426 rte_vhost_driver_get_vdpa_device_id(const char *path);
429 * Set the feature bits the vhost-user driver supports.
432 * The vhost-user socket file path
436 * 0 on success, -1 on failure
438 int rte_vhost_driver_set_features(const char *path, uint64_t features);
441 * Enable vhost-user driver features.
444 * - the param features should be a subset of the feature bits provided
445 * by rte_vhost_driver_set_features().
446 * - it must be invoked before vhost-user negotiation starts.
449 * The vhost-user socket file path
453 * 0 on success, -1 on failure
455 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
458 * Disable vhost-user driver features.
460 * The two notes at rte_vhost_driver_enable_features() also apply here.
463 * The vhost-user socket file path
465 * Features to disable
467 * 0 on success, -1 on failure
469 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
472 * Get the feature bits before feature negotiation.
475 * The vhost-user socket file path
477 * A pointer to store the queried feature bits
479 * 0 on success, -1 on failure
481 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
484 * Set the protocol feature bits before feature negotiation.
487 * The vhost-user socket file path
488 * @param protocol_features
489 * Supported protocol features
491 * 0 on success, -1 on failure
495 rte_vhost_driver_set_protocol_features(const char *path,
496 uint64_t protocol_features);
499 * Get the protocol feature bits before feature negotiation.
502 * The vhost-user socket file path
503 * @param protocol_features
504 * A pointer to store the queried protocol feature bits
506 * 0 on success, -1 on failure
510 rte_vhost_driver_get_protocol_features(const char *path,
511 uint64_t *protocol_features);
514 * Get the queue number bits before feature negotiation.
517 * The vhost-user socket file path
519 * A pointer to store the queried queue number bits
521 * 0 on success, -1 on failure
525 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
528 * Get the feature bits after negotiation
533 * A pointer to store the queried feature bits
535 * 0 on success, -1 on failure
537 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
539 /* Register callbacks. */
540 int rte_vhost_driver_callback_register(const char *path,
541 struct vhost_device_ops const * const ops);
545 * Start the vhost-user driver.
547 * This function triggers the vhost-user negotiation.
550 * The vhost-user socket file path
552 * 0 on success, -1 on failure
554 int rte_vhost_driver_start(const char *path);
557 * Get the MTU value of the device if set in QEMU.
560 * virtio-net device ID
562 * The variable to store the MTU value
566 * -EAGAIN: device not yet started
567 * -ENOTSUP: device does not support MTU feature
569 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
572 * Get the numa node from which the virtio net device's memory
579 * The numa node, -1 on failure
581 int rte_vhost_get_numa_node(int vid);
585 * Get the number of queues the device supports.
587 * Note this function is deprecated, as it returns a queue pair number,
588 * which is vhost specific. Instead, rte_vhost_get_vring_num should
595 * The number of queues, 0 on failure
598 uint32_t rte_vhost_get_queue_num(int vid);
601 * Get the number of vrings the device supports.
607 * The number of vrings, 0 on failure
609 uint16_t rte_vhost_get_vring_num(int vid);
612 * Get the virtio net device's ifname, which is the vhost-user socket
618 * The buffer to stored the queried ifname
623 * 0 on success, -1 on failure
625 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
628 * Get how many avail entries are left in the queue
636 * num of avail entries left
638 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
643 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
644 * be received from the physical port or from another virtual device. A packet
645 * count is returned to indicate the number of packets that were successfully
646 * added to the RX queue.
650 * virtio queue index in mq case
652 * array to contain packets to be enqueued
654 * packets num to be enqueued
656 * num of packets enqueued
658 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
659 struct rte_mbuf **pkts, uint16_t count);
662 * This function gets guest buffers from the virtio device TX virtqueue,
663 * construct host mbufs, copies guest buffer content to host mbufs and
664 * store them in pkts to be processed.
668 * virtio queue index in mq case
670 * mbuf_pool where host mbuf is allocated.
672 * array to contain packets to be dequeued
674 * packets num to be dequeued
676 * num of packets dequeued
678 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
679 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
682 * Get guest mem table: a list of memory regions.
684 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
685 * guest memory regions. Application should free it at destroy_device()
691 * To store the returned mem regions
693 * 0 on success, -1 on failure
695 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
698 * Get guest vring info, including the vring address, vring size, etc.
705 * the structure to hold the requested vring info
707 * 0 on success, -1 on failure
709 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
710 struct rte_vhost_vring *vring);
713 * Get guest inflight vring info, including inflight ring and resubmit list.
720 * the structure to hold the requested inflight vring info
722 * 0 on success, -1 on failure
726 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
727 struct rte_vhost_ring_inflight *vring);
730 * Set split inflight descriptor.
732 * This function save descriptors that has been comsumed in available
740 * inflight entry index
742 * 0 on success, -1 on failure
746 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
750 * Set packed inflight descriptor and get corresponding inflight entry
752 * This function save descriptors that has been comsumed
759 * head of descriptors
761 * last of descriptors
762 * @param inflight_entry
763 * corresponding inflight entry
765 * 0 on success, -1 on failure
769 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
770 uint16_t head, uint16_t last, uint16_t *inflight_entry);
773 * Save the head of list that the last batch of used descriptors.
780 * descriptor entry index
782 * 0 on success, -1 on failure
786 rte_vhost_set_last_inflight_io_split(int vid,
787 uint16_t vring_idx, uint16_t idx);
790 * Update the inflight free_head, used_idx and used_wrap_counter.
792 * This function will update status first before updating descriptors
800 * head of descriptors
802 * 0 on success, -1 on failure
806 rte_vhost_set_last_inflight_io_packed(int vid,
807 uint16_t vring_idx, uint16_t head);
810 * Clear the split inflight status.
816 * @param last_used_idx
817 * last used idx of used ring
819 * inflight entry index
821 * 0 on success, -1 on failure
825 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
826 uint16_t last_used_idx, uint16_t idx);
829 * Clear the packed inflight status.
836 * inflight entry index
838 * 0 on success, -1 on failure
842 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
846 * Notify the guest that used descriptors have been added to the vring. This
847 * function acts as a memory barrier.
854 * 0 on success, -1 on failure
856 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
859 * Get vhost RX queue avail count.
864 * virtio queue index in mq case
866 * num of desc available
868 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
871 * Get log base and log size of the vhost device
880 * 0 on success, -1 on failure
884 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
887 * Get last_avail/used_idx of the vhost virtqueue
893 * @param last_avail_idx
894 * vhost last_avail_idx to get
895 * @param last_used_idx
896 * vhost last_used_idx to get
898 * 0 on success, -1 on failure
902 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
903 uint16_t *last_avail_idx, uint16_t *last_used_idx);
906 * Get last_avail/last_used of the vhost virtqueue
908 * This function is designed for the reconnection and it's specific for
909 * the packed ring as we can get the two parameters from the inflight
916 * @param last_avail_idx
917 * vhost last_avail_idx to get
918 * @param last_used_idx
919 * vhost last_used_idx to get
921 * 0 on success, -1 on failure
925 rte_vhost_get_vring_base_from_inflight(int vid,
926 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
929 * Set last_avail/used_idx of the vhost virtqueue
935 * @param last_avail_idx
936 * last_avail_idx to set
937 * @param last_used_idx
938 * last_used_idx to set
940 * 0 on success, -1 on failure
944 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
945 uint16_t last_avail_idx, uint16_t last_used_idx);
948 * Register external message handling callbacks
953 * virtio external callbacks to register
955 * additional context passed to the callbacks
957 * 0 on success, -1 on failure
961 rte_vhost_extern_callback_register(int vid,
962 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
965 * Get vdpa device id for vhost device.
974 rte_vhost_get_vdpa_device_id(int vid);
980 #endif /* _RTE_VHOST_H_ */