4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #ifndef MIGRATION_QEMU_FILE_H
26 #define MIGRATION_QEMU_FILE_H
29 #include "exec/cpu-common.h"
30 #include "io/channel.h"
33 * This function provides hooks around different
34 * stages of RAM migration.
35 * 'data' is call specific data associated with the 'flags' value
37 typedef int (QEMURamHookFunc)(QEMUFile *f, uint64_t flags, void *data);
40 * Constants used by ram_control_* hooks
42 #define RAM_CONTROL_SETUP 0
43 #define RAM_CONTROL_ROUND 1
44 #define RAM_CONTROL_HOOK 2
45 #define RAM_CONTROL_FINISH 3
46 #define RAM_CONTROL_BLOCK_REG 4
49 * This function allows override of where the RAM page
50 * is saved (such as RDMA, for example.)
52 typedef size_t (QEMURamSaveFunc)(QEMUFile *f,
53 ram_addr_t block_offset,
56 uint64_t *bytes_sent);
58 typedef struct QEMUFileHooks {
59 QEMURamHookFunc *before_ram_iterate;
60 QEMURamHookFunc *after_ram_iterate;
61 QEMURamHookFunc *hook_ram_load;
62 QEMURamSaveFunc *save_page;
65 QEMUFile *qemu_file_new_input(QIOChannel *ioc);
66 QEMUFile *qemu_file_new_output(QIOChannel *ioc);
67 void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks);
68 int qemu_fclose(QEMUFile *f);
71 * qemu_file_transferred:
73 * Report the total number of bytes transferred with
76 * For writable files, any pending buffers will be
77 * flushed, so the reported value will be equal to
78 * the number of bytes transferred on the wire.
80 * For readable files, the reported value will be
81 * equal to the number of bytes transferred on the
84 * Returns: the total bytes transferred
86 uint64_t qemu_file_transferred(QEMUFile *f);
89 * qemu_file_transferred_fast:
91 * As qemu_file_transferred except for writable
92 * files, where no flush is performed and the reported
93 * amount will include the size of any queued buffers,
94 * on top of the amount actually transferred.
96 * Returns: the total bytes transferred and queued
98 uint64_t qemu_file_transferred_fast(QEMUFile *f);
101 * put_buffer without copying the buffer.
102 * The buffer should be available till it is sent asynchronously.
104 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
106 bool qemu_file_mode_is_not_valid(const char *mode);
107 bool qemu_file_is_writable(QEMUFile *f);
109 #include "migration/qemu-file-types.h"
111 size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset);
112 size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size);
113 ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
114 const uint8_t *p, size_t size);
115 int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src);
116 bool qemu_file_buffer_empty(QEMUFile *file);
119 * Note that you can only peek continuous bytes from where the current pointer
120 * is; you aren't guaranteed to be able to peak to +n bytes unless you've
121 * previously peeked +n-1.
123 int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset);
124 void qemu_file_skip(QEMUFile *f, int size);
126 * qemu_file_credit_transfer:
128 * Report on a number of bytes that have been transferred
129 * out of band from the main file object I/O methods. This
130 * accounting information tracks the total migration traffic.
132 void qemu_file_credit_transfer(QEMUFile *f, size_t size);
133 int qemu_file_get_error_obj(QEMUFile *f, Error **errp);
134 int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp);
135 void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err);
136 void qemu_file_set_error(QEMUFile *f, int ret);
137 int qemu_file_shutdown(QEMUFile *f);
138 QEMUFile *qemu_file_get_return_path(QEMUFile *f);
139 void qemu_fflush(QEMUFile *f);
140 void qemu_file_set_blocking(QEMUFile *f, bool block);
141 int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size);
143 void ram_control_before_iterate(QEMUFile *f, uint64_t flags);
144 void ram_control_after_iterate(QEMUFile *f, uint64_t flags);
145 void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data);
147 /* Whenever this is found in the data stream, the flags
148 * will be passed to ram_control_load_hook in the incoming-migration
149 * side. This lets before_ram_iterate/after_ram_iterate add
150 * transport-specific sections to the RAM migration data.
152 #define RAM_SAVE_FLAG_HOOK 0x80
154 #define RAM_SAVE_CONTROL_NOT_SUPP -1000
155 #define RAM_SAVE_CONTROL_DELAYED -2000
157 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
158 ram_addr_t offset, size_t size,
159 uint64_t *bytes_sent);
160 QIOChannel *qemu_file_get_ioc(QEMUFile *file);