interface_backend_netcf.c 33.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
static int
virNetcfDriverStateOnceInit(void)
{
    if (!(virNetcfDriverStateClass = virClassNew(virClassForObjectLockable(),
                                       "virNetcfDriverState",
                                       sizeof(virNetcfDriverState),
                                       virNetcfDriverStateDispose)))
        return -1;
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetcfDriverState)

66
static virNetcfDriverStatePtr driver;
67 68 69 70


static void
virNetcfDriverStateDispose(void *obj)
71
{
72
    virNetcfDriverStatePtr _driver = obj;
73

74 75
    if (_driver->netcf)
        ncf_close(_driver->netcf);
76 77 78 79 80 81 82 83 84 85 86
}


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

87
    if (!(driver = virObjectLockableNew(virNetcfDriverStateClass)))
88 89 90
        return -1;

    /* open netcf */
91
    if (ncf_init(&driver->netcf, NULL) != 0) {
92 93
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to initialize netcf"));
94 95
        virObjectUnref(driver);
        driver = NULL;
96 97 98
        return -1;
    }
    return 0;
99 100
}

101 102 103

static int
netcfStateCleanup(void)
104
{
105
    if (!driver)
106 107
        return -1;

108
    if (virObjectUnref(driver)) {
109 110 111 112 113
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Attempt to close netcf state driver "
                         "with open connections"));
        return -1;
    }
114
    driver = NULL;
115
    return 0;
116 117
}

118 119 120 121 122 123

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

124
    if (!driver)
125 126
        return 0;

127 128 129
    virObjectLock(driver);
    ncf_close(driver->netcf);
    if (ncf_init(&driver->netcf, NULL) != 0) {
130 131 132 133 134 135 136 137
        /* 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"));
138
        driver->netcf = NULL;
139 140 141 142
        goto cleanup;
    }

    ret = 0;
143
 cleanup:
144
    virObjectUnlock(driver);
145 146 147 148 149

    return ret;
}


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

    /* Allocate our interface definition structure */
161
    if (VIR_ALLOC(def) < 0)
162 163 164 165 166 167 168 169 170 171
        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;

172
 cleanup:
173 174 175 176 177
    virInterfaceDefFree(def);
    return NULL;
}


178 179
static int netcf_to_vir_err(int netcf_errcode)
{
E
Eric Blake 已提交
180
    switch (netcf_errcode) {
181 182 183 184 185 186 187 188 189 190
        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 245 246 247
static int
netcfInterfaceObjIsActive(struct netcf_if *iface,
                          bool *active)
{
    int ret = -1;
    unsigned int flags = 0;

248
    virObjectRef(driver);
249 250
    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
251
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
252 253 254 255 256 257 258 259 260 261
        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;

262
 cleanup:
263
    virObjectUnref(driver);
264 265 266
    return ret;
}

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

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

291 292 293 294 295
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

296
    if (VIR_ALLOC_N(names, count) < 0)
297 298 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
        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;

350
 cleanup:
351
    if (names && count > 0)
352 353 354 355
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);
    return ret;
356 357
}

358 359 360 361 362

static int netcfConnectListInterfacesImpl(virConnectPtr conn,
                                          int status,
                                          char **const names, int nnames,
                                          virInterfaceObjListFilter filter)
363
{
364 365 366
    int count = 0;
    int want = 0;
    int ret = -1;
367
    size_t i;
368
    char **allnames = NULL;
369

370 371 372 373 374 375 376 377 378 379
    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;
    }
380

381 382 383 384
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
385

386
    if (VIR_ALLOC_N(allnames, count) < 0)
387 388 389
        goto cleanup;

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

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

446
 cleanup:
447
    if (allnames && count > 0)
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
        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;

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

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

static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    int count;

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

481
    virObjectLock(driver);
482 483 484 485
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
486
    virObjectUnlock(driver);
487 488 489 490
    return count;

}

491
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
492 493 494
{
    int count;

495 496 497
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

498
    virObjectLock(driver);
499 500 501
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
502
    virObjectUnlock(driver);
503 504 505
    return count;
}

506
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
507 508 509
{
    int count;

510 511 512
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

513
    virObjectLock(driver);
514 515 516 517
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
518
    virObjectUnlock(driver);
519 520 521 522
    return count;

}

523
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
524
static int
525 526 527
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
528 529
{
    int count;
530
    size_t i;
O
Osier Yang 已提交
531 532 533
    struct netcf_if *iface = NULL;
    virInterfacePtr *tmp_iface_objs = NULL;
    virInterfacePtr iface_obj = NULL;
534
    bool active;
O
Osier Yang 已提交
535 536 537 538
    int niface_objs = 0;
    int ret = -1;
    char **names = NULL;

539
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
540

541 542 543
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

544
    virObjectLock(driver);
O
Osier Yang 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

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

566
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
        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;
    }

581 582
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
583 584

    for (i = 0; i < count; i++) {
585
        virInterfaceDefPtr def;
O
Osier Yang 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
        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;
            }
        }

606
        if (netcfInterfaceObjIsActive(iface, &active) < 0)
O
Osier Yang 已提交
607 608
            goto cleanup;

609 610 611 612 613 614 615 616 617 618 619 620
        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 已提交
621 622
         * except active|inactive are supported.
         */
623
        if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) &&
624 625
            !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) && active) ||
              (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) && !active))) {
626
            ncf_if_free(iface);
627
            iface = NULL;
628 629 630 631 632 633 634
            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 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
        }

        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;

650
 cleanup:
O
Osier Yang 已提交
651 652
    ncf_if_free(iface);

653
    if (names && count > 0)
O
Osier Yang 已提交
654 655 656 657 658
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);

    if (tmp_iface_objs) {
659 660
        for (i = 0; i < niface_objs; i++)
            virObjectUnref(tmp_iface_objs[i]);
661
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
662 663
    }

664
    virObjectUnlock(driver);
O
Osier Yang 已提交
665 666 667 668
    return ret;
}


669 670
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
671 672 673
{
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
674
    virInterfaceDefPtr def = NULL;
675

676
    virObjectLock(driver);
677 678 679 680 681
    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) {
682 683 684 685
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
686
        } else {
687 688
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
689 690 691 692
        }
        goto cleanup;
    }

693 694 695 696 697 698
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

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

701
 cleanup:
702
    ncf_if_free(iface);
703
    virInterfaceDefFree(def);
704
    virObjectUnlock(driver);
705 706 707
    return ret;
}

708 709
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
710 711 712 713
{
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
714
    virInterfaceDefPtr def = NULL;
715

716
    virObjectLock(driver);
717 718 719 720 721
    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);
722 723 724 725
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
                       details ? details : "");
726 727 728
        goto cleanup;
    }
    if (niface == 0) {
729 730 731
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
732 733 734
        goto cleanup;
    }
    if (niface > 1) {
735 736
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
737 738 739
        goto cleanup;
    }

740 741 742 743 744 745 746

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

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

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

749
 cleanup:
750
    ncf_if_free(iface);
751
    virInterfaceDefFree(def);
752
    virObjectUnlock(driver);
753 754 755
    return ret;
}

756 757
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
758 759 760 761 762
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;
763
    bool active;
764

765 766
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

767
    virObjectLock(driver);
768 769 770 771 772 773 774

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

775 776 777 778
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
       goto cleanup;

    if ((flags & VIR_INTERFACE_XML_INACTIVE) || !active) {
779 780 781 782
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
783 784 785
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
786 787 788 789
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
790 791 792
        goto cleanup;
    }

793
    ifacedef = virInterfaceDefParseString(xmlstr);
794 795 796 797 798
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

799 800 801
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

802
    ret = virInterfaceDefFormat(ifacedef);
803 804 805 806 807
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

808
 cleanup:
809 810 811
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
812
    virObjectUnlock(driver);
813 814 815
    return ret;
}

816 817 818
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
819 820 821 822 823 824
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

825 826
    virCheckFlags(0, NULL);

827
    virObjectLock(driver);
828

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

835 836 837
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

838
    xmlstr = virInterfaceDefFormat(ifacedef);
839 840 841 842 843 844 845 846 847
    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);
848 849 850 851
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
852 853 854 855 856
        goto cleanup;
    }

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

857
 cleanup:
858 859 860
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
861
    virObjectUnlock(driver);
862 863 864
    return ret;
}

865 866
static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
{
867
    struct netcf_if *iface = NULL;
868
    virInterfaceDefPtr def = NULL;
869 870
    int ret = -1;

871
    virObjectLock(driver);
872 873 874 875 876 877 878

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

879 880 881 882 883 884 885

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

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

886 887 888 889
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
890 891 892 893
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
894 895 896
        goto cleanup;
    }

897
 cleanup:
898
    ncf_if_free(iface);
899
    virInterfaceDefFree(def);
900
    virObjectUnlock(driver);
901 902 903
    return ret;
}

904 905
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
906 907
{
    struct netcf_if *iface = NULL;
908
    virInterfaceDefPtr def = NULL;
909
    int ret = -1;
910
    bool active;
911

912 913
    virCheckFlags(0, -1);

914
    virObjectLock(driver);
915 916 917 918 919 920 921

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

922 923 924 925 926 927 928

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

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

929 930 931 932 933 934 935 936 937
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

938 939 940 941
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
942 943 944 945
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
946 947 948
        goto cleanup;
    }

949
 cleanup:
950
    ncf_if_free(iface);
951
    virInterfaceDefFree(def);
952
    virObjectUnlock(driver);
953 954 955
    return ret;
}

956 957
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
958 959
{
    struct netcf_if *iface = NULL;
960
    virInterfaceDefPtr def = NULL;
961
    int ret = -1;
962
    bool active;
963

964 965
    virCheckFlags(0, -1);

966
    virObjectLock(driver);
967 968 969 970 971 972 973

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

974 975 976 977 978 979 980

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

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

981 982 983 984 985 986 987 988 989
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

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

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

1008
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
1009 1010
{
    struct netcf_if *iface = NULL;
1011
    virInterfaceDefPtr def = NULL;
1012
    int ret = -1;
1013
    bool active;
1014

1015
    virObjectLock(driver);
1016 1017 1018 1019 1020 1021 1022

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

1023 1024 1025 1026 1027 1028
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

1029
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
1030 1031
        goto cleanup;

1032
    ret = active ? 1 : 0;
1033

1034
 cleanup:
1035
    ncf_if_free(iface);
1036
    virInterfaceDefFree(def);
1037
    virObjectUnlock(driver);
1038 1039 1040
    return ret;
}

1041
#ifdef HAVE_NETCF_TRANSACTIONS
1042
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1043 1044 1045 1046 1047
{
    int ret;

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

1048 1049 1050
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1051
    virObjectLock(driver);
1052 1053 1054 1055 1056

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1057 1058 1059 1060
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1061 1062
    }

1063
    virObjectUnlock(driver);
1064 1065 1066
    return ret;
}

1067
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1068 1069 1070 1071 1072
{
    int ret;

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

1073 1074 1075
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1076
    virObjectLock(driver);
1077 1078 1079 1080 1081

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1082 1083 1084 1085
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1086 1087
    }

1088
    virObjectUnlock(driver);
1089 1090 1091
    return ret;
}

1092
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1093 1094 1095 1096 1097
{
    int ret;

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

1098 1099 1100
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1101
    virObjectLock(driver);
1102 1103 1104 1105 1106

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1107 1108 1109 1110
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
1111 1112
    }

1113
    virObjectUnlock(driver);
1114 1115 1116 1117
    return ret;
}
#endif /* HAVE_NETCF_TRANSACTIONS */

1118
static virInterfaceDriver interfaceDriver = {
1119
    .name = INTERFACE_DRIVER_NAME,
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    .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 */
1133
#ifdef HAVE_NETCF_TRANSACTIONS
1134 1135 1136
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
1137
#endif /* HAVE_NETCF_TRANSACTIONS */
1138 1139
};

1140 1141 1142 1143 1144 1145 1146
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

1147 1148
int netcfIfaceRegister(void)
{
1149
    if (virSetSharedInterfaceDriver(&interfaceDriver) < 0)
1150
        return -1;
1151 1152
    if (virRegisterStateDriver(&interfaceStateDriver) < 0)
        return -1;
1153 1154
    return 0;
}