qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [patch] Linux TUN/TAP driver updates
@ 2005-04-15 16:18 Henrik Nordstrom
  2005-04-15 20:07 ` [Qemu-devel] Regarding Linux TUN/TAP Hetz Ben Hamo
  2005-04-15 22:40 ` [Qemu-devel] [patch] Linux TUN/TAP driver updates Jean-Christian de Rivaz
  0 siblings, 2 replies; 7+ messages in thread
From: Henrik Nordstrom @ 2005-04-15 16:18 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1249 bytes --]

The attached patch updates the Linux TUN/TAP interface

   - Allow use of static/persistent TUN/TAP devices, eleminating the need 
to run anything as root when starting qemu.

   - A minor bugfix in filedescriptor based TUN/TAP devices to allow more 
than one filedescriptor (was colliding on the qemu internal device name).

   - Added the interface index as argument to the ifup script to simplify 
integration.


A small howto use persistent TUN/TAP devices:

1. Grab the tunctl tool from UML. 
<url:http://www.user-mode-linux.org/cvs/tools/tunctl/>
<url:http://www.fifi.org/cgi-bin/man2html/usr/share/man/man1/tunctl.1.gz>

this small tools allows you to create persistent TAP devices with a 
fixed name on the host. A persistent tap device stays until it is manually 
deleted with tunctl.

2. Configure the host side of the created TAP devices using your normal 
network setup tools, or the network configuration tools provided by your 
distribution vendor if preferred.

3. When starting qemu, specify the TAP device names with the new -tun-dev 
qemu command line option.

       -tun-dev name   use this already created tun device

-tun-dev can be specified multiple times if you want qemu to connect to 
more than one device.

Regards
Henrik

[-- Attachment #2: Type: TEXT/PLAIN, Size: 4116 bytes --]

Index: vl.c
===================================================================
RCS file: /cvsroot/qemu/qemu/vl.c,v
retrieving revision 1.126
diff -u -r1.126 vl.c
--- vl.c	7 Apr 2005 22:20:28 -0000	1.126
+++ vl.c	15 Apr 2005 15:45:44 -0000
@@ -1623,14 +1623,16 @@
     }
     memset(&ifr, 0, sizeof(ifr));
     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
-    pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
+    if (*ifname)
+	pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
+    else
+	pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%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;
     }
-    printf("Connected to host network interface: %s\n", ifr.ifr_name);
     pstrcpy(ifname, ifname_size, ifr.ifr_name);
     fcntl(fd, F_SETFL, O_NONBLOCK);
     return fd;
@@ -1652,20 +1654,24 @@
 static int net_tun_init(NetDriverState *nd)
 {
     int pid, status;
-    char *args[3];
-    char **parg;
 
     nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
     if (nd->fd < 0)
         return -1;
 
+    printf("Network %d connected to host network interface: %s\n", nd->index, nd->ifname);
     /* try to launch network init script */
     pid = fork();
     if (pid >= 0) {
         if (pid == 0) {
+	    char ifnum[4];
+	    char *args[4];
+	    char **parg;
+	    snprintf(ifnum, sizeof(ifnum), "%d", nd->index);
             parg = args;
             *parg++ = network_script;
             *parg++ = nd->ifname;
+            *parg++ = ifnum;
             *parg++ = NULL;
             execv(network_script, args);
             exit(1);
@@ -1687,7 +1693,7 @@
     nd->fd = fd;
     nd->send_packet = tun_send_packet;
     nd->add_read_packet = tun_add_read_packet;
-    pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
+    snprintf(nd->ifname, sizeof(nd->ifname), "tunfd%d", fd);
     return 0;
 }
 
@@ -2758,6 +2764,7 @@
            "-macaddr addr   set the mac address of the first interface\n"
            "-n script       set tap/tun network init script [default=%s]\n"
            "-tun-fd fd      use this fd as already opened tap/tun interface\n"
+           "-tun-dev name   use this already created tun device\n"
 #ifdef CONFIG_SLIRP
            "-user-net       use user mode network stack [default if no tap/tun script]\n"
            "-tftp prefix    allow tftp access to files starting with prefix [-user-net]\n"
@@ -2846,6 +2853,7 @@
     QEMU_OPTION_macaddr,
     QEMU_OPTION_n,
     QEMU_OPTION_tun_fd,
+    QEMU_OPTION_tun_dev,
     QEMU_OPTION_user_net,
     QEMU_OPTION_tftp,
     QEMU_OPTION_smb,
@@ -2907,6 +2915,7 @@
     { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
     { "n", HAS_ARG, QEMU_OPTION_n },
     { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
+    { "tun-dev", HAS_ARG, QEMU_OPTION_tun_dev },
 #ifdef CONFIG_SLIRP
     { "user-net", 0, QEMU_OPTION_user_net },
     { "tftp", HAS_ARG, QEMU_OPTION_tftp },
@@ -3018,7 +3027,7 @@
     int cyls, heads, secs, translation;
     int start_emulation = 1;
     uint8_t macaddr[6];
-    int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
+    int net_if_type, nb_tun_fds, tun_fds[MAX_NICS], nb_tun_devices;
     int optind;
     const char *r, *optarg;
     CharDriverState *monitor_hd;
@@ -3066,6 +3075,7 @@
     parallel_device_index = 0;
     
     nb_tun_fds = 0;
+    nb_tun_devices = 0;
     net_if_type = -1;
     nb_nics = 1;
     /* default mac address of the first network interface */
@@ -3184,6 +3194,15 @@
                     }
                 }
 		break;
+	    case QEMU_OPTION_tun_dev:
+                {
+                    net_if_type = NET_IF_TUN;
+                    if (nb_tun_devices < MAX_NICS) {
+			pstrcpy(nd_table[nb_tun_devices].ifname, sizeof(nd_table[nb_tun_devices].ifname), optarg);
+			nb_tun_devices++;
+		    }
+                }
+		break;
             case QEMU_OPTION_hdc:
                 hd_filename[2] = optarg;
                 has_cdrom = 0;

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

end of thread, other threads:[~2005-04-18 15:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-15 16:18 [Qemu-devel] [patch] Linux TUN/TAP driver updates Henrik Nordstrom
2005-04-15 20:07 ` [Qemu-devel] Regarding Linux TUN/TAP Hetz Ben Hamo
2005-04-15 20:18   ` Laurent Amon
2005-04-15 22:33     ` Paul Brook
2005-04-18 15:46   ` Henrik Nordstrom
2005-04-15 22:40 ` [Qemu-devel] [patch] Linux TUN/TAP driver updates Jean-Christian de Rivaz
2005-04-16  7:00   ` emuls

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).