Push_nmi should be only used by x86
[qemu-kvm/fedora.git] / net.c
blob6da5dc470f91f95f9291be0e025f0c2e757d9188
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 #include "qemu-kvm.h"
125 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
126 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
127 #ifdef __sun__
128 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
129 #else
130 #define SMBD_COMMAND "/usr/sbin/smbd"
131 #endif
133 static VLANState *first_vlan;
135 /***********************************************************/
136 /* network device redirectors */
138 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
139 static void hex_dump(FILE *f, const uint8_t *buf, int size)
141 int len, i, j, c;
143 for(i=0;i<size;i+=16) {
144 len = size - i;
145 if (len > 16)
146 len = 16;
147 fprintf(f, "%08x ", i);
148 for(j=0;j<16;j++) {
149 if (j < len)
150 fprintf(f, " %02x", buf[i+j]);
151 else
152 fprintf(f, " ");
154 fprintf(f, " ");
155 for(j=0;j<len;j++) {
156 c = buf[i+j];
157 if (c < ' ' || c > '~')
158 c = '.';
159 fprintf(f, "%c", c);
161 fprintf(f, "\n");
164 #endif
166 static int parse_macaddr(uint8_t *macaddr, const char *p)
168 int i;
169 char *last_char;
170 long int offset;
172 errno = 0;
173 offset = strtol(p, &last_char, 0);
174 if (0 == errno && '\0' == *last_char &&
175 offset >= 0 && offset <= 0xFFFFFF) {
176 macaddr[3] = (offset & 0xFF0000) >> 16;
177 macaddr[4] = (offset & 0xFF00) >> 8;
178 macaddr[5] = offset & 0xFF;
179 return 0;
180 } else {
181 for(i = 0; i < 6; i++) {
182 macaddr[i] = strtol(p, (char **)&p, 16);
183 if (i == 5) {
184 if (*p != '\0')
185 return -1;
186 } else {
187 if (*p != ':' && *p != '-')
188 return -1;
189 p++;
192 return 0;
195 return -1;
198 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
200 const char *p, *p1;
201 int len;
202 p = *pp;
203 p1 = strchr(p, sep);
204 if (!p1)
205 return -1;
206 len = p1 - p;
207 p1++;
208 if (buf_size > 0) {
209 if (len > buf_size - 1)
210 len = buf_size - 1;
211 memcpy(buf, p, len);
212 buf[len] = '\0';
214 *pp = p1;
215 return 0;
218 int parse_host_src_port(struct sockaddr_in *haddr,
219 struct sockaddr_in *saddr,
220 const char *input_str)
222 char *str = strdup(input_str);
223 char *host_str = str;
224 char *src_str;
225 const char *src_str2;
226 char *ptr;
229 * Chop off any extra arguments at the end of the string which
230 * would start with a comma, then fill in the src port information
231 * if it was provided else use the "any address" and "any port".
233 if ((ptr = strchr(str,',')))
234 *ptr = '\0';
236 if ((src_str = strchr(input_str,'@'))) {
237 *src_str = '\0';
238 src_str++;
241 if (parse_host_port(haddr, host_str) < 0)
242 goto fail;
244 src_str2 = src_str;
245 if (!src_str || *src_str == '\0')
246 src_str2 = ":0";
248 if (parse_host_port(saddr, src_str2) < 0)
249 goto fail;
251 free(str);
252 return(0);
254 fail:
255 free(str);
256 return -1;
259 int parse_host_port(struct sockaddr_in *saddr, const char *str)
261 char buf[512];
262 struct hostent *he;
263 const char *p, *r;
264 int port;
266 p = str;
267 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
268 return -1;
269 saddr->sin_family = AF_INET;
270 if (buf[0] == '\0') {
271 saddr->sin_addr.s_addr = 0;
272 } else {
273 if (qemu_isdigit(buf[0])) {
274 if (!inet_aton(buf, &saddr->sin_addr))
275 return -1;
276 } else {
277 if ((he = gethostbyname(buf)) == NULL)
278 return - 1;
279 saddr->sin_addr = *(struct in_addr *)he->h_addr;
282 port = strtol(p, (char **)&r, 0);
283 if (r == p)
284 return -1;
285 saddr->sin_port = htons(port);
286 return 0;
289 #ifndef _WIN32
290 int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
292 const char *p;
293 int len;
295 len = MIN(108, strlen(str));
296 p = strchr(str, ',');
297 if (p)
298 len = MIN(len, p - str);
300 memset(uaddr, 0, sizeof(*uaddr));
302 uaddr->sun_family = AF_UNIX;
303 memcpy(uaddr->sun_path, str, len);
305 return 0;
307 #endif
309 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
310 IOReadHandler *fd_read,
311 IOCanRWHandler *fd_can_read,
312 void *opaque)
314 VLANClientState *vc, **pvc;
315 vc = qemu_mallocz(sizeof(VLANClientState));
316 if (!vc)
317 return NULL;
318 vc->fd_read = fd_read;
319 vc->fd_can_read = fd_can_read;
320 vc->opaque = opaque;
321 vc->vlan = vlan;
323 vc->next = NULL;
324 pvc = &vlan->first_client;
325 while (*pvc != NULL)
326 pvc = &(*pvc)->next;
327 *pvc = vc;
328 return vc;
331 void qemu_del_vlan_client(VLANClientState *vc)
333 VLANClientState **pvc = &vc->vlan->first_client;
335 while (*pvc != NULL)
336 if (*pvc == vc) {
337 *pvc = vc->next;
338 free(vc);
339 break;
340 } else
341 pvc = &(*pvc)->next;
344 int qemu_can_send_packet(VLANClientState *vc1)
346 VLANState *vlan = vc1->vlan;
347 VLANClientState *vc;
349 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
350 if (vc != vc1) {
351 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
352 return 1;
355 return 0;
358 int qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
360 VLANState *vlan = vc1->vlan;
361 VLANClientState *vc;
362 int ret = -EAGAIN;
364 #ifdef DEBUG_NET
365 printf("vlan %d send:\n", vlan->id);
366 hex_dump(stdout, buf, size);
367 #endif
368 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
369 if (vc != vc1) {
370 if (!vc->fd_can_read || vc->fd_can_read(vc->opaque)) {
371 vc->fd_read(vc->opaque, buf, size);
372 ret = 0;
376 return ret;
380 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
381 int iovcnt)
383 char buffer[4096];
384 size_t offset = 0;
385 int i;
387 for (i = 0; i < iovcnt; i++) {
388 size_t len;
390 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
391 memcpy(buffer + offset, iov[i].iov_base, len);
392 offset += len;
395 vc->fd_read(vc->opaque, buffer, offset);
397 return offset;
400 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
401 int iovcnt)
403 VLANState *vlan = vc1->vlan;
404 VLANClientState *vc;
405 ssize_t max_len = 0;
407 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
408 ssize_t len = 0;
410 if (vc == vc1)
411 continue;
413 if (vc->fd_readv)
414 len = vc->fd_readv(vc->opaque, iov, iovcnt);
415 else if (vc->fd_read)
416 len = vc_sendv_compat(vc, iov, iovcnt);
418 max_len = MAX(max_len, len);
421 return max_len;
424 #if defined(CONFIG_SLIRP)
426 /* slirp network adapter */
428 static int slirp_inited;
429 static VLANClientState *slirp_vc;
431 int slirp_can_output(void)
433 return !slirp_vc || qemu_can_send_packet(slirp_vc);
436 void slirp_output(const uint8_t *pkt, int pkt_len)
438 #ifdef DEBUG_SLIRP
439 printf("slirp output:\n");
440 hex_dump(stdout, pkt, pkt_len);
441 #endif
442 if (!slirp_vc)
443 return;
444 qemu_send_packet(slirp_vc, pkt, pkt_len);
447 int slirp_is_inited(void)
449 return slirp_inited;
452 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
454 #ifdef DEBUG_SLIRP
455 printf("slirp input:\n");
456 hex_dump(stdout, buf, size);
457 #endif
458 slirp_input(buf, size);
461 static int net_slirp_init(VLANState *vlan)
463 if (!slirp_inited) {
464 slirp_inited = 1;
465 slirp_init();
467 slirp_vc = qemu_new_vlan_client(vlan,
468 slirp_receive, NULL, NULL);
469 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
470 return 0;
473 void net_slirp_redir(const char *redir_str)
475 int is_udp;
476 char buf[256], *r;
477 const char *p;
478 struct in_addr guest_addr;
479 int host_port, guest_port;
481 if (!slirp_inited) {
482 slirp_inited = 1;
483 slirp_init();
486 p = redir_str;
487 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
488 goto fail;
489 if (!strcmp(buf, "tcp")) {
490 is_udp = 0;
491 } else if (!strcmp(buf, "udp")) {
492 is_udp = 1;
493 } else {
494 goto fail;
497 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
498 goto fail;
499 host_port = strtol(buf, &r, 0);
500 if (r == buf)
501 goto fail;
503 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
504 goto fail;
505 if (buf[0] == '\0') {
506 pstrcpy(buf, sizeof(buf), "10.0.2.15");
508 if (!inet_aton(buf, &guest_addr))
509 goto fail;
511 guest_port = strtol(p, &r, 0);
512 if (r == p)
513 goto fail;
515 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
516 fprintf(stderr, "qemu: could not set up redirection\n");
517 exit(1);
519 return;
520 fail:
521 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
522 exit(1);
525 #ifndef _WIN32
527 static char smb_dir[1024];
529 static void erase_dir(char *dir_name)
531 DIR *d;
532 struct dirent *de;
533 char filename[1024];
535 /* erase all the files in the directory */
536 if ((d = opendir(dir_name)) != 0) {
537 for(;;) {
538 de = readdir(d);
539 if (!de)
540 break;
541 if (strcmp(de->d_name, ".") != 0 &&
542 strcmp(de->d_name, "..") != 0) {
543 snprintf(filename, sizeof(filename), "%s/%s",
544 smb_dir, de->d_name);
545 if (unlink(filename) != 0) /* is it a directory? */
546 erase_dir(filename);
549 closedir(d);
550 rmdir(dir_name);
554 /* automatic user mode samba server configuration */
555 static void smb_exit(void)
557 erase_dir(smb_dir);
560 /* automatic user mode samba server configuration */
561 void net_slirp_smb(const char *exported_dir)
563 char smb_conf[1024];
564 char smb_cmdline[1024];
565 FILE *f;
567 if (!slirp_inited) {
568 slirp_inited = 1;
569 slirp_init();
572 /* XXX: better tmp dir construction */
573 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
574 if (mkdir(smb_dir, 0700) < 0) {
575 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
576 exit(1);
578 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
580 f = fopen(smb_conf, "w");
581 if (!f) {
582 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
583 exit(1);
585 fprintf(f,
586 "[global]\n"
587 "private dir=%s\n"
588 "smb ports=0\n"
589 "socket address=127.0.0.1\n"
590 "pid directory=%s\n"
591 "lock directory=%s\n"
592 "log file=%s/log.smbd\n"
593 "smb passwd file=%s/smbpasswd\n"
594 "security = share\n"
595 "[qemu]\n"
596 "path=%s\n"
597 "read only=no\n"
598 "guest ok=yes\n",
599 smb_dir,
600 smb_dir,
601 smb_dir,
602 smb_dir,
603 smb_dir,
604 exported_dir
606 fclose(f);
607 atexit(smb_exit);
609 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
610 SMBD_COMMAND, smb_conf);
612 slirp_add_exec(0, smb_cmdline, 4, 139);
615 #endif /* !defined(_WIN32) */
616 void do_info_slirp(void)
618 slirp_stats();
621 #endif /* CONFIG_SLIRP */
623 #ifdef _WIN32
625 int tap_has_vnet_hdr(void *opaque)
627 return 0;
630 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
634 #else /* !defined(_WIN32) */
636 /* Maximum GSO packet size (64k) plus plenty of room for
637 * the ethernet and virtio_net headers
639 #define TAP_BUFSIZE (4096 + 65536)
641 #ifdef IFF_VNET_HDR
642 #include <linux/virtio_net.h>
643 #endif
645 typedef struct TAPState {
646 VLANClientState *vc;
647 int fd;
648 char down_script[1024];
649 char buf[TAP_BUFSIZE];
650 int size;
651 unsigned int has_vnet_hdr : 1;
652 unsigned int using_vnet_hdr : 1;
653 } TAPState;
655 static ssize_t tap_writev(void *opaque, const struct iovec *iov,
656 int iovcnt)
658 TAPState *s = opaque;
659 ssize_t len;
661 do {
662 len = writev(s->fd, iov, iovcnt);
663 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
665 return len;
668 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
669 int iovcnt)
671 #ifdef IFF_VNET_HDR
672 TAPState *s = opaque;
674 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
675 struct iovec *iov_copy;
676 struct virtio_net_hdr hdr = { 0, };
678 iov_copy = alloca(sizeof(struct iovec) * (iovcnt + 1));
680 iov_copy[0].iov_base = &hdr;
681 iov_copy[0].iov_len = sizeof(hdr);
683 memcpy(&iov_copy[1], iov, sizeof(struct iovec) * iovcnt);
685 return tap_writev(opaque, iov_copy, iovcnt + 1);
687 #endif
689 return tap_writev(opaque, iov, iovcnt);
692 static void tap_receive(void *opaque, const uint8_t *buf, int size)
694 struct iovec iov[2];
695 int i = 0;
697 #ifdef IFF_VNET_HDR
698 TAPState *s = opaque;
699 struct virtio_net_hdr hdr = { 0, };
701 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
702 iov[i].iov_base = &hdr;
703 iov[i].iov_len = sizeof(hdr);
704 i++;
706 #endif
708 iov[i].iov_base = (char *) buf;
709 iov[i].iov_len = size;
710 i++;
712 tap_writev(opaque, iov, i);
715 static int tap_can_send(void *opaque)
717 TAPState *s = opaque;
718 VLANClientState *vc;
719 int can_receive = 0;
721 /* Check to see if any of our clients can receive a packet */
722 for (vc = s->vc->vlan->first_client; vc; vc = vc->next) {
723 /* Skip ourselves */
724 if (vc == s->vc)
725 continue;
727 if (!vc->fd_can_read) {
728 /* no fd_can_read handler, they always can receive */
729 can_receive = 1;
730 } else
731 can_receive = vc->fd_can_read(vc->opaque);
733 /* Once someone can receive, we try to send a packet */
734 if (can_receive)
735 break;
738 return can_receive;
741 static int tap_send_packet(TAPState *s)
743 uint8_t *buf = s->buf;
744 int size = s->size;
746 #ifdef IFF_VNET_HDR
747 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
748 buf += sizeof(struct virtio_net_hdr);
749 size -= sizeof(struct virtio_net_hdr);
751 #endif
753 return qemu_send_packet(s->vc, buf, size);
756 static void tap_send(void *opaque)
758 TAPState *s = opaque;
760 /* First try to send any buffered packet */
761 if (s->size > 0) {
762 int err;
764 /* If noone can receive the packet, buffer it */
765 err = tap_send_packet(s);
766 if (err == -EAGAIN)
767 return;
770 /* Read packets until we hit EAGAIN */
771 do {
772 #ifdef __sun__
773 struct strbuf sbuf;
774 int f = 0;
775 sbuf.maxlen = sizeof(s->buf);
776 sbuf.buf = s->buf;
777 s->size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
778 #else
779 kvm_sleep_begin();
780 s->size = read(s->fd, s->buf, sizeof(s->buf));
781 kvm_sleep_end();
782 #endif
784 if (s->size == -1 && errno == EINTR)
785 continue;
787 if (s->size > 0) {
788 int err;
790 /* If noone can receive the packet, buffer it */
791 err = tap_send_packet(s);
792 if (err == -EAGAIN)
793 break;
795 } while (s->size > 0);
798 int tap_has_vnet_hdr(void *opaque)
800 VLANClientState *vc = opaque;
801 TAPState *s = vc->opaque;
803 return s ? s->has_vnet_hdr : 0;
806 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
808 VLANClientState *vc = opaque;
809 TAPState *s = vc->opaque;
811 if (!s || !s->has_vnet_hdr)
812 return;
814 s->using_vnet_hdr = using_vnet_hdr != 0;
817 static int tap_probe_vnet_hdr(int fd)
819 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
820 struct ifreq ifr;
822 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
823 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
824 return 0;
827 return ifr.ifr_flags & IFF_VNET_HDR;
828 #else
829 return 0;
830 #endif
833 #ifdef TUNSETOFFLOAD
834 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
835 int ecn)
837 TAPState *s = vc->opaque;
838 unsigned int offload = 0;
840 if (csum) {
841 offload |= TUN_F_CSUM;
842 if (tso4)
843 offload |= TUN_F_TSO4;
844 if (tso6)
845 offload |= TUN_F_TSO6;
846 if ((tso4 || tso6) && ecn)
847 offload |= TUN_F_TSO_ECN;
850 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
851 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
852 strerror(errno));
854 #endif /* TUNSETOFFLOAD */
856 /* fd support */
858 static TAPState *net_tap_fd_init(VLANState *vlan, int fd, int vnet_hdr)
860 TAPState *s;
862 s = qemu_mallocz(sizeof(TAPState));
863 if (!s)
864 return NULL;
865 s->fd = fd;
866 s->has_vnet_hdr = vnet_hdr != 0;
867 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
868 s->vc->fd_readv = tap_receive_iov;
869 #ifdef TUNSETOFFLOAD
870 s->vc->set_offload = tap_set_offload;
871 #endif
872 qemu_set_fd_handler2(s->fd, tap_can_send, tap_send, NULL, s);
873 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
874 return s;
877 #if defined (_BSD) || defined (__FreeBSD_kernel__)
878 static int tap_open(char *ifname, int ifname_size)
880 int fd;
881 char *dev;
882 struct stat s;
884 TFR(fd = open("/dev/tap", O_RDWR));
885 if (fd < 0) {
886 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
887 return -1;
890 fstat(fd, &s);
891 dev = devname(s.st_rdev, S_IFCHR);
892 pstrcpy(ifname, ifname_size, dev);
894 fcntl(fd, F_SETFL, O_NONBLOCK);
895 return fd;
897 #elif defined(__sun__)
898 #define TUNNEWPPA (('T'<<16) | 0x0001)
900 * Allocate TAP device, returns opened fd.
901 * Stores dev name in the first arg(must be large enough).
903 int tap_alloc(char *dev, size_t dev_size)
905 int tap_fd, if_fd, ppa = -1;
906 static int ip_fd = 0;
907 char *ptr;
909 static int arp_fd = 0;
910 int ip_muxid, arp_muxid;
911 struct strioctl strioc_if, strioc_ppa;
912 int link_type = I_PLINK;;
913 struct lifreq ifr;
914 char actual_name[32] = "";
916 memset(&ifr, 0x0, sizeof(ifr));
918 if( *dev ){
919 ptr = dev;
920 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
921 ppa = atoi(ptr);
924 /* Check if IP device was opened */
925 if( ip_fd )
926 close(ip_fd);
928 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
929 if (ip_fd < 0) {
930 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
931 return -1;
934 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
935 if (tap_fd < 0) {
936 syslog(LOG_ERR, "Can't open /dev/tap");
937 return -1;
940 /* Assign a new PPA and get its unit number. */
941 strioc_ppa.ic_cmd = TUNNEWPPA;
942 strioc_ppa.ic_timout = 0;
943 strioc_ppa.ic_len = sizeof(ppa);
944 strioc_ppa.ic_dp = (char *)&ppa;
945 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
946 syslog (LOG_ERR, "Can't assign new interface");
948 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
949 if (if_fd < 0) {
950 syslog(LOG_ERR, "Can't open /dev/tap (2)");
951 return -1;
953 if(ioctl(if_fd, I_PUSH, "ip") < 0){
954 syslog(LOG_ERR, "Can't push IP module");
955 return -1;
958 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
959 syslog(LOG_ERR, "Can't get flags\n");
961 snprintf (actual_name, 32, "tap%d", ppa);
962 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
964 ifr.lifr_ppa = ppa;
965 /* Assign ppa according to the unit number returned by tun device */
967 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
968 syslog (LOG_ERR, "Can't set PPA %d", ppa);
969 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
970 syslog (LOG_ERR, "Can't get flags\n");
971 /* Push arp module to if_fd */
972 if (ioctl (if_fd, I_PUSH, "arp") < 0)
973 syslog (LOG_ERR, "Can't push ARP module (2)");
975 /* Push arp module to ip_fd */
976 if (ioctl (ip_fd, I_POP, NULL) < 0)
977 syslog (LOG_ERR, "I_POP failed\n");
978 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
979 syslog (LOG_ERR, "Can't push ARP module (3)\n");
980 /* Open arp_fd */
981 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
982 if (arp_fd < 0)
983 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
985 /* Set ifname to arp */
986 strioc_if.ic_cmd = SIOCSLIFNAME;
987 strioc_if.ic_timout = 0;
988 strioc_if.ic_len = sizeof(ifr);
989 strioc_if.ic_dp = (char *)&ifr;
990 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
991 syslog (LOG_ERR, "Can't set ifname to arp\n");
994 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
995 syslog(LOG_ERR, "Can't link TAP device to IP");
996 return -1;
999 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1000 syslog (LOG_ERR, "Can't link TAP device to ARP");
1002 close (if_fd);
1004 memset(&ifr, 0x0, sizeof(ifr));
1005 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1006 ifr.lifr_ip_muxid = ip_muxid;
1007 ifr.lifr_arp_muxid = arp_muxid;
1009 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1011 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1012 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1013 syslog (LOG_ERR, "Can't set multiplexor id");
1016 snprintf(dev, dev_size, "tap%d", ppa);
1017 return tap_fd;
1020 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1022 char dev[10]="";
1023 int fd;
1024 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1025 fprintf(stderr, "Cannot allocate TAP device\n");
1026 return -1;
1028 pstrcpy(ifname, ifname_size, dev);
1029 fcntl(fd, F_SETFL, O_NONBLOCK);
1030 return fd;
1032 #elif defined (_AIX)
1033 static int tap_open(char *ifname, int ifname_size)
1035 fprintf (stderr, "no tap on AIX\n");
1036 return -1;
1038 #else
1039 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1041 struct ifreq ifr;
1042 int fd, ret;
1044 TFR(fd = open("/dev/net/tun", O_RDWR));
1045 if (fd < 0) {
1046 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1047 return -1;
1049 memset(&ifr, 0, sizeof(ifr));
1050 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1052 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1054 unsigned int features;
1056 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1057 features & IFF_VNET_HDR) {
1058 *vnet_hdr = 1;
1059 ifr.ifr_flags |= IFF_VNET_HDR;
1062 #endif
1064 if (ifname[0] != '\0')
1065 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1066 else
1067 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1068 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1069 if (ret != 0) {
1070 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1071 close(fd);
1072 return -1;
1074 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1075 fcntl(fd, F_SETFL, O_NONBLOCK);
1076 return fd;
1078 #endif
1080 static int launch_script(const char *setup_script, const char *ifname, int fd)
1082 int pid, status;
1083 char *args[3];
1084 char **parg;
1086 /* try to launch network script */
1087 pid = fork();
1088 if (pid >= 0) {
1089 if (pid == 0) {
1090 int open_max = sysconf (_SC_OPEN_MAX), i;
1091 for (i = 0; i < open_max; i++)
1092 if (i != STDIN_FILENO &&
1093 i != STDOUT_FILENO &&
1094 i != STDERR_FILENO &&
1095 i != fd)
1096 close(i);
1098 parg = args;
1099 *parg++ = (char *)setup_script;
1100 *parg++ = (char *)ifname;
1101 *parg++ = NULL;
1102 execv(setup_script, args);
1103 _exit(1);
1105 while (waitpid(pid, &status, 0) != pid);
1106 if (!WIFEXITED(status) ||
1107 WEXITSTATUS(status) != 0) {
1108 fprintf(stderr, "%s: could not launch network script\n",
1109 setup_script);
1110 return -1;
1113 return 0;
1116 static int net_tap_init(VLANState *vlan, const char *ifname1,
1117 const char *setup_script, const char *down_script)
1119 TAPState *s;
1120 int fd;
1121 int vnet_hdr;
1122 char ifname[128];
1124 if (ifname1 != NULL)
1125 pstrcpy(ifname, sizeof(ifname), ifname1);
1126 else
1127 ifname[0] = '\0';
1128 vnet_hdr = 0;
1129 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1130 if (fd < 0)
1131 return -1;
1133 if (!setup_script || !strcmp(setup_script, "no"))
1134 setup_script = "";
1135 if (setup_script[0] != '\0') {
1136 if (launch_script(setup_script, ifname, fd))
1137 return -1;
1139 s = net_tap_fd_init(vlan, fd, vnet_hdr);
1140 if (!s)
1141 return -1;
1143 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1144 "tap: ifname=%s setup_script=%s", ifname, setup_script);
1145 if (down_script && strcmp(down_script, "no"))
1146 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1147 return 0;
1150 #endif /* !_WIN32 */
1152 #if defined(CONFIG_VDE)
1153 typedef struct VDEState {
1154 VLANClientState *vc;
1155 VDECONN *vde;
1156 } VDEState;
1158 static void vde_to_qemu(void *opaque)
1160 VDEState *s = opaque;
1161 uint8_t buf[4096];
1162 int size;
1164 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1165 if (size > 0) {
1166 qemu_send_packet(s->vc, buf, size);
1170 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1172 VDEState *s = opaque;
1173 int ret;
1174 for(;;) {
1175 ret = vde_send(s->vde, buf, size, 0);
1176 if (ret < 0 && errno == EINTR) {
1177 } else {
1178 break;
1183 static int net_vde_init(VLANState *vlan, const char *sock, int port,
1184 const char *group, int mode)
1186 VDEState *s;
1187 char *init_group = strlen(group) ? (char *)group : NULL;
1188 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1190 struct vde_open_args args = {
1191 .port = port,
1192 .group = init_group,
1193 .mode = mode,
1196 s = qemu_mallocz(sizeof(VDEState));
1197 if (!s)
1198 return -1;
1199 s->vde = vde_open(init_sock, "QEMU", &args);
1200 if (!s->vde){
1201 free(s);
1202 return -1;
1204 s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
1205 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1206 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
1207 sock, vde_datafd(s->vde));
1208 return 0;
1210 #endif
1212 /* network connection */
1213 typedef struct NetSocketState {
1214 VLANClientState *vc;
1215 int fd;
1216 int state; /* 0 = getting length, 1 = getting data */
1217 int index;
1218 int packet_len;
1219 uint8_t buf[4096];
1220 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1221 } NetSocketState;
1223 typedef struct NetSocketListenState {
1224 VLANState *vlan;
1225 int fd;
1226 } NetSocketListenState;
1228 /* XXX: we consider we can send the whole packet without blocking */
1229 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1231 NetSocketState *s = opaque;
1232 uint32_t len;
1233 len = htonl(size);
1235 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1236 send_all(s->fd, buf, size);
1239 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1241 NetSocketState *s = opaque;
1242 sendto(s->fd, buf, size, 0,
1243 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1246 static void net_socket_send(void *opaque)
1248 NetSocketState *s = opaque;
1249 int l, size, err;
1250 uint8_t buf1[4096];
1251 const uint8_t *buf;
1253 size = recv(s->fd, buf1, sizeof(buf1), 0);
1254 if (size < 0) {
1255 err = socket_error();
1256 if (err != EWOULDBLOCK)
1257 goto eoc;
1258 } else if (size == 0) {
1259 /* end of connection */
1260 eoc:
1261 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1262 closesocket(s->fd);
1263 return;
1265 buf = buf1;
1266 while (size > 0) {
1267 /* reassemble a packet from the network */
1268 switch(s->state) {
1269 case 0:
1270 l = 4 - s->index;
1271 if (l > size)
1272 l = size;
1273 memcpy(s->buf + s->index, buf, l);
1274 buf += l;
1275 size -= l;
1276 s->index += l;
1277 if (s->index == 4) {
1278 /* got length */
1279 s->packet_len = ntohl(*(uint32_t *)s->buf);
1280 s->index = 0;
1281 s->state = 1;
1283 break;
1284 case 1:
1285 l = s->packet_len - s->index;
1286 if (l > size)
1287 l = size;
1288 memcpy(s->buf + s->index, buf, l);
1289 s->index += l;
1290 buf += l;
1291 size -= l;
1292 if (s->index >= s->packet_len) {
1293 qemu_send_packet(s->vc, s->buf, s->packet_len);
1294 s->index = 0;
1295 s->state = 0;
1297 break;
1302 static void net_socket_send_dgram(void *opaque)
1304 NetSocketState *s = opaque;
1305 int size;
1307 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1308 if (size < 0)
1309 return;
1310 if (size == 0) {
1311 /* end of connection */
1312 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1313 return;
1315 qemu_send_packet(s->vc, s->buf, size);
1318 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1320 struct ip_mreq imr;
1321 int fd;
1322 int val, ret;
1323 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1324 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1325 inet_ntoa(mcastaddr->sin_addr),
1326 (int)ntohl(mcastaddr->sin_addr.s_addr));
1327 return -1;
1330 fd = socket(PF_INET, SOCK_DGRAM, 0);
1331 if (fd < 0) {
1332 perror("socket(PF_INET, SOCK_DGRAM)");
1333 return -1;
1336 val = 1;
1337 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1338 (const char *)&val, sizeof(val));
1339 if (ret < 0) {
1340 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1341 goto fail;
1344 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1345 if (ret < 0) {
1346 perror("bind");
1347 goto fail;
1350 /* Add host to multicast group */
1351 imr.imr_multiaddr = mcastaddr->sin_addr;
1352 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1354 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1355 (const char *)&imr, sizeof(struct ip_mreq));
1356 if (ret < 0) {
1357 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1358 goto fail;
1361 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1362 val = 1;
1363 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1364 (const char *)&val, sizeof(val));
1365 if (ret < 0) {
1366 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1367 goto fail;
1370 socket_set_nonblock(fd);
1371 return fd;
1372 fail:
1373 if (fd >= 0)
1374 closesocket(fd);
1375 return -1;
1378 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
1379 int is_connected)
1381 struct sockaddr_in saddr;
1382 int newfd;
1383 socklen_t saddr_len;
1384 NetSocketState *s;
1386 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1387 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1388 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1391 if (is_connected) {
1392 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1393 /* must be bound */
1394 if (saddr.sin_addr.s_addr==0) {
1395 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1396 fd);
1397 return NULL;
1399 /* clone dgram socket */
1400 newfd = net_socket_mcast_create(&saddr);
1401 if (newfd < 0) {
1402 /* error already reported by net_socket_mcast_create() */
1403 close(fd);
1404 return NULL;
1406 /* clone newfd to fd, close newfd */
1407 dup2(newfd, fd);
1408 close(newfd);
1410 } else {
1411 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1412 fd, strerror(errno));
1413 return NULL;
1417 s = qemu_mallocz(sizeof(NetSocketState));
1418 if (!s)
1419 return NULL;
1420 s->fd = fd;
1422 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
1423 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1425 /* mcast: save bound address as dst */
1426 if (is_connected) s->dgram_dst=saddr;
1428 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1429 "socket: fd=%d (%s mcast=%s:%d)",
1430 fd, is_connected? "cloned" : "",
1431 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1432 return s;
1435 static void net_socket_connect(void *opaque)
1437 NetSocketState *s = opaque;
1438 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1441 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
1442 int is_connected)
1444 NetSocketState *s;
1445 s = qemu_mallocz(sizeof(NetSocketState));
1446 if (!s)
1447 return NULL;
1448 s->fd = fd;
1449 s->vc = qemu_new_vlan_client(vlan,
1450 net_socket_receive, NULL, s);
1451 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1452 "socket: fd=%d", fd);
1453 if (is_connected) {
1454 net_socket_connect(s);
1455 } else {
1456 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1458 return s;
1461 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
1462 int is_connected)
1464 int so_type=-1, optlen=sizeof(so_type);
1466 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1467 (socklen_t *)&optlen)< 0) {
1468 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1469 return NULL;
1471 switch(so_type) {
1472 case SOCK_DGRAM:
1473 return net_socket_fd_init_dgram(vlan, fd, is_connected);
1474 case SOCK_STREAM:
1475 return net_socket_fd_init_stream(vlan, fd, is_connected);
1476 default:
1477 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1478 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1479 return net_socket_fd_init_stream(vlan, fd, is_connected);
1481 return NULL;
1484 static void net_socket_accept(void *opaque)
1486 NetSocketListenState *s = opaque;
1487 NetSocketState *s1;
1488 struct sockaddr_in saddr;
1489 socklen_t len;
1490 int fd;
1492 for(;;) {
1493 len = sizeof(saddr);
1494 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1495 if (fd < 0 && errno != EINTR) {
1496 return;
1497 } else if (fd >= 0) {
1498 break;
1501 s1 = net_socket_fd_init(s->vlan, fd, 1);
1502 if (!s1) {
1503 closesocket(fd);
1504 } else {
1505 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1506 "socket: connection from %s:%d",
1507 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1511 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
1513 NetSocketListenState *s;
1514 int fd, val, ret;
1515 struct sockaddr_in saddr;
1517 if (parse_host_port(&saddr, host_str) < 0)
1518 return -1;
1520 s = qemu_mallocz(sizeof(NetSocketListenState));
1521 if (!s)
1522 return -1;
1524 fd = socket(PF_INET, SOCK_STREAM, 0);
1525 if (fd < 0) {
1526 perror("socket");
1527 return -1;
1529 socket_set_nonblock(fd);
1531 /* allow fast reuse */
1532 val = 1;
1533 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1535 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1536 if (ret < 0) {
1537 perror("bind");
1538 return -1;
1540 ret = listen(fd, 0);
1541 if (ret < 0) {
1542 perror("listen");
1543 return -1;
1545 s->vlan = vlan;
1546 s->fd = fd;
1547 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1548 return 0;
1551 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
1553 NetSocketState *s;
1554 int fd, connected, ret, err;
1555 struct sockaddr_in saddr;
1557 if (parse_host_port(&saddr, host_str) < 0)
1558 return -1;
1560 fd = socket(PF_INET, SOCK_STREAM, 0);
1561 if (fd < 0) {
1562 perror("socket");
1563 return -1;
1565 socket_set_nonblock(fd);
1567 connected = 0;
1568 for(;;) {
1569 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1570 if (ret < 0) {
1571 err = socket_error();
1572 if (err == EINTR || err == EWOULDBLOCK) {
1573 } else if (err == EINPROGRESS) {
1574 break;
1575 #ifdef _WIN32
1576 } else if (err == WSAEALREADY) {
1577 break;
1578 #endif
1579 } else {
1580 perror("connect");
1581 closesocket(fd);
1582 return -1;
1584 } else {
1585 connected = 1;
1586 break;
1589 s = net_socket_fd_init(vlan, fd, connected);
1590 if (!s)
1591 return -1;
1592 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1593 "socket: connect to %s:%d",
1594 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1595 return 0;
1598 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
1600 NetSocketState *s;
1601 int fd;
1602 struct sockaddr_in saddr;
1604 if (parse_host_port(&saddr, host_str) < 0)
1605 return -1;
1608 fd = net_socket_mcast_create(&saddr);
1609 if (fd < 0)
1610 return -1;
1612 s = net_socket_fd_init(vlan, fd, 0);
1613 if (!s)
1614 return -1;
1616 s->dgram_dst = saddr;
1618 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1619 "socket: mcast=%s:%d",
1620 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1621 return 0;
1625 /* find or alloc a new VLAN */
1626 VLANState *qemu_find_vlan(int id)
1628 VLANState **pvlan, *vlan;
1629 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1630 if (vlan->id == id)
1631 return vlan;
1633 vlan = qemu_mallocz(sizeof(VLANState));
1634 if (!vlan)
1635 return NULL;
1636 vlan->id = id;
1637 vlan->next = NULL;
1638 pvlan = &first_vlan;
1639 while (*pvlan != NULL)
1640 pvlan = &(*pvlan)->next;
1641 *pvlan = vlan;
1642 return vlan;
1645 int net_client_init(const char *device, const char *p)
1647 char buf[1024];
1648 int vlan_id, ret;
1649 VLANState *vlan;
1651 vlan_id = 0;
1652 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1653 vlan_id = strtol(buf, NULL, 0);
1655 vlan = qemu_find_vlan(vlan_id);
1656 if (!vlan) {
1657 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1658 return -1;
1660 if (!strcmp(device, "nic")) {
1661 NICInfo *nd;
1662 uint8_t *macaddr;
1664 if (nb_nics >= MAX_NICS) {
1665 fprintf(stderr, "Too Many NICs\n");
1666 return -1;
1668 nd = &nd_table[nb_nics];
1669 macaddr = nd->macaddr;
1670 macaddr[0] = 0x52;
1671 macaddr[1] = 0x54;
1672 macaddr[2] = 0x00;
1673 macaddr[3] = 0x12;
1674 macaddr[4] = 0x34;
1675 macaddr[5] = 0x56 + nb_nics;
1677 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1678 if (parse_macaddr(macaddr, buf) < 0) {
1679 fprintf(stderr, "invalid syntax for ethernet address\n");
1680 return -1;
1683 if (get_param_value(buf, sizeof(buf), "model", p)) {
1684 nd->model = strdup(buf);
1686 nd->vlan = vlan;
1687 nb_nics++;
1688 vlan->nb_guest_devs++;
1689 ret = 0;
1690 } else
1691 if (!strcmp(device, "none")) {
1692 /* does nothing. It is needed to signal that no network cards
1693 are wanted */
1694 ret = 0;
1695 } else
1696 #ifdef CONFIG_SLIRP
1697 if (!strcmp(device, "user")) {
1698 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1699 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1701 vlan->nb_host_devs++;
1702 ret = net_slirp_init(vlan);
1703 } else
1704 #endif
1705 #ifdef _WIN32
1706 if (!strcmp(device, "tap")) {
1707 char ifname[64];
1708 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1709 fprintf(stderr, "tap: no interface name\n");
1710 return -1;
1712 vlan->nb_host_devs++;
1713 ret = tap_win32_init(vlan, ifname);
1714 } else
1715 #elif defined (_AIX)
1716 #else
1717 if (!strcmp(device, "tap")) {
1718 char ifname[64];
1719 char setup_script[1024], down_script[1024];
1720 int fd;
1721 vlan->nb_host_devs++;
1722 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1723 fd = strtol(buf, NULL, 0);
1724 fcntl(fd, F_SETFL, O_NONBLOCK);
1725 ret = -1;
1726 if (net_tap_fd_init(vlan, fd, tap_probe_vnet_hdr(fd)))
1727 ret = 0;
1728 } else {
1729 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1730 ifname[0] = '\0';
1732 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1733 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1735 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1736 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1738 ret = net_tap_init(vlan, ifname, setup_script, down_script);
1740 } else
1741 #endif
1742 if (!strcmp(device, "socket")) {
1743 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1744 int fd;
1745 fd = strtol(buf, NULL, 0);
1746 ret = -1;
1747 if (net_socket_fd_init(vlan, fd, 1))
1748 ret = 0;
1749 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1750 ret = net_socket_listen_init(vlan, buf);
1751 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1752 ret = net_socket_connect_init(vlan, buf);
1753 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1754 ret = net_socket_mcast_init(vlan, buf);
1755 } else {
1756 fprintf(stderr, "Unknown socket options: %s\n", p);
1757 return -1;
1759 vlan->nb_host_devs++;
1760 } else
1761 #ifdef CONFIG_VDE
1762 if (!strcmp(device, "vde")) {
1763 char vde_sock[1024], vde_group[512];
1764 int vde_port, vde_mode;
1765 vlan->nb_host_devs++;
1766 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1767 vde_sock[0] = '\0';
1769 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1770 vde_port = strtol(buf, NULL, 10);
1771 } else {
1772 vde_port = 0;
1774 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1775 vde_group[0] = '\0';
1777 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1778 vde_mode = strtol(buf, NULL, 8);
1779 } else {
1780 vde_mode = 0700;
1782 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
1783 } else
1784 #endif
1786 fprintf(stderr, "Unknown network device: %s\n", device);
1787 return -1;
1789 if (ret < 0) {
1790 fprintf(stderr, "Could not initialize device '%s'\n", device);
1793 return ret;
1796 void net_client_uninit(NICInfo *nd)
1798 nd->vlan->nb_guest_devs--; /* XXX: free vlan on last reference */
1799 nb_nics--;
1800 nd->used = 0;
1801 free((void *)nd->model);
1804 int net_client_parse(const char *str)
1806 const char *p;
1807 char *q;
1808 char device[64];
1810 p = str;
1811 q = device;
1812 while (*p != '\0' && *p != ',') {
1813 if ((q - device) < sizeof(device) - 1)
1814 *q++ = *p;
1815 p++;
1817 *q = '\0';
1818 if (*p == ',')
1819 p++;
1821 return net_client_init(device, p);
1824 void do_info_network(void)
1826 VLANState *vlan;
1827 VLANClientState *vc;
1829 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1830 term_printf("VLAN %d devices:\n", vlan->id);
1831 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1832 term_printf(" %s\n", vc->info_str);
1836 void net_cleanup(void)
1838 VLANState *vlan;
1840 #if !defined(_WIN32)
1841 /* close network clients */
1842 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1843 VLANClientState *vc;
1845 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1846 if (vc->fd_read == tap_receive) {
1847 char ifname[64];
1848 TAPState *s = vc->opaque;
1850 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
1851 s->down_script[0])
1852 launch_script(s->down_script, ifname, s->fd);
1854 #if defined(CONFIG_VDE)
1855 if (vc->fd_read == vde_from_qemu) {
1856 VDEState *s = vc->opaque;
1857 vde_close(s->vde);
1859 #endif
1862 #endif
1865 void net_client_check(void)
1867 VLANState *vlan;
1869 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1870 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1871 continue;
1872 if (vlan->nb_guest_devs == 0)
1873 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1874 if (vlan->nb_host_devs == 0)
1875 fprintf(stderr,
1876 "Warning: vlan %d is not connected to host network\n",
1877 vlan->id);