2
0

multifd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Multifd common functions
  3. *
  4. * Copyright (c) 2019-2020 Red Hat Inc
  5. *
  6. * Authors:
  7. * Juan Quintela <quintela@redhat.com>
  8. *
  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.
  11. */
  12. #ifndef QEMU_MIGRATION_MULTIFD_H
  13. #define QEMU_MIGRATION_MULTIFD_H
  14. #include "exec/target_page.h"
  15. #include "ram.h"
  16. typedef struct MultiFDRecvData MultiFDRecvData;
  17. typedef struct MultiFDSendData MultiFDSendData;
  18. typedef enum {
  19. /* No sync request */
  20. MULTIFD_SYNC_NONE = 0,
  21. /* Sync locally on the sender threads without pushing messages */
  22. MULTIFD_SYNC_LOCAL,
  23. /*
  24. * Sync not only on the sender threads, but also push MULTIFD_FLAG_SYNC
  25. * message to the wire for each iochannel (which is for a remote sync).
  26. *
  27. * When remote sync is used, need to be paired with a follow up
  28. * RAM_SAVE_FLAG_EOS / RAM_SAVE_FLAG_MULTIFD_FLUSH message on the main
  29. * channel.
  30. */
  31. MULTIFD_SYNC_ALL,
  32. } MultiFDSyncReq;
  33. bool multifd_send_setup(void);
  34. void multifd_send_shutdown(void);
  35. void multifd_send_channel_created(void);
  36. int multifd_recv_setup(Error **errp);
  37. void multifd_recv_cleanup(void);
  38. void multifd_recv_shutdown(void);
  39. bool multifd_recv_all_channels_created(void);
  40. void multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
  41. void multifd_recv_sync_main(void);
  42. int multifd_send_sync_main(MultiFDSyncReq req);
  43. bool multifd_queue_page(RAMBlock *block, ram_addr_t offset);
  44. bool multifd_recv(void);
  45. MultiFDRecvData *multifd_get_recv_data(void);
  46. /* Multifd Compression flags */
  47. #define MULTIFD_FLAG_SYNC (1 << 0)
  48. /* We reserve 5 bits for compression methods */
  49. #define MULTIFD_FLAG_COMPRESSION_MASK (0x1f << 1)
  50. /* we need to be compatible. Before compression value was 0 */
  51. #define MULTIFD_FLAG_NOCOMP (0 << 1)
  52. #define MULTIFD_FLAG_ZLIB (1 << 1)
  53. #define MULTIFD_FLAG_ZSTD (2 << 1)
  54. #define MULTIFD_FLAG_QPL (4 << 1)
  55. #define MULTIFD_FLAG_UADK (8 << 1)
  56. #define MULTIFD_FLAG_QATZIP (16 << 1)
  57. /*
  58. * If set it means that this packet contains device state
  59. * (MultiFDPacketDeviceState_t), not RAM data (MultiFDPacket_t).
  60. */
  61. #define MULTIFD_FLAG_DEVICE_STATE (32 << 1)
  62. /* This value needs to be a multiple of qemu_target_page_size() */
  63. #define MULTIFD_PACKET_SIZE (512 * 1024)
  64. typedef struct {
  65. uint32_t magic;
  66. uint32_t version;
  67. uint32_t flags;
  68. } __attribute__((packed)) MultiFDPacketHdr_t;
  69. typedef struct {
  70. MultiFDPacketHdr_t hdr;
  71. /* maximum number of allocated pages */
  72. uint32_t pages_alloc;
  73. /* non zero pages */
  74. uint32_t normal_pages;
  75. /* size of the next packet that contains pages */
  76. uint32_t next_packet_size;
  77. uint64_t packet_num;
  78. /* zero pages */
  79. uint32_t zero_pages;
  80. uint32_t unused32[1]; /* Reserved for future use */
  81. uint64_t unused64[3]; /* Reserved for future use */
  82. char ramblock[256];
  83. /*
  84. * This array contains the pointers to:
  85. * - normal pages (initial normal_pages entries)
  86. * - zero pages (following zero_pages entries)
  87. */
  88. uint64_t offset[];
  89. } __attribute__((packed)) MultiFDPacket_t;
  90. typedef struct {
  91. MultiFDPacketHdr_t hdr;
  92. char idstr[256];
  93. uint32_t instance_id;
  94. /* size of the next packet that contains the actual data */
  95. uint32_t next_packet_size;
  96. } __attribute__((packed)) MultiFDPacketDeviceState_t;
  97. typedef struct {
  98. /* number of used pages */
  99. uint32_t num;
  100. /* number of normal pages */
  101. uint32_t normal_num;
  102. /*
  103. * Pointer to the ramblock. NOTE: it's caller's responsibility to make
  104. * sure the pointer is always valid!
  105. */
  106. RAMBlock *block;
  107. /* offset array of each page, managed by multifd */
  108. ram_addr_t *offset;
  109. } MultiFDPages_t;
  110. struct MultiFDRecvData {
  111. void *opaque;
  112. size_t size;
  113. /* for preadv */
  114. off_t file_offset;
  115. };
  116. typedef struct {
  117. char *idstr;
  118. uint32_t instance_id;
  119. char *buf;
  120. size_t buf_len;
  121. } MultiFDDeviceState_t;
  122. typedef enum {
  123. MULTIFD_PAYLOAD_NONE,
  124. MULTIFD_PAYLOAD_RAM,
  125. MULTIFD_PAYLOAD_DEVICE_STATE,
  126. } MultiFDPayloadType;
  127. typedef struct MultiFDPayload {
  128. MultiFDPages_t ram;
  129. MultiFDDeviceState_t device_state;
  130. } MultiFDPayload;
  131. struct MultiFDSendData {
  132. MultiFDPayloadType type;
  133. MultiFDPayload u;
  134. };
  135. static inline bool multifd_payload_empty(MultiFDSendData *data)
  136. {
  137. return data->type == MULTIFD_PAYLOAD_NONE;
  138. }
  139. static inline bool multifd_payload_device_state(MultiFDSendData *data)
  140. {
  141. return data->type == MULTIFD_PAYLOAD_DEVICE_STATE;
  142. }
  143. static inline void multifd_set_payload_type(MultiFDSendData *data,
  144. MultiFDPayloadType type)
  145. {
  146. assert(multifd_payload_empty(data));
  147. assert(type != MULTIFD_PAYLOAD_NONE);
  148. data->type = type;
  149. }
  150. typedef struct {
  151. /* Fields are only written at creating/deletion time */
  152. /* No lock required for them, they are read only */
  153. /* channel number */
  154. uint8_t id;
  155. /* channel thread name */
  156. char *name;
  157. /* channel thread id */
  158. QemuThread thread;
  159. bool thread_created;
  160. QemuThread tls_thread;
  161. bool tls_thread_created;
  162. /* communication channel */
  163. QIOChannel *c;
  164. /* packet allocated len */
  165. uint32_t packet_len;
  166. /* multifd flags for sending ram */
  167. int write_flags;
  168. /* sem where to wait for more work */
  169. QemuSemaphore sem;
  170. /* syncs main thread and channels */
  171. QemuSemaphore sem_sync;
  172. /* multifd flags for each packet */
  173. uint32_t flags;
  174. /*
  175. * The sender thread has work to do if either of below field is set.
  176. *
  177. * @pending_job: a job is pending
  178. * @pending_sync: a sync request is pending
  179. *
  180. * For both of these fields, they're only set by the requesters, and
  181. * cleared by the multifd sender threads.
  182. */
  183. bool pending_job;
  184. MultiFDSyncReq pending_sync;
  185. MultiFDSendData *data;
  186. /* thread local variables. No locking required */
  187. /* pointers to the possible packet types */
  188. MultiFDPacket_t *packet;
  189. MultiFDPacketDeviceState_t *packet_device_state;
  190. /* size of the next packet that contains pages */
  191. uint32_t next_packet_size;
  192. /* packets sent through this channel */
  193. uint64_t packets_sent;
  194. /* buffers to send */
  195. struct iovec *iov;
  196. /* number of iovs used */
  197. uint32_t iovs_num;
  198. /* used for compression methods */
  199. void *compress_data;
  200. } MultiFDSendParams;
  201. typedef struct {
  202. /* Fields are only written at creating/deletion time */
  203. /* No lock required for them, they are read only */
  204. /* channel number */
  205. uint8_t id;
  206. /* channel thread name */
  207. char *name;
  208. /* channel thread id */
  209. QemuThread thread;
  210. bool thread_created;
  211. /* communication channel */
  212. QIOChannel *c;
  213. /* packet allocated len */
  214. uint32_t packet_len;
  215. /* syncs main thread and channels */
  216. QemuSemaphore sem_sync;
  217. /* sem where to wait for more work */
  218. QemuSemaphore sem;
  219. /* this mutex protects the following parameters */
  220. QemuMutex mutex;
  221. /* should this thread finish */
  222. bool quit;
  223. /* multifd flags for each packet */
  224. uint32_t flags;
  225. /* global number of generated multifd packets */
  226. uint64_t packet_num;
  227. int pending_job;
  228. MultiFDRecvData *data;
  229. /* thread local variables. No locking required */
  230. /* pointers to the possible packet types */
  231. MultiFDPacket_t *packet;
  232. MultiFDPacketDeviceState_t *packet_dev_state;
  233. /* size of the next packet that contains pages */
  234. uint32_t next_packet_size;
  235. /* packets received through this channel */
  236. uint64_t packets_recved;
  237. /* ramblock */
  238. RAMBlock *block;
  239. /* ramblock host address */
  240. uint8_t *host;
  241. /* buffers to recv */
  242. struct iovec *iov;
  243. /* Pages that are not zero */
  244. ram_addr_t *normal;
  245. /* num of non zero pages */
  246. uint32_t normal_num;
  247. /* Pages that are zero */
  248. ram_addr_t *zero;
  249. /* num of zero pages */
  250. uint32_t zero_num;
  251. /* used for de-compression methods */
  252. void *compress_data;
  253. /* Flags for the QIOChannel */
  254. int read_flags;
  255. } MultiFDRecvParams;
  256. typedef struct {
  257. /*
  258. * The send_setup, send_cleanup, send_prepare are only called on
  259. * the QEMU instance at the migration source.
  260. */
  261. /*
  262. * Setup for sending side. Called once per channel during channel
  263. * setup phase.
  264. *
  265. * Must allocate p->iov. If packets are in use (default), one
  266. * extra iovec must be allocated for the packet header. Any memory
  267. * allocated in this hook must be released at send_cleanup.
  268. *
  269. * p->write_flags may be used for passing flags to the QIOChannel.
  270. *
  271. * p->compression_data may be used by compression methods to store
  272. * compression data.
  273. */
  274. int (*send_setup)(MultiFDSendParams *p, Error **errp);
  275. /*
  276. * Cleanup for sending side. Called once per channel during
  277. * channel cleanup phase.
  278. */
  279. void (*send_cleanup)(MultiFDSendParams *p, Error **errp);
  280. /*
  281. * Prepare the send packet. Called as a result of multifd_send()
  282. * on the client side, with p pointing to the MultiFDSendParams of
  283. * a channel that is currently idle.
  284. *
  285. * Must populate p->iov with the data to be sent, increment
  286. * p->iovs_num to match the amount of iovecs used and set
  287. * p->next_packet_size with the amount of data currently present
  288. * in p->iov.
  289. *
  290. * Must indicate whether this is a compression packet by setting
  291. * p->flags.
  292. *
  293. * As a last step, if packets are in use (default), must prepare
  294. * the packet by calling multifd_send_fill_packet().
  295. */
  296. int (*send_prepare)(MultiFDSendParams *p, Error **errp);
  297. /*
  298. * The recv_setup, recv_cleanup, recv are only called on the QEMU
  299. * instance at the migration destination.
  300. */
  301. /*
  302. * Setup for receiving side. Called once per channel during
  303. * channel setup phase. May be empty.
  304. *
  305. * May allocate data structures for the receiving of data. May use
  306. * p->iov. Compression methods may use p->compress_data.
  307. */
  308. int (*recv_setup)(MultiFDRecvParams *p, Error **errp);
  309. /*
  310. * Cleanup for receiving side. Called once per channel during
  311. * channel cleanup phase. May be empty.
  312. */
  313. void (*recv_cleanup)(MultiFDRecvParams *p);
  314. /*
  315. * Data receive method. Called as a result of multifd_recv() on
  316. * the client side, with p pointing to the MultiFDRecvParams of a
  317. * channel that is currently idle. Only called if there is data
  318. * available to receive.
  319. *
  320. * Must validate p->flags according to what was set at
  321. * send_prepare.
  322. *
  323. * Must read the data from the QIOChannel p->c.
  324. */
  325. int (*recv)(MultiFDRecvParams *p, Error **errp);
  326. } MultiFDMethods;
  327. void multifd_register_ops(int method, const MultiFDMethods *ops);
  328. void multifd_send_fill_packet(MultiFDSendParams *p);
  329. bool multifd_send_prepare_common(MultiFDSendParams *p);
  330. void multifd_send_zero_page_detect(MultiFDSendParams *p);
  331. void multifd_recv_zero_page_process(MultiFDRecvParams *p);
  332. void multifd_channel_connect(MultiFDSendParams *p, QIOChannel *ioc);
  333. bool multifd_send(MultiFDSendData **send_data);
  334. MultiFDSendData *multifd_send_data_alloc(void);
  335. void multifd_send_data_clear(MultiFDSendData *data);
  336. void multifd_send_data_free(MultiFDSendData *data);
  337. static inline uint32_t multifd_ram_page_size(void)
  338. {
  339. return qemu_target_page_size();
  340. }
  341. static inline uint32_t multifd_ram_page_count(void)
  342. {
  343. return MULTIFD_PACKET_SIZE / qemu_target_page_size();
  344. }
  345. void multifd_ram_save_setup(void);
  346. void multifd_ram_save_cleanup(void);
  347. int multifd_ram_flush_and_sync(QEMUFile *f);
  348. bool multifd_ram_sync_per_round(void);
  349. bool multifd_ram_sync_per_section(void);
  350. void multifd_ram_payload_alloc(MultiFDPages_t *pages);
  351. void multifd_ram_payload_free(MultiFDPages_t *pages);
  352. void multifd_ram_fill_packet(MultiFDSendParams *p);
  353. int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp);
  354. void multifd_send_data_clear_device_state(MultiFDDeviceState_t *device_state);
  355. void multifd_device_state_send_setup(void);
  356. void multifd_device_state_send_cleanup(void);
  357. void multifd_device_state_send_prepare(MultiFDSendParams *p);
  358. #endif