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 // FIXME: #include "qemu-kvm.h"
125 static VLANState
*first_vlan
;
127 /***********************************************************/
128 /* network device redirectors */
130 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
131 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
135 for(i
=0;i
<size
;i
+=16) {
139 fprintf(f
, "%08x ", i
);
142 fprintf(f
, " %02x", buf
[i
+j
]);
149 if (c
< ' ' || c
> '~')
158 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
165 offset
= strtol(p
, &last_char
, 0);
166 if (0 == errno
&& '\0' == *last_char
&&
167 offset
>= 0 && offset
<= 0xFFFFFF) {
168 macaddr
[3] = (offset
& 0xFF0000) >> 16;
169 macaddr
[4] = (offset
& 0xFF00) >> 8;
170 macaddr
[5] = offset
& 0xFF;
173 for(i
= 0; i
< 6; i
++) {
174 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
179 if (*p
!= ':' && *p
!= '-')
190 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
201 if (len
> buf_size
- 1)
210 int parse_host_src_port(struct sockaddr_in
*haddr
,
211 struct sockaddr_in
*saddr
,
212 const char *input_str
)
214 char *str
= strdup(input_str
);
215 char *host_str
= str
;
217 const char *src_str2
;
221 * Chop off any extra arguments at the end of the string which
222 * would start with a comma, then fill in the src port information
223 * if it was provided else use the "any address" and "any port".
225 if ((ptr
= strchr(str
,',')))
228 if ((src_str
= strchr(input_str
,'@'))) {
233 if (parse_host_port(haddr
, host_str
) < 0)
237 if (!src_str
|| *src_str
== '\0')
240 if (parse_host_port(saddr
, src_str2
) < 0)
251 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
259 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
261 saddr
->sin_family
= AF_INET
;
262 if (buf
[0] == '\0') {
263 saddr
->sin_addr
.s_addr
= 0;
265 if (qemu_isdigit(buf
[0])) {
266 if (!inet_aton(buf
, &saddr
->sin_addr
))
269 if ((he
= gethostbyname(buf
)) == NULL
)
271 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
274 port
= strtol(p
, (char **)&r
, 0);
277 saddr
->sin_port
= htons(port
);
281 #if !defined(_WIN32) && 0
282 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
287 len
= MIN(108, strlen(str
));
288 p
= strchr(str
, ',');
290 len
= MIN(len
, p
- str
);
292 memset(uaddr
, 0, sizeof(*uaddr
));
294 uaddr
->sun_family
= AF_UNIX
;
295 memcpy(uaddr
->sun_path
, str
, len
);
301 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
303 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
304 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
306 macaddr
[0], macaddr
[1], macaddr
[2],
307 macaddr
[3], macaddr
[4], macaddr
[5]);
310 static char *assign_name(VLANClientState
*vc1
, const char *model
)
316 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
319 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
320 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
324 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
329 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
332 IOReadHandler
*fd_read
,
333 IOCanRWHandler
*fd_can_read
,
336 VLANClientState
*vc
, **pvc
;
337 vc
= qemu_mallocz(sizeof(VLANClientState
));
338 vc
->model
= strdup(model
);
340 vc
->name
= strdup(name
);
342 vc
->name
= assign_name(vc
, model
);
343 vc
->fd_read
= fd_read
;
344 vc
->fd_can_read
= fd_can_read
;
349 pvc
= &vlan
->first_client
;
356 void qemu_del_vlan_client(VLANClientState
*vc
)
358 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
371 int qemu_can_send_packet(VLANClientState
*vc1
)
373 VLANState
*vlan
= vc1
->vlan
;
376 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
378 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
385 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
387 VLANState
*vlan
= vc1
->vlan
;
395 printf("vlan %d send:\n", vlan
->id
);
396 hex_dump(stdout
, buf
, size
);
398 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
399 if (vc
!= vc1
&& !vc
->link_down
) {
400 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
401 vc
->fd_read(vc
->opaque
, buf
, size
);
409 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
412 uint8_t buffer
[4096];
416 for (i
= 0; i
< iovcnt
; i
++) {
419 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
420 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
424 vc
->fd_read(vc
->opaque
, buffer
, offset
);
429 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
434 for (i
= 0; i
< iovcnt
; i
++)
435 offset
+= iov
[i
].iov_len
;
439 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
442 VLANState
*vlan
= vc1
->vlan
;
447 return calc_iov_length(iov
, iovcnt
);
449 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
456 len
= calc_iov_length(iov
, iovcnt
);
458 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
459 else if (vc
->fd_read
)
460 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
462 max_len
= MAX(max_len
, len
);
468 #if defined(CONFIG_SLIRP)
470 /* slirp network adapter */
472 static int slirp_inited
;
473 static int slirp_restrict
;
474 static char *slirp_ip
;
475 static VLANClientState
*slirp_vc
;
477 int slirp_can_output(void)
479 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
482 void slirp_output(const uint8_t *pkt
, int pkt_len
)
485 printf("slirp output:\n");
486 hex_dump(stdout
, pkt
, pkt_len
);
490 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
493 int slirp_is_inited(void)
498 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
501 printf("slirp input:\n");
502 hex_dump(stdout
, buf
, size
);
504 slirp_input(buf
, size
);
507 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
511 slirp_init(slirp_restrict
, slirp_ip
);
513 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
514 slirp_receive
, NULL
, NULL
);
515 slirp_vc
->info_str
[0] = '\0';
519 void net_slirp_redir(const char *redir_str
)
524 struct in_addr guest_addr
;
525 int host_port
, guest_port
;
529 slirp_init(slirp_restrict
, slirp_ip
);
533 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
535 if (!strcmp(buf
, "tcp")) {
537 } else if (!strcmp(buf
, "udp")) {
543 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
545 host_port
= strtol(buf
, &r
, 0);
549 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
551 if (buf
[0] == '\0') {
552 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
554 if (!inet_aton(buf
, &guest_addr
))
557 guest_port
= strtol(p
, &r
, 0);
561 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
562 fprintf(stderr
, "qemu: could not set up redirection\n");
567 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
573 static char smb_dir
[1024];
575 static void erase_dir(char *dir_name
)
581 /* erase all the files in the directory */
582 if ((d
= opendir(dir_name
)) != 0) {
587 if (strcmp(de
->d_name
, ".") != 0 &&
588 strcmp(de
->d_name
, "..") != 0) {
589 snprintf(filename
, sizeof(filename
), "%s/%s",
590 smb_dir
, de
->d_name
);
591 if (unlink(filename
) != 0) /* is it a directory? */
600 /* automatic user mode samba server configuration */
601 static void smb_exit(void)
606 /* automatic user mode samba server configuration */
607 void net_slirp_smb(const char *exported_dir
)
610 char smb_cmdline
[1024];
615 slirp_init(slirp_restrict
, slirp_ip
);
618 /* XXX: better tmp dir construction */
619 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
620 if (mkdir(smb_dir
, 0700) < 0) {
621 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
624 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
626 f
= fopen(smb_conf
, "w");
628 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
635 "socket address=127.0.0.1\n"
637 "lock directory=%s\n"
638 "log file=%s/log.smbd\n"
639 "smb passwd file=%s/smbpasswd\n"
655 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
656 SMBD_COMMAND
, smb_conf
);
658 slirp_add_exec(0, smb_cmdline
, 4, 139);
661 #endif /* !defined(_WIN32) */
662 void do_info_slirp(void)
667 #endif /* CONFIG_SLIRP */
671 int tap_has_vnet_hdr(void *opaque
)
676 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
680 #else /* !defined(_WIN32) */
682 /* Maximum GSO packet size (64k) plus plenty of room for
683 * the ethernet and virtio_net headers
685 #define TAP_BUFSIZE (4096 + 65536)
688 #include <linux/virtio_net.h>
691 typedef struct TAPState
{
694 char down_script
[1024];
695 char down_script_arg
[128];
696 char buf
[TAP_BUFSIZE
];
698 unsigned int has_vnet_hdr
: 1;
699 unsigned int using_vnet_hdr
: 1;
703 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
706 TAPState
*s
= opaque
;
710 len
= writev(s
->fd
, iov
, iovcnt
);
711 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
717 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
723 TAPState
*s
= opaque
;
724 struct virtio_net_hdr hdr
= { 0, };
726 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
727 iov
[i
].iov_base
= &hdr
;
728 iov
[i
].iov_len
= sizeof(hdr
);
733 iov
[i
].iov_base
= (char *) buf
;
734 iov
[i
].iov_len
= size
;
737 tap_receive_iov(opaque
, iov
, i
);
740 static int tap_can_send(void *opaque
)
742 TAPState
*s
= opaque
;
746 /* Check to see if any of our clients can receive a packet */
747 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
752 if (!vc
->fd_can_read
) {
753 /* no fd_can_read handler, they always can receive */
756 can_receive
= vc
->fd_can_read(vc
->opaque
);
758 /* Once someone can receive, we try to send a packet */
766 static int tap_send_packet(TAPState
*s
)
768 uint8_t *buf
= (uint8_t *)s
->buf
;
772 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
773 buf
+= sizeof(struct virtio_net_hdr
);
774 size
-= sizeof(struct virtio_net_hdr
);
778 return qemu_send_packet(s
->vc
, buf
, size
);
781 static void tap_send(void *opaque
)
783 TAPState
*s
= opaque
;
785 /* First try to send any buffered packet */
789 /* If noone can receive the packet, buffer it */
790 err
= tap_send_packet(s
);
795 /* Read packets until we hit EAGAIN */
800 sbuf
.maxlen
= sizeof(s
->buf
);
802 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
804 // FIXME: kvm_sleep_begin();
805 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
806 // FIXME: kvm_sleep_end();
809 if (s
->size
== -1 && errno
== EINTR
)
815 /* If noone can receive the packet, buffer it */
816 err
= tap_send_packet(s
);
820 } while (s
->size
> 0);
823 int tap_has_vnet_hdr(void *opaque
)
825 VLANClientState
*vc
= opaque
;
826 TAPState
*s
= vc
->opaque
;
828 return s
? s
->has_vnet_hdr
: 0;
831 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
833 VLANClientState
*vc
= opaque
;
834 TAPState
*s
= vc
->opaque
;
836 if (!s
|| !s
->has_vnet_hdr
)
839 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
842 static int tap_probe_vnet_hdr(int fd
)
844 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
847 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
848 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
852 return ifr
.ifr_flags
& IFF_VNET_HDR
;
859 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
862 TAPState
*s
= vc
->opaque
;
863 unsigned int offload
= 0;
866 offload
|= TUN_F_CSUM
;
868 offload
|= TUN_F_TSO4
;
870 offload
|= TUN_F_TSO6
;
871 if ((tso4
|| tso6
) && ecn
)
872 offload
|= TUN_F_TSO_ECN
;
875 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
876 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
879 #endif /* TUNSETOFFLOAD */
883 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
891 s
= qemu_mallocz(sizeof(TAPState
));
893 s
->has_vnet_hdr
= vnet_hdr
!= 0;
894 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
896 s
->vc
->fd_readv
= tap_receive_iov
;
899 s
->vc
->set_offload
= tap_set_offload
;
901 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
902 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
906 #if defined (_BSD) || defined (__FreeBSD_kernel__)
907 static int tap_open(char *ifname
, int ifname_size
)
913 TFR(fd
= open("/dev/tap", O_RDWR
));
915 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
920 dev
= devname(s
.st_rdev
, S_IFCHR
);
921 pstrcpy(ifname
, ifname_size
, dev
);
923 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
926 #elif defined(__sun__)
927 #define TUNNEWPPA (('T'<<16) | 0x0001)
929 * Allocate TAP device, returns opened fd.
930 * Stores dev name in the first arg(must be large enough).
932 int tap_alloc(char *dev
, size_t dev_size
)
934 int tap_fd
, if_fd
, ppa
= -1;
935 static int ip_fd
= 0;
938 static int arp_fd
= 0;
939 int ip_muxid
, arp_muxid
;
940 struct strioctl strioc_if
, strioc_ppa
;
941 int link_type
= I_PLINK
;;
943 char actual_name
[32] = "";
945 memset(&ifr
, 0x0, sizeof(ifr
));
949 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
953 /* Check if IP device was opened */
957 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
959 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
963 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
965 syslog(LOG_ERR
, "Can't open /dev/tap");
969 /* Assign a new PPA and get its unit number. */
970 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
971 strioc_ppa
.ic_timout
= 0;
972 strioc_ppa
.ic_len
= sizeof(ppa
);
973 strioc_ppa
.ic_dp
= (char *)&ppa
;
974 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
975 syslog (LOG_ERR
, "Can't assign new interface");
977 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
979 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
982 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
983 syslog(LOG_ERR
, "Can't push IP module");
987 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
988 syslog(LOG_ERR
, "Can't get flags\n");
990 snprintf (actual_name
, 32, "tap%d", ppa
);
991 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
994 /* Assign ppa according to the unit number returned by tun device */
996 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
997 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
998 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
999 syslog (LOG_ERR
, "Can't get flags\n");
1000 /* Push arp module to if_fd */
1001 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1002 syslog (LOG_ERR
, "Can't push ARP module (2)");
1004 /* Push arp module to ip_fd */
1005 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1006 syslog (LOG_ERR
, "I_POP failed\n");
1007 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1008 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1010 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1012 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1014 /* Set ifname to arp */
1015 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1016 strioc_if
.ic_timout
= 0;
1017 strioc_if
.ic_len
= sizeof(ifr
);
1018 strioc_if
.ic_dp
= (char *)&ifr
;
1019 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1020 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1023 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1024 syslog(LOG_ERR
, "Can't link TAP device to IP");
1028 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1029 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1033 memset(&ifr
, 0x0, sizeof(ifr
));
1034 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1035 ifr
.lifr_ip_muxid
= ip_muxid
;
1036 ifr
.lifr_arp_muxid
= arp_muxid
;
1038 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1040 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1041 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1042 syslog (LOG_ERR
, "Can't set multiplexor id");
1045 snprintf(dev
, dev_size
, "tap%d", ppa
);
1049 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1053 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1054 fprintf(stderr
, "Cannot allocate TAP device\n");
1057 pstrcpy(ifname
, ifname_size
, dev
);
1058 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1061 #elif defined (_AIX)
1062 static int tap_open(char *ifname
, int ifname_size
)
1064 fprintf (stderr
, "no tap on AIX\n");
1068 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1073 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1075 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1078 memset(&ifr
, 0, sizeof(ifr
));
1079 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1081 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1083 unsigned int features
;
1085 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1086 features
& IFF_VNET_HDR
) {
1088 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1093 if (ifname
[0] != '\0')
1094 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1096 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1097 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1099 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1103 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1104 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1109 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1115 /* try to launch network script */
1119 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1120 for (i
= 0; i
< open_max
; i
++)
1121 if (i
!= STDIN_FILENO
&&
1122 i
!= STDOUT_FILENO
&&
1123 i
!= STDERR_FILENO
&&
1128 *parg
++ = (char *)setup_script
;
1129 *parg
++ = (char *)ifname
;
1131 execv(setup_script
, args
);
1134 while (waitpid(pid
, &status
, 0) != pid
);
1135 if (!WIFEXITED(status
) ||
1136 WEXITSTATUS(status
) != 0) {
1137 fprintf(stderr
, "%s: could not launch network script\n",
1145 static int net_tap_init(VLANState
*vlan
, const char *model
,
1146 const char *name
, const char *ifname1
,
1147 const char *setup_script
, const char *down_script
)
1154 if (ifname1
!= NULL
)
1155 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1159 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1163 if (!setup_script
|| !strcmp(setup_script
, "no"))
1165 if (setup_script
[0] != '\0') {
1166 if (launch_script(setup_script
, ifname
, fd
))
1169 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1173 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1174 "ifname=%s,script=%s,downscript=%s",
1175 ifname
, setup_script
, down_script
);
1176 if (down_script
&& strcmp(down_script
, "no")) {
1177 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1178 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1183 #endif /* !_WIN32 */
1185 #if defined(CONFIG_VDE)
1186 typedef struct VDEState
{
1187 VLANClientState
*vc
;
1191 static void vde_to_qemu(void *opaque
)
1193 VDEState
*s
= opaque
;
1197 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1199 qemu_send_packet(s
->vc
, buf
, size
);
1203 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1205 VDEState
*s
= opaque
;
1208 ret
= vde_send(s
->vde
, buf
, size
, 0);
1209 if (ret
< 0 && errno
== EINTR
) {
1216 static int net_vde_init(VLANState
*vlan
, const char *model
,
1217 const char *name
, const char *sock
,
1218 int port
, const char *group
, int mode
)
1221 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1222 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1224 struct vde_open_args args
= {
1226 .group
= init_group
,
1230 s
= qemu_mallocz(sizeof(VDEState
));
1231 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1236 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1237 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1238 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1239 sock
, vde_datafd(s
->vde
));
1244 /* network connection */
1245 typedef struct NetSocketState
{
1246 VLANClientState
*vc
;
1248 int state
; /* 0 = getting length, 1 = getting data */
1252 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1255 typedef struct NetSocketListenState
{
1260 } NetSocketListenState
;
1262 /* XXX: we consider we can send the whole packet without blocking */
1263 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1265 NetSocketState
*s
= opaque
;
1269 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1270 send_all(s
->fd
, buf
, size
);
1273 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1275 NetSocketState
*s
= opaque
;
1276 sendto(s
->fd
, buf
, size
, 0,
1277 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1280 static void net_socket_send(void *opaque
)
1282 NetSocketState
*s
= opaque
;
1287 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1289 err
= socket_error();
1290 if (err
!= EWOULDBLOCK
)
1292 } else if (size
== 0) {
1293 /* end of connection */
1295 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1301 /* reassemble a packet from the network */
1307 memcpy(s
->buf
+ s
->index
, buf
, l
);
1311 if (s
->index
== 4) {
1313 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1319 l
= s
->packet_len
- s
->index
;
1322 memcpy(s
->buf
+ s
->index
, buf
, l
);
1326 if (s
->index
>= s
->packet_len
) {
1327 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1336 static void net_socket_send_dgram(void *opaque
)
1338 NetSocketState
*s
= opaque
;
1341 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1345 /* end of connection */
1346 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1349 qemu_send_packet(s
->vc
, s
->buf
, size
);
1352 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1357 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1358 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1359 inet_ntoa(mcastaddr
->sin_addr
),
1360 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1364 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1366 perror("socket(PF_INET, SOCK_DGRAM)");
1371 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1372 (const char *)&val
, sizeof(val
));
1374 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1378 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1384 /* Add host to multicast group */
1385 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1386 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1388 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1389 (const char *)&imr
, sizeof(struct ip_mreq
));
1391 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1395 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1397 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1398 (const char *)&val
, sizeof(val
));
1400 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1404 socket_set_nonblock(fd
);
1412 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1415 int fd
, int is_connected
)
1417 struct sockaddr_in saddr
;
1419 socklen_t saddr_len
;
1422 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1423 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1424 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1428 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1430 if (saddr
.sin_addr
.s_addr
==0) {
1431 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1435 /* clone dgram socket */
1436 newfd
= net_socket_mcast_create(&saddr
);
1438 /* error already reported by net_socket_mcast_create() */
1442 /* clone newfd to fd, close newfd */
1447 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1448 fd
, strerror(errno
));
1453 s
= qemu_mallocz(sizeof(NetSocketState
));
1456 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1457 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1459 /* mcast: save bound address as dst */
1460 if (is_connected
) s
->dgram_dst
=saddr
;
1462 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1463 "socket: fd=%d (%s mcast=%s:%d)",
1464 fd
, is_connected
? "cloned" : "",
1465 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1469 static void net_socket_connect(void *opaque
)
1471 NetSocketState
*s
= opaque
;
1472 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1475 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1478 int fd
, int is_connected
)
1481 s
= qemu_mallocz(sizeof(NetSocketState
));
1483 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1484 net_socket_receive
, NULL
, s
);
1485 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1486 "socket: fd=%d", fd
);
1488 net_socket_connect(s
);
1490 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1495 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1496 const char *model
, const char *name
,
1497 int fd
, int is_connected
)
1499 int so_type
=-1, optlen
=sizeof(so_type
);
1501 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1502 (socklen_t
*)&optlen
)< 0) {
1503 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1508 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1510 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1512 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1513 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1514 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1519 static void net_socket_accept(void *opaque
)
1521 NetSocketListenState
*s
= opaque
;
1523 struct sockaddr_in saddr
;
1528 len
= sizeof(saddr
);
1529 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1530 if (fd
< 0 && errno
!= EINTR
) {
1532 } else if (fd
>= 0) {
1536 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1540 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1541 "socket: connection from %s:%d",
1542 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1546 static int net_socket_listen_init(VLANState
*vlan
,
1549 const char *host_str
)
1551 NetSocketListenState
*s
;
1553 struct sockaddr_in saddr
;
1555 if (parse_host_port(&saddr
, host_str
) < 0)
1558 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1560 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1565 socket_set_nonblock(fd
);
1567 /* allow fast reuse */
1569 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1571 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1576 ret
= listen(fd
, 0);
1582 s
->model
= strdup(model
);
1583 s
->name
= strdup(name
);
1585 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1589 static int net_socket_connect_init(VLANState
*vlan
,
1592 const char *host_str
)
1595 int fd
, connected
, ret
, err
;
1596 struct sockaddr_in saddr
;
1598 if (parse_host_port(&saddr
, host_str
) < 0)
1601 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1606 socket_set_nonblock(fd
);
1610 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1612 err
= socket_error();
1613 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1614 } else if (err
== EINPROGRESS
) {
1617 } else if (err
== WSAEALREADY
) {
1630 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1633 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1634 "socket: connect to %s:%d",
1635 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1639 static int net_socket_mcast_init(VLANState
*vlan
,
1642 const char *host_str
)
1646 struct sockaddr_in saddr
;
1648 if (parse_host_port(&saddr
, host_str
) < 0)
1652 fd
= net_socket_mcast_create(&saddr
);
1656 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1660 s
->dgram_dst
= saddr
;
1662 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1663 "socket: mcast=%s:%d",
1664 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1669 /* find or alloc a new VLAN */
1670 VLANState
*qemu_find_vlan(int id
)
1672 VLANState
**pvlan
, *vlan
;
1673 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1677 vlan
= qemu_mallocz(sizeof(VLANState
));
1680 pvlan
= &first_vlan
;
1681 while (*pvlan
!= NULL
)
1682 pvlan
= &(*pvlan
)->next
;
1687 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1689 const char *models
[2];
1694 qemu_check_nic_model_list(nd
, models
, model
);
1697 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1698 const char *default_model
)
1700 int i
, exit_status
= 0;
1703 nd
->model
= strdup(default_model
);
1705 if (strcmp(nd
->model
, "?") != 0) {
1706 for (i
= 0 ; models
[i
]; i
++)
1707 if (strcmp(nd
->model
, models
[i
]) == 0)
1710 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1714 fprintf(stderr
, "qemu: Supported NIC models: ");
1715 for (i
= 0 ; models
[i
]; i
++)
1716 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1721 int net_client_init(const char *device
, const char *p
)
1729 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1730 vlan_id
= strtol(buf
, NULL
, 0);
1732 vlan
= qemu_find_vlan(vlan_id
);
1734 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1737 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1740 if (!strcmp(device
, "nic")) {
1744 if (nb_nics
>= MAX_NICS
) {
1745 fprintf(stderr
, "Too Many NICs\n");
1748 nd
= &nd_table
[nb_nics
];
1749 macaddr
= nd
->macaddr
;
1755 macaddr
[5] = 0x56 + nb_nics
;
1757 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1758 if (parse_macaddr(macaddr
, buf
) < 0) {
1759 fprintf(stderr
, "invalid syntax for ethernet address\n");
1763 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1764 nd
->model
= strdup(buf
);
1770 vlan
->nb_guest_devs
++;
1773 if (!strcmp(device
, "none")) {
1774 /* does nothing. It is needed to signal that no network cards
1779 if (!strcmp(device
, "user")) {
1780 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1781 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1783 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1784 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1786 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1787 slirp_ip
= strdup(buf
);
1789 vlan
->nb_host_devs
++;
1790 ret
= net_slirp_init(vlan
, device
, name
);
1794 if (!strcmp(device
, "tap")) {
1796 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1797 fprintf(stderr
, "tap: no interface name\n");
1800 vlan
->nb_host_devs
++;
1801 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1803 #elif defined (_AIX)
1805 if (!strcmp(device
, "tap")) {
1807 char setup_script
[1024], down_script
[1024];
1809 vlan
->nb_host_devs
++;
1810 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1811 fd
= strtol(buf
, NULL
, 0);
1812 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1814 if (net_tap_fd_init(vlan
, device
, name
, fd
,
1815 tap_probe_vnet_hdr(fd
)))
1818 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1821 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1822 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1824 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1825 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1827 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1831 if (!strcmp(device
, "socket")) {
1832 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1834 fd
= strtol(buf
, NULL
, 0);
1836 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1838 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1839 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1840 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1841 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1842 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1843 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1845 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1848 vlan
->nb_host_devs
++;
1851 if (!strcmp(device
, "vde")) {
1852 char vde_sock
[1024], vde_group
[512];
1853 int vde_port
, vde_mode
;
1854 vlan
->nb_host_devs
++;
1855 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1858 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1859 vde_port
= strtol(buf
, NULL
, 10);
1863 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1864 vde_group
[0] = '\0';
1866 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1867 vde_mode
= strtol(buf
, NULL
, 8);
1871 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1875 fprintf(stderr
, "Unknown network device: %s\n", device
);
1881 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1888 void net_client_uninit(NICInfo
*nd
)
1890 nd
->vlan
->nb_guest_devs
--; /* XXX: free vlan on last reference */
1893 free((void *)nd
->model
);
1896 int net_client_parse(const char *str
)
1904 while (*p
!= '\0' && *p
!= ',') {
1905 if ((q
- device
) < sizeof(device
) - 1)
1913 return net_client_init(device
, p
);
1916 void do_info_network(void)
1919 VLANClientState
*vc
;
1921 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1922 term_printf("VLAN %d devices:\n", vlan
->id
);
1923 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1924 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
1928 int do_set_link(const char *name
, const char *up_or_down
)
1931 VLANClientState
*vc
= NULL
;
1933 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
1934 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1935 if (strcmp(vc
->name
, name
) == 0)
1940 term_printf("could not find network device '%s'", name
);
1944 if (strcmp(up_or_down
, "up") == 0)
1946 else if (strcmp(up_or_down
, "down") == 0)
1949 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
1952 if (vc
->link_status_changed
)
1953 vc
->link_status_changed(vc
);
1958 void net_cleanup(void)
1962 #if !defined(_WIN32)
1963 /* close network clients */
1964 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1965 VLANClientState
*vc
;
1967 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1968 if (vc
->fd_read
== tap_receive
) {
1969 TAPState
*s
= vc
->opaque
;
1971 if (s
->down_script
[0])
1972 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1974 #if defined(CONFIG_VDE)
1975 if (vc
->fd_read
== vde_from_qemu
) {
1976 VDEState
*s
= vc
->opaque
;
1985 void net_client_check(void)
1989 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1990 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1992 if (vlan
->nb_guest_devs
== 0)
1993 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1994 if (vlan
->nb_host_devs
== 0)
1996 "Warning: vlan %d is not connected to host network\n",