build-fix typo in target-ia64/libkvm.c
[qemu-kvm/fedora.git] / net.c
blobfb70b7084f704faf48bd63682bd1dc4c7907e114
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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>
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
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>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #ifdef __NetBSD__
46 #include <net/if_tap.h>
47 #endif
48 #ifdef __linux__
49 #include <linux/if_tun.h>
50 #endif
51 #include <arpa/inet.h>
52 #include <dirent.h>
53 #include <netdb.h>
54 #include <sys/select.h>
55 #ifdef HOST_BSD
56 #include <sys/stat.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #include <libutil.h>
59 #else
60 #include <util.h>
61 #endif
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
64 #else
65 #ifdef __linux__
66 #include <pty.h>
67 #include <malloc.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
73 #include "hpet.h"
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
77 #endif
78 #ifdef __sun__
79 #include <sys/stat.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
89 #include <net/if.h>
90 #include <syslog.h>
91 #include <stropts.h>
92 #endif
93 #endif
94 #endif
96 #if defined(__OpenBSD__)
97 #include <util.h>
98 #endif
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
102 #endif
104 #ifdef _WIN32
105 #include <windows.h>
106 #include <malloc.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
111 #endif
113 // FIXME: #include "qemu-kvm.h"
114 #include "qemu-common.h"
115 #include "net.h"
116 #include "monitor.h"
117 #include "sysemu.h"
118 #include "qemu-timer.h"
119 #include "qemu-char.h"
120 #include "audio/audio.h"
121 #include "qemu_socket.h"
122 #include "qemu-log.h"
124 #if defined(CONFIG_SLIRP)
125 #include "libslirp.h"
126 #endif
129 static VLANState *first_vlan;
131 /***********************************************************/
132 /* network device redirectors */
134 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
135 static void hex_dump(FILE *f, const uint8_t *buf, int size)
137 int len, i, j, c;
139 for(i=0;i<size;i+=16) {
140 len = size - i;
141 if (len > 16)
142 len = 16;
143 fprintf(f, "%08x ", i);
144 for(j=0;j<16;j++) {
145 if (j < len)
146 fprintf(f, " %02x", buf[i+j]);
147 else
148 fprintf(f, " ");
150 fprintf(f, " ");
151 for(j=0;j<len;j++) {
152 c = buf[i+j];
153 if (c < ' ' || c > '~')
154 c = '.';
155 fprintf(f, "%c", c);
157 fprintf(f, "\n");
160 #endif
162 static int parse_macaddr(uint8_t *macaddr, const char *p)
164 int i;
165 char *last_char;
166 long int offset;
168 errno = 0;
169 offset = strtol(p, &last_char, 0);
170 if (0 == errno && '\0' == *last_char &&
171 offset >= 0 && offset <= 0xFFFFFF) {
172 macaddr[3] = (offset & 0xFF0000) >> 16;
173 macaddr[4] = (offset & 0xFF00) >> 8;
174 macaddr[5] = offset & 0xFF;
175 return 0;
176 } else {
177 for(i = 0; i < 6; i++) {
178 macaddr[i] = strtol(p, (char **)&p, 16);
179 if (i == 5) {
180 if (*p != '\0')
181 return -1;
182 } else {
183 if (*p != ':' && *p != '-')
184 return -1;
185 p++;
188 return 0;
191 return -1;
194 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
196 const char *p, *p1;
197 int len;
198 p = *pp;
199 p1 = strchr(p, sep);
200 if (!p1)
201 return -1;
202 len = p1 - p;
203 p1++;
204 if (buf_size > 0) {
205 if (len > buf_size - 1)
206 len = buf_size - 1;
207 memcpy(buf, p, len);
208 buf[len] = '\0';
210 *pp = p1;
211 return 0;
214 int parse_host_src_port(struct sockaddr_in *haddr,
215 struct sockaddr_in *saddr,
216 const char *input_str)
218 char *str = strdup(input_str);
219 char *host_str = str;
220 char *src_str;
221 const char *src_str2;
222 char *ptr;
225 * Chop off any extra arguments at the end of the string which
226 * would start with a comma, then fill in the src port information
227 * if it was provided else use the "any address" and "any port".
229 if ((ptr = strchr(str,',')))
230 *ptr = '\0';
232 if ((src_str = strchr(input_str,'@'))) {
233 *src_str = '\0';
234 src_str++;
237 if (parse_host_port(haddr, host_str) < 0)
238 goto fail;
240 src_str2 = src_str;
241 if (!src_str || *src_str == '\0')
242 src_str2 = ":0";
244 if (parse_host_port(saddr, src_str2) < 0)
245 goto fail;
247 free(str);
248 return(0);
250 fail:
251 free(str);
252 return -1;
255 int parse_host_port(struct sockaddr_in *saddr, const char *str)
257 char buf[512];
258 struct hostent *he;
259 const char *p, *r;
260 int port;
262 p = str;
263 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
264 return -1;
265 saddr->sin_family = AF_INET;
266 if (buf[0] == '\0') {
267 saddr->sin_addr.s_addr = 0;
268 } else {
269 if (qemu_isdigit(buf[0])) {
270 if (!inet_aton(buf, &saddr->sin_addr))
271 return -1;
272 } else {
273 if ((he = gethostbyname(buf)) == NULL)
274 return - 1;
275 saddr->sin_addr = *(struct in_addr *)he->h_addr;
278 port = strtol(p, (char **)&r, 0);
279 if (r == p)
280 return -1;
281 saddr->sin_port = htons(port);
282 return 0;
285 #if !defined(_WIN32) && 0
286 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
288 const char *p;
289 int len;
291 len = MIN(108, strlen(str));
292 p = strchr(str, ',');
293 if (p)
294 len = MIN(len, p - str);
296 memset(uaddr, 0, sizeof(*uaddr));
298 uaddr->sun_family = AF_UNIX;
299 memcpy(uaddr->sun_path, str, len);
301 return 0;
303 #endif
305 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
307 snprintf(vc->info_str, sizeof(vc->info_str),
308 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
309 vc->model,
310 macaddr[0], macaddr[1], macaddr[2],
311 macaddr[3], macaddr[4], macaddr[5]);
314 static char *assign_name(VLANClientState *vc1, const char *model)
316 VLANState *vlan;
317 char buf[256];
318 int id = 0;
320 for (vlan = first_vlan; vlan; vlan = vlan->next) {
321 VLANClientState *vc;
323 for (vc = vlan->first_client; vc; vc = vc->next)
324 if (vc != vc1 && strcmp(vc->model, model) == 0)
325 id++;
328 snprintf(buf, sizeof(buf), "%s.%d", model, id);
330 return strdup(buf);
333 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
334 const char *model,
335 const char *name,
336 IOReadHandler *fd_read,
337 IOCanRWHandler *fd_can_read,
338 NetCleanup *cleanup,
339 void *opaque)
341 VLANClientState *vc, **pvc;
342 vc = qemu_mallocz(sizeof(VLANClientState));
343 vc->model = strdup(model);
344 if (name)
345 vc->name = strdup(name);
346 else
347 vc->name = assign_name(vc, model);
348 vc->fd_read = fd_read;
349 vc->fd_can_read = fd_can_read;
350 vc->cleanup = cleanup;
351 vc->opaque = opaque;
352 vc->vlan = vlan;
354 vc->next = NULL;
355 pvc = &vlan->first_client;
356 while (*pvc != NULL)
357 pvc = &(*pvc)->next;
358 *pvc = vc;
359 return vc;
362 void qemu_del_vlan_client(VLANClientState *vc)
364 VLANClientState **pvc = &vc->vlan->first_client;
366 while (*pvc != NULL)
367 if (*pvc == vc) {
368 *pvc = vc->next;
369 if (vc->cleanup) {
370 vc->cleanup(vc);
372 free(vc->name);
373 free(vc->model);
374 qemu_free(vc);
375 break;
376 } else
377 pvc = &(*pvc)->next;
380 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
382 VLANClientState **pvc = &vlan->first_client;
384 while (*pvc != NULL)
385 if ((*pvc)->opaque == opaque)
386 return *pvc;
387 else
388 pvc = &(*pvc)->next;
390 return NULL;
393 int qemu_can_send_packet(VLANClientState *vc1)
395 VLANState *vlan = vc1->vlan;
396 VLANClientState *vc;
398 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
399 if (vc != vc1) {
400 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
401 return 1;
404 return 0;
407 static int
408 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
410 VLANClientState *vc;
411 int ret = -EAGAIN;
413 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
414 if (vc != sender) {
415 if (vc->link_down) {
416 ret = 0;
417 } else if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
418 vc->fd_read(vc->opaque, buf, size);
419 ret = 0;
423 return ret;
426 int qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
428 VLANState *vlan = vc->vlan;
429 VLANPacket *packet;
430 int ret = -EAGAIN;
432 if (vc->link_down)
433 return 0;
435 #ifdef DEBUG_NET
436 printf("vlan %d send:\n", vlan->id);
437 hex_dump(stdout, buf, size);
438 #endif
439 if (vlan->delivering) {
440 packet = qemu_malloc(sizeof(VLANPacket) + size);
441 packet->next = vlan->send_queue;
442 packet->sender = vc;
443 packet->size = size;
444 memcpy(packet->data, buf, size);
445 vlan->send_queue = packet;
446 } else {
447 vlan->delivering = 1;
448 ret = qemu_deliver_packet(vc, buf, size);
449 while ((packet = vlan->send_queue) != NULL) {
450 qemu_deliver_packet(packet->sender, packet->data, packet->size);
451 vlan->send_queue = packet->next;
452 qemu_free(packet);
454 vlan->delivering = 0;
456 return ret;
459 void qemu_send_packet_raw(VLANClientState *sender, const uint8_t *buf, int size)
461 VLANState *vlan = sender->vlan;
462 VLANClientState *vc;
464 if (sender->link_down)
465 return;
467 #ifdef DEBUG_NET
468 printf("vlan %d send raw:\n", vlan->id);
469 hex_dump(stdout, buf, size);
470 #endif
471 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
472 if (vc == sender || vc->link_down) {
473 continue;
475 if (vc->fd_read_raw) {
476 vc->fd_read_raw(vc->opaque, buf, size);
477 } else {
478 vc->fd_read(vc->opaque, buf, size);
481 return;
484 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
485 int iovcnt)
487 uint8_t buffer[4096];
488 size_t offset = 0;
489 int i;
491 for (i = 0; i < iovcnt; i++) {
492 size_t len;
494 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
495 memcpy(buffer + offset, iov[i].iov_base, len);
496 offset += len;
499 vc->fd_read(vc->opaque, buffer, offset);
501 return offset;
504 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
506 size_t offset = 0;
507 int i;
509 for (i = 0; i < iovcnt; i++)
510 offset += iov[i].iov_len;
511 return offset;
514 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
515 int iovcnt)
517 VLANState *vlan = vc1->vlan;
518 VLANClientState *vc;
519 ssize_t max_len = 0;
521 if (vc1->link_down)
522 return calc_iov_length(iov, iovcnt);
524 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
525 ssize_t len = 0;
527 if (vc == vc1)
528 continue;
530 if (vc->link_down)
531 len = calc_iov_length(iov, iovcnt);
532 if (vc->fd_readv)
533 len = vc->fd_readv(vc->opaque, iov, iovcnt);
534 else if (vc->fd_read)
535 len = vc_sendv_compat(vc, iov, iovcnt);
537 max_len = MAX(max_len, len);
540 return max_len;
543 #if defined(CONFIG_SLIRP)
545 /* slirp network adapter */
547 static int slirp_inited;
548 static int slirp_restrict;
549 static char *slirp_ip;
550 static VLANClientState *slirp_vc;
552 int slirp_can_output(void)
554 return !slirp_vc || qemu_can_send_packet(slirp_vc);
557 void slirp_output(const uint8_t *pkt, int pkt_len)
559 #ifdef DEBUG_SLIRP
560 printf("slirp output:\n");
561 hex_dump(stdout, pkt, pkt_len);
562 #endif
563 if (!slirp_vc)
564 return;
565 qemu_send_packet(slirp_vc, pkt, pkt_len);
568 int slirp_is_inited(void)
570 return slirp_inited;
573 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
575 #ifdef DEBUG_SLIRP
576 printf("slirp input:\n");
577 hex_dump(stdout, buf, size);
578 #endif
579 slirp_input(buf, size);
582 static int slirp_in_use;
584 static void net_slirp_cleanup(VLANClientState *vc)
586 slirp_in_use = 0;
589 static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
591 if (slirp_in_use) {
592 /* slirp only supports a single instance so far */
593 return -1;
595 if (!slirp_inited) {
596 slirp_inited = 1;
597 slirp_init(slirp_restrict, slirp_ip);
599 slirp_vc = qemu_new_vlan_client(vlan, model, name,
600 slirp_receive, NULL, net_slirp_cleanup, NULL);
601 slirp_vc->info_str[0] = '\0';
602 slirp_in_use = 1;
603 return 0;
606 static void net_slirp_redir_print(void *opaque, int is_udp,
607 struct in_addr *laddr, u_int lport,
608 struct in_addr *faddr, u_int fport)
610 Monitor *mon = (Monitor *)opaque;
611 uint32_t h_addr;
612 uint32_t g_addr;
613 char buf[16];
615 h_addr = ntohl(faddr->s_addr);
616 g_addr = ntohl(laddr->s_addr);
618 monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
619 snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
620 (h_addr >> 16) & 0xff,
621 (h_addr >> 8) & 0xff,
622 (h_addr) & 0xff);
623 monitor_printf(mon, " %15s |", buf);
624 monitor_printf(mon, " %5d |", fport);
626 snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
627 (g_addr >> 16) & 0xff,
628 (g_addr >> 8) & 0xff,
629 (g_addr) & 0xff);
630 monitor_printf(mon, " %15s |", buf);
631 monitor_printf(mon, " %5d\n", lport);
635 static void net_slirp_redir_list(Monitor *mon)
637 if (!mon)
638 return;
640 monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
641 monitor_printf(mon, " | | | | \n");
642 slirp_redir_loop(net_slirp_redir_print, mon);
645 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
647 int host_port;
648 char buf[256] = "";
649 const char *p = port_str;
650 int is_udp = 0;
651 int n;
653 if (!mon)
654 return;
656 if (!port_str || !port_str[0])
657 goto fail_syntax;
659 get_str_sep(buf, sizeof(buf), &p, ':');
661 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
662 is_udp = 0;
663 } else if (!strcmp(buf, "udp")) {
664 is_udp = 1;
665 } else {
666 goto fail_syntax;
669 host_port = atoi(p);
671 n = slirp_redir_rm(is_udp, host_port);
673 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
674 is_udp ? "udp" : "tcp", host_port);
675 return;
677 fail_syntax:
678 monitor_printf(mon, "invalid format\n");
681 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
683 int is_udp;
684 char buf[256], *r;
685 const char *p, *errmsg;
686 struct in_addr guest_addr;
687 int host_port, guest_port;
689 if (!slirp_inited) {
690 slirp_inited = 1;
691 slirp_init(slirp_restrict, slirp_ip);
694 if (!strcmp(redir_str, "remove")) {
695 net_slirp_redir_rm(mon, redir_opt2);
696 return;
699 if (!strcmp(redir_str, "list")) {
700 net_slirp_redir_list(mon);
701 return;
704 p = redir_str;
705 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
706 goto fail_syntax;
707 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
708 is_udp = 0;
709 } else if (!strcmp(buf, "udp")) {
710 is_udp = 1;
711 } else {
712 goto fail_syntax;
715 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
716 goto fail_syntax;
717 host_port = strtol(buf, &r, 0);
718 if (r == buf)
719 goto fail_syntax;
721 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
722 goto fail_syntax;
723 if (buf[0] == '\0') {
724 pstrcpy(buf, sizeof(buf), "10.0.2.15");
726 if (!inet_aton(buf, &guest_addr))
727 goto fail_syntax;
729 guest_port = strtol(p, &r, 0);
730 if (r == p)
731 goto fail_syntax;
733 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
734 errmsg = "could not set up redirection\n";
735 goto fail;
737 return;
739 fail_syntax:
740 errmsg = "invalid redirection format\n";
741 fail:
742 if (mon) {
743 monitor_printf(mon, "%s", errmsg);
744 } else {
745 fprintf(stderr, "qemu: %s", errmsg);
746 exit(1);
750 #ifndef _WIN32
752 static char smb_dir[1024];
754 static void erase_dir(char *dir_name)
756 DIR *d;
757 struct dirent *de;
758 char filename[1024];
760 /* erase all the files in the directory */
761 if ((d = opendir(dir_name)) != NULL) {
762 for(;;) {
763 de = readdir(d);
764 if (!de)
765 break;
766 if (strcmp(de->d_name, ".") != 0 &&
767 strcmp(de->d_name, "..") != 0) {
768 snprintf(filename, sizeof(filename), "%s/%s",
769 smb_dir, de->d_name);
770 if (unlink(filename) != 0) /* is it a directory? */
771 erase_dir(filename);
774 closedir(d);
775 rmdir(dir_name);
779 /* automatic user mode samba server configuration */
780 static void smb_exit(void)
782 erase_dir(smb_dir);
785 /* automatic user mode samba server configuration */
786 void net_slirp_smb(const char *exported_dir)
788 char smb_conf[1024];
789 char smb_cmdline[1024];
790 FILE *f;
792 if (!slirp_inited) {
793 slirp_inited = 1;
794 slirp_init(slirp_restrict, slirp_ip);
797 /* XXX: better tmp dir construction */
798 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
799 if (mkdir(smb_dir, 0700) < 0) {
800 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
801 exit(1);
803 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
805 f = fopen(smb_conf, "w");
806 if (!f) {
807 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
808 exit(1);
810 fprintf(f,
811 "[global]\n"
812 "private dir=%s\n"
813 "smb ports=0\n"
814 "socket address=127.0.0.1\n"
815 "pid directory=%s\n"
816 "lock directory=%s\n"
817 "log file=%s/log.smbd\n"
818 "smb passwd file=%s/smbpasswd\n"
819 "security = share\n"
820 "[qemu]\n"
821 "path=%s\n"
822 "read only=no\n"
823 "guest ok=yes\n",
824 smb_dir,
825 smb_dir,
826 smb_dir,
827 smb_dir,
828 smb_dir,
829 exported_dir
831 fclose(f);
832 atexit(smb_exit);
834 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
835 SMBD_COMMAND, smb_conf);
837 slirp_add_exec(0, smb_cmdline, 4, 139);
840 #endif /* !defined(_WIN32) */
841 void do_info_slirp(Monitor *mon)
843 slirp_stats();
846 struct VMChannel {
847 CharDriverState *hd;
848 int port;
851 static int vmchannel_can_read(void *opaque)
853 struct VMChannel *vmc = (struct VMChannel*)opaque;
854 return slirp_socket_can_recv(4, vmc->port);
857 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
859 struct VMChannel *vmc = (struct VMChannel*)opaque;
860 slirp_socket_recv(4, vmc->port, buf, size);
863 #endif /* CONFIG_SLIRP */
865 #ifdef _WIN32
867 int tap_has_vnet_hdr(void *opaque)
869 return 0;
872 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
876 #else /* !defined(_WIN32) */
878 /* Maximum GSO packet size (64k) plus plenty of room for
879 * the ethernet and virtio_net headers
881 #define TAP_BUFSIZE (4096 + 65536)
883 #ifdef IFF_VNET_HDR
884 #include <linux/virtio_net.h>
885 #endif
887 typedef struct TAPState {
888 VLANClientState *vc;
889 int fd;
890 char down_script[1024];
891 char down_script_arg[128];
892 char buf[TAP_BUFSIZE];
893 int size;
894 unsigned int has_vnet_hdr : 1;
895 unsigned int using_vnet_hdr : 1;
896 } TAPState;
898 static int launch_script(const char *setup_script, const char *ifname, int fd);
900 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
901 int iovcnt)
903 TAPState *s = opaque;
904 ssize_t len;
906 do {
907 len = writev(s->fd, iov, iovcnt);
908 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
910 return len;
913 static void tap_receive(void *opaque, const uint8_t *buf, int size)
915 struct iovec iov[2];
916 int i = 0;
918 #ifdef IFF_VNET_HDR
919 TAPState *s = opaque;
920 struct virtio_net_hdr hdr = { 0, };
922 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
923 iov[i].iov_base = &hdr;
924 iov[i].iov_len = sizeof(hdr);
925 i++;
927 #endif
929 iov[i].iov_base = (char *) buf;
930 iov[i].iov_len = size;
931 i++;
933 tap_receive_iov(opaque, iov, i);
936 static void tap_receive_raw(void *opaque, const uint8_t *buf, int size)
938 struct iovec iov[2];
939 int i = 0;
941 #ifdef IFF_VNET_HDR
942 TAPState *s = opaque;
943 struct virtio_net_hdr hdr = { 0, };
945 if (s->has_vnet_hdr && s->using_vnet_hdr) {
946 iov[i].iov_base = &hdr;
947 iov[i].iov_len = sizeof(hdr);
948 i++;
950 #endif
952 iov[i].iov_base = (char *) buf;
953 iov[i].iov_len = size;
954 i++;
956 tap_receive_iov(opaque, iov, i);
959 static int tap_can_send(void *opaque)
961 TAPState *s = opaque;
962 VLANClientState *vc;
963 int can_receive = 0;
965 /* Check to see if any of our clients can receive a packet */
966 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
967 /* Skip ourselves */
968 if (vc == s->vc)
969 continue;
971 if (!vc->fd_can_read) {
972 /* no fd_can_read handler, they always can receive */
973 can_receive = 1;
974 } else
975 can_receive = vc->fd_can_read(vc->opaque);
977 /* Once someone can receive, we try to send a packet */
978 if (can_receive)
979 break;
982 return can_receive;
985 static int tap_send_packet(TAPState *s)
987 uint8_t *buf = (uint8_t *)s->buf;
988 int size = s->size;
990 #ifdef IFF_VNET_HDR
991 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
992 buf += sizeof(struct virtio_net_hdr);
993 size -= sizeof(struct virtio_net_hdr);
995 #endif
997 return qemu_send_packet(s->vc, buf, size);
1000 static void tap_send(void *opaque)
1002 TAPState *s = opaque;
1004 /* First try to send any buffered packet */
1005 if (s->size > 0) {
1006 int err;
1008 /* If noone can receive the packet, buffer it */
1009 err = tap_send_packet(s);
1010 if (err == -EAGAIN)
1011 return;
1014 /* Read packets until we hit EAGAIN */
1015 do {
1016 #ifdef __sun__
1017 struct strbuf sbuf;
1018 int f = 0;
1019 sbuf.maxlen = sizeof(buf);
1020 sbuf.buf = (char *)buf;
1021 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
1022 #else
1023 // FIXME: kvm_sleep_begin();
1024 s->size = read(s->fd, s->buf, sizeof(s->buf));
1025 // FIXME: kvm_sleep_end();
1026 #endif
1028 if (s->size == -1 && errno == EINTR)
1029 continue;
1031 if (s->size > 0) {
1032 int err;
1034 /* If noone can receive the packet, buffer it */
1035 err = tap_send_packet(s);
1036 if (err == -EAGAIN)
1037 break;
1039 } while (s->size > 0);
1042 int tap_has_vnet_hdr(void *opaque)
1044 VLANClientState *vc = opaque;
1045 TAPState *s = vc->opaque;
1047 return s ? s->has_vnet_hdr : 0;
1050 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1052 VLANClientState *vc = opaque;
1053 TAPState *s = vc->opaque;
1055 if (!s || !s->has_vnet_hdr)
1056 return;
1058 s->using_vnet_hdr = using_vnet_hdr != 0;
1061 static int tap_probe_vnet_hdr(int fd)
1063 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1064 struct ifreq ifr;
1066 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
1067 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
1068 return 0;
1071 return ifr.ifr_flags & IFF_VNET_HDR;
1072 #else
1073 return 0;
1074 #endif
1077 #ifdef TUNSETOFFLOAD
1078 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
1079 int ecn)
1081 TAPState *s = vc->opaque;
1082 unsigned int offload = 0;
1084 if (csum) {
1085 offload |= TUN_F_CSUM;
1086 if (tso4)
1087 offload |= TUN_F_TSO4;
1088 if (tso6)
1089 offload |= TUN_F_TSO6;
1090 if ((tso4 || tso6) && ecn)
1091 offload |= TUN_F_TSO_ECN;
1094 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
1095 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
1096 strerror(errno));
1098 #endif /* TUNSETOFFLOAD */
1100 static void tap_cleanup(VLANClientState *vc)
1102 TAPState *s = vc->opaque;
1104 if (s->down_script[0])
1105 launch_script(s->down_script, s->down_script_arg, s->fd);
1107 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1108 close(s->fd);
1109 qemu_free(s);
1112 /* fd support */
1114 static TAPState *net_tap_fd_init(VLANState *vlan,
1115 const char *model,
1116 const char *name,
1117 int fd,
1118 int vnet_hdr)
1120 TAPState *s;
1122 s = qemu_mallocz(sizeof(TAPState));
1123 s->fd = fd;
1124 s->has_vnet_hdr = vnet_hdr != 0;
1125 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
1126 NULL, tap_cleanup, s);
1127 s->vc->fd_readv = tap_receive_iov;
1128 s->vc->fd_read_raw = tap_receive_raw;
1129 #ifdef TUNSETOFFLOAD
1130 s->vc->set_offload = tap_set_offload;
1131 tap_set_offload(s->vc, 0, 0, 0, 0);
1132 #endif
1133 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
1134 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1135 return s;
1138 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1139 static int tap_open(char *ifname, int ifname_size)
1141 int fd;
1142 char *dev;
1143 struct stat s;
1145 TFR(fd = open("/dev/tap", O_RDWR));
1146 if (fd < 0) {
1147 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1148 return -1;
1151 fstat(fd, &s);
1152 dev = devname(s.st_rdev, S_IFCHR);
1153 pstrcpy(ifname, ifname_size, dev);
1155 fcntl(fd, F_SETFL, O_NONBLOCK);
1156 return fd;
1158 #elif defined(__sun__)
1159 #define TUNNEWPPA (('T'<<16) | 0x0001)
1161 * Allocate TAP device, returns opened fd.
1162 * Stores dev name in the first arg(must be large enough).
1164 static int tap_alloc(char *dev, size_t dev_size)
1166 int tap_fd, if_fd, ppa = -1;
1167 static int ip_fd = 0;
1168 char *ptr;
1170 static int arp_fd = 0;
1171 int ip_muxid, arp_muxid;
1172 struct strioctl strioc_if, strioc_ppa;
1173 int link_type = I_PLINK;;
1174 struct lifreq ifr;
1175 char actual_name[32] = "";
1177 memset(&ifr, 0x0, sizeof(ifr));
1179 if( *dev ){
1180 ptr = dev;
1181 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1182 ppa = atoi(ptr);
1185 /* Check if IP device was opened */
1186 if( ip_fd )
1187 close(ip_fd);
1189 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1190 if (ip_fd < 0) {
1191 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1192 return -1;
1195 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1196 if (tap_fd < 0) {
1197 syslog(LOG_ERR, "Can't open /dev/tap");
1198 return -1;
1201 /* Assign a new PPA and get its unit number. */
1202 strioc_ppa.ic_cmd = TUNNEWPPA;
1203 strioc_ppa.ic_timout = 0;
1204 strioc_ppa.ic_len = sizeof(ppa);
1205 strioc_ppa.ic_dp = (char *)&ppa;
1206 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1207 syslog (LOG_ERR, "Can't assign new interface");
1209 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1210 if (if_fd < 0) {
1211 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1212 return -1;
1214 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1215 syslog(LOG_ERR, "Can't push IP module");
1216 return -1;
1219 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1220 syslog(LOG_ERR, "Can't get flags\n");
1222 snprintf (actual_name, 32, "tap%d", ppa);
1223 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1225 ifr.lifr_ppa = ppa;
1226 /* Assign ppa according to the unit number returned by tun device */
1228 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1229 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1230 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1231 syslog (LOG_ERR, "Can't get flags\n");
1232 /* Push arp module to if_fd */
1233 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1234 syslog (LOG_ERR, "Can't push ARP module (2)");
1236 /* Push arp module to ip_fd */
1237 if (ioctl (ip_fd, I_POP, NULL) < 0)
1238 syslog (LOG_ERR, "I_POP failed\n");
1239 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1240 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1241 /* Open arp_fd */
1242 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1243 if (arp_fd < 0)
1244 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1246 /* Set ifname to arp */
1247 strioc_if.ic_cmd = SIOCSLIFNAME;
1248 strioc_if.ic_timout = 0;
1249 strioc_if.ic_len = sizeof(ifr);
1250 strioc_if.ic_dp = (char *)&ifr;
1251 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1252 syslog (LOG_ERR, "Can't set ifname to arp\n");
1255 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1256 syslog(LOG_ERR, "Can't link TAP device to IP");
1257 return -1;
1260 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1261 syslog (LOG_ERR, "Can't link TAP device to ARP");
1263 close (if_fd);
1265 memset(&ifr, 0x0, sizeof(ifr));
1266 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1267 ifr.lifr_ip_muxid = ip_muxid;
1268 ifr.lifr_arp_muxid = arp_muxid;
1270 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1272 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1273 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1274 syslog (LOG_ERR, "Can't set multiplexor id");
1277 snprintf(dev, dev_size, "tap%d", ppa);
1278 return tap_fd;
1281 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1283 char dev[10]="";
1284 int fd;
1285 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1286 fprintf(stderr, "Cannot allocate TAP device\n");
1287 return -1;
1289 pstrcpy(ifname, ifname_size, dev);
1290 fcntl(fd, F_SETFL, O_NONBLOCK);
1291 return fd;
1293 #elif defined (_AIX)
1294 static int tap_open(char *ifname, int ifname_size)
1296 fprintf (stderr, "no tap on AIX\n");
1297 return -1;
1299 #else
1300 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1302 struct ifreq ifr;
1303 int fd, ret;
1305 TFR(fd = open("/dev/net/tun", O_RDWR));
1306 if (fd < 0) {
1307 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1308 return -1;
1310 memset(&ifr, 0, sizeof(ifr));
1311 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1313 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1315 unsigned int features;
1317 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1318 features & IFF_VNET_HDR) {
1319 *vnet_hdr = 1;
1320 ifr.ifr_flags |= IFF_VNET_HDR;
1323 #endif
1325 if (ifname[0] != '\0')
1326 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1327 else
1328 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1329 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1330 if (ret != 0) {
1331 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1332 close(fd);
1333 return -1;
1335 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1336 fcntl(fd, F_SETFL, O_NONBLOCK);
1337 return fd;
1339 #endif
1341 static int launch_script(const char *setup_script, const char *ifname, int fd)
1343 int pid, status;
1344 char *args[3];
1345 char **parg;
1347 /* try to launch network script */
1348 pid = fork();
1349 if (pid >= 0) {
1350 if (pid == 0) {
1351 int open_max = sysconf (_SC_OPEN_MAX), i;
1352 for (i = 0; i < open_max; i++)
1353 if (i != STDIN_FILENO &&
1354 i != STDOUT_FILENO &&
1355 i != STDERR_FILENO &&
1356 i != fd)
1357 close(i);
1359 parg = args;
1360 *parg++ = (char *)setup_script;
1361 *parg++ = (char *)ifname;
1362 *parg++ = NULL;
1363 execv(setup_script, args);
1364 _exit(1);
1366 while (waitpid(pid, &status, 0) != pid);
1367 if (!WIFEXITED(status) ||
1368 WEXITSTATUS(status) != 0) {
1369 fprintf(stderr, "%s: could not launch network script\n",
1370 setup_script);
1371 return -1;
1374 return 0;
1377 static int net_tap_init(VLANState *vlan, const char *model,
1378 const char *name, const char *ifname1,
1379 const char *setup_script, const char *down_script)
1381 TAPState *s;
1382 int fd;
1383 int vnet_hdr;
1384 char ifname[128];
1386 if (ifname1 != NULL)
1387 pstrcpy(ifname, sizeof(ifname), ifname1);
1388 else
1389 ifname[0] = '\0';
1390 vnet_hdr = 0;
1391 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1392 if (fd < 0)
1393 return -1;
1395 if (!setup_script || !strcmp(setup_script, "no"))
1396 setup_script = "";
1397 if (setup_script[0] != '\0') {
1398 if (launch_script(setup_script, ifname, fd))
1399 return -1;
1401 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1402 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1403 "ifname=%s,script=%s,downscript=%s",
1404 ifname, setup_script, down_script);
1405 if (down_script && strcmp(down_script, "no")) {
1406 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1407 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1409 return 0;
1412 #endif /* !_WIN32 */
1414 #if defined(CONFIG_VDE)
1415 typedef struct VDEState {
1416 VLANClientState *vc;
1417 VDECONN *vde;
1418 } VDEState;
1420 static void vde_to_qemu(void *opaque)
1422 VDEState *s = opaque;
1423 uint8_t buf[4096];
1424 int size;
1426 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1427 if (size > 0) {
1428 qemu_send_packet(s->vc, buf, size);
1432 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1434 VDEState *s = opaque;
1435 int ret;
1436 for(;;) {
1437 ret = vde_send(s->vde, (const char *)buf, size, 0);
1438 if (ret < 0 && errno == EINTR) {
1439 } else {
1440 break;
1445 static void vde_cleanup(VLANClientState *vc)
1447 VDEState *s = vc->opaque;
1448 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1449 vde_close(s->vde);
1450 qemu_free(s);
1453 static int net_vde_init(VLANState *vlan, const char *model,
1454 const char *name, const char *sock,
1455 int port, const char *group, int mode)
1457 VDEState *s;
1458 char *init_group = strlen(group) ? (char *)group : NULL;
1459 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1461 struct vde_open_args args = {
1462 .port = port,
1463 .group = init_group,
1464 .mode = mode,
1467 s = qemu_mallocz(sizeof(VDEState));
1468 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1469 if (!s->vde){
1470 free(s);
1471 return -1;
1473 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1474 NULL, vde_cleanup, s);
1475 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1476 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1477 sock, vde_datafd(s->vde));
1478 return 0;
1480 #endif
1482 /* network connection */
1483 typedef struct NetSocketState {
1484 VLANClientState *vc;
1485 int fd;
1486 int state; /* 0 = getting length, 1 = getting data */
1487 unsigned int index;
1488 unsigned int packet_len;
1489 uint8_t buf[4096];
1490 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1491 } NetSocketState;
1493 typedef struct NetSocketListenState {
1494 VLANState *vlan;
1495 char *model;
1496 char *name;
1497 int fd;
1498 } NetSocketListenState;
1500 /* XXX: we consider we can send the whole packet without blocking */
1501 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1503 NetSocketState *s = opaque;
1504 uint32_t len;
1505 len = htonl(size);
1507 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1508 send_all(s->fd, buf, size);
1511 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1513 NetSocketState *s = opaque;
1514 sendto(s->fd, buf, size, 0,
1515 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1518 static void net_socket_send(void *opaque)
1520 NetSocketState *s = opaque;
1521 int size, err;
1522 unsigned l;
1523 uint8_t buf1[4096];
1524 const uint8_t *buf;
1526 size = recv(s->fd, buf1, sizeof(buf1), 0);
1527 if (size < 0) {
1528 err = socket_error();
1529 if (err != EWOULDBLOCK)
1530 goto eoc;
1531 } else if (size == 0) {
1532 /* end of connection */
1533 eoc:
1534 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1535 closesocket(s->fd);
1536 return;
1538 buf = buf1;
1539 while (size > 0) {
1540 /* reassemble a packet from the network */
1541 switch(s->state) {
1542 case 0:
1543 l = 4 - s->index;
1544 if (l > size)
1545 l = size;
1546 memcpy(s->buf + s->index, buf, l);
1547 buf += l;
1548 size -= l;
1549 s->index += l;
1550 if (s->index == 4) {
1551 /* got length */
1552 s->packet_len = ntohl(*(uint32_t *)s->buf);
1553 s->index = 0;
1554 s->state = 1;
1556 break;
1557 case 1:
1558 l = s->packet_len - s->index;
1559 if (l > size)
1560 l = size;
1561 if (s->index + l <= sizeof(s->buf)) {
1562 memcpy(s->buf + s->index, buf, l);
1563 } else {
1564 fprintf(stderr, "serious error: oversized packet received,"
1565 "connection terminated.\n");
1566 s->state = 0;
1567 goto eoc;
1570 s->index += l;
1571 buf += l;
1572 size -= l;
1573 if (s->index >= s->packet_len) {
1574 qemu_send_packet(s->vc, s->buf, s->packet_len);
1575 s->index = 0;
1576 s->state = 0;
1578 break;
1583 static void net_socket_send_dgram(void *opaque)
1585 NetSocketState *s = opaque;
1586 int size;
1588 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1589 if (size < 0)
1590 return;
1591 if (size == 0) {
1592 /* end of connection */
1593 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1594 return;
1596 qemu_send_packet(s->vc, s->buf, size);
1599 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1601 struct ip_mreq imr;
1602 int fd;
1603 int val, ret;
1604 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1605 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1606 inet_ntoa(mcastaddr->sin_addr),
1607 (int)ntohl(mcastaddr->sin_addr.s_addr));
1608 return -1;
1611 fd = socket(PF_INET, SOCK_DGRAM, 0);
1612 if (fd < 0) {
1613 perror("socket(PF_INET, SOCK_DGRAM)");
1614 return -1;
1617 val = 1;
1618 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1619 (const char *)&val, sizeof(val));
1620 if (ret < 0) {
1621 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1622 goto fail;
1625 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1626 if (ret < 0) {
1627 perror("bind");
1628 goto fail;
1631 /* Add host to multicast group */
1632 imr.imr_multiaddr = mcastaddr->sin_addr;
1633 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1635 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1636 (const char *)&imr, sizeof(struct ip_mreq));
1637 if (ret < 0) {
1638 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1639 goto fail;
1642 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1643 val = 1;
1644 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1645 (const char *)&val, sizeof(val));
1646 if (ret < 0) {
1647 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1648 goto fail;
1651 socket_set_nonblock(fd);
1652 return fd;
1653 fail:
1654 if (fd >= 0)
1655 closesocket(fd);
1656 return -1;
1659 static void net_socket_cleanup(VLANClientState *vc)
1661 NetSocketState *s = vc->opaque;
1662 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1663 close(s->fd);
1664 qemu_free(s);
1667 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1668 const char *model,
1669 const char *name,
1670 int fd, int is_connected)
1672 struct sockaddr_in saddr;
1673 int newfd;
1674 socklen_t saddr_len;
1675 NetSocketState *s;
1677 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1678 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1679 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1682 if (is_connected) {
1683 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1684 /* must be bound */
1685 if (saddr.sin_addr.s_addr==0) {
1686 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1687 fd);
1688 return NULL;
1690 /* clone dgram socket */
1691 newfd = net_socket_mcast_create(&saddr);
1692 if (newfd < 0) {
1693 /* error already reported by net_socket_mcast_create() */
1694 close(fd);
1695 return NULL;
1697 /* clone newfd to fd, close newfd */
1698 dup2(newfd, fd);
1699 close(newfd);
1701 } else {
1702 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1703 fd, strerror(errno));
1704 return NULL;
1708 s = qemu_mallocz(sizeof(NetSocketState));
1709 s->fd = fd;
1711 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1712 NULL, net_socket_cleanup, s);
1713 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1715 /* mcast: save bound address as dst */
1716 if (is_connected) s->dgram_dst=saddr;
1718 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1719 "socket: fd=%d (%s mcast=%s:%d)",
1720 fd, is_connected? "cloned" : "",
1721 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1722 return s;
1725 static void net_socket_connect(void *opaque)
1727 NetSocketState *s = opaque;
1728 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1731 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1732 const char *model,
1733 const char *name,
1734 int fd, int is_connected)
1736 NetSocketState *s;
1737 s = qemu_mallocz(sizeof(NetSocketState));
1738 s->fd = fd;
1739 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1740 NULL, net_socket_cleanup, s);
1741 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1742 "socket: fd=%d", fd);
1743 if (is_connected) {
1744 net_socket_connect(s);
1745 } else {
1746 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1748 return s;
1751 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1752 const char *model, const char *name,
1753 int fd, int is_connected)
1755 int so_type=-1, optlen=sizeof(so_type);
1757 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1758 (socklen_t *)&optlen)< 0) {
1759 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1760 return NULL;
1762 switch(so_type) {
1763 case SOCK_DGRAM:
1764 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1765 case SOCK_STREAM:
1766 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1767 default:
1768 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1769 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1770 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1772 return NULL;
1775 static void net_socket_accept(void *opaque)
1777 NetSocketListenState *s = opaque;
1778 NetSocketState *s1;
1779 struct sockaddr_in saddr;
1780 socklen_t len;
1781 int fd;
1783 for(;;) {
1784 len = sizeof(saddr);
1785 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1786 if (fd < 0 && errno != EINTR) {
1787 return;
1788 } else if (fd >= 0) {
1789 break;
1792 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1793 if (!s1) {
1794 closesocket(fd);
1795 } else {
1796 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1797 "socket: connection from %s:%d",
1798 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1802 static int net_socket_listen_init(VLANState *vlan,
1803 const char *model,
1804 const char *name,
1805 const char *host_str)
1807 NetSocketListenState *s;
1808 int fd, val, ret;
1809 struct sockaddr_in saddr;
1811 if (parse_host_port(&saddr, host_str) < 0)
1812 return -1;
1814 s = qemu_mallocz(sizeof(NetSocketListenState));
1816 fd = socket(PF_INET, SOCK_STREAM, 0);
1817 if (fd < 0) {
1818 perror("socket");
1819 return -1;
1821 socket_set_nonblock(fd);
1823 /* allow fast reuse */
1824 val = 1;
1825 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1827 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1828 if (ret < 0) {
1829 perror("bind");
1830 return -1;
1832 ret = listen(fd, 0);
1833 if (ret < 0) {
1834 perror("listen");
1835 return -1;
1837 s->vlan = vlan;
1838 s->model = strdup(model);
1839 s->name = name ? strdup(name) : NULL;
1840 s->fd = fd;
1841 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1842 return 0;
1845 static int net_socket_connect_init(VLANState *vlan,
1846 const char *model,
1847 const char *name,
1848 const char *host_str)
1850 NetSocketState *s;
1851 int fd, connected, ret, err;
1852 struct sockaddr_in saddr;
1854 if (parse_host_port(&saddr, host_str) < 0)
1855 return -1;
1857 fd = socket(PF_INET, SOCK_STREAM, 0);
1858 if (fd < 0) {
1859 perror("socket");
1860 return -1;
1862 socket_set_nonblock(fd);
1864 connected = 0;
1865 for(;;) {
1866 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1867 if (ret < 0) {
1868 err = socket_error();
1869 if (err == EINTR || err == EWOULDBLOCK) {
1870 } else if (err == EINPROGRESS) {
1871 break;
1872 #ifdef _WIN32
1873 } else if (err == WSAEALREADY) {
1874 break;
1875 #endif
1876 } else {
1877 perror("connect");
1878 closesocket(fd);
1879 return -1;
1881 } else {
1882 connected = 1;
1883 break;
1886 s = net_socket_fd_init(vlan, model, name, fd, connected);
1887 if (!s)
1888 return -1;
1889 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1890 "socket: connect to %s:%d",
1891 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1892 return 0;
1895 static int net_socket_mcast_init(VLANState *vlan,
1896 const char *model,
1897 const char *name,
1898 const char *host_str)
1900 NetSocketState *s;
1901 int fd;
1902 struct sockaddr_in saddr;
1904 if (parse_host_port(&saddr, host_str) < 0)
1905 return -1;
1908 fd = net_socket_mcast_create(&saddr);
1909 if (fd < 0)
1910 return -1;
1912 s = net_socket_fd_init(vlan, model, name, fd, 0);
1913 if (!s)
1914 return -1;
1916 s->dgram_dst = saddr;
1918 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1919 "socket: mcast=%s:%d",
1920 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1921 return 0;
1925 typedef struct DumpState {
1926 VLANClientState *pcap_vc;
1927 int fd;
1928 int pcap_caplen;
1929 } DumpState;
1931 #define PCAP_MAGIC 0xa1b2c3d4
1933 struct pcap_file_hdr {
1934 uint32_t magic;
1935 uint16_t version_major;
1936 uint16_t version_minor;
1937 int32_t thiszone;
1938 uint32_t sigfigs;
1939 uint32_t snaplen;
1940 uint32_t linktype;
1943 struct pcap_sf_pkthdr {
1944 struct {
1945 int32_t tv_sec;
1946 int32_t tv_usec;
1947 } ts;
1948 uint32_t caplen;
1949 uint32_t len;
1952 static void dump_receive(void *opaque, const uint8_t *buf, int size)
1954 DumpState *s = opaque;
1955 struct pcap_sf_pkthdr hdr;
1956 int64_t ts;
1957 int caplen;
1959 /* Early return in case of previous error. */
1960 if (s->fd < 0) {
1961 return;
1964 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
1965 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
1967 hdr.ts.tv_sec = ts / 1000000;
1968 hdr.ts.tv_usec = ts % 1000000;
1969 hdr.caplen = caplen;
1970 hdr.len = size;
1971 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
1972 write(s->fd, buf, caplen) != caplen) {
1973 qemu_log("-net dump write error - stop dump\n");
1974 close(s->fd);
1975 s->fd = -1;
1979 static void net_dump_cleanup(VLANClientState *vc)
1981 DumpState *s = vc->opaque;
1983 close(s->fd);
1984 qemu_free(s);
1987 static int net_dump_init(VLANState *vlan, const char *device,
1988 const char *name, const char *filename, int len)
1990 struct pcap_file_hdr hdr;
1991 DumpState *s;
1993 s = qemu_malloc(sizeof(DumpState));
1995 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
1996 if (s->fd < 0) {
1997 fprintf(stderr, "-net dump: can't open %s\n", filename);
1998 return -1;
2001 s->pcap_caplen = len;
2003 hdr.magic = PCAP_MAGIC;
2004 hdr.version_major = 2;
2005 hdr.version_minor = 4;
2006 hdr.thiszone = 0;
2007 hdr.sigfigs = 0;
2008 hdr.snaplen = s->pcap_caplen;
2009 hdr.linktype = 1;
2011 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
2012 perror("-net dump write error");
2013 close(s->fd);
2014 qemu_free(s);
2015 return -1;
2018 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, dump_receive, NULL,
2019 net_dump_cleanup, s);
2020 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2021 "dump to %s (len=%d)", filename, len);
2022 return 0;
2025 /* find or alloc a new VLAN */
2026 VLANState *qemu_find_vlan(int id)
2028 VLANState **pvlan, *vlan;
2029 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2030 if (vlan->id == id)
2031 return vlan;
2033 vlan = qemu_mallocz(sizeof(VLANState));
2034 vlan->id = id;
2035 vlan->next = NULL;
2036 pvlan = &first_vlan;
2037 while (*pvlan != NULL)
2038 pvlan = &(*pvlan)->next;
2039 *pvlan = vlan;
2040 return vlan;
2043 static int nic_get_free_idx(void)
2045 int index;
2047 for (index = 0; index < MAX_NICS; index++)
2048 if (!nd_table[index].used)
2049 return index;
2050 return -1;
2053 void qemu_check_nic_model(NICInfo *nd, const char *model)
2055 const char *models[2];
2057 models[0] = model;
2058 models[1] = NULL;
2060 qemu_check_nic_model_list(nd, models, model);
2063 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2064 const char *default_model)
2066 int i, exit_status = 0;
2068 if (!nd->model)
2069 nd->model = strdup(default_model);
2071 if (strcmp(nd->model, "?") != 0) {
2072 for (i = 0 ; models[i]; i++)
2073 if (strcmp(nd->model, models[i]) == 0)
2074 return;
2076 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2077 exit_status = 1;
2080 fprintf(stderr, "qemu: Supported NIC models: ");
2081 for (i = 0 ; models[i]; i++)
2082 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2084 exit(exit_status);
2087 int net_client_init(const char *device, const char *p)
2089 static const char * const fd_params[] = {
2090 "vlan", "name", "fd", NULL
2092 char buf[1024];
2093 int vlan_id, ret;
2094 VLANState *vlan;
2095 char *name = NULL;
2097 vlan_id = 0;
2098 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2099 vlan_id = strtol(buf, NULL, 0);
2101 vlan = qemu_find_vlan(vlan_id);
2103 if (get_param_value(buf, sizeof(buf), "name", p)) {
2104 name = strdup(buf);
2106 if (!strcmp(device, "nic")) {
2107 static const char * const nic_params[] = {
2108 "vlan", "name", "macaddr", "model", NULL
2110 NICInfo *nd;
2111 uint8_t *macaddr;
2112 int idx = nic_get_free_idx();
2114 if (check_params(nic_params, p) < 0) {
2115 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2116 return -1;
2118 if (idx == -1 || nb_nics >= MAX_NICS) {
2119 fprintf(stderr, "Too Many NICs\n");
2120 ret = -1;
2121 goto out;
2123 nd = &nd_table[idx];
2124 macaddr = nd->macaddr;
2125 macaddr[0] = 0x52;
2126 macaddr[1] = 0x54;
2127 macaddr[2] = 0x00;
2128 macaddr[3] = 0x12;
2129 macaddr[4] = 0x34;
2130 macaddr[5] = 0x56 + idx;
2132 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2133 if (parse_macaddr(macaddr, buf) < 0) {
2134 fprintf(stderr, "invalid syntax for ethernet address\n");
2135 ret = -1;
2136 goto out;
2139 if (get_param_value(buf, sizeof(buf), "model", p)) {
2140 nd->model = strdup(buf);
2142 nd->vlan = vlan;
2143 nd->name = name;
2144 nd->used = 1;
2145 name = NULL;
2146 nb_nics++;
2147 vlan->nb_guest_devs++;
2148 ret = idx;
2149 } else
2150 if (!strcmp(device, "none")) {
2151 if (*p != '\0') {
2152 fprintf(stderr, "qemu: 'none' takes no parameters\n");
2153 return -1;
2155 /* does nothing. It is needed to signal that no network cards
2156 are wanted */
2157 ret = 0;
2158 } else
2159 #ifdef CONFIG_SLIRP
2160 if (!strcmp(device, "user")) {
2161 static const char * const slirp_params[] = {
2162 "vlan", "name", "hostname", "restrict", "ip", NULL
2164 if (check_params(slirp_params, p) < 0) {
2165 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2166 return -1;
2168 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2169 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2171 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2172 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
2174 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2175 slirp_ip = strdup(buf);
2177 vlan->nb_host_devs++;
2178 ret = net_slirp_init(vlan, device, name);
2179 } else if (!strcmp(device, "channel")) {
2180 long port;
2181 char name[20], *devname;
2182 struct VMChannel *vmc;
2184 port = strtol(p, &devname, 10);
2185 devname++;
2186 if (port < 1 || port > 65535) {
2187 fprintf(stderr, "vmchannel wrong port number\n");
2188 ret = -1;
2189 goto out;
2191 vmc = malloc(sizeof(struct VMChannel));
2192 snprintf(name, 20, "vmchannel%ld", port);
2193 vmc->hd = qemu_chr_open(name, devname, NULL);
2194 if (!vmc->hd) {
2195 fprintf(stderr, "qemu: could not open vmchannel device"
2196 "'%s'\n", devname);
2197 ret = -1;
2198 goto out;
2200 vmc->port = port;
2201 slirp_add_exec(3, vmc->hd, 4, port);
2202 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
2203 NULL, vmc);
2204 ret = 0;
2205 } else
2206 #endif
2207 #ifdef _WIN32
2208 if (!strcmp(device, "tap")) {
2209 static const char * const tap_params[] = {
2210 "vlan", "name", "ifname", NULL
2212 char ifname[64];
2214 if (check_params(tap_params, p) < 0) {
2215 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2216 return -1;
2218 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2219 fprintf(stderr, "tap: no interface name\n");
2220 ret = -1;
2221 goto out;
2223 vlan->nb_host_devs++;
2224 ret = tap_win32_init(vlan, device, name, ifname);
2225 } else
2226 #elif defined (_AIX)
2227 #else
2228 if (!strcmp(device, "tap")) {
2229 char ifname[64];
2230 char setup_script[1024], down_script[1024];
2231 int fd;
2232 vlan->nb_host_devs++;
2233 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2234 if (check_params(fd_params, p) < 0) {
2235 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2236 return -1;
2238 fd = strtol(buf, NULL, 0);
2239 fcntl(fd, F_SETFL, O_NONBLOCK);
2240 net_tap_fd_init(vlan, device, name, fd, tap_probe_vnet_hdr(fd));
2241 ret = 0;
2242 } else {
2243 static const char * const tap_params[] = {
2244 "vlan", "name", "ifname", "script", "downscript", NULL
2246 if (check_params(tap_params, p) < 0) {
2247 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2248 return -1;
2250 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2251 ifname[0] = '\0';
2253 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2254 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2256 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2257 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2259 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2261 } else
2262 #endif
2263 if (!strcmp(device, "socket")) {
2264 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2265 int fd;
2266 if (check_params(fd_params, p) < 0) {
2267 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2268 return -1;
2270 fd = strtol(buf, NULL, 0);
2271 ret = -1;
2272 if (net_socket_fd_init(vlan, device, name, fd, 1))
2273 ret = 0;
2274 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2275 static const char * const listen_params[] = {
2276 "vlan", "name", "listen", NULL
2278 if (check_params(listen_params, p) < 0) {
2279 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2280 return -1;
2282 ret = net_socket_listen_init(vlan, device, name, buf);
2283 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2284 static const char * const connect_params[] = {
2285 "vlan", "name", "connect", NULL
2287 if (check_params(connect_params, p) < 0) {
2288 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2289 return -1;
2291 ret = net_socket_connect_init(vlan, device, name, buf);
2292 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2293 static const char * const mcast_params[] = {
2294 "vlan", "name", "mcast", NULL
2296 if (check_params(mcast_params, p) < 0) {
2297 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2298 return -1;
2300 ret = net_socket_mcast_init(vlan, device, name, buf);
2301 } else {
2302 fprintf(stderr, "Unknown socket options: %s\n", p);
2303 ret = -1;
2304 goto out;
2306 vlan->nb_host_devs++;
2307 } else
2308 #ifdef CONFIG_VDE
2309 if (!strcmp(device, "vde")) {
2310 static const char * const vde_params[] = {
2311 "vlan", "name", "sock", "port", "group", "mode", NULL
2313 char vde_sock[1024], vde_group[512];
2314 int vde_port, vde_mode;
2316 if (check_params(vde_params, p) < 0) {
2317 fprintf(stderr, "qemu: invalid parameter in '%s'\n", p);
2318 return -1;
2320 vlan->nb_host_devs++;
2321 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2322 vde_sock[0] = '\0';
2324 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2325 vde_port = strtol(buf, NULL, 10);
2326 } else {
2327 vde_port = 0;
2329 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2330 vde_group[0] = '\0';
2332 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2333 vde_mode = strtol(buf, NULL, 8);
2334 } else {
2335 vde_mode = 0700;
2337 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2338 } else
2339 #endif
2340 if (!strcmp(device, "dump")) {
2341 int len = 65536;
2343 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2344 len = strtol(buf, NULL, 0);
2346 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2347 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2349 ret = net_dump_init(vlan, device, name, buf, len);
2350 } else {
2351 fprintf(stderr, "Unknown network device: %s\n", device);
2352 ret = -1;
2353 goto out;
2355 if (ret < 0) {
2356 fprintf(stderr, "Could not initialize device '%s'\n", device);
2358 out:
2359 if (name)
2360 free(name);
2361 return ret;
2364 void net_client_uninit(NICInfo *nd)
2366 nd->vlan->nb_guest_devs--;
2367 nb_nics--;
2368 nd->used = 0;
2369 free((void *)nd->model);
2372 static int net_host_check_device(const char *device)
2374 int i;
2375 const char *valid_param_list[] = { "tap", "socket", "dump"
2376 #ifdef CONFIG_SLIRP
2377 ,"user"
2378 #endif
2379 #ifdef CONFIG_VDE
2380 ,"vde"
2381 #endif
2383 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2384 if (!strncmp(valid_param_list[i], device,
2385 strlen(valid_param_list[i])))
2386 return 1;
2389 return 0;
2392 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2394 if (!net_host_check_device(device)) {
2395 monitor_printf(mon, "invalid host network device %s\n", device);
2396 return;
2398 if (net_client_init(device, opts ? opts : "") < 0) {
2399 monitor_printf(mon, "adding host network device %s failed\n", device);
2403 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2405 VLANState *vlan;
2406 VLANClientState *vc;
2408 vlan = qemu_find_vlan(vlan_id);
2410 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2411 if (!strcmp(vc->name, device)) {
2412 break;
2416 if (!vc) {
2417 monitor_printf(mon, "can't find device %s\n", device);
2418 return;
2420 if (!net_host_check_device(vc->model)) {
2421 monitor_printf(mon, "invalid host network device %s\n", device);
2422 return;
2424 qemu_del_vlan_client(vc);
2427 int net_client_parse(const char *str)
2429 const char *p;
2430 char *q;
2431 char device[64];
2433 p = str;
2434 q = device;
2435 while (*p != '\0' && *p != ',') {
2436 if ((q - device) < sizeof(device) - 1)
2437 *q++ = *p;
2438 p++;
2440 *q = '\0';
2441 if (*p == ',')
2442 p++;
2444 return net_client_init(device, p);
2447 void do_info_network(Monitor *mon)
2449 VLANState *vlan;
2450 VLANClientState *vc;
2452 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2453 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2454 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2455 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2459 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2461 VLANState *vlan;
2462 VLANClientState *vc = NULL;
2464 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2465 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2466 if (strcmp(vc->name, name) == 0)
2467 goto done;
2468 done:
2470 if (!vc) {
2471 monitor_printf(mon, "could not find network device '%s'", name);
2472 return 0;
2475 if (strcmp(up_or_down, "up") == 0)
2476 vc->link_down = 0;
2477 else if (strcmp(up_or_down, "down") == 0)
2478 vc->link_down = 1;
2479 else
2480 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2481 "valid\n", up_or_down);
2483 if (vc->link_status_changed)
2484 vc->link_status_changed(vc);
2486 return 1;
2489 void net_cleanup(void)
2491 VLANState *vlan;
2493 /* close network clients */
2494 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2495 VLANClientState *vc = vlan->first_client;
2497 while (vc) {
2498 VLANClientState *next = vc->next;
2500 qemu_del_vlan_client(vc);
2502 vc = next;
2507 void net_client_check(void)
2509 VLANState *vlan;
2511 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2512 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2513 continue;
2514 if (vlan->nb_guest_devs == 0)
2515 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2516 if (vlan->nb_host_devs == 0)
2517 fprintf(stderr,
2518 "Warning: vlan %d is not connected to host network\n",
2519 vlan->id);