qemu_migration_params.c 43.5 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
#define LIBVIRT_QEMU_MIGRATION_PARAMSPRIV_H_ALLOW
34
#include "qemu_migration_paramspriv.h"
35
#include "qemu_monitor.h"
36 37 38 39 40 41 42

#define VIR_FROM_THIS VIR_FROM_QEMU

VIR_LOG_INIT("qemu.qemu_migration_params");

#define QEMU_MIGRATION_TLS_ALIAS_BASE "libvirt_migrate"

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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;
60 61
};

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

68 69 70 71 72 73
typedef enum {
    QEMU_MIGRATION_COMPRESS_XBZRLE = 0,
    QEMU_MIGRATION_COMPRESS_MT,

    QEMU_MIGRATION_COMPRESS_LAST
} qemuMigrationCompressMethod;
74
VIR_ENUM_DECL(qemuMigrationCompressMethod);
75 76
VIR_ENUM_IMPL(qemuMigrationCompressMethod,
              QEMU_MIGRATION_COMPRESS_LAST,
77 78 79 80
              "xbzrle",
              "mt",
);

81 82
VIR_ENUM_IMPL(qemuMigrationCapability,
              QEMU_MIGRATION_CAP_LAST,
83 84 85 86 87 88 89
              "xbzrle",
              "auto-converge",
              "rdma-pin-all",
              "events",
              "postcopy-ram",
              "compress",
              "pause-before-switchover",
90
              "late-block-activate",
91
              "multifd",
92 93 94
);


95
VIR_ENUM_DECL(qemuMigrationParam);
96 97
VIR_ENUM_IMPL(qemuMigrationParam,
              QEMU_MIGRATION_PARAM_LAST,
98 99 100 101 102 103 104 105 106 107 108
              "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",
109
              "max-postcopy-bandwidth",
110
              "multifd-channels",
111
);
112

113 114
typedef struct _qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOnItem;
struct _qemuMigrationParamsAlwaysOnItem {
115
    qemuMigrationCapability cap;
116 117 118
    int party; /* bit-wise OR of qemuMigrationParty */
};

119 120 121
typedef struct _qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMapItem;
struct _qemuMigrationParamsFlagMapItem {
    virDomainMigrateFlags flag;
122
    qemuMigrationCapability cap;
123 124 125
    int party; /* bit-wise OR of qemuMigrationParty */
};

126 127 128
typedef struct _qemuMigrationParamsTPMapItem qemuMigrationParamsTPMapItem;
struct _qemuMigrationParamsTPMapItem {
    const char *typedParam;
129
    unsigned int unit;
130 131 132 133
    qemuMigrationParam param;
    int party; /* bit-wise OR of qemuMigrationParty */
};

134
/* Migration capabilities which should always be enabled as long as they
135 136 137
 * 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.
 */
138
static const qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOn[] = {
139
    {QEMU_MIGRATION_CAP_PAUSE_BEFORE_SWITCHOVER,
140
     QEMU_MIGRATION_SOURCE},
141 142 143

    {QEMU_MIGRATION_CAP_LATE_BLOCK_ACTIVATE,
     QEMU_MIGRATION_DESTINATION},
144 145
};

146
/* Translation from virDomainMigrateFlags to qemuMigrationCapability. */
147 148
static const qemuMigrationParamsFlagMapItem qemuMigrationParamsFlagMap[] = {
    {VIR_MIGRATE_RDMA_PIN_ALL,
149
     QEMU_MIGRATION_CAP_RDMA_PIN_ALL,
150 151 152
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},

    {VIR_MIGRATE_AUTO_CONVERGE,
153
     QEMU_MIGRATION_CAP_AUTO_CONVERGE,
154 155 156
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_POSTCOPY,
157
     QEMU_MIGRATION_CAP_POSTCOPY,
158
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
159 160 161 162

    {VIR_MIGRATE_PARALLEL,
     QEMU_MIGRATION_CAP_MULTIFD,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
163 164
};

165 166 167
/* Translation from VIR_MIGRATE_PARAM_* typed parameters to
 * qemuMigrationParams. */
static const qemuMigrationParamsTPMapItem qemuMigrationParamsTPMap[] = {
168 169 170
    {.typedParam = VIR_MIGRATE_PARAM_AUTO_CONVERGE_INITIAL,
     .param = QEMU_MIGRATION_PARAM_THROTTLE_INITIAL,
     .party = QEMU_MIGRATION_SOURCE},
171

172 173 174
    {.typedParam = VIR_MIGRATE_PARAM_AUTO_CONVERGE_INCREMENT,
     .param = QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT,
     .party = QEMU_MIGRATION_SOURCE},
175

176 177 178
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_LEVEL,
     .param = QEMU_MIGRATION_PARAM_COMPRESS_LEVEL,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
179

180 181 182
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_THREADS,
     .param = QEMU_MIGRATION_PARAM_COMPRESS_THREADS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
183

184 185 186
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_DTHREADS,
     .param = QEMU_MIGRATION_PARAM_DECOMPRESS_THREADS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
187

188 189 190
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_XBZRLE_CACHE,
     .param = QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
191 192 193 194 195

    {.typedParam = VIR_MIGRATE_PARAM_BANDWIDTH_POSTCOPY,
     .unit = 1024 * 1024, /* MiB/s */
     .param = QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
196 197 198 199

    {.typedParam = VIR_MIGRATE_PARAM_PARALLEL_CONNECTIONS,
     .param = QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
200 201
};

202 203 204 205 206 207 208 209 210 211 212 213
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,
214
    [QEMU_MIGRATION_PARAM_MAX_POSTCOPY_BANDWIDTH] = QEMU_MIGRATION_PARAM_TYPE_ULL,
215
    [QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS] = QEMU_MIGRATION_PARAM_TYPE_INT,
216 217 218
};
verify(ARRAY_CARDINALITY(qemuMigrationParamTypes) == QEMU_MIGRATION_PARAM_LAST);

219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
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;
}


240
qemuMigrationParamsPtr
241 242
qemuMigrationParamsNew(void)
{
243
    qemuMigrationParamsPtr params;
244 245 246 247

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

248
    params->caps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
249 250 251
    if (!params->caps)
        goto error;

252
    return params;
253 254 255 256

 error:
    qemuMigrationParamsFree(params);
    return NULL;
257 258 259
}


260
void
261
qemuMigrationParamsFree(qemuMigrationParamsPtr migParams)
262
{
263 264
    size_t i;

265 266 267
    if (!migParams)
        return;

268 269 270 271 272
    for (i = 0; i < QEMU_MIGRATION_PARAM_LAST; i++) {
        if (qemuMigrationParamTypes[i] == QEMU_MIGRATION_PARAM_TYPE_STRING)
            VIR_FREE(migParams->params[i].value.s);
    }

273
    virBitmapFree(migParams->caps);
274
    VIR_FREE(migParams);
275 276 277
}


278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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,
298 299
                            const char *name,
                            unsigned int unit)
300 301 302 303 304 305 306 307 308 309 310 311 312
{
    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;

313 314 315 316 317 318 319 320 321 322 323
    if (unit > 0) {
        unsigned int max = UINT_MAX / unit;
        if (migParams->params[param].value.i > max) {
            virReportError(VIR_ERR_OVERFLOW,
                           _("migration parameter '%s' must be less than %u"),
                           name, max + 1);
            return -1;
        }
        migParams->params[param].value.i *= unit;
    }

324 325 326 327 328 329 330 331 332 333 334
    migParams->params[param].set = !!rc;
    return 0;
}


static int
qemuMigrationParamsSetTPInt(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr *params,
                            int *nparams,
                            int *maxparams,
335 336
                            const char *name,
                            unsigned int unit)
337
{
338 339
    int value;

340 341 342 343 344 345
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_INT) < 0)
        return -1;

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

346 347 348 349 350
    value = migParams->params[param].value.i;
    if (unit > 0)
        value /= unit;

    return virTypedParamsAddInt(params, nparams, maxparams, name, value);
351 352 353 354 355 356 357 358
}


static int
qemuMigrationParamsGetTPULL(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr params,
                            int nparams,
359 360
                            const char *name,
                            unsigned int unit)
361 362 363 364 365 366 367 368 369 370 371 372 373
{
    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;

374 375 376 377 378 379 380 381 382 383 384
    if (unit > 0) {
        unsigned long long max = ULLONG_MAX / unit;
        if (migParams->params[param].value.ull > max) {
            virReportError(VIR_ERR_OVERFLOW,
                           _("migration parameter '%s' must be less than %llu"),
                           name, max + 1);
            return -1;
        }
        migParams->params[param].value.ull *= unit;
    }

385 386 387 388 389 390 391 392 393 394 395
    migParams->params[param].set = !!rc;
    return 0;
}


static int
qemuMigrationParamsSetTPULL(qemuMigrationParamsPtr migParams,
                            qemuMigrationParam param,
                            virTypedParameterPtr *params,
                            int *nparams,
                            int *maxparams,
396 397
                            const char *name,
                            unsigned int unit)
398
{
399 400
    unsigned long long value;

401 402 403 404 405 406
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

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

407 408 409 410 411
    value = migParams->params[param].value.ull;
    if (unit > 0)
        value /= unit;

    return virTypedParamsAddULLong(params, nparams, maxparams, name, value);
412
}
413

414

415
static int
416 417 418
qemuMigrationParamsSetCompression(virTypedParameterPtr params,
                                  int nparams,
                                  unsigned long flags,
419 420
                                  qemuMigrationParamsPtr migParams)
{
421 422
    size_t i;
    int method;
423
    qemuMigrationCapability cap;
424

425 426 427
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, VIR_MIGRATE_PARAM_COMPRESSION))
            continue;
428

429 430 431 432 433 434 435 436 437 438 439 440 441 442
        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;
        }
443

444
        migParams->compMethods |= 1ULL << method;
445

446 447
        switch ((qemuMigrationCompressMethod) method) {
        case QEMU_MIGRATION_COMPRESS_XBZRLE:
448
            cap = QEMU_MIGRATION_CAP_XBZRLE;
449
            break;
450

451
        case QEMU_MIGRATION_COMPRESS_MT:
452
            cap = QEMU_MIGRATION_CAP_COMPRESS;
453
            break;
454

455 456 457 458 459 460 461
        case QEMU_MIGRATION_COMPRESS_LAST:
        default:
            continue;
        }
        ignore_value(virBitmapSetBit(migParams->caps, cap));
    }

462 463 464
    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) &&
465 466 467 468 469 470
        !(migParams->compMethods & (1ULL << QEMU_MIGRATION_COMPRESS_MT))) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn multithread compression on to tune it"));
        goto error;
    }

471
    if (migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set &&
472 473 474 475 476 477 478 479 480
        !(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,
481
                                     QEMU_MIGRATION_CAP_XBZRLE));
482
    }
483 484

    return 0;
485 486 487

 error:
    return -1;
488 489 490
}


491
qemuMigrationParamsPtr
492 493
qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                             int nparams,
494
                             unsigned long flags,
495
                             qemuMigrationParty party)
496
{
497
    qemuMigrationParamsPtr migParams;
498
    size_t i;
499

500
    if (!(migParams = qemuMigrationParamsNew()))
501 502
        return NULL;

503
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsFlagMap); i++) {
504
        qemuMigrationCapability cap = qemuMigrationParamsFlagMap[i].cap;
505 506 507 508

        if (qemuMigrationParamsFlagMap[i].party & party &&
            flags & qemuMigrationParamsFlagMap[i].flag) {
            VIR_DEBUG("Enabling migration capability '%s'",
509
                      qemuMigrationCapabilityTypeToString(cap));
510 511 512
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }
513

514 515
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
516

517 518 519 520 521 522 523 524 525
        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,
526 527
                                            nparams, item->typedParam,
                                            item->unit) < 0)
528 529 530 531 532
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsGetTPULL(migParams, item->param, params,
533 534
                                            nparams, item->typedParam,
                                            item->unit) < 0)
535 536 537 538 539 540 541
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
542
    }
543

544 545
    if ((migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INITIAL].set ||
         migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT].set) &&
546 547 548 549 550 551
        !(flags & VIR_MIGRATE_AUTO_CONVERGE)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn auto convergence on to tune it"));
        goto error;
    }

552 553 554 555 556 557 558
    if (migParams->params[QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS].set &&
        !(flags & VIR_MIGRATE_PARALLEL)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn parallel migration on to tune it"));
        goto error;
    }

559
    if (qemuMigrationParamsSetCompression(params, nparams, flags, migParams) < 0)
560 561
        goto error;

562 563 564
    return migParams;

 error:
565
    qemuMigrationParamsFree(migParams);
566 567 568 569
    return NULL;
}


570
int
571 572 573 574 575
qemuMigrationParamsDump(qemuMigrationParamsPtr migParams,
                        virTypedParameterPtr *params,
                        int *nparams,
                        int *maxparams,
                        unsigned long *flags)
576 577 578
{
    size_t i;

579
    if (migParams->compMethods == 1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE &&
580
        !migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set) {
581 582 583 584 585
        *flags |= VIR_MIGRATE_COMPRESSED;
        return 0;
    }

    for (i = 0; i < QEMU_MIGRATION_COMPRESS_LAST; ++i) {
586
        if ((migParams->compMethods & (1ULL << i)) &&
587 588 589 590 591 592
            virTypedParamsAddString(params, nparams, maxparams,
                                    VIR_MIGRATE_PARAM_COMPRESSION,
                                    qemuMigrationCompressMethodTypeToString(i)) < 0)
            return -1;
    }

593 594
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
595

596 597
        if (!(item->party & QEMU_MIGRATION_DESTINATION))
            continue;
598

599 600 601 602
        switch (qemuMigrationParamTypes[item->param]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            if (qemuMigrationParamsSetTPInt(migParams, item->param,
                                            params, nparams, maxparams,
603
                                            item->typedParam, item->unit) < 0)
604 605
                return -1;
            break;
606

607 608 609
        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsSetTPULL(migParams, item->param,
                                            params, nparams, maxparams,
610
                                            item->typedParam, item->unit) < 0)
611 612 613 614 615 616 617 618
                return -1;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
    }
619 620 621 622 623

    return 0;
}


624
qemuMigrationParamsPtr
625 626
qemuMigrationParamsFromJSON(virJSONValuePtr params)
{
627 628 629 630 631
    qemuMigrationParamsPtr migParams;
    qemuMigrationParamValuePtr pv;
    const char *name;
    const char *str;
    size_t i;
632 633 634 635 636 637 638

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

    if (!params)
        return migParams;

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    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;
        }
    }
668 669 670 671 672 673 674 675 676

    return migParams;

 error:
    qemuMigrationParamsFree(migParams);
    return NULL;
}


677
virJSONValuePtr
678 679 680
qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
{
    virJSONValuePtr params = NULL;
681 682 683 684
    qemuMigrationParamValuePtr pv;
    const char *name;
    size_t i;
    int rc;
685 686 687 688

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

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    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;
    }
718 719 720 721 722 723 724 725 726

    return params;

 error:
    virJSONValueFree(params);
    return NULL;
}


727 728 729 730 731 732
virJSONValuePtr
qemuMigrationCapsToJSON(virBitmapPtr caps,
                        virBitmapPtr states)
{
    virJSONValuePtr json = NULL;
    virJSONValuePtr cap = NULL;
733
    qemuMigrationCapability bit;
734 735 736 737 738
    const char *name;

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

739
    for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) {
740 741 742 743 744 745 746 747 748 749 750 751
        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;

752
        name = qemuMigrationCapabilityTypeToString(bit);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
        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;
}


774 775 776 777 778 779 780 781 782 783 784
/**
 * 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.
 */
785
int
786 787 788 789
qemuMigrationParamsApply(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
790 791
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
792
    bool xbzrleCacheSize_old = false;
793
    virJSONValuePtr params = NULL;
794
    virJSONValuePtr caps = NULL;
795
    qemuMigrationParam xbzrle = QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE;
796
    int ret = -1;
797
    int rc;
798 799 800 801

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

802 803 804 805 806
    if (asyncJob == QEMU_ASYNC_JOB_NONE) {
        if (!virBitmapIsAllClear(migParams->caps)) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Migration capabilities can only be set by "
                             "a migration job"));
807
            goto cleanup;
808 809 810 811 812 813 814 815 816 817 818
        }
    } else {
        if (!(caps = qemuMigrationCapsToJSON(priv->migrationCaps, migParams->caps)))
            goto cleanup;

        if (virJSONValueArraySize(caps) > 0) {
            rc = qemuMonitorSetMigrationCapabilities(priv->mon, caps);
            caps = NULL;
            if (rc < 0)
                goto cleanup;
        }
819 820
    }

821 822 823 824
    /* 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.
     */
825
    if (migParams->params[xbzrle].set &&
826
        (!priv->job.migParams ||
827
         !priv->job.migParams->params[xbzrle].set)) {
828
        if (qemuMonitorSetMigrationCacheSize(priv->mon,
829
                                             migParams->params[xbzrle].value.ull) < 0)
830 831
            goto cleanup;
        xbzrleCacheSize_old = true;
832
        migParams->params[xbzrle].set = false;
833 834
    }

835
    if (!(params = qemuMigrationParamsToJSON(migParams)))
836 837
        goto cleanup;

838 839 840 841 842 843 844
    if (virJSONValueObjectKeysNumber(params) > 0) {
        rc = qemuMonitorSetMigrationParams(priv->mon, params);
        params = NULL;
        if (rc < 0)
            goto cleanup;
    }

845 846 847 848 849 850
    ret = 0;

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

851
    if (xbzrleCacheSize_old)
852
        migParams->params[xbzrle].set = true;
853

854
    virJSONValueFree(params);
855
    virJSONValueFree(caps);
856

857 858 859 860
    return ret;
}


861 862 863 864 865 866 867 868 869
/**
 * 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.
 */
870 871 872 873 874 875 876 877 878 879 880
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;

881 882
    migParams->params[param].set = true;

883 884 885 886
    return 0;
}


887
/* qemuMigrationParamsEnableTLS
888 889
 * @driver: pointer to qemu driver
 * @vm: domain object
890 891 892
 * @tlsListen: server or client
 * @asyncJob: Migration job to join
 * @tlsAlias: alias to be generated for TLS object
893
 * @hostname: hostname of the migration destination
894
 * @migParams: migration parameters to set
895
 *
896 897 898
 * 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.
899
 *
900
 * Returns 0 on success, -1 on failure
901 902
 */
int
903 904 905 906 907
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
908
                             const char *hostname,
909
                             qemuMigrationParamsPtr migParams)
910 911
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
912 913
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
914
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
915
    const char *secAlias = NULL;
916
    int ret = -1;
917 918 919 920

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

924
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set) {
925 926 927
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
928
        goto error;
929 930 931
    }

    /* If there's a secret, then grab/store it now using the connection */
932 933 934 935 936 937 938
    if (cfg->migrateTLSx509secretUUID) {
        if (!(priv->migSecinfo =
              qemuDomainSecretInfoTLSNew(priv, QEMU_MIGRATION_TLS_ALIAS_BASE,
                                         cfg->migrateTLSx509secretUUID)))
            goto error;
        secAlias = priv->migSecinfo->s.aes.alias;
    }
939

940 941 942
    if (!(*tlsAlias = qemuAliasTLSObjFromSrcAlias(QEMU_MIGRATION_TLS_ALIAS_BASE)))
        goto error;

943 944 945
    if (qemuDomainGetTLSObjects(priv->qemuCaps, priv->migSecinfo,
                                cfg->migrateTLSx509certdir, tlsListen,
                                cfg->migrateTLSx509verify,
946
                                *tlsAlias, &tlsProps, &secProps) < 0)
947 948 949 950 951 952
        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 . */
953
    qemuDomainDelTLSObjects(driver, vm, asyncJob, secAlias, *tlsAlias);
954

955
    if (qemuDomainAddTLSObjects(driver, vm, asyncJob, &secProps, &tlsProps) < 0)
956 957
        goto error;

958 959 960 961 962
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS,
                                     *tlsAlias) < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME,
J
Ján Tomko 已提交
963
                                     NULLSTR_EMPTY(hostname)) < 0)
964 965
        goto error;

966 967 968 969 970
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
971 972 973 974

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
975
    goto cleanup;
976 977 978
}


979
/* qemuMigrationParamsDisableTLS
980 981 982 983 984 985 986 987 988 989
 * @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
990 991
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
992
{
993
    qemuDomainObjPrivatePtr priv = vm->privateData;
994

995
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
996
        return 0;
997

998 999 1000 1001
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS, "") < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME, "") < 0)
1002
        return -1;
1003 1004 1005 1006 1007 1008 1009 1010 1011

    return 0;
}


/* qemuMigrationParamsResetTLS
 * @driver: pointer to qemu driver
 * @vm: domain object
 * @asyncJob: migration job to join
1012
 * @apiFlags: API flags used to start the migration
1013 1014 1015 1016
 *
 * Deconstruct all the setup possibly done for TLS - delete the TLS and
 * security objects, free the secinfo, and reset the migration params to "".
 */
1017
static void
1018 1019
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
1020
                            int asyncJob,
1021 1022
                            qemuMigrationParamsPtr origParams,
                            unsigned long apiFlags)
1023 1024 1025 1026
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

1027 1028 1029 1030
    /* 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))
1031
        return;
1032

1033 1034 1035 1036 1037 1038 1039
    /* 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);
1040
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
1041 1042 1043 1044 1045 1046

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


1047 1048 1049 1050 1051 1052 1053
int
qemuMigrationParamsFetch(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr *migParams)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1054
    virJSONValuePtr jsonParams = NULL;
1055 1056 1057 1058 1059 1060 1061 1062
    int ret = -1;
    int rc;

    *migParams = NULL;

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

1063
    rc = qemuMonitorGetMigrationParams(priv->mon, &jsonParams);
1064 1065 1066 1067

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

1068 1069 1070
    if (!(*migParams = qemuMigrationParamsFromJSON(jsonParams)))
        goto cleanup;

1071 1072 1073
    ret = 0;

 cleanup:
1074
    virJSONValueFree(jsonParams);
1075 1076 1077 1078
    return ret;
}


1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
int
qemuMigrationParamsSetULL(qemuMigrationParamsPtr migParams,
                          qemuMigrationParam param,
                          unsigned long long value)
{
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

    migParams->params[param].value.ull = value;
    migParams->params[param].set = true;
    return 0;
}


1093
/**
1094 1095
 * Returns -1 on error,
 *          0 on success,
1096 1097 1098
 *          1 if the parameter is not supported by QEMU.
 */
int
1099 1100 1101
qemuMigrationParamsGetULL(qemuMigrationParamsPtr migParams,
                          qemuMigrationParam param,
                          unsigned long long *value)
1102
{
1103 1104 1105 1106
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

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

1109
    *value = migParams->params[param].value.ull;
1110 1111 1112 1113
    return 0;
}


1114 1115 1116 1117 1118
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
1119 1120
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
1121 1122 1123 1124
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
1125
                         int asyncJob,
1126 1127
                         qemuMigrationParamsPtr migParams,
                         virBitmapPtr remoteCaps)
1128 1129
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1130
    qemuMigrationCapability cap;
1131 1132
    qemuMigrationParty party;
    size_t i;
1133

1134 1135 1136 1137 1138
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

1139
    for (cap = 0; cap < QEMU_MIGRATION_CAP_LAST; cap++) {
1140 1141 1142 1143 1144 1145 1146
        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"),
1147
                           qemuMigrationCapabilityTypeToString(cap));
1148 1149 1150 1151
            return -1;
        }
    }

1152 1153 1154 1155 1156
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        cap = qemuMigrationParamsAlwaysOn[i].cap;

        if (qemuMigrationParamsAlwaysOn[i].party & party &&
            qemuMigrationCapsGet(vm, cap)) {
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
            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;
                }
            }

1172
            VIR_DEBUG("Enabling migration capability '%s'",
1173
                      qemuMigrationCapabilityTypeToString(cap));
1174 1175 1176 1177
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }

1178 1179 1180 1181 1182
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

1183
    return qemuMigrationParamsFetch(driver, vm, asyncJob, &priv->job.migParams);
1184 1185 1186
}


1187 1188 1189 1190 1191 1192 1193 1194 1195
/*
 * 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,
1196
                         int asyncJob,
1197 1198
                         qemuMigrationParamsPtr origParams,
                         unsigned long apiFlags)
1199 1200 1201
{
    virErrorPtr err = virSaveLastError();

1202 1203
    VIR_DEBUG("Resetting migration parameters %p, flags 0x%lx",
              origParams, apiFlags);
1204

1205
    if (!virDomainObjIsActive(vm) || !origParams)
1206 1207
        goto cleanup;

1208 1209
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
1210

1211
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams, apiFlags);
1212 1213 1214 1215 1216 1217 1218

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
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 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
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;
}


1366 1367 1368 1369 1370 1371
int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1372
    virBitmapPtr migEvent = NULL;
1373
    virJSONValuePtr json = NULL;
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    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;
    }

1392
    priv->migrationCaps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1393 1394 1395 1396
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
1397
        int cap = qemuMigrationCapabilityTypeFromString(*capStr);
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407

        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)) {
1408
        migEvent = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1409 1410 1411
        if (!migEvent)
            goto cleanup;

1412
        ignore_value(virBitmapSetBit(migEvent, QEMU_MIGRATION_CAP_EVENTS));
1413

1414 1415 1416
        if (!(json = qemuMigrationCapsToJSON(migEvent, migEvent)))
            goto cleanup;

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

1420 1421
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, json);
        json = NULL;
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

        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,
1438
                                   QEMU_MIGRATION_CAP_EVENTS));
1439 1440 1441 1442

    ret = 0;

 cleanup:
1443
    virBitmapFree(migEvent);
1444
    virJSONValueFree(json);
1445 1446 1447
    virStringListFree(caps);
    return ret;
}
J
Jiri Denemark 已提交
1448 1449 1450 1451


bool
qemuMigrationCapsGet(virDomainObjPtr vm,
1452
                     qemuMigrationCapability cap)
J
Jiri Denemark 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    bool enabled = false;

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

    return enabled;
}