Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / migration / multifd.h
blob50d58c0c9cecd1beb5257eb94001d6cffa8d0e1e
1 /*
2 * Multifd common functions
4 * Copyright (c) 2019-2020 Red Hat Inc
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #ifndef QEMU_MIGRATION_MULTIFD_H
14 #define QEMU_MIGRATION_MULTIFD_H
16 #include "exec/target_page.h"
17 #include "ram.h"
19 typedef struct MultiFDRecvData MultiFDRecvData;
20 typedef struct MultiFDSendData MultiFDSendData;
22 bool multifd_send_setup(void);
23 void multifd_send_shutdown(void);
24 void multifd_send_channel_created(void);
25 int multifd_recv_setup(Error **errp);
26 void multifd_recv_cleanup(void);
27 void multifd_recv_shutdown(void);
28 bool multifd_recv_all_channels_created(void);
29 void multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
30 void multifd_recv_sync_main(void);
31 int multifd_send_sync_main(void);
32 bool multifd_queue_page(RAMBlock *block, ram_addr_t offset);
33 bool multifd_recv(void);
34 MultiFDRecvData *multifd_get_recv_data(void);
36 /* Multifd Compression flags */
37 #define MULTIFD_FLAG_SYNC (1 << 0)
39 /* We reserve 5 bits for compression methods */
40 #define MULTIFD_FLAG_COMPRESSION_MASK (0x1f << 1)
41 /* we need to be compatible. Before compression value was 0 */
42 #define MULTIFD_FLAG_NOCOMP (0 << 1)
43 #define MULTIFD_FLAG_ZLIB (1 << 1)
44 #define MULTIFD_FLAG_ZSTD (2 << 1)
45 #define MULTIFD_FLAG_QPL (4 << 1)
46 #define MULTIFD_FLAG_UADK (8 << 1)
47 #define MULTIFD_FLAG_QATZIP (16 << 1)
49 /* This value needs to be a multiple of qemu_target_page_size() */
50 #define MULTIFD_PACKET_SIZE (512 * 1024)
52 typedef struct {
53 uint32_t magic;
54 uint32_t version;
55 uint32_t flags;
56 /* maximum number of allocated pages */
57 uint32_t pages_alloc;
58 /* non zero pages */
59 uint32_t normal_pages;
60 /* size of the next packet that contains pages */
61 uint32_t next_packet_size;
62 uint64_t packet_num;
63 /* zero pages */
64 uint32_t zero_pages;
65 uint32_t unused32[1]; /* Reserved for future use */
66 uint64_t unused64[3]; /* Reserved for future use */
67 char ramblock[256];
69 * This array contains the pointers to:
70 * - normal pages (initial normal_pages entries)
71 * - zero pages (following zero_pages entries)
73 uint64_t offset[];
74 } __attribute__((packed)) MultiFDPacket_t;
76 typedef struct {
77 /* number of used pages */
78 uint32_t num;
79 /* number of normal pages */
80 uint32_t normal_num;
81 RAMBlock *block;
82 /* offset of each page */
83 ram_addr_t offset[];
84 } MultiFDPages_t;
86 struct MultiFDRecvData {
87 void *opaque;
88 size_t size;
89 /* for preadv */
90 off_t file_offset;
93 typedef enum {
94 MULTIFD_PAYLOAD_NONE,
95 MULTIFD_PAYLOAD_RAM,
96 } MultiFDPayloadType;
98 typedef union MultiFDPayload {
99 MultiFDPages_t ram;
100 } MultiFDPayload;
102 struct MultiFDSendData {
103 MultiFDPayloadType type;
104 MultiFDPayload u;
107 static inline bool multifd_payload_empty(MultiFDSendData *data)
109 return data->type == MULTIFD_PAYLOAD_NONE;
112 static inline void multifd_set_payload_type(MultiFDSendData *data,
113 MultiFDPayloadType type)
115 data->type = type;
118 typedef struct {
119 /* Fields are only written at creating/deletion time */
120 /* No lock required for them, they are read only */
122 /* channel number */
123 uint8_t id;
124 /* channel thread name */
125 char *name;
126 /* channel thread id */
127 QemuThread thread;
128 bool thread_created;
129 QemuThread tls_thread;
130 bool tls_thread_created;
131 /* communication channel */
132 QIOChannel *c;
133 /* packet allocated len */
134 uint32_t packet_len;
135 /* multifd flags for sending ram */
136 int write_flags;
138 /* sem where to wait for more work */
139 QemuSemaphore sem;
140 /* syncs main thread and channels */
141 QemuSemaphore sem_sync;
143 /* multifd flags for each packet */
144 uint32_t flags;
146 * The sender thread has work to do if either of below boolean is set.
148 * @pending_job: a job is pending
149 * @pending_sync: a sync request is pending
151 * For both of these fields, they're only set by the requesters, and
152 * cleared by the multifd sender threads.
154 bool pending_job;
155 bool pending_sync;
156 MultiFDSendData *data;
158 /* thread local variables. No locking required */
160 /* pointer to the packet */
161 MultiFDPacket_t *packet;
162 /* size of the next packet that contains pages */
163 uint32_t next_packet_size;
164 /* packets sent through this channel */
165 uint64_t packets_sent;
166 /* buffers to send */
167 struct iovec *iov;
168 /* number of iovs used */
169 uint32_t iovs_num;
170 /* used for compression methods */
171 void *compress_data;
172 } MultiFDSendParams;
174 typedef struct {
175 /* Fields are only written at creating/deletion time */
176 /* No lock required for them, they are read only */
178 /* channel number */
179 uint8_t id;
180 /* channel thread name */
181 char *name;
182 /* channel thread id */
183 QemuThread thread;
184 bool thread_created;
185 /* communication channel */
186 QIOChannel *c;
187 /* packet allocated len */
188 uint32_t packet_len;
190 /* syncs main thread and channels */
191 QemuSemaphore sem_sync;
192 /* sem where to wait for more work */
193 QemuSemaphore sem;
195 /* this mutex protects the following parameters */
196 QemuMutex mutex;
197 /* should this thread finish */
198 bool quit;
199 /* multifd flags for each packet */
200 uint32_t flags;
201 /* global number of generated multifd packets */
202 uint64_t packet_num;
203 int pending_job;
204 MultiFDRecvData *data;
206 /* thread local variables. No locking required */
208 /* pointer to the packet */
209 MultiFDPacket_t *packet;
210 /* size of the next packet that contains pages */
211 uint32_t next_packet_size;
212 /* packets received through this channel */
213 uint64_t packets_recved;
214 /* ramblock */
215 RAMBlock *block;
216 /* ramblock host address */
217 uint8_t *host;
218 /* buffers to recv */
219 struct iovec *iov;
220 /* Pages that are not zero */
221 ram_addr_t *normal;
222 /* num of non zero pages */
223 uint32_t normal_num;
224 /* Pages that are zero */
225 ram_addr_t *zero;
226 /* num of zero pages */
227 uint32_t zero_num;
228 /* used for de-compression methods */
229 void *compress_data;
230 } MultiFDRecvParams;
232 typedef struct {
234 * The send_setup, send_cleanup, send_prepare are only called on
235 * the QEMU instance at the migration source.
239 * Setup for sending side. Called once per channel during channel
240 * setup phase.
242 * Must allocate p->iov. If packets are in use (default), one
243 * extra iovec must be allocated for the packet header. Any memory
244 * allocated in this hook must be released at send_cleanup.
246 * p->write_flags may be used for passing flags to the QIOChannel.
248 * p->compression_data may be used by compression methods to store
249 * compression data.
251 int (*send_setup)(MultiFDSendParams *p, Error **errp);
254 * Cleanup for sending side. Called once per channel during
255 * channel cleanup phase.
257 void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
260 * Prepare the send packet. Called as a result of multifd_send()
261 * on the client side, with p pointing to the MultiFDSendParams of
262 * a channel that is currently idle.
264 * Must populate p->iov with the data to be sent, increment
265 * p->iovs_num to match the amount of iovecs used and set
266 * p->next_packet_size with the amount of data currently present
267 * in p->iov.
269 * Must indicate whether this is a compression packet by setting
270 * p->flags.
272 * As a last step, if packets are in use (default), must prepare
273 * the packet by calling multifd_send_fill_packet().
275 int (*send_prepare)(MultiFDSendParams *p, Error **errp);
278 * The recv_setup, recv_cleanup, recv are only called on the QEMU
279 * instance at the migration destination.
283 * Setup for receiving side. Called once per channel during
284 * channel setup phase. May be empty.
286 * May allocate data structures for the receiving of data. May use
287 * p->iov. Compression methods may use p->compress_data.
289 int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
292 * Cleanup for receiving side. Called once per channel during
293 * channel cleanup phase. May be empty.
295 void (*recv_cleanup)(MultiFDRecvParams *p);
298 * Data receive method. Called as a result of multifd_recv() on
299 * the client side, with p pointing to the MultiFDRecvParams of a
300 * channel that is currently idle. Only called if there is data
301 * available to receive.
303 * Must validate p->flags according to what was set at
304 * send_prepare.
306 * Must read the data from the QIOChannel p->c.
308 int (*recv)(MultiFDRecvParams *p, Error **errp);
309 } MultiFDMethods;
311 void multifd_register_ops(int method, const MultiFDMethods *ops);
312 void multifd_send_fill_packet(MultiFDSendParams *p);
313 bool multifd_send_prepare_common(MultiFDSendParams *p);
314 void multifd_send_zero_page_detect(MultiFDSendParams *p);
315 void multifd_recv_zero_page_process(MultiFDRecvParams *p);
317 static inline void multifd_send_prepare_header(MultiFDSendParams *p)
319 p->iov[0].iov_len = p->packet_len;
320 p->iov[0].iov_base = p->packet;
321 p->iovs_num++;
324 void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc);
325 bool multifd_send(MultiFDSendData **send_data);
326 MultiFDSendData *multifd_send_data_alloc(void);
328 static inline uint32_t multifd_ram_page_size(void)
330 return qemu_target_page_size();
333 static inline uint32_t multifd_ram_page_count(void)
335 return MULTIFD_PACKET_SIZE / qemu_target_page_size();
338 void multifd_ram_save_setup(void);
339 void multifd_ram_save_cleanup(void);
340 int multifd_ram_flush_and_sync(void);
341 size_t multifd_ram_payload_size(void);
342 void multifd_ram_fill_packet(MultiFDSendParams *p);
343 int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp);
344 #endif