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
));
340 vc
->model
= strdup(model
);
342 vc
->name
= strdup(name
);
344 vc
->name
= assign_name(vc
, model
);
345 vc
->fd_read
= fd_read
;
346 vc
->fd_can_read
= fd_can_read
;
351 pvc
= &vlan
->first_client
;
358 void qemu_del_vlan_client(VLANClientState
*vc
)
360 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
373 int qemu_can_send_packet(VLANClientState
*vc1
)
375 VLANState
*vlan
= vc1
->vlan
;
378 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
380 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
387 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
389 VLANState
*vlan
= vc1
->vlan
;
397 printf("vlan %d send:\n", vlan
->id
);
398 hex_dump(stdout
, buf
, size
);
400 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
401 if (vc
!= vc1
&& !vc
->link_down
) {
402 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
403 vc
->fd_read(vc
->opaque
, buf
, size
);
411 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
414 uint8_t buffer
[4096];
418 for (i
= 0; i
< iovcnt
; i
++) {
421 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
422 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
426 vc
->fd_read(vc
->opaque
, buffer
, offset
);
431 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
436 for (i
= 0; i
< iovcnt
; i
++)
437 offset
+= iov
[i
].iov_len
;
441 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
444 VLANState
*vlan
= vc1
->vlan
;
449 return calc_iov_length(iov
, iovcnt
);
451 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
458 len
= calc_iov_length(iov
, iovcnt
);
460 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
461 else if (vc
->fd_read
)
462 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
464 max_len
= MAX(max_len
, len
);
470 #if defined(CONFIG_SLIRP)
472 /* slirp network adapter */
474 static int slirp_inited
;
475 static int slirp_restrict
;
476 static char *slirp_ip
;
477 static VLANClientState
*slirp_vc
;
479 int slirp_can_output(void)
481 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
484 void slirp_output(const uint8_t *pkt
, int pkt_len
)
487 printf("slirp output:\n");
488 hex_dump(stdout
, pkt
, pkt_len
);
492 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
495 int slirp_is_inited(void)
500 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
503 printf("slirp input:\n");
504 hex_dump(stdout
, buf
, size
);
506 slirp_input(buf
, size
);
509 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
513 slirp_init(slirp_restrict
, slirp_ip
);
515 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
516 slirp_receive
, NULL
, NULL
);
517 slirp_vc
->info_str
[0] = '\0';
521 void net_slirp_redir(const char *redir_str
)
526 struct in_addr guest_addr
;
527 int host_port
, guest_port
;
531 slirp_init(slirp_restrict
, slirp_ip
);
535 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
537 if (!strcmp(buf
, "tcp")) {
539 } else if (!strcmp(buf
, "udp")) {
545 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
547 host_port
= strtol(buf
, &r
, 0);
551 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
553 if (buf
[0] == '\0') {
554 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
556 if (!inet_aton(buf
, &guest_addr
))
559 guest_port
= strtol(p
, &r
, 0);
563 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
564 fprintf(stderr
, "qemu: could not set up redirection\n");
569 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
575 static char smb_dir
[1024];
577 static void erase_dir(char *dir_name
)
583 /* erase all the files in the directory */
584 if ((d
= opendir(dir_name
)) != 0) {
589 if (strcmp(de
->d_name
, ".") != 0 &&
590 strcmp(de
->d_name
, "..") != 0) {
591 snprintf(filename
, sizeof(filename
), "%s/%s",
592 smb_dir
, de
->d_name
);
593 if (unlink(filename
) != 0) /* is it a directory? */
602 /* automatic user mode samba server configuration */
603 static void smb_exit(void)
608 /* automatic user mode samba server configuration */
609 void net_slirp_smb(const char *exported_dir
)
612 char smb_cmdline
[1024];
617 slirp_init(slirp_restrict
, slirp_ip
);
620 /* XXX: better tmp dir construction */
621 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
622 if (mkdir(smb_dir
, 0700) < 0) {
623 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
626 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
628 f
= fopen(smb_conf
, "w");
630 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
637 "socket address=127.0.0.1\n"
639 "lock directory=%s\n"
640 "log file=%s/log.smbd\n"
641 "smb passwd file=%s/smbpasswd\n"
657 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
658 SMBD_COMMAND
, smb_conf
);
660 slirp_add_exec(0, smb_cmdline
, 4, 139);
663 #endif /* !defined(_WIN32) */
664 void do_info_slirp(void)
669 #endif /* CONFIG_SLIRP */
673 int tap_has_vnet_hdr(void *opaque
)
678 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
682 #else /* !defined(_WIN32) */
684 /* Maximum GSO packet size (64k) plus plenty of room for
685 * the ethernet and virtio_net headers
687 #define TAP_BUFSIZE (4096 + 65536)
690 #include <linux/virtio_net.h>
693 typedef struct TAPState
{
696 char down_script
[1024];
697 char down_script_arg
[128];
698 char buf
[TAP_BUFSIZE
];
700 unsigned int has_vnet_hdr
: 1;
701 unsigned int using_vnet_hdr
: 1;
705 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
708 TAPState
*s
= opaque
;
712 len
= writev(s
->fd
, iov
, iovcnt
);
713 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
719 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
725 TAPState
*s
= opaque
;
726 struct virtio_net_hdr hdr
= { 0, };
728 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
729 iov
[i
].iov_base
= &hdr
;
730 iov
[i
].iov_len
= sizeof(hdr
);
735 iov
[i
].iov_base
= (char *) buf
;
736 iov
[i
].iov_len
= size
;
739 tap_receive_iov(opaque
, iov
, i
);
742 static int tap_can_send(void *opaque
)
744 TAPState
*s
= opaque
;
748 /* Check to see if any of our clients can receive a packet */
749 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
754 if (!vc
->fd_can_read
) {
755 /* no fd_can_read handler, they always can receive */
758 can_receive
= vc
->fd_can_read(vc
->opaque
);
760 /* Once someone can receive, we try to send a packet */
768 static int tap_send_packet(TAPState
*s
)
770 uint8_t *buf
= s
->buf
;
774 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
775 buf
+= sizeof(struct virtio_net_hdr
);
776 size
-= sizeof(struct virtio_net_hdr
);
780 return qemu_send_packet(s
->vc
, buf
, size
);
783 static void tap_send(void *opaque
)
785 TAPState
*s
= opaque
;
787 /* First try to send any buffered packet */
791 /* If noone can receive the packet, buffer it */
792 err
= tap_send_packet(s
);
797 /* Read packets until we hit EAGAIN */
802 sbuf
.maxlen
= sizeof(s
->buf
);
804 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
806 // FIXME: kvm_sleep_begin();
807 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
808 // FIXME: kvm_sleep_end();
811 if (s
->size
== -1 && errno
== EINTR
)
817 /* If noone can receive the packet, buffer it */
818 err
= tap_send_packet(s
);
822 } while (s
->size
> 0);
825 int tap_has_vnet_hdr(void *opaque
)
827 VLANClientState
*vc
= opaque
;
828 TAPState
*s
= vc
->opaque
;
830 return s
? s
->has_vnet_hdr
: 0;
833 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
835 VLANClientState
*vc
= opaque
;
836 TAPState
*s
= vc
->opaque
;
838 if (!s
|| !s
->has_vnet_hdr
)
841 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
844 static int tap_probe_vnet_hdr(int fd
)
846 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
849 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
850 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
854 return ifr
.ifr_flags
& IFF_VNET_HDR
;
861 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
864 TAPState
*s
= vc
->opaque
;
865 unsigned int offload
= 0;
868 offload
|= TUN_F_CSUM
;
870 offload
|= TUN_F_TSO4
;
872 offload
|= TUN_F_TSO6
;
873 if ((tso4
|| tso6
) && ecn
)
874 offload
|= TUN_F_TSO_ECN
;
877 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
878 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
881 #endif /* TUNSETOFFLOAD */
885 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
893 s
= qemu_mallocz(sizeof(TAPState
));
897 s
->has_vnet_hdr
= vnet_hdr
!= 0;
898 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
900 s
->vc
->fd_readv
= tap_receive_iov
;
903 s
->vc
->set_offload
= tap_set_offload
;
905 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
906 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
910 #if defined (_BSD) || defined (__FreeBSD_kernel__)
911 static int tap_open(char *ifname
, int ifname_size
)
917 TFR(fd
= open("/dev/tap", O_RDWR
));
919 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
924 dev
= devname(s
.st_rdev
, S_IFCHR
);
925 pstrcpy(ifname
, ifname_size
, dev
);
927 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
930 #elif defined(__sun__)
931 #define TUNNEWPPA (('T'<<16) | 0x0001)
933 * Allocate TAP device, returns opened fd.
934 * Stores dev name in the first arg(must be large enough).
936 int tap_alloc(char *dev
, size_t dev_size
)
938 int tap_fd
, if_fd
, ppa
= -1;
939 static int ip_fd
= 0;
942 static int arp_fd
= 0;
943 int ip_muxid
, arp_muxid
;
944 struct strioctl strioc_if
, strioc_ppa
;
945 int link_type
= I_PLINK
;;
947 char actual_name
[32] = "";
949 memset(&ifr
, 0x0, sizeof(ifr
));
953 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
957 /* Check if IP device was opened */
961 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
963 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
967 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
969 syslog(LOG_ERR
, "Can't open /dev/tap");
973 /* Assign a new PPA and get its unit number. */
974 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
975 strioc_ppa
.ic_timout
= 0;
976 strioc_ppa
.ic_len
= sizeof(ppa
);
977 strioc_ppa
.ic_dp
= (char *)&ppa
;
978 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
979 syslog (LOG_ERR
, "Can't assign new interface");
981 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
983 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
986 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
987 syslog(LOG_ERR
, "Can't push IP module");
991 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
992 syslog(LOG_ERR
, "Can't get flags\n");
994 snprintf (actual_name
, 32, "tap%d", ppa
);
995 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
998 /* Assign ppa according to the unit number returned by tun device */
1000 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1001 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1002 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1003 syslog (LOG_ERR
, "Can't get flags\n");
1004 /* Push arp module to if_fd */
1005 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1006 syslog (LOG_ERR
, "Can't push ARP module (2)");
1008 /* Push arp module to ip_fd */
1009 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1010 syslog (LOG_ERR
, "I_POP failed\n");
1011 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1012 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1014 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1016 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1018 /* Set ifname to arp */
1019 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1020 strioc_if
.ic_timout
= 0;
1021 strioc_if
.ic_len
= sizeof(ifr
);
1022 strioc_if
.ic_dp
= (char *)&ifr
;
1023 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1024 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1027 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1028 syslog(LOG_ERR
, "Can't link TAP device to IP");
1032 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1033 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1037 memset(&ifr
, 0x0, sizeof(ifr
));
1038 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1039 ifr
.lifr_ip_muxid
= ip_muxid
;
1040 ifr
.lifr_arp_muxid
= arp_muxid
;
1042 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1044 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1045 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1046 syslog (LOG_ERR
, "Can't set multiplexor id");
1049 snprintf(dev
, dev_size
, "tap%d", ppa
);
1053 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1057 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1058 fprintf(stderr
, "Cannot allocate TAP device\n");
1061 pstrcpy(ifname
, ifname_size
, dev
);
1062 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1065 #elif defined (_AIX)
1066 static int tap_open(char *ifname
, int ifname_size
)
1068 fprintf (stderr
, "no tap on AIX\n");
1072 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1077 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1079 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1082 memset(&ifr
, 0, sizeof(ifr
));
1083 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1085 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1087 unsigned int features
;
1089 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1090 features
& IFF_VNET_HDR
) {
1092 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1097 if (ifname
[0] != '\0')
1098 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1100 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1101 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1103 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1107 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1108 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1113 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1119 /* try to launch network script */
1123 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1124 for (i
= 0; i
< open_max
; i
++)
1125 if (i
!= STDIN_FILENO
&&
1126 i
!= STDOUT_FILENO
&&
1127 i
!= STDERR_FILENO
&&
1132 *parg
++ = (char *)setup_script
;
1133 *parg
++ = (char *)ifname
;
1135 execv(setup_script
, args
);
1138 while (waitpid(pid
, &status
, 0) != pid
);
1139 if (!WIFEXITED(status
) ||
1140 WEXITSTATUS(status
) != 0) {
1141 fprintf(stderr
, "%s: could not launch network script\n",
1149 static int net_tap_init(VLANState
*vlan
, const char *model
,
1150 const char *name
, const char *ifname1
,
1151 const char *setup_script
, const char *down_script
)
1158 if (ifname1
!= NULL
)
1159 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1163 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1167 if (!setup_script
|| !strcmp(setup_script
, "no"))
1169 if (setup_script
[0] != '\0') {
1170 if (launch_script(setup_script
, ifname
, fd
))
1173 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1177 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1178 "ifname=%s,script=%s,downscript=%s",
1179 ifname
, setup_script
, down_script
);
1180 if (down_script
&& strcmp(down_script
, "no")) {
1181 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1182 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1187 #endif /* !_WIN32 */
1189 #if defined(CONFIG_VDE)
1190 typedef struct VDEState
{
1191 VLANClientState
*vc
;
1195 static void vde_to_qemu(void *opaque
)
1197 VDEState
*s
= opaque
;
1201 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1203 qemu_send_packet(s
->vc
, buf
, size
);
1207 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1209 VDEState
*s
= opaque
;
1212 ret
= vde_send(s
->vde
, buf
, size
, 0);
1213 if (ret
< 0 && errno
== EINTR
) {
1220 static int net_vde_init(VLANState
*vlan
, const char *model
,
1221 const char *name
, const char *sock
,
1222 int port
, const char *group
, int mode
)
1225 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1226 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1228 struct vde_open_args args
= {
1230 .group
= init_group
,
1234 s
= qemu_mallocz(sizeof(VDEState
));
1237 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1242 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1243 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1244 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1245 sock
, vde_datafd(s
->vde
));
1250 /* network connection */
1251 typedef struct NetSocketState
{
1252 VLANClientState
*vc
;
1254 int state
; /* 0 = getting length, 1 = getting data */
1258 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1261 typedef struct NetSocketListenState
{
1266 } NetSocketListenState
;
1268 /* XXX: we consider we can send the whole packet without blocking */
1269 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1271 NetSocketState
*s
= opaque
;
1275 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1276 send_all(s
->fd
, buf
, size
);
1279 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1281 NetSocketState
*s
= opaque
;
1282 sendto(s
->fd
, buf
, size
, 0,
1283 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1286 static void net_socket_send(void *opaque
)
1288 NetSocketState
*s
= opaque
;
1293 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1295 err
= socket_error();
1296 if (err
!= EWOULDBLOCK
)
1298 } else if (size
== 0) {
1299 /* end of connection */
1301 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1307 /* reassemble a packet from the network */
1313 memcpy(s
->buf
+ s
->index
, buf
, l
);
1317 if (s
->index
== 4) {
1319 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1325 l
= s
->packet_len
- s
->index
;
1328 memcpy(s
->buf
+ s
->index
, buf
, l
);
1332 if (s
->index
>= s
->packet_len
) {
1333 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1342 static void net_socket_send_dgram(void *opaque
)
1344 NetSocketState
*s
= opaque
;
1347 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1351 /* end of connection */
1352 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1355 qemu_send_packet(s
->vc
, s
->buf
, size
);
1358 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1363 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1364 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1365 inet_ntoa(mcastaddr
->sin_addr
),
1366 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1370 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1372 perror("socket(PF_INET, SOCK_DGRAM)");
1377 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1378 (const char *)&val
, sizeof(val
));
1380 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1384 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1390 /* Add host to multicast group */
1391 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1392 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1394 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1395 (const char *)&imr
, sizeof(struct ip_mreq
));
1397 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1401 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1403 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1404 (const char *)&val
, sizeof(val
));
1406 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1410 socket_set_nonblock(fd
);
1418 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1421 int fd
, int is_connected
)
1423 struct sockaddr_in saddr
;
1425 socklen_t saddr_len
;
1428 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1429 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1430 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1434 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1436 if (saddr
.sin_addr
.s_addr
==0) {
1437 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1441 /* clone dgram socket */
1442 newfd
= net_socket_mcast_create(&saddr
);
1444 /* error already reported by net_socket_mcast_create() */
1448 /* clone newfd to fd, close newfd */
1453 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1454 fd
, strerror(errno
));
1459 s
= qemu_mallocz(sizeof(NetSocketState
));
1464 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1465 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1467 /* mcast: save bound address as dst */
1468 if (is_connected
) s
->dgram_dst
=saddr
;
1470 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1471 "socket: fd=%d (%s mcast=%s:%d)",
1472 fd
, is_connected
? "cloned" : "",
1473 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1477 static void net_socket_connect(void *opaque
)
1479 NetSocketState
*s
= opaque
;
1480 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1483 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1486 int fd
, int is_connected
)
1489 s
= qemu_mallocz(sizeof(NetSocketState
));
1493 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1494 net_socket_receive
, NULL
, s
);
1495 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1496 "socket: fd=%d", fd
);
1498 net_socket_connect(s
);
1500 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1505 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1506 const char *model
, const char *name
,
1507 int fd
, int is_connected
)
1509 int so_type
=-1, optlen
=sizeof(so_type
);
1511 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1512 (socklen_t
*)&optlen
)< 0) {
1513 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1518 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1520 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1522 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1523 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1524 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1529 static void net_socket_accept(void *opaque
)
1531 NetSocketListenState
*s
= opaque
;
1533 struct sockaddr_in saddr
;
1538 len
= sizeof(saddr
);
1539 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1540 if (fd
< 0 && errno
!= EINTR
) {
1542 } else if (fd
>= 0) {
1546 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1550 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1551 "socket: connection from %s:%d",
1552 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1556 static int net_socket_listen_init(VLANState
*vlan
,
1559 const char *host_str
)
1561 NetSocketListenState
*s
;
1563 struct sockaddr_in saddr
;
1565 if (parse_host_port(&saddr
, host_str
) < 0)
1568 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1572 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1577 socket_set_nonblock(fd
);
1579 /* allow fast reuse */
1581 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1583 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1588 ret
= listen(fd
, 0);
1594 s
->model
= strdup(model
);
1595 s
->name
= strdup(name
);
1597 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1601 static int net_socket_connect_init(VLANState
*vlan
,
1604 const char *host_str
)
1607 int fd
, connected
, ret
, err
;
1608 struct sockaddr_in saddr
;
1610 if (parse_host_port(&saddr
, host_str
) < 0)
1613 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1618 socket_set_nonblock(fd
);
1622 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1624 err
= socket_error();
1625 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1626 } else if (err
== EINPROGRESS
) {
1629 } else if (err
== WSAEALREADY
) {
1642 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1645 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1646 "socket: connect to %s:%d",
1647 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1651 static int net_socket_mcast_init(VLANState
*vlan
,
1654 const char *host_str
)
1658 struct sockaddr_in saddr
;
1660 if (parse_host_port(&saddr
, host_str
) < 0)
1664 fd
= net_socket_mcast_create(&saddr
);
1668 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1672 s
->dgram_dst
= saddr
;
1674 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1675 "socket: mcast=%s:%d",
1676 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1681 /* find or alloc a new VLAN */
1682 VLANState
*qemu_find_vlan(int id
)
1684 VLANState
**pvlan
, *vlan
;
1685 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1689 vlan
= qemu_mallocz(sizeof(VLANState
));
1694 pvlan
= &first_vlan
;
1695 while (*pvlan
!= NULL
)
1696 pvlan
= &(*pvlan
)->next
;
1701 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1703 const char *models
[2];
1708 qemu_check_nic_model_list(nd
, models
, model
);
1711 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1712 const char *default_model
)
1714 int i
, exit_status
= 0;
1717 nd
->model
= strdup(default_model
);
1719 if (strcmp(nd
->model
, "?") != 0) {
1720 for (i
= 0 ; models
[i
]; i
++)
1721 if (strcmp(nd
->model
, models
[i
]) == 0)
1724 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1728 fprintf(stderr
, "qemu: Supported NIC models: ");
1729 for (i
= 0 ; models
[i
]; i
++)
1730 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1735 int net_client_init(const char *device
, const char *p
)
1743 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1744 vlan_id
= strtol(buf
, NULL
, 0);
1746 vlan
= qemu_find_vlan(vlan_id
);
1748 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1751 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1754 if (!strcmp(device
, "nic")) {
1758 if (nb_nics
>= MAX_NICS
) {
1759 fprintf(stderr
, "Too Many NICs\n");
1762 nd
= &nd_table
[nb_nics
];
1763 macaddr
= nd
->macaddr
;
1769 macaddr
[5] = 0x56 + nb_nics
;
1771 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1772 if (parse_macaddr(macaddr
, buf
) < 0) {
1773 fprintf(stderr
, "invalid syntax for ethernet address\n");
1777 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1778 nd
->model
= strdup(buf
);
1784 vlan
->nb_guest_devs
++;
1787 if (!strcmp(device
, "none")) {
1788 /* does nothing. It is needed to signal that no network cards
1793 if (!strcmp(device
, "user")) {
1794 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1795 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1797 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1798 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1800 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1801 slirp_ip
= strdup(buf
);
1803 vlan
->nb_host_devs
++;
1804 ret
= net_slirp_init(vlan
, device
, name
);
1808 if (!strcmp(device
, "tap")) {
1810 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1811 fprintf(stderr
, "tap: no interface name\n");
1814 vlan
->nb_host_devs
++;
1815 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1817 #elif defined (_AIX)
1819 if (!strcmp(device
, "tap")) {
1821 char setup_script
[1024], down_script
[1024];
1823 vlan
->nb_host_devs
++;
1824 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1825 fd
= strtol(buf
, NULL
, 0);
1826 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1828 if (net_tap_fd_init(vlan
, device
, name
, fd
,
1829 tap_probe_vnet_hdr(fd
)))
1832 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1835 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1836 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1838 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1839 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1841 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1845 if (!strcmp(device
, "socket")) {
1846 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1848 fd
= strtol(buf
, NULL
, 0);
1850 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1852 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1853 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1854 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1855 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1856 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1857 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1859 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1862 vlan
->nb_host_devs
++;
1865 if (!strcmp(device
, "vde")) {
1866 char vde_sock
[1024], vde_group
[512];
1867 int vde_port
, vde_mode
;
1868 vlan
->nb_host_devs
++;
1869 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1872 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1873 vde_port
= strtol(buf
, NULL
, 10);
1877 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1878 vde_group
[0] = '\0';
1880 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1881 vde_mode
= strtol(buf
, NULL
, 8);
1885 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1889 fprintf(stderr
, "Unknown network device: %s\n", device
);
1895 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1902 void net_client_uninit(NICInfo
*nd
)
1904 nd
->vlan
->nb_guest_devs
--; /* XXX: free vlan on last reference */
1907 free((void *)nd
->model
);
1910 int net_client_parse(const char *str
)
1918 while (*p
!= '\0' && *p
!= ',') {
1919 if ((q
- device
) < sizeof(device
) - 1)
1927 return net_client_init(device
, p
);
1930 void do_info_network(void)
1933 VLANClientState
*vc
;
1935 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1936 term_printf("VLAN %d devices:\n", vlan
->id
);
1937 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1938 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
1942 int do_set_link(const char *name
, const char *up_or_down
)
1945 VLANClientState
*vc
= NULL
;
1947 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
1948 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1949 if (strcmp(vc
->name
, name
) == 0)
1954 term_printf("could not find network device '%s'", name
);
1958 if (strcmp(up_or_down
, "up") == 0)
1960 else if (strcmp(up_or_down
, "down") == 0)
1963 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
1966 if (vc
->link_status_changed
)
1967 vc
->link_status_changed(vc
);
1972 void net_cleanup(void)
1976 #if !defined(_WIN32)
1977 /* close network clients */
1978 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1979 VLANClientState
*vc
;
1981 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1982 if (vc
->fd_read
== tap_receive
) {
1983 TAPState
*s
= vc
->opaque
;
1985 if (s
->down_script
[0])
1986 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1988 #if defined(CONFIG_VDE)
1989 if (vc
->fd_read
== vde_from_qemu
) {
1990 VDEState
*s
= vc
->opaque
;
1999 void net_client_check(void)
2003 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2004 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2006 if (vlan
->nb_guest_devs
== 0)
2007 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2008 if (vlan
->nb_host_devs
== 0)
2010 "Warning: vlan %d is not connected to host network\n",