domain_capabilities.c 15.6 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 43 44 45 46 47

static int virDomainCapsOnceInit(void)
{
    if (!(virDomainCapsClass = virClassNew(virClassForObjectLockable(),
                                           "virDomainCapsClass",
                                           sizeof(virDomainCaps),
                                           virDomainCapsDispose)))
        return -1;
48 49 50 51 52 53 54 55

    virDomainCapsCPUModelsClass = virClassNew(virClassForObject(),
                                              "virDomainCapsCPUModelsClass",
                                              sizeof(virDomainCapsCPUModels),
                                              virDomainCapsCPUModelsDispose);
    if (!virDomainCapsCPUModelsClass)
        return -1;

M
Michal Privoznik 已提交
56 57 58 59 60 61 62
    return 0;
}


VIR_ONCE_GLOBAL_INIT(virDomainCaps)


63 64 65 66 67 68 69 70 71 72 73 74 75 76
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);
}


M
Michal Privoznik 已提交
77 78 79 80 81 82 83
static void
virDomainCapsDispose(void *obj)
{
    virDomainCapsPtr caps = obj;

    VIR_FREE(caps->path);
    VIR_FREE(caps->machine);
84
    virObjectUnref(caps->cpu.custom);
85
    virCPUDefFree(caps->cpu.hostModel);
86 87

    virDomainCapsStringValuesFree(&caps->os.loader.values);
M
Michal Privoznik 已提交
88 89 90
}


91 92 93 94 95 96 97 98 99 100 101 102 103
static void
virDomainCapsCPUModelsDispose(void *obj)
{
    virDomainCapsCPUModelsPtr cpuModels = obj;
    size_t i;

    for (i = 0; i < cpuModels->nmodels; i++)
        VIR_FREE(cpuModels->models[i].name);

    VIR_FREE(cpuModels->models);
}


M
Michal Privoznik 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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;
}


131 132 133 134 135 136 137 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
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 已提交
164 165
        if (virDomainCapsCPUModelsAdd(cpuModels,
                                      old->models[i].name, -1,
166 167
                                      old->models[i].usable,
                                      old->models[i].blockers) < 0)
168 169 170 171 172 173 174 175 176 177 178
            goto error;
    }

    return cpuModels;

 error:
    virObjectUnref(cpuModels);
    return NULL;
}


179 180
virDomainCapsCPUModelsPtr
virDomainCapsCPUModelsFilter(virDomainCapsCPUModelsPtr old,
181 182
                             const char **models,
                             const char **blacklist)
183 184 185 186 187 188 189 190
{
    virDomainCapsCPUModelsPtr cpuModels;
    size_t i;

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

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

194 195 196
        if (blacklist && virStringListHasString(blacklist, old->models[i].name))
            continue;

197
        if (virDomainCapsCPUModelsAdd(cpuModels,
J
Jiri Denemark 已提交
198
                                      old->models[i].name, -1,
199 200
                                      old->models[i].usable,
                                      old->models[i].blockers) < 0)
201 202 203 204 205 206 207 208 209 210 211
            goto error;
    }

    return cpuModels;

 error:
    virObjectUnref(cpuModels);
    return NULL;
}


212 213
int
virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
J
Jiri Denemark 已提交
214
                               char **name,
215 216
                               virDomainCapsCPUUsable usable,
                               char ***blockers)
217 218 219 220 221
{
    if (VIR_RESIZE_N(cpuModels->models, cpuModels->nmodels_max,
                     cpuModels->nmodels, 1) < 0)
        return -1;

J
Jiri Denemark 已提交
222 223
    cpuModels->models[cpuModels->nmodels].usable = usable;
    VIR_STEAL_PTR(cpuModels->models[cpuModels->nmodels].name, *name);
224 225 226 227

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

J
Jiri Denemark 已提交
228
    cpuModels->nmodels++;
229 230 231 232 233 234 235
    return 0;
}


int
virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
                          const char *name,
J
Jiri Denemark 已提交
236
                          ssize_t nameLen,
237 238
                          virDomainCapsCPUUsable usable,
                          char **blockers)
239
{
240 241 242 243 244
    char *nameCopy = NULL;
    char **blockersCopy = NULL;

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

246
    if (virStringListCopy(&blockersCopy, (const char **)blockers) < 0)
247 248
        goto error;

249 250
    if (virDomainCapsCPUModelsAddSteal(cpuModels, &nameCopy,
                                       usable, &blockersCopy) < 0)
251 252 253 254 255
        goto error;

    return 0;

 error:
256 257
    VIR_FREE(nameCopy);
    virStringListFree(blockersCopy);
258 259 260 261
    return -1;
}


M
Michal Privoznik 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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;
}

334 335 336 337 338 339 340 341 342 343 344 345

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]);
}


M
Michal Privoznik 已提交
346 347 348
#define FORMAT_PROLOGUE(item)                                       \
    do {                                                            \
        virBufferAsprintf(buf, "<" #item " supported='%s'%s\n",     \
349 350 351
                          item->supported ? "yes" : "no",           \
                          item->supported ? ">" : "/>");            \
        if (!item->supported)                                       \
M
Michal Privoznik 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
            return;                                                 \
        virBufferAdjustIndent(buf, 2);                              \
    } while (0)

#define FORMAT_EPILOGUE(item)                                       \
    do {                                                            \
        virBufferAdjustIndent(buf, -2);                             \
        virBufferAddLit(buf, "</" #item ">\n");                     \
    } while (0)

#define ENUM_PROCESS(master, capsEnum, valToStr)                    \
    do {                                                            \
        virDomainCapsEnumFormat(buf, &master->capsEnum,             \
                                #capsEnum, valToStr);               \
    } while (0)

368 369 370 371 372 373 374

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

375
    virDomainCapsStringValuesFormat(buf, &loader->values);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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);
}

395 396 397 398 399 400 401 402 403
static void
virDomainCapsCPUCustomFormat(virBufferPtr buf,
                             virDomainCapsCPUModelsPtr custom)
{
    size_t i;

    virBufferAdjustIndent(buf, 2);

    for (i = 0; i < custom->nmodels; i++) {
J
Jiri Denemark 已提交
404 405 406 407
        virDomainCapsCPUModelPtr model = custom->models + i;
        virBufferAsprintf(buf, "<model usable='%s'>%s</model>\n",
                          virDomainCapsCPUUsableTypeToString(model->usable),
                          model->name);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    }

    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");

424 425 426 427 428 429
    virBufferAsprintf(buf, "<mode name='%s' ",
                      virCPUModeTypeToString(VIR_CPU_MODE_HOST_MODEL));
    if (cpu->hostModel) {
        virBufferAddLit(buf, "supported='yes'>\n");
        virBufferAdjustIndent(buf, 2);

430
        virCPUDefFormatBuf(buf, cpu->hostModel);
431 432 433 434 435 436

        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</mode>\n");
    } else {
        virBufferAddLit(buf, "supported='no'/>\n");
    }
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

    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 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464
static void
virDomainCapsDeviceDiskFormat(virBufferPtr buf,
                              virDomainCapsDeviceDiskPtr const disk)
{
    FORMAT_PROLOGUE(disk);

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

    FORMAT_EPILOGUE(disk);
}


465 466 467 468 469 470 471 472 473 474 475 476
static void
virDomainCapsDeviceGraphicsFormat(virBufferPtr buf,
                                  virDomainCapsDeviceGraphicsPtr const graphics)
{
    FORMAT_PROLOGUE(graphics);

    ENUM_PROCESS(graphics, type, virDomainGraphicsTypeToString);

    FORMAT_EPILOGUE(graphics);
}


477 478 479 480 481 482 483 484 485 486 487 488
static void
virDomainCapsDeviceVideoFormat(virBufferPtr buf,
                               virDomainCapsDeviceVideoPtr const video)
{
    FORMAT_PROLOGUE(video);

    ENUM_PROCESS(video, modelType, virDomainVideoTypeToString);

    FORMAT_EPILOGUE(video);
}


M
Michal Privoznik 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
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 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
/**
 * 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);
}


533 534
char *
virDomainCapsFormat(virDomainCapsPtr const caps)
M
Michal Privoznik 已提交
535
{
536
    virBuffer buf = VIR_BUFFER_INITIALIZER;
M
Michal Privoznik 已提交
537 538 539
    const char *virttype_str = virDomainVirtTypeToString(caps->virttype);
    const char *arch_str = virArchToString(caps->arch);

540 541
    virBufferAddLit(&buf, "<domainCapabilities>\n");
    virBufferAdjustIndent(&buf, 2);
M
Michal Privoznik 已提交
542

543 544
    virBufferEscapeString(&buf, "<path>%s</path>\n", caps->path);
    virBufferAsprintf(&buf, "<domain>%s</domain>\n", virttype_str);
545
    if (caps->machine)
546 547
        virBufferAsprintf(&buf, "<machine>%s</machine>\n", caps->machine);
    virBufferAsprintf(&buf, "<arch>%s</arch>\n", arch_str);
M
Michal Privoznik 已提交
548 549

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

552 553
    virDomainCapsOSFormat(&buf, &caps->os);
    virDomainCapsCPUFormat(&buf, &caps->cpu);
554

555 556
    virBufferAddLit(&buf, "<devices>\n");
    virBufferAdjustIndent(&buf, 2);
M
Michal Privoznik 已提交
557

558 559 560 561
    virDomainCapsDeviceDiskFormat(&buf, &caps->disk);
    virDomainCapsDeviceGraphicsFormat(&buf, &caps->graphics);
    virDomainCapsDeviceVideoFormat(&buf, &caps->video);
    virDomainCapsDeviceHostdevFormat(&buf, &caps->hostdev);
M
Michal Privoznik 已提交
562

563 564
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</devices>\n");
M
Michal Privoznik 已提交
565

566 567
    virBufferAddLit(&buf, "<features>\n");
    virBufferAdjustIndent(&buf, 2);
A
Andrea Bolognani 已提交
568

569
    virDomainCapsFeatureGICFormat(&buf, &caps->gic);
A
Andrea Bolognani 已提交
570

571 572
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</features>\n");
A
Andrea Bolognani 已提交
573

574 575
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</domainCapabilities>\n");
M
Michal Privoznik 已提交
576

577
    virBufferCheckError(&buf);
M
Michal Privoznik 已提交
578 579
    return virBufferContentAndReset(&buf);
}