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_RESERVED_1 (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
434 rte_vhost_driver_attach_vdpa_device(const char *path,
435 struct rte_vdpa_device *dev);
438 * Unset the vdpa device id
441 * The vhost-user socket file path
443 * 0 on success, -1 on failure
446 rte_vhost_driver_detach_vdpa_device(const char *path);
452 * The vhost-user socket file path
454 * vDPA device pointer, NULL on failure
456 struct rte_vdpa_device *
457 rte_vhost_driver_get_vdpa_device(const char *path);
460 * Set the feature bits the vhost-user driver supports.
463 * The vhost-user socket file path
467 * 0 on success, -1 on failure
469 int rte_vhost_driver_set_features(const char *path, uint64_t features);
472 * Enable vhost-user driver features.
475 * - the param features should be a subset of the feature bits provided
476 * by rte_vhost_driver_set_features().
477 * - it must be invoked before vhost-user negotiation starts.
480 * The vhost-user socket file path
484 * 0 on success, -1 on failure
486 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
489 * Disable vhost-user driver features.
491 * The two notes at rte_vhost_driver_enable_features() also apply here.
494 * The vhost-user socket file path
496 * Features to disable
498 * 0 on success, -1 on failure
500 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
503 * Get the feature bits before feature negotiation.
506 * The vhost-user socket file path
508 * A pointer to store the queried feature bits
510 * 0 on success, -1 on failure
512 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
515 * Set the protocol feature bits before feature negotiation.
518 * The vhost-user socket file path
519 * @param protocol_features
520 * Supported protocol features
522 * 0 on success, -1 on failure
526 rte_vhost_driver_set_protocol_features(const char *path,
527 uint64_t protocol_features);
530 * Get the protocol feature bits before feature negotiation.
533 * The vhost-user socket file path
534 * @param protocol_features
535 * A pointer to store the queried protocol feature bits
537 * 0 on success, -1 on failure
541 rte_vhost_driver_get_protocol_features(const char *path,
542 uint64_t *protocol_features);
545 * Get the queue number bits before feature negotiation.
548 * The vhost-user socket file path
550 * A pointer to store the queried queue number bits
552 * 0 on success, -1 on failure
556 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
559 * Get the feature bits after negotiation
564 * A pointer to store the queried feature bits
566 * 0 on success, -1 on failure
568 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
570 /* Register callbacks. */
571 int rte_vhost_driver_callback_register(const char *path,
572 struct vhost_device_ops const * const ops);
576 * Start the vhost-user driver.
578 * This function triggers the vhost-user negotiation.
581 * The vhost-user socket file path
583 * 0 on success, -1 on failure
585 int rte_vhost_driver_start(const char *path);
588 * Get the MTU value of the device if set in QEMU.
591 * virtio-net device ID
593 * The variable to store the MTU value
597 * -EAGAIN: device not yet started
598 * -ENOTSUP: device does not support MTU feature
600 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
603 * Get the numa node from which the virtio net device's memory
610 * The numa node, -1 on failure
612 int rte_vhost_get_numa_node(int vid);
616 * Get the number of queues the device supports.
618 * Note this function is deprecated, as it returns a queue pair number,
619 * which is vhost specific. Instead, rte_vhost_get_vring_num should
626 * The number of queues, 0 on failure
629 uint32_t rte_vhost_get_queue_num(int vid);
632 * Get the number of vrings the device supports.
638 * The number of vrings, 0 on failure
640 uint16_t rte_vhost_get_vring_num(int vid);
643 * Get the virtio net device's ifname, which is the vhost-user socket
649 * The buffer to stored the queried ifname
654 * 0 on success, -1 on failure
656 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
659 * Get how many avail entries are left in the queue
667 * num of avail entries left
669 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
674 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
675 * be received from the physical port or from another virtual device. A packet
676 * count is returned to indicate the number of packets that were successfully
677 * added to the RX queue.
681 * virtio queue index in mq case
683 * array to contain packets to be enqueued
685 * packets num to be enqueued
687 * num of packets enqueued
689 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
690 struct rte_mbuf **pkts, uint16_t count);
693 * This function gets guest buffers from the virtio device TX virtqueue,
694 * construct host mbufs, copies guest buffer content to host mbufs and
695 * store them in pkts to be processed.
699 * virtio queue index in mq case
701 * mbuf_pool where host mbuf is allocated.
703 * array to contain packets to be dequeued
705 * packets num to be dequeued
707 * num of packets dequeued
709 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
710 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
713 * Get guest mem table: a list of memory regions.
715 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
716 * guest memory regions. Application should free it at destroy_device()
722 * To store the returned mem regions
724 * 0 on success, -1 on failure
726 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
729 * Get guest vring info, including the vring address, vring size, etc.
736 * the structure to hold the requested vring info
738 * 0 on success, -1 on failure
740 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
741 struct rte_vhost_vring *vring);
744 * Get guest inflight vring info, including inflight ring and resubmit list.
751 * the structure to hold the requested inflight vring info
753 * 0 on success, -1 on failure
757 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
758 struct rte_vhost_ring_inflight *vring);
761 * Set split inflight descriptor.
763 * This function save descriptors that has been comsumed in available
771 * inflight entry index
773 * 0 on success, -1 on failure
777 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
781 * Set packed inflight descriptor and get corresponding inflight entry
783 * This function save descriptors that has been comsumed
790 * head of descriptors
792 * last of descriptors
793 * @param inflight_entry
794 * corresponding inflight entry
796 * 0 on success, -1 on failure
800 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
801 uint16_t head, uint16_t last, uint16_t *inflight_entry);
804 * Save the head of list that the last batch of used descriptors.
811 * descriptor entry index
813 * 0 on success, -1 on failure
817 rte_vhost_set_last_inflight_io_split(int vid,
818 uint16_t vring_idx, uint16_t idx);
821 * Update the inflight free_head, used_idx and used_wrap_counter.
823 * This function will update status first before updating descriptors
831 * head of descriptors
833 * 0 on success, -1 on failure
837 rte_vhost_set_last_inflight_io_packed(int vid,
838 uint16_t vring_idx, uint16_t head);
841 * Clear the split inflight status.
847 * @param last_used_idx
848 * last used idx of used ring
850 * inflight entry index
852 * 0 on success, -1 on failure
856 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
857 uint16_t last_used_idx, uint16_t idx);
860 * Clear the packed inflight status.
867 * inflight entry index
869 * 0 on success, -1 on failure
873 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
877 * Notify the guest that used descriptors have been added to the vring. This
878 * function acts as a memory barrier.
885 * 0 on success, -1 on failure
887 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
890 * Get vhost RX queue avail count.
895 * virtio queue index in mq case
897 * num of desc available
899 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
902 * Get log base and log size of the vhost device
911 * 0 on success, -1 on failure
914 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
917 * Get last_avail/used_idx of the vhost virtqueue
923 * @param last_avail_idx
924 * vhost last_avail_idx to get
925 * @param last_used_idx
926 * vhost last_used_idx to get
928 * 0 on success, -1 on failure
931 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
932 uint16_t *last_avail_idx, uint16_t *last_used_idx);
935 * Get last_avail/last_used of the vhost virtqueue
937 * This function is designed for the reconnection and it's specific for
938 * the packed ring as we can get the two parameters from the inflight
945 * @param last_avail_idx
946 * vhost last_avail_idx to get
947 * @param last_used_idx
948 * vhost last_used_idx to get
950 * 0 on success, -1 on failure
954 rte_vhost_get_vring_base_from_inflight(int vid,
955 uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
958 * Set last_avail/used_idx of the vhost virtqueue
964 * @param last_avail_idx
965 * last_avail_idx to set
966 * @param last_used_idx
967 * last_used_idx to set
969 * 0 on success, -1 on failure
972 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
973 uint16_t last_avail_idx, uint16_t last_used_idx);
976 * Register external message handling callbacks
981 * virtio external callbacks to register
983 * additional context passed to the callbacks
985 * 0 on success, -1 on failure
989 rte_vhost_extern_callback_register(int vid,
990 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
993 * Get vdpa device id for vhost device.
998 * vDPA device pointer on success, NULL on failure
1000 struct rte_vdpa_device *
1001 rte_vhost_get_vdpa_device(int vid);
1004 * Notify the guest that should get virtio configuration space from backend.
1009 * wait for the master response the status of this operation
1011 * 0 on success, < 0 on failure
1015 rte_vhost_slave_config_change(int vid, bool need_reply);
1021 #endif /* _RTE_VHOST_H_ */