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 CONFIG_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>
104 // FIXME: #include "qemu-kvm.h"
105 #include "qemu-common.h"
109 #include "qemu-timer.h"
110 #include "qemu-char.h"
111 #include "audio/audio.h"
112 #include "qemu_socket.h"
113 #include "qemu-log.h"
114 #include "qemu-config.h"
116 #include "slirp/libslirp.h"
118 static QTAILQ_HEAD(, VLANState
) vlans
;
120 /***********************************************************/
121 /* network device redirectors */
123 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
124 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
128 for(i
=0;i
<size
;i
+=16) {
132 fprintf(f
, "%08x ", i
);
135 fprintf(f
, " %02x", buf
[i
+j
]);
142 if (c
< ' ' || c
> '~')
151 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
158 offset
= strtol(p
, &last_char
, 0);
159 if (0 == errno
&& '\0' == *last_char
&&
160 offset
>= 0 && offset
<= 0xFFFFFF) {
161 macaddr
[3] = (offset
& 0xFF0000) >> 16;
162 macaddr
[4] = (offset
& 0xFF00) >> 8;
163 macaddr
[5] = offset
& 0xFF;
166 for(i
= 0; i
< 6; i
++) {
167 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
172 if (*p
!= ':' && *p
!= '-')
183 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
194 if (len
> buf_size
- 1)
203 int parse_host_src_port(struct sockaddr_in
*haddr
,
204 struct sockaddr_in
*saddr
,
205 const char *input_str
)
207 char *str
= strdup(input_str
);
208 char *host_str
= str
;
210 const char *src_str2
;
214 * Chop off any extra arguments at the end of the string which
215 * would start with a comma, then fill in the src port information
216 * if it was provided else use the "any address" and "any port".
218 if ((ptr
= strchr(str
,',')))
221 if ((src_str
= strchr(input_str
,'@'))) {
226 if (parse_host_port(haddr
, host_str
) < 0)
230 if (!src_str
|| *src_str
== '\0')
233 if (parse_host_port(saddr
, src_str2
) < 0)
244 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
252 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
254 saddr
->sin_family
= AF_INET
;
255 if (buf
[0] == '\0') {
256 saddr
->sin_addr
.s_addr
= 0;
258 if (qemu_isdigit(buf
[0])) {
259 if (!inet_aton(buf
, &saddr
->sin_addr
))
262 if ((he
= gethostbyname(buf
)) == NULL
)
264 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
267 port
= strtol(p
, (char **)&r
, 0);
270 saddr
->sin_port
= htons(port
);
274 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
276 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
277 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
279 macaddr
[0], macaddr
[1], macaddr
[2],
280 macaddr
[3], macaddr
[4], macaddr
[5]);
283 static char *assign_name(VLANClientState
*vc1
, const char *model
)
289 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
292 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
293 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
299 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
301 return qemu_strdup(buf
);
304 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
307 NetCanReceive
*can_receive
,
309 NetReceiveIOV
*receive_iov
,
315 vc
= qemu_mallocz(sizeof(VLANClientState
));
317 vc
->model
= qemu_strdup(model
);
319 vc
->name
= qemu_strdup(name
);
321 vc
->name
= assign_name(vc
, model
);
322 vc
->can_receive
= can_receive
;
323 vc
->receive
= receive
;
324 vc
->receive_iov
= receive_iov
;
325 vc
->cleanup
= cleanup
;
329 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
334 void qemu_del_vlan_client(VLANClientState
*vc
)
336 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
343 qemu_free(vc
->model
);
347 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
351 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
352 if (vc
->opaque
== opaque
) {
360 static VLANClientState
*
361 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
362 const char *client_str
)
367 vlan
= qemu_find_vlan(vlan_id
, 0);
369 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
373 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
374 if (!strcmp(vc
->name
, client_str
)) {
379 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
380 client_str
, vlan_id
);
386 int qemu_can_send_packet(VLANClientState
*sender
)
388 VLANState
*vlan
= sender
->vlan
;
391 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
396 /* no can_receive() handler, they can always receive */
397 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
405 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
, int raw
)
410 sender
->vlan
->delivering
= 1;
412 QTAILQ_FOREACH(vc
, &sender
->vlan
->clients
, next
) {
424 if (raw
&& vc
->receive_raw
) {
425 len
= vc
->receive_raw(vc
, buf
, size
);
427 len
= vc
->receive(vc
, buf
, size
);
430 ret
= (ret
>= 0) ? ret
: len
;
433 sender
->vlan
->delivering
= 0;
438 void qemu_purge_queued_packets(VLANClientState
*vc
)
440 VLANPacket
*packet
, *next
;
442 QTAILQ_FOREACH_SAFE(packet
, &vc
->vlan
->send_queue
, entry
, next
) {
443 if (packet
->sender
== vc
) {
444 QTAILQ_REMOVE(&vc
->vlan
->send_queue
, packet
, entry
);
450 void qemu_flush_queued_packets(VLANClientState
*vc
)
452 while (!QTAILQ_EMPTY(&vc
->vlan
->send_queue
)) {
456 packet
= QTAILQ_FIRST(&vc
->vlan
->send_queue
);
457 QTAILQ_REMOVE(&vc
->vlan
->send_queue
, packet
, entry
);
459 ret
= qemu_deliver_packet(packet
->sender
, packet
->data
,
460 packet
->size
, packet
->raw
);
461 if (ret
== 0 && packet
->sent_cb
!= NULL
) {
462 QTAILQ_INSERT_HEAD(&vc
->vlan
->send_queue
, packet
, entry
);
467 packet
->sent_cb(packet
->sender
, ret
);
473 static void qemu_enqueue_packet(VLANClientState
*sender
,
474 const uint8_t *buf
, int size
, int raw
,
475 NetPacketSent
*sent_cb
)
479 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
480 packet
->sender
= sender
;
483 packet
->sent_cb
= sent_cb
;
484 memcpy(packet
->data
, buf
, size
);
486 QTAILQ_INSERT_TAIL(&sender
->vlan
->send_queue
, packet
, entry
);
489 static ssize_t
qemu_send_packet_async2(VLANClientState
*sender
,
490 const uint8_t *buf
, int size
, int raw
,
491 NetPacketSent
*sent_cb
)
495 if (sender
->link_down
) {
500 printf("vlan %d send:\n", sender
->vlan
->id
);
501 hex_dump(stdout
, buf
, size
);
504 if (sender
->vlan
->delivering
) {
505 qemu_enqueue_packet(sender
, buf
, size
, raw
, NULL
);
509 ret
= qemu_deliver_packet(sender
, buf
, size
, raw
);
510 if (ret
== 0 && sent_cb
!= NULL
) {
511 qemu_enqueue_packet(sender
, buf
, size
, raw
, sent_cb
);
515 qemu_flush_queued_packets(sender
);
520 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
521 const uint8_t *buf
, int size
,
522 NetPacketSent
*sent_cb
)
524 return qemu_send_packet_async2(sender
, buf
, size
, 0, sent_cb
);
527 ssize_t
qemu_send_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
529 return qemu_send_packet_async2(sender
, buf
, size
, 0, NULL
);
532 ssize_t
qemu_send_packet_raw(VLANClientState
*sender
, const uint8_t *buf
, int size
)
534 return qemu_send_packet_async2(sender
, buf
, size
, 1, NULL
);
537 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
540 uint8_t buffer
[4096];
544 for (i
= 0; i
< iovcnt
; i
++) {
547 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
548 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
552 return vc
->receive(vc
, buffer
, offset
);
555 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
560 for (i
= 0; i
< iovcnt
; i
++)
561 offset
+= iov
[i
].iov_len
;
565 static int qemu_deliver_packet_iov(VLANClientState
*sender
,
566 const struct iovec
*iov
, int iovcnt
)
571 sender
->vlan
->delivering
= 1;
573 QTAILQ_FOREACH(vc
, &sender
->vlan
->clients
, next
) {
581 ret
= calc_iov_length(iov
, iovcnt
);
585 if (vc
->receive_iov
) {
586 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
588 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
591 ret
= (ret
>= 0) ? ret
: len
;
594 sender
->vlan
->delivering
= 0;
599 static ssize_t
qemu_enqueue_packet_iov(VLANClientState
*sender
,
600 const struct iovec
*iov
, int iovcnt
,
601 NetPacketSent
*sent_cb
)
607 max_len
= calc_iov_length(iov
, iovcnt
);
609 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
610 packet
->sender
= sender
;
611 packet
->sent_cb
= sent_cb
;
615 for (i
= 0; i
< iovcnt
; i
++) {
616 size_t len
= iov
[i
].iov_len
;
618 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
622 QTAILQ_INSERT_TAIL(&sender
->vlan
->send_queue
, packet
, entry
);
627 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
628 const struct iovec
*iov
, int iovcnt
,
629 NetPacketSent
*sent_cb
)
633 if (sender
->link_down
) {
634 return calc_iov_length(iov
, iovcnt
);
637 if (sender
->vlan
->delivering
) {
638 return qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, NULL
);
641 ret
= qemu_deliver_packet_iov(sender
, iov
, iovcnt
);
642 if (ret
== 0 && sent_cb
!= NULL
) {
643 qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, sent_cb
);
647 qemu_flush_queued_packets(sender
);
653 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
655 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
658 #if defined(CONFIG_SLIRP)
660 /* slirp network adapter */
662 #define SLIRP_CFG_HOSTFWD 1
663 #define SLIRP_CFG_LEGACY 2
665 struct slirp_config_str
{
666 struct slirp_config_str
*next
;
672 typedef struct SlirpState
{
673 QTAILQ_ENTRY(SlirpState
) entry
;
681 static struct slirp_config_str
*slirp_configs
;
682 const char *legacy_tftp_prefix
;
683 const char *legacy_bootp_filename
;
684 static QTAILQ_HEAD(slirp_stacks
, SlirpState
) slirp_stacks
=
685 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
687 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
689 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
693 static const char *legacy_smb_export
;
695 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
696 struct in_addr vserver_addr
);
697 static void slirp_smb_cleanup(SlirpState
*s
);
699 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
702 int slirp_can_output(void *opaque
)
704 SlirpState
*s
= opaque
;
706 return qemu_can_send_packet(s
->vc
);
709 void slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
711 SlirpState
*s
= opaque
;
714 printf("slirp output:\n");
715 hex_dump(stdout
, pkt
, pkt_len
);
717 qemu_send_packet(s
->vc
, pkt
, pkt_len
);
720 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
722 SlirpState
*s
= vc
->opaque
;
725 printf("slirp input:\n");
726 hex_dump(stdout
, buf
, size
);
728 slirp_input(s
->slirp
, buf
, size
);
732 static void net_slirp_cleanup(VLANClientState
*vc
)
734 SlirpState
*s
= vc
->opaque
;
736 slirp_cleanup(s
->slirp
);
737 slirp_smb_cleanup(s
);
738 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
742 static int net_slirp_init(VLANState
*vlan
, const char *model
,
743 const char *name
, int restricted
,
744 const char *vnetwork
, const char *vhost
,
745 const char *vhostname
, const char *tftp_export
,
746 const char *bootfile
, const char *vdhcp_start
,
747 const char *vnameserver
, const char *smb_export
,
748 const char *vsmbserver
)
750 /* default settings according to historic slirp */
751 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
752 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
753 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
754 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
755 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
757 struct in_addr smbsrv
= { .s_addr
= 0 };
764 struct slirp_config_str
*config
;
767 tftp_export
= legacy_tftp_prefix
;
770 bootfile
= legacy_bootp_filename
;
774 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
775 if (!inet_aton(vnetwork
, &net
)) {
778 addr
= ntohl(net
.s_addr
);
779 if (!(addr
& 0x80000000)) {
780 mask
.s_addr
= htonl(0xff000000); /* class A */
781 } else if ((addr
& 0xfff00000) == 0xac100000) {
782 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
783 } else if ((addr
& 0xc0000000) == 0x80000000) {
784 mask
.s_addr
= htonl(0xffff0000); /* class B */
785 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
786 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
787 } else if ((addr
& 0xffff0000) == 0xc6120000) {
788 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
789 } else if ((addr
& 0xe0000000) == 0xe0000000) {
790 mask
.s_addr
= htonl(0xffffff00); /* class C */
792 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
795 if (!inet_aton(buf
, &net
)) {
798 shift
= strtol(vnetwork
, &end
, 10);
800 if (!inet_aton(vnetwork
, &mask
)) {
803 } else if (shift
< 4 || shift
> 32) {
806 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
809 net
.s_addr
&= mask
.s_addr
;
810 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
811 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
812 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
815 if (vhost
&& !inet_aton(vhost
, &host
)) {
818 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
822 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
825 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
||
826 dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
830 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
833 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
||
834 dns
.s_addr
== host
.s_addr
) {
839 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
844 s
= qemu_mallocz(sizeof(SlirpState
));
845 s
->slirp
= slirp_init(restricted
, net
, mask
, host
, vhostname
,
846 tftp_export
, bootfile
, dhcp
, dns
, s
);
847 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
849 for (config
= slirp_configs
; config
; config
= config
->next
) {
850 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
851 if (slirp_hostfwd(s
, config
->str
,
852 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
855 if (slirp_guestfwd(s
, config
->str
,
856 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
862 smb_export
= legacy_smb_export
;
865 if (slirp_smb(s
, smb_export
, smbsrv
) < 0)
870 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
, NULL
,
871 net_slirp_cleanup
, s
);
872 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
873 "net=%s, restricted=%c", inet_ntoa(net
), restricted
? 'y' : 'n');
877 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *vlan
,
883 vc
= qemu_find_vlan_client_by_name(mon
, strtol(vlan
, NULL
, 0), stack
);
887 if (strcmp(vc
->model
, "user")) {
888 monitor_printf(mon
, "invalid device specified\n");
893 if (QTAILQ_EMPTY(&slirp_stacks
)) {
894 monitor_printf(mon
, "user mode network stack not in use\n");
897 return QTAILQ_FIRST(&slirp_stacks
);
901 void net_slirp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
903 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
906 const char *src_str
, *p
;
910 const char *arg1
= qdict_get_str(qdict
, "arg1");
911 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
912 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
915 s
= slirp_lookup(mon
, arg1
, arg2
);
918 s
= slirp_lookup(mon
, NULL
, NULL
);
925 if (!src_str
|| !src_str
[0])
929 get_str_sep(buf
, sizeof(buf
), &p
, ':');
931 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
933 } else if (!strcmp(buf
, "udp")) {
939 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
942 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
948 err
= slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks
)->slirp
, is_udp
,
949 host_addr
, host_port
);
951 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
952 err
? "removed" : "not found");
956 monitor_printf(mon
, "invalid format\n");
959 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
962 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
963 struct in_addr guest_addr
= { .s_addr
= 0 };
964 int host_port
, guest_port
;
971 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
974 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
976 } else if (!strcmp(buf
, "udp")) {
982 if (!legacy_format
) {
983 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
986 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
991 if (get_str_sep(buf
, sizeof(buf
), &p
, legacy_format
? ':' : '-') < 0) {
994 host_port
= strtol(buf
, &end
, 0);
995 if (*end
!= '\0' || host_port
< 1 || host_port
> 65535) {
999 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1002 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
1006 guest_port
= strtol(p
, &end
, 0);
1007 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
1011 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
1013 qemu_error("could not set up host forwarding rule '%s'\n",
1020 qemu_error("invalid host forwarding rule '%s'\n", redir_str
);
1024 void net_slirp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
1026 const char *redir_str
;
1028 const char *arg1
= qdict_get_str(qdict
, "arg1");
1029 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
1030 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
1033 s
= slirp_lookup(mon
, arg1
, arg2
);
1036 s
= slirp_lookup(mon
, NULL
, NULL
);
1040 slirp_hostfwd(s
, redir_str
, 0);
1045 int net_slirp_redir(const char *redir_str
)
1047 struct slirp_config_str
*config
;
1049 if (QTAILQ_EMPTY(&slirp_stacks
)) {
1050 config
= qemu_malloc(sizeof(*config
));
1051 pstrcpy(config
->str
, sizeof(config
->str
), redir_str
);
1052 config
->flags
= SLIRP_CFG_HOSTFWD
| SLIRP_CFG_LEGACY
;
1053 config
->next
= slirp_configs
;
1054 slirp_configs
= config
;
1058 return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks
), redir_str
, 1);
1063 /* automatic user mode samba server configuration */
1064 static void slirp_smb_cleanup(SlirpState
*s
)
1068 if (s
->smb_dir
[0] != '\0') {
1069 snprintf(cmd
, sizeof(cmd
), "rm -rf %s", s
->smb_dir
);
1071 s
->smb_dir
[0] = '\0';
1075 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
1076 struct in_addr vserver_addr
)
1078 static int instance
;
1080 char smb_cmdline
[128];
1083 snprintf(s
->smb_dir
, sizeof(s
->smb_dir
), "/tmp/qemu-smb.%ld-%d",
1084 (long)getpid(), instance
++);
1085 if (mkdir(s
->smb_dir
, 0700) < 0) {
1086 qemu_error("could not create samba server dir '%s'\n", s
->smb_dir
);
1089 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", s
->smb_dir
, "smb.conf");
1091 f
= fopen(smb_conf
, "w");
1093 slirp_smb_cleanup(s
);
1094 qemu_error("could not create samba server configuration file '%s'\n",
1102 "socket address=127.0.0.1\n"
1103 "pid directory=%s\n"
1104 "lock directory=%s\n"
1105 "log file=%s/log.smbd\n"
1106 "smb passwd file=%s/smbpasswd\n"
1107 "security = share\n"
1121 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1122 SMBD_COMMAND
, smb_conf
);
1124 if (slirp_add_exec(s
->slirp
, 0, smb_cmdline
, &vserver_addr
, 139) < 0) {
1125 slirp_smb_cleanup(s
);
1126 qemu_error("conflicting/invalid smbserver address\n");
1132 /* automatic user mode samba server configuration (legacy interface) */
1133 int net_slirp_smb(const char *exported_dir
)
1135 struct in_addr vserver_addr
= { .s_addr
= 0 };
1137 if (legacy_smb_export
) {
1138 fprintf(stderr
, "-smb given twice\n");
1141 legacy_smb_export
= exported_dir
;
1142 if (!QTAILQ_EMPTY(&slirp_stacks
)) {
1143 return slirp_smb(QTAILQ_FIRST(&slirp_stacks
), exported_dir
,
1149 #endif /* !defined(_WIN32) */
1152 CharDriverState
*hd
;
1153 struct in_addr server
;
1158 static int guestfwd_can_read(void *opaque
)
1160 struct GuestFwd
*fwd
= opaque
;
1161 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1164 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1166 struct GuestFwd
*fwd
= opaque
;
1167 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1170 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
1173 struct in_addr server
= { .s_addr
= 0 };
1174 struct GuestFwd
*fwd
;
1181 if (legacy_format
) {
1182 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1186 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1189 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1192 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1195 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1198 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1202 port
= strtol(buf
, &end
, 10);
1203 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1207 fwd
= qemu_malloc(sizeof(struct GuestFwd
));
1208 snprintf(buf
, sizeof(buf
), "guestfwd.tcp:%d", port
);
1209 fwd
->hd
= qemu_chr_open(buf
, p
, NULL
);
1211 qemu_error("could not open guest forwarding device '%s'\n", buf
);
1216 if (slirp_add_exec(s
->slirp
, 3, fwd
->hd
, &server
, port
) < 0) {
1217 qemu_error("conflicting/invalid host:port in guest forwarding "
1218 "rule '%s'\n", config_str
);
1222 fwd
->server
= server
;
1224 fwd
->slirp
= s
->slirp
;
1226 qemu_chr_add_handlers(fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1231 qemu_error("invalid guest forwarding rule '%s'\n", config_str
);
1235 void do_info_usernet(Monitor
*mon
)
1239 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1240 monitor_printf(mon
, "VLAN %d (%s):\n", s
->vc
->vlan
->id
, s
->vc
->name
);
1241 slirp_connection_info(s
->slirp
, mon
);
1245 #endif /* CONFIG_SLIRP */
1249 int tap_has_vnet_hdr(void *opaque
)
1254 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1258 int tap_has_ufo(void *opaque
)
1263 #else /* !defined(_WIN32) */
1265 /* Maximum GSO packet size (64k) plus plenty of room for
1266 * the ethernet and virtio_net headers
1268 #define TAP_BUFSIZE (4096 + 65536)
1271 #include <linux/virtio_net.h>
1274 typedef struct TAPState
{
1275 VLANClientState
*vc
;
1277 char down_script
[1024];
1278 char down_script_arg
[128];
1279 uint8_t buf
[TAP_BUFSIZE
];
1280 unsigned int read_poll
: 1;
1281 unsigned int write_poll
: 1;
1282 unsigned int has_vnet_hdr
: 1;
1283 unsigned int using_vnet_hdr
: 1;
1284 unsigned int has_ufo
: 1;
1287 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
1289 static int tap_can_send(void *opaque
);
1290 static void tap_send(void *opaque
);
1291 static void tap_writable(void *opaque
);
1293 static void tap_update_fd_handler(TAPState
*s
)
1295 qemu_set_fd_handler2(s
->fd
,
1296 s
->read_poll
? tap_can_send
: NULL
,
1297 s
->read_poll
? tap_send
: NULL
,
1298 s
->write_poll
? tap_writable
: NULL
,
1302 static void tap_read_poll(TAPState
*s
, int enable
)
1304 s
->read_poll
= !!enable
;
1305 tap_update_fd_handler(s
);
1308 static void tap_write_poll(TAPState
*s
, int enable
)
1310 s
->write_poll
= !!enable
;
1311 tap_update_fd_handler(s
);
1314 static void tap_writable(void *opaque
)
1316 TAPState
*s
= opaque
;
1318 tap_write_poll(s
, 0);
1320 qemu_flush_queued_packets(s
->vc
);
1323 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
1326 TAPState
*s
= vc
->opaque
;
1330 len
= writev(s
->fd
, iov
, iovcnt
);
1331 } while (len
== -1 && errno
== EINTR
);
1333 if (len
== -1 && errno
== EAGAIN
) {
1334 tap_write_poll(s
, 1);
1341 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1343 struct iovec iov
[2];
1347 TAPState
*s
= vc
->opaque
;
1348 struct virtio_net_hdr hdr
= { 0, };
1350 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
1351 iov
[i
].iov_base
= &hdr
;
1352 iov
[i
].iov_len
= sizeof(hdr
);
1357 iov
[i
].iov_base
= (char *) buf
;
1358 iov
[i
].iov_len
= size
;
1361 return tap_receive_iov(vc
, iov
, i
);
1364 static ssize_t
tap_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1366 struct iovec iov
[2];
1370 TAPState
*s
= vc
->opaque
;
1371 struct virtio_net_hdr hdr
= { 0, };
1373 if (s
->has_vnet_hdr
&& s
->using_vnet_hdr
) {
1374 iov
[i
].iov_base
= &hdr
;
1375 iov
[i
].iov_len
= sizeof(hdr
);
1380 iov
[i
].iov_base
= (char *) buf
;
1381 iov
[i
].iov_len
= size
;
1384 return tap_receive_iov(vc
, iov
, i
);
1387 static int tap_can_send(void *opaque
)
1389 TAPState
*s
= opaque
;
1391 return qemu_can_send_packet(s
->vc
);
1395 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1400 sbuf
.maxlen
= maxlen
;
1401 sbuf
.buf
= (char *)buf
;
1403 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1406 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1408 return read(tapfd
, buf
, maxlen
);
1412 static void tap_send_completed(VLANClientState
*vc
, ssize_t len
)
1414 TAPState
*s
= vc
->opaque
;
1415 tap_read_poll(s
, 1);
1418 static void tap_send(void *opaque
)
1420 TAPState
*s
= opaque
;
1424 uint8_t *buf
= s
->buf
;
1426 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
1432 if (s
->has_vnet_hdr
&& !s
->using_vnet_hdr
) {
1433 buf
+= sizeof(struct virtio_net_hdr
);
1434 size
-= sizeof(struct virtio_net_hdr
);
1438 size
= qemu_send_packet_async(s
->vc
, buf
, size
, tap_send_completed
);
1440 tap_read_poll(s
, 0);
1446 /* sndbuf should be set to a value lower than the tx queue
1447 * capacity of any destination network interface.
1448 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
1449 * a good default, given a 1500 byte MTU.
1451 #define TAP_DEFAULT_SNDBUF 1024*1024
1453 static int tap_set_sndbuf(TAPState
*s
, QemuOpts
*opts
)
1457 sndbuf
= qemu_opt_get_size(opts
, "sndbuf", TAP_DEFAULT_SNDBUF
);
1462 if (ioctl(s
->fd
, TUNSETSNDBUF
, &sndbuf
) == -1 && qemu_opt_get(opts
, "sndbuf")) {
1463 qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno
));
1469 static int tap_set_sndbuf(TAPState
*s
, QemuOpts
*opts
)
1473 #endif /* TUNSETSNDBUF */
1475 int tap_has_vnet_hdr(void *opaque
)
1477 VLANClientState
*vc
= opaque
;
1478 TAPState
*s
= vc
->opaque
;
1480 if (vc
->receive
!= tap_receive
)
1483 return s
? s
->has_vnet_hdr
: 0;
1486 void tap_using_vnet_hdr(void *opaque
, int using_vnet_hdr
)
1488 VLANClientState
*vc
= opaque
;
1489 TAPState
*s
= vc
->opaque
;
1491 if (vc
->receive
!= tap_receive
)
1494 if (!s
|| !s
->has_vnet_hdr
)
1497 s
->using_vnet_hdr
= using_vnet_hdr
!= 0;
1500 static int tap_probe_vnet_hdr(int fd
)
1502 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1505 if (ioctl(fd
, TUNGETIFF
, &ifr
) != 0) {
1506 fprintf(stderr
, "TUNGETIFF ioctl() failed: %s\n", strerror(errno
));
1510 return ifr
.ifr_flags
& IFF_VNET_HDR
;
1516 int tap_has_ufo(void *opaque
)
1518 VLANClientState
*vc
= opaque
;
1519 TAPState
*s
= vc
->opaque
;
1521 return s
? s
->has_ufo
: 0;
1524 #ifdef TUNSETOFFLOAD
1527 #define TUN_F_UFO 0x10
1530 static void tap_set_offload(VLANClientState
*vc
, int csum
, int tso4
, int tso6
,
1533 TAPState
*s
= vc
->opaque
;
1534 unsigned int offload
= 0;
1537 offload
|= TUN_F_CSUM
;
1539 offload
|= TUN_F_TSO4
;
1541 offload
|= TUN_F_TSO6
;
1542 if ((tso4
|| tso6
) && ecn
)
1543 offload
|= TUN_F_TSO_ECN
;
1545 offload
|= TUN_F_UFO
;
1548 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0) {
1549 /* Try without UFO */
1550 offload
&= ~TUN_F_UFO
;
1551 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) != 0) {
1552 fprintf(stderr
, "TUNSETOFFLOAD ioctl() failed: %s\n",
1557 #endif /* TUNSETOFFLOAD */
1559 static void tap_cleanup(VLANClientState
*vc
)
1561 TAPState
*s
= vc
->opaque
;
1563 qemu_purge_queued_packets(vc
);
1565 if (s
->down_script
[0])
1566 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1568 tap_read_poll(s
, 0);
1569 tap_write_poll(s
, 0);
1576 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1583 unsigned int offload
;
1585 s
= qemu_mallocz(sizeof(TAPState
));
1587 s
->has_vnet_hdr
= vnet_hdr
!= 0;
1588 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1589 tap_receive_iov
, tap_cleanup
, s
);
1590 s
->vc
->receive_raw
= tap_receive_raw
;
1591 #ifdef TUNSETOFFLOAD
1592 s
->vc
->set_offload
= tap_set_offload
;
1595 /* Check if tap supports UFO */
1596 offload
= TUN_F_CSUM
| TUN_F_UFO
;
1597 if (ioctl(s
->fd
, TUNSETOFFLOAD
, offload
) == 0)
1600 tap_set_offload(s
->vc
, 0, 0, 0, 0, 0);
1602 tap_read_poll(s
, 1);
1603 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1607 #if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__)
1608 static int tap_open(char *ifname
, int ifname_size
)
1614 TFR(fd
= open("/dev/tap", O_RDWR
));
1616 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1621 dev
= devname(s
.st_rdev
, S_IFCHR
);
1622 pstrcpy(ifname
, ifname_size
, dev
);
1624 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1627 #elif defined(__sun__)
1628 #define TUNNEWPPA (('T'<<16) | 0x0001)
1630 * Allocate TAP device, returns opened fd.
1631 * Stores dev name in the first arg(must be large enough).
1633 static int tap_alloc(char *dev
, size_t dev_size
)
1635 int tap_fd
, if_fd
, ppa
= -1;
1636 static int ip_fd
= 0;
1639 static int arp_fd
= 0;
1640 int ip_muxid
, arp_muxid
;
1641 struct strioctl strioc_if
, strioc_ppa
;
1642 int link_type
= I_PLINK
;;
1644 char actual_name
[32] = "";
1646 memset(&ifr
, 0x0, sizeof(ifr
));
1650 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1654 /* Check if IP device was opened */
1658 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1660 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1664 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1666 syslog(LOG_ERR
, "Can't open /dev/tap");
1670 /* Assign a new PPA and get its unit number. */
1671 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1672 strioc_ppa
.ic_timout
= 0;
1673 strioc_ppa
.ic_len
= sizeof(ppa
);
1674 strioc_ppa
.ic_dp
= (char *)&ppa
;
1675 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1676 syslog (LOG_ERR
, "Can't assign new interface");
1678 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1680 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1683 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1684 syslog(LOG_ERR
, "Can't push IP module");
1688 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1689 syslog(LOG_ERR
, "Can't get flags\n");
1691 snprintf (actual_name
, 32, "tap%d", ppa
);
1692 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1695 /* Assign ppa according to the unit number returned by tun device */
1697 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1698 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1699 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1700 syslog (LOG_ERR
, "Can't get flags\n");
1701 /* Push arp module to if_fd */
1702 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1703 syslog (LOG_ERR
, "Can't push ARP module (2)");
1705 /* Push arp module to ip_fd */
1706 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1707 syslog (LOG_ERR
, "I_POP failed\n");
1708 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1709 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1711 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1713 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1715 /* Set ifname to arp */
1716 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1717 strioc_if
.ic_timout
= 0;
1718 strioc_if
.ic_len
= sizeof(ifr
);
1719 strioc_if
.ic_dp
= (char *)&ifr
;
1720 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1721 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1724 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1725 syslog(LOG_ERR
, "Can't link TAP device to IP");
1729 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1730 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1734 memset(&ifr
, 0x0, sizeof(ifr
));
1735 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1736 ifr
.lifr_ip_muxid
= ip_muxid
;
1737 ifr
.lifr_arp_muxid
= arp_muxid
;
1739 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1741 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1742 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1743 syslog (LOG_ERR
, "Can't set multiplexor id");
1746 snprintf(dev
, dev_size
, "tap%d", ppa
);
1750 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1754 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1755 fprintf(stderr
, "Cannot allocate TAP device\n");
1758 pstrcpy(ifname
, ifname_size
, dev
);
1759 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1762 #elif defined (_AIX)
1763 static int tap_open(char *ifname
, int ifname_size
)
1765 fprintf (stderr
, "no tap on AIX\n");
1769 static int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
)
1774 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1776 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1779 memset(&ifr
, 0, sizeof(ifr
));
1780 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1782 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1784 unsigned int features
;
1786 if (ioctl(fd
, TUNGETFEATURES
, &features
) == 0 &&
1787 features
& IFF_VNET_HDR
) {
1789 ifr
.ifr_flags
|= IFF_VNET_HDR
;
1794 if (ifname
[0] != '\0')
1795 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1797 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1798 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1800 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1804 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1805 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1810 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1812 sigset_t oldmask
, mask
;
1818 sigaddset(&mask
, SIGCHLD
);
1819 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1821 /* try to launch network script */
1824 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1826 for (i
= 0; i
< open_max
; i
++) {
1827 if (i
!= STDIN_FILENO
&&
1828 i
!= STDOUT_FILENO
&&
1829 i
!= STDERR_FILENO
&&
1835 *parg
++ = (char *)setup_script
;
1836 *parg
++ = (char *)ifname
;
1838 execv(setup_script
, args
);
1840 } else if (pid
> 0) {
1841 while (waitpid(pid
, &status
, 0) != pid
) {
1844 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1846 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1850 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1854 static TAPState
*net_tap_init(VLANState
*vlan
, const char *model
,
1855 const char *name
, const char *ifname1
,
1856 const char *setup_script
, const char *down_script
)
1863 if (ifname1
!= NULL
)
1864 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1868 TFR(fd
= tap_open(ifname
, sizeof(ifname
), &vnet_hdr
));
1872 if (!setup_script
|| !strcmp(setup_script
, "no"))
1874 if (setup_script
[0] != '\0' &&
1875 launch_script(setup_script
, ifname
, fd
)) {
1878 s
= net_tap_fd_init(vlan
, model
, name
, fd
, vnet_hdr
);
1879 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1880 "ifname=%s,script=%s,downscript=%s",
1881 ifname
, setup_script
, down_script
);
1882 if (down_script
&& strcmp(down_script
, "no")) {
1883 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1884 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1889 #endif /* !_WIN32 */
1891 #if defined(CONFIG_VDE)
1892 typedef struct VDEState
{
1893 VLANClientState
*vc
;
1897 static void vde_to_qemu(void *opaque
)
1899 VDEState
*s
= opaque
;
1903 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1905 qemu_send_packet(s
->vc
, buf
, size
);
1909 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1911 VDEState
*s
= vc
->opaque
;
1915 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1916 } while (ret
< 0 && errno
== EINTR
);
1921 static void vde_cleanup(VLANClientState
*vc
)
1923 VDEState
*s
= vc
->opaque
;
1924 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1929 static int net_vde_init(VLANState
*vlan
, const char *model
,
1930 const char *name
, const char *sock
,
1931 int port
, const char *group
, int mode
)
1934 char *init_group
= (char *)group
;
1935 char *init_sock
= (char *)sock
;
1937 struct vde_open_args args
= {
1939 .group
= init_group
,
1943 s
= qemu_mallocz(sizeof(VDEState
));
1944 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1949 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_receive
,
1950 NULL
, vde_cleanup
, s
);
1951 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1952 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1953 sock
, vde_datafd(s
->vde
));
1958 /* network connection */
1959 typedef struct NetSocketState
{
1960 VLANClientState
*vc
;
1962 int state
; /* 0 = getting length, 1 = getting data */
1964 unsigned int packet_len
;
1966 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1969 typedef struct NetSocketListenState
{
1974 } NetSocketListenState
;
1976 /* XXX: we consider we can send the whole packet without blocking */
1977 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1979 NetSocketState
*s
= vc
->opaque
;
1983 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1984 return send_all(s
->fd
, buf
, size
);
1987 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1989 NetSocketState
*s
= vc
->opaque
;
1991 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1992 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1995 static void net_socket_send(void *opaque
)
1997 NetSocketState
*s
= opaque
;
2003 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
2005 err
= socket_error();
2006 if (err
!= EWOULDBLOCK
)
2008 } else if (size
== 0) {
2009 /* end of connection */
2011 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2017 /* reassemble a packet from the network */
2023 memcpy(s
->buf
+ s
->index
, buf
, l
);
2027 if (s
->index
== 4) {
2029 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
2035 l
= s
->packet_len
- s
->index
;
2038 if (s
->index
+ l
<= sizeof(s
->buf
)) {
2039 memcpy(s
->buf
+ s
->index
, buf
, l
);
2041 fprintf(stderr
, "serious error: oversized packet received,"
2042 "connection terminated.\n");
2050 if (s
->index
>= s
->packet_len
) {
2051 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
2060 static void net_socket_send_dgram(void *opaque
)
2062 NetSocketState
*s
= opaque
;
2065 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
2069 /* end of connection */
2070 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2073 qemu_send_packet(s
->vc
, s
->buf
, size
);
2076 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
2081 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
2082 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2083 inet_ntoa(mcastaddr
->sin_addr
),
2084 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
2088 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
2090 perror("socket(PF_INET, SOCK_DGRAM)");
2095 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
2096 (const char *)&val
, sizeof(val
));
2098 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2102 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
2108 /* Add host to multicast group */
2109 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
2110 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
2112 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
2113 (const char *)&imr
, sizeof(struct ip_mreq
));
2115 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2119 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2121 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
2122 (const char *)&val
, sizeof(val
));
2124 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2128 socket_set_nonblock(fd
);
2136 static void net_socket_cleanup(VLANClientState
*vc
)
2138 NetSocketState
*s
= vc
->opaque
;
2139 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
2144 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
2147 int fd
, int is_connected
)
2149 struct sockaddr_in saddr
;
2151 socklen_t saddr_len
;
2154 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2155 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2156 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2160 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
2162 if (saddr
.sin_addr
.s_addr
==0) {
2163 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2167 /* clone dgram socket */
2168 newfd
= net_socket_mcast_create(&saddr
);
2170 /* error already reported by net_socket_mcast_create() */
2174 /* clone newfd to fd, close newfd */
2179 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2180 fd
, strerror(errno
));
2185 s
= qemu_mallocz(sizeof(NetSocketState
));
2188 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
2189 NULL
, net_socket_cleanup
, s
);
2190 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
2192 /* mcast: save bound address as dst */
2193 if (is_connected
) s
->dgram_dst
=saddr
;
2195 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2196 "socket: fd=%d (%s mcast=%s:%d)",
2197 fd
, is_connected
? "cloned" : "",
2198 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2202 static void net_socket_connect(void *opaque
)
2204 NetSocketState
*s
= opaque
;
2205 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2208 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
2211 int fd
, int is_connected
)
2214 s
= qemu_mallocz(sizeof(NetSocketState
));
2216 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
2217 NULL
, net_socket_cleanup
, s
);
2218 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2219 "socket: fd=%d", fd
);
2221 net_socket_connect(s
);
2223 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2228 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
2229 const char *model
, const char *name
,
2230 int fd
, int is_connected
)
2232 int so_type
= -1, optlen
=sizeof(so_type
);
2234 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
2235 (socklen_t
*)&optlen
)< 0) {
2236 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2241 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
2243 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2245 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2246 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2247 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2252 static void net_socket_accept(void *opaque
)
2254 NetSocketListenState
*s
= opaque
;
2256 struct sockaddr_in saddr
;
2261 len
= sizeof(saddr
);
2262 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2263 if (fd
< 0 && errno
!= EINTR
) {
2265 } else if (fd
>= 0) {
2269 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
2273 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2274 "socket: connection from %s:%d",
2275 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2279 static int net_socket_listen_init(VLANState
*vlan
,
2282 const char *host_str
)
2284 NetSocketListenState
*s
;
2286 struct sockaddr_in saddr
;
2288 if (parse_host_port(&saddr
, host_str
) < 0)
2291 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2293 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2298 socket_set_nonblock(fd
);
2300 /* allow fast reuse */
2302 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2304 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2309 ret
= listen(fd
, 0);
2315 s
->model
= qemu_strdup(model
);
2316 s
->name
= name
? qemu_strdup(name
) : NULL
;
2318 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2322 static int net_socket_connect_init(VLANState
*vlan
,
2325 const char *host_str
)
2328 int fd
, connected
, ret
, err
;
2329 struct sockaddr_in saddr
;
2331 if (parse_host_port(&saddr
, host_str
) < 0)
2334 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2339 socket_set_nonblock(fd
);
2343 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2345 err
= socket_error();
2346 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2347 } else if (err
== EINPROGRESS
) {
2350 } else if (err
== WSAEALREADY
) {
2363 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
2366 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2367 "socket: connect to %s:%d",
2368 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2372 static int net_socket_mcast_init(VLANState
*vlan
,
2375 const char *host_str
)
2379 struct sockaddr_in saddr
;
2381 if (parse_host_port(&saddr
, host_str
) < 0)
2385 fd
= net_socket_mcast_create(&saddr
);
2389 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
2393 s
->dgram_dst
= saddr
;
2395 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2396 "socket: mcast=%s:%d",
2397 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2402 typedef struct DumpState
{
2403 VLANClientState
*pcap_vc
;
2408 #define PCAP_MAGIC 0xa1b2c3d4
2410 struct pcap_file_hdr
{
2412 uint16_t version_major
;
2413 uint16_t version_minor
;
2420 struct pcap_sf_pkthdr
{
2429 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
2431 DumpState
*s
= vc
->opaque
;
2432 struct pcap_sf_pkthdr hdr
;
2436 /* Early return in case of previous error. */
2441 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, get_ticks_per_sec());
2442 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
2444 hdr
.ts
.tv_sec
= ts
/ 1000000;
2445 hdr
.ts
.tv_usec
= ts
% 1000000;
2446 hdr
.caplen
= caplen
;
2448 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
2449 write(s
->fd
, buf
, caplen
) != caplen
) {
2450 qemu_log("-net dump write error - stop dump\n");
2458 static void net_dump_cleanup(VLANClientState
*vc
)
2460 DumpState
*s
= vc
->opaque
;
2466 static int net_dump_init(VLANState
*vlan
, const char *device
,
2467 const char *name
, const char *filename
, int len
)
2469 struct pcap_file_hdr hdr
;
2472 s
= qemu_malloc(sizeof(DumpState
));
2474 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
2476 qemu_error("-net dump: can't open %s\n", filename
);
2480 s
->pcap_caplen
= len
;
2482 hdr
.magic
= PCAP_MAGIC
;
2483 hdr
.version_major
= 2;
2484 hdr
.version_minor
= 4;
2487 hdr
.snaplen
= s
->pcap_caplen
;
2490 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2491 qemu_error("-net dump write error: %s\n", strerror(errno
));
2497 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
2498 net_dump_cleanup
, s
);
2499 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2500 "dump to %s (len=%d)", filename
, len
);
2504 /* find or alloc a new VLAN */
2505 VLANState
*qemu_find_vlan(int id
, int allocate
)
2509 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2510 if (vlan
->id
== id
) {
2519 vlan
= qemu_mallocz(sizeof(VLANState
));
2521 QTAILQ_INIT(&vlan
->clients
);
2522 QTAILQ_INIT(&vlan
->send_queue
);
2524 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
2529 static int nic_get_free_idx(void)
2533 for (index
= 0; index
< MAX_NICS
; index
++)
2534 if (!nd_table
[index
].used
)
2539 int qemu_show_nic_models(const char *arg
, const char *const *models
)
2543 if (!arg
|| strcmp(arg
, "?"))
2546 fprintf(stderr
, "qemu: Supported NIC models: ");
2547 for (i
= 0 ; models
[i
]; i
++)
2548 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2552 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2554 const char *models
[2];
2559 if (qemu_show_nic_models(nd
->model
, models
))
2561 if (qemu_find_nic_model(nd
, models
, model
) < 0)
2565 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
2566 const char *default_model
)
2571 nd
->model
= qemu_strdup(default_model
);
2573 for (i
= 0 ; models
[i
]; i
++) {
2574 if (strcmp(nd
->model
, models
[i
]) == 0)
2578 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
2582 static int net_handle_fd_param(Monitor
*mon
, const char *param
)
2584 if (!qemu_isdigit(param
[0])) {
2587 fd
= monitor_get_fd(mon
, param
);
2589 qemu_error("No file descriptor named %s found", param
);
2595 return strtol(param
, NULL
, 0);
2599 static int net_init_nic(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2604 idx
= nic_get_free_idx();
2605 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2606 qemu_error("Too Many NICs\n");
2610 nd
= &nd_table
[idx
];
2612 memset(nd
, 0, sizeof(*nd
));
2614 nd
->vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2617 nd
->name
= qemu_strdup(name
);
2619 if (qemu_opt_get(opts
, "model")) {
2620 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
2622 if (qemu_opt_get(opts
, "addr")) {
2623 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
2626 nd
->macaddr
[0] = 0x52;
2627 nd
->macaddr
[1] = 0x54;
2628 nd
->macaddr
[2] = 0x00;
2629 nd
->macaddr
[3] = 0x12;
2630 nd
->macaddr
[4] = 0x34;
2631 nd
->macaddr
[5] = 0x56 + idx
;
2633 if (qemu_opt_get(opts
, "macaddr") &&
2634 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
2635 qemu_error("invalid syntax for ethernet address\n");
2639 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
2640 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
2641 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
2642 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
2647 nd
->vlan
->nb_guest_devs
++;
2653 #if defined(CONFIG_SLIRP)
2654 static int net_init_slirp_configs(const char *name
, const char *value
, void *opaque
)
2656 struct slirp_config_str
*config
;
2658 if (strcmp(name
, "hostfwd") != 0 && strcmp(name
, "guestfwd") != 0) {
2662 config
= qemu_mallocz(sizeof(*config
));
2664 pstrcpy(config
->str
, sizeof(config
->str
), value
);
2666 if (!strcmp(name
, "hostfwd")) {
2667 config
->flags
= SLIRP_CFG_HOSTFWD
;
2670 config
->next
= slirp_configs
;
2671 slirp_configs
= config
;
2676 static int net_init_slirp(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2679 struct slirp_config_str
*config
;
2681 const char *vhostname
;
2682 const char *vdhcp_start
;
2683 const char *vnamesrv
;
2684 const char *tftp_export
;
2685 const char *bootfile
;
2686 const char *smb_export
;
2687 const char *vsmbsrv
;
2692 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2694 vhost
= qemu_opt_get(opts
, "host");
2695 vhostname
= qemu_opt_get(opts
, "hostname");
2696 vdhcp_start
= qemu_opt_get(opts
, "dhcpstart");
2697 vnamesrv
= qemu_opt_get(opts
, "dns");
2698 tftp_export
= qemu_opt_get(opts
, "tftp");
2699 bootfile
= qemu_opt_get(opts
, "bootfile");
2700 smb_export
= qemu_opt_get(opts
, "smb");
2701 vsmbsrv
= qemu_opt_get(opts
, "smbserver");
2703 if (qemu_opt_get(opts
, "ip")) {
2704 const char *ip
= qemu_opt_get(opts
, "ip");
2705 int l
= strlen(ip
) + strlen("/24") + 1;
2707 vnet
= qemu_malloc(l
);
2709 /* emulate legacy ip= parameter */
2710 pstrcpy(vnet
, l
, ip
);
2711 pstrcat(vnet
, l
, "/24");
2714 if (qemu_opt_get(opts
, "net")) {
2718 vnet
= qemu_strdup(qemu_opt_get(opts
, "net"));
2721 if (qemu_opt_get(opts
, "restrict") &&
2722 qemu_opt_get(opts
, "restrict")[0] == 'y') {
2726 qemu_opt_foreach(opts
, net_init_slirp_configs
, NULL
, 0);
2728 ret
= net_slirp_init(vlan
, "user", name
, restricted
, vnet
, vhost
,
2729 vhostname
, tftp_export
, bootfile
, vdhcp_start
,
2730 vnamesrv
, smb_export
, vsmbsrv
);
2732 while (slirp_configs
) {
2733 config
= slirp_configs
;
2734 slirp_configs
= config
->next
;
2739 vlan
->nb_host_devs
++;
2746 #endif /* CONFIG_SLIRP */
2749 static int net_init_tap_win32(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2754 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2756 ifname
= qemu_opt_get(opts
, "ifname");
2759 qemu_error("tap: no interface name\n");
2763 if (tap_win32_init(vlan
, "tap", name
, ifname
) == -1) {
2767 vlan
->nb_host_devs
++;
2771 #elif !defined(_AIX)
2772 static int net_init_tap(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2777 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2779 if (qemu_opt_get(opts
, "fd")) {
2782 if (qemu_opt_get(opts
, "ifname") ||
2783 qemu_opt_get(opts
, "script") ||
2784 qemu_opt_get(opts
, "downscript")) {
2785 qemu_error("ifname=, script= and downscript= is invalid with fd=\n");
2789 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2794 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2796 s
= net_tap_fd_init(vlan
, "tap", name
, fd
, tap_probe_vnet_hdr(fd
));
2801 const char *ifname
, *script
, *downscript
;
2803 ifname
= qemu_opt_get(opts
, "ifname");
2804 script
= qemu_opt_get(opts
, "script");
2805 downscript
= qemu_opt_get(opts
, "downscript");
2808 script
= DEFAULT_NETWORK_SCRIPT
;
2811 downscript
= DEFAULT_NETWORK_DOWN_SCRIPT
;
2814 s
= net_tap_init(vlan
, "tap", name
, ifname
, script
, downscript
);
2821 if (tap_set_sndbuf(s
, opts
) < 0) {
2825 vlan
->nb_host_devs
++;
2831 static int net_init_socket(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2835 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2837 if (qemu_opt_get(opts
, "fd")) {
2840 if (qemu_opt_get(opts
, "listen") ||
2841 qemu_opt_get(opts
, "connect") ||
2842 qemu_opt_get(opts
, "mcast")) {
2843 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
2847 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2852 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
2856 } else if (qemu_opt_get(opts
, "listen")) {
2859 if (qemu_opt_get(opts
, "fd") ||
2860 qemu_opt_get(opts
, "connect") ||
2861 qemu_opt_get(opts
, "mcast")) {
2862 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
2866 listen
= qemu_opt_get(opts
, "listen");
2868 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
2871 } else if (qemu_opt_get(opts
, "connect")) {
2872 const char *connect
;
2874 if (qemu_opt_get(opts
, "fd") ||
2875 qemu_opt_get(opts
, "listen") ||
2876 qemu_opt_get(opts
, "mcast")) {
2877 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
2881 connect
= qemu_opt_get(opts
, "connect");
2883 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
2886 } else if (qemu_opt_get(opts
, "mcast")) {
2889 if (qemu_opt_get(opts
, "fd") ||
2890 qemu_opt_get(opts
, "connect") ||
2891 qemu_opt_get(opts
, "listen")) {
2892 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
2896 mcast
= qemu_opt_get(opts
, "mcast");
2898 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
) == -1) {
2902 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
2906 vlan
->nb_host_devs
++;
2912 static int net_init_vde(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2919 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2921 sock
= qemu_opt_get(opts
, "sock");
2922 group
= qemu_opt_get(opts
, "group");
2924 port
= qemu_opt_get_number(opts
, "port", 0);
2925 mode
= qemu_opt_get_number(opts
, "mode", 0700);
2927 if (net_vde_init(vlan
, "vde", name
, sock
, port
, group
, mode
) == -1) {
2931 vlan
->nb_host_devs
++;
2937 static int net_init_dump(QemuOpts
*opts
, Monitor
*mon
, const char *name
)
2944 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2946 file
= qemu_opt_get(opts
, "file");
2948 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", vlan
->id
);
2952 len
= qemu_opt_get_size(opts
, "len", 65536);
2954 return net_dump_init(vlan
, "dump", name
, file
, len
);
2957 #define NET_COMMON_PARAMS_DESC \
2960 .type = QEMU_OPT_STRING, \
2961 .help = "net client type (nic, tap etc.)", \
2964 .type = QEMU_OPT_NUMBER, \
2965 .help = "vlan number", \
2968 .type = QEMU_OPT_STRING, \
2969 .help = "identifier for monitor commands", \
2972 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
2976 /* magic number, but compiler will warn if too small */
2977 #define NET_MAX_DESC 20
2981 net_client_init_func init
;
2982 QemuOptDesc desc
[NET_MAX_DESC
];
2983 } net_client_types
[] = {
2987 NET_COMMON_PARAMS_DESC
,
2988 { /* end of list */ }
2992 .init
= net_init_nic
,
2994 NET_COMMON_PARAMS_DESC
,
2997 .type
= QEMU_OPT_STRING
,
2998 .help
= "MAC address",
3001 .type
= QEMU_OPT_STRING
,
3002 .help
= "device model (e1000, rtl8139, virtio etc.)",
3005 .type
= QEMU_OPT_STRING
,
3006 .help
= "PCI device address",
3009 .type
= QEMU_OPT_NUMBER
,
3010 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
3012 { /* end of list */ }
3017 .init
= net_init_slirp
,
3019 NET_COMMON_PARAMS_DESC
,
3022 .type
= QEMU_OPT_STRING
,
3023 .help
= "client hostname reported by the builtin DHCP server",
3026 .type
= QEMU_OPT_STRING
,
3027 .help
= "isolate the guest from the host (y|yes|n|no)",
3030 .type
= QEMU_OPT_STRING
,
3031 .help
= "legacy parameter, use net= instead",
3034 .type
= QEMU_OPT_STRING
,
3035 .help
= "IP address and optional netmask",
3038 .type
= QEMU_OPT_STRING
,
3039 .help
= "guest-visible address of the host",
3042 .type
= QEMU_OPT_STRING
,
3043 .help
= "root directory of the built-in TFTP server",
3046 .type
= QEMU_OPT_STRING
,
3047 .help
= "BOOTP filename, for use with tftp=",
3049 .name
= "dhcpstart",
3050 .type
= QEMU_OPT_STRING
,
3051 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
3054 .type
= QEMU_OPT_STRING
,
3055 .help
= "guest-visible address of the virtual nameserver",
3058 .type
= QEMU_OPT_STRING
,
3059 .help
= "root directory of the built-in SMB server",
3061 .name
= "smbserver",
3062 .type
= QEMU_OPT_STRING
,
3063 .help
= "IP address of the built-in SMB server",
3066 .type
= QEMU_OPT_STRING
,
3067 .help
= "guest port number to forward incoming TCP or UDP connections",
3070 .type
= QEMU_OPT_STRING
,
3071 .help
= "IP address and port to forward guest TCP connections",
3073 { /* end of list */ }
3079 .init
= net_init_tap_win32
,
3081 NET_COMMON_PARAMS_DESC
,
3084 .type
= QEMU_OPT_STRING
,
3085 .help
= "interface name",
3087 { /* end of list */ }
3089 #elif !defined(_AIX)
3092 .init
= net_init_tap
,
3094 NET_COMMON_PARAMS_DESC
,
3097 .type
= QEMU_OPT_STRING
,
3098 .help
= "file descriptor of an already opened tap",
3101 .type
= QEMU_OPT_STRING
,
3102 .help
= "interface name",
3105 .type
= QEMU_OPT_STRING
,
3106 .help
= "script to initialize the interface",
3108 .name
= "downscript",
3109 .type
= QEMU_OPT_STRING
,
3110 .help
= "script to shut down the interface",
3114 .type
= QEMU_OPT_SIZE
,
3115 .help
= "send buffer limit"
3118 { /* end of list */ }
3123 .init
= net_init_socket
,
3125 NET_COMMON_PARAMS_DESC
,
3128 .type
= QEMU_OPT_STRING
,
3129 .help
= "file descriptor of an already opened socket",
3132 .type
= QEMU_OPT_STRING
,
3133 .help
= "port number, and optional hostname, to listen on",
3136 .type
= QEMU_OPT_STRING
,
3137 .help
= "port number, and optional hostname, to connect to",
3140 .type
= QEMU_OPT_STRING
,
3141 .help
= "UDP multicast address and port number",
3143 { /* end of list */ }
3148 .init
= net_init_vde
,
3150 NET_COMMON_PARAMS_DESC
,
3153 .type
= QEMU_OPT_STRING
,
3154 .help
= "socket path",
3157 .type
= QEMU_OPT_NUMBER
,
3158 .help
= "port number",
3161 .type
= QEMU_OPT_STRING
,
3162 .help
= "group owner of socket",
3165 .type
= QEMU_OPT_NUMBER
,
3166 .help
= "permissions for socket",
3168 { /* end of list */ }
3173 .init
= net_init_dump
,
3175 NET_COMMON_PARAMS_DESC
,
3178 .type
= QEMU_OPT_SIZE
,
3179 .help
= "per-packet size limit (64k default)",
3182 .type
= QEMU_OPT_STRING
,
3183 .help
= "dump file path (default is qemu-vlan0.pcap)",
3185 { /* end of list */ }
3188 { /* end of list */ }
3191 int net_client_init(Monitor
*mon
, QemuOpts
*opts
)
3197 type
= qemu_opt_get(opts
, "type");
3199 qemu_error("No type specified for -net\n");
3203 name
= qemu_opts_id(opts
);
3205 name
= qemu_opt_get(opts
, "name");
3208 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
3209 if (!strcmp(net_client_types
[i
].type
, type
)) {
3210 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
3214 if (net_client_types
[i
].init
) {
3215 return net_client_types
[i
].init(opts
, mon
, name
);
3222 qemu_error("Invalid -net type '%s'\n", type
);
3226 void net_client_uninit(NICInfo
*nd
)
3228 nd
->vlan
->nb_guest_devs
--;
3231 qemu_free(nd
->model
);
3232 qemu_free(nd
->name
);
3233 qemu_free(nd
->devaddr
);
3238 static int net_host_check_device(const char *device
)
3241 const char *valid_param_list
[] = { "tap", "socket", "dump"
3249 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
3250 if (!strncmp(valid_param_list
[i
], device
,
3251 strlen(valid_param_list
[i
])))
3258 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
3260 const char *device
= qdict_get_str(qdict
, "device");
3261 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
3264 if (!net_host_check_device(device
)) {
3265 monitor_printf(mon
, "invalid host network device %s\n", device
);
3269 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
3271 monitor_printf(mon
, "parsing network options '%s' failed\n",
3272 opts_str
? opts_str
: "");
3276 qemu_opt_set(opts
, "type", device
);
3278 if (net_client_init(mon
, opts
) < 0) {
3279 monitor_printf(mon
, "adding host network device %s failed\n", device
);
3283 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
3285 VLANClientState
*vc
;
3286 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
3287 const char *device
= qdict_get_str(qdict
, "device");
3289 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
3293 if (!net_host_check_device(vc
->model
)) {
3294 monitor_printf(mon
, "invalid host network device %s\n", device
);
3297 qemu_del_vlan_client(vc
);
3300 void net_set_boot_mask(int net_boot_mask
)
3304 /* Only the first four NICs may be bootable */
3305 net_boot_mask
= net_boot_mask
& 0xF;
3307 for (i
= 0; i
< nb_nics
; i
++) {
3308 if (net_boot_mask
& (1 << i
)) {
3309 nd_table
[i
].bootable
= 1;
3310 net_boot_mask
&= ~(1 << i
);
3314 if (net_boot_mask
) {
3315 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
3320 void do_info_network(Monitor
*mon
)
3324 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
3325 VLANClientState
*vc
;
3327 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
3329 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
3330 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
3335 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
3338 VLANClientState
*vc
= NULL
;
3339 const char *name
= qdict_get_str(qdict
, "name");
3340 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
3342 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
3343 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
3344 if (strcmp(vc
->name
, name
) == 0) {
3352 monitor_printf(mon
, "could not find network device '%s'\n", name
);
3356 if (strcmp(up_or_down
, "up") == 0)
3358 else if (strcmp(up_or_down
, "down") == 0)
3361 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
3362 "valid\n", up_or_down
);
3364 if (vc
->link_status_changed
)
3365 vc
->link_status_changed(vc
);
3368 void net_cleanup(void)
3372 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
3373 VLANClientState
*vc
, *next_vc
;
3375 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
3376 qemu_del_vlan_client(vc
);
3381 static void net_check_clients(void)
3385 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
3386 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
3388 if (vlan
->nb_guest_devs
== 0)
3389 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
3390 if (vlan
->nb_host_devs
== 0)
3392 "Warning: vlan %d is not connected to host network\n",
3397 static int net_init_client(QemuOpts
*opts
, void *dummy
)
3399 return net_client_init(NULL
, opts
);
3402 int net_init_clients(void)
3404 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
3405 /* if no clients, we use a default config */
3406 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
3408 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
3412 QTAILQ_INIT(&vlans
);
3414 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
3418 net_check_clients();
3423 int net_client_parse(const char *optarg
)
3425 #if defined(CONFIG_SLIRP)
3426 /* handle legacy -net channel,port:chr */
3427 if (!strncmp(optarg
, "channel,", strlen("channel,"))) {
3430 optarg
+= strlen("channel,");
3432 if (QTAILQ_EMPTY(&slirp_stacks
)) {
3433 struct slirp_config_str
*config
;
3435 config
= qemu_malloc(sizeof(*config
));
3436 pstrcpy(config
->str
, sizeof(config
->str
), optarg
);
3437 config
->flags
= SLIRP_CFG_LEGACY
;
3438 config
->next
= slirp_configs
;
3439 slirp_configs
= config
;
3442 ret
= slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks
), optarg
, 1);
3448 if (!qemu_opts_parse(&qemu_net_opts
, optarg
, "type")) {