]>
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 BS |
34 | #include "monitor.h" |
35 | #include "sysemu.h" | |
1df49e04 | 36 | #include "qemu-common.h" |
511d2b14 | 37 | #include "qemu_socket.h" |
75422b0d | 38 | #include "hw/qdev.h" |
511d2b14 | 39 | |
5610c3aa | 40 | static QTAILQ_HEAD(, VLANState) vlans; |
577c4af9 | 41 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
63a01ef8 | 42 | |
cb4522cc GH |
43 | int default_net = 1; |
44 | ||
63a01ef8 AL |
45 | /***********************************************************/ |
46 | /* network device redirectors */ | |
47 | ||
68ac40d2 | 48 | #if defined(DEBUG_NET) |
63a01ef8 AL |
49 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
50 | { | |
51 | int len, i, j, c; | |
52 | ||
53 | for(i=0;i<size;i+=16) { | |
54 | len = size - i; | |
55 | if (len > 16) | |
56 | len = 16; | |
57 | fprintf(f, "%08x ", i); | |
58 | for(j=0;j<16;j++) { | |
59 | if (j < len) | |
60 | fprintf(f, " %02x", buf[i+j]); | |
61 | else | |
62 | fprintf(f, " "); | |
63 | } | |
64 | fprintf(f, " "); | |
65 | for(j=0;j<len;j++) { | |
66 | c = buf[i+j]; | |
67 | if (c < ' ' || c > '~') | |
68 | c = '.'; | |
69 | fprintf(f, "%c", c); | |
70 | } | |
71 | fprintf(f, "\n"); | |
72 | } | |
73 | } | |
74 | #endif | |
75 | ||
63a01ef8 AL |
76 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
77 | { | |
78 | const char *p, *p1; | |
79 | int len; | |
80 | p = *pp; | |
81 | p1 = strchr(p, sep); | |
82 | if (!p1) | |
83 | return -1; | |
84 | len = p1 - p; | |
85 | p1++; | |
86 | if (buf_size > 0) { | |
87 | if (len > buf_size - 1) | |
88 | len = buf_size - 1; | |
89 | memcpy(buf, p, len); | |
90 | buf[len] = '\0'; | |
91 | } | |
92 | *pp = p1; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | int parse_host_src_port(struct sockaddr_in *haddr, | |
97 | struct sockaddr_in *saddr, | |
98 | const char *input_str) | |
99 | { | |
6265eb26 | 100 | char *str = qemu_strdup(input_str); |
63a01ef8 AL |
101 | char *host_str = str; |
102 | char *src_str; | |
103 | const char *src_str2; | |
104 | char *ptr; | |
105 | ||
106 | /* | |
107 | * Chop off any extra arguments at the end of the string which | |
108 | * would start with a comma, then fill in the src port information | |
109 | * if it was provided else use the "any address" and "any port". | |
110 | */ | |
111 | if ((ptr = strchr(str,','))) | |
112 | *ptr = '\0'; | |
113 | ||
114 | if ((src_str = strchr(input_str,'@'))) { | |
115 | *src_str = '\0'; | |
116 | src_str++; | |
117 | } | |
118 | ||
119 | if (parse_host_port(haddr, host_str) < 0) | |
120 | goto fail; | |
121 | ||
122 | src_str2 = src_str; | |
123 | if (!src_str || *src_str == '\0') | |
124 | src_str2 = ":0"; | |
125 | ||
126 | if (parse_host_port(saddr, src_str2) < 0) | |
127 | goto fail; | |
128 | ||
129 | free(str); | |
130 | return(0); | |
131 | ||
132 | fail: | |
133 | free(str); | |
134 | return -1; | |
135 | } | |
136 | ||
137 | int parse_host_port(struct sockaddr_in *saddr, const char *str) | |
138 | { | |
139 | char buf[512]; | |
140 | struct hostent *he; | |
141 | const char *p, *r; | |
142 | int port; | |
143 | ||
144 | p = str; | |
145 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
146 | return -1; | |
147 | saddr->sin_family = AF_INET; | |
148 | if (buf[0] == '\0') { | |
149 | saddr->sin_addr.s_addr = 0; | |
150 | } else { | |
cd390083 | 151 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
152 | if (!inet_aton(buf, &saddr->sin_addr)) |
153 | return -1; | |
154 | } else { | |
155 | if ((he = gethostbyname(buf)) == NULL) | |
156 | return - 1; | |
157 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
158 | } | |
159 | } | |
160 | port = strtol(p, (char **)&r, 0); | |
161 | if (r == p) | |
162 | return -1; | |
163 | saddr->sin_port = htons(port); | |
164 | return 0; | |
165 | } | |
166 | ||
7cb7434b AL |
167 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
168 | { | |
169 | snprintf(vc->info_str, sizeof(vc->info_str), | |
4dda4063 AL |
170 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
171 | vc->model, | |
7cb7434b AL |
172 | macaddr[0], macaddr[1], macaddr[2], |
173 | macaddr[3], macaddr[4], macaddr[5]); | |
174 | } | |
175 | ||
76d32cba GH |
176 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
177 | { | |
178 | static int index = 0; | |
179 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; | |
180 | ||
181 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) | |
182 | return; | |
183 | macaddr->a[0] = 0x52; | |
184 | macaddr->a[1] = 0x54; | |
185 | macaddr->a[2] = 0x00; | |
186 | macaddr->a[3] = 0x12; | |
187 | macaddr->a[4] = 0x34; | |
188 | macaddr->a[5] = 0x56 + index++; | |
189 | } | |
190 | ||
676cff29 AL |
191 | static char *assign_name(VLANClientState *vc1, const char *model) |
192 | { | |
193 | VLANState *vlan; | |
194 | char buf[256]; | |
195 | int id = 0; | |
196 | ||
5610c3aa | 197 | QTAILQ_FOREACH(vlan, &vlans, next) { |
676cff29 AL |
198 | VLANClientState *vc; |
199 | ||
5610c3aa MM |
200 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
201 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
676cff29 | 202 | id++; |
5610c3aa MM |
203 | } |
204 | } | |
676cff29 AL |
205 | } |
206 | ||
207 | snprintf(buf, sizeof(buf), "%s.%d", model, id); | |
208 | ||
02374aa0 | 209 | return qemu_strdup(buf); |
676cff29 AL |
210 | } |
211 | ||
9a6ecb30 | 212 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 213 | unsigned flags, |
9a6ecb30 MM |
214 | const uint8_t *data, |
215 | size_t size, | |
216 | void *opaque); | |
217 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, | |
c0b8e49c | 218 | unsigned flags, |
9a6ecb30 MM |
219 | const struct iovec *iov, |
220 | int iovcnt, | |
221 | void *opaque); | |
222 | ||
45460d1a MM |
223 | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
224 | VLANState *vlan, | |
225 | VLANClientState *peer, | |
226 | const char *model, | |
227 | const char *name) | |
63a01ef8 | 228 | { |
5610c3aa MM |
229 | VLANClientState *vc; |
230 | ||
45460d1a MM |
231 | assert(info->size >= sizeof(VLANClientState)); |
232 | ||
233 | vc = qemu_mallocz(info->size); | |
5610c3aa | 234 | |
665a3b07 | 235 | vc->info = info; |
02374aa0 | 236 | vc->model = qemu_strdup(model); |
45460d1a | 237 | if (name) { |
02374aa0 | 238 | vc->name = qemu_strdup(name); |
45460d1a | 239 | } else { |
7a9f6e4a | 240 | vc->name = assign_name(vc, model); |
45460d1a | 241 | } |
5610c3aa | 242 | |
d80b9fc6 | 243 | if (vlan) { |
283c7c63 | 244 | assert(!peer); |
d80b9fc6 MM |
245 | vc->vlan = vlan; |
246 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); | |
577c4af9 | 247 | } else { |
283c7c63 MM |
248 | if (peer) { |
249 | vc->peer = peer; | |
250 | peer->peer = vc; | |
251 | } | |
577c4af9 | 252 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
9a6ecb30 MM |
253 | |
254 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, | |
255 | qemu_deliver_packet_iov, | |
256 | vc); | |
d80b9fc6 | 257 | } |
63a01ef8 | 258 | |
63a01ef8 AL |
259 | return vc; |
260 | } | |
261 | ||
ebef2c09 MM |
262 | NICState *qemu_new_nic(NetClientInfo *info, |
263 | NICConf *conf, | |
264 | const char *model, | |
265 | const char *name, | |
266 | void *opaque) | |
267 | { | |
268 | VLANClientState *nc; | |
269 | NICState *nic; | |
270 | ||
271 | assert(info->type == NET_CLIENT_TYPE_NIC); | |
272 | assert(info->size >= sizeof(NICState)); | |
273 | ||
274 | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); | |
275 | ||
276 | nic = DO_UPCAST(NICState, nc, nc); | |
277 | nic->conf = conf; | |
278 | nic->opaque = opaque; | |
279 | ||
280 | return nic; | |
281 | } | |
282 | ||
63a01ef8 AL |
283 | void qemu_del_vlan_client(VLANClientState *vc) |
284 | { | |
d80b9fc6 MM |
285 | if (vc->vlan) { |
286 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); | |
577c4af9 | 287 | } else { |
9a6ecb30 MM |
288 | if (vc->send_queue) { |
289 | qemu_del_net_queue(vc->send_queue); | |
290 | } | |
577c4af9 | 291 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
283c7c63 MM |
292 | if (vc->peer) { |
293 | vc->peer->peer = NULL; | |
294 | } | |
d80b9fc6 | 295 | } |
63a01ef8 | 296 | |
665a3b07 MM |
297 | if (vc->info->cleanup) { |
298 | vc->info->cleanup(vc); | |
5610c3aa MM |
299 | } |
300 | ||
301 | qemu_free(vc->name); | |
302 | qemu_free(vc->model); | |
303 | qemu_free(vc); | |
63a01ef8 AL |
304 | } |
305 | ||
68ac40d2 | 306 | VLANClientState * |
1a609520 JK |
307 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
308 | const char *client_str) | |
309 | { | |
310 | VLANState *vlan; | |
311 | VLANClientState *vc; | |
312 | ||
313 | vlan = qemu_find_vlan(vlan_id, 0); | |
314 | if (!vlan) { | |
315 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); | |
316 | return NULL; | |
317 | } | |
318 | ||
5610c3aa | 319 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1a609520 JK |
320 | if (!strcmp(vc->name, client_str)) { |
321 | break; | |
322 | } | |
323 | } | |
324 | if (!vc) { | |
325 | monitor_printf(mon, "can't find device %s on VLAN %d\n", | |
326 | client_str, vlan_id); | |
327 | } | |
328 | ||
329 | return vc; | |
330 | } | |
331 | ||
57f9ef17 MM |
332 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
333 | { | |
334 | VLANClientState *nc; | |
335 | VLANState *vlan; | |
336 | ||
337 | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { | |
338 | if (nc->info->type == NET_CLIENT_TYPE_NIC) { | |
339 | func(DO_UPCAST(NICState, nc, nc), opaque); | |
340 | } | |
341 | } | |
342 | ||
343 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
344 | QTAILQ_FOREACH(nc, &vlan->clients, next) { | |
345 | if (nc->info->type == NET_CLIENT_TYPE_NIC) { | |
346 | func(DO_UPCAST(NICState, nc, nc), opaque); | |
347 | } | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
2e1e0641 | 352 | int qemu_can_send_packet(VLANClientState *sender) |
63a01ef8 | 353 | { |
2e1e0641 | 354 | VLANState *vlan = sender->vlan; |
63a01ef8 AL |
355 | VLANClientState *vc; |
356 | ||
9a6ecb30 | 357 | if (sender->peer) { |
893379ef MM |
358 | if (sender->peer->receive_disabled) { |
359 | return 0; | |
665a3b07 MM |
360 | } else if (sender->peer->info->can_receive && |
361 | !sender->peer->info->can_receive(sender->peer)) { | |
9a6ecb30 | 362 | return 0; |
893379ef MM |
363 | } else { |
364 | return 1; | |
9a6ecb30 MM |
365 | } |
366 | } | |
367 | ||
d80b9fc6 MM |
368 | if (!sender->vlan) { |
369 | return 1; | |
370 | } | |
371 | ||
5610c3aa | 372 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
2e1e0641 MM |
373 | if (vc == sender) { |
374 | continue; | |
375 | } | |
376 | ||
cda9046b | 377 | /* no can_receive() handler, they can always receive */ |
665a3b07 | 378 | if (!vc->info->can_receive || vc->info->can_receive(vc)) { |
2e1e0641 | 379 | return 1; |
63a01ef8 AL |
380 | } |
381 | } | |
382 | return 0; | |
383 | } | |
384 | ||
9a6ecb30 | 385 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 386 | unsigned flags, |
9a6ecb30 MM |
387 | const uint8_t *data, |
388 | size_t size, | |
389 | void *opaque) | |
390 | { | |
391 | VLANClientState *vc = opaque; | |
893379ef | 392 | ssize_t ret; |
9a6ecb30 MM |
393 | |
394 | if (vc->link_down) { | |
395 | return size; | |
396 | } | |
397 | ||
893379ef MM |
398 | if (vc->receive_disabled) { |
399 | return 0; | |
400 | } | |
401 | ||
665a3b07 MM |
402 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
403 | ret = vc->info->receive_raw(vc, data, size); | |
893379ef | 404 | } else { |
665a3b07 | 405 | ret = vc->info->receive(vc, data, size); |
893379ef MM |
406 | } |
407 | ||
408 | if (ret == 0) { | |
409 | vc->receive_disabled = 1; | |
410 | }; | |
411 | ||
412 | return ret; | |
9a6ecb30 MM |
413 | } |
414 | ||
f7105843 | 415 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
c0b8e49c | 416 | unsigned flags, |
f7105843 MM |
417 | const uint8_t *buf, |
418 | size_t size, | |
419 | void *opaque) | |
63a01ef8 | 420 | { |
f7105843 | 421 | VLANState *vlan = opaque; |
63a01ef8 | 422 | VLANClientState *vc; |
893379ef | 423 | ssize_t ret = -1; |
63a01ef8 | 424 | |
f7105843 | 425 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3e021d40 MM |
426 | ssize_t len; |
427 | ||
428 | if (vc == sender) { | |
429 | continue; | |
764a4d1d | 430 | } |
3e021d40 MM |
431 | |
432 | if (vc->link_down) { | |
433 | ret = size; | |
434 | continue; | |
435 | } | |
436 | ||
893379ef MM |
437 | if (vc->receive_disabled) { |
438 | ret = 0; | |
439 | continue; | |
440 | } | |
441 | ||
665a3b07 MM |
442 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) { |
443 | len = vc->info->receive_raw(vc, buf, size); | |
893379ef | 444 | } else { |
665a3b07 | 445 | len = vc->info->receive(vc, buf, size); |
893379ef MM |
446 | } |
447 | ||
448 | if (len == 0) { | |
449 | vc->receive_disabled = 1; | |
450 | } | |
3e021d40 MM |
451 | |
452 | ret = (ret >= 0) ? ret : len; | |
893379ef | 453 | |
764a4d1d | 454 | } |
3e021d40 MM |
455 | |
456 | return ret; | |
764a4d1d AL |
457 | } |
458 | ||
8cad5516 MM |
459 | void qemu_purge_queued_packets(VLANClientState *vc) |
460 | { | |
9a6ecb30 MM |
461 | NetQueue *queue; |
462 | ||
463 | if (!vc->peer && !vc->vlan) { | |
d80b9fc6 | 464 | return; |
9a6ecb30 | 465 | } |
d80b9fc6 | 466 | |
9a6ecb30 MM |
467 | if (vc->peer) { |
468 | queue = vc->peer->send_queue; | |
469 | } else { | |
470 | queue = vc->vlan->send_queue; | |
471 | } | |
472 | ||
473 | qemu_net_queue_purge(queue, vc); | |
8cad5516 MM |
474 | } |
475 | ||
f3b6c7fc | 476 | void qemu_flush_queued_packets(VLANClientState *vc) |
e94667b9 | 477 | { |
9a6ecb30 MM |
478 | NetQueue *queue; |
479 | ||
893379ef MM |
480 | vc->receive_disabled = 0; |
481 | ||
9a6ecb30 MM |
482 | if (vc->vlan) { |
483 | queue = vc->vlan->send_queue; | |
484 | } else { | |
485 | queue = vc->send_queue; | |
486 | } | |
d80b9fc6 | 487 | |
9a6ecb30 | 488 | qemu_net_queue_flush(queue); |
e94667b9 MM |
489 | } |
490 | ||
ca77d175 MM |
491 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
492 | unsigned flags, | |
493 | const uint8_t *buf, int size, | |
494 | NetPacketSent *sent_cb) | |
764a4d1d | 495 | { |
9a6ecb30 | 496 | NetQueue *queue; |
436e5e53 | 497 | |
63a01ef8 | 498 | #ifdef DEBUG_NET |
d80b9fc6 | 499 | printf("qemu_send_packet_async:\n"); |
63a01ef8 AL |
500 | hex_dump(stdout, buf, size); |
501 | #endif | |
f3b6c7fc | 502 | |
9a6ecb30 MM |
503 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
504 | return size; | |
505 | } | |
506 | ||
507 | if (sender->peer) { | |
508 | queue = sender->peer->send_queue; | |
509 | } else { | |
510 | queue = sender->vlan->send_queue; | |
511 | } | |
512 | ||
ca77d175 MM |
513 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
514 | } | |
515 | ||
516 | ssize_t qemu_send_packet_async(VLANClientState *sender, | |
517 | const uint8_t *buf, int size, | |
518 | NetPacketSent *sent_cb) | |
519 | { | |
520 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, | |
521 | buf, size, sent_cb); | |
f3b6c7fc MM |
522 | } |
523 | ||
524 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
525 | { | |
526 | qemu_send_packet_async(vc, buf, size, NULL); | |
63a01ef8 AL |
527 | } |
528 | ||
ca77d175 MM |
529 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
530 | { | |
531 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, | |
532 | buf, size, NULL); | |
533 | } | |
534 | ||
fbe78f4f AL |
535 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
536 | int iovcnt) | |
537 | { | |
538 | uint8_t buffer[4096]; | |
539 | size_t offset = 0; | |
540 | int i; | |
541 | ||
542 | for (i = 0; i < iovcnt; i++) { | |
543 | size_t len; | |
544 | ||
545 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); | |
546 | memcpy(buffer + offset, iov[i].iov_base, len); | |
547 | offset += len; | |
548 | } | |
549 | ||
665a3b07 | 550 | return vc->info->receive(vc, buffer, offset); |
fbe78f4f AL |
551 | } |
552 | ||
e0e7877a AL |
553 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
554 | { | |
555 | size_t offset = 0; | |
556 | int i; | |
557 | ||
558 | for (i = 0; i < iovcnt; i++) | |
559 | offset += iov[i].iov_len; | |
560 | return offset; | |
561 | } | |
562 | ||
9a6ecb30 | 563 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 564 | unsigned flags, |
9a6ecb30 MM |
565 | const struct iovec *iov, |
566 | int iovcnt, | |
567 | void *opaque) | |
568 | { | |
569 | VLANClientState *vc = opaque; | |
570 | ||
571 | if (vc->link_down) { | |
572 | return calc_iov_length(iov, iovcnt); | |
573 | } | |
574 | ||
665a3b07 MM |
575 | if (vc->info->receive_iov) { |
576 | return vc->info->receive_iov(vc, iov, iovcnt); | |
9a6ecb30 MM |
577 | } else { |
578 | return vc_sendv_compat(vc, iov, iovcnt); | |
579 | } | |
580 | } | |
581 | ||
f7105843 | 582 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 583 | unsigned flags, |
f7105843 MM |
584 | const struct iovec *iov, |
585 | int iovcnt, | |
586 | void *opaque) | |
fbe78f4f | 587 | { |
f7105843 | 588 | VLANState *vlan = opaque; |
fbe78f4f | 589 | VLANClientState *vc; |
f7105843 | 590 | ssize_t ret = -1; |
e94667b9 | 591 | |
f7105843 | 592 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
e94667b9 MM |
593 | ssize_t len; |
594 | ||
595 | if (vc == sender) { | |
596 | continue; | |
597 | } | |
598 | ||
599 | if (vc->link_down) { | |
600 | ret = calc_iov_length(iov, iovcnt); | |
601 | continue; | |
602 | } | |
603 | ||
ca77d175 MM |
604 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
605 | ||
665a3b07 MM |
606 | if (vc->info->receive_iov) { |
607 | len = vc->info->receive_iov(vc, iov, iovcnt); | |
e94667b9 MM |
608 | } else { |
609 | len = vc_sendv_compat(vc, iov, iovcnt); | |
610 | } | |
611 | ||
612 | ret = (ret >= 0) ? ret : len; | |
613 | } | |
614 | ||
e94667b9 MM |
615 | return ret; |
616 | } | |
617 | ||
f3b6c7fc MM |
618 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
619 | const struct iovec *iov, int iovcnt, | |
620 | NetPacketSent *sent_cb) | |
e94667b9 | 621 | { |
9a6ecb30 MM |
622 | NetQueue *queue; |
623 | ||
624 | if (sender->link_down || (!sender->peer && !sender->vlan)) { | |
e94667b9 MM |
625 | return calc_iov_length(iov, iovcnt); |
626 | } | |
627 | ||
9a6ecb30 MM |
628 | if (sender->peer) { |
629 | queue = sender->peer->send_queue; | |
630 | } else { | |
631 | queue = sender->vlan->send_queue; | |
632 | } | |
633 | ||
c0b8e49c MM |
634 | return qemu_net_queue_send_iov(queue, sender, |
635 | QEMU_NET_PACKET_FLAG_NONE, | |
636 | iov, iovcnt, sent_cb); | |
fbe78f4f AL |
637 | } |
638 | ||
f3b6c7fc MM |
639 | ssize_t |
640 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
641 | { | |
642 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
643 | } | |
644 | ||
63a01ef8 | 645 | /* find or alloc a new VLAN */ |
1a609520 | 646 | VLANState *qemu_find_vlan(int id, int allocate) |
63a01ef8 | 647 | { |
5610c3aa MM |
648 | VLANState *vlan; |
649 | ||
650 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
651 | if (vlan->id == id) { | |
63a01ef8 | 652 | return vlan; |
5610c3aa | 653 | } |
63a01ef8 | 654 | } |
5610c3aa | 655 | |
1a609520 JK |
656 | if (!allocate) { |
657 | return NULL; | |
658 | } | |
5610c3aa | 659 | |
63a01ef8 | 660 | vlan = qemu_mallocz(sizeof(VLANState)); |
63a01ef8 | 661 | vlan->id = id; |
5610c3aa | 662 | QTAILQ_INIT(&vlan->clients); |
f7105843 MM |
663 | |
664 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, | |
665 | qemu_vlan_deliver_packet_iov, | |
666 | vlan); | |
5610c3aa MM |
667 | |
668 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); | |
669 | ||
63a01ef8 AL |
670 | return vlan; |
671 | } | |
672 | ||
2ef924b4 | 673 | VLANClientState *qemu_find_netdev(const char *id) |
5869c4d5 MM |
674 | { |
675 | VLANClientState *vc; | |
676 | ||
677 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
678 | if (!strcmp(vc->name, id)) { | |
679 | return vc; | |
680 | } | |
681 | } | |
682 | ||
683 | return NULL; | |
684 | } | |
685 | ||
7697079b AL |
686 | static int nic_get_free_idx(void) |
687 | { | |
688 | int index; | |
689 | ||
690 | for (index = 0; index < MAX_NICS; index++) | |
691 | if (!nd_table[index].used) | |
692 | return index; | |
693 | return -1; | |
694 | } | |
695 | ||
07caea31 MA |
696 | int qemu_show_nic_models(const char *arg, const char *const *models) |
697 | { | |
698 | int i; | |
699 | ||
700 | if (!arg || strcmp(arg, "?")) | |
701 | return 0; | |
702 | ||
703 | fprintf(stderr, "qemu: Supported NIC models: "); | |
704 | for (i = 0 ; models[i]; i++) | |
705 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
706 | return 1; | |
707 | } | |
708 | ||
d07f22c5 AL |
709 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
710 | { | |
711 | const char *models[2]; | |
712 | ||
713 | models[0] = model; | |
714 | models[1] = NULL; | |
715 | ||
07caea31 MA |
716 | if (qemu_show_nic_models(nd->model, models)) |
717 | exit(0); | |
718 | if (qemu_find_nic_model(nd, models, model) < 0) | |
719 | exit(1); | |
d07f22c5 AL |
720 | } |
721 | ||
07caea31 MA |
722 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
723 | const char *default_model) | |
d07f22c5 | 724 | { |
07caea31 | 725 | int i; |
d07f22c5 AL |
726 | |
727 | if (!nd->model) | |
32a8e14a | 728 | nd->model = qemu_strdup(default_model); |
d07f22c5 | 729 | |
07caea31 MA |
730 | for (i = 0 ; models[i]; i++) { |
731 | if (strcmp(nd->model, models[i]) == 0) | |
732 | return i; | |
d07f22c5 AL |
733 | } |
734 | ||
07caea31 MA |
735 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); |
736 | return -1; | |
d07f22c5 AL |
737 | } |
738 | ||
5281d757 | 739 | int net_handle_fd_param(Monitor *mon, const char *param) |
c1d6eed7 MM |
740 | { |
741 | if (!qemu_isdigit(param[0])) { | |
742 | int fd; | |
743 | ||
744 | fd = monitor_get_fd(mon, param); | |
745 | if (fd == -1) { | |
fb12577c | 746 | qemu_error("No file descriptor named %s found", param); |
c1d6eed7 MM |
747 | return -1; |
748 | } | |
749 | ||
750 | return fd; | |
751 | } else { | |
752 | return strtol(param, NULL, 0); | |
753 | } | |
754 | } | |
755 | ||
f6b134ac MM |
756 | static int net_init_nic(QemuOpts *opts, |
757 | Monitor *mon, | |
758 | const char *name, | |
759 | VLANState *vlan) | |
f83c6e10 MM |
760 | { |
761 | int idx; | |
762 | NICInfo *nd; | |
5869c4d5 | 763 | const char *netdev; |
f83c6e10 MM |
764 | |
765 | idx = nic_get_free_idx(); | |
766 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
767 | qemu_error("Too Many NICs\n"); | |
768 | return -1; | |
769 | } | |
770 | ||
771 | nd = &nd_table[idx]; | |
772 | ||
773 | memset(nd, 0, sizeof(*nd)); | |
774 | ||
5869c4d5 MM |
775 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
776 | nd->netdev = qemu_find_netdev(netdev); | |
777 | if (!nd->netdev) { | |
778 | qemu_error("netdev '%s' not found\n", netdev); | |
779 | return -1; | |
780 | } | |
781 | } else { | |
782 | assert(vlan); | |
783 | nd->vlan = vlan; | |
784 | } | |
6d952ebe MM |
785 | if (name) { |
786 | nd->name = qemu_strdup(name); | |
787 | } | |
f83c6e10 MM |
788 | if (qemu_opt_get(opts, "model")) { |
789 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); | |
790 | } | |
791 | if (qemu_opt_get(opts, "addr")) { | |
792 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); | |
793 | } | |
794 | ||
795 | nd->macaddr[0] = 0x52; | |
796 | nd->macaddr[1] = 0x54; | |
797 | nd->macaddr[2] = 0x00; | |
798 | nd->macaddr[3] = 0x12; | |
799 | nd->macaddr[4] = 0x34; | |
800 | nd->macaddr[5] = 0x56 + idx; | |
801 | ||
802 | if (qemu_opt_get(opts, "macaddr") && | |
f1d078c3 | 803 | net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
f83c6e10 MM |
804 | qemu_error("invalid syntax for ethernet address\n"); |
805 | return -1; | |
806 | } | |
807 | ||
75422b0d AS |
808 | nd->nvectors = qemu_opt_get_number(opts, "vectors", |
809 | DEV_NVECTORS_UNSPECIFIED); | |
810 | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && | |
f83c6e10 MM |
811 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
812 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); | |
813 | return -1; | |
814 | } | |
815 | ||
816 | nd->used = 1; | |
f83c6e10 MM |
817 | nb_nics++; |
818 | ||
819 | return idx; | |
820 | } | |
821 | ||
822 | #define NET_COMMON_PARAMS_DESC \ | |
823 | { \ | |
824 | .name = "type", \ | |
825 | .type = QEMU_OPT_STRING, \ | |
826 | .help = "net client type (nic, tap etc.)", \ | |
827 | }, { \ | |
828 | .name = "vlan", \ | |
829 | .type = QEMU_OPT_NUMBER, \ | |
830 | .help = "vlan number", \ | |
831 | }, { \ | |
832 | .name = "name", \ | |
833 | .type = QEMU_OPT_STRING, \ | |
834 | .help = "identifier for monitor commands", \ | |
835 | } | |
836 | ||
6d952ebe MM |
837 | typedef int (*net_client_init_func)(QemuOpts *opts, |
838 | Monitor *mon, | |
f6b134ac MM |
839 | const char *name, |
840 | VLANState *vlan); | |
f83c6e10 MM |
841 | |
842 | /* magic number, but compiler will warn if too small */ | |
843 | #define NET_MAX_DESC 20 | |
844 | ||
238431a9 | 845 | static const struct { |
f83c6e10 MM |
846 | const char *type; |
847 | net_client_init_func init; | |
848 | QemuOptDesc desc[NET_MAX_DESC]; | |
849 | } net_client_types[] = { | |
850 | { | |
851 | .type = "none", | |
852 | .desc = { | |
853 | NET_COMMON_PARAMS_DESC, | |
854 | { /* end of list */ } | |
855 | }, | |
856 | }, { | |
857 | .type = "nic", | |
858 | .init = net_init_nic, | |
859 | .desc = { | |
860 | NET_COMMON_PARAMS_DESC, | |
5869c4d5 MM |
861 | { |
862 | .name = "netdev", | |
863 | .type = QEMU_OPT_STRING, | |
864 | .help = "id of -netdev to connect to", | |
865 | }, | |
f83c6e10 MM |
866 | { |
867 | .name = "macaddr", | |
868 | .type = QEMU_OPT_STRING, | |
869 | .help = "MAC address", | |
870 | }, { | |
871 | .name = "model", | |
872 | .type = QEMU_OPT_STRING, | |
873 | .help = "device model (e1000, rtl8139, virtio etc.)", | |
874 | }, { | |
875 | .name = "addr", | |
876 | .type = QEMU_OPT_STRING, | |
877 | .help = "PCI device address", | |
878 | }, { | |
879 | .name = "vectors", | |
880 | .type = QEMU_OPT_NUMBER, | |
881 | .help = "number of MSI-x vectors, 0 to disable MSI-X", | |
882 | }, | |
883 | { /* end of list */ } | |
884 | }, | |
ec302ffd MM |
885 | #ifdef CONFIG_SLIRP |
886 | }, { | |
887 | .type = "user", | |
888 | .init = net_init_slirp, | |
889 | .desc = { | |
890 | NET_COMMON_PARAMS_DESC, | |
891 | { | |
892 | .name = "hostname", | |
893 | .type = QEMU_OPT_STRING, | |
894 | .help = "client hostname reported by the builtin DHCP server", | |
895 | }, { | |
896 | .name = "restrict", | |
897 | .type = QEMU_OPT_STRING, | |
898 | .help = "isolate the guest from the host (y|yes|n|no)", | |
899 | }, { | |
900 | .name = "ip", | |
901 | .type = QEMU_OPT_STRING, | |
902 | .help = "legacy parameter, use net= instead", | |
903 | }, { | |
904 | .name = "net", | |
905 | .type = QEMU_OPT_STRING, | |
906 | .help = "IP address and optional netmask", | |
907 | }, { | |
908 | .name = "host", | |
909 | .type = QEMU_OPT_STRING, | |
910 | .help = "guest-visible address of the host", | |
911 | }, { | |
912 | .name = "tftp", | |
913 | .type = QEMU_OPT_STRING, | |
914 | .help = "root directory of the built-in TFTP server", | |
915 | }, { | |
916 | .name = "bootfile", | |
917 | .type = QEMU_OPT_STRING, | |
918 | .help = "BOOTP filename, for use with tftp=", | |
919 | }, { | |
920 | .name = "dhcpstart", | |
921 | .type = QEMU_OPT_STRING, | |
922 | .help = "the first of the 16 IPs the built-in DHCP server can assign", | |
923 | }, { | |
924 | .name = "dns", | |
925 | .type = QEMU_OPT_STRING, | |
926 | .help = "guest-visible address of the virtual nameserver", | |
927 | }, { | |
928 | .name = "smb", | |
929 | .type = QEMU_OPT_STRING, | |
930 | .help = "root directory of the built-in SMB server", | |
931 | }, { | |
932 | .name = "smbserver", | |
933 | .type = QEMU_OPT_STRING, | |
934 | .help = "IP address of the built-in SMB server", | |
935 | }, { | |
936 | .name = "hostfwd", | |
937 | .type = QEMU_OPT_STRING, | |
938 | .help = "guest port number to forward incoming TCP or UDP connections", | |
939 | }, { | |
940 | .name = "guestfwd", | |
941 | .type = QEMU_OPT_STRING, | |
942 | .help = "IP address and port to forward guest TCP connections", | |
943 | }, | |
944 | { /* end of list */ } | |
945 | }, | |
8a1c5235 | 946 | #endif |
8a1c5235 MM |
947 | }, { |
948 | .type = "tap", | |
a8ed73f7 | 949 | .init = net_init_tap, |
8a1c5235 MM |
950 | .desc = { |
951 | NET_COMMON_PARAMS_DESC, | |
952 | { | |
953 | .name = "ifname", | |
954 | .type = QEMU_OPT_STRING, | |
955 | .help = "interface name", | |
956 | }, | |
a8ed73f7 | 957 | #ifndef _WIN32 |
8a1c5235 MM |
958 | { |
959 | .name = "fd", | |
960 | .type = QEMU_OPT_STRING, | |
961 | .help = "file descriptor of an already opened tap", | |
8a1c5235 MM |
962 | }, { |
963 | .name = "script", | |
964 | .type = QEMU_OPT_STRING, | |
965 | .help = "script to initialize the interface", | |
966 | }, { | |
967 | .name = "downscript", | |
968 | .type = QEMU_OPT_STRING, | |
969 | .help = "script to shut down the interface", | |
8a1c5235 MM |
970 | }, { |
971 | .name = "sndbuf", | |
972 | .type = QEMU_OPT_SIZE, | |
973 | .help = "send buffer limit" | |
baf74c95 MM |
974 | }, { |
975 | .name = "vnet_hdr", | |
976 | .type = QEMU_OPT_BOOL, | |
977 | .help = "enable the IFF_VNET_HDR flag on the tap interface" | |
8a1c5235 | 978 | }, |
a8ed73f7 | 979 | #endif /* _WIN32 */ |
8a1c5235 MM |
980 | { /* end of list */ } |
981 | }, | |
88ce16ca MM |
982 | }, { |
983 | .type = "socket", | |
984 | .init = net_init_socket, | |
985 | .desc = { | |
986 | NET_COMMON_PARAMS_DESC, | |
987 | { | |
988 | .name = "fd", | |
989 | .type = QEMU_OPT_STRING, | |
990 | .help = "file descriptor of an already opened socket", | |
991 | }, { | |
992 | .name = "listen", | |
993 | .type = QEMU_OPT_STRING, | |
994 | .help = "port number, and optional hostname, to listen on", | |
995 | }, { | |
996 | .name = "connect", | |
997 | .type = QEMU_OPT_STRING, | |
998 | .help = "port number, and optional hostname, to connect to", | |
999 | }, { | |
1000 | .name = "mcast", | |
1001 | .type = QEMU_OPT_STRING, | |
1002 | .help = "UDP multicast address and port number", | |
1003 | }, | |
1004 | { /* end of list */ } | |
1005 | }, | |
dd51058d MM |
1006 | #ifdef CONFIG_VDE |
1007 | }, { | |
1008 | .type = "vde", | |
1009 | .init = net_init_vde, | |
1010 | .desc = { | |
1011 | NET_COMMON_PARAMS_DESC, | |
1012 | { | |
1013 | .name = "sock", | |
1014 | .type = QEMU_OPT_STRING, | |
1015 | .help = "socket path", | |
1016 | }, { | |
1017 | .name = "port", | |
1018 | .type = QEMU_OPT_NUMBER, | |
1019 | .help = "port number", | |
1020 | }, { | |
1021 | .name = "group", | |
1022 | .type = QEMU_OPT_STRING, | |
1023 | .help = "group owner of socket", | |
1024 | }, { | |
1025 | .name = "mode", | |
1026 | .type = QEMU_OPT_NUMBER, | |
1027 | .help = "permissions for socket", | |
1028 | }, | |
1029 | { /* end of list */ } | |
1030 | }, | |
1031 | #endif | |
ed2955c2 MM |
1032 | }, { |
1033 | .type = "dump", | |
1034 | .init = net_init_dump, | |
1035 | .desc = { | |
1036 | NET_COMMON_PARAMS_DESC, | |
1037 | { | |
1038 | .name = "len", | |
1039 | .type = QEMU_OPT_SIZE, | |
1040 | .help = "per-packet size limit (64k default)", | |
1041 | }, { | |
1042 | .name = "file", | |
1043 | .type = QEMU_OPT_STRING, | |
1044 | .help = "dump file path (default is qemu-vlan0.pcap)", | |
1045 | }, | |
1046 | { /* end of list */ } | |
1047 | }, | |
f83c6e10 MM |
1048 | }, |
1049 | { /* end of list */ } | |
1050 | }; | |
1051 | ||
f6b134ac | 1052 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
f83c6e10 | 1053 | { |
6d952ebe | 1054 | const char *name; |
f83c6e10 MM |
1055 | const char *type; |
1056 | int i; | |
1057 | ||
1058 | type = qemu_opt_get(opts, "type"); | |
f83c6e10 | 1059 | |
0f2fbf40 MM |
1060 | if (!is_netdev) { |
1061 | if (!type) { | |
1062 | qemu_error("No type specified for -net\n"); | |
1063 | return -1; | |
1064 | } | |
1065 | } else { | |
1066 | if (!type) { | |
1067 | qemu_error("No type specified for -netdev\n"); | |
1068 | return -1; | |
1069 | } | |
1070 | ||
f6b134ac MM |
1071 | if (strcmp(type, "tap") != 0 && |
1072 | #ifdef CONFIG_SLIRP | |
1073 | strcmp(type, "user") != 0 && | |
1074 | #endif | |
1075 | #ifdef CONFIG_VDE | |
1076 | strcmp(type, "vde") != 0 && | |
1077 | #endif | |
1078 | strcmp(type, "socket") != 0) { | |
1079 | qemu_error("The '%s' network backend type is not valid with -netdev\n", | |
1080 | type); | |
1081 | return -1; | |
1082 | } | |
1083 | ||
1084 | if (qemu_opt_get(opts, "vlan")) { | |
1085 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); | |
1086 | return -1; | |
1087 | } | |
1088 | if (qemu_opt_get(opts, "name")) { | |
1089 | qemu_error("The 'name' parameter is not valid with -netdev\n"); | |
1090 | return -1; | |
1091 | } | |
1092 | if (!qemu_opts_id(opts)) { | |
1093 | qemu_error("The id= parameter is required with -netdev\n"); | |
1094 | return -1; | |
1095 | } | |
1096 | } | |
1097 | ||
6d952ebe MM |
1098 | name = qemu_opts_id(opts); |
1099 | if (!name) { | |
1100 | name = qemu_opt_get(opts, "name"); | |
1101 | } | |
1102 | ||
f83c6e10 MM |
1103 | for (i = 0; net_client_types[i].type != NULL; i++) { |
1104 | if (!strcmp(net_client_types[i].type, type)) { | |
f6b134ac MM |
1105 | VLANState *vlan = NULL; |
1106 | ||
f83c6e10 MM |
1107 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1108 | return -1; | |
1109 | } | |
1110 | ||
5869c4d5 MM |
1111 | /* Do not add to a vlan if it's a -netdev or a nic with a |
1112 | * netdev= parameter. */ | |
1113 | if (!(is_netdev || | |
1114 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { | |
f6b134ac MM |
1115 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1116 | } | |
1117 | ||
f83c6e10 | 1118 | if (net_client_types[i].init) { |
f6b134ac | 1119 | return net_client_types[i].init(opts, mon, name, vlan); |
f83c6e10 MM |
1120 | } else { |
1121 | return 0; | |
1122 | } | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | qemu_error("Invalid -net type '%s'\n", type); | |
1127 | return -1; | |
1128 | } | |
1129 | ||
6f338c34 AL |
1130 | static int net_host_check_device(const char *device) |
1131 | { | |
1132 | int i; | |
bb9ea79e | 1133 | const char *valid_param_list[] = { "tap", "socket", "dump" |
6f338c34 AL |
1134 | #ifdef CONFIG_SLIRP |
1135 | ,"user" | |
1136 | #endif | |
1137 | #ifdef CONFIG_VDE | |
1138 | ,"vde" | |
1139 | #endif | |
1140 | }; | |
1141 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
1142 | if (!strncmp(valid_param_list[i], device, | |
1143 | strlen(valid_param_list[i]))) | |
1144 | return 1; | |
1145 | } | |
1146 | ||
1147 | return 0; | |
1148 | } | |
1149 | ||
f18c16de | 1150 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
6f338c34 | 1151 | { |
f18c16de | 1152 | const char *device = qdict_get_str(qdict, "device"); |
7f1c9d20 MM |
1153 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1154 | QemuOpts *opts; | |
f18c16de | 1155 | |
6f338c34 | 1156 | if (!net_host_check_device(device)) { |
376253ec | 1157 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
1158 | return; |
1159 | } | |
7f1c9d20 MM |
1160 | |
1161 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); | |
1162 | if (!opts) { | |
1163 | monitor_printf(mon, "parsing network options '%s' failed\n", | |
1164 | opts_str ? opts_str : ""); | |
1165 | return; | |
1166 | } | |
1167 | ||
1168 | qemu_opt_set(opts, "type", device); | |
1169 | ||
f6b134ac | 1170 | if (net_client_init(mon, opts, 0) < 0) { |
5c8be678 AL |
1171 | monitor_printf(mon, "adding host network device %s failed\n", device); |
1172 | } | |
6f338c34 AL |
1173 | } |
1174 | ||
f18c16de | 1175 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
6f338c34 | 1176 | { |
6f338c34 | 1177 | VLANClientState *vc; |
f18c16de LC |
1178 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1179 | const char *device = qdict_get_str(qdict, "device"); | |
6f338c34 | 1180 | |
1a609520 | 1181 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
6f338c34 | 1182 | if (!vc) { |
6f338c34 AL |
1183 | return; |
1184 | } | |
e8f1f9db AL |
1185 | if (!net_host_check_device(vc->model)) { |
1186 | monitor_printf(mon, "invalid host network device %s\n", device); | |
1187 | return; | |
1188 | } | |
6f338c34 AL |
1189 | qemu_del_vlan_client(vc); |
1190 | } | |
1191 | ||
406c8df3 GC |
1192 | void net_set_boot_mask(int net_boot_mask) |
1193 | { | |
1194 | int i; | |
1195 | ||
1196 | /* Only the first four NICs may be bootable */ | |
1197 | net_boot_mask = net_boot_mask & 0xF; | |
1198 | ||
1199 | for (i = 0; i < nb_nics; i++) { | |
1200 | if (net_boot_mask & (1 << i)) { | |
1201 | nd_table[i].bootable = 1; | |
1202 | net_boot_mask &= ~(1 << i); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | if (net_boot_mask) { | |
1207 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); | |
1208 | exit(1); | |
1209 | } | |
1210 | } | |
1211 | ||
376253ec | 1212 | void do_info_network(Monitor *mon) |
63a01ef8 AL |
1213 | { |
1214 | VLANState *vlan; | |
a0104e0e | 1215 | VLANClientState *vc; |
63a01ef8 | 1216 | |
5610c3aa | 1217 | QTAILQ_FOREACH(vlan, &vlans, next) { |
376253ec | 1218 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
5610c3aa MM |
1219 | |
1220 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
376253ec | 1221 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
5610c3aa | 1222 | } |
63a01ef8 | 1223 | } |
a0104e0e MA |
1224 | monitor_printf(mon, "Devices not on any VLAN:\n"); |
1225 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
1226 | monitor_printf(mon, " %s: %s", vc->name, vc->info_str); | |
1227 | if (vc->peer) { | |
1228 | monitor_printf(mon, " peer=%s", vc->peer->name); | |
1229 | } | |
1230 | monitor_printf(mon, "\n"); | |
1231 | } | |
63a01ef8 AL |
1232 | } |
1233 | ||
f18c16de | 1234 | void do_set_link(Monitor *mon, const QDict *qdict) |
436e5e53 AL |
1235 | { |
1236 | VLANState *vlan; | |
1237 | VLANClientState *vc = NULL; | |
f18c16de LC |
1238 | const char *name = qdict_get_str(qdict, "name"); |
1239 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); | |
436e5e53 | 1240 | |
5610c3aa MM |
1241 | QTAILQ_FOREACH(vlan, &vlans, next) { |
1242 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
1243 | if (strcmp(vc->name, name) == 0) { | |
dd5de373 | 1244 | goto done; |
5610c3aa MM |
1245 | } |
1246 | } | |
1247 | } | |
2583ba97 | 1248 | vc = qemu_find_netdev(name); |
dd5de373 | 1249 | done: |
436e5e53 AL |
1250 | |
1251 | if (!vc) { | |
7dc3fa09 | 1252 | monitor_printf(mon, "could not find network device '%s'\n", name); |
c3cf0d3f | 1253 | return; |
436e5e53 AL |
1254 | } |
1255 | ||
1256 | if (strcmp(up_or_down, "up") == 0) | |
1257 | vc->link_down = 0; | |
1258 | else if (strcmp(up_or_down, "down") == 0) | |
1259 | vc->link_down = 1; | |
1260 | else | |
376253ec AL |
1261 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
1262 | "valid\n", up_or_down); | |
436e5e53 | 1263 | |
665a3b07 MM |
1264 | if (vc->info->link_status_changed) { |
1265 | vc->info->link_status_changed(vc); | |
1266 | } | |
436e5e53 AL |
1267 | } |
1268 | ||
63a01ef8 AL |
1269 | void net_cleanup(void) |
1270 | { | |
1271 | VLANState *vlan; | |
577c4af9 | 1272 | VLANClientState *vc, *next_vc; |
63a01ef8 | 1273 | |
5610c3aa | 1274 | QTAILQ_FOREACH(vlan, &vlans, next) { |
5610c3aa | 1275 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
b946a153 | 1276 | qemu_del_vlan_client(vc); |
63a01ef8 AL |
1277 | } |
1278 | } | |
577c4af9 MM |
1279 | |
1280 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { | |
1281 | qemu_del_vlan_client(vc); | |
1282 | } | |
63a01ef8 AL |
1283 | } |
1284 | ||
668680f7 | 1285 | void net_check_clients(void) |
63a01ef8 AL |
1286 | { |
1287 | VLANState *vlan; | |
62112d18 | 1288 | VLANClientState *vc; |
64e69d50 | 1289 | int has_nic = 0, has_host_dev = 0; |
63a01ef8 | 1290 | |
5610c3aa | 1291 | QTAILQ_FOREACH(vlan, &vlans, next) { |
62112d18 MA |
1292 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1293 | switch (vc->info->type) { | |
1294 | case NET_CLIENT_TYPE_NIC: | |
1295 | has_nic = 1; | |
1296 | break; | |
1297 | case NET_CLIENT_TYPE_SLIRP: | |
1298 | case NET_CLIENT_TYPE_TAP: | |
1299 | case NET_CLIENT_TYPE_SOCKET: | |
1300 | case NET_CLIENT_TYPE_VDE: | |
1301 | has_host_dev = 1; | |
1302 | break; | |
1303 | default: ; | |
1304 | } | |
1305 | } | |
1306 | if (has_host_dev && !has_nic) | |
63a01ef8 | 1307 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
62112d18 | 1308 | if (has_nic && !has_host_dev) |
63a01ef8 AL |
1309 | fprintf(stderr, |
1310 | "Warning: vlan %d is not connected to host network\n", | |
1311 | vlan->id); | |
1312 | } | |
efe32fdd MA |
1313 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1314 | if (!vc->peer) { | |
1315 | fprintf(stderr, "Warning: %s %s has no peer\n", | |
1316 | vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev", | |
1317 | vc->name); | |
1318 | } | |
1319 | } | |
63a01ef8 | 1320 | } |
dc1c9fe8 MM |
1321 | |
1322 | static int net_init_client(QemuOpts *opts, void *dummy) | |
1323 | { | |
c1671a08 MM |
1324 | if (net_client_init(NULL, opts, 0) < 0) |
1325 | return -1; | |
1326 | return 0; | |
f6b134ac MM |
1327 | } |
1328 | ||
1329 | static int net_init_netdev(QemuOpts *opts, void *dummy) | |
1330 | { | |
1331 | return net_client_init(NULL, opts, 1); | |
dc1c9fe8 MM |
1332 | } |
1333 | ||
1334 | int net_init_clients(void) | |
1335 | { | |
cb4522cc | 1336 | if (default_net) { |
dc1c9fe8 MM |
1337 | /* if no clients, we use a default config */ |
1338 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); | |
1339 | #ifdef CONFIG_SLIRP | |
1340 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); | |
1341 | #endif | |
1342 | } | |
1343 | ||
5610c3aa | 1344 | QTAILQ_INIT(&vlans); |
577c4af9 | 1345 | QTAILQ_INIT(&non_vlan_clients); |
5610c3aa | 1346 | |
f6b134ac MM |
1347 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
1348 | return -1; | |
1349 | ||
dc1c9fe8 MM |
1350 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
1351 | return -1; | |
1352 | } | |
1353 | ||
dc1c9fe8 MM |
1354 | return 0; |
1355 | } | |
1356 | ||
7f161aae | 1357 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
dc1c9fe8 | 1358 | { |
a3a766e7 | 1359 | #if defined(CONFIG_SLIRP) |
68ac40d2 MM |
1360 | int ret; |
1361 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { | |
dc1c9fe8 MM |
1362 | return ret; |
1363 | } | |
a3a766e7 | 1364 | #endif |
68ac40d2 | 1365 | |
7f161aae | 1366 | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
dc1c9fe8 MM |
1367 | return -1; |
1368 | } | |
1369 | ||
cb4522cc | 1370 | default_net = 0; |
dc1c9fe8 MM |
1371 | return 0; |
1372 | } |