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 NetCanReceive
*can_receive
,
337 NetReceiveIOV
*receive_iov
,
341 VLANClientState
*vc
, **pvc
;
342 vc
= qemu_mallocz(sizeof(VLANClientState
));
343 vc
->model
= strdup(model
);
345 vc
->name
= strdup(name
);
347 vc
->name
= assign_name(vc
, model
);
348 vc
->can_receive
= can_receive
;
349 vc
->receive
= receive
;
350 vc
->receive_iov
= receive_iov
;
351 vc
->cleanup
= cleanup
;
356 pvc
= &vlan
->first_client
;
363 void qemu_del_vlan_client(VLANClientState
*vc
)
365 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
381 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
383 VLANClientState
**pvc
= &vlan
->first_client
;
386 if ((*pvc
)->opaque
== opaque
)
394 int qemu_can_send_packet(VLANClientState
*sender
)
396 VLANState
*vlan
= sender
->vlan
;
399 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
404 /* no can_receive() handler, they can always receive */
405 if (!vc
->can_receive
|| vc
->can_receive(vc
->opaque
)) {
413 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
417 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
418 if (vc
!= sender
&& !vc
->link_down
) {
419 vc
->receive(vc
->opaque
, buf
, size
);
424 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
426 VLANState
*vlan
= vc
->vlan
;
433 printf("vlan %d send:\n", vlan
->id
);
434 hex_dump(stdout
, buf
, size
);
436 if (vlan
->delivering
) {
437 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
438 packet
->next
= vlan
->send_queue
;
441 memcpy(packet
->data
, buf
, size
);
442 vlan
->send_queue
= packet
;
444 vlan
->delivering
= 1;
445 qemu_deliver_packet(vc
, buf
, size
);
446 while ((packet
= vlan
->send_queue
) != NULL
) {
447 vlan
->send_queue
= packet
->next
;
448 qemu_deliver_packet(packet
->sender
, packet
->data
, packet
->size
);
451 vlan
->delivering
= 0;
455 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
458 uint8_t buffer
[4096];
462 for (i
= 0; i
< iovcnt
; i
++) {
465 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
466 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
470 vc
->receive(vc
->opaque
, buffer
, offset
);
475 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
480 for (i
= 0; i
< iovcnt
; i
++)
481 offset
+= iov
[i
].iov_len
;
485 ssize_t
qemu_sendv_packet(VLANClientState
*sender
, const struct iovec
*iov
,
488 VLANState
*vlan
= sender
->vlan
;
494 if (sender
->link_down
)
495 return calc_iov_length(iov
, iovcnt
);
497 if (vlan
->delivering
) {
498 max_len
= calc_iov_length(iov
, iovcnt
);
500 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
501 packet
->next
= vlan
->send_queue
;
502 packet
->sender
= sender
;
504 for (i
= 0; i
< iovcnt
; i
++) {
505 size_t len
= iov
[i
].iov_len
;
507 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
510 vlan
->send_queue
= packet
;
512 vlan
->delivering
= 1;
514 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
521 len
= calc_iov_length(iov
, iovcnt
);
522 } else if (vc
->receive_iov
) {
523 len
= vc
->receive_iov(vc
->opaque
, iov
, iovcnt
);
524 } else if (vc
->receive
) {
525 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
527 max_len
= MAX(max_len
, len
);
530 while ((packet
= vlan
->send_queue
) != NULL
) {
531 vlan
->send_queue
= packet
->next
;
532 qemu_deliver_packet(packet
->sender
, packet
->data
, packet
->size
);
535 vlan
->delivering
= 0;
541 static void config_error(Monitor
*mon
, const char *fmt
, ...)
547 monitor_vprintf(mon
, fmt
, ap
);
549 fprintf(stderr
, "qemu: ");
550 vfprintf(stderr
, fmt
, ap
);
556 #if defined(CONFIG_SLIRP)
558 /* slirp network adapter */
560 struct slirp_config_str
{
561 struct slirp_config_str
*next
;
565 static int slirp_inited
;
566 static struct slirp_config_str
*slirp_redirs
;
568 static const char *slirp_smb_export
;
570 static VLANClientState
*slirp_vc
;
572 static void slirp_smb(const char *exported_dir
);
573 static void slirp_redirection(Monitor
*mon
, const char *redir_str
);
575 int slirp_can_output(void)
577 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
580 void slirp_output(const uint8_t *pkt
, int pkt_len
)
583 printf("slirp output:\n");
584 hex_dump(stdout
, pkt
, pkt_len
);
588 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
591 int slirp_is_inited(void)
596 static void slirp_receive(void *opaque
, const uint8_t *buf
, size_t size
)
599 printf("slirp input:\n");
600 hex_dump(stdout
, buf
, size
);
602 slirp_input(buf
, size
);
605 static int slirp_in_use
;
607 static void net_slirp_cleanup(VLANClientState
*vc
)
612 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
,
613 int restricted
, const char *ip
)
616 /* slirp only supports a single instance so far */
621 slirp_init(restricted
, ip
);
623 while (slirp_redirs
) {
624 struct slirp_config_str
*config
= slirp_redirs
;
626 slirp_redirection(NULL
, config
->str
);
627 slirp_redirs
= config
->next
;
631 if (slirp_smb_export
) {
632 slirp_smb(slirp_smb_export
);
637 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
,
638 NULL
, net_slirp_cleanup
, NULL
);
639 slirp_vc
->info_str
[0] = '\0';
644 static void net_slirp_redir_print(void *opaque
, int is_udp
,
645 struct in_addr
*laddr
, u_int lport
,
646 struct in_addr
*faddr
, u_int fport
)
648 Monitor
*mon
= (Monitor
*)opaque
;
653 h_addr
= ntohl(faddr
->s_addr
);
654 g_addr
= ntohl(laddr
->s_addr
);
656 monitor_printf(mon
, " %s |", is_udp
? "udp" : "tcp" );
657 snprintf(buf
, 15, "%d.%d.%d.%d", (h_addr
>> 24) & 0xff,
658 (h_addr
>> 16) & 0xff,
659 (h_addr
>> 8) & 0xff,
661 monitor_printf(mon
, " %15s |", buf
);
662 monitor_printf(mon
, " %5d |", fport
);
664 snprintf(buf
, 15, "%d.%d.%d.%d", (g_addr
>> 24) & 0xff,
665 (g_addr
>> 16) & 0xff,
666 (g_addr
>> 8) & 0xff,
668 monitor_printf(mon
, " %15s |", buf
);
669 monitor_printf(mon
, " %5d\n", lport
);
673 static void net_slirp_redir_list(Monitor
*mon
)
678 monitor_printf(mon
, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
679 monitor_printf(mon
, " | | | | \n");
680 slirp_redir_loop(net_slirp_redir_print
, mon
);
683 static void net_slirp_redir_rm(Monitor
*mon
, const char *port_str
)
687 const char *p
= port_str
;
694 if (!port_str
|| !port_str
[0])
697 get_str_sep(buf
, sizeof(buf
), &p
, ':');
699 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
701 } else if (!strcmp(buf
, "udp")) {
709 n
= slirp_redir_rm(is_udp
, host_port
);
711 monitor_printf(mon
, "removed %d redirections to %s port %d\n", n
,
712 is_udp
? "udp" : "tcp", host_port
);
716 monitor_printf(mon
, "invalid format\n");
719 static void slirp_redirection(Monitor
*mon
, const char *redir_str
)
721 struct in_addr guest_addr
;
722 int host_port
, guest_port
;
728 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
731 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
733 } else if (!strcmp(buf
, "udp")) {
739 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
742 host_port
= strtol(buf
, &r
, 0);
747 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
750 if (buf
[0] == '\0') {
751 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
753 if (!inet_aton(buf
, &guest_addr
)) {
757 guest_port
= strtol(p
, &r
, 0);
762 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
763 config_error(mon
, "could not set up redirection '%s'\n", redir_str
);
768 config_error(mon
, "invalid redirection format '%s'\n", redir_str
);
771 void net_slirp_redir(Monitor
*mon
, const char *redir_str
, const char *redir_opt2
)
773 struct slirp_config_str
*config
;
777 monitor_printf(mon
, "user mode network stack not in use\n");
779 config
= qemu_malloc(sizeof(*config
));
780 config
->str
= redir_str
;
781 config
->next
= slirp_redirs
;
782 slirp_redirs
= config
;
787 if (!strcmp(redir_str
, "remove")) {
788 net_slirp_redir_rm(mon
, redir_opt2
);
792 if (!strcmp(redir_str
, "list")) {
793 net_slirp_redir_list(mon
);
797 slirp_redirection(mon
, redir_str
);
802 static char smb_dir
[1024];
804 static void erase_dir(char *dir_name
)
810 /* erase all the files in the directory */
811 if ((d
= opendir(dir_name
)) != NULL
) {
816 if (strcmp(de
->d_name
, ".") != 0 &&
817 strcmp(de
->d_name
, "..") != 0) {
818 snprintf(filename
, sizeof(filename
), "%s/%s",
819 smb_dir
, de
->d_name
);
820 if (unlink(filename
) != 0) /* is it a directory? */
829 /* automatic user mode samba server configuration */
830 static void smb_exit(void)
835 static void slirp_smb(const char *exported_dir
)
838 char smb_cmdline
[1024];
841 /* XXX: better tmp dir construction */
842 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%ld", (long)getpid());
843 if (mkdir(smb_dir
, 0700) < 0) {
844 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
847 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
849 f
= fopen(smb_conf
, "w");
851 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
858 "socket address=127.0.0.1\n"
860 "lock directory=%s\n"
861 "log file=%s/log.smbd\n"
862 "smb passwd file=%s/smbpasswd\n"
878 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
879 SMBD_COMMAND
, smb_conf
);
881 slirp_add_exec(0, smb_cmdline
, 4, 139);
884 /* automatic user mode samba server configuration */
885 void net_slirp_smb(const char *exported_dir
)
887 if (slirp_smb_export
) {
888 fprintf(stderr
, "-smb given twice\n");
891 slirp_smb_export
= exported_dir
;
893 slirp_smb(exported_dir
);
897 #endif /* !defined(_WIN32) */
899 void do_info_slirp(Monitor
*mon
)
909 static int vmchannel_can_read(void *opaque
)
911 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
912 return slirp_socket_can_recv(4, vmc
->port
);
915 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
917 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
918 slirp_socket_recv(4, vmc
->port
, buf
, size
);
921 #endif /* CONFIG_SLIRP */
925 typedef struct TAPState
{
928 char down_script
[1024];
929 char down_script_arg
[128];
933 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
935 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
938 TAPState
*s
= opaque
;
942 len
= writev(s
->fd
, iov
, iovcnt
);
943 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
948 static void tap_receive(void *opaque
, const uint8_t *buf
, size_t size
)
950 TAPState
*s
= opaque
;
953 ret
= write(s
->fd
, buf
, size
);
954 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
961 static int tap_can_send(void *opaque
)
963 TAPState
*s
= opaque
;
965 return qemu_can_send_packet(s
->vc
);
969 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
974 sbuf
.maxlen
= maxlen
;
975 sbuf
.buf
= (char *)buf
;
977 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
980 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
982 return read(tapfd
, buf
, maxlen
);
986 static void tap_send(void *opaque
)
988 TAPState
*s
= opaque
;
991 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
993 qemu_send_packet(s
->vc
, s
->buf
, size
);
997 static void tap_cleanup(VLANClientState
*vc
)
999 TAPState
*s
= vc
->opaque
;
1001 if (s
->down_script
[0])
1002 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1004 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1011 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1018 s
= qemu_mallocz(sizeof(TAPState
));
1020 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1021 tap_receive_iov
, tap_cleanup
, s
);
1022 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
1023 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1027 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1028 static int tap_open(char *ifname
, int ifname_size
)
1034 TFR(fd
= open("/dev/tap", O_RDWR
));
1036 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1041 dev
= devname(s
.st_rdev
, S_IFCHR
);
1042 pstrcpy(ifname
, ifname_size
, dev
);
1044 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1047 #elif defined(__sun__)
1048 #define TUNNEWPPA (('T'<<16) | 0x0001)
1050 * Allocate TAP device, returns opened fd.
1051 * Stores dev name in the first arg(must be large enough).
1053 static int tap_alloc(char *dev
, size_t dev_size
)
1055 int tap_fd
, if_fd
, ppa
= -1;
1056 static int ip_fd
= 0;
1059 static int arp_fd
= 0;
1060 int ip_muxid
, arp_muxid
;
1061 struct strioctl strioc_if
, strioc_ppa
;
1062 int link_type
= I_PLINK
;;
1064 char actual_name
[32] = "";
1066 memset(&ifr
, 0x0, sizeof(ifr
));
1070 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1074 /* Check if IP device was opened */
1078 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1080 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1084 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1086 syslog(LOG_ERR
, "Can't open /dev/tap");
1090 /* Assign a new PPA and get its unit number. */
1091 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1092 strioc_ppa
.ic_timout
= 0;
1093 strioc_ppa
.ic_len
= sizeof(ppa
);
1094 strioc_ppa
.ic_dp
= (char *)&ppa
;
1095 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1096 syslog (LOG_ERR
, "Can't assign new interface");
1098 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1100 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1103 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1104 syslog(LOG_ERR
, "Can't push IP module");
1108 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1109 syslog(LOG_ERR
, "Can't get flags\n");
1111 snprintf (actual_name
, 32, "tap%d", ppa
);
1112 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1115 /* Assign ppa according to the unit number returned by tun device */
1117 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1118 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1119 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1120 syslog (LOG_ERR
, "Can't get flags\n");
1121 /* Push arp module to if_fd */
1122 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1123 syslog (LOG_ERR
, "Can't push ARP module (2)");
1125 /* Push arp module to ip_fd */
1126 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1127 syslog (LOG_ERR
, "I_POP failed\n");
1128 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1129 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1131 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1133 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1135 /* Set ifname to arp */
1136 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1137 strioc_if
.ic_timout
= 0;
1138 strioc_if
.ic_len
= sizeof(ifr
);
1139 strioc_if
.ic_dp
= (char *)&ifr
;
1140 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1141 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1144 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1145 syslog(LOG_ERR
, "Can't link TAP device to IP");
1149 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1150 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1154 memset(&ifr
, 0x0, sizeof(ifr
));
1155 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1156 ifr
.lifr_ip_muxid
= ip_muxid
;
1157 ifr
.lifr_arp_muxid
= arp_muxid
;
1159 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1161 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1162 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1163 syslog (LOG_ERR
, "Can't set multiplexor id");
1166 snprintf(dev
, dev_size
, "tap%d", ppa
);
1170 static int tap_open(char *ifname
, int ifname_size
)
1174 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1175 fprintf(stderr
, "Cannot allocate TAP device\n");
1178 pstrcpy(ifname
, ifname_size
, dev
);
1179 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1182 #elif defined (_AIX)
1183 static int tap_open(char *ifname
, int ifname_size
)
1185 fprintf (stderr
, "no tap on AIX\n");
1189 static int tap_open(char *ifname
, int ifname_size
)
1194 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1196 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1199 memset(&ifr
, 0, sizeof(ifr
));
1200 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1201 if (ifname
[0] != '\0')
1202 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1204 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1205 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1207 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1211 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1212 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1217 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1219 sigset_t oldmask
, mask
;
1225 sigaddset(&mask
, SIGCHLD
);
1226 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1228 /* try to launch network script */
1231 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1233 for (i
= 0; i
< open_max
; i
++) {
1234 if (i
!= STDIN_FILENO
&&
1235 i
!= STDOUT_FILENO
&&
1236 i
!= STDERR_FILENO
&&
1242 *parg
++ = (char *)setup_script
;
1243 *parg
++ = (char *)ifname
;
1245 execv(setup_script
, args
);
1247 } else if (pid
> 0) {
1248 while (waitpid(pid
, &status
, 0) != pid
) {
1251 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1253 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1257 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1261 static int net_tap_init(VLANState
*vlan
, const char *model
,
1262 const char *name
, const char *ifname1
,
1263 const char *setup_script
, const char *down_script
)
1269 if (ifname1
!= NULL
)
1270 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1273 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
1277 if (!setup_script
|| !strcmp(setup_script
, "no"))
1279 if (setup_script
[0] != '\0') {
1280 if (launch_script(setup_script
, ifname
, fd
))
1283 s
= net_tap_fd_init(vlan
, model
, name
, fd
);
1284 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1285 "ifname=%s,script=%s,downscript=%s",
1286 ifname
, setup_script
, down_script
);
1287 if (down_script
&& strcmp(down_script
, "no")) {
1288 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1289 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1294 #endif /* !_WIN32 */
1296 #if defined(CONFIG_VDE)
1297 typedef struct VDEState
{
1298 VLANClientState
*vc
;
1302 static void vde_to_qemu(void *opaque
)
1304 VDEState
*s
= opaque
;
1308 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1310 qemu_send_packet(s
->vc
, buf
, size
);
1314 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1316 VDEState
*s
= opaque
;
1319 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1320 if (ret
< 0 && errno
== EINTR
) {
1327 static void vde_cleanup(VLANClientState
*vc
)
1329 VDEState
*s
= vc
->opaque
;
1330 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1335 static int net_vde_init(VLANState
*vlan
, const char *model
,
1336 const char *name
, const char *sock
,
1337 int port
, const char *group
, int mode
)
1340 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1341 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1343 struct vde_open_args args
= {
1345 .group
= init_group
,
1349 s
= qemu_mallocz(sizeof(VDEState
));
1350 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1355 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_from_qemu
,
1356 NULL
, vde_cleanup
, s
);
1357 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1358 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1359 sock
, vde_datafd(s
->vde
));
1364 /* network connection */
1365 typedef struct NetSocketState
{
1366 VLANClientState
*vc
;
1368 int state
; /* 0 = getting length, 1 = getting data */
1370 unsigned int packet_len
;
1372 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1375 typedef struct NetSocketListenState
{
1380 } NetSocketListenState
;
1382 /* XXX: we consider we can send the whole packet without blocking */
1383 static void net_socket_receive(void *opaque
, const uint8_t *buf
, size_t size
)
1385 NetSocketState
*s
= opaque
;
1389 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1390 send_all(s
->fd
, buf
, size
);
1393 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, size_t size
)
1395 NetSocketState
*s
= opaque
;
1396 sendto(s
->fd
, buf
, size
, 0,
1397 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1400 static void net_socket_send(void *opaque
)
1402 NetSocketState
*s
= opaque
;
1408 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1410 err
= socket_error();
1411 if (err
!= EWOULDBLOCK
)
1413 } else if (size
== 0) {
1414 /* end of connection */
1416 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1422 /* reassemble a packet from the network */
1428 memcpy(s
->buf
+ s
->index
, buf
, l
);
1432 if (s
->index
== 4) {
1434 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1440 l
= s
->packet_len
- s
->index
;
1443 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1444 memcpy(s
->buf
+ s
->index
, buf
, l
);
1446 fprintf(stderr
, "serious error: oversized packet received,"
1447 "connection terminated.\n");
1455 if (s
->index
>= s
->packet_len
) {
1456 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1465 static void net_socket_send_dgram(void *opaque
)
1467 NetSocketState
*s
= opaque
;
1470 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1474 /* end of connection */
1475 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1478 qemu_send_packet(s
->vc
, s
->buf
, size
);
1481 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1486 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1487 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1488 inet_ntoa(mcastaddr
->sin_addr
),
1489 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1493 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1495 perror("socket(PF_INET, SOCK_DGRAM)");
1500 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1501 (const char *)&val
, sizeof(val
));
1503 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1507 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1513 /* Add host to multicast group */
1514 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1515 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1517 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1518 (const char *)&imr
, sizeof(struct ip_mreq
));
1520 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1524 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1526 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1527 (const char *)&val
, sizeof(val
));
1529 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1533 socket_set_nonblock(fd
);
1541 static void net_socket_cleanup(VLANClientState
*vc
)
1543 NetSocketState
*s
= vc
->opaque
;
1544 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1549 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1552 int fd
, int is_connected
)
1554 struct sockaddr_in saddr
;
1556 socklen_t saddr_len
;
1559 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1560 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1561 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1565 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1567 if (saddr
.sin_addr
.s_addr
==0) {
1568 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1572 /* clone dgram socket */
1573 newfd
= net_socket_mcast_create(&saddr
);
1575 /* error already reported by net_socket_mcast_create() */
1579 /* clone newfd to fd, close newfd */
1584 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1585 fd
, strerror(errno
));
1590 s
= qemu_mallocz(sizeof(NetSocketState
));
1593 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
1594 NULL
, net_socket_cleanup
, s
);
1595 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1597 /* mcast: save bound address as dst */
1598 if (is_connected
) s
->dgram_dst
=saddr
;
1600 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1601 "socket: fd=%d (%s mcast=%s:%d)",
1602 fd
, is_connected
? "cloned" : "",
1603 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1607 static void net_socket_connect(void *opaque
)
1609 NetSocketState
*s
= opaque
;
1610 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1613 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1616 int fd
, int is_connected
)
1619 s
= qemu_mallocz(sizeof(NetSocketState
));
1621 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
1622 NULL
, net_socket_cleanup
, s
);
1623 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1624 "socket: fd=%d", fd
);
1626 net_socket_connect(s
);
1628 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1633 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1634 const char *model
, const char *name
,
1635 int fd
, int is_connected
)
1637 int so_type
=-1, optlen
=sizeof(so_type
);
1639 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1640 (socklen_t
*)&optlen
)< 0) {
1641 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1646 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1648 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1650 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1651 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1652 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1657 static void net_socket_accept(void *opaque
)
1659 NetSocketListenState
*s
= opaque
;
1661 struct sockaddr_in saddr
;
1666 len
= sizeof(saddr
);
1667 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1668 if (fd
< 0 && errno
!= EINTR
) {
1670 } else if (fd
>= 0) {
1674 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1678 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1679 "socket: connection from %s:%d",
1680 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1684 static int net_socket_listen_init(VLANState
*vlan
,
1687 const char *host_str
)
1689 NetSocketListenState
*s
;
1691 struct sockaddr_in saddr
;
1693 if (parse_host_port(&saddr
, host_str
) < 0)
1696 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1698 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1703 socket_set_nonblock(fd
);
1705 /* allow fast reuse */
1707 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1709 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1714 ret
= listen(fd
, 0);
1720 s
->model
= strdup(model
);
1721 s
->name
= name
? strdup(name
) : NULL
;
1723 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1727 static int net_socket_connect_init(VLANState
*vlan
,
1730 const char *host_str
)
1733 int fd
, connected
, ret
, err
;
1734 struct sockaddr_in saddr
;
1736 if (parse_host_port(&saddr
, host_str
) < 0)
1739 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1744 socket_set_nonblock(fd
);
1748 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1750 err
= socket_error();
1751 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1752 } else if (err
== EINPROGRESS
) {
1755 } else if (err
== WSAEALREADY
) {
1768 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1771 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1772 "socket: connect to %s:%d",
1773 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1777 static int net_socket_mcast_init(VLANState
*vlan
,
1780 const char *host_str
)
1784 struct sockaddr_in saddr
;
1786 if (parse_host_port(&saddr
, host_str
) < 0)
1790 fd
= net_socket_mcast_create(&saddr
);
1794 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1798 s
->dgram_dst
= saddr
;
1800 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1801 "socket: mcast=%s:%d",
1802 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1807 typedef struct DumpState
{
1808 VLANClientState
*pcap_vc
;
1813 #define PCAP_MAGIC 0xa1b2c3d4
1815 struct pcap_file_hdr
{
1817 uint16_t version_major
;
1818 uint16_t version_minor
;
1825 struct pcap_sf_pkthdr
{
1834 static void dump_receive(void *opaque
, const uint8_t *buf
, size_t size
)
1836 DumpState
*s
= opaque
;
1837 struct pcap_sf_pkthdr hdr
;
1841 /* Early return in case of previous error. */
1846 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, ticks_per_sec
);
1847 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1849 hdr
.ts
.tv_sec
= ts
/ 1000000;
1850 hdr
.ts
.tv_usec
= ts
% 1000000;
1851 hdr
.caplen
= caplen
;
1853 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1854 write(s
->fd
, buf
, caplen
) != caplen
) {
1855 qemu_log("-net dump write error - stop dump\n");
1861 static void net_dump_cleanup(VLANClientState
*vc
)
1863 DumpState
*s
= vc
->opaque
;
1869 static int net_dump_init(Monitor
*mon
, VLANState
*vlan
, const char *device
,
1870 const char *name
, const char *filename
, int len
)
1872 struct pcap_file_hdr hdr
;
1875 s
= qemu_malloc(sizeof(DumpState
));
1877 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
, 0644);
1879 config_error(mon
, "-net dump: can't open %s\n", filename
);
1883 s
->pcap_caplen
= len
;
1885 hdr
.magic
= PCAP_MAGIC
;
1886 hdr
.version_major
= 2;
1887 hdr
.version_minor
= 4;
1890 hdr
.snaplen
= s
->pcap_caplen
;
1893 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
1894 config_error(mon
, "-net dump write error: %s\n", strerror(errno
));
1900 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
1901 net_dump_cleanup
, s
);
1902 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
1903 "dump to %s (len=%d)", filename
, len
);
1907 /* find or alloc a new VLAN */
1908 VLANState
*qemu_find_vlan(int id
)
1910 VLANState
**pvlan
, *vlan
;
1911 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1915 vlan
= qemu_mallocz(sizeof(VLANState
));
1918 pvlan
= &first_vlan
;
1919 while (*pvlan
!= NULL
)
1920 pvlan
= &(*pvlan
)->next
;
1925 static int nic_get_free_idx(void)
1929 for (index
= 0; index
< MAX_NICS
; index
++)
1930 if (!nd_table
[index
].used
)
1935 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1937 const char *models
[2];
1942 qemu_check_nic_model_list(nd
, models
, model
);
1945 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1946 const char *default_model
)
1948 int i
, exit_status
= 0;
1951 nd
->model
= strdup(default_model
);
1953 if (strcmp(nd
->model
, "?") != 0) {
1954 for (i
= 0 ; models
[i
]; i
++)
1955 if (strcmp(nd
->model
, models
[i
]) == 0)
1958 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1962 fprintf(stderr
, "qemu: Supported NIC models: ");
1963 for (i
= 0 ; models
[i
]; i
++)
1964 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1969 int net_client_init(Monitor
*mon
, const char *device
, const char *p
)
1971 static const char * const fd_params
[] = {
1972 "vlan", "name", "fd", NULL
1980 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1981 vlan_id
= strtol(buf
, NULL
, 0);
1983 vlan
= qemu_find_vlan(vlan_id
);
1985 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1986 name
= qemu_strdup(buf
);
1988 if (!strcmp(device
, "nic")) {
1989 static const char * const nic_params
[] = {
1990 "vlan", "name", "macaddr", "model", NULL
1994 int idx
= nic_get_free_idx();
1996 if (check_params(buf
, sizeof(buf
), nic_params
, p
) < 0) {
1997 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2001 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2002 config_error(mon
, "Too Many NICs\n");
2006 nd
= &nd_table
[idx
];
2007 macaddr
= nd
->macaddr
;
2013 macaddr
[5] = 0x56 + idx
;
2015 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2016 if (parse_macaddr(macaddr
, buf
) < 0) {
2017 config_error(mon
, "invalid syntax for ethernet address\n");
2022 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2023 nd
->model
= strdup(buf
);
2030 vlan
->nb_guest_devs
++;
2033 if (!strcmp(device
, "none")) {
2035 config_error(mon
, "'none' takes no parameters\n");
2039 /* does nothing. It is needed to signal that no network cards
2044 if (!strcmp(device
, "user")) {
2045 static const char * const slirp_params
[] = {
2046 "vlan", "name", "hostname", "restrict", "ip", NULL
2051 if (check_params(buf
, sizeof(buf
), slirp_params
, p
) < 0) {
2052 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2056 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2057 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
2059 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2060 restricted
= (buf
[0] == 'y') ? 1 : 0;
2062 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2063 ip
= qemu_strdup(buf
);
2065 vlan
->nb_host_devs
++;
2066 ret
= net_slirp_init(vlan
, device
, name
, restricted
, ip
);
2068 } else if (!strcmp(device
, "channel")) {
2070 char name
[20], *devname
;
2071 struct VMChannel
*vmc
;
2073 port
= strtol(p
, &devname
, 10);
2075 if (port
< 1 || port
> 65535) {
2076 config_error(mon
, "vmchannel wrong port number\n");
2080 vmc
= malloc(sizeof(struct VMChannel
));
2081 snprintf(name
, 20, "vmchannel%ld", port
);
2082 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
2084 config_error(mon
, "could not open vmchannel device '%s'\n",
2090 slirp_add_exec(3, vmc
->hd
, 4, port
);
2091 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
2097 if (!strcmp(device
, "tap")) {
2098 static const char * const tap_params
[] = {
2099 "vlan", "name", "ifname", NULL
2103 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
2104 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2108 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2109 config_error(mon
, "tap: no interface name\n");
2113 vlan
->nb_host_devs
++;
2114 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2116 #elif defined (_AIX)
2118 if (!strcmp(device
, "tap")) {
2119 char ifname
[64], chkbuf
[64];
2120 char setup_script
[1024], down_script
[1024];
2122 vlan
->nb_host_devs
++;
2123 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2124 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2125 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2129 fd
= strtol(buf
, NULL
, 0);
2130 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2131 net_tap_fd_init(vlan
, device
, name
, fd
);
2134 static const char * const tap_params
[] = {
2135 "vlan", "name", "ifname", "script", "downscript", NULL
2137 if (check_params(chkbuf
, sizeof(chkbuf
), tap_params
, p
) < 0) {
2138 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2142 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2145 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2146 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2148 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2149 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2151 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2155 if (!strcmp(device
, "socket")) {
2157 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2159 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2160 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2164 fd
= strtol(buf
, NULL
, 0);
2166 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2168 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2169 static const char * const listen_params
[] = {
2170 "vlan", "name", "listen", NULL
2172 if (check_params(chkbuf
, sizeof(chkbuf
), listen_params
, p
) < 0) {
2173 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2177 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2178 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2179 static const char * const connect_params
[] = {
2180 "vlan", "name", "connect", NULL
2182 if (check_params(chkbuf
, sizeof(chkbuf
), connect_params
, p
) < 0) {
2183 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2187 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2188 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2189 static const char * const mcast_params
[] = {
2190 "vlan", "name", "mcast", NULL
2192 if (check_params(chkbuf
, sizeof(chkbuf
), mcast_params
, p
) < 0) {
2193 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2197 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2199 config_error(mon
, "Unknown socket options: %s\n", p
);
2203 vlan
->nb_host_devs
++;
2206 if (!strcmp(device
, "vde")) {
2207 static const char * const vde_params
[] = {
2208 "vlan", "name", "sock", "port", "group", "mode", NULL
2210 char vde_sock
[1024], vde_group
[512];
2211 int vde_port
, vde_mode
;
2213 if (check_params(buf
, sizeof(buf
), vde_params
, p
) < 0) {
2214 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2218 vlan
->nb_host_devs
++;
2219 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2222 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2223 vde_port
= strtol(buf
, NULL
, 10);
2227 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2228 vde_group
[0] = '\0';
2230 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2231 vde_mode
= strtol(buf
, NULL
, 8);
2235 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2238 if (!strcmp(device
, "dump")) {
2241 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2242 len
= strtol(buf
, NULL
, 0);
2244 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2245 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2247 ret
= net_dump_init(mon
, vlan
, device
, name
, buf
, len
);
2249 config_error(mon
, "Unknown network device: %s\n", device
);
2254 config_error(mon
, "Could not initialize device '%s'\n", device
);
2261 void net_client_uninit(NICInfo
*nd
)
2263 nd
->vlan
->nb_guest_devs
--;
2266 free((void *)nd
->model
);
2269 static int net_host_check_device(const char *device
)
2272 const char *valid_param_list
[] = { "tap", "socket", "dump"
2280 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2281 if (!strncmp(valid_param_list
[i
], device
,
2282 strlen(valid_param_list
[i
])))
2289 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2291 if (!net_host_check_device(device
)) {
2292 monitor_printf(mon
, "invalid host network device %s\n", device
);
2295 if (net_client_init(mon
, device
, opts
? opts
: "") < 0) {
2296 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2300 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2303 VLANClientState
*vc
;
2305 vlan
= qemu_find_vlan(vlan_id
);
2307 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2308 if (!strcmp(vc
->name
, device
)) {
2314 monitor_printf(mon
, "can't find device %s\n", device
);
2317 if (!net_host_check_device(vc
->model
)) {
2318 monitor_printf(mon
, "invalid host network device %s\n", device
);
2321 qemu_del_vlan_client(vc
);
2324 int net_client_parse(const char *str
)
2332 while (*p
!= '\0' && *p
!= ',') {
2333 if ((q
- device
) < sizeof(device
) - 1)
2341 return net_client_init(NULL
, device
, p
);
2344 void do_info_network(Monitor
*mon
)
2347 VLANClientState
*vc
;
2349 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2350 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2351 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2352 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2356 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2359 VLANClientState
*vc
= NULL
;
2361 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2362 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2363 if (strcmp(vc
->name
, name
) == 0)
2368 monitor_printf(mon
, "could not find network device '%s'", name
);
2372 if (strcmp(up_or_down
, "up") == 0)
2374 else if (strcmp(up_or_down
, "down") == 0)
2377 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2378 "valid\n", up_or_down
);
2380 if (vc
->link_status_changed
)
2381 vc
->link_status_changed(vc
);
2386 void net_cleanup(void)
2390 /* close network clients */
2391 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2392 VLANClientState
*vc
= vlan
->first_client
;
2395 VLANClientState
*next
= vc
->next
;
2397 qemu_del_vlan_client(vc
);
2404 void net_client_check(void)
2408 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2409 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2411 if (vlan
->nb_guest_devs
== 0)
2412 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2413 if (vlan
->nb_host_devs
== 0)
2415 "Warning: vlan %d is not connected to host network\n",