From 027a247bbf703e94258d07e38948946d7b85e91c Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 27 May 2015 17:16:48 +0100 Subject: [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. Signed-off-by: Stefan Hajnoczi Message-id: 1432743412-15943-2-git-send-email-stefanha@redhat.com --- net/net.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/net.c b/net/net.c index cc36c7b4fe..f43af97af3 100644 --- a/net/net.c +++ b/net/net.c @@ -60,6 +60,9 @@ const char *host_net_devices[] = { #ifdef CONFIG_NET_BRIDGE "bridge", #endif +#ifdef CONFIG_NETMAP + "netmap", +#endif #ifdef CONFIG_SLIRP "user", #endif From 1322629b4f25730aed973d51983e7a3b021fe9c9 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 27 May 2015 17:16:49 +0100 Subject: [PATCH 2/5] net: replace net_client_init1() netdev whitelist with blacklist 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. Signed-off-by: Stefan Hajnoczi Reviewed-by: Thomas Huth Message-id: 1432743412-15943-3-git-send-email-stefanha@redhat.com --- net/net.c | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/net/net.c b/net/net.c index f43af97af3..63450c0300 100644 --- a/net/net.c +++ b/net/net.c @@ -925,31 +925,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_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", "a netdev backend type"); return -1; From d139e9a6cf01b8c31f5904b4ba40521d7224f7de Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 27 May 2015 17:16:50 +0100 Subject: [PATCH 3/5] net: raise an error if -net type is invalid 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. Signed-off-by: Stefan Hajnoczi Reviewed-by: Thomas Huth Message-id: 1432743412-15943-4-git-send-email-stefanha@redhat.com --- net/net.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/net/net.c b/net/net.c index 63450c0300..7c1b203b46 100644 --- a/net/net.c +++ b/net/net.c @@ -942,6 +942,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_setg(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]) { From 4ef0defbad9bc8b195f3392d1b7dcb42cd7ebe11 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 27 May 2015 17:16:51 +0100 Subject: [PATCH 4/5] net: drop if expression that is always true 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. Signed-off-by: Stefan Hajnoczi Reviewed-by: Thomas Huth Message-id: 1432743412-15943-5-git-send-email-stefanha@redhat.com --- net/net.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/net/net.c b/net/net.c index 7c1b203b46..8e5b6f6363 100644 --- a/net/net.c +++ b/net/net.c @@ -919,6 +919,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; @@ -955,25 +956,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_setg(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_setg(errp, QERR_DEVICE_INIT_FAILED, + NetClientOptionsKind_lookup[opts->kind]); } + return -1; } return 0; } From 1e81aba5ac0b908ab859bf8ddf43ece33732d49c Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 27 May 2015 17:16:52 +0100 Subject: [PATCH 5/5] net: simplify net_client_init1() Drop the union and move the hubport creation into the !is_netdev case. Signed-off-by: Stefan Hajnoczi Reviewed-by: Thomas Huth Message-id: 1432743412-15943-6-git-send-email-stefanha@redhat.com --- net/net.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/net/net.c b/net/net.c index 8e5b6f6363..6ff7fec1bb 100644 --- a/net/net.c +++ b/net/net.c @@ -913,18 +913,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 || @@ -934,19 +930,19 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) return -1; } } else { - u.net = object; - opts = u.net->opts; + const NetLegacy *net = object; + opts = net->opts; + /* missing optional values have been initialized to "all bits zero" */ + 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_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", "a net type"); return -1; } - /* 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_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", @@ -954,14 +950,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) {