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 NetCanReceive
*can_receive
,
338 NetReceiveIOV
*receive_iov
,
342 VLANClientState
*vc
, **pvc
;
343 vc
= qemu_mallocz(sizeof(VLANClientState
));
344 vc
->model
= strdup(model
);
346 vc
->name
= strdup(name
);
348 vc
->name
= assign_name(vc
, model
);
349 vc
->can_receive
= can_receive
;
350 vc
->receive
= receive
;
351 vc
->receive_iov
= receive_iov
;
352 vc
->cleanup
= cleanup
;
357 pvc
= &vlan
->first_client
;
364 void qemu_del_vlan_client(VLANClientState
*vc
)
366 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
382 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
384 VLANClientState
**pvc
= &vlan
->first_client
;
387 if ((*pvc
)->opaque
== opaque
)
395 int qemu_can_send_packet(VLANClientState
*sender
)
397 VLANState
*vlan
= sender
->vlan
;
400 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
405 /* no can_receive() handler, they can always receive */
406 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
414 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
, int raw
)
419 sender
->vlan
->delivering
= 1;
421 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
433 if (raw
&& vc
->receive_raw
) {
434 len
= vc
->receive_raw(vc
, buf
, size
);
436 len
= vc
->receive(vc
, buf
, size
);
439 ret
= (ret
>= 0) ? ret
: len
;
442 sender
->vlan
->delivering
= 0;
447 void qemu_flush_queued_packets(VLANClientState
*vc
)
451 while ((packet
= vc
->vlan
->send_queue
) != NULL
) {
454 vc
->vlan
->send_queue
= packet
->next
;
456 ret
= qemu_deliver_packet(packet
->sender
, packet
->data
,
457 packet
->size
, packet
->raw
);
458 if (ret
== 0 && packet
->sent_cb
!= NULL
) {
459 packet
->next
= vc
->vlan
->send_queue
;
460 vc
->vlan
->send_queue
= packet
;
465 packet
->sent_cb(packet
->sender
);
471 static void qemu_enqueue_packet(VLANClientState
*sender
,
472 const uint8_t *buf
, int size
, int raw
,
473 NetPacketSent
*sent_cb
)
477 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
478 packet
->next
= sender
->vlan
->send_queue
;
479 packet
->sender
= sender
;
482 packet
->sent_cb
= sent_cb
;
483 memcpy(packet
->data
, buf
, size
);
484 sender
->vlan
->send_queue
= packet
;
487 static ssize_t
qemu_send_packet_async2(VLANClientState
*sender
,
488 const uint8_t *buf
, int size
, int raw
,
489 NetPacketSent
*sent_cb
)
493 if (sender
->link_down
) {
498 printf("vlan %d send:\n", sender
->vlan
->id
);
499 hex_dump(stdout
, buf
, size
);
502 if (sender
->vlan
->delivering
) {
503 qemu_enqueue_packet(sender
, buf
, size
, raw
, NULL
);
507 ret
= qemu_deliver_packet(sender
, buf
, size
, raw
);
508 if (ret
== 0 && sent_cb
!= NULL
) {
509 qemu_enqueue_packet(sender
, buf
, size
, raw
, sent_cb
);
513 qemu_flush_queued_packets(sender
);
518 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
519 const uint8_t *buf
, int size
,
520 NetPacketSent
*sent_cb
)
522 return qemu_send_packet_async2(sender
, buf
, size
, 0, sent_cb
);
525 ssize_t
qemu_send_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
527 return qemu_send_packet_async2(sender
, buf
, size
, 0, NULL
);
530 ssize_t
qemu_send_packet_raw(VLANClientState
*sender
, const uint8_t *buf
, int size
)
532 return qemu_send_packet_async2(sender
, buf
, size
, 1, NULL
);
535 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
538 uint8_t buffer
[4096];
542 for (i
= 0; i
< iovcnt
; i
++) {
545 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
546 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
550 return vc
->receive(vc
, buffer
, offset
);
553 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
558 for (i
= 0; i
< iovcnt
; i
++)
559 offset
+= iov
[i
].iov_len
;
563 static int qemu_deliver_packet_iov(VLANClientState
*sender
,
564 const struct iovec
*iov
, int iovcnt
)
569 sender
->vlan
->delivering
= 1;
571 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
579 ret
= calc_iov_length(iov
, iovcnt
);
583 if (vc
->receive_iov
) {
584 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
586 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
589 ret
= (ret
>= 0) ? ret
: len
;
592 sender
->vlan
->delivering
= 0;
597 static ssize_t
qemu_enqueue_packet_iov(VLANClientState
*sender
,
598 const struct iovec
*iov
, int iovcnt
,
599 NetPacketSent
*sent_cb
)
605 max_len
= calc_iov_length(iov
, iovcnt
);
607 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
608 packet
->next
= sender
->vlan
->send_queue
;
609 packet
->sender
= sender
;
610 packet
->sent_cb
= sent_cb
;
614 for (i
= 0; i
< iovcnt
; i
++) {
615 size_t len
= iov
[i
].iov_len
;
617 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
621 sender
->vlan
->send_queue
= packet
;
626 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
627 const struct iovec
*iov
, int iovcnt
,
628 NetPacketSent
*sent_cb
)
632 if (sender
->link_down
) {
633 return calc_iov_length(iov
, iovcnt
);
636 if (sender
->vlan
->delivering
) {
637 return qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, NULL
);
640 ret
= qemu_deliver_packet_iov(sender
, iov
, iovcnt
);
641 if (ret
== 0 && sent_cb
!= NULL
) {
642 qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, sent_cb
);
646 qemu_flush_queued_packets(sender
);
652 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
654 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
657 static void config_error(Monitor
*mon
, const char *fmt
, ...)
663 monitor_vprintf(mon
, fmt
, ap
);
665 fprintf(stderr
, "qemu: ");
666 vfprintf(stderr
, fmt
, ap
);
672 #if defined(CONFIG_SLIRP)
674 /* slirp network adapter */
676 struct slirp_config_str
{
677 struct slirp_config_str
*next
;
681 static int slirp_inited
;
682 static struct slirp_config_str
*slirp_redirs
;
684 static const char *slirp_smb_export
;
686 static VLANClientState
*slirp_vc
;
689 static void slirp_smb(const char *exported_dir
);
691 static void slirp_redirection(Monitor
*mon
, const char *redir_str
);
693 int slirp_can_output(void)
695 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
698 void slirp_output(const uint8_t *pkt
, int pkt_len
)
701 printf("slirp output:\n");
702 hex_dump(stdout
, pkt
, pkt_len
);
706 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
709 int slirp_is_inited(void)
714 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
717 printf("slirp input:\n");
718 hex_dump(stdout
, buf
, size
);
720 slirp_input(buf
, size
);
724 static int slirp_in_use
;
726 static void net_slirp_cleanup(VLANClientState
*vc
)
731 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
,
732 int restricted
, const char *ip
)
735 /* slirp only supports a single instance so far */
740 slirp_init(restricted
, ip
);
742 while (slirp_redirs
) {
743 struct slirp_config_str
*config
= slirp_redirs
;
745 slirp_redirection(NULL
, config
->str
);
746 slirp_redirs
= config
->next
;
750 if (slirp_smb_export
) {
751 slirp_smb(slirp_smb_export
);
756 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
,
757 NULL
, net_slirp_cleanup
, NULL
);
758 slirp_vc
->info_str
[0] = '\0';
763 static void net_slirp_redir_print(void *opaque
, int is_udp
,
764 struct in_addr
*laddr
, u_int lport
,
765 struct in_addr
*faddr
, u_int fport
)
767 Monitor
*mon
= (Monitor
*)opaque
;
772 h_addr
= ntohl(faddr
->s_addr
);
773 g_addr
= ntohl(laddr
->s_addr
);
775 monitor_printf(mon
, " %s |", is_udp
? "udp" : "tcp" );
776 snprintf(buf
, 15, "%d.%d.%d.%d", (h_addr
>> 24) & 0xff,
777 (h_addr
>> 16) & 0xff,
778 (h_addr
>> 8) & 0xff,
780 monitor_printf(mon
, " %15s |", buf
);
781 monitor_printf(mon
, " %5d |", fport
);
783 snprintf(buf
, 15, "%d.%d.%d.%d", (g_addr
>> 24) & 0xff,
784 (g_addr
>> 16) & 0xff,
785 (g_addr
>> 8) & 0xff,
787 monitor_printf(mon
, " %15s |", buf
);
788 monitor_printf(mon
, " %5d\n", lport
);
792 static void net_slirp_redir_list(Monitor
*mon
)
797 monitor_printf(mon
, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
798 monitor_printf(mon
, " | | | | \n");
799 slirp_redir_loop(net_slirp_redir_print
, mon
);
802 static void net_slirp_redir_rm(Monitor
*mon
, const char *port_str
)
806 const char *p
= port_str
;
813 if (!port_str
|| !port_str
[0])
816 get_str_sep(buf
, sizeof(buf
), &p
, ':');
818 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
820 } else if (!strcmp(buf
, "udp")) {
828 n
= slirp_redir_rm(is_udp
, host_port
);
830 monitor_printf(mon
, "removed %d redirections to %s port %d\n", n
,
831 is_udp
? "udp" : "tcp", host_port
);
835 monitor_printf(mon
, "invalid format\n");
838 static void slirp_redirection(Monitor
*mon
, const char *redir_str
)
840 struct in_addr guest_addr
;
841 int host_port
, guest_port
;
847 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
850 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
852 } else if (!strcmp(buf
, "udp")) {
858 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
861 host_port
= strtol(buf
, &r
, 0);
866 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
869 if (buf
[0] == '\0') {
870 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
872 if (!inet_aton(buf
, &guest_addr
)) {
876 guest_port
= strtol(p
, &r
, 0);
881 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
882 config_error(mon
, "could not set up redirection '%s'\n", redir_str
);
887 config_error(mon
, "invalid redirection format '%s'\n", redir_str
);
890 void net_slirp_redir(Monitor
*mon
, const char *redir_str
, const char *redir_opt2
)
892 struct slirp_config_str
*config
;
896 monitor_printf(mon
, "user mode network stack not in use\n");
898 config
= qemu_malloc(sizeof(*config
));
899 config
->str
= redir_str
;
900 config
->next
= slirp_redirs
;
901 slirp_redirs
= config
;
906 if (!strcmp(redir_str
, "remove")) {
907 net_slirp_redir_rm(mon
, redir_opt2
);
911 if (!strcmp(redir_str
, "list")) {
912 net_slirp_redir_list(mon
);
916 slirp_redirection(mon
, redir_str
);
921 static char smb_dir
[1024];
923 static void erase_dir(char *dir_name
)
929 /* erase all the files in the directory */
930 if ((d
= opendir(dir_name
)) != NULL
) {
935 if (strcmp(de
->d_name
, ".") != 0 &&
936 strcmp(de
->d_name
, "..") != 0) {
937 snprintf(filename
, sizeof(filename
), "%s/%s",
938 smb_dir
, de
->d_name
);
939 if (unlink(filename
) != 0) /* is it a directory? */
948 /* automatic user mode samba server configuration */
949 static void smb_exit(void)
954 static void slirp_smb(const char *exported_dir
)
957 char smb_cmdline
[1024];
960 /* XXX: better tmp dir construction */
961 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%ld", (long)getpid());
962 if (mkdir(smb_dir
, 0700) < 0) {
963 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
966 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
968 f
= fopen(smb_conf
, "w");
970 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
977 "socket address=127.0.0.1\n"
979 "lock directory=%s\n"
980 "log file=%s/log.smbd\n"
981 "smb passwd file=%s/smbpasswd\n"
997 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
998 SMBD_COMMAND
, smb_conf
);
1000 slirp_add_exec(0, smb_cmdline
, 4, 139);
1003 /* automatic user mode samba server configuration */
1004 void net_slirp_smb(const char *exported_dir
)
1006 if (slirp_smb_export
) {
1007 fprintf(stderr
, "-smb given twice\n");
1010 slirp_smb_export
= exported_dir
;
1012 slirp_smb(exported_dir
);
1016 #endif /* !defined(_WIN32) */
1018 void do_info_slirp(Monitor
*mon
)
1024 CharDriverState
*hd
;
1028 static int vmchannel_can_read(void *opaque
)
1030 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
1031 return slirp_socket_can_recv(4, vmc
->port
);
1034 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
1036 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
1037 slirp_socket_recv(4, vmc
->port
, buf
, size
);
1040 #endif /* CONFIG_SLIRP */
1044 int tap_has_vnet_hdr(void *opaque
)
1049 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1053 #else /* !defined(_WIN32) */
1055 /* Maximum GSO packet size (64k) plus plenty of room for
1056 * the ethernet and virtio_net headers
1058 #define TAP_BUFSIZE (4096 + 65536)
1061 #include <linux/virtio_net.h>
1064 typedef struct TAPState
{
1065 VLANClientState
*vc
;
1067 char down_script
[1024];
1068 char down_script_arg
[128];
1069 uint8_t buf
[TAP_BUFSIZE
];
1070 unsigned int has_vnet_hdr
: 1;
1071 unsigned int using_vnet_hdr
: 1;
1074 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
1076 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
1079 TAPState
*s
= vc
->opaque
;
1083 len
= writev(s
->fd
, iov
, iovcnt
);
1084 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1089 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1091 struct iovec iov
[2];
1095 TAPState
*s
= vc
->opaque
;
1096 struct virtio_net_hdr hdr
= { 0, };
1098 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
1099 iov
[i
].iov_base
= &hdr
;
1100 iov
[i
].iov_len
= sizeof(hdr
);
1105 iov
[i
].iov_base
= (char *) buf
;
1106 iov
[i
].iov_len
= size
;
1109 return tap_receive_iov(vc
, iov
, i
);
1112 static ssize_t
tap_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1114 struct iovec iov
[2];
1118 TAPState
*s
= vc
->opaque
;
1119 struct virtio_net_hdr hdr
= { 0, };
1121 if (s
->has_vnet_hdr
&& s
->using_vnet_hdr
) {
1122 iov
[i
].iov_base
= &hdr
;
1123 iov
[i
].iov_len
= sizeof(hdr
);
1128 iov
[i
].iov_base
= (char *) buf
;
1129 iov
[i
].iov_len
= size
;
1132 return tap_receive_iov(vc
, iov
, i
);
1135 static int tap_can_send(void *opaque
)
1137 TAPState
*s
= opaque
;
1139 return qemu_can_send_packet(s
->vc
);
1143 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1148 sbuf
.maxlen
= maxlen
;
1149 sbuf
.buf
= (char *)buf
;
1151 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1154 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1156 return read(tapfd
, buf
, maxlen
);
1160 static void tap_send(void *opaque
);
1162 static void tap_send_completed(VLANClientState
*vc
)
1164 TAPState
*s
= vc
->opaque
;
1166 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
1169 static void tap_send(void *opaque
)
1171 TAPState
*s
= opaque
;
1175 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
1180 size
= qemu_send_packet_async(s
->vc
, s
->buf
, size
, tap_send_completed
);
1182 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1187 int tap_has_vnet_hdr(void *opaque
)
1189 VLANClientState
*vc
= opaque
;
1190 TAPState
*s
= vc
->opaque
;
1192 return s
? s
->has_vnet_hdr
: 0;
1195 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1197 VLANClientState
*vc
= opaque
;
1198 TAPState
*s
= vc
->opaque
;
1200 if (!s
|| !s
->has_vnet_hdr
)
1203 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
1206 static int tap_probe_vnet_hdr(int fd
)
1208 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1211 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
1212 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
1216 return ifr
.ifr_flags
& IFF_VNET_HDR
;
1222 #ifdef TUNSETOFFLOAD
1223 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
1226 TAPState
*s
= vc
->opaque
;
1227 unsigned int offload
= 0;
1230 offload
|= TUN_F_CSUM
;
1232 offload
|= TUN_F_TSO4
;
1234 offload
|= TUN_F_TSO6
;
1235 if ((tso4
|| tso6
) && ecn
)
1236 offload
|= TUN_F_TSO_ECN
;
1239 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
1240 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
1243 #endif /* TUNSETOFFLOAD */
1245 static void tap_cleanup(VLANClientState
*vc
)
1247 TAPState
*s
= vc
->opaque
;
1249 if (s
->down_script
[0])
1250 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1252 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1259 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1267 s
= qemu_mallocz(sizeof(TAPState
));
1269 s
->has_vnet_hdr
= vnet_hdr
!= 0;
1270 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1271 tap_receive_iov
, tap_cleanup
, s
);
1272 s
->vc
->receive_raw
= tap_receive_raw
;
1273 #ifdef TUNSETOFFLOAD
1274 s
->vc
->set_offload
= tap_set_offload
;
1275 tap_set_offload(s
->vc
, 0, 0, 0, 0);
1277 qemu_set_fd_handler2(s
->fd
, tap_can_send
, tap_send
, NULL
, s
);
1278 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1282 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1283 static int tap_open(char *ifname
, int ifname_size
)
1289 TFR(fd
= open("/dev/tap", O_RDWR
));
1291 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1296 dev
= devname(s
.st_rdev
, S_IFCHR
);
1297 pstrcpy(ifname
, ifname_size
, dev
);
1299 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1302 #elif defined(__sun__)
1303 #define TUNNEWPPA (('T'<<16) | 0x0001)
1305 * Allocate TAP device, returns opened fd.
1306 * Stores dev name in the first arg(must be large enough).
1308 static int tap_alloc(char *dev
, size_t dev_size
)
1310 int tap_fd
, if_fd
, ppa
= -1;
1311 static int ip_fd
= 0;
1314 static int arp_fd
= 0;
1315 int ip_muxid
, arp_muxid
;
1316 struct strioctl strioc_if
, strioc_ppa
;
1317 int link_type
= I_PLINK
;;
1319 char actual_name
[32] = "";
1321 memset(&ifr
, 0x0, sizeof(ifr
));
1325 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1329 /* Check if IP device was opened */
1333 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1335 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1339 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1341 syslog(LOG_ERR
, "Can't open /dev/tap");
1345 /* Assign a new PPA and get its unit number. */
1346 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1347 strioc_ppa
.ic_timout
= 0;
1348 strioc_ppa
.ic_len
= sizeof(ppa
);
1349 strioc_ppa
.ic_dp
= (char *)&ppa
;
1350 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1351 syslog (LOG_ERR
, "Can't assign new interface");
1353 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1355 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1358 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1359 syslog(LOG_ERR
, "Can't push IP module");
1363 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1364 syslog(LOG_ERR
, "Can't get flags\n");
1366 snprintf (actual_name
, 32, "tap%d", ppa
);
1367 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1370 /* Assign ppa according to the unit number returned by tun device */
1372 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1373 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1374 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1375 syslog (LOG_ERR
, "Can't get flags\n");
1376 /* Push arp module to if_fd */
1377 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1378 syslog (LOG_ERR
, "Can't push ARP module (2)");
1380 /* Push arp module to ip_fd */
1381 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1382 syslog (LOG_ERR
, "I_POP failed\n");
1383 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1384 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1386 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1388 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1390 /* Set ifname to arp */
1391 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1392 strioc_if
.ic_timout
= 0;
1393 strioc_if
.ic_len
= sizeof(ifr
);
1394 strioc_if
.ic_dp
= (char *)&ifr
;
1395 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1396 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1399 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1400 syslog(LOG_ERR
, "Can't link TAP device to IP");
1404 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1405 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1409 memset(&ifr
, 0x0, sizeof(ifr
));
1410 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1411 ifr
.lifr_ip_muxid
= ip_muxid
;
1412 ifr
.lifr_arp_muxid
= arp_muxid
;
1414 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1416 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1417 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1418 syslog (LOG_ERR
, "Can't set multiplexor id");
1421 snprintf(dev
, dev_size
, "tap%d", ppa
);
1425 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1429 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1430 fprintf(stderr
, "Cannot allocate TAP device\n");
1433 pstrcpy(ifname
, ifname_size
, dev
);
1434 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1437 #elif defined (_AIX)
1438 static int tap_open(char *ifname
, int ifname_size
)
1440 fprintf (stderr
, "no tap on AIX\n");
1444 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1449 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1451 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1454 memset(&ifr
, 0, sizeof(ifr
));
1455 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1457 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1459 unsigned int features
;
1461 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1462 features
& IFF_VNET_HDR
) {
1464 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1469 if (ifname
[0] != '\0')
1470 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1472 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1473 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1475 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1479 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1480 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1485 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1487 sigset_t oldmask
, mask
;
1493 sigaddset(&mask
, SIGCHLD
);
1494 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1496 /* try to launch network script */
1499 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1501 for (i
= 0; i
< open_max
; i
++) {
1502 if (i
!= STDIN_FILENO
&&
1503 i
!= STDOUT_FILENO
&&
1504 i
!= STDERR_FILENO
&&
1510 *parg
++ = (char *)setup_script
;
1511 *parg
++ = (char *)ifname
;
1513 execv(setup_script
, args
);
1515 } else if (pid
> 0) {
1516 while (waitpid(pid
, &status
, 0) != pid
) {
1519 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1521 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1525 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1529 static int net_tap_init(VLANState
*vlan
, const char *model
,
1530 const char *name
, const char *ifname1
,
1531 const char *setup_script
, const char *down_script
)
1538 if (ifname1
!= NULL
)
1539 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1543 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1547 if (!setup_script
|| !strcmp(setup_script
, "no"))
1549 if (setup_script
[0] != '\0') {
1550 if (launch_script(setup_script
, ifname
, fd
))
1553 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1554 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1555 "ifname=%s,script=%s,downscript=%s",
1556 ifname
, setup_script
, down_script
);
1557 if (down_script
&& strcmp(down_script
, "no")) {
1558 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1559 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1564 #endif /* !_WIN32 */
1566 #if defined(CONFIG_VDE)
1567 typedef struct VDEState
{
1568 VLANClientState
*vc
;
1572 static void vde_to_qemu(void *opaque
)
1574 VDEState
*s
= opaque
;
1578 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1580 qemu_send_packet(s
->vc
, buf
, size
);
1584 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1586 VDEState
*s
= vc
->opaque
;
1590 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1591 } while (ret
< 0 && errno
== EINTR
);
1596 static void vde_cleanup(VLANClientState
*vc
)
1598 VDEState
*s
= vc
->opaque
;
1599 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1604 static int net_vde_init(VLANState
*vlan
, const char *model
,
1605 const char *name
, const char *sock
,
1606 int port
, const char *group
, int mode
)
1609 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1610 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1612 struct vde_open_args args
= {
1614 .group
= init_group
,
1618 s
= qemu_mallocz(sizeof(VDEState
));
1619 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1624 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_receive
,
1625 NULL
, vde_cleanup
, s
);
1626 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1627 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1628 sock
, vde_datafd(s
->vde
));
1633 /* network connection */
1634 typedef struct NetSocketState
{
1635 VLANClientState
*vc
;
1637 int state
; /* 0 = getting length, 1 = getting data */
1639 unsigned int packet_len
;
1641 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1644 typedef struct NetSocketListenState
{
1649 } NetSocketListenState
;
1651 /* XXX: we consider we can send the whole packet without blocking */
1652 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1654 NetSocketState
*s
= vc
->opaque
;
1658 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1659 return send_all(s
->fd
, buf
, size
);
1662 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1664 NetSocketState
*s
= vc
->opaque
;
1666 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1667 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1670 static void net_socket_send(void *opaque
)
1672 NetSocketState
*s
= opaque
;
1678 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1680 err
= socket_error();
1681 if (err
!= EWOULDBLOCK
)
1683 } else if (size
== 0) {
1684 /* end of connection */
1686 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1692 /* reassemble a packet from the network */
1698 memcpy(s
->buf
+ s
->index
, buf
, l
);
1702 if (s
->index
== 4) {
1704 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1710 l
= s
->packet_len
- s
->index
;
1713 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1714 memcpy(s
->buf
+ s
->index
, buf
, l
);
1716 fprintf(stderr
, "serious error: oversized packet received,"
1717 "connection terminated.\n");
1725 if (s
->index
>= s
->packet_len
) {
1726 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1735 static void net_socket_send_dgram(void *opaque
)
1737 NetSocketState
*s
= opaque
;
1740 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1744 /* end of connection */
1745 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1748 qemu_send_packet(s
->vc
, s
->buf
, size
);
1751 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1756 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1757 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1758 inet_ntoa(mcastaddr
->sin_addr
),
1759 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1763 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1765 perror("socket(PF_INET, SOCK_DGRAM)");
1770 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1771 (const char *)&val
, sizeof(val
));
1773 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1777 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1783 /* Add host to multicast group */
1784 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1785 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1787 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1788 (const char *)&imr
, sizeof(struct ip_mreq
));
1790 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1794 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1796 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1797 (const char *)&val
, sizeof(val
));
1799 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1803 socket_set_nonblock(fd
);
1811 static void net_socket_cleanup(VLANClientState
*vc
)
1813 NetSocketState
*s
= vc
->opaque
;
1814 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1819 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1822 int fd
, int is_connected
)
1824 struct sockaddr_in saddr
;
1826 socklen_t saddr_len
;
1829 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1830 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1831 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1835 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1837 if (saddr
.sin_addr
.s_addr
==0) {
1838 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1842 /* clone dgram socket */
1843 newfd
= net_socket_mcast_create(&saddr
);
1845 /* error already reported by net_socket_mcast_create() */
1849 /* clone newfd to fd, close newfd */
1854 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1855 fd
, strerror(errno
));
1860 s
= qemu_mallocz(sizeof(NetSocketState
));
1863 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
1864 NULL
, net_socket_cleanup
, s
);
1865 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1867 /* mcast: save bound address as dst */
1868 if (is_connected
) s
->dgram_dst
=saddr
;
1870 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1871 "socket: fd=%d (%s mcast=%s:%d)",
1872 fd
, is_connected
? "cloned" : "",
1873 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1877 static void net_socket_connect(void *opaque
)
1879 NetSocketState
*s
= opaque
;
1880 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1883 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1886 int fd
, int is_connected
)
1889 s
= qemu_mallocz(sizeof(NetSocketState
));
1891 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
1892 NULL
, net_socket_cleanup
, s
);
1893 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1894 "socket: fd=%d", fd
);
1896 net_socket_connect(s
);
1898 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1903 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1904 const char *model
, const char *name
,
1905 int fd
, int is_connected
)
1907 int so_type
=-1, optlen
=sizeof(so_type
);
1909 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1910 (socklen_t
*)&optlen
)< 0) {
1911 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1916 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1918 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1920 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1921 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1922 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1927 static void net_socket_accept(void *opaque
)
1929 NetSocketListenState
*s
= opaque
;
1931 struct sockaddr_in saddr
;
1936 len
= sizeof(saddr
);
1937 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1938 if (fd
< 0 && errno
!= EINTR
) {
1940 } else if (fd
>= 0) {
1944 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1948 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1949 "socket: connection from %s:%d",
1950 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1954 static int net_socket_listen_init(VLANState
*vlan
,
1957 const char *host_str
)
1959 NetSocketListenState
*s
;
1961 struct sockaddr_in saddr
;
1963 if (parse_host_port(&saddr
, host_str
) < 0)
1966 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1968 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1973 socket_set_nonblock(fd
);
1975 /* allow fast reuse */
1977 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1979 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1984 ret
= listen(fd
, 0);
1990 s
->model
= strdup(model
);
1991 s
->name
= name
? strdup(name
) : NULL
;
1993 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1997 static int net_socket_connect_init(VLANState
*vlan
,
2000 const char *host_str
)
2003 int fd
, connected
, ret
, err
;
2004 struct sockaddr_in saddr
;
2006 if (parse_host_port(&saddr
, host_str
) < 0)
2009 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2014 socket_set_nonblock(fd
);
2018 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2020 err
= socket_error();
2021 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2022 } else if (err
== EINPROGRESS
) {
2025 } else if (err
== WSAEALREADY
) {
2038 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
2041 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2042 "socket: connect to %s:%d",
2043 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2047 static int net_socket_mcast_init(VLANState
*vlan
,
2050 const char *host_str
)
2054 struct sockaddr_in saddr
;
2056 if (parse_host_port(&saddr
, host_str
) < 0)
2060 fd
= net_socket_mcast_create(&saddr
);
2064 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
2068 s
->dgram_dst
= saddr
;
2070 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2071 "socket: mcast=%s:%d",
2072 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2077 typedef struct DumpState
{
2078 VLANClientState
*pcap_vc
;
2083 #define PCAP_MAGIC 0xa1b2c3d4
2085 struct pcap_file_hdr
{
2087 uint16_t version_major
;
2088 uint16_t version_minor
;
2095 struct pcap_sf_pkthdr
{
2104 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
2106 DumpState
*s
= vc
->opaque
;
2107 struct pcap_sf_pkthdr hdr
;
2111 /* Early return in case of previous error. */
2116 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, ticks_per_sec
);
2117 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
2119 hdr
.ts
.tv_sec
= ts
/ 1000000;
2120 hdr
.ts
.tv_usec
= ts
% 1000000;
2121 hdr
.caplen
= caplen
;
2123 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
2124 write(s
->fd
, buf
, caplen
) != caplen
) {
2125 qemu_log("-net dump write error - stop dump\n");
2133 static void net_dump_cleanup(VLANClientState
*vc
)
2135 DumpState
*s
= vc
->opaque
;
2141 static int net_dump_init(Monitor
*mon
, VLANState
*vlan
, const char *device
,
2142 const char *name
, const char *filename
, int len
)
2144 struct pcap_file_hdr hdr
;
2147 s
= qemu_malloc(sizeof(DumpState
));
2149 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
, 0644);
2151 config_error(mon
, "-net dump: can't open %s\n", filename
);
2155 s
->pcap_caplen
= len
;
2157 hdr
.magic
= PCAP_MAGIC
;
2158 hdr
.version_major
= 2;
2159 hdr
.version_minor
= 4;
2162 hdr
.snaplen
= s
->pcap_caplen
;
2165 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2166 config_error(mon
, "-net dump write error: %s\n", strerror(errno
));
2172 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
2173 net_dump_cleanup
, s
);
2174 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2175 "dump to %s (len=%d)", filename
, len
);
2179 /* find or alloc a new VLAN */
2180 VLANState
*qemu_find_vlan(int id
)
2182 VLANState
**pvlan
, *vlan
;
2183 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2187 vlan
= qemu_mallocz(sizeof(VLANState
));
2190 pvlan
= &first_vlan
;
2191 while (*pvlan
!= NULL
)
2192 pvlan
= &(*pvlan
)->next
;
2197 static int nic_get_free_idx(void)
2201 for (index
= 0; index
< MAX_NICS
; index
++)
2202 if (!nd_table
[index
].used
)
2207 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2209 const char *models
[2];
2214 qemu_check_nic_model_list(nd
, models
, model
);
2217 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
2218 const char *default_model
)
2220 int i
, exit_status
= 0;
2223 nd
->model
= strdup(default_model
);
2225 if (strcmp(nd
->model
, "?") != 0) {
2226 for (i
= 0 ; models
[i
]; i
++)
2227 if (strcmp(nd
->model
, models
[i
]) == 0)
2230 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
2234 fprintf(stderr
, "qemu: Supported NIC models: ");
2235 for (i
= 0 ; models
[i
]; i
++)
2236 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2241 int net_client_init(Monitor
*mon
, const char *device
, const char *p
)
2243 static const char * const fd_params
[] = {
2244 "vlan", "name", "fd", NULL
2252 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
2253 vlan_id
= strtol(buf
, NULL
, 0);
2255 vlan
= qemu_find_vlan(vlan_id
);
2257 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
2258 name
= qemu_strdup(buf
);
2260 if (!strcmp(device
, "nic")) {
2261 static const char * const nic_params
[] = {
2262 "vlan", "name", "macaddr", "model", NULL
2266 int idx
= nic_get_free_idx();
2268 if (check_params(buf
, sizeof(buf
), nic_params
, p
) < 0) {
2269 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2273 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2274 config_error(mon
, "Too Many NICs\n");
2278 nd
= &nd_table
[idx
];
2279 macaddr
= nd
->macaddr
;
2285 macaddr
[5] = 0x56 + idx
;
2287 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2288 if (parse_macaddr(macaddr
, buf
) < 0) {
2289 config_error(mon
, "invalid syntax for ethernet address\n");
2294 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2295 nd
->model
= strdup(buf
);
2302 vlan
->nb_guest_devs
++;
2305 if (!strcmp(device
, "none")) {
2307 config_error(mon
, "'none' takes no parameters\n");
2311 /* does nothing. It is needed to signal that no network cards
2316 if (!strcmp(device
, "user")) {
2317 static const char * const slirp_params
[] = {
2318 "vlan", "name", "hostname", "restrict", "ip", NULL
2323 if (check_params(buf
, sizeof(buf
), slirp_params
, p
) < 0) {
2324 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2328 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2329 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
2331 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2332 restricted
= (buf
[0] == 'y') ? 1 : 0;
2334 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2335 ip
= qemu_strdup(buf
);
2337 vlan
->nb_host_devs
++;
2338 ret
= net_slirp_init(vlan
, device
, name
, restricted
, ip
);
2340 } else if (!strcmp(device
, "channel")) {
2342 char name
[20], *devname
;
2343 struct VMChannel
*vmc
;
2345 port
= strtol(p
, &devname
, 10);
2347 if (port
< 1 || port
> 65535) {
2348 config_error(mon
, "vmchannel wrong port number\n");
2352 vmc
= malloc(sizeof(struct VMChannel
));
2353 snprintf(name
, 20, "vmchannel%ld", port
);
2354 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
2356 config_error(mon
, "could not open vmchannel device '%s'\n",
2362 slirp_add_exec(3, vmc
->hd
, 4, port
);
2363 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
2369 if (!strcmp(device
, "tap")) {
2370 static const char * const tap_params
[] = {
2371 "vlan", "name", "ifname", NULL
2375 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
2376 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2380 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2381 config_error(mon
, "tap: no interface name\n");
2385 vlan
->nb_host_devs
++;
2386 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2388 #elif defined (_AIX)
2390 if (!strcmp(device
, "tap")) {
2391 char ifname
[64], chkbuf
[64];
2392 char setup_script
[1024], down_script
[1024];
2394 vlan
->nb_host_devs
++;
2395 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2396 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2397 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2401 fd
= strtol(buf
, NULL
, 0);
2402 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2403 net_tap_fd_init(vlan
, device
, name
, fd
, tap_probe_vnet_hdr(fd
));
2406 static const char * const tap_params
[] = {
2407 "vlan", "name", "ifname", "script", "downscript", NULL
2409 if (check_params(chkbuf
, sizeof(chkbuf
), tap_params
, p
) < 0) {
2410 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2414 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2417 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2418 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2420 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2421 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2423 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2427 if (!strcmp(device
, "socket")) {
2429 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2431 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2432 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2436 fd
= strtol(buf
, NULL
, 0);
2438 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2440 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2441 static const char * const listen_params
[] = {
2442 "vlan", "name", "listen", NULL
2444 if (check_params(chkbuf
, sizeof(chkbuf
), listen_params
, p
) < 0) {
2445 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2449 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2450 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2451 static const char * const connect_params
[] = {
2452 "vlan", "name", "connect", NULL
2454 if (check_params(chkbuf
, sizeof(chkbuf
), connect_params
, p
) < 0) {
2455 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2459 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2460 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2461 static const char * const mcast_params
[] = {
2462 "vlan", "name", "mcast", NULL
2464 if (check_params(chkbuf
, sizeof(chkbuf
), mcast_params
, p
) < 0) {
2465 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2469 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2471 config_error(mon
, "Unknown socket options: %s\n", p
);
2475 vlan
->nb_host_devs
++;
2478 if (!strcmp(device
, "vde")) {
2479 static const char * const vde_params
[] = {
2480 "vlan", "name", "sock", "port", "group", "mode", NULL
2482 char vde_sock
[1024], vde_group
[512];
2483 int vde_port
, vde_mode
;
2485 if (check_params(buf
, sizeof(buf
), vde_params
, p
) < 0) {
2486 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2490 vlan
->nb_host_devs
++;
2491 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2494 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2495 vde_port
= strtol(buf
, NULL
, 10);
2499 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2500 vde_group
[0] = '\0';
2502 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2503 vde_mode
= strtol(buf
, NULL
, 8);
2507 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2510 if (!strcmp(device
, "dump")) {
2513 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2514 len
= strtol(buf
, NULL
, 0);
2516 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2517 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2519 ret
= net_dump_init(mon
, vlan
, device
, name
, buf
, len
);
2521 config_error(mon
, "Unknown network device: %s\n", device
);
2526 config_error(mon
, "Could not initialize device '%s'\n", device
);
2533 void net_client_uninit(NICInfo
*nd
)
2535 nd
->vlan
->nb_guest_devs
--;
2538 free((void *)nd
->model
);
2541 static int net_host_check_device(const char *device
)
2544 const char *valid_param_list
[] = { "tap", "socket", "dump"
2552 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2553 if (!strncmp(valid_param_list
[i
], device
,
2554 strlen(valid_param_list
[i
])))
2561 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2563 if (!net_host_check_device(device
)) {
2564 monitor_printf(mon
, "invalid host network device %s\n", device
);
2567 if (net_client_init(mon
, device
, opts
? opts
: "") < 0) {
2568 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2572 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2575 VLANClientState
*vc
;
2577 vlan
= qemu_find_vlan(vlan_id
);
2579 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2580 if (!strcmp(vc
->name
, device
)) {
2586 monitor_printf(mon
, "can't find device %s\n", device
);
2589 if (!net_host_check_device(vc
->model
)) {
2590 monitor_printf(mon
, "invalid host network device %s\n", device
);
2593 qemu_del_vlan_client(vc
);
2596 int net_client_parse(const char *str
)
2604 while (*p
!= '\0' && *p
!= ',') {
2605 if ((q
- device
) < sizeof(device
) - 1)
2613 return net_client_init(NULL
, device
, p
);
2616 void do_info_network(Monitor
*mon
)
2619 VLANClientState
*vc
;
2621 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2622 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2623 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2624 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2628 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2631 VLANClientState
*vc
= NULL
;
2633 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2634 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2635 if (strcmp(vc
->name
, name
) == 0)
2640 monitor_printf(mon
, "could not find network device '%s'", name
);
2644 if (strcmp(up_or_down
, "up") == 0)
2646 else if (strcmp(up_or_down
, "down") == 0)
2649 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2650 "valid\n", up_or_down
);
2652 if (vc
->link_status_changed
)
2653 vc
->link_status_changed(vc
);
2658 void net_cleanup(void)
2662 /* close network clients */
2663 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2664 VLANClientState
*vc
= vlan
->first_client
;
2667 VLANClientState
*next
= vc
->next
;
2669 qemu_del_vlan_client(vc
);
2676 void net_client_check(void)
2680 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2681 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2683 if (vlan
->nb_guest_devs
== 0)
2684 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2685 if (vlan
->nb_host_devs
== 0)
2687 "Warning: vlan %d is not connected to host network\n",