interface_backend_netcf.c 24.8 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 119 120
        }
    }
    return iface;
}

static virDrvOpenStatus interfaceOpenInterface(virConnectPtr conn,
                                               virConnectAuthPtr auth ATTRIBUTE_UNUSED,
121
                                               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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

static int interfaceCloseInterface(virConnectPtr conn)
{

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

static int interfaceNumOfInterfaces(virConnectPtr conn)
{
    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 199 200 201 202 203 204 205 206 207 208 209
    }

    interfaceDriverUnlock(driver);
    return count;
}

static int interfaceListInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    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 221 222 223 224 225 226 227 228 229 230
    }

    interfaceDriverUnlock(driver);
    return count;

}

static int interfaceNumOfDefinedInterfaces(virConnectPtr conn)
{
    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 241 242 243 244 245 246 247 248 249 250 251
    }

    interfaceDriverUnlock(driver);
    return count;
}

static int interfaceListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
{
    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;

}

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

    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
                  VIR_CONNECT_LIST_INTERFACES_INACTIVE, -1);

    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 : "");
        ret = -1;
        goto cleanup;
    }

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

    if (VIR_ALLOC_N(names, count) < 0) {
        virReportOOMError();
        ret = -1;
        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.
         */
        if (((status & NETCF_IFACE_ACTIVE) &&
             (flags & VIR_CONNECT_LIST_INTERFACES_ACTIVE)) ||
            ((status & NETCF_IFACE_INACTIVE) &&
             (flags & VIR_CONNECT_LIST_INTERFACES_INACTIVE))) {
            if (ifaces) {
                iface_obj = virGetInterface(conn, ncf_if_name(iface),
                                            ncf_if_mac_string(iface));
                tmp_iface_objs[niface_objs] = iface_obj;
            }
            niface_objs++;
        }

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

    interfaceDriverUnlock(driver);
    return ret;
}


409 410 411 412 413 414 415 416 417 418 419 420 421
static virInterfacePtr interfaceLookupByName(virConnectPtr conn,
                                             const char *name)
{
    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) {
422 423 424 425
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
                           details ? " - " : "", details ? details : "");
426
        } else {
427 428
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
        }
        goto cleanup;
    }

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

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

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

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

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

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

490 491
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

492 493 494 495 496 497 498 499
    interfaceDriverLock(driver);

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

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

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

521
    ret = virInterfaceDefFormat(ifacedef);
522 523 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;
}

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

545 546
    virCheckFlags(0, NULL);

547 548
    interfaceDriverLock(driver);

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

555
    xmlstr = virInterfaceDefFormat(ifacedef);
556 557 558 559 560 561 562 563 564
    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);
565 566 567 568
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
        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;
}

static int interfaceUndefine(virInterfacePtr ifinfo) {
    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);
599 600 601 602
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
603 604 605 606 607 608 609 610 611 612
        goto cleanup;
    }

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

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

619 620
    virCheckFlags(0, -1);

621 622 623 624 625 626 627 628 629 630 631 632
    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);
633 634 635 636
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
637 638 639 640 641 642 643 644 645 646
        goto cleanup;
    }

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

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

653 654
    virCheckFlags(0, -1);

655 656 657 658 659 660 661 662 663 664 665 666
    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);
667 668 669 670
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
671 672 673 674 675 676 677 678 679
        goto cleanup;
    }

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

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static int interfaceIsActive(virInterfacePtr ifinfo)
{
    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);
698 699 700 701
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get status of interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
                       details ? details : "");
702 703 704 705 706 707 708 709 710 711 712
        goto cleanup;
    }

    ret = flags & NETCF_IFACE_ACTIVE ? 1 : 0;

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

713 714 715 716 717 718 719 720 721 722 723 724 725 726
#ifdef HAVE_NETCF_TRANSACTIONS
static int interfaceChangeBegin(virConnectPtr conn, unsigned int flags)
{
    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);
727 728 729 730
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    }

    interfaceDriverUnlock(driver);
    return ret;
}

static int interfaceChangeCommit(virConnectPtr conn, unsigned int flags)
{
    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);
750 751 752 753
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    }

    interfaceDriverUnlock(driver);
    return ret;
}

static int interfaceChangeRollback(virConnectPtr conn, unsigned int flags)
{
    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);
773 774 775 776
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
                       details ? details : "");
777 778 779 780 781 782 783
    }

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

784
static virInterfaceDriver interfaceDriver = {
785
    "netcf",
786 787 788 789 790 791
    .open = interfaceOpenInterface, /* 0.7.0 */
    .close = interfaceCloseInterface, /* 0.7.0 */
    .numOfInterfaces = interfaceNumOfInterfaces, /* 0.7.0 */
    .listInterfaces = interfaceListInterfaces, /* 0.7.0 */
    .numOfDefinedInterfaces = interfaceNumOfDefinedInterfaces, /* 0.7.0 */
    .listDefinedInterfaces = interfaceListDefinedInterfaces, /* 0.7.0 */
O
Osier Yang 已提交
792
    .listAllInterfaces = interfaceListAllInterfaces, /* 0.10.2 */
793 794 795 796 797 798 799 800
    .interfaceLookupByName = interfaceLookupByName, /* 0.7.0 */
    .interfaceLookupByMACString = interfaceLookupByMACString, /* 0.7.0 */
    .interfaceGetXMLDesc = interfaceGetXMLDesc, /* 0.7.0 */
    .interfaceDefineXML = interfaceDefineXML, /* 0.7.0 */
    .interfaceUndefine = interfaceUndefine, /* 0.7.0 */
    .interfaceCreate = interfaceCreate, /* 0.7.0 */
    .interfaceDestroy = interfaceDestroy, /* 0.7.0 */
    .interfaceIsActive = interfaceIsActive, /* 0.7.3 */
801 802 803 804 805
#ifdef HAVE_NETCF_TRANSACTIONS
    .interfaceChangeBegin = interfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = interfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = interfaceChangeRollback, /* 0.9.2 */
#endif /* HAVE_NETCF_TRANSACTIONS */
806 807
};

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