Linux TUN/TAP driver updates
A message that is not in the archive (sent off-list or not yet received): 1127063858.9233.116.camel@libretto
Quoted 4 lines · Felix Lechner
Apparently I have the same desire to use a persistent tap0 device with qemu like you did in April. I can configure tap0 with tunctl and standard debian networking, but passing that information to qemu is a riddle to me.
Attached is my patch relative the current CVS.
This patch adds the -tun-dev command line option
-tun-dev name
specifying the TUN/TAP device name to attach to.
In addition the patch fixes the existing -tun-fd option allowing it to be specified multiple times if desired.
Quoted 1 line
Your patch never made it into version 7.2 (and it does not apply without errors anymore) which makes me think there is another way to do it.
No, just not many doing this it seems.
Quoted 3 lines
Like everybody else, I also thought about a little program similar to Russell's tundev that opens the interface and passes the fd via command line option -tun-fd.
To qemu there is no real difference.
However, without patching the -tun-fd option is limited to a single interface due to a name collision on the internal qemu device name.
Quoted 1 line
What's the right way to do this?
The current configuration options have quite many shortcomings. There is discussion about rearranging the networking options making configuration more sane
- TUN/TAP devices
- TUN/TAP file descriptors
- MAC addresses
- user-net
- etc..
There is a very nice proposal by Fabrice on how he likes the command line to look like making configuration quite sane and flexible, what remains is for someone to implement this.
http://lists.gnu.org/archive/html/qemu-devel/2005-08/msg00196.html
Regards
Henrik
Index: qemu-doc.texi
===================================================================
RCS file: /cvsroot/qemu/qemu/qemu-doc.texi,v
retrieving revision 1.65
qemu-doc.texi +5 -0
@@ -227,6 +227,11 @@ it. Read @url{http://bellard.org/qemu/tetrinet.html} to have an example of its use. +@item -tun-dev ifname + +Set the host TUN/TAP device name. Useful if you use persistent TUN/TAP +interfaces or host firewalling based on the device name. + @item -user-net Use the user mode network stack. This is the default if no tun/tap network init script is found.
Index: vl.c
===================================================================
RCS file: /cvsroot/qemu/qemu/vl.c,v
retrieving revision 1.136
vl.c +25 -6
@@ -1663,14 +1663,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;
@@ -1692,20 +1694,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 );
@@ -1727,7 +1733,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 ; }
@@ -2852,6 +2858,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"
@@ -2941,6 +2948,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,
@@ -3004,6 +3012,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 },
@@ -3136,7 +3145,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;
@@ -3191,6 +3200,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 */
@@ -3326,6 +3336,15 @@ } tun_fds[nb_tun_fds++] = fd; } + } +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_cdrom: