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
24 #include "qemu-common.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 #include <net/if_tap.h>
54 #include <linux/if_tun.h>
56 #include <arpa/inet.h>
59 #include <sys/select.h>
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.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> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.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>
101 #include "qemu_socket.h"
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
107 #if defined(__OpenBSD__)
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
123 #include "qemu-kvm.h"
125 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
126 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
128 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
130 #define SMBD_COMMAND "/usr/sbin/smbd"
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
)
143 for(i
=0;i
<size
;i
+=16) {
147 fprintf(f
, "%08x ", i
);
150 fprintf(f
, " %02x", buf
[i
+j
]);
157 if (c
< ' ' || c
> '~')
166 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
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;
181 for(i
= 0; i
< 6; i
++) {
182 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
187 if (*p
!= ':' && *p
!= '-')
198 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
209 if (len
> buf_size
- 1)
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
;
225 const char *src_str2
;
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
,',')))
236 if ((src_str
= strchr(input_str
,'@'))) {
241 if (parse_host_port(haddr
, host_str
) < 0)
245 if (!src_str
|| *src_str
== '\0')
248 if (parse_host_port(saddr
, src_str2
) < 0)
259 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
267 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
269 saddr
->sin_family
= AF_INET
;
270 if (buf
[0] == '\0') {
271 saddr
->sin_addr
.s_addr
= 0;
273 if (qemu_isdigit(buf
[0])) {
274 if (!inet_aton(buf
, &saddr
->sin_addr
))
277 if ((he
= gethostbyname(buf
)) == NULL
)
279 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
282 port
= strtol(p
, (char **)&r
, 0);
285 saddr
->sin_port
= htons(port
);
290 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
295 len
= MIN(108, strlen(str
));
296 p
= strchr(str
, ',');
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
);
309 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
310 IOReadHandler
*fd_read
,
311 IOCanRWHandler
*fd_can_read
,
314 VLANClientState
*vc
, **pvc
;
315 vc
= qemu_mallocz(sizeof(VLANClientState
));
318 vc
->fd_read
= fd_read
;
319 vc
->fd_can_read
= fd_can_read
;
324 pvc
= &vlan
->first_client
;
331 void qemu_del_vlan_client(VLANClientState
*vc
)
333 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
344 int qemu_can_send_packet(VLANClientState
*vc1
)
346 VLANState
*vlan
= vc1
->vlan
;
349 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
351 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
358 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
360 VLANState
*vlan
= vc1
->vlan
;
365 printf("vlan %d send:\n", vlan
->id
);
366 hex_dump(stdout
, buf
, size
);
368 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
370 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
371 vc
->fd_read(vc
->opaque
, buf
, size
);
380 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
387 for (i
= 0; i
< iovcnt
; i
++) {
390 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
391 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
395 vc
->fd_read(vc
->opaque
, buffer
, offset
);
400 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
403 VLANState
*vlan
= vc1
->vlan
;
407 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
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
);
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
)
439 printf("slirp output:\n");
440 hex_dump(stdout
, pkt
, pkt_len
);
444 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
447 int slirp_is_inited(void)
452 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
455 printf("slirp input:\n");
456 hex_dump(stdout
, buf
, size
);
458 slirp_input(buf
, size
);
461 static int net_slirp_init(VLANState
*vlan
)
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");
473 void net_slirp_redir(const char *redir_str
)
478 struct in_addr guest_addr
;
479 int host_port
, guest_port
;
487 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
489 if (!strcmp(buf
, "tcp")) {
491 } else if (!strcmp(buf
, "udp")) {
497 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
499 host_port
= strtol(buf
, &r
, 0);
503 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
505 if (buf
[0] == '\0') {
506 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
508 if (!inet_aton(buf
, &guest_addr
))
511 guest_port
= strtol(p
, &r
, 0);
515 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
516 fprintf(stderr
, "qemu: could not set up redirection\n");
521 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
527 static char smb_dir
[1024];
529 static void erase_dir(char *dir_name
)
535 /* erase all the files in the directory */
536 if ((d
= opendir(dir_name
)) != 0) {
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? */
554 /* automatic user mode samba server configuration */
555 static void smb_exit(void)
560 /* automatic user mode samba server configuration */
561 void net_slirp_smb(const char *exported_dir
)
564 char smb_cmdline
[1024];
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
);
578 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
580 f
= fopen(smb_conf
, "w");
582 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
589 "socket address=127.0.0.1\n"
591 "lock directory=%s\n"
592 "log file=%s/log.smbd\n"
593 "smb passwd file=%s/smbpasswd\n"
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)
621 #endif /* CONFIG_SLIRP */
625 int tap_has_vnet_hdr(void *opaque
)
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)
642 #include <linux/virtio_net.h>
645 typedef struct TAPState
{
648 char down_script
[1024];
649 char buf
[TAP_BUFSIZE
];
651 unsigned int has_vnet_hdr
: 1;
652 unsigned int using_vnet_hdr
: 1;
655 static ssize_t
tap_writev(void *opaque
, const struct iovec
*iov
,
658 TAPState
*s
= opaque
;
662 len
= writev(s
->fd
, iov
, iovcnt
);
663 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
668 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
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);
689 return tap_writev(opaque
, iov
, iovcnt
);
692 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
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
);
708 iov
[i
].iov_base
= (char *) buf
;
709 iov
[i
].iov_len
= size
;
712 tap_writev(opaque
, iov
, i
);
715 static int tap_can_send(void *opaque
)
717 TAPState
*s
= opaque
;
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
) {
727 if (!vc
->fd_can_read
) {
728 /* no fd_can_read handler, they always can receive */
731 can_receive
= vc
->fd_can_read(vc
->opaque
);
733 /* Once someone can receive, we try to send a packet */
741 static int tap_send_packet(TAPState
*s
)
743 uint8_t *buf
= s
->buf
;
747 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
748 buf
+= sizeof(struct virtio_net_hdr
);
749 size
-= sizeof(struct virtio_net_hdr
);
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 */
764 /* If noone can receive the packet, buffer it */
765 err
= tap_send_packet(s
);
770 /* Read packets until we hit EAGAIN */
775 sbuf
.maxlen
= sizeof(s
->buf
);
777 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
780 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
784 if (s
->size
== -1 && errno
== EINTR
)
790 /* If noone can receive the packet, buffer it */
791 err
= tap_send_packet(s
);
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
)
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)
822 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
823 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
827 return ifr
.ifr_flags
& IFF_VNET_HDR
;
834 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
837 TAPState
*s
= vc
->opaque
;
838 unsigned int offload
= 0;
841 offload
|= TUN_F_CSUM
;
843 offload
|= TUN_F_TSO4
;
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",
854 #endif /* TUNSETOFFLOAD */
858 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
, int vnet_hdr
)
862 s
= qemu_mallocz(sizeof(TAPState
));
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
;
870 s
->vc
->set_offload
= tap_set_offload
;
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
);
877 #if defined (_BSD) || defined (__FreeBSD_kernel__)
878 static int tap_open(char *ifname
, int ifname_size
)
884 TFR(fd
= open("/dev/tap", O_RDWR
));
886 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
891 dev
= devname(s
.st_rdev
, S_IFCHR
);
892 pstrcpy(ifname
, ifname_size
, dev
);
894 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
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;
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
;;
914 char actual_name
[32] = "";
916 memset(&ifr
, 0x0, sizeof(ifr
));
920 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
924 /* Check if IP device was opened */
928 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
930 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
934 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
936 syslog(LOG_ERR
, "Can't open /dev/tap");
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));
950 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
953 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
954 syslog(LOG_ERR
, "Can't push IP module");
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
);
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");
981 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 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");
999 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1000 syslog (LOG_ERR
, "Can't link TAP device to ARP");
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
);
1020 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1024 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1025 fprintf(stderr
, "Cannot allocate TAP device\n");
1028 pstrcpy(ifname
, ifname_size
, dev
);
1029 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1032 #elif defined (_AIX)
1033 static int tap_open(char *ifname
, int ifname_size
)
1035 fprintf (stderr
, "no tap on AIX\n");
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
);
1715 #elif defined (_AIX)
1717 if (!strcmp(device
, "tap")) {
1719 char setup_script
[1024], down_script
[1024];
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
);
1726 if (net_tap_fd_init(vlan
, fd
, tap_probe_vnet_hdr(fd
)))
1729 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 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
);
1742 if (!strcmp(device
, "socket")) {
1743 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1745 fd
= strtol(buf
, NULL
, 0);
1747 if (net_socket_fd_init(vlan
, fd
, 1))
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
);
1756 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1759 vlan
->nb_host_devs
++;
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) {
1769 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1770 vde_port
= strtol(buf
, NULL
, 10);
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);
1782 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1786 fprintf(stderr
, "Unknown network device: %s\n", device
);
1790 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1796 void net_client_uninit(NICInfo
*nd
)
1798 nd
->vlan
->nb_guest_devs
--; /* XXX: free vlan on last reference */
1801 free((void *)nd
->model
);
1804 int net_client_parse(const char *str
)
1812 while (*p
!= '\0' && *p
!= ',') {
1813 if ((q
- device
) < sizeof(device
) - 1)
1821 return net_client_init(device
, p
);
1824 void do_info_network(void)
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)
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
) {
1848 TAPState
*s
= vc
->opaque
;
1850 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
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
;
1865 void net_client_check(void)
1869 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1870 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
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)
1876 "Warning: vlan %d is not connected to host network\n",