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 void qemu_send_packet_raw(VLANClientState
*sender
, const uint8_t *buf
, int size
)
461 VLANState
*vlan
= sender
->vlan
;
464 if (sender
->link_down
)
468 printf("vlan %d send raw:\n", vlan
->id
);
469 hex_dump(stdout
, buf
, size
);
471 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
472 if (vc
== sender
|| vc
->link_down
) {
475 if (vc
->fd_read_raw
) {
476 vc
->fd_read_raw(vc
->opaque
, buf
, size
);
478 vc
->fd_read(vc
->opaque
, buf
, size
);
484 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
487 uint8_t buffer
[4096];
491 for (i
= 0; i
< iovcnt
; i
++) {
494 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
495 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
499 vc
->fd_read(vc
->opaque
, buffer
, offset
);
504 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
509 for (i
= 0; i
< iovcnt
; i
++)
510 offset
+= iov
[i
].iov_len
;
514 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
517 VLANState
*vlan
= vc1
->vlan
;
522 return calc_iov_length(iov
, iovcnt
);
524 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
531 len
= calc_iov_length(iov
, iovcnt
);
533 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
534 else if (vc
->fd_read
)
535 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
537 max_len
= MAX(max_len
, len
);
543 #if defined(CONFIG_SLIRP)
545 /* slirp network adapter */
547 static int slirp_inited
;
548 static int slirp_restrict
;
549 static char *slirp_ip
;
550 static VLANClientState
*slirp_vc
;
552 int slirp_can_output(void)
554 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
557 void slirp_output(const uint8_t *pkt
, int pkt_len
)
560 printf("slirp output:\n");
561 hex_dump(stdout
, pkt
, pkt_len
);
565 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
568 int slirp_is_inited(void)
573 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
576 printf("slirp input:\n");
577 hex_dump(stdout
, buf
, size
);
579 slirp_input(buf
, size
);
582 static int slirp_in_use
;
584 static void net_slirp_cleanup(VLANClientState
*vc
)
589 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
592 /* slirp only supports a single instance so far */
597 slirp_init(slirp_restrict
, slirp_ip
);
599 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
600 slirp_receive
, NULL
, net_slirp_cleanup
, NULL
);
601 slirp_vc
->info_str
[0] = '\0';
606 static void net_slirp_redir_print(void *opaque
, int is_udp
,
607 struct in_addr
*laddr
, u_int lport
,
608 struct in_addr
*faddr
, u_int fport
)
610 Monitor
*mon
= (Monitor
*)opaque
;
615 h_addr
= ntohl(faddr
->s_addr
);
616 g_addr
= ntohl(laddr
->s_addr
);
618 monitor_printf(mon
, " %s |", is_udp
? "udp" : "tcp" );
619 snprintf(buf
, 15, "%d.%d.%d.%d", (h_addr
>> 24) & 0xff,
620 (h_addr
>> 16) & 0xff,
621 (h_addr
>> 8) & 0xff,
623 monitor_printf(mon
, " %15s |", buf
);
624 monitor_printf(mon
, " %5d |", fport
);
626 snprintf(buf
, 15, "%d.%d.%d.%d", (g_addr
>> 24) & 0xff,
627 (g_addr
>> 16) & 0xff,
628 (g_addr
>> 8) & 0xff,
630 monitor_printf(mon
, " %15s |", buf
);
631 monitor_printf(mon
, " %5d\n", lport
);
635 static void net_slirp_redir_list(Monitor
*mon
)
640 monitor_printf(mon
, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
641 monitor_printf(mon
, " | | | | \n");
642 slirp_redir_loop(net_slirp_redir_print
, mon
);
645 static void net_slirp_redir_rm(Monitor
*mon
, const char *port_str
)
649 const char *p
= port_str
;
656 if (!port_str
|| !port_str
[0])
659 get_str_sep(buf
, sizeof(buf
), &p
, ':');
661 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
663 } else if (!strcmp(buf
, "udp")) {
671 n
= slirp_redir_rm(is_udp
, host_port
);
673 monitor_printf(mon
, "removed %d redirections to %s port %d\n", n
,
674 is_udp
? "udp" : "tcp", host_port
);
678 monitor_printf(mon
, "invalid format\n");
681 void net_slirp_redir(Monitor
*mon
, const char *redir_str
, const char *redir_opt2
)
685 const char *p
, *errmsg
;
686 struct in_addr guest_addr
;
687 int host_port
, guest_port
;
691 slirp_init(slirp_restrict
, slirp_ip
);
694 if (!strcmp(redir_str
, "remove")) {
695 net_slirp_redir_rm(mon
, redir_opt2
);
699 if (!strcmp(redir_str
, "list")) {
700 net_slirp_redir_list(mon
);
705 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
707 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
709 } else if (!strcmp(buf
, "udp")) {
715 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
717 host_port
= strtol(buf
, &r
, 0);
721 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
723 if (buf
[0] == '\0') {
724 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
726 if (!inet_aton(buf
, &guest_addr
))
729 guest_port
= strtol(p
, &r
, 0);
733 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
734 errmsg
= "could not set up redirection\n";
740 errmsg
= "invalid redirection format\n";
743 monitor_printf(mon
, "%s", errmsg
);
745 fprintf(stderr
, "qemu: %s", errmsg
);
752 static char smb_dir
[1024];
754 static void erase_dir(char *dir_name
)
760 /* erase all the files in the directory */
761 if ((d
= opendir(dir_name
)) != NULL
) {
766 if (strcmp(de
->d_name
, ".") != 0 &&
767 strcmp(de
->d_name
, "..") != 0) {
768 snprintf(filename
, sizeof(filename
), "%s/%s",
769 smb_dir
, de
->d_name
);
770 if (unlink(filename
) != 0) /* is it a directory? */
779 /* automatic user mode samba server configuration */
780 static void smb_exit(void)
785 /* automatic user mode samba server configuration */
786 void net_slirp_smb(const char *exported_dir
)
789 char smb_cmdline
[1024];
794 slirp_init(slirp_restrict
, slirp_ip
);
797 /* XXX: better tmp dir construction */
798 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%ld", (long)getpid());
799 if (mkdir(smb_dir
, 0700) < 0) {
800 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
803 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
805 f
= fopen(smb_conf
, "w");
807 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
814 "socket address=127.0.0.1\n"
816 "lock directory=%s\n"
817 "log file=%s/log.smbd\n"
818 "smb passwd file=%s/smbpasswd\n"
834 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
835 SMBD_COMMAND
, smb_conf
);
837 slirp_add_exec(0, smb_cmdline
, 4, 139);
840 #endif /* !defined(_WIN32) */
841 void do_info_slirp(Monitor
*mon
)
851 static int vmchannel_can_read(void *opaque
)
853 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
854 return slirp_socket_can_recv(4, vmc
->port
);
857 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
859 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
860 slirp_socket_recv(4, vmc
->port
, buf
, size
);
863 #endif /* CONFIG_SLIRP */
867 int tap_has_vnet_hdr(void *opaque
)
872 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
876 #else /* !defined(_WIN32) */
878 /* Maximum GSO packet size (64k) plus plenty of room for
879 * the ethernet and virtio_net headers
881 #define TAP_BUFSIZE (4096 + 65536)
884 #include <linux/virtio_net.h>
887 typedef struct TAPState
{
890 char down_script
[1024];
891 char down_script_arg
[128];
892 char buf
[TAP_BUFSIZE
];
894 unsigned int has_vnet_hdr
: 1;
895 unsigned int using_vnet_hdr
: 1;
898 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
900 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
903 TAPState
*s
= opaque
;
907 len
= writev(s
->fd
, iov
, iovcnt
);
908 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
913 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
919 TAPState
*s
= opaque
;
920 struct virtio_net_hdr hdr
= { 0, };
922 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
923 iov
[i
].iov_base
= &hdr
;
924 iov
[i
].iov_len
= sizeof(hdr
);
929 iov
[i
].iov_base
= (char *) buf
;
930 iov
[i
].iov_len
= size
;
933 tap_receive_iov(opaque
, iov
, i
);
936 static void tap_receive_raw(void *opaque
, const uint8_t *buf
, int size
)
942 TAPState
*s
= opaque
;
943 struct virtio_net_hdr hdr
= { 0, };
945 if (s
->has_vnet_hdr
&& s
->using_vnet_hdr
) {
946 iov
[i
].iov_base
= &hdr
;
947 iov
[i
].iov_len
= sizeof(hdr
);
952 iov
[i
].iov_base
= (char *) buf
;
953 iov
[i
].iov_len
= size
;
956 tap_receive_iov(opaque
, iov
, i
);
959 static int tap_can_send(void *opaque
)
961 TAPState
*s
= opaque
;
965 /* Check to see if any of our clients can receive a packet */
966 for (vc
= s
->vc
->vlan
->first_client
; vc
; vc
= vc
->next
) {
971 if (!vc
->fd_can_read
) {
972 /* no fd_can_read handler, they always can receive */
975 can_receive
= vc
->fd_can_read(vc
->opaque
);
977 /* Once someone can receive, we try to send a packet */
985 static int tap_send_packet(TAPState
*s
)
987 uint8_t *buf
= (uint8_t *)s
->buf
;
991 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
992 buf
+= sizeof(struct virtio_net_hdr
);
993 size
-= sizeof(struct virtio_net_hdr
);
997 return qemu_send_packet(s
->vc
, buf
, size
);
1000 static void tap_send(void *opaque
)
1002 TAPState
*s
= opaque
;
1004 /* First try to send any buffered packet */
1008 /* If noone can receive the packet, buffer it */
1009 err
= tap_send_packet(s
);
1014 /* Read packets until we hit EAGAIN */
1019 sbuf
.maxlen
= sizeof(buf
);
1020 sbuf
.buf
= (char *)buf
;
1021 s
->size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
1023 // FIXME: kvm_sleep_begin();
1024 s
->size
= read(s
->fd
, s
->buf
, sizeof(s
->buf
));
1025 // FIXME: kvm_sleep_end();
1028 if (s
->size
== -1 && errno
== EINTR
)
1034 /* If noone can receive the packet, buffer it */
1035 err
= tap_send_packet(s
);
1039 } while (s
->size
> 0);
1042 int tap_has_vnet_hdr(void *opaque
)
1044 VLANClientState
*vc
= opaque
;
1045 TAPState
*s
= vc
->opaque
;
1047 return s
? s
->has_vnet_hdr
: 0;
1050 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1052 VLANClientState
*vc
= opaque
;
1053 TAPState
*s
= vc
->opaque
;
1055 if (!s
|| !s
->has_vnet_hdr
)
1058 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
1061 static int tap_probe_vnet_hdr(int fd
)
1063 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1066 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
1067 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
1071 return ifr
.ifr_flags
& IFF_VNET_HDR
;
1077 #ifdef TUNSETOFFLOAD
1078 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
1081 TAPState
*s
= vc
->opaque
;
1082 unsigned int offload
= 0;
1085 offload
|= TUN_F_CSUM
;
1087 offload
|= TUN_F_TSO4
;
1089 offload
|= TUN_F_TSO6
;
1090 if ((tso4
|| tso6
) && ecn
)
1091 offload
|= TUN_F_TSO_ECN
;
1094 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
1095 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
1098 #endif /* TUNSETOFFLOAD */
1100 static void tap_cleanup(VLANClientState
*vc
)
1102 TAPState
*s
= vc
->opaque
;
1104 if (s
->down_script
[0])
1105 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1107 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1114 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1122 s
= qemu_mallocz(sizeof(TAPState
));
1124 s
->has_vnet_hdr
= vnet_hdr
!= 0;
1125 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
,
1126 NULL
, tap_cleanup
, s
);
1127 s
->vc
->fd_readv
= tap_receive_iov
;
1128 s
->vc
->fd_read_raw
= tap_receive_raw
;
1129 #ifdef TUNSETOFFLOAD
1130 s
->vc
->set_offload
= tap_set_offload
;
1131 tap_set_offload(s
->vc
, 0, 0, 0, 0);
1133 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
1134 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1138 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1139 static int tap_open(char *ifname
, int ifname_size
)
1145 TFR(fd
= open("/dev/tap", O_RDWR
));
1147 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1152 dev
= devname(s
.st_rdev
, S_IFCHR
);
1153 pstrcpy(ifname
, ifname_size
, dev
);
1155 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1158 #elif defined(__sun__)
1159 #define TUNNEWPPA (('T'<<16) | 0x0001)
1161 * Allocate TAP device, returns opened fd.
1162 * Stores dev name in the first arg(must be large enough).
1164 static int tap_alloc(char *dev
, size_t dev_size
)
1166 int tap_fd
, if_fd
, ppa
= -1;
1167 static int ip_fd
= 0;
1170 static int arp_fd
= 0;
1171 int ip_muxid
, arp_muxid
;
1172 struct strioctl strioc_if
, strioc_ppa
;
1173 int link_type
= I_PLINK
;;
1175 char actual_name
[32] = "";
1177 memset(&ifr
, 0x0, sizeof(ifr
));
1181 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1185 /* Check if IP device was opened */
1189 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1191 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1195 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1197 syslog(LOG_ERR
, "Can't open /dev/tap");
1201 /* Assign a new PPA and get its unit number. */
1202 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1203 strioc_ppa
.ic_timout
= 0;
1204 strioc_ppa
.ic_len
= sizeof(ppa
);
1205 strioc_ppa
.ic_dp
= (char *)&ppa
;
1206 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1207 syslog (LOG_ERR
, "Can't assign new interface");
1209 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1211 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1214 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1215 syslog(LOG_ERR
, "Can't push IP module");
1219 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1220 syslog(LOG_ERR
, "Can't get flags\n");
1222 snprintf (actual_name
, 32, "tap%d", ppa
);
1223 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1226 /* Assign ppa according to the unit number returned by tun device */
1228 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1229 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1230 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1231 syslog (LOG_ERR
, "Can't get flags\n");
1232 /* Push arp module to if_fd */
1233 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1234 syslog (LOG_ERR
, "Can't push ARP module (2)");
1236 /* Push arp module to ip_fd */
1237 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1238 syslog (LOG_ERR
, "I_POP failed\n");
1239 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1240 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1242 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1244 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1246 /* Set ifname to arp */
1247 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1248 strioc_if
.ic_timout
= 0;
1249 strioc_if
.ic_len
= sizeof(ifr
);
1250 strioc_if
.ic_dp
= (char *)&ifr
;
1251 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1252 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1255 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1256 syslog(LOG_ERR
, "Can't link TAP device to IP");
1260 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1261 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1265 memset(&ifr
, 0x0, sizeof(ifr
));
1266 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1267 ifr
.lifr_ip_muxid
= ip_muxid
;
1268 ifr
.lifr_arp_muxid
= arp_muxid
;
1270 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1272 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1273 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1274 syslog (LOG_ERR
, "Can't set multiplexor id");
1277 snprintf(dev
, dev_size
, "tap%d", ppa
);
1281 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1285 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1286 fprintf(stderr
, "Cannot allocate TAP device\n");
1289 pstrcpy(ifname
, ifname_size
, dev
);
1290 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1293 #elif defined (_AIX)
1294 static int tap_open(char *ifname
, int ifname_size
)
1296 fprintf (stderr
, "no tap on AIX\n");
1300 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1305 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1307 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1310 memset(&ifr
, 0, sizeof(ifr
));
1311 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1313 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1315 unsigned int features
;
1317 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1318 features
& IFF_VNET_HDR
) {
1320 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1325 if (ifname
[0] != '\0')
1326 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1328 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1329 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1331 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1335 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1336 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1341 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1347 /* try to launch network script */
1351 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
1352 for (i
= 0; i
< open_max
; i
++)
1353 if (i
!= STDIN_FILENO
&&
1354 i
!= STDOUT_FILENO
&&
1355 i
!= STDERR_FILENO
&&
1360 *parg
++ = (char *)setup_script
;
1361 *parg
++ = (char *)ifname
;
1363 execv(setup_script
, args
);
1366 while (waitpid(pid
, &status
, 0) != pid
);
1367 if (!WIFEXITED(status
) ||
1368 WEXITSTATUS(status
) != 0) {
1369 fprintf(stderr
, "%s: could not launch network script\n",
1377 static int net_tap_init(VLANState
*vlan
, const char *model
,
1378 const char *name
, const char *ifname1
,
1379 const char *setup_script
, const char *down_script
)
1386 if (ifname1
!= NULL
)
1387 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1391 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1395 if (!setup_script
|| !strcmp(setup_script
, "no"))
1397 if (setup_script
[0] != '\0') {
1398 if (launch_script(setup_script
, ifname
, fd
))
1401 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1402 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1403 "ifname=%s,script=%s,downscript=%s",
1404 ifname
, setup_script
, down_script
);
1405 if (down_script
&& strcmp(down_script
, "no")) {
1406 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1407 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1412 #endif /* !_WIN32 */
1414 #if defined(CONFIG_VDE)
1415 typedef struct VDEState
{
1416 VLANClientState
*vc
;
1420 static void vde_to_qemu(void *opaque
)
1422 VDEState
*s
= opaque
;
1426 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1428 qemu_send_packet(s
->vc
, buf
, size
);
1432 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1434 VDEState
*s
= opaque
;
1437 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1438 if (ret
< 0 && errno
== EINTR
) {
1445 static void vde_cleanup(VLANClientState
*vc
)
1447 VDEState
*s
= vc
->opaque
;
1448 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1453 static int net_vde_init(VLANState
*vlan
, const char *model
,
1454 const char *name
, const char *sock
,
1455 int port
, const char *group
, int mode
)
1458 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1459 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1461 struct vde_open_args args
= {
1463 .group
= init_group
,
1467 s
= qemu_mallocz(sizeof(VDEState
));
1468 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1473 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
,
1474 NULL
, vde_cleanup
, s
);
1475 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1476 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1477 sock
, vde_datafd(s
->vde
));
1482 /* network connection */
1483 typedef struct NetSocketState
{
1484 VLANClientState
*vc
;
1486 int state
; /* 0 = getting length, 1 = getting data */
1488 unsigned int packet_len
;
1490 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1493 typedef struct NetSocketListenState
{
1498 } NetSocketListenState
;
1500 /* XXX: we consider we can send the whole packet without blocking */
1501 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1503 NetSocketState
*s
= opaque
;
1507 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1508 send_all(s
->fd
, buf
, size
);
1511 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1513 NetSocketState
*s
= opaque
;
1514 sendto(s
->fd
, buf
, size
, 0,
1515 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1518 static void net_socket_send(void *opaque
)
1520 NetSocketState
*s
= opaque
;
1526 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1528 err
= socket_error();
1529 if (err
!= EWOULDBLOCK
)
1531 } else if (size
== 0) {
1532 /* end of connection */
1534 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1540 /* reassemble a packet from the network */
1546 memcpy(s
->buf
+ s
->index
, buf
, l
);
1550 if (s
->index
== 4) {
1552 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1558 l
= s
->packet_len
- s
->index
;
1561 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1562 memcpy(s
->buf
+ s
->index
, buf
, l
);
1564 fprintf(stderr
, "serious error: oversized packet received,"
1565 "connection terminated.\n");
1573 if (s
->index
>= s
->packet_len
) {
1574 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1583 static void net_socket_send_dgram(void *opaque
)
1585 NetSocketState
*s
= opaque
;
1588 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1592 /* end of connection */
1593 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1596 qemu_send_packet(s
->vc
, s
->buf
, size
);
1599 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1604 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1605 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1606 inet_ntoa(mcastaddr
->sin_addr
),
1607 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1611 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1613 perror("socket(PF_INET, SOCK_DGRAM)");
1618 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1619 (const char *)&val
, sizeof(val
));
1621 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1625 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1631 /* Add host to multicast group */
1632 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1633 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1635 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1636 (const char *)&imr
, sizeof(struct ip_mreq
));
1638 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1642 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1644 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1645 (const char *)&val
, sizeof(val
));
1647 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1651 socket_set_nonblock(fd
);
1659 static void net_socket_cleanup(VLANClientState
*vc
)
1661 NetSocketState
*s
= vc
->opaque
;
1662 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1667 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1670 int fd
, int is_connected
)
1672 struct sockaddr_in saddr
;
1674 socklen_t saddr_len
;
1677 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1678 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1679 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1683 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1685 if (saddr
.sin_addr
.s_addr
==0) {
1686 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1690 /* clone dgram socket */
1691 newfd
= net_socket_mcast_create(&saddr
);
1693 /* error already reported by net_socket_mcast_create() */
1697 /* clone newfd to fd, close newfd */
1702 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1703 fd
, strerror(errno
));
1708 s
= qemu_mallocz(sizeof(NetSocketState
));
1711 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
,
1712 NULL
, net_socket_cleanup
, s
);
1713 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1715 /* mcast: save bound address as dst */
1716 if (is_connected
) s
->dgram_dst
=saddr
;
1718 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1719 "socket: fd=%d (%s mcast=%s:%d)",
1720 fd
, is_connected
? "cloned" : "",
1721 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1725 static void net_socket_connect(void *opaque
)
1727 NetSocketState
*s
= opaque
;
1728 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1731 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1734 int fd
, int is_connected
)
1737 s
= qemu_mallocz(sizeof(NetSocketState
));
1739 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive
,
1740 NULL
, net_socket_cleanup
, s
);
1741 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1742 "socket: fd=%d", fd
);
1744 net_socket_connect(s
);
1746 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1751 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1752 const char *model
, const char *name
,
1753 int fd
, int is_connected
)
1755 int so_type
=-1, optlen
=sizeof(so_type
);
1757 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1758 (socklen_t
*)&optlen
)< 0) {
1759 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1764 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1766 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1768 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1769 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1770 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1775 static void net_socket_accept(void *opaque
)
1777 NetSocketListenState
*s
= opaque
;
1779 struct sockaddr_in saddr
;
1784 len
= sizeof(saddr
);
1785 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1786 if (fd
< 0 && errno
!= EINTR
) {
1788 } else if (fd
>= 0) {
1792 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1796 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1797 "socket: connection from %s:%d",
1798 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1802 static int net_socket_listen_init(VLANState
*vlan
,
1805 const char *host_str
)
1807 NetSocketListenState
*s
;
1809 struct sockaddr_in saddr
;
1811 if (parse_host_port(&saddr
, host_str
) < 0)
1814 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1816 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1821 socket_set_nonblock(fd
);
1823 /* allow fast reuse */
1825 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1827 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1832 ret
= listen(fd
, 0);
1838 s
->model
= strdup(model
);
1839 s
->name
= name
? strdup(name
) : NULL
;
1841 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1845 static int net_socket_connect_init(VLANState
*vlan
,
1848 const char *host_str
)
1851 int fd
, connected
, ret
, err
;
1852 struct sockaddr_in saddr
;
1854 if (parse_host_port(&saddr
, host_str
) < 0)
1857 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1862 socket_set_nonblock(fd
);
1866 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1868 err
= socket_error();
1869 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1870 } else if (err
== EINPROGRESS
) {
1873 } else if (err
== WSAEALREADY
) {
1886 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1889 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1890 "socket: connect to %s:%d",
1891 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1895 static int net_socket_mcast_init(VLANState
*vlan
,
1898 const char *host_str
)
1902 struct sockaddr_in saddr
;
1904 if (parse_host_port(&saddr
, host_str
) < 0)
1908 fd
= net_socket_mcast_create(&saddr
);
1912 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1916 s
->dgram_dst
= saddr
;
1918 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1919 "socket: mcast=%s:%d",
1920 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1925 typedef struct DumpState
{
1926 VLANClientState
*pcap_vc
;
1931 #define PCAP_MAGIC 0xa1b2c3d4
1933 struct pcap_file_hdr
{
1935 uint16_t version_major
;
1936 uint16_t version_minor
;
1943 struct pcap_sf_pkthdr
{
1952 static void dump_receive(void *opaque
, const uint8_t *buf
, int size
)
1954 DumpState
*s
= opaque
;
1955 struct pcap_sf_pkthdr hdr
;
1959 /* Early return in case of previous error. */
1964 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, ticks_per_sec
);
1965 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1967 hdr
.ts
.tv_sec
= ts
/ 1000000;
1968 hdr
.ts
.tv_usec
= ts
% 1000000;
1969 hdr
.caplen
= caplen
;
1971 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1972 write(s
->fd
, buf
, caplen
) != caplen
) {
1973 qemu_log("-net dump write error - stop dump\n");
1979 static void net_dump_cleanup(VLANClientState
*vc
)
1981 DumpState
*s
= vc
->opaque
;
1987 static int net_dump_init(VLANState
*vlan
, const char *device
,
1988 const char *name
, const char *filename
, int len
)
1990 struct pcap_file_hdr hdr
;
1993 s
= qemu_malloc(sizeof(DumpState
));
1995 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
, 0644);
1997 fprintf(stderr
, "-net dump: can't open %s\n", filename
);
2001 s
->pcap_caplen
= len
;
2003 hdr
.magic
= PCAP_MAGIC
;
2004 hdr
.version_major
= 2;
2005 hdr
.version_minor
= 4;
2008 hdr
.snaplen
= s
->pcap_caplen
;
2011 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2012 perror("-net dump write error");
2018 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, dump_receive
, NULL
,
2019 net_dump_cleanup
, s
);
2020 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2021 "dump to %s (len=%d)", filename
, len
);
2025 /* find or alloc a new VLAN */
2026 VLANState
*qemu_find_vlan(int id
)
2028 VLANState
**pvlan
, *vlan
;
2029 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2033 vlan
= qemu_mallocz(sizeof(VLANState
));
2036 pvlan
= &first_vlan
;
2037 while (*pvlan
!= NULL
)
2038 pvlan
= &(*pvlan
)->next
;
2043 static int nic_get_free_idx(void)
2047 for (index
= 0; index
< MAX_NICS
; index
++)
2048 if (!nd_table
[index
].used
)
2053 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2055 const char *models
[2];
2060 qemu_check_nic_model_list(nd
, models
, model
);
2063 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
2064 const char *default_model
)
2066 int i
, exit_status
= 0;
2069 nd
->model
= strdup(default_model
);
2071 if (strcmp(nd
->model
, "?") != 0) {
2072 for (i
= 0 ; models
[i
]; i
++)
2073 if (strcmp(nd
->model
, models
[i
]) == 0)
2076 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
2080 fprintf(stderr
, "qemu: Supported NIC models: ");
2081 for (i
= 0 ; models
[i
]; i
++)
2082 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2087 int net_client_init(const char *device
, const char *p
)
2089 static const char * const fd_params
[] = {
2090 "vlan", "name", "fd", NULL
2098 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
2099 vlan_id
= strtol(buf
, NULL
, 0);
2101 vlan
= qemu_find_vlan(vlan_id
);
2103 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
2106 if (!strcmp(device
, "nic")) {
2107 static const char * const nic_params
[] = {
2108 "vlan", "name", "macaddr", "model", NULL
2112 int idx
= nic_get_free_idx();
2114 if (check_params(nic_params
, p
) < 0) {
2115 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2118 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2119 fprintf(stderr
, "Too Many NICs\n");
2123 nd
= &nd_table
[idx
];
2124 macaddr
= nd
->macaddr
;
2130 macaddr
[5] = 0x56 + idx
;
2132 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2133 if (parse_macaddr(macaddr
, buf
) < 0) {
2134 fprintf(stderr
, "invalid syntax for ethernet address\n");
2139 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2140 nd
->model
= strdup(buf
);
2147 vlan
->nb_guest_devs
++;
2150 if (!strcmp(device
, "none")) {
2152 fprintf(stderr
, "qemu: 'none' takes no parameters\n");
2155 /* does nothing. It is needed to signal that no network cards
2160 if (!strcmp(device
, "user")) {
2161 static const char * const slirp_params
[] = {
2162 "vlan", "name", "hostname", "restrict", "ip", NULL
2164 if (check_params(slirp_params
, p
) < 0) {
2165 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2168 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2169 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
2171 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2172 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
2174 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2175 slirp_ip
= strdup(buf
);
2177 vlan
->nb_host_devs
++;
2178 ret
= net_slirp_init(vlan
, device
, name
);
2179 } else if (!strcmp(device
, "channel")) {
2181 char name
[20], *devname
;
2182 struct VMChannel
*vmc
;
2184 port
= strtol(p
, &devname
, 10);
2186 if (port
< 1 || port
> 65535) {
2187 fprintf(stderr
, "vmchannel wrong port number\n");
2191 vmc
= malloc(sizeof(struct VMChannel
));
2192 snprintf(name
, 20, "vmchannel%ld", port
);
2193 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
2195 fprintf(stderr
, "qemu: could not open vmchannel device"
2201 slirp_add_exec(3, vmc
->hd
, 4, port
);
2202 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
2208 if (!strcmp(device
, "tap")) {
2209 static const char * const tap_params
[] = {
2210 "vlan", "name", "ifname", NULL
2214 if (check_params(tap_params
, p
) < 0) {
2215 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2218 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2219 fprintf(stderr
, "tap: no interface name\n");
2223 vlan
->nb_host_devs
++;
2224 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2226 #elif defined (_AIX)
2228 if (!strcmp(device
, "tap")) {
2230 char setup_script
[1024], down_script
[1024];
2232 vlan
->nb_host_devs
++;
2233 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2234 if (check_params(fd_params
, p
) < 0) {
2235 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2238 fd
= strtol(buf
, NULL
, 0);
2239 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2240 net_tap_fd_init(vlan
, device
, name
, fd
, tap_probe_vnet_hdr(fd
));
2243 static const char * const tap_params
[] = {
2244 "vlan", "name", "ifname", "script", "downscript", NULL
2246 if (check_params(tap_params
, p
) < 0) {
2247 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2250 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2253 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2254 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2256 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2257 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2259 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2263 if (!strcmp(device
, "socket")) {
2264 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2266 if (check_params(fd_params
, p
) < 0) {
2267 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2270 fd
= strtol(buf
, NULL
, 0);
2272 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2274 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2275 static const char * const listen_params
[] = {
2276 "vlan", "name", "listen", NULL
2278 if (check_params(listen_params
, p
) < 0) {
2279 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2282 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2283 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2284 static const char * const connect_params
[] = {
2285 "vlan", "name", "connect", NULL
2287 if (check_params(connect_params
, p
) < 0) {
2288 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2291 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2292 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2293 static const char * const mcast_params
[] = {
2294 "vlan", "name", "mcast", NULL
2296 if (check_params(mcast_params
, p
) < 0) {
2297 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2300 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2302 fprintf(stderr
, "Unknown socket options: %s\n", p
);
2306 vlan
->nb_host_devs
++;
2309 if (!strcmp(device
, "vde")) {
2310 static const char * const vde_params
[] = {
2311 "vlan", "name", "sock", "port", "group", "mode", NULL
2313 char vde_sock
[1024], vde_group
[512];
2314 int vde_port
, vde_mode
;
2316 if (check_params(vde_params
, p
) < 0) {
2317 fprintf(stderr
, "qemu: invalid parameter in '%s'\n", p
);
2320 vlan
->nb_host_devs
++;
2321 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2324 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2325 vde_port
= strtol(buf
, NULL
, 10);
2329 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2330 vde_group
[0] = '\0';
2332 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2333 vde_mode
= strtol(buf
, NULL
, 8);
2337 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2340 if (!strcmp(device
, "dump")) {
2343 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2344 len
= strtol(buf
, NULL
, 0);
2346 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2347 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2349 ret
= net_dump_init(vlan
, device
, name
, buf
, len
);
2351 fprintf(stderr
, "Unknown network device: %s\n", device
);
2356 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
2364 void net_client_uninit(NICInfo
*nd
)
2366 nd
->vlan
->nb_guest_devs
--;
2369 free((void *)nd
->model
);
2372 static int net_host_check_device(const char *device
)
2375 const char *valid_param_list
[] = { "tap", "socket", "dump"
2383 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2384 if (!strncmp(valid_param_list
[i
], device
,
2385 strlen(valid_param_list
[i
])))
2392 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2394 if (!net_host_check_device(device
)) {
2395 monitor_printf(mon
, "invalid host network device %s\n", device
);
2398 if (net_client_init(device
, opts
? opts
: "") < 0) {
2399 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2403 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2406 VLANClientState
*vc
;
2408 vlan
= qemu_find_vlan(vlan_id
);
2410 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2411 if (!strcmp(vc
->name
, device
)) {
2417 monitor_printf(mon
, "can't find device %s\n", device
);
2420 if (!net_host_check_device(vc
->model
)) {
2421 monitor_printf(mon
, "invalid host network device %s\n", device
);
2424 qemu_del_vlan_client(vc
);
2427 int net_client_parse(const char *str
)
2435 while (*p
!= '\0' && *p
!= ',') {
2436 if ((q
- device
) < sizeof(device
) - 1)
2444 return net_client_init(device
, p
);
2447 void do_info_network(Monitor
*mon
)
2450 VLANClientState
*vc
;
2452 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2453 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2454 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2455 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2459 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2462 VLANClientState
*vc
= NULL
;
2464 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2465 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2466 if (strcmp(vc
->name
, name
) == 0)
2471 monitor_printf(mon
, "could not find network device '%s'", name
);
2475 if (strcmp(up_or_down
, "up") == 0)
2477 else if (strcmp(up_or_down
, "down") == 0)
2480 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2481 "valid\n", up_or_down
);
2483 if (vc
->link_status_changed
)
2484 vc
->link_status_changed(vc
);
2489 void net_cleanup(void)
2493 /* close network clients */
2494 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2495 VLANClientState
*vc
= vlan
->first_client
;
2498 VLANClientState
*next
= vc
->next
;
2500 qemu_del_vlan_client(vc
);
2507 void net_client_check(void)
2511 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2512 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2514 if (vlan
->nb_guest_devs
== 0)
2515 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2516 if (vlan
->nb_host_devs
== 0)
2518 "Warning: vlan %d is not connected to host network\n",