1 /* 2 * QEMU migration vmstate registration 3 * 4 * Copyright IBM, Corp. 2008 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef MIGRATION_REGISTER_H 15 #define MIGRATION_REGISTER_H 16 17 #include "hw/vmstate-if.h" 18 19 /** 20 * struct SaveVMHandlers: handler structure to finely control 21 * migration of complex subsystems and devices, such as RAM, block and 22 * VFIO. 23 */ 24 typedef struct SaveVMHandlers { 25 26 /* The following handlers run inside the BQL. */ 27 28 /** 29 * @save_state 30 * 31 * Saves state section on the source using the latest state format 32 * version. 33 * 34 * Legacy method. Should be deprecated when all users are ported 35 * to VMStateDescription. 36 * 37 * @f: QEMUFile where to send the data 38 * @opaque: data pointer passed to register_savevm_live() 39 */ 40 void (*save_state)(QEMUFile *f, void *opaque); 41 42 /** 43 * @save_prepare 44 * 45 * Called early, even before migration starts, and can be used to 46 * perform early checks. 47 * 48 * @opaque: data pointer passed to register_savevm_live() 49 * @errp: pointer to Error*, to store an error if it happens. 50 * 51 * Returns zero to indicate success and negative for error 52 */ 53 int (*save_prepare)(void *opaque, Error **errp); 54 55 /** 56 * @save_setup 57 * 58 * Initializes the data structures on the source and transmits 59 * first section containing information on the device 60 * 61 * @f: QEMUFile where to send the data 62 * @opaque: data pointer passed to register_savevm_live() 63 * @errp: pointer to Error*, to store an error if it happens. 64 * 65 * Returns zero to indicate success and negative for error 66 */ 67 int (*save_setup)(QEMUFile *f, void *opaque, Error **errp); 68 69 /** 70 * @save_cleanup 71 * 72 * Uninitializes the data structures on the source. 73 * Note that this handler can be called even if save_setup 74 * wasn't called earlier. 75 * 76 * @opaque: data pointer passed to register_savevm_live() 77 */ 78 void (*save_cleanup)(void *opaque); 79 80 /** 81 * @save_live_complete_postcopy 82 * 83 * Called at the end of postcopy for all postcopyable devices. 84 * 85 * @f: QEMUFile where to send the data 86 * @opaque: data pointer passed to register_savevm_live() 87 * 88 * Returns zero to indicate success and negative for error 89 */ 90 int (*save_live_complete_postcopy)(QEMUFile *f, void *opaque); 91 92 /** 93 * @save_live_complete_precopy 94 * 95 * Transmits the last section for the device containing any 96 * remaining data at the end of a precopy phase. When postcopy is 97 * enabled, devices that support postcopy will skip this step, 98 * where the final data will be flushed at the end of postcopy via 99 * @save_live_complete_postcopy instead. 100 * 101 * @f: QEMUFile where to send the data 102 * @opaque: data pointer passed to register_savevm_live() 103 * 104 * Returns zero to indicate success and negative for error 105 */ 106 int (*save_live_complete_precopy)(QEMUFile *f, void *opaque); 107 108 /** 109 * @save_live_complete_precopy_thread (invoked in a separate thread) 110 * 111 * Called at the end of a precopy phase from a separate worker thread 112 * in configurations where multifd device state transfer is supported 113 * in order to perform asynchronous transmission of the remaining data in 114 * parallel with @save_live_complete_precopy handlers. 115 * When postcopy is enabled, devices that support postcopy will skip this 116 * step. 117 * 118 * @d: a #SaveLiveCompletePrecopyThreadData containing parameters that the 119 * handler may need, including this device section idstr and instance_id, 120 * and opaque data pointer passed to register_savevm_live(). 121 * @errp: pointer to Error*, to store an error if it happens. 122 * 123 * Returns true to indicate success and false for errors. 124 */ 125 SaveLiveCompletePrecopyThreadHandler save_live_complete_precopy_thread; 126 127 /* This runs both outside and inside the BQL. */ 128 129 /** 130 * @is_active 131 * 132 * Will skip a state section if not active 133 * 134 * @opaque: data pointer passed to register_savevm_live() 135 * 136 * Returns true if state section is active else false 137 */ 138 bool (*is_active)(void *opaque); 139 140 /** 141 * @has_postcopy 142 * 143 * Checks if a device supports postcopy 144 * 145 * @opaque: data pointer passed to register_savevm_live() 146 * 147 * Returns true for postcopy support else false 148 */ 149 bool (*has_postcopy)(void *opaque); 150 151 /** 152 * @is_active_iterate 153 * 154 * As #SaveVMHandlers.is_active(), will skip an inactive state 155 * section in qemu_savevm_state_iterate. 156 * 157 * For example, it is needed for only-postcopy-states, which needs 158 * to be handled by qemu_savevm_state_setup() and 159 * qemu_savevm_state_pending(), but do not need iterations until 160 * not in postcopy stage. 161 * 162 * @opaque: data pointer passed to register_savevm_live() 163 * 164 * Returns true if state section is active else false 165 */ 166 bool (*is_active_iterate)(void *opaque); 167 168 /* This runs outside the BQL in the migration case, and 169 * within the lock in the savevm case. The callback had better only 170 * use data that is local to the migration thread or protected 171 * by other locks. 172 */ 173 174 /** 175 * @save_live_iterate 176 * 177 * Should send a chunk of data until the point that stream 178 * bandwidth limits tell it to stop. Each call generates one 179 * section. 180 * 181 * @f: QEMUFile where to send the data 182 * @opaque: data pointer passed to register_savevm_live() 183 * 184 * Returns 0 to indicate that there is still more data to send, 185 * 1 that there is no more data to send and 186 * negative to indicate an error. 187 */ 188 int (*save_live_iterate)(QEMUFile *f, void *opaque); 189 190 /* This runs outside the BQL! */ 191 192 /** 193 * @state_pending_estimate 194 * 195 * This estimates the remaining data to transfer 196 * 197 * Sum of @can_postcopy and @must_postcopy is the whole amount of 198 * pending data. 199 * 200 * @opaque: data pointer passed to register_savevm_live() 201 * @must_precopy: amount of data that must be migrated in precopy 202 * or in stopped state, i.e. that must be migrated 203 * before target start. 204 * @can_postcopy: amount of data that can be migrated in postcopy 205 * or in stopped state, i.e. after target start. 206 * Some can also be migrated during precopy (RAM). 207 * Some must be migrated after source stops 208 * (block-dirty-bitmap) 209 */ 210 void (*state_pending_estimate)(void *opaque, uint64_t *must_precopy, 211 uint64_t *can_postcopy); 212 213 /** 214 * @state_pending_exact 215 * 216 * This calculates the exact remaining data to transfer 217 * 218 * Sum of @can_postcopy and @must_postcopy is the whole amount of 219 * pending data. 220 * 221 * @opaque: data pointer passed to register_savevm_live() 222 * @must_precopy: amount of data that must be migrated in precopy 223 * or in stopped state, i.e. that must be migrated 224 * before target start. 225 * @can_postcopy: amount of data that can be migrated in postcopy 226 * or in stopped state, i.e. after target start. 227 * Some can also be migrated during precopy (RAM). 228 * Some must be migrated after source stops 229 * (block-dirty-bitmap) 230 */ 231 void (*state_pending_exact)(void *opaque, uint64_t *must_precopy, 232 uint64_t *can_postcopy); 233 234 /** 235 * @load_state 236 * 237 * Load sections generated by any of the save functions that 238 * generate sections. 239 * 240 * Legacy method. Should be deprecated when all users are ported 241 * to VMStateDescription. 242 * 243 * @f: QEMUFile where to receive the data 244 * @opaque: data pointer passed to register_savevm_live() 245 * @version_id: the maximum version_id supported 246 * 247 * Returns zero to indicate success and negative for error 248 */ 249 int (*load_state)(QEMUFile *f, void *opaque, int version_id); 250 251 /** 252 * @load_state_buffer (invoked outside the BQL) 253 * 254 * Load device state buffer provided to qemu_loadvm_load_state_buffer(). 255 * 256 * @opaque: data pointer passed to register_savevm_live() 257 * @buf: the data buffer to load 258 * @len: the data length in buffer 259 * @errp: pointer to Error*, to store an error if it happens. 260 * 261 * Returns true to indicate success and false for errors. 262 */ 263 bool (*load_state_buffer)(void *opaque, char *buf, size_t len, 264 Error **errp); 265 266 /** 267 * @load_setup 268 * 269 * Initializes the data structures on the destination. 270 * 271 * @f: QEMUFile where to receive the data 272 * @opaque: data pointer passed to register_savevm_live() 273 * @errp: pointer to Error*, to store an error if it happens. 274 * 275 * Returns zero to indicate success and negative for error 276 */ 277 int (*load_setup)(QEMUFile *f, void *opaque, Error **errp); 278 279 /** 280 * @load_cleanup 281 * 282 * Uninitializes the data structures on the destination. 283 * Note that this handler can be called even if load_setup 284 * wasn't called earlier. 285 * 286 * @opaque: data pointer passed to register_savevm_live() 287 * 288 * Returns zero to indicate success and negative for error 289 */ 290 int (*load_cleanup)(void *opaque); 291 292 /** 293 * @resume_prepare 294 * 295 * Called when postcopy migration wants to resume from failure 296 * 297 * @s: Current migration state 298 * @opaque: data pointer passed to register_savevm_live() 299 * 300 * Returns zero to indicate success and negative for error 301 */ 302 int (*resume_prepare)(MigrationState *s, void *opaque); 303 304 /** 305 * @switchover_ack_needed 306 * 307 * Checks if switchover ack should be used. Called only on 308 * destination. 309 * 310 * @opaque: data pointer passed to register_savevm_live() 311 * 312 * Returns true if switchover ack should be used and false 313 * otherwise 314 */ 315 bool (*switchover_ack_needed)(void *opaque); 316 317 /** 318 * @switchover_start 319 * 320 * Notifies that the switchover has started. Called only on 321 * the destination. 322 * 323 * @opaque: data pointer passed to register_savevm_live() 324 * 325 * Returns zero to indicate success and negative for error 326 */ 327 int (*switchover_start)(void *opaque); 328 } SaveVMHandlers; 329 330 /** 331 * register_savevm_live: Register a set of custom migration handlers 332 * 333 * @idstr: state section identifier 334 * @instance_id: instance id 335 * @version_id: version id supported 336 * @ops: SaveVMHandlers structure 337 * @opaque: data pointer passed to SaveVMHandlers handlers 338 */ 339 int register_savevm_live(const char *idstr, 340 uint32_t instance_id, 341 int version_id, 342 const SaveVMHandlers *ops, 343 void *opaque); 344 345 /** 346 * unregister_savevm: Unregister custom migration handlers 347 * 348 * @obj: object associated with state section 349 * @idstr: state section identifier 350 * @opaque: data pointer passed to register_savevm_live() 351 */ 352 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque); 353 354 #endif 355