node_device_conf.c 71.7 KB
Newer Older
1 2 3
/*
 * node_device_conf.c: config handling for node devices
 *
4
 * Copyright (C) 2009-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2008 Virtual Iron Software, Inc.
 * Copyright (C) 2008 David F. Lively
 *
 * 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
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

#include <config.h>

#include <unistd.h>
#include <errno.h>

30
#include "virerror.h"
31
#include "datatypes.h"
32
#include "viralloc.h"
33
#include "virstring.h"
34
#include "node_device_conf.h"
35
#include "device_conf.h"
36
#include "virxml.h"
37
#include "virbuffer.h"
38
#include "viruuid.h"
39
#include "virrandom.h"
40

41
#define VIR_FROM_THIS VIR_FROM_NODEDEV
42 43 44 45 46 47 48 49

VIR_ENUM_IMPL(virNodeDevCap, VIR_NODE_DEV_CAP_LAST,
              "system",
              "pci",
              "usb_device",
              "usb",
              "net",
              "scsi_host",
D
David Allan 已提交
50
              "scsi_target",
51
              "scsi",
52 53
              "storage",
              "fc_host",
54 55
              "vports",
              "scsi_generic")
56 57 58

VIR_ENUM_IMPL(virNodeDevNetCap, VIR_NODE_DEV_CAP_NET_LAST,
              "80203",
59
              "80211")
60

61
static int
62
virNodeDevCapsDefParseString(const char *xpath,
63
                             xmlXPathContextPtr ctxt,
64
                             char **string)
65 66 67
{
    char *s;

68
    if (!(s = virXPathString(xpath, ctxt)))
69 70 71 72 73 74
        return -1;

    *string = s;
    return 0;
}

E
Eric Blake 已提交
75
int virNodeDeviceHasCap(const virNodeDeviceObj *dev, const char *cap)
76 77
{
    virNodeDevCapsDefPtr caps = dev->def->caps;
78 79 80 81 82
    const char *fc_host_cap =
        virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_FC_HOST);
    const char *vports_cap =
        virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_VPORTS);

83
    while (caps) {
84
        if (STREQ(cap, virNodeDevCapTypeToString(caps->data.type)))
85
            return 1;
86
        else if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST)
87
            if ((STREQ(cap, fc_host_cap) &&
88 89
                (caps->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST)) ||
90
                (STREQ(cap, vports_cap) &&
91 92 93
                (caps->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS)))
                return 1;
94 95 96 97
        caps = caps->next;
    }
    return 0;
}
98

99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* virNodeDeviceFindFCCapDef:
 * @dev: Pointer to current device
 *
 * Search the device object 'caps' array for fc_host capability.
 *
 * Returns:
 * Pointer to the caps or NULL if not found
 */
static virNodeDevCapsDefPtr
virNodeDeviceFindFCCapDef(const virNodeDeviceObj *dev)
{
    virNodeDevCapsDefPtr caps = dev->def->caps;

    while (caps) {
        if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST &&
            (caps->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST))
            break;

        caps = caps->next;
    }
    return caps;
}


124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/* virNodeDeviceFindVPORTCapDef:
 * @dev: Pointer to current device
 *
 * Search the device object 'caps' array for vport_ops capability.
 *
 * Returns:
 * Pointer to the caps or NULL if not found
 */
static virNodeDevCapsDefPtr
virNodeDeviceFindVPORTCapDef(const virNodeDeviceObj *dev)
{
    virNodeDevCapsDefPtr caps = dev->def->caps;

    while (caps) {
        if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST &&
            (caps->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS))
            break;

        caps = caps->next;
    }
    return caps;
}


148
virNodeDeviceObjPtr
E
Eric Blake 已提交
149
virNodeDeviceFindBySysfsPath(virNodeDeviceObjListPtr devs,
150 151
                             const char *sysfs_path)
{
152
    size_t i;
153 154 155 156 157 158 159 160 161 162 163 164 165 166

    for (i = 0; i < devs->count; i++) {
        virNodeDeviceObjLock(devs->objs[i]);
        if ((devs->objs[i]->def->sysfs_path != NULL) &&
            (STREQ(devs->objs[i]->def->sysfs_path, sysfs_path))) {
            return devs->objs[i];
        }
        virNodeDeviceObjUnlock(devs->objs[i]);
    }

    return NULL;
}


E
Eric Blake 已提交
167
virNodeDeviceObjPtr virNodeDeviceFindByName(virNodeDeviceObjListPtr devs,
168 169
                                            const char *name)
{
170
    size_t i;
171

172 173
    for (i = 0; i < devs->count; i++) {
        virNodeDeviceObjLock(devs->objs[i]);
174 175
        if (STREQ(devs->objs[i]->def->name, name))
            return devs->objs[i];
176 177
        virNodeDeviceObjUnlock(devs->objs[i]);
    }
178 179 180 181 182

    return NULL;
}


183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static virNodeDeviceObjPtr
virNodeDeviceFindByWWNs(virNodeDeviceObjListPtr devs,
                        const char *parent_wwnn,
                        const char *parent_wwpn)
{
    size_t i;

    for (i = 0; i < devs->count; i++) {
        virNodeDevCapsDefPtr cap;
        virNodeDeviceObjLock(devs->objs[i]);
        if ((cap = virNodeDeviceFindFCCapDef(devs->objs[i])) &&
            STREQ_NULLABLE(cap->data.scsi_host.wwnn, parent_wwnn) &&
            STREQ_NULLABLE(cap->data.scsi_host.wwpn, parent_wwpn))
            return devs->objs[i];
        virNodeDeviceObjUnlock(devs->objs[i]);
    }

    return NULL;
}


static virNodeDeviceObjPtr
virNodeDeviceFindByFabricWWN(virNodeDeviceObjListPtr devs,
                             const char *parent_fabric_wwn)
{
    size_t i;

    for (i = 0; i < devs->count; i++) {
        virNodeDevCapsDefPtr cap;
        virNodeDeviceObjLock(devs->objs[i]);
        if ((cap = virNodeDeviceFindFCCapDef(devs->objs[i])) &&
            STREQ_NULLABLE(cap->data.scsi_host.fabric_wwn, parent_fabric_wwn))
            return devs->objs[i];
        virNodeDeviceObjUnlock(devs->objs[i]);
    }

    return NULL;
}


223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static virNodeDeviceObjPtr
virNodeDeviceFindByCap(virNodeDeviceObjListPtr devs,
                       const char *cap)
{
    size_t i;

    for (i = 0; i < devs->count; i++) {
        virNodeDeviceObjLock(devs->objs[i]);
        if (virNodeDeviceHasCap(devs->objs[i], cap))
            return devs->objs[i];
        virNodeDeviceObjUnlock(devs->objs[i]);
    }

    return NULL;
}


240 241 242 243 244 245 246 247 248
void virNodeDeviceDefFree(virNodeDeviceDefPtr def)
{
    virNodeDevCapsDefPtr caps;

    if (!def)
        return;

    VIR_FREE(def->name);
    VIR_FREE(def->parent);
249 250 251
    VIR_FREE(def->parent_wwnn);
    VIR_FREE(def->parent_wwpn);
    VIR_FREE(def->parent_fabric_wwn);
252
    VIR_FREE(def->driver);
253 254
    VIR_FREE(def->sysfs_path);
    VIR_FREE(def->parent_sysfs_path);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    caps = def->caps;
    while (caps) {
        virNodeDevCapsDefPtr next = caps->next;
        virNodeDevCapsDefFree(caps);
        caps = next;
    }

    VIR_FREE(def);
}

void virNodeDeviceObjFree(virNodeDeviceObjPtr dev)
{
    if (!dev)
        return;

    virNodeDeviceDefFree(dev->def);
    if (dev->privateFree)
        (*dev->privateFree)(dev->privateData);

275 276
    virMutexDestroy(&dev->lock);

277 278 279 280 281
    VIR_FREE(dev);
}

void virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs)
{
282
    size_t i;
283
    for (i = 0; i < devs->count; i++)
284 285 286 287 288
        virNodeDeviceObjFree(devs->objs[i]);
    VIR_FREE(devs->objs);
    devs->count = 0;
}

289
virNodeDeviceObjPtr virNodeDeviceAssignDef(virNodeDeviceObjListPtr devs,
E
Eric Blake 已提交
290
                                           virNodeDeviceDefPtr def)
291 292 293 294 295 296 297 298 299
{
    virNodeDeviceObjPtr device;

    if ((device = virNodeDeviceFindByName(devs, def->name))) {
        virNodeDeviceDefFree(device->def);
        device->def = def;
        return device;
    }

300
    if (VIR_ALLOC(device) < 0)
301 302
        return NULL;

303
    if (virMutexInit(&device->lock) < 0) {
304 305
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
306 307 308
        VIR_FREE(device);
        return NULL;
    }
309
    virNodeDeviceObjLock(device);
310

311
    if (VIR_APPEND_ELEMENT_COPY(devs->objs, devs->count, device) < 0) {
312
        virNodeDeviceObjUnlock(device);
313 314 315
        virNodeDeviceObjFree(device);
        return NULL;
    }
316
    device->def = def;
317 318 319 320 321 322

    return device;

}

void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
323
                            virNodeDeviceObjPtr *dev)
324
{
325
    size_t i;
326

327
    virNodeDeviceObjUnlock(*dev);
328

329
    for (i = 0; i < devs->count; i++) {
330 331 332
        virNodeDeviceObjLock(*dev);
        if (devs->objs[i] == *dev) {
            virNodeDeviceObjUnlock(*dev);
333
            virNodeDeviceObjFree(devs->objs[i]);
334
            *dev = NULL;
335

336
            VIR_DELETE_ELEMENT(devs->objs, i, devs->count);
337 338
            break;
        }
339
        virNodeDeviceObjUnlock(*dev);
340 341 342
    }
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
static void
virPCIELinkFormat(virBufferPtr buf,
                  virPCIELinkPtr lnk,
                  const char *attrib)
{
    if (!lnk)
        return;

    virBufferAsprintf(buf, "<link validity='%s'", attrib);
    if (lnk->port >= 0)
        virBufferAsprintf(buf, " port='%d'", lnk->port);
    if (lnk->speed)
        virBufferAsprintf(buf, " speed='%s'",
                          virPCIELinkSpeedTypeToString(lnk->speed));
    virBufferAsprintf(buf, " width='%d'", lnk->width);
    virBufferAddLit(buf, "/>\n");
}

static void
virPCIEDeviceInfoFormat(virBufferPtr buf,
                        virPCIEDeviceInfoPtr info)
{
    if (!info->link_cap && !info->link_sta) {
        virBufferAddLit(buf, "<pci-express/>\n");
        return;
    }

    virBufferAddLit(buf, "<pci-express>\n");
    virBufferAdjustIndent(buf, 2);

    virPCIELinkFormat(buf, info->link_cap, "cap");
    virPCIELinkFormat(buf, info->link_sta, "sta");

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

E
Eric Blake 已提交
380
char *virNodeDeviceDefFormat(const virNodeDeviceDef *def)
381 382
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
383
    virNodeDevCapsDefPtr caps;
384
    size_t i = 0;
385 386

    virBufferAddLit(&buf, "<device>\n");
387 388 389 390 391
    virBufferAdjustIndent(&buf, 2);
    virBufferEscapeString(&buf, "<name>%s</name>\n", def->name);
    virBufferEscapeString(&buf, "<path>%s</path>\n", def->sysfs_path);
    if (def->parent)
        virBufferEscapeString(&buf, "<parent>%s</parent>\n", def->parent);
392
    if (def->driver) {
393 394 395 396 397
        virBufferAddLit(&buf, "<driver>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferEscapeString(&buf, "<name>%s</name>\n", def->driver);
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</driver>\n");
398
    }
399 400 401

    for (caps = def->caps; caps; caps = caps->next) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
402
        virNodeDevCapDataPtr data = &caps->data;
403

404
        virBufferAsprintf(&buf, "<capability type='%s'>\n",
405
                          virNodeDevCapTypeToString(caps->data.type));
406
        virBufferAdjustIndent(&buf, 2);
407
        switch (caps->data.type) {
408 409
        case VIR_NODE_DEV_CAP_SYSTEM:
            if (data->system.product_name)
410
                virBufferEscapeString(&buf, "<product>%s</product>\n",
411
                                      data->system.product_name);
412 413
            virBufferAddLit(&buf, "<hardware>\n");
            virBufferAdjustIndent(&buf, 2);
414
            if (data->system.hardware.vendor_name)
415
                virBufferEscapeString(&buf, "<vendor>%s</vendor>\n",
416 417
                                      data->system.hardware.vendor_name);
            if (data->system.hardware.version)
418
                virBufferEscapeString(&buf, "<version>%s</version>\n",
419 420
                                      data->system.hardware.version);
            if (data->system.hardware.serial)
421
                virBufferEscapeString(&buf, "<serial>%s</serial>\n",
422 423
                                      data->system.hardware.serial);
            virUUIDFormat(data->system.hardware.uuid, uuidstr);
424 425 426 427 428 429
            virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", uuidstr);
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</hardware>\n");

            virBufferAddLit(&buf, "<firmware>\n");
            virBufferAdjustIndent(&buf, 2);
430
            if (data->system.firmware.vendor_name)
431
                virBufferEscapeString(&buf, "<vendor>%s</vendor>\n",
432 433
                                      data->system.firmware.vendor_name);
            if (data->system.firmware.version)
434
                virBufferEscapeString(&buf, "<version>%s</version>\n",
435 436
                                      data->system.firmware.version);
            if (data->system.firmware.release_date)
437
                virBufferEscapeString(&buf, "<release_date>%s</release_date>\n",
438
                                      data->system.firmware.release_date);
439 440
            virBufferAdjustIndent(&buf, -2);
            virBufferAddLit(&buf, "</firmware>\n");
441 442
            break;
        case VIR_NODE_DEV_CAP_PCI_DEV:
443
            virBufferAsprintf(&buf, "<domain>%d</domain>\n",
444
                              data->pci_dev.domain);
445 446
            virBufferAsprintf(&buf, "<bus>%d</bus>\n", data->pci_dev.bus);
            virBufferAsprintf(&buf, "<slot>%d</slot>\n",
447
                              data->pci_dev.slot);
448
            virBufferAsprintf(&buf, "<function>%d</function>\n",
449
                              data->pci_dev.function);
450
            virBufferAsprintf(&buf, "<product id='0x%04x'",
451 452 453 454 455 456
                                  data->pci_dev.product);
            if (data->pci_dev.product_name)
                virBufferEscapeString(&buf, ">%s</product>\n",
                                      data->pci_dev.product_name);
            else
                virBufferAddLit(&buf, " />\n");
457
            virBufferAsprintf(&buf, "<vendor id='0x%04x'",
458 459 460 461 462 463
                                  data->pci_dev.vendor);
            if (data->pci_dev.vendor_name)
                virBufferEscapeString(&buf, ">%s</vendor>\n",
                                      data->pci_dev.vendor_name);
            else
                virBufferAddLit(&buf, " />\n");
464
            if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION) {
465 466
                virBufferAddLit(&buf, "<capability type='phys_function'>\n");
                virBufferAdjustIndent(&buf, 2);
467
                virBufferAsprintf(&buf,
468
                                  "<address domain='0x%.4x' bus='0x%.2x' "
469 470 471 472 473
                                  "slot='0x%.2x' function='0x%.1x'/>\n",
                                  data->pci_dev.physical_function->domain,
                                  data->pci_dev.physical_function->bus,
                                  data->pci_dev.physical_function->slot,
                                  data->pci_dev.physical_function->function);
474 475
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</capability>\n");
476 477
            }
            if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION) {
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                virBufferAddLit(&buf, "<capability type='virt_functions'");
                if (data->pci_dev.max_virtual_functions)
                    virBufferAsprintf(&buf, " maxCount='%u'",
                                      data->pci_dev.max_virtual_functions);
                if (data->pci_dev.num_virtual_functions == 0) {
                    virBufferAddLit(&buf, "/>\n");
                } else {
                    virBufferAddLit(&buf, ">\n");
                    virBufferAdjustIndent(&buf, 2);
                    for (i = 0; i < data->pci_dev.num_virtual_functions; i++) {
                        virBufferAsprintf(&buf,
                                          "<address domain='0x%.4x' bus='0x%.2x' "
                                          "slot='0x%.2x' function='0x%.1x'/>\n",
                                          data->pci_dev.virtual_functions[i]->domain,
                                          data->pci_dev.virtual_functions[i]->bus,
                                          data->pci_dev.virtual_functions[i]->slot,
                                          data->pci_dev.virtual_functions[i]->function);
                    }
                    virBufferAdjustIndent(&buf, -2);
                    virBufferAddLit(&buf, "</capability>\n");
498 499
                }
            }
500 501 502 503
            if (data->pci_dev.hdrType) {
                virBufferAsprintf(&buf, "<capability type='%s'/>\n",
                                  virPCIHeaderTypeToString(data->pci_dev.hdrType));
            }
504
            if (data->pci_dev.nIommuGroupDevices) {
505
                virBufferAsprintf(&buf, "<iommuGroup number='%d'>\n",
506
                                  data->pci_dev.iommuGroupNumber);
507
                virBufferAdjustIndent(&buf, 2);
508 509
                for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++) {
                    virBufferAsprintf(&buf,
510
                                      "<address domain='0x%.4x' bus='0x%.2x' "
511 512 513 514 515 516
                                      "slot='0x%.2x' function='0x%.1x'/>\n",
                                      data->pci_dev.iommuGroupDevices[i]->domain,
                                      data->pci_dev.iommuGroupDevices[i]->bus,
                                      data->pci_dev.iommuGroupDevices[i]->slot,
                                      data->pci_dev.iommuGroupDevices[i]->function);
                }
517 518
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</iommuGroup>\n");
519
            }
520 521 522
            if (data->pci_dev.numa_node >= 0)
                virBufferAsprintf(&buf, "<numa node='%d'/>\n",
                                  data->pci_dev.numa_node);
523

524 525
            if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCIE)
                virPCIEDeviceInfoFormat(&buf, data->pci_dev.pci_express);
526 527
            break;
        case VIR_NODE_DEV_CAP_USB_DEV:
528 529
            virBufferAsprintf(&buf, "<bus>%d</bus>\n", data->usb_dev.bus);
            virBufferAsprintf(&buf, "<device>%d</device>\n",
530
                              data->usb_dev.device);
531
            virBufferAsprintf(&buf, "<product id='0x%04x'",
532 533 534 535 536 537
                                  data->usb_dev.product);
            if (data->usb_dev.product_name)
                virBufferEscapeString(&buf, ">%s</product>\n",
                                      data->usb_dev.product_name);
            else
                virBufferAddLit(&buf, " />\n");
538
            virBufferAsprintf(&buf, "<vendor id='0x%04x'",
539 540 541 542 543 544 545 546
                                  data->usb_dev.vendor);
            if (data->usb_dev.vendor_name)
                virBufferEscapeString(&buf, ">%s</vendor>\n",
                                      data->usb_dev.vendor_name);
            else
                virBufferAddLit(&buf, " />\n");
            break;
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
547
            virBufferAsprintf(&buf, "<number>%d</number>\n",
548
                              data->usb_if.number);
549
            virBufferAsprintf(&buf, "<class>%d</class>\n",
550
                              data->usb_if._class);
551
            virBufferAsprintf(&buf, "<subclass>%d</subclass>\n",
552
                              data->usb_if.subclass);
553
            virBufferAsprintf(&buf, "<protocol>%d</protocol>\n",
554 555
                              data->usb_if.protocol);
            if (data->usb_if.description)
556
                virBufferEscapeString(&buf,
557
                                  "<description>%s</description>\n",
558 559 560
                                  data->usb_if.description);
            break;
        case VIR_NODE_DEV_CAP_NET:
561
            virBufferEscapeString(&buf, "<interface>%s</interface>\n",
562
                              data->net.ifname);
563
            if (data->net.address)
564
                virBufferEscapeString(&buf, "<address>%s</address>\n",
565
                                  data->net.address);
566
            virInterfaceLinkFormat(&buf, &data->net.lnk);
567 568
            if (data->net.features) {
                for (i = 0; i < VIR_NET_DEV_FEAT_LAST; i++) {
J
Ján Tomko 已提交
569
                    if (virBitmapIsBitSet(data->net.features, i)) {
570 571 572 573 574
                        virBufferAsprintf(&buf, "<feature name='%s'/>\n",
                                          virNetDevFeatureTypeToString(i));
                    }
                }
            }
575 576 577
            if (data->net.subtype != VIR_NODE_DEV_CAP_NET_LAST) {
                const char *subtyp =
                    virNodeDevNetCapTypeToString(data->net.subtype);
578
                virBufferEscapeString(&buf, "<capability type='%s'/>\n",
579
                                      subtyp);
580 581 582
            }
            break;
        case VIR_NODE_DEV_CAP_SCSI_HOST:
583
            virBufferAsprintf(&buf, "<host>%d</host>\n",
584
                              data->scsi_host.host);
J
John Ferlan 已提交
585 586 587
            if (data->scsi_host.unique_id != -1)
                virBufferAsprintf(&buf, "<unique_id>%d</unique_id>\n",
                                  data->scsi_host.unique_id);
588
            if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
589 590 591
                virBufferAddLit(&buf, "<capability type='fc_host'>\n");
                virBufferAdjustIndent(&buf, 2);
                virBufferEscapeString(&buf, "<wwnn>%s</wwnn>\n",
592
                                      data->scsi_host.wwnn);
593
                virBufferEscapeString(&buf, "<wwpn>%s</wwpn>\n",
594
                                      data->scsi_host.wwpn);
595
                virBufferEscapeString(&buf, "<fabric_wwn>%s</fabric_wwn>\n",
O
Osier Yang 已提交
596
                                      data->scsi_host.fabric_wwn);
597 598
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</capability>\n");
599 600
            }
            if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
601 602 603
                virBufferAddLit(&buf, "<capability type='vport_ops'>\n");
                virBufferAdjustIndent(&buf, 2);
                virBufferAsprintf(&buf, "<max_vports>%d</max_vports>\n",
604
                                  data->scsi_host.max_vports);
605
                virBufferAsprintf(&buf, "<vports>%d</vports>\n",
606
                                  data->scsi_host.vports);
607 608
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</capability>\n");
609 610
            }

611
            break;
D
David Allan 已提交
612 613

        case VIR_NODE_DEV_CAP_SCSI_TARGET:
614
            virBufferEscapeString(&buf, "<target>%s</target>\n",
615
                                  data->scsi_target.name);
D
David Allan 已提交
616 617
            break;

618
        case VIR_NODE_DEV_CAP_SCSI:
619 620 621
            virBufferAsprintf(&buf, "<host>%d</host>\n", data->scsi.host);
            virBufferAsprintf(&buf, "<bus>%d</bus>\n", data->scsi.bus);
            virBufferAsprintf(&buf, "<target>%d</target>\n",
622
                              data->scsi.target);
623
            virBufferAsprintf(&buf, "<lun>%d</lun>\n", data->scsi.lun);
624
            if (data->scsi.type)
625
                virBufferEscapeString(&buf, "<type>%s</type>\n",
626
                                      data->scsi.type);
627 628
            break;
        case VIR_NODE_DEV_CAP_STORAGE:
629 630
            virBufferEscapeString(&buf, "<block>%s</block>\n",
                                  data->storage.block);
631
            if (data->storage.bus)
632 633
                virBufferEscapeString(&buf, "<bus>%s</bus>\n",
                                      data->storage.bus);
634
            if (data->storage.drive_type)
635 636
                virBufferEscapeString(&buf, "<drive_type>%s</drive_type>\n",
                                      data->storage.drive_type);
637
            if (data->storage.model)
638 639
                virBufferEscapeString(&buf, "<model>%s</model>\n",
                                      data->storage.model);
640
            if (data->storage.vendor)
641 642
                virBufferEscapeString(&buf, "<vendor>%s</vendor>\n",
                                      data->storage.vendor);
643
            if (data->storage.serial)
644 645
                virBufferEscapeString(&buf, "<serial>%s</serial>\n",
                                      data->storage.serial);
646 647 648
            if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE) {
                int avl = data->storage.flags &
                    VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE;
649 650 651
                virBufferAddLit(&buf, "<capability type='removable'>\n");
                virBufferAdjustIndent(&buf, 2);
                virBufferAsprintf(&buf, "<media_available>%d"
652
                                  "</media_available>\n", avl ? 1 : 0);
653
                virBufferAsprintf(&buf, "<media_size>%llu</media_size>\n",
654
                                  data->storage.removable_media_size);
655 656
                if (data->storage.media_label)
                    virBufferEscapeString(&buf,
657 658
                                          "<media_label>%s</media_label>\n",
                                          data->storage.media_label);
659
                if (data->storage.logical_block_size > 0)
660
                    virBufferAsprintf(&buf, "<logical_block_size>%llu"
661 662 663
                                      "</logical_block_size>\n",
                                      data->storage.logical_block_size);
                if (data->storage.num_blocks > 0)
664
                    virBufferAsprintf(&buf,
665
                                      "<num_blocks>%llu</num_blocks>\n",
666
                                      data->storage.num_blocks);
667 668
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</capability>\n");
669
            } else {
670
                virBufferAsprintf(&buf, "<size>%llu</size>\n",
671
                                  data->storage.size);
672
                if (data->storage.logical_block_size > 0)
673
                    virBufferAsprintf(&buf, "<logical_block_size>%llu"
674 675 676
                                      "</logical_block_size>\n",
                                      data->storage.logical_block_size);
                if (data->storage.num_blocks > 0)
677
                    virBufferAsprintf(&buf, "<num_blocks>%llu</num_blocks>\n",
678
                                      data->storage.num_blocks);
679 680
            }
            if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE)
681
                virBufferAddLit(&buf, "<capability type='hotpluggable' />\n");
682
            break;
683
        case VIR_NODE_DEV_CAP_SCSI_GENERIC:
684
            virBufferEscapeString(&buf, "<char>%s</char>\n",
685 686
                                  data->sg.path);
            break;
687 688
        case VIR_NODE_DEV_CAP_FC_HOST:
        case VIR_NODE_DEV_CAP_VPORTS:
689 690 691 692
        case VIR_NODE_DEV_CAP_LAST:
            break;
        }

693 694
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</capability>\n");
695 696
    }

697
    virBufferAdjustIndent(&buf, -2);
698 699
    virBufferAddLit(&buf, "</device>\n");

700 701
    if (virBufferCheckError(&buf) < 0)
        return NULL;
702 703 704 705

    return virBufferContentAndReset(&buf);
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
/**
 * virNodeDevCapsDefParseIntOptional:
 * @xpath:  XPath to evaluate
 * @ctxt:   Context
 * @value:  Where to store parsed value
 * @def:    Node device which is parsed
 * @invalid_error_fmt:  error message to print on invalid format
 *
 * Returns: -1 on error (invalid int format under @xpath)
 *           0 if @xpath was not found (@value is untouched)
 *           1 on success
 */
static int
virNodeDevCapsDefParseIntOptional(const char *xpath,
                                  xmlXPathContextPtr ctxt,
                                  int *value,
                                  virNodeDeviceDefPtr def,
                                  const char *invalid_error_fmt)
{
    int ret;
    int val;

    ret = virXPathInt(xpath, ctxt, &val);
    if (ret < -1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       invalid_error_fmt,
                       def->name);
        return -1;
    } else if (ret == -1) {
        return 0;
    }
    *value = val;
    return 1;
}

741
static int
742
virNodeDevCapsDefParseULong(const char *xpath,
743 744 745 746 747 748 749 750 751
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

752
    ret = virXPathULong(xpath, ctxt, &val);
753
    if (ret < 0) {
754 755 756
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
757 758 759 760 761 762 763 764
        return -1;
    }

    *value = val;
    return 0;
}

static int
765
virNodeDevCapsDefParseULongLong(const char *xpath,
766 767 768 769 770 771 772 773 774
                                xmlXPathContextPtr ctxt,
                                unsigned long long *value,
                                virNodeDeviceDefPtr def,
                                const char *missing_error_fmt,
                                const char *invalid_error_fmt)
{
    int ret;
    unsigned long long val;

775
    ret = virXPathULongLong(xpath, ctxt, &val);
776
    if (ret < 0) {
777 778 779
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
780 781 782 783 784 785 786 787
        return -1;
    }

    *value = val;
    return 0;
}

static int
788
virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt,
789 790
                             virNodeDeviceDefPtr def,
                             xmlNodePtr node,
791
                             virNodeDevCapDataPtr data)
792 793
{
    xmlNodePtr orignode, *nodes = NULL;
794 795
    size_t i;
    int n, ret = -1;
796 797 798 799 800
    unsigned long long val;

    orignode = ctxt->node;
    ctxt->node = node;

801
    data->storage.block = virXPathString("string(./block[1])", ctxt);
802
    if (!data->storage.block) {
803 804 805
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no block device path supplied for '%s'"),
                       def->name);
806 807 808
        goto out;
    }

809 810 811 812 813
    data->storage.bus        = virXPathString("string(./bus[1])", ctxt);
    data->storage.drive_type = virXPathString("string(./drive_type[1])", ctxt);
    data->storage.model      = virXPathString("string(./model[1])", ctxt);
    data->storage.vendor     = virXPathString("string(./vendor[1])", ctxt);
    data->storage.serial     = virXPathString("string(./serial[1])", ctxt);
814

815
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
816 817
        goto out;

818
    for (i = 0; i < n; i++) {
819 820 821
        char *type = virXMLPropString(nodes[i], "type");

        if (!type) {
822 823 824
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing storage capability type for '%s'"),
                           def->name);
825 826 827
            goto out;
        }

828
        if (STREQ(type, "hotpluggable")) {
829
            data->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE;
830
        } else if (STREQ(type, "removable")) {
831 832 833 834 835 836 837
            xmlNodePtr orignode2;

            data->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE;

            orignode2 = ctxt->node;
            ctxt->node = nodes[i];

838
            if (virXPathBoolean("count(./media_available[. = '1']) > 0", ctxt))
839 840
                data->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE;

841
            data->storage.media_label = virXPathString("string(./media_label[1])", ctxt);
842

843
            val = 0;
844
            if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def,
845 846 847 848 849 850 851 852 853 854
                                                _("no removable media size supplied for '%s'"),
                                                _("invalid removable media size supplied for '%s'")) < 0) {
                ctxt->node = orignode2;
                VIR_FREE(type);
                goto out;
            }
            data->storage.removable_media_size = val;

            ctxt->node = orignode2;
        } else {
855 856 857
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown storage capability type '%s' for '%s'"),
                           type, def->name);
858 859 860 861 862 863 864 865 866
            VIR_FREE(type);
            goto out;
        }

        VIR_FREE(type);
    }

    if (!(data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE)) {
        val = 0;
867
        if (virNodeDevCapsDefParseULongLong("number(./size[1])", ctxt, &val, def,
868 869 870 871 872 873 874
                                            _("no size supplied for '%s'"),
                                            _("invalid size supplied for '%s'")) < 0)
            goto out;
        data->storage.size = val;
    }

    ret = 0;
875
 out:
876 877 878 879 880 881
    VIR_FREE(nodes);
    ctxt->node = orignode;
    return ret;
}

static int
882
virNodeDevCapSCSIParseXML(xmlXPathContextPtr ctxt,
883 884
                          virNodeDeviceDefPtr def,
                          xmlNodePtr node,
885
                          virNodeDevCapDataPtr data)
886 887 888 889 890 891 892
{
    xmlNodePtr orignode;
    int ret = -1;

    orignode = ctxt->node;
    ctxt->node = node;

893
    if (virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
894 895 896 897 898
                                    &data->scsi.host, def,
                                    _("no SCSI host ID supplied for '%s'"),
                                    _("invalid SCSI host ID supplied for '%s'")) < 0)
        goto out;

899
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
900 901 902 903 904
                                    &data->scsi.bus, def,
                                    _("no SCSI bus ID supplied for '%s'"),
                                    _("invalid SCSI bus ID supplied for '%s'")) < 0)
        goto out;

905
    if (virNodeDevCapsDefParseULong("number(./target[1])", ctxt,
906 907 908 909 910
                                    &data->scsi.target, def,
                                    _("no SCSI target ID supplied for '%s'"),
                                    _("invalid SCSI target ID supplied for '%s'")) < 0)
        goto out;

911
    if (virNodeDevCapsDefParseULong("number(./lun[1])", ctxt,
912 913 914 915 916
                                    &data->scsi.lun, def,
                                    _("no SCSI LUN ID supplied for '%s'"),
                                    _("invalid SCSI LUN ID supplied for '%s'")) < 0)
        goto out;

917
    data->scsi.type = virXPathString("string(./type[1])", ctxt);
918 919

    ret = 0;
920
 out:
921 922 923 924
    ctxt->node = orignode;
    return ret;
}

D
David Allan 已提交
925 926

static int
927
virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt,
D
David Allan 已提交
928 929
                                virNodeDeviceDefPtr def,
                                xmlNodePtr node,
930
                                virNodeDevCapDataPtr data)
D
David Allan 已提交
931 932 933 934 935 936 937
{
    xmlNodePtr orignode;
    int ret = -1;

    orignode = ctxt->node;
    ctxt->node = node;

938
    data->scsi_target.name = virXPathString("string(./target[1])", ctxt);
D
David Allan 已提交
939
    if (!data->scsi_target.name) {
940 941 942
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no target name supplied for '%s'"),
                       def->name);
D
David Allan 已提交
943 944 945 946 947
        goto out;
    }

    ret = 0;

948
 out:
D
David Allan 已提交
949 950 951 952 953
    ctxt->node = orignode;
    return ret;
}


954
static int
955
virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt,
956 957
                              virNodeDeviceDefPtr def,
                              xmlNodePtr node,
958
                              virNodeDevCapDataPtr data,
959 960
                              int create,
                              const char *virt_type)
961
{
962
    xmlNodePtr orignode, *nodes = NULL;
963 964
    int ret = -1, n = 0;
    size_t i;
965
    char *type = NULL;
966 967 968 969

    orignode = ctxt->node;
    ctxt->node = node;

J
John Ferlan 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983
    if (create == EXISTING_DEVICE) {
        if (virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
                                        &data->scsi_host.host, def,
                                        _("no SCSI host ID supplied for '%s'"),
                                        _("invalid SCSI host ID supplied for '%s'")) < 0) {
            goto out;
        }
        /* Optional unique_id value */
        data->scsi_host.unique_id = -1;
        if (virNodeDevCapsDefParseIntOptional("number(./unique_id[1])", ctxt,
                                              &data->scsi_host.unique_id, def,
                                              _("invalid unique_id supplied for '%s'")) < 0) {
            goto out;
        }
984 985
    }

986
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
987
        goto out;
988

989
    for (i = 0; i < n; i++) {
990 991 992
        type = virXMLPropString(nodes[i], "type");

        if (!type) {
993 994 995
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing SCSI host capability type for '%s'"),
                           def->name);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
            goto out;
        }

        if (STREQ(type, "vport_ops")) {

            data->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS;

        } else if (STREQ(type, "fc_host")) {

            xmlNodePtr orignode2;

            data->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST;

            orignode2 = ctxt->node;
            ctxt->node = nodes[i];

1012
            if (virNodeDevCapsDefParseString("string(./wwnn[1])",
1013
                                             ctxt,
1014 1015
                                             &data->scsi_host.wwnn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwnn, virt_type) < 0) {
1016 1017 1018 1019
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWNN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
1020 1021
                    goto out;
                }
1022 1023
            }

1024
            if (virNodeDevCapsDefParseString("string(./wwpn[1])",
1025
                                             ctxt,
1026 1027
                                             &data->scsi_host.wwpn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwpn, virt_type) < 0) {
1028 1029 1030 1031
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWPN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
1032 1033
                    goto out;
                }
1034 1035 1036 1037 1038
            }

            ctxt->node = orignode2;

        } else {
1039 1040 1041
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown SCSI host capability type '%s' for '%s'"),
                           type, def->name);
1042 1043 1044 1045 1046
            goto out;
        }

        VIR_FREE(type);
    }
1047 1048

    ret = 0;
1049

1050
 out:
1051
    VIR_FREE(type);
1052
    ctxt->node = orignode;
1053
    VIR_FREE(nodes);
1054 1055 1056
    return ret;
}

1057

1058
static int
1059
virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt,
1060 1061
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
1062
                         virNodeDevCapDataPtr data)
1063
{
1064
    xmlNodePtr orignode, lnk;
1065 1066
    size_t i = -1;
    int ret = -1, n = -1;
1067
    char *tmp = NULL;
1068
    xmlNodePtr *nodes = NULL;
1069 1070 1071 1072

    orignode = ctxt->node;
    ctxt->node = node;

1073
    data->net.ifname = virXPathString("string(./interface[1])", ctxt);
1074
    if (!data->net.ifname) {
1075 1076 1077
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no network interface supplied for '%s'"),
                       def->name);
1078 1079 1080
        goto out;
    }

1081
    data->net.address = virXPathString("string(./address[1])", ctxt);
1082

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
        goto out;

    if (n > 0) {
        if (!(data->net.features = virBitmapNew(VIR_NET_DEV_FEAT_LAST)))
            goto out;
    }

    for (i = 0; i < n; i++) {
        int val;
        if (!(tmp = virXMLPropString(nodes[i], "name"))) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing network device feature name"));
            goto out;
        }

        if ((val = virNetDevFeatureTypeFromString(tmp)) < 0) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("unknown network device feature '%s'"),
                           tmp);
            goto out;
        }
        ignore_value(virBitmapSetBit(data->net.features, val));
1106
        VIR_FREE(tmp);
1107 1108
    }

1109 1110
    data->net.subtype = VIR_NODE_DEV_CAP_NET_LAST;

1111
    tmp = virXPathString("string(./capability/@type)", ctxt);
1112 1113 1114 1115
    if (tmp) {
        int val = virNodeDevNetCapTypeFromString(tmp);
        VIR_FREE(tmp);
        if (val < 0) {
1116
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1117 1118
                           _("invalid network type supplied for '%s'"),
                           def->name);
1119 1120 1121 1122 1123
            goto out;
        }
        data->net.subtype = val;
    }

1124 1125 1126 1127
    lnk = virXPathNode("./link", ctxt);
    if (lnk && virInterfaceLinkParseXML(lnk, &data->net.lnk) < 0)
        goto out;

1128
    ret = 0;
1129
 out:
1130
    ctxt->node = orignode;
1131 1132
    VIR_FREE(nodes);
    VIR_FREE(tmp);
1133 1134 1135 1136
    return ret;
}

static int
1137
virNodeDevCapUSBInterfaceParseXML(xmlXPathContextPtr ctxt,
1138 1139
                                  virNodeDeviceDefPtr def,
                                  xmlNodePtr node,
1140
                                  virNodeDevCapDataPtr data)
1141 1142 1143 1144 1145 1146 1147
{
    xmlNodePtr orignode;
    int ret = -1;

    orignode = ctxt->node;
    ctxt->node = node;

1148
    if (virNodeDevCapsDefParseULong("number(./number[1])", ctxt,
1149 1150 1151 1152 1153
                                    &data->usb_if.number, def,
                                    _("no USB interface number supplied for '%s'"),
                                    _("invalid USB interface number supplied for '%s'")) < 0)
        goto out;

1154
    if (virNodeDevCapsDefParseULong("number(./class[1])", ctxt,
1155 1156 1157 1158 1159
                                    &data->usb_if._class, def,
                                    _("no USB interface class supplied for '%s'"),
                                    _("invalid USB interface class supplied for '%s'")) < 0)
        goto out;

1160
    if (virNodeDevCapsDefParseULong("number(./subclass[1])", ctxt,
1161 1162 1163 1164 1165
                                    &data->usb_if.subclass, def,
                                    _("no USB interface subclass supplied for '%s'"),
                                    _("invalid USB interface subclass supplied for '%s'")) < 0)
        goto out;

1166
    if (virNodeDevCapsDefParseULong("number(./protocol[1])", ctxt,
1167 1168 1169 1170 1171
                                    &data->usb_if.protocol, def,
                                    _("no USB interface protocol supplied for '%s'"),
                                    _("invalid USB interface protocol supplied for '%s'")) < 0)
        goto out;

1172
    data->usb_if.description = virXPathString("string(./description[1])", ctxt);
1173 1174

    ret = 0;
1175
 out:
1176 1177 1178 1179 1180
    ctxt->node = orignode;
    return ret;
}

static int
1181
virNodeDevCapsDefParseHexId(const char *xpath,
1182 1183 1184 1185 1186 1187 1188 1189 1190
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

1191
    ret = virXPathULongHex(xpath, ctxt, &val);
1192
    if (ret < 0) {
1193 1194 1195
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
1196 1197 1198 1199 1200 1201 1202 1203
        return -1;
    }

    *value = val;
    return 0;
}

static int
1204
virNodeDevCapUSBDevParseXML(xmlXPathContextPtr ctxt,
1205 1206
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1207
                            virNodeDevCapDataPtr data)
1208 1209 1210 1211 1212 1213 1214
{
    xmlNodePtr orignode;
    int ret = -1;

    orignode = ctxt->node;
    ctxt->node = node;

1215
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
1216 1217 1218 1219 1220
                                    &data->usb_dev.bus, def,
                                    _("no USB bus number supplied for '%s'"),
                                    _("invalid USB bus number supplied for '%s'")) < 0)
        goto out;

1221
    if (virNodeDevCapsDefParseULong("number(./device[1])", ctxt,
1222 1223 1224 1225 1226
                                    &data->usb_dev.device, def,
                                    _("no USB device number supplied for '%s'"),
                                    _("invalid USB device number supplied for '%s'")) < 0)
        goto out;

1227
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1228 1229 1230 1231 1232
                                    &data->usb_dev.vendor, def,
                                    _("no USB vendor ID supplied for '%s'"),
                                    _("invalid USB vendor ID supplied for '%s'")) < 0)
        goto out;

1233
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1234 1235 1236 1237 1238
                                    &data->usb_dev.product, def,
                                    _("no USB product ID supplied for '%s'"),
                                    _("invalid USB product ID supplied for '%s'")) < 0)
        goto out;

1239 1240
    data->usb_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->usb_dev.product_name = virXPathString("string(./product[1])", ctxt);
1241 1242

    ret = 0;
1243
 out:
1244 1245 1246 1247
    ctxt->node = orignode;
    return ret;
}

1248
static int
1249
virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt,
1250
                                      xmlNodePtr iommuGroupNode,
1251
                                      virNodeDevCapDataPtr data)
1252 1253 1254 1255
{
    xmlNodePtr origNode = ctxt->node;
    xmlNodePtr *addrNodes = NULL;
    char *numberStr = NULL;
1256 1257
    int nAddrNodes, ret = -1;
    size_t i;
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    virPCIDeviceAddressPtr pciAddr = NULL;

    ctxt->node = iommuGroupNode;

    numberStr = virXMLPropString(iommuGroupNode, "number");
    if (!numberStr) {
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("missing iommuGroup number attribute"));
        goto cleanup;
    }
    if (virStrToLong_ui(numberStr, NULL, 10,
                        &data->pci_dev.iommuGroupNumber) < 0) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("invalid iommuGroup number attribute '%s'"),
                       numberStr);
        goto cleanup;
    }

    if ((nAddrNodes = virXPathNodeSet("./address", ctxt, &addrNodes)) < 0)
        goto cleanup;

1279
    for (i = 0; i < nAddrNodes; i++) {
1280
        virPCIDeviceAddress addr = {0};
1281
        if (virPCIDeviceAddressParseXML(addrNodes[i], &addr) < 0)
1282
            goto cleanup;
1283
        if (VIR_ALLOC(pciAddr) < 0)
1284 1285 1286 1287 1288 1289 1290
            goto cleanup;
        pciAddr->domain = addr.domain;
        pciAddr->bus = addr.bus;
        pciAddr->slot = addr.slot;
        pciAddr->function = addr.function;
        if (VIR_APPEND_ELEMENT(data->pci_dev.iommuGroupDevices,
                               data->pci_dev.nIommuGroupDevices,
1291
                               pciAddr) < 0)
1292 1293 1294 1295
            goto cleanup;
    }

    ret = 0;
1296
 cleanup:
1297
    ctxt->node = origNode;
1298
    VIR_FREE(numberStr);
1299 1300 1301 1302 1303
    VIR_FREE(addrNodes);
    VIR_FREE(pciAddr);
    return ret;
}

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 1378 1379 1380 1381 1382 1383
static int
virPCIEDeviceInfoLinkParseXML(xmlXPathContextPtr ctxt,
                              xmlNodePtr linkNode,
                              virPCIELinkPtr lnk)
{
    xmlNodePtr origNode = ctxt->node;
    int ret = -1, speed;
    char *speedStr = NULL, *portStr = NULL;

    ctxt->node = linkNode;

    if (virXPathUInt("number(./@width)", ctxt, &lnk->width) < 0) {
        virReportError(VIR_ERR_XML_DETAIL, "%s",
                       _("mandatory attribute 'width' is missing or malformed"));
        goto cleanup;
    }

    if ((speedStr = virXPathString("string(./@speed)", ctxt))) {
        if ((speed = virPCIELinkSpeedTypeFromString(speedStr)) < 0) {
            virReportError(VIR_ERR_XML_DETAIL,
                           _("malformed 'speed' attribute: %s"),
                           speedStr);
            goto cleanup;
        }
        lnk->speed = speed;
    }

    if ((portStr = virXPathString("string(./@port)", ctxt))) {
        if (virStrToLong_i(portStr, NULL, 10, &lnk->port) < 0) {
            virReportError(VIR_ERR_XML_DETAIL,
                           _("malformed 'port' attribute: %s"),
                           portStr);
            goto cleanup;
        }
    } else {
        lnk->port = -1;
    }

    ret = 0;
 cleanup:
    VIR_FREE(portStr);
    VIR_FREE(speedStr);
    ctxt->node = origNode;
    return ret;
}

static int
virPCIEDeviceInfoParseXML(xmlXPathContextPtr ctxt,
                          xmlNodePtr pciExpressNode,
                          virPCIEDeviceInfoPtr pci_express)
{
    xmlNodePtr lnk, origNode = ctxt->node;
    int ret = -1;

    ctxt->node = pciExpressNode;

    if ((lnk = virXPathNode("./link[@validity='cap']", ctxt))) {
        if (VIR_ALLOC(pci_express->link_cap) < 0)
            goto cleanup;

        if (virPCIEDeviceInfoLinkParseXML(ctxt, lnk,
                                          pci_express->link_cap) < 0)
            goto cleanup;
    }

    if ((lnk = virXPathNode("./link[@validity='sta']", ctxt))) {
        if (VIR_ALLOC(pci_express->link_sta) < 0)
            goto cleanup;

        if (virPCIEDeviceInfoLinkParseXML(ctxt, lnk,
                                          pci_express->link_sta) < 0)
            goto cleanup;
    }

    ret = 0;
 cleanup:
    ctxt->node = origNode;
    return ret;
}

1384

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static int
virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt,
                                xmlNodePtr node,
                                virNodeDevCapDataPtr data)
{
    char *maxFuncsStr = virXMLPropString(node, "maxCount");
    char *type = virXMLPropString(node, "type");
    xmlNodePtr *addresses = NULL;
    xmlNodePtr orignode = ctxt->node;
    int ret = -1;
    size_t i = 0;

    ctxt->node = node;

    if (!type) {
        virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing capability type"));
        goto out;
    }

    if (STREQ(type, "phys_function")) {
        xmlNodePtr address = virXPathNode("./address[1]", ctxt);

        if (VIR_ALLOC(data->pci_dev.physical_function) < 0)
            goto out;

        if (!address) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing address in 'phys_function' capability"));
            goto out;
        }

        if (virPCIDeviceAddressParseXML(address,
                                        data->pci_dev.physical_function) < 0)
            goto out;
1419 1420

        data->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;
1421
    } else if (STREQ(type, "virt_functions")) {
1422 1423 1424 1425
        int naddresses;

        if ((naddresses = virXPathNodeSet("./address", ctxt, &addresses)) < 0)
            goto out;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472

        if (maxFuncsStr &&
            virStrToLong_uip(maxFuncsStr, NULL, 10,
                             &data->pci_dev.max_virtual_functions) < 0) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Malformed 'maxCount' parameter"));
            goto out;
        }

        if (VIR_ALLOC_N(data->pci_dev.virtual_functions, naddresses) < 0)
            goto out;

        for (i = 0; i < naddresses; i++) {
            virPCIDeviceAddressPtr addr = NULL;

            if (VIR_ALLOC(addr) < 0)
                goto out;

            if (virPCIDeviceAddressParseXML(addresses[i], addr) < 0) {
                VIR_FREE(addr);
                goto out;
            }

            if (VIR_APPEND_ELEMENT(data->pci_dev.virtual_functions,
                                   data->pci_dev.num_virtual_functions,
                                   addr) < 0)
                goto out;
        }

        data->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;
    } else {
        int hdrType = virPCIHeaderTypeFromString(type);

        if (hdrType > 0 && !data->pci_dev.hdrType)
            data->pci_dev.hdrType = hdrType;
    }

    ret = 0;
 out:
    VIR_FREE(addresses);
    VIR_FREE(maxFuncsStr);
    VIR_FREE(type);
    ctxt->node = orignode;
    return ret;
}


1473
static int
1474
virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt,
1475 1476
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1477
                            virNodeDevCapDataPtr data)
1478
{
1479
    xmlNodePtr orignode, iommuGroupNode, pciExpress;
1480 1481
    xmlNodePtr *nodes = NULL;
    int n = 0;
1482
    int ret = -1;
1483
    virPCIEDeviceInfoPtr pci_express = NULL;
1484
    char *tmp = NULL;
1485
    size_t i = 0;
1486 1487 1488 1489

    orignode = ctxt->node;
    ctxt->node = node;

1490
    if (virNodeDevCapsDefParseULong("number(./domain[1])", ctxt,
1491 1492 1493 1494 1495
                                    &data->pci_dev.domain, def,
                                    _("no PCI domain ID supplied for '%s'"),
                                    _("invalid PCI domain ID supplied for '%s'")) < 0)
        goto out;

1496
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
1497 1498 1499 1500 1501
                                    &data->pci_dev.bus, def,
                                    _("no PCI bus ID supplied for '%s'"),
                                    _("invalid PCI bus ID supplied for '%s'")) < 0)
        goto out;

1502
    if (virNodeDevCapsDefParseULong("number(./slot[1])", ctxt,
1503 1504 1505 1506 1507
                                    &data->pci_dev.slot, def,
                                    _("no PCI slot ID supplied for '%s'"),
                                    _("invalid PCI slot ID supplied for '%s'")) < 0)
        goto out;

1508
    if (virNodeDevCapsDefParseULong("number(./function[1])", ctxt,
1509 1510 1511 1512 1513
                                    &data->pci_dev.function, def,
                                    _("no PCI function ID supplied for '%s'"),
                                    _("invalid PCI function ID supplied for '%s'")) < 0)
        goto out;

1514
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1515 1516 1517 1518 1519
                                    &data->pci_dev.vendor, def,
                                    _("no PCI vendor ID supplied for '%s'"),
                                    _("invalid PCI vendor ID supplied for '%s'")) < 0)
        goto out;

1520
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1521 1522 1523 1524 1525
                                    &data->pci_dev.product, def,
                                    _("no PCI product ID supplied for '%s'"),
                                    _("invalid PCI product ID supplied for '%s'")) < 0)
        goto out;

1526 1527
    data->pci_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->pci_dev.product_name = virXPathString("string(./product[1])", ctxt);
1528

1529 1530 1531 1532 1533 1534 1535 1536 1537
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
        goto out;

    for (i = 0; i < n; i++) {
        if (virNodeDevPCICapabilityParseXML(ctxt, nodes[i], data) < 0)
            goto out;
    }
    VIR_FREE(nodes);

1538
    if ((iommuGroupNode = virXPathNode("./iommuGroup[1]", ctxt))) {
1539
        if (virNodeDevCapPCIDevIommuGroupParseXML(ctxt, iommuGroupNode,
1540 1541 1542 1543
                                                  data) < 0) {
            goto out;
        }
    }
1544

1545 1546
    /* The default value is -1 since zero is valid NUMA node number */
    data->pci_dev.numa_node = -1;
1547 1548 1549 1550 1551
    if (virNodeDevCapsDefParseIntOptional("number(./numa[1]/@node)", ctxt,
                                          &data->pci_dev.numa_node, def,
                                          _("invalid NUMA node ID supplied for '%s'")) < 0)
        goto out;

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    if ((pciExpress = virXPathNode("./pci-express[1]", ctxt))) {
        if (VIR_ALLOC(pci_express) < 0)
            goto out;

        if (virPCIEDeviceInfoParseXML(ctxt, pciExpress, pci_express) < 0)
            goto out;

        data->pci_dev.pci_express = pci_express;
        pci_express = NULL;
        data->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCIE;
    }

1564
    ret = 0;
1565
 out:
1566
    VIR_FREE(nodes);
1567
    VIR_FREE(tmp);
1568
    virPCIEDeviceInfoFree(pci_express);
1569 1570 1571 1572 1573
    ctxt->node = orignode;
    return ret;
}

static int
1574
virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt,
1575 1576
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1577
                            virNodeDevCapDataPtr data)
1578 1579 1580 1581 1582 1583 1584 1585
{
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

    orignode = ctxt->node;
    ctxt->node = node;

1586
    data->system.product_name = virXPathString("string(./product[1])", ctxt);
1587

1588 1589 1590
    data->system.hardware.vendor_name = virXPathString("string(./hardware/vendor[1])", ctxt);
    data->system.hardware.version     = virXPathString("string(./hardware/version[1])", ctxt);
    data->system.hardware.serial      = virXPathString("string(./hardware/serial[1])", ctxt);
1591

1592
    tmp = virXPathString("string(./hardware/uuid[1])", ctxt);
1593
    if (!tmp) {
1594 1595
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no system UUID supplied for '%s'"), def->name);
1596 1597 1598 1599
        goto out;
    }

    if (virUUIDParse(tmp, data->system.hardware.uuid) < 0) {
1600 1601
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("malformed uuid element for '%s'"), def->name);
1602 1603 1604 1605 1606
        VIR_FREE(tmp);
        goto out;
    }
    VIR_FREE(tmp);

1607 1608 1609
    data->system.firmware.vendor_name  = virXPathString("string(./firmware/vendor[1])", ctxt);
    data->system.firmware.version      = virXPathString("string(./firmware/version[1])", ctxt);
    data->system.firmware.release_date = virXPathString("string(./firmware/release_date[1])", ctxt);
1610 1611

    ret = 0;
1612
 out:
1613 1614 1615 1616 1617
    ctxt->node = orignode;
    return ret;
}

static virNodeDevCapsDefPtr
1618
virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt,
1619
                          virNodeDeviceDefPtr def,
1620
                          xmlNodePtr node,
1621 1622
                          int create,
                          const char *virt_type)
1623 1624 1625
{
    virNodeDevCapsDefPtr caps;
    char *tmp;
1626
    int val, ret = -1;
1627

1628
    if (VIR_ALLOC(caps) < 0)
1629 1630 1631 1632
        return NULL;

    tmp = virXMLPropString(node, "type");
    if (!tmp) {
1633 1634
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing capability type"));
1635 1636 1637 1638
        goto error;
    }

    if ((val = virNodeDevCapTypeFromString(tmp)) < 0) {
1639
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1640
                       _("unknown capability type '%s'"), tmp);
1641 1642 1643
        VIR_FREE(tmp);
        goto error;
    }
1644
    caps->data.type = val;
1645 1646
    VIR_FREE(tmp);

1647
    switch (caps->data.type) {
1648
    case VIR_NODE_DEV_CAP_SYSTEM:
1649
        ret = virNodeDevCapSystemParseXML(ctxt, def, node, &caps->data);
1650 1651
        break;
    case VIR_NODE_DEV_CAP_PCI_DEV:
1652
        ret = virNodeDevCapPCIDevParseXML(ctxt, def, node, &caps->data);
1653 1654
        break;
    case VIR_NODE_DEV_CAP_USB_DEV:
1655
        ret = virNodeDevCapUSBDevParseXML(ctxt, def, node, &caps->data);
1656 1657
        break;
    case VIR_NODE_DEV_CAP_USB_INTERFACE:
1658
        ret = virNodeDevCapUSBInterfaceParseXML(ctxt, def, node, &caps->data);
1659 1660
        break;
    case VIR_NODE_DEV_CAP_NET:
1661
        ret = virNodeDevCapNetParseXML(ctxt, def, node, &caps->data);
1662 1663
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1664
        ret = virNodeDevCapSCSIHostParseXML(ctxt, def, node,
1665 1666 1667
                                            &caps->data,
                                            create,
                                            virt_type);
1668
        break;
D
David Allan 已提交
1669
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
1670
        ret = virNodeDevCapSCSITargetParseXML(ctxt, def, node, &caps->data);
D
David Allan 已提交
1671
        break;
1672
    case VIR_NODE_DEV_CAP_SCSI:
1673
        ret = virNodeDevCapSCSIParseXML(ctxt, def, node, &caps->data);
1674 1675
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1676
        ret = virNodeDevCapStorageParseXML(ctxt, def, node, &caps->data);
1677
        break;
1678 1679 1680 1681
    case VIR_NODE_DEV_CAP_FC_HOST:
    case VIR_NODE_DEV_CAP_VPORTS:
    case VIR_NODE_DEV_CAP_SCSI_GENERIC:
    case VIR_NODE_DEV_CAP_LAST:
1682 1683
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown capability type '%d' for '%s'"),
1684
                       caps->data.type, def->name);
1685 1686 1687 1688 1689 1690 1691 1692
        ret = -1;
        break;
    }

    if (ret < 0)
        goto error;
    return caps;

1693
 error:
1694 1695 1696 1697 1698
    virNodeDevCapsDefFree(caps);
    return NULL;
}

static virNodeDeviceDefPtr
1699 1700 1701
virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt,
                         int create,
                         const char *virt_type)
1702 1703 1704 1705
{
    virNodeDeviceDefPtr def;
    virNodeDevCapsDefPtr *next_cap;
    xmlNodePtr *nodes;
1706 1707
    int n;
    size_t i;
1708

1709
    if (VIR_ALLOC(def) < 0)
1710 1711 1712
        return NULL;

    /* Extract device name */
1713
    if (create == EXISTING_DEVICE) {
1714
        def->name = virXPathString("string(./name[1])", ctxt);
1715 1716

        if (!def->name) {
1717
            virReportError(VIR_ERR_NO_NAME, NULL);
1718 1719
            goto error;
        }
1720
    } else {
1721
        if (VIR_STRDUP(def->name, "new device") < 0)
1722
            goto error;
1723 1724 1725
    }

    /* Extract device parent, if any */
1726
    def->parent = virXPathString("string(./parent[1])", ctxt);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
    def->parent_wwnn = virXPathString("string(./parent[1]/@wwnn)", ctxt);
    def->parent_wwpn = virXPathString("string(./parent[1]/@wwpn)", ctxt);
    if ((def->parent_wwnn && !def->parent_wwpn) ||
        (!def->parent_wwnn && def->parent_wwpn)) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("must supply both wwnn and wwpn for parent"));
        goto error;
    }
    def->parent_fabric_wwn = virXPathString("string(./parent[1]/@fabric_wwn)",
                                            ctxt);
1737 1738 1739

    /* Parse device capabilities */
    nodes = NULL;
1740
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
1741 1742 1743
        goto error;

    if (n == 0) {
1744 1745 1746
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no device capabilities for '%s'"),
                       def->name);
1747 1748 1749 1750
        goto error;
    }

    next_cap = &def->caps;
1751
    for (i = 0; i < n; i++) {
1752 1753 1754 1755
        *next_cap = virNodeDevCapsDefParseXML(ctxt, def,
                                              nodes[i],
                                              create,
                                              virt_type);
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
        if (!*next_cap) {
            VIR_FREE(nodes);
            goto error;
        }

        next_cap = &(*next_cap)->next;
    }
    VIR_FREE(nodes);

    return def;

 error:
    virNodeDeviceDefFree(def);
    return NULL;
}

1772
virNodeDeviceDefPtr
1773
virNodeDeviceDefParseNode(xmlDocPtr xml,
1774
                          xmlNodePtr root,
1775 1776
                          int create,
                          const char *virt_type)
1777 1778 1779 1780 1781
{
    xmlXPathContextPtr ctxt = NULL;
    virNodeDeviceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "device")) {
1782 1783 1784 1785
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s> "
                         "expecting <device>"),
                       root->name);
1786 1787 1788 1789 1790
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1791
        virReportOOMError();
1792 1793 1794 1795
        goto cleanup;
    }

    ctxt->node = root;
1796
    def = virNodeDeviceDefParseXML(ctxt, create, virt_type);
1797

1798
 cleanup:
1799 1800 1801 1802
    xmlXPathFreeContext(ctxt);
    return def;
}

1803
static virNodeDeviceDefPtr
1804
virNodeDeviceDefParse(const char *str,
1805
                      const char *filename,
1806 1807
                      int create,
                      const char *virt_type)
1808
{
J
Jiri Denemark 已提交
1809
    xmlDocPtr xml;
1810 1811
    virNodeDeviceDefPtr def = NULL;

1812
    if ((xml = virXMLParse(filename, str, _("(node_device_definition)")))) {
1813 1814
        def = virNodeDeviceDefParseNode(xml, xmlDocGetRootElement(xml),
                                        create, virt_type);
J
Jiri Denemark 已提交
1815
        xmlFreeDoc(xml);
1816 1817
    }

1818 1819 1820
    return def;
}

1821
virNodeDeviceDefPtr
1822
virNodeDeviceDefParseString(const char *str,
1823 1824
                            int create,
                            const char *virt_type)
1825
{
1826
    return virNodeDeviceDefParse(str, NULL, create, virt_type);
1827 1828 1829
}

virNodeDeviceDefPtr
1830
virNodeDeviceDefParseFile(const char *filename,
1831 1832
                          int create,
                          const char *virt_type)
1833
{
1834
    return virNodeDeviceDefParse(NULL, filename, create, virt_type);
1835 1836
}

1837 1838 1839 1840
/*
 * Return fc_host dev's WWNN and WWPN
 */
int
1841
virNodeDeviceGetWWNs(virNodeDeviceDefPtr def,
1842 1843 1844 1845
                     char **wwnn,
                     char **wwpn)
{
    virNodeDevCapsDefPtr cap = NULL;
1846
    int ret = -1;
1847 1848 1849

    cap = def->caps;
    while (cap != NULL) {
1850
        if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST &&
1851
            cap->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
1852 1853 1854 1855 1856 1857
            if (VIR_STRDUP(*wwnn, cap->data.scsi_host.wwnn) < 0 ||
                VIR_STRDUP(*wwpn, cap->data.scsi_host.wwpn) < 0) {
                /* Free the other one, if allocated... */
                VIR_FREE(*wwnn);
                goto cleanup;
            }
1858 1859 1860 1861 1862 1863 1864
            break;
        }

        cap = cap->next;
    }

    if (cap == NULL) {
1865 1866
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Device is not a fibre channel HBA"));
1867
        goto cleanup;
1868 1869
    }

1870
    ret = 0;
1871
 cleanup:
1872 1873 1874 1875 1876 1877
    return ret;
}

/*
 * Return the NPIV dev's parent device name
 */
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
/* virNodeDeviceFindFCParentHost:
 * @parent: Pointer to node device object
 * @parent_host: Pointer to return parent host number
 *
 * Search the capabilities for the device to find the FC capabilities
 * in order to set the parent_host value.
 *
 * Returns:
 *   0 on success with parent_host set, -1 otherwise;
 */
static int
virNodeDeviceFindFCParentHost(virNodeDeviceObjPtr parent,
                              int *parent_host)
{
    virNodeDevCapsDefPtr cap = virNodeDeviceFindVPORTCapDef(parent);

    if (!cap) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parent device %s is not capable "
                         "of vport operations"),
                       parent->def->name);
        return -1;
    }

    *parent_host = cap->data.scsi_host.host;
    return 0;
}


1907
int
E
Eric Blake 已提交
1908
virNodeDeviceGetParentHost(virNodeDeviceObjListPtr devs,
1909 1910 1911 1912 1913
                           const char *dev_name,
                           const char *parent_name,
                           int *parent_host)
{
    virNodeDeviceObjPtr parent = NULL;
1914
    int ret;
1915

1916
    if (!(parent = virNodeDeviceFindByName(devs, parent_name))) {
1917 1918 1919
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find parent device for '%s'"),
                       dev_name);
1920
        return -1;
1921 1922
    }

1923
    ret = virNodeDeviceFindFCParentHost(parent, parent_host);
1924 1925 1926 1927 1928

    virNodeDeviceObjUnlock(parent);

    return ret;
}
1929

1930

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
int
virNodeDeviceGetParentHostByWWNs(virNodeDeviceObjListPtr devs,
                                 const char *dev_name,
                                 const char *parent_wwnn,
                                 const char *parent_wwpn,
                                 int *parent_host)
{
    virNodeDeviceObjPtr parent = NULL;
    int ret;

    if (!(parent = virNodeDeviceFindByWWNs(devs, parent_wwnn, parent_wwpn))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find parent device for '%s'"),
                       dev_name);
        return -1;
    }

    ret = virNodeDeviceFindFCParentHost(parent, parent_host);

    virNodeDeviceObjUnlock(parent);

    return ret;
}


int
virNodeDeviceGetParentHostByFabricWWN(virNodeDeviceObjListPtr devs,
                                      const char *dev_name,
                                      const char *parent_fabric_wwn,
                                      int *parent_host)
{
    virNodeDeviceObjPtr parent = NULL;
    int ret;

    if (!(parent = virNodeDeviceFindByFabricWWN(devs, parent_fabric_wwn))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find parent device for '%s'"),
                       dev_name);
        return -1;
    }

    ret = virNodeDeviceFindFCParentHost(parent, parent_host);

    virNodeDeviceObjUnlock(parent);

    return ret;
}


1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
int
virNodeDeviceFindVportParentHost(virNodeDeviceObjListPtr devs,
                                 int *parent_host)
{
    virNodeDeviceObjPtr parent = NULL;
    const char *cap = virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_VPORTS);
    int ret;

    if (!(parent = virNodeDeviceFindByCap(devs, cap))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not find any vport capable device"));
        return -1;
    }

    ret = virNodeDeviceFindFCParentHost(parent, parent_host);

    virNodeDeviceObjUnlock(parent);

    return ret;
}


2002 2003
void virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
{
2004
    size_t i = 0;
2005
    virNodeDevCapDataPtr data = &caps->data;
2006

2007
    switch (caps->data.type) {
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
    case VIR_NODE_DEV_CAP_SYSTEM:
        VIR_FREE(data->system.product_name);
        VIR_FREE(data->system.hardware.vendor_name);
        VIR_FREE(data->system.hardware.version);
        VIR_FREE(data->system.hardware.serial);
        VIR_FREE(data->system.firmware.vendor_name);
        VIR_FREE(data->system.firmware.version);
        VIR_FREE(data->system.firmware.release_date);
        break;
    case VIR_NODE_DEV_CAP_PCI_DEV:
        VIR_FREE(data->pci_dev.product_name);
        VIR_FREE(data->pci_dev.vendor_name);
2020
        VIR_FREE(data->pci_dev.physical_function);
2021
        for (i = 0; i < data->pci_dev.num_virtual_functions; i++)
2022
            VIR_FREE(data->pci_dev.virtual_functions[i]);
2023
        VIR_FREE(data->pci_dev.virtual_functions);
2024
        for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++)
2025
            VIR_FREE(data->pci_dev.iommuGroupDevices[i]);
2026
        VIR_FREE(data->pci_dev.iommuGroupDevices);
2027
        virPCIEDeviceInfoFree(data->pci_dev.pci_express);
2028 2029 2030 2031 2032 2033 2034 2035 2036
        break;
    case VIR_NODE_DEV_CAP_USB_DEV:
        VIR_FREE(data->usb_dev.product_name);
        VIR_FREE(data->usb_dev.vendor_name);
        break;
    case VIR_NODE_DEV_CAP_USB_INTERFACE:
        VIR_FREE(data->usb_if.description);
        break;
    case VIR_NODE_DEV_CAP_NET:
2037
        VIR_FREE(data->net.ifname);
2038
        VIR_FREE(data->net.address);
2039 2040
        virBitmapFree(data->net.features);
        data->net.features = NULL;
2041 2042
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
2043 2044
        VIR_FREE(data->scsi_host.wwnn);
        VIR_FREE(data->scsi_host.wwpn);
O
Osier Yang 已提交
2045
        VIR_FREE(data->scsi_host.fabric_wwn);
2046
        break;
D
David Allan 已提交
2047 2048 2049
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
        VIR_FREE(data->scsi_target.name);
        break;
2050 2051 2052 2053
    case VIR_NODE_DEV_CAP_SCSI:
        VIR_FREE(data->scsi.type);
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
2054
        VIR_FREE(data->storage.block);
2055 2056 2057 2058
        VIR_FREE(data->storage.bus);
        VIR_FREE(data->storage.drive_type);
        VIR_FREE(data->storage.model);
        VIR_FREE(data->storage.vendor);
2059
        VIR_FREE(data->storage.serial);
2060
        VIR_FREE(data->storage.media_label);
2061
        break;
2062 2063 2064
    case VIR_NODE_DEV_CAP_SCSI_GENERIC:
        VIR_FREE(data->sg.path);
        break;
2065 2066
    case VIR_NODE_DEV_CAP_FC_HOST:
    case VIR_NODE_DEV_CAP_VPORTS:
2067 2068 2069 2070 2071 2072 2073 2074
    case VIR_NODE_DEV_CAP_LAST:
        /* This case is here to shutup the compiler */
        break;
    }

    VIR_FREE(caps);
}

D
Daniel P. Berrange 已提交
2075

2076 2077
void virNodeDeviceObjLock(virNodeDeviceObjPtr obj)
{
2078
    virMutexLock(&obj->lock);
2079 2080 2081 2082
}

void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj)
{
2083
    virMutexUnlock(&obj->lock);
D
Daniel P. Berrange 已提交
2084
}
2085 2086 2087 2088 2089 2090 2091 2092

static bool
virNodeDeviceCapMatch(virNodeDeviceObjPtr devobj,
                      int type)
{
    virNodeDevCapsDefPtr cap = NULL;

    for (cap = devobj->def->caps; cap; cap = cap->next) {
2093
        if (type == cap->data.type)
2094
            return true;
2095

2096
        if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
2097
            if (type == VIR_NODE_DEV_CAP_FC_HOST &&
2098 2099 2100 2101
                (cap->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST))
                return true;

2102
            if (type == VIR_NODE_DEV_CAP_VPORTS &&
2103 2104 2105 2106
                (cap->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS))
                return true;
        }
2107 2108 2109 2110 2111
    }

    return false;
}

2112 2113
#define MATCH(FLAG) ((flags & (VIR_CONNECT_LIST_NODE_DEVICES_CAP_ ## FLAG)) && \
                     virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_ ## FLAG))
2114 2115 2116 2117 2118
static bool
virNodeDeviceMatch(virNodeDeviceObjPtr devobj,
                   unsigned int flags)
{
    /* filter by cap type */
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
    if (flags & VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP) {
        if (!(MATCH(SYSTEM)        ||
              MATCH(PCI_DEV)       ||
              MATCH(USB_DEV)       ||
              MATCH(USB_INTERFACE) ||
              MATCH(NET)           ||
              MATCH(SCSI_HOST)     ||
              MATCH(SCSI_TARGET)   ||
              MATCH(SCSI)          ||
              MATCH(STORAGE)       ||
              MATCH(FC_HOST)       ||
              MATCH(VPORTS)        ||
              MATCH(SCSI_GENERIC)))
2132 2133 2134 2135 2136 2137 2138 2139
            return false;
    }

    return true;
}
#undef MATCH

int
2140 2141 2142 2143 2144
virNodeDeviceObjListExport(virConnectPtr conn,
                           virNodeDeviceObjList devobjs,
                           virNodeDevicePtr **devices,
                           virNodeDeviceObjListFilter filter,
                           unsigned int flags)
2145 2146 2147 2148 2149
{
    virNodeDevicePtr *tmp_devices = NULL;
    virNodeDevicePtr device = NULL;
    int ndevices = 0;
    int ret = -1;
2150
    size_t i;
2151

2152 2153
    if (devices && VIR_ALLOC_N(tmp_devices, devobjs.count + 1) < 0)
        goto cleanup;
2154 2155 2156 2157

    for (i = 0; i < devobjs.count; i++) {
        virNodeDeviceObjPtr devobj = devobjs.objs[i];
        virNodeDeviceObjLock(devobj);
2158 2159
        if ((!filter || filter(conn, devobj->def)) &&
            virNodeDeviceMatch(devobj, flags)) {
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
            if (devices) {
                if (!(device = virGetNodeDevice(conn,
                                                devobj->def->name))) {
                    virNodeDeviceObjUnlock(devobj);
                    goto cleanup;
                }
                tmp_devices[ndevices] = device;
            }
            ndevices++;
        }
        virNodeDeviceObjUnlock(devobj);
    }

    if (tmp_devices) {
        /* trim the array to the final size */
        ignore_value(VIR_REALLOC_N(tmp_devices, ndevices + 1));
        *devices = tmp_devices;
        tmp_devices = NULL;
    }

    ret = ndevices;

2182
 cleanup:
2183
    if (tmp_devices) {
2184 2185
        for (i = 0; i < ndevices; i++)
            virObjectUnref(tmp_devices[i]);
2186 2187 2188 2189 2190
    }

    VIR_FREE(tmp_devices);
    return ret;
}