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 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
62 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
65 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
66 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
69 /** Indicate whether protocol features negotiation is supported. */
70 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
71 #define VHOST_USER_F_PROTOCOL_FEATURES 30
75 * Information relating to memory regions including offsets to
76 * addresses in QEMUs memory file.
78 struct rte_vhost_mem_region {
79 uint64_t guest_phys_addr;
80 uint64_t guest_user_addr;
81 uint64_t host_user_addr;
89 * Memory structure includes region and mapping information.
91 struct rte_vhost_memory {
93 struct rte_vhost_mem_region regions[];
96 struct rte_vhost_vring {
97 struct vring_desc *desc;
98 struct vring_avail *avail;
99 struct vring_used *used;
100 uint64_t log_guest_addr;
102 /** Deprecated, use rte_vhost_vring_call() instead. */
110 * Device and vring operations.
112 struct vhost_device_ops {
113 int (*new_device)(int vid); /**< Add device. */
114 void (*destroy_device)(int vid); /**< Remove device. */
116 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
119 * Features could be changed after the feature negotiation.
120 * For example, VHOST_F_LOG_ALL will be set/cleared at the
121 * start/end of live migration, respectively. This callback
122 * is used to inform the application on such change.
124 int (*features_changed)(int vid, uint64_t features);
126 int (*new_connection)(int vid);
127 void (*destroy_connection)(int vid);
129 void *reserved[2]; /**< Reserved for future extension */
133 * Convert guest physical address to host virtual address
135 * This function is deprecated because unsafe.
136 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
137 * guest physical ranges are fully and contiguously mapped into
138 * process virtual address space.
141 * the guest memory regions
143 * the guest physical address for querying
145 * the host virtual address on success, 0 on failure
148 static __rte_always_inline uint64_t
149 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
151 struct rte_vhost_mem_region *reg;
154 for (i = 0; i < mem->nregions; i++) {
155 reg = &mem->regions[i];
156 if (gpa >= reg->guest_phys_addr &&
157 gpa < reg->guest_phys_addr + reg->size) {
158 return gpa - reg->guest_phys_addr +
167 * Convert guest physical address to host virtual address safely
169 * This variant of rte_vhost_gpa_to_vva() takes care all the
170 * requested length is mapped and contiguous in process address
174 * the guest memory regions
176 * the guest physical address for querying
178 * the size of the requested area to map, updated with actual size mapped
180 * the host virtual address on success, 0 on failure
182 static __rte_always_inline uint64_t
183 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
184 uint64_t gpa, uint64_t *len)
186 struct rte_vhost_mem_region *r;
189 for (i = 0; i < mem->nregions; i++) {
190 r = &mem->regions[i];
191 if (gpa >= r->guest_phys_addr &&
192 gpa < r->guest_phys_addr + r->size) {
194 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
195 *len = r->guest_phys_addr + r->size - gpa;
197 return gpa - r->guest_phys_addr +
206 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
209 * Log the memory write start with given address.
211 * This function only need be invoked when the live migration starts.
212 * Therefore, we won't need call it at all in the most of time. For
213 * making the performance impact be minimum, it's suggested to do a
214 * check before calling it:
216 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
217 * rte_vhost_log_write(vid, addr, len);
222 * the starting address for write
224 * the length to write
226 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
229 * Log the used ring update start at given offset.
231 * Same as rte_vhost_log_write, it's suggested to do a check before
234 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
235 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
242 * the offset inside the used ring
244 * the length to write
246 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
247 uint64_t offset, uint64_t len);
249 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
252 * Register vhost driver. path could be different for multiple
255 int rte_vhost_driver_register(const char *path, uint64_t flags);
257 /* Unregister vhost driver. This is only meaningful to vhost user. */
258 int rte_vhost_driver_unregister(const char *path);
261 * Set the vdpa device id, enforce single connection per socket
264 * The vhost-user socket file path
268 * 0 on success, -1 on failure
270 int __rte_experimental
271 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
274 * Unset the vdpa device id
277 * The vhost-user socket file path
279 * 0 on success, -1 on failure
281 int __rte_experimental
282 rte_vhost_driver_detach_vdpa_device(const char *path);
288 * The vhost-user socket file path
290 * Device id, -1 on failure
292 int __rte_experimental
293 rte_vhost_driver_get_vdpa_device_id(const char *path);
296 * Set the feature bits the vhost-user driver supports.
299 * The vhost-user socket file path
303 * 0 on success, -1 on failure
305 int rte_vhost_driver_set_features(const char *path, uint64_t features);
308 * Enable vhost-user driver features.
311 * - the param features should be a subset of the feature bits provided
312 * by rte_vhost_driver_set_features().
313 * - it must be invoked before vhost-user negotiation starts.
316 * The vhost-user socket file path
320 * 0 on success, -1 on failure
322 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
325 * Disable vhost-user driver features.
327 * The two notes at rte_vhost_driver_enable_features() also apply here.
330 * The vhost-user socket file path
332 * Features to disable
334 * 0 on success, -1 on failure
336 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
339 * Get the feature bits before feature negotiation.
342 * The vhost-user socket file path
344 * A pointer to store the queried feature bits
346 * 0 on success, -1 on failure
348 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
351 * Get the protocol feature bits before feature negotiation.
354 * The vhost-user socket file path
355 * @param protocol_features
356 * A pointer to store the queried protocol feature bits
358 * 0 on success, -1 on failure
360 int __rte_experimental
361 rte_vhost_driver_get_protocol_features(const char *path,
362 uint64_t *protocol_features);
365 * Get the queue number bits before feature negotiation.
368 * The vhost-user socket file path
370 * A pointer to store the queried queue number bits
372 * 0 on success, -1 on failure
374 int __rte_experimental
375 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
378 * Get the feature bits after negotiation
383 * A pointer to store the queried feature bits
385 * 0 on success, -1 on failure
387 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
389 /* Register callbacks. */
390 int rte_vhost_driver_callback_register(const char *path,
391 struct vhost_device_ops const * const ops);
395 * Start the vhost-user driver.
397 * This function triggers the vhost-user negotiation.
400 * The vhost-user socket file path
402 * 0 on success, -1 on failure
404 int rte_vhost_driver_start(const char *path);
407 * Get the MTU value of the device if set in QEMU.
410 * virtio-net device ID
412 * The variable to store the MTU value
416 * -EAGAIN: device not yet started
417 * -ENOTSUP: device does not support MTU feature
419 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
422 * Get the numa node from which the virtio net device's memory
429 * The numa node, -1 on failure
431 int rte_vhost_get_numa_node(int vid);
435 * Get the number of queues the device supports.
437 * Note this function is deprecated, as it returns a queue pair number,
438 * which is vhost specific. Instead, rte_vhost_get_vring_num should
445 * The number of queues, 0 on failure
448 uint32_t rte_vhost_get_queue_num(int vid);
451 * Get the number of vrings the device supports.
457 * The number of vrings, 0 on failure
459 uint16_t rte_vhost_get_vring_num(int vid);
462 * Get the virtio net device's ifname, which is the vhost-user socket
468 * The buffer to stored the queried ifname
473 * 0 on success, -1 on failure
475 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
478 * Get how many avail entries are left in the queue
486 * num of avail entires left
488 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
493 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
494 * be received from the physical port or from another virtual device. A packet
495 * count is returned to indicate the number of packets that were successfully
496 * added to the RX queue.
500 * virtio queue index in mq case
502 * array to contain packets to be enqueued
504 * packets num to be enqueued
506 * num of packets enqueued
508 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
509 struct rte_mbuf **pkts, uint16_t count);
512 * This function gets guest buffers from the virtio device TX virtqueue,
513 * construct host mbufs, copies guest buffer content to host mbufs and
514 * store them in pkts to be processed.
518 * virtio queue index in mq case
520 * mbuf_pool where host mbuf is allocated.
522 * array to contain packets to be dequeued
524 * packets num to be dequeued
526 * num of packets dequeued
528 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
529 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
532 * Get guest mem table: a list of memory regions.
534 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
535 * guest memory regions. Application should free it at destroy_device()
541 * To store the returned mem regions
543 * 0 on success, -1 on failure
545 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
548 * Get guest vring info, including the vring address, vring size, etc.
555 * the structure to hold the requested vring info
557 * 0 on success, -1 on failure
559 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
560 struct rte_vhost_vring *vring);
563 * Notify the guest that used descriptors have been added to the vring. This
564 * function acts as a memory barrier.
571 * 0 on success, -1 on failure
573 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
576 * Get vhost RX queue avail count.
581 * virtio queue index in mq case
583 * num of desc available
585 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
588 * Get log base and log size of the vhost device
597 * 0 on success, -1 on failure
599 int __rte_experimental
600 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
603 * Get last_avail/used_idx of the vhost virtqueue
609 * @param last_avail_idx
610 * vhost last_avail_idx to get
611 * @param last_used_idx
612 * vhost last_used_idx to get
614 * 0 on success, -1 on failure
616 int __rte_experimental
617 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
618 uint16_t *last_avail_idx, uint16_t *last_used_idx);
621 * Set last_avail/used_idx of the vhost virtqueue
627 * @param last_avail_idx
628 * last_avail_idx to set
629 * @param last_used_idx
630 * last_used_idx to set
632 * 0 on success, -1 on failure
634 int __rte_experimental
635 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
636 uint16_t last_avail_idx, uint16_t last_used_idx);
639 * Get vdpa device id for vhost device.
646 int __rte_experimental
647 rte_vhost_get_vdpa_device_id(int vid);
653 #endif /* _RTE_VHOST_H_ */