qemu_migration_params.c 35.8 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_migration_paramspriv.h"
34
#include "qemu_monitor.h"
35 36 37 38 39 40 41

#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_migration_params");

#define QEMU_MIGRATION_TLS_ALIAS_BASE "libvirt_migrate"

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
typedef enum {
    QEMU_MIGRATION_PARAM_TYPE_INT,
    QEMU_MIGRATION_PARAM_TYPE_ULL,
    QEMU_MIGRATION_PARAM_TYPE_BOOL,
    QEMU_MIGRATION_PARAM_TYPE_STRING,
} qemuMigrationParamType;

typedef struct _qemuMigrationParamValue qemuMigrationParamValue;
typedef qemuMigrationParamValue *qemuMigrationParamValuePtr;
struct _qemuMigrationParamValue {
    bool set;
    union {
        int i; /* exempt from syntax-check */
        unsigned long long ull;
        bool b;
        char *s;
    } value;
59 60
};

61
struct _qemuMigrationParams {
62
    unsigned long long compMethods; /* bit-wise OR of qemuMigrationCompressMethod */
63
    virBitmapPtr caps;
64
    qemuMigrationParamValue params[QEMU_MIGRATION_PARAM_LAST];
65 66
};

67 68 69 70 71 72 73 74 75 76 77 78
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",
);

79 80 81 82 83 84 85 86 87 88 89
VIR_ENUM_IMPL(qemuMigrationCapability, QEMU_MIGRATION_CAP_LAST,
              "xbzrle",
              "auto-converge",
              "rdma-pin-all",
              "events",
              "postcopy-ram",
              "compress",
              "pause-before-switchover",
);


90 91 92 93 94 95 96 97 98 99 100 101 102 103
VIR_ENUM_DECL(qemuMigrationParam)
VIR_ENUM_IMPL(qemuMigrationParam, QEMU_MIGRATION_PARAM_LAST,
              "compress-level",
              "compress-threads",
              "decompress-threads",
              "cpu-throttle-initial",
              "cpu-throttle-increment",
              "tls-creds",
              "tls-hostname",
              "max-bandwidth",
              "downtime-limit",
              "block-incremental",
              "xbzrle-cache-size",
);
104

105 106
typedef struct _qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOnItem;
struct _qemuMigrationParamsAlwaysOnItem {
107
    qemuMigrationCapability cap;
108 109 110
    int party; /* bit-wise OR of qemuMigrationParty */
};

111 112 113
typedef struct _qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMapItem;
struct _qemuMigrationParamsFlagMapItem {
    virDomainMigrateFlags flag;
114
    qemuMigrationCapability cap;
115 116 117
    int party; /* bit-wise OR of qemuMigrationParty */
};

118 119 120 121 122 123 124
typedef struct _qemuMigrationParamsTPMapItem qemuMigrationParamsTPMapItem;
struct _qemuMigrationParamsTPMapItem {
    const char *typedParam;
    qemuMigrationParam param;
    int party; /* bit-wise OR of qemuMigrationParty */
};

125
/* Migration capabilities which should always be enabled as long as they
126 127 128
 * are supported by QEMU. If the capability is supposed to be enabled on both
 * sides of migration, it won't be enabled unless both sides support it.
 */
129
static const qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOn[] = {
130
    {QEMU_MIGRATION_CAP_PAUSE_BEFORE_SWITCHOVER,
131 132 133
     QEMU_MIGRATION_SOURCE},
};

134
/* Translation from virDomainMigrateFlags to qemuMigrationCapability. */
135 136
static const qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMap[] = {
    {VIR_MIGRATE_RDMA_PIN_ALL,
137
     QEMU_MIGRATION_CAP_RDMA_PIN_ALL,
138 139 140
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_AUTO_CONVERGE,
141
     QEMU_MIGRATION_CAP_AUTO_CONVERGE,
142 143 144
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_POSTCOPY,
145
     QEMU_MIGRATION_CAP_POSTCOPY,
146 147 148
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
};

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* Translation from VIR_MIGRATE_PARAM_* typed parameters to
 * qemuMigrationParams. */
static const qemuMigrationParamsTPMapItem qemuMigrationParamsTPMap[] = {
    {VIR_MIGRATE_PARAM_AUTO_CONVERGE_INITIAL,
     QEMU_MIGRATION_PARAM_THROTTLE_INITIAL,
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_PARAM_AUTO_CONVERGE_INCREMENT,
     QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT,
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_PARAM_COMPRESSION_MT_LEVEL,
     QEMU_MIGRATION_PARAM_COMPRESS_LEVEL,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_PARAM_COMPRESSION_MT_THREADS,
     QEMU_MIGRATION_PARAM_COMPRESS_THREADS,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_PARAM_COMPRESSION_MT_DTHREADS,
     QEMU_MIGRATION_PARAM_DECOMPRESS_THREADS,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_PARAM_COMPRESSION_XBZRLE_CACHE,
     QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
};

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static const qemuMigrationParamType qemuMigrationParamTypes[] = {
    [QEMU_MIGRATION_PARAM_COMPRESS_LEVEL] = QEMU_MIGRATION_PARAM_TYPE_INT,
    [QEMU_MIGRATION_PARAM_COMPRESS_THREADS] = QEMU_MIGRATION_PARAM_TYPE_INT,
    [QEMU_MIGRATION_PARAM_DECOMPRESS_THREADS] = QEMU_MIGRATION_PARAM_TYPE_INT,
    [QEMU_MIGRATION_PARAM_THROTTLE_INITIAL] = QEMU_MIGRATION_PARAM_TYPE_INT,
    [QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT] = QEMU_MIGRATION_PARAM_TYPE_INT,
    [QEMU_MIGRATION_PARAM_TLS_CREDS] = QEMU_MIGRATION_PARAM_TYPE_STRING,
    [QEMU_MIGRATION_PARAM_TLS_HOSTNAME] = QEMU_MIGRATION_PARAM_TYPE_STRING,
    [QEMU_MIGRATION_PARAM_MAX_BANDWIDTH] = QEMU_MIGRATION_PARAM_TYPE_ULL,
    [QEMU_MIGRATION_PARAM_DOWNTIME_LIMIT] = QEMU_MIGRATION_PARAM_TYPE_ULL,
    [QEMU_MIGRATION_PARAM_BLOCK_INCREMENTAL] = QEMU_MIGRATION_PARAM_TYPE_BOOL,
    [QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE] = QEMU_MIGRATION_PARAM_TYPE_ULL,
};
verify(ARRAY_CARDINALITY(qemuMigrationParamTypes) == QEMU_MIGRATION_PARAM_LAST);

192

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
virBitmapPtr
qemuMigrationParamsGetAlwaysOnCaps(qemuMigrationParty party)
{
    virBitmapPtr caps = NULL;
    size_t i;

    if (!(caps = virBitmapNew(QEMU_MIGRATION_CAP_LAST)))
        return NULL;

    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        if (!(qemuMigrationParamsAlwaysOn[i].party & party))
            continue;

        ignore_value(virBitmapSetBit(caps, qemuMigrationParamsAlwaysOn[i].cap));
    }

    return caps;
}


213
static qemuMigrationParamsPtr
214 215
qemuMigrationParamsNew(void)
{
216
    qemuMigrationParamsPtr params;
217 218 219 220

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

221
    params->caps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
222 223 224
    if (!params->caps)
        goto error;

225
    return params;
226 227 228 229

 error:
    qemuMigrationParamsFree(params);
    return NULL;
230 231 232
}


233
void
234
qemuMigrationParamsFree(qemuMigrationParamsPtr migParams)
235
{
236 237
    size_t i;

238 239 240
    if (!migParams)
        return;

241 242 243 244 245
    for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
        if (qemuMigrationParamTypes[i] == QEMU_MIGRATION_PARAM_TYPE_STRING)
            VIR_FREE(migParams->params[i].value.s);
    }

246
    virBitmapFree(migParams->caps);
247
    VIR_FREE(migParams);
248 249 250
}


251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static int
qemuMigrationParamsCheckType(qemuMigrationParam param,
                             qemuMigrationParamType type)
{
    if (qemuMigrationParamTypes[param] != type) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Type mismatch for '%s' migration parameter"),
                       qemuMigrationParamTypeToString(param));
        return -1;
    }

    return 0;
}


static int
qemuMigrationParamsGetTPInt(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr params,
                            int nparams,
                            const char *name)
{
    int rc;

    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_INT) < 0)
        return -1;

    if (!params)
        return 0;

    if ((rc = virTypedParamsGetInt(params, nparams, name,
                                   &migParams->params[param].value.i)) < 0)
        return -1;

    migParams->params[param].set = !!rc;
    return 0;
}


static int
qemuMigrationParamsSetTPInt(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr *params,
                            int *nparams,
                            int *maxparams,
                            const char *name)
{
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_INT) < 0)
        return -1;

    if (!migParams->params[param].set)
        return 0;

    return virTypedParamsAddInt(params, nparams, maxparams, name,
                                migParams->params[param].value.i);
}


static int
qemuMigrationParamsGetTPULL(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr params,
                            int nparams,
                            const char *name)
{
    int rc;

    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

    if (!params)
        return 0;

    if ((rc = virTypedParamsGetULLong(params, nparams, name,
                                      &migParams->params[param].value.ull)) < 0)
        return -1;

    migParams->params[param].set = !!rc;
    return 0;
}


static int
qemuMigrationParamsSetTPULL(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr *params,
                            int *nparams,
                            int *maxparams,
                            const char *name)
{
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

    if (!migParams->params[param].set)
        return 0;

    return virTypedParamsAddULLong(params, nparams, maxparams, name,
                                   migParams->params[param].value.ull);
}
350

351

352
static int
353 354 355
qemuMigrationParamsSetCompression(virTypedParameterPtr params,
                                  int nparams,
                                  unsigned long flags,
356 357
                                  qemuMigrationParamsPtr migParams)
{
358 359
    size_t i;
    int method;
360
    qemuMigrationCapability cap;
361

362 363 364
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, VIR_MIGRATE_PARAM_COMPRESSION))
            continue;
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379
        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;
        }
380

381
        migParams->compMethods |= 1ULL << method;
382

383 384
        switch ((qemuMigrationCompressMethod) method) {
        case QEMU_MIGRATION_COMPRESS_XBZRLE:
385
            cap = QEMU_MIGRATION_CAP_XBZRLE;
386
            break;
387

388
        case QEMU_MIGRATION_COMPRESS_MT:
389
            cap = QEMU_MIGRATION_CAP_COMPRESS;
390
            break;
391

392 393 394 395 396 397 398
        case QEMU_MIGRATION_COMPRESS_LAST:
        default:
            continue;
        }
        ignore_value(virBitmapSetBit(migParams->caps, cap));
    }

399 400 401
    if ((migParams->params[QEMU_MIGRATION_PARAM_COMPRESS_LEVEL].set ||
         migParams->params[QEMU_MIGRATION_PARAM_COMPRESS_THREADS].set ||
         migParams->params[QEMU_MIGRATION_PARAM_DECOMPRESS_THREADS].set) &&
402 403 404 405 406 407
        !(migParams->compMethods & (1ULL << QEMU_MIGRATION_COMPRESS_MT))) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn multithread compression on to tune it"));
        goto error;
    }

408
    if (migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set &&
409 410 411 412 413 414 415 416 417
        !(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,
418
                                     QEMU_MIGRATION_CAP_XBZRLE));
419
    }
420 421

    return 0;
422 423 424

 error:
    return -1;
425 426 427
}


428
qemuMigrationParamsPtr
429 430
qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                             int nparams,
431
                             unsigned long flags,
432
                             qemuMigrationParty party)
433
{
434
    qemuMigrationParamsPtr migParams;
435
    size_t i;
436

437
    if (!(migParams = qemuMigrationParamsNew()))
438 439
        return NULL;

440
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsFlagMap); i++) {
441
        qemuMigrationCapability cap = qemuMigrationParamsFlagMap[i].cap;
442 443 444 445

        if (qemuMigrationParamsFlagMap[i].party & party &&
            flags & qemuMigrationParamsFlagMap[i].flag) {
            VIR_DEBUG("Enabling migration capability '%s'",
446
                      qemuMigrationCapabilityTypeToString(cap));
447 448 449
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }
450

451 452
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
        if (!(item->party & party))
            continue;

        VIR_DEBUG("Setting migration parameter '%s' from '%s'",
                  qemuMigrationParamTypeToString(item->param), item->typedParam);

        switch (qemuMigrationParamTypes[item->param]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            if (qemuMigrationParamsGetTPInt(migParams, item->param, params,
                                            nparams, item->typedParam) < 0)
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsGetTPULL(migParams, item->param, params,
                                            nparams, item->typedParam) < 0)
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
477
    }
478

479 480
    if ((migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INITIAL].set ||
         migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT].set) &&
481 482 483 484 485 486
        !(flags & VIR_MIGRATE_AUTO_CONVERGE)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn auto convergence on to tune it"));
        goto error;
    }

487
    if (qemuMigrationParamsSetCompression(params, nparams, flags, migParams) < 0)
488 489
        goto error;

490 491 492
    return migParams;

 error:
493
    qemuMigrationParamsFree(migParams);
494 495 496 497
    return NULL;
}


498
int
499 500 501 502 503
qemuMigrationParamsDump(qemuMigrationParamsPtr migParams,
                        virTypedParameterPtr *params,
                        int *nparams,
                        int *maxparams,
                        unsigned long *flags)
504 505 506
{
    size_t i;

507
    if (migParams->compMethods == 1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE &&
508
        !migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set) {
509 510 511 512 513
        *flags |= VIR_MIGRATE_COMPRESSED;
        return 0;
    }

    for (i = 0; i < QEMU_MIGRATION_COMPRESS_LAST; ++i) {
514
        if ((migParams->compMethods & (1ULL << i)) &&
515 516 517 518 519 520
            virTypedParamsAddString(params, nparams, maxparams,
                                    VIR_MIGRATE_PARAM_COMPRESSION,
                                    qemuMigrationCompressMethodTypeToString(i)) < 0)
            return -1;
    }

521 522
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
523

524 525
        if (!(item->party & QEMU_MIGRATION_DESTINATION))
            continue;
526

527 528 529 530 531 532 533
        switch (qemuMigrationParamTypes[item->param]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            if (qemuMigrationParamsSetTPInt(migParams, item->param,
                                            params, nparams, maxparams,
                                            item->typedParam) < 0)
                return -1;
            break;
534

535 536 537 538 539 540 541 542 543 544 545 546
        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsSetTPULL(migParams, item->param,
                                            params, nparams, maxparams,
                                            item->typedParam) < 0)
                return -1;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
    }
547 548 549 550 551

    return 0;
}


552
qemuMigrationParamsPtr
553 554
qemuMigrationParamsFromJSON(virJSONValuePtr params)
{
555 556 557 558 559
    qemuMigrationParamsPtr migParams;
    qemuMigrationParamValuePtr pv;
    const char *name;
    const char *str;
    size_t i;
560 561 562 563 564 565 566

    if (!(migParams = qemuMigrationParamsNew()))
        return NULL;

    if (!params)
        return migParams;

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
        name = qemuMigrationParamTypeToString(i);
        pv = &migParams->params[i];

        switch (qemuMigrationParamTypes[i]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            if (virJSONValueObjectGetNumberInt(params, name, &pv->value.i) == 0)
                pv->set = true;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (virJSONValueObjectGetNumberUlong(params, name, &pv->value.ull) == 0)
                pv->set = true;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
            if (virJSONValueObjectGetBoolean(params, name, &pv->value.b) == 0)
                pv->set = true;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            if ((str = virJSONValueObjectGetString(params, name))) {
                if (VIR_STRDUP(pv->value.s, str) < 0)
                    goto error;
                pv->set = true;
            }
            break;
        }
    }
596 597 598 599 600 601 602 603 604

    return migParams;

 error:
    qemuMigrationParamsFree(migParams);
    return NULL;
}


605
virJSONValuePtr
606 607 608
qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
{
    virJSONValuePtr params = NULL;
609 610 611 612
    qemuMigrationParamValuePtr pv;
    const char *name;
    size_t i;
    int rc;
613 614 615 616

    if (!(params = virJSONValueNewObject()))
        return NULL;

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
        name = qemuMigrationParamTypeToString(i);
        pv = &migParams->params[i];

        if (!pv->set)
            continue;

        rc = 0;
        switch (qemuMigrationParamTypes[i]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            rc = virJSONValueObjectAppendNumberInt(params, name, pv->value.i);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            rc = virJSONValueObjectAppendNumberUlong(params, name, pv->value.ull);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
            rc = virJSONValueObjectAppendBoolean(params, name, pv->value.b);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            rc = virJSONValueObjectAppendString(params, name, pv->value.s);
            break;
        }

        if (rc < 0)
            goto error;
    }
646 647 648 649 650 651 652 653 654

    return params;

 error:
    virJSONValueFree(params);
    return NULL;
}


655 656 657 658 659 660
virJSONValuePtr
qemuMigrationCapsToJSON(virBitmapPtr caps,
                        virBitmapPtr states)
{
    virJSONValuePtr json = NULL;
    virJSONValuePtr cap = NULL;
661
    qemuMigrationCapability bit;
662 663 664 665 666
    const char *name;

    if (!(json = virJSONValueNewArray()))
        return NULL;

667
    for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) {
668 669 670 671 672 673 674 675 676 677 678 679
        bool supported = false;
        bool state = false;

        ignore_value(virBitmapGetBit(caps, bit, &supported));
        if (!supported)
            continue;

        ignore_value(virBitmapGetBit(states, bit, &state));

        if (!(cap = virJSONValueNewObject()))
            goto error;

680
        name = qemuMigrationCapabilityTypeToString(bit);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
        if (virJSONValueObjectAppendString(cap, "capability", name) < 0)
            goto error;

        if (virJSONValueObjectAppendBoolean(cap, "state", state) < 0)
            goto error;

        if (virJSONValueArrayAppend(json, cap) < 0)
            goto error;

        cap = NULL;
    }

    return json;

 error:
    virJSONValueFree(json);
    virJSONValueFree(cap);
    return NULL;
}


702 703 704 705 706 707 708 709 710 711 712
/**
 * 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.
 */
713
int
714 715 716 717
qemuMigrationParamsApply(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
718 719
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
720
    bool xbzrleCacheSize_old = false;
721
    virJSONValuePtr params = NULL;
722
    virJSONValuePtr caps = NULL;
723
    qemuMigrationParam xbzrle = QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE;
724
    int ret = -1;
725
    int rc;
726 727 728 729

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

730
    if (!(caps = qemuMigrationCapsToJSON(priv->migrationCaps, migParams->caps)))
731 732
        goto cleanup;

733 734 735 736 737 738 739
    if (virJSONValueArraySize(caps) > 0) {
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, caps);
        caps = NULL;
        if (rc < 0)
            goto cleanup;
    }

740 741 742 743
    /* 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.
     */
744
    if (migParams->params[xbzrle].set &&
745
        (!priv->job.migParams ||
746
         !priv->job.migParams->params[xbzrle].set)) {
747
        if (qemuMonitorSetMigrationCacheSize(priv->mon,
748
                                             migParams->params[xbzrle].value.ull) < 0)
749 750
            goto cleanup;
        xbzrleCacheSize_old = true;
751
        migParams->params[xbzrle].set = false;
752 753
    }

754
    if (!(params = qemuMigrationParamsToJSON(migParams)))
755 756
        goto cleanup;

757 758 759 760 761 762 763
    if (virJSONValueObjectKeysNumber(params) > 0) {
        rc = qemuMonitorSetMigrationParams(priv->mon, params);
        params = NULL;
        if (rc < 0)
            goto cleanup;
    }

764 765 766 767 768 769
    ret = 0;

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

770
    if (xbzrleCacheSize_old)
771
        migParams->params[xbzrle].set = true;
772

773 774
    virJSONValueFree(params);

775 776 777 778
    return ret;
}


779
/* qemuMigrationParamsEnableTLS
780 781
 * @driver: pointer to qemu driver
 * @vm: domain object
782 783 784 785
 * @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
786
 * @hostname: hostname of the migration destination
787
 * @migParams: migration parameters to set
788
 *
789 790 791
 * 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.
792
 *
793
 * Returns 0 on success, -1 on failure
794 795
 */
int
796 797 798 799 800 801
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
                             char **secAlias,
802
                             const char *hostname,
803
                             qemuMigrationParamsPtr migParams)
804 805
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
806 807
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
808 809
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
    int ret = -1;
810 811 812 813

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

817
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set) {
818 819 820
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
821
        goto error;
822 823 824 825 826 827 828
    }

    /* 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)))
829
        goto error;
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

    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;

848 849 850 851 852 853
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS,
                                     *tlsAlias) < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME,
                                     hostname ? hostname : "") < 0)
854 855
        goto error;

856 857 858 859 860
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
861 862 863 864

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
865
    goto cleanup;
866 867 868
}


869
/* qemuMigrationParamsDisableTLS
870 871 872 873 874 875 876 877 878 879
 * @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
880 881
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
882
{
883
    qemuDomainObjPrivatePtr priv = vm->privateData;
884

885
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
886
        return 0;
887

888 889 890 891
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS, "") < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME, "") < 0)
892
        return -1;
893 894 895 896 897 898 899 900 901 902 903 904 905

    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 "".
 */
906
static void
907 908
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
909 910
                            int asyncJob,
                            qemuMigrationParamsPtr origParams)
911 912 913 914
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

915
    /* If QEMU does not support TLS migration we didn't set the aliases. */
916
    if (!origParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
917
        return;
918

919 920 921 922 923 924 925
    /* 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);
926
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
927 928 929 930 931 932

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


933 934 935 936 937 938 939
int
qemuMigrationParamsFetch(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr *migParams)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
940
    virJSONValuePtr jsonParams = NULL;
941 942 943 944 945 946 947 948
    int ret = -1;
    int rc;

    *migParams = NULL;

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

949
    rc = qemuMonitorGetMigrationParams(priv->mon, &jsonParams);
950 951 952 953

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

954 955 956
    if (!(*migParams = qemuMigrationParamsFromJSON(jsonParams)))
        goto cleanup;

957 958 959
    ret = 0;

 cleanup:
960
    virJSONValueFree(jsonParams);
961 962 963 964
    return ret;
}


965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
int
qemuMigrationParamsSetString(qemuMigrationParamsPtr migParams,
                             qemuMigrationParam param,
                             const char *value)
{
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_STRING) < 0)
        return -1;

    if (VIR_STRDUP(migParams->params[param].value.s, value) < 0)
        return -1;

    return 0;
}


980
/**
981 982
 * Returns -1 on error,
 *          0 on success,
983 984 985
 *          1 if the parameter is not supported by QEMU.
 */
int
986 987 988
qemuMigrationParamsGetULL(qemuMigrationParamsPtr migParams,
                          qemuMigrationParam param,
                          unsigned long long *value)
989
{
990 991 992 993
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

    if (!migParams->params[param].set)
994 995
        return 1;

996
    *value = migParams->params[param].value.ull;
997 998 999 1000
    return 0;
}


1001 1002 1003 1004 1005
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
1006 1007
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
1008 1009 1010 1011
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
1012
                         int asyncJob,
1013 1014
                         qemuMigrationParamsPtr migParams,
                         virBitmapPtr remoteCaps)
1015 1016
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1017
    qemuMigrationCapability cap;
1018 1019
    qemuMigrationParty party;
    size_t i;
1020

1021 1022 1023 1024 1025
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

1026
    for (cap = 0; cap < QEMU_MIGRATION_CAP_LAST; cap++) {
1027 1028 1029 1030 1031 1032 1033
        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"),
1034
                           qemuMigrationCapabilityTypeToString(cap));
1035 1036 1037 1038
            return -1;
        }
    }

1039 1040 1041 1042 1043
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        cap = qemuMigrationParamsAlwaysOn[i].cap;

        if (qemuMigrationParamsAlwaysOn[i].party & party &&
            qemuMigrationCapsGet(vm, cap)) {
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
            if (qemuMigrationParamsAlwaysOn[i].party != party) {
                bool remote = false;

                if (remoteCaps)
                    ignore_value(virBitmapGetBit(remoteCaps, cap, &remote));

                if (!remote) {
                    VIR_DEBUG("Not enabling migration capability '%s'; it is "
                              "not supported or automatically enabled by the "
                              "other side of migration",
                              qemuMigrationCapabilityTypeToString(cap));
                    continue;
                }
            }

1059
            VIR_DEBUG("Enabling migration capability '%s'",
1060
                      qemuMigrationCapabilityTypeToString(cap));
1061 1062 1063 1064
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }

1065 1066 1067 1068 1069
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

1070
    return qemuMigrationParamsFetch(driver, vm, asyncJob, &priv->job.migParams);
1071 1072 1073
}


1074 1075 1076 1077 1078 1079 1080 1081 1082
/*
 * 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,
1083 1084
                         int asyncJob,
                         qemuMigrationParamsPtr origParams)
1085 1086 1087
{
    virErrorPtr err = virSaveLastError();

1088 1089
    VIR_DEBUG("Resetting migration parameters %p", origParams);

1090
    if (!virDomainObjIsActive(vm) || !origParams)
1091 1092
        goto cleanup;

1093 1094
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
1095

1096
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams);
1097 1098 1099 1100 1101 1102 1103

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
1104 1105 1106 1107 1108 1109 1110 1111


int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1112
    virBitmapPtr migEvent = NULL;
1113
    virJSONValuePtr json = NULL;
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    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;
    }

1132
    priv->migrationCaps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1133 1134 1135 1136
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
1137
        int cap = qemuMigrationCapabilityTypeFromString(*capStr);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

        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)) {
1148
        migEvent = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1149 1150 1151
        if (!migEvent)
            goto cleanup;

1152
        ignore_value(virBitmapSetBit(migEvent, QEMU_MIGRATION_CAP_EVENTS));
1153

1154 1155 1156
        if (!(json = qemuMigrationCapsToJSON(migEvent, migEvent)))
            goto cleanup;

1157 1158 1159
        if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
            goto cleanup;

1160 1161
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, json);
        json = NULL;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

        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,
1178
                                   QEMU_MIGRATION_CAP_EVENTS));
1179 1180 1181 1182

    ret = 0;

 cleanup:
1183
    virJSONValueFree(json);
1184 1185 1186
    virStringListFree(caps);
    return ret;
}
J
Jiri Denemark 已提交
1187 1188 1189 1190


bool
qemuMigrationCapsGet(virDomainObjPtr vm,
1191
                     qemuMigrationCapability cap)
J
Jiri Denemark 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    bool enabled = false;

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

    return enabled;
}