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 /** Indicate whether protocol features negotiation is supported. */
76 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
77 #define VHOST_USER_F_PROTOCOL_FEATURES 30
81 * Information relating to memory regions including offsets to
82 * addresses in QEMUs memory file.
84 struct rte_vhost_mem_region {
85 uint64_t guest_phys_addr;
86 uint64_t guest_user_addr;
87 uint64_t host_user_addr;
95 * Memory structure includes region and mapping information.
97 struct rte_vhost_memory {
99 struct rte_vhost_mem_region regions[];
102 struct rte_vhost_vring {
103 struct vring_desc *desc;
104 struct vring_avail *avail;
105 struct vring_used *used;
106 uint64_t log_guest_addr;
108 /** Deprecated, use rte_vhost_vring_call() instead. */
116 * Possible results of the vhost user message handling callbacks
118 enum rte_vhost_msg_result {
119 /* Message handling failed */
120 RTE_VHOST_MSG_RESULT_ERR = -1,
121 /* Message handling successful */
122 RTE_VHOST_MSG_RESULT_OK = 0,
123 /* Message handling successful and reply prepared */
124 RTE_VHOST_MSG_RESULT_REPLY = 1,
125 /* Message not handled */
126 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
130 * Function prototype for the vhost backend to handle specific vhost user
138 * RTE_VHOST_MSG_RESULT_OK on success,
139 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
140 * RTE_VHOST_MSG_RESULT_ERR on failure,
141 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
143 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
146 * Optional vhost user message handlers.
148 struct rte_vhost_user_extern_ops {
149 /* Called prior to the master message handling. */
150 rte_vhost_msg_handle pre_msg_handle;
151 /* Called after the master message handling. */
152 rte_vhost_msg_handle post_msg_handle;
156 * Device and vring operations.
158 struct vhost_device_ops {
159 int (*new_device)(int vid); /**< Add device. */
160 void (*destroy_device)(int vid); /**< Remove device. */
162 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
165 * Features could be changed after the feature negotiation.
166 * For example, VHOST_F_LOG_ALL will be set/cleared at the
167 * start/end of live migration, respectively. This callback
168 * is used to inform the application on such change.
170 int (*features_changed)(int vid, uint64_t features);
172 int (*new_connection)(int vid);
173 void (*destroy_connection)(int vid);
176 * This callback gets called each time a guest gets notified
177 * about waiting packets. This is the interrupt handling trough
178 * the eventfd_write(callfd), which can be used for counting these
181 void (*guest_notified)(int vid);
183 void *reserved[1]; /**< Reserved for future extension */
187 * Convert guest physical address to host virtual address
189 * This function is deprecated because unsafe.
190 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
191 * guest physical ranges are fully and contiguously mapped into
192 * process virtual address space.
195 * the guest memory regions
197 * the guest physical address for querying
199 * the host virtual address on success, 0 on failure
202 static __rte_always_inline uint64_t
203 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
205 struct rte_vhost_mem_region *reg;
208 for (i = 0; i < mem->nregions; i++) {
209 reg = &mem->regions[i];
210 if (gpa >= reg->guest_phys_addr &&
211 gpa < reg->guest_phys_addr + reg->size) {
212 return gpa - reg->guest_phys_addr +
221 * Convert guest physical address to host virtual address safely
223 * This variant of rte_vhost_gpa_to_vva() takes care all the
224 * requested length is mapped and contiguous in process address
228 * the guest memory regions
230 * the guest physical address for querying
232 * the size of the requested area to map, updated with actual size mapped
234 * the host virtual address on success, 0 on failure
236 static __rte_always_inline uint64_t
237 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
238 uint64_t gpa, uint64_t *len)
240 struct rte_vhost_mem_region *r;
243 for (i = 0; i < mem->nregions; i++) {
244 r = &mem->regions[i];
245 if (gpa >= r->guest_phys_addr &&
246 gpa < r->guest_phys_addr + r->size) {
248 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
249 *len = r->guest_phys_addr + r->size - gpa;
251 return gpa - r->guest_phys_addr +
260 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
263 * Log the memory write start with given address.
265 * This function only need be invoked when the live migration starts.
266 * Therefore, we won't need call it at all in the most of time. For
267 * making the performance impact be minimum, it's suggested to do a
268 * check before calling it:
270 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
271 * rte_vhost_log_write(vid, addr, len);
276 * the starting address for write
278 * the length to write
280 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
283 * Log the used ring update start at given offset.
285 * Same as rte_vhost_log_write, it's suggested to do a check before
288 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
289 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
296 * the offset inside the used ring
298 * the length to write
300 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
301 uint64_t offset, uint64_t len);
303 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
306 * Register vhost driver. path could be different for multiple
309 int rte_vhost_driver_register(const char *path, uint64_t flags);
311 /* Unregister vhost driver. This is only meaningful to vhost user. */
312 int rte_vhost_driver_unregister(const char *path);
315 * Set the vdpa device id, enforce single connection per socket
318 * The vhost-user socket file path
322 * 0 on success, -1 on failure
326 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
329 * Unset the vdpa device id
332 * The vhost-user socket file path
334 * 0 on success, -1 on failure
338 rte_vhost_driver_detach_vdpa_device(const char *path);
344 * The vhost-user socket file path
346 * Device id, -1 on failure
350 rte_vhost_driver_get_vdpa_device_id(const char *path);
353 * Set the feature bits the vhost-user driver supports.
356 * The vhost-user socket file path
360 * 0 on success, -1 on failure
362 int rte_vhost_driver_set_features(const char *path, uint64_t features);
365 * Enable vhost-user driver features.
368 * - the param features should be a subset of the feature bits provided
369 * by rte_vhost_driver_set_features().
370 * - it must be invoked before vhost-user negotiation starts.
373 * The vhost-user socket file path
377 * 0 on success, -1 on failure
379 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
382 * Disable vhost-user driver features.
384 * The two notes at rte_vhost_driver_enable_features() also apply here.
387 * The vhost-user socket file path
389 * Features to disable
391 * 0 on success, -1 on failure
393 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
396 * Get the feature bits before feature negotiation.
399 * The vhost-user socket file path
401 * A pointer to store the queried feature bits
403 * 0 on success, -1 on failure
405 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
408 * Set the protocol feature bits before feature negotiation.
411 * The vhost-user socket file path
412 * @param protocol_features
413 * Supported protocol features
415 * 0 on success, -1 on failure
419 rte_vhost_driver_set_protocol_features(const char *path,
420 uint64_t protocol_features);
423 * Get the protocol feature bits before feature negotiation.
426 * The vhost-user socket file path
427 * @param protocol_features
428 * A pointer to store the queried protocol feature bits
430 * 0 on success, -1 on failure
434 rte_vhost_driver_get_protocol_features(const char *path,
435 uint64_t *protocol_features);
438 * Get the queue number bits before feature negotiation.
441 * The vhost-user socket file path
443 * A pointer to store the queried queue number bits
445 * 0 on success, -1 on failure
449 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
452 * Get the feature bits after negotiation
457 * A pointer to store the queried feature bits
459 * 0 on success, -1 on failure
461 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
463 /* Register callbacks. */
464 int rte_vhost_driver_callback_register(const char *path,
465 struct vhost_device_ops const * const ops);
469 * Start the vhost-user driver.
471 * This function triggers the vhost-user negotiation.
474 * The vhost-user socket file path
476 * 0 on success, -1 on failure
478 int rte_vhost_driver_start(const char *path);
481 * Get the MTU value of the device if set in QEMU.
484 * virtio-net device ID
486 * The variable to store the MTU value
490 * -EAGAIN: device not yet started
491 * -ENOTSUP: device does not support MTU feature
493 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
496 * Get the numa node from which the virtio net device's memory
503 * The numa node, -1 on failure
505 int rte_vhost_get_numa_node(int vid);
509 * Get the number of queues the device supports.
511 * Note this function is deprecated, as it returns a queue pair number,
512 * which is vhost specific. Instead, rte_vhost_get_vring_num should
519 * The number of queues, 0 on failure
522 uint32_t rte_vhost_get_queue_num(int vid);
525 * Get the number of vrings the device supports.
531 * The number of vrings, 0 on failure
533 uint16_t rte_vhost_get_vring_num(int vid);
536 * Get the virtio net device's ifname, which is the vhost-user socket
542 * The buffer to stored the queried ifname
547 * 0 on success, -1 on failure
549 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
552 * Get how many avail entries are left in the queue
560 * num of avail entries left
562 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
567 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
568 * be received from the physical port or from another virtual device. A packet
569 * count is returned to indicate the number of packets that were successfully
570 * added to the RX queue.
574 * virtio queue index in mq case
576 * array to contain packets to be enqueued
578 * packets num to be enqueued
580 * num of packets enqueued
582 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
583 struct rte_mbuf **pkts, uint16_t count);
586 * This function gets guest buffers from the virtio device TX virtqueue,
587 * construct host mbufs, copies guest buffer content to host mbufs and
588 * store them in pkts to be processed.
592 * virtio queue index in mq case
594 * mbuf_pool where host mbuf is allocated.
596 * array to contain packets to be dequeued
598 * packets num to be dequeued
600 * num of packets dequeued
602 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
603 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
606 * Get guest mem table: a list of memory regions.
608 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
609 * guest memory regions. Application should free it at destroy_device()
615 * To store the returned mem regions
617 * 0 on success, -1 on failure
619 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
622 * Get guest vring info, including the vring address, vring size, etc.
629 * the structure to hold the requested vring info
631 * 0 on success, -1 on failure
633 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
634 struct rte_vhost_vring *vring);
637 * Notify the guest that used descriptors have been added to the vring. This
638 * function acts as a memory barrier.
645 * 0 on success, -1 on failure
647 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
650 * Get vhost RX queue avail count.
655 * virtio queue index in mq case
657 * num of desc available
659 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
662 * Get log base and log size of the vhost device
671 * 0 on success, -1 on failure
675 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
678 * Get last_avail/used_idx of the vhost virtqueue
684 * @param last_avail_idx
685 * vhost last_avail_idx to get
686 * @param last_used_idx
687 * vhost last_used_idx to get
689 * 0 on success, -1 on failure
693 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
694 uint16_t *last_avail_idx, uint16_t *last_used_idx);
697 * Set last_avail/used_idx of the vhost virtqueue
703 * @param last_avail_idx
704 * last_avail_idx to set
705 * @param last_used_idx
706 * last_used_idx to set
708 * 0 on success, -1 on failure
712 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
713 uint16_t last_avail_idx, uint16_t last_used_idx);
716 * Register external message handling callbacks
721 * virtio external callbacks to register
723 * additional context passed to the callbacks
725 * 0 on success, -1 on failure
729 rte_vhost_extern_callback_register(int vid,
730 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
733 * Get vdpa device id for vhost device.
742 rte_vhost_get_vdpa_device_id(int vid);
748 #endif /* _RTE_VHOST_H_ */