qemu_migration_params.c 44.9 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
verify(G_N_ELEMENTS(qemuMigrationParamTypes) == QEMU_MIGRATION_PARAM_LAST);
218

219

220 221 222 223 224 225 226 227 228
virBitmapPtr
qemuMigrationParamsGetAlwaysOnCaps(qemuMigrationParty party)
{
    virBitmapPtr caps = NULL;
    size_t i;

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

229
    for (i = 0; i < G_N_ELEMENTS(qemuMigrationParamsAlwaysOn); i++) {
230 231 232 233 234 235 236 237 238 239
        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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
static int
qemuMigrationParamsGetTPString(qemuMigrationParamsPtr migParams,
                               qemuMigrationParam param,
                               virTypedParameterPtr params,
                               int nparams,
                               const char *name)
{
    const char *value = NULL;
    int rc;

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

    if (!params)
        return 0;

    if ((rc = virTypedParamsGetString(params, nparams, name, &value)) < 0)
        return -1;

    migParams->params[param].value.s = g_strdup(value);
    migParams->params[param].set = !!rc;
    return 0;
}


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

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

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



460
static int
461 462 463
qemuMigrationParamsSetCompression(virTypedParameterPtr params,
                                  int nparams,
                                  unsigned long flags,
464 465
                                  qemuMigrationParamsPtr migParams)
{
466 467
    size_t i;
    int method;
468
    qemuMigrationCapability cap;
469

470 471 472
    for (i = 0; i < nparams; i++) {
        if (STRNEQ(params[i].field, VIR_MIGRATE_PARAM_COMPRESSION))
            continue;
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487
        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;
        }
488

489
        migParams->compMethods |= 1ULL << method;
490

491 492
        switch ((qemuMigrationCompressMethod) method) {
        case QEMU_MIGRATION_COMPRESS_XBZRLE:
493
            cap = QEMU_MIGRATION_CAP_XBZRLE;
494
            break;
495

496
        case QEMU_MIGRATION_COMPRESS_MT:
497
            cap = QEMU_MIGRATION_CAP_COMPRESS;
498
            break;
499

500 501 502 503 504 505 506
        case QEMU_MIGRATION_COMPRESS_LAST:
        default:
            continue;
        }
        ignore_value(virBitmapSetBit(migParams->caps, cap));
    }

507 508 509
    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) &&
510 511 512 513 514 515
        !(migParams->compMethods & (1ULL << QEMU_MIGRATION_COMPRESS_MT))) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn multithread compression on to tune it"));
        goto error;
    }

516
    if (migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set &&
517 518 519 520 521 522 523 524 525
        !(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,
526
                                     QEMU_MIGRATION_CAP_XBZRLE));
527
    }
528 529

    return 0;
530 531 532

 error:
    return -1;
533 534 535
}


536
qemuMigrationParamsPtr
537 538
qemuMigrationParamsFromFlags(virTypedParameterPtr params,
                             int nparams,
539
                             unsigned long flags,
540
                             qemuMigrationParty party)
541
{
542
    qemuMigrationParamsPtr migParams;
543
    size_t i;
544

545
    if (!(migParams = qemuMigrationParamsNew()))
546 547
        return NULL;

548
    for (i = 0; i < G_N_ELEMENTS(qemuMigrationParamsFlagMap); i++) {
549
        qemuMigrationCapability cap = qemuMigrationParamsFlagMap[i].cap;
550 551 552 553

        if (qemuMigrationParamsFlagMap[i].party & party &&
            flags & qemuMigrationParamsFlagMap[i].flag) {
            VIR_DEBUG("Enabling migration capability '%s'",
554
                      qemuMigrationCapabilityTypeToString(cap));
555 556 557
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }
558

559
    for (i = 0; i < G_N_ELEMENTS(qemuMigrationParamsTPMap); i++) {
560
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
561

562 563 564 565 566 567 568 569 570
        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,
571 572
                                            nparams, item->typedParam,
                                            item->unit) < 0)
573 574 575 576 577
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsGetTPULL(migParams, item->param, params,
578 579
                                            nparams, item->typedParam,
                                            item->unit) < 0)
580 581 582 583
                goto error;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
584 585
            break;

586
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
587 588 589
            if (qemuMigrationParamsGetTPString(migParams, item->param, params,
                                               nparams, item->typedParam) < 0)
                goto error;
590 591
            break;
        }
592
    }
593

594 595
    if ((migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INITIAL].set ||
         migParams->params[QEMU_MIGRATION_PARAM_THROTTLE_INCREMENT].set) &&
596 597 598 599 600 601
        !(flags & VIR_MIGRATE_AUTO_CONVERGE)) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Turn auto convergence on to tune it"));
        goto error;
    }

602 603 604 605 606 607 608
    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;
    }

609
    if (qemuMigrationParamsSetCompression(params, nparams, flags, migParams) < 0)
610 611
        goto error;

612 613 614
    return migParams;

 error:
615
    qemuMigrationParamsFree(migParams);
616 617 618 619
    return NULL;
}


620
int
621 622 623 624 625
qemuMigrationParamsDump(qemuMigrationParamsPtr migParams,
                        virTypedParameterPtr *params,
                        int *nparams,
                        int *maxparams,
                        unsigned long *flags)
626 627 628
{
    size_t i;

629
    if (migParams->compMethods == 1ULL << QEMU_MIGRATION_COMPRESS_XBZRLE &&
630
        !migParams->params[QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE].set) {
631 632 633 634 635
        *flags |= VIR_MIGRATE_COMPRESSED;
        return 0;
    }

    for (i = 0; i < QEMU_MIGRATION_COMPRESS_LAST; ++i) {
636
        if ((migParams->compMethods & (1ULL << i)) &&
637 638 639 640 641 642
            virTypedParamsAddString(params, nparams, maxparams,
                                    VIR_MIGRATE_PARAM_COMPRESSION,
                                    qemuMigrationCompressMethodTypeToString(i)) < 0)
            return -1;
    }

643
    for (i = 0; i < G_N_ELEMENTS(qemuMigrationParamsTPMap); i++) {
644
        const qemuMigrationParamsTPMapItem *item = &qemuMigrationParamsTPMap[i];
645

646 647
        if (!(item->party & QEMU_MIGRATION_DESTINATION))
            continue;
648

649 650 651 652
        switch (qemuMigrationParamTypes[item->param]) {
        case QEMU_MIGRATION_PARAM_TYPE_INT:
            if (qemuMigrationParamsSetTPInt(migParams, item->param,
                                            params, nparams, maxparams,
653
                                            item->typedParam, item->unit) < 0)
654 655
                return -1;
            break;
656

657 658 659
        case QEMU_MIGRATION_PARAM_TYPE_ULL:
            if (qemuMigrationParamsSetTPULL(migParams, item->param,
                                            params, nparams, maxparams,
660
                                            item->typedParam, item->unit) < 0)
661 662 663 664
                return -1;
            break;

        case QEMU_MIGRATION_PARAM_TYPE_BOOL:
665 666
            break;

667
        case QEMU_MIGRATION_PARAM_TYPE_STRING:
668 669 670 671
            if (qemuMigrationParamsSetTPString(migParams, item->param,
                                               params, nparams, maxparams,
                                               item->typedParam) < 0)
                return -1;
672 673 674
            break;
        }
    }
675 676 677 678 679

    return 0;
}


680
qemuMigrationParamsPtr
681 682
qemuMigrationParamsFromJSON(virJSONValuePtr params)
{
683 684 685 686 687
    qemuMigrationParamsPtr migParams;
    qemuMigrationParamValuePtr pv;
    const char *name;
    const char *str;
    size_t i;
688 689 690 691 692 693 694

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

    if (!params)
        return migParams;

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    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))) {
717
                pv->value.s = g_strdup(str);
718 719 720 721 722
                pv->set = true;
            }
            break;
        }
    }
723 724 725 726 727

    return migParams;
}


728
virJSONValuePtr
729 730 731
qemuMigrationParamsToJSON(qemuMigrationParamsPtr migParams)
{
    virJSONValuePtr params = NULL;
732 733 734 735
    qemuMigrationParamValuePtr pv;
    const char *name;
    size_t i;
    int rc;
736 737 738 739

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

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    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;
    }
769 770 771 772 773 774 775 776 777

    return params;

 error:
    virJSONValueFree(params);
    return NULL;
}


778 779 780 781 782 783
virJSONValuePtr
qemuMigrationCapsToJSON(virBitmapPtr caps,
                        virBitmapPtr states)
{
    virJSONValuePtr json = NULL;
    virJSONValuePtr cap = NULL;
784
    qemuMigrationCapability bit;
785 786 787 788 789
    const char *name;

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

790
    for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) {
791 792 793 794 795 796 797 798 799 800 801 802
        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;

803
        name = qemuMigrationCapabilityTypeToString(bit);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
        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;
}


825 826 827 828 829 830 831 832 833 834 835
/**
 * 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.
 */
836
int
837 838 839 840
qemuMigrationParamsApply(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr migParams)
841 842
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
843
    bool xbzrleCacheSize_old = false;
844
    virJSONValuePtr params = NULL;
845
    virJSONValuePtr caps = NULL;
846
    qemuMigrationParam xbzrle = QEMU_MIGRATION_PARAM_XBZRLE_CACHE_SIZE;
847
    int ret = -1;
848
    int rc;
849 850 851 852

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

853 854 855 856 857
    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"));
858
            goto cleanup;
859 860 861 862 863 864 865 866 867 868 869
        }
    } 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;
        }
870 871
    }

872 873 874 875
    /* 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.
     */
876
    if (migParams->params[xbzrle].set &&
877
        (!priv->job.migParams ||
878
         !priv->job.migParams->params[xbzrle].set)) {
879
        if (qemuMonitorSetMigrationCacheSize(priv->mon,
880
                                             migParams->params[xbzrle].value.ull) < 0)
881 882
            goto cleanup;
        xbzrleCacheSize_old = true;
883
        migParams->params[xbzrle].set = false;
884 885
    }

886
    if (!(params = qemuMigrationParamsToJSON(migParams)))
887 888
        goto cleanup;

889 890 891 892 893 894 895
    if (virJSONValueObjectKeysNumber(params) > 0) {
        rc = qemuMonitorSetMigrationParams(priv->mon, params);
        params = NULL;
        if (rc < 0)
            goto cleanup;
    }

896 897 898 899 900 901
    ret = 0;

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

902
    if (xbzrleCacheSize_old)
903
        migParams->params[xbzrle].set = true;
904

905
    virJSONValueFree(params);
906
    virJSONValueFree(caps);
907

908 909 910 911
    return ret;
}


912 913 914 915 916 917 918 919 920
/**
 * 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.
 */
921 922 923 924 925 926 927 928
static int
qemuMigrationParamsSetString(qemuMigrationParamsPtr migParams,
                             qemuMigrationParam param,
                             const char *value)
{
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_STRING) < 0)
        return -1;

929
    migParams->params[param].value.s = g_strdup(value);
930

931 932
    migParams->params[param].set = true;

933 934 935 936
    return 0;
}


937
/* qemuMigrationParamsEnableTLS
938 939
 * @driver: pointer to qemu driver
 * @vm: domain object
940 941 942
 * @tlsListen: server or client
 * @asyncJob: Migration job to join
 * @tlsAlias: alias to be generated for TLS object
943
 * @hostname: hostname of the migration destination
944
 * @migParams: migration parameters to set
945
 *
946 947 948
 * 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.
949
 *
950
 * Returns 0 on success, -1 on failure
951 952
 */
int
953 954 955 956 957
qemuMigrationParamsEnableTLS(virQEMUDriverPtr driver,
                             virDomainObjPtr vm,
                             bool tlsListen,
                             int asyncJob,
                             char **tlsAlias,
958
                             const char *hostname,
959
                             qemuMigrationParamsPtr migParams)
960 961
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
962 963
    virJSONValuePtr tlsProps = NULL;
    virJSONValuePtr secProps = NULL;
964
    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
965
    const char *secAlias = NULL;
966
    int ret = -1;
967 968 969 970

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

974
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set) {
975 976 977
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("TLS migration is not supported with this "
                         "QEMU binary"));
978
        goto error;
979 980 981
    }

    /* If there's a secret, then grab/store it now using the connection */
982 983 984 985 986 987 988
    if (cfg->migrateTLSx509secretUUID) {
        if (!(priv->migSecinfo =
              qemuDomainSecretInfoTLSNew(priv, QEMU_MIGRATION_TLS_ALIAS_BASE,
                                         cfg->migrateTLSx509secretUUID)))
            goto error;
        secAlias = priv->migSecinfo->s.aes.alias;
    }
989

990 991 992
    if (!(*tlsAlias = qemuAliasTLSObjFromSrcAlias(QEMU_MIGRATION_TLS_ALIAS_BASE)))
        goto error;

993 994 995
    if (qemuDomainGetTLSObjects(priv->qemuCaps, priv->migSecinfo,
                                cfg->migrateTLSx509certdir, tlsListen,
                                cfg->migrateTLSx509verify,
996
                                *tlsAlias, &tlsProps, &secProps) < 0)
997 998 999 1000 1001 1002
        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 . */
1003
    qemuDomainDelTLSObjects(driver, vm, asyncJob, secAlias, *tlsAlias);
1004

1005
    if (qemuDomainAddTLSObjects(driver, vm, asyncJob, &secProps, &tlsProps) < 0)
1006 1007
        goto error;

1008 1009 1010 1011 1012
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS,
                                     *tlsAlias) < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME,
J
Ján Tomko 已提交
1013
                                     NULLSTR_EMPTY(hostname)) < 0)
1014 1015
        goto error;

1016 1017 1018 1019 1020
    ret = 0;

 cleanup:
    virObjectUnref(cfg);
    return ret;
1021 1022 1023 1024

 error:
    virJSONValueFree(tlsProps);
    virJSONValueFree(secProps);
1025
    goto cleanup;
1026 1027 1028
}


1029
/* qemuMigrationParamsDisableTLS
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
 * @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
1040 1041
qemuMigrationParamsDisableTLS(virDomainObjPtr vm,
                              qemuMigrationParamsPtr migParams)
1042
{
1043
    qemuDomainObjPrivatePtr priv = vm->privateData;
1044

1045
    if (!priv->job.migParams->params[QEMU_MIGRATION_PARAM_TLS_CREDS].set)
1046
        return 0;
1047

1048 1049 1050 1051
    if (qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_CREDS, "") < 0 ||
        qemuMigrationParamsSetString(migParams,
                                     QEMU_MIGRATION_PARAM_TLS_HOSTNAME, "") < 0)
1052
        return -1;
1053 1054 1055 1056 1057 1058 1059 1060 1061

    return 0;
}


/* qemuMigrationParamsResetTLS
 * @driver: pointer to qemu driver
 * @vm: domain object
 * @asyncJob: migration job to join
1062
 * @apiFlags: API flags used to start the migration
1063 1064 1065 1066
 *
 * Deconstruct all the setup possibly done for TLS - delete the TLS and
 * security objects, free the secinfo, and reset the migration params to "".
 */
1067
static void
1068 1069
qemuMigrationParamsResetTLS(virQEMUDriverPtr driver,
                            virDomainObjPtr vm,
1070
                            int asyncJob,
1071 1072
                            qemuMigrationParamsPtr origParams,
                            unsigned long apiFlags)
1073 1074 1075 1076
{
    char *tlsAlias = NULL;
    char *secAlias = NULL;

1077 1078 1079 1080
    /* 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))
1081
        return;
1082

1083 1084 1085 1086 1087 1088 1089
    /* 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);
1090
    qemuDomainSecretInfoFree(&QEMU_DOMAIN_PRIVATE(vm)->migSecinfo);
1091 1092 1093 1094 1095 1096

    VIR_FREE(tlsAlias);
    VIR_FREE(secAlias);
}


1097 1098 1099 1100 1101 1102 1103
int
qemuMigrationParamsFetch(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
                         int asyncJob,
                         qemuMigrationParamsPtr *migParams)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1104
    virJSONValuePtr jsonParams = NULL;
1105 1106 1107 1108 1109 1110 1111 1112
    int ret = -1;
    int rc;

    *migParams = NULL;

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

1113
    rc = qemuMonitorGetMigrationParams(priv->mon, &jsonParams);
1114 1115 1116 1117

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

1118 1119 1120
    if (!(*migParams = qemuMigrationParamsFromJSON(jsonParams)))
        goto cleanup;

1121 1122 1123
    ret = 0;

 cleanup:
1124
    virJSONValueFree(jsonParams);
1125 1126 1127 1128
    return ret;
}


1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
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;
}


1143
/**
1144 1145
 * Returns -1 on error,
 *          0 on success,
1146 1147 1148
 *          1 if the parameter is not supported by QEMU.
 */
int
1149 1150 1151
qemuMigrationParamsGetULL(qemuMigrationParamsPtr migParams,
                          qemuMigrationParam param,
                          unsigned long long *value)
1152
{
1153 1154 1155 1156
    if (qemuMigrationParamsCheckType(param, QEMU_MIGRATION_PARAM_TYPE_ULL) < 0)
        return -1;

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

1159
    *value = migParams->params[param].value.ull;
1160 1161 1162 1163
    return 0;
}


1164 1165 1166 1167 1168
/**
 * qemuMigrationParamsCheck:
 *
 * Check supported migration parameters and keep their original values in
 * qemuDomainJobObj so that we can properly reset them at the end of migration.
1169 1170
 * Reports an error if any of the currently used capabilities in @migParams
 * are unsupported by QEMU.
1171 1172 1173 1174
 */
int
qemuMigrationParamsCheck(virQEMUDriverPtr driver,
                         virDomainObjPtr vm,
1175
                         int asyncJob,
1176 1177
                         qemuMigrationParamsPtr migParams,
                         virBitmapPtr remoteCaps)
1178 1179
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1180
    qemuMigrationCapability cap;
1181 1182
    qemuMigrationParty party;
    size_t i;
1183

1184 1185 1186 1187 1188
    if (asyncJob == QEMU_ASYNC_JOB_MIGRATION_OUT)
        party = QEMU_MIGRATION_SOURCE;
    else
        party = QEMU_MIGRATION_DESTINATION;

1189
    for (cap = 0; cap < QEMU_MIGRATION_CAP_LAST; cap++) {
1190 1191 1192 1193 1194 1195 1196
        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"),
1197
                           qemuMigrationCapabilityTypeToString(cap));
1198 1199 1200 1201
            return -1;
        }
    }

1202
    for (i = 0; i < G_N_ELEMENTS(qemuMigrationParamsAlwaysOn); i++) {
1203 1204 1205 1206
        cap = qemuMigrationParamsAlwaysOn[i].cap;

        if (qemuMigrationParamsAlwaysOn[i].party & party &&
            qemuMigrationCapsGet(vm, cap)) {
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
            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;
                }
            }

1222
            VIR_DEBUG("Enabling migration capability '%s'",
1223
                      qemuMigrationCapabilityTypeToString(cap));
1224 1225 1226 1227
            ignore_value(virBitmapSetBit(migParams->caps, cap));
        }
    }

1228 1229 1230 1231 1232
    /*
     * We want to disable all migration capabilities after migration, no need
     * to ask QEMU for their current settings.
     */

1233
    return qemuMigrationParamsFetch(driver, vm, asyncJob, &priv->job.migParams);
1234 1235 1236
}


1237 1238 1239 1240 1241 1242 1243 1244 1245
/*
 * 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,
1246
                         int asyncJob,
1247 1248
                         qemuMigrationParamsPtr origParams,
                         unsigned long apiFlags)
1249
{
1250 1251 1252
    virErrorPtr err;

    virErrorPreserveLast(&err);
1253

1254 1255
    VIR_DEBUG("Resetting migration parameters %p, flags 0x%lx",
              origParams, apiFlags);
1256

1257
    if (!virDomainObjIsActive(vm) || !origParams)
1258 1259
        goto cleanup;

1260 1261
    if (qemuMigrationParamsApply(driver, vm, asyncJob, origParams) < 0)
        goto cleanup;
1262

1263
    qemuMigrationParamsResetTLS(driver, vm, asyncJob, origParams, apiFlags);
1264 1265

 cleanup:
1266
    virErrorRestore(&err);
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 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
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:
1378
            rc = virStringParseYesNo(value, &pv->value.b);
1379 1380 1381
            break;

        case QEMU_MIGRATION_PARAM_TYPE_STRING:
1382
            pv->value.s = g_steal_pointer(&value);
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
            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);
    }

1398
    *migParams = g_steal_pointer(&params);
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
    ret = 0;

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


1410 1411 1412 1413 1414 1415
int
qemuMigrationCapsCheck(virQEMUDriverPtr driver,
                       virDomainObjPtr vm,
                       int asyncJob)
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
1416
    virBitmapPtr migEvent = NULL;
1417
    virJSONValuePtr json = NULL;
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    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;
    }

1436
    priv->migrationCaps = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1437 1438 1439 1440
    if (!priv->migrationCaps)
        goto cleanup;

    for (capStr = caps; *capStr; capStr++) {
1441
        int cap = qemuMigrationCapabilityTypeFromString(*capStr);
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

        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)) {
1452
        migEvent = virBitmapNew(QEMU_MIGRATION_CAP_LAST);
1453 1454 1455
        if (!migEvent)
            goto cleanup;

1456
        ignore_value(virBitmapSetBit(migEvent, QEMU_MIGRATION_CAP_EVENTS));
1457

1458 1459 1460
        if (!(json = qemuMigrationCapsToJSON(migEvent, migEvent)))
            goto cleanup;

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

1464 1465
        rc = qemuMonitorSetMigrationCapabilities(priv->mon, json);
        json = NULL;
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481

        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,
1482
                                   QEMU_MIGRATION_CAP_EVENTS));
1483 1484 1485 1486

    ret = 0;

 cleanup:
1487
    virBitmapFree(migEvent);
1488
    virJSONValueFree(json);
1489 1490 1491
    virStringListFree(caps);
    return ret;
}
J
Jiri Denemark 已提交
1492 1493 1494 1495


bool
qemuMigrationCapsGet(virDomainObjPtr vm,
1496
                     qemuMigrationCapability cap)
J
Jiri Denemark 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505
{
    qemuDomainObjPrivatePtr priv = vm->privateData;
    bool enabled = false;

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

    return enabled;
}