qemu_migration_params.c 43.4 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 77 78 79
VIR_ENUM_IMPL(qemuMigrationCompressMethod, QEMU_MIGRATION_COMPRESS_LAST,
              "xbzrle",
              "mt",
);

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


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

110 111
typedef struct _qemuMigrationParamsAlwaysOnItem qemuMigrationParamsAlwaysOnItem;
struct _qemuMigrationParamsAlwaysOnItem {
112
    qemuMigrationCapability cap;
113 114 115
    int party; /* bit-wise OR of qemuMigrationParty */
};

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

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

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

    {QEMU_MIGRATION_CAP_LATE_BLOCK_ACTIVATE,
     QEMU_MIGRATION_DESTINATION},
141 142
};

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

    {VIR_MIGRATE_AUTO_CONVERGE,
150
     QEMU_MIGRATION_CAP_AUTO_CONVERGE,
151 152 153
     QEMU_MIGRATION_SOURCE},

    {VIR_MIGRATE_POSTCOPY,
154
     QEMU_MIGRATION_CAP_POSTCOPY,
155
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
156 157 158 159

    {VIR_MIGRATE_PARALLEL,
     QEMU_MIGRATION_CAP_MULTIFD,
     QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
160 161
};

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

169 170 171
    {.typedParam = VIR_MIGRATE_PARAM_AUTO_CONVERGE_INCREMENT,
     .param = QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT,
     .party = QEMU_MIGRATION_SOURCE},
172

173 174 175
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_LEVEL,
     .param = QEMU_MIGRATION_PARAM_COMPRESS_LEVEL,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
176

177 178 179
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_THREADS,
     .param = QEMU_MIGRATION_PARAM_COMPRESS_THREADS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
180

181 182 183
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_MT_DTHREADS,
     .param = QEMU_MIGRATION_PARAM_DECOMPRESS_THREADS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
184

185 186 187
    {.typedParam = VIR_MIGRATE_PARAM_COMPRESSION_XBZRLE_CACHE,
     .param = QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
188 189 190 191 192

    {.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},
193 194 195 196

    {.typedParam = VIR_MIGRATE_PARAM_PARALLEL_CONNECTIONS,
     .param = QEMU_MIGRATION_PARAM_MULTIFD_CHANNELS,
     .party = QEMU_MIGRATION_SOURCE | QEMU_MIGRATION_DESTINATION},
197 198
};

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

216

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


237
qemuMigrationParamsPtr
238 239
qemuMigrationParamsNew(void)
{
240
    qemuMigrationParamsPtr params;
241 242 243 244

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

245
    params->caps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
246 247 248
    if (!params->caps)
        goto error;

249
    return params;
250 251 252 253

 error:
    qemuMigrationParamsFree(params);
    return NULL;
254 255 256
}


257
void
258
qemuMigrationParamsFree(qemuMigrationParamsPtr migParams)
259
{
260 261
    size_t i;

262 263 264
    if (!migParams)
        return;

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

270
    virBitmapFree(migParams->caps);
271
    VIR_FREE(migParams);
272 273 274
}


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

310 311 312 313 314 315 316 317 318 319 320
    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;
    }

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


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

337 338 339 340 341 342
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_INT) < 0)
        return -1;

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

343 344 345 346 347
    value = migParams->params[param].value.i;
    if (unit > 0)
        value /= unit;

    return virTypedParamsAddInt(params, nparams, maxparams, name, value);
348 349 350 351 352 353 354 355
}


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

371 372 373 374 375 376 377 378 379 380 381
    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;
    }

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


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

398 399 400 401 402 403
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

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

404 405 406 407 408
    value = migParams->params[param].value.ull;
    if (unit > 0)
        value /= unit;

    return virTypedParamsAddULLong(params, nparams, maxparams, name, value);
409
}
410

411

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

422 423 424
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, VIR_MIGRATE_PARAM_COMPRESSION))
            continue;
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439
        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;
        }
440

441
        migParams->compMethods |= 1ULL << method;
442

443 444
        switch ((qemuMigrationCompressMethod) method) {
        case QEMU_MIGRATION_COMPRESS_XBZRLE:
445
            cap = QEMU_MIGRATION_CAP_XBZRLE;
446
            break;
447

448
        case QEMU_MIGRATION_COMPRESS_MT:
449
            cap = QEMU_MIGRATION_CAP_COMPRESS;
450
            break;
451

452 453 454 455 456 457 458
        case QEMU_MIGRATION_COMPRESS_LAST:
        default:
            continue;
        }
        ignore_value(virBitmapSetBit(migParams->caps, cap));
    }

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

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

    return 0;
482 483 484

 error:
    return -1;
485 486 487
}


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

497
    if (!(migParams = qemuMigrationParamsNew()))
498 499
        return NULL;

500
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsFlagMap); i++) {
501
        qemuMigrationCapability cap = qemuMigrationParamsFlagMap[i].cap;
502 503 504 505

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

511 512
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
513

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

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

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
539
    }
540

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

549 550 551 552 553 554 555
    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;
    }

556
    if (qemuMigrationParamsSetCompression(params, nparams, flags, migParams) < 0)
557 558
        goto error;

559 560 561
    return migParams;

 error:
562
    qemuMigrationParamsFree(migParams);
563 564 565 566
    return NULL;
}


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

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

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

590 591
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsTPMap); i++) {
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
592

593 594
        if (!(item->party & QEMU_MIGRATION_DESTINATION))
            continue;
595

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

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

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
            break;
        }
    }
616 617 618 619 620

    return 0;
}


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

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

    if (!params)
        return migParams;

636 637 638 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
    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;
        }
    }
665 666 667 668 669 670 671 672 673

    return migParams;

 error:
    qemuMigrationParamsFree(migParams);
    return NULL;
}


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

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

686 687 688 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
    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;
    }
715 716 717 718 719 720 721 722 723

    return params;

 error:
    virJSONValueFree(params);
    return NULL;
}


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

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

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

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


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

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

799 800 801 802 803
    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"));
804
            goto cleanup;
805 806 807 808 809 810 811 812 813 814 815
        }
    } 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;
        }
816 817
    }

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

832
    if (!(params = qemuMigrationParamsToJSON(migParams)))
833 834
        goto cleanup;

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

842 843 844 845 846 847
    ret = 0;

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

848
    if (xbzrleCacheSize_old)
849
        migParams->params[xbzrle].set = true;
850

851
    virJSONValueFree(params);
852
    virJSONValueFree(caps);
853

854 855 856 857
    return ret;
}


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

878 879
    migParams->params[param].set = true;

880 881 882 883
    return 0;
}


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

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

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

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

937 938 939
    if (!(*tlsAlias = qemuAliasTLSObjFromSrcAlias(QEMU_MIGRATION_TLS_ALIAS_BASE)))
        goto error;

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

952
    if (qemuDomainAddTLSObjects(driver, vm, asyncJob, &secProps, &tlsProps) < 0)
953 954
        goto error;

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

963 964 965 966 967
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
968 969 970 971

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
972
    goto cleanup;
973 974 975
}


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

992
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
993
        return 0;
994

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

    return 0;
}


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

1024 1025 1026 1027
    /* 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))
1028
        return;
1029

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

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


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

    *migParams = NULL;

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

1060
    rc = qemuMonitorGetMigrationParams(priv->mon, &jsonParams);
1061 1062 1063 1064

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

1065 1066 1067
    if (!(*migParams = qemuMigrationParamsFromJSON(jsonParams)))
        goto cleanup;

1068 1069 1070
    ret = 0;

 cleanup:
1071
    virJSONValueFree(jsonParams);
1072 1073 1074 1075
    return ret;
}


1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
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;
}


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

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

1106
    *value = migParams->params[param].value.ull;
1107 1108 1109 1110
    return 0;
}


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

1131 1132 1133 1134 1135
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

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

1149 1150 1151 1152 1153
    for (i = 0; i < ARRAY_CARDINALITY(qemuMigrationParamsAlwaysOn); i++) {
        cap = qemuMigrationParamsAlwaysOn[i].cap;

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

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

1175 1176 1177 1178 1179
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

1180
    return qemuMigrationParamsFetch(driver, vm, asyncJob, &priv->job.migParams);
1181 1182 1183
}


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

1199 1200
    VIR_DEBUG("Resetting migration parameters %p, flags 0x%lx",
              origParams, apiFlags);
1201

1202
    if (!virDomainObjIsActive(vm) || !origParams)
1203 1204
        goto cleanup;

1205 1206
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
1207

1208
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams, apiFlags);
1209 1210 1211 1212 1213 1214 1215

 cleanup:
    if (err) {
        virSetError(err);
        virFreeError(err);
    }
}
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 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
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;
}


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

1389
    priv->migrationCaps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1390 1391 1392 1393
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
1394
        int cap = qemuMigrationCapabilityTypeFromString(*capStr);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404

        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)) {
1405
        migEvent = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1406 1407 1408
        if (!migEvent)
            goto cleanup;

1409
        ignore_value(virBitmapSetBit(migEvent, QEMU_MIGRATION_CAP_EVENTS));
1410

1411 1412 1413
        if (!(json = qemuMigrationCapsToJSON(migEvent, migEvent)))
            goto cleanup;

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

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

        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,
1435
                                   QEMU_MIGRATION_CAP_EVENTS));
1436 1437 1438 1439

    ret = 0;

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


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

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

    return enabled;
}