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)
33 * Information relating to memory regions including offsets to
34 * addresses in QEMUs memory file.
36 struct rte_vhost_mem_region {
37 uint64_t guest_phys_addr;
38 uint64_t guest_user_addr;
39 uint64_t host_user_addr;
47 * Memory structure includes region and mapping information.
49 struct rte_vhost_memory {
51 struct rte_vhost_mem_region regions[];
54 struct rte_vhost_vring {
55 struct vring_desc *desc;
56 struct vring_avail *avail;
57 struct vring_used *used;
58 uint64_t log_guest_addr;
66 * Device and vring operations.
68 struct vhost_device_ops {
69 int (*new_device)(int vid); /**< Add device. */
70 void (*destroy_device)(int vid); /**< Remove device. */
72 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
75 * Features could be changed after the feature negotiation.
76 * For example, VHOST_F_LOG_ALL will be set/cleared at the
77 * start/end of live migration, respectively. This callback
78 * is used to inform the application on such change.
80 int (*features_changed)(int vid, uint64_t features);
82 int (*new_connection)(int vid);
83 void (*destroy_connection)(int vid);
85 void *reserved[2]; /**< Reserved for future extension */
89 * Convert guest physical address to host virtual address
92 * the guest memory regions
94 * the guest physical address for querying
96 * the host virtual address on success, 0 on failure
98 static __rte_always_inline uint64_t
99 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
101 struct rte_vhost_mem_region *reg;
104 for (i = 0; i < mem->nregions; i++) {
105 reg = &mem->regions[i];
106 if (gpa >= reg->guest_phys_addr &&
107 gpa < reg->guest_phys_addr + reg->size) {
108 return gpa - reg->guest_phys_addr +
116 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
119 * Log the memory write start with given address.
121 * This function only need be invoked when the live migration starts.
122 * Therefore, we won't need call it at all in the most of time. For
123 * making the performance impact be minimum, it's suggested to do a
124 * check before calling it:
126 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
127 * rte_vhost_log_write(vid, addr, len);
132 * the starting address for write
134 * the length to write
136 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
139 * Log the used ring update start at given offset.
141 * Same as rte_vhost_log_write, it's suggested to do a check before
144 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
145 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
152 * the offset inside the used ring
154 * the length to write
156 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
157 uint64_t offset, uint64_t len);
159 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
162 * Register vhost driver. path could be different for multiple
165 int rte_vhost_driver_register(const char *path, uint64_t flags);
167 /* Unregister vhost driver. This is only meaningful to vhost user. */
168 int rte_vhost_driver_unregister(const char *path);
171 * Set the feature bits the vhost-user driver supports.
174 * The vhost-user socket file path
178 * 0 on success, -1 on failure
180 int rte_vhost_driver_set_features(const char *path, uint64_t features);
183 * Enable vhost-user driver features.
186 * - the param features should be a subset of the feature bits provided
187 * by rte_vhost_driver_set_features().
188 * - it must be invoked before vhost-user negotiation starts.
191 * The vhost-user socket file path
195 * 0 on success, -1 on failure
197 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
200 * Disable vhost-user driver features.
202 * The two notes at rte_vhost_driver_enable_features() also apply here.
205 * The vhost-user socket file path
207 * Features to disable
209 * 0 on success, -1 on failure
211 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
214 * Get the feature bits before feature negotiation.
217 * The vhost-user socket file path
219 * A pointer to store the queried feature bits
221 * 0 on success, -1 on failure
223 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
226 * Get the feature bits after negotiation
231 * A pointer to store the queried feature bits
233 * 0 on success, -1 on failure
235 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
237 /* Register callbacks. */
238 int rte_vhost_driver_callback_register(const char *path,
239 struct vhost_device_ops const * const ops);
243 * Start the vhost-user driver.
245 * This function triggers the vhost-user negotiation.
248 * The vhost-user socket file path
250 * 0 on success, -1 on failure
252 int rte_vhost_driver_start(const char *path);
255 * Get the MTU value of the device if set in QEMU.
258 * virtio-net device ID
260 * The variable to store the MTU value
264 * -EAGAIN: device not yet started
265 * -ENOTSUP: device does not support MTU feature
267 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
270 * Get the numa node from which the virtio net device's memory
277 * The numa node, -1 on failure
279 int rte_vhost_get_numa_node(int vid);
283 * Get the number of queues the device supports.
285 * Note this function is deprecated, as it returns a queue pair number,
286 * which is vhost specific. Instead, rte_vhost_get_vring_num should
293 * The number of queues, 0 on failure
296 uint32_t rte_vhost_get_queue_num(int vid);
299 * Get the number of vrings the device supports.
305 * The number of vrings, 0 on failure
307 uint16_t rte_vhost_get_vring_num(int vid);
310 * Get the virtio net device's ifname, which is the vhost-user socket
316 * The buffer to stored the queried ifname
321 * 0 on success, -1 on failure
323 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
326 * Get how many avail entries are left in the queue
334 * num of avail entires left
336 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
341 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
342 * be received from the physical port or from another virtual device. A packet
343 * count is returned to indicate the number of packets that were successfully
344 * added to the RX queue.
348 * virtio queue index in mq case
350 * array to contain packets to be enqueued
352 * packets num to be enqueued
354 * num of packets enqueued
356 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
357 struct rte_mbuf **pkts, uint16_t count);
360 * This function gets guest buffers from the virtio device TX virtqueue,
361 * construct host mbufs, copies guest buffer content to host mbufs and
362 * store them in pkts to be processed.
366 * virtio queue index in mq case
368 * mbuf_pool where host mbuf is allocated.
370 * array to contain packets to be dequeued
372 * packets num to be dequeued
374 * num of packets dequeued
376 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
377 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
380 * Get guest mem table: a list of memory regions.
382 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
383 * guest memory regions. Application should free it at destroy_device()
389 * To store the returned mem regions
391 * 0 on success, -1 on failure
393 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
396 * Get guest vring info, including the vring address, vring size, etc.
403 * the structure to hold the requested vring info
405 * 0 on success, -1 on failure
407 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
408 struct rte_vhost_vring *vring);
411 * Get vhost RX queue avail count.
416 * virtio queue index in mq case
418 * num of desc available
420 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
426 #endif /* _RTE_VHOST_H_ */