2010-03-17 14:08:17 +03:00
|
|
|
#ifndef VHOST_H
|
|
|
|
#define VHOST_H
|
|
|
|
|
2014-05-27 16:05:35 +04:00
|
|
|
#include "hw/virtio/vhost-backend.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/virtio/virtio.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "exec/memory.h"
|
2010-03-17 14:08:17 +03:00
|
|
|
|
|
|
|
/* Generic structures common for any vhost based device. */
|
2019-02-28 11:53:49 +03:00
|
|
|
|
|
|
|
struct vhost_inflight {
|
|
|
|
int fd;
|
|
|
|
void *addr;
|
|
|
|
uint64_t size;
|
|
|
|
uint64_t offset;
|
|
|
|
uint16_t queue_size;
|
|
|
|
};
|
|
|
|
|
2010-03-17 14:08:17 +03:00
|
|
|
struct vhost_virtqueue {
|
|
|
|
int kick;
|
|
|
|
int call;
|
|
|
|
void *desc;
|
|
|
|
void *avail;
|
|
|
|
void *used;
|
|
|
|
int num;
|
2016-11-04 11:39:15 +03:00
|
|
|
unsigned long long desc_phys;
|
|
|
|
unsigned desc_size;
|
|
|
|
unsigned long long avail_phys;
|
|
|
|
unsigned avail_size;
|
2010-03-17 14:08:17 +03:00
|
|
|
unsigned long long used_phys;
|
|
|
|
unsigned used_size;
|
2012-12-24 19:37:01 +04:00
|
|
|
EventNotifier masked_notifier;
|
2022-06-23 19:13:25 +03:00
|
|
|
EventNotifier error_notifier;
|
2017-01-11 07:32:12 +03:00
|
|
|
struct vhost_dev *dev;
|
2010-03-17 14:08:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef unsigned long vhost_log_chunk_t;
|
|
|
|
#define VHOST_LOG_PAGE 0x1000
|
|
|
|
#define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
|
|
|
|
#define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
|
2014-05-27 16:04:42 +04:00
|
|
|
#define VHOST_INVALID_FEATURE_BIT (0xff)
|
2010-03-17 14:08:17 +03:00
|
|
|
|
2015-06-04 12:28:46 +03:00
|
|
|
struct vhost_log {
|
|
|
|
unsigned long long size;
|
|
|
|
int refcnt;
|
2015-10-09 18:17:25 +03:00
|
|
|
int fd;
|
|
|
|
vhost_log_chunk_t *log;
|
2015-06-04 12:28:46 +03:00
|
|
|
};
|
|
|
|
|
2017-03-29 07:10:04 +03:00
|
|
|
struct vhost_dev;
|
|
|
|
struct vhost_iommu {
|
|
|
|
struct vhost_dev *hdev;
|
|
|
|
MemoryRegion *mr;
|
|
|
|
hwaddr iommu_offset;
|
|
|
|
IOMMUNotifier n;
|
|
|
|
QLIST_ENTRY(vhost_iommu) iommu_next;
|
|
|
|
};
|
|
|
|
|
2018-01-04 04:53:31 +03:00
|
|
|
typedef struct VhostDevConfigOps {
|
|
|
|
/* Vhost device config space changed callback
|
|
|
|
*/
|
|
|
|
int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
|
|
|
|
} VhostDevConfigOps;
|
|
|
|
|
2010-03-17 14:08:17 +03:00
|
|
|
struct vhost_memory;
|
2022-03-21 18:30:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* struct vhost_dev - common vhost_dev structure
|
|
|
|
* @vhost_ops: backend specific ops
|
|
|
|
* @config_ops: ops for config changes (see @vhost_dev_set_config_notifier)
|
|
|
|
*/
|
2010-03-17 14:08:17 +03:00
|
|
|
struct vhost_dev {
|
2017-01-11 07:32:12 +03:00
|
|
|
VirtIODevice *vdev;
|
2011-12-18 16:06:05 +04:00
|
|
|
MemoryListener memory_listener;
|
2017-03-29 07:10:04 +03:00
|
|
|
MemoryListener iommu_listener;
|
2010-03-17 14:08:17 +03:00
|
|
|
struct vhost_memory *mem;
|
2011-12-19 15:18:13 +04:00
|
|
|
int n_mem_sections;
|
|
|
|
MemoryRegionSection *mem_sections;
|
2018-01-19 13:39:18 +03:00
|
|
|
int n_tmp_sections;
|
|
|
|
MemoryRegionSection *tmp_sections;
|
2010-03-17 14:08:17 +03:00
|
|
|
struct vhost_virtqueue *vqs;
|
2021-09-03 12:10:14 +03:00
|
|
|
unsigned int nvqs;
|
2015-03-26 14:10:29 +03:00
|
|
|
/* the first virtqueue which would be used by this vhost dev */
|
2013-01-30 15:12:35 +04:00
|
|
|
int vq_index;
|
2021-11-04 11:56:24 +03:00
|
|
|
/* one past the last vq index for the virtio device (not vhost) */
|
|
|
|
int vq_index_end;
|
2021-04-29 20:13:16 +03:00
|
|
|
/* if non-zero, minimum required value for max_queues */
|
|
|
|
int num_queues;
|
2015-10-09 18:17:28 +03:00
|
|
|
uint64_t features;
|
2022-08-02 12:49:54 +03:00
|
|
|
/** @acked_features: final set of negotiated features */
|
2015-10-09 18:17:28 +03:00
|
|
|
uint64_t acked_features;
|
2022-08-02 12:49:54 +03:00
|
|
|
/** @backend_features: backend specific feature bits */
|
2015-10-09 18:17:28 +03:00
|
|
|
uint64_t backend_features;
|
2022-08-02 12:49:54 +03:00
|
|
|
/** @protocol_features: final negotiated protocol features */
|
2015-10-09 18:17:28 +03:00
|
|
|
uint64_t protocol_features;
|
|
|
|
uint64_t max_queues;
|
2020-09-07 13:49:02 +03:00
|
|
|
uint64_t backend_cap;
|
2022-08-02 12:49:59 +03:00
|
|
|
/* @started: is the vhost device started? */
|
2010-03-17 14:08:17 +03:00
|
|
|
bool started;
|
|
|
|
bool log_enabled;
|
2015-10-09 18:17:28 +03:00
|
|
|
uint64_t log_size;
|
2014-06-18 18:20:42 +04:00
|
|
|
Error *migration_blocker;
|
2014-05-27 16:05:35 +04:00
|
|
|
const VhostOps *vhost_ops;
|
2014-05-27 16:05:49 +04:00
|
|
|
void *opaque;
|
2015-06-04 12:28:46 +03:00
|
|
|
struct vhost_log *log;
|
2015-10-06 11:37:27 +03:00
|
|
|
QLIST_ENTRY(vhost_dev) entry;
|
2017-03-29 07:10:04 +03:00
|
|
|
QLIST_HEAD(, vhost_iommu) iommu_list;
|
2017-01-11 07:32:12 +03:00
|
|
|
IOMMUNotifier n;
|
2018-01-04 04:53:31 +03:00
|
|
|
const VhostDevConfigOps *config_ops;
|
2010-03-17 14:08:17 +03:00
|
|
|
};
|
|
|
|
|
2021-08-09 16:40:15 +03:00
|
|
|
extern const VhostOps kernel_ops;
|
|
|
|
extern const VhostOps user_ops;
|
|
|
|
extern const VhostOps vdpa_ops;
|
|
|
|
|
2020-07-01 17:55:37 +03:00
|
|
|
struct vhost_net {
|
|
|
|
struct vhost_dev dev;
|
|
|
|
struct vhost_virtqueue vqs[2];
|
|
|
|
int backend;
|
|
|
|
NetClientState *nc;
|
|
|
|
};
|
|
|
|
|
2022-03-21 18:30:34 +03:00
|
|
|
/**
|
|
|
|
* vhost_dev_init() - initialise the vhost interface
|
|
|
|
* @hdev: the common vhost_dev structure
|
|
|
|
* @opaque: opaque ptr passed to backend (vhost/vhost-user/vdpa)
|
|
|
|
* @backend_type: type of backend
|
|
|
|
* @busyloop_timeout: timeout for polling virtqueue
|
|
|
|
* @errp: error handle
|
|
|
|
*
|
|
|
|
* The initialisation of the vhost device will trigger the
|
|
|
|
* initialisation of the backend and potentially capability
|
|
|
|
* negotiation of backend interface. Configuration of the VirtIO
|
|
|
|
* itself won't happen until the interface is started.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, non-zero on error while setting errp.
|
|
|
|
*/
|
2014-05-27 16:05:22 +04:00
|
|
|
int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
|
2016-07-06 04:57:55 +03:00
|
|
|
VhostBackendType backend_type,
|
2021-06-09 18:46:52 +03:00
|
|
|
uint32_t busyloop_timeout, Error **errp);
|
2022-03-21 18:30:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_cleanup() - tear down and cleanup vhost interface
|
|
|
|
* @hdev: the common vhost_dev structure
|
|
|
|
*/
|
2010-03-17 14:08:17 +03:00
|
|
|
void vhost_dev_cleanup(struct vhost_dev *hdev);
|
2022-03-21 18:30:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_enable_notifiers() - enable event notifiers
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @vdev: the VirtIODevice structure
|
|
|
|
*
|
|
|
|
* Enable notifications directly to the vhost device rather than being
|
|
|
|
* triggered by QEMU itself. Notifications should be enabled before
|
|
|
|
* the vhost device is started via @vhost_dev_start.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, < 0 on error.
|
|
|
|
*/
|
2011-08-11 11:21:18 +04:00
|
|
|
int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
|
2022-03-21 18:30:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_disable_notifiers - disable event notifications
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @vdev: the VirtIODevice structure
|
|
|
|
*
|
|
|
|
* Disable direct notifications to vhost device.
|
|
|
|
*/
|
2011-08-11 11:21:18 +04:00
|
|
|
void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
|
2010-03-17 14:08:17 +03:00
|
|
|
|
2022-08-02 12:49:59 +03:00
|
|
|
/**
|
|
|
|
* vhost_dev_is_started() - report status of vhost device
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
*
|
|
|
|
* Return the started status of the vhost device
|
|
|
|
*/
|
|
|
|
static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
|
|
|
|
{
|
|
|
|
return hdev->started;
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:30:34 +03:00
|
|
|
/**
|
|
|
|
* vhost_dev_start() - start the vhost device
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @vdev: the VirtIODevice structure
|
|
|
|
*
|
|
|
|
* Starts the vhost device. From this point VirtIO feature negotiation
|
|
|
|
* can start and the device can start processing VirtIO transactions.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, < 0 on error.
|
|
|
|
*/
|
|
|
|
int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_stop() - stop the vhost device
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @vdev: the VirtIODevice structure
|
|
|
|
*
|
|
|
|
* Stop the vhost device. After the device is stopped the notifiers
|
|
|
|
* can be disabled (@vhost_dev_disable_notifiers) and the device can
|
|
|
|
* be torn down (@vhost_dev_cleanup).
|
|
|
|
*/
|
|
|
|
void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOC: vhost device configuration handling
|
|
|
|
*
|
|
|
|
* The VirtIO device configuration space is used for rarely changing
|
|
|
|
* or initialisation time parameters. The configuration can be updated
|
|
|
|
* by either the guest driver or the device itself. If the device can
|
|
|
|
* change the configuration over time the vhost handler should
|
|
|
|
* register a @VhostDevConfigOps structure with
|
|
|
|
* @vhost_dev_set_config_notifier so the guest can be notified. Some
|
|
|
|
* devices register a handler anyway and will signal an error if an
|
|
|
|
* unexpected config change happens.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_get_config() - fetch device configuration
|
|
|
|
* @hdev: common vhost_dev_structure
|
|
|
|
* @config: pointer to device appropriate config structure
|
|
|
|
* @config_len: size of device appropriate config structure
|
|
|
|
*
|
|
|
|
* Return: 0 on success, < 0 on error while setting errp
|
|
|
|
*/
|
|
|
|
int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
|
|
|
|
uint32_t config_len, Error **errp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_set_config() - set device configuration
|
|
|
|
* @hdev: common vhost_dev_structure
|
|
|
|
* @data: pointer to data to set
|
|
|
|
* @offset: offset into configuration space
|
|
|
|
* @size: length of set
|
|
|
|
* @flags: @VhostSetConfigType flags
|
|
|
|
*
|
|
|
|
* By use of @offset/@size a subset of the configuration space can be
|
|
|
|
* written to. The @flags are used to indicate if it is a normal
|
|
|
|
* transaction or related to migration.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, non-zero on error
|
|
|
|
*/
|
|
|
|
int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
|
|
|
|
uint32_t offset, uint32_t size, uint32_t flags);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_dev_set_config_notifier() - register VhostDevConfigOps
|
|
|
|
* @hdev: common vhost_dev_structure
|
|
|
|
* @ops: notifier ops
|
|
|
|
*
|
|
|
|
* If the device is expected to change configuration a notifier can be
|
|
|
|
* setup to handle the case.
|
|
|
|
*/
|
|
|
|
void vhost_dev_set_config_notifier(struct vhost_dev *dev,
|
|
|
|
const VhostDevConfigOps *ops);
|
|
|
|
|
|
|
|
|
2012-12-24 19:37:01 +04:00
|
|
|
/* Test and clear masked event pending status.
|
|
|
|
* Should be called after unmask to avoid losing events.
|
|
|
|
*/
|
|
|
|
bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
|
|
|
|
|
|
|
|
/* Mask/unmask events from this vq.
|
|
|
|
*/
|
|
|
|
void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
|
|
|
|
bool mask);
|
2022-05-24 18:40:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_get_features() - return a sanitised set of feature bits
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @feature_bits: pointer to terminated table of feature bits
|
|
|
|
* @features: original feature set
|
|
|
|
*
|
|
|
|
* This returns a set of features bits that is an intersection of what
|
|
|
|
* is supported by the vhost backend (hdev->features), the supported
|
|
|
|
* feature_bits and the requested feature set.
|
|
|
|
*/
|
2015-06-04 13:34:20 +03:00
|
|
|
uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
|
|
|
|
uint64_t features);
|
2022-05-24 18:40:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vhost_ack_features() - set vhost acked_features
|
|
|
|
* @hdev: common vhost_dev structure
|
|
|
|
* @feature_bits: pointer to terminated table of feature bits
|
|
|
|
* @features: requested feature set
|
|
|
|
*
|
|
|
|
* This sets the internal hdev->acked_features to the intersection of
|
|
|
|
* the backends advertised features and the supported feature_bits.
|
|
|
|
*/
|
2014-05-27 16:04:42 +04:00
|
|
|
void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
|
2015-06-04 13:34:20 +03:00
|
|
|
uint64_t features);
|
2015-10-06 11:37:27 +03:00
|
|
|
bool vhost_has_free_slot(void);
|
2016-07-27 00:15:25 +03:00
|
|
|
|
|
|
|
int vhost_net_set_backend(struct vhost_dev *hdev,
|
|
|
|
struct vhost_vring_file *file);
|
|
|
|
|
2017-06-02 13:18:27 +03:00
|
|
|
int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
|
2019-02-28 11:53:49 +03:00
|
|
|
|
|
|
|
void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
|
|
|
|
void vhost_dev_free_inflight(struct vhost_inflight *inflight);
|
|
|
|
void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
|
|
|
|
int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
|
2020-11-03 15:36:17 +03:00
|
|
|
int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
|
2019-02-28 11:53:49 +03:00
|
|
|
int vhost_dev_set_inflight(struct vhost_dev *dev,
|
|
|
|
struct vhost_inflight *inflight);
|
|
|
|
int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
|
|
|
|
struct vhost_inflight *inflight);
|
2010-03-17 14:08:17 +03:00
|
|
|
#endif
|