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;
60 /** Deprecated, use rte_vhost_vring_call() instead. */
68 * Device and vring operations.
70 struct vhost_device_ops {
71 int (*new_device)(int vid); /**< Add device. */
72 void (*destroy_device)(int vid); /**< Remove device. */
74 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
77 * Features could be changed after the feature negotiation.
78 * For example, VHOST_F_LOG_ALL will be set/cleared at the
79 * start/end of live migration, respectively. This callback
80 * is used to inform the application on such change.
82 int (*features_changed)(int vid, uint64_t features);
84 int (*new_connection)(int vid);
85 void (*destroy_connection)(int vid);
87 void *reserved[2]; /**< Reserved for future extension */
91 * Convert guest physical address to host virtual address
94 * the guest memory regions
96 * the guest physical address for querying
98 * the host virtual address on success, 0 on failure
100 static __rte_always_inline uint64_t
101 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
103 struct rte_vhost_mem_region *reg;
106 for (i = 0; i < mem->nregions; i++) {
107 reg = &mem->regions[i];
108 if (gpa >= reg->guest_phys_addr &&
109 gpa < reg->guest_phys_addr + reg->size) {
110 return gpa - reg->guest_phys_addr +
118 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
121 * Log the memory write start with given address.
123 * This function only need be invoked when the live migration starts.
124 * Therefore, we won't need call it at all in the most of time. For
125 * making the performance impact be minimum, it's suggested to do a
126 * check before calling it:
128 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
129 * rte_vhost_log_write(vid, addr, len);
134 * the starting address for write
136 * the length to write
138 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
141 * Log the used ring update start at given offset.
143 * Same as rte_vhost_log_write, it's suggested to do a check before
146 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
147 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
154 * the offset inside the used ring
156 * the length to write
158 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
159 uint64_t offset, uint64_t len);
161 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
164 * Register vhost driver. path could be different for multiple
167 int rte_vhost_driver_register(const char *path, uint64_t flags);
169 /* Unregister vhost driver. This is only meaningful to vhost user. */
170 int rte_vhost_driver_unregister(const char *path);
173 * Set the feature bits the vhost-user driver supports.
176 * The vhost-user socket file path
180 * 0 on success, -1 on failure
182 int rte_vhost_driver_set_features(const char *path, uint64_t features);
185 * Enable vhost-user driver features.
188 * - the param features should be a subset of the feature bits provided
189 * by rte_vhost_driver_set_features().
190 * - it must be invoked before vhost-user negotiation starts.
193 * The vhost-user socket file path
197 * 0 on success, -1 on failure
199 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
202 * Disable vhost-user driver features.
204 * The two notes at rte_vhost_driver_enable_features() also apply here.
207 * The vhost-user socket file path
209 * Features to disable
211 * 0 on success, -1 on failure
213 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
216 * Get the feature bits before feature negotiation.
219 * The vhost-user socket file path
221 * A pointer to store the queried feature bits
223 * 0 on success, -1 on failure
225 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
228 * Get the feature bits after negotiation
233 * A pointer to store the queried feature bits
235 * 0 on success, -1 on failure
237 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
239 /* Register callbacks. */
240 int rte_vhost_driver_callback_register(const char *path,
241 struct vhost_device_ops const * const ops);
245 * Start the vhost-user driver.
247 * This function triggers the vhost-user negotiation.
250 * The vhost-user socket file path
252 * 0 on success, -1 on failure
254 int rte_vhost_driver_start(const char *path);
257 * Get the MTU value of the device if set in QEMU.
260 * virtio-net device ID
262 * The variable to store the MTU value
266 * -EAGAIN: device not yet started
267 * -ENOTSUP: device does not support MTU feature
269 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
272 * Get the numa node from which the virtio net device's memory
279 * The numa node, -1 on failure
281 int rte_vhost_get_numa_node(int vid);
285 * Get the number of queues the device supports.
287 * Note this function is deprecated, as it returns a queue pair number,
288 * which is vhost specific. Instead, rte_vhost_get_vring_num should
295 * The number of queues, 0 on failure
298 uint32_t rte_vhost_get_queue_num(int vid);
301 * Get the number of vrings the device supports.
307 * The number of vrings, 0 on failure
309 uint16_t rte_vhost_get_vring_num(int vid);
312 * Get the virtio net device's ifname, which is the vhost-user socket
318 * The buffer to stored the queried ifname
323 * 0 on success, -1 on failure
325 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
328 * Get how many avail entries are left in the queue
336 * num of avail entires left
338 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
343 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
344 * be received from the physical port or from another virtual device. A packet
345 * count is returned to indicate the number of packets that were successfully
346 * added to the RX queue.
350 * virtio queue index in mq case
352 * array to contain packets to be enqueued
354 * packets num to be enqueued
356 * num of packets enqueued
358 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
359 struct rte_mbuf **pkts, uint16_t count);
362 * This function gets guest buffers from the virtio device TX virtqueue,
363 * construct host mbufs, copies guest buffer content to host mbufs and
364 * store them in pkts to be processed.
368 * virtio queue index in mq case
370 * mbuf_pool where host mbuf is allocated.
372 * array to contain packets to be dequeued
374 * packets num to be dequeued
376 * num of packets dequeued
378 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
379 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
382 * Get guest mem table: a list of memory regions.
384 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
385 * guest memory regions. Application should free it at destroy_device()
391 * To store the returned mem regions
393 * 0 on success, -1 on failure
395 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
398 * Get guest vring info, including the vring address, vring size, etc.
405 * the structure to hold the requested vring info
407 * 0 on success, -1 on failure
409 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
410 struct rte_vhost_vring *vring);
413 * Notify the guest that used descriptors have been added to the vring. This
414 * function acts as a memory barrier.
421 * 0 on success, -1 on failure
423 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
426 * Get vhost RX queue avail count.
431 * virtio queue index in mq case
433 * num of desc available
435 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
441 #endif /* _RTE_VHOST_H_ */