xref: /openbmc/qemu/migration/qemu-file.c (revision c1fcf220c95b17f1a05adb799824d2c352ff9281)
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 "qemu/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_new0(QEMUFile, 1);
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, NULL);
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, NULL);
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, void *data)
152 {
153     int ret = -EINVAL;
154 
155     if (f->ops->hook_ram_load) {
156         ret = f->ops->hook_ram_load(f, f->opaque, flags, data);
157         if (ret < 0) {
158             qemu_file_set_error(f, ret);
159         }
160     } else {
161         /*
162          * Hook is a hook specifically requested by the source sending a flag
163          * that expects there to be a hook on the destination.
164          */
165         if (flags == RAM_CONTROL_HOOK) {
166             qemu_file_set_error(f, ret);
167         }
168     }
169 }
170 
171 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
172                              ram_addr_t offset, size_t size,
173                              uint64_t *bytes_sent)
174 {
175     if (f->ops->save_page) {
176         int ret = f->ops->save_page(f, f->opaque, block_offset,
177                                     offset, size, bytes_sent);
178 
179         if (ret != RAM_SAVE_CONTROL_DELAYED) {
180             if (bytes_sent && *bytes_sent > 0) {
181                 qemu_update_position(f, *bytes_sent);
182             } else if (ret < 0) {
183                 qemu_file_set_error(f, ret);
184             }
185         }
186 
187         return ret;
188     }
189 
190     return RAM_SAVE_CONTROL_NOT_SUPP;
191 }
192 
193 /*
194  * Attempt to fill the buffer from the underlying file
195  * Returns the number of bytes read, or negative value for an error.
196  *
197  * Note that it can return a partially full buffer even in a not error/not EOF
198  * case if the underlying file descriptor gives a short read, and that can
199  * happen even on a blocking fd.
200  */
201 static ssize_t qemu_fill_buffer(QEMUFile *f)
202 {
203     int len;
204     int pending;
205 
206     assert(!qemu_file_is_writable(f));
207 
208     pending = f->buf_size - f->buf_index;
209     if (pending > 0) {
210         memmove(f->buf, f->buf + f->buf_index, pending);
211     }
212     f->buf_index = 0;
213     f->buf_size = pending;
214 
215     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
216                         IO_BUF_SIZE - pending);
217     if (len > 0) {
218         f->buf_size += len;
219         f->pos += len;
220     } else if (len == 0) {
221         qemu_file_set_error(f, -EIO);
222     } else if (len != -EAGAIN) {
223         qemu_file_set_error(f, len);
224     }
225 
226     return len;
227 }
228 
229 int qemu_get_fd(QEMUFile *f)
230 {
231     if (f->ops->get_fd) {
232         return f->ops->get_fd(f->opaque);
233     }
234     return -1;
235 }
236 
237 void qemu_update_position(QEMUFile *f, size_t size)
238 {
239     f->pos += size;
240 }
241 
242 /** Closes the file
243  *
244  * Returns negative error value if any error happened on previous operations or
245  * while closing the file. Returns 0 or positive number on success.
246  *
247  * The meaning of return value on success depends on the specific backend
248  * being used.
249  */
250 int qemu_fclose(QEMUFile *f)
251 {
252     int ret;
253     qemu_fflush(f);
254     ret = qemu_file_get_error(f);
255 
256     if (f->ops->close) {
257         int ret2 = f->ops->close(f->opaque);
258         if (ret >= 0) {
259             ret = ret2;
260         }
261     }
262     /* If any error was spotted before closing, we should report it
263      * instead of the close() return value.
264      */
265     if (f->last_error) {
266         ret = f->last_error;
267     }
268     g_free(f);
269     trace_qemu_file_fclose();
270     return ret;
271 }
272 
273 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size)
274 {
275     /* check for adjacent buffer and coalesce them */
276     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
277         f->iov[f->iovcnt - 1].iov_len) {
278         f->iov[f->iovcnt - 1].iov_len += size;
279     } else {
280         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
281         f->iov[f->iovcnt++].iov_len = size;
282     }
283 
284     if (f->iovcnt >= MAX_IOV_SIZE) {
285         qemu_fflush(f);
286     }
287 }
288 
289 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size)
290 {
291     if (!f->ops->writev_buffer) {
292         qemu_put_buffer(f, buf, size);
293         return;
294     }
295 
296     if (f->last_error) {
297         return;
298     }
299 
300     f->bytes_xfer += size;
301     add_to_iovec(f, buf, size);
302 }
303 
304 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
305 {
306     size_t l;
307 
308     if (f->last_error) {
309         return;
310     }
311 
312     while (size > 0) {
313         l = IO_BUF_SIZE - f->buf_index;
314         if (l > size) {
315             l = size;
316         }
317         memcpy(f->buf + f->buf_index, buf, l);
318         f->bytes_xfer += l;
319         if (f->ops->writev_buffer) {
320             add_to_iovec(f, f->buf + f->buf_index, l);
321         }
322         f->buf_index += l;
323         if (f->buf_index == IO_BUF_SIZE) {
324             qemu_fflush(f);
325         }
326         if (qemu_file_get_error(f)) {
327             break;
328         }
329         buf += l;
330         size -= l;
331     }
332 }
333 
334 void qemu_put_byte(QEMUFile *f, int v)
335 {
336     if (f->last_error) {
337         return;
338     }
339 
340     f->buf[f->buf_index] = v;
341     f->bytes_xfer++;
342     if (f->ops->writev_buffer) {
343         add_to_iovec(f, f->buf + f->buf_index, 1);
344     }
345     f->buf_index++;
346     if (f->buf_index == IO_BUF_SIZE) {
347         qemu_fflush(f);
348     }
349 }
350 
351 void qemu_file_skip(QEMUFile *f, int size)
352 {
353     if (f->buf_index + size <= f->buf_size) {
354         f->buf_index += size;
355     }
356 }
357 
358 /*
359  * Read 'size' bytes from file (at 'offset') without moving the
360  * pointer and set 'buf' to point to that data.
361  *
362  * It will return size bytes unless there was an error, in which case it will
363  * return as many as it managed to read (assuming blocking fd's which
364  * all current QEMUFile are)
365  */
366 size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
367 {
368     ssize_t pending;
369     size_t index;
370 
371     assert(!qemu_file_is_writable(f));
372     assert(offset < IO_BUF_SIZE);
373     assert(size <= IO_BUF_SIZE - offset);
374 
375     /* The 1st byte to read from */
376     index = f->buf_index + offset;
377     /* The number of available bytes starting at index */
378     pending = f->buf_size - index;
379 
380     /*
381      * qemu_fill_buffer might return just a few bytes, even when there isn't
382      * an error, so loop collecting them until we get enough.
383      */
384     while (pending < size) {
385         int received = qemu_fill_buffer(f);
386 
387         if (received <= 0) {
388             break;
389         }
390 
391         index = f->buf_index + offset;
392         pending = f->buf_size - index;
393     }
394 
395     if (pending <= 0) {
396         return 0;
397     }
398     if (size > pending) {
399         size = pending;
400     }
401 
402     *buf = f->buf + index;
403     return size;
404 }
405 
406 /*
407  * Read 'size' bytes of data from the file into buf.
408  * 'size' can be larger than the internal buffer.
409  *
410  * It will return size bytes unless there was an error, in which case it will
411  * return as many as it managed to read (assuming blocking fd's which
412  * all current QEMUFile are)
413  */
414 size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
415 {
416     size_t pending = size;
417     size_t done = 0;
418 
419     while (pending > 0) {
420         size_t res;
421         uint8_t *src;
422 
423         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
424         if (res == 0) {
425             return done;
426         }
427         memcpy(buf, src, res);
428         qemu_file_skip(f, res);
429         buf += res;
430         pending -= res;
431         done += res;
432     }
433     return done;
434 }
435 
436 /*
437  * Read 'size' bytes of data from the file.
438  * 'size' can be larger than the internal buffer.
439  *
440  * The data:
441  *   may be held on an internal buffer (in which case *buf is updated
442  *     to point to it) that is valid until the next qemu_file operation.
443  * OR
444  *   will be copied to the *buf that was passed in.
445  *
446  * The code tries to avoid the copy if possible.
447  *
448  * It will return size bytes unless there was an error, in which case it will
449  * return as many as it managed to read (assuming blocking fd's which
450  * all current QEMUFile are)
451  *
452  * Note: Since **buf may get changed, the caller should take care to
453  *       keep a pointer to the original buffer if it needs to deallocate it.
454  */
455 size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
456 {
457     if (size < IO_BUF_SIZE) {
458         size_t res;
459         uint8_t *src;
460 
461         res = qemu_peek_buffer(f, &src, size, 0);
462 
463         if (res == size) {
464             qemu_file_skip(f, res);
465             *buf = src;
466             return res;
467         }
468     }
469 
470     return qemu_get_buffer(f, *buf, size);
471 }
472 
473 /*
474  * Peeks a single byte from the buffer; this isn't guaranteed to work if
475  * offset leaves a gap after the previous read/peeked data.
476  */
477 int qemu_peek_byte(QEMUFile *f, int offset)
478 {
479     int index = f->buf_index + offset;
480 
481     assert(!qemu_file_is_writable(f));
482     assert(offset < IO_BUF_SIZE);
483 
484     if (index >= f->buf_size) {
485         qemu_fill_buffer(f);
486         index = f->buf_index + offset;
487         if (index >= f->buf_size) {
488             return 0;
489         }
490     }
491     return f->buf[index];
492 }
493 
494 int qemu_get_byte(QEMUFile *f)
495 {
496     int result;
497 
498     result = qemu_peek_byte(f, 0);
499     qemu_file_skip(f, 1);
500     return result;
501 }
502 
503 int64_t qemu_ftell_fast(QEMUFile *f)
504 {
505     int64_t ret = f->pos;
506     int i;
507 
508     if (f->ops->writev_buffer) {
509         for (i = 0; i < f->iovcnt; i++) {
510             ret += f->iov[i].iov_len;
511         }
512     } else {
513         ret += f->buf_index;
514     }
515 
516     return ret;
517 }
518 
519 int64_t qemu_ftell(QEMUFile *f)
520 {
521     qemu_fflush(f);
522     return f->pos;
523 }
524 
525 int qemu_file_rate_limit(QEMUFile *f)
526 {
527     if (qemu_file_get_error(f)) {
528         return 1;
529     }
530     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
531         return 1;
532     }
533     return 0;
534 }
535 
536 int64_t qemu_file_get_rate_limit(QEMUFile *f)
537 {
538     return f->xfer_limit;
539 }
540 
541 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
542 {
543     f->xfer_limit = limit;
544 }
545 
546 void qemu_file_reset_rate_limit(QEMUFile *f)
547 {
548     f->bytes_xfer = 0;
549 }
550 
551 void qemu_put_be16(QEMUFile *f, unsigned int v)
552 {
553     qemu_put_byte(f, v >> 8);
554     qemu_put_byte(f, v);
555 }
556 
557 void qemu_put_be32(QEMUFile *f, unsigned int v)
558 {
559     qemu_put_byte(f, v >> 24);
560     qemu_put_byte(f, v >> 16);
561     qemu_put_byte(f, v >> 8);
562     qemu_put_byte(f, v);
563 }
564 
565 void qemu_put_be64(QEMUFile *f, uint64_t v)
566 {
567     qemu_put_be32(f, v >> 32);
568     qemu_put_be32(f, v);
569 }
570 
571 unsigned int qemu_get_be16(QEMUFile *f)
572 {
573     unsigned int v;
574     v = qemu_get_byte(f) << 8;
575     v |= qemu_get_byte(f);
576     return v;
577 }
578 
579 unsigned int qemu_get_be32(QEMUFile *f)
580 {
581     unsigned int v;
582     v = (unsigned int)qemu_get_byte(f) << 24;
583     v |= qemu_get_byte(f) << 16;
584     v |= qemu_get_byte(f) << 8;
585     v |= qemu_get_byte(f);
586     return v;
587 }
588 
589 uint64_t qemu_get_be64(QEMUFile *f)
590 {
591     uint64_t v;
592     v = (uint64_t)qemu_get_be32(f) << 32;
593     v |= qemu_get_be32(f);
594     return v;
595 }
596 
597 /* compress size bytes of data start at p with specific compression
598  * level and store the compressed data to the buffer of f.
599  */
600 
601 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
602                                   int level)
603 {
604     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
605 
606     if (blen < compressBound(size)) {
607         return 0;
608     }
609     if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
610                   (Bytef *)p, size, level) != Z_OK) {
611         error_report("Compress Failed!");
612         return 0;
613     }
614     qemu_put_be32(f, blen);
615     f->buf_index += blen;
616     return blen + sizeof(int32_t);
617 }
618 
619 /* Put the data in the buffer of f_src to the buffer of f_des, and
620  * then reset the buf_index of f_src to 0.
621  */
622 
623 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
624 {
625     int len = 0;
626 
627     if (f_src->buf_index > 0) {
628         len = f_src->buf_index;
629         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
630         f_src->buf_index = 0;
631     }
632     return len;
633 }
634 
635 /*
636  * Get a string whose length is determined by a single preceding byte
637  * A preallocated 256 byte buffer must be passed in.
638  * Returns: len on success and a 0 terminated string in the buffer
639  *          else 0
640  *          (Note a 0 length string will return 0 either way)
641  */
642 size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
643 {
644     size_t len = qemu_get_byte(f);
645     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
646 
647     buf[res] = 0;
648 
649     return res == len ? res : 0;
650 }
651 
652 /*
653  * Set the blocking state of the QEMUFile.
654  * Note: On some transports the OS only keeps a single blocking state for
655  *       both directions, and thus changing the blocking on the main
656  *       QEMUFile can also affect the return path.
657  */
658 void qemu_file_set_blocking(QEMUFile *f, bool block)
659 {
660     if (block) {
661         qemu_set_block(qemu_get_fd(f));
662     } else {
663         qemu_set_nonblock(qemu_get_fd(f));
664     }
665 }
666