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 {
107 struct vring_desc *desc;
108 struct vring_avail *avail;
109 struct vring_used *used;
110 uint64_t log_guest_addr;
112 /** Deprecated, use rte_vhost_vring_call() instead. */
120 * Possible results of the vhost user message handling callbacks
122 enum rte_vhost_msg_result {
123 /* Message handling failed */
124 RTE_VHOST_MSG_RESULT_ERR = -1,
125 /* Message handling successful */
126 RTE_VHOST_MSG_RESULT_OK = 0,
127 /* Message handling successful and reply prepared */
128 RTE_VHOST_MSG_RESULT_REPLY = 1,
129 /* Message not handled */
130 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
134 * Function prototype for the vhost backend to handle specific vhost user
142 * RTE_VHOST_MSG_RESULT_OK on success,
143 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
144 * RTE_VHOST_MSG_RESULT_ERR on failure,
145 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
147 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
150 * Optional vhost user message handlers.
152 struct rte_vhost_user_extern_ops {
153 /* Called prior to the master message handling. */
154 rte_vhost_msg_handle pre_msg_handle;
155 /* Called after the master message handling. */
156 rte_vhost_msg_handle post_msg_handle;
160 * Device and vring operations.
162 struct vhost_device_ops {
163 int (*new_device)(int vid); /**< Add device. */
164 void (*destroy_device)(int vid); /**< Remove device. */
166 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
169 * Features could be changed after the feature negotiation.
170 * For example, VHOST_F_LOG_ALL will be set/cleared at the
171 * start/end of live migration, respectively. This callback
172 * is used to inform the application on such change.
174 int (*features_changed)(int vid, uint64_t features);
176 int (*new_connection)(int vid);
177 void (*destroy_connection)(int vid);
180 * This callback gets called each time a guest gets notified
181 * about waiting packets. This is the interrupt handling trough
182 * the eventfd_write(callfd), which can be used for counting these
185 void (*guest_notified)(int vid);
187 void *reserved[1]; /**< Reserved for future extension */
191 * Convert guest physical address to host virtual address
193 * This function is deprecated because unsafe.
194 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
195 * guest physical ranges are fully and contiguously mapped into
196 * process virtual address space.
199 * the guest memory regions
201 * the guest physical address for querying
203 * the host virtual address on success, 0 on failure
206 static __rte_always_inline uint64_t
207 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
209 struct rte_vhost_mem_region *reg;
212 for (i = 0; i < mem->nregions; i++) {
213 reg = &mem->regions[i];
214 if (gpa >= reg->guest_phys_addr &&
215 gpa < reg->guest_phys_addr + reg->size) {
216 return gpa - reg->guest_phys_addr +
225 * Convert guest physical address to host virtual address safely
227 * This variant of rte_vhost_gpa_to_vva() takes care all the
228 * requested length is mapped and contiguous in process address
232 * the guest memory regions
234 * the guest physical address for querying
236 * the size of the requested area to map, updated with actual size mapped
238 * the host virtual address on success, 0 on failure
241 static __rte_always_inline uint64_t
242 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
243 uint64_t gpa, uint64_t *len)
245 struct rte_vhost_mem_region *r;
248 for (i = 0; i < mem->nregions; i++) {
249 r = &mem->regions[i];
250 if (gpa >= r->guest_phys_addr &&
251 gpa < r->guest_phys_addr + r->size) {
253 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
254 *len = r->guest_phys_addr + r->size - gpa;
256 return gpa - r->guest_phys_addr +
265 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
268 * Log the memory write start with given address.
270 * This function only need be invoked when the live migration starts.
271 * Therefore, we won't need call it at all in the most of time. For
272 * making the performance impact be minimum, it's suggested to do a
273 * check before calling it:
275 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
276 * rte_vhost_log_write(vid, addr, len);
281 * the starting address for write (in guest physical address space)
283 * the length to write
285 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
288 * Log the used ring update start at given offset.
290 * Same as rte_vhost_log_write, it's suggested to do a check before
293 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
294 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
301 * the offset inside the used ring
303 * the length to write
305 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
306 uint64_t offset, uint64_t len);
308 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
311 * Register vhost driver. path could be different for multiple
314 int rte_vhost_driver_register(const char *path, uint64_t flags);
316 /* Unregister vhost driver. This is only meaningful to vhost user. */
317 int rte_vhost_driver_unregister(const char *path);
320 * Set the vdpa device id, enforce single connection per socket
323 * The vhost-user socket file path
327 * 0 on success, -1 on failure
331 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
334 * Unset the vdpa device id
337 * The vhost-user socket file path
339 * 0 on success, -1 on failure
343 rte_vhost_driver_detach_vdpa_device(const char *path);
349 * The vhost-user socket file path
351 * Device id, -1 on failure
355 rte_vhost_driver_get_vdpa_device_id(const char *path);
358 * Set the feature bits the vhost-user driver supports.
361 * The vhost-user socket file path
365 * 0 on success, -1 on failure
367 int rte_vhost_driver_set_features(const char *path, uint64_t features);
370 * Enable vhost-user driver features.
373 * - the param features should be a subset of the feature bits provided
374 * by rte_vhost_driver_set_features().
375 * - it must be invoked before vhost-user negotiation starts.
378 * The vhost-user socket file path
382 * 0 on success, -1 on failure
384 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
387 * Disable vhost-user driver features.
389 * The two notes at rte_vhost_driver_enable_features() also apply here.
392 * The vhost-user socket file path
394 * Features to disable
396 * 0 on success, -1 on failure
398 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
401 * Get the feature bits before feature negotiation.
404 * The vhost-user socket file path
406 * A pointer to store the queried feature bits
408 * 0 on success, -1 on failure
410 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
413 * Set the protocol feature bits before feature negotiation.
416 * The vhost-user socket file path
417 * @param protocol_features
418 * Supported protocol features
420 * 0 on success, -1 on failure
424 rte_vhost_driver_set_protocol_features(const char *path,
425 uint64_t protocol_features);
428 * Get the protocol feature bits before feature negotiation.
431 * The vhost-user socket file path
432 * @param protocol_features
433 * A pointer to store the queried protocol feature bits
435 * 0 on success, -1 on failure
439 rte_vhost_driver_get_protocol_features(const char *path,
440 uint64_t *protocol_features);
443 * Get the queue number bits before feature negotiation.
446 * The vhost-user socket file path
448 * A pointer to store the queried queue number bits
450 * 0 on success, -1 on failure
454 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
457 * Get the feature bits after negotiation
462 * A pointer to store the queried feature bits
464 * 0 on success, -1 on failure
466 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
468 /* Register callbacks. */
469 int rte_vhost_driver_callback_register(const char *path,
470 struct vhost_device_ops const * const ops);
474 * Start the vhost-user driver.
476 * This function triggers the vhost-user negotiation.
479 * The vhost-user socket file path
481 * 0 on success, -1 on failure
483 int rte_vhost_driver_start(const char *path);
486 * Get the MTU value of the device if set in QEMU.
489 * virtio-net device ID
491 * The variable to store the MTU value
495 * -EAGAIN: device not yet started
496 * -ENOTSUP: device does not support MTU feature
498 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
501 * Get the numa node from which the virtio net device's memory
508 * The numa node, -1 on failure
510 int rte_vhost_get_numa_node(int vid);
514 * Get the number of queues the device supports.
516 * Note this function is deprecated, as it returns a queue pair number,
517 * which is vhost specific. Instead, rte_vhost_get_vring_num should
524 * The number of queues, 0 on failure
527 uint32_t rte_vhost_get_queue_num(int vid);
530 * Get the number of vrings the device supports.
536 * The number of vrings, 0 on failure
538 uint16_t rte_vhost_get_vring_num(int vid);
541 * Get the virtio net device's ifname, which is the vhost-user socket
547 * The buffer to stored the queried ifname
552 * 0 on success, -1 on failure
554 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
557 * Get how many avail entries are left in the queue
565 * num of avail entries left
567 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
572 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
573 * be received from the physical port or from another virtual device. A packet
574 * count is returned to indicate the number of packets that were successfully
575 * added to the RX queue.
579 * virtio queue index in mq case
581 * array to contain packets to be enqueued
583 * packets num to be enqueued
585 * num of packets enqueued
587 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
588 struct rte_mbuf **pkts, uint16_t count);
591 * This function gets guest buffers from the virtio device TX virtqueue,
592 * construct host mbufs, copies guest buffer content to host mbufs and
593 * store them in pkts to be processed.
597 * virtio queue index in mq case
599 * mbuf_pool where host mbuf is allocated.
601 * array to contain packets to be dequeued
603 * packets num to be dequeued
605 * num of packets dequeued
607 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
608 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
611 * Get guest mem table: a list of memory regions.
613 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
614 * guest memory regions. Application should free it at destroy_device()
620 * To store the returned mem regions
622 * 0 on success, -1 on failure
624 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
627 * Get guest vring info, including the vring address, vring size, etc.
634 * the structure to hold the requested vring info
636 * 0 on success, -1 on failure
638 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
639 struct rte_vhost_vring *vring);
642 * Notify the guest that used descriptors have been added to the vring. This
643 * function acts as a memory barrier.
650 * 0 on success, -1 on failure
652 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
655 * Get vhost RX queue avail count.
660 * virtio queue index in mq case
662 * num of desc available
664 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
667 * Get log base and log size of the vhost device
676 * 0 on success, -1 on failure
680 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
683 * Get last_avail/used_idx of the vhost virtqueue
689 * @param last_avail_idx
690 * vhost last_avail_idx to get
691 * @param last_used_idx
692 * vhost last_used_idx to get
694 * 0 on success, -1 on failure
698 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
699 uint16_t *last_avail_idx, uint16_t *last_used_idx);
702 * Set last_avail/used_idx of the vhost virtqueue
708 * @param last_avail_idx
709 * last_avail_idx to set
710 * @param last_used_idx
711 * last_used_idx to set
713 * 0 on success, -1 on failure
717 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
718 uint16_t last_avail_idx, uint16_t last_used_idx);
721 * Register external message handling callbacks
726 * virtio external callbacks to register
728 * additional context passed to the callbacks
730 * 0 on success, -1 on failure
734 rte_vhost_extern_callback_register(int vid,
735 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
738 * Get vdpa device id for vhost device.
747 rte_vhost_get_vdpa_device_id(int vid);
753 #endif /* _RTE_VHOST_H_ */