Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 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 | |
Li Zhijian | b7acd65 | 2021-09-09 09:29:50 +0200 | [diff] [blame] | 16 | bool migrate_multifd_is_allowed(void); |
| 17 | void migrate_protocol_allow_multifd(bool allow); |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 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 | |
Juan Quintela | 7ec2c2b | 2019-01-04 15:30:06 +0100 | [diff] [blame] | 28 | /* Multifd Compression flags */ |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 29 | #define MULTIFD_FLAG_SYNC (1 << 0) |
| 30 | |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 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) |
Juan Quintela | 7ec2c2b | 2019-01-04 15:30:06 +0100 | [diff] [blame] | 35 | #define MULTIFD_FLAG_ZLIB (1 << 1) |
Juan Quintela | 87dc6f5 | 2019-12-13 13:47:14 +0100 | [diff] [blame] | 36 | #define MULTIFD_FLAG_ZSTD (2 << 1) |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 37 | |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 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; |
Juan Quintela | 8c0ec0b | 2021-11-22 14:13:51 +0100 | [diff] [blame] | 47 | /* non zero pages */ |
| 48 | uint32_t normal_pages; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 49 | /* size of the next packet that contains pages */ |
| 50 | uint32_t next_packet_size; |
| 51 | uint64_t packet_num; |
| 52 | uint64_t unused[4]; /* Reserved for future use */ |
| 53 | char ramblock[256]; |
| 54 | uint64_t offset[]; |
| 55 | } __attribute__((packed)) MultiFDPacket_t; |
| 56 | |
| 57 | typedef struct { |
| 58 | /* number of used pages */ |
Juan Quintela | 90a3d2f | 2021-11-22 11:51:40 +0100 | [diff] [blame] | 59 | uint32_t num; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 60 | /* number of allocated pages */ |
| 61 | uint32_t allocated; |
| 62 | /* global number of generated multifd packets */ |
| 63 | uint64_t packet_num; |
| 64 | /* offset of each page */ |
| 65 | ram_addr_t *offset; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 66 | RAMBlock *block; |
| 67 | } MultiFDPages_t; |
| 68 | |
| 69 | typedef struct { |
| 70 | /* this fields are not changed once the thread is created */ |
| 71 | /* channel number */ |
| 72 | uint8_t id; |
| 73 | /* channel thread name */ |
| 74 | char *name; |
Chuan Zheng | 8e5fa05 | 2020-09-15 11:03:59 +0800 | [diff] [blame] | 75 | /* tls hostname */ |
| 76 | char *tls_hostname; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 77 | /* channel thread id */ |
| 78 | QemuThread thread; |
| 79 | /* communication channel */ |
| 80 | QIOChannel *c; |
| 81 | /* sem where to wait for more work */ |
| 82 | QemuSemaphore sem; |
| 83 | /* this mutex protects the following parameters */ |
| 84 | QemuMutex mutex; |
| 85 | /* is this channel thread running */ |
| 86 | bool running; |
| 87 | /* should this thread finish */ |
| 88 | bool quit; |
Lukas Straub | 20171ea | 2021-09-09 09:18:08 +0200 | [diff] [blame] | 89 | /* is the yank function registered */ |
| 90 | bool registered_yank; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 91 | /* thread has work to do */ |
| 92 | int pending_job; |
| 93 | /* array of pages to sent */ |
| 94 | MultiFDPages_t *pages; |
| 95 | /* packet allocated len */ |
| 96 | uint32_t packet_len; |
| 97 | /* pointer to the packet */ |
| 98 | MultiFDPacket_t *packet; |
| 99 | /* multifd flags for each packet */ |
| 100 | uint32_t flags; |
| 101 | /* size of the next packet that contains pages */ |
| 102 | uint32_t next_packet_size; |
| 103 | /* global number of generated multifd packets */ |
| 104 | uint64_t packet_num; |
| 105 | /* thread local variables */ |
| 106 | /* packets sent through this channel */ |
| 107 | uint64_t num_packets; |
Juan Quintela | 815956f | 2021-11-22 13:26:18 +0100 | [diff] [blame] | 108 | /* non zero pages sent through this channel */ |
| 109 | uint64_t total_normal_pages; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 110 | /* syncs main thread and channels */ |
| 111 | QemuSemaphore sem_sync; |
Juan Quintela | 226468b | 2021-11-19 12:06:05 +0100 | [diff] [blame] | 112 | /* buffers to send */ |
| 113 | struct iovec *iov; |
| 114 | /* number of iovs used */ |
| 115 | uint32_t iovs_num; |
Juan Quintela | 815956f | 2021-11-22 13:26:18 +0100 | [diff] [blame] | 116 | /* Pages that are not zero */ |
| 117 | ram_addr_t *normal; |
| 118 | /* num of non zero pages */ |
| 119 | uint32_t normal_num; |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 120 | /* used for compression methods */ |
| 121 | void *data; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 122 | } MultiFDSendParams; |
| 123 | |
| 124 | typedef struct { |
| 125 | /* this fields are not changed once the thread is created */ |
| 126 | /* channel number */ |
| 127 | uint8_t id; |
| 128 | /* channel thread name */ |
| 129 | char *name; |
| 130 | /* channel thread id */ |
| 131 | QemuThread thread; |
| 132 | /* communication channel */ |
| 133 | QIOChannel *c; |
| 134 | /* this mutex protects the following parameters */ |
| 135 | QemuMutex mutex; |
| 136 | /* is this channel thread running */ |
| 137 | bool running; |
| 138 | /* should this thread finish */ |
| 139 | bool quit; |
Juan Quintela | faf6093 | 2021-11-22 14:10:57 +0100 | [diff] [blame] | 140 | /* ramblock host address */ |
| 141 | uint8_t *host; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 142 | /* packet allocated len */ |
| 143 | uint32_t packet_len; |
| 144 | /* pointer to the packet */ |
| 145 | MultiFDPacket_t *packet; |
| 146 | /* multifd flags for each packet */ |
| 147 | uint32_t flags; |
| 148 | /* global number of generated multifd packets */ |
| 149 | uint64_t packet_num; |
| 150 | /* thread local variables */ |
| 151 | /* size of the next packet that contains pages */ |
| 152 | uint32_t next_packet_size; |
| 153 | /* packets sent through this channel */ |
| 154 | uint64_t num_packets; |
Juan Quintela | cf2d4aa | 2021-11-22 13:41:06 +0100 | [diff] [blame] | 155 | /* non zero pages recv through this channel */ |
| 156 | uint64_t total_normal_pages; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 157 | /* syncs main thread and channels */ |
| 158 | QemuSemaphore sem_sync; |
Juan Quintela | 226468b | 2021-11-19 12:06:05 +0100 | [diff] [blame] | 159 | /* buffers to recv */ |
| 160 | struct iovec *iov; |
Juan Quintela | cf2d4aa | 2021-11-22 13:41:06 +0100 | [diff] [blame] | 161 | /* Pages that are not zero */ |
| 162 | ram_addr_t *normal; |
| 163 | /* num of non zero pages */ |
| 164 | uint32_t normal_num; |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 165 | /* used for de-compression methods */ |
| 166 | void *data; |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 167 | } MultiFDRecvParams; |
| 168 | |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 169 | typedef struct { |
| 170 | /* Setup for sending side */ |
| 171 | int (*send_setup)(MultiFDSendParams *p, Error **errp); |
| 172 | /* Cleanup for sending side */ |
| 173 | void (*send_cleanup)(MultiFDSendParams *p, Error **errp); |
| 174 | /* Prepare the send packet */ |
Juan Quintela | 02fb810 | 2021-11-22 12:08:08 +0100 | [diff] [blame] | 175 | int (*send_prepare)(MultiFDSendParams *p, Error **errp); |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 176 | /* Setup for receiving side */ |
| 177 | int (*recv_setup)(MultiFDRecvParams *p, Error **errp); |
| 178 | /* Cleanup for receiving side */ |
| 179 | void (*recv_cleanup)(MultiFDRecvParams *p); |
| 180 | /* Read all pages */ |
Juan Quintela | 40a4bfe | 2021-11-22 12:49:43 +0100 | [diff] [blame] | 181 | int (*recv_pages)(MultiFDRecvParams *p, Error **errp); |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 182 | } MultiFDMethods; |
| 183 | |
Juan Quintela | 7ec2c2b | 2019-01-04 15:30:06 +0100 | [diff] [blame] | 184 | void multifd_register_ops(int method, MultiFDMethods *ops); |
| 185 | |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 186 | #endif |
| 187 | |