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 migrate_multifd_is_allowed(void); 17 void migrate_protocol_allow_multifd(bool allow); 18 int multifd_save_setup(Error **errp); 19 void multifd_save_cleanup(void); 20 int multifd_load_setup(Error **errp); 21 int multifd_load_cleanup(Error **errp); 22 bool multifd_recv_all_channels_created(void); 23 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp); 24 void multifd_recv_sync_main(void); 25 void multifd_send_sync_main(QEMUFile *f); 26 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset); 27 28 /* Multifd Compression flags */ 29 #define MULTIFD_FLAG_SYNC (1 << 0) 30 31 /* We reserve 3 bits for compression methods */ 32 #define MULTIFD_FLAG_COMPRESSION_MASK (7 << 1) 33 /* we need to be compatible. Before compression value was 0 */ 34 #define MULTIFD_FLAG_NOCOMP (0 << 1) 35 #define MULTIFD_FLAG_ZLIB (1 << 1) 36 #define MULTIFD_FLAG_ZSTD (2 << 1) 37 38 /* This value needs to be a multiple of qemu_target_page_size() */ 39 #define MULTIFD_PACKET_SIZE (512 * 1024) 40 41 typedef struct { 42 uint32_t magic; 43 uint32_t version; 44 uint32_t flags; 45 /* maximum number of allocated pages */ 46 uint32_t pages_alloc; 47 uint32_t pages_used; 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 /* global number of generated multifd packets */ 62 uint64_t packet_num; 63 /* offset of each page */ 64 ram_addr_t *offset; 65 /* pointer to each page */ 66 struct iovec *iov; 67 RAMBlock *block; 68 } MultiFDPages_t; 69 70 typedef struct { 71 /* this fields are not changed once the thread is created */ 72 /* channel number */ 73 uint8_t id; 74 /* channel thread name */ 75 char *name; 76 /* tls hostname */ 77 char *tls_hostname; 78 /* channel thread id */ 79 QemuThread thread; 80 /* communication channel */ 81 QIOChannel *c; 82 /* sem where to wait for more work */ 83 QemuSemaphore sem; 84 /* this mutex protects the following parameters */ 85 QemuMutex mutex; 86 /* is this channel thread running */ 87 bool running; 88 /* should this thread finish */ 89 bool quit; 90 /* is the yank function registered */ 91 bool registered_yank; 92 /* thread has work to do */ 93 int pending_job; 94 /* array of pages to sent */ 95 MultiFDPages_t *pages; 96 /* packet allocated len */ 97 uint32_t packet_len; 98 /* pointer to the packet */ 99 MultiFDPacket_t *packet; 100 /* multifd flags for each packet */ 101 uint32_t flags; 102 /* size of the next packet that contains pages */ 103 uint32_t next_packet_size; 104 /* global number of generated multifd packets */ 105 uint64_t packet_num; 106 /* thread local variables */ 107 /* packets sent through this channel */ 108 uint64_t num_packets; 109 /* pages sent through this channel */ 110 uint64_t num_pages; 111 /* syncs main thread and channels */ 112 QemuSemaphore sem_sync; 113 /* used for compression methods */ 114 void *data; 115 } MultiFDSendParams; 116 117 typedef struct { 118 /* this fields are not changed once the thread is created */ 119 /* channel number */ 120 uint8_t id; 121 /* channel thread name */ 122 char *name; 123 /* channel thread id */ 124 QemuThread thread; 125 /* communication channel */ 126 QIOChannel *c; 127 /* this mutex protects the following parameters */ 128 QemuMutex mutex; 129 /* is this channel thread running */ 130 bool running; 131 /* should this thread finish */ 132 bool quit; 133 /* array of pages to receive */ 134 MultiFDPages_t *pages; 135 /* packet allocated len */ 136 uint32_t packet_len; 137 /* pointer to the packet */ 138 MultiFDPacket_t *packet; 139 /* multifd flags for each packet */ 140 uint32_t flags; 141 /* global number of generated multifd packets */ 142 uint64_t packet_num; 143 /* thread local variables */ 144 /* size of the next packet that contains pages */ 145 uint32_t next_packet_size; 146 /* packets sent through this channel */ 147 uint64_t num_packets; 148 /* pages sent through this channel */ 149 uint64_t num_pages; 150 /* syncs main thread and channels */ 151 QemuSemaphore sem_sync; 152 /* used for de-compression methods */ 153 void *data; 154 } MultiFDRecvParams; 155 156 typedef struct { 157 /* Setup for sending side */ 158 int (*send_setup)(MultiFDSendParams *p, Error **errp); 159 /* Cleanup for sending side */ 160 void (*send_cleanup)(MultiFDSendParams *p, Error **errp); 161 /* Prepare the send packet */ 162 int (*send_prepare)(MultiFDSendParams *p, Error **errp); 163 /* Write the send packet */ 164 int (*send_write)(MultiFDSendParams *p, uint32_t used, Error **errp); 165 /* Setup for receiving side */ 166 int (*recv_setup)(MultiFDRecvParams *p, Error **errp); 167 /* Cleanup for receiving side */ 168 void (*recv_cleanup)(MultiFDRecvParams *p); 169 /* Read all pages */ 170 int (*recv_pages)(MultiFDRecvParams *p, Error **errp); 171 } MultiFDMethods; 172 173 void multifd_register_ops(int method, MultiFDMethods *ops); 174 175 #endif 176 177