4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
113 // FIXME: #include "qemu-kvm.h"
114 #include "qemu-common.h"
118 #include "qemu-timer.h"
119 #include "qemu-char.h"
120 #include "audio/audio.h"
121 #include "qemu_socket.h"
122 #include "qemu-log.h"
124 #if defined(CONFIG_SLIRP)
125 #include "libslirp.h"
129 static VLANState
*first_vlan
;
131 /***********************************************************/
132 /* network device redirectors */
134 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
135 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
139 for(i
=0;i
<size
;i
+=16) {
143 fprintf(f
, "%08x ", i
);
146 fprintf(f
, " %02x", buf
[i
+j
]);
153 if (c
< ' ' || c
> '~')
162 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
169 offset
= strtol(p
, &last_char
, 0);
170 if (0 == errno
&& '\0' == *last_char
&&
171 offset
>= 0 && offset
<= 0xFFFFFF) {
172 macaddr
[3] = (offset
& 0xFF0000) >> 16;
173 macaddr
[4] = (offset
& 0xFF00) >> 8;
174 macaddr
[5] = offset
& 0xFF;
177 for(i
= 0; i
< 6; i
++) {
178 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
183 if (*p
!= ':' && *p
!= '-')
194 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
205 if (len
> buf_size
- 1)
214 int parse_host_src_port(struct sockaddr_in
*haddr
,
215 struct sockaddr_in
*saddr
,
216 const char *input_str
)
218 char *str
= strdup(input_str
);
219 char *host_str
= str
;
221 const char *src_str2
;
225 * Chop off any extra arguments at the end of the string which
226 * would start with a comma, then fill in the src port information
227 * if it was provided else use the "any address" and "any port".
229 if ((ptr
= strchr(str
,',')))
232 if ((src_str
= strchr(input_str
,'@'))) {
237 if (parse_host_port(haddr
, host_str
) < 0)
241 if (!src_str
|| *src_str
== '\0')
244 if (parse_host_port(saddr
, src_str2
) < 0)
255 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
263 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
265 saddr
->sin_family
= AF_INET
;
266 if (buf
[0] == '\0') {
267 saddr
->sin_addr
.s_addr
= 0;
269 if (qemu_isdigit(buf
[0])) {
270 if (!inet_aton(buf
, &saddr
->sin_addr
))
273 if ((he
= gethostbyname(buf
)) == NULL
)
275 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
278 port
= strtol(p
, (char **)&r
, 0);
281 saddr
->sin_port
= htons(port
);
285 #if !defined(_WIN32) && 0
286 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
291 len
= MIN(108, strlen(str
));
292 p
= strchr(str
, ',');
294 len
= MIN(len
, p
- str
);
296 memset(uaddr
, 0, sizeof(*uaddr
));
298 uaddr
->sun_family
= AF_UNIX
;
299 memcpy(uaddr
->sun_path
, str
, len
);
305 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
307 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
308 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
310 macaddr
[0], macaddr
[1], macaddr
[2],
311 macaddr
[3], macaddr
[4], macaddr
[5]);
314 static char *assign_name(VLANClientState
*vc1
, const char *model
)
320 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
323 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
324 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
328 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
333 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
336 IOReadHandler
*fd_read
,
337 IOCanRWHandler
*fd_can_read
,
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
->fd_read
= fd_read
;
349 vc
->fd_can_read
= fd_can_read
;
350 vc
->cleanup
= cleanup
;
355 pvc
= &vlan
->first_client
;
362 void qemu_del_vlan_client(VLANClientState
*vc
)
364 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
380 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
382 VLANClientState
**pvc
= &vlan
->first_client
;
385 if ((*pvc
)->opaque
== opaque
)
393 int qemu_can_send_packet(VLANClientState
*vc1
)
395 VLANState
*vlan
= vc1
->vlan
;
398 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
400 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
408 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
413 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
417 } else if (!vc
->fd_can_read
|| vc
->fd_can_read(vc
->opaque
)) {
418 vc
->fd_read(vc
->opaque
, buf
, size
);
426 int qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
428 VLANState
*vlan
= vc
->vlan
;
436 printf("vlan %d send:\n", vlan
->id
);
437 hex_dump(stdout
, buf
, size
);
439 if (vlan
->delivering
) {
440 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
441 packet
->next
= vlan
->send_queue
;
444 memcpy(packet
->data
, buf
, size
);
445 vlan
->send_queue
= packet
;
447 vlan
->delivering
= 1;
448 ret
= qemu_deliver_packet(vc
, buf
, size
);
449 while ((packet
= vlan
->send_queue
) != NULL
) {
450 qemu_deliver_packet(packet
->sender
, packet
->data
, packet
->size
);
451 vlan
->send_queue
= packet
->next
;
454 vlan
->delivering
= 0;
459 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
462 uint8_t buffer
[4096];
466 for (i
= 0; i
< iovcnt
; i
++) {
469 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
470 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
474 vc
->fd_read(vc
->opaque
, buffer
, offset
);
479 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
484 for (i
= 0; i
< iovcnt
; i
++)
485 offset
+= iov
[i
].iov_len
;
489 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
492 VLANState
*vlan
= vc1
->vlan
;
497 return calc_iov_length(iov
, iovcnt
);
499 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
506 len
= calc_iov_length(iov
, iovcnt
);
508 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
509 else if (vc
->fd_read
)
510 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
512 max_len
= MAX(max_len
, len
);
518 #if defined(CONFIG_SLIRP)
520 /* slirp network adapter */
522 static int slirp_inited
;
523 static int slirp_restrict
;
524 static char *slirp_ip
;
525 static VLANClientState
*slirp_vc
;
527 int slirp_can_output(void)
529 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
532 void slirp_output(const uint8_t *pkt
, int pkt_len
)
535 printf("slirp output:\n");
536 hex_dump(stdout
, pkt
, pkt_len
);
540 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
543 int slirp_is_inited(void)
548 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
551 printf("slirp input:\n");
552 hex_dump(stdout
, buf
, size
);
554 slirp_input(buf
, size
);
557 static int slirp_in_use
;
559 static void net_slirp_cleanup(VLANClientState
*vc
)
564 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
567 /* slirp only supports a single instance so far */
572 slirp_init(slirp_restrict
, slirp_ip
);
574 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
575 slirp_receive
, NULL
, net_slirp_cleanup
, NULL
);
576 slirp_vc
->info_str
[0] = '\0';
581 void net_slirp_redir(Monitor
*mon
, const char *redir_str
)
585 const char *p
, *errmsg
;
586 struct in_addr guest_addr
;
587 int host_port
, guest_port
;
591 slirp_init(slirp_restrict
, slirp_ip
);
595 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
597 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
599 } else if (!strcmp(buf
, "udp")) {
605 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
607 host_port
= strtol(buf
, &r
, 0);
611 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
613 if (buf
[0] == '\0') {
614 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
616 if (!inet_aton(buf
, &guest_addr
))
619 guest_port
= strtol(p
, &r
, 0);
623 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
624 errmsg
= "could not set up redirection\n";
630 errmsg
= "invalid redirection format\n";
633 monitor_printf(mon
, "%s", errmsg
);
635 fprintf(stderr
, "qemu: %s", errmsg
);
642 static char smb_dir
[1024];
644 static void erase_dir(char *dir_name
)
650 /* erase all the files in the directory */
651 if ((d
= opendir(dir_name
)) != NULL
) {
656 if (strcmp(de
->d_name
, ".") != 0 &&
657 strcmp(de
->d_name
, "..") != 0) {
658 snprintf(filename
, sizeof(filename
), "%s/%s",
659 smb_dir
, de
->d_name
);
660 if (unlink(filename
) != 0) /* is it a directory? */
669 /* automatic user mode samba server configuration */
670 static void smb_exit(void)
675 /* automatic user mode samba server configuration */
676 void net_slirp_smb(const char *exported_dir
)
679 char smb_cmdline
[1024];
684 slirp_init(slirp_restrict
, slirp_ip
);
687 /* XXX: better tmp dir construction */
688 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%ld", (long)getpid());
689 if (mkdir(smb_dir
, 0700) < 0) {
690 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
693 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
695 f
= fopen(smb_conf
, "w");
697 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
704 "socket address=127.0.0.1\n"
706 "lock directory=%s\n"
707 "log file=%s/log.smbd\n"
708 "smb passwd file=%s/smbpasswd\n"
724 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
725 SMBD_COMMAND
, smb_conf
);
727 slirp_add_exec(0, smb_cmdline
, 4, 139);
730 #endif /* !defined(_WIN32) */
731 void do_info_slirp(Monitor
*mon
)
741 static int vmchannel_can_read(void *opaque
)
743 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
744 return slirp_socket_can_recv(4, vmc
->port
);
747 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
749 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
750 slirp_socket_recv(4, vmc
->port
, buf
, size
);
753 #endif /* CONFIG_SLIRP */
757 int tap_has_vnet_hdr(void *opaque
)
762 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
766 #else /* !defined(_WIN32) */
768 /* Maximum GSO packet size (64k) plus plenty of room for
769 * the ethernet and virtio_net headers
771 #define TAP_BUFSIZE (4096 + 65536)
774 #include <linux/virtio_net.h>
777 typedef struct TAPState
{
780 char down_script
[1024];
781 char down_script_arg
[128];
782 char buf
[TAP_BUFSIZE
];
784 unsigned int has_vnet_hdr
: 1;
785 unsigned int using_vnet_hdr
: 1;
788 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
790 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
793 TAPState
*s
= opaque
;
797 len
= writev(s
->fd
, iov
, iovcnt
);
798 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
803 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
809 TAPState
*s
= opaque
;
810 struct virtio_net_hdr hdr
= { 0, };
812 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
813 iov
[i
].iov_base
= &hdr
;
814 iov
[i
].iov_len
= sizeof(hdr
);
819 iov
[i
].iov_base
= (char *) buf
;
820 iov
[i
].iov_len
= size
;
823 tap_receive_iov(opaque
, iov
, i
);
826 static int tap_can_send(void *opaque
)
828 TAPState
*s
= opaque
;
832 /* Check to see if any of our clients can receive a packet */
833 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
838 if (!vc
->fd_can_read
) {
839 /* no fd_can_read handler, they always can receive */
842 can_receive
= vc
->fd_can_read(vc
->opaque
);
844 /* Once someone can receive, we try to send a packet */
852 static int tap_send_packet(TAPState
*s
)
854 uint8_t *buf
= (uint8_t *)s
->buf
;
858 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
859 buf
+= sizeof(struct virtio_net_hdr
);
860 size
-= sizeof(struct virtio_net_hdr
);
864 return qemu_send_packet(s
->vc
, buf
, size
);
867 static void tap_send(void *opaque
)
869 TAPState
*s
= opaque
;
871 /* First try to send any buffered packet */
875 /* If noone can receive the packet, buffer it */
876 err
= tap_send_packet(s
);
881 /* Read packets until we hit EAGAIN */
886 sbuf
.maxlen
= sizeof(buf
);
887 sbuf
.buf
= (char *)buf
;
888 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
890 // FIXME: kvm_sleep_begin();
891 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
892 // FIXME: kvm_sleep_end();
895 if (s
->size
== -1 && errno
== EINTR
)
901 /* If noone can receive the packet, buffer it */
902 err
= tap_send_packet(s
);
906 } while (s
->size
> 0);
909 int tap_has_vnet_hdr(void *opaque
)
911 VLANClientState
*vc
= opaque
;
912 TAPState
*s
= vc
->opaque
;
914 return s
? s
->has_vnet_hdr
: 0;
917 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
919 VLANClientState
*vc
= opaque
;
920 TAPState
*s
= vc
->opaque
;
922 if (!s
|| !s
->has_vnet_hdr
)
925 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
928 static int tap_probe_vnet_hdr(int fd
)
930 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
933 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
934 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
938 return ifr
.ifr_flags
& IFF_VNET_HDR
;
945 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
948 TAPState
*s
= vc
->opaque
;
949 unsigned int offload
= 0;
952 offload
|= TUN_F_CSUM
;
954 offload
|= TUN_F_TSO4
;
956 offload
|= TUN_F_TSO6
;
957 if ((tso4
|| tso6
) && ecn
)
958 offload
|= TUN_F_TSO_ECN
;
961 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
962 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
965 #endif /* TUNSETOFFLOAD */
967 static void tap_cleanup(VLANClientState
*vc
)
969 TAPState
*s
= vc
->opaque
;
971 if (s
->down_script
[0])
972 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
974 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
981 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
989 s
= qemu_mallocz(sizeof(TAPState
));
991 s
->has_vnet_hdr
= vnet_hdr
!= 0;
992 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
,
993 NULL
, tap_cleanup
, s
);
994 s
->vc
->fd_readv
= tap_receive_iov
;
996 s
->vc
->set_offload
= tap_set_offload
;
997 tap_set_offload(s
->vc
, 0, 0, 0, 0);
999 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
1000 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1004 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1005 static int tap_open(char *ifname
, int ifname_size
)
1011 TFR(fd
= open("/dev/tap", O_RDWR
));
1013 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1018 dev
= devname(s
.st_rdev
, S_IFCHR
);
1019 pstrcpy(ifname
, ifname_size
, dev
);
1021 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1024 #elif defined(__sun__)
1025 #define TUNNEWPPA (('T'<<16) | 0x0001)
1027 * Allocate TAP device, returns opened fd.
1028 * Stores dev name in the first arg(must be large enough).
1030 static int tap_alloc(char *dev
, size_t dev_size
)
1032 int tap_fd
, if_fd
, ppa
= -1;
1033 static int ip_fd
= 0;
1036 static int arp_fd
= 0;
1037 int ip_muxid
, arp_muxid
;
1038 struct strioctl strioc_if
, strioc_ppa
;
1039 int link_type
= I_PLINK
;;
1041 char actual_name
[32] = "";
1043 memset(&ifr
, 0x0, sizeof(ifr
));
1047 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1051 /* Check if IP device was opened */
1055 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1057 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1061 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1063 syslog(LOG_ERR
, "Can't open /dev/tap");
1067 /* Assign a new PPA and get its unit number. */
1068 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1069 strioc_ppa
.ic_timout
= 0;
1070 strioc_ppa
.ic_len
= sizeof(ppa
);
1071 strioc_ppa
.ic_dp
= (char *)&ppa
;
1072 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1073 syslog (LOG_ERR
, "Can't assign new interface");
1075 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1077 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1080 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1081 syslog(LOG_ERR
, "Can't push IP module");
1085 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1086 syslog(LOG_ERR
, "Can't get flags\n");
1088 snprintf (actual_name
, 32, "tap%d", ppa
);
1089 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1092 /* Assign ppa according to the unit number returned by tun device */
1094 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1095 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1096 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1097 syslog (LOG_ERR
, "Can't get flags\n");
1098 /* Push arp module to if_fd */
1099 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1100 syslog (LOG_ERR
, "Can't push ARP module (2)");
1102 /* Push arp module to ip_fd */
1103 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1104 syslog (LOG_ERR
, "I_POP failed\n");
1105 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1106 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1108 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1110 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1112 /* Set ifname to arp */
1113 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1114 strioc_if
.ic_timout
= 0;
1115 strioc_if
.ic_len
= sizeof(ifr
);
1116 strioc_if
.ic_dp
= (char *)&ifr
;
1117 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1118 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1121 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1122 syslog(LOG_ERR
, "Can't link TAP device to IP");
1126 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1127 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1131 memset(&ifr
, 0x0, sizeof(ifr
));
1132 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1133 ifr
.lifr_ip_muxid
= ip_muxid
;
1134 ifr
.lifr_arp_muxid
= arp_muxid
;
1136 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1138 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1139 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1140 syslog (LOG_ERR
, "Can't set multiplexor id");
1143 snprintf(dev
, dev_size
, "tap%d", ppa
);
1147 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1151 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1152 fprintf(stderr
, "Cannot allocate TAP device\n");
1155 pstrcpy(ifname
, ifname_size
, dev
);
1156 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1159 #elif defined (_AIX)
1160 static int tap_open(char *ifname
, int ifname_size
)
1162 fprintf (stderr
, "no tap on AIX\n");
1166 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1171 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1173 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1176 memset(&ifr
, 0, sizeof(ifr
));
1177 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1179 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1181 unsigned int features
;
1183 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1184 features
& IFF_VNET_HDR
) {
1186 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1191 if (ifname
[0] != '\0')
1192 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1194 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1195 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1197 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1201 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1202 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1207 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1213 /* try to launch network script */
1217 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1218 for (i
= 0; i
< open_max
; i
++)
1219 if (i
!= STDIN_FILENO
&&
1220 i
!= STDOUT_FILENO
&&
1221 i
!= STDERR_FILENO
&&
1226 *parg
++ = (char *)setup_script
;
1227 *parg
++ = (char *)ifname
;
1229 execv(setup_script
, args
);
1232 while (waitpid(pid
, &status
, 0) != pid
);
1233 if (!WIFEXITED(status
) ||
1234 WEXITSTATUS(status
) != 0) {
1235 fprintf(stderr
, "%s: could not launch network script\n",
1243 static int net_tap_init(VLANState
*vlan
, const char *model
,
1244 const char *name
, const char *ifname1
,
1245 const char *setup_script
, const char *down_script
)
1252 if (ifname1
!= NULL
)
1253 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1257 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1261 if (!setup_script
|| !strcmp(setup_script
, "no"))
1263 if (setup_script
[0] != '\0') {
1264 if (launch_script(setup_script
, ifname
, fd
))
1267 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1268 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1269 "ifname=%s,script=%s,downscript=%s",
1270 ifname
, setup_script
, down_script
);
1271 if (down_script
&& strcmp(down_script
, "no")) {
1272 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1273 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1278 #endif /* !_WIN32 */
1280 #if defined(CONFIG_VDE)
1281 typedef struct VDEState
{
1282 VLANClientState
*vc
;
1286 static void vde_to_qemu(void *opaque
)
1288 VDEState
*s
= opaque
;
1292 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1294 qemu_send_packet(s
->vc
, buf
, size
);
1298 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1300 VDEState
*s
= opaque
;
1303 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1304 if (ret
< 0 && errno
== EINTR
) {
1311 static void vde_cleanup(VLANClientState
*vc
)
1313 VDEState
*s
= vc
->opaque
;
1314 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1319 static int net_vde_init(VLANState
*vlan
, const char *model
,
1320 const char *name
, const char *sock
,
1321 int port
, const char *group
, int mode
)
1324 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1325 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1327 struct vde_open_args args
= {
1329 .group
= init_group
,
1333 s
= qemu_mallocz(sizeof(VDEState
));
1334 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1339 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
,
1340 NULL
, vde_cleanup
, s
);
1341 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1342 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1343 sock
, vde_datafd(s
->vde
));
1348 /* network connection */
1349 typedef struct NetSocketState
{
1350 VLANClientState
*vc
;
1352 int state
; /* 0 = getting length, 1 = getting data */
1354 unsigned int packet_len
;
1356 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1359 typedef struct NetSocketListenState
{
1364 } NetSocketListenState
;
1366 /* XXX: we consider we can send the whole packet without blocking */
1367 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1369 NetSocketState
*s
= opaque
;
1373 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1374 send_all(s
->fd
, buf
, size
);
1377 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1379 NetSocketState
*s
= opaque
;
1380 sendto(s
->fd
, buf
, size
, 0,
1381 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1384 static void net_socket_send(void *opaque
)
1386 NetSocketState
*s
= opaque
;
1392 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1394 err
= socket_error();
1395 if (err
!= EWOULDBLOCK
)
1397 } else if (size
== 0) {
1398 /* end of connection */
1400 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1406 /* reassemble a packet from the network */
1412 memcpy(s
->buf
+ s
->index
, buf
, l
);
1416 if (s
->index
== 4) {
1418 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1424 l
= s
->packet_len
- s
->index
;
1427 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1428 memcpy(s
->buf
+ s
->index
, buf
, l
);
1430 fprintf(stderr
, "serious error: oversized packet received,"
1431 "connection terminated.\n");
1439 if (s
->index
>= s
->packet_len
) {
1440 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1449 static void net_socket_send_dgram(void *opaque
)
1451 NetSocketState
*s
= opaque
;
1454 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1458 /* end of connection */
1459 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1462 qemu_send_packet(s
->vc
, s
->buf
, size
);
1465 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1470 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1471 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1472 inet_ntoa(mcastaddr
->sin_addr
),
1473 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1477 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1479 perror("socket(PF_INET, SOCK_DGRAM)");
1484 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1485 (const char *)&val
, sizeof(val
));
1487 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1491 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1497 /* Add host to multicast group */
1498 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1499 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1501 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1502 (const char *)&imr
, sizeof(struct ip_mreq
));
1504 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1508 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1510 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1511 (const char *)&val
, sizeof(val
));
1513 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1517 socket_set_nonblock(fd
);
1525 static void net_socket_cleanup(VLANClientState
*vc
)
1527 NetSocketState
*s
= vc
->opaque
;
1528 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1533 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1536 int fd
, int is_connected
)
1538 struct sockaddr_in saddr
;
1540 socklen_t saddr_len
;
1543 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1544 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1545 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1549 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1551 if (saddr
.sin_addr
.s_addr
==0) {
1552 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1556 /* clone dgram socket */
1557 newfd
= net_socket_mcast_create(&saddr
);
1559 /* error already reported by net_socket_mcast_create() */
1563 /* clone newfd to fd, close newfd */
1568 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1569 fd
, strerror(errno
));
1574 s
= qemu_mallocz(sizeof(NetSocketState
));
1577 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
,
1578 NULL
, net_socket_cleanup
, s
);
1579 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1581 /* mcast: save bound address as dst */
1582 if (is_connected
) s
->dgram_dst
=saddr
;
1584 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1585 "socket: fd=%d (%s mcast=%s:%d)",
1586 fd
, is_connected
? "cloned" : "",
1587 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1591 static void net_socket_connect(void *opaque
)
1593 NetSocketState
*s
= opaque
;
1594 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1597 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1600 int fd
, int is_connected
)
1603 s
= qemu_mallocz(sizeof(NetSocketState
));
1605 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive
,
1606 NULL
, net_socket_cleanup
, s
);
1607 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1608 "socket: fd=%d", fd
);
1610 net_socket_connect(s
);
1612 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1617 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1618 const char *model
, const char *name
,
1619 int fd
, int is_connected
)
1621 int so_type
=-1, optlen
=sizeof(so_type
);
1623 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1624 (socklen_t
*)&optlen
)< 0) {
1625 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1630 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1632 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1634 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1635 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1636 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1641 static void net_socket_accept(void *opaque
)
1643 NetSocketListenState
*s
= opaque
;
1645 struct sockaddr_in saddr
;
1650 len
= sizeof(saddr
);
1651 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1652 if (fd
< 0 && errno
!= EINTR
) {
1654 } else if (fd
>= 0) {
1658 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1662 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1663 "socket: connection from %s:%d",
1664 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1668 static int net_socket_listen_init(VLANState
*vlan
,
1671 const char *host_str
)
1673 NetSocketListenState
*s
;
1675 struct sockaddr_in saddr
;
1677 if (parse_host_port(&saddr
, host_str
) < 0)
1680 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1682 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1687 socket_set_nonblock(fd
);
1689 /* allow fast reuse */
1691 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1693 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1698 ret
= listen(fd
, 0);
1704 s
->model
= strdup(model
);
1705 s
->name
= name
? strdup(name
) : NULL
;
1707 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1711 static int net_socket_connect_init(VLANState
*vlan
,
1714 const char *host_str
)
1717 int fd
, connected
, ret
, err
;
1718 struct sockaddr_in saddr
;
1720 if (parse_host_port(&saddr
, host_str
) < 0)
1723 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1728 socket_set_nonblock(fd
);
1732 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1734 err
= socket_error();
1735 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1736 } else if (err
== EINPROGRESS
) {
1739 } else if (err
== WSAEALREADY
) {
1752 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1755 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1756 "socket: connect to %s:%d",
1757 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1761 static int net_socket_mcast_init(VLANState
*vlan
,
1764 const char *host_str
)
1768 struct sockaddr_in saddr
;
1770 if (parse_host_port(&saddr
, host_str
) < 0)
1774 fd
= net_socket_mcast_create(&saddr
);
1778 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1782 s
->dgram_dst
= saddr
;
1784 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1785 "socket: mcast=%s:%d",
1786 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1791 typedef struct DumpState
{
1792 VLANClientState
*pcap_vc
;
1797 #define PCAP_MAGIC 0xa1b2c3d4
1799 struct pcap_file_hdr
{
1801 uint16_t version_major
;
1802 uint16_t version_minor
;
1809 struct pcap_sf_pkthdr
{
1818 static void dump_receive(void *opaque
, const uint8_t *buf
, int size
)
1820 DumpState
*s
= opaque
;
1821 struct pcap_sf_pkthdr hdr
;
1825 /* Early return in case of previous error. */
1830 ts
= muldiv64 (qemu_get_clock(vm_clock
),1000000, ticks_per_sec
);
1831 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1833 hdr
.ts
.tv_sec
= ts
/ 1000000000LL;
1834 hdr
.ts
.tv_usec
= ts
% 1000000;
1835 hdr
.caplen
= caplen
;
1837 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1838 write(s
->fd
, buf
, caplen
) != caplen
) {
1839 qemu_log("-net dump write error - stop dump\n");
1845 static void net_dump_cleanup(VLANClientState
*vc
)
1847 DumpState
*s
= vc
->opaque
;
1853 static int net_dump_init(VLANState
*vlan
, const char *device
,
1854 const char *name
, const char *filename
, int len
)
1856 struct pcap_file_hdr hdr
;
1859 s
= qemu_malloc(sizeof(DumpState
));
1861 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
, 0644);
1863 fprintf(stderr
, "-net dump: can't open %s\n", filename
);
1867 s
->pcap_caplen
= len
;
1869 hdr
.magic
= PCAP_MAGIC
;
1870 hdr
.version_major
= 2;
1871 hdr
.version_minor
= 4;
1874 hdr
.snaplen
= s
->pcap_caplen
;
1877 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
1878 perror("-net dump write error");
1884 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, dump_receive
, NULL
,
1885 net_dump_cleanup
, s
);
1886 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
1887 "dump to %s (len=%d)", filename
, len
);
1891 /* find or alloc a new VLAN */
1892 VLANState
*qemu_find_vlan(int id
)
1894 VLANState
**pvlan
, *vlan
;
1895 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1899 vlan
= qemu_mallocz(sizeof(VLANState
));
1902 pvlan
= &first_vlan
;
1903 while (*pvlan
!= NULL
)
1904 pvlan
= &(*pvlan
)->next
;
1909 static int nic_get_free_idx(void)
1913 for (index
= 0; index
< MAX_NICS
; index
++)
1914 if (!nd_table
[index
].used
)
1919 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1921 const char *models
[2];
1926 qemu_check_nic_model_list(nd
, models
, model
);
1929 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1930 const char *default_model
)
1932 int i
, exit_status
= 0;
1935 nd
->model
= strdup(default_model
);
1937 if (strcmp(nd
->model
, "?") != 0) {
1938 for (i
= 0 ; models
[i
]; i
++)
1939 if (strcmp(nd
->model
, models
[i
]) == 0)
1942 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1946 fprintf(stderr
, "qemu: Supported NIC models: ");
1947 for (i
= 0 ; models
[i
]; i
++)
1948 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1953 int net_client_init(const char *device
, const char *p
)
1955 static const char * const fd_params
[] = {
1956 "vlan", "name", "fd", NULL
1964 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1965 vlan_id
= strtol(buf
, NULL
, 0);
1967 vlan
= qemu_find_vlan(vlan_id
);
1969 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1972 if (!strcmp(device
, "nic")) {
1973 static const char * const nic_params
[] = {
1974 "vlan", "name", "macaddr", "model", NULL
1978 int idx
= nic_get_free_idx();
1980 if (check_params(nic_params
, p
) < 0) {
1981 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
1985 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
1986 fprintf(stderr
, "Too Many NICs\n");
1990 nd
= &nd_table
[idx
];
1991 macaddr
= nd
->macaddr
;
1997 macaddr
[5] = 0x56 + idx
;
1999 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2000 if (parse_macaddr(macaddr
, buf
) < 0) {
2001 fprintf(stderr
, "invalid syntax for ethernet address\n");
2006 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2007 nd
->model
= strdup(buf
);
2014 vlan
->nb_guest_devs
++;
2017 if (!strcmp(device
, "none")) {
2019 fprintf(stderr
, "qemu: 'none' takes no parameters\n");
2022 /* does nothing. It is needed to signal that no network cards
2027 if (!strcmp(device
, "user")) {
2028 static const char * const slirp_params
[] = {
2029 "vlan", "name", "hostname", "restrict", "ip", NULL
2031 if (check_params(slirp_params
, p
) < 0) {
2032 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2036 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2037 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
2039 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2040 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
2042 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2043 slirp_ip
= strdup(buf
);
2045 vlan
->nb_host_devs
++;
2046 ret
= net_slirp_init(vlan
, device
, name
);
2047 } else if (!strcmp(device
, "channel")) {
2049 char name
[20], *devname
;
2050 struct VMChannel
*vmc
;
2052 port
= strtol(p
, &devname
, 10);
2054 if (port
< 1 || port
> 65535) {
2055 fprintf(stderr
, "vmchannel wrong port number\n");
2059 vmc
= malloc(sizeof(struct VMChannel
));
2060 snprintf(name
, 20, "vmchannel%ld", port
);
2061 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
2063 fprintf(stderr
, "qemu: could not open vmchannel device"
2069 slirp_add_exec(3, vmc
->hd
, 4, port
);
2070 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
2076 if (!strcmp(device
, "tap")) {
2077 static const char * const tap_params
[] = {
2078 "vlan", "name", "ifname", NULL
2082 if (check_params(tap_params
, p
) < 0) {
2083 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2087 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2088 fprintf(stderr
, "tap: no interface name\n");
2092 vlan
->nb_host_devs
++;
2093 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2095 #elif defined (_AIX)
2097 if (!strcmp(device
, "tap")) {
2099 char setup_script
[1024], down_script
[1024];
2101 vlan
->nb_host_devs
++;
2102 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2103 if (check_params(fd_params
, p
) < 0) {
2104 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2108 fd
= strtol(buf
, NULL
, 0);
2109 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2110 net_tap_fd_init(vlan
, device
, name
, fd
, tap_probe_vnet_hdr(fd
));
2113 static const char * const tap_params
[] = {
2114 "vlan", "name", "ifname", "script", "downscript", NULL
2116 if (check_params(tap_params
, p
) < 0) {
2117 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2121 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2124 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2125 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2127 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2128 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2130 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2134 if (!strcmp(device
, "socket")) {
2135 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2137 if (check_params(fd_params
, p
) < 0) {
2138 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2142 fd
= strtol(buf
, NULL
, 0);
2144 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2146 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2147 static const char * const listen_params
[] = {
2148 "vlan", "name", "listen", NULL
2150 if (check_params(listen_params
, p
) < 0) {
2151 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2155 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2156 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2157 static const char * const connect_params
[] = {
2158 "vlan", "name", "connect", NULL
2160 if (check_params(connect_params
, p
) < 0) {
2161 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2165 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2166 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2167 static const char * const mcast_params
[] = {
2168 "vlan", "name", "mcast", NULL
2170 if (check_params(mcast_params
, p
) < 0) {
2171 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2175 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2177 fprintf(stderr
, "Unknown socket options: %s\n", p
);
2181 vlan
->nb_host_devs
++;
2184 if (!strcmp(device
, "vde")) {
2185 static const char * const vde_params
[] = {
2186 "vlan", "name", "sock", "port", "group", "mode", NULL
2188 char vde_sock
[1024], vde_group
[512];
2189 int vde_port
, vde_mode
;
2191 if (check_params(vde_params
, p
) < 0) {
2192 fprintf(stderr
, "qemu: invalid parameter '%s' in '%s'\n",
2196 vlan
->nb_host_devs
++;
2197 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2200 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2201 vde_port
= strtol(buf
, NULL
, 10);
2205 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2206 vde_group
[0] = '\0';
2208 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2209 vde_mode
= strtol(buf
, NULL
, 8);
2213 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2216 if (!strcmp(device
, "dump")) {
2219 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2220 len
= strtol(buf
, NULL
, 0);
2222 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2223 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2225 ret
= net_dump_init(vlan
, device
, name
, buf
, len
);
2227 fprintf(stderr
, "Unknown network device: %s\n", device
);
2232 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
2240 void net_client_uninit(NICInfo
*nd
)
2242 nd
->vlan
->nb_guest_devs
--;
2245 free((void *)nd
->model
);
2248 static int net_host_check_device(const char *device
)
2251 const char *valid_param_list
[] = { "tap", "socket", "dump"
2259 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2260 if (!strncmp(valid_param_list
[i
], device
,
2261 strlen(valid_param_list
[i
])))
2268 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2270 if (!net_host_check_device(device
)) {
2271 monitor_printf(mon
, "invalid host network device %s\n", device
);
2274 if (net_client_init(device
, opts
? opts
: "") < 0) {
2275 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2279 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2282 VLANClientState
*vc
;
2284 vlan
= qemu_find_vlan(vlan_id
);
2286 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2287 if (!strcmp(vc
->name
, device
)) {
2293 monitor_printf(mon
, "can't find device %s\n", device
);
2296 if (!net_host_check_device(vc
->model
)) {
2297 monitor_printf(mon
, "invalid host network device %s\n", device
);
2300 qemu_del_vlan_client(vc
);
2303 int net_client_parse(const char *str
)
2311 while (*p
!= '\0' && *p
!= ',') {
2312 if ((q
- device
) < sizeof(device
) - 1)
2320 return net_client_init(device
, p
);
2323 void do_info_network(Monitor
*mon
)
2326 VLANClientState
*vc
;
2328 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2329 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2330 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2331 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2335 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2338 VLANClientState
*vc
= NULL
;
2340 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2341 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2342 if (strcmp(vc
->name
, name
) == 0)
2347 monitor_printf(mon
, "could not find network device '%s'", name
);
2351 if (strcmp(up_or_down
, "up") == 0)
2353 else if (strcmp(up_or_down
, "down") == 0)
2356 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2357 "valid\n", up_or_down
);
2359 if (vc
->link_status_changed
)
2360 vc
->link_status_changed(vc
);
2365 void net_cleanup(void)
2369 /* close network clients */
2370 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2371 VLANClientState
*vc
= vlan
->first_client
;
2374 VLANClientState
*next
= vc
->next
;
2376 qemu_del_vlan_client(vc
);
2383 void net_client_check(void)
2387 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2388 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2390 if (vlan
->nb_guest_devs
== 0)
2391 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2392 if (vlan
->nb_host_devs
== 0)
2394 "Warning: vlan %d is not connected to host network\n",