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

#include <config.h>

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

29
#include "virerror.h"
30
#include "datatypes.h"
31
#include "viralloc.h"
32 33

#include "node_device_conf.h"
34
#include "viralloc.h"
35
#include "virxml.h"
36
#include "virutil.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 54
              "storage",
              "fc_host",
              "vports")
55 56 57

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

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

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

    *string = s;
    return 0;
}

74 75 76 77 78 79 80 81 82 83
int virNodeDeviceHasCap(const virNodeDeviceObjPtr dev, const char *cap)
{
    virNodeDevCapsDefPtr caps = dev->def->caps;
    while (caps) {
        if (STREQ(cap, virNodeDevCapTypeToString(caps->type)))
            return 1;
        caps = caps->next;
    }
    return 0;
}
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

virNodeDeviceObjPtr
virNodeDeviceFindBySysfsPath(const virNodeDeviceObjListPtr devs,
                             const char *sysfs_path)
{
    unsigned int i;

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


105 106 107 108 109
virNodeDeviceObjPtr virNodeDeviceFindByName(const virNodeDeviceObjListPtr devs,
                                            const char *name)
{
    unsigned int i;

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

    return NULL;
}


void virNodeDeviceDefFree(virNodeDeviceDefPtr def)
{
    virNodeDevCapsDefPtr caps;

    if (!def)
        return;

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

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

153 154
    virMutexDestroy(&dev->lock);

155 156 157 158 159 160 161 162 163 164 165 166
    VIR_FREE(dev);
}

void virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs)
{
    unsigned int i;
    for (i = 0 ; i < devs->count ; i++)
        virNodeDeviceObjFree(devs->objs[i]);
    VIR_FREE(devs->objs);
    devs->count = 0;
}

167
virNodeDeviceObjPtr virNodeDeviceAssignDef(virNodeDeviceObjListPtr devs,
168 169 170 171 172 173 174 175 176 177 178
                                           const virNodeDeviceDefPtr def)
{
    virNodeDeviceObjPtr device;

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

    if (VIR_ALLOC(device) < 0) {
179
        virReportOOMError();
180 181 182
        return NULL;
    }

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

    if (VIR_REALLOC_N(devs->objs, devs->count+1) < 0) {
        device->def = NULL;
194
        virNodeDeviceObjUnlock(device);
195
        virNodeDeviceObjFree(device);
196
        virReportOOMError();
197 198 199 200 201 202 203 204 205 206 207 208 209
        return NULL;
    }
    devs->objs[devs->count++] = device;

    return device;

}

void virNodeDeviceObjRemove(virNodeDeviceObjListPtr devs,
                            const virNodeDeviceObjPtr dev)
{
    unsigned int i;

210 211
    virNodeDeviceObjUnlock(dev);

212
    for (i = 0; i < devs->count; i++) {
213
        virNodeDeviceObjLock(dev);
214
        if (devs->objs[i] == dev) {
215
            virNodeDeviceObjUnlock(dev);
216 217 218 219 220 221 222 223 224 225 226 227 228
            virNodeDeviceObjFree(devs->objs[i]);

            if (i < (devs->count - 1))
                memmove(devs->objs + i, devs->objs + i + 1,
                        sizeof(*(devs->objs)) * (devs->count - (i + 1)));

            if (VIR_REALLOC_N(devs->objs, devs->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            devs->count--;

            break;
        }
229
        virNodeDeviceObjUnlock(dev);
230 231 232
    }
}

233
char *virNodeDeviceDefFormat(const virNodeDeviceDefPtr def)
234 235
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
236
    virNodeDevCapsDefPtr caps;
237
    unsigned int i = 0;
238 239 240

    virBufferAddLit(&buf, "<device>\n");
    virBufferEscapeString(&buf, "  <name>%s</name>\n", def->name);
241
    if (def->parent) {
242
        virBufferEscapeString(&buf, "  <parent>%s</parent>\n", def->parent);
243
    }
244 245 246 247 248
    if (def->driver) {
        virBufferAddLit(&buf, "  <driver>\n");
        virBufferEscapeString(&buf, "    <name>%s</name>\n", def->driver);
        virBufferAddLit(&buf, "  </driver>\n");
    }
249 250 251 252 253

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

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

402
            break;
D
David Allan 已提交
403 404

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

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

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

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

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

    if (virBufferError(&buf))
        goto no_memory;

    return virBufferContentAndReset(&buf);

 no_memory:
494
    virReportOOMError();
495
    virBufferFreeAndReset(&buf);
496 497 498
    return NULL;
}

499
static int
500
virNodeDevCapsDefParseULong(const char *xpath,
501 502 503 504 505 506 507 508 509
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

510
    ret = virXPathULong(xpath, ctxt, &val);
511
    if (ret < 0) {
512 513 514
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
515 516 517 518 519 520 521 522
        return -1;
    }

    *value = val;
    return 0;
}

static int
523
virNodeDevCapsDefParseULongLong(const char *xpath,
524 525 526 527 528 529 530 531 532
                                xmlXPathContextPtr ctxt,
                                unsigned long long *value,
                                virNodeDeviceDefPtr def,
                                const char *missing_error_fmt,
                                const char *invalid_error_fmt)
{
    int ret;
    unsigned long long val;

533
    ret = virXPathULongLong(xpath, ctxt, &val);
534
    if (ret < 0) {
535 536 537
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
538 539 540 541 542 543 544 545
        return -1;
    }

    *value = val;
    return 0;
}

static int
546
virNodeDevCapStorageParseXML(xmlXPathContextPtr ctxt,
547 548 549 550 551 552 553 554 555 556 557
                             virNodeDeviceDefPtr def,
                             xmlNodePtr node,
                             union _virNodeDevCapData *data)
{
    xmlNodePtr orignode, *nodes = NULL;
    int i, n, ret = -1;
    unsigned long long val;

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

558
    data->storage.block = virXPathString("string(./block[1])", ctxt);
559
    if (!data->storage.block) {
560 561 562
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no block device path supplied for '%s'"),
                       def->name);
563 564 565
        goto out;
    }

566 567 568 569 570
    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);
571

572
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
573 574 575 576 577 578 579
        goto out;
    }

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

        if (!type) {
580 581 582
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing storage capability type for '%s'"),
                           def->name);
583 584 585 586 587 588 589 590 591 592 593 594 595
            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];

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

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

601
            val = 0;
602
            if (virNodeDevCapsDefParseULongLong("number(./media_size[1])", ctxt, &val, def,
603 604 605 606 607 608 609 610 611 612
                                                _("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 {
613 614 615
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown storage capability type '%s' for '%s'"),
                           type, def->name);
616 617 618 619 620 621 622 623 624
            VIR_FREE(type);
            goto out;
        }

        VIR_FREE(type);
    }

    if (!(data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE)) {
        val = 0;
625
        if (virNodeDevCapsDefParseULongLong("number(./size[1])", ctxt, &val, def,
626 627 628 629 630 631 632 633 634 635 636 637 638 639
                                            _("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
640
virNodeDevCapScsiParseXML(xmlXPathContextPtr ctxt,
641 642 643 644 645 646 647 648 649 650
                          virNodeDeviceDefPtr def,
                          xmlNodePtr node,
                          union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

651
    if (virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
652 653 654 655 656
                                    &data->scsi.host, def,
                                    _("no SCSI host ID supplied for '%s'"),
                                    _("invalid SCSI host ID supplied for '%s'")) < 0)
        goto out;

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

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

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

675
    data->scsi.type = virXPathString("string(./type[1])", ctxt);
676 677 678 679 680 681 682

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

D
David Allan 已提交
683 684

static int
685
virNodeDevCapScsiTargetParseXML(xmlXPathContextPtr ctxt,
D
David Allan 已提交
686 687 688 689 690 691 692 693 694 695
                                virNodeDeviceDefPtr def,
                                xmlNodePtr node,
                                union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

696
    data->scsi_target.name = virXPathString("string(./name[1])", ctxt);
D
David Allan 已提交
697
    if (!data->scsi_target.name) {
698 699 700
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no target name supplied for '%s'"),
                       def->name);
D
David Allan 已提交
701 702 703 704 705 706 707 708 709 710 711
        goto out;
    }

    ret = 0;

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


712
static int
713
virNodeDevCapScsiHostParseXML(xmlXPathContextPtr ctxt,
714 715
                              virNodeDeviceDefPtr def,
                              xmlNodePtr node,
716
                              union _virNodeDevCapData *data,
717 718
                              int create,
                              const char *virt_type)
719
{
720 721 722
    xmlNodePtr orignode, *nodes = NULL;
    int ret = -1, n = 0, i;
    char *type = NULL;
723 724 725 726

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

727
    if (create == EXISTING_DEVICE &&
728
        virNodeDevCapsDefParseULong("number(./host[1])", ctxt,
729 730
                                    &data->scsi_host.host, def,
                                    _("no SCSI host ID supplied for '%s'"),
731 732 733 734
                                    _("invalid SCSI host ID supplied for '%s'")) < 0) {
        goto out;
    }

735
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
736
        goto out;
737 738 739 740 741 742
    }

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

        if (!type) {
743 744 745
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing SCSI host capability type for '%s'"),
                           def->name);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
            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];

762
            if (virNodeDevCapsDefParseString("string(./wwnn[1])",
763
                                             ctxt,
764 765
                                             &data->scsi_host.wwnn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwnn, virt_type) < 0) {
766 767 768 769
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWNN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
770 771
                    goto out;
                }
772 773
            }

774
            if (virNodeDevCapsDefParseString("string(./wwpn[1])",
775
                                             ctxt,
776 777
                                             &data->scsi_host.wwpn) < 0) {
                if (virRandomGenerateWWN(&data->scsi_host.wwpn, virt_type) < 0) {
778 779 780 781
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("no WWPN supplied for '%s', and "
                                     "auto-generation failed"),
                                   def->name);
782 783
                    goto out;
                }
784 785 786 787 788
            }

            ctxt->node = orignode2;

        } else {
789 790 791
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown SCSI host capability type '%s' for '%s'"),
                           type, def->name);
792 793 794 795 796
            goto out;
        }

        VIR_FREE(type);
    }
797 798

    ret = 0;
799

800
out:
801
    VIR_FREE(type);
802
    ctxt->node = orignode;
803
    VIR_FREE(nodes);
804 805 806
    return ret;
}

807

808
static int
809
virNodeDevCapNetParseXML(xmlXPathContextPtr ctxt,
810 811 812 813 814 815 816 817 818 819 820
                         virNodeDeviceDefPtr def,
                         xmlNodePtr node,
                         union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

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

821
    data->net.ifname = virXPathString("string(./interface[1])", ctxt);
822
    if (!data->net.ifname) {
823 824 825
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no network interface supplied for '%s'"),
                       def->name);
826 827 828
        goto out;
    }

829
    data->net.address = virXPathString("string(./address[1])", ctxt);
830 831 832

    data->net.subtype = VIR_NODE_DEV_CAP_NET_LAST;

833
    tmp = virXPathString("string(./capability/@type)", ctxt);
834 835 836 837
    if (tmp) {
        int val = virNodeDevNetCapTypeFromString(tmp);
        VIR_FREE(tmp);
        if (val < 0) {
838 839 840
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("invalid network type supplied for '%s'"),
                           def->name);
841 842 843 844 845 846 847 848 849 850 851 852
            goto out;
        }
        data->net.subtype = val;
    }

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

static int
853
virNodeDevCapUsbInterfaceParseXML(xmlXPathContextPtr ctxt,
854 855 856 857 858 859 860 861 862 863
                                  virNodeDeviceDefPtr def,
                                  xmlNodePtr node,
                                  union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

864
    if (virNodeDevCapsDefParseULong("number(./number[1])", ctxt,
865 866 867 868 869
                                    &data->usb_if.number, def,
                                    _("no USB interface number supplied for '%s'"),
                                    _("invalid USB interface number supplied for '%s'")) < 0)
        goto out;

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

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

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

888
    data->usb_if.description = virXPathString("string(./description[1])", ctxt);
889 890 891 892 893 894 895 896

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

static int
897
virNodeDevCapsDefParseHexId(const char *xpath,
898 899 900 901 902 903 904 905 906
                            xmlXPathContextPtr ctxt,
                            unsigned *value,
                            virNodeDeviceDefPtr def,
                            const char *missing_error_fmt,
                            const char *invalid_error_fmt)
{
    int ret;
    unsigned long val;

907
    ret = virXPathULongHex(xpath, ctxt, &val);
908
    if (ret < 0) {
909 910 911
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       ret == -1 ? missing_error_fmt : invalid_error_fmt,
                       def->name);
912 913 914 915 916 917 918 919
        return -1;
    }

    *value = val;
    return 0;
}

static int
920
virNodeDevCapUsbDevParseXML(xmlXPathContextPtr ctxt,
921 922 923 924 925 926 927 928 929 930
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

931
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
932 933 934 935 936
                                    &data->usb_dev.bus, def,
                                    _("no USB bus number supplied for '%s'"),
                                    _("invalid USB bus number supplied for '%s'")) < 0)
        goto out;

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

943
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
944 945 946 947 948
                                    &data->usb_dev.vendor, def,
                                    _("no USB vendor ID supplied for '%s'"),
                                    _("invalid USB vendor ID supplied for '%s'")) < 0)
        goto out;

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

955 956
    data->usb_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->usb_dev.product_name = virXPathString("string(./product[1])", ctxt);
957 958 959 960 961 962 963 964

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

static int
965
virNodeDevCapPciDevParseXML(xmlXPathContextPtr ctxt,
966 967 968 969 970 971 972 973 974 975
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;

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

976
    if (virNodeDevCapsDefParseULong("number(./domain[1])", ctxt,
977 978 979 980 981
                                    &data->pci_dev.domain, def,
                                    _("no PCI domain ID supplied for '%s'"),
                                    _("invalid PCI domain ID supplied for '%s'")) < 0)
        goto out;

982
    if (virNodeDevCapsDefParseULong("number(./bus[1])", ctxt,
983 984 985 986 987
                                    &data->pci_dev.bus, def,
                                    _("no PCI bus ID supplied for '%s'"),
                                    _("invalid PCI bus ID supplied for '%s'")) < 0)
        goto out;

988
    if (virNodeDevCapsDefParseULong("number(./slot[1])", ctxt,
989 990 991 992 993
                                    &data->pci_dev.slot, def,
                                    _("no PCI slot ID supplied for '%s'"),
                                    _("invalid PCI slot ID supplied for '%s'")) < 0)
        goto out;

994
    if (virNodeDevCapsDefParseULong("number(./function[1])", ctxt,
995 996 997 998 999
                                    &data->pci_dev.function, def,
                                    _("no PCI function ID supplied for '%s'"),
                                    _("invalid PCI function ID supplied for '%s'")) < 0)
        goto out;

1000
    if (virNodeDevCapsDefParseHexId("string(./vendor[1]/@id)", ctxt,
1001 1002 1003 1004 1005
                                    &data->pci_dev.vendor, def,
                                    _("no PCI vendor ID supplied for '%s'"),
                                    _("invalid PCI vendor ID supplied for '%s'")) < 0)
        goto out;

1006
    if (virNodeDevCapsDefParseHexId("string(./product[1]/@id)", ctxt,
1007 1008 1009 1010 1011
                                    &data->pci_dev.product, def,
                                    _("no PCI product ID supplied for '%s'"),
                                    _("invalid PCI product ID supplied for '%s'")) < 0)
        goto out;

1012 1013
    data->pci_dev.vendor_name  = virXPathString("string(./vendor[1])", ctxt);
    data->pci_dev.product_name = virXPathString("string(./product[1])", ctxt);
1014 1015 1016 1017 1018 1019 1020 1021

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

static int
1022
virNodeDevCapSystemParseXML(xmlXPathContextPtr ctxt,
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
                            virNodeDeviceDefPtr def,
                            xmlNodePtr node,
                            union _virNodeDevCapData *data)
{
    xmlNodePtr orignode;
    int ret = -1;
    char *tmp;

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

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

1036 1037 1038
    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);
1039

1040
    tmp = virXPathString("string(./hardware/uuid[1])", ctxt);
1041
    if (!tmp) {
1042 1043
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no system UUID supplied for '%s'"), def->name);
1044 1045 1046 1047
        goto out;
    }

    if (virUUIDParse(tmp, data->system.hardware.uuid) < 0) {
1048 1049
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("malformed uuid element for '%s'"), def->name);
1050 1051 1052 1053 1054
        VIR_FREE(tmp);
        goto out;
    }
    VIR_FREE(tmp);

1055 1056 1057
    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);
1058 1059 1060 1061 1062 1063 1064 1065

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

static virNodeDevCapsDefPtr
1066
virNodeDevCapsDefParseXML(xmlXPathContextPtr ctxt,
1067
                          virNodeDeviceDefPtr def,
1068
                          xmlNodePtr node,
1069 1070
                          int create,
                          const char *virt_type)
1071 1072 1073 1074 1075 1076
{
    virNodeDevCapsDefPtr caps;
    char *tmp;
    int val, ret;

    if (VIR_ALLOC(caps) < 0) {
1077
        virReportOOMError();
1078 1079 1080 1081 1082
        return NULL;
    }

    tmp = virXMLPropString(node, "type");
    if (!tmp) {
1083 1084
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("missing capability type"));
1085 1086 1087 1088
        goto error;
    }

    if ((val = virNodeDevCapTypeFromString(tmp)) < 0) {
1089 1090
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown capability type '%s'"), tmp);
1091 1092 1093 1094 1095 1096 1097 1098
        VIR_FREE(tmp);
        goto error;
    }
    caps->type = val;
    VIR_FREE(tmp);

    switch (caps->type) {
    case VIR_NODE_DEV_CAP_SYSTEM:
1099
        ret = virNodeDevCapSystemParseXML(ctxt, def, node, &caps->data);
1100 1101
        break;
    case VIR_NODE_DEV_CAP_PCI_DEV:
1102
        ret = virNodeDevCapPciDevParseXML(ctxt, def, node, &caps->data);
1103 1104
        break;
    case VIR_NODE_DEV_CAP_USB_DEV:
1105
        ret = virNodeDevCapUsbDevParseXML(ctxt, def, node, &caps->data);
1106 1107
        break;
    case VIR_NODE_DEV_CAP_USB_INTERFACE:
1108
        ret = virNodeDevCapUsbInterfaceParseXML(ctxt, def, node, &caps->data);
1109 1110
        break;
    case VIR_NODE_DEV_CAP_NET:
1111
        ret = virNodeDevCapNetParseXML(ctxt, def, node, &caps->data);
1112 1113
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1114 1115 1116 1117
        ret = virNodeDevCapScsiHostParseXML(ctxt, def, node,
                                            &caps->data,
                                            create,
                                            virt_type);
1118
        break;
D
David Allan 已提交
1119
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
1120
        ret = virNodeDevCapScsiTargetParseXML(ctxt, def, node, &caps->data);
D
David Allan 已提交
1121
        break;
1122
    case VIR_NODE_DEV_CAP_SCSI:
1123
        ret = virNodeDevCapScsiParseXML(ctxt, def, node, &caps->data);
1124 1125
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1126
        ret = virNodeDevCapStorageParseXML(ctxt, def, node, &caps->data);
1127 1128
        break;
    default:
1129 1130 1131
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown capability type '%d' for '%s'"),
                       caps->type, def->name);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
        ret = -1;
        break;
    }

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

error:
    virNodeDevCapsDefFree(caps);
    return NULL;
}

static virNodeDeviceDefPtr
1146 1147 1148
virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt,
                         int create,
                         const char *virt_type)
1149 1150 1151 1152 1153 1154 1155
{
    virNodeDeviceDefPtr def;
    virNodeDevCapsDefPtr *next_cap;
    xmlNodePtr *nodes;
    int n, i;

    if (VIR_ALLOC(def) < 0) {
1156
        virReportOOMError();
1157 1158 1159 1160
        return NULL;
    }

    /* Extract device name */
1161
    if (create == EXISTING_DEVICE) {
1162
        def->name = virXPathString("string(./name[1])", ctxt);
1163 1164

        if (!def->name) {
1165
            virReportError(VIR_ERR_NO_NAME, NULL);
1166 1167
            goto error;
        }
1168 1169 1170
    } else {
        def->name = strdup("new device");

1171
        if (!def->name) {
1172
            virReportOOMError();
1173 1174
            goto error;
        }
1175 1176 1177
    }

    /* Extract device parent, if any */
1178
    def->parent = virXPathString("string(./parent[1])", ctxt);
1179 1180 1181

    /* Parse device capabilities */
    nodes = NULL;
1182 1183 1184 1185 1186
    if ((n = virXPathNodeSet("./capability", ctxt, &nodes)) < 0) {
        goto error;
    }

    if (n == 0) {
1187 1188 1189
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no device capabilities for '%s'"),
                       def->name);
1190 1191 1192 1193 1194
        goto error;
    }

    next_cap = &def->caps;
    for (i = 0 ; i < n ; i++) {
1195 1196 1197 1198
        *next_cap = virNodeDevCapsDefParseXML(ctxt, def,
                                              nodes[i],
                                              create,
                                              virt_type);
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        if (!*next_cap) {
            VIR_FREE(nodes);
            goto error;
        }

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

    return def;

 error:
    virNodeDeviceDefFree(def);
    return NULL;
}

1215
virNodeDeviceDefPtr
1216
virNodeDeviceDefParseNode(xmlDocPtr xml,
1217
                          xmlNodePtr root,
1218 1219
                          int create,
                          const char *virt_type)
1220 1221 1222 1223 1224
{
    xmlXPathContextPtr ctxt = NULL;
    virNodeDeviceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "device")) {
1225 1226 1227 1228
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s> "
                         "expecting <device>"),
                       root->name);
1229 1230 1231 1232 1233
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
1234
        virReportOOMError();
1235 1236 1237 1238
        goto cleanup;
    }

    ctxt->node = root;
1239
    def = virNodeDeviceDefParseXML(ctxt, create, virt_type);
1240 1241 1242 1243 1244 1245

cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

1246
static virNodeDeviceDefPtr
1247
virNodeDeviceDefParse(const char *str,
1248
                      const char *filename,
1249 1250
                      int create,
                      const char *virt_type)
1251
{
J
Jiri Denemark 已提交
1252
    xmlDocPtr xml;
1253 1254
    virNodeDeviceDefPtr def = NULL;

1255
    if ((xml = virXMLParse(filename, str, _("(node_device_definition)")))) {
1256 1257
        def = virNodeDeviceDefParseNode(xml, xmlDocGetRootElement(xml),
                                        create, virt_type);
J
Jiri Denemark 已提交
1258
        xmlFreeDoc(xml);
1259 1260
    }

1261 1262 1263
    return def;
}

1264
virNodeDeviceDefPtr
1265
virNodeDeviceDefParseString(const char *str,
1266 1267
                            int create,
                            const char *virt_type)
1268
{
1269
    return virNodeDeviceDefParse(str, NULL, create, virt_type);
1270 1271 1272
}

virNodeDeviceDefPtr
1273
virNodeDeviceDefParseFile(const char *filename,
1274 1275
                          int create,
                          const char *virt_type)
1276
{
1277
    return virNodeDeviceDefParse(NULL, filename, create, virt_type);
1278 1279
}

1280 1281 1282 1283
/*
 * Return fc_host dev's WWNN and WWPN
 */
int
1284
virNodeDeviceGetWWNs(virNodeDeviceDefPtr def,
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
                     char **wwnn,
                     char **wwpn)
{
    virNodeDevCapsDefPtr cap = NULL;
    int ret = 0;

    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) {
            *wwnn = strdup(cap->data.scsi_host.wwnn);
            *wwpn = strdup(cap->data.scsi_host.wwpn);
            break;
        }

        cap = cap->next;
    }

    if (cap == NULL) {
1304 1305
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Device is not a fibre channel HBA"));
1306
        ret = -1;
1307
    } else if (*wwnn == NULL || *wwpn == NULL) {
1308
        /* Free the other one, if allocated... */
1309 1310
        VIR_FREE(*wwnn);
        VIR_FREE(*wwpn);
1311
        ret = -1;
1312
        virReportOOMError();
1313 1314 1315 1316 1317 1318 1319 1320 1321
    }

    return ret;
}

/*
 * Return the NPIV dev's parent device name
 */
int
1322
virNodeDeviceGetParentHost(const virNodeDeviceObjListPtr devs,
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
                           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) {
1333 1334 1335
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not find parent device for '%s'"),
                       dev_name);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
        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) {
1353 1354 1355 1356
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parent device %s is not capable "
                         "of vport operations"),
                       parent->def->name);
1357 1358 1359 1360 1361 1362 1363 1364
        ret = -1;
    }

    virNodeDeviceObjUnlock(parent);

out:
    return ret;
}
1365

1366 1367
void virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps)
{
1368
    int i = 0;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    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);
1384 1385 1386 1387
        VIR_FREE(data->pci_dev.physical_function);
        for (i = 0 ; i < data->pci_dev.num_virtual_functions ; i++) {
            VIR_FREE(data->pci_dev.virtual_functions[i]);
        }
1388 1389 1390 1391 1392 1393 1394 1395 1396
        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:
1397
        VIR_FREE(data->net.ifname);
1398 1399 1400
        VIR_FREE(data->net.address);
        break;
    case VIR_NODE_DEV_CAP_SCSI_HOST:
1401 1402
        VIR_FREE(data->scsi_host.wwnn);
        VIR_FREE(data->scsi_host.wwpn);
O
Osier Yang 已提交
1403
        VIR_FREE(data->scsi_host.fabric_wwn);
1404
        break;
D
David Allan 已提交
1405 1406 1407
    case VIR_NODE_DEV_CAP_SCSI_TARGET:
        VIR_FREE(data->scsi_target.name);
        break;
1408 1409 1410 1411
    case VIR_NODE_DEV_CAP_SCSI:
        VIR_FREE(data->scsi.type);
        break;
    case VIR_NODE_DEV_CAP_STORAGE:
1412
        VIR_FREE(data->storage.block);
1413 1414 1415 1416
        VIR_FREE(data->storage.bus);
        VIR_FREE(data->storage.drive_type);
        VIR_FREE(data->storage.model);
        VIR_FREE(data->storage.vendor);
1417
        VIR_FREE(data->storage.serial);
1418
        VIR_FREE(data->storage.media_label);
1419
        break;
1420 1421
    case VIR_NODE_DEV_CAP_FC_HOST:
    case VIR_NODE_DEV_CAP_VPORTS:
1422
    case VIR_NODE_DEV_CAP_LAST:
1423
    default:
1424 1425 1426 1427 1428 1429 1430
        /* This case is here to shutup the compiler */
        break;
    }

    VIR_FREE(caps);
}

D
Daniel P. Berrange 已提交
1431

1432 1433
void virNodeDeviceObjLock(virNodeDeviceObjPtr obj)
{
1434
    virMutexLock(&obj->lock);
1435 1436 1437 1438
}

void virNodeDeviceObjUnlock(virNodeDeviceObjPtr obj)
{
1439
    virMutexUnlock(&obj->lock);
D
Daniel P. Berrange 已提交
1440
}
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450

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;
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

        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;
        }
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    }

    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) &&
1492 1493 1494 1495 1496
               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) &&
               virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_VPORTS))))
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
            return false;
    }

    return true;
}
#undef MATCH

int
virNodeDeviceList(virConnectPtr conn,
                  virNodeDeviceObjList devobjs,
                  virNodeDevicePtr **devices,
                  unsigned int flags)
{
    virNodeDevicePtr *tmp_devices = NULL;
    virNodeDevicePtr device = NULL;
    int ndevices = 0;
    int ret = -1;
    int i;

    if (devices) {
        if (VIR_ALLOC_N(tmp_devices, devobjs.count + 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
    }

    for (i = 0; i < devobjs.count; i++) {
        virNodeDeviceObjPtr devobj = devobjs.objs[i];
        virNodeDeviceObjLock(devobj);
        if (virNodeDeviceMatch(devobj, flags)) {
            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;
}