2012-07-24 19:35:04 +04:00
|
|
|
/*
|
|
|
|
* Hub net client
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
|
|
|
|
* Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:00 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "monitor/monitor.h"
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2012-09-17 20:43:51 +04:00
|
|
|
#include "clients.h"
|
2012-07-24 19:35:04 +04:00
|
|
|
#include "hub.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/iov.h"
|
2012-07-24 19:35:04 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A hub broadcasts incoming packets to all its ports except the source port.
|
|
|
|
* Hubs can be used to provide independent network segments, also confusingly
|
|
|
|
* named the QEMU 'vlan' feature.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct NetHub NetHub;
|
|
|
|
|
|
|
|
typedef struct NetHubPort {
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState nc;
|
2012-07-24 19:35:04 +04:00
|
|
|
QLIST_ENTRY(NetHubPort) next;
|
|
|
|
NetHub *hub;
|
|
|
|
int id;
|
|
|
|
} NetHubPort;
|
|
|
|
|
|
|
|
struct NetHub {
|
|
|
|
int id;
|
|
|
|
QLIST_ENTRY(NetHub) next;
|
|
|
|
int num_ports;
|
|
|
|
QLIST_HEAD(, NetHubPort) ports;
|
|
|
|
};
|
|
|
|
|
|
|
|
static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
|
|
|
|
|
|
|
|
static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
|
|
|
|
const uint8_t *buf, size_t len)
|
|
|
|
{
|
|
|
|
NetHubPort *port;
|
|
|
|
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
if (port == source_port) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_send_packet(&port->nc, buf, len);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
|
|
|
|
const struct iovec *iov, int iovcnt)
|
|
|
|
{
|
|
|
|
NetHubPort *port;
|
2012-07-24 19:35:19 +04:00
|
|
|
ssize_t len = iov_size(iov, iovcnt);
|
2012-07-24 19:35:04 +04:00
|
|
|
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
if (port == source_port) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:19 +04:00
|
|
|
qemu_sendv_packet(&port->nc, iov, iovcnt);
|
2012-07-24 19:35:04 +04:00
|
|
|
}
|
2012-07-24 19:35:19 +04:00
|
|
|
return len;
|
2012-07-24 19:35:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static NetHub *net_hub_new(int id)
|
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
|
|
|
|
hub = g_malloc(sizeof(*hub));
|
|
|
|
hub->id = id;
|
|
|
|
hub->num_ports = 0;
|
|
|
|
QLIST_INIT(&hub->ports);
|
|
|
|
|
|
|
|
QLIST_INSERT_HEAD(&hubs, hub, next);
|
|
|
|
|
|
|
|
return hub;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:19 +04:00
|
|
|
static int net_hub_port_can_receive(NetClientState *nc)
|
|
|
|
{
|
|
|
|
NetHubPort *port;
|
|
|
|
NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
NetHub *hub = src_port->hub;
|
|
|
|
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
if (port == src_port) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:50:30 +04:00
|
|
|
if (qemu_can_send_packet(&port->nc)) {
|
|
|
|
return 1;
|
2012-07-24 19:35:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:50:30 +04:00
|
|
|
return 0;
|
2012-07-24 19:35:19 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_hub_port_receive(NetClientState *nc,
|
2012-07-24 19:35:04 +04:00
|
|
|
const uint8_t *buf, size_t len)
|
|
|
|
{
|
|
|
|
NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
|
|
|
|
return net_hub_receive(port->hub, port, buf, len);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_hub_port_receive_iov(NetClientState *nc,
|
2012-07-24 19:35:04 +04:00
|
|
|
const struct iovec *iov, int iovcnt)
|
|
|
|
{
|
|
|
|
NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
|
|
|
|
return net_hub_receive_iov(port->hub, port, iov, iovcnt);
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void net_hub_port_cleanup(NetClientState *nc)
|
2012-07-24 19:35:04 +04:00
|
|
|
{
|
|
|
|
NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
|
|
|
|
QLIST_REMOVE(port, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NetClientInfo net_hub_port_info = {
|
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_HUBPORT,
|
|
|
|
.size = sizeof(NetHubPort),
|
2012-07-24 19:35:19 +04:00
|
|
|
.can_receive = net_hub_port_can_receive,
|
2012-07-24 19:35:04 +04:00
|
|
|
.receive = net_hub_port_receive,
|
|
|
|
.receive_iov = net_hub_port_receive_iov,
|
|
|
|
.cleanup = net_hub_port_cleanup,
|
|
|
|
};
|
|
|
|
|
|
|
|
static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
|
|
|
|
{
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2012-07-24 19:35:04 +04:00
|
|
|
NetHubPort *port;
|
|
|
|
int id = hub->num_ports++;
|
|
|
|
char default_name[128];
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
snprintf(default_name, sizeof(default_name),
|
|
|
|
"hub%dport%d", hub->id, id);
|
|
|
|
name = default_name;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:08 +04:00
|
|
|
nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name);
|
2012-07-24 19:35:04 +04:00
|
|
|
port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
port->id = id;
|
|
|
|
port->hub = hub;
|
|
|
|
|
|
|
|
QLIST_INSERT_HEAD(&hub->ports, port, next);
|
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a port on a given hub
|
|
|
|
* @name: Net client name or NULL for default name.
|
|
|
|
*
|
|
|
|
* If there is no existing hub with the given id then a new hub is created.
|
|
|
|
*/
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *net_hub_add_port(int hub_id, const char *name)
|
2012-07-24 19:35:04 +04:00
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
NetHubPort *port;
|
|
|
|
|
|
|
|
QLIST_FOREACH(hub, &hubs, next) {
|
|
|
|
if (hub->id == hub_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hub) {
|
|
|
|
hub = net_hub_new(hub_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
port = net_hub_port_new(hub, name);
|
|
|
|
return &port->nc;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:06 +04:00
|
|
|
/**
|
|
|
|
* Find a specific client on a hub
|
|
|
|
*/
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
|
2012-07-24 19:35:06 +04:00
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
NetHubPort *port;
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *peer;
|
2012-07-24 19:35:06 +04:00
|
|
|
|
|
|
|
QLIST_FOREACH(hub, &hubs, next) {
|
|
|
|
if (hub->id == hub_id) {
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
peer = port->nc.peer;
|
|
|
|
|
|
|
|
if (peer && strcmp(peer->name, name) == 0) {
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:09 +04:00
|
|
|
/**
|
|
|
|
* Find a available port on a hub; otherwise create one new port
|
|
|
|
*/
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *net_hub_port_find(int hub_id)
|
2012-07-24 19:35:09 +04:00
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
NetHubPort *port;
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2012-07-24 19:35:09 +04:00
|
|
|
|
|
|
|
QLIST_FOREACH(hub, &hubs, next) {
|
|
|
|
if (hub->id == hub_id) {
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
nc = port->nc.peer;
|
|
|
|
if (!nc) {
|
|
|
|
return &(port->nc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nc = net_hub_add_port(hub_id, NULL);
|
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:04 +04:00
|
|
|
/**
|
|
|
|
* Print hub configuration
|
|
|
|
*/
|
|
|
|
void net_hub_info(Monitor *mon)
|
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
NetHubPort *port;
|
|
|
|
|
|
|
|
QLIST_FOREACH(hub, &hubs, next) {
|
|
|
|
monitor_printf(mon, "hub %d\n", hub->id);
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
2015-02-02 10:06:35 +03:00
|
|
|
monitor_printf(mon, " \\ %s", port->nc.name);
|
2012-07-24 19:35:16 +04:00
|
|
|
if (port->nc.peer) {
|
2015-02-02 10:06:35 +03:00
|
|
|
monitor_printf(mon, ": ");
|
2012-07-24 19:35:16 +04:00
|
|
|
print_net_client(mon, port->nc.peer);
|
2015-02-02 10:06:35 +03:00
|
|
|
} else {
|
|
|
|
monitor_printf(mon, "\n");
|
2012-07-24 19:35:16 +04:00
|
|
|
}
|
2012-07-24 19:35:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the hub id that a client is connected to
|
|
|
|
*
|
2012-12-07 05:43:18 +04:00
|
|
|
* @id: Pointer for hub id output, may be NULL
|
2012-07-24 19:35:04 +04:00
|
|
|
*/
|
2012-07-24 19:35:13 +04:00
|
|
|
int net_hub_id_for_client(NetClientState *nc, int *id)
|
2012-07-24 19:35:04 +04:00
|
|
|
{
|
|
|
|
NetHubPort *port;
|
|
|
|
|
|
|
|
if (nc->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
|
|
|
|
port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
} else if (nc->peer != NULL && nc->peer->info->type ==
|
|
|
|
NET_CLIENT_OPTIONS_KIND_HUBPORT) {
|
|
|
|
port = DO_UPCAST(NetHubPort, nc, nc->peer);
|
|
|
|
} else {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
*id = port->hub->id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_init_hubport(const NetClientOptions *opts, const char *name,
|
2015-05-15 14:58:50 +03:00
|
|
|
NetClientState *peer, Error **errp)
|
2012-07-24 19:35:04 +04:00
|
|
|
{
|
|
|
|
const NetdevHubPortOptions *hubport;
|
|
|
|
|
2015-10-27 01:34:56 +03:00
|
|
|
assert(opts->type == NET_CLIENT_OPTIONS_KIND_HUBPORT);
|
2015-05-15 14:58:49 +03:00
|
|
|
assert(!peer);
|
qapi: Don't special-case simple union wrappers
Simple unions were carrying a special case that hid their 'data'
QMP member from the resulting C struct, via the hack method
QAPISchemaObjectTypeVariant.simple_union_type(). But by using
the work we started by unboxing flat union and alternate
branches, coupled with the ability to visit the members of an
implicit type, we can now expose the simple union's implicit
type in qapi-types.h:
| struct q_obj_ImageInfoSpecificQCow2_wrapper {
| ImageInfoSpecificQCow2 *data;
| };
|
| struct q_obj_ImageInfoSpecificVmdk_wrapper {
| ImageInfoSpecificVmdk *data;
| };
...
| struct ImageInfoSpecific {
| ImageInfoSpecificKind type;
| union { /* union tag is @type */
| void *data;
|- ImageInfoSpecificQCow2 *qcow2;
|- ImageInfoSpecificVmdk *vmdk;
|+ q_obj_ImageInfoSpecificQCow2_wrapper qcow2;
|+ q_obj_ImageInfoSpecificVmdk_wrapper vmdk;
| } u;
| };
Doing this removes asymmetry between QAPI's QMP side and its
C side (both sides now expose 'data'), and means that the
treatment of a simple union as sugar for a flat union is now
equivalent in both languages (previously the two approaches used
a different layer of dereferencing, where the simple union could
be converted to a flat union with equivalent C layout but
different {} on the wire, or to an equivalent QMP wire form
but with different C representation). Using the implicit type
also lets us get rid of the simple_union_type() hack.
Of course, now all clients of simple unions have to adjust from
using su->u.member to using su->u.member.data; while this touches
a number of files in the tree, some earlier cleanup patches
helped minimize the change to the initialization of a temporary
variable rather than every single member access. The generated
qapi-visit.c code is also affected by the layout change:
|@@ -7393,10 +7393,10 @@ void visit_type_ImageInfoSpecific_member
| }
| switch (obj->type) {
| case IMAGE_INFO_SPECIFIC_KIND_QCOW2:
|- visit_type_ImageInfoSpecificQCow2(v, "data", &obj->u.qcow2, &err);
|+ visit_type_q_obj_ImageInfoSpecificQCow2_wrapper_members(v, &obj->u.qcow2, &err);
| break;
| case IMAGE_INFO_SPECIFIC_KIND_VMDK:
|- visit_type_ImageInfoSpecificVmdk(v, "data", &obj->u.vmdk, &err);
|+ visit_type_q_obj_ImageInfoSpecificVmdk_wrapper_members(v, &obj->u.vmdk, &err);
| break;
| default:
| abort();
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1458254921-17042-13-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-03-18 01:48:37 +03:00
|
|
|
hubport = opts->u.hubport.data;
|
2012-07-24 19:35:04 +04:00
|
|
|
|
|
|
|
net_hub_add_port(hubport->hubid, name);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-24 19:35:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Warn if hub configurations are likely wrong
|
|
|
|
*/
|
|
|
|
void net_hub_check_clients(void)
|
|
|
|
{
|
|
|
|
NetHub *hub;
|
|
|
|
NetHubPort *port;
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *peer;
|
2012-07-24 19:35:07 +04:00
|
|
|
|
|
|
|
QLIST_FOREACH(hub, &hubs, next) {
|
|
|
|
int has_nic = 0, has_host_dev = 0;
|
|
|
|
|
|
|
|
QLIST_FOREACH(port, &hub->ports, next) {
|
|
|
|
peer = port->nc.peer;
|
|
|
|
if (!peer) {
|
|
|
|
fprintf(stderr, "Warning: hub port %s has no peer\n",
|
|
|
|
port->nc.name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (peer->info->type) {
|
|
|
|
case NET_CLIENT_OPTIONS_KIND_NIC:
|
|
|
|
has_nic = 1;
|
|
|
|
break;
|
|
|
|
case NET_CLIENT_OPTIONS_KIND_USER:
|
|
|
|
case NET_CLIENT_OPTIONS_KIND_TAP:
|
|
|
|
case NET_CLIENT_OPTIONS_KIND_SOCKET:
|
|
|
|
case NET_CLIENT_OPTIONS_KIND_VDE:
|
2014-06-10 14:02:16 +04:00
|
|
|
case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
|
2012-07-24 19:35:07 +04:00
|
|
|
has_host_dev = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (has_host_dev && !has_nic) {
|
|
|
|
fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
|
|
|
|
}
|
|
|
|
if (has_nic && !has_host_dev) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: vlan %d is not connected to host network\n",
|
|
|
|
hub->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-05 20:53:31 +04:00
|
|
|
|
|
|
|
bool net_hub_flush(NetClientState *nc)
|
|
|
|
{
|
|
|
|
NetHubPort *port;
|
|
|
|
NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
QLIST_FOREACH(port, &source_port->hub->ports, next) {
|
|
|
|
if (port != source_port) {
|
2013-08-02 23:47:08 +04:00
|
|
|
ret += qemu_net_queue_flush(port->nc.incoming_queue);
|
2013-02-05 20:53:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret ? true : false;
|
|
|
|
}
|