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
,
337 VLANClientState
*vc
, **pvc
;
338 vc
= qemu_mallocz(sizeof(VLANClientState
));
339 vc
->model
= strdup(model
);
341 vc
->name
= strdup(name
);
343 vc
->name
= assign_name(vc
, model
);
344 vc
->fd_read
= fd_read
;
345 vc
->fd_can_read
= fd_can_read
;
346 vc
->cleanup
= cleanup
;
351 pvc
= &vlan
->first_client
;
358 void qemu_del_vlan_client(VLANClientState
*vc
)
360 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
376 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
378 VLANClientState
**pvc
= &vlan
->first_client
;
381 if ((*pvc
)->opaque
== opaque
)
389 int qemu_can_send_packet(VLANClientState
*vc1
)
391 VLANState
*vlan
= vc1
->vlan
;
394 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
396 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
403 int qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
405 VLANState
*vlan
= vc1
->vlan
;
413 printf("vlan %d send:\n", vlan
->id
);
414 hex_dump(stdout
, buf
, size
);
416 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
417 if (vc
!= vc1
&& !vc
->link_down
) {
418 if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
419 vc
->fd_read(vc
->opaque
, buf
, size
);
427 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
430 uint8_t buffer
[4096];
434 for (i
= 0; i
< iovcnt
; i
++) {
437 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
438 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
442 vc
->fd_read(vc
->opaque
, buffer
, offset
);
447 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
452 for (i
= 0; i
< iovcnt
; i
++)
453 offset
+= iov
[i
].iov_len
;
457 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
460 VLANState
*vlan
= vc1
->vlan
;
465 return calc_iov_length(iov
, iovcnt
);
467 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
474 len
= calc_iov_length(iov
, iovcnt
);
476 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
477 else if (vc
->fd_read
)
478 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
480 max_len
= MAX(max_len
, len
);
486 #if defined(CONFIG_SLIRP)
488 /* slirp network adapter */
490 static int slirp_inited
;
491 static int slirp_restrict
;
492 static char *slirp_ip
;
493 static VLANClientState
*slirp_vc
;
495 int slirp_can_output(void)
497 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
500 void slirp_output(const uint8_t *pkt
, int pkt_len
)
503 printf("slirp output:\n");
504 hex_dump(stdout
, pkt
, pkt_len
);
508 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
511 int slirp_is_inited(void)
516 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
519 printf("slirp input:\n");
520 hex_dump(stdout
, buf
, size
);
522 slirp_input(buf
, size
);
525 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
529 slirp_init(slirp_restrict
, slirp_ip
);
531 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
532 slirp_receive
, NULL
, NULL
, NULL
);
533 slirp_vc
->info_str
[0] = '\0';
537 void net_slirp_redir(const char *redir_str
)
542 struct in_addr guest_addr
;
543 int host_port
, guest_port
;
547 slirp_init(slirp_restrict
, slirp_ip
);
551 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
553 if (!strcmp(buf
, "tcp")) {
555 } else if (!strcmp(buf
, "udp")) {
561 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
563 host_port
= strtol(buf
, &r
, 0);
567 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
569 if (buf
[0] == '\0') {
570 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
572 if (!inet_aton(buf
, &guest_addr
))
575 guest_port
= strtol(p
, &r
, 0);
579 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
580 fprintf(stderr
, "qemu: could not set up redirection\n");
585 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
591 static char smb_dir
[1024];
593 static void erase_dir(char *dir_name
)
599 /* erase all the files in the directory */
600 if ((d
= opendir(dir_name
)) != 0) {
605 if (strcmp(de
->d_name
, ".") != 0 &&
606 strcmp(de
->d_name
, "..") != 0) {
607 snprintf(filename
, sizeof(filename
), "%s/%s",
608 smb_dir
, de
->d_name
);
609 if (unlink(filename
) != 0) /* is it a directory? */
618 /* automatic user mode samba server configuration */
619 static void smb_exit(void)
624 /* automatic user mode samba server configuration */
625 void net_slirp_smb(const char *exported_dir
)
628 char smb_cmdline
[1024];
633 slirp_init(slirp_restrict
, slirp_ip
);
636 /* XXX: better tmp dir construction */
637 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
638 if (mkdir(smb_dir
, 0700) < 0) {
639 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
642 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
644 f
= fopen(smb_conf
, "w");
646 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
653 "socket address=127.0.0.1\n"
655 "lock directory=%s\n"
656 "log file=%s/log.smbd\n"
657 "smb passwd file=%s/smbpasswd\n"
673 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
674 SMBD_COMMAND
, smb_conf
);
676 slirp_add_exec(0, smb_cmdline
, 4, 139);
679 #endif /* !defined(_WIN32) */
680 void do_info_slirp(void)
690 static int vmchannel_can_read(void *opaque
)
692 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
693 return slirp_socket_can_recv(4, vmc
->port
);
696 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
698 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
699 slirp_socket_recv(4, vmc
->port
, buf
, size
);
702 #endif /* CONFIG_SLIRP */
706 int tap_has_vnet_hdr(void *opaque
)
711 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
715 #else /* !defined(_WIN32) */
717 /* Maximum GSO packet size (64k) plus plenty of room for
718 * the ethernet and virtio_net headers
720 #define TAP_BUFSIZE (4096 + 65536)
723 #include <linux/virtio_net.h>
726 typedef struct TAPState
{
729 char down_script
[1024];
730 char down_script_arg
[128];
731 char buf
[TAP_BUFSIZE
];
733 unsigned int has_vnet_hdr
: 1;
734 unsigned int using_vnet_hdr
: 1;
737 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
739 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
742 TAPState
*s
= opaque
;
746 len
= writev(s
->fd
, iov
, iovcnt
);
747 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
752 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
758 TAPState
*s
= opaque
;
759 struct virtio_net_hdr hdr
= { 0, };
761 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
762 iov
[i
].iov_base
= &hdr
;
763 iov
[i
].iov_len
= sizeof(hdr
);
768 iov
[i
].iov_base
= (char *) buf
;
769 iov
[i
].iov_len
= size
;
772 tap_receive_iov(opaque
, iov
, i
);
775 static int tap_can_send(void *opaque
)
777 TAPState
*s
= opaque
;
781 /* Check to see if any of our clients can receive a packet */
782 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
787 if (!vc
->fd_can_read
) {
788 /* no fd_can_read handler, they always can receive */
791 can_receive
= vc
->fd_can_read(vc
->opaque
);
793 /* Once someone can receive, we try to send a packet */
801 static int tap_send_packet(TAPState
*s
)
803 uint8_t *buf
= (uint8_t *)s
->buf
;
807 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
808 buf
+= sizeof(struct virtio_net_hdr
);
809 size
-= sizeof(struct virtio_net_hdr
);
813 return qemu_send_packet(s
->vc
, buf
, size
);
816 static void tap_send(void *opaque
)
818 TAPState
*s
= opaque
;
820 /* First try to send any buffered packet */
824 /* If noone can receive the packet, buffer it */
825 err
= tap_send_packet(s
);
830 /* Read packets until we hit EAGAIN */
835 sbuf
.maxlen
= sizeof(s
->buf
);
837 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
839 // FIXME: kvm_sleep_begin();
840 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
841 // FIXME: kvm_sleep_end();
844 if (s
->size
== -1 && errno
== EINTR
)
850 /* If noone can receive the packet, buffer it */
851 err
= tap_send_packet(s
);
855 } while (s
->size
> 0);
858 int tap_has_vnet_hdr(void *opaque
)
860 VLANClientState
*vc
= opaque
;
861 TAPState
*s
= vc
->opaque
;
863 return s
? s
->has_vnet_hdr
: 0;
866 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
868 VLANClientState
*vc
= opaque
;
869 TAPState
*s
= vc
->opaque
;
871 if (!s
|| !s
->has_vnet_hdr
)
874 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
877 static int tap_probe_vnet_hdr(int fd
)
879 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
882 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
883 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
887 return ifr
.ifr_flags
& IFF_VNET_HDR
;
894 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
897 TAPState
*s
= vc
->opaque
;
898 unsigned int offload
= 0;
901 offload
|= TUN_F_CSUM
;
903 offload
|= TUN_F_TSO4
;
905 offload
|= TUN_F_TSO6
;
906 if ((tso4
|| tso6
) && ecn
)
907 offload
|= TUN_F_TSO_ECN
;
910 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
911 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
914 #endif /* TUNSETOFFLOAD */
916 static void tap_cleanup(VLANClientState
*vc
)
918 TAPState
*s
= vc
->opaque
;
920 if (s
->down_script
[0])
921 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
923 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
930 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
938 s
= qemu_mallocz(sizeof(TAPState
));
940 s
->has_vnet_hdr
= vnet_hdr
!= 0;
941 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
,
942 NULL
, tap_cleanup
, s
);
944 s
->vc
->fd_readv
= tap_receive_iov
;
947 s
->vc
->set_offload
= tap_set_offload
;
949 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
950 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
954 #if defined (_BSD) || defined (__FreeBSD_kernel__)
955 static int tap_open(char *ifname
, int ifname_size
)
961 TFR(fd
= open("/dev/tap", O_RDWR
));
963 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
968 dev
= devname(s
.st_rdev
, S_IFCHR
);
969 pstrcpy(ifname
, ifname_size
, dev
);
971 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
974 #elif defined(__sun__)
975 #define TUNNEWPPA (('T'<<16) | 0x0001)
977 * Allocate TAP device, returns opened fd.
978 * Stores dev name in the first arg(must be large enough).
980 int tap_alloc(char *dev
, size_t dev_size
)
982 int tap_fd
, if_fd
, ppa
= -1;
983 static int ip_fd
= 0;
986 static int arp_fd
= 0;
987 int ip_muxid
, arp_muxid
;
988 struct strioctl strioc_if
, strioc_ppa
;
989 int link_type
= I_PLINK
;;
991 char actual_name
[32] = "";
993 memset(&ifr
, 0x0, sizeof(ifr
));
997 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1001 /* Check if IP device was opened */
1005 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1007 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1011 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1013 syslog(LOG_ERR
, "Can't open /dev/tap");
1017 /* Assign a new PPA and get its unit number. */
1018 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1019 strioc_ppa
.ic_timout
= 0;
1020 strioc_ppa
.ic_len
= sizeof(ppa
);
1021 strioc_ppa
.ic_dp
= (char *)&ppa
;
1022 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1023 syslog (LOG_ERR
, "Can't assign new interface");
1025 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1027 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1030 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1031 syslog(LOG_ERR
, "Can't push IP module");
1035 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1036 syslog(LOG_ERR
, "Can't get flags\n");
1038 snprintf (actual_name
, 32, "tap%d", ppa
);
1039 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1042 /* Assign ppa according to the unit number returned by tun device */
1044 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1045 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1046 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1047 syslog (LOG_ERR
, "Can't get flags\n");
1048 /* Push arp module to if_fd */
1049 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1050 syslog (LOG_ERR
, "Can't push ARP module (2)");
1052 /* Push arp module to ip_fd */
1053 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1054 syslog (LOG_ERR
, "I_POP failed\n");
1055 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1056 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1058 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1060 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1062 /* Set ifname to arp */
1063 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1064 strioc_if
.ic_timout
= 0;
1065 strioc_if
.ic_len
= sizeof(ifr
);
1066 strioc_if
.ic_dp
= (char *)&ifr
;
1067 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1068 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1071 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1072 syslog(LOG_ERR
, "Can't link TAP device to IP");
1076 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1077 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1081 memset(&ifr
, 0x0, sizeof(ifr
));
1082 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1083 ifr
.lifr_ip_muxid
= ip_muxid
;
1084 ifr
.lifr_arp_muxid
= arp_muxid
;
1086 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1088 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1089 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1090 syslog (LOG_ERR
, "Can't set multiplexor id");
1093 snprintf(dev
, dev_size
, "tap%d", ppa
);
1097 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1101 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1102 fprintf(stderr
, "Cannot allocate TAP device\n");
1105 pstrcpy(ifname
, ifname_size
, dev
);
1106 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1109 #elif defined (_AIX)
1110 static int tap_open(char *ifname
, int ifname_size
)
1112 fprintf (stderr
, "no tap on AIX\n");
1116 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1121 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1123 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1126 memset(&ifr
, 0, sizeof(ifr
));
1127 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1129 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1131 unsigned int features
;
1133 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1134 features
& IFF_VNET_HDR
) {
1136 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1141 if (ifname
[0] != '\0')
1142 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1144 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1145 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1147 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1151 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1152 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1157 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1163 /* try to launch network script */
1167 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1168 for (i
= 0; i
< open_max
; i
++)
1169 if (i
!= STDIN_FILENO
&&
1170 i
!= STDOUT_FILENO
&&
1171 i
!= STDERR_FILENO
&&
1176 *parg
++ = (char *)setup_script
;
1177 *parg
++ = (char *)ifname
;
1179 execv(setup_script
, args
);
1182 while (waitpid(pid
, &status
, 0) != pid
);
1183 if (!WIFEXITED(status
) ||
1184 WEXITSTATUS(status
) != 0) {
1185 fprintf(stderr
, "%s: could not launch network script\n",
1193 static int net_tap_init(VLANState
*vlan
, const char *model
,
1194 const char *name
, const char *ifname1
,
1195 const char *setup_script
, const char *down_script
)
1202 if (ifname1
!= NULL
)
1203 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1207 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1211 if (!setup_script
|| !strcmp(setup_script
, "no"))
1213 if (setup_script
[0] != '\0') {
1214 if (launch_script(setup_script
, ifname
, fd
))
1217 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1221 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1222 "ifname=%s,script=%s,downscript=%s",
1223 ifname
, setup_script
, down_script
);
1224 if (down_script
&& strcmp(down_script
, "no")) {
1225 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1226 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1231 #endif /* !_WIN32 */
1233 #if defined(CONFIG_VDE)
1234 typedef struct VDEState
{
1235 VLANClientState
*vc
;
1239 static void vde_to_qemu(void *opaque
)
1241 VDEState
*s
= opaque
;
1245 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1247 qemu_send_packet(s
->vc
, buf
, size
);
1251 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1253 VDEState
*s
= opaque
;
1256 ret
= vde_send(s
->vde
, buf
, size
, 0);
1257 if (ret
< 0 && errno
== EINTR
) {
1264 static void vde_cleanup(VLANClientState
*vc
)
1266 VDEState
*s
= vc
->opaque
;
1267 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1272 static int net_vde_init(VLANState
*vlan
, const char *model
,
1273 const char *name
, const char *sock
,
1274 int port
, const char *group
, int mode
)
1277 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1278 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1280 struct vde_open_args args
= {
1282 .group
= init_group
,
1286 s
= qemu_mallocz(sizeof(VDEState
));
1287 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1292 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
,
1293 NULL
, vde_cleanup
, s
);
1294 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1295 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1296 sock
, vde_datafd(s
->vde
));
1301 /* network connection */
1302 typedef struct NetSocketState
{
1303 VLANClientState
*vc
;
1305 int state
; /* 0 = getting length, 1 = getting data */
1307 unsigned int packet_len
;
1309 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1312 typedef struct NetSocketListenState
{
1317 } NetSocketListenState
;
1319 /* XXX: we consider we can send the whole packet without blocking */
1320 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1322 NetSocketState
*s
= opaque
;
1326 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1327 send_all(s
->fd
, buf
, size
);
1330 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1332 NetSocketState
*s
= opaque
;
1333 sendto(s
->fd
, buf
, size
, 0,
1334 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1337 static void net_socket_send(void *opaque
)
1339 NetSocketState
*s
= opaque
;
1345 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1347 err
= socket_error();
1348 if (err
!= EWOULDBLOCK
)
1350 } else if (size
== 0) {
1351 /* end of connection */
1353 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1359 /* reassemble a packet from the network */
1365 memcpy(s
->buf
+ s
->index
, buf
, l
);
1369 if (s
->index
== 4) {
1371 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1377 l
= s
->packet_len
- s
->index
;
1380 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1381 memcpy(s
->buf
+ s
->index
, buf
, l
);
1383 fprintf(stderr
, "serious error: oversized packet received,"
1384 "connection terminated.\n");
1392 if (s
->index
>= s
->packet_len
) {
1393 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1402 static void net_socket_send_dgram(void *opaque
)
1404 NetSocketState
*s
= opaque
;
1407 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1411 /* end of connection */
1412 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1415 qemu_send_packet(s
->vc
, s
->buf
, size
);
1418 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1423 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1424 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1425 inet_ntoa(mcastaddr
->sin_addr
),
1426 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1430 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1432 perror("socket(PF_INET, SOCK_DGRAM)");
1437 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1438 (const char *)&val
, sizeof(val
));
1440 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1444 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1450 /* Add host to multicast group */
1451 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1452 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1454 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1455 (const char *)&imr
, sizeof(struct ip_mreq
));
1457 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1461 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1463 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1464 (const char *)&val
, sizeof(val
));
1466 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1470 socket_set_nonblock(fd
);
1478 static void net_socket_cleanup(VLANClientState
*vc
)
1480 NetSocketState
*s
= vc
->opaque
;
1481 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1486 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1489 int fd
, int is_connected
)
1491 struct sockaddr_in saddr
;
1493 socklen_t saddr_len
;
1496 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1497 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1498 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1502 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1504 if (saddr
.sin_addr
.s_addr
==0) {
1505 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1509 /* clone dgram socket */
1510 newfd
= net_socket_mcast_create(&saddr
);
1512 /* error already reported by net_socket_mcast_create() */
1516 /* clone newfd to fd, close newfd */
1521 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1522 fd
, strerror(errno
));
1527 s
= qemu_mallocz(sizeof(NetSocketState
));
1530 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
,
1531 NULL
, net_socket_cleanup
, s
);
1532 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1534 /* mcast: save bound address as dst */
1535 if (is_connected
) s
->dgram_dst
=saddr
;
1537 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1538 "socket: fd=%d (%s mcast=%s:%d)",
1539 fd
, is_connected
? "cloned" : "",
1540 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1544 static void net_socket_connect(void *opaque
)
1546 NetSocketState
*s
= opaque
;
1547 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1550 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1553 int fd
, int is_connected
)
1556 s
= qemu_mallocz(sizeof(NetSocketState
));
1558 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive
,
1559 NULL
, net_socket_cleanup
, s
);
1560 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1561 "socket: fd=%d", fd
);
1563 net_socket_connect(s
);
1565 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1570 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1571 const char *model
, const char *name
,
1572 int fd
, int is_connected
)
1574 int so_type
=-1, optlen
=sizeof(so_type
);
1576 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1577 (socklen_t
*)&optlen
)< 0) {
1578 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1583 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1585 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1587 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1588 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1589 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1594 static void net_socket_accept(void *opaque
)
1596 NetSocketListenState
*s
= opaque
;
1598 struct sockaddr_in saddr
;
1603 len
= sizeof(saddr
);
1604 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1605 if (fd
< 0 && errno
!= EINTR
) {
1607 } else if (fd
>= 0) {
1611 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1615 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1616 "socket: connection from %s:%d",
1617 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1621 static int net_socket_listen_init(VLANState
*vlan
,
1624 const char *host_str
)
1626 NetSocketListenState
*s
;
1628 struct sockaddr_in saddr
;
1630 if (parse_host_port(&saddr
, host_str
) < 0)
1633 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1635 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1640 socket_set_nonblock(fd
);
1642 /* allow fast reuse */
1644 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1646 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1651 ret
= listen(fd
, 0);
1657 s
->model
= strdup(model
);
1658 s
->name
= strdup(name
);
1660 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1664 static int net_socket_connect_init(VLANState
*vlan
,
1667 const char *host_str
)
1670 int fd
, connected
, ret
, err
;
1671 struct sockaddr_in saddr
;
1673 if (parse_host_port(&saddr
, host_str
) < 0)
1676 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1681 socket_set_nonblock(fd
);
1685 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1687 err
= socket_error();
1688 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1689 } else if (err
== EINPROGRESS
) {
1692 } else if (err
== WSAEALREADY
) {
1705 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1708 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1709 "socket: connect to %s:%d",
1710 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1714 static int net_socket_mcast_init(VLANState
*vlan
,
1717 const char *host_str
)
1721 struct sockaddr_in saddr
;
1723 if (parse_host_port(&saddr
, host_str
) < 0)
1727 fd
= net_socket_mcast_create(&saddr
);
1731 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1735 s
->dgram_dst
= saddr
;
1737 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1738 "socket: mcast=%s:%d",
1739 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1744 /* find or alloc a new VLAN */
1745 VLANState
*qemu_find_vlan(int id
)
1747 VLANState
**pvlan
, *vlan
;
1748 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1752 vlan
= qemu_mallocz(sizeof(VLANState
));
1755 pvlan
= &first_vlan
;
1756 while (*pvlan
!= NULL
)
1757 pvlan
= &(*pvlan
)->next
;
1762 static int nic_get_free_idx(void)
1766 for (index
= 0; index
< MAX_NICS
; index
++)
1767 if (!nd_table
[index
].used
)
1772 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1774 const char *models
[2];
1779 qemu_check_nic_model_list(nd
, models
, model
);
1782 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1783 const char *default_model
)
1785 int i
, exit_status
= 0;
1788 nd
->model
= strdup(default_model
);
1790 if (strcmp(nd
->model
, "?") != 0) {
1791 for (i
= 0 ; models
[i
]; i
++)
1792 if (strcmp(nd
->model
, models
[i
]) == 0)
1795 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1799 fprintf(stderr
, "qemu: Supported NIC models: ");
1800 for (i
= 0 ; models
[i
]; i
++)
1801 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1806 int net_client_init(const char *device
, const char *p
)
1814 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1815 vlan_id
= strtol(buf
, NULL
, 0);
1817 vlan
= qemu_find_vlan(vlan_id
);
1819 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1822 if (!strcmp(device
, "nic")) {
1825 int idx
= nic_get_free_idx();
1827 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1828 fprintf(stderr
, "Too Many NICs\n");
1832 nd
= &nd_table
[idx
];
1833 macaddr
= nd
->macaddr
;
1839 macaddr
[5] = 0x56 + idx
;
1841 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1842 if (parse_macaddr(macaddr
, buf
) < 0) {
1843 fprintf(stderr
, "invalid syntax for ethernet address\n");
1848 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1849 nd
->model
= strdup(buf
);
1856 vlan
->nb_guest_devs
++;
1859 if (!strcmp(device
, "none")) {
1860 /* does nothing. It is needed to signal that no network cards
1865 if (!strcmp(device
, "user")) {
1866 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1867 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1869 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1870 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1872 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1873 slirp_ip
= strdup(buf
);
1875 vlan
->nb_host_devs
++;
1876 ret
= net_slirp_init(vlan
, device
, name
);
1877 } else if (!strcmp(device
, "channel")) {
1879 char name
[20], *devname
;
1880 struct VMChannel
*vmc
;
1882 port
= strtol(p
, &devname
, 10);
1884 if (port
< 1 || port
> 65535) {
1885 fprintf(stderr
, "vmchannel wrong port number\n");
1889 vmc
= malloc(sizeof(struct VMChannel
));
1890 snprintf(name
, 20, "vmchannel%ld", port
);
1891 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
1893 fprintf(stderr
, "qemu: could not open vmchannel device"
1899 slirp_add_exec(3, vmc
->hd
, 4, port
);
1900 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
1906 if (!strcmp(device
, "tap")) {
1908 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1909 fprintf(stderr
, "tap: no interface name\n");
1913 vlan
->nb_host_devs
++;
1914 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1916 #elif defined (_AIX)
1918 if (!strcmp(device
, "tap")) {
1920 char setup_script
[1024], down_script
[1024];
1922 vlan
->nb_host_devs
++;
1923 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1924 fd
= strtol(buf
, NULL
, 0);
1925 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1927 if (net_tap_fd_init(vlan
, device
, name
, fd
,
1928 tap_probe_vnet_hdr(fd
)))
1931 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1934 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1935 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1937 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1938 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1940 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1944 if (!strcmp(device
, "socket")) {
1945 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1947 fd
= strtol(buf
, NULL
, 0);
1949 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1951 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1952 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1953 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1954 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1955 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1956 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1958 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1962 vlan
->nb_host_devs
++;
1965 if (!strcmp(device
, "vde")) {
1966 char vde_sock
[1024], vde_group
[512];
1967 int vde_port
, vde_mode
;
1968 vlan
->nb_host_devs
++;
1969 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1972 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1973 vde_port
= strtol(buf
, NULL
, 10);
1977 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1978 vde_group
[0] = '\0';
1980 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1981 vde_mode
= strtol(buf
, NULL
, 8);
1985 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1989 fprintf(stderr
, "Unknown network device: %s\n", device
);
1994 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
2002 void net_client_uninit(NICInfo
*nd
)
2004 nd
->vlan
->nb_guest_devs
--;
2007 free((void *)nd
->model
);
2010 static int net_host_check_device(const char *device
)
2013 const char *valid_param_list
[] = { "tap", "socket"
2021 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2022 if (!strncmp(valid_param_list
[i
], device
,
2023 strlen(valid_param_list
[i
])))
2030 void net_host_device_add(const char *device
, const char *opts
)
2032 if (!net_host_check_device(device
)) {
2033 term_printf("invalid host network device %s\n", device
);
2036 net_client_init(device
, opts
);
2039 void net_host_device_remove(int vlan_id
, const char *device
)
2042 VLANClientState
*vc
;
2044 vlan
= qemu_find_vlan(vlan_id
);
2046 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2047 if (!strcmp(vc
->name
, device
))
2051 term_printf("can't find device %s\n", device
);
2054 qemu_del_vlan_client(vc
);
2057 int net_client_parse(const char *str
)
2065 while (*p
!= '\0' && *p
!= ',') {
2066 if ((q
- device
) < sizeof(device
) - 1)
2074 return net_client_init(device
, p
);
2077 void do_info_network(void)
2080 VLANClientState
*vc
;
2082 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2083 term_printf("VLAN %d devices:\n", vlan
->id
);
2084 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2085 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
2089 int do_set_link(const char *name
, const char *up_or_down
)
2092 VLANClientState
*vc
= NULL
;
2094 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2095 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2096 if (strcmp(vc
->name
, name
) == 0)
2101 term_printf("could not find network device '%s'", name
);
2105 if (strcmp(up_or_down
, "up") == 0)
2107 else if (strcmp(up_or_down
, "down") == 0)
2110 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
2113 if (vc
->link_status_changed
)
2114 vc
->link_status_changed(vc
);
2119 void net_cleanup(void)
2123 /* close network clients */
2124 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2125 VLANClientState
*vc
= vlan
->first_client
;
2128 VLANClientState
*next
= vc
->next
;
2130 qemu_del_vlan_client(vc
);
2137 void net_client_check(void)
2141 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2142 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2144 if (vlan
->nb_guest_devs
== 0)
2145 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2146 if (vlan
->nb_host_devs
== 0)
2148 "Warning: vlan %d is not connected to host network\n",