Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/migration/migration-hmp-cmds.c
Line
Count
Source
1
/*
2
 * HMP commands related to migration
3
 *
4
 * Copyright IBM, Corp. 2011
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
 * Contributions after 2012-01-13 are licensed under the terms of the
13
 * GNU GPL, version 2 or (at your option) any later version.
14
 */
15
16
#include "qemu/osdep.h"
17
#include "block/qapi.h"
18
#include "block/block-global-state.h"
19
#include "migration/snapshot.h"
20
#include "monitor/hmp.h"
21
#include "monitor/hmp-completion.h"
22
#include "monitor/monitor.h"
23
#include "monitor/monitor-internal.h"
24
#include "monitor/monitor-hmp-internal.h"
25
#include "qapi/error.h"
26
#include "qapi/qapi-commands-migration.h"
27
#include "qapi/qapi-visit-migration.h"
28
#include "qobject/qdict.h"
29
#include "qapi/string-input-visitor.h"
30
#include "qapi/string-output-visitor.h"
31
#include "qemu/cutils.h"
32
#include "qemu/error-report.h"
33
#include "qemu/sockets.h"
34
#include "system/runstate.h"
35
#include "ui/qemu-spice.h"
36
#include "system/system.h"
37
#include "options.h"
38
#include "migration.h"
39
40
static void migration_global_dump(MonitorHMP *hmp)
41
0
{
42
0
    MigrationState *ms = migrate_get_current();
43
44
0
    monitor_hmp_printf(hmp, "Globals:\n");
45
0
    monitor_hmp_printf(hmp, "  store-global-state: %s\n",
46
0
                       ms->store_global_state ? "on" : "off");
47
0
    monitor_hmp_printf(hmp, "  only-migratable: %s\n",
48
0
                       only_migratable ? "on" : "off");
49
0
    monitor_hmp_printf(hmp, "  send-configuration: %s\n",
50
0
                       ms->send_configuration ? "on" : "off");
51
0
    monitor_hmp_printf(hmp, "  send-section-footer: %s\n",
52
0
                       ms->send_section_footer ? "on" : "off");
53
0
    monitor_hmp_printf(hmp, "  send-switchover-start: %s\n",
54
0
                       ms->send_switchover_start ? "on" : "off");
55
0
    monitor_hmp_printf(hmp, "  clear-bitmap-shift: %u\n",
56
0
                       ms->clear_bitmap_shift);
57
0
}
58
59
static const gchar *format_time_str(uint64_t us)
60
0
{
61
0
    const char *units[] = {"us", "ms", "sec"};
62
0
    int index = 0;
63
64
0
    while (us >= 1000 && index + 1 < ARRAY_SIZE(units)) {
65
0
        us /= 1000;
66
0
        index++;
67
0
    }
68
69
0
    return g_strdup_printf("%"PRIu64" %s", us, units[index]);
70
0
}
71
72
static void migration_dump_blocktime(MonitorHMP *hmp, MigrationInfo *info)
73
0
{
74
0
    if (info->has_postcopy_blocktime) {
75
0
        monitor_hmp_printf(hmp, "Postcopy Blocktime (ms): %" PRIu32 "\n",
76
0
                           info->postcopy_blocktime);
77
0
    }
78
79
0
    if (info->has_postcopy_vcpu_blocktime) {
80
0
        uint32List *item = info->postcopy_vcpu_blocktime;
81
0
        const char *sep = "";
82
0
        int count = 0;
83
84
0
        monitor_hmp_printf(hmp, "Postcopy vCPU Blocktime (ms):\n [");
85
86
0
        while (item) {
87
0
            monitor_hmp_printf(hmp, "%s%"PRIu32, sep, item->value);
88
0
            item = item->next;
89
            /* Each line 10 vcpu results, newline if there's more */
90
0
            sep = ((++count % 10 == 0) && item) ? ",\n  " : ", ";
91
0
        }
92
0
        monitor_hmp_printf(hmp, "]\n");
93
0
    }
94
95
0
    if (info->has_postcopy_latency) {
96
0
        monitor_hmp_printf(hmp, "Postcopy Latency (ns): %" PRIu64 "\n",
97
0
                           info->postcopy_latency);
98
0
    }
99
100
0
    if (info->has_postcopy_non_vcpu_latency) {
101
0
        monitor_hmp_printf(hmp, "Postcopy non-vCPU Latency (ns): %" PRIu64 "\n",
102
0
                           info->postcopy_non_vcpu_latency);
103
0
    }
104
105
0
    if (info->has_postcopy_vcpu_latency) {
106
0
        uint64List *item = info->postcopy_vcpu_latency;
107
0
        const char *sep = "";
108
0
        int count = 0;
109
110
0
        monitor_hmp_printf(hmp, "Postcopy vCPU Latencies (ns):\n [");
111
112
0
        while (item) {
113
0
            monitor_hmp_printf(hmp, "%s%"PRIu64, sep, item->value);
114
0
            item = item->next;
115
            /* Each line 10 vcpu results, newline if there's more */
116
0
            sep = ((++count % 10 == 0) && item) ? ",\n  " : ", ";
117
0
        }
118
0
        monitor_hmp_printf(hmp, "]\n");
119
0
    }
120
121
0
    if (info->has_postcopy_latency_dist) {
122
0
        uint64List *item = info->postcopy_latency_dist;
123
0
        int count = 0;
124
125
0
        monitor_hmp_printf(hmp, "Postcopy Latency Distribution:\n");
126
127
0
        while (item) {
128
0
            g_autofree const gchar *from = format_time_str(1UL << count);
129
0
            g_autofree const gchar *to = format_time_str(1UL << (count + 1));
130
131
0
            monitor_hmp_printf(hmp, "  [ %8s - %8s ]: %10"PRIu64"\n",
132
0
                               from, to, item->value);
133
0
            item = item->next;
134
0
            count++;
135
0
        }
136
0
    }
137
0
}
138
139
void hmp_info_migrate(MonitorHMP *hmp, const QDict *qdict)
140
0
{
141
0
    bool show_all = qdict_get_try_bool(qdict, "all", false);
142
0
    MigrationInfo *info;
143
144
0
    info = qmp_query_migrate(NULL);
145
146
0
    if (info->blocked_reasons) {
147
0
        strList *reasons = info->blocked_reasons;
148
0
        monitor_hmp_printf(hmp, "Outgoing migration blocked:\n");
149
0
        while (reasons) {
150
0
            monitor_hmp_printf(hmp, "  %s\n", reasons->value);
151
0
            reasons = reasons->next;
152
0
        }
153
0
    }
154
155
0
    if (info->has_status) {
156
0
        monitor_hmp_printf(hmp, "Status: \t\t%s",
157
0
                           MigrationStatus_str(info->status));
158
0
        if ((info->status == MIGRATION_STATUS_FAILED ||
159
0
             info->status == MIGRATION_STATUS_POSTCOPY_PAUSED) &&
160
0
            info->error_desc) {
161
0
            monitor_hmp_printf(hmp, " (%s)\n", info->error_desc);
162
0
        } else {
163
0
            monitor_hmp_printf(hmp, "\n");
164
0
        }
165
166
0
        if (info->total_time) {
167
0
            monitor_hmp_printf(hmp, "Time (ms): \t\ttotal=%" PRIu64,
168
0
                               info->total_time);
169
0
            if (info->has_setup_time) {
170
0
                monitor_hmp_printf(hmp, ", setup=%" PRIu64,
171
0
                                   info->setup_time);
172
0
            }
173
0
            if (info->has_expected_downtime) {
174
0
                monitor_hmp_printf(hmp, ", exp_down=%" PRIu64,
175
0
                                   info->expected_downtime);
176
0
            }
177
0
            if (info->has_downtime) {
178
0
                monitor_hmp_printf(hmp, ", down=%" PRIu64,
179
0
                                   info->downtime);
180
0
            }
181
0
            monitor_hmp_printf(hmp, "\n");
182
0
        }
183
0
    }
184
185
0
    if (info->has_remaining) {
186
0
        g_autofree char *remaining = size_to_str(info->remaining);
187
0
        monitor_hmp_printf(hmp, "Remaining: \t\t%s\n", remaining);
188
0
    }
189
190
0
    if (info->has_socket_address) {
191
0
        SocketAddressList *addr;
192
193
0
        monitor_hmp_printf(hmp, "Sockets: [\n");
194
195
0
        for (addr = info->socket_address; addr; addr = addr->next) {
196
0
            char *s = socket_uri(addr->value);
197
0
            monitor_hmp_printf(hmp, "\t%s\n", s);
198
0
            g_free(s);
199
0
        }
200
0
        monitor_hmp_printf(hmp, "]\n");
201
0
    }
202
203
0
    if (info->ram) {
204
0
        g_autofree char *str_psize = size_to_str(info->ram->page_size);
205
0
        g_autofree char *str_total = size_to_str(info->ram->total);
206
0
        g_autofree char *str_transferred = size_to_str(info->ram->transferred);
207
0
        g_autofree char *str_remaining = size_to_str(info->ram->remaining);
208
0
        g_autofree char *str_precopy = size_to_str(info->ram->precopy_bytes);
209
0
        g_autofree char *str_multifd = size_to_str(info->ram->multifd_bytes);
210
0
        g_autofree char *str_postcopy = size_to_str(info->ram->postcopy_bytes);
211
212
0
        monitor_hmp_printf(hmp, "RAM info:\n");
213
0
        monitor_hmp_printf(hmp, "  Throughput (Mbps): \t%0.2f\n",
214
0
                           info->ram->mbps);
215
0
        monitor_hmp_printf(hmp, "  Sizes: \t\tpagesize=%s, total=%s\n",
216
0
                           str_psize, str_total);
217
0
        monitor_hmp_printf(hmp, "  Transfers: \t\ttransferred=%s, remain=%s\n",
218
0
                           str_transferred, str_remaining);
219
0
        monitor_hmp_printf(hmp, "    Channels: \t\tprecopy=%s, "
220
0
                           "multifd=%s, postcopy=%s",
221
0
                           str_precopy, str_multifd, str_postcopy);
222
223
0
        if (info->vfio) {
224
0
            g_autofree char *str_vfio = size_to_str(info->vfio->transferred);
225
226
0
            monitor_hmp_printf(hmp, ", vfio=%s", str_vfio);
227
0
        }
228
0
        monitor_hmp_printf(hmp, "\n");
229
230
0
        monitor_hmp_printf(hmp, "    Page Types: \tnormal=%" PRIu64
231
0
                           ", zero=%" PRIu64 "\n",
232
0
                           info->ram->normal, info->ram->duplicate);
233
0
        monitor_hmp_printf(hmp, "  Page Rates (pps): \ttransfer=%" PRIu64,
234
0
                           info->ram->pages_per_second);
235
0
        if (info->ram->dirty_pages_rate) {
236
0
            monitor_hmp_printf(hmp, ", dirty=%" PRIu64,
237
0
                               info->ram->dirty_pages_rate);
238
0
        }
239
0
        monitor_hmp_printf(hmp, "\n");
240
241
0
        monitor_hmp_printf(hmp, "  Others: \t\tdirty_syncs=%" PRIu64,
242
0
                           info->ram->dirty_sync_count);
243
0
        if (info->ram->postcopy_requests) {
244
0
            monitor_hmp_printf(hmp, ", postcopy_req=%" PRIu64,
245
0
                               info->ram->postcopy_requests);
246
0
        }
247
0
        if (info->ram->downtime_bytes) {
248
0
            monitor_hmp_printf(hmp, ", downtime_bytes=%" PRIu64,
249
0
                               info->ram->downtime_bytes);
250
0
        }
251
0
        if (info->ram->dirty_sync_missed_zero_copy) {
252
0
            monitor_hmp_printf(hmp, ", zerocopy_fallbacks=%" PRIu64,
253
0
                               info->ram->dirty_sync_missed_zero_copy);
254
0
        }
255
0
        monitor_hmp_printf(hmp, "\n");
256
0
    }
257
258
0
    if (!show_all) {
259
0
        goto out;
260
0
    }
261
262
0
    migration_global_dump(hmp);
263
264
0
    if (info->xbzrle_cache) {
265
0
        monitor_hmp_printf(hmp, "XBZRLE: size=%" PRIu64
266
0
                           ", transferred=%" PRIu64
267
0
                           ", pages=%" PRIu64
268
0
                           ", miss=%" PRIu64 "\n"
269
0
                           "  miss_rate=%0.2f"
270
0
                           ", encode_rate=%0.2f"
271
0
                           ", overflow=%" PRIu64 "\n",
272
0
                           info->xbzrle_cache->cache_size,
273
0
                           info->xbzrle_cache->bytes,
274
0
                           info->xbzrle_cache->pages,
275
0
                           info->xbzrle_cache->cache_miss,
276
0
                           info->xbzrle_cache->cache_miss_rate,
277
0
                           info->xbzrle_cache->encoding_rate,
278
0
                           info->xbzrle_cache->overflow);
279
0
    }
280
281
0
    if (info->has_cpu_throttle_percentage) {
282
0
        monitor_hmp_printf(hmp, "CPU Throttle (%%): %" PRIu64 "\n",
283
0
                           info->cpu_throttle_percentage);
284
0
    }
285
286
0
    if (info->has_dirty_limit_throttle_time_per_round) {
287
0
        monitor_hmp_printf(hmp, "Dirty-limit Throttle (us): %" PRIu64 "\n",
288
0
                           info->dirty_limit_throttle_time_per_round);
289
0
    }
290
291
0
    if (info->has_dirty_limit_ring_full_time) {
292
0
        monitor_hmp_printf(hmp, "Dirty-limit Ring Full (us): %" PRIu64 "\n",
293
0
                           info->dirty_limit_ring_full_time);
294
0
    }
295
296
0
    migration_dump_blocktime(hmp, info);
297
0
out:
298
0
    qapi_free_MigrationInfo(info);
299
0
}
300
301
void hmp_info_migrate_capabilities(MonitorHMP *hmp, const QDict *qdict)
302
0
{
303
0
    MigrationCapabilityStatusList *caps, *cap;
304
305
0
    caps = qmp_query_migrate_capabilities(NULL);
306
307
0
    if (caps) {
308
0
        for (cap = caps; cap; cap = cap->next) {
309
0
            monitor_hmp_printf(hmp, "%s: %s\n",
310
0
                               MigrationCapability_str(cap->value->capability),
311
0
                               cap->value->state ? "on" : "off");
312
0
        }
313
0
    }
314
315
0
    qapi_free_MigrationCapabilityStatusList(caps);
316
0
}
317
318
static void monitor_print_cpr_exec_command(MonitorHMP *hmp, strList *args)
319
0
{
320
0
    monitor_hmp_printf(hmp, "%s:",
321
0
        MigrationParameter_str(MIGRATION_PARAMETER_CPR_EXEC_COMMAND));
322
323
0
    while (args) {
324
0
        monitor_hmp_printf(hmp, " %s", args->value);
325
0
        args = args->next;
326
0
    }
327
0
    monitor_hmp_printf(hmp, "\n");
328
0
}
329
330
void hmp_info_migrate_parameters(MonitorHMP *hmp, const QDict *qdict)
331
0
{
332
0
    MigrationParameters *params;
333
0
    MigrationState *s = migrate_get_current();
334
335
0
    params = qmp_query_migrate_parameters(NULL);
336
337
0
    if (params) {
338
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " ms\n",
339
0
            MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
340
0
            params->announce_initial);
341
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " ms\n",
342
0
            MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
343
0
            params->announce_max);
344
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 "\n",
345
0
            MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
346
0
            params->announce_rounds);
347
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " ms\n",
348
0
            MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
349
0
            params->announce_step);
350
0
        assert(params->has_throttle_trigger_threshold);
351
0
        monitor_hmp_printf(hmp, "%s: %u\n",
352
0
            MigrationParameter_str(MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD),
353
0
            params->throttle_trigger_threshold);
354
0
        assert(params->has_cpu_throttle_initial);
355
0
        monitor_hmp_printf(hmp, "%s: %u\n",
356
0
            MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
357
0
            params->cpu_throttle_initial);
358
0
        assert(params->has_cpu_throttle_increment);
359
0
        monitor_hmp_printf(hmp, "%s: %u\n",
360
0
            MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
361
0
            params->cpu_throttle_increment);
362
0
        assert(params->has_cpu_throttle_tailslow);
363
0
        monitor_hmp_printf(hmp, "%s: %s\n",
364
0
            MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW),
365
0
            params->cpu_throttle_tailslow ? "on" : "off");
366
0
        assert(params->has_max_cpu_throttle);
367
0
        monitor_hmp_printf(hmp, "%s: %u\n",
368
0
            MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
369
0
            params->max_cpu_throttle);
370
0
        assert(params->tls_creds);
371
0
        monitor_hmp_printf(hmp, "%s: '%s'\n",
372
0
            MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
373
0
                       params->tls_creds->u.s);
374
0
        assert(params->tls_hostname);
375
0
        monitor_hmp_printf(hmp, "%s: '%s'\n",
376
0
            MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
377
0
                       params->tls_hostname->u.s);
378
0
        assert(params->tls_authz);
379
0
        monitor_hmp_printf(hmp, "%s: '%s'\n",
380
0
            MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
381
0
                       params->tls_authz->u.s);
382
0
        assert(params->has_max_bandwidth);
383
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " bytes/second\n",
384
0
            MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
385
0
            params->max_bandwidth);
386
0
        assert(params->has_avail_switchover_bandwidth);
387
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " bytes/second\n",
388
0
            MigrationParameter_str(MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH),
389
0
            params->avail_switchover_bandwidth);
390
0
        assert(params->has_max_postcopy_bandwidth);
391
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " bytes/second\n",
392
0
            MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
393
0
            params->max_postcopy_bandwidth);
394
0
        assert(params->has_downtime_limit);
395
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " ms\n",
396
0
            MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
397
0
            params->downtime_limit);
398
0
        assert(params->has_x_checkpoint_delay);
399
0
        monitor_hmp_printf(hmp, "%s: %u ms\n",
400
0
            MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
401
0
            params->x_checkpoint_delay);
402
0
        monitor_hmp_printf(hmp, "%s: %u\n",
403
0
            MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
404
0
            params->multifd_channels);
405
0
        monitor_hmp_printf(hmp, "%s: %s\n",
406
0
            MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_COMPRESSION),
407
0
            MultiFDCompression_str(params->multifd_compression));
408
0
        assert(params->has_zero_page_detection);
409
0
        monitor_hmp_printf(hmp, "%s: %s\n",
410
0
            MigrationParameter_str(MIGRATION_PARAMETER_ZERO_PAGE_DETECTION),
411
0
            qapi_enum_lookup(&ZeroPageDetection_lookup,
412
0
                params->zero_page_detection));
413
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " bytes\n",
414
0
            MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
415
0
            params->xbzrle_cache_size);
416
417
0
        if (s->has_block_bitmap_mapping) {
418
0
            const BitmapMigrationNodeAliasList *bmnal;
419
420
0
            monitor_hmp_printf(hmp, "%s:\n",
421
0
                               MigrationParameter_str(
422
0
                                   MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING));
423
424
0
            for (bmnal = params->block_bitmap_mapping;
425
0
                 bmnal;
426
0
                 bmnal = bmnal->next)
427
0
            {
428
0
                const BitmapMigrationNodeAlias *bmna = bmnal->value;
429
0
                const BitmapMigrationBitmapAliasList *bmbal;
430
431
0
                monitor_hmp_printf(hmp, "  '%s' -> '%s'\n",
432
0
                                   bmna->node_name, bmna->alias);
433
434
0
                for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
435
0
                    const BitmapMigrationBitmapAlias *bmba = bmbal->value;
436
437
0
                    monitor_hmp_printf(hmp, "    '%s' -> '%s'\n",
438
0
                                       bmba->name, bmba->alias);
439
0
                }
440
0
            }
441
0
        }
442
443
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " ms\n",
444
0
        MigrationParameter_str(MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD),
445
0
        params->x_vcpu_dirty_limit_period);
446
447
0
        monitor_hmp_printf(hmp, "%s: %" PRIu64 " MB/s\n",
448
0
            MigrationParameter_str(MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT),
449
0
            params->vcpu_dirty_limit);
450
451
0
        assert(params->has_mode);
452
0
        monitor_hmp_printf(hmp, "%s: %s\n",
453
0
            MigrationParameter_str(MIGRATION_PARAMETER_MODE),
454
0
            qapi_enum_lookup(&MigMode_lookup, params->mode));
455
456
0
        if (params->has_direct_io) {
457
0
            monitor_hmp_printf(hmp, "%s: %s\n",
458
0
                               MigrationParameter_str(
459
0
                                   MIGRATION_PARAMETER_DIRECT_IO),
460
0
                               params->direct_io ? "on" : "off");
461
0
        }
462
463
0
        if (params->has_x_rdma_chunk_size) {
464
0
            monitor_hmp_printf(hmp, "%s: %" PRIu64 " bytes\n",
465
0
                               MigrationParameter_str(
466
0
                                   MIGRATION_PARAMETER_X_RDMA_CHUNK_SIZE),
467
0
                               params->x_rdma_chunk_size);
468
0
        }
469
470
0
        assert(params->has_cpr_exec_command);
471
0
        monitor_print_cpr_exec_command(hmp, params->cpr_exec_command);
472
0
    }
473
474
0
    qapi_free_MigrationParameters(params);
475
0
}
476
477
void hmp_loadvm(MonitorHMP *hmp, const QDict *qdict)
478
0
{
479
0
    RunState saved_state = runstate_get();
480
481
0
    const char *name = qdict_get_str(qdict, "name");
482
0
    Error *err = NULL;
483
484
0
    vm_stop(RUN_STATE_RESTORE_VM);
485
486
0
    if (load_snapshot(name, NULL, false, NULL, &err)) {
487
0
        load_snapshot_resume(saved_state);
488
0
    }
489
490
0
    hmp_handle_error(hmp, err);
491
0
}
492
493
void hmp_savevm(MonitorHMP *hmp, const QDict *qdict)
494
0
{
495
0
    Error *err = NULL;
496
497
0
    save_snapshot(qdict_get_try_str(qdict, "name"),
498
0
                  true, NULL, false, NULL, &err);
499
0
    hmp_handle_error(hmp, err);
500
0
}
501
502
void hmp_delvm(MonitorHMP *hmp, const QDict *qdict)
503
0
{
504
0
    Error *err = NULL;
505
0
    const char *name = qdict_get_str(qdict, "name");
506
507
0
    delete_snapshot(name, false, NULL, &err);
508
0
    hmp_handle_error(hmp, err);
509
0
}
510
511
void hmp_migrate_cancel(MonitorHMP *hmp, const QDict *qdict)
512
0
{
513
0
    qmp_migrate_cancel(NULL);
514
0
}
515
516
void hmp_migrate_continue(MonitorHMP *hmp, const QDict *qdict)
517
0
{
518
0
    Error *err = NULL;
519
0
    const char *state = qdict_get_str(qdict, "state");
520
0
    int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
521
522
0
    if (val >= 0) {
523
0
        qmp_migrate_continue(val, &err);
524
0
    }
525
526
0
    hmp_handle_error(hmp, err);
527
0
}
528
529
void hmp_migrate_incoming(MonitorHMP *hmp, const QDict *qdict)
530
0
{
531
0
    Error *err = NULL;
532
0
    const char *uri = qdict_get_str(qdict, "uri");
533
0
    MigrationChannelList *caps = NULL;
534
0
    g_autoptr(MigrationChannel) channel = NULL;
535
536
0
    if (!migrate_uri_parse(uri, &channel, &err)) {
537
0
        goto end;
538
0
    }
539
0
    QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
540
541
0
    qmp_migrate_incoming(NULL, true, caps, true, false, &err);
542
0
    qapi_free_MigrationChannelList(caps);
543
544
0
end:
545
0
    hmp_handle_error(hmp, err);
546
0
}
547
548
void hmp_migrate_recover(MonitorHMP *hmp, const QDict *qdict)
549
0
{
550
0
    Error *err = NULL;
551
0
    const char *uri = qdict_get_str(qdict, "uri");
552
553
0
    qmp_migrate_recover(uri, &err);
554
555
0
    hmp_handle_error(hmp, err);
556
0
}
557
558
void hmp_migrate_pause(MonitorHMP *hmp, const QDict *qdict)
559
0
{
560
0
    Error *err = NULL;
561
562
0
    qmp_migrate_pause(&err);
563
564
0
    hmp_handle_error(hmp, err);
565
0
}
566
567
568
void hmp_migrate_set_capability(MonitorHMP *hmp, const QDict *qdict)
569
0
{
570
0
    const char *cap = qdict_get_str(qdict, "capability");
571
0
    bool state = qdict_get_bool(qdict, "state");
572
0
    Error *err = NULL;
573
0
    MigrationCapabilityStatusList *caps = NULL;
574
0
    MigrationCapabilityStatus *value;
575
0
    int val;
576
577
0
    val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
578
0
    if (val < 0) {
579
0
        goto end;
580
0
    }
581
582
0
    value = g_malloc0(sizeof(*value));
583
0
    value->capability = val;
584
0
    value->state = state;
585
0
    QAPI_LIST_PREPEND(caps, value);
586
0
    qmp_migrate_set_capabilities(caps, &err);
587
0
    qapi_free_MigrationCapabilityStatusList(caps);
588
589
0
end:
590
0
    hmp_handle_error(hmp, err);
591
0
}
592
593
void hmp_migrate_set_parameter(MonitorHMP *hmp, const QDict *qdict)
594
0
{
595
0
    const char *param = qdict_get_str(qdict, "parameter");
596
0
    const char *valuestr = qdict_get_str(qdict, "value");
597
0
    Visitor *v = string_input_visitor_new(valuestr);
598
0
    MigrationParameters *p = g_new0(MigrationParameters, 1);
599
0
    uint64_t valuebw = 0;
600
0
    uint64_t cache_size;
601
0
    Error *err = NULL;
602
0
    int val, ret;
603
604
0
    val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
605
0
    if (val < 0) {
606
0
        goto cleanup;
607
0
    }
608
609
0
    switch (val) {
610
0
    case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
611
0
        p->has_throttle_trigger_threshold = true;
612
0
        visit_type_uint8(v, param, &p->throttle_trigger_threshold, &err);
613
0
        break;
614
0
    case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
615
0
        p->has_cpu_throttle_initial = true;
616
0
        visit_type_uint8(v, param, &p->cpu_throttle_initial, &err);
617
0
        break;
618
0
    case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
619
0
        p->has_cpu_throttle_increment = true;
620
0
        visit_type_uint8(v, param, &p->cpu_throttle_increment, &err);
621
0
        break;
622
0
    case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
623
0
        p->has_cpu_throttle_tailslow = true;
624
0
        visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
625
0
        break;
626
0
    case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
627
0
        p->has_max_cpu_throttle = true;
628
0
        visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
629
0
        break;
630
0
    case MIGRATION_PARAMETER_TLS_CREDS:
631
0
        p->tls_creds = g_new0(StrOrNull, 1);
632
0
        p->tls_creds->type = QTYPE_QSTRING;
633
0
        visit_type_str(v, param, &p->tls_creds->u.s, &err);
634
0
        break;
635
0
    case MIGRATION_PARAMETER_TLS_HOSTNAME:
636
0
        p->tls_hostname = g_new0(StrOrNull, 1);
637
0
        p->tls_hostname->type = QTYPE_QSTRING;
638
0
        visit_type_str(v, param, &p->tls_hostname->u.s, &err);
639
0
        break;
640
0
    case MIGRATION_PARAMETER_TLS_AUTHZ:
641
0
        p->tls_authz = g_new0(StrOrNull, 1);
642
0
        p->tls_authz->type = QTYPE_QSTRING;
643
0
        visit_type_str(v, param, &p->tls_authz->u.s, &err);
644
0
        break;
645
0
    case MIGRATION_PARAMETER_MAX_BANDWIDTH:
646
0
        p->has_max_bandwidth = true;
647
        /*
648
         * Can't use visit_type_size() here, because it
649
         * defaults to Bytes rather than Mebibytes.
650
         */
651
0
        ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
652
0
        if (ret < 0 || valuebw > INT64_MAX
653
0
            || (size_t)valuebw != valuebw) {
654
0
            error_setg(&err, "Invalid size %s", valuestr);
655
0
            break;
656
0
        }
657
0
        p->max_bandwidth = valuebw;
658
0
        break;
659
0
    case MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH:
660
0
        p->has_avail_switchover_bandwidth = true;
661
0
        ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
662
0
        if (ret < 0 || valuebw > INT64_MAX
663
0
            || (size_t)valuebw != valuebw) {
664
0
            error_setg(&err, "Invalid size %s", valuestr);
665
0
            break;
666
0
        }
667
0
        p->avail_switchover_bandwidth = valuebw;
668
0
        break;
669
0
    case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
670
0
        p->has_downtime_limit = true;
671
0
        visit_type_size(v, param, &p->downtime_limit, &err);
672
0
        break;
673
0
    case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
674
0
        p->has_x_checkpoint_delay = true;
675
0
        visit_type_uint32(v, param, &p->x_checkpoint_delay, &err);
676
0
        break;
677
0
    case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
678
0
        p->has_multifd_channels = true;
679
0
        visit_type_uint8(v, param, &p->multifd_channels, &err);
680
0
        break;
681
0
    case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
682
0
        p->has_multifd_compression = true;
683
0
        visit_type_MultiFDCompression(v, param, &p->multifd_compression,
684
0
                                      &err);
685
0
        break;
686
0
    case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
687
0
        p->has_multifd_zlib_level = true;
688
0
        visit_type_uint8(v, param, &p->multifd_zlib_level, &err);
689
0
        break;
690
0
    case MIGRATION_PARAMETER_MULTIFD_QATZIP_LEVEL:
691
0
        p->has_multifd_qatzip_level = true;
692
0
        visit_type_uint8(v, param, &p->multifd_qatzip_level, &err);
693
0
        break;
694
0
    case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
695
0
        p->has_multifd_zstd_level = true;
696
0
        visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
697
0
        break;
698
0
    case MIGRATION_PARAMETER_ZERO_PAGE_DETECTION:
699
0
        p->has_zero_page_detection = true;
700
0
        visit_type_ZeroPageDetection(v, param, &p->zero_page_detection, &err);
701
0
        break;
702
0
    case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
703
0
        p->has_xbzrle_cache_size = true;
704
0
        if (!visit_type_size(v, param, &cache_size, &err)) {
705
0
            break;
706
0
        }
707
0
        if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
708
0
            error_setg(&err, "Invalid size %s", valuestr);
709
0
            break;
710
0
        }
711
0
        p->xbzrle_cache_size = cache_size;
712
0
        break;
713
0
    case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
714
0
        p->has_max_postcopy_bandwidth = true;
715
0
        visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
716
0
        break;
717
0
    case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
718
0
        p->has_announce_initial = true;
719
0
        visit_type_size(v, param, &p->announce_initial, &err);
720
0
        break;
721
0
    case MIGRATION_PARAMETER_ANNOUNCE_MAX:
722
0
        p->has_announce_max = true;
723
0
        visit_type_size(v, param, &p->announce_max, &err);
724
0
        break;
725
0
    case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
726
0
        p->has_announce_rounds = true;
727
0
        visit_type_size(v, param, &p->announce_rounds, &err);
728
0
        break;
729
0
    case MIGRATION_PARAMETER_ANNOUNCE_STEP:
730
0
        p->has_announce_step = true;
731
0
        visit_type_size(v, param, &p->announce_step, &err);
732
0
        break;
733
0
    case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
734
0
        error_setg(&err, "The block-bitmap-mapping parameter can only be set "
735
0
                   "through QMP");
736
0
        break;
737
0
    case MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD:
738
0
        p->has_x_vcpu_dirty_limit_period = true;
739
0
        visit_type_size(v, param, &p->x_vcpu_dirty_limit_period, &err);
740
0
        break;
741
0
    case MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT:
742
0
        p->has_vcpu_dirty_limit = true;
743
0
        visit_type_size(v, param, &p->vcpu_dirty_limit, &err);
744
0
        break;
745
0
    case MIGRATION_PARAMETER_MODE:
746
0
        p->has_mode = true;
747
0
        visit_type_MigMode(v, param, &p->mode, &err);
748
0
        break;
749
0
    case MIGRATION_PARAMETER_DIRECT_IO:
750
0
        p->has_direct_io = true;
751
0
        visit_type_bool(v, param, &p->direct_io, &err);
752
0
        break;
753
0
    case MIGRATION_PARAMETER_X_RDMA_CHUNK_SIZE:
754
0
        p->has_x_rdma_chunk_size = true;
755
0
        visit_type_size(v, param, &p->x_rdma_chunk_size, &err);
756
0
        break;
757
0
    case MIGRATION_PARAMETER_CPR_EXEC_COMMAND: {
758
        /*
759
         * NOTE: g_autofree will only auto g_free() the strv array when
760
         * needed, it will not free the strings within the array. It's
761
         * intentional: when strv is set, the ownership of the strings will
762
         * always be passed to p->cpr_exec_command via QAPI_LIST_APPEND().
763
         */
764
0
        g_autofree char **strv = NULL;
765
0
        g_autoptr(GError) gerr = NULL;
766
0
        strList **tail = &p->cpr_exec_command;
767
768
0
        if (!g_shell_parse_argv(valuestr, NULL, &strv, &gerr)) {
769
0
            error_setg(&err, "%s", gerr->message);
770
0
            break;
771
0
        }
772
0
        for (int i = 0; strv[i]; i++) {
773
0
            QAPI_LIST_APPEND(tail, strv[i]);
774
0
        }
775
0
        p->has_cpr_exec_command = true;
776
0
        break;
777
0
    }
778
0
    default:
779
0
        g_assert_not_reached();
780
0
    }
781
782
0
    if (err) {
783
0
        goto cleanup;
784
0
    }
785
786
0
    qmp_migrate_set_parameters(p, &err);
787
788
0
 cleanup:
789
0
    qapi_free_MigrationParameters(p);
790
0
    visit_free(v);
791
0
    hmp_handle_error(hmp, err);
792
0
}
793
794
void hmp_migrate_start_postcopy(MonitorHMP *hmp, const QDict *qdict)
795
0
{
796
0
    Error *err = NULL;
797
0
    qmp_migrate_start_postcopy(&err);
798
0
    hmp_handle_error(hmp, err);
799
0
}
800
801
#ifdef CONFIG_REPLICATION
802
void hmp_x_colo_lost_heartbeat(MonitorHMP *hmp, const QDict *qdict)
803
0
{
804
0
    Error *err = NULL;
805
806
0
    qmp_x_colo_lost_heartbeat(&err);
807
0
    hmp_handle_error(hmp, err);
808
0
}
809
#endif
810
811
typedef struct HMPMigrationStatus {
812
    QEMUTimer *timer;
813
    Monitor *mon;
814
} HMPMigrationStatus;
815
816
static void hmp_migrate_status_cb(void *opaque)
817
0
{
818
0
    HMPMigrationStatus *status = opaque;
819
0
    MigrationInfo *info;
820
821
0
    info = qmp_query_migrate(NULL);
822
0
    if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
823
0
        info->status == MIGRATION_STATUS_SETUP) {
824
0
        timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
825
0
    } else {
826
0
        if (info->error_desc) {
827
0
            error_report("%s", info->error_desc);
828
0
        }
829
0
        monitor_resume(status->mon);
830
0
        timer_free(status->timer);
831
0
        g_free(status);
832
0
    }
833
834
0
    qapi_free_MigrationInfo(info);
835
0
}
836
837
void hmp_migrate(MonitorHMP *hmp, const QDict *qdict)
838
0
{
839
0
    Monitor *mon = MONITOR(hmp);
840
0
    bool detach = qdict_get_try_bool(qdict, "detach", false);
841
0
    bool resume = qdict_get_try_bool(qdict, "resume", false);
842
0
    const char *uri = qdict_get_str(qdict, "uri");
843
0
    const char *uri_cpr = qdict_get_try_str(qdict, "uri-cpr");
844
0
    Error *err = NULL;
845
0
    g_autoptr(MigrationChannelList) caps = NULL;
846
0
    g_autoptr(MigrationChannel) channel = NULL;
847
0
    g_autoptr(MigrationChannel) channel_cpr = NULL;
848
849
0
    if (!migrate_uri_parse(uri, &channel, &err)) {
850
0
        hmp_handle_error(hmp, err);
851
0
        return;
852
0
    }
853
0
    QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
854
855
0
    if (uri_cpr) {
856
0
        if (migrate_mode() != MIG_MODE_CPR_TRANSFER) {
857
0
            error_setg(&err, "-c can only be used in cpr-transfer mode");
858
0
            hmp_handle_error(hmp, err);
859
0
            return;
860
0
        }
861
862
0
        if (!migrate_uri_parse(uri_cpr, &channel_cpr, &err)) {
863
0
            hmp_handle_error(hmp, err);
864
0
            return;
865
0
        }
866
867
0
        channel_cpr->channel_type = MIGRATION_CHANNEL_TYPE_CPR;
868
0
        QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel_cpr));
869
0
    }
870
871
0
    qmp_migrate(NULL, true, caps, true, resume, &err);
872
0
    if (hmp_handle_error(hmp, err)) {
873
0
        return;
874
0
    }
875
876
0
    if (!detach) {
877
0
        HMPMigrationStatus *status;
878
879
0
        if (!hmp->use_readline) {
880
0
            monitor_hmp_printf(hmp, "terminal does not allow synchronous "
881
0
                               "migration, continuing detached\n");
882
0
            return;
883
0
        }
884
0
        monitor_suspend(mon);
885
886
0
        status = g_malloc0(sizeof(*status));
887
0
        status->mon = mon;
888
0
        status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
889
0
                                          status);
890
0
        timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
891
0
    }
892
0
}
893
894
void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
895
                                       const char *str)
896
0
{
897
0
    size_t len;
898
899
0
    len = strlen(str);
900
0
    readline_set_completion_index(rs, len);
901
0
    if (nb_args == 2) {
902
0
        int i;
903
0
        for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
904
0
            readline_add_completion_of(rs, str, MigrationCapability_str(i));
905
0
        }
906
0
    } else if (nb_args == 3) {
907
0
        readline_add_completion_of(rs, str, "on");
908
0
        readline_add_completion_of(rs, str, "off");
909
0
    }
910
0
}
911
912
void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
913
                                      const char *str)
914
0
{
915
0
    size_t len;
916
917
0
    len = strlen(str);
918
0
    readline_set_completion_index(rs, len);
919
0
    if (nb_args == 2) {
920
0
        int i;
921
0
        for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
922
0
            readline_add_completion_of(rs, str, MigrationParameter_str(i));
923
0
        }
924
0
    }
925
0
}
926
927
static void vm_completion(ReadLineState *rs, const char *str)
928
0
{
929
0
    size_t len;
930
0
    BlockDriverState *bs;
931
0
    BdrvNextIterator it;
932
933
0
    GRAPH_RDLOCK_GUARD_MAINLOOP();
934
935
0
    len = strlen(str);
936
0
    readline_set_completion_index(rs, len);
937
938
0
    for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
939
0
        SnapshotInfoList *snapshots, *snapshot;
940
0
        bool ok = false;
941
942
0
        if (bdrv_can_snapshot(bs)) {
943
0
            ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
944
0
        }
945
0
        if (!ok) {
946
0
            continue;
947
0
        }
948
949
0
        snapshot = snapshots;
950
0
        while (snapshot) {
951
0
            readline_add_completion_of(rs, str, snapshot->value->name);
952
0
            readline_add_completion_of(rs, str, snapshot->value->id);
953
0
            snapshot = snapshot->next;
954
0
        }
955
0
        qapi_free_SnapshotInfoList(snapshots);
956
0
    }
957
958
0
}
959
960
void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
961
0
{
962
0
    if (nb_args == 2) {
963
0
        vm_completion(rs, str);
964
0
    }
965
0
}
966
967
void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
968
0
{
969
0
    if (nb_args == 2) {
970
0
        vm_completion(rs, str);
971
0
    }
972
0
}