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>
26 #include <linux/virtio_net.h>
28 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
29 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
30 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
31 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
32 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
34 /** Protocol features. */
35 #ifndef VHOST_USER_PROTOCOL_F_MQ
36 #define VHOST_USER_PROTOCOL_F_MQ 0
39 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
40 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
43 #ifndef VHOST_USER_PROTOCOL_F_RARP
44 #define VHOST_USER_PROTOCOL_F_RARP 2
47 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
48 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
51 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
52 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
55 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
56 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
59 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
60 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
63 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
64 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
67 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
68 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
71 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
72 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
75 /** Indicate whether protocol features negotiation is supported. */
76 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
77 #define VHOST_USER_F_PROTOCOL_FEATURES 30
81 * Information relating to memory regions including offsets to
82 * addresses in QEMUs memory file.
84 struct rte_vhost_mem_region {
85 uint64_t guest_phys_addr;
86 uint64_t guest_user_addr;
87 uint64_t host_user_addr;
95 * Memory structure includes region and mapping information.
97 struct rte_vhost_memory {
99 struct rte_vhost_mem_region regions[];
102 struct rte_vhost_vring {
103 struct vring_desc *desc;
104 struct vring_avail *avail;
105 struct vring_used *used;
106 uint64_t log_guest_addr;
108 /** Deprecated, use rte_vhost_vring_call() instead. */
116 * Possible results of the vhost user message handling callbacks
118 enum rte_vhost_msg_result {
119 /* Message handling failed */
120 RTE_VHOST_MSG_RESULT_ERR = -1,
121 /* Message handling successful */
122 RTE_VHOST_MSG_RESULT_OK = 0,
123 /* Message handling successful and reply prepared */
124 RTE_VHOST_MSG_RESULT_REPLY = 1,
125 /* Message not handled */
126 RTE_VHOST_MSG_RESULT_NOT_HANDLED,
130 * Function prototype for the vhost backend to handle specific vhost user
138 * RTE_VHOST_MSG_RESULT_OK on success,
139 * RTE_VHOST_MSG_RESULT_REPLY on success with reply,
140 * RTE_VHOST_MSG_RESULT_ERR on failure,
141 * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
143 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
146 * Optional vhost user message handlers.
148 struct rte_vhost_user_extern_ops {
149 /* Called prior to the master message handling. */
150 rte_vhost_msg_handle pre_msg_handle;
151 /* Called after the master message handling. */
152 rte_vhost_msg_handle post_msg_handle;
156 * Device and vring operations.
158 struct vhost_device_ops {
159 int (*new_device)(int vid); /**< Add device. */
160 void (*destroy_device)(int vid); /**< Remove device. */
162 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
165 * Features could be changed after the feature negotiation.
166 * For example, VHOST_F_LOG_ALL will be set/cleared at the
167 * start/end of live migration, respectively. This callback
168 * is used to inform the application on such change.
170 int (*features_changed)(int vid, uint64_t features);
172 int (*new_connection)(int vid);
173 void (*destroy_connection)(int vid);
175 void *reserved[2]; /**< Reserved for future extension */
179 * Convert guest physical address to host virtual address
181 * This function is deprecated because unsafe.
182 * New rte_vhost_va_from_guest_pa() should be used instead to ensure
183 * guest physical ranges are fully and contiguously mapped into
184 * process virtual address space.
187 * the guest memory regions
189 * the guest physical address for querying
191 * the host virtual address on success, 0 on failure
194 static __rte_always_inline uint64_t
195 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
197 struct rte_vhost_mem_region *reg;
200 for (i = 0; i < mem->nregions; i++) {
201 reg = &mem->regions[i];
202 if (gpa >= reg->guest_phys_addr &&
203 gpa < reg->guest_phys_addr + reg->size) {
204 return gpa - reg->guest_phys_addr +
213 * Convert guest physical address to host virtual address safely
215 * This variant of rte_vhost_gpa_to_vva() takes care all the
216 * requested length is mapped and contiguous in process address
220 * the guest memory regions
222 * the guest physical address for querying
224 * the size of the requested area to map, updated with actual size mapped
226 * the host virtual address on success, 0 on failure
228 static __rte_always_inline uint64_t
229 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
230 uint64_t gpa, uint64_t *len)
232 struct rte_vhost_mem_region *r;
235 for (i = 0; i < mem->nregions; i++) {
236 r = &mem->regions[i];
237 if (gpa >= r->guest_phys_addr &&
238 gpa < r->guest_phys_addr + r->size) {
240 if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
241 *len = r->guest_phys_addr + r->size - gpa;
243 return gpa - r->guest_phys_addr +
252 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
255 * Log the memory write start with given address.
257 * This function only need be invoked when the live migration starts.
258 * Therefore, we won't need call it at all in the most of time. For
259 * making the performance impact be minimum, it's suggested to do a
260 * check before calling it:
262 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
263 * rte_vhost_log_write(vid, addr, len);
268 * the starting address for write
270 * the length to write
272 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
275 * Log the used ring update start at given offset.
277 * Same as rte_vhost_log_write, it's suggested to do a check before
280 * if (unlikely(RTE_VHOST_NEED_LOG(features)))
281 * rte_vhost_log_used_vring(vid, vring_idx, offset, len);
288 * the offset inside the used ring
290 * the length to write
292 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
293 uint64_t offset, uint64_t len);
295 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
298 * Register vhost driver. path could be different for multiple
301 int rte_vhost_driver_register(const char *path, uint64_t flags);
303 /* Unregister vhost driver. This is only meaningful to vhost user. */
304 int rte_vhost_driver_unregister(const char *path);
307 * Set the vdpa device id, enforce single connection per socket
310 * The vhost-user socket file path
314 * 0 on success, -1 on failure
318 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
321 * Unset the vdpa device id
324 * The vhost-user socket file path
326 * 0 on success, -1 on failure
330 rte_vhost_driver_detach_vdpa_device(const char *path);
336 * The vhost-user socket file path
338 * Device id, -1 on failure
342 rte_vhost_driver_get_vdpa_device_id(const char *path);
345 * Set the feature bits the vhost-user driver supports.
348 * The vhost-user socket file path
352 * 0 on success, -1 on failure
354 int rte_vhost_driver_set_features(const char *path, uint64_t features);
357 * Enable vhost-user driver features.
360 * - the param features should be a subset of the feature bits provided
361 * by rte_vhost_driver_set_features().
362 * - it must be invoked before vhost-user negotiation starts.
365 * The vhost-user socket file path
369 * 0 on success, -1 on failure
371 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
374 * Disable vhost-user driver features.
376 * The two notes at rte_vhost_driver_enable_features() also apply here.
379 * The vhost-user socket file path
381 * Features to disable
383 * 0 on success, -1 on failure
385 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
388 * Get the feature bits before feature negotiation.
391 * The vhost-user socket file path
393 * A pointer to store the queried feature bits
395 * 0 on success, -1 on failure
397 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
400 * Set the protocol feature bits before feature negotiation.
403 * The vhost-user socket file path
404 * @param protocol_features
405 * Supported protocol features
407 * 0 on success, -1 on failure
411 rte_vhost_driver_set_protocol_features(const char *path,
412 uint64_t protocol_features);
415 * Get the protocol feature bits before feature negotiation.
418 * The vhost-user socket file path
419 * @param protocol_features
420 * A pointer to store the queried protocol feature bits
422 * 0 on success, -1 on failure
426 rte_vhost_driver_get_protocol_features(const char *path,
427 uint64_t *protocol_features);
430 * Get the queue number bits before feature negotiation.
433 * The vhost-user socket file path
435 * A pointer to store the queried queue number bits
437 * 0 on success, -1 on failure
441 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
444 * Get the feature bits after negotiation
449 * A pointer to store the queried feature bits
451 * 0 on success, -1 on failure
453 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
455 /* Register callbacks. */
456 int rte_vhost_driver_callback_register(const char *path,
457 struct vhost_device_ops const * const ops);
461 * Start the vhost-user driver.
463 * This function triggers the vhost-user negotiation.
466 * The vhost-user socket file path
468 * 0 on success, -1 on failure
470 int rte_vhost_driver_start(const char *path);
473 * Get the MTU value of the device if set in QEMU.
476 * virtio-net device ID
478 * The variable to store the MTU value
482 * -EAGAIN: device not yet started
483 * -ENOTSUP: device does not support MTU feature
485 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
488 * Get the numa node from which the virtio net device's memory
495 * The numa node, -1 on failure
497 int rte_vhost_get_numa_node(int vid);
501 * Get the number of queues the device supports.
503 * Note this function is deprecated, as it returns a queue pair number,
504 * which is vhost specific. Instead, rte_vhost_get_vring_num should
511 * The number of queues, 0 on failure
514 uint32_t rte_vhost_get_queue_num(int vid);
517 * Get the number of vrings the device supports.
523 * The number of vrings, 0 on failure
525 uint16_t rte_vhost_get_vring_num(int vid);
528 * Get the virtio net device's ifname, which is the vhost-user socket
534 * The buffer to stored the queried ifname
539 * 0 on success, -1 on failure
541 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
544 * Get how many avail entries are left in the queue
552 * num of avail entries left
554 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
559 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
560 * be received from the physical port or from another virtual device. A packet
561 * count is returned to indicate the number of packets that were successfully
562 * added to the RX queue.
566 * virtio queue index in mq case
568 * array to contain packets to be enqueued
570 * packets num to be enqueued
572 * num of packets enqueued
574 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
575 struct rte_mbuf **pkts, uint16_t count);
578 * This function gets guest buffers from the virtio device TX virtqueue,
579 * construct host mbufs, copies guest buffer content to host mbufs and
580 * store them in pkts to be processed.
584 * virtio queue index in mq case
586 * mbuf_pool where host mbuf is allocated.
588 * array to contain packets to be dequeued
590 * packets num to be dequeued
592 * num of packets dequeued
594 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
595 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
598 * Get guest mem table: a list of memory regions.
600 * An rte_vhost_vhost_memory object will be allocated internally, to hold the
601 * guest memory regions. Application should free it at destroy_device()
607 * To store the returned mem regions
609 * 0 on success, -1 on failure
611 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
614 * Get guest vring info, including the vring address, vring size, etc.
621 * the structure to hold the requested vring info
623 * 0 on success, -1 on failure
625 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
626 struct rte_vhost_vring *vring);
629 * Notify the guest that used descriptors have been added to the vring. This
630 * function acts as a memory barrier.
637 * 0 on success, -1 on failure
639 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
642 * Get vhost RX queue avail count.
647 * virtio queue index in mq case
649 * num of desc available
651 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
654 * Get log base and log size of the vhost device
663 * 0 on success, -1 on failure
667 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
670 * Get last_avail/used_idx of the vhost virtqueue
676 * @param last_avail_idx
677 * vhost last_avail_idx to get
678 * @param last_used_idx
679 * vhost last_used_idx to get
681 * 0 on success, -1 on failure
685 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
686 uint16_t *last_avail_idx, uint16_t *last_used_idx);
689 * Set last_avail/used_idx of the vhost virtqueue
695 * @param last_avail_idx
696 * last_avail_idx to set
697 * @param last_used_idx
698 * last_used_idx to set
700 * 0 on success, -1 on failure
704 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
705 uint16_t last_avail_idx, uint16_t last_used_idx);
708 * Register external message handling callbacks
713 * virtio external callbacks to register
715 * additional context passed to the callbacks
717 * 0 on success, -1 on failure
721 rte_vhost_extern_callback_register(int vid,
722 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
725 * Get vdpa device id for vhost device.
734 rte_vhost_get_vdpa_device_id(int vid);
740 #endif /* _RTE_VHOST_H_ */