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
18 typedef struct MultiFDRecvData MultiFDRecvData
;
20 bool multifd_send_setup(void);
21 void multifd_send_shutdown(void);
22 void multifd_send_channel_created(void);
23 int multifd_recv_setup(Error
**errp
);
24 void multifd_recv_cleanup(void);
25 void multifd_recv_shutdown(void);
26 bool multifd_recv_all_channels_created(void);
27 void multifd_recv_new_channel(QIOChannel
*ioc
, Error
**errp
);
28 void multifd_recv_sync_main(void);
29 int multifd_send_sync_main(void);
30 bool multifd_queue_page(RAMBlock
*block
, ram_addr_t offset
);
31 bool multifd_recv(void);
32 MultiFDRecvData
*multifd_get_recv_data(void);
34 /* Multifd Compression flags */
35 #define MULTIFD_FLAG_SYNC (1 << 0)
37 /* We reserve 4 bits for compression methods */
38 #define MULTIFD_FLAG_COMPRESSION_MASK (0xf << 1)
39 /* we need to be compatible. Before compression value was 0 */
40 #define MULTIFD_FLAG_NOCOMP (0 << 1)
41 #define MULTIFD_FLAG_ZLIB (1 << 1)
42 #define MULTIFD_FLAG_ZSTD (2 << 1)
43 #define MULTIFD_FLAG_QPL (4 << 1)
44 #define MULTIFD_FLAG_UADK (8 << 1)
46 /* This value needs to be a multiple of qemu_target_page_size() */
47 #define MULTIFD_PACKET_SIZE (512 * 1024)
53 /* maximum number of allocated pages */
56 uint32_t normal_pages
;
57 /* size of the next packet that contains pages */
58 uint32_t next_packet_size
;
62 uint32_t unused32
[1]; /* Reserved for future use */
63 uint64_t unused64
[3]; /* Reserved for future use */
66 * This array contains the pointers to:
67 * - normal pages (initial normal_pages entries)
68 * - zero pages (following zero_pages entries)
71 } __attribute__((packed
)) MultiFDPacket_t
;
74 /* number of used pages */
76 /* number of normal pages */
78 /* number of allocated pages */
80 /* offset of each page */
85 struct MultiFDRecvData
{
93 /* Fields are only written at creating/deletion time */
94 /* No lock required for them, they are read only */
98 /* channel thread name */
100 /* channel thread id */
103 QemuThread tls_thread
;
104 bool tls_thread_created
;
105 /* communication channel */
107 /* packet allocated len */
109 /* guest page size */
111 /* number of pages in a full packet */
113 /* multifd flags for sending ram */
116 /* sem where to wait for more work */
118 /* syncs main thread and channels */
119 QemuSemaphore sem_sync
;
121 /* multifd flags for each packet */
124 * The sender thread has work to do if either of below boolean is set.
126 * @pending_job: a job is pending
127 * @pending_sync: a sync request is pending
129 * For both of these fields, they're only set by the requesters, and
130 * cleared by the multifd sender threads.
134 /* array of pages to sent.
135 * The owner of 'pages' depends of 'pending_job' value:
136 * pending_job == 0 -> migration_thread can use it.
137 * pending_job != 0 -> multifd_channel can use it.
139 MultiFDPages_t
*pages
;
141 /* thread local variables. No locking required */
143 /* pointer to the packet */
144 MultiFDPacket_t
*packet
;
145 /* size of the next packet that contains pages */
146 uint32_t next_packet_size
;
147 /* packets sent through this channel */
148 uint64_t packets_sent
;
149 /* non zero pages sent through this channel */
150 uint64_t total_normal_pages
;
151 /* zero pages sent through this channel */
152 uint64_t total_zero_pages
;
153 /* buffers to send */
155 /* number of iovs used */
157 /* used for compression methods */
162 /* Fields are only written at creating/deletion time */
163 /* No lock required for them, they are read only */
167 /* channel thread name */
169 /* channel thread id */
172 /* communication channel */
174 /* packet allocated len */
176 /* guest page size */
178 /* number of pages in a full packet */
181 /* syncs main thread and channels */
182 QemuSemaphore sem_sync
;
183 /* sem where to wait for more work */
186 /* this mutex protects the following parameters */
188 /* should this thread finish */
190 /* multifd flags for each packet */
192 /* global number of generated multifd packets */
195 MultiFDRecvData
*data
;
197 /* thread local variables. No locking required */
199 /* pointer to the packet */
200 MultiFDPacket_t
*packet
;
201 /* size of the next packet that contains pages */
202 uint32_t next_packet_size
;
203 /* packets received through this channel */
204 uint64_t packets_recved
;
207 /* ramblock host address */
209 /* non zero pages recv through this channel */
210 uint64_t total_normal_pages
;
211 /* zero pages recv through this channel */
212 uint64_t total_zero_pages
;
213 /* buffers to recv */
215 /* Pages that are not zero */
217 /* num of non zero pages */
219 /* Pages that are zero */
221 /* num of zero pages */
223 /* used for de-compression methods */
228 /* Setup for sending side */
229 int (*send_setup
)(MultiFDSendParams
*p
, Error
**errp
);
230 /* Cleanup for sending side */
231 void (*send_cleanup
)(MultiFDSendParams
*p
, Error
**errp
);
232 /* Prepare the send packet */
233 int (*send_prepare
)(MultiFDSendParams
*p
, Error
**errp
);
234 /* Setup for receiving side */
235 int (*recv_setup
)(MultiFDRecvParams
*p
, Error
**errp
);
236 /* Cleanup for receiving side */
237 void (*recv_cleanup
)(MultiFDRecvParams
*p
);
239 int (*recv
)(MultiFDRecvParams
*p
, Error
**errp
);
242 void multifd_register_ops(int method
, MultiFDMethods
*ops
);
243 void multifd_send_fill_packet(MultiFDSendParams
*p
);
244 bool multifd_send_prepare_common(MultiFDSendParams
*p
);
245 void multifd_send_zero_page_detect(MultiFDSendParams
*p
);
246 void multifd_recv_zero_page_process(MultiFDRecvParams
*p
);
248 static inline void multifd_send_prepare_header(MultiFDSendParams
*p
)
250 p
->iov
[0].iov_len
= p
->packet_len
;
251 p
->iov
[0].iov_base
= p
->packet
;
255 void multifd_channel_connect(MultiFDSendParams
*p
, QIOChannel
*ioc
);