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_purge_queued_packets(VLANClientState
*vc
)
449 VLANPacket
**pp
= &vc
->vlan
->send_queue
;
451 while (*pp
!= NULL
) {
452 VLANPacket
*packet
= *pp
;
454 if (packet
->sender
== vc
) {
463 void qemu_flush_queued_packets(VLANClientState
*vc
)
467 while ((packet
= vc
->vlan
->send_queue
) != NULL
) {
470 vc
->vlan
->send_queue
= packet
->next
;
472 ret
= qemu_deliver_packet(packet
->sender
, packet
->data
,
473 packet
->size
, packet
->raw
);
474 if (ret
== 0 && packet
->sent_cb
!= NULL
) {
475 packet
->next
= vc
->vlan
->send_queue
;
476 vc
->vlan
->send_queue
= packet
;
481 packet
->sent_cb(packet
->sender
);
487 static void qemu_enqueue_packet(VLANClientState
*sender
,
488 const uint8_t *buf
, int size
, int raw
,
489 NetPacketSent
*sent_cb
)
493 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
494 packet
->next
= sender
->vlan
->send_queue
;
495 packet
->sender
= sender
;
498 packet
->sent_cb
= sent_cb
;
499 memcpy(packet
->data
, buf
, size
);
500 sender
->vlan
->send_queue
= packet
;
503 static ssize_t
qemu_send_packet_async2(VLANClientState
*sender
,
504 const uint8_t *buf
, int size
, int raw
,
505 NetPacketSent
*sent_cb
)
509 if (sender
->link_down
) {
514 printf("vlan %d send:\n", sender
->vlan
->id
);
515 hex_dump(stdout
, buf
, size
);
518 if (sender
->vlan
->delivering
) {
519 qemu_enqueue_packet(sender
, buf
, size
, raw
, NULL
);
523 ret
= qemu_deliver_packet(sender
, buf
, size
, raw
);
524 if (ret
== 0 && sent_cb
!= NULL
) {
525 qemu_enqueue_packet(sender
, buf
, size
, raw
, sent_cb
);
529 qemu_flush_queued_packets(sender
);
534 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
535 const uint8_t *buf
, int size
,
536 NetPacketSent
*sent_cb
)
538 return qemu_send_packet_async2(sender
, buf
, size
, 0, sent_cb
);
541 ssize_t
qemu_send_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
543 return qemu_send_packet_async2(sender
, buf
, size
, 0, NULL
);
546 ssize_t
qemu_send_packet_raw(VLANClientState
*sender
, const uint8_t *buf
, int size
)
548 return qemu_send_packet_async2(sender
, buf
, size
, 1, NULL
);
551 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
554 uint8_t buffer
[4096];
558 for (i
= 0; i
< iovcnt
; i
++) {
561 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
562 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
566 return vc
->receive(vc
, buffer
, offset
);
569 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
574 for (i
= 0; i
< iovcnt
; i
++)
575 offset
+= iov
[i
].iov_len
;
579 static int qemu_deliver_packet_iov(VLANClientState
*sender
,
580 const struct iovec
*iov
, int iovcnt
)
585 sender
->vlan
->delivering
= 1;
587 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
595 ret
= calc_iov_length(iov
, iovcnt
);
599 if (vc
->receive_iov
) {
600 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
602 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
605 ret
= (ret
>= 0) ? ret
: len
;
608 sender
->vlan
->delivering
= 0;
613 static ssize_t
qemu_enqueue_packet_iov(VLANClientState
*sender
,
614 const struct iovec
*iov
, int iovcnt
,
615 NetPacketSent
*sent_cb
)
621 max_len
= calc_iov_length(iov
, iovcnt
);
623 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
624 packet
->next
= sender
->vlan
->send_queue
;
625 packet
->sender
= sender
;
626 packet
->sent_cb
= sent_cb
;
630 for (i
= 0; i
< iovcnt
; i
++) {
631 size_t len
= iov
[i
].iov_len
;
633 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
637 sender
->vlan
->send_queue
= packet
;
642 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
643 const struct iovec
*iov
, int iovcnt
,
644 NetPacketSent
*sent_cb
)
648 if (sender
->link_down
) {
649 return calc_iov_length(iov
, iovcnt
);
652 if (sender
->vlan
->delivering
) {
653 return qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, NULL
);
656 ret
= qemu_deliver_packet_iov(sender
, iov
, iovcnt
);
657 if (ret
== 0 && sent_cb
!= NULL
) {
658 qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, sent_cb
);
662 qemu_flush_queued_packets(sender
);
668 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
670 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
673 static void config_error(Monitor
*mon
, const char *fmt
, ...)
679 monitor_vprintf(mon
, fmt
, ap
);
681 fprintf(stderr
, "qemu: ");
682 vfprintf(stderr
, fmt
, ap
);
688 #if defined(CONFIG_SLIRP)
690 /* slirp network adapter */
692 struct slirp_config_str
{
693 struct slirp_config_str
*next
;
697 static int slirp_inited
;
698 static struct slirp_config_str
*slirp_redirs
;
700 static const char *slirp_smb_export
;
702 static VLANClientState
*slirp_vc
;
705 static void slirp_smb(const char *exported_dir
);
707 static void slirp_redirection(Monitor
*mon
, const char *redir_str
);
709 int slirp_can_output(void)
711 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
714 void slirp_output(const uint8_t *pkt
, int pkt_len
)
717 printf("slirp output:\n");
718 hex_dump(stdout
, pkt
, pkt_len
);
722 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
725 int slirp_is_inited(void)
730 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
733 printf("slirp input:\n");
734 hex_dump(stdout
, buf
, size
);
736 slirp_input(buf
, size
);
740 static int slirp_in_use
;
742 static void net_slirp_cleanup(VLANClientState
*vc
)
747 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
,
748 int restricted
, const char *ip
)
751 /* slirp only supports a single instance so far */
756 slirp_init(restricted
, ip
);
758 while (slirp_redirs
) {
759 struct slirp_config_str
*config
= slirp_redirs
;
761 slirp_redirection(NULL
, config
->str
);
762 slirp_redirs
= config
->next
;
766 if (slirp_smb_export
) {
767 slirp_smb(slirp_smb_export
);
772 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
,
773 NULL
, net_slirp_cleanup
, NULL
);
774 slirp_vc
->info_str
[0] = '\0';
779 static void net_slirp_redir_print(void *opaque
, int is_udp
,
780 struct in_addr
*laddr
, u_int lport
,
781 struct in_addr
*faddr
, u_int fport
)
783 Monitor
*mon
= (Monitor
*)opaque
;
788 h_addr
= ntohl(faddr
->s_addr
);
789 g_addr
= ntohl(laddr
->s_addr
);
791 monitor_printf(mon
, " %s |", is_udp
? "udp" : "tcp" );
792 snprintf(buf
, 15, "%d.%d.%d.%d", (h_addr
>> 24) & 0xff,
793 (h_addr
>> 16) & 0xff,
794 (h_addr
>> 8) & 0xff,
796 monitor_printf(mon
, " %15s |", buf
);
797 monitor_printf(mon
, " %5d |", fport
);
799 snprintf(buf
, 15, "%d.%d.%d.%d", (g_addr
>> 24) & 0xff,
800 (g_addr
>> 16) & 0xff,
801 (g_addr
>> 8) & 0xff,
803 monitor_printf(mon
, " %15s |", buf
);
804 monitor_printf(mon
, " %5d\n", lport
);
808 static void net_slirp_redir_list(Monitor
*mon
)
813 monitor_printf(mon
, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
814 monitor_printf(mon
, " | | | | \n");
815 slirp_redir_loop(net_slirp_redir_print
, mon
);
818 static void net_slirp_redir_rm(Monitor
*mon
, const char *port_str
)
822 const char *p
= port_str
;
829 if (!port_str
|| !port_str
[0])
832 get_str_sep(buf
, sizeof(buf
), &p
, ':');
834 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
836 } else if (!strcmp(buf
, "udp")) {
844 n
= slirp_redir_rm(is_udp
, host_port
);
846 monitor_printf(mon
, "removed %d redirections to %s port %d\n", n
,
847 is_udp
? "udp" : "tcp", host_port
);
851 monitor_printf(mon
, "invalid format\n");
854 static void slirp_redirection(Monitor
*mon
, const char *redir_str
)
856 struct in_addr guest_addr
;
857 int host_port
, guest_port
;
863 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
866 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
868 } else if (!strcmp(buf
, "udp")) {
874 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
877 host_port
= strtol(buf
, &r
, 0);
882 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
885 if (buf
[0] == '\0') {
886 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
888 if (!inet_aton(buf
, &guest_addr
)) {
892 guest_port
= strtol(p
, &r
, 0);
897 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
898 config_error(mon
, "could not set up redirection '%s'\n", redir_str
);
903 config_error(mon
, "invalid redirection format '%s'\n", redir_str
);
906 void net_slirp_redir(Monitor
*mon
, const char *redir_str
, const char *redir_opt2
)
908 struct slirp_config_str
*config
;
912 monitor_printf(mon
, "user mode network stack not in use\n");
914 config
= qemu_malloc(sizeof(*config
));
915 config
->str
= redir_str
;
916 config
->next
= slirp_redirs
;
917 slirp_redirs
= config
;
922 if (!strcmp(redir_str
, "remove")) {
923 net_slirp_redir_rm(mon
, redir_opt2
);
927 if (!strcmp(redir_str
, "list")) {
928 net_slirp_redir_list(mon
);
932 slirp_redirection(mon
, redir_str
);
937 static char smb_dir
[1024];
939 static void erase_dir(char *dir_name
)
945 /* erase all the files in the directory */
946 if ((d
= opendir(dir_name
)) != NULL
) {
951 if (strcmp(de
->d_name
, ".") != 0 &&
952 strcmp(de
->d_name
, "..") != 0) {
953 snprintf(filename
, sizeof(filename
), "%s/%s",
954 smb_dir
, de
->d_name
);
955 if (unlink(filename
) != 0) /* is it a directory? */
964 /* automatic user mode samba server configuration */
965 static void smb_exit(void)
970 static void slirp_smb(const char *exported_dir
)
973 char smb_cmdline
[1024];
976 /* XXX: better tmp dir construction */
977 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%ld", (long)getpid());
978 if (mkdir(smb_dir
, 0700) < 0) {
979 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
982 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
984 f
= fopen(smb_conf
, "w");
986 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
993 "socket address=127.0.0.1\n"
995 "lock directory=%s\n"
996 "log file=%s/log.smbd\n"
997 "smb passwd file=%s/smbpasswd\n"
1013 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1014 SMBD_COMMAND
, smb_conf
);
1016 slirp_add_exec(0, smb_cmdline
, 4, 139);
1019 /* automatic user mode samba server configuration */
1020 void net_slirp_smb(const char *exported_dir
)
1022 if (slirp_smb_export
) {
1023 fprintf(stderr
, "-smb given twice\n");
1026 slirp_smb_export
= exported_dir
;
1028 slirp_smb(exported_dir
);
1032 #endif /* !defined(_WIN32) */
1034 void do_info_slirp(Monitor
*mon
)
1040 CharDriverState
*hd
;
1044 static int vmchannel_can_read(void *opaque
)
1046 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
1047 return slirp_socket_can_recv(4, vmc
->port
);
1050 static void vmchannel_read(void *opaque
, const uint8_t *buf
, int size
)
1052 struct VMChannel
*vmc
= (struct VMChannel
*)opaque
;
1053 slirp_socket_recv(4, vmc
->port
, buf
, size
);
1056 #endif /* CONFIG_SLIRP */
1060 int tap_has_vnet_hdr(void *opaque
)
1065 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1069 #else /* !defined(_WIN32) */
1071 /* Maximum GSO packet size (64k) plus plenty of room for
1072 * the ethernet and virtio_net headers
1074 #define TAP_BUFSIZE (4096 + 65536)
1077 #include <linux/virtio_net.h>
1080 typedef struct TAPState
{
1081 VLANClientState
*vc
;
1083 char down_script
[1024];
1084 char down_script_arg
[128];
1085 uint8_t buf
[TAP_BUFSIZE
];
1086 unsigned int read_poll
: 1;
1087 unsigned int write_poll
: 1;
1088 unsigned int has_vnet_hdr
: 1;
1089 unsigned int using_vnet_hdr
: 1;
1092 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
1094 static int tap_can_send(void *opaque
);
1095 static void tap_send(void *opaque
);
1096 static void tap_writable(void *opaque
);
1098 static void tap_update_fd_handler(TAPState
*s
)
1100 qemu_set_fd_handler2(s
->fd
,
1101 s
->read_poll
? tap_can_send
: NULL
,
1102 s
->read_poll
? tap_send
: NULL
,
1103 s
->write_poll
? tap_writable
: NULL
,
1107 static void tap_read_poll(TAPState
*s
, int enable
)
1109 s
->read_poll
= !!enable
;
1110 tap_update_fd_handler(s
);
1113 static void tap_write_poll(TAPState
*s
, int enable
)
1115 s
->write_poll
= !!enable
;
1116 tap_update_fd_handler(s
);
1119 static void tap_writable(void *opaque
)
1121 TAPState
*s
= opaque
;
1123 tap_write_poll(s
, 0);
1125 qemu_flush_queued_packets(s
->vc
);
1128 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
1131 TAPState
*s
= vc
->opaque
;
1135 len
= writev(s
->fd
, iov
, iovcnt
);
1136 } while (len
== -1 && errno
== EINTR
);
1138 if (len
== -1 && errno
== EAGAIN
) {
1139 tap_write_poll(s
, 1);
1146 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1148 struct iovec iov
[2];
1152 TAPState
*s
= vc
->opaque
;
1153 struct virtio_net_hdr hdr
= { 0, };
1155 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
1156 iov
[i
].iov_base
= &hdr
;
1157 iov
[i
].iov_len
= sizeof(hdr
);
1162 iov
[i
].iov_base
= (char *) buf
;
1163 iov
[i
].iov_len
= size
;
1166 return tap_receive_iov(vc
, iov
, i
);
1169 static ssize_t
tap_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1171 struct iovec iov
[2];
1175 TAPState
*s
= vc
->opaque
;
1176 struct virtio_net_hdr hdr
= { 0, };
1178 if (s
->has_vnet_hdr
&& s
->using_vnet_hdr
) {
1179 iov
[i
].iov_base
= &hdr
;
1180 iov
[i
].iov_len
= sizeof(hdr
);
1185 iov
[i
].iov_base
= (char *) buf
;
1186 iov
[i
].iov_len
= size
;
1189 return tap_receive_iov(vc
, iov
, i
);
1192 static int tap_can_send(void *opaque
)
1194 TAPState
*s
= opaque
;
1196 return qemu_can_send_packet(s
->vc
);
1200 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1205 sbuf
.maxlen
= maxlen
;
1206 sbuf
.buf
= (char *)buf
;
1208 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1211 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1213 return read(tapfd
, buf
, maxlen
);
1217 static void tap_send_completed(VLANClientState
*vc
)
1219 TAPState
*s
= vc
->opaque
;
1220 tap_read_poll(s
, 1);
1223 static void tap_send(void *opaque
)
1225 TAPState
*s
= opaque
;
1229 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
1234 size
= qemu_send_packet_async(s
->vc
, s
->buf
, size
, tap_send_completed
);
1236 tap_read_poll(s
, 0);
1241 int tap_has_vnet_hdr(void *opaque
)
1243 VLANClientState
*vc
= opaque
;
1244 TAPState
*s
= vc
->opaque
;
1246 return s
? s
->has_vnet_hdr
: 0;
1249 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1251 VLANClientState
*vc
= opaque
;
1252 TAPState
*s
= vc
->opaque
;
1254 if (!s
|| !s
->has_vnet_hdr
)
1257 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
1260 static int tap_probe_vnet_hdr(int fd
)
1262 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1265 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
1266 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
1270 return ifr
.ifr_flags
& IFF_VNET_HDR
;
1276 #ifdef TUNSETOFFLOAD
1277 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
1280 TAPState
*s
= vc
->opaque
;
1281 unsigned int offload
= 0;
1284 offload
|= TUN_F_CSUM
;
1286 offload
|= TUN_F_TSO4
;
1288 offload
|= TUN_F_TSO6
;
1289 if ((tso4
|| tso6
) && ecn
)
1290 offload
|= TUN_F_TSO_ECN
;
1293 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
1294 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
1297 #endif /* TUNSETOFFLOAD */
1299 static void tap_cleanup(VLANClientState
*vc
)
1301 TAPState
*s
= vc
->opaque
;
1303 qemu_purge_queued_packets(vc
);
1305 if (s
->down_script
[0])
1306 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1308 tap_read_poll(s
, 0);
1309 tap_write_poll(s
, 0);
1316 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1324 s
= qemu_mallocz(sizeof(TAPState
));
1326 s
->has_vnet_hdr
= vnet_hdr
!= 0;
1327 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1328 tap_receive_iov
, tap_cleanup
, s
);
1329 s
->vc
->receive_raw
= tap_receive_raw
;
1330 #ifdef TUNSETOFFLOAD
1331 s
->vc
->set_offload
= tap_set_offload
;
1332 tap_set_offload(s
->vc
, 0, 0, 0, 0);
1334 tap_read_poll(s
, 1);
1335 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1339 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1340 static int tap_open(char *ifname
, int ifname_size
)
1346 TFR(fd
= open("/dev/tap", O_RDWR
));
1348 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1353 dev
= devname(s
.st_rdev
, S_IFCHR
);
1354 pstrcpy(ifname
, ifname_size
, dev
);
1356 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1359 #elif defined(__sun__)
1360 #define TUNNEWPPA (('T'<<16) | 0x0001)
1362 * Allocate TAP device, returns opened fd.
1363 * Stores dev name in the first arg(must be large enough).
1365 static int tap_alloc(char *dev
, size_t dev_size
)
1367 int tap_fd
, if_fd
, ppa
= -1;
1368 static int ip_fd
= 0;
1371 static int arp_fd
= 0;
1372 int ip_muxid
, arp_muxid
;
1373 struct strioctl strioc_if
, strioc_ppa
;
1374 int link_type
= I_PLINK
;;
1376 char actual_name
[32] = "";
1378 memset(&ifr
, 0x0, sizeof(ifr
));
1382 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1386 /* Check if IP device was opened */
1390 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1392 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1396 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1398 syslog(LOG_ERR
, "Can't open /dev/tap");
1402 /* Assign a new PPA and get its unit number. */
1403 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1404 strioc_ppa
.ic_timout
= 0;
1405 strioc_ppa
.ic_len
= sizeof(ppa
);
1406 strioc_ppa
.ic_dp
= (char *)&ppa
;
1407 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1408 syslog (LOG_ERR
, "Can't assign new interface");
1410 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1412 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1415 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1416 syslog(LOG_ERR
, "Can't push IP module");
1420 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1421 syslog(LOG_ERR
, "Can't get flags\n");
1423 snprintf (actual_name
, 32, "tap%d", ppa
);
1424 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1427 /* Assign ppa according to the unit number returned by tun device */
1429 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1430 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1431 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1432 syslog (LOG_ERR
, "Can't get flags\n");
1433 /* Push arp module to if_fd */
1434 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1435 syslog (LOG_ERR
, "Can't push ARP module (2)");
1437 /* Push arp module to ip_fd */
1438 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1439 syslog (LOG_ERR
, "I_POP failed\n");
1440 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1441 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1443 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1445 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1447 /* Set ifname to arp */
1448 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1449 strioc_if
.ic_timout
= 0;
1450 strioc_if
.ic_len
= sizeof(ifr
);
1451 strioc_if
.ic_dp
= (char *)&ifr
;
1452 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1453 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1456 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1457 syslog(LOG_ERR
, "Can't link TAP device to IP");
1461 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1462 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1466 memset(&ifr
, 0x0, sizeof(ifr
));
1467 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1468 ifr
.lifr_ip_muxid
= ip_muxid
;
1469 ifr
.lifr_arp_muxid
= arp_muxid
;
1471 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1473 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1474 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1475 syslog (LOG_ERR
, "Can't set multiplexor id");
1478 snprintf(dev
, dev_size
, "tap%d", ppa
);
1482 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1486 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1487 fprintf(stderr
, "Cannot allocate TAP device\n");
1490 pstrcpy(ifname
, ifname_size
, dev
);
1491 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1494 #elif defined (_AIX)
1495 static int tap_open(char *ifname
, int ifname_size
)
1497 fprintf (stderr
, "no tap on AIX\n");
1501 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1506 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1508 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1511 memset(&ifr
, 0, sizeof(ifr
));
1512 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1514 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1516 unsigned int features
;
1518 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1519 features
& IFF_VNET_HDR
) {
1521 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1526 if (ifname
[0] != '\0')
1527 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1529 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1530 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1532 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1536 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1537 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1542 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1544 sigset_t oldmask
, mask
;
1550 sigaddset(&mask
, SIGCHLD
);
1551 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1553 /* try to launch network script */
1556 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1558 for (i
= 0; i
< open_max
; i
++) {
1559 if (i
!= STDIN_FILENO
&&
1560 i
!= STDOUT_FILENO
&&
1561 i
!= STDERR_FILENO
&&
1567 *parg
++ = (char *)setup_script
;
1568 *parg
++ = (char *)ifname
;
1570 execv(setup_script
, args
);
1572 } else if (pid
> 0) {
1573 while (waitpid(pid
, &status
, 0) != pid
) {
1576 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1578 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1582 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1586 static int net_tap_init(VLANState
*vlan
, const char *model
,
1587 const char *name
, const char *ifname1
,
1588 const char *setup_script
, const char *down_script
)
1595 if (ifname1
!= NULL
)
1596 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1600 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1604 if (!setup_script
|| !strcmp(setup_script
, "no"))
1606 if (setup_script
[0] != '\0') {
1607 if (launch_script(setup_script
, ifname
, fd
))
1610 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1611 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1612 "ifname=%s,script=%s,downscript=%s",
1613 ifname
, setup_script
, down_script
);
1614 if (down_script
&& strcmp(down_script
, "no")) {
1615 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1616 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1621 #endif /* !_WIN32 */
1623 #if defined(CONFIG_VDE)
1624 typedef struct VDEState
{
1625 VLANClientState
*vc
;
1629 static void vde_to_qemu(void *opaque
)
1631 VDEState
*s
= opaque
;
1635 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1637 qemu_send_packet(s
->vc
, buf
, size
);
1641 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1643 VDEState
*s
= vc
->opaque
;
1647 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1648 } while (ret
< 0 && errno
== EINTR
);
1653 static void vde_cleanup(VLANClientState
*vc
)
1655 VDEState
*s
= vc
->opaque
;
1656 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1661 static int net_vde_init(VLANState
*vlan
, const char *model
,
1662 const char *name
, const char *sock
,
1663 int port
, const char *group
, int mode
)
1666 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1667 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1669 struct vde_open_args args
= {
1671 .group
= init_group
,
1675 s
= qemu_mallocz(sizeof(VDEState
));
1676 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1681 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_receive
,
1682 NULL
, vde_cleanup
, s
);
1683 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1684 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1685 sock
, vde_datafd(s
->vde
));
1690 /* network connection */
1691 typedef struct NetSocketState
{
1692 VLANClientState
*vc
;
1694 int state
; /* 0 = getting length, 1 = getting data */
1696 unsigned int packet_len
;
1698 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1701 typedef struct NetSocketListenState
{
1706 } NetSocketListenState
;
1708 /* XXX: we consider we can send the whole packet without blocking */
1709 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1711 NetSocketState
*s
= vc
->opaque
;
1715 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1716 return send_all(s
->fd
, buf
, size
);
1719 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1721 NetSocketState
*s
= vc
->opaque
;
1723 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1724 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1727 static void net_socket_send(void *opaque
)
1729 NetSocketState
*s
= opaque
;
1735 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1737 err
= socket_error();
1738 if (err
!= EWOULDBLOCK
)
1740 } else if (size
== 0) {
1741 /* end of connection */
1743 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1749 /* reassemble a packet from the network */
1755 memcpy(s
->buf
+ s
->index
, buf
, l
);
1759 if (s
->index
== 4) {
1761 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1767 l
= s
->packet_len
- s
->index
;
1770 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1771 memcpy(s
->buf
+ s
->index
, buf
, l
);
1773 fprintf(stderr
, "serious error: oversized packet received,"
1774 "connection terminated.\n");
1782 if (s
->index
>= s
->packet_len
) {
1783 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1792 static void net_socket_send_dgram(void *opaque
)
1794 NetSocketState
*s
= opaque
;
1797 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1801 /* end of connection */
1802 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1805 qemu_send_packet(s
->vc
, s
->buf
, size
);
1808 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1813 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1814 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1815 inet_ntoa(mcastaddr
->sin_addr
),
1816 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1820 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1822 perror("socket(PF_INET, SOCK_DGRAM)");
1827 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1828 (const char *)&val
, sizeof(val
));
1830 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1834 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1840 /* Add host to multicast group */
1841 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1842 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1844 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1845 (const char *)&imr
, sizeof(struct ip_mreq
));
1847 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1851 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1853 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1854 (const char *)&val
, sizeof(val
));
1856 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1860 socket_set_nonblock(fd
);
1868 static void net_socket_cleanup(VLANClientState
*vc
)
1870 NetSocketState
*s
= vc
->opaque
;
1871 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1876 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1879 int fd
, int is_connected
)
1881 struct sockaddr_in saddr
;
1883 socklen_t saddr_len
;
1886 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1887 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1888 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1892 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1894 if (saddr
.sin_addr
.s_addr
==0) {
1895 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1899 /* clone dgram socket */
1900 newfd
= net_socket_mcast_create(&saddr
);
1902 /* error already reported by net_socket_mcast_create() */
1906 /* clone newfd to fd, close newfd */
1911 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1912 fd
, strerror(errno
));
1917 s
= qemu_mallocz(sizeof(NetSocketState
));
1920 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
1921 NULL
, net_socket_cleanup
, s
);
1922 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1924 /* mcast: save bound address as dst */
1925 if (is_connected
) s
->dgram_dst
=saddr
;
1927 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1928 "socket: fd=%d (%s mcast=%s:%d)",
1929 fd
, is_connected
? "cloned" : "",
1930 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1934 static void net_socket_connect(void *opaque
)
1936 NetSocketState
*s
= opaque
;
1937 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1940 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1943 int fd
, int is_connected
)
1946 s
= qemu_mallocz(sizeof(NetSocketState
));
1948 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
1949 NULL
, net_socket_cleanup
, s
);
1950 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1951 "socket: fd=%d", fd
);
1953 net_socket_connect(s
);
1955 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1960 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1961 const char *model
, const char *name
,
1962 int fd
, int is_connected
)
1964 int so_type
=-1, optlen
=sizeof(so_type
);
1966 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1967 (socklen_t
*)&optlen
)< 0) {
1968 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1973 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1975 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1977 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1978 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1979 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1984 static void net_socket_accept(void *opaque
)
1986 NetSocketListenState
*s
= opaque
;
1988 struct sockaddr_in saddr
;
1993 len
= sizeof(saddr
);
1994 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1995 if (fd
< 0 && errno
!= EINTR
) {
1997 } else if (fd
>= 0) {
2001 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
2005 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2006 "socket: connection from %s:%d",
2007 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2011 static int net_socket_listen_init(VLANState
*vlan
,
2014 const char *host_str
)
2016 NetSocketListenState
*s
;
2018 struct sockaddr_in saddr
;
2020 if (parse_host_port(&saddr
, host_str
) < 0)
2023 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2025 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2030 socket_set_nonblock(fd
);
2032 /* allow fast reuse */
2034 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2036 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2041 ret
= listen(fd
, 0);
2047 s
->model
= strdup(model
);
2048 s
->name
= name
? strdup(name
) : NULL
;
2050 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2054 static int net_socket_connect_init(VLANState
*vlan
,
2057 const char *host_str
)
2060 int fd
, connected
, ret
, err
;
2061 struct sockaddr_in saddr
;
2063 if (parse_host_port(&saddr
, host_str
) < 0)
2066 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2071 socket_set_nonblock(fd
);
2075 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2077 err
= socket_error();
2078 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2079 } else if (err
== EINPROGRESS
) {
2082 } else if (err
== WSAEALREADY
) {
2095 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
2098 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2099 "socket: connect to %s:%d",
2100 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2104 static int net_socket_mcast_init(VLANState
*vlan
,
2107 const char *host_str
)
2111 struct sockaddr_in saddr
;
2113 if (parse_host_port(&saddr
, host_str
) < 0)
2117 fd
= net_socket_mcast_create(&saddr
);
2121 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
2125 s
->dgram_dst
= saddr
;
2127 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2128 "socket: mcast=%s:%d",
2129 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2134 typedef struct DumpState
{
2135 VLANClientState
*pcap_vc
;
2140 #define PCAP_MAGIC 0xa1b2c3d4
2142 struct pcap_file_hdr
{
2144 uint16_t version_major
;
2145 uint16_t version_minor
;
2152 struct pcap_sf_pkthdr
{
2161 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
2163 DumpState
*s
= vc
->opaque
;
2164 struct pcap_sf_pkthdr hdr
;
2168 /* Early return in case of previous error. */
2173 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, ticks_per_sec
);
2174 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
2176 hdr
.ts
.tv_sec
= ts
/ 1000000;
2177 hdr
.ts
.tv_usec
= ts
% 1000000;
2178 hdr
.caplen
= caplen
;
2180 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
2181 write(s
->fd
, buf
, caplen
) != caplen
) {
2182 qemu_log("-net dump write error - stop dump\n");
2190 static void net_dump_cleanup(VLANClientState
*vc
)
2192 DumpState
*s
= vc
->opaque
;
2198 static int net_dump_init(Monitor
*mon
, VLANState
*vlan
, const char *device
,
2199 const char *name
, const char *filename
, int len
)
2201 struct pcap_file_hdr hdr
;
2204 s
= qemu_malloc(sizeof(DumpState
));
2206 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
2208 config_error(mon
, "-net dump: can't open %s\n", filename
);
2212 s
->pcap_caplen
= len
;
2214 hdr
.magic
= PCAP_MAGIC
;
2215 hdr
.version_major
= 2;
2216 hdr
.version_minor
= 4;
2219 hdr
.snaplen
= s
->pcap_caplen
;
2222 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2223 config_error(mon
, "-net dump write error: %s\n", strerror(errno
));
2229 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
2230 net_dump_cleanup
, s
);
2231 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2232 "dump to %s (len=%d)", filename
, len
);
2236 /* find or alloc a new VLAN */
2237 VLANState
*qemu_find_vlan(int id
)
2239 VLANState
**pvlan
, *vlan
;
2240 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2244 vlan
= qemu_mallocz(sizeof(VLANState
));
2247 pvlan
= &first_vlan
;
2248 while (*pvlan
!= NULL
)
2249 pvlan
= &(*pvlan
)->next
;
2254 static int nic_get_free_idx(void)
2258 for (index
= 0; index
< MAX_NICS
; index
++)
2259 if (!nd_table
[index
].used
)
2264 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2266 const char *models
[2];
2271 qemu_check_nic_model_list(nd
, models
, model
);
2274 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
2275 const char *default_model
)
2277 int i
, exit_status
= 0;
2280 nd
->model
= strdup(default_model
);
2282 if (strcmp(nd
->model
, "?") != 0) {
2283 for (i
= 0 ; models
[i
]; i
++)
2284 if (strcmp(nd
->model
, models
[i
]) == 0)
2287 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
2291 fprintf(stderr
, "qemu: Supported NIC models: ");
2292 for (i
= 0 ; models
[i
]; i
++)
2293 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2298 int net_client_init(Monitor
*mon
, const char *device
, const char *p
)
2300 static const char * const fd_params
[] = {
2301 "vlan", "name", "fd", NULL
2309 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
2310 vlan_id
= strtol(buf
, NULL
, 0);
2312 vlan
= qemu_find_vlan(vlan_id
);
2314 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
2315 name
= qemu_strdup(buf
);
2317 if (!strcmp(device
, "nic")) {
2318 static const char * const nic_params
[] = {
2319 "vlan", "name", "macaddr", "model", "addr", NULL
2323 int idx
= nic_get_free_idx();
2325 if (check_params(buf
, sizeof(buf
), nic_params
, p
) < 0) {
2326 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2330 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2331 config_error(mon
, "Too Many NICs\n");
2335 nd
= &nd_table
[idx
];
2336 macaddr
= nd
->macaddr
;
2342 macaddr
[5] = 0x56 + idx
;
2344 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2345 if (parse_macaddr(macaddr
, buf
) < 0) {
2346 config_error(mon
, "invalid syntax for ethernet address\n");
2351 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2352 nd
->model
= strdup(buf
);
2354 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
2355 nd
->devaddr
= strdup(buf
);
2362 vlan
->nb_guest_devs
++;
2365 if (!strcmp(device
, "none")) {
2367 config_error(mon
, "'none' takes no parameters\n");
2371 /* does nothing. It is needed to signal that no network cards
2376 if (!strcmp(device
, "user")) {
2377 static const char * const slirp_params
[] = {
2378 "vlan", "name", "hostname", "restrict", "ip", NULL
2383 if (check_params(buf
, sizeof(buf
), slirp_params
, p
) < 0) {
2384 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2388 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2389 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
2391 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2392 restricted
= (buf
[0] == 'y') ? 1 : 0;
2394 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2395 ip
= qemu_strdup(buf
);
2397 vlan
->nb_host_devs
++;
2398 ret
= net_slirp_init(vlan
, device
, name
, restricted
, ip
);
2400 } else if (!strcmp(device
, "channel")) {
2402 char name
[20], *devname
;
2403 struct VMChannel
*vmc
;
2405 port
= strtol(p
, &devname
, 10);
2407 if (port
< 1 || port
> 65535) {
2408 config_error(mon
, "vmchannel wrong port number\n");
2412 vmc
= malloc(sizeof(struct VMChannel
));
2413 snprintf(name
, 20, "vmchannel%ld", port
);
2414 vmc
->hd
= qemu_chr_open(name
, devname
, NULL
);
2416 config_error(mon
, "could not open vmchannel device '%s'\n",
2422 slirp_add_exec(3, vmc
->hd
, 4, port
);
2423 qemu_chr_add_handlers(vmc
->hd
, vmchannel_can_read
, vmchannel_read
,
2429 if (!strcmp(device
, "tap")) {
2430 static const char * const tap_params
[] = {
2431 "vlan", "name", "ifname", NULL
2435 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
2436 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2440 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2441 config_error(mon
, "tap: no interface name\n");
2445 vlan
->nb_host_devs
++;
2446 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2448 #elif defined (_AIX)
2450 if (!strcmp(device
, "tap")) {
2451 char ifname
[64], chkbuf
[64];
2452 char setup_script
[1024], down_script
[1024];
2454 vlan
->nb_host_devs
++;
2455 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2456 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2457 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2461 fd
= strtol(buf
, NULL
, 0);
2462 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2463 net_tap_fd_init(vlan
, device
, name
, fd
, tap_probe_vnet_hdr(fd
));
2466 static const char * const tap_params
[] = {
2467 "vlan", "name", "ifname", "script", "downscript", NULL
2469 if (check_params(chkbuf
, sizeof(chkbuf
), tap_params
, p
) < 0) {
2470 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2474 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2477 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2478 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2480 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2481 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2483 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2487 if (!strcmp(device
, "socket")) {
2489 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2491 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2492 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2496 fd
= strtol(buf
, NULL
, 0);
2498 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2500 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2501 static const char * const listen_params
[] = {
2502 "vlan", "name", "listen", NULL
2504 if (check_params(chkbuf
, sizeof(chkbuf
), listen_params
, p
) < 0) {
2505 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2509 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2510 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2511 static const char * const connect_params
[] = {
2512 "vlan", "name", "connect", NULL
2514 if (check_params(chkbuf
, sizeof(chkbuf
), connect_params
, p
) < 0) {
2515 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2519 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2520 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2521 static const char * const mcast_params
[] = {
2522 "vlan", "name", "mcast", NULL
2524 if (check_params(chkbuf
, sizeof(chkbuf
), mcast_params
, p
) < 0) {
2525 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2529 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2531 config_error(mon
, "Unknown socket options: %s\n", p
);
2535 vlan
->nb_host_devs
++;
2538 if (!strcmp(device
, "vde")) {
2539 static const char * const vde_params
[] = {
2540 "vlan", "name", "sock", "port", "group", "mode", NULL
2542 char vde_sock
[1024], vde_group
[512];
2543 int vde_port
, vde_mode
;
2545 if (check_params(buf
, sizeof(buf
), vde_params
, p
) < 0) {
2546 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2550 vlan
->nb_host_devs
++;
2551 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2554 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2555 vde_port
= strtol(buf
, NULL
, 10);
2559 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2560 vde_group
[0] = '\0';
2562 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2563 vde_mode
= strtol(buf
, NULL
, 8);
2567 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2570 if (!strcmp(device
, "dump")) {
2573 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2574 len
= strtol(buf
, NULL
, 0);
2576 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2577 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2579 ret
= net_dump_init(mon
, vlan
, device
, name
, buf
, len
);
2581 config_error(mon
, "Unknown network device: %s\n", device
);
2586 config_error(mon
, "Could not initialize device '%s'\n", device
);
2593 void net_client_uninit(NICInfo
*nd
)
2595 nd
->vlan
->nb_guest_devs
--;
2598 free((void *)nd
->model
);
2601 static int net_host_check_device(const char *device
)
2604 const char *valid_param_list
[] = { "tap", "socket", "dump"
2612 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2613 if (!strncmp(valid_param_list
[i
], device
,
2614 strlen(valid_param_list
[i
])))
2621 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2623 if (!net_host_check_device(device
)) {
2624 monitor_printf(mon
, "invalid host network device %s\n", device
);
2627 if (net_client_init(mon
, device
, opts
? opts
: "") < 0) {
2628 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2632 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2635 VLANClientState
*vc
;
2637 vlan
= qemu_find_vlan(vlan_id
);
2639 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2640 if (!strcmp(vc
->name
, device
)) {
2646 monitor_printf(mon
, "can't find device %s\n", device
);
2649 if (!net_host_check_device(vc
->model
)) {
2650 monitor_printf(mon
, "invalid host network device %s\n", device
);
2653 qemu_del_vlan_client(vc
);
2656 int net_client_parse(const char *str
)
2664 while (*p
!= '\0' && *p
!= ',') {
2665 if ((q
- device
) < sizeof(device
) - 1)
2673 return net_client_init(NULL
, device
, p
);
2676 void net_set_boot_mask(int net_boot_mask
)
2680 /* Only the first four NICs may be bootable */
2681 net_boot_mask
= net_boot_mask
& 0xF;
2683 for (i
= 0; i
< nb_nics
; i
++) {
2684 if (net_boot_mask
& (1 << i
)) {
2685 nd_table
[i
].bootable
= 1;
2686 net_boot_mask
&= ~(1 << i
);
2690 if (net_boot_mask
) {
2691 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
2696 void do_info_network(Monitor
*mon
)
2699 VLANClientState
*vc
;
2701 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2702 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2703 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2704 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2708 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
2711 VLANClientState
*vc
= NULL
;
2713 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
2714 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
2715 if (strcmp(vc
->name
, name
) == 0)
2720 monitor_printf(mon
, "could not find network device '%s'", name
);
2724 if (strcmp(up_or_down
, "up") == 0)
2726 else if (strcmp(up_or_down
, "down") == 0)
2729 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2730 "valid\n", up_or_down
);
2732 if (vc
->link_status_changed
)
2733 vc
->link_status_changed(vc
);
2738 void net_cleanup(void)
2742 /* close network clients */
2743 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2744 VLANClientState
*vc
= vlan
->first_client
;
2747 VLANClientState
*next
= vc
->next
;
2749 qemu_del_vlan_client(vc
);
2756 void net_client_check(void)
2760 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2761 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2763 if (vlan
->nb_guest_devs
== 0)
2764 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2765 if (vlan
->nb_host_devs
== 0)
2767 "Warning: vlan %d is not connected to host network\n",