From patchwork Thu Nov 26 19:34:04 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 39587 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7C7661007D4 for ; Fri, 27 Nov 2009 06:36:25 +1100 (EST) Received: from localhost ([127.0.0.1]:49045 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NDk8c-00022k-IZ for incoming@patchwork.ozlabs.org; Thu, 26 Nov 2009 14:36:22 -0500 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 Received: from klappe2.localnet (deibp9eh1--blueice3n1.emea.ibm.com [195.212.29.179]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0Lualk-1ODARb3lPk-010Km2; Thu, 26 Nov 2009 20:34:08 +0100 From: Arnd Bergmann To: qemu-devel@nongnu.org Date: Thu, 26 Nov 2009 20:34:04 +0100 User-Agent: KMail/1.12.2 (Linux/2.6.31-14-generic; KDE/4.3.2; x86_64; ; ) MIME-Version: 1.0 Message-Id: <200911262034.04440.arnd@arndb.de> X-Provags-ID: V01U2FsdGVkX18wyEeGDajDF+rm1VeBHXil+oDfqq845D+WAzV ECz6PFJZ7GmOYz06dmQLDEGqHHTP0SlTMkutnol+KKLLnveXEh OMT6UggEEp78GSRnRnpvw== X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@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; }