interface_backend_netcf.c 35.7 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 "virfile.h"
#include "virpidfile.h"
34 35
#include "virstring.h"
#include "viraccessapicheck.h"
J
John Ferlan 已提交
36
#include "virinterfaceobj.h"
37

38 39
#include "configmake.h"

40 41
#define VIR_FROM_THIS VIR_FROM_INTERFACE

42 43
VIR_LOG_INIT("interface.interface_backend_netcf");

44 45
#define INTERFACE_DRIVER_NAME "netcf"

46
/* Main driver state */
47
typedef struct
48
{
49
    virObjectLockable parent;
50 51 52 53
    /* pid file FD, ensures two copies of the driver can't use the same root */
    int lockFD;

    char *stateDir;
54
    struct netcf *netcf;
55
    bool privileged;
56
} virNetcfDriverState, *virNetcfDriverStatePtr;
57

58 59
static virClassPtr virNetcfDriverStateClass;
static void virNetcfDriverStateDispose(void *obj);
60

61 62 63
static int
virNetcfDriverStateOnceInit(void)
{
64
    if (!VIR_CLASS_NEW(virNetcfDriverState, virClassForObjectLockable()))
65
        return -1;
66

67 68 69
    return 0;
}

70
VIR_ONCE_GLOBAL_INIT(virNetcfDriverState);
71

72
static virNetcfDriverStatePtr driver;
73 74 75 76


static void
virNetcfDriverStateDispose(void *obj)
77
{
78
    virNetcfDriverStatePtr _driver = obj;
79

80 81
    if (_driver->netcf)
        ncf_close(_driver->netcf);
82 83 84 85 86

    if (_driver->lockFD != -1)
        virPidFileRelease(_driver->stateDir, "driver", _driver->lockFD);

    VIR_FREE(_driver->stateDir);
87 88 89 90
}


static int
91
netcfStateInitialize(bool privileged,
92
                     const char *root,
J
Ján Tomko 已提交
93 94
                     virStateInhibitCallback callback G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED)
95
{
96 97 98 99 100 101
    if (root != NULL) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Driver does not support embedded mode"));
        return -1;
    }

102
    if (virNetcfDriverStateInitialize() < 0)
103
        return VIR_DRV_STATE_INIT_ERROR;
104

105
    if (!(driver = virObjectLockableNew(virNetcfDriverStateClass)))
106
        return VIR_DRV_STATE_INIT_ERROR;
107

108 109
    driver->privileged = privileged;

110
    if (privileged) {
111
        driver->stateDir = g_strdup_printf("%s/libvirt/interface", RUNSTATEDIR);
112
    } else {
113
        g_autofree char *rundir = NULL;
114

115
        rundir = virGetUserRuntimeDirectory();
116
        driver->stateDir = g_strdup_printf("%s/interface/run", rundir);
117 118 119 120 121 122 123 124 125
    }

    if (virFileMakePathWithMode(driver->stateDir, S_IRWXU) < 0) {
        virReportSystemError(errno, _("cannot create state directory '%s'"),
                             driver->stateDir);
        goto error;
    }

    if ((driver->lockFD =
126
         virPidFileAcquire(driver->stateDir, "driver", false, getpid())) < 0)
127 128
        goto error;

129
    /* open netcf */
130
    if (ncf_init(&driver->netcf, NULL) != 0) {
131 132
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to initialize netcf"));
133
        goto error;
134
    }
135
    return VIR_DRV_STATE_INIT_COMPLETE;
136 137 138 139

 error:
    virObjectUnref(driver);
    driver = NULL;
140
    return VIR_DRV_STATE_INIT_ERROR;
141 142
}

143 144 145

static int
netcfStateCleanup(void)
146
{
147
    if (!driver)
148 149
        return -1;

150
    if (virObjectUnref(driver)) {
151 152 153 154 155
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Attempt to close netcf state driver "
                         "with open connections"));
        return -1;
    }
156
    driver = NULL;
157
    return 0;
158 159
}

160 161 162 163 164 165

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

166
    if (!driver)
167 168
        return 0;

169 170 171
    virObjectLock(driver);
    ncf_close(driver->netcf);
    if (ncf_init(&driver->netcf, NULL) != 0) {
172 173 174 175 176 177 178 179
        /* 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"));
180
        driver->netcf = NULL;
181 182 183 184
        goto cleanup;
    }

    ret = 0;
185
 cleanup:
186
    virObjectUnlock(driver);
187 188 189 190 191

    return ret;
}


192 193
static virDrvOpenStatus
netcfConnectOpen(virConnectPtr conn,
J
Ján Tomko 已提交
194 195
                 virConnectAuthPtr auth G_GNUC_UNUSED,
                 virConfPtr conf G_GNUC_UNUSED,
196 197 198 199
                 unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

200 201 202 203 204 205
    if (driver == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("interface state driver is not active"));
        return VIR_DRV_OPEN_ERROR;
    }

206 207 208 209
    if (!virConnectValidateURIPath(conn->uri->path,
                                   "interface",
                                   driver->privileged))
        return VIR_DRV_OPEN_ERROR;
210 211 212 213 214 215 216

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

    return VIR_DRV_OPEN_SUCCESS;
}

J
Ján Tomko 已提交
217
static int netcfConnectClose(virConnectPtr conn G_GNUC_UNUSED)
218 219 220 221 222
{
    return 0;
}


J
Ján Tomko 已提交
223
static int netcfConnectIsSecure(virConnectPtr conn G_GNUC_UNUSED)
224 225 226 227 228 229
{
    /* Trivially secure, since always inside the daemon */
    return 1;
}


J
Ján Tomko 已提交
230
static int netcfConnectIsEncrypted(virConnectPtr conn G_GNUC_UNUSED)
231 232 233 234 235 236
{
    /* Not encrypted, but remote driver takes care of that */
    return 0;
}


J
Ján Tomko 已提交
237
static int netcfConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED)
238 239 240 241 242
{
    return 1;
}


243 244 245
/*
 * Get a minimal virInterfaceDef containing enough metadata
 * for access control checks to be performed. Currently
N
Nehal J Wani 已提交
246
 * this implies existence of name and mac address attributes
247 248 249 250 251 252 253
 */
static virInterfaceDef * ATTRIBUTE_NONNULL(1)
netcfGetMinimalDefForDevice(struct netcf_if *iface)
{
    virInterfaceDef *def;

    /* Allocate our interface definition structure */
254
    if (VIR_ALLOC(def) < 0)
255 256
        return NULL;

257 258
    def->name = g_strdup(ncf_if_name(iface));
    def->mac = g_strdup(ncf_if_mac_string(iface));
259 260 261 262 263

    return def;
}


264 265
static int netcf_to_vir_err(int netcf_errcode)
{
E
Eric Blake 已提交
266
    switch (netcf_errcode) {
267 268 269 270 271 272 273 274 275 276
        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:
277 278 279 280
            /*
             * allocation failed return VIR ERR NO MEMORY
             * though it should not be used now.
             */
281
            return 2;
282 283 284 285 286 287 288 289 290 291 292 293
        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;
294 295 296 297 298
#ifdef NETCF_EINVALIDOP
        case NETCF_EINVALIDOP:
            /* attempted operation is invalid while the system is in the current state. */
            return VIR_ERR_OPERATION_INVALID;
#endif
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        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) {
314 315 316
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
317
                           NULLSTR_EMPTY(details));
318
        } else {
319 320 321
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"),
                           ifinfo->name);
322 323 324 325 326
        }
    }
    return iface;
}

327 328 329 330 331 332 333
static int
netcfInterfaceObjIsActive(struct netcf_if *iface,
                          bool *active)
{
    int ret = -1;
    unsigned int flags = 0;

334
    virObjectRef(driver);
335 336
    if (ncf_if_status(iface, &flags) < 0) {
        const char *errmsg, *details;
337
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
338 339 340
        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 已提交
341
                       NULLSTR_EMPTY(details));
342 343 344 345 346 347
        goto cleanup;
    }

    *active = flags & NETCF_IFACE_ACTIVE;
    ret = 0;

348
 cleanup:
349
    virObjectUnref(driver);
350 351 352
    return ret;
}

353 354 355
static int netcfConnectNumOfInterfacesImpl(virConnectPtr conn,
                                           int status,
                                           virInterfaceObjListFilter filter)
356
{
357 358 359
    int count;
    int want = 0;
    int ret = -1;
360
    size_t i;
361
    char **names = NULL;
362

363 364 365 366
    /* List all interfaces, in case we might support new filter flags
     * beyond active|inactive in future.
     */
    count = ncf_num_of_interfaces(driver->netcf, status);
367 368 369
    if (count < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
370
        virReportError(netcf_to_vir_err(errcode),
371 372
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
373
                       NULLSTR_EMPTY(details));
374
        goto cleanup;
375 376
    }

377 378 379 380 381
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

382
    if (VIR_ALLOC_N(names, count) < 0)
383 384 385 386 387 388 389 390
        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 已提交
391
                       NULLSTR_EMPTY(details));
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        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 已提交
407
                               details ? " - " : "", NULLSTR_EMPTY(details));
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
                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;

436
 cleanup:
437
    if (names && count > 0)
438 439 440 441
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);
    return ret;
442 443
}

444 445 446 447 448

static int netcfConnectListInterfacesImpl(virConnectPtr conn,
                                          int status,
                                          char **const names, int nnames,
                                          virInterfaceObjListFilter filter)
449
{
450 451 452
    int count = 0;
    int want = 0;
    int ret = -1;
453
    size_t i;
454
    char **allnames = NULL;
455

456 457 458 459 460 461 462
    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 已提交
463
                       NULLSTR_EMPTY(details));
464 465
        goto cleanup;
    }
466

467 468 469 470
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
471

472
    if (VIR_ALLOC_N(allnames, count) < 0)
473 474 475
        goto cleanup;

    if ((count = ncf_list_interfaces(driver->netcf, count, allnames, status)) < 0) {
476 477
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
478 479 480
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
481
                       NULLSTR_EMPTY(details));
482
        goto cleanup;
483 484
    }

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    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 已提交
502
                               details ? " - " : "", NULLSTR_EMPTY(details));
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
                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;

532
 cleanup:
533
    if (allnames && count > 0)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        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;

552
    virObjectLock(driver);
553 554 555
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_ACTIVE,
                                            virConnectNumOfInterfacesCheckACL);
556
    virObjectUnlock(driver);
557 558 559 560 561 562 563 564 565 566
    return count;
}

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

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

567
    virObjectLock(driver);
568 569 570 571
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
572
    virObjectUnlock(driver);
573 574 575 576
    return count;

}

577
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
578 579 580
{
    int count;

581 582 583
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

584
    virObjectLock(driver);
585 586 587
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
588
    virObjectUnlock(driver);
589 590 591
    return count;
}

592
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
593 594 595
{
    int count;

596 597 598
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

599
    virObjectLock(driver);
600 601 602 603
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
604
    virObjectUnlock(driver);
605 606 607 608
    return count;

}

609
#define MATCH(FLAG) (flags & (FLAG))
O
Osier Yang 已提交
610
static int
611 612 613
netcfConnectListAllInterfaces(virConnectPtr conn,
                              virInterfacePtr **ifaces,
                              unsigned int flags)
O
Osier Yang 已提交
614 615
{
    int count;
616
    size_t i;
617
    unsigned int ncf_flags = 0;
O
Osier Yang 已提交
618 619 620 621 622 623 624
    struct netcf_if *iface = NULL;
    virInterfacePtr *tmp_iface_objs = NULL;
    virInterfacePtr iface_obj = NULL;
    int niface_objs = 0;
    int ret = -1;
    char **names = NULL;

625
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
626

627 628 629
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

630
    virObjectLock(driver);
O
Osier Yang 已提交
631

632 633 634 635 636 637 638 639 640 641 642
    /* 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 已提交
643 644
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
645

O
Osier Yang 已提交
646 647 648
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to get number of host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
649
                       NULLSTR_EMPTY(details));
O
Osier Yang 已提交
650 651 652 653 654 655 656 657
        goto cleanup;
    }

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

658
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
659 660
        goto cleanup;

661 662
    if ((count = ncf_list_interfaces(driver->netcf, count,
                                     names, ncf_flags)) < 0) {
O
Osier Yang 已提交
663 664
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
665

O
Osier Yang 已提交
666 667 668
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to list host interfaces: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
669
                       NULLSTR_EMPTY(details));
O
Osier Yang 已提交
670 671 672
        goto cleanup;
    }

673 674
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
675 676

    for (i = 0; i < count; i++) {
677
        virInterfaceDefPtr def;
678

O
Osier Yang 已提交
679 680 681 682 683 684 685 686
        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 已提交
687
                               details ? " - " : "", NULLSTR_EMPTY(details));
O
Osier Yang 已提交
688 689 690 691 692 693 694 695 696 697 698
                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;
            }
        }

699 700 701 702 703 704 705 706 707
        if (!(def = netcfGetMinimalDefForDevice(iface)))
            goto cleanup;

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

        if (ifaces) {
710 711
            if (!(iface_obj = virGetInterface(conn, def->name, def->mac))) {
                virInterfaceDefFree(def);
712
                goto cleanup;
713
            }
714
            tmp_iface_objs[niface_objs] = iface_obj;
O
Osier Yang 已提交
715
        }
716
        niface_objs++;
O
Osier Yang 已提交
717

718
        virInterfaceDefFree(def);
O
Osier Yang 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731
        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;

732
 cleanup:
O
Osier Yang 已提交
733 734
    ncf_if_free(iface);

735
    if (names && count > 0)
O
Osier Yang 已提交
736 737 738 739 740
        for (i = 0; i < count; i++)
            VIR_FREE(names[i]);
    VIR_FREE(names);

    if (tmp_iface_objs) {
741 742
        for (i = 0; i < niface_objs; i++)
            virObjectUnref(tmp_iface_objs[i]);
743
        VIR_FREE(tmp_iface_objs);
O
Osier Yang 已提交
744 745
    }

746
    virObjectUnlock(driver);
O
Osier Yang 已提交
747 748 749 750
    return ret;
}


751 752
static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
                                                  const char *name)
753 754 755
{
    struct netcf_if *iface;
    virInterfacePtr ret = NULL;
756
    virInterfaceDefPtr def = NULL;
757

758
    virObjectLock(driver);
759 760 761 762 763
    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) {
764 765 766
            virReportError(netcf_to_vir_err(errcode),
                           _("couldn't find interface named '%s': %s%s%s"),
                           name, errmsg,
J
Ján Tomko 已提交
767
                           details ? " - " : "", NULLSTR_EMPTY(details));
768
        } else {
769 770
            virReportError(VIR_ERR_NO_INTERFACE,
                           _("couldn't find interface named '%s'"), name);
771 772 773 774
        }
        goto cleanup;
    }

775 776 777 778 779 780
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

781
    ret = virGetInterface(conn, def->name, def->mac);
782

783
 cleanup:
784
    ncf_if_free(iface);
785
    virInterfaceDefFree(def);
786
    virObjectUnlock(driver);
787 788 789
    return ret;
}

790 791
static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
                                                       const char *macstr)
792 793 794 795
{
    struct netcf_if *iface;
    int niface;
    virInterfacePtr ret = NULL;
796
    virInterfaceDefPtr def = NULL;
797

798
    virObjectLock(driver);
799 800 801 802 803
    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);
804 805 806
        virReportError(netcf_to_vir_err(errcode),
                       _("couldn't find interface with MAC address '%s': %s%s%s"),
                       macstr, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
807
                       NULLSTR_EMPTY(details));
808 809 810
        goto cleanup;
    }
    if (niface == 0) {
811 812 813
        virReportError(VIR_ERR_NO_INTERFACE,
                       _("couldn't find interface with MAC address '%s'"),
                       macstr);
814 815 816
        goto cleanup;
    }
    if (niface > 1) {
817 818
        virReportError(VIR_ERR_MULTIPLE_INTERFACES,
                       "%s", _("multiple interfaces with matching MAC address"));
819 820 821
        goto cleanup;
    }

822 823 824 825 826 827 828

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

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

829
    ret = virGetInterface(conn, def->name, def->mac);
830

831
 cleanup:
832
    ncf_if_free(iface);
833
    virInterfaceDefFree(def);
834
    virObjectUnlock(driver);
835 836 837
    return ret;
}

838 839
static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
                                      unsigned int flags)
840 841 842 843 844
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    char *ret = NULL;
845
    bool active;
846

847 848
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

849
    virObjectLock(driver);
850 851 852 853 854 855 856

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

857 858 859 860
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
       goto cleanup;

    if ((flags & VIR_INTERFACE_XML_INACTIVE) || !active) {
861 862 863 864
        xmlstr = ncf_if_xml_desc(iface);
    } else {
        xmlstr = ncf_if_xml_state(iface);
    }
865 866 867
    if (!xmlstr) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
868 869 870
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
871
                       NULLSTR_EMPTY(details));
872 873 874
        goto cleanup;
    }

875
    ifacedef = virInterfaceDefParseString(xmlstr);
876 877 878 879 880
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

881 882 883
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

884
    ret = virInterfaceDefFormat(ifacedef);
885 886 887 888 889
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

890
 cleanup:
891 892 893
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
894
    virObjectUnlock(driver);
895 896 897
    return ret;
}

898 899 900
static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
                                               const char *xml,
                                               unsigned int flags)
901 902 903 904 905 906
{
    struct netcf_if *iface = NULL;
    char *xmlstr = NULL;
    virInterfaceDefPtr ifacedef = NULL;
    virInterfacePtr ret = NULL;

907 908
    virCheckFlags(0, NULL);

909
    virObjectLock(driver);
910

911
    ifacedef = virInterfaceDefParseString(xml);
912 913 914 915 916
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

917 918 919
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

920
    xmlstr = virInterfaceDefFormat(ifacedef);
921 922 923 924 925 926 927 928 929
    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);
930 931 932
        virReportError(netcf_to_vir_err(errcode),
                       _("could not get interface XML description: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
933
                       NULLSTR_EMPTY(details));
934 935 936 937 938
        goto cleanup;
    }

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

939
 cleanup:
940 941 942
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
943
    virObjectUnlock(driver);
944 945 946
    return ret;
}

947 948
static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
{
949
    struct netcf_if *iface = NULL;
950
    virInterfaceDefPtr def = NULL;
951 952
    int ret = -1;

953
    virObjectLock(driver);
954 955 956 957 958 959 960

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

961 962 963 964 965 966 967

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

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

968 969 970 971
    ret = ncf_if_undefine(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
972 973 974
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to undefine interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
975
                       NULLSTR_EMPTY(details));
976 977 978
        goto cleanup;
    }

979
 cleanup:
980
    ncf_if_free(iface);
981
    virInterfaceDefFree(def);
982
    virObjectUnlock(driver);
983 984 985
    return ret;
}

986 987
static int netcfInterfaceCreate(virInterfacePtr ifinfo,
                                unsigned int flags)
988 989
{
    struct netcf_if *iface = NULL;
990
    virInterfaceDefPtr def = NULL;
991
    int ret = -1;
992
    bool active;
993

994 995
    virCheckFlags(0, -1);

996
    virObjectLock(driver);
997 998 999 1000 1001 1002 1003

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

1004 1005 1006 1007 1008 1009 1010

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

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

1011 1012 1013 1014 1015 1016 1017 1018 1019
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

1020 1021 1022 1023
    ret = ncf_if_up(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1024 1025 1026
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to create (start) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1027
                       NULLSTR_EMPTY(details));
1028 1029 1030
        goto cleanup;
    }

1031
 cleanup:
1032
    ncf_if_free(iface);
1033
    virInterfaceDefFree(def);
1034
    virObjectUnlock(driver);
1035 1036 1037
    return ret;
}

1038 1039
static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
                                 unsigned int flags)
1040 1041
{
    struct netcf_if *iface = NULL;
1042
    virInterfaceDefPtr def = NULL;
1043
    int ret = -1;
1044
    bool active;
1045

1046 1047
    virCheckFlags(0, -1);

1048
    virObjectLock(driver);
1049 1050 1051 1052 1053 1054 1055

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

1056 1057 1058 1059 1060 1061 1062

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

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

1063 1064 1065 1066 1067 1068 1069 1070 1071
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

1072 1073 1074 1075
    ret = ncf_if_down(iface);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1076 1077 1078
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to destroy (stop) interface %s: %s%s%s"),
                       ifinfo->name, errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1079
                       NULLSTR_EMPTY(details));
1080 1081 1082
        goto cleanup;
    }

1083
 cleanup:
1084
    ncf_if_free(iface);
1085
    virInterfaceDefFree(def);
1086
    virObjectUnlock(driver);
1087 1088 1089
    return ret;
}

1090
static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
1091 1092
{
    struct netcf_if *iface = NULL;
1093
    virInterfaceDefPtr def = NULL;
1094
    int ret = -1;
1095
    bool active;
1096

1097
    virObjectLock(driver);
1098 1099 1100 1101 1102 1103 1104

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

1105 1106 1107 1108 1109 1110
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

1111
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
1112 1113
        goto cleanup;

1114
    ret = active ? 1 : 0;
1115

1116
 cleanup:
1117
    ncf_if_free(iface);
1118
    virInterfaceDefFree(def);
1119
    virObjectUnlock(driver);
1120 1121 1122
    return ret;
}

1123
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1124 1125 1126 1127 1128
{
    int ret;

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

1129 1130 1131
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1132
    virObjectLock(driver);
1133 1134 1135 1136 1137

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

1144
    virObjectUnlock(driver);
1145 1146 1147
    return ret;
}

1148
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1149 1150 1151 1152 1153
{
    int ret;

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

1154 1155 1156
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1157
    virObjectLock(driver);
1158 1159 1160 1161 1162

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

1169
    virObjectUnlock(driver);
1170 1171 1172
    return ret;
}

1173
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1174 1175 1176 1177 1178
{
    int ret;

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

1179 1180 1181
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1182
    virObjectLock(driver);
1183 1184 1185 1186 1187

    ret = ncf_change_rollback(driver->netcf, 0);
    if (ret < 0) {
        const char *errmsg, *details;
        int errcode = ncf_error(driver->netcf, &errmsg, &details);
1188 1189 1190
        virReportError(netcf_to_vir_err(errcode),
                       _("failed to rollback transaction: %s%s%s"),
                       errmsg, details ? " - " : "",
J
Ján Tomko 已提交
1191
                       NULLSTR_EMPTY(details));
1192 1193
    }

1194
    virObjectUnlock(driver);
1195 1196 1197
    return ret;
}

1198
static virInterfaceDriver interfaceDriver = {
1199
    .name = INTERFACE_DRIVER_NAME,
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    .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 */
    .interfaceChangeBegin = netcfInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = netcfInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = netcfInterfaceChangeRollback, /* 0.9.2 */
1216 1217
};

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

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 = {
1230
    .localOnly = true,
1231
    .uriSchemes = (const char *[]){ "interface", NULL },
1232 1233 1234 1235 1236
    .hypervisorDriver = &interfaceHypervisorDriver,
    .interfaceDriver = &interfaceDriver,
};


1237 1238 1239 1240 1241 1242 1243
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

1244 1245
int netcfIfaceRegister(void)
{
1246 1247
    if (virRegisterConnectDriver(&interfaceConnectDriver, false) < 0)
        return -1;
1248
    if (virSetSharedInterfaceDriver(&interfaceDriver) < 0)
1249
        return -1;
1250 1251
    if (virRegisterStateDriver(&interfaceStateDriver) < 0)
        return -1;
1252 1253
    return 0;
}