Skip to contentSkip to conversation

net: Add persistent flag to -net tap option

PATCH In review Roy Vardi 4 participants 6 messages Dec 15, 2014 – Jan 6, 2015 Networking

lore
Share & tools
lore

Roy Vardi

author
#0 lore raw

PATCH 1/1 net: Add persistent flag to -net tap option

From: Roy Vardi <[email protected]>

Add 'persistent' boolean flag to -net tap option. When set to off - tap interface will be released on shutdown When set to on\not specified - tap interface will remain

Running with -net tap,persistent=off will force the tap interface down when qemu goes down, thus ensuring that there're no zombie tap interfaces left

This is achieved using another ioctl

Note: This commit includes the above support only for linux systems

---
 net/tap-linux.c  |   14 +++++++++++++-
 net/tap.c        |   10 ++++++----
 net/tap_int.h    |    2 +-
 qapi-schema.json |    5 ++++-
 qemu-options.hx  |    3 ++-
 5 files changed, 26 insertions(+), 8 deletions(-)
net/tap-linux.c +13 -1
index 812bf2d..2072166 100644
@@ -29,6 +29,7 @@
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <linux/if_tun.h>
 
 #include "sysemu/sysemu.h"
 #include "qemu-common.h"
@@ -37,7 +38,7 @@
 #define PATH_NET_TUN "/dev/net/tun"
 
 int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
-             int vnet_hdr_required, int mq_required)
+             int vnet_hdr_required, int mq_required, int persistent_required)
 {
     struct ifreq ifr;
     int fd, ret;
@@ -109,6 +110,17 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
         close(fd);
         return -1;
     }
+
+    if (!persistent_required) {
+        ret = ioctl(fd, TUNSETPERSIST, 0);
+            if (ret != 0) {
+                error_report("could not configure non-persistent %s (%s): %m",
+                             PATH_NET_TUN, ifr.ifr_name);
+                close(fd);
+                return -1;
+        }
+    }
+
     pstrcpy(ifname, ifname_size, ifr.ifr_name);
     fcntl(fd, F_SETFL, O_NONBLOCK);
     return fd;
net/tap.c +6 -4
index bde6b58..43267bb 100644
@@ -556,7 +556,8 @@ int net_init_bridge(const NetClientOptions *opts, const char *name,
 
 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
                         const char *setup_script, char *ifname,
-                        size_t ifname_sz, int mq_required)
+                        size_t ifname_sz, int mq_required,
+                        int persistent_reuired)
 {
     int fd, vnet_hdr_required;
 
@@ -569,7 +570,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
     }
 
     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
-                      mq_required));
+                      mq_required, persistent_reuired));
     if (fd < 0) {
         return -1;
     }
@@ -688,7 +689,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
                  NetClientState *peer)
 {
     const NetdevTapOptions *tap;
-    int fd, vnet_hdr = 0, i = 0, queues;
+    int fd, vnet_hdr = 0, i = 0, queues, persistent;
     /* for the no-fd, no-helper case */
     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
     const char *downscript = NULL;
@@ -699,6 +700,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
     tap = opts->tap;
     queues = tap->has_queues ? tap->queues : 1;
     vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
+    persistent = tap->has_persistent ? tap->persistent : 1;
 
     /* QEMU vlans does not support multiqueue tap, in this case peer is set.
      * For -netdev, peer is always NULL. */
@@ -816,7 +818,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
 
         for (i = 0; i < queues; i++) {
             fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
-                              ifname, sizeof ifname, queues > 1);
+                              ifname, sizeof ifname, queues > 1, persistent);
             if (fd == -1) {
                 return -1;
             }
net/tap_int.h +1 -1
index 79afdf2..0eb2168 100644
@@ -30,7 +30,7 @@
 #include "qapi-types.h"
 
 int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
-             int vnet_hdr_required, int mq_required);
+             int vnet_hdr_required, int mq_required, int persistent_required);
 
 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen);
 
qapi-schema.json +4 -1
index 563b4ad..e4ea73f 100644
@@ -2007,6 +2007,8 @@
 #
 # @queues: #optional number of queues to be created for multiqueue capable tap
 #
+# @persistent: #optional for opening tap in persistent mode
+#
 # Since 1.2
 ##
 { 'type': 'NetdevTapOptions',
@@ -2023,7 +2025,8 @@
     '*vhostfd':    'str',
     '*vhostfds':   'str',
     '*vhostforce': 'bool',
-    '*queues':     'uint32'} }
+    '*queues':     'uint32',
+    '*persistent': 'bool'} }
 
 ##
 # @NetdevSocketOptions
qemu-options.hx +2 -1
index 10b9568..d26215a 100644
@@ -1417,7 +1417,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "-net tap[,vlan=n][,name=str],ifname=name\n"
     "                connect the host TAP network interface to VLAN 'n'\n"
 #else
-    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"
+    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][persistent=on|off]\n"
     "                connect the host TAP network interface to VLAN 'n'\n"
     "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
     "                to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
@@ -1437,6 +1437,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
     "                use 'vhostfds=x:y:...:z to connect to multiple already opened vhost net devices\n"
     "                use 'queues=n' to specify the number of queues to be created for multiqueue TAP\n"
+    "                use persistent=off to release the TAP interface on shutdown (default=on)\n"
     "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
     "                connects a host TAP network interface to a host bridge device 'br'\n"
     "                (default=" DEFAULT_BRIDGE_INTERFACE ") using the program 'helper'\n"
Signature · 1 line
1.7.1
#1 lore raw
Quoted 6 lines · Roy Vardi

From: Roy Vardi <[email protected]>

Add 'persistent' boolean flag to -net tap option. When set to off - tap interface will be released on shutdown When set to on\not specified - tap interface will remain

Quoted 7 lines

Running with -net tap,persistent=off will force the tap interface down when qemu goes down, thus ensuring that there're no zombie tap interfaces left

This is achieved using another ioctl

Note: This commit includes the above support only for linux systems

I don't understand the point of this patch. The following doesn't persist the tun interface:

qemu-system-i386 -net tap,script=myscript.sh,downscript=no -net nic

You are changing the default to persist the interface, won't this cause problems for existing users who don't expect persistent interfaces?

net/tap-linux.c int tap_open(char *ifname, int ifname_size, int *vnet_hdr, · line 116

Quoted 8 lines
net/tap-linux.c +4 -0
@@ -109,6 +110,17 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
         close(fd);
         return -1;
     }
+
+    if (!persistent_required) {
+        ret = ioctl(fd, TUNSETPERSIST, 0);
+            if (ret != 0) {

Indentation is off here. QEMU uses 4-space indentation and this if statement should not be indented.

Quoted 15 lines
net/tap-linux.c +7 -0
+                error_report("could not configure non-persistent %s (%s): %m",
+                             PATH_NET_TUN, ifr.ifr_name);
+                close(fd);
+                return -1;
+        }
+    }
+
     pstrcpy(ifname, ifname_size, ifr.ifr_name);
     fcntl(fd, F_SETFL, O_NONBLOCK);
     return fd;
net/tap.c +0 -0
index bde6b58..43267bb 100644

net/tap.c int net_init_bridge(const NetClientOptions *opts, const char *name, · line 560

Quoted 6 lines
net/tap.c +2 -1
@@ -556,7 +556,8 @@ int net_init_bridge(const NetClientOptions *opts, const char *name,
 
 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
                         const char *setup_script, char *ifname,
-                        size_t ifname_sz, int mq_required)
+                        size_t ifname_sz, int mq_required,
+                        int persistent_reuired)

Typo s/reuired/required/

qemu-options.hx DEF("net", HAS_ARG, QEMU_OPTION_net, · line 1420

Quoted 10 lines
qemu-options.hx +1 -1
index 10b9568..d26215a 100644
@@ -1417,7 +1417,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "-net tap[,vlan=n][,name=str],ifname=name\n"
     "                connect the host TAP network interface to VLAN 'n'\n"
 #else
-    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"
+    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][persistent=on|off]\n"

Missing comma:
[,persistent=on|off]

qemu-options.hx DEF("net", HAS_ARG, QEMU_OPTION_net, · line 1440

Quoted 8 lines
qemu-options.hx +1 -0
     "                connect the host TAP network interface to VLAN 'n'\n"
     "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"
     "                to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"
@@ -1437,6 +1437,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
     "                use 'vhostfds=x:y:...:z to connect to multiple already opened vhost net devices\n"
     "                use 'queues=n' to specify the number of queues to be created for multiqueue TAP\n"
+    "                use persistent=off to release the TAP interface on shutdown (default=on)\n"

Please use the same formatting as for the other options:

use 'persistent=off' to release ...

#2 lore raw
Quoted 15 lines · Stefan Hajnoczi
Quoted 6 lines · Roy Vardi

From: Roy Vardi <[email protected]>

Add 'persistent' boolean flag to -net tap option. When set to off - tap interface will be released on shutdown When set to on\not specified - tap interface will remain

Quoted 7 lines

Running with -net tap,persistent=off will force the tap interface down when qemu goes down, thus ensuring that there're no zombie tap interfaces left

This is achieved using another ioctl

Note: This commit includes the above support only for linux systems

Quoted 7 lines

I don't understand the point of this patch. The following doesn't persist the tun interface:

qemu-system-i386 -net tap,script=myscript.sh,downscript=no -net nic

You are changing the default to persist the interface, won't this cause problems for existing users who don't expect persistent interfaces?

Yea, changing the default to persist interfaces is going to cause existing apps using this syntax to leak these tap devices on QEMU shutdown.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
author
#3 lore raw
In reply to Stefan Hajnoczi

Hi Stefan,

When using "-net tap" flag today, the tap interface is not released on shutdown. This is the default behavior and I'm not breaking it.

Running with "-net tap persistent=off " will cause the interface to be released on shutdown, and running with "-net tap persistent=on " (or without the persistent flag at all), the behavior remains the same (i.e - interface won't be released. This is the status in qemu today, I didn't change any legacy command-lines)

Example for a command line:

. /qemu-system -net tap,ifname=tap0,script=no,downscript=no,persistent=off

. /qemu-system -net tap,ifname=tap0,script=no,downscript=no,persistent=on

. /qemu-system -net tap,ifname=tap0,script=no,downscript=no

The last two lines are identical as far as releasing the tap is concerned.

This is relevant for users who want their tap interfaces to be released on qemu shutdown. The rest of the users won't be influenced by this change.

I'll fix the spelling and format bugs and will send a new patch soon.

Thanks for your review and comments,

Roy Vardi

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Friday, December 19, 2014 3:14 PM
To: Roy Vardi
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net tap option

Quoted 20 lines · Roy Vardi

From: Roy Vardi <[email protected]<mailto:[email protected]>>

Add 'persistent' boolean flag to -net tap option.

When set to off - tap interface will be released on shutdown

When set to on\not specified - tap interface will remain

Running with -net tap,persistent=off will force the tap interface

down when qemu goes down, thus ensuring that there're no zombie tap

interfaces left

Quoted 7 lines

This is achieved using another ioctl

Note: This commit includes the above support only for linux

systems

I don't understand the point of this patch. The following doesn't persist the tun interface:

qemu-system-i386 -net tap,script=myscript.sh,downscript=no -net nic

You are changing the default to persist the interface, won't this cause problems for existing users who don't expect persistent interfaces?

Quoted 5 lines
net/tap-linux.c +0 -0
@@ -109,6 +110,17 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
 
         close(fd);
 
         return -1;

net/tap-linux.c int tap_open(char *ifname, int ifname_size, int *vnet_hdr, · line 116

Quoted 10 lines
net/tap-linux.c +4 -0
 
     }
 
+
 
+    if (!persistent_required) {
 
+        ret = ioctl(fd, TUNSETPERSIST, 0);
 
+            if (ret != 0) {

Indentation is off here. QEMU uses 4-space indentation and this if statement should not be indented.

Quoted 33 lines
net/tap-linux.c +7 -0
+                error_report("could not configure non-persistent %s (%s): %m",
 
+                             PATH_NET_TUN, ifr.ifr_name);
 
+                close(fd);
 
+                return -1;
 
+        }
 
+    }
 
+
 
     pstrcpy(ifname, ifname_size, ifr.ifr_name);
 
     fcntl(fd, F_SETFL, O_NONBLOCK);
 
     return fd;
net/tap.c +0 -0

index bde6b58..43267bb 100644

--- a/net/tap.c

+++ b/net/tap.c

net/tap.c +0 -0
@@ -556,7 +556,8 @@ int net_init_bridge(const NetClientOptions *opts,
 
 const char *name,
 
 

net/tap.c int net_init_bridge(const NetClientOptions *opts,

Quoted 10 lines
net/tap.c +2 -1
 
 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
 
                         const char *setup_script, char *ifname,
 
-                        size_t ifname_sz, int mq_required)
 
+                        size_t ifname_sz, int mq_required,
 
+                        int persistent_reuired)

Typo s/reuired/required/

Quoted 9 lines
qemu-options.hx index 10b9568..d26215a renamed from qemu-options.hx +0 -0

100644

--- a/qemu-options.hx

+++ b/qemu-options.hx

qemu-options.hx index 10b9568..d26215a DEF("net", HAS_ARG, QEMU_OPTION_net,

Quoted 10 lines
qemu-options.hx index 10b9568..d26215a renamed from qemu-options.hx +1 -1
@@ -1417,7 +1417,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
 
     "-net tap[,vlan=n][,name=str],ifname=name\n"
 
     "                connect the host TAP network interface to VLAN 'n'\n"
 
 #else
 
-    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"
 
+    "-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][persistent=on|off]\n"

Missing comma:

[,persistent=on|off]

Quoted 7 lines

" connect the host TAP network interface to VLAN 'n'\n"

" use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n"

" to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n"

qemu-options.hx index 10b9568..d26215a DEF("net", HAS_ARG, QEMU_OPTION_net,

Quoted 8 lines
qemu-options.hx index 10b9568..d26215a renamed from qemu-options.hx +1 -0
@@ -1437,6 +1437,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
 
     "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
 
     "                use 'vhostfds=x:y:...:z to connect to multiple already opened vhost net devices\n"
 
     "                use 'queues=n' to specify the number of queues to be created for multiqueue TAP\n"
 
+    "                use persistent=off to release the TAP interface on shutdown (default=on)\n"

Please use the same formatting as for the other options:

use 'persistent=off' to release ...

#4 lore raw
Quoted 1 line · Roy Vardi

When using "-net tap" flag today, the tap interface is not released on shutdown. This is the default behavior and I'm not breaking it.

...

Quoted 1 line

. /qemu-system -net tap,ifname=tap0,script=no,downscript=no

That is not the behavior that I observe with Linux 3.17.4-302.fc21.x86_64 and qemu.git/master (ab0302ee764fd702465aef6d88612cdff4302809).

When I run your command-line, the tap0 interface is visible.

After QEMU shuts down the tap interface is no longer visible in the output of the "ip addr" command.

What do you observe on your machine that makes you say "the tap interface is not released on shutdown"?

Stefan

#5 lore raw
In reply to Roy Vardi
Quoted 19 lines · Roy Vardi

From: Roy Vardi <[email protected]>

Add 'persistent' boolean flag to -net tap option. When set to off - tap interface will be released on shutdown When set to on\not specified - tap interface will remain

Running with -net tap,persistent=off will force the tap interface down when qemu goes down, thus ensuring that there're no zombie tap interfaces left

This is achieved using another ioctl

Note: This commit includes the above support only for linux systems

---

+++ b/qapi-schema.json

qapi-schema.json line 2010

Quoted 4 lines
qapi-schema.json +1 -0
@@ -2007,6 +2007,8 @@
 #
 # @queues: #optional number of queues to be created for multiqueue capable tap
 #
+# @persistent: #optional for opening tap in persistent mode

Missing a '(since 2.3)' designator; also might be worth mentioning that the default is true if omitted.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org