]>
Commit | Line | Data |
---|---|---|
f6c874e3 SH |
1 | /* |
2 | * Hub net client | |
3 | * | |
4 | * Copyright IBM, Corp. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
8 | * Zhi Yong Wu <[email protected]> | |
9 | * | |
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. | |
12 | * | |
13 | */ | |
14 | ||
2744d920 | 15 | #include "qemu/osdep.h" |
83c9089e | 16 | #include "monitor/monitor.h" |
1422e32d | 17 | #include "net/net.h" |
a245fc18 | 18 | #include "clients.h" |
f6c874e3 | 19 | #include "hub.h" |
1de7afc9 | 20 | #include "qemu/iov.h" |
2ab4b135 | 21 | #include "qemu/error-report.h" |
f6c874e3 SH |
22 | |
23 | /* | |
24 | * A hub broadcasts incoming packets to all its ports except the source port. | |
25 | * Hubs can be used to provide independent network segments, also confusingly | |
26 | * named the QEMU 'vlan' feature. | |
27 | */ | |
28 | ||
29 | typedef struct NetHub NetHub; | |
30 | ||
31 | typedef struct NetHubPort { | |
4e68f7a0 | 32 | NetClientState nc; |
f6c874e3 SH |
33 | QLIST_ENTRY(NetHubPort) next; |
34 | NetHub *hub; | |
35 | int id; | |
36 | } NetHubPort; | |
37 | ||
38 | struct NetHub { | |
39 | int id; | |
40 | QLIST_ENTRY(NetHub) next; | |
41 | int num_ports; | |
42 | QLIST_HEAD(, NetHubPort) ports; | |
43 | }; | |
44 | ||
45 | static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs); | |
46 | ||
47 | static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, | |
48 | const uint8_t *buf, size_t len) | |
49 | { | |
50 | NetHubPort *port; | |
51 | ||
52 | QLIST_FOREACH(port, &hub->ports, next) { | |
53 | if (port == source_port) { | |
54 | continue; | |
55 | } | |
56 | ||
57 | qemu_send_packet(&port->nc, buf, len); | |
58 | } | |
59 | return len; | |
60 | } | |
61 | ||
62 | static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, | |
63 | const struct iovec *iov, int iovcnt) | |
64 | { | |
65 | NetHubPort *port; | |
52a3cb86 | 66 | ssize_t len = iov_size(iov, iovcnt); |
f6c874e3 SH |
67 | |
68 | QLIST_FOREACH(port, &hub->ports, next) { | |
69 | if (port == source_port) { | |
70 | continue; | |
71 | } | |
72 | ||
52a3cb86 | 73 | qemu_sendv_packet(&port->nc, iov, iovcnt); |
f6c874e3 | 74 | } |
52a3cb86 | 75 | return len; |
f6c874e3 SH |
76 | } |
77 | ||
78 | static NetHub *net_hub_new(int id) | |
79 | { | |
80 | NetHub *hub; | |
81 | ||
82 | hub = g_malloc(sizeof(*hub)); | |
83 | hub->id = id; | |
84 | hub->num_ports = 0; | |
85 | QLIST_INIT(&hub->ports); | |
86 | ||
87 | QLIST_INSERT_HEAD(&hubs, hub, next); | |
88 | ||
89 | return hub; | |
90 | } | |
91 | ||
52a3cb86 ZYW |
92 | static int net_hub_port_can_receive(NetClientState *nc) |
93 | { | |
94 | NetHubPort *port; | |
95 | NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc); | |
96 | NetHub *hub = src_port->hub; | |
97 | ||
98 | QLIST_FOREACH(port, &hub->ports, next) { | |
99 | if (port == src_port) { | |
100 | continue; | |
101 | } | |
102 | ||
61518a74 SH |
103 | if (qemu_can_send_packet(&port->nc)) { |
104 | return 1; | |
52a3cb86 ZYW |
105 | } |
106 | } | |
107 | ||
61518a74 | 108 | return 0; |
52a3cb86 ZYW |
109 | } |
110 | ||
4e68f7a0 | 111 | static ssize_t net_hub_port_receive(NetClientState *nc, |
f6c874e3 SH |
112 | const uint8_t *buf, size_t len) |
113 | { | |
114 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
115 | ||
116 | return net_hub_receive(port->hub, port, buf, len); | |
117 | } | |
118 | ||
4e68f7a0 | 119 | static ssize_t net_hub_port_receive_iov(NetClientState *nc, |
f6c874e3 SH |
120 | const struct iovec *iov, int iovcnt) |
121 | { | |
122 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
123 | ||
124 | return net_hub_receive_iov(port->hub, port, iov, iovcnt); | |
125 | } | |
126 | ||
4e68f7a0 | 127 | static void net_hub_port_cleanup(NetClientState *nc) |
f6c874e3 SH |
128 | { |
129 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
130 | ||
131 | QLIST_REMOVE(port, next); | |
132 | } | |
133 | ||
134 | static NetClientInfo net_hub_port_info = { | |
f394b2e2 | 135 | .type = NET_CLIENT_DRIVER_HUBPORT, |
f6c874e3 | 136 | .size = sizeof(NetHubPort), |
52a3cb86 | 137 | .can_receive = net_hub_port_can_receive, |
f6c874e3 SH |
138 | .receive = net_hub_port_receive, |
139 | .receive_iov = net_hub_port_receive_iov, | |
140 | .cleanup = net_hub_port_cleanup, | |
141 | }; | |
142 | ||
143 | static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) | |
144 | { | |
4e68f7a0 | 145 | NetClientState *nc; |
f6c874e3 SH |
146 | NetHubPort *port; |
147 | int id = hub->num_ports++; | |
148 | char default_name[128]; | |
149 | ||
150 | if (!name) { | |
151 | snprintf(default_name, sizeof(default_name), | |
152 | "hub%dport%d", hub->id, id); | |
153 | name = default_name; | |
154 | } | |
155 | ||
ab5f3f84 | 156 | nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name); |
f6c874e3 SH |
157 | port = DO_UPCAST(NetHubPort, nc, nc); |
158 | port->id = id; | |
159 | port->hub = hub; | |
160 | ||
161 | QLIST_INSERT_HEAD(&hub->ports, port, next); | |
162 | ||
163 | return port; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Create a port on a given hub | |
168 | * @name: Net client name or NULL for default name. | |
169 | * | |
170 | * If there is no existing hub with the given id then a new hub is created. | |
171 | */ | |
4e68f7a0 | 172 | NetClientState *net_hub_add_port(int hub_id, const char *name) |
f6c874e3 SH |
173 | { |
174 | NetHub *hub; | |
175 | NetHubPort *port; | |
176 | ||
177 | QLIST_FOREACH(hub, &hubs, next) { | |
178 | if (hub->id == hub_id) { | |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | if (!hub) { | |
184 | hub = net_hub_new(hub_id); | |
185 | } | |
186 | ||
187 | port = net_hub_port_new(hub, name); | |
188 | return &port->nc; | |
189 | } | |
190 | ||
90d87a33 SH |
191 | /** |
192 | * Find a specific client on a hub | |
193 | */ | |
4e68f7a0 | 194 | NetClientState *net_hub_find_client_by_name(int hub_id, const char *name) |
90d87a33 SH |
195 | { |
196 | NetHub *hub; | |
197 | NetHubPort *port; | |
4e68f7a0 | 198 | NetClientState *peer; |
90d87a33 SH |
199 | |
200 | QLIST_FOREACH(hub, &hubs, next) { | |
201 | if (hub->id == hub_id) { | |
202 | QLIST_FOREACH(port, &hub->ports, next) { | |
203 | peer = port->nc.peer; | |
204 | ||
205 | if (peer && strcmp(peer->name, name) == 0) { | |
206 | return peer; | |
207 | } | |
208 | } | |
209 | } | |
210 | } | |
211 | return NULL; | |
212 | } | |
213 | ||
606c10e2 ZYW |
214 | /** |
215 | * Find a available port on a hub; otherwise create one new port | |
216 | */ | |
4e68f7a0 | 217 | NetClientState *net_hub_port_find(int hub_id) |
606c10e2 ZYW |
218 | { |
219 | NetHub *hub; | |
220 | NetHubPort *port; | |
4e68f7a0 | 221 | NetClientState *nc; |
606c10e2 ZYW |
222 | |
223 | QLIST_FOREACH(hub, &hubs, next) { | |
224 | if (hub->id == hub_id) { | |
225 | QLIST_FOREACH(port, &hub->ports, next) { | |
226 | nc = port->nc.peer; | |
227 | if (!nc) { | |
228 | return &(port->nc); | |
229 | } | |
230 | } | |
231 | break; | |
232 | } | |
233 | } | |
234 | ||
235 | nc = net_hub_add_port(hub_id, NULL); | |
236 | return nc; | |
237 | } | |
238 | ||
f6c874e3 SH |
239 | /** |
240 | * Print hub configuration | |
241 | */ | |
242 | void net_hub_info(Monitor *mon) | |
243 | { | |
244 | NetHub *hub; | |
245 | NetHubPort *port; | |
246 | ||
247 | QLIST_FOREACH(hub, &hubs, next) { | |
248 | monitor_printf(mon, "hub %d\n", hub->id); | |
249 | QLIST_FOREACH(port, &hub->ports, next) { | |
a6efd6ae | 250 | monitor_printf(mon, " \\ %s", port->nc.name); |
1a859593 | 251 | if (port->nc.peer) { |
a6efd6ae | 252 | monitor_printf(mon, ": "); |
1a859593 | 253 | print_net_client(mon, port->nc.peer); |
a6efd6ae JW |
254 | } else { |
255 | monitor_printf(mon, "\n"); | |
1a859593 | 256 | } |
f6c874e3 SH |
257 | } |
258 | } | |
259 | } | |
260 | ||
261 | /** | |
262 | * Get the hub id that a client is connected to | |
263 | * | |
e103129b | 264 | * @id: Pointer for hub id output, may be NULL |
f6c874e3 | 265 | */ |
4e68f7a0 | 266 | int net_hub_id_for_client(NetClientState *nc, int *id) |
f6c874e3 SH |
267 | { |
268 | NetHubPort *port; | |
269 | ||
f394b2e2 | 270 | if (nc->info->type == NET_CLIENT_DRIVER_HUBPORT) { |
f6c874e3 SH |
271 | port = DO_UPCAST(NetHubPort, nc, nc); |
272 | } else if (nc->peer != NULL && nc->peer->info->type == | |
f394b2e2 | 273 | NET_CLIENT_DRIVER_HUBPORT) { |
f6c874e3 SH |
274 | port = DO_UPCAST(NetHubPort, nc, nc->peer); |
275 | } else { | |
276 | return -ENOENT; | |
277 | } | |
278 | ||
279 | if (id) { | |
280 | *id = port->hub->id; | |
281 | } | |
282 | return 0; | |
283 | } | |
284 | ||
cebea510 | 285 | int net_init_hubport(const Netdev *netdev, const char *name, |
a30ecde6 | 286 | NetClientState *peer, Error **errp) |
f6c874e3 SH |
287 | { |
288 | const NetdevHubPortOptions *hubport; | |
289 | ||
f394b2e2 | 290 | assert(netdev->type == NET_CLIENT_DRIVER_HUBPORT); |
ca7eb184 | 291 | assert(!peer); |
f394b2e2 | 292 | hubport = &netdev->u.hubport; |
f6c874e3 | 293 | |
f6c874e3 SH |
294 | net_hub_add_port(hubport->hubid, name); |
295 | return 0; | |
296 | } | |
81017645 SH |
297 | |
298 | /** | |
299 | * Warn if hub configurations are likely wrong | |
300 | */ | |
301 | void net_hub_check_clients(void) | |
302 | { | |
303 | NetHub *hub; | |
304 | NetHubPort *port; | |
4e68f7a0 | 305 | NetClientState *peer; |
81017645 SH |
306 | |
307 | QLIST_FOREACH(hub, &hubs, next) { | |
308 | int has_nic = 0, has_host_dev = 0; | |
309 | ||
310 | QLIST_FOREACH(port, &hub->ports, next) { | |
311 | peer = port->nc.peer; | |
312 | if (!peer) { | |
b62e39b4 | 313 | warn_report("hub port %s has no peer", port->nc.name); |
81017645 SH |
314 | continue; |
315 | } | |
316 | ||
317 | switch (peer->info->type) { | |
f394b2e2 | 318 | case NET_CLIENT_DRIVER_NIC: |
81017645 SH |
319 | has_nic = 1; |
320 | break; | |
f394b2e2 EB |
321 | case NET_CLIENT_DRIVER_USER: |
322 | case NET_CLIENT_DRIVER_TAP: | |
323 | case NET_CLIENT_DRIVER_SOCKET: | |
324 | case NET_CLIENT_DRIVER_VDE: | |
325 | case NET_CLIENT_DRIVER_VHOST_USER: | |
81017645 SH |
326 | has_host_dev = 1; |
327 | break; | |
328 | default: | |
329 | break; | |
330 | } | |
331 | } | |
332 | if (has_host_dev && !has_nic) { | |
2ab4b135 | 333 | warn_report("vlan %d with no nics", hub->id); |
81017645 SH |
334 | } |
335 | if (has_nic && !has_host_dev) { | |
b62e39b4 | 336 | warn_report("vlan %d is not connected to host network", hub->id); |
81017645 SH |
337 | } |
338 | } | |
339 | } | |
199ee608 LR |
340 | |
341 | bool net_hub_flush(NetClientState *nc) | |
342 | { | |
343 | NetHubPort *port; | |
344 | NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc); | |
345 | int ret = 0; | |
346 | ||
347 | QLIST_FOREACH(port, &source_port->hub->ports, next) { | |
348 | if (port != source_port) { | |
067404be | 349 | ret += qemu_net_queue_flush(port->nc.incoming_queue); |
199ee608 LR |
350 | } |
351 | } | |
352 | return ret ? true : false; | |
353 | } |