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>
27 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
28 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
29 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
30 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
32 /** Protocol features. */
33 #ifndef VHOST_USER_PROTOCOL_F_MQ
34 #define VHOST_USER_PROTOCOL_F_MQ 0
37 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
38 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
41 #ifndef VHOST_USER_PROTOCOL_F_RARP
42 #define VHOST_USER_PROTOCOL_F_RARP 2
45 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
46 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
49 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
50 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
53 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
54 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
57 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
58 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
61 /** Indicate whether protocol features negotiation is supported. */
62 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
63 #define VHOST_USER_F_PROTOCOL_FEATURES 30
67 * Information relating to memory regions including offsets to
68 * addresses in QEMUs memory file.
70 struct rte_vhost_mem_region {
71 uint64_t guest_phys_addr;
72 uint64_t guest_user_addr;
73 uint64_t host_user_addr;
81 * Memory structure includes region and mapping information.
83 struct rte_vhost_memory {
85 struct rte_vhost_mem_region regions[];
88 struct rte_vhost_vring {
89 struct vring_desc *desc;
90 struct vring_avail *avail;
91 struct vring_used *used;
92 uint64_t log_guest_addr;
94 /** Deprecated, use rte_vhost_vring_call() instead. */
102 * Device and vring operations.
104 struct vhost_device_ops {
105 int (*new_device)(int vid); /**< Add device. */
106 void (*destroy_device)(int vid); /**< Remove device. */
108 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
111 * Features could be changed after the feature negotiation.
112 * For example, VHOST_F_LOG_ALL will be set/cleared at the
113 * start/end of live migration, respectively. This callback
114 * is used to inform the application on such change.
116 int (*features_changed)(int vid, uint64_t features);
118 int (*new_connection)(int vid);
119 void (*destroy_connection)(int vid);
121 void *reserved[2]; /**< Reserved for future extension */
125 * Convert guest physical address to host virtual address
127 * This function is deprecated because unsafe.
128 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
129 * guest physical ranges are fully and contiguously mapped into
130 * process virtual address space.
133 * the guest memory regions
135 * the guest physical address for querying
137 * the host virtual address on success, 0 on failure
140 static __rte_always_inline uint64_t
141 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
143 struct rte_vhost_mem_region *reg;
146 for (i = 0; i < mem->nregions; i++) {
147 reg = &mem->regions[i];
148 if (gpa >= reg->guest_phys_addr &&
149 gpa < reg->guest_phys_addr + reg->size) {
150 return gpa - reg->guest_phys_addr +
159 * Convert guest physical address to host virtual address safely
161 * This variant of rte_vhost_gpa_to_vva() takes care all the
162 * requested length is mapped and contiguous in process address
166 * the guest memory regions
168 * the guest physical address for querying
170 * the size of the requested area to map, updated with actual size mapped
172 * the host virtual address on success, 0 on failure
174 static __rte_always_inline uint64_t
175 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
176 uint64_t gpa, uint64_t *len)
178 struct rte_vhost_mem_region *r;
181 for (i = 0; i < mem->nregions; i++) {
182 r = &mem->regions[i];
183 if (gpa >= r->guest_phys_addr &&
184 gpa < r->guest_phys_addr + r->size) {
186 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
187 *len = r->guest_phys_addr + r->size - gpa;
189 return gpa - r->guest_phys_addr +
198 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
201 * Log the memory write start with given address.
203 * This function only need be invoked when the live migration starts.
204 * Therefore, we won't need call it at all in the most of time. For
205 * making the performance impact be minimum, it's suggested to do a
206 * check before calling it:
208 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
209 * rte_vhost_log_write(vid, addr, len);
214 * the starting address for write
216 * the length to write
218 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
221 * Log the used ring update start at given offset.
223 * Same as rte_vhost_log_write, it's suggested to do a check before
226 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
227 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
234 * the offset inside the used ring
236 * the length to write
238 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
239 uint64_t offset, uint64_t len);
241 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
244 * Register vhost driver. path could be different for multiple
247 int rte_vhost_driver_register(const char *path, uint64_t flags);
249 /* Unregister vhost driver. This is only meaningful to vhost user. */
250 int rte_vhost_driver_unregister(const char *path);
253 * Set the vdpa device id, enforce single connection per socket
256 * The vhost-user socket file path
260 * 0 on success, -1 on failure
262 int __rte_experimental
263 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
266 * Unset the vdpa device id
269 * The vhost-user socket file path
271 * 0 on success, -1 on failure
273 int __rte_experimental
274 rte_vhost_driver_detach_vdpa_device(const char *path);
280 * The vhost-user socket file path
282 * Device id, -1 on failure
284 int __rte_experimental
285 rte_vhost_driver_get_vdpa_device_id(const char *path);
288 * Set the feature bits the vhost-user driver supports.
291 * The vhost-user socket file path
295 * 0 on success, -1 on failure
297 int rte_vhost_driver_set_features(const char *path, uint64_t features);
300 * Enable vhost-user driver features.
303 * - the param features should be a subset of the feature bits provided
304 * by rte_vhost_driver_set_features().
305 * - it must be invoked before vhost-user negotiation starts.
308 * The vhost-user socket file path
312 * 0 on success, -1 on failure
314 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
317 * Disable vhost-user driver features.
319 * The two notes at rte_vhost_driver_enable_features() also apply here.
322 * The vhost-user socket file path
324 * Features to disable
326 * 0 on success, -1 on failure
328 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
331 * Get the feature bits before feature negotiation.
334 * The vhost-user socket file path
336 * A pointer to store the queried feature bits
338 * 0 on success, -1 on failure
340 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
343 * Get the protocol feature bits before feature negotiation.
346 * The vhost-user socket file path
347 * @param protocol_features
348 * A pointer to store the queried protocol feature bits
350 * 0 on success, -1 on failure
352 int __rte_experimental
353 rte_vhost_driver_get_protocol_features(const char *path,
354 uint64_t *protocol_features);
357 * Get the queue number bits before feature negotiation.
360 * The vhost-user socket file path
362 * A pointer to store the queried queue number bits
364 * 0 on success, -1 on failure
366 int __rte_experimental
367 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
370 * Get the feature bits after negotiation
375 * A pointer to store the queried feature bits
377 * 0 on success, -1 on failure
379 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
381 /* Register callbacks. */
382 int rte_vhost_driver_callback_register(const char *path,
383 struct vhost_device_ops const * const ops);
387 * Start the vhost-user driver.
389 * This function triggers the vhost-user negotiation.
392 * The vhost-user socket file path
394 * 0 on success, -1 on failure
396 int rte_vhost_driver_start(const char *path);
399 * Get the MTU value of the device if set in QEMU.
402 * virtio-net device ID
404 * The variable to store the MTU value
408 * -EAGAIN: device not yet started
409 * -ENOTSUP: device does not support MTU feature
411 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
414 * Get the numa node from which the virtio net device's memory
421 * The numa node, -1 on failure
423 int rte_vhost_get_numa_node(int vid);
427 * Get the number of queues the device supports.
429 * Note this function is deprecated, as it returns a queue pair number,
430 * which is vhost specific. Instead, rte_vhost_get_vring_num should
437 * The number of queues, 0 on failure
440 uint32_t rte_vhost_get_queue_num(int vid);
443 * Get the number of vrings the device supports.
449 * The number of vrings, 0 on failure
451 uint16_t rte_vhost_get_vring_num(int vid);
454 * Get the virtio net device's ifname, which is the vhost-user socket
460 * The buffer to stored the queried ifname
465 * 0 on success, -1 on failure
467 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
470 * Get how many avail entries are left in the queue
478 * num of avail entires left
480 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
485 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
486 * be received from the physical port or from another virtual device. A packet
487 * count is returned to indicate the number of packets that were successfully
488 * added to the RX queue.
492 * virtio queue index in mq case
494 * array to contain packets to be enqueued
496 * packets num to be enqueued
498 * num of packets enqueued
500 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
501 struct rte_mbuf **pkts, uint16_t count);
504 * This function gets guest buffers from the virtio device TX virtqueue,
505 * construct host mbufs, copies guest buffer content to host mbufs and
506 * store them in pkts to be processed.
510 * virtio queue index in mq case
512 * mbuf_pool where host mbuf is allocated.
514 * array to contain packets to be dequeued
516 * packets num to be dequeued
518 * num of packets dequeued
520 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
521 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
524 * Get guest mem table: a list of memory regions.
526 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
527 * guest memory regions. Application should free it at destroy_device()
533 * To store the returned mem regions
535 * 0 on success, -1 on failure
537 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
540 * Get guest vring info, including the vring address, vring size, etc.
547 * the structure to hold the requested vring info
549 * 0 on success, -1 on failure
551 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
552 struct rte_vhost_vring *vring);
555 * Notify the guest that used descriptors have been added to the vring. This
556 * function acts as a memory barrier.
563 * 0 on success, -1 on failure
565 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
568 * Get vhost RX queue avail count.
573 * virtio queue index in mq case
575 * num of desc available
577 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
580 * Get log base and log size of the vhost device
589 * 0 on success, -1 on failure
591 int __rte_experimental
592 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
595 * Get last_avail/used_idx of the vhost virtqueue
601 * @param last_avail_idx
602 * vhost last_avail_idx to get
603 * @param last_used_idx
604 * vhost last_used_idx to get
606 * 0 on success, -1 on failure
608 int __rte_experimental
609 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
610 uint16_t *last_avail_idx, uint16_t *last_used_idx);
613 * Set last_avail/used_idx of the vhost virtqueue
619 * @param last_avail_idx
620 * last_avail_idx to set
621 * @param last_used_idx
622 * last_used_idx to set
624 * 0 on success, -1 on failure
626 int __rte_experimental
627 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
628 uint16_t last_avail_idx, uint16_t last_used_idx);
631 * Get vdpa device id for vhost device.
638 int __rte_experimental
639 rte_vhost_get_vdpa_device_id(int vid);
645 #endif /* _RTE_VHOST_H_ */