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>
25 /* These are not C++-aware. */
26 #include <linux/vhost.h>
27 #include <linux/virtio_ring.h>
28 #include <linux/virtio_net.h>
31 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
32 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
33 #define RTE_VHOST_USER_RESERVED_1 (1ULL << 2)
34 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
35 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
36 /* support mbuf with external buffer attached */
37 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
38 /* support only linear buffers (no chained mbufs) */
39 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
40 #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7)
41 #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS (1ULL << 8)
44 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
45 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
48 #ifndef VIRTIO_NET_F_MQ
49 #define VIRTIO_NET_F_MQ 22
52 #ifndef VIRTIO_NET_F_MTU
53 #define VIRTIO_NET_F_MTU 3
56 #ifndef VIRTIO_F_ANY_LAYOUT
57 #define VIRTIO_F_ANY_LAYOUT 27
60 /** Protocol features. */
61 #ifndef VHOST_USER_PROTOCOL_F_MQ
62 #define VHOST_USER_PROTOCOL_F_MQ 0
65 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
66 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
69 #ifndef VHOST_USER_PROTOCOL_F_RARP
70 #define VHOST_USER_PROTOCOL_F_RARP 2
73 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
74 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
77 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
78 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
81 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
82 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
85 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
86 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
89 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
90 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
93 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
94 #define VHOST_USER_PROTOCOL_F_CONFIG 9
97 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
98 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
101 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
102 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
105 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
106 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
109 #ifndef VHOST_USER_PROTOCOL_F_STATUS
110 #define VHOST_USER_PROTOCOL_F_STATUS 16
113 /** Indicate whether protocol features negotiation is supported. */
114 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
115 #define VHOST_USER_F_PROTOCOL_FEATURES 30
118 struct rte_vdpa_device;
121 * Information relating to memory regions including offsets to
122 * addresses in QEMUs memory file.
124 struct rte_vhost_mem_region {
125 uint64_t guest_phys_addr;
126 uint64_t guest_user_addr;
127 uint64_t host_user_addr;
135 * Memory structure includes region and mapping information.
137 struct rte_vhost_memory {
139 struct rte_vhost_mem_region regions[];
142 struct rte_vhost_inflight_desc_split {
149 struct rte_vhost_inflight_info_split {
153 uint16_t last_inflight_io;
155 struct rte_vhost_inflight_desc_split desc[0];
158 struct rte_vhost_inflight_desc_packed {
171 struct rte_vhost_inflight_info_packed {
176 uint16_t old_free_head;
178 uint16_t old_used_idx;
179 uint8_t used_wrap_counter;
180 uint8_t old_used_wrap_counter;
182 struct rte_vhost_inflight_desc_packed desc[0];
185 struct rte_vhost_resubmit_desc {
190 struct rte_vhost_resubmit_info {
191 struct rte_vhost_resubmit_desc *resubmit_list;
192 uint16_t resubmit_num;
195 struct rte_vhost_ring_inflight {
197 struct rte_vhost_inflight_info_split *inflight_split;
198 struct rte_vhost_inflight_info_packed *inflight_packed;
201 struct rte_vhost_resubmit_info *resubmit_inflight;
204 struct rte_vhost_vring {
206 struct vring_desc *desc;
207 struct vring_packed_desc *desc_packed;
210 struct vring_avail *avail;
211 struct vring_packed_desc_event *driver_event;
214 struct vring_used *used;
215 struct vring_packed_desc_event *device_event;
217 uint64_t log_guest_addr;
219 /** Deprecated, use rte_vhost_vring_call() instead. */
227 * Possible results of the vhost user message handling callbacks
229 enum rte_vhost_msg_result {
230 /* Message handling failed */
231 RTE_VHOST_MSG_RESULT_ERR = -1,
232 /* Message handling successful */
233 RTE_VHOST_MSG_RESULT_OK = 0,
234 /* Message handling successful and reply prepared */
235 RTE_VHOST_MSG_RESULT_REPLY = 1,
236 /* Message not handled */
237 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
241 * Function prototype for the vhost backend to handle specific vhost user
249 * RTE_VHOST_MSG_RESULT_OK on success,
250 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
251 * RTE_VHOST_MSG_RESULT_ERR on failure,
252 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
254 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
257 * Optional vhost user message handlers.
259 struct rte_vhost_user_extern_ops {
260 /* Called prior to the master message handling. */
261 rte_vhost_msg_handle pre_msg_handle;
262 /* Called after the master message handling. */
263 rte_vhost_msg_handle post_msg_handle;
267 * Device and vring operations.
269 struct rte_vhost_device_ops {
270 int (*new_device)(int vid); /**< Add device. */
271 void (*destroy_device)(int vid); /**< Remove device. */
273 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
276 * Features could be changed after the feature negotiation.
277 * For example, VHOST_F_LOG_ALL will be set/cleared at the
278 * start/end of live migration, respectively. This callback
279 * is used to inform the application on such change.
281 int (*features_changed)(int vid, uint64_t features);
283 int (*new_connection)(int vid);
284 void (*destroy_connection)(int vid);
287 * This callback gets called each time a guest gets notified
288 * about waiting packets. This is the interrupt handling through
289 * the eventfd_write(callfd), which can be used for counting these
292 void (*guest_notified)(int vid);
294 void *reserved[1]; /**< Reserved for future extension */
298 * Power monitor condition.
300 struct rte_vhost_power_monitor_cond {
301 /**< Address to monitor for changes */
303 /**< If the `mask` is non-zero, location pointed
304 * to by `addr` will be read and masked, then
305 * compared with this value.
308 /**< 64-bit mask to extract value read from `addr` */
310 /**< Data size (in bytes) that will be read from the
311 * monitored memory location (`addr`).
314 /**< If 1, and masked value that read from 'addr' equals
315 * 'val', the driver should skip core sleep. If 0, and
316 * masked value that read from 'addr' does not equal 'val',
317 * the driver should skip core sleep.
323 * Convert guest physical address to host virtual address
325 * This function is deprecated because unsafe.
326 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
327 * guest physical ranges are fully and contiguously mapped into
328 * process virtual address space.
331 * the guest memory regions
333 * the guest physical address for querying
335 * the host virtual address on success, 0 on failure
338 static __rte_always_inline uint64_t
339 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
341 struct rte_vhost_mem_region *reg;
344 for (i = 0; i < mem->nregions; i++) {
345 reg = &mem->regions[i];
346 if (gpa >= reg->guest_phys_addr &&
347 gpa < reg->guest_phys_addr + reg->size) {
348 return gpa - reg->guest_phys_addr +
357 * Convert guest physical address to host virtual address safely
359 * This variant of rte_vhost_gpa_to_vva() takes care all the
360 * requested length is mapped and contiguous in process address
364 * the guest memory regions
366 * the guest physical address for querying
368 * the size of the requested area to map, updated with actual size mapped
370 * the host virtual address on success, 0 on failure
372 static __rte_always_inline uint64_t
373 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
374 uint64_t gpa, uint64_t *len)
376 struct rte_vhost_mem_region *r;
379 for (i = 0; i < mem->nregions; i++) {
380 r = &mem->regions[i];
381 if (gpa >= r->guest_phys_addr &&
382 gpa < r->guest_phys_addr + r->size) {
384 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
385 *len = r->guest_phys_addr + r->size - gpa;
387 return gpa - r->guest_phys_addr +
396 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
399 * Log the memory write start with given address.
401 * This function only need be invoked when the live migration starts.
402 * Therefore, we won't need call it at all in the most of time. For
403 * making the performance impact be minimum, it's suggested to do a
404 * check before calling it:
406 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
407 * rte_vhost_log_write(vid, addr, len);
412 * the starting address for write (in guest physical address space)
414 * the length to write
416 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
419 * Log the used ring update start at given offset.
421 * Same as rte_vhost_log_write, it's suggested to do a check before
424 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
425 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
432 * the offset inside the used ring
434 * the length to write
436 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
437 uint64_t offset, uint64_t len);
439 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
442 * Register vhost driver. path could be different for multiple
445 int rte_vhost_driver_register(const char *path, uint64_t flags);
447 /* Unregister vhost driver. This is only meaningful to vhost user. */
448 int rte_vhost_driver_unregister(const char *path);
451 * Set the vdpa device id, enforce single connection per socket
454 * The vhost-user socket file path
456 * vDPA device pointer
458 * 0 on success, -1 on failure
461 rte_vhost_driver_attach_vdpa_device(const char *path,
462 struct rte_vdpa_device *dev);
465 * Unset the vdpa device id
468 * The vhost-user socket file path
470 * 0 on success, -1 on failure
473 rte_vhost_driver_detach_vdpa_device(const char *path);
479 * The vhost-user socket file path
481 * vDPA device pointer, NULL on failure
483 struct rte_vdpa_device *
484 rte_vhost_driver_get_vdpa_device(const char *path);
487 * Set the feature bits the vhost-user driver supports.
490 * The vhost-user socket file path
494 * 0 on success, -1 on failure
496 int rte_vhost_driver_set_features(const char *path, uint64_t features);
499 * Enable vhost-user driver features.
502 * - the param features should be a subset of the feature bits provided
503 * by rte_vhost_driver_set_features().
504 * - it must be invoked before vhost-user negotiation starts.
507 * The vhost-user socket file path
511 * 0 on success, -1 on failure
513 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
516 * Disable vhost-user driver features.
518 * The two notes at rte_vhost_driver_enable_features() also apply here.
521 * The vhost-user socket file path
523 * Features to disable
525 * 0 on success, -1 on failure
527 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
530 * Get the feature bits before feature negotiation.
533 * The vhost-user socket file path
535 * A pointer to store the queried feature bits
537 * 0 on success, -1 on failure
539 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
542 * Set the protocol feature bits before feature negotiation.
545 * The vhost-user socket file path
546 * @param protocol_features
547 * Supported protocol features
549 * 0 on success, -1 on failure
552 rte_vhost_driver_set_protocol_features(const char *path,
553 uint64_t protocol_features);
556 * Get the protocol feature bits before feature negotiation.
559 * The vhost-user socket file path
560 * @param protocol_features
561 * A pointer to store the queried protocol feature bits
563 * 0 on success, -1 on failure
566 rte_vhost_driver_get_protocol_features(const char *path,
567 uint64_t *protocol_features);
570 * Get the queue number bits before feature negotiation.
573 * The vhost-user socket file path
575 * A pointer to store the queried queue number bits
577 * 0 on success, -1 on failure
580 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
583 * Get the feature bits after negotiation
588 * A pointer to store the queried feature bits
590 * 0 on success, -1 on failure
592 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
595 * Get the protocol feature bits after negotiation
599 * @param protocol_features
600 * A pointer to store the queried protocol feature bits
602 * 0 on success, -1 on failure
606 rte_vhost_get_negotiated_protocol_features(int vid,
607 uint64_t *protocol_features);
609 /* Register callbacks. */
610 int rte_vhost_driver_callback_register(const char *path,
611 struct rte_vhost_device_ops const * const ops);
615 * Start the vhost-user driver.
617 * This function triggers the vhost-user negotiation.
620 * The vhost-user socket file path
622 * 0 on success, -1 on failure
624 int rte_vhost_driver_start(const char *path);
627 * Get the MTU value of the device if set in QEMU.
630 * virtio-net device ID
632 * The variable to store the MTU value
636 * -EAGAIN: device not yet started
637 * -ENOTSUP: device does not support MTU feature
639 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
642 * Get the numa node from which the virtio net device's memory
649 * The numa node, -1 on failure
651 int rte_vhost_get_numa_node(int vid);
655 * Get the number of queues the device supports.
657 * Note this function is deprecated, as it returns a queue pair number,
658 * which is vhost specific. Instead, rte_vhost_get_vring_num should
665 * The number of queues, 0 on failure
668 uint32_t rte_vhost_get_queue_num(int vid);
671 * Get the number of vrings the device supports.
677 * The number of vrings, 0 on failure
679 uint16_t rte_vhost_get_vring_num(int vid);
682 * Get the virtio net device's ifname, which is the vhost-user socket
688 * The buffer to stored the queried ifname
693 * 0 on success, -1 on failure
695 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
698 * Get how many avail entries are left in the queue
706 * num of avail entries left
708 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
713 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
714 * be received from the physical port or from another virtual device. A packet
715 * count is returned to indicate the number of packets that were successfully
716 * added to the RX queue.
720 * virtio queue index in mq case
722 * array to contain packets to be enqueued
724 * packets num to be enqueued
726 * num of packets enqueued
728 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
729 struct rte_mbuf **pkts, uint16_t count);
732 * This function gets guest buffers from the virtio device TX virtqueue,
733 * construct host mbufs, copies guest buffer content to host mbufs and
734 * store them in pkts to be processed.
738 * virtio queue index in mq case
740 * mbuf_pool where host mbuf is allocated.
742 * array to contain packets to be dequeued
744 * packets num to be dequeued
746 * num of packets dequeued
748 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
749 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
752 * Get guest mem table: a list of memory regions.
754 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
755 * guest memory regions. Application should free it at destroy_device()
761 * To store the returned mem regions
763 * 0 on success, -1 on failure
765 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
768 * Get guest vring info, including the vring address, vring size, etc.
775 * the structure to hold the requested vring info
777 * 0 on success, -1 on failure
779 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
780 struct rte_vhost_vring *vring);
783 * Get guest inflight vring info, including inflight ring and resubmit list.
790 * the structure to hold the requested inflight vring info
792 * 0 on success, -1 on failure
795 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
796 struct rte_vhost_ring_inflight *vring);
799 * Set split inflight descriptor.
801 * This function save descriptors that has been consumed in available
809 * inflight entry index
811 * 0 on success, -1 on failure
814 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
818 * Set packed inflight descriptor and get corresponding inflight entry
820 * This function save descriptors that has been consumed
827 * head of descriptors
829 * last of descriptors
830 * @param inflight_entry
831 * corresponding inflight entry
833 * 0 on success, -1 on failure
836 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
837 uint16_t head, uint16_t last, uint16_t *inflight_entry);
840 * Save the head of list that the last batch of used descriptors.
847 * descriptor entry index
849 * 0 on success, -1 on failure
852 rte_vhost_set_last_inflight_io_split(int vid,
853 uint16_t vring_idx, uint16_t idx);
856 * Update the inflight free_head, used_idx and used_wrap_counter.
858 * This function will update status first before updating descriptors
866 * head of descriptors
868 * 0 on success, -1 on failure
871 rte_vhost_set_last_inflight_io_packed(int vid,
872 uint16_t vring_idx, uint16_t head);
875 * Clear the split inflight status.
881 * @param last_used_idx
882 * last used idx of used ring
884 * inflight entry index
886 * 0 on success, -1 on failure
889 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
890 uint16_t last_used_idx, uint16_t idx);
893 * Clear the packed inflight status.
900 * inflight entry index
902 * 0 on success, -1 on failure
905 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
909 * Notify the guest that used descriptors have been added to the vring. This
910 * function acts as a memory barrier.
917 * 0 on success, -1 on failure
919 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
922 * Get vhost RX queue avail count.
927 * virtio queue index in mq case
929 * num of desc available
931 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
934 * Get power monitor address of the vhost device
941 * power monitor condition
943 * 0 on success, -1 on failure
947 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
948 struct rte_vhost_power_monitor_cond *pmc);
951 * Get log base and log size of the vhost device
960 * 0 on success, -1 on failure
963 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
966 * Get last_avail/used_idx of the vhost virtqueue
972 * @param last_avail_idx
973 * vhost last_avail_idx to get
974 * @param last_used_idx
975 * vhost last_used_idx to get
977 * 0 on success, -1 on failure
980 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
981 uint16_t *last_avail_idx, uint16_t *last_used_idx);
984 * Get last_avail/last_used of the vhost virtqueue
986 * This function is designed for the reconnection and it's specific for
987 * the packed ring as we can get the two parameters from the inflight
994 * @param last_avail_idx
995 * vhost last_avail_idx to get
996 * @param last_used_idx
997 * vhost last_used_idx to get
999 * 0 on success, -1 on failure
1002 rte_vhost_get_vring_base_from_inflight(int vid,
1003 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1006 * Set last_avail/used_idx of the vhost virtqueue
1012 * @param last_avail_idx
1013 * last_avail_idx to set
1014 * @param last_used_idx
1015 * last_used_idx to set
1017 * 0 on success, -1 on failure
1020 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1021 uint16_t last_avail_idx, uint16_t last_used_idx);
1024 * Register external message handling callbacks
1029 * virtio external callbacks to register
1031 * additional context passed to the callbacks
1033 * 0 on success, -1 on failure
1036 rte_vhost_extern_callback_register(int vid,
1037 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1040 * Get vdpa device id for vhost device.
1045 * vDPA device pointer on success, NULL on failure
1047 struct rte_vdpa_device *
1048 rte_vhost_get_vdpa_device(int vid);
1051 * Notify the guest that should get virtio configuration space from backend.
1056 * wait for the master response the status of this operation
1058 * 0 on success, < 0 on failure
1062 rte_vhost_slave_config_change(int vid, bool need_reply);
1068 #endif /* _RTE_VHOST_H_ */