Skip to contentSkip to conversation

tun/tap patch for OSX

Qemu-devel Lev Lvovsky 3 participants 4 messages Dec 29, 2008 – Jan 8, 2009

lore
Share & tools
lore
#0 lore raw

The following patch provided by Mattias Nissler, and tweaked by myself to be applied to net.c (previously to vl.c) allows qemu to use the tun/tap devices provided at Mattias' site (http://tuntaposx.sourceforge.net).

Mattias is CC'd.

thanks,
-lev

----
bash-3.2$ svn diff net.c

net.c (working copy) renamed from net.c (revision 6082) +0 -0
Index: net.c
===================================================================
@@ -686,8 +686,36 @@
     snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d",  
fd);
     return s;

}
+#if defined(__APPLE__)
+static int tap_open(char *ifname, int ifname_size) +{

+    int fd;
+    int i = 0;
+    char *dev;
+    char buf[20];
+    struct stat s;

-#if defined (_BSD) || defined (__FreeBSD_kernel__)
+ while (1) {

+        snprintf(buf, 20, "/dev/tap%d", i);
+        fd = open(buf, O_RDWR);
+        if (fd < 0) {

+ if (errno != EBUSY) {
+ fprintf(stderr, "warning: could not open %s: no virtual network emulation\n", buf);
+ return -1;
+ }
+ i++;
+ } else
+ break;
+ }
+

+    fstat(fd, &s);
+    dev = devname(s.st_rdev, S_IFCHR);
+    pstrcpy(ifname, ifname_size, dev);

+
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+ return fd;
+}
+#elif defined (_BSD) || defined (__FreeBSD_kernel__)
static int tap_open(char *ifname, int ifname_size)

 {
     int fd;
#1 lore raw

Any interest in this guys?

To my knowledge, Mattias' tun/tap kernel extension is the only way to get tun/tap working in OSX.

thanks,
-lev

Quoted 55 lines · Lev Lvovsky

The following patch provided by Mattias Nissler, and tweaked by myself to be applied to net.c (previously to vl.c) allows qemu to use the tun/tap devices provided at Mattias' site (http://tuntaposx.sourceforge.net).

Mattias is CC'd.

thanks,
-lev

----
bash-3.2$ svn diff net.c
Index: net.c
===================================================================

net.c (working copy) renamed from net.c (revision 6082) +29 -1
@@ -686,8 +686,36 @@
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d",  
 fd);
    return s;
 }
+#if defined(__APPLE__)
+static int tap_open(char *ifname, int ifname_size)
+{
+    int fd;
+    int i = 0;
+    char *dev;
+    char buf[20];
+    struct stat s;
 
-#if defined (_BSD) || defined (__FreeBSD_kernel__)
+    while (1) {
+        snprintf(buf, 20, "/dev/tap%d", i);
+        fd = open(buf, O_RDWR);
+        if (fd < 0) {
+            if (errno != EBUSY) {
+                fprintf(stderr, "warning: could not open %s: no  
 virtual network emulation\n", buf);
+                return -1;
+            }
+            i++;
+        } else
+            break;
+    }
+
+    fstat(fd, &s);
+    dev = devname(s.st_rdev, S_IFCHR);
+    pstrcpy(ifname, ifname_size, dev);
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+    return fd;
+}
+#elif defined (_BSD) || defined (__FreeBSD_kernel__)
 static int tap_open(char *ifname, int ifname_size)
{
    int fd;
#2 lore raw

If we do commit this, there should be a way to check and see of these kernel modules are installed.

Quoted 67 lines · Lev Lvovsky

Any interest in this guys?

To my knowledge, Mattias' tun/tap kernel extension is the only way to get tun/tap working in OSX.

thanks,
-lev

Quoted 55 lines · Lev Lvovsky

The following patch provided by Mattias Nissler, and tweaked by myself to be applied to net.c (previously to vl.c) allows qemu to use the tun/tap devices provided at Mattias' site (http://tuntaposx.sourceforge.net).

Mattias is CC'd.

thanks,
-lev

----
bash-3.2$ svn diff net.c
Index: net.c
===================================================================

net.c (working copy) renamed from net.c (revision 6082) +29 -1
@@ -686,8 +686,36 @@
   snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d",  
 fd);
   return s;
 }
+#if defined(__APPLE__)
+static int tap_open(char *ifname, int ifname_size)
+{
+    int fd;
+    int i = 0;
+    char *dev;
+    char buf[20];
+    struct stat s;
 
-#if defined (_BSD) || defined (__FreeBSD_kernel__)
+    while (1) {
+        snprintf(buf, 20, "/dev/tap%d", i);
+        fd = open(buf, O_RDWR);
+        if (fd < 0) {
+            if (errno != EBUSY) {
+                fprintf(stderr, "warning: could not open %s: no  
 virtual network emulation\n", buf);
+                return -1;
+            }
+            i++;
+        } else
+            break;
+    }
+
+    fstat(fd, &s);
+    dev = devname(s.st_rdev, S_IFCHR);
+    pstrcpy(ifname, ifname_size, dev);
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+    return fd;
+}
+#elif defined (_BSD) || defined (__FreeBSD_kernel__)
 static int tap_open(char *ifname, int ifname_size)
{
   int fd;
#3 lore raw
In reply to Lev Lvovsky
Quoted 1 line · Lev Lvovsky

The following patch provided by Mattias Nissler, and tweaked by myself to be applied to net.c (previously to vl.c) allows qemu to use the tun/tap devices provided at Mattias' site (http://tuntaposx.sourceforge.net).

Needs a valid Signed-off-by along with appropriate documentation pointing users to tuntaposx.

Regards,

Anthony Liguori

Quoted 54 lines

Mattias is CC'd.

thanks,
-lev

----
bash-3.2$ svn diff net.c
Index: net.c
===================================================================

net.c (working copy) renamed from net.c (revision 6082) +29 -1
@@ -686,8 +686,36 @@
     snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", 
 fd);
     return s;
 }
+#if defined(__APPLE__)
+static int tap_open(char *ifname, int ifname_size)
+{
+    int fd;
+    int i = 0;
+    char *dev;
+    char buf[20];
+    struct stat s;
 
-#if defined (_BSD) || defined (__FreeBSD_kernel__)
+    while (1) {
+        snprintf(buf, 20, "/dev/tap%d", i);
+        fd = open(buf, O_RDWR);
+        if (fd < 0) {
+            if (errno != EBUSY) {
+                fprintf(stderr, "warning: could not open %s: no 
 virtual network emulation\n", buf);
+                return -1;
+            }
+            i++;
+        } else
+            break;
+    }
+
+    fstat(fd, &s);
+    dev = devname(s.st_rdev, S_IFCHR);
+    pstrcpy(ifname, ifname_size, dev);
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+    return fd;
+}
+#elif defined (_BSD) || defined (__FreeBSD_kernel__)
 static int tap_open(char *ifname, int ifname_size)
 {
     int fd;