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)
38 #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7)
41 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
42 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
45 #ifndef VIRTIO_NET_F_MQ
46 #define VIRTIO_NET_F_MQ 22
49 #ifndef VIRTIO_NET_F_MTU
50 #define VIRTIO_NET_F_MTU 3
53 #ifndef VIRTIO_F_ANY_LAYOUT
54 #define VIRTIO_F_ANY_LAYOUT 27
57 /** Protocol features. */
58 #ifndef VHOST_USER_PROTOCOL_F_MQ
59 #define VHOST_USER_PROTOCOL_F_MQ 0
62 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
63 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
66 #ifndef VHOST_USER_PROTOCOL_F_RARP
67 #define VHOST_USER_PROTOCOL_F_RARP 2
70 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
71 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
74 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
75 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
78 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
79 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
82 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
83 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
86 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
87 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
90 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
91 #define VHOST_USER_PROTOCOL_F_CONFIG 9
94 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
95 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
98 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
99 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
102 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
103 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
106 #ifndef VHOST_USER_PROTOCOL_F_STATUS
107 #define VHOST_USER_PROTOCOL_F_STATUS 16
110 /** Indicate whether protocol features negotiation is supported. */
111 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
112 #define VHOST_USER_F_PROTOCOL_FEATURES 30
115 struct rte_vdpa_device;
118 * Information relating to memory regions including offsets to
119 * addresses in QEMUs memory file.
121 struct rte_vhost_mem_region {
122 uint64_t guest_phys_addr;
123 uint64_t guest_user_addr;
124 uint64_t host_user_addr;
132 * Memory structure includes region and mapping information.
134 struct rte_vhost_memory {
136 struct rte_vhost_mem_region regions[];
139 struct rte_vhost_inflight_desc_split {
146 struct rte_vhost_inflight_info_split {
150 uint16_t last_inflight_io;
152 struct rte_vhost_inflight_desc_split desc[0];
155 struct rte_vhost_inflight_desc_packed {
168 struct rte_vhost_inflight_info_packed {
173 uint16_t old_free_head;
175 uint16_t old_used_idx;
176 uint8_t used_wrap_counter;
177 uint8_t old_used_wrap_counter;
179 struct rte_vhost_inflight_desc_packed desc[0];
182 struct rte_vhost_resubmit_desc {
187 struct rte_vhost_resubmit_info {
188 struct rte_vhost_resubmit_desc *resubmit_list;
189 uint16_t resubmit_num;
192 struct rte_vhost_ring_inflight {
194 struct rte_vhost_inflight_info_split *inflight_split;
195 struct rte_vhost_inflight_info_packed *inflight_packed;
198 struct rte_vhost_resubmit_info *resubmit_inflight;
201 struct rte_vhost_vring {
203 struct vring_desc *desc;
204 struct vring_packed_desc *desc_packed;
207 struct vring_avail *avail;
208 struct vring_packed_desc_event *driver_event;
211 struct vring_used *used;
212 struct vring_packed_desc_event *device_event;
214 uint64_t log_guest_addr;
216 /** Deprecated, use rte_vhost_vring_call() instead. */
224 * Possible results of the vhost user message handling callbacks
226 enum rte_vhost_msg_result {
227 /* Message handling failed */
228 RTE_VHOST_MSG_RESULT_ERR = -1,
229 /* Message handling successful */
230 RTE_VHOST_MSG_RESULT_OK = 0,
231 /* Message handling successful and reply prepared */
232 RTE_VHOST_MSG_RESULT_REPLY = 1,
233 /* Message not handled */
234 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
238 * Function prototype for the vhost backend to handle specific vhost user
246 * RTE_VHOST_MSG_RESULT_OK on success,
247 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
248 * RTE_VHOST_MSG_RESULT_ERR on failure,
249 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
251 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
254 * Optional vhost user message handlers.
256 struct rte_vhost_user_extern_ops {
257 /* Called prior to the master message handling. */
258 rte_vhost_msg_handle pre_msg_handle;
259 /* Called after the master message handling. */
260 rte_vhost_msg_handle post_msg_handle;
264 * Device and vring operations.
266 struct vhost_device_ops {
267 int (*new_device)(int vid); /**< Add device. */
268 void (*destroy_device)(int vid); /**< Remove device. */
270 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
273 * Features could be changed after the feature negotiation.
274 * For example, VHOST_F_LOG_ALL will be set/cleared at the
275 * start/end of live migration, respectively. This callback
276 * is used to inform the application on such change.
278 int (*features_changed)(int vid, uint64_t features);
280 int (*new_connection)(int vid);
281 void (*destroy_connection)(int vid);
284 * This callback gets called each time a guest gets notified
285 * about waiting packets. This is the interrupt handling through
286 * the eventfd_write(callfd), which can be used for counting these
289 void (*guest_notified)(int vid);
291 void *reserved[1]; /**< Reserved for future extension */
295 * Convert guest physical address to host virtual address
297 * This function is deprecated because unsafe.
298 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
299 * guest physical ranges are fully and contiguously mapped into
300 * process virtual address space.
303 * the guest memory regions
305 * the guest physical address for querying
307 * the host virtual address on success, 0 on failure
310 static __rte_always_inline uint64_t
311 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
313 struct rte_vhost_mem_region *reg;
316 for (i = 0; i < mem->nregions; i++) {
317 reg = &mem->regions[i];
318 if (gpa >= reg->guest_phys_addr &&
319 gpa < reg->guest_phys_addr + reg->size) {
320 return gpa - reg->guest_phys_addr +
329 * Convert guest physical address to host virtual address safely
331 * This variant of rte_vhost_gpa_to_vva() takes care all the
332 * requested length is mapped and contiguous in process address
336 * the guest memory regions
338 * the guest physical address for querying
340 * the size of the requested area to map, updated with actual size mapped
342 * the host virtual address on success, 0 on failure
345 static __rte_always_inline uint64_t
346 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
347 uint64_t gpa, uint64_t *len)
349 struct rte_vhost_mem_region *r;
352 for (i = 0; i < mem->nregions; i++) {
353 r = &mem->regions[i];
354 if (gpa >= r->guest_phys_addr &&
355 gpa < r->guest_phys_addr + r->size) {
357 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
358 *len = r->guest_phys_addr + r->size - gpa;
360 return gpa - r->guest_phys_addr +
369 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
372 * Log the memory write start with given address.
374 * This function only need be invoked when the live migration starts.
375 * Therefore, we won't need call it at all in the most of time. For
376 * making the performance impact be minimum, it's suggested to do a
377 * check before calling it:
379 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
380 * rte_vhost_log_write(vid, addr, len);
385 * the starting address for write (in guest physical address space)
387 * the length to write
389 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
392 * Log the used ring update start at given offset.
394 * Same as rte_vhost_log_write, it's suggested to do a check before
397 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
398 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
405 * the offset inside the used ring
407 * the length to write
409 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
410 uint64_t offset, uint64_t len);
412 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
415 * Register vhost driver. path could be different for multiple
418 int rte_vhost_driver_register(const char *path, uint64_t flags);
420 /* Unregister vhost driver. This is only meaningful to vhost user. */
421 int rte_vhost_driver_unregister(const char *path);
424 * Set the vdpa device id, enforce single connection per socket
427 * The vhost-user socket file path
429 * vDPA device pointer
431 * 0 on success, -1 on failure
435 rte_vhost_driver_attach_vdpa_device(const char *path,
436 struct rte_vdpa_device *dev);
439 * Unset the vdpa device id
442 * The vhost-user socket file path
444 * 0 on success, -1 on failure
448 rte_vhost_driver_detach_vdpa_device(const char *path);
454 * The vhost-user socket file path
456 * vDPA device pointer, NULL on failure
459 struct rte_vdpa_device *
460 rte_vhost_driver_get_vdpa_device(const char *path);
463 * Set the feature bits the vhost-user driver supports.
466 * The vhost-user socket file path
470 * 0 on success, -1 on failure
472 int rte_vhost_driver_set_features(const char *path, uint64_t features);
475 * Enable vhost-user driver features.
478 * - the param features should be a subset of the feature bits provided
479 * by rte_vhost_driver_set_features().
480 * - it must be invoked before vhost-user negotiation starts.
483 * The vhost-user socket file path
487 * 0 on success, -1 on failure
489 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
492 * Disable vhost-user driver features.
494 * The two notes at rte_vhost_driver_enable_features() also apply here.
497 * The vhost-user socket file path
499 * Features to disable
501 * 0 on success, -1 on failure
503 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
506 * Get the feature bits before feature negotiation.
509 * The vhost-user socket file path
511 * A pointer to store the queried feature bits
513 * 0 on success, -1 on failure
515 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
518 * Set the protocol feature bits before feature negotiation.
521 * The vhost-user socket file path
522 * @param protocol_features
523 * Supported protocol features
525 * 0 on success, -1 on failure
529 rte_vhost_driver_set_protocol_features(const char *path,
530 uint64_t protocol_features);
533 * Get the protocol feature bits before feature negotiation.
536 * The vhost-user socket file path
537 * @param protocol_features
538 * A pointer to store the queried protocol feature bits
540 * 0 on success, -1 on failure
544 rte_vhost_driver_get_protocol_features(const char *path,
545 uint64_t *protocol_features);
548 * Get the queue number bits before feature negotiation.
551 * The vhost-user socket file path
553 * A pointer to store the queried queue number bits
555 * 0 on success, -1 on failure
559 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
562 * Get the feature bits after negotiation
567 * A pointer to store the queried feature bits
569 * 0 on success, -1 on failure
571 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
573 /* Register callbacks. */
574 int rte_vhost_driver_callback_register(const char *path,
575 struct vhost_device_ops const * const ops);
579 * Start the vhost-user driver.
581 * This function triggers the vhost-user negotiation.
584 * The vhost-user socket file path
586 * 0 on success, -1 on failure
588 int rte_vhost_driver_start(const char *path);
591 * Get the MTU value of the device if set in QEMU.
594 * virtio-net device ID
596 * The variable to store the MTU value
600 * -EAGAIN: device not yet started
601 * -ENOTSUP: device does not support MTU feature
603 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
606 * Get the numa node from which the virtio net device's memory
613 * The numa node, -1 on failure
615 int rte_vhost_get_numa_node(int vid);
619 * Get the number of queues the device supports.
621 * Note this function is deprecated, as it returns a queue pair number,
622 * which is vhost specific. Instead, rte_vhost_get_vring_num should
629 * The number of queues, 0 on failure
632 uint32_t rte_vhost_get_queue_num(int vid);
635 * Get the number of vrings the device supports.
641 * The number of vrings, 0 on failure
643 uint16_t rte_vhost_get_vring_num(int vid);
646 * Get the virtio net device's ifname, which is the vhost-user socket
652 * The buffer to stored the queried ifname
657 * 0 on success, -1 on failure
659 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
662 * Get how many avail entries are left in the queue
670 * num of avail entries left
672 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
677 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
678 * be received from the physical port or from another virtual device. A packet
679 * count is returned to indicate the number of packets that were successfully
680 * added to the RX queue.
684 * virtio queue index in mq case
686 * array to contain packets to be enqueued
688 * packets num to be enqueued
690 * num of packets enqueued
692 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
693 struct rte_mbuf **pkts, uint16_t count);
696 * This function gets guest buffers from the virtio device TX virtqueue,
697 * construct host mbufs, copies guest buffer content to host mbufs and
698 * store them in pkts to be processed.
702 * virtio queue index in mq case
704 * mbuf_pool where host mbuf is allocated.
706 * array to contain packets to be dequeued
708 * packets num to be dequeued
710 * num of packets dequeued
712 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
713 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
716 * Get guest mem table: a list of memory regions.
718 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
719 * guest memory regions. Application should free it at destroy_device()
725 * To store the returned mem regions
727 * 0 on success, -1 on failure
729 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
732 * Get guest vring info, including the vring address, vring size, etc.
739 * the structure to hold the requested vring info
741 * 0 on success, -1 on failure
743 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
744 struct rte_vhost_vring *vring);
747 * Get guest inflight vring info, including inflight ring and resubmit list.
754 * the structure to hold the requested inflight vring info
756 * 0 on success, -1 on failure
760 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
761 struct rte_vhost_ring_inflight *vring);
764 * Set split inflight descriptor.
766 * This function save descriptors that has been comsumed in available
774 * inflight entry index
776 * 0 on success, -1 on failure
780 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
784 * Set packed inflight descriptor and get corresponding inflight entry
786 * This function save descriptors that has been comsumed
793 * head of descriptors
795 * last of descriptors
796 * @param inflight_entry
797 * corresponding inflight entry
799 * 0 on success, -1 on failure
803 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
804 uint16_t head, uint16_t last, uint16_t *inflight_entry);
807 * Save the head of list that the last batch of used descriptors.
814 * descriptor entry index
816 * 0 on success, -1 on failure
820 rte_vhost_set_last_inflight_io_split(int vid,
821 uint16_t vring_idx, uint16_t idx);
824 * Update the inflight free_head, used_idx and used_wrap_counter.
826 * This function will update status first before updating descriptors
834 * head of descriptors
836 * 0 on success, -1 on failure
840 rte_vhost_set_last_inflight_io_packed(int vid,
841 uint16_t vring_idx, uint16_t head);
844 * Clear the split inflight status.
850 * @param last_used_idx
851 * last used idx of used ring
853 * inflight entry index
855 * 0 on success, -1 on failure
859 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
860 uint16_t last_used_idx, uint16_t idx);
863 * Clear the packed inflight status.
870 * inflight entry index
872 * 0 on success, -1 on failure
876 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
880 * Notify the guest that used descriptors have been added to the vring. This
881 * function acts as a memory barrier.
888 * 0 on success, -1 on failure
890 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
893 * Get vhost RX queue avail count.
898 * virtio queue index in mq case
900 * num of desc available
902 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
905 * Get log base and log size of the vhost device
914 * 0 on success, -1 on failure
918 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
921 * Get last_avail/used_idx of the vhost virtqueue
927 * @param last_avail_idx
928 * vhost last_avail_idx to get
929 * @param last_used_idx
930 * vhost last_used_idx to get
932 * 0 on success, -1 on failure
936 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
937 uint16_t *last_avail_idx, uint16_t *last_used_idx);
940 * Get last_avail/last_used of the vhost virtqueue
942 * This function is designed for the reconnection and it's specific for
943 * the packed ring as we can get the two parameters from the inflight
950 * @param last_avail_idx
951 * vhost last_avail_idx to get
952 * @param last_used_idx
953 * vhost last_used_idx to get
955 * 0 on success, -1 on failure
959 rte_vhost_get_vring_base_from_inflight(int vid,
960 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
963 * Set last_avail/used_idx of the vhost virtqueue
969 * @param last_avail_idx
970 * last_avail_idx to set
971 * @param last_used_idx
972 * last_used_idx to set
974 * 0 on success, -1 on failure
978 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
979 uint16_t last_avail_idx, uint16_t last_used_idx);
982 * Register external message handling callbacks
987 * virtio external callbacks to register
989 * additional context passed to the callbacks
991 * 0 on success, -1 on failure
995 rte_vhost_extern_callback_register(int vid,
996 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
999 * Get vdpa device id for vhost device.
1004 * vDPA device pointer on success, NULL on failure
1007 struct rte_vdpa_device *
1008 rte_vhost_get_vdpa_device(int vid);
1011 * Notify the guest that should get virtio configuration space from backend.
1016 * wait for the master response the status of this operation
1018 * 0 on success, < 0 on failure
1022 rte_vhost_slave_config_change(int vid, bool need_reply);
1028 #endif /* _RTE_VHOST_H_ */