node_device_conf.c 84.2 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
 */

#include <config.h>

#include <unistd.h>

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

41
#define VIR_FROM_THIS VIR_FROM_NODEDEV
42

43 44
VIR_LOG_INIT("conf.node_device_conf");

45 46
VIR_ENUM_IMPL(virNodeDevDevnode, VIR_NODE_DEV_DEVNODE_LAST,
              "dev",
47 48
              "link",
);
49

50 51 52 53 54 55 56
VIR_ENUM_IMPL(virNodeDevCap, VIR_NODE_DEV_CAP_LAST,
              "system",
              "pci",
              "usb_device",
              "usb",
              "net",
              "scsi_host",
D
David Allan 已提交
57
              "scsi_target",
58
              "scsi",
59 60
              "storage",
              "fc_host",
61
              "vports",
M
Marc-André Lureau 已提交
62
              "scsi_generic",
63 64
              "drm",
              "mdev_types",
B
Bjoern Walk 已提交
65
              "mdev",
66 67
              "ccw",
);
68 69 70

VIR_ENUM_IMPL(virNodeDevNetCap, VIR_NODE_DEV_CAP_NET_LAST,
              "80203",
71 72
              "80211",
);
73

M
Marc-André Lureau 已提交
74 75 76
VIR_ENUM_IMPL(virNodeDevDRM, VIR_NODE_DEV_DRM_LAST,
              "primary",
              "control",
77 78
              "render",
);
M
Marc-André Lureau 已提交
79

80
static int
81
virNodeDevCapsDefParseString(const char *xpath,
82
                             xmlXPathContextPtr ctxt,
83
                             char **string)
84 85 86
{
    char *s;

87
    if (!(s = virXPathString(xpath, ctxt)))
88 89 90 91 92 93
        return -1;

    *string = s;
    return 0;
}

94

95 96
void
virNodeDeviceDefFree(virNodeDeviceDefPtr def)
97 98 99 100 101 102 103 104
{
    virNodeDevCapsDefPtr caps;

    if (!def)
        return;

    VIR_FREE(def->name);
    VIR_FREE(def->parent);
105 106 107
    VIR_FREE(def->parent_wwnn);
    VIR_FREE(def->parent_wwpn);
    VIR_FREE(def->parent_fabric_wwn);
108
    VIR_FREE(def->driver);
109 110
    VIR_FREE(def->sysfs_path);
    VIR_FREE(def->parent_sysfs_path);
111 112
    VIR_FREE(def->devnode);
    virStringListFree(def->devlinks);
113 114 115 116 117 118 119 120 121 122 123 124

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

    VIR_FREE(def);
}


125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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");
}

143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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");
}

163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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
static void
virNodeDeviceCapSystemDefFormat(virBufferPtr buf,
                                const virNodeDevCapData *data)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];

    if (data->system.product_name)
        virBufferEscapeString(buf, "<product>%s</product>\n",
                              data->system.product_name);
    virBufferAddLit(buf, "<hardware>\n");
    virBufferAdjustIndent(buf, 2);
    if (data->system.hardware.vendor_name)
        virBufferEscapeString(buf, "<vendor>%s</vendor>\n",
                              data->system.hardware.vendor_name);
    if (data->system.hardware.version)
        virBufferEscapeString(buf, "<version>%s</version>\n",
                              data->system.hardware.version);
    if (data->system.hardware.serial)
        virBufferEscapeString(buf, "<serial>%s</serial>\n",
                              data->system.hardware.serial);
    virUUIDFormat(data->system.hardware.uuid, uuidstr);
    virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</hardware>\n");

    virBufferAddLit(buf, "<firmware>\n");
    virBufferAdjustIndent(buf, 2);
    if (data->system.firmware.vendor_name)
        virBufferEscapeString(buf, "<vendor>%s</vendor>\n",
                              data->system.firmware.vendor_name);
    if (data->system.firmware.version)
        virBufferEscapeString(buf, "<version>%s</version>\n",
                              data->system.firmware.version);
    if (data->system.firmware.release_date)
        virBufferEscapeString(buf, "<release_date>%s</release_date>\n",
                              data->system.firmware.release_date);
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</firmware>\n");
}


static void
virNodeDeviceCapPCIDefFormat(virBufferPtr buf,
                             const virNodeDevCapData *data)
{
    size_t i;

211
    virBufferAsprintf(buf, "<class>0x%.6x</class>\n", data->pci_dev.klass);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    virBufferAsprintf(buf, "<domain>%d</domain>\n",
                      data->pci_dev.domain);
    virBufferAsprintf(buf, "<bus>%d</bus>\n", data->pci_dev.bus);
    virBufferAsprintf(buf, "<slot>%d</slot>\n",
                      data->pci_dev.slot);
    virBufferAsprintf(buf, "<function>%d</function>\n",
                      data->pci_dev.function);
    virBufferAsprintf(buf, "<product id='0x%04x'",
                      data->pci_dev.product);
    if (data->pci_dev.product_name)
        virBufferEscapeString(buf, ">%s</product>\n",
                              data->pci_dev.product_name);
    else
        virBufferAddLit(buf, " />\n");
    virBufferAsprintf(buf, "<vendor id='0x%04x'",
                      data->pci_dev.vendor);
    if (data->pci_dev.vendor_name)
        virBufferEscapeString(buf, ">%s</vendor>\n",
                              data->pci_dev.vendor_name);
    else
        virBufferAddLit(buf, " />\n");
    if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION) {
        virBufferAddLit(buf, "<capability type='phys_function'>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferAsprintf(buf,
                          "<address domain='0x%.4x' bus='0x%.2x' "
                          "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);
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</capability>\n");
    }
    if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION) {
        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");
        }
    }
    if (data->pci_dev.hdrType) {
        virBufferAsprintf(buf, "<capability type='%s'/>\n",
                          virPCIHeaderTypeToString(data->pci_dev.hdrType));
    }
273 274 275 276
    if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) {
        virBufferAddLit(buf, "<capability type='mdev_types'>\n");
        virBufferAdjustIndent(buf, 2);
        for (i = 0; i < data->pci_dev.nmdev_types; i++) {
277
            virMediatedDeviceTypePtr type = data->pci_dev.mdev_types[i];
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            virBufferEscapeString(buf, "<type id='%s'>\n", type->id);
            virBufferAdjustIndent(buf, 2);
            if (type->name)
                virBufferEscapeString(buf, "<name>%s</name>\n",
                                      type->name);
            virBufferEscapeString(buf, "<deviceAPI>%s</deviceAPI>\n",
                                  type->device_api);
            virBufferAsprintf(buf,
                              "<availableInstances>%u</availableInstances>\n",
                              type->available_instances);
            virBufferAdjustIndent(buf, -2);
            virBufferAddLit(buf, "</type>\n");
        }
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</capability>\n");
    }
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    if (data->pci_dev.nIommuGroupDevices) {
        virBufferAsprintf(buf, "<iommuGroup number='%d'>\n",
                          data->pci_dev.iommuGroupNumber);
        virBufferAdjustIndent(buf, 2);
        for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++) {
            virBufferAsprintf(buf,
                              "<address domain='0x%.4x' bus='0x%.2x' "
                              "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);
        }
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</iommuGroup>\n");
    }
    if (data->pci_dev.numa_node >= 0)
        virBufferAsprintf(buf, "<numa node='%d'/>\n",
                          data->pci_dev.numa_node);

    if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCIE)
        virPCIEDeviceInfoFormat(buf, data->pci_dev.pci_express);
}


static void
virNodeDeviceCapUSBDevDefFormat(virBufferPtr buf,
                                const virNodeDevCapData *data)
{
    virBufferAsprintf(buf, "<bus>%d</bus>\n", data->usb_dev.bus);
    virBufferAsprintf(buf, "<device>%d</device>\n",
                      data->usb_dev.device);
    virBufferAsprintf(buf, "<product id='0x%04x'",
                      data->usb_dev.product);
    if (data->usb_dev.product_name)
        virBufferEscapeString(buf, ">%s</product>\n",
                              data->usb_dev.product_name);
    else
        virBufferAddLit(buf, " />\n");
    virBufferAsprintf(buf, "<vendor id='0x%04x'",
                      data->usb_dev.vendor);
    if (data->usb_dev.vendor_name)
        virBufferEscapeString(buf, ">%s</vendor>\n",
                              data->usb_dev.vendor_name);
    else
        virBufferAddLit(buf, " />\n");
}


static void
virNodeDeviceCapUSBInterfaceDefFormat(virBufferPtr buf,
                                      const virNodeDevCapData *data)
{
    virBufferAsprintf(buf, "<number>%d</number>\n",
                      data->usb_if.number);
    virBufferAsprintf(buf, "<class>%d</class>\n",
350
                      data->usb_if.klass);
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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    virBufferAsprintf(buf, "<subclass>%d</subclass>\n",
                      data->usb_if.subclass);
    virBufferAsprintf(buf, "<protocol>%d</protocol>\n",
                      data->usb_if.protocol);
    if (data->usb_if.description)
        virBufferEscapeString(buf,
                              "<description>%s</description>\n",
                              data->usb_if.description);
}


static void
virNodeDeviceCapNetDefFormat(virBufferPtr buf,
                             const virNodeDevCapData *data)
{
    size_t i;

    virBufferEscapeString(buf, "<interface>%s</interface>\n",
                          data->net.ifname);
    if (data->net.address)
        virBufferEscapeString(buf, "<address>%s</address>\n",
                              data->net.address);
    virInterfaceLinkFormat(buf, &data->net.lnk);
    if (data->net.features) {
        for (i = 0; i < VIR_NET_DEV_FEAT_LAST; i++) {
            if (virBitmapIsBitSet(data->net.features, i)) {
                virBufferAsprintf(buf, "<feature name='%s'/>\n",
                                  virNetDevFeatureTypeToString(i));
            }
        }
    }
    if (data->net.subtype != VIR_NODE_DEV_CAP_NET_LAST) {
        const char *subtyp =
            virNodeDevNetCapTypeToString(data->net.subtype);
        virBufferEscapeString(buf, "<capability type='%s'/>\n",
                              subtyp);
    }
}


static void
virNodeDeviceCapSCSIHostDefFormat(virBufferPtr buf,
                                  const virNodeDevCapData *data)
{
    virBufferAsprintf(buf, "<host>%d</host>\n",
                      data->scsi_host.host);
    if (data->scsi_host.unique_id != -1)
        virBufferAsprintf(buf, "<unique_id>%d</unique_id>\n",
                          data->scsi_host.unique_id);
    if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
        virBufferAddLit(buf, "<capability type='fc_host'>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferEscapeString(buf, "<wwnn>%s</wwnn>\n",
                              data->scsi_host.wwnn);
        virBufferEscapeString(buf, "<wwpn>%s</wwpn>\n",
                              data->scsi_host.wwpn);
        virBufferEscapeString(buf, "<fabric_wwn>%s</fabric_wwn>\n",
                              data->scsi_host.fabric_wwn);
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</capability>\n");
    }
    if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
        virBufferAddLit(buf, "<capability type='vport_ops'>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferAsprintf(buf, "<max_vports>%d</max_vports>\n",
                          data->scsi_host.max_vports);
        virBufferAsprintf(buf, "<vports>%d</vports>\n",
                          data->scsi_host.vports);
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</capability>\n");
    }
}


static void
virNodeDeviceCapSCSIDefFormat(virBufferPtr buf,
                              const virNodeDevCapData *data)
{
    virBufferAsprintf(buf, "<host>%d</host>\n", data->scsi.host);
    virBufferAsprintf(buf, "<bus>%d</bus>\n", data->scsi.bus);
    virBufferAsprintf(buf, "<target>%d</target>\n",
                      data->scsi.target);
    virBufferAsprintf(buf, "<lun>%d</lun>\n", data->scsi.lun);
    if (data->scsi.type)
        virBufferEscapeString(buf, "<type>%s</type>\n",
                              data->scsi.type);
}


static void
virNodeDeviceCapStorageDefFormat(virBufferPtr buf,
                                 const virNodeDevCapData *data)
{
    virBufferEscapeString(buf, "<block>%s</block>\n",
                          data->storage.block);
    if (data->storage.bus)
        virBufferEscapeString(buf, "<bus>%s</bus>\n",
                              data->storage.bus);
    if (data->storage.drive_type)
        virBufferEscapeString(buf, "<drive_type>%s</drive_type>\n",
                              data->storage.drive_type);
    if (data->storage.model)
        virBufferEscapeString(buf, "<model>%s</model>\n",
                              data->storage.model);
    if (data->storage.vendor)
        virBufferEscapeString(buf, "<vendor>%s</vendor>\n",
                              data->storage.vendor);
    if (data->storage.serial)
        virBufferEscapeString(buf, "<serial>%s</serial>\n",
                              data->storage.serial);
    if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE) {
        int avl = data->storage.flags &
            VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE;
        virBufferAddLit(buf, "<capability type='removable'>\n");
        virBufferAdjustIndent(buf, 2);
        virBufferAsprintf(buf, "<media_available>%d"
                          "</media_available>\n", avl ? 1 : 0);
        virBufferAsprintf(buf, "<media_size>%llu</media_size>\n",
                          data->storage.removable_media_size);
        if (data->storage.media_label)
            virBufferEscapeString(buf,
                                  "<media_label>%s</media_label>\n",
                                  data->storage.media_label);
        if (data->storage.logical_block_size > 0)
            virBufferAsprintf(buf, "<logical_block_size>%llu"
                              "</logical_block_size>\n",
                              data->storage.logical_block_size);
        if (data->storage.num_blocks > 0)
            virBufferAsprintf(buf,
                              "<num_blocks>%llu</num_blocks>\n",
                              data->storage.num_blocks);
        virBufferAdjustIndent(buf, -2);
        virBufferAddLit(buf, "</capability>\n");
    } else {
        virBufferAsprintf(buf, "<size>%llu</size>\n",
                          data->storage.size);
        if (data->storage.logical_block_size > 0)
            virBufferAsprintf(buf, "<logical_block_size>%llu"
                              "</logical_block_size>\n",
                              data->storage.logical_block_size);
        if (data->storage.num_blocks > 0)
            virBufferAsprintf(buf, "<num_blocks>%llu</num_blocks>\n",
                              data->storage.num_blocks);
    }
    if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE)
        virBufferAddLit(buf, "<capability type='hotpluggable'/>\n");
}


500 501
char *
virNodeDeviceDefFormat(const virNodeDeviceDef *def)
502 503
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
504
    virNodeDevCapsDefPtr caps;
505
    size_t i = 0;
506 507

    virBufferAddLit(&buf, "<device>\n");
508 509 510
    virBufferAdjustIndent(&buf, 2);
    virBufferEscapeString(&buf, "<name>%s</name>\n", def->name);
    virBufferEscapeString(&buf, "<path>%s</path>\n", def->sysfs_path);
511 512 513 514 515 516 517 518
    if (def->devnode)
        virBufferEscapeString(&buf, "<devnode type='dev'>%s</devnode>\n",
                              def->devnode);
    if (def->devlinks) {
        for (i = 0; def->devlinks[i]; i++)
            virBufferEscapeString(&buf, "<devnode type='link'>%s</devnode>\n",
                                  def->devlinks[i]);
    }
519 520
    if (def->parent)
        virBufferEscapeString(&buf, "<parent>%s</parent>\n", def->parent);
521
    if (def->driver) {
522 523 524 525 526
        virBufferAddLit(&buf, "<driver>\n");
        virBufferAdjustIndent(&buf, 2);
        virBufferEscapeString(&buf, "<name>%s</name>\n", def->driver);
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</driver>\n");
527
    }
528 529

    for (caps = def->caps; caps; caps = caps->next) {
530
        virNodeDevCapDataPtr data = &caps->data;
531

532
        virBufferAsprintf(&buf, "<capability type='%s'>\n",
533
                          virNodeDevCapTypeToString(caps->data.type));
534
        virBufferAdjustIndent(&buf, 2);
535
        switch (caps->data.type) {
536
        case VIR_NODE_DEV_CAP_SYSTEM:
537
            virNodeDeviceCapSystemDefFormat(&buf, data);
538 539
            break;
        case VIR_NODE_DEV_CAP_PCI_DEV:
540
            virNodeDeviceCapPCIDefFormat(&buf, data);
541 542
            break;
        case VIR_NODE_DEV_CAP_USB_DEV:
543
            virNodeDeviceCapUSBDevDefFormat(&buf, data);
544 545
            break;
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
546
            virNodeDeviceCapUSBInterfaceDefFormat(&buf, data);
547 548
            break;
        case VIR_NODE_DEV_CAP_NET:
549
            virNodeDeviceCapNetDefFormat(&buf, data);
550 551
            break;
        case VIR_NODE_DEV_CAP_SCSI_HOST:
552
            virNodeDeviceCapSCSIHostDefFormat(&buf, data);
553
            break;
D
David Allan 已提交
554
        case VIR_NODE_DEV_CAP_SCSI_TARGET:
555
            virBufferEscapeString(&buf, "<target>%s</target>\n",
556
                                  data->scsi_target.name);
557 558 559 560 561 562 563 564 565 566
            if (data->scsi_target.flags & VIR_NODE_DEV_CAP_FLAG_FC_RPORT) {
                virBufferAddLit(&buf, "<capability type='fc_remote_port'>\n");
                virBufferAdjustIndent(&buf, 2);
                virBufferAsprintf(&buf, "<rport>%s</rport>\n",
                                  data->scsi_target.rport);
                virBufferAsprintf(&buf, "<wwpn>%s</wwpn>\n",
                                  data->scsi_target.wwpn);
                virBufferAdjustIndent(&buf, -2);
                virBufferAddLit(&buf, "</capability>\n");
            }
D
David Allan 已提交
567
            break;
568
        case VIR_NODE_DEV_CAP_SCSI:
569
            virNodeDeviceCapSCSIDefFormat(&buf, data);
570 571
            break;
        case VIR_NODE_DEV_CAP_STORAGE:
572
            virNodeDeviceCapStorageDefFormat(&buf, data);
573
            break;
574
        case VIR_NODE_DEV_CAP_SCSI_GENERIC:
575
            virBufferEscapeString(&buf, "<char>%s</char>\n",
576 577
                                  data->sg.path);
            break;
M
Marc-André Lureau 已提交
578 579 580
        case VIR_NODE_DEV_CAP_DRM:
            virBufferEscapeString(&buf, "<type>%s</type>\n", virNodeDevDRMTypeToString(data->drm.type));
            break;
581
        case VIR_NODE_DEV_CAP_MDEV:
582 583 584 585
            virBufferEscapeString(&buf, "<type id='%s'/>\n", data->mdev.type);
            virBufferAsprintf(&buf, "<iommuGroup number='%u'/>\n",
                              data->mdev.iommuGroupNumber);
            break;
B
Bjoern Walk 已提交
586 587 588 589 590 591 592 593
        case VIR_NODE_DEV_CAP_CCW_DEV:
            virBufferAsprintf(&buf, "<cssid>0x%x</cssid>\n",
                              data->ccw_dev.cssid);
            virBufferAsprintf(&buf, "<ssid>0x%x</ssid>\n",
                              data->ccw_dev.ssid);
            virBufferAsprintf(&buf, "<devno>0x%04x</devno>\n",
                              data->ccw_dev.devno);
            break;
594
        case VIR_NODE_DEV_CAP_MDEV_TYPES:
595 596
        case VIR_NODE_DEV_CAP_FC_HOST:
        case VIR_NODE_DEV_CAP_VPORTS:
597 598 599 600
        case VIR_NODE_DEV_CAP_LAST:
            break;
        }

601 602
        virBufferAdjustIndent(&buf, -2);
        virBufferAddLit(&buf, "</capability>\n");
603 604
    }

605
    virBufferAdjustIndent(&buf, -2);
606 607
    virBufferAddLit(&buf, "</device>\n");

608 609
    if (virBufferCheckError(&buf) < 0)
        return NULL;
610 611 612 613

    return virBufferContentAndReset(&buf);
}

614

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/**
 * 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;
}

650

651
static int
652
virNodeDevCapsDefParseULong(const char *xpath,
653 654 655 656 657 658 659 660 661
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

662
    ret = virXPathULong(xpath, ctxt, &val);
663
    if (ret < 0) {
664 665 666
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
667 668 669 670 671 672 673
        return -1;
    }

    *value = val;
    return 0;
}

674

675
static int
676
virNodeDevCapsDefParseULongLong(const char *xpath,
677 678 679 680 681 682 683 684 685
                                xmlXPathContextPtr ctxt,
                                unsigned long long *value,
                                virNodeDeviceDefPtr def,
                                const char *missing_error_fmt,
                                const char *invalid_error_fmt)
{
    int ret;
    unsigned long long val;

686
    ret = virXPathULongLong(xpath, ctxt, &val);
687
    if (ret < 0) {
688 689 690
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
691 692 693 694 695 696 697
        return -1;
    }

    *value = val;
    return 0;
}

698

M
Marc-André Lureau 已提交
699 700 701 702
static int
virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt,
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
703
                         virNodeDevCapDRMPtr drm)
M
Marc-André Lureau 已提交
704 705
{
    xmlNodePtr orignode;
706
    int ret = -1, val;
M
Marc-André Lureau 已提交
707 708 709 710 711 712 713
    char *type = NULL;

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

    type = virXPathString("string(./type[1])", ctxt);

714
    if ((val = virNodeDevDRMTypeFromString(type)) < 0) {
M
Marc-André Lureau 已提交
715 716 717 718
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unknown drm type '%s' for '%s'"), type, def->name);
        goto out;
    }
719
    drm->type = val;
M
Marc-André Lureau 已提交
720 721 722 723 724 725 726 727 728

    ret = 0;

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

729

B
Bjoern Walk 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
static int
virNodeDevCapCCWParseXML(xmlXPathContextPtr ctxt,
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
                         virNodeDevCapCCWPtr ccw_dev)
{
    xmlNodePtr orignode;
    int ret = -1;
    char *cssid = NULL, *ssid = NULL, *devno = NULL;

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

   if (!(cssid = virXPathString("string(./cssid[1])", ctxt))) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("missing cssid value for '%s'"), def->name);
        goto out;
    }

    if (virStrToLong_uip(cssid, NULL, 0, &ccw_dev->cssid) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("invalid cssid value '%s' for '%s'"),
                       cssid, def->name);
        goto out;
    }

    if (!(ssid = virXPathString("string(./ssid[1])", ctxt))) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("missing ssid value for '%s'"), def->name);
        goto out;
    }

    if (virStrToLong_uip(ssid, NULL, 0, &ccw_dev->ssid) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("invalid ssid value '%s' for '%s'"),
                       cssid, def->name);
        goto out;
    }

    if (!(devno = virXPathString("string(./devno[1])", ctxt))) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("missing devno value for '%s'"), def->name);
        goto out;
    }

    if (virStrToLong_uip(devno, NULL, 16, &ccw_dev->devno) < 0) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("invalid devno value '%s' for '%s'"),
                       devno, def->name);
        goto out;
    }

    ret = 0;

 out:
    ctxt->node = orignode;
786 787 788
    VIR_FREE(cssid);
    VIR_FREE(ssid);
    VIR_FREE(devno);
B
Bjoern Walk 已提交
789 790 791 792
    return ret;
}


793
static int
794
virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt,
795 796
                             virNodeDeviceDefPtr def,
                             xmlNodePtr node,
797
                             virNodeDevCapStoragePtr storage)
798 799
{
    xmlNodePtr orignode, *nodes = NULL;
800 801
    size_t i;
    int n, ret = -1;
802 803 804 805 806
    unsigned long long val;

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

807 808
    storage->block = virXPathString("string(./block[1])", ctxt);
    if (!storage->block) {
809 810 811
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no block device path supplied for '%s'"),
                       def->name);
812 813 814
        goto out;
    }

815 816 817 818 819
    storage->bus        = virXPathString("string(./bus[1])", ctxt);
    storage->drive_type = virXPathString("string(./drive_type[1])", ctxt);
    storage->model      = virXPathString("string(./model[1])", ctxt);
    storage->vendor     = virXPathString("string(./vendor[1])", ctxt);
    storage->serial     = virXPathString("string(./serial[1])", ctxt);
820

821
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
822 823
        goto out;

824
    for (i = 0; i < n; i++) {
825 826 827
        char *type = virXMLPropString(nodes[i], "type");

        if (!type) {
828 829 830
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing storage capability type for '%s'"),
                           def->name);
831 832 833
            goto out;
        }

834
        if (STREQ(type, "hotpluggable")) {
835
            storage->flags |= VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE;
836
        } else if (STREQ(type, "removable")) {
837 838
            xmlNodePtr orignode2;

839
            storage->flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE;
840 841 842 843

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

844
            if (virXPathBoolean("count(./media_available[. = '1']) > 0", ctxt))
845
                storage->flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE;
846

847
            storage->media_label = virXPathString("string(./media_label[1])", ctxt);
848

849
            val = 0;
850
            if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def,
851 852 853 854 855 856
                                                _("no removable media size supplied for '%s'"),
                                                _("invalid removable media size supplied for '%s'")) < 0) {
                ctxt->node = orignode2;
                VIR_FREE(type);
                goto out;
            }
857
            storage->removable_media_size = val;
858 859 860

            ctxt->node = orignode2;
        } else {
861 862 863
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown storage capability type '%s' for '%s'"),
                           type, def->name);
864 865 866 867 868 869 870
            VIR_FREE(type);
            goto out;
        }

        VIR_FREE(type);
    }

871
    if (!(storage->flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE)) {
872
        val = 0;
873
        if (virNodeDevCapsDefParseULongLong("number(./size[1])", ctxt, &val, def,
874 875 876
                                            _("no size supplied for '%s'"),
                                            _("invalid size supplied for '%s'")) < 0)
            goto out;
877
        storage->size = val;
878 879 880
    }

    ret = 0;
881
 out:
882 883 884 885 886
    VIR_FREE(nodes);
    ctxt->node = orignode;
    return ret;
}

887

888
static int
889
virNodeDevCapSCSIParseXML(xmlXPathContextPtr ctxt,
890 891
                          virNodeDeviceDefPtr def,
                          xmlNodePtr node,
892
                          virNodeDevCapSCSIPtr scsi)
893 894 895 896 897 898 899
{
    xmlNodePtr orignode;
    int ret = -1;

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

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

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

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

918
    if (virNodeDevCapsDefParseULong("number(./lun[1])", ctxt,
919
                                    &scsi->lun, def,
920 921 922 923
                                    _("no SCSI LUN ID supplied for '%s'"),
                                    _("invalid SCSI LUN ID supplied for '%s'")) < 0)
        goto out;

924
    scsi->type = virXPathString("string(./type[1])", ctxt);
925 926

    ret = 0;
927
 out:
928 929 930 931
    ctxt->node = orignode;
    return ret;
}

D
David Allan 已提交
932 933

static int
934
virNodeDevCapSCSITargetParseXML(xmlXPathContextPtr ctxt,
D
David Allan 已提交
935 936
                                virNodeDeviceDefPtr def,
                                xmlNodePtr node,
937
                                virNodeDevCapSCSITargetPtr scsi_target)
D
David Allan 已提交
938
{
939 940 941 942
    xmlNodePtr orignode, *nodes = NULL;
    int ret = -1, n = 0;
    size_t i;
    char *type = NULL;
D
David Allan 已提交
943 944 945 946

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

947 948
    scsi_target->name = virXPathString("string(./target[1])", ctxt);
    if (!scsi_target->name) {
949 950 951
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no target name supplied for '%s'"),
                       def->name);
D
David Allan 已提交
952 953 954
        goto out;
    }

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
        goto out;

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

        if (!type) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing type for SCSI target capability for '%s'"),
                           def->name);
            goto out;
        }

        if (STREQ(type, "fc_remote_port")) {
            xmlNodePtr orignode2;

            scsi_target->flags |= VIR_NODE_DEV_CAP_FLAG_FC_RPORT;

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

            if (virNodeDevCapsDefParseString("string(./rport[1])",
                                             ctxt,
                                             &scsi_target->rport) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("missing rport name for '%s'"), def->name);
                goto out;
            }

            if (virNodeDevCapsDefParseString("string(./wwpn[1])",
                                             ctxt, &scsi_target->wwpn) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("missing wwpn identifier for '%s'"),
                               def->name);
                goto out;
            }

            ctxt->node = orignode2;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown SCSI target capability type '%s' for '%s'"),
                           type, def->name);
            goto out;
        }

        VIR_FREE(type);
    }

D
David Allan 已提交
1003 1004
    ret = 0;

1005
 out:
D
David Allan 已提交
1006
    ctxt->node = orignode;
1007 1008
    VIR_FREE(type);
    VIR_FREE(nodes);
D
David Allan 已提交
1009 1010 1011 1012
    return ret;
}


1013
static int
1014
virNodeDevCapSCSIHostParseXML(xmlXPathContextPtr ctxt,
1015 1016
                              virNodeDeviceDefPtr def,
                              xmlNodePtr node,
1017
                              virNodeDevCapSCSIHostPtr scsi_host,
1018 1019
                              int create,
                              const char *virt_type)
1020
{
1021
    xmlNodePtr orignode, *nodes = NULL;
1022 1023
    int ret = -1, n = 0;
    size_t i;
1024
    char *type = NULL;
1025 1026 1027 1028

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

J
John Ferlan 已提交
1029 1030
    if (create == EXISTING_DEVICE) {
        if (virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
1031
                                        &scsi_host->host, def,
J
John Ferlan 已提交
1032 1033 1034 1035 1036
                                        _("no SCSI host ID supplied for '%s'"),
                                        _("invalid SCSI host ID supplied for '%s'")) < 0) {
            goto out;
        }
        /* Optional unique_id value */
1037
        scsi_host->unique_id = -1;
J
John Ferlan 已提交
1038
        if (virNodeDevCapsDefParseIntOptional("number(./unique_id[1])", ctxt,
1039
                                              &scsi_host->unique_id, def,
J
John Ferlan 已提交
1040 1041 1042
                                              _("invalid unique_id supplied for '%s'")) < 0) {
            goto out;
        }
1043 1044
    }

1045
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
1046
        goto out;
1047

1048
    for (i = 0; i < n; i++) {
1049 1050 1051
        type = virXMLPropString(nodes[i], "type");

        if (!type) {
1052 1053 1054
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing SCSI host capability type for '%s'"),
                           def->name);
1055 1056 1057 1058 1059
            goto out;
        }

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

1060
            scsi_host->flags |= VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS;
1061 1062 1063 1064 1065

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

            xmlNodePtr orignode2;

1066
            scsi_host->flags |= VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST;
1067 1068 1069 1070

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

1071
            if (virNodeDevCapsDefParseString("string(./wwnn[1])",
1072
                                             ctxt,
1073 1074
                                             &scsi_host->wwnn) < 0) {
                if (virRandomGenerateWWN(&scsi_host->wwnn, virt_type) < 0) {
1075 1076 1077 1078
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWNN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
1079 1080
                    goto out;
                }
1081 1082
            }

1083
            if (virNodeDevCapsDefParseString("string(./wwpn[1])",
1084
                                             ctxt,
1085 1086
                                             &scsi_host->wwpn) < 0) {
                if (virRandomGenerateWWN(&scsi_host->wwpn, virt_type) < 0) {
1087 1088 1089 1090
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWPN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
1091 1092
                    goto out;
                }
1093 1094
            }

1095 1096
            if (virNodeDevCapsDefParseString("string(./fabric_wwn[1])",
                                             ctxt,
1097
                                             &scsi_host->fabric_wwn) < 0)
1098 1099
                VIR_DEBUG("No fabric_wwn defined for '%s'", def->name);

1100 1101 1102
            ctxt->node = orignode2;

        } else {
1103 1104 1105
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown SCSI host capability type '%s' for '%s'"),
                           type, def->name);
1106 1107 1108 1109 1110
            goto out;
        }

        VIR_FREE(type);
    }
1111 1112

    ret = 0;
1113

1114
 out:
1115
    VIR_FREE(type);
1116
    ctxt->node = orignode;
1117
    VIR_FREE(nodes);
1118 1119 1120
    return ret;
}

1121

1122
static int
1123
virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt,
1124 1125
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
1126
                         virNodeDevCapNetPtr net)
1127
{
1128
    xmlNodePtr orignode, lnk;
1129 1130
    size_t i = -1;
    int ret = -1, n = -1;
1131
    char *tmp = NULL;
1132
    xmlNodePtr *nodes = NULL;
1133 1134 1135 1136

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

1137 1138
    net->ifname = virXPathString("string(./interface[1])", ctxt);
    if (!net->ifname) {
1139 1140 1141
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no network interface supplied for '%s'"),
                       def->name);
1142 1143 1144
        goto out;
    }

1145
    net->address = virXPathString("string(./address[1])", ctxt);
1146

1147 1148 1149 1150
    if ((n = virXPathNodeSet("./feature", ctxt, &nodes)) < 0)
        goto out;

    if (n > 0) {
1151
        if (!(net->features = virBitmapNew(VIR_NET_DEV_FEAT_LAST)))
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
            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;
        }
1169
        ignore_value(virBitmapSetBit(net->features, val));
1170
        VIR_FREE(tmp);
1171 1172
    }

1173
    net->subtype = VIR_NODE_DEV_CAP_NET_LAST;
1174

1175
    tmp = virXPathString("string(./capability/@type)", ctxt);
1176 1177 1178 1179
    if (tmp) {
        int val = virNodeDevNetCapTypeFromString(tmp);
        VIR_FREE(tmp);
        if (val < 0) {
1180
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1181 1182
                           _("invalid network type supplied for '%s'"),
                           def->name);
1183 1184
            goto out;
        }
1185
        net->subtype = val;
1186 1187
    }

1188
    lnk = virXPathNode("./link", ctxt);
1189
    if (lnk && virInterfaceLinkParseXML(lnk, &net->lnk) < 0)
1190 1191
        goto out;

1192
    ret = 0;
1193
 out:
1194
    ctxt->node = orignode;
1195 1196
    VIR_FREE(nodes);
    VIR_FREE(tmp);
1197 1198 1199
    return ret;
}

1200

1201
static int
1202
virNodeDevCapUSBInterfaceParseXML(xmlXPathContextPtr ctxt,
1203 1204
                                  virNodeDeviceDefPtr def,
                                  xmlNodePtr node,
1205
                                  virNodeDevCapUSBIfPtr usb_if)
1206 1207 1208 1209 1210 1211 1212
{
    xmlNodePtr orignode;
    int ret = -1;

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

1213
    if (virNodeDevCapsDefParseULong("number(./number[1])", ctxt,
1214
                                    &usb_if->number, def,
1215 1216 1217 1218
                                    _("no USB interface number supplied for '%s'"),
                                    _("invalid USB interface number supplied for '%s'")) < 0)
        goto out;

1219
    if (virNodeDevCapsDefParseULong("number(./class[1])", ctxt,
1220
                                    &usb_if->klass, def,
1221 1222 1223 1224
                                    _("no USB interface class supplied for '%s'"),
                                    _("invalid USB interface class supplied for '%s'")) < 0)
        goto out;

1225
    if (virNodeDevCapsDefParseULong("number(./subclass[1])", ctxt,
1226
                                    &usb_if->subclass, def,
1227 1228 1229 1230
                                    _("no USB interface subclass supplied for '%s'"),
                                    _("invalid USB interface subclass supplied for '%s'")) < 0)
        goto out;

1231
    if (virNodeDevCapsDefParseULong("number(./protocol[1])", ctxt,
1232
                                    &usb_if->protocol, def,
1233 1234 1235 1236
                                    _("no USB interface protocol supplied for '%s'"),
                                    _("invalid USB interface protocol supplied for '%s'")) < 0)
        goto out;

1237
    usb_if->description = virXPathString("string(./description[1])", ctxt);
1238 1239

    ret = 0;
1240
 out:
1241 1242 1243 1244
    ctxt->node = orignode;
    return ret;
}

1245

1246
static int
1247
virNodeDevCapsDefParseHexId(const char *xpath,
1248 1249 1250 1251 1252 1253 1254 1255 1256
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

1257
    ret = virXPathULongHex(xpath, ctxt, &val);
1258
    if (ret < 0) {
1259 1260 1261
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
1262 1263 1264 1265 1266 1267 1268
        return -1;
    }

    *value = val;
    return 0;
}

1269

1270
static int
1271
virNodeDevCapUSBDevParseXML(xmlXPathContextPtr ctxt,
1272 1273
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1274
                            virNodeDevCapUSBDevPtr usb_dev)
1275 1276 1277 1278 1279 1280 1281
{
    xmlNodePtr orignode;
    int ret = -1;

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

1282
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
1283
                                    &usb_dev->bus, def,
1284 1285 1286 1287
                                    _("no USB bus number supplied for '%s'"),
                                    _("invalid USB bus number supplied for '%s'")) < 0)
        goto out;

1288
    if (virNodeDevCapsDefParseULong("number(./device[1])", ctxt,
1289
                                    &usb_dev->device, def,
1290 1291 1292 1293
                                    _("no USB device number supplied for '%s'"),
                                    _("invalid USB device number supplied for '%s'")) < 0)
        goto out;

1294
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1295
                                    &usb_dev->vendor, def,
1296 1297 1298 1299
                                    _("no USB vendor ID supplied for '%s'"),
                                    _("invalid USB vendor ID supplied for '%s'")) < 0)
        goto out;

1300
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1301
                                    &usb_dev->product, def,
1302 1303 1304 1305
                                    _("no USB product ID supplied for '%s'"),
                                    _("invalid USB product ID supplied for '%s'")) < 0)
        goto out;

1306 1307
    usb_dev->vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    usb_dev->product_name = virXPathString("string(./product[1])", ctxt);
1308 1309

    ret = 0;
1310
 out:
1311 1312 1313 1314
    ctxt->node = orignode;
    return ret;
}

1315

1316
static int
1317
virNodeDevCapPCIDevIommuGroupParseXML(xmlXPathContextPtr ctxt,
1318
                                      xmlNodePtr iommuGroupNode,
1319
                                      virNodeDevCapPCIDevPtr pci_dev)
1320 1321 1322 1323
{
    xmlNodePtr origNode = ctxt->node;
    xmlNodePtr *addrNodes = NULL;
    char *numberStr = NULL;
1324 1325
    int nAddrNodes, ret = -1;
    size_t i;
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    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,
1337
                        &pci_dev->iommuGroupNumber) < 0) {
1338 1339 1340 1341 1342 1343 1344 1345 1346
        virReportError(VIR_ERR_XML_ERROR,
                       _("invalid iommuGroup number attribute '%s'"),
                       numberStr);
        goto cleanup;
    }

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

1347
    for (i = 0; i < nAddrNodes; i++) {
1348
        virPCIDeviceAddress addr = {0};
1349
        if (virPCIDeviceAddressParseXML(addrNodes[i], &addr) < 0)
1350
            goto cleanup;
1351
        if (VIR_ALLOC(pciAddr) < 0)
1352 1353 1354 1355 1356
            goto cleanup;
        pciAddr->domain = addr.domain;
        pciAddr->bus = addr.bus;
        pciAddr->slot = addr.slot;
        pciAddr->function = addr.function;
1357 1358
        if (VIR_APPEND_ELEMENT(pci_dev->iommuGroupDevices,
                               pci_dev->nIommuGroupDevices,
1359
                               pciAddr) < 0)
1360 1361 1362 1363
            goto cleanup;
    }

    ret = 0;
1364
 cleanup:
1365
    ctxt->node = origNode;
1366
    VIR_FREE(numberStr);
1367 1368 1369 1370 1371
    VIR_FREE(addrNodes);
    VIR_FREE(pciAddr);
    return ret;
}

1372

1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 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
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;
}

1419

1420 1421 1422 1423 1424 1425 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
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;
}

1454

1455
static int
1456 1457
virNodeDevPCICapSRIOVPhysicalParseXML(xmlXPathContextPtr ctxt,
                                      virNodeDevCapPCIDevPtr pci_dev)
1458
{
1459
    xmlNodePtr address = virXPathNode("./address[1]", ctxt);
1460

1461 1462
    if (VIR_ALLOC(pci_dev->physical_function) < 0)
        return -1;
1463

1464 1465 1466 1467
    if (!address) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Missing address in 'phys_function' capability"));
        return -1;
1468 1469
    }

1470 1471 1472
    if (virPCIDeviceAddressParseXML(address,
                                    pci_dev->physical_function) < 0)
        return -1;
1473

1474
    pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;
1475

1476 1477
    return 0;
}
1478

1479

1480 1481 1482 1483 1484 1485 1486 1487 1488
static int
virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt,
                                     virNodeDevCapPCIDevPtr pci_dev)
{
    int ret = -1;
    xmlNodePtr *addresses = NULL;
    int naddresses = virXPathNodeSet("./address", ctxt, &addresses);
    char *maxFuncsStr = virXPathString("string(./@maxCount)", ctxt);
    size_t i;
1489

1490 1491
    if (naddresses < 0)
        goto cleanup;
1492

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
    if (maxFuncsStr &&
        virStrToLong_uip(maxFuncsStr, NULL, 10,
                         &pci_dev->max_virtual_functions) < 0) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Malformed 'maxCount' parameter"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(pci_dev->virtual_functions, naddresses) < 0)
        goto cleanup;

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

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

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

1515 1516 1517 1518 1519
        if (VIR_APPEND_ELEMENT(pci_dev->virtual_functions,
                               pci_dev->num_virtual_functions,
                               addr) < 0)
            goto cleanup;
    }
1520

1521 1522 1523 1524 1525 1526 1527
    pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;
    ret = 0;
 cleanup:
    VIR_FREE(addresses);
    VIR_FREE(maxFuncsStr);
    return ret;
}
1528 1529


1530 1531 1532 1533 1534 1535 1536 1537
static int
virNodeDevPCICapMdevTypesParseXML(xmlXPathContextPtr ctxt,
                                  virNodeDevCapPCIDevPtr pci_dev)
{
    int ret = -1;
    xmlNodePtr orignode = NULL;
    xmlNodePtr *nodes = NULL;
    int nmdev_types = -1;
1538
    virMediatedDeviceTypePtr type = NULL;
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    size_t i;

    if ((nmdev_types = virXPathNodeSet("./type", ctxt, &nodes)) < 0)
        goto cleanup;

    orignode = ctxt->node;
    for (i = 0; i < nmdev_types; i++) {
        ctxt->node = nodes[i];

        if (VIR_ALLOC(type) < 0)
            goto cleanup;

        if (!(type->id = virXPathString("string(./@id[1])", ctxt))) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("missing 'id' attribute for mediated device's "
                             "<type> element"));
            goto cleanup;
        }

        if (!(type->device_api = virXPathString("string(./deviceAPI[1])", ctxt))) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("missing device API for mediated device type '%s'"),
                           type->id);
            goto cleanup;
        }

        if (virXPathUInt("number(./availableInstances)", ctxt,
                         &type->available_instances) < 0) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("missing number of available instances for "
                             "mediated device type '%s'"),
                           type->id);
            goto cleanup;
        }

        type->name = virXPathString("string(./name)", ctxt);

        if (VIR_APPEND_ELEMENT(pci_dev->mdev_types,
                               pci_dev->nmdev_types, type) < 0)
            goto cleanup;
    }

    pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;
    ret = 0;
 cleanup:
    VIR_FREE(nodes);
1585
    virMediatedDeviceTypeFree(type);
1586 1587 1588 1589 1590
    ctxt->node = orignode;
    return ret;
}


1591 1592 1593 1594 1595 1596 1597 1598
static int
virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt,
                                xmlNodePtr node,
                                virNodeDevCapPCIDevPtr pci_dev)
{
    char *type = virXMLPropString(node, "type");
    xmlNodePtr orignode = ctxt->node;
    int ret = -1;
1599

1600 1601 1602 1603 1604 1605
    ctxt->node = node;

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

1607 1608 1609 1610 1611 1612
    if (STREQ(type, "phys_function") &&
        virNodeDevPCICapSRIOVPhysicalParseXML(ctxt, pci_dev) < 0) {
        goto cleanup;
    } else if (STREQ(type, "virt_functions") &&
               virNodeDevPCICapSRIOVVirtualParseXML(ctxt, pci_dev) < 0) {
        goto cleanup;
1613 1614 1615
    } else if (STREQ(type, "mdev_types") &&
        virNodeDevPCICapMdevTypesParseXML(ctxt, pci_dev) < 0) {
        goto cleanup;
1616 1617 1618
    } else {
        int hdrType = virPCIHeaderTypeFromString(type);

1619 1620
        if (hdrType > 0 && !pci_dev->hdrType)
            pci_dev->hdrType = hdrType;
1621 1622 1623
    }

    ret = 0;
1624
 cleanup:
1625 1626 1627 1628 1629 1630
    VIR_FREE(type);
    ctxt->node = orignode;
    return ret;
}


1631
static int
1632
virNodeDevCapPCIDevParseXML(xmlXPathContextPtr ctxt,
1633 1634
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1635
                            virNodeDevCapPCIDevPtr pci_dev)
1636
{
1637
    xmlNodePtr orignode, iommuGroupNode, pciExpress;
1638 1639
    xmlNodePtr *nodes = NULL;
    int n = 0;
1640
    int ret = -1;
1641
    virPCIEDeviceInfoPtr pci_express = NULL;
1642
    char *tmp = NULL;
1643
    size_t i = 0;
1644 1645 1646 1647

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

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
    if (virNodeDevCapsDefParseHexId("string(./class[1])", ctxt,
                                    &pci_dev->klass, def,
                                    _("no PCI class supplied for '%s'"),
                                    _("invalid PCI class supplied for '%s'")) < 0)
        goto out;

    if (pci_dev->klass > 0xffffff) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("invalid PCI class supplied for '%s'"), def->name);
        goto out;
    }

1660
    if (virNodeDevCapsDefParseULong("number(./domain[1])", ctxt,
1661
                                    &pci_dev->domain, def,
1662 1663 1664 1665
                                    _("no PCI domain ID supplied for '%s'"),
                                    _("invalid PCI domain ID supplied for '%s'")) < 0)
        goto out;

1666
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
1667
                                    &pci_dev->bus, def,
1668 1669 1670 1671
                                    _("no PCI bus ID supplied for '%s'"),
                                    _("invalid PCI bus ID supplied for '%s'")) < 0)
        goto out;

1672
    if (virNodeDevCapsDefParseULong("number(./slot[1])", ctxt,
1673
                                    &pci_dev->slot, def,
1674 1675 1676 1677
                                    _("no PCI slot ID supplied for '%s'"),
                                    _("invalid PCI slot ID supplied for '%s'")) < 0)
        goto out;

1678
    if (virNodeDevCapsDefParseULong("number(./function[1])", ctxt,
1679
                                    &pci_dev->function, def,
1680 1681 1682 1683
                                    _("no PCI function ID supplied for '%s'"),
                                    _("invalid PCI function ID supplied for '%s'")) < 0)
        goto out;

1684
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1685
                                    &pci_dev->vendor, def,
1686 1687 1688 1689
                                    _("no PCI vendor ID supplied for '%s'"),
                                    _("invalid PCI vendor ID supplied for '%s'")) < 0)
        goto out;

1690
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1691
                                    &pci_dev->product, def,
1692 1693 1694 1695
                                    _("no PCI product ID supplied for '%s'"),
                                    _("invalid PCI product ID supplied for '%s'")) < 0)
        goto out;

1696 1697
    pci_dev->vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    pci_dev->product_name = virXPathString("string(./product[1])", ctxt);
1698

1699 1700 1701 1702
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
        goto out;

    for (i = 0; i < n; i++) {
1703
        if (virNodeDevPCICapabilityParseXML(ctxt, nodes[i], pci_dev) < 0)
1704 1705 1706 1707
            goto out;
    }
    VIR_FREE(nodes);

1708
    if ((iommuGroupNode = virXPathNode("./iommuGroup[1]", ctxt))) {
1709
        if (virNodeDevCapPCIDevIommuGroupParseXML(ctxt, iommuGroupNode,
1710
                                                  pci_dev) < 0) {
1711 1712 1713
            goto out;
        }
    }
1714

1715
    /* The default value is -1 since zero is valid NUMA node number */
1716
    pci_dev->numa_node = -1;
1717
    if (virNodeDevCapsDefParseIntOptional("number(./numa[1]/@node)", ctxt,
1718
                                          &pci_dev->numa_node, def,
1719 1720 1721
                                          _("invalid NUMA node ID supplied for '%s'")) < 0)
        goto out;

1722 1723 1724 1725 1726 1727 1728
    if ((pciExpress = virXPathNode("./pci-express[1]", ctxt))) {
        if (VIR_ALLOC(pci_express) < 0)
            goto out;

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

1729
        pci_dev->pci_express = pci_express;
1730
        pci_express = NULL;
1731
        pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCIE;
1732 1733
    }

1734
    ret = 0;
1735
 out:
1736
    VIR_FREE(nodes);
1737
    VIR_FREE(tmp);
1738
    virPCIEDeviceInfoFree(pci_express);
1739 1740 1741 1742
    ctxt->node = orignode;
    return ret;
}

1743

1744
static int
1745
virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt,
1746 1747
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
1748
                            virNodeDevCapSystemPtr syscap)
1749
{
1750 1751
    virNodeDevCapSystemHardwarePtr hardware = &syscap->hardware;
    virNodeDevCapSystemFirmwarePtr firmware = &syscap->firmware;
1752 1753 1754 1755 1756 1757 1758
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

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

1759
    syscap->product_name = virXPathString("string(./product[1])", ctxt);
1760

1761 1762 1763
    hardware->vendor_name = virXPathString("string(./hardware/vendor[1])", ctxt);
    hardware->version     = virXPathString("string(./hardware/version[1])", ctxt);
    hardware->serial      = virXPathString("string(./hardware/serial[1])", ctxt);
1764

1765
    tmp = virXPathString("string(./hardware/uuid[1])", ctxt);
1766
    if (!tmp) {
1767 1768
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no system UUID supplied for '%s'"), def->name);
1769 1770 1771
        goto out;
    }

1772
    if (virUUIDParse(tmp, hardware->uuid) < 0) {
1773 1774
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("malformed uuid element for '%s'"), def->name);
1775 1776 1777 1778 1779
        VIR_FREE(tmp);
        goto out;
    }
    VIR_FREE(tmp);

1780 1781 1782
    firmware->vendor_name  = virXPathString("string(./firmware/vendor[1])", ctxt);
    firmware->version      = virXPathString("string(./firmware/version[1])", ctxt);
    firmware->release_date = virXPathString("string(./firmware/release_date[1])", ctxt);
1783 1784

    ret = 0;
1785
 out:
1786 1787 1788 1789
    ctxt->node = orignode;
    return ret;
}

1790

1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
static int
virNodeDevCapMdevParseXML(xmlXPathContextPtr ctxt,
                          virNodeDeviceDefPtr def,
                          xmlNodePtr node,
                          virNodeDevCapMdevPtr mdev)
{
    xmlNodePtr orignode;
    int ret = -1;

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

    if (!(mdev->type = virXPathString("string(./type[1]/@id)", ctxt))) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("missing type id attribute for '%s'"), def->name);
        goto out;
    }

    if (virNodeDevCapsDefParseULong("number(./iommuGroup[1]/@number)", ctxt,
                                    &mdev->iommuGroupNumber, def,
                                    _("missing iommuGroup number attribute for "
                                      "'%s'"),
                                    _("invalid iommuGroup number attribute for "
                                      "'%s'")) < 0)
        goto out;

    ret = 0;
 out:
    ctxt->node = orignode;
    return ret;
}


1824
static virNodeDevCapsDefPtr
1825
virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt,
1826
                          virNodeDeviceDefPtr def,
1827
                          xmlNodePtr node,
1828 1829
                          int create,
                          const char *virt_type)
1830 1831 1832
{
    virNodeDevCapsDefPtr caps;
    char *tmp;
1833
    int val, ret = -1;
1834

1835
    if (VIR_ALLOC(caps) < 0)
1836 1837 1838 1839
        return NULL;

    tmp = virXMLPropString(node, "type");
    if (!tmp) {
1840 1841
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing capability type"));
1842 1843 1844 1845
        goto error;
    }

    if ((val = virNodeDevCapTypeFromString(tmp)) < 0) {
1846
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1847
                       _("unknown capability type '%s'"), tmp);
1848 1849 1850
        VIR_FREE(tmp);
        goto error;
    }
1851
    caps->data.type = val;
1852 1853
    VIR_FREE(tmp);

1854
    switch (caps->data.type) {
1855
    case VIR_NODE_DEV_CAP_SYSTEM:
1856
        ret = virNodeDevCapSystemParseXML(ctxt, def, node, &caps->data.system);
1857 1858
        break;
    case VIR_NODE_DEV_CAP_PCI_DEV:
1859
        ret = virNodeDevCapPCIDevParseXML(ctxt, def, node, &caps->data.pci_dev);
1860 1861
        break;
    case VIR_NODE_DEV_CAP_USB_DEV:
1862
        ret = virNodeDevCapUSBDevParseXML(ctxt, def, node, &caps->data.usb_dev);
1863 1864
        break;
    case VIR_NODE_DEV_CAP_USB_INTERFACE:
1865 1866
        ret = virNodeDevCapUSBInterfaceParseXML(ctxt, def, node,
                                                &caps->data.usb_if);
1867 1868
        break;
    case VIR_NODE_DEV_CAP_NET:
1869
        ret = virNodeDevCapNetParseXML(ctxt, def, node, &caps->data.net);
1870 1871
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1872
        ret = virNodeDevCapSCSIHostParseXML(ctxt, def, node,
1873
                                            &caps->data.scsi_host,
1874 1875
                                            create,
                                            virt_type);
1876
        break;
D
David Allan 已提交
1877
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
1878 1879
        ret = virNodeDevCapSCSITargetParseXML(ctxt, def, node,
                                              &caps->data.scsi_target);
D
David Allan 已提交
1880
        break;
1881
    case VIR_NODE_DEV_CAP_SCSI:
1882
        ret = virNodeDevCapSCSIParseXML(ctxt, def, node, &caps->data.scsi);
1883 1884
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1885 1886
        ret = virNodeDevCapStorageParseXML(ctxt, def, node,
                                           &caps->data.storage);
1887
        break;
M
Marc-André Lureau 已提交
1888
    case VIR_NODE_DEV_CAP_DRM:
1889
        ret = virNodeDevCapDRMParseXML(ctxt, def, node, &caps->data.drm);
M
Marc-André Lureau 已提交
1890
        break;
1891
    case VIR_NODE_DEV_CAP_MDEV:
1892 1893
        ret = virNodeDevCapMdevParseXML(ctxt, def, node, &caps->data.mdev);
        break;
B
Bjoern Walk 已提交
1894 1895 1896
    case VIR_NODE_DEV_CAP_CCW_DEV:
        ret = virNodeDevCapCCWParseXML(ctxt, def, node, &caps->data.ccw_dev);
        break;
1897
    case VIR_NODE_DEV_CAP_MDEV_TYPES:
1898 1899 1900 1901
    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:
1902 1903
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown capability type '%d' for '%s'"),
1904
                       caps->data.type, def->name);
1905 1906 1907 1908 1909 1910 1911 1912
        ret = -1;
        break;
    }

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

1913
 error:
1914 1915 1916 1917
    virNodeDevCapsDefFree(caps);
    return NULL;
}

1918

1919
static virNodeDeviceDefPtr
1920 1921 1922
virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt,
                         int create,
                         const char *virt_type)
1923 1924 1925
{
    virNodeDeviceDefPtr def;
    virNodeDevCapsDefPtr *next_cap;
1926
    xmlNodePtr *nodes = NULL;
1927
    int n, m;
1928
    size_t i;
1929

1930
    if (VIR_ALLOC(def) < 0)
1931 1932 1933
        return NULL;

    /* Extract device name */
1934
    if (create == EXISTING_DEVICE) {
1935
        def->name = virXPathString("string(./name[1])", ctxt);
1936 1937

        if (!def->name) {
1938
            virReportError(VIR_ERR_NO_NAME, NULL);
1939 1940
            goto error;
        }
1941
    } else {
1942
        if (VIR_STRDUP(def->name, "new device") < 0)
1943
            goto error;
1944 1945
    }

M
Marc-André Lureau 已提交
1946 1947
    def->sysfs_path = virXPathString("string(./path[1])", ctxt);

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
    /* Parse devnodes */
    if ((n = virXPathNodeSet("./devnode", ctxt, &nodes)) < 0)
        goto error;

    if (VIR_ALLOC_N(def->devlinks, n + 1) < 0)
        goto error;

    for (i = 0, m = 0; i < n; i++) {
        xmlNodePtr node = nodes[i];
        char *tmp = virXMLPropString(node, "type");
1958
        int val;
1959 1960 1961 1962 1963 1964 1965

        if (!tmp) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing devnode type"));
            goto error;
        }

1966 1967 1968
        val = virNodeDevDevnodeTypeFromString(tmp);

        if (val < 0) {
1969 1970 1971 1972 1973
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unknown devnode type '%s'"), tmp);
            VIR_FREE(tmp);
            goto error;
        }
1974
        VIR_FREE(tmp);
1975

1976
        switch ((virNodeDevDevnodeType)val) {
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
        case VIR_NODE_DEV_DEVNODE_DEV:
            def->devnode = (char*)xmlNodeGetContent(node);
            break;
        case VIR_NODE_DEV_DEVNODE_LINK:
            def->devlinks[m++] = (char*)xmlNodeGetContent(node);
            break;
        case VIR_NODE_DEV_DEVNODE_LAST:
            break;
        }
    }

1988
    /* Extract device parent, if any */
1989
    def->parent = virXPathString("string(./parent[1])", ctxt);
1990 1991
    def->parent_wwnn = virXPathString("string(./parent[1]/@wwnn)", ctxt);
    def->parent_wwpn = virXPathString("string(./parent[1]/@wwpn)", ctxt);
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
    if (def->parent_wwnn && !def->parent_wwpn) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("when providing parent wwnn='%s', the "
                         "wwpn must also be provided"),
                       def->parent_wwnn);
        goto error;
    }

    if (!def->parent_wwnn && def->parent_wwpn) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("when providing parent wwpn='%s', the "
                         "wwnn must also be provided"),
                       def->parent_wwpn);
2005 2006 2007 2008
        goto error;
    }
    def->parent_fabric_wwn = virXPathString("string(./parent[1]/@fabric_wwn)",
                                            ctxt);
2009 2010

    /* Parse device capabilities */
2011
    VIR_FREE(nodes);
2012
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0)
2013 2014 2015
        goto error;

    if (n == 0) {
2016 2017 2018
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no device capabilities for '%s'"),
                       def->name);
2019 2020 2021 2022
        goto error;
    }

    next_cap = &def->caps;
2023
    for (i = 0; i < n; i++) {
2024 2025 2026 2027
        *next_cap = virNodeDevCapsDefParseXML(ctxt, def,
                                              nodes[i],
                                              create,
                                              virt_type);
2028
        if (!*next_cap)
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
            goto error;

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

    return def;

 error:
    virNodeDeviceDefFree(def);
2039
    VIR_FREE(nodes);
2040 2041 2042
    return NULL;
}

2043

2044
virNodeDeviceDefPtr
2045
virNodeDeviceDefParseNode(xmlDocPtr xml,
2046
                          xmlNodePtr root,
2047 2048
                          int create,
                          const char *virt_type)
2049 2050 2051 2052
{
    xmlXPathContextPtr ctxt = NULL;
    virNodeDeviceDefPtr def = NULL;

2053
    if (!virXMLNodeNameEqual(root, "device")) {
2054 2055 2056 2057
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s> "
                         "expecting <device>"),
                       root->name);
2058 2059 2060 2061 2062
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
2063
        virReportOOMError();
2064 2065 2066 2067
        goto cleanup;
    }

    ctxt->node = root;
2068
    def = virNodeDeviceDefParseXML(ctxt, create, virt_type);
2069

2070
 cleanup:
2071 2072 2073 2074
    xmlXPathFreeContext(ctxt);
    return def;
}

2075

2076
static virNodeDeviceDefPtr
2077
virNodeDeviceDefParse(const char *str,
2078
                      const char *filename,
2079 2080
                      int create,
                      const char *virt_type)
2081
{
J
Jiri Denemark 已提交
2082
    xmlDocPtr xml;
2083 2084
    virNodeDeviceDefPtr def = NULL;

2085
    if ((xml = virXMLParse(filename, str, _("(node_device_definition)")))) {
2086 2087
        def = virNodeDeviceDefParseNode(xml, xmlDocGetRootElement(xml),
                                        create, virt_type);
J
Jiri Denemark 已提交
2088
        xmlFreeDoc(xml);
2089 2090
    }

2091 2092 2093
    return def;
}

2094

2095
virNodeDeviceDefPtr
2096
virNodeDeviceDefParseString(const char *str,
2097 2098
                            int create,
                            const char *virt_type)
2099
{
2100
    return virNodeDeviceDefParse(str, NULL, create, virt_type);
2101 2102
}

2103

2104
virNodeDeviceDefPtr
2105
virNodeDeviceDefParseFile(const char *filename,
2106 2107
                          int create,
                          const char *virt_type)
2108
{
2109
    return virNodeDeviceDefParse(NULL, filename, create, virt_type);
2110 2111
}

2112

2113 2114 2115 2116
/*
 * Return fc_host dev's WWNN and WWPN
 */
int
2117
virNodeDeviceGetWWNs(virNodeDeviceDefPtr def,
2118 2119 2120 2121
                     char **wwnn,
                     char **wwpn)
{
    virNodeDevCapsDefPtr cap = NULL;
2122
    int ret = -1;
2123 2124 2125

    cap = def->caps;
    while (cap != NULL) {
2126
        if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST &&
2127
            cap->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
2128 2129 2130 2131 2132 2133
            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;
            }
2134 2135 2136 2137 2138 2139 2140
            break;
        }

        cap = cap->next;
    }

    if (cap == NULL) {
2141 2142
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Device is not a fibre channel HBA"));
2143
        goto cleanup;
2144 2145
    }

2146
    ret = 0;
2147
 cleanup:
2148 2149 2150
    return ret;
}

2151

2152 2153
void
virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
2154
{
2155
    size_t i = 0;
2156
    virNodeDevCapDataPtr data = &caps->data;
2157

2158
    switch (caps->data.type) {
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    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);
2171
        VIR_FREE(data->pci_dev.physical_function);
2172
        for (i = 0; i < data->pci_dev.num_virtual_functions; i++)
2173
            VIR_FREE(data->pci_dev.virtual_functions[i]);
2174
        VIR_FREE(data->pci_dev.virtual_functions);
2175
        for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++)
2176
            VIR_FREE(data->pci_dev.iommuGroupDevices[i]);
2177
        VIR_FREE(data->pci_dev.iommuGroupDevices);
2178
        virPCIEDeviceInfoFree(data->pci_dev.pci_express);
2179
        for (i = 0; i < data->pci_dev.nmdev_types; i++)
2180
            virMediatedDeviceTypeFree(data->pci_dev.mdev_types[i]);
2181
        VIR_FREE(data->pci_dev.mdev_types);
2182 2183 2184 2185 2186 2187 2188 2189 2190
        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:
2191
        VIR_FREE(data->net.ifname);
2192
        VIR_FREE(data->net.address);
2193 2194
        virBitmapFree(data->net.features);
        data->net.features = NULL;
2195 2196
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
2197 2198
        VIR_FREE(data->scsi_host.wwnn);
        VIR_FREE(data->scsi_host.wwpn);
O
Osier Yang 已提交
2199
        VIR_FREE(data->scsi_host.fabric_wwn);
2200
        break;
D
David Allan 已提交
2201 2202
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
        VIR_FREE(data->scsi_target.name);
2203 2204
        VIR_FREE(data->scsi_target.rport);
        VIR_FREE(data->scsi_target.wwpn);
D
David Allan 已提交
2205
        break;
2206 2207 2208 2209
    case VIR_NODE_DEV_CAP_SCSI:
        VIR_FREE(data->scsi.type);
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
2210
        VIR_FREE(data->storage.block);
2211 2212 2213 2214
        VIR_FREE(data->storage.bus);
        VIR_FREE(data->storage.drive_type);
        VIR_FREE(data->storage.model);
        VIR_FREE(data->storage.vendor);
2215
        VIR_FREE(data->storage.serial);
2216
        VIR_FREE(data->storage.media_label);
2217
        break;
2218 2219 2220
    case VIR_NODE_DEV_CAP_SCSI_GENERIC:
        VIR_FREE(data->sg.path);
        break;
2221
    case VIR_NODE_DEV_CAP_MDEV:
2222 2223
        VIR_FREE(data->mdev.type);
        break;
2224
    case VIR_NODE_DEV_CAP_MDEV_TYPES:
M
Marc-André Lureau 已提交
2225
    case VIR_NODE_DEV_CAP_DRM:
2226 2227
    case VIR_NODE_DEV_CAP_FC_HOST:
    case VIR_NODE_DEV_CAP_VPORTS:
B
Bjoern Walk 已提交
2228
    case VIR_NODE_DEV_CAP_CCW_DEV:
2229 2230 2231 2232 2233 2234 2235 2236
    case VIR_NODE_DEV_CAP_LAST:
        /* This case is here to shutup the compiler */
        break;
    }

    VIR_FREE(caps);
}

D
Daniel P. Berrange 已提交
2237

2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
int
virNodeDeviceUpdateCaps(virNodeDeviceDefPtr def)
{
    virNodeDevCapsDefPtr cap = def->caps;

    while (cap) {
        switch (cap->data.type) {
        case VIR_NODE_DEV_CAP_SCSI_HOST:
            virNodeDeviceGetSCSIHostCaps(&cap->data.scsi_host);
            break;
        case VIR_NODE_DEV_CAP_SCSI_TARGET:
            virNodeDeviceGetSCSITargetCaps(def->sysfs_path,
                                           &cap->data.scsi_target);
            break;
        case VIR_NODE_DEV_CAP_NET:
            if (virNetDevGetLinkInfo(cap->data.net.ifname,
                                     &cap->data.net.lnk) < 0)
                return -1;
            virBitmapFree(cap->data.net.features);
            if (virNetDevGetFeatures(cap->data.net.ifname,
                                     &cap->data.net.features) < 0)
                return -1;
            break;
        case VIR_NODE_DEV_CAP_PCI_DEV:
            if (virNodeDeviceGetPCIDynamicCaps(def->sysfs_path,
                                               &cap->data.pci_dev) < 0)
                return -1;
            break;

            /* all types that (supposedly) don't require any updates
             * relative to what's in the cache.
             */
        case VIR_NODE_DEV_CAP_DRM:
        case VIR_NODE_DEV_CAP_SYSTEM:
        case VIR_NODE_DEV_CAP_USB_DEV:
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
        case VIR_NODE_DEV_CAP_SCSI:
        case VIR_NODE_DEV_CAP_STORAGE:
        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_MDEV_TYPES:
        case VIR_NODE_DEV_CAP_MDEV:
        case VIR_NODE_DEV_CAP_CCW_DEV:
        case VIR_NODE_DEV_CAP_LAST:
            break;
        }
        cap = cap->next;
    }

    return 0;
}


2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
/**
 * virNodeDeviceCapsListExport:
 * @def: node device definition
 * @list: pointer to an array to store all supported capabilities by a device
 *
 * Takes the definition, scans through all the capabilities that the device
 * supports (including the nested caps) and populates a newly allocated list
 * with them. Caller is responsible for freeing the list.
 * If NULL is passed to @list, only the number of caps will be returned.
 *
 * Returns the number of capabilities the device supports, -1 on error.
 */
int
virNodeDeviceCapsListExport(virNodeDeviceDefPtr def,
                            virNodeDevCapType **list)
{
    virNodeDevCapsDefPtr caps = NULL;
    virNodeDevCapType *tmp = NULL;
    bool want_list = !!list;
    int ncaps = 0;
    int ret = -1;

#define MAYBE_ADD_CAP(cap) \
    do { \
        if (want_list) \
            tmp[ncaps] = cap; \
    } while (0)

2320 2321 2322
    if (virNodeDeviceUpdateCaps(def) < 0)
        goto cleanup;

2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
    if (want_list && VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST - 1) < 0)
        goto cleanup;

    for (caps = def->caps; caps; caps = caps->next) {
        unsigned int flags;

        MAYBE_ADD_CAP(caps->data.type);
        ncaps++;

        /* check nested caps for a given type as well */
        if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
            flags = caps->data.scsi_host.flags;

            if (flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
                MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_FC_HOST);
                ncaps++;
            }

            if (flags  & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
                MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_VPORTS);
                ncaps++;
            }
        }

        if (caps->data.type == VIR_NODE_DEV_CAP_PCI_DEV) {
            flags = caps->data.pci_dev.flags;

            if (flags & VIR_NODE_DEV_CAP_FLAG_PCI_MDEV) {
                MAYBE_ADD_CAP(VIR_NODE_DEV_CAP_MDEV_TYPES);
                ncaps++;
            }
        }
    }

#undef MAYBE_ADD_CAP

    if (want_list)
        VIR_STEAL_PTR(*list, tmp);
    ret = ncaps;
 cleanup:
    VIR_FREE(tmp);
    return ret;
}


2368 2369
#ifdef __linux__

2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
int
virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHostPtr scsi_host)
{
    char *tmp = NULL;
    int ret = -1;

    if ((scsi_host->unique_id =
         virSCSIHostGetUniqueId(NULL, scsi_host->host)) < 0) {
        VIR_DEBUG("Failed to read unique_id for host%d", scsi_host->host);
        scsi_host->unique_id = -1;
    }

    VIR_DEBUG("Checking if host%d is an FC HBA", scsi_host->host);

    if (virVHBAPathExists(NULL, scsi_host->host)) {
        scsi_host->flags |= VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST;

        if (!(tmp = virVHBAGetConfig(NULL, scsi_host->host, "port_name"))) {
            VIR_WARN("Failed to read WWPN for host%d", scsi_host->host);
            goto cleanup;
        }
        VIR_FREE(scsi_host->wwpn);
        VIR_STEAL_PTR(scsi_host->wwpn, tmp);

        if (!(tmp = virVHBAGetConfig(NULL, scsi_host->host, "node_name"))) {
            VIR_WARN("Failed to read WWNN for host%d", scsi_host->host);
            goto cleanup;
        }
        VIR_FREE(scsi_host->wwnn);
        VIR_STEAL_PTR(scsi_host->wwnn, tmp);

        if ((tmp = virVHBAGetConfig(NULL, scsi_host->host, "fabric_name"))) {
            VIR_FREE(scsi_host->fabric_wwn);
            VIR_STEAL_PTR(scsi_host->fabric_wwn, tmp);
        }
    }

    if (virVHBAIsVportCapable(NULL, scsi_host->host)) {
        scsi_host->flags |= VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS;

        if (!(tmp = virVHBAGetConfig(NULL, scsi_host->host,
                                     "max_npiv_vports"))) {
            VIR_WARN("Failed to read max_npiv_vports for host%d",
                     scsi_host->host);
            goto cleanup;
        }

        if (virStrToLong_i(tmp, NULL, 10, &scsi_host->max_vports) < 0) {
            VIR_WARN("Failed to parse value of max_npiv_vports '%s'", tmp);
            goto cleanup;
        }

        VIR_FREE(tmp);
        if (!(tmp = virVHBAGetConfig(NULL, scsi_host->host,
                                      "npiv_vports_inuse"))) {
            VIR_WARN("Failed to read npiv_vports_inuse for host%d",
                     scsi_host->host);
            goto cleanup;
        }

        if (virStrToLong_i(tmp, NULL, 10, &scsi_host->vports) < 0) {
            VIR_WARN("Failed to parse value of npiv_vports_inuse '%s'", tmp);
            goto cleanup;
        }
    }

    ret = 0;
 cleanup:
    if (ret < 0) {
        /* Clear the two flags in case of producing confusing XML output */
        scsi_host->flags &= ~(VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST |
                              VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS);

        VIR_FREE(scsi_host->wwnn);
        VIR_FREE(scsi_host->wwpn);
        VIR_FREE(scsi_host->fabric_wwn);
    }
    VIR_FREE(tmp);
    return ret;
}
2450

2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575

int
virNodeDeviceGetSCSITargetCaps(const char *sysfsPath,
                               virNodeDevCapSCSITargetPtr scsi_target)
{
    int ret = -1;
    char *dir = NULL, *rport = NULL;

    VIR_DEBUG("Checking if '%s' is an FC remote port", scsi_target->name);

    /* /sys/devices/[...]/host0/rport-0:0-0/target0:0:0 -> rport-0:0-0 */
    if (!(dir = mdir_name(sysfsPath)))
        return -1;

    if (VIR_STRDUP(rport, last_component(dir)) < 0)
        goto cleanup;

    if (!virFCIsCapableRport(rport))
        goto cleanup;

    VIR_FREE(scsi_target->rport);
    VIR_STEAL_PTR(scsi_target->rport, rport);

    if (virFCReadRportValue(scsi_target->rport, "port_name",
                            &scsi_target->wwpn) < 0) {
        VIR_WARN("Failed to read port_name for '%s'", scsi_target->rport);
        goto cleanup;
    }

    scsi_target->flags |= VIR_NODE_DEV_CAP_FLAG_FC_RPORT;
    ret = 0;

 cleanup:
    if (ret < 0) {
        VIR_FREE(scsi_target->rport);
        VIR_FREE(scsi_target->wwpn);
        scsi_target->flags &= ~VIR_NODE_DEV_CAP_FLAG_FC_RPORT;
    }
    VIR_FREE(rport);
    VIR_FREE(dir);

    return ret;
}


static int
virNodeDeviceGetPCISRIOVCaps(const char *sysfsPath,
                             virNodeDevCapPCIDevPtr pci_dev)
{
    size_t i;
    int ret;

    /* this could be a refresh, so clear out the old data */
    for (i = 0; i < pci_dev->num_virtual_functions; i++)
       VIR_FREE(pci_dev->virtual_functions[i]);
    VIR_FREE(pci_dev->virtual_functions);
    pci_dev->num_virtual_functions = 0;
    pci_dev->max_virtual_functions = 0;
    pci_dev->flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;
    pci_dev->flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;

    ret = virPCIGetPhysicalFunction(sysfsPath,
                                    &pci_dev->physical_function);
    if (ret < 0)
        goto cleanup;

    if (pci_dev->physical_function)
        pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;

    ret = virPCIGetVirtualFunctions(sysfsPath, &pci_dev->virtual_functions,
                                    &pci_dev->num_virtual_functions,
                                    &pci_dev->max_virtual_functions);
    if (ret < 0)
        goto cleanup;

    if (pci_dev->num_virtual_functions > 0 ||
        pci_dev->max_virtual_functions > 0)
        pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;

 cleanup:
    return ret;
}


static int
virNodeDeviceGetPCIIOMMUGroupCaps(virNodeDevCapPCIDevPtr pci_dev)
{
    size_t i;
    int tmpGroup, ret = -1;
    virPCIDeviceAddress addr;

    /* this could be a refresh, so clear out the old data */
    for (i = 0; i < pci_dev->nIommuGroupDevices; i++)
       VIR_FREE(pci_dev->iommuGroupDevices[i]);
    VIR_FREE(pci_dev->iommuGroupDevices);
    pci_dev->nIommuGroupDevices = 0;
    pci_dev->iommuGroupNumber = 0;

    addr.domain = pci_dev->domain;
    addr.bus = pci_dev->bus;
    addr.slot = pci_dev->slot;
    addr.function = pci_dev->function;
    tmpGroup = virPCIDeviceAddressGetIOMMUGroupNum(&addr);
    if (tmpGroup == -1) {
        /* error was already reported */
        goto cleanup;
    }
    if (tmpGroup == -2) {
        /* -2 return means there is no iommu_group data */
        ret = 0;
        goto cleanup;
    }
    if (tmpGroup >= 0) {
        if (virPCIDeviceAddressGetIOMMUGroupAddresses(&addr, &pci_dev->iommuGroupDevices,
                                                      &pci_dev->nIommuGroupDevices) < 0)
            goto cleanup;
        pci_dev->iommuGroupNumber = tmpGroup;
    }

    ret = 0;
 cleanup:
    return ret;
}


2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
static int
virNodeDeviceGetPCIMdevTypesCaps(const char *sysfspath,
                                 virNodeDevCapPCIDevPtr pci_dev)
{
    virMediatedDeviceTypePtr *types = NULL;
    int rc = 0;
    size_t i;

    /* this could be a refresh, so clear out the old data */
    for (i = 0; i < pci_dev->nmdev_types; i++)
       virMediatedDeviceTypeFree(pci_dev->mdev_types[i]);
    VIR_FREE(pci_dev->mdev_types);
    pci_dev->nmdev_types = 0;
    pci_dev->flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;

    rc = virPCIGetMdevTypes(sysfspath, &types);

    if (rc <= 0)
        return rc;

    VIR_STEAL_PTR(pci_dev->mdev_types, types);
    pci_dev->nmdev_types = rc;
    pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;

    return 0;
}


2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
/* virNodeDeviceGetPCIDynamicCaps() get info that is stored in sysfs
 * about devices related to this device, i.e. things that can change
 * without this device itself changing. These must be refreshed
 * anytime full XML of the device is requested, because they can
 * change with no corresponding notification from the kernel/udev.
 */
int
virNodeDeviceGetPCIDynamicCaps(const char *sysfsPath,
                               virNodeDevCapPCIDevPtr pci_dev)
{
2614 2615 2616
    if (virNodeDeviceGetPCISRIOVCaps(sysfsPath, pci_dev) < 0 ||
        virNodeDeviceGetPCIIOMMUGroupCaps(pci_dev) < 0 ||
        virNodeDeviceGetPCIMdevTypesCaps(sysfsPath, pci_dev) < 0)
2617 2618 2619 2620
        return -1;
    return 0;
}

2621 2622
#else

2623 2624 2625 2626 2627 2628
int
virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHostPtr scsi_host ATTRIBUTE_UNUSED)
{
    return -1;
}

2629
int
2630 2631 2632 2633 2634 2635 2636 2637 2638
virNodeDeviceGetPCIDynamicCaps(const char *sysfsPath ATTRIBUTE_UNUSED,
                               virNodeDevCapPCIDevPtr pci_dev ATTRIBUTE_UNUSED)
{
    return -1;
}


int virNodeDeviceGetSCSITargetCaps(const char *sysfsPath ATTRIBUTE_UNUSED,
                                   virNodeDevCapSCSITargetPtr scsi_target ATTRIBUTE_UNUSED)
2639 2640 2641 2642 2643
{
    return -1;
}

#endif /* __linux__ */