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 +
153 * Convert guest physical address to host virtual address safely
155 * This variant of rte_vhost_gpa_to_vva() takes care all the
156 * requested length is mapped and contiguous in process address
160 * the guest memory regions
162 * the guest physical address for querying
164 * the size of the requested area to map, updated with actual size mapped
166 * the host virtual address on success, 0 on failure
168 static __rte_always_inline uint64_t
169 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
170 uint64_t gpa, uint64_t *len)
172 struct rte_vhost_mem_region *r;
175 for (i = 0; i < mem->nregions; i++) {
176 r = &mem->regions[i];
177 if (gpa >= r->guest_phys_addr &&
178 gpa < r->guest_phys_addr + r->size) {
180 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
181 *len = r->guest_phys_addr + r->size - gpa;
183 return gpa - r->guest_phys_addr +
192 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
195 * Log the memory write start with given address.
197 * This function only need be invoked when the live migration starts.
198 * Therefore, we won't need call it at all in the most of time. For
199 * making the performance impact be minimum, it's suggested to do a
200 * check before calling it:
202 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
203 * rte_vhost_log_write(vid, addr, len);
208 * the starting address for write
210 * the length to write
212 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
215 * Log the used ring update start at given offset.
217 * Same as rte_vhost_log_write, it's suggested to do a check before
220 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
221 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
228 * the offset inside the used ring
230 * the length to write
232 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
233 uint64_t offset, uint64_t len);
235 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
238 * Register vhost driver. path could be different for multiple
241 int rte_vhost_driver_register(const char *path, uint64_t flags);
243 /* Unregister vhost driver. This is only meaningful to vhost user. */
244 int rte_vhost_driver_unregister(const char *path);
247 * Set the vdpa device id, enforce single connection per socket
250 * The vhost-user socket file path
254 * 0 on success, -1 on failure
256 int __rte_experimental
257 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
260 * Unset the vdpa device id
263 * The vhost-user socket file path
265 * 0 on success, -1 on failure
267 int __rte_experimental
268 rte_vhost_driver_detach_vdpa_device(const char *path);
274 * The vhost-user socket file path
276 * Device id, -1 on failure
278 int __rte_experimental
279 rte_vhost_driver_get_vdpa_device_id(const char *path);
282 * Set the feature bits the vhost-user driver supports.
285 * The vhost-user socket file path
289 * 0 on success, -1 on failure
291 int rte_vhost_driver_set_features(const char *path, uint64_t features);
294 * Enable vhost-user driver features.
297 * - the param features should be a subset of the feature bits provided
298 * by rte_vhost_driver_set_features().
299 * - it must be invoked before vhost-user negotiation starts.
302 * The vhost-user socket file path
306 * 0 on success, -1 on failure
308 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
311 * Disable vhost-user driver features.
313 * The two notes at rte_vhost_driver_enable_features() also apply here.
316 * The vhost-user socket file path
318 * Features to disable
320 * 0 on success, -1 on failure
322 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
325 * Get the feature bits before feature negotiation.
328 * The vhost-user socket file path
330 * A pointer to store the queried feature bits
332 * 0 on success, -1 on failure
334 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
337 * Get the protocol feature bits before feature negotiation.
340 * The vhost-user socket file path
341 * @param protocol_features
342 * A pointer to store the queried protocol feature bits
344 * 0 on success, -1 on failure
346 int __rte_experimental
347 rte_vhost_driver_get_protocol_features(const char *path,
348 uint64_t *protocol_features);
351 * Get the queue number bits before feature negotiation.
354 * The vhost-user socket file path
356 * A pointer to store the queried queue number bits
358 * 0 on success, -1 on failure
360 int __rte_experimental
361 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
364 * Get the feature bits after negotiation
369 * A pointer to store the queried feature bits
371 * 0 on success, -1 on failure
373 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
375 /* Register callbacks. */
376 int rte_vhost_driver_callback_register(const char *path,
377 struct vhost_device_ops const * const ops);
381 * Start the vhost-user driver.
383 * This function triggers the vhost-user negotiation.
386 * The vhost-user socket file path
388 * 0 on success, -1 on failure
390 int rte_vhost_driver_start(const char *path);
393 * Get the MTU value of the device if set in QEMU.
396 * virtio-net device ID
398 * The variable to store the MTU value
402 * -EAGAIN: device not yet started
403 * -ENOTSUP: device does not support MTU feature
405 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
408 * Get the numa node from which the virtio net device's memory
415 * The numa node, -1 on failure
417 int rte_vhost_get_numa_node(int vid);
421 * Get the number of queues the device supports.
423 * Note this function is deprecated, as it returns a queue pair number,
424 * which is vhost specific. Instead, rte_vhost_get_vring_num should
431 * The number of queues, 0 on failure
434 uint32_t rte_vhost_get_queue_num(int vid);
437 * Get the number of vrings the device supports.
443 * The number of vrings, 0 on failure
445 uint16_t rte_vhost_get_vring_num(int vid);
448 * Get the virtio net device's ifname, which is the vhost-user socket
454 * The buffer to stored the queried ifname
459 * 0 on success, -1 on failure
461 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
464 * Get how many avail entries are left in the queue
472 * num of avail entires left
474 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
479 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
480 * be received from the physical port or from another virtual device. A packet
481 * count is returned to indicate the number of packets that were successfully
482 * added to the RX queue.
486 * virtio queue index in mq case
488 * array to contain packets to be enqueued
490 * packets num to be enqueued
492 * num of packets enqueued
494 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
495 struct rte_mbuf **pkts, uint16_t count);
498 * This function gets guest buffers from the virtio device TX virtqueue,
499 * construct host mbufs, copies guest buffer content to host mbufs and
500 * store them in pkts to be processed.
504 * virtio queue index in mq case
506 * mbuf_pool where host mbuf is allocated.
508 * array to contain packets to be dequeued
510 * packets num to be dequeued
512 * num of packets dequeued
514 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
515 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
518 * Get guest mem table: a list of memory regions.
520 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
521 * guest memory regions. Application should free it at destroy_device()
527 * To store the returned mem regions
529 * 0 on success, -1 on failure
531 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
534 * Get guest vring info, including the vring address, vring size, etc.
541 * the structure to hold the requested vring info
543 * 0 on success, -1 on failure
545 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
546 struct rte_vhost_vring *vring);
549 * Notify the guest that used descriptors have been added to the vring. This
550 * function acts as a memory barrier.
557 * 0 on success, -1 on failure
559 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
562 * Get vhost RX queue avail count.
567 * virtio queue index in mq case
569 * num of desc available
571 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
574 * Get log base and log size of the vhost device
583 * 0 on success, -1 on failure
585 int __rte_experimental
586 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
589 * Get last_avail/used_idx of the vhost virtqueue
595 * @param last_avail_idx
596 * vhost last_avail_idx to get
597 * @param last_used_idx
598 * vhost last_used_idx to get
600 * 0 on success, -1 on failure
602 int __rte_experimental
603 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
604 uint16_t *last_avail_idx, uint16_t *last_used_idx);
607 * Set last_avail/used_idx of the vhost virtqueue
613 * @param last_avail_idx
614 * last_avail_idx to set
615 * @param last_used_idx
616 * last_used_idx to set
618 * 0 on success, -1 on failure
620 int __rte_experimental
621 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
622 uint16_t last_avail_idx, uint16_t last_used_idx);
625 * Get vdpa device id for vhost device.
632 int __rte_experimental
633 rte_vhost_get_vdpa_device_id(int vid);
639 #endif /* _RTE_VHOST_H_ */