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 13 #ifndef QEMU_MIGRATION_MULTIFD_H 14 #define QEMU_MIGRATION_MULTIFD_H 15 16 bool multifd_send_setup(void); 17 void multifd_send_shutdown(void); 18 int multifd_recv_setup(Error **errp); 19 void multifd_recv_cleanup(void); 20 void multifd_recv_shutdown(void); 21 bool multifd_recv_all_channels_created(void); 22 void multifd_recv_new_channel(QIOChannel *ioc, Error **errp); 23 void multifd_recv_sync_main(void); 24 int multifd_send_sync_main(void); 25 bool multifd_queue_page(RAMBlock *block, ram_addr_t offset); 26 27 /* Multifd Compression flags */ 28 #define MULTIFD_FLAG_SYNC (1 << 0) 29 30 /* We reserve 3 bits for compression methods */ 31 #define MULTIFD_FLAG_COMPRESSION_MASK (7 << 1) 32 /* we need to be compatible. Before compression value was 0 */ 33 #define MULTIFD_FLAG_NOCOMP (0 << 1) 34 #define MULTIFD_FLAG_ZLIB (1 << 1) 35 #define MULTIFD_FLAG_ZSTD (2 << 1) 36 37 /* This value needs to be a multiple of qemu_target_page_size() */ 38 #define MULTIFD_PACKET_SIZE (512 * 1024) 39 40 typedef struct { 41 uint32_t magic; 42 uint32_t version; 43 uint32_t flags; 44 /* maximum number of allocated pages */ 45 uint32_t pages_alloc; 46 /* non zero pages */ 47 uint32_t normal_pages; 48 /* size of the next packet that contains pages */ 49 uint32_t next_packet_size; 50 uint64_t packet_num; 51 uint64_t unused[4]; /* Reserved for future use */ 52 char ramblock[256]; 53 uint64_t offset[]; 54 } __attribute__((packed)) MultiFDPacket_t; 55 56 typedef struct { 57 /* number of used pages */ 58 uint32_t num; 59 /* number of allocated pages */ 60 uint32_t allocated; 61 /* offset of each page */ 62 ram_addr_t *offset; 63 RAMBlock *block; 64 } MultiFDPages_t; 65 66 typedef struct { 67 /* Fields are only written at creating/deletion time */ 68 /* No lock required for them, they are read only */ 69 70 /* channel number */ 71 uint8_t id; 72 /* channel thread name */ 73 char *name; 74 /* channel thread id */ 75 QemuThread thread; 76 bool thread_created; 77 QemuThread tls_thread; 78 bool tls_thread_created; 79 /* communication channel */ 80 QIOChannel *c; 81 /* packet allocated len */ 82 uint32_t packet_len; 83 /* guest page size */ 84 uint32_t page_size; 85 /* number of pages in a full packet */ 86 uint32_t page_count; 87 /* multifd flags for sending ram */ 88 int write_flags; 89 90 /* sem where to wait for more work */ 91 QemuSemaphore sem; 92 /* syncs main thread and channels */ 93 QemuSemaphore sem_sync; 94 95 /* multifd flags for each packet */ 96 uint32_t flags; 97 /* 98 * The sender thread has work to do if either of below boolean is set. 99 * 100 * @pending_job: a job is pending 101 * @pending_sync: a sync request is pending 102 * 103 * For both of these fields, they're only set by the requesters, and 104 * cleared by the multifd sender threads. 105 */ 106 bool pending_job; 107 bool pending_sync; 108 /* array of pages to sent. 109 * The owner of 'pages' depends of 'pending_job' value: 110 * pending_job == 0 -> migration_thread can use it. 111 * pending_job != 0 -> multifd_channel can use it. 112 */ 113 MultiFDPages_t *pages; 114 115 /* thread local variables. No locking required */ 116 117 /* pointer to the packet */ 118 MultiFDPacket_t *packet; 119 /* size of the next packet that contains pages */ 120 uint32_t next_packet_size; 121 /* packets sent through this channel */ 122 uint64_t packets_sent; 123 /* non zero pages sent through this channel */ 124 uint64_t total_normal_pages; 125 /* buffers to send */ 126 struct iovec *iov; 127 /* number of iovs used */ 128 uint32_t iovs_num; 129 /* used for compression methods */ 130 void *data; 131 } MultiFDSendParams; 132 133 typedef struct { 134 /* Fields are only written at creating/deletion time */ 135 /* No lock required for them, they are read only */ 136 137 /* channel number */ 138 uint8_t id; 139 /* channel thread name */ 140 char *name; 141 /* channel thread id */ 142 QemuThread thread; 143 bool thread_created; 144 /* communication channel */ 145 QIOChannel *c; 146 /* packet allocated len */ 147 uint32_t packet_len; 148 /* guest page size */ 149 uint32_t page_size; 150 /* number of pages in a full packet */ 151 uint32_t page_count; 152 153 /* syncs main thread and channels */ 154 QemuSemaphore sem_sync; 155 156 /* this mutex protects the following parameters */ 157 QemuMutex mutex; 158 /* should this thread finish */ 159 bool quit; 160 /* multifd flags for each packet */ 161 uint32_t flags; 162 /* global number of generated multifd packets */ 163 uint64_t packet_num; 164 165 /* thread local variables. No locking required */ 166 167 /* pointer to the packet */ 168 MultiFDPacket_t *packet; 169 /* size of the next packet that contains pages */ 170 uint32_t next_packet_size; 171 /* packets received through this channel */ 172 uint64_t packets_recved; 173 /* ramblock */ 174 RAMBlock *block; 175 /* ramblock host address */ 176 uint8_t *host; 177 /* non zero pages recv through this channel */ 178 uint64_t total_normal_pages; 179 /* buffers to recv */ 180 struct iovec *iov; 181 /* Pages that are not zero */ 182 ram_addr_t *normal; 183 /* num of non zero pages */ 184 uint32_t normal_num; 185 /* used for de-compression methods */ 186 void *data; 187 } MultiFDRecvParams; 188 189 typedef struct { 190 /* Setup for sending side */ 191 int (*send_setup)(MultiFDSendParams *p, Error **errp); 192 /* Cleanup for sending side */ 193 void (*send_cleanup)(MultiFDSendParams *p, Error **errp); 194 /* Prepare the send packet */ 195 int (*send_prepare)(MultiFDSendParams *p, Error **errp); 196 /* Setup for receiving side */ 197 int (*recv_setup)(MultiFDRecvParams *p, Error **errp); 198 /* Cleanup for receiving side */ 199 void (*recv_cleanup)(MultiFDRecvParams *p); 200 /* Read all pages */ 201 int (*recv_pages)(MultiFDRecvParams *p, Error **errp); 202 } MultiFDMethods; 203 204 void multifd_register_ops(int method, MultiFDMethods *ops); 205 void multifd_send_fill_packet(MultiFDSendParams *p); 206 207 static inline void multifd_send_prepare_header(MultiFDSendParams *p) 208 { 209 p->iov[0].iov_len = p->packet_len; 210 p->iov[0].iov_base = p->packet; 211 p->iovs_num++; 212 } 213 214 215 #endif 216