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_vring {
108 struct vring_desc *desc;
109 struct vring_packed_desc *desc_packed;
112 struct vring_avail *avail;
113 struct vring_packed_desc_event *driver_event;
116 struct vring_used *used;
117 struct vring_packed_desc_event *device_event;
119 uint64_t log_guest_addr;
121 /** Deprecated, use rte_vhost_vring_call() instead. */
129 * Possible results of the vhost user message handling callbacks
131 enum rte_vhost_msg_result {
132 /* Message handling failed */
133 RTE_VHOST_MSG_RESULT_ERR = -1,
134 /* Message handling successful */
135 RTE_VHOST_MSG_RESULT_OK = 0,
136 /* Message handling successful and reply prepared */
137 RTE_VHOST_MSG_RESULT_REPLY = 1,
138 /* Message not handled */
139 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
143 * Function prototype for the vhost backend to handle specific vhost user
151 * RTE_VHOST_MSG_RESULT_OK on success,
152 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
153 * RTE_VHOST_MSG_RESULT_ERR on failure,
154 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
156 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
159 * Optional vhost user message handlers.
161 struct rte_vhost_user_extern_ops {
162 /* Called prior to the master message handling. */
163 rte_vhost_msg_handle pre_msg_handle;
164 /* Called after the master message handling. */
165 rte_vhost_msg_handle post_msg_handle;
169 * Device and vring operations.
171 struct vhost_device_ops {
172 int (*new_device)(int vid); /**< Add device. */
173 void (*destroy_device)(int vid); /**< Remove device. */
175 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
178 * Features could be changed after the feature negotiation.
179 * For example, VHOST_F_LOG_ALL will be set/cleared at the
180 * start/end of live migration, respectively. This callback
181 * is used to inform the application on such change.
183 int (*features_changed)(int vid, uint64_t features);
185 int (*new_connection)(int vid);
186 void (*destroy_connection)(int vid);
189 * This callback gets called each time a guest gets notified
190 * about waiting packets. This is the interrupt handling trough
191 * the eventfd_write(callfd), which can be used for counting these
194 void (*guest_notified)(int vid);
196 void *reserved[1]; /**< Reserved for future extension */
200 * Convert guest physical address to host virtual address
202 * This function is deprecated because unsafe.
203 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
204 * guest physical ranges are fully and contiguously mapped into
205 * process virtual address space.
208 * the guest memory regions
210 * the guest physical address for querying
212 * the host virtual address on success, 0 on failure
215 static __rte_always_inline uint64_t
216 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
218 struct rte_vhost_mem_region *reg;
221 for (i = 0; i < mem->nregions; i++) {
222 reg = &mem->regions[i];
223 if (gpa >= reg->guest_phys_addr &&
224 gpa < reg->guest_phys_addr + reg->size) {
225 return gpa - reg->guest_phys_addr +
234 * Convert guest physical address to host virtual address safely
236 * This variant of rte_vhost_gpa_to_vva() takes care all the
237 * requested length is mapped and contiguous in process address
241 * the guest memory regions
243 * the guest physical address for querying
245 * the size of the requested area to map, updated with actual size mapped
247 * the host virtual address on success, 0 on failure
250 static __rte_always_inline uint64_t
251 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
252 uint64_t gpa, uint64_t *len)
254 struct rte_vhost_mem_region *r;
257 for (i = 0; i < mem->nregions; i++) {
258 r = &mem->regions[i];
259 if (gpa >= r->guest_phys_addr &&
260 gpa < r->guest_phys_addr + r->size) {
262 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
263 *len = r->guest_phys_addr + r->size - gpa;
265 return gpa - r->guest_phys_addr +
274 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
277 * Log the memory write start with given address.
279 * This function only need be invoked when the live migration starts.
280 * Therefore, we won't need call it at all in the most of time. For
281 * making the performance impact be minimum, it's suggested to do a
282 * check before calling it:
284 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
285 * rte_vhost_log_write(vid, addr, len);
290 * the starting address for write (in guest physical address space)
292 * the length to write
294 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
297 * Log the used ring update start at given offset.
299 * Same as rte_vhost_log_write, it's suggested to do a check before
302 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
303 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
310 * the offset inside the used ring
312 * the length to write
314 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
315 uint64_t offset, uint64_t len);
317 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
320 * Register vhost driver. path could be different for multiple
323 int rte_vhost_driver_register(const char *path, uint64_t flags);
325 /* Unregister vhost driver. This is only meaningful to vhost user. */
326 int rte_vhost_driver_unregister(const char *path);
329 * Set the vdpa device id, enforce single connection per socket
332 * The vhost-user socket file path
336 * 0 on success, -1 on failure
340 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
343 * Unset the vdpa device id
346 * The vhost-user socket file path
348 * 0 on success, -1 on failure
352 rte_vhost_driver_detach_vdpa_device(const char *path);
358 * The vhost-user socket file path
360 * Device id, -1 on failure
364 rte_vhost_driver_get_vdpa_device_id(const char *path);
367 * Set the feature bits the vhost-user driver supports.
370 * The vhost-user socket file path
374 * 0 on success, -1 on failure
376 int rte_vhost_driver_set_features(const char *path, uint64_t features);
379 * Enable vhost-user driver features.
382 * - the param features should be a subset of the feature bits provided
383 * by rte_vhost_driver_set_features().
384 * - it must be invoked before vhost-user negotiation starts.
387 * The vhost-user socket file path
391 * 0 on success, -1 on failure
393 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
396 * Disable vhost-user driver features.
398 * The two notes at rte_vhost_driver_enable_features() also apply here.
401 * The vhost-user socket file path
403 * Features to disable
405 * 0 on success, -1 on failure
407 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
410 * Get the feature bits before feature negotiation.
413 * The vhost-user socket file path
415 * A pointer to store the queried feature bits
417 * 0 on success, -1 on failure
419 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
422 * Set the protocol feature bits before feature negotiation.
425 * The vhost-user socket file path
426 * @param protocol_features
427 * Supported protocol features
429 * 0 on success, -1 on failure
433 rte_vhost_driver_set_protocol_features(const char *path,
434 uint64_t protocol_features);
437 * Get the protocol feature bits before feature negotiation.
440 * The vhost-user socket file path
441 * @param protocol_features
442 * A pointer to store the queried protocol feature bits
444 * 0 on success, -1 on failure
448 rte_vhost_driver_get_protocol_features(const char *path,
449 uint64_t *protocol_features);
452 * Get the queue number bits before feature negotiation.
455 * The vhost-user socket file path
457 * A pointer to store the queried queue number bits
459 * 0 on success, -1 on failure
463 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
466 * Get the feature bits after negotiation
471 * A pointer to store the queried feature bits
473 * 0 on success, -1 on failure
475 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
477 /* Register callbacks. */
478 int rte_vhost_driver_callback_register(const char *path,
479 struct vhost_device_ops const * const ops);
483 * Start the vhost-user driver.
485 * This function triggers the vhost-user negotiation.
488 * The vhost-user socket file path
490 * 0 on success, -1 on failure
492 int rte_vhost_driver_start(const char *path);
495 * Get the MTU value of the device if set in QEMU.
498 * virtio-net device ID
500 * The variable to store the MTU value
504 * -EAGAIN: device not yet started
505 * -ENOTSUP: device does not support MTU feature
507 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
510 * Get the numa node from which the virtio net device's memory
517 * The numa node, -1 on failure
519 int rte_vhost_get_numa_node(int vid);
523 * Get the number of queues the device supports.
525 * Note this function is deprecated, as it returns a queue pair number,
526 * which is vhost specific. Instead, rte_vhost_get_vring_num should
533 * The number of queues, 0 on failure
536 uint32_t rte_vhost_get_queue_num(int vid);
539 * Get the number of vrings the device supports.
545 * The number of vrings, 0 on failure
547 uint16_t rte_vhost_get_vring_num(int vid);
550 * Get the virtio net device's ifname, which is the vhost-user socket
556 * The buffer to stored the queried ifname
561 * 0 on success, -1 on failure
563 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
566 * Get how many avail entries are left in the queue
574 * num of avail entries left
576 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
581 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
582 * be received from the physical port or from another virtual device. A packet
583 * count is returned to indicate the number of packets that were successfully
584 * added to the RX queue.
588 * virtio queue index in mq case
590 * array to contain packets to be enqueued
592 * packets num to be enqueued
594 * num of packets enqueued
596 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
597 struct rte_mbuf **pkts, uint16_t count);
600 * This function gets guest buffers from the virtio device TX virtqueue,
601 * construct host mbufs, copies guest buffer content to host mbufs and
602 * store them in pkts to be processed.
606 * virtio queue index in mq case
608 * mbuf_pool where host mbuf is allocated.
610 * array to contain packets to be dequeued
612 * packets num to be dequeued
614 * num of packets dequeued
616 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
617 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
620 * Get guest mem table: a list of memory regions.
622 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
623 * guest memory regions. Application should free it at destroy_device()
629 * To store the returned mem regions
631 * 0 on success, -1 on failure
633 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
636 * Get guest vring info, including the vring address, vring size, etc.
643 * the structure to hold the requested vring info
645 * 0 on success, -1 on failure
647 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
648 struct rte_vhost_vring *vring);
651 * Notify the guest that used descriptors have been added to the vring. This
652 * function acts as a memory barrier.
659 * 0 on success, -1 on failure
661 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
664 * Get vhost RX queue avail count.
669 * virtio queue index in mq case
671 * num of desc available
673 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
676 * Get log base and log size of the vhost device
685 * 0 on success, -1 on failure
689 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
692 * Get last_avail/used_idx of the vhost virtqueue
698 * @param last_avail_idx
699 * vhost last_avail_idx to get
700 * @param last_used_idx
701 * vhost last_used_idx to get
703 * 0 on success, -1 on failure
707 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
708 uint16_t *last_avail_idx, uint16_t *last_used_idx);
711 * Set last_avail/used_idx of the vhost virtqueue
717 * @param last_avail_idx
718 * last_avail_idx to set
719 * @param last_used_idx
720 * last_used_idx to set
722 * 0 on success, -1 on failure
726 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
727 uint16_t last_avail_idx, uint16_t last_used_idx);
730 * Register external message handling callbacks
735 * virtio external callbacks to register
737 * additional context passed to the callbacks
739 * 0 on success, -1 on failure
743 rte_vhost_extern_callback_register(int vid,
744 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
747 * Get vdpa device id for vhost device.
756 rte_vhost_get_vdpa_device_id(int vid);
762 #endif /* _RTE_VHOST_H_ */