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 #include "slirp/libslirp.h"
127 static VLANState
*first_vlan
;
129 /***********************************************************/
130 /* network device redirectors */
132 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
133 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
137 for(i
=0;i
<size
;i
+=16) {
141 fprintf(f
, "%08x ", i
);
144 fprintf(f
, " %02x", buf
[i
+j
]);
151 if (c
< ' ' || c
> '~')
160 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
167 offset
= strtol(p
, &last_char
, 0);
168 if (0 == errno
&& '\0' == *last_char
&&
169 offset
>= 0 && offset
<= 0xFFFFFF) {
170 macaddr
[3] = (offset
& 0xFF0000) >> 16;
171 macaddr
[4] = (offset
& 0xFF00) >> 8;
172 macaddr
[5] = offset
& 0xFF;
175 for(i
= 0; i
< 6; i
++) {
176 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
181 if (*p
!= ':' && *p
!= '-')
192 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
203 if (len
> buf_size
- 1)
212 int parse_host_src_port(struct sockaddr_in
*haddr
,
213 struct sockaddr_in
*saddr
,
214 const char *input_str
)
216 char *str
= strdup(input_str
);
217 char *host_str
= str
;
219 const char *src_str2
;
223 * Chop off any extra arguments at the end of the string which
224 * would start with a comma, then fill in the src port information
225 * if it was provided else use the "any address" and "any port".
227 if ((ptr
= strchr(str
,',')))
230 if ((src_str
= strchr(input_str
,'@'))) {
235 if (parse_host_port(haddr
, host_str
) < 0)
239 if (!src_str
|| *src_str
== '\0')
242 if (parse_host_port(saddr
, src_str2
) < 0)
253 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
261 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
263 saddr
->sin_family
= AF_INET
;
264 if (buf
[0] == '\0') {
265 saddr
->sin_addr
.s_addr
= 0;
267 if (qemu_isdigit(buf
[0])) {
268 if (!inet_aton(buf
, &saddr
->sin_addr
))
271 if ((he
= gethostbyname(buf
)) == NULL
)
273 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
276 port
= strtol(p
, (char **)&r
, 0);
279 saddr
->sin_port
= htons(port
);
283 #if !defined(_WIN32) && 0
284 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
289 len
= MIN(108, strlen(str
));
290 p
= strchr(str
, ',');
292 len
= MIN(len
, p
- str
);
294 memset(uaddr
, 0, sizeof(*uaddr
));
296 uaddr
->sun_family
= AF_UNIX
;
297 memcpy(uaddr
->sun_path
, str
, len
);
303 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
305 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
306 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
308 macaddr
[0], macaddr
[1], macaddr
[2],
309 macaddr
[3], macaddr
[4], macaddr
[5]);
312 static char *assign_name(VLANClientState
*vc1
, const char *model
)
318 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
321 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
322 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
326 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
331 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
334 NetCanReceive
*can_receive
,
336 NetReceiveIOV
*receive_iov
,
340 VLANClientState
*vc
, **pvc
;
341 vc
= qemu_mallocz(sizeof(VLANClientState
));
342 vc
->model
= strdup(model
);
344 vc
->name
= strdup(name
);
346 vc
->name
= assign_name(vc
, model
);
347 vc
->can_receive
= can_receive
;
348 vc
->receive
= receive
;
349 vc
->receive_iov
= receive_iov
;
350 vc
->cleanup
= cleanup
;
355 pvc
= &vlan
->first_client
;
362 void qemu_del_vlan_client(VLANClientState
*vc
)
364 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
380 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
382 VLANClientState
**pvc
= &vlan
->first_client
;
385 if ((*pvc
)->opaque
== opaque
)
393 static VLANClientState
*
394 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
395 const char *client_str
)
400 vlan
= qemu_find_vlan(vlan_id
, 0);
402 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
406 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
407 if (!strcmp(vc
->name
, client_str
)) {
412 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
413 client_str
, vlan_id
);
419 int qemu_can_send_packet(VLANClientState
*sender
)
421 VLANState
*vlan
= sender
->vlan
;
424 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
429 /* no can_receive() handler, they can always receive */
430 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
438 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
, int raw
)
443 sender
->vlan
->delivering
= 1;
445 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
457 if (raw
&& vc
->receive_raw
) {
458 len
= vc
->receive_raw(vc
, buf
, size
);
460 len
= vc
->receive(vc
, buf
, size
);
463 ret
= (ret
>= 0) ? ret
: len
;
466 sender
->vlan
->delivering
= 0;
471 void qemu_purge_queued_packets(VLANClientState
*vc
)
473 VLANPacket
**pp
= &vc
->vlan
->send_queue
;
475 while (*pp
!= NULL
) {
476 VLANPacket
*packet
= *pp
;
478 if (packet
->sender
== vc
) {
487 void qemu_flush_queued_packets(VLANClientState
*vc
)
491 while ((packet
= vc
->vlan
->send_queue
) != NULL
) {
494 vc
->vlan
->send_queue
= packet
->next
;
496 ret
= qemu_deliver_packet(packet
->sender
, packet
->data
,
497 packet
->size
, packet
->raw
);
498 if (ret
== 0 && packet
->sent_cb
!= NULL
) {
499 packet
->next
= vc
->vlan
->send_queue
;
500 vc
->vlan
->send_queue
= packet
;
505 packet
->sent_cb(packet
->sender
, ret
);
511 static void qemu_enqueue_packet(VLANClientState
*sender
,
512 const uint8_t *buf
, int size
, int raw
,
513 NetPacketSent
*sent_cb
)
517 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
518 packet
->next
= sender
->vlan
->send_queue
;
519 packet
->sender
= sender
;
522 packet
->sent_cb
= sent_cb
;
523 memcpy(packet
->data
, buf
, size
);
524 sender
->vlan
->send_queue
= packet
;
527 static ssize_t
qemu_send_packet_async2(VLANClientState
*sender
,
528 const uint8_t *buf
, int size
, int raw
,
529 NetPacketSent
*sent_cb
)
533 if (sender
->link_down
) {
538 printf("vlan %d send:\n", sender
->vlan
->id
);
539 hex_dump(stdout
, buf
, size
);
542 if (sender
->vlan
->delivering
) {
543 qemu_enqueue_packet(sender
, buf
, size
, raw
, NULL
);
547 ret
= qemu_deliver_packet(sender
, buf
, size
, raw
);
548 if (ret
== 0 && sent_cb
!= NULL
) {
549 qemu_enqueue_packet(sender
, buf
, size
, raw
, sent_cb
);
553 qemu_flush_queued_packets(sender
);
558 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
559 const uint8_t *buf
, int size
,
560 NetPacketSent
*sent_cb
)
562 return qemu_send_packet_async2(sender
, buf
, size
, 0, sent_cb
);
565 ssize_t
qemu_send_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
567 return qemu_send_packet_async2(sender
, buf
, size
, 0, NULL
);
570 ssize_t
qemu_send_packet_raw(VLANClientState
*sender
, const uint8_t *buf
, int size
)
572 return qemu_send_packet_async2(sender
, buf
, size
, 1, NULL
);
575 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
578 uint8_t buffer
[4096];
582 for (i
= 0; i
< iovcnt
; i
++) {
585 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
586 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
590 return vc
->receive(vc
, buffer
, offset
);
593 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
598 for (i
= 0; i
< iovcnt
; i
++)
599 offset
+= iov
[i
].iov_len
;
603 static int qemu_deliver_packet_iov(VLANClientState
*sender
,
604 const struct iovec
*iov
, int iovcnt
)
609 sender
->vlan
->delivering
= 1;
611 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
619 ret
= calc_iov_length(iov
, iovcnt
);
623 if (vc
->receive_iov
) {
624 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
626 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
629 ret
= (ret
>= 0) ? ret
: len
;
632 sender
->vlan
->delivering
= 0;
637 static ssize_t
qemu_enqueue_packet_iov(VLANClientState
*sender
,
638 const struct iovec
*iov
, int iovcnt
,
639 NetPacketSent
*sent_cb
)
645 max_len
= calc_iov_length(iov
, iovcnt
);
647 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
648 packet
->next
= sender
->vlan
->send_queue
;
649 packet
->sender
= sender
;
650 packet
->sent_cb
= sent_cb
;
654 for (i
= 0; i
< iovcnt
; i
++) {
655 size_t len
= iov
[i
].iov_len
;
657 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
661 sender
->vlan
->send_queue
= packet
;
666 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
667 const struct iovec
*iov
, int iovcnt
,
668 NetPacketSent
*sent_cb
)
672 if (sender
->link_down
) {
673 return calc_iov_length(iov
, iovcnt
);
676 if (sender
->vlan
->delivering
) {
677 return qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, NULL
);
680 ret
= qemu_deliver_packet_iov(sender
, iov
, iovcnt
);
681 if (ret
== 0 && sent_cb
!= NULL
) {
682 qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, sent_cb
);
686 qemu_flush_queued_packets(sender
);
692 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
694 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
697 static void config_error(Monitor
*mon
, const char *fmt
, ...)
703 monitor_vprintf(mon
, fmt
, ap
);
705 fprintf(stderr
, "qemu: ");
706 vfprintf(stderr
, fmt
, ap
);
712 #if defined(CONFIG_SLIRP)
714 /* slirp network adapter */
716 #define SLIRP_CFG_HOSTFWD 1
717 #define SLIRP_CFG_LEGACY 2
719 struct slirp_config_str
{
720 struct slirp_config_str
*next
;
726 typedef struct SlirpState
{
727 TAILQ_ENTRY(SlirpState
) entry
;
735 static struct slirp_config_str
*slirp_configs
;
736 const char *legacy_tftp_prefix
;
737 const char *legacy_bootp_filename
;
738 static TAILQ_HEAD(slirp_stacks
, SlirpState
) slirp_stacks
=
739 TAILQ_HEAD_INITIALIZER(slirp_stacks
);
741 static void slirp_hostfwd(SlirpState
*s
, Monitor
*mon
, const char *redir_str
,
743 static void slirp_guestfwd(SlirpState
*s
, Monitor
*mon
, const char *config_str
,
747 static const char *legacy_smb_export
;
749 static void slirp_smb(SlirpState
*s
, Monitor
*mon
, const char *exported_dir
,
750 struct in_addr vserver_addr
);
751 static void slirp_smb_cleanup(SlirpState
*s
);
753 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
756 int slirp_can_output(void *opaque
)
758 SlirpState
*s
= opaque
;
760 return qemu_can_send_packet(s
->vc
);
763 void slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
765 SlirpState
*s
= opaque
;
768 printf("slirp output:\n");
769 hex_dump(stdout
, pkt
, pkt_len
);
771 qemu_send_packet(s
->vc
, pkt
, pkt_len
);
774 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
776 SlirpState
*s
= vc
->opaque
;
779 printf("slirp input:\n");
780 hex_dump(stdout
, buf
, size
);
782 slirp_input(s
->slirp
, buf
, size
);
786 static void net_slirp_cleanup(VLANClientState
*vc
)
788 SlirpState
*s
= vc
->opaque
;
790 slirp_cleanup(s
->slirp
);
791 slirp_smb_cleanup(s
);
792 TAILQ_REMOVE(&slirp_stacks
, s
, entry
);
796 static int net_slirp_init(Monitor
*mon
, VLANState
*vlan
, const char *model
,
797 const char *name
, int restricted
,
798 const char *vnetwork
, const char *vhost
,
799 const char *vhostname
, const char *tftp_export
,
800 const char *bootfile
, const char *vdhcp_start
,
801 const char *vnameserver
, const char *smb_export
,
802 const char *vsmbserver
)
804 /* default settings according to historic slirp */
805 struct in_addr net
= { .s_addr
= htonl(0x0a000000) }; /* 10.0.0.0 */
806 struct in_addr mask
= { .s_addr
= htonl(0xff000000) }; /* 255.0.0.0 */
807 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
808 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
809 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
811 struct in_addr smbsrv
= { .s_addr
= 0 };
820 tftp_export
= legacy_tftp_prefix
;
823 bootfile
= legacy_bootp_filename
;
827 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
828 if (!inet_aton(vnetwork
, &net
)) {
831 addr
= ntohl(net
.s_addr
);
832 if (!(addr
& 0x80000000)) {
833 mask
.s_addr
= htonl(0xff000000); /* class A */
834 } else if ((addr
& 0xfff00000) == 0xac100000) {
835 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
836 } else if ((addr
& 0xc0000000) == 0x80000000) {
837 mask
.s_addr
= htonl(0xffff0000); /* class B */
838 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
839 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
840 } else if ((addr
& 0xffff0000) == 0xc6120000) {
841 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
842 } else if ((addr
& 0xe0000000) == 0xe0000000) {
843 mask
.s_addr
= htonl(0xffffff00); /* class C */
845 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
848 if (!inet_aton(buf
, &net
)) {
851 shift
= strtol(vnetwork
, &end
, 10);
853 if (!inet_aton(vnetwork
, &mask
)) {
856 } else if (shift
< 4 || shift
> 32) {
859 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
862 net
.s_addr
&= mask
.s_addr
;
863 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
864 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
865 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
868 if (vhost
&& !inet_aton(vhost
, &host
)) {
871 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
875 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
878 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
||
879 dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
883 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
886 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
||
887 dns
.s_addr
== host
.s_addr
) {
892 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
897 s
= qemu_mallocz(sizeof(SlirpState
));
898 s
->slirp
= slirp_init(restricted
, net
, mask
, host
, vhostname
,
899 tftp_export
, bootfile
, dhcp
, dns
, s
);
900 TAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
902 while (slirp_configs
) {
903 struct slirp_config_str
*config
= slirp_configs
;
905 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
906 slirp_hostfwd(s
, mon
, config
->str
,
907 config
->flags
& SLIRP_CFG_LEGACY
);
909 slirp_guestfwd(s
, mon
, config
->str
,
910 config
->flags
& SLIRP_CFG_LEGACY
);
912 slirp_configs
= config
->next
;
917 smb_export
= legacy_smb_export
;
920 slirp_smb(s
, mon
, smb_export
, smbsrv
);
924 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
, NULL
,
925 net_slirp_cleanup
, s
);
926 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
927 "net=%s, restricted=%c", inet_ntoa(net
), restricted
? 'y' : 'n');
931 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *vlan
,
937 vc
= qemu_find_vlan_client_by_name(mon
, strtol(vlan
, NULL
, 0), stack
);
941 if (strcmp(vc
->model
, "user")) {
942 monitor_printf(mon
, "invalid device specified\n");
947 if (TAILQ_EMPTY(&slirp_stacks
)) {
948 monitor_printf(mon
, "user mode network stack not in use\n");
951 return TAILQ_FIRST(&slirp_stacks
);
955 void net_slirp_hostfwd_remove(Monitor
*mon
, const char *arg1
,
956 const char *arg2
, const char *arg3
)
958 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
961 const char *src_str
, *p
;
967 s
= slirp_lookup(mon
, arg1
, arg2
);
970 s
= slirp_lookup(mon
, NULL
, NULL
);
977 if (!src_str
|| !src_str
[0])
981 get_str_sep(buf
, sizeof(buf
), &p
, ':');
983 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
985 } else if (!strcmp(buf
, "udp")) {
991 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
994 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
1000 err
= slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks
)->slirp
, is_udp
,
1001 host_addr
, host_port
);
1003 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
1004 err
? "removed" : "not found");
1008 monitor_printf(mon
, "invalid format\n");
1011 static void slirp_hostfwd(SlirpState
*s
, Monitor
*mon
, const char *redir_str
,
1014 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
1015 struct in_addr guest_addr
= { .s_addr
= 0 };
1016 int host_port
, guest_port
;
1023 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1026 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
1028 } else if (!strcmp(buf
, "udp")) {
1034 if (!legacy_format
) {
1035 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1038 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
1043 if (get_str_sep(buf
, sizeof(buf
), &p
, legacy_format
? ':' : '-') < 0) {
1046 host_port
= strtol(buf
, &end
, 0);
1047 if (*end
!= '\0' || host_port
< 1 || host_port
> 65535) {
1051 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1054 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
1058 guest_port
= strtol(p
, &end
, 0);
1059 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
1063 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
1065 config_error(mon
, "could not set up host forwarding rule '%s'\n",
1071 config_error(mon
, "invalid host forwarding rule '%s'\n", redir_str
);
1074 void net_slirp_hostfwd_add(Monitor
*mon
, const char *arg1
,
1075 const char *arg2
, const char *arg3
)
1077 const char *redir_str
;
1081 s
= slirp_lookup(mon
, arg1
, arg2
);
1084 s
= slirp_lookup(mon
, NULL
, NULL
);
1088 slirp_hostfwd(s
, mon
, redir_str
, 0);
1093 void net_slirp_redir(const char *redir_str
)
1095 struct slirp_config_str
*config
;
1097 if (TAILQ_EMPTY(&slirp_stacks
)) {
1098 config
= qemu_malloc(sizeof(*config
));
1099 pstrcpy(config
->str
, sizeof(config
->str
), redir_str
);
1100 config
->flags
= SLIRP_CFG_HOSTFWD
| SLIRP_CFG_LEGACY
;
1101 config
->next
= slirp_configs
;
1102 slirp_configs
= config
;
1106 slirp_hostfwd(TAILQ_FIRST(&slirp_stacks
), NULL
, redir_str
, 1);
1111 /* automatic user mode samba server configuration */
1112 static void slirp_smb_cleanup(SlirpState
*s
)
1116 if (s
->smb_dir
[0] != '\0') {
1117 snprintf(cmd
, sizeof(cmd
), "rm -rf %s", s
->smb_dir
);
1119 s
->smb_dir
[0] = '\0';
1123 static void slirp_smb(SlirpState
* s
, Monitor
*mon
, const char *exported_dir
,
1124 struct in_addr vserver_addr
)
1126 static int instance
;
1128 char smb_cmdline
[128];
1131 snprintf(s
->smb_dir
, sizeof(s
->smb_dir
), "/tmp/qemu-smb.%ld-%d",
1132 (long)getpid(), instance
++);
1133 if (mkdir(s
->smb_dir
, 0700) < 0) {
1134 config_error(mon
, "could not create samba server dir '%s'\n",
1138 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", s
->smb_dir
, "smb.conf");
1140 f
= fopen(smb_conf
, "w");
1142 slirp_smb_cleanup(s
);
1143 config_error(mon
, "could not create samba server "
1144 "configuration file '%s'\n", smb_conf
);
1151 "socket address=127.0.0.1\n"
1152 "pid directory=%s\n"
1153 "lock directory=%s\n"
1154 "log file=%s/log.smbd\n"
1155 "smb passwd file=%s/smbpasswd\n"
1156 "security = share\n"
1170 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1171 SMBD_COMMAND
, smb_conf
);
1173 if (slirp_add_exec(s
->slirp
, 0, smb_cmdline
, vserver_addr
, 139) < 0) {
1174 slirp_smb_cleanup(s
);
1175 config_error(mon
, "conflicting/invalid smbserver address\n");
1179 /* automatic user mode samba server configuration (legacy interface) */
1180 void net_slirp_smb(const char *exported_dir
)
1182 struct in_addr vserver_addr
= { .s_addr
= 0 };
1184 if (legacy_smb_export
) {
1185 fprintf(stderr
, "-smb given twice\n");
1188 legacy_smb_export
= exported_dir
;
1189 if (!TAILQ_EMPTY(&slirp_stacks
)) {
1190 slirp_smb(TAILQ_FIRST(&slirp_stacks
), NULL
, exported_dir
,
1195 #endif /* !defined(_WIN32) */
1198 CharDriverState
*hd
;
1199 struct in_addr server
;
1204 static int guestfwd_can_read(void *opaque
)
1206 struct GuestFwd
*fwd
= opaque
;
1207 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1210 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1212 struct GuestFwd
*fwd
= opaque
;
1213 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1216 static void slirp_guestfwd(SlirpState
*s
, Monitor
*mon
, const char *config_str
,
1219 struct in_addr server
= { .s_addr
= 0 };
1220 struct GuestFwd
*fwd
;
1227 if (legacy_format
) {
1228 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1232 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1235 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1238 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1241 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1244 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1248 port
= strtol(buf
, &end
, 10);
1249 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1253 fwd
= qemu_malloc(sizeof(struct GuestFwd
));
1254 snprintf(buf
, sizeof(buf
), "guestfwd.tcp:%d", port
);
1255 fwd
->hd
= qemu_chr_open(buf
, p
, NULL
);
1257 config_error(mon
, "could not open guest forwarding device '%s'\n",
1262 fwd
->server
= server
;
1264 fwd
->slirp
= s
->slirp
;
1266 if (slirp_add_exec(s
->slirp
, 3, fwd
->hd
, server
, port
) < 0) {
1267 config_error(mon
, "conflicting/invalid host:port in guest forwarding "
1268 "rule '%s'\n", config_str
);
1272 qemu_chr_add_handlers(fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1277 config_error(mon
, "invalid guest forwarding rule '%s'\n", config_str
);
1280 void do_info_usernet(Monitor
*mon
)
1284 TAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1285 monitor_printf(mon
, "VLAN %d (%s):\n", s
->vc
->vlan
->id
, s
->vc
->name
);
1286 slirp_connection_info(s
->slirp
, mon
);
1290 #endif /* CONFIG_SLIRP */
1294 int tap_has_vnet_hdr(void *opaque
)
1299 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1303 #else /* !defined(_WIN32) */
1305 /* Maximum GSO packet size (64k) plus plenty of room for
1306 * the ethernet and virtio_net headers
1308 #define TAP_BUFSIZE (4096 + 65536)
1311 #include <linux/virtio_net.h>
1314 typedef struct TAPState
{
1315 VLANClientState
*vc
;
1317 char down_script
[1024];
1318 char down_script_arg
[128];
1319 uint8_t buf
[TAP_BUFSIZE
];
1320 unsigned int read_poll
: 1;
1321 unsigned int write_poll
: 1;
1322 unsigned int has_vnet_hdr
: 1;
1323 unsigned int using_vnet_hdr
: 1;
1326 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
1328 static int tap_can_send(void *opaque
);
1329 static void tap_send(void *opaque
);
1330 static void tap_writable(void *opaque
);
1332 static void tap_update_fd_handler(TAPState
*s
)
1334 qemu_set_fd_handler2(s
->fd
,
1335 s
->read_poll
? tap_can_send
: NULL
,
1336 s
->read_poll
? tap_send
: NULL
,
1337 s
->write_poll
? tap_writable
: NULL
,
1341 static void tap_read_poll(TAPState
*s
, int enable
)
1343 s
->read_poll
= !!enable
;
1344 tap_update_fd_handler(s
);
1347 static void tap_write_poll(TAPState
*s
, int enable
)
1349 s
->write_poll
= !!enable
;
1350 tap_update_fd_handler(s
);
1353 static void tap_writable(void *opaque
)
1355 TAPState
*s
= opaque
;
1357 tap_write_poll(s
, 0);
1359 qemu_flush_queued_packets(s
->vc
);
1362 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
1365 TAPState
*s
= vc
->opaque
;
1369 len
= writev(s
->fd
, iov
, iovcnt
);
1370 } while (len
== -1 && errno
== EINTR
);
1372 if (len
== -1 && errno
== EAGAIN
) {
1373 tap_write_poll(s
, 1);
1380 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1382 struct iovec iov
[2];
1386 TAPState
*s
= vc
->opaque
;
1387 struct virtio_net_hdr hdr
= { 0, };
1389 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
1390 iov
[i
].iov_base
= &hdr
;
1391 iov
[i
].iov_len
= sizeof(hdr
);
1396 iov
[i
].iov_base
= (char *) buf
;
1397 iov
[i
].iov_len
= size
;
1400 return tap_receive_iov(vc
, iov
, i
);
1403 static ssize_t
tap_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1405 struct iovec iov
[2];
1409 TAPState
*s
= vc
->opaque
;
1410 struct virtio_net_hdr hdr
= { 0, };
1412 if (s
->has_vnet_hdr
&& s
->using_vnet_hdr
) {
1413 iov
[i
].iov_base
= &hdr
;
1414 iov
[i
].iov_len
= sizeof(hdr
);
1419 iov
[i
].iov_base
= (char *) buf
;
1420 iov
[i
].iov_len
= size
;
1423 return tap_receive_iov(vc
, iov
, i
);
1426 static int tap_can_send(void *opaque
)
1428 TAPState
*s
= opaque
;
1430 return qemu_can_send_packet(s
->vc
);
1434 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1439 sbuf
.maxlen
= maxlen
;
1440 sbuf
.buf
= (char *)buf
;
1442 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1445 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1447 return read(tapfd
, buf
, maxlen
);
1451 static void tap_send_completed(VLANClientState
*vc
, ssize_t len
)
1453 TAPState
*s
= vc
->opaque
;
1454 tap_read_poll(s
, 1);
1457 static void tap_send(void *opaque
)
1459 TAPState
*s
= opaque
;
1463 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
1468 size
= qemu_send_packet_async(s
->vc
, s
->buf
, size
, tap_send_completed
);
1470 tap_read_poll(s
, 0);
1475 static void tap_set_sndbuf(TAPState
*s
, int sndbuf
, Monitor
*mon
)
1478 if (ioctl(s
->fd
, TUNSETSNDBUF
, &sndbuf
) == -1) {
1479 config_error(mon
, "TUNSETSNDBUF ioctl failed: %s\n",
1483 config_error(mon
, "No '-net tap,sndbuf=<nbytes>' support available\n");
1487 int tap_has_vnet_hdr(void *opaque
)
1489 VLANClientState
*vc
= opaque
;
1490 TAPState
*s
= vc
->opaque
;
1492 return s
? s
->has_vnet_hdr
: 0;
1495 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1497 VLANClientState
*vc
= opaque
;
1498 TAPState
*s
= vc
->opaque
;
1500 if (!s
|| !s
->has_vnet_hdr
)
1503 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
1506 static int tap_probe_vnet_hdr(int fd
)
1508 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1511 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
1512 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
1516 return ifr
.ifr_flags
& IFF_VNET_HDR
;
1522 #ifdef TUNSETOFFLOAD
1523 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
1526 TAPState
*s
= vc
->opaque
;
1527 unsigned int offload
= 0;
1530 offload
|= TUN_F_CSUM
;
1532 offload
|= TUN_F_TSO4
;
1534 offload
|= TUN_F_TSO6
;
1535 if ((tso4
|| tso6
) && ecn
)
1536 offload
|= TUN_F_TSO_ECN
;
1539 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0)
1540 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
1543 #endif /* TUNSETOFFLOAD */
1545 static void tap_cleanup(VLANClientState
*vc
)
1547 TAPState
*s
= vc
->opaque
;
1549 qemu_purge_queued_packets(vc
);
1551 if (s
->down_script
[0])
1552 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1554 tap_read_poll(s
, 0);
1555 tap_write_poll(s
, 0);
1562 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1570 s
= qemu_mallocz(sizeof(TAPState
));
1572 s
->has_vnet_hdr
= vnet_hdr
!= 0;
1573 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1574 tap_receive_iov
, tap_cleanup
, s
);
1575 s
->vc
->receive_raw
= tap_receive_raw
;
1576 #ifdef TUNSETOFFLOAD
1577 s
->vc
->set_offload
= tap_set_offload
;
1578 tap_set_offload(s
->vc
, 0, 0, 0, 0);
1580 tap_read_poll(s
, 1);
1581 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1585 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1586 static int tap_open(char *ifname
, int ifname_size
)
1592 TFR(fd
= open("/dev/tap", O_RDWR
));
1594 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1599 dev
= devname(s
.st_rdev
, S_IFCHR
);
1600 pstrcpy(ifname
, ifname_size
, dev
);
1602 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1605 #elif defined(__sun__)
1606 #define TUNNEWPPA (('T'<<16) | 0x0001)
1608 * Allocate TAP device, returns opened fd.
1609 * Stores dev name in the first arg(must be large enough).
1611 static int tap_alloc(char *dev
, size_t dev_size
)
1613 int tap_fd
, if_fd
, ppa
= -1;
1614 static int ip_fd
= 0;
1617 static int arp_fd
= 0;
1618 int ip_muxid
, arp_muxid
;
1619 struct strioctl strioc_if
, strioc_ppa
;
1620 int link_type
= I_PLINK
;;
1622 char actual_name
[32] = "";
1624 memset(&ifr
, 0x0, sizeof(ifr
));
1628 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1632 /* Check if IP device was opened */
1636 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1638 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1642 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1644 syslog(LOG_ERR
, "Can't open /dev/tap");
1648 /* Assign a new PPA and get its unit number. */
1649 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1650 strioc_ppa
.ic_timout
= 0;
1651 strioc_ppa
.ic_len
= sizeof(ppa
);
1652 strioc_ppa
.ic_dp
= (char *)&ppa
;
1653 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1654 syslog (LOG_ERR
, "Can't assign new interface");
1656 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1658 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1661 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1662 syslog(LOG_ERR
, "Can't push IP module");
1666 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1667 syslog(LOG_ERR
, "Can't get flags\n");
1669 snprintf (actual_name
, 32, "tap%d", ppa
);
1670 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1673 /* Assign ppa according to the unit number returned by tun device */
1675 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1676 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1677 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1678 syslog (LOG_ERR
, "Can't get flags\n");
1679 /* Push arp module to if_fd */
1680 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1681 syslog (LOG_ERR
, "Can't push ARP module (2)");
1683 /* Push arp module to ip_fd */
1684 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1685 syslog (LOG_ERR
, "I_POP failed\n");
1686 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1687 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1689 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1691 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1693 /* Set ifname to arp */
1694 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1695 strioc_if
.ic_timout
= 0;
1696 strioc_if
.ic_len
= sizeof(ifr
);
1697 strioc_if
.ic_dp
= (char *)&ifr
;
1698 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1699 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1702 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1703 syslog(LOG_ERR
, "Can't link TAP device to IP");
1707 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1708 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1712 memset(&ifr
, 0x0, sizeof(ifr
));
1713 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1714 ifr
.lifr_ip_muxid
= ip_muxid
;
1715 ifr
.lifr_arp_muxid
= arp_muxid
;
1717 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1719 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1720 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1721 syslog (LOG_ERR
, "Can't set multiplexor id");
1724 snprintf(dev
, dev_size
, "tap%d", ppa
);
1728 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1732 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1733 fprintf(stderr
, "Cannot allocate TAP device\n");
1736 pstrcpy(ifname
, ifname_size
, dev
);
1737 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1740 #elif defined (_AIX)
1741 static int tap_open(char *ifname
, int ifname_size
)
1743 fprintf (stderr
, "no tap on AIX\n");
1747 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1752 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1754 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1757 memset(&ifr
, 0, sizeof(ifr
));
1758 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1760 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1762 unsigned int features
;
1764 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1765 features
& IFF_VNET_HDR
) {
1767 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1772 if (ifname
[0] != '\0')
1773 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1775 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1776 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1778 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1782 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1783 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1788 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1790 sigset_t oldmask
, mask
;
1796 sigaddset(&mask
, SIGCHLD
);
1797 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1799 /* try to launch network script */
1802 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1804 for (i
= 0; i
< open_max
; i
++) {
1805 if (i
!= STDIN_FILENO
&&
1806 i
!= STDOUT_FILENO
&&
1807 i
!= STDERR_FILENO
&&
1813 *parg
++ = (char *)setup_script
;
1814 *parg
++ = (char *)ifname
;
1816 execv(setup_script
, args
);
1818 } else if (pid
> 0) {
1819 while (waitpid(pid
, &status
, 0) != pid
) {
1822 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1824 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1828 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1832 static TAPState
*net_tap_init(VLANState
*vlan
, const char *model
,
1833 const char *name
, const char *ifname1
,
1834 const char *setup_script
, const char *down_script
)
1841 if (ifname1
!= NULL
)
1842 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1846 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1850 if (!setup_script
|| !strcmp(setup_script
, "no"))
1852 if (setup_script
[0] != '\0' &&
1853 launch_script(setup_script
, ifname
, fd
)) {
1856 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1857 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1858 "ifname=%s,script=%s,downscript=%s",
1859 ifname
, setup_script
, down_script
);
1860 if (down_script
&& strcmp(down_script
, "no")) {
1861 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1862 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1867 #endif /* !_WIN32 */
1869 #if defined(CONFIG_VDE)
1870 typedef struct VDEState
{
1871 VLANClientState
*vc
;
1875 static void vde_to_qemu(void *opaque
)
1877 VDEState
*s
= opaque
;
1881 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1883 qemu_send_packet(s
->vc
, buf
, size
);
1887 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1889 VDEState
*s
= vc
->opaque
;
1893 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1894 } while (ret
< 0 && errno
== EINTR
);
1899 static void vde_cleanup(VLANClientState
*vc
)
1901 VDEState
*s
= vc
->opaque
;
1902 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1907 static int net_vde_init(VLANState
*vlan
, const char *model
,
1908 const char *name
, const char *sock
,
1909 int port
, const char *group
, int mode
)
1912 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1913 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1915 struct vde_open_args args
= {
1917 .group
= init_group
,
1921 s
= qemu_mallocz(sizeof(VDEState
));
1922 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1927 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_receive
,
1928 NULL
, vde_cleanup
, s
);
1929 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1930 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1931 sock
, vde_datafd(s
->vde
));
1936 /* network connection */
1937 typedef struct NetSocketState
{
1938 VLANClientState
*vc
;
1940 int state
; /* 0 = getting length, 1 = getting data */
1942 unsigned int packet_len
;
1944 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1947 typedef struct NetSocketListenState
{
1952 } NetSocketListenState
;
1954 /* XXX: we consider we can send the whole packet without blocking */
1955 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1957 NetSocketState
*s
= vc
->opaque
;
1961 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1962 return send_all(s
->fd
, buf
, size
);
1965 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1967 NetSocketState
*s
= vc
->opaque
;
1969 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1970 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1973 static void net_socket_send(void *opaque
)
1975 NetSocketState
*s
= opaque
;
1981 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1983 err
= socket_error();
1984 if (err
!= EWOULDBLOCK
)
1986 } else if (size
== 0) {
1987 /* end of connection */
1989 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1995 /* reassemble a packet from the network */
2001 memcpy(s
->buf
+ s
->index
, buf
, l
);
2005 if (s
->index
== 4) {
2007 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2013 l
= s
->packet_len
- s
->index
;
2016 if (s
->index
+ l
<= sizeof(s
->buf
)) {
2017 memcpy(s
->buf
+ s
->index
, buf
, l
);
2019 fprintf(stderr
, "serious error: oversized packet received,"
2020 "connection terminated.\n");
2028 if (s
->index
>= s
->packet_len
) {
2029 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2038 static void net_socket_send_dgram(void *opaque
)
2040 NetSocketState
*s
= opaque
;
2043 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
2047 /* end of connection */
2048 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2051 qemu_send_packet(s
->vc
, s
->buf
, size
);
2054 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2059 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2060 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2061 inet_ntoa(mcastaddr
->sin_addr
),
2062 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2066 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2068 perror("socket(PF_INET, SOCK_DGRAM)");
2073 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2074 (const char *)&val
, sizeof(val
));
2076 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2080 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2086 /* Add host to multicast group */
2087 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2088 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2090 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2091 (const char *)&imr
, sizeof(struct ip_mreq
));
2093 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2097 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2099 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2100 (const char *)&val
, sizeof(val
));
2102 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2106 socket_set_nonblock(fd
);
2114 static void net_socket_cleanup(VLANClientState
*vc
)
2116 NetSocketState
*s
= vc
->opaque
;
2117 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2122 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
2125 int fd
, int is_connected
)
2127 struct sockaddr_in saddr
;
2129 socklen_t saddr_len
;
2132 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2133 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2134 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2138 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2140 if (saddr
.sin_addr
.s_addr
==0) {
2141 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2145 /* clone dgram socket */
2146 newfd
= net_socket_mcast_create(&saddr
);
2148 /* error already reported by net_socket_mcast_create() */
2152 /* clone newfd to fd, close newfd */
2157 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2158 fd
, strerror(errno
));
2163 s
= qemu_mallocz(sizeof(NetSocketState
));
2166 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
2167 NULL
, net_socket_cleanup
, s
);
2168 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2170 /* mcast: save bound address as dst */
2171 if (is_connected
) s
->dgram_dst
=saddr
;
2173 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2174 "socket: fd=%d (%s mcast=%s:%d)",
2175 fd
, is_connected
? "cloned" : "",
2176 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2180 static void net_socket_connect(void *opaque
)
2182 NetSocketState
*s
= opaque
;
2183 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2186 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
2189 int fd
, int is_connected
)
2192 s
= qemu_mallocz(sizeof(NetSocketState
));
2194 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
2195 NULL
, net_socket_cleanup
, s
);
2196 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2197 "socket: fd=%d", fd
);
2199 net_socket_connect(s
);
2201 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2206 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
2207 const char *model
, const char *name
,
2208 int fd
, int is_connected
)
2210 int so_type
=-1, optlen
=sizeof(so_type
);
2212 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
2213 (socklen_t
*)&optlen
)< 0) {
2214 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2219 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
2221 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2223 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2224 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2225 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2230 static void net_socket_accept(void *opaque
)
2232 NetSocketListenState
*s
= opaque
;
2234 struct sockaddr_in saddr
;
2239 len
= sizeof(saddr
);
2240 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2241 if (fd
< 0 && errno
!= EINTR
) {
2243 } else if (fd
>= 0) {
2247 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
2251 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2252 "socket: connection from %s:%d",
2253 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2257 static int net_socket_listen_init(VLANState
*vlan
,
2260 const char *host_str
)
2262 NetSocketListenState
*s
;
2264 struct sockaddr_in saddr
;
2266 if (parse_host_port(&saddr
, host_str
) < 0)
2269 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2271 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2276 socket_set_nonblock(fd
);
2278 /* allow fast reuse */
2280 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2282 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2287 ret
= listen(fd
, 0);
2293 s
->model
= strdup(model
);
2294 s
->name
= name
? strdup(name
) : NULL
;
2296 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2300 static int net_socket_connect_init(VLANState
*vlan
,
2303 const char *host_str
)
2306 int fd
, connected
, ret
, err
;
2307 struct sockaddr_in saddr
;
2309 if (parse_host_port(&saddr
, host_str
) < 0)
2312 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2317 socket_set_nonblock(fd
);
2321 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2323 err
= socket_error();
2324 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2325 } else if (err
== EINPROGRESS
) {
2328 } else if (err
== WSAEALREADY
) {
2341 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
2344 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2345 "socket: connect to %s:%d",
2346 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2350 static int net_socket_mcast_init(VLANState
*vlan
,
2353 const char *host_str
)
2357 struct sockaddr_in saddr
;
2359 if (parse_host_port(&saddr
, host_str
) < 0)
2363 fd
= net_socket_mcast_create(&saddr
);
2367 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
2371 s
->dgram_dst
= saddr
;
2373 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2374 "socket: mcast=%s:%d",
2375 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2380 typedef struct DumpState
{
2381 VLANClientState
*pcap_vc
;
2386 #define PCAP_MAGIC 0xa1b2c3d4
2388 struct pcap_file_hdr
{
2390 uint16_t version_major
;
2391 uint16_t version_minor
;
2398 struct pcap_sf_pkthdr
{
2407 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
2409 DumpState
*s
= vc
->opaque
;
2410 struct pcap_sf_pkthdr hdr
;
2414 /* Early return in case of previous error. */
2419 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, ticks_per_sec
);
2420 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
2422 hdr
.ts
.tv_sec
= ts
/ 1000000;
2423 hdr
.ts
.tv_usec
= ts
% 1000000;
2424 hdr
.caplen
= caplen
;
2426 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
2427 write(s
->fd
, buf
, caplen
) != caplen
) {
2428 qemu_log("-net dump write error - stop dump\n");
2436 static void net_dump_cleanup(VLANClientState
*vc
)
2438 DumpState
*s
= vc
->opaque
;
2444 static int net_dump_init(Monitor
*mon
, VLANState
*vlan
, const char *device
,
2445 const char *name
, const char *filename
, int len
)
2447 struct pcap_file_hdr hdr
;
2450 s
= qemu_malloc(sizeof(DumpState
));
2452 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
2454 config_error(mon
, "-net dump: can't open %s\n", filename
);
2458 s
->pcap_caplen
= len
;
2460 hdr
.magic
= PCAP_MAGIC
;
2461 hdr
.version_major
= 2;
2462 hdr
.version_minor
= 4;
2465 hdr
.snaplen
= s
->pcap_caplen
;
2468 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2469 config_error(mon
, "-net dump write error: %s\n", strerror(errno
));
2475 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
2476 net_dump_cleanup
, s
);
2477 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2478 "dump to %s (len=%d)", filename
, len
);
2482 /* find or alloc a new VLAN */
2483 VLANState
*qemu_find_vlan(int id
, int allocate
)
2485 VLANState
**pvlan
, *vlan
;
2486 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2493 vlan
= qemu_mallocz(sizeof(VLANState
));
2496 pvlan
= &first_vlan
;
2497 while (*pvlan
!= NULL
)
2498 pvlan
= &(*pvlan
)->next
;
2503 static int nic_get_free_idx(void)
2507 for (index
= 0; index
< MAX_NICS
; index
++)
2508 if (!nd_table
[index
].used
)
2513 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2515 const char *models
[2];
2520 qemu_check_nic_model_list(nd
, models
, model
);
2523 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
2524 const char *default_model
)
2526 int i
, exit_status
= 0;
2529 nd
->model
= strdup(default_model
);
2531 if (strcmp(nd
->model
, "?") != 0) {
2532 for (i
= 0 ; models
[i
]; i
++)
2533 if (strcmp(nd
->model
, models
[i
]) == 0)
2536 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
2540 fprintf(stderr
, "qemu: Supported NIC models: ");
2541 for (i
= 0 ; models
[i
]; i
++)
2542 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2547 int net_client_init(Monitor
*mon
, const char *device
, const char *p
)
2555 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
2556 vlan_id
= strtol(buf
, NULL
, 0);
2558 vlan
= qemu_find_vlan(vlan_id
, 1);
2560 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
2561 name
= qemu_strdup(buf
);
2563 if (!strcmp(device
, "nic")) {
2564 static const char * const nic_params
[] = {
2565 "vlan", "name", "macaddr", "model", "addr", "vectors", NULL
2569 int idx
= nic_get_free_idx();
2571 if (check_params(buf
, sizeof(buf
), nic_params
, p
) < 0) {
2572 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2576 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2577 config_error(mon
, "Too Many NICs\n");
2581 nd
= &nd_table
[idx
];
2582 macaddr
= nd
->macaddr
;
2588 macaddr
[5] = 0x56 + idx
;
2590 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
2591 if (parse_macaddr(macaddr
, buf
) < 0) {
2592 config_error(mon
, "invalid syntax for ethernet address\n");
2597 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
2598 nd
->model
= strdup(buf
);
2600 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
2601 nd
->devaddr
= strdup(buf
);
2603 nd
->nvectors
= NIC_NVECTORS_UNSPECIFIED
;
2604 if (get_param_value(buf
, sizeof(buf
), "vectors", p
)) {
2606 long vectors
= strtol(buf
, &endptr
, 0);
2608 config_error(mon
, "invalid syntax for # of vectors\n");
2612 if (vectors
< 0 || vectors
> 0x7ffffff) {
2613 config_error(mon
, "invalid # of vectors\n");
2617 nd
->nvectors
= vectors
;
2624 vlan
->nb_guest_devs
++;
2627 if (!strcmp(device
, "none")) {
2629 config_error(mon
, "'none' takes no parameters\n");
2633 /* does nothing. It is needed to signal that no network cards
2638 if (!strcmp(device
, "user")) {
2639 static const char * const slirp_params
[] = {
2640 "vlan", "name", "hostname", "restrict", "ip", "net", "host",
2641 "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver",
2642 "hostfwd", "guestfwd", NULL
2644 struct slirp_config_str
*config
;
2648 char *vhostname
= NULL
;
2649 char *tftp_export
= NULL
;
2650 char *bootfile
= NULL
;
2651 char *vdhcp_start
= NULL
;
2652 char *vnamesrv
= NULL
;
2653 char *smb_export
= NULL
;
2654 char *vsmbsrv
= NULL
;
2657 if (check_params(buf
, sizeof(buf
), slirp_params
, p
) < 0) {
2658 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2662 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
2663 /* emulate legacy parameter */
2664 vnet
= qemu_malloc(strlen(buf
) + strlen("/24") + 1);
2666 strcat(vnet
, "/24");
2668 if (get_param_value(buf
, sizeof(buf
), "net", p
)) {
2669 vnet
= qemu_strdup(buf
);
2671 if (get_param_value(buf
, sizeof(buf
), "host", p
)) {
2672 vhost
= qemu_strdup(buf
);
2674 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
2675 vhostname
= qemu_strdup(buf
);
2677 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
2678 restricted
= (buf
[0] == 'y') ? 1 : 0;
2680 if (get_param_value(buf
, sizeof(buf
), "dhcpstart", p
)) {
2681 vdhcp_start
= qemu_strdup(buf
);
2683 if (get_param_value(buf
, sizeof(buf
), "dns", p
)) {
2684 vnamesrv
= qemu_strdup(buf
);
2686 if (get_param_value(buf
, sizeof(buf
), "tftp", p
)) {
2687 tftp_export
= qemu_strdup(buf
);
2689 if (get_param_value(buf
, sizeof(buf
), "bootfile", p
)) {
2690 bootfile
= qemu_strdup(buf
);
2692 if (get_param_value(buf
, sizeof(buf
), "smb", p
)) {
2693 smb_export
= qemu_strdup(buf
);
2694 if (get_param_value(buf
, sizeof(buf
), "smbserver", p
)) {
2695 vsmbsrv
= qemu_strdup(buf
);
2700 config
= qemu_malloc(sizeof(*config
));
2701 if (!get_next_param_value(config
->str
, sizeof(config
->str
),
2705 config
->flags
= SLIRP_CFG_HOSTFWD
;
2706 config
->next
= slirp_configs
;
2707 slirp_configs
= config
;
2712 config
= qemu_malloc(sizeof(*config
));
2713 if (!get_next_param_value(config
->str
, sizeof(config
->str
),
2718 config
->next
= slirp_configs
;
2719 slirp_configs
= config
;
2723 vlan
->nb_host_devs
++;
2724 ret
= net_slirp_init(mon
, vlan
, device
, name
, restricted
, vnet
, vhost
,
2725 vhostname
, tftp_export
, bootfile
, vdhcp_start
,
2726 vnamesrv
, smb_export
, vsmbsrv
);
2729 qemu_free(vhostname
);
2730 qemu_free(tftp_export
);
2731 qemu_free(bootfile
);
2732 qemu_free(vdhcp_start
);
2733 qemu_free(vnamesrv
);
2734 qemu_free(smb_export
);
2736 } else if (!strcmp(device
, "channel")) {
2737 if (TAILQ_EMPTY(&slirp_stacks
)) {
2738 struct slirp_config_str
*config
;
2740 config
= qemu_malloc(sizeof(*config
));
2741 pstrcpy(config
->str
, sizeof(config
->str
), p
);
2742 config
->flags
= SLIRP_CFG_LEGACY
;
2743 config
->next
= slirp_configs
;
2744 slirp_configs
= config
;
2746 slirp_guestfwd(TAILQ_FIRST(&slirp_stacks
), mon
, p
, 1);
2752 if (!strcmp(device
, "tap")) {
2753 static const char * const tap_params
[] = {
2754 "vlan", "name", "ifname", NULL
2758 if (check_params(buf
, sizeof(buf
), tap_params
, p
) < 0) {
2759 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2763 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2764 config_error(mon
, "tap: no interface name\n");
2768 vlan
->nb_host_devs
++;
2769 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
2771 #elif defined (_AIX)
2773 if (!strcmp(device
, "tap")) {
2774 char ifname
[64], chkbuf
[64];
2775 char setup_script
[1024], down_script
[1024];
2778 vlan
->nb_host_devs
++;
2779 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2780 static const char * const fd_params
[] = {
2781 "vlan", "name", "fd", "sndbuf", NULL
2783 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2784 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2788 fd
= strtol(buf
, NULL
, 0);
2789 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2790 s
= net_tap_fd_init(vlan
, device
, name
, fd
, tap_probe_vnet_hdr(fd
));
2792 static const char * const tap_params
[] = {
2793 "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
2795 if (check_params(chkbuf
, sizeof(chkbuf
), tap_params
, p
) < 0) {
2796 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2800 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
2803 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
2804 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
2806 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
2807 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
2809 s
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
2812 if (get_param_value(buf
, sizeof(buf
), "sndbuf", p
)) {
2813 tap_set_sndbuf(s
, atoi(buf
), mon
);
2821 if (!strcmp(device
, "socket")) {
2823 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
2824 static const char * const fd_params
[] = {
2825 "vlan", "name", "fd", NULL
2828 if (check_params(chkbuf
, sizeof(chkbuf
), fd_params
, p
) < 0) {
2829 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2833 fd
= strtol(buf
, NULL
, 0);
2835 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
2837 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
2838 static const char * const listen_params
[] = {
2839 "vlan", "name", "listen", NULL
2841 if (check_params(chkbuf
, sizeof(chkbuf
), listen_params
, p
) < 0) {
2842 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2846 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
2847 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
2848 static const char * const connect_params
[] = {
2849 "vlan", "name", "connect", NULL
2851 if (check_params(chkbuf
, sizeof(chkbuf
), connect_params
, p
) < 0) {
2852 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2856 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
2857 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
2858 static const char * const mcast_params
[] = {
2859 "vlan", "name", "mcast", NULL
2861 if (check_params(chkbuf
, sizeof(chkbuf
), mcast_params
, p
) < 0) {
2862 config_error(mon
, "invalid parameter '%s' in '%s'\n", chkbuf
, p
);
2866 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
2868 config_error(mon
, "Unknown socket options: %s\n", p
);
2872 vlan
->nb_host_devs
++;
2875 if (!strcmp(device
, "vde")) {
2876 static const char * const vde_params
[] = {
2877 "vlan", "name", "sock", "port", "group", "mode", NULL
2879 char vde_sock
[1024], vde_group
[512];
2880 int vde_port
, vde_mode
;
2882 if (check_params(buf
, sizeof(buf
), vde_params
, p
) < 0) {
2883 config_error(mon
, "invalid parameter '%s' in '%s'\n", buf
, p
);
2887 vlan
->nb_host_devs
++;
2888 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
2891 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
2892 vde_port
= strtol(buf
, NULL
, 10);
2896 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
2897 vde_group
[0] = '\0';
2899 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
2900 vde_mode
= strtol(buf
, NULL
, 8);
2904 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
2907 if (!strcmp(device
, "dump")) {
2910 if (get_param_value(buf
, sizeof(buf
), "len", p
) > 0) {
2911 len
= strtol(buf
, NULL
, 0);
2913 if (!get_param_value(buf
, sizeof(buf
), "file", p
)) {
2914 snprintf(buf
, sizeof(buf
), "qemu-vlan%d.pcap", vlan_id
);
2916 ret
= net_dump_init(mon
, vlan
, device
, name
, buf
, len
);
2918 config_error(mon
, "Unknown network device: %s\n", device
);
2923 config_error(mon
, "Could not initialize device '%s'\n", device
);
2930 void net_client_uninit(NICInfo
*nd
)
2932 nd
->vlan
->nb_guest_devs
--;
2935 free((void *)nd
->model
);
2938 static int net_host_check_device(const char *device
)
2941 const char *valid_param_list
[] = { "tap", "socket", "dump"
2949 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2950 if (!strncmp(valid_param_list
[i
], device
,
2951 strlen(valid_param_list
[i
])))
2958 void net_host_device_add(Monitor
*mon
, const char *device
, const char *opts
)
2960 if (!net_host_check_device(device
)) {
2961 monitor_printf(mon
, "invalid host network device %s\n", device
);
2964 if (net_client_init(mon
, device
, opts
? opts
: "") < 0) {
2965 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2969 void net_host_device_remove(Monitor
*mon
, int vlan_id
, const char *device
)
2971 VLANClientState
*vc
;
2973 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
2977 if (!net_host_check_device(vc
->model
)) {
2978 monitor_printf(mon
, "invalid host network device %s\n", device
);
2981 qemu_del_vlan_client(vc
);
2984 int net_client_parse(const char *str
)
2992 while (*p
!= '\0' && *p
!= ',') {
2993 if ((q
- device
) < sizeof(device
) - 1)
3001 return net_client_init(NULL
, device
, p
);
3004 void net_set_boot_mask(int net_boot_mask
)
3008 /* Only the first four NICs may be bootable */
3009 net_boot_mask
= net_boot_mask
& 0xF;
3011 for (i
= 0; i
< nb_nics
; i
++) {
3012 if (net_boot_mask
& (1 << i
)) {
3013 nd_table
[i
].bootable
= 1;
3014 net_boot_mask
&= ~(1 << i
);
3018 if (net_boot_mask
) {
3019 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
3024 void do_info_network(Monitor
*mon
)
3027 VLANClientState
*vc
;
3029 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3030 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
3031 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3032 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
3036 int do_set_link(Monitor
*mon
, const char *name
, const char *up_or_down
)
3039 VLANClientState
*vc
= NULL
;
3041 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
3042 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3043 if (strcmp(vc
->name
, name
) == 0)
3048 monitor_printf(mon
, "could not find network device '%s'", name
);
3052 if (strcmp(up_or_down
, "up") == 0)
3054 else if (strcmp(up_or_down
, "down") == 0)
3057 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
3058 "valid\n", up_or_down
);
3060 if (vc
->link_status_changed
)
3061 vc
->link_status_changed(vc
);
3066 void net_cleanup(void)
3070 /* close network clients */
3071 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3072 VLANClientState
*vc
= vlan
->first_client
;
3075 VLANClientState
*next
= vc
->next
;
3077 qemu_del_vlan_client(vc
);
3084 void net_client_check(void)
3088 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3089 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
3091 if (vlan
->nb_guest_devs
== 0)
3092 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
3093 if (vlan
->nb_host_devs
== 0)
3095 "Warning: vlan %d is not connected to host network\n",