node_device_driver.c 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * node_device.c: node device enumeration
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

#include <config.h>

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

#include "virterror_internal.h"
#include "datatypes.h"
#include "memory.h"
34
#include "logging.h"
35
#include "node_device_conf.h"
36
#include "node_device_hal.h"
37
#include "node_device_driver.h"
D
Daniel P. Berrange 已提交
38
#include "util.h"
39

40 41
#define VIR_FROM_THIS VIR_FROM_NODEDEV

42 43 44 45 46 47

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

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

    return 0;
}


59 60 61 62
#if defined (__linux__) && defined (HAVE_HAL)
/* 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. */
63
static int update_driver_name(virNodeDeviceObjPtr dev)
64 65
{
    char *driver_link = NULL;
66
    char *devpath = NULL;
67 68 69 70 71
    char *p;
    int ret = -1;

    VIR_FREE(dev->def->driver);

72
    if (virAsprintf(&driver_link, "%s/driver", dev->def->sysfs_path) < 0) {
73
        virReportOOMError();
74 75 76 77 78 79 80 81 82 83
        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;
    }

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

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

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

113

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

123 124 125 126 127 128 129 130
static int nodeNumOfDevices(virConnectPtr conn,
                            const char *cap,
                            unsigned int flags ATTRIBUTE_UNUSED)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ndevs = 0;
    unsigned int i;

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

    return ndevs;
}

static int
nodeListDevices(virConnectPtr conn,
                const char *cap,
                char **const names, int maxnames,
                unsigned int flags ATTRIBUTE_UNUSED)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ndevs = 0;
    unsigned int i;

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

    return ndevs;

 failure:
172
    nodeDeviceUnlock(driver);
173 174 175 176 177 178 179 180 181 182 183
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}


static virNodeDevicePtr nodeDeviceLookupByName(virConnectPtr conn,
                                               const char *name)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
184 185
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;
186

187
    nodeDeviceLock(driver);
188
    obj = virNodeDeviceFindByName(&driver->devs, name);
189 190
    nodeDeviceUnlock(driver);

191
    if (!obj) {
192
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
193
        goto cleanup;
194 195
    }

196
    ret = virGetNodeDevice(conn, name);
197

198
cleanup:
199 200
    if (obj)
        virNodeDeviceObjUnlock(obj);
201
    return ret;
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

static virNodeDevicePtr
nodeDeviceLookupByWWN(virConnectPtr conn,
                      const char *wwnn,
                      const char *wwpn)
{
    unsigned int i;
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDeviceObjListPtr devs = &driver->devs;
    virNodeDevCapsDefPtr cap = NULL;
    virNodeDeviceObjPtr obj = NULL;
    virNodeDevicePtr dev = NULL;

    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) {
228
                check_fc_host(&cap->data);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                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;
}


252 253 254 255
static char *nodeDeviceDumpXML(virNodeDevicePtr dev,
                               unsigned int flags ATTRIBUTE_UNUSED)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
256 257
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
258

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

263
    if (!obj) {
264 265
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
266
                                 dev->name);
267
        goto cleanup;
268 269
    }

270
    update_driver_name(obj);
271 272
    update_caps(obj);

273
    ret = virNodeDeviceDefFormat(obj->def);
274 275

cleanup:
276 277
    if (obj)
        virNodeDeviceObjUnlock(obj);
278
    return ret;
279 280 281 282 283 284
}


static char *nodeDeviceGetParent(virNodeDevicePtr dev)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
285 286
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
287

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

292
    if (!obj) {
293 294
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
295
                                 dev->name);
296
        goto cleanup;
297 298
    }

299 300 301
    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
302
            virReportOOMError();
303
    } else {
304
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
305 306
                                 "%s", _("no parent for this device"));
    }
307 308

cleanup:
309 310
    if (obj)
        virNodeDeviceObjUnlock(obj);
311
    return ret;
312 313 314 315 316 317
}


static int nodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
318
    virNodeDeviceObjPtr obj;
319 320
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
321
    int ret = -1;
322

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

327
    if (!obj) {
328 329
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
330
                                 dev->name);
331
        goto cleanup;
332 333 334 335
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
336
    ret = ncaps;
337

338
cleanup:
339 340
    if (obj)
        virNodeDeviceObjUnlock(obj);
341
    return ret;
342 343 344 345 346 347 348
}


static int
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
349
    virNodeDeviceObjPtr obj;
350 351
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
352
    int ret = -1;
353

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

358
    if (!obj) {
359 360
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
                                 _("no node device with matching name '%s'"),
361
                                 dev->name);
362
        goto cleanup;
363 364 365 366
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
367
        if (names[ncaps++] == NULL) {
368
            virReportOOMError();
369
            goto cleanup;
370
        }
371
    }
372
    ret = ncaps;
373

374
cleanup:
375 376
    if (obj)
        virNodeDeviceObjUnlock(obj);
377 378 379 380 381 382
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
383 384 385
}


386
static int
387
nodeDeviceVportCreateDelete(const int parent_host,
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
                            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:
404
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
405 406 407 408 409 410 411 412 413 414 415 416
                                 _("Invalid vport operation (%d)"), operation);
        retval = -1;
        goto cleanup;
        break;
    }

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

417
        virReportOOMError();
418 419 420 421
        retval = -1;
        goto cleanup;
    }

422
    VIR_DEBUG("Vport operation path is '%s'", operation_path);
423 424 425 426 427 428

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

429
        virReportOOMError();
430 431 432 433 434
        retval = -1;
        goto cleanup;
    }

    if (virFileWriteStr(operation_path, vport_name) == -1) {
435
        virReportSystemError(errno,
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
                             _("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
451
get_time(time_t *t)
452 453 454 455 456
{
    int ret = 0;

    *t = time(NULL);
    if (*t == (time_t)-1) {
457
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                                 "%s", _("Could not get current time"));

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

495
    get_time(&start);
496 497 498

    while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {

499
        virFileWaitForDevices();
500 501 502 503 504 505 506 507

        dev = nodeDeviceLookupByWWN(conn, wwnn, wwpn);

        if (dev != NULL) {
            break;
        }

        sleep(5);
508
        if (get_time(&now) == -1) {
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            break;
        }
    }

    nodeDeviceLock(driver);

    return dev;
}

static virNodeDevicePtr
nodeDeviceCreateXML(virConnectPtr conn,
                    const char *xmlDesc,
                    unsigned int flags ATTRIBUTE_UNUSED)
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;

    nodeDeviceLock(driver);

531
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
532 533 534 535
    if (def == NULL) {
        goto cleanup;
    }

536
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
537 538 539
        goto cleanup;
    }

540
    if (virNodeDeviceGetParentHost(&driver->devs,
541 542 543
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
544 545 546
        goto cleanup;
    }

547
    if (nodeDeviceVportCreateDelete(parent_host,
548 549 550 551 552 553 554 555 556 557 558
                                    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) {
559
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
560 561 562 563 564 565 566 567 568 569 570 571 572 573
    }

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


static int
nodeDeviceDestroy(virNodeDevicePtr dev)
{
D
David Allan 已提交
574
    int ret = -1;
575 576 577 578 579 580 581 582 583 584
    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) {
585
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
586 587 588
        goto out;
    }

589
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
590 591 592 593 594
        goto out;
    }

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

595
    /* virNodeDeviceGetParentHost will cause the device object's lock to be
596 597 598 599 600 601 602
     * 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) {
603
        virReportOOMError();
604 605 606
        goto out;
    }

607
    if (virNodeDeviceGetParentHost(&driver->devs,
608 609 610
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
611 612 613
        goto out;
    }

614
    if (nodeDeviceVportCreateDelete(parent_host,
615 616 617 618 619 620
                                    wwpn,
                                    wwnn,
                                    VPORT_DELETE) == -1) {
        goto out;
    }

D
David Allan 已提交
621
    ret = 0;
622
out:
623 624
    if (obj)
        virNodeDeviceObjUnlock(obj);
625 626 627 628 629 630 631
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}


632 633 634 635 636 637 638 639 640
void registerCommonNodeFuncs(virDeviceMonitorPtr driver)
{
    driver->numOfDevices = nodeNumOfDevices;
    driver->listDevices = nodeListDevices;
    driver->deviceLookupByName = nodeDeviceLookupByName;
    driver->deviceDumpXML = nodeDeviceDumpXML;
    driver->deviceGetParent = nodeDeviceGetParent;
    driver->deviceNumOfCaps = nodeDeviceNumOfCaps;
    driver->deviceListCaps = nodeDeviceListCaps;
641 642
    driver->deviceCreateXML = nodeDeviceCreateXML;
    driver->deviceDestroy = nodeDeviceDestroy;
643 644 645 646
}


int nodedevRegister(void) {
647
#if defined(HAVE_HAL) && defined(HAVE_UDEV)
648 649
    /* Register only one of these two - they conflict */
    if (halNodeRegister() == -1)
650
        return udevNodeRegister();
651 652
    return 0;
#else
653
# ifdef HAVE_HAL
654
    return halNodeRegister();
655 656
# endif
# ifdef HAVE_UDEV
657
    return udevNodeRegister();
658
# endif
659 660
#endif
}