node_device_conf.c 55.5 KB
Newer Older
1 2 3
/*
 * node_device_conf.c: config handling for node devices
 *
4
 * Copyright (C) 2009-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2008 Virtual Iron Software, Inc.
 * Copyright (C) 2008 David F. Lively
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

#include <config.h>

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

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

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

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

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

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

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

    *string = s;
    return 0;
}

E
Eric Blake 已提交
75
int virNodeDeviceHasCap(const virNodeDeviceObj *dev, const char *cap)
76 77 78 79 80 81 82 83 84
{
    virNodeDevCapsDefPtr caps = dev->def->caps;
    while (caps) {
        if (STREQ(cap, virNodeDevCapTypeToString(caps->type)))
            return 1;
        caps = caps->next;
    }
    return 0;
}
85

86 87

virNodeDeviceObjPtr
E
Eric Blake 已提交
88
virNodeDeviceFindBySysfsPath(virNodeDeviceObjListPtr devs,
89 90
                             const char *sysfs_path)
{
91
    size_t i;
92 93 94 95 96 97 98 99 100 101 102 103 104 105

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

    return NULL;
}


E
Eric Blake 已提交
106
virNodeDeviceObjPtr virNodeDeviceFindByName(virNodeDeviceObjListPtr devs,
107 108
                                            const char *name)
{
109
    size_t i;
110

111 112
    for (i = 0; i < devs->count; i++) {
        virNodeDeviceObjLock(devs->objs[i]);
113 114
        if (STREQ(devs->objs[i]->def->name, name))
            return devs->objs[i];
115 116
        virNodeDeviceObjUnlock(devs->objs[i]);
    }
117 118 119 120 121 122 123 124 125 126 127 128 129 130

    return NULL;
}


void virNodeDeviceDefFree(virNodeDeviceDefPtr def)
{
    virNodeDevCapsDefPtr caps;

    if (!def)
        return;

    VIR_FREE(def->name);
    VIR_FREE(def->parent);
131
    VIR_FREE(def->driver);
132 133
    VIR_FREE(def->sysfs_path);
    VIR_FREE(def->parent_sysfs_path);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

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

    VIR_FREE(def);
}

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

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

154 155
    virMutexDestroy(&dev->lock);

156 157 158 159 160
    VIR_FREE(dev);
}

void virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs)
{
161
    size_t i;
162
    for (i = 0; i < devs->count; i++)
163 164 165 166 167
        virNodeDeviceObjFree(devs->objs[i]);
    VIR_FREE(devs->objs);
    devs->count = 0;
}

168
virNodeDeviceObjPtr virNodeDeviceAssignDef(virNodeDeviceObjListPtr devs,
E
Eric Blake 已提交
169
                                           virNodeDeviceDefPtr def)
170 171 172 173 174 175 176 177 178
{
    virNodeDeviceObjPtr device;

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

179
    if (VIR_ALLOC(device) < 0)
180 181
        return NULL;

182
    if (virMutexInit(&device->lock) < 0) {
183 184
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
185 186 187
        VIR_FREE(device);
        return NULL;
    }
188
    virNodeDeviceObjLock(device);
189

190
    if (VIR_APPEND_ELEMENT_COPY(devs->objs, devs->count, device) < 0){
191
        virNodeDeviceObjUnlock(device);
192 193 194
        virNodeDeviceObjFree(device);
        return NULL;
    }
195
    device->def = def;
196 197 198 199 200 201

    return device;

}

void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
E
Eric Blake 已提交
202
                            virNodeDeviceObjPtr dev)
203
{
204
    size_t i;
205

206 207
    virNodeDeviceObjUnlock(dev);

208
    for (i = 0; i < devs->count; i++) {
209
        virNodeDeviceObjLock(dev);
210
        if (devs->objs[i] == dev) {
211
            virNodeDeviceObjUnlock(dev);
212 213
            virNodeDeviceObjFree(devs->objs[i]);

214
            VIR_DELETE_ELEMENT(devs->objs, i, devs->count);
215 216
            break;
        }
217
        virNodeDeviceObjUnlock(dev);
218 219 220
    }
}

E
Eric Blake 已提交
221
char *virNodeDeviceDefFormat(const virNodeDeviceDef *def)
222 223
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
224
    virNodeDevCapsDefPtr caps;
225
    size_t i = 0;
226 227 228

    virBufferAddLit(&buf, "<device>\n");
    virBufferEscapeString(&buf, "  <name>%s</name>\n", def->name);
229
    virBufferEscapeString(&buf, "  <path>%s</path>\n", def->sysfs_path);
230
    if (def->parent) {
231
        virBufferEscapeString(&buf, "  <parent>%s</parent>\n", def->parent);
232
    }
233 234 235 236 237
    if (def->driver) {
        virBufferAddLit(&buf, "  <driver>\n");
        virBufferEscapeString(&buf, "    <name>%s</name>\n", def->driver);
        virBufferAddLit(&buf, "  </driver>\n");
    }
238 239 240 241 242

    for (caps = def->caps; caps; caps = caps->next) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        union _virNodeDevCapData *data = &caps->data;

243
        virBufferAsprintf(&buf, "  <capability type='%s'>\n",
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                          virNodeDevCapTypeToString(caps->type));
        switch (caps->type) {
        case VIR_NODE_DEV_CAP_SYSTEM:
            if (data->system.product_name)
                virBufferEscapeString(&buf, "    <product>%s</product>\n",
                                      data->system.product_name);
            virBufferAddLit(&buf, "    <hardware>\n");
            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);
261
            virBufferAsprintf(&buf, "      <uuid>%s</uuid>\n", uuidstr);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
            virBufferAddLit(&buf, "    </hardware>\n");
            virBufferAddLit(&buf, "    <firmware>\n");
            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);
            virBufferAddLit(&buf, "    </firmware>\n");
            break;
        case VIR_NODE_DEV_CAP_PCI_DEV:
277
            virBufferAsprintf(&buf, "    <domain>%d</domain>\n",
278
                              data->pci_dev.domain);
279 280
            virBufferAsprintf(&buf, "    <bus>%d</bus>\n", data->pci_dev.bus);
            virBufferAsprintf(&buf, "    <slot>%d</slot>\n",
281
                              data->pci_dev.slot);
282
            virBufferAsprintf(&buf, "    <function>%d</function>\n",
283
                              data->pci_dev.function);
284
            virBufferAsprintf(&buf, "    <product id='0x%04x'",
285 286 287 288 289 290
                                  data->pci_dev.product);
            if (data->pci_dev.product_name)
                virBufferEscapeString(&buf, ">%s</product>\n",
                                      data->pci_dev.product_name);
            else
                virBufferAddLit(&buf, " />\n");
291
            virBufferAsprintf(&buf, "    <vendor id='0x%04x'",
292 293 294 295 296 297
                                  data->pci_dev.vendor);
            if (data->pci_dev.vendor_name)
                virBufferEscapeString(&buf, ">%s</vendor>\n",
                                      data->pci_dev.vendor_name);
            else
                virBufferAddLit(&buf, " />\n");
298 299
            if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION) {
                virBufferAddLit(&buf, "    <capability type='phys_function'>\n");
300
                virBufferAsprintf(&buf,
301 302 303 304 305 306 307 308 309 310
                                  "      <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);
                virBufferAddLit(&buf, "    </capability>\n");
            }
            if (data->pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION) {
                virBufferAddLit(&buf, "    <capability type='virt_functions'>\n");
311
                for (i = 0; i < data->pci_dev.num_virtual_functions; i++) {
312
                    virBufferAsprintf(&buf,
313 314 315 316 317 318 319 320 321
                                      "      <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);
                }
                virBufferAddLit(&buf, "    </capability>\n");
            }
322 323 324 325 326 327 328 329 330 331 332 333 334 335
            if (data->pci_dev.nIommuGroupDevices) {
                virBufferAsprintf(&buf, "    <iommuGroup number='%d'>\n",
                                  data->pci_dev.iommuGroupNumber);
                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);
                }
                virBufferAddLit(&buf, "    </iommuGroup>\n");
            }
336 337
            break;
        case VIR_NODE_DEV_CAP_USB_DEV:
338 339
            virBufferAsprintf(&buf, "    <bus>%d</bus>\n", data->usb_dev.bus);
            virBufferAsprintf(&buf, "    <device>%d</device>\n",
340
                              data->usb_dev.device);
341
            virBufferAsprintf(&buf, "    <product id='0x%04x'",
342 343 344 345 346 347
                                  data->usb_dev.product);
            if (data->usb_dev.product_name)
                virBufferEscapeString(&buf, ">%s</product>\n",
                                      data->usb_dev.product_name);
            else
                virBufferAddLit(&buf, " />\n");
348
            virBufferAsprintf(&buf, "    <vendor id='0x%04x'",
349 350 351 352 353 354 355 356
                                  data->usb_dev.vendor);
            if (data->usb_dev.vendor_name)
                virBufferEscapeString(&buf, ">%s</vendor>\n",
                                      data->usb_dev.vendor_name);
            else
                virBufferAddLit(&buf, " />\n");
            break;
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
357
            virBufferAsprintf(&buf, "    <number>%d</number>\n",
358
                              data->usb_if.number);
359
            virBufferAsprintf(&buf, "    <class>%d</class>\n",
360
                              data->usb_if._class);
361
            virBufferAsprintf(&buf, "    <subclass>%d</subclass>\n",
362
                              data->usb_if.subclass);
363
            virBufferAsprintf(&buf, "    <protocol>%d</protocol>\n",
364 365
                              data->usb_if.protocol);
            if (data->usb_if.description)
366 367
                virBufferEscapeString(&buf,
                                  "    <description>%s</description>\n",
368 369 370
                                  data->usb_if.description);
            break;
        case VIR_NODE_DEV_CAP_NET:
371
            virBufferEscapeString(&buf, "    <interface>%s</interface>\n",
372
                              data->net.ifname);
373
            if (data->net.address)
374
                virBufferEscapeString(&buf, "    <address>%s</address>\n",
375 376 377 378
                                  data->net.address);
            if (data->net.subtype != VIR_NODE_DEV_CAP_NET_LAST) {
                const char *subtyp =
                    virNodeDevNetCapTypeToString(data->net.subtype);
379 380
                virBufferEscapeString(&buf, "    <capability type='%s'/>\n",
                                      subtyp);
381 382 383
            }
            break;
        case VIR_NODE_DEV_CAP_SCSI_HOST:
384
            virBufferAsprintf(&buf, "    <host>%d</host>\n",
385
                              data->scsi_host.host);
386 387
            if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
                virBufferAddLit(&buf, "    <capability type='fc_host'>\n");
388 389 390 391
                virBufferEscapeString(&buf, "      <wwnn>%s</wwnn>\n",
                                      data->scsi_host.wwnn);
                virBufferEscapeString(&buf, "      <wwpn>%s</wwpn>\n",
                                      data->scsi_host.wwpn);
O
Osier Yang 已提交
392 393
                virBufferEscapeString(&buf, "      <fabric_wwn>%s</fabric_wwn>\n",
                                      data->scsi_host.fabric_wwn);
394 395 396
                virBufferAddLit(&buf, "    </capability>\n");
            }
            if (data->scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
397 398 399 400 401 402
                virBufferAddLit(&buf, "    <capability type='vport_ops'>\n");
                virBufferAsprintf(&buf, "      <max_vports>%d</max_vports>\n",
                                  data->scsi_host.max_vports);
                virBufferAsprintf(&buf, "      <vports>%d</vports>\n",
                                  data->scsi_host.vports);
                virBufferAddLit(&buf, "    </capability>\n");
403 404
            }

405
            break;
D
David Allan 已提交
406 407

        case VIR_NODE_DEV_CAP_SCSI_TARGET:
408 409
            virBufferEscapeString(&buf, "    <target>%s</target>\n",
                                  data->scsi_target.name);
D
David Allan 已提交
410 411
            break;

412
        case VIR_NODE_DEV_CAP_SCSI:
413 414 415
            virBufferAsprintf(&buf, "    <host>%d</host>\n", data->scsi.host);
            virBufferAsprintf(&buf, "    <bus>%d</bus>\n", data->scsi.bus);
            virBufferAsprintf(&buf, "    <target>%d</target>\n",
416
                              data->scsi.target);
417
            virBufferAsprintf(&buf, "    <lun>%d</lun>\n", data->scsi.lun);
418
            if (data->scsi.type)
419 420
                virBufferEscapeString(&buf, "    <type>%s</type>\n",
                                      data->scsi.type);
421 422
            break;
        case VIR_NODE_DEV_CAP_STORAGE:
423
            virBufferEscapeString(&buf, "    <block>%s</block>\n",
424
                              data->storage.block);
425
            if (data->storage.bus)
426
                virBufferEscapeString(&buf, "    <bus>%s</bus>\n",
427 428
                                  data->storage.bus);
            if (data->storage.drive_type)
429
                virBufferEscapeString(&buf, "    <drive_type>%s</drive_type>\n",
430 431
                                  data->storage.drive_type);
            if (data->storage.model)
432
                virBufferEscapeString(&buf, "    <model>%s</model>\n",
433 434
                                  data->storage.model);
            if (data->storage.vendor)
435
                virBufferEscapeString(&buf, "    <vendor>%s</vendor>\n",
436
                                  data->storage.vendor);
437
            if (data->storage.serial)
438
                virBufferAsprintf(&buf, "    <serial>%s</serial>\n",
439
                                  data->storage.serial);
440 441 442 443
            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");
444
                virBufferAsprintf(&buf,
445 446
                                  "      <media_available>%d"
                                  "</media_available>\n", avl ? 1 : 0);
447
                virBufferAsprintf(&buf, "      <media_size>%llu</media_size>\n",
448
                                  data->storage.removable_media_size);
449 450 451 452 453
                if (data->storage.media_label)
                    virBufferEscapeString(&buf,
                                      "      <media_label>%s</media_label>\n",
                                      data->storage.media_label);

454
                if (data->storage.logical_block_size > 0)
455
                    virBufferAsprintf(&buf, "      <logical_block_size>%llu"
456 457 458
                                      "</logical_block_size>\n",
                                      data->storage.logical_block_size);
                if (data->storage.num_blocks > 0)
459
                    virBufferAsprintf(&buf,
460 461
                                      "      <num_blocks>%llu</num_blocks>\n",
                                      data->storage.num_blocks);
462 463
                virBufferAddLit(&buf, "    </capability>\n");
            } else {
464
                virBufferAsprintf(&buf, "    <size>%llu</size>\n",
465
                                  data->storage.size);
466
                if (data->storage.logical_block_size > 0)
467
                    virBufferAsprintf(&buf, "    <logical_block_size>%llu"
468 469 470
                                      "</logical_block_size>\n",
                                      data->storage.logical_block_size);
                if (data->storage.num_blocks > 0)
471
                    virBufferAsprintf(&buf,
472 473
                                      "    <num_blocks>%llu</num_blocks>\n",
                                      data->storage.num_blocks);
474 475 476 477 478
            }
            if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE)
                virBufferAddLit(&buf,
                                "    <capability type='hotpluggable' />\n");
            break;
479 480 481 482
        case VIR_NODE_DEV_CAP_SCSI_GENERIC:
            virBufferEscapeString(&buf, "    <char>%s</char>\n",
                                  data->sg.path);
            break;
483 484
        case VIR_NODE_DEV_CAP_FC_HOST:
        case VIR_NODE_DEV_CAP_VPORTS:
485
        case VIR_NODE_DEV_CAP_LAST:
486
        default:
487 488 489 490 491 492 493 494 495 496 497 498 499 500
            break;
        }

        virBufferAddLit(&buf, "  </capability>\n");
    }

    virBufferAddLit(&buf, "</device>\n");

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

 no_memory:
501
    virReportOOMError();
502
    virBufferFreeAndReset(&buf);
503 504 505
    return NULL;
}

506
static int
507
virNodeDevCapsDefParseULong(const char *xpath,
508 509 510 511 512 513 514 515 516
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

517
    ret = virXPathULong(xpath, ctxt, &val);
518
    if (ret < 0) {
519 520 521
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
522 523 524 525 526 527 528 529
        return -1;
    }

    *value = val;
    return 0;
}

static int
530
virNodeDevCapsDefParseULongLong(const char *xpath,
531 532 533 534 535 536 537 538 539
                                xmlXPathContextPtr ctxt,
                                unsigned long long *value,
                                virNodeDeviceDefPtr def,
                                const char *missing_error_fmt,
                                const char *invalid_error_fmt)
{
    int ret;
    unsigned long long val;

540
    ret = virXPathULongLong(xpath, ctxt, &val);
541
    if (ret < 0) {
542 543 544
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
545 546 547 548 549 550 551 552
        return -1;
    }

    *value = val;
    return 0;
}

static int
553
virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt,
554 555 556 557 558
                             virNodeDeviceDefPtr def,
                             xmlNodePtr node,
                             union _virNodeDevCapData *data)
{
    xmlNodePtr orignode, *nodes = NULL;
559 560
    size_t i;
    int n, ret = -1;
561 562 563 564 565
    unsigned long long val;

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

566
    data->storage.block = virXPathString("string(./block[1])", ctxt);
567
    if (!data->storage.block) {
568 569 570
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no block device path supplied for '%s'"),
                       def->name);
571 572 573
        goto out;
    }

574 575 576 577 578
    data->storage.bus        = virXPathString("string(./bus[1])", ctxt);
    data->storage.drive_type = virXPathString("string(./drive_type[1])", ctxt);
    data->storage.model      = virXPathString("string(./model[1])", ctxt);
    data->storage.vendor     = virXPathString("string(./vendor[1])", ctxt);
    data->storage.serial     = virXPathString("string(./serial[1])", ctxt);
579

580
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
581 582 583
        goto out;
    }

584
    for (i = 0; i < n; i++) {
585 586 587
        char *type = virXMLPropString(nodes[i], "type");

        if (!type) {
588 589 590
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing storage capability type for '%s'"),
                           def->name);
591 592 593 594 595 596 597 598 599 600 601 602 603
            goto out;
        }

        if (STREQ(type, "hotpluggable"))
            data->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE;
        else if (STREQ(type, "removable")) {
            xmlNodePtr orignode2;

            data->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE;

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

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

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

609
            val = 0;
610
            if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def,
611 612 613 614 615 616 617 618 619 620
                                                _("no removable media size supplied for '%s'"),
                                                _("invalid removable media size supplied for '%s'")) < 0) {
                ctxt->node = orignode2;
                VIR_FREE(type);
                goto out;
            }
            data->storage.removable_media_size = val;

            ctxt->node = orignode2;
        } else {
621 622 623
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown storage capability type '%s' for '%s'"),
                           type, def->name);
624 625 626 627 628 629 630 631 632
            VIR_FREE(type);
            goto out;
        }

        VIR_FREE(type);
    }

    if (!(data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE)) {
        val = 0;
633
        if (virNodeDevCapsDefParseULongLong("number(./size[1])", ctxt, &val, def,
634 635 636 637 638 639 640 641 642 643 644 645 646 647
                                            _("no size supplied for '%s'"),
                                            _("invalid size supplied for '%s'")) < 0)
            goto out;
        data->storage.size = val;
    }

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

static int
648
virNodeDevCapScsiParseXML(xmlXPathContextPtr ctxt,
649 650 651 652 653 654 655 656 657 658
                          virNodeDeviceDefPtr def,
                          xmlNodePtr node,
                          union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

659
    if (virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
660 661 662 663 664
                                    &data->scsi.host, def,
                                    _("no SCSI host ID supplied for '%s'"),
                                    _("invalid SCSI host ID supplied for '%s'")) < 0)
        goto out;

665
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
666 667 668 669 670
                                    &data->scsi.bus, def,
                                    _("no SCSI bus ID supplied for '%s'"),
                                    _("invalid SCSI bus ID supplied for '%s'")) < 0)
        goto out;

671
    if (virNodeDevCapsDefParseULong("number(./target[1])", ctxt,
672 673 674 675 676
                                    &data->scsi.target, def,
                                    _("no SCSI target ID supplied for '%s'"),
                                    _("invalid SCSI target ID supplied for '%s'")) < 0)
        goto out;

677
    if (virNodeDevCapsDefParseULong("number(./lun[1])", ctxt,
678 679 680 681 682
                                    &data->scsi.lun, def,
                                    _("no SCSI LUN ID supplied for '%s'"),
                                    _("invalid SCSI LUN ID supplied for '%s'")) < 0)
        goto out;

683
    data->scsi.type = virXPathString("string(./type[1])", ctxt);
684 685 686 687 688 689 690

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

D
David Allan 已提交
691 692

static int
693
virNodeDevCapScsiTargetParseXML(xmlXPathContextPtr ctxt,
D
David Allan 已提交
694 695 696 697 698 699 700 701 702 703
                                virNodeDeviceDefPtr def,
                                xmlNodePtr node,
                                union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

704
    data->scsi_target.name = virXPathString("string(./name[1])", ctxt);
D
David Allan 已提交
705
    if (!data->scsi_target.name) {
706 707 708
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no target name supplied for '%s'"),
                       def->name);
D
David Allan 已提交
709 710 711 712 713 714 715 716 717 718 719
        goto out;
    }

    ret = 0;

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


720
static int
721
virNodeDevCapScsiHostParseXML(xmlXPathContextPtr ctxt,
722 723
                              virNodeDeviceDefPtr def,
                              xmlNodePtr node,
724
                              union _virNodeDevCapData *data,
725 726
                              int create,
                              const char *virt_type)
727
{
728
    xmlNodePtr orignode, *nodes = NULL;
729 730
    int ret = -1, n = 0;
    size_t i;
731
    char *type = NULL;
732 733 734 735

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

736
    if (create == EXISTING_DEVICE &&
737
        virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
738 739
                                    &data->scsi_host.host, def,
                                    _("no SCSI host ID supplied for '%s'"),
740 741 742 743
                                    _("invalid SCSI host ID supplied for '%s'")) < 0) {
        goto out;
    }

744
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
745
        goto out;
746 747
    }

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

        if (!type) {
752 753 754
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing SCSI host capability type for '%s'"),
                           def->name);
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            goto out;
        }

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

            data->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS;

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

            xmlNodePtr orignode2;

            data->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST;

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

771
            if (virNodeDevCapsDefParseString("string(./wwnn[1])",
772
                                             ctxt,
773 774
                                             &data->scsi_host.wwnn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwnn, virt_type) < 0) {
775 776 777 778
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWNN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
779 780
                    goto out;
                }
781 782
            }

783
            if (virNodeDevCapsDefParseString("string(./wwpn[1])",
784
                                             ctxt,
785 786
                                             &data->scsi_host.wwpn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwpn, virt_type) < 0) {
787 788 789 790
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWPN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
791 792
                    goto out;
                }
793 794 795 796 797
            }

            ctxt->node = orignode2;

        } else {
798 799 800
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown SCSI host capability type '%s' for '%s'"),
                           type, def->name);
801 802 803 804 805
            goto out;
        }

        VIR_FREE(type);
    }
806 807

    ret = 0;
808

809
out:
810
    VIR_FREE(type);
811
    ctxt->node = orignode;
812
    VIR_FREE(nodes);
813 814 815
    return ret;
}

816

817
static int
818
virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt,
819 820 821 822 823 824 825 826 827 828 829
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
                         union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

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

830
    data->net.ifname = virXPathString("string(./interface[1])", ctxt);
831
    if (!data->net.ifname) {
832 833 834
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no network interface supplied for '%s'"),
                       def->name);
835 836 837
        goto out;
    }

838
    data->net.address = virXPathString("string(./address[1])", ctxt);
839 840 841

    data->net.subtype = VIR_NODE_DEV_CAP_NET_LAST;

842
    tmp = virXPathString("string(./capability/@type)", ctxt);
843 844 845 846
    if (tmp) {
        int val = virNodeDevNetCapTypeFromString(tmp);
        VIR_FREE(tmp);
        if (val < 0) {
847
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
848 849
                           _("invalid network type supplied for '%s'"),
                           def->name);
850 851 852 853 854 855 856 857 858 859 860 861
            goto out;
        }
        data->net.subtype = val;
    }

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

static int
862
virNodeDevCapUsbInterfaceParseXML(xmlXPathContextPtr ctxt,
863 864 865 866 867 868 869 870 871 872
                                  virNodeDeviceDefPtr def,
                                  xmlNodePtr node,
                                  union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

873
    if (virNodeDevCapsDefParseULong("number(./number[1])", ctxt,
874 875 876 877 878
                                    &data->usb_if.number, def,
                                    _("no USB interface number supplied for '%s'"),
                                    _("invalid USB interface number supplied for '%s'")) < 0)
        goto out;

879
    if (virNodeDevCapsDefParseULong("number(./class[1])", ctxt,
880 881 882 883 884
                                    &data->usb_if._class, def,
                                    _("no USB interface class supplied for '%s'"),
                                    _("invalid USB interface class supplied for '%s'")) < 0)
        goto out;

885
    if (virNodeDevCapsDefParseULong("number(./subclass[1])", ctxt,
886 887 888 889 890
                                    &data->usb_if.subclass, def,
                                    _("no USB interface subclass supplied for '%s'"),
                                    _("invalid USB interface subclass supplied for '%s'")) < 0)
        goto out;

891
    if (virNodeDevCapsDefParseULong("number(./protocol[1])", ctxt,
892 893 894 895 896
                                    &data->usb_if.protocol, def,
                                    _("no USB interface protocol supplied for '%s'"),
                                    _("invalid USB interface protocol supplied for '%s'")) < 0)
        goto out;

897
    data->usb_if.description = virXPathString("string(./description[1])", ctxt);
898 899 900 901 902 903 904 905

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

static int
906
virNodeDevCapsDefParseHexId(const char *xpath,
907 908 909 910 911 912 913 914 915
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

916
    ret = virXPathULongHex(xpath, ctxt, &val);
917
    if (ret < 0) {
918 919 920
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
921 922 923 924 925 926 927 928
        return -1;
    }

    *value = val;
    return 0;
}

static int
929
virNodeDevCapUsbDevParseXML(xmlXPathContextPtr ctxt,
930 931 932 933 934 935 936 937 938 939
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

940
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
941 942 943 944 945
                                    &data->usb_dev.bus, def,
                                    _("no USB bus number supplied for '%s'"),
                                    _("invalid USB bus number supplied for '%s'")) < 0)
        goto out;

946
    if (virNodeDevCapsDefParseULong("number(./device[1])", ctxt,
947 948 949 950 951
                                    &data->usb_dev.device, def,
                                    _("no USB device number supplied for '%s'"),
                                    _("invalid USB device number supplied for '%s'")) < 0)
        goto out;

952
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
953 954 955 956 957
                                    &data->usb_dev.vendor, def,
                                    _("no USB vendor ID supplied for '%s'"),
                                    _("invalid USB vendor ID supplied for '%s'")) < 0)
        goto out;

958
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
959 960 961 962 963
                                    &data->usb_dev.product, def,
                                    _("no USB product ID supplied for '%s'"),
                                    _("invalid USB product ID supplied for '%s'")) < 0)
        goto out;

964 965
    data->usb_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->usb_dev.product_name = virXPathString("string(./product[1])", ctxt);
966 967 968 969 970 971 972

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

973 974 975 976 977 978 979 980
static int
virNodeDevCapPciDevIommuGroupParseXML(xmlXPathContextPtr ctxt,
                                      xmlNodePtr iommuGroupNode,
                                      union _virNodeDevCapData *data)
{
    xmlNodePtr origNode = ctxt->node;
    xmlNodePtr *addrNodes = NULL;
    char *numberStr = NULL;
981 982
    int nAddrNodes, ret = -1;
    size_t i;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    virPCIDeviceAddressPtr pciAddr = NULL;

    ctxt->node = iommuGroupNode;

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

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

1004
    for (i = 0; i < nAddrNodes; i++) {
1005
        virDevicePCIAddress addr = { 0, 0, 0, 0, 0 };
1006
        if (virDevicePCIAddressParseXML(addrNodes[i], &addr) < 0)
1007
            goto cleanup;
1008
        if (VIR_ALLOC(pciAddr) < 0)
1009 1010 1011 1012 1013 1014 1015
            goto cleanup;
        pciAddr->domain = addr.domain;
        pciAddr->bus = addr.bus;
        pciAddr->slot = addr.slot;
        pciAddr->function = addr.function;
        if (VIR_APPEND_ELEMENT(data->pci_dev.iommuGroupDevices,
                               data->pci_dev.nIommuGroupDevices,
1016
                               pciAddr) < 0)
1017 1018 1019 1020 1021 1022
            goto cleanup;
    }

    ret = 0;
cleanup:
    ctxt->node = origNode;
1023
    VIR_FREE(numberStr);
1024 1025 1026 1027 1028 1029
    VIR_FREE(addrNodes);
    VIR_FREE(pciAddr);
    return ret;
}


1030
static int
1031
virNodeDevCapPciDevParseXML(xmlXPathContextPtr ctxt,
1032 1033 1034 1035
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
1036
    xmlNodePtr orignode, iommuGroupNode;
1037 1038 1039 1040 1041
    int ret = -1;

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

1042
    if (virNodeDevCapsDefParseULong("number(./domain[1])", ctxt,
1043 1044 1045 1046 1047
                                    &data->pci_dev.domain, def,
                                    _("no PCI domain ID supplied for '%s'"),
                                    _("invalid PCI domain ID supplied for '%s'")) < 0)
        goto out;

1048
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
1049 1050 1051 1052 1053
                                    &data->pci_dev.bus, def,
                                    _("no PCI bus ID supplied for '%s'"),
                                    _("invalid PCI bus ID supplied for '%s'")) < 0)
        goto out;

1054
    if (virNodeDevCapsDefParseULong("number(./slot[1])", ctxt,
1055 1056 1057 1058 1059
                                    &data->pci_dev.slot, def,
                                    _("no PCI slot ID supplied for '%s'"),
                                    _("invalid PCI slot ID supplied for '%s'")) < 0)
        goto out;

1060
    if (virNodeDevCapsDefParseULong("number(./function[1])", ctxt,
1061 1062 1063 1064 1065
                                    &data->pci_dev.function, def,
                                    _("no PCI function ID supplied for '%s'"),
                                    _("invalid PCI function ID supplied for '%s'")) < 0)
        goto out;

1066
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1067 1068 1069 1070 1071
                                    &data->pci_dev.vendor, def,
                                    _("no PCI vendor ID supplied for '%s'"),
                                    _("invalid PCI vendor ID supplied for '%s'")) < 0)
        goto out;

1072
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1073 1074 1075 1076 1077
                                    &data->pci_dev.product, def,
                                    _("no PCI product ID supplied for '%s'"),
                                    _("invalid PCI product ID supplied for '%s'")) < 0)
        goto out;

1078 1079
    data->pci_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->pci_dev.product_name = virXPathString("string(./product[1])", ctxt);
1080

1081 1082 1083 1084 1085 1086
    if ((iommuGroupNode = virXPathNode("./iommuGroup[1]", ctxt))) {
        if (virNodeDevCapPciDevIommuGroupParseXML(ctxt, iommuGroupNode,
                                                  data) < 0) {
            goto out;
        }
    }
1087 1088 1089 1090 1091 1092 1093
    ret = 0;
out:
    ctxt->node = orignode;
    return ret;
}

static int
1094
virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt,
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

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

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

1108 1109 1110
    data->system.hardware.vendor_name = virXPathString("string(./hardware/vendor[1])", ctxt);
    data->system.hardware.version     = virXPathString("string(./hardware/version[1])", ctxt);
    data->system.hardware.serial      = virXPathString("string(./hardware/serial[1])", ctxt);
1111

1112
    tmp = virXPathString("string(./hardware/uuid[1])", ctxt);
1113
    if (!tmp) {
1114 1115
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no system UUID supplied for '%s'"), def->name);
1116 1117 1118 1119
        goto out;
    }

    if (virUUIDParse(tmp, data->system.hardware.uuid) < 0) {
1120 1121
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("malformed uuid element for '%s'"), def->name);
1122 1123 1124 1125 1126
        VIR_FREE(tmp);
        goto out;
    }
    VIR_FREE(tmp);

1127 1128 1129
    data->system.firmware.vendor_name  = virXPathString("string(./firmware/vendor[1])", ctxt);
    data->system.firmware.version      = virXPathString("string(./firmware/version[1])", ctxt);
    data->system.firmware.release_date = virXPathString("string(./firmware/release_date[1])", ctxt);
1130 1131 1132 1133 1134 1135 1136 1137

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

static virNodeDevCapsDefPtr
1138
virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt,
1139
                          virNodeDeviceDefPtr def,
1140
                          xmlNodePtr node,
1141 1142
                          int create,
                          const char *virt_type)
1143 1144 1145 1146 1147
{
    virNodeDevCapsDefPtr caps;
    char *tmp;
    int val, ret;

1148
    if (VIR_ALLOC(caps) < 0)
1149 1150 1151 1152
        return NULL;

    tmp = virXMLPropString(node, "type");
    if (!tmp) {
1153 1154
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing capability type"));
1155 1156 1157 1158
        goto error;
    }

    if ((val = virNodeDevCapTypeFromString(tmp)) < 0) {
1159
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1160
                       _("unknown capability type '%s'"), tmp);
1161 1162 1163 1164 1165 1166 1167 1168
        VIR_FREE(tmp);
        goto error;
    }
    caps->type = val;
    VIR_FREE(tmp);

    switch (caps->type) {
    case VIR_NODE_DEV_CAP_SYSTEM:
1169
        ret = virNodeDevCapSystemParseXML(ctxt, def, node, &caps->data);
1170 1171
        break;
    case VIR_NODE_DEV_CAP_PCI_DEV:
1172
        ret = virNodeDevCapPciDevParseXML(ctxt, def, node, &caps->data);
1173 1174
        break;
    case VIR_NODE_DEV_CAP_USB_DEV:
1175
        ret = virNodeDevCapUsbDevParseXML(ctxt, def, node, &caps->data);
1176 1177
        break;
    case VIR_NODE_DEV_CAP_USB_INTERFACE:
1178
        ret = virNodeDevCapUsbInterfaceParseXML(ctxt, def, node, &caps->data);
1179 1180
        break;
    case VIR_NODE_DEV_CAP_NET:
1181
        ret = virNodeDevCapNetParseXML(ctxt, def, node, &caps->data);
1182 1183
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1184 1185 1186 1187
        ret = virNodeDevCapScsiHostParseXML(ctxt, def, node,
                                            &caps->data,
                                            create,
                                            virt_type);
1188
        break;
D
David Allan 已提交
1189
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
1190
        ret = virNodeDevCapScsiTargetParseXML(ctxt, def, node, &caps->data);
D
David Allan 已提交
1191
        break;
1192
    case VIR_NODE_DEV_CAP_SCSI:
1193
        ret = virNodeDevCapScsiParseXML(ctxt, def, node, &caps->data);
1194 1195
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1196
        ret = virNodeDevCapStorageParseXML(ctxt, def, node, &caps->data);
1197 1198
        break;
    default:
1199 1200 1201
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown capability type '%d' for '%s'"),
                       caps->type, def->name);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
        ret = -1;
        break;
    }

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

error:
    virNodeDevCapsDefFree(caps);
    return NULL;
}

static virNodeDeviceDefPtr
1216 1217 1218
virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt,
                         int create,
                         const char *virt_type)
1219 1220 1221 1222
{
    virNodeDeviceDefPtr def;
    virNodeDevCapsDefPtr *next_cap;
    xmlNodePtr *nodes;
1223 1224
    int n;
    size_t i;
1225

1226
    if (VIR_ALLOC(def) < 0)
1227 1228 1229
        return NULL;

    /* Extract device name */
1230
    if (create == EXISTING_DEVICE) {
1231
        def->name = virXPathString("string(./name[1])", ctxt);
1232 1233

        if (!def->name) {
1234
            virReportError(VIR_ERR_NO_NAME, NULL);
1235 1236
            goto error;
        }
1237
    } else {
1238
        if (VIR_STRDUP(def->name, "new device") < 0)
1239
            goto error;
1240 1241 1242
    }

    /* Extract device parent, if any */
1243
    def->parent = virXPathString("string(./parent[1])", ctxt);
1244 1245 1246

    /* Parse device capabilities */
    nodes = NULL;
1247 1248 1249 1250 1251
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
        goto error;
    }

    if (n == 0) {
1252 1253 1254
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no device capabilities for '%s'"),
                       def->name);
1255 1256 1257 1258
        goto error;
    }

    next_cap = &def->caps;
1259
    for (i = 0; i < n; i++) {
1260 1261 1262 1263
        *next_cap = virNodeDevCapsDefParseXML(ctxt, def,
                                              nodes[i],
                                              create,
                                              virt_type);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        if (!*next_cap) {
            VIR_FREE(nodes);
            goto error;
        }

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

    return def;

 error:
    virNodeDeviceDefFree(def);
    return NULL;
}

1280
virNodeDeviceDefPtr
1281
virNodeDeviceDefParseNode(xmlDocPtr xml,
1282
                          xmlNodePtr root,
1283 1284
                          int create,
                          const char *virt_type)
1285 1286 1287 1288 1289
{
    xmlXPathContextPtr ctxt = NULL;
    virNodeDeviceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "device")) {
1290 1291 1292 1293
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s> "
                         "expecting <device>"),
                       root->name);
1294 1295 1296 1297 1298
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1299
        virReportOOMError();
1300 1301 1302 1303
        goto cleanup;
    }

    ctxt->node = root;
1304
    def = virNodeDeviceDefParseXML(ctxt, create, virt_type);
1305 1306 1307 1308 1309 1310

cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

1311
static virNodeDeviceDefPtr
1312
virNodeDeviceDefParse(const char *str,
1313
                      const char *filename,
1314 1315
                      int create,
                      const char *virt_type)
1316
{
J
Jiri Denemark 已提交
1317
    xmlDocPtr xml;
1318 1319
    virNodeDeviceDefPtr def = NULL;

1320
    if ((xml = virXMLParse(filename, str, _("(node_device_definition)")))) {
1321 1322
        def = virNodeDeviceDefParseNode(xml, xmlDocGetRootElement(xml),
                                        create, virt_type);
J
Jiri Denemark 已提交
1323
        xmlFreeDoc(xml);
1324 1325
    }

1326 1327 1328
    return def;
}

1329
virNodeDeviceDefPtr
1330
virNodeDeviceDefParseString(const char *str,
1331 1332
                            int create,
                            const char *virt_type)
1333
{
1334
    return virNodeDeviceDefParse(str, NULL, create, virt_type);
1335 1336 1337
}

virNodeDeviceDefPtr
1338
virNodeDeviceDefParseFile(const char *filename,
1339 1340
                          int create,
                          const char *virt_type)
1341
{
1342
    return virNodeDeviceDefParse(NULL, filename, create, virt_type);
1343 1344
}

1345 1346 1347 1348
/*
 * Return fc_host dev's WWNN and WWPN
 */
int
1349
virNodeDeviceGetWWNs(virNodeDeviceDefPtr def,
1350 1351 1352 1353
                     char **wwnn,
                     char **wwpn)
{
    virNodeDevCapsDefPtr cap = NULL;
1354
    int ret = -1;
1355 1356 1357 1358 1359

    cap = def->caps;
    while (cap != NULL) {
        if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST &&
            cap->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
1360 1361 1362 1363 1364 1365
            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;
            }
1366 1367 1368 1369 1370 1371 1372
            break;
        }

        cap = cap->next;
    }

    if (cap == NULL) {
1373 1374
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Device is not a fibre channel HBA"));
1375
        goto cleanup;
1376 1377
    }

1378 1379
    ret = 0;
cleanup:
1380 1381 1382 1383 1384 1385 1386
    return ret;
}

/*
 * Return the NPIV dev's parent device name
 */
int
E
Eric Blake 已提交
1387
virNodeDeviceGetParentHost(virNodeDeviceObjListPtr devs,
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
                           const char *dev_name,
                           const char *parent_name,
                           int *parent_host)
{
    virNodeDeviceObjPtr parent = NULL;
    virNodeDevCapsDefPtr cap = NULL;
    int ret = 0;

    parent = virNodeDeviceFindByName(devs, parent_name);
    if (parent == NULL) {
1398 1399 1400
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find parent device for '%s'"),
                       dev_name);
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        ret = -1;
        goto out;
    }

    cap = parent->def->caps;
    while (cap != NULL) {
        if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST &&
            (cap->data.scsi_host.flags &
             VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS)) {
                *parent_host = cap->data.scsi_host.host;
                break;
        }

        cap = cap->next;
    }

    if (cap == NULL) {
1418 1419 1420 1421
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parent device %s is not capable "
                         "of vport operations"),
                       parent->def->name);
1422 1423 1424 1425 1426 1427 1428 1429
        ret = -1;
    }

    virNodeDeviceObjUnlock(parent);

out:
    return ret;
}
1430

1431 1432
void virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
{
1433
    size_t i = 0;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    union _virNodeDevCapData *data = &caps->data;

    switch (caps->type) {
    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);
1449
        VIR_FREE(data->pci_dev.physical_function);
1450
        for (i = 0; i < data->pci_dev.num_virtual_functions; i++) {
1451 1452
            VIR_FREE(data->pci_dev.virtual_functions[i]);
        }
1453
        VIR_FREE(data->pci_dev.virtual_functions);
1454 1455 1456
        for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++) {
            VIR_FREE(data->pci_dev.iommuGroupDevices[i]);
        }
1457
        VIR_FREE(data->pci_dev.iommuGroupDevices);
1458 1459 1460 1461 1462 1463 1464 1465 1466
        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:
1467
        VIR_FREE(data->net.ifname);
1468 1469 1470
        VIR_FREE(data->net.address);
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1471 1472
        VIR_FREE(data->scsi_host.wwnn);
        VIR_FREE(data->scsi_host.wwpn);
O
Osier Yang 已提交
1473
        VIR_FREE(data->scsi_host.fabric_wwn);
1474
        break;
D
David Allan 已提交
1475 1476 1477
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
        VIR_FREE(data->scsi_target.name);
        break;
1478 1479 1480 1481
    case VIR_NODE_DEV_CAP_SCSI:
        VIR_FREE(data->scsi.type);
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1482
        VIR_FREE(data->storage.block);
1483 1484 1485 1486
        VIR_FREE(data->storage.bus);
        VIR_FREE(data->storage.drive_type);
        VIR_FREE(data->storage.model);
        VIR_FREE(data->storage.vendor);
1487
        VIR_FREE(data->storage.serial);
1488
        VIR_FREE(data->storage.media_label);
1489
        break;
1490 1491 1492
    case VIR_NODE_DEV_CAP_SCSI_GENERIC:
        VIR_FREE(data->sg.path);
        break;
1493 1494
    case VIR_NODE_DEV_CAP_FC_HOST:
    case VIR_NODE_DEV_CAP_VPORTS:
1495
    case VIR_NODE_DEV_CAP_LAST:
1496
    default:
1497 1498 1499 1500 1501 1502 1503
        /* This case is here to shutup the compiler */
        break;
    }

    VIR_FREE(caps);
}

D
Daniel P. Berrange 已提交
1504

1505 1506
void virNodeDeviceObjLock(virNodeDeviceObjPtr obj)
{
1507
    virMutexLock(&obj->lock);
1508 1509 1510 1511
}

void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj)
{
1512
    virMutexUnlock(&obj->lock);
D
Daniel P. Berrange 已提交
1513
}
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

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

    for (cap = devobj->def->caps; cap; cap = cap->next) {
        if (type == cap->type)
            return true;
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

        if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST) {
            if (type == VIR_CONNECT_LIST_NODE_DEVICES_CAP_FC_HOST &&
                (cap->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST))
                return true;

            if (type == VIR_CONNECT_LIST_NODE_DEVICES_CAP_VPORTS &&
                (cap->data.scsi_host.flags &
                 VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS))
                return true;
        }
1536 1537 1538 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
    }

    return false;
}

#define MATCH(FLAG) (flags & (FLAG))
static bool
virNodeDeviceMatch(virNodeDeviceObjPtr devobj,
                   unsigned int flags)
{
    /* filter by cap type */
    if (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP)) {
        if (!((MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_SYSTEM) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_SYSTEM))        ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_PCI_DEV) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_PCI_DEV))       ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_DEV) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_USB_DEV))       ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_INTERFACE) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_USB_INTERFACE)) ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_NET) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_NET))           ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_HOST) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_SCSI_HOST))     ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_TARGET) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_SCSI_TARGET))   ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_SCSI))          ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_STORAGE) &&
1565 1566 1567 1568
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_STORAGE))       ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_FC_HOST) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_FC_HOST))       ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_VPORTS) &&
1569 1570 1571
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_VPORTS))        ||
              (MATCH(VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_GENERIC) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_SCSI_GENERIC))))
1572 1573 1574 1575 1576 1577 1578 1579
            return false;
    }

    return true;
}
#undef MATCH

int
1580 1581 1582 1583 1584
virNodeDeviceObjListExport(virConnectPtr conn,
                           virNodeDeviceObjList devobjs,
                           virNodeDevicePtr **devices,
                           virNodeDeviceObjListFilter filter,
                           unsigned int flags)
1585 1586 1587 1588 1589
{
    virNodeDevicePtr *tmp_devices = NULL;
    virNodeDevicePtr device = NULL;
    int ndevices = 0;
    int ret = -1;
1590
    size_t i;
1591

1592 1593
    if (devices && VIR_ALLOC_N(tmp_devices, devobjs.count + 1) < 0)
        goto cleanup;
1594 1595 1596 1597

    for (i = 0; i < devobjs.count; i++) {
        virNodeDeviceObjPtr devobj = devobjs.objs[i];
        virNodeDeviceObjLock(devobj);
1598 1599
        if ((!filter || filter(conn, devobj->def)) &&
            virNodeDeviceMatch(devobj, flags)) {
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
            if (devices) {
                if (!(device = virGetNodeDevice(conn,
                                                devobj->def->name))) {
                    virNodeDeviceObjUnlock(devobj);
                    goto cleanup;
                }
                tmp_devices[ndevices] = device;
            }
            ndevices++;
        }
        virNodeDeviceObjUnlock(devobj);
    }

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

    ret = ndevices;

cleanup:
    if (tmp_devices) {
        for (i = 0; i < ndevices; i++) {
            if (tmp_devices[i])
                virNodeDeviceFree(tmp_devices[i]);
        }
    }

    VIR_FREE(tmp_devices);
    return ret;
}