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
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
113 // FIXME: #include "qemu-kvm.h"
114 #include "qemu-common.h"
118 #include "qemu-timer.h"
119 #include "qemu-char.h"
120 #include "audio/audio.h"
121 #include "qemu_socket.h"
123 #if defined(CONFIG_SLIRP)
124 #include "libslirp.h"
128 static VLANState
*first_vlan
;
130 /***********************************************************/
131 /* network device redirectors */
133 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
134 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
138 for(i
=0;i
<size
;i
+=16) {
142 fprintf(f
, "%08x ", i
);
145 fprintf(f
, " %02x", buf
[i
+j
]);
152 if (c
< ' ' || c
> '~')
161 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
168 offset
= strtol(p
, &last_char
, 0);
169 if (0 == errno
&& '\0' == *last_char
&&
170 offset
>= 0 && offset
<= 0xFFFFFF) {
171 macaddr
[3] = (offset
& 0xFF0000) >> 16;
172 macaddr
[4] = (offset
& 0xFF00) >> 8;
173 macaddr
[5] = offset
& 0xFF;
176 for(i
= 0; i
< 6; i
++) {
177 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
182 if (*p
!= ':' && *p
!= '-')
193 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
204 if (len
> buf_size
- 1)
213 int parse_host_src_port(struct sockaddr_in
*haddr
,
214 struct sockaddr_in
*saddr
,
215 const char *input_str
)
217 char *str
= strdup(input_str
);
218 char *host_str
= str
;
220 const char *src_str2
;
224 * Chop off any extra arguments at the end of the string which
225 * would start with a comma, then fill in the src port information
226 * if it was provided else use the "any address" and "any port".
228 if ((ptr
= strchr(str
,',')))
231 if ((src_str
= strchr(input_str
,'@'))) {
236 if (parse_host_port(haddr
, host_str
) < 0)
240 if (!src_str
|| *src_str
== '\0')
243 if (parse_host_port(saddr
, src_str2
) < 0)
254 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
262 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
264 saddr
->sin_family
= AF_INET
;
265 if (buf
[0] == '\0') {
266 saddr
->sin_addr
.s_addr
= 0;
268 if (qemu_isdigit(buf
[0])) {
269 if (!inet_aton(buf
, &saddr
->sin_addr
))
272 if ((he
= gethostbyname(buf
)) == NULL
)
274 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
277 port
= strtol(p
, (char **)&r
, 0);
280 saddr
->sin_port
= htons(port
);
284 #if !defined(_WIN32) && 0
285 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
290 len
= MIN(108, strlen(str
));
291 p
= strchr(str
, ',');
293 len
= MIN(len
, p
- str
);
295 memset(uaddr
, 0, sizeof(*uaddr
));
297 uaddr
->sun_family
= AF_UNIX
;
298 memcpy(uaddr
->sun_path
, str
, len
);
304 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
306 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
307 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
309 macaddr
[0], macaddr
[1], macaddr
[2],
310 macaddr
[3], macaddr
[4], macaddr
[5]);
313 static char *assign_name(VLANClientState
*vc1
, const char *model
)
319 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
322 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
323 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
327 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
332 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
335 IOReadHandler
*fd_read
,
336 IOCanRWHandler
*fd_can_read
,
339 VLANClientState
*vc
, **pvc
;
340 vc
= qemu_mallocz(sizeof(VLANClientState
));
341 vc
->model
= strdup(model
);
343 vc
->name
= strdup(name
);
345 vc
->name
= assign_name(vc
, model
);
346 vc
->fd_read
= fd_read
;
347 vc
->fd_can_read
= fd_can_read
;
352 pvc
= &vlan
->first_client
;
359 void qemu_del_vlan_client(VLANClientState
*vc
)
361 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
374 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
376 VLANClientState
**pvc
= &vlan
->first_client
;
379 if ((*pvc
)->opaque
== opaque
)
387 int qemu_can_send_packet(VLANClientState
*vc1
)
389 VLANState
*vlan
= vc1
->vlan
;
392 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
394 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
401 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
403 VLANState
*vlan
= vc1
->vlan
;
411 printf("vlan %d send:\n", vlan
->id
);
412 hex_dump(stdout
, buf
, size
);
414 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
415 if (vc
!= vc1
&& !vc
->link_down
) {
416 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
417 vc
->fd_read(vc
->opaque
, buf
, size
);
425 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
428 uint8_t buffer
[4096];
432 for (i
= 0; i
< iovcnt
; i
++) {
435 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
436 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
440 vc
->fd_read(vc
->opaque
, buffer
, offset
);
445 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
450 for (i
= 0; i
< iovcnt
; i
++)
451 offset
+= iov
[i
].iov_len
;
455 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
458 VLANState
*vlan
= vc1
->vlan
;
463 return calc_iov_length(iov
, iovcnt
);
465 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
472 len
= calc_iov_length(iov
, iovcnt
);
474 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
475 else if (vc
->fd_read
)
476 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
478 max_len
= MAX(max_len
, len
);
484 #if defined(CONFIG_SLIRP)
486 /* slirp network adapter */
488 static int slirp_inited
;
489 static int slirp_restrict
;
490 static char *slirp_ip
;
491 static VLANClientState
*slirp_vc
;
493 int slirp_can_output(void)
495 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
498 void slirp_output(const uint8_t *pkt
, int pkt_len
)
501 printf("slirp output:\n");
502 hex_dump(stdout
, pkt
, pkt_len
);
506 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
509 int slirp_is_inited(void)
514 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
517 printf("slirp input:\n");
518 hex_dump(stdout
, buf
, size
);
520 slirp_input(buf
, size
);
523 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
527 slirp_init(slirp_restrict
, slirp_ip
);
529 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
530 slirp_receive
, NULL
, NULL
);
531 slirp_vc
->info_str
[0] = '\0';
535 void net_slirp_redir(const char *redir_str
)
540 struct in_addr guest_addr
;
541 int host_port
, guest_port
;
545 slirp_init(slirp_restrict
, slirp_ip
);
549 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
551 if (!strcmp(buf
, "tcp")) {
553 } else if (!strcmp(buf
, "udp")) {
559 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
561 host_port
= strtol(buf
, &r
, 0);
565 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
567 if (buf
[0] == '\0') {
568 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
570 if (!inet_aton(buf
, &guest_addr
))
573 guest_port
= strtol(p
, &r
, 0);
577 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
578 fprintf(stderr
, "qemu: could not set up redirection\n");
583 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
589 static char smb_dir
[1024];
591 static void erase_dir(char *dir_name
)
597 /* erase all the files in the directory */
598 if ((d
= opendir(dir_name
)) != NULL
) {
603 if (strcmp(de
->d_name
, ".") != 0 &&
604 strcmp(de
->d_name
, "..") != 0) {
605 snprintf(filename
, sizeof(filename
), "%s/%s",
606 smb_dir
, de
->d_name
);
607 if (unlink(filename
) != 0) /* is it a directory? */
616 /* automatic user mode samba server configuration */
617 static void smb_exit(void)
622 /* automatic user mode samba server configuration */
623 void net_slirp_smb(const char *exported_dir
)
626 char smb_cmdline
[1024];
631 slirp_init(slirp_restrict
, slirp_ip
);
634 /* XXX: better tmp dir construction */
635 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
636 if (mkdir(smb_dir
, 0700) < 0) {
637 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
640 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
642 f
= fopen(smb_conf
, "w");
644 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
651 "socket address=127.0.0.1\n"
653 "lock directory=%s\n"
654 "log file=%s/log.smbd\n"
655 "smb passwd file=%s/smbpasswd\n"
671 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
672 SMBD_COMMAND
, smb_conf
);
674 slirp_add_exec(0, smb_cmdline
, 4, 139);
677 #endif /* !defined(_WIN32) */
678 void do_info_slirp(Monitor
*mon
)
688 static int vmchannel_can_read(void *opaque
)
690 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
691 return slirp_socket_can_recv(4, vmc
->port
);
694 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
696 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
697 slirp_socket_recv(4, vmc
->port
, buf
, size
);
700 #endif /* CONFIG_SLIRP */
704 int tap_has_vnet_hdr(void *opaque
)
709 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
713 #else /* !defined(_WIN32) */
715 /* Maximum GSO packet size (64k) plus plenty of room for
716 * the ethernet and virtio_net headers
718 #define TAP_BUFSIZE (4096 + 65536)
721 #include <linux/virtio_net.h>
724 typedef struct TAPState
{
727 char down_script
[1024];
728 char down_script_arg
[128];
729 char buf
[TAP_BUFSIZE
];
731 unsigned int has_vnet_hdr
: 1;
732 unsigned int using_vnet_hdr
: 1;
736 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
739 TAPState
*s
= opaque
;
743 len
= writev(s
->fd
, iov
, iovcnt
);
744 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
750 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
756 TAPState
*s
= opaque
;
757 struct virtio_net_hdr hdr
= { 0, };
759 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
760 iov
[i
].iov_base
= &hdr
;
761 iov
[i
].iov_len
= sizeof(hdr
);
766 iov
[i
].iov_base
= (char *) buf
;
767 iov
[i
].iov_len
= size
;
770 tap_receive_iov(opaque
, iov
, i
);
773 static int tap_can_send(void *opaque
)
775 TAPState
*s
= opaque
;
779 /* Check to see if any of our clients can receive a packet */
780 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
785 if (!vc
->fd_can_read
) {
786 /* no fd_can_read handler, they always can receive */
789 can_receive
= vc
->fd_can_read(vc
->opaque
);
791 /* Once someone can receive, we try to send a packet */
799 static int tap_send_packet(TAPState
*s
)
801 uint8_t *buf
= (uint8_t *)s
->buf
;
805 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
806 buf
+= sizeof(struct virtio_net_hdr
);
807 size
-= sizeof(struct virtio_net_hdr
);
811 return qemu_send_packet(s
->vc
, buf
, size
);
814 static void tap_send(void *opaque
)
816 TAPState
*s
= opaque
;
818 /* First try to send any buffered packet */
822 /* If noone can receive the packet, buffer it */
823 err
= tap_send_packet(s
);
828 /* Read packets until we hit EAGAIN */
833 sbuf
.maxlen
= sizeof(s
->buf
);
835 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
837 // FIXME: kvm_sleep_begin();
838 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
839 // FIXME: kvm_sleep_end();
842 if (s
->size
== -1 && errno
== EINTR
)
848 /* If noone can receive the packet, buffer it */
849 err
= tap_send_packet(s
);
853 } while (s
->size
> 0);
856 int tap_has_vnet_hdr(void *opaque
)
858 VLANClientState
*vc
= opaque
;
859 TAPState
*s
= vc
->opaque
;
861 return s
? s
->has_vnet_hdr
: 0;
864 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
866 VLANClientState
*vc
= opaque
;
867 TAPState
*s
= vc
->opaque
;
869 if (!s
|| !s
->has_vnet_hdr
)
872 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
875 static int tap_probe_vnet_hdr(int fd
)
877 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
880 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
881 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
885 return ifr
.ifr_flags
& IFF_VNET_HDR
;
892 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
895 TAPState
*s
= vc
->opaque
;
896 unsigned int offload
= 0;
899 offload
|= TUN_F_CSUM
;
901 offload
|= TUN_F_TSO4
;
903 offload
|= TUN_F_TSO6
;
904 if ((tso4
|| tso6
) && ecn
)
905 offload
|= TUN_F_TSO_ECN
;
908 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
909 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
912 #endif /* TUNSETOFFLOAD */
916 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
924 s
= qemu_mallocz(sizeof(TAPState
));
926 s
->has_vnet_hdr
= vnet_hdr
!= 0;
927 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
929 s
->vc
->fd_readv
= tap_receive_iov
;
932 s
->vc
->set_offload
= tap_set_offload
;
933 tap_set_offload(s
->vc
, 0, 0, 0, 0);
935 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
936 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
940 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
941 static int tap_open(char *ifname
, int ifname_size
)
947 TFR(fd
= open("/dev/tap", O_RDWR
));
949 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
954 dev
= devname(s
.st_rdev
, S_IFCHR
);
955 pstrcpy(ifname
, ifname_size
, dev
);
957 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
960 #elif defined(__sun__)
961 #define TUNNEWPPA (('T'<<16) | 0x0001)
963 * Allocate TAP device, returns opened fd.
964 * Stores dev name in the first arg(must be large enough).
966 int tap_alloc(char *dev
, size_t dev_size
)
968 int tap_fd
, if_fd
, ppa
= -1;
969 static int ip_fd
= 0;
972 static int arp_fd
= 0;
973 int ip_muxid
, arp_muxid
;
974 struct strioctl strioc_if
, strioc_ppa
;
975 int link_type
= I_PLINK
;;
977 char actual_name
[32] = "";
979 memset(&ifr
, 0x0, sizeof(ifr
));
983 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
987 /* Check if IP device was opened */
991 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
993 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
997 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
999 syslog(LOG_ERR
, "Can't open /dev/tap");
1003 /* Assign a new PPA and get its unit number. */
1004 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1005 strioc_ppa
.ic_timout
= 0;
1006 strioc_ppa
.ic_len
= sizeof(ppa
);
1007 strioc_ppa
.ic_dp
= (char *)&ppa
;
1008 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1009 syslog (LOG_ERR
, "Can't assign new interface");
1011 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1013 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1016 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1017 syslog(LOG_ERR
, "Can't push IP module");
1021 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1022 syslog(LOG_ERR
, "Can't get flags\n");
1024 snprintf (actual_name
, 32, "tap%d", ppa
);
1025 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1028 /* Assign ppa according to the unit number returned by tun device */
1030 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1031 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1032 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1033 syslog (LOG_ERR
, "Can't get flags\n");
1034 /* Push arp module to if_fd */
1035 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1036 syslog (LOG_ERR
, "Can't push ARP module (2)");
1038 /* Push arp module to ip_fd */
1039 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1040 syslog (LOG_ERR
, "I_POP failed\n");
1041 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1042 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1044 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1046 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1048 /* Set ifname to arp */
1049 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1050 strioc_if
.ic_timout
= 0;
1051 strioc_if
.ic_len
= sizeof(ifr
);
1052 strioc_if
.ic_dp
= (char *)&ifr
;
1053 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1054 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1057 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1058 syslog(LOG_ERR
, "Can't link TAP device to IP");
1062 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1063 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1067 memset(&ifr
, 0x0, sizeof(ifr
));
1068 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1069 ifr
.lifr_ip_muxid
= ip_muxid
;
1070 ifr
.lifr_arp_muxid
= arp_muxid
;
1072 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1074 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1075 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1076 syslog (LOG_ERR
, "Can't set multiplexor id");
1079 snprintf(dev
, dev_size
, "tap%d", ppa
);
1083 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1087 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1088 fprintf(stderr
, "Cannot allocate TAP device\n");
1091 pstrcpy(ifname
, ifname_size
, dev
);
1092 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1095 #elif defined (_AIX)
1096 static int tap_open(char *ifname
, int ifname_size
)
1098 fprintf (stderr
, "no tap on AIX\n");
1102 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1107 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1109 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1112 memset(&ifr
, 0, sizeof(ifr
));
1113 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1115 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1117 unsigned int features
;
1119 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1120 features
& IFF_VNET_HDR
) {
1122 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1127 if (ifname
[0] != '\0')
1128 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1130 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1131 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1133 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1137 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1138 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1143 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1149 /* try to launch network script */
1153 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1154 for (i
= 0; i
< open_max
; i
++)
1155 if (i
!= STDIN_FILENO
&&
1156 i
!= STDOUT_FILENO
&&
1157 i
!= STDERR_FILENO
&&
1162 *parg
++ = (char *)setup_script
;
1163 *parg
++ = (char *)ifname
;
1165 execv(setup_script
, args
);
1168 while (waitpid(pid
, &status
, 0) != pid
);
1169 if (!WIFEXITED(status
) ||
1170 WEXITSTATUS(status
) != 0) {
1171 fprintf(stderr
, "%s: could not launch network script\n",
1179 static int net_tap_init(VLANState
*vlan
, const char *model
,
1180 const char *name
, const char *ifname1
,
1181 const char *setup_script
, const char *down_script
)
1188 if (ifname1
!= NULL
)
1189 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1193 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1197 if (!setup_script
|| !strcmp(setup_script
, "no"))
1199 if (setup_script
[0] != '\0') {
1200 if (launch_script(setup_script
, ifname
, fd
))
1203 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1207 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1208 "ifname=%s,script=%s,downscript=%s",
1209 ifname
, setup_script
, down_script
);
1210 if (down_script
&& strcmp(down_script
, "no")) {
1211 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1212 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1217 #endif /* !_WIN32 */
1219 #if defined(CONFIG_VDE)
1220 typedef struct VDEState
{
1221 VLANClientState
*vc
;
1225 static void vde_to_qemu(void *opaque
)
1227 VDEState
*s
= opaque
;
1231 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1233 qemu_send_packet(s
->vc
, buf
, size
);
1237 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1239 VDEState
*s
= opaque
;
1242 ret
= vde_send(s
->vde
, buf
, size
, 0);
1243 if (ret
< 0 && errno
== EINTR
) {
1250 static int net_vde_init(VLANState
*vlan
, const char *model
,
1251 const char *name
, const char *sock
,
1252 int port
, const char *group
, int mode
)
1255 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1256 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1258 struct vde_open_args args
= {
1260 .group
= init_group
,
1264 s
= qemu_mallocz(sizeof(VDEState
));
1265 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1270 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1271 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1272 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1273 sock
, vde_datafd(s
->vde
));
1278 /* network connection */
1279 typedef struct NetSocketState
{
1280 VLANClientState
*vc
;
1282 int state
; /* 0 = getting length, 1 = getting data */
1284 unsigned int packet_len
;
1286 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1289 typedef struct NetSocketListenState
{
1294 } NetSocketListenState
;
1296 /* XXX: we consider we can send the whole packet without blocking */
1297 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1299 NetSocketState
*s
= opaque
;
1303 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1304 send_all(s
->fd
, buf
, size
);
1307 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1309 NetSocketState
*s
= opaque
;
1310 sendto(s
->fd
, buf
, size
, 0,
1311 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1314 static void net_socket_send(void *opaque
)
1316 NetSocketState
*s
= opaque
;
1322 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1324 err
= socket_error();
1325 if (err
!= EWOULDBLOCK
)
1327 } else if (size
== 0) {
1328 /* end of connection */
1330 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1336 /* reassemble a packet from the network */
1342 memcpy(s
->buf
+ s
->index
, buf
, l
);
1346 if (s
->index
== 4) {
1348 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1354 l
= s
->packet_len
- s
->index
;
1357 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1358 memcpy(s
->buf
+ s
->index
, buf
, l
);
1360 fprintf(stderr
, "serious error: oversized packet received,"
1361 "connection terminated.\n");
1369 if (s
->index
>= s
->packet_len
) {
1370 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1379 static void net_socket_send_dgram(void *opaque
)
1381 NetSocketState
*s
= opaque
;
1384 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1388 /* end of connection */
1389 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1392 qemu_send_packet(s
->vc
, s
->buf
, size
);
1395 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1400 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1401 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1402 inet_ntoa(mcastaddr
->sin_addr
),
1403 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1407 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1409 perror("socket(PF_INET, SOCK_DGRAM)");
1414 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1415 (const char *)&val
, sizeof(val
));
1417 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1421 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1427 /* Add host to multicast group */
1428 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1429 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1431 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1432 (const char *)&imr
, sizeof(struct ip_mreq
));
1434 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1438 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1440 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1441 (const char *)&val
, sizeof(val
));
1443 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1447 socket_set_nonblock(fd
);
1455 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1458 int fd
, int is_connected
)
1460 struct sockaddr_in saddr
;
1462 socklen_t saddr_len
;
1465 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1466 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1467 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1471 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1473 if (saddr
.sin_addr
.s_addr
==0) {
1474 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1478 /* clone dgram socket */
1479 newfd
= net_socket_mcast_create(&saddr
);
1481 /* error already reported by net_socket_mcast_create() */
1485 /* clone newfd to fd, close newfd */
1490 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1491 fd
, strerror(errno
));
1496 s
= qemu_mallocz(sizeof(NetSocketState
));
1499 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1500 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1502 /* mcast: save bound address as dst */
1503 if (is_connected
) s
->dgram_dst
=saddr
;
1505 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1506 "socket: fd=%d (%s mcast=%s:%d)",
1507 fd
, is_connected
? "cloned" : "",
1508 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1512 static void net_socket_connect(void *opaque
)
1514 NetSocketState
*s
= opaque
;
1515 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1518 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1521 int fd
, int is_connected
)
1524 s
= qemu_mallocz(sizeof(NetSocketState
));
1526 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1527 net_socket_receive
, NULL
, s
);
1528 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1529 "socket: fd=%d", fd
);
1531 net_socket_connect(s
);
1533 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1538 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1539 const char *model
, const char *name
,
1540 int fd
, int is_connected
)
1542 int so_type
=-1, optlen
=sizeof(so_type
);
1544 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1545 (socklen_t
*)&optlen
)< 0) {
1546 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1551 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1553 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1555 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1556 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1557 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1562 static void net_socket_accept(void *opaque
)
1564 NetSocketListenState
*s
= opaque
;
1566 struct sockaddr_in saddr
;
1571 len
= sizeof(saddr
);
1572 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1573 if (fd
< 0 && errno
!= EINTR
) {
1575 } else if (fd
>= 0) {
1579 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1583 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1584 "socket: connection from %s:%d",
1585 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1589 static int net_socket_listen_init(VLANState
*vlan
,
1592 const char *host_str
)
1594 NetSocketListenState
*s
;
1596 struct sockaddr_in saddr
;
1598 if (parse_host_port(&saddr
, host_str
) < 0)
1601 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1603 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1608 socket_set_nonblock(fd
);
1610 /* allow fast reuse */
1612 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1614 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1619 ret
= listen(fd
, 0);
1625 s
->model
= strdup(model
);
1626 s
->name
= strdup(name
);
1628 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1632 static int net_socket_connect_init(VLANState
*vlan
,
1635 const char *host_str
)
1638 int fd
, connected
, ret
, err
;
1639 struct sockaddr_in saddr
;
1641 if (parse_host_port(&saddr
, host_str
) < 0)
1644 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1649 socket_set_nonblock(fd
);
1653 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1655 err
= socket_error();
1656 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1657 } else if (err
== EINPROGRESS
) {
1660 } else if (err
== WSAEALREADY
) {
1673 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1676 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1677 "socket: connect to %s:%d",
1678 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1682 static int net_socket_mcast_init(VLANState
*vlan
,
1685 const char *host_str
)
1689 struct sockaddr_in saddr
;
1691 if (parse_host_port(&saddr
, host_str
) < 0)
1695 fd
= net_socket_mcast_create(&saddr
);
1699 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1703 s
->dgram_dst
= saddr
;
1705 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1706 "socket: mcast=%s:%d",
1707 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1712 /* find or alloc a new VLAN */
1713 VLANState
*qemu_find_vlan(int id
)
1715 VLANState
**pvlan
, *vlan
;
1716 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1720 vlan
= qemu_mallocz(sizeof(VLANState
));
1723 pvlan
= &first_vlan
;
1724 while (*pvlan
!= NULL
)
1725 pvlan
= &(*pvlan
)->next
;
1730 static int nic_get_free_idx(void)
1734 for (index
= 0; index
< MAX_NICS
; index
++)
1735 if (!nd_table
[index
].used
)
1740 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1742 const char *models
[2];
1747 qemu_check_nic_model_list(nd
, models
, model
);
1750 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1751 const char *default_model
)
1753 int i
, exit_status
= 0;
1756 nd
->model
= strdup(default_model
);
1758 if (strcmp(nd
->model
, "?") != 0) {
1759 for (i
= 0 ; models
[i
]; i
++)
1760 if (strcmp(nd
->model
, models
[i
]) == 0)
1763 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1767 fprintf(stderr
, "qemu: Supported NIC models: ");
1768 for (i
= 0 ; models
[i
]; i
++)
1769 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1774 int net_client_init(const char *device
, const char *p
)
1782 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1783 vlan_id
= strtol(buf
, NULL
, 0);
1785 vlan
= qemu_find_vlan(vlan_id
);
1787 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1790 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1793 if (!strcmp(device
, "nic")) {
1796 int idx
= nic_get_free_idx();
1798 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1799 fprintf(stderr
, "Too Many NICs\n");
1802 nd
= &nd_table
[idx
];
1803 macaddr
= nd
->macaddr
;
1809 macaddr
[5] = 0x56 + idx
;
1811 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1812 if (parse_macaddr(macaddr
, buf
) < 0) {
1813 fprintf(stderr
, "invalid syntax for ethernet address\n");
1817 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1818 nd
->model
= strdup(buf
);
1825 vlan
->nb_guest_devs
++;
1828 if (!strcmp(device
, "none")) {
1829 /* does nothing. It is needed to signal that no network cards
1834 if (!strcmp(device
, "user")) {
1835 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1836 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1838 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1839 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1841 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1842 slirp_ip
= strdup(buf
);
1844 vlan
->nb_host_devs
++;
1845 ret
= net_slirp_init(vlan
, device
, name
);
1846 } else if (!strcmp(device
, "channel")) {
1848 char name
[20], *devname
;
1849 struct VMChannel
*vmc
;
1851 port
= strtol(p
, &devname
, 10);
1853 if (port
< 1 || port
> 65535) {
1854 fprintf(stderr
, "vmchannel wrong port number\n");
1857 vmc
= malloc(sizeof(struct VMChannel
));
1858 snprintf(name
, 20, "vmchannel%ld", port
);
1859 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
1861 fprintf(stderr
, "qemu: could not open vmchannel device"
1866 slirp_add_exec(3, vmc
->hd
, 4, port
);
1867 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
1873 if (!strcmp(device
, "tap")) {
1875 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1876 fprintf(stderr
, "tap: no interface name\n");
1879 vlan
->nb_host_devs
++;
1880 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1882 #elif defined (_AIX)
1884 if (!strcmp(device
, "tap")) {
1886 char setup_script
[1024], down_script
[1024];
1888 vlan
->nb_host_devs
++;
1889 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1890 fd
= strtol(buf
, NULL
, 0);
1891 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1893 if (net_tap_fd_init(vlan
, device
, name
, fd
,
1894 tap_probe_vnet_hdr(fd
)))
1897 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1900 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1901 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1903 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1904 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1906 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1910 if (!strcmp(device
, "socket")) {
1911 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1913 fd
= strtol(buf
, NULL
, 0);
1915 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1917 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1918 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1919 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1920 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1921 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1922 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1924 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1927 vlan
->nb_host_devs
++;
1930 if (!strcmp(device
, "vde")) {
1931 char vde_sock
[1024], vde_group
[512];
1932 int vde_port
, vde_mode
;
1933 vlan
->nb_host_devs
++;
1934 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1937 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1938 vde_port
= strtol(buf
, NULL
, 10);
1942 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1943 vde_group
[0] = '\0';
1945 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1946 vde_mode
= strtol(buf
, NULL
, 8);
1950 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1954 fprintf(stderr
, "Unknown network device: %s\n", device
);
1960 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1967 void net_client_uninit(NICInfo
*nd
)
1969 nd
->vlan
->nb_guest_devs
--;
1972 free((void *)nd
->model
);
1975 static int net_host_check_device(const char *device
)
1978 const char *valid_param_list
[] = { "tap", "socket"
1986 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1987 if (!strncmp(valid_param_list
[i
], device
,
1988 strlen(valid_param_list
[i
])))
1995 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
1997 if (!net_host_check_device(device
)) {
1998 monitor_printf(mon
, "invalid host network device %s\n", device
);
2001 net_client_init(device
, opts
);
2004 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2007 VLANClientState
*vc
;
2009 vlan
= qemu_find_vlan(vlan_id
);
2011 monitor_printf(mon
, "can't find vlan %d\n", vlan_id
);
2015 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2016 if (!strcmp(vc
->name
, device
))
2020 monitor_printf(mon
, "can't find device %s\n", device
);
2023 qemu_del_vlan_client(vc
);
2026 int net_client_parse(const char *str
)
2034 while (*p
!= '\0' && *p
!= ',') {
2035 if ((q
- device
) < sizeof(device
) - 1)
2043 return net_client_init(device
, p
);
2046 void do_info_network(Monitor
*mon
)
2049 VLANClientState
*vc
;
2051 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2052 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2053 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2054 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2058 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2061 VLANClientState
*vc
= NULL
;
2063 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2064 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2065 if (strcmp(vc
->name
, name
) == 0)
2070 monitor_printf(mon
, "could not find network device '%s'", name
);
2074 if (strcmp(up_or_down
, "up") == 0)
2076 else if (strcmp(up_or_down
, "down") == 0)
2079 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2080 "valid\n", up_or_down
);
2082 if (vc
->link_status_changed
)
2083 vc
->link_status_changed(vc
);
2088 void net_cleanup(void)
2090 #if !defined(_WIN32)
2093 /* close network clients */
2094 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2095 VLANClientState
*vc
;
2097 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2098 if (vc
->fd_read
== tap_receive
) {
2099 TAPState
*s
= vc
->opaque
;
2101 if (s
->down_script
[0])
2102 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
2104 #if defined(CONFIG_VDE)
2105 if (vc
->fd_read
== vde_from_qemu
) {
2106 VDEState
*s
= vc
->opaque
;
2115 void net_client_check(void)
2119 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2120 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2122 if (vlan
->nb_guest_devs
== 0)
2123 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2124 if (vlan
->nb_host_devs
== 0)
2126 "Warning: vlan %d is not connected to host network\n",