199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2017 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef _RTE_VHOST_H_ 699a2dd95SBruce Richardson #define _RTE_VHOST_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson /** 999a2dd95SBruce Richardson * @file 1099a2dd95SBruce Richardson * Interface to vhost-user 1199a2dd95SBruce Richardson */ 1299a2dd95SBruce Richardson 1399a2dd95SBruce Richardson #include <stdbool.h> 1499a2dd95SBruce Richardson #include <stdint.h> 1599a2dd95SBruce Richardson #include <sys/eventfd.h> 1699a2dd95SBruce Richardson 171094dd94SDavid Marchand #include <rte_compat.h> 1899a2dd95SBruce Richardson #include <rte_memory.h> 1999a2dd95SBruce Richardson #include <rte_mempool.h> 2099a2dd95SBruce Richardson 211d1126adSBruce Richardson #ifndef __cplusplus 2299a2dd95SBruce Richardson /* These are not C++-aware. */ 2399a2dd95SBruce Richardson #include <linux/vhost.h> 2499a2dd95SBruce Richardson #include <linux/virtio_ring.h> 2599a2dd95SBruce Richardson #include <linux/virtio_net.h> 261d1126adSBruce Richardson #endif 2799a2dd95SBruce Richardson 28719834a6SMattias Rönnblom #ifdef __cplusplus 29719834a6SMattias Rönnblom extern "C" { 30719834a6SMattias Rönnblom #endif 31719834a6SMattias Rönnblom 3299a2dd95SBruce Richardson #define RTE_VHOST_USER_CLIENT (1ULL << 0) 3399a2dd95SBruce Richardson #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1) 3499a2dd95SBruce Richardson #define RTE_VHOST_USER_RESERVED_1 (1ULL << 2) 3599a2dd95SBruce Richardson #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3) 3699a2dd95SBruce Richardson #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4) 3799a2dd95SBruce Richardson /* support mbuf with external buffer attached */ 3899a2dd95SBruce Richardson #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5) 3999a2dd95SBruce Richardson /* support only linear buffers (no chained mbufs) */ 4099a2dd95SBruce Richardson #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6) 4199a2dd95SBruce Richardson #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7) 42ca7036b4SDavid Marchand #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS (1ULL << 8) 43be75dc99SMaxime Coquelin #define RTE_VHOST_USER_NET_STATS_ENABLE (1ULL << 9) 44ff8989c4SDaniil Ushkov #define RTE_VHOST_USER_ASYNC_CONNECT (1ULL << 10) 4599a2dd95SBruce Richardson 4699a2dd95SBruce Richardson /* Features. */ 4799a2dd95SBruce Richardson #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE 4899a2dd95SBruce Richardson #define VIRTIO_NET_F_GUEST_ANNOUNCE 21 4999a2dd95SBruce Richardson #endif 5099a2dd95SBruce Richardson 5199a2dd95SBruce Richardson #ifndef VIRTIO_NET_F_MQ 5299a2dd95SBruce Richardson #define VIRTIO_NET_F_MQ 22 5399a2dd95SBruce Richardson #endif 5499a2dd95SBruce Richardson 5599a2dd95SBruce Richardson #ifndef VIRTIO_NET_F_MTU 5699a2dd95SBruce Richardson #define VIRTIO_NET_F_MTU 3 5799a2dd95SBruce Richardson #endif 5899a2dd95SBruce Richardson 5999a2dd95SBruce Richardson #ifndef VIRTIO_F_ANY_LAYOUT 6099a2dd95SBruce Richardson #define VIRTIO_F_ANY_LAYOUT 27 6199a2dd95SBruce Richardson #endif 6299a2dd95SBruce Richardson 6399a2dd95SBruce Richardson /** Protocol features. */ 6499a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_MQ 6599a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_MQ 0 6699a2dd95SBruce Richardson #endif 6799a2dd95SBruce Richardson 6899a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD 6999a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 7099a2dd95SBruce Richardson #endif 7199a2dd95SBruce Richardson 7299a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_RARP 7399a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_RARP 2 7499a2dd95SBruce Richardson #endif 7599a2dd95SBruce Richardson 7699a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK 7799a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3 7899a2dd95SBruce Richardson #endif 7999a2dd95SBruce Richardson 8099a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_NET_MTU 8199a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_NET_MTU 4 8299a2dd95SBruce Richardson #endif 8399a2dd95SBruce Richardson 84f1eb477eSNobuhiro Miki #ifndef VHOST_USER_PROTOCOL_F_BACKEND_REQ 85f1eb477eSNobuhiro Miki #define VHOST_USER_PROTOCOL_F_BACKEND_REQ 5 8699a2dd95SBruce Richardson #endif 8799a2dd95SBruce Richardson 8899a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 8999a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7 9099a2dd95SBruce Richardson #endif 9199a2dd95SBruce Richardson 9299a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT 9399a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8 9499a2dd95SBruce Richardson #endif 9599a2dd95SBruce Richardson 9699a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_CONFIG 9799a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_CONFIG 9 9899a2dd95SBruce Richardson #endif 9999a2dd95SBruce Richardson 100f1eb477eSNobuhiro Miki #ifndef VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD 101f1eb477eSNobuhiro Miki #define VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD 10 10299a2dd95SBruce Richardson #endif 10399a2dd95SBruce Richardson 10499a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 10599a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11 10699a2dd95SBruce Richardson #endif 10799a2dd95SBruce Richardson 10899a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 10999a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12 11099a2dd95SBruce Richardson #endif 11199a2dd95SBruce Richardson 11299a2dd95SBruce Richardson #ifndef VHOST_USER_PROTOCOL_F_STATUS 11399a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_F_STATUS 16 11499a2dd95SBruce Richardson #endif 11599a2dd95SBruce Richardson 11699a2dd95SBruce Richardson /** Indicate whether protocol features negotiation is supported. */ 11799a2dd95SBruce Richardson #ifndef VHOST_USER_F_PROTOCOL_FEATURES 11899a2dd95SBruce Richardson #define VHOST_USER_F_PROTOCOL_FEATURES 30 11999a2dd95SBruce Richardson #endif 12099a2dd95SBruce Richardson 12153d3f477SJiayu Hu #define RTE_MAX_VHOST_DEVICE 1024 12253d3f477SJiayu Hu 123486f65e6SAndy Pei #define RTE_VHOST_VDPA_DEVICE_TYPE_NET 0 124486f65e6SAndy Pei #define RTE_VHOST_VDPA_DEVICE_TYPE_BLK 1 125486f65e6SAndy Pei 12699a2dd95SBruce Richardson struct rte_vdpa_device; 12799a2dd95SBruce Richardson 12899a2dd95SBruce Richardson /** 12999a2dd95SBruce Richardson * Information relating to memory regions including offsets to 13099a2dd95SBruce Richardson * addresses in QEMUs memory file. 13199a2dd95SBruce Richardson */ 13299a2dd95SBruce Richardson struct rte_vhost_mem_region { 13399a2dd95SBruce Richardson uint64_t guest_phys_addr; 13499a2dd95SBruce Richardson uint64_t guest_user_addr; 13599a2dd95SBruce Richardson uint64_t host_user_addr; 13699a2dd95SBruce Richardson uint64_t size; 13799a2dd95SBruce Richardson void *mmap_addr; 13899a2dd95SBruce Richardson uint64_t mmap_size; 13999a2dd95SBruce Richardson int fd; 14099a2dd95SBruce Richardson }; 14199a2dd95SBruce Richardson 14299a2dd95SBruce Richardson /** 14399a2dd95SBruce Richardson * Memory structure includes region and mapping information. 14499a2dd95SBruce Richardson */ 14599a2dd95SBruce Richardson struct rte_vhost_memory { 14699a2dd95SBruce Richardson uint32_t nregions; 14799a2dd95SBruce Richardson struct rte_vhost_mem_region regions[]; 14899a2dd95SBruce Richardson }; 14999a2dd95SBruce Richardson 15099a2dd95SBruce Richardson struct rte_vhost_inflight_desc_split { 15199a2dd95SBruce Richardson uint8_t inflight; 15299a2dd95SBruce Richardson uint8_t padding[5]; 15399a2dd95SBruce Richardson uint16_t next; 15499a2dd95SBruce Richardson uint64_t counter; 15599a2dd95SBruce Richardson }; 15699a2dd95SBruce Richardson 15799a2dd95SBruce Richardson struct rte_vhost_inflight_info_split { 15899a2dd95SBruce Richardson uint64_t features; 15999a2dd95SBruce Richardson uint16_t version; 16099a2dd95SBruce Richardson uint16_t desc_num; 16199a2dd95SBruce Richardson uint16_t last_inflight_io; 16299a2dd95SBruce Richardson uint16_t used_idx; 163013b4c52SBruce Richardson struct rte_vhost_inflight_desc_split desc[]; 16499a2dd95SBruce Richardson }; 16599a2dd95SBruce Richardson 16699a2dd95SBruce Richardson struct rte_vhost_inflight_desc_packed { 16799a2dd95SBruce Richardson uint8_t inflight; 16899a2dd95SBruce Richardson uint8_t padding; 16999a2dd95SBruce Richardson uint16_t next; 17099a2dd95SBruce Richardson uint16_t last; 17199a2dd95SBruce Richardson uint16_t num; 17299a2dd95SBruce Richardson uint64_t counter; 17399a2dd95SBruce Richardson uint16_t id; 17499a2dd95SBruce Richardson uint16_t flags; 17599a2dd95SBruce Richardson uint32_t len; 17699a2dd95SBruce Richardson uint64_t addr; 17799a2dd95SBruce Richardson }; 17899a2dd95SBruce Richardson 17999a2dd95SBruce Richardson struct rte_vhost_inflight_info_packed { 18099a2dd95SBruce Richardson uint64_t features; 18199a2dd95SBruce Richardson uint16_t version; 18299a2dd95SBruce Richardson uint16_t desc_num; 18399a2dd95SBruce Richardson uint16_t free_head; 18499a2dd95SBruce Richardson uint16_t old_free_head; 18599a2dd95SBruce Richardson uint16_t used_idx; 18699a2dd95SBruce Richardson uint16_t old_used_idx; 18799a2dd95SBruce Richardson uint8_t used_wrap_counter; 18899a2dd95SBruce Richardson uint8_t old_used_wrap_counter; 18999a2dd95SBruce Richardson uint8_t padding[7]; 190013b4c52SBruce Richardson struct rte_vhost_inflight_desc_packed desc[]; 19199a2dd95SBruce Richardson }; 19299a2dd95SBruce Richardson 19399a2dd95SBruce Richardson struct rte_vhost_resubmit_desc { 19499a2dd95SBruce Richardson uint16_t index; 19599a2dd95SBruce Richardson uint64_t counter; 19699a2dd95SBruce Richardson }; 19799a2dd95SBruce Richardson 19899a2dd95SBruce Richardson struct rte_vhost_resubmit_info { 19999a2dd95SBruce Richardson struct rte_vhost_resubmit_desc *resubmit_list; 20099a2dd95SBruce Richardson uint16_t resubmit_num; 20199a2dd95SBruce Richardson }; 20299a2dd95SBruce Richardson 20399a2dd95SBruce Richardson struct rte_vhost_ring_inflight { 20499a2dd95SBruce Richardson union { 20599a2dd95SBruce Richardson struct rte_vhost_inflight_info_split *inflight_split; 20699a2dd95SBruce Richardson struct rte_vhost_inflight_info_packed *inflight_packed; 20799a2dd95SBruce Richardson }; 20899a2dd95SBruce Richardson 20999a2dd95SBruce Richardson struct rte_vhost_resubmit_info *resubmit_inflight; 21099a2dd95SBruce Richardson }; 21199a2dd95SBruce Richardson 21299a2dd95SBruce Richardson struct rte_vhost_vring { 21399a2dd95SBruce Richardson union { 21499a2dd95SBruce Richardson struct vring_desc *desc; 21599a2dd95SBruce Richardson struct vring_packed_desc *desc_packed; 21699a2dd95SBruce Richardson }; 21799a2dd95SBruce Richardson union { 21899a2dd95SBruce Richardson struct vring_avail *avail; 21999a2dd95SBruce Richardson struct vring_packed_desc_event *driver_event; 22099a2dd95SBruce Richardson }; 22199a2dd95SBruce Richardson union { 22299a2dd95SBruce Richardson struct vring_used *used; 22399a2dd95SBruce Richardson struct vring_packed_desc_event *device_event; 22499a2dd95SBruce Richardson }; 22599a2dd95SBruce Richardson uint64_t log_guest_addr; 22699a2dd95SBruce Richardson 22799a2dd95SBruce Richardson /** Deprecated, use rte_vhost_vring_call() instead. */ 22899a2dd95SBruce Richardson int callfd; 22999a2dd95SBruce Richardson 23099a2dd95SBruce Richardson int kickfd; 23199a2dd95SBruce Richardson uint16_t size; 23299a2dd95SBruce Richardson }; 23399a2dd95SBruce Richardson 23499a2dd95SBruce Richardson /** 23599a2dd95SBruce Richardson * Possible results of the vhost user message handling callbacks 23699a2dd95SBruce Richardson */ 23799a2dd95SBruce Richardson enum rte_vhost_msg_result { 23899a2dd95SBruce Richardson /* Message handling failed */ 23999a2dd95SBruce Richardson RTE_VHOST_MSG_RESULT_ERR = -1, 24099a2dd95SBruce Richardson /* Message handling successful */ 24199a2dd95SBruce Richardson RTE_VHOST_MSG_RESULT_OK = 0, 24299a2dd95SBruce Richardson /* Message handling successful and reply prepared */ 24399a2dd95SBruce Richardson RTE_VHOST_MSG_RESULT_REPLY = 1, 24499a2dd95SBruce Richardson /* Message not handled */ 24599a2dd95SBruce Richardson RTE_VHOST_MSG_RESULT_NOT_HANDLED, 24699a2dd95SBruce Richardson }; 24799a2dd95SBruce Richardson 24899a2dd95SBruce Richardson /** 24999a2dd95SBruce Richardson * Function prototype for the vhost backend to handle specific vhost user 25099a2dd95SBruce Richardson * messages. 25199a2dd95SBruce Richardson * 25299a2dd95SBruce Richardson * @param vid 25399a2dd95SBruce Richardson * vhost device id 25499a2dd95SBruce Richardson * @param msg 25599a2dd95SBruce Richardson * Message pointer. 25699a2dd95SBruce Richardson * @return 25799a2dd95SBruce Richardson * RTE_VHOST_MSG_RESULT_OK on success, 25899a2dd95SBruce Richardson * RTE_VHOST_MSG_RESULT_REPLY on success with reply, 25999a2dd95SBruce Richardson * RTE_VHOST_MSG_RESULT_ERR on failure, 26099a2dd95SBruce Richardson * RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled. 26199a2dd95SBruce Richardson */ 26299a2dd95SBruce Richardson typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg); 26399a2dd95SBruce Richardson 26499a2dd95SBruce Richardson /** 26599a2dd95SBruce Richardson * Optional vhost user message handlers. 26699a2dd95SBruce Richardson */ 26799a2dd95SBruce Richardson struct rte_vhost_user_extern_ops { 26871998eb6SNobuhiro Miki /* Called prior to the frontend message handling. */ 26999a2dd95SBruce Richardson rte_vhost_msg_handle pre_msg_handle; 27071998eb6SNobuhiro Miki /* Called after the frontend message handling. */ 27199a2dd95SBruce Richardson rte_vhost_msg_handle post_msg_handle; 27299a2dd95SBruce Richardson }; 27399a2dd95SBruce Richardson 27499a2dd95SBruce Richardson /** 27599a2dd95SBruce Richardson * Device and vring operations. 27699a2dd95SBruce Richardson */ 277ab4bb424SMaxime Coquelin struct rte_vhost_device_ops { 27899a2dd95SBruce Richardson int (*new_device)(int vid); /**< Add device. */ 27999a2dd95SBruce Richardson void (*destroy_device)(int vid); /**< Remove device. */ 28099a2dd95SBruce Richardson 28199a2dd95SBruce Richardson int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */ 28299a2dd95SBruce Richardson 28399a2dd95SBruce Richardson /** 28499a2dd95SBruce Richardson * Features could be changed after the feature negotiation. 28599a2dd95SBruce Richardson * For example, VHOST_F_LOG_ALL will be set/cleared at the 28699a2dd95SBruce Richardson * start/end of live migration, respectively. This callback 28799a2dd95SBruce Richardson * is used to inform the application on such change. 28899a2dd95SBruce Richardson */ 28999a2dd95SBruce Richardson int (*features_changed)(int vid, uint64_t features); 29099a2dd95SBruce Richardson 29199a2dd95SBruce Richardson int (*new_connection)(int vid); 29299a2dd95SBruce Richardson void (*destroy_connection)(int vid); 29399a2dd95SBruce Richardson 29499a2dd95SBruce Richardson /** 29599a2dd95SBruce Richardson * This callback gets called each time a guest gets notified 29699a2dd95SBruce Richardson * about waiting packets. This is the interrupt handling through 29799a2dd95SBruce Richardson * the eventfd_write(callfd), which can be used for counting these 29899a2dd95SBruce Richardson * "slow" syscalls. 29999a2dd95SBruce Richardson */ 30099a2dd95SBruce Richardson void (*guest_notified)(int vid); 30199a2dd95SBruce Richardson 302d761d455SEelco Chaudron /** 303d761d455SEelco Chaudron * If this callback is registered, notification to the guest can 304d761d455SEelco Chaudron * be handled by the front-end calling rte_vhost_notify_guest(). 305d761d455SEelco Chaudron * If it's not handled, 'false' should be returned. This can be used 306d761d455SEelco Chaudron * to remove the "slow" eventfd_write() syscall from the datapath. 307d761d455SEelco Chaudron */ 308d761d455SEelco Chaudron bool (*guest_notify)(int vid, uint16_t queue_id); 30999a2dd95SBruce Richardson }; 31099a2dd95SBruce Richardson 31199a2dd95SBruce Richardson /** 31234fd4373SMiao Li * Power monitor condition. 31334fd4373SMiao Li */ 31434fd4373SMiao Li struct rte_vhost_power_monitor_cond { 31534fd4373SMiao Li /**< Address to monitor for changes */ 31634fd4373SMiao Li volatile void *addr; 31734fd4373SMiao Li /**< If the `mask` is non-zero, location pointed 31834fd4373SMiao Li * to by `addr` will be read and masked, then 31934fd4373SMiao Li * compared with this value. 32034fd4373SMiao Li */ 32134fd4373SMiao Li uint64_t val; 32234fd4373SMiao Li /**< 64-bit mask to extract value read from `addr` */ 32334fd4373SMiao Li uint64_t mask; 32434fd4373SMiao Li /**< Data size (in bytes) that will be read from the 32534fd4373SMiao Li * monitored memory location (`addr`). 32634fd4373SMiao Li */ 32734fd4373SMiao Li uint8_t size; 32834fd4373SMiao Li /**< If 1, and masked value that read from 'addr' equals 32934fd4373SMiao Li * 'val', the driver should skip core sleep. If 0, and 33034fd4373SMiao Li * masked value that read from 'addr' does not equal 'val', 33134fd4373SMiao Li * the driver should skip core sleep. 33234fd4373SMiao Li */ 33334fd4373SMiao Li uint8_t match; 33434fd4373SMiao Li }; 33534fd4373SMiao Li 336be75dc99SMaxime Coquelin /** Maximum name length for the statistics counters */ 337be75dc99SMaxime Coquelin #define RTE_VHOST_STATS_NAME_SIZE 64 338be75dc99SMaxime Coquelin 339be75dc99SMaxime Coquelin /** 340be75dc99SMaxime Coquelin * Vhost virtqueue statistics structure 341be75dc99SMaxime Coquelin * 342be75dc99SMaxime Coquelin * This structure is used by rte_vhost_vring_stats_get() to provide 343be75dc99SMaxime Coquelin * virtqueue statistics to the calling application. 344be75dc99SMaxime Coquelin * It maps a name ID, corresponding to an index in the array returned 345be75dc99SMaxime Coquelin * by rte_vhost_vring_stats_get_names(), to a statistic value. 346be75dc99SMaxime Coquelin */ 347be75dc99SMaxime Coquelin struct rte_vhost_stat { 348be75dc99SMaxime Coquelin uint64_t id; /**< The index in xstats name array. */ 349be75dc99SMaxime Coquelin uint64_t value; /**< The statistic counter value. */ 350be75dc99SMaxime Coquelin }; 351be75dc99SMaxime Coquelin 352be75dc99SMaxime Coquelin /** 353be75dc99SMaxime Coquelin * Vhost virtqueue statistic name element 354be75dc99SMaxime Coquelin * 355be75dc99SMaxime Coquelin * This structure is used by rte_vhost_vring_stats_get_names() to 356be75dc99SMaxime Coquelin * provide virtqueue statistics names to the calling application. 357be75dc99SMaxime Coquelin */ 358be75dc99SMaxime Coquelin struct rte_vhost_stat_name { 359be75dc99SMaxime Coquelin char name[RTE_VHOST_STATS_NAME_SIZE]; /**< The statistic name. */ 360be75dc99SMaxime Coquelin }; 361be75dc99SMaxime Coquelin 36234fd4373SMiao Li /** 36399a2dd95SBruce Richardson * Convert guest physical address to host virtual address 36499a2dd95SBruce Richardson * 36599a2dd95SBruce Richardson * @param mem 36699a2dd95SBruce Richardson * the guest memory regions 36799a2dd95SBruce Richardson * @param gpa 36899a2dd95SBruce Richardson * the guest physical address for querying 36999a2dd95SBruce Richardson * @param len 37099a2dd95SBruce Richardson * the size of the requested area to map, updated with actual size mapped 37199a2dd95SBruce Richardson * @return 37299a2dd95SBruce Richardson * the host virtual address on success, 0 on failure 37399a2dd95SBruce Richardson */ 37499a2dd95SBruce Richardson static __rte_always_inline uint64_t 37599a2dd95SBruce Richardson rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem, 37699a2dd95SBruce Richardson uint64_t gpa, uint64_t *len) 37799a2dd95SBruce Richardson { 37899a2dd95SBruce Richardson struct rte_vhost_mem_region *r; 37999a2dd95SBruce Richardson uint32_t i; 38099a2dd95SBruce Richardson 38199a2dd95SBruce Richardson for (i = 0; i < mem->nregions; i++) { 38299a2dd95SBruce Richardson r = &mem->regions[i]; 38399a2dd95SBruce Richardson if (gpa >= r->guest_phys_addr && 38499a2dd95SBruce Richardson gpa < r->guest_phys_addr + r->size) { 38599a2dd95SBruce Richardson 38699a2dd95SBruce Richardson if (unlikely(*len > r->guest_phys_addr + r->size - gpa)) 38799a2dd95SBruce Richardson *len = r->guest_phys_addr + r->size - gpa; 38899a2dd95SBruce Richardson 38999a2dd95SBruce Richardson return gpa - r->guest_phys_addr + 39099a2dd95SBruce Richardson r->host_user_addr; 39199a2dd95SBruce Richardson } 39299a2dd95SBruce Richardson } 39399a2dd95SBruce Richardson *len = 0; 39499a2dd95SBruce Richardson 39599a2dd95SBruce Richardson return 0; 39699a2dd95SBruce Richardson } 39799a2dd95SBruce Richardson 39899a2dd95SBruce Richardson #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL)) 39999a2dd95SBruce Richardson 40099a2dd95SBruce Richardson /** 40199a2dd95SBruce Richardson * Log the memory write start with given address. 40299a2dd95SBruce Richardson * 40399a2dd95SBruce Richardson * This function only need be invoked when the live migration starts. 40499a2dd95SBruce Richardson * Therefore, we won't need call it at all in the most of time. For 40599a2dd95SBruce Richardson * making the performance impact be minimum, it's suggested to do a 40699a2dd95SBruce Richardson * check before calling it: 40799a2dd95SBruce Richardson * 40899a2dd95SBruce Richardson * if (unlikely(RTE_VHOST_NEED_LOG(features))) 40999a2dd95SBruce Richardson * rte_vhost_log_write(vid, addr, len); 41099a2dd95SBruce Richardson * 41199a2dd95SBruce Richardson * @param vid 41299a2dd95SBruce Richardson * vhost device ID 41399a2dd95SBruce Richardson * @param addr 41499a2dd95SBruce Richardson * the starting address for write (in guest physical address space) 41599a2dd95SBruce Richardson * @param len 41699a2dd95SBruce Richardson * the length to write 41799a2dd95SBruce Richardson */ 41899a2dd95SBruce Richardson void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len); 41999a2dd95SBruce Richardson 42099a2dd95SBruce Richardson /** 42199a2dd95SBruce Richardson * Log the used ring update start at given offset. 42299a2dd95SBruce Richardson * 42399a2dd95SBruce Richardson * Same as rte_vhost_log_write, it's suggested to do a check before 42499a2dd95SBruce Richardson * calling it: 42599a2dd95SBruce Richardson * 42699a2dd95SBruce Richardson * if (unlikely(RTE_VHOST_NEED_LOG(features))) 42799a2dd95SBruce Richardson * rte_vhost_log_used_vring(vid, vring_idx, offset, len); 42899a2dd95SBruce Richardson * 42999a2dd95SBruce Richardson * @param vid 43099a2dd95SBruce Richardson * vhost device ID 43199a2dd95SBruce Richardson * @param vring_idx 43299a2dd95SBruce Richardson * the vring index 43399a2dd95SBruce Richardson * @param offset 43499a2dd95SBruce Richardson * the offset inside the used ring 43599a2dd95SBruce Richardson * @param len 43699a2dd95SBruce Richardson * the length to write 43799a2dd95SBruce Richardson */ 43899a2dd95SBruce Richardson void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, 43999a2dd95SBruce Richardson uint64_t offset, uint64_t len); 44099a2dd95SBruce Richardson 44199a2dd95SBruce Richardson int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable); 44299a2dd95SBruce Richardson 44399a2dd95SBruce Richardson /** 444d761d455SEelco Chaudron * @warning 445d761d455SEelco Chaudron * @b EXPERIMENTAL: this API may change, or be removed, without prior notice. 446d761d455SEelco Chaudron * 447d761d455SEelco Chaudron * Inject the offloaded interrupt into the vhost device's queue. 448d761d455SEelco Chaudron * @see guest_notify vhost device operation 449d761d455SEelco Chaudron * 450d761d455SEelco Chaudron * @param vid 451d761d455SEelco Chaudron * vhost device ID 452d761d455SEelco Chaudron * @param queue_id 453d761d455SEelco Chaudron * virtio queue index 454d761d455SEelco Chaudron */ 455d761d455SEelco Chaudron __rte_experimental 456d761d455SEelco Chaudron void rte_vhost_notify_guest(int vid, uint16_t queue_id); 457d761d455SEelco Chaudron 458d761d455SEelco Chaudron /** 45999a2dd95SBruce Richardson * Register vhost driver. path could be different for multiple 46099a2dd95SBruce Richardson * instance support. 46199a2dd95SBruce Richardson */ 46299a2dd95SBruce Richardson int rte_vhost_driver_register(const char *path, uint64_t flags); 46399a2dd95SBruce Richardson 46499a2dd95SBruce Richardson /* Unregister vhost driver. This is only meaningful to vhost user. */ 46599a2dd95SBruce Richardson int rte_vhost_driver_unregister(const char *path); 46699a2dd95SBruce Richardson 46799a2dd95SBruce Richardson /** 46899a2dd95SBruce Richardson * Set the vdpa device id, enforce single connection per socket 46999a2dd95SBruce Richardson * 47099a2dd95SBruce Richardson * @param path 47199a2dd95SBruce Richardson * The vhost-user socket file path 47299a2dd95SBruce Richardson * @param dev 47399a2dd95SBruce Richardson * vDPA device pointer 47499a2dd95SBruce Richardson * @return 47599a2dd95SBruce Richardson * 0 on success, -1 on failure 47699a2dd95SBruce Richardson */ 47799a2dd95SBruce Richardson int 47899a2dd95SBruce Richardson rte_vhost_driver_attach_vdpa_device(const char *path, 47999a2dd95SBruce Richardson struct rte_vdpa_device *dev); 48099a2dd95SBruce Richardson 48199a2dd95SBruce Richardson /** 48299a2dd95SBruce Richardson * Unset the vdpa device id 48399a2dd95SBruce Richardson * 48499a2dd95SBruce Richardson * @param path 48599a2dd95SBruce Richardson * The vhost-user socket file path 48699a2dd95SBruce Richardson * @return 48799a2dd95SBruce Richardson * 0 on success, -1 on failure 48899a2dd95SBruce Richardson */ 48999a2dd95SBruce Richardson int 49099a2dd95SBruce Richardson rte_vhost_driver_detach_vdpa_device(const char *path); 49199a2dd95SBruce Richardson 49299a2dd95SBruce Richardson /** 49399a2dd95SBruce Richardson * Get the device id 49499a2dd95SBruce Richardson * 49599a2dd95SBruce Richardson * @param path 49699a2dd95SBruce Richardson * The vhost-user socket file path 49799a2dd95SBruce Richardson * @return 49899a2dd95SBruce Richardson * vDPA device pointer, NULL on failure 49999a2dd95SBruce Richardson */ 50099a2dd95SBruce Richardson struct rte_vdpa_device * 50199a2dd95SBruce Richardson rte_vhost_driver_get_vdpa_device(const char *path); 50299a2dd95SBruce Richardson 50399a2dd95SBruce Richardson /** 504486f65e6SAndy Pei * Get the device type of the vdpa device. 505486f65e6SAndy Pei * 506486f65e6SAndy Pei * @param path 507486f65e6SAndy Pei * The vhost-user socket file path 508486f65e6SAndy Pei * @param type 509486f65e6SAndy Pei * the device type of the vdpa device 510486f65e6SAndy Pei * @return 511486f65e6SAndy Pei * 0 on success, -1 on failure 512486f65e6SAndy Pei */ 513486f65e6SAndy Pei int 514486f65e6SAndy Pei rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type); 515486f65e6SAndy Pei 516486f65e6SAndy Pei /** 51799a2dd95SBruce Richardson * Set the feature bits the vhost-user driver supports. 51899a2dd95SBruce Richardson * 51999a2dd95SBruce Richardson * @param path 52099a2dd95SBruce Richardson * The vhost-user socket file path 52199a2dd95SBruce Richardson * @param features 52299a2dd95SBruce Richardson * Supported features 52399a2dd95SBruce Richardson * @return 52499a2dd95SBruce Richardson * 0 on success, -1 on failure 52599a2dd95SBruce Richardson */ 52699a2dd95SBruce Richardson int rte_vhost_driver_set_features(const char *path, uint64_t features); 52799a2dd95SBruce Richardson 52899a2dd95SBruce Richardson /** 52999a2dd95SBruce Richardson * Enable vhost-user driver features. 53099a2dd95SBruce Richardson * 53199a2dd95SBruce Richardson * Note that 53299a2dd95SBruce Richardson * - the param features should be a subset of the feature bits provided 53399a2dd95SBruce Richardson * by rte_vhost_driver_set_features(). 53499a2dd95SBruce Richardson * - it must be invoked before vhost-user negotiation starts. 53599a2dd95SBruce Richardson * 53699a2dd95SBruce Richardson * @param path 53799a2dd95SBruce Richardson * The vhost-user socket file path 53899a2dd95SBruce Richardson * @param features 53999a2dd95SBruce Richardson * Features to enable 54099a2dd95SBruce Richardson * @return 54199a2dd95SBruce Richardson * 0 on success, -1 on failure 54299a2dd95SBruce Richardson */ 54399a2dd95SBruce Richardson int rte_vhost_driver_enable_features(const char *path, uint64_t features); 54499a2dd95SBruce Richardson 54599a2dd95SBruce Richardson /** 54699a2dd95SBruce Richardson * Disable vhost-user driver features. 54799a2dd95SBruce Richardson * 54899a2dd95SBruce Richardson * The two notes at rte_vhost_driver_enable_features() also apply here. 54999a2dd95SBruce Richardson * 55099a2dd95SBruce Richardson * @param path 55199a2dd95SBruce Richardson * The vhost-user socket file path 55299a2dd95SBruce Richardson * @param features 55399a2dd95SBruce Richardson * Features to disable 55499a2dd95SBruce Richardson * @return 55599a2dd95SBruce Richardson * 0 on success, -1 on failure 55699a2dd95SBruce Richardson */ 55799a2dd95SBruce Richardson int rte_vhost_driver_disable_features(const char *path, uint64_t features); 55899a2dd95SBruce Richardson 55999a2dd95SBruce Richardson /** 56099a2dd95SBruce Richardson * Get the feature bits before feature negotiation. 56199a2dd95SBruce Richardson * 56299a2dd95SBruce Richardson * @param path 56399a2dd95SBruce Richardson * The vhost-user socket file path 56499a2dd95SBruce Richardson * @param features 56599a2dd95SBruce Richardson * A pointer to store the queried feature bits 56699a2dd95SBruce Richardson * @return 56799a2dd95SBruce Richardson * 0 on success, -1 on failure 56899a2dd95SBruce Richardson */ 56999a2dd95SBruce Richardson int rte_vhost_driver_get_features(const char *path, uint64_t *features); 57099a2dd95SBruce Richardson 57199a2dd95SBruce Richardson /** 57299a2dd95SBruce Richardson * Set the protocol feature bits before feature negotiation. 57399a2dd95SBruce Richardson * 57499a2dd95SBruce Richardson * @param path 57599a2dd95SBruce Richardson * The vhost-user socket file path 57699a2dd95SBruce Richardson * @param protocol_features 57799a2dd95SBruce Richardson * Supported protocol features 57899a2dd95SBruce Richardson * @return 57999a2dd95SBruce Richardson * 0 on success, -1 on failure 58099a2dd95SBruce Richardson */ 58199a2dd95SBruce Richardson int 58299a2dd95SBruce Richardson rte_vhost_driver_set_protocol_features(const char *path, 58399a2dd95SBruce Richardson uint64_t protocol_features); 58499a2dd95SBruce Richardson 58599a2dd95SBruce Richardson /** 58699a2dd95SBruce Richardson * Get the protocol feature bits before feature negotiation. 58799a2dd95SBruce Richardson * 58899a2dd95SBruce Richardson * @param path 58999a2dd95SBruce Richardson * The vhost-user socket file path 59099a2dd95SBruce Richardson * @param protocol_features 59199a2dd95SBruce Richardson * A pointer to store the queried protocol feature bits 59299a2dd95SBruce Richardson * @return 59399a2dd95SBruce Richardson * 0 on success, -1 on failure 59499a2dd95SBruce Richardson */ 59599a2dd95SBruce Richardson int 59699a2dd95SBruce Richardson rte_vhost_driver_get_protocol_features(const char *path, 59799a2dd95SBruce Richardson uint64_t *protocol_features); 59899a2dd95SBruce Richardson 59999a2dd95SBruce Richardson /** 60099a2dd95SBruce Richardson * Get the queue number bits before feature negotiation. 60199a2dd95SBruce Richardson * 60299a2dd95SBruce Richardson * @param path 60399a2dd95SBruce Richardson * The vhost-user socket file path 60499a2dd95SBruce Richardson * @param queue_num 60599a2dd95SBruce Richardson * A pointer to store the queried queue number bits 60699a2dd95SBruce Richardson * @return 60799a2dd95SBruce Richardson * 0 on success, -1 on failure 60899a2dd95SBruce Richardson */ 60999a2dd95SBruce Richardson int 61099a2dd95SBruce Richardson rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num); 61199a2dd95SBruce Richardson 61299a2dd95SBruce Richardson /** 6134aa1f88aSMaxime Coquelin * Set the maximum number of queue pairs supported by the device. 614*e1808999SMaxime Coquelin * The value set is ignored for Vhost-user backends. It is only taken into 615*e1808999SMaxime Coquelin * account with VDUSE backends. 6164aa1f88aSMaxime Coquelin * 6174aa1f88aSMaxime Coquelin * @param path 6184aa1f88aSMaxime Coquelin * The vhost-user socket file path 6194aa1f88aSMaxime Coquelin * @param max_queue_pairs 6204aa1f88aSMaxime Coquelin * The maximum number of queue pairs 6214aa1f88aSMaxime Coquelin * @return 6224aa1f88aSMaxime Coquelin * 0 on success, -1 on failure 6234aa1f88aSMaxime Coquelin */ 6244aa1f88aSMaxime Coquelin int 6254aa1f88aSMaxime Coquelin rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs); 6264aa1f88aSMaxime Coquelin 6274aa1f88aSMaxime Coquelin /** 62899a2dd95SBruce Richardson * Get the feature bits after negotiation 62999a2dd95SBruce Richardson * 63099a2dd95SBruce Richardson * @param vid 63199a2dd95SBruce Richardson * Vhost device ID 63299a2dd95SBruce Richardson * @param features 63399a2dd95SBruce Richardson * A pointer to store the queried feature bits 63499a2dd95SBruce Richardson * @return 63599a2dd95SBruce Richardson * 0 on success, -1 on failure 63699a2dd95SBruce Richardson */ 63799a2dd95SBruce Richardson int rte_vhost_get_negotiated_features(int vid, uint64_t *features); 63899a2dd95SBruce Richardson 63999a2dd95SBruce Richardson /** 64099a2dd95SBruce Richardson * Get the protocol feature bits after negotiation 64199a2dd95SBruce Richardson * 64299a2dd95SBruce Richardson * @param vid 64399a2dd95SBruce Richardson * Vhost device ID 64499a2dd95SBruce Richardson * @param protocol_features 64599a2dd95SBruce Richardson * A pointer to store the queried protocol feature bits 64699a2dd95SBruce Richardson * @return 64799a2dd95SBruce Richardson * 0 on success, -1 on failure 64899a2dd95SBruce Richardson */ 64999a2dd95SBruce Richardson int 65099a2dd95SBruce Richardson rte_vhost_get_negotiated_protocol_features(int vid, 65199a2dd95SBruce Richardson uint64_t *protocol_features); 65299a2dd95SBruce Richardson 65399a2dd95SBruce Richardson /* Register callbacks. */ 65499a2dd95SBruce Richardson int rte_vhost_driver_callback_register(const char *path, 655ab4bb424SMaxime Coquelin struct rte_vhost_device_ops const * const ops); 65699a2dd95SBruce Richardson 65799a2dd95SBruce Richardson /** 65899a2dd95SBruce Richardson * 65999a2dd95SBruce Richardson * Start the vhost-user driver. 66099a2dd95SBruce Richardson * 66199a2dd95SBruce Richardson * This function triggers the vhost-user negotiation. 66299a2dd95SBruce Richardson * 66399a2dd95SBruce Richardson * @param path 66499a2dd95SBruce Richardson * The vhost-user socket file path 66599a2dd95SBruce Richardson * @return 66699a2dd95SBruce Richardson * 0 on success, -1 on failure 66799a2dd95SBruce Richardson */ 66899a2dd95SBruce Richardson int rte_vhost_driver_start(const char *path); 66999a2dd95SBruce Richardson 67099a2dd95SBruce Richardson /** 67199a2dd95SBruce Richardson * Get the MTU value of the device if set in QEMU. 67299a2dd95SBruce Richardson * 67399a2dd95SBruce Richardson * @param vid 67499a2dd95SBruce Richardson * virtio-net device ID 67599a2dd95SBruce Richardson * @param mtu 67699a2dd95SBruce Richardson * The variable to store the MTU value 67799a2dd95SBruce Richardson * 67899a2dd95SBruce Richardson * @return 67999a2dd95SBruce Richardson * 0: success 68099a2dd95SBruce Richardson * -EAGAIN: device not yet started 68199a2dd95SBruce Richardson * -ENOTSUP: device does not support MTU feature 68299a2dd95SBruce Richardson */ 68399a2dd95SBruce Richardson int rte_vhost_get_mtu(int vid, uint16_t *mtu); 68499a2dd95SBruce Richardson 68599a2dd95SBruce Richardson /** 68699a2dd95SBruce Richardson * Get the numa node from which the virtio net device's memory 68799a2dd95SBruce Richardson * is allocated. 68899a2dd95SBruce Richardson * 68999a2dd95SBruce Richardson * @param vid 69099a2dd95SBruce Richardson * vhost device ID 69199a2dd95SBruce Richardson * 69299a2dd95SBruce Richardson * @return 69399a2dd95SBruce Richardson * The numa node, -1 on failure 69499a2dd95SBruce Richardson */ 69599a2dd95SBruce Richardson int rte_vhost_get_numa_node(int vid); 69699a2dd95SBruce Richardson 69799a2dd95SBruce Richardson /** 69899a2dd95SBruce Richardson * Get the number of vrings the device supports. 69999a2dd95SBruce Richardson * 70099a2dd95SBruce Richardson * @param vid 70199a2dd95SBruce Richardson * vhost device ID 70299a2dd95SBruce Richardson * 70399a2dd95SBruce Richardson * @return 70499a2dd95SBruce Richardson * The number of vrings, 0 on failure 70599a2dd95SBruce Richardson */ 70699a2dd95SBruce Richardson uint16_t rte_vhost_get_vring_num(int vid); 70799a2dd95SBruce Richardson 70899a2dd95SBruce Richardson /** 70999a2dd95SBruce Richardson * Get the virtio net device's ifname, which is the vhost-user socket 71099a2dd95SBruce Richardson * file path. 71199a2dd95SBruce Richardson * 71299a2dd95SBruce Richardson * @param vid 71399a2dd95SBruce Richardson * vhost device ID 71499a2dd95SBruce Richardson * @param buf 71599a2dd95SBruce Richardson * The buffer to stored the queried ifname 71699a2dd95SBruce Richardson * @param len 71799a2dd95SBruce Richardson * The length of buf 71899a2dd95SBruce Richardson * 71999a2dd95SBruce Richardson * @return 72099a2dd95SBruce Richardson * 0 on success, -1 on failure 72199a2dd95SBruce Richardson */ 72299a2dd95SBruce Richardson int rte_vhost_get_ifname(int vid, char *buf, size_t len); 72399a2dd95SBruce Richardson 72499a2dd95SBruce Richardson /** 72599a2dd95SBruce Richardson * Get how many avail entries are left in the queue 72699a2dd95SBruce Richardson * 72799a2dd95SBruce Richardson * @param vid 72899a2dd95SBruce Richardson * vhost device ID 72999a2dd95SBruce Richardson * @param queue_id 73099a2dd95SBruce Richardson * virtio queue index 73199a2dd95SBruce Richardson * 73299a2dd95SBruce Richardson * @return 73399a2dd95SBruce Richardson * num of avail entries left 73499a2dd95SBruce Richardson */ 73599a2dd95SBruce Richardson uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id); 73699a2dd95SBruce Richardson 73799a2dd95SBruce Richardson struct rte_mbuf; 73899a2dd95SBruce Richardson struct rte_mempool; 73999a2dd95SBruce Richardson /** 74099a2dd95SBruce Richardson * This function adds buffers to the virtio devices RX virtqueue. Buffers can 74199a2dd95SBruce Richardson * be received from the physical port or from another virtual device. A packet 74299a2dd95SBruce Richardson * count is returned to indicate the number of packets that were successfully 74399a2dd95SBruce Richardson * added to the RX queue. 74499a2dd95SBruce Richardson * @param vid 74599a2dd95SBruce Richardson * vhost device ID 74699a2dd95SBruce Richardson * @param queue_id 74799a2dd95SBruce Richardson * virtio queue index in mq case 74899a2dd95SBruce Richardson * @param pkts 74999a2dd95SBruce Richardson * array to contain packets to be enqueued 75099a2dd95SBruce Richardson * @param count 75199a2dd95SBruce Richardson * packets num to be enqueued 75299a2dd95SBruce Richardson * @return 75399a2dd95SBruce Richardson * num of packets enqueued 75499a2dd95SBruce Richardson */ 75599a2dd95SBruce Richardson uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id, 75699a2dd95SBruce Richardson struct rte_mbuf **pkts, uint16_t count); 75799a2dd95SBruce Richardson 75899a2dd95SBruce Richardson /** 75999a2dd95SBruce Richardson * This function gets guest buffers from the virtio device TX virtqueue, 76099a2dd95SBruce Richardson * construct host mbufs, copies guest buffer content to host mbufs and 76199a2dd95SBruce Richardson * store them in pkts to be processed. 76299a2dd95SBruce Richardson * @param vid 76399a2dd95SBruce Richardson * vhost device ID 76499a2dd95SBruce Richardson * @param queue_id 76599a2dd95SBruce Richardson * virtio queue index in mq case 76699a2dd95SBruce Richardson * @param mbuf_pool 76799a2dd95SBruce Richardson * mbuf_pool where host mbuf is allocated. 76899a2dd95SBruce Richardson * @param pkts 76999a2dd95SBruce Richardson * array to contain packets to be dequeued 77099a2dd95SBruce Richardson * @param count 77199a2dd95SBruce Richardson * packets num to be dequeued 77299a2dd95SBruce Richardson * @return 77399a2dd95SBruce Richardson * num of packets dequeued 77499a2dd95SBruce Richardson */ 77599a2dd95SBruce Richardson uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id, 77699a2dd95SBruce Richardson struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count); 77799a2dd95SBruce Richardson 77899a2dd95SBruce Richardson /** 77999a2dd95SBruce Richardson * Get guest mem table: a list of memory regions. 78099a2dd95SBruce Richardson * 78199a2dd95SBruce Richardson * An rte_vhost_vhost_memory object will be allocated internally, to hold the 78299a2dd95SBruce Richardson * guest memory regions. Application should free it at destroy_device() 78399a2dd95SBruce Richardson * callback. 78499a2dd95SBruce Richardson * 78599a2dd95SBruce Richardson * @param vid 78699a2dd95SBruce Richardson * vhost device ID 78799a2dd95SBruce Richardson * @param mem 78899a2dd95SBruce Richardson * To store the returned mem regions 78999a2dd95SBruce Richardson * @return 79099a2dd95SBruce Richardson * 0 on success, -1 on failure 79199a2dd95SBruce Richardson */ 79299a2dd95SBruce Richardson int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem); 79399a2dd95SBruce Richardson 79499a2dd95SBruce Richardson /** 79599a2dd95SBruce Richardson * Get guest vring info, including the vring address, vring size, etc. 79699a2dd95SBruce Richardson * 79799a2dd95SBruce Richardson * @param vid 79899a2dd95SBruce Richardson * vhost device ID 79999a2dd95SBruce Richardson * @param vring_idx 80099a2dd95SBruce Richardson * vring index 80199a2dd95SBruce Richardson * @param vring 80299a2dd95SBruce Richardson * the structure to hold the requested vring info 80399a2dd95SBruce Richardson * @return 80499a2dd95SBruce Richardson * 0 on success, -1 on failure 80599a2dd95SBruce Richardson */ 80699a2dd95SBruce Richardson int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, 80799a2dd95SBruce Richardson struct rte_vhost_vring *vring); 80899a2dd95SBruce Richardson 80999a2dd95SBruce Richardson /** 81099a2dd95SBruce Richardson * Get guest inflight vring info, including inflight ring and resubmit list. 81199a2dd95SBruce Richardson * 81299a2dd95SBruce Richardson * @param vid 81399a2dd95SBruce Richardson * vhost device ID 81499a2dd95SBruce Richardson * @param vring_idx 81599a2dd95SBruce Richardson * vring index 81699a2dd95SBruce Richardson * @param vring 81799a2dd95SBruce Richardson * the structure to hold the requested inflight vring info 81899a2dd95SBruce Richardson * @return 81999a2dd95SBruce Richardson * 0 on success, -1 on failure 82099a2dd95SBruce Richardson */ 82199a2dd95SBruce Richardson int 82299a2dd95SBruce Richardson rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx, 82399a2dd95SBruce Richardson struct rte_vhost_ring_inflight *vring); 82499a2dd95SBruce Richardson 82599a2dd95SBruce Richardson /** 82699a2dd95SBruce Richardson * Set split inflight descriptor. 82799a2dd95SBruce Richardson * 8284a6672c2SStephen Hemminger * This function save descriptors that has been consumed in available 82999a2dd95SBruce Richardson * ring 83099a2dd95SBruce Richardson * 83199a2dd95SBruce Richardson * @param vid 83299a2dd95SBruce Richardson * vhost device ID 83399a2dd95SBruce Richardson * @param vring_idx 83499a2dd95SBruce Richardson * vring index 83599a2dd95SBruce Richardson * @param idx 83699a2dd95SBruce Richardson * inflight entry index 83799a2dd95SBruce Richardson * @return 83899a2dd95SBruce Richardson * 0 on success, -1 on failure 83999a2dd95SBruce Richardson */ 84099a2dd95SBruce Richardson int 84199a2dd95SBruce Richardson rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx, 84299a2dd95SBruce Richardson uint16_t idx); 84399a2dd95SBruce Richardson 84499a2dd95SBruce Richardson /** 84599a2dd95SBruce Richardson * Set packed inflight descriptor and get corresponding inflight entry 84699a2dd95SBruce Richardson * 8474a6672c2SStephen Hemminger * This function save descriptors that has been consumed 84899a2dd95SBruce Richardson * 84999a2dd95SBruce Richardson * @param vid 85099a2dd95SBruce Richardson * vhost device ID 85199a2dd95SBruce Richardson * @param vring_idx 85299a2dd95SBruce Richardson * vring index 85399a2dd95SBruce Richardson * @param head 85499a2dd95SBruce Richardson * head of descriptors 85599a2dd95SBruce Richardson * @param last 85699a2dd95SBruce Richardson * last of descriptors 85799a2dd95SBruce Richardson * @param inflight_entry 85899a2dd95SBruce Richardson * corresponding inflight entry 85999a2dd95SBruce Richardson * @return 86099a2dd95SBruce Richardson * 0 on success, -1 on failure 86199a2dd95SBruce Richardson */ 86299a2dd95SBruce Richardson int 86399a2dd95SBruce Richardson rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx, 86499a2dd95SBruce Richardson uint16_t head, uint16_t last, uint16_t *inflight_entry); 86599a2dd95SBruce Richardson 86699a2dd95SBruce Richardson /** 86799a2dd95SBruce Richardson * Save the head of list that the last batch of used descriptors. 86899a2dd95SBruce Richardson * 86999a2dd95SBruce Richardson * @param vid 87099a2dd95SBruce Richardson * vhost device ID 87199a2dd95SBruce Richardson * @param vring_idx 87299a2dd95SBruce Richardson * vring index 87399a2dd95SBruce Richardson * @param idx 87499a2dd95SBruce Richardson * descriptor entry index 87599a2dd95SBruce Richardson * @return 87699a2dd95SBruce Richardson * 0 on success, -1 on failure 87799a2dd95SBruce Richardson */ 87899a2dd95SBruce Richardson int 87999a2dd95SBruce Richardson rte_vhost_set_last_inflight_io_split(int vid, 88099a2dd95SBruce Richardson uint16_t vring_idx, uint16_t idx); 88199a2dd95SBruce Richardson 88299a2dd95SBruce Richardson /** 88399a2dd95SBruce Richardson * Update the inflight free_head, used_idx and used_wrap_counter. 88499a2dd95SBruce Richardson * 88599a2dd95SBruce Richardson * This function will update status first before updating descriptors 88699a2dd95SBruce Richardson * to used 88799a2dd95SBruce Richardson * 88899a2dd95SBruce Richardson * @param vid 88999a2dd95SBruce Richardson * vhost device ID 89099a2dd95SBruce Richardson * @param vring_idx 89199a2dd95SBruce Richardson * vring index 89299a2dd95SBruce Richardson * @param head 89399a2dd95SBruce Richardson * head of descriptors 89499a2dd95SBruce Richardson * @return 89599a2dd95SBruce Richardson * 0 on success, -1 on failure 89699a2dd95SBruce Richardson */ 89799a2dd95SBruce Richardson int 89899a2dd95SBruce Richardson rte_vhost_set_last_inflight_io_packed(int vid, 89999a2dd95SBruce Richardson uint16_t vring_idx, uint16_t head); 90099a2dd95SBruce Richardson 90199a2dd95SBruce Richardson /** 90299a2dd95SBruce Richardson * Clear the split inflight status. 90399a2dd95SBruce Richardson * 90499a2dd95SBruce Richardson * @param vid 90599a2dd95SBruce Richardson * vhost device ID 90699a2dd95SBruce Richardson * @param vring_idx 90799a2dd95SBruce Richardson * vring index 90899a2dd95SBruce Richardson * @param last_used_idx 90999a2dd95SBruce Richardson * last used idx of used ring 91099a2dd95SBruce Richardson * @param idx 91199a2dd95SBruce Richardson * inflight entry index 91299a2dd95SBruce Richardson * @return 91399a2dd95SBruce Richardson * 0 on success, -1 on failure 91499a2dd95SBruce Richardson */ 91599a2dd95SBruce Richardson int 91699a2dd95SBruce Richardson rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx, 91799a2dd95SBruce Richardson uint16_t last_used_idx, uint16_t idx); 91899a2dd95SBruce Richardson 91999a2dd95SBruce Richardson /** 92099a2dd95SBruce Richardson * Clear the packed inflight status. 92199a2dd95SBruce Richardson * 92299a2dd95SBruce Richardson * @param vid 92399a2dd95SBruce Richardson * vhost device ID 92499a2dd95SBruce Richardson * @param vring_idx 92599a2dd95SBruce Richardson * vring index 92699a2dd95SBruce Richardson * @param head 92799a2dd95SBruce Richardson * inflight entry index 92899a2dd95SBruce Richardson * @return 92999a2dd95SBruce Richardson * 0 on success, -1 on failure 93099a2dd95SBruce Richardson */ 93199a2dd95SBruce Richardson int 93299a2dd95SBruce Richardson rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx, 93399a2dd95SBruce Richardson uint16_t head); 93499a2dd95SBruce Richardson 93599a2dd95SBruce Richardson /** 93699a2dd95SBruce Richardson * Notify the guest that used descriptors have been added to the vring. This 93799a2dd95SBruce Richardson * function acts as a memory barrier. 93899a2dd95SBruce Richardson * 93999a2dd95SBruce Richardson * @param vid 94099a2dd95SBruce Richardson * vhost device ID 94199a2dd95SBruce Richardson * @param vring_idx 94299a2dd95SBruce Richardson * vring index 94399a2dd95SBruce Richardson * @return 94499a2dd95SBruce Richardson * 0 on success, -1 on failure 94599a2dd95SBruce Richardson */ 94699a2dd95SBruce Richardson int rte_vhost_vring_call(int vid, uint16_t vring_idx); 94799a2dd95SBruce Richardson 94899a2dd95SBruce Richardson /** 949830f7e79SChangpeng Liu * Notify the guest that used descriptors have been added to the vring. This 950830f7e79SChangpeng Liu * function acts as a memory barrier. This function will return -EAGAIN when 951830f7e79SChangpeng Liu * vq's access lock is held by other thread, user should try again later. 952830f7e79SChangpeng Liu * 953830f7e79SChangpeng Liu * @param vid 954830f7e79SChangpeng Liu * vhost device ID 955830f7e79SChangpeng Liu * @param vring_idx 956830f7e79SChangpeng Liu * vring index 957830f7e79SChangpeng Liu * @return 958830f7e79SChangpeng Liu * 0 on success, -1 on failure, -EAGAIN for another retry 959830f7e79SChangpeng Liu */ 960830f7e79SChangpeng Liu int rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx); 961830f7e79SChangpeng Liu 962830f7e79SChangpeng Liu /** 96399a2dd95SBruce Richardson * Get vhost RX queue avail count. 96499a2dd95SBruce Richardson * 96599a2dd95SBruce Richardson * @param vid 96699a2dd95SBruce Richardson * vhost device ID 96799a2dd95SBruce Richardson * @param qid 96899a2dd95SBruce Richardson * virtio queue index in mq case 96999a2dd95SBruce Richardson * @return 97099a2dd95SBruce Richardson * num of desc available 97199a2dd95SBruce Richardson */ 97299a2dd95SBruce Richardson uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid); 97399a2dd95SBruce Richardson 97499a2dd95SBruce Richardson /** 97534fd4373SMiao Li * Get power monitor address of the vhost device 97634fd4373SMiao Li * 97734fd4373SMiao Li * @param vid 97834fd4373SMiao Li * vhost device ID 97934fd4373SMiao Li * @param queue_id 98034fd4373SMiao Li * vhost queue ID 98134fd4373SMiao Li * @param pmc 98234fd4373SMiao Li * power monitor condition 98334fd4373SMiao Li * @return 98434fd4373SMiao Li * 0 on success, -1 on failure 98534fd4373SMiao Li */ 98634fd4373SMiao Li int 98734fd4373SMiao Li rte_vhost_get_monitor_addr(int vid, uint16_t queue_id, 98834fd4373SMiao Li struct rte_vhost_power_monitor_cond *pmc); 98934fd4373SMiao Li 99034fd4373SMiao Li /** 99199a2dd95SBruce Richardson * Get log base and log size of the vhost device 99299a2dd95SBruce Richardson * 99399a2dd95SBruce Richardson * @param vid 99499a2dd95SBruce Richardson * vhost device ID 99599a2dd95SBruce Richardson * @param log_base 99699a2dd95SBruce Richardson * vhost log base 99799a2dd95SBruce Richardson * @param log_size 99899a2dd95SBruce Richardson * vhost log size 99999a2dd95SBruce Richardson * @return 100099a2dd95SBruce Richardson * 0 on success, -1 on failure 100199a2dd95SBruce Richardson */ 100299a2dd95SBruce Richardson int 100399a2dd95SBruce Richardson rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size); 100499a2dd95SBruce Richardson 100599a2dd95SBruce Richardson /** 100699a2dd95SBruce Richardson * Get last_avail/used_idx of the vhost virtqueue 100799a2dd95SBruce Richardson * 100899a2dd95SBruce Richardson * @param vid 100999a2dd95SBruce Richardson * vhost device ID 101099a2dd95SBruce Richardson * @param queue_id 101199a2dd95SBruce Richardson * vhost queue index 101299a2dd95SBruce Richardson * @param last_avail_idx 101399a2dd95SBruce Richardson * vhost last_avail_idx to get 101499a2dd95SBruce Richardson * @param last_used_idx 101599a2dd95SBruce Richardson * vhost last_used_idx to get 101699a2dd95SBruce Richardson * @return 101799a2dd95SBruce Richardson * 0 on success, -1 on failure 101899a2dd95SBruce Richardson */ 101999a2dd95SBruce Richardson int 102099a2dd95SBruce Richardson rte_vhost_get_vring_base(int vid, uint16_t queue_id, 102199a2dd95SBruce Richardson uint16_t *last_avail_idx, uint16_t *last_used_idx); 102299a2dd95SBruce Richardson 102399a2dd95SBruce Richardson /** 102499a2dd95SBruce Richardson * Get last_avail/last_used of the vhost virtqueue 102599a2dd95SBruce Richardson * 102699a2dd95SBruce Richardson * This function is designed for the reconnection and it's specific for 102799a2dd95SBruce Richardson * the packed ring as we can get the two parameters from the inflight 102899a2dd95SBruce Richardson * queueregion 102999a2dd95SBruce Richardson * 103099a2dd95SBruce Richardson * @param vid 103199a2dd95SBruce Richardson * vhost device ID 103299a2dd95SBruce Richardson * @param queue_id 103399a2dd95SBruce Richardson * vhost queue index 103499a2dd95SBruce Richardson * @param last_avail_idx 103599a2dd95SBruce Richardson * vhost last_avail_idx to get 103699a2dd95SBruce Richardson * @param last_used_idx 103799a2dd95SBruce Richardson * vhost last_used_idx to get 103899a2dd95SBruce Richardson * @return 103999a2dd95SBruce Richardson * 0 on success, -1 on failure 104099a2dd95SBruce Richardson */ 104199a2dd95SBruce Richardson int 104299a2dd95SBruce Richardson rte_vhost_get_vring_base_from_inflight(int vid, 104399a2dd95SBruce Richardson uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx); 104499a2dd95SBruce Richardson 104599a2dd95SBruce Richardson /** 104699a2dd95SBruce Richardson * Set last_avail/used_idx of the vhost virtqueue 104799a2dd95SBruce Richardson * 104899a2dd95SBruce Richardson * @param vid 104999a2dd95SBruce Richardson * vhost device ID 105099a2dd95SBruce Richardson * @param queue_id 105199a2dd95SBruce Richardson * vhost queue index 105299a2dd95SBruce Richardson * @param last_avail_idx 105399a2dd95SBruce Richardson * last_avail_idx to set 105499a2dd95SBruce Richardson * @param last_used_idx 105599a2dd95SBruce Richardson * last_used_idx to set 105699a2dd95SBruce Richardson * @return 105799a2dd95SBruce Richardson * 0 on success, -1 on failure 105899a2dd95SBruce Richardson */ 105999a2dd95SBruce Richardson int 106099a2dd95SBruce Richardson rte_vhost_set_vring_base(int vid, uint16_t queue_id, 106199a2dd95SBruce Richardson uint16_t last_avail_idx, uint16_t last_used_idx); 106299a2dd95SBruce Richardson 106399a2dd95SBruce Richardson /** 106499a2dd95SBruce Richardson * Register external message handling callbacks 106599a2dd95SBruce Richardson * 106699a2dd95SBruce Richardson * @param vid 106799a2dd95SBruce Richardson * vhost device ID 106899a2dd95SBruce Richardson * @param ops 106999a2dd95SBruce Richardson * virtio external callbacks to register 107099a2dd95SBruce Richardson * @param ctx 107199a2dd95SBruce Richardson * additional context passed to the callbacks 107299a2dd95SBruce Richardson * @return 107399a2dd95SBruce Richardson * 0 on success, -1 on failure 107499a2dd95SBruce Richardson */ 107599a2dd95SBruce Richardson int 107699a2dd95SBruce Richardson rte_vhost_extern_callback_register(int vid, 107799a2dd95SBruce Richardson struct rte_vhost_user_extern_ops const * const ops, void *ctx); 107899a2dd95SBruce Richardson 107999a2dd95SBruce Richardson /** 108099a2dd95SBruce Richardson * Get vdpa device id for vhost device. 108199a2dd95SBruce Richardson * 108299a2dd95SBruce Richardson * @param vid 108399a2dd95SBruce Richardson * vhost device id 108499a2dd95SBruce Richardson * @return 108599a2dd95SBruce Richardson * vDPA device pointer on success, NULL on failure 108699a2dd95SBruce Richardson */ 108799a2dd95SBruce Richardson struct rte_vdpa_device * 108899a2dd95SBruce Richardson rte_vhost_get_vdpa_device(int vid); 108999a2dd95SBruce Richardson 109099a2dd95SBruce Richardson /** 109199a2dd95SBruce Richardson * Notify the guest that should get virtio configuration space from backend. 109299a2dd95SBruce Richardson * 109399a2dd95SBruce Richardson * @param vid 109499a2dd95SBruce Richardson * vhost device ID 109599a2dd95SBruce Richardson * @param need_reply 109671998eb6SNobuhiro Miki * wait for the frontend response the status of this operation 109799a2dd95SBruce Richardson * @return 109899a2dd95SBruce Richardson * 0 on success, < 0 on failure 109999a2dd95SBruce Richardson */ 110099a2dd95SBruce Richardson int 110171998eb6SNobuhiro Miki rte_vhost_backend_config_change(int vid, bool need_reply); 110299a2dd95SBruce Richardson 1103be75dc99SMaxime Coquelin /** 1104be75dc99SMaxime Coquelin * Retrieve names of statistics of a Vhost virtqueue. 1105be75dc99SMaxime Coquelin * 1106be75dc99SMaxime Coquelin * There is an assumption that 'stat_names' and 'stats' arrays are matched 1107be75dc99SMaxime Coquelin * by array index: stats_names[i].name => stats[i].value 1108be75dc99SMaxime Coquelin * 1109be75dc99SMaxime Coquelin * @param vid 1110be75dc99SMaxime Coquelin * vhost device ID 1111be75dc99SMaxime Coquelin * @param queue_id 1112be75dc99SMaxime Coquelin * vhost queue index 1113be75dc99SMaxime Coquelin * @param name 1114be75dc99SMaxime Coquelin * array of at least size elements to be filled. 1115be75dc99SMaxime Coquelin * If set to NULL, the function returns the required number of elements. 1116be75dc99SMaxime Coquelin * @param size 1117be75dc99SMaxime Coquelin * The number of elements in stats_names array. 1118be75dc99SMaxime Coquelin * @return 1119be75dc99SMaxime Coquelin * - Success if greater than 0 and lower or equal to *size*. The return value 1120be75dc99SMaxime Coquelin * indicates the number of elements filled in the *names* array. 1121be75dc99SMaxime Coquelin * - Failure if greater than *size*. The return value indicates the number of 1122be75dc99SMaxime Coquelin * elements the *names* array that should be given to succeed. 1123be75dc99SMaxime Coquelin * - Failure if lower than 0. The device ID or queue ID is invalid or 1124be75dc99SMaxime Coquelin + statistics collection is not enabled. 1125be75dc99SMaxime Coquelin */ 1126be75dc99SMaxime Coquelin int 1127be75dc99SMaxime Coquelin rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id, 1128be75dc99SMaxime Coquelin struct rte_vhost_stat_name *name, unsigned int size); 1129be75dc99SMaxime Coquelin 1130be75dc99SMaxime Coquelin /** 1131be75dc99SMaxime Coquelin * Retrieve statistics of a Vhost virtqueue. 1132be75dc99SMaxime Coquelin * 1133be75dc99SMaxime Coquelin * There is an assumption that 'stat_names' and 'stats' arrays are matched 1134be75dc99SMaxime Coquelin * by array index: stats_names[i].name => stats[i].value 1135be75dc99SMaxime Coquelin * 1136be75dc99SMaxime Coquelin * @param vid 1137be75dc99SMaxime Coquelin * vhost device ID 1138be75dc99SMaxime Coquelin * @param queue_id 1139be75dc99SMaxime Coquelin * vhost queue index 1140be75dc99SMaxime Coquelin * @param stats 1141be75dc99SMaxime Coquelin * A pointer to a table of structure of type rte_vhost_stat to be filled with 1142be75dc99SMaxime Coquelin * virtqueue statistics ids and values. 1143be75dc99SMaxime Coquelin * @param n 1144be75dc99SMaxime Coquelin * The number of elements in stats array. 1145be75dc99SMaxime Coquelin * @return 1146be75dc99SMaxime Coquelin * - Success if greater than 0 and lower or equal to *n*. The return value 1147be75dc99SMaxime Coquelin * indicates the number of elements filled in the *stats* array. 1148be75dc99SMaxime Coquelin * - Failure if greater than *n*. The return value indicates the number of 1149be75dc99SMaxime Coquelin * elements the *stats* array that should be given to succeed. 1150be75dc99SMaxime Coquelin * - Failure if lower than 0. The device ID or queue ID is invalid, or 1151be75dc99SMaxime Coquelin * statistics collection is not enabled. 1152be75dc99SMaxime Coquelin */ 1153be75dc99SMaxime Coquelin int 1154be75dc99SMaxime Coquelin rte_vhost_vring_stats_get(int vid, uint16_t queue_id, 1155be75dc99SMaxime Coquelin struct rte_vhost_stat *stats, unsigned int n); 1156be75dc99SMaxime Coquelin 1157be75dc99SMaxime Coquelin /** 1158be75dc99SMaxime Coquelin * Reset statistics of a Vhost virtqueue. 1159be75dc99SMaxime Coquelin * 1160be75dc99SMaxime Coquelin * @param vid 1161be75dc99SMaxime Coquelin * vhost device ID 1162be75dc99SMaxime Coquelin * @param queue_id 1163be75dc99SMaxime Coquelin * vhost queue index 1164be75dc99SMaxime Coquelin * @return 1165be75dc99SMaxime Coquelin * - Success if 0. Statistics have been reset. 1166be75dc99SMaxime Coquelin * - Failure if lower than 0. The device ID or queue ID is invalid, or 1167be75dc99SMaxime Coquelin * statistics collection is not enabled. 1168be75dc99SMaxime Coquelin */ 1169be75dc99SMaxime Coquelin int 1170be75dc99SMaxime Coquelin rte_vhost_vring_stats_reset(int vid, uint16_t queue_id); 1171be75dc99SMaxime Coquelin 117299a2dd95SBruce Richardson #ifdef __cplusplus 117399a2dd95SBruce Richardson } 117499a2dd95SBruce Richardson #endif 117599a2dd95SBruce Richardson 117699a2dd95SBruce Richardson #endif /* _RTE_VHOST_H_ */ 1177