interface_backend_netcf.c 25.1 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

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

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:
69 70 71 72
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
73
            return 2;
74 75 76 77 78 79 80 81 82 83 84 85
        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;
86 87 88 89 90
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        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) {
106 107 108 109
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
                           details ? details : "");
110
        } else {
111 112 113
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
114 115 116 117 118
        }
    }
    return iface;
}

119 120 121
static virDrvOpenStatus netcfInterfaceOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           unsigned int flags)
122 123 124
{
    struct interface_driver *driverState;

125 126
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

127 128
    if (VIR_ALLOC(driverState) < 0)
    {
129
        virReportOOMError();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        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;
148
    return VIR_DRV_OPEN_SUCCESS;
149 150 151 152 153 154

netcf_error:
    if (driverState->netcf)
    {
        ncf_close(driverState->netcf);
    }
155
    virMutexDestroy(&driverState->lock);
156 157 158
mutex_error:
    VIR_FREE(driverState);
alloc_error:
159
    return VIR_DRV_OPEN_ERROR;
160 161
}

162
static int netcfInterfaceClose(virConnectPtr conn)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
{

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

180
static int netcfConnectNumOfInterfaces(virConnectPtr conn)
181 182 183 184 185 186 187 188 189
{
    int count;
    struct interface_driver *driver = conn->interfacePrivateData;

    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);
190 191 192
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of interfaces on host: %s%s%s"),
                       errmsg, details ? " - " : "", details ? details : "");
193 194 195 196 197 198
    }

    interfaceDriverUnlock(driver);
    return count;
}

199
static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
200 201 202 203 204 205 206 207 208 209
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int count;

    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);
210 211 212 213
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
214 215 216 217 218 219 220
    }

    interfaceDriverUnlock(driver);
    return count;

}

221
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
222 223 224 225 226 227 228 229 230
{
    int count;
    struct interface_driver *driver = conn->interfacePrivateData;

    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);
231 232 233 234
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of defined interfaces on host: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
235 236 237 238 239 240
    }

    interfaceDriverUnlock(driver);
    return count;
}

241
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
242 243 244 245 246 247 248 249 250 251
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int count;

    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);
252 253 254 255
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host defined interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
256 257 258 259 260 261 262
    }

    interfaceDriverUnlock(driver);
    return count;

}

263
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
264
static int
265 266 267
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
268 269 270 271 272 273 274 275 276 277 278 279
{
    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;

280
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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 350 351 352 353 354 355 356 357 358 359 360 361

    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.
         */
362 363 364 365 366 367 368 369 370 371 372 373 374
        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);
            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 已提交
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
        }

        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]);
        }
403
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
404 405 406 407 408 409 410
    }

    interfaceDriverUnlock(driver);
    return ret;
}


411 412
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
413 414 415 416 417 418 419 420 421 422 423
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;

    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) {
424 425 426 427
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
428
        } else {
429 430
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
431 432 433 434 435 436 437 438 439 440 441 442
        }
        goto cleanup;
    }

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

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

443 444
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
445 446 447 448 449 450 451 452 453 454 455 456
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;

    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);
457 458 459 460
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
                       details ? details : "");
461 462 463
        goto cleanup;
    }
    if (niface == 0) {
464 465 466
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
467 468 469
        goto cleanup;
    }
    if (niface > 1) {
470 471
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
472 473 474 475 476 477 478 479 480 481 482
        goto cleanup;
    }

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

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

483 484
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
485 486 487 488 489 490 491
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;

492 493
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

494 495 496 497 498 499 500 501
    interfaceDriverLock(driver);

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

502 503 504 505 506
    if ((flags & VIR_INTERFACE_XML_INACTIVE)) {
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
507 508 509
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
510 511 512 513
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
514 515 516
        goto cleanup;
    }

517
    ifacedef = virInterfaceDefParseString(xmlstr);
518 519 520 521 522
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

523
    ret = virInterfaceDefFormat(ifacedef);
524 525 526 527 528 529 530 531 532 533 534 535 536
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

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

537 538 539
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
540 541 542 543 544 545 546
{
    struct interface_driver *driver = conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

547 548
    virCheckFlags(0, NULL);

549 550
    interfaceDriverLock(driver);

551
    ifacedef = virInterfaceDefParseString(xml);
552 553 554 555 556
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

557
    xmlstr = virInterfaceDefFormat(ifacedef);
558 559 560 561 562 563 564 565 566
    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);
567 568 569 570
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
571 572 573 574 575 576 577 578 579 580 581 582 583
        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;
}

584
static int netcfInterfaceUndefine(virInterfacePtr ifinfo) {
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    int ret = -1;

    interfaceDriverLock(driver);

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

    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
601 602 603 604
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
605 606 607 608 609 610 611 612 613
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

614 615
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
616 617 618 619 620
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    int ret = -1;

621 622
    virCheckFlags(0, -1);

623 624 625 626 627 628 629 630 631 632 633 634
    interfaceDriverLock(driver);

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

    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
635 636 637 638
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
639 640 641 642 643 644 645 646 647
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

648 649
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
650 651 652 653 654
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    int ret = -1;

655 656
    virCheckFlags(0, -1);

657 658 659 660 661 662 663 664 665 666 667 668
    interfaceDriverLock(driver);

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

    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
669 670 671 672
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
673 674 675 676 677 678 679 680 681
        goto cleanup;
    }

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

682
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
{
    struct interface_driver *driver = ifinfo->conn->interfacePrivateData;
    struct netcf_if *iface = NULL;
    unsigned int flags = 0;
    int ret = -1;

    interfaceDriverLock(driver);

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

    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
700 701 702 703
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get status of interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
704 705 706 707 708 709 710 711 712 713 714
        goto cleanup;
    }

    ret = flags & NETCF_IFACE_ACTIVE ? 1 : 0;

cleanup:
    ncf_if_free(iface);
    interfaceDriverUnlock(driver);
    return ret;
}

715
#ifdef HAVE_NETCF_TRANSACTIONS
716
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
717 718 719 720 721 722 723 724 725 726 727 728
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

    interfaceDriverLock(driver);

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
729 730 731 732
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
733 734 735 736 737 738
    }

    interfaceDriverUnlock(driver);
    return ret;
}

739
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
740 741 742 743 744 745 746 747 748 749 750 751
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

    interfaceDriverLock(driver);

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
752 753 754 755
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
756 757 758 759 760 761
    }

    interfaceDriverUnlock(driver);
    return ret;
}

762
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
763 764 765 766 767 768 769 770 771 772 773 774
{
    struct interface_driver *driver = conn->interfacePrivateData;
    int ret;

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

    interfaceDriverLock(driver);

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
775 776 777 778
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
779 780 781 782 783 784 785
    }

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

786
static virInterfaceDriver interfaceDriver = {
787
    "netcf",
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    .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 */
803
#ifdef HAVE_NETCF_TRANSACTIONS
804 805 806
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
807
#endif /* HAVE_NETCF_TRANSACTIONS */
808 809
};

810
int netcfIfaceRegister(void) {
811 812 813 814 815
    if (virRegisterInterfaceDriver(&interfaceDriver) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to register netcf interface driver"));
        return -1;
    }
816 817
    return 0;
}