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.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "monitor/monitor.h"
22 #include "qemu/error-report.h"
25 * A hub broadcasts incoming packets to all its ports except the source port.
26 * Hubs can be used to provide independent network segments, also confusingly
27 * named the QEMU 'vlan' feature.
30 typedef struct NetHub NetHub
;
32 typedef struct NetHubPort
{
34 QLIST_ENTRY(NetHubPort
) next
;
41 QLIST_ENTRY(NetHub
) next
;
43 QLIST_HEAD(, NetHubPort
) ports
;
46 static QLIST_HEAD(, NetHub
) hubs
= QLIST_HEAD_INITIALIZER(&hubs
);
48 static ssize_t
net_hub_receive(NetHub
*hub
, NetHubPort
*source_port
,
49 const uint8_t *buf
, size_t len
)
53 QLIST_FOREACH(port
, &hub
->ports
, next
) {
54 if (port
== source_port
) {
58 qemu_send_packet(&port
->nc
, buf
, len
);
63 static ssize_t
net_hub_receive_iov(NetHub
*hub
, NetHubPort
*source_port
,
64 const struct iovec
*iov
, int iovcnt
)
67 ssize_t len
= iov_size(iov
, iovcnt
);
69 QLIST_FOREACH(port
, &hub
->ports
, next
) {
70 if (port
== source_port
) {
74 qemu_sendv_packet(&port
->nc
, iov
, iovcnt
);
79 static NetHub
*net_hub_new(int id
)
83 hub
= g_malloc(sizeof(*hub
));
86 QLIST_INIT(&hub
->ports
);
88 QLIST_INSERT_HEAD(&hubs
, hub
, next
);
93 static int net_hub_port_can_receive(NetClientState
*nc
)
96 NetHubPort
*src_port
= DO_UPCAST(NetHubPort
, nc
, nc
);
97 NetHub
*hub
= src_port
->hub
;
99 QLIST_FOREACH(port
, &hub
->ports
, next
) {
100 if (port
== src_port
) {
104 if (qemu_can_send_packet(&port
->nc
)) {
112 static ssize_t
net_hub_port_receive(NetClientState
*nc
,
113 const uint8_t *buf
, size_t len
)
115 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
117 return net_hub_receive(port
->hub
, port
, buf
, len
);
120 static ssize_t
net_hub_port_receive_iov(NetClientState
*nc
,
121 const struct iovec
*iov
, int iovcnt
)
123 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
125 return net_hub_receive_iov(port
->hub
, port
, iov
, iovcnt
);
128 static void net_hub_port_cleanup(NetClientState
*nc
)
130 NetHubPort
*port
= DO_UPCAST(NetHubPort
, nc
, nc
);
132 QLIST_REMOVE(port
, next
);
135 static NetClientInfo net_hub_port_info
= {
136 .type
= NET_CLIENT_DRIVER_HUBPORT
,
137 .size
= sizeof(NetHubPort
),
138 .can_receive
= net_hub_port_can_receive
,
139 .receive
= net_hub_port_receive
,
140 .receive_iov
= net_hub_port_receive_iov
,
141 .cleanup
= net_hub_port_cleanup
,
144 static NetHubPort
*net_hub_port_new(NetHub
*hub
, const char *name
,
145 NetClientState
*hubpeer
)
149 int id
= hub
->num_ports
++;
150 char default_name
[128];
153 snprintf(default_name
, sizeof(default_name
),
154 "hub%dport%d", hub
->id
, id
);
158 nc
= qemu_new_net_client(&net_hub_port_info
, hubpeer
, "hub", name
);
159 port
= DO_UPCAST(NetHubPort
, nc
, nc
);
163 QLIST_INSERT_HEAD(&hub
->ports
, port
, next
);
169 * Create a port on a given hub
170 * @hub_id: Number of the hub
171 * @name: Net client name or NULL for default name.
172 * @hubpeer: Peer to use (if "netdev=id" has been specified)
174 * If there is no existing hub with the given id then a new hub is created.
176 NetClientState
*net_hub_add_port(int hub_id
, const char *name
,
177 NetClientState
*hubpeer
)
182 QLIST_FOREACH(hub
, &hubs
, next
) {
183 if (hub
->id
== hub_id
) {
189 hub
= net_hub_new(hub_id
);
192 port
= net_hub_port_new(hub
, name
, hubpeer
);
197 * Find a specific client on a hub
199 NetClientState
*net_hub_find_client_by_name(int hub_id
, const char *name
)
203 NetClientState
*peer
;
205 QLIST_FOREACH(hub
, &hubs
, next
) {
206 if (hub
->id
== hub_id
) {
207 QLIST_FOREACH(port
, &hub
->ports
, next
) {
208 peer
= port
->nc
.peer
;
210 if (peer
&& strcmp(peer
->name
, name
) == 0) {
220 * Find a available port on a hub; otherwise create one new port
222 NetClientState
*net_hub_port_find(int hub_id
)
228 QLIST_FOREACH(hub
, &hubs
, next
) {
229 if (hub
->id
== hub_id
) {
230 QLIST_FOREACH(port
, &hub
->ports
, next
) {
240 nc
= net_hub_add_port(hub_id
, NULL
, NULL
);
245 * Print hub configuration
247 void net_hub_info(Monitor
*mon
)
252 QLIST_FOREACH(hub
, &hubs
, next
) {
253 monitor_printf(mon
, "hub %d\n", hub
->id
);
254 QLIST_FOREACH(port
, &hub
->ports
, next
) {
255 monitor_printf(mon
, " \\ %s", port
->nc
.name
);
257 monitor_printf(mon
, ": ");
258 print_net_client(mon
, port
->nc
.peer
);
260 monitor_printf(mon
, "\n");
267 * Get the hub id that a client is connected to
269 * @id: Pointer for hub id output, may be NULL
271 int net_hub_id_for_client(NetClientState
*nc
, int *id
)
275 if (nc
->info
->type
== NET_CLIENT_DRIVER_HUBPORT
) {
276 port
= DO_UPCAST(NetHubPort
, nc
, nc
);
277 } else if (nc
->peer
!= NULL
&& nc
->peer
->info
->type
==
278 NET_CLIENT_DRIVER_HUBPORT
) {
279 port
= DO_UPCAST(NetHubPort
, nc
, nc
->peer
);
290 int net_init_hubport(const Netdev
*netdev
, const char *name
,
291 NetClientState
*peer
, Error
**errp
)
293 const NetdevHubPortOptions
*hubport
;
294 NetClientState
*hubpeer
= NULL
;
296 assert(netdev
->type
== NET_CLIENT_DRIVER_HUBPORT
);
298 hubport
= &netdev
->u
.hubport
;
300 if (hubport
->has_netdev
) {
301 hubpeer
= qemu_find_netdev(hubport
->netdev
);
303 error_setg(errp
, "netdev '%s' not found", hubport
->netdev
);
308 net_hub_add_port(hubport
->hubid
, name
, hubpeer
);
314 * Warn if hub configurations are likely wrong
316 void net_hub_check_clients(void)
320 NetClientState
*peer
;
322 QLIST_FOREACH(hub
, &hubs
, next
) {
323 int has_nic
= 0, has_host_dev
= 0;
325 QLIST_FOREACH(port
, &hub
->ports
, next
) {
326 peer
= port
->nc
.peer
;
328 warn_report("hub port %s has no peer", port
->nc
.name
);
332 switch (peer
->info
->type
) {
333 case NET_CLIENT_DRIVER_NIC
:
336 case NET_CLIENT_DRIVER_USER
:
337 case NET_CLIENT_DRIVER_TAP
:
338 case NET_CLIENT_DRIVER_SOCKET
:
339 case NET_CLIENT_DRIVER_VDE
:
340 case NET_CLIENT_DRIVER_VHOST_USER
:
347 if (has_host_dev
&& !has_nic
) {
348 warn_report("vlan %d with no nics", hub
->id
);
350 if (has_nic
&& !has_host_dev
) {
351 warn_report("vlan %d is not connected to host network", hub
->id
);
356 bool net_hub_flush(NetClientState
*nc
)
359 NetHubPort
*source_port
= DO_UPCAST(NetHubPort
, nc
, nc
);
362 QLIST_FOREACH(port
, &source_port
->hub
->ports
, next
) {
363 if (port
!= source_port
) {
364 ret
+= qemu_net_queue_flush(port
->nc
.incoming_queue
);
367 return ret
? true : false;