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
26 #include "config-host.h"
29 #include "net/socket.h"
31 #include "net/slirp.h"
36 #include "qemu-common.h"
37 #include "qemu_socket.h"
40 static QTAILQ_HEAD(, VLANState
) vlans
;
41 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
45 /***********************************************************/
46 /* network device redirectors */
48 #if defined(DEBUG_NET)
49 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
53 for(i
=0;i
<size
;i
+=16) {
57 fprintf(f
, "%08x ", i
);
60 fprintf(f
, " %02x", buf
[i
+j
]);
67 if (c
< ' ' || c
> '~')
76 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
87 if (len
> buf_size
- 1)
96 int parse_host_src_port(struct sockaddr_in
*haddr
,
97 struct sockaddr_in
*saddr
,
98 const char *input_str
)
100 char *str
= qemu_strdup(input_str
);
101 char *host_str
= str
;
103 const char *src_str2
;
107 * Chop off any extra arguments at the end of the string which
108 * would start with a comma, then fill in the src port information
109 * if it was provided else use the "any address" and "any port".
111 if ((ptr
= strchr(str
,',')))
114 if ((src_str
= strchr(input_str
,'@'))) {
119 if (parse_host_port(haddr
, host_str
) < 0)
123 if (!src_str
|| *src_str
== '\0')
126 if (parse_host_port(saddr
, src_str2
) < 0)
137 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
145 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
147 saddr
->sin_family
= AF_INET
;
148 if (buf
[0] == '\0') {
149 saddr
->sin_addr
.s_addr
= 0;
151 if (qemu_isdigit(buf
[0])) {
152 if (!inet_aton(buf
, &saddr
->sin_addr
))
155 if ((he
= gethostbyname(buf
)) == NULL
)
157 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
160 port
= strtol(p
, (char **)&r
, 0);
163 saddr
->sin_port
= htons(port
);
167 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
169 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
170 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
172 macaddr
[0], macaddr
[1], macaddr
[2],
173 macaddr
[3], macaddr
[4], macaddr
[5]);
176 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
178 static int index
= 0;
179 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
181 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
183 macaddr
->a
[0] = 0x52;
184 macaddr
->a
[1] = 0x54;
185 macaddr
->a
[2] = 0x00;
186 macaddr
->a
[3] = 0x12;
187 macaddr
->a
[4] = 0x34;
188 macaddr
->a
[5] = 0x56 + index
++;
191 static char *assign_name(VLANClientState
*vc1
, const char *model
)
197 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
200 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
201 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
207 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
209 return qemu_strdup(buf
);
212 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
217 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
219 const struct iovec
*iov
,
223 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
225 VLANClientState
*peer
,
231 assert(info
->size
>= sizeof(VLANClientState
));
233 vc
= qemu_mallocz(info
->size
);
236 vc
->model
= qemu_strdup(model
);
238 vc
->name
= qemu_strdup(name
);
240 vc
->name
= assign_name(vc
, model
);
246 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
253 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
255 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
256 qemu_deliver_packet_iov
,
263 NICState
*qemu_new_nic(NetClientInfo
*info
,
272 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
273 assert(info
->size
>= sizeof(NICState
));
275 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
277 nic
= DO_UPCAST(NICState
, nc
, nc
);
279 nic
->opaque
= opaque
;
284 void qemu_del_vlan_client(VLANClientState
*vc
)
287 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
289 if (vc
->send_queue
) {
290 qemu_del_net_queue(vc
->send_queue
);
292 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
294 vc
->peer
->peer
= NULL
;
298 if (vc
->info
->cleanup
) {
299 vc
->info
->cleanup(vc
);
303 qemu_free(vc
->model
);
308 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
309 const char *client_str
)
314 vlan
= qemu_find_vlan(vlan_id
, 0);
316 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
320 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
321 if (!strcmp(vc
->name
, client_str
)) {
326 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
327 client_str
, vlan_id
);
333 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
338 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
339 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
340 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
344 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
345 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
346 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
347 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
353 int qemu_can_send_packet(VLANClientState
*sender
)
355 VLANState
*vlan
= sender
->vlan
;
359 if (sender
->peer
->receive_disabled
) {
361 } else if (sender
->peer
->info
->can_receive
&&
362 !sender
->peer
->info
->can_receive(sender
->peer
)) {
373 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
378 /* no can_receive() handler, they can always receive */
379 if (!vc
->info
->can_receive
|| vc
->info
->can_receive(vc
)) {
386 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
392 VLANClientState
*vc
= opaque
;
399 if (vc
->receive_disabled
) {
403 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
404 ret
= vc
->info
->receive_raw(vc
, data
, size
);
406 ret
= vc
->info
->receive(vc
, data
, size
);
410 vc
->receive_disabled
= 1;
416 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
422 VLANState
*vlan
= opaque
;
426 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
438 if (vc
->receive_disabled
) {
443 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
444 len
= vc
->info
->receive_raw(vc
, buf
, size
);
446 len
= vc
->info
->receive(vc
, buf
, size
);
450 vc
->receive_disabled
= 1;
453 ret
= (ret
>= 0) ? ret
: len
;
460 void qemu_purge_queued_packets(VLANClientState
*vc
)
464 if (!vc
->peer
&& !vc
->vlan
) {
469 queue
= vc
->peer
->send_queue
;
471 queue
= vc
->vlan
->send_queue
;
474 qemu_net_queue_purge(queue
, vc
);
477 void qemu_flush_queued_packets(VLANClientState
*vc
)
481 vc
->receive_disabled
= 0;
484 queue
= vc
->vlan
->send_queue
;
486 queue
= vc
->send_queue
;
489 qemu_net_queue_flush(queue
);
492 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
494 const uint8_t *buf
, int size
,
495 NetPacketSent
*sent_cb
)
500 printf("qemu_send_packet_async:\n");
501 hex_dump(stdout
, buf
, size
);
504 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
509 queue
= sender
->peer
->send_queue
;
511 queue
= sender
->vlan
->send_queue
;
514 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
517 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
518 const uint8_t *buf
, int size
,
519 NetPacketSent
*sent_cb
)
521 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
525 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
527 qemu_send_packet_async(vc
, buf
, size
, NULL
);
530 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
532 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
536 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
539 uint8_t buffer
[4096];
543 for (i
= 0; i
< iovcnt
; i
++) {
546 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
547 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
551 return vc
->info
->receive(vc
, buffer
, offset
);
554 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
559 for (i
= 0; i
< iovcnt
; i
++)
560 offset
+= iov
[i
].iov_len
;
564 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
566 const struct iovec
*iov
,
570 VLANClientState
*vc
= opaque
;
573 return calc_iov_length(iov
, iovcnt
);
576 if (vc
->info
->receive_iov
) {
577 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
579 return vc_sendv_compat(vc
, iov
, iovcnt
);
583 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
585 const struct iovec
*iov
,
589 VLANState
*vlan
= opaque
;
593 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
601 ret
= calc_iov_length(iov
, iovcnt
);
605 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
607 if (vc
->info
->receive_iov
) {
608 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
610 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
613 ret
= (ret
>= 0) ? ret
: len
;
619 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
620 const struct iovec
*iov
, int iovcnt
,
621 NetPacketSent
*sent_cb
)
625 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
626 return calc_iov_length(iov
, iovcnt
);
630 queue
= sender
->peer
->send_queue
;
632 queue
= sender
->vlan
->send_queue
;
635 return qemu_net_queue_send_iov(queue
, sender
,
636 QEMU_NET_PACKET_FLAG_NONE
,
637 iov
, iovcnt
, sent_cb
);
641 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
643 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
646 /* find or alloc a new VLAN */
647 VLANState
*qemu_find_vlan(int id
, int allocate
)
651 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
652 if (vlan
->id
== id
) {
661 vlan
= qemu_mallocz(sizeof(VLANState
));
663 QTAILQ_INIT(&vlan
->clients
);
665 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
666 qemu_vlan_deliver_packet_iov
,
669 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
674 VLANClientState
*qemu_find_netdev(const char *id
)
678 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
679 if (!strcmp(vc
->name
, id
)) {
687 static int nic_get_free_idx(void)
691 for (index
= 0; index
< MAX_NICS
; index
++)
692 if (!nd_table
[index
].used
)
697 int qemu_show_nic_models(const char *arg
, const char *const *models
)
701 if (!arg
|| strcmp(arg
, "?"))
704 fprintf(stderr
, "qemu: Supported NIC models: ");
705 for (i
= 0 ; models
[i
]; i
++)
706 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
710 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
712 const char *models
[2];
717 if (qemu_show_nic_models(nd
->model
, models
))
719 if (qemu_find_nic_model(nd
, models
, model
) < 0)
723 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
724 const char *default_model
)
729 nd
->model
= qemu_strdup(default_model
);
731 for (i
= 0 ; models
[i
]; i
++) {
732 if (strcmp(nd
->model
, models
[i
]) == 0)
736 error_report("qemu: Unsupported NIC model: %s", nd
->model
);
740 int net_handle_fd_param(Monitor
*mon
, const char *param
)
742 if (!qemu_isdigit(param
[0])) {
745 fd
= monitor_get_fd(mon
, param
);
747 error_report("No file descriptor named %s found", param
);
753 return strtol(param
, NULL
, 0);
757 static int net_init_nic(QemuOpts
*opts
,
766 idx
= nic_get_free_idx();
767 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
768 error_report("Too Many NICs");
774 memset(nd
, 0, sizeof(*nd
));
776 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
777 nd
->netdev
= qemu_find_netdev(netdev
);
779 error_report("netdev '%s' not found", netdev
);
787 nd
->name
= qemu_strdup(name
);
789 if (qemu_opt_get(opts
, "model")) {
790 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
792 if (qemu_opt_get(opts
, "addr")) {
793 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
796 nd
->macaddr
[0] = 0x52;
797 nd
->macaddr
[1] = 0x54;
798 nd
->macaddr
[2] = 0x00;
799 nd
->macaddr
[3] = 0x12;
800 nd
->macaddr
[4] = 0x34;
801 nd
->macaddr
[5] = 0x56 + idx
;
803 if (qemu_opt_get(opts
, "macaddr") &&
804 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
805 error_report("invalid syntax for ethernet address");
809 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
810 DEV_NVECTORS_UNSPECIFIED
);
811 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
812 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
813 error_report("invalid # of vectors: %d", nd
->nvectors
);
823 #define NET_COMMON_PARAMS_DESC \
826 .type = QEMU_OPT_STRING, \
827 .help = "net client type (nic, tap etc.)", \
830 .type = QEMU_OPT_NUMBER, \
831 .help = "vlan number", \
834 .type = QEMU_OPT_STRING, \
835 .help = "identifier for monitor commands", \
838 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
843 /* magic number, but compiler will warn if too small */
844 #define NET_MAX_DESC 20
846 static const struct {
848 net_client_init_func init
;
849 QemuOptDesc desc
[NET_MAX_DESC
];
850 } net_client_types
[] = {
854 NET_COMMON_PARAMS_DESC
,
855 { /* end of list */ }
859 .init
= net_init_nic
,
861 NET_COMMON_PARAMS_DESC
,
864 .type
= QEMU_OPT_STRING
,
865 .help
= "id of -netdev to connect to",
869 .type
= QEMU_OPT_STRING
,
870 .help
= "MAC address",
873 .type
= QEMU_OPT_STRING
,
874 .help
= "device model (e1000, rtl8139, virtio etc.)",
877 .type
= QEMU_OPT_STRING
,
878 .help
= "PCI device address",
881 .type
= QEMU_OPT_NUMBER
,
882 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
884 { /* end of list */ }
889 .init
= net_init_slirp
,
891 NET_COMMON_PARAMS_DESC
,
894 .type
= QEMU_OPT_STRING
,
895 .help
= "client hostname reported by the builtin DHCP server",
898 .type
= QEMU_OPT_STRING
,
899 .help
= "isolate the guest from the host (y|yes|n|no)",
902 .type
= QEMU_OPT_STRING
,
903 .help
= "legacy parameter, use net= instead",
906 .type
= QEMU_OPT_STRING
,
907 .help
= "IP address and optional netmask",
910 .type
= QEMU_OPT_STRING
,
911 .help
= "guest-visible address of the host",
914 .type
= QEMU_OPT_STRING
,
915 .help
= "root directory of the built-in TFTP server",
918 .type
= QEMU_OPT_STRING
,
919 .help
= "BOOTP filename, for use with tftp=",
922 .type
= QEMU_OPT_STRING
,
923 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
926 .type
= QEMU_OPT_STRING
,
927 .help
= "guest-visible address of the virtual nameserver",
930 .type
= QEMU_OPT_STRING
,
931 .help
= "root directory of the built-in SMB server",
934 .type
= QEMU_OPT_STRING
,
935 .help
= "IP address of the built-in SMB server",
938 .type
= QEMU_OPT_STRING
,
939 .help
= "guest port number to forward incoming TCP or UDP connections",
942 .type
= QEMU_OPT_STRING
,
943 .help
= "IP address and port to forward guest TCP connections",
945 { /* end of list */ }
950 .init
= net_init_tap
,
952 NET_COMMON_PARAMS_DESC
,
955 .type
= QEMU_OPT_STRING
,
956 .help
= "interface name",
961 .type
= QEMU_OPT_STRING
,
962 .help
= "file descriptor of an already opened tap",
965 .type
= QEMU_OPT_STRING
,
966 .help
= "script to initialize the interface",
968 .name
= "downscript",
969 .type
= QEMU_OPT_STRING
,
970 .help
= "script to shut down the interface",
973 .type
= QEMU_OPT_SIZE
,
974 .help
= "send buffer limit"
977 .type
= QEMU_OPT_BOOL
,
978 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
981 .type
= QEMU_OPT_BOOL
,
982 .help
= "enable vhost-net network accelerator",
985 .type
= QEMU_OPT_STRING
,
986 .help
= "file descriptor of an already opened vhost net device",
989 { /* end of list */ }
993 .init
= net_init_socket
,
995 NET_COMMON_PARAMS_DESC
,
998 .type
= QEMU_OPT_STRING
,
999 .help
= "file descriptor of an already opened socket",
1002 .type
= QEMU_OPT_STRING
,
1003 .help
= "port number, and optional hostname, to listen on",
1006 .type
= QEMU_OPT_STRING
,
1007 .help
= "port number, and optional hostname, to connect to",
1010 .type
= QEMU_OPT_STRING
,
1011 .help
= "UDP multicast address and port number",
1013 { /* end of list */ }
1018 .init
= net_init_vde
,
1020 NET_COMMON_PARAMS_DESC
,
1023 .type
= QEMU_OPT_STRING
,
1024 .help
= "socket path",
1027 .type
= QEMU_OPT_NUMBER
,
1028 .help
= "port number",
1031 .type
= QEMU_OPT_STRING
,
1032 .help
= "group owner of socket",
1035 .type
= QEMU_OPT_NUMBER
,
1036 .help
= "permissions for socket",
1038 { /* end of list */ }
1043 .init
= net_init_dump
,
1045 NET_COMMON_PARAMS_DESC
,
1048 .type
= QEMU_OPT_SIZE
,
1049 .help
= "per-packet size limit (64k default)",
1052 .type
= QEMU_OPT_STRING
,
1053 .help
= "dump file path (default is qemu-vlan0.pcap)",
1055 { /* end of list */ }
1058 { /* end of list */ }
1061 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1067 type
= qemu_opt_get(opts
, "type");
1069 qerror_report(QERR_MISSING_PARAMETER
, "type");
1074 if (strcmp(type
, "tap") != 0 &&
1076 strcmp(type
, "user") != 0 &&
1079 strcmp(type
, "vde") != 0 &&
1081 strcmp(type
, "socket") != 0) {
1082 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1083 "a netdev backend type");
1087 if (qemu_opt_get(opts
, "vlan")) {
1088 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1091 if (qemu_opt_get(opts
, "name")) {
1092 qerror_report(QERR_INVALID_PARAMETER
, "name");
1095 if (!qemu_opts_id(opts
)) {
1096 qerror_report(QERR_MISSING_PARAMETER
, "id");
1101 name
= qemu_opts_id(opts
);
1103 name
= qemu_opt_get(opts
, "name");
1106 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1107 if (!strcmp(net_client_types
[i
].type
, type
)) {
1108 VLANState
*vlan
= NULL
;
1111 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1115 /* Do not add to a vlan if it's a -netdev or a nic with a
1116 * netdev= parameter. */
1118 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1119 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1123 if (net_client_types
[i
].init
) {
1124 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1126 /* TODO push error reporting into init() methods */
1127 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1135 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1136 "a network client type");
1140 static int net_host_check_device(const char *device
)
1143 const char *valid_param_list
[] = { "tap", "socket", "dump"
1151 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1152 if (!strncmp(valid_param_list
[i
], device
,
1153 strlen(valid_param_list
[i
])))
1160 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1162 const char *device
= qdict_get_str(qdict
, "device");
1163 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1166 if (!net_host_check_device(device
)) {
1167 monitor_printf(mon
, "invalid host network device %s\n", device
);
1171 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", 0);
1176 qemu_opt_set(opts
, "type", device
);
1178 if (net_client_init(mon
, opts
, 0) < 0) {
1179 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1183 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1185 VLANClientState
*vc
;
1186 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1187 const char *device
= qdict_get_str(qdict
, "device");
1189 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1193 if (!net_host_check_device(vc
->model
)) {
1194 monitor_printf(mon
, "invalid host network device %s\n", device
);
1197 qemu_del_vlan_client(vc
);
1200 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1205 opts
= qemu_opts_from_qdict(&qemu_netdev_opts
, qdict
);
1210 res
= net_client_init(mon
, opts
, 1);
1214 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1216 const char *id
= qdict_get_str(qdict
, "id");
1217 VLANClientState
*vc
;
1219 vc
= qemu_find_netdev(id
);
1220 if (!vc
|| vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1221 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1224 qemu_del_vlan_client(vc
);
1225 qemu_opts_del(qemu_opts_find(&qemu_netdev_opts
, id
));
1229 void do_info_network(Monitor
*mon
)
1232 VLANClientState
*vc
;
1234 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1235 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1237 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1238 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1241 monitor_printf(mon
, "Devices not on any VLAN:\n");
1242 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1243 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1245 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1247 monitor_printf(mon
, "\n");
1251 int do_set_link(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1254 VLANClientState
*vc
= NULL
;
1255 const char *name
= qdict_get_str(qdict
, "name");
1256 int up
= qdict_get_bool(qdict
, "up");
1258 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1259 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1260 if (strcmp(vc
->name
, name
) == 0) {
1265 vc
= qemu_find_netdev(name
);
1269 qerror_report(QERR_DEVICE_NOT_FOUND
, name
);
1273 vc
->link_down
= !up
;
1275 if (vc
->info
->link_status_changed
) {
1276 vc
->info
->link_status_changed(vc
);
1281 void net_cleanup(void)
1284 VLANClientState
*vc
, *next_vc
;
1286 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1287 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1288 qemu_del_vlan_client(vc
);
1292 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1293 qemu_del_vlan_client(vc
);
1297 void net_check_clients(void)
1300 VLANClientState
*vc
;
1301 int has_nic
= 0, has_host_dev
= 0;
1303 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1304 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1305 switch (vc
->info
->type
) {
1306 case NET_CLIENT_TYPE_NIC
:
1309 case NET_CLIENT_TYPE_SLIRP
:
1310 case NET_CLIENT_TYPE_TAP
:
1311 case NET_CLIENT_TYPE_SOCKET
:
1312 case NET_CLIENT_TYPE_VDE
:
1318 if (has_host_dev
&& !has_nic
)
1319 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1320 if (has_nic
&& !has_host_dev
)
1322 "Warning: vlan %d is not connected to host network\n",
1325 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1327 fprintf(stderr
, "Warning: %s %s has no peer\n",
1328 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1334 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1336 if (net_client_init(NULL
, opts
, 0) < 0)
1341 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1343 return net_client_init(NULL
, opts
, 1);
1346 int net_init_clients(void)
1349 /* if no clients, we use a default config */
1350 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1352 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1356 QTAILQ_INIT(&vlans
);
1357 QTAILQ_INIT(&non_vlan_clients
);
1359 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1362 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1369 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1371 #if defined(CONFIG_SLIRP)
1373 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1378 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {