4 * Copyright IBM, Corp. 2012
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
21 * A hub broadcasts incoming packets to all its ports except the source port.
22 * Hubs can be used to provide independent network segments, also confusingly
23 * named the QEMU 'vlan' feature.
26 typedef struct NetHub NetHub
;
28 typedef struct NetHubPort
{
30 QLIST_ENTRY(NetHubPort
) next
;
37 QLIST_ENTRY(NetHub
) next
;
39 QLIST_HEAD(, NetHubPort
) ports
;
42 static QLIST_HEAD(, NetHub
) hubs
= QLIST_HEAD_INITIALIZER(&hubs
);
44 static ssize_t
net_hub_receive(NetHub
*hub
, NetHubPort
*source_port
,
45 const uint8_t *buf
, size_t len
)
49 QLIST_FOREACH(port
, &hub
->ports
, next
) {
50 if (port
== source_port
) {
54 qemu_send_packet(&port
->nc
, buf
, len
);
59 static ssize_t
net_hub_receive_iov(NetHub
*hub
, NetHubPort
*source_port
,
60 const struct iovec
*iov
, int iovcnt
)
63 ssize_t len
= iov_size(iov
, iovcnt
);
65 QLIST_FOREACH(port
, &hub
->ports
, next
) {
66 if (port
== source_port
) {
70 qemu_sendv_packet(&port
->nc
, iov
, iovcnt
);
75 static NetHub
*net_hub_new(int id
)
79 hub
= g_malloc(sizeof(*hub
));
82 QLIST_INIT(&hub
->ports
);
84 QLIST_INSERT_HEAD(&hubs
, hub
, next
);
89 static int net_hub_port_can_receive(NetClientState
*nc
)
92 NetHubPort
*src_port
= DO_UPCAST(NetHubPort
, nc
, nc
);
93 NetHub
*hub
= src_port
->hub
;
95 QLIST_FOREACH(port
, &hub
->ports
, next
) {
96 if (port
== src_port
) {
100 if (qemu_can_send_packet(&port
->nc
)) {
108 static ssize_t
net_hub_port_receive(NetClientState
*nc
,
109 const uint8_t *buf
, size_t len
)
111 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
113 return net_hub_receive(port
->hub
, port
, buf
, len
);
116 static ssize_t
net_hub_port_receive_iov(NetClientState
*nc
,
117 const struct iovec
*iov
, int iovcnt
)
119 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
121 return net_hub_receive_iov(port
->hub
, port
, iov
, iovcnt
);
124 static void net_hub_port_cleanup(NetClientState
*nc
)
126 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
128 QLIST_REMOVE(port
, next
);
131 static NetClientInfo net_hub_port_info
= {
132 .type
= NET_CLIENT_OPTIONS_KIND_HUBPORT
,
133 .size
= sizeof(NetHubPort
),
134 .can_receive
= net_hub_port_can_receive
,
135 .receive
= net_hub_port_receive
,
136 .receive_iov
= net_hub_port_receive_iov
,
137 .cleanup
= net_hub_port_cleanup
,
140 static NetHubPort
*net_hub_port_new(NetHub
*hub
, const char *name
)
144 int id
= hub
->num_ports
++;
145 char default_name
[128];
148 snprintf(default_name
, sizeof(default_name
),
149 "hub%dport%d", hub
->id
, id
);
153 nc
= qemu_new_net_client(&net_hub_port_info
, NULL
, "hub", name
);
154 port
= DO_UPCAST(NetHubPort
, nc
, nc
);
158 QLIST_INSERT_HEAD(&hub
->ports
, port
, next
);
164 * Create a port on a given hub
165 * @name: Net client name or NULL for default name.
167 * If there is no existing hub with the given id then a new hub is created.
169 NetClientState
*net_hub_add_port(int hub_id
, const char *name
)
174 QLIST_FOREACH(hub
, &hubs
, next
) {
175 if (hub
->id
== hub_id
) {
181 hub
= net_hub_new(hub_id
);
184 port
= net_hub_port_new(hub
, name
);
189 * Find a specific client on a hub
191 NetClientState
*net_hub_find_client_by_name(int hub_id
, const char *name
)
195 NetClientState
*peer
;
197 QLIST_FOREACH(hub
, &hubs
, next
) {
198 if (hub
->id
== hub_id
) {
199 QLIST_FOREACH(port
, &hub
->ports
, next
) {
200 peer
= port
->nc
.peer
;
202 if (peer
&& strcmp(peer
->name
, name
) == 0) {
212 * Find a available port on a hub; otherwise create one new port
214 NetClientState
*net_hub_port_find(int hub_id
)
220 QLIST_FOREACH(hub
, &hubs
, next
) {
221 if (hub
->id
== hub_id
) {
222 QLIST_FOREACH(port
, &hub
->ports
, next
) {
232 nc
= net_hub_add_port(hub_id
, NULL
);
237 * Print hub configuration
239 void net_hub_info(Monitor
*mon
)
244 QLIST_FOREACH(hub
, &hubs
, next
) {
245 monitor_printf(mon
, "hub %d\n", hub
->id
);
246 QLIST_FOREACH(port
, &hub
->ports
, next
) {
248 monitor_printf(mon
, " \\ ");
249 print_net_client(mon
, port
->nc
.peer
);
256 * Get the hub id that a client is connected to
258 * @id Pointer for hub id output, may be NULL
260 int net_hub_id_for_client(NetClientState
*nc
, int *id
)
264 if (nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
265 port
= DO_UPCAST(NetHubPort
, nc
, nc
);
266 } else if (nc
->peer
!= NULL
&& nc
->peer
->info
->type
==
267 NET_CLIENT_OPTIONS_KIND_HUBPORT
) {
268 port
= DO_UPCAST(NetHubPort
, nc
, nc
->peer
);
279 int net_init_hubport(const NetClientOptions
*opts
, const char *name
,
280 NetClientState
*peer
)
282 const NetdevHubPortOptions
*hubport
;
284 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_HUBPORT
);
285 hubport
= opts
->hubport
;
287 /* Treat hub port like a backend, NIC must be the one to peer */
292 net_hub_add_port(hubport
->hubid
, name
);
297 * Warn if hub configurations are likely wrong
299 void net_hub_check_clients(void)
303 NetClientState
*peer
;
305 QLIST_FOREACH(hub
, &hubs
, next
) {
306 int has_nic
= 0, has_host_dev
= 0;
308 QLIST_FOREACH(port
, &hub
->ports
, next
) {
309 peer
= port
->nc
.peer
;
311 fprintf(stderr
, "Warning: hub port %s has no peer\n",
316 switch (peer
->info
->type
) {
317 case NET_CLIENT_OPTIONS_KIND_NIC
:
320 case NET_CLIENT_OPTIONS_KIND_USER
:
321 case NET_CLIENT_OPTIONS_KIND_TAP
:
322 case NET_CLIENT_OPTIONS_KIND_SOCKET
:
323 case NET_CLIENT_OPTIONS_KIND_VDE
:
330 if (has_host_dev
&& !has_nic
) {
331 fprintf(stderr
, "Warning: vlan %d with no nics\n", hub
->id
);
333 if (has_nic
&& !has_host_dev
) {
335 "Warning: vlan %d is not connected to host network\n",