]>
Commit | Line | Data |
---|---|---|
5f6f6664 NN |
1 | /* |
2 | * vhost-user | |
3 | * | |
4 | * Copyright (c) 2013 Virtual Open Systems Sarl. | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | * | |
9 | */ | |
10 | ||
9b8bfe21 | 11 | #include "qemu/osdep.h" |
da34e65c | 12 | #include "qapi/error.h" |
5f6f6664 | 13 | #include "hw/virtio/vhost.h" |
4d0cf552 | 14 | #include "hw/virtio/vhost-user.h" |
5f6f6664 | 15 | #include "hw/virtio/vhost-backend.h" |
44866521 | 16 | #include "hw/virtio/virtio.h" |
3e866365 | 17 | #include "hw/virtio/virtio-net.h" |
4d43a603 | 18 | #include "chardev/char-fe.h" |
57dc0217 | 19 | #include "io/channel-socket.h" |
5f6f6664 NN |
20 | #include "sysemu/kvm.h" |
21 | #include "qemu/error-report.h" | |
db725815 | 22 | #include "qemu/main-loop.h" |
5f6f6664 | 23 | #include "qemu/sockets.h" |
efbfeb81 | 24 | #include "sysemu/cryptodev.h" |
9ccbfe14 DDAG |
25 | #include "migration/migration.h" |
26 | #include "migration/postcopy-ram.h" | |
6864a7b5 | 27 | #include "trace.h" |
0b0af4d6 | 28 | #include "exec/ramblock.h" |
5f6f6664 | 29 | |
5f6f6664 NN |
30 | #include <sys/ioctl.h> |
31 | #include <sys/socket.h> | |
32 | #include <sys/un.h> | |
18658a3c PB |
33 | |
34 | #include "standard-headers/linux/vhost_types.h" | |
35 | ||
36 | #ifdef CONFIG_LINUX | |
375318d0 | 37 | #include <linux/userfaultfd.h> |
18658a3c | 38 | #endif |
5f6f6664 | 39 | |
27598393 | 40 | #define VHOST_MEMORY_BASELINE_NREGIONS 8 |
dcb10c00 | 41 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 |
5f57fbea | 42 | #define VHOST_USER_SLAVE_MAX_FDS 8 |
e2051e9e | 43 | |
27598393 RN |
44 | /* |
45 | * Set maximum number of RAM slots supported to | |
46 | * the maximum number supported by the target | |
47 | * hardware plaform. | |
48 | */ | |
49 | #if defined(TARGET_X86) || defined(TARGET_X86_64) || \ | |
50 | defined(TARGET_ARM) || defined(TARGET_ARM_64) | |
51 | #include "hw/acpi/acpi.h" | |
52 | #define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS | |
53 | ||
97252353 | 54 | #elif defined(TARGET_PPC) || defined(TARGET_PPC64) |
27598393 RN |
55 | #include "hw/ppc/spapr.h" |
56 | #define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS | |
57 | ||
58 | #else | |
59 | #define VHOST_USER_MAX_RAM_SLOTS 512 | |
60 | #endif | |
61 | ||
4c3e257b CL |
62 | /* |
63 | * Maximum size of virtio device config space | |
64 | */ | |
65 | #define VHOST_USER_MAX_CONFIG_SIZE 256 | |
66 | ||
de1372d4 TC |
67 | enum VhostUserProtocolFeature { |
68 | VHOST_USER_PROTOCOL_F_MQ = 0, | |
69 | VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1, | |
70 | VHOST_USER_PROTOCOL_F_RARP = 2, | |
ca525ce5 | 71 | VHOST_USER_PROTOCOL_F_REPLY_ACK = 3, |
c5f048d8 | 72 | VHOST_USER_PROTOCOL_F_NET_MTU = 4, |
4bbeeba0 | 73 | VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5, |
5df04f17 | 74 | VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6, |
efbfeb81 | 75 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7, |
9ccbfe14 | 76 | VHOST_USER_PROTOCOL_F_PAGEFAULT = 8, |
1c3e5a26 | 77 | VHOST_USER_PROTOCOL_F_CONFIG = 9, |
5f57fbea | 78 | VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10, |
44866521 | 79 | VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11, |
5ad204bf | 80 | VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12, |
d91d57e6 | 81 | VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13, |
6b0eff1a RN |
82 | /* Feature 14 reserved for VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS. */ |
83 | VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15, | |
923b8921 | 84 | VHOST_USER_PROTOCOL_F_STATUS = 16, |
de1372d4 TC |
85 | VHOST_USER_PROTOCOL_F_MAX |
86 | }; | |
87 | ||
88 | #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1) | |
5f6f6664 NN |
89 | |
90 | typedef enum VhostUserRequest { | |
91 | VHOST_USER_NONE = 0, | |
92 | VHOST_USER_GET_FEATURES = 1, | |
93 | VHOST_USER_SET_FEATURES = 2, | |
94 | VHOST_USER_SET_OWNER = 3, | |
60915dc4 | 95 | VHOST_USER_RESET_OWNER = 4, |
5f6f6664 NN |
96 | VHOST_USER_SET_MEM_TABLE = 5, |
97 | VHOST_USER_SET_LOG_BASE = 6, | |
98 | VHOST_USER_SET_LOG_FD = 7, | |
99 | VHOST_USER_SET_VRING_NUM = 8, | |
100 | VHOST_USER_SET_VRING_ADDR = 9, | |
101 | VHOST_USER_SET_VRING_BASE = 10, | |
102 | VHOST_USER_GET_VRING_BASE = 11, | |
103 | VHOST_USER_SET_VRING_KICK = 12, | |
104 | VHOST_USER_SET_VRING_CALL = 13, | |
105 | VHOST_USER_SET_VRING_ERR = 14, | |
dcb10c00 MT |
106 | VHOST_USER_GET_PROTOCOL_FEATURES = 15, |
107 | VHOST_USER_SET_PROTOCOL_FEATURES = 16, | |
e2051e9e | 108 | VHOST_USER_GET_QUEUE_NUM = 17, |
7263a0ad | 109 | VHOST_USER_SET_VRING_ENABLE = 18, |
3e866365 | 110 | VHOST_USER_SEND_RARP = 19, |
c5f048d8 | 111 | VHOST_USER_NET_SET_MTU = 20, |
4bbeeba0 | 112 | VHOST_USER_SET_SLAVE_REQ_FD = 21, |
6dcdd06e | 113 | VHOST_USER_IOTLB_MSG = 22, |
5df04f17 | 114 | VHOST_USER_SET_VRING_ENDIAN = 23, |
4c3e257b CL |
115 | VHOST_USER_GET_CONFIG = 24, |
116 | VHOST_USER_SET_CONFIG = 25, | |
efbfeb81 GA |
117 | VHOST_USER_CREATE_CRYPTO_SESSION = 26, |
118 | VHOST_USER_CLOSE_CRYPTO_SESSION = 27, | |
d3dff7a5 | 119 | VHOST_USER_POSTCOPY_ADVISE = 28, |
6864a7b5 | 120 | VHOST_USER_POSTCOPY_LISTEN = 29, |
c639187e | 121 | VHOST_USER_POSTCOPY_END = 30, |
5ad204bf XY |
122 | VHOST_USER_GET_INFLIGHT_FD = 31, |
123 | VHOST_USER_SET_INFLIGHT_FD = 32, | |
bd2e44fe | 124 | VHOST_USER_GPU_SET_SOCKET = 33, |
d91d57e6 | 125 | VHOST_USER_RESET_DEVICE = 34, |
6b0eff1a RN |
126 | /* Message number 35 reserved for VHOST_USER_VRING_KICK. */ |
127 | VHOST_USER_GET_MAX_MEM_SLOTS = 36, | |
f1aeb14b RN |
128 | VHOST_USER_ADD_MEM_REG = 37, |
129 | VHOST_USER_REM_MEM_REG = 38, | |
923b8921 YW |
130 | VHOST_USER_SET_STATUS = 39, |
131 | VHOST_USER_GET_STATUS = 40, | |
5f6f6664 NN |
132 | VHOST_USER_MAX |
133 | } VhostUserRequest; | |
134 | ||
4bbeeba0 MAL |
135 | typedef enum VhostUserSlaveRequest { |
136 | VHOST_USER_SLAVE_NONE = 0, | |
6dcdd06e | 137 | VHOST_USER_SLAVE_IOTLB_MSG = 1, |
4c3e257b | 138 | VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2, |
44866521 | 139 | VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3, |
4bbeeba0 MAL |
140 | VHOST_USER_SLAVE_MAX |
141 | } VhostUserSlaveRequest; | |
142 | ||
5f6f6664 NN |
143 | typedef struct VhostUserMemoryRegion { |
144 | uint64_t guest_phys_addr; | |
145 | uint64_t memory_size; | |
146 | uint64_t userspace_addr; | |
3fd74b84 | 147 | uint64_t mmap_offset; |
5f6f6664 NN |
148 | } VhostUserMemoryRegion; |
149 | ||
150 | typedef struct VhostUserMemory { | |
151 | uint32_t nregions; | |
152 | uint32_t padding; | |
27598393 | 153 | VhostUserMemoryRegion regions[VHOST_MEMORY_BASELINE_NREGIONS]; |
5f6f6664 NN |
154 | } VhostUserMemory; |
155 | ||
f1aeb14b | 156 | typedef struct VhostUserMemRegMsg { |
3009edff | 157 | uint64_t padding; |
f1aeb14b RN |
158 | VhostUserMemoryRegion region; |
159 | } VhostUserMemRegMsg; | |
160 | ||
2b8819c6 VK |
161 | typedef struct VhostUserLog { |
162 | uint64_t mmap_size; | |
163 | uint64_t mmap_offset; | |
164 | } VhostUserLog; | |
165 | ||
4c3e257b CL |
166 | typedef struct VhostUserConfig { |
167 | uint32_t offset; | |
168 | uint32_t size; | |
169 | uint32_t flags; | |
170 | uint8_t region[VHOST_USER_MAX_CONFIG_SIZE]; | |
171 | } VhostUserConfig; | |
172 | ||
efbfeb81 GA |
173 | #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512 |
174 | #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64 | |
175 | ||
176 | typedef struct VhostUserCryptoSession { | |
177 | /* session id for success, -1 on errors */ | |
178 | int64_t session_id; | |
179 | CryptoDevBackendSymSessionInfo session_setup_data; | |
180 | uint8_t key[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN]; | |
181 | uint8_t auth_key[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN]; | |
182 | } VhostUserCryptoSession; | |
183 | ||
4c3e257b CL |
184 | static VhostUserConfig c __attribute__ ((unused)); |
185 | #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \ | |
186 | + sizeof(c.size) \ | |
187 | + sizeof(c.flags)) | |
188 | ||
44866521 TB |
189 | typedef struct VhostUserVringArea { |
190 | uint64_t u64; | |
191 | uint64_t size; | |
192 | uint64_t offset; | |
193 | } VhostUserVringArea; | |
194 | ||
5ad204bf XY |
195 | typedef struct VhostUserInflight { |
196 | uint64_t mmap_size; | |
197 | uint64_t mmap_offset; | |
198 | uint16_t num_queues; | |
199 | uint16_t queue_size; | |
200 | } VhostUserInflight; | |
201 | ||
24e34754 | 202 | typedef struct { |
5f6f6664 NN |
203 | VhostUserRequest request; |
204 | ||
205 | #define VHOST_USER_VERSION_MASK (0x3) | |
c97c76b3 | 206 | #define VHOST_USER_REPLY_MASK (0x1 << 2) |
ca525ce5 | 207 | #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3) |
5f6f6664 NN |
208 | uint32_t flags; |
209 | uint32_t size; /* the following payload size */ | |
24e34754 MT |
210 | } QEMU_PACKED VhostUserHeader; |
211 | ||
212 | typedef union { | |
5f6f6664 | 213 | #define VHOST_USER_VRING_IDX_MASK (0xff) |
c97c76b3 | 214 | #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8) |
5f6f6664 NN |
215 | uint64_t u64; |
216 | struct vhost_vring_state state; | |
217 | struct vhost_vring_addr addr; | |
218 | VhostUserMemory memory; | |
f1aeb14b | 219 | VhostUserMemRegMsg mem_reg; |
2b8819c6 | 220 | VhostUserLog log; |
6dcdd06e | 221 | struct vhost_iotlb_msg iotlb; |
4c3e257b | 222 | VhostUserConfig config; |
efbfeb81 | 223 | VhostUserCryptoSession session; |
44866521 | 224 | VhostUserVringArea area; |
5ad204bf | 225 | VhostUserInflight inflight; |
24e34754 MT |
226 | } VhostUserPayload; |
227 | ||
228 | typedef struct VhostUserMsg { | |
229 | VhostUserHeader hdr; | |
230 | VhostUserPayload payload; | |
5f6f6664 NN |
231 | } QEMU_PACKED VhostUserMsg; |
232 | ||
233 | static VhostUserMsg m __attribute__ ((unused)); | |
24e34754 | 234 | #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader)) |
5f6f6664 | 235 | |
24e34754 | 236 | #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload)) |
5f6f6664 NN |
237 | |
238 | /* The version of the protocol we support */ | |
239 | #define VHOST_USER_VERSION (0x1) | |
240 | ||
2152f3fe | 241 | struct vhost_user { |
9ccbfe14 | 242 | struct vhost_dev *dev; |
4d0cf552 TB |
243 | /* Shared between vhost devs of the same virtio device */ |
244 | VhostUserState *user; | |
57dc0217 GK |
245 | QIOChannel *slave_ioc; |
246 | GSource *slave_src; | |
9ccbfe14 | 247 | NotifierWithReturn postcopy_notifier; |
f82c1116 | 248 | struct PostCopyFD postcopy_fd; |
27598393 | 249 | uint64_t postcopy_client_bases[VHOST_USER_MAX_RAM_SLOTS]; |
905125d0 DDAG |
250 | /* Length of the region_rb and region_rb_offset arrays */ |
251 | size_t region_rb_len; | |
252 | /* RAMBlock associated with a given region */ | |
253 | RAMBlock **region_rb; | |
c97c76b3 AB |
254 | /* |
255 | * The offset from the start of the RAMBlock to the start of the | |
905125d0 DDAG |
256 | * vhost region. |
257 | */ | |
258 | ram_addr_t *region_rb_offset; | |
259 | ||
6864a7b5 DDAG |
260 | /* True once we've entered postcopy_listen */ |
261 | bool postcopy_listen; | |
f1aeb14b RN |
262 | |
263 | /* Our current regions */ | |
264 | int num_shadow_regions; | |
27598393 | 265 | struct vhost_memory_region shadow_regions[VHOST_USER_MAX_RAM_SLOTS]; |
f1aeb14b RN |
266 | }; |
267 | ||
268 | struct scrub_regions { | |
269 | struct vhost_memory_region *region; | |
270 | int reg_idx; | |
271 | int fd_idx; | |
2152f3fe MAL |
272 | }; |
273 | ||
5f6f6664 NN |
274 | static bool ioeventfd_enabled(void) |
275 | { | |
b0aa77d3 | 276 | return !kvm_enabled() || kvm_eventfds_enabled(); |
5f6f6664 NN |
277 | } |
278 | ||
9af84c02 | 279 | static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg) |
5f6f6664 | 280 | { |
2152f3fe | 281 | struct vhost_user *u = dev->opaque; |
4d0cf552 | 282 | CharBackend *chr = u->user->chr; |
5f6f6664 NN |
283 | uint8_t *p = (uint8_t *) msg; |
284 | int r, size = VHOST_USER_HDR_SIZE; | |
285 | ||
286 | r = qemu_chr_fe_read_all(chr, p, size); | |
287 | if (r != size) { | |
025faa87 | 288 | int saved_errno = errno; |
5421f318 | 289 | error_report("Failed to read msg header. Read %d instead of %d." |
24e34754 | 290 | " Original request %d.", r, size, msg->hdr.request); |
025faa87 | 291 | return r < 0 ? -saved_errno : -EIO; |
5f6f6664 NN |
292 | } |
293 | ||
294 | /* validate received flags */ | |
24e34754 | 295 | if (msg->hdr.flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) { |
5f6f6664 | 296 | error_report("Failed to read msg header." |
24e34754 | 297 | " Flags 0x%x instead of 0x%x.", msg->hdr.flags, |
5f6f6664 | 298 | VHOST_USER_REPLY_MASK | VHOST_USER_VERSION); |
025faa87 | 299 | return -EPROTO; |
9af84c02 MAL |
300 | } |
301 | ||
643a9435 AB |
302 | trace_vhost_user_read(msg->hdr.request, msg->hdr.flags); |
303 | ||
9af84c02 MAL |
304 | return 0; |
305 | } | |
306 | ||
a7f523c7 GK |
307 | struct vhost_user_read_cb_data { |
308 | struct vhost_dev *dev; | |
309 | VhostUserMsg *msg; | |
310 | GMainLoop *loop; | |
311 | int ret; | |
312 | }; | |
313 | ||
bf7b1eab | 314 | static gboolean vhost_user_read_cb(void *do_not_use, GIOCondition condition, |
a7f523c7 | 315 | gpointer opaque) |
9af84c02 | 316 | { |
a7f523c7 GK |
317 | struct vhost_user_read_cb_data *data = opaque; |
318 | struct vhost_dev *dev = data->dev; | |
319 | VhostUserMsg *msg = data->msg; | |
9af84c02 MAL |
320 | struct vhost_user *u = dev->opaque; |
321 | CharBackend *chr = u->user->chr; | |
322 | uint8_t *p = (uint8_t *) msg; | |
323 | int r, size; | |
324 | ||
025faa87 RK |
325 | r = vhost_user_read_header(dev, msg); |
326 | if (r < 0) { | |
327 | data->ret = r; | |
a7f523c7 | 328 | goto end; |
5f6f6664 NN |
329 | } |
330 | ||
331 | /* validate message size is sane */ | |
24e34754 | 332 | if (msg->hdr.size > VHOST_USER_PAYLOAD_SIZE) { |
5f6f6664 | 333 | error_report("Failed to read msg header." |
24e34754 | 334 | " Size %d exceeds the maximum %zu.", msg->hdr.size, |
5f6f6664 | 335 | VHOST_USER_PAYLOAD_SIZE); |
025faa87 | 336 | data->ret = -EPROTO; |
a7f523c7 | 337 | goto end; |
5f6f6664 NN |
338 | } |
339 | ||
24e34754 | 340 | if (msg->hdr.size) { |
5f6f6664 | 341 | p += VHOST_USER_HDR_SIZE; |
24e34754 | 342 | size = msg->hdr.size; |
5f6f6664 NN |
343 | r = qemu_chr_fe_read_all(chr, p, size); |
344 | if (r != size) { | |
025faa87 | 345 | int saved_errno = errno; |
5f6f6664 | 346 | error_report("Failed to read msg payload." |
24e34754 | 347 | " Read %d instead of %d.", r, msg->hdr.size); |
025faa87 | 348 | data->ret = r < 0 ? -saved_errno : -EIO; |
a7f523c7 | 349 | goto end; |
5f6f6664 NN |
350 | } |
351 | } | |
352 | ||
a7f523c7 GK |
353 | end: |
354 | g_main_loop_quit(data->loop); | |
355 | return G_SOURCE_REMOVE; | |
356 | } | |
357 | ||
db8a3772 GK |
358 | static gboolean slave_read(QIOChannel *ioc, GIOCondition condition, |
359 | gpointer opaque); | |
360 | ||
361 | /* | |
362 | * This updates the read handler to use a new event loop context. | |
363 | * Event sources are removed from the previous context : this ensures | |
364 | * that events detected in the previous context are purged. They will | |
365 | * be re-detected and processed in the new context. | |
366 | */ | |
367 | static void slave_update_read_handler(struct vhost_dev *dev, | |
368 | GMainContext *ctxt) | |
369 | { | |
370 | struct vhost_user *u = dev->opaque; | |
371 | ||
372 | if (!u->slave_ioc) { | |
373 | return; | |
374 | } | |
375 | ||
376 | if (u->slave_src) { | |
377 | g_source_destroy(u->slave_src); | |
378 | g_source_unref(u->slave_src); | |
379 | } | |
380 | ||
381 | u->slave_src = qio_channel_add_watch_source(u->slave_ioc, | |
382 | G_IO_IN | G_IO_HUP, | |
383 | slave_read, dev, NULL, | |
384 | ctxt); | |
385 | } | |
386 | ||
a7f523c7 GK |
387 | static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg) |
388 | { | |
389 | struct vhost_user *u = dev->opaque; | |
390 | CharBackend *chr = u->user->chr; | |
391 | GMainContext *prev_ctxt = chr->chr->gcontext; | |
392 | GMainContext *ctxt = g_main_context_new(); | |
393 | GMainLoop *loop = g_main_loop_new(ctxt, FALSE); | |
394 | struct vhost_user_read_cb_data data = { | |
395 | .dev = dev, | |
396 | .loop = loop, | |
397 | .msg = msg, | |
398 | .ret = 0 | |
399 | }; | |
400 | ||
401 | /* | |
402 | * We want to be able to monitor the slave channel fd while waiting | |
403 | * for chr I/O. This requires an event loop, but we can't nest the | |
404 | * one to which chr is currently attached : its fd handlers might not | |
405 | * be prepared for re-entrancy. So we create a new one and switch chr | |
406 | * to use it. | |
407 | */ | |
db8a3772 | 408 | slave_update_read_handler(dev, ctxt); |
a7f523c7 GK |
409 | qemu_chr_be_update_read_handlers(chr->chr, ctxt); |
410 | qemu_chr_fe_add_watch(chr, G_IO_IN | G_IO_HUP, vhost_user_read_cb, &data); | |
411 | ||
412 | g_main_loop_run(loop); | |
413 | ||
414 | /* | |
415 | * Restore the previous event loop context. This also destroys/recreates | |
416 | * event sources : this guarantees that all pending events in the original | |
417 | * context that have been processed by the nested loop are purged. | |
418 | */ | |
419 | qemu_chr_be_update_read_handlers(chr->chr, prev_ctxt); | |
db8a3772 | 420 | slave_update_read_handler(dev, NULL); |
a7f523c7 GK |
421 | |
422 | g_main_loop_unref(loop); | |
423 | g_main_context_unref(ctxt); | |
424 | ||
425 | return data.ret; | |
5f6f6664 NN |
426 | } |
427 | ||
ca525ce5 | 428 | static int process_message_reply(struct vhost_dev *dev, |
3cf7daf8 | 429 | const VhostUserMsg *msg) |
ca525ce5 | 430 | { |
025faa87 | 431 | int ret; |
60cd1102 | 432 | VhostUserMsg msg_reply; |
ca525ce5 | 433 | |
24e34754 | 434 | if ((msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) == 0) { |
60cd1102 ZY |
435 | return 0; |
436 | } | |
437 | ||
025faa87 RK |
438 | ret = vhost_user_read(dev, &msg_reply); |
439 | if (ret < 0) { | |
440 | return ret; | |
ca525ce5 PS |
441 | } |
442 | ||
24e34754 | 443 | if (msg_reply.hdr.request != msg->hdr.request) { |
edb40732 | 444 | error_report("Received unexpected msg type. " |
ca525ce5 | 445 | "Expected %d received %d", |
24e34754 | 446 | msg->hdr.request, msg_reply.hdr.request); |
025faa87 | 447 | return -EPROTO; |
ca525ce5 PS |
448 | } |
449 | ||
025faa87 | 450 | return msg_reply.payload.u64 ? -EIO : 0; |
ca525ce5 PS |
451 | } |
452 | ||
21e70425 MAL |
453 | static bool vhost_user_one_time_request(VhostUserRequest request) |
454 | { | |
455 | switch (request) { | |
456 | case VHOST_USER_SET_OWNER: | |
60915dc4 | 457 | case VHOST_USER_RESET_OWNER: |
21e70425 MAL |
458 | case VHOST_USER_SET_MEM_TABLE: |
459 | case VHOST_USER_GET_QUEUE_NUM: | |
c5f048d8 | 460 | case VHOST_USER_NET_SET_MTU: |
21e70425 MAL |
461 | return true; |
462 | default: | |
463 | return false; | |
464 | } | |
465 | } | |
466 | ||
467 | /* most non-init callers ignore the error */ | |
5f6f6664 NN |
468 | static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg, |
469 | int *fds, int fd_num) | |
470 | { | |
2152f3fe | 471 | struct vhost_user *u = dev->opaque; |
4d0cf552 | 472 | CharBackend *chr = u->user->chr; |
24e34754 | 473 | int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size; |
5f6f6664 | 474 | |
21e70425 MAL |
475 | /* |
476 | * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE, | |
477 | * we just need send it once in the first time. For later such | |
478 | * request, we just ignore it. | |
479 | */ | |
24e34754 MT |
480 | if (vhost_user_one_time_request(msg->hdr.request) && dev->vq_index != 0) { |
481 | msg->hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK; | |
21e70425 MAL |
482 | return 0; |
483 | } | |
484 | ||
6fab2f3f | 485 | if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) { |
f6b85710 | 486 | error_report("Failed to set msg fds."); |
025faa87 | 487 | return -EINVAL; |
6fab2f3f | 488 | } |
5f6f6664 | 489 | |
f6b85710 MAL |
490 | ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size); |
491 | if (ret != size) { | |
025faa87 | 492 | int saved_errno = errno; |
f6b85710 MAL |
493 | error_report("Failed to write msg." |
494 | " Wrote %d instead of %d.", ret, size); | |
025faa87 | 495 | return ret < 0 ? -saved_errno : -EIO; |
f6b85710 MAL |
496 | } |
497 | ||
6ca6d8ee AB |
498 | trace_vhost_user_write(msg->hdr.request, msg->hdr.flags); |
499 | ||
f6b85710 | 500 | return 0; |
5f6f6664 NN |
501 | } |
502 | ||
bd2e44fe MAL |
503 | int vhost_user_gpu_set_socket(struct vhost_dev *dev, int fd) |
504 | { | |
505 | VhostUserMsg msg = { | |
506 | .hdr.request = VHOST_USER_GPU_SET_SOCKET, | |
507 | .hdr.flags = VHOST_USER_VERSION, | |
508 | }; | |
509 | ||
510 | return vhost_user_write(dev, &msg, &fd, 1); | |
511 | } | |
512 | ||
21e70425 MAL |
513 | static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base, |
514 | struct vhost_log *log) | |
b931bfbf | 515 | { |
27598393 | 516 | int fds[VHOST_USER_MAX_RAM_SLOTS]; |
21e70425 MAL |
517 | size_t fd_num = 0; |
518 | bool shmfd = virtio_has_feature(dev->protocol_features, | |
519 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
025faa87 | 520 | int ret; |
21e70425 | 521 | VhostUserMsg msg = { |
24e34754 MT |
522 | .hdr.request = VHOST_USER_SET_LOG_BASE, |
523 | .hdr.flags = VHOST_USER_VERSION, | |
48854f57 | 524 | .payload.log.mmap_size = log->size * sizeof(*(log->log)), |
2b8819c6 | 525 | .payload.log.mmap_offset = 0, |
24e34754 | 526 | .hdr.size = sizeof(msg.payload.log), |
21e70425 MAL |
527 | }; |
528 | ||
529 | if (shmfd && log->fd != -1) { | |
530 | fds[fd_num++] = log->fd; | |
531 | } | |
532 | ||
025faa87 RK |
533 | ret = vhost_user_write(dev, &msg, fds, fd_num); |
534 | if (ret < 0) { | |
535 | return ret; | |
c4843a45 | 536 | } |
21e70425 MAL |
537 | |
538 | if (shmfd) { | |
24e34754 | 539 | msg.hdr.size = 0; |
025faa87 RK |
540 | ret = vhost_user_read(dev, &msg); |
541 | if (ret < 0) { | |
542 | return ret; | |
21e70425 MAL |
543 | } |
544 | ||
24e34754 | 545 | if (msg.hdr.request != VHOST_USER_SET_LOG_BASE) { |
21e70425 MAL |
546 | error_report("Received unexpected msg type. " |
547 | "Expected %d received %d", | |
24e34754 | 548 | VHOST_USER_SET_LOG_BASE, msg.hdr.request); |
025faa87 | 549 | return -EPROTO; |
21e70425 | 550 | } |
b931bfbf | 551 | } |
21e70425 MAL |
552 | |
553 | return 0; | |
b931bfbf CO |
554 | } |
555 | ||
23374a84 RN |
556 | static MemoryRegion *vhost_user_get_mr_data(uint64_t addr, ram_addr_t *offset, |
557 | int *fd) | |
558 | { | |
559 | MemoryRegion *mr; | |
560 | ||
561 | assert((uintptr_t)addr == addr); | |
562 | mr = memory_region_from_host((void *)(uintptr_t)addr, offset); | |
563 | *fd = memory_region_get_fd(mr); | |
564 | ||
565 | return mr; | |
566 | } | |
567 | ||
ece99091 | 568 | static void vhost_user_fill_msg_region(VhostUserMemoryRegion *dst, |
8d193715 RN |
569 | struct vhost_memory_region *src, |
570 | uint64_t mmap_offset) | |
ece99091 RN |
571 | { |
572 | assert(src != NULL && dst != NULL); | |
573 | dst->userspace_addr = src->userspace_addr; | |
574 | dst->memory_size = src->memory_size; | |
575 | dst->guest_phys_addr = src->guest_phys_addr; | |
8d193715 | 576 | dst->mmap_offset = mmap_offset; |
ece99091 RN |
577 | } |
578 | ||
2d9da9df RN |
579 | static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u, |
580 | struct vhost_dev *dev, | |
581 | VhostUserMsg *msg, | |
582 | int *fds, size_t *fd_num, | |
583 | bool track_ramblocks) | |
584 | { | |
585 | int i, fd; | |
586 | ram_addr_t offset; | |
587 | MemoryRegion *mr; | |
588 | struct vhost_memory_region *reg; | |
ece99091 | 589 | VhostUserMemoryRegion region_buffer; |
2d9da9df RN |
590 | |
591 | msg->hdr.request = VHOST_USER_SET_MEM_TABLE; | |
592 | ||
593 | for (i = 0; i < dev->mem->nregions; ++i) { | |
594 | reg = dev->mem->regions + i; | |
595 | ||
23374a84 | 596 | mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd); |
2d9da9df RN |
597 | if (fd > 0) { |
598 | if (track_ramblocks) { | |
27598393 | 599 | assert(*fd_num < VHOST_MEMORY_BASELINE_NREGIONS); |
2d9da9df RN |
600 | trace_vhost_user_set_mem_table_withfd(*fd_num, mr->name, |
601 | reg->memory_size, | |
602 | reg->guest_phys_addr, | |
603 | reg->userspace_addr, | |
604 | offset); | |
605 | u->region_rb_offset[i] = offset; | |
606 | u->region_rb[i] = mr->ram_block; | |
27598393 | 607 | } else if (*fd_num == VHOST_MEMORY_BASELINE_NREGIONS) { |
2d9da9df | 608 | error_report("Failed preparing vhost-user memory table msg"); |
025faa87 | 609 | return -ENOBUFS; |
2d9da9df | 610 | } |
8d193715 | 611 | vhost_user_fill_msg_region(®ion_buffer, reg, offset); |
ece99091 | 612 | msg->payload.memory.regions[*fd_num] = region_buffer; |
2d9da9df RN |
613 | fds[(*fd_num)++] = fd; |
614 | } else if (track_ramblocks) { | |
615 | u->region_rb_offset[i] = 0; | |
616 | u->region_rb[i] = NULL; | |
617 | } | |
618 | } | |
619 | ||
620 | msg->payload.memory.nregions = *fd_num; | |
621 | ||
622 | if (!*fd_num) { | |
623 | error_report("Failed initializing vhost-user memory map, " | |
624 | "consider using -object memory-backend-file share=on"); | |
025faa87 | 625 | return -EINVAL; |
2d9da9df RN |
626 | } |
627 | ||
628 | msg->hdr.size = sizeof(msg->payload.memory.nregions); | |
629 | msg->hdr.size += sizeof(msg->payload.memory.padding); | |
630 | msg->hdr.size += *fd_num * sizeof(VhostUserMemoryRegion); | |
631 | ||
025faa87 | 632 | return 0; |
2d9da9df RN |
633 | } |
634 | ||
f1aeb14b RN |
635 | static inline bool reg_equal(struct vhost_memory_region *shadow_reg, |
636 | struct vhost_memory_region *vdev_reg) | |
637 | { | |
638 | return shadow_reg->guest_phys_addr == vdev_reg->guest_phys_addr && | |
639 | shadow_reg->userspace_addr == vdev_reg->userspace_addr && | |
640 | shadow_reg->memory_size == vdev_reg->memory_size; | |
641 | } | |
642 | ||
643 | static void scrub_shadow_regions(struct vhost_dev *dev, | |
644 | struct scrub_regions *add_reg, | |
645 | int *nr_add_reg, | |
646 | struct scrub_regions *rem_reg, | |
647 | int *nr_rem_reg, uint64_t *shadow_pcb, | |
648 | bool track_ramblocks) | |
649 | { | |
650 | struct vhost_user *u = dev->opaque; | |
27598393 | 651 | bool found[VHOST_USER_MAX_RAM_SLOTS] = {}; |
f1aeb14b RN |
652 | struct vhost_memory_region *reg, *shadow_reg; |
653 | int i, j, fd, add_idx = 0, rm_idx = 0, fd_num = 0; | |
654 | ram_addr_t offset; | |
655 | MemoryRegion *mr; | |
656 | bool matching; | |
657 | ||
658 | /* | |
659 | * Find memory regions present in our shadow state which are not in | |
660 | * the device's current memory state. | |
661 | * | |
662 | * Mark regions in both the shadow and device state as "found". | |
663 | */ | |
664 | for (i = 0; i < u->num_shadow_regions; i++) { | |
665 | shadow_reg = &u->shadow_regions[i]; | |
666 | matching = false; | |
667 | ||
668 | for (j = 0; j < dev->mem->nregions; j++) { | |
669 | reg = &dev->mem->regions[j]; | |
670 | ||
671 | mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd); | |
672 | ||
673 | if (reg_equal(shadow_reg, reg)) { | |
674 | matching = true; | |
675 | found[j] = true; | |
676 | if (track_ramblocks) { | |
677 | /* | |
678 | * Reset postcopy client bases, region_rb, and | |
679 | * region_rb_offset in case regions are removed. | |
680 | */ | |
681 | if (fd > 0) { | |
682 | u->region_rb_offset[j] = offset; | |
683 | u->region_rb[j] = mr->ram_block; | |
684 | shadow_pcb[j] = u->postcopy_client_bases[i]; | |
685 | } else { | |
686 | u->region_rb_offset[j] = 0; | |
687 | u->region_rb[j] = NULL; | |
688 | } | |
689 | } | |
690 | break; | |
691 | } | |
692 | } | |
693 | ||
694 | /* | |
695 | * If the region was not found in the current device memory state | |
696 | * create an entry for it in the removed list. | |
697 | */ | |
698 | if (!matching) { | |
699 | rem_reg[rm_idx].region = shadow_reg; | |
700 | rem_reg[rm_idx++].reg_idx = i; | |
701 | } | |
702 | } | |
703 | ||
704 | /* | |
705 | * For regions not marked "found", create entries in the added list. | |
706 | * | |
707 | * Note their indexes in the device memory state and the indexes of their | |
708 | * file descriptors. | |
709 | */ | |
710 | for (i = 0; i < dev->mem->nregions; i++) { | |
711 | reg = &dev->mem->regions[i]; | |
8b616bee | 712 | vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd); |
f1aeb14b RN |
713 | if (fd > 0) { |
714 | ++fd_num; | |
715 | } | |
716 | ||
717 | /* | |
718 | * If the region was in both the shadow and device state we don't | |
719 | * need to send a VHOST_USER_ADD_MEM_REG message for it. | |
720 | */ | |
721 | if (found[i]) { | |
722 | continue; | |
723 | } | |
724 | ||
725 | add_reg[add_idx].region = reg; | |
726 | add_reg[add_idx].reg_idx = i; | |
727 | add_reg[add_idx++].fd_idx = fd_num; | |
728 | } | |
729 | *nr_rem_reg = rm_idx; | |
730 | *nr_add_reg = add_idx; | |
731 | ||
732 | return; | |
733 | } | |
734 | ||
735 | static int send_remove_regions(struct vhost_dev *dev, | |
736 | struct scrub_regions *remove_reg, | |
737 | int nr_rem_reg, VhostUserMsg *msg, | |
738 | bool reply_supported) | |
739 | { | |
740 | struct vhost_user *u = dev->opaque; | |
741 | struct vhost_memory_region *shadow_reg; | |
742 | int i, fd, shadow_reg_idx, ret; | |
743 | ram_addr_t offset; | |
744 | VhostUserMemoryRegion region_buffer; | |
745 | ||
746 | /* | |
747 | * The regions in remove_reg appear in the same order they do in the | |
748 | * shadow table. Therefore we can minimize memory copies by iterating | |
749 | * through remove_reg backwards. | |
750 | */ | |
751 | for (i = nr_rem_reg - 1; i >= 0; i--) { | |
752 | shadow_reg = remove_reg[i].region; | |
753 | shadow_reg_idx = remove_reg[i].reg_idx; | |
754 | ||
755 | vhost_user_get_mr_data(shadow_reg->userspace_addr, &offset, &fd); | |
756 | ||
757 | if (fd > 0) { | |
758 | msg->hdr.request = VHOST_USER_REM_MEM_REG; | |
8d193715 | 759 | vhost_user_fill_msg_region(®ion_buffer, shadow_reg, 0); |
f1aeb14b RN |
760 | msg->payload.mem_reg.region = region_buffer; |
761 | ||
a81d8d4a | 762 | ret = vhost_user_write(dev, msg, NULL, 0); |
025faa87 RK |
763 | if (ret < 0) { |
764 | return ret; | |
f1aeb14b RN |
765 | } |
766 | ||
767 | if (reply_supported) { | |
768 | ret = process_message_reply(dev, msg); | |
769 | if (ret) { | |
770 | return ret; | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
775 | /* | |
776 | * At this point we know the backend has unmapped the region. It is now | |
777 | * safe to remove it from the shadow table. | |
778 | */ | |
779 | memmove(&u->shadow_regions[shadow_reg_idx], | |
780 | &u->shadow_regions[shadow_reg_idx + 1], | |
781 | sizeof(struct vhost_memory_region) * | |
4fdecf05 | 782 | (u->num_shadow_regions - shadow_reg_idx - 1)); |
f1aeb14b RN |
783 | u->num_shadow_regions--; |
784 | } | |
785 | ||
786 | return 0; | |
787 | } | |
788 | ||
789 | static int send_add_regions(struct vhost_dev *dev, | |
790 | struct scrub_regions *add_reg, int nr_add_reg, | |
791 | VhostUserMsg *msg, uint64_t *shadow_pcb, | |
792 | bool reply_supported, bool track_ramblocks) | |
793 | { | |
794 | struct vhost_user *u = dev->opaque; | |
795 | int i, fd, ret, reg_idx, reg_fd_idx; | |
796 | struct vhost_memory_region *reg; | |
797 | MemoryRegion *mr; | |
798 | ram_addr_t offset; | |
799 | VhostUserMsg msg_reply; | |
800 | VhostUserMemoryRegion region_buffer; | |
801 | ||
802 | for (i = 0; i < nr_add_reg; i++) { | |
803 | reg = add_reg[i].region; | |
804 | reg_idx = add_reg[i].reg_idx; | |
805 | reg_fd_idx = add_reg[i].fd_idx; | |
806 | ||
807 | mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd); | |
808 | ||
809 | if (fd > 0) { | |
810 | if (track_ramblocks) { | |
811 | trace_vhost_user_set_mem_table_withfd(reg_fd_idx, mr->name, | |
812 | reg->memory_size, | |
813 | reg->guest_phys_addr, | |
814 | reg->userspace_addr, | |
815 | offset); | |
816 | u->region_rb_offset[reg_idx] = offset; | |
817 | u->region_rb[reg_idx] = mr->ram_block; | |
818 | } | |
819 | msg->hdr.request = VHOST_USER_ADD_MEM_REG; | |
8d193715 | 820 | vhost_user_fill_msg_region(®ion_buffer, reg, offset); |
f1aeb14b | 821 | msg->payload.mem_reg.region = region_buffer; |
f1aeb14b | 822 | |
025faa87 RK |
823 | ret = vhost_user_write(dev, msg, &fd, 1); |
824 | if (ret < 0) { | |
825 | return ret; | |
f1aeb14b RN |
826 | } |
827 | ||
828 | if (track_ramblocks) { | |
829 | uint64_t reply_gpa; | |
830 | ||
025faa87 RK |
831 | ret = vhost_user_read(dev, &msg_reply); |
832 | if (ret < 0) { | |
833 | return ret; | |
f1aeb14b RN |
834 | } |
835 | ||
836 | reply_gpa = msg_reply.payload.mem_reg.region.guest_phys_addr; | |
837 | ||
838 | if (msg_reply.hdr.request != VHOST_USER_ADD_MEM_REG) { | |
839 | error_report("%s: Received unexpected msg type." | |
840 | "Expected %d received %d", __func__, | |
841 | VHOST_USER_ADD_MEM_REG, | |
842 | msg_reply.hdr.request); | |
025faa87 | 843 | return -EPROTO; |
f1aeb14b RN |
844 | } |
845 | ||
846 | /* | |
847 | * We're using the same structure, just reusing one of the | |
848 | * fields, so it should be the same size. | |
849 | */ | |
850 | if (msg_reply.hdr.size != msg->hdr.size) { | |
851 | error_report("%s: Unexpected size for postcopy reply " | |
852 | "%d vs %d", __func__, msg_reply.hdr.size, | |
853 | msg->hdr.size); | |
025faa87 | 854 | return -EPROTO; |
f1aeb14b RN |
855 | } |
856 | ||
857 | /* Get the postcopy client base from the backend's reply. */ | |
858 | if (reply_gpa == dev->mem->regions[reg_idx].guest_phys_addr) { | |
859 | shadow_pcb[reg_idx] = | |
860 | msg_reply.payload.mem_reg.region.userspace_addr; | |
861 | trace_vhost_user_set_mem_table_postcopy( | |
862 | msg_reply.payload.mem_reg.region.userspace_addr, | |
863 | msg->payload.mem_reg.region.userspace_addr, | |
864 | reg_fd_idx, reg_idx); | |
865 | } else { | |
866 | error_report("%s: invalid postcopy reply for region. " | |
867 | "Got guest physical address %" PRIX64 ", expected " | |
868 | "%" PRIX64, __func__, reply_gpa, | |
869 | dev->mem->regions[reg_idx].guest_phys_addr); | |
025faa87 | 870 | return -EPROTO; |
f1aeb14b RN |
871 | } |
872 | } else if (reply_supported) { | |
873 | ret = process_message_reply(dev, msg); | |
874 | if (ret) { | |
875 | return ret; | |
876 | } | |
877 | } | |
878 | } else if (track_ramblocks) { | |
879 | u->region_rb_offset[reg_idx] = 0; | |
880 | u->region_rb[reg_idx] = NULL; | |
881 | } | |
882 | ||
883 | /* | |
884 | * At this point, we know the backend has mapped in the new | |
885 | * region, if the region has a valid file descriptor. | |
886 | * | |
887 | * The region should now be added to the shadow table. | |
888 | */ | |
889 | u->shadow_regions[u->num_shadow_regions].guest_phys_addr = | |
890 | reg->guest_phys_addr; | |
891 | u->shadow_regions[u->num_shadow_regions].userspace_addr = | |
892 | reg->userspace_addr; | |
893 | u->shadow_regions[u->num_shadow_regions].memory_size = | |
894 | reg->memory_size; | |
895 | u->num_shadow_regions++; | |
896 | } | |
897 | ||
898 | return 0; | |
899 | } | |
900 | ||
901 | static int vhost_user_add_remove_regions(struct vhost_dev *dev, | |
902 | VhostUserMsg *msg, | |
903 | bool reply_supported, | |
904 | bool track_ramblocks) | |
905 | { | |
906 | struct vhost_user *u = dev->opaque; | |
27598393 RN |
907 | struct scrub_regions add_reg[VHOST_USER_MAX_RAM_SLOTS]; |
908 | struct scrub_regions rem_reg[VHOST_USER_MAX_RAM_SLOTS]; | |
909 | uint64_t shadow_pcb[VHOST_USER_MAX_RAM_SLOTS] = {}; | |
f1aeb14b | 910 | int nr_add_reg, nr_rem_reg; |
025faa87 | 911 | int ret; |
f1aeb14b | 912 | |
3009edff | 913 | msg->hdr.size = sizeof(msg->payload.mem_reg); |
f1aeb14b RN |
914 | |
915 | /* Find the regions which need to be removed or added. */ | |
916 | scrub_shadow_regions(dev, add_reg, &nr_add_reg, rem_reg, &nr_rem_reg, | |
917 | shadow_pcb, track_ramblocks); | |
918 | ||
025faa87 RK |
919 | if (nr_rem_reg) { |
920 | ret = send_remove_regions(dev, rem_reg, nr_rem_reg, msg, | |
921 | reply_supported); | |
922 | if (ret < 0) { | |
923 | goto err; | |
924 | } | |
f1aeb14b RN |
925 | } |
926 | ||
025faa87 RK |
927 | if (nr_add_reg) { |
928 | ret = send_add_regions(dev, add_reg, nr_add_reg, msg, shadow_pcb, | |
929 | reply_supported, track_ramblocks); | |
930 | if (ret < 0) { | |
931 | goto err; | |
932 | } | |
f1aeb14b RN |
933 | } |
934 | ||
935 | if (track_ramblocks) { | |
936 | memcpy(u->postcopy_client_bases, shadow_pcb, | |
27598393 | 937 | sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); |
f1aeb14b RN |
938 | /* |
939 | * Now we've registered this with the postcopy code, we ack to the | |
940 | * client, because now we're in the position to be able to deal with | |
941 | * any faults it generates. | |
942 | */ | |
943 | /* TODO: Use this for failure cases as well with a bad value. */ | |
944 | msg->hdr.size = sizeof(msg->payload.u64); | |
945 | msg->payload.u64 = 0; /* OK */ | |
946 | ||
025faa87 RK |
947 | ret = vhost_user_write(dev, msg, NULL, 0); |
948 | if (ret < 0) { | |
949 | return ret; | |
f1aeb14b RN |
950 | } |
951 | } | |
952 | ||
953 | return 0; | |
954 | ||
955 | err: | |
956 | if (track_ramblocks) { | |
957 | memcpy(u->postcopy_client_bases, shadow_pcb, | |
27598393 | 958 | sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); |
f1aeb14b RN |
959 | } |
960 | ||
025faa87 | 961 | return ret; |
f1aeb14b RN |
962 | } |
963 | ||
55d754b3 | 964 | static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev, |
f1aeb14b RN |
965 | struct vhost_memory *mem, |
966 | bool reply_supported, | |
967 | bool config_mem_slots) | |
55d754b3 | 968 | { |
9bb38019 | 969 | struct vhost_user *u = dev->opaque; |
27598393 | 970 | int fds[VHOST_MEMORY_BASELINE_NREGIONS]; |
55d754b3 | 971 | size_t fd_num = 0; |
9bb38019 DDAG |
972 | VhostUserMsg msg_reply; |
973 | int region_i, msg_i; | |
025faa87 | 974 | int ret; |
9bb38019 | 975 | |
55d754b3 | 976 | VhostUserMsg msg = { |
55d754b3 DDAG |
977 | .hdr.flags = VHOST_USER_VERSION, |
978 | }; | |
979 | ||
905125d0 DDAG |
980 | if (u->region_rb_len < dev->mem->nregions) { |
981 | u->region_rb = g_renew(RAMBlock*, u->region_rb, dev->mem->nregions); | |
982 | u->region_rb_offset = g_renew(ram_addr_t, u->region_rb_offset, | |
983 | dev->mem->nregions); | |
984 | memset(&(u->region_rb[u->region_rb_len]), '\0', | |
985 | sizeof(RAMBlock *) * (dev->mem->nregions - u->region_rb_len)); | |
986 | memset(&(u->region_rb_offset[u->region_rb_len]), '\0', | |
987 | sizeof(ram_addr_t) * (dev->mem->nregions - u->region_rb_len)); | |
988 | u->region_rb_len = dev->mem->nregions; | |
989 | } | |
990 | ||
f1aeb14b | 991 | if (config_mem_slots) { |
025faa87 RK |
992 | ret = vhost_user_add_remove_regions(dev, &msg, reply_supported, true); |
993 | if (ret < 0) { | |
994 | return ret; | |
f1aeb14b RN |
995 | } |
996 | } else { | |
025faa87 RK |
997 | ret = vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, |
998 | true); | |
999 | if (ret < 0) { | |
1000 | return ret; | |
f1aeb14b | 1001 | } |
55d754b3 | 1002 | |
025faa87 RK |
1003 | ret = vhost_user_write(dev, &msg, fds, fd_num); |
1004 | if (ret < 0) { | |
1005 | return ret; | |
f1aeb14b | 1006 | } |
55d754b3 | 1007 | |
025faa87 RK |
1008 | ret = vhost_user_read(dev, &msg_reply); |
1009 | if (ret < 0) { | |
1010 | return ret; | |
f1aeb14b | 1011 | } |
9bb38019 | 1012 | |
f1aeb14b RN |
1013 | if (msg_reply.hdr.request != VHOST_USER_SET_MEM_TABLE) { |
1014 | error_report("%s: Received unexpected msg type." | |
1015 | "Expected %d received %d", __func__, | |
1016 | VHOST_USER_SET_MEM_TABLE, msg_reply.hdr.request); | |
025faa87 | 1017 | return -EPROTO; |
f1aeb14b | 1018 | } |
9bb38019 | 1019 | |
f1aeb14b RN |
1020 | /* |
1021 | * We're using the same structure, just reusing one of the | |
1022 | * fields, so it should be the same size. | |
1023 | */ | |
1024 | if (msg_reply.hdr.size != msg.hdr.size) { | |
1025 | error_report("%s: Unexpected size for postcopy reply " | |
1026 | "%d vs %d", __func__, msg_reply.hdr.size, | |
1027 | msg.hdr.size); | |
025faa87 | 1028 | return -EPROTO; |
f1aeb14b RN |
1029 | } |
1030 | ||
1031 | memset(u->postcopy_client_bases, 0, | |
27598393 | 1032 | sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); |
f1aeb14b RN |
1033 | |
1034 | /* | |
1035 | * They're in the same order as the regions that were sent | |
1036 | * but some of the regions were skipped (above) if they | |
1037 | * didn't have fd's | |
1038 | */ | |
1039 | for (msg_i = 0, region_i = 0; | |
1040 | region_i < dev->mem->nregions; | |
1041 | region_i++) { | |
1042 | if (msg_i < fd_num && | |
1043 | msg_reply.payload.memory.regions[msg_i].guest_phys_addr == | |
1044 | dev->mem->regions[region_i].guest_phys_addr) { | |
1045 | u->postcopy_client_bases[region_i] = | |
1046 | msg_reply.payload.memory.regions[msg_i].userspace_addr; | |
1047 | trace_vhost_user_set_mem_table_postcopy( | |
1048 | msg_reply.payload.memory.regions[msg_i].userspace_addr, | |
1049 | msg.payload.memory.regions[msg_i].userspace_addr, | |
1050 | msg_i, region_i); | |
1051 | msg_i++; | |
1052 | } | |
1053 | } | |
1054 | if (msg_i != fd_num) { | |
1055 | error_report("%s: postcopy reply not fully consumed " | |
1056 | "%d vs %zd", | |
1057 | __func__, msg_i, fd_num); | |
025faa87 | 1058 | return -EIO; |
f1aeb14b RN |
1059 | } |
1060 | ||
1061 | /* | |
1062 | * Now we've registered this with the postcopy code, we ack to the | |
1063 | * client, because now we're in the position to be able to deal | |
1064 | * with any faults it generates. | |
1065 | */ | |
1066 | /* TODO: Use this for failure cases as well with a bad value. */ | |
1067 | msg.hdr.size = sizeof(msg.payload.u64); | |
1068 | msg.payload.u64 = 0; /* OK */ | |
025faa87 RK |
1069 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1070 | if (ret < 0) { | |
1071 | return ret; | |
9bb38019 | 1072 | } |
9bb38019 DDAG |
1073 | } |
1074 | ||
55d754b3 DDAG |
1075 | return 0; |
1076 | } | |
1077 | ||
94c9cb31 MT |
1078 | static int vhost_user_set_mem_table(struct vhost_dev *dev, |
1079 | struct vhost_memory *mem) | |
1080 | { | |
55d754b3 | 1081 | struct vhost_user *u = dev->opaque; |
27598393 | 1082 | int fds[VHOST_MEMORY_BASELINE_NREGIONS]; |
94c9cb31 | 1083 | size_t fd_num = 0; |
55d754b3 | 1084 | bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler; |
94c9cb31 | 1085 | bool reply_supported = virtio_has_feature(dev->protocol_features, |
5ce43896 | 1086 | VHOST_USER_PROTOCOL_F_REPLY_ACK); |
f1aeb14b RN |
1087 | bool config_mem_slots = |
1088 | virtio_has_feature(dev->protocol_features, | |
1089 | VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS); | |
025faa87 | 1090 | int ret; |
94c9cb31 | 1091 | |
55d754b3 | 1092 | if (do_postcopy) { |
f1aeb14b RN |
1093 | /* |
1094 | * Postcopy has enough differences that it's best done in it's own | |
55d754b3 DDAG |
1095 | * version |
1096 | */ | |
f1aeb14b RN |
1097 | return vhost_user_set_mem_table_postcopy(dev, mem, reply_supported, |
1098 | config_mem_slots); | |
55d754b3 DDAG |
1099 | } |
1100 | ||
94c9cb31 | 1101 | VhostUserMsg msg = { |
24e34754 | 1102 | .hdr.flags = VHOST_USER_VERSION, |
94c9cb31 MT |
1103 | }; |
1104 | ||
1105 | if (reply_supported) { | |
24e34754 | 1106 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
94c9cb31 MT |
1107 | } |
1108 | ||
f1aeb14b | 1109 | if (config_mem_slots) { |
025faa87 RK |
1110 | ret = vhost_user_add_remove_regions(dev, &msg, reply_supported, false); |
1111 | if (ret < 0) { | |
1112 | return ret; | |
f1aeb14b RN |
1113 | } |
1114 | } else { | |
025faa87 RK |
1115 | ret = vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, |
1116 | false); | |
1117 | if (ret < 0) { | |
1118 | return ret; | |
f1aeb14b | 1119 | } |
025faa87 RK |
1120 | |
1121 | ret = vhost_user_write(dev, &msg, fds, fd_num); | |
1122 | if (ret < 0) { | |
1123 | return ret; | |
f1aeb14b | 1124 | } |
94c9cb31 | 1125 | |
f1aeb14b RN |
1126 | if (reply_supported) { |
1127 | return process_message_reply(dev, &msg); | |
1128 | } | |
94c9cb31 MT |
1129 | } |
1130 | ||
1131 | return 0; | |
1132 | } | |
1133 | ||
21e70425 MAL |
1134 | static int vhost_user_set_vring_endian(struct vhost_dev *dev, |
1135 | struct vhost_vring_state *ring) | |
1136 | { | |
5df04f17 FF |
1137 | bool cross_endian = virtio_has_feature(dev->protocol_features, |
1138 | VHOST_USER_PROTOCOL_F_CROSS_ENDIAN); | |
1139 | VhostUserMsg msg = { | |
24e34754 MT |
1140 | .hdr.request = VHOST_USER_SET_VRING_ENDIAN, |
1141 | .hdr.flags = VHOST_USER_VERSION, | |
5df04f17 | 1142 | .payload.state = *ring, |
24e34754 | 1143 | .hdr.size = sizeof(msg.payload.state), |
5df04f17 FF |
1144 | }; |
1145 | ||
1146 | if (!cross_endian) { | |
1147 | error_report("vhost-user trying to send unhandled ioctl"); | |
025faa87 | 1148 | return -ENOTSUP; |
5df04f17 FF |
1149 | } |
1150 | ||
025faa87 | 1151 | return vhost_user_write(dev, &msg, NULL, 0); |
21e70425 | 1152 | } |
5f6f6664 | 1153 | |
21e70425 MAL |
1154 | static int vhost_set_vring(struct vhost_dev *dev, |
1155 | unsigned long int request, | |
1156 | struct vhost_vring_state *ring) | |
1157 | { | |
1158 | VhostUserMsg msg = { | |
24e34754 MT |
1159 | .hdr.request = request, |
1160 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 1161 | .payload.state = *ring, |
24e34754 | 1162 | .hdr.size = sizeof(msg.payload.state), |
21e70425 MAL |
1163 | }; |
1164 | ||
025faa87 | 1165 | return vhost_user_write(dev, &msg, NULL, 0); |
21e70425 MAL |
1166 | } |
1167 | ||
1168 | static int vhost_user_set_vring_num(struct vhost_dev *dev, | |
1169 | struct vhost_vring_state *ring) | |
1170 | { | |
1171 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring); | |
1172 | } | |
1173 | ||
0b0af4d6 | 1174 | static void vhost_user_host_notifier_free(VhostUserHostNotifier *n) |
44866521 | 1175 | { |
0b0af4d6 | 1176 | assert(n && n->unmap_addr); |
8e3b0cbb | 1177 | munmap(n->unmap_addr, qemu_real_host_page_size()); |
0b0af4d6 XL |
1178 | n->unmap_addr = NULL; |
1179 | } | |
1180 | ||
503e3554 AB |
1181 | /* |
1182 | * clean-up function for notifier, will finally free the structure | |
1183 | * under rcu. | |
1184 | */ | |
1185 | static void vhost_user_host_notifier_remove(VhostUserHostNotifier *n, | |
1186 | VirtIODevice *vdev) | |
0b0af4d6 | 1187 | { |
e867144b | 1188 | if (n->addr) { |
0b0af4d6 | 1189 | if (vdev) { |
503e3554 | 1190 | virtio_queue_set_host_notifier_mr(vdev, n->idx, &n->mr, false); |
0b0af4d6 XL |
1191 | } |
1192 | assert(!n->unmap_addr); | |
1193 | n->unmap_addr = n->addr; | |
1194 | n->addr = NULL; | |
1195 | call_rcu(n, vhost_user_host_notifier_free, rcu); | |
44866521 TB |
1196 | } |
1197 | } | |
1198 | ||
21e70425 MAL |
1199 | static int vhost_user_set_vring_base(struct vhost_dev *dev, |
1200 | struct vhost_vring_state *ring) | |
1201 | { | |
1202 | return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring); | |
1203 | } | |
1204 | ||
1205 | static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable) | |
1206 | { | |
dc3db6ad | 1207 | int i; |
21e70425 | 1208 | |
923e2d98 | 1209 | if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) { |
025faa87 | 1210 | return -EINVAL; |
5f6f6664 NN |
1211 | } |
1212 | ||
dc3db6ad | 1213 | for (i = 0; i < dev->nvqs; ++i) { |
025faa87 | 1214 | int ret; |
dc3db6ad MT |
1215 | struct vhost_vring_state state = { |
1216 | .index = dev->vq_index + i, | |
1217 | .num = enable, | |
1218 | }; | |
1219 | ||
025faa87 RK |
1220 | ret = vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state); |
1221 | if (ret < 0) { | |
1222 | /* | |
1223 | * Restoring the previous state is likely infeasible, as well as | |
1224 | * proceeding regardless the error, so just bail out and hope for | |
1225 | * the device-level recovery. | |
1226 | */ | |
1227 | return ret; | |
1228 | } | |
dc3db6ad | 1229 | } |
21e70425 | 1230 | |
dc3db6ad MT |
1231 | return 0; |
1232 | } | |
21e70425 | 1233 | |
503e3554 AB |
1234 | static VhostUserHostNotifier *fetch_notifier(VhostUserState *u, |
1235 | int idx) | |
1236 | { | |
1237 | if (idx >= u->notifiers->len) { | |
1238 | return NULL; | |
1239 | } | |
1240 | return g_ptr_array_index(u->notifiers, idx); | |
1241 | } | |
1242 | ||
21e70425 MAL |
1243 | static int vhost_user_get_vring_base(struct vhost_dev *dev, |
1244 | struct vhost_vring_state *ring) | |
1245 | { | |
025faa87 | 1246 | int ret; |
21e70425 | 1247 | VhostUserMsg msg = { |
24e34754 MT |
1248 | .hdr.request = VHOST_USER_GET_VRING_BASE, |
1249 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 1250 | .payload.state = *ring, |
24e34754 | 1251 | .hdr.size = sizeof(msg.payload.state), |
21e70425 | 1252 | }; |
0b0af4d6 | 1253 | struct vhost_user *u = dev->opaque; |
21e70425 | 1254 | |
503e3554 AB |
1255 | VhostUserHostNotifier *n = fetch_notifier(u->user, ring->index); |
1256 | if (n) { | |
1257 | vhost_user_host_notifier_remove(n, dev->vdev); | |
1258 | } | |
44866521 | 1259 | |
025faa87 RK |
1260 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1261 | if (ret < 0) { | |
1262 | return ret; | |
c4843a45 | 1263 | } |
21e70425 | 1264 | |
025faa87 RK |
1265 | ret = vhost_user_read(dev, &msg); |
1266 | if (ret < 0) { | |
1267 | return ret; | |
5f6f6664 NN |
1268 | } |
1269 | ||
24e34754 | 1270 | if (msg.hdr.request != VHOST_USER_GET_VRING_BASE) { |
21e70425 | 1271 | error_report("Received unexpected msg type. Expected %d received %d", |
24e34754 | 1272 | VHOST_USER_GET_VRING_BASE, msg.hdr.request); |
025faa87 | 1273 | return -EPROTO; |
21e70425 | 1274 | } |
5f6f6664 | 1275 | |
24e34754 | 1276 | if (msg.hdr.size != sizeof(msg.payload.state)) { |
21e70425 | 1277 | error_report("Received bad msg size."); |
025faa87 | 1278 | return -EPROTO; |
5f6f6664 NN |
1279 | } |
1280 | ||
7f4a930e | 1281 | *ring = msg.payload.state; |
21e70425 | 1282 | |
5f6f6664 NN |
1283 | return 0; |
1284 | } | |
1285 | ||
21e70425 MAL |
1286 | static int vhost_set_vring_file(struct vhost_dev *dev, |
1287 | VhostUserRequest request, | |
1288 | struct vhost_vring_file *file) | |
c2bea314 | 1289 | { |
27598393 | 1290 | int fds[VHOST_USER_MAX_RAM_SLOTS]; |
9a78a5dd | 1291 | size_t fd_num = 0; |
c2bea314 | 1292 | VhostUserMsg msg = { |
24e34754 MT |
1293 | .hdr.request = request, |
1294 | .hdr.flags = VHOST_USER_VERSION, | |
7f4a930e | 1295 | .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK, |
24e34754 | 1296 | .hdr.size = sizeof(msg.payload.u64), |
c2bea314 MAL |
1297 | }; |
1298 | ||
21e70425 MAL |
1299 | if (ioeventfd_enabled() && file->fd > 0) { |
1300 | fds[fd_num++] = file->fd; | |
1301 | } else { | |
7f4a930e | 1302 | msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK; |
9a78a5dd MAL |
1303 | } |
1304 | ||
025faa87 | 1305 | return vhost_user_write(dev, &msg, fds, fd_num); |
21e70425 | 1306 | } |
9a78a5dd | 1307 | |
21e70425 MAL |
1308 | static int vhost_user_set_vring_kick(struct vhost_dev *dev, |
1309 | struct vhost_vring_file *file) | |
1310 | { | |
1311 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file); | |
1312 | } | |
1313 | ||
1314 | static int vhost_user_set_vring_call(struct vhost_dev *dev, | |
1315 | struct vhost_vring_file *file) | |
1316 | { | |
1317 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file); | |
1318 | } | |
1319 | ||
60dc3c5b KK |
1320 | static int vhost_user_set_vring_err(struct vhost_dev *dev, |
1321 | struct vhost_vring_file *file) | |
1322 | { | |
1323 | return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_ERR, file); | |
1324 | } | |
21e70425 MAL |
1325 | |
1326 | static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64) | |
1327 | { | |
025faa87 | 1328 | int ret; |
21e70425 | 1329 | VhostUserMsg msg = { |
24e34754 MT |
1330 | .hdr.request = request, |
1331 | .hdr.flags = VHOST_USER_VERSION, | |
21e70425 MAL |
1332 | }; |
1333 | ||
1334 | if (vhost_user_one_time_request(request) && dev->vq_index != 0) { | |
1335 | return 0; | |
9a78a5dd | 1336 | } |
c2bea314 | 1337 | |
025faa87 RK |
1338 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1339 | if (ret < 0) { | |
1340 | return ret; | |
c4843a45 | 1341 | } |
21e70425 | 1342 | |
025faa87 RK |
1343 | ret = vhost_user_read(dev, &msg); |
1344 | if (ret < 0) { | |
1345 | return ret; | |
21e70425 MAL |
1346 | } |
1347 | ||
24e34754 | 1348 | if (msg.hdr.request != request) { |
21e70425 | 1349 | error_report("Received unexpected msg type. Expected %d received %d", |
24e34754 | 1350 | request, msg.hdr.request); |
025faa87 | 1351 | return -EPROTO; |
21e70425 MAL |
1352 | } |
1353 | ||
24e34754 | 1354 | if (msg.hdr.size != sizeof(msg.payload.u64)) { |
21e70425 | 1355 | error_report("Received bad msg size."); |
025faa87 | 1356 | return -EPROTO; |
21e70425 MAL |
1357 | } |
1358 | ||
7f4a930e | 1359 | *u64 = msg.payload.u64; |
21e70425 MAL |
1360 | |
1361 | return 0; | |
1362 | } | |
1363 | ||
1364 | static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features) | |
1365 | { | |
f2a6e6c4 KW |
1366 | if (vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features) < 0) { |
1367 | return -EPROTO; | |
1368 | } | |
1369 | ||
1370 | return 0; | |
21e70425 MAL |
1371 | } |
1372 | ||
699f2e53 DP |
1373 | static int enforce_reply(struct vhost_dev *dev, |
1374 | const VhostUserMsg *msg) | |
1375 | { | |
1376 | uint64_t dummy; | |
1377 | ||
1378 | if (msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) { | |
1379 | return process_message_reply(dev, msg); | |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * We need to wait for a reply but the backend does not | |
1384 | * support replies for the command we just sent. | |
1385 | * Send VHOST_USER_GET_FEATURES which makes all backends | |
1386 | * send a reply. | |
1387 | */ | |
1388 | return vhost_user_get_features(dev, &dummy); | |
1389 | } | |
1390 | ||
1391 | static int vhost_user_set_vring_addr(struct vhost_dev *dev, | |
1392 | struct vhost_vring_addr *addr) | |
1393 | { | |
025faa87 | 1394 | int ret; |
699f2e53 DP |
1395 | VhostUserMsg msg = { |
1396 | .hdr.request = VHOST_USER_SET_VRING_ADDR, | |
1397 | .hdr.flags = VHOST_USER_VERSION, | |
1398 | .payload.addr = *addr, | |
1399 | .hdr.size = sizeof(msg.payload.addr), | |
1400 | }; | |
1401 | ||
1402 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
1403 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
1404 | ||
1405 | /* | |
1406 | * wait for a reply if logging is enabled to make sure | |
1407 | * backend is actually logging changes | |
1408 | */ | |
1409 | bool wait_for_reply = addr->flags & (1 << VHOST_VRING_F_LOG); | |
1410 | ||
1411 | if (reply_supported && wait_for_reply) { | |
1412 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; | |
1413 | } | |
1414 | ||
025faa87 RK |
1415 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1416 | if (ret < 0) { | |
1417 | return ret; | |
699f2e53 DP |
1418 | } |
1419 | ||
1420 | if (wait_for_reply) { | |
1421 | return enforce_reply(dev, &msg); | |
1422 | } | |
1423 | ||
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64, | |
1428 | bool wait_for_reply) | |
1429 | { | |
1430 | VhostUserMsg msg = { | |
1431 | .hdr.request = request, | |
1432 | .hdr.flags = VHOST_USER_VERSION, | |
1433 | .payload.u64 = u64, | |
1434 | .hdr.size = sizeof(msg.payload.u64), | |
1435 | }; | |
025faa87 | 1436 | int ret; |
699f2e53 DP |
1437 | |
1438 | if (wait_for_reply) { | |
1439 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
1440 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
1441 | if (reply_supported) { | |
1442 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; | |
1443 | } | |
1444 | } | |
1445 | ||
025faa87 RK |
1446 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1447 | if (ret < 0) { | |
1448 | return ret; | |
699f2e53 DP |
1449 | } |
1450 | ||
1451 | if (wait_for_reply) { | |
1452 | return enforce_reply(dev, &msg); | |
1453 | } | |
1454 | ||
1455 | return 0; | |
1456 | } | |
1457 | ||
923b8921 YW |
1458 | static int vhost_user_set_status(struct vhost_dev *dev, uint8_t status) |
1459 | { | |
1460 | return vhost_user_set_u64(dev, VHOST_USER_SET_STATUS, status, false); | |
1461 | } | |
1462 | ||
1463 | static int vhost_user_get_status(struct vhost_dev *dev, uint8_t *status) | |
1464 | { | |
1465 | uint64_t value; | |
1466 | int ret; | |
1467 | ||
1468 | ret = vhost_user_get_u64(dev, VHOST_USER_GET_STATUS, &value); | |
1469 | if (ret < 0) { | |
1470 | return ret; | |
1471 | } | |
1472 | *status = value; | |
1473 | ||
1474 | return 0; | |
1475 | } | |
1476 | ||
1477 | static int vhost_user_add_status(struct vhost_dev *dev, uint8_t status) | |
1478 | { | |
1479 | uint8_t s; | |
1480 | int ret; | |
1481 | ||
1482 | ret = vhost_user_get_status(dev, &s); | |
1483 | if (ret < 0) { | |
1484 | return ret; | |
1485 | } | |
1486 | ||
1487 | if ((s & status) == status) { | |
1488 | return 0; | |
1489 | } | |
1490 | s |= status; | |
1491 | ||
1492 | return vhost_user_set_status(dev, s); | |
1493 | } | |
1494 | ||
699f2e53 DP |
1495 | static int vhost_user_set_features(struct vhost_dev *dev, |
1496 | uint64_t features) | |
1497 | { | |
1498 | /* | |
1499 | * wait for a reply if logging is enabled to make sure | |
1500 | * backend is actually logging changes | |
1501 | */ | |
1502 | bool log_enabled = features & (0x1ULL << VHOST_F_LOG_ALL); | |
923b8921 | 1503 | int ret; |
699f2e53 | 1504 | |
02b61f38 AB |
1505 | /* |
1506 | * We need to include any extra backend only feature bits that | |
1507 | * might be needed by our device. Currently this includes the | |
1508 | * VHOST_USER_F_PROTOCOL_FEATURES bit for enabling protocol | |
1509 | * features. | |
1510 | */ | |
923b8921 | 1511 | ret = vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, |
02b61f38 | 1512 | features | dev->backend_features, |
699f2e53 | 1513 | log_enabled); |
923b8921 YW |
1514 | |
1515 | if (virtio_has_feature(dev->protocol_features, | |
1516 | VHOST_USER_PROTOCOL_F_STATUS)) { | |
1517 | if (!ret) { | |
1518 | return vhost_user_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK); | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | return ret; | |
699f2e53 DP |
1523 | } |
1524 | ||
1525 | static int vhost_user_set_protocol_features(struct vhost_dev *dev, | |
1526 | uint64_t features) | |
1527 | { | |
1528 | return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features, | |
1529 | false); | |
1530 | } | |
1531 | ||
21e70425 MAL |
1532 | static int vhost_user_set_owner(struct vhost_dev *dev) |
1533 | { | |
1534 | VhostUserMsg msg = { | |
24e34754 MT |
1535 | .hdr.request = VHOST_USER_SET_OWNER, |
1536 | .hdr.flags = VHOST_USER_VERSION, | |
21e70425 MAL |
1537 | }; |
1538 | ||
025faa87 | 1539 | return vhost_user_write(dev, &msg, NULL, 0); |
21e70425 MAL |
1540 | } |
1541 | ||
6b0eff1a RN |
1542 | static int vhost_user_get_max_memslots(struct vhost_dev *dev, |
1543 | uint64_t *max_memslots) | |
1544 | { | |
1545 | uint64_t backend_max_memslots; | |
1546 | int err; | |
1547 | ||
1548 | err = vhost_user_get_u64(dev, VHOST_USER_GET_MAX_MEM_SLOTS, | |
1549 | &backend_max_memslots); | |
1550 | if (err < 0) { | |
1551 | return err; | |
1552 | } | |
1553 | ||
1554 | *max_memslots = backend_max_memslots; | |
1555 | ||
1556 | return 0; | |
1557 | } | |
1558 | ||
21e70425 MAL |
1559 | static int vhost_user_reset_device(struct vhost_dev *dev) |
1560 | { | |
1561 | VhostUserMsg msg = { | |
24e34754 | 1562 | .hdr.flags = VHOST_USER_VERSION, |
21e70425 MAL |
1563 | }; |
1564 | ||
d91d57e6 RN |
1565 | msg.hdr.request = virtio_has_feature(dev->protocol_features, |
1566 | VHOST_USER_PROTOCOL_F_RESET_DEVICE) | |
1567 | ? VHOST_USER_RESET_DEVICE | |
1568 | : VHOST_USER_RESET_OWNER; | |
1569 | ||
025faa87 | 1570 | return vhost_user_write(dev, &msg, NULL, 0); |
c2bea314 MAL |
1571 | } |
1572 | ||
4c3e257b CL |
1573 | static int vhost_user_slave_handle_config_change(struct vhost_dev *dev) |
1574 | { | |
025faa87 RK |
1575 | if (!dev->config_ops || !dev->config_ops->vhost_dev_config_notifier) { |
1576 | return -ENOSYS; | |
4c3e257b CL |
1577 | } |
1578 | ||
025faa87 | 1579 | return dev->config_ops->vhost_dev_config_notifier(dev); |
4c3e257b CL |
1580 | } |
1581 | ||
503e3554 AB |
1582 | /* |
1583 | * Fetch or create the notifier for a given idx. Newly created | |
1584 | * notifiers are added to the pointer array that tracks them. | |
1585 | */ | |
1586 | static VhostUserHostNotifier *fetch_or_create_notifier(VhostUserState *u, | |
1587 | int idx) | |
1588 | { | |
1589 | VhostUserHostNotifier *n = NULL; | |
1590 | if (idx >= u->notifiers->len) { | |
b595d627 | 1591 | g_ptr_array_set_size(u->notifiers, idx + 1); |
503e3554 AB |
1592 | } |
1593 | ||
1594 | n = g_ptr_array_index(u->notifiers, idx); | |
1595 | if (!n) { | |
bd437c96 YW |
1596 | /* |
1597 | * In case notification arrive out-of-order, | |
1598 | * make room for current index. | |
1599 | */ | |
1600 | g_ptr_array_remove_index(u->notifiers, idx); | |
503e3554 AB |
1601 | n = g_new0(VhostUserHostNotifier, 1); |
1602 | n->idx = idx; | |
1603 | g_ptr_array_insert(u->notifiers, idx, n); | |
1604 | trace_vhost_user_create_notifier(idx, n); | |
1605 | } | |
1606 | ||
1607 | return n; | |
1608 | } | |
1609 | ||
44866521 TB |
1610 | static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev *dev, |
1611 | VhostUserVringArea *area, | |
1612 | int fd) | |
1613 | { | |
1614 | int queue_idx = area->u64 & VHOST_USER_VRING_IDX_MASK; | |
8e3b0cbb | 1615 | size_t page_size = qemu_real_host_page_size(); |
44866521 TB |
1616 | struct vhost_user *u = dev->opaque; |
1617 | VhostUserState *user = u->user; | |
1618 | VirtIODevice *vdev = dev->vdev; | |
1619 | VhostUserHostNotifier *n; | |
1620 | void *addr; | |
1621 | char *name; | |
1622 | ||
1623 | if (!virtio_has_feature(dev->protocol_features, | |
1624 | VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) || | |
1625 | vdev == NULL || queue_idx >= virtio_get_num_queues(vdev)) { | |
025faa87 | 1626 | return -EINVAL; |
44866521 TB |
1627 | } |
1628 | ||
503e3554 AB |
1629 | /* |
1630 | * Fetch notifier and invalidate any old data before setting up | |
1631 | * new mapped address. | |
1632 | */ | |
1633 | n = fetch_or_create_notifier(user, queue_idx); | |
1634 | vhost_user_host_notifier_remove(n, vdev); | |
44866521 TB |
1635 | |
1636 | if (area->u64 & VHOST_USER_VRING_NOFD_MASK) { | |
1637 | return 0; | |
1638 | } | |
1639 | ||
1640 | /* Sanity check. */ | |
1641 | if (area->size != page_size) { | |
025faa87 | 1642 | return -EINVAL; |
44866521 TB |
1643 | } |
1644 | ||
1645 | addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, | |
1646 | fd, area->offset); | |
1647 | if (addr == MAP_FAILED) { | |
025faa87 | 1648 | return -EFAULT; |
44866521 TB |
1649 | } |
1650 | ||
1651 | name = g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]", | |
1652 | user, queue_idx); | |
0b0af4d6 | 1653 | if (!n->mr.ram) { /* Don't init again after suspend. */ |
a1ed9ef1 XL |
1654 | memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name, |
1655 | page_size, addr); | |
0b0af4d6 XL |
1656 | } else { |
1657 | n->mr.ram_block->host = addr; | |
1658 | } | |
44866521 TB |
1659 | g_free(name); |
1660 | ||
1661 | if (virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true)) { | |
1f89d3b9 | 1662 | object_unparent(OBJECT(&n->mr)); |
44866521 | 1663 | munmap(addr, page_size); |
025faa87 | 1664 | return -ENXIO; |
44866521 TB |
1665 | } |
1666 | ||
1667 | n->addr = addr; | |
44866521 TB |
1668 | |
1669 | return 0; | |
1670 | } | |
1671 | ||
de62e494 GK |
1672 | static void close_slave_channel(struct vhost_user *u) |
1673 | { | |
57dc0217 GK |
1674 | g_source_destroy(u->slave_src); |
1675 | g_source_unref(u->slave_src); | |
1676 | u->slave_src = NULL; | |
1677 | object_unref(OBJECT(u->slave_ioc)); | |
1678 | u->slave_ioc = NULL; | |
de62e494 GK |
1679 | } |
1680 | ||
57dc0217 GK |
1681 | static gboolean slave_read(QIOChannel *ioc, GIOCondition condition, |
1682 | gpointer opaque) | |
4bbeeba0 MAL |
1683 | { |
1684 | struct vhost_dev *dev = opaque; | |
1685 | struct vhost_user *u = dev->opaque; | |
69aff030 MT |
1686 | VhostUserHeader hdr = { 0, }; |
1687 | VhostUserPayload payload = { 0, }; | |
57dc0217 GK |
1688 | Error *local_err = NULL; |
1689 | gboolean rc = G_SOURCE_CONTINUE; | |
1690 | int ret = 0; | |
1f3a4519 | 1691 | struct iovec iov; |
57dc0217 GK |
1692 | g_autofree int *fd = NULL; |
1693 | size_t fdsize = 0; | |
1694 | int i; | |
5f57fbea | 1695 | |
4bbeeba0 | 1696 | /* Read header */ |
1f3a4519 TB |
1697 | iov.iov_base = &hdr; |
1698 | iov.iov_len = VHOST_USER_HDR_SIZE; | |
1699 | ||
57dc0217 GK |
1700 | if (qio_channel_readv_full_all(ioc, &iov, 1, &fd, &fdsize, &local_err)) { |
1701 | error_report_err(local_err); | |
4bbeeba0 MAL |
1702 | goto err; |
1703 | } | |
1704 | ||
69aff030 | 1705 | if (hdr.size > VHOST_USER_PAYLOAD_SIZE) { |
4bbeeba0 | 1706 | error_report("Failed to read msg header." |
69aff030 | 1707 | " Size %d exceeds the maximum %zu.", hdr.size, |
4bbeeba0 MAL |
1708 | VHOST_USER_PAYLOAD_SIZE); |
1709 | goto err; | |
1710 | } | |
1711 | ||
1712 | /* Read payload */ | |
57dc0217 GK |
1713 | if (qio_channel_read_all(ioc, (char *) &payload, hdr.size, &local_err)) { |
1714 | error_report_err(local_err); | |
4bbeeba0 MAL |
1715 | goto err; |
1716 | } | |
1717 | ||
69aff030 | 1718 | switch (hdr.request) { |
6dcdd06e | 1719 | case VHOST_USER_SLAVE_IOTLB_MSG: |
69aff030 | 1720 | ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb); |
6dcdd06e | 1721 | break; |
4c3e257b CL |
1722 | case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG : |
1723 | ret = vhost_user_slave_handle_config_change(dev); | |
1724 | break; | |
44866521 TB |
1725 | case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG: |
1726 | ret = vhost_user_slave_handle_vring_host_notifier(dev, &payload.area, | |
57dc0217 | 1727 | fd ? fd[0] : -1); |
44866521 | 1728 | break; |
4bbeeba0 | 1729 | default: |
0fdc465d | 1730 | error_report("Received unexpected msg type: %d.", hdr.request); |
4bbeeba0 MAL |
1731 | ret = -EINVAL; |
1732 | } | |
1733 | ||
1734 | /* | |
1735 | * REPLY_ACK feature handling. Other reply types has to be managed | |
1736 | * directly in their request handlers. | |
1737 | */ | |
69aff030 MT |
1738 | if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) { |
1739 | struct iovec iovec[2]; | |
4bbeeba0 | 1740 | |
4bbeeba0 | 1741 | |
69aff030 MT |
1742 | hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK; |
1743 | hdr.flags |= VHOST_USER_REPLY_MASK; | |
1744 | ||
1745 | payload.u64 = !!ret; | |
1746 | hdr.size = sizeof(payload.u64); | |
1747 | ||
1748 | iovec[0].iov_base = &hdr; | |
1749 | iovec[0].iov_len = VHOST_USER_HDR_SIZE; | |
1750 | iovec[1].iov_base = &payload; | |
1751 | iovec[1].iov_len = hdr.size; | |
1752 | ||
57dc0217 GK |
1753 | if (qio_channel_writev_all(ioc, iovec, ARRAY_SIZE(iovec), &local_err)) { |
1754 | error_report_err(local_err); | |
4bbeeba0 MAL |
1755 | goto err; |
1756 | } | |
1757 | } | |
1758 | ||
9e06080b | 1759 | goto fdcleanup; |
4bbeeba0 MAL |
1760 | |
1761 | err: | |
de62e494 | 1762 | close_slave_channel(u); |
57dc0217 | 1763 | rc = G_SOURCE_REMOVE; |
9e06080b GK |
1764 | |
1765 | fdcleanup: | |
57dc0217 GK |
1766 | if (fd) { |
1767 | for (i = 0; i < fdsize; i++) { | |
5f57fbea TB |
1768 | close(fd[i]); |
1769 | } | |
1f3a4519 | 1770 | } |
57dc0217 | 1771 | return rc; |
4bbeeba0 MAL |
1772 | } |
1773 | ||
1774 | static int vhost_setup_slave_channel(struct vhost_dev *dev) | |
1775 | { | |
1776 | VhostUserMsg msg = { | |
24e34754 MT |
1777 | .hdr.request = VHOST_USER_SET_SLAVE_REQ_FD, |
1778 | .hdr.flags = VHOST_USER_VERSION, | |
4bbeeba0 MAL |
1779 | }; |
1780 | struct vhost_user *u = dev->opaque; | |
1781 | int sv[2], ret = 0; | |
1782 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
1783 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
57dc0217 GK |
1784 | Error *local_err = NULL; |
1785 | QIOChannel *ioc; | |
4bbeeba0 MAL |
1786 | |
1787 | if (!virtio_has_feature(dev->protocol_features, | |
1788 | VHOST_USER_PROTOCOL_F_SLAVE_REQ)) { | |
1789 | return 0; | |
1790 | } | |
1791 | ||
9cbda7b3 | 1792 | if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
025faa87 | 1793 | int saved_errno = errno; |
4bbeeba0 | 1794 | error_report("socketpair() failed"); |
025faa87 | 1795 | return -saved_errno; |
4bbeeba0 MAL |
1796 | } |
1797 | ||
57dc0217 GK |
1798 | ioc = QIO_CHANNEL(qio_channel_socket_new_fd(sv[0], &local_err)); |
1799 | if (!ioc) { | |
1800 | error_report_err(local_err); | |
025faa87 | 1801 | return -ECONNREFUSED; |
57dc0217 GK |
1802 | } |
1803 | u->slave_ioc = ioc; | |
db8a3772 | 1804 | slave_update_read_handler(dev, NULL); |
4bbeeba0 MAL |
1805 | |
1806 | if (reply_supported) { | |
24e34754 | 1807 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
4bbeeba0 MAL |
1808 | } |
1809 | ||
1810 | ret = vhost_user_write(dev, &msg, &sv[1], 1); | |
1811 | if (ret) { | |
1812 | goto out; | |
1813 | } | |
1814 | ||
1815 | if (reply_supported) { | |
1816 | ret = process_message_reply(dev, &msg); | |
1817 | } | |
1818 | ||
1819 | out: | |
1820 | close(sv[1]); | |
1821 | if (ret) { | |
de62e494 | 1822 | close_slave_channel(u); |
4bbeeba0 MAL |
1823 | } |
1824 | ||
1825 | return ret; | |
1826 | } | |
1827 | ||
18658a3c | 1828 | #ifdef CONFIG_LINUX |
f82c1116 DDAG |
1829 | /* |
1830 | * Called back from the postcopy fault thread when a fault is received on our | |
1831 | * ufd. | |
1832 | * TODO: This is Linux specific | |
1833 | */ | |
1834 | static int vhost_user_postcopy_fault_handler(struct PostCopyFD *pcfd, | |
1835 | void *ufd) | |
1836 | { | |
375318d0 DDAG |
1837 | struct vhost_dev *dev = pcfd->data; |
1838 | struct vhost_user *u = dev->opaque; | |
1839 | struct uffd_msg *msg = ufd; | |
1840 | uint64_t faultaddr = msg->arg.pagefault.address; | |
1841 | RAMBlock *rb = NULL; | |
1842 | uint64_t rb_offset; | |
1843 | int i; | |
1844 | ||
1845 | trace_vhost_user_postcopy_fault_handler(pcfd->idstr, faultaddr, | |
1846 | dev->mem->nregions); | |
1847 | for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) { | |
1848 | trace_vhost_user_postcopy_fault_handler_loop(i, | |
1849 | u->postcopy_client_bases[i], dev->mem->regions[i].memory_size); | |
1850 | if (faultaddr >= u->postcopy_client_bases[i]) { | |
1851 | /* Ofset of the fault address in the vhost region */ | |
1852 | uint64_t region_offset = faultaddr - u->postcopy_client_bases[i]; | |
1853 | if (region_offset < dev->mem->regions[i].memory_size) { | |
1854 | rb_offset = region_offset + u->region_rb_offset[i]; | |
1855 | trace_vhost_user_postcopy_fault_handler_found(i, | |
1856 | region_offset, rb_offset); | |
1857 | rb = u->region_rb[i]; | |
1858 | return postcopy_request_shared_page(pcfd, rb, faultaddr, | |
1859 | rb_offset); | |
1860 | } | |
1861 | } | |
1862 | } | |
1863 | error_report("%s: Failed to find region for fault %" PRIx64, | |
1864 | __func__, faultaddr); | |
1865 | return -1; | |
f82c1116 DDAG |
1866 | } |
1867 | ||
c07e3615 DDAG |
1868 | static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb, |
1869 | uint64_t offset) | |
1870 | { | |
1871 | struct vhost_dev *dev = pcfd->data; | |
1872 | struct vhost_user *u = dev->opaque; | |
1873 | int i; | |
1874 | ||
1875 | trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb), offset); | |
1876 | ||
1877 | if (!u) { | |
1878 | return 0; | |
1879 | } | |
1880 | /* Translate the offset into an address in the clients address space */ | |
1881 | for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) { | |
1882 | if (u->region_rb[i] == rb && | |
1883 | offset >= u->region_rb_offset[i] && | |
1884 | offset < (u->region_rb_offset[i] + | |
1885 | dev->mem->regions[i].memory_size)) { | |
1886 | uint64_t client_addr = (offset - u->region_rb_offset[i]) + | |
1887 | u->postcopy_client_bases[i]; | |
1888 | trace_vhost_user_postcopy_waker_found(client_addr); | |
1889 | return postcopy_wake_shared(pcfd, client_addr, rb); | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb), offset); | |
1894 | return 0; | |
1895 | } | |
18658a3c | 1896 | #endif |
c07e3615 | 1897 | |
d3dff7a5 DDAG |
1898 | /* |
1899 | * Called at the start of an inbound postcopy on reception of the | |
1900 | * 'advise' command. | |
1901 | */ | |
1902 | static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp) | |
1903 | { | |
18658a3c | 1904 | #ifdef CONFIG_LINUX |
d3dff7a5 | 1905 | struct vhost_user *u = dev->opaque; |
4d0cf552 | 1906 | CharBackend *chr = u->user->chr; |
d3dff7a5 | 1907 | int ufd; |
025faa87 | 1908 | int ret; |
d3dff7a5 DDAG |
1909 | VhostUserMsg msg = { |
1910 | .hdr.request = VHOST_USER_POSTCOPY_ADVISE, | |
1911 | .hdr.flags = VHOST_USER_VERSION, | |
1912 | }; | |
1913 | ||
025faa87 RK |
1914 | ret = vhost_user_write(dev, &msg, NULL, 0); |
1915 | if (ret < 0) { | |
d3dff7a5 | 1916 | error_setg(errp, "Failed to send postcopy_advise to vhost"); |
025faa87 | 1917 | return ret; |
d3dff7a5 DDAG |
1918 | } |
1919 | ||
025faa87 RK |
1920 | ret = vhost_user_read(dev, &msg); |
1921 | if (ret < 0) { | |
d3dff7a5 | 1922 | error_setg(errp, "Failed to get postcopy_advise reply from vhost"); |
025faa87 | 1923 | return ret; |
d3dff7a5 DDAG |
1924 | } |
1925 | ||
1926 | if (msg.hdr.request != VHOST_USER_POSTCOPY_ADVISE) { | |
1927 | error_setg(errp, "Unexpected msg type. Expected %d received %d", | |
1928 | VHOST_USER_POSTCOPY_ADVISE, msg.hdr.request); | |
025faa87 | 1929 | return -EPROTO; |
d3dff7a5 DDAG |
1930 | } |
1931 | ||
1932 | if (msg.hdr.size) { | |
1933 | error_setg(errp, "Received bad msg size."); | |
025faa87 | 1934 | return -EPROTO; |
d3dff7a5 DDAG |
1935 | } |
1936 | ufd = qemu_chr_fe_get_msgfd(chr); | |
1937 | if (ufd < 0) { | |
1938 | error_setg(errp, "%s: Failed to get ufd", __func__); | |
025faa87 | 1939 | return -EIO; |
d3dff7a5 | 1940 | } |
ff5927ba | 1941 | qemu_socket_set_nonblock(ufd); |
d3dff7a5 | 1942 | |
f82c1116 DDAG |
1943 | /* register ufd with userfault thread */ |
1944 | u->postcopy_fd.fd = ufd; | |
1945 | u->postcopy_fd.data = dev; | |
1946 | u->postcopy_fd.handler = vhost_user_postcopy_fault_handler; | |
c07e3615 | 1947 | u->postcopy_fd.waker = vhost_user_postcopy_waker; |
f82c1116 DDAG |
1948 | u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */ |
1949 | postcopy_register_shared_ufd(&u->postcopy_fd); | |
d3dff7a5 | 1950 | return 0; |
18658a3c PB |
1951 | #else |
1952 | error_setg(errp, "Postcopy not supported on non-Linux systems"); | |
025faa87 | 1953 | return -ENOSYS; |
18658a3c | 1954 | #endif |
d3dff7a5 DDAG |
1955 | } |
1956 | ||
6864a7b5 DDAG |
1957 | /* |
1958 | * Called at the switch to postcopy on reception of the 'listen' command. | |
1959 | */ | |
1960 | static int vhost_user_postcopy_listen(struct vhost_dev *dev, Error **errp) | |
1961 | { | |
1962 | struct vhost_user *u = dev->opaque; | |
1963 | int ret; | |
1964 | VhostUserMsg msg = { | |
1965 | .hdr.request = VHOST_USER_POSTCOPY_LISTEN, | |
1966 | .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, | |
1967 | }; | |
1968 | u->postcopy_listen = true; | |
025faa87 | 1969 | |
6864a7b5 | 1970 | trace_vhost_user_postcopy_listen(); |
025faa87 RK |
1971 | |
1972 | ret = vhost_user_write(dev, &msg, NULL, 0); | |
1973 | if (ret < 0) { | |
6864a7b5 | 1974 | error_setg(errp, "Failed to send postcopy_listen to vhost"); |
025faa87 | 1975 | return ret; |
6864a7b5 DDAG |
1976 | } |
1977 | ||
1978 | ret = process_message_reply(dev, &msg); | |
1979 | if (ret) { | |
1980 | error_setg(errp, "Failed to receive reply to postcopy_listen"); | |
1981 | return ret; | |
1982 | } | |
1983 | ||
1984 | return 0; | |
1985 | } | |
1986 | ||
46343570 DDAG |
1987 | /* |
1988 | * Called at the end of postcopy | |
1989 | */ | |
1990 | static int vhost_user_postcopy_end(struct vhost_dev *dev, Error **errp) | |
1991 | { | |
1992 | VhostUserMsg msg = { | |
1993 | .hdr.request = VHOST_USER_POSTCOPY_END, | |
1994 | .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, | |
1995 | }; | |
1996 | int ret; | |
1997 | struct vhost_user *u = dev->opaque; | |
1998 | ||
1999 | trace_vhost_user_postcopy_end_entry(); | |
025faa87 RK |
2000 | |
2001 | ret = vhost_user_write(dev, &msg, NULL, 0); | |
2002 | if (ret < 0) { | |
46343570 | 2003 | error_setg(errp, "Failed to send postcopy_end to vhost"); |
025faa87 | 2004 | return ret; |
46343570 DDAG |
2005 | } |
2006 | ||
2007 | ret = process_message_reply(dev, &msg); | |
2008 | if (ret) { | |
2009 | error_setg(errp, "Failed to receive reply to postcopy_end"); | |
2010 | return ret; | |
2011 | } | |
2012 | postcopy_unregister_shared_ufd(&u->postcopy_fd); | |
c4f75385 | 2013 | close(u->postcopy_fd.fd); |
46343570 DDAG |
2014 | u->postcopy_fd.handler = NULL; |
2015 | ||
2016 | trace_vhost_user_postcopy_end_exit(); | |
2017 | ||
2018 | return 0; | |
2019 | } | |
2020 | ||
9ccbfe14 DDAG |
2021 | static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier, |
2022 | void *opaque) | |
2023 | { | |
2024 | struct PostcopyNotifyData *pnd = opaque; | |
2025 | struct vhost_user *u = container_of(notifier, struct vhost_user, | |
2026 | postcopy_notifier); | |
2027 | struct vhost_dev *dev = u->dev; | |
2028 | ||
2029 | switch (pnd->reason) { | |
2030 | case POSTCOPY_NOTIFY_PROBE: | |
2031 | if (!virtio_has_feature(dev->protocol_features, | |
2032 | VHOST_USER_PROTOCOL_F_PAGEFAULT)) { | |
2033 | /* TODO: Get the device name into this error somehow */ | |
2034 | error_setg(pnd->errp, | |
2035 | "vhost-user backend not capable of postcopy"); | |
2036 | return -ENOENT; | |
2037 | } | |
2038 | break; | |
2039 | ||
d3dff7a5 DDAG |
2040 | case POSTCOPY_NOTIFY_INBOUND_ADVISE: |
2041 | return vhost_user_postcopy_advise(dev, pnd->errp); | |
2042 | ||
6864a7b5 DDAG |
2043 | case POSTCOPY_NOTIFY_INBOUND_LISTEN: |
2044 | return vhost_user_postcopy_listen(dev, pnd->errp); | |
2045 | ||
46343570 DDAG |
2046 | case POSTCOPY_NOTIFY_INBOUND_END: |
2047 | return vhost_user_postcopy_end(dev, pnd->errp); | |
2048 | ||
9ccbfe14 DDAG |
2049 | default: |
2050 | /* We ignore notifications we don't know */ | |
2051 | break; | |
2052 | } | |
2053 | ||
2054 | return 0; | |
2055 | } | |
2056 | ||
28770ff9 KW |
2057 | static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque, |
2058 | Error **errp) | |
5f6f6664 | 2059 | { |
56534930 | 2060 | uint64_t features, ram_slots; |
2152f3fe | 2061 | struct vhost_user *u; |
56534930 | 2062 | VhostUserState *vus = (VhostUserState *) opaque; |
dcb10c00 MT |
2063 | int err; |
2064 | ||
5f6f6664 NN |
2065 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
2066 | ||
2152f3fe | 2067 | u = g_new0(struct vhost_user, 1); |
56534930 | 2068 | u->user = vus; |
9ccbfe14 | 2069 | u->dev = dev; |
2152f3fe | 2070 | dev->opaque = u; |
5f6f6664 | 2071 | |
21e70425 | 2072 | err = vhost_user_get_features(dev, &features); |
dcb10c00 | 2073 | if (err < 0) { |
998647dc | 2074 | error_setg_errno(errp, -err, "vhost_backend_init failed"); |
f2a6e6c4 | 2075 | return err; |
dcb10c00 MT |
2076 | } |
2077 | ||
2078 | if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) { | |
56534930 AB |
2079 | bool supports_f_config = vus->supports_config || |
2080 | (dev->config_ops && dev->config_ops->vhost_dev_config_notifier); | |
2081 | uint64_t protocol_features; | |
2082 | ||
dcb10c00 MT |
2083 | dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES; |
2084 | ||
21e70425 | 2085 | err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES, |
6dcdd06e | 2086 | &protocol_features); |
dcb10c00 | 2087 | if (err < 0) { |
998647dc | 2088 | error_setg_errno(errp, EPROTO, "vhost_backend_init failed"); |
28770ff9 | 2089 | return -EPROTO; |
dcb10c00 MT |
2090 | } |
2091 | ||
56534930 AB |
2092 | /* |
2093 | * We will use all the protocol features we support - although | |
2094 | * we suppress F_CONFIG if we know QEMUs internal code can not support | |
2095 | * it. | |
2096 | */ | |
2097 | protocol_features &= VHOST_USER_PROTOCOL_FEATURE_MASK; | |
2098 | ||
2099 | if (supports_f_config) { | |
2100 | if (!virtio_has_feature(protocol_features, | |
2101 | VHOST_USER_PROTOCOL_F_CONFIG)) { | |
fb38d0c9 | 2102 | error_setg(errp, "vhost-user device expecting " |
56534930 | 2103 | "VHOST_USER_PROTOCOL_F_CONFIG but the vhost-user backend does " |
fb38d0c9 | 2104 | "not support it."); |
56534930 AB |
2105 | return -EPROTO; |
2106 | } | |
2107 | } else { | |
2108 | if (virtio_has_feature(protocol_features, | |
2109 | VHOST_USER_PROTOCOL_F_CONFIG)) { | |
2110 | warn_reportf_err(*errp, "vhost-user backend supports " | |
fb38d0c9 | 2111 | "VHOST_USER_PROTOCOL_F_CONFIG but QEMU does not."); |
56534930 AB |
2112 | protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG); |
2113 | } | |
1c3e5a26 MC |
2114 | } |
2115 | ||
56534930 AB |
2116 | /* final set of protocol features */ |
2117 | dev->protocol_features = protocol_features; | |
21e70425 | 2118 | err = vhost_user_set_protocol_features(dev, dev->protocol_features); |
dcb10c00 | 2119 | if (err < 0) { |
998647dc | 2120 | error_setg_errno(errp, EPROTO, "vhost_backend_init failed"); |
28770ff9 | 2121 | return -EPROTO; |
dcb10c00 | 2122 | } |
e2051e9e YL |
2123 | |
2124 | /* query the max queues we support if backend supports Multiple Queue */ | |
2125 | if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) { | |
21e70425 MAL |
2126 | err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM, |
2127 | &dev->max_queues); | |
e2051e9e | 2128 | if (err < 0) { |
998647dc | 2129 | error_setg_errno(errp, EPROTO, "vhost_backend_init failed"); |
28770ff9 | 2130 | return -EPROTO; |
e2051e9e | 2131 | } |
84affad1 KW |
2132 | } else { |
2133 | dev->max_queues = 1; | |
e2051e9e | 2134 | } |
84affad1 | 2135 | |
c90bd505 | 2136 | if (dev->num_queues && dev->max_queues < dev->num_queues) { |
28770ff9 KW |
2137 | error_setg(errp, "The maximum number of queues supported by the " |
2138 | "backend is %" PRIu64, dev->max_queues); | |
c90bd505 KW |
2139 | return -EINVAL; |
2140 | } | |
6dcdd06e MC |
2141 | |
2142 | if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) && | |
2143 | !(virtio_has_feature(dev->protocol_features, | |
2144 | VHOST_USER_PROTOCOL_F_SLAVE_REQ) && | |
2145 | virtio_has_feature(dev->protocol_features, | |
2146 | VHOST_USER_PROTOCOL_F_REPLY_ACK))) { | |
28770ff9 KW |
2147 | error_setg(errp, "IOMMU support requires reply-ack and " |
2148 | "slave-req protocol features."); | |
2149 | return -EINVAL; | |
6dcdd06e | 2150 | } |
6b0eff1a RN |
2151 | |
2152 | /* get max memory regions if backend supports configurable RAM slots */ | |
2153 | if (!virtio_has_feature(dev->protocol_features, | |
2154 | VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS)) { | |
27598393 | 2155 | u->user->memory_slots = VHOST_MEMORY_BASELINE_NREGIONS; |
6b0eff1a RN |
2156 | } else { |
2157 | err = vhost_user_get_max_memslots(dev, &ram_slots); | |
2158 | if (err < 0) { | |
998647dc | 2159 | error_setg_errno(errp, EPROTO, "vhost_backend_init failed"); |
28770ff9 | 2160 | return -EPROTO; |
6b0eff1a RN |
2161 | } |
2162 | ||
2163 | if (ram_slots < u->user->memory_slots) { | |
28770ff9 KW |
2164 | error_setg(errp, "The backend specified a max ram slots limit " |
2165 | "of %" PRIu64", when the prior validated limit was " | |
2166 | "%d. This limit should never decrease.", ram_slots, | |
2167 | u->user->memory_slots); | |
2168 | return -EINVAL; | |
6b0eff1a RN |
2169 | } |
2170 | ||
27598393 | 2171 | u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS); |
6b0eff1a | 2172 | } |
dcb10c00 MT |
2173 | } |
2174 | ||
d2fc4402 MAL |
2175 | if (dev->migration_blocker == NULL && |
2176 | !virtio_has_feature(dev->protocol_features, | |
2177 | VHOST_USER_PROTOCOL_F_LOG_SHMFD)) { | |
2178 | error_setg(&dev->migration_blocker, | |
2179 | "Migration disabled: vhost-user backend lacks " | |
2180 | "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature."); | |
2181 | } | |
2182 | ||
67b3965e AM |
2183 | if (dev->vq_index == 0) { |
2184 | err = vhost_setup_slave_channel(dev); | |
2185 | if (err < 0) { | |
998647dc | 2186 | error_setg_errno(errp, EPROTO, "vhost_backend_init failed"); |
28770ff9 | 2187 | return -EPROTO; |
67b3965e | 2188 | } |
4bbeeba0 MAL |
2189 | } |
2190 | ||
9ccbfe14 DDAG |
2191 | u->postcopy_notifier.notify = vhost_user_postcopy_notifier; |
2192 | postcopy_add_notifier(&u->postcopy_notifier); | |
2193 | ||
5f6f6664 NN |
2194 | return 0; |
2195 | } | |
2196 | ||
4d0cf552 | 2197 | static int vhost_user_backend_cleanup(struct vhost_dev *dev) |
5f6f6664 | 2198 | { |
2152f3fe MAL |
2199 | struct vhost_user *u; |
2200 | ||
5f6f6664 NN |
2201 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
2202 | ||
2152f3fe | 2203 | u = dev->opaque; |
9ccbfe14 DDAG |
2204 | if (u->postcopy_notifier.notify) { |
2205 | postcopy_remove_notifier(&u->postcopy_notifier); | |
2206 | u->postcopy_notifier.notify = NULL; | |
2207 | } | |
c4f75385 IM |
2208 | u->postcopy_listen = false; |
2209 | if (u->postcopy_fd.handler) { | |
2210 | postcopy_unregister_shared_ufd(&u->postcopy_fd); | |
2211 | close(u->postcopy_fd.fd); | |
2212 | u->postcopy_fd.handler = NULL; | |
2213 | } | |
57dc0217 | 2214 | if (u->slave_ioc) { |
de62e494 | 2215 | close_slave_channel(u); |
4bbeeba0 | 2216 | } |
905125d0 DDAG |
2217 | g_free(u->region_rb); |
2218 | u->region_rb = NULL; | |
2219 | g_free(u->region_rb_offset); | |
2220 | u->region_rb_offset = NULL; | |
2221 | u->region_rb_len = 0; | |
2152f3fe | 2222 | g_free(u); |
5f6f6664 NN |
2223 | dev->opaque = 0; |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
fc57fd99 YL |
2228 | static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx) |
2229 | { | |
2230 | assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); | |
2231 | ||
2232 | return idx; | |
2233 | } | |
2234 | ||
2ce68e4c IM |
2235 | static int vhost_user_memslots_limit(struct vhost_dev *dev) |
2236 | { | |
6b0eff1a RN |
2237 | struct vhost_user *u = dev->opaque; |
2238 | ||
2239 | return u->user->memory_slots; | |
2ce68e4c IM |
2240 | } |
2241 | ||
1be0ac21 MAL |
2242 | static bool vhost_user_requires_shm_log(struct vhost_dev *dev) |
2243 | { | |
2244 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
2245 | ||
2246 | return virtio_has_feature(dev->protocol_features, | |
2247 | VHOST_USER_PROTOCOL_F_LOG_SHMFD); | |
2248 | } | |
2249 | ||
3e866365 TC |
2250 | static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr) |
2251 | { | |
ebf2a499 | 2252 | VhostUserMsg msg = { }; |
3e866365 TC |
2253 | |
2254 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
2255 | ||
2256 | /* If guest supports GUEST_ANNOUNCE do nothing */ | |
2257 | if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) { | |
2258 | return 0; | |
2259 | } | |
2260 | ||
2261 | /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */ | |
2262 | if (virtio_has_feature(dev->protocol_features, | |
2263 | VHOST_USER_PROTOCOL_F_RARP)) { | |
24e34754 MT |
2264 | msg.hdr.request = VHOST_USER_SEND_RARP; |
2265 | msg.hdr.flags = VHOST_USER_VERSION; | |
7f4a930e | 2266 | memcpy((char *)&msg.payload.u64, mac_addr, 6); |
24e34754 | 2267 | msg.hdr.size = sizeof(msg.payload.u64); |
3e866365 | 2268 | |
c4843a45 | 2269 | return vhost_user_write(dev, &msg, NULL, 0); |
3e866365 | 2270 | } |
025faa87 | 2271 | return -ENOTSUP; |
3e866365 TC |
2272 | } |
2273 | ||
ffe42cc1 MT |
2274 | static bool vhost_user_can_merge(struct vhost_dev *dev, |
2275 | uint64_t start1, uint64_t size1, | |
2276 | uint64_t start2, uint64_t size2) | |
2277 | { | |
07bdaa41 | 2278 | ram_addr_t offset; |
ffe42cc1 | 2279 | int mfd, rfd; |
ffe42cc1 | 2280 | |
23374a84 RN |
2281 | (void)vhost_user_get_mr_data(start1, &offset, &mfd); |
2282 | (void)vhost_user_get_mr_data(start2, &offset, &rfd); | |
ffe42cc1 MT |
2283 | |
2284 | return mfd == rfd; | |
2285 | } | |
2286 | ||
c5f048d8 MC |
2287 | static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu) |
2288 | { | |
2289 | VhostUserMsg msg; | |
2290 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
2291 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
025faa87 | 2292 | int ret; |
c5f048d8 MC |
2293 | |
2294 | if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) { | |
2295 | return 0; | |
2296 | } | |
2297 | ||
24e34754 | 2298 | msg.hdr.request = VHOST_USER_NET_SET_MTU; |
c5f048d8 | 2299 | msg.payload.u64 = mtu; |
24e34754 MT |
2300 | msg.hdr.size = sizeof(msg.payload.u64); |
2301 | msg.hdr.flags = VHOST_USER_VERSION; | |
c5f048d8 | 2302 | if (reply_supported) { |
24e34754 | 2303 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
c5f048d8 MC |
2304 | } |
2305 | ||
025faa87 RK |
2306 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2307 | if (ret < 0) { | |
2308 | return ret; | |
c5f048d8 MC |
2309 | } |
2310 | ||
2311 | /* If reply_ack supported, slave has to ack specified MTU is valid */ | |
2312 | if (reply_supported) { | |
3cf7daf8 | 2313 | return process_message_reply(dev, &msg); |
c5f048d8 MC |
2314 | } |
2315 | ||
2316 | return 0; | |
2317 | } | |
2318 | ||
6dcdd06e MC |
2319 | static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev, |
2320 | struct vhost_iotlb_msg *imsg) | |
2321 | { | |
025faa87 | 2322 | int ret; |
6dcdd06e | 2323 | VhostUserMsg msg = { |
24e34754 MT |
2324 | .hdr.request = VHOST_USER_IOTLB_MSG, |
2325 | .hdr.size = sizeof(msg.payload.iotlb), | |
2326 | .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK, | |
6dcdd06e MC |
2327 | .payload.iotlb = *imsg, |
2328 | }; | |
2329 | ||
025faa87 RK |
2330 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2331 | if (ret < 0) { | |
2332 | return ret; | |
6dcdd06e MC |
2333 | } |
2334 | ||
2335 | return process_message_reply(dev, &msg); | |
2336 | } | |
2337 | ||
2338 | ||
2339 | static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled) | |
2340 | { | |
2341 | /* No-op as the receive channel is not dedicated to IOTLB messages. */ | |
2342 | } | |
2343 | ||
4c3e257b | 2344 | static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config, |
50de5138 | 2345 | uint32_t config_len, Error **errp) |
4c3e257b | 2346 | { |
025faa87 | 2347 | int ret; |
4c3e257b | 2348 | VhostUserMsg msg = { |
24e34754 MT |
2349 | .hdr.request = VHOST_USER_GET_CONFIG, |
2350 | .hdr.flags = VHOST_USER_VERSION, | |
2351 | .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len, | |
4c3e257b CL |
2352 | }; |
2353 | ||
1c3e5a26 MC |
2354 | if (!virtio_has_feature(dev->protocol_features, |
2355 | VHOST_USER_PROTOCOL_F_CONFIG)) { | |
50de5138 KW |
2356 | error_setg(errp, "VHOST_USER_PROTOCOL_F_CONFIG not supported"); |
2357 | return -EINVAL; | |
1c3e5a26 MC |
2358 | } |
2359 | ||
50de5138 | 2360 | assert(config_len <= VHOST_USER_MAX_CONFIG_SIZE); |
4c3e257b CL |
2361 | |
2362 | msg.payload.config.offset = 0; | |
2363 | msg.payload.config.size = config_len; | |
025faa87 RK |
2364 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2365 | if (ret < 0) { | |
2366 | error_setg_errno(errp, -ret, "vhost_get_config failed"); | |
2367 | return ret; | |
4c3e257b CL |
2368 | } |
2369 | ||
025faa87 RK |
2370 | ret = vhost_user_read(dev, &msg); |
2371 | if (ret < 0) { | |
2372 | error_setg_errno(errp, -ret, "vhost_get_config failed"); | |
2373 | return ret; | |
4c3e257b CL |
2374 | } |
2375 | ||
24e34754 | 2376 | if (msg.hdr.request != VHOST_USER_GET_CONFIG) { |
50de5138 KW |
2377 | error_setg(errp, |
2378 | "Received unexpected msg type. Expected %d received %d", | |
2379 | VHOST_USER_GET_CONFIG, msg.hdr.request); | |
025faa87 | 2380 | return -EPROTO; |
4c3e257b CL |
2381 | } |
2382 | ||
24e34754 | 2383 | if (msg.hdr.size != VHOST_USER_CONFIG_HDR_SIZE + config_len) { |
50de5138 | 2384 | error_setg(errp, "Received bad msg size."); |
025faa87 | 2385 | return -EPROTO; |
4c3e257b CL |
2386 | } |
2387 | ||
2388 | memcpy(config, msg.payload.config.region, config_len); | |
2389 | ||
2390 | return 0; | |
2391 | } | |
2392 | ||
2393 | static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data, | |
2394 | uint32_t offset, uint32_t size, uint32_t flags) | |
2395 | { | |
025faa87 | 2396 | int ret; |
4c3e257b CL |
2397 | uint8_t *p; |
2398 | bool reply_supported = virtio_has_feature(dev->protocol_features, | |
2399 | VHOST_USER_PROTOCOL_F_REPLY_ACK); | |
2400 | ||
2401 | VhostUserMsg msg = { | |
24e34754 MT |
2402 | .hdr.request = VHOST_USER_SET_CONFIG, |
2403 | .hdr.flags = VHOST_USER_VERSION, | |
2404 | .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size, | |
4c3e257b CL |
2405 | }; |
2406 | ||
1c3e5a26 MC |
2407 | if (!virtio_has_feature(dev->protocol_features, |
2408 | VHOST_USER_PROTOCOL_F_CONFIG)) { | |
025faa87 | 2409 | return -ENOTSUP; |
1c3e5a26 MC |
2410 | } |
2411 | ||
4c3e257b | 2412 | if (reply_supported) { |
24e34754 | 2413 | msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK; |
4c3e257b CL |
2414 | } |
2415 | ||
2416 | if (size > VHOST_USER_MAX_CONFIG_SIZE) { | |
025faa87 | 2417 | return -EINVAL; |
4c3e257b CL |
2418 | } |
2419 | ||
2420 | msg.payload.config.offset = offset, | |
2421 | msg.payload.config.size = size, | |
2422 | msg.payload.config.flags = flags, | |
2423 | p = msg.payload.config.region; | |
2424 | memcpy(p, data, size); | |
2425 | ||
025faa87 RK |
2426 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2427 | if (ret < 0) { | |
2428 | return ret; | |
4c3e257b CL |
2429 | } |
2430 | ||
2431 | if (reply_supported) { | |
2432 | return process_message_reply(dev, &msg); | |
2433 | } | |
2434 | ||
2435 | return 0; | |
2436 | } | |
2437 | ||
efbfeb81 GA |
2438 | static int vhost_user_crypto_create_session(struct vhost_dev *dev, |
2439 | void *session_info, | |
2440 | uint64_t *session_id) | |
2441 | { | |
025faa87 | 2442 | int ret; |
efbfeb81 GA |
2443 | bool crypto_session = virtio_has_feature(dev->protocol_features, |
2444 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION); | |
2445 | CryptoDevBackendSymSessionInfo *sess_info = session_info; | |
2446 | VhostUserMsg msg = { | |
2447 | .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION, | |
2448 | .hdr.flags = VHOST_USER_VERSION, | |
2449 | .hdr.size = sizeof(msg.payload.session), | |
2450 | }; | |
2451 | ||
2452 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); | |
2453 | ||
2454 | if (!crypto_session) { | |
2455 | error_report("vhost-user trying to send unhandled ioctl"); | |
025faa87 | 2456 | return -ENOTSUP; |
efbfeb81 GA |
2457 | } |
2458 | ||
2459 | memcpy(&msg.payload.session.session_setup_data, sess_info, | |
2460 | sizeof(CryptoDevBackendSymSessionInfo)); | |
2461 | if (sess_info->key_len) { | |
2462 | memcpy(&msg.payload.session.key, sess_info->cipher_key, | |
2463 | sess_info->key_len); | |
2464 | } | |
2465 | if (sess_info->auth_key_len > 0) { | |
2466 | memcpy(&msg.payload.session.auth_key, sess_info->auth_key, | |
2467 | sess_info->auth_key_len); | |
2468 | } | |
025faa87 RK |
2469 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2470 | if (ret < 0) { | |
2471 | error_report("vhost_user_write() return %d, create session failed", | |
2472 | ret); | |
2473 | return ret; | |
efbfeb81 GA |
2474 | } |
2475 | ||
025faa87 RK |
2476 | ret = vhost_user_read(dev, &msg); |
2477 | if (ret < 0) { | |
2478 | error_report("vhost_user_read() return %d, create session failed", | |
2479 | ret); | |
2480 | return ret; | |
efbfeb81 GA |
2481 | } |
2482 | ||
2483 | if (msg.hdr.request != VHOST_USER_CREATE_CRYPTO_SESSION) { | |
2484 | error_report("Received unexpected msg type. Expected %d received %d", | |
2485 | VHOST_USER_CREATE_CRYPTO_SESSION, msg.hdr.request); | |
025faa87 | 2486 | return -EPROTO; |
efbfeb81 GA |
2487 | } |
2488 | ||
2489 | if (msg.hdr.size != sizeof(msg.payload.session)) { | |
2490 | error_report("Received bad msg size."); | |
025faa87 | 2491 | return -EPROTO; |
efbfeb81 GA |
2492 | } |
2493 | ||
2494 | if (msg.payload.session.session_id < 0) { | |
2495 | error_report("Bad session id: %" PRId64 "", | |
2496 | msg.payload.session.session_id); | |
025faa87 | 2497 | return -EINVAL; |
efbfeb81 GA |
2498 | } |
2499 | *session_id = msg.payload.session.session_id; | |
2500 | ||
2501 | return 0; | |
2502 | } | |
2503 | ||
2504 | static int | |
2505 | vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) | |
2506 | { | |
025faa87 | 2507 | int ret; |
efbfeb81 GA |
2508 | bool crypto_session = virtio_has_feature(dev->protocol_features, |
2509 | VHOST_USER_PROTOCOL_F_CRYPTO_SESSION); | |
2510 | VhostUserMsg msg = { | |
2511 | .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION, | |
2512 | .hdr.flags = VHOST_USER_VERSION, | |
2513 | .hdr.size = sizeof(msg.payload.u64), | |
2514 | }; | |
2515 | msg.payload.u64 = session_id; | |
2516 | ||
2517 | if (!crypto_session) { | |
2518 | error_report("vhost-user trying to send unhandled ioctl"); | |
025faa87 | 2519 | return -ENOTSUP; |
efbfeb81 GA |
2520 | } |
2521 | ||
025faa87 RK |
2522 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2523 | if (ret < 0) { | |
2524 | error_report("vhost_user_write() return %d, close session failed", | |
2525 | ret); | |
2526 | return ret; | |
efbfeb81 GA |
2527 | } |
2528 | ||
2529 | return 0; | |
2530 | } | |
2531 | ||
988a2775 TB |
2532 | static bool vhost_user_mem_section_filter(struct vhost_dev *dev, |
2533 | MemoryRegionSection *section) | |
2534 | { | |
2535 | bool result; | |
2536 | ||
2537 | result = memory_region_get_fd(section->mr) >= 0; | |
2538 | ||
2539 | return result; | |
2540 | } | |
2541 | ||
5ad204bf XY |
2542 | static int vhost_user_get_inflight_fd(struct vhost_dev *dev, |
2543 | uint16_t queue_size, | |
2544 | struct vhost_inflight *inflight) | |
2545 | { | |
2546 | void *addr; | |
2547 | int fd; | |
025faa87 | 2548 | int ret; |
5ad204bf XY |
2549 | struct vhost_user *u = dev->opaque; |
2550 | CharBackend *chr = u->user->chr; | |
2551 | VhostUserMsg msg = { | |
2552 | .hdr.request = VHOST_USER_GET_INFLIGHT_FD, | |
2553 | .hdr.flags = VHOST_USER_VERSION, | |
2554 | .payload.inflight.num_queues = dev->nvqs, | |
2555 | .payload.inflight.queue_size = queue_size, | |
2556 | .hdr.size = sizeof(msg.payload.inflight), | |
2557 | }; | |
2558 | ||
2559 | if (!virtio_has_feature(dev->protocol_features, | |
2560 | VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { | |
2561 | return 0; | |
2562 | } | |
2563 | ||
025faa87 RK |
2564 | ret = vhost_user_write(dev, &msg, NULL, 0); |
2565 | if (ret < 0) { | |
2566 | return ret; | |
5ad204bf XY |
2567 | } |
2568 | ||
025faa87 RK |
2569 | ret = vhost_user_read(dev, &msg); |
2570 | if (ret < 0) { | |
2571 | return ret; | |
5ad204bf XY |
2572 | } |
2573 | ||
2574 | if (msg.hdr.request != VHOST_USER_GET_INFLIGHT_FD) { | |
2575 | error_report("Received unexpected msg type. " | |
2576 | "Expected %d received %d", | |
2577 | VHOST_USER_GET_INFLIGHT_FD, msg.hdr.request); | |
025faa87 | 2578 | return -EPROTO; |
5ad204bf XY |
2579 | } |
2580 | ||
2581 | if (msg.hdr.size != sizeof(msg.payload.inflight)) { | |
2582 | error_report("Received bad msg size."); | |
025faa87 | 2583 | return -EPROTO; |
5ad204bf XY |
2584 | } |
2585 | ||
2586 | if (!msg.payload.inflight.mmap_size) { | |
2587 | return 0; | |
2588 | } | |
2589 | ||
2590 | fd = qemu_chr_fe_get_msgfd(chr); | |
2591 | if (fd < 0) { | |
2592 | error_report("Failed to get mem fd"); | |
025faa87 | 2593 | return -EIO; |
5ad204bf XY |
2594 | } |
2595 | ||
2596 | addr = mmap(0, msg.payload.inflight.mmap_size, PROT_READ | PROT_WRITE, | |
2597 | MAP_SHARED, fd, msg.payload.inflight.mmap_offset); | |
2598 | ||
2599 | if (addr == MAP_FAILED) { | |
2600 | error_report("Failed to mmap mem fd"); | |
2601 | close(fd); | |
025faa87 | 2602 | return -EFAULT; |
5ad204bf XY |
2603 | } |
2604 | ||
2605 | inflight->addr = addr; | |
2606 | inflight->fd = fd; | |
2607 | inflight->size = msg.payload.inflight.mmap_size; | |
2608 | inflight->offset = msg.payload.inflight.mmap_offset; | |
2609 | inflight->queue_size = queue_size; | |
2610 | ||
2611 | return 0; | |
2612 | } | |
2613 | ||
2614 | static int vhost_user_set_inflight_fd(struct vhost_dev *dev, | |
2615 | struct vhost_inflight *inflight) | |
2616 | { | |
2617 | VhostUserMsg msg = { | |
2618 | .hdr.request = VHOST_USER_SET_INFLIGHT_FD, | |
2619 | .hdr.flags = VHOST_USER_VERSION, | |
2620 | .payload.inflight.mmap_size = inflight->size, | |
2621 | .payload.inflight.mmap_offset = inflight->offset, | |
2622 | .payload.inflight.num_queues = dev->nvqs, | |
2623 | .payload.inflight.queue_size = inflight->queue_size, | |
2624 | .hdr.size = sizeof(msg.payload.inflight), | |
2625 | }; | |
2626 | ||
2627 | if (!virtio_has_feature(dev->protocol_features, | |
2628 | VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) { | |
2629 | return 0; | |
2630 | } | |
2631 | ||
025faa87 | 2632 | return vhost_user_write(dev, &msg, &inflight->fd, 1); |
5ad204bf XY |
2633 | } |
2634 | ||
503e3554 AB |
2635 | static void vhost_user_state_destroy(gpointer data) |
2636 | { | |
2637 | VhostUserHostNotifier *n = (VhostUserHostNotifier *) data; | |
2638 | if (n) { | |
2639 | vhost_user_host_notifier_remove(n, NULL); | |
2640 | object_unparent(OBJECT(&n->mr)); | |
2641 | /* | |
2642 | * We can't free until vhost_user_host_notifier_remove has | |
2643 | * done it's thing so schedule the free with RCU. | |
2644 | */ | |
2645 | g_free_rcu(n, rcu); | |
2646 | } | |
2647 | } | |
2648 | ||
0b99f224 | 2649 | bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp) |
4d0cf552 | 2650 | { |
0b99f224 MAL |
2651 | if (user->chr) { |
2652 | error_setg(errp, "Cannot initialize vhost-user state"); | |
2653 | return false; | |
2654 | } | |
2655 | user->chr = chr; | |
6b0eff1a | 2656 | user->memory_slots = 0; |
503e3554 AB |
2657 | user->notifiers = g_ptr_array_new_full(VIRTIO_QUEUE_MAX / 4, |
2658 | &vhost_user_state_destroy); | |
0b99f224 | 2659 | return true; |
4d0cf552 TB |
2660 | } |
2661 | ||
2662 | void vhost_user_cleanup(VhostUserState *user) | |
2663 | { | |
0b99f224 MAL |
2664 | if (!user->chr) { |
2665 | return; | |
2666 | } | |
c6effa9c | 2667 | memory_region_transaction_begin(); |
503e3554 | 2668 | user->notifiers = (GPtrArray *) g_ptr_array_free(user->notifiers, true); |
c6effa9c | 2669 | memory_region_transaction_commit(); |
0b99f224 | 2670 | user->chr = NULL; |
4d0cf552 TB |
2671 | } |
2672 | ||
923b8921 YW |
2673 | static int vhost_user_dev_start(struct vhost_dev *dev, bool started) |
2674 | { | |
2675 | if (!virtio_has_feature(dev->protocol_features, | |
2676 | VHOST_USER_PROTOCOL_F_STATUS)) { | |
2677 | return 0; | |
2678 | } | |
2679 | ||
2680 | /* Set device status only for last queue pair */ | |
2681 | if (dev->vq_index + dev->nvqs != dev->vq_index_end) { | |
2682 | return 0; | |
2683 | } | |
2684 | ||
2685 | if (started) { | |
2686 | return vhost_user_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | | |
2687 | VIRTIO_CONFIG_S_DRIVER | | |
2688 | VIRTIO_CONFIG_S_DRIVER_OK); | |
2689 | } else { | |
2690 | return vhost_user_set_status(dev, 0); | |
2691 | } | |
2692 | } | |
2693 | ||
5f6f6664 NN |
2694 | const VhostOps user_ops = { |
2695 | .backend_type = VHOST_BACKEND_TYPE_USER, | |
4d0cf552 TB |
2696 | .vhost_backend_init = vhost_user_backend_init, |
2697 | .vhost_backend_cleanup = vhost_user_backend_cleanup, | |
2ce68e4c | 2698 | .vhost_backend_memslots_limit = vhost_user_memslots_limit, |
21e70425 MAL |
2699 | .vhost_set_log_base = vhost_user_set_log_base, |
2700 | .vhost_set_mem_table = vhost_user_set_mem_table, | |
2701 | .vhost_set_vring_addr = vhost_user_set_vring_addr, | |
2702 | .vhost_set_vring_endian = vhost_user_set_vring_endian, | |
2703 | .vhost_set_vring_num = vhost_user_set_vring_num, | |
2704 | .vhost_set_vring_base = vhost_user_set_vring_base, | |
2705 | .vhost_get_vring_base = vhost_user_get_vring_base, | |
2706 | .vhost_set_vring_kick = vhost_user_set_vring_kick, | |
2707 | .vhost_set_vring_call = vhost_user_set_vring_call, | |
60dc3c5b | 2708 | .vhost_set_vring_err = vhost_user_set_vring_err, |
21e70425 MAL |
2709 | .vhost_set_features = vhost_user_set_features, |
2710 | .vhost_get_features = vhost_user_get_features, | |
2711 | .vhost_set_owner = vhost_user_set_owner, | |
2712 | .vhost_reset_device = vhost_user_reset_device, | |
2713 | .vhost_get_vq_index = vhost_user_get_vq_index, | |
2714 | .vhost_set_vring_enable = vhost_user_set_vring_enable, | |
1be0ac21 | 2715 | .vhost_requires_shm_log = vhost_user_requires_shm_log, |
3e866365 | 2716 | .vhost_migration_done = vhost_user_migration_done, |
ffe42cc1 | 2717 | .vhost_backend_can_merge = vhost_user_can_merge, |
c5f048d8 | 2718 | .vhost_net_set_mtu = vhost_user_net_set_mtu, |
6dcdd06e MC |
2719 | .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback, |
2720 | .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg, | |
4c3e257b CL |
2721 | .vhost_get_config = vhost_user_get_config, |
2722 | .vhost_set_config = vhost_user_set_config, | |
efbfeb81 GA |
2723 | .vhost_crypto_create_session = vhost_user_crypto_create_session, |
2724 | .vhost_crypto_close_session = vhost_user_crypto_close_session, | |
988a2775 | 2725 | .vhost_backend_mem_section_filter = vhost_user_mem_section_filter, |
5ad204bf XY |
2726 | .vhost_get_inflight_fd = vhost_user_get_inflight_fd, |
2727 | .vhost_set_inflight_fd = vhost_user_set_inflight_fd, | |
923b8921 | 2728 | .vhost_dev_start = vhost_user_dev_start, |
fc57fd99 | 2729 | }; |