qemu_migration_params.c 40.2 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
    virJSONValueFree(params);
774
    virJSONValueFree(caps);
775

776 777 778 779
    return ret;
}


780 781 782 783 784 785 786 787 788
/**
 * qemuMigrationParamsSetString:
 * @migrParams: migration parameter object
 * @param: parameter to set
 * @value: new value
 *
 * Enables and sets the migration parameter @param in @migrParams. Returns 0 on
 * success and -1 on error. Libvirt error is reported.
 */
789 790 791 792 793 794 795 796 797 798 799
static 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;

800 801
    migParams->params[param].set = true;

802 803 804 805
    return 0;
}


806
/* qemuMigrationParamsEnableTLS
807 808
 * @driver: pointer to qemu driver
 * @vm: domain object
809 810 811
 * @tlsListen: server or client
 * @asyncJob: Migration job to join
 * @tlsAlias: alias to be generated for TLS object
812
 * @hostname: hostname of the migration destination
813
 * @migParams: migration parameters to set
814
 *
815 816 817
 * 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.
818
 *
819
 * Returns 0 on success, -1 on failure
820 821
 */
int
822 823 824 825 826
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
827
                             const char *hostname,
828
                             qemuMigrationParamsPtr migParams)
829 830
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
831 832
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
833
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
834
    const char *secAlias = NULL;
835
    int ret = -1;
836 837 838 839

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

843
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set) {
844 845 846
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
847
        goto error;
848 849 850
    }

    /* If there's a secret, then grab/store it now using the connection */
851 852 853 854 855 856 857
    if (cfg->migrateTLSx509secretUUID) {
        if (!(priv->migSecinfo =
              qemuDomainSecretInfoTLSNew(priv, QEMU_MIGRATION_TLS_ALIAS_BASE,
                                         cfg->migrateTLSx509secretUUID)))
            goto error;
        secAlias = priv->migSecinfo->s.aes.alias;
    }
858

859 860 861
    if (!(*tlsAlias = qemuAliasTLSObjFromSrcAlias(QEMU_MIGRATION_TLS_ALIAS_BASE)))
        goto error;

862 863 864
    if (qemuDomainGetTLSObjects(priv->qemuCaps, priv->migSecinfo,
                                cfg->migrateTLSx509certdir, tlsListen,
                                cfg->migrateTLSx509verify,
865
                                *tlsAlias, &tlsProps, &secProps) < 0)
866 867 868 869 870 871
        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 . */
872
    qemuDomainDelTLSObjects(driver, vm, asyncJob, secAlias, *tlsAlias);
873

874
    if (qemuDomainAddTLSObjects(driver, vm, asyncJob, &secProps, &tlsProps) < 0)
875 876
        goto error;

877 878 879 880 881 882
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS,
                                     *tlsAlias) < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME,
                                     hostname ? hostname : "") < 0)
883 884
        goto error;

885 886 887 888 889
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
890 891 892 893

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
894
    goto cleanup;
895 896 897
}


898
/* qemuMigrationParamsDisableTLS
899 900 901 902 903 904 905 906 907 908
 * @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
909 910
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
911
{
912
    qemuDomainObjPrivatePtr priv = vm->privateData;
913

914
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
915
        return 0;
916

917 918 919 920
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS, "") < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME, "") < 0)
921
        return -1;
922 923 924 925 926 927 928 929 930

    return 0;
}


/* qemuMigrationParamsResetTLS
 * @driver: pointer to qemu driver
 * @vm: domain object
 * @asyncJob: migration job to join
931
 * @apiFlags: API flags used to start the migration
932 933 934 935
 *
 * Deconstruct all the setup possibly done for TLS - delete the TLS and
 * security objects, free the secinfo, and reset the migration params to "".
 */
936
static void
937 938
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
939
                            int asyncJob,
940 941
                            qemuMigrationParamsPtr origParams,
                            unsigned long apiFlags)
942 943 944 945
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

946 947 948 949
    /* There's nothing to do if QEMU does not support TLS migration or we were
     * not asked to enable it. */
    if (!origParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set ||
        !(apiFlags & VIR_MIGRATE_TLS))
950
        return;
951

952 953 954 955 956 957 958
    /* 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);
959
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
960 961 962 963 964 965

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


966 967 968 969 970 971 972
int
qemuMigrationParamsFetch(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr *migParams)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
973
    virJSONValuePtr jsonParams = NULL;
974 975 976 977 978 979 980 981
    int ret = -1;
    int rc;

    *migParams = NULL;

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

982
    rc = qemuMonitorGetMigrationParams(priv->mon, &jsonParams);
983 984 985 986

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

987 988 989
    if (!(*migParams = qemuMigrationParamsFromJSON(jsonParams)))
        goto cleanup;

990 991 992
    ret = 0;

 cleanup:
993
    virJSONValueFree(jsonParams);
994 995 996 997
    return ret;
}


998
/**
999 1000
 * Returns -1 on error,
 *          0 on success,
1001 1002 1003
 *          1 if the parameter is not supported by QEMU.
 */
int
1004 1005 1006
qemuMigrationParamsGetULL(qemuMigrationParamsPtr migParams,
                          qemuMigrationParam param,
                          unsigned long long *value)
1007
{
1008 1009 1010 1011
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

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

1014
    *value = migParams->params[param].value.ull;
1015 1016 1017 1018
    return 0;
}


1019 1020 1021 1022 1023
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
1024 1025
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
1026 1027 1028 1029
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
1030
                         int asyncJob,
1031 1032
                         qemuMigrationParamsPtr migParams,
                         virBitmapPtr remoteCaps)
1033 1034
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1035
    qemuMigrationCapability cap;
1036 1037
    qemuMigrationParty party;
    size_t i;
1038

1039 1040 1041 1042 1043
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

1044
    for (cap = 0; cap < QEMU_MIGRATION_CAP_LAST; cap++) {
1045 1046 1047 1048 1049 1050 1051
        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"),
1052
                           qemuMigrationCapabilityTypeToString(cap));
1053 1054 1055 1056
            return -1;
        }
    }

1057 1058 1059 1060 1061
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        cap = qemuMigrationParamsAlwaysOn[i].cap;

        if (qemuMigrationParamsAlwaysOn[i].party & party &&
            qemuMigrationCapsGet(vm, cap)) {
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
            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;
                }
            }

1077
            VIR_DEBUG("Enabling migration capability '%s'",
1078
                      qemuMigrationCapabilityTypeToString(cap));
1079 1080 1081 1082
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }

1083 1084 1085 1086 1087
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

1088
    return qemuMigrationParamsFetch(driver, vm, asyncJob, &priv->job.migParams);
1089 1090 1091
}


1092 1093 1094 1095 1096 1097 1098 1099 1100
/*
 * 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,
1101
                         int asyncJob,
1102 1103
                         qemuMigrationParamsPtr origParams,
                         unsigned long apiFlags)
1104 1105 1106
{
    virErrorPtr err = virSaveLastError();

1107 1108
    VIR_DEBUG("Resetting migration parameters %p, flags 0x%lx",
              origParams, apiFlags);
1109

1110
    if (!virDomainObjIsActive(vm) || !origParams)
1111 1112
        goto cleanup;

1113 1114
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
1115

1116
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams, apiFlags);
1117 1118 1119 1120 1121 1122 1123

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
1124 1125


1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
void
qemuMigrationParamsFormat(virBufferPtr buf,
                          qemuMigrationParamsPtr migParams)
{
    qemuMigrationParamValuePtr pv;
    size_t i;

    virBufferAddLit(buf, "<migParams>\n");
    virBufferAdjustIndent(buf, 2);

    for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
        pv = &migParams->params[i];

        if (!pv->set)
            continue;

        virBufferAsprintf(buf, "<param name='%s' ",
                          qemuMigrationParamTypeToString(i));

        switch (qemuMigrationParamTypes[i]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            virBufferAsprintf(buf, "value='%d'", pv->value.i);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            virBufferAsprintf(buf, "value='%llu'", pv->value.ull);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
            virBufferAsprintf(buf, "value='%s'", pv->value.b ? "yes" : "no");
            break;

        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            virBufferEscapeString(buf, "value='%s'", pv->value.s);
            break;
        }

        virBufferAddLit(buf, "/>\n");
    }

    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</migParams>\n");
}


int
qemuMigrationParamsParse(xmlXPathContextPtr ctxt,
                         qemuMigrationParamsPtr *migParams)
{
    qemuMigrationParamsPtr params = NULL;
    qemuMigrationParamValuePtr pv;
    xmlNodePtr *nodes = NULL;
    char *name = NULL;
    char *value = NULL;
    int param;
    size_t i;
    int rc;
    int n;
    int ret = -1;

    *migParams = NULL;

    if ((rc = virXPathBoolean("boolean(./migParams)", ctxt)) < 0)
        goto cleanup;

    if (rc == 0) {
        ret = 0;
        goto cleanup;
    }

    if ((n = virXPathNodeSet("./migParams[1]/param", ctxt, &nodes)) < 0)
        return -1;

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

    for (i = 0; i < n; i++) {
        if (!(name = virXMLPropString(nodes[i], "name"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("missing migration parameter name"));
            goto cleanup;
        }

        if ((param = qemuMigrationParamTypeFromString(name)) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown migration parameter '%s'"), name);
            goto cleanup;
        }
        pv = &params->params[param];

        if (!(value = virXMLPropString(nodes[i], "value"))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing value for migration parameter '%s'"),
                           name);
            goto cleanup;
        }

        rc = 0;
        switch (qemuMigrationParamTypes[param]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            rc = virStrToLong_i(value, NULL, 10, &pv->value.i);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            rc = virStrToLong_ullp(value, NULL, 10, &pv->value.ull);
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
            if (STREQ(value, "yes"))
                pv->value.b = true;
            else if (STREQ(value, "no"))
                pv->value.b = false;
            else
                rc = -1;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            VIR_STEAL_PTR(pv->value.s, value);
            break;
        }

        if (rc < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("invalid value '%s' for migration parameter '%s'"),
                           value, name);
            goto cleanup;
        }

        pv->set = true;
        VIR_FREE(name);
        VIR_FREE(value);
    }

    VIR_STEAL_PTR(*migParams, params);
    ret = 0;

 cleanup:
    qemuMigrationParamsFree(params);
    VIR_FREE(nodes);
    VIR_FREE(name);
    VIR_FREE(value);
    return ret;
}


1271 1272 1273 1274 1275 1276
int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1277
    virBitmapPtr migEvent = NULL;
1278
    virJSONValuePtr json = NULL;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    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;
    }

1297
    priv->migrationCaps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1298 1299 1300 1301
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
1302
        int cap = qemuMigrationCapabilityTypeFromString(*capStr);
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

        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)) {
1313
        migEvent = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1314 1315 1316
        if (!migEvent)
            goto cleanup;

1317
        ignore_value(virBitmapSetBit(migEvent, QEMU_MIGRATION_CAP_EVENTS));
1318

1319 1320 1321
        if (!(json = qemuMigrationCapsToJSON(migEvent, migEvent)))
            goto cleanup;

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

1325 1326
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, json);
        json = NULL;
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

        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,
1343
                                   QEMU_MIGRATION_CAP_EVENTS));
1344 1345 1346 1347

    ret = 0;

 cleanup:
1348
    virBitmapFree(migEvent);
1349
    virJSONValueFree(json);
1350 1351 1352
    virStringListFree(caps);
    return ret;
}
J
Jiri Denemark 已提交
1353 1354 1355 1356


bool
qemuMigrationCapsGet(virDomainObjPtr vm,
1357
                     qemuMigrationCapability cap)
J
Jiri Denemark 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    bool enabled = false;

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

    return enabled;
}