qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devi


From: Arnd Bergmann
Subject: [Qemu-devel] [PATCH, RFC] tap-linux: support opening arbitrary char devices
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; ; )

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 <address@hidden>

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;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]