Skip to contentSkip to conversation

net: Add persistent flag to -net tap option

PATCH In review Roy Vardi 3 participants 8 messages Dec 21, 2014 – Jan 22, 2015 1 message not in the archive 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..7a98390 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..055863a 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_required)
 {
     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_required));
     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..99e6482 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 (default: on) (Since: 2.3)
+#
 # 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..d8c033d 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 5 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

I'm interested of the user cases in the case. Usually, persistent flag was used to let privileged application to create/configure the device and then it could be used by non-privileged users. If qemu has already had privilege, why need set persistent in this case?

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

But your patch will break non-linux builds, no?

Quoted 15 lines
---
 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 +0 -0
index 812bf2d..7a98390 100644

net/tap-linux.c line 32

Quoted 4 lines
net/tap-linux.c +1 -0
@@ -29,6 +29,7 @@
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <linux/if_tun.h>

To reduce headers dependency, we put tun flags in net/tap-linux.h.

Quoted 129 lines
net/tap-linux.c +12 -1
 #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..055863a 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_required)
 {
     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_required));
     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..99e6482 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 (default: on) (Since: 2.3)
+#
 # 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 +1 -1
index 10b9568..d8c033d 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"

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

Quoted 4 lines
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"

As Stefan mentioned, we don't set persistent by default in the past. So please don't change this behaviour.

Quoted 3 lines
qemu-options.hx +0 -0
     "-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"
author
#2 lore raw
Quoted 14 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Monday, December 22, 2014 8:33 AM
To: Roy Vardi; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Noam Camus; [email protected]
Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net tap option

Quoted 2 lines · Roy Vardi

From: Roy Vardi <[email protected]>

Quoted 8 lines
Quoted 3 lines

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

I'm interested of the user cases in the case. Usually, persistent flag was used to let privileged application to create/configure the device and then it could be used by non-privileged users. If qemu has already had privilege, why need set persistent in this case?

We're running qemu as users, non-privilege...
Our work flow includes:
1. Running an internal tool for opening a persistent tap interface
2. Running qemu with the tap interface we got from above
Our environment includes a lot of such qemu runs, and we want to avoid "zombie" tap interfaces, and we achieve it with this new flag I've added.
It's also worth mentioning that we're able to configure the tap interface in qemu through the ioctl as users (non-privileged).
If the persistent flag is not given (or if it's given as on), I'm not actively doing anything, the same code that you're familiar with will run. My change only affects the case where "persistent=off" is given

Quoted 10 lines
Quoted 8 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

But your patch will break non-linux builds, no?

I'm using qemu only on linux, so I can't really tell.
I would appreciate it if you could perhaps tell me how can I make sure I didn't break anything

Quoted 16 lines
Quoted 16 lines
---
 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 index 812bf2d..7a98390 renamed from net/tap-linux.c +0 -0

100644

net/tap-linux.c +0 -0
@@ -29,6 +29,7 @@
 
Quoted 5 lines

net/tap-linux.c line 32

Quoted 3 lines
net/tap-linux.c +1 -0
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <linux/if_tun.h>

To reduce headers dependency, we put tun flags in net/tap-linux.h.

Sure, I'll fix and will send another patch

Quoted 148 lines

net/tap-linux.c int tap_open(char *ifname, int ifname_size, int

Quoted 14 lines
net/tap-linux.c +2 -1
 #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,

net/tap.c static int net_tap_init(const NetdevTapOptions *tap,

Quoted 33 lines
net/tap-linux.c +11 -0
         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 +2 -1
index bde6b58..055863a 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_required)
 {
     int fd, vnet_hdr_required;
@@ -569,7 +570,7 @@ static int net_tap_init(const NetdevTapOptions *tap,

int *vnet_hdr,

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

Quoted 9 lines
net/tap.c +1 -1
     }
 
     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
-                      mq_required));
+                      mq_required, persistent_required));
     if (fd < 0) {
         return -1;
     }
@@ -688,7 +689,7 @@ int net_init_tap(const NetClientOptions *opts, const

char *name,

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

Quoted 8 lines
net/tap.c +1 -1
                  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,

Quoted 59 lines
net/tap.c +1 -0
     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,
net/tap.c +2 -1
-                              ifname, sizeof ifname, queues > 1);
+                              ifname, sizeof ifname, queues > 1,
+ persistent);
             if (fd == -1) {
                 return -1;
             }
net/tap_int.h index 79afdf2..0eb2168 renamed from net/tap_int.h +0 -0

100644

net/tap_int.h +2 -1
@@ -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 index renamed from qapi-schema.json +0 -0

563b4ad..99e6482 100644

qapi-schema.json +0 -0
@@ -2007,6 +2007,8 @@
 #
 # @queues: #optional number of queues to be created for multiqueue

capable tap #

qapi-schema.json +4 -1
+# @persistent: #optional for opening tap in persistent mode (default:
+on) (Since: 2.3) #
 # 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 index 10b9568..d8c033d renamed from qemu-options.hx +0 -0

100644

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

Quoted 4 lines
qemu-options.hx +0 -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][,down script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off ][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"

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

Quoted 1 line
qemu-options.hx +1 -0
+    "-net

tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,down script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off ][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][,persistent=o n|off]\n"

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

Quoted 2 lines
qemu-options.hx +0 -0
     "                connect the host TAP network interface to VLAN 'n'\n"
     "                use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT

")\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                to configure it and 'dfile' (default="

DEFAULT_NETWORK_DOWN_SCRIPT ")\n"

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

Quoted 2 lines
qemu-options.hx +0 -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"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                use 'vhostfds=x:y:...:z to connect to multiple already opened vhost
Quoted 8 lines

net devices\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                use 'queues=n' to specify the number of queues to be created for

multiqueue TAP\n"

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

Quoted 1 line
qemu-options.hx +1 -0
+    "                use 'persistent=off' to release the TAP interface on shutdown

(default=on)\n"

As Stefan mentioned, we don't set persistent by default in the past. So please don't change this behaviour.

I didn't change any legacy behavior.
If the persistent flag is not given, then the code that will be executed is as it is today. I'm only actively changing (configuring) the interface if "persistent=off" is given

Quoted 5 lines

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

Quoted 2 lines
qemu-options.hx +0 -0
     "-net bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"
     "                connects a host TAP network interface to a host bridge device

'br'\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                (default=" DEFAULT_BRIDGE_INTERFACE ") using the program

'helper'\n"

#4 lore raw
In reply to a message not in the archive
Quoted 27 lines · Roy Vardi
Quoted 19 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Monday, December 22, 2014 8:33 AM
To: Roy Vardi; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Noam Camus; [email protected]
Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net
tap option

Quoted 5 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

I'm interested of the user cases in the case. Usually, persistent
flag was used to
let privileged application to create/configure the device and then
it could be
used by non-privileged users. If qemu has already had privilege,
why need set
persistent in this case?

Quoted 3 lines

We're running qemu as users, non-privilege... Our work flow includes:
1. Running an internal tool for opening a persistent tap interface 2. Running qemu with the tap interface we got from above Our environment includes a lot of such qemu runs, and we want to avoid "zombie" tap interfaces, and we achieve it with this new flag I've added.

I get the case, thanks for the explaining. But qemu has already had method to solve this. Try downscript for tap, this external script can do cleanup before closing tap fd.

E.g. in your case, you may need to run tunctl -d.

Quoted 3 lines

It's also worth mentioning that we're able to configure the tap interface in qemu through the ioctl as users (non-privileged).
If the persistent flag is not given (or if it's given as on), I'm not actively doing anything, the same code that you're familiar with will run. My change only affects the case where "persistent=off" is given

Right, but it was not elegant to do this during tap_open. A more sutiable place is tap_cleanup(). Since qemu has already had downscript which can do even more complex things, there's no need for qemu to handle this specific case by itself.

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines

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

interface

Quoted 1 line

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

zombie tap

Quoted 4 lines

interfaces left

This is achieved using another ioctl

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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

But your patch will break non-linux builds, no?

I'm using qemu only on linux, so I can't really tell.
I would appreciate it if you could perhaps tell me how can I make sure I didn't break anything

E.g only linux version of tap_open() can accept persist_required. This will break the build of other platforms for sure.

Quoted 208 lines
Quoted 19 lines

net/tap-linux.c index

Quoted 13 lines
---
 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 index renamed from net/tap-linux.c +0 -0

812bf2d..7a98390

Quoted 5 lines

100644

net/tap-linux.c +0 -0
@@ -29,6 +29,7 @@
 
Quoted 5 lines

net/tap-linux.c line 32

Quoted 3 lines
net/tap-linux.c +1 -0
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <linux/if_tun.h>

To reduce headers dependency, we put tun flags in net/tap-linux.h.

Sure, I'll fix and will send another patch

Quoted 181 lines

net/tap-linux.c int tap_open(char *ifname, int ifname_size,

Quoted 14 lines
net/tap-linux.c +2 -1
 #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,

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

Quoted 8 lines
net/tap-linux.c +5 -0
         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",

net/tap.c int net_init_bridge(const NetClientOptions

Quoted 14 lines
net/tap-linux.c +6 -0
+                        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..055863a 100644
@@ -556,7 +556,8 @@ int net_init_bridge(const NetClientOptions 

*opts,

net/tap.c int net_init_bridge(const NetClientOptions · line 558

Quoted 3 lines
net/tap.c +0 -0
 const char *name,
 
 static int net_tap_init(const NetdevTapOptions *tap, int 

*vnet_hdr,

net/tap.c static int net_tap_init(const

Quoted 8 lines
net/tap.c +2 -1
                         const char *setup_script, char *ifname,
-                        size_t ifname_sz, int mq_required)
+                        size_t ifname_sz, int mq_required,
+                        int persistent_required)
 {
     int fd, vnet_hdr_required;
@@ -569,7 +570,7 @@ static int net_tap_init(const 

NetdevTapOptions *tap,
int *vnet_hdr,

net/tap.c static int net_tap_init(const · line 572

Quoted 3 lines
net/tap.c +0 -0
     }
 
     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, 

vnet_hdr_required,

net/tap.c int net_init_tap(const NetClientOptions

Quoted 6 lines
net/tap.c +1 -1
-                      mq_required));
+                      mq_required, persistent_required));
     if (fd < 0) {
         return -1;
     }
@@ -688,7 +689,7 @@ int net_init_tap(const NetClientOptions 

*opts, const
char *name,

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name,

Quoted 6 lines
net/tap.c +1 -1
                  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 */

net/tap.c int net_init_tap(const NetClientOptions

Quoted 2 lines
net/tap.c +0 -0
     const char *downscript = NULL;
@@ -699,6 +700,7 @@ int net_init_tap(const NetClientOptions 

*opts, const
char *name,

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name,

Quoted 6 lines
net/tap.c +1 -0
     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.

net/tap.c int net_init_tap(const NetClientOptions · line 706

Quoted 1 line
net/tap.c +0 -0
      * For -netdev, peer is always NULL. */ @@ -816,7 +818,7 @@ 

int

Quoted 4 lines

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,

Quoted 25 lines
net/tap.c +2 -1
-                              ifname, sizeof ifname, queues > 1);
+                              ifname, sizeof ifname, queues > 1,
+ persistent);
             if (fd == -1) {
                 return -1;
             }
net/tap_int.h index 79afdf2..0eb2168 renamed from net/tap_int.h +0 -0

100644

net/tap_int.h +2 -1
@@ -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 index renamed from qapi-schema.json +0 -0

563b4ad..99e6482 100644

qapi-schema.json line 2008

Quoted 2 lines
qapi-schema.json +0 -0
@@ -2007,6 +2007,8 @@
 #
 # @queues: #optional number of queues to be created for 

multiqueue

Quoted 2 lines

capable tap #
+# @persistent: #optional for opening tap in persistent mode

(default:

qemu-options.hx index

Quoted 15 lines
qapi-schema.json +3 -1
+on) (Since: 2.3) #
 # 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 index renamed from qemu-options.hx +0 -0

10b9568..d8c033d

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

Quoted 6 lines

100644

qemu-options.hx +0 -0
@@ -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"

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

Quoted 2 lines
qemu-options.hx +0 -1
 #else
-    "-net

tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,down

script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off
][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"

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

Quoted 1 line
qemu-options.hx +1 -0
+    "-net

tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,down

script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off

][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][,persistent=o
n|off]\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                connect the host TAP network interface to 

VLAN 'n'\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                use network scripts 'file' (default=" 

DEFAULT_NETWORK_SCRIPT
")\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                to configure it and 'dfile' (default="

DEFAULT_NETWORK_DOWN_SCRIPT ")\n"

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

Quoted 2 lines
qemu-options.hx +0 -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"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                use 'vhostfds=x:y:...:z to connect to 

multiple already opened vhost
net devices\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                use 'queues=n' to specify the number of 

queues to be created for
multiqueue TAP\n"

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

Quoted 1 line
qemu-options.hx +1 -0
+    "                use 'persistent=off' to release the TAP 

interface on shutdown
(default=on)\n"

Quoted 7 lines
Quoted 3 lines

As Stefan mentioned, we don't set persistent by default in the
past. So please
don't change this behaviour.

I didn't change any legacy behavior.
If the persistent flag is not given, then the code that will be executed is as it is today. I'm only actively changing (configuring) the interface if "persistent=off" is given

Yes, I see.

Thanks

Quoted 12 lines
Quoted 10 lines

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

Quoted 3 lines
qemu-options.hx +0 -0
     "-net 

bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                connects a host TAP network interface to a 

host bridge device
'br'\n"

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

Quoted 1 line
qemu-options.hx +0 -0
     "                (default=" DEFAULT_BRIDGE_INTERFACE ") 

using the program
'helper'\n"

author
#5 lore raw
Quoted 90 lines

-----Original Message-----

From: Jason Wang [mailto:[email protected]]

Sent: Tuesday, December 23, 2014 11:13 AM

To: Roy Vardi

[email protected]; [email protected]; [email protected]

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net tap option

Quoted 52 lines · On Tue, Dec 23, 2014 at 4:44 PM, Roy Vardi…
Quoted 38 lines

-----Original Message-----

From: Jason Wang [mailto:[email protected]]

Sent: Monday, December 22, 2014 8:33 AM

To: Roy Vardi; [email protected]<mailto:[email protected]>

Noam Camus; [email protected]<mailto:[email protected]>

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net

tap option

Quoted 9 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

Quoted 7 lines

I'm interested of the user cases in the case. Usually, persistent

flag was used to let privileged application to create/configure the

device and then it could be used by non-privileged users. If qemu

has already had privilege, why need set persistent in this case?

We're running qemu as users, non-privilege...

Our work flow includes:

Quoted 7 lines

1. Running an internal tool for opening a persistent tap interface 2.

Running qemu with the tap interface we got from above Our environment

includes a lot of such qemu runs, and we want to avoid "zombie" tap

interfaces, and we achieve it with this new flag I've added.

I get the case, thanks for the explaining. But qemu has already had method to

Quoted 7 lines

solve this. Try downscript for tap, this external script can do cleanup before

closing tap fd.

E.g. in your case, you may need to run tunctl -d.

Thanks for the reference.

I've checked the downscript option, but found it unsuitable for us: Qemu runs the downscript before closing the fd, so a script which removes the interface will fail due to busy device.

I've changed the order in the qemu code so that the script is called after the descriptor is closed and it works for us.

What do you think about the following patch:

--- a/net/tap.c

+++ b/net/tap.c

@@ -296,12 +296,11 @@ static void tap_cleanup(NetClientState *nc)

     qemu_purge_queued_packets(nc);

- if (s->down_script[0])

- launch_script(s->down_script, s->down_script_arg, s->fd);

-

     tap_read_poll(s, false);

     tap_write_poll(s, false);

     close(s->fd);

+ if (s->down_script[0])

+ launch_script(s->down_script, s->down_script_arg, s->fd);

     s->fd = -1;

}

?

Thanks,

Roy Vardi

Quoted 491 lines
Quoted 11 lines

It's also worth mentioning that we're able to configure the tap

interface in qemu through the ioctl as users (non-privileged).

If the persistent flag is not given (or if it's given as on), I'm not

actively doing anything, the same code that you're familiar with will

run. My change only affects the case where "persistent=off" is given

Right, but it was not elegant to do this during tap_open. A more sutiable place is

tap_cleanup(). Since qemu has already had downscript which can do even more

complex things, there's no need for qemu to handle this specific case by itself.

Quoted 30 lines
Quoted 20 lines
Quoted 5 lines

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

interface

Quoted 1 line

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

zombie tap

Quoted 7 lines

interfaces left

This is achieved using another ioctl

Quoted 7 lines
Quoted 3 lines

Note: This commit includes the above support only for linux

systems

But your patch will break non-linux builds, no?

Quoted 5 lines

I'm using qemu only on linux, so I can't really tell.

I would appreciate it if you could perhaps tell me how can I make sure

I didn't break anything

E.g only linux version of tap_open() can accept persist_required. This will break

the build of other platforms for sure.

Quoted 390 lines
Quoted 36 lines
Quoted 12 lines
 net/tap-linux.c  |   14 +++++++++++++-
 net/tap.c        |   10 ++++++----
Quoted 7 lines
 net/tap_int.h    |    2 +-
 qapi-schema.json |    5 ++++-
 qemu-options.hx  |    3 ++-
 5 files changed, 26 insertions(+), 8 deletions(-)  >  > diff

--git a/net/tap-linux.c b/net/tap-linux.c index

812bf2d..7a98390

Quoted 11 lines

100644

--- a/net/tap-linux.c

+++ b/net/tap-linux.c

net/tap-linux.c +0 -0
@@ -29,6 +29,7 @@
 
 
 
 #include <net/if.h>
Quoted 7 lines

net/tap-linux.c

Quoted 3 lines
net/tap-linux.c +1 -0
 #include <sys/ioctl.h>
 
+#include <linux/if_tun.h>

To reduce headers dependency, we put tun flags in net/tap-linux.h.

Sure, I'll fix and will send another patch

Quoted 328 lines

Review int tap_open(char *ifname, int ifname_size,

Quoted 27 lines

#include "sysemu/sysemu.h"

#include "qemu-common.h"

(quoted diff) +2 -1
@@ -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,

Quoted 5 lines
(quoted diff) +0 -0
         close(fd);
 
         return -1;
 
     }

Review int tap_open(char *ifname, int ifname_size,

Quoted 10 lines
(quoted diff) +5 -0
 
+
 
+    if (!persistent_required) {
 
+        ret = ioctl(fd, TUNSETPERSIST, 0);
 
+        if (ret != 0) {
 
+            error_report("could not configure non-persistent %s

(%s): %m",

net/tap.c > index bde6b58..055863a

Quoted 19 lines
net/tap-linux.c +6 -0
+                        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 > index bde6b58..055863a renamed from net/tap.c +0 -0

100644 > --- a/net/tap.c > +++ b/net/tap.c > @@ -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,

net/tap.c > index bde6b58..055863a static int net_tap_init(const NetdevTapOptions

Quoted 15 lines

const char *setup_script, char *ifname,

net/tap.c > index bde6b58..055863a renamed from net/tap.c +2 -1
-                        size_t ifname_sz, int mq_required)
 
+                        size_t ifname_sz, int mq_required,
 
+                        int persistent_required)
 
 {
 
     int fd, vnet_hdr_required;
@@ -569,7 +570,7 @@ static int net_tap_init(const NetdevTapOptions

*tap, int *vnet_hdr,

net/tap.c > index bde6b58..055863a static int net_tap_init(const NetdevTapOptions · line 574

Quoted 5 lines
net/tap.c > index bde6b58..055863a renamed from net/tap.c +0 -0
     }
 
 
 
     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr,

vnet_hdr_required,

net/tap.c > index bde6b58..055863a int net_init_tap(const NetClientOptions *opts,

Quoted 11 lines
net/tap.c > index bde6b58..055863a renamed from net/tap.c +1 -1
-                      mq_required));
 
+                      mq_required, persistent_required));
     if (fd < 0) {

         return -1;

     }
net/tap.c > index bde6b58..055863a renamed from net/tap.c +0 -0
@@ -688,7 +689,7 @@ int net_init_tap(const NetClientOptions *opts,

const char *name,

Quoted 11 lines
net/tap.c > index bde6b58..055863a renamed from net/tap.c +1 -1
                  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 */

net/tap.c > index bde6b58..055863a int net_init_tap(const NetClientOptions *opts,

Quoted 3 lines
     const char *downscript = NULL;
net/tap.c > index bde6b58..055863a renamed from net/tap.c +0 -0
@@ -699,6 +700,7 @@ int net_init_tap(const NetClientOptions *opts,

const char *name,

Quoted 11 lines
net/tap.c > index bde6b58..055863a renamed from net/tap.c +1 -0
     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.

Quoted 1 line

* For -netdev, peer is always NULL. */ @@ -816,7 +818,7 @@

int

Quoted 5 lines

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,

Quoted 14 lines
net/tap.c > index bde6b58..055863a renamed from net/tap.c +2 -1
-                              ifname, sizeof ifname, queues > 1);
 
+                              ifname, sizeof ifname, queues > 1,
 
+ persistent);
 
             if (fd == -1) {
 
                 return -1;
 
             }
net/tap_int.h index 79afdf2..0eb2168 renamed from net/tap_int.h +0 -0
Quoted 1 line

100644 > --- a/net/tap_int.h > +++ b/net/tap_int.h > @@ -30,7

+30,7 @@ > #include "qapi-types.h"

qapi-schema.json index >

Quoted 15 lines

int tap_open(char *ifname, int ifname_size, int *vnet_hdr,

net/tap_int.h index 79afdf2..0eb2168 renamed from net/tap_int.h +2 -1
-             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 index > renamed from qapi-schema.json +0 -0

563b4ad..99e6482 100644 > --- a/qapi-schema.json > +++

b/qapi-schema.json > @@ -2007,6 +2007,8 @@ > # > # @queues:

#optional number of queues to be created for multiqueue > capable

tap # > +# @persistent: #optional for opening tap in persistent

mode

(default:

qemu-options.hx index

Quoted 29 lines
qapi-schema.json index > renamed from qapi-schema.json +3 -1
+on) (Since: 2.3) #
 
 # 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 index renamed from qemu-options.hx +0 -0

10b9568..d8c033d > 100644 > --- a/qemu-options.hx > +++

b/qemu-options.hx > @@ -1417,7 +1417,7 @@ DEF("net", HAS_ARG,

QEMU_OPTION_net,

Quoted 3 lines

"-net tap[,vlan=n][,name=str],ifname=name\n"

" connect the host TAP network interface to

VLAN 'n'\n"

Quoted 3 lines

#else

- "-net

tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=

file][,down

script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhos

t=on|off

][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]\n"

Quoted 1 line

+ "-net

tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=

file][,down

script=dfile][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhos

t=on|off

][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n][,pe

rsistent=o

n|off]\n"

Quoted 1 line

" connect the host TAP network interface to

VLAN 'n'\n"

Quoted 1 line

" use network scripts 'file' (default="

DEFAULT_NETWORK_SCRIPT

")\n"

Quoted 1 line

" to configure it and 'dfile' (default="

DEFAULT_NETWORK_DOWN_SCRIPT ")\n"

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

Quoted 3 lines
qemu-options.hx index renamed from qemu-options.hx +0 -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"

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

Quoted 1 line
qemu-options.hx index renamed from qemu-options.hx +0 -0
     "                use 'vhostfds=x:y:...:z to connect to

multiple already opened vhost

net devices\n"

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

Quoted 1 line
qemu-options.hx index renamed from qemu-options.hx +0 -0
     "                use 'queues=n' to specify the number of

queues to be created for

multiqueue TAP\n"

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

Quoted 1 line
qemu-options.hx index renamed from qemu-options.hx +1 -0
+    "                use 'persistent=off' to release the TAP

interface on shutdown

Quoted 7 lines

(default=on)\n"

As Stefan mentioned, we don't set persistent by default in the past.

So please don't change this behaviour.

I didn't change any legacy behavior.

Quoted 7 lines

If the persistent flag is not given, then the code that will be

executed is as it is today.

I'm only actively changing (configuring) the interface if

"persistent=off" is given

Yes, I see.

Thanks

Quoted 23 lines
Quoted 19 lines

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

Quoted 5 lines
qemu-options.hx index renamed from qemu-options.hx +0 -0
     "-net

bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]\n"

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

Quoted 1 line
qemu-options.hx index renamed from qemu-options.hx +0 -0
     "                connects a host TAP network interface to a

host bridge device

'br'\n"

Quoted 1 line

" (default=" DEFAULT_BRIDGE_INTERFACE ")

using the program

'helper'\n"

#6 lore raw
Quoted 107 lines · Roy Vardi
Quoted 11 lines

-----Original Message-----

From: Jason Wang [mailto:[email protected]]

Sent: Tuesday, December 23, 2014 11:13 AM

Quoted 7 lines

To: Roy Vardi

[email protected]; [email protected]; [email protected]

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net

tap option

Quoted 7 lines

On Tue, Dec 23, 2014 at 4:44 PM, Roy Vardi <[email protected]

Quoted 6 lines · <mailto:[email protected]>> wrote:
Quoted 5 lines
Quoted 5 lines

-----Original Message-----

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

From: Jason Wang [mailto:[email protected]]

Sent: Monday, December 22, 2014 8:33 AM

To: Roy Vardi; [email protected] <mailto:[email protected]>

[email protected] <mailto:[email protected]>; [email protected] <mailto:[email protected]>;

Quoted 42 lines
Quoted 41 lines
Quoted 24 lines

Noam Camus; [email protected] <mailto:[email protected]>

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net

tap option

Quoted 9 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

Quoted 7 lines

I'm interested of the user cases in the case. Usually, persistent

flag was used to let privileged application to create/configure the

device and then it could be used by non-privileged users. If qemu

has already had privilege, why need set persistent in this case?

We're running qemu as users, non-privilege...

Our work flow includes:

1. Running an internal tool for opening a persistent tap interface 2.

Running qemu with the tap interface we got from above Our environment

Quoted 7 lines
Quoted 3 lines

includes a lot of such qemu runs, and we want to avoid "zombie" tap

interfaces, and we achieve it with this new flag I've added.

I get the case, thanks for the explaining. But qemu has already had

method to

Quoted 1 line

solve this. Try downscript for tap, this external script can do

cleanup before

Quoted 5 lines

closing tap fd.

E.g. in your case, you may need to run tunctl -d.

Quoted 6 lines

Thanks for the reference.

I've checked the downscript option, but found it unsuitable for us:
Qemu runs the downscript before closing the fd, so a script which removes the interface will fail due to busy device.

Right, tunctl needs another TUNSETIFF ioctl(). You may want to delete it through ip link del link dev $1 in your qemu-ifdown.

Quoted 10 lines

I've changed the order in the qemu code so that the script is called after the descriptor is closed and it works for us.

What do you think about the following patch:

This change behaviour which may break existing down scripts.

Thanks

Quoted 39 lines

--- a/net/tap.c

+++ b/net/tap.c

(quoted diff) +2 -3
@@ -296,12 +296,11 @@ static void tap_cleanup(NetClientState *nc)
 
     qemu_purge_queued_packets(nc);
 
-    if (s->down_script[0])
 
-        launch_script(s->down_script, s->down_script_arg, s->fd);
 
-
 
     tap_read_poll(s, false);
 
     tap_write_poll(s, false);
 
     close(s->fd);
 
+    if (s->down_script[0])
 
+        launch_script(s->down_script, s->down_script_arg, s->fd);
 
     s->fd = -1;

}

?

Thanks,

Roy Vardi

author
#7 lore raw
Quoted 118 lines · Roy Vardi
Quoted 37 lines
Quoted 11 lines

-----Original Message-----

From: Jason Wang [mailto:[email protected]]

Sent: Tuesday, December 23, 2014 11:13 AM

Quoted 7 lines

To: Roy Vardi

[email protected]; [email protected]; [email protected]

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to -net

tap option

Quoted 7 lines

On Tue, Dec 23, 2014 at 4:44 PM, Roy Vardi <[email protected]

Quoted 7 lines · <mailto:[email protected]>> wrote:
Quoted 7 lines
Quoted 7 lines

-----Original Message-----

From: Jason Wang [mailto:[email protected]]

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Sent: Monday, December 22, 2014 8:33 AM

To: Roy Vardi; [email protected]
<mailto:[email protected]>

[email protected] <mailto:[email protected]>;

[email protected]

Quoted 72 lines

<mailto:[email protected]>;

Quoted 47 lines
Quoted 46 lines
Quoted 28 lines

Noam Camus; [email protected] <mailto:[email protected]>

Subject: Re: [Qemu-devel] [PATCH] net: Add persistent flag to
-net

tap option

Quoted 9 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

I'm interested of the user cases in the case. Usually,
persistent

Quoted 7 lines

flag was used to let privileged application to create/configure the

device and then it could be used by non-privileged users. If qemu

has already had privilege, why need set persistent in this case?

We're running qemu as users, non-privilege...

Our work flow includes:

1. Running an internal tool for opening a persistent tap interface 2.

Running qemu with the tap interface we got from above Our environment

Quoted 8 lines
Quoted 4 lines

includes a lot of such qemu runs, and we want to avoid "zombie" tap

interfaces, and we achieve it with this new flag I've added.

I get the case, thanks for the explaining. But qemu has already had

method to

Quoted 1 line

solve this. Try downscript for tap, this external script can do

cleanup before

Quoted 5 lines

closing tap fd.

E.g. in your case, you may need to run tunctl -d.

Thanks for the reference.

Quoted 7 lines
Quoted 4 lines

I've checked the downscript option, but found it unsuitable for us:
Qemu runs the downscript before closing the fd, so a script which removes the interface will fail due to busy device.

Right, tunctl needs another TUNSETIFF ioctl(). You may want to delete it through ip link del link dev $1 in your qemu-ifdown.

Yes, this works, thanks!

Quoted 53 lines
Quoted 10 lines

I've changed the order in the qemu code so that the script is called after the descriptor is closed and it works for us.

What do you think about the following patch:

This change behaviour which may break existing down scripts.

Thanks

Quoted 39 lines

--- a/net/tap.c

+++ b/net/tap.c

(quoted diff) +2 -3
@@ -296,12 +296,11 @@ static void tap_cleanup(NetClientState *nc)
 
     qemu_purge_queued_packets(nc);
 
-    if (s->down_script[0])
 
-        launch_script(s->down_script, s->down_script_arg, s->fd);
 
-
 
     tap_read_poll(s, false);
 
     tap_write_poll(s, false);
 
     close(s->fd);
 
+    if (s->down_script[0])
 
+        launch_script(s->down_script, s->down_script_arg, s->fd);
 
     s->fd = -1;

}

?

Thanks,

Roy Vardi

#8 lore raw
In reply to Roy Vardi
Quoted 8 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

s/there're/there are/

net/tap-linux.c line 41

Quoted 13 lines

interfaces left

This is achieved using another ioctl

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

---
#define PATH_NET_TUN "/dev/net/tun"

int tap_open(char *ifname, int ifname_size, int *vnet_hdr,

net/tap-linux.c +1 -1
-             int vnet_hdr_required, int mq_required)
+             int vnet_hdr_required, int mq_required, int persistent_required)

Used as a boolean, so s/int/bool/

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

Quoted 12 lines
 {
     struct ifreq ifr;
     int fd, ret;
net/tap-linux.c +5 -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) {
+            error_report("could not configure non-persistent %s (%s): %m",

%m is not portable to non-glibc (then again, this file is Linux-only).

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

Quoted 8 lines
net/tap.c +3 -1
+++ b/net/tap.c
@@ -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_required)

Again, prefer bool.

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

Quoted 6 lines
net/tap.c +1 -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;

Use bool.

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

Quoted 8 lines
net/tap.c +1 -0
     /* 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;

s/1/true/

qapi-schema.json line 2010

Quoted 6 lines
qapi-schema.json +2 -0
+++ b/qapi-schema.json
@@ -2007,6 +2007,8 @@
 #
 # @queues: #optional number of queues to be created for multiqueue capable tap
 #
+# @persistent: #optional for opening tap in persistent mode (default: on) (Since: 2.3)

Long line; please wrap at 80 columns. In QMP, the default is 'true', not 'on' (the command line parser maps multiple strings including 'on' into the single QMP bool type).

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