1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include <zlib.h>
26#include "qemu/madvise.h"
27#include "qemu/error-report.h"
28#include "qemu/iov.h"
29#include "migration.h"
30#include "migration-stats.h"
31#include "qemu-file.h"
32#include "trace.h"
33#include "options.h"
34#include "qapi/error.h"
35
36#define IO_BUF_SIZE 32768
37#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
38
39struct QEMUFile {
40 const QEMUFileHooks *hooks;
41 QIOChannel *ioc;
42 bool is_writable;
43
44
45 uint64_t total_transferred;
46
47 int buf_index;
48 int buf_size;
49 uint8_t buf[IO_BUF_SIZE];
50
51 DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
52 struct iovec iov[MAX_IOV_SIZE];
53 unsigned int iovcnt;
54
55 int last_error;
56 Error *last_error_obj;
57};
58
59
60
61
62
63
64
65
66int qemu_file_shutdown(QEMUFile *f)
67{
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 if (!f->last_error) {
88 qemu_file_set_error(f, -EIO);
89 }
90
91 if (!qio_channel_has_feature(f->ioc,
92 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
93 return -ENOSYS;
94 }
95
96 if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) {
97 return -EIO;
98 }
99
100 return 0;
101}
102
103static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
104{
105 QEMUFile *f;
106
107 f = g_new0(QEMUFile, 1);
108
109 object_ref(ioc);
110 f->ioc = ioc;
111 f->is_writable = is_writable;
112
113 return f;
114}
115
116
117
118
119
120QEMUFile *qemu_file_get_return_path(QEMUFile *f)
121{
122 return qemu_file_new_impl(f->ioc, !f->is_writable);
123}
124
125QEMUFile *qemu_file_new_output(QIOChannel *ioc)
126{
127 return qemu_file_new_impl(ioc, true);
128}
129
130QEMUFile *qemu_file_new_input(QIOChannel *ioc)
131{
132 return qemu_file_new_impl(ioc, false);
133}
134
135void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
136{
137 f->hooks = hooks;
138}
139
140
141
142
143
144
145
146
147
148
149static int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
150{
151 if (errp) {
152 *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL;
153 }
154 return f->last_error;
155}
156
157
158
159
160
161
162
163
164
165int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp)
166{
167 int ret = 0;
168
169 if (f1) {
170 ret = qemu_file_get_error_obj(f1, errp);
171
172 if (ret) {
173 return ret;
174 }
175 }
176
177 if (f2) {
178 ret = qemu_file_get_error_obj(f2, errp);
179 }
180
181 return ret;
182}
183
184
185
186
187void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
188{
189 if (f->last_error == 0 && ret) {
190 f->last_error = ret;
191 error_propagate(&f->last_error_obj, err);
192 } else if (err) {
193 error_report_err(err);
194 }
195}
196
197
198
199
200
201
202
203
204int qemu_file_get_error(QEMUFile *f)
205{
206 return qemu_file_get_error_obj(f, NULL);
207}
208
209
210
211
212void qemu_file_set_error(QEMUFile *f, int ret)
213{
214 qemu_file_set_error_obj(f, ret, NULL);
215}
216
217static bool qemu_file_is_writable(QEMUFile *f)
218{
219 return f->is_writable;
220}
221
222static void qemu_iovec_release_ram(QEMUFile *f)
223{
224 struct iovec iov;
225 unsigned long idx;
226
227
228 idx = find_next_bit(f->may_free, f->iovcnt, 0);
229 if (idx >= f->iovcnt) {
230 return;
231 }
232 iov = f->iov[idx];
233
234
235
236
237
238 while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
239
240 if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
241 iov.iov_len += f->iov[idx].iov_len;
242 continue;
243 }
244 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
245 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
246 iov.iov_base, iov.iov_len, strerror(errno));
247 }
248 iov = f->iov[idx];
249 }
250 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
251 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
252 iov.iov_base, iov.iov_len, strerror(errno));
253 }
254 memset(f->may_free, 0, sizeof(f->may_free));
255}
256
257
258
259
260
261
262
263
264void qemu_fflush(QEMUFile *f)
265{
266 if (!qemu_file_is_writable(f)) {
267 return;
268 }
269
270 if (qemu_file_get_error(f)) {
271 return;
272 }
273 if (f->iovcnt > 0) {
274 Error *local_error = NULL;
275 if (qio_channel_writev_all(f->ioc,
276 f->iov, f->iovcnt,
277 &local_error) < 0) {
278 qemu_file_set_error_obj(f, -EIO, local_error);
279 } else {
280 uint64_t size = iov_size(f->iov, f->iovcnt);
281 f->total_transferred += size;
282 }
283
284 qemu_iovec_release_ram(f);
285 }
286
287 f->buf_index = 0;
288 f->iovcnt = 0;
289}
290
291void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
292{
293 int ret = 0;
294
295 if (f->hooks && f->hooks->before_ram_iterate) {
296 ret = f->hooks->before_ram_iterate(f, flags, NULL);
297 if (ret < 0) {
298 qemu_file_set_error(f, ret);
299 }
300 }
301}
302
303void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
304{
305 int ret = 0;
306
307 if (f->hooks && f->hooks->after_ram_iterate) {
308 ret = f->hooks->after_ram_iterate(f, flags, NULL);
309 if (ret < 0) {
310 qemu_file_set_error(f, ret);
311 }
312 }
313}
314
315void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
316{
317 if (f->hooks && f->hooks->hook_ram_load) {
318 int ret = f->hooks->hook_ram_load(f, flags, data);
319 if (ret < 0) {
320 qemu_file_set_error(f, ret);
321 }
322 }
323}
324
325size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
326 ram_addr_t offset, size_t size,
327 uint64_t *bytes_sent)
328{
329 if (f->hooks && f->hooks->save_page) {
330 int ret = f->hooks->save_page(f, block_offset,
331 offset, size, bytes_sent);
332
333 if (ret != RAM_SAVE_CONTROL_DELAYED &&
334 ret != RAM_SAVE_CONTROL_NOT_SUPP) {
335 if (bytes_sent && *bytes_sent > 0) {
336 qemu_file_credit_transfer(f, *bytes_sent);
337 } else if (ret < 0) {
338 qemu_file_set_error(f, ret);
339 }
340 }
341
342 return ret;
343 }
344
345 return RAM_SAVE_CONTROL_NOT_SUPP;
346}
347
348
349
350
351
352
353
354
355
356static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
357{
358 int len;
359 int pending;
360 Error *local_error = NULL;
361
362 assert(!qemu_file_is_writable(f));
363
364 pending = f->buf_size - f->buf_index;
365 if (pending > 0) {
366 memmove(f->buf, f->buf + f->buf_index, pending);
367 }
368 f->buf_index = 0;
369 f->buf_size = pending;
370
371 if (qemu_file_get_error(f)) {
372 return 0;
373 }
374
375 do {
376 len = qio_channel_read(f->ioc,
377 (char *)f->buf + pending,
378 IO_BUF_SIZE - pending,
379 &local_error);
380 if (len == QIO_CHANNEL_ERR_BLOCK) {
381 if (qemu_in_coroutine()) {
382 qio_channel_yield(f->ioc, G_IO_IN);
383 } else {
384 qio_channel_wait(f->ioc, G_IO_IN);
385 }
386 } else if (len < 0) {
387 len = -EIO;
388 }
389 } while (len == QIO_CHANNEL_ERR_BLOCK);
390
391 if (len > 0) {
392 f->buf_size += len;
393 f->total_transferred += len;
394 } else if (len == 0) {
395 qemu_file_set_error_obj(f, -EIO, local_error);
396 } else {
397 qemu_file_set_error_obj(f, len, local_error);
398 }
399
400 return len;
401}
402
403void qemu_file_credit_transfer(QEMUFile *f, size_t size)
404{
405 f->total_transferred += size;
406}
407
408
409
410
411
412
413
414
415
416int qemu_fclose(QEMUFile *f)
417{
418 int ret, ret2;
419 qemu_fflush(f);
420 ret = qemu_file_get_error(f);
421
422 ret2 = qio_channel_close(f->ioc, NULL);
423 if (ret >= 0) {
424 ret = ret2;
425 }
426 g_clear_pointer(&f->ioc, object_unref);
427
428
429
430
431 if (f->last_error) {
432 ret = f->last_error;
433 }
434 error_free(f->last_error_obj);
435 g_free(f);
436 trace_qemu_file_fclose();
437 return ret;
438}
439
440
441
442
443
444
445
446
447
448static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
449 bool may_free)
450{
451
452 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
453 f->iov[f->iovcnt - 1].iov_len &&
454 may_free == test_bit(f->iovcnt - 1, f->may_free))
455 {
456 f->iov[f->iovcnt - 1].iov_len += size;
457 } else {
458 if (f->iovcnt >= MAX_IOV_SIZE) {
459
460 assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
461 return 1;
462 }
463 if (may_free) {
464 set_bit(f->iovcnt, f->may_free);
465 }
466 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
467 f->iov[f->iovcnt++].iov_len = size;
468 }
469
470 if (f->iovcnt >= MAX_IOV_SIZE) {
471 qemu_fflush(f);
472 return 1;
473 }
474
475 return 0;
476}
477
478static void add_buf_to_iovec(QEMUFile *f, size_t len)
479{
480 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
481 f->buf_index += len;
482 if (f->buf_index == IO_BUF_SIZE) {
483 qemu_fflush(f);
484 }
485 }
486}
487
488void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
489 bool may_free)
490{
491 if (f->last_error) {
492 return;
493 }
494
495 add_to_iovec(f, buf, size, may_free);
496}
497
498void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
499{
500 size_t l;
501
502 if (f->last_error) {
503 return;
504 }
505
506 while (size > 0) {
507 l = IO_BUF_SIZE - f->buf_index;
508 if (l > size) {
509 l = size;
510 }
511 memcpy(f->buf + f->buf_index, buf, l);
512 add_buf_to_iovec(f, l);
513 if (qemu_file_get_error(f)) {
514 break;
515 }
516 buf += l;
517 size -= l;
518 }
519}
520
521void qemu_put_byte(QEMUFile *f, int v)
522{
523 if (f->last_error) {
524 return;
525 }
526
527 f->buf[f->buf_index] = v;
528 add_buf_to_iovec(f, 1);
529}
530
531void qemu_file_skip(QEMUFile *f, int size)
532{
533 if (f->buf_index + size <= f->buf_size) {
534 f->buf_index += size;
535 }
536}
537
538
539
540
541
542
543
544
545
546size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
547{
548 ssize_t pending;
549 size_t index;
550
551 assert(!qemu_file_is_writable(f));
552 assert(offset < IO_BUF_SIZE);
553 assert(size <= IO_BUF_SIZE - offset);
554
555
556 index = f->buf_index + offset;
557
558 pending = f->buf_size - index;
559
560
561
562
563
564 while (pending < size) {
565 int received = qemu_fill_buffer(f);
566
567 if (received <= 0) {
568 break;
569 }
570
571 index = f->buf_index + offset;
572 pending = f->buf_size - index;
573 }
574
575 if (pending <= 0) {
576 return 0;
577 }
578 if (size > pending) {
579 size = pending;
580 }
581
582 *buf = f->buf + index;
583 return size;
584}
585
586
587
588
589
590
591
592
593
594size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
595{
596 size_t pending = size;
597 size_t done = 0;
598
599 while (pending > 0) {
600 size_t res;
601 uint8_t *src;
602
603 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
604 if (res == 0) {
605 return done;
606 }
607 memcpy(buf, src, res);
608 qemu_file_skip(f, res);
609 buf += res;
610 pending -= res;
611 done += res;
612 }
613 return done;
614}
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
636{
637 if (size < IO_BUF_SIZE) {
638 size_t res;
639 uint8_t *src = NULL;
640
641 res = qemu_peek_buffer(f, &src, size, 0);
642
643 if (res == size) {
644 qemu_file_skip(f, res);
645 *buf = src;
646 return res;
647 }
648 }
649
650 return qemu_get_buffer(f, *buf, size);
651}
652
653
654
655
656
657int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset)
658{
659 int index = f->buf_index + offset;
660
661 assert(!qemu_file_is_writable(f));
662 assert(offset < IO_BUF_SIZE);
663
664 if (index >= f->buf_size) {
665 qemu_fill_buffer(f);
666 index = f->buf_index + offset;
667 if (index >= f->buf_size) {
668 return 0;
669 }
670 }
671 return f->buf[index];
672}
673
674int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
675{
676 int result;
677
678 result = qemu_peek_byte(f, 0);
679 qemu_file_skip(f, 1);
680 return result;
681}
682
683uint64_t qemu_file_transferred_noflush(QEMUFile *f)
684{
685 uint64_t ret = f->total_transferred;
686 int i;
687
688 for (i = 0; i < f->iovcnt; i++) {
689 ret += f->iov[i].iov_len;
690 }
691
692 return ret;
693}
694
695uint64_t qemu_file_transferred(QEMUFile *f)
696{
697 qemu_fflush(f);
698 return f->total_transferred;
699}
700
701void qemu_put_be16(QEMUFile *f, unsigned int v)
702{
703 qemu_put_byte(f, v >> 8);
704 qemu_put_byte(f, v);
705}
706
707void qemu_put_be32(QEMUFile *f, unsigned int v)
708{
709 qemu_put_byte(f, v >> 24);
710 qemu_put_byte(f, v >> 16);
711 qemu_put_byte(f, v >> 8);
712 qemu_put_byte(f, v);
713}
714
715void qemu_put_be64(QEMUFile *f, uint64_t v)
716{
717 qemu_put_be32(f, v >> 32);
718 qemu_put_be32(f, v);
719}
720
721unsigned int qemu_get_be16(QEMUFile *f)
722{
723 unsigned int v;
724 v = qemu_get_byte(f) << 8;
725 v |= qemu_get_byte(f);
726 return v;
727}
728
729unsigned int qemu_get_be32(QEMUFile *f)
730{
731 unsigned int v;
732 v = (unsigned int)qemu_get_byte(f) << 24;
733 v |= qemu_get_byte(f) << 16;
734 v |= qemu_get_byte(f) << 8;
735 v |= qemu_get_byte(f);
736 return v;
737}
738
739uint64_t qemu_get_be64(QEMUFile *f)
740{
741 uint64_t v;
742 v = (uint64_t)qemu_get_be32(f) << 32;
743 v |= qemu_get_be32(f);
744 return v;
745}
746
747
748static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
749 const uint8_t *source, size_t source_len)
750{
751 int err;
752
753 err = deflateReset(stream);
754 if (err != Z_OK) {
755 return -1;
756 }
757
758 stream->avail_in = source_len;
759 stream->next_in = (uint8_t *)source;
760 stream->avail_out = dest_len;
761 stream->next_out = dest;
762
763 err = deflate(stream, Z_FINISH);
764 if (err != Z_STREAM_END) {
765 return -1;
766 }
767
768 return stream->next_out - dest;
769}
770
771
772
773
774
775
776
777ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
778 const uint8_t *p, size_t size)
779{
780 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
781
782 if (blen < compressBound(size)) {
783 return -1;
784 }
785
786 blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
787 blen, p, size);
788 if (blen < 0) {
789 return -1;
790 }
791
792 qemu_put_be32(f, blen);
793 add_buf_to_iovec(f, blen);
794 return blen + sizeof(int32_t);
795}
796
797
798
799
800
801int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
802{
803 int len = 0;
804
805 if (f_src->buf_index > 0) {
806 len = f_src->buf_index;
807 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
808 f_src->buf_index = 0;
809 f_src->iovcnt = 0;
810 }
811 return len;
812}
813
814
815
816
817
818bool qemu_file_buffer_empty(QEMUFile *file)
819{
820 assert(qemu_file_is_writable(file));
821
822 return !file->iovcnt;
823}
824
825
826
827
828
829
830
831
832size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256])
833{
834 size_t len = qemu_get_byte(f);
835 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
836
837 buf[res] = 0;
838
839 return res == len ? res : 0;
840}
841
842
843
844
845
846void qemu_put_counted_string(QEMUFile *f, const char *str)
847{
848 size_t len = strlen(str);
849
850 assert(len < 256);
851 qemu_put_byte(f, len);
852 qemu_put_buffer(f, (const uint8_t *)str, len);
853}
854
855
856
857
858
859
860
861void qemu_file_set_blocking(QEMUFile *f, bool block)
862{
863 qio_channel_set_blocking(f->ioc, block, NULL);
864}
865
866
867
868
869
870
871
872
873
874QIOChannel *qemu_file_get_ioc(QEMUFile *file)
875{
876 return file->ioc;
877}
878
879
880
881
882int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size)
883{
884 while (size) {
885 size_t pending = f->buf_size - f->buf_index;
886 ssize_t rc;
887
888 if (!pending) {
889 rc = qemu_fill_buffer(f);
890 if (rc < 0) {
891 return rc;
892 }
893 if (rc == 0) {
894 return -EIO;
895 }
896 continue;
897 }
898
899 rc = write(fd, f->buf + f->buf_index, MIN(pending, size));
900 if (rc < 0) {
901 return -errno;
902 }
903 if (rc == 0) {
904 return -EIO;
905 }
906 f->buf_index += rc;
907 size -= rc;
908 }
909
910 return 0;
911}
912