xref: /qemu/migration/qemu-file.c (revision 6402cbbb)
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 "qemu/osdep.h"
25 #include <zlib.h>
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
28 #include "qemu/iov.h"
29 #include "migration.h"
30 #include "qemu-file.h"
31 #include "trace.h"
32 
33 #define IO_BUF_SIZE 32768
34 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
35 
36 struct QEMUFile {
37     const QEMUFileOps *ops;
38     const QEMUFileHooks *hooks;
39     void *opaque;
40 
41     int64_t bytes_xfer;
42     int64_t xfer_limit;
43 
44     int64_t pos; /* start of buffer when writing, end of buffer
45                     when reading */
46     int buf_index;
47     int buf_size; /* 0 when writing */
48     uint8_t buf[IO_BUF_SIZE];
49 
50     DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
51     struct iovec iov[MAX_IOV_SIZE];
52     unsigned int iovcnt;
53 
54     int last_error;
55 };
56 
57 /*
58  * Stop a file from being read/written - not all backing files can do this
59  * typically only sockets can.
60  */
61 int qemu_file_shutdown(QEMUFile *f)
62 {
63     if (!f->ops->shut_down) {
64         return -ENOSYS;
65     }
66     return f->ops->shut_down(f->opaque, true, true);
67 }
68 
69 /*
70  * Result: QEMUFile* for a 'return path' for comms in the opposite direction
71  *         NULL if not available
72  */
73 QEMUFile *qemu_file_get_return_path(QEMUFile *f)
74 {
75     if (!f->ops->get_return_path) {
76         return NULL;
77     }
78     return f->ops->get_return_path(f->opaque);
79 }
80 
81 bool qemu_file_mode_is_not_valid(const char *mode)
82 {
83     if (mode == NULL ||
84         (mode[0] != 'r' && mode[0] != 'w') ||
85         mode[1] != 'b' || mode[2] != 0) {
86         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
87         return true;
88     }
89 
90     return false;
91 }
92 
93 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
94 {
95     QEMUFile *f;
96 
97     f = g_new0(QEMUFile, 1);
98 
99     f->opaque = opaque;
100     f->ops = ops;
101     return f;
102 }
103 
104 
105 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
106 {
107     f->hooks = hooks;
108 }
109 
110 /*
111  * Get last error for stream f
112  *
113  * Return negative error value if there has been an error on previous
114  * operations, return 0 if no error happened.
115  *
116  */
117 int qemu_file_get_error(QEMUFile *f)
118 {
119     return f->last_error;
120 }
121 
122 void qemu_file_set_error(QEMUFile *f, int ret)
123 {
124     if (f->last_error == 0) {
125         f->last_error = ret;
126     }
127 }
128 
129 bool qemu_file_is_writable(QEMUFile *f)
130 {
131     return f->ops->writev_buffer;
132 }
133 
134 static void qemu_iovec_release_ram(QEMUFile *f)
135 {
136     struct iovec iov;
137     unsigned long idx;
138 
139     /* Find and release all the contiguous memory ranges marked as may_free. */
140     idx = find_next_bit(f->may_free, f->iovcnt, 0);
141     if (idx >= f->iovcnt) {
142         return;
143     }
144     iov = f->iov[idx];
145 
146     /* The madvise() in the loop is called for iov within a continuous range and
147      * then reinitialize the iov. And in the end, madvise() is called for the
148      * last iov.
149      */
150     while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
151         /* check for adjacent buffer and coalesce them */
152         if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
153             iov.iov_len += f->iov[idx].iov_len;
154             continue;
155         }
156         if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
157             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
158                          iov.iov_base, iov.iov_len, strerror(errno));
159         }
160         iov = f->iov[idx];
161     }
162     if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
163             error_report("migrate: madvise DONTNEED failed %p %zd: %s",
164                          iov.iov_base, iov.iov_len, strerror(errno));
165     }
166     memset(f->may_free, 0, sizeof(f->may_free));
167 }
168 
169 /**
170  * Flushes QEMUFile buffer
171  *
172  * If there is writev_buffer QEMUFileOps it uses it otherwise uses
173  * put_buffer ops. This will flush all pending data. If data was
174  * only partially flushed, it will set an error state.
175  */
176 void qemu_fflush(QEMUFile *f)
177 {
178     ssize_t ret = 0;
179     ssize_t expect = 0;
180 
181     if (!qemu_file_is_writable(f)) {
182         return;
183     }
184 
185     if (f->iovcnt > 0) {
186         expect = iov_size(f->iov, f->iovcnt);
187         ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
188 
189         qemu_iovec_release_ram(f);
190     }
191 
192     if (ret >= 0) {
193         f->pos += ret;
194     }
195     /* We expect the QEMUFile write impl to send the full
196      * data set we requested, so sanity check that.
197      */
198     if (ret != expect) {
199         qemu_file_set_error(f, ret < 0 ? ret : -EIO);
200     }
201     f->buf_index = 0;
202     f->iovcnt = 0;
203 }
204 
205 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
206 {
207     int ret = 0;
208 
209     if (f->hooks && f->hooks->before_ram_iterate) {
210         ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL);
211         if (ret < 0) {
212             qemu_file_set_error(f, ret);
213         }
214     }
215 }
216 
217 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
218 {
219     int ret = 0;
220 
221     if (f->hooks && f->hooks->after_ram_iterate) {
222         ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL);
223         if (ret < 0) {
224             qemu_file_set_error(f, ret);
225         }
226     }
227 }
228 
229 void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
230 {
231     int ret = -EINVAL;
232 
233     if (f->hooks && f->hooks->hook_ram_load) {
234         ret = f->hooks->hook_ram_load(f, f->opaque, flags, data);
235         if (ret < 0) {
236             qemu_file_set_error(f, ret);
237         }
238     } else {
239         /*
240          * Hook is a hook specifically requested by the source sending a flag
241          * that expects there to be a hook on the destination.
242          */
243         if (flags == RAM_CONTROL_HOOK) {
244             qemu_file_set_error(f, ret);
245         }
246     }
247 }
248 
249 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
250                              ram_addr_t offset, size_t size,
251                              uint64_t *bytes_sent)
252 {
253     if (f->hooks && f->hooks->save_page) {
254         int ret = f->hooks->save_page(f, f->opaque, block_offset,
255                                       offset, size, bytes_sent);
256 
257         if (ret != RAM_SAVE_CONTROL_DELAYED) {
258             if (bytes_sent && *bytes_sent > 0) {
259                 qemu_update_position(f, *bytes_sent);
260             } else if (ret < 0) {
261                 qemu_file_set_error(f, ret);
262             }
263         }
264 
265         return ret;
266     }
267 
268     return RAM_SAVE_CONTROL_NOT_SUPP;
269 }
270 
271 /*
272  * Attempt to fill the buffer from the underlying file
273  * Returns the number of bytes read, or negative value for an error.
274  *
275  * Note that it can return a partially full buffer even in a not error/not EOF
276  * case if the underlying file descriptor gives a short read, and that can
277  * happen even on a blocking fd.
278  */
279 static ssize_t qemu_fill_buffer(QEMUFile *f)
280 {
281     int len;
282     int pending;
283 
284     assert(!qemu_file_is_writable(f));
285 
286     pending = f->buf_size - f->buf_index;
287     if (pending > 0) {
288         memmove(f->buf, f->buf + f->buf_index, pending);
289     }
290     f->buf_index = 0;
291     f->buf_size = pending;
292 
293     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
294                         IO_BUF_SIZE - pending);
295     if (len > 0) {
296         f->buf_size += len;
297         f->pos += len;
298     } else if (len == 0) {
299         qemu_file_set_error(f, -EIO);
300     } else if (len != -EAGAIN) {
301         qemu_file_set_error(f, len);
302     }
303 
304     return len;
305 }
306 
307 void qemu_update_position(QEMUFile *f, size_t size)
308 {
309     f->pos += size;
310 }
311 
312 /** Closes the file
313  *
314  * Returns negative error value if any error happened on previous operations or
315  * while closing the file. Returns 0 or positive number on success.
316  *
317  * The meaning of return value on success depends on the specific backend
318  * being used.
319  */
320 int qemu_fclose(QEMUFile *f)
321 {
322     int ret;
323     qemu_fflush(f);
324     ret = qemu_file_get_error(f);
325 
326     if (f->ops->close) {
327         int ret2 = f->ops->close(f->opaque);
328         if (ret >= 0) {
329             ret = ret2;
330         }
331     }
332     /* If any error was spotted before closing, we should report it
333      * instead of the close() return value.
334      */
335     if (f->last_error) {
336         ret = f->last_error;
337     }
338     g_free(f);
339     trace_qemu_file_fclose();
340     return ret;
341 }
342 
343 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
344                          bool may_free)
345 {
346     /* check for adjacent buffer and coalesce them */
347     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
348         f->iov[f->iovcnt - 1].iov_len &&
349         may_free == test_bit(f->iovcnt - 1, f->may_free))
350     {
351         f->iov[f->iovcnt - 1].iov_len += size;
352     } else {
353         if (may_free) {
354             set_bit(f->iovcnt, f->may_free);
355         }
356         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
357         f->iov[f->iovcnt++].iov_len = size;
358     }
359 
360     if (f->iovcnt >= MAX_IOV_SIZE) {
361         qemu_fflush(f);
362     }
363 }
364 
365 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
366                            bool may_free)
367 {
368     if (f->last_error) {
369         return;
370     }
371 
372     f->bytes_xfer += size;
373     add_to_iovec(f, buf, size, may_free);
374 }
375 
376 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
377 {
378     size_t l;
379 
380     if (f->last_error) {
381         return;
382     }
383 
384     while (size > 0) {
385         l = IO_BUF_SIZE - f->buf_index;
386         if (l > size) {
387             l = size;
388         }
389         memcpy(f->buf + f->buf_index, buf, l);
390         f->bytes_xfer += l;
391         add_to_iovec(f, f->buf + f->buf_index, l, false);
392         f->buf_index += l;
393         if (f->buf_index == IO_BUF_SIZE) {
394             qemu_fflush(f);
395         }
396         if (qemu_file_get_error(f)) {
397             break;
398         }
399         buf += l;
400         size -= l;
401     }
402 }
403 
404 void qemu_put_byte(QEMUFile *f, int v)
405 {
406     if (f->last_error) {
407         return;
408     }
409 
410     f->buf[f->buf_index] = v;
411     f->bytes_xfer++;
412     add_to_iovec(f, f->buf + f->buf_index, 1, false);
413     f->buf_index++;
414     if (f->buf_index == IO_BUF_SIZE) {
415         qemu_fflush(f);
416     }
417 }
418 
419 void qemu_file_skip(QEMUFile *f, int size)
420 {
421     if (f->buf_index + size <= f->buf_size) {
422         f->buf_index += size;
423     }
424 }
425 
426 /*
427  * Read 'size' bytes from file (at 'offset') without moving the
428  * pointer and set 'buf' to point to that data.
429  *
430  * It will return size bytes unless there was an error, in which case it will
431  * return as many as it managed to read (assuming blocking fd's which
432  * all current QEMUFile are)
433  */
434 size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
435 {
436     ssize_t pending;
437     size_t index;
438 
439     assert(!qemu_file_is_writable(f));
440     assert(offset < IO_BUF_SIZE);
441     assert(size <= IO_BUF_SIZE - offset);
442 
443     /* The 1st byte to read from */
444     index = f->buf_index + offset;
445     /* The number of available bytes starting at index */
446     pending = f->buf_size - index;
447 
448     /*
449      * qemu_fill_buffer might return just a few bytes, even when there isn't
450      * an error, so loop collecting them until we get enough.
451      */
452     while (pending < size) {
453         int received = qemu_fill_buffer(f);
454 
455         if (received <= 0) {
456             break;
457         }
458 
459         index = f->buf_index + offset;
460         pending = f->buf_size - index;
461     }
462 
463     if (pending <= 0) {
464         return 0;
465     }
466     if (size > pending) {
467         size = pending;
468     }
469 
470     *buf = f->buf + index;
471     return size;
472 }
473 
474 /*
475  * Read 'size' bytes of data from the file into buf.
476  * 'size' can be larger than the internal buffer.
477  *
478  * It will return size bytes unless there was an error, in which case it will
479  * return as many as it managed to read (assuming blocking fd's which
480  * all current QEMUFile are)
481  */
482 size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
483 {
484     size_t pending = size;
485     size_t done = 0;
486 
487     while (pending > 0) {
488         size_t res;
489         uint8_t *src;
490 
491         res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
492         if (res == 0) {
493             return done;
494         }
495         memcpy(buf, src, res);
496         qemu_file_skip(f, res);
497         buf += res;
498         pending -= res;
499         done += res;
500     }
501     return done;
502 }
503 
504 /*
505  * Read 'size' bytes of data from the file.
506  * 'size' can be larger than the internal buffer.
507  *
508  * The data:
509  *   may be held on an internal buffer (in which case *buf is updated
510  *     to point to it) that is valid until the next qemu_file operation.
511  * OR
512  *   will be copied to the *buf that was passed in.
513  *
514  * The code tries to avoid the copy if possible.
515  *
516  * It will return size bytes unless there was an error, in which case it will
517  * return as many as it managed to read (assuming blocking fd's which
518  * all current QEMUFile are)
519  *
520  * Note: Since **buf may get changed, the caller should take care to
521  *       keep a pointer to the original buffer if it needs to deallocate it.
522  */
523 size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
524 {
525     if (size < IO_BUF_SIZE) {
526         size_t res;
527         uint8_t *src;
528 
529         res = qemu_peek_buffer(f, &src, size, 0);
530 
531         if (res == size) {
532             qemu_file_skip(f, res);
533             *buf = src;
534             return res;
535         }
536     }
537 
538     return qemu_get_buffer(f, *buf, size);
539 }
540 
541 /*
542  * Peeks a single byte from the buffer; this isn't guaranteed to work if
543  * offset leaves a gap after the previous read/peeked data.
544  */
545 int qemu_peek_byte(QEMUFile *f, int offset)
546 {
547     int index = f->buf_index + offset;
548 
549     assert(!qemu_file_is_writable(f));
550     assert(offset < IO_BUF_SIZE);
551 
552     if (index >= f->buf_size) {
553         qemu_fill_buffer(f);
554         index = f->buf_index + offset;
555         if (index >= f->buf_size) {
556             return 0;
557         }
558     }
559     return f->buf[index];
560 }
561 
562 int qemu_get_byte(QEMUFile *f)
563 {
564     int result;
565 
566     result = qemu_peek_byte(f, 0);
567     qemu_file_skip(f, 1);
568     return result;
569 }
570 
571 int64_t qemu_ftell_fast(QEMUFile *f)
572 {
573     int64_t ret = f->pos;
574     int i;
575 
576     for (i = 0; i < f->iovcnt; i++) {
577         ret += f->iov[i].iov_len;
578     }
579 
580     return ret;
581 }
582 
583 int64_t qemu_ftell(QEMUFile *f)
584 {
585     qemu_fflush(f);
586     return f->pos;
587 }
588 
589 int qemu_file_rate_limit(QEMUFile *f)
590 {
591     if (qemu_file_get_error(f)) {
592         return 1;
593     }
594     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
595         return 1;
596     }
597     return 0;
598 }
599 
600 int64_t qemu_file_get_rate_limit(QEMUFile *f)
601 {
602     return f->xfer_limit;
603 }
604 
605 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
606 {
607     f->xfer_limit = limit;
608 }
609 
610 void qemu_file_reset_rate_limit(QEMUFile *f)
611 {
612     f->bytes_xfer = 0;
613 }
614 
615 void qemu_put_be16(QEMUFile *f, unsigned int v)
616 {
617     qemu_put_byte(f, v >> 8);
618     qemu_put_byte(f, v);
619 }
620 
621 void qemu_put_be32(QEMUFile *f, unsigned int v)
622 {
623     qemu_put_byte(f, v >> 24);
624     qemu_put_byte(f, v >> 16);
625     qemu_put_byte(f, v >> 8);
626     qemu_put_byte(f, v);
627 }
628 
629 void qemu_put_be64(QEMUFile *f, uint64_t v)
630 {
631     qemu_put_be32(f, v >> 32);
632     qemu_put_be32(f, v);
633 }
634 
635 unsigned int qemu_get_be16(QEMUFile *f)
636 {
637     unsigned int v;
638     v = qemu_get_byte(f) << 8;
639     v |= qemu_get_byte(f);
640     return v;
641 }
642 
643 unsigned int qemu_get_be32(QEMUFile *f)
644 {
645     unsigned int v;
646     v = (unsigned int)qemu_get_byte(f) << 24;
647     v |= qemu_get_byte(f) << 16;
648     v |= qemu_get_byte(f) << 8;
649     v |= qemu_get_byte(f);
650     return v;
651 }
652 
653 uint64_t qemu_get_be64(QEMUFile *f)
654 {
655     uint64_t v;
656     v = (uint64_t)qemu_get_be32(f) << 32;
657     v |= qemu_get_be32(f);
658     return v;
659 }
660 
661 /* Compress size bytes of data start at p with specific compression
662  * level and store the compressed data to the buffer of f.
663  *
664  * When f is not writable, return -1 if f has no space to save the
665  * compressed data.
666  * When f is wirtable and it has no space to save the compressed data,
667  * do fflush first, if f still has no space to save the compressed
668  * data, return -1.
669  */
670 
671 ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
672                                   int level)
673 {
674     ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
675 
676     if (blen < compressBound(size)) {
677         if (!qemu_file_is_writable(f)) {
678             return -1;
679         }
680         qemu_fflush(f);
681         blen = IO_BUF_SIZE - sizeof(int32_t);
682         if (blen < compressBound(size)) {
683             return -1;
684         }
685     }
686     if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
687                   (Bytef *)p, size, level) != Z_OK) {
688         error_report("Compress Failed!");
689         return 0;
690     }
691     qemu_put_be32(f, blen);
692     if (f->ops->writev_buffer) {
693         add_to_iovec(f, f->buf + f->buf_index, blen, false);
694     }
695     f->buf_index += blen;
696     if (f->buf_index == IO_BUF_SIZE) {
697         qemu_fflush(f);
698     }
699     return blen + sizeof(int32_t);
700 }
701 
702 /* Put the data in the buffer of f_src to the buffer of f_des, and
703  * then reset the buf_index of f_src to 0.
704  */
705 
706 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
707 {
708     int len = 0;
709 
710     if (f_src->buf_index > 0) {
711         len = f_src->buf_index;
712         qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
713         f_src->buf_index = 0;
714         f_src->iovcnt = 0;
715     }
716     return len;
717 }
718 
719 /*
720  * Get a string whose length is determined by a single preceding byte
721  * A preallocated 256 byte buffer must be passed in.
722  * Returns: len on success and a 0 terminated string in the buffer
723  *          else 0
724  *          (Note a 0 length string will return 0 either way)
725  */
726 size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
727 {
728     size_t len = qemu_get_byte(f);
729     size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
730 
731     buf[res] = 0;
732 
733     return res == len ? res : 0;
734 }
735 
736 /*
737  * Set the blocking state of the QEMUFile.
738  * Note: On some transports the OS only keeps a single blocking state for
739  *       both directions, and thus changing the blocking on the main
740  *       QEMUFile can also affect the return path.
741  */
742 void qemu_file_set_blocking(QEMUFile *f, bool block)
743 {
744     if (f->ops->set_blocking) {
745         f->ops->set_blocking(f->opaque, block);
746     }
747 }
748