]>
Commit | Line | Data |
---|---|---|
63a01ef8 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
1df49e04 | 24 | #include "net.h" |
63a01ef8 | 25 | |
d40cdb10 BS |
26 | #include "config-host.h" |
27 | ||
a8ed73f7 | 28 | #include "net/tap.h" |
42281ac9 | 29 | #include "net/socket.h" |
1abecf77 | 30 | #include "net/dump.h" |
68ac40d2 | 31 | #include "net/slirp.h" |
5c361cc3 | 32 | #include "net/vde.h" |
f1d078c3 | 33 | #include "net/util.h" |
511d2b14 | 34 | #include "monitor.h" |
1df49e04 | 35 | #include "qemu-common.h" |
511d2b14 | 36 | #include "qemu_socket.h" |
4b37156c | 37 | #include "qmp-commands.h" |
75422b0d | 38 | #include "hw/qdev.h" |
ce053661 | 39 | #include "iov.h" |
6687b79d LE |
40 | #include "qapi-visit.h" |
41 | #include "qapi/opts-visitor.h" | |
42 | #include "qapi/qapi-dealloc-visitor.h" | |
511d2b14 | 43 | |
2944e4ec SW |
44 | /* Net bridge is currently not supported for W32. */ |
45 | #if !defined(_WIN32) | |
46 | # define CONFIG_NET_BRIDGE | |
47 | #endif | |
48 | ||
5610c3aa | 49 | static QTAILQ_HEAD(, VLANState) vlans; |
577c4af9 | 50 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
63a01ef8 | 51 | |
cb4522cc GH |
52 | int default_net = 1; |
53 | ||
63a01ef8 AL |
54 | /***********************************************************/ |
55 | /* network device redirectors */ | |
56 | ||
68ac40d2 | 57 | #if defined(DEBUG_NET) |
63a01ef8 AL |
58 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
59 | { | |
60 | int len, i, j, c; | |
61 | ||
62 | for(i=0;i<size;i+=16) { | |
63 | len = size - i; | |
64 | if (len > 16) | |
65 | len = 16; | |
66 | fprintf(f, "%08x ", i); | |
67 | for(j=0;j<16;j++) { | |
68 | if (j < len) | |
69 | fprintf(f, " %02x", buf[i+j]); | |
70 | else | |
71 | fprintf(f, " "); | |
72 | } | |
73 | fprintf(f, " "); | |
74 | for(j=0;j<len;j++) { | |
75 | c = buf[i+j]; | |
76 | if (c < ' ' || c > '~') | |
77 | c = '.'; | |
78 | fprintf(f, "%c", c); | |
79 | } | |
80 | fprintf(f, "\n"); | |
81 | } | |
82 | } | |
83 | #endif | |
84 | ||
63a01ef8 AL |
85 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
86 | { | |
87 | const char *p, *p1; | |
88 | int len; | |
89 | p = *pp; | |
90 | p1 = strchr(p, sep); | |
91 | if (!p1) | |
92 | return -1; | |
93 | len = p1 - p; | |
94 | p1++; | |
95 | if (buf_size > 0) { | |
96 | if (len > buf_size - 1) | |
97 | len = buf_size - 1; | |
98 | memcpy(buf, p, len); | |
99 | buf[len] = '\0'; | |
100 | } | |
101 | *pp = p1; | |
102 | return 0; | |
103 | } | |
104 | ||
63a01ef8 AL |
105 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
106 | { | |
107 | char buf[512]; | |
108 | struct hostent *he; | |
109 | const char *p, *r; | |
110 | int port; | |
111 | ||
112 | p = str; | |
113 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
114 | return -1; | |
115 | saddr->sin_family = AF_INET; | |
116 | if (buf[0] == '\0') { | |
117 | saddr->sin_addr.s_addr = 0; | |
118 | } else { | |
cd390083 | 119 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
120 | if (!inet_aton(buf, &saddr->sin_addr)) |
121 | return -1; | |
122 | } else { | |
123 | if ((he = gethostbyname(buf)) == NULL) | |
124 | return - 1; | |
125 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
126 | } | |
127 | } | |
128 | port = strtol(p, (char **)&r, 0); | |
129 | if (r == p) | |
130 | return -1; | |
131 | saddr->sin_port = htons(port); | |
132 | return 0; | |
133 | } | |
134 | ||
7cb7434b AL |
135 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
136 | { | |
137 | snprintf(vc->info_str, sizeof(vc->info_str), | |
4dda4063 AL |
138 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
139 | vc->model, | |
7cb7434b AL |
140 | macaddr[0], macaddr[1], macaddr[2], |
141 | macaddr[3], macaddr[4], macaddr[5]); | |
142 | } | |
143 | ||
76d32cba GH |
144 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
145 | { | |
146 | static int index = 0; | |
147 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; | |
148 | ||
149 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) | |
150 | return; | |
151 | macaddr->a[0] = 0x52; | |
152 | macaddr->a[1] = 0x54; | |
153 | macaddr->a[2] = 0x00; | |
154 | macaddr->a[3] = 0x12; | |
155 | macaddr->a[4] = 0x34; | |
156 | macaddr->a[5] = 0x56 + index++; | |
157 | } | |
158 | ||
676cff29 AL |
159 | static char *assign_name(VLANClientState *vc1, const char *model) |
160 | { | |
161 | VLANState *vlan; | |
53e51d85 | 162 | VLANClientState *vc; |
676cff29 AL |
163 | char buf[256]; |
164 | int id = 0; | |
165 | ||
5610c3aa | 166 | QTAILQ_FOREACH(vlan, &vlans, next) { |
5610c3aa MM |
167 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
168 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
676cff29 | 169 | id++; |
5610c3aa MM |
170 | } |
171 | } | |
676cff29 AL |
172 | } |
173 | ||
53e51d85 MA |
174 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
175 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
176 | id++; | |
177 | } | |
178 | } | |
179 | ||
676cff29 AL |
180 | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
181 | ||
7267c094 | 182 | return g_strdup(buf); |
676cff29 AL |
183 | } |
184 | ||
9a6ecb30 | 185 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 186 | unsigned flags, |
9a6ecb30 MM |
187 | const uint8_t *data, |
188 | size_t size, | |
189 | void *opaque); | |
190 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, | |
c0b8e49c | 191 | unsigned flags, |
9a6ecb30 MM |
192 | const struct iovec *iov, |
193 | int iovcnt, | |
194 | void *opaque); | |
195 | ||
45460d1a MM |
196 | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
197 | VLANState *vlan, | |
198 | VLANClientState *peer, | |
199 | const char *model, | |
200 | const char *name) | |
63a01ef8 | 201 | { |
5610c3aa MM |
202 | VLANClientState *vc; |
203 | ||
45460d1a MM |
204 | assert(info->size >= sizeof(VLANClientState)); |
205 | ||
7267c094 | 206 | vc = g_malloc0(info->size); |
5610c3aa | 207 | |
665a3b07 | 208 | vc->info = info; |
7267c094 | 209 | vc->model = g_strdup(model); |
45460d1a | 210 | if (name) { |
7267c094 | 211 | vc->name = g_strdup(name); |
45460d1a | 212 | } else { |
7a9f6e4a | 213 | vc->name = assign_name(vc, model); |
45460d1a | 214 | } |
5610c3aa | 215 | |
d80b9fc6 | 216 | if (vlan) { |
283c7c63 | 217 | assert(!peer); |
d80b9fc6 MM |
218 | vc->vlan = vlan; |
219 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); | |
577c4af9 | 220 | } else { |
283c7c63 | 221 | if (peer) { |
27f3f8a3 | 222 | assert(!peer->peer); |
283c7c63 MM |
223 | vc->peer = peer; |
224 | peer->peer = vc; | |
225 | } | |
577c4af9 | 226 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
9a6ecb30 MM |
227 | |
228 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, | |
229 | qemu_deliver_packet_iov, | |
230 | vc); | |
d80b9fc6 | 231 | } |
63a01ef8 | 232 | |
63a01ef8 AL |
233 | return vc; |
234 | } | |
235 | ||
ebef2c09 MM |
236 | NICState *qemu_new_nic(NetClientInfo *info, |
237 | NICConf *conf, | |
238 | const char *model, | |
239 | const char *name, | |
240 | void *opaque) | |
241 | { | |
242 | VLANClientState *nc; | |
243 | NICState *nic; | |
244 | ||
2be64a68 | 245 | assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC); |
ebef2c09 MM |
246 | assert(info->size >= sizeof(NICState)); |
247 | ||
248 | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); | |
249 | ||
250 | nic = DO_UPCAST(NICState, nc, nc); | |
251 | nic->conf = conf; | |
252 | nic->opaque = opaque; | |
253 | ||
254 | return nic; | |
255 | } | |
256 | ||
a083a89d | 257 | static void qemu_cleanup_vlan_client(VLANClientState *vc) |
63a01ef8 | 258 | { |
d80b9fc6 MM |
259 | if (vc->vlan) { |
260 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); | |
577c4af9 MM |
261 | } else { |
262 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); | |
d80b9fc6 | 263 | } |
63a01ef8 | 264 | |
665a3b07 MM |
265 | if (vc->info->cleanup) { |
266 | vc->info->cleanup(vc); | |
5610c3aa | 267 | } |
a083a89d | 268 | } |
5610c3aa | 269 | |
a083a89d MT |
270 | static void qemu_free_vlan_client(VLANClientState *vc) |
271 | { | |
272 | if (!vc->vlan) { | |
273 | if (vc->send_queue) { | |
274 | qemu_del_net_queue(vc->send_queue); | |
275 | } | |
276 | if (vc->peer) { | |
277 | vc->peer->peer = NULL; | |
278 | } | |
279 | } | |
7267c094 AL |
280 | g_free(vc->name); |
281 | g_free(vc->model); | |
282 | g_free(vc); | |
63a01ef8 AL |
283 | } |
284 | ||
a083a89d MT |
285 | void qemu_del_vlan_client(VLANClientState *vc) |
286 | { | |
287 | /* If there is a peer NIC, delete and cleanup client, but do not free. */ | |
2be64a68 | 288 | if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
a083a89d MT |
289 | NICState *nic = DO_UPCAST(NICState, nc, vc->peer); |
290 | if (nic->peer_deleted) { | |
291 | return; | |
292 | } | |
293 | nic->peer_deleted = true; | |
294 | /* Let NIC know peer is gone. */ | |
295 | vc->peer->link_down = true; | |
296 | if (vc->peer->info->link_status_changed) { | |
297 | vc->peer->info->link_status_changed(vc->peer); | |
298 | } | |
299 | qemu_cleanup_vlan_client(vc); | |
300 | return; | |
301 | } | |
302 | ||
303 | /* If this is a peer NIC and peer has already been deleted, free it now. */ | |
2be64a68 | 304 | if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
a083a89d MT |
305 | NICState *nic = DO_UPCAST(NICState, nc, vc); |
306 | if (nic->peer_deleted) { | |
307 | qemu_free_vlan_client(vc->peer); | |
308 | } | |
309 | } | |
310 | ||
311 | qemu_cleanup_vlan_client(vc); | |
312 | qemu_free_vlan_client(vc); | |
313 | } | |
314 | ||
68ac40d2 | 315 | VLANClientState * |
1a609520 JK |
316 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
317 | const char *client_str) | |
318 | { | |
319 | VLANState *vlan; | |
320 | VLANClientState *vc; | |
321 | ||
322 | vlan = qemu_find_vlan(vlan_id, 0); | |
323 | if (!vlan) { | |
324 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); | |
325 | return NULL; | |
326 | } | |
327 | ||
5610c3aa | 328 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1a609520 JK |
329 | if (!strcmp(vc->name, client_str)) { |
330 | break; | |
331 | } | |
332 | } | |
333 | if (!vc) { | |
334 | monitor_printf(mon, "can't find device %s on VLAN %d\n", | |
335 | client_str, vlan_id); | |
336 | } | |
337 | ||
338 | return vc; | |
339 | } | |
340 | ||
57f9ef17 MM |
341 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
342 | { | |
343 | VLANClientState *nc; | |
344 | VLANState *vlan; | |
345 | ||
346 | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { | |
2be64a68 | 347 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
57f9ef17 MM |
348 | func(DO_UPCAST(NICState, nc, nc), opaque); |
349 | } | |
350 | } | |
351 | ||
352 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
353 | QTAILQ_FOREACH(nc, &vlan->clients, next) { | |
2be64a68 | 354 | if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { |
57f9ef17 MM |
355 | func(DO_UPCAST(NICState, nc, nc), opaque); |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
2e1e0641 | 361 | int qemu_can_send_packet(VLANClientState *sender) |
63a01ef8 | 362 | { |
2e1e0641 | 363 | VLANState *vlan = sender->vlan; |
63a01ef8 AL |
364 | VLANClientState *vc; |
365 | ||
9a6ecb30 | 366 | if (sender->peer) { |
893379ef MM |
367 | if (sender->peer->receive_disabled) { |
368 | return 0; | |
665a3b07 MM |
369 | } else if (sender->peer->info->can_receive && |
370 | !sender->peer->info->can_receive(sender->peer)) { | |
9a6ecb30 | 371 | return 0; |
893379ef MM |
372 | } else { |
373 | return 1; | |
9a6ecb30 MM |
374 | } |
375 | } | |
376 | ||
d80b9fc6 MM |
377 | if (!sender->vlan) { |
378 | return 1; | |
379 | } | |
380 | ||
5610c3aa | 381 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
2e1e0641 MM |
382 | if (vc == sender) { |
383 | continue; | |
384 | } | |
385 | ||
cda9046b | 386 | /* no can_receive() handler, they can always receive */ |
60c07d93 VP |
387 | if (vc->info->can_receive && !vc->info->can_receive(vc)) { |
388 | return 0; | |
63a01ef8 AL |
389 | } |
390 | } | |
60c07d93 | 391 | return 1; |
63a01ef8 AL |
392 | } |
393 | ||
9a6ecb30 | 394 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 395 | unsigned flags, |
9a6ecb30 MM |
396 | const uint8_t *data, |
397 | size_t size, | |
398 | void *opaque) | |
399 | { | |
400 | VLANClientState *vc = opaque; | |
893379ef | 401 | ssize_t ret; |
9a6ecb30 MM |
402 | |
403 | if (vc->link_down) { | |
404 | return size; | |
405 | } | |
406 | ||
893379ef MM |
407 | if (vc->receive_disabled) { |
408 | return 0; | |
409 | } | |
410 | ||
665a3b07 MM |
411 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
412 | ret = vc->info->receive_raw(vc, data, size); | |
893379ef | 413 | } else { |
665a3b07 | 414 | ret = vc->info->receive(vc, data, size); |
893379ef MM |
415 | } |
416 | ||
417 | if (ret == 0) { | |
418 | vc->receive_disabled = 1; | |
419 | }; | |
420 | ||
421 | return ret; | |
9a6ecb30 MM |
422 | } |
423 | ||
f7105843 | 424 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
c0b8e49c | 425 | unsigned flags, |
f7105843 MM |
426 | const uint8_t *buf, |
427 | size_t size, | |
428 | void *opaque) | |
63a01ef8 | 429 | { |
f7105843 | 430 | VLANState *vlan = opaque; |
63a01ef8 | 431 | VLANClientState *vc; |
893379ef | 432 | ssize_t ret = -1; |
63a01ef8 | 433 | |
f7105843 | 434 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3e021d40 MM |
435 | ssize_t len; |
436 | ||
437 | if (vc == sender) { | |
438 | continue; | |
764a4d1d | 439 | } |
3e021d40 MM |
440 | |
441 | if (vc->link_down) { | |
442 | ret = size; | |
443 | continue; | |
444 | } | |
445 | ||
893379ef MM |
446 | if (vc->receive_disabled) { |
447 | ret = 0; | |
448 | continue; | |
449 | } | |
450 | ||
665a3b07 MM |
451 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
452 | len = vc->info->receive_raw(vc, buf, size); | |
893379ef | 453 | } else { |
665a3b07 | 454 | len = vc->info->receive(vc, buf, size); |
893379ef MM |
455 | } |
456 | ||
457 | if (len == 0) { | |
458 | vc->receive_disabled = 1; | |
459 | } | |
3e021d40 MM |
460 | |
461 | ret = (ret >= 0) ? ret : len; | |
893379ef | 462 | |
764a4d1d | 463 | } |
3e021d40 MM |
464 | |
465 | return ret; | |
764a4d1d AL |
466 | } |
467 | ||
8cad5516 MM |
468 | void qemu_purge_queued_packets(VLANClientState *vc) |
469 | { | |
9a6ecb30 MM |
470 | NetQueue *queue; |
471 | ||
472 | if (!vc->peer && !vc->vlan) { | |
d80b9fc6 | 473 | return; |
9a6ecb30 | 474 | } |
d80b9fc6 | 475 | |
9a6ecb30 MM |
476 | if (vc->peer) { |
477 | queue = vc->peer->send_queue; | |
478 | } else { | |
479 | queue = vc->vlan->send_queue; | |
480 | } | |
481 | ||
482 | qemu_net_queue_purge(queue, vc); | |
8cad5516 MM |
483 | } |
484 | ||
f3b6c7fc | 485 | void qemu_flush_queued_packets(VLANClientState *vc) |
e94667b9 | 486 | { |
9a6ecb30 MM |
487 | NetQueue *queue; |
488 | ||
893379ef MM |
489 | vc->receive_disabled = 0; |
490 | ||
9a6ecb30 MM |
491 | if (vc->vlan) { |
492 | queue = vc->vlan->send_queue; | |
493 | } else { | |
494 | queue = vc->send_queue; | |
495 | } | |
d80b9fc6 | 496 | |
9a6ecb30 | 497 | qemu_net_queue_flush(queue); |
e94667b9 MM |
498 | } |
499 | ||
ca77d175 MM |
500 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
501 | unsigned flags, | |
502 | const uint8_t *buf, int size, | |
503 | NetPacketSent *sent_cb) | |
764a4d1d | 504 | { |
9a6ecb30 | 505 | NetQueue *queue; |
436e5e53 | 506 | |
63a01ef8 | 507 | #ifdef DEBUG_NET |
d80b9fc6 | 508 | printf("qemu_send_packet_async:\n"); |
63a01ef8 AL |
509 | hex_dump(stdout, buf, size); |
510 | #endif | |
f3b6c7fc | 511 | |
9a6ecb30 MM |
512 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
513 | return size; | |
514 | } | |
515 | ||
516 | if (sender->peer) { | |
517 | queue = sender->peer->send_queue; | |
518 | } else { | |
519 | queue = sender->vlan->send_queue; | |
520 | } | |
521 | ||
ca77d175 MM |
522 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
523 | } | |
524 | ||
525 | ssize_t qemu_send_packet_async(VLANClientState *sender, | |
526 | const uint8_t *buf, int size, | |
527 | NetPacketSent *sent_cb) | |
528 | { | |
529 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, | |
530 | buf, size, sent_cb); | |
f3b6c7fc MM |
531 | } |
532 | ||
533 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
534 | { | |
535 | qemu_send_packet_async(vc, buf, size, NULL); | |
63a01ef8 AL |
536 | } |
537 | ||
ca77d175 MM |
538 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
539 | { | |
540 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, | |
541 | buf, size, NULL); | |
542 | } | |
543 | ||
fbe78f4f AL |
544 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
545 | int iovcnt) | |
546 | { | |
547 | uint8_t buffer[4096]; | |
ce053661 | 548 | size_t offset; |
fbe78f4f | 549 | |
dcf6f5e1 | 550 | offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer)); |
fbe78f4f | 551 | |
665a3b07 | 552 | return vc->info->receive(vc, buffer, offset); |
fbe78f4f AL |
553 | } |
554 | ||
9a6ecb30 | 555 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 556 | unsigned flags, |
9a6ecb30 MM |
557 | const struct iovec *iov, |
558 | int iovcnt, | |
559 | void *opaque) | |
560 | { | |
561 | VLANClientState *vc = opaque; | |
562 | ||
563 | if (vc->link_down) { | |
ce053661 | 564 | return iov_size(iov, iovcnt); |
9a6ecb30 MM |
565 | } |
566 | ||
665a3b07 MM |
567 | if (vc->info->receive_iov) { |
568 | return vc->info->receive_iov(vc, iov, iovcnt); | |
9a6ecb30 MM |
569 | } else { |
570 | return vc_sendv_compat(vc, iov, iovcnt); | |
571 | } | |
572 | } | |
573 | ||
f7105843 | 574 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 575 | unsigned flags, |
f7105843 MM |
576 | const struct iovec *iov, |
577 | int iovcnt, | |
578 | void *opaque) | |
fbe78f4f | 579 | { |
f7105843 | 580 | VLANState *vlan = opaque; |
fbe78f4f | 581 | VLANClientState *vc; |
f7105843 | 582 | ssize_t ret = -1; |
e94667b9 | 583 | |
f7105843 | 584 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
e94667b9 MM |
585 | ssize_t len; |
586 | ||
587 | if (vc == sender) { | |
588 | continue; | |
589 | } | |
590 | ||
591 | if (vc->link_down) { | |
ce053661 | 592 | ret = iov_size(iov, iovcnt); |
e94667b9 MM |
593 | continue; |
594 | } | |
595 | ||
ca77d175 MM |
596 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
597 | ||
665a3b07 MM |
598 | if (vc->info->receive_iov) { |
599 | len = vc->info->receive_iov(vc, iov, iovcnt); | |
e94667b9 MM |
600 | } else { |
601 | len = vc_sendv_compat(vc, iov, iovcnt); | |
602 | } | |
603 | ||
604 | ret = (ret >= 0) ? ret : len; | |
605 | } | |
606 | ||
e94667b9 MM |
607 | return ret; |
608 | } | |
609 | ||
f3b6c7fc MM |
610 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
611 | const struct iovec *iov, int iovcnt, | |
612 | NetPacketSent *sent_cb) | |
e94667b9 | 613 | { |
9a6ecb30 MM |
614 | NetQueue *queue; |
615 | ||
616 | if (sender->link_down || (!sender->peer && !sender->vlan)) { | |
ce053661 | 617 | return iov_size(iov, iovcnt); |
e94667b9 MM |
618 | } |
619 | ||
9a6ecb30 MM |
620 | if (sender->peer) { |
621 | queue = sender->peer->send_queue; | |
622 | } else { | |
623 | queue = sender->vlan->send_queue; | |
624 | } | |
625 | ||
c0b8e49c MM |
626 | return qemu_net_queue_send_iov(queue, sender, |
627 | QEMU_NET_PACKET_FLAG_NONE, | |
628 | iov, iovcnt, sent_cb); | |
fbe78f4f AL |
629 | } |
630 | ||
f3b6c7fc MM |
631 | ssize_t |
632 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
633 | { | |
634 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
635 | } | |
636 | ||
63a01ef8 | 637 | /* find or alloc a new VLAN */ |
1a609520 | 638 | VLANState *qemu_find_vlan(int id, int allocate) |
63a01ef8 | 639 | { |
5610c3aa MM |
640 | VLANState *vlan; |
641 | ||
642 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
643 | if (vlan->id == id) { | |
63a01ef8 | 644 | return vlan; |
5610c3aa | 645 | } |
63a01ef8 | 646 | } |
5610c3aa | 647 | |
1a609520 JK |
648 | if (!allocate) { |
649 | return NULL; | |
650 | } | |
5610c3aa | 651 | |
7267c094 | 652 | vlan = g_malloc0(sizeof(VLANState)); |
63a01ef8 | 653 | vlan->id = id; |
5610c3aa | 654 | QTAILQ_INIT(&vlan->clients); |
f7105843 MM |
655 | |
656 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, | |
657 | qemu_vlan_deliver_packet_iov, | |
658 | vlan); | |
5610c3aa MM |
659 | |
660 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); | |
661 | ||
63a01ef8 AL |
662 | return vlan; |
663 | } | |
664 | ||
2ef924b4 | 665 | VLANClientState *qemu_find_netdev(const char *id) |
5869c4d5 MM |
666 | { |
667 | VLANClientState *vc; | |
668 | ||
669 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
2be64a68 | 670 | if (vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) |
85dde9a9 | 671 | continue; |
5869c4d5 MM |
672 | if (!strcmp(vc->name, id)) { |
673 | return vc; | |
674 | } | |
675 | } | |
676 | ||
677 | return NULL; | |
678 | } | |
679 | ||
7697079b AL |
680 | static int nic_get_free_idx(void) |
681 | { | |
682 | int index; | |
683 | ||
684 | for (index = 0; index < MAX_NICS; index++) | |
685 | if (!nd_table[index].used) | |
686 | return index; | |
687 | return -1; | |
688 | } | |
689 | ||
07caea31 MA |
690 | int qemu_show_nic_models(const char *arg, const char *const *models) |
691 | { | |
692 | int i; | |
693 | ||
c8057f95 | 694 | if (!arg || !is_help_option(arg)) { |
07caea31 | 695 | return 0; |
c8057f95 | 696 | } |
07caea31 MA |
697 | |
698 | fprintf(stderr, "qemu: Supported NIC models: "); | |
699 | for (i = 0 ; models[i]; i++) | |
700 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
701 | return 1; | |
702 | } | |
703 | ||
d07f22c5 AL |
704 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
705 | { | |
706 | const char *models[2]; | |
707 | ||
708 | models[0] = model; | |
709 | models[1] = NULL; | |
710 | ||
07caea31 MA |
711 | if (qemu_show_nic_models(nd->model, models)) |
712 | exit(0); | |
713 | if (qemu_find_nic_model(nd, models, model) < 0) | |
714 | exit(1); | |
d07f22c5 AL |
715 | } |
716 | ||
07caea31 MA |
717 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
718 | const char *default_model) | |
d07f22c5 | 719 | { |
07caea31 | 720 | int i; |
d07f22c5 AL |
721 | |
722 | if (!nd->model) | |
7267c094 | 723 | nd->model = g_strdup(default_model); |
d07f22c5 | 724 | |
07caea31 MA |
725 | for (i = 0 ; models[i]; i++) { |
726 | if (strcmp(nd->model, models[i]) == 0) | |
727 | return i; | |
d07f22c5 AL |
728 | } |
729 | ||
6daf194d | 730 | error_report("Unsupported NIC model: %s", nd->model); |
07caea31 | 731 | return -1; |
d07f22c5 AL |
732 | } |
733 | ||
5281d757 | 734 | int net_handle_fd_param(Monitor *mon, const char *param) |
c1d6eed7 | 735 | { |
f7c31d63 JW |
736 | int fd; |
737 | ||
738 | if (!qemu_isdigit(param[0]) && mon) { | |
c1d6eed7 MM |
739 | |
740 | fd = monitor_get_fd(mon, param); | |
741 | if (fd == -1) { | |
1ecda02b | 742 | error_report("No file descriptor named %s found", param); |
c1d6eed7 MM |
743 | return -1; |
744 | } | |
c1d6eed7 | 745 | } else { |
443916d1 | 746 | fd = qemu_parse_fd(param); |
c1d6eed7 | 747 | } |
f7c31d63 JW |
748 | |
749 | return fd; | |
c1d6eed7 MM |
750 | } |
751 | ||
1a0c0958 LE |
752 | static int net_init_nic(const NetClientOptions *opts, const char *name, |
753 | VLANState *vlan) | |
f83c6e10 MM |
754 | { |
755 | int idx; | |
756 | NICInfo *nd; | |
2456f36f LE |
757 | const NetLegacyNicOptions *nic; |
758 | ||
759 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC); | |
760 | nic = opts->nic; | |
f83c6e10 MM |
761 | |
762 | idx = nic_get_free_idx(); | |
763 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
1ecda02b | 764 | error_report("Too Many NICs"); |
f83c6e10 MM |
765 | return -1; |
766 | } | |
767 | ||
768 | nd = &nd_table[idx]; | |
769 | ||
770 | memset(nd, 0, sizeof(*nd)); | |
771 | ||
2456f36f LE |
772 | if (nic->has_netdev) { |
773 | nd->netdev = qemu_find_netdev(nic->netdev); | |
5869c4d5 | 774 | if (!nd->netdev) { |
2456f36f | 775 | error_report("netdev '%s' not found", nic->netdev); |
5869c4d5 MM |
776 | return -1; |
777 | } | |
778 | } else { | |
779 | assert(vlan); | |
780 | nd->vlan = vlan; | |
781 | } | |
6d952ebe | 782 | if (name) { |
7267c094 | 783 | nd->name = g_strdup(name); |
6d952ebe | 784 | } |
2456f36f LE |
785 | if (nic->has_model) { |
786 | nd->model = g_strdup(nic->model); | |
f83c6e10 | 787 | } |
2456f36f LE |
788 | if (nic->has_addr) { |
789 | nd->devaddr = g_strdup(nic->addr); | |
f83c6e10 MM |
790 | } |
791 | ||
2456f36f LE |
792 | if (nic->has_macaddr && |
793 | net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { | |
1ecda02b | 794 | error_report("invalid syntax for ethernet address"); |
f83c6e10 MM |
795 | return -1; |
796 | } | |
6eed1856 | 797 | qemu_macaddr_default_if_unset(&nd->macaddr); |
f83c6e10 | 798 | |
2456f36f LE |
799 | if (nic->has_vectors) { |
800 | if (nic->vectors > 0x7ffffff) { | |
801 | error_report("invalid # of vectors: %"PRIu32, nic->vectors); | |
802 | return -1; | |
803 | } | |
804 | nd->nvectors = nic->vectors; | |
805 | } else { | |
806 | nd->nvectors = DEV_NVECTORS_UNSPECIFIED; | |
f83c6e10 MM |
807 | } |
808 | ||
809 | nd->used = 1; | |
f83c6e10 MM |
810 | nb_nics++; |
811 | ||
812 | return idx; | |
813 | } | |
814 | ||
6687b79d LE |
815 | |
816 | static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])( | |
1a0c0958 | 817 | const NetClientOptions *opts, |
6687b79d LE |
818 | const char *name, |
819 | VLANState *vlan) = { | |
820 | [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic, | |
ec302ffd | 821 | #ifdef CONFIG_SLIRP |
6687b79d | 822 | [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp, |
2944e4ec | 823 | #endif |
6687b79d LE |
824 | [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap, |
825 | [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket, | |
dd51058d | 826 | #ifdef CONFIG_VDE |
6687b79d | 827 | [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde, |
dd51058d | 828 | #endif |
6687b79d | 829 | [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump, |
2944e4ec | 830 | #ifdef CONFIG_NET_BRIDGE |
6687b79d LE |
831 | [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge, |
832 | #endif | |
f83c6e10 MM |
833 | }; |
834 | ||
6687b79d | 835 | |
1a0c0958 | 836 | static int net_client_init1(const void *object, int is_netdev, Error **errp) |
f83c6e10 | 837 | { |
6687b79d LE |
838 | union { |
839 | const Netdev *netdev; | |
840 | const NetLegacy *net; | |
841 | } u; | |
842 | const NetClientOptions *opts; | |
6d952ebe | 843 | const char *name; |
f83c6e10 | 844 | |
5294e2c7 | 845 | if (is_netdev) { |
6687b79d LE |
846 | u.netdev = object; |
847 | opts = u.netdev->opts; | |
848 | name = u.netdev->id; | |
849 | ||
850 | switch (opts->kind) { | |
f6b134ac | 851 | #ifdef CONFIG_SLIRP |
6687b79d | 852 | case NET_CLIENT_OPTIONS_KIND_USER: |
f6b134ac | 853 | #endif |
6687b79d LE |
854 | case NET_CLIENT_OPTIONS_KIND_TAP: |
855 | case NET_CLIENT_OPTIONS_KIND_SOCKET: | |
f6b134ac | 856 | #ifdef CONFIG_VDE |
6687b79d LE |
857 | case NET_CLIENT_OPTIONS_KIND_VDE: |
858 | #endif | |
859 | #ifdef CONFIG_NET_BRIDGE | |
860 | case NET_CLIENT_OPTIONS_KIND_BRIDGE: | |
f6b134ac | 861 | #endif |
6687b79d LE |
862 | break; |
863 | ||
864 | default: | |
4559a1db LC |
865 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type", |
866 | "a netdev backend type"); | |
f6b134ac MM |
867 | return -1; |
868 | } | |
6687b79d LE |
869 | } else { |
870 | u.net = object; | |
871 | opts = u.net->opts; | |
872 | /* missing optional values have been initialized to "all bits zero" */ | |
873 | name = u.net->has_id ? u.net->id : u.net->name; | |
874 | } | |
f6b134ac | 875 | |
6687b79d LE |
876 | if (net_client_init_fun[opts->kind]) { |
877 | VLANState *vlan = NULL; | |
878 | ||
879 | /* Do not add to a vlan if it's a -netdev or a nic with a netdev= | |
880 | * parameter. */ | |
881 | if (!is_netdev && | |
882 | (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC || | |
883 | !opts->nic->has_netdev)) { | |
884 | vlan = qemu_find_vlan(u.net->has_vlan ? u.net->vlan : 0, true); | |
f6b134ac | 885 | } |
6687b79d | 886 | |
1a0c0958 | 887 | if (net_client_init_fun[opts->kind](opts, name, vlan) < 0) { |
6687b79d LE |
888 | /* TODO push error reporting into init() methods */ |
889 | error_set(errp, QERR_DEVICE_INIT_FAILED, | |
890 | NetClientOptionsKind_lookup[opts->kind]); | |
f6b134ac MM |
891 | return -1; |
892 | } | |
893 | } | |
6687b79d LE |
894 | return 0; |
895 | } | |
896 | ||
f6b134ac | 897 | |
6687b79d LE |
898 | static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp) |
899 | { | |
900 | if (is_netdev) { | |
901 | visit_type_Netdev(v, (Netdev **)object, NULL, errp); | |
902 | } else { | |
903 | visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp); | |
6d952ebe | 904 | } |
6687b79d | 905 | } |
6d952ebe | 906 | |
f6b134ac | 907 | |
6687b79d LE |
908 | int net_client_init(QemuOpts *opts, int is_netdev, Error **errp) |
909 | { | |
910 | void *object = NULL; | |
911 | Error *err = NULL; | |
912 | int ret = -1; | |
f83c6e10 | 913 | |
6687b79d LE |
914 | { |
915 | OptsVisitor *ov = opts_visitor_new(opts); | |
f6b134ac | 916 | |
6687b79d LE |
917 | net_visit(opts_get_visitor(ov), is_netdev, &object, &err); |
918 | opts_visitor_cleanup(ov); | |
f83c6e10 MM |
919 | } |
920 | ||
6687b79d | 921 | if (!err) { |
1a0c0958 | 922 | ret = net_client_init1(object, is_netdev, &err); |
6687b79d LE |
923 | } |
924 | ||
925 | if (object) { | |
926 | QapiDeallocVisitor *dv = qapi_dealloc_visitor_new(); | |
927 | ||
928 | net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL); | |
929 | qapi_dealloc_visitor_cleanup(dv); | |
930 | } | |
931 | ||
932 | error_propagate(errp, err); | |
933 | return ret; | |
f83c6e10 MM |
934 | } |
935 | ||
6687b79d | 936 | |
6f338c34 AL |
937 | static int net_host_check_device(const char *device) |
938 | { | |
939 | int i; | |
bb9ea79e | 940 | const char *valid_param_list[] = { "tap", "socket", "dump" |
2944e4ec SW |
941 | #ifdef CONFIG_NET_BRIDGE |
942 | , "bridge" | |
943 | #endif | |
6f338c34 AL |
944 | #ifdef CONFIG_SLIRP |
945 | ,"user" | |
946 | #endif | |
947 | #ifdef CONFIG_VDE | |
948 | ,"vde" | |
949 | #endif | |
950 | }; | |
951 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
952 | if (!strncmp(valid_param_list[i], device, | |
953 | strlen(valid_param_list[i]))) | |
954 | return 1; | |
955 | } | |
956 | ||
957 | return 0; | |
958 | } | |
959 | ||
f18c16de | 960 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
6f338c34 | 961 | { |
f18c16de | 962 | const char *device = qdict_get_str(qdict, "device"); |
7f1c9d20 | 963 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
4559a1db | 964 | Error *local_err = NULL; |
7f1c9d20 | 965 | QemuOpts *opts; |
f18c16de | 966 | |
6f338c34 | 967 | if (!net_host_check_device(device)) { |
376253ec | 968 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
969 | return; |
970 | } | |
7f1c9d20 | 971 | |
3329f07b | 972 | opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); |
7f1c9d20 | 973 | if (!opts) { |
7f1c9d20 MM |
974 | return; |
975 | } | |
976 | ||
977 | qemu_opt_set(opts, "type", device); | |
978 | ||
4559a1db LC |
979 | net_client_init(opts, 0, &local_err); |
980 | if (error_is_set(&local_err)) { | |
981 | qerror_report_err(local_err); | |
982 | error_free(local_err); | |
5c8be678 AL |
983 | monitor_printf(mon, "adding host network device %s failed\n", device); |
984 | } | |
6f338c34 AL |
985 | } |
986 | ||
f18c16de | 987 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
6f338c34 | 988 | { |
6f338c34 | 989 | VLANClientState *vc; |
f18c16de LC |
990 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
991 | const char *device = qdict_get_str(qdict, "device"); | |
6f338c34 | 992 | |
1a609520 | 993 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
6f338c34 | 994 | if (!vc) { |
6f338c34 AL |
995 | return; |
996 | } | |
e8f1f9db AL |
997 | if (!net_host_check_device(vc->model)) { |
998 | monitor_printf(mon, "invalid host network device %s\n", device); | |
999 | return; | |
1000 | } | |
6f338c34 AL |
1001 | qemu_del_vlan_client(vc); |
1002 | } | |
1003 | ||
928059a3 LC |
1004 | void netdev_add(QemuOpts *opts, Error **errp) |
1005 | { | |
1006 | net_client_init(opts, 1, errp); | |
1007 | } | |
1008 | ||
1009 | int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret) | |
ae82d324 | 1010 | { |
4e89978e | 1011 | Error *local_err = NULL; |
928059a3 | 1012 | QemuOptsList *opts_list; |
ae82d324 | 1013 | QemuOpts *opts; |
ae82d324 | 1014 | |
928059a3 LC |
1015 | opts_list = qemu_find_opts_err("netdev", &local_err); |
1016 | if (error_is_set(&local_err)) { | |
1017 | goto exit_err; | |
ae82d324 MA |
1018 | } |
1019 | ||
928059a3 LC |
1020 | opts = qemu_opts_from_qdict(opts_list, qdict, &local_err); |
1021 | if (error_is_set(&local_err)) { | |
1022 | goto exit_err; | |
1023 | } | |
1024 | ||
1025 | netdev_add(opts, &local_err); | |
1026 | if (error_is_set(&local_err)) { | |
410cbafe | 1027 | qemu_opts_del(opts); |
928059a3 | 1028 | goto exit_err; |
410cbafe YT |
1029 | } |
1030 | ||
928059a3 LC |
1031 | return 0; |
1032 | ||
1033 | exit_err: | |
1034 | qerror_report_err(local_err); | |
1035 | error_free(local_err); | |
1036 | return -1; | |
ae82d324 MA |
1037 | } |
1038 | ||
5f964155 | 1039 | void qmp_netdev_del(const char *id, Error **errp) |
ae82d324 | 1040 | { |
ae82d324 MA |
1041 | VLANClientState *vc; |
1042 | ||
1043 | vc = qemu_find_netdev(id); | |
85dde9a9 | 1044 | if (!vc) { |
5f964155 LC |
1045 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
1046 | return; | |
ae82d324 | 1047 | } |
5f964155 | 1048 | |
ae82d324 | 1049 | qemu_del_vlan_client(vc); |
5f964155 | 1050 | qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp), id)); |
ae82d324 MA |
1051 | } |
1052 | ||
44e798d3 JK |
1053 | static void print_net_client(Monitor *mon, VLANClientState *vc) |
1054 | { | |
1055 | monitor_printf(mon, "%s: type=%s,%s\n", vc->name, | |
6687b79d | 1056 | NetClientOptionsKind_lookup[vc->info->type], vc->info_str); |
44e798d3 JK |
1057 | } |
1058 | ||
376253ec | 1059 | void do_info_network(Monitor *mon) |
63a01ef8 AL |
1060 | { |
1061 | VLANState *vlan; | |
19061e63 | 1062 | VLANClientState *vc, *peer; |
2be64a68 | 1063 | NetClientOptionsKind type; |
63a01ef8 | 1064 | |
5610c3aa | 1065 | QTAILQ_FOREACH(vlan, &vlans, next) { |
376253ec | 1066 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
5610c3aa MM |
1067 | |
1068 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
44e798d3 JK |
1069 | monitor_printf(mon, " "); |
1070 | print_net_client(mon, vc); | |
5610c3aa | 1071 | } |
63a01ef8 | 1072 | } |
a0104e0e MA |
1073 | monitor_printf(mon, "Devices not on any VLAN:\n"); |
1074 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
19061e63 JK |
1075 | peer = vc->peer; |
1076 | type = vc->info->type; | |
2be64a68 | 1077 | if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) { |
44e798d3 JK |
1078 | monitor_printf(mon, " "); |
1079 | print_net_client(mon, vc); | |
19061e63 | 1080 | } /* else it's a netdev connected to a NIC, printed with the NIC */ |
2be64a68 | 1081 | if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) { |
44e798d3 JK |
1082 | monitor_printf(mon, " \\ "); |
1083 | print_net_client(mon, peer); | |
a0104e0e | 1084 | } |
a0104e0e | 1085 | } |
63a01ef8 AL |
1086 | } |
1087 | ||
4b37156c | 1088 | void qmp_set_link(const char *name, bool up, Error **errp) |
436e5e53 AL |
1089 | { |
1090 | VLANState *vlan; | |
1091 | VLANClientState *vc = NULL; | |
1092 | ||
5610c3aa MM |
1093 | QTAILQ_FOREACH(vlan, &vlans, next) { |
1094 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
1095 | if (strcmp(vc->name, name) == 0) { | |
dd5de373 | 1096 | goto done; |
5610c3aa MM |
1097 | } |
1098 | } | |
1099 | } | |
85dde9a9 MA |
1100 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1101 | if (!strcmp(vc->name, name)) { | |
1102 | goto done; | |
1103 | } | |
1104 | } | |
dd5de373 | 1105 | done: |
436e5e53 AL |
1106 | |
1107 | if (!vc) { | |
4b37156c LC |
1108 | error_set(errp, QERR_DEVICE_NOT_FOUND, name); |
1109 | return; | |
436e5e53 AL |
1110 | } |
1111 | ||
c9b26a4c | 1112 | vc->link_down = !up; |
436e5e53 | 1113 | |
665a3b07 MM |
1114 | if (vc->info->link_status_changed) { |
1115 | vc->info->link_status_changed(vc); | |
1116 | } | |
ab1cbe1c MT |
1117 | |
1118 | /* Notify peer. Don't update peer link status: this makes it possible to | |
1119 | * disconnect from host network without notifying the guest. | |
1120 | * FIXME: is disconnected link status change operation useful? | |
1121 | * | |
1122 | * Current behaviour is compatible with qemu vlans where there could be | |
1123 | * multiple clients that can still communicate with each other in | |
1124 | * disconnected mode. For now maintain this compatibility. */ | |
1125 | if (vc->peer && vc->peer->info->link_status_changed) { | |
1126 | vc->peer->info->link_status_changed(vc->peer); | |
1127 | } | |
436e5e53 AL |
1128 | } |
1129 | ||
63a01ef8 AL |
1130 | void net_cleanup(void) |
1131 | { | |
1132 | VLANState *vlan; | |
577c4af9 | 1133 | VLANClientState *vc, *next_vc; |
63a01ef8 | 1134 | |
5610c3aa | 1135 | QTAILQ_FOREACH(vlan, &vlans, next) { |
5610c3aa | 1136 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
b946a153 | 1137 | qemu_del_vlan_client(vc); |
63a01ef8 AL |
1138 | } |
1139 | } | |
577c4af9 MM |
1140 | |
1141 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { | |
1142 | qemu_del_vlan_client(vc); | |
1143 | } | |
63a01ef8 AL |
1144 | } |
1145 | ||
668680f7 | 1146 | void net_check_clients(void) |
63a01ef8 AL |
1147 | { |
1148 | VLANState *vlan; | |
62112d18 | 1149 | VLANClientState *vc; |
48e2faf2 | 1150 | int i; |
63a01ef8 | 1151 | |
641f6eae PM |
1152 | /* Don't warn about the default network setup that you get if |
1153 | * no command line -net or -netdev options are specified. There | |
1154 | * are two cases that we would otherwise complain about: | |
1155 | * (1) board doesn't support a NIC but the implicit "-net nic" | |
1156 | * requested one | |
1157 | * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic" | |
1158 | * sets up a nic that isn't connected to anything. | |
1159 | */ | |
1160 | if (default_net) { | |
1161 | return; | |
1162 | } | |
1163 | ||
5610c3aa | 1164 | QTAILQ_FOREACH(vlan, &vlans, next) { |
ac60cc18 TG |
1165 | int has_nic = 0, has_host_dev = 0; |
1166 | ||
62112d18 MA |
1167 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1168 | switch (vc->info->type) { | |
2be64a68 | 1169 | case NET_CLIENT_OPTIONS_KIND_NIC: |
62112d18 MA |
1170 | has_nic = 1; |
1171 | break; | |
2be64a68 LE |
1172 | case NET_CLIENT_OPTIONS_KIND_USER: |
1173 | case NET_CLIENT_OPTIONS_KIND_TAP: | |
1174 | case NET_CLIENT_OPTIONS_KIND_SOCKET: | |
1175 | case NET_CLIENT_OPTIONS_KIND_VDE: | |
62112d18 MA |
1176 | has_host_dev = 1; |
1177 | break; | |
1178 | default: ; | |
1179 | } | |
1180 | } | |
1181 | if (has_host_dev && !has_nic) | |
63a01ef8 | 1182 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
62112d18 | 1183 | if (has_nic && !has_host_dev) |
63a01ef8 AL |
1184 | fprintf(stderr, |
1185 | "Warning: vlan %d is not connected to host network\n", | |
1186 | vlan->id); | |
1187 | } | |
efe32fdd MA |
1188 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1189 | if (!vc->peer) { | |
1190 | fprintf(stderr, "Warning: %s %s has no peer\n", | |
2be64a68 | 1191 | vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? "nic" : "netdev", |
efe32fdd MA |
1192 | vc->name); |
1193 | } | |
1194 | } | |
48e2faf2 PM |
1195 | |
1196 | /* Check that all NICs requested via -net nic actually got created. | |
1197 | * NICs created via -device don't need to be checked here because | |
1198 | * they are always instantiated. | |
1199 | */ | |
1200 | for (i = 0; i < MAX_NICS; i++) { | |
1201 | NICInfo *nd = &nd_table[i]; | |
1202 | if (nd->used && !nd->instantiated) { | |
1203 | fprintf(stderr, "Warning: requested NIC (%s, model %s) " | |
1204 | "was not created (not supported by this machine?)\n", | |
1205 | nd->name ? nd->name : "anonymous", | |
1206 | nd->model ? nd->model : "unspecified"); | |
1207 | } | |
1208 | } | |
63a01ef8 | 1209 | } |
dc1c9fe8 MM |
1210 | |
1211 | static int net_init_client(QemuOpts *opts, void *dummy) | |
1212 | { | |
4559a1db LC |
1213 | Error *local_err = NULL; |
1214 | ||
1215 | net_client_init(opts, 0, &local_err); | |
1216 | if (error_is_set(&local_err)) { | |
1217 | qerror_report_err(local_err); | |
1218 | error_free(local_err); | |
c1671a08 | 1219 | return -1; |
4559a1db LC |
1220 | } |
1221 | ||
c1671a08 | 1222 | return 0; |
f6b134ac MM |
1223 | } |
1224 | ||
1225 | static int net_init_netdev(QemuOpts *opts, void *dummy) | |
1226 | { | |
4559a1db LC |
1227 | Error *local_err = NULL; |
1228 | int ret; | |
1229 | ||
1230 | ret = net_client_init(opts, 1, &local_err); | |
1231 | if (error_is_set(&local_err)) { | |
1232 | qerror_report_err(local_err); | |
1233 | error_free(local_err); | |
1234 | return -1; | |
1235 | } | |
1236 | ||
1237 | return ret; | |
dc1c9fe8 MM |
1238 | } |
1239 | ||
1240 | int net_init_clients(void) | |
1241 | { | |
3329f07b GH |
1242 | QemuOptsList *net = qemu_find_opts("net"); |
1243 | ||
cb4522cc | 1244 | if (default_net) { |
dc1c9fe8 | 1245 | /* if no clients, we use a default config */ |
3329f07b | 1246 | qemu_opts_set(net, NULL, "type", "nic"); |
dc1c9fe8 | 1247 | #ifdef CONFIG_SLIRP |
3329f07b | 1248 | qemu_opts_set(net, NULL, "type", "user"); |
dc1c9fe8 MM |
1249 | #endif |
1250 | } | |
1251 | ||
5610c3aa | 1252 | QTAILQ_INIT(&vlans); |
577c4af9 | 1253 | QTAILQ_INIT(&non_vlan_clients); |
5610c3aa | 1254 | |
3329f07b | 1255 | if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1) |
f6b134ac MM |
1256 | return -1; |
1257 | ||
3329f07b | 1258 | if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) { |
dc1c9fe8 MM |
1259 | return -1; |
1260 | } | |
1261 | ||
dc1c9fe8 MM |
1262 | return 0; |
1263 | } | |
1264 | ||
7f161aae | 1265 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
dc1c9fe8 | 1266 | { |
a3a766e7 | 1267 | #if defined(CONFIG_SLIRP) |
68ac40d2 MM |
1268 | int ret; |
1269 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { | |
dc1c9fe8 MM |
1270 | return ret; |
1271 | } | |
a3a766e7 | 1272 | #endif |
68ac40d2 | 1273 | |
8212c64f | 1274 | if (!qemu_opts_parse(opts_list, optarg, 1)) { |
dc1c9fe8 MM |
1275 | return -1; |
1276 | } | |
1277 | ||
cb4522cc | 1278 | default_net = 0; |
dc1c9fe8 MM |
1279 | return 0; |
1280 | } | |
7fc8d918 JW |
1281 | |
1282 | /* From FreeBSD */ | |
1283 | /* XXX: optimize */ | |
1284 | unsigned compute_mcast_idx(const uint8_t *ep) | |
1285 | { | |
1286 | uint32_t crc; | |
1287 | int carry, i, j; | |
1288 | uint8_t b; | |
1289 | ||
1290 | crc = 0xffffffff; | |
1291 | for (i = 0; i < 6; i++) { | |
1292 | b = *ep++; | |
1293 | for (j = 0; j < 8; j++) { | |
1294 | carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); | |
1295 | crc <<= 1; | |
1296 | b >>= 1; | |
1297 | if (carry) { | |
1298 | crc = ((crc ^ POLYNOMIAL) | carry); | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | return crc >> 26; | |
1303 | } |