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 #include "qemu-common.h"
117 #include "qemu-timer.h"
118 #include "qemu-char.h"
119 #include "audio/audio.h"
120 #include "qemu_socket.h"
121 #include "qemu-log.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
,
340 VLANClientState
*vc
, **pvc
;
341 vc
= qemu_mallocz(sizeof(VLANClientState
));
342 vc
->model
= strdup(model
);
344 vc
->name
= strdup(name
);
346 vc
->name
= assign_name(vc
, model
);
347 vc
->fd_read
= fd_read
;
348 vc
->fd_can_read
= fd_can_read
;
349 vc
->cleanup
= cleanup
;
354 pvc
= &vlan
->first_client
;
361 void qemu_del_vlan_client(VLANClientState
*vc
)
363 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
379 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
381 VLANClientState
**pvc
= &vlan
->first_client
;
384 if ((*pvc
)->opaque
== opaque
)
392 int qemu_can_send_packet(VLANClientState
*vc1
)
394 VLANState
*vlan
= vc1
->vlan
;
397 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
399 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
406 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
408 VLANState
*vlan
= vc1
->vlan
;
415 printf("vlan %d send:\n", vlan
->id
);
416 hex_dump(stdout
, buf
, size
);
418 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
419 if (vc
!= vc1
&& !vc
->link_down
) {
420 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
, 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.%ld", (long)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 typedef struct TAPState
{
707 char down_script
[1024];
708 char down_script_arg
[128];
711 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
713 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
716 TAPState
*s
= opaque
;
720 len
= writev(s
->fd
, iov
, iovcnt
);
721 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
726 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
728 TAPState
*s
= opaque
;
731 ret
= write(s
->fd
, buf
, size
);
732 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
739 static void tap_send(void *opaque
)
741 TAPState
*s
= opaque
;
748 sbuf
.maxlen
= sizeof(buf
);
749 sbuf
.buf
= (char *)buf
;
750 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
752 size
= read(s
->fd
, buf
, sizeof(buf
));
755 qemu_send_packet(s
->vc
, buf
, size
);
759 static void tap_cleanup(VLANClientState
*vc
)
761 TAPState
*s
= vc
->opaque
;
763 if (s
->down_script
[0])
764 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
766 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
773 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
780 s
= qemu_mallocz(sizeof(TAPState
));
782 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
,
783 NULL
, tap_cleanup
, s
);
784 s
->vc
->fd_readv
= tap_receive_iov
;
785 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
786 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
790 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
791 static int tap_open(char *ifname
, int ifname_size
)
797 TFR(fd
= open("/dev/tap", O_RDWR
));
799 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
804 dev
= devname(s
.st_rdev
, S_IFCHR
);
805 pstrcpy(ifname
, ifname_size
, dev
);
807 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
810 #elif defined(__sun__)
811 #define TUNNEWPPA (('T'<<16) | 0x0001)
813 * Allocate TAP device, returns opened fd.
814 * Stores dev name in the first arg(must be large enough).
816 static int tap_alloc(char *dev
, size_t dev_size
)
818 int tap_fd
, if_fd
, ppa
= -1;
819 static int ip_fd
= 0;
822 static int arp_fd
= 0;
823 int ip_muxid
, arp_muxid
;
824 struct strioctl strioc_if
, strioc_ppa
;
825 int link_type
= I_PLINK
;;
827 char actual_name
[32] = "";
829 memset(&ifr
, 0x0, sizeof(ifr
));
833 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
837 /* Check if IP device was opened */
841 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
843 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
847 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
849 syslog(LOG_ERR
, "Can't open /dev/tap");
853 /* Assign a new PPA and get its unit number. */
854 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
855 strioc_ppa
.ic_timout
= 0;
856 strioc_ppa
.ic_len
= sizeof(ppa
);
857 strioc_ppa
.ic_dp
= (char *)&ppa
;
858 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
859 syslog (LOG_ERR
, "Can't assign new interface");
861 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
863 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
866 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
867 syslog(LOG_ERR
, "Can't push IP module");
871 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
872 syslog(LOG_ERR
, "Can't get flags\n");
874 snprintf (actual_name
, 32, "tap%d", ppa
);
875 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
878 /* Assign ppa according to the unit number returned by tun device */
880 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
881 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
882 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
883 syslog (LOG_ERR
, "Can't get flags\n");
884 /* Push arp module to if_fd */
885 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
886 syslog (LOG_ERR
, "Can't push ARP module (2)");
888 /* Push arp module to ip_fd */
889 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
890 syslog (LOG_ERR
, "I_POP failed\n");
891 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
892 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
894 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
896 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
898 /* Set ifname to arp */
899 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
900 strioc_if
.ic_timout
= 0;
901 strioc_if
.ic_len
= sizeof(ifr
);
902 strioc_if
.ic_dp
= (char *)&ifr
;
903 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
904 syslog (LOG_ERR
, "Can't set ifname to arp\n");
907 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
908 syslog(LOG_ERR
, "Can't link TAP device to IP");
912 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
913 syslog (LOG_ERR
, "Can't link TAP device to ARP");
917 memset(&ifr
, 0x0, sizeof(ifr
));
918 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
919 ifr
.lifr_ip_muxid
= ip_muxid
;
920 ifr
.lifr_arp_muxid
= arp_muxid
;
922 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
924 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
925 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
926 syslog (LOG_ERR
, "Can't set multiplexor id");
929 snprintf(dev
, dev_size
, "tap%d", ppa
);
933 static int tap_open(char *ifname
, int ifname_size
)
937 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
938 fprintf(stderr
, "Cannot allocate TAP device\n");
941 pstrcpy(ifname
, ifname_size
, dev
);
942 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
946 static int tap_open(char *ifname
, int ifname_size
)
948 fprintf (stderr
, "no tap on AIX\n");
952 static int tap_open(char *ifname
, int ifname_size
)
957 TFR(fd
= open("/dev/net/tun", O_RDWR
));
959 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
962 memset(&ifr
, 0, sizeof(ifr
));
963 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
964 if (ifname
[0] != '\0')
965 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
967 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
968 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
970 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
974 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
975 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
980 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
986 /* try to launch network script */
990 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
991 for (i
= 0; i
< open_max
; i
++)
992 if (i
!= STDIN_FILENO
&&
993 i
!= STDOUT_FILENO
&&
994 i
!= STDERR_FILENO
&&
999 *parg
++ = (char *)setup_script
;
1000 *parg
++ = (char *)ifname
;
1002 execv(setup_script
, args
);
1005 while (waitpid(pid
, &status
, 0) != pid
);
1006 if (!WIFEXITED(status
) ||
1007 WEXITSTATUS(status
) != 0) {
1008 fprintf(stderr
, "%s: could not launch network script\n",
1016 static int net_tap_init(VLANState
*vlan
, const char *model
,
1017 const char *name
, const char *ifname1
,
1018 const char *setup_script
, const char *down_script
)
1024 if (ifname1
!= NULL
)
1025 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1028 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
1032 if (!setup_script
|| !strcmp(setup_script
, "no"))
1034 if (setup_script
[0] != '\0') {
1035 if (launch_script(setup_script
, ifname
, fd
))
1038 s
= net_tap_fd_init(vlan
, model
, name
, fd
);
1039 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1040 "ifname=%s,script=%s,downscript=%s",
1041 ifname
, setup_script
, down_script
);
1042 if (down_script
&& strcmp(down_script
, "no")) {
1043 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1044 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1049 #endif /* !_WIN32 */
1051 #if defined(CONFIG_VDE)
1052 typedef struct VDEState
{
1053 VLANClientState
*vc
;
1057 static void vde_to_qemu(void *opaque
)
1059 VDEState
*s
= opaque
;
1063 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1065 qemu_send_packet(s
->vc
, buf
, size
);
1069 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1071 VDEState
*s
= opaque
;
1074 ret
= vde_send(s
->vde
, buf
, size
, 0);
1075 if (ret
< 0 && errno
== EINTR
) {
1082 static void vde_cleanup(VLANClientState
*vc
)
1084 VDEState
*s
= vc
->opaque
;
1085 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1090 static int net_vde_init(VLANState
*vlan
, const char *model
,
1091 const char *name
, const char *sock
,
1092 int port
, const char *group
, int mode
)
1095 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1096 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1098 struct vde_open_args args
= {
1100 .group
= init_group
,
1104 s
= qemu_mallocz(sizeof(VDEState
));
1105 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1110 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
,
1111 NULL
, vde_cleanup
, s
);
1112 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1113 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1114 sock
, vde_datafd(s
->vde
));
1119 /* network connection */
1120 typedef struct NetSocketState
{
1121 VLANClientState
*vc
;
1123 int state
; /* 0 = getting length, 1 = getting data */
1125 unsigned int packet_len
;
1127 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1130 typedef struct NetSocketListenState
{
1135 } NetSocketListenState
;
1137 /* XXX: we consider we can send the whole packet without blocking */
1138 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1140 NetSocketState
*s
= opaque
;
1144 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1145 send_all(s
->fd
, buf
, size
);
1148 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1150 NetSocketState
*s
= opaque
;
1151 sendto(s
->fd
, buf
, size
, 0,
1152 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1155 static void net_socket_send(void *opaque
)
1157 NetSocketState
*s
= opaque
;
1163 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1165 err
= socket_error();
1166 if (err
!= EWOULDBLOCK
)
1168 } else if (size
== 0) {
1169 /* end of connection */
1171 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1177 /* reassemble a packet from the network */
1183 memcpy(s
->buf
+ s
->index
, buf
, l
);
1187 if (s
->index
== 4) {
1189 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1195 l
= s
->packet_len
- s
->index
;
1198 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1199 memcpy(s
->buf
+ s
->index
, buf
, l
);
1201 fprintf(stderr
, "serious error: oversized packet received,"
1202 "connection terminated.\n");
1210 if (s
->index
>= s
->packet_len
) {
1211 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1220 static void net_socket_send_dgram(void *opaque
)
1222 NetSocketState
*s
= opaque
;
1225 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1229 /* end of connection */
1230 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1233 qemu_send_packet(s
->vc
, s
->buf
, size
);
1236 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1241 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1242 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1243 inet_ntoa(mcastaddr
->sin_addr
),
1244 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1248 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1250 perror("socket(PF_INET, SOCK_DGRAM)");
1255 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1256 (const char *)&val
, sizeof(val
));
1258 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1262 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1268 /* Add host to multicast group */
1269 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1270 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1272 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1273 (const char *)&imr
, sizeof(struct ip_mreq
));
1275 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1279 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1281 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1282 (const char *)&val
, sizeof(val
));
1284 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1288 socket_set_nonblock(fd
);
1296 static void net_socket_cleanup(VLANClientState
*vc
)
1298 NetSocketState
*s
= vc
->opaque
;
1299 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1304 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1307 int fd
, int is_connected
)
1309 struct sockaddr_in saddr
;
1311 socklen_t saddr_len
;
1314 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1315 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1316 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1320 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1322 if (saddr
.sin_addr
.s_addr
==0) {
1323 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1327 /* clone dgram socket */
1328 newfd
= net_socket_mcast_create(&saddr
);
1330 /* error already reported by net_socket_mcast_create() */
1334 /* clone newfd to fd, close newfd */
1339 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1340 fd
, strerror(errno
));
1345 s
= qemu_mallocz(sizeof(NetSocketState
));
1348 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
,
1349 NULL
, net_socket_cleanup
, s
);
1350 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1352 /* mcast: save bound address as dst */
1353 if (is_connected
) s
->dgram_dst
=saddr
;
1355 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1356 "socket: fd=%d (%s mcast=%s:%d)",
1357 fd
, is_connected
? "cloned" : "",
1358 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1362 static void net_socket_connect(void *opaque
)
1364 NetSocketState
*s
= opaque
;
1365 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1368 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1371 int fd
, int is_connected
)
1374 s
= qemu_mallocz(sizeof(NetSocketState
));
1376 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive
,
1377 NULL
, net_socket_cleanup
, s
);
1378 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1379 "socket: fd=%d", fd
);
1381 net_socket_connect(s
);
1383 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1388 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1389 const char *model
, const char *name
,
1390 int fd
, int is_connected
)
1392 int so_type
=-1, optlen
=sizeof(so_type
);
1394 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1395 (socklen_t
*)&optlen
)< 0) {
1396 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1401 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1403 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1405 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1406 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1407 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1412 static void net_socket_accept(void *opaque
)
1414 NetSocketListenState
*s
= opaque
;
1416 struct sockaddr_in saddr
;
1421 len
= sizeof(saddr
);
1422 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1423 if (fd
< 0 && errno
!= EINTR
) {
1425 } else if (fd
>= 0) {
1429 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1433 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1434 "socket: connection from %s:%d",
1435 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1439 static int net_socket_listen_init(VLANState
*vlan
,
1442 const char *host_str
)
1444 NetSocketListenState
*s
;
1446 struct sockaddr_in saddr
;
1448 if (parse_host_port(&saddr
, host_str
) < 0)
1451 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1453 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1458 socket_set_nonblock(fd
);
1460 /* allow fast reuse */
1462 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1464 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1469 ret
= listen(fd
, 0);
1475 s
->model
= strdup(model
);
1476 s
->name
= name
? strdup(name
) : NULL
;
1478 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1482 static int net_socket_connect_init(VLANState
*vlan
,
1485 const char *host_str
)
1488 int fd
, connected
, ret
, err
;
1489 struct sockaddr_in saddr
;
1491 if (parse_host_port(&saddr
, host_str
) < 0)
1494 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1499 socket_set_nonblock(fd
);
1503 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1505 err
= socket_error();
1506 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1507 } else if (err
== EINPROGRESS
) {
1510 } else if (err
== WSAEALREADY
) {
1523 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1526 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1527 "socket: connect to %s:%d",
1528 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1532 static int net_socket_mcast_init(VLANState
*vlan
,
1535 const char *host_str
)
1539 struct sockaddr_in saddr
;
1541 if (parse_host_port(&saddr
, host_str
) < 0)
1545 fd
= net_socket_mcast_create(&saddr
);
1549 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1553 s
->dgram_dst
= saddr
;
1555 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1556 "socket: mcast=%s:%d",
1557 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1562 typedef struct DumpState
{
1563 VLANClientState
*pcap_vc
;
1568 #define PCAP_MAGIC 0xa1b2c3d4
1570 struct pcap_file_hdr
{
1572 uint16_t version_major
;
1573 uint16_t version_minor
;
1580 struct pcap_sf_pkthdr
{
1589 static void dump_receive(void *opaque
, const uint8_t *buf
, int size
)
1591 DumpState
*s
= opaque
;
1592 struct pcap_sf_pkthdr hdr
;
1596 /* Early return in case of previous error. */
1601 ts
= muldiv64 (qemu_get_clock(vm_clock
),1000000, ticks_per_sec
);
1602 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1604 hdr
.ts
.tv_sec
= ts
/ 1000000000LL;
1605 hdr
.ts
.tv_usec
= ts
% 1000000;
1606 hdr
.caplen
= caplen
;
1608 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1609 write(s
->fd
, buf
, caplen
) != caplen
) {
1610 qemu_log("-net dump write error - stop dump\n");
1616 static void net_dump_cleanup(VLANClientState
*vc
)
1618 DumpState
*s
= vc
->opaque
;
1624 static int net_dump_init(VLANState
*vlan
, const char *device
,
1625 const char *name
, const char *filename
, int len
)
1627 struct pcap_file_hdr hdr
;
1630 s
= qemu_malloc(sizeof(DumpState
));
1632 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
, 0644);
1634 fprintf(stderr
, "-net dump: can't open %s\n", filename
);
1638 s
->pcap_caplen
= len
;
1640 hdr
.magic
= PCAP_MAGIC
;
1641 hdr
.version_major
= 2;
1642 hdr
.version_minor
= 4;
1645 hdr
.snaplen
= s
->pcap_caplen
;
1648 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
1649 perror("-net dump write error");
1655 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, dump_receive
, NULL
,
1656 net_dump_cleanup
, s
);
1657 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
1658 "dump to %s (len=%d)", filename
, len
);
1662 /* find or alloc a new VLAN */
1663 VLANState
*qemu_find_vlan(int id
)
1665 VLANState
**pvlan
, *vlan
;
1666 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1670 vlan
= qemu_mallocz(sizeof(VLANState
));
1673 pvlan
= &first_vlan
;
1674 while (*pvlan
!= NULL
)
1675 pvlan
= &(*pvlan
)->next
;
1680 static int nic_get_free_idx(void)
1684 for (index
= 0; index
< MAX_NICS
; index
++)
1685 if (!nd_table
[index
].used
)
1690 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1692 const char *models
[2];
1697 qemu_check_nic_model_list(nd
, models
, model
);
1700 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1701 const char *default_model
)
1703 int i
, exit_status
= 0;
1706 nd
->model
= strdup(default_model
);
1708 if (strcmp(nd
->model
, "?") != 0) {
1709 for (i
= 0 ; models
[i
]; i
++)
1710 if (strcmp(nd
->model
, models
[i
]) == 0)
1713 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1717 fprintf(stderr
, "qemu: Supported NIC models: ");
1718 for (i
= 0 ; models
[i
]; i
++)
1719 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1724 int net_client_init(const char *device
, const char *p
)
1726 static const char * const fd_params
[] = {
1727 "vlan", "name", "fd", NULL
1735 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1736 vlan_id
= strtol(buf
, NULL
, 0);
1738 vlan
= qemu_find_vlan(vlan_id
);
1740 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1743 if (!strcmp(device
, "nic")) {
1744 static const char * const nic_params
[] = {
1745 "vlan", "name", "macaddr", "model", NULL
1749 int idx
= nic_get_free_idx();
1751 if (check_params(buf
, sizeof(buf
), nic_params
, p
) < 0) {
1752 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1756 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1757 fprintf(stderr
, "Too Many NICs\n");
1761 nd
= &nd_table
[idx
];
1762 macaddr
= nd
->macaddr
;
1768 macaddr
[5] = 0x56 + idx
;
1770 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1771 if (parse_macaddr(macaddr
, buf
) < 0) {
1772 fprintf(stderr
, "invalid syntax for ethernet address\n");
1777 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1778 nd
->model
= strdup(buf
);
1785 vlan
->nb_guest_devs
++;
1788 if (!strcmp(device
, "none")) {
1790 fprintf(stderr
, "qemu: 'none' takes no parameters\n");
1793 /* does nothing. It is needed to signal that no network cards
1798 if (!strcmp(device
, "user")) {
1799 static const char * const slirp_params
[] = {
1800 "vlan", "name", "hostname", "restrict", "ip", NULL
1802 if (check_params(buf
, sizeof(buf
), slirp_params
, p
) < 0) {
1803 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1807 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1808 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1810 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1811 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1813 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1814 slirp_ip
= strdup(buf
);
1816 vlan
->nb_host_devs
++;
1817 ret
= net_slirp_init(vlan
, device
, name
);
1818 } else if (!strcmp(device
, "channel")) {
1820 char name
[20], *devname
;
1821 struct VMChannel
*vmc
;
1823 port
= strtol(p
, &devname
, 10);
1825 if (port
< 1 || port
> 65535) {
1826 fprintf(stderr
, "vmchannel wrong port number\n");
1830 vmc
= malloc(sizeof(struct VMChannel
));
1831 snprintf(name
, 20, "vmchannel%ld", port
);
1832 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
1834 fprintf(stderr
, "qemu: could not open vmchannel device"
1840 slirp_add_exec(3, vmc
->hd
, 4, port
);
1841 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
1847 if (!strcmp(device
, "tap")) {
1848 static const char * const tap_params
[] = {
1849 "vlan", "name", "ifname", NULL
1853 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
1854 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1858 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1859 fprintf(stderr
, "tap: no interface name\n");
1863 vlan
->nb_host_devs
++;
1864 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1866 #elif defined (_AIX)
1868 if (!strcmp(device
, "tap")) {
1870 char setup_script
[1024], down_script
[1024];
1872 vlan
->nb_host_devs
++;
1873 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1874 if (check_params(buf
, sizeof(buf
), fd_params
, p
) < 0) {
1875 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1879 fd
= strtol(buf
, NULL
, 0);
1880 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1881 net_tap_fd_init(vlan
, device
, name
, fd
);
1884 static const char * const tap_params
[] = {
1885 "vlan", "name", "ifname", "script", "downscript", NULL
1887 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
1888 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1892 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1895 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1896 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1898 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1899 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1901 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1905 if (!strcmp(device
, "socket")) {
1906 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1908 if (check_params(buf
, sizeof(buf
), fd_params
, p
) < 0) {
1909 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
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 static const char * const listen_params
[] = {
1919 "vlan", "name", "listen", NULL
1921 if (check_params(buf
, sizeof(buf
), listen_params
, p
) < 0) {
1922 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1926 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1927 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1928 static const char * const connect_params
[] = {
1929 "vlan", "name", "connect", NULL
1931 if (check_params(buf
, sizeof(buf
), connect_params
, p
) < 0) {
1932 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1936 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1937 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1938 static const char * const mcast_params
[] = {
1939 "vlan", "name", "mcast", NULL
1941 if (check_params(buf
, sizeof(buf
), mcast_params
, p
) < 0) {
1942 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1946 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1948 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1952 vlan
->nb_host_devs
++;
1955 if (!strcmp(device
, "vde")) {
1956 static const char * const vde_params
[] = {
1957 "vlan", "name", "sock", "port", "group", "mode", NULL
1959 char vde_sock
[1024], vde_group
[512];
1960 int vde_port
, vde_mode
;
1962 if (check_params(buf
, sizeof(buf
), vde_params
, p
) < 0) {
1963 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1967 vlan
->nb_host_devs
++;
1968 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1971 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1972 vde_port
= strtol(buf
, NULL
, 10);
1976 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1977 vde_group
[0] = '\0';
1979 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1980 vde_mode
= strtol(buf
, NULL
, 8);
1984 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1987 if (!strcmp(device
, "dump")) {
1990 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
1991 len
= strtol(buf
, NULL
, 0);
1993 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
1994 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
1996 ret
= net_dump_init(vlan
, device
, name
, buf
, len
);
1998 fprintf(stderr
, "Unknown network device: %s\n", device
);
2003 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
2011 void net_client_uninit(NICInfo
*nd
)
2013 nd
->vlan
->nb_guest_devs
--;
2016 free((void *)nd
->model
);
2019 static int net_host_check_device(const char *device
)
2022 const char *valid_param_list
[] = { "tap", "socket", "dump"
2030 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2031 if (!strncmp(valid_param_list
[i
], device
,
2032 strlen(valid_param_list
[i
])))
2039 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2041 if (!net_host_check_device(device
)) {
2042 monitor_printf(mon
, "invalid host network device %s\n", device
);
2045 net_client_init(device
, opts
);
2048 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2051 VLANClientState
*vc
;
2053 vlan
= qemu_find_vlan(vlan_id
);
2055 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2056 if (!strcmp(vc
->name
, device
)) {
2062 monitor_printf(mon
, "can't find device %s\n", device
);
2065 if (!net_host_check_device(vc
->model
)) {
2066 monitor_printf(mon
, "invalid host network device %s\n", device
);
2069 qemu_del_vlan_client(vc
);
2072 int net_client_parse(const char *str
)
2080 while (*p
!= '\0' && *p
!= ',') {
2081 if ((q
- device
) < sizeof(device
) - 1)
2089 return net_client_init(device
, p
);
2092 void do_info_network(Monitor
*mon
)
2095 VLANClientState
*vc
;
2097 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2098 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2099 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2100 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2104 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2107 VLANClientState
*vc
= NULL
;
2109 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2110 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2111 if (strcmp(vc
->name
, name
) == 0)
2116 monitor_printf(mon
, "could not find network device '%s'", name
);
2120 if (strcmp(up_or_down
, "up") == 0)
2122 else if (strcmp(up_or_down
, "down") == 0)
2125 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2126 "valid\n", up_or_down
);
2128 if (vc
->link_status_changed
)
2129 vc
->link_status_changed(vc
);
2134 void net_cleanup(void)
2138 /* close network clients */
2139 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2140 VLANClientState
*vc
= vlan
->first_client
;
2143 VLANClientState
*next
= vc
->next
;
2145 qemu_del_vlan_client(vc
);
2152 void net_client_check(void)
2156 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2157 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2159 if (vlan
->nb_guest_devs
== 0)
2160 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2161 if (vlan
->nb_host_devs
== 0)
2163 "Warning: vlan %d is not connected to host network\n",