node_device_driver.c 17.2 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 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
int
nodeNumOfDevices(virConnectPtr conn,
                 const char *cap,
E
Eric Blake 已提交
128
                 unsigned int flags)
129 130 131 132 133
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    int ndevs = 0;
    unsigned int i;

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

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

    return ndevs;
}

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

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

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

    return ndevs;

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


187 188
virNodeDevicePtr
nodeDeviceLookupByName(virConnectPtr conn, const char *name)
189 190
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
191 192
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;
193

194
    nodeDeviceLock(driver);
195
    obj = virNodeDeviceFindByName(&driver->devs, name);
196 197
    nodeDeviceUnlock(driver);

198
    if (!obj) {
199
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
200
        goto cleanup;
201 202
    }

203
    ret = virGetNodeDevice(conn, name);
204

205
cleanup:
206 207
    if (obj)
        virNodeDeviceObjUnlock(obj);
208
    return ret;
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

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) {
235
                check_fc_host(&cap->data);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                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;
}


259 260
char *
nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
261
                     unsigned int flags)
262 263
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
264 265
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
266

E
Eric Blake 已提交
267 268
    virCheckFlags(0, NULL);

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

273
    if (!obj) {
274 275 276
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
277
        goto cleanup;
278 279
    }

280
    update_driver_name(obj);
281 282
    update_caps(obj);

283
    ret = virNodeDeviceDefFormat(obj->def);
284 285

cleanup:
286 287
    if (obj)
        virNodeDeviceObjUnlock(obj);
288
    return ret;
289 290 291
}


292 293
char *
nodeDeviceGetParent(virNodeDevicePtr dev)
294 295
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
296 297
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
298

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

303
    if (!obj) {
304 305 306
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
307
        goto cleanup;
308 309
    }

310 311 312
    if (obj->def->parent) {
        ret = strdup(obj->def->parent);
        if (!ret)
313
            virReportOOMError();
314
    } else {
315 316
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
317
    }
318 319

cleanup:
320 321
    if (obj)
        virNodeDeviceObjUnlock(obj);
322
    return ret;
323 324 325
}


326 327
int
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
328 329
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
330
    virNodeDeviceObjPtr obj;
331 332
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
333
    int ret = -1;
334

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

339
    if (!obj) {
340 341 342
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
343
        goto cleanup;
344 345 346 347
    }

    for (caps = obj->def->caps; caps; caps = caps->next)
        ++ncaps;
348
    ret = ncaps;
349

350
cleanup:
351 352
    if (obj)
        virNodeDeviceObjUnlock(obj);
353
    return ret;
354 355 356
}


357
int
358 359 360
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
    virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
361
    virNodeDeviceObjPtr obj;
362 363
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
364
    int ret = -1;
365

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

370
    if (!obj) {
371 372 373
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
374
        goto cleanup;
375 376 377 378
    }

    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
379
        if (names[ncaps++] == NULL) {
380
            virReportOOMError();
381
            goto cleanup;
382
        }
383
    }
384
    ret = ncaps;
385

386
cleanup:
387 388
    if (obj)
        virNodeDeviceObjUnlock(obj);
389 390 391 392 393 394
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
395 396 397
}


398
static int
399
nodeDeviceVportCreateDelete(const int parent_host,
400 401 402 403 404 405 406
                            const char *wwpn,
                            const char *wwnn,
                            int operation)
{
    int retval = 0;
    char *operation_path = NULL, *vport_name = NULL;
    const char *operation_file = NULL;
407
    struct stat st;
408 409 410 411 412 413 414 415 416

    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:
417 418
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid vport operation (%d)"), operation);
419 420 421 422 423 424 425 426 427 428 429
        retval = -1;
        goto cleanup;
        break;
    }

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

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

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    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;
        }
    }

455
    VIR_DEBUG("Vport operation path is '%s'", operation_path);
456 457 458 459 460 461

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

462
        virReportOOMError();
463 464 465 466
        retval = -1;
        goto cleanup;
    }

467
    if (virFileWriteStr(operation_path, vport_name, 0) == -1) {
468
        virReportSystemError(errno,
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
                             _("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
484
get_time(time_t *t)
485 486 487 488 489
{
    int ret = 0;

    *t = time(NULL);
    if (*t == (time_t)-1) {
490 491
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Could not get current time"));
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 518 519 520 521 522 523 524 525 526 527

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

528
    get_time(&start);
529 530 531

    while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {

532
        virFileWaitForDevices();
533 534 535 536 537 538 539 540

        dev = nodeDeviceLookupByWWN(conn, wwnn, wwpn);

        if (dev != NULL) {
            break;
        }

        sleep(5);
541
        if (get_time(&now) == -1) {
542 543 544 545 546 547 548 549 550
            break;
        }
    }

    nodeDeviceLock(driver);

    return dev;
}

551
virNodeDevicePtr
552 553
nodeDeviceCreateXML(virConnectPtr conn,
                    const char *xmlDesc,
E
Eric Blake 已提交
554
                    unsigned int flags)
555 556 557 558 559 560
{
    virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
561
    const char *virt_type = NULL;
562

E
Eric Blake 已提交
563
    virCheckFlags(0, NULL);
564
    virt_type  = virConnectGetType(conn);
E
Eric Blake 已提交
565

566 567
    nodeDeviceLock(driver);

568
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type);
569 570 571 572
    if (def == NULL) {
        goto cleanup;
    }

573
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
574 575 576
        goto cleanup;
    }

577
    if (virNodeDeviceGetParentHost(&driver->devs,
578 579 580
                                   def->name,
                                   def->parent,
                                   &parent_host) == -1) {
581 582 583
        goto cleanup;
    }

584
    if (nodeDeviceVportCreateDelete(parent_host,
585 586 587 588 589 590 591 592 593 594 595
                                    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) {
596
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
597 598 599 600 601 602 603 604 605 606 607
    }

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


608
int
609 610
nodeDeviceDestroy(virNodeDevicePtr dev)
{
D
David Allan 已提交
611
    int ret = -1;
612 613 614 615 616 617 618 619 620 621
    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) {
622
        virReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
623 624 625
        goto out;
    }

626
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
627 628 629 630 631
        goto out;
    }

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

632
    /* virNodeDeviceGetParentHost will cause the device object's lock to be
633 634 635 636 637 638 639
     * 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) {
640
        virReportOOMError();
641 642 643
        goto out;
    }

644
    if (virNodeDeviceGetParentHost(&driver->devs,
645 646 647
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
648 649 650
        goto out;
    }

651
    if (nodeDeviceVportCreateDelete(parent_host,
652 653 654 655 656 657
                                    wwpn,
                                    wwnn,
                                    VPORT_DELETE) == -1) {
        goto out;
    }

D
David Allan 已提交
658
    ret = 0;
659
out:
660 661
    if (obj)
        virNodeDeviceObjUnlock(obj);
662 663 664 665 666 667
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

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