]>
Commit | Line | Data |
---|---|---|
d5970055 MT |
1 | #ifndef VHOST_H |
2 | #define VHOST_H | |
3 | ||
24d1eb33 | 4 | #include "hw/virtio/vhost-backend.h" |
0d09e41a | 5 | #include "hw/virtio/virtio.h" |
022c62cb | 6 | #include "exec/memory.h" |
d5970055 | 7 | |
f3034ad7 LV |
8 | #define VHOST_F_DEVICE_IOTLB 63 |
9 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 | |
10 | ||
4dfcc09f LF |
11 | #define VU_REALIZE_CONN_RETRIES 3 |
12 | ||
d5970055 | 13 | /* Generic structures common for any vhost based device. */ |
5ad204bf XY |
14 | |
15 | struct vhost_inflight { | |
16 | int fd; | |
17 | void *addr; | |
18 | uint64_t size; | |
19 | uint64_t offset; | |
20 | uint16_t queue_size; | |
21 | }; | |
22 | ||
d5970055 MT |
23 | struct vhost_virtqueue { |
24 | int kick; | |
25 | int call; | |
26 | void *desc; | |
27 | void *avail; | |
28 | void *used; | |
29 | int num; | |
f1f9e6c5 GK |
30 | unsigned long long desc_phys; |
31 | unsigned desc_size; | |
32 | unsigned long long avail_phys; | |
33 | unsigned avail_size; | |
d5970055 MT |
34 | unsigned long long used_phys; |
35 | unsigned used_size; | |
f56a1247 | 36 | EventNotifier masked_notifier; |
ae50ae0b | 37 | EventNotifier error_notifier; |
f9a09ca3 | 38 | EventNotifier masked_config_notifier; |
c471ad0e | 39 | struct vhost_dev *dev; |
d5970055 MT |
40 | }; |
41 | ||
42 | typedef unsigned long vhost_log_chunk_t; | |
43 | #define VHOST_LOG_PAGE 0x1000 | |
44 | #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t)) | |
45 | #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS) | |
2e6d46d7 | 46 | #define VHOST_INVALID_FEATURE_BIT (0xff) |
f9a09ca3 | 47 | #define VHOST_QUEUE_NUM_CONFIG_INR 0 |
d5970055 | 48 | |
309750fa JW |
49 | struct vhost_log { |
50 | unsigned long long size; | |
51 | int refcnt; | |
15324404 MAL |
52 | int fd; |
53 | vhost_log_chunk_t *log; | |
309750fa JW |
54 | }; |
55 | ||
375f74f4 JW |
56 | struct vhost_dev; |
57 | struct vhost_iommu { | |
58 | struct vhost_dev *hdev; | |
59 | MemoryRegion *mr; | |
60 | hwaddr iommu_offset; | |
61 | IOMMUNotifier n; | |
62 | QLIST_ENTRY(vhost_iommu) iommu_next; | |
63 | }; | |
64 | ||
4c3e257b CL |
65 | typedef struct VhostDevConfigOps { |
66 | /* Vhost device config space changed callback | |
67 | */ | |
68 | int (*vhost_dev_config_notifier)(struct vhost_dev *dev); | |
69 | } VhostDevConfigOps; | |
70 | ||
d5970055 | 71 | struct vhost_memory; |
27351992 AB |
72 | |
73 | /** | |
74 | * struct vhost_dev - common vhost_dev structure | |
75 | * @vhost_ops: backend specific ops | |
76 | * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier) | |
77 | */ | |
d5970055 | 78 | struct vhost_dev { |
c471ad0e | 79 | VirtIODevice *vdev; |
04097f7c | 80 | MemoryListener memory_listener; |
375f74f4 | 81 | MemoryListener iommu_listener; |
d5970055 | 82 | struct vhost_memory *mem; |
2817b260 AK |
83 | int n_mem_sections; |
84 | MemoryRegionSection *mem_sections; | |
c44317ef DDAG |
85 | int n_tmp_sections; |
86 | MemoryRegionSection *tmp_sections; | |
d5970055 | 87 | struct vhost_virtqueue *vqs; |
5fc13603 | 88 | unsigned int nvqs; |
9be6e69f | 89 | /* the first virtqueue which would be used by this vhost dev */ |
a9f98bb5 | 90 | int vq_index; |
245cf2c2 EP |
91 | /* one past the last vq index for the virtio device (not vhost) */ |
92 | int vq_index_end; | |
c90bd505 KW |
93 | /* if non-zero, minimum required value for max_queues */ |
94 | int num_queues; | |
9600c98e AB |
95 | /** |
96 | * vhost feature handling requires matching the feature set | |
97 | * offered by a backend which may be a subset of the total | |
98 | * features eventually offered to the guest. | |
99 | * | |
100 | * @features: available features provided by the backend | |
101 | * @acked_features: final negotiated features with front-end driver | |
102 | * | |
103 | * @backend_features: this is used in a couple of places to either | |
104 | * store VHOST_USER_F_PROTOCOL_FEATURES to apply to | |
105 | * VHOST_USER_SET_FEATURES or VHOST_NET_F_VIRTIO_NET_HDR. Its | |
106 | * future use should be discouraged and the variable retired as | |
107 | * its easy to confuse with the VirtIO backend_features. | |
108 | */ | |
21e70425 MAL |
109 | uint64_t features; |
110 | uint64_t acked_features; | |
111 | uint64_t backend_features; | |
9600c98e AB |
112 | |
113 | /** | |
114 | * @protocol_features: is the vhost-user only feature set by | |
115 | * VHOST_USER_SET_PROTOCOL_FEATURES. Protocol features are only | |
116 | * negotiated if VHOST_USER_F_PROTOCOL_FEATURES has been offered | |
117 | * by the backend (see @features). | |
118 | */ | |
21e70425 | 119 | uint64_t protocol_features; |
9600c98e | 120 | |
21e70425 | 121 | uint64_t max_queues; |
b37556ed | 122 | uint64_t backend_cap; |
b8f3e6a1 | 123 | /* @started: is the vhost device started? */ |
d5970055 MT |
124 | bool started; |
125 | bool log_enabled; | |
21e70425 | 126 | uint64_t log_size; |
7145872e | 127 | Error *migration_blocker; |
24d1eb33 | 128 | const VhostOps *vhost_ops; |
1a1bfac9 | 129 | void *opaque; |
309750fa | 130 | struct vhost_log *log; |
2ce68e4c | 131 | QLIST_ENTRY(vhost_dev) entry; |
375f74f4 | 132 | QLIST_HEAD(, vhost_iommu) iommu_list; |
c471ad0e | 133 | IOMMUNotifier n; |
4c3e257b | 134 | const VhostDevConfigOps *config_ops; |
d5970055 MT |
135 | }; |
136 | ||
9b1d929a TG |
137 | extern const VhostOps kernel_ops; |
138 | extern const VhostOps user_ops; | |
139 | extern const VhostOps vdpa_ops; | |
140 | ||
108a6481 CL |
141 | struct vhost_net { |
142 | struct vhost_dev dev; | |
143 | struct vhost_virtqueue vqs[2]; | |
144 | int backend; | |
145 | NetClientState *nc; | |
146 | }; | |
147 | ||
27351992 AB |
148 | /** |
149 | * vhost_dev_init() - initialise the vhost interface | |
150 | * @hdev: the common vhost_dev structure | |
151 | * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa) | |
152 | * @backend_type: type of backend | |
153 | * @busyloop_timeout: timeout for polling virtqueue | |
154 | * @errp: error handle | |
155 | * | |
156 | * The initialisation of the vhost device will trigger the | |
157 | * initialisation of the backend and potentially capability | |
158 | * negotiation of backend interface. Configuration of the VirtIO | |
159 | * itself won't happen until the interface is started. | |
160 | * | |
161 | * Return: 0 on success, non-zero on error while setting errp. | |
162 | */ | |
81647a65 | 163 | int vhost_dev_init(struct vhost_dev *hdev, void *opaque, |
69e87b32 | 164 | VhostBackendType backend_type, |
a6945f22 | 165 | uint32_t busyloop_timeout, Error **errp); |
27351992 AB |
166 | |
167 | /** | |
168 | * vhost_dev_cleanup() - tear down and cleanup vhost interface | |
169 | * @hdev: the common vhost_dev structure | |
170 | */ | |
d5970055 | 171 | void vhost_dev_cleanup(struct vhost_dev *hdev); |
27351992 AB |
172 | |
173 | /** | |
174 | * vhost_dev_enable_notifiers() - enable event notifiers | |
175 | * @hdev: common vhost_dev structure | |
176 | * @vdev: the VirtIODevice structure | |
177 | * | |
178 | * Enable notifications directly to the vhost device rather than being | |
179 | * triggered by QEMU itself. Notifications should be enabled before | |
180 | * the vhost device is started via @vhost_dev_start. | |
181 | * | |
182 | * Return: 0 on success, < 0 on error. | |
183 | */ | |
b0b3db79 | 184 | int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); |
27351992 AB |
185 | |
186 | /** | |
187 | * vhost_dev_disable_notifiers - disable event notifications | |
188 | * @hdev: common vhost_dev structure | |
189 | * @vdev: the VirtIODevice structure | |
190 | * | |
191 | * Disable direct notifications to vhost device. | |
192 | */ | |
b0b3db79 | 193 | void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev); |
f9a09ca3 CL |
194 | bool vhost_config_pending(struct vhost_dev *hdev); |
195 | void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask); | |
d5970055 | 196 | |
b8f3e6a1 AB |
197 | /** |
198 | * vhost_dev_is_started() - report status of vhost device | |
199 | * @hdev: common vhost_dev structure | |
200 | * | |
201 | * Return the started status of the vhost device | |
202 | */ | |
203 | static inline bool vhost_dev_is_started(struct vhost_dev *hdev) | |
204 | { | |
205 | return hdev->started; | |
206 | } | |
207 | ||
27351992 AB |
208 | /** |
209 | * vhost_dev_start() - start the vhost device | |
210 | * @hdev: common vhost_dev structure | |
211 | * @vdev: the VirtIODevice structure | |
4daa5054 | 212 | * @vrings: true to have vrings enabled in this call |
27351992 AB |
213 | * |
214 | * Starts the vhost device. From this point VirtIO feature negotiation | |
215 | * can start and the device can start processing VirtIO transactions. | |
216 | * | |
217 | * Return: 0 on success, < 0 on error. | |
218 | */ | |
4daa5054 | 219 | int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); |
27351992 AB |
220 | |
221 | /** | |
222 | * vhost_dev_stop() - stop the vhost device | |
223 | * @hdev: common vhost_dev structure | |
224 | * @vdev: the VirtIODevice structure | |
4daa5054 | 225 | * @vrings: true to have vrings disabled in this call |
27351992 AB |
226 | * |
227 | * Stop the vhost device. After the device is stopped the notifiers | |
228 | * can be disabled (@vhost_dev_disable_notifiers) and the device can | |
229 | * be torn down (@vhost_dev_cleanup). | |
230 | */ | |
4daa5054 | 231 | void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); |
27351992 AB |
232 | |
233 | /** | |
234 | * DOC: vhost device configuration handling | |
235 | * | |
236 | * The VirtIO device configuration space is used for rarely changing | |
237 | * or initialisation time parameters. The configuration can be updated | |
238 | * by either the guest driver or the device itself. If the device can | |
239 | * change the configuration over time the vhost handler should | |
240 | * register a @VhostDevConfigOps structure with | |
241 | * @vhost_dev_set_config_notifier so the guest can be notified. Some | |
242 | * devices register a handler anyway and will signal an error if an | |
243 | * unexpected config change happens. | |
244 | */ | |
245 | ||
246 | /** | |
247 | * vhost_dev_get_config() - fetch device configuration | |
248 | * @hdev: common vhost_dev_structure | |
249 | * @config: pointer to device appropriate config structure | |
250 | * @config_len: size of device appropriate config structure | |
251 | * | |
252 | * Return: 0 on success, < 0 on error while setting errp | |
253 | */ | |
254 | int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, | |
255 | uint32_t config_len, Error **errp); | |
256 | ||
257 | /** | |
258 | * vhost_dev_set_config() - set device configuration | |
259 | * @hdev: common vhost_dev_structure | |
260 | * @data: pointer to data to set | |
261 | * @offset: offset into configuration space | |
262 | * @size: length of set | |
263 | * @flags: @VhostSetConfigType flags | |
264 | * | |
265 | * By use of @offset/@size a subset of the configuration space can be | |
266 | * written to. The @flags are used to indicate if it is a normal | |
267 | * transaction or related to migration. | |
268 | * | |
269 | * Return: 0 on success, non-zero on error | |
270 | */ | |
271 | int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data, | |
272 | uint32_t offset, uint32_t size, uint32_t flags); | |
273 | ||
274 | /** | |
275 | * vhost_dev_set_config_notifier() - register VhostDevConfigOps | |
276 | * @hdev: common vhost_dev_structure | |
277 | * @ops: notifier ops | |
278 | * | |
279 | * If the device is expected to change configuration a notifier can be | |
280 | * setup to handle the case. | |
281 | */ | |
282 | void vhost_dev_set_config_notifier(struct vhost_dev *dev, | |
283 | const VhostDevConfigOps *ops); | |
284 | ||
285 | ||
f56a1247 MT |
286 | /* Test and clear masked event pending status. |
287 | * Should be called after unmask to avoid losing events. | |
288 | */ | |
289 | bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n); | |
290 | ||
291 | /* Mask/unmask events from this vq. | |
292 | */ | |
293 | void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, | |
294 | bool mask); | |
2055c2a4 AB |
295 | |
296 | /** | |
297 | * vhost_get_features() - return a sanitised set of feature bits | |
298 | * @hdev: common vhost_dev structure | |
299 | * @feature_bits: pointer to terminated table of feature bits | |
300 | * @features: original feature set | |
301 | * | |
302 | * This returns a set of features bits that is an intersection of what | |
303 | * is supported by the vhost backend (hdev->features), the supported | |
304 | * feature_bits and the requested feature set. | |
305 | */ | |
9a2ba823 CH |
306 | uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, |
307 | uint64_t features); | |
81cf38f3 AB |
308 | |
309 | /** | |
310 | * vhost_ack_features() - set vhost acked_features | |
311 | * @hdev: common vhost_dev structure | |
312 | * @feature_bits: pointer to terminated table of feature bits | |
313 | * @features: requested feature set | |
314 | * | |
315 | * This sets the internal hdev->acked_features to the intersection of | |
316 | * the backends advertised features and the supported feature_bits. | |
317 | */ | |
2e6d46d7 | 318 | void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, |
9a2ba823 | 319 | uint64_t features); |
cd89c065 | 320 | unsigned int vhost_get_max_memslots(void); |
8c49951c | 321 | unsigned int vhost_get_free_memslots(void); |
950d94ba MAL |
322 | |
323 | int vhost_net_set_backend(struct vhost_dev *hdev, | |
324 | struct vhost_vring_file *file); | |
325 | ||
ee071f67 | 326 | void vhost_toggle_device_iotlb(VirtIODevice *vdev); |
fc58bd0d | 327 | int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); |
5ad204bf | 328 | |
ff48b628 KX |
329 | int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, |
330 | struct vhost_virtqueue *vq, unsigned idx); | |
e1f101d9 KX |
331 | void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev, |
332 | struct vhost_virtqueue *vq, unsigned idx); | |
ff48b628 | 333 | |
5ad204bf XY |
334 | void vhost_dev_reset_inflight(struct vhost_inflight *inflight); |
335 | void vhost_dev_free_inflight(struct vhost_inflight *inflight); | |
336 | void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f); | |
337 | int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f); | |
1b0063b3 | 338 | int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev); |
5ad204bf XY |
339 | int vhost_dev_set_inflight(struct vhost_dev *dev, |
340 | struct vhost_inflight *inflight); | |
341 | int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, | |
342 | struct vhost_inflight *inflight); | |
74b5d2b5 | 343 | bool vhost_dev_has_iommu(struct vhost_dev *dev); |
c0c4f147 SH |
344 | |
345 | #ifdef CONFIG_VHOST | |
346 | int vhost_reset_device(struct vhost_dev *hdev); | |
347 | #else | |
348 | static inline int vhost_reset_device(struct vhost_dev *hdev) | |
349 | { | |
350 | return -ENOSYS; | |
351 | } | |
352 | #endif /* CONFIG_VHOST */ | |
353 | ||
cda83adc HC |
354 | /** |
355 | * vhost_supports_device_state(): Checks whether the back-end supports | |
356 | * transferring internal device state for the purpose of migration. | |
357 | * Support for this feature is required for vhost_set_device_state_fd() | |
358 | * and vhost_check_device_state(). | |
359 | * | |
360 | * @dev: The vhost device | |
361 | * | |
362 | * Returns true if the device supports these commands, and false if it | |
363 | * does not. | |
364 | */ | |
365 | bool vhost_supports_device_state(struct vhost_dev *dev); | |
366 | ||
367 | /** | |
368 | * vhost_set_device_state_fd(): Begin transfer of internal state from/to | |
369 | * the back-end for the purpose of migration. Data is to be transferred | |
370 | * over a pipe according to @direction and @phase. The sending end must | |
371 | * only write to the pipe, and the receiving end must only read from it. | |
372 | * Once the sending end is done, it closes its FD. The receiving end | |
373 | * must take this as the end-of-transfer signal and close its FD, too. | |
374 | * | |
375 | * @fd is the back-end's end of the pipe: The write FD for SAVE, and the | |
376 | * read FD for LOAD. This function transfers ownership of @fd to the | |
377 | * back-end, i.e. closes it in the front-end. | |
378 | * | |
379 | * The back-end may optionally reply with an FD of its own, if this | |
380 | * improves efficiency on its end. In this case, the returned FD is | |
381 | * stored in *reply_fd. The back-end will discard the FD sent to it, | |
382 | * and the front-end must use *reply_fd for transferring state to/from | |
383 | * the back-end. | |
384 | * | |
385 | * @dev: The vhost device | |
386 | * @direction: The direction in which the state is to be transferred. | |
387 | * For outgoing migrations, this is SAVE, and data is read | |
388 | * from the back-end and stored by the front-end in the | |
389 | * migration stream. | |
390 | * For incoming migrations, this is LOAD, and data is read | |
391 | * by the front-end from the migration stream and sent to | |
392 | * the back-end to restore the saved state. | |
393 | * @phase: Which migration phase we are in. Currently, there is only | |
394 | * STOPPED (device and all vrings are stopped), in the future, | |
395 | * more phases such as PRE_COPY or POST_COPY may be added. | |
396 | * @fd: Back-end's end of the pipe through which to transfer state; note | |
397 | * that ownership is transferred to the back-end, so this function | |
398 | * closes @fd in the front-end. | |
399 | * @reply_fd: If the back-end wishes to use a different pipe for state | |
400 | * transfer, this will contain an FD for the front-end to | |
401 | * use. Otherwise, -1 is stored here. | |
402 | * @errp: Potential error description | |
403 | * | |
404 | * Returns 0 on success, and -errno on failure. | |
405 | */ | |
406 | int vhost_set_device_state_fd(struct vhost_dev *dev, | |
407 | VhostDeviceStateDirection direction, | |
408 | VhostDeviceStatePhase phase, | |
409 | int fd, | |
410 | int *reply_fd, | |
411 | Error **errp); | |
412 | ||
413 | /** | |
414 | * vhost_set_device_state_fd(): After transferring state from/to the | |
415 | * back-end via vhost_set_device_state_fd(), i.e. once the sending end | |
416 | * has closed the pipe, inquire the back-end to report any potential | |
417 | * errors that have occurred on its side. This allows to sense errors | |
418 | * like: | |
419 | * - During outgoing migration, when the source side had already started | |
420 | * to produce its state, something went wrong and it failed to finish | |
421 | * - During incoming migration, when the received state is somehow | |
422 | * invalid and cannot be processed by the back-end | |
423 | * | |
424 | * @dev: The vhost device | |
425 | * @errp: Potential error description | |
426 | * | |
427 | * Returns 0 when the back-end reports successful state transfer and | |
428 | * processing, and -errno when an error occurred somewhere. | |
429 | */ | |
430 | int vhost_check_device_state(struct vhost_dev *dev, Error **errp); | |
431 | ||
4a00d5d7 HC |
432 | /** |
433 | * vhost_save_backend_state(): High-level function to receive a vhost | |
434 | * back-end's state, and save it in @f. Uses | |
435 | * `vhost_set_device_state_fd()` to get the data from the back-end, and | |
436 | * stores it in consecutive chunks that are each prefixed by their | |
437 | * respective length (be32). The end is marked by a 0-length chunk. | |
438 | * | |
439 | * Must only be called while the device and all its vrings are stopped | |
440 | * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`). | |
441 | * | |
442 | * @dev: The vhost device from which to save the state | |
443 | * @f: Migration stream in which to save the state | |
444 | * @errp: Potential error message | |
445 | * | |
446 | * Returns 0 on success, and -errno otherwise. | |
447 | */ | |
448 | int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp); | |
449 | ||
450 | /** | |
451 | * vhost_load_backend_state(): High-level function to load a vhost | |
452 | * back-end's state from @f, and send it over to the back-end. Reads | |
453 | * the data from @f in the format used by `vhost_save_state()`, and uses | |
454 | * `vhost_set_device_state_fd()` to transfer it to the back-end. | |
455 | * | |
456 | * Must only be called while the device and all its vrings are stopped | |
457 | * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`). | |
458 | * | |
801faee4 | 459 | * @dev: The vhost device to which to send the state |
4a00d5d7 HC |
460 | * @f: Migration stream from which to load the state |
461 | * @errp: Potential error message | |
462 | * | |
463 | * Returns 0 on success, and -errno otherwise. | |
464 | */ | |
465 | int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp); | |
466 | ||
d5970055 | 467 | #endif |