Skip to contentSkip to conversation

net: error when -net type isn't compiled in

PATCH Applied Stefan Hajnoczi 3 participants 13 messages May 27, 2015 – Jun 18, 2015 Networking

lore
Share & tools
lore
#0 lore raw

cover net: error when -net type isn't compiled in

This series cleans up net_client_init1() and reports an error when the -net type isn't compiled into the QEMU binary.

It is based on my net branch with Markus' error cleanups:
https://github.com/stefanha/qemu/tree/net

Stefan Hajnoczi (5):
net: add missing "netmap" to host_net_devices[]
net: replace net_client_init1() netdev whitelist with blacklist
net: raise an error if -net type is invalid
net: drop if expression that is always true
net: simplify net_client_init1()

 net/net.c | 91 ++++++++++++++++++++++++++-------------------------------------
 1 file changed, 37 insertions(+), 54 deletions(-)
Signature · 1 line
2.4.1
#1 lore raw

PATCH 1/5 net: add missing "netmap" to host_net_devices[]

Although hmp-commands.hx lists "netmap" as a valid host_net_add type, the command rejects it because it's missing from the list.

---
 net/net.c | 3 +++
 1 file changed, 3 insertions(+)
net/net.c +3 -0
index db6be12..c5349d2 100644
@@ -58,6 +58,9 @@ const char *host_net_devices[] = {
 #ifdef CONFIG_NET_BRIDGE
     "bridge",
 #endif
+#ifdef CONFIG_NETMAP
+    "netmap",
+#endif
 #ifdef CONFIG_SLIRP
     "user",
 #endif
Signature · 1 line
2.4.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] net: add missing "netmap" to host_net_devices[]

Quoted 13 lines · On Wed, 27 May 2015 17:16:48 +0100 Stefan Hajnoczi <[email protected]> wrote:

Although hmp-commands.hx lists "netmap" as a valid host_net_add type, the command rejects it because it's missing from the list.

---
 net/net.c | 3 +++
 1 file changed, 3 insertions(+)
net/net.c +0 -0
index db6be12..c5349d2 100644

net/net.c const char *host_net_devices[] = { · line 66

Quoted 9 lines
net/net.c +3 -0
@@ -58,6 +58,9 @@ const char *host_net_devices[] = {
 #ifdef CONFIG_NET_BRIDGE
     "bridge",
 #endif
+#ifdef CONFIG_NETMAP
+    "netmap",
+#endif
 #ifdef CONFIG_SLIRP
     "user",
 #endif

Did you consider to remove it from the help text in hmp-commands.hx instead?
That would force the users to use "netdev_add" for this instead - one more reason to get away from the legacy "net" syntax ;-)

Thomas

#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] net: add missing "netmap" to host_net_devices[]

Quoted 22 lines · Thomas Huth
Quoted 19 lines · On Wed, 27 May 2015 17:16:48 +0100 Stefan Hajnoczi <[email protected]> wrote:

Although hmp-commands.hx lists "netmap" as a valid host_net_add type, the command rejects it because it's missing from the list.

---
 net/net.c | 3 +++
 1 file changed, 3 insertions(+)
net/net.c +3 -0
index db6be12..c5349d2 100644
@@ -58,6 +58,9 @@ const char *host_net_devices[] = {
 #ifdef CONFIG_NET_BRIDGE
     "bridge",
 #endif
+#ifdef CONFIG_NETMAP
+    "netmap",
+#endif
Quoted 8 lines

net/net.c const char *host_net_devices[] = { · line 66

Quoted 3 lines
net/net.c +0 -0
 #ifdef CONFIG_SLIRP
     "user",
 #endif

Did you consider to remove it from the help text in hmp-commands.hx instead?
That would force the users to use "netdev_add" for this instead - one more reason to get away from the legacy "net" syntax ;-)

There's no reason to artifically limit the command and the intention of the documentation was clear.

Stefan

#4 lore raw

PATCH 2/5 net: replace net_client_init1() netdev whitelist with blacklist

In reply to Stefan Hajnoczi

It's cumbersome to keep the whitelist up-to-date. New netdev backends should most likely be allowed so a blacklist makes more sense than a whitelist.

---
 net/net.c | 28 +++-------------------------
 1 file changed, 3 insertions(+), 25 deletions(-)
net/net.c +3 -25
index c5349d2..3352b2b 100644
@@ -905,31 +905,9 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         opts = u.netdev->opts;
         name = u.netdev->id;
 
-        switch (opts->kind) {
-#ifdef CONFIG_SLIRP
-        case NET_CLIENT_OPTIONS_KIND_USER:
-#endif
-        case NET_CLIENT_OPTIONS_KIND_TAP:
-        case NET_CLIENT_OPTIONS_KIND_SOCKET:
-#ifdef CONFIG_VDE
-        case NET_CLIENT_OPTIONS_KIND_VDE:
-#endif
-#ifdef CONFIG_NETMAP
-        case NET_CLIENT_OPTIONS_KIND_NETMAP:
-#endif
-#ifdef CONFIG_NET_BRIDGE
-        case NET_CLIENT_OPTIONS_KIND_BRIDGE:
-#endif
-        case NET_CLIENT_OPTIONS_KIND_HUBPORT:
-#ifdef CONFIG_VHOST_NET_USED
-        case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
-#endif
-#ifdef CONFIG_L2TPV3
-        case NET_CLIENT_OPTIONS_KIND_L2TPV3:
-#endif
-            break;
-
-        default:
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
+            opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
+            !net_client_init_fun[opts->kind]) {
             error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
                       "a netdev backend type");
             return -1;
Signature · 1 line
2.4.1
Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] net: replace net_client_init1() netdev whitelist with blacklist

Quoted 38 lines · On Wed, 27 May 2015 17:16:49 +0100 Stefan Hajnoczi <[email protected]> wrote:

It's cumbersome to keep the whitelist up-to-date. New netdev backends should most likely be allowed so a blacklist makes more sense than a whitelist.

---
 net/net.c | 28 +++-------------------------
 1 file changed, 3 insertions(+), 25 deletions(-)
net/net.c +0 -21
index c5349d2..3352b2b 100644
@@ -905,31 +905,9 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         opts = u.netdev->opts;
         name = u.netdev->id;
 
-        switch (opts->kind) {
-#ifdef CONFIG_SLIRP
-        case NET_CLIENT_OPTIONS_KIND_USER:
-#endif
-        case NET_CLIENT_OPTIONS_KIND_TAP:
-        case NET_CLIENT_OPTIONS_KIND_SOCKET:
-#ifdef CONFIG_VDE
-        case NET_CLIENT_OPTIONS_KIND_VDE:
-#endif
-#ifdef CONFIG_NETMAP
-        case NET_CLIENT_OPTIONS_KIND_NETMAP:
-#endif
-#ifdef CONFIG_NET_BRIDGE
-        case NET_CLIENT_OPTIONS_KIND_BRIDGE:
-#endif
-        case NET_CLIENT_OPTIONS_KIND_HUBPORT:
-#ifdef CONFIG_VHOST_NET_USED
-        case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
-#endif
-#ifdef CONFIG_L2TPV3
-        case NET_CLIENT_OPTIONS_KIND_L2TPV3:

net/net.c static int net_client_init1(const void *object, int is_netdev, Error **errp) · line 913

Quoted 10 lines
net/net.c +3 -4
-#endif
-            break;
-
-        default:
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
+            opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
+            !net_client_init_fun[opts->kind]) {
             error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
                       "a netdev backend type");
             return -1;
#6 lore raw

PATCH 3/5 net: raise an error if -net type is invalid

In reply to Stefan Hajnoczi

When a -net type is used that was not compiled into the binary there should be an error message.

Note the special case for -net none, which is a no-op.

---
 net/net.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/net.c +11 -0
index 3352b2b..85a9ddb 100644
@@ -922,6 +922,17 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         }
         /* missing optional values have been initialized to "all bits zero" */
         name = u.net->has_id ? u.net->id : u.net->name;
+
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
+            return 0; /* nothing to do */
+        }
+
+        if (!net_client_init_fun[opts->kind]) {
+            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+                      "a net backend type (maybe it is not compiled "
+                      "into this binary)");
+            return -1;
+        }
     }
 
     if (net_client_init_fun[opts->kind]) {
Signature · 1 line
2.4.1
Reviewed-by on v1 3/5
#7 lore raw

Re: [Qemu-devel] [PATCH 3/5] net: raise an error if -net type is invalid

Quoted 22 lines · On Wed, 27 May 2015 17:16:50 +0100 Stefan Hajnoczi <[email protected]> wrote:

When a -net type is used that was not compiled into the binary there should be an error message.

Note the special case for -net none, which is a no-op.

---
 net/net.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/net.c +4 -0
index 3352b2b..85a9ddb 100644
@@ -922,6 +922,17 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         }
         /* missing optional values have been initialized to "all bits zero" */
         name = u.net->has_id ? u.net->id : u.net->name;
+
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
+            return 0; /* nothing to do */
+        }

net/net.c static int net_client_init1(const void *object, int is_netdev, Error **errp) · line 938

Quoted 10 lines
net/net.c +7 -0
+
+        if (!net_client_init_fun[opts->kind]) {
+            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+                      "a net backend type (maybe it is not compiled "
+                      "into this binary)");
+            return -1;
+        }
     }
 
     if (net_client_init_fun[opts->kind]) {
#8 lore raw

PATCH 4/5 net: drop if expression that is always true

In reply to Stefan Hajnoczi

Both is_netdev and !is_netdev paths already check that net_client_init_func[opts->kind] is non-NULL so there is no need for the if statement.

---
 net/net.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
net/net.c +14 -17
index 85a9ddb..cc1793c 100644
@@ -899,6 +899,7 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
     } u;
     const NetClientOptions *opts;
     const char *name;
+    NetClientState *peer = NULL;
 
     if (is_netdev) {
         u.netdev = object;
@@ -935,25 +936,21 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         }
     }
 
-    if (net_client_init_fun[opts->kind]) {
-        NetClientState *peer = NULL;
+    /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
+     * parameter. */
+    if (!is_netdev &&
+        (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
+         !opts->nic->has_netdev)) {
+        peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
+    }
 
-        /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
-         * parameter. */
-        if (!is_netdev &&
-            (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
-             !opts->nic->has_netdev)) {
-            peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
-        }
-
-        if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
-            /* FIXME drop when all init functions store an Error */
-            if (errp && !*errp) {
-                error_set(errp, QERR_DEVICE_INIT_FAILED,
-                          NetClientOptionsKind_lookup[opts->kind]);
-            }
-            return -1;
+    if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
+        /* FIXME drop when all init functions store an Error */
+        if (errp && !*errp) {
+            error_set(errp, QERR_DEVICE_INIT_FAILED,
+                      NetClientOptionsKind_lookup[opts->kind]);
         }
+        return -1;
     }
     return 0;
 }
Signature · 1 line
2.4.1
Reviewed-by on v1 4/5
#9 lore raw

Re: [Qemu-devel] [PATCH 4/5] net: drop if expression that is always true

Quoted 50 lines · On Wed, 27 May 2015 17:16:51 +0100 Stefan Hajnoczi <[email protected]> wrote:

Both is_netdev and !is_netdev paths already check that net_client_init_func[opts->kind] is non-NULL so there is no need for the if statement.

---
 net/net.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
net/net.c +8 -17
index 85a9ddb..cc1793c 100644
@@ -899,6 +899,7 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
     } u;
     const NetClientOptions *opts;
     const char *name;
+    NetClientState *peer = NULL;
 
     if (is_netdev) {
         u.netdev = object;
@@ -935,25 +936,21 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
         }
     }
 
-    if (net_client_init_fun[opts->kind]) {
-        NetClientState *peer = NULL;
+    /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
+     * parameter. */
+    if (!is_netdev &&
+        (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
+         !opts->nic->has_netdev)) {
+        peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
+    }
 
-        /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
-         * parameter. */
-        if (!is_netdev &&
-            (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
-             !opts->nic->has_netdev)) {
-            peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
-        }
-
-        if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
-            /* FIXME drop when all init functions store an Error */
-            if (errp && !*errp) {
-                error_set(errp, QERR_DEVICE_INIT_FAILED,
-                          NetClientOptionsKind_lookup[opts->kind]);
-            }
-            return -1;

net/net.c static int net_client_init1(const void *object, int is_netdev, Error **errp) · line 956

Quoted 10 lines
net/net.c +6 -0
+    if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
+        /* FIXME drop when all init functions store an Error */
+        if (errp && !*errp) {
+            error_set(errp, QERR_DEVICE_INIT_FAILED,
+                      NetClientOptionsKind_lookup[opts->kind]);
         }
+        return -1;
     }
     return 0;
 }
#10 lore raw

PATCH 5/5 net: simplify net_client_init1()

In reply to Stefan Hajnoczi

Drop the union and move the hubport creation into the !is_netdev case.

---
 net/net.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
net/net.c +16 -22
index cc1793c..e57a089 100644
@@ -893,18 +893,14 @@ static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
 
 static int net_client_init1(const void *object, int is_netdev, Error **errp)
 {
-    union {
-        const Netdev    *netdev;
-        const NetLegacy *net;
-    } u;
     const NetClientOptions *opts;
     const char *name;
     NetClientState *peer = NULL;
 
     if (is_netdev) {
-        u.netdev = object;
-        opts = u.netdev->opts;
-        name = u.netdev->id;
+        const Netdev *netdev = object;
+        opts = netdev->opts;
+        name = netdev->id;
 
         if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
             opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
@@ -914,19 +910,19 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
             return -1;
         }
     } else {
-        u.net = object;
-        opts = u.net->opts;
-        if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
-            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
-                      "a net type");
-            return -1;
-        }
+        const NetLegacy *net = object;
+        opts = net->opts;
         /* missing optional values have been initialized to "all bits zero" */
-        name = u.net->has_id ? u.net->id : u.net->name;
+        name = net->has_id ? net->id : net->name;
 
         if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
             return 0; /* nothing to do */
         }
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
+            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+                      "a net type");
+            return -1;
+        }
 
         if (!net_client_init_fun[opts->kind]) {
             error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
@@ -934,14 +930,12 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
                       "into this binary)");
             return -1;
         }
-    }
 
-    /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
-     * parameter. */
-    if (!is_netdev &&
-        (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
-         !opts->nic->has_netdev)) {
-        peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
+        /* Do not add to a vlan if it's a nic with a netdev= parameter. */
+        if (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
+            !opts->nic->has_netdev) {
+            peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
+        }
     }
 
     if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
Signature · 1 line
2.4.1
Reviewed-by on v1 5/5
#11 lore raw

Re: [Qemu-devel] [PATCH 5/5] net: simplify net_client_init1()

Quoted 71 lines · On Wed, 27 May 2015 17:16:52 +0100 Stefan Hajnoczi <[email protected]> wrote:

Drop the union and move the hubport creation into the !is_netdev case.

---
 net/net.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
net/net.c +11 -20
index cc1793c..e57a089 100644
@@ -893,18 +893,14 @@ static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
 
 static int net_client_init1(const void *object, int is_netdev, Error **errp)
 {
-    union {
-        const Netdev    *netdev;
-        const NetLegacy *net;
-    } u;
     const NetClientOptions *opts;
     const char *name;
     NetClientState *peer = NULL;
 
     if (is_netdev) {
-        u.netdev = object;
-        opts = u.netdev->opts;
-        name = u.netdev->id;
+        const Netdev *netdev = object;
+        opts = netdev->opts;
+        name = netdev->id;
 
         if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
             opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
@@ -914,19 +910,19 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
             return -1;
         }
     } else {
-        u.net = object;
-        opts = u.net->opts;
-        if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
-            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
-                      "a net type");
-            return -1;
-        }
+        const NetLegacy *net = object;
+        opts = net->opts;
         /* missing optional values have been initialized to "all bits zero" */
-        name = u.net->has_id ? u.net->id : u.net->name;
+        name = net->has_id ? net->id : net->name;
 
         if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
             return 0; /* nothing to do */
         }
+        if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
+            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+                      "a net type");
+            return -1;
+        }
 
         if (!net_client_init_fun[opts->kind]) {
             error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
@@ -934,14 +930,12 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
                       "into this binary)");
             return -1;
         }
-    }
 
-    /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
-     * parameter. */
-    if (!is_netdev &&
-        (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||

net/net.c static int net_client_init1(const void *object, int is_netdev, Error **errp) · line 941

Quoted 10 lines
net/net.c +5 -2
-         !opts->nic->has_netdev)) {
-        peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
+        /* Do not add to a vlan if it's a nic with a netdev= parameter. */
+        if (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
+            !opts->nic->has_netdev) {
+            peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
+        }
     }
 
     if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
#12 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

This series cleans up net_client_init1() and reports an error when the -net type isn't compiled into the QEMU binary.

It is based on my net branch with Markus' error cleanups:
https://github.com/stefanha/qemu/tree/net

Stefan Hajnoczi (5):
net: add missing "netmap" to host_net_devices[]
net: replace net_client_init1() netdev whitelist with blacklist
net: raise an error if -net type is invalid
net: drop if expression that is always true
net: simplify net_client_init1()

Quoted 7 lines
 net/net.c | 91 ++++++++++++++++++++++++++-------------------------------------
 1 file changed, 37 insertions(+), 54 deletions(-)
Signature · 1 line
2.4.1

Thanks, applied to my net tree:
https://github.com/stefanha/qemu/commits/net

Stefan