q35: ioapic: add support for emulated IOAPIC IR
[qemu/cris-port.git] / net / hub.c
blob6d90c6ee67dfe64d65746554bd5b4a1a8e069207
1 /*
2 * Hub net client
4 * Copyright IBM, Corp. 2012
6 * Authors:
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 "monitor/monitor.h"
17 #include "net/net.h"
18 #include "clients.h"
19 #include "hub.h"
20 #include "qemu/iov.h"
23 * A hub broadcasts incoming packets to all its ports except the source port.
24 * Hubs can be used to provide independent network segments, also confusingly
25 * named the QEMU 'vlan' feature.
28 typedef struct NetHub NetHub;
30 typedef struct NetHubPort {
31 NetClientState nc;
32 QLIST_ENTRY(NetHubPort) next;
33 NetHub *hub;
34 int id;
35 } NetHubPort;
37 struct NetHub {
38 int id;
39 QLIST_ENTRY(NetHub) next;
40 int num_ports;
41 QLIST_HEAD(, NetHubPort) ports;
44 static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
46 static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
47 const uint8_t *buf, size_t len)
49 NetHubPort *port;
51 QLIST_FOREACH(port, &hub->ports, next) {
52 if (port == source_port) {
53 continue;
56 qemu_send_packet(&port->nc, buf, len);
58 return len;
61 static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
62 const struct iovec *iov, int iovcnt)
64 NetHubPort *port;
65 ssize_t len = iov_size(iov, iovcnt);
67 QLIST_FOREACH(port, &hub->ports, next) {
68 if (port == source_port) {
69 continue;
72 qemu_sendv_packet(&port->nc, iov, iovcnt);
74 return len;
77 static NetHub *net_hub_new(int id)
79 NetHub *hub;
81 hub = g_malloc(sizeof(*hub));
82 hub->id = id;
83 hub->num_ports = 0;
84 QLIST_INIT(&hub->ports);
86 QLIST_INSERT_HEAD(&hubs, hub, next);
88 return hub;
91 static int net_hub_port_can_receive(NetClientState *nc)
93 NetHubPort *port;
94 NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
95 NetHub *hub = src_port->hub;
97 QLIST_FOREACH(port, &hub->ports, next) {
98 if (port == src_port) {
99 continue;
102 if (qemu_can_send_packet(&port->nc)) {
103 return 1;
107 return 0;
110 static ssize_t net_hub_port_receive(NetClientState *nc,
111 const uint8_t *buf, size_t len)
113 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
115 return net_hub_receive(port->hub, port, buf, len);
118 static ssize_t net_hub_port_receive_iov(NetClientState *nc,
119 const struct iovec *iov, int iovcnt)
121 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
123 return net_hub_receive_iov(port->hub, port, iov, iovcnt);
126 static void net_hub_port_cleanup(NetClientState *nc)
128 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
130 QLIST_REMOVE(port, next);
133 static NetClientInfo net_hub_port_info = {
134 .type = NET_CLIENT_OPTIONS_KIND_HUBPORT,
135 .size = sizeof(NetHubPort),
136 .can_receive = net_hub_port_can_receive,
137 .receive = net_hub_port_receive,
138 .receive_iov = net_hub_port_receive_iov,
139 .cleanup = net_hub_port_cleanup,
142 static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
144 NetClientState *nc;
145 NetHubPort *port;
146 int id = hub->num_ports++;
147 char default_name[128];
149 if (!name) {
150 snprintf(default_name, sizeof(default_name),
151 "hub%dport%d", hub->id, id);
152 name = default_name;
155 nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name);
156 port = DO_UPCAST(NetHubPort, nc, nc);
157 port->id = id;
158 port->hub = hub;
160 QLIST_INSERT_HEAD(&hub->ports, port, next);
162 return port;
166 * Create a port on a given hub
167 * @name: Net client name or NULL for default name.
169 * If there is no existing hub with the given id then a new hub is created.
171 NetClientState *net_hub_add_port(int hub_id, const char *name)
173 NetHub *hub;
174 NetHubPort *port;
176 QLIST_FOREACH(hub, &hubs, next) {
177 if (hub->id == hub_id) {
178 break;
182 if (!hub) {
183 hub = net_hub_new(hub_id);
186 port = net_hub_port_new(hub, name);
187 return &port->nc;
191 * Find a specific client on a hub
193 NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
195 NetHub *hub;
196 NetHubPort *port;
197 NetClientState *peer;
199 QLIST_FOREACH(hub, &hubs, next) {
200 if (hub->id == hub_id) {
201 QLIST_FOREACH(port, &hub->ports, next) {
202 peer = port->nc.peer;
204 if (peer && strcmp(peer->name, name) == 0) {
205 return peer;
210 return NULL;
214 * Find a available port on a hub; otherwise create one new port
216 NetClientState *net_hub_port_find(int hub_id)
218 NetHub *hub;
219 NetHubPort *port;
220 NetClientState *nc;
222 QLIST_FOREACH(hub, &hubs, next) {
223 if (hub->id == hub_id) {
224 QLIST_FOREACH(port, &hub->ports, next) {
225 nc = port->nc.peer;
226 if (!nc) {
227 return &(port->nc);
230 break;
234 nc = net_hub_add_port(hub_id, NULL);
235 return nc;
239 * Print hub configuration
241 void net_hub_info(Monitor *mon)
243 NetHub *hub;
244 NetHubPort *port;
246 QLIST_FOREACH(hub, &hubs, next) {
247 monitor_printf(mon, "hub %d\n", hub->id);
248 QLIST_FOREACH(port, &hub->ports, next) {
249 monitor_printf(mon, " \\ %s", port->nc.name);
250 if (port->nc.peer) {
251 monitor_printf(mon, ": ");
252 print_net_client(mon, port->nc.peer);
253 } else {
254 monitor_printf(mon, "\n");
261 * Get the hub id that a client is connected to
263 * @id: Pointer for hub id output, may be NULL
265 int net_hub_id_for_client(NetClientState *nc, int *id)
267 NetHubPort *port;
269 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
270 port = DO_UPCAST(NetHubPort, nc, nc);
271 } else if (nc->peer != NULL && nc->peer->info->type ==
272 NET_CLIENT_OPTIONS_KIND_HUBPORT) {
273 port = DO_UPCAST(NetHubPort, nc, nc->peer);
274 } else {
275 return -ENOENT;
278 if (id) {
279 *id = port->hub->id;
281 return 0;
284 int net_init_hubport(const NetClientOptions *opts, const char *name,
285 NetClientState *peer, Error **errp)
287 const NetdevHubPortOptions *hubport;
289 assert(opts->type == NET_CLIENT_OPTIONS_KIND_HUBPORT);
290 assert(!peer);
291 hubport = opts->u.hubport.data;
293 net_hub_add_port(hubport->hubid, name);
294 return 0;
298 * Warn if hub configurations are likely wrong
300 void net_hub_check_clients(void)
302 NetHub *hub;
303 NetHubPort *port;
304 NetClientState *peer;
306 QLIST_FOREACH(hub, &hubs, next) {
307 int has_nic = 0, has_host_dev = 0;
309 QLIST_FOREACH(port, &hub->ports, next) {
310 peer = port->nc.peer;
311 if (!peer) {
312 fprintf(stderr, "Warning: hub port %s has no peer\n",
313 port->nc.name);
314 continue;
317 switch (peer->info->type) {
318 case NET_CLIENT_OPTIONS_KIND_NIC:
319 has_nic = 1;
320 break;
321 case NET_CLIENT_OPTIONS_KIND_USER:
322 case NET_CLIENT_OPTIONS_KIND_TAP:
323 case NET_CLIENT_OPTIONS_KIND_SOCKET:
324 case NET_CLIENT_OPTIONS_KIND_VDE:
325 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
326 has_host_dev = 1;
327 break;
328 default:
329 break;
332 if (has_host_dev && !has_nic) {
333 fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
335 if (has_nic && !has_host_dev) {
336 fprintf(stderr,
337 "Warning: vlan %d is not connected to host network\n",
338 hub->id);
343 bool net_hub_flush(NetClientState *nc)
345 NetHubPort *port;
346 NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
347 int ret = 0;
349 QLIST_FOREACH(port, &source_port->hub->ports, next) {
350 if (port != source_port) {
351 ret += qemu_net_queue_flush(port->nc.incoming_queue);
354 return ret ? true : false;