qemu_migration_params.c 17.1 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
    virBitmapPtr caps;
43 44 45
    qemuMonitorMigrationParams params;
};

46

47
qemuMigrationParamsPtr
48 49
qemuMigrationParamsNew(void)
{
50
    qemuMigrationParamsPtr params;
51 52 53 54

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

55 56 57 58
    params->caps = virBitmapNew(QEMU_MONITOR_MIGRATION_CAPS_LAST);
    if (!params->caps)
        goto error;

59
    return params;
60 61 62 63

 error:
    qemuMigrationParamsFree(params);
    return NULL;
64 65 66
}


67
void
68
qemuMigrationParamsFree(qemuMigrationParamsPtr migParams)
69 70 71 72
{
    if (!migParams)
        return;

73
    virBitmapFree(migParams->caps);
74 75
    VIR_FREE(migParams->params.tlsCreds);
    VIR_FREE(migParams->params.tlsHostname);
76
    VIR_FREE(migParams);
77 78 79
}


80
qemuMigrationParamsPtr
81 82
qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                             int nparams,
83 84
                             unsigned long flags,
                             qemuMigrationParty party)
85
{
86
    qemuMigrationParamsPtr migParams;
87

88
    if (!(migParams = qemuMigrationParamsNew()))
89 90 91 92 93 94 95 96 97 98
        return NULL;

    if (!params)
        return migParams;

#define GET(PARAM, VAR) \
    do { \
        int rc; \
        if ((rc = virTypedParamsGetInt(params, nparams, \
                                       VIR_MIGRATE_PARAM_ ## PARAM, \
99
                                       &migParams->params.VAR)) < 0) \
100 101 102
            goto error; \
 \
        if (rc == 1) \
103
            migParams->params.VAR ## _set = true; \
104 105
    } while (0)

106 107 108 109
    if (party == QEMU_MIGRATION_SOURCE) {
        GET(AUTO_CONVERGE_INITIAL, cpuThrottleInitial);
        GET(AUTO_CONVERGE_INCREMENT, cpuThrottleIncrement);
    }
110 111 112

#undef GET

113 114
    if ((migParams->params.cpuThrottleInitial_set ||
         migParams->params.cpuThrottleIncrement_set) &&
115 116 117 118 119 120 121 122 123
        !(flags & VIR_MIGRATE_AUTO_CONVERGE)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn auto convergence on to tune it"));
        goto error;
    }

    return migParams;

 error:
124
    qemuMigrationParamsFree(migParams);
125 126 127 128
    return NULL;
}


129 130 131 132 133 134 135 136 137 138 139
/**
 * 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.
 */
140
int
141 142 143 144
qemuMigrationParamsApply(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
145 146
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
147
    bool xbzrleCacheSize_old = false;
148 149 150 151 152
    int ret = -1;

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

153 154 155 156
    if (qemuMonitorSetMigrationCapabilities(priv->mon, priv->migrationCaps,
                                            migParams->caps) < 0)
        goto cleanup;

157 158 159 160 161 162 163 164 165 166 167 168 169 170
    /* 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;
    }

171
    if (qemuMonitorSetMigrationParams(priv->mon, &migParams->params) < 0)
172 173 174 175 176 177 178 179
        goto cleanup;

    ret = 0;

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

180 181 182
    if (xbzrleCacheSize_old)
        migParams->params.xbzrleCacheSize_set = true;

183 184 185 186
    return ret;
}


187
int
188
qemuMigrationParamsSetCapability(virDomainObjPtr vm ATTRIBUTE_UNUSED,
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 216 217 218
                                 qemuMonitorMigrationCaps capability,
                                 bool state,
                                 qemuMigrationParamsPtr migParams)
{
    if (state)
        ignore_value(virBitmapSetBit(migParams->caps, capability));
    else
        ignore_value(virBitmapClearBit(migParams->caps, capability));

    return 0;
}


int
qemuMigrationParamsSetPostCopy(virDomainObjPtr vm,
                               bool state,
                               qemuMigrationParamsPtr migParams)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;

    if (qemuMigrationParamsSetCapability(vm,
                                         QEMU_MONITOR_MIGRATION_CAPS_POSTCOPY,
                                         state, migParams) < 0)
        return -1;

    priv->job.postcopyEnabled = state;
    return 0;
}


219
/* qemuMigrationParamsEnableTLS
220 221
 * @driver: pointer to qemu driver
 * @vm: domain object
222 223 224 225
 * @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
226
 * @hostname: hostname of the migration destination
227
 * @migParams: migration parameters to set
228
 *
229 230 231
 * 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.
232
 *
233
 * Returns 0 on success, -1 on failure
234 235
 */
int
236 237 238 239 240 241
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
                             char **secAlias,
242
                             const char *hostname,
243
                             qemuMigrationParamsPtr migParams)
244 245
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
246 247
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
248 249
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
    int ret = -1;
250 251 252 253

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

257
    if ((!priv->job.migParams->params.tlsCreds)) {
258 259 260
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
261
        goto error;
262 263 264 265 266 267 268
    }

    /* 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)))
269
        goto error;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

    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;

288 289
    if (VIR_STRDUP(migParams->params.tlsCreds, *tlsAlias) < 0 ||
        VIR_STRDUP(migParams->params.tlsHostname, hostname ? hostname : "") < 0)
290 291
        goto error;

292 293 294 295 296
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
297 298 299 300

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
301
    goto cleanup;
302 303 304
}


305
/* qemuMigrationParamsDisableTLS
306 307 308 309 310 311 312 313 314 315
 * @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
316 317
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
318
{
319
    qemuDomainObjPrivatePtr priv = vm->privateData;
320

321
    if (!priv->job.migParams->params.tlsCreds)
322
        return 0;
323

324 325
    if (VIR_STRDUP(migParams->params.tlsCreds, "") < 0 ||
        VIR_STRDUP(migParams->params.tlsHostname, "") < 0)
326
        return -1;
327 328 329 330 331 332

    return 0;
}


int
333
qemuMigrationParamsSetCompression(virDomainObjPtr vm,
334
                                  qemuMigrationCompressionPtr compression,
335
                                  qemuMigrationParamsPtr migParams)
336
{
337 338 339 340 341
    if (qemuMigrationParamsSetCapability(vm,
                                         QEMU_MONITOR_MIGRATION_CAPS_XBZRLE,
                                         compression->methods &
                                         (1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE),
                                         migParams) < 0)
342 343
        return -1;

344 345 346 347 348
    if (qemuMigrationParamsSetCapability(vm,
                                         QEMU_MONITOR_MIGRATION_CAPS_COMPRESS,
                                         compression->methods &
                                         (1ULL << QEMU_MIGRATION_COMPRESS_MT),
                                         migParams) < 0)
349 350
        return -1;

351 352
    migParams->params.compressLevel_set = compression->level_set;
    migParams->params.compressLevel = compression->level;
353

354 355
    migParams->params.compressThreads_set = compression->threads_set;
    migParams->params.compressThreads = compression->threads;
356

357 358
    migParams->params.decompressThreads_set = compression->dthreads_set;
    migParams->params.decompressThreads = compression->dthreads;
359

360 361
    migParams->params.xbzrleCacheSize_set = compression->xbzrle_cache_set;
    migParams->params.xbzrleCacheSize = compression->xbzrle_cache;
362

363
    return 0;
364 365 366 367 368 369 370 371 372 373 374
}


/* 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 "".
 */
375
static void
376 377
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
378 379
                            int asyncJob,
                            qemuMigrationParamsPtr origParams)
380 381 382 383
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

384 385 386
    /* If QEMU does not support TLS migration we didn't set the aliases. */
    if (!origParams->params.tlsCreds)
        return;
387

388 389 390 391 392 393 394
    /* 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);
395
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
396 397 398 399 400 401

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


402 403 404 405 406
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
407 408
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
409 410 411 412
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
413 414
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
415 416 417
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    qemuMigrationParamsPtr origParams = NULL;
418
    qemuMonitorMigrationCaps cap;
419 420
    int ret = -1;

421 422 423 424 425 426 427 428 429 430 431 432 433
    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;
        }
    }

434 435 436 437 438 439
    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
        return -1;

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

440 441 442 443 444
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    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;
}


462 463 464 465 466 467 468 469 470
/*
 * 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,
471 472
                         int asyncJob,
                         qemuMigrationParamsPtr origParams)
473 474 475
{
    virErrorPtr err = virSaveLastError();

476 477
    VIR_DEBUG("Resetting migration parameters %p", origParams);

478
    if (!virDomainObjIsActive(vm) || !origParams)
479 480
        goto cleanup;

481 482
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
483

484
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams);
485 486 487 488 489 490 491

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
492 493 494 495 496 497 498 499


int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
500
    virBitmapPtr migEvent = NULL;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    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)) {
535 536 537 538 539 540
        migEvent = virBitmapNew(QEMU_MONITOR_MIGRATION_CAPS_LAST);
        if (!migEvent)
            goto cleanup;

        ignore_value(virBitmapSetBit(migEvent, QEMU_MONITOR_MIGRATION_CAPS_EVENTS));

541 542 543
        if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
            goto cleanup;

544
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, migEvent, migEvent);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

        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 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582


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;
}