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 /* This runs both outside and inside the BQL. */ 109 110 /** 111 * @is_active 112 * 113 * Will skip a state section if not active 114 * 115 * @opaque: data pointer passed to register_savevm_live() 116 * 117 * Returns true if state section is active else false 118 */ 119 bool (*is_active)(void *opaque); 120 121 /** 122 * @has_postcopy 123 * 124 * Checks if a device supports postcopy 125 * 126 * @opaque: data pointer passed to register_savevm_live() 127 * 128 * Returns true for postcopy support else false 129 */ 130 bool (*has_postcopy)(void *opaque); 131 132 /** 133 * @is_active_iterate 134 * 135 * As #SaveVMHandlers.is_active(), will skip an inactive state 136 * section in qemu_savevm_state_iterate. 137 * 138 * For example, it is needed for only-postcopy-states, which needs 139 * to be handled by qemu_savevm_state_setup() and 140 * qemu_savevm_state_pending(), but do not need iterations until 141 * not in postcopy stage. 142 * 143 * @opaque: data pointer passed to register_savevm_live() 144 * 145 * Returns true if state section is active else false 146 */ 147 bool (*is_active_iterate)(void *opaque); 148 149 /* This runs outside the BQL in the migration case, and 150 * within the lock in the savevm case. The callback had better only 151 * use data that is local to the migration thread or protected 152 * by other locks. 153 */ 154 155 /** 156 * @save_live_iterate 157 * 158 * Should send a chunk of data until the point that stream 159 * bandwidth limits tell it to stop. Each call generates one 160 * section. 161 * 162 * @f: QEMUFile where to send the data 163 * @opaque: data pointer passed to register_savevm_live() 164 * 165 * Returns 0 to indicate that there is still more data to send, 166 * 1 that there is no more data to send and 167 * negative to indicate an error. 168 */ 169 int (*save_live_iterate)(QEMUFile *f, void *opaque); 170 171 /* This runs outside the BQL! */ 172 173 /** 174 * @state_pending_estimate 175 * 176 * This estimates the remaining data to transfer 177 * 178 * Sum of @can_postcopy and @must_postcopy is the whole amount of 179 * pending data. 180 * 181 * @opaque: data pointer passed to register_savevm_live() 182 * @must_precopy: amount of data that must be migrated in precopy 183 * or in stopped state, i.e. that must be migrated 184 * before target start. 185 * @can_postcopy: amount of data that can be migrated in postcopy 186 * or in stopped state, i.e. after target start. 187 * Some can also be migrated during precopy (RAM). 188 * Some must be migrated after source stops 189 * (block-dirty-bitmap) 190 */ 191 void (*state_pending_estimate)(void *opaque, uint64_t *must_precopy, 192 uint64_t *can_postcopy); 193 194 /** 195 * @state_pending_exact 196 * 197 * This calculates the exact remaining data to transfer 198 * 199 * Sum of @can_postcopy and @must_postcopy is the whole amount of 200 * pending data. 201 * 202 * @opaque: data pointer passed to register_savevm_live() 203 * @must_precopy: amount of data that must be migrated in precopy 204 * or in stopped state, i.e. that must be migrated 205 * before target start. 206 * @can_postcopy: amount of data that can be migrated in postcopy 207 * or in stopped state, i.e. after target start. 208 * Some can also be migrated during precopy (RAM). 209 * Some must be migrated after source stops 210 * (block-dirty-bitmap) 211 */ 212 void (*state_pending_exact)(void *opaque, uint64_t *must_precopy, 213 uint64_t *can_postcopy); 214 215 /** 216 * @load_state 217 * 218 * Load sections generated by any of the save functions that 219 * generate sections. 220 * 221 * Legacy method. Should be deprecated when all users are ported 222 * to VMStateDescription. 223 * 224 * @f: QEMUFile where to receive the data 225 * @opaque: data pointer passed to register_savevm_live() 226 * @version_id: the maximum version_id supported 227 * 228 * Returns zero to indicate success and negative for error 229 */ 230 int (*load_state)(QEMUFile *f, void *opaque, int version_id); 231 232 /** 233 * @load_setup 234 * 235 * Initializes the data structures on the destination. 236 * 237 * @f: QEMUFile where to receive the data 238 * @opaque: data pointer passed to register_savevm_live() 239 * @errp: pointer to Error*, to store an error if it happens. 240 * 241 * Returns zero to indicate success and negative for error 242 */ 243 int (*load_setup)(QEMUFile *f, void *opaque, Error **errp); 244 245 /** 246 * @load_cleanup 247 * 248 * Uninitializes the data structures on the destination. 249 * Note that this handler can be called even if load_setup 250 * wasn't called earlier. 251 * 252 * @opaque: data pointer passed to register_savevm_live() 253 * 254 * Returns zero to indicate success and negative for error 255 */ 256 int (*load_cleanup)(void *opaque); 257 258 /** 259 * @resume_prepare 260 * 261 * Called when postcopy migration wants to resume from failure 262 * 263 * @s: Current migration state 264 * @opaque: data pointer passed to register_savevm_live() 265 * 266 * Returns zero to indicate success and negative for error 267 */ 268 int (*resume_prepare)(MigrationState *s, void *opaque); 269 270 /** 271 * @switchover_ack_needed 272 * 273 * Checks if switchover ack should be used. Called only on 274 * destination. 275 * 276 * @opaque: data pointer passed to register_savevm_live() 277 * 278 * Returns true if switchover ack should be used and false 279 * otherwise 280 */ 281 bool (*switchover_ack_needed)(void *opaque); 282 } SaveVMHandlers; 283 284 /** 285 * register_savevm_live: Register a set of custom migration handlers 286 * 287 * @idstr: state section identifier 288 * @instance_id: instance id 289 * @version_id: version id supported 290 * @ops: SaveVMHandlers structure 291 * @opaque: data pointer passed to SaveVMHandlers handlers 292 */ 293 int register_savevm_live(const char *idstr, 294 uint32_t instance_id, 295 int version_id, 296 const SaveVMHandlers *ops, 297 void *opaque); 298 299 /** 300 * unregister_savevm: Unregister custom migration handlers 301 * 302 * @obj: object associated with state section 303 * @idstr: state section identifier 304 * @opaque: data pointer passed to register_savevm_live() 305 */ 306 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque); 307 308 #endif 309