Add HPET emulation to qemu (Beth Kon)
[qemu/mini2440/sniper_sniper_test.git] / net.c
blobc7960350066b302913bfca251aed5626c9b4332f
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 "qemu-common.h"
25 #include "net.h"
26 #include "console.h"
27 #include "sysemu.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <sys/time.h>
38 #include <zlib.h>
40 #ifndef _WIN32
41 #include <sys/times.h>
42 #include <sys/wait.h>
43 #include <termios.h>
44 #include <sys/mman.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/if.h>
50 #ifdef __NetBSD__
51 #include <net/if_tap.h>
52 #endif
53 #ifdef __linux__
54 #include <linux/if_tun.h>
55 #endif
56 #include <arpa/inet.h>
57 #include <dirent.h>
58 #include <netdb.h>
59 #include <sys/select.h>
60 #ifdef _BSD
61 #include <sys/stat.h>
62 #ifdef __FreeBSD__
63 #include <libutil.h>
64 #else
65 #include <util.h>
66 #endif
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
69 #else
70 #ifdef __linux__
71 #include <pty.h>
72 #include <malloc.h>
73 #include <linux/rtc.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
78 #include "hpet.h"
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
82 #endif
83 #ifdef __sun__
84 #include <sys/stat.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
94 #include <net/if.h>
95 #include <syslog.h>
96 #include <stropts.h>
97 #endif
98 #endif
99 #endif
101 #include "qemu_socket.h"
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
105 #endif
107 #if defined(__OpenBSD__)
108 #include <util.h>
109 #endif
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
113 #endif
115 #ifdef _WIN32
116 #include <malloc.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
121 #endif
123 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
124 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
125 #ifdef __sun__
126 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
127 #else
128 #define SMBD_COMMAND "/usr/sbin/smbd"
129 #endif
131 static VLANState *first_vlan;
133 /***********************************************************/
134 /* network device redirectors */
136 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
137 static void hex_dump(FILE *f, const uint8_t *buf, int size)
139 int len, i, j, c;
141 for(i=0;i<size;i+=16) {
142 len = size - i;
143 if (len > 16)
144 len = 16;
145 fprintf(f, "%08x ", i);
146 for(j=0;j<16;j++) {
147 if (j < len)
148 fprintf(f, " %02x", buf[i+j]);
149 else
150 fprintf(f, " ");
152 fprintf(f, " ");
153 for(j=0;j<len;j++) {
154 c = buf[i+j];
155 if (c < ' ' || c > '~')
156 c = '.';
157 fprintf(f, "%c", c);
159 fprintf(f, "\n");
162 #endif
164 static int parse_macaddr(uint8_t *macaddr, const char *p)
166 int i;
167 char *last_char;
168 long int offset;
170 errno = 0;
171 offset = strtol(p, &last_char, 0);
172 if (0 == errno && '\0' == *last_char &&
173 offset >= 0 && offset <= 0xFFFFFF) {
174 macaddr[3] = (offset & 0xFF0000) >> 16;
175 macaddr[4] = (offset & 0xFF00) >> 8;
176 macaddr[5] = offset & 0xFF;
177 return 0;
178 } else {
179 for(i = 0; i < 6; i++) {
180 macaddr[i] = strtol(p, (char **)&p, 16);
181 if (i == 5) {
182 if (*p != '\0')
183 return -1;
184 } else {
185 if (*p != ':' && *p != '-')
186 return -1;
187 p++;
190 return 0;
193 return -1;
196 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
198 const char *p, *p1;
199 int len;
200 p = *pp;
201 p1 = strchr(p, sep);
202 if (!p1)
203 return -1;
204 len = p1 - p;
205 p1++;
206 if (buf_size > 0) {
207 if (len > buf_size - 1)
208 len = buf_size - 1;
209 memcpy(buf, p, len);
210 buf[len] = '\0';
212 *pp = p1;
213 return 0;
216 int parse_host_src_port(struct sockaddr_in *haddr,
217 struct sockaddr_in *saddr,
218 const char *input_str)
220 char *str = strdup(input_str);
221 char *host_str = str;
222 char *src_str;
223 const char *src_str2;
224 char *ptr;
227 * Chop off any extra arguments at the end of the string which
228 * would start with a comma, then fill in the src port information
229 * if it was provided else use the "any address" and "any port".
231 if ((ptr = strchr(str,',')))
232 *ptr = '\0';
234 if ((src_str = strchr(input_str,'@'))) {
235 *src_str = '\0';
236 src_str++;
239 if (parse_host_port(haddr, host_str) < 0)
240 goto fail;
242 src_str2 = src_str;
243 if (!src_str || *src_str == '\0')
244 src_str2 = ":0";
246 if (parse_host_port(saddr, src_str2) < 0)
247 goto fail;
249 free(str);
250 return(0);
252 fail:
253 free(str);
254 return -1;
257 int parse_host_port(struct sockaddr_in *saddr, const char *str)
259 char buf[512];
260 struct hostent *he;
261 const char *p, *r;
262 int port;
264 p = str;
265 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
266 return -1;
267 saddr->sin_family = AF_INET;
268 if (buf[0] == '\0') {
269 saddr->sin_addr.s_addr = 0;
270 } else {
271 if (qemu_isdigit(buf[0])) {
272 if (!inet_aton(buf, &saddr->sin_addr))
273 return -1;
274 } else {
275 if ((he = gethostbyname(buf)) == NULL)
276 return - 1;
277 saddr->sin_addr = *(struct in_addr *)he->h_addr;
280 port = strtol(p, (char **)&r, 0);
281 if (r == p)
282 return -1;
283 saddr->sin_port = htons(port);
284 return 0;
287 #if !defined(_WIN32) && 0
288 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
290 const char *p;
291 int len;
293 len = MIN(108, strlen(str));
294 p = strchr(str, ',');
295 if (p)
296 len = MIN(len, p - str);
298 memset(uaddr, 0, sizeof(*uaddr));
300 uaddr->sun_family = AF_UNIX;
301 memcpy(uaddr->sun_path, str, len);
303 return 0;
305 #endif
307 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
308 IOReadHandler *fd_read,
309 IOCanRWHandler *fd_can_read,
310 void *opaque)
312 VLANClientState *vc, **pvc;
313 vc = qemu_mallocz(sizeof(VLANClientState));
314 if (!vc)
315 return NULL;
316 vc->fd_read = fd_read;
317 vc->fd_can_read = fd_can_read;
318 vc->opaque = opaque;
319 vc->vlan = vlan;
321 vc->next = NULL;
322 pvc = &vlan->first_client;
323 while (*pvc != NULL)
324 pvc = &(*pvc)->next;
325 *pvc = vc;
326 return vc;
329 void qemu_del_vlan_client(VLANClientState *vc)
331 VLANClientState **pvc = &vc->vlan->first_client;
333 while (*pvc != NULL)
334 if (*pvc == vc) {
335 *pvc = vc->next;
336 free(vc);
337 break;
338 } else
339 pvc = &(*pvc)->next;
342 int qemu_can_send_packet(VLANClientState *vc1)
344 VLANState *vlan = vc1->vlan;
345 VLANClientState *vc;
347 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
348 if (vc != vc1) {
349 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
350 return 1;
353 return 0;
356 void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
358 VLANState *vlan = vc1->vlan;
359 VLANClientState *vc;
361 #ifdef DEBUG_NET
362 printf("vlan %d send:\n", vlan->id);
363 hex_dump(stdout, buf, size);
364 #endif
365 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
366 if (vc != vc1) {
367 vc->fd_read(vc->opaque, buf, size);
372 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
373 int iovcnt)
375 uint8_t buffer[4096];
376 size_t offset = 0;
377 int i;
379 for (i = 0; i < iovcnt; i++) {
380 size_t len;
382 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
383 memcpy(buffer + offset, iov[i].iov_base, len);
384 offset += len;
387 vc->fd_read(vc->opaque, buffer, offset);
389 return offset;
392 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
393 int iovcnt)
395 VLANState *vlan = vc1->vlan;
396 VLANClientState *vc;
397 ssize_t max_len = 0;
399 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
400 ssize_t len = 0;
402 if (vc == vc1)
403 continue;
405 if (vc->fd_readv)
406 len = vc->fd_readv(vc->opaque, iov, iovcnt);
407 else if (vc->fd_read)
408 len = vc_sendv_compat(vc, iov, iovcnt);
410 max_len = MAX(max_len, len);
413 return max_len;
416 #if defined(CONFIG_SLIRP)
418 /* slirp network adapter */
420 static int slirp_inited;
421 static VLANClientState *slirp_vc;
423 int slirp_can_output(void)
425 return !slirp_vc || qemu_can_send_packet(slirp_vc);
428 void slirp_output(const uint8_t *pkt, int pkt_len)
430 #ifdef DEBUG_SLIRP
431 printf("slirp output:\n");
432 hex_dump(stdout, pkt, pkt_len);
433 #endif
434 if (!slirp_vc)
435 return;
436 qemu_send_packet(slirp_vc, pkt, pkt_len);
439 int slirp_is_inited(void)
441 return slirp_inited;
444 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
446 #ifdef DEBUG_SLIRP
447 printf("slirp input:\n");
448 hex_dump(stdout, buf, size);
449 #endif
450 slirp_input(buf, size);
453 static int net_slirp_init(VLANState *vlan)
455 if (!slirp_inited) {
456 slirp_inited = 1;
457 slirp_init();
459 slirp_vc = qemu_new_vlan_client(vlan,
460 slirp_receive, NULL, NULL);
461 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
462 return 0;
465 void net_slirp_redir(const char *redir_str)
467 int is_udp;
468 char buf[256], *r;
469 const char *p;
470 struct in_addr guest_addr;
471 int host_port, guest_port;
473 if (!slirp_inited) {
474 slirp_inited = 1;
475 slirp_init();
478 p = redir_str;
479 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
480 goto fail;
481 if (!strcmp(buf, "tcp")) {
482 is_udp = 0;
483 } else if (!strcmp(buf, "udp")) {
484 is_udp = 1;
485 } else {
486 goto fail;
489 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
490 goto fail;
491 host_port = strtol(buf, &r, 0);
492 if (r == buf)
493 goto fail;
495 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
496 goto fail;
497 if (buf[0] == '\0') {
498 pstrcpy(buf, sizeof(buf), "10.0.2.15");
500 if (!inet_aton(buf, &guest_addr))
501 goto fail;
503 guest_port = strtol(p, &r, 0);
504 if (r == p)
505 goto fail;
507 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
508 fprintf(stderr, "qemu: could not set up redirection\n");
509 exit(1);
511 return;
512 fail:
513 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
514 exit(1);
517 #ifndef _WIN32
519 static char smb_dir[1024];
521 static void erase_dir(char *dir_name)
523 DIR *d;
524 struct dirent *de;
525 char filename[1024];
527 /* erase all the files in the directory */
528 if ((d = opendir(dir_name)) != 0) {
529 for(;;) {
530 de = readdir(d);
531 if (!de)
532 break;
533 if (strcmp(de->d_name, ".") != 0 &&
534 strcmp(de->d_name, "..") != 0) {
535 snprintf(filename, sizeof(filename), "%s/%s",
536 smb_dir, de->d_name);
537 if (unlink(filename) != 0) /* is it a directory? */
538 erase_dir(filename);
541 closedir(d);
542 rmdir(dir_name);
546 /* automatic user mode samba server configuration */
547 static void smb_exit(void)
549 erase_dir(smb_dir);
552 /* automatic user mode samba server configuration */
553 void net_slirp_smb(const char *exported_dir)
555 char smb_conf[1024];
556 char smb_cmdline[1024];
557 FILE *f;
559 if (!slirp_inited) {
560 slirp_inited = 1;
561 slirp_init();
564 /* XXX: better tmp dir construction */
565 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
566 if (mkdir(smb_dir, 0700) < 0) {
567 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
568 exit(1);
570 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
572 f = fopen(smb_conf, "w");
573 if (!f) {
574 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
575 exit(1);
577 fprintf(f,
578 "[global]\n"
579 "private dir=%s\n"
580 "smb ports=0\n"
581 "socket address=127.0.0.1\n"
582 "pid directory=%s\n"
583 "lock directory=%s\n"
584 "log file=%s/log.smbd\n"
585 "smb passwd file=%s/smbpasswd\n"
586 "security = share\n"
587 "[qemu]\n"
588 "path=%s\n"
589 "read only=no\n"
590 "guest ok=yes\n",
591 smb_dir,
592 smb_dir,
593 smb_dir,
594 smb_dir,
595 smb_dir,
596 exported_dir
598 fclose(f);
599 atexit(smb_exit);
601 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
602 SMBD_COMMAND, smb_conf);
604 slirp_add_exec(0, smb_cmdline, 4, 139);
607 #endif /* !defined(_WIN32) */
608 void do_info_slirp(void)
610 slirp_stats();
613 #endif /* CONFIG_SLIRP */
615 #if !defined(_WIN32)
617 typedef struct TAPState {
618 VLANClientState *vc;
619 int fd;
620 char down_script[1024];
621 } TAPState;
623 #ifdef HAVE_IOVEC
624 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
625 int iovcnt)
627 TAPState *s = opaque;
628 ssize_t len;
630 do {
631 len = writev(s->fd, iov, iovcnt);
632 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
634 return len;
636 #endif
638 static void tap_receive(void *opaque, const uint8_t *buf, int size)
640 TAPState *s = opaque;
641 int ret;
642 for(;;) {
643 ret = write(s->fd, buf, size);
644 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
645 } else {
646 break;
651 static void tap_send(void *opaque)
653 TAPState *s = opaque;
654 uint8_t buf[4096];
655 int size;
657 #ifdef __sun__
658 struct strbuf sbuf;
659 int f = 0;
660 sbuf.maxlen = sizeof(buf);
661 sbuf.buf = buf;
662 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
663 #else
664 size = read(s->fd, buf, sizeof(buf));
665 #endif
666 if (size > 0) {
667 qemu_send_packet(s->vc, buf, size);
671 /* fd support */
673 static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
675 TAPState *s;
677 s = qemu_mallocz(sizeof(TAPState));
678 if (!s)
679 return NULL;
680 s->fd = fd;
681 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
682 #ifdef HAVE_IOVEC
683 s->vc->fd_readv = tap_receive_iov;
684 #endif
685 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
686 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
687 return s;
690 #if defined (_BSD) || defined (__FreeBSD_kernel__)
691 static int tap_open(char *ifname, int ifname_size)
693 int fd;
694 char *dev;
695 struct stat s;
697 TFR(fd = open("/dev/tap", O_RDWR));
698 if (fd < 0) {
699 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
700 return -1;
703 fstat(fd, &s);
704 dev = devname(s.st_rdev, S_IFCHR);
705 pstrcpy(ifname, ifname_size, dev);
707 fcntl(fd, F_SETFL, O_NONBLOCK);
708 return fd;
710 #elif defined(__sun__)
711 #define TUNNEWPPA (('T'<<16) | 0x0001)
713 * Allocate TAP device, returns opened fd.
714 * Stores dev name in the first arg(must be large enough).
716 int tap_alloc(char *dev, size_t dev_size)
718 int tap_fd, if_fd, ppa = -1;
719 static int ip_fd = 0;
720 char *ptr;
722 static int arp_fd = 0;
723 int ip_muxid, arp_muxid;
724 struct strioctl strioc_if, strioc_ppa;
725 int link_type = I_PLINK;;
726 struct lifreq ifr;
727 char actual_name[32] = "";
729 memset(&ifr, 0x0, sizeof(ifr));
731 if( *dev ){
732 ptr = dev;
733 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
734 ppa = atoi(ptr);
737 /* Check if IP device was opened */
738 if( ip_fd )
739 close(ip_fd);
741 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
742 if (ip_fd < 0) {
743 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
744 return -1;
747 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
748 if (tap_fd < 0) {
749 syslog(LOG_ERR, "Can't open /dev/tap");
750 return -1;
753 /* Assign a new PPA and get its unit number. */
754 strioc_ppa.ic_cmd = TUNNEWPPA;
755 strioc_ppa.ic_timout = 0;
756 strioc_ppa.ic_len = sizeof(ppa);
757 strioc_ppa.ic_dp = (char *)&ppa;
758 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
759 syslog (LOG_ERR, "Can't assign new interface");
761 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
762 if (if_fd < 0) {
763 syslog(LOG_ERR, "Can't open /dev/tap (2)");
764 return -1;
766 if(ioctl(if_fd, I_PUSH, "ip") < 0){
767 syslog(LOG_ERR, "Can't push IP module");
768 return -1;
771 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
772 syslog(LOG_ERR, "Can't get flags\n");
774 snprintf (actual_name, 32, "tap%d", ppa);
775 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
777 ifr.lifr_ppa = ppa;
778 /* Assign ppa according to the unit number returned by tun device */
780 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
781 syslog (LOG_ERR, "Can't set PPA %d", ppa);
782 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
783 syslog (LOG_ERR, "Can't get flags\n");
784 /* Push arp module to if_fd */
785 if (ioctl (if_fd, I_PUSH, "arp") < 0)
786 syslog (LOG_ERR, "Can't push ARP module (2)");
788 /* Push arp module to ip_fd */
789 if (ioctl (ip_fd, I_POP, NULL) < 0)
790 syslog (LOG_ERR, "I_POP failed\n");
791 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
792 syslog (LOG_ERR, "Can't push ARP module (3)\n");
793 /* Open arp_fd */
794 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
795 if (arp_fd < 0)
796 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
798 /* Set ifname to arp */
799 strioc_if.ic_cmd = SIOCSLIFNAME;
800 strioc_if.ic_timout = 0;
801 strioc_if.ic_len = sizeof(ifr);
802 strioc_if.ic_dp = (char *)&ifr;
803 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
804 syslog (LOG_ERR, "Can't set ifname to arp\n");
807 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
808 syslog(LOG_ERR, "Can't link TAP device to IP");
809 return -1;
812 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
813 syslog (LOG_ERR, "Can't link TAP device to ARP");
815 close (if_fd);
817 memset(&ifr, 0x0, sizeof(ifr));
818 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
819 ifr.lifr_ip_muxid = ip_muxid;
820 ifr.lifr_arp_muxid = arp_muxid;
822 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
824 ioctl (ip_fd, I_PUNLINK , arp_muxid);
825 ioctl (ip_fd, I_PUNLINK, ip_muxid);
826 syslog (LOG_ERR, "Can't set multiplexor id");
829 snprintf(dev, dev_size, "tap%d", ppa);
830 return tap_fd;
833 static int tap_open(char *ifname, int ifname_size)
835 char dev[10]="";
836 int fd;
837 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
838 fprintf(stderr, "Cannot allocate TAP device\n");
839 return -1;
841 pstrcpy(ifname, ifname_size, dev);
842 fcntl(fd, F_SETFL, O_NONBLOCK);
843 return fd;
845 #elif defined (_AIX)
846 static int tap_open(char *ifname, int ifname_size)
848 fprintf (stderr, "no tap on AIX\n");
849 return -1;
851 #else
852 static int tap_open(char *ifname, int ifname_size)
854 struct ifreq ifr;
855 int fd, ret;
857 TFR(fd = open("/dev/net/tun", O_RDWR));
858 if (fd < 0) {
859 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
860 return -1;
862 memset(&ifr, 0, sizeof(ifr));
863 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
864 if (ifname[0] != '\0')
865 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
866 else
867 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
868 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
869 if (ret != 0) {
870 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
871 close(fd);
872 return -1;
874 pstrcpy(ifname, ifname_size, ifr.ifr_name);
875 fcntl(fd, F_SETFL, O_NONBLOCK);
876 return fd;
878 #endif
880 static int launch_script(const char *setup_script, const char *ifname, int fd)
882 int pid, status;
883 char *args[3];
884 char **parg;
886 /* try to launch network script */
887 pid = fork();
888 if (pid >= 0) {
889 if (pid == 0) {
890 int open_max = sysconf (_SC_OPEN_MAX), i;
891 for (i = 0; i < open_max; i++)
892 if (i != STDIN_FILENO &&
893 i != STDOUT_FILENO &&
894 i != STDERR_FILENO &&
895 i != fd)
896 close(i);
898 parg = args;
899 *parg++ = (char *)setup_script;
900 *parg++ = (char *)ifname;
901 *parg++ = NULL;
902 execv(setup_script, args);
903 _exit(1);
905 while (waitpid(pid, &status, 0) != pid);
906 if (!WIFEXITED(status) ||
907 WEXITSTATUS(status) != 0) {
908 fprintf(stderr, "%s: could not launch network script\n",
909 setup_script);
910 return -1;
913 return 0;
916 static int net_tap_init(VLANState *vlan, const char *ifname1,
917 const char *setup_script, const char *down_script)
919 TAPState *s;
920 int fd;
921 char ifname[128];
923 if (ifname1 != NULL)
924 pstrcpy(ifname, sizeof(ifname), ifname1);
925 else
926 ifname[0] = '\0';
927 TFR(fd = tap_open(ifname, sizeof(ifname)));
928 if (fd < 0)
929 return -1;
931 if (!setup_script || !strcmp(setup_script, "no"))
932 setup_script = "";
933 if (setup_script[0] != '\0') {
934 if (launch_script(setup_script, ifname, fd))
935 return -1;
937 s = net_tap_fd_init(vlan, fd);
938 if (!s)
939 return -1;
940 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
941 "tap: ifname=%s setup_script=%s", ifname, setup_script);
942 if (down_script && strcmp(down_script, "no"))
943 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
944 return 0;
947 #endif /* !_WIN32 */
949 #if defined(CONFIG_VDE)
950 typedef struct VDEState {
951 VLANClientState *vc;
952 VDECONN *vde;
953 } VDEState;
955 static void vde_to_qemu(void *opaque)
957 VDEState *s = opaque;
958 uint8_t buf[4096];
959 int size;
961 size = vde_recv(s->vde, buf, sizeof(buf), 0);
962 if (size > 0) {
963 qemu_send_packet(s->vc, buf, size);
967 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
969 VDEState *s = opaque;
970 int ret;
971 for(;;) {
972 ret = vde_send(s->vde, buf, size, 0);
973 if (ret < 0 && errno == EINTR) {
974 } else {
975 break;
980 static int net_vde_init(VLANState *vlan, const char *sock, int port,
981 const char *group, int mode)
983 VDEState *s;
984 char *init_group = strlen(group) ? (char *)group : NULL;
985 char *init_sock = strlen(sock) ? (char *)sock : NULL;
987 struct vde_open_args args = {
988 .port = port,
989 .group = init_group,
990 .mode = mode,
993 s = qemu_mallocz(sizeof(VDEState));
994 if (!s)
995 return -1;
996 s->vde = vde_open(init_sock, "QEMU", &args);
997 if (!s->vde){
998 free(s);
999 return -1;
1001 s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
1002 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1003 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
1004 sock, vde_datafd(s->vde));
1005 return 0;
1007 #endif
1009 /* network connection */
1010 typedef struct NetSocketState {
1011 VLANClientState *vc;
1012 int fd;
1013 int state; /* 0 = getting length, 1 = getting data */
1014 int index;
1015 int packet_len;
1016 uint8_t buf[4096];
1017 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1018 } NetSocketState;
1020 typedef struct NetSocketListenState {
1021 VLANState *vlan;
1022 int fd;
1023 } NetSocketListenState;
1025 /* XXX: we consider we can send the whole packet without blocking */
1026 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1028 NetSocketState *s = opaque;
1029 uint32_t len;
1030 len = htonl(size);
1032 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1033 send_all(s->fd, buf, size);
1036 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1038 NetSocketState *s = opaque;
1039 sendto(s->fd, buf, size, 0,
1040 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1043 static void net_socket_send(void *opaque)
1045 NetSocketState *s = opaque;
1046 int l, size, err;
1047 uint8_t buf1[4096];
1048 const uint8_t *buf;
1050 size = recv(s->fd, buf1, sizeof(buf1), 0);
1051 if (size < 0) {
1052 err = socket_error();
1053 if (err != EWOULDBLOCK)
1054 goto eoc;
1055 } else if (size == 0) {
1056 /* end of connection */
1057 eoc:
1058 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1059 closesocket(s->fd);
1060 return;
1062 buf = buf1;
1063 while (size > 0) {
1064 /* reassemble a packet from the network */
1065 switch(s->state) {
1066 case 0:
1067 l = 4 - s->index;
1068 if (l > size)
1069 l = size;
1070 memcpy(s->buf + s->index, buf, l);
1071 buf += l;
1072 size -= l;
1073 s->index += l;
1074 if (s->index == 4) {
1075 /* got length */
1076 s->packet_len = ntohl(*(uint32_t *)s->buf);
1077 s->index = 0;
1078 s->state = 1;
1080 break;
1081 case 1:
1082 l = s->packet_len - s->index;
1083 if (l > size)
1084 l = size;
1085 memcpy(s->buf + s->index, buf, l);
1086 s->index += l;
1087 buf += l;
1088 size -= l;
1089 if (s->index >= s->packet_len) {
1090 qemu_send_packet(s->vc, s->buf, s->packet_len);
1091 s->index = 0;
1092 s->state = 0;
1094 break;
1099 static void net_socket_send_dgram(void *opaque)
1101 NetSocketState *s = opaque;
1102 int size;
1104 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1105 if (size < 0)
1106 return;
1107 if (size == 0) {
1108 /* end of connection */
1109 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1110 return;
1112 qemu_send_packet(s->vc, s->buf, size);
1115 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1117 struct ip_mreq imr;
1118 int fd;
1119 int val, ret;
1120 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1121 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1122 inet_ntoa(mcastaddr->sin_addr),
1123 (int)ntohl(mcastaddr->sin_addr.s_addr));
1124 return -1;
1127 fd = socket(PF_INET, SOCK_DGRAM, 0);
1128 if (fd < 0) {
1129 perror("socket(PF_INET, SOCK_DGRAM)");
1130 return -1;
1133 val = 1;
1134 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1135 (const char *)&val, sizeof(val));
1136 if (ret < 0) {
1137 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1138 goto fail;
1141 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1142 if (ret < 0) {
1143 perror("bind");
1144 goto fail;
1147 /* Add host to multicast group */
1148 imr.imr_multiaddr = mcastaddr->sin_addr;
1149 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1151 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1152 (const char *)&imr, sizeof(struct ip_mreq));
1153 if (ret < 0) {
1154 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1155 goto fail;
1158 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1159 val = 1;
1160 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1161 (const char *)&val, sizeof(val));
1162 if (ret < 0) {
1163 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1164 goto fail;
1167 socket_set_nonblock(fd);
1168 return fd;
1169 fail:
1170 if (fd >= 0)
1171 closesocket(fd);
1172 return -1;
1175 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
1176 int is_connected)
1178 struct sockaddr_in saddr;
1179 int newfd;
1180 socklen_t saddr_len;
1181 NetSocketState *s;
1183 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1184 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1185 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1188 if (is_connected) {
1189 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1190 /* must be bound */
1191 if (saddr.sin_addr.s_addr==0) {
1192 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1193 fd);
1194 return NULL;
1196 /* clone dgram socket */
1197 newfd = net_socket_mcast_create(&saddr);
1198 if (newfd < 0) {
1199 /* error already reported by net_socket_mcast_create() */
1200 close(fd);
1201 return NULL;
1203 /* clone newfd to fd, close newfd */
1204 dup2(newfd, fd);
1205 close(newfd);
1207 } else {
1208 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1209 fd, strerror(errno));
1210 return NULL;
1214 s = qemu_mallocz(sizeof(NetSocketState));
1215 if (!s)
1216 return NULL;
1217 s->fd = fd;
1219 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
1220 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1222 /* mcast: save bound address as dst */
1223 if (is_connected) s->dgram_dst=saddr;
1225 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1226 "socket: fd=%d (%s mcast=%s:%d)",
1227 fd, is_connected? "cloned" : "",
1228 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1229 return s;
1232 static void net_socket_connect(void *opaque)
1234 NetSocketState *s = opaque;
1235 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1238 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
1239 int is_connected)
1241 NetSocketState *s;
1242 s = qemu_mallocz(sizeof(NetSocketState));
1243 if (!s)
1244 return NULL;
1245 s->fd = fd;
1246 s->vc = qemu_new_vlan_client(vlan,
1247 net_socket_receive, NULL, s);
1248 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1249 "socket: fd=%d", fd);
1250 if (is_connected) {
1251 net_socket_connect(s);
1252 } else {
1253 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1255 return s;
1258 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
1259 int is_connected)
1261 int so_type=-1, optlen=sizeof(so_type);
1263 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1264 (socklen_t *)&optlen)< 0) {
1265 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1266 return NULL;
1268 switch(so_type) {
1269 case SOCK_DGRAM:
1270 return net_socket_fd_init_dgram(vlan, fd, is_connected);
1271 case SOCK_STREAM:
1272 return net_socket_fd_init_stream(vlan, fd, is_connected);
1273 default:
1274 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1275 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1276 return net_socket_fd_init_stream(vlan, fd, is_connected);
1278 return NULL;
1281 static void net_socket_accept(void *opaque)
1283 NetSocketListenState *s = opaque;
1284 NetSocketState *s1;
1285 struct sockaddr_in saddr;
1286 socklen_t len;
1287 int fd;
1289 for(;;) {
1290 len = sizeof(saddr);
1291 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1292 if (fd < 0 && errno != EINTR) {
1293 return;
1294 } else if (fd >= 0) {
1295 break;
1298 s1 = net_socket_fd_init(s->vlan, fd, 1);
1299 if (!s1) {
1300 closesocket(fd);
1301 } else {
1302 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1303 "socket: connection from %s:%d",
1304 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1308 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
1310 NetSocketListenState *s;
1311 int fd, val, ret;
1312 struct sockaddr_in saddr;
1314 if (parse_host_port(&saddr, host_str) < 0)
1315 return -1;
1317 s = qemu_mallocz(sizeof(NetSocketListenState));
1318 if (!s)
1319 return -1;
1321 fd = socket(PF_INET, SOCK_STREAM, 0);
1322 if (fd < 0) {
1323 perror("socket");
1324 return -1;
1326 socket_set_nonblock(fd);
1328 /* allow fast reuse */
1329 val = 1;
1330 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1332 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1333 if (ret < 0) {
1334 perror("bind");
1335 return -1;
1337 ret = listen(fd, 0);
1338 if (ret < 0) {
1339 perror("listen");
1340 return -1;
1342 s->vlan = vlan;
1343 s->fd = fd;
1344 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1345 return 0;
1348 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
1350 NetSocketState *s;
1351 int fd, connected, ret, err;
1352 struct sockaddr_in saddr;
1354 if (parse_host_port(&saddr, host_str) < 0)
1355 return -1;
1357 fd = socket(PF_INET, SOCK_STREAM, 0);
1358 if (fd < 0) {
1359 perror("socket");
1360 return -1;
1362 socket_set_nonblock(fd);
1364 connected = 0;
1365 for(;;) {
1366 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1367 if (ret < 0) {
1368 err = socket_error();
1369 if (err == EINTR || err == EWOULDBLOCK) {
1370 } else if (err == EINPROGRESS) {
1371 break;
1372 #ifdef _WIN32
1373 } else if (err == WSAEALREADY) {
1374 break;
1375 #endif
1376 } else {
1377 perror("connect");
1378 closesocket(fd);
1379 return -1;
1381 } else {
1382 connected = 1;
1383 break;
1386 s = net_socket_fd_init(vlan, fd, connected);
1387 if (!s)
1388 return -1;
1389 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1390 "socket: connect to %s:%d",
1391 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1392 return 0;
1395 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
1397 NetSocketState *s;
1398 int fd;
1399 struct sockaddr_in saddr;
1401 if (parse_host_port(&saddr, host_str) < 0)
1402 return -1;
1405 fd = net_socket_mcast_create(&saddr);
1406 if (fd < 0)
1407 return -1;
1409 s = net_socket_fd_init(vlan, fd, 0);
1410 if (!s)
1411 return -1;
1413 s->dgram_dst = saddr;
1415 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1416 "socket: mcast=%s:%d",
1417 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1418 return 0;
1422 /* find or alloc a new VLAN */
1423 VLANState *qemu_find_vlan(int id)
1425 VLANState **pvlan, *vlan;
1426 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1427 if (vlan->id == id)
1428 return vlan;
1430 vlan = qemu_mallocz(sizeof(VLANState));
1431 if (!vlan)
1432 return NULL;
1433 vlan->id = id;
1434 vlan->next = NULL;
1435 pvlan = &first_vlan;
1436 while (*pvlan != NULL)
1437 pvlan = &(*pvlan)->next;
1438 *pvlan = vlan;
1439 return vlan;
1442 int net_client_init(const char *device, const char *p)
1444 char buf[1024];
1445 int vlan_id, ret;
1446 VLANState *vlan;
1448 vlan_id = 0;
1449 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1450 vlan_id = strtol(buf, NULL, 0);
1452 vlan = qemu_find_vlan(vlan_id);
1453 if (!vlan) {
1454 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1455 return -1;
1457 if (!strcmp(device, "nic")) {
1458 NICInfo *nd;
1459 uint8_t *macaddr;
1461 if (nb_nics >= MAX_NICS) {
1462 fprintf(stderr, "Too Many NICs\n");
1463 return -1;
1465 nd = &nd_table[nb_nics];
1466 macaddr = nd->macaddr;
1467 macaddr[0] = 0x52;
1468 macaddr[1] = 0x54;
1469 macaddr[2] = 0x00;
1470 macaddr[3] = 0x12;
1471 macaddr[4] = 0x34;
1472 macaddr[5] = 0x56 + nb_nics;
1474 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1475 if (parse_macaddr(macaddr, buf) < 0) {
1476 fprintf(stderr, "invalid syntax for ethernet address\n");
1477 return -1;
1480 if (get_param_value(buf, sizeof(buf), "model", p)) {
1481 nd->model = strdup(buf);
1483 nd->vlan = vlan;
1484 nb_nics++;
1485 vlan->nb_guest_devs++;
1486 ret = 0;
1487 } else
1488 if (!strcmp(device, "none")) {
1489 /* does nothing. It is needed to signal that no network cards
1490 are wanted */
1491 ret = 0;
1492 } else
1493 #ifdef CONFIG_SLIRP
1494 if (!strcmp(device, "user")) {
1495 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1496 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1498 vlan->nb_host_devs++;
1499 ret = net_slirp_init(vlan);
1500 } else
1501 #endif
1502 #ifdef _WIN32
1503 if (!strcmp(device, "tap")) {
1504 char ifname[64];
1505 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1506 fprintf(stderr, "tap: no interface name\n");
1507 return -1;
1509 vlan->nb_host_devs++;
1510 ret = tap_win32_init(vlan, ifname);
1511 } else
1512 #elif defined (_AIX)
1513 #else
1514 if (!strcmp(device, "tap")) {
1515 char ifname[64];
1516 char setup_script[1024], down_script[1024];
1517 int fd;
1518 vlan->nb_host_devs++;
1519 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1520 fd = strtol(buf, NULL, 0);
1521 fcntl(fd, F_SETFL, O_NONBLOCK);
1522 ret = -1;
1523 if (net_tap_fd_init(vlan, fd))
1524 ret = 0;
1525 } else {
1526 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1527 ifname[0] = '\0';
1529 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1530 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1532 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1533 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1535 ret = net_tap_init(vlan, ifname, setup_script, down_script);
1537 } else
1538 #endif
1539 if (!strcmp(device, "socket")) {
1540 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1541 int fd;
1542 fd = strtol(buf, NULL, 0);
1543 ret = -1;
1544 if (net_socket_fd_init(vlan, fd, 1))
1545 ret = 0;
1546 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1547 ret = net_socket_listen_init(vlan, buf);
1548 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1549 ret = net_socket_connect_init(vlan, buf);
1550 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1551 ret = net_socket_mcast_init(vlan, buf);
1552 } else {
1553 fprintf(stderr, "Unknown socket options: %s\n", p);
1554 return -1;
1556 vlan->nb_host_devs++;
1557 } else
1558 #ifdef CONFIG_VDE
1559 if (!strcmp(device, "vde")) {
1560 char vde_sock[1024], vde_group[512];
1561 int vde_port, vde_mode;
1562 vlan->nb_host_devs++;
1563 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1564 vde_sock[0] = '\0';
1566 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1567 vde_port = strtol(buf, NULL, 10);
1568 } else {
1569 vde_port = 0;
1571 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1572 vde_group[0] = '\0';
1574 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1575 vde_mode = strtol(buf, NULL, 8);
1576 } else {
1577 vde_mode = 0700;
1579 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
1580 } else
1581 #endif
1583 fprintf(stderr, "Unknown network device: %s\n", device);
1584 return -1;
1586 if (ret < 0) {
1587 fprintf(stderr, "Could not initialize device '%s'\n", device);
1590 return ret;
1593 int net_client_parse(const char *str)
1595 const char *p;
1596 char *q;
1597 char device[64];
1599 p = str;
1600 q = device;
1601 while (*p != '\0' && *p != ',') {
1602 if ((q - device) < sizeof(device) - 1)
1603 *q++ = *p;
1604 p++;
1606 *q = '\0';
1607 if (*p == ',')
1608 p++;
1610 return net_client_init(device, p);
1613 void do_info_network(void)
1615 VLANState *vlan;
1616 VLANClientState *vc;
1618 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1619 term_printf("VLAN %d devices:\n", vlan->id);
1620 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1621 term_printf(" %s\n", vc->info_str);
1625 void net_cleanup(void)
1627 VLANState *vlan;
1629 #if !defined(_WIN32)
1630 /* close network clients */
1631 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1632 VLANClientState *vc;
1634 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1635 if (vc->fd_read == tap_receive) {
1636 char ifname[64];
1637 TAPState *s = vc->opaque;
1639 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
1640 s->down_script[0])
1641 launch_script(s->down_script, ifname, s->fd);
1643 #if defined(CONFIG_VDE)
1644 if (vc->fd_read == vde_from_qemu) {
1645 VDEState *s = vc->opaque;
1646 vde_close(s->vde);
1648 #endif
1651 #endif
1654 void net_client_check(void)
1656 VLANState *vlan;
1658 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1659 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1660 continue;
1661 if (vlan->nb_guest_devs == 0)
1662 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1663 if (vlan->nb_host_devs == 0)
1664 fprintf(stderr,
1665 "Warning: vlan %d is not connected to host network\n",
1666 vlan->id);