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 /** Indicate whether protocol features negotiation is supported. */
58 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
59 #define VHOST_USER_F_PROTOCOL_FEATURES 30
63 * Information relating to memory regions including offsets to
64 * addresses in QEMUs memory file.
66 struct rte_vhost_mem_region {
67 uint64_t guest_phys_addr;
68 uint64_t guest_user_addr;
69 uint64_t host_user_addr;
77 * Memory structure includes region and mapping information.
79 struct rte_vhost_memory {
81 struct rte_vhost_mem_region regions[];
84 struct rte_vhost_vring {
85 struct vring_desc *desc;
86 struct vring_avail *avail;
87 struct vring_used *used;
88 uint64_t log_guest_addr;
90 /** Deprecated, use rte_vhost_vring_call() instead. */
98 * Device and vring operations.
100 struct vhost_device_ops {
101 int (*new_device)(int vid); /**< Add device. */
102 void (*destroy_device)(int vid); /**< Remove device. */
104 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
107 * Features could be changed after the feature negotiation.
108 * For example, VHOST_F_LOG_ALL will be set/cleared at the
109 * start/end of live migration, respectively. This callback
110 * is used to inform the application on such change.
112 int (*features_changed)(int vid, uint64_t features);
114 int (*new_connection)(int vid);
115 void (*destroy_connection)(int vid);
117 void *reserved[2]; /**< Reserved for future extension */
121 * Convert guest physical address to host virtual address
124 * the guest memory regions
126 * the guest physical address for querying
128 * the host virtual address on success, 0 on failure
130 static __rte_always_inline uint64_t
131 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
133 struct rte_vhost_mem_region *reg;
136 for (i = 0; i < mem->nregions; i++) {
137 reg = &mem->regions[i];
138 if (gpa >= reg->guest_phys_addr &&
139 gpa < reg->guest_phys_addr + reg->size) {
140 return gpa - reg->guest_phys_addr +
148 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
151 * Log the memory write start with given address.
153 * This function only need be invoked when the live migration starts.
154 * Therefore, we won't need call it at all in the most of time. For
155 * making the performance impact be minimum, it's suggested to do a
156 * check before calling it:
158 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
159 * rte_vhost_log_write(vid, addr, len);
164 * the starting address for write
166 * the length to write
168 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
171 * Log the used ring update start at given offset.
173 * Same as rte_vhost_log_write, it's suggested to do a check before
176 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
177 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
184 * the offset inside the used ring
186 * the length to write
188 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
189 uint64_t offset, uint64_t len);
191 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
194 * Register vhost driver. path could be different for multiple
197 int rte_vhost_driver_register(const char *path, uint64_t flags);
199 /* Unregister vhost driver. This is only meaningful to vhost user. */
200 int rte_vhost_driver_unregister(const char *path);
203 * Set the vdpa device id, enforce single connection per socket
206 * The vhost-user socket file path
210 * 0 on success, -1 on failure
212 int __rte_experimental
213 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
216 * Unset the vdpa device id
219 * The vhost-user socket file path
221 * 0 on success, -1 on failure
223 int __rte_experimental
224 rte_vhost_driver_detach_vdpa_device(const char *path);
230 * The vhost-user socket file path
232 * Device id, -1 on failure
234 int __rte_experimental
235 rte_vhost_driver_get_vdpa_device_id(const char *path);
238 * Set the feature bits the vhost-user driver supports.
241 * The vhost-user socket file path
245 * 0 on success, -1 on failure
247 int rte_vhost_driver_set_features(const char *path, uint64_t features);
250 * Enable vhost-user driver features.
253 * - the param features should be a subset of the feature bits provided
254 * by rte_vhost_driver_set_features().
255 * - it must be invoked before vhost-user negotiation starts.
258 * The vhost-user socket file path
262 * 0 on success, -1 on failure
264 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
267 * Disable vhost-user driver features.
269 * The two notes at rte_vhost_driver_enable_features() also apply here.
272 * The vhost-user socket file path
274 * Features to disable
276 * 0 on success, -1 on failure
278 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
281 * Get the feature bits before feature negotiation.
284 * The vhost-user socket file path
286 * A pointer to store the queried feature bits
288 * 0 on success, -1 on failure
290 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
293 * Get the protocol feature bits before feature negotiation.
296 * The vhost-user socket file path
297 * @param protocol_features
298 * A pointer to store the queried protocol feature bits
300 * 0 on success, -1 on failure
302 int __rte_experimental
303 rte_vhost_driver_get_protocol_features(const char *path,
304 uint64_t *protocol_features);
307 * Get the queue number bits before feature negotiation.
310 * The vhost-user socket file path
312 * A pointer to store the queried queue number bits
314 * 0 on success, -1 on failure
316 int __rte_experimental
317 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
320 * Get the feature bits after negotiation
325 * A pointer to store the queried feature bits
327 * 0 on success, -1 on failure
329 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
331 /* Register callbacks. */
332 int rte_vhost_driver_callback_register(const char *path,
333 struct vhost_device_ops const * const ops);
337 * Start the vhost-user driver.
339 * This function triggers the vhost-user negotiation.
342 * The vhost-user socket file path
344 * 0 on success, -1 on failure
346 int rte_vhost_driver_start(const char *path);
349 * Get the MTU value of the device if set in QEMU.
352 * virtio-net device ID
354 * The variable to store the MTU value
358 * -EAGAIN: device not yet started
359 * -ENOTSUP: device does not support MTU feature
361 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
364 * Get the numa node from which the virtio net device's memory
371 * The numa node, -1 on failure
373 int rte_vhost_get_numa_node(int vid);
377 * Get the number of queues the device supports.
379 * Note this function is deprecated, as it returns a queue pair number,
380 * which is vhost specific. Instead, rte_vhost_get_vring_num should
387 * The number of queues, 0 on failure
390 uint32_t rte_vhost_get_queue_num(int vid);
393 * Get the number of vrings the device supports.
399 * The number of vrings, 0 on failure
401 uint16_t rte_vhost_get_vring_num(int vid);
404 * Get the virtio net device's ifname, which is the vhost-user socket
410 * The buffer to stored the queried ifname
415 * 0 on success, -1 on failure
417 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
420 * Get how many avail entries are left in the queue
428 * num of avail entires left
430 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
435 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
436 * be received from the physical port or from another virtual device. A packet
437 * count is returned to indicate the number of packets that were successfully
438 * added to the RX queue.
442 * virtio queue index in mq case
444 * array to contain packets to be enqueued
446 * packets num to be enqueued
448 * num of packets enqueued
450 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
451 struct rte_mbuf **pkts, uint16_t count);
454 * This function gets guest buffers from the virtio device TX virtqueue,
455 * construct host mbufs, copies guest buffer content to host mbufs and
456 * store them in pkts to be processed.
460 * virtio queue index in mq case
462 * mbuf_pool where host mbuf is allocated.
464 * array to contain packets to be dequeued
466 * packets num to be dequeued
468 * num of packets dequeued
470 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
471 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
474 * Get guest mem table: a list of memory regions.
476 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
477 * guest memory regions. Application should free it at destroy_device()
483 * To store the returned mem regions
485 * 0 on success, -1 on failure
487 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
490 * Get guest vring info, including the vring address, vring size, etc.
497 * the structure to hold the requested vring info
499 * 0 on success, -1 on failure
501 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
502 struct rte_vhost_vring *vring);
505 * Notify the guest that used descriptors have been added to the vring. This
506 * function acts as a memory barrier.
513 * 0 on success, -1 on failure
515 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
518 * Get vhost RX queue avail count.
523 * virtio queue index in mq case
525 * num of desc available
527 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
530 * Get vdpa device id for vhost device.
537 int __rte_experimental
538 rte_vhost_get_vdpa_device_id(int vid);
544 #endif /* _RTE_VHOST_H_ */