node_device_driver.c 17.9 KB
Newer Older
1 2 3
/*
 * node_device.c: node device enumeration
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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>
29 30
#include <fcntl.h>
#include <time.h>
E
Eric Blake 已提交
31
#include <sys/stat.h>
32 33 34 35

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

42 43
#define VIR_FROM_THIS VIR_FROM_NODEDEV

44 45 46 47 48 49

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

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

    return 0;
}


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

    VIR_FREE(dev->def->driver);

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

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

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

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

115

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

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

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

    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;

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

    return ndevs;

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


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

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

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

198
    ret = virGetNodeDevice(conn, name);
199

200
cleanup:
201 202
    if (obj)
        virNodeDeviceObjUnlock(obj);
203
    return ret;
204 205
}

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

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


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

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

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

272
    update_driver_name(obj);
273 274
    update_caps(obj);

275
    ret = virNodeDeviceDefFormat(obj->def);
276 277

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


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

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

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

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

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


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

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

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

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
338
    ret = ncaps;
339

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


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

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

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

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

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


388
static int
389
nodeDeviceVportCreateDelete(const int parent_host,
390 391 392 393 394 395 396
                            const char *wwpn,
                            const char *wwnn,
                            int operation)
{
    int retval = 0;
    char *operation_path = NULL, *vport_name = NULL;
    const char *operation_file = NULL;
397
    struct stat st;
398 399 400 401 402 403 404 405 406

    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:
407
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
408 409 410 411 412 413 414 415 416 417 418 419
                                 _("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) {

420
        virReportOOMError();
421 422 423 424
        retval = -1;
        goto cleanup;
    }

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    if (stat(operation_path, &st) != 0) {
        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;
        }

        if (stat(operation_path, &st) != 0) {
            VIR_ERROR(_("No vport operation path found for host%d"),
                      parent_host);
            retval = -1;
            goto cleanup;
        }
    }

445
    VIR_DEBUG("Vport operation path is '%s'", operation_path);
446 447 448 449 450 451

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

452
        virReportOOMError();
453 454 455 456
        retval = -1;
        goto cleanup;
    }

457
    if (virFileWriteStr(operation_path, vport_name, 0) == -1) {
458
        virReportSystemError(errno,
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
                             _("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
474
get_time(time_t *t)
475 476 477 478 479
{
    int ret = 0;

    *t = time(NULL);
    if (*t == (time_t)-1) {
480
        virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
                                 "%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);

518
    get_time(&start);
519 520 521

    while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {

522
        virFileWaitForDevices();
523 524 525 526 527 528 529 530

        dev = nodeDeviceLookupByWWN(conn, wwnn, wwpn);

        if (dev != NULL) {
            break;
        }

        sleep(5);
531
        if (get_time(&now) == -1) {
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
            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);

554
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
555 556 557 558
    if (def == NULL) {
        goto cleanup;
    }

559
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
560 561 562
        goto cleanup;
    }

563
    if (virNodeDeviceGetParentHost(&driver->devs,
564 565 566
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
567 568 569
        goto cleanup;
    }

570
    if (nodeDeviceVportCreateDelete(parent_host,
571 572 573 574 575 576 577 578 579 580 581
                                    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) {
582
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
583 584 585 586 587 588 589 590 591 592 593 594 595 596
    }

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


static int
nodeDeviceDestroy(virNodeDevicePtr dev)
{
D
David Allan 已提交
597
    int ret = -1;
598 599 600 601 602 603 604 605 606 607
    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) {
608
        virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
609 610 611
        goto out;
    }

612
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
613 614 615 616 617
        goto out;
    }

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

618
    /* virNodeDeviceGetParentHost will cause the device object's lock to be
619 620 621 622 623 624 625
     * 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) {
626
        virReportOOMError();
627 628 629
        goto out;
    }

630
    if (virNodeDeviceGetParentHost(&driver->devs,
631 632 633
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
634 635 636
        goto out;
    }

637
    if (nodeDeviceVportCreateDelete(parent_host,
638 639 640 641 642 643
                                    wwpn,
                                    wwnn,
                                    VPORT_DELETE) == -1) {
        goto out;
    }

D
David Allan 已提交
644
    ret = 0;
645
out:
646 647
    if (obj)
        virNodeDeviceObjUnlock(obj);
648 649 650 651 652 653 654
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}


655 656 657 658 659
void registerCommonNodeFuncs(virDeviceMonitorPtr driver)
{
    driver->numOfDevices = nodeNumOfDevices;
    driver->listDevices = nodeListDevices;
    driver->deviceLookupByName = nodeDeviceLookupByName;
660
    driver->deviceGetXMLDesc = nodeDeviceGetXMLDesc;
661 662 663
    driver->deviceGetParent = nodeDeviceGetParent;
    driver->deviceNumOfCaps = nodeDeviceNumOfCaps;
    driver->deviceListCaps = nodeDeviceListCaps;
664 665
    driver->deviceCreateXML = nodeDeviceCreateXML;
    driver->deviceDestroy = nodeDeviceDestroy;
666 667 668 669
}


int nodedevRegister(void) {
670
#if defined(HAVE_HAL) && defined(HAVE_UDEV)
671
    /* Register only one of these two - they conflict */
672 673
    if (udevNodeRegister() == -1)
        return halNodeRegister();
674 675
    return 0;
#else
676
# ifdef HAVE_HAL
677
    return halNodeRegister();
678 679
# endif
# ifdef HAVE_UDEV
680
    return udevNodeRegister();
681
# endif
682 683
#endif
}