4 * Copyright IBM, Corp. 2012
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.
20 * A hub broadcasts incoming packets to all its ports except the source port.
21 * Hubs can be used to provide independent network segments, also confusingly
22 * named the QEMU 'vlan' feature.
25 typedef struct NetHub NetHub;
27 typedef struct NetHubPort {
29 QLIST_ENTRY(NetHubPort) next;
36 QLIST_ENTRY(NetHub) next;
38 QLIST_HEAD(, NetHubPort) ports;
41 static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
43 static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
44 const uint8_t *buf, size_t len)
48 QLIST_FOREACH(port, &hub->ports, next) {
49 if (port == source_port) {
53 qemu_send_packet(&port->nc, buf, len);
58 static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
59 const struct iovec *iov, int iovcnt)
64 QLIST_FOREACH(port, &hub->ports, next) {
65 if (port == source_port) {
69 ret = qemu_sendv_packet(&port->nc, iov, iovcnt);
74 static NetHub *net_hub_new(int id)
78 hub = g_malloc(sizeof(*hub));
81 QLIST_INIT(&hub->ports);
83 QLIST_INSERT_HEAD(&hubs, hub, next);
88 static ssize_t net_hub_port_receive(NetClientState *nc,
89 const uint8_t *buf, size_t len)
91 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
93 return net_hub_receive(port->hub, port, buf, len);
96 static ssize_t net_hub_port_receive_iov(NetClientState *nc,
97 const struct iovec *iov, int iovcnt)
99 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
101 return net_hub_receive_iov(port->hub, port, iov, iovcnt);
104 static void net_hub_port_cleanup(NetClientState *nc)
106 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
108 QLIST_REMOVE(port, next);
111 static NetClientInfo net_hub_port_info = {
112 .type = NET_CLIENT_OPTIONS_KIND_HUBPORT,
113 .size = sizeof(NetHubPort),
114 .receive = net_hub_port_receive,
115 .receive_iov = net_hub_port_receive_iov,
116 .cleanup = net_hub_port_cleanup,
119 static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
123 int id = hub->num_ports++;
124 char default_name[128];
127 snprintf(default_name, sizeof(default_name),
128 "hub%dport%d", hub->id, id);
132 nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name);
133 port = DO_UPCAST(NetHubPort, nc, nc);
137 QLIST_INSERT_HEAD(&hub->ports, port, next);
143 * Create a port on a given hub
144 * @name: Net client name or NULL for default name.
146 * If there is no existing hub with the given id then a new hub is created.
148 NetClientState *net_hub_add_port(int hub_id, const char *name)
153 QLIST_FOREACH(hub, &hubs, next) {
154 if (hub->id == hub_id) {
160 hub = net_hub_new(hub_id);
163 port = net_hub_port_new(hub, name);
168 * Find a specific client on a hub
170 NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
174 NetClientState *peer;
176 QLIST_FOREACH(hub, &hubs, next) {
177 if (hub->id == hub_id) {
178 QLIST_FOREACH(port, &hub->ports, next) {
179 peer = port->nc.peer;
181 if (peer && strcmp(peer->name, name) == 0) {
191 * Find a available port on a hub; otherwise create one new port
193 NetClientState *net_hub_port_find(int hub_id)
199 QLIST_FOREACH(hub, &hubs, next) {
200 if (hub->id == hub_id) {
201 QLIST_FOREACH(port, &hub->ports, next) {
211 nc = net_hub_add_port(hub_id, NULL);
216 * Print hub configuration
218 void net_hub_info(Monitor *mon)
223 QLIST_FOREACH(hub, &hubs, next) {
224 monitor_printf(mon, "hub %d\n", hub->id);
225 QLIST_FOREACH(port, &hub->ports, next) {
227 monitor_printf(mon, " \\ ");
228 print_net_client(mon, port->nc.peer);
235 * Get the hub id that a client is connected to
237 * @id Pointer for hub id output, may be NULL
239 int net_hub_id_for_client(NetClientState *nc, int *id)
243 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
244 port = DO_UPCAST(NetHubPort, nc, nc);
245 } else if (nc->peer != NULL && nc->peer->info->type ==
246 NET_CLIENT_OPTIONS_KIND_HUBPORT) {
247 port = DO_UPCAST(NetHubPort, nc, nc->peer);
258 int net_init_hubport(const NetClientOptions *opts, const char *name,
259 NetClientState *peer)
261 const NetdevHubPortOptions *hubport;
263 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT);
264 hubport = opts->hubport;
266 /* Treat hub port like a backend, NIC must be the one to peer */
271 net_hub_add_port(hubport->hubid, name);
276 * Warn if hub configurations are likely wrong
278 void net_hub_check_clients(void)
282 NetClientState *peer;
284 QLIST_FOREACH(hub, &hubs, next) {
285 int has_nic = 0, has_host_dev = 0;
287 QLIST_FOREACH(port, &hub->ports, next) {
288 peer = port->nc.peer;
290 fprintf(stderr, "Warning: hub port %s has no peer\n",
295 switch (peer->info->type) {
296 case NET_CLIENT_OPTIONS_KIND_NIC:
299 case NET_CLIENT_OPTIONS_KIND_USER:
300 case NET_CLIENT_OPTIONS_KIND_TAP:
301 case NET_CLIENT_OPTIONS_KIND_SOCKET:
302 case NET_CLIENT_OPTIONS_KIND_VDE:
309 if (has_host_dev && !has_nic) {
310 fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
312 if (has_nic && !has_host_dev) {
314 "Warning: vlan %d is not connected to host network\n",