From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NDk6a-0000EP-B5 for qemu-devel@nongnu.org; Thu, 26 Nov 2009 14:34:16 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NDk6U-00005b-SG for qemu-devel@nongnu.org; Thu, 26 Nov 2009 14:34:15 -0500 Received: from [199.232.76.173] (port=34949 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NDk6U-00005P-IS for qemu-devel@nongnu.org; Thu, 26 Nov 2009 14:34:10 -0500 Received: from moutng.kundenserver.de ([212.227.17.8]:61174) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NDk6U-0003mb-47 for qemu-devel@nongnu.org; Thu, 26 Nov 2009 14:34:10 -0500 From: Arnd Bergmann Date: Thu, 26 Nov 2009 20:34:04 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <200911262034.04440.arnd@arndb.de> Subject: [Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devices List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org With the upcoming macvtap, we will want to open devices other than /dev/net/tun but no longer need to call TUNSETIFF. This makes it possible to do 'qemu -net tap,ifname=/dev/tap/macvtap0' to refer to a chardev in addition to the current way of doing 'qemu -net tap,ifname=tap0' to refer to a tap network interface set with TUNSETIFF. This is consistent with what we do on BSD. This is guaranteed not to cause conflicts with existing usage, because network devices on Linux cannot start with a '/'. Alternatively, I could do a patch to use the -net tap,name= parameter which documented to have a similar purpose but not currently implemented at all. Signed-off-by: Arnd Bergmann diff --git a/net/tap-linux.c b/net/tap-linux.c index 0f621a2..21f0167 100644 --- a/net/tap-linux.c +++ b/net/tap-linux.c @@ -36,10 +36,20 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required { struct ifreq ifr; int fd, ret; + int open_named; + char *devname; + + if (ifname_size >= 1 && *ifname == "/") { + open_named = 1; + devname = ifname; + } else { + open_named = 0; + devname = "/dev/net/tun"; + } - TFR(fd = open("/dev/net/tun", O_RDWR)); + TFR(fd = open(devname, O_RDWR)); if (fd < 0) { - fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); + fprintf(stderr, "warning: could not open %s: no virtual network emulation\n", devname); return -1; } memset(&ifr, 0, sizeof(ifr)); @@ -62,17 +72,20 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required } } - if (ifname[0] != '\0') - pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); - else - pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); - ret = ioctl(fd, TUNSETIFF, (void *) &ifr); - if (ret != 0) { - fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); - close(fd); - return -1; + if (!open_named) { + if (ifname[0] != '\0') + pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); + else + pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); + ret = ioctl(fd, TUNSETIFF, (void *) &ifr); + if (ret != 0) { + fprintf(stderr, "warning: could not configure %s: no virtual " + "network emulation\n", devname); + close(fd); + return -1; + } + pstrcpy(ifname, ifname_size, ifr.ifr_name); } - pstrcpy(ifname, ifname_size, ifr.ifr_name); fcntl(fd, F_SETFL, O_NONBLOCK); return fd; }