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 <stefanha@linux.vnet.ibm.com>
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
stable-1.2
Stefan Hajnoczi 2012-07-24 16:35:05 +01:00
parent f6c874e300
commit d33d93b2c4
14 changed files with 85 additions and 75 deletions

37
net.c
View File

@ -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)

View File

@ -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);
}

View File

@ -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 */

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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 */

View File

@ -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;
}

View File

@ -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;

View File

@ -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 */

View File

@ -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;
}

View File

@ -30,7 +30,7 @@
#ifdef CONFIG_VDE
int net_init_vde(const NetClientOptions *opts, const char *name,
VLANState *vlan);
VLANClientState *peer);
#endif /* CONFIG_VDE */