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 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
373 VLANClientState
**pvc
= &vlan
->first_client
;
376 if ((*pvc
)->opaque
== opaque
)
384 int qemu_can_send_packet(VLANClientState
*vc1
)
386 VLANState
*vlan
= vc1
->vlan
;
389 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
391 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
398 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
400 VLANState
*vlan
= vc1
->vlan
;
408 printf("vlan %d send:\n", vlan
->id
);
409 hex_dump(stdout
, buf
, size
);
411 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
412 if (vc
!= vc1
&& !vc
->link_down
) {
413 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
414 vc
->fd_read(vc
->opaque
, buf
, size
);
422 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
425 uint8_t buffer
[4096];
429 for (i
= 0; i
< iovcnt
; i
++) {
432 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
433 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
437 vc
->fd_read(vc
->opaque
, buffer
, offset
);
442 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
447 for (i
= 0; i
< iovcnt
; i
++)
448 offset
+= iov
[i
].iov_len
;
452 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
455 VLANState
*vlan
= vc1
->vlan
;
460 return calc_iov_length(iov
, iovcnt
);
462 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
469 len
= calc_iov_length(iov
, iovcnt
);
471 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
472 else if (vc
->fd_read
)
473 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
475 max_len
= MAX(max_len
, len
);
481 #if defined(CONFIG_SLIRP)
483 /* slirp network adapter */
485 static int slirp_inited
;
486 static int slirp_restrict
;
487 static char *slirp_ip
;
488 static VLANClientState
*slirp_vc
;
490 int slirp_can_output(void)
492 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
495 void slirp_output(const uint8_t *pkt
, int pkt_len
)
498 printf("slirp output:\n");
499 hex_dump(stdout
, pkt
, pkt_len
);
503 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
506 int slirp_is_inited(void)
511 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
514 printf("slirp input:\n");
515 hex_dump(stdout
, buf
, size
);
517 slirp_input(buf
, size
);
520 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
524 slirp_init(slirp_restrict
, slirp_ip
);
526 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
527 slirp_receive
, NULL
, NULL
);
528 slirp_vc
->info_str
[0] = '\0';
532 void net_slirp_redir(const char *redir_str
)
537 struct in_addr guest_addr
;
538 int host_port
, guest_port
;
542 slirp_init(slirp_restrict
, slirp_ip
);
546 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
548 if (!strcmp(buf
, "tcp")) {
550 } else if (!strcmp(buf
, "udp")) {
556 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
558 host_port
= strtol(buf
, &r
, 0);
562 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
564 if (buf
[0] == '\0') {
565 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
567 if (!inet_aton(buf
, &guest_addr
))
570 guest_port
= strtol(p
, &r
, 0);
574 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
575 fprintf(stderr
, "qemu: could not set up redirection\n");
580 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
586 static char smb_dir
[1024];
588 static void erase_dir(char *dir_name
)
594 /* erase all the files in the directory */
595 if ((d
= opendir(dir_name
)) != 0) {
600 if (strcmp(de
->d_name
, ".") != 0 &&
601 strcmp(de
->d_name
, "..") != 0) {
602 snprintf(filename
, sizeof(filename
), "%s/%s",
603 smb_dir
, de
->d_name
);
604 if (unlink(filename
) != 0) /* is it a directory? */
613 /* automatic user mode samba server configuration */
614 static void smb_exit(void)
619 /* automatic user mode samba server configuration */
620 void net_slirp_smb(const char *exported_dir
)
623 char smb_cmdline
[1024];
628 slirp_init(slirp_restrict
, slirp_ip
);
631 /* XXX: better tmp dir construction */
632 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
633 if (mkdir(smb_dir
, 0700) < 0) {
634 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
637 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
639 f
= fopen(smb_conf
, "w");
641 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
648 "socket address=127.0.0.1\n"
650 "lock directory=%s\n"
651 "log file=%s/log.smbd\n"
652 "smb passwd file=%s/smbpasswd\n"
668 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
669 SMBD_COMMAND
, smb_conf
);
671 slirp_add_exec(0, smb_cmdline
, 4, 139);
674 #endif /* !defined(_WIN32) */
675 void do_info_slirp(void)
685 static int vmchannel_can_read(void *opaque
)
687 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
688 return slirp_socket_can_recv(4, vmc
->port
);
691 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
693 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
694 slirp_socket_recv(4, vmc
->port
, buf
, size
);
697 #endif /* CONFIG_SLIRP */
701 int tap_has_vnet_hdr(void *opaque
)
706 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
710 #else /* !defined(_WIN32) */
712 /* Maximum GSO packet size (64k) plus plenty of room for
713 * the ethernet and virtio_net headers
715 #define TAP_BUFSIZE (4096 + 65536)
718 #include <linux/virtio_net.h>
721 typedef struct TAPState
{
724 char down_script
[1024];
725 char down_script_arg
[128];
726 char buf
[TAP_BUFSIZE
];
728 unsigned int has_vnet_hdr
: 1;
729 unsigned int using_vnet_hdr
: 1;
733 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
736 TAPState
*s
= opaque
;
740 len
= writev(s
->fd
, iov
, iovcnt
);
741 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
747 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
753 TAPState
*s
= opaque
;
754 struct virtio_net_hdr hdr
= { 0, };
756 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
757 iov
[i
].iov_base
= &hdr
;
758 iov
[i
].iov_len
= sizeof(hdr
);
763 iov
[i
].iov_base
= (char *) buf
;
764 iov
[i
].iov_len
= size
;
767 tap_receive_iov(opaque
, iov
, i
);
770 static int tap_can_send(void *opaque
)
772 TAPState
*s
= opaque
;
776 /* Check to see if any of our clients can receive a packet */
777 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
782 if (!vc
->fd_can_read
) {
783 /* no fd_can_read handler, they always can receive */
786 can_receive
= vc
->fd_can_read(vc
->opaque
);
788 /* Once someone can receive, we try to send a packet */
796 static int tap_send_packet(TAPState
*s
)
798 uint8_t *buf
= (uint8_t *)s
->buf
;
802 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
803 buf
+= sizeof(struct virtio_net_hdr
);
804 size
-= sizeof(struct virtio_net_hdr
);
808 return qemu_send_packet(s
->vc
, buf
, size
);
811 static void tap_send(void *opaque
)
813 TAPState
*s
= opaque
;
815 /* First try to send any buffered packet */
819 /* If noone can receive the packet, buffer it */
820 err
= tap_send_packet(s
);
825 /* Read packets until we hit EAGAIN */
830 sbuf
.maxlen
= sizeof(s
->buf
);
832 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
834 // FIXME: kvm_sleep_begin();
835 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
836 // FIXME: kvm_sleep_end();
839 if (s
->size
== -1 && errno
== EINTR
)
845 /* If noone can receive the packet, buffer it */
846 err
= tap_send_packet(s
);
850 } while (s
->size
> 0);
853 int tap_has_vnet_hdr(void *opaque
)
855 VLANClientState
*vc
= opaque
;
856 TAPState
*s
= vc
->opaque
;
858 return s
? s
->has_vnet_hdr
: 0;
861 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
863 VLANClientState
*vc
= opaque
;
864 TAPState
*s
= vc
->opaque
;
866 if (!s
|| !s
->has_vnet_hdr
)
869 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
872 static int tap_probe_vnet_hdr(int fd
)
874 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
877 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
878 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
882 return ifr
.ifr_flags
& IFF_VNET_HDR
;
889 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
892 TAPState
*s
= vc
->opaque
;
893 unsigned int offload
= 0;
896 offload
|= TUN_F_CSUM
;
898 offload
|= TUN_F_TSO4
;
900 offload
|= TUN_F_TSO6
;
901 if ((tso4
|| tso6
) && ecn
)
902 offload
|= TUN_F_TSO_ECN
;
905 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
906 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
909 #endif /* TUNSETOFFLOAD */
913 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
921 s
= qemu_mallocz(sizeof(TAPState
));
923 s
->has_vnet_hdr
= vnet_hdr
!= 0;
924 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
926 s
->vc
->fd_readv
= tap_receive_iov
;
929 s
->vc
->set_offload
= tap_set_offload
;
931 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
932 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
936 #if defined (_BSD) || defined (__FreeBSD_kernel__)
937 static int tap_open(char *ifname
, int ifname_size
)
943 TFR(fd
= open("/dev/tap", O_RDWR
));
945 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
950 dev
= devname(s
.st_rdev
, S_IFCHR
);
951 pstrcpy(ifname
, ifname_size
, dev
);
953 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
956 #elif defined(__sun__)
957 #define TUNNEWPPA (('T'<<16) | 0x0001)
959 * Allocate TAP device, returns opened fd.
960 * Stores dev name in the first arg(must be large enough).
962 int tap_alloc(char *dev
, size_t dev_size
)
964 int tap_fd
, if_fd
, ppa
= -1;
965 static int ip_fd
= 0;
968 static int arp_fd
= 0;
969 int ip_muxid
, arp_muxid
;
970 struct strioctl strioc_if
, strioc_ppa
;
971 int link_type
= I_PLINK
;;
973 char actual_name
[32] = "";
975 memset(&ifr
, 0x0, sizeof(ifr
));
979 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
983 /* Check if IP device was opened */
987 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
989 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
993 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
995 syslog(LOG_ERR
, "Can't open /dev/tap");
999 /* Assign a new PPA and get its unit number. */
1000 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1001 strioc_ppa
.ic_timout
= 0;
1002 strioc_ppa
.ic_len
= sizeof(ppa
);
1003 strioc_ppa
.ic_dp
= (char *)&ppa
;
1004 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1005 syslog (LOG_ERR
, "Can't assign new interface");
1007 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1009 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1012 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1013 syslog(LOG_ERR
, "Can't push IP module");
1017 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1018 syslog(LOG_ERR
, "Can't get flags\n");
1020 snprintf (actual_name
, 32, "tap%d", ppa
);
1021 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1024 /* Assign ppa according to the unit number returned by tun device */
1026 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1027 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1028 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1029 syslog (LOG_ERR
, "Can't get flags\n");
1030 /* Push arp module to if_fd */
1031 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1032 syslog (LOG_ERR
, "Can't push ARP module (2)");
1034 /* Push arp module to ip_fd */
1035 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1036 syslog (LOG_ERR
, "I_POP failed\n");
1037 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1038 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1040 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1042 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1044 /* Set ifname to arp */
1045 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1046 strioc_if
.ic_timout
= 0;
1047 strioc_if
.ic_len
= sizeof(ifr
);
1048 strioc_if
.ic_dp
= (char *)&ifr
;
1049 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1050 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1053 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1054 syslog(LOG_ERR
, "Can't link TAP device to IP");
1058 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1059 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1063 memset(&ifr
, 0x0, sizeof(ifr
));
1064 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1065 ifr
.lifr_ip_muxid
= ip_muxid
;
1066 ifr
.lifr_arp_muxid
= arp_muxid
;
1068 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1070 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1071 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1072 syslog (LOG_ERR
, "Can't set multiplexor id");
1075 snprintf(dev
, dev_size
, "tap%d", ppa
);
1079 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1083 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1084 fprintf(stderr
, "Cannot allocate TAP device\n");
1087 pstrcpy(ifname
, ifname_size
, dev
);
1088 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1091 #elif defined (_AIX)
1092 static int tap_open(char *ifname
, int ifname_size
)
1094 fprintf (stderr
, "no tap on AIX\n");
1098 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1103 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1105 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1108 memset(&ifr
, 0, sizeof(ifr
));
1109 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1111 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1113 unsigned int features
;
1115 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1116 features
& IFF_VNET_HDR
) {
1118 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1123 if (ifname
[0] != '\0')
1124 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1126 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1127 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1129 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1133 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1134 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1139 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1145 /* try to launch network script */
1149 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1150 for (i
= 0; i
< open_max
; i
++)
1151 if (i
!= STDIN_FILENO
&&
1152 i
!= STDOUT_FILENO
&&
1153 i
!= STDERR_FILENO
&&
1158 *parg
++ = (char *)setup_script
;
1159 *parg
++ = (char *)ifname
;
1161 execv(setup_script
, args
);
1164 while (waitpid(pid
, &status
, 0) != pid
);
1165 if (!WIFEXITED(status
) ||
1166 WEXITSTATUS(status
) != 0) {
1167 fprintf(stderr
, "%s: could not launch network script\n",
1175 static int net_tap_init(VLANState
*vlan
, const char *model
,
1176 const char *name
, const char *ifname1
,
1177 const char *setup_script
, const char *down_script
)
1184 if (ifname1
!= NULL
)
1185 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1189 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1193 if (!setup_script
|| !strcmp(setup_script
, "no"))
1195 if (setup_script
[0] != '\0') {
1196 if (launch_script(setup_script
, ifname
, fd
))
1199 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1203 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1204 "ifname=%s,script=%s,downscript=%s",
1205 ifname
, setup_script
, down_script
);
1206 if (down_script
&& strcmp(down_script
, "no")) {
1207 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1208 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1213 #endif /* !_WIN32 */
1215 #if defined(CONFIG_VDE)
1216 typedef struct VDEState
{
1217 VLANClientState
*vc
;
1221 static void vde_to_qemu(void *opaque
)
1223 VDEState
*s
= opaque
;
1227 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1229 qemu_send_packet(s
->vc
, buf
, size
);
1233 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1235 VDEState
*s
= opaque
;
1238 ret
= vde_send(s
->vde
, buf
, size
, 0);
1239 if (ret
< 0 && errno
== EINTR
) {
1246 static int net_vde_init(VLANState
*vlan
, const char *model
,
1247 const char *name
, const char *sock
,
1248 int port
, const char *group
, int mode
)
1251 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1252 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1254 struct vde_open_args args
= {
1256 .group
= init_group
,
1260 s
= qemu_mallocz(sizeof(VDEState
));
1261 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1266 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1267 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1268 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1269 sock
, vde_datafd(s
->vde
));
1274 /* network connection */
1275 typedef struct NetSocketState
{
1276 VLANClientState
*vc
;
1278 int state
; /* 0 = getting length, 1 = getting data */
1280 unsigned int packet_len
;
1282 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1285 typedef struct NetSocketListenState
{
1290 } NetSocketListenState
;
1292 /* XXX: we consider we can send the whole packet without blocking */
1293 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1295 NetSocketState
*s
= opaque
;
1299 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1300 send_all(s
->fd
, buf
, size
);
1303 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1305 NetSocketState
*s
= opaque
;
1306 sendto(s
->fd
, buf
, size
, 0,
1307 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1310 static void net_socket_send(void *opaque
)
1312 NetSocketState
*s
= opaque
;
1318 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1320 err
= socket_error();
1321 if (err
!= EWOULDBLOCK
)
1323 } else if (size
== 0) {
1324 /* end of connection */
1326 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1332 /* reassemble a packet from the network */
1338 memcpy(s
->buf
+ s
->index
, buf
, l
);
1342 if (s
->index
== 4) {
1344 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1350 l
= s
->packet_len
- s
->index
;
1353 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1354 memcpy(s
->buf
+ s
->index
, buf
, l
);
1356 fprintf(stderr
, "serious error: oversized packet received,"
1357 "connection terminated.\n");
1365 if (s
->index
>= s
->packet_len
) {
1366 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1375 static void net_socket_send_dgram(void *opaque
)
1377 NetSocketState
*s
= opaque
;
1380 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1384 /* end of connection */
1385 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1388 qemu_send_packet(s
->vc
, s
->buf
, size
);
1391 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1396 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1397 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1398 inet_ntoa(mcastaddr
->sin_addr
),
1399 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1403 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1405 perror("socket(PF_INET, SOCK_DGRAM)");
1410 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1411 (const char *)&val
, sizeof(val
));
1413 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1417 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1423 /* Add host to multicast group */
1424 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1425 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1427 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1428 (const char *)&imr
, sizeof(struct ip_mreq
));
1430 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1434 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1436 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1437 (const char *)&val
, sizeof(val
));
1439 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1443 socket_set_nonblock(fd
);
1451 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1454 int fd
, int is_connected
)
1456 struct sockaddr_in saddr
;
1458 socklen_t saddr_len
;
1461 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1462 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1463 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1467 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1469 if (saddr
.sin_addr
.s_addr
==0) {
1470 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1474 /* clone dgram socket */
1475 newfd
= net_socket_mcast_create(&saddr
);
1477 /* error already reported by net_socket_mcast_create() */
1481 /* clone newfd to fd, close newfd */
1486 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1487 fd
, strerror(errno
));
1492 s
= qemu_mallocz(sizeof(NetSocketState
));
1495 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1496 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1498 /* mcast: save bound address as dst */
1499 if (is_connected
) s
->dgram_dst
=saddr
;
1501 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1502 "socket: fd=%d (%s mcast=%s:%d)",
1503 fd
, is_connected
? "cloned" : "",
1504 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1508 static void net_socket_connect(void *opaque
)
1510 NetSocketState
*s
= opaque
;
1511 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1514 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1517 int fd
, int is_connected
)
1520 s
= qemu_mallocz(sizeof(NetSocketState
));
1522 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1523 net_socket_receive
, NULL
, s
);
1524 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1525 "socket: fd=%d", fd
);
1527 net_socket_connect(s
);
1529 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1534 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1535 const char *model
, const char *name
,
1536 int fd
, int is_connected
)
1538 int so_type
=-1, optlen
=sizeof(so_type
);
1540 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1541 (socklen_t
*)&optlen
)< 0) {
1542 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1547 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1549 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1551 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1552 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1553 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1558 static void net_socket_accept(void *opaque
)
1560 NetSocketListenState
*s
= opaque
;
1562 struct sockaddr_in saddr
;
1567 len
= sizeof(saddr
);
1568 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1569 if (fd
< 0 && errno
!= EINTR
) {
1571 } else if (fd
>= 0) {
1575 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1579 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1580 "socket: connection from %s:%d",
1581 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1585 static int net_socket_listen_init(VLANState
*vlan
,
1588 const char *host_str
)
1590 NetSocketListenState
*s
;
1592 struct sockaddr_in saddr
;
1594 if (parse_host_port(&saddr
, host_str
) < 0)
1597 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1599 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1604 socket_set_nonblock(fd
);
1606 /* allow fast reuse */
1608 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1610 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1615 ret
= listen(fd
, 0);
1621 s
->model
= strdup(model
);
1622 s
->name
= strdup(name
);
1624 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1628 static int net_socket_connect_init(VLANState
*vlan
,
1631 const char *host_str
)
1634 int fd
, connected
, ret
, err
;
1635 struct sockaddr_in saddr
;
1637 if (parse_host_port(&saddr
, host_str
) < 0)
1640 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1645 socket_set_nonblock(fd
);
1649 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1651 err
= socket_error();
1652 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1653 } else if (err
== EINPROGRESS
) {
1656 } else if (err
== WSAEALREADY
) {
1669 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1672 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1673 "socket: connect to %s:%d",
1674 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1678 static int net_socket_mcast_init(VLANState
*vlan
,
1681 const char *host_str
)
1685 struct sockaddr_in saddr
;
1687 if (parse_host_port(&saddr
, host_str
) < 0)
1691 fd
= net_socket_mcast_create(&saddr
);
1695 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1699 s
->dgram_dst
= saddr
;
1701 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1702 "socket: mcast=%s:%d",
1703 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1708 /* find or alloc a new VLAN */
1709 VLANState
*qemu_find_vlan(int id
)
1711 VLANState
**pvlan
, *vlan
;
1712 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1716 vlan
= qemu_mallocz(sizeof(VLANState
));
1719 pvlan
= &first_vlan
;
1720 while (*pvlan
!= NULL
)
1721 pvlan
= &(*pvlan
)->next
;
1726 static int nic_get_free_idx(void)
1730 for (index
= 0; index
< MAX_NICS
; index
++)
1731 if (!nd_table
[index
].used
)
1736 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1738 const char *models
[2];
1743 qemu_check_nic_model_list(nd
, models
, model
);
1746 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1747 const char *default_model
)
1749 int i
, exit_status
= 0;
1752 nd
->model
= strdup(default_model
);
1754 if (strcmp(nd
->model
, "?") != 0) {
1755 for (i
= 0 ; models
[i
]; i
++)
1756 if (strcmp(nd
->model
, models
[i
]) == 0)
1759 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1763 fprintf(stderr
, "qemu: Supported NIC models: ");
1764 for (i
= 0 ; models
[i
]; i
++)
1765 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1770 int net_client_init(const char *device
, const char *p
)
1778 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1779 vlan_id
= strtol(buf
, NULL
, 0);
1781 vlan
= qemu_find_vlan(vlan_id
);
1783 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1786 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1789 if (!strcmp(device
, "nic")) {
1792 int idx
= nic_get_free_idx();
1794 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1795 fprintf(stderr
, "Too Many NICs\n");
1798 nd
= &nd_table
[idx
];
1799 macaddr
= nd
->macaddr
;
1805 macaddr
[5] = 0x56 + idx
;
1807 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1808 if (parse_macaddr(macaddr
, buf
) < 0) {
1809 fprintf(stderr
, "invalid syntax for ethernet address\n");
1813 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1814 nd
->model
= strdup(buf
);
1821 vlan
->nb_guest_devs
++;
1824 if (!strcmp(device
, "none")) {
1825 /* does nothing. It is needed to signal that no network cards
1830 if (!strcmp(device
, "user")) {
1831 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1832 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1834 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1835 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1837 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1838 slirp_ip
= strdup(buf
);
1840 vlan
->nb_host_devs
++;
1841 ret
= net_slirp_init(vlan
, device
, name
);
1842 } else if (!strcmp(device
, "channel")) {
1844 char name
[20], *devname
;
1845 struct VMChannel
*vmc
;
1847 port
= strtol(p
, &devname
, 10);
1849 if (port
< 1 || port
> 65535) {
1850 fprintf(stderr
, "vmchannel wrong port number\n");
1853 vmc
= malloc(sizeof(struct VMChannel
));
1854 snprintf(name
, 20, "vmchannel%ld", port
);
1855 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
1857 fprintf(stderr
, "qemu: could not open vmchannel device"
1862 slirp_add_exec(3, vmc
->hd
, 4, port
);
1863 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
1869 if (!strcmp(device
, "tap")) {
1871 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1872 fprintf(stderr
, "tap: no interface name\n");
1875 vlan
->nb_host_devs
++;
1876 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1878 #elif defined (_AIX)
1880 if (!strcmp(device
, "tap")) {
1882 char setup_script
[1024], down_script
[1024];
1884 vlan
->nb_host_devs
++;
1885 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1886 fd
= strtol(buf
, NULL
, 0);
1887 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1889 if (net_tap_fd_init(vlan
, device
, name
, fd
,
1890 tap_probe_vnet_hdr(fd
)))
1893 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1896 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1897 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1899 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1900 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1902 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1906 if (!strcmp(device
, "socket")) {
1907 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1909 fd
= strtol(buf
, NULL
, 0);
1911 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1913 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1914 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1915 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1916 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1917 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1918 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1920 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1923 vlan
->nb_host_devs
++;
1926 if (!strcmp(device
, "vde")) {
1927 char vde_sock
[1024], vde_group
[512];
1928 int vde_port
, vde_mode
;
1929 vlan
->nb_host_devs
++;
1930 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1933 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1934 vde_port
= strtol(buf
, NULL
, 10);
1938 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1939 vde_group
[0] = '\0';
1941 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1942 vde_mode
= strtol(buf
, NULL
, 8);
1946 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1950 fprintf(stderr
, "Unknown network device: %s\n", device
);
1956 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1963 void net_client_uninit(NICInfo
*nd
)
1965 nd
->vlan
->nb_guest_devs
--;
1968 free((void *)nd
->model
);
1971 static int net_host_check_device(const char *device
)
1974 const char *valid_param_list
[] = { "tap", "socket"
1982 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1983 if (!strncmp(valid_param_list
[i
], device
,
1984 strlen(valid_param_list
[i
])))
1991 void net_host_device_add(const char *device
, const char *opts
)
1993 if (!net_host_check_device(device
)) {
1994 term_printf("invalid host network device %s\n", device
);
1997 net_client_init(device
, opts
);
2000 void net_host_device_remove(int vlan_id
, const char *device
)
2003 VLANClientState
*vc
;
2005 vlan
= qemu_find_vlan(vlan_id
);
2007 term_printf("can't find vlan %d\n", vlan_id
);
2011 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2012 if (!strcmp(vc
->name
, device
))
2016 term_printf("can't find device %s\n", device
);
2019 qemu_del_vlan_client(vc
);
2022 int net_client_parse(const char *str
)
2030 while (*p
!= '\0' && *p
!= ',') {
2031 if ((q
- device
) < sizeof(device
) - 1)
2039 return net_client_init(device
, p
);
2042 void do_info_network(void)
2045 VLANClientState
*vc
;
2047 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2048 term_printf("VLAN %d devices:\n", vlan
->id
);
2049 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2050 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
2054 int do_set_link(const char *name
, const char *up_or_down
)
2057 VLANClientState
*vc
= NULL
;
2059 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2060 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2061 if (strcmp(vc
->name
, name
) == 0)
2066 term_printf("could not find network device '%s'", name
);
2070 if (strcmp(up_or_down
, "up") == 0)
2072 else if (strcmp(up_or_down
, "down") == 0)
2075 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
2078 if (vc
->link_status_changed
)
2079 vc
->link_status_changed(vc
);
2084 void net_cleanup(void)
2088 #if !defined(_WIN32)
2089 /* close network clients */
2090 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2091 VLANClientState
*vc
;
2093 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2094 if (vc
->fd_read
== tap_receive
) {
2095 TAPState
*s
= vc
->opaque
;
2097 if (s
->down_script
[0])
2098 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
2100 #if defined(CONFIG_VDE)
2101 if (vc
->fd_read
== vde_from_qemu
) {
2102 VDEState
*s
= vc
->opaque
;
2111 void net_client_check(void)
2115 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2116 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2118 if (vlan
->nb_guest_devs
== 0)
2119 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2120 if (vlan
->nb_host_devs
== 0)
2122 "Warning: vlan %d is not connected to host network\n",