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
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
40 #include "audio/audio.h"
41 #include "migration.h"
52 #include <sys/times.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
61 #include <sys/select.h>
62 #include <arpa/inet.h>
65 #if !defined(__APPLE__) && !defined(__OpenBSD__)
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
76 #include <linux/if_tun.h>
79 #include <linux/rtc.h>
81 /* For the benefit of older linux systems which don't supply it,
82 we use a local copy of hpet.h. */
83 /* #include <linux/hpet.h> */
86 #include <linux/ppdev.h>
87 #include <linux/parport.h>
91 #include <sys/ethernet.h>
92 #include <sys/sockio.h>
93 #include <netinet/arp.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h> // must come after ip.h
98 #include <netinet/udp.h>
99 #include <netinet/tcp.h>
107 #include "qemu_socket.h"
109 #if defined(CONFIG_SLIRP)
110 #include "libslirp.h"
113 #if defined(__OpenBSD__)
117 #if defined(CONFIG_VDE)
118 #include <libvdeplug.h>
123 #include <sys/timeb.h>
124 #include <mmsystem.h>
125 #define getopt_long_only getopt_long
126 #define memalign(align, size) malloc(size)
129 #include "qemu-kvm.h"
131 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
132 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
134 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
136 #define SMBD_COMMAND "/usr/sbin/smbd"
139 static VLANState
*first_vlan
;
141 /***********************************************************/
142 /* network device redirectors */
144 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
145 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
149 for(i
=0;i
<size
;i
+=16) {
153 fprintf(f
, "%08x ", i
);
156 fprintf(f
, " %02x", buf
[i
+j
]);
163 if (c
< ' ' || c
> '~')
172 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
179 offset
= strtol(p
, &last_char
, 0);
180 if (0 == errno
&& '\0' == *last_char
&&
181 offset
>= 0 && offset
<= 0xFFFFFF) {
182 macaddr
[3] = (offset
& 0xFF0000) >> 16;
183 macaddr
[4] = (offset
& 0xFF00) >> 8;
184 macaddr
[5] = offset
& 0xFF;
187 for(i
= 0; i
< 6; i
++) {
188 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
193 if (*p
!= ':' && *p
!= '-')
204 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
215 if (len
> buf_size
- 1)
224 int parse_host_src_port(struct sockaddr_in
*haddr
,
225 struct sockaddr_in
*saddr
,
226 const char *input_str
)
228 char *str
= strdup(input_str
);
229 char *host_str
= str
;
231 const char *src_str2
;
235 * Chop off any extra arguments at the end of the string which
236 * would start with a comma, then fill in the src port information
237 * if it was provided else use the "any address" and "any port".
239 if ((ptr
= strchr(str
,',')))
242 if ((src_str
= strchr(input_str
,'@'))) {
247 if (parse_host_port(haddr
, host_str
) < 0)
251 if (!src_str
|| *src_str
== '\0')
254 if (parse_host_port(saddr
, src_str2
) < 0)
265 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
273 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
275 saddr
->sin_family
= AF_INET
;
276 if (buf
[0] == '\0') {
277 saddr
->sin_addr
.s_addr
= 0;
279 if (isdigit(buf
[0])) {
280 if (!inet_aton(buf
, &saddr
->sin_addr
))
283 if ((he
= gethostbyname(buf
)) == NULL
)
285 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
288 port
= strtol(p
, (char **)&r
, 0);
291 saddr
->sin_port
= htons(port
);
296 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
301 len
= MIN(108, strlen(str
));
302 p
= strchr(str
, ',');
304 len
= MIN(len
, p
- str
);
306 memset(uaddr
, 0, sizeof(*uaddr
));
308 uaddr
->sun_family
= AF_UNIX
;
309 memcpy(uaddr
->sun_path
, str
, len
);
315 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
316 IOReadHandler
*fd_read
,
317 IOCanRWHandler
*fd_can_read
,
320 VLANClientState
*vc
, **pvc
;
321 vc
= qemu_mallocz(sizeof(VLANClientState
));
324 vc
->fd_read
= fd_read
;
325 vc
->fd_can_read
= fd_can_read
;
330 pvc
= &vlan
->first_client
;
337 void qemu_del_vlan_client(VLANClientState
*vc
)
339 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
350 int qemu_can_send_packet(VLANClientState
*vc1
)
352 VLANState
*vlan
= vc1
->vlan
;
355 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
357 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
364 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
366 VLANState
*vlan
= vc1
->vlan
;
371 printf("vlan %d send:\n", vlan
->id
);
372 hex_dump(stdout
, buf
, size
);
374 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
376 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
377 vc
->fd_read(vc
->opaque
, buf
, size
);
386 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
393 for (i
= 0; i
< iovcnt
; i
++) {
396 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
397 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
401 vc
->fd_read(vc
->opaque
, buffer
, offset
);
406 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
409 VLANState
*vlan
= vc1
->vlan
;
413 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
420 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
421 else if (vc
->fd_read
)
422 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
424 max_len
= MAX(max_len
, len
);
430 #if defined(CONFIG_SLIRP)
432 /* slirp network adapter */
434 static int slirp_inited
;
435 static VLANClientState
*slirp_vc
;
437 int slirp_can_output(void)
439 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
442 void slirp_output(const uint8_t *pkt
, int pkt_len
)
445 printf("slirp output:\n");
446 hex_dump(stdout
, pkt
, pkt_len
);
450 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
453 int slirp_is_inited(void)
458 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
461 printf("slirp input:\n");
462 hex_dump(stdout
, buf
, size
);
464 slirp_input(buf
, size
);
467 static int net_slirp_init(VLANState
*vlan
)
473 slirp_vc
= qemu_new_vlan_client(vlan
,
474 slirp_receive
, NULL
, NULL
);
475 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
479 void net_slirp_redir(const char *redir_str
)
484 struct in_addr guest_addr
;
485 int host_port
, guest_port
;
493 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
495 if (!strcmp(buf
, "tcp")) {
497 } else if (!strcmp(buf
, "udp")) {
503 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
505 host_port
= strtol(buf
, &r
, 0);
509 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
511 if (buf
[0] == '\0') {
512 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
514 if (!inet_aton(buf
, &guest_addr
))
517 guest_port
= strtol(p
, &r
, 0);
521 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
522 fprintf(stderr
, "qemu: could not set up redirection\n");
527 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
533 static char smb_dir
[1024];
535 static void erase_dir(char *dir_name
)
541 /* erase all the files in the directory */
542 if ((d
= opendir(dir_name
)) != 0) {
547 if (strcmp(de
->d_name
, ".") != 0 &&
548 strcmp(de
->d_name
, "..") != 0) {
549 snprintf(filename
, sizeof(filename
), "%s/%s",
550 smb_dir
, de
->d_name
);
551 if (unlink(filename
) != 0) /* is it a directory? */
560 /* automatic user mode samba server configuration */
561 static void smb_exit(void)
566 /* automatic user mode samba server configuration */
567 void net_slirp_smb(const char *exported_dir
)
570 char smb_cmdline
[1024];
578 /* XXX: better tmp dir construction */
579 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
580 if (mkdir(smb_dir
, 0700) < 0) {
581 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
584 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
586 f
= fopen(smb_conf
, "w");
588 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
595 "socket address=127.0.0.1\n"
597 "lock directory=%s\n"
598 "log file=%s/log.smbd\n"
599 "smb passwd file=%s/smbpasswd\n"
615 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
616 SMBD_COMMAND
, smb_conf
);
618 slirp_add_exec(0, smb_cmdline
, 4, 139);
621 #endif /* !defined(_WIN32) */
622 void do_info_slirp(void)
627 #endif /* CONFIG_SLIRP */
631 int tap_has_vnet_hdr(void *opaque
)
636 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
640 #else /* !defined(_WIN32) */
642 /* Maximum GSO packet size (64k) plus plenty of room for
643 * the ethernet and virtio_net headers
645 #define TAP_BUFSIZE (4096 + 65536)
648 #include <linux/virtio_net.h>
651 typedef struct TAPState
{
654 char down_script
[1024];
655 char buf
[TAP_BUFSIZE
];
657 unsigned int has_vnet_hdr
: 1;
658 unsigned int using_vnet_hdr
: 1;
661 static ssize_t
tap_writev(void *opaque
, const struct iovec
*iov
,
664 TAPState
*s
= opaque
;
668 len
= writev(s
->fd
, iov
, iovcnt
);
669 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
674 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
678 TAPState
*s
= opaque
;
680 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
681 struct iovec
*iov_copy
;
682 struct virtio_net_hdr hdr
= { 0, };
684 iov_copy
= alloca(sizeof(struct iovec
) * (iovcnt
+ 1));
686 iov_copy
[0].iov_base
= &hdr
;
687 iov_copy
[0].iov_len
= sizeof(hdr
);
689 memcpy(&iov_copy
[1], iov
, sizeof(struct iovec
) * iovcnt
);
691 return tap_writev(opaque
, iov_copy
, iovcnt
+ 1);
695 return tap_writev(opaque
, iov
, iovcnt
);
698 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
704 TAPState
*s
= opaque
;
705 struct virtio_net_hdr hdr
= { 0, };
707 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
708 iov
[i
].iov_base
= &hdr
;
709 iov
[i
].iov_len
= sizeof(hdr
);
714 iov
[i
].iov_base
= (char *) buf
;
715 iov
[i
].iov_len
= size
;
718 tap_writev(opaque
, iov
, i
);
721 static int tap_can_send(void *opaque
)
723 TAPState
*s
= opaque
;
727 /* Check to see if any of our clients can receive a packet */
728 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
733 if (!vc
->fd_can_read
) {
734 /* no fd_can_read handler, they always can receive */
737 can_receive
= vc
->fd_can_read(vc
->opaque
);
739 /* Once someone can receive, we try to send a packet */
747 static int tap_send_packet(TAPState
*s
)
749 uint8_t *buf
= s
->buf
;
753 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
754 buf
+= sizeof(struct virtio_net_hdr
);
755 size
-= sizeof(struct virtio_net_hdr
);
759 return qemu_send_packet(s
->vc
, buf
, size
);
762 static void tap_send(void *opaque
)
764 TAPState
*s
= opaque
;
766 /* First try to send any buffered packet */
770 /* If noone can receive the packet, buffer it */
771 err
= tap_send_packet(s
);
776 /* Read packets until we hit EAGAIN */
781 sbuf
.maxlen
= sizeof(s
->buf
);
783 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
786 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
790 if (s
->size
== -1 && errno
== EINTR
)
796 /* If noone can receive the packet, buffer it */
797 err
= tap_send_packet(s
);
801 } while (s
->size
> 0);
804 int tap_has_vnet_hdr(void *opaque
)
806 VLANClientState
*vc
= opaque
;
807 TAPState
*s
= vc
->opaque
;
809 return s
? s
->has_vnet_hdr
: 0;
812 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
814 VLANClientState
*vc
= opaque
;
815 TAPState
*s
= vc
->opaque
;
817 if (!s
|| !s
->has_vnet_hdr
)
820 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
823 static int tap_probe_vnet_hdr(int fd
)
825 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
828 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
829 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
833 return ifr
.ifr_flags
& IFF_VNET_HDR
;
840 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
843 TAPState
*s
= vc
->opaque
;
844 unsigned int offload
= 0;
847 offload
|= TUN_F_CSUM
;
849 offload
|= TUN_F_TSO4
;
851 offload
|= TUN_F_TSO6
;
852 if ((tso4
|| tso6
) && ecn
)
853 offload
|= TUN_F_TSO_ECN
;
856 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
857 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
860 #endif /* TUNSETOFFLOAD */
864 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
, int vnet_hdr
)
868 s
= qemu_mallocz(sizeof(TAPState
));
872 s
->has_vnet_hdr
= vnet_hdr
!= 0;
873 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
874 s
->vc
->fd_readv
= tap_receive_iov
;
876 s
->vc
->set_offload
= tap_set_offload
;
878 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
879 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
883 #if defined (_BSD) || defined (__FreeBSD_kernel__)
884 static int tap_open(char *ifname
, int ifname_size
)
890 TFR(fd
= open("/dev/tap", O_RDWR
));
892 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
897 dev
= devname(s
.st_rdev
, S_IFCHR
);
898 pstrcpy(ifname
, ifname_size
, dev
);
900 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
903 #elif defined(__sun__)
904 #define TUNNEWPPA (('T'<<16) | 0x0001)
906 * Allocate TAP device, returns opened fd.
907 * Stores dev name in the first arg(must be large enough).
909 int tap_alloc(char *dev
, size_t dev_size
)
911 int tap_fd
, if_fd
, ppa
= -1;
912 static int ip_fd
= 0;
915 static int arp_fd
= 0;
916 int ip_muxid
, arp_muxid
;
917 struct strioctl strioc_if
, strioc_ppa
;
918 int link_type
= I_PLINK
;;
920 char actual_name
[32] = "";
922 memset(&ifr
, 0x0, sizeof(ifr
));
926 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
930 /* Check if IP device was opened */
934 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
936 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
940 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
942 syslog(LOG_ERR
, "Can't open /dev/tap");
946 /* Assign a new PPA and get its unit number. */
947 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
948 strioc_ppa
.ic_timout
= 0;
949 strioc_ppa
.ic_len
= sizeof(ppa
);
950 strioc_ppa
.ic_dp
= (char *)&ppa
;
951 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
952 syslog (LOG_ERR
, "Can't assign new interface");
954 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
956 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
959 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
960 syslog(LOG_ERR
, "Can't push IP module");
964 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
965 syslog(LOG_ERR
, "Can't get flags\n");
967 snprintf (actual_name
, 32, "tap%d", ppa
);
968 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
971 /* Assign ppa according to the unit number returned by tun device */
973 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
974 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
975 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
976 syslog (LOG_ERR
, "Can't get flags\n");
977 /* Push arp module to if_fd */
978 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
979 syslog (LOG_ERR
, "Can't push ARP module (2)");
981 /* Push arp module to ip_fd */
982 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
983 syslog (LOG_ERR
, "I_POP failed\n");
984 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
985 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
987 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
989 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
991 /* Set ifname to arp */
992 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
993 strioc_if
.ic_timout
= 0;
994 strioc_if
.ic_len
= sizeof(ifr
);
995 strioc_if
.ic_dp
= (char *)&ifr
;
996 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
997 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1000 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1001 syslog(LOG_ERR
, "Can't link TAP device to IP");
1005 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1006 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1010 memset(&ifr
, 0x0, sizeof(ifr
));
1011 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1012 ifr
.lifr_ip_muxid
= ip_muxid
;
1013 ifr
.lifr_arp_muxid
= arp_muxid
;
1015 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1017 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1018 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1019 syslog (LOG_ERR
, "Can't set multiplexor id");
1022 snprintf(dev
, dev_size
, "tap%d", ppa
);
1026 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1030 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1031 fprintf(stderr
, "Cannot allocate TAP device\n");
1034 pstrcpy(ifname
, ifname_size
, dev
);
1035 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1039 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1044 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1046 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
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
) {
1059 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1064 if (ifname
[0] != '\0')
1065 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1067 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1068 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1070 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1074 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1075 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1080 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1086 /* try to launch network script */
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
&&
1099 *parg
++ = (char *)setup_script
;
1100 *parg
++ = (char *)ifname
;
1102 execv(setup_script
, args
);
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",
1116 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
1117 const char *setup_script
, const char *down_script
)
1124 if (ifname1
!= NULL
)
1125 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1129 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1133 if (!setup_script
|| !strcmp(setup_script
, "no"))
1135 if (setup_script
[0] != '\0') {
1136 if (launch_script(setup_script
, ifname
, fd
))
1139 s
= net_tap_fd_init(vlan
, fd
, vnet_hdr
);
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
);
1150 #endif /* !_WIN32 */
1152 #if defined(CONFIG_VDE)
1153 typedef struct VDEState
{
1154 VLANClientState
*vc
;
1158 static void vde_to_qemu(void *opaque
)
1160 VDEState
*s
= opaque
;
1164 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 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
;
1175 ret
= vde_send(s
->vde
, buf
, size
, 0);
1176 if (ret
< 0 && errno
== EINTR
) {
1183 static int net_vde_init(VLANState
*vlan
, const char *sock
, int port
,
1184 const char *group
, int mode
)
1187 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1188 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1190 struct vde_open_args args
= {
1192 .group
= init_group
,
1196 s
= qemu_mallocz(sizeof(VDEState
));
1199 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
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
));
1212 /* network connection */
1213 typedef struct NetSocketState
{
1214 VLANClientState
*vc
;
1216 int state
; /* 0 = getting length, 1 = getting data */
1220 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1223 typedef struct NetSocketListenState
{
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
;
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
;
1253 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1255 err
= socket_error();
1256 if (err
!= EWOULDBLOCK
)
1258 } else if (size
== 0) {
1259 /* end of connection */
1261 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1267 /* reassemble a packet from the network */
1273 memcpy(s
->buf
+ s
->index
, buf
, l
);
1277 if (s
->index
== 4) {
1279 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1285 l
= s
->packet_len
- s
->index
;
1288 memcpy(s
->buf
+ s
->index
, buf
, l
);
1292 if (s
->index
>= s
->packet_len
) {
1293 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1302 static void net_socket_send_dgram(void *opaque
)
1304 NetSocketState
*s
= opaque
;
1307 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1311 /* end of connection */
1312 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1315 qemu_send_packet(s
->vc
, s
->buf
, size
);
1318 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
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
));
1330 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1332 perror("socket(PF_INET, SOCK_DGRAM)");
1337 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1338 (const char *)&val
, sizeof(val
));
1340 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1344 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
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
));
1357 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1361 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1363 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1364 (const char *)&val
, sizeof(val
));
1366 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1370 socket_set_nonblock(fd
);
1378 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
1381 struct sockaddr_in saddr
;
1383 socklen_t saddr_len
;
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
1392 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1394 if (saddr
.sin_addr
.s_addr
==0) {
1395 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1399 /* clone dgram socket */
1400 newfd
= net_socket_mcast_create(&saddr
);
1402 /* error already reported by net_socket_mcast_create() */
1406 /* clone newfd to fd, close newfd */
1411 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1412 fd
, strerror(errno
));
1417 s
= qemu_mallocz(sizeof(NetSocketState
));
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
));
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
,
1445 s
= qemu_mallocz(sizeof(NetSocketState
));
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
);
1454 net_socket_connect(s
);
1456 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1461 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
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
);
1473 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
1475 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
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
);
1484 static void net_socket_accept(void *opaque
)
1486 NetSocketListenState
*s
= opaque
;
1488 struct sockaddr_in saddr
;
1493 len
= sizeof(saddr
);
1494 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1495 if (fd
< 0 && errno
!= EINTR
) {
1497 } else if (fd
>= 0) {
1501 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
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
;
1515 struct sockaddr_in saddr
;
1517 if (parse_host_port(&saddr
, host_str
) < 0)
1520 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1524 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1529 socket_set_nonblock(fd
);
1531 /* allow fast reuse */
1533 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1535 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1540 ret
= listen(fd
, 0);
1547 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1551 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
1554 int fd
, connected
, ret
, err
;
1555 struct sockaddr_in saddr
;
1557 if (parse_host_port(&saddr
, host_str
) < 0)
1560 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1565 socket_set_nonblock(fd
);
1569 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1571 err
= socket_error();
1572 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1573 } else if (err
== EINPROGRESS
) {
1576 } else if (err
== WSAEALREADY
) {
1589 s
= net_socket_fd_init(vlan
, fd
, connected
);
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
));
1598 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
1602 struct sockaddr_in saddr
;
1604 if (parse_host_port(&saddr
, host_str
) < 0)
1608 fd
= net_socket_mcast_create(&saddr
);
1612 s
= net_socket_fd_init(vlan
, fd
, 0);
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
));
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
) {
1633 vlan
= qemu_mallocz(sizeof(VLANState
));
1638 pvlan
= &first_vlan
;
1639 while (*pvlan
!= NULL
)
1640 pvlan
= &(*pvlan
)->next
;
1645 int net_client_init(const char *device
, const char *p
)
1652 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1653 vlan_id
= strtol(buf
, NULL
, 0);
1655 vlan
= qemu_find_vlan(vlan_id
);
1657 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1660 if (!strcmp(device
, "nic")) {
1664 if (nb_nics
>= MAX_NICS
) {
1665 fprintf(stderr
, "Too Many NICs\n");
1668 nd
= &nd_table
[nb_nics
];
1669 macaddr
= nd
->macaddr
;
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");
1683 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1684 nd
->model
= strdup(buf
);
1688 vlan
->nb_guest_devs
++;
1691 if (!strcmp(device
, "none")) {
1692 /* does nothing. It is needed to signal that no network cards
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
);
1706 if (!strcmp(device
, "tap")) {
1708 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1709 fprintf(stderr
, "tap: no interface name\n");
1712 vlan
->nb_host_devs
++;
1713 ret
= tap_win32_init(vlan
, ifname
);
1716 if (!strcmp(device
, "tap")) {
1718 char setup_script
[1024], down_script
[1024];
1720 vlan
->nb_host_devs
++;
1721 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1722 fd
= strtol(buf
, NULL
, 0);
1723 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1725 if (net_tap_fd_init(vlan
, fd
, tap_probe_vnet_hdr(fd
)))
1728 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1731 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1732 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1734 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1735 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1737 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
1741 if (!strcmp(device
, "socket")) {
1742 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1744 fd
= strtol(buf
, NULL
, 0);
1746 if (net_socket_fd_init(vlan
, fd
, 1))
1748 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1749 ret
= net_socket_listen_init(vlan
, buf
);
1750 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1751 ret
= net_socket_connect_init(vlan
, buf
);
1752 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1753 ret
= net_socket_mcast_init(vlan
, buf
);
1755 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1758 vlan
->nb_host_devs
++;
1761 if (!strcmp(device
, "vde")) {
1762 char vde_sock
[1024], vde_group
[512];
1763 int vde_port
, vde_mode
;
1764 vlan
->nb_host_devs
++;
1765 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1768 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1769 vde_port
= strtol(buf
, NULL
, 10);
1773 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1774 vde_group
[0] = '\0';
1776 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1777 vde_mode
= strtol(buf
, NULL
, 8);
1781 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1785 fprintf(stderr
, "Unknown network device: %s\n", device
);
1789 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1795 void net_client_uninit(NICInfo
*nd
)
1797 nd
->vlan
->nb_guest_devs
--; /* XXX: free vlan on last reference */
1800 free((void *)nd
->model
);
1803 int net_client_parse(const char *str
)
1811 while (*p
!= '\0' && *p
!= ',') {
1812 if ((q
- device
) < sizeof(device
) - 1)
1820 return net_client_init(device
, p
);
1823 void do_info_network(void)
1826 VLANClientState
*vc
;
1828 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1829 term_printf("VLAN %d devices:\n", vlan
->id
);
1830 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1831 term_printf(" %s\n", vc
->info_str
);
1835 void net_cleanup(void)
1839 #if !defined(_WIN32)
1840 /* close network clients */
1841 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1842 VLANClientState
*vc
;
1844 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1845 if (vc
->fd_read
== tap_receive
) {
1847 TAPState
*s
= vc
->opaque
;
1849 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
1851 launch_script(s
->down_script
, ifname
, s
->fd
);
1853 #if defined(CONFIG_VDE)
1854 if (vc
->fd_read
== vde_from_qemu
) {
1855 VDEState
*s
= vc
->opaque
;
1864 void net_client_check(void)
1868 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1869 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1871 if (vlan
->nb_guest_devs
== 0)
1872 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1873 if (vlan
->nb_host_devs
== 0)
1875 "Warning: vlan %d is not connected to host network\n",