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 #include "qemu-common.h"
108 #include "qemu-timer.h"
109 #include "qemu-char.h"
110 #include "audio/audio.h"
111 #include "qemu_socket.h"
112 #include "qemu-log.h"
113 #include "qemu-config.h"
115 #include "slirp/libslirp.h"
116 #include "qemu-queue.h"
119 static VLANState
*first_vlan
;
121 /***********************************************************/
122 /* network device redirectors */
124 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
125 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
129 for(i
=0;i
<size
;i
+=16) {
133 fprintf(f
, "%08x ", i
);
136 fprintf(f
, " %02x", buf
[i
+j
]);
143 if (c
< ' ' || c
> '~')
152 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
159 offset
= strtol(p
, &last_char
, 0);
160 if (0 == errno
&& '\0' == *last_char
&&
161 offset
>= 0 && offset
<= 0xFFFFFF) {
162 macaddr
[3] = (offset
& 0xFF0000) >> 16;
163 macaddr
[4] = (offset
& 0xFF00) >> 8;
164 macaddr
[5] = offset
& 0xFF;
167 for(i
= 0; i
< 6; i
++) {
168 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
173 if (*p
!= ':' && *p
!= '-')
184 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
195 if (len
> buf_size
- 1)
204 int parse_host_src_port(struct sockaddr_in
*haddr
,
205 struct sockaddr_in
*saddr
,
206 const char *input_str
)
208 char *str
= strdup(input_str
);
209 char *host_str
= str
;
211 const char *src_str2
;
215 * Chop off any extra arguments at the end of the string which
216 * would start with a comma, then fill in the src port information
217 * if it was provided else use the "any address" and "any port".
219 if ((ptr
= strchr(str
,',')))
222 if ((src_str
= strchr(input_str
,'@'))) {
227 if (parse_host_port(haddr
, host_str
) < 0)
231 if (!src_str
|| *src_str
== '\0')
234 if (parse_host_port(saddr
, src_str2
) < 0)
245 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
253 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
255 saddr
->sin_family
= AF_INET
;
256 if (buf
[0] == '\0') {
257 saddr
->sin_addr
.s_addr
= 0;
259 if (qemu_isdigit(buf
[0])) {
260 if (!inet_aton(buf
, &saddr
->sin_addr
))
263 if ((he
= gethostbyname(buf
)) == NULL
)
265 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
268 port
= strtol(p
, (char **)&r
, 0);
271 saddr
->sin_port
= htons(port
);
275 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
277 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
278 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
280 macaddr
[0], macaddr
[1], macaddr
[2],
281 macaddr
[3], macaddr
[4], macaddr
[5]);
284 static char *assign_name(VLANClientState
*vc1
, const char *model
)
290 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
293 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
294 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
298 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
300 return qemu_strdup(buf
);
303 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
306 NetCanReceive
*can_receive
,
308 NetReceiveIOV
*receive_iov
,
312 VLANClientState
*vc
, **pvc
;
313 vc
= qemu_mallocz(sizeof(VLANClientState
));
314 vc
->model
= qemu_strdup(model
);
316 vc
->name
= qemu_strdup(name
);
318 vc
->name
= assign_name(vc
, model
);
319 vc
->can_receive
= can_receive
;
320 vc
->receive
= receive
;
321 vc
->receive_iov
= receive_iov
;
322 vc
->cleanup
= cleanup
;
327 pvc
= &vlan
->first_client
;
334 void qemu_del_vlan_client(VLANClientState
*vc
)
336 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
345 qemu_free(vc
->model
);
352 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
354 VLANClientState
**pvc
= &vlan
->first_client
;
357 if ((*pvc
)->opaque
== opaque
)
365 static VLANClientState
*
366 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
367 const char *client_str
)
372 vlan
= qemu_find_vlan(vlan_id
, 0);
374 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
378 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
379 if (!strcmp(vc
->name
, client_str
)) {
384 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
385 client_str
, vlan_id
);
391 int qemu_can_send_packet(VLANClientState
*sender
)
393 VLANState
*vlan
= sender
->vlan
;
396 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
401 /* no can_receive() handler, they can always receive */
402 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
410 qemu_deliver_packet(VLANClientState
*sender
, const uint8_t *buf
, int size
)
415 sender
->vlan
->delivering
= 1;
417 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
429 len
= vc
->receive(vc
, buf
, size
);
431 ret
= (ret
>= 0) ? ret
: len
;
434 sender
->vlan
->delivering
= 0;
439 void qemu_purge_queued_packets(VLANClientState
*vc
)
441 VLANPacket
*packet
, *next
;
443 QTAILQ_FOREACH_SAFE(packet
, &vc
->vlan
->send_queue
, entry
, next
) {
444 if (packet
->sender
== vc
) {
445 QTAILQ_REMOVE(&vc
->vlan
->send_queue
, packet
, entry
);
451 void qemu_flush_queued_packets(VLANClientState
*vc
)
453 while (!QTAILQ_EMPTY(&vc
->vlan
->send_queue
)) {
457 packet
= QTAILQ_FIRST(&vc
->vlan
->send_queue
);
458 QTAILQ_REMOVE(&vc
->vlan
->send_queue
, packet
, entry
);
460 ret
= qemu_deliver_packet(packet
->sender
, packet
->data
, packet
->size
);
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
,
475 NetPacketSent
*sent_cb
)
479 packet
= qemu_malloc(sizeof(VLANPacket
) + size
);
480 packet
->sender
= sender
;
482 packet
->sent_cb
= sent_cb
;
483 memcpy(packet
->data
, buf
, size
);
485 QTAILQ_INSERT_TAIL(&sender
->vlan
->send_queue
, packet
, entry
);
488 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
489 const uint8_t *buf
, int size
,
490 NetPacketSent
*sent_cb
)
494 if (sender
->link_down
) {
499 printf("vlan %d send:\n", sender
->vlan
->id
);
500 hex_dump(stdout
, buf
, size
);
503 if (sender
->vlan
->delivering
) {
504 qemu_enqueue_packet(sender
, buf
, size
, NULL
);
508 ret
= qemu_deliver_packet(sender
, buf
, size
);
509 if (ret
== 0 && sent_cb
!= NULL
) {
510 qemu_enqueue_packet(sender
, buf
, size
, sent_cb
);
514 qemu_flush_queued_packets(sender
);
519 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
521 qemu_send_packet_async(vc
, buf
, size
, NULL
);
524 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
527 uint8_t buffer
[4096];
531 for (i
= 0; i
< iovcnt
; i
++) {
534 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
535 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
539 return vc
->receive(vc
, buffer
, offset
);
542 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
547 for (i
= 0; i
< iovcnt
; i
++)
548 offset
+= iov
[i
].iov_len
;
552 static int qemu_deliver_packet_iov(VLANClientState
*sender
,
553 const struct iovec
*iov
, int iovcnt
)
558 sender
->vlan
->delivering
= 1;
560 for (vc
= sender
->vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
568 ret
= calc_iov_length(iov
, iovcnt
);
572 if (vc
->receive_iov
) {
573 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
575 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
578 ret
= (ret
>= 0) ? ret
: len
;
581 sender
->vlan
->delivering
= 0;
586 static ssize_t
qemu_enqueue_packet_iov(VLANClientState
*sender
,
587 const struct iovec
*iov
, int iovcnt
,
588 NetPacketSent
*sent_cb
)
594 max_len
= calc_iov_length(iov
, iovcnt
);
596 packet
= qemu_malloc(sizeof(VLANPacket
) + max_len
);
597 packet
->sender
= sender
;
598 packet
->sent_cb
= sent_cb
;
601 for (i
= 0; i
< iovcnt
; i
++) {
602 size_t len
= iov
[i
].iov_len
;
604 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
608 QTAILQ_INSERT_TAIL(&sender
->vlan
->send_queue
, packet
, entry
);
613 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
614 const struct iovec
*iov
, int iovcnt
,
615 NetPacketSent
*sent_cb
)
619 if (sender
->link_down
) {
620 return calc_iov_length(iov
, iovcnt
);
623 if (sender
->vlan
->delivering
) {
624 return qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, NULL
);
627 ret
= qemu_deliver_packet_iov(sender
, iov
, iovcnt
);
628 if (ret
== 0 && sent_cb
!= NULL
) {
629 qemu_enqueue_packet_iov(sender
, iov
, iovcnt
, sent_cb
);
633 qemu_flush_queued_packets(sender
);
639 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
641 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
644 #if defined(CONFIG_SLIRP)
646 /* slirp network adapter */
648 #define SLIRP_CFG_HOSTFWD 1
649 #define SLIRP_CFG_LEGACY 2
651 struct slirp_config_str
{
652 struct slirp_config_str
*next
;
658 typedef struct SlirpState
{
659 QTAILQ_ENTRY(SlirpState
) entry
;
667 static struct slirp_config_str
*slirp_configs
;
668 const char *legacy_tftp_prefix
;
669 const char *legacy_bootp_filename
;
670 static QTAILQ_HEAD(slirp_stacks
, SlirpState
) slirp_stacks
=
671 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
673 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
675 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
679 static const char *legacy_smb_export
;
681 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
682 struct in_addr vserver_addr
);
683 static void slirp_smb_cleanup(SlirpState
*s
);
685 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
688 int slirp_can_output(void *opaque
)
690 SlirpState
*s
= opaque
;
692 return qemu_can_send_packet(s
->vc
);
695 void slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
697 SlirpState
*s
= opaque
;
700 printf("slirp output:\n");
701 hex_dump(stdout
, pkt
, pkt_len
);
703 qemu_send_packet(s
->vc
, pkt
, pkt_len
);
706 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
708 SlirpState
*s
= vc
->opaque
;
711 printf("slirp input:\n");
712 hex_dump(stdout
, buf
, size
);
714 slirp_input(s
->slirp
, buf
, size
);
718 static void net_slirp_cleanup(VLANClientState
*vc
)
720 SlirpState
*s
= vc
->opaque
;
722 slirp_cleanup(s
->slirp
);
723 slirp_smb_cleanup(s
);
724 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
728 static int net_slirp_init(VLANState
*vlan
, const char *model
,
729 const char *name
, int restricted
,
730 const char *vnetwork
, const char *vhost
,
731 const char *vhostname
, const char *tftp_export
,
732 const char *bootfile
, const char *vdhcp_start
,
733 const char *vnameserver
, const char *smb_export
,
734 const char *vsmbserver
)
736 /* default settings according to historic slirp */
737 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
738 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
739 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
740 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
741 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
743 struct in_addr smbsrv
= { .s_addr
= 0 };
750 struct slirp_config_str
*config
;
753 tftp_export
= legacy_tftp_prefix
;
756 bootfile
= legacy_bootp_filename
;
760 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
761 if (!inet_aton(vnetwork
, &net
)) {
764 addr
= ntohl(net
.s_addr
);
765 if (!(addr
& 0x80000000)) {
766 mask
.s_addr
= htonl(0xff000000); /* class A */
767 } else if ((addr
& 0xfff00000) == 0xac100000) {
768 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
769 } else if ((addr
& 0xc0000000) == 0x80000000) {
770 mask
.s_addr
= htonl(0xffff0000); /* class B */
771 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
772 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
773 } else if ((addr
& 0xffff0000) == 0xc6120000) {
774 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
775 } else if ((addr
& 0xe0000000) == 0xe0000000) {
776 mask
.s_addr
= htonl(0xffffff00); /* class C */
778 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
781 if (!inet_aton(buf
, &net
)) {
784 shift
= strtol(vnetwork
, &end
, 10);
786 if (!inet_aton(vnetwork
, &mask
)) {
789 } else if (shift
< 4 || shift
> 32) {
792 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
795 net
.s_addr
&= mask
.s_addr
;
796 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
797 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
798 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
801 if (vhost
&& !inet_aton(vhost
, &host
)) {
804 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
808 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
811 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
||
812 dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
816 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
819 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
||
820 dns
.s_addr
== host
.s_addr
) {
825 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
830 s
= qemu_mallocz(sizeof(SlirpState
));
831 s
->slirp
= slirp_init(restricted
, net
, mask
, host
, vhostname
,
832 tftp_export
, bootfile
, dhcp
, dns
, s
);
833 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
835 for (config
= slirp_configs
; config
; config
= config
->next
) {
836 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
837 if (slirp_hostfwd(s
, config
->str
,
838 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
841 if (slirp_guestfwd(s
, config
->str
,
842 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
848 smb_export
= legacy_smb_export
;
851 if (slirp_smb(s
, smb_export
, smbsrv
) < 0)
856 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, slirp_receive
, NULL
,
857 net_slirp_cleanup
, s
);
858 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
859 "net=%s, restricted=%c", inet_ntoa(net
), restricted
? 'y' : 'n');
863 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *vlan
,
869 vc
= qemu_find_vlan_client_by_name(mon
, strtol(vlan
, NULL
, 0), stack
);
873 if (strcmp(vc
->model
, "user")) {
874 monitor_printf(mon
, "invalid device specified\n");
879 if (QTAILQ_EMPTY(&slirp_stacks
)) {
880 monitor_printf(mon
, "user mode network stack not in use\n");
883 return QTAILQ_FIRST(&slirp_stacks
);
887 void net_slirp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
889 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
892 const char *src_str
, *p
;
896 const char *arg1
= qdict_get_str(qdict
, "arg1");
897 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
898 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
901 s
= slirp_lookup(mon
, arg1
, arg2
);
904 s
= slirp_lookup(mon
, NULL
, NULL
);
911 if (!src_str
|| !src_str
[0])
915 get_str_sep(buf
, sizeof(buf
), &p
, ':');
917 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
919 } else if (!strcmp(buf
, "udp")) {
925 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
928 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
934 err
= slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks
)->slirp
, is_udp
,
935 host_addr
, host_port
);
937 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
938 err
? "removed" : "not found");
942 monitor_printf(mon
, "invalid format\n");
945 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
948 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
949 struct in_addr guest_addr
= { .s_addr
= 0 };
950 int host_port
, guest_port
;
957 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
960 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
962 } else if (!strcmp(buf
, "udp")) {
968 if (!legacy_format
) {
969 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
972 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
977 if (get_str_sep(buf
, sizeof(buf
), &p
, legacy_format
? ':' : '-') < 0) {
980 host_port
= strtol(buf
, &end
, 0);
981 if (*end
!= '\0' || host_port
< 1 || host_port
> 65535) {
985 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
988 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
992 guest_port
= strtol(p
, &end
, 0);
993 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
997 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
999 qemu_error("could not set up host forwarding rule '%s'\n",
1006 qemu_error("invalid host forwarding rule '%s'\n", redir_str
);
1010 void net_slirp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
1012 const char *redir_str
;
1014 const char *arg1
= qdict_get_str(qdict
, "arg1");
1015 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
1016 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
1019 s
= slirp_lookup(mon
, arg1
, arg2
);
1022 s
= slirp_lookup(mon
, NULL
, NULL
);
1026 slirp_hostfwd(s
, redir_str
, 0);
1031 int net_slirp_redir(const char *redir_str
)
1033 struct slirp_config_str
*config
;
1035 if (QTAILQ_EMPTY(&slirp_stacks
)) {
1036 config
= qemu_malloc(sizeof(*config
));
1037 pstrcpy(config
->str
, sizeof(config
->str
), redir_str
);
1038 config
->flags
= SLIRP_CFG_HOSTFWD
| SLIRP_CFG_LEGACY
;
1039 config
->next
= slirp_configs
;
1040 slirp_configs
= config
;
1044 return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks
), redir_str
, 1);
1049 /* automatic user mode samba server configuration */
1050 static void slirp_smb_cleanup(SlirpState
*s
)
1054 if (s
->smb_dir
[0] != '\0') {
1055 snprintf(cmd
, sizeof(cmd
), "rm -rf %s", s
->smb_dir
);
1057 s
->smb_dir
[0] = '\0';
1061 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
1062 struct in_addr vserver_addr
)
1064 static int instance
;
1066 char smb_cmdline
[128];
1069 snprintf(s
->smb_dir
, sizeof(s
->smb_dir
), "/tmp/qemu-smb.%ld-%d",
1070 (long)getpid(), instance
++);
1071 if (mkdir(s
->smb_dir
, 0700) < 0) {
1072 qemu_error("could not create samba server dir '%s'\n", s
->smb_dir
);
1075 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", s
->smb_dir
, "smb.conf");
1077 f
= fopen(smb_conf
, "w");
1079 slirp_smb_cleanup(s
);
1080 qemu_error("could not create samba server configuration file '%s'\n",
1088 "socket address=127.0.0.1\n"
1089 "pid directory=%s\n"
1090 "lock directory=%s\n"
1091 "log file=%s/log.smbd\n"
1092 "smb passwd file=%s/smbpasswd\n"
1093 "security = share\n"
1107 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1108 SMBD_COMMAND
, smb_conf
);
1110 if (slirp_add_exec(s
->slirp
, 0, smb_cmdline
, &vserver_addr
, 139) < 0) {
1111 slirp_smb_cleanup(s
);
1112 qemu_error("conflicting/invalid smbserver address\n");
1118 /* automatic user mode samba server configuration (legacy interface) */
1119 int net_slirp_smb(const char *exported_dir
)
1121 struct in_addr vserver_addr
= { .s_addr
= 0 };
1123 if (legacy_smb_export
) {
1124 fprintf(stderr
, "-smb given twice\n");
1127 legacy_smb_export
= exported_dir
;
1128 if (!QTAILQ_EMPTY(&slirp_stacks
)) {
1129 return slirp_smb(QTAILQ_FIRST(&slirp_stacks
), exported_dir
,
1135 #endif /* !defined(_WIN32) */
1138 CharDriverState
*hd
;
1139 struct in_addr server
;
1144 static int guestfwd_can_read(void *opaque
)
1146 struct GuestFwd
*fwd
= opaque
;
1147 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1150 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1152 struct GuestFwd
*fwd
= opaque
;
1153 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1156 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
1159 struct in_addr server
= { .s_addr
= 0 };
1160 struct GuestFwd
*fwd
;
1167 if (legacy_format
) {
1168 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1172 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1175 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1178 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1181 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1184 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1188 port
= strtol(buf
, &end
, 10);
1189 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1193 fwd
= qemu_malloc(sizeof(struct GuestFwd
));
1194 snprintf(buf
, sizeof(buf
), "guestfwd.tcp:%d", port
);
1195 fwd
->hd
= qemu_chr_open(buf
, p
, NULL
);
1197 qemu_error("could not open guest forwarding device '%s'\n", buf
);
1202 if (slirp_add_exec(s
->slirp
, 3, fwd
->hd
, &server
, port
) < 0) {
1203 qemu_error("conflicting/invalid host:port in guest forwarding "
1204 "rule '%s'\n", config_str
);
1208 fwd
->server
= server
;
1210 fwd
->slirp
= s
->slirp
;
1212 qemu_chr_add_handlers(fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1217 qemu_error("invalid guest forwarding rule '%s'\n", config_str
);
1221 void do_info_usernet(Monitor
*mon
)
1225 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1226 monitor_printf(mon
, "VLAN %d (%s):\n", s
->vc
->vlan
->id
, s
->vc
->name
);
1227 slirp_connection_info(s
->slirp
, mon
);
1231 #endif /* CONFIG_SLIRP */
1233 #if !defined(_WIN32)
1235 typedef struct TAPState
{
1236 VLANClientState
*vc
;
1238 char down_script
[1024];
1239 char down_script_arg
[128];
1241 unsigned int read_poll
: 1;
1242 unsigned int write_poll
: 1;
1245 static int launch_script(const char *setup_script
, const char *ifname
, int fd
);
1247 static int tap_can_send(void *opaque
);
1248 static void tap_send(void *opaque
);
1249 static void tap_writable(void *opaque
);
1251 static void tap_update_fd_handler(TAPState
*s
)
1253 qemu_set_fd_handler2(s
->fd
,
1254 s
->read_poll
? tap_can_send
: NULL
,
1255 s
->read_poll
? tap_send
: NULL
,
1256 s
->write_poll
? tap_writable
: NULL
,
1260 static void tap_read_poll(TAPState
*s
, int enable
)
1262 s
->read_poll
= !!enable
;
1263 tap_update_fd_handler(s
);
1266 static void tap_write_poll(TAPState
*s
, int enable
)
1268 s
->write_poll
= !!enable
;
1269 tap_update_fd_handler(s
);
1272 static void tap_writable(void *opaque
)
1274 TAPState
*s
= opaque
;
1276 tap_write_poll(s
, 0);
1278 qemu_flush_queued_packets(s
->vc
);
1281 static ssize_t
tap_receive_iov(VLANClientState
*vc
, const struct iovec
*iov
,
1284 TAPState
*s
= vc
->opaque
;
1288 len
= writev(s
->fd
, iov
, iovcnt
);
1289 } while (len
== -1 && errno
== EINTR
);
1291 if (len
== -1 && errno
== EAGAIN
) {
1292 tap_write_poll(s
, 1);
1299 static ssize_t
tap_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1301 TAPState
*s
= vc
->opaque
;
1305 len
= write(s
->fd
, buf
, size
);
1306 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
1311 static int tap_can_send(void *opaque
)
1313 TAPState
*s
= opaque
;
1315 return qemu_can_send_packet(s
->vc
);
1319 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1324 sbuf
.maxlen
= maxlen
;
1325 sbuf
.buf
= (char *)buf
;
1327 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
1330 static ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
1332 return read(tapfd
, buf
, maxlen
);
1336 static void tap_send_completed(VLANClientState
*vc
, ssize_t len
)
1338 TAPState
*s
= vc
->opaque
;
1339 tap_read_poll(s
, 1);
1342 static void tap_send(void *opaque
)
1344 TAPState
*s
= opaque
;
1348 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
1353 size
= qemu_send_packet_async(s
->vc
, s
->buf
, size
, tap_send_completed
);
1355 tap_read_poll(s
, 0);
1361 /* sndbuf should be set to a value lower than the tx queue
1362 * capacity of any destination network interface.
1363 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
1364 * a good default, given a 1500 byte MTU.
1366 #define TAP_DEFAULT_SNDBUF 1024*1024
1368 static int tap_set_sndbuf(TAPState
*s
, QemuOpts
*opts
)
1372 sndbuf
= qemu_opt_get_size(opts
, "sndbuf", TAP_DEFAULT_SNDBUF
);
1377 if (ioctl(s
->fd
, TUNSETSNDBUF
, &sndbuf
) == -1 && qemu_opt_get(opts
, "sndbuf")) {
1378 qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno
));
1384 static int tap_set_sndbuf(TAPState
*s
, QemuOpts
*opts
)
1388 #endif /* TUNSETSNDBUF */
1390 static void tap_cleanup(VLANClientState
*vc
)
1392 TAPState
*s
= vc
->opaque
;
1394 qemu_purge_queued_packets(vc
);
1396 if (s
->down_script
[0])
1397 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1399 tap_read_poll(s
, 0);
1400 tap_write_poll(s
, 0);
1407 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
1414 s
= qemu_mallocz(sizeof(TAPState
));
1416 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, tap_receive
,
1417 tap_receive_iov
, tap_cleanup
, s
);
1418 tap_read_poll(s
, 1);
1419 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
1423 #if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__)
1424 static int tap_open(char *ifname
, int ifname_size
)
1430 TFR(fd
= open("/dev/tap", O_RDWR
));
1432 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1437 dev
= devname(s
.st_rdev
, S_IFCHR
);
1438 pstrcpy(ifname
, ifname_size
, dev
);
1440 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1443 #elif defined(__sun__)
1444 #define TUNNEWPPA (('T'<<16) | 0x0001)
1446 * Allocate TAP device, returns opened fd.
1447 * Stores dev name in the first arg(must be large enough).
1449 static int tap_alloc(char *dev
, size_t dev_size
)
1451 int tap_fd
, if_fd
, ppa
= -1;
1452 static int ip_fd
= 0;
1455 static int arp_fd
= 0;
1456 int ip_muxid
, arp_muxid
;
1457 struct strioctl strioc_if
, strioc_ppa
;
1458 int link_type
= I_PLINK
;;
1460 char actual_name
[32] = "";
1462 memset(&ifr
, 0x0, sizeof(ifr
));
1466 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
1470 /* Check if IP device was opened */
1474 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
1476 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
1480 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
1482 syslog(LOG_ERR
, "Can't open /dev/tap");
1486 /* Assign a new PPA and get its unit number. */
1487 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
1488 strioc_ppa
.ic_timout
= 0;
1489 strioc_ppa
.ic_len
= sizeof(ppa
);
1490 strioc_ppa
.ic_dp
= (char *)&ppa
;
1491 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
1492 syslog (LOG_ERR
, "Can't assign new interface");
1494 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
1496 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
1499 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
1500 syslog(LOG_ERR
, "Can't push IP module");
1504 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
1505 syslog(LOG_ERR
, "Can't get flags\n");
1507 snprintf (actual_name
, 32, "tap%d", ppa
);
1508 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1511 /* Assign ppa according to the unit number returned by tun device */
1513 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
1514 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
1515 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
1516 syslog (LOG_ERR
, "Can't get flags\n");
1517 /* Push arp module to if_fd */
1518 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
1519 syslog (LOG_ERR
, "Can't push ARP module (2)");
1521 /* Push arp module to ip_fd */
1522 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
1523 syslog (LOG_ERR
, "I_POP failed\n");
1524 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
1525 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
1527 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
1529 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
1531 /* Set ifname to arp */
1532 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
1533 strioc_if
.ic_timout
= 0;
1534 strioc_if
.ic_len
= sizeof(ifr
);
1535 strioc_if
.ic_dp
= (char *)&ifr
;
1536 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
1537 syslog (LOG_ERR
, "Can't set ifname to arp\n");
1540 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
1541 syslog(LOG_ERR
, "Can't link TAP device to IP");
1545 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
1546 syslog (LOG_ERR
, "Can't link TAP device to ARP");
1550 memset(&ifr
, 0x0, sizeof(ifr
));
1551 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
1552 ifr
.lifr_ip_muxid
= ip_muxid
;
1553 ifr
.lifr_arp_muxid
= arp_muxid
;
1555 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
1557 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
1558 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
1559 syslog (LOG_ERR
, "Can't set multiplexor id");
1562 snprintf(dev
, dev_size
, "tap%d", ppa
);
1566 static int tap_open(char *ifname
, int ifname_size
)
1570 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
1571 fprintf(stderr
, "Cannot allocate TAP device\n");
1574 pstrcpy(ifname
, ifname_size
, dev
);
1575 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1578 #elif defined (_AIX)
1579 static int tap_open(char *ifname
, int ifname_size
)
1581 fprintf (stderr
, "no tap on AIX\n");
1585 static int tap_open(char *ifname
, int ifname_size
)
1590 TFR(fd
= open("/dev/net/tun", O_RDWR
));
1592 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1595 memset(&ifr
, 0, sizeof(ifr
));
1596 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1597 if (ifname
[0] != '\0')
1598 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
1600 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
1601 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1603 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1607 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1608 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1613 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
1615 sigset_t oldmask
, mask
;
1621 sigaddset(&mask
, SIGCHLD
);
1622 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
1624 /* try to launch network script */
1627 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
1629 for (i
= 0; i
< open_max
; i
++) {
1630 if (i
!= STDIN_FILENO
&&
1631 i
!= STDOUT_FILENO
&&
1632 i
!= STDERR_FILENO
&&
1638 *parg
++ = (char *)setup_script
;
1639 *parg
++ = (char *)ifname
;
1641 execv(setup_script
, args
);
1643 } else if (pid
> 0) {
1644 while (waitpid(pid
, &status
, 0) != pid
) {
1647 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
1649 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
1653 fprintf(stderr
, "%s: could not launch network script\n", setup_script
);
1657 static TAPState
*net_tap_init(VLANState
*vlan
, const char *model
,
1658 const char *name
, const char *ifname1
,
1659 const char *setup_script
, const char *down_script
)
1665 if (ifname1
!= NULL
)
1666 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
1669 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
1673 if (!setup_script
|| !strcmp(setup_script
, "no"))
1675 if (setup_script
[0] != '\0' &&
1676 launch_script(setup_script
, ifname
, fd
)) {
1679 s
= net_tap_fd_init(vlan
, model
, name
, fd
);
1680 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1681 "ifname=%s,script=%s,downscript=%s",
1682 ifname
, setup_script
, down_script
);
1683 if (down_script
&& strcmp(down_script
, "no")) {
1684 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
1685 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1690 #endif /* !_WIN32 */
1692 #if defined(CONFIG_VDE)
1693 typedef struct VDEState
{
1694 VLANClientState
*vc
;
1698 static void vde_to_qemu(void *opaque
)
1700 VDEState
*s
= opaque
;
1704 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1706 qemu_send_packet(s
->vc
, buf
, size
);
1710 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1712 VDEState
*s
= vc
->opaque
;
1716 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1717 } while (ret
< 0 && errno
== EINTR
);
1722 static void vde_cleanup(VLANClientState
*vc
)
1724 VDEState
*s
= vc
->opaque
;
1725 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1730 static int net_vde_init(VLANState
*vlan
, const char *model
,
1731 const char *name
, const char *sock
,
1732 int port
, const char *group
, int mode
)
1735 char *init_group
= (char *)group
;
1736 char *init_sock
= (char *)sock
;
1738 struct vde_open_args args
= {
1740 .group
= init_group
,
1744 s
= qemu_mallocz(sizeof(VDEState
));
1745 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1750 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, vde_receive
,
1751 NULL
, vde_cleanup
, s
);
1752 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1753 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1754 sock
, vde_datafd(s
->vde
));
1759 /* network connection */
1760 typedef struct NetSocketState
{
1761 VLANClientState
*vc
;
1763 int state
; /* 0 = getting length, 1 = getting data */
1765 unsigned int packet_len
;
1767 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1770 typedef struct NetSocketListenState
{
1775 } NetSocketListenState
;
1777 /* XXX: we consider we can send the whole packet without blocking */
1778 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1780 NetSocketState
*s
= vc
->opaque
;
1784 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1785 return send_all(s
->fd
, buf
, size
);
1788 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1790 NetSocketState
*s
= vc
->opaque
;
1792 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1793 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1796 static void net_socket_send(void *opaque
)
1798 NetSocketState
*s
= opaque
;
1804 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1806 err
= socket_error();
1807 if (err
!= EWOULDBLOCK
)
1809 } else if (size
== 0) {
1810 /* end of connection */
1812 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1818 /* reassemble a packet from the network */
1824 memcpy(s
->buf
+ s
->index
, buf
, l
);
1828 if (s
->index
== 4) {
1830 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1836 l
= s
->packet_len
- s
->index
;
1839 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1840 memcpy(s
->buf
+ s
->index
, buf
, l
);
1842 fprintf(stderr
, "serious error: oversized packet received,"
1843 "connection terminated.\n");
1851 if (s
->index
>= s
->packet_len
) {
1852 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1861 static void net_socket_send_dgram(void *opaque
)
1863 NetSocketState
*s
= opaque
;
1866 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1870 /* end of connection */
1871 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1874 qemu_send_packet(s
->vc
, s
->buf
, size
);
1877 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1882 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1883 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1884 inet_ntoa(mcastaddr
->sin_addr
),
1885 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1889 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1891 perror("socket(PF_INET, SOCK_DGRAM)");
1896 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1897 (const char *)&val
, sizeof(val
));
1899 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1903 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1909 /* Add host to multicast group */
1910 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1911 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1913 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1914 (const char *)&imr
, sizeof(struct ip_mreq
));
1916 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1920 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1922 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1923 (const char *)&val
, sizeof(val
));
1925 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1929 socket_set_nonblock(fd
);
1937 static void net_socket_cleanup(VLANClientState
*vc
)
1939 NetSocketState
*s
= vc
->opaque
;
1940 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1945 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1948 int fd
, int is_connected
)
1950 struct sockaddr_in saddr
;
1952 socklen_t saddr_len
;
1955 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1956 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1957 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1961 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1963 if (saddr
.sin_addr
.s_addr
==0) {
1964 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1968 /* clone dgram socket */
1969 newfd
= net_socket_mcast_create(&saddr
);
1971 /* error already reported by net_socket_mcast_create() */
1975 /* clone newfd to fd, close newfd */
1980 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1981 fd
, strerror(errno
));
1986 s
= qemu_mallocz(sizeof(NetSocketState
));
1989 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive_dgram
,
1990 NULL
, net_socket_cleanup
, s
);
1991 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1993 /* mcast: save bound address as dst */
1994 if (is_connected
) s
->dgram_dst
=saddr
;
1996 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1997 "socket: fd=%d (%s mcast=%s:%d)",
1998 fd
, is_connected
? "cloned" : "",
1999 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2003 static void net_socket_connect(void *opaque
)
2005 NetSocketState
*s
= opaque
;
2006 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
2009 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
2012 int fd
, int is_connected
)
2015 s
= qemu_mallocz(sizeof(NetSocketState
));
2017 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, NULL
, net_socket_receive
,
2018 NULL
, net_socket_cleanup
, s
);
2019 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2020 "socket: fd=%d", fd
);
2022 net_socket_connect(s
);
2024 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
2029 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
2030 const char *model
, const char *name
,
2031 int fd
, int is_connected
)
2033 int so_type
= -1, optlen
=sizeof(so_type
);
2035 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
2036 (socklen_t
*)&optlen
)< 0) {
2037 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
2042 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
2044 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2046 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2047 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
2048 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
2053 static void net_socket_accept(void *opaque
)
2055 NetSocketListenState
*s
= opaque
;
2057 struct sockaddr_in saddr
;
2062 len
= sizeof(saddr
);
2063 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
2064 if (fd
< 0 && errno
!= EINTR
) {
2066 } else if (fd
>= 0) {
2070 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
2074 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
2075 "socket: connection from %s:%d",
2076 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2080 static int net_socket_listen_init(VLANState
*vlan
,
2083 const char *host_str
)
2085 NetSocketListenState
*s
;
2087 struct sockaddr_in saddr
;
2089 if (parse_host_port(&saddr
, host_str
) < 0)
2092 s
= qemu_mallocz(sizeof(NetSocketListenState
));
2094 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2099 socket_set_nonblock(fd
);
2101 /* allow fast reuse */
2103 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
2105 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2110 ret
= listen(fd
, 0);
2116 s
->model
= qemu_strdup(model
);
2117 s
->name
= name
? qemu_strdup(name
) : NULL
;
2119 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
2123 static int net_socket_connect_init(VLANState
*vlan
,
2126 const char *host_str
)
2129 int fd
, connected
, ret
, err
;
2130 struct sockaddr_in saddr
;
2132 if (parse_host_port(&saddr
, host_str
) < 0)
2135 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2140 socket_set_nonblock(fd
);
2144 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
2146 err
= socket_error();
2147 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
2148 } else if (err
== EINPROGRESS
) {
2151 } else if (err
== WSAEALREADY
) {
2164 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
2167 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2168 "socket: connect to %s:%d",
2169 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2173 static int net_socket_mcast_init(VLANState
*vlan
,
2176 const char *host_str
)
2180 struct sockaddr_in saddr
;
2182 if (parse_host_port(&saddr
, host_str
) < 0)
2186 fd
= net_socket_mcast_create(&saddr
);
2190 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
2194 s
->dgram_dst
= saddr
;
2196 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
2197 "socket: mcast=%s:%d",
2198 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
2203 typedef struct DumpState
{
2204 VLANClientState
*pcap_vc
;
2209 #define PCAP_MAGIC 0xa1b2c3d4
2211 struct pcap_file_hdr
{
2213 uint16_t version_major
;
2214 uint16_t version_minor
;
2221 struct pcap_sf_pkthdr
{
2230 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
2232 DumpState
*s
= vc
->opaque
;
2233 struct pcap_sf_pkthdr hdr
;
2237 /* Early return in case of previous error. */
2242 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, get_ticks_per_sec());
2243 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
2245 hdr
.ts
.tv_sec
= ts
/ 1000000;
2246 hdr
.ts
.tv_usec
= ts
% 1000000;
2247 hdr
.caplen
= caplen
;
2249 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
2250 write(s
->fd
, buf
, caplen
) != caplen
) {
2251 qemu_log("-net dump write error - stop dump\n");
2259 static void net_dump_cleanup(VLANClientState
*vc
)
2261 DumpState
*s
= vc
->opaque
;
2267 static int net_dump_init(VLANState
*vlan
, const char *device
,
2268 const char *name
, const char *filename
, int len
)
2270 struct pcap_file_hdr hdr
;
2273 s
= qemu_malloc(sizeof(DumpState
));
2275 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
2277 qemu_error("-net dump: can't open %s\n", filename
);
2281 s
->pcap_caplen
= len
;
2283 hdr
.magic
= PCAP_MAGIC
;
2284 hdr
.version_major
= 2;
2285 hdr
.version_minor
= 4;
2288 hdr
.snaplen
= s
->pcap_caplen
;
2291 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
2292 qemu_error("-net dump write error: %s\n", strerror(errno
));
2298 s
->pcap_vc
= qemu_new_vlan_client(vlan
, device
, name
, NULL
, dump_receive
, NULL
,
2299 net_dump_cleanup
, s
);
2300 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
2301 "dump to %s (len=%d)", filename
, len
);
2305 /* find or alloc a new VLAN */
2306 VLANState
*qemu_find_vlan(int id
, int allocate
)
2308 VLANState
**pvlan
, *vlan
;
2309 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2316 vlan
= qemu_mallocz(sizeof(VLANState
));
2318 QTAILQ_INIT(&vlan
->send_queue
);
2320 pvlan
= &first_vlan
;
2321 while (*pvlan
!= NULL
)
2322 pvlan
= &(*pvlan
)->next
;
2327 static int nic_get_free_idx(void)
2331 for (index
= 0; index
< MAX_NICS
; index
++)
2332 if (!nd_table
[index
].used
)
2337 int qemu_show_nic_models(const char *arg
, const char *const *models
)
2341 if (!arg
|| strcmp(arg
, "?"))
2344 fprintf(stderr
, "qemu: Supported NIC models: ");
2345 for (i
= 0 ; models
[i
]; i
++)
2346 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2350 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2352 const char *models
[2];
2357 if (qemu_show_nic_models(nd
->model
, models
))
2359 if (qemu_find_nic_model(nd
, models
, model
) < 0)
2363 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
2364 const char *default_model
)
2369 nd
->model
= qemu_strdup(default_model
);
2371 for (i
= 0 ; models
[i
]; i
++) {
2372 if (strcmp(nd
->model
, models
[i
]) == 0)
2376 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
2380 static int net_handle_fd_param(Monitor
*mon
, const char *param
)
2382 if (!qemu_isdigit(param
[0])) {
2385 fd
= monitor_get_fd(mon
, param
);
2387 qemu_error("No file descriptor named %s found", param
);
2393 return strtol(param
, NULL
, 0);
2397 static int net_init_nic(QemuOpts
*opts
, Monitor
*mon
)
2402 idx
= nic_get_free_idx();
2403 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2404 qemu_error("Too Many NICs\n");
2408 nd
= &nd_table
[idx
];
2410 memset(nd
, 0, sizeof(*nd
));
2412 nd
->vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2414 if (qemu_opts_id(opts
)) {
2415 nd
->id
= qemu_strdup(qemu_opts_id(opts
));
2417 if (qemu_opt_get(opts
, "name")) {
2418 nd
->name
= qemu_strdup(qemu_opt_get(opts
, "name"));
2420 if (qemu_opt_get(opts
, "model")) {
2421 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
2423 if (qemu_opt_get(opts
, "addr")) {
2424 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
2427 nd
->macaddr
[0] = 0x52;
2428 nd
->macaddr
[1] = 0x54;
2429 nd
->macaddr
[2] = 0x00;
2430 nd
->macaddr
[3] = 0x12;
2431 nd
->macaddr
[4] = 0x34;
2432 nd
->macaddr
[5] = 0x56 + idx
;
2434 if (qemu_opt_get(opts
, "macaddr") &&
2435 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
2436 qemu_error("invalid syntax for ethernet address\n");
2440 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
2441 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
2442 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
2443 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
2448 nd
->vlan
->nb_guest_devs
++;
2454 static int net_init_slirp_configs(const char *name
, const char *value
, void *opaque
)
2456 struct slirp_config_str
*config
;
2458 if (strcmp(name
, "hostfwd") != 0 && strcmp(name
, "guestfwd") != 0) {
2462 config
= qemu_mallocz(sizeof(*config
));
2464 pstrcpy(config
->str
, sizeof(config
->str
), value
);
2466 if (!strcmp(name
, "hostfwd")) {
2467 config
->flags
= SLIRP_CFG_HOSTFWD
;
2470 config
->next
= slirp_configs
;
2471 slirp_configs
= config
;
2476 static int net_init_slirp(QemuOpts
*opts
, Monitor
*mon
)
2479 struct slirp_config_str
*config
;
2482 const char *vhostname
;
2483 const char *vdhcp_start
;
2484 const char *vnamesrv
;
2485 const char *tftp_export
;
2486 const char *bootfile
;
2487 const char *smb_export
;
2488 const char *vsmbsrv
;
2493 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2495 name
= qemu_opt_get(opts
, "name");
2497 vhost
= qemu_opt_get(opts
, "host");
2498 vhostname
= qemu_opt_get(opts
, "hostname");
2499 vdhcp_start
= qemu_opt_get(opts
, "dhcpstart");
2500 vnamesrv
= qemu_opt_get(opts
, "dns");
2501 tftp_export
= qemu_opt_get(opts
, "tftp");
2502 bootfile
= qemu_opt_get(opts
, "bootfile");
2503 smb_export
= qemu_opt_get(opts
, "smb");
2504 vsmbsrv
= qemu_opt_get(opts
, "smbserver");
2506 if (qemu_opt_get(opts
, "ip")) {
2507 const char *ip
= qemu_opt_get(opts
, "ip");
2508 int l
= strlen(ip
) + strlen("/24") + 1;
2510 vnet
= qemu_malloc(l
);
2512 /* emulate legacy ip= parameter */
2513 pstrcpy(vnet
, l
, ip
);
2514 pstrcat(vnet
, l
, "/24");
2517 if (qemu_opt_get(opts
, "net")) {
2521 vnet
= qemu_strdup(qemu_opt_get(opts
, "net"));
2524 if (qemu_opt_get(opts
, "restrict") &&
2525 qemu_opt_get(opts
, "restrict")[0] == 'y') {
2529 qemu_opt_foreach(opts
, net_init_slirp_configs
, NULL
, 0);
2531 ret
= net_slirp_init(vlan
, "user", name
, restricted
, vnet
, vhost
,
2532 vhostname
, tftp_export
, bootfile
, vdhcp_start
,
2533 vnamesrv
, smb_export
, vsmbsrv
);
2535 while (slirp_configs
) {
2536 config
= slirp_configs
;
2537 slirp_configs
= config
->next
;
2542 vlan
->nb_host_devs
++;
2551 static int net_init_tap_win32(QemuOpts
*opts
, Monitor
*mon
)
2557 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2559 name
= qemu_opt_get(opts
, "name");
2560 ifname
= qemu_opt_get(opts
, "ifname");
2563 qemu_error("tap: no interface name\n");
2567 if (tap_win32_init(vlan
, "tap", name
, ifname
) == -1) {
2571 vlan
->nb_host_devs
++;
2575 #elif !defined(_AIX)
2576 static int net_init_tap(QemuOpts
*opts
, Monitor
*mon
)
2582 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2584 name
= qemu_opt_get(opts
, "name");
2586 if (qemu_opt_get(opts
, "fd")) {
2589 if (qemu_opt_get(opts
, "ifname") ||
2590 qemu_opt_get(opts
, "script") ||
2591 qemu_opt_get(opts
, "downscript")) {
2592 qemu_error("ifname=, script= and downscript= is invalid with fd=\n");
2596 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2601 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2603 s
= net_tap_fd_init(vlan
, "tap", name
, fd
);
2608 const char *ifname
, *script
, *downscript
;
2610 ifname
= qemu_opt_get(opts
, "ifname");
2611 script
= qemu_opt_get(opts
, "script");
2612 downscript
= qemu_opt_get(opts
, "downscript");
2615 script
= DEFAULT_NETWORK_SCRIPT
;
2618 downscript
= DEFAULT_NETWORK_DOWN_SCRIPT
;
2621 s
= net_tap_init(vlan
, "tap", name
, ifname
, script
, downscript
);
2628 if (tap_set_sndbuf(s
, opts
) < 0) {
2632 vlan
->nb_host_devs
++;
2638 static int net_init_socket(QemuOpts
*opts
, Monitor
*mon
)
2643 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2645 name
= qemu_opt_get(opts
, "name");
2647 if (qemu_opt_get(opts
, "fd")) {
2650 if (qemu_opt_get(opts
, "listen") ||
2651 qemu_opt_get(opts
, "connect") ||
2652 qemu_opt_get(opts
, "mcast")) {
2653 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
2657 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2662 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
2666 } else if (qemu_opt_get(opts
, "listen")) {
2669 if (qemu_opt_get(opts
, "fd") ||
2670 qemu_opt_get(opts
, "connect") ||
2671 qemu_opt_get(opts
, "mcast")) {
2672 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
2676 listen
= qemu_opt_get(opts
, "listen");
2678 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
2681 } else if (qemu_opt_get(opts
, "connect")) {
2682 const char *connect
;
2684 if (qemu_opt_get(opts
, "fd") ||
2685 qemu_opt_get(opts
, "listen") ||
2686 qemu_opt_get(opts
, "mcast")) {
2687 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
2691 connect
= qemu_opt_get(opts
, "connect");
2693 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
2696 } else if (qemu_opt_get(opts
, "mcast")) {
2699 if (qemu_opt_get(opts
, "fd") ||
2700 qemu_opt_get(opts
, "connect") ||
2701 qemu_opt_get(opts
, "listen")) {
2702 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
2706 mcast
= qemu_opt_get(opts
, "mcast");
2708 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
) == -1) {
2712 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
2716 vlan
->nb_host_devs
++;
2722 static int net_init_vde(QemuOpts
*opts
, Monitor
*mon
)
2730 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2732 name
= qemu_opt_get(opts
, "name");
2733 sock
= qemu_opt_get(opts
, "sock");
2734 group
= qemu_opt_get(opts
, "group");
2736 port
= qemu_opt_get_number(opts
, "port", 0);
2737 mode
= qemu_opt_get_number(opts
, "mode", 0700);
2739 if (net_vde_init(vlan
, "vde", name
, sock
, port
, group
, mode
) == -1) {
2743 vlan
->nb_host_devs
++;
2749 static int net_init_dump(QemuOpts
*opts
, Monitor
*mon
)
2757 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2759 name
= qemu_opt_get(opts
, "name");
2761 file
= qemu_opt_get(opts
, "file");
2763 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", vlan
->id
);
2767 len
= qemu_opt_get_size(opts
, "len", 65536);
2769 return net_dump_init(vlan
, "dump", name
, file
, len
);
2772 #define NET_COMMON_PARAMS_DESC \
2775 .type = QEMU_OPT_STRING, \
2776 .help = "net client type (nic, tap etc.)", \
2779 .type = QEMU_OPT_NUMBER, \
2780 .help = "vlan number", \
2783 .type = QEMU_OPT_STRING, \
2784 .help = "identifier for monitor commands", \
2787 typedef int (*net_client_init_func
)(QemuOpts
*opts
, Monitor
*mon
);
2789 /* magic number, but compiler will warn if too small */
2790 #define NET_MAX_DESC 20
2794 net_client_init_func init
;
2795 QemuOptDesc desc
[NET_MAX_DESC
];
2796 } net_client_types
[] = {
2800 NET_COMMON_PARAMS_DESC
,
2801 { /* end of list */ }
2805 .init
= net_init_nic
,
2807 NET_COMMON_PARAMS_DESC
,
2810 .type
= QEMU_OPT_STRING
,
2811 .help
= "MAC address",
2814 .type
= QEMU_OPT_STRING
,
2815 .help
= "device model (e1000, rtl8139, virtio etc.)",
2818 .type
= QEMU_OPT_STRING
,
2819 .help
= "PCI device address",
2822 .type
= QEMU_OPT_NUMBER
,
2823 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
2825 { /* end of list */ }
2830 .init
= net_init_slirp
,
2832 NET_COMMON_PARAMS_DESC
,
2835 .type
= QEMU_OPT_STRING
,
2836 .help
= "client hostname reported by the builtin DHCP server",
2839 .type
= QEMU_OPT_STRING
,
2840 .help
= "isolate the guest from the host (y|yes|n|no)",
2843 .type
= QEMU_OPT_STRING
,
2844 .help
= "legacy parameter, use net= instead",
2847 .type
= QEMU_OPT_STRING
,
2848 .help
= "IP address and optional netmask",
2851 .type
= QEMU_OPT_STRING
,
2852 .help
= "guest-visible address of the host",
2855 .type
= QEMU_OPT_STRING
,
2856 .help
= "root directory of the built-in TFTP server",
2859 .type
= QEMU_OPT_STRING
,
2860 .help
= "BOOTP filename, for use with tftp=",
2862 .name
= "dhcpstart",
2863 .type
= QEMU_OPT_STRING
,
2864 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
2867 .type
= QEMU_OPT_STRING
,
2868 .help
= "guest-visible address of the virtual nameserver",
2871 .type
= QEMU_OPT_STRING
,
2872 .help
= "root directory of the built-in SMB server",
2874 .name
= "smbserver",
2875 .type
= QEMU_OPT_STRING
,
2876 .help
= "IP address of the built-in SMB server",
2879 .type
= QEMU_OPT_STRING
,
2880 .help
= "guest port number to forward incoming TCP or UDP connections",
2883 .type
= QEMU_OPT_STRING
,
2884 .help
= "IP address and port to forward guest TCP connections",
2886 { /* end of list */ }
2892 .init
= net_init_tap_win32
,
2894 NET_COMMON_PARAMS_DESC
,
2897 .type
= QEMU_OPT_STRING
,
2898 .help
= "interface name",
2900 { /* end of list */ }
2902 #elif !defined(_AIX)
2905 .init
= net_init_tap
,
2907 NET_COMMON_PARAMS_DESC
,
2910 .type
= QEMU_OPT_STRING
,
2911 .help
= "file descriptor of an already opened tap",
2914 .type
= QEMU_OPT_STRING
,
2915 .help
= "interface name",
2918 .type
= QEMU_OPT_STRING
,
2919 .help
= "script to initialize the interface",
2921 .name
= "downscript",
2922 .type
= QEMU_OPT_STRING
,
2923 .help
= "script to shut down the interface",
2927 .type
= QEMU_OPT_SIZE
,
2928 .help
= "send buffer limit"
2931 { /* end of list */ }
2936 .init
= net_init_socket
,
2938 NET_COMMON_PARAMS_DESC
,
2941 .type
= QEMU_OPT_STRING
,
2942 .help
= "file descriptor of an already opened socket",
2945 .type
= QEMU_OPT_STRING
,
2946 .help
= "port number, and optional hostname, to listen on",
2949 .type
= QEMU_OPT_STRING
,
2950 .help
= "port number, and optional hostname, to connect to",
2953 .type
= QEMU_OPT_STRING
,
2954 .help
= "UDP multicast address and port number",
2956 { /* end of list */ }
2961 .init
= net_init_vde
,
2963 NET_COMMON_PARAMS_DESC
,
2966 .type
= QEMU_OPT_STRING
,
2967 .help
= "socket path",
2970 .type
= QEMU_OPT_NUMBER
,
2971 .help
= "port number",
2974 .type
= QEMU_OPT_STRING
,
2975 .help
= "group owner of socket",
2978 .type
= QEMU_OPT_NUMBER
,
2979 .help
= "permissions for socket",
2981 { /* end of list */ }
2986 .init
= net_init_dump
,
2988 NET_COMMON_PARAMS_DESC
,
2991 .type
= QEMU_OPT_SIZE
,
2992 .help
= "per-packet size limit (64k default)",
2995 .type
= QEMU_OPT_STRING
,
2996 .help
= "dump file path (default is qemu-vlan0.pcap)",
2998 { /* end of list */ }
3001 { /* end of list */ }
3004 int net_client_init(Monitor
*mon
, QemuOpts
*opts
)
3009 type
= qemu_opt_get(opts
, "type");
3011 qemu_error("No type specified for -net\n");
3015 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
3016 if (!strcmp(net_client_types
[i
].type
, type
)) {
3017 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
3021 if (net_client_types
[i
].init
) {
3022 return net_client_types
[i
].init(opts
, NULL
);
3029 qemu_error("Invalid -net type '%s'\n", type
);
3033 void net_client_uninit(NICInfo
*nd
)
3035 nd
->vlan
->nb_guest_devs
--;
3038 qemu_free(nd
->model
);
3039 qemu_free(nd
->name
);
3040 qemu_free(nd
->devaddr
);
3046 static int net_host_check_device(const char *device
)
3049 const char *valid_param_list
[] = { "tap", "socket", "dump"
3057 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
3058 if (!strncmp(valid_param_list
[i
], device
,
3059 strlen(valid_param_list
[i
])))
3066 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
3068 const char *device
= qdict_get_str(qdict
, "device");
3069 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
3072 if (!net_host_check_device(device
)) {
3073 monitor_printf(mon
, "invalid host network device %s\n", device
);
3077 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
3079 monitor_printf(mon
, "parsing network options '%s' failed\n",
3080 opts_str
? opts_str
: "");
3084 qemu_opt_set(opts
, "type", device
);
3086 if (net_client_init(mon
, opts
) < 0) {
3087 monitor_printf(mon
, "adding host network device %s failed\n", device
);
3091 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
3093 VLANClientState
*vc
;
3094 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
3095 const char *device
= qdict_get_str(qdict
, "device");
3097 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
3101 if (!net_host_check_device(vc
->model
)) {
3102 monitor_printf(mon
, "invalid host network device %s\n", device
);
3105 qemu_del_vlan_client(vc
);
3108 void net_set_boot_mask(int net_boot_mask
)
3112 /* Only the first four NICs may be bootable */
3113 net_boot_mask
= net_boot_mask
& 0xF;
3115 for (i
= 0; i
< nb_nics
; i
++) {
3116 if (net_boot_mask
& (1 << i
)) {
3117 nd_table
[i
].bootable
= 1;
3118 net_boot_mask
&= ~(1 << i
);
3122 if (net_boot_mask
) {
3123 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
3128 void do_info_network(Monitor
*mon
)
3131 VLANClientState
*vc
;
3133 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3134 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
3135 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3136 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
3140 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
3143 VLANClientState
*vc
= NULL
;
3144 const char *name
= qdict_get_str(qdict
, "name");
3145 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
3147 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
3148 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
3149 if (strcmp(vc
->name
, name
) == 0)
3154 monitor_printf(mon
, "could not find network device '%s'\n", name
);
3158 if (strcmp(up_or_down
, "up") == 0)
3160 else if (strcmp(up_or_down
, "down") == 0)
3163 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
3164 "valid\n", up_or_down
);
3166 if (vc
->link_status_changed
)
3167 vc
->link_status_changed(vc
);
3170 void net_cleanup(void)
3174 /* close network clients */
3175 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3176 VLANClientState
*vc
= vlan
->first_client
;
3179 VLANClientState
*next
= vc
->next
;
3181 qemu_del_vlan_client(vc
);
3188 static void net_check_clients(void)
3192 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
3193 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
3195 if (vlan
->nb_guest_devs
== 0)
3196 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
3197 if (vlan
->nb_host_devs
== 0)
3199 "Warning: vlan %d is not connected to host network\n",
3204 static int net_init_client(QemuOpts
*opts
, void *dummy
)
3206 return net_client_init(NULL
, opts
);
3209 int net_init_clients(void)
3211 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
3212 /* if no clients, we use a default config */
3213 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
3215 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
3219 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
3223 net_check_clients();
3228 int net_client_parse(const char *optarg
)
3230 /* handle legacy -net channel,port:chr */
3231 if (!strncmp(optarg
, "channel,", strlen("channel,"))) {
3234 optarg
+= strlen("channel,");
3236 if (QTAILQ_EMPTY(&slirp_stacks
)) {
3237 struct slirp_config_str
*config
;
3239 config
= qemu_malloc(sizeof(*config
));
3240 pstrcpy(config
->str
, sizeof(config
->str
), optarg
);
3241 config
->flags
= SLIRP_CFG_LEGACY
;
3242 config
->next
= slirp_configs
;
3243 slirp_configs
= config
;
3246 ret
= slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks
), optarg
, 1);
3252 if (!qemu_opts_parse(&qemu_net_opts
, optarg
, "type")) {