xref: /openbmc/qemu/migration/qemu-file.c (revision 12dc273e98e4e111880b25c12bf671dc8951b8e6)
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  */
24 #include <zlib.h>
25 #include "qemu-common.h"
26 #include "qemu/error-report.h"
27 #include "qemu/iov.h"
28 #include "qemu/sockets.h"
29 #include "block/coroutine.h"
30 #include "migration/migration.h"
31 #include "migration/qemu-file.h"
32 #include "migration/qemu-file-internal.h"
33 #include "trace.h"
34 
35 /*
36  * Stop a file from being read/written - not all backing files can do this
37  * typically only sockets can.
38  */
39 int qemu_file_shutdown(QEMUFile *f)
40 {
41     if (!f->ops->shut_down) {
42         return -ENOSYS;
43     }
44     return f->ops->shut_down(f->opaque, true, true);
45 }
46 
47 bool qemu_file_mode_is_not_valid(const char *mode)
48 {
49     if (mode == NULL ||
50         (mode[0] != 'r' && mode[0] != 'w') ||
51         mode[1] != 'b' || mode[2] != 0) {
52         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
53         return true;
54     }
55 
56     return false;
57 }
58 
59 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
60 {
61     QEMUFile *f;
62 
63     f = g_malloc0(sizeof(QEMUFile));
64 
65     f->opaque = opaque;
66     f->ops = ops;
67     return f;
68 }
69 
70 /*
71  * Get last error for stream f
72  *
73  * Return negative error value if there has been an error on previous
74  * operations, return 0 if no error happened.
75  *
76  */
77 int qemu_file_get_error(QEMUFile *f)
78 {
79     return f->last_error;
80 }
81 
82 void qemu_file_set_error(QEMUFile *f, int ret)
83 {
84     if (f->last_error == 0) {
85         f->last_error = ret;
86     }
87 }
88 
89 bool qemu_file_is_writable(QEMUFile *f)
90 {
91     return f->ops->writev_buffer || f->ops->put_buffer;
92 }
93 
94 /**
95  * Flushes QEMUFile buffer
96  *
97  * If there is writev_buffer QEMUFileOps it uses it otherwise uses
98  * put_buffer ops.
99  */
100 void qemu_fflush(QEMUFile *f)
101 {
102     ssize_t ret = 0;
103 
104     if (!qemu_file_is_writable(f)) {
105         return;
106     }
107 
108     if (f->ops->writev_buffer) {
109         if (f->iovcnt > 0) {
110             ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
111         }
112     } else {
113         if (f->buf_index > 0) {
114             ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
115         }
116     }
117     if (ret >= 0) {
118         f->pos += ret;
119     }
120     f->buf_index = 0;
121     f->iovcnt = 0;
122     if (ret < 0) {
123         qemu_file_set_error(f, ret);
124     }
125 }
126 
127 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
128 {
129     int ret = 0;
130 
131     if (f->ops->before_ram_iterate) {
132         ret = f->ops->before_ram_iterate(f, f->opaque, flags);
133         if (ret < 0) {
134             qemu_file_set_error(f, ret);
135         }
136     }
137 }
138 
139 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
140 {
141     int ret = 0;
142 
143     if (f->ops->after_ram_iterate) {
144         ret = f->ops->after_ram_iterate(f, f->opaque, flags);
145         if (ret < 0) {
146             qemu_file_set_error(f, ret);
147         }
148     }
149 }
150 
151 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
152 {
153     int ret = -EINVAL;
154 
155     if (f->ops->hook_ram_load) {
156         ret = f->ops->hook_ram_load(f, f->opaque, flags);
157         if (ret < 0) {
158             qemu_file_set_error(f, ret);
159         }
160     } else {
161         qemu_file_set_error(f, ret);
162     }
163 }
164 
165 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
166                              ram_addr_t offset, size_t size,
167                              uint64_t *bytes_sent)
168 {
169     if (f->ops->save_page) {
170         int ret = f->ops->save_page(f, f->opaque, block_offset,
171                                     offset, size, bytes_sent);
172 
173         if (ret != RAM_SAVE_CONTROL_DELAYED) {
174             if (bytes_sent && *bytes_sent > 0) {
175                 qemu_update_position(f, *bytes_sent);
176             } else if (ret < 0) {
177                 qemu_file_set_error(f, ret);
178             }
179         }
180 
181         return ret;
182     }
183 
184     return RAM_SAVE_CONTROL_NOT_SUPP;
185 }
186 
187 /*
188  * Attempt to fill the buffer from the underlying file
189  * Returns the number of bytes read, or negative value for an error.
190  *
191  * Note that it can return a partially full buffer even in a not error/not EOF
192  * case if the underlying file descriptor gives a short read, and that can
193  * happen even on a blocking fd.
194  */
195 static ssize_t qemu_fill_buffer(QEMUFile *f)
196 {
197     int len;
198     int pending;
199 
200     assert(!qemu_file_is_writable(f));
201 
202     pending = f->buf_size - f->buf_index;
203     if (pending > 0) {
204         memmove(f->buf, f->buf + f->buf_index, pending);
205     }
206     f->buf_index = 0;
207     f->buf_size = pending;
208 
209     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
210                         IO_BUF_SIZE - pending);
211     if (len > 0) {
212         f->buf_size += len;
213         f->pos += len;
214     } else if (len == 0) {
215         qemu_file_set_error(f, -EIO);
216     } else if (len != -EAGAIN) {
217         qemu_file_set_error(f, len);
218     }
219 
220     return len;
221 }
222 
223 int qemu_get_fd(QEMUFile *f)
224 {
225     if (f->ops->get_fd) {
226         return f->ops->get_fd(f->opaque);
227     }
228     return -1;
229 }
230 
231 void qemu_update_position(QEMUFile *f, size_t size)
232 {
233     f->pos += size;
234 }
235 
236 /** Closes the file
237  *
238  * Returns negative error value if any error happened on previous operations or
239  * while closing the file. Returns 0 or positive number on success.
240  *
241  * The meaning of return value on success depends on the specific backend
242  * being used.
243  */
244 int qemu_fclose(QEMUFile *f)
245 {
246     int ret;
247     qemu_fflush(f);
248     ret = qemu_file_get_error(f);
249 
250     if (f->ops->close) {
251         int ret2 = f->ops->close(f->opaque);
252         if (ret >= 0) {
253             ret = ret2;
254         }
255     }
256     /* If any error was spotted before closing, we should report it
257      * instead of the close() return value.
258      */
259     if (f->last_error) {
260         ret = f->last_error;
261     }
262     g_free(f);
263     trace_qemu_file_fclose();
264     return ret;
265 }
266 
267 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
268 {
269     /* check for adjacent buffer and coalesce them */
270     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
271         f->iov[f->iovcnt - 1].iov_len) {
272         f->iov[f->iovcnt - 1].iov_len += size;
273     } else {
274         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
275         f->iov[f->iovcnt++].iov_len = size;
276     }
277 
278     if (f->iovcnt >= MAX_IOV_SIZE) {
279         qemu_fflush(f);
280     }
281 }
282 
283 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
284 {
285     if (!f->ops->writev_buffer) {
286         qemu_put_buffer(f, buf, size);
287         return;
288     }
289 
290     if (f->last_error) {
291         return;
292     }
293 
294     f->bytes_xfer += size;
295     add_to_iovec(f, buf, size);
296 }
297 
298 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
299 {
300     int l;
301 
302     if (f->last_error) {
303         return;
304     }
305 
306     while (size > 0) {
307         l = IO_BUF_SIZE - f->buf_index;
308         if (l > size) {
309             l = size;
310         }
311         memcpy(f->buf + f->buf_index, buf, l);
312         f->bytes_xfer += l;
313         if (f->ops->writev_buffer) {
314             add_to_iovec(f, f->buf + f->buf_index, l);
315         }
316         f->buf_index += l;
317         if (f->buf_index == IO_BUF_SIZE) {
318             qemu_fflush(f);
319         }
320         if (qemu_file_get_error(f)) {
321             break;
322         }
323         buf += l;
324         size -= l;
325     }
326 }
327 
328 void qemu_put_byte(QEMUFile *f, int v)
329 {
330     if (f->last_error) {
331         return;
332     }
333 
334     f->buf[f->buf_index] = v;
335     f->bytes_xfer++;
336     if (f->ops->writev_buffer) {
337         add_to_iovec(f, f->buf + f->buf_index, 1);
338     }
339     f->buf_index++;
340     if (f->buf_index == IO_BUF_SIZE) {
341         qemu_fflush(f);
342     }
343 }
344 
345 void qemu_file_skip(QEMUFile *f, int size)
346 {
347     if (f->buf_index + size <= f->buf_size) {
348         f->buf_index += size;
349     }
350 }
351 
352 /*
353  * Read 'size' bytes from file (at 'offset') without moving the
354  * pointer and set 'buf' to point to that data.
355  *
356  * It will return size bytes unless there was an error, in which case it will
357  * return as many as it managed to read (assuming blocking fd's which
358  * all current QEMUFile are)
359  */
360 int qemu_peek_buffer(QEMUFile *f, uint8_t **buf, int size, size_t offset)
361 {
362     int pending;
363     int index;
364 
365     assert(!qemu_file_is_writable(f));
366     assert(offset < IO_BUF_SIZE);
367     assert(size <= IO_BUF_SIZE - offset);
368 
369     /* The 1st byte to read from */
370     index = f->buf_index + offset;
371     /* The number of available bytes starting at index */
372     pending = f->buf_size - index;
373 
374     /*
375      * qemu_fill_buffer might return just a few bytes, even when there isn't
376      * an error, so loop collecting them until we get enough.
377      */
378     while (pending < size) {
379         int received = qemu_fill_buffer(f);
380 
381         if (received <= 0) {
382             break;
383         }
384 
385         index = f->buf_index + offset;
386         pending = f->buf_size - index;
387     }
388 
389     if (pending <= 0) {
390         return 0;
391     }
392     if (size > pending) {
393         size = pending;
394     }
395 
396     *buf = f->buf + index;
397     return size;
398 }
399 
400 /*
401  * Read 'size' bytes of data from the file into buf.
402  * 'size' can be larger than the internal buffer.
403  *
404  * It will return size bytes unless there was an error, in which case it will
405  * return as many as it managed to read (assuming blocking fd's which
406  * all current QEMUFile are)
407  */
408 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
409 {
410     int pending = size;
411     int done = 0;
412 
413     while (pending > 0) {
414         int res;
415         uint8_t *src;
416 
417         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
418         if (res == 0) {
419             return done;
420         }
421         memcpy(buf, src, res);
422         qemu_file_skip(f, res);
423         buf += res;
424         pending -= res;
425         done += res;
426     }
427     return done;
428 }
429 
430 /*
431  * Peeks a single byte from the buffer; this isn't guaranteed to work if
432  * offset leaves a gap after the previous read/peeked data.
433  */
434 int qemu_peek_byte(QEMUFile *f, int offset)
435 {
436     int index = f->buf_index + offset;
437 
438     assert(!qemu_file_is_writable(f));
439     assert(offset < IO_BUF_SIZE);
440 
441     if (index >= f->buf_size) {
442         qemu_fill_buffer(f);
443         index = f->buf_index + offset;
444         if (index >= f->buf_size) {
445             return 0;
446         }
447     }
448     return f->buf[index];
449 }
450 
451 int qemu_get_byte(QEMUFile *f)
452 {
453     int result;
454 
455     result = qemu_peek_byte(f, 0);
456     qemu_file_skip(f, 1);
457     return result;
458 }
459 
460 int64_t qemu_ftell_fast(QEMUFile *f)
461 {
462     int64_t ret = f->pos;
463     int i;
464 
465     if (f->ops->writev_buffer) {
466         for (i = 0; i < f->iovcnt; i++) {
467             ret += f->iov[i].iov_len;
468         }
469     } else {
470         ret += f->buf_index;
471     }
472 
473     return ret;
474 }
475 
476 int64_t qemu_ftell(QEMUFile *f)
477 {
478     qemu_fflush(f);
479     return f->pos;
480 }
481 
482 int qemu_file_rate_limit(QEMUFile *f)
483 {
484     if (qemu_file_get_error(f)) {
485         return 1;
486     }
487     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
488         return 1;
489     }
490     return 0;
491 }
492 
493 int64_t qemu_file_get_rate_limit(QEMUFile *f)
494 {
495     return f->xfer_limit;
496 }
497 
498 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
499 {
500     f->xfer_limit = limit;
501 }
502 
503 void qemu_file_reset_rate_limit(QEMUFile *f)
504 {
505     f->bytes_xfer = 0;
506 }
507 
508 void qemu_put_be16(QEMUFile *f, unsigned int v)
509 {
510     qemu_put_byte(f, v >> 8);
511     qemu_put_byte(f, v);
512 }
513 
514 void qemu_put_be32(QEMUFile *f, unsigned int v)
515 {
516     qemu_put_byte(f, v >> 24);
517     qemu_put_byte(f, v >> 16);
518     qemu_put_byte(f, v >> 8);
519     qemu_put_byte(f, v);
520 }
521 
522 void qemu_put_be64(QEMUFile *f, uint64_t v)
523 {
524     qemu_put_be32(f, v >> 32);
525     qemu_put_be32(f, v);
526 }
527 
528 unsigned int qemu_get_be16(QEMUFile *f)
529 {
530     unsigned int v;
531     v = qemu_get_byte(f) << 8;
532     v |= qemu_get_byte(f);
533     return v;
534 }
535 
536 unsigned int qemu_get_be32(QEMUFile *f)
537 {
538     unsigned int v;
539     v = (unsigned int)qemu_get_byte(f) << 24;
540     v |= qemu_get_byte(f) << 16;
541     v |= qemu_get_byte(f) << 8;
542     v |= qemu_get_byte(f);
543     return v;
544 }
545 
546 uint64_t qemu_get_be64(QEMUFile *f)
547 {
548     uint64_t v;
549     v = (uint64_t)qemu_get_be32(f) << 32;
550     v |= qemu_get_be32(f);
551     return v;
552 }
553 
554 /* compress size bytes of data start at p with specific compression
555  * level and store the compressed data to the buffer of f.
556  */
557 
558 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
559                                   int level)
560 {
561     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
562 
563     if (blen < compressBound(size)) {
564         return 0;
565     }
566     if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
567                   (Bytef *)p, size, level) != Z_OK) {
568         error_report("Compress Failed!");
569         return 0;
570     }
571     qemu_put_be32(f, blen);
572     f->buf_index += blen;
573     return blen + sizeof(int32_t);
574 }
575 
576 /* Put the data in the buffer of f_src to the buffer of f_des, and
577  * then reset the buf_index of f_src to 0.
578  */
579 
580 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
581 {
582     int len = 0;
583 
584     if (f_src->buf_index > 0) {
585         len = f_src->buf_index;
586         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
587         f_src->buf_index = 0;
588     }
589     return len;
590 }
591 
592 /*
593  * Get a string whose length is determined by a single preceding byte
594  * A preallocated 256 byte buffer must be passed in.
595  * Returns: len on success and a 0 terminated string in the buffer
596  *          else 0
597  *          (Note a 0 length string will return 0 either way)
598  */
599 size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
600 {
601     size_t len = qemu_get_byte(f);
602     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
603 
604     buf[res] = 0;
605 
606     return res == len ? res : 0;
607 }
608