1#ifndef VHOST_H
2#define VHOST_H
3
4#include "hw/virtio/vhost-backend.h"
5#include "hw/virtio/virtio.h"
6#include "exec/memory.h"
7
8
9
10struct vhost_inflight {
11 int fd;
12 void *addr;
13 uint64_t size;
14 uint64_t offset;
15 uint16_t queue_size;
16};
17
18struct vhost_virtqueue {
19 int kick;
20 int call;
21 void *desc;
22 void *avail;
23 void *used;
24 int num;
25 unsigned long long desc_phys;
26 unsigned desc_size;
27 unsigned long long avail_phys;
28 unsigned avail_size;
29 unsigned long long used_phys;
30 unsigned used_size;
31 EventNotifier masked_notifier;
32 EventNotifier error_notifier;
33 struct vhost_dev *dev;
34};
35
36typedef unsigned long vhost_log_chunk_t;
37#define VHOST_LOG_PAGE 0x1000
38#define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
39#define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
40#define VHOST_INVALID_FEATURE_BIT (0xff)
41
42struct vhost_log {
43 unsigned long long size;
44 int refcnt;
45 int fd;
46 vhost_log_chunk_t *log;
47};
48
49struct vhost_dev;
50struct vhost_iommu {
51 struct vhost_dev *hdev;
52 MemoryRegion *mr;
53 hwaddr iommu_offset;
54 IOMMUNotifier n;
55 QLIST_ENTRY(vhost_iommu) iommu_next;
56};
57
58typedef struct VhostDevConfigOps {
59
60
61 int (*vhost_dev_config_notifier)(struct vhost_dev *dev);
62} VhostDevConfigOps;
63
64struct vhost_memory;
65
66
67
68
69
70
71struct vhost_dev {
72 VirtIODevice *vdev;
73 MemoryListener memory_listener;
74 MemoryListener iommu_listener;
75 struct vhost_memory *mem;
76 int n_mem_sections;
77 MemoryRegionSection *mem_sections;
78 int n_tmp_sections;
79 MemoryRegionSection *tmp_sections;
80 struct vhost_virtqueue *vqs;
81 unsigned int nvqs;
82
83 int vq_index;
84
85 int vq_index_end;
86
87 int num_queues;
88 uint64_t features;
89 uint64_t acked_features;
90 uint64_t backend_features;
91 uint64_t protocol_features;
92 uint64_t max_queues;
93 uint64_t backend_cap;
94 bool started;
95 bool log_enabled;
96 uint64_t log_size;
97 Error *migration_blocker;
98 const VhostOps *vhost_ops;
99 void *opaque;
100 struct vhost_log *log;
101 QLIST_ENTRY(vhost_dev) entry;
102 QLIST_HEAD(, vhost_iommu) iommu_list;
103 IOMMUNotifier n;
104 const VhostDevConfigOps *config_ops;
105};
106
107extern const VhostOps kernel_ops;
108extern const VhostOps user_ops;
109extern const VhostOps vdpa_ops;
110
111struct vhost_net {
112 struct vhost_dev dev;
113 struct vhost_virtqueue vqs[2];
114 int backend;
115 NetClientState *nc;
116};
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
134 VhostBackendType backend_type,
135 uint32_t busyloop_timeout, Error **errp);
136
137
138
139
140
141void vhost_dev_cleanup(struct vhost_dev *hdev);
142
143
144
145
146
147
148
149
150
151
152
153
154int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
155
156
157
158
159
160
161
162
163void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
164
165
166
167
168
169
170
171
172
173
174
175int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev);
176
177
178
179
180
181
182
183
184
185
186void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev);
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
210 uint32_t config_len, Error **errp);
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data,
227 uint32_t offset, uint32_t size, uint32_t flags);
228
229
230
231
232
233
234
235
236
237void vhost_dev_set_config_notifier(struct vhost_dev *dev,
238 const VhostDevConfigOps *ops);
239
240
241
242
243
244bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n);
245
246
247
248void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
249 bool mask);
250
251
252
253
254
255
256
257
258
259
260
261uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
262 uint64_t features);
263
264
265
266
267
268
269
270
271
272
273void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
274 uint64_t features);
275bool vhost_has_free_slot(void);
276
277int vhost_net_set_backend(struct vhost_dev *hdev,
278 struct vhost_vring_file *file);
279
280int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write);
281
282void vhost_dev_reset_inflight(struct vhost_inflight *inflight);
283void vhost_dev_free_inflight(struct vhost_inflight *inflight);
284void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f);
285int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f);
286int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev);
287int vhost_dev_set_inflight(struct vhost_dev *dev,
288 struct vhost_inflight *inflight);
289int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
290 struct vhost_inflight *inflight);
291#endif
292