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