bridge_driver.c 48.1 KB
Newer Older
1 2 3
/*
 * driver.c: core driver methods for managing qemu guests
 *
4
 * Copyright (C) 2006-2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <strings.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/ioctl.h>

47
#include "virterror_internal.h"
48
#include "datatypes.h"
49
#include "bridge_driver.h"
50 51 52 53 54 55 56 57 58
#include "network_conf.h"
#include "driver.h"
#include "event.h"
#include "buf.h"
#include "util.h"
#include "memory.h"
#include "uuid.h"
#include "iptables.h"
#include "bridge.h"
59
#include "logging.h"
60

61 62
#define NETWORK_PID_DIR LOCAL_STATE_DIR "/run/libvirt/network"
#define NETWORK_STATE_DIR LOCAL_STATE_DIR "/lib/libvirt/network"
63 64 65

#define VIR_FROM_THIS VIR_FROM_NETWORK

66 67
/* Main driver state */
struct network_driver {
68
    virMutex lock;
69

70
    virNetworkObjList networks;
71 72 73 74 75 76 77 78

    iptablesContext *iptables;
    brControl *brctl;
    char *networkConfigDir;
    char *networkAutostartDir;
    char *logDir;
};

79 80 81

static void networkDriverLock(struct network_driver *driver)
{
82
    virMutexLock(&driver->lock);
83 84 85
}
static void networkDriverUnlock(struct network_driver *driver)
{
86
    virMutexUnlock(&driver->lock);
87 88
}

89 90 91 92 93 94 95 96 97 98
static int networkShutdown(void);

static int networkStartNetworkDaemon(virConnectPtr conn,
                                   struct network_driver *driver,
                                   virNetworkObjPtr network);

static int networkShutdownNetworkDaemon(virConnectPtr conn,
                                      struct network_driver *driver,
                                      virNetworkObjPtr network);

99 100
static void networkReloadIptablesRules(struct network_driver *driver);

101 102 103
static struct network_driver *driverState = NULL;


104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static void
networkFindActiveConfigs(struct network_driver *driver) {
    unsigned int i;

    for (i = 0 ; i < driver->networks.count ; i++) {
        virNetworkObjPtr obj = driver->networks.objs[i];
        virNetworkDefPtr tmp;
        char *config;

        virNetworkObjLock(obj);

        if ((config = virNetworkConfigFile(NULL,
                                           NETWORK_STATE_DIR,
                                           obj->def->name)) == NULL) {
            virNetworkObjUnlock(obj);
            continue;
        }

        if (access(config, R_OK) < 0) {
            VIR_FREE(config);
            virNetworkObjUnlock(obj);
            continue;
        }

        /* Try and load the live config */
        tmp = virNetworkDefParseFile(NULL, config);
        VIR_FREE(config);
        if (tmp) {
            obj->newDef = obj->def;
            obj->def = tmp;
        }

        /* If bridge exists, then mark it active */
        if (obj->def->bridge &&
            brHasBridge(driver->brctl, obj->def->bridge) == 0) {
            obj->active = 1;

141 142 143
            /* Finally try and read dnsmasq pid if any */
            if ((obj->def->ipAddress ||
                 obj->def->nranges) &&
144 145 146 147 148 149 150 151 152 153
                virFileReadPid(NETWORK_PID_DIR, obj->def->name,
                               &obj->dnsmasqPid) == 0) {

                /* Check its still alive */
                if (kill(obj->dnsmasqPid, 0) != 0)
                    obj->dnsmasqPid = -1;

#ifdef __linux__
                char *pidpath;

154 155 156 157
                if (virAsprintf(&pidpath, "/proc/%d/exe", obj->dnsmasqPid) < 0) {
                    virReportOOMError(NULL);
                    goto cleanup;
                }
158 159 160 161 162 163 164
                if (virFileLinkPointsTo(pidpath, DNSMASQ) == 0)
                    obj->dnsmasqPid = -1;
                VIR_FREE(pidpath);
#endif
            }
        }

165
    cleanup:
166 167 168 169 170
        virNetworkObjUnlock(obj);
    }
}


171 172 173
static void
networkAutostartConfigs(struct network_driver *driver) {
    unsigned int i;
174

175
    for (i = 0 ; i < driver->networks.count ; i++) {
176
        virNetworkObjLock(driver->networks.objs[i]);
177
        if (driver->networks.objs[i]->autostart &&
D
Daniel P. Berrange 已提交
178
            !virNetworkObjIsActive(driver->networks.objs[i]) &&
179
            networkStartNetworkDaemon(NULL, driver, driver->networks.objs[i]) < 0) {
180
            /* failed to start but already logged */
181
        }
182
        virNetworkObjUnlock(driver->networks.objs[i]);
183 184 185 186 187 188 189 190 191
    }
}

/**
 * networkStartup:
 *
 * Initialization function for the QEmu daemon
 */
static int
192
networkStartup(int privileged) {
193 194
    uid_t uid = geteuid();
    char *base = NULL;
195
    int err;
196 197

    if (VIR_ALLOC(driverState) < 0)
198
        goto error;
199

200 201 202 203
    if (virMutexInit(&driverState->lock) < 0) {
        VIR_FREE(driverState);
        goto error;
    }
204 205
    networkDriverLock(driverState);

206
    if (privileged) {
207 208
        if (virAsprintf(&driverState->logDir,
                        "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
209 210 211 212 213
            goto out_of_memory;

        if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
            goto out_of_memory;
    } else {
214 215 216 217
        char *userdir = virGetUserDirectory(NULL, uid);

        if (!userdir)
            goto error;
218

219
        if (virAsprintf(&driverState->logDir,
220 221
                        "%s/.libvirt/qemu/log", userdir) == -1) {
            VIR_FREE(userdir);
222
            goto out_of_memory;
223
        }
224

225 226
        if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
            VIR_FREE(userdir);
227 228
            goto out_of_memory;
        }
229
        VIR_FREE(userdir);
230 231 232 233 234
    }

    /* Configuration paths are either ~/.libvirt/qemu/... (session) or
     * /etc/libvirt/qemu/... (system).
     */
235
    if (virAsprintf(&driverState->networkConfigDir, "%s/qemu/networks", base) == -1)
236 237
        goto out_of_memory;

238 239
    if (virAsprintf(&driverState->networkAutostartDir, "%s/qemu/networks/autostart",
                    base) == -1)
240 241 242 243
        goto out_of_memory;

    VIR_FREE(base);

244 245 246 247 248 249 250
    if ((err = brInit(&driverState->brctl))) {
        virReportSystemError(NULL, err, "%s",
                             _("cannot initialize bridge support"));
        goto error;
    }

    if (!(driverState->iptables = iptablesContextNew())) {
251
        goto out_of_memory;
252 253 254
    }


255 256 257
    if (virNetworkLoadAllConfigs(NULL,
                                 &driverState->networks,
                                 driverState->networkConfigDir,
258 259 260
                                 driverState->networkAutostartDir) < 0)
        goto error;

261
    networkFindActiveConfigs(driverState);
262
    networkReloadIptablesRules(driverState);
263 264
    networkAutostartConfigs(driverState);

265 266
    networkDriverUnlock(driverState);

267 268
    return 0;

269
out_of_memory:
270
    virReportOOMError(NULL);
271 272

error:
273 274 275
    if (driverState)
        networkDriverUnlock(driverState);

276
    VIR_FREE(base);
277
    networkShutdown();
278 279 280 281 282 283 284 285 286 287 288
    return -1;
}

/**
 * networkReload:
 *
 * Function to restart the QEmu daemon, it will recheck the configuration
 * files and update its state and the networking
 */
static int
networkReload(void) {
289 290 291
    if (!driverState)
        return 0;

292
    networkDriverLock(driverState);
293 294 295 296
    virNetworkLoadAllConfigs(NULL,
                             &driverState->networks,
                             driverState->networkConfigDir,
                             driverState->networkAutostartDir);
297
    networkReloadIptablesRules(driverState);
298
    networkAutostartConfigs(driverState);
299
    networkDriverUnlock(driverState);
300 301 302 303 304 305 306 307 308 309 310 311 312
    return 0;
}

/**
 * networkActive:
 *
 * Checks if the QEmu daemon is active, i.e. has an active domain or
 * an active network
 *
 * Returns 1 if active, 0 otherwise
 */
static int
networkActive(void) {
313
    unsigned int i;
314
    int active = 0;
315

316 317 318
    if (!driverState)
        return 0;

319
    networkDriverLock(driverState);
320 321
    for (i = 0 ; i < driverState->networks.count ; i++) {
        virNetworkObjPtr net = driverState->networks.objs[i];
322
        virNetworkObjLock(net);
D
Daniel P. Berrange 已提交
323
        if (virNetworkObjIsActive(net))
324
            active = 1;
325
        virNetworkObjUnlock(net);
326
    }
327
    networkDriverUnlock(driverState);
328
    return active;
329 330 331 332 333 334 335 336 337 338 339 340
}

/**
 * networkShutdown:
 *
 * Shutdown the QEmu daemon, it will stop all active domains and networks
 */
static int
networkShutdown(void) {
    if (!driverState)
        return -1;

341 342
    networkDriverLock(driverState);

343
    /* free inactive networks */
344
    virNetworkObjListFree(&driverState->networks);
345 346 347 348 349 350 351 352 353 354

    VIR_FREE(driverState->logDir);
    VIR_FREE(driverState->networkConfigDir);
    VIR_FREE(driverState->networkAutostartDir);

    if (driverState->brctl)
        brShutdown(driverState->brctl);
    if (driverState->iptables)
        iptablesContextFree(driverState->iptables);

355
    networkDriverUnlock(driverState);
356
    virMutexDestroy(&driverState->lock);
357

358 359 360 361 362 363 364 365
    VIR_FREE(driverState);

    return 0;
}


static int
networkBuildDnsmasqArgv(virConnectPtr conn,
366 367 368
                        virNetworkObjPtr network,
                        const char *pidfile,
                        const char ***argv) {
369
    int i, len, r;
370
    int nbleases = 0;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    char *pidfileArg;
    char buf[1024];

    /*
     * NB, be careful about syntax for dnsmasq options in long format
     *
     * If the flag has a mandatory argument, it can be given using
     * either syntax:
     *
     *     --foo bar
     *     --foo=bar
     *
     * If the flag has a optional argument, it *must* be given using
     * the syntax:
     *
     *     --foo=bar
     *
     * It is hard to determine whether a flag is optional or not,
     * without reading the dnsmasq source :-( The manpages is not
     * very explicit on this
     */
392 393 394 395 396 397

    len =
        1 + /* dnsmasq */
        1 + /* --strict-order */
        1 + /* --bind-interfaces */
        (network->def->domain?2:0) + /* --domain name */
398
        2 + /* --pid-file /var/run/libvirt/network/$NAME.pid */
399 400 401 402 403
        2 + /* --conf-file "" */
        /*2 + *//* --interface virbr0 */
        2 + /* --except-interface lo */
        2 + /* --listen-address 10.0.0.1 */
        (2 * network->def->nranges) + /* --dhcp-range 10.0.0.2,10.0.0.254 */
404 405
        /* --dhcp-lease-max=xxx if needed */
        (network->def->nranges ? 0 : 1) +
406 407
        /*  --dhcp-host 01:23:45:67:89:0a,hostname,10.0.0.3 */
        (2 * network->def->nhosts) +
408 409
        /* --enable-tftp --tftp-root /srv/tftp */
        (network->def->tftproot ? 3 : 0) +
410
        /* --dhcp-boot pxeboot.img[,,12.34.56.78] */
411
        (network->def->bootfile ? 2 : 0) +
412 413 414 415 416 417 418 419 420 421
        1;  /* NULL */

    if (VIR_ALLOC_N(*argv, len) < 0)
        goto no_memory;

#define APPEND_ARG(v, n, s) do {     \
        if (!((v)[(n)] = strdup(s))) \
            goto no_memory;          \
    } while (0)

422 423 424
#define APPEND_ARG_LIT(v, n, s) \
        (v)[(n)] = s

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    i = 0;

    APPEND_ARG(*argv, i++, DNSMASQ);

    /*
     * Needed to ensure dnsmasq uses same algorithm for processing
     * multiple namedriver entries in /etc/resolv.conf as GLibC.
     */
    APPEND_ARG(*argv, i++, "--strict-order");
    APPEND_ARG(*argv, i++, "--bind-interfaces");

    if (network->def->domain) {
       APPEND_ARG(*argv, i++, "--domain");
       APPEND_ARG(*argv, i++, network->def->domain);
    }

441 442 443
    if (virAsprintf(&pidfileArg, "--pid-file=%s", pidfile) < 0)
        goto no_memory;
    APPEND_ARG_LIT(*argv, i++, pidfileArg);
444

445
    APPEND_ARG(*argv, i++, "--conf-file=");
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    APPEND_ARG(*argv, i++, "");

    /*
     * XXX does not actually work, due to some kind of
     * race condition setting up ipv6 addresses on the
     * interface. A sleep(10) makes it work, but that's
     * clearly not practical
     *
     * APPEND_ARG(*argv, i++, "--interface");
     * APPEND_ARG(*argv, i++, network->def->bridge);
     */
    APPEND_ARG(*argv, i++, "--listen-address");
    APPEND_ARG(*argv, i++, network->def->ipAddress);

    APPEND_ARG(*argv, i++, "--except-interface");
    APPEND_ARG(*argv, i++, "lo");

    for (r = 0 ; r < network->def->nranges ; r++) {
        snprintf(buf, sizeof(buf), "%s,%s",
                 network->def->ranges[r].start,
                 network->def->ranges[r].end);

        APPEND_ARG(*argv, i++, "--dhcp-range");
        APPEND_ARG(*argv, i++, buf);
470 471 472 473 474 475
        nbleases += network->def->ranges[r].size;
    }

    if (network->def->nranges > 0) {
        snprintf(buf, sizeof(buf), "--dhcp-lease-max=%d", nbleases);
        APPEND_ARG(*argv, i++, buf);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    }

    for (r = 0 ; r < network->def->nhosts ; r++) {
        virNetworkDHCPHostDefPtr host = &(network->def->hosts[r]);
        if ((host->mac) && (host->name)) {
            snprintf(buf, sizeof(buf), "%s,%s,%s",
                     host->mac, host->name, host->ip);
        } else if (host->mac) {
            snprintf(buf, sizeof(buf), "%s,%s",
                     host->mac, host->ip);
        } else if (host->name) {
            snprintf(buf, sizeof(buf), "%s,%s",
                     host->name, host->ip);
        } else
            continue;

        APPEND_ARG(*argv, i++, "--dhcp-host");
        APPEND_ARG(*argv, i++, buf);
    }

496 497 498 499 500 501
    if (network->def->tftproot) {
        APPEND_ARG(*argv, i++, "--enable-tftp");
        APPEND_ARG(*argv, i++, "--tftp-root");
        APPEND_ARG(*argv, i++, network->def->tftproot);
    }
    if (network->def->bootfile) {
502 503 504 505 506
        snprintf(buf, sizeof(buf), "%s%s%s",
                 network->def->bootfile,
                 network->def->bootserver ? ",," : "",
                 network->def->bootserver ? network->def->bootserver : "");

507
        APPEND_ARG(*argv, i++, "--dhcp-boot");
508
        APPEND_ARG(*argv, i++, buf);
509 510
    }

511 512 513 514 515
#undef APPEND_ARG

    return 0;

 no_memory:
516
    if (*argv) {
517 518 519 520
        for (i = 0; (*argv)[i]; i++)
            VIR_FREE((*argv)[i]);
        VIR_FREE(*argv);
    }
521
    virReportOOMError(conn);
522 523 524 525 526 527 528 529 530
    return -1;
}


static int
dhcpStartDhcpDaemon(virConnectPtr conn,
                    virNetworkObjPtr network)
{
    const char **argv;
531 532 533 534
    char *pidfile;
    int ret = -1, i, err;

    network->dnsmasqPid = -1;
535 536 537 538 539 540 541

    if (network->def->ipAddress == NULL) {
        networkReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot start dhcp daemon without IP address for server"));
        return -1;
    }

L
Laine Stump 已提交
542
    if ((err = virFileMakePath(NETWORK_PID_DIR)) != 0) {
543 544 545 546 547
        virReportSystemError(conn, err,
                             _("cannot create directory %s"),
                             NETWORK_PID_DIR);
        return -1;
    }
L
Laine Stump 已提交
548
    if ((err = virFileMakePath(NETWORK_STATE_DIR)) != 0) {
549 550 551 552 553 554 555 556 557 558 559
        virReportSystemError(conn, err,
                             _("cannot create directory %s"),
                             NETWORK_STATE_DIR);
        return -1;
    }

    if (!(pidfile = virFilePid(NETWORK_PID_DIR, network->def->name))) {
        virReportOOMError(conn);
        return -1;
    }

560
    argv = NULL;
561 562
    if (networkBuildDnsmasqArgv(conn, network, pidfile, &argv) < 0) {
        VIR_FREE(pidfile);
563
        return -1;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    }

    if (virRun(conn, argv, NULL) < 0)
        goto cleanup;

    /*
     * There really is no race here - when dnsmasq daemonizes,
     * its leader process stays around until its child has
     * actually written its pidfile. So by time virRun exits
     * it has waitpid'd and guaranteed the proess has started
     * and written a pid
     */

    if (virFileReadPid(NETWORK_PID_DIR, network->def->name,
                       &network->dnsmasqPid) < 0)
        goto cleanup;
580

581
    ret = 0;
582

583 584
cleanup:
    VIR_FREE(pidfile);
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    for (i = 0; argv[i]; i++)
        VIR_FREE(argv[i]);
    VIR_FREE(argv);

    return ret;
}

static int
networkAddMasqueradingIptablesRules(virConnectPtr conn,
                      struct network_driver *driver,
                      virNetworkObjPtr network) {
    int err;
    /* allow forwarding packets from the bridge interface */
    if ((err = iptablesAddForwardAllowOut(driver->iptables,
                                          network->def->network,
                                          network->def->bridge,
                                          network->def->forwardDev))) {
602 603 604
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow forwarding from '%s'"),
                             network->def->bridge);
605 606 607 608 609 610 611 612
        goto masqerr1;
    }

    /* allow forwarding packets to the bridge interface if they are part of an existing connection */
    if ((err = iptablesAddForwardAllowRelatedIn(driver->iptables,
                                         network->def->network,
                                         network->def->bridge,
                                         network->def->forwardDev))) {
613 614 615
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow forwarding to '%s'"),
                             network->def->bridge);
616 617 618 619 620 621 622
        goto masqerr2;
    }

    /* enable masquerading */
    if ((err = iptablesAddForwardMasquerade(driver->iptables,
                                            network->def->network,
                                            network->def->forwardDev))) {
623 624 625
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to enable masquerading to '%s'\n"),
                             network->def->forwardDev ? network->def->forwardDev : NULL);
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
        goto masqerr3;
    }

    return 1;

 masqerr3:
    iptablesRemoveForwardAllowRelatedIn(driver->iptables,
                                 network->def->network,
                                 network->def->bridge,
                                 network->def->forwardDev);
 masqerr2:
    iptablesRemoveForwardAllowOut(driver->iptables,
                                  network->def->network,
                                  network->def->bridge,
                                  network->def->forwardDev);
 masqerr1:
    return 0;
}

static int
networkAddRoutingIptablesRules(virConnectPtr conn,
                      struct network_driver *driver,
                      virNetworkObjPtr network) {
    int err;
    /* allow routing packets from the bridge interface */
    if ((err = iptablesAddForwardAllowOut(driver->iptables,
                                          network->def->network,
                                          network->def->bridge,
                                          network->def->forwardDev))) {
655 656 657
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow routing from '%s'"),
                             network->def->bridge);
658 659 660 661 662 663 664 665
        goto routeerr1;
    }

    /* allow routing packets to the bridge interface */
    if ((err = iptablesAddForwardAllowIn(driver->iptables,
                                         network->def->network,
                                         network->def->bridge,
                                         network->def->forwardDev))) {
666 667 668
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow routing to '%s'"),
                             network->def->bridge);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
        goto routeerr2;
    }

    return 1;


 routeerr2:
    iptablesRemoveForwardAllowOut(driver->iptables,
                                  network->def->network,
                                  network->def->bridge,
                                  network->def->forwardDev);
 routeerr1:
    return 0;
}

static int
networkAddIptablesRules(virConnectPtr conn,
                      struct network_driver *driver,
                      virNetworkObjPtr network) {
    int err;

    /* allow DHCP requests through to dnsmasq */
    if ((err = iptablesAddTcpInput(driver->iptables, network->def->bridge, 67))) {
692 693 694
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow DHCP requests from '%s'"),
                             network->def->bridge);
695 696 697 698
        goto err1;
    }

    if ((err = iptablesAddUdpInput(driver->iptables, network->def->bridge, 67))) {
699 700 701
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow DHCP requests from '%s'"),
                             network->def->bridge);
702 703 704 705 706
        goto err2;
    }

    /* allow DNS requests through to dnsmasq */
    if ((err = iptablesAddTcpInput(driver->iptables, network->def->bridge, 53))) {
707 708 709
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow DNS requests from '%s'"),
                             network->def->bridge);
710 711 712 713
        goto err3;
    }

    if ((err = iptablesAddUdpInput(driver->iptables, network->def->bridge, 53))) {
714 715 716
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow DNS requests from '%s'"),
                             network->def->bridge);
717 718 719 720 721 722 723
        goto err4;
    }


    /* Catch all rules to block forwarding to/from bridges */

    if ((err = iptablesAddForwardRejectOut(driver->iptables, network->def->bridge))) {
724 725 726
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to block outbound traffic from '%s'"),
                             network->def->bridge);
727 728 729 730
        goto err5;
    }

    if ((err = iptablesAddForwardRejectIn(driver->iptables, network->def->bridge))) {
731 732 733
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to block inbound traffic to '%s'"),
                             network->def->bridge);
734 735 736 737 738
        goto err6;
    }

    /* Allow traffic between guests on the same bridge */
    if ((err = iptablesAddForwardAllowCross(driver->iptables, network->def->bridge))) {
739 740 741
        virReportSystemError(conn, err,
                             _("failed to add iptables rule to allow cross bridge traffic on '%s'"),
                             network->def->bridge);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
        goto err7;
    }


    /* If masquerading is enabled, set up the rules*/
    if (network->def->forwardType == VIR_NETWORK_FORWARD_NAT &&
        !networkAddMasqueradingIptablesRules(conn, driver, network))
        goto err8;
    /* else if routing is enabled, set up the rules*/
    else if (network->def->forwardType == VIR_NETWORK_FORWARD_ROUTE &&
             !networkAddRoutingIptablesRules(conn, driver, network))
        goto err8;

    return 1;

 err8:
    iptablesRemoveForwardAllowCross(driver->iptables,
                                    network->def->bridge);
 err7:
    iptablesRemoveForwardRejectIn(driver->iptables,
                                  network->def->bridge);
 err6:
    iptablesRemoveForwardRejectOut(driver->iptables,
                                   network->def->bridge);
 err5:
    iptablesRemoveUdpInput(driver->iptables, network->def->bridge, 53);
 err4:
    iptablesRemoveTcpInput(driver->iptables, network->def->bridge, 53);
 err3:
    iptablesRemoveUdpInput(driver->iptables, network->def->bridge, 67);
 err2:
    iptablesRemoveTcpInput(driver->iptables, network->def->bridge, 67);
 err1:
    return 0;
}

static void
networkRemoveIptablesRules(struct network_driver *driver,
                         virNetworkObjPtr network) {
    if (network->def->forwardType != VIR_NETWORK_FORWARD_NONE) {
782 783 784 785
        if (network->def->forwardType == VIR_NETWORK_FORWARD_NAT) {
            iptablesRemoveForwardMasquerade(driver->iptables,
                                                network->def->network,
                                                network->def->forwardDev);
786 787 788 789
            iptablesRemoveForwardAllowRelatedIn(driver->iptables,
                                                network->def->network,
                                                network->def->bridge,
                                                network->def->forwardDev);
790
        } else if (network->def->forwardType == VIR_NETWORK_FORWARD_ROUTE)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
            iptablesRemoveForwardAllowIn(driver->iptables,
                                         network->def->network,
                                         network->def->bridge,
                                         network->def->forwardDev);

        iptablesRemoveForwardAllowOut(driver->iptables,
                                      network->def->network,
                                      network->def->bridge,
                                      network->def->forwardDev);
    }
    iptablesRemoveForwardAllowCross(driver->iptables, network->def->bridge);
    iptablesRemoveForwardRejectIn(driver->iptables, network->def->bridge);
    iptablesRemoveForwardRejectOut(driver->iptables, network->def->bridge);
    iptablesRemoveUdpInput(driver->iptables, network->def->bridge, 53);
    iptablesRemoveTcpInput(driver->iptables, network->def->bridge, 53);
    iptablesRemoveUdpInput(driver->iptables, network->def->bridge, 67);
    iptablesRemoveTcpInput(driver->iptables, network->def->bridge, 67);
}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
static void
networkReloadIptablesRules(struct network_driver *driver)
{
    unsigned int i;

    VIR_INFO0(_("Reloading iptables rules"));

    for (i = 0 ; i < driver->networks.count ; i++) {
        virNetworkObjLock(driver->networks.objs[i]);

        if (virNetworkObjIsActive(driver->networks.objs[i])) {
            networkRemoveIptablesRules(driver, driver->networks.objs[i]);
            if (!networkAddIptablesRules(NULL, driver, driver->networks.objs[i])) {
                /* failed to add but already logged */
            }
        }

        virNetworkObjUnlock(driver->networks.objs[i]);
    }
}

831
/* Enable IP Forwarding. Return 0 for success, -1 for failure. */
832 833 834
static int
networkEnableIpForwarding(void)
{
M
Mark McLoughlin 已提交
835
    return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n");
836 837
}

838 839 840 841 842 843 844 845 846 847 848 849 850
#define SYSCTL_PATH "/proc/sys"

static int networkDisableIPV6(virConnectPtr conn,
                              virNetworkObjPtr network)
{
    char *field = NULL;
    int ret = -1;

    if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/disable_ipv6", network->def->bridge) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

851 852 853 854 855 856
    if (access(field, W_OK) < 0 && errno == ENOENT) {
        VIR_DEBUG("ipv6 appears to already be disabled on %s", network->def->bridge);
        ret = 0;
        goto cleanup;
    }

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    if (virFileWriteStr(field, "1") < 0) {
        virReportSystemError(conn, errno,
                             _("cannot enable %s"), field);
        goto cleanup;
    }
    VIR_FREE(field);

    if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/accept_ra", network->def->bridge) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

    if (virFileWriteStr(field, "0") < 0) {
        virReportSystemError(conn, errno,
                             _("cannot disable %s"), field);
        goto cleanup;
    }
    VIR_FREE(field);

    if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/autoconf", network->def->bridge) < 0) {
        virReportOOMError(conn);
        goto cleanup;
    }

    if (virFileWriteStr(field, "1") < 0) {
        virReportSystemError(conn, errno,
                             _("cannot enable %s"), field);
        goto cleanup;
    }

    ret = 0;
cleanup:
    VIR_FREE(field);
    return ret;
}

893 894 895 896 897
static int networkStartNetworkDaemon(virConnectPtr conn,
                                   struct network_driver *driver,
                                   virNetworkObjPtr network) {
    int err;

D
Daniel P. Berrange 已提交
898
    if (virNetworkObjIsActive(network)) {
899 900 901 902 903
        networkReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("network is already active"));
        return -1;
    }

904
    if ((err = brAddBridge(driver->brctl, network->def->bridge))) {
905 906 907
        virReportSystemError(conn, err,
                             _("cannot create bridge '%s'"),
                             network->def->bridge);
908 909 910
        return -1;
    }

911 912 913
    if (networkDisableIPV6(conn, network) < 0)
        goto err_delbr;

914 915 916 917 918 919 920 921
    if (brSetForwardDelay(driver->brctl, network->def->bridge, network->def->delay) < 0)
        goto err_delbr;

    if (brSetEnableSTP(driver->brctl, network->def->bridge, network->def->stp ? 1 : 0) < 0)
        goto err_delbr;

    if (network->def->ipAddress &&
        (err = brSetInetAddress(driver->brctl, network->def->bridge, network->def->ipAddress))) {
922 923 924
        virReportSystemError(conn, err,
                             _("cannot set IP address on bridge '%s' to '%s'"),
                             network->def->bridge, network->def->ipAddress);
925 926 927 928 929
        goto err_delbr;
    }

    if (network->def->netmask &&
        (err = brSetInetNetmask(driver->brctl, network->def->bridge, network->def->netmask))) {
930 931 932
        virReportSystemError(conn, err,
                             _("cannot set netmask on bridge '%s' to '%s'"),
                             network->def->bridge, network->def->netmask);
933 934 935
        goto err_delbr;
    }

936
    if ((err = brSetInterfaceUp(driver->brctl, network->def->bridge, 1))) {
937 938 939
        virReportSystemError(conn, err,
                             _("failed to bring the bridge '%s' up"),
                             network->def->bridge);
940 941 942 943 944 945 946
        goto err_delbr;
    }

    if (!networkAddIptablesRules(conn, driver, network))
        goto err_delbr1;

    if (network->def->forwardType != VIR_NETWORK_FORWARD_NONE &&
947
        networkEnableIpForwarding() < 0) {
948 949
        virReportSystemError(conn, errno, "%s",
                             _("failed to enable IP forwarding"));
950 951 952
        goto err_delbr2;
    }

953 954
    if ((network->def->ipAddress ||
         network->def->nranges) &&
955 956 957
        dhcpStartDhcpDaemon(conn, network) < 0)
        goto err_delbr2;

958 959 960 961 962 963

    /* Persist the live configuration now we have bridge info  */
    if (virNetworkSaveConfig(conn, NETWORK_STATE_DIR, network->def) < 0) {
        goto err_kill;
    }

964 965 966 967
    network->active = 1;

    return 0;

968 969 970 971 972 973
 err_kill:
    if (network->dnsmasqPid > 0) {
        kill(network->dnsmasqPid, SIGTERM);
        network->dnsmasqPid = -1;
    }

974 975 976 977
 err_delbr2:
    networkRemoveIptablesRules(driver, network);

 err_delbr1:
978
    if ((err = brSetInterfaceUp(driver->brctl, network->def->bridge, 0))) {
979
        char ebuf[1024];
980
        VIR_WARN(_("Failed to bring down bridge '%s' : %s"),
981
                 network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
982 983 984 985
    }

 err_delbr:
    if ((err = brDeleteBridge(driver->brctl, network->def->bridge))) {
986
        char ebuf[1024];
987
        VIR_WARN(_("Failed to delete bridge '%s' : %s"),
988
                 network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
989 990 991 992 993 994
    }

    return -1;
}


995 996 997
static int networkShutdownNetworkDaemon(virConnectPtr conn,
                                        struct network_driver *driver,
                                        virNetworkObjPtr network) {
998
    int err;
999
    char *stateFile;
1000

1001
    VIR_INFO(_("Shutting down network '%s'"), network->def->name);
1002

D
Daniel P. Berrange 已提交
1003
    if (!virNetworkObjIsActive(network))
1004 1005
        return 0;

1006 1007 1008 1009 1010 1011 1012
    stateFile = virNetworkConfigFile(conn, NETWORK_STATE_DIR, network->def->name);
    if (!stateFile)
        return -1;

    unlink(stateFile);
    VIR_FREE(stateFile);

1013 1014 1015 1016 1017
    if (network->dnsmasqPid > 0)
        kill(network->dnsmasqPid, SIGTERM);

    networkRemoveIptablesRules(driver, network);

1018
    char ebuf[1024];
1019
    if ((err = brSetInterfaceUp(driver->brctl, network->def->bridge, 0))) {
1020
        VIR_WARN(_("Failed to bring down bridge '%s' : %s"),
1021
                 network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
1022 1023 1024
    }

    if ((err = brDeleteBridge(driver->brctl, network->def->bridge))) {
1025
        VIR_WARN(_("Failed to delete bridge '%s' : %s"),
1026
                 network->def->bridge, virStrerror(err, ebuf, sizeof ebuf));
1027 1028
    }

1029
    /* See if its still alive and really really kill it */
1030
    if (network->dnsmasqPid > 0 &&
1031
        (kill(network->dnsmasqPid, 0) == 0))
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        kill(network->dnsmasqPid, SIGKILL);

    network->dnsmasqPid = -1;
    network->active = 0;

    if (network->newDef) {
        virNetworkDefFree(network->def);
        network->def = network->newDef;
        network->newDef = NULL;
    }

    return 0;
}


1047 1048 1049 1050 1051
static virNetworkPtr networkLookupByUUID(virConnectPtr conn,
                                         const unsigned char *uuid) {
    struct network_driver *driver = conn->networkPrivateData;
    virNetworkObjPtr network;
    virNetworkPtr ret = NULL;
1052

1053
    networkDriverLock(driver);
1054
    network = virNetworkFindByUUID(&driver->networks, uuid);
1055
    networkDriverUnlock(driver);
1056 1057 1058
    if (!network) {
        networkReportError(conn, NULL, NULL, VIR_ERR_NO_NETWORK,
                         "%s", _("no network with matching uuid"));
1059
        goto cleanup;
1060 1061
    }

1062 1063 1064
    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

cleanup:
1065 1066
    if (network)
        virNetworkObjUnlock(network);
1067
    return ret;
1068 1069
}

1070 1071 1072 1073 1074 1075
static virNetworkPtr networkLookupByName(virConnectPtr conn,
                                         const char *name) {
    struct network_driver *driver = conn->networkPrivateData;
    virNetworkObjPtr network;
    virNetworkPtr ret = NULL;

1076
    networkDriverLock(driver);
1077
    network = virNetworkFindByName(&driver->networks, name);
1078
    networkDriverUnlock(driver);
1079 1080
    if (!network) {
        networkReportError(conn, NULL, NULL, VIR_ERR_NO_NETWORK,
1081
                         _("no network with matching name '%s'"), name);
1082
        goto cleanup;
1083 1084
    }

1085 1086 1087
    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

cleanup:
1088 1089
    if (network)
        virNetworkObjUnlock(network);
1090
    return ret;
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
}

static virDrvOpenStatus networkOpenNetwork(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           int flags ATTRIBUTE_UNUSED) {
    if (!driverState)
        return VIR_DRV_OPEN_DECLINED;

    conn->networkPrivateData = driverState;
    return VIR_DRV_OPEN_SUCCESS;
}

static int networkCloseNetwork(virConnectPtr conn) {
    conn->networkPrivateData = NULL;
    return 0;
}

static int networkNumNetworks(virConnectPtr conn) {
1109
    int nactive = 0, i;
1110
    struct network_driver *driver = conn->networkPrivateData;
1111

1112 1113 1114
    networkDriverLock(driver);
    for (i = 0 ; i < driver->networks.count ; i++) {
        virNetworkObjLock(driver->networks.objs[i]);
D
Daniel P. Berrange 已提交
1115
        if (virNetworkObjIsActive(driver->networks.objs[i]))
1116
            nactive++;
1117 1118 1119
        virNetworkObjUnlock(driver->networks.objs[i]);
    }
    networkDriverUnlock(driver);
1120

1121 1122 1123 1124
    return nactive;
}

static int networkListNetworks(virConnectPtr conn, char **const names, int nnames) {
1125
    struct network_driver *driver = conn->networkPrivateData;
1126
    int got = 0, i;
1127

1128
    networkDriverLock(driver);
1129
    for (i = 0 ; i < driver->networks.count && got < nnames ; i++) {
1130
        virNetworkObjLock(driver->networks.objs[i]);
D
Daniel P. Berrange 已提交
1131
        if (virNetworkObjIsActive(driver->networks.objs[i])) {
1132
            if (!(names[got] = strdup(driver->networks.objs[i]->def->name))) {
1133
                virNetworkObjUnlock(driver->networks.objs[i]);
1134
                virReportOOMError(conn);
1135 1136 1137 1138
                goto cleanup;
            }
            got++;
        }
1139
        virNetworkObjUnlock(driver->networks.objs[i]);
1140
    }
1141 1142
    networkDriverUnlock(driver);

1143 1144 1145
    return got;

 cleanup:
1146
    networkDriverUnlock(driver);
1147 1148 1149 1150 1151 1152
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
    return -1;
}

static int networkNumDefinedNetworks(virConnectPtr conn) {
1153
    int ninactive = 0, i;
1154
    struct network_driver *driver = conn->networkPrivateData;
1155

1156 1157 1158
    networkDriverLock(driver);
    for (i = 0 ; i < driver->networks.count ; i++) {
        virNetworkObjLock(driver->networks.objs[i]);
D
Daniel P. Berrange 已提交
1159
        if (!virNetworkObjIsActive(driver->networks.objs[i]))
1160
            ninactive++;
1161 1162 1163
        virNetworkObjUnlock(driver->networks.objs[i]);
    }
    networkDriverUnlock(driver);
1164

1165 1166 1167 1168
    return ninactive;
}

static int networkListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
1169
    struct network_driver *driver = conn->networkPrivateData;
1170
    int got = 0, i;
1171

1172
    networkDriverLock(driver);
1173
    for (i = 0 ; i < driver->networks.count && got < nnames ; i++) {
1174
        virNetworkObjLock(driver->networks.objs[i]);
D
Daniel P. Berrange 已提交
1175
        if (!virNetworkObjIsActive(driver->networks.objs[i])) {
1176
            if (!(names[got] = strdup(driver->networks.objs[i]->def->name))) {
1177
                virNetworkObjUnlock(driver->networks.objs[i]);
1178
                virReportOOMError(conn);
1179 1180 1181 1182
                goto cleanup;
            }
            got++;
        }
1183
        virNetworkObjUnlock(driver->networks.objs[i]);
1184
    }
1185
    networkDriverUnlock(driver);
1186 1187 1188
    return got;

 cleanup:
1189
    networkDriverUnlock(driver);
1190 1191 1192 1193 1194
    for (i = 0 ; i < got ; i++)
        VIR_FREE(names[i]);
    return -1;
}

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238

static int networkIsActive(virNetworkPtr net)
{
    struct network_driver *driver = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    networkDriverLock(driver);
    obj = virNetworkFindByUUID(&driver->networks, net->uuid);
    networkDriverUnlock(driver);
    if (!obj) {
        networkReportError(net->conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

cleanup:
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}

static int networkIsPersistent(virNetworkPtr net)
{
    struct network_driver *driver = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    networkDriverLock(driver);
    obj = virNetworkFindByUUID(&driver->networks, net->uuid);
    networkDriverUnlock(driver);
    if (!obj) {
        networkReportError(net->conn, NULL, NULL, VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}


1239
static virNetworkPtr networkCreate(virConnectPtr conn, const char *xml) {
1240
    struct network_driver *driver = conn->networkPrivateData;
1241
    virNetworkDefPtr def;
1242
    virNetworkObjPtr network = NULL;
1243
    virNetworkPtr ret = NULL;
1244

1245 1246
    networkDriverLock(driver);

1247
    if (!(def = virNetworkDefParseString(conn, xml)))
1248
        goto cleanup;
1249

1250
    if (virNetworkSetBridgeName(conn, &driver->networks, def, 1))
1251 1252
        goto cleanup;

1253 1254
    if (!(network = virNetworkAssignDef(conn,
                                        &driver->networks,
1255 1256 1257
                                        def)))
        goto cleanup;
    def = NULL;
1258 1259 1260 1261

    if (networkStartNetworkDaemon(conn, driver, network) < 0) {
        virNetworkRemoveInactive(&driver->networks,
                                 network);
1262
        network = NULL;
1263
        goto cleanup;
1264 1265
    }

1266 1267 1268 1269
    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

cleanup:
    virNetworkDefFree(def);
1270 1271 1272
    if (network)
        virNetworkObjUnlock(network);
    networkDriverUnlock(driver);
1273
    return ret;
1274 1275 1276
}

static virNetworkPtr networkDefine(virConnectPtr conn, const char *xml) {
1277
    struct network_driver *driver = conn->networkPrivateData;
1278
    virNetworkDefPtr def;
1279
    virNetworkObjPtr network = NULL;
1280
    virNetworkPtr ret = NULL;
1281

1282 1283
    networkDriverLock(driver);

1284
    if (!(def = virNetworkDefParseString(conn, xml)))
1285
        goto cleanup;
1286

1287
    if (virNetworkSetBridgeName(conn, &driver->networks, def, 1))
1288 1289
        goto cleanup;

1290 1291
    if (!(network = virNetworkAssignDef(conn,
                                        &driver->networks,
1292 1293 1294
                                        def)))
        goto cleanup;
    def = NULL;
1295

1296 1297
    network->persistent = 1;

1298 1299
    if (virNetworkSaveConfig(conn,
                             driver->networkConfigDir,
1300
                             network->newDef ? network->newDef : network->def) < 0) {
1301 1302
        virNetworkRemoveInactive(&driver->networks,
                                 network);
1303
        network = NULL;
1304
        goto cleanup;
1305 1306
    }

1307 1308 1309 1310
    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

cleanup:
    virNetworkDefFree(def);
1311 1312 1313
    if (network)
        virNetworkObjUnlock(network);
    networkDriverUnlock(driver);
1314
    return ret;
1315 1316 1317
}

static int networkUndefine(virNetworkPtr net) {
1318
    struct network_driver *driver = net->conn->networkPrivateData;
1319
    virNetworkObjPtr network = NULL;
1320
    int ret = -1;
1321

1322 1323
    networkDriverLock(driver);

1324
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1325
    if (!network) {
1326
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
1327 1328
                           "%s", _("no network with matching uuid"));
        goto cleanup;
1329 1330
    }

D
Daniel P. Berrange 已提交
1331
    if (virNetworkObjIsActive(network)) {
1332
        networkReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
1333 1334
                           "%s", _("network is still active"));
        goto cleanup;
1335 1336
    }

1337 1338 1339 1340
    if (virNetworkDeleteConfig(net->conn,
                               driver->networkConfigDir,
                               driver->networkAutostartDir,
                               network) < 0)
1341
        goto cleanup;
1342 1343 1344

    virNetworkRemoveInactive(&driver->networks,
                             network);
1345
    network = NULL;
1346
    ret = 0;
1347

1348
cleanup:
1349 1350 1351
    if (network)
        virNetworkObjUnlock(network);
    networkDriverUnlock(driver);
1352
    return ret;
1353 1354 1355
}

static int networkStart(virNetworkPtr net) {
1356 1357 1358
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
    int ret = -1;
1359

1360
    networkDriverLock(driver);
1361
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1362

1363 1364
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
1365 1366
                           "%s", _("no network with matching uuid"));
        goto cleanup;
1367 1368
    }

1369 1370 1371
    ret = networkStartNetworkDaemon(net->conn, driver, network);

cleanup:
1372 1373
    if (network)
        virNetworkObjUnlock(network);
1374
    networkDriverUnlock(driver);
1375
    return ret;
1376 1377 1378
}

static int networkDestroy(virNetworkPtr net) {
1379 1380 1381
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
    int ret = -1;
1382

1383
    networkDriverLock(driver);
1384
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1385

1386 1387
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
1388 1389
                           "%s", _("no network with matching uuid"));
        goto cleanup;
1390 1391
    }

D
Daniel P. Berrange 已提交
1392
    if (!virNetworkObjIsActive(network)) {
1393 1394 1395 1396 1397
        networkReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                           "%s", _("network is not active"));
        goto cleanup;
    }

1398
    ret = networkShutdownNetworkDaemon(net->conn, driver, network);
1399
    if (!network->persistent) {
1400 1401 1402 1403
        virNetworkRemoveInactive(&driver->networks,
                                 network);
        network = NULL;
    }
1404

1405
cleanup:
1406 1407
    if (network)
        virNetworkObjUnlock(network);
1408
    networkDriverUnlock(driver);
1409 1410 1411 1412
    return ret;
}

static char *networkDumpXML(virNetworkPtr net, int flags ATTRIBUTE_UNUSED) {
1413 1414 1415
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
    char *ret = NULL;
1416

1417
    networkDriverLock(driver);
1418
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1419 1420
    networkDriverUnlock(driver);

1421 1422
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
1423 1424
                           "%s", _("no network with matching uuid"));
        goto cleanup;
1425 1426
    }

1427 1428 1429
    ret = virNetworkDefFormat(net->conn, network->def);

cleanup:
1430 1431
    if (network)
        virNetworkObjUnlock(network);
1432
    return ret;
1433 1434 1435
}

static char *networkGetBridgeName(virNetworkPtr net) {
1436 1437 1438 1439
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
    char *bridge = NULL;

1440
    networkDriverLock(driver);
1441
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1442 1443
    networkDriverUnlock(driver);

1444 1445
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
1446 1447
                           "%s", _("no network with matching id"));
        goto cleanup;
1448 1449
    }

1450 1451 1452 1453 1454 1455 1456
    if (!(network->def->bridge)) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                           _("network '%s' does not have a bridge name."),
                           network->def->name);
        goto cleanup;
    }

1457
    bridge = strdup(network->def->bridge);
1458
    if (!bridge)
1459
        virReportOOMError(net->conn);
1460 1461

cleanup:
1462 1463
    if (network)
        virNetworkObjUnlock(network);
1464 1465 1466 1467 1468
    return bridge;
}

static int networkGetAutostart(virNetworkPtr net,
                             int *autostart) {
1469 1470 1471
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
    int ret = -1;
1472

1473
    networkDriverLock(driver);
1474
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1475
    networkDriverUnlock(driver);
1476 1477 1478
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
                         "%s", _("no network with matching uuid"));
1479
        goto cleanup;
1480 1481 1482
    }

    *autostart = network->autostart;
1483
    ret = 0;
1484

1485
cleanup:
1486 1487
    if (network)
        virNetworkObjUnlock(network);
1488
    return ret;
1489 1490 1491
}

static int networkSetAutostart(virNetworkPtr net,
1492
                               int autostart) {
1493 1494
    struct network_driver *driver = net->conn->networkPrivateData;
    virNetworkObjPtr network;
1495
    char *configFile = NULL, *autostartLink = NULL;
1496
    int ret = -1;
1497

1498
    networkDriverLock(driver);
1499
    network = virNetworkFindByUUID(&driver->networks, net->uuid);
1500

1501 1502 1503
    if (!network) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
                         "%s", _("no network with matching uuid"));
1504
        goto cleanup;
1505 1506
    }

1507 1508 1509 1510 1511 1512
    if (!network->persistent) {
        networkReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                         "%s", _("cannot set autostart for transient network"));
        goto cleanup;
    }

1513 1514
    autostart = (autostart != 0);

1515
    if (network->autostart != autostart) {
1516 1517 1518 1519 1520
        if ((configFile = virNetworkConfigFile(net->conn, driver->networkConfigDir, network->def->name)) == NULL)
            goto cleanup;
        if ((autostartLink = virNetworkConfigFile(net->conn, driver->networkAutostartDir, network->def->name)) == NULL)
            goto cleanup;

1521
        if (autostart) {
1522
            if (virFileMakePath(driver->networkAutostartDir)) {
1523 1524 1525
                virReportSystemError(net->conn, errno,
                                     _("cannot create autostart directory '%s'"),
                                     driver->networkAutostartDir);
1526 1527
                goto cleanup;
            }
1528

1529
            if (symlink(configFile, autostartLink) < 0) {
1530 1531
                virReportSystemError(net->conn, errno,
                                     _("Failed to create symlink '%s' to '%s'"),
1532
                                     autostartLink, configFile);
1533 1534 1535
                goto cleanup;
            }
        } else {
1536
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
1537 1538
                virReportSystemError(net->conn, errno,
                                     _("Failed to delete symlink '%s'"),
1539
                                     autostartLink);
1540 1541
                goto cleanup;
            }
1542 1543
        }

1544
        network->autostart = autostart;
1545
    }
1546
    ret = 0;
1547

1548
cleanup:
1549 1550
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
1551 1552
    if (network)
        virNetworkObjUnlock(network);
1553
    networkDriverUnlock(driver);
1554
    return ret;
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
}


static virNetworkDriver networkDriver = {
    "Network",
    networkOpenNetwork, /* open */
    networkCloseNetwork, /* close */
    networkNumNetworks, /* numOfNetworks */
    networkListNetworks, /* listNetworks */
    networkNumDefinedNetworks, /* numOfDefinedNetworks */
    networkListDefinedNetworks, /* listDefinedNetworks */
    networkLookupByUUID, /* networkLookupByUUID */
    networkLookupByName, /* networkLookupByName */
    networkCreate, /* networkCreateXML */
    networkDefine, /* networkDefineXML */
    networkUndefine, /* networkUndefine */
    networkStart, /* networkCreate */
    networkDestroy, /* networkDestroy */
    networkDumpXML, /* networkDumpXML */
    networkGetBridgeName, /* networkGetBridgeName */
    networkGetAutostart, /* networkGetAutostart */
    networkSetAutostart, /* networkSetAutostart */
1577 1578
    networkIsActive,
    networkIsPersistent,
1579 1580 1581
};

static virStateDriver networkStateDriver = {
1582
    "Network",
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    networkStartup,
    networkShutdown,
    networkReload,
    networkActive,
};

int networkRegister(void) {
    virRegisterNetworkDriver(&networkDriver);
    virRegisterStateDriver(&networkStateDriver);
    return 0;
}