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
237 static __rte_always_inline uint64_t
238 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
239 uint64_t gpa, uint64_t *len)
241 struct rte_vhost_mem_region *r;
244 for (i = 0; i < mem->nregions; i++) {
245 r = &mem->regions[i];
246 if (gpa >= r->guest_phys_addr &&
247 gpa < r->guest_phys_addr + r->size) {
249 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
250 *len = r->guest_phys_addr + r->size - gpa;
252 return gpa - r->guest_phys_addr +
261 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
264 * Log the memory write start with given address.
266 * This function only need be invoked when the live migration starts.
267 * Therefore, we won't need call it at all in the most of time. For
268 * making the performance impact be minimum, it's suggested to do a
269 * check before calling it:
271 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
272 * rte_vhost_log_write(vid, addr, len);
277 * the starting address for write (in guest physical address space)
279 * the length to write
281 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
284 * Log the used ring update start at given offset.
286 * Same as rte_vhost_log_write, it's suggested to do a check before
289 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
290 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
297 * the offset inside the used ring
299 * the length to write
301 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
302 uint64_t offset, uint64_t len);
304 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
307 * Register vhost driver. path could be different for multiple
310 int rte_vhost_driver_register(const char *path, uint64_t flags);
312 /* Unregister vhost driver. This is only meaningful to vhost user. */
313 int rte_vhost_driver_unregister(const char *path);
316 * Set the vdpa device id, enforce single connection per socket
319 * The vhost-user socket file path
323 * 0 on success, -1 on failure
327 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
330 * Unset the vdpa device id
333 * The vhost-user socket file path
335 * 0 on success, -1 on failure
339 rte_vhost_driver_detach_vdpa_device(const char *path);
345 * The vhost-user socket file path
347 * Device id, -1 on failure
351 rte_vhost_driver_get_vdpa_device_id(const char *path);
354 * Set the feature bits the vhost-user driver supports.
357 * The vhost-user socket file path
361 * 0 on success, -1 on failure
363 int rte_vhost_driver_set_features(const char *path, uint64_t features);
366 * Enable vhost-user driver features.
369 * - the param features should be a subset of the feature bits provided
370 * by rte_vhost_driver_set_features().
371 * - it must be invoked before vhost-user negotiation starts.
374 * The vhost-user socket file path
378 * 0 on success, -1 on failure
380 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
383 * Disable vhost-user driver features.
385 * The two notes at rte_vhost_driver_enable_features() also apply here.
388 * The vhost-user socket file path
390 * Features to disable
392 * 0 on success, -1 on failure
394 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
397 * Get the feature bits before feature negotiation.
400 * The vhost-user socket file path
402 * A pointer to store the queried feature bits
404 * 0 on success, -1 on failure
406 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
409 * Set the protocol feature bits before feature negotiation.
412 * The vhost-user socket file path
413 * @param protocol_features
414 * Supported protocol features
416 * 0 on success, -1 on failure
420 rte_vhost_driver_set_protocol_features(const char *path,
421 uint64_t protocol_features);
424 * Get the protocol feature bits before feature negotiation.
427 * The vhost-user socket file path
428 * @param protocol_features
429 * A pointer to store the queried protocol feature bits
431 * 0 on success, -1 on failure
435 rte_vhost_driver_get_protocol_features(const char *path,
436 uint64_t *protocol_features);
439 * Get the queue number bits before feature negotiation.
442 * The vhost-user socket file path
444 * A pointer to store the queried queue number bits
446 * 0 on success, -1 on failure
450 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
453 * Get the feature bits after negotiation
458 * A pointer to store the queried feature bits
460 * 0 on success, -1 on failure
462 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
464 /* Register callbacks. */
465 int rte_vhost_driver_callback_register(const char *path,
466 struct vhost_device_ops const * const ops);
470 * Start the vhost-user driver.
472 * This function triggers the vhost-user negotiation.
475 * The vhost-user socket file path
477 * 0 on success, -1 on failure
479 int rte_vhost_driver_start(const char *path);
482 * Get the MTU value of the device if set in QEMU.
485 * virtio-net device ID
487 * The variable to store the MTU value
491 * -EAGAIN: device not yet started
492 * -ENOTSUP: device does not support MTU feature
494 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
497 * Get the numa node from which the virtio net device's memory
504 * The numa node, -1 on failure
506 int rte_vhost_get_numa_node(int vid);
510 * Get the number of queues the device supports.
512 * Note this function is deprecated, as it returns a queue pair number,
513 * which is vhost specific. Instead, rte_vhost_get_vring_num should
520 * The number of queues, 0 on failure
523 uint32_t rte_vhost_get_queue_num(int vid);
526 * Get the number of vrings the device supports.
532 * The number of vrings, 0 on failure
534 uint16_t rte_vhost_get_vring_num(int vid);
537 * Get the virtio net device's ifname, which is the vhost-user socket
543 * The buffer to stored the queried ifname
548 * 0 on success, -1 on failure
550 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
553 * Get how many avail entries are left in the queue
561 * num of avail entries left
563 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
568 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
569 * be received from the physical port or from another virtual device. A packet
570 * count is returned to indicate the number of packets that were successfully
571 * added to the RX queue.
575 * virtio queue index in mq case
577 * array to contain packets to be enqueued
579 * packets num to be enqueued
581 * num of packets enqueued
583 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
584 struct rte_mbuf **pkts, uint16_t count);
587 * This function gets guest buffers from the virtio device TX virtqueue,
588 * construct host mbufs, copies guest buffer content to host mbufs and
589 * store them in pkts to be processed.
593 * virtio queue index in mq case
595 * mbuf_pool where host mbuf is allocated.
597 * array to contain packets to be dequeued
599 * packets num to be dequeued
601 * num of packets dequeued
603 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
604 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
607 * Get guest mem table: a list of memory regions.
609 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
610 * guest memory regions. Application should free it at destroy_device()
616 * To store the returned mem regions
618 * 0 on success, -1 on failure
620 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
623 * Get guest vring info, including the vring address, vring size, etc.
630 * the structure to hold the requested vring info
632 * 0 on success, -1 on failure
634 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
635 struct rte_vhost_vring *vring);
638 * Notify the guest that used descriptors have been added to the vring. This
639 * function acts as a memory barrier.
646 * 0 on success, -1 on failure
648 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
651 * Get vhost RX queue avail count.
656 * virtio queue index in mq case
658 * num of desc available
660 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
663 * Get log base and log size of the vhost device
672 * 0 on success, -1 on failure
676 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
679 * Get last_avail/used_idx of the vhost virtqueue
685 * @param last_avail_idx
686 * vhost last_avail_idx to get
687 * @param last_used_idx
688 * vhost last_used_idx to get
690 * 0 on success, -1 on failure
694 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
695 uint16_t *last_avail_idx, uint16_t *last_used_idx);
698 * Set last_avail/used_idx of the vhost virtqueue
704 * @param last_avail_idx
705 * last_avail_idx to set
706 * @param last_used_idx
707 * last_used_idx to set
709 * 0 on success, -1 on failure
713 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
714 uint16_t last_avail_idx, uint16_t last_used_idx);
717 * Register external message handling callbacks
722 * virtio external callbacks to register
724 * additional context passed to the callbacks
726 * 0 on success, -1 on failure
730 rte_vhost_extern_callback_register(int vid,
731 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
734 * Get vdpa device id for vhost device.
743 rte_vhost_get_vdpa_device_id(int vid);
749 #endif /* _RTE_VHOST_H_ */