interface_backend_netcf.c 27.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-2012 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

#define VIR_FROM_THIS VIR_FROM_INTERFACE

/* Main driver state */
struct interface_driver
{
    virMutex lock;
    struct netcf *netcf;
};


static void interfaceDriverLock(struct interface_driver *driver)
{
    virMutexLock(&driver->lock);
}

static void interfaceDriverUnlock(struct interface_driver *driver)
{
    virMutexUnlock(&driver->lock);
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * 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 */
    if (VIR_ALLOC(def) < 0) {
        virReportOOMError();
        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;
}


87 88 89 90 91 92 93 94 95 96 97 98 99 100
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:
101 102 103 104
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
105
            return 2;
106 107 108 109 110 111 112 113 114 115 116 117
        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;
118 119 120 121 122
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        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) {
138 139 140 141
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
                           details ? details : "");
142
        } else {
143 144 145
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
146 147 148 149 150
        }
    }
    return iface;
}

151 152 153
static virDrvOpenStatus netcfInterfaceOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           unsigned int flags)
154 155 156
{
    struct interface_driver *driverState;

157 158
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

159 160
    if (VIR_ALLOC(driverState) < 0)
    {
161
        virReportOOMError();
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        goto alloc_error;
    }

    /* initialize non-0 stuff in driverState */
    if (virMutexInit(&driverState->lock) < 0)
    {
        /* what error to report? */
        goto mutex_error;
    }

    /* open netcf */
    if (ncf_init(&driverState->netcf, NULL) != 0)
    {
        /* what error to report? */
        goto netcf_error;
    }

    conn->interfacePrivateData = driverState;
180
    return VIR_DRV_OPEN_SUCCESS;
181 182 183 184 185 186

netcf_error:
    if (driverState->netcf)
    {
        ncf_close(driverState->netcf);
    }
187
    virMutexDestroy(&driverState->lock);
188 189 190
mutex_error:
    VIR_FREE(driverState);
alloc_error:
191
    return VIR_DRV_OPEN_ERROR;
192 193
}

194
static int netcfInterfaceClose(virConnectPtr conn)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{

    if (conn->interfacePrivateData != NULL)
    {
        struct interface_driver *driver = conn->interfacePrivateData;

        /* close netcf instance */
        ncf_close(driver->netcf);
        /* destroy lock */
        virMutexDestroy(&driver->lock);
        /* free driver state */
        VIR_FREE(driver);
    }
    conn->interfacePrivateData = NULL;
    return 0;
}

212
static int netcfConnectNumOfInterfaces(virConnectPtr conn)
213 214 215 216
{
    int count;
    struct interface_driver *driver = conn->interfacePrivateData;

217 218 219
    if (virConnectNumOfInterfacesEnsureACL(conn) < 0)
        return -1;

220 221 222 223 224
    interfaceDriverLock(driver);
    count = ncf_num_of_interfaces(driver->netcf, NETCF_IFACE_ACTIVE);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
225 226 227
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of interfaces on host: %s%s%s"),
                       errmsg, details ? " - " : "", details ? details : "");
228 229 230 231 232 233
    }

    interfaceDriverUnlock(driver);
    return count;
}

234
static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
235 236 237 238
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int count;

239 240 241
    if (virConnectListInterfacesEnsureACL(conn) < 0)
        return -1;

242 243 244 245 246 247
    interfaceDriverLock(driver);

    count = ncf_list_interfaces(driver->netcf, nnames, names, NETCF_IFACE_ACTIVE);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
248 249 250 251
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
252 253 254 255 256 257 258
    }

    interfaceDriverUnlock(driver);
    return count;

}

259
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
260 261 262 263
{
    int count;
    struct interface_driver *driver = conn->interfacePrivateData;

264 265 266
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

267 268 269 270 271
    interfaceDriverLock(driver);
    count = ncf_num_of_interfaces(driver->netcf, NETCF_IFACE_INACTIVE);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
272 273 274 275
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of defined interfaces on host: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
276 277 278 279 280 281
    }

    interfaceDriverUnlock(driver);
    return count;
}

282
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
283 284 285 286
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int count;

287 288 289
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

290 291 292 293 294 295
    interfaceDriverLock(driver);

    count = ncf_list_interfaces(driver->netcf, nnames, names, NETCF_IFACE_INACTIVE);
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
296 297 298 299
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host defined interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
300 301 302 303 304 305 306
    }

    interfaceDriverUnlock(driver);
    return count;

}

307
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
308
static int
309 310 311
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
312 313 314 315 316 317 318 319 320 321 322 323
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int count;
    int i;
    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;

324
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
325

326 327 328
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

O
Osier Yang 已提交
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    interfaceDriverLock(driver);

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

    if (VIR_ALLOC_N(names, count) < 0) {
        virReportOOMError();
        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;
    }

    if (ifaces) {
        if (VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0) {
            virReportOOMError();
            goto cleanup;
        }
    }

    for (i = 0; i < count; i++) {
        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;
        }

        /* XXX: Filter the result, need to be splitted once new filter flags
         * except active|inactive are supported.
         */
409 410 411 412 413 414
        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);
415
            iface = NULL;
416 417 418 419 420 421 422
            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 已提交
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 450
        }

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

    if (names)
        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]);
        }
451
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
452 453 454 455 456 457 458
    }

    interfaceDriverUnlock(driver);
    return ret;
}


459 460
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
461 462 463 464
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
465
    virInterfaceDefPtr def = NULL;
466 467 468 469 470 471 472

    interfaceDriverLock(driver);
    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) {
473 474 475 476
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
477
        } else {
478 479
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
480 481 482 483
        }
        goto cleanup;
    }

484 485 486 487 488 489
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

490 491 492 493
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
494
    virInterfaceDefFree(def);
495 496 497 498
    interfaceDriverUnlock(driver);
    return ret;
}

499 500
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
501 502 503 504 505
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
506
    virInterfaceDefPtr def = NULL;
507 508 509 510 511 512 513

    interfaceDriverLock(driver);
    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);
514 515 516 517
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
                       details ? details : "");
518 519 520
        goto cleanup;
    }
    if (niface == 0) {
521 522 523
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
524 525 526
        goto cleanup;
    }
    if (niface > 1) {
527 528
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
529 530 531
        goto cleanup;
    }

532 533 534 535 536 537 538

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

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

539 540 541 542
    ret = virGetInterface(conn, ncf_if_name(iface), ncf_if_mac_string(iface));

cleanup:
    ncf_if_free(iface);
543
    virInterfaceDefFree(def);
544 545 546 547
    interfaceDriverUnlock(driver);
    return ret;
}

548 549
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
550 551 552 553 554 555 556
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;

557 558
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

559 560 561 562 563 564 565 566
    interfaceDriverLock(driver);

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

567 568 569 570 571
    if ((flags & VIR_INTERFACE_XML_INACTIVE)) {
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
572 573 574
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
575 576 577 578
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
579 580 581
        goto cleanup;
    }

582
    ifacedef = virInterfaceDefParseString(xmlstr);
583 584 585 586 587
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

588 589 590
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

591
    ret = virInterfaceDefFormat(ifacedef);
592 593 594 595 596 597 598 599 600 601 602 603 604
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
    interfaceDriverUnlock(driver);
    return ret;
}

605 606 607
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
608 609 610 611 612 613 614
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

615 616
    virCheckFlags(0, NULL);

617 618
    interfaceDriverLock(driver);

619
    ifacedef = virInterfaceDefParseString(xml);
620 621 622 623 624
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

625 626 627
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

628
    xmlstr = virInterfaceDefFormat(ifacedef);
629 630 631 632 633 634 635 636 637
    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);
638 639 640 641
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
642 643 644 645 646 647 648 649 650 651 652 653 654
        goto cleanup;
    }

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

cleanup:
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
    interfaceDriverUnlock(driver);
    return ret;
}

655
static int netcfInterfaceUndefine(virInterfacePtr ifinfo) {
656 657
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
658
    virInterfaceDefPtr def = NULL;
659 660 661 662 663 664 665 666 667 668
    int ret = -1;

    interfaceDriverLock(driver);

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

669 670 671 672 673 674 675

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

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

676 677 678 679
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
680 681 682 683
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
684 685 686 687 688
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
689
    virInterfaceDefFree(def);
690 691 692 693
    interfaceDriverUnlock(driver);
    return ret;
}

694 695
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
696 697 698
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
699
    virInterfaceDefPtr def = NULL;
700 701
    int ret = -1;

702 703
    virCheckFlags(0, -1);

704 705 706 707 708 709 710 711
    interfaceDriverLock(driver);

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

712 713 714 715 716 717 718

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

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

719 720 721 722
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
723 724 725 726
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
727 728 729 730 731
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
732
    virInterfaceDefFree(def);
733 734 735 736
    interfaceDriverUnlock(driver);
    return ret;
}

737 738
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
739 740 741
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
742
    virInterfaceDefPtr def = NULL;
743 744
    int ret = -1;

745 746
    virCheckFlags(0, -1);

747 748 749 750 751 752 753 754
    interfaceDriverLock(driver);

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

755 756 757 758 759 760 761

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

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

762 763 764 765
    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
766 767 768 769
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
770 771 772 773 774
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
775
    virInterfaceDefFree(def);
776 777 778 779
    interfaceDriverUnlock(driver);
    return ret;
}

780
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
781 782 783 784
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    unsigned int flags = 0;
785
    virInterfaceDefPtr def = NULL;
786 787 788 789 790 791 792 793 794 795
    int ret = -1;

    interfaceDriverLock(driver);

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

796 797 798 799 800 801 802

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

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

803 804 805
    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
806 807 808 809
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get status of interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
810 811 812 813 814 815 816
        goto cleanup;
    }

    ret = flags & NETCF_IFACE_ACTIVE ? 1 : 0;

cleanup:
    ncf_if_free(iface);
817
    virInterfaceDefFree(def);
818 819 820 821
    interfaceDriverUnlock(driver);
    return ret;
}

822
#ifdef HAVE_NETCF_TRANSACTIONS
823
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
824 825 826 827 828 829
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

830 831 832
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

833 834 835 836 837 838
    interfaceDriverLock(driver);

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
839 840 841 842
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
843 844 845 846 847 848
    }

    interfaceDriverUnlock(driver);
    return ret;
}

849
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
850 851 852 853 854 855
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

856 857 858
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

859 860 861 862 863 864
    interfaceDriverLock(driver);

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
865 866 867 868
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
869 870 871 872 873 874
    }

    interfaceDriverUnlock(driver);
    return ret;
}

875
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
876 877 878 879 880 881
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

882 883 884
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

885 886 887 888 889 890
    interfaceDriverLock(driver);

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
891 892 893 894
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
895 896 897 898 899 900 901
    }

    interfaceDriverUnlock(driver);
    return ret;
}
#endif /* HAVE_NETCF_TRANSACTIONS */

902
static virInterfaceDriver interfaceDriver = {
903
    "netcf",
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    .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 */
919
#ifdef HAVE_NETCF_TRANSACTIONS
920 921 922
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
923
#endif /* HAVE_NETCF_TRANSACTIONS */
924 925
};

926
int netcfIfaceRegister(void) {
927 928 929 930 931
    if (virRegisterInterfaceDriver(&interfaceDriver) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to register netcf interface driver"));
        return -1;
    }
932 933
    return 0;
}