]>
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 | ||
15 | #include "monitor.h" | |
16 | #include "net.h" | |
17 | #include "hub.h" | |
52a3cb86 | 18 | #include "iov.h" |
f6c874e3 SH |
19 | |
20 | /* | |
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. | |
24 | */ | |
25 | ||
26 | typedef struct NetHub NetHub; | |
27 | ||
28 | typedef struct NetHubPort { | |
4e68f7a0 | 29 | NetClientState nc; |
f6c874e3 SH |
30 | QLIST_ENTRY(NetHubPort) next; |
31 | NetHub *hub; | |
32 | int id; | |
33 | } NetHubPort; | |
34 | ||
35 | struct NetHub { | |
36 | int id; | |
37 | QLIST_ENTRY(NetHub) next; | |
38 | int num_ports; | |
39 | QLIST_HEAD(, NetHubPort) ports; | |
40 | }; | |
41 | ||
42 | static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs); | |
43 | ||
44 | static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, | |
45 | const uint8_t *buf, size_t len) | |
46 | { | |
47 | NetHubPort *port; | |
48 | ||
49 | QLIST_FOREACH(port, &hub->ports, next) { | |
50 | if (port == source_port) { | |
51 | continue; | |
52 | } | |
53 | ||
54 | qemu_send_packet(&port->nc, buf, len); | |
55 | } | |
56 | return len; | |
57 | } | |
58 | ||
59 | static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, | |
60 | const struct iovec *iov, int iovcnt) | |
61 | { | |
62 | NetHubPort *port; | |
52a3cb86 | 63 | ssize_t len = iov_size(iov, iovcnt); |
f6c874e3 SH |
64 | |
65 | QLIST_FOREACH(port, &hub->ports, next) { | |
66 | if (port == source_port) { | |
67 | continue; | |
68 | } | |
69 | ||
52a3cb86 | 70 | qemu_sendv_packet(&port->nc, iov, iovcnt); |
f6c874e3 | 71 | } |
52a3cb86 | 72 | return len; |
f6c874e3 SH |
73 | } |
74 | ||
75 | static NetHub *net_hub_new(int id) | |
76 | { | |
77 | NetHub *hub; | |
78 | ||
79 | hub = g_malloc(sizeof(*hub)); | |
80 | hub->id = id; | |
81 | hub->num_ports = 0; | |
82 | QLIST_INIT(&hub->ports); | |
83 | ||
84 | QLIST_INSERT_HEAD(&hubs, hub, next); | |
85 | ||
86 | return hub; | |
87 | } | |
88 | ||
52a3cb86 ZYW |
89 | static int net_hub_port_can_receive(NetClientState *nc) |
90 | { | |
91 | NetHubPort *port; | |
92 | NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc); | |
93 | NetHub *hub = src_port->hub; | |
94 | ||
95 | QLIST_FOREACH(port, &hub->ports, next) { | |
96 | if (port == src_port) { | |
97 | continue; | |
98 | } | |
99 | ||
61518a74 SH |
100 | if (qemu_can_send_packet(&port->nc)) { |
101 | return 1; | |
52a3cb86 ZYW |
102 | } |
103 | } | |
104 | ||
61518a74 | 105 | return 0; |
52a3cb86 ZYW |
106 | } |
107 | ||
4e68f7a0 | 108 | static ssize_t net_hub_port_receive(NetClientState *nc, |
f6c874e3 SH |
109 | const uint8_t *buf, size_t len) |
110 | { | |
111 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
112 | ||
113 | return net_hub_receive(port->hub, port, buf, len); | |
114 | } | |
115 | ||
4e68f7a0 | 116 | static ssize_t net_hub_port_receive_iov(NetClientState *nc, |
f6c874e3 SH |
117 | const struct iovec *iov, int iovcnt) |
118 | { | |
119 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
120 | ||
121 | return net_hub_receive_iov(port->hub, port, iov, iovcnt); | |
122 | } | |
123 | ||
4e68f7a0 | 124 | static void net_hub_port_cleanup(NetClientState *nc) |
f6c874e3 SH |
125 | { |
126 | NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc); | |
127 | ||
128 | QLIST_REMOVE(port, next); | |
129 | } | |
130 | ||
131 | static NetClientInfo net_hub_port_info = { | |
132 | .type = NET_CLIENT_OPTIONS_KIND_HUBPORT, | |
133 | .size = sizeof(NetHubPort), | |
52a3cb86 | 134 | .can_receive = net_hub_port_can_receive, |
f6c874e3 SH |
135 | .receive = net_hub_port_receive, |
136 | .receive_iov = net_hub_port_receive_iov, | |
137 | .cleanup = net_hub_port_cleanup, | |
138 | }; | |
139 | ||
140 | static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) | |
141 | { | |
4e68f7a0 | 142 | NetClientState *nc; |
f6c874e3 SH |
143 | NetHubPort *port; |
144 | int id = hub->num_ports++; | |
145 | char default_name[128]; | |
146 | ||
147 | if (!name) { | |
148 | snprintf(default_name, sizeof(default_name), | |
149 | "hub%dport%d", hub->id, id); | |
150 | name = default_name; | |
151 | } | |
152 | ||
ab5f3f84 | 153 | nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name); |
f6c874e3 SH |
154 | port = DO_UPCAST(NetHubPort, nc, nc); |
155 | port->id = id; | |
156 | port->hub = hub; | |
157 | ||
158 | QLIST_INSERT_HEAD(&hub->ports, port, next); | |
159 | ||
160 | return port; | |
161 | } | |
162 | ||
163 | /** | |
164 | * Create a port on a given hub | |
165 | * @name: Net client name or NULL for default name. | |
166 | * | |
167 | * If there is no existing hub with the given id then a new hub is created. | |
168 | */ | |
4e68f7a0 | 169 | NetClientState *net_hub_add_port(int hub_id, const char *name) |
f6c874e3 SH |
170 | { |
171 | NetHub *hub; | |
172 | NetHubPort *port; | |
173 | ||
174 | QLIST_FOREACH(hub, &hubs, next) { | |
175 | if (hub->id == hub_id) { | |
176 | break; | |
177 | } | |
178 | } | |
179 | ||
180 | if (!hub) { | |
181 | hub = net_hub_new(hub_id); | |
182 | } | |
183 | ||
184 | port = net_hub_port_new(hub, name); | |
185 | return &port->nc; | |
186 | } | |
187 | ||
90d87a33 SH |
188 | /** |
189 | * Find a specific client on a hub | |
190 | */ | |
4e68f7a0 | 191 | NetClientState *net_hub_find_client_by_name(int hub_id, const char *name) |
90d87a33 SH |
192 | { |
193 | NetHub *hub; | |
194 | NetHubPort *port; | |
4e68f7a0 | 195 | NetClientState *peer; |
90d87a33 SH |
196 | |
197 | QLIST_FOREACH(hub, &hubs, next) { | |
198 | if (hub->id == hub_id) { | |
199 | QLIST_FOREACH(port, &hub->ports, next) { | |
200 | peer = port->nc.peer; | |
201 | ||
202 | if (peer && strcmp(peer->name, name) == 0) { | |
203 | return peer; | |
204 | } | |
205 | } | |
206 | } | |
207 | } | |
208 | return NULL; | |
209 | } | |
210 | ||
606c10e2 ZYW |
211 | /** |
212 | * Find a available port on a hub; otherwise create one new port | |
213 | */ | |
4e68f7a0 | 214 | NetClientState *net_hub_port_find(int hub_id) |
606c10e2 ZYW |
215 | { |
216 | NetHub *hub; | |
217 | NetHubPort *port; | |
4e68f7a0 | 218 | NetClientState *nc; |
606c10e2 ZYW |
219 | |
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); | |
226 | } | |
227 | } | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | nc = net_hub_add_port(hub_id, NULL); | |
233 | return nc; | |
234 | } | |
235 | ||
f6c874e3 SH |
236 | /** |
237 | * Print hub configuration | |
238 | */ | |
239 | void net_hub_info(Monitor *mon) | |
240 | { | |
241 | NetHub *hub; | |
242 | NetHubPort *port; | |
243 | ||
244 | QLIST_FOREACH(hub, &hubs, next) { | |
245 | monitor_printf(mon, "hub %d\n", hub->id); | |
246 | QLIST_FOREACH(port, &hub->ports, next) { | |
1a859593 ZYW |
247 | if (port->nc.peer) { |
248 | monitor_printf(mon, " \\ "); | |
249 | print_net_client(mon, port->nc.peer); | |
250 | } | |
f6c874e3 SH |
251 | } |
252 | } | |
253 | } | |
254 | ||
255 | /** | |
256 | * Get the hub id that a client is connected to | |
257 | * | |
258 | * @id Pointer for hub id output, may be NULL | |
259 | */ | |
4e68f7a0 | 260 | int net_hub_id_for_client(NetClientState *nc, int *id) |
f6c874e3 SH |
261 | { |
262 | NetHubPort *port; | |
263 | ||
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; | |
271 | } | |
272 | ||
273 | if (id) { | |
274 | *id = port->hub->id; | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | int net_init_hubport(const NetClientOptions *opts, const char *name, | |
4e68f7a0 | 280 | NetClientState *peer) |
f6c874e3 SH |
281 | { |
282 | const NetdevHubPortOptions *hubport; | |
283 | ||
284 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT); | |
285 | hubport = opts->hubport; | |
286 | ||
d33d93b2 SH |
287 | /* Treat hub port like a backend, NIC must be the one to peer */ |
288 | if (peer) { | |
f6c874e3 SH |
289 | return -EINVAL; |
290 | } | |
291 | ||
292 | net_hub_add_port(hubport->hubid, name); | |
293 | return 0; | |
294 | } | |
81017645 SH |
295 | |
296 | /** | |
297 | * Warn if hub configurations are likely wrong | |
298 | */ | |
299 | void net_hub_check_clients(void) | |
300 | { | |
301 | NetHub *hub; | |
302 | NetHubPort *port; | |
4e68f7a0 | 303 | NetClientState *peer; |
81017645 SH |
304 | |
305 | QLIST_FOREACH(hub, &hubs, next) { | |
306 | int has_nic = 0, has_host_dev = 0; | |
307 | ||
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; | |
314 | } | |
315 | ||
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; | |
328 | } | |
329 | } | |
330 | if (has_host_dev && !has_nic) { | |
331 | fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id); | |
332 | } | |
333 | if (has_nic && !has_host_dev) { | |
334 | fprintf(stderr, | |
335 | "Warning: vlan %d is not connected to host network\n", | |
336 | hub->id); | |
337 | } | |
338 | } | |
339 | } |