master
h 594 lines 20.2 KB
Raw
1 #ifndef VHOST_H
2 #define VHOST_H
3
4 #include "net/vhost_net.h"
5 #include "hw/virtio/vhost-backend.h"
6 #include "hw/virtio/virtio.h"
7 #include "system/memory.h"
8
9 #define VHOST_F_DEVICE_IOTLB 63
10 #define VHOST_USER_F_PROTOCOL_FEATURES 30
11
12 #define VU_REALIZE_CONN_RETRIES 3
13
14 /* Generic structures common for any vhost based device. */
15
16 struct vhost_inflight {
17 int fd;
18 void *addr;
19 uint64_t size;
20 uint64_t offset;
21 uint16_t queue_size;
22 };
23
24 struct vhost_virtqueue {
25 int kick;
26 int call;
27 void *desc_user;
28 void *avail_user;
29 void *used_user;
30 int num;
31 unsigned long long desc_phys;
32 unsigned desc_size;
33 unsigned long long avail_phys;
34 unsigned avail_size;
35 unsigned long long used_phys;
36 unsigned used_size;
37 EventNotifier masked_notifier;
38 EventNotifier error_notifier;
39 EventNotifier masked_config_notifier;
40 struct vhost_dev *dev;
41 };
42
43 typedef unsigned long vhost_log_chunk_t;
44 #define VHOST_LOG_PAGE 0x1000
45 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
46 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
47 #define VHOST_INVALID_FEATURE_BIT (0xff)
48 #define VHOST_QUEUE_NUM_CONFIG_INR 0
49
50 struct vhost_log {
51 unsigned long long size;
52 int refcnt;
53 int fd;
54 vhost_log_chunk_t *log;
55 };
56
57 struct vhost_dev;
58 struct vhost_iommu {
59 struct vhost_dev *hdev;
60 MemoryRegion *mr;
61 hwaddr iommu_offset;
62 IOMMUNotifier n;
63 QLIST_ENTRY(vhost_iommu) iommu_next;
64 };
65
66 typedef struct VhostDevConfigOps {
67 /* Vhost device config space changed callback
68 */
69 int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
70 } VhostDevConfigOps;
71
72 struct vhost_memory;
73
74 /**
75 * struct vhost_dev - common vhost_dev structure
76 * @vhost_ops: backend specific ops
77 * @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
78 */
79 struct vhost_dev {
80 VirtIODevice *vdev;
81 MemoryListener memory_listener;
82 MemoryListener iommu_listener;
83 struct vhost_memory *mem;
84 int n_mem_sections;
85 MemoryRegionSection *mem_sections;
86 int n_tmp_sections;
87 MemoryRegionSection *tmp_sections;
88 struct vhost_virtqueue *vqs;
89 unsigned int nvqs;
90 /* the first virtqueue which would be used by this vhost dev */
91 int vq_index;
92 /* one past the last vq index for the virtio device (not vhost) */
93 int vq_index_end;
94 /* if non-zero, minimum required value for max_queues */
95 int num_queues;
96 /**
97 * vhost feature handling requires matching the feature set
98 * offered by a backend which may be a subset of the total
99 * features eventually offered to the guest.
100 *
101 * @_features: available features provided by the backend, private,
102 * direct access only in vhost.h/vhost.c
103 * @acked_features: final negotiated features with front-end driver
104 */
105 VIRTIO_DECLARE_FEATURES(_features);
106 VIRTIO_DECLARE_FEATURES(acked_features);
107
108 uint64_t max_queues;
109 uint64_t backend_cap;
110 /* @started: is the vhost device started? */
111 bool started;
112 bool log_enabled;
113 uint64_t log_size;
114 Error *migration_blocker;
115 const VhostOps *vhost_ops;
116 void *opaque;
117 struct vhost_log *log;
118 QLIST_ENTRY(vhost_dev) entry;
119 QLIST_ENTRY(vhost_dev) logdev_entry;
120 QLIST_HEAD(, vhost_iommu) iommu_list;
121 IOMMUNotifier n;
122 const VhostDevConfigOps *config_ops;
123 };
124
125 extern const VhostOps kernel_ops;
126 extern const VhostOps user_ops;
127 extern const VhostOps vdpa_ops;
128
129 struct vhost_net {
130 struct vhost_dev dev;
131 struct vhost_virtqueue vqs[2];
132 int backend;
133 const int *feature_bits;
134 int max_tx_queue_size;
135 SaveAcketFeatures *save_acked_features;
136 bool is_vhost_user;
137 NetClientState *nc;
138 };
139
140 /**
141 * vhost_dev_init() - initialise the vhost interface
142 * @hdev: the common vhost_dev structure
143 * @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
144 * @backend_type: type of backend
145 * @busyloop_timeout: timeout for polling virtqueue
146 * @errp: error handle
147 *
148 * The initialisation of the vhost device will trigger the
149 * initialisation of the backend and potentially capability
150 * negotiation of backend interface. Configuration of the VirtIO
151 * itself won't happen until the interface is started.
152 *
153 * Return: 0 on success, non-zero on error while setting errp.
154 */
155 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
156 VhostBackendType backend_type,
157 uint32_t busyloop_timeout, Error **errp);
158
159 /**
160 * vhost_dev_cleanup() - tear down and cleanup vhost interface
161 * @hdev: the common vhost_dev structure
162 */
163 void vhost_dev_cleanup(struct vhost_dev *hdev);
164
165 void vhost_dev_disable_notifiers_nvqs(struct vhost_dev *hdev,
166 VirtIODevice *vdev,
167 unsigned int nvqs);
168
169 /**
170 * vhost_dev_enable_notifiers() - enable event notifiers
171 * @hdev: common vhost_dev structure
172 * @vdev: the VirtIODevice structure
173 *
174 * Enable notifications directly to the vhost device rather than being
175 * triggered by QEMU itself. Notifications should be enabled before
176 * the vhost device is started via @vhost_dev_start.
177 *
178 * Return: 0 on success, < 0 on error.
179 */
180 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
181
182 /**
183 * vhost_dev_disable_notifiers - disable event notifications
184 * @hdev: common vhost_dev structure
185 * @vdev: the VirtIODevice structure
186 *
187 * Disable direct notifications to vhost device.
188 */
189 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
190 bool vhost_config_pending(struct vhost_dev *hdev);
191 void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
192
193 /**
194 * vhost_dev_is_started() - report status of vhost device
195 * @hdev: common vhost_dev structure
196 *
197 * Return the started status of the vhost device
198 */
199 static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
200 {
201 return hdev->started;
202 }
203
204 static inline int vhost_dev_set_vring_enable(struct vhost_dev *hdev, int enable)
205 {
206 if (!hdev->vhost_ops->vhost_set_vring_enable) {
207 return 0;
208 }
209
210 return hdev->vhost_ops->vhost_set_vring_enable(hdev, enable);
211 }
212
213 /**
214 * vhost_dev_start() - start the vhost device
215 * @hdev: common vhost_dev structure
216 * @vdev: the VirtIODevice structure
217 * @vrings: true to have vrings enabled in this call
218 *
219 * Starts the vhost device. From this point VirtIO feature negotiation
220 * can start and the device can start processing VirtIO transactions.
221 *
222 * Return: 0 on success, < 0 on error.
223 */
224 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
225
226 /**
227 * vhost_dev_stop() - stop the vhost device
228 * @hdev: common vhost_dev structure
229 * @vdev: the VirtIODevice structure
230 * @vrings: true to have vrings disabled in this call
231 *
232 * Stop the vhost device. After the device is stopped the notifiers
233 * can be disabled (@vhost_dev_disable_notifiers) and the device can
234 * be torn down (@vhost_dev_cleanup).
235 *
236 * Return: 0 on success, != 0 on error when stopping dev.
237 */
238 int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings);
239
240 /**
241 * vhost_dev_force_stop() - force stop the vhost device
242 * @hdev: common vhost_dev structure
243 * @vdev: the VirtIODevice structure
244 * @vrings: true to have vrings disabled in this call
245 *
246 * Force stop the vhost device. After the device is stopped the notifiers
247 * can be disabled (@vhost_dev_disable_notifiers) and the device can
248 * be torn down (@vhost_dev_cleanup). Unlike @vhost_dev_stop, this doesn't
249 * attempt to flush in-flight backend requests by skipping GET_VRING_BASE
250 * entirely.
251 */
252 int vhost_dev_force_stop(struct vhost_dev *hdev, VirtIODevice *vdev,
253 bool vrings);
254
255 /**
256 * DOC: vhost device configuration handling
257 *
258 * The VirtIO device configuration space is used for rarely changing
259 * or initialisation time parameters. The configuration can be updated
260 * by either the guest driver or the device itself. If the device can
261 * change the configuration over time the vhost handler should
262 * register a @VhostDevConfigOps structure with
263 * @vhost_dev_set_config_notifier so the guest can be notified. Some
264 * devices register a handler anyway and will signal an error if an
265 * unexpected config change happens.
266 */
267
268 /**
269 * vhost_dev_get_config() - fetch device configuration
270 * @hdev: common vhost_dev_structure
271 * @config: pointer to device appropriate config structure
272 * @config_len: size of device appropriate config structure
273 *
274 * Return: 0 on success, < 0 on error while setting errp
275 */
276 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
277 uint32_t config_len, Error **errp);
278
279 /**
280 * vhost_dev_set_config() - set device configuration
281 * @hdev: common vhost_dev_structure
282 * @data: pointer to data to set
283 * @offset: offset into configuration space
284 * @size: length of set
285 * @flags: @VhostSetConfigType flags
286 *
287 * By use of @offset/@size a subset of the configuration space can be
288 * written to. The @flags are used to indicate if it is a normal
289 * transaction or related to migration.
290 *
291 * Return: 0 on success, non-zero on error
292 */
293 int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
294 uint32_t offset, uint32_t size, uint32_t flags);
295
296 /**
297 * vhost_dev_set_config_notifier() - register VhostDevConfigOps
298 * @hdev: common vhost_dev_structure
299 * @ops: notifier ops
300 *
301 * If the device is expected to change configuration a notifier can be
302 * setup to handle the case.
303 */
304 void vhost_dev_set_config_notifier(struct vhost_dev *dev,
305 const VhostDevConfigOps *ops);
306
307
308 /* Test and clear masked event pending status.
309 * Should be called after unmask to avoid losing events.
310 */
311 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
312
313 /* Mask/unmask events from this vq.
314 */
315 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
316 bool mask);
317
318 /**
319 * vhost_get_features_ex() - sanitize the extended features set
320 * @hdev: common vhost_dev structure
321 * @feature_bits: pointer to terminated table of feature bits
322 * @features: original features set, filtered out on return
323 *
324 * This is the extended variant of vhost_get_features(), supporting the
325 * the extended features set. Filter it with the intersection of what is
326 * supported by the vhost backend (hdev->features) and the supported
327 * feature_bits.
328 */
329 void vhost_get_features_ex(struct vhost_dev *hdev,
330 const int *feature_bits,
331 uint64_t *features);
332 /**
333 * vhost_get_features() - return a sanitised set of feature bits
334 * @hdev: common vhost_dev structure
335 * @feature_bits: pointer to terminated table of feature bits
336 * @features: original feature set
337 *
338 * This returns a set of features bits that is an intersection of what
339 * is supported by the vhost backend (hdev->features), the supported
340 * feature_bits and the requested feature set.
341 */
342 static inline uint64_t vhost_get_features(struct vhost_dev *hdev,
343 const int *feature_bits,
344 uint64_t features)
345 {
346 uint64_t features_ex[VIRTIO_FEATURES_NU64S];
347
348 virtio_features_from_u64(features_ex, features);
349 vhost_get_features_ex(hdev, feature_bits, features_ex);
350 return features_ex[0];
351 }
352
353 /**
354 * vhost_ack_features_ex() - set vhost full set of acked_features
355 * @hdev: common vhost_dev structure
356 * @feature_bits: pointer to terminated table of feature bits
357 * @features: requested feature set
358 *
359 * This sets the internal hdev->acked_features to the intersection of
360 * the backends advertised features and the supported feature_bits.
361 */
362 void vhost_ack_features_ex(struct vhost_dev *hdev, const int *feature_bits,
363 const uint64_t *features);
364
365 /**
366 * vhost_ack_features() - set vhost acked_features
367 * @hdev: common vhost_dev structure
368 * @feature_bits: pointer to terminated table of feature bits
369 * @features: requested feature set
370 *
371 * This sets the internal hdev->acked_features to the intersection of
372 * the backends advertised features and the supported feature_bits.
373 */
374 static inline void vhost_ack_features(struct vhost_dev *hdev,
375 const int *feature_bits,
376 uint64_t features)
377 {
378 uint64_t features_ex[VIRTIO_FEATURES_NU64S];
379
380 virtio_features_from_u64(features_ex, features);
381 vhost_ack_features_ex(hdev, feature_bits, features_ex);
382 }
383
384 unsigned int vhost_get_max_memslots(void);
385 unsigned int vhost_get_free_memslots(void);
386
387 int vhost_net_set_backend(struct vhost_dev *hdev,
388 struct vhost_vring_file *file);
389
390 void vhost_toggle_device_iotlb(VirtIODevice *vdev);
391 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
392
393 int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev,
394 struct vhost_virtqueue *vq, unsigned idx);
395 int vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev,
396 struct vhost_virtqueue *vq, unsigned idx);
397
398 void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
399 void vhost_dev_free_inflight(struct vhost_inflight *inflight);
400 int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
401 int vhost_dev_set_inflight(struct vhost_dev *dev,
402 struct vhost_inflight *inflight);
403 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
404 struct vhost_inflight *inflight);
405 bool vhost_dev_has_iommu(struct vhost_dev *dev);
406 int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg);
407
408
409 static inline bool vhost_dev_has_feature(struct vhost_dev *dev,
410 uint64_t feature)
411 {
412 return virtio_has_feature(dev->_features, feature);
413 }
414
415 static inline bool vhost_dev_has_feature_ex(struct vhost_dev *dev,
416 uint64_t feature)
417 {
418 return virtio_has_feature_ex(dev->_features_ex, feature);
419 }
420
421 static inline uint64_t vhost_dev_features(struct vhost_dev *dev)
422 {
423 return dev->_features;
424 }
425
426 static inline const uint64_t *vhost_dev_features_ex(struct vhost_dev *dev)
427 {
428 return dev->_features_ex;
429 }
430
431 static inline void vhost_dev_clear_feature(struct vhost_dev *dev,
432 uint64_t feature)
433 {
434 virtio_clear_feature(&dev->_features, feature);
435 }
436
437 static inline void vhost_dev_clear_feature_ex(struct vhost_dev *dev,
438 uint64_t feature)
439 {
440 virtio_clear_feature_ex(dev->_features_ex, feature);
441 }
442
443 #ifdef CONFIG_VHOST
444 int vhost_reset_device(struct vhost_dev *hdev);
445 #else
446 static inline int vhost_reset_device(struct vhost_dev *hdev)
447 {
448 return -ENOSYS;
449 }
450 #endif /* CONFIG_VHOST */
451
452 /**
453 * vhost_supports_device_state(): Checks whether the back-end supports
454 * transferring internal device state for the purpose of migration.
455 * Support for this feature is required for vhost_set_device_state_fd()
456 * and vhost_check_device_state().
457 *
458 * @dev: The vhost device
459 *
460 * Returns true if the device supports these commands, and false if it
461 * does not.
462 */
463 #ifdef CONFIG_VHOST
464 bool vhost_supports_device_state(struct vhost_dev *dev);
465 #else
466 static inline bool vhost_supports_device_state(struct vhost_dev *dev)
467 {
468 return false;
469 }
470 #endif
471
472 /**
473 * vhost_set_device_state_fd(): Begin transfer of internal state from/to
474 * the back-end for the purpose of migration. Data is to be transferred
475 * over a pipe according to @direction and @phase. The sending end must
476 * only write to the pipe, and the receiving end must only read from it.
477 * Once the sending end is done, it closes its FD. The receiving end
478 * must take this as the end-of-transfer signal and close its FD, too.
479 *
480 * @fd is the back-end's end of the pipe: The write FD for SAVE, and the
481 * read FD for LOAD. This function transfers ownership of @fd to the
482 * back-end, i.e. closes it in the front-end.
483 *
484 * The back-end may optionally reply with an FD of its own, if this
485 * improves efficiency on its end. In this case, the returned FD is
486 * stored in *reply_fd. The back-end will discard the FD sent to it,
487 * and the front-end must use *reply_fd for transferring state to/from
488 * the back-end.
489 *
490 * @dev: The vhost device
491 * @direction: The direction in which the state is to be transferred.
492 * For outgoing migrations, this is SAVE, and data is read
493 * from the back-end and stored by the front-end in the
494 * migration stream.
495 * For incoming migrations, this is LOAD, and data is read
496 * by the front-end from the migration stream and sent to
497 * the back-end to restore the saved state.
498 * @phase: Which migration phase we are in. Currently, there is only
499 * STOPPED (device and all vrings are stopped), in the future,
500 * more phases such as PRE_COPY or POST_COPY may be added.
501 * @fd: Back-end's end of the pipe through which to transfer state; note
502 * that ownership is transferred to the back-end, so this function
503 * closes @fd in the front-end.
504 * @reply_fd: If the back-end wishes to use a different pipe for state
505 * transfer, this will contain an FD for the front-end to
506 * use. Otherwise, -1 is stored here.
507 * @errp: Potential error description
508 *
509 * Returns 0 on success, and -errno on failure.
510 */
511 int vhost_set_device_state_fd(struct vhost_dev *dev,
512 VhostDeviceStateDirection direction,
513 VhostDeviceStatePhase phase,
514 int fd,
515 int *reply_fd,
516 Error **errp);
517
518 /**
519 * vhost_set_device_state_fd(): After transferring state from/to the
520 * back-end via vhost_set_device_state_fd(), i.e. once the sending end
521 * has closed the pipe, inquire the back-end to report any potential
522 * errors that have occurred on its side. This allows to sense errors
523 * like:
524 * - During outgoing migration, when the source side had already started
525 * to produce its state, something went wrong and it failed to finish
526 * - During incoming migration, when the received state is somehow
527 * invalid and cannot be processed by the back-end
528 *
529 * @dev: The vhost device
530 * @errp: Potential error description
531 *
532 * Returns 0 when the back-end reports successful state transfer and
533 * processing, and -errno when an error occurred somewhere.
534 */
535 int vhost_check_device_state(struct vhost_dev *dev, Error **errp);
536
537 /**
538 * vhost_save_backend_state(): High-level function to receive a vhost
539 * back-end's state, and save it in @f. Uses
540 * `vhost_set_device_state_fd()` to get the data from the back-end, and
541 * stores it in consecutive chunks that are each prefixed by their
542 * respective length (be32). The end is marked by a 0-length chunk.
543 *
544 * Must only be called while the device and all its vrings are stopped
545 * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`).
546 *
547 * @dev: The vhost device from which to save the state
548 * @f: Migration stream in which to save the state
549 * @errp: Potential error message
550 *
551 * Returns 0 on success, and -errno otherwise.
552 */
553 #ifdef CONFIG_VHOST
554 int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp);
555 #else
556 static inline int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f,
557 Error **errp)
558 {
559 return -ENOSYS;
560 }
561 #endif
562
563 /**
564 * vhost_load_backend_state(): High-level function to load a vhost
565 * back-end's state from @f, and send it over to the back-end. Reads
566 * the data from @f in the format used by `vhost_save_state()`, and uses
567 * `vhost_set_device_state_fd()` to transfer it to the back-end.
568 *
569 * Must only be called while the device and all its vrings are stopped
570 * (`VHOST_TRANSFER_STATE_PHASE_STOPPED`).
571 *
572 * @dev: The vhost device to which to send the state
573 * @f: Migration stream from which to load the state
574 * @errp: Potential error message
575 *
576 * Returns 0 on success, and -errno otherwise.
577 */
578 #ifdef CONFIG_VHOST
579 int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp);
580 #else
581 static inline int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f,
582 Error **errp)
583 {
584 return -ENOSYS;
585 }
586 #endif
587
588 extern const VMStateDescription vmstate_vhost_inflight_region;
589 #define VMSTATE_VHOST_INFLIGHT_REGION(_field, _state) \
590 VMSTATE_STRUCT_POINTER(_field, _state, \
591 vmstate_vhost_inflight_region, \
592 struct vhost_inflight)
593
594 #endif