interface_backend_netcf.c 34.9 KB
Newer Older
1 2 3 4
/*
 * interface_driver.c: backend driver methods to handle physical
 *                     interface configuration using the netcf library.
 *
5
 * Copyright (C) 2006-2013 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
#define INTERFACE_DRIVER_NAME "netcf"

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

48 49
static virClassPtr virNetcfDriverStateClass;
static void virNetcfDriverStateDispose(void *obj);
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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)
69
{
70 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
    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;
97 98
}

99 100 101

static int
netcfStateCleanup(void)
102
{
103
    if (!driverState)
104 105 106 107 108 109 110 111 112 113
        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;
114 115
}

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

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;
}


149 150 151 152 153 154 155 156 157 158 159
/*
 * 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 */
160
    if (VIR_ALLOC(def) < 0)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        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;
}


177 178 179 180 181 182 183 184 185 186 187 188 189 190
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:
191 192 193 194
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
195
            return 2;
196 197 198 199 200 201 202 203 204 205 206 207
        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;
208 209 210 211 212
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        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) {
228 229 230 231
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
                           details ? details : "");
232
        } else {
233 234 235
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
236 237 238 239 240
        }
    }
    return iface;
}

241 242 243 244
static virDrvOpenStatus
netcfInterfaceOpen(virConnectPtr conn,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                   unsigned int flags)
245
{
246 247
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

248 249
    if (!driverState)
        return VIR_DRV_OPEN_ERROR;
250

251
    virObjectRef(driverState);
252
    conn->interfacePrivateData = driverState;
253
    return VIR_DRV_OPEN_SUCCESS;
254 255
}

256 257
static int
netcfInterfaceClose(virConnectPtr conn)
258 259 260 261
{

    if (conn->interfacePrivateData != NULL)
    {
262 263
        virObjectUnref(conn->interfacePrivateData);
        conn->interfacePrivateData = NULL;
264 265 266 267
    }
    return 0;
}

268 269 270
static int netcfConnectNumOfInterfacesImpl(virConnectPtr conn,
                                           int status,
                                           virInterfaceObjListFilter filter)
271
{
272
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
273 274 275
    int count;
    int want = 0;
    int ret = -1;
276
    size_t i;
277
    char **names = NULL;
278

279 280 281 282
    /* List all interfaces, in case we might support new filter flags
     * beyond active|inactive in future.
     */
    count = ncf_num_of_interfaces(driver->netcf, status);
283 284 285
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
286
        virReportError(netcf_to_vir_err(errcode),
287 288 289 290
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
        goto cleanup;
291 292
    }

293 294 295 296 297
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

298
    if (VIR_ALLOC_N(names, count) < 0)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 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
        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:
353
    if (names && count > 0)
354 355 356 357
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);
    return ret;
358 359
}

360 361 362 363 364

static int netcfConnectListInterfacesImpl(virConnectPtr conn,
                                          int status,
                                          char **const names, int nnames,
                                          virInterfaceObjListFilter filter)
365
{
366
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
367 368 369
    int count = 0;
    int want = 0;
    int ret = -1;
370
    size_t i;
371
    char **allnames = NULL;
372

373 374 375 376 377 378 379 380 381 382
    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;
    }
383

384 385 386 387
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
388

389
    if (VIR_ALLOC_N(allnames, count) < 0)
390 391 392
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, allnames, status)) < 0) {
393 394
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
395 396 397 398
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
399
        goto cleanup;
400 401
    }

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    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:
450
    if (allnames && count > 0)
451 452 453 454 455 456 457 458 459 460 461 462 463 464
        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;
465
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
466 467 468 469

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

470
    virObjectLock(driver);
471 472 473
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_ACTIVE,
                                            virConnectNumOfInterfacesCheckACL);
474
    virObjectUnlock(driver);
475 476 477 478 479
    return count;
}

static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
480
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
481 482 483 484 485
    int count;

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

486
    virObjectLock(driver);
487 488 489 490
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
491
    virObjectUnlock(driver);
492 493 494 495
    return count;

}

496
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
497 498
{
    int count;
499
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
500

501 502 503
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

504
    virObjectLock(driver);
505 506 507
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
508
    virObjectUnlock(driver);
509 510 511
    return count;
}

512
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
513
{
514
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
515 516
    int count;

517 518 519
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

520
    virObjectLock(driver);
521 522 523 524
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
525
    virObjectUnlock(driver);
526 527 528 529
    return count;

}

530
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
531
static int
532 533 534
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
535
{
536
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
O
Osier Yang 已提交
537
    int count;
538
    size_t i;
O
Osier Yang 已提交
539 540 541 542 543 544 545 546
    struct netcf_if *iface = NULL;
    virInterfacePtr *tmp_iface_objs = NULL;
    virInterfacePtr iface_obj = NULL;
    unsigned int status;
    int niface_objs = 0;
    int ret = -1;
    char **names = NULL;

547
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
548

549 550 551
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

552
    virObjectLock(driver);
O
Osier Yang 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

    /* 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;
    }

574
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588
        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;
    }

589 590
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
591 592

    for (i = 0; i < count; i++) {
593
        virInterfaceDefPtr def;
O
Osier Yang 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        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 (ncf_if_status(iface, &status) < 0) {
            const char *errmsg, *details;
            int errcode = ncf_error(driver->netcf, &errmsg, &details);
            virReportError(netcf_to_vir_err(errcode),
                           _("failed to get status of interface %s: %s%s%s"),
                           names[i], errmsg, details ? " - " : "",
                           details ? details : "");
            goto cleanup;
        }

624 625 626 627 628 629 630 631 632 633 634 635
        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 已提交
636 637
         * except active|inactive are supported.
         */
638 639 640 641 642 643
        if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
            !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) &&
               (status & NETCF_IFACE_ACTIVE)) ||
              (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) &&
               (status & NETCF_IFACE_INACTIVE)))) {
            ncf_if_free(iface);
644
            iface = NULL;
645 646 647 648 649 650 651
            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 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
        }

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

670
    if (names && count > 0)
O
Osier Yang 已提交
671 672 673 674 675 676 677 678 679
        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]);
        }
680
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
681 682
    }

683
    virObjectUnlock(driver);
O
Osier Yang 已提交
684 685 686 687
    return ret;
}


688 689
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
690
{
691
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
692 693
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
694
    virInterfaceDefPtr def = NULL;
695

696
    virObjectLock(driver);
697 698 699 700 701
    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) {
702 703 704 705
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
706
        } else {
707 708
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
709 710 711 712
        }
        goto cleanup;
    }

713 714 715 716 717 718
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

719 720 721 722
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
723
    virInterfaceDefFree(def);
724
    virObjectUnlock(driver);
725 726 727
    return ret;
}

728 729
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
730
{
731
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
732 733 734
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
735
    virInterfaceDefPtr def = NULL;
736

737
    virObjectLock(driver);
738 739 740 741 742
    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);
743 744 745 746
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
                       details ? details : "");
747 748 749
        goto cleanup;
    }
    if (niface == 0) {
750 751 752
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
753 754 755
        goto cleanup;
    }
    if (niface > 1) {
756 757
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
758 759 760
        goto cleanup;
    }

761 762 763 764 765 766 767

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

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

768 769 770 771
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
772
    virInterfaceDefFree(def);
773
    virObjectUnlock(driver);
774 775 776
    return ret;
}

777 778
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
779
{
780
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
781 782 783 784 785
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;

786 787
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

788
    virObjectLock(driver);
789 790 791 792 793 794 795

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

796 797 798 799 800
    if ((flags & VIR_INTERFACE_XML_INACTIVE)) {
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
801 802 803
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
804 805 806 807
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
808 809 810
        goto cleanup;
    }

811
    ifacedef = virInterfaceDefParseString(xmlstr);
812 813 814 815 816
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

817 818 819
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

820
    ret = virInterfaceDefFormat(ifacedef);
821 822 823 824 825 826 827 828 829
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
830
    virObjectUnlock(driver);
831 832 833
    return ret;
}

834 835 836
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
837
{
838
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
839 840 841 842 843
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

844 845
    virCheckFlags(0, NULL);

846
    virObjectLock(driver);
847

848
    ifacedef = virInterfaceDefParseString(xml);
849 850 851 852 853
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

854 855 856
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

857
    xmlstr = virInterfaceDefFormat(ifacedef);
858 859 860 861 862 863 864 865 866
    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);
867 868 869 870
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
871 872 873 874 875 876 877 878 879
        goto cleanup;
    }

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

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
880
    virObjectUnlock(driver);
881 882 883
    return ret;
}

884
static int netcfInterfaceUndefine(virInterfacePtr ifinfo) {
885
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
886
    struct netcf_if *iface = NULL;
887
    virInterfaceDefPtr def = NULL;
888 889
    int ret = -1;

890
    virObjectLock(driver);
891 892 893 894 895 896 897

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

898 899 900 901 902 903 904

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

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

905 906 907 908
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
909 910 911 912
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
913 914 915 916 917
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
918
    virInterfaceDefFree(def);
919
    virObjectUnlock(driver);
920 921 922
    return ret;
}

923 924
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
925
{
926
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
927
    struct netcf_if *iface = NULL;
928
    virInterfaceDefPtr def = NULL;
929 930
    int ret = -1;

931 932
    virCheckFlags(0, -1);

933
    virObjectLock(driver);
934 935 936 937 938 939 940

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

941 942 943 944 945 946 947

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

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

948 949 950 951
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
952 953 954 955
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
956 957 958 959 960
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
961
    virInterfaceDefFree(def);
962
    virObjectUnlock(driver);
963 964 965
    return ret;
}

966 967
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
968
{
969
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
970
    struct netcf_if *iface = NULL;
971
    virInterfaceDefPtr def = NULL;
972 973
    int ret = -1;

974 975
    virCheckFlags(0, -1);

976
    virObjectLock(driver);
977 978 979 980 981 982 983

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

984 985 986 987 988 989 990

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

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

991 992 993 994
    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
995 996 997 998
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
999 1000 1001 1002 1003
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
1004
    virInterfaceDefFree(def);
1005
    virObjectUnlock(driver);
1006 1007 1008
    return ret;
}

1009
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
1010
{
1011
    virNetcfDriverStatePtr driver = ifinfo->conn->interfacePrivateData;
1012 1013
    struct netcf_if *iface = NULL;
    unsigned int flags = 0;
1014
    virInterfaceDefPtr def = NULL;
1015 1016
    int ret = -1;

1017
    virObjectLock(driver);
1018 1019 1020 1021 1022 1023 1024

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

1025 1026 1027 1028 1029 1030 1031

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

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

1032 1033 1034
    if (ncf_if_status(iface, &flags) < 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 get status of interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
1039 1040 1041 1042 1043 1044 1045
        goto cleanup;
    }

    ret = flags & NETCF_IFACE_ACTIVE ? 1 : 0;

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

1051
#ifdef HAVE_NETCF_TRANSACTIONS
1052
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1053
{
1054
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1055 1056 1057 1058
    int ret;

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

1059 1060 1061
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1062
    virObjectLock(driver);
1063 1064 1065 1066 1067

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1068 1069 1070 1071
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1072 1073
    }

1074
    virObjectUnlock(driver);
1075 1076 1077
    return ret;
}

1078
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1079
{
1080
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1081 1082 1083 1084
    int ret;

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

1085 1086 1087
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1088
    virObjectLock(driver);
1089 1090 1091 1092 1093

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1094 1095 1096 1097
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1098 1099
    }

1100
    virObjectUnlock(driver);
1101 1102 1103
    return ret;
}

1104
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1105
{
1106
    virNetcfDriverStatePtr driver = conn->interfacePrivateData;
1107 1108 1109 1110
    int ret;

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

1111 1112 1113
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1114
    virObjectLock(driver);
1115 1116 1117 1118 1119

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1120 1121 1122 1123
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1124 1125
    }

1126
    virObjectUnlock(driver);
1127 1128 1129 1130
    return ret;
}
#endif /* HAVE_NETCF_TRANSACTIONS */

1131
static virInterfaceDriver interfaceDriver = {
1132
    .name = INTERFACE_DRIVER_NAME,
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
    .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 */
1148
#ifdef HAVE_NETCF_TRANSACTIONS
1149 1150 1151
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
1152
#endif /* HAVE_NETCF_TRANSACTIONS */
1153 1154
};

1155 1156 1157 1158 1159 1160 1161
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

1162
int netcfIfaceRegister(void) {
1163 1164 1165 1166 1167
    if (virRegisterInterfaceDriver(&interfaceDriver) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to register netcf interface driver"));
        return -1;
    }
1168
    virRegisterStateDriver(&interfaceStateDriver);
1169 1170
    return 0;
}