All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu
@ 2011-12-19 13:11 Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 1/4] Add basic version of bridge helper Corey Bryant
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 13:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, rmarwah

With qemu it is possible to run a guest from an unprivileged user but if
we wanted to communicate with the outside world we had to switch
to root.

We address this problem by introducing a new network backend and a new
network option for -net tap.  This is less flexible when compared to
existing -net tap options because it relies on a helper with elevated
privileges to do the heavy lifting of allocating and attaching a tap
device to a bridge.  We use a special purpose helper because we don't
want to elevate the privileges of more generic tools like brctl.

Qemu can be run with the default network helper as follows (in these cases
attaching the tap device to the default br0 bridge):

  qemu linux.img -net bridge -net nic,model=virtio

  qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
                 -net nic,model=virtio

  qemu linux.img -netdev bridge,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

  qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

The default helper uses it's own ACL mechanism for access control, but
future network helpers could be developed, for example, to support PolicyKit
for access control.

More details are included in individual patches.  The helper is broken into
a series of patches to improve reviewabilty.

v2:
 - Updated signed-off-by's
 - Updated author's email
 - Set default bridge to br0
 - Added -net bridge
 - Updated ACL example
 - Moved from libcap to libcap-ng
 - Fail helper when libcap-ng not configured

v3:
 - Use simple queue to store ACLs
 - Added goto cleanup to helper's main
 - Allow helper execution if libcap-ng not configured
 - Completed static analysis and memory analysis on helper

v4:
 - Update has_vnet_hdr() to return bool
 - Update helper's main() to prevent errno clobbering
 - Let Kernel cleanup helper's file descriptors

v5:
 - Removed if statement with TUNGETIFF ioctl() from has_vnet_hdr()
 - Added -netdev examples and udpated qemu -help netdev documentation
 - Disallow vnet_hdr option with -net tap,helper

v6:
 - Fixed uninitialized variable (TAPState *s) in net_tap_init()

Corey Bryant (4):
  Add basic version of bridge helper
  Add access control support to qemu bridge helper
  Add cap reduction support to enable use as SUID
  Add support for net bridge

 Makefile             |   12 ++-
 configure            |   37 +++++
 net.c                |   29 ++++-
 net.h                |    3 +
 net/tap.c            |  187 +++++++++++++++++++++++-
 net/tap.h            |    3 +
 qemu-bridge-helper.c |  402 ++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx      |   74 ++++++++--
 8 files changed, 728 insertions(+), 19 deletions(-)
 create mode 100644 qemu-bridge-helper.c

-- 
1.7.3.4

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH v6 1/4] Add basic version of bridge helper
  2011-12-19 13:11 [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu Corey Bryant
@ 2011-12-19 13:11 ` Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 2/4] Add access control support to qemu " Corey Bryant
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 13:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, rmarwah

This patch adds a helper that can be used to create a tap device attached to
a bridge device.  Since this helper is minimal in what it does, it can be
given CAP_NET_ADMIN which allows qemu to avoid running as root while still
satisfying the majority of what users tend to want to do with tap devices.

The way this all works is that qemu launches this helper passing a bridge
name and the name of an inherited file descriptor.  The descriptor is one
end of a socketpair() of domain sockets.  This domain socket is used to
transmit a file descriptor of the opened tap device from the helper to qemu.

The helper can then exit and let qemu use the tap device.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
---
 Makefile             |   12 +++-
 configure            |    1 +
 qemu-bridge-helper.c |  213 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 224 insertions(+), 2 deletions(-)
 create mode 100644 qemu-bridge-helper.c

diff --git a/Makefile b/Makefile
index 2c03055..96dc4bd 100644
--- a/Makefile
+++ b/Makefile
@@ -36,6 +36,8 @@ $(call set-vpath, $(SRC_PATH):$(SRC_PATH)/hw)
 
 LIBS+=-lz $(LIBS_TOOLS)
 
+HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
+
 ifdef BUILD_DOCS
 DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 QMP/qmp-commands.txt
 else
@@ -76,7 +78,7 @@ defconfig:
 
 -include config-all-devices.mak
 
-build-all: $(DOCS) $(TOOLS) $(CHECKS) recurse-all
+build-all: $(DOCS) $(TOOLS) $(CHECKS) $(HELPERS-y) recurse-all
 
 config-host.h: config-host.h-timestamp
 config-host.h-timestamp: config-host.mak
@@ -154,6 +156,8 @@ qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
 qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y)
 qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
+qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
+
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
 
@@ -224,7 +228,7 @@ clean:
 # avoid old build problems by removing potentially incorrect old files
 	rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h
 	rm -f qemu-options.def
-	rm -f *.o *.d *.a *.lo $(TOOLS) $(CHECKS) qemu-ga TAGS cscope.* *.pod *~ */*~
+	rm -f *.o *.d *.a *.lo $(TOOLS) $(CHECKS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 	rm -Rf .libs
 	rm -f slirp/*.o slirp/*.d audio/*.o audio/*.d block/*.o block/*.d net/*.o net/*.d fsdev/*.o fsdev/*.d ui/*.o ui/*.d qapi/*.o qapi/*.d qga/*.o qga/*.d
 	rm -f qemu-img-cmds.h
@@ -293,6 +297,10 @@ install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig
 ifneq ($(TOOLS),)
 	$(INSTALL_PROG) $(STRIP_OPT) $(TOOLS) "$(DESTDIR)$(bindir)"
 endif
+ifneq ($(HELPERS-y),)
+	$(INSTALL_DIR) "$(DESTDIR)$(libexecdir)"
+	$(INSTALL_PROG) $(STRIP_OPT) $(HELPERS-y) "$(DESTDIR)$(libexecdir)"
+endif
 ifneq ($(BLOBS),)
 	$(INSTALL_DIR) "$(DESTDIR)$(datadir)"
 	set -e; for x in $(BLOBS); do \
diff --git a/configure b/configure
index 6fd580e..6c3a2f3 100755
--- a/configure
+++ b/configure
@@ -2873,6 +2873,7 @@ echo "datadir=$datadir" >> $config_host_mak
 echo "sysconfdir=$sysconfdir" >> $config_host_mak
 echo "docdir=$docdir" >> $config_host_mak
 echo "confdir=$confdir" >> $config_host_mak
+echo "libexecdir=\${prefix}/libexec" >> $config_host_mak
 
 case "$cpu" in
   i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
diff --git a/qemu-bridge-helper.c b/qemu-bridge-helper.c
new file mode 100644
index 0000000..e0ba917
--- /dev/null
+++ b/qemu-bridge-helper.c
@@ -0,0 +1,213 @@
+/*
+ * QEMU Bridge Helper
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ * Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "config-host.h"
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <ctype.h>
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/prctl.h>
+
+#include <net/if.h>
+
+#include <linux/sockios.h>
+
+#include "net/tap-linux.h"
+
+static bool has_vnet_hdr(int fd)
+{
+    unsigned int features = 0;
+
+    if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
+        return false;
+    }
+
+    if (!(features & IFF_VNET_HDR)) {
+        return false;
+    }
+
+    return true;
+}
+
+static void prep_ifreq(struct ifreq *ifr, const char *ifname)
+{
+    memset(ifr, 0, sizeof(*ifr));
+    snprintf(ifr->ifr_name, IFNAMSIZ, "%s", ifname);
+}
+
+static int send_fd(int c, int fd)
+{
+    char msgbuf[CMSG_SPACE(sizeof(fd))];
+    struct msghdr msg = {
+        .msg_control = msgbuf,
+        .msg_controllen = sizeof(msgbuf),
+    };
+    struct cmsghdr *cmsg;
+    struct iovec iov;
+    char req[1] = { 0x00 };
+
+    cmsg = CMSG_FIRSTHDR(&msg);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+    msg.msg_controllen = cmsg->cmsg_len;
+
+    iov.iov_base = req;
+    iov.iov_len = sizeof(req);
+
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+    memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
+
+    return sendmsg(c, &msg, 0);
+}
+
+int main(int argc, char **argv)
+{
+    struct ifreq ifr;
+    int fd, ctlfd, unixfd;
+    int use_vnet = 0;
+    int mtu;
+    const char *bridge;
+    char iface[IFNAMSIZ];
+    int index;
+    int ret = EXIT_SUCCESS;
+
+    /* parse arguments */
+    if (argc < 3 || argc > 4) {
+        fprintf(stderr, "Usage: %s [--use-vnet] BRIDGE FD\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    index = 1;
+    if (strcmp(argv[index], "--use-vnet") == 0) {
+        use_vnet = 1;
+        index++;
+        if (argc == 3) {
+            fprintf(stderr, "invalid number of arguments\n");
+            return EXIT_FAILURE;
+        }
+    }
+
+    bridge = argv[index++];
+    unixfd = atoi(argv[index++]);
+
+    /* open a socket to use to control the network interfaces */
+    ctlfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (ctlfd == -1) {
+        fprintf(stderr, "failed to open control socket: %s\n", strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* open the tap device */
+    fd = open("/dev/net/tun", O_RDWR);
+    if (fd == -1) {
+        fprintf(stderr, "failed to open /dev/net/tun: %s\n", strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* request a tap device, disable PI, and add vnet header support if
+     * requested and it's available. */
+    prep_ifreq(&ifr, "tap%d");
+    ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
+    if (use_vnet && has_vnet_hdr(fd)) {
+        ifr.ifr_flags |= IFF_VNET_HDR;
+    }
+
+    if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
+        fprintf(stderr, "failed to create tun device: %s\n", strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* save tap device name */
+    snprintf(iface, sizeof(iface), "%s", ifr.ifr_name);
+
+    /* get the mtu of the bridge */
+    prep_ifreq(&ifr, bridge);
+    if (ioctl(ctlfd, SIOCGIFMTU, &ifr) == -1) {
+        fprintf(stderr, "failed to get mtu of bridge `%s': %s\n",
+                bridge, strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* save mtu */
+    mtu = ifr.ifr_mtu;
+
+    /* set the mtu of the interface based on the bridge */
+    prep_ifreq(&ifr, iface);
+    ifr.ifr_mtu = mtu;
+    if (ioctl(ctlfd, SIOCSIFMTU, &ifr) == -1) {
+        fprintf(stderr, "failed to set mtu of device `%s' to %d: %s\n",
+                iface, mtu, strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* add the interface to the bridge */
+    prep_ifreq(&ifr, bridge);
+    ifr.ifr_ifindex = if_nametoindex(iface);
+
+    if (ioctl(ctlfd, SIOCBRADDIF, &ifr) == -1) {
+        fprintf(stderr, "failed to add interface `%s' to bridge `%s': %s\n",
+                iface, bridge, strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* bring the interface up */
+    prep_ifreq(&ifr, iface);
+    if (ioctl(ctlfd, SIOCGIFFLAGS, &ifr) == -1) {
+        fprintf(stderr, "failed to get interface flags for `%s': %s\n",
+                iface, strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    ifr.ifr_flags |= IFF_UP;
+    if (ioctl(ctlfd, SIOCSIFFLAGS, &ifr) == -1) {
+        fprintf(stderr, "failed to bring up interface `%s': %s\n",
+                iface, strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* write fd to the domain socket */
+    if (send_fd(unixfd, fd) == -1) {
+        fprintf(stderr, "failed to write fd to unix socket: %s\n",
+                strerror(errno));
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* ... */
+
+    /* profit! */
+
+cleanup:
+
+    return ret;
+}
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH v6 2/4] Add access control support to qemu bridge helper
  2011-12-19 13:11 [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 1/4] Add basic version of bridge helper Corey Bryant
@ 2011-12-19 13:11 ` Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 3/4] Add cap reduction support to enable use as SUID Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 4/4] Add support for net bridge Corey Bryant
  3 siblings, 0 replies; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 13:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, rmarwah

We go to great lengths to restrict ourselves to just cap_net_admin as an OS
enforced security mechanism.  However, we further restrict what we allow users
to do to simply adding a tap device to a bridge interface by virtue of the fact
that this is the only functionality we expose.

This is not good enough though.  An administrator is likely to want to restrict
the bridges that an unprivileged user can access, in particular, to restrict
an unprivileged user from putting a guest on what should be isolated networks.

This patch implements an ACL mechanism that is enforced by qemu-bridge-helper.
The ACLs are fairly simple whitelist/blacklist mechanisms with a wildcard of
'all'.  All users are blacklisted by default, and deny takes precedence over
allow.

An interesting feature of this ACL mechanism is that you can include external
ACL files.  The main reason to support this is so that you can set different
file system permissions on those external ACL files.  This allows an
administrator to implement rather sophisticated ACL policies based on
user/group policies via the file system.

As an example:

/etc/qemu/bridge.conf root:qemu 0640

 allow br0
 include /etc/qemu/alice.conf
 include /etc/qemu/bob.conf
 include /etc/qemu/charlie.conf

/etc/qemu/alice.conf root:alice 0640
 allow br1

/etc/qemu/bob.conf root:bob 0640
 allow br2

/etc/qemu/charlie.conf root:charlie 0640
 deny all

This ACL pattern allows any user in the qemu group to get a tap device
connected to br0 (which is bridged to the physical network).

Users in the alice group can additionally get a tap device connected to br1.
This allows br1 to act as a private bridge for the alice group.

Users in the bob group can additionally get a tap device connected to br2.
This allows br2 to act as a private bridge for the bob group.

Users in the charlie group cannot get a tap device connected to any bridge.

Under no circumstance can the bob group get access to br1 or can the alice
group get access to br2.  And under no cicumstance can the charlie group
get access to any bridge.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
---
 qemu-bridge-helper.c |  153 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 153 insertions(+), 0 deletions(-)

diff --git a/qemu-bridge-helper.c b/qemu-bridge-helper.c
index e0ba917..2f137c2 100644
--- a/qemu-bridge-helper.c
+++ b/qemu-bridge-helper.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <ctype.h>
+#include <glib.h>
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
@@ -32,8 +33,110 @@
 
 #include <linux/sockios.h>
 
+#include "qemu-queue.h"
+
 #include "net/tap-linux.h"
 
+#define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
+
+enum {
+    ACL_ALLOW = 0,
+    ACL_ALLOW_ALL,
+    ACL_DENY,
+    ACL_DENY_ALL,
+};
+
+typedef struct ACLRule {
+    int type;
+    char iface[IFNAMSIZ];
+    QSIMPLEQ_ENTRY(ACLRule) entry;
+} ACLRule;
+
+typedef QSIMPLEQ_HEAD(ACLList, ACLRule) ACLList;
+
+static int parse_acl_file(const char *filename, ACLList *acl_list)
+{
+    FILE *f;
+    char line[4096];
+    ACLRule *acl_rule;
+
+    f = fopen(filename, "r");
+    if (f == NULL) {
+        return -1;
+    }
+
+    while (fgets(line, sizeof(line), f) != NULL) {
+        char *ptr = line;
+        char *cmd, *arg, *argend;
+
+        while (isspace(*ptr)) {
+            ptr++;
+        }
+
+        /* skip comments and empty lines */
+        if (*ptr == '#' || *ptr == 0) {
+            continue;
+        }
+
+        cmd = ptr;
+        arg = strchr(cmd, ' ');
+        if (arg == NULL) {
+            arg = strchr(cmd, '\t');
+        }
+
+        if (arg == NULL) {
+            fprintf(stderr, "Invalid config line:\n  %s\n", line);
+            fclose(f);
+            errno = EINVAL;
+            return -1;
+        }
+
+        *arg = 0;
+        arg++;
+        while (isspace(*arg)) {
+            arg++;
+        }
+
+        argend = arg + strlen(arg);
+        while (arg != argend && isspace(*(argend - 1))) {
+            argend--;
+        }
+        *argend = 0;
+
+        if (strcmp(cmd, "deny") == 0) {
+            acl_rule = g_malloc(sizeof(*acl_rule));
+            if (strcmp(arg, "all") == 0) {
+                acl_rule->type = ACL_DENY_ALL;
+            } else {
+                acl_rule->type = ACL_DENY;
+                snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
+            }
+            QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "allow") == 0) {
+            acl_rule = g_malloc(sizeof(*acl_rule));
+            if (strcmp(arg, "all") == 0) {
+                acl_rule->type = ACL_ALLOW_ALL;
+            } else {
+                acl_rule->type = ACL_ALLOW;
+                snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
+            }
+            QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "include") == 0) {
+            /* ignore errors */
+            parse_acl_file(arg, acl_list);
+        } else {
+            fprintf(stderr, "Unknown command `%s'\n", cmd);
+            fclose(f);
+            errno = EINVAL;
+            return -1;
+        }
+    }
+
+    fclose(f);
+
+    return 0;
+}
+
 static bool has_vnet_hdr(int fd)
 {
     unsigned int features = 0;
@@ -91,6 +194,9 @@ int main(int argc, char **argv)
     const char *bridge;
     char iface[IFNAMSIZ];
     int index;
+    ACLRule *acl_rule;
+    ACLList acl_list;
+    int access_allowed, access_denied;
     int ret = EXIT_SUCCESS;
 
     /* parse arguments */
@@ -112,6 +218,48 @@ int main(int argc, char **argv)
     bridge = argv[index++];
     unixfd = atoi(argv[index++]);
 
+    /* parse default acl file */
+    QSIMPLEQ_INIT(&acl_list);
+    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
+        fprintf(stderr, "failed to parse default acl file `%s'\n",
+                DEFAULT_ACL_FILE);
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
+    /* validate bridge against acl -- default policy is to deny
+     * according acl policy if we have a deny and allow both
+     * then deny should always win over allow
+     */
+    access_allowed = 0;
+    access_denied = 0;
+    QSIMPLEQ_FOREACH(acl_rule, &acl_list, entry) {
+        switch (acl_rule->type) {
+        case ACL_ALLOW_ALL:
+            access_allowed = 1;
+            break;
+        case ACL_ALLOW:
+            if (strcmp(bridge, acl_rule->iface) == 0) {
+                access_allowed = 1;
+            }
+            break;
+        case ACL_DENY_ALL:
+            access_denied = 1;
+            break;
+        case ACL_DENY:
+            if (strcmp(bridge, acl_rule->iface) == 0) {
+                access_denied = 1;
+            }
+            break;
+        }
+    }
+
+    if ((access_allowed == 0) || (access_denied == 1)) {
+        fprintf(stderr, "access denied by acl file\n");
+        ret = EXIT_FAILURE;
+        goto cleanup;
+    }
+
     /* open a socket to use to control the network interfaces */
     ctlfd = socket(AF_INET, SOCK_STREAM, 0);
     if (ctlfd == -1) {
@@ -209,5 +357,10 @@ int main(int argc, char **argv)
 
 cleanup:
 
+    while ((acl_rule = QSIMPLEQ_FIRST(&acl_list)) != NULL) {
+        QSIMPLEQ_REMOVE_HEAD(&acl_list, entry);
+        g_free(acl_rule);
+    }
+
     return ret;
 }
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH v6 3/4] Add cap reduction support to enable use as SUID
  2011-12-19 13:11 [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 1/4] Add basic version of bridge helper Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 2/4] Add access control support to qemu " Corey Bryant
@ 2011-12-19 13:11 ` Corey Bryant
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 4/4] Add support for net bridge Corey Bryant
  3 siblings, 0 replies; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 13:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, rmarwah

The ideal way to use qemu-bridge-helper is to give it an fscap of using:

 setcap cap_net_admin=ep qemu-bridge-helper

Unfortunately, most distros still do not have a mechanism to package files
with fscaps applied.  This means they'll have to SUID the qemu-bridge-helper
binary.

To improve security, use libcap to reduce our capability set to just
cap_net_admin, then reduce privileges down to the calling user.  This is
hopefully close to equivalent to fscap support from a security perspective.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
---
 configure            |   34 ++++++++++++++++++++++++++++++++++
 qemu-bridge-helper.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/configure b/configure
index 6c3a2f3..6ed4196 100755
--- a/configure
+++ b/configure
@@ -133,6 +133,7 @@ vnc_thread="no"
 xen=""
 xen_ctrl_version=""
 linux_aio=""
+cap=""
 attr=""
 libattr=""
 xfs=""
@@ -668,6 +669,10 @@ for opt do
   ;;
   --enable-tcg-interpreter) tcg_interpreter="yes"
   ;;
+  --disable-cap)  cap="no"
+  ;;
+  --enable-cap) cap="yes"
+  ;;
   --disable-spice) spice="no"
   ;;
   --enable-spice) spice="yes"
@@ -1051,6 +1056,8 @@ echo "  --disable-vde            disable support for vde network"
 echo "  --enable-vde             enable support for vde network"
 echo "  --disable-linux-aio      disable Linux AIO support"
 echo "  --enable-linux-aio       enable Linux AIO support"
+echo "  --disable-cap            disable libcap-ng support"
+echo "  --enable-cap             enable libcap-ng support"
 echo "  --disable-attr           disables attr and xattr support"
 echo "  --enable-attr            enable attr and xattr support"
 echo "  --disable-blobs          disable installing provided firmware blobs"
@@ -1713,6 +1720,29 @@ EOF
 fi
 
 ##########################################
+# libcap-ng library probe
+if test "$cap" != "no" ; then
+  cap_libs="-lcap-ng"
+  cat > $TMPC << EOF
+#include <cap-ng.h>
+int main(void)
+{
+    capng_capability_to_name(CAPNG_EFFECTIVE);
+    return 0;
+}
+EOF
+  if compile_prog "" "$cap_libs" ; then
+    cap=yes
+    libs_tools="$cap_libs $libs_tools"
+  else
+    if test "$cap" = "yes" ; then
+      feature_not_found "cap"
+    fi
+    cap=no
+  fi
+fi
+
+##########################################
 # Sound support libraries probe
 
 audio_drv_probe()
@@ -2839,6 +2869,7 @@ echo "fdatasync         $fdatasync"
 echo "madvise           $madvise"
 echo "posix_madvise     $posix_madvise"
 echo "uuid support      $uuid"
+echo "libcap-ng support $cap"
 echo "vhost-net support $vhost_net"
 echo "Trace backend     $trace_backend"
 echo "Trace output file $trace_file-<pid>"
@@ -2957,6 +2988,9 @@ fi
 if test "$vde" = "yes" ; then
   echo "CONFIG_VDE=y" >> $config_host_mak
 fi
+if test "$cap" = "yes" ; then
+  echo "CONFIG_LIBCAP=y" >> $config_host_mak
+fi
 for card in $audio_card_list; do
     def=CONFIG_`echo $card | tr '[:lower:]' '[:upper:]'`
     echo "$def=y" >> $config_host_mak
diff --git a/qemu-bridge-helper.c b/qemu-bridge-helper.c
index 2f137c2..b52038d 100644
--- a/qemu-bridge-helper.c
+++ b/qemu-bridge-helper.c
@@ -37,6 +37,10 @@
 
 #include "net/tap-linux.h"
 
+#ifdef CONFIG_LIBCAP
+#include <cap-ng.h>
+#endif
+
 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
 
 enum {
@@ -185,6 +189,27 @@ static int send_fd(int c, int fd)
     return sendmsg(c, &msg, 0);
 }
 
+#ifdef CONFIG_LIBCAP
+static int drop_privileges(void)
+{
+    /* clear all capabilities */
+    capng_clear(CAPNG_SELECT_BOTH);
+
+    if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
+                     CAP_NET_ADMIN) < 0) {
+        return -1;
+    }
+
+    /* change to calling user's real uid and gid, retaining supplemental
+     * groups and CAP_NET_ADMIN */
+    if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING)) {
+        return -1;
+    }
+
+    return 0;
+}
+#endif
+
 int main(int argc, char **argv)
 {
     struct ifreq ifr;
@@ -199,6 +224,17 @@ int main(int argc, char **argv)
     int access_allowed, access_denied;
     int ret = EXIT_SUCCESS;
 
+#ifdef CONFIG_LIBCAP
+    /* if we're run from an suid binary, immediately drop privileges preserving
+     * cap_net_admin */
+    if (geteuid() == 0 && getuid() != geteuid()) {
+        if (drop_privileges() == -1) {
+            fprintf(stderr, "failed to drop privileges\n");
+            return 1;
+        }
+    }
+#endif
+
     /* parse arguments */
     if (argc < 3 || argc > 4) {
         fprintf(stderr, "Usage: %s [--use-vnet] BRIDGE FD\n", argv[0]);
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 13:11 [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu Corey Bryant
                   ` (2 preceding siblings ...)
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 3/4] Add cap reduction support to enable use as SUID Corey Bryant
@ 2011-12-19 13:11 ` Corey Bryant
  2011-12-19 19:36   ` Anthony Liguori
  2011-12-20 10:02   ` Hui Kai Ran
  3 siblings, 2 replies; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 13:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, rmarwah

The most common use of -net tap is to connect a tap device to a bridge.  This
requires the use of a script and running qemu as root in order to allocate a
tap device to pass to the script.

This model is great for portability and flexibility but it's incredibly
difficult to eliminate the need to run qemu as root.  The only really viable
mechanism is to use tunctl to create a tap device, attach it to a bridge as
root, and then hand that tap device to qemu.  The problem with this mechanism
is that it requires administrator intervention whenever a user wants to create
a guest.

By essentially writing a helper that implements the most common qemu-ifup
script that can be safely given cap_net_admin, we can dramatically simplify
things for non-privileged users.  We still support existing -net tap options
as a mechanism for advanced users and backwards compatibility.

Currently, this is very Linux centric but there's really no reason why it
couldn't be extended for other Unixes.

A typical invocation would be similar to one of the following:

  qemu linux.img -net bridge -net nic,model=virtio

  qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
                 -net nic,model=virtio

  qemu linux.img -netdev bridge,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

  qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

The default bridge that we attach to is br0.  The thinking is that a distro
could preconfigure such an interface to allow out-of-the-box bridged networking.

Alternatively, if a user wants to use a different bridge, a typical invocation
would be simliar to one of the following:

  qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio

  qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0
                 -net nic,model=virtio

  qemu linux.img -netdev bridge,br=qemubr0,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

  qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0,id=hn0
                 -device virtio-net-pci,netdev=hn0,id=nic1

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Richa Marwaha <rmarwah@linux.vnet.ibm.com>
Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
---
 configure       |    2 +
 net.c           |   29 ++++++++-
 net.h           |    3 +
 net/tap.c       |  187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 net/tap.h       |    3 +
 qemu-options.hx |   74 ++++++++++++++++++----
 6 files changed, 281 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index 6ed4196..4839694 100755
--- a/configure
+++ b/configure
@@ -2905,6 +2905,8 @@ echo "sysconfdir=$sysconfdir" >> $config_host_mak
 echo "docdir=$docdir" >> $config_host_mak
 echo "confdir=$confdir" >> $config_host_mak
 echo "libexecdir=\${prefix}/libexec" >> $config_host_mak
+echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"" >> $config_host_mak
+echo "CONFIG_QEMU_HELPERDIR=\"$prefix/libexec\"" >> $config_host_mak
 
 case "$cpu" in
   i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
diff --git a/net.c b/net.c
index f7bebf8..9296224 100644
--- a/net.c
+++ b/net.c
@@ -952,6 +952,14 @@ static const struct {
                 .type = QEMU_OPT_STRING,
                 .help = "script to shut down the interface",
             }, {
+                .name = "br",
+                .type = QEMU_OPT_STRING,
+                .help = "bridge name",
+            }, {
+                .name = "helper",
+                .type = QEMU_OPT_STRING,
+                .help = "command to execute to configure bridge",
+            }, {
                 .name = "sndbuf",
                 .type = QEMU_OPT_SIZE,
                 .help = "send buffer limit"
@@ -1049,6 +1057,23 @@ static const struct {
             { /* end of list */ }
         },
     },
+    [NET_CLIENT_TYPE_BRIDGE] = {
+        .type = "bridge",
+        .init = net_init_bridge,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "br",
+                .type = QEMU_OPT_STRING,
+                .help = "bridge name",
+            }, {
+                .name = "helper",
+                .type = QEMU_OPT_STRING,
+                .help = "command to execute to configure bridge",
+            },
+            { /* end of list */ }
+        },
+    },
 };
 
 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
@@ -1071,7 +1096,8 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
 #ifdef CONFIG_VDE
             strcmp(type, "vde") != 0 &&
 #endif
-            strcmp(type, "socket") != 0) {
+            strcmp(type, "socket") != 0 &&
+            strcmp(type, "bridge") != 0) {
             qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
                           "a netdev backend type");
             return -1;
@@ -1141,6 +1167,7 @@ static int net_host_check_device(const char *device)
 #ifdef CONFIG_VDE
                                        ,"vde"
 #endif
+                                       , "bridge"
     };
     for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
         if (!strncmp(valid_param_list[i], device,
diff --git a/net.h b/net.h
index c6b4190..0fd7e23 100644
--- a/net.h
+++ b/net.h
@@ -36,6 +36,7 @@ typedef enum {
     NET_CLIENT_TYPE_SOCKET,
     NET_CLIENT_TYPE_VDE,
     NET_CLIENT_TYPE_DUMP,
+    NET_CLIENT_TYPE_BRIDGE,
 
     NET_CLIENT_TYPE_MAX
 } net_client_type;
@@ -173,6 +174,8 @@ int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
+#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
+#define DEFAULT_BRIDGE_INTERFACE "br0"
 
 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
 
diff --git a/net/tap.c b/net/tap.c
index 6c27a94..b2b82a1 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -382,6 +382,143 @@ static int launch_script(const char *setup_script, const char *ifname, int fd)
     return -1;
 }
 
+static int recv_fd(int c)
+{
+    int fd;
+    uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
+    struct msghdr msg = {
+        .msg_control = msgbuf,
+        .msg_controllen = sizeof(msgbuf),
+    };
+    struct cmsghdr *cmsg;
+    struct iovec iov;
+    uint8_t req[1];
+    ssize_t len;
+
+    cmsg = CMSG_FIRSTHDR(&msg);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+    msg.msg_controllen = cmsg->cmsg_len;
+
+    iov.iov_base = req;
+    iov.iov_len = sizeof(req);
+
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+
+    len = recvmsg(c, &msg, 0);
+    if (len > 0) {
+        memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
+        return fd;
+    }
+
+    return len;
+}
+
+static int net_bridge_run_helper(const char *helper, const char *bridge)
+{
+    sigset_t oldmask, mask;
+    int pid, status;
+    char *args[5];
+    char **parg;
+    int sv[2];
+
+    sigemptyset(&mask);
+    sigaddset(&mask, SIGCHLD);
+    sigprocmask(SIG_BLOCK, &mask, &oldmask);
+
+    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
+        return -1;
+    }
+
+    /* try to launch bridge helper */
+    pid = fork();
+    if (pid == 0) {
+        int open_max = sysconf(_SC_OPEN_MAX), i;
+        char buf[32];
+
+        snprintf(buf, sizeof(buf), "%d", sv[1]);
+
+        for (i = 0; i < open_max; i++) {
+            if (i != STDIN_FILENO &&
+                i != STDOUT_FILENO &&
+                i != STDERR_FILENO &&
+                i != sv[1]) {
+                close(i);
+            }
+        }
+        parg = args;
+        *parg++ = (char *)helper;
+        *parg++ = (char *)"--use-vnet";
+        *parg++ = (char *)bridge;
+        *parg++ = buf;
+        *parg++ = NULL;
+        execv(helper, args);
+        _exit(1);
+    } else if (pid > 0) {
+        int fd;
+
+        close(sv[1]);
+
+        do {
+            fd = recv_fd(sv[0]);
+        } while (fd == -1 && errno == EINTR);
+
+        close(sv[0]);
+
+        while (waitpid(pid, &status, 0) != pid) {
+            /* loop */
+        }
+        sigprocmask(SIG_SETMASK, &oldmask, NULL);
+        if (fd < 0) {
+            fprintf(stderr, "failed to recv file descriptor\n");
+            return -1;
+        }
+
+        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+            return fd;
+        }
+    }
+    fprintf(stderr, "failed to launch bridge helper\n");
+    return -1;
+}
+
+int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
+                    VLANState *vlan)
+{
+    TAPState *s;
+    int fd, vnet_hdr;
+
+    if (!qemu_opt_get(opts, "br")) {
+        qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
+    }
+    if (!qemu_opt_get(opts, "helper")) {
+        qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
+    }
+
+    fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
+                               qemu_opt_get(opts, "br"));
+    if (fd == -1) {
+        return -1;
+    }
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+
+    vnet_hdr = tap_probe_vnet_hdr(fd);
+
+    s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
+    if (!s) {
+        close(fd);
+        return -1;
+    }
+
+    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+             "br=%s", qemu_opt_get(opts, "br"));
+
+    return 0;
+}
+
 static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
 {
     int fd, vnet_hdr_required;
@@ -422,13 +559,17 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
 {
     TAPState *s;
     int fd, vnet_hdr = 0;
+    const char *model;
 
     if (qemu_opt_get(opts, "fd")) {
         if (qemu_opt_get(opts, "ifname") ||
             qemu_opt_get(opts, "script") ||
             qemu_opt_get(opts, "downscript") ||
-            qemu_opt_get(opts, "vnet_hdr")) {
-            error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
+            qemu_opt_get(opts, "vnet_hdr") ||
+            qemu_opt_get(opts, "br") ||
+            qemu_opt_get(opts, "helper")) {
+            error_report("ifname=, script=, downscript=, vnet_hdr=, "
+                         "br= and helper= are invalid with fd=");
             return -1;
         }
 
@@ -440,7 +581,41 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
         fcntl(fd, F_SETFL, O_NONBLOCK);
 
         vnet_hdr = tap_probe_vnet_hdr(fd);
+
+        model = "tap";
+
+    } else if (qemu_opt_get(opts, "helper")) {
+        if (qemu_opt_get(opts, "ifname") ||
+            qemu_opt_get(opts, "script") ||
+            qemu_opt_get(opts, "downscript") ||
+            qemu_opt_get(opts, "vnet_hdr")) {
+            error_report("ifname=, script=, downscript=, and vnet_hdr= "
+                         "are invalid with helper=");
+            return -1;
+        }
+
+        if (!qemu_opt_get(opts, "br")) {
+            qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
+        }
+
+        fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
+                                   qemu_opt_get(opts, "br"));
+        if (fd == -1) {
+            return -1;
+        }
+
+        fcntl(fd, F_SETFL, O_NONBLOCK);
+
+        vnet_hdr = tap_probe_vnet_hdr(fd);
+
+        model = "bridge";
+
     } else {
+        if (qemu_opt_get(opts, "br")) {
+            error_report("br= is invalid with script=");
+            return -1;
+        }
+
         if (!qemu_opt_get(opts, "script")) {
             qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
         }
@@ -453,9 +628,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
         if (fd == -1) {
             return -1;
         }
+
+        model = "tap";
     }
 
-    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
+    s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
     if (!s) {
         close(fd);
         return -1;
@@ -467,6 +644,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
 
     if (qemu_opt_get(opts, "fd")) {
         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
+    } else if (qemu_opt_get(opts, "helper")) {
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                "helper=%s,br=%s", qemu_opt_get(opts, "helper"),
+                qemu_opt_get(opts, "br"));
     } else {
         const char *ifname, *script, *downscript;
 
diff --git a/net/tap.h b/net/tap.h
index e44bd2b..56c591f 100644
--- a/net/tap.h
+++ b/net/tap.h
@@ -57,4 +57,7 @@ int tap_get_fd(VLANClientState *vc);
 struct vhost_net;
 struct vhost_net *tap_get_vhost_net(VLANClientState *vc);
 
+int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
+                    VLANState *vlan);
+
 #endif /* QEMU_NET_TAP_H */
diff --git a/qemu-options.hx b/qemu-options.hx
index 087a3b9..4f2385d 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1205,11 +1205,14 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "-net tap[,vlan=n][,name=str],ifname=name\n"
     "                connect the host TAP network interface to VLAN 'n'\n"
 #else
-    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
-    "                connect the host TAP network interface to VLAN 'n' and use the\n"
-    "                network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
-    "                and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
+    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
+    "                connect the host TAP network interface to VLAN 'n' \n"
+    "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
+    "                to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
+    "                to deconfigure it\n"
     "                use '[down]script=no' to disable script execution\n"
+    "                use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ") and\n"
+    "                bridge 'br' (default=" DEFAULT_BRIDGE_INTERFACE ") to configure it\n"
     "                use 'fd=h' to connect to an already opened TAP interface\n"
     "                use 'sndbuf=nbytes' to limit the size of the send buffer (the\n"
     "                default is disabled 'sndbuf=0' to enable flow control set 'sndbuf=1048576')\n"
@@ -1219,6 +1222,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "                    (only has effect for virtio guests which use MSIX)\n"
     "                use vhostforce=on to force vhost on for non-MSIX virtio guests\n"
     "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
+    "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
+    "                connects a host TAP network interface to a host bridge device 'br'\n"
+    "                (default=" DEFAULT_BRIDGE_INTERFACE ") using the program 'helper'\n"
+    "                (default=" DEFAULT_BRIDGE_HELPER ")\n"
 #endif
     "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
     "                connect the vlan 'n' to another VLAN using a socket connection\n"
@@ -1242,6 +1249,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
     "user|"
 #endif
     "tap|"
+    "bridge|"
 #ifdef CONFIG_VDE
     "vde|"
 #endif
@@ -1378,26 +1386,66 @@ processed and applied to -net user. Mixing them with the new configuration
 syntax gives undefined results. Their use for new applications is discouraged
 as they will be removed from future versions.
 
-@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}] [,script=@var{file}][,downscript=@var{dfile}]
-Connect the host TAP network interface @var{name} to VLAN @var{n}, use
-the network script @var{file} to configure it and the network script
+@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}][,br=@var{bridge}][,helper=@var{helper}]
+Connect the host TAP network interface @var{name} to VLAN @var{n}.
+
+Use the network script @var{file} to configure it and the network script
 @var{dfile} to deconfigure it. If @var{name} is not provided, the OS
-automatically provides one. @option{fd}=@var{h} can be used to specify
-the handle of an already opened host TAP interface. The default network
-configure script is @file{/etc/qemu-ifup} and the default network
-deconfigure script is @file{/etc/qemu-ifdown}. Use @option{script=no}
-or @option{downscript=no} to disable script execution. Example:
+automatically provides one. The default network configure script is
+@file{/etc/qemu-ifup} and the default network deconfigure script is
+@file{/etc/qemu-ifdown}. Use @option{script=no} or @option{downscript=no}
+to disable script execution.
+
+If running QEMU as an unprivileged user, use the network helper
+@var{helper} to configure the TAP interface. The default network
+helper executable is @file{/usr/local/libexec/qemu-bridge-helper}
+and the default bridge device is @file{br0}.
+
+@option{fd}=@var{h} can be used to specify the handle of an already
+opened host TAP interface.
+
+Examples:
 
 @example
+#launch a QEMU instance with the default network script
 qemu linux.img -net nic -net tap
 @end example
 
-More complicated example (two NICs, each one connected to a TAP device)
 @example
+#launch a QEMU instance with two NICs, each one connected
+#to a TAP device
 qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
                -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
 @end example
 
+@example
+#launch a QEMU instance with the default network helper to
+#connect a TAP device to bridge br0
+qemu linux.img -net nic -net tap,helper=/usr/local/libexec/qemu-bridge-helper
+@end example
+
+@item -net bridge[,vlan=@var{n}][,name=@var{name}][,br=@var{bridge}][,helper=@var{helper}]
+Connect a host TAP network interface to a host bridge device.
+
+Use the network helper @var{helper} to configure the TAP interface and
+attach it to the bridge. The default network helper executable is
+@file{/usr/local/libexec/qemu-bridge-helper} and the default bridge
+device is @file{br0}.
+
+Examples:
+
+@example
+#launch a QEMU instance with the default network helper to
+#connect a TAP device to bridge br0
+qemu linux.img -net bridge -net nic,model=virtio
+@end example
+
+@example
+#launch a QEMU instance with the default network helper to
+#connect a TAP device to bridge qemubr0
+qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
+@end example
+
 @item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
 
 Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 4/4] Add support for net bridge Corey Bryant
@ 2011-12-19 19:36   ` Anthony Liguori
  2011-12-19 22:55     ` Corey Bryant
  2011-12-20 10:02   ` Hui Kai Ran
  1 sibling, 1 reply; 12+ messages in thread
From: Anthony Liguori @ 2011-12-19 19:36 UTC (permalink / raw)
  To: Corey Bryant; +Cc: rmarwah, qemu-devel

On 12/19/2011 07:11 AM, Corey Bryant wrote:
> The most common use of -net tap is to connect a tap device to a bridge.  This
> requires the use of a script and running qemu as root in order to allocate a
> tap device to pass to the script.
>
> This model is great for portability and flexibility but it's incredibly
> difficult to eliminate the need to run qemu as root.  The only really viable
> mechanism is to use tunctl to create a tap device, attach it to a bridge as
> root, and then hand that tap device to qemu.  The problem with this mechanism
> is that it requires administrator intervention whenever a user wants to create
> a guest.
>
> By essentially writing a helper that implements the most common qemu-ifup
> script that can be safely given cap_net_admin, we can dramatically simplify
> things for non-privileged users.  We still support existing -net tap options
> as a mechanism for advanced users and backwards compatibility.
>
> Currently, this is very Linux centric but there's really no reason why it
> couldn't be extended for other Unixes.
>
> A typical invocation would be similar to one of the following:
>
>    qemu linux.img -net bridge -net nic,model=virtio
>
>    qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
>                   -net nic,model=virtio
>
>    qemu linux.img -netdev bridge,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
>    qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
> The default bridge that we attach to is br0.  The thinking is that a distro
> could preconfigure such an interface to allow out-of-the-box bridged networking.
>
> Alternatively, if a user wants to use a different bridge, a typical invocation
> would be simliar to one of the following:
>
>    qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>
>    qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0
>                   -net nic,model=virtio
>
>    qemu linux.img -netdev bridge,br=qemubr0,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
>    qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
> Signed-off-by: Richa Marwaha<rmarwah@linux.vnet.ibm.com>
> Signed-off-by: Corey Bryant<coreyb@linux.vnet.ibm.com>
> ---
>   configure       |    2 +
>   net.c           |   29 ++++++++-
>   net.h           |    3 +
>   net/tap.c       |  187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>   net/tap.h       |    3 +
>   qemu-options.hx |   74 ++++++++++++++++++----
>   6 files changed, 281 insertions(+), 17 deletions(-)
>
> diff --git a/configure b/configure
> index 6ed4196..4839694 100755
> --- a/configure
> +++ b/configure
> @@ -2905,6 +2905,8 @@ echo "sysconfdir=$sysconfdir">>  $config_host_mak
>   echo "docdir=$docdir">>  $config_host_mak
>   echo "confdir=$confdir">>  $config_host_mak
>   echo "libexecdir=\${prefix}/libexec">>  $config_host_mak
> +echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"">>  $config_host_mak
> +echo "CONFIG_QEMU_HELPERDIR=\"$prefix/libexec\"">>  $config_host_mak
>
>   case "$cpu" in
>     i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
> diff --git a/net.c b/net.c
> index f7bebf8..9296224 100644
> --- a/net.c
> +++ b/net.c
> @@ -952,6 +952,14 @@ static const struct {
>                   .type = QEMU_OPT_STRING,
>                   .help = "script to shut down the interface",
>               }, {
> +                .name = "br",
> +                .type = QEMU_OPT_STRING,
> +                .help = "bridge name",
> +            }, {

I don't think passing br= makes a whole of sense for -net tap.  I think it would 
make more sense to make sure that helper could take a shell string so you could do:

-netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=br0"

Regards,

Anthony Liguori

> +                .name = "helper",
> +                .type = QEMU_OPT_STRING,
> +                .help = "command to execute to configure bridge",
> +            }, {
>                   .name = "sndbuf",
>                   .type = QEMU_OPT_SIZE,
>                   .help = "send buffer limit"
> @@ -1049,6 +1057,23 @@ static const struct {
>               { /* end of list */ }
>           },
>       },
> +    [NET_CLIENT_TYPE_BRIDGE] = {
> +        .type = "bridge",
> +        .init = net_init_bridge,
> +        .desc = {
> +            NET_COMMON_PARAMS_DESC,
> +            {
> +                .name = "br",
> +                .type = QEMU_OPT_STRING,
> +                .help = "bridge name",
> +            }, {
> +                .name = "helper",
> +                .type = QEMU_OPT_STRING,
> +                .help = "command to execute to configure bridge",
> +            },
> +            { /* end of list */ }
> +        },
> +    },
>   };
>
>   int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
> @@ -1071,7 +1096,8 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
>   #ifdef CONFIG_VDE
>               strcmp(type, "vde") != 0&&
>   #endif
> -            strcmp(type, "socket") != 0) {
> +            strcmp(type, "socket") != 0&&
> +            strcmp(type, "bridge") != 0) {
>               qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
>                             "a netdev backend type");
>               return -1;
> @@ -1141,6 +1167,7 @@ static int net_host_check_device(const char *device)
>   #ifdef CONFIG_VDE
>                                          ,"vde"
>   #endif
> +                                       , "bridge"
>       };
>       for (i = 0; i<  sizeof(valid_param_list) / sizeof(char *); i++) {
>           if (!strncmp(valid_param_list[i], device,
> diff --git a/net.h b/net.h
> index c6b4190..0fd7e23 100644
> --- a/net.h
> +++ b/net.h
> @@ -36,6 +36,7 @@ typedef enum {
>       NET_CLIENT_TYPE_SOCKET,
>       NET_CLIENT_TYPE_VDE,
>       NET_CLIENT_TYPE_DUMP,
> +    NET_CLIENT_TYPE_BRIDGE,
>
>       NET_CLIENT_TYPE_MAX
>   } net_client_type;
> @@ -173,6 +174,8 @@ int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
>
>   #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
>   #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
> +#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
> +#define DEFAULT_BRIDGE_INTERFACE "br0"
>
>   void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
>
> diff --git a/net/tap.c b/net/tap.c
> index 6c27a94..b2b82a1 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -382,6 +382,143 @@ static int launch_script(const char *setup_script, const char *ifname, int fd)
>       return -1;
>   }
>
> +static int recv_fd(int c)
> +{
> +    int fd;
> +    uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
> +    struct msghdr msg = {
> +        .msg_control = msgbuf,
> +        .msg_controllen = sizeof(msgbuf),
> +    };
> +    struct cmsghdr *cmsg;
> +    struct iovec iov;
> +    uint8_t req[1];
> +    ssize_t len;
> +
> +    cmsg = CMSG_FIRSTHDR(&msg);
> +    cmsg->cmsg_level = SOL_SOCKET;
> +    cmsg->cmsg_type = SCM_RIGHTS;
> +    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
> +    msg.msg_controllen = cmsg->cmsg_len;
> +
> +    iov.iov_base = req;
> +    iov.iov_len = sizeof(req);
> +
> +    msg.msg_iov =&iov;
> +    msg.msg_iovlen = 1;
> +
> +    len = recvmsg(c,&msg, 0);
> +    if (len>  0) {
> +        memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
> +        return fd;
> +    }
> +
> +    return len;
> +}
> +
> +static int net_bridge_run_helper(const char *helper, const char *bridge)
> +{
> +    sigset_t oldmask, mask;
> +    int pid, status;
> +    char *args[5];
> +    char **parg;
> +    int sv[2];
> +
> +    sigemptyset(&mask);
> +    sigaddset(&mask, SIGCHLD);
> +    sigprocmask(SIG_BLOCK,&mask,&oldmask);
> +
> +    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
> +        return -1;
> +    }
> +
> +    /* try to launch bridge helper */
> +    pid = fork();
> +    if (pid == 0) {
> +        int open_max = sysconf(_SC_OPEN_MAX), i;
> +        char buf[32];
> +
> +        snprintf(buf, sizeof(buf), "%d", sv[1]);
> +
> +        for (i = 0; i<  open_max; i++) {
> +            if (i != STDIN_FILENO&&
> +                i != STDOUT_FILENO&&
> +                i != STDERR_FILENO&&
> +                i != sv[1]) {
> +                close(i);
> +            }
> +        }
> +        parg = args;
> +        *parg++ = (char *)helper;
> +        *parg++ = (char *)"--use-vnet";
> +        *parg++ = (char *)bridge;
> +        *parg++ = buf;
> +        *parg++ = NULL;
> +        execv(helper, args);
> +        _exit(1);
> +    } else if (pid>  0) {
> +        int fd;
> +
> +        close(sv[1]);
> +
> +        do {
> +            fd = recv_fd(sv[0]);
> +        } while (fd == -1&&  errno == EINTR);
> +
> +        close(sv[0]);
> +
> +        while (waitpid(pid,&status, 0) != pid) {
> +            /* loop */
> +        }
> +        sigprocmask(SIG_SETMASK,&oldmask, NULL);
> +        if (fd<  0) {
> +            fprintf(stderr, "failed to recv file descriptor\n");
> +            return -1;
> +        }
> +
> +        if (WIFEXITED(status)&&  WEXITSTATUS(status) == 0) {
> +            return fd;
> +        }
> +    }
> +    fprintf(stderr, "failed to launch bridge helper\n");
> +    return -1;
> +}
> +
> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
> +                    VLANState *vlan)
> +{
> +    TAPState *s;
> +    int fd, vnet_hdr;
> +
> +    if (!qemu_opt_get(opts, "br")) {
> +        qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
> +    }
> +    if (!qemu_opt_get(opts, "helper")) {
> +        qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
> +    }
> +
> +    fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
> +                               qemu_opt_get(opts, "br"));
> +    if (fd == -1) {
> +        return -1;
> +    }
> +
> +    fcntl(fd, F_SETFL, O_NONBLOCK);
> +
> +    vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +    s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
> +    if (!s) {
> +        close(fd);
> +        return -1;
> +    }
> +
> +    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +             "br=%s", qemu_opt_get(opts, "br"));
> +
> +    return 0;
> +}
> +
>   static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
>   {
>       int fd, vnet_hdr_required;
> @@ -422,13 +559,17 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>   {
>       TAPState *s;
>       int fd, vnet_hdr = 0;
> +    const char *model;
>
>       if (qemu_opt_get(opts, "fd")) {
>           if (qemu_opt_get(opts, "ifname") ||
>               qemu_opt_get(opts, "script") ||
>               qemu_opt_get(opts, "downscript") ||
> -            qemu_opt_get(opts, "vnet_hdr")) {
> -            error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
> +            qemu_opt_get(opts, "vnet_hdr") ||
> +            qemu_opt_get(opts, "br") ||
> +            qemu_opt_get(opts, "helper")) {
> +            error_report("ifname=, script=, downscript=, vnet_hdr=, "
> +                         "br= and helper= are invalid with fd=");
>               return -1;
>           }
>
> @@ -440,7 +581,41 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>           fcntl(fd, F_SETFL, O_NONBLOCK);
>
>           vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +        model = "tap";
> +
> +    } else if (qemu_opt_get(opts, "helper")) {
> +        if (qemu_opt_get(opts, "ifname") ||
> +            qemu_opt_get(opts, "script") ||
> +            qemu_opt_get(opts, "downscript") ||
> +            qemu_opt_get(opts, "vnet_hdr")) {
> +            error_report("ifname=, script=, downscript=, and vnet_hdr= "
> +                         "are invalid with helper=");
> +            return -1;
> +        }
> +
> +        if (!qemu_opt_get(opts, "br")) {
> +            qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
> +        }
> +
> +        fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
> +                                   qemu_opt_get(opts, "br"));
> +        if (fd == -1) {
> +            return -1;
> +        }
> +
> +        fcntl(fd, F_SETFL, O_NONBLOCK);
> +
> +        vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +        model = "bridge";
> +
>       } else {
> +        if (qemu_opt_get(opts, "br")) {
> +            error_report("br= is invalid with script=");
> +            return -1;
> +        }
> +
>           if (!qemu_opt_get(opts, "script")) {
>               qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
>           }
> @@ -453,9 +628,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>           if (fd == -1) {
>               return -1;
>           }
> +
> +        model = "tap";
>       }
>
> -    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
> +    s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
>       if (!s) {
>           close(fd);
>           return -1;
> @@ -467,6 +644,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>
>       if (qemu_opt_get(opts, "fd")) {
>           snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
> +    } else if (qemu_opt_get(opts, "helper")) {
> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +                "helper=%s,br=%s", qemu_opt_get(opts, "helper"),
> +                qemu_opt_get(opts, "br"));
>       } else {
>           const char *ifname, *script, *downscript;
>
> diff --git a/net/tap.h b/net/tap.h
> index e44bd2b..56c591f 100644
> --- a/net/tap.h
> +++ b/net/tap.h
> @@ -57,4 +57,7 @@ int tap_get_fd(VLANClientState *vc);
>   struct vhost_net;
>   struct vhost_net *tap_get_vhost_net(VLANClientState *vc);
>
> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
> +                    VLANState *vlan);
> +
>   #endif /* QEMU_NET_TAP_H */
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 087a3b9..4f2385d 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -1205,11 +1205,14 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>       "-net tap[,vlan=n][,name=str],ifname=name\n"
>       "                connect the host TAP network interface to VLAN 'n'\n"
>   #else
> -    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
> -    "                connect the host TAP network interface to VLAN 'n' and use the\n"
> -    "                network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
> -    "                and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
> +    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
> +    "                connect the host TAP network interface to VLAN 'n' \n"
> +    "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
> +    "                to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
> +    "                to deconfigure it\n"
>       "                use '[down]script=no' to disable script execution\n"
> +    "                use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ") and\n"
> +    "                bridge 'br' (default=" DEFAULT_BRIDGE_INTERFACE ") to configure it\n"
>       "                use 'fd=h' to connect to an already opened TAP interface\n"
>       "                use 'sndbuf=nbytes' to limit the size of the send buffer (the\n"
>       "                default is disabled 'sndbuf=0' to enable flow control set 'sndbuf=1048576')\n"
> @@ -1219,6 +1222,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>       "                    (only has effect for virtio guests which use MSIX)\n"
>       "                use vhostforce=on to force vhost on for non-MSIX virtio guests\n"
>       "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
> +    "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
> +    "                connects a host TAP network interface to a host bridge device 'br'\n"
> +    "                (default=" DEFAULT_BRIDGE_INTERFACE ") using the program 'helper'\n"
> +    "                (default=" DEFAULT_BRIDGE_HELPER ")\n"
>   #endif
>       "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
>       "                connect the vlan 'n' to another VLAN using a socket connection\n"
> @@ -1242,6 +1249,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
>       "user|"
>   #endif
>       "tap|"
> +    "bridge|"
>   #ifdef CONFIG_VDE
>       "vde|"
>   #endif
> @@ -1378,26 +1386,66 @@ processed and applied to -net user. Mixing them with the new configuration
>   syntax gives undefined results. Their use for new applications is discouraged
>   as they will be removed from future versions.
>
> -@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}] [,script=@var{file}][,downscript=@var{dfile}]
> -Connect the host TAP network interface @var{name} to VLAN @var{n}, use
> -the network script @var{file} to configure it and the network script
> +@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}][,br=@var{bridge}][,helper=@var{helper}]
> +Connect the host TAP network interface @var{name} to VLAN @var{n}.
> +
> +Use the network script @var{file} to configure it and the network script
>   @var{dfile} to deconfigure it. If @var{name} is not provided, the OS
> -automatically provides one. @option{fd}=@var{h} can be used to specify
> -the handle of an already opened host TAP interface. The default network
> -configure script is @file{/etc/qemu-ifup} and the default network
> -deconfigure script is @file{/etc/qemu-ifdown}. Use @option{script=no}
> -or @option{downscript=no} to disable script execution. Example:
> +automatically provides one. The default network configure script is
> +@file{/etc/qemu-ifup} and the default network deconfigure script is
> +@file{/etc/qemu-ifdown}. Use @option{script=no} or @option{downscript=no}
> +to disable script execution.
> +
> +If running QEMU as an unprivileged user, use the network helper
> +@var{helper} to configure the TAP interface. The default network
> +helper executable is @file{/usr/local/libexec/qemu-bridge-helper}
> +and the default bridge device is @file{br0}.
> +
> +@option{fd}=@var{h} can be used to specify the handle of an already
> +opened host TAP interface.
> +
> +Examples:
>
>   @example
> +#launch a QEMU instance with the default network script
>   qemu linux.img -net nic -net tap
>   @end example
>
> -More complicated example (two NICs, each one connected to a TAP device)
>   @example
> +#launch a QEMU instance with two NICs, each one connected
> +#to a TAP device
>   qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
>                  -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
>   @end example
>
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge br0
> +qemu linux.img -net nic -net tap,helper=/usr/local/libexec/qemu-bridge-helper
> +@end example
> +
> +@item -net bridge[,vlan=@var{n}][,name=@var{name}][,br=@var{bridge}][,helper=@var{helper}]
> +Connect a host TAP network interface to a host bridge device.
> +
> +Use the network helper @var{helper} to configure the TAP interface and
> +attach it to the bridge. The default network helper executable is
> +@file{/usr/local/libexec/qemu-bridge-helper} and the default bridge
> +device is @file{br0}.
> +
> +Examples:
> +
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge br0
> +qemu linux.img -net bridge -net nic,model=virtio
> +@end example
> +
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge qemubr0
> +qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
> +@end example
> +
>   @item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
>
>   Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 19:36   ` Anthony Liguori
@ 2011-12-19 22:55     ` Corey Bryant
  2011-12-19 23:15       ` Anthony Liguori
  0 siblings, 1 reply; 12+ messages in thread
From: Corey Bryant @ 2011-12-19 22:55 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: rmarwah, qemu-devel



On 12/19/2011 02:36 PM, Anthony Liguori wrote:
> On 12/19/2011 07:11 AM, Corey Bryant wrote:
>> The most common use of -net tap is to connect a tap device to a
>> bridge. This
>> requires the use of a script and running qemu as root in order to
>> allocate a
>> tap device to pass to the script.
>>
>> This model is great for portability and flexibility but it's incredibly
>> difficult to eliminate the need to run qemu as root. The only really
>> viable
>> mechanism is to use tunctl to create a tap device, attach it to a
>> bridge as
>> root, and then hand that tap device to qemu. The problem with this
>> mechanism
>> is that it requires administrator intervention whenever a user wants
>> to create
>> a guest.
>>
>> By essentially writing a helper that implements the most common qemu-ifup
>> script that can be safely given cap_net_admin, we can dramatically
>> simplify
>> things for non-privileged users. We still support existing -net tap
>> options
>> as a mechanism for advanced users and backwards compatibility.
>>
>> Currently, this is very Linux centric but there's really no reason why it
>> couldn't be extended for other Unixes.
>>
>> A typical invocation would be similar to one of the following:
>>
>> qemu linux.img -net bridge -net nic,model=virtio
>>
>> qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
>> -net nic,model=virtio
>>
>> qemu linux.img -netdev bridge,id=hn0
>> -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> qemu linux.img -netdev
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
>> -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> The default bridge that we attach to is br0. The thinking is that a
>> distro
>> could preconfigure such an interface to allow out-of-the-box bridged
>> networking.
>>
>> Alternatively, if a user wants to use a different bridge, a typical
>> invocation
>> would be simliar to one of the following:
>>
>> qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>>
>> qemu linux.img -net
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0
>> -net nic,model=virtio
>>
>> qemu linux.img -netdev bridge,br=qemubr0,id=hn0
>> -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> qemu linux.img -netdev
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0,id=hn0
>> -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>> Signed-off-by: Richa Marwaha<rmarwah@linux.vnet.ibm.com>
>> Signed-off-by: Corey Bryant<coreyb@linux.vnet.ibm.com>
>> ---
>> configure | 2 +
>> net.c | 29 ++++++++-
>> net.h | 3 +
>> net/tap.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>> net/tap.h | 3 +
>> qemu-options.hx | 74 ++++++++++++++++++----
>> 6 files changed, 281 insertions(+), 17 deletions(-)
>>
>> diff --git a/configure b/configure
>> index 6ed4196..4839694 100755
>> --- a/configure
>> +++ b/configure
>> @@ -2905,6 +2905,8 @@ echo "sysconfdir=$sysconfdir">> $config_host_mak
>> echo "docdir=$docdir">> $config_host_mak
>> echo "confdir=$confdir">> $config_host_mak
>> echo "libexecdir=\${prefix}/libexec">> $config_host_mak
>> +echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"">> $config_host_mak
>> +echo "CONFIG_QEMU_HELPERDIR=\"$prefix/libexec\"">> $config_host_mak
>>
>> case "$cpu" in
>> i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
>>
>> diff --git a/net.c b/net.c
>> index f7bebf8..9296224 100644
>> --- a/net.c
>> +++ b/net.c
>> @@ -952,6 +952,14 @@ static const struct {
>> .type = QEMU_OPT_STRING,
>> .help = "script to shut down the interface",
>> }, {
>> + .name = "br",
>> + .type = QEMU_OPT_STRING,
>> + .help = "bridge name",
>> + }, {
>
> I don't think passing br= makes a whole of sense for -net tap. I think
> it would make more sense to make sure that helper could take a shell
> string so you could do:
>
> -netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=br0"
>
> Regards,
>
> Anthony Liguori
>

Ok but do you think the -net bridge options should remain as-is?  It 
seems like execution of the helper should be consistent.  Here are the 
current options for -net bridge:

-net bridge,helper=/usr/local/libexec/qemu-bridge-helper,br=br0

-- 
Regards,
Corey

>> + .name = "helper",
>> + .type = QEMU_OPT_STRING,
>> + .help = "command to execute to configure bridge",
>> + }, {
>> .name = "sndbuf",
>> .type = QEMU_OPT_SIZE,
>> .help = "send buffer limit"
>> @@ -1049,6 +1057,23 @@ static const struct {
>> { /* end of list */ }
>> },
>> },
>> + [NET_CLIENT_TYPE_BRIDGE] = {
>> + .type = "bridge",
>> + .init = net_init_bridge,
>> + .desc = {
>> + NET_COMMON_PARAMS_DESC,
>> + {
>> + .name = "br",
>> + .type = QEMU_OPT_STRING,
>> + .help = "bridge name",
>> + }, {
>> + .name = "helper",
>> + .type = QEMU_OPT_STRING,
>> + .help = "command to execute to configure bridge",
>> + },
>> + { /* end of list */ }
>> + },
>> + },
>> };
>>
>> int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
>> @@ -1071,7 +1096,8 @@ int net_client_init(Monitor *mon, QemuOpts
>> *opts, int is_netdev)
>> #ifdef CONFIG_VDE
>> strcmp(type, "vde") != 0&&
>> #endif
>> - strcmp(type, "socket") != 0) {
>> + strcmp(type, "socket") != 0&&
>> + strcmp(type, "bridge") != 0) {
>> qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
>> "a netdev backend type");
>> return -1;
>> @@ -1141,6 +1167,7 @@ static int net_host_check_device(const char
>> *device)
>> #ifdef CONFIG_VDE
>> ,"vde"
>> #endif
>> + , "bridge"
>> };
>> for (i = 0; i< sizeof(valid_param_list) / sizeof(char *); i++) {
>> if (!strncmp(valid_param_list[i], device,
>> diff --git a/net.h b/net.h
>> index c6b4190..0fd7e23 100644
>> --- a/net.h
>> +++ b/net.h
>> @@ -36,6 +36,7 @@ typedef enum {
>> NET_CLIENT_TYPE_SOCKET,
>> NET_CLIENT_TYPE_VDE,
>> NET_CLIENT_TYPE_DUMP,
>> + NET_CLIENT_TYPE_BRIDGE,
>>
>> NET_CLIENT_TYPE_MAX
>> } net_client_type;
>> @@ -173,6 +174,8 @@ int do_netdev_del(Monitor *mon, const QDict
>> *qdict, QObject **ret_data);
>>
>> #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
>> #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
>> +#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR
>> "/qemu-bridge-helper"
>> +#define DEFAULT_BRIDGE_INTERFACE "br0"
>>
>> void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
>>
>> diff --git a/net/tap.c b/net/tap.c
>> index 6c27a94..b2b82a1 100644
>> --- a/net/tap.c
>> +++ b/net/tap.c
>> @@ -382,6 +382,143 @@ static int launch_script(const char
>> *setup_script, const char *ifname, int fd)
>> return -1;
>> }
>>
>> +static int recv_fd(int c)
>> +{
>> + int fd;
>> + uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
>> + struct msghdr msg = {
>> + .msg_control = msgbuf,
>> + .msg_controllen = sizeof(msgbuf),
>> + };
>> + struct cmsghdr *cmsg;
>> + struct iovec iov;
>> + uint8_t req[1];
>> + ssize_t len;
>> +
>> + cmsg = CMSG_FIRSTHDR(&msg);
>> + cmsg->cmsg_level = SOL_SOCKET;
>> + cmsg->cmsg_type = SCM_RIGHTS;
>> + cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
>> + msg.msg_controllen = cmsg->cmsg_len;
>> +
>> + iov.iov_base = req;
>> + iov.iov_len = sizeof(req);
>> +
>> + msg.msg_iov =&iov;
>> + msg.msg_iovlen = 1;
>> +
>> + len = recvmsg(c,&msg, 0);
>> + if (len> 0) {
>> + memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
>> + return fd;
>> + }
>> +
>> + return len;
>> +}
>> +
>> +static int net_bridge_run_helper(const char *helper, const char *bridge)
>> +{
>> + sigset_t oldmask, mask;
>> + int pid, status;
>> + char *args[5];
>> + char **parg;
>> + int sv[2];
>> +
>> + sigemptyset(&mask);
>> + sigaddset(&mask, SIGCHLD);
>> + sigprocmask(SIG_BLOCK,&mask,&oldmask);
>> +
>> + if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
>> + return -1;
>> + }
>> +
>> + /* try to launch bridge helper */
>> + pid = fork();
>> + if (pid == 0) {
>> + int open_max = sysconf(_SC_OPEN_MAX), i;
>> + char buf[32];
>> +
>> + snprintf(buf, sizeof(buf), "%d", sv[1]);
>> +
>> + for (i = 0; i< open_max; i++) {
>> + if (i != STDIN_FILENO&&
>> + i != STDOUT_FILENO&&
>> + i != STDERR_FILENO&&
>> + i != sv[1]) {
>> + close(i);
>> + }
>> + }
>> + parg = args;
>> + *parg++ = (char *)helper;
>> + *parg++ = (char *)"--use-vnet";
>> + *parg++ = (char *)bridge;
>> + *parg++ = buf;
>> + *parg++ = NULL;
>> + execv(helper, args);
>> + _exit(1);
>> + } else if (pid> 0) {
>> + int fd;
>> +
>> + close(sv[1]);
>> +
>> + do {
>> + fd = recv_fd(sv[0]);
>> + } while (fd == -1&& errno == EINTR);
>> +
>> + close(sv[0]);
>> +
>> + while (waitpid(pid,&status, 0) != pid) {
>> + /* loop */
>> + }
>> + sigprocmask(SIG_SETMASK,&oldmask, NULL);
>> + if (fd< 0) {
>> + fprintf(stderr, "failed to recv file descriptor\n");
>> + return -1;
>> + }
>> +
>> + if (WIFEXITED(status)&& WEXITSTATUS(status) == 0) {
>> + return fd;
>> + }
>> + }
>> + fprintf(stderr, "failed to launch bridge helper\n");
>> + return -1;
>> +}
>> +
>> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
>> + VLANState *vlan)
>> +{
>> + TAPState *s;
>> + int fd, vnet_hdr;
>> +
>> + if (!qemu_opt_get(opts, "br")) {
>> + qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
>> + }
>> + if (!qemu_opt_get(opts, "helper")) {
>> + qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
>> + }
>> +
>> + fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
>> + qemu_opt_get(opts, "br"));
>> + if (fd == -1) {
>> + return -1;
>> + }
>> +
>> + fcntl(fd, F_SETFL, O_NONBLOCK);
>> +
>> + vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> + s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
>> + if (!s) {
>> + close(fd);
>> + return -1;
>> + }
>> +
>> + snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> + "br=%s", qemu_opt_get(opts, "br"));
>> +
>> + return 0;
>> +}
>> +
>> static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
>> {
>> int fd, vnet_hdr_required;
>> @@ -422,13 +559,17 @@ int net_init_tap(QemuOpts *opts, Monitor *mon,
>> const char *name, VLANState *vlan
>> {
>> TAPState *s;
>> int fd, vnet_hdr = 0;
>> + const char *model;
>>
>> if (qemu_opt_get(opts, "fd")) {
>> if (qemu_opt_get(opts, "ifname") ||
>> qemu_opt_get(opts, "script") ||
>> qemu_opt_get(opts, "downscript") ||
>> - qemu_opt_get(opts, "vnet_hdr")) {
>> - error_report("ifname=, script=, downscript= and vnet_hdr= is invalid
>> with fd=");
>> + qemu_opt_get(opts, "vnet_hdr") ||
>> + qemu_opt_get(opts, "br") ||
>> + qemu_opt_get(opts, "helper")) {
>> + error_report("ifname=, script=, downscript=, vnet_hdr=, "
>> + "br= and helper= are invalid with fd=");
>> return -1;
>> }
>>
>> @@ -440,7 +581,41 @@ int net_init_tap(QemuOpts *opts, Monitor *mon,
>> const char *name, VLANState *vlan
>> fcntl(fd, F_SETFL, O_NONBLOCK);
>>
>> vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> + model = "tap";
>> +
>> + } else if (qemu_opt_get(opts, "helper")) {
>> + if (qemu_opt_get(opts, "ifname") ||
>> + qemu_opt_get(opts, "script") ||
>> + qemu_opt_get(opts, "downscript") ||
>> + qemu_opt_get(opts, "vnet_hdr")) {
>> + error_report("ifname=, script=, downscript=, and vnet_hdr= "
>> + "are invalid with helper=");
>> + return -1;
>> + }
>> +
>> + if (!qemu_opt_get(opts, "br")) {
>> + qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
>> + }
>> +
>> + fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
>> + qemu_opt_get(opts, "br"));
>> + if (fd == -1) {
>> + return -1;
>> + }
>> +
>> + fcntl(fd, F_SETFL, O_NONBLOCK);
>> +
>> + vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> + model = "bridge";
>> +
>> } else {
>> + if (qemu_opt_get(opts, "br")) {
>> + error_report("br= is invalid with script=");
>> + return -1;
>> + }
>> +
>> if (!qemu_opt_get(opts, "script")) {
>> qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
>> }
>> @@ -453,9 +628,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon,
>> const char *name, VLANState *vlan
>> if (fd == -1) {
>> return -1;
>> }
>> +
>> + model = "tap";
>> }
>>
>> - s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
>> + s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
>> if (!s) {
>> close(fd);
>> return -1;
>> @@ -467,6 +644,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon,
>> const char *name, VLANState *vlan
>>
>> if (qemu_opt_get(opts, "fd")) {
>> snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
>> + } else if (qemu_opt_get(opts, "helper")) {
>> + snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> + "helper=%s,br=%s", qemu_opt_get(opts, "helper"),
>> + qemu_opt_get(opts, "br"));
>> } else {
>> const char *ifname, *script, *downscript;
>>
>> diff --git a/net/tap.h b/net/tap.h
>> index e44bd2b..56c591f 100644
>> --- a/net/tap.h
>> +++ b/net/tap.h
>> @@ -57,4 +57,7 @@ int tap_get_fd(VLANClientState *vc);
>> struct vhost_net;
>> struct vhost_net *tap_get_vhost_net(VLANClientState *vc);
>>
>> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
>> + VLANState *vlan);
>> +
>> #endif /* QEMU_NET_TAP_H */
>> diff --git a/qemu-options.hx b/qemu-options.hx
>> index 087a3b9..4f2385d 100644
>> --- a/qemu-options.hx
>> +++ b/qemu-options.hx
>> @@ -1205,11 +1205,14 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>> "-net tap[,vlan=n][,name=str],ifname=name\n"
>> " connect the host TAP network interface to VLAN 'n'\n"
>> #else
>> - "-net
>> tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
>>
>> - " connect the host TAP network interface to VLAN 'n' and use the\n"
>> - " network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
>> - " and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
>> + "-net
>> tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
>>
>> + " connect the host TAP network interface to VLAN 'n' \n"
>> + " use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
>> + " to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT
>> ")\n"
>> + " to deconfigure it\n"
>> " use '[down]script=no' to disable script execution\n"
>> + " use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ")
>> and\n"
>> + " bridge 'br' (default=" DEFAULT_BRIDGE_INTERFACE ") to configure it\n"
>> " use 'fd=h' to connect to an already opened TAP interface\n"
>> " use 'sndbuf=nbytes' to limit the size of the send buffer (the\n"
>> " default is disabled 'sndbuf=0' to enable flow control set
>> 'sndbuf=1048576')\n"
>> @@ -1219,6 +1222,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>> " (only has effect for virtio guests which use MSIX)\n"
>> " use vhostforce=on to force vhost on for non-MSIX virtio guests\n"
>> " use 'vhostfd=h' to connect to an already opened vhost net device\n"
>> + "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
>> + " connects a host TAP network interface to a host bridge device 'br'\n"
>> + " (default=" DEFAULT_BRIDGE_INTERFACE ") using the program 'helper'\n"
>> + " (default=" DEFAULT_BRIDGE_HELPER ")\n"
>> #endif
>> "-net
>> socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
>>
>> " connect the vlan 'n' to another VLAN using a socket connection\n"
>> @@ -1242,6 +1249,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
>> "user|"
>> #endif
>> "tap|"
>> + "bridge|"
>> #ifdef CONFIG_VDE
>> "vde|"
>> #endif
>> @@ -1378,26 +1386,66 @@ processed and applied to -net user. Mixing
>> them with the new configuration
>> syntax gives undefined results. Their use for new applications is
>> discouraged
>> as they will be removed from future versions.
>>
>> -@item -net
>> tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}]
>> [,script=@var{file}][,downscript=@var{dfile}]
>> -Connect the host TAP network interface @var{name} to VLAN @var{n}, use
>> -the network script @var{file} to configure it and the network script
>> +@item -net
>> tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}][,br=@var{bridge}][,helper=@var{helper}]
>>
>> +Connect the host TAP network interface @var{name} to VLAN @var{n}.
>> +
>> +Use the network script @var{file} to configure it and the network script
>> @var{dfile} to deconfigure it. If @var{name} is not provided, the OS
>> -automatically provides one. @option{fd}=@var{h} can be used to specify
>> -the handle of an already opened host TAP interface. The default network
>> -configure script is @file{/etc/qemu-ifup} and the default network
>> -deconfigure script is @file{/etc/qemu-ifdown}. Use @option{script=no}
>> -or @option{downscript=no} to disable script execution. Example:
>> +automatically provides one. The default network configure script is
>> +@file{/etc/qemu-ifup} and the default network deconfigure script is
>> +@file{/etc/qemu-ifdown}. Use @option{script=no} or
>> @option{downscript=no}
>> +to disable script execution.
>> +
>> +If running QEMU as an unprivileged user, use the network helper
>> +@var{helper} to configure the TAP interface. The default network
>> +helper executable is @file{/usr/local/libexec/qemu-bridge-helper}
>> +and the default bridge device is @file{br0}.
>> +
>> +@option{fd}=@var{h} can be used to specify the handle of an already
>> +opened host TAP interface.
>> +
>> +Examples:
>>
>> @example
>> +#launch a QEMU instance with the default network script
>> qemu linux.img -net nic -net tap
>> @end example
>>
>> -More complicated example (two NICs, each one connected to a TAP device)
>> @example
>> +#launch a QEMU instance with two NICs, each one connected
>> +#to a TAP device
>> qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
>> -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
>> @end example
>>
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge br0
>> +qemu linux.img -net nic -net
>> tap,helper=/usr/local/libexec/qemu-bridge-helper
>> +@end example
>> +
>> +@item -net
>> bridge[,vlan=@var{n}][,name=@var{name}][,br=@var{bridge}][,helper=@var{helper}]
>>
>> +Connect a host TAP network interface to a host bridge device.
>> +
>> +Use the network helper @var{helper} to configure the TAP interface and
>> +attach it to the bridge. The default network helper executable is
>> +@file{/usr/local/libexec/qemu-bridge-helper} and the default bridge
>> +device is @file{br0}.
>> +
>> +Examples:
>> +
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge br0
>> +qemu linux.img -net bridge -net nic,model=virtio
>> +@end example
>> +
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge qemubr0
>> +qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>> +@end example
>> +
>> @item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}]
>> [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
>>
>> Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 22:55     ` Corey Bryant
@ 2011-12-19 23:15       ` Anthony Liguori
  2011-12-20 17:13         ` Corey Bryant
  0 siblings, 1 reply; 12+ messages in thread
From: Anthony Liguori @ 2011-12-19 23:15 UTC (permalink / raw)
  To: Corey Bryant; +Cc: Anthony Liguori, rmarwah, qemu-devel

On 12/19/2011 04:55 PM, Corey Bryant wrote:
>
>
>>> diff --git a/net.c b/net.c
>>> index f7bebf8..9296224 100644
>>> --- a/net.c
>>> +++ b/net.c
>>> @@ -952,6 +952,14 @@ static const struct {
>>> .type = QEMU_OPT_STRING,
>>> .help = "script to shut down the interface",
>>> }, {
>>> + .name = "br",
>>> + .type = QEMU_OPT_STRING,
>>> + .help = "bridge name",
>>> + }, {
>>
>> I don't think passing br= makes a whole of sense for -net tap. I think
>> it would make more sense to make sure that helper could take a shell
>> string so you could do:
>>
>> -netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=br0"
>>
>> Regards,
>>
>> Anthony Liguori
>>
>
> Ok but do you think the -net bridge options should remain as-is? It seems like
> execution of the helper should be consistent. Here are the current options for
> -net bridge:
>
> -net bridge,helper=/usr/local/libexec/qemu-bridge-helper,br=br0

Yes.  -net bridge is syntactic sugar for -net tap with specific knowledge of the 
qemu-bridge-helper.

If someone wrote a 'qemu-openvswitch-helper' then you could imagine a '-net 
openvswitch' option that passed a bunch of openvswitch specific arguments.

Regards,

Anthony Liguori

>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 4/4] Add support for net bridge Corey Bryant
  2011-12-19 19:36   ` Anthony Liguori
@ 2011-12-20 10:02   ` Hui Kai Ran
  2011-12-20 10:58     ` Hui Kai Ran
  1 sibling, 1 reply; 12+ messages in thread
From: Hui Kai Ran @ 2011-12-20 10:02 UTC (permalink / raw)
  To: Corey Bryant; +Cc: rmarwah, aliguori, qemu-devel

On 12/19/2011 09:11 PM, Corey Bryant wrote:
> The most common use of -net tap is to connect a tap device to a bridge.  This
> requires the use of a script and running qemu as root in order to allocate a
> tap device to pass to the script.
>
> This model is great for portability and flexibility but it's incredibly
> difficult to eliminate the need to run qemu as root.  The only really viable
> mechanism is to use tunctl to create a tap device, attach it to a bridge as
> root, and then hand that tap device to qemu.  The problem with this mechanism
> is that it requires administrator intervention whenever a user wants to create
> a guest.
>
> By essentially writing a helper that implements the most common qemu-ifup
> script that can be safely given cap_net_admin, we can dramatically simplify
> things for non-privileged users.  We still support existing -net tap options
> as a mechanism for advanced users and backwards compatibility.
>
> Currently, this is very Linux centric but there's really no reason why it
> couldn't be extended for other Unixes.
>
> A typical invocation would be similar to one of the following:
>
>    qemu linux.img -net bridge -net nic,model=virtio
>
>    qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
>                   -net nic,model=virtio
>
>    qemu linux.img -netdev bridge,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
>    qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
> The default bridge that we attach to is br0.  The thinking is that a distro
> could preconfigure such an interface to allow out-of-the-box bridged networking.
>
> Alternatively, if a user wants to use a different bridge, a typical invocation
> would be simliar to one of the following:
>
>    qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>
>    qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0
>                   -net nic,model=virtio
>
>    qemu linux.img -netdev bridge,br=qemubr0,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
>    qemu linux.img -netdev tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0,id=hn0
>                   -device virtio-net-pci,netdev=hn0,id=nic1
>
> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
> Signed-off-by: Richa Marwaha<rmarwah@linux.vnet.ibm.com>
> Signed-off-by: Corey Bryant<coreyb@linux.vnet.ibm.com>
> ---
>   configure       |    2 +
>   net.c           |   29 ++++++++-
>   net.h           |    3 +
>   net/tap.c       |  187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>   net/tap.h       |    3 +
>   qemu-options.hx |   74 ++++++++++++++++++----
>   6 files changed, 281 insertions(+), 17 deletions(-)
>
> diff --git a/configure b/configure
> index 6ed4196..4839694 100755
> --- a/configure
> +++ b/configure
> @@ -2905,6 +2905,8 @@ echo "sysconfdir=$sysconfdir">>  $config_host_mak
>   echo "docdir=$docdir">>  $config_host_mak
>   echo "confdir=$confdir">>  $config_host_mak
>   echo "libexecdir=\${prefix}/libexec">>  $config_host_mak
> +echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"">>  $config_host_mak
> +echo "CONFIG_QEMU_HELPERDIR=\"$prefix/libexec\"">>  $config_host_mak
>
>   case "$cpu" in
>     i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
> diff --git a/net.c b/net.c
> index f7bebf8..9296224 100644
> --- a/net.c
> +++ b/net.c
> @@ -952,6 +952,14 @@ static const struct {
>                   .type = QEMU_OPT_STRING,
>                   .help = "script to shut down the interface",
>               }, {
> +                .name = "br",
> +                .type = QEMU_OPT_STRING,
> +                .help = "bridge name",
> +            }, {
> +                .name = "helper",
> +                .type = QEMU_OPT_STRING,
> +                .help = "command to execute to configure bridge",
> +            }, {
>                   .name = "sndbuf",
>                   .type = QEMU_OPT_SIZE,
>                   .help = "send buffer limit"
> @@ -1049,6 +1057,23 @@ static const struct {
>               { /* end of list */ }
>           },
>       },
> +    [NET_CLIENT_TYPE_BRIDGE] = {
> +        .type = "bridge",
> +        .init = net_init_bridge,
> +        .desc = {
> +            NET_COMMON_PARAMS_DESC,
> +            {
> +                .name = "br",
> +                .type = QEMU_OPT_STRING,
> +                .help = "bridge name",
> +            }, {
> +                .name = "helper",
> +                .type = QEMU_OPT_STRING,
> +                .help = "command to execute to configure bridge",
> +            },
> +            { /* end of list */ }
> +        },
> +    },
>   };
>
>   int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
> @@ -1071,7 +1096,8 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
>   #ifdef CONFIG_VDE
>               strcmp(type, "vde") != 0&&
>   #endif
> -            strcmp(type, "socket") != 0) {
> +            strcmp(type, "socket") != 0&&
> +            strcmp(type, "bridge") != 0) {
>               qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
>                             "a netdev backend type");
>               return -1;
> @@ -1141,6 +1167,7 @@ static int net_host_check_device(const char *device)
>   #ifdef CONFIG_VDE
>                                          ,"vde"
>   #endif
> +                                       , "bridge"
>       };
>       for (i = 0; i<  sizeof(valid_param_list) / sizeof(char *); i++) {
>           if (!strncmp(valid_param_list[i], device,
> diff --git a/net.h b/net.h
> index c6b4190..0fd7e23 100644
> --- a/net.h
> +++ b/net.h
> @@ -36,6 +36,7 @@ typedef enum {
>       NET_CLIENT_TYPE_SOCKET,
>       NET_CLIENT_TYPE_VDE,
>       NET_CLIENT_TYPE_DUMP,
> +    NET_CLIENT_TYPE_BRIDGE,
>
>       NET_CLIENT_TYPE_MAX
>   } net_client_type;
> @@ -173,6 +174,8 @@ int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
>
>   #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
>   #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
> +#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
> +#define DEFAULT_BRIDGE_INTERFACE "br0"
>
>   void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
>
> diff --git a/net/tap.c b/net/tap.c
> index 6c27a94..b2b82a1 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -382,6 +382,143 @@ static int launch_script(const char *setup_script, const char *ifname, int fd)
>       return -1;
>   }
>
> +static int recv_fd(int c)
> +{
> +    int fd;
> +    uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
> +    struct msghdr msg = {
> +        .msg_control = msgbuf,
> +        .msg_controllen = sizeof(msgbuf),
> +    };
> +    struct cmsghdr *cmsg;
> +    struct iovec iov;
> +    uint8_t req[1];
> +    ssize_t len;
> +
> +    cmsg = CMSG_FIRSTHDR(&msg);
> +    cmsg->cmsg_level = SOL_SOCKET;
> +    cmsg->cmsg_type = SCM_RIGHTS;
> +    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
> +    msg.msg_controllen = cmsg->cmsg_len;
> +
> +    iov.iov_base = req;
> +    iov.iov_len = sizeof(req);
> +
> +    msg.msg_iov =&iov;
> +    msg.msg_iovlen = 1;
> +
> +    len = recvmsg(c,&msg, 0);
> +    if (len>  0) {
> +        memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
> +        return fd;
> +    }
> +
> +    return len;
> +}
> +
> +static int net_bridge_run_helper(const char *helper, const char *bridge)
> +{
> +    sigset_t oldmask, mask;
> +    int pid, status;
> +    char *args[5];
> +    char **parg;
> +    int sv[2];
> +
> +    sigemptyset(&mask);
> +    sigaddset(&mask, SIGCHLD);
> +    sigprocmask(SIG_BLOCK,&mask,&oldmask);
> +
> +    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
> +        return -1;
> +    }
> +
> +    /* try to launch bridge helper */
> +    pid = fork();
> +    if (pid == 0) {
> +        int open_max = sysconf(_SC_OPEN_MAX), i;
> +        char buf[32];
> +
> +        snprintf(buf, sizeof(buf), "%d", sv[1]);
> +
> +        for (i = 0; i<  open_max; i++) {
> +            if (i != STDIN_FILENO&&
> +                i != STDOUT_FILENO&&
> +                i != STDERR_FILENO&&
> +                i != sv[1]) {
> +                close(i);
> +            }
> +        }
> +        parg = args;
> +        *parg++ = (char *)helper;
> +        *parg++ = (char *)"--use-vnet";
> +        *parg++ = (char *)bridge;
> +        *parg++ = buf;
> +        *parg++ = NULL;
> +        execv(helper, args);
> +        _exit(1);
> +    } else if (pid>  0) {
> +        int fd;
> +
> +        close(sv[1]);
> +
> +        do {
> +            fd = recv_fd(sv[0]);
> +        } while (fd == -1&&  errno == EINTR);
> +
> +        close(sv[0]);
> +
> +        while (waitpid(pid,&status, 0) != pid) {
> +            /* loop */
> +        }
> +        sigprocmask(SIG_SETMASK,&oldmask, NULL);
> +        if (fd<  0) {
> +            fprintf(stderr, "failed to recv file descriptor\n");
> +            return -1;
> +        }
> +
> +        if (WIFEXITED(status)&&  WEXITSTATUS(status) == 0) {
> +            return fd;
> +        }
> +    }
> +    fprintf(stderr, "failed to launch bridge helper\n");
> +    return -1;
> +}
> +
> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
> +                    VLANState *vlan)
> +{
> +    TAPState *s;
> +    int fd, vnet_hdr;
> +
> +    if (!qemu_opt_get(opts, "br")) {
> +        qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
> +    }
> +    if (!qemu_opt_get(opts, "helper")) {
> +        qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
> +    }
> +
> +    fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
> +                               qemu_opt_get(opts, "br"));
> +    if (fd == -1) {
> +        return -1;
> +    }
> +
> +    fcntl(fd, F_SETFL, O_NONBLOCK);
> +
> +    vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +    s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
> +    if (!s) {
> +        close(fd);
> +        return -1;
> +    }
> +
> +    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +             "br=%s", qemu_opt_get(opts, "br"));
> +
> +    return 0;
> +}
> +
>   static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
>   {
>       int fd, vnet_hdr_required;
> @@ -422,13 +559,17 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>   {
>       TAPState *s;
>       int fd, vnet_hdr = 0;
> +    const char *model;
>
>       if (qemu_opt_get(opts, "fd")) {
>           if (qemu_opt_get(opts, "ifname") ||
>               qemu_opt_get(opts, "script") ||
>               qemu_opt_get(opts, "downscript") ||
> -            qemu_opt_get(opts, "vnet_hdr")) {
> -            error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
> +            qemu_opt_get(opts, "vnet_hdr") ||
> +            qemu_opt_get(opts, "br") ||
> +            qemu_opt_get(opts, "helper")) {
> +            error_report("ifname=, script=, downscript=, vnet_hdr=, "
> +                         "br= and helper= are invalid with fd=");
>               return -1;
>           }
>
> @@ -440,7 +581,41 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>           fcntl(fd, F_SETFL, O_NONBLOCK);
>
>           vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +        model = "tap";
> +
> +    } else if (qemu_opt_get(opts, "helper")) {
> +        if (qemu_opt_get(opts, "ifname") ||
> +            qemu_opt_get(opts, "script") ||
> +            qemu_opt_get(opts, "downscript") ||
> +            qemu_opt_get(opts, "vnet_hdr")) {
> +            error_report("ifname=, script=, downscript=, and vnet_hdr= "
> +                         "are invalid with helper=");
> +            return -1;
> +        }
> +
> +        if (!qemu_opt_get(opts, "br")) {
> +            qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
> +        }
> +
> +        fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
> +                                   qemu_opt_get(opts, "br"));
> +        if (fd == -1) {
> +            return -1;
> +        }
> +
> +        fcntl(fd, F_SETFL, O_NONBLOCK);
> +
> +        vnet_hdr = tap_probe_vnet_hdr(fd);
> +
> +        model = "bridge";
> +
>       } else {
> +        if (qemu_opt_get(opts, "br")) {
> +            error_report("br= is invalid with script=");
> +            return -1;
> +        }
> +
>           if (!qemu_opt_get(opts, "script")) {
>               qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
>           }
> @@ -453,9 +628,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>           if (fd == -1) {
>               return -1;
>           }
> +
> +        model = "tap";
>       }
>
> -    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
> +    s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
>       if (!s) {
>           close(fd);
>           return -1;
> @@ -467,6 +644,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan
>
>       if (qemu_opt_get(opts, "fd")) {
>           snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
> +    } else if (qemu_opt_get(opts, "helper")) {
> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +                "helper=%s,br=%s", qemu_opt_get(opts, "helper"),
> +                qemu_opt_get(opts, "br"));
>       } else {
>           const char *ifname, *script, *downscript;
>
> diff --git a/net/tap.h b/net/tap.h
> index e44bd2b..56c591f 100644
> --- a/net/tap.h
> +++ b/net/tap.h
> @@ -57,4 +57,7 @@ int tap_get_fd(VLANClientState *vc);
>   struct vhost_net;
>   struct vhost_net *tap_get_vhost_net(VLANClientState *vc);
>
> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
> +                    VLANState *vlan);
> +
>   #endif /* QEMU_NET_TAP_H */
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 087a3b9..4f2385d 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -1205,11 +1205,14 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>       "-net tap[,vlan=n][,name=str],ifname=name\n"
>       "                connect the host TAP network interface to VLAN 'n'\n"
>   #else
> -    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
> -    "                connect the host TAP network interface to VLAN 'n' and use the\n"
> -    "                network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
> -    "                and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
> +    "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
> +    "                connect the host TAP network interface to VLAN 'n' \n"
> +    "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
> +    "                to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
> +    "                to deconfigure it\n"
>       "                use '[down]script=no' to disable script execution\n"
> +    "                use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ") and\n"
> +    "                bridge 'br' (default=" DEFAULT_BRIDGE_INTERFACE ") to configure it\n"
>       "                use 'fd=h' to connect to an already opened TAP interface\n"
>       "                use 'sndbuf=nbytes' to limit the size of the send buffer (the\n"
>       "                default is disabled 'sndbuf=0' to enable flow control set 'sndbuf=1048576')\n"
> @@ -1219,6 +1222,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>       "                    (only has effect for virtio guests which use MSIX)\n"
>       "                use vhostforce=on to force vhost on for non-MSIX virtio guests\n"
>       "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
> +    "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
> +    "                connects a host TAP network interface to a host bridge device 'br'\n"
> +    "                (default=" DEFAULT_BRIDGE_INTERFACE ") using the program 'helper'\n"
> +    "                (default=" DEFAULT_BRIDGE_HELPER ")\n"
>   #endif
>       "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
>       "                connect the vlan 'n' to another VLAN using a socket connection\n"
> @@ -1242,6 +1249,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
>       "user|"
>   #endif
>       "tap|"
> +    "bridge|"
>   #ifdef CONFIG_VDE
>       "vde|"
>   #endif
> @@ -1378,26 +1386,66 @@ processed and applied to -net user. Mixing them with the new configuration
>   syntax gives undefined results. Their use for new applications is discouraged
>   as they will be removed from future versions.
>
> -@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}] [,script=@var{file}][,downscript=@var{dfile}]
> -Connect the host TAP network interface @var{name} to VLAN @var{n}, use
> -the network script @var{file} to configure it and the network script
> +@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}][,br=@var{bridge}][,helper=@var{helper}]
> +Connect the host TAP network interface @var{name} to VLAN @var{n}.
> +
> +Use the network script @var{file} to configure it and the network script
>   @var{dfile} to deconfigure it. If @var{name} is not provided, the OS
> -automatically provides one. @option{fd}=@var{h} can be used to specify
> -the handle of an already opened host TAP interface. The default network
> -configure script is @file{/etc/qemu-ifup} and the default network
> -deconfigure script is @file{/etc/qemu-ifdown}. Use @option{script=no}
> -or @option{downscript=no} to disable script execution. Example:
> +automatically provides one. The default network configure script is
> +@file{/etc/qemu-ifup} and the default network deconfigure script is
> +@file{/etc/qemu-ifdown}. Use @option{script=no} or @option{downscript=no}
> +to disable script execution.
> +
> +If running QEMU as an unprivileged user, use the network helper
> +@var{helper} to configure the TAP interface. The default network
> +helper executable is @file{/usr/local/libexec/qemu-bridge-helper}
> +and the default bridge device is @file{br0}.
> +
> +@option{fd}=@var{h} can be used to specify the handle of an already
> +opened host TAP interface.
> +
> +Examples:
>
>   @example
> +#launch a QEMU instance with the default network script
>   qemu linux.img -net nic -net tap
>   @end example
>
> -More complicated example (two NICs, each one connected to a TAP device)
>   @example
> +#launch a QEMU instance with two NICs, each one connected
> +#to a TAP device
>   qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
>                  -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
>   @end example
>
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge br0
> +qemu linux.img -net nic -net tap,helper=/usr/local/libexec/qemu-bridge-helper
> +@end example
> +
> +@item -net bridge[,vlan=@var{n}][,name=@var{name}][,br=@var{bridge}][,helper=@var{helper}]
> +Connect a host TAP network interface to a host bridge device.
> +
> +Use the network helper @var{helper} to configure the TAP interface and
> +attach it to the bridge. The default network helper executable is
> +@file{/usr/local/libexec/qemu-bridge-helper} and the default bridge
> +device is @file{br0}.
> +
> +Examples:
> +
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge br0
> +qemu linux.img -net bridge -net nic,model=virtio
> +@end example
> +
> +@example
> +#launch a QEMU instance with the default network helper to
> +#connect a TAP device to bridge qemubr0
> +qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
> +@end example
> +
>   @item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
>
>   Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
Hi, I applied the patches, but an error  prompts there should be a file 
named bridge.conf ,
how can i find it?

/home/huikai/qemu10/bin/qemu-system-x86_64 -enable-kvm -m 1024 -drive 
file=imgs/sles.img,if=virtio -net bridge  -net nic,model=virtiofailed to 
parse default acl file `/home/huikai/qemu10/etc/qemu/bridge.conf'
failed to launch bridge helper
qemu-system-x86_64: -net bridge: Device 'bridge' could not be initialize

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-20 10:02   ` Hui Kai Ran
@ 2011-12-20 10:58     ` Hui Kai Ran
  0 siblings, 0 replies; 12+ messages in thread
From: Hui Kai Ran @ 2011-12-20 10:58 UTC (permalink / raw)
  To: Corey Bryant; +Cc: rmarwah, aliguori, qemu-devel

On 12/20/2011 06:02 PM, Hui Kai Ran wrote:
> On 12/19/2011 09:11 PM, Corey Bryant wrote:
>> The most common use of -net tap is to connect a tap device to a 
>> bridge.  This
>> requires the use of a script and running qemu as root in order to 
>> allocate a
>> tap device to pass to the script.
>>
>> This model is great for portability and flexibility but it's incredibly
>> difficult to eliminate the need to run qemu as root.  The only really 
>> viable
>> mechanism is to use tunctl to create a tap device, attach it to a 
>> bridge as
>> root, and then hand that tap device to qemu.  The problem with this 
>> mechanism
>> is that it requires administrator intervention whenever a user wants 
>> to create
>> a guest.
>>
>> By essentially writing a helper that implements the most common 
>> qemu-ifup
>> script that can be safely given cap_net_admin, we can dramatically 
>> simplify
>> things for non-privileged users.  We still support existing -net tap 
>> options
>> as a mechanism for advanced users and backwards compatibility.
>>
>> Currently, this is very Linux centric but there's really no reason 
>> why it
>> couldn't be extended for other Unixes.
>>
>> A typical invocation would be similar to one of the following:
>>
>>    qemu linux.img -net bridge -net nic,model=virtio
>>
>>    qemu linux.img -net tap,helper=/usr/local/libexec/qemu-bridge-helper
>>                   -net nic,model=virtio
>>
>>    qemu linux.img -netdev bridge,id=hn0
>>                   -device virtio-net-pci,netdev=hn0,id=nic1
>>
>>    qemu linux.img -netdev 
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,id=hn0
>>                   -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> The default bridge that we attach to is br0.  The thinking is that a 
>> distro
>> could preconfigure such an interface to allow out-of-the-box bridged 
>> networking.
>>
>> Alternatively, if a user wants to use a different bridge, a typical 
>> invocation
>> would be simliar to one of the following:
>>
>>    qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>>
>>    qemu linux.img -net 
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0
>>                   -net nic,model=virtio
>>
>>    qemu linux.img -netdev bridge,br=qemubr0,id=hn0
>>                   -device virtio-net-pci,netdev=hn0,id=nic1
>>
>>    qemu linux.img -netdev 
>> tap,helper=/usr/local/libexec/qemu-bridge-helper,br=qemubr0,id=hn0
>>                   -device virtio-net-pci,netdev=hn0,id=nic1
>>
>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>> Signed-off-by: Richa Marwaha<rmarwah@linux.vnet.ibm.com>
>> Signed-off-by: Corey Bryant<coreyb@linux.vnet.ibm.com>
>> ---
>>   configure       |    2 +
>>   net.c           |   29 ++++++++-
>>   net.h           |    3 +
>>   net/tap.c       |  187 
>> ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>   net/tap.h       |    3 +
>>   qemu-options.hx |   74 ++++++++++++++++++----
>>   6 files changed, 281 insertions(+), 17 deletions(-)
>>
>> diff --git a/configure b/configure
>> index 6ed4196..4839694 100755
>> --- a/configure
>> +++ b/configure
>> @@ -2905,6 +2905,8 @@ echo "sysconfdir=$sysconfdir">>  $config_host_mak
>>   echo "docdir=$docdir">>  $config_host_mak
>>   echo "confdir=$confdir">>  $config_host_mak
>>   echo "libexecdir=\${prefix}/libexec">>  $config_host_mak
>> +echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"">>  $config_host_mak
>> +echo "CONFIG_QEMU_HELPERDIR=\"$prefix/libexec\"">>  $config_host_mak
>>
>>   case "$cpu" in
>>     
>> i386|x86_64|alpha|arm|cris|hppa|ia64|lm32|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64|unicore32)
>> diff --git a/net.c b/net.c
>> index f7bebf8..9296224 100644
>> --- a/net.c
>> +++ b/net.c
>> @@ -952,6 +952,14 @@ static const struct {
>>                   .type = QEMU_OPT_STRING,
>>                   .help = "script to shut down the interface",
>>               }, {
>> +                .name = "br",
>> +                .type = QEMU_OPT_STRING,
>> +                .help = "bridge name",
>> +            }, {
>> +                .name = "helper",
>> +                .type = QEMU_OPT_STRING,
>> +                .help = "command to execute to configure bridge",
>> +            }, {
>>                   .name = "sndbuf",
>>                   .type = QEMU_OPT_SIZE,
>>                   .help = "send buffer limit"
>> @@ -1049,6 +1057,23 @@ static const struct {
>>               { /* end of list */ }
>>           },
>>       },
>> +    [NET_CLIENT_TYPE_BRIDGE] = {
>> +        .type = "bridge",
>> +        .init = net_init_bridge,
>> +        .desc = {
>> +            NET_COMMON_PARAMS_DESC,
>> +            {
>> +                .name = "br",
>> +                .type = QEMU_OPT_STRING,
>> +                .help = "bridge name",
>> +            }, {
>> +                .name = "helper",
>> +                .type = QEMU_OPT_STRING,
>> +                .help = "command to execute to configure bridge",
>> +            },
>> +            { /* end of list */ }
>> +        },
>> +    },
>>   };
>>
>>   int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
>> @@ -1071,7 +1096,8 @@ int net_client_init(Monitor *mon, QemuOpts 
>> *opts, int is_netdev)
>>   #ifdef CONFIG_VDE
>>               strcmp(type, "vde") != 0&&
>>   #endif
>> -            strcmp(type, "socket") != 0) {
>> +            strcmp(type, "socket") != 0&&
>> +            strcmp(type, "bridge") != 0) {
>>               qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
>>                             "a netdev backend type");
>>               return -1;
>> @@ -1141,6 +1167,7 @@ static int net_host_check_device(const char 
>> *device)
>>   #ifdef CONFIG_VDE
>>                                          ,"vde"
>>   #endif
>> +                                       , "bridge"
>>       };
>>       for (i = 0; i<  sizeof(valid_param_list) / sizeof(char *); i++) {
>>           if (!strncmp(valid_param_list[i], device,
>> diff --git a/net.h b/net.h
>> index c6b4190..0fd7e23 100644
>> --- a/net.h
>> +++ b/net.h
>> @@ -36,6 +36,7 @@ typedef enum {
>>       NET_CLIENT_TYPE_SOCKET,
>>       NET_CLIENT_TYPE_VDE,
>>       NET_CLIENT_TYPE_DUMP,
>> +    NET_CLIENT_TYPE_BRIDGE,
>>
>>       NET_CLIENT_TYPE_MAX
>>   } net_client_type;
>> @@ -173,6 +174,8 @@ int do_netdev_del(Monitor *mon, const QDict 
>> *qdict, QObject **ret_data);
>>
>>   #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
>>   #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
>> +#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR 
>> "/qemu-bridge-helper"
>> +#define DEFAULT_BRIDGE_INTERFACE "br0"
>>
>>   void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
>>
>> diff --git a/net/tap.c b/net/tap.c
>> index 6c27a94..b2b82a1 100644
>> --- a/net/tap.c
>> +++ b/net/tap.c
>> @@ -382,6 +382,143 @@ static int launch_script(const char 
>> *setup_script, const char *ifname, int fd)
>>       return -1;
>>   }
>>
>> +static int recv_fd(int c)
>> +{
>> +    int fd;
>> +    uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
>> +    struct msghdr msg = {
>> +        .msg_control = msgbuf,
>> +        .msg_controllen = sizeof(msgbuf),
>> +    };
>> +    struct cmsghdr *cmsg;
>> +    struct iovec iov;
>> +    uint8_t req[1];
>> +    ssize_t len;
>> +
>> +    cmsg = CMSG_FIRSTHDR(&msg);
>> +    cmsg->cmsg_level = SOL_SOCKET;
>> +    cmsg->cmsg_type = SCM_RIGHTS;
>> +    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
>> +    msg.msg_controllen = cmsg->cmsg_len;
>> +
>> +    iov.iov_base = req;
>> +    iov.iov_len = sizeof(req);
>> +
>> +    msg.msg_iov =&iov;
>> +    msg.msg_iovlen = 1;
>> +
>> +    len = recvmsg(c,&msg, 0);
>> +    if (len>  0) {
>> +        memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
>> +        return fd;
>> +    }
>> +
>> +    return len;
>> +}
>> +
>> +static int net_bridge_run_helper(const char *helper, const char 
>> *bridge)
>> +{
>> +    sigset_t oldmask, mask;
>> +    int pid, status;
>> +    char *args[5];
>> +    char **parg;
>> +    int sv[2];
>> +
>> +    sigemptyset(&mask);
>> +    sigaddset(&mask, SIGCHLD);
>> +    sigprocmask(SIG_BLOCK,&mask,&oldmask);
>> +
>> +    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
>> +        return -1;
>> +    }
>> +
>> +    /* try to launch bridge helper */
>> +    pid = fork();
>> +    if (pid == 0) {
>> +        int open_max = sysconf(_SC_OPEN_MAX), i;
>> +        char buf[32];
>> +
>> +        snprintf(buf, sizeof(buf), "%d", sv[1]);
>> +
>> +        for (i = 0; i<  open_max; i++) {
>> +            if (i != STDIN_FILENO&&
>> +                i != STDOUT_FILENO&&
>> +                i != STDERR_FILENO&&
>> +                i != sv[1]) {
>> +                close(i);
>> +            }
>> +        }
>> +        parg = args;
>> +        *parg++ = (char *)helper;
>> +        *parg++ = (char *)"--use-vnet";
>> +        *parg++ = (char *)bridge;
>> +        *parg++ = buf;
>> +        *parg++ = NULL;
>> +        execv(helper, args);
>> +        _exit(1);
>> +    } else if (pid>  0) {
>> +        int fd;
>> +
>> +        close(sv[1]);
>> +
>> +        do {
>> +            fd = recv_fd(sv[0]);
>> +        } while (fd == -1&&  errno == EINTR);
>> +
>> +        close(sv[0]);
>> +
>> +        while (waitpid(pid,&status, 0) != pid) {
>> +            /* loop */
>> +        }
>> +        sigprocmask(SIG_SETMASK,&oldmask, NULL);
>> +        if (fd<  0) {
>> +            fprintf(stderr, "failed to recv file descriptor\n");
>> +            return -1;
>> +        }
>> +
>> +        if (WIFEXITED(status)&&  WEXITSTATUS(status) == 0) {
>> +            return fd;
>> +        }
>> +    }
>> +    fprintf(stderr, "failed to launch bridge helper\n");
>> +    return -1;
>> +}
>> +
>> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
>> +                    VLANState *vlan)
>> +{
>> +    TAPState *s;
>> +    int fd, vnet_hdr;
>> +
>> +    if (!qemu_opt_get(opts, "br")) {
>> +        qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
>> +    }
>> +    if (!qemu_opt_get(opts, "helper")) {
>> +        qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
>> +    }
>> +
>> +    fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
>> +                               qemu_opt_get(opts, "br"));
>> +    if (fd == -1) {
>> +        return -1;
>> +    }
>> +
>> +    fcntl(fd, F_SETFL, O_NONBLOCK);
>> +
>> +    vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> +    s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
>> +    if (!s) {
>> +        close(fd);
>> +        return -1;
>> +    }
>> +
>> +    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> +             "br=%s", qemu_opt_get(opts, "br"));
>> +
>> +    return 0;
>> +}
>> +
>>   static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
>>   {
>>       int fd, vnet_hdr_required;
>> @@ -422,13 +559,17 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, 
>> const char *name, VLANState *vlan
>>   {
>>       TAPState *s;
>>       int fd, vnet_hdr = 0;
>> +    const char *model;
>>
>>       if (qemu_opt_get(opts, "fd")) {
>>           if (qemu_opt_get(opts, "ifname") ||
>>               qemu_opt_get(opts, "script") ||
>>               qemu_opt_get(opts, "downscript") ||
>> -            qemu_opt_get(opts, "vnet_hdr")) {
>> -            error_report("ifname=, script=, downscript= and 
>> vnet_hdr= is invalid with fd=");
>> +            qemu_opt_get(opts, "vnet_hdr") ||
>> +            qemu_opt_get(opts, "br") ||
>> +            qemu_opt_get(opts, "helper")) {
>> +            error_report("ifname=, script=, downscript=, vnet_hdr=, "
>> +                         "br= and helper= are invalid with fd=");
>>               return -1;
>>           }
>>
>> @@ -440,7 +581,41 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, 
>> const char *name, VLANState *vlan
>>           fcntl(fd, F_SETFL, O_NONBLOCK);
>>
>>           vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> +        model = "tap";
>> +
>> +    } else if (qemu_opt_get(opts, "helper")) {
>> +        if (qemu_opt_get(opts, "ifname") ||
>> +            qemu_opt_get(opts, "script") ||
>> +            qemu_opt_get(opts, "downscript") ||
>> +            qemu_opt_get(opts, "vnet_hdr")) {
>> +            error_report("ifname=, script=, downscript=, and 
>> vnet_hdr= "
>> +                         "are invalid with helper=");
>> +            return -1;
>> +        }
>> +
>> +        if (!qemu_opt_get(opts, "br")) {
>> +            qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
>> +        }
>> +
>> +        fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
>> +                                   qemu_opt_get(opts, "br"));
>> +        if (fd == -1) {
>> +            return -1;
>> +        }
>> +
>> +        fcntl(fd, F_SETFL, O_NONBLOCK);
>> +
>> +        vnet_hdr = tap_probe_vnet_hdr(fd);
>> +
>> +        model = "bridge";
>> +
>>       } else {
>> +        if (qemu_opt_get(opts, "br")) {
>> +            error_report("br= is invalid with script=");
>> +            return -1;
>> +        }
>> +
>>           if (!qemu_opt_get(opts, "script")) {
>>               qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
>>           }
>> @@ -453,9 +628,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, 
>> const char *name, VLANState *vlan
>>           if (fd == -1) {
>>               return -1;
>>           }
>> +
>> +        model = "tap";
>>       }
>>
>> -    s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
>> +    s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
>>       if (!s) {
>>           close(fd);
>>           return -1;
>> @@ -467,6 +644,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, 
>> const char *name, VLANState *vlan
>>
>>       if (qemu_opt_get(opts, "fd")) {
>>           snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
>> +    } else if (qemu_opt_get(opts, "helper")) {
>> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> +                "helper=%s,br=%s", qemu_opt_get(opts, "helper"),
>> +                qemu_opt_get(opts, "br"));
>>       } else {
>>           const char *ifname, *script, *downscript;
>>
>> diff --git a/net/tap.h b/net/tap.h
>> index e44bd2b..56c591f 100644
>> --- a/net/tap.h
>> +++ b/net/tap.h
>> @@ -57,4 +57,7 @@ int tap_get_fd(VLANClientState *vc);
>>   struct vhost_net;
>>   struct vhost_net *tap_get_vhost_net(VLANClientState *vc);
>>
>> +int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
>> +                    VLANState *vlan);
>> +
>>   #endif /* QEMU_NET_TAP_H */
>> diff --git a/qemu-options.hx b/qemu-options.hx
>> index 087a3b9..4f2385d 100644
>> --- a/qemu-options.hx
>> +++ b/qemu-options.hx
>> @@ -1205,11 +1205,14 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>>       "-net tap[,vlan=n][,name=str],ifname=name\n"
>>       "                connect the host TAP network interface to VLAN 
>> 'n'\n"
>>   #else
>> -    "-net 
>> tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
>> -    "                connect the host TAP network interface to VLAN 
>> 'n' and use the\n"
>> -    "                network scripts 'file' (default=" 
>> DEFAULT_NETWORK_SCRIPT ")\n"
>> -    "                and 'dfile' (default=" 
>> DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
>> +    "-net 
>> tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]\n"
>> +    "                connect the host TAP network interface to VLAN 
>> 'n' \n"
>> +    "                use network scripts 'file' (default=" 
>> DEFAULT_NETWORK_SCRIPT ")\n"
>> +    "                to configure it and 'dfile' (default=" 
>> DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
>> +    "                to deconfigure it\n"
>>       "                use '[down]script=no' to disable script 
>> execution\n"
>> +    "                use network helper 'helper' (default=" 
>> DEFAULT_BRIDGE_HELPER ") and\n"
>> +    "                bridge 'br' (default=" DEFAULT_BRIDGE_INTERFACE 
>> ") to configure it\n"
>>       "                use 'fd=h' to connect to an already opened TAP 
>> interface\n"
>>       "                use 'sndbuf=nbytes' to limit the size of the 
>> send buffer (the\n"
>>       "                default is disabled 'sndbuf=0' to enable flow 
>> control set 'sndbuf=1048576')\n"
>> @@ -1219,6 +1222,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>>       "                    (only has effect for virtio guests which 
>> use MSIX)\n"
>>       "                use vhostforce=on to force vhost on for 
>> non-MSIX virtio guests\n"
>>       "                use 'vhostfd=h' to connect to an already 
>> opened vhost net device\n"
>> +    "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
>> +    "                connects a host TAP network interface to a host 
>> bridge device 'br'\n"
>> +    "                (default=" DEFAULT_BRIDGE_INTERFACE ") using 
>> the program 'helper'\n"
>> +    "                (default=" DEFAULT_BRIDGE_HELPER ")\n"
>>   #endif
>>       "-net 
>> socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
>>       "                connect the vlan 'n' to another VLAN using a 
>> socket connection\n"
>> @@ -1242,6 +1249,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
>>       "user|"
>>   #endif
>>       "tap|"
>> +    "bridge|"
>>   #ifdef CONFIG_VDE
>>       "vde|"
>>   #endif
>> @@ -1378,26 +1386,66 @@ processed and applied to -net user. Mixing 
>> them with the new configuration
>>   syntax gives undefined results. Their use for new applications is 
>> discouraged
>>   as they will be removed from future versions.
>>
>> -@item -net 
>> tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}] 
>> [,script=@var{file}][,downscript=@var{dfile}]
>> -Connect the host TAP network interface @var{name} to VLAN @var{n}, use
>> -the network script @var{file} to configure it and the network script
>> +@item -net 
>> tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}][,br=@var{bridge}][,helper=@var{helper}]
>> +Connect the host TAP network interface @var{name} to VLAN @var{n}.
>> +
>> +Use the network script @var{file} to configure it and the network 
>> script
>>   @var{dfile} to deconfigure it. If @var{name} is not provided, the OS
>> -automatically provides one. @option{fd}=@var{h} can be used to specify
>> -the handle of an already opened host TAP interface. The default network
>> -configure script is @file{/etc/qemu-ifup} and the default network
>> -deconfigure script is @file{/etc/qemu-ifdown}. Use @option{script=no}
>> -or @option{downscript=no} to disable script execution. Example:
>> +automatically provides one. The default network configure script is
>> +@file{/etc/qemu-ifup} and the default network deconfigure script is
>> +@file{/etc/qemu-ifdown}. Use @option{script=no} or 
>> @option{downscript=no}
>> +to disable script execution.
>> +
>> +If running QEMU as an unprivileged user, use the network helper
>> +@var{helper} to configure the TAP interface. The default network
>> +helper executable is @file{/usr/local/libexec/qemu-bridge-helper}
>> +and the default bridge device is @file{br0}.
>> +
>> +@option{fd}=@var{h} can be used to specify the handle of an already
>> +opened host TAP interface.
>> +
>> +Examples:
>>
>>   @example
>> +#launch a QEMU instance with the default network script
>>   qemu linux.img -net nic -net tap
>>   @end example
>>
>> -More complicated example (two NICs, each one connected to a TAP device)
>>   @example
>> +#launch a QEMU instance with two NICs, each one connected
>> +#to a TAP device
>>   qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
>>                  -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
>>   @end example
>>
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge br0
>> +qemu linux.img -net nic -net 
>> tap,helper=/usr/local/libexec/qemu-bridge-helper
>> +@end example
>> +
>> +@item -net 
>> bridge[,vlan=@var{n}][,name=@var{name}][,br=@var{bridge}][,helper=@var{helper}]
>> +Connect a host TAP network interface to a host bridge device.
>> +
>> +Use the network helper @var{helper} to configure the TAP interface and
>> +attach it to the bridge. The default network helper executable is
>> +@file{/usr/local/libexec/qemu-bridge-helper} and the default bridge
>> +device is @file{br0}.
>> +
>> +Examples:
>> +
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge br0
>> +qemu linux.img -net bridge -net nic,model=virtio
>> +@end example
>> +
>> +@example
>> +#launch a QEMU instance with the default network helper to
>> +#connect a TAP device to bridge qemubr0
>> +qemu linux.img -net bridge,br=qemubr0 -net nic,model=virtio
>> +@end example
>> +
>>   @item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] 
>> [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
>>
>>   Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
> Hi, I applied the patches, but an error  prompts there should be a 
> file named bridge.conf ,
> how can i find it?
>
> /home/huikai/qemu10/bin/qemu-system-x86_64 -enable-kvm -m 1024 -drive 
> file=imgs/sles.img,if=virtio -net bridge  -net nic,model=virtiofailed 
> to parse default acl file `/home/huikai/qemu10/etc/qemu/bridge.conf'
> failed to launch bridge helper
> qemu-system-x86_64: -net bridge: Device 'bridge' could not be initialize
>
>
Sorry, i found it in the patch set.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-19 23:15       ` Anthony Liguori
@ 2011-12-20 17:13         ` Corey Bryant
  2011-12-22 15:54           ` Anthony Liguori
  0 siblings, 1 reply; 12+ messages in thread
From: Corey Bryant @ 2011-12-20 17:13 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: Anthony Liguori, rmarwah, qemu-devel



On 12/19/2011 06:15 PM, Anthony Liguori wrote:
> On 12/19/2011 04:55 PM, Corey Bryant wrote:
>>
>>
>>>> diff --git a/net.c b/net.c
>>>> index f7bebf8..9296224 100644
>>>> --- a/net.c
>>>> +++ b/net.c
>>>> @@ -952,6 +952,14 @@ static const struct {
>>>> .type = QEMU_OPT_STRING,
>>>> .help = "script to shut down the interface",
>>>> }, {
>>>> + .name = "br",
>>>> + .type = QEMU_OPT_STRING,
>>>> + .help = "bridge name",
>>>> + }, {
>>>
>>> I don't think passing br= makes a whole of sense for -net tap. I think
>>> it would make more sense to make sure that helper could take a shell
>>> string so you could do:
>>>
>>> -netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=br0"
>>>
>>> Regards,
>>>
>>> Anthony Liguori
>>>
>>
>> Ok but do you think the -net bridge options should remain as-is? It
>> seems like
>> execution of the helper should be consistent. Here are the current
>> options for
>> -net bridge:
>>
>> -net bridge,helper=/usr/local/libexec/qemu-bridge-helper,br=br0
>
> Yes. -net bridge is syntactic sugar for -net tap with specific knowledge
> of the qemu-bridge-helper.
>
> If someone wrote a 'qemu-openvswitch-helper' then you could imagine a
> '-net openvswitch' option that passed a bunch of openvswitch specific
> arguments.
>
> Regards,
>
> Anthony Liguori
>
>>
>
>

It seems like the helper should accept the following arguments:

--vnet-hdr --br=<bridge name> --fd=<unix fd>

(It already accept these, but the --br= and --fd= syntax aren't required 
at the moment.)

Then QEMU would only allow the following to be specified for -netdev tap:

-netdev tap,helper="/usr/libexec/qemu-bridge-helper"
or
-netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=bridge"

and would ignore or reject --vnet-hdr and --fd=.  --vnet-hdr and --fd= 
would always be specified internally when the helper is exec'd.

-- 
Regards,
Corey

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Qemu-devel] [PATCH v6 4/4] Add support for net bridge
  2011-12-20 17:13         ` Corey Bryant
@ 2011-12-22 15:54           ` Anthony Liguori
  0 siblings, 0 replies; 12+ messages in thread
From: Anthony Liguori @ 2011-12-22 15:54 UTC (permalink / raw)
  To: Corey Bryant; +Cc: Anthony Liguori, rmarwah, qemu-devel

On 12/20/2011 11:13 AM, Corey Bryant wrote:
>
>
> On 12/19/2011 06:15 PM, Anthony Liguori wrote:
>> On 12/19/2011 04:55 PM, Corey Bryant wrote:
>>>
>>>
>>>>> diff --git a/net.c b/net.c
>>>>> index f7bebf8..9296224 100644
>>>>> --- a/net.c
>>>>> +++ b/net.c
>>>>> @@ -952,6 +952,14 @@ static const struct {
>>>>> .type = QEMU_OPT_STRING,
>>>>> .help = "script to shut down the interface",
>>>>> }, {
>>>>> + .name = "br",
>>>>> + .type = QEMU_OPT_STRING,
>>>>> + .help = "bridge name",
>>>>> + }, {
>>>>
>>>> I don't think passing br= makes a whole of sense for -net tap. I think
>>>> it would make more sense to make sure that helper could take a shell
>>>> string so you could do:
>>>>
>>>> -netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=br0"
>>>>
>>>> Regards,
>>>>
>>>> Anthony Liguori
>>>>
>>>
>>> Ok but do you think the -net bridge options should remain as-is? It
>>> seems like
>>> execution of the helper should be consistent. Here are the current
>>> options for
>>> -net bridge:
>>>
>>> -net bridge,helper=/usr/local/libexec/qemu-bridge-helper,br=br0
>>
>> Yes. -net bridge is syntactic sugar for -net tap with specific knowledge
>> of the qemu-bridge-helper.
>>
>> If someone wrote a 'qemu-openvswitch-helper' then you could imagine a
>> '-net openvswitch' option that passed a bunch of openvswitch specific
>> arguments.
>>
>> Regards,
>>
>> Anthony Liguori
>>
>>>
>>
>>
>
> It seems like the helper should accept the following arguments:
>
> --vnet-hdr --br=<bridge name> --fd=<unix fd>
>
> (It already accept these, but the --br= and --fd= syntax aren't required at the
> moment.)
>
> Then QEMU would only allow the following to be specified for -netdev tap:
>
> -netdev tap,helper="/usr/libexec/qemu-bridge-helper"
> or
> -netdev tap,helper="/usr/libexec/qemu-bridge-helper --br=bridge"
>
> and would ignore or reject --vnet-hdr and --fd=. --vnet-hdr and --fd= would
> always be specified internally when the helper is exec'd.

I don't know what you mean by "ignore or reject".  Just take whatever the helper 
string is, concat " --vnet-hdr --fd=" as appropriate, and execute it via /bin/sh.

Regards,

Anthony Liguori

>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-12-22 15:54 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-19 13:11 [Qemu-devel] [PATCH v6 0/4] -net bridge: rootless bridge support for qemu Corey Bryant
2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 1/4] Add basic version of bridge helper Corey Bryant
2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 2/4] Add access control support to qemu " Corey Bryant
2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 3/4] Add cap reduction support to enable use as SUID Corey Bryant
2011-12-19 13:11 ` [Qemu-devel] [PATCH v6 4/4] Add support for net bridge Corey Bryant
2011-12-19 19:36   ` Anthony Liguori
2011-12-19 22:55     ` Corey Bryant
2011-12-19 23:15       ` Anthony Liguori
2011-12-20 17:13         ` Corey Bryant
2011-12-22 15:54           ` Anthony Liguori
2011-12-20 10:02   ` Hui Kai Ran
2011-12-20 10:58     ` Hui Kai Ran

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.