vhost.h 16 KB

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