| 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/core/vmstate-if.h" |
| 18 | |
| 19 | typedef struct MigPendingData { |
| 20 | /* Amount of pending bytes can be transferred in precopy or stopcopy */ |
| 21 | uint64_t precopy_bytes; |
| 22 | /* Amount of pending bytes can be transferred in postcopy */ |
| 23 | uint64_t postcopy_bytes; |
| 24 | /* Amount of pending bytes can be transferred only in stopcopy */ |
| 25 | uint64_t stopcopy_bytes; |
| 26 | /* Number of new pending switchover ACKs */ |
| 27 | uint32_t switchover_ack_pending; |
| 28 | /* |
| 29 | * Total pending data, modules do not need to update this field, it |
| 30 | * will be automatically calculated by migration core API. |
| 31 | */ |
| 32 | uint64_t total_bytes; |
| 33 | } MigPendingData; |
| 34 | |
| 35 | /** |
| 36 | * struct SaveVMHandlers: handler structure to finely control |
| 37 | * migration of complex subsystems and devices, such as RAM, block and |
| 38 | * VFIO. |
| 39 | */ |
| 40 | typedef struct SaveVMHandlers { |
| 41 | |
| 42 | /* The following handlers run inside the BQL. */ |
| 43 | |
| 44 | /** |
| 45 | * @save_state |
| 46 | * |
| 47 | * Saves state section on the source using the latest state format |
| 48 | * version. |
| 49 | * |
| 50 | * Legacy method. Should be deprecated when all users are ported |
| 51 | * to VMStateDescription. |
| 52 | * |
| 53 | * @f: QEMUFile where to send the data |
| 54 | * @opaque: data pointer passed to register_savevm_live() |
| 55 | */ |
| 56 | void (*save_state)(QEMUFile *f, void *opaque); |
| 57 | |
| 58 | /** |
| 59 | * @save_prepare |
| 60 | * |
| 61 | * Called early, even before migration starts, and can be used to |
| 62 | * perform early checks. |
| 63 | * |
| 64 | * @opaque: data pointer passed to register_savevm_live() |
| 65 | * @errp: pointer to Error*, to store an error if it happens. |
| 66 | * |
| 67 | * Returns zero to indicate success and negative for error |
| 68 | */ |
| 69 | int (*save_prepare)(void *opaque, Error **errp); |
| 70 | |
| 71 | /** |
| 72 | * @save_setup |
| 73 | * |
| 74 | * Initializes the data structures on the source and transmits |
| 75 | * first section containing information on the device |
| 76 | * |
| 77 | * @f: QEMUFile where to send the data |
| 78 | * @opaque: data pointer passed to register_savevm_live() |
| 79 | * @errp: pointer to Error*, to store an error if it happens. |
| 80 | * |
| 81 | * Returns zero to indicate success and negative for error |
| 82 | */ |
| 83 | int (*save_setup)(QEMUFile *f, void *opaque, Error **errp); |
| 84 | |
| 85 | /** |
| 86 | * @save_cleanup |
| 87 | * |
| 88 | * Uninitializes the data structures on the source. |
| 89 | * Note that this handler can be called even if save_setup |
| 90 | * wasn't called earlier. |
| 91 | * |
| 92 | * @opaque: data pointer passed to register_savevm_live() |
| 93 | */ |
| 94 | void (*save_cleanup)(void *opaque); |
| 95 | |
| 96 | /** |
| 97 | * @save_complete |
| 98 | * |
| 99 | * Transmits the last section for the device containing any |
| 100 | * remaining data at the end phase of migration. |
| 101 | * |
| 102 | * For precopy, this will be invoked _during_ the switchover phase |
| 103 | * after source VM is stopped. |
| 104 | * |
| 105 | * For postcopy, this will be invoked _after_ the switchover phase |
| 106 | * (except some very unusual cases, like PMEM ramblocks), while |
| 107 | * destination VM can be running. |
| 108 | * |
| 109 | * @f: QEMUFile where to send the data |
| 110 | * @opaque: data pointer passed to register_savevm_live() |
| 111 | * |
| 112 | * Returns zero to indicate success and negative for error |
| 113 | */ |
| 114 | int (*save_complete)(QEMUFile *f, void *opaque); |
| 115 | |
| 116 | /** |
| 117 | * @save_complete_precopy_thread (invoked in a separate thread) |
| 118 | * |
| 119 | * Called at the end of a precopy phase from a separate worker thread |
| 120 | * in configurations where multifd device state transfer is supported |
| 121 | * in order to perform asynchronous transmission of the remaining data in |
| 122 | * parallel with @save_complete handlers. |
| 123 | * When postcopy is enabled, devices that support postcopy will skip this |
| 124 | * step. |
| 125 | * |
| 126 | * @d: a #SaveCompletePrecopyThreadData containing parameters that the |
| 127 | * handler may need, including this device section idstr and instance_id, |
| 128 | * and opaque data pointer passed to register_savevm_live(). |
| 129 | * @errp: pointer to Error*, to store an error if it happens. |
| 130 | * |
| 131 | * Returns true to indicate success and false for errors. |
| 132 | */ |
| 133 | SaveCompletePrecopyThreadHandler save_complete_precopy_thread; |
| 134 | |
| 135 | /* This runs both outside and inside the BQL. */ |
| 136 | |
| 137 | /** |
| 138 | * @is_active |
| 139 | * |
| 140 | * Will skip a state section if not active |
| 141 | * |
| 142 | * @opaque: data pointer passed to register_savevm_live() |
| 143 | * |
| 144 | * Returns true if state section is active else false |
| 145 | */ |
| 146 | bool (*is_active)(void *opaque); |
| 147 | |
| 148 | /** |
| 149 | * @has_postcopy |
| 150 | * |
| 151 | * Checks if a device supports postcopy |
| 152 | * |
| 153 | * @opaque: data pointer passed to register_savevm_live() |
| 154 | * |
| 155 | * Returns true for postcopy support else false |
| 156 | */ |
| 157 | bool (*has_postcopy)(void *opaque); |
| 158 | |
| 159 | /** |
| 160 | * @is_active_iterate |
| 161 | * |
| 162 | * As #SaveVMHandlers.is_active(), will skip an inactive state |
| 163 | * section in qemu_savevm_state_iterate. |
| 164 | * |
| 165 | * For example, it is needed for only-postcopy-states, which needs |
| 166 | * to be handled by qemu_savevm_state_setup() and |
| 167 | * qemu_savevm_state_pending(), but do not need iterations until |
| 168 | * not in postcopy stage. |
| 169 | * |
| 170 | * @opaque: data pointer passed to register_savevm_live() |
| 171 | * |
| 172 | * Returns true if state section is active else false |
| 173 | */ |
| 174 | bool (*is_active_iterate)(void *opaque); |
| 175 | |
| 176 | /** |
| 177 | * @save_query_pending |
| 178 | * |
| 179 | * This estimates the remaining data to transfer on the source side. |
| 180 | * |
| 181 | * When @exact is true, a module must report accurate results. When |
| 182 | * @exact is false, a module may report estimates. |
| 183 | * |
| 184 | * It's highly recommended that modules implement a faster version of |
| 185 | * the query path (for example, by proper caching on the counters) if |
| 186 | * an accurate query will be time-consuming. |
| 187 | * |
| 188 | * @opaque: data pointer passed to register_savevm_live() |
| 189 | * @pending: pointer to a MigPendingData struct |
| 190 | * @exact: set to true for an accurate (slow) query |
| 191 | * @final: set to true for the final query during switchover. When final is |
| 192 | * true, the query is called with BQL locked. Otherwise, it's called with |
| 193 | * BQL unlocked. |
| 194 | */ |
| 195 | void (*save_query_pending)(void *opaque, MigPendingData *pending, |
| 196 | bool exact, bool final); |
| 197 | |
| 198 | /* This runs outside the BQL in the migration case, and |
| 199 | * within the lock in the savevm case. The callback had better only |
| 200 | * use data that is local to the migration thread or protected |
| 201 | * by other locks. |
| 202 | */ |
| 203 | |
| 204 | /** |
| 205 | * @save_live_iterate |
| 206 | * |
| 207 | * Should send a chunk of data until the point that stream |
| 208 | * bandwidth limits tell it to stop. Each call generates one |
| 209 | * section. |
| 210 | * |
| 211 | * @f: QEMUFile where to send the data |
| 212 | * @opaque: data pointer passed to register_savevm_live() |
| 213 | * |
| 214 | * Returns 0 to indicate that there is still more data to send, |
| 215 | * 1 that there is no more data to send and |
| 216 | * negative to indicate an error. |
| 217 | */ |
| 218 | int (*save_live_iterate)(QEMUFile *f, void *opaque); |
| 219 | |
| 220 | /* This runs outside the BQL! */ |
| 221 | |
| 222 | /** |
| 223 | * @save_postcopy_prepare |
| 224 | * |
| 225 | * This hook will be invoked on the source side right before switching |
| 226 | * to postcopy (before VM stopped). |
| 227 | * |
| 228 | * @f: QEMUFile where to send the data |
| 229 | * @opaque: Data pointer passed to register_savevm_live() |
| 230 | * @errp: Error** used to report error message |
| 231 | * |
| 232 | * Returns: true if succeeded, false if error occured. When false is |
| 233 | * returned, @errp must be set. |
| 234 | */ |
| 235 | bool (*save_postcopy_prepare)(QEMUFile *f, void *opaque, Error **errp); |
| 236 | |
| 237 | /** |
| 238 | * @load_state |
| 239 | * |
| 240 | * Load sections generated by any of the save functions that |
| 241 | * generate sections. |
| 242 | * |
| 243 | * Legacy method. Should be deprecated when all users are ported |
| 244 | * to VMStateDescription. |
| 245 | * |
| 246 | * @f: QEMUFile where to receive the data |
| 247 | * @opaque: data pointer passed to register_savevm_live() |
| 248 | * @version_id: the maximum version_id supported |
| 249 | * |
| 250 | * Returns zero to indicate success and negative for error |
| 251 | */ |
| 252 | int (*load_state)(QEMUFile *f, void *opaque, int version_id); |
| 253 | |
| 254 | /** |
| 255 | * @load_state_buffer (invoked outside the BQL) |
| 256 | * |
| 257 | * Load device state buffer provided to qemu_loadvm_load_state_buffer(). |
| 258 | * |
| 259 | * @opaque: data pointer passed to register_savevm_live() |
| 260 | * @buf: the data buffer to load |
| 261 | * @len: the data length in buffer |
| 262 | * @errp: pointer to Error*, to store an error if it happens. |
| 263 | * |
| 264 | * Returns true to indicate success and false for errors. |
| 265 | */ |
| 266 | bool (*load_state_buffer)(void *opaque, char *buf, size_t len, |
| 267 | Error **errp); |
| 268 | |
| 269 | /** |
| 270 | * @load_setup |
| 271 | * |
| 272 | * Initializes the data structures on the destination. |
| 273 | * |
| 274 | * @f: QEMUFile where to receive the data |
| 275 | * @opaque: data pointer passed to register_savevm_live() |
| 276 | * @errp: pointer to Error*, to store an error if it happens. |
| 277 | * |
| 278 | * Returns zero to indicate success and negative for error |
| 279 | */ |
| 280 | int (*load_setup)(QEMUFile *f, void *opaque, Error **errp); |
| 281 | |
| 282 | /** |
| 283 | * @load_cleanup |
| 284 | * |
| 285 | * Uninitializes the data structures on the destination. |
| 286 | * Note that this handler can be called even if load_setup |
| 287 | * wasn't called earlier. |
| 288 | * |
| 289 | * @opaque: data pointer passed to register_savevm_live() |
| 290 | * |
| 291 | * Returns zero to indicate success and negative for error |
| 292 | */ |
| 293 | int (*load_cleanup)(void *opaque); |
| 294 | |
| 295 | /** |
| 296 | * @resume_prepare |
| 297 | * |
| 298 | * Called when postcopy migration wants to resume from failure |
| 299 | * |
| 300 | * @s: Current migration state |
| 301 | * @opaque: data pointer passed to register_savevm_live() |
| 302 | * |
| 303 | * Returns zero to indicate success and negative for error |
| 304 | */ |
| 305 | int (*resume_prepare)(MigrationState *s, void *opaque); |
| 306 | |
| 307 | /** |
| 308 | * @switchover_start |
| 309 | * |
| 310 | * Notifies that the switchover has started. Called only on |
| 311 | * the destination. |
| 312 | * |
| 313 | * @opaque: data pointer passed to register_savevm_live() |
| 314 | * |
| 315 | * Returns zero to indicate success and negative for error |
| 316 | */ |
| 317 | int (*switchover_start)(void *opaque); |
| 318 | } SaveVMHandlers; |
| 319 | |
| 320 | /** |
| 321 | * register_savevm_live: Register a set of custom migration handlers |
| 322 | * |
| 323 | * @idstr: state section identifier |
| 324 | * @instance_id: instance id |
| 325 | * @version_id: version id supported |
| 326 | * @ops: SaveVMHandlers structure |
| 327 | * @opaque: data pointer passed to SaveVMHandlers handlers |
| 328 | */ |
| 329 | int register_savevm_live(const char *idstr, |
| 330 | uint32_t instance_id, |
| 331 | int version_id, |
| 332 | const SaveVMHandlers *ops, |
| 333 | void *opaque); |
| 334 | |
| 335 | /** |
| 336 | * unregister_savevm: Unregister custom migration handlers |
| 337 | * |
| 338 | * @obj: object associated with state section |
| 339 | * @idstr: state section identifier |
| 340 | * @opaque: data pointer passed to register_savevm_live() |
| 341 | */ |
| 342 | void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque); |
| 343 | |
| 344 | #endif |