interface_backend_netcf.c 35.1 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-2015 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
 */

#include <config.h>

#include <netcf.h>

26
#include "virerror.h"
27
#include "datatypes.h"
28
#include "interface_driver.h"
29
#include "interface_conf.h"
30
#include "viralloc.h"
31
#include "virlog.h"
32 33
#include "virstring.h"
#include "viraccessapicheck.h"
J
John Ferlan 已提交
34
#include "virinterfaceobj.h"
35 36 37

#define VIR_FROM_THIS VIR_FROM_INTERFACE

38 39
VIR_LOG_INIT("interface.interface_backend_netcf");

40 41
#define INTERFACE_DRIVER_NAME "netcf"

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

50 51
static virClassPtr virNetcfDriverStateClass;
static void virNetcfDriverStateDispose(void *obj);
52

53 54 55
static int
virNetcfDriverStateOnceInit(void)
{
56
    if (!VIR_CLASS_NEW(virNetcfDriverState, virClassForObjectLockable()))
57
        return -1;
58

59 60 61
    return 0;
}

62
VIR_ONCE_GLOBAL_INIT(virNetcfDriverState);
63

64
static virNetcfDriverStatePtr driver;
65 66 67 68


static void
virNetcfDriverStateDispose(void *obj)
69
{
70
    virNetcfDriverStatePtr _driver = obj;
71

72 73
    if (_driver->netcf)
        ncf_close(_driver->netcf);
74 75 76 77
}


static int
78
netcfStateInitialize(bool privileged,
79 80 81 82 83 84
                     virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                     void *opaque ATTRIBUTE_UNUSED)
{
    if (virNetcfDriverStateInitialize() < 0)
        return -1;

85
    if (!(driver = virObjectLockableNew(virNetcfDriverStateClass)))
86 87
        return -1;

88 89
    driver->privileged = privileged;

90
    /* 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 153 154 155 156 157
static virDrvOpenStatus
netcfConnectOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 virConfPtr conf ATTRIBUTE_UNUSED,
                 unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

158 159 160 161 162 163 164 165 166 167 168
    if (driver == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("interface state driver is not active"));
        return VIR_DRV_OPEN_ERROR;
    }

    if (driver->privileged) {
        if (STRNEQ(conn->uri->path, "/system")) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected interface URI path '%s', try interface:///system"),
                           conn->uri->path);
169 170
            return VIR_DRV_OPEN_ERROR;
        }
171 172 173 174 175 176
    } else {
        if (STRNEQ(conn->uri->path, "/session")) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected interface URI path '%s', try interface:///session"),
                           conn->uri->path);
            return VIR_DRV_OPEN_ERROR;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        }
    }

    if (virConnectOpenEnsureACL(conn) < 0)
        return VIR_DRV_OPEN_ERROR;

    return VIR_DRV_OPEN_SUCCESS;
}

static int netcfConnectClose(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 0;
}


static int netcfConnectIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Trivially secure, since always inside the daemon */
    return 1;
}


static int netcfConnectIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Not encrypted, but remote driver takes care of that */
    return 0;
}


static int netcfConnectIsAlive(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    return 1;
}


212 213 214
/*
 * Get a minimal virInterfaceDef containing enough metadata
 * for access control checks to be performed. Currently
N
Nehal J Wani 已提交
215
 * this implies existence of name and mac address attributes
216 217 218 219 220 221 222
 */
static virInterfaceDef * ATTRIBUTE_NONNULL(1)
netcfGetMinimalDefForDevice(struct netcf_if *iface)
{
    virInterfaceDef *def;

    /* Allocate our interface definition structure */
223
    if (VIR_ALLOC(def) < 0)
224 225 226 227 228 229 230 231 232 233
        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;

234
 cleanup:
235 236 237 238 239
    virInterfaceDefFree(def);
    return NULL;
}


240 241
static int netcf_to_vir_err(int netcf_errcode)
{
E
Eric Blake 已提交
242
    switch (netcf_errcode) {
243 244 245 246 247 248 249 250 251 252
        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:
253 254 255 256
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
257
            return 2;
258 259 260 261 262 263 264 265 266 267 268 269
        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;
270 271 272 273 274
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        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) {
290 291 292
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
293
                           NULLSTR_EMPTY(details));
294
        } else {
295 296 297
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
298 299 300 301 302
        }
    }
    return iface;
}

303 304 305 306 307 308 309
static int
netcfInterfaceObjIsActive(struct netcf_if *iface,
                          bool *active)
{
    int ret = -1;
    unsigned int flags = 0;

310
    virObjectRef(driver);
311 312
    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
313
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
314 315 316
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get status of interface %s: %s%s%s"),
                       ncf_if_name(iface), errmsg, details ? " - " : "",
J
Ján Tomko 已提交
317
                       NULLSTR_EMPTY(details));
318 319 320 321 322 323
        goto cleanup;
    }

    *active = flags & NETCF_IFACE_ACTIVE;
    ret = 0;

324
 cleanup:
325
    virObjectUnref(driver);
326 327 328
    return ret;
}

329 330 331
static int netcfConnectNumOfInterfacesImpl(virConnectPtr conn,
                                           int status,
                                           virInterfaceObjListFilter filter)
332
{
333 334 335
    int count;
    int want = 0;
    int ret = -1;
336
    size_t i;
337
    char **names = NULL;
338

339 340 341 342
    /* List all interfaces, in case we might support new filter flags
     * beyond active|inactive in future.
     */
    count = ncf_num_of_interfaces(driver->netcf, status);
343 344 345
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
346
        virReportError(netcf_to_vir_err(errcode),
347 348
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
349
                       NULLSTR_EMPTY(details));
350
        goto cleanup;
351 352
    }

353 354 355 356 357
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

358
    if (VIR_ALLOC_N(names, count) < 0)
359 360 361 362 363 364 365 366
        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 ? " - " : "",
J
Ján Tomko 已提交
367
                       NULLSTR_EMPTY(details));
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        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,
J
Ján Tomko 已提交
383
                               details ? " - " : "", NULLSTR_EMPTY(details));
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 409 410 411
                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;

412
 cleanup:
413
    if (names && count > 0)
414 415 416 417
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);
    return ret;
418 419
}

420 421 422 423 424

static int netcfConnectListInterfacesImpl(virConnectPtr conn,
                                          int status,
                                          char **const names, int nnames,
                                          virInterfaceObjListFilter filter)
425
{
426 427 428
    int count = 0;
    int want = 0;
    int ret = -1;
429
    size_t i;
430
    char **allnames = NULL;
431

432 433 434 435 436 437 438
    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 ? " - " : "",
J
Ján Tomko 已提交
439
                       NULLSTR_EMPTY(details));
440 441
        goto cleanup;
    }
442

443 444 445 446
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
447

448
    if (VIR_ALLOC_N(allnames, count) < 0)
449 450 451
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, allnames, status)) < 0) {
452 453
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
454 455 456
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
457
                       NULLSTR_EMPTY(details));
458
        goto cleanup;
459 460
    }

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    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,
J
Ján Tomko 已提交
478
                               details ? " - " : "", NULLSTR_EMPTY(details));
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                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;

508
 cleanup:
509
    if (allnames && count > 0)
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        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;

528
    virObjectLock(driver);
529 530 531
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_ACTIVE,
                                            virConnectNumOfInterfacesCheckACL);
532
    virObjectUnlock(driver);
533 534 535 536 537 538 539 540 541 542
    return count;
}

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

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

543
    virObjectLock(driver);
544 545 546 547
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
548
    virObjectUnlock(driver);
549 550 551 552
    return count;

}

553
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
554 555 556
{
    int count;

557 558 559
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

560
    virObjectLock(driver);
561 562 563
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
564
    virObjectUnlock(driver);
565 566 567
    return count;
}

568
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
569 570 571
{
    int count;

572 573 574
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

575
    virObjectLock(driver);
576 577 578 579
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
580
    virObjectUnlock(driver);
581 582 583 584
    return count;

}

585
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
586
static int
587 588 589
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
590 591
{
    int count;
592
    size_t i;
593
    unsigned int ncf_flags = 0;
O
Osier Yang 已提交
594 595 596 597 598 599 600
    struct netcf_if *iface = NULL;
    virInterfacePtr *tmp_iface_objs = NULL;
    virInterfacePtr iface_obj = NULL;
    int niface_objs = 0;
    int ret = -1;
    char **names = NULL;

601
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
602

603 604 605
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

606
    virObjectLock(driver);
O
Osier Yang 已提交
607

608 609 610 611 612 613 614 615 616 617 618
    /* let netcf pre-filter for this flag to save time */
    if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE)) {
        if (MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE))
            ncf_flags |= NETCF_IFACE_ACTIVE;
        if (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE))
            ncf_flags |= NETCF_IFACE_INACTIVE;
    } else {
        ncf_flags = NETCF_IFACE_ACTIVE | NETCF_IFACE_INACTIVE;
    }

    if ((count = ncf_num_of_interfaces(driver->netcf, ncf_flags)) < 0) {
O
Osier Yang 已提交
619 620
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
621

O
Osier Yang 已提交
622 623 624
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
625
                       NULLSTR_EMPTY(details));
O
Osier Yang 已提交
626 627 628 629 630 631 632 633
        goto cleanup;
    }

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

634
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
635 636
        goto cleanup;

637 638
    if ((count = ncf_list_interfaces(driver->netcf, count,
                                     names, ncf_flags)) < 0) {
O
Osier Yang 已提交
639 640
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
641

O
Osier Yang 已提交
642 643 644
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
645
                       NULLSTR_EMPTY(details));
O
Osier Yang 已提交
646 647 648
        goto cleanup;
    }

649 650
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
651 652

    for (i = 0; i < count; i++) {
653
        virInterfaceDefPtr def;
654

O
Osier Yang 已提交
655 656 657 658 659 660 661 662
        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,
J
Ján Tomko 已提交
663
                               details ? " - " : "", NULLSTR_EMPTY(details));
O
Osier Yang 已提交
664 665 666 667 668 669 670 671 672 673 674
                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;
            }
        }

675 676 677 678 679 680 681 682 683
        if (!(def = netcfGetMinimalDefForDevice(iface)))
            goto cleanup;

        if (!virConnectListAllInterfacesCheckACL(conn, def)) {
            ncf_if_free(iface);
            iface = NULL;
            virInterfaceDefFree(def);
            continue;
        }
684 685

        if (ifaces) {
686 687
            if (!(iface_obj = virGetInterface(conn, def->name, def->mac))) {
                virInterfaceDefFree(def);
688
                goto cleanup;
689
            }
690
            tmp_iface_objs[niface_objs] = iface_obj;
O
Osier Yang 已提交
691
        }
692
        niface_objs++;
O
Osier Yang 已提交
693

694
        virInterfaceDefFree(def);
O
Osier Yang 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707
        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;

708
 cleanup:
O
Osier Yang 已提交
709 710
    ncf_if_free(iface);

711
    if (names && count > 0)
O
Osier Yang 已提交
712 713 714 715 716
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);

    if (tmp_iface_objs) {
717 718
        for (i = 0; i < niface_objs; i++)
            virObjectUnref(tmp_iface_objs[i]);
719
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
720 721
    }

722
    virObjectUnlock(driver);
O
Osier Yang 已提交
723 724 725 726
    return ret;
}


727 728
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
729 730 731
{
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
732
    virInterfaceDefPtr def = NULL;
733

734
    virObjectLock(driver);
735 736 737 738 739
    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) {
740 741 742
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
J
Ján Tomko 已提交
743
                           details ? " - " : "", NULLSTR_EMPTY(details));
744
        } else {
745 746
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
747 748 749 750
        }
        goto cleanup;
    }

751 752 753 754 755 756
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

757
    ret = virGetInterface(conn, def->name, def->mac);
758

759
 cleanup:
760
    ncf_if_free(iface);
761
    virInterfaceDefFree(def);
762
    virObjectUnlock(driver);
763 764 765
    return ret;
}

766 767
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
768 769 770 771
{
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
772
    virInterfaceDefPtr def = NULL;
773

774
    virObjectLock(driver);
775 776 777 778 779
    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);
780 781 782
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
783
                       NULLSTR_EMPTY(details));
784 785 786
        goto cleanup;
    }
    if (niface == 0) {
787 788 789
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
790 791 792
        goto cleanup;
    }
    if (niface > 1) {
793 794
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
795 796 797
        goto cleanup;
    }

798 799 800 801 802 803 804

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

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

805
    ret = virGetInterface(conn, def->name, def->mac);
806

807
 cleanup:
808
    ncf_if_free(iface);
809
    virInterfaceDefFree(def);
810
    virObjectUnlock(driver);
811 812 813
    return ret;
}

814 815
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
816 817 818 819 820
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;
821
    bool active;
822

823 824
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

825
    virObjectLock(driver);
826 827 828 829 830 831 832

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

833 834 835 836
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
       goto cleanup;

    if ((flags & VIR_INTERFACE_XML_INACTIVE) || !active) {
837 838 839 840
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
841 842 843
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
844 845 846
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
847
                       NULLSTR_EMPTY(details));
848 849 850
        goto cleanup;
    }

851
    ifacedef = virInterfaceDefParseString(xmlstr);
852 853 854 855 856
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

857 858 859
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

860
    ret = virInterfaceDefFormat(ifacedef);
861 862 863 864 865
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

866
 cleanup:
867 868 869
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
870
    virObjectUnlock(driver);
871 872 873
    return ret;
}

874 875 876
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
877 878 879 880 881 882
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

883 884
    virCheckFlags(0, NULL);

885
    virObjectLock(driver);
886

887
    ifacedef = virInterfaceDefParseString(xml);
888 889 890 891 892
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

893 894 895
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

896
    xmlstr = virInterfaceDefFormat(ifacedef);
897 898 899 900 901 902 903 904 905
    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);
906 907 908
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
909
                       NULLSTR_EMPTY(details));
910 911 912 913 914
        goto cleanup;
    }

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

915
 cleanup:
916 917 918
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
919
    virObjectUnlock(driver);
920 921 922
    return ret;
}

923 924
static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
{
925
    struct netcf_if *iface = NULL;
926
    virInterfaceDefPtr def = NULL;
927 928
    int ret = -1;

929
    virObjectLock(driver);
930 931 932 933 934 935 936

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

937 938 939 940 941 942 943

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

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

944 945 946 947
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
948 949 950
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
951
                       NULLSTR_EMPTY(details));
952 953 954
        goto cleanup;
    }

955
 cleanup:
956
    ncf_if_free(iface);
957
    virInterfaceDefFree(def);
958
    virObjectUnlock(driver);
959 960 961
    return ret;
}

962 963
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
964 965
{
    struct netcf_if *iface = NULL;
966
    virInterfaceDefPtr def = NULL;
967
    int ret = -1;
968
    bool active;
969

970 971
    virCheckFlags(0, -1);

972
    virObjectLock(driver);
973 974 975 976 977 978 979

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

980 981 982 983 984 985 986

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

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

987 988 989 990 991 992 993 994 995
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

996 997 998 999
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1000 1001 1002
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1003
                       NULLSTR_EMPTY(details));
1004 1005 1006
        goto cleanup;
    }

1007
 cleanup:
1008
    ncf_if_free(iface);
1009
    virInterfaceDefFree(def);
1010
    virObjectUnlock(driver);
1011 1012 1013
    return ret;
}

1014 1015
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
1016 1017
{
    struct netcf_if *iface = NULL;
1018
    virInterfaceDefPtr def = NULL;
1019
    int ret = -1;
1020
    bool active;
1021

1022 1023
    virCheckFlags(0, -1);

1024
    virObjectLock(driver);
1025 1026 1027 1028 1029 1030 1031

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

1032 1033 1034 1035 1036 1037 1038

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

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

1039 1040 1041 1042 1043 1044 1045 1046 1047
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

1048 1049 1050 1051
    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1052 1053 1054
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1055
                       NULLSTR_EMPTY(details));
1056 1057 1058
        goto cleanup;
    }

1059
 cleanup:
1060
    ncf_if_free(iface);
1061
    virInterfaceDefFree(def);
1062
    virObjectUnlock(driver);
1063 1064 1065
    return ret;
}

1066
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
1067 1068
{
    struct netcf_if *iface = NULL;
1069
    virInterfaceDefPtr def = NULL;
1070
    int ret = -1;
1071
    bool active;
1072

1073
    virObjectLock(driver);
1074 1075 1076 1077 1078 1079 1080

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

1081 1082 1083 1084 1085 1086
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

1087
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
1088 1089
        goto cleanup;

1090
    ret = active ? 1 : 0;
1091

1092
 cleanup:
1093
    ncf_if_free(iface);
1094
    virInterfaceDefFree(def);
1095
    virObjectUnlock(driver);
1096 1097 1098
    return ret;
}

1099
#ifdef HAVE_NETCF_TRANSACTIONS
1100
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1101 1102 1103 1104 1105
{
    int ret;

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

1106 1107 1108
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1109
    virObjectLock(driver);
1110 1111 1112 1113 1114

    ret = ncf_change_begin(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1115 1116 1117
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to begin transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1118
                       NULLSTR_EMPTY(details));
1119 1120
    }

1121
    virObjectUnlock(driver);
1122 1123 1124
    return ret;
}

1125
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1126 1127 1128 1129 1130
{
    int ret;

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

1131 1132 1133
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1134
    virObjectLock(driver);
1135 1136 1137 1138 1139

    ret = ncf_change_commit(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1140 1141 1142
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to commit transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1143
                       NULLSTR_EMPTY(details));
1144 1145
    }

1146
    virObjectUnlock(driver);
1147 1148 1149
    return ret;
}

1150
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1151 1152 1153 1154 1155
{
    int ret;

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

1156 1157 1158
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1159
    virObjectLock(driver);
1160 1161 1162 1163 1164

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1165 1166 1167
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1168
                       NULLSTR_EMPTY(details));
1169 1170
    }

1171
    virObjectUnlock(driver);
1172 1173 1174 1175
    return ret;
}
#endif /* HAVE_NETCF_TRANSACTIONS */

1176
static virInterfaceDriver interfaceDriver = {
1177
    .name = INTERFACE_DRIVER_NAME,
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    .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 */
1191
#ifdef HAVE_NETCF_TRANSACTIONS
1192 1193 1194
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
1195
#endif /* HAVE_NETCF_TRANSACTIONS */
1196 1197
};

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

static virHypervisorDriver interfaceHypervisorDriver = {
    .name = "interface",
    .connectOpen = netcfConnectOpen, /* 4.1.0 */
    .connectClose = netcfConnectClose, /* 4.1.0 */
    .connectIsEncrypted = netcfConnectIsEncrypted, /* 4.1.0 */
    .connectIsSecure = netcfConnectIsSecure, /* 4.1.0 */
    .connectIsAlive = netcfConnectIsAlive, /* 4.1.0 */
};


static virConnectDriver interfaceConnectDriver = {
1210
    .localOnly = true,
1211
    .uriSchemes = (const char *[]){ "interface", NULL },
1212 1213 1214 1215 1216
    .hypervisorDriver = &interfaceHypervisorDriver,
    .interfaceDriver = &interfaceDriver,
};


1217 1218 1219 1220 1221 1222 1223
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

1224 1225
int netcfIfaceRegister(void)
{
1226 1227
    if (virRegisterConnectDriver(&interfaceConnectDriver, false) < 0)
        return -1;
1228
    if (virSetSharedInterfaceDriver(&interfaceDriver) < 0)
1229
        return -1;
1230 1231
    if (virRegisterStateDriver(&interfaceStateDriver) < 0)
        return -1;
1232 1233
    return 0;
}