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
128 * the guest memory regions
130 * the guest physical address for querying
132 * the host virtual address on success, 0 on failure
134 static __rte_always_inline uint64_t
135 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
137 struct rte_vhost_mem_region *reg;
140 for (i = 0; i < mem->nregions; i++) {
141 reg = &mem->regions[i];
142 if (gpa >= reg->guest_phys_addr &&
143 gpa < reg->guest_phys_addr + reg->size) {
144 return gpa - reg->guest_phys_addr +
152 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
155 * Log the memory write start with given address.
157 * This function only need be invoked when the live migration starts.
158 * Therefore, we won't need call it at all in the most of time. For
159 * making the performance impact be minimum, it's suggested to do a
160 * check before calling it:
162 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
163 * rte_vhost_log_write(vid, addr, len);
168 * the starting address for write
170 * the length to write
172 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
175 * Log the used ring update start at given offset.
177 * Same as rte_vhost_log_write, it's suggested to do a check before
180 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
181 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
188 * the offset inside the used ring
190 * the length to write
192 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
193 uint64_t offset, uint64_t len);
195 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
198 * Register vhost driver. path could be different for multiple
201 int rte_vhost_driver_register(const char *path, uint64_t flags);
203 /* Unregister vhost driver. This is only meaningful to vhost user. */
204 int rte_vhost_driver_unregister(const char *path);
207 * Set the vdpa device id, enforce single connection per socket
210 * The vhost-user socket file path
214 * 0 on success, -1 on failure
216 int __rte_experimental
217 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
220 * Unset the vdpa device id
223 * The vhost-user socket file path
225 * 0 on success, -1 on failure
227 int __rte_experimental
228 rte_vhost_driver_detach_vdpa_device(const char *path);
234 * The vhost-user socket file path
236 * Device id, -1 on failure
238 int __rte_experimental
239 rte_vhost_driver_get_vdpa_device_id(const char *path);
242 * Set the feature bits the vhost-user driver supports.
245 * The vhost-user socket file path
249 * 0 on success, -1 on failure
251 int rte_vhost_driver_set_features(const char *path, uint64_t features);
254 * Enable vhost-user driver features.
257 * - the param features should be a subset of the feature bits provided
258 * by rte_vhost_driver_set_features().
259 * - it must be invoked before vhost-user negotiation starts.
262 * The vhost-user socket file path
266 * 0 on success, -1 on failure
268 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
271 * Disable vhost-user driver features.
273 * The two notes at rte_vhost_driver_enable_features() also apply here.
276 * The vhost-user socket file path
278 * Features to disable
280 * 0 on success, -1 on failure
282 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
285 * Get the feature bits before feature negotiation.
288 * The vhost-user socket file path
290 * A pointer to store the queried feature bits
292 * 0 on success, -1 on failure
294 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
297 * Get the protocol feature bits before feature negotiation.
300 * The vhost-user socket file path
301 * @param protocol_features
302 * A pointer to store the queried protocol feature bits
304 * 0 on success, -1 on failure
306 int __rte_experimental
307 rte_vhost_driver_get_protocol_features(const char *path,
308 uint64_t *protocol_features);
311 * Get the queue number bits before feature negotiation.
314 * The vhost-user socket file path
316 * A pointer to store the queried queue number bits
318 * 0 on success, -1 on failure
320 int __rte_experimental
321 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
324 * Get the feature bits after negotiation
329 * A pointer to store the queried feature bits
331 * 0 on success, -1 on failure
333 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
335 /* Register callbacks. */
336 int rte_vhost_driver_callback_register(const char *path,
337 struct vhost_device_ops const * const ops);
341 * Start the vhost-user driver.
343 * This function triggers the vhost-user negotiation.
346 * The vhost-user socket file path
348 * 0 on success, -1 on failure
350 int rte_vhost_driver_start(const char *path);
353 * Get the MTU value of the device if set in QEMU.
356 * virtio-net device ID
358 * The variable to store the MTU value
362 * -EAGAIN: device not yet started
363 * -ENOTSUP: device does not support MTU feature
365 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
368 * Get the numa node from which the virtio net device's memory
375 * The numa node, -1 on failure
377 int rte_vhost_get_numa_node(int vid);
381 * Get the number of queues the device supports.
383 * Note this function is deprecated, as it returns a queue pair number,
384 * which is vhost specific. Instead, rte_vhost_get_vring_num should
391 * The number of queues, 0 on failure
394 uint32_t rte_vhost_get_queue_num(int vid);
397 * Get the number of vrings the device supports.
403 * The number of vrings, 0 on failure
405 uint16_t rte_vhost_get_vring_num(int vid);
408 * Get the virtio net device's ifname, which is the vhost-user socket
414 * The buffer to stored the queried ifname
419 * 0 on success, -1 on failure
421 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
424 * Get how many avail entries are left in the queue
432 * num of avail entires left
434 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
439 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
440 * be received from the physical port or from another virtual device. A packet
441 * count is returned to indicate the number of packets that were successfully
442 * added to the RX queue.
446 * virtio queue index in mq case
448 * array to contain packets to be enqueued
450 * packets num to be enqueued
452 * num of packets enqueued
454 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
455 struct rte_mbuf **pkts, uint16_t count);
458 * This function gets guest buffers from the virtio device TX virtqueue,
459 * construct host mbufs, copies guest buffer content to host mbufs and
460 * store them in pkts to be processed.
464 * virtio queue index in mq case
466 * mbuf_pool where host mbuf is allocated.
468 * array to contain packets to be dequeued
470 * packets num to be dequeued
472 * num of packets dequeued
474 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
475 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
478 * Get guest mem table: a list of memory regions.
480 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
481 * guest memory regions. Application should free it at destroy_device()
487 * To store the returned mem regions
489 * 0 on success, -1 on failure
491 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
494 * Get guest vring info, including the vring address, vring size, etc.
501 * the structure to hold the requested vring info
503 * 0 on success, -1 on failure
505 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
506 struct rte_vhost_vring *vring);
509 * Notify the guest that used descriptors have been added to the vring. This
510 * function acts as a memory barrier.
517 * 0 on success, -1 on failure
519 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
522 * Get vhost RX queue avail count.
527 * virtio queue index in mq case
529 * num of desc available
531 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
534 * Get log base and log size of the vhost device
543 * 0 on success, -1 on failure
545 int __rte_experimental
546 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
549 * Get last_avail/used_idx of the vhost virtqueue
555 * @param last_avail_idx
556 * vhost last_avail_idx to get
557 * @param last_used_idx
558 * vhost last_used_idx to get
560 * 0 on success, -1 on failure
562 int __rte_experimental
563 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
564 uint16_t *last_avail_idx, uint16_t *last_used_idx);
567 * Set last_avail/used_idx of the vhost virtqueue
573 * @param last_avail_idx
574 * last_avail_idx to set
575 * @param last_used_idx
576 * last_used_idx to set
578 * 0 on success, -1 on failure
580 int __rte_experimental
581 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
582 uint16_t last_avail_idx, uint16_t last_used_idx);
585 * Get vdpa device id for vhost device.
592 int __rte_experimental
593 rte_vhost_get_vdpa_device_id(int vid);
599 #endif /* _RTE_VHOST_H_ */