]>
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 | */ | |
63a01ef8 AL |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
26 | #include <signal.h> | |
27 | #include <time.h> | |
28 | #include <errno.h> | |
29 | #include <sys/time.h> | |
30 | #include <zlib.h> | |
31 | ||
71e72a19 | 32 | /* Needed early for CONFIG_BSD etc. */ |
d40cdb10 BS |
33 | #include "config-host.h" |
34 | ||
63a01ef8 AL |
35 | #ifndef _WIN32 |
36 | #include <sys/times.h> | |
37 | #include <sys/wait.h> | |
38 | #include <termios.h> | |
39 | #include <sys/mman.h> | |
40 | #include <sys/ioctl.h> | |
24646c7e | 41 | #include <sys/resource.h> |
63a01ef8 | 42 | #include <sys/socket.h> |
24646c7e | 43 | #include <net/if.h> |
63a01ef8 AL |
44 | #include <dirent.h> |
45 | #include <netdb.h> | |
46 | #include <sys/select.h> | |
71e72a19 | 47 | #ifdef CONFIG_BSD |
63a01ef8 | 48 | #include <sys/stat.h> |
a167ba50 | 49 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
63a01ef8 | 50 | #include <libutil.h> |
24646c7e BS |
51 | #else |
52 | #include <util.h> | |
63a01ef8 | 53 | #endif |
63a01ef8 | 54 | #ifdef __linux__ |
63a01ef8 AL |
55 | #include <pty.h> |
56 | #include <malloc.h> | |
57 | #include <linux/rtc.h> | |
58 | ||
59 | /* For the benefit of older linux systems which don't supply it, | |
60 | we use a local copy of hpet.h. */ | |
61 | /* #include <linux/hpet.h> */ | |
62 | #include "hpet.h" | |
63 | ||
64 | #include <linux/ppdev.h> | |
65 | #include <linux/parport.h> | |
66 | #endif | |
67 | #ifdef __sun__ | |
68 | #include <sys/stat.h> | |
69 | #include <sys/ethernet.h> | |
70 | #include <sys/sockio.h> | |
71 | #include <netinet/arp.h> | |
72 | #include <netinet/in.h> | |
73 | #include <netinet/in_systm.h> | |
74 | #include <netinet/ip.h> | |
75 | #include <netinet/ip_icmp.h> // must come after ip.h | |
76 | #include <netinet/udp.h> | |
77 | #include <netinet/tcp.h> | |
78 | #include <net/if.h> | |
79 | #include <syslog.h> | |
80 | #include <stropts.h> | |
81 | #endif | |
82 | #endif | |
83 | #endif | |
84 | ||
63a01ef8 AL |
85 | #if defined(__OpenBSD__) |
86 | #include <util.h> | |
87 | #endif | |
88 | ||
511d2b14 BS |
89 | #include "qemu-common.h" |
90 | #include "net.h" | |
a8ed73f7 | 91 | #include "net/tap.h" |
68ac40d2 | 92 | #include "net/slirp.h" |
5c361cc3 | 93 | #include "net/vde.h" |
511d2b14 BS |
94 | #include "monitor.h" |
95 | #include "sysemu.h" | |
96 | #include "qemu-timer.h" | |
97 | #include "qemu-char.h" | |
98 | #include "audio/audio.h" | |
99 | #include "qemu_socket.h" | |
bb9ea79e | 100 | #include "qemu-log.h" |
f83c6e10 | 101 | #include "qemu-config.h" |
511d2b14 | 102 | |
5610c3aa | 103 | static QTAILQ_HEAD(, VLANState) vlans; |
577c4af9 | 104 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
63a01ef8 AL |
105 | |
106 | /***********************************************************/ | |
107 | /* network device redirectors */ | |
108 | ||
68ac40d2 | 109 | #if defined(DEBUG_NET) |
63a01ef8 AL |
110 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
111 | { | |
112 | int len, i, j, c; | |
113 | ||
114 | for(i=0;i<size;i+=16) { | |
115 | len = size - i; | |
116 | if (len > 16) | |
117 | len = 16; | |
118 | fprintf(f, "%08x ", i); | |
119 | for(j=0;j<16;j++) { | |
120 | if (j < len) | |
121 | fprintf(f, " %02x", buf[i+j]); | |
122 | else | |
123 | fprintf(f, " "); | |
124 | } | |
125 | fprintf(f, " "); | |
126 | for(j=0;j<len;j++) { | |
127 | c = buf[i+j]; | |
128 | if (c < ' ' || c > '~') | |
129 | c = '.'; | |
130 | fprintf(f, "%c", c); | |
131 | } | |
132 | fprintf(f, "\n"); | |
133 | } | |
134 | } | |
135 | #endif | |
136 | ||
137 | static int parse_macaddr(uint8_t *macaddr, const char *p) | |
138 | { | |
139 | int i; | |
140 | char *last_char; | |
141 | long int offset; | |
142 | ||
143 | errno = 0; | |
144 | offset = strtol(p, &last_char, 0); | |
145 | if (0 == errno && '\0' == *last_char && | |
146 | offset >= 0 && offset <= 0xFFFFFF) { | |
147 | macaddr[3] = (offset & 0xFF0000) >> 16; | |
148 | macaddr[4] = (offset & 0xFF00) >> 8; | |
149 | macaddr[5] = offset & 0xFF; | |
150 | return 0; | |
151 | } else { | |
152 | for(i = 0; i < 6; i++) { | |
153 | macaddr[i] = strtol(p, (char **)&p, 16); | |
154 | if (i == 5) { | |
155 | if (*p != '\0') | |
156 | return -1; | |
157 | } else { | |
158 | if (*p != ':' && *p != '-') | |
159 | return -1; | |
160 | p++; | |
161 | } | |
162 | } | |
163 | return 0; | |
164 | } | |
165 | ||
166 | return -1; | |
167 | } | |
168 | ||
169 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
170 | { | |
171 | const char *p, *p1; | |
172 | int len; | |
173 | p = *pp; | |
174 | p1 = strchr(p, sep); | |
175 | if (!p1) | |
176 | return -1; | |
177 | len = p1 - p; | |
178 | p1++; | |
179 | if (buf_size > 0) { | |
180 | if (len > buf_size - 1) | |
181 | len = buf_size - 1; | |
182 | memcpy(buf, p, len); | |
183 | buf[len] = '\0'; | |
184 | } | |
185 | *pp = p1; | |
186 | return 0; | |
187 | } | |
188 | ||
189 | int parse_host_src_port(struct sockaddr_in *haddr, | |
190 | struct sockaddr_in *saddr, | |
191 | const char *input_str) | |
192 | { | |
193 | char *str = strdup(input_str); | |
194 | char *host_str = str; | |
195 | char *src_str; | |
196 | const char *src_str2; | |
197 | char *ptr; | |
198 | ||
199 | /* | |
200 | * Chop off any extra arguments at the end of the string which | |
201 | * would start with a comma, then fill in the src port information | |
202 | * if it was provided else use the "any address" and "any port". | |
203 | */ | |
204 | if ((ptr = strchr(str,','))) | |
205 | *ptr = '\0'; | |
206 | ||
207 | if ((src_str = strchr(input_str,'@'))) { | |
208 | *src_str = '\0'; | |
209 | src_str++; | |
210 | } | |
211 | ||
212 | if (parse_host_port(haddr, host_str) < 0) | |
213 | goto fail; | |
214 | ||
215 | src_str2 = src_str; | |
216 | if (!src_str || *src_str == '\0') | |
217 | src_str2 = ":0"; | |
218 | ||
219 | if (parse_host_port(saddr, src_str2) < 0) | |
220 | goto fail; | |
221 | ||
222 | free(str); | |
223 | return(0); | |
224 | ||
225 | fail: | |
226 | free(str); | |
227 | return -1; | |
228 | } | |
229 | ||
230 | int parse_host_port(struct sockaddr_in *saddr, const char *str) | |
231 | { | |
232 | char buf[512]; | |
233 | struct hostent *he; | |
234 | const char *p, *r; | |
235 | int port; | |
236 | ||
237 | p = str; | |
238 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
239 | return -1; | |
240 | saddr->sin_family = AF_INET; | |
241 | if (buf[0] == '\0') { | |
242 | saddr->sin_addr.s_addr = 0; | |
243 | } else { | |
cd390083 | 244 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
245 | if (!inet_aton(buf, &saddr->sin_addr)) |
246 | return -1; | |
247 | } else { | |
248 | if ((he = gethostbyname(buf)) == NULL) | |
249 | return - 1; | |
250 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
251 | } | |
252 | } | |
253 | port = strtol(p, (char **)&r, 0); | |
254 | if (r == p) | |
255 | return -1; | |
256 | saddr->sin_port = htons(port); | |
257 | return 0; | |
258 | } | |
259 | ||
7cb7434b AL |
260 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
261 | { | |
262 | snprintf(vc->info_str, sizeof(vc->info_str), | |
4dda4063 AL |
263 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
264 | vc->model, | |
7cb7434b AL |
265 | macaddr[0], macaddr[1], macaddr[2], |
266 | macaddr[3], macaddr[4], macaddr[5]); | |
267 | } | |
268 | ||
76d32cba GH |
269 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
270 | { | |
271 | static int index = 0; | |
272 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; | |
273 | ||
274 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) | |
275 | return; | |
276 | macaddr->a[0] = 0x52; | |
277 | macaddr->a[1] = 0x54; | |
278 | macaddr->a[2] = 0x00; | |
279 | macaddr->a[3] = 0x12; | |
280 | macaddr->a[4] = 0x34; | |
281 | macaddr->a[5] = 0x56 + index++; | |
282 | } | |
283 | ||
676cff29 AL |
284 | static char *assign_name(VLANClientState *vc1, const char *model) |
285 | { | |
286 | VLANState *vlan; | |
287 | char buf[256]; | |
288 | int id = 0; | |
289 | ||
5610c3aa | 290 | QTAILQ_FOREACH(vlan, &vlans, next) { |
676cff29 AL |
291 | VLANClientState *vc; |
292 | ||
5610c3aa MM |
293 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
294 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
676cff29 | 295 | id++; |
5610c3aa MM |
296 | } |
297 | } | |
676cff29 AL |
298 | } |
299 | ||
300 | snprintf(buf, sizeof(buf), "%s.%d", model, id); | |
301 | ||
02374aa0 | 302 | return qemu_strdup(buf); |
676cff29 AL |
303 | } |
304 | ||
9a6ecb30 | 305 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 306 | unsigned flags, |
9a6ecb30 MM |
307 | const uint8_t *data, |
308 | size_t size, | |
309 | void *opaque); | |
310 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, | |
c0b8e49c | 311 | unsigned flags, |
9a6ecb30 MM |
312 | const struct iovec *iov, |
313 | int iovcnt, | |
314 | void *opaque); | |
315 | ||
bb6e6364 MM |
316 | VLANClientState *qemu_new_vlan_client(net_client_type type, |
317 | VLANState *vlan, | |
283c7c63 | 318 | VLANClientState *peer, |
bf38c1a0 | 319 | const char *model, |
7a9f6e4a | 320 | const char *name, |
cda9046b MM |
321 | NetCanReceive *can_receive, |
322 | NetReceive *receive, | |
70783b9c | 323 | NetReceive *receive_raw, |
cda9046b | 324 | NetReceiveIOV *receive_iov, |
b946a153 | 325 | NetCleanup *cleanup, |
63a01ef8 AL |
326 | void *opaque) |
327 | { | |
5610c3aa MM |
328 | VLANClientState *vc; |
329 | ||
63a01ef8 | 330 | vc = qemu_mallocz(sizeof(VLANClientState)); |
5610c3aa | 331 | |
bb6e6364 | 332 | vc->type = type; |
02374aa0 | 333 | vc->model = qemu_strdup(model); |
7a9f6e4a | 334 | if (name) |
02374aa0 | 335 | vc->name = qemu_strdup(name); |
7a9f6e4a AL |
336 | else |
337 | vc->name = assign_name(vc, model); | |
cda9046b MM |
338 | vc->can_receive = can_receive; |
339 | vc->receive = receive; | |
70783b9c | 340 | vc->receive_raw = receive_raw; |
cda9046b | 341 | vc->receive_iov = receive_iov; |
b946a153 | 342 | vc->cleanup = cleanup; |
63a01ef8 | 343 | vc->opaque = opaque; |
5610c3aa | 344 | |
d80b9fc6 | 345 | if (vlan) { |
283c7c63 | 346 | assert(!peer); |
d80b9fc6 MM |
347 | vc->vlan = vlan; |
348 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); | |
577c4af9 | 349 | } else { |
283c7c63 MM |
350 | if (peer) { |
351 | vc->peer = peer; | |
352 | peer->peer = vc; | |
353 | } | |
577c4af9 | 354 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
9a6ecb30 MM |
355 | |
356 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, | |
357 | qemu_deliver_packet_iov, | |
358 | vc); | |
d80b9fc6 | 359 | } |
63a01ef8 | 360 | |
63a01ef8 AL |
361 | return vc; |
362 | } | |
363 | ||
364 | void qemu_del_vlan_client(VLANClientState *vc) | |
365 | { | |
d80b9fc6 MM |
366 | if (vc->vlan) { |
367 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); | |
577c4af9 | 368 | } else { |
9a6ecb30 MM |
369 | if (vc->send_queue) { |
370 | qemu_del_net_queue(vc->send_queue); | |
371 | } | |
577c4af9 | 372 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
283c7c63 MM |
373 | if (vc->peer) { |
374 | vc->peer->peer = NULL; | |
375 | } | |
d80b9fc6 | 376 | } |
63a01ef8 | 377 | |
5610c3aa MM |
378 | if (vc->cleanup) { |
379 | vc->cleanup(vc); | |
380 | } | |
381 | ||
382 | qemu_free(vc->name); | |
383 | qemu_free(vc->model); | |
384 | qemu_free(vc); | |
63a01ef8 AL |
385 | } |
386 | ||
8b13c4a7 AL |
387 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) |
388 | { | |
5610c3aa | 389 | VLANClientState *vc; |
8b13c4a7 | 390 | |
5610c3aa MM |
391 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
392 | if (vc->opaque == opaque) { | |
393 | return vc; | |
394 | } | |
395 | } | |
8b13c4a7 AL |
396 | |
397 | return NULL; | |
398 | } | |
399 | ||
68ac40d2 | 400 | VLANClientState * |
1a609520 JK |
401 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
402 | const char *client_str) | |
403 | { | |
404 | VLANState *vlan; | |
405 | VLANClientState *vc; | |
406 | ||
407 | vlan = qemu_find_vlan(vlan_id, 0); | |
408 | if (!vlan) { | |
409 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); | |
410 | return NULL; | |
411 | } | |
412 | ||
5610c3aa | 413 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1a609520 JK |
414 | if (!strcmp(vc->name, client_str)) { |
415 | break; | |
416 | } | |
417 | } | |
418 | if (!vc) { | |
419 | monitor_printf(mon, "can't find device %s on VLAN %d\n", | |
420 | client_str, vlan_id); | |
421 | } | |
422 | ||
423 | return vc; | |
424 | } | |
425 | ||
2e1e0641 | 426 | int qemu_can_send_packet(VLANClientState *sender) |
63a01ef8 | 427 | { |
2e1e0641 | 428 | VLANState *vlan = sender->vlan; |
63a01ef8 AL |
429 | VLANClientState *vc; |
430 | ||
9a6ecb30 | 431 | if (sender->peer) { |
893379ef MM |
432 | if (sender->peer->receive_disabled) { |
433 | return 0; | |
434 | } else if (sender->peer->can_receive && | |
435 | !sender->peer->can_receive(sender->peer)) { | |
9a6ecb30 | 436 | return 0; |
893379ef MM |
437 | } else { |
438 | return 1; | |
9a6ecb30 MM |
439 | } |
440 | } | |
441 | ||
d80b9fc6 MM |
442 | if (!sender->vlan) { |
443 | return 1; | |
444 | } | |
445 | ||
5610c3aa | 446 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
2e1e0641 MM |
447 | if (vc == sender) { |
448 | continue; | |
449 | } | |
450 | ||
cda9046b | 451 | /* no can_receive() handler, they can always receive */ |
e3f5ec2b | 452 | if (!vc->can_receive || vc->can_receive(vc)) { |
2e1e0641 | 453 | return 1; |
63a01ef8 AL |
454 | } |
455 | } | |
456 | return 0; | |
457 | } | |
458 | ||
9a6ecb30 | 459 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
c0b8e49c | 460 | unsigned flags, |
9a6ecb30 MM |
461 | const uint8_t *data, |
462 | size_t size, | |
463 | void *opaque) | |
464 | { | |
465 | VLANClientState *vc = opaque; | |
893379ef | 466 | ssize_t ret; |
9a6ecb30 MM |
467 | |
468 | if (vc->link_down) { | |
469 | return size; | |
470 | } | |
471 | ||
893379ef MM |
472 | if (vc->receive_disabled) { |
473 | return 0; | |
474 | } | |
475 | ||
476 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { | |
477 | ret = vc->receive_raw(vc, data, size); | |
478 | } else { | |
479 | ret = vc->receive(vc, data, size); | |
480 | } | |
481 | ||
482 | if (ret == 0) { | |
483 | vc->receive_disabled = 1; | |
484 | }; | |
485 | ||
486 | return ret; | |
9a6ecb30 MM |
487 | } |
488 | ||
f7105843 | 489 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
c0b8e49c | 490 | unsigned flags, |
f7105843 MM |
491 | const uint8_t *buf, |
492 | size_t size, | |
493 | void *opaque) | |
63a01ef8 | 494 | { |
f7105843 | 495 | VLANState *vlan = opaque; |
63a01ef8 | 496 | VLANClientState *vc; |
893379ef | 497 | ssize_t ret = -1; |
63a01ef8 | 498 | |
f7105843 | 499 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3e021d40 MM |
500 | ssize_t len; |
501 | ||
502 | if (vc == sender) { | |
503 | continue; | |
764a4d1d | 504 | } |
3e021d40 MM |
505 | |
506 | if (vc->link_down) { | |
507 | ret = size; | |
508 | continue; | |
509 | } | |
510 | ||
893379ef MM |
511 | if (vc->receive_disabled) { |
512 | ret = 0; | |
513 | continue; | |
514 | } | |
515 | ||
516 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { | |
ca77d175 | 517 | len = vc->receive_raw(vc, buf, size); |
893379ef | 518 | } else { |
ca77d175 | 519 | len = vc->receive(vc, buf, size); |
893379ef MM |
520 | } |
521 | ||
522 | if (len == 0) { | |
523 | vc->receive_disabled = 1; | |
524 | } | |
3e021d40 MM |
525 | |
526 | ret = (ret >= 0) ? ret : len; | |
893379ef | 527 | |
764a4d1d | 528 | } |
3e021d40 MM |
529 | |
530 | return ret; | |
764a4d1d AL |
531 | } |
532 | ||
8cad5516 MM |
533 | void qemu_purge_queued_packets(VLANClientState *vc) |
534 | { | |
9a6ecb30 MM |
535 | NetQueue *queue; |
536 | ||
537 | if (!vc->peer && !vc->vlan) { | |
d80b9fc6 | 538 | return; |
9a6ecb30 | 539 | } |
d80b9fc6 | 540 | |
9a6ecb30 MM |
541 | if (vc->peer) { |
542 | queue = vc->peer->send_queue; | |
543 | } else { | |
544 | queue = vc->vlan->send_queue; | |
545 | } | |
546 | ||
547 | qemu_net_queue_purge(queue, vc); | |
8cad5516 MM |
548 | } |
549 | ||
f3b6c7fc | 550 | void qemu_flush_queued_packets(VLANClientState *vc) |
e94667b9 | 551 | { |
9a6ecb30 MM |
552 | NetQueue *queue; |
553 | ||
893379ef MM |
554 | vc->receive_disabled = 0; |
555 | ||
9a6ecb30 MM |
556 | if (vc->vlan) { |
557 | queue = vc->vlan->send_queue; | |
558 | } else { | |
559 | queue = vc->send_queue; | |
560 | } | |
d80b9fc6 | 561 | |
9a6ecb30 | 562 | qemu_net_queue_flush(queue); |
e94667b9 MM |
563 | } |
564 | ||
ca77d175 MM |
565 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
566 | unsigned flags, | |
567 | const uint8_t *buf, int size, | |
568 | NetPacketSent *sent_cb) | |
764a4d1d | 569 | { |
9a6ecb30 | 570 | NetQueue *queue; |
436e5e53 | 571 | |
63a01ef8 | 572 | #ifdef DEBUG_NET |
d80b9fc6 | 573 | printf("qemu_send_packet_async:\n"); |
63a01ef8 AL |
574 | hex_dump(stdout, buf, size); |
575 | #endif | |
f3b6c7fc | 576 | |
9a6ecb30 MM |
577 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
578 | return size; | |
579 | } | |
580 | ||
581 | if (sender->peer) { | |
582 | queue = sender->peer->send_queue; | |
583 | } else { | |
584 | queue = sender->vlan->send_queue; | |
585 | } | |
586 | ||
ca77d175 MM |
587 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
588 | } | |
589 | ||
590 | ssize_t qemu_send_packet_async(VLANClientState *sender, | |
591 | const uint8_t *buf, int size, | |
592 | NetPacketSent *sent_cb) | |
593 | { | |
594 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, | |
595 | buf, size, sent_cb); | |
f3b6c7fc MM |
596 | } |
597 | ||
598 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
599 | { | |
600 | qemu_send_packet_async(vc, buf, size, NULL); | |
63a01ef8 AL |
601 | } |
602 | ||
ca77d175 MM |
603 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
604 | { | |
605 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, | |
606 | buf, size, NULL); | |
607 | } | |
608 | ||
fbe78f4f AL |
609 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
610 | int iovcnt) | |
611 | { | |
612 | uint8_t buffer[4096]; | |
613 | size_t offset = 0; | |
614 | int i; | |
615 | ||
616 | for (i = 0; i < iovcnt; i++) { | |
617 | size_t len; | |
618 | ||
619 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); | |
620 | memcpy(buffer + offset, iov[i].iov_base, len); | |
621 | offset += len; | |
622 | } | |
623 | ||
f3b6c7fc | 624 | return vc->receive(vc, buffer, offset); |
fbe78f4f AL |
625 | } |
626 | ||
e0e7877a AL |
627 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
628 | { | |
629 | size_t offset = 0; | |
630 | int i; | |
631 | ||
632 | for (i = 0; i < iovcnt; i++) | |
633 | offset += iov[i].iov_len; | |
634 | return offset; | |
635 | } | |
636 | ||
9a6ecb30 | 637 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 638 | unsigned flags, |
9a6ecb30 MM |
639 | const struct iovec *iov, |
640 | int iovcnt, | |
641 | void *opaque) | |
642 | { | |
643 | VLANClientState *vc = opaque; | |
644 | ||
645 | if (vc->link_down) { | |
646 | return calc_iov_length(iov, iovcnt); | |
647 | } | |
648 | ||
649 | if (vc->receive_iov) { | |
650 | return vc->receive_iov(vc, iov, iovcnt); | |
651 | } else { | |
652 | return vc_sendv_compat(vc, iov, iovcnt); | |
653 | } | |
654 | } | |
655 | ||
f7105843 | 656 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
c0b8e49c | 657 | unsigned flags, |
f7105843 MM |
658 | const struct iovec *iov, |
659 | int iovcnt, | |
660 | void *opaque) | |
fbe78f4f | 661 | { |
f7105843 | 662 | VLANState *vlan = opaque; |
fbe78f4f | 663 | VLANClientState *vc; |
f7105843 | 664 | ssize_t ret = -1; |
e94667b9 | 665 | |
f7105843 | 666 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
e94667b9 MM |
667 | ssize_t len; |
668 | ||
669 | if (vc == sender) { | |
670 | continue; | |
671 | } | |
672 | ||
673 | if (vc->link_down) { | |
674 | ret = calc_iov_length(iov, iovcnt); | |
675 | continue; | |
676 | } | |
677 | ||
ca77d175 MM |
678 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
679 | ||
e94667b9 MM |
680 | if (vc->receive_iov) { |
681 | len = vc->receive_iov(vc, iov, iovcnt); | |
682 | } else { | |
683 | len = vc_sendv_compat(vc, iov, iovcnt); | |
684 | } | |
685 | ||
686 | ret = (ret >= 0) ? ret : len; | |
687 | } | |
688 | ||
e94667b9 MM |
689 | return ret; |
690 | } | |
691 | ||
f3b6c7fc MM |
692 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
693 | const struct iovec *iov, int iovcnt, | |
694 | NetPacketSent *sent_cb) | |
e94667b9 | 695 | { |
9a6ecb30 MM |
696 | NetQueue *queue; |
697 | ||
698 | if (sender->link_down || (!sender->peer && !sender->vlan)) { | |
e94667b9 MM |
699 | return calc_iov_length(iov, iovcnt); |
700 | } | |
701 | ||
9a6ecb30 MM |
702 | if (sender->peer) { |
703 | queue = sender->peer->send_queue; | |
704 | } else { | |
705 | queue = sender->vlan->send_queue; | |
706 | } | |
707 | ||
c0b8e49c MM |
708 | return qemu_net_queue_send_iov(queue, sender, |
709 | QEMU_NET_PACKET_FLAG_NONE, | |
710 | iov, iovcnt, sent_cb); | |
fbe78f4f AL |
711 | } |
712 | ||
f3b6c7fc MM |
713 | ssize_t |
714 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
715 | { | |
716 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
717 | } | |
718 | ||
63a01ef8 AL |
719 | /* network connection */ |
720 | typedef struct NetSocketState { | |
721 | VLANClientState *vc; | |
722 | int fd; | |
723 | int state; /* 0 = getting length, 1 = getting data */ | |
abcd2baa AL |
724 | unsigned int index; |
725 | unsigned int packet_len; | |
63a01ef8 AL |
726 | uint8_t buf[4096]; |
727 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ | |
728 | } NetSocketState; | |
729 | ||
730 | typedef struct NetSocketListenState { | |
731 | VLANState *vlan; | |
bf38c1a0 | 732 | char *model; |
7a9f6e4a | 733 | char *name; |
63a01ef8 AL |
734 | int fd; |
735 | } NetSocketListenState; | |
736 | ||
737 | /* XXX: we consider we can send the whole packet without blocking */ | |
4f1c942b | 738 | static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 739 | { |
e3f5ec2b | 740 | NetSocketState *s = vc->opaque; |
63a01ef8 AL |
741 | uint32_t len; |
742 | len = htonl(size); | |
743 | ||
744 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); | |
4f1c942b | 745 | return send_all(s->fd, buf, size); |
63a01ef8 AL |
746 | } |
747 | ||
4f1c942b | 748 | static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 749 | { |
e3f5ec2b | 750 | NetSocketState *s = vc->opaque; |
4f1c942b | 751 | |
70503264 | 752 | return sendto(s->fd, (const void *)buf, size, 0, |
4f1c942b | 753 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
63a01ef8 AL |
754 | } |
755 | ||
756 | static void net_socket_send(void *opaque) | |
757 | { | |
758 | NetSocketState *s = opaque; | |
abcd2baa AL |
759 | int size, err; |
760 | unsigned l; | |
63a01ef8 AL |
761 | uint8_t buf1[4096]; |
762 | const uint8_t *buf; | |
763 | ||
c5b76b38 | 764 | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); |
63a01ef8 AL |
765 | if (size < 0) { |
766 | err = socket_error(); | |
767 | if (err != EWOULDBLOCK) | |
768 | goto eoc; | |
769 | } else if (size == 0) { | |
770 | /* end of connection */ | |
771 | eoc: | |
772 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
773 | closesocket(s->fd); | |
774 | return; | |
775 | } | |
776 | buf = buf1; | |
777 | while (size > 0) { | |
778 | /* reassemble a packet from the network */ | |
779 | switch(s->state) { | |
780 | case 0: | |
781 | l = 4 - s->index; | |
782 | if (l > size) | |
783 | l = size; | |
784 | memcpy(s->buf + s->index, buf, l); | |
785 | buf += l; | |
786 | size -= l; | |
787 | s->index += l; | |
788 | if (s->index == 4) { | |
789 | /* got length */ | |
790 | s->packet_len = ntohl(*(uint32_t *)s->buf); | |
791 | s->index = 0; | |
792 | s->state = 1; | |
793 | } | |
794 | break; | |
795 | case 1: | |
796 | l = s->packet_len - s->index; | |
797 | if (l > size) | |
798 | l = size; | |
abcd2baa AL |
799 | if (s->index + l <= sizeof(s->buf)) { |
800 | memcpy(s->buf + s->index, buf, l); | |
801 | } else { | |
802 | fprintf(stderr, "serious error: oversized packet received," | |
803 | "connection terminated.\n"); | |
804 | s->state = 0; | |
805 | goto eoc; | |
806 | } | |
807 | ||
63a01ef8 AL |
808 | s->index += l; |
809 | buf += l; | |
810 | size -= l; | |
811 | if (s->index >= s->packet_len) { | |
812 | qemu_send_packet(s->vc, s->buf, s->packet_len); | |
813 | s->index = 0; | |
814 | s->state = 0; | |
815 | } | |
816 | break; | |
817 | } | |
818 | } | |
819 | } | |
820 | ||
821 | static void net_socket_send_dgram(void *opaque) | |
822 | { | |
823 | NetSocketState *s = opaque; | |
824 | int size; | |
825 | ||
c5b76b38 | 826 | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
63a01ef8 AL |
827 | if (size < 0) |
828 | return; | |
829 | if (size == 0) { | |
830 | /* end of connection */ | |
831 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
832 | return; | |
833 | } | |
834 | qemu_send_packet(s->vc, s->buf, size); | |
835 | } | |
836 | ||
837 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) | |
838 | { | |
839 | struct ip_mreq imr; | |
840 | int fd; | |
841 | int val, ret; | |
842 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { | |
843 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", | |
844 | inet_ntoa(mcastaddr->sin_addr), | |
845 | (int)ntohl(mcastaddr->sin_addr.s_addr)); | |
846 | return -1; | |
847 | ||
848 | } | |
849 | fd = socket(PF_INET, SOCK_DGRAM, 0); | |
850 | if (fd < 0) { | |
851 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
852 | return -1; | |
853 | } | |
854 | ||
855 | val = 1; | |
856 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
857 | (const char *)&val, sizeof(val)); | |
858 | if (ret < 0) { | |
859 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); | |
860 | goto fail; | |
861 | } | |
862 | ||
863 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); | |
864 | if (ret < 0) { | |
865 | perror("bind"); | |
866 | goto fail; | |
867 | } | |
868 | ||
869 | /* Add host to multicast group */ | |
870 | imr.imr_multiaddr = mcastaddr->sin_addr; | |
871 | imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
872 | ||
873 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
874 | (const char *)&imr, sizeof(struct ip_mreq)); | |
875 | if (ret < 0) { | |
876 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); | |
877 | goto fail; | |
878 | } | |
879 | ||
880 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ | |
881 | val = 1; | |
882 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, | |
883 | (const char *)&val, sizeof(val)); | |
884 | if (ret < 0) { | |
885 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); | |
886 | goto fail; | |
887 | } | |
888 | ||
889 | socket_set_nonblock(fd); | |
890 | return fd; | |
891 | fail: | |
892 | if (fd >= 0) | |
893 | closesocket(fd); | |
894 | return -1; | |
895 | } | |
896 | ||
b946a153 AL |
897 | static void net_socket_cleanup(VLANClientState *vc) |
898 | { | |
899 | NetSocketState *s = vc->opaque; | |
900 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
901 | close(s->fd); | |
902 | qemu_free(s); | |
903 | } | |
904 | ||
7a9f6e4a AL |
905 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, |
906 | const char *model, | |
907 | const char *name, | |
bf38c1a0 | 908 | int fd, int is_connected) |
63a01ef8 AL |
909 | { |
910 | struct sockaddr_in saddr; | |
911 | int newfd; | |
912 | socklen_t saddr_len; | |
913 | NetSocketState *s; | |
914 | ||
915 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it | |
916 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() | |
917 | * by ONLY ONE process: we must "clone" this dgram socket --jjo | |
918 | */ | |
919 | ||
920 | if (is_connected) { | |
921 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { | |
922 | /* must be bound */ | |
923 | if (saddr.sin_addr.s_addr==0) { | |
924 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", | |
925 | fd); | |
926 | return NULL; | |
927 | } | |
928 | /* clone dgram socket */ | |
929 | newfd = net_socket_mcast_create(&saddr); | |
930 | if (newfd < 0) { | |
931 | /* error already reported by net_socket_mcast_create() */ | |
932 | close(fd); | |
933 | return NULL; | |
934 | } | |
935 | /* clone newfd to fd, close newfd */ | |
936 | dup2(newfd, fd); | |
937 | close(newfd); | |
938 | ||
939 | } else { | |
940 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", | |
941 | fd, strerror(errno)); | |
942 | return NULL; | |
943 | } | |
944 | } | |
945 | ||
946 | s = qemu_mallocz(sizeof(NetSocketState)); | |
63a01ef8 AL |
947 | s->fd = fd; |
948 | ||
bb6e6364 MM |
949 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
950 | vlan, NULL, model, name, NULL, | |
70783b9c | 951 | net_socket_receive_dgram, NULL, NULL, |
283c7c63 | 952 | net_socket_cleanup, s); |
63a01ef8 AL |
953 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
954 | ||
955 | /* mcast: save bound address as dst */ | |
956 | if (is_connected) s->dgram_dst=saddr; | |
957 | ||
958 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
959 | "socket: fd=%d (%s mcast=%s:%d)", | |
960 | fd, is_connected? "cloned" : "", | |
961 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
962 | return s; | |
963 | } | |
964 | ||
965 | static void net_socket_connect(void *opaque) | |
966 | { | |
967 | NetSocketState *s = opaque; | |
968 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); | |
969 | } | |
970 | ||
7a9f6e4a AL |
971 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, |
972 | const char *model, | |
973 | const char *name, | |
bf38c1a0 | 974 | int fd, int is_connected) |
63a01ef8 AL |
975 | { |
976 | NetSocketState *s; | |
977 | s = qemu_mallocz(sizeof(NetSocketState)); | |
63a01ef8 | 978 | s->fd = fd; |
bb6e6364 MM |
979 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
980 | vlan, NULL, model, name, NULL, | |
70783b9c | 981 | net_socket_receive, NULL, NULL, |
283c7c63 | 982 | net_socket_cleanup, s); |
63a01ef8 AL |
983 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
984 | "socket: fd=%d", fd); | |
985 | if (is_connected) { | |
986 | net_socket_connect(s); | |
987 | } else { | |
988 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); | |
989 | } | |
990 | return s; | |
991 | } | |
992 | ||
7a9f6e4a AL |
993 | static NetSocketState *net_socket_fd_init(VLANState *vlan, |
994 | const char *model, const char *name, | |
bf38c1a0 | 995 | int fd, int is_connected) |
63a01ef8 | 996 | { |
acedcfbf | 997 | int so_type = -1, optlen=sizeof(so_type); |
63a01ef8 AL |
998 | |
999 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, | |
1000 | (socklen_t *)&optlen)< 0) { | |
1001 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); | |
1002 | return NULL; | |
1003 | } | |
1004 | switch(so_type) { | |
1005 | case SOCK_DGRAM: | |
7a9f6e4a | 1006 | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); |
63a01ef8 | 1007 | case SOCK_STREAM: |
7a9f6e4a | 1008 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
63a01ef8 AL |
1009 | default: |
1010 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ | |
1011 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); | |
7a9f6e4a | 1012 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
63a01ef8 AL |
1013 | } |
1014 | return NULL; | |
1015 | } | |
1016 | ||
1017 | static void net_socket_accept(void *opaque) | |
1018 | { | |
1019 | NetSocketListenState *s = opaque; | |
1020 | NetSocketState *s1; | |
1021 | struct sockaddr_in saddr; | |
1022 | socklen_t len; | |
1023 | int fd; | |
1024 | ||
1025 | for(;;) { | |
1026 | len = sizeof(saddr); | |
1027 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); | |
1028 | if (fd < 0 && errno != EINTR) { | |
1029 | return; | |
1030 | } else if (fd >= 0) { | |
1031 | break; | |
1032 | } | |
1033 | } | |
7a9f6e4a | 1034 | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); |
63a01ef8 AL |
1035 | if (!s1) { |
1036 | closesocket(fd); | |
1037 | } else { | |
1038 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), | |
1039 | "socket: connection from %s:%d", | |
1040 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
1041 | } | |
1042 | } | |
1043 | ||
7a9f6e4a AL |
1044 | static int net_socket_listen_init(VLANState *vlan, |
1045 | const char *model, | |
1046 | const char *name, | |
bf38c1a0 | 1047 | const char *host_str) |
63a01ef8 AL |
1048 | { |
1049 | NetSocketListenState *s; | |
1050 | int fd, val, ret; | |
1051 | struct sockaddr_in saddr; | |
1052 | ||
1053 | if (parse_host_port(&saddr, host_str) < 0) | |
1054 | return -1; | |
1055 | ||
1056 | s = qemu_mallocz(sizeof(NetSocketListenState)); | |
63a01ef8 AL |
1057 | |
1058 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
1059 | if (fd < 0) { | |
1060 | perror("socket"); | |
1061 | return -1; | |
1062 | } | |
1063 | socket_set_nonblock(fd); | |
1064 | ||
1065 | /* allow fast reuse */ | |
1066 | val = 1; | |
1067 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
1068 | ||
1069 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
1070 | if (ret < 0) { | |
1071 | perror("bind"); | |
1072 | return -1; | |
1073 | } | |
1074 | ret = listen(fd, 0); | |
1075 | if (ret < 0) { | |
1076 | perror("listen"); | |
1077 | return -1; | |
1078 | } | |
1079 | s->vlan = vlan; | |
02374aa0 MM |
1080 | s->model = qemu_strdup(model); |
1081 | s->name = name ? qemu_strdup(name) : NULL; | |
63a01ef8 AL |
1082 | s->fd = fd; |
1083 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); | |
1084 | return 0; | |
1085 | } | |
1086 | ||
7a9f6e4a AL |
1087 | static int net_socket_connect_init(VLANState *vlan, |
1088 | const char *model, | |
1089 | const char *name, | |
bf38c1a0 | 1090 | const char *host_str) |
63a01ef8 AL |
1091 | { |
1092 | NetSocketState *s; | |
1093 | int fd, connected, ret, err; | |
1094 | struct sockaddr_in saddr; | |
1095 | ||
1096 | if (parse_host_port(&saddr, host_str) < 0) | |
1097 | return -1; | |
1098 | ||
1099 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
1100 | if (fd < 0) { | |
1101 | perror("socket"); | |
1102 | return -1; | |
1103 | } | |
1104 | socket_set_nonblock(fd); | |
1105 | ||
1106 | connected = 0; | |
1107 | for(;;) { | |
1108 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
1109 | if (ret < 0) { | |
1110 | err = socket_error(); | |
1111 | if (err == EINTR || err == EWOULDBLOCK) { | |
1112 | } else if (err == EINPROGRESS) { | |
1113 | break; | |
1114 | #ifdef _WIN32 | |
1115 | } else if (err == WSAEALREADY) { | |
1116 | break; | |
1117 | #endif | |
1118 | } else { | |
1119 | perror("connect"); | |
1120 | closesocket(fd); | |
1121 | return -1; | |
1122 | } | |
1123 | } else { | |
1124 | connected = 1; | |
1125 | break; | |
1126 | } | |
1127 | } | |
7a9f6e4a | 1128 | s = net_socket_fd_init(vlan, model, name, fd, connected); |
63a01ef8 AL |
1129 | if (!s) |
1130 | return -1; | |
1131 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1132 | "socket: connect to %s:%d", | |
1133 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
1134 | return 0; | |
1135 | } | |
1136 | ||
7a9f6e4a AL |
1137 | static int net_socket_mcast_init(VLANState *vlan, |
1138 | const char *model, | |
1139 | const char *name, | |
bf38c1a0 | 1140 | const char *host_str) |
63a01ef8 AL |
1141 | { |
1142 | NetSocketState *s; | |
1143 | int fd; | |
1144 | struct sockaddr_in saddr; | |
1145 | ||
1146 | if (parse_host_port(&saddr, host_str) < 0) | |
1147 | return -1; | |
1148 | ||
1149 | ||
1150 | fd = net_socket_mcast_create(&saddr); | |
1151 | if (fd < 0) | |
1152 | return -1; | |
1153 | ||
7a9f6e4a | 1154 | s = net_socket_fd_init(vlan, model, name, fd, 0); |
63a01ef8 AL |
1155 | if (!s) |
1156 | return -1; | |
1157 | ||
1158 | s->dgram_dst = saddr; | |
1159 | ||
1160 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1161 | "socket: mcast=%s:%d", | |
1162 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
1163 | return 0; | |
1164 | ||
1165 | } | |
1166 | ||
bb9ea79e AL |
1167 | typedef struct DumpState { |
1168 | VLANClientState *pcap_vc; | |
1169 | int fd; | |
1170 | int pcap_caplen; | |
1171 | } DumpState; | |
1172 | ||
1173 | #define PCAP_MAGIC 0xa1b2c3d4 | |
1174 | ||
1175 | struct pcap_file_hdr { | |
1176 | uint32_t magic; | |
1177 | uint16_t version_major; | |
1178 | uint16_t version_minor; | |
1179 | int32_t thiszone; | |
1180 | uint32_t sigfigs; | |
1181 | uint32_t snaplen; | |
1182 | uint32_t linktype; | |
1183 | }; | |
1184 | ||
1185 | struct pcap_sf_pkthdr { | |
1186 | struct { | |
1187 | int32_t tv_sec; | |
1188 | int32_t tv_usec; | |
1189 | } ts; | |
1190 | uint32_t caplen; | |
1191 | uint32_t len; | |
1192 | }; | |
1193 | ||
4f1c942b | 1194 | static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
bb9ea79e | 1195 | { |
e3f5ec2b | 1196 | DumpState *s = vc->opaque; |
bb9ea79e AL |
1197 | struct pcap_sf_pkthdr hdr; |
1198 | int64_t ts; | |
1199 | int caplen; | |
1200 | ||
1201 | /* Early return in case of previous error. */ | |
1202 | if (s->fd < 0) { | |
4f1c942b | 1203 | return size; |
bb9ea79e AL |
1204 | } |
1205 | ||
6ee093c9 | 1206 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec()); |
bb9ea79e AL |
1207 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
1208 | ||
37cb6fc3 | 1209 | hdr.ts.tv_sec = ts / 1000000; |
bb9ea79e AL |
1210 | hdr.ts.tv_usec = ts % 1000000; |
1211 | hdr.caplen = caplen; | |
1212 | hdr.len = size; | |
1213 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || | |
1214 | write(s->fd, buf, caplen) != caplen) { | |
1215 | qemu_log("-net dump write error - stop dump\n"); | |
1216 | close(s->fd); | |
1217 | s->fd = -1; | |
1218 | } | |
4f1c942b MM |
1219 | |
1220 | return size; | |
bb9ea79e AL |
1221 | } |
1222 | ||
1223 | static void net_dump_cleanup(VLANClientState *vc) | |
1224 | { | |
1225 | DumpState *s = vc->opaque; | |
1226 | ||
1227 | close(s->fd); | |
1228 | qemu_free(s); | |
1229 | } | |
1230 | ||
fb12577c | 1231 | static int net_dump_init(VLANState *vlan, const char *device, |
bb9ea79e AL |
1232 | const char *name, const char *filename, int len) |
1233 | { | |
1234 | struct pcap_file_hdr hdr; | |
1235 | DumpState *s; | |
1236 | ||
1237 | s = qemu_malloc(sizeof(DumpState)); | |
1238 | ||
024431b3 | 1239 | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
bb9ea79e | 1240 | if (s->fd < 0) { |
fb12577c | 1241 | qemu_error("-net dump: can't open %s\n", filename); |
bb9ea79e AL |
1242 | return -1; |
1243 | } | |
1244 | ||
1245 | s->pcap_caplen = len; | |
1246 | ||
1247 | hdr.magic = PCAP_MAGIC; | |
1248 | hdr.version_major = 2; | |
1249 | hdr.version_minor = 4; | |
1250 | hdr.thiszone = 0; | |
1251 | hdr.sigfigs = 0; | |
1252 | hdr.snaplen = s->pcap_caplen; | |
1253 | hdr.linktype = 1; | |
1254 | ||
1255 | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { | |
fb12577c | 1256 | qemu_error("-net dump write error: %s\n", strerror(errno)); |
bb9ea79e AL |
1257 | close(s->fd); |
1258 | qemu_free(s); | |
1259 | return -1; | |
1260 | } | |
1261 | ||
bb6e6364 MM |
1262 | s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP, |
1263 | vlan, NULL, device, name, NULL, | |
70783b9c | 1264 | dump_receive, NULL, NULL, |
bb9ea79e AL |
1265 | net_dump_cleanup, s); |
1266 | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), | |
1267 | "dump to %s (len=%d)", filename, len); | |
1268 | return 0; | |
1269 | } | |
1270 | ||
63a01ef8 | 1271 | /* find or alloc a new VLAN */ |
1a609520 | 1272 | VLANState *qemu_find_vlan(int id, int allocate) |
63a01ef8 | 1273 | { |
5610c3aa MM |
1274 | VLANState *vlan; |
1275 | ||
1276 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
1277 | if (vlan->id == id) { | |
63a01ef8 | 1278 | return vlan; |
5610c3aa | 1279 | } |
63a01ef8 | 1280 | } |
5610c3aa | 1281 | |
1a609520 JK |
1282 | if (!allocate) { |
1283 | return NULL; | |
1284 | } | |
5610c3aa | 1285 | |
63a01ef8 | 1286 | vlan = qemu_mallocz(sizeof(VLANState)); |
63a01ef8 | 1287 | vlan->id = id; |
5610c3aa | 1288 | QTAILQ_INIT(&vlan->clients); |
f7105843 MM |
1289 | |
1290 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, | |
1291 | qemu_vlan_deliver_packet_iov, | |
1292 | vlan); | |
5610c3aa MM |
1293 | |
1294 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); | |
1295 | ||
63a01ef8 AL |
1296 | return vlan; |
1297 | } | |
1298 | ||
2ef924b4 | 1299 | VLANClientState *qemu_find_netdev(const char *id) |
5869c4d5 MM |
1300 | { |
1301 | VLANClientState *vc; | |
1302 | ||
1303 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
1304 | if (!strcmp(vc->name, id)) { | |
1305 | return vc; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | return NULL; | |
1310 | } | |
1311 | ||
7697079b AL |
1312 | static int nic_get_free_idx(void) |
1313 | { | |
1314 | int index; | |
1315 | ||
1316 | for (index = 0; index < MAX_NICS; index++) | |
1317 | if (!nd_table[index].used) | |
1318 | return index; | |
1319 | return -1; | |
1320 | } | |
1321 | ||
07caea31 MA |
1322 | int qemu_show_nic_models(const char *arg, const char *const *models) |
1323 | { | |
1324 | int i; | |
1325 | ||
1326 | if (!arg || strcmp(arg, "?")) | |
1327 | return 0; | |
1328 | ||
1329 | fprintf(stderr, "qemu: Supported NIC models: "); | |
1330 | for (i = 0 ; models[i]; i++) | |
1331 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
1332 | return 1; | |
1333 | } | |
1334 | ||
d07f22c5 AL |
1335 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
1336 | { | |
1337 | const char *models[2]; | |
1338 | ||
1339 | models[0] = model; | |
1340 | models[1] = NULL; | |
1341 | ||
07caea31 MA |
1342 | if (qemu_show_nic_models(nd->model, models)) |
1343 | exit(0); | |
1344 | if (qemu_find_nic_model(nd, models, model) < 0) | |
1345 | exit(1); | |
d07f22c5 AL |
1346 | } |
1347 | ||
07caea31 MA |
1348 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
1349 | const char *default_model) | |
d07f22c5 | 1350 | { |
07caea31 | 1351 | int i; |
d07f22c5 AL |
1352 | |
1353 | if (!nd->model) | |
32a8e14a | 1354 | nd->model = qemu_strdup(default_model); |
d07f22c5 | 1355 | |
07caea31 MA |
1356 | for (i = 0 ; models[i]; i++) { |
1357 | if (strcmp(nd->model, models[i]) == 0) | |
1358 | return i; | |
d07f22c5 AL |
1359 | } |
1360 | ||
07caea31 MA |
1361 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); |
1362 | return -1; | |
d07f22c5 AL |
1363 | } |
1364 | ||
5281d757 | 1365 | int net_handle_fd_param(Monitor *mon, const char *param) |
c1d6eed7 MM |
1366 | { |
1367 | if (!qemu_isdigit(param[0])) { | |
1368 | int fd; | |
1369 | ||
1370 | fd = monitor_get_fd(mon, param); | |
1371 | if (fd == -1) { | |
fb12577c | 1372 | qemu_error("No file descriptor named %s found", param); |
c1d6eed7 MM |
1373 | return -1; |
1374 | } | |
1375 | ||
1376 | return fd; | |
1377 | } else { | |
1378 | return strtol(param, NULL, 0); | |
1379 | } | |
1380 | } | |
1381 | ||
f6b134ac MM |
1382 | static int net_init_nic(QemuOpts *opts, |
1383 | Monitor *mon, | |
1384 | const char *name, | |
1385 | VLANState *vlan) | |
f83c6e10 MM |
1386 | { |
1387 | int idx; | |
1388 | NICInfo *nd; | |
5869c4d5 | 1389 | const char *netdev; |
f83c6e10 MM |
1390 | |
1391 | idx = nic_get_free_idx(); | |
1392 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
1393 | qemu_error("Too Many NICs\n"); | |
1394 | return -1; | |
1395 | } | |
1396 | ||
1397 | nd = &nd_table[idx]; | |
1398 | ||
1399 | memset(nd, 0, sizeof(*nd)); | |
1400 | ||
5869c4d5 MM |
1401 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
1402 | nd->netdev = qemu_find_netdev(netdev); | |
1403 | if (!nd->netdev) { | |
1404 | qemu_error("netdev '%s' not found\n", netdev); | |
1405 | return -1; | |
1406 | } | |
1407 | } else { | |
1408 | assert(vlan); | |
1409 | nd->vlan = vlan; | |
1410 | } | |
6d952ebe MM |
1411 | if (name) { |
1412 | nd->name = qemu_strdup(name); | |
1413 | } | |
f83c6e10 MM |
1414 | if (qemu_opt_get(opts, "model")) { |
1415 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); | |
1416 | } | |
1417 | if (qemu_opt_get(opts, "addr")) { | |
1418 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); | |
1419 | } | |
1420 | ||
1421 | nd->macaddr[0] = 0x52; | |
1422 | nd->macaddr[1] = 0x54; | |
1423 | nd->macaddr[2] = 0x00; | |
1424 | nd->macaddr[3] = 0x12; | |
1425 | nd->macaddr[4] = 0x34; | |
1426 | nd->macaddr[5] = 0x56 + idx; | |
1427 | ||
1428 | if (qemu_opt_get(opts, "macaddr") && | |
1429 | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { | |
1430 | qemu_error("invalid syntax for ethernet address\n"); | |
1431 | return -1; | |
1432 | } | |
1433 | ||
1434 | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED); | |
1435 | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED && | |
1436 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { | |
1437 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); | |
1438 | return -1; | |
1439 | } | |
1440 | ||
1441 | nd->used = 1; | |
5869c4d5 MM |
1442 | if (vlan) { |
1443 | nd->vlan->nb_guest_devs++; | |
1444 | } | |
f83c6e10 MM |
1445 | nb_nics++; |
1446 | ||
1447 | return idx; | |
1448 | } | |
1449 | ||
f6b134ac MM |
1450 | static int net_init_socket(QemuOpts *opts, |
1451 | Monitor *mon, | |
1452 | const char *name, | |
1453 | VLANState *vlan) | |
88ce16ca | 1454 | { |
88ce16ca MM |
1455 | if (qemu_opt_get(opts, "fd")) { |
1456 | int fd; | |
1457 | ||
1458 | if (qemu_opt_get(opts, "listen") || | |
1459 | qemu_opt_get(opts, "connect") || | |
1460 | qemu_opt_get(opts, "mcast")) { | |
1461 | qemu_error("listen=, connect= and mcast= is invalid with fd=\n"); | |
1462 | return -1; | |
1463 | } | |
1464 | ||
1465 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); | |
1466 | if (fd == -1) { | |
1467 | return -1; | |
1468 | } | |
1469 | ||
1470 | if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { | |
1471 | close(fd); | |
1472 | return -1; | |
1473 | } | |
1474 | } else if (qemu_opt_get(opts, "listen")) { | |
1475 | const char *listen; | |
1476 | ||
1477 | if (qemu_opt_get(opts, "fd") || | |
1478 | qemu_opt_get(opts, "connect") || | |
1479 | qemu_opt_get(opts, "mcast")) { | |
1480 | qemu_error("fd=, connect= and mcast= is invalid with listen=\n"); | |
1481 | return -1; | |
1482 | } | |
1483 | ||
1484 | listen = qemu_opt_get(opts, "listen"); | |
1485 | ||
1486 | if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { | |
1487 | return -1; | |
1488 | } | |
1489 | } else if (qemu_opt_get(opts, "connect")) { | |
1490 | const char *connect; | |
1491 | ||
1492 | if (qemu_opt_get(opts, "fd") || | |
1493 | qemu_opt_get(opts, "listen") || | |
1494 | qemu_opt_get(opts, "mcast")) { | |
1495 | qemu_error("fd=, listen= and mcast= is invalid with connect=\n"); | |
1496 | return -1; | |
1497 | } | |
1498 | ||
1499 | connect = qemu_opt_get(opts, "connect"); | |
1500 | ||
1501 | if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { | |
1502 | return -1; | |
1503 | } | |
1504 | } else if (qemu_opt_get(opts, "mcast")) { | |
1505 | const char *mcast; | |
1506 | ||
1507 | if (qemu_opt_get(opts, "fd") || | |
1508 | qemu_opt_get(opts, "connect") || | |
1509 | qemu_opt_get(opts, "listen")) { | |
1510 | qemu_error("fd=, connect= and listen= is invalid with mcast=\n"); | |
1511 | return -1; | |
1512 | } | |
1513 | ||
1514 | mcast = qemu_opt_get(opts, "mcast"); | |
1515 | ||
1516 | if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) { | |
1517 | return -1; | |
1518 | } | |
1519 | } else { | |
1520 | qemu_error("-socket requires fd=, listen=, connect= or mcast=\n"); | |
1521 | return -1; | |
1522 | } | |
1523 | ||
f6b134ac MM |
1524 | if (vlan) { |
1525 | vlan->nb_host_devs++; | |
1526 | } | |
88ce16ca MM |
1527 | |
1528 | return 0; | |
1529 | } | |
1530 | ||
f6b134ac MM |
1531 | static int net_init_dump(QemuOpts *opts, |
1532 | Monitor *mon, | |
1533 | const char *name, | |
1534 | VLANState *vlan) | |
ed2955c2 | 1535 | { |
ed2955c2 MM |
1536 | int len; |
1537 | const char *file; | |
ed2955c2 MM |
1538 | char def_file[128]; |
1539 | ||
f6b134ac | 1540 | assert(vlan); |
ed2955c2 | 1541 | |
ed2955c2 MM |
1542 | file = qemu_opt_get(opts, "file"); |
1543 | if (!file) { | |
1544 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); | |
1545 | file = def_file; | |
1546 | } | |
1547 | ||
1548 | len = qemu_opt_get_size(opts, "len", 65536); | |
1549 | ||
1550 | return net_dump_init(vlan, "dump", name, file, len); | |
1551 | } | |
1552 | ||
f83c6e10 MM |
1553 | #define NET_COMMON_PARAMS_DESC \ |
1554 | { \ | |
1555 | .name = "type", \ | |
1556 | .type = QEMU_OPT_STRING, \ | |
1557 | .help = "net client type (nic, tap etc.)", \ | |
1558 | }, { \ | |
1559 | .name = "vlan", \ | |
1560 | .type = QEMU_OPT_NUMBER, \ | |
1561 | .help = "vlan number", \ | |
1562 | }, { \ | |
1563 | .name = "name", \ | |
1564 | .type = QEMU_OPT_STRING, \ | |
1565 | .help = "identifier for monitor commands", \ | |
1566 | } | |
1567 | ||
6d952ebe MM |
1568 | typedef int (*net_client_init_func)(QemuOpts *opts, |
1569 | Monitor *mon, | |
f6b134ac MM |
1570 | const char *name, |
1571 | VLANState *vlan); | |
f83c6e10 MM |
1572 | |
1573 | /* magic number, but compiler will warn if too small */ | |
1574 | #define NET_MAX_DESC 20 | |
1575 | ||
1576 | static struct { | |
1577 | const char *type; | |
1578 | net_client_init_func init; | |
1579 | QemuOptDesc desc[NET_MAX_DESC]; | |
1580 | } net_client_types[] = { | |
1581 | { | |
1582 | .type = "none", | |
1583 | .desc = { | |
1584 | NET_COMMON_PARAMS_DESC, | |
1585 | { /* end of list */ } | |
1586 | }, | |
1587 | }, { | |
1588 | .type = "nic", | |
1589 | .init = net_init_nic, | |
1590 | .desc = { | |
1591 | NET_COMMON_PARAMS_DESC, | |
5869c4d5 MM |
1592 | { |
1593 | .name = "netdev", | |
1594 | .type = QEMU_OPT_STRING, | |
1595 | .help = "id of -netdev to connect to", | |
1596 | }, | |
f83c6e10 MM |
1597 | { |
1598 | .name = "macaddr", | |
1599 | .type = QEMU_OPT_STRING, | |
1600 | .help = "MAC address", | |
1601 | }, { | |
1602 | .name = "model", | |
1603 | .type = QEMU_OPT_STRING, | |
1604 | .help = "device model (e1000, rtl8139, virtio etc.)", | |
1605 | }, { | |
1606 | .name = "addr", | |
1607 | .type = QEMU_OPT_STRING, | |
1608 | .help = "PCI device address", | |
1609 | }, { | |
1610 | .name = "vectors", | |
1611 | .type = QEMU_OPT_NUMBER, | |
1612 | .help = "number of MSI-x vectors, 0 to disable MSI-X", | |
1613 | }, | |
1614 | { /* end of list */ } | |
1615 | }, | |
ec302ffd MM |
1616 | #ifdef CONFIG_SLIRP |
1617 | }, { | |
1618 | .type = "user", | |
1619 | .init = net_init_slirp, | |
1620 | .desc = { | |
1621 | NET_COMMON_PARAMS_DESC, | |
1622 | { | |
1623 | .name = "hostname", | |
1624 | .type = QEMU_OPT_STRING, | |
1625 | .help = "client hostname reported by the builtin DHCP server", | |
1626 | }, { | |
1627 | .name = "restrict", | |
1628 | .type = QEMU_OPT_STRING, | |
1629 | .help = "isolate the guest from the host (y|yes|n|no)", | |
1630 | }, { | |
1631 | .name = "ip", | |
1632 | .type = QEMU_OPT_STRING, | |
1633 | .help = "legacy parameter, use net= instead", | |
1634 | }, { | |
1635 | .name = "net", | |
1636 | .type = QEMU_OPT_STRING, | |
1637 | .help = "IP address and optional netmask", | |
1638 | }, { | |
1639 | .name = "host", | |
1640 | .type = QEMU_OPT_STRING, | |
1641 | .help = "guest-visible address of the host", | |
1642 | }, { | |
1643 | .name = "tftp", | |
1644 | .type = QEMU_OPT_STRING, | |
1645 | .help = "root directory of the built-in TFTP server", | |
1646 | }, { | |
1647 | .name = "bootfile", | |
1648 | .type = QEMU_OPT_STRING, | |
1649 | .help = "BOOTP filename, for use with tftp=", | |
1650 | }, { | |
1651 | .name = "dhcpstart", | |
1652 | .type = QEMU_OPT_STRING, | |
1653 | .help = "the first of the 16 IPs the built-in DHCP server can assign", | |
1654 | }, { | |
1655 | .name = "dns", | |
1656 | .type = QEMU_OPT_STRING, | |
1657 | .help = "guest-visible address of the virtual nameserver", | |
1658 | }, { | |
1659 | .name = "smb", | |
1660 | .type = QEMU_OPT_STRING, | |
1661 | .help = "root directory of the built-in SMB server", | |
1662 | }, { | |
1663 | .name = "smbserver", | |
1664 | .type = QEMU_OPT_STRING, | |
1665 | .help = "IP address of the built-in SMB server", | |
1666 | }, { | |
1667 | .name = "hostfwd", | |
1668 | .type = QEMU_OPT_STRING, | |
1669 | .help = "guest port number to forward incoming TCP or UDP connections", | |
1670 | }, { | |
1671 | .name = "guestfwd", | |
1672 | .type = QEMU_OPT_STRING, | |
1673 | .help = "IP address and port to forward guest TCP connections", | |
1674 | }, | |
1675 | { /* end of list */ } | |
1676 | }, | |
8a1c5235 | 1677 | #endif |
8a1c5235 MM |
1678 | }, { |
1679 | .type = "tap", | |
a8ed73f7 | 1680 | .init = net_init_tap, |
8a1c5235 MM |
1681 | .desc = { |
1682 | NET_COMMON_PARAMS_DESC, | |
1683 | { | |
1684 | .name = "ifname", | |
1685 | .type = QEMU_OPT_STRING, | |
1686 | .help = "interface name", | |
1687 | }, | |
a8ed73f7 | 1688 | #ifndef _WIN32 |
8a1c5235 MM |
1689 | { |
1690 | .name = "fd", | |
1691 | .type = QEMU_OPT_STRING, | |
1692 | .help = "file descriptor of an already opened tap", | |
8a1c5235 MM |
1693 | }, { |
1694 | .name = "script", | |
1695 | .type = QEMU_OPT_STRING, | |
1696 | .help = "script to initialize the interface", | |
1697 | }, { | |
1698 | .name = "downscript", | |
1699 | .type = QEMU_OPT_STRING, | |
1700 | .help = "script to shut down the interface", | |
8a1c5235 MM |
1701 | }, { |
1702 | .name = "sndbuf", | |
1703 | .type = QEMU_OPT_SIZE, | |
1704 | .help = "send buffer limit" | |
baf74c95 MM |
1705 | }, { |
1706 | .name = "vnet_hdr", | |
1707 | .type = QEMU_OPT_BOOL, | |
1708 | .help = "enable the IFF_VNET_HDR flag on the tap interface" | |
8a1c5235 | 1709 | }, |
a8ed73f7 | 1710 | #endif /* _WIN32 */ |
8a1c5235 MM |
1711 | { /* end of list */ } |
1712 | }, | |
88ce16ca MM |
1713 | }, { |
1714 | .type = "socket", | |
1715 | .init = net_init_socket, | |
1716 | .desc = { | |
1717 | NET_COMMON_PARAMS_DESC, | |
1718 | { | |
1719 | .name = "fd", | |
1720 | .type = QEMU_OPT_STRING, | |
1721 | .help = "file descriptor of an already opened socket", | |
1722 | }, { | |
1723 | .name = "listen", | |
1724 | .type = QEMU_OPT_STRING, | |
1725 | .help = "port number, and optional hostname, to listen on", | |
1726 | }, { | |
1727 | .name = "connect", | |
1728 | .type = QEMU_OPT_STRING, | |
1729 | .help = "port number, and optional hostname, to connect to", | |
1730 | }, { | |
1731 | .name = "mcast", | |
1732 | .type = QEMU_OPT_STRING, | |
1733 | .help = "UDP multicast address and port number", | |
1734 | }, | |
1735 | { /* end of list */ } | |
1736 | }, | |
dd51058d MM |
1737 | #ifdef CONFIG_VDE |
1738 | }, { | |
1739 | .type = "vde", | |
1740 | .init = net_init_vde, | |
1741 | .desc = { | |
1742 | NET_COMMON_PARAMS_DESC, | |
1743 | { | |
1744 | .name = "sock", | |
1745 | .type = QEMU_OPT_STRING, | |
1746 | .help = "socket path", | |
1747 | }, { | |
1748 | .name = "port", | |
1749 | .type = QEMU_OPT_NUMBER, | |
1750 | .help = "port number", | |
1751 | }, { | |
1752 | .name = "group", | |
1753 | .type = QEMU_OPT_STRING, | |
1754 | .help = "group owner of socket", | |
1755 | }, { | |
1756 | .name = "mode", | |
1757 | .type = QEMU_OPT_NUMBER, | |
1758 | .help = "permissions for socket", | |
1759 | }, | |
1760 | { /* end of list */ } | |
1761 | }, | |
1762 | #endif | |
ed2955c2 MM |
1763 | }, { |
1764 | .type = "dump", | |
1765 | .init = net_init_dump, | |
1766 | .desc = { | |
1767 | NET_COMMON_PARAMS_DESC, | |
1768 | { | |
1769 | .name = "len", | |
1770 | .type = QEMU_OPT_SIZE, | |
1771 | .help = "per-packet size limit (64k default)", | |
1772 | }, { | |
1773 | .name = "file", | |
1774 | .type = QEMU_OPT_STRING, | |
1775 | .help = "dump file path (default is qemu-vlan0.pcap)", | |
1776 | }, | |
1777 | { /* end of list */ } | |
1778 | }, | |
f83c6e10 MM |
1779 | }, |
1780 | { /* end of list */ } | |
1781 | }; | |
1782 | ||
f6b134ac | 1783 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
f83c6e10 | 1784 | { |
6d952ebe | 1785 | const char *name; |
f83c6e10 MM |
1786 | const char *type; |
1787 | int i; | |
1788 | ||
1789 | type = qemu_opt_get(opts, "type"); | |
1790 | if (!type) { | |
1791 | qemu_error("No type specified for -net\n"); | |
1792 | return -1; | |
1793 | } | |
1794 | ||
f6b134ac MM |
1795 | if (is_netdev) { |
1796 | if (strcmp(type, "tap") != 0 && | |
1797 | #ifdef CONFIG_SLIRP | |
1798 | strcmp(type, "user") != 0 && | |
1799 | #endif | |
1800 | #ifdef CONFIG_VDE | |
1801 | strcmp(type, "vde") != 0 && | |
1802 | #endif | |
1803 | strcmp(type, "socket") != 0) { | |
1804 | qemu_error("The '%s' network backend type is not valid with -netdev\n", | |
1805 | type); | |
1806 | return -1; | |
1807 | } | |
1808 | ||
1809 | if (qemu_opt_get(opts, "vlan")) { | |
1810 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); | |
1811 | return -1; | |
1812 | } | |
1813 | if (qemu_opt_get(opts, "name")) { | |
1814 | qemu_error("The 'name' parameter is not valid with -netdev\n"); | |
1815 | return -1; | |
1816 | } | |
1817 | if (!qemu_opts_id(opts)) { | |
1818 | qemu_error("The id= parameter is required with -netdev\n"); | |
1819 | return -1; | |
1820 | } | |
1821 | } | |
1822 | ||
6d952ebe MM |
1823 | name = qemu_opts_id(opts); |
1824 | if (!name) { | |
1825 | name = qemu_opt_get(opts, "name"); | |
1826 | } | |
1827 | ||
f83c6e10 MM |
1828 | for (i = 0; net_client_types[i].type != NULL; i++) { |
1829 | if (!strcmp(net_client_types[i].type, type)) { | |
f6b134ac MM |
1830 | VLANState *vlan = NULL; |
1831 | ||
f83c6e10 MM |
1832 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1833 | return -1; | |
1834 | } | |
1835 | ||
5869c4d5 MM |
1836 | /* Do not add to a vlan if it's a -netdev or a nic with a |
1837 | * netdev= parameter. */ | |
1838 | if (!(is_netdev || | |
1839 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { | |
f6b134ac MM |
1840 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1841 | } | |
1842 | ||
f83c6e10 | 1843 | if (net_client_types[i].init) { |
f6b134ac | 1844 | return net_client_types[i].init(opts, mon, name, vlan); |
f83c6e10 MM |
1845 | } else { |
1846 | return 0; | |
1847 | } | |
1848 | } | |
1849 | } | |
1850 | ||
1851 | qemu_error("Invalid -net type '%s'\n", type); | |
1852 | return -1; | |
1853 | } | |
1854 | ||
8b13c4a7 AL |
1855 | void net_client_uninit(NICInfo *nd) |
1856 | { | |
d80b9fc6 MM |
1857 | if (nd->vlan) { |
1858 | nd->vlan->nb_guest_devs--; | |
1859 | } | |
8b13c4a7 | 1860 | nb_nics--; |
a9796703 | 1861 | |
9203f520 MM |
1862 | qemu_free(nd->model); |
1863 | qemu_free(nd->name); | |
1864 | qemu_free(nd->devaddr); | |
a9796703 | 1865 | |
d2cffe30 | 1866 | nd->used = 0; |
8b13c4a7 AL |
1867 | } |
1868 | ||
6f338c34 AL |
1869 | static int net_host_check_device(const char *device) |
1870 | { | |
1871 | int i; | |
bb9ea79e | 1872 | const char *valid_param_list[] = { "tap", "socket", "dump" |
6f338c34 AL |
1873 | #ifdef CONFIG_SLIRP |
1874 | ,"user" | |
1875 | #endif | |
1876 | #ifdef CONFIG_VDE | |
1877 | ,"vde" | |
1878 | #endif | |
1879 | }; | |
1880 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
1881 | if (!strncmp(valid_param_list[i], device, | |
1882 | strlen(valid_param_list[i]))) | |
1883 | return 1; | |
1884 | } | |
1885 | ||
1886 | return 0; | |
1887 | } | |
1888 | ||
f18c16de | 1889 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
6f338c34 | 1890 | { |
f18c16de | 1891 | const char *device = qdict_get_str(qdict, "device"); |
7f1c9d20 MM |
1892 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1893 | QemuOpts *opts; | |
f18c16de | 1894 | |
6f338c34 | 1895 | if (!net_host_check_device(device)) { |
376253ec | 1896 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
1897 | return; |
1898 | } | |
7f1c9d20 MM |
1899 | |
1900 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); | |
1901 | if (!opts) { | |
1902 | monitor_printf(mon, "parsing network options '%s' failed\n", | |
1903 | opts_str ? opts_str : ""); | |
1904 | return; | |
1905 | } | |
1906 | ||
1907 | qemu_opt_set(opts, "type", device); | |
1908 | ||
f6b134ac | 1909 | if (net_client_init(mon, opts, 0) < 0) { |
5c8be678 AL |
1910 | monitor_printf(mon, "adding host network device %s failed\n", device); |
1911 | } | |
6f338c34 AL |
1912 | } |
1913 | ||
f18c16de | 1914 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
6f338c34 | 1915 | { |
6f338c34 | 1916 | VLANClientState *vc; |
f18c16de LC |
1917 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1918 | const char *device = qdict_get_str(qdict, "device"); | |
6f338c34 | 1919 | |
1a609520 | 1920 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
6f338c34 | 1921 | if (!vc) { |
6f338c34 AL |
1922 | return; |
1923 | } | |
e8f1f9db AL |
1924 | if (!net_host_check_device(vc->model)) { |
1925 | monitor_printf(mon, "invalid host network device %s\n", device); | |
1926 | return; | |
1927 | } | |
6f338c34 AL |
1928 | qemu_del_vlan_client(vc); |
1929 | } | |
1930 | ||
406c8df3 GC |
1931 | void net_set_boot_mask(int net_boot_mask) |
1932 | { | |
1933 | int i; | |
1934 | ||
1935 | /* Only the first four NICs may be bootable */ | |
1936 | net_boot_mask = net_boot_mask & 0xF; | |
1937 | ||
1938 | for (i = 0; i < nb_nics; i++) { | |
1939 | if (net_boot_mask & (1 << i)) { | |
1940 | nd_table[i].bootable = 1; | |
1941 | net_boot_mask &= ~(1 << i); | |
1942 | } | |
1943 | } | |
1944 | ||
1945 | if (net_boot_mask) { | |
1946 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); | |
1947 | exit(1); | |
1948 | } | |
1949 | } | |
1950 | ||
376253ec | 1951 | void do_info_network(Monitor *mon) |
63a01ef8 AL |
1952 | { |
1953 | VLANState *vlan; | |
63a01ef8 | 1954 | |
5610c3aa MM |
1955 | QTAILQ_FOREACH(vlan, &vlans, next) { |
1956 | VLANClientState *vc; | |
1957 | ||
376253ec | 1958 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
5610c3aa MM |
1959 | |
1960 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
376253ec | 1961 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
5610c3aa | 1962 | } |
63a01ef8 AL |
1963 | } |
1964 | } | |
1965 | ||
f18c16de | 1966 | void do_set_link(Monitor *mon, const QDict *qdict) |
436e5e53 AL |
1967 | { |
1968 | VLANState *vlan; | |
1969 | VLANClientState *vc = NULL; | |
f18c16de LC |
1970 | const char *name = qdict_get_str(qdict, "name"); |
1971 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); | |
436e5e53 | 1972 | |
5610c3aa MM |
1973 | QTAILQ_FOREACH(vlan, &vlans, next) { |
1974 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
1975 | if (strcmp(vc->name, name) == 0) { | |
dd5de373 | 1976 | goto done; |
5610c3aa MM |
1977 | } |
1978 | } | |
1979 | } | |
dd5de373 | 1980 | done: |
436e5e53 AL |
1981 | |
1982 | if (!vc) { | |
7dc3fa09 | 1983 | monitor_printf(mon, "could not find network device '%s'\n", name); |
c3cf0d3f | 1984 | return; |
436e5e53 AL |
1985 | } |
1986 | ||
1987 | if (strcmp(up_or_down, "up") == 0) | |
1988 | vc->link_down = 0; | |
1989 | else if (strcmp(up_or_down, "down") == 0) | |
1990 | vc->link_down = 1; | |
1991 | else | |
376253ec AL |
1992 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
1993 | "valid\n", up_or_down); | |
436e5e53 | 1994 | |
34b25ca7 AL |
1995 | if (vc->link_status_changed) |
1996 | vc->link_status_changed(vc); | |
436e5e53 AL |
1997 | } |
1998 | ||
63a01ef8 AL |
1999 | void net_cleanup(void) |
2000 | { | |
2001 | VLANState *vlan; | |
577c4af9 | 2002 | VLANClientState *vc, *next_vc; |
63a01ef8 | 2003 | |
5610c3aa | 2004 | QTAILQ_FOREACH(vlan, &vlans, next) { |
5610c3aa | 2005 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
b946a153 | 2006 | qemu_del_vlan_client(vc); |
63a01ef8 AL |
2007 | } |
2008 | } | |
577c4af9 MM |
2009 | |
2010 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { | |
2011 | qemu_del_vlan_client(vc); | |
2012 | } | |
63a01ef8 AL |
2013 | } |
2014 | ||
dc1c9fe8 | 2015 | static void net_check_clients(void) |
63a01ef8 AL |
2016 | { |
2017 | VLANState *vlan; | |
2018 | ||
5610c3aa | 2019 | QTAILQ_FOREACH(vlan, &vlans, next) { |
63a01ef8 AL |
2020 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
2021 | continue; | |
2022 | if (vlan->nb_guest_devs == 0) | |
2023 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); | |
2024 | if (vlan->nb_host_devs == 0) | |
2025 | fprintf(stderr, | |
2026 | "Warning: vlan %d is not connected to host network\n", | |
2027 | vlan->id); | |
2028 | } | |
2029 | } | |
dc1c9fe8 MM |
2030 | |
2031 | static int net_init_client(QemuOpts *opts, void *dummy) | |
2032 | { | |
c1671a08 MM |
2033 | if (net_client_init(NULL, opts, 0) < 0) |
2034 | return -1; | |
2035 | return 0; | |
f6b134ac MM |
2036 | } |
2037 | ||
2038 | static int net_init_netdev(QemuOpts *opts, void *dummy) | |
2039 | { | |
2040 | return net_client_init(NULL, opts, 1); | |
dc1c9fe8 MM |
2041 | } |
2042 | ||
2043 | int net_init_clients(void) | |
2044 | { | |
2045 | if (QTAILQ_EMPTY(&qemu_net_opts.head)) { | |
2046 | /* if no clients, we use a default config */ | |
2047 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); | |
2048 | #ifdef CONFIG_SLIRP | |
2049 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); | |
2050 | #endif | |
2051 | } | |
2052 | ||
5610c3aa | 2053 | QTAILQ_INIT(&vlans); |
577c4af9 | 2054 | QTAILQ_INIT(&non_vlan_clients); |
5610c3aa | 2055 | |
f6b134ac MM |
2056 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
2057 | return -1; | |
2058 | ||
dc1c9fe8 MM |
2059 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
2060 | return -1; | |
2061 | } | |
2062 | ||
2063 | net_check_clients(); | |
2064 | ||
2065 | return 0; | |
2066 | } | |
2067 | ||
7f161aae | 2068 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
dc1c9fe8 | 2069 | { |
a3a766e7 | 2070 | #if defined(CONFIG_SLIRP) |
68ac40d2 MM |
2071 | int ret; |
2072 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { | |
dc1c9fe8 MM |
2073 | return ret; |
2074 | } | |
a3a766e7 | 2075 | #endif |
68ac40d2 | 2076 | |
7f161aae | 2077 | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
dc1c9fe8 MM |
2078 | return -1; |
2079 | } | |
2080 | ||
2081 | return 0; | |
2082 | } |