blockdev: preserve readonly and snapshot states across media changes
[qemu/ar7.git] / net / hub.c
blob650a8b4a40188cf60b994f06e8de2980a9b6c3f3
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 "monitor.h"
16 #include "net.h"
17 #include "hub.h"
18 #include "iov.h"
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 {
29 NetClientState nc;
30 QLIST_ENTRY(NetHubPort) next;
31 NetHub *hub;
32 int id;
33 } NetHubPort;
35 struct NetHub {
36 int id;
37 QLIST_ENTRY(NetHub) next;
38 int num_ports;
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)
47 NetHubPort *port;
49 QLIST_FOREACH(port, &hub->ports, next) {
50 if (port == source_port) {
51 continue;
54 qemu_send_packet(&port->nc, buf, len);
56 return len;
59 static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
60 const struct iovec *iov, int iovcnt)
62 NetHubPort *port;
63 ssize_t len = iov_size(iov, iovcnt);
65 QLIST_FOREACH(port, &hub->ports, next) {
66 if (port == source_port) {
67 continue;
70 qemu_sendv_packet(&port->nc, iov, iovcnt);
72 return len;
75 static NetHub *net_hub_new(int id)
77 NetHub *hub;
79 hub = g_malloc(sizeof(*hub));
80 hub->id = id;
81 hub->num_ports = 0;
82 QLIST_INIT(&hub->ports);
84 QLIST_INSERT_HEAD(&hubs, hub, next);
86 return hub;
89 static int net_hub_port_can_receive(NetClientState *nc)
91 NetHubPort *port;
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) {
97 continue;
100 if (qemu_can_send_packet(&port->nc)) {
101 return 1;
105 return 0;
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)
142 NetClientState *nc;
143 NetHubPort *port;
144 int id = hub->num_ports++;
145 char default_name[128];
147 if (!name) {
148 snprintf(default_name, sizeof(default_name),
149 "hub%dport%d", hub->id, id);
150 name = default_name;
153 nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name);
154 port = DO_UPCAST(NetHubPort, nc, nc);
155 port->id = id;
156 port->hub = hub;
158 QLIST_INSERT_HEAD(&hub->ports, port, next);
160 return port;
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)
171 NetHub *hub;
172 NetHubPort *port;
174 QLIST_FOREACH(hub, &hubs, next) {
175 if (hub->id == hub_id) {
176 break;
180 if (!hub) {
181 hub = net_hub_new(hub_id);
184 port = net_hub_port_new(hub, name);
185 return &port->nc;
189 * Find a specific client on a hub
191 NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
193 NetHub *hub;
194 NetHubPort *port;
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) {
203 return peer;
208 return NULL;
212 * Find a available port on a hub; otherwise create one new port
214 NetClientState *net_hub_port_find(int hub_id)
216 NetHub *hub;
217 NetHubPort *port;
218 NetClientState *nc;
220 QLIST_FOREACH(hub, &hubs, next) {
221 if (hub->id == hub_id) {
222 QLIST_FOREACH(port, &hub->ports, next) {
223 nc = port->nc.peer;
224 if (!nc) {
225 return &(port->nc);
228 break;
232 nc = net_hub_add_port(hub_id, NULL);
233 return nc;
237 * Print hub configuration
239 void net_hub_info(Monitor *mon)
241 NetHub *hub;
242 NetHubPort *port;
244 QLIST_FOREACH(hub, &hubs, next) {
245 monitor_printf(mon, "hub %d\n", hub->id);
246 QLIST_FOREACH(port, &hub->ports, next) {
247 if (port->nc.peer) {
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)
262 NetHubPort *port;
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);
269 } else {
270 return -ENOENT;
273 if (id) {
274 *id = port->hub->id;
276 return 0;
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 */
288 if (peer) {
289 return -EINVAL;
292 net_hub_add_port(hubport->hubid, name);
293 return 0;
297 * Warn if hub configurations are likely wrong
299 void net_hub_check_clients(void)
301 NetHub *hub;
302 NetHubPort *port;
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;
310 if (!peer) {
311 fprintf(stderr, "Warning: hub port %s has no peer\n",
312 port->nc.name);
313 continue;
316 switch (peer->info->type) {
317 case NET_CLIENT_OPTIONS_KIND_NIC:
318 has_nic = 1;
319 break;
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:
324 has_host_dev = 1;
325 break;
326 default:
327 break;
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) {
334 fprintf(stderr,
335 "Warning: vlan %d is not connected to host network\n",
336 hub->id);