“b030084f07f438af85eab35851a8cde4b51721ba”上不存在“git@gitcode.net:openeuler/libvirt.git”
interface_backend_netcf.c 35.4 KB
Newer Older
1
/*
2 3
 * interface_backend_netcf.c: backend driver methods to handle physical
 *                            interface configuration using the netcf library.
4
 *
5
 * Copyright (C) 2006-2014 Red Hat, Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
 *
 * Author: Laine Stump <laine@redhat.com>
 */

#include <config.h>

#include <netcf.h>

28
#include "virerror.h"
29
#include "datatypes.h"
30
#include "interface_driver.h"
31
#include "interface_conf.h"
32
#include "viralloc.h"
33
#include "virlog.h"
34 35
#include "virstring.h"
#include "viraccessapicheck.h"
36 37 38

#define VIR_FROM_THIS VIR_FROM_INTERFACE

39 40
VIR_LOG_INIT("interface.interface_backend_netcf");

41 42
#define INTERFACE_DRIVER_NAME "netcf"

43
/* Main driver state */
44
typedef struct
45
{
46
    virObjectLockable parent;
47
    struct netcf *netcf;
48
} virNetcfDriverState, *virNetcfDriverStatePtr;
49

50 51
static virClassPtr virNetcfDriverStateClass;
static void virNetcfDriverStateDispose(void *obj);
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static int
virNetcfDriverStateOnceInit(void)
{
    if (!(virNetcfDriverStateClass = virClassNew(virClassForObjectLockable(),
                                       "virNetcfDriverState",
                                       sizeof(virNetcfDriverState),
                                       virNetcfDriverStateDispose)))
        return -1;
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetcfDriverState)

static virNetcfDriverStatePtr driverState = NULL;


static void
virNetcfDriverStateDispose(void *obj)
71
{
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    virNetcfDriverStatePtr driver = obj;

    if (driver->netcf)
        ncf_close(driver->netcf);
}


static int
netcfStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
{
    if (virNetcfDriverStateInitialize() < 0)
        return -1;

    if (!(driverState = virObjectLockableNew(virNetcfDriverStateClass)))
        return -1;

    /* open netcf */
    if (ncf_init(&driverState->netcf, NULL) != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to initialize netcf"));
        virObjectUnref(driverState);
        driverState = NULL;
        return -1;
    }
    return 0;
99 100
}

101 102 103

static int
netcfStateCleanup(void)
104
{
105
    if (!driverState)
106 107 108 109 110 111 112 113 114 115
        return -1;

    if (virObjectUnref(driverState)) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Attempt to close netcf state driver "
                         "with open connections"));
        return -1;
    }
    driverState = NULL;
    return 0;
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

static int
netcfStateReload(void)
{
    int ret = -1;

    if (!driverState)
        return 0;

    virObjectLock(driverState);
    ncf_close(driverState->netcf);
    if (ncf_init(&driverState->netcf, NULL) != 0)
    {
        /* this isn't a good situation, because we can't shut down the
         * driver as there may still be connections to it. If we set
         * the netcf handle to NULL, any subsequent calls to netcf
         * will just fail rather than causing a crash. Not ideal, but
         * livable (since this should never happen).
         */
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to re-init netcf"));
        driverState->netcf = NULL;
        goto cleanup;
    }

    ret = 0;
cleanup:
    virObjectUnlock(driverState);

    return ret;
}


151 152 153 154 155 156 157 158 159 160 161
/*
 * Get a minimal virInterfaceDef containing enough metadata
 * for access control checks to be performed. Currently
 * this implies existance of name and mac address attributes
 */
static virInterfaceDef * ATTRIBUTE_NONNULL(1)
netcfGetMinimalDefForDevice(struct netcf_if *iface)
{
    virInterfaceDef *def;

    /* Allocate our interface definition structure */
162
    if (VIR_ALLOC(def) < 0)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        return NULL;

    if (VIR_STRDUP(def->name, ncf_if_name(iface)) < 0)
        goto cleanup;

    if (VIR_STRDUP(def->mac, ncf_if_mac_string(iface)) < 0)
        goto cleanup;

    return def;

cleanup:
    virInterfaceDefFree(def);
    return NULL;
}


179 180 181 182 183 184 185 186 187 188 189 190 191 192
static int netcf_to_vir_err(int netcf_errcode)
{
    switch (netcf_errcode)
    {
        case NETCF_NOERROR:
            /* no error, everything ok */
            return VIR_ERR_OK;
        case NETCF_EINTERNAL:
            /* internal error, aka bug */
            return VIR_ERR_INTERNAL_ERROR;
        case NETCF_EOTHER:
            /* other error, copout for being more specific */
            return VIR_ERR_INTERNAL_ERROR;
        case NETCF_ENOMEM:
193 194 195 196
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
197
            return 2;
198 199 200 201 202 203 204 205 206 207 208 209
        case NETCF_EXMLPARSER:
            /* XML parser choked */
            return VIR_ERR_XML_ERROR;
        case NETCF_EXMLINVALID:
            /* XML invalid in some form */
            return VIR_ERR_XML_ERROR;
        case NETCF_ENOENT:
            /* Required entry in a tree is missing */
            return VIR_ERR_INTERNAL_ERROR;
        case NETCF_EEXEC:
            /* external program execution failed or returned non-0 */
            return VIR_ERR_INTERNAL_ERROR;
210 211 212 213 214
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        default:
            return VIR_ERR_INTERNAL_ERROR;
    }
}

static struct netcf_if *interfaceDriverGetNetcfIF(struct netcf *ncf, virInterfacePtr ifinfo)
{
    /* 1) caller already has lock,
     * 2) caller cleans up iface on return
     */
    struct netcf_if *iface = ncf_lookup_by_name(ncf, ifinfo->name);
    if (!iface) {
        const char *errmsg, *details;
        int errcode = ncf_error(ncf, &errmsg, &details);
        if (errcode != NETCF_NOERROR) {
230 231 232 233
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
                           details ? details : "");
234
        } else {
235 236 237
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
238 239 240 241 242
        }
    }
    return iface;
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int
netcfInterfaceObjIsActive(struct netcf_if *iface,
                          bool *active)
{
    int ret = -1;
    unsigned int flags = 0;

    virObjectRef(driverState);
    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driverState->netcf, &errmsg, &details);
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get status of interface %s: %s%s%s"),
                       ncf_if_name(iface), errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
    }

    *active = flags & NETCF_IFACE_ACTIVE;
    ret = 0;

cleanup:
    virObjectUnref(driverState);
    return ret;
}

269 270 271 272
static virDrvOpenStatus
netcfInterfaceOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                   unsigned int flags)
273
{
274 275
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

276 277
    if (!driverState)
        return VIR_DRV_OPEN_ERROR;
278

279
    virObjectRef(driverState);
280
    conn->interfacePrivateData = driverState;
281
    return VIR_DRV_OPEN_SUCCESS;
282 283
}

284 285
static int
netcfInterfaceClose(virConnectPtr conn)
286 287 288 289
{

    if (conn->interfacePrivateData != NULL)
    {
290 291
        virObjectUnref(conn->interfacePrivateData);
        conn->interfacePrivateData = NULL;
292 293 294 295
    }
    return 0;
}

296 297 298
static int netcfConnectNumOfInterfacesImpl(virConnectPtr conn,
                                           int status,
                                           virInterfaceObjListFilter filter)
299
{
300
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
301 302 303
    int count;
    int want = 0;
    int ret = -1;
304
    size_t i;
305
    char **names = NULL;
306

307 308 309 310
    /* List all interfaces, in case we might support new filter flags
     * beyond active|inactive in future.
     */
    count = ncf_num_of_interfaces(driver->netcf, status);
311 312 313
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
314
        virReportError(netcf_to_vir_err(errcode),
315 316 317 318
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
319 320
    }

321 322 323 324 325
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

326
    if (VIR_ALLOC_N(names, count) < 0)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, names, status)) < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
    }

    for (i = 0; i < count; i++) {
        virInterfaceDefPtr def;
        struct netcf_if *iface;

        iface = ncf_lookup_by_name(driver->netcf, names[i]);
        if (!iface) {
            const char *errmsg, *details;
            int errcode = ncf_error(driver->netcf, &errmsg, &details);
            if (errcode != NETCF_NOERROR) {
                virReportError(netcf_to_vir_err(errcode),
                               _("couldn't find interface named '%s': %s%s%s"),
                               names[i], errmsg,
                               details ? " - " : "", details ? details : "");
                goto cleanup;
            } else {
                /* Ignore the NETCF_NOERROR, as the interface is very likely
                 * deleted by other management apps (e.g. virt-manager).
                 */
                VIR_WARN("couldn't find interface named '%s', might be "
                         "deleted by other process", names[i]);
                continue;
            }
        }

        if (!(def = netcfGetMinimalDefForDevice(iface))) {
            ncf_if_free(iface);
            goto cleanup;
        }
        ncf_if_free(iface);

        if (!filter(conn, def)) {
            virInterfaceDefFree(def);
            continue;
        }
        virInterfaceDefFree(def);

        want++;
    }

    ret = want;

cleanup:
381
    if (names && count > 0)
382 383 384 385
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);
    return ret;
386 387
}

388 389 390 391 392

static int netcfConnectListInterfacesImpl(virConnectPtr conn,
                                          int status,
                                          char **const names, int nnames,
                                          virInterfaceObjListFilter filter)
393
{
394
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
395 396 397
    int count = 0;
    int want = 0;
    int ret = -1;
398
    size_t i;
399
    char **allnames = NULL;
400

401 402 403 404 405 406 407 408 409 410
    count = ncf_num_of_interfaces(driver->netcf, status);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
    }
411

412 413 414 415
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
416

417
    if (VIR_ALLOC_N(allnames, count) < 0)
418 419 420
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, allnames, status)) < 0) {
421 422
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
423 424 425 426
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
427
        goto cleanup;
428 429
    }

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

    for (i = 0; i < count && want < nnames; i++) {
        virInterfaceDefPtr def;
        struct netcf_if *iface;

        iface = ncf_lookup_by_name(driver->netcf, allnames[i]);
        if (!iface) {
            const char *errmsg, *details;
            int errcode = ncf_error(driver->netcf, &errmsg, &details);
            if (errcode != NETCF_NOERROR) {
                virReportError(netcf_to_vir_err(errcode),
                               _("couldn't find interface named '%s': %s%s%s"),
                               allnames[i], errmsg,
                               details ? " - " : "", details ? details : "");
                goto cleanup;
            } else {
                /* Ignore the NETCF_NOERROR, as the interface is very likely
                 * deleted by other management apps (e.g. virt-manager).
                 */
                VIR_WARN("couldn't find interface named '%s', might be "
                         "deleted by other process", allnames[i]);
                continue;
            }
        }

        if (!(def = netcfGetMinimalDefForDevice(iface))) {
            ncf_if_free(iface);
            goto cleanup;
        }
        ncf_if_free(iface);

        if (!filter(conn, def)) {
            virInterfaceDefFree(def);
            continue;
        }
        virInterfaceDefFree(def);

        names[want++] = allnames[i];
        allnames[i] = NULL;
    }

    ret = want;

cleanup:
478
    if (allnames && count > 0)
479 480 481 482 483 484 485 486 487 488 489 490 491 492
        for (i = 0; i < count; i++)
            VIR_FREE(allnames[i]);
    VIR_FREE(allnames);
    if (ret < 0) {
        for (i = 0; i < nnames; i++)
            VIR_FREE(names[i]);
    }
    return ret;
}


static int netcfConnectNumOfInterfaces(virConnectPtr conn)
{
    int count;
493
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
494 495 496 497

    if (virConnectNumOfInterfacesEnsureACL(conn) < 0)
        return -1;

498
    virObjectLock(driver);
499 500 501
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_ACTIVE,
                                            virConnectNumOfInterfacesCheckACL);
502
    virObjectUnlock(driver);
503 504 505 506 507
    return count;
}

static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
508
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
509 510 511 512 513
    int count;

    if (virConnectListInterfacesEnsureACL(conn) < 0)
        return -1;

514
    virObjectLock(driver);
515 516 517 518
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
519
    virObjectUnlock(driver);
520 521 522 523
    return count;

}

524
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
525 526
{
    int count;
527
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
528

529 530 531
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

532
    virObjectLock(driver);
533 534 535
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
536
    virObjectUnlock(driver);
537 538 539
    return count;
}

540
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
541
{
542
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
543 544
    int count;

545 546 547
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

548
    virObjectLock(driver);
549 550 551 552
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
553
    virObjectUnlock(driver);
554 555 556 557
    return count;

}

558
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
559
static int
560 561 562
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
563
{
564
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
O
Osier Yang 已提交
565
    int count;
566
    size_t i;
O
Osier Yang 已提交
567 568 569
    struct netcf_if *iface = NULL;
    virInterfacePtr *tmp_iface_objs = NULL;
    virInterfacePtr iface_obj = NULL;
570
    bool active;
O
Osier Yang 已提交
571 572 573 574
    int niface_objs = 0;
    int ret = -1;
    char **names = NULL;

575
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
576

577 578 579
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

580
    virObjectLock(driver);
O
Osier Yang 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

    /* List all interfaces, in case of we might support new filter flags
     * except active|inactive in future.
     */
    count = ncf_num_of_interfaces(driver->netcf, NETCF_IFACE_ACTIVE |
                                  NETCF_IFACE_INACTIVE);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
    }

    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

602
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, names,
                                     NETCF_IFACE_ACTIVE |
                                     NETCF_IFACE_INACTIVE)) < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
    }

617 618
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
619 620

    for (i = 0; i < count; i++) {
621
        virInterfaceDefPtr def;
O
Osier Yang 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
        iface = ncf_lookup_by_name(driver->netcf, names[i]);
        if (!iface) {
            const char *errmsg, *details;
            int errcode = ncf_error(driver->netcf, &errmsg, &details);
            if (errcode != NETCF_NOERROR) {
                virReportError(netcf_to_vir_err(errcode),
                               _("couldn't find interface named '%s': %s%s%s"),
                               names[i], errmsg,
                               details ? " - " : "", details ? details : "");
                goto cleanup;
            } else {
                /* Ignore the NETCF_NOERROR, as the interface is very likely
                 * deleted by other management apps (e.g. virt-manager).
                 */
                VIR_WARN("couldn't find interface named '%s', might be "
                         "deleted by other process", names[i]);
                continue;
            }
        }

642
        if (netcfInterfaceObjIsActive(iface, &active) < 0)
O
Osier Yang 已提交
643 644
            goto cleanup;

645 646 647 648 649 650 651 652 653 654 655 656
        if (!(def = netcfGetMinimalDefForDevice(iface)))
            goto cleanup;

        if (!virConnectListAllInterfacesCheckACL(conn, def)) {
            ncf_if_free(iface);
            iface = NULL;
            virInterfaceDefFree(def);
            continue;
        }
        virInterfaceDefFree(def);

        /* XXX: Filter the result, need to be split once new filter flags
O
Osier Yang 已提交
657 658
         * except active|inactive are supported.
         */
659
        if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
660 661
            !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) && active) ||
              (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) && !active))) {
662
            ncf_if_free(iface);
663
            iface = NULL;
664 665 666 667 668 669 670
            continue;
        }

        if (ifaces) {
            iface_obj = virGetInterface(conn, ncf_if_name(iface),
                                        ncf_if_mac_string(iface));
            tmp_iface_objs[niface_objs++] = iface_obj;
O
Osier Yang 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        }

        ncf_if_free(iface);
        iface = NULL;
    }

    if (tmp_iface_objs) {
        /* trim the array to the final size */
        ignore_value(VIR_REALLOC_N(tmp_iface_objs, niface_objs + 1));
        *ifaces = tmp_iface_objs;
        tmp_iface_objs = NULL;
    }

    ret = niface_objs;

cleanup:
    ncf_if_free(iface);

689
    if (names && count > 0)
O
Osier Yang 已提交
690 691 692 693 694 695 696 697 698
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);

    if (tmp_iface_objs) {
        for (i = 0; i < niface_objs; i++) {
            if (tmp_iface_objs[i])
                virInterfaceFree(tmp_iface_objs[i]);
        }
699
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
700 701
    }

702
    virObjectUnlock(driver);
O
Osier Yang 已提交
703 704 705 706
    return ret;
}


707 708
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
709
{
710
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
711 712
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
713
    virInterfaceDefPtr def = NULL;
714

715
    virObjectLock(driver);
716 717 718 719 720
    iface = ncf_lookup_by_name(driver->netcf, name);
    if (!iface) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
        if (errcode != NETCF_NOERROR) {
721 722 723 724
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
725
        } else {
726 727
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
728 729 730 731
        }
        goto cleanup;
    }

732 733 734 735 736 737
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceLookupByNameEnsureACL(conn, def) < 0)
       goto cleanup;

738 739 740 741
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
742
    virInterfaceDefFree(def);
743
    virObjectUnlock(driver);
744 745 746
    return ret;
}

747 748
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
749
{
750
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
751 752 753
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
754
    virInterfaceDefPtr def = NULL;
755

756
    virObjectLock(driver);
757 758 759 760 761
    niface = ncf_lookup_by_mac_string(driver->netcf, macstr, 1, &iface);

    if (niface < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
762 763 764 765
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
                       details ? details : "");
766 767 768
        goto cleanup;
    }
    if (niface == 0) {
769 770 771
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
772 773 774
        goto cleanup;
    }
    if (niface > 1) {
775 776
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
777 778 779
        goto cleanup;
    }

780 781 782 783 784 785 786

    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceLookupByMACStringEnsureACL(conn, def) < 0)
       goto cleanup;

787 788 789 790
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
791
    virInterfaceDefFree(def);
792
    virObjectUnlock(driver);
793 794 795
    return ret;
}

796 797
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
798
{
799
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
800 801 802 803 804
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;

805 806
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

807
    virObjectLock(driver);
808 809 810 811 812 813 814

    iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
    if (!iface) {
        /* helper already reported error */
        goto cleanup;
    }

815 816 817 818 819
    if ((flags & VIR_INTERFACE_XML_INACTIVE)) {
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
820 821 822
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
823 824 825 826
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
827 828 829
        goto cleanup;
    }

830
    ifacedef = virInterfaceDefParseString(xmlstr);
831 832 833 834 835
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

836 837 838
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

839
    ret = virInterfaceDefFormat(ifacedef);
840 841 842 843 844 845 846 847 848
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
849
    virObjectUnlock(driver);
850 851 852
    return ret;
}

853 854 855
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
856
{
857
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
858 859 860 861 862
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

863 864
    virCheckFlags(0, NULL);

865
    virObjectLock(driver);
866

867
    ifacedef = virInterfaceDefParseString(xml);
868 869 870 871 872
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

873 874 875
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

876
    xmlstr = virInterfaceDefFormat(ifacedef);
877 878 879 880 881 882 883 884 885
    if (!xmlstr) {
        /* error was already reported */
        goto cleanup;
    }

    iface = ncf_define(driver->netcf, xmlstr);
    if (!iface) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
886 887 888 889
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
890 891 892 893 894 895 896 897 898
        goto cleanup;
    }

    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
899
    virObjectUnlock(driver);
900 901 902
    return ret;
}

903 904
static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
{
905
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
906
    struct netcf_if *iface = NULL;
907
    virInterfaceDefPtr def = NULL;
908 909
    int ret = -1;

910
    virObjectLock(driver);
911 912 913 914 915 916 917

    iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
    if (!iface) {
        /* helper already reported error */
        goto cleanup;
    }

918 919 920 921 922 923 924

    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceUndefineEnsureACL(ifinfo->conn, def) < 0)
       goto cleanup;

925 926 927 928
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
929 930 931 932
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
933 934 935 936 937
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
938
    virInterfaceDefFree(def);
939
    virObjectUnlock(driver);
940 941 942
    return ret;
}

943 944
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
945
{
946
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
947
    struct netcf_if *iface = NULL;
948
    virInterfaceDefPtr def = NULL;
949
    int ret = -1;
950
    bool active;
951

952 953
    virCheckFlags(0, -1);

954
    virObjectLock(driver);
955 956 957 958 959 960 961

    iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
    if (!iface) {
        /* helper already reported error */
        goto cleanup;
    }

962 963 964 965 966 967 968

    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceCreateEnsureACL(ifinfo->conn, def) < 0)
       goto cleanup;

969 970 971 972 973 974 975 976 977
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

    if (active) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("interface is already running"));
        goto cleanup;
    }

978 979 980 981
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
982 983 984 985
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
986 987 988 989 990
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
991
    virInterfaceDefFree(def);
992
    virObjectUnlock(driver);
993 994 995
    return ret;
}

996 997
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
998
{
999
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
1000
    struct netcf_if *iface = NULL;
1001
    virInterfaceDefPtr def = NULL;
1002
    int ret = -1;
1003
    bool active;
1004

1005 1006
    virCheckFlags(0, -1);

1007
    virObjectLock(driver);
1008 1009 1010 1011 1012 1013 1014

    iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
    if (!iface) {
        /* helper already reported error */
        goto cleanup;
    }

1015 1016 1017 1018 1019 1020 1021

    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceDestroyEnsureACL(ifinfo->conn, def) < 0)
       goto cleanup;

1022 1023 1024 1025 1026 1027 1028 1029 1030
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

    if (!active) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("interface is not running"));
        goto cleanup;
    }

1031 1032 1033 1034
    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1035 1036 1037 1038
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
1039 1040 1041 1042 1043
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
1044
    virInterfaceDefFree(def);
1045
    virObjectUnlock(driver);
1046 1047 1048
    return ret;
}

1049
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
1050
{
1051
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
1052
    struct netcf_if *iface = NULL;
1053
    virInterfaceDefPtr def = NULL;
1054
    int ret = -1;
1055
    bool active;
1056

1057
    virObjectLock(driver);
1058 1059 1060 1061 1062 1063 1064

    iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
    if (!iface) {
        /* helper already reported error */
        goto cleanup;
    }

1065 1066 1067 1068 1069 1070
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

    if (virInterfaceIsActiveEnsureACL(ifinfo->conn, def) < 0)
       goto cleanup;

1071
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
1072 1073
        goto cleanup;

1074
    ret = active ? 1 : 0;
1075 1076 1077

cleanup:
    ncf_if_free(iface);
1078
    virInterfaceDefFree(def);
1079
    virObjectUnlock(driver);
1080 1081 1082
    return ret;
}

1083
#ifdef HAVE_NETCF_TRANSACTIONS
1084
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1085
{
1086
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1087 1088 1089 1090
    int ret;

    virCheckFlags(0, -1); /* currently flags must be 0 */

1091 1092 1093
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1094
    virObjectLock(driver);
1095 1096 1097 1098 1099

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1100 1101 1102 1103
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1104 1105
    }

1106
    virObjectUnlock(driver);
1107 1108 1109
    return ret;
}

1110
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1111
{
1112
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1113 1114 1115 1116
    int ret;

    virCheckFlags(0, -1); /* currently flags must be 0 */

1117 1118 1119
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1120
    virObjectLock(driver);
1121 1122 1123 1124 1125

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1126 1127 1128 1129
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1130 1131
    }

1132
    virObjectUnlock(driver);
1133 1134 1135
    return ret;
}

1136
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1137
{
1138
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1139 1140 1141 1142
    int ret;

    virCheckFlags(0, -1); /* currently flags must be 0 */

1143 1144 1145
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1146
    virObjectLock(driver);
1147 1148 1149 1150 1151

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1152 1153 1154 1155
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1156 1157
    }

1158
    virObjectUnlock(driver);
1159 1160 1161 1162
    return ret;
}
#endif /* HAVE_NETCF_TRANSACTIONS */

1163
static virInterfaceDriver interfaceDriver = {
1164
    .name = INTERFACE_DRIVER_NAME,
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    .interfaceOpen = netcfInterfaceOpen, /* 0.7.0 */
    .interfaceClose = netcfInterfaceClose, /* 0.7.0 */
    .connectNumOfInterfaces = netcfConnectNumOfInterfaces, /* 0.7.0 */
    .connectListInterfaces = netcfConnectListInterfaces, /* 0.7.0 */
    .connectNumOfDefinedInterfaces = netcfConnectNumOfDefinedInterfaces, /* 0.7.0 */
    .connectListDefinedInterfaces = netcfConnectListDefinedInterfaces, /* 0.7.0 */
    .connectListAllInterfaces = netcfConnectListAllInterfaces, /* 0.10.2 */
    .interfaceLookupByName = netcfInterfaceLookupByName, /* 0.7.0 */
    .interfaceLookupByMACString = netcfInterfaceLookupByMACString, /* 0.7.0 */
    .interfaceGetXMLDesc = netcfInterfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = netcfInterfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = netcfInterfaceUndefine, /* 0.7.0 */
    .interfaceCreate = netcfInterfaceCreate, /* 0.7.0 */
    .interfaceDestroy = netcfInterfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = netcfInterfaceIsActive, /* 0.7.3 */
1180
#ifdef HAVE_NETCF_TRANSACTIONS
1181 1182 1183
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
1184
#endif /* HAVE_NETCF_TRANSACTIONS */
1185 1186
};

1187 1188 1189 1190 1191 1192 1193
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

1194 1195
int netcfIfaceRegister(void)
{
1196 1197 1198 1199 1200
    if (virRegisterInterfaceDriver(&interfaceDriver) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to register netcf interface driver"));
        return -1;
    }
1201 1202
    if (virRegisterStateDriver(&interfaceStateDriver) < 0)
        return -1;
1203 1204
    return 0;
}