xref: /qemu/include/hw/virtio/vhost.h (revision cda83adc62b6108afc8a82d0f54d9a9a861e7aa8)
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"
6022c62cbSPaolo Bonzini #include "exec/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;
132375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
133c471ad0eSJason Wang     IOMMUNotifier n;
1344c3e257bSChangpeng Liu     const VhostDevConfigOps *config_ops;
135d5970055SMichael S. Tsirkin };
136d5970055SMichael S. Tsirkin 
1379b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops;
1389b1d929aSTiberiu Georgescu extern const VhostOps user_ops;
1399b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops;
1409b1d929aSTiberiu Georgescu 
141108a6481SCindy Lu struct vhost_net {
142108a6481SCindy Lu     struct vhost_dev dev;
143108a6481SCindy Lu     struct vhost_virtqueue vqs[2];
144108a6481SCindy Lu     int backend;
145108a6481SCindy Lu     NetClientState *nc;
146108a6481SCindy Lu };
147108a6481SCindy Lu 
14827351992SAlex Bennée /**
14927351992SAlex Bennée  * vhost_dev_init() - initialise the vhost interface
15027351992SAlex Bennée  * @hdev: the common vhost_dev structure
15127351992SAlex Bennée  * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
15227351992SAlex Bennée  * @backend_type: type of backend
15327351992SAlex Bennée  * @busyloop_timeout: timeout for polling virtqueue
15427351992SAlex Bennée  * @errp: error handle
15527351992SAlex Bennée  *
15627351992SAlex Bennée  * The initialisation of the vhost device will trigger the
15727351992SAlex Bennée  * initialisation of the backend and potentially capability
15827351992SAlex Bennée  * negotiation of backend interface. Configuration of the VirtIO
15927351992SAlex Bennée  * itself won't happen until the interface is started.
16027351992SAlex Bennée  *
16127351992SAlex Bennée  * Return: 0 on success, non-zero on error while setting errp.
16227351992SAlex Bennée  */
16381647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
16469e87b32SJason Wang                    VhostBackendType backend_type,
165a6945f22SKevin Wolf                    uint32_t busyloop_timeout, Error **errp);
16627351992SAlex Bennée 
16727351992SAlex Bennée /**
16827351992SAlex Bennée  * vhost_dev_cleanup() - tear down and cleanup vhost interface
16927351992SAlex Bennée  * @hdev: the common vhost_dev structure
17027351992SAlex Bennée  */
171d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
17227351992SAlex Bennée 
17327351992SAlex Bennée /**
17427351992SAlex Bennée  * vhost_dev_enable_notifiers() - enable event notifiers
17527351992SAlex Bennée  * @hdev: common vhost_dev structure
17627351992SAlex Bennée  * @vdev: the VirtIODevice structure
17727351992SAlex Bennée  *
17827351992SAlex Bennée  * Enable notifications directly to the vhost device rather than being
17927351992SAlex Bennée  * triggered by QEMU itself. Notifications should be enabled before
18027351992SAlex Bennée  * the vhost device is started via @vhost_dev_start.
18127351992SAlex Bennée  *
18227351992SAlex Bennée  * Return: 0 on success, < 0 on error.
18327351992SAlex Bennée  */
184b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
18527351992SAlex Bennée 
18627351992SAlex Bennée /**
18727351992SAlex Bennée  * vhost_dev_disable_notifiers - disable event notifications
18827351992SAlex Bennée  * @hdev: common vhost_dev structure
18927351992SAlex Bennée  * @vdev: the VirtIODevice structure
19027351992SAlex Bennée  *
19127351992SAlex Bennée  * Disable direct notifications to vhost device.
19227351992SAlex Bennée  */
193b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
194f9a09ca3SCindy Lu bool vhost_config_pending(struct vhost_dev *hdev);
195f9a09ca3SCindy Lu void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
196d5970055SMichael S. Tsirkin 
19727351992SAlex Bennée /**
198b8f3e6a1SAlex Bennée  * vhost_dev_is_started() - report status of vhost device
199b8f3e6a1SAlex Bennée  * @hdev: common vhost_dev structure
200b8f3e6a1SAlex Bennée  *
201b8f3e6a1SAlex Bennée  * Return the started status of the vhost device
202b8f3e6a1SAlex Bennée  */
203b8f3e6a1SAlex Bennée static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
204b8f3e6a1SAlex Bennée {
205b8f3e6a1SAlex Bennée     return hdev->started;
206b8f3e6a1SAlex Bennée }
207b8f3e6a1SAlex Bennée 
208b8f3e6a1SAlex Bennée /**
20927351992SAlex Bennée  * vhost_dev_start() - start the vhost device
21027351992SAlex Bennée  * @hdev: common vhost_dev structure
21127351992SAlex Bennée  * @vdev: the VirtIODevice structure
2124daa5054SStefano Garzarella  * @vrings: true to have vrings enabled in this call
21327351992SAlex Bennée  *
21427351992SAlex Bennée  * Starts the vhost device. From this point VirtIO feature negotiation
21527351992SAlex Bennée  * can start and the device can start processing VirtIO transactions.
21627351992SAlex Bennée  *
21727351992SAlex Bennée  * Return: 0 on success, < 0 on error.
21827351992SAlex Bennée  */
2194daa5054SStefano Garzarella int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
22027351992SAlex Bennée 
22127351992SAlex Bennée /**
22227351992SAlex Bennée  * vhost_dev_stop() - stop the vhost device
22327351992SAlex Bennée  * @hdev: common vhost_dev structure
22427351992SAlex Bennée  * @vdev: the VirtIODevice structure
2254daa5054SStefano Garzarella  * @vrings: true to have vrings disabled in this call
22627351992SAlex Bennée  *
22727351992SAlex Bennée  * Stop the vhost device. After the device is stopped the notifiers
22827351992SAlex Bennée  * can be disabled (@vhost_dev_disable_notifiers) and the device can
22927351992SAlex Bennée  * be torn down (@vhost_dev_cleanup).
23027351992SAlex Bennée  */
2314daa5054SStefano Garzarella void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
23227351992SAlex Bennée 
23327351992SAlex Bennée /**
23427351992SAlex Bennée  * DOC: vhost device configuration handling
23527351992SAlex Bennée  *
23627351992SAlex Bennée  * The VirtIO device configuration space is used for rarely changing
23727351992SAlex Bennée  * or initialisation time parameters. The configuration can be updated
23827351992SAlex Bennée  * by either the guest driver or the device itself. If the device can
23927351992SAlex Bennée  * change the configuration over time the vhost handler should
24027351992SAlex Bennée  * register a @VhostDevConfigOps structure with
24127351992SAlex Bennée  * @vhost_dev_set_config_notifier so the guest can be notified. Some
24227351992SAlex Bennée  * devices register a handler anyway and will signal an error if an
24327351992SAlex Bennée  * unexpected config change happens.
24427351992SAlex Bennée  */
24527351992SAlex Bennée 
24627351992SAlex Bennée /**
24727351992SAlex Bennée  * vhost_dev_get_config() - fetch device configuration
24827351992SAlex Bennée  * @hdev: common vhost_dev_structure
24927351992SAlex Bennée  * @config: pointer to device appropriate config structure
25027351992SAlex Bennée  * @config_len: size of device appropriate config structure
25127351992SAlex Bennée  *
25227351992SAlex Bennée  * Return: 0 on success, < 0 on error while setting errp
25327351992SAlex Bennée  */
25427351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
25527351992SAlex Bennée                          uint32_t config_len, Error **errp);
25627351992SAlex Bennée 
25727351992SAlex Bennée /**
25827351992SAlex Bennée  * vhost_dev_set_config() - set device configuration
25927351992SAlex Bennée  * @hdev: common vhost_dev_structure
26027351992SAlex Bennée  * @data: pointer to data to set
26127351992SAlex Bennée  * @offset: offset into configuration space
26227351992SAlex Bennée  * @size: length of set
26327351992SAlex Bennée  * @flags: @VhostSetConfigType flags
26427351992SAlex Bennée  *
26527351992SAlex Bennée  * By use of @offset/@size a subset of the configuration space can be
26627351992SAlex Bennée  * written to. The @flags are used to indicate if it is a normal
26727351992SAlex Bennée  * transaction or related to migration.
26827351992SAlex Bennée  *
26927351992SAlex Bennée  * Return: 0 on success, non-zero on error
27027351992SAlex Bennée  */
27127351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
27227351992SAlex Bennée                          uint32_t offset, uint32_t size, uint32_t flags);
27327351992SAlex Bennée 
27427351992SAlex Bennée /**
27527351992SAlex Bennée  * vhost_dev_set_config_notifier() - register VhostDevConfigOps
27627351992SAlex Bennée  * @hdev: common vhost_dev_structure
27727351992SAlex Bennée  * @ops: notifier ops
27827351992SAlex Bennée  *
27927351992SAlex Bennée  * If the device is expected to change configuration a notifier can be
28027351992SAlex Bennée  * setup to handle the case.
28127351992SAlex Bennée  */
28227351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev,
28327351992SAlex Bennée                                    const VhostDevConfigOps *ops);
28427351992SAlex Bennée 
28527351992SAlex Bennée 
286f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
287f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
288f56a1247SMichael S. Tsirkin  */
289f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
290f56a1247SMichael S. Tsirkin 
291f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
292f56a1247SMichael S. Tsirkin  */
293f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
294f56a1247SMichael S. Tsirkin                           bool mask);
2952055c2a4SAlex Bennée 
2962055c2a4SAlex Bennée /**
2972055c2a4SAlex Bennée  * vhost_get_features() - return a sanitised set of feature bits
2982055c2a4SAlex Bennée  * @hdev: common vhost_dev structure
2992055c2a4SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
3002055c2a4SAlex Bennée  * @features: original feature set
3012055c2a4SAlex Bennée  *
3022055c2a4SAlex Bennée  * This returns a set of features bits that is an intersection of what
3032055c2a4SAlex Bennée  * is supported by the vhost backend (hdev->features), the supported
3042055c2a4SAlex Bennée  * feature_bits and the requested feature set.
3052055c2a4SAlex Bennée  */
3069a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
3079a2ba823SCornelia Huck                             uint64_t features);
30881cf38f3SAlex Bennée 
30981cf38f3SAlex Bennée /**
31081cf38f3SAlex Bennée  * vhost_ack_features() - set vhost acked_features
31181cf38f3SAlex Bennée  * @hdev: common vhost_dev structure
31281cf38f3SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
31381cf38f3SAlex Bennée  * @features: requested feature set
31481cf38f3SAlex Bennée  *
31581cf38f3SAlex Bennée  * This sets the internal hdev->acked_features to the intersection of
31681cf38f3SAlex Bennée  * the backends advertised features and the supported feature_bits.
31781cf38f3SAlex Bennée  */
3182e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
3199a2ba823SCornelia Huck                         uint64_t features);
320cd89c065SDavid Hildenbrand unsigned int vhost_get_max_memslots(void);
3218c49951cSDavid Hildenbrand unsigned int vhost_get_free_memslots(void);
322950d94baSMarc-André Lureau 
323950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
324950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
325950d94baSMarc-André Lureau 
326ee071f67SViktor Prutyanov void vhost_toggle_device_iotlb(VirtIODevice *vdev);
327fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
3285ad204bfSXie Yongji 
329ff48b628SKangjie Xu int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev,
330ff48b628SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
331e1f101d9SKangjie Xu void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev,
332e1f101d9SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
333ff48b628SKangjie Xu 
3345ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
3355ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight);
3365ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3375ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3381b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
3395ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev,
3405ad204bfSXie Yongji                            struct vhost_inflight *inflight);
3415ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
3425ad204bfSXie Yongji                            struct vhost_inflight *inflight);
34374b5d2b5SCindy Lu bool vhost_dev_has_iommu(struct vhost_dev *dev);
344c0c4f147SStefan Hajnoczi 
345c0c4f147SStefan Hajnoczi #ifdef CONFIG_VHOST
346c0c4f147SStefan Hajnoczi int vhost_reset_device(struct vhost_dev *hdev);
347c0c4f147SStefan Hajnoczi #else
348c0c4f147SStefan Hajnoczi static inline int vhost_reset_device(struct vhost_dev *hdev)
349c0c4f147SStefan Hajnoczi {
350c0c4f147SStefan Hajnoczi     return -ENOSYS;
351c0c4f147SStefan Hajnoczi }
352c0c4f147SStefan Hajnoczi #endif /* CONFIG_VHOST */
353c0c4f147SStefan Hajnoczi 
354*cda83adcSHanna Czenczek /**
355*cda83adcSHanna Czenczek  * vhost_supports_device_state(): Checks whether the back-end supports
356*cda83adcSHanna Czenczek  * transferring internal device state for the purpose of migration.
357*cda83adcSHanna Czenczek  * Support for this feature is required for vhost_set_device_state_fd()
358*cda83adcSHanna Czenczek  * and vhost_check_device_state().
359*cda83adcSHanna Czenczek  *
360*cda83adcSHanna Czenczek  * @dev: The vhost device
361*cda83adcSHanna Czenczek  *
362*cda83adcSHanna Czenczek  * Returns true if the device supports these commands, and false if it
363*cda83adcSHanna Czenczek  * does not.
364*cda83adcSHanna Czenczek  */
365*cda83adcSHanna Czenczek bool vhost_supports_device_state(struct vhost_dev *dev);
366*cda83adcSHanna Czenczek 
367*cda83adcSHanna Czenczek /**
368*cda83adcSHanna Czenczek  * vhost_set_device_state_fd(): Begin transfer of internal state from/to
369*cda83adcSHanna Czenczek  * the back-end for the purpose of migration.  Data is to be transferred
370*cda83adcSHanna Czenczek  * over a pipe according to @direction and @phase.  The sending end must
371*cda83adcSHanna Czenczek  * only write to the pipe, and the receiving end must only read from it.
372*cda83adcSHanna Czenczek  * Once the sending end is done, it closes its FD.  The receiving end
373*cda83adcSHanna Czenczek  * must take this as the end-of-transfer signal and close its FD, too.
374*cda83adcSHanna Czenczek  *
375*cda83adcSHanna Czenczek  * @fd is the back-end's end of the pipe: The write FD for SAVE, and the
376*cda83adcSHanna Czenczek  * read FD for LOAD.  This function transfers ownership of @fd to the
377*cda83adcSHanna Czenczek  * back-end, i.e. closes it in the front-end.
378*cda83adcSHanna Czenczek  *
379*cda83adcSHanna Czenczek  * The back-end may optionally reply with an FD of its own, if this
380*cda83adcSHanna Czenczek  * improves efficiency on its end.  In this case, the returned FD is
381*cda83adcSHanna Czenczek  * stored in *reply_fd.  The back-end will discard the FD sent to it,
382*cda83adcSHanna Czenczek  * and the front-end must use *reply_fd for transferring state to/from
383*cda83adcSHanna Czenczek  * the back-end.
384*cda83adcSHanna Czenczek  *
385*cda83adcSHanna Czenczek  * @dev: The vhost device
386*cda83adcSHanna Czenczek  * @direction: The direction in which the state is to be transferred.
387*cda83adcSHanna Czenczek  *             For outgoing migrations, this is SAVE, and data is read
388*cda83adcSHanna Czenczek  *             from the back-end and stored by the front-end in the
389*cda83adcSHanna Czenczek  *             migration stream.
390*cda83adcSHanna Czenczek  *             For incoming migrations, this is LOAD, and data is read
391*cda83adcSHanna Czenczek  *             by the front-end from the migration stream and sent to
392*cda83adcSHanna Czenczek  *             the back-end to restore the saved state.
393*cda83adcSHanna Czenczek  * @phase: Which migration phase we are in.  Currently, there is only
394*cda83adcSHanna Czenczek  *         STOPPED (device and all vrings are stopped), in the future,
395*cda83adcSHanna Czenczek  *         more phases such as PRE_COPY or POST_COPY may be added.
396*cda83adcSHanna Czenczek  * @fd: Back-end's end of the pipe through which to transfer state; note
397*cda83adcSHanna Czenczek  *      that ownership is transferred to the back-end, so this function
398*cda83adcSHanna Czenczek  *      closes @fd in the front-end.
399*cda83adcSHanna Czenczek  * @reply_fd: If the back-end wishes to use a different pipe for state
400*cda83adcSHanna Czenczek  *            transfer, this will contain an FD for the front-end to
401*cda83adcSHanna Czenczek  *            use.  Otherwise, -1 is stored here.
402*cda83adcSHanna Czenczek  * @errp: Potential error description
403*cda83adcSHanna Czenczek  *
404*cda83adcSHanna Czenczek  * Returns 0 on success, and -errno on failure.
405*cda83adcSHanna Czenczek  */
406*cda83adcSHanna Czenczek int vhost_set_device_state_fd(struct vhost_dev *dev,
407*cda83adcSHanna Czenczek                               VhostDeviceStateDirection direction,
408*cda83adcSHanna Czenczek                               VhostDeviceStatePhase phase,
409*cda83adcSHanna Czenczek                               int fd,
410*cda83adcSHanna Czenczek                               int *reply_fd,
411*cda83adcSHanna Czenczek                               Error **errp);
412*cda83adcSHanna Czenczek 
413*cda83adcSHanna Czenczek /**
414*cda83adcSHanna Czenczek  * vhost_set_device_state_fd(): After transferring state from/to the
415*cda83adcSHanna Czenczek  * back-end via vhost_set_device_state_fd(), i.e. once the sending end
416*cda83adcSHanna Czenczek  * has closed the pipe, inquire the back-end to report any potential
417*cda83adcSHanna Czenczek  * errors that have occurred on its side.  This allows to sense errors
418*cda83adcSHanna Czenczek  * like:
419*cda83adcSHanna Czenczek  * - During outgoing migration, when the source side had already started
420*cda83adcSHanna Czenczek  *   to produce its state, something went wrong and it failed to finish
421*cda83adcSHanna Czenczek  * - During incoming migration, when the received state is somehow
422*cda83adcSHanna Czenczek  *   invalid and cannot be processed by the back-end
423*cda83adcSHanna Czenczek  *
424*cda83adcSHanna Czenczek  * @dev: The vhost device
425*cda83adcSHanna Czenczek  * @errp: Potential error description
426*cda83adcSHanna Czenczek  *
427*cda83adcSHanna Czenczek  * Returns 0 when the back-end reports successful state transfer and
428*cda83adcSHanna Czenczek  * processing, and -errno when an error occurred somewhere.
429*cda83adcSHanna Czenczek  */
430*cda83adcSHanna Czenczek int vhost_check_device_state(struct vhost_dev *dev, Error **errp);
431*cda83adcSHanna Czenczek 
432d5970055SMichael S. Tsirkin #endif
433