]>
Commit | Line | Data |
---|---|---|
9a4ac51f EH |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
1393a485 | 24 | #include "qemu/osdep.h" |
44f0eadc | 25 | #include <zlib.h> |
b85ea5fa | 26 | #include "qemu/madvise.h" |
d49b6836 | 27 | #include "qemu/error-report.h" |
093c455a | 28 | #include "qemu/iov.h" |
6666c96a | 29 | #include "migration.h" |
8e4b2a70 | 30 | #include "migration-stats.h" |
08a0aee1 | 31 | #include "qemu-file.h" |
9013dca5 | 32 | #include "trace.h" |
9d3ebbe2 | 33 | #include "options.h" |
3d661c8a | 34 | #include "qapi/error.h" |
48408174 | 35 | #include "rdma.h" |
093c455a | 36 | |
a24939f2 | 37 | #define IO_BUF_SIZE 32768 |
f9919116 | 38 | #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64) |
a24939f2 DB |
39 | |
40 | struct QEMUFile { | |
2893a288 | 41 | QIOChannel *ioc; |
c0c6e1e2 | 42 | bool is_writable; |
a24939f2 | 43 | |
154d87b4 | 44 | /* The sum of bytes transferred on the wire */ |
61abf1eb | 45 | uint64_t total_transferred; |
154d87b4 | 46 | |
a24939f2 DB |
47 | int buf_index; |
48 | int buf_size; /* 0 when writing */ | |
49 | uint8_t buf[IO_BUF_SIZE]; | |
50 | ||
53f09a10 | 51 | DECLARE_BITMAP(may_free, MAX_IOV_SIZE); |
a24939f2 DB |
52 | struct iovec iov[MAX_IOV_SIZE]; |
53 | unsigned int iovcnt; | |
54 | ||
55 | int last_error; | |
3d661c8a | 56 | Error *last_error_obj; |
a24939f2 DB |
57 | }; |
58 | ||
e1a8c9b6 DDAG |
59 | /* |
60 | * Stop a file from being read/written - not all backing files can do this | |
61 | * typically only sockets can. | |
d3c581b7 DB |
62 | * |
63 | * TODO: convert to propagate Error objects instead of squashing | |
64 | * to a fixed errno value | |
e1a8c9b6 DDAG |
65 | */ |
66 | int qemu_file_shutdown(QEMUFile *f) | |
67 | { | |
f5816b5c PX |
68 | /* |
69 | * We must set qemufile error before the real shutdown(), otherwise | |
70 | * there can be a race window where we thought IO all went though | |
71 | * (because last_error==NULL) but actually IO has already stopped. | |
72 | * | |
73 | * If without correct ordering, the race can happen like this: | |
74 | * | |
75 | * page receiver other thread | |
76 | * ------------- ------------ | |
77 | * qemu_get_buffer() | |
78 | * do shutdown() | |
79 | * returns 0 (buffer all zero) | |
80 | * (we didn't check this retcode) | |
81 | * try to detect IO error | |
82 | * last_error==NULL, IO okay | |
83 | * install ALL-ZERO page | |
84 | * set last_error | |
85 | * --> guest crash! | |
86 | */ | |
87 | if (!f->last_error) { | |
88 | qemu_file_set_error(f, -EIO); | |
89 | } | |
90 | ||
d3c581b7 DB |
91 | if (!qio_channel_has_feature(f->ioc, |
92 | QIO_CHANNEL_FEATURE_SHUTDOWN)) { | |
e1a8c9b6 DDAG |
93 | return -ENOSYS; |
94 | } | |
d3c581b7 DB |
95 | |
96 | if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) { | |
8c5ee0bf | 97 | return -EIO; |
d3c581b7 | 98 | } |
a555b809 | 99 | |
8c5ee0bf | 100 | return 0; |
e1a8c9b6 DDAG |
101 | } |
102 | ||
77ef2dc1 | 103 | static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable) |
093c455a EH |
104 | { |
105 | QEMUFile *f; | |
106 | ||
97f3ad35 | 107 | f = g_new0(QEMUFile, 1); |
093c455a | 108 | |
77ef2dc1 | 109 | object_ref(ioc); |
2893a288 | 110 | f->ioc = ioc; |
c0c6e1e2 | 111 | f->is_writable = is_writable; |
2893a288 | 112 | |
093c455a EH |
113 | return f; |
114 | } | |
115 | ||
02bdbe17 DB |
116 | /* |
117 | * Result: QEMUFile* for a 'return path' for comms in the opposite direction | |
118 | * NULL if not available | |
119 | */ | |
120 | QEMUFile *qemu_file_get_return_path(QEMUFile *f) | |
121 | { | |
77ef2dc1 | 122 | return qemu_file_new_impl(f->ioc, !f->is_writable); |
02bdbe17 DB |
123 | } |
124 | ||
77ef2dc1 | 125 | QEMUFile *qemu_file_new_output(QIOChannel *ioc) |
c0c6e1e2 | 126 | { |
77ef2dc1 | 127 | return qemu_file_new_impl(ioc, true); |
c0c6e1e2 DB |
128 | } |
129 | ||
77ef2dc1 | 130 | QEMUFile *qemu_file_new_input(QIOChannel *ioc) |
c0c6e1e2 | 131 | { |
77ef2dc1 | 132 | return qemu_file_new_impl(ioc, false); |
c0c6e1e2 DB |
133 | } |
134 | ||
093c455a | 135 | /* |
3d661c8a | 136 | * Get last error for stream f with optional Error* |
093c455a EH |
137 | * |
138 | * Return negative error value if there has been an error on previous | |
139 | * operations, return 0 if no error happened. | |
140 | * | |
f4b897f4 | 141 | * If errp is specified, a verbose error message will be copied over. |
093c455a | 142 | */ |
ac6f48e1 | 143 | static int qemu_file_get_error_obj(QEMUFile *f, Error **errp) |
093c455a | 144 | { |
f4b897f4 PX |
145 | if (!f->last_error) { |
146 | return 0; | |
147 | } | |
148 | ||
149 | /* There is an error */ | |
3d661c8a | 150 | if (errp) { |
f4b897f4 PX |
151 | if (f->last_error_obj) { |
152 | *errp = error_copy(f->last_error_obj); | |
153 | } else { | |
154 | error_setg_errno(errp, -f->last_error, "Channel error"); | |
155 | } | |
3d661c8a | 156 | } |
f4b897f4 | 157 | |
093c455a EH |
158 | return f->last_error; |
159 | } | |
160 | ||
60bb3c58 PX |
161 | /* |
162 | * Get last error for either stream f1 or f2 with optional Error*. | |
163 | * The error returned (non-zero) can be either from f1 or f2. | |
164 | * | |
165 | * If any of the qemufile* is NULL, then skip the check on that file. | |
166 | * | |
167 | * When there is no error on both qemufile, zero is returned. | |
168 | */ | |
169 | int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp) | |
170 | { | |
171 | int ret = 0; | |
172 | ||
173 | if (f1) { | |
174 | ret = qemu_file_get_error_obj(f1, errp); | |
175 | /* If there's already error detected, return */ | |
176 | if (ret) { | |
177 | return ret; | |
178 | } | |
179 | } | |
180 | ||
181 | if (f2) { | |
182 | ret = qemu_file_get_error_obj(f2, errp); | |
183 | } | |
184 | ||
185 | return ret; | |
186 | } | |
187 | ||
3d661c8a YK |
188 | /* |
189 | * Set the last error for stream f with optional Error* | |
190 | */ | |
191 | void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err) | |
093c455a | 192 | { |
3d661c8a | 193 | if (f->last_error == 0 && ret) { |
093c455a | 194 | f->last_error = ret; |
3d661c8a YK |
195 | error_propagate(&f->last_error_obj, err); |
196 | } else if (err) { | |
197 | error_report_err(err); | |
093c455a EH |
198 | } |
199 | } | |
200 | ||
3d661c8a YK |
201 | /* |
202 | * Get last error for stream f | |
203 | * | |
204 | * Return negative error value if there has been an error on previous | |
205 | * operations, return 0 if no error happened. | |
206 | * | |
207 | */ | |
208 | int qemu_file_get_error(QEMUFile *f) | |
209 | { | |
210 | return qemu_file_get_error_obj(f, NULL); | |
211 | } | |
212 | ||
213 | /* | |
214 | * Set the last error for stream f | |
215 | */ | |
216 | void qemu_file_set_error(QEMUFile *f, int ret) | |
217 | { | |
218 | qemu_file_set_error_obj(f, ret, NULL); | |
219 | } | |
220 | ||
9ccf83f4 | 221 | static bool qemu_file_is_writable(QEMUFile *f) |
093c455a | 222 | { |
c0c6e1e2 | 223 | return f->is_writable; |
093c455a EH |
224 | } |
225 | ||
53f09a10 PB |
226 | static void qemu_iovec_release_ram(QEMUFile *f) |
227 | { | |
228 | struct iovec iov; | |
229 | unsigned long idx; | |
230 | ||
231 | /* Find and release all the contiguous memory ranges marked as may_free. */ | |
232 | idx = find_next_bit(f->may_free, f->iovcnt, 0); | |
233 | if (idx >= f->iovcnt) { | |
234 | return; | |
235 | } | |
236 | iov = f->iov[idx]; | |
237 | ||
238 | /* The madvise() in the loop is called for iov within a continuous range and | |
239 | * then reinitialize the iov. And in the end, madvise() is called for the | |
240 | * last iov. | |
241 | */ | |
242 | while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { | |
243 | /* check for adjacent buffer and coalesce them */ | |
244 | if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { | |
245 | iov.iov_len += f->iov[idx].iov_len; | |
246 | continue; | |
247 | } | |
248 | if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { | |
249 | error_report("migrate: madvise DONTNEED failed %p %zd: %s", | |
250 | iov.iov_base, iov.iov_len, strerror(errno)); | |
251 | } | |
252 | iov = f->iov[idx]; | |
253 | } | |
254 | if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { | |
255 | error_report("migrate: madvise DONTNEED failed %p %zd: %s", | |
256 | iov.iov_base, iov.iov_len, strerror(errno)); | |
257 | } | |
258 | memset(f->may_free, 0, sizeof(f->may_free)); | |
259 | } | |
260 | ||
77ef2dc1 | 261 | |
093c455a EH |
262 | /** |
263 | * Flushes QEMUFile buffer | |
264 | * | |
3b348706 DDAG |
265 | * This will flush all pending data. If data was only partially flushed, it |
266 | * will set an error state. | |
093c455a EH |
267 | */ |
268 | void qemu_fflush(QEMUFile *f) | |
269 | { | |
093c455a EH |
270 | if (!qemu_file_is_writable(f)) { |
271 | return; | |
272 | } | |
273 | ||
ac7d25b8 | 274 | if (qemu_file_get_error(f)) { |
a555b809 JQ |
275 | return; |
276 | } | |
11808bb0 | 277 | if (f->iovcnt > 0) { |
ec2135ee DB |
278 | Error *local_error = NULL; |
279 | if (qio_channel_writev_all(f->ioc, | |
280 | f->iov, f->iovcnt, | |
281 | &local_error) < 0) { | |
282 | qemu_file_set_error_obj(f, -EIO, local_error); | |
283 | } else { | |
de37f8b9 | 284 | uint64_t size = iov_size(f->iov, f->iovcnt); |
de37f8b9 | 285 | f->total_transferred += size; |
ec2135ee | 286 | } |
53f09a10 PB |
287 | |
288 | qemu_iovec_release_ram(f); | |
093c455a | 289 | } |
baf51e77 | 290 | |
093c455a EH |
291 | f->buf_index = 0; |
292 | f->iovcnt = 0; | |
093c455a EH |
293 | } |
294 | ||
548f52ea DDAG |
295 | /* |
296 | * Attempt to fill the buffer from the underlying file | |
297 | * Returns the number of bytes read, or negative value for an error. | |
298 | * | |
299 | * Note that it can return a partially full buffer even in a not error/not EOF | |
300 | * case if the underlying file descriptor gives a short read, and that can | |
301 | * happen even on a blocking fd. | |
302 | */ | |
394b9407 | 303 | static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f) |
093c455a EH |
304 | { |
305 | int len; | |
306 | int pending; | |
3d661c8a | 307 | Error *local_error = NULL; |
093c455a EH |
308 | |
309 | assert(!qemu_file_is_writable(f)); | |
310 | ||
311 | pending = f->buf_size - f->buf_index; | |
312 | if (pending > 0) { | |
313 | memmove(f->buf, f->buf + f->buf_index, pending); | |
314 | } | |
315 | f->buf_index = 0; | |
316 | f->buf_size = pending; | |
317 | ||
ac7d25b8 | 318 | if (qemu_file_get_error(f)) { |
a555b809 JQ |
319 | return 0; |
320 | } | |
321 | ||
f759d705 DB |
322 | do { |
323 | len = qio_channel_read(f->ioc, | |
324 | (char *)f->buf + pending, | |
325 | IO_BUF_SIZE - pending, | |
326 | &local_error); | |
327 | if (len == QIO_CHANNEL_ERR_BLOCK) { | |
328 | if (qemu_in_coroutine()) { | |
329 | qio_channel_yield(f->ioc, G_IO_IN); | |
330 | } else { | |
331 | qio_channel_wait(f->ioc, G_IO_IN); | |
332 | } | |
333 | } else if (len < 0) { | |
334 | len = -EIO; | |
335 | } | |
336 | } while (len == QIO_CHANNEL_ERR_BLOCK); | |
337 | ||
093c455a EH |
338 | if (len > 0) { |
339 | f->buf_size += len; | |
154d87b4 | 340 | f->total_transferred += len; |
093c455a | 341 | } else if (len == 0) { |
3d661c8a | 342 | qemu_file_set_error_obj(f, -EIO, local_error); |
3d661c8a | 343 | } else { |
5f87072e | 344 | qemu_file_set_error_obj(f, len, local_error); |
093c455a | 345 | } |
548f52ea DDAG |
346 | |
347 | return len; | |
093c455a EH |
348 | } |
349 | ||
093c455a EH |
350 | /** Closes the file |
351 | * | |
352 | * Returns negative error value if any error happened on previous operations or | |
353 | * while closing the file. Returns 0 or positive number on success. | |
354 | * | |
355 | * The meaning of return value on success depends on the specific backend | |
356 | * being used. | |
357 | */ | |
358 | int qemu_fclose(QEMUFile *f) | |
359 | { | |
0ae1f7f0 | 360 | int ret, ret2; |
093c455a EH |
361 | qemu_fflush(f); |
362 | ret = qemu_file_get_error(f); | |
363 | ||
0ae1f7f0 DB |
364 | ret2 = qio_channel_close(f->ioc, NULL); |
365 | if (ret >= 0) { | |
366 | ret = ret2; | |
093c455a | 367 | } |
0ae1f7f0 DB |
368 | g_clear_pointer(&f->ioc, object_unref); |
369 | ||
093c455a EH |
370 | /* If any error was spotted before closing, we should report it |
371 | * instead of the close() return value. | |
372 | */ | |
373 | if (f->last_error) { | |
374 | ret = f->last_error; | |
375 | } | |
3d661c8a | 376 | error_free(f->last_error_obj); |
093c455a | 377 | g_free(f); |
9013dca5 | 378 | trace_qemu_file_fclose(); |
093c455a EH |
379 | return ret; |
380 | } | |
381 | ||
1bf57fb3 WY |
382 | /* |
383 | * Add buf to iovec. Do flush if iovec is full. | |
384 | * | |
385 | * Return values: | |
386 | * 1 iovec is full and flushed | |
387 | * 0 iovec is not flushed | |
388 | * | |
389 | */ | |
390 | static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, | |
391 | bool may_free) | |
093c455a EH |
392 | { |
393 | /* check for adjacent buffer and coalesce them */ | |
394 | if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + | |
53f09a10 PB |
395 | f->iov[f->iovcnt - 1].iov_len && |
396 | may_free == test_bit(f->iovcnt - 1, f->may_free)) | |
397 | { | |
093c455a EH |
398 | f->iov[f->iovcnt - 1].iov_len += size; |
399 | } else { | |
c00d434a FL |
400 | if (f->iovcnt >= MAX_IOV_SIZE) { |
401 | /* Should only happen if a previous fflush failed */ | |
ac7d25b8 | 402 | assert(qemu_file_get_error(f) || !qemu_file_is_writable(f)); |
c00d434a FL |
403 | return 1; |
404 | } | |
53f09a10 PB |
405 | if (may_free) { |
406 | set_bit(f->iovcnt, f->may_free); | |
407 | } | |
093c455a EH |
408 | f->iov[f->iovcnt].iov_base = (uint8_t *)buf; |
409 | f->iov[f->iovcnt++].iov_len = size; | |
410 | } | |
411 | ||
412 | if (f->iovcnt >= MAX_IOV_SIZE) { | |
413 | qemu_fflush(f); | |
1bf57fb3 WY |
414 | return 1; |
415 | } | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | static void add_buf_to_iovec(QEMUFile *f, size_t len) | |
421 | { | |
422 | if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { | |
423 | f->buf_index += len; | |
424 | if (f->buf_index == IO_BUF_SIZE) { | |
425 | qemu_fflush(f); | |
426 | } | |
093c455a EH |
427 | } |
428 | } | |
429 | ||
53f09a10 PB |
430 | void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, |
431 | bool may_free) | |
093c455a | 432 | { |
093c455a EH |
433 | if (f->last_error) { |
434 | return; | |
435 | } | |
436 | ||
53f09a10 | 437 | add_to_iovec(f, buf, size, may_free); |
093c455a EH |
438 | } |
439 | ||
56f3835f | 440 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) |
093c455a | 441 | { |
56f3835f | 442 | size_t l; |
093c455a EH |
443 | |
444 | if (f->last_error) { | |
445 | return; | |
446 | } | |
447 | ||
448 | while (size > 0) { | |
449 | l = IO_BUF_SIZE - f->buf_index; | |
450 | if (l > size) { | |
451 | l = size; | |
452 | } | |
453 | memcpy(f->buf + f->buf_index, buf, l); | |
1bf57fb3 | 454 | add_buf_to_iovec(f, l); |
093c455a EH |
455 | if (qemu_file_get_error(f)) { |
456 | break; | |
457 | } | |
458 | buf += l; | |
459 | size -= l; | |
460 | } | |
461 | } | |
462 | ||
463 | void qemu_put_byte(QEMUFile *f, int v) | |
464 | { | |
465 | if (f->last_error) { | |
466 | return; | |
467 | } | |
468 | ||
469 | f->buf[f->buf_index] = v; | |
1bf57fb3 | 470 | add_buf_to_iovec(f, 1); |
093c455a EH |
471 | } |
472 | ||
473 | void qemu_file_skip(QEMUFile *f, int size) | |
474 | { | |
475 | if (f->buf_index + size <= f->buf_size) { | |
476 | f->buf_index += size; | |
477 | } | |
478 | } | |
479 | ||
548f52ea | 480 | /* |
7c1e52ba DDAG |
481 | * Read 'size' bytes from file (at 'offset') without moving the |
482 | * pointer and set 'buf' to point to that data. | |
548f52ea DDAG |
483 | * |
484 | * It will return size bytes unless there was an error, in which case it will | |
485 | * return as many as it managed to read (assuming blocking fd's which | |
486 | * all current QEMUFile are) | |
487 | */ | |
394b9407 | 488 | size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) |
093c455a | 489 | { |
56f3835f DDAG |
490 | ssize_t pending; |
491 | size_t index; | |
093c455a EH |
492 | |
493 | assert(!qemu_file_is_writable(f)); | |
548f52ea DDAG |
494 | assert(offset < IO_BUF_SIZE); |
495 | assert(size <= IO_BUF_SIZE - offset); | |
093c455a | 496 | |
548f52ea | 497 | /* The 1st byte to read from */ |
093c455a | 498 | index = f->buf_index + offset; |
548f52ea | 499 | /* The number of available bytes starting at index */ |
093c455a | 500 | pending = f->buf_size - index; |
548f52ea DDAG |
501 | |
502 | /* | |
503 | * qemu_fill_buffer might return just a few bytes, even when there isn't | |
504 | * an error, so loop collecting them until we get enough. | |
505 | */ | |
506 | while (pending < size) { | |
507 | int received = qemu_fill_buffer(f); | |
508 | ||
509 | if (received <= 0) { | |
510 | break; | |
511 | } | |
512 | ||
093c455a EH |
513 | index = f->buf_index + offset; |
514 | pending = f->buf_size - index; | |
515 | } | |
516 | ||
517 | if (pending <= 0) { | |
518 | return 0; | |
519 | } | |
520 | if (size > pending) { | |
521 | size = pending; | |
522 | } | |
523 | ||
7c1e52ba | 524 | *buf = f->buf + index; |
093c455a EH |
525 | return size; |
526 | } | |
527 | ||
548f52ea DDAG |
528 | /* |
529 | * Read 'size' bytes of data from the file into buf. | |
530 | * 'size' can be larger than the internal buffer. | |
531 | * | |
532 | * It will return size bytes unless there was an error, in which case it will | |
533 | * return as many as it managed to read (assuming blocking fd's which | |
534 | * all current QEMUFile are) | |
535 | */ | |
394b9407 | 536 | size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) |
093c455a | 537 | { |
56f3835f DDAG |
538 | size_t pending = size; |
539 | size_t done = 0; | |
093c455a EH |
540 | |
541 | while (pending > 0) { | |
56f3835f | 542 | size_t res; |
7c1e52ba | 543 | uint8_t *src; |
093c455a | 544 | |
7c1e52ba | 545 | res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); |
093c455a EH |
546 | if (res == 0) { |
547 | return done; | |
548 | } | |
7c1e52ba | 549 | memcpy(buf, src, res); |
093c455a EH |
550 | qemu_file_skip(f, res); |
551 | buf += res; | |
552 | pending -= res; | |
553 | done += res; | |
554 | } | |
555 | return done; | |
556 | } | |
557 | ||
9504fb51 DDAG |
558 | /* |
559 | * Read 'size' bytes of data from the file. | |
560 | * 'size' can be larger than the internal buffer. | |
561 | * | |
562 | * The data: | |
563 | * may be held on an internal buffer (in which case *buf is updated | |
564 | * to point to it) that is valid until the next qemu_file operation. | |
565 | * OR | |
566 | * will be copied to the *buf that was passed in. | |
567 | * | |
568 | * The code tries to avoid the copy if possible. | |
569 | * | |
570 | * It will return size bytes unless there was an error, in which case it will | |
571 | * return as many as it managed to read (assuming blocking fd's which | |
572 | * all current QEMUFile are) | |
573 | * | |
574 | * Note: Since **buf may get changed, the caller should take care to | |
575 | * keep a pointer to the original buffer if it needs to deallocate it. | |
576 | */ | |
394b9407 | 577 | size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) |
9504fb51 DDAG |
578 | { |
579 | if (size < IO_BUF_SIZE) { | |
580 | size_t res; | |
1dfafcbd | 581 | uint8_t *src = NULL; |
9504fb51 DDAG |
582 | |
583 | res = qemu_peek_buffer(f, &src, size, 0); | |
584 | ||
585 | if (res == size) { | |
586 | qemu_file_skip(f, res); | |
587 | *buf = src; | |
588 | return res; | |
589 | } | |
590 | } | |
591 | ||
592 | return qemu_get_buffer(f, *buf, size); | |
593 | } | |
594 | ||
548f52ea DDAG |
595 | /* |
596 | * Peeks a single byte from the buffer; this isn't guaranteed to work if | |
597 | * offset leaves a gap after the previous read/peeked data. | |
598 | */ | |
394b9407 | 599 | int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset) |
093c455a EH |
600 | { |
601 | int index = f->buf_index + offset; | |
602 | ||
603 | assert(!qemu_file_is_writable(f)); | |
548f52ea | 604 | assert(offset < IO_BUF_SIZE); |
093c455a EH |
605 | |
606 | if (index >= f->buf_size) { | |
607 | qemu_fill_buffer(f); | |
608 | index = f->buf_index + offset; | |
609 | if (index >= f->buf_size) { | |
610 | return 0; | |
611 | } | |
612 | } | |
613 | return f->buf[index]; | |
614 | } | |
615 | ||
394b9407 | 616 | int coroutine_mixed_fn qemu_get_byte(QEMUFile *f) |
093c455a EH |
617 | { |
618 | int result; | |
619 | ||
620 | result = qemu_peek_byte(f, 0); | |
621 | qemu_file_skip(f, 1); | |
622 | return result; | |
623 | } | |
624 | ||
fc95c63b | 625 | uint64_t qemu_file_transferred_noflush(QEMUFile *f) |
97221400 | 626 | { |
61abf1eb | 627 | uint64_t ret = f->total_transferred; |
97221400 AG |
628 | int i; |
629 | ||
11808bb0 DB |
630 | for (i = 0; i < f->iovcnt; i++) { |
631 | ret += f->iov[i].iov_len; | |
97221400 AG |
632 | } |
633 | ||
634 | return ret; | |
635 | } | |
636 | ||
6da835d4 | 637 | uint64_t qemu_file_transferred(QEMUFile *f) |
093c455a EH |
638 | { |
639 | qemu_fflush(f); | |
154d87b4 | 640 | return f->total_transferred; |
093c455a EH |
641 | } |
642 | ||
093c455a EH |
643 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
644 | { | |
645 | qemu_put_byte(f, v >> 8); | |
646 | qemu_put_byte(f, v); | |
647 | } | |
648 | ||
649 | void qemu_put_be32(QEMUFile *f, unsigned int v) | |
650 | { | |
651 | qemu_put_byte(f, v >> 24); | |
652 | qemu_put_byte(f, v >> 16); | |
653 | qemu_put_byte(f, v >> 8); | |
654 | qemu_put_byte(f, v); | |
655 | } | |
656 | ||
657 | void qemu_put_be64(QEMUFile *f, uint64_t v) | |
658 | { | |
659 | qemu_put_be32(f, v >> 32); | |
660 | qemu_put_be32(f, v); | |
661 | } | |
662 | ||
663 | unsigned int qemu_get_be16(QEMUFile *f) | |
664 | { | |
665 | unsigned int v; | |
666 | v = qemu_get_byte(f) << 8; | |
667 | v |= qemu_get_byte(f); | |
668 | return v; | |
669 | } | |
670 | ||
671 | unsigned int qemu_get_be32(QEMUFile *f) | |
672 | { | |
673 | unsigned int v; | |
90d6a673 | 674 | v = (unsigned int)qemu_get_byte(f) << 24; |
093c455a EH |
675 | v |= qemu_get_byte(f) << 16; |
676 | v |= qemu_get_byte(f) << 8; | |
677 | v |= qemu_get_byte(f); | |
678 | return v; | |
679 | } | |
680 | ||
681 | uint64_t qemu_get_be64(QEMUFile *f) | |
682 | { | |
683 | uint64_t v; | |
684 | v = (uint64_t)qemu_get_be32(f) << 32; | |
685 | v |= qemu_get_be32(f); | |
686 | return v; | |
687 | } | |
44f0eadc | 688 | |
dcaf446e XG |
689 | /* return the size after compression, or negative value on error */ |
690 | static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, | |
691 | const uint8_t *source, size_t source_len) | |
692 | { | |
693 | int err; | |
694 | ||
695 | err = deflateReset(stream); | |
696 | if (err != Z_OK) { | |
697 | return -1; | |
698 | } | |
699 | ||
700 | stream->avail_in = source_len; | |
701 | stream->next_in = (uint8_t *)source; | |
702 | stream->avail_out = dest_len; | |
703 | stream->next_out = dest; | |
704 | ||
705 | err = deflate(stream, Z_FINISH); | |
706 | if (err != Z_STREAM_END) { | |
707 | return -1; | |
708 | } | |
709 | ||
710 | return stream->next_out - dest; | |
711 | } | |
712 | ||
713 | /* Compress size bytes of data start at p and store the compressed | |
714 | * data to the buffer of f. | |
b3be2896 | 715 | * |
42d24611 WY |
716 | * Since the file is dummy file with empty_ops, return -1 if f has no space to |
717 | * save the compressed data. | |
44f0eadc | 718 | */ |
dcaf446e XG |
719 | ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, |
720 | const uint8_t *p, size_t size) | |
44f0eadc LL |
721 | { |
722 | ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); | |
723 | ||
724 | if (blen < compressBound(size)) { | |
42d24611 | 725 | return -1; |
44f0eadc | 726 | } |
dcaf446e XG |
727 | |
728 | blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), | |
729 | blen, p, size); | |
730 | if (blen < 0) { | |
34ab9e97 | 731 | return -1; |
44f0eadc | 732 | } |
34ab9e97 | 733 | |
44f0eadc | 734 | qemu_put_be32(f, blen); |
1bf57fb3 | 735 | add_buf_to_iovec(f, blen); |
44f0eadc LL |
736 | return blen + sizeof(int32_t); |
737 | } | |
738 | ||
739 | /* Put the data in the buffer of f_src to the buffer of f_des, and | |
740 | * then reset the buf_index of f_src to 0. | |
741 | */ | |
742 | ||
743 | int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) | |
744 | { | |
745 | int len = 0; | |
746 | ||
747 | if (f_src->buf_index > 0) { | |
748 | len = f_src->buf_index; | |
749 | qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); | |
750 | f_src->buf_index = 0; | |
787d134f | 751 | f_src->iovcnt = 0; |
44f0eadc LL |
752 | } |
753 | return len; | |
754 | } | |
b3af1bc9 | 755 | |
4024cc85 LS |
756 | /* |
757 | * Check if the writable buffer is empty | |
758 | */ | |
759 | ||
760 | bool qemu_file_buffer_empty(QEMUFile *file) | |
761 | { | |
762 | assert(qemu_file_is_writable(file)); | |
763 | ||
764 | return !file->iovcnt; | |
765 | } | |
766 | ||
b3af1bc9 DDAG |
767 | /* |
768 | * Get a string whose length is determined by a single preceding byte | |
769 | * A preallocated 256 byte buffer must be passed in. | |
770 | * Returns: len on success and a 0 terminated string in the buffer | |
771 | * else 0 | |
772 | * (Note a 0 length string will return 0 either way) | |
773 | */ | |
394b9407 | 774 | size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256]) |
b3af1bc9 DDAG |
775 | { |
776 | size_t len = qemu_get_byte(f); | |
777 | size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); | |
778 | ||
779 | buf[res] = 0; | |
780 | ||
781 | return res == len ? res : 0; | |
782 | } | |
a800cd5c | 783 | |
f0d64cb7 VSO |
784 | /* |
785 | * Put a string with one preceding byte containing its length. The length of | |
786 | * the string should be less than 256. | |
787 | */ | |
788 | void qemu_put_counted_string(QEMUFile *f, const char *str) | |
789 | { | |
790 | size_t len = strlen(str); | |
791 | ||
792 | assert(len < 256); | |
793 | qemu_put_byte(f, len); | |
794 | qemu_put_buffer(f, (const uint8_t *)str, len); | |
795 | } | |
796 | ||
a800cd5c DDAG |
797 | /* |
798 | * Set the blocking state of the QEMUFile. | |
799 | * Note: On some transports the OS only keeps a single blocking state for | |
800 | * both directions, and thus changing the blocking on the main | |
801 | * QEMUFile can also affect the return path. | |
802 | */ | |
803 | void qemu_file_set_blocking(QEMUFile *f, bool block) | |
804 | { | |
80ad9706 | 805 | qio_channel_set_blocking(f->ioc, block, NULL); |
a800cd5c | 806 | } |
c6ad5be7 PX |
807 | |
808 | /* | |
2893a288 DB |
809 | * qemu_file_get_ioc: |
810 | * | |
811 | * Get the ioc object for the file, without incrementing | |
812 | * the reference count. | |
813 | * | |
814 | * Returns: the ioc object | |
c6ad5be7 PX |
815 | */ |
816 | QIOChannel *qemu_file_get_ioc(QEMUFile *file) | |
817 | { | |
2893a288 | 818 | return file->ioc; |
c6ad5be7 | 819 | } |
c7a7db4b AH |
820 | |
821 | /* | |
822 | * Read size bytes from QEMUFile f and write them to fd. | |
823 | */ | |
824 | int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size) | |
825 | { | |
826 | while (size) { | |
827 | size_t pending = f->buf_size - f->buf_index; | |
828 | ssize_t rc; | |
829 | ||
830 | if (!pending) { | |
831 | rc = qemu_fill_buffer(f); | |
832 | if (rc < 0) { | |
833 | return rc; | |
834 | } | |
835 | if (rc == 0) { | |
836 | return -EIO; | |
837 | } | |
838 | continue; | |
839 | } | |
840 | ||
841 | rc = write(fd, f->buf + f->buf_index, MIN(pending, size)); | |
842 | if (rc < 0) { | |
843 | return -errno; | |
844 | } | |
845 | if (rc == 0) { | |
846 | return -EIO; | |
847 | } | |
848 | f->buf_index += rc; | |
849 | size -= rc; | |
850 | } | |
851 | ||
852 | return 0; | |
853 | } |