qemu_migration_params.c 21.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * qemu_migration_params.c: QEMU migration parameters handling
 *
 * Copyright (C) 2006-2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "virlog.h"
#include "virerror.h"
#include "viralloc.h"
#include "virstring.h"

#include "qemu_alias.h"
#include "qemu_hotplug.h"
#include "qemu_migration.h"
#include "qemu_migration_params.h"
33
#include "qemu_monitor.h"
34 35 36 37 38 39 40

#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_migration_params");

#define QEMU_MIGRATION_TLS_ALIAS_BASE "libvirt_migrate"

41
struct _qemuMigrationParams {
42
    unsigned long long compMethods; /* bit-wise OR of qemuMigrationCompressMethod */
43
    virBitmapPtr caps;
44 45 46
    qemuMonitorMigrationParams params;
};

47 48 49 50 51 52 53 54 55 56 57 58 59
typedef enum {
    QEMU_MIGRATION_COMPRESS_XBZRLE = 0,
    QEMU_MIGRATION_COMPRESS_MT,

    QEMU_MIGRATION_COMPRESS_LAST
} qemuMigrationCompressMethod;
VIR_ENUM_DECL(qemuMigrationCompressMethod)
VIR_ENUM_IMPL(qemuMigrationCompressMethod, QEMU_MIGRATION_COMPRESS_LAST,
              "xbzrle",
              "mt",
);


60 61 62 63 64 65
typedef struct _qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOnItem;
struct _qemuMigrationParamsAlwaysOnItem {
    qemuMonitorMigrationCaps cap;
    int party; /* bit-wise OR of qemuMigrationParty */
};

66 67 68 69 70 71 72
typedef struct _qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMapItem;
struct _qemuMigrationParamsFlagMapItem {
    virDomainMigrateFlags flag;
    qemuMonitorMigrationCaps cap;
    int party; /* bit-wise OR of qemuMigrationParty */
};

73 74 75 76 77 78 79
/* Migration capabilities which should always be enabled as long as they
 * are supported by QEMU. */
static const qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOn[] = {
    {QEMU_MONITOR_MIGRATION_CAPS_PAUSE_BEFORE_SWITCHOVER,
     QEMU_MIGRATION_SOURCE},
};

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/* Translation from virDomainMigrateFlags to qemuMonitorMigrationCaps. */
static const qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMap[] = {
    {VIR_MIGRATE_RDMA_PIN_ALL,
     QEMU_MONITOR_MIGRATION_CAPS_RDMA_PIN_ALL,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_AUTO_CONVERGE,
     QEMU_MONITOR_MIGRATION_CAPS_AUTO_CONVERGE,
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_POSTCOPY,
     QEMU_MONITOR_MIGRATION_CAPS_POSTCOPY,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
};

95

96
static qemuMigrationParamsPtr
97 98
qemuMigrationParamsNew(void)
{
99
    qemuMigrationParamsPtr params;
100 101 102 103

    if (VIR_ALLOC(params) < 0)
        return NULL;

104 105 106 107
    params->caps = virBitmapNew(QEMU_MONITOR_MIGRATION_CAPS_LAST);
    if (!params->caps)
        goto error;

108
    return params;
109 110 111 112

 error:
    qemuMigrationParamsFree(params);
    return NULL;
113 114 115
}


116
void
117
qemuMigrationParamsFree(qemuMigrationParamsPtr migParams)
118 119 120 121
{
    if (!migParams)
        return;

122
    virBitmapFree(migParams->caps);
123 124
    VIR_FREE(migParams->params.tlsCreds);
    VIR_FREE(migParams->params.tlsHostname);
125
    VIR_FREE(migParams);
126 127 128
}


129 130 131 132 133 134 135 136 137 138 139
#define GET(API, PARAM, VAR) \
    do { \
        int rc; \
        if ((rc = API(params, nparams, VIR_MIGRATE_PARAM_ ## PARAM, \
                      &migParams->params.VAR)) < 0) \
            goto error; \
 \
        if (rc == 1) \
            migParams->params.VAR ## _set = true; \
    } while (0)

140

141
static int
142 143 144
qemuMigrationParamsSetCompression(virTypedParameterPtr params,
                                  int nparams,
                                  unsigned long flags,
145 146
                                  qemuMigrationParamsPtr migParams)
{
147 148 149
    size_t i;
    int method;
    qemuMonitorMigrationCaps cap;
150

151 152 153
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, VIR_MIGRATE_PARAM_COMPRESSION))
            continue;
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
        method = qemuMigrationCompressMethodTypeFromString(params[i].value.s);
        if (method < 0) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("Unsupported compression method '%s'"),
                           params[i].value.s);
            goto error;
        }

        if (migParams->compMethods & (1ULL << method)) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("Compression method '%s' is specified twice"),
                           params[i].value.s);
            goto error;
        }
169

170
        migParams->compMethods |= 1ULL << method;
171

172 173 174 175
        switch ((qemuMigrationCompressMethod) method) {
        case QEMU_MIGRATION_COMPRESS_XBZRLE:
            cap = QEMU_MONITOR_MIGRATION_CAPS_XBZRLE;
            break;
176

177 178 179
        case QEMU_MIGRATION_COMPRESS_MT:
            cap = QEMU_MONITOR_MIGRATION_CAPS_COMPRESS;
            break;
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        case QEMU_MIGRATION_COMPRESS_LAST:
        default:
            continue;
        }
        ignore_value(virBitmapSetBit(migParams->caps, cap));
    }

    if (params) {
        GET(virTypedParamsGetInt, COMPRESSION_MT_LEVEL, compressLevel);
        GET(virTypedParamsGetInt, COMPRESSION_MT_THREADS, compressThreads);
        GET(virTypedParamsGetInt, COMPRESSION_MT_DTHREADS, decompressThreads);
        GET(virTypedParamsGetULLong, COMPRESSION_XBZRLE_CACHE, xbzrleCacheSize);
    }

    if ((migParams->params.compressLevel_set ||
         migParams->params.compressThreads_set ||
         migParams->params.decompressThreads_set) &&
        !(migParams->compMethods & (1ULL << QEMU_MIGRATION_COMPRESS_MT))) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn multithread compression on to tune it"));
        goto error;
    }

    if (migParams->params.xbzrleCacheSize_set &&
        !(migParams->compMethods & (1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE))) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn xbzrle compression on to tune it"));
        goto error;
    }

    if (!migParams->compMethods && (flags & VIR_MIGRATE_COMPRESSED)) {
        migParams->compMethods = 1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE;
        ignore_value(virBitmapSetBit(migParams->caps,
                                     QEMU_MONITOR_MIGRATION_CAPS_XBZRLE));
    }
216 217

    return 0;
218 219 220

 error:
    return -1;
221 222 223
}


224
qemuMigrationParamsPtr
225 226
qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                             int nparams,
227
                             unsigned long flags,
228
                             qemuMigrationParty party)
229
{
230
    qemuMigrationParamsPtr migParams;
231
    size_t i;
232

233
    if (!(migParams = qemuMigrationParamsNew()))
234 235
        return NULL;

236 237 238 239 240 241 242 243 244 245
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsFlagMap); i++) {
        qemuMonitorMigrationCaps cap = qemuMigrationParamsFlagMap[i].cap;

        if (qemuMigrationParamsFlagMap[i].party & party &&
            flags & qemuMigrationParamsFlagMap[i].flag) {
            VIR_DEBUG("Enabling migration capability '%s'",
                      qemuMonitorMigrationCapsTypeToString(cap));
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }
246

247 248
    if (params) {
        if (party == QEMU_MIGRATION_SOURCE) {
249 250
            GET(virTypedParamsGetInt, AUTO_CONVERGE_INITIAL, cpuThrottleInitial);
            GET(virTypedParamsGetInt, AUTO_CONVERGE_INCREMENT, cpuThrottleIncrement);
251
        }
252
    }
253

254 255
    if ((migParams->params.cpuThrottleInitial_set ||
         migParams->params.cpuThrottleIncrement_set) &&
256 257 258 259 260 261
        !(flags & VIR_MIGRATE_AUTO_CONVERGE)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn auto convergence on to tune it"));
        goto error;
    }

262
    if (qemuMigrationParamsSetCompression(params, nparams, flags, migParams) < 0)
263 264
        goto error;

265 266 267
    return migParams;

 error:
268
    qemuMigrationParamsFree(migParams);
269 270 271
    return NULL;
}

272 273
#undef GET

274

275
int
276 277 278 279 280
qemuMigrationParamsDump(qemuMigrationParamsPtr migParams,
                        virTypedParameterPtr *params,
                        int *nparams,
                        int *maxparams,
                        unsigned long *flags)
281 282 283
{
    size_t i;

284 285
    if (migParams->compMethods == 1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE &&
        !migParams->params.xbzrleCacheSize_set) {
286 287 288 289 290
        *flags |= VIR_MIGRATE_COMPRESSED;
        return 0;
    }

    for (i = 0; i < QEMU_MIGRATION_COMPRESS_LAST; ++i) {
291
        if ((migParams->compMethods & (1ULL << i)) &&
292 293 294 295 296 297
            virTypedParamsAddString(params, nparams, maxparams,
                                    VIR_MIGRATE_PARAM_COMPRESSION,
                                    qemuMigrationCompressMethodTypeToString(i)) < 0)
            return -1;
    }

298 299 300 301 302 303 304
#define SET(API, PARAM, VAR) \
    do { \
        if (migParams->params.VAR ## _set && \
            API(params, nparams, maxparams, VIR_MIGRATE_PARAM_ ## PARAM, \
                migParams->params.VAR) < 0) \
            return -1; \
    } while (0)
305

306 307 308 309
    SET(virTypedParamsAddInt, COMPRESSION_MT_LEVEL, compressLevel);
    SET(virTypedParamsAddInt, COMPRESSION_MT_THREADS, compressThreads);
    SET(virTypedParamsAddInt, COMPRESSION_MT_DTHREADS, decompressThreads);
    SET(virTypedParamsAddULLong, COMPRESSION_XBZRLE_CACHE, xbzrleCacheSize);
310

311
#undef SET
312 313 314 315 316

    return 0;
}


317 318 319 320 321 322 323 324 325 326 327
/**
 * qemuMigrationParamsApply
 * @driver: qemu driver
 * @vm: domain object
 * @asyncJob: migration job
 * @migParams: migration parameters to send to QEMU
 *
 * Send all parameters stored in @migParams to QEMU.
 *
 * Returns 0 on success, -1 on failure.
 */
328
int
329 330 331 332
qemuMigrationParamsApply(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
333 334
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
335
    bool xbzrleCacheSize_old = false;
336 337 338 339 340
    int ret = -1;

    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
        return -1;

341 342 343 344
    if (qemuMonitorSetMigrationCapabilities(priv->mon, priv->migrationCaps,
                                            migParams->caps) < 0)
        goto cleanup;

345 346 347 348 349 350 351 352 353 354 355 356 357 358
    /* If QEMU is too old to support xbzrle-cache-size migration parameter,
     * we need to set it via migrate-set-cache-size and tell
     * qemuMonitorSetMigrationParams to ignore this parameter.
     */
    if (migParams->params.xbzrleCacheSize_set &&
        (!priv->job.migParams ||
         !priv->job.migParams->params.xbzrleCacheSize_set)) {
        if (qemuMonitorSetMigrationCacheSize(priv->mon,
                                             migParams->params.xbzrleCacheSize) < 0)
            goto cleanup;
        xbzrleCacheSize_old = true;
        migParams->params.xbzrleCacheSize_set = false;
    }

359
    if (qemuMonitorSetMigrationParams(priv->mon, &migParams->params) < 0)
360 361 362 363 364 365 366 367
        goto cleanup;

    ret = 0;

 cleanup:
    if (qemuDomainObjExitMonitor(driver, vm) < 0)
        ret = -1;

368 369 370
    if (xbzrleCacheSize_old)
        migParams->params.xbzrleCacheSize_set = true;

371 372 373 374
    return ret;
}


375
/* qemuMigrationParamsEnableTLS
376 377
 * @driver: pointer to qemu driver
 * @vm: domain object
378 379 380 381
 * @tlsListen: server or client
 * @asyncJob: Migration job to join
 * @tlsAlias: alias to be generated for TLS object
 * @secAlias: alias to be generated for a secinfo object
382
 * @hostname: hostname of the migration destination
383
 * @migParams: migration parameters to set
384
 *
385 386 387
 * Create the TLS objects for the migration and set the migParams value.
 * If QEMU itself does not connect to the destination @hostname must be
 * provided for certificate verification.
388
 *
389
 * Returns 0 on success, -1 on failure
390 391
 */
int
392 393 394 395 396 397
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
                             char **secAlias,
398
                             const char *hostname,
399
                             qemuMigrationParamsPtr migParams)
400 401
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
402 403
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
404 405
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
    int ret = -1;
406 407 408 409

    if (!cfg->migrateTLSx509certdir) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("host migration TLS directory not configured"));
410
        goto error;
411 412
    }

413
    if ((!priv->job.migParams->params.tlsCreds)) {
414 415 416
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
417
        goto error;
418 419 420 421 422 423 424
    }

    /* If there's a secret, then grab/store it now using the connection */
    if (cfg->migrateTLSx509secretUUID &&
        !(priv->migSecinfo =
          qemuDomainSecretInfoTLSNew(priv, QEMU_MIGRATION_TLS_ALIAS_BASE,
                                     cfg->migrateTLSx509secretUUID)))
425
        goto error;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    if (qemuDomainGetTLSObjects(priv->qemuCaps, priv->migSecinfo,
                                cfg->migrateTLSx509certdir, tlsListen,
                                cfg->migrateTLSx509verify,
                                QEMU_MIGRATION_TLS_ALIAS_BASE,
                                &tlsProps, tlsAlias, &secProps, secAlias) < 0)
        goto error;

    /* Ensure the domain doesn't already have the TLS objects defined...
     * This should prevent any issues just in case some cleanup wasn't
     * properly completed (both src and dst use the same alias) or
     * some other error path between now and perform . */
    qemuDomainDelTLSObjects(driver, vm, asyncJob, *secAlias, *tlsAlias);

    if (qemuDomainAddTLSObjects(driver, vm, asyncJob, *secAlias, &secProps,
                                *tlsAlias, &tlsProps) < 0)
        goto error;

444 445
    if (VIR_STRDUP(migParams->params.tlsCreds, *tlsAlias) < 0 ||
        VIR_STRDUP(migParams->params.tlsHostname, hostname ? hostname : "") < 0)
446 447
        goto error;

448 449 450 451 452
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
453 454 455 456

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
457
    goto cleanup;
458 459 460
}


461
/* qemuMigrationParamsDisableTLS
462 463 464 465 466 467 468 469 470 471
 * @vm: domain object
 * @migParams: Pointer to a migration parameters block
 *
 * If we support setting the tls-creds, then set both tls-creds and
 * tls-hostname to the empty string ("") which indicates to not use
 * TLS on this migration.
 *
 * Returns 0 on success, -1 on failure
 */
int
472 473
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
474
{
475
    qemuDomainObjPrivatePtr priv = vm->privateData;
476

477
    if (!priv->job.migParams->params.tlsCreds)
478
        return 0;
479

480 481
    if (VIR_STRDUP(migParams->params.tlsCreds, "") < 0 ||
        VIR_STRDUP(migParams->params.tlsHostname, "") < 0)
482
        return -1;
483 484 485 486 487 488 489 490 491 492 493 494 495

    return 0;
}


/* qemuMigrationParamsResetTLS
 * @driver: pointer to qemu driver
 * @vm: domain object
 * @asyncJob: migration job to join
 *
 * Deconstruct all the setup possibly done for TLS - delete the TLS and
 * security objects, free the secinfo, and reset the migration params to "".
 */
496
static void
497 498
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
499 500
                            int asyncJob,
                            qemuMigrationParamsPtr origParams)
501 502 503 504
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

505 506 507
    /* If QEMU does not support TLS migration we didn't set the aliases. */
    if (!origParams->params.tlsCreds)
        return;
508

509 510 511 512 513 514 515
    /* NB: If either or both fail to allocate memory we can still proceed
     *     since the next time we migrate another deletion attempt will be
     *     made after successfully generating the aliases. */
    tlsAlias = qemuAliasTLSObjFromSrcAlias(QEMU_MIGRATION_TLS_ALIAS_BASE);
    secAlias = qemuDomainGetSecretAESAlias(QEMU_MIGRATION_TLS_ALIAS_BASE, false);

    qemuDomainDelTLSObjects(driver, vm, asyncJob, secAlias, tlsAlias);
516
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
517 518 519 520 521 522

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


523 524 525 526 527
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
528 529
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
530 531 532 533
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
534 535
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
536 537 538
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    qemuMigrationParamsPtr origParams = NULL;
539
    qemuMonitorMigrationCaps cap;
540 541
    qemuMigrationParty party;
    size_t i;
542 543
    int ret = -1;

544 545 546 547 548
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

549 550 551 552 553 554 555 556 557 558 559 560 561
    for (cap = 0; cap < QEMU_MONITOR_MIGRATION_CAPS_LAST; cap++) {
        bool state = false;

        ignore_value(virBitmapGetBit(migParams->caps, cap, &state));

        if (state && !qemuMigrationCapsGet(vm, cap)) {
            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
                           _("Migration option '%s' is not supported by QEMU binary"),
                           qemuMonitorMigrationCapsTypeToString(cap));
            return -1;
        }
    }

562 563 564 565 566 567 568 569 570 571 572
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        cap = qemuMigrationParamsAlwaysOn[i].cap;

        if (qemuMigrationParamsAlwaysOn[i].party & party &&
            qemuMigrationCapsGet(vm, cap)) {
            VIR_DEBUG("Enabling migration capability '%s'",
                      qemuMonitorMigrationCapsTypeToString(cap));
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }

573 574 575 576 577 578
    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
        return -1;

    if (!(origParams = qemuMigrationParamsNew()))
        goto cleanup;

579 580 581 582 583
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    if (qemuMonitorGetMigrationParams(priv->mon, &origParams->params) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    if (qemuDomainObjExitMonitor(driver, vm) < 0)
        ret = -1;

    if (ret == 0)
        VIR_STEAL_PTR(priv->job.migParams, origParams);
    qemuMigrationParamsFree(origParams);

    return ret;
}


601 602 603 604 605 606 607 608 609
/*
 * qemuMigrationParamsReset:
 *
 * Reset all migration parameters so that the next job which internally uses
 * migration (save, managedsave, snapshots, dump) will not try to use them.
 */
void
qemuMigrationParamsReset(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
610 611
                         int asyncJob,
                         qemuMigrationParamsPtr origParams)
612 613 614
{
    virErrorPtr err = virSaveLastError();

615 616
    VIR_DEBUG("Resetting migration parameters %p", origParams);

617
    if (!virDomainObjIsActive(vm) || !origParams)
618 619
        goto cleanup;

620 621
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
622

623
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams);
624 625 626 627 628 629 630

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
631 632 633 634 635 636 637 638


int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
639
    virBitmapPtr migEvent = NULL;
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    char **caps = NULL;
    char **capStr;
    int ret = -1;
    int rc;

    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
        return -1;

    rc = qemuMonitorGetMigrationCapabilities(priv->mon, &caps);

    if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
        goto cleanup;

    if (!caps) {
        ret = 0;
        goto cleanup;
    }

    priv->migrationCaps = virBitmapNew(QEMU_MONITOR_MIGRATION_CAPS_LAST);
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
        int cap = qemuMonitorMigrationCapsTypeFromString(*capStr);

        if (cap < 0) {
            VIR_DEBUG("Unknown migration capability: '%s'", *capStr);
        } else {
            ignore_value(virBitmapSetBit(priv->migrationCaps, cap));
            VIR_DEBUG("Found migration capability: '%s'", *capStr);
        }
    }

    if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATION_EVENT)) {
674 675 676 677 678 679
        migEvent = virBitmapNew(QEMU_MONITOR_MIGRATION_CAPS_LAST);
        if (!migEvent)
            goto cleanup;

        ignore_value(virBitmapSetBit(migEvent, QEMU_MONITOR_MIGRATION_CAPS_EVENTS));

680 681 682
        if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
            goto cleanup;

683
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, migEvent, migEvent);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

        if (qemuDomainObjExitMonitor(driver, vm) < 0)
            goto cleanup;

        if (rc < 0) {
            virResetLastError();
            VIR_DEBUG("Cannot enable migration events; clearing capability");
            virQEMUCapsClear(priv->qemuCaps, QEMU_CAPS_MIGRATION_EVENT);
        }
    }

    /* Migration events capability must always be enabled, clearing it from
     * migration capabilities bitmap makes sure it won't be touched anywhere
     * else.
     */
    ignore_value(virBitmapClearBit(priv->migrationCaps,
                                   QEMU_MONITOR_MIGRATION_CAPS_EVENTS));

    ret = 0;

 cleanup:
    virStringListFree(caps);
    return ret;
}
J
Jiri Denemark 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721


bool
qemuMigrationCapsGet(virDomainObjPtr vm,
                     qemuMonitorMigrationCaps cap)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    bool enabled = false;

    if (priv->migrationCaps)
        ignore_value(virBitmapGetBit(priv->migrationCaps, cap, &enabled));

    return enabled;
}