node_device_driver.c 17.6 KB
Newer Older
1 2 3
/*
 * node_device.c: node device enumeration
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 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
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

#include <config.h>

#include <unistd.h>
#include <errno.h>
29 30
#include <fcntl.h>
#include <time.h>
31

32
#include "virerror.h"
33
#include "datatypes.h"
34
#include "viralloc.h"
35
#include "virlog.h"
36
#include "node_device_conf.h"
37
#include "node_device_hal.h"
38
#include "node_device_driver.h"
39
#include "virutil.h"
40

41 42
#define VIR_FROM_THIS VIR_FROM_NODEDEV

43 44 45 46 47 48

static int update_caps(virNodeDeviceObjPtr dev)
{
    virNodeDevCapsDefPtr cap = dev->def->caps;

    while (cap) {
49
        /* The only caps that currently need updating are FC related. */
50
        if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST) {
O
Osier Yang 已提交
51
            detect_scsi_host_caps(&dev->def->caps->data);
52
        }
53
        cap = cap->next;
54 55 56 57 58 59
    }

    return 0;
}


60
#if defined (__linux__) && defined (WITH_HAL)
61 62 63
/* Under libudev changes to the driver name should be picked up as
 * "change" events, so we don't call update driver name unless we're
 * using the HAL backend. */
64
static int update_driver_name(virNodeDeviceObjPtr dev)
65 66
{
    char *driver_link = NULL;
67
    char *devpath = NULL;
68 69 70 71 72
    char *p;
    int ret = -1;

    VIR_FREE(dev->def->driver);

73
    if (virAsprintf(&driver_link, "%s/driver", dev->def->sysfs_path) < 0) {
74
        virReportOOMError();
75 76 77 78 79 80 81 82 83 84
        goto cleanup;
    }

    /* Some devices don't have an explicit driver, so just return
       without a name */
    if (access(driver_link, R_OK) < 0) {
        ret = 0;
        goto cleanup;
    }

85
    if (virFileResolveLink(driver_link, &devpath) < 0) {
86
        virReportSystemError(errno,
87 88 89 90 91 92 93 94
                             _("cannot resolve driver link %s"), driver_link);
        goto cleanup;
    }

    p = strrchr(devpath, '/');
    if (p) {
        dev->def->driver = strdup(p+1);
        if (!dev->def->driver) {
95
            virReportOOMError();
96 97 98 99 100 101 102
            goto cleanup;
        }
    }
    ret = 0;

cleanup:
    VIR_FREE(driver_link);
103
    VIR_FREE(devpath);
104 105 106 107
    return ret;
}
#else
/* XXX: Implement me for non-linux */
108
static int update_driver_name(virNodeDeviceObjPtr dev ATTRIBUTE_UNUSED)
109 110 111 112 113
{
    return 0;
}
#endif

114

115 116
void nodeDeviceLock(virDeviceMonitorStatePtr driver)
{
117
    virMutexLock(&driver->lock);
118 119 120
}
void nodeDeviceUnlock(virDeviceMonitorStatePtr driver)
{
121
    virMutexUnlock(&driver->lock);
122 123
}

124 125 126
int
nodeNumOfDevices(virConnectPtr conn,
                 const char *cap,
E
Eric Blake 已提交
127
                 unsigned int flags)
128 129 130 131 132
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
133 134
    virCheckFlags(0, -1);

135 136 137
    nodeDeviceLock(driver);
    for (i = 0; i < driver->devs.count; i++) {
        virNodeDeviceObjLock(driver->devs.objs[i]);
138
        if ((cap == NULL) ||
139
            virNodeDeviceHasCap(driver->devs.objs[i], cap))
140
            ++ndevs;
141 142 143
        virNodeDeviceObjUnlock(driver->devs.objs[i]);
    }
    nodeDeviceUnlock(driver);
144 145 146 147

    return ndevs;
}

148
int
149 150 151
nodeListDevices(virConnectPtr conn,
                const char *cap,
                char **const names, int maxnames,
E
Eric Blake 已提交
152
                unsigned int flags)
153 154 155 156 157
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ndevs = 0;
    unsigned int i;

E
Eric Blake 已提交
158 159
    virCheckFlags(0, -1);

160 161 162
    nodeDeviceLock(driver);
    for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
        virNodeDeviceObjLock(driver->devs.objs[i]);
163
        if (cap == NULL ||
164
            virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
165 166
            if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
                virNodeDeviceObjUnlock(driver->devs.objs[i]);
167
                virReportOOMError();
168
                goto failure;
169 170 171 172 173
            }
        }
        virNodeDeviceObjUnlock(driver->devs.objs[i]);
    }
    nodeDeviceUnlock(driver);
174 175 176 177

    return ndevs;

 failure:
178
    nodeDeviceUnlock(driver);
179 180 181 182 183 184
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}

O
Osier Yang 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
int
nodeListAllNodeDevices(virConnectPtr conn,
                       virNodeDevicePtr **devices,
                       unsigned int flags)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP, -1);

    nodeDeviceLock(driver);
    ret = virNodeDeviceList(conn, driver->devs, devices, flags);
    nodeDeviceUnlock(driver);
    return ret;
}
200

201 202
virNodeDevicePtr
nodeDeviceLookupByName(virConnectPtr conn, const char *name)
203 204
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
205 206
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;
207

208
    nodeDeviceLock(driver);
209
    obj = virNodeDeviceFindByName(&driver->devs, name);
210 211
    nodeDeviceUnlock(driver);

212
    if (!obj) {
213
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
214
        goto cleanup;
215 216
    }

217
    ret = virGetNodeDevice(conn, name);
218

219
cleanup:
220 221
    if (obj)
        virNodeDeviceObjUnlock(obj);
222
    return ret;
223 224
}

225

226 227 228 229 230
virNodeDevicePtr
nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
                              const char *wwnn,
                              const char *wwpn,
                              unsigned int flags)
231 232 233 234 235 236 237 238
{
    unsigned int i;
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDeviceObjListPtr devs = &driver->devs;
    virNodeDevCapsDefPtr cap = NULL;
    virNodeDeviceObjPtr obj = NULL;
    virNodeDevicePtr dev = NULL;

239 240
    virCheckFlags(0, NULL);

241 242 243 244 245 246 247 248 249
    nodeDeviceLock(driver);

    for (i = 0; i < devs->count; i++) {
        obj = devs->objs[i];
        virNodeDeviceObjLock(obj);
        cap = obj->def->caps;

        while (cap) {
            if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST) {
O
Osier Yang 已提交
250
                detect_scsi_host_caps(&cap->data);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                if (cap->data.scsi_host.flags &
                    VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
                    if (STREQ(cap->data.scsi_host.wwnn, wwnn) &&
                        STREQ(cap->data.scsi_host.wwpn, wwpn)) {
                        dev = virGetNodeDevice(conn, obj->def->name);
                        virNodeDeviceObjUnlock(obj);
                        goto out;
                    }
                }
            }
            cap = cap->next;
        }

        virNodeDeviceObjUnlock(obj);
    }

out:
    nodeDeviceUnlock(driver);
    return dev;
}


273 274
char *
nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
275
                     unsigned int flags)
276 277
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
278 279
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
280

E
Eric Blake 已提交
281 282
    virCheckFlags(0, NULL);

283
    nodeDeviceLock(driver);
284
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
285 286
    nodeDeviceUnlock(driver);

287
    if (!obj) {
288 289 290
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
291
        goto cleanup;
292 293
    }

294
    update_driver_name(obj);
295 296
    update_caps(obj);

297
    ret = virNodeDeviceDefFormat(obj->def);
298 299

cleanup:
300 301
    if (obj)
        virNodeDeviceObjUnlock(obj);
302
    return ret;
303 304 305
}


306 307
char *
nodeDeviceGetParent(virNodeDevicePtr dev)
308 309
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
310 311
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
312

313
    nodeDeviceLock(driver);
314
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
315 316
    nodeDeviceUnlock(driver);

317
    if (!obj) {
318 319 320
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
321
        goto cleanup;
322 323
    }

324 325 326
    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
327
            virReportOOMError();
328
    } else {
329 330
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
331
    }
332 333

cleanup:
334 335
    if (obj)
        virNodeDeviceObjUnlock(obj);
336
    return ret;
337 338 339
}


340 341
int
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
342 343
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
344
    virNodeDeviceObjPtr obj;
345 346
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
347
    int ret = -1;
348

349
    nodeDeviceLock(driver);
350
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
351 352
    nodeDeviceUnlock(driver);

353
    if (!obj) {
354 355 356
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
357
        goto cleanup;
358 359 360 361
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
362
    ret = ncaps;
363

364
cleanup:
365 366
    if (obj)
        virNodeDeviceObjUnlock(obj);
367
    return ret;
368 369 370
}


371
int
372 373 374
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
375
    virNodeDeviceObjPtr obj;
376 377
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
378
    int ret = -1;
379

380
    nodeDeviceLock(driver);
381
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
382 383
    nodeDeviceUnlock(driver);

384
    if (!obj) {
385 386 387
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
388
        goto cleanup;
389 390 391 392
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
393
        if (names[ncaps++] == NULL) {
394
            virReportOOMError();
395
            goto cleanup;
396
        }
397
    }
398
    ret = ncaps;
399

400
cleanup:
401 402
    if (obj)
        virNodeDeviceObjUnlock(obj);
403 404 405 406 407 408
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
409 410 411
}


412
static int
413
nodeDeviceVportCreateDelete(const int parent_host,
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                            const char *wwpn,
                            const char *wwnn,
                            int operation)
{
    int retval = 0;
    char *operation_path = NULL, *vport_name = NULL;
    const char *operation_file = NULL;

    switch (operation) {
    case VPORT_CREATE:
        operation_file = LINUX_SYSFS_VPORT_CREATE_POSTFIX;
        break;
    case VPORT_DELETE:
        operation_file = LINUX_SYSFS_VPORT_DELETE_POSTFIX;
        break;
    default:
430 431
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid vport operation (%d)"), operation);
432 433 434 435 436 437 438 439 440 441 442
        retval = -1;
        goto cleanup;
        break;
    }

    if (virAsprintf(&operation_path,
                    "%shost%d%s",
                    LINUX_SYSFS_FC_HOST_PREFIX,
                    parent_host,
                    operation_file) < 0) {

443
        virReportOOMError();
444 445 446 447
        retval = -1;
        goto cleanup;
    }

O
Osier Yang 已提交
448
    if (!virFileExists(operation_path)) {
449 450 451 452 453 454 455 456 457 458 459
        VIR_FREE(operation_path);
        if (virAsprintf(&operation_path,
                        "%shost%d%s",
                        LINUX_SYSFS_SCSI_HOST_PREFIX,
                        parent_host,
                        operation_file) < 0) {
            virReportOOMError();
            retval = -1;
            goto cleanup;
        }

O
Osier Yang 已提交
460
        if (!virFileExists(operation_path)) {
461 462 463 464 465 466 467
            VIR_ERROR(_("No vport operation path found for host%d"),
                      parent_host);
            retval = -1;
            goto cleanup;
        }
    }

468
    VIR_DEBUG("Vport operation path is '%s'", operation_path);
469 470 471 472 473 474

    if (virAsprintf(&vport_name,
                    "%s:%s",
                    wwpn,
                    wwnn) < 0) {

475
        virReportOOMError();
476 477 478 479
        retval = -1;
        goto cleanup;
    }

480
    if (virFileWriteStr(operation_path, vport_name, 0) == -1) {
481
        virReportSystemError(errno,
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                             _("Write of '%s' to '%s' during "
                               "vport create/delete failed"),
                             vport_name, operation_path);
        retval = -1;
    }

cleanup:
    VIR_FREE(vport_name);
    VIR_FREE(operation_path);
    VIR_DEBUG("%s", _("Vport operation complete"));
    return retval;
}


static int
497
get_time(time_t *t)
498 499 500 501 502
{
    int ret = 0;

    *t = time(NULL);
    if (*t == (time_t)-1) {
503 504
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Could not get current time"));
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

        *t = 0;
        ret = -1;
    }

    return ret;
}


/* When large numbers of devices are present on the host, it's
 * possible for udev not to realize that it has work to do before we
 * get here.  We thus keep trying to find the new device we just
 * created for up to LINUX_NEW_DEVICE_WAIT_TIME.  Note that udev's
 * default settle time is 180 seconds, so once udev realizes that it
 * has work to do, it might take that long for the udev wait to
 * return.  Thus the total maximum time for this function to return is
 * the udev settle time plus LINUX_NEW_DEVICE_WAIT_TIME.
 *
 * This whole area is a race, but if we retry the udev wait for
 * LINUX_NEW_DEVICE_WAIT_TIME seconds and there's still no device,
 * it's probably safe to assume it's not going to appear.
 */
static virNodeDevicePtr
find_new_device(virConnectPtr conn, const char *wwnn, const char *wwpn)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDevicePtr dev = NULL;
    time_t start = 0, now = 0;

    /* The thread that creates the device takes the driver lock, so we
     * must release it in order to allow the device to be created.
     * We're not doing anything with the driver pointer at this point,
     * so it's safe to release it, assuming that the pointer itself
     * doesn't become invalid.  */
    nodeDeviceUnlock(driver);

541
    get_time(&start);
542 543 544

    while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {

545
        virFileWaitForDevices();
546

547
        dev = nodeDeviceLookupSCSIHostByWWN(conn, wwnn, wwpn, 0);
548 549 550 551 552 553

        if (dev != NULL) {
            break;
        }

        sleep(5);
554
        if (get_time(&now) == -1) {
555 556 557 558 559 560 561 562 563
            break;
        }
    }

    nodeDeviceLock(driver);

    return dev;
}

564
virNodeDevicePtr
565 566
nodeDeviceCreateXML(virConnectPtr conn,
                    const char *xmlDesc,
E
Eric Blake 已提交
567
                    unsigned int flags)
568 569 570 571 572 573
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
574
    const char *virt_type = NULL;
575

E
Eric Blake 已提交
576
    virCheckFlags(0, NULL);
577
    virt_type  = virConnectGetType(conn);
E
Eric Blake 已提交
578

579 580
    nodeDeviceLock(driver);

581
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type);
582 583 584 585
    if (def == NULL) {
        goto cleanup;
    }

586
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
587 588 589
        goto cleanup;
    }

590
    if (virNodeDeviceGetParentHost(&driver->devs,
591 592 593
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
594 595 596
        goto cleanup;
    }

597
    if (nodeDeviceVportCreateDelete(parent_host,
598 599 600 601 602 603 604 605 606 607 608
                                    wwpn,
                                    wwnn,
                                    VPORT_CREATE) == -1) {
        goto cleanup;
    }

    dev = find_new_device(conn, wwnn, wwpn);
    /* We don't check the return value, because one way or another,
     * we're returning what we get... */

    if (dev == NULL) {
609
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
610 611 612 613 614 615 616 617 618 619 620
    }

cleanup:
    nodeDeviceUnlock(driver);
    virNodeDeviceDefFree(def);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}


621
int
622 623
nodeDeviceDestroy(virNodeDevicePtr dev)
{
D
David Allan 已提交
624
    int ret = -1;
625 626 627 628 629 630 631 632 633 634
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;

    nodeDeviceLock(driver);
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
    nodeDeviceUnlock(driver);

    if (!obj) {
635
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
636 637 638
        goto out;
    }

639
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
640 641 642 643 644
        goto out;
    }

    parent_name = strdup(obj->def->parent);

645
    /* virNodeDeviceGetParentHost will cause the device object's lock to be
646 647 648 649 650 651 652
     * taken, so we have to dup the parent's name and drop the lock
     * before calling it.  We don't need the reference to the object
     * any more once we have the parent's name.  */
    virNodeDeviceObjUnlock(obj);
    obj = NULL;

    if (parent_name == NULL) {
653
        virReportOOMError();
654 655 656
        goto out;
    }

657
    if (virNodeDeviceGetParentHost(&driver->devs,
658 659 660
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
661 662 663
        goto out;
    }

664
    if (nodeDeviceVportCreateDelete(parent_host,
665 666 667 668 669 670
                                    wwpn,
                                    wwnn,
                                    VPORT_DELETE) == -1) {
        goto out;
    }

D
David Allan 已提交
671
    ret = 0;
672
out:
673 674
    if (obj)
        virNodeDeviceObjUnlock(obj);
675 676 677 678 679 680
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

681
int nodedevRegister(void) {
682
#if defined(WITH_HAL) && defined(WITH_UDEV)
683
    /* Register only one of these two - they conflict */
684 685
    if (udevNodeRegister() == -1)
        return halNodeRegister();
686 687
    return 0;
#else
688
# ifdef WITH_HAL
689
    return halNodeRegister();
690
# endif
691
# ifdef WITH_UDEV
692
    return udevNodeRegister();
693
# endif
694 695
#endif
}