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