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_SLAVE_SEND_FD
73 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
76 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
77 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
80 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
81 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
84 /** Indicate whether protocol features negotiation is supported. */
85 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
86 #define VHOST_USER_F_PROTOCOL_FEATURES 30
90 * Information relating to memory regions including offsets to
91 * addresses in QEMUs memory file.
93 struct rte_vhost_mem_region {
94 uint64_t guest_phys_addr;
95 uint64_t guest_user_addr;
96 uint64_t host_user_addr;
104 * Memory structure includes region and mapping information.
106 struct rte_vhost_memory {
108 struct rte_vhost_mem_region regions[];
111 struct rte_vhost_inflight_desc_split {
118 struct rte_vhost_inflight_info_split {
122 uint16_t last_inflight_io;
124 struct rte_vhost_inflight_desc_split desc[0];
127 struct rte_vhost_inflight_desc_packed {
140 struct rte_vhost_inflight_info_packed {
145 uint16_t old_free_head;
147 uint16_t old_used_idx;
148 uint8_t used_wrap_counter;
149 uint8_t old_used_wrap_counter;
151 struct rte_vhost_inflight_desc_packed desc[0];
154 struct rte_vhost_resubmit_desc {
159 struct rte_vhost_resubmit_info {
160 struct rte_vhost_resubmit_desc *resubmit_list;
161 uint16_t resubmit_num;
164 struct rte_vhost_ring_inflight {
166 struct rte_vhost_inflight_info_split *inflight_split;
167 struct rte_vhost_inflight_info_packed *inflight_packed;
170 struct rte_vhost_resubmit_info *resubmit_inflight;
173 struct rte_vhost_vring {
175 struct vring_desc *desc;
176 struct vring_packed_desc *desc_packed;
179 struct vring_avail *avail;
180 struct vring_packed_desc_event *driver_event;
183 struct vring_used *used;
184 struct vring_packed_desc_event *device_event;
186 uint64_t log_guest_addr;
188 /** Deprecated, use rte_vhost_vring_call() instead. */
196 * Possible results of the vhost user message handling callbacks
198 enum rte_vhost_msg_result {
199 /* Message handling failed */
200 RTE_VHOST_MSG_RESULT_ERR = -1,
201 /* Message handling successful */
202 RTE_VHOST_MSG_RESULT_OK = 0,
203 /* Message handling successful and reply prepared */
204 RTE_VHOST_MSG_RESULT_REPLY = 1,
205 /* Message not handled */
206 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
210 * Function prototype for the vhost backend to handle specific vhost user
218 * RTE_VHOST_MSG_RESULT_OK on success,
219 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
220 * RTE_VHOST_MSG_RESULT_ERR on failure,
221 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
223 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
226 * Optional vhost user message handlers.
228 struct rte_vhost_user_extern_ops {
229 /* Called prior to the master message handling. */
230 rte_vhost_msg_handle pre_msg_handle;
231 /* Called after the master message handling. */
232 rte_vhost_msg_handle post_msg_handle;
236 * Device and vring operations.
238 struct vhost_device_ops {
239 int (*new_device)(int vid); /**< Add device. */
240 void (*destroy_device)(int vid); /**< Remove device. */
242 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
245 * Features could be changed after the feature negotiation.
246 * For example, VHOST_F_LOG_ALL will be set/cleared at the
247 * start/end of live migration, respectively. This callback
248 * is used to inform the application on such change.
250 int (*features_changed)(int vid, uint64_t features);
252 int (*new_connection)(int vid);
253 void (*destroy_connection)(int vid);
256 * This callback gets called each time a guest gets notified
257 * about waiting packets. This is the interrupt handling trough
258 * the eventfd_write(callfd), which can be used for counting these
261 void (*guest_notified)(int vid);
263 void *reserved[1]; /**< Reserved for future extension */
267 * Convert guest physical address to host virtual address
269 * This function is deprecated because unsafe.
270 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
271 * guest physical ranges are fully and contiguously mapped into
272 * process virtual address space.
275 * the guest memory regions
277 * the guest physical address for querying
279 * the host virtual address on success, 0 on failure
282 static __rte_always_inline uint64_t
283 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
285 struct rte_vhost_mem_region *reg;
288 for (i = 0; i < mem->nregions; i++) {
289 reg = &mem->regions[i];
290 if (gpa >= reg->guest_phys_addr &&
291 gpa < reg->guest_phys_addr + reg->size) {
292 return gpa - reg->guest_phys_addr +
301 * Convert guest physical address to host virtual address safely
303 * This variant of rte_vhost_gpa_to_vva() takes care all the
304 * requested length is mapped and contiguous in process address
308 * the guest memory regions
310 * the guest physical address for querying
312 * the size of the requested area to map, updated with actual size mapped
314 * the host virtual address on success, 0 on failure
317 static __rte_always_inline uint64_t
318 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
319 uint64_t gpa, uint64_t *len)
321 struct rte_vhost_mem_region *r;
324 for (i = 0; i < mem->nregions; i++) {
325 r = &mem->regions[i];
326 if (gpa >= r->guest_phys_addr &&
327 gpa < r->guest_phys_addr + r->size) {
329 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
330 *len = r->guest_phys_addr + r->size - gpa;
332 return gpa - r->guest_phys_addr +
341 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
344 * Log the memory write start with given address.
346 * This function only need be invoked when the live migration starts.
347 * Therefore, we won't need call it at all in the most of time. For
348 * making the performance impact be minimum, it's suggested to do a
349 * check before calling it:
351 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
352 * rte_vhost_log_write(vid, addr, len);
357 * the starting address for write (in guest physical address space)
359 * the length to write
361 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
364 * Log the used ring update start at given offset.
366 * Same as rte_vhost_log_write, it's suggested to do a check before
369 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
370 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
377 * the offset inside the used ring
379 * the length to write
381 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
382 uint64_t offset, uint64_t len);
384 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
387 * Register vhost driver. path could be different for multiple
390 int rte_vhost_driver_register(const char *path, uint64_t flags);
392 /* Unregister vhost driver. This is only meaningful to vhost user. */
393 int rte_vhost_driver_unregister(const char *path);
396 * Set the vdpa device id, enforce single connection per socket
399 * The vhost-user socket file path
403 * 0 on success, -1 on failure
407 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
410 * Unset the vdpa device id
413 * The vhost-user socket file path
415 * 0 on success, -1 on failure
419 rte_vhost_driver_detach_vdpa_device(const char *path);
425 * The vhost-user socket file path
427 * Device id, -1 on failure
431 rte_vhost_driver_get_vdpa_device_id(const char *path);
434 * Set the feature bits the vhost-user driver supports.
437 * The vhost-user socket file path
441 * 0 on success, -1 on failure
443 int rte_vhost_driver_set_features(const char *path, uint64_t features);
446 * Enable vhost-user driver features.
449 * - the param features should be a subset of the feature bits provided
450 * by rte_vhost_driver_set_features().
451 * - it must be invoked before vhost-user negotiation starts.
454 * The vhost-user socket file path
458 * 0 on success, -1 on failure
460 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
463 * Disable vhost-user driver features.
465 * The two notes at rte_vhost_driver_enable_features() also apply here.
468 * The vhost-user socket file path
470 * Features to disable
472 * 0 on success, -1 on failure
474 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
477 * Get the feature bits before feature negotiation.
480 * The vhost-user socket file path
482 * A pointer to store the queried feature bits
484 * 0 on success, -1 on failure
486 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
489 * Set the protocol feature bits before feature negotiation.
492 * The vhost-user socket file path
493 * @param protocol_features
494 * Supported protocol features
496 * 0 on success, -1 on failure
500 rte_vhost_driver_set_protocol_features(const char *path,
501 uint64_t protocol_features);
504 * Get the protocol feature bits before feature negotiation.
507 * The vhost-user socket file path
508 * @param protocol_features
509 * A pointer to store the queried protocol feature bits
511 * 0 on success, -1 on failure
515 rte_vhost_driver_get_protocol_features(const char *path,
516 uint64_t *protocol_features);
519 * Get the queue number bits before feature negotiation.
522 * The vhost-user socket file path
524 * A pointer to store the queried queue number bits
526 * 0 on success, -1 on failure
530 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
533 * Get the feature bits after negotiation
538 * A pointer to store the queried feature bits
540 * 0 on success, -1 on failure
542 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
544 /* Register callbacks. */
545 int rte_vhost_driver_callback_register(const char *path,
546 struct vhost_device_ops const * const ops);
550 * Start the vhost-user driver.
552 * This function triggers the vhost-user negotiation.
555 * The vhost-user socket file path
557 * 0 on success, -1 on failure
559 int rte_vhost_driver_start(const char *path);
562 * Get the MTU value of the device if set in QEMU.
565 * virtio-net device ID
567 * The variable to store the MTU value
571 * -EAGAIN: device not yet started
572 * -ENOTSUP: device does not support MTU feature
574 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
577 * Get the numa node from which the virtio net device's memory
584 * The numa node, -1 on failure
586 int rte_vhost_get_numa_node(int vid);
590 * Get the number of queues the device supports.
592 * Note this function is deprecated, as it returns a queue pair number,
593 * which is vhost specific. Instead, rte_vhost_get_vring_num should
600 * The number of queues, 0 on failure
603 uint32_t rte_vhost_get_queue_num(int vid);
606 * Get the number of vrings the device supports.
612 * The number of vrings, 0 on failure
614 uint16_t rte_vhost_get_vring_num(int vid);
617 * Get the virtio net device's ifname, which is the vhost-user socket
623 * The buffer to stored the queried ifname
628 * 0 on success, -1 on failure
630 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
633 * Get how many avail entries are left in the queue
641 * num of avail entries left
643 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
648 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
649 * be received from the physical port or from another virtual device. A packet
650 * count is returned to indicate the number of packets that were successfully
651 * added to the RX queue.
655 * virtio queue index in mq case
657 * array to contain packets to be enqueued
659 * packets num to be enqueued
661 * num of packets enqueued
663 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
664 struct rte_mbuf **pkts, uint16_t count);
667 * This function gets guest buffers from the virtio device TX virtqueue,
668 * construct host mbufs, copies guest buffer content to host mbufs and
669 * store them in pkts to be processed.
673 * virtio queue index in mq case
675 * mbuf_pool where host mbuf is allocated.
677 * array to contain packets to be dequeued
679 * packets num to be dequeued
681 * num of packets dequeued
683 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
684 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
687 * Get guest mem table: a list of memory regions.
689 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
690 * guest memory regions. Application should free it at destroy_device()
696 * To store the returned mem regions
698 * 0 on success, -1 on failure
700 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
703 * Get guest vring info, including the vring address, vring size, etc.
710 * the structure to hold the requested vring info
712 * 0 on success, -1 on failure
714 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
715 struct rte_vhost_vring *vring);
718 * Get guest inflight vring info, including inflight ring and resubmit list.
725 * the structure to hold the requested inflight vring info
727 * 0 on success, -1 on failure
731 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
732 struct rte_vhost_ring_inflight *vring);
735 * Set split inflight descriptor.
737 * This function save descriptors that has been comsumed in available
745 * inflight entry index
747 * 0 on success, -1 on failure
751 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
755 * Set packed inflight descriptor and get corresponding inflight entry
757 * This function save descriptors that has been comsumed
764 * head of descriptors
766 * last of descriptors
767 * @param inflight_entry
768 * corresponding inflight entry
770 * 0 on success, -1 on failure
774 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
775 uint16_t head, uint16_t last, uint16_t *inflight_entry);
778 * Save the head of list that the last batch of used descriptors.
785 * descriptor entry index
787 * 0 on success, -1 on failure
791 rte_vhost_set_last_inflight_io_split(int vid,
792 uint16_t vring_idx, uint16_t idx);
795 * Update the inflight free_head, used_idx and used_wrap_counter.
797 * This function will update status first before updating descriptors
805 * head of descriptors
807 * 0 on success, -1 on failure
811 rte_vhost_set_last_inflight_io_packed(int vid,
812 uint16_t vring_idx, uint16_t head);
815 * Clear the split inflight status.
821 * @param last_used_idx
822 * last used idx of used ring
824 * inflight entry index
826 * 0 on success, -1 on failure
830 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
831 uint16_t last_used_idx, uint16_t idx);
834 * Clear the packed inflight status.
841 * inflight entry index
843 * 0 on success, -1 on failure
847 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
851 * Notify the guest that used descriptors have been added to the vring. This
852 * function acts as a memory barrier.
859 * 0 on success, -1 on failure
861 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
864 * Get vhost RX queue avail count.
869 * virtio queue index in mq case
871 * num of desc available
873 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
876 * Get log base and log size of the vhost device
885 * 0 on success, -1 on failure
889 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
892 * Get last_avail/used_idx of the vhost virtqueue
898 * @param last_avail_idx
899 * vhost last_avail_idx to get
900 * @param last_used_idx
901 * vhost last_used_idx to get
903 * 0 on success, -1 on failure
907 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
908 uint16_t *last_avail_idx, uint16_t *last_used_idx);
911 * Get last_avail/last_used of the vhost virtqueue
913 * This function is designed for the reconnection and it's specific for
914 * the packed ring as we can get the two parameters from the inflight
921 * @param last_avail_idx
922 * vhost last_avail_idx to get
923 * @param last_used_idx
924 * vhost last_used_idx to get
926 * 0 on success, -1 on failure
930 rte_vhost_get_vring_base_from_inflight(int vid,
931 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
934 * Set last_avail/used_idx of the vhost virtqueue
940 * @param last_avail_idx
941 * last_avail_idx to set
942 * @param last_used_idx
943 * last_used_idx to set
945 * 0 on success, -1 on failure
949 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
950 uint16_t last_avail_idx, uint16_t last_used_idx);
953 * Register external message handling callbacks
958 * virtio external callbacks to register
960 * additional context passed to the callbacks
962 * 0 on success, -1 on failure
966 rte_vhost_extern_callback_register(int vid,
967 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
970 * Get vdpa device id for vhost device.
979 rte_vhost_get_vdpa_device_id(int vid);
982 * Notify the guest that should get virtio configuration space from backend.
987 * wait for the master response the status of this operation
989 * 0 on success, < 0 on failure
993 rte_vhost_slave_config_change(int vid, bool need_reply);
999 #endif /* _RTE_VHOST_H_ */