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)
31 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
33 /** Protocol features. */
34 #ifndef VHOST_USER_PROTOCOL_F_MQ
35 #define VHOST_USER_PROTOCOL_F_MQ 0
38 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
39 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
42 #ifndef VHOST_USER_PROTOCOL_F_RARP
43 #define VHOST_USER_PROTOCOL_F_RARP 2
46 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
47 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
50 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
51 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
54 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
55 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
58 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
59 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
62 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
63 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
66 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
67 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
70 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
71 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
74 /** Indicate whether protocol features negotiation is supported. */
75 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
76 #define VHOST_USER_F_PROTOCOL_FEATURES 30
80 * Information relating to memory regions including offsets to
81 * addresses in QEMUs memory file.
83 struct rte_vhost_mem_region {
84 uint64_t guest_phys_addr;
85 uint64_t guest_user_addr;
86 uint64_t host_user_addr;
94 * Memory structure includes region and mapping information.
96 struct rte_vhost_memory {
98 struct rte_vhost_mem_region regions[];
101 struct rte_vhost_vring {
102 struct vring_desc *desc;
103 struct vring_avail *avail;
104 struct vring_used *used;
105 uint64_t log_guest_addr;
107 /** Deprecated, use rte_vhost_vring_call() instead. */
115 * Device and vring operations.
117 struct vhost_device_ops {
118 int (*new_device)(int vid); /**< Add device. */
119 void (*destroy_device)(int vid); /**< Remove device. */
121 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
124 * Features could be changed after the feature negotiation.
125 * For example, VHOST_F_LOG_ALL will be set/cleared at the
126 * start/end of live migration, respectively. This callback
127 * is used to inform the application on such change.
129 int (*features_changed)(int vid, uint64_t features);
131 int (*new_connection)(int vid);
132 void (*destroy_connection)(int vid);
134 void *reserved[2]; /**< Reserved for future extension */
138 * Convert guest physical address to host virtual address
140 * This function is deprecated because unsafe.
141 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
142 * guest physical ranges are fully and contiguously mapped into
143 * process virtual address space.
146 * the guest memory regions
148 * the guest physical address for querying
150 * the host virtual address on success, 0 on failure
153 static __rte_always_inline uint64_t
154 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
156 struct rte_vhost_mem_region *reg;
159 for (i = 0; i < mem->nregions; i++) {
160 reg = &mem->regions[i];
161 if (gpa >= reg->guest_phys_addr &&
162 gpa < reg->guest_phys_addr + reg->size) {
163 return gpa - reg->guest_phys_addr +
172 * Convert guest physical address to host virtual address safely
174 * This variant of rte_vhost_gpa_to_vva() takes care all the
175 * requested length is mapped and contiguous in process address
179 * the guest memory regions
181 * the guest physical address for querying
183 * the size of the requested area to map, updated with actual size mapped
185 * the host virtual address on success, 0 on failure
187 static __rte_always_inline uint64_t
188 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
189 uint64_t gpa, uint64_t *len)
191 struct rte_vhost_mem_region *r;
194 for (i = 0; i < mem->nregions; i++) {
195 r = &mem->regions[i];
196 if (gpa >= r->guest_phys_addr &&
197 gpa < r->guest_phys_addr + r->size) {
199 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
200 *len = r->guest_phys_addr + r->size - gpa;
202 return gpa - r->guest_phys_addr +
211 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
214 * Log the memory write start with given address.
216 * This function only need be invoked when the live migration starts.
217 * Therefore, we won't need call it at all in the most of time. For
218 * making the performance impact be minimum, it's suggested to do a
219 * check before calling it:
221 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
222 * rte_vhost_log_write(vid, addr, len);
227 * the starting address for write
229 * the length to write
231 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
234 * Log the used ring update start at given offset.
236 * Same as rte_vhost_log_write, it's suggested to do a check before
239 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
240 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
247 * the offset inside the used ring
249 * the length to write
251 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
252 uint64_t offset, uint64_t len);
254 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
257 * Register vhost driver. path could be different for multiple
260 int rte_vhost_driver_register(const char *path, uint64_t flags);
262 /* Unregister vhost driver. This is only meaningful to vhost user. */
263 int rte_vhost_driver_unregister(const char *path);
266 * Set the vdpa device id, enforce single connection per socket
269 * The vhost-user socket file path
273 * 0 on success, -1 on failure
275 int __rte_experimental
276 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
279 * Unset the vdpa device id
282 * The vhost-user socket file path
284 * 0 on success, -1 on failure
286 int __rte_experimental
287 rte_vhost_driver_detach_vdpa_device(const char *path);
293 * The vhost-user socket file path
295 * Device id, -1 on failure
297 int __rte_experimental
298 rte_vhost_driver_get_vdpa_device_id(const char *path);
301 * Set the feature bits the vhost-user driver supports.
304 * The vhost-user socket file path
308 * 0 on success, -1 on failure
310 int rte_vhost_driver_set_features(const char *path, uint64_t features);
313 * Enable vhost-user driver features.
316 * - the param features should be a subset of the feature bits provided
317 * by rte_vhost_driver_set_features().
318 * - it must be invoked before vhost-user negotiation starts.
321 * The vhost-user socket file path
325 * 0 on success, -1 on failure
327 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
330 * Disable vhost-user driver features.
332 * The two notes at rte_vhost_driver_enable_features() also apply here.
335 * The vhost-user socket file path
337 * Features to disable
339 * 0 on success, -1 on failure
341 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
344 * Get the feature bits before feature negotiation.
347 * The vhost-user socket file path
349 * A pointer to store the queried feature bits
351 * 0 on success, -1 on failure
353 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
356 * Get the protocol feature bits before feature negotiation.
359 * The vhost-user socket file path
360 * @param protocol_features
361 * A pointer to store the queried protocol feature bits
363 * 0 on success, -1 on failure
365 int __rte_experimental
366 rte_vhost_driver_get_protocol_features(const char *path,
367 uint64_t *protocol_features);
370 * Get the queue number bits before feature negotiation.
373 * The vhost-user socket file path
375 * A pointer to store the queried queue number bits
377 * 0 on success, -1 on failure
379 int __rte_experimental
380 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
383 * Get the feature bits after negotiation
388 * A pointer to store the queried feature bits
390 * 0 on success, -1 on failure
392 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
394 /* Register callbacks. */
395 int rte_vhost_driver_callback_register(const char *path,
396 struct vhost_device_ops const * const ops);
400 * Start the vhost-user driver.
402 * This function triggers the vhost-user negotiation.
405 * The vhost-user socket file path
407 * 0 on success, -1 on failure
409 int rte_vhost_driver_start(const char *path);
412 * Get the MTU value of the device if set in QEMU.
415 * virtio-net device ID
417 * The variable to store the MTU value
421 * -EAGAIN: device not yet started
422 * -ENOTSUP: device does not support MTU feature
424 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
427 * Get the numa node from which the virtio net device's memory
434 * The numa node, -1 on failure
436 int rte_vhost_get_numa_node(int vid);
440 * Get the number of queues the device supports.
442 * Note this function is deprecated, as it returns a queue pair number,
443 * which is vhost specific. Instead, rte_vhost_get_vring_num should
450 * The number of queues, 0 on failure
453 uint32_t rte_vhost_get_queue_num(int vid);
456 * Get the number of vrings the device supports.
462 * The number of vrings, 0 on failure
464 uint16_t rte_vhost_get_vring_num(int vid);
467 * Get the virtio net device's ifname, which is the vhost-user socket
473 * The buffer to stored the queried ifname
478 * 0 on success, -1 on failure
480 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
483 * Get how many avail entries are left in the queue
491 * num of avail entires left
493 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
498 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
499 * be received from the physical port or from another virtual device. A packet
500 * count is returned to indicate the number of packets that were successfully
501 * added to the RX queue.
505 * virtio queue index in mq case
507 * array to contain packets to be enqueued
509 * packets num to be enqueued
511 * num of packets enqueued
513 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
514 struct rte_mbuf **pkts, uint16_t count);
517 * This function gets guest buffers from the virtio device TX virtqueue,
518 * construct host mbufs, copies guest buffer content to host mbufs and
519 * store them in pkts to be processed.
523 * virtio queue index in mq case
525 * mbuf_pool where host mbuf is allocated.
527 * array to contain packets to be dequeued
529 * packets num to be dequeued
531 * num of packets dequeued
533 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
534 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
537 * Get guest mem table: a list of memory regions.
539 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
540 * guest memory regions. Application should free it at destroy_device()
546 * To store the returned mem regions
548 * 0 on success, -1 on failure
550 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
553 * Get guest vring info, including the vring address, vring size, etc.
560 * the structure to hold the requested vring info
562 * 0 on success, -1 on failure
564 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
565 struct rte_vhost_vring *vring);
568 * Notify the guest that used descriptors have been added to the vring. This
569 * function acts as a memory barrier.
576 * 0 on success, -1 on failure
578 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
581 * Get vhost RX queue avail count.
586 * virtio queue index in mq case
588 * num of desc available
590 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
593 * Get log base and log size of the vhost device
602 * 0 on success, -1 on failure
604 int __rte_experimental
605 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
608 * Get last_avail/used_idx of the vhost virtqueue
614 * @param last_avail_idx
615 * vhost last_avail_idx to get
616 * @param last_used_idx
617 * vhost last_used_idx to get
619 * 0 on success, -1 on failure
621 int __rte_experimental
622 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
623 uint16_t *last_avail_idx, uint16_t *last_used_idx);
626 * Set last_avail/used_idx of the vhost virtqueue
632 * @param last_avail_idx
633 * last_avail_idx to set
634 * @param last_used_idx
635 * last_used_idx to set
637 * 0 on success, -1 on failure
639 int __rte_experimental
640 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
641 uint16_t last_avail_idx, uint16_t last_used_idx);
644 * Get vdpa device id for vhost device.
651 int __rte_experimental
652 rte_vhost_get_vdpa_device_id(int vid);
658 #endif /* _RTE_VHOST_H_ */