interface_backend_netcf.c 35.6 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,
J
Ján Tomko 已提交
92 93
                     virStateInhibitCallback callback G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED)
94 95
{
    if (virNetcfDriverStateInitialize() < 0)
96
        return VIR_DRV_STATE_INIT_ERROR;
97

98
    if (!(driver = virObjectLockableNew(virNetcfDriverStateClass)))
99
        return VIR_DRV_STATE_INIT_ERROR;
100

101 102
    driver->privileged = privileged;

103 104
    if (privileged) {
        if (virAsprintf(&driver->stateDir,
105
                        "%s/libvirt/interface", RUNSTATEDIR) < 0)
106 107
            goto error;
    } else {
108
        g_autofree char *rundir = NULL;
109 110 111

        if (!(rundir = virGetUserRuntimeDirectory()))
            goto error;
112
        if (virAsprintf(&driver->stateDir, "%s/interface/run", rundir) < 0)
113 114 115 116 117 118 119 120 121 122
            goto error;
    }

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

    if ((driver->lockFD =
123
         virPidFileAcquire(driver->stateDir, "driver", false, getpid())) < 0)
124 125
        goto error;

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

 error:
    virObjectUnref(driver);
    driver = NULL;
137
    return VIR_DRV_STATE_INIT_ERROR;
138 139
}

140 141 142

static int
netcfStateCleanup(void)
143
{
144
    if (!driver)
145 146
        return -1;

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

157 158 159 160 161 162

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

163
    if (!driver)
164 165
        return 0;

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

    ret = 0;
182
 cleanup:
183
    virObjectUnlock(driver);
184 185 186 187 188

    return ret;
}


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

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

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

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

    return VIR_DRV_OPEN_SUCCESS;
}

J
Ján Tomko 已提交
214
static int netcfConnectClose(virConnectPtr conn G_GNUC_UNUSED)
215 216 217 218 219
{
    return 0;
}


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


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


J
Ján Tomko 已提交
234
static int netcfConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED)
235 236 237 238 239
{
    return 1;
}


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

    /* Allocate our interface definition structure */
251
    if (VIR_ALLOC(def) < 0)
252 253
        return NULL;

254 255
    def->name = g_strdup(ncf_if_name(iface));
    def->mac = g_strdup(ncf_if_mac_string(iface));
256 257 258 259 260

    return def;
}


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

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

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

    *active = flags & NETCF_IFACE_ACTIVE;
    ret = 0;

345
 cleanup:
346
    virObjectUnref(driver);
347 348 349
    return ret;
}

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

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

374 375 376 377 378
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }

379
    if (VIR_ALLOC_N(names, count) < 0)
380 381 382 383 384 385 386 387
        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 已提交
388
                       NULLSTR_EMPTY(details));
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        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 已提交
404
                               details ? " - " : "", NULLSTR_EMPTY(details));
405 406 407 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
                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;

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

441 442 443 444 445

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

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

464 465 466 467
    if (count == 0) {
        ret = 0;
        goto cleanup;
    }
468

469
    if (VIR_ALLOC_N(allnames, count) < 0)
470 471 472
        goto cleanup;

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

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    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 已提交
499
                               details ? " - " : "", NULLSTR_EMPTY(details));
500 501 502 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
                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;

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

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

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

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

564
    virObjectLock(driver);
565 566 567 568
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_ACTIVE,
                                           names, nnames,
                                           virConnectListInterfacesCheckACL);
569
    virObjectUnlock(driver);
570 571 572 573
    return count;

}

574
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
575 576 577
{
    int count;

578 579 580
    if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

581
    virObjectLock(driver);
582 583 584
    count = netcfConnectNumOfInterfacesImpl(conn,
                                            NETCF_IFACE_INACTIVE,
                                            virConnectNumOfDefinedInterfacesCheckACL);
585
    virObjectUnlock(driver);
586 587 588
    return count;
}

589
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
590 591 592
{
    int count;

593 594 595
    if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
        return -1;

596
    virObjectLock(driver);
597 598 599 600
    count = netcfConnectListInterfacesImpl(conn,
                                           NETCF_IFACE_INACTIVE,
                                           names, nnames,
                                           virConnectListDefinedInterfacesCheckACL);
601
    virObjectUnlock(driver);
602 603 604 605
    return count;

}

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

622
    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1);
O
Osier Yang 已提交
623

624 625 626
    if (virConnectListAllInterfacesEnsureACL(conn) < 0)
        return -1;

627
    virObjectLock(driver);
O
Osier Yang 已提交
628

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

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

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

655
    if (VIR_ALLOC_N(names, count) < 0)
O
Osier Yang 已提交
656 657
        goto cleanup;

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

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

670 671
    if (ifaces && VIR_ALLOC_N(tmp_iface_objs, count + 1) < 0)
        goto cleanup;
O
Osier Yang 已提交
672 673

    for (i = 0; i < count; i++) {
674
        virInterfaceDefPtr def;
675

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

696 697 698 699 700 701 702 703 704
        if (!(def = netcfGetMinimalDefForDevice(iface)))
            goto cleanup;

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

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

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

729
 cleanup:
O
Osier Yang 已提交
730 731
    ncf_if_free(iface);

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

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

743
    virObjectUnlock(driver);
O
Osier Yang 已提交
744 745 746 747
    return ret;
}


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

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

772 773 774 775 776 777
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

778
    ret = virGetInterface(conn, def->name, def->mac);
779

780
 cleanup:
781
    ncf_if_free(iface);
782
    virInterfaceDefFree(def);
783
    virObjectUnlock(driver);
784 785 786
    return ret;
}

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

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

819 820 821 822 823 824 825

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

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

826
    ret = virGetInterface(conn, def->name, def->mac);
827

828
 cleanup:
829
    ncf_if_free(iface);
830
    virInterfaceDefFree(def);
831
    virObjectUnlock(driver);
832 833 834
    return ret;
}

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

844 845
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

846
    virObjectLock(driver);
847 848 849 850 851 852 853

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

854 855 856 857
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
       goto cleanup;

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

872
    ifacedef = virInterfaceDefParseString(xmlstr);
873 874 875 876 877
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

878 879 880
    if (virInterfaceGetXMLDescEnsureACL(ifinfo->conn, ifacedef) < 0)
        goto cleanup;

881
    ret = virInterfaceDefFormat(ifacedef);
882 883 884 885 886
    if (!ret) {
        /* error was already reported */
        goto cleanup;
    }

887
 cleanup:
888 889 890
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
891
    virObjectUnlock(driver);
892 893 894
    return ret;
}

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

904 905
    virCheckFlags(0, NULL);

906
    virObjectLock(driver);
907

908
    ifacedef = virInterfaceDefParseString(xml);
909 910 911 912 913
    if (!ifacedef) {
        /* error was already reported */
        goto cleanup;
    }

914 915 916
    if (virInterfaceDefineXMLEnsureACL(conn, ifacedef) < 0)
        goto cleanup;

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

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

936
 cleanup:
937 938 939
    ncf_if_free(iface);
    VIR_FREE(xmlstr);
    virInterfaceDefFree(ifacedef);
940
    virObjectUnlock(driver);
941 942 943
    return ret;
}

944 945
static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
{
946
    struct netcf_if *iface = NULL;
947
    virInterfaceDefPtr def = NULL;
948 949
    int ret = -1;

950
    virObjectLock(driver);
951 952 953 954 955 956 957

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

958 959 960 961 962 963 964

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

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

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

976
 cleanup:
977
    ncf_if_free(iface);
978
    virInterfaceDefFree(def);
979
    virObjectUnlock(driver);
980 981 982
    return ret;
}

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

991 992
    virCheckFlags(0, -1);

993
    virObjectLock(driver);
994 995 996 997 998 999 1000

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

1001 1002 1003 1004 1005 1006 1007

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

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

1008 1009 1010 1011 1012 1013 1014 1015 1016
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

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

1028
 cleanup:
1029
    ncf_if_free(iface);
1030
    virInterfaceDefFree(def);
1031
    virObjectUnlock(driver);
1032 1033 1034
    return ret;
}

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

1043 1044
    virCheckFlags(0, -1);

1045
    virObjectLock(driver);
1046 1047 1048 1049 1050 1051 1052

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

1053 1054 1055 1056 1057 1058 1059

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

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

1060 1061 1062 1063 1064 1065 1066 1067 1068
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
        goto cleanup;

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

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

1080
 cleanup:
1081
    ncf_if_free(iface);
1082
    virInterfaceDefFree(def);
1083
    virObjectUnlock(driver);
1084 1085 1086
    return ret;
}

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

1094
    virObjectLock(driver);
1095 1096 1097 1098 1099 1100 1101

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

1102 1103 1104 1105 1106 1107
    if (!(def = netcfGetMinimalDefForDevice(iface)))
        goto cleanup;

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

1108
    if (netcfInterfaceObjIsActive(iface, &active) < 0)
1109 1110
        goto cleanup;

1111
    ret = active ? 1 : 0;
1112

1113
 cleanup:
1114
    ncf_if_free(iface);
1115
    virInterfaceDefFree(def);
1116
    virObjectUnlock(driver);
1117 1118 1119
    return ret;
}

1120
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
1121 1122 1123 1124 1125
{
    int ret;

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

1126 1127 1128
    if (virInterfaceChangeBeginEnsureACL(conn) < 0)
        return -1;

1129
    virObjectLock(driver);
1130 1131 1132 1133 1134

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

1141
    virObjectUnlock(driver);
1142 1143 1144
    return ret;
}

1145
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
1146 1147 1148 1149 1150
{
    int ret;

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

1151 1152 1153
    if (virInterfaceChangeCommitEnsureACL(conn) < 0)
        return -1;

1154
    virObjectLock(driver);
1155 1156 1157 1158 1159

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

1166
    virObjectUnlock(driver);
1167 1168 1169
    return ret;
}

1170
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
1171 1172 1173 1174 1175
{
    int ret;

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

1176 1177 1178
    if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
        return -1;

1179
    virObjectLock(driver);
1180 1181 1182 1183 1184

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

1191
    virObjectUnlock(driver);
1192 1193 1194
    return ret;
}

1195
static virInterfaceDriver interfaceDriver = {
1196
    .name = INTERFACE_DRIVER_NAME,
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    .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 */
1213 1214
};

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

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


1234 1235 1236 1237 1238 1239 1240
static virStateDriver interfaceStateDriver = {
    .name = INTERFACE_DRIVER_NAME,
    .stateInitialize = netcfStateInitialize,
    .stateCleanup = netcfStateCleanup,
    .stateReload = netcfStateReload,
};

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