2 * Multifd common functions
4 * Copyright (c) 2019-2020 Red Hat Inc
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"
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)
56 /* maximum number of allocated pages */
59 uint32_t normal_pages
;
60 /* size of the next packet that contains pages */
61 uint32_t next_packet_size
;
65 uint32_t unused32
[1]; /* Reserved for future use */
66 uint64_t unused64
[3]; /* Reserved for future use */
69 * This array contains the pointers to:
70 * - normal pages (initial normal_pages entries)
71 * - zero pages (following zero_pages entries)
74 } __attribute__((packed
)) MultiFDPacket_t
;
77 /* number of used pages */
79 /* number of normal pages */
82 /* offset of each page */
86 struct MultiFDRecvData
{
98 typedef union MultiFDPayload
{
102 struct MultiFDSendData
{
103 MultiFDPayloadType type
;
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
)
119 /* Fields are only written at creating/deletion time */
120 /* No lock required for them, they are read only */
124 /* channel thread name */
126 /* channel thread id */
129 QemuThread tls_thread
;
130 bool tls_thread_created
;
131 /* communication channel */
133 /* packet allocated len */
135 /* multifd flags for sending ram */
138 /* sem where to wait for more work */
140 /* syncs main thread and channels */
141 QemuSemaphore sem_sync
;
143 /* multifd flags for each packet */
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.
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 */
168 /* number of iovs used */
170 /* used for compression methods */
175 /* Fields are only written at creating/deletion time */
176 /* No lock required for them, they are read only */
180 /* channel thread name */
182 /* channel thread id */
185 /* communication channel */
187 /* packet allocated len */
190 /* syncs main thread and channels */
191 QemuSemaphore sem_sync
;
192 /* sem where to wait for more work */
195 /* this mutex protects the following parameters */
197 /* should this thread finish */
199 /* multifd flags for each packet */
201 /* global number of generated multifd packets */
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
;
216 /* ramblock host address */
218 /* buffers to recv */
220 /* Pages that are not zero */
222 /* num of non zero pages */
224 /* Pages that are zero */
226 /* num of zero pages */
228 /* used for de-compression methods */
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
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
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
269 * Must indicate whether this is a compression packet by setting
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
306 * Must read the data from the QIOChannel p->c.
308 int (*recv
)(MultiFDRecvParams
*p
, Error
**errp
);
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
;
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
);