1d5970055SMichael S. Tsirkin #ifndef VHOST_H 2d5970055SMichael S. Tsirkin #define VHOST_H 3d5970055SMichael S. Tsirkin 424d1eb33SNikolay Nikolaev #include "hw/virtio/vhost-backend.h" 50d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 6*8be545baSRichard Henderson #include "system/memory.h" 7d5970055SMichael S. Tsirkin 8f3034ad7SLaurent Vivier #define VHOST_F_DEVICE_IOTLB 63 9f3034ad7SLaurent Vivier #define VHOST_USER_F_PROTOCOL_FEATURES 30 10f3034ad7SLaurent Vivier 114dfcc09fSLi Feng #define VU_REALIZE_CONN_RETRIES 3 124dfcc09fSLi Feng 13d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */ 145ad204bfSXie Yongji 155ad204bfSXie Yongji struct vhost_inflight { 165ad204bfSXie Yongji int fd; 175ad204bfSXie Yongji void *addr; 185ad204bfSXie Yongji uint64_t size; 195ad204bfSXie Yongji uint64_t offset; 205ad204bfSXie Yongji uint16_t queue_size; 215ad204bfSXie Yongji }; 225ad204bfSXie Yongji 23d5970055SMichael S. Tsirkin struct vhost_virtqueue { 24d5970055SMichael S. Tsirkin int kick; 25d5970055SMichael S. Tsirkin int call; 26d5970055SMichael S. Tsirkin void *desc; 27d5970055SMichael S. Tsirkin void *avail; 28d5970055SMichael S. Tsirkin void *used; 29d5970055SMichael S. Tsirkin int num; 30f1f9e6c5SGreg Kurz unsigned long long desc_phys; 31f1f9e6c5SGreg Kurz unsigned desc_size; 32f1f9e6c5SGreg Kurz unsigned long long avail_phys; 33f1f9e6c5SGreg Kurz unsigned avail_size; 34d5970055SMichael S. Tsirkin unsigned long long used_phys; 35d5970055SMichael S. Tsirkin unsigned used_size; 36f56a1247SMichael S. Tsirkin EventNotifier masked_notifier; 37ae50ae0bSKonstantin Khlebnikov EventNotifier error_notifier; 38f9a09ca3SCindy Lu EventNotifier masked_config_notifier; 39c471ad0eSJason Wang struct vhost_dev *dev; 40d5970055SMichael S. Tsirkin }; 41d5970055SMichael S. Tsirkin 42d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t; 43d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000 44d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t)) 45d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS) 462e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT (0xff) 47f9a09ca3SCindy Lu #define VHOST_QUEUE_NUM_CONFIG_INR 0 48d5970055SMichael S. Tsirkin 49309750faSJason Wang struct vhost_log { 50309750faSJason Wang unsigned long long size; 51309750faSJason Wang int refcnt; 5215324404SMarc-André Lureau int fd; 5315324404SMarc-André Lureau vhost_log_chunk_t *log; 54309750faSJason Wang }; 55309750faSJason Wang 56375f74f4SJason Wang struct vhost_dev; 57375f74f4SJason Wang struct vhost_iommu { 58375f74f4SJason Wang struct vhost_dev *hdev; 59375f74f4SJason Wang MemoryRegion *mr; 60375f74f4SJason Wang hwaddr iommu_offset; 61375f74f4SJason Wang IOMMUNotifier n; 62375f74f4SJason Wang QLIST_ENTRY(vhost_iommu) iommu_next; 63375f74f4SJason Wang }; 64375f74f4SJason Wang 654c3e257bSChangpeng Liu typedef struct VhostDevConfigOps { 664c3e257bSChangpeng Liu /* Vhost device config space changed callback 674c3e257bSChangpeng Liu */ 684c3e257bSChangpeng Liu int (*vhost_dev_config_notifier)(struct vhost_dev *dev); 694c3e257bSChangpeng Liu } VhostDevConfigOps; 704c3e257bSChangpeng Liu 71d5970055SMichael S. Tsirkin struct vhost_memory; 7227351992SAlex Bennée 7327351992SAlex Bennée /** 7427351992SAlex Bennée * struct vhost_dev - common vhost_dev structure 7527351992SAlex Bennée * @vhost_ops: backend specific ops 7627351992SAlex Bennée * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier) 7727351992SAlex Bennée */ 78d5970055SMichael S. Tsirkin struct vhost_dev { 79c471ad0eSJason Wang VirtIODevice *vdev; 8004097f7cSAvi Kivity MemoryListener memory_listener; 81375f74f4SJason Wang MemoryListener iommu_listener; 82d5970055SMichael S. Tsirkin struct vhost_memory *mem; 832817b260SAvi Kivity int n_mem_sections; 842817b260SAvi Kivity MemoryRegionSection *mem_sections; 85c44317efSDr. David Alan Gilbert int n_tmp_sections; 86c44317efSDr. David Alan Gilbert MemoryRegionSection *tmp_sections; 87d5970055SMichael S. Tsirkin struct vhost_virtqueue *vqs; 885fc13603SJason Wang unsigned int nvqs; 899be6e69fSGreg Kurz /* the first virtqueue which would be used by this vhost dev */ 90a9f98bb5SJason Wang int vq_index; 91245cf2c2SEugenio Pérez /* one past the last vq index for the virtio device (not vhost) */ 92245cf2c2SEugenio Pérez int vq_index_end; 93c90bd505SKevin Wolf /* if non-zero, minimum required value for max_queues */ 94c90bd505SKevin Wolf int num_queues; 959600c98eSAlex Bennée /** 969600c98eSAlex Bennée * vhost feature handling requires matching the feature set 979600c98eSAlex Bennée * offered by a backend which may be a subset of the total 989600c98eSAlex Bennée * features eventually offered to the guest. 999600c98eSAlex Bennée * 1009600c98eSAlex Bennée * @features: available features provided by the backend 1019600c98eSAlex Bennée * @acked_features: final negotiated features with front-end driver 1029600c98eSAlex Bennée * 1039600c98eSAlex Bennée * @backend_features: this is used in a couple of places to either 1049600c98eSAlex Bennée * store VHOST_USER_F_PROTOCOL_FEATURES to apply to 1059600c98eSAlex Bennée * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its 1069600c98eSAlex Bennée * future use should be discouraged and the variable retired as 1079600c98eSAlex Bennée * its easy to confuse with the VirtIO backend_features. 1089600c98eSAlex Bennée */ 10921e70425SMarc-André Lureau uint64_t features; 11021e70425SMarc-André Lureau uint64_t acked_features; 11121e70425SMarc-André Lureau uint64_t backend_features; 1129600c98eSAlex Bennée 1139600c98eSAlex Bennée /** 1149600c98eSAlex Bennée * @protocol_features: is the vhost-user only feature set by 1159600c98eSAlex Bennée * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only 1169600c98eSAlex Bennée * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered 1179600c98eSAlex Bennée * by the backend (see @features). 1189600c98eSAlex Bennée */ 11921e70425SMarc-André Lureau uint64_t protocol_features; 1209600c98eSAlex Bennée 12121e70425SMarc-André Lureau uint64_t max_queues; 122b37556edSJason Wang uint64_t backend_cap; 123b8f3e6a1SAlex Bennée /* @started: is the vhost device started? */ 124d5970055SMichael S. Tsirkin bool started; 125d5970055SMichael S. Tsirkin bool log_enabled; 12621e70425SMarc-André Lureau uint64_t log_size; 1277145872eSMichael S. Tsirkin Error *migration_blocker; 12824d1eb33SNikolay Nikolaev const VhostOps *vhost_ops; 1291a1bfac9SNikolay Nikolaev void *opaque; 130309750faSJason Wang struct vhost_log *log; 1312ce68e4cSIgor Mammedov QLIST_ENTRY(vhost_dev) entry; 132c5cd7e5fSSi-Wei Liu QLIST_ENTRY(vhost_dev) logdev_entry; 133375f74f4SJason Wang QLIST_HEAD(, vhost_iommu) iommu_list; 134c471ad0eSJason Wang IOMMUNotifier n; 1354c3e257bSChangpeng Liu const VhostDevConfigOps *config_ops; 136d5970055SMichael S. Tsirkin }; 137d5970055SMichael S. Tsirkin 1389b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops; 1399b1d929aSTiberiu Georgescu extern const VhostOps user_ops; 1409b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops; 1419b1d929aSTiberiu Georgescu 142108a6481SCindy Lu struct vhost_net { 143108a6481SCindy Lu struct vhost_dev dev; 144108a6481SCindy Lu struct vhost_virtqueue vqs[2]; 145108a6481SCindy Lu int backend; 146108a6481SCindy Lu NetClientState *nc; 147108a6481SCindy Lu }; 148108a6481SCindy Lu 14927351992SAlex Bennée /** 15027351992SAlex Bennée * vhost_dev_init() - initialise the vhost interface 15127351992SAlex Bennée * @hdev: the common vhost_dev structure 15227351992SAlex Bennée * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa) 15327351992SAlex Bennée * @backend_type: type of backend 15427351992SAlex Bennée * @busyloop_timeout: timeout for polling virtqueue 15527351992SAlex Bennée * @errp: error handle 15627351992SAlex Bennée * 15727351992SAlex Bennée * The initialisation of the vhost device will trigger the 15827351992SAlex Bennée * initialisation of the backend and potentially capability 15927351992SAlex Bennée * negotiation of backend interface. Configuration of the VirtIO 16027351992SAlex Bennée * itself won't happen until the interface is started. 16127351992SAlex Bennée * 16227351992SAlex Bennée * Return: 0 on success, non-zero on error while setting errp. 16327351992SAlex Bennée */ 16481647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque, 16569e87b32SJason Wang VhostBackendType backend_type, 166a6945f22SKevin Wolf uint32_t busyloop_timeout, Error **errp); 16727351992SAlex Bennée 16827351992SAlex Bennée /** 16927351992SAlex Bennée * vhost_dev_cleanup() - tear down and cleanup vhost interface 17027351992SAlex Bennée * @hdev: the common vhost_dev structure 17127351992SAlex Bennée */ 172d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev); 17327351992SAlex Bennée 1746166799fSzuoboqun void vhost_dev_disable_notifiers_nvqs(struct vhost_dev *hdev, 1756166799fSzuoboqun VirtIODevice *vdev, 1766166799fSzuoboqun unsigned int nvqs); 1776166799fSzuoboqun 17827351992SAlex Bennée /** 17927351992SAlex Bennée * vhost_dev_enable_notifiers() - enable event notifiers 18027351992SAlex Bennée * @hdev: common vhost_dev structure 18127351992SAlex Bennée * @vdev: the VirtIODevice structure 18227351992SAlex Bennée * 18327351992SAlex Bennée * Enable notifications directly to the vhost device rather than being 18427351992SAlex Bennée * triggered by QEMU itself. Notifications should be enabled before 18527351992SAlex Bennée * the vhost device is started via @vhost_dev_start. 18627351992SAlex Bennée * 18727351992SAlex Bennée * Return: 0 on success, < 0 on error. 18827351992SAlex Bennée */ 189b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); 19027351992SAlex Bennée 19127351992SAlex Bennée /** 19227351992SAlex Bennée * vhost_dev_disable_notifiers - disable event notifications 19327351992SAlex Bennée * @hdev: common vhost_dev structure 19427351992SAlex Bennée * @vdev: the VirtIODevice structure 19527351992SAlex Bennée * 19627351992SAlex Bennée * Disable direct notifications to vhost device. 19727351992SAlex Bennée */ 198b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); 199f9a09ca3SCindy Lu bool vhost_config_pending(struct vhost_dev *hdev); 200f9a09ca3SCindy Lu void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask); 201d5970055SMichael S. Tsirkin 20227351992SAlex Bennée /** 203b8f3e6a1SAlex Bennée * vhost_dev_is_started() - report status of vhost device 204b8f3e6a1SAlex Bennée * @hdev: common vhost_dev structure 205b8f3e6a1SAlex Bennée * 206b8f3e6a1SAlex Bennée * Return the started status of the vhost device 207b8f3e6a1SAlex Bennée */ 208b8f3e6a1SAlex Bennée static inline bool vhost_dev_is_started(struct vhost_dev *hdev) 209b8f3e6a1SAlex Bennée { 210b8f3e6a1SAlex Bennée return hdev->started; 211b8f3e6a1SAlex Bennée } 212b8f3e6a1SAlex Bennée 213b8f3e6a1SAlex Bennée /** 21427351992SAlex Bennée * vhost_dev_start() - start the vhost device 21527351992SAlex Bennée * @hdev: common vhost_dev structure 21627351992SAlex Bennée * @vdev: the VirtIODevice structure 2174daa5054SStefano Garzarella * @vrings: true to have vrings enabled in this call 21827351992SAlex Bennée * 21927351992SAlex Bennée * Starts the vhost device. From this point VirtIO feature negotiation 22027351992SAlex Bennée * can start and the device can start processing VirtIO transactions. 22127351992SAlex Bennée * 22227351992SAlex Bennée * Return: 0 on success, < 0 on error. 22327351992SAlex Bennée */ 2244daa5054SStefano Garzarella int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); 22527351992SAlex Bennée 22627351992SAlex Bennée /** 22727351992SAlex Bennée * vhost_dev_stop() - stop the vhost device 22827351992SAlex Bennée * @hdev: common vhost_dev structure 22927351992SAlex Bennée * @vdev: the VirtIODevice structure 2304daa5054SStefano Garzarella * @vrings: true to have vrings disabled in this call 23127351992SAlex Bennée * 23227351992SAlex Bennée * Stop the vhost device. After the device is stopped the notifiers 23327351992SAlex Bennée * can be disabled (@vhost_dev_disable_notifiers) and the device can 23427351992SAlex Bennée * be torn down (@vhost_dev_cleanup). 23527351992SAlex Bennée */ 2364daa5054SStefano Garzarella void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); 23727351992SAlex Bennée 23827351992SAlex Bennée /** 23927351992SAlex Bennée * DOC: vhost device configuration handling 24027351992SAlex Bennée * 24127351992SAlex Bennée * The VirtIO device configuration space is used for rarely changing 24227351992SAlex Bennée * or initialisation time parameters. The configuration can be updated 24327351992SAlex Bennée * by either the guest driver or the device itself. If the device can 24427351992SAlex Bennée * change the configuration over time the vhost handler should 24527351992SAlex Bennée * register a @VhostDevConfigOps structure with 24627351992SAlex Bennée * @vhost_dev_set_config_notifier so the guest can be notified. Some 24727351992SAlex Bennée * devices register a handler anyway and will signal an error if an 24827351992SAlex Bennée * unexpected config change happens. 24927351992SAlex Bennée */ 25027351992SAlex Bennée 25127351992SAlex Bennée /** 25227351992SAlex Bennée * vhost_dev_get_config() - fetch device configuration 25327351992SAlex Bennée * @hdev: common vhost_dev_structure 25427351992SAlex Bennée * @config: pointer to device appropriate config structure 25527351992SAlex Bennée * @config_len: size of device appropriate config structure 25627351992SAlex Bennée * 25727351992SAlex Bennée * Return: 0 on success, < 0 on error while setting errp 25827351992SAlex Bennée */ 25927351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, 26027351992SAlex Bennée uint32_t config_len, Error **errp); 26127351992SAlex Bennée 26227351992SAlex Bennée /** 26327351992SAlex Bennée * vhost_dev_set_config() - set device configuration 26427351992SAlex Bennée * @hdev: common vhost_dev_structure 26527351992SAlex Bennée * @data: pointer to data to set 26627351992SAlex Bennée * @offset: offset into configuration space 26727351992SAlex Bennée * @size: length of set 26827351992SAlex Bennée * @flags: @VhostSetConfigType flags 26927351992SAlex Bennée * 27027351992SAlex Bennée * By use of @offset/@size a subset of the configuration space can be 27127351992SAlex Bennée * written to. The @flags are used to indicate if it is a normal 27227351992SAlex Bennée * transaction or related to migration. 27327351992SAlex Bennée * 27427351992SAlex Bennée * Return: 0 on success, non-zero on error 27527351992SAlex Bennée */ 27627351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data, 27727351992SAlex Bennée uint32_t offset, uint32_t size, uint32_t flags); 27827351992SAlex Bennée 27927351992SAlex Bennée /** 28027351992SAlex Bennée * vhost_dev_set_config_notifier() - register VhostDevConfigOps 28127351992SAlex Bennée * @hdev: common vhost_dev_structure 28227351992SAlex Bennée * @ops: notifier ops 28327351992SAlex Bennée * 28427351992SAlex Bennée * If the device is expected to change configuration a notifier can be 28527351992SAlex Bennée * setup to handle the case. 28627351992SAlex Bennée */ 28727351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev, 28827351992SAlex Bennée const VhostDevConfigOps *ops); 28927351992SAlex Bennée 29027351992SAlex Bennée 291f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status. 292f56a1247SMichael S. Tsirkin * Should be called after unmask to avoid losing events. 293f56a1247SMichael S. Tsirkin */ 294f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n); 295f56a1247SMichael S. Tsirkin 296f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq. 297f56a1247SMichael S. Tsirkin */ 298f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, 299f56a1247SMichael S. Tsirkin bool mask); 3002055c2a4SAlex Bennée 3012055c2a4SAlex Bennée /** 3022055c2a4SAlex Bennée * vhost_get_features() - return a sanitised set of feature bits 3032055c2a4SAlex Bennée * @hdev: common vhost_dev structure 3042055c2a4SAlex Bennée * @feature_bits: pointer to terminated table of feature bits 3052055c2a4SAlex Bennée * @features: original feature set 3062055c2a4SAlex Bennée * 3072055c2a4SAlex Bennée * This returns a set of features bits that is an intersection of what 3082055c2a4SAlex Bennée * is supported by the vhost backend (hdev->features), the supported 3092055c2a4SAlex Bennée * feature_bits and the requested feature set. 3102055c2a4SAlex Bennée */ 3119a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, 3129a2ba823SCornelia Huck uint64_t features); 31381cf38f3SAlex Bennée 31481cf38f3SAlex Bennée /** 31581cf38f3SAlex Bennée * vhost_ack_features() - set vhost acked_features 31681cf38f3SAlex Bennée * @hdev: common vhost_dev structure 31781cf38f3SAlex Bennée * @feature_bits: pointer to terminated table of feature bits 31881cf38f3SAlex Bennée * @features: requested feature set 31981cf38f3SAlex Bennée * 32081cf38f3SAlex Bennée * This sets the internal hdev->acked_features to the intersection of 32181cf38f3SAlex Bennée * the backends advertised features and the supported feature_bits. 32281cf38f3SAlex Bennée */ 3232e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, 3249a2ba823SCornelia Huck uint64_t features); 325cd89c065SDavid Hildenbrand unsigned int vhost_get_max_memslots(void); 3268c49951cSDavid Hildenbrand unsigned int vhost_get_free_memslots(void); 327950d94baSMarc-André Lureau 328950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev, 329950d94baSMarc-André Lureau struct vhost_vring_file *file); 330950d94baSMarc-André Lureau 331ee071f67SViktor Prutyanov void vhost_toggle_device_iotlb(VirtIODevice *vdev); 332fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); 3335ad204bfSXie Yongji 334ff48b628SKangjie Xu int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, 335ff48b628SKangjie Xu struct vhost_virtqueue *vq, unsigned idx); 336e1f101d9SKangjie Xu void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev, 337e1f101d9SKangjie Xu struct vhost_virtqueue *vq, unsigned idx); 338ff48b628SKangjie Xu 3395ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight); 3405ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight); 3411b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev); 3425ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev, 3435ad204bfSXie Yongji struct vhost_inflight *inflight); 3445ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, 3455ad204bfSXie Yongji struct vhost_inflight *inflight); 34674b5d2b5SCindy Lu bool vhost_dev_has_iommu(struct vhost_dev *dev); 347c0c4f147SStefan Hajnoczi 348c0c4f147SStefan Hajnoczi #ifdef CONFIG_VHOST 349c0c4f147SStefan Hajnoczi int vhost_reset_device(struct vhost_dev *hdev); 350c0c4f147SStefan Hajnoczi #else 351c0c4f147SStefan Hajnoczi static inline int vhost_reset_device(struct vhost_dev *hdev) 352c0c4f147SStefan Hajnoczi { 353c0c4f147SStefan Hajnoczi return -ENOSYS; 354c0c4f147SStefan Hajnoczi } 355c0c4f147SStefan Hajnoczi #endif /* CONFIG_VHOST */ 356c0c4f147SStefan Hajnoczi 357cda83adcSHanna Czenczek /** 358cda83adcSHanna Czenczek * vhost_supports_device_state(): Checks whether the back-end supports 359cda83adcSHanna Czenczek * transferring internal device state for the purpose of migration. 360cda83adcSHanna Czenczek * Support for this feature is required for vhost_set_device_state_fd() 361cda83adcSHanna Czenczek * and vhost_check_device_state(). 362cda83adcSHanna Czenczek * 363cda83adcSHanna Czenczek * @dev: The vhost device 364cda83adcSHanna Czenczek * 365cda83adcSHanna Czenczek * Returns true if the device supports these commands, and false if it 366cda83adcSHanna Czenczek * does not. 367cda83adcSHanna Czenczek */ 3683f653573SLaurent Vivier #ifdef CONFIG_VHOST 369cda83adcSHanna Czenczek bool vhost_supports_device_state(struct vhost_dev *dev); 3703f653573SLaurent Vivier #else 3713f653573SLaurent Vivier static inline bool vhost_supports_device_state(struct vhost_dev *dev) 3723f653573SLaurent Vivier { 3733f653573SLaurent Vivier return false; 3743f653573SLaurent Vivier } 3753f653573SLaurent Vivier #endif 376cda83adcSHanna Czenczek 377cda83adcSHanna Czenczek /** 378cda83adcSHanna Czenczek * vhost_set_device_state_fd(): Begin transfer of internal state from/to 379cda83adcSHanna Czenczek * the back-end for the purpose of migration. Data is to be transferred 380cda83adcSHanna Czenczek * over a pipe according to @direction and @phase. The sending end must 381cda83adcSHanna Czenczek * only write to the pipe, and the receiving end must only read from it. 382cda83adcSHanna Czenczek * Once the sending end is done, it closes its FD. The receiving end 383cda83adcSHanna Czenczek * must take this as the end-of-transfer signal and close its FD, too. 384cda83adcSHanna Czenczek * 385cda83adcSHanna Czenczek * @fd is the back-end's end of the pipe: The write FD for SAVE, and the 386cda83adcSHanna Czenczek * read FD for LOAD. This function transfers ownership of @fd to the 387cda83adcSHanna Czenczek * back-end, i.e. closes it in the front-end. 388cda83adcSHanna Czenczek * 389cda83adcSHanna Czenczek * The back-end may optionally reply with an FD of its own, if this 390cda83adcSHanna Czenczek * improves efficiency on its end. In this case, the returned FD is 391cda83adcSHanna Czenczek * stored in *reply_fd. The back-end will discard the FD sent to it, 392cda83adcSHanna Czenczek * and the front-end must use *reply_fd for transferring state to/from 393cda83adcSHanna Czenczek * the back-end. 394cda83adcSHanna Czenczek * 395cda83adcSHanna Czenczek * @dev: The vhost device 396cda83adcSHanna Czenczek * @direction: The direction in which the state is to be transferred. 397cda83adcSHanna Czenczek * For outgoing migrations, this is SAVE, and data is read 398cda83adcSHanna Czenczek * from the back-end and stored by the front-end in the 399cda83adcSHanna Czenczek * migration stream. 400cda83adcSHanna Czenczek * For incoming migrations, this is LOAD, and data is read 401cda83adcSHanna Czenczek * by the front-end from the migration stream and sent to 402cda83adcSHanna Czenczek * the back-end to restore the saved state. 403cda83adcSHanna Czenczek * @phase: Which migration phase we are in. Currently, there is only 404cda83adcSHanna Czenczek * STOPPED (device and all vrings are stopped), in the future, 405cda83adcSHanna Czenczek * more phases such as PRE_COPY or POST_COPY may be added. 406cda83adcSHanna Czenczek * @fd: Back-end's end of the pipe through which to transfer state; note 407cda83adcSHanna Czenczek * that ownership is transferred to the back-end, so this function 408cda83adcSHanna Czenczek * closes @fd in the front-end. 409cda83adcSHanna Czenczek * @reply_fd: If the back-end wishes to use a different pipe for state 410cda83adcSHanna Czenczek * transfer, this will contain an FD for the front-end to 411cda83adcSHanna Czenczek * use. Otherwise, -1 is stored here. 412cda83adcSHanna Czenczek * @errp: Potential error description 413cda83adcSHanna Czenczek * 414cda83adcSHanna Czenczek * Returns 0 on success, and -errno on failure. 415cda83adcSHanna Czenczek */ 416cda83adcSHanna Czenczek int vhost_set_device_state_fd(struct vhost_dev *dev, 417cda83adcSHanna Czenczek VhostDeviceStateDirection direction, 418cda83adcSHanna Czenczek VhostDeviceStatePhase phase, 419cda83adcSHanna Czenczek int fd, 420cda83adcSHanna Czenczek int *reply_fd, 421cda83adcSHanna Czenczek Error **errp); 422cda83adcSHanna Czenczek 423cda83adcSHanna Czenczek /** 424cda83adcSHanna Czenczek * vhost_set_device_state_fd(): After transferring state from/to the 425cda83adcSHanna Czenczek * back-end via vhost_set_device_state_fd(), i.e. once the sending end 426cda83adcSHanna Czenczek * has closed the pipe, inquire the back-end to report any potential 427cda83adcSHanna Czenczek * errors that have occurred on its side. This allows to sense errors 428cda83adcSHanna Czenczek * like: 429cda83adcSHanna Czenczek * - During outgoing migration, when the source side had already started 430cda83adcSHanna Czenczek * to produce its state, something went wrong and it failed to finish 431cda83adcSHanna Czenczek * - During incoming migration, when the received state is somehow 432cda83adcSHanna Czenczek * invalid and cannot be processed by the back-end 433cda83adcSHanna Czenczek * 434cda83adcSHanna Czenczek * @dev: The vhost device 435cda83adcSHanna Czenczek * @errp: Potential error description 436cda83adcSHanna Czenczek * 437cda83adcSHanna Czenczek * Returns 0 when the back-end reports successful state transfer and 438cda83adcSHanna Czenczek * processing, and -errno when an error occurred somewhere. 439cda83adcSHanna Czenczek */ 440cda83adcSHanna Czenczek int vhost_check_device_state(struct vhost_dev *dev, Error **errp); 441cda83adcSHanna Czenczek 4424a00d5d7SHanna Czenczek /** 4434a00d5d7SHanna Czenczek * vhost_save_backend_state(): High-level function to receive a vhost 4444a00d5d7SHanna Czenczek * back-end's state, and save it in @f. Uses 4454a00d5d7SHanna Czenczek * `vhost_set_device_state_fd()` to get the data from the back-end, and 4464a00d5d7SHanna Czenczek * stores it in consecutive chunks that are each prefixed by their 4474a00d5d7SHanna Czenczek * respective length (be32). The end is marked by a 0-length chunk. 4484a00d5d7SHanna Czenczek * 4494a00d5d7SHanna Czenczek * Must only be called while the device and all its vrings are stopped 4504a00d5d7SHanna Czenczek * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`). 4514a00d5d7SHanna Czenczek * 4524a00d5d7SHanna Czenczek * @dev: The vhost device from which to save the state 4534a00d5d7SHanna Czenczek * @f: Migration stream in which to save the state 4544a00d5d7SHanna Czenczek * @errp: Potential error message 4554a00d5d7SHanna Czenczek * 4564a00d5d7SHanna Czenczek * Returns 0 on success, and -errno otherwise. 4574a00d5d7SHanna Czenczek */ 4583f653573SLaurent Vivier #ifdef CONFIG_VHOST 4594a00d5d7SHanna Czenczek int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp); 4603f653573SLaurent Vivier #else 4613f653573SLaurent Vivier static inline int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, 4623f653573SLaurent Vivier Error **errp) 4633f653573SLaurent Vivier { 4643f653573SLaurent Vivier return -ENOSYS; 4653f653573SLaurent Vivier } 4663f653573SLaurent Vivier #endif 4674a00d5d7SHanna Czenczek 4684a00d5d7SHanna Czenczek /** 4694a00d5d7SHanna Czenczek * vhost_load_backend_state(): High-level function to load a vhost 4704a00d5d7SHanna Czenczek * back-end's state from @f, and send it over to the back-end. Reads 4714a00d5d7SHanna Czenczek * the data from @f in the format used by `vhost_save_state()`, and uses 4724a00d5d7SHanna Czenczek * `vhost_set_device_state_fd()` to transfer it to the back-end. 4734a00d5d7SHanna Czenczek * 4744a00d5d7SHanna Czenczek * Must only be called while the device and all its vrings are stopped 4754a00d5d7SHanna Czenczek * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`). 4764a00d5d7SHanna Czenczek * 477801faee4SMichael Tokarev * @dev: The vhost device to which to send the state 4784a00d5d7SHanna Czenczek * @f: Migration stream from which to load the state 4794a00d5d7SHanna Czenczek * @errp: Potential error message 4804a00d5d7SHanna Czenczek * 4814a00d5d7SHanna Czenczek * Returns 0 on success, and -errno otherwise. 4824a00d5d7SHanna Czenczek */ 4833f653573SLaurent Vivier #ifdef CONFIG_VHOST 4844a00d5d7SHanna Czenczek int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp); 4853f653573SLaurent Vivier #else 4863f653573SLaurent Vivier static inline int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, 4873f653573SLaurent Vivier Error **errp) 4883f653573SLaurent Vivier { 4893f653573SLaurent Vivier return -ENOSYS; 4903f653573SLaurent Vivier } 4913f653573SLaurent Vivier #endif 4924a00d5d7SHanna Czenczek 493d5970055SMichael S. Tsirkin #endif 494