From d33d93b2c40b820c2cfab1e2e6da631f12091957 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 24 Jul 2012 16:35:05 +0100 Subject: [PATCH] net: Use hubs for the vlan feature Stop using the special-case vlan code in net.c. Instead use the hub net client to implement the vlan feature. The next patch will remove vlan code from net.c completely. Signed-off-by: Stefan Hajnoczi Signed-off-by: Zhi Yong Wu Reviewed-by: Laszlo Ersek --- net.c | 37 ++++++++++++++++++++----------------- net/dump.c | 19 +++++++++++++------ net/dump.h | 2 +- net/hub.c | 6 +++--- net/hub.h | 2 +- net/slirp.c | 8 ++++---- net/slirp.h | 2 +- net/socket.c | 48 ++++++++++++++++++++++++------------------------ net/socket.h | 2 +- net/tap-win32.c | 8 ++++---- net/tap.c | 12 ++++++------ net/tap.h | 4 ++-- net/vde.c | 8 ++++---- net/vde.h | 2 +- 14 files changed, 85 insertions(+), 75 deletions(-) diff --git a/net.c b/net.c index e7a8d81591..4feca6c0b9 100644 --- a/net.c +++ b/net.c @@ -157,23 +157,25 @@ void qemu_macaddr_default_if_unset(MACAddr *macaddr) macaddr->a[5] = 0x56 + index++; } +/** + * Generate a name for net client + * + * Only net clients created with the legacy -net option need this. Naming is + * mandatory for net clients created with -netdev. + */ static char *assign_name(VLANClientState *vc1, const char *model) { - VLANState *vlan; VLANClientState *vc; char buf[256]; int id = 0; - QTAILQ_FOREACH(vlan, &vlans, next) { - QTAILQ_FOREACH(vc, &vlan->clients, next) { - if (vc != vc1 && strcmp(vc->model, model) == 0) { - id++; - } - } - } - QTAILQ_FOREACH(vc, &non_vlan_clients, next) { - if (vc != vc1 && strcmp(vc->model, model) == 0) { + if (vc == vc1) { + continue; + } + /* For compatibility only bump id for net clients on a vlan */ + if (strcmp(vc->model, model) == 0 && + net_hub_id_for_client(vc, NULL) == 0) { id++; } } @@ -750,7 +752,7 @@ int net_handle_fd_param(Monitor *mon, const char *param) } static int net_init_nic(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { int idx; NICInfo *nd; @@ -776,8 +778,8 @@ static int net_init_nic(const NetClientOptions *opts, const char *name, return -1; } } else { - assert(vlan); - nd->vlan = vlan; + assert(peer); + nd->netdev = peer; } if (name) { nd->name = g_strdup(name); @@ -816,7 +818,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, - VLANState *vlan) = { + VLANClientState *peer) = { [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic, #ifdef CONFIG_SLIRP [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp, @@ -876,17 +878,17 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) } if (net_client_init_fun[opts->kind]) { - VLANState *vlan = NULL; + VLANClientState *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)) { - vlan = qemu_find_vlan(u.net->has_vlan ? u.net->vlan : 0, true); + peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL); } - if (net_client_init_fun[opts->kind](opts, name, vlan) < 0) { + 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]); @@ -1085,6 +1087,7 @@ void do_info_network(Monitor *mon) print_net_client(mon, peer); } } + net_hub_info(mon); } void qmp_set_link(const char *name, bool up, Error **errp) diff --git a/net/dump.c b/net/dump.c index b575430787..9d7bf3be4d 100644 --- a/net/dump.c +++ b/net/dump.c @@ -27,6 +27,7 @@ #include "qemu-error.h" #include "qemu-log.h" #include "qemu-timer.h" +#include "hub.h" typedef struct DumpState { VLANClientState nc; @@ -99,7 +100,7 @@ static NetClientInfo net_dump_info = { .cleanup = dump_cleanup, }; -static int net_dump_init(VLANState *vlan, const char *device, +static int net_dump_init(VLANClientState *peer, const char *device, const char *name, const char *filename, int len) { struct pcap_file_hdr hdr; @@ -128,7 +129,7 @@ static int net_dump_init(VLANState *vlan, const char *device, return -1; } - nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name); + nc = qemu_new_net_client(&net_dump_info, NULL, peer, device, name); snprintf(nc->info_str, sizeof(nc->info_str), "dump to %s (len=%d)", filename, len); @@ -145,7 +146,7 @@ static int net_dump_init(VLANState *vlan, const char *device, } int net_init_dump(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { int len; const char *file; @@ -155,12 +156,18 @@ int net_init_dump(const NetClientOptions *opts, const char *name, assert(opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP); dump = opts->dump; - assert(vlan); + assert(peer); if (dump->has_file) { file = dump->file; } else { - snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); + int id; + int ret; + + ret = net_hub_id_for_client(peer, &id); + assert(ret == 0); /* peer must be on a hub */ + + snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", id); file = def_file; } @@ -174,5 +181,5 @@ int net_init_dump(const NetClientOptions *opts, const char *name, len = 65536; } - return net_dump_init(vlan, "dump", name, file, len); + return net_dump_init(peer, "dump", name, file, len); } diff --git a/net/dump.h b/net/dump.h index 0fa2dd72ca..9d70195a8b 100644 --- a/net/dump.h +++ b/net/dump.h @@ -28,6 +28,6 @@ #include "qapi-types.h" int net_init_dump(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); #endif /* QEMU_NET_DUMP_H */ diff --git a/net/hub.c b/net/hub.c index be6e014aad..ee9d878313 100644 --- a/net/hub.c +++ b/net/hub.c @@ -206,15 +206,15 @@ int net_hub_id_for_client(VLANClientState *nc, int *id) } int net_init_hubport(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevHubPortOptions *hubport; assert(opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT); hubport = opts->hubport; - /* The hub is a "vlan" so this option makes no sense. */ - if (vlan) { + /* Treat hub port like a backend, NIC must be the one to peer */ + if (peer) { return -EINVAL; } diff --git a/net/hub.h b/net/hub.h index f0d98f2bf6..a5fa2ba89c 100644 --- a/net/hub.h +++ b/net/hub.h @@ -18,7 +18,7 @@ #include "qemu-common.h" int net_init_hubport(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); VLANClientState *net_hub_add_port(int hub_id, const char *name); void net_hub_info(Monitor *mon); int net_hub_id_for_client(VLANClientState *nc, int *id); diff --git a/net/slirp.c b/net/slirp.c index 5c2e6b2cec..97e380cb27 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -135,7 +135,7 @@ static NetClientInfo net_slirp_info = { .cleanup = net_slirp_cleanup, }; -static int net_slirp_init(VLANState *vlan, const char *model, +static int net_slirp_init(VLANClientState *peer, const char *model, const char *name, int restricted, const char *vnetwork, const char *vhost, const char *vhostname, const char *tftp_export, @@ -238,7 +238,7 @@ static int net_slirp_init(VLANState *vlan, const char *model, } #endif - nc = qemu_new_net_client(&net_slirp_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_slirp_info, NULL, peer, model, name); snprintf(nc->info_str, sizeof(nc->info_str), "net=%s,restrict=%s", inet_ntoa(net), @@ -703,7 +703,7 @@ net_init_slirp_configs(const StringList *fwd, int flags) } int net_init_slirp(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { struct slirp_config_str *config; char *vnet; @@ -722,7 +722,7 @@ int net_init_slirp(const NetClientOptions *opts, const char *name, net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD); net_init_slirp_configs(user->guestfwd, 0); - ret = net_slirp_init(vlan, "user", name, user->restrict, vnet, user->host, + ret = net_slirp_init(peer, "user", name, user->restrict, vnet, user->host, user->hostname, user->tftp, user->bootfile, user->dhcpstart, user->dns, user->smb, user->smbserver); diff --git a/net/slirp.h b/net/slirp.h index e2c71eeca0..1ff21b064a 100644 --- a/net/slirp.h +++ b/net/slirp.h @@ -32,7 +32,7 @@ #ifdef CONFIG_SLIRP int net_init_slirp(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict); void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict); diff --git a/net/socket.c b/net/socket.c index 600c287d79..09bdc66e10 100644 --- a/net/socket.c +++ b/net/socket.c @@ -44,7 +44,7 @@ typedef struct NetSocketState { } NetSocketState; typedef struct NetSocketListenState { - VLANState *vlan; + VLANClientState *peer; char *model; char *name; int fd; @@ -245,7 +245,7 @@ static NetClientInfo net_dgram_socket_info = { .cleanup = net_socket_cleanup, }; -static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, +static NetSocketState *net_socket_fd_init_dgram(VLANClientState *peer, const char *model, const char *name, int fd, int is_connected) @@ -287,7 +287,7 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, } } - nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_dgram_socket_info, NULL, peer, model, name); snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (%s mcast=%s:%d)", @@ -323,7 +323,7 @@ static NetClientInfo net_socket_info = { .cleanup = net_socket_cleanup, }; -static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, +static NetSocketState *net_socket_fd_init_stream(VLANClientState *peer, const char *model, const char *name, int fd, int is_connected) @@ -331,7 +331,7 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, VLANClientState *nc; NetSocketState *s; - nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_socket_info, NULL, peer, model, name); snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); @@ -347,7 +347,7 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, return s; } -static NetSocketState *net_socket_fd_init(VLANState *vlan, +static NetSocketState *net_socket_fd_init(VLANClientState *peer, const char *model, const char *name, int fd, int is_connected) { @@ -362,13 +362,13 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan, } switch(so_type) { case SOCK_DGRAM: - return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); + return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); case SOCK_STREAM: - return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); + return net_socket_fd_init_stream(peer, model, name, fd, is_connected); default: /* who knows ... this could be a eg. a pty, do warn and continue as stream */ fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); - return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); + return net_socket_fd_init_stream(peer, model, name, fd, is_connected); } return NULL; } @@ -390,7 +390,7 @@ static void net_socket_accept(void *opaque) break; } } - s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); + s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1); if (s1) { snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), "socket: connection from %s:%d", @@ -398,7 +398,7 @@ static void net_socket_accept(void *opaque) } } -static int net_socket_listen_init(VLANState *vlan, +static int net_socket_listen_init(VLANClientState *peer, const char *model, const char *name, const char *host_str) @@ -438,7 +438,7 @@ static int net_socket_listen_init(VLANState *vlan, closesocket(fd); return -1; } - s->vlan = vlan; + s->peer = peer; s->model = g_strdup(model); s->name = name ? g_strdup(name) : NULL; s->fd = fd; @@ -446,7 +446,7 @@ static int net_socket_listen_init(VLANState *vlan, return 0; } -static int net_socket_connect_init(VLANState *vlan, +static int net_socket_connect_init(VLANClientState *peer, const char *model, const char *name, const char *host_str) @@ -487,7 +487,7 @@ static int net_socket_connect_init(VLANState *vlan, break; } } - s = net_socket_fd_init(vlan, model, name, fd, connected); + s = net_socket_fd_init(peer, model, name, fd, connected); if (!s) return -1; snprintf(s->nc.info_str, sizeof(s->nc.info_str), @@ -496,7 +496,7 @@ static int net_socket_connect_init(VLANState *vlan, return 0; } -static int net_socket_mcast_init(VLANState *vlan, +static int net_socket_mcast_init(VLANClientState *peer, const char *model, const char *name, const char *host_str, @@ -522,7 +522,7 @@ static int net_socket_mcast_init(VLANState *vlan, if (fd < 0) return -1; - s = net_socket_fd_init(vlan, model, name, fd, 0); + s = net_socket_fd_init(peer, model, name, fd, 0); if (!s) return -1; @@ -535,7 +535,7 @@ static int net_socket_mcast_init(VLANState *vlan, } -static int net_socket_udp_init(VLANState *vlan, +static int net_socket_udp_init(VLANClientState *peer, const char *model, const char *name, const char *rhost, @@ -573,7 +573,7 @@ static int net_socket_udp_init(VLANState *vlan, return -1; } - s = net_socket_fd_init(vlan, model, name, fd, 0); + s = net_socket_fd_init(peer, model, name, fd, 0); if (!s) { return -1; } @@ -587,7 +587,7 @@ static int net_socket_udp_init(VLANState *vlan, } int net_init_socket(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevSocketOptions *sock; @@ -610,21 +610,21 @@ int net_init_socket(const NetClientOptions *opts, const char *name, int fd; fd = net_handle_fd_param(cur_mon, sock->fd); - if (fd == -1 || !net_socket_fd_init(vlan, "socket", name, fd, 1)) { + if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) { return -1; } return 0; } if (sock->has_listen) { - if (net_socket_listen_init(vlan, "socket", name, sock->listen) == -1) { + if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { return -1; } return 0; } if (sock->has_connect) { - if (net_socket_connect_init(vlan, "socket", name, sock->connect) == + if (net_socket_connect_init(peer, "socket", name, sock->connect) == -1) { return -1; } @@ -634,7 +634,7 @@ int net_init_socket(const NetClientOptions *opts, const char *name, if (sock->has_mcast) { /* if sock->localaddr is missing, it has been initialized to "all bits * zero" */ - if (net_socket_mcast_init(vlan, "socket", name, sock->mcast, + if (net_socket_mcast_init(peer, "socket", name, sock->mcast, sock->localaddr) == -1) { return -1; } @@ -646,7 +646,7 @@ int net_init_socket(const NetClientOptions *opts, const char *name, error_report("localaddr= is mandatory with udp="); return -1; } - if (net_socket_udp_init(vlan, "udp", name, sock->udp, sock->localaddr) == + if (net_socket_udp_init(peer, "udp", name, sock->udp, sock->localaddr) == -1) { return -1; } diff --git a/net/socket.h b/net/socket.h index c4809ad0d9..82b4d169d9 100644 --- a/net/socket.h +++ b/net/socket.h @@ -28,6 +28,6 @@ #include "qapi-types.h" int net_init_socket(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); #endif /* QEMU_NET_SOCKET_H */ diff --git a/net/tap-win32.c b/net/tap-win32.c index 232807236a..0e3b883c00 100644 --- a/net/tap-win32.c +++ b/net/tap-win32.c @@ -673,7 +673,7 @@ static NetClientInfo net_tap_win32_info = { .cleanup = tap_cleanup, }; -static int tap_win32_init(VLANState *vlan, const char *model, +static int tap_win32_init(VLANClientState *peer, const char *model, const char *name, const char *ifname) { VLANClientState *nc; @@ -685,7 +685,7 @@ static int tap_win32_init(VLANState *vlan, const char *model, return -1; } - nc = qemu_new_net_client(&net_tap_win32_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_tap_win32_info, NULL, peer, model, name); s = DO_UPCAST(TAPState, nc, nc); @@ -700,7 +700,7 @@ static int tap_win32_init(VLANState *vlan, const char *model, } int net_init_tap(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevTapOptions *tap; @@ -712,7 +712,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name, return -1; } - if (tap_win32_init(vlan, "tap", name, tap->ifname) == -1) { + if (tap_win32_init(peer, "tap", name, tap->ifname) == -1) { return -1; } diff --git a/net/tap.c b/net/tap.c index 72062275fe..43c3fcd749 100644 --- a/net/tap.c +++ b/net/tap.c @@ -322,7 +322,7 @@ static NetClientInfo net_tap_info = { .cleanup = tap_cleanup, }; -static TAPState *net_tap_fd_init(VLANState *vlan, +static TAPState *net_tap_fd_init(VLANClientState *peer, const char *model, const char *name, int fd, @@ -331,7 +331,7 @@ static TAPState *net_tap_fd_init(VLANState *vlan, VLANClientState *nc; TAPState *s; - nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_tap_info, NULL, peer, model, name); s = DO_UPCAST(TAPState, nc, nc); @@ -514,7 +514,7 @@ static int net_bridge_run_helper(const char *helper, const char *bridge) } int net_init_bridge(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevBridgeOptions *bridge; const char *helper, *br; @@ -537,7 +537,7 @@ int net_init_bridge(const NetClientOptions *opts, const char *name, vnet_hdr = tap_probe_vnet_hdr(fd); - s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr); + s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); if (!s) { close(fd); return -1; @@ -587,7 +587,7 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, } int net_init_tap(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevTapOptions *tap; @@ -650,7 +650,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name, model = "tap"; } - s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr); + s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); if (!s) { close(fd); return -1; diff --git a/net/tap.h b/net/tap.h index 19dea58dc5..113906fc1b 100644 --- a/net/tap.h +++ b/net/tap.h @@ -33,7 +33,7 @@ #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" int net_init_tap(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required); @@ -59,6 +59,6 @@ struct vhost_net; struct vhost_net *tap_get_vhost_net(VLANClientState *vc); int net_init_bridge(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); #endif /* QEMU_NET_TAP_H */ diff --git a/net/vde.c b/net/vde.c index ee19f5c42c..302a022753 100644 --- a/net/vde.c +++ b/net/vde.c @@ -75,7 +75,7 @@ static NetClientInfo net_vde_info = { .cleanup = vde_cleanup, }; -static int net_vde_init(VLANState *vlan, const char *model, +static int net_vde_init(VLANClientState *peer, const char *model, const char *name, const char *sock, int port, const char *group, int mode) { @@ -96,7 +96,7 @@ static int net_vde_init(VLANState *vlan, const char *model, return -1; } - nc = qemu_new_net_client(&net_vde_info, vlan, NULL, model, name); + nc = qemu_new_net_client(&net_vde_info, NULL, peer, model, name); snprintf(nc->info_str, sizeof(nc->info_str), "sock=%s,fd=%d", sock, vde_datafd(vde)); @@ -111,7 +111,7 @@ static int net_vde_init(VLANState *vlan, const char *model, } int net_init_vde(const NetClientOptions *opts, const char *name, - VLANState *vlan) + VLANClientState *peer) { const NetdevVdeOptions *vde; @@ -119,7 +119,7 @@ int net_init_vde(const NetClientOptions *opts, const char *name, vde = opts->vde; /* missing optional values have been initialized to "all bits zero" */ - if (net_vde_init(vlan, "vde", name, vde->sock, vde->port, vde->group, + if (net_vde_init(peer, "vde", name, vde->sock, vde->port, vde->group, vde->has_mode ? vde->mode : 0700) == -1) { return -1; } diff --git a/net/vde.h b/net/vde.h index ad502ef4de..d6f7af439d 100644 --- a/net/vde.h +++ b/net/vde.h @@ -30,7 +30,7 @@ #ifdef CONFIG_VDE int net_init_vde(const NetClientOptions *opts, const char *name, - VLANState *vlan); + VLANClientState *peer); #endif /* CONFIG_VDE */