xref: /qemu/include/hw/virtio/vhost.h (revision 9600c98e12617649a7e09533beb722d2ffc71f44)
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 
11d5970055SMichael S. Tsirkin /* Generic structures common for any vhost based device. */
125ad204bfSXie Yongji 
135ad204bfSXie Yongji struct vhost_inflight {
145ad204bfSXie Yongji     int fd;
155ad204bfSXie Yongji     void *addr;
165ad204bfSXie Yongji     uint64_t size;
175ad204bfSXie Yongji     uint64_t offset;
185ad204bfSXie Yongji     uint16_t queue_size;
195ad204bfSXie Yongji };
205ad204bfSXie Yongji 
21d5970055SMichael S. Tsirkin struct vhost_virtqueue {
22d5970055SMichael S. Tsirkin     int kick;
23d5970055SMichael S. Tsirkin     int call;
24d5970055SMichael S. Tsirkin     void *desc;
25d5970055SMichael S. Tsirkin     void *avail;
26d5970055SMichael S. Tsirkin     void *used;
27d5970055SMichael S. Tsirkin     int num;
28f1f9e6c5SGreg Kurz     unsigned long long desc_phys;
29f1f9e6c5SGreg Kurz     unsigned desc_size;
30f1f9e6c5SGreg Kurz     unsigned long long avail_phys;
31f1f9e6c5SGreg Kurz     unsigned avail_size;
32d5970055SMichael S. Tsirkin     unsigned long long used_phys;
33d5970055SMichael S. Tsirkin     unsigned used_size;
34f56a1247SMichael S. Tsirkin     EventNotifier masked_notifier;
35ae50ae0bSKonstantin Khlebnikov     EventNotifier error_notifier;
36c471ad0eSJason Wang     struct vhost_dev *dev;
37d5970055SMichael S. Tsirkin };
38d5970055SMichael S. Tsirkin 
39d5970055SMichael S. Tsirkin typedef unsigned long vhost_log_chunk_t;
40d5970055SMichael S. Tsirkin #define VHOST_LOG_PAGE 0x1000
41d5970055SMichael S. Tsirkin #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
42d5970055SMichael S. Tsirkin #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
432e6d46d7SNikolay Nikolaev #define VHOST_INVALID_FEATURE_BIT   (0xff)
44d5970055SMichael S. Tsirkin 
45309750faSJason Wang struct vhost_log {
46309750faSJason Wang     unsigned long long size;
47309750faSJason Wang     int refcnt;
4815324404SMarc-André Lureau     int fd;
4915324404SMarc-André Lureau     vhost_log_chunk_t *log;
50309750faSJason Wang };
51309750faSJason Wang 
52375f74f4SJason Wang struct vhost_dev;
53375f74f4SJason Wang struct vhost_iommu {
54375f74f4SJason Wang     struct vhost_dev *hdev;
55375f74f4SJason Wang     MemoryRegion *mr;
56375f74f4SJason Wang     hwaddr iommu_offset;
57375f74f4SJason Wang     IOMMUNotifier n;
58375f74f4SJason Wang     QLIST_ENTRY(vhost_iommu) iommu_next;
59375f74f4SJason Wang };
60375f74f4SJason Wang 
614c3e257bSChangpeng Liu typedef struct VhostDevConfigOps {
624c3e257bSChangpeng Liu     /* Vhost device config space changed callback
634c3e257bSChangpeng Liu      */
644c3e257bSChangpeng Liu     int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
654c3e257bSChangpeng Liu } VhostDevConfigOps;
664c3e257bSChangpeng Liu 
67d5970055SMichael S. Tsirkin struct vhost_memory;
6827351992SAlex Bennée 
6927351992SAlex Bennée /**
7027351992SAlex Bennée  * struct vhost_dev - common vhost_dev structure
7127351992SAlex Bennée  * @vhost_ops: backend specific ops
7227351992SAlex Bennée  * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
7327351992SAlex Bennée  */
74d5970055SMichael S. Tsirkin struct vhost_dev {
75c471ad0eSJason Wang     VirtIODevice *vdev;
7604097f7cSAvi Kivity     MemoryListener memory_listener;
77375f74f4SJason Wang     MemoryListener iommu_listener;
78d5970055SMichael S. Tsirkin     struct vhost_memory *mem;
792817b260SAvi Kivity     int n_mem_sections;
802817b260SAvi Kivity     MemoryRegionSection *mem_sections;
81c44317efSDr. David Alan Gilbert     int n_tmp_sections;
82c44317efSDr. David Alan Gilbert     MemoryRegionSection *tmp_sections;
83d5970055SMichael S. Tsirkin     struct vhost_virtqueue *vqs;
845fc13603SJason Wang     unsigned int nvqs;
859be6e69fSGreg Kurz     /* the first virtqueue which would be used by this vhost dev */
86a9f98bb5SJason Wang     int vq_index;
87245cf2c2SEugenio Pérez     /* one past the last vq index for the virtio device (not vhost) */
88245cf2c2SEugenio Pérez     int vq_index_end;
89c90bd505SKevin Wolf     /* if non-zero, minimum required value for max_queues */
90c90bd505SKevin Wolf     int num_queues;
91*9600c98eSAlex Bennée     /**
92*9600c98eSAlex Bennée      * vhost feature handling requires matching the feature set
93*9600c98eSAlex Bennée      * offered by a backend which may be a subset of the total
94*9600c98eSAlex Bennée      * features eventually offered to the guest.
95*9600c98eSAlex Bennée      *
96*9600c98eSAlex Bennée      * @features: available features provided by the backend
97*9600c98eSAlex Bennée      * @acked_features: final negotiated features with front-end driver
98*9600c98eSAlex Bennée      *
99*9600c98eSAlex Bennée      * @backend_features: this is used in a couple of places to either
100*9600c98eSAlex Bennée      * store VHOST_USER_F_PROTOCOL_FEATURES to apply to
101*9600c98eSAlex Bennée      * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its
102*9600c98eSAlex Bennée      * future use should be discouraged and the variable retired as
103*9600c98eSAlex Bennée      * its easy to confuse with the VirtIO backend_features.
104*9600c98eSAlex Bennée      */
10521e70425SMarc-André Lureau     uint64_t features;
10621e70425SMarc-André Lureau     uint64_t acked_features;
10721e70425SMarc-André Lureau     uint64_t backend_features;
108*9600c98eSAlex Bennée 
109*9600c98eSAlex Bennée     /**
110*9600c98eSAlex Bennée      * @protocol_features: is the vhost-user only feature set by
111*9600c98eSAlex Bennée      * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only
112*9600c98eSAlex Bennée      * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered
113*9600c98eSAlex Bennée      * by the backend (see @features).
114*9600c98eSAlex Bennée      */
11521e70425SMarc-André Lureau     uint64_t protocol_features;
116*9600c98eSAlex Bennée 
11721e70425SMarc-André Lureau     uint64_t max_queues;
118b37556edSJason Wang     uint64_t backend_cap;
119b8f3e6a1SAlex Bennée     /* @started: is the vhost device started? */
120d5970055SMichael S. Tsirkin     bool started;
121d5970055SMichael S. Tsirkin     bool log_enabled;
12221e70425SMarc-André Lureau     uint64_t log_size;
1237145872eSMichael S. Tsirkin     Error *migration_blocker;
12424d1eb33SNikolay Nikolaev     const VhostOps *vhost_ops;
1251a1bfac9SNikolay Nikolaev     void *opaque;
126309750faSJason Wang     struct vhost_log *log;
1272ce68e4cSIgor Mammedov     QLIST_ENTRY(vhost_dev) entry;
128375f74f4SJason Wang     QLIST_HEAD(, vhost_iommu) iommu_list;
129c471ad0eSJason Wang     IOMMUNotifier n;
1304c3e257bSChangpeng Liu     const VhostDevConfigOps *config_ops;
131d5970055SMichael S. Tsirkin };
132d5970055SMichael S. Tsirkin 
1339b1d929aSTiberiu Georgescu extern const VhostOps kernel_ops;
1349b1d929aSTiberiu Georgescu extern const VhostOps user_ops;
1359b1d929aSTiberiu Georgescu extern const VhostOps vdpa_ops;
1369b1d929aSTiberiu Georgescu 
137108a6481SCindy Lu struct vhost_net {
138108a6481SCindy Lu     struct vhost_dev dev;
139108a6481SCindy Lu     struct vhost_virtqueue vqs[2];
140108a6481SCindy Lu     int backend;
141108a6481SCindy Lu     NetClientState *nc;
142108a6481SCindy Lu };
143108a6481SCindy Lu 
14427351992SAlex Bennée /**
14527351992SAlex Bennée  * vhost_dev_init() - initialise the vhost interface
14627351992SAlex Bennée  * @hdev: the common vhost_dev structure
14727351992SAlex Bennée  * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
14827351992SAlex Bennée  * @backend_type: type of backend
14927351992SAlex Bennée  * @busyloop_timeout: timeout for polling virtqueue
15027351992SAlex Bennée  * @errp: error handle
15127351992SAlex Bennée  *
15227351992SAlex Bennée  * The initialisation of the vhost device will trigger the
15327351992SAlex Bennée  * initialisation of the backend and potentially capability
15427351992SAlex Bennée  * negotiation of backend interface. Configuration of the VirtIO
15527351992SAlex Bennée  * itself won't happen until the interface is started.
15627351992SAlex Bennée  *
15727351992SAlex Bennée  * Return: 0 on success, non-zero on error while setting errp.
15827351992SAlex Bennée  */
15981647a65SNikolay Nikolaev int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
16069e87b32SJason Wang                    VhostBackendType backend_type,
161a6945f22SKevin Wolf                    uint32_t busyloop_timeout, Error **errp);
16227351992SAlex Bennée 
16327351992SAlex Bennée /**
16427351992SAlex Bennée  * vhost_dev_cleanup() - tear down and cleanup vhost interface
16527351992SAlex Bennée  * @hdev: the common vhost_dev structure
16627351992SAlex Bennée  */
167d5970055SMichael S. Tsirkin void vhost_dev_cleanup(struct vhost_dev *hdev);
16827351992SAlex Bennée 
16927351992SAlex Bennée /**
17027351992SAlex Bennée  * vhost_dev_enable_notifiers() - enable event notifiers
17127351992SAlex Bennée  * @hdev: common vhost_dev structure
17227351992SAlex Bennée  * @vdev: the VirtIODevice structure
17327351992SAlex Bennée  *
17427351992SAlex Bennée  * Enable notifications directly to the vhost device rather than being
17527351992SAlex Bennée  * triggered by QEMU itself. Notifications should be enabled before
17627351992SAlex Bennée  * the vhost device is started via @vhost_dev_start.
17727351992SAlex Bennée  *
17827351992SAlex Bennée  * Return: 0 on success, < 0 on error.
17927351992SAlex Bennée  */
180b0b3db79SMichael S. Tsirkin int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
18127351992SAlex Bennée 
18227351992SAlex Bennée /**
18327351992SAlex Bennée  * vhost_dev_disable_notifiers - disable event notifications
18427351992SAlex Bennée  * @hdev: common vhost_dev structure
18527351992SAlex Bennée  * @vdev: the VirtIODevice structure
18627351992SAlex Bennée  *
18727351992SAlex Bennée  * Disable direct notifications to vhost device.
18827351992SAlex Bennée  */
189b0b3db79SMichael S. Tsirkin void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
190d5970055SMichael S. Tsirkin 
19127351992SAlex Bennée /**
192b8f3e6a1SAlex Bennée  * vhost_dev_is_started() - report status of vhost device
193b8f3e6a1SAlex Bennée  * @hdev: common vhost_dev structure
194b8f3e6a1SAlex Bennée  *
195b8f3e6a1SAlex Bennée  * Return the started status of the vhost device
196b8f3e6a1SAlex Bennée  */
197b8f3e6a1SAlex Bennée static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
198b8f3e6a1SAlex Bennée {
199b8f3e6a1SAlex Bennée     return hdev->started;
200b8f3e6a1SAlex Bennée }
201b8f3e6a1SAlex Bennée 
202b8f3e6a1SAlex Bennée /**
20327351992SAlex Bennée  * vhost_dev_start() - start the vhost device
20427351992SAlex Bennée  * @hdev: common vhost_dev structure
20527351992SAlex Bennée  * @vdev: the VirtIODevice structure
2064daa5054SStefano Garzarella  * @vrings: true to have vrings enabled in this call
20727351992SAlex Bennée  *
20827351992SAlex Bennée  * Starts the vhost device. From this point VirtIO feature negotiation
20927351992SAlex Bennée  * can start and the device can start processing VirtIO transactions.
21027351992SAlex Bennée  *
21127351992SAlex Bennée  * Return: 0 on success, < 0 on error.
21227351992SAlex Bennée  */
2134daa5054SStefano Garzarella int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
21427351992SAlex Bennée 
21527351992SAlex Bennée /**
21627351992SAlex Bennée  * vhost_dev_stop() - stop the vhost device
21727351992SAlex Bennée  * @hdev: common vhost_dev structure
21827351992SAlex Bennée  * @vdev: the VirtIODevice structure
2194daa5054SStefano Garzarella  * @vrings: true to have vrings disabled in this call
22027351992SAlex Bennée  *
22127351992SAlex Bennée  * Stop the vhost device. After the device is stopped the notifiers
22227351992SAlex Bennée  * can be disabled (@vhost_dev_disable_notifiers) and the device can
22327351992SAlex Bennée  * be torn down (@vhost_dev_cleanup).
22427351992SAlex Bennée  */
2254daa5054SStefano Garzarella void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
22627351992SAlex Bennée 
22727351992SAlex Bennée /**
22827351992SAlex Bennée  * DOC: vhost device configuration handling
22927351992SAlex Bennée  *
23027351992SAlex Bennée  * The VirtIO device configuration space is used for rarely changing
23127351992SAlex Bennée  * or initialisation time parameters. The configuration can be updated
23227351992SAlex Bennée  * by either the guest driver or the device itself. If the device can
23327351992SAlex Bennée  * change the configuration over time the vhost handler should
23427351992SAlex Bennée  * register a @VhostDevConfigOps structure with
23527351992SAlex Bennée  * @vhost_dev_set_config_notifier so the guest can be notified. Some
23627351992SAlex Bennée  * devices register a handler anyway and will signal an error if an
23727351992SAlex Bennée  * unexpected config change happens.
23827351992SAlex Bennée  */
23927351992SAlex Bennée 
24027351992SAlex Bennée /**
24127351992SAlex Bennée  * vhost_dev_get_config() - fetch device configuration
24227351992SAlex Bennée  * @hdev: common vhost_dev_structure
24327351992SAlex Bennée  * @config: pointer to device appropriate config structure
24427351992SAlex Bennée  * @config_len: size of device appropriate config structure
24527351992SAlex Bennée  *
24627351992SAlex Bennée  * Return: 0 on success, < 0 on error while setting errp
24727351992SAlex Bennée  */
24827351992SAlex Bennée int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
24927351992SAlex Bennée                          uint32_t config_len, Error **errp);
25027351992SAlex Bennée 
25127351992SAlex Bennée /**
25227351992SAlex Bennée  * vhost_dev_set_config() - set device configuration
25327351992SAlex Bennée  * @hdev: common vhost_dev_structure
25427351992SAlex Bennée  * @data: pointer to data to set
25527351992SAlex Bennée  * @offset: offset into configuration space
25627351992SAlex Bennée  * @size: length of set
25727351992SAlex Bennée  * @flags: @VhostSetConfigType flags
25827351992SAlex Bennée  *
25927351992SAlex Bennée  * By use of @offset/@size a subset of the configuration space can be
26027351992SAlex Bennée  * written to. The @flags are used to indicate if it is a normal
26127351992SAlex Bennée  * transaction or related to migration.
26227351992SAlex Bennée  *
26327351992SAlex Bennée  * Return: 0 on success, non-zero on error
26427351992SAlex Bennée  */
26527351992SAlex Bennée int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
26627351992SAlex Bennée                          uint32_t offset, uint32_t size, uint32_t flags);
26727351992SAlex Bennée 
26827351992SAlex Bennée /**
26927351992SAlex Bennée  * vhost_dev_set_config_notifier() - register VhostDevConfigOps
27027351992SAlex Bennée  * @hdev: common vhost_dev_structure
27127351992SAlex Bennée  * @ops: notifier ops
27227351992SAlex Bennée  *
27327351992SAlex Bennée  * If the device is expected to change configuration a notifier can be
27427351992SAlex Bennée  * setup to handle the case.
27527351992SAlex Bennée  */
27627351992SAlex Bennée void vhost_dev_set_config_notifier(struct vhost_dev *dev,
27727351992SAlex Bennée                                    const VhostDevConfigOps *ops);
27827351992SAlex Bennée 
27927351992SAlex Bennée 
280f56a1247SMichael S. Tsirkin /* Test and clear masked event pending status.
281f56a1247SMichael S. Tsirkin  * Should be called after unmask to avoid losing events.
282f56a1247SMichael S. Tsirkin  */
283f56a1247SMichael S. Tsirkin bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
284f56a1247SMichael S. Tsirkin 
285f56a1247SMichael S. Tsirkin /* Mask/unmask events from this vq.
286f56a1247SMichael S. Tsirkin  */
287f56a1247SMichael S. Tsirkin void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
288f56a1247SMichael S. Tsirkin                           bool mask);
2892055c2a4SAlex Bennée 
2902055c2a4SAlex Bennée /**
2912055c2a4SAlex Bennée  * vhost_get_features() - return a sanitised set of feature bits
2922055c2a4SAlex Bennée  * @hdev: common vhost_dev structure
2932055c2a4SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
2942055c2a4SAlex Bennée  * @features: original feature set
2952055c2a4SAlex Bennée  *
2962055c2a4SAlex Bennée  * This returns a set of features bits that is an intersection of what
2972055c2a4SAlex Bennée  * is supported by the vhost backend (hdev->features), the supported
2982055c2a4SAlex Bennée  * feature_bits and the requested feature set.
2992055c2a4SAlex Bennée  */
3009a2ba823SCornelia Huck uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
3019a2ba823SCornelia Huck                             uint64_t features);
30281cf38f3SAlex Bennée 
30381cf38f3SAlex Bennée /**
30481cf38f3SAlex Bennée  * vhost_ack_features() - set vhost acked_features
30581cf38f3SAlex Bennée  * @hdev: common vhost_dev structure
30681cf38f3SAlex Bennée  * @feature_bits: pointer to terminated table of feature bits
30781cf38f3SAlex Bennée  * @features: requested feature set
30881cf38f3SAlex Bennée  *
30981cf38f3SAlex Bennée  * This sets the internal hdev->acked_features to the intersection of
31081cf38f3SAlex Bennée  * the backends advertised features and the supported feature_bits.
31181cf38f3SAlex Bennée  */
3122e6d46d7SNikolay Nikolaev void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
3139a2ba823SCornelia Huck                         uint64_t features);
3142ce68e4cSIgor Mammedov bool vhost_has_free_slot(void);
315950d94baSMarc-André Lureau 
316950d94baSMarc-André Lureau int vhost_net_set_backend(struct vhost_dev *hdev,
317950d94baSMarc-André Lureau                           struct vhost_vring_file *file);
318950d94baSMarc-André Lureau 
319fc58bd0dSMaxime Coquelin int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
3205ad204bfSXie Yongji 
321ff48b628SKangjie Xu int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev,
322ff48b628SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
323e1f101d9SKangjie Xu void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev,
324e1f101d9SKangjie Xu                           struct vhost_virtqueue *vq, unsigned idx);
325ff48b628SKangjie Xu 
3265ad204bfSXie Yongji void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
3275ad204bfSXie Yongji void vhost_dev_free_inflight(struct vhost_inflight *inflight);
3285ad204bfSXie Yongji void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3295ad204bfSXie Yongji int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
3301b0063b3SJin Yu int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
3315ad204bfSXie Yongji int vhost_dev_set_inflight(struct vhost_dev *dev,
3325ad204bfSXie Yongji                            struct vhost_inflight *inflight);
3335ad204bfSXie Yongji int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
3345ad204bfSXie Yongji                            struct vhost_inflight *inflight);
335d5970055SMichael S. Tsirkin #endif
336