From ca7eb1848bb06d9b75784d7760b83c7b0beb1102 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 15 May 2015 13:58:49 +0200 Subject: net: Improve error message for -net hubport a bit Type "hubport" is valid only with -netdev. Unfortunately, that's detected late and the error message doesn't explain why: $ qemu-system-i386 -net hubport,id=foo,hubid=0 qemu-system-i386: -net hubport,id=foo,hubid=0: Device 'hubport' could not be initialized Improve the error message to "Parameter 'type' expects a net type". Not fixed: -net hubport without the parameters required by -netdev hubport still asks for those parameters: $ qemu-system-i386 -net hubport qemu-system-i386: -net hubport: Parameter 'hubid' is missing Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-id: 1431691143-1015-2-git-send-email-armbru@redhat.com Signed-off-by: Stefan Hajnoczi --- net/net.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'net/net.c') diff --git a/net/net.c b/net/net.c index 7427f6a65a..d9aaeb5341 100644 --- a/net/net.c +++ b/net/net.c @@ -882,6 +882,11 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) } 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; + } /* missing optional values have been initialized to "all bits zero" */ name = u.net->has_id ? u.net->id : u.net->name; } -- cgit 1.4.1 From a30ecde6e795682d1473c45acae66a60a76fca2f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 15 May 2015 13:58:50 +0200 Subject: net: Permit incremental conversion of init functions to Error Error reporting for netdev_add is broken: the net_client_init_fun[] report the actual errors with (at best) error_report(), and their caller net_client_init1() makes up a generic error on top. For command line and HMP, this produces an mildly ugly error cascade. In QMP, the actual errors go to stderr, and the generic error becomes the command's error reply. To fix this, we need to convert the net_client_init_fun[] to Error. To permit fixing them one by one, add an Error ** parameter to the net_client_init_fun[]. If the call fails without returning an Error, make up the same generic Error as before. But if it returns one, use that instead. Since none of them does so far, no functional change. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-id: 1431691143-1015-3-git-send-email-armbru@redhat.com Signed-off-by: Stefan Hajnoczi --- net/clients.h | 20 ++++++++++---------- net/dump.c | 3 ++- net/hub.c | 2 +- net/l2tpv3.c | 5 ++--- net/net.c | 15 +++++++++------ net/netmap.c | 3 ++- net/slirp.c | 3 ++- net/socket.c | 3 ++- net/tap-win32.c | 3 ++- net/tap.c | 6 ++++-- net/vde.c | 3 ++- net/vhost-user.c | 3 ++- 12 files changed, 40 insertions(+), 29 deletions(-) (limited to 'net/net.c') diff --git a/net/clients.h b/net/clients.h index 2e8fedad8d..d47530e82f 100644 --- a/net/clients.h +++ b/net/clients.h @@ -28,38 +28,38 @@ #include "qapi-types.h" int net_init_dump(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #ifdef CONFIG_SLIRP int net_init_slirp(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #endif int net_init_hubport(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); int net_init_socket(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); int net_init_tap(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); int net_init_bridge(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); int net_init_l2tpv3(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #ifdef CONFIG_VDE int net_init_vde(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #endif #ifdef CONFIG_NETMAP int net_init_netmap(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #endif int net_init_vhost_user(const NetClientOptions *opts, const char *name, - NetClientState *peer); + NetClientState *peer, Error **errp); #endif /* QEMU_NET_CLIENTS_H */ diff --git a/net/dump.c b/net/dump.c index 9d3a09e334..214e88a768 100644 --- a/net/dump.c +++ b/net/dump.c @@ -146,8 +146,9 @@ static int net_dump_init(NetClientState *peer, const char *device, } int net_init_dump(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ int len; const char *file; char def_file[128]; diff --git a/net/hub.c b/net/hub.c index 261f8ccc3f..3047f12766 100644 --- a/net/hub.c +++ b/net/hub.c @@ -281,7 +281,7 @@ int net_hub_id_for_client(NetClientState *nc, int *id) } int net_init_hubport(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { const NetdevHubPortOptions *hubport; diff --git a/net/l2tpv3.c b/net/l2tpv3.c index 8c598b09bc..ed395dc126 100644 --- a/net/l2tpv3.c +++ b/net/l2tpv3.c @@ -536,10 +536,9 @@ static NetClientInfo net_l2tpv3_info = { int net_init_l2tpv3(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { - - + /* FIXME error_setg(errp, ...) on failure */ const NetdevL2TPv3Options *l2tpv3; NetL2TPV3State *s; NetClientState *nc; diff --git a/net/net.c b/net/net.c index d9aaeb5341..3295741d1d 100644 --- a/net/net.c +++ b/net/net.c @@ -740,8 +740,9 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models, } static int net_init_nic(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ int idx; NICInfo *nd; const NetLegacyNicOptions *nic; @@ -809,7 +810,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])( const NetClientOptions *opts, const char *name, - NetClientState *peer) = { + NetClientState *peer, Error **errp) = { [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic, #ifdef CONFIG_SLIRP [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp, @@ -902,10 +903,12 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL); } - if (net_client_init_fun[opts->kind](opts, name, peer) < 0) { - /* TODO push error reporting into init() methods */ - error_set(errp, QERR_DEVICE_INIT_FAILED, - NetClientOptionsKind_lookup[opts->kind]); + 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; } } diff --git a/net/netmap.c b/net/netmap.c index 0c1772b03f..69300eb1ae 100644 --- a/net/netmap.c +++ b/net/netmap.c @@ -446,8 +446,9 @@ static NetClientInfo net_netmap_info = { * ... -net netmap,ifname="..." */ int net_init_netmap(const NetClientOptions *opts, - const char *name, NetClientState *peer) + const char *name, NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevNetmapOptions *netmap_opts = opts->netmap; NetClientState *nc; NetmapPriv me; diff --git a/net/slirp.c b/net/slirp.c index 9bbed7447a..0e15cf6750 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -737,8 +737,9 @@ static const char **slirp_dnssearch(const StringList *dnsname) } int net_init_slirp(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ struct slirp_config_str *config; char *vnet; int ret; diff --git a/net/socket.c b/net/socket.c index c30e03f5ae..5a19aa1881 100644 --- a/net/socket.c +++ b/net/socket.c @@ -693,8 +693,9 @@ static int net_socket_udp_init(NetClientState *peer, } int net_init_socket(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ Error *err = NULL; const NetdevSocketOptions *sock; diff --git a/net/tap-win32.c b/net/tap-win32.c index 8aee611f7d..f6fc9610a7 100644 --- a/net/tap-win32.c +++ b/net/tap-win32.c @@ -752,8 +752,9 @@ static int tap_win32_init(NetClientState *peer, const char *model, } int net_init_tap(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevTapOptions *tap; assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP); diff --git a/net/tap.c b/net/tap.c index 968df46c8c..8f06cb7382 100644 --- a/net/tap.c +++ b/net/tap.c @@ -531,8 +531,9 @@ static int net_bridge_run_helper(const char *helper, const char *bridge) } int net_init_bridge(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevBridgeOptions *bridge; const char *helper, *br; @@ -699,8 +700,9 @@ static int get_fds(char *str, char *fds[], int max) } int net_init_tap(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevTapOptions *tap; int fd, vnet_hdr = 0, i = 0, queues; /* for the no-fd, no-helper case */ diff --git a/net/vde.c b/net/vde.c index 2a619fbc81..dacaa64b47 100644 --- a/net/vde.c +++ b/net/vde.c @@ -110,8 +110,9 @@ static int net_vde_init(NetClientState *peer, const char *model, } int net_init_vde(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevVdeOptions *vde; assert(opts->kind == NET_CLIENT_OPTIONS_KIND_VDE); diff --git a/net/vhost-user.c b/net/vhost-user.c index 1d86a2be11..11899c53c0 100644 --- a/net/vhost-user.c +++ b/net/vhost-user.c @@ -223,8 +223,9 @@ static int net_vhost_check_net(QemuOpts *opts, void *opaque) } int net_init_vhost_user(const NetClientOptions *opts, const char *name, - NetClientState *peer) + NetClientState *peer, Error **errp) { + /* FIXME error_setg(errp, ...) on failure */ const NetdevVhostUserOptions *vhost_user_opts; CharDriverState *chr; -- cgit 1.4.1 From 6630886863d4a9b3b7bcb3b0e2895d83eb269c75 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 15 May 2015 13:58:51 +0200 Subject: net: Improve -net nic error reporting When -net nic fails, it first reports a specific error, then a generic one, like this: $ qemu-system-x86_64 -net nic,netdev=nonexistent qemu-system-x86_64: -net nic,netdev=nonexistent: netdev 'nonexistent' not found qemu-system-x86_64: -net nic,netdev=nonexistent: Device 'nic' could not be initialized Convert net_init_nic() to Error to get rid of the unwanted second error message. While there, tidy up an Overcapitalized Error Message. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-id: 1431691143-1015-4-git-send-email-armbru@redhat.com Signed-off-by: Stefan Hajnoczi --- net/net.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'net/net.c') diff --git a/net/net.c b/net/net.c index 3295741d1d..0fe5b8bbd5 100644 --- a/net/net.c +++ b/net/net.c @@ -742,7 +742,6 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models, static int net_init_nic(const NetClientOptions *opts, const char *name, NetClientState *peer, Error **errp) { - /* FIXME error_setg(errp, ...) on failure */ int idx; NICInfo *nd; const NetLegacyNicOptions *nic; @@ -752,7 +751,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, idx = nic_get_free_idx(); if (idx == -1 || nb_nics >= MAX_NICS) { - error_report("Too Many NICs"); + error_setg(errp, "too many NICs"); return -1; } @@ -763,7 +762,7 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, if (nic->has_netdev) { nd->netdev = qemu_find_netdev(nic->netdev); if (!nd->netdev) { - error_report("netdev '%s' not found", nic->netdev); + error_setg(errp, "netdev '%s' not found", nic->netdev); return -1; } } else { @@ -780,19 +779,20 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, if (nic->has_macaddr && net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { - error_report("invalid syntax for ethernet address"); + error_setg(errp, "invalid syntax for ethernet address"); return -1; } if (nic->has_macaddr && is_multicast_ether_addr(nd->macaddr.a)) { - error_report("NIC cannot have multicast MAC address (odd 1st byte)"); + error_setg(errp, + "NIC cannot have multicast MAC address (odd 1st byte)"); return -1; } qemu_macaddr_default_if_unset(&nd->macaddr); if (nic->has_vectors) { if (nic->vectors > 0x7ffffff) { - error_report("invalid # of vectors: %"PRIu32, nic->vectors); + error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); return -1; } nd->nvectors = nic->vectors; -- cgit 1.4.1 From 2bc22a58e16f0650e56dccfac9495e5aef58e2ef Mon Sep 17 00:00:00 2001 From: Shannon Zhao Date: Thu, 21 May 2015 17:44:48 +0800 Subject: net/net: Record usage status of mac address Currently QEMU dynamically generates mac address for the NIC which doesn't specify the mac address. But when we hotplug a NIC without specifying mac address, the mac address will increase for the same NIC along with hotplug and hot-unplug, and at last it will overflow. And if we codeplug one NIC with mac address e.g. "52:54:00:12:34:56", then hotplug one NIC without specifying mac address and the mac address of the hotplugged NIC is duplicate of "52:54:00:12:34:56". This patch add a mac_table to record the usage status and free the mac address when the NIC is unrealized. Signed-off-by: Shannon Zhao Signed-off-by: Shannon Zhao Signed-off-by: Stefan Hajnoczi --- net/net.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'net/net.c') diff --git a/net/net.c b/net/net.c index 0fe5b8bbd5..db6be12a1e 100644 --- a/net/net.c +++ b/net/net.c @@ -167,19 +167,68 @@ void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) macaddr[3], macaddr[4], macaddr[5]); } +static int mac_table[256] = {0}; + +static void qemu_macaddr_set_used(MACAddr *macaddr) +{ + int index; + + for (index = 0x56; index < 0xFF; index++) { + if (macaddr->a[5] == index) { + mac_table[index]++; + } + } +} + +static void qemu_macaddr_set_free(MACAddr *macaddr) +{ + int index; + static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; + + if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { + return; + } + for (index = 0x56; index < 0xFF; index++) { + if (macaddr->a[5] == index) { + mac_table[index]--; + } + } +} + +static int qemu_macaddr_get_free(void) +{ + int index; + + for (index = 0x56; index < 0xFF; index++) { + if (mac_table[index] == 0) { + return index; + } + } + + return -1; +} + void qemu_macaddr_default_if_unset(MACAddr *macaddr) { - static int index = 0; static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; + static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; + + if (memcmp(macaddr, &zero, sizeof(zero)) != 0) { + if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { + return; + } else { + qemu_macaddr_set_used(macaddr); + return; + } + } - if (memcmp(macaddr, &zero, sizeof(zero)) != 0) - return; macaddr->a[0] = 0x52; macaddr->a[1] = 0x54; macaddr->a[2] = 0x00; macaddr->a[3] = 0x12; macaddr->a[4] = 0x34; - macaddr->a[5] = 0x56 + index++; + macaddr->a[5] = qemu_macaddr_get_free(); + qemu_macaddr_set_used(macaddr); } /** @@ -374,6 +423,8 @@ void qemu_del_nic(NICState *nic) { int i, queues = MAX(nic->conf->peers.queues, 1); + qemu_macaddr_set_free(&nic->conf->macaddr); + /* If this is a peer NIC and peer has already been deleted, free it now. */ if (nic->peer_deleted) { for (i = 0; i < queues; i++) { -- cgit 1.4.1