domain_capabilities.c 15.5 KB
Newer Older
M
Michal Privoznik 已提交
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
/*
 * domain_capabilities.c: domain capabilities XML processing
 *
 * Copyright (C) 2014 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/>.
 *
 * Author: Michal Privoznik <mprivozn@redhat.com>
 */

#include <config.h>

#include "domain_capabilities.h"
#include "domain_conf.h"
#include "viralloc.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_CAPABILITIES

J
Jiri Denemark 已提交
32 33 34
VIR_ENUM_IMPL(virDomainCapsCPUUsable, VIR_DOMCAPS_CPU_USABLE_LAST,
              "unknown", "yes", "no");

M
Michal Privoznik 已提交
35
static virClassPtr virDomainCapsClass;
36
static virClassPtr virDomainCapsCPUModelsClass;
M
Michal Privoznik 已提交
37 38

static void virDomainCapsDispose(void *obj);
39
static void virDomainCapsCPUModelsDispose(void *obj);
M
Michal Privoznik 已提交
40 41 42

static int virDomainCapsOnceInit(void)
{
43
    if (!VIR_CLASS_NEW(virDomainCaps, virClassForObjectLockable()))
M
Michal Privoznik 已提交
44
        return -1;
45

46
    if (!VIR_CLASS_NEW(virDomainCapsCPUModels, virClassForObject()))
47 48
        return -1;

M
Michal Privoznik 已提交
49 50 51 52 53 54 55
    return 0;
}


VIR_ONCE_GLOBAL_INIT(virDomainCaps)


56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void
virDomainCapsStringValuesFree(virDomainCapsStringValuesPtr values)
{
    size_t i;

    if (!values || !values->values)
        return;

    for (i = 0; i < values->nvalues; i++)
        VIR_FREE(values->values[i]);
    VIR_FREE(values->values);
}


70 71 72 73 74 75 76 77 78 79 80 81
void
virSEVCapabilitiesFree(virSEVCapability *cap)
{
    if (!cap)
        return;

    VIR_FREE(cap->pdh);
    VIR_FREE(cap->cert_chain);
    VIR_FREE(cap);
}


M
Michal Privoznik 已提交
82 83 84 85 86 87 88
static void
virDomainCapsDispose(void *obj)
{
    virDomainCapsPtr caps = obj;

    VIR_FREE(caps->path);
    VIR_FREE(caps->machine);
89
    virObjectUnref(caps->cpu.custom);
90
    virCPUDefFree(caps->cpu.hostModel);
91 92

    virDomainCapsStringValuesFree(&caps->os.loader.values);
M
Michal Privoznik 已提交
93 94 95
}


96 97 98 99 100 101
static void
virDomainCapsCPUModelsDispose(void *obj)
{
    virDomainCapsCPUModelsPtr cpuModels = obj;
    size_t i;

102
    for (i = 0; i < cpuModels->nmodels; i++) {
103
        VIR_FREE(cpuModels->models[i].name);
104 105
        virStringListFree(cpuModels->models[i].blockers);
    }
106 107 108 109 110

    VIR_FREE(cpuModels->models);
}


M
Michal Privoznik 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
virDomainCapsPtr
virDomainCapsNew(const char *path,
                 const char *machine,
                 virArch arch,
                 virDomainVirtType virttype)
{
    virDomainCapsPtr caps = NULL;

    if (virDomainCapsInitialize() < 0)
        return NULL;

    if (!(caps = virObjectLockableNew(virDomainCapsClass)))
        return NULL;

    if (VIR_STRDUP(caps->path, path) < 0 ||
        VIR_STRDUP(caps->machine, machine) < 0)
        goto error;
    caps->arch = arch;
    caps->virttype = virttype;

    return caps;
 error:
    virObjectUnref(caps);
    return NULL;
}


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
virDomainCapsCPUModelsPtr
virDomainCapsCPUModelsNew(size_t nmodels)
{
    virDomainCapsCPUModelsPtr cpuModels = NULL;

    if (virDomainCapsInitialize() < 0)
        return NULL;

    if (!(cpuModels = virObjectNew(virDomainCapsCPUModelsClass)))
        return NULL;

    if (VIR_ALLOC_N(cpuModels->models, nmodels) < 0)
        goto error;
    cpuModels->nmodels_max = nmodels;

    return cpuModels;

 error:
    virObjectUnref(cpuModels);
    return NULL;
}


virDomainCapsCPUModelsPtr
virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old)
{
    virDomainCapsCPUModelsPtr cpuModels;
    size_t i;

    if (!(cpuModels = virDomainCapsCPUModelsNew(old->nmodels)))
        return NULL;

    for (i = 0; i < old->nmodels; i++) {
J
Jiri Denemark 已提交
171 172
        if (virDomainCapsCPUModelsAdd(cpuModels,
                                      old->models[i].name, -1,
173 174
                                      old->models[i].usable,
                                      old->models[i].blockers) < 0)
175 176 177 178 179 180 181 182 183 184 185
            goto error;
    }

    return cpuModels;

 error:
    virObjectUnref(cpuModels);
    return NULL;
}


186 187
virDomainCapsCPUModelsPtr
virDomainCapsCPUModelsFilter(virDomainCapsCPUModelsPtr old,
188 189
                             const char **models,
                             const char **blacklist)
190 191 192 193 194 195 196 197
{
    virDomainCapsCPUModelsPtr cpuModels;
    size_t i;

    if (!(cpuModels = virDomainCapsCPUModelsNew(0)))
        return NULL;

    for (i = 0; i < old->nmodels; i++) {
198
        if (models && !virStringListHasString(models, old->models[i].name))
199 200
            continue;

201 202 203
        if (blacklist && virStringListHasString(blacklist, old->models[i].name))
            continue;

204
        if (virDomainCapsCPUModelsAdd(cpuModels,
J
Jiri Denemark 已提交
205
                                      old->models[i].name, -1,
206 207
                                      old->models[i].usable,
                                      old->models[i].blockers) < 0)
208 209 210 211 212 213 214 215 216 217 218
            goto error;
    }

    return cpuModels;

 error:
    virObjectUnref(cpuModels);
    return NULL;
}


219 220
int
virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
J
Jiri Denemark 已提交
221
                               char **name,
222 223
                               virDomainCapsCPUUsable usable,
                               char ***blockers)
224 225 226 227 228
{
    if (VIR_RESIZE_N(cpuModels->models, cpuModels->nmodels_max,
                     cpuModels->nmodels, 1) < 0)
        return -1;

J
Jiri Denemark 已提交
229 230
    cpuModels->models[cpuModels->nmodels].usable = usable;
    VIR_STEAL_PTR(cpuModels->models[cpuModels->nmodels].name, *name);
231 232 233 234

    if (blockers)
        VIR_STEAL_PTR(cpuModels->models[cpuModels->nmodels].blockers, *blockers);

J
Jiri Denemark 已提交
235
    cpuModels->nmodels++;
236 237 238 239 240 241 242
    return 0;
}


int
virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
                          const char *name,
J
Jiri Denemark 已提交
243
                          ssize_t nameLen,
244 245
                          virDomainCapsCPUUsable usable,
                          char **blockers)
246
{
247 248 249 250 251
    char *nameCopy = NULL;
    char **blockersCopy = NULL;

    if (VIR_STRNDUP(nameCopy, name, nameLen) < 0)
        goto error;
252

253
    if (virStringListCopy(&blockersCopy, (const char **)blockers) < 0)
254 255
        goto error;

256 257
    if (virDomainCapsCPUModelsAddSteal(cpuModels, &nameCopy,
                                       usable, &blockersCopy) < 0)
258 259 260 261 262
        goto error;

    return 0;

 error:
263 264
    VIR_FREE(nameCopy);
    virStringListFree(blockersCopy);
265 266 267 268
    return -1;
}


269 270 271 272 273 274 275 276 277 278
virDomainCapsCPUModelPtr
virDomainCapsCPUModelsGet(virDomainCapsCPUModelsPtr cpuModels,
                          const char *name)
{
    size_t i;

    if (!cpuModels)
        return NULL;

    for (i = 0; i < cpuModels->nmodels; i++) {
279
        if (STRCASEEQ(cpuModels->models[i].name, name))
280 281 282 283 284 285 286
            return cpuModels->models + i;
    }

    return NULL;
}


M
Michal Privoznik 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
int
virDomainCapsEnumSet(virDomainCapsEnumPtr capsEnum,
                     const char *capsEnumName,
                     size_t nvalues,
                     unsigned int *values)
{
    int ret = -1;
    size_t i;

    for (i = 0; i < nvalues; i++) {
        unsigned int val = 1 << values[i];

        if (!val) {
            /* Integer overflow */
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("integer overflow on %s. Please contact the "
                             "libvirt development team at libvir-list@redhat.com"),
                           capsEnumName);
            goto cleanup;
        }

        capsEnum->values |= val;
    }

    ret = 0;
 cleanup:
    return ret;
}


void
virDomainCapsEnumClear(virDomainCapsEnumPtr capsEnum)
{
    capsEnum->values = 0;
}


static int
virDomainCapsEnumFormat(virBufferPtr buf,
                        virDomainCapsEnumPtr capsEnum,
                        const char *capsEnumName,
                        virDomainCapsValToStr valToStr)
{
    int ret = -1;
    size_t i;

    virBufferAsprintf(buf, "<enum name='%s'", capsEnumName);
    if (!capsEnum->values) {
        virBufferAddLit(buf, "/>\n");
        ret = 0;
        goto cleanup;
    }
    virBufferAddLit(buf, ">\n");
    virBufferAdjustIndent(buf, 2);

    for (i = 0; i < sizeof(capsEnum->values) * CHAR_BIT; i++) {
        const char *val;

        if (!(capsEnum->values & (1 << i)))
            continue;

        if ((val = (valToStr)(i)))
            virBufferAsprintf(buf, "<value>%s</value>\n", val);
    }
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</enum>\n");

    ret = 0;
 cleanup:
    return ret;
}

359 360 361 362 363 364 365 366 367 368 369 370

static void
virDomainCapsStringValuesFormat(virBufferPtr buf,
                                virDomainCapsStringValuesPtr values)
{
    size_t i;

    for (i = 0; i < values->nvalues; i++)
        virBufferEscapeString(buf, "<value>%s</value>\n", values->values[i]);
}


371 372 373 374 375 376 377 378
#define FORMAT_PROLOGUE(item) \
    do { \
        virBufferAsprintf(buf, "<" #item " supported='%s'%s\n", \
                          item->supported ? "yes" : "no", \
                          item->supported ? ">" : "/>"); \
        if (!item->supported) \
            return; \
        virBufferAdjustIndent(buf, 2); \
M
Michal Privoznik 已提交
379 380
    } while (0)

381 382 383 384
#define FORMAT_EPILOGUE(item) \
    do { \
        virBufferAdjustIndent(buf, -2); \
        virBufferAddLit(buf, "</" #item ">\n"); \
M
Michal Privoznik 已提交
385 386
    } while (0)

387 388 389 390
#define ENUM_PROCESS(master, capsEnum, valToStr) \
    do { \
        virDomainCapsEnumFormat(buf, &master->capsEnum, \
                                #capsEnum, valToStr); \
M
Michal Privoznik 已提交
391 392
    } while (0)

393 394 395 396 397 398 399

static void
virDomainCapsLoaderFormat(virBufferPtr buf,
                          virDomainCapsLoaderPtr loader)
{
    FORMAT_PROLOGUE(loader);

400
    virDomainCapsStringValuesFormat(buf, &loader->values);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    ENUM_PROCESS(loader, type, virDomainLoaderTypeToString);
    ENUM_PROCESS(loader, readonly, virTristateBoolTypeToString);

    FORMAT_EPILOGUE(loader);
}

static void
virDomainCapsOSFormat(virBufferPtr buf,
                      virDomainCapsOSPtr os)
{
    virDomainCapsLoaderPtr loader = &os->loader;

    FORMAT_PROLOGUE(os);

    virDomainCapsLoaderFormat(buf, loader);

    FORMAT_EPILOGUE(os);
}

420 421 422 423 424 425 426 427 428
static void
virDomainCapsCPUCustomFormat(virBufferPtr buf,
                             virDomainCapsCPUModelsPtr custom)
{
    size_t i;

    virBufferAdjustIndent(buf, 2);

    for (i = 0; i < custom->nmodels; i++) {
J
Jiri Denemark 已提交
429 430 431 432
        virDomainCapsCPUModelPtr model = custom->models + i;
        virBufferAsprintf(buf, "<model usable='%s'>%s</model>\n",
                          virDomainCapsCPUUsableTypeToString(model->usable),
                          model->name);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    }

    virBufferAdjustIndent(buf, -2);
}

static void
virDomainCapsCPUFormat(virBufferPtr buf,
                       virDomainCapsCPUPtr cpu)
{
    virBufferAddLit(buf, "<cpu>\n");
    virBufferAdjustIndent(buf, 2);

    virBufferAsprintf(buf, "<mode name='%s' supported='%s'/>\n",
                      virCPUModeTypeToString(VIR_CPU_MODE_HOST_PASSTHROUGH),
                      cpu->hostPassthrough ? "yes" : "no");

449 450 451 452 453 454
    virBufferAsprintf(buf, "<mode name='%s' ",
                      virCPUModeTypeToString(VIR_CPU_MODE_HOST_MODEL));
    if (cpu->hostModel) {
        virBufferAddLit(buf, "supported='yes'>\n");
        virBufferAdjustIndent(buf, 2);

455
        virCPUDefFormatBuf(buf, cpu->hostModel);
456 457 458 459 460 461

        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</mode>\n");
    } else {
        virBufferAddLit(buf, "supported='no'/>\n");
    }
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

    virBufferAsprintf(buf, "<mode name='%s' ",
                      virCPUModeTypeToString(VIR_CPU_MODE_CUSTOM));
    if (cpu->custom && cpu->custom->nmodels) {
        virBufferAddLit(buf, "supported='yes'>\n");
        virDomainCapsCPUCustomFormat(buf, cpu->custom);
        virBufferAddLit(buf, "</mode>\n");
    } else {
        virBufferAddLit(buf, "supported='no'/>\n");
    }

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

M
Michal Privoznik 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489
static void
virDomainCapsDeviceDiskFormat(virBufferPtr buf,
                              virDomainCapsDeviceDiskPtr const disk)
{
    FORMAT_PROLOGUE(disk);

    ENUM_PROCESS(disk, diskDevice, virDomainDiskDeviceTypeToString);
    ENUM_PROCESS(disk, bus, virDomainDiskBusTypeToString);

    FORMAT_EPILOGUE(disk);
}


490 491 492 493 494 495 496 497 498 499 500 501
static void
virDomainCapsDeviceGraphicsFormat(virBufferPtr buf,
                                  virDomainCapsDeviceGraphicsPtr const graphics)
{
    FORMAT_PROLOGUE(graphics);

    ENUM_PROCESS(graphics, type, virDomainGraphicsTypeToString);

    FORMAT_EPILOGUE(graphics);
}


502 503 504 505 506 507 508 509 510 511 512 513
static void
virDomainCapsDeviceVideoFormat(virBufferPtr buf,
                               virDomainCapsDeviceVideoPtr const video)
{
    FORMAT_PROLOGUE(video);

    ENUM_PROCESS(video, modelType, virDomainVideoTypeToString);

    FORMAT_EPILOGUE(video);
}


M
Michal Privoznik 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
static void
virDomainCapsDeviceHostdevFormat(virBufferPtr buf,
                                 virDomainCapsDeviceHostdevPtr const hostdev)
{
    FORMAT_PROLOGUE(hostdev);

    ENUM_PROCESS(hostdev, mode, virDomainHostdevModeTypeToString);
    ENUM_PROCESS(hostdev, startupPolicy, virDomainStartupPolicyTypeToString);
    ENUM_PROCESS(hostdev, subsysType, virDomainHostdevSubsysTypeToString);
    ENUM_PROCESS(hostdev, capsType, virDomainHostdevCapsTypeToString);
    ENUM_PROCESS(hostdev, pciBackend, virDomainHostdevSubsysPCIBackendTypeToString);

    FORMAT_EPILOGUE(hostdev);
}


A
Andrea Bolognani 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/**
 * virDomainCapsFeatureGICFormat:
 * @buf: target buffer
 * @gic: GIC features
 *
 * Format GIC features for inclusion in the domcapabilities XML.
 *
 * The resulting XML will look like
 *
 *   <gic supported='yes'>
 *     <enum name='version>
 *       <value>2</value>
 *       <value>3</value>
 *     </enum>
 *   </gic>
 */
static void
virDomainCapsFeatureGICFormat(virBufferPtr buf,
                              virDomainCapsFeatureGICPtr const gic)
{
    FORMAT_PROLOGUE(gic);

    ENUM_PROCESS(gic, version, virGICVersionTypeToString);

    FORMAT_EPILOGUE(gic);
}


558 559
char *
virDomainCapsFormat(virDomainCapsPtr const caps)
M
Michal Privoznik 已提交
560
{
561
    virBuffer buf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
562 563 564
    const char *virttype_str = virDomainVirtTypeToString(caps->virttype);
    const char *arch_str = virArchToString(caps->arch);

565 566
    virBufferAddLit(&buf, "<domainCapabilities>\n");
    virBufferAdjustIndent(&buf, 2);
M
Michal Privoznik 已提交
567

568 569
    virBufferEscapeString(&buf, "<path>%s</path>\n", caps->path);
    virBufferAsprintf(&buf, "<domain>%s</domain>\n", virttype_str);
570
    if (caps->machine)
571 572
        virBufferAsprintf(&buf, "<machine>%s</machine>\n", caps->machine);
    virBufferAsprintf(&buf, "<arch>%s</arch>\n", arch_str);
M
Michal Privoznik 已提交
573 574

    if (caps->maxvcpus)
575
        virBufferAsprintf(&buf, "<vcpu max='%d'/>\n", caps->maxvcpus);
M
Michal Privoznik 已提交
576

577 578 579
    virBufferAsprintf(&buf, "<iothreads supported='%s'/>\n",
                      caps->iothreads ? "yes" : "no");

580 581
    virDomainCapsOSFormat(&buf, &caps->os);
    virDomainCapsCPUFormat(&buf, &caps->cpu);
582

583 584
    virBufferAddLit(&buf, "<devices>\n");
    virBufferAdjustIndent(&buf, 2);
M
Michal Privoznik 已提交
585

586 587 588 589
    virDomainCapsDeviceDiskFormat(&buf, &caps->disk);
    virDomainCapsDeviceGraphicsFormat(&buf, &caps->graphics);
    virDomainCapsDeviceVideoFormat(&buf, &caps->video);
    virDomainCapsDeviceHostdevFormat(&buf, &caps->hostdev);
M
Michal Privoznik 已提交
590

591 592
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</devices>\n");
M
Michal Privoznik 已提交
593

594 595
    virBufferAddLit(&buf, "<features>\n");
    virBufferAdjustIndent(&buf, 2);
A
Andrea Bolognani 已提交
596

597
    virDomainCapsFeatureGICFormat(&buf, &caps->gic);
598 599
    virBufferAsprintf(&buf, "<vmcoreinfo supported='%s'/>\n",
                      caps->vmcoreinfo ? "yes" : "no");
A
Andrea Bolognani 已提交
600

601 602 603
    virBufferAsprintf(&buf, "<genid supported='%s'/>\n",
                      caps->genid ? "yes" : "no");

604 605
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</features>\n");
A
Andrea Bolognani 已提交
606

607 608
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</domainCapabilities>\n");
M
Michal Privoznik 已提交
609

610
    virBufferCheckError(&buf);
M
Michal Privoznik 已提交
611 612
    return virBufferContentAndReset(&buf);
}