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"
39 static QTAILQ_HEAD(, VLANState
) vlans
;
40 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
42 /***********************************************************/
43 /* network device redirectors */
45 #if defined(DEBUG_NET)
46 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
50 for(i
=0;i
<size
;i
+=16) {
54 fprintf(f
, "%08x ", i
);
57 fprintf(f
, " %02x", buf
[i
+j
]);
64 if (c
< ' ' || c
> '~')
73 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
84 if (len
> buf_size
- 1)
93 int parse_host_src_port(struct sockaddr_in
*haddr
,
94 struct sockaddr_in
*saddr
,
95 const char *input_str
)
97 char *str
= strdup(input_str
);
100 const char *src_str2
;
104 * Chop off any extra arguments at the end of the string which
105 * would start with a comma, then fill in the src port information
106 * if it was provided else use the "any address" and "any port".
108 if ((ptr
= strchr(str
,',')))
111 if ((src_str
= strchr(input_str
,'@'))) {
116 if (parse_host_port(haddr
, host_str
) < 0)
120 if (!src_str
|| *src_str
== '\0')
123 if (parse_host_port(saddr
, src_str2
) < 0)
134 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
142 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
144 saddr
->sin_family
= AF_INET
;
145 if (buf
[0] == '\0') {
146 saddr
->sin_addr
.s_addr
= 0;
148 if (qemu_isdigit(buf
[0])) {
149 if (!inet_aton(buf
, &saddr
->sin_addr
))
152 if ((he
= gethostbyname(buf
)) == NULL
)
154 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
157 port
= strtol(p
, (char **)&r
, 0);
160 saddr
->sin_port
= htons(port
);
164 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
166 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
167 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
169 macaddr
[0], macaddr
[1], macaddr
[2],
170 macaddr
[3], macaddr
[4], macaddr
[5]);
173 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
175 static int index
= 0;
176 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
178 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
180 macaddr
->a
[0] = 0x52;
181 macaddr
->a
[1] = 0x54;
182 macaddr
->a
[2] = 0x00;
183 macaddr
->a
[3] = 0x12;
184 macaddr
->a
[4] = 0x34;
185 macaddr
->a
[5] = 0x56 + index
++;
188 static char *assign_name(VLANClientState
*vc1
, const char *model
)
194 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
197 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
198 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
204 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
206 return qemu_strdup(buf
);
209 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
214 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
216 const struct iovec
*iov
,
220 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
222 VLANClientState
*peer
,
228 assert(info
->size
>= sizeof(VLANClientState
));
230 vc
= qemu_mallocz(info
->size
);
232 vc
->type
= info
->type
;
233 vc
->model
= qemu_strdup(model
);
235 vc
->name
= qemu_strdup(name
);
237 vc
->name
= assign_name(vc
, model
);
239 vc
->can_receive
= info
->can_receive
;
240 vc
->receive
= info
->receive
;
241 vc
->receive_raw
= info
->receive_raw
;
242 vc
->receive_iov
= info
->receive_iov
;
243 vc
->cleanup
= info
->cleanup
;
244 vc
->link_status_changed
= info
->link_status_changed
;
249 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
255 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
257 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
258 qemu_deliver_packet_iov
,
265 NICState
*qemu_new_nic(NetClientInfo
*info
,
274 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
275 assert(info
->size
>= sizeof(NICState
));
277 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
279 nic
= DO_UPCAST(NICState
, nc
, nc
);
281 nic
->opaque
= opaque
;
286 VLANClientState
*qemu_new_vlan_client(net_client_type type
,
288 VLANClientState
*peer
,
291 NetCanReceive
*can_receive
,
293 NetReceive
*receive_raw
,
294 NetReceiveIOV
*receive_iov
,
298 VLANClientState
*ret
;
302 info
.size
= sizeof(VLANClientState
);
303 info
.can_receive
= can_receive
;
304 info
.receive
= receive
;
305 info
.receive_raw
= receive_raw
;
306 info
.receive_iov
= receive_iov
;
307 info
.cleanup
= cleanup
;
308 info
.link_status_changed
= NULL
;
310 ret
= qemu_new_net_client(&info
, vlan
, peer
, model
, name
);
312 ret
->opaque
= opaque
;
317 void qemu_del_vlan_client(VLANClientState
*vc
)
320 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
322 if (vc
->send_queue
) {
323 qemu_del_net_queue(vc
->send_queue
);
325 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
327 vc
->peer
->peer
= NULL
;
336 qemu_free(vc
->model
);
340 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
344 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
345 if (vc
->opaque
== opaque
) {
354 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
355 const char *client_str
)
360 vlan
= qemu_find_vlan(vlan_id
, 0);
362 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
366 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
367 if (!strcmp(vc
->name
, client_str
)) {
372 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
373 client_str
, vlan_id
);
379 int qemu_can_send_packet(VLANClientState
*sender
)
381 VLANState
*vlan
= sender
->vlan
;
385 if (sender
->peer
->receive_disabled
) {
387 } else if (sender
->peer
->can_receive
&&
388 !sender
->peer
->can_receive(sender
->peer
)) {
399 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
404 /* no can_receive() handler, they can always receive */
405 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
412 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
418 VLANClientState
*vc
= opaque
;
425 if (vc
->receive_disabled
) {
429 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
430 ret
= vc
->receive_raw(vc
, data
, size
);
432 ret
= vc
->receive(vc
, data
, size
);
436 vc
->receive_disabled
= 1;
442 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
448 VLANState
*vlan
= opaque
;
452 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
464 if (vc
->receive_disabled
) {
469 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
470 len
= vc
->receive_raw(vc
, buf
, size
);
472 len
= vc
->receive(vc
, buf
, size
);
476 vc
->receive_disabled
= 1;
479 ret
= (ret
>= 0) ? ret
: len
;
486 void qemu_purge_queued_packets(VLANClientState
*vc
)
490 if (!vc
->peer
&& !vc
->vlan
) {
495 queue
= vc
->peer
->send_queue
;
497 queue
= vc
->vlan
->send_queue
;
500 qemu_net_queue_purge(queue
, vc
);
503 void qemu_flush_queued_packets(VLANClientState
*vc
)
507 vc
->receive_disabled
= 0;
510 queue
= vc
->vlan
->send_queue
;
512 queue
= vc
->send_queue
;
515 qemu_net_queue_flush(queue
);
518 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
520 const uint8_t *buf
, int size
,
521 NetPacketSent
*sent_cb
)
526 printf("qemu_send_packet_async:\n");
527 hex_dump(stdout
, buf
, size
);
530 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
535 queue
= sender
->peer
->send_queue
;
537 queue
= sender
->vlan
->send_queue
;
540 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
543 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
544 const uint8_t *buf
, int size
,
545 NetPacketSent
*sent_cb
)
547 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
551 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
553 qemu_send_packet_async(vc
, buf
, size
, NULL
);
556 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
558 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
562 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
565 uint8_t buffer
[4096];
569 for (i
= 0; i
< iovcnt
; i
++) {
572 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
573 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
577 return vc
->receive(vc
, buffer
, offset
);
580 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
585 for (i
= 0; i
< iovcnt
; i
++)
586 offset
+= iov
[i
].iov_len
;
590 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
592 const struct iovec
*iov
,
596 VLANClientState
*vc
= opaque
;
599 return calc_iov_length(iov
, iovcnt
);
602 if (vc
->receive_iov
) {
603 return vc
->receive_iov(vc
, iov
, iovcnt
);
605 return vc_sendv_compat(vc
, iov
, iovcnt
);
609 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
611 const struct iovec
*iov
,
615 VLANState
*vlan
= opaque
;
619 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
627 ret
= calc_iov_length(iov
, iovcnt
);
631 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
633 if (vc
->receive_iov
) {
634 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
636 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
639 ret
= (ret
>= 0) ? ret
: len
;
645 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
646 const struct iovec
*iov
, int iovcnt
,
647 NetPacketSent
*sent_cb
)
651 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
652 return calc_iov_length(iov
, iovcnt
);
656 queue
= sender
->peer
->send_queue
;
658 queue
= sender
->vlan
->send_queue
;
661 return qemu_net_queue_send_iov(queue
, sender
,
662 QEMU_NET_PACKET_FLAG_NONE
,
663 iov
, iovcnt
, sent_cb
);
667 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
669 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
672 /* find or alloc a new VLAN */
673 VLANState
*qemu_find_vlan(int id
, int allocate
)
677 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
678 if (vlan
->id
== id
) {
687 vlan
= qemu_mallocz(sizeof(VLANState
));
689 QTAILQ_INIT(&vlan
->clients
);
691 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
692 qemu_vlan_deliver_packet_iov
,
695 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
700 VLANClientState
*qemu_find_netdev(const char *id
)
704 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
705 if (!strcmp(vc
->name
, id
)) {
713 static int nic_get_free_idx(void)
717 for (index
= 0; index
< MAX_NICS
; index
++)
718 if (!nd_table
[index
].used
)
723 int qemu_show_nic_models(const char *arg
, const char *const *models
)
727 if (!arg
|| strcmp(arg
, "?"))
730 fprintf(stderr
, "qemu: Supported NIC models: ");
731 for (i
= 0 ; models
[i
]; i
++)
732 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
736 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
738 const char *models
[2];
743 if (qemu_show_nic_models(nd
->model
, models
))
745 if (qemu_find_nic_model(nd
, models
, model
) < 0)
749 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
750 const char *default_model
)
755 nd
->model
= qemu_strdup(default_model
);
757 for (i
= 0 ; models
[i
]; i
++) {
758 if (strcmp(nd
->model
, models
[i
]) == 0)
762 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
766 int net_handle_fd_param(Monitor
*mon
, const char *param
)
768 if (!qemu_isdigit(param
[0])) {
771 fd
= monitor_get_fd(mon
, param
);
773 qemu_error("No file descriptor named %s found", param
);
779 return strtol(param
, NULL
, 0);
783 static int net_init_nic(QemuOpts
*opts
,
792 idx
= nic_get_free_idx();
793 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
794 qemu_error("Too Many NICs\n");
800 memset(nd
, 0, sizeof(*nd
));
802 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
803 nd
->netdev
= qemu_find_netdev(netdev
);
805 qemu_error("netdev '%s' not found\n", netdev
);
813 nd
->name
= qemu_strdup(name
);
815 if (qemu_opt_get(opts
, "model")) {
816 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
818 if (qemu_opt_get(opts
, "addr")) {
819 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
822 nd
->macaddr
[0] = 0x52;
823 nd
->macaddr
[1] = 0x54;
824 nd
->macaddr
[2] = 0x00;
825 nd
->macaddr
[3] = 0x12;
826 nd
->macaddr
[4] = 0x34;
827 nd
->macaddr
[5] = 0x56 + idx
;
829 if (qemu_opt_get(opts
, "macaddr") &&
830 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
831 qemu_error("invalid syntax for ethernet address\n");
835 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
836 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
837 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
838 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
844 nd
->vlan
->nb_guest_devs
++;
851 #define NET_COMMON_PARAMS_DESC \
854 .type = QEMU_OPT_STRING, \
855 .help = "net client type (nic, tap etc.)", \
858 .type = QEMU_OPT_NUMBER, \
859 .help = "vlan number", \
862 .type = QEMU_OPT_STRING, \
863 .help = "identifier for monitor commands", \
866 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
871 /* magic number, but compiler will warn if too small */
872 #define NET_MAX_DESC 20
876 net_client_init_func init
;
877 QemuOptDesc desc
[NET_MAX_DESC
];
878 } net_client_types
[] = {
882 NET_COMMON_PARAMS_DESC
,
883 { /* end of list */ }
887 .init
= net_init_nic
,
889 NET_COMMON_PARAMS_DESC
,
892 .type
= QEMU_OPT_STRING
,
893 .help
= "id of -netdev to connect to",
897 .type
= QEMU_OPT_STRING
,
898 .help
= "MAC address",
901 .type
= QEMU_OPT_STRING
,
902 .help
= "device model (e1000, rtl8139, virtio etc.)",
905 .type
= QEMU_OPT_STRING
,
906 .help
= "PCI device address",
909 .type
= QEMU_OPT_NUMBER
,
910 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
912 { /* end of list */ }
917 .init
= net_init_slirp
,
919 NET_COMMON_PARAMS_DESC
,
922 .type
= QEMU_OPT_STRING
,
923 .help
= "client hostname reported by the builtin DHCP server",
926 .type
= QEMU_OPT_STRING
,
927 .help
= "isolate the guest from the host (y|yes|n|no)",
930 .type
= QEMU_OPT_STRING
,
931 .help
= "legacy parameter, use net= instead",
934 .type
= QEMU_OPT_STRING
,
935 .help
= "IP address and optional netmask",
938 .type
= QEMU_OPT_STRING
,
939 .help
= "guest-visible address of the host",
942 .type
= QEMU_OPT_STRING
,
943 .help
= "root directory of the built-in TFTP server",
946 .type
= QEMU_OPT_STRING
,
947 .help
= "BOOTP filename, for use with tftp=",
950 .type
= QEMU_OPT_STRING
,
951 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
954 .type
= QEMU_OPT_STRING
,
955 .help
= "guest-visible address of the virtual nameserver",
958 .type
= QEMU_OPT_STRING
,
959 .help
= "root directory of the built-in SMB server",
962 .type
= QEMU_OPT_STRING
,
963 .help
= "IP address of the built-in SMB server",
966 .type
= QEMU_OPT_STRING
,
967 .help
= "guest port number to forward incoming TCP or UDP connections",
970 .type
= QEMU_OPT_STRING
,
971 .help
= "IP address and port to forward guest TCP connections",
973 { /* end of list */ }
978 .init
= net_init_tap
,
980 NET_COMMON_PARAMS_DESC
,
983 .type
= QEMU_OPT_STRING
,
984 .help
= "interface name",
989 .type
= QEMU_OPT_STRING
,
990 .help
= "file descriptor of an already opened tap",
993 .type
= QEMU_OPT_STRING
,
994 .help
= "script to initialize the interface",
996 .name
= "downscript",
997 .type
= QEMU_OPT_STRING
,
998 .help
= "script to shut down the interface",
1001 .type
= QEMU_OPT_SIZE
,
1002 .help
= "send buffer limit"
1005 .type
= QEMU_OPT_BOOL
,
1006 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
1009 { /* end of list */ }
1013 .init
= net_init_socket
,
1015 NET_COMMON_PARAMS_DESC
,
1018 .type
= QEMU_OPT_STRING
,
1019 .help
= "file descriptor of an already opened socket",
1022 .type
= QEMU_OPT_STRING
,
1023 .help
= "port number, and optional hostname, to listen on",
1026 .type
= QEMU_OPT_STRING
,
1027 .help
= "port number, and optional hostname, to connect to",
1030 .type
= QEMU_OPT_STRING
,
1031 .help
= "UDP multicast address and port number",
1033 { /* end of list */ }
1038 .init
= net_init_vde
,
1040 NET_COMMON_PARAMS_DESC
,
1043 .type
= QEMU_OPT_STRING
,
1044 .help
= "socket path",
1047 .type
= QEMU_OPT_NUMBER
,
1048 .help
= "port number",
1051 .type
= QEMU_OPT_STRING
,
1052 .help
= "group owner of socket",
1055 .type
= QEMU_OPT_NUMBER
,
1056 .help
= "permissions for socket",
1058 { /* end of list */ }
1063 .init
= net_init_dump
,
1065 NET_COMMON_PARAMS_DESC
,
1068 .type
= QEMU_OPT_SIZE
,
1069 .help
= "per-packet size limit (64k default)",
1072 .type
= QEMU_OPT_STRING
,
1073 .help
= "dump file path (default is qemu-vlan0.pcap)",
1075 { /* end of list */ }
1078 { /* end of list */ }
1081 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1087 type
= qemu_opt_get(opts
, "type");
1089 qemu_error("No type specified for -net\n");
1094 if (strcmp(type
, "tap") != 0 &&
1096 strcmp(type
, "user") != 0 &&
1099 strcmp(type
, "vde") != 0 &&
1101 strcmp(type
, "socket") != 0) {
1102 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1107 if (qemu_opt_get(opts
, "vlan")) {
1108 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1111 if (qemu_opt_get(opts
, "name")) {
1112 qemu_error("The 'name' parameter is not valid with -netdev\n");
1115 if (!qemu_opts_id(opts
)) {
1116 qemu_error("The id= parameter is required with -netdev\n");
1121 name
= qemu_opts_id(opts
);
1123 name
= qemu_opt_get(opts
, "name");
1126 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1127 if (!strcmp(net_client_types
[i
].type
, type
)) {
1128 VLANState
*vlan
= NULL
;
1130 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1134 /* Do not add to a vlan if it's a -netdev or a nic with a
1135 * netdev= parameter. */
1137 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1138 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1141 if (net_client_types
[i
].init
) {
1142 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1149 qemu_error("Invalid -net type '%s'\n", type
);
1153 void net_client_uninit(NICInfo
*nd
)
1156 nd
->vlan
->nb_guest_devs
--;
1160 qemu_free(nd
->model
);
1161 qemu_free(nd
->name
);
1162 qemu_free(nd
->devaddr
);
1167 static int net_host_check_device(const char *device
)
1170 const char *valid_param_list
[] = { "tap", "socket", "dump"
1178 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1179 if (!strncmp(valid_param_list
[i
], device
,
1180 strlen(valid_param_list
[i
])))
1187 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1189 const char *device
= qdict_get_str(qdict
, "device");
1190 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1193 if (!net_host_check_device(device
)) {
1194 monitor_printf(mon
, "invalid host network device %s\n", device
);
1198 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
1200 monitor_printf(mon
, "parsing network options '%s' failed\n",
1201 opts_str
? opts_str
: "");
1205 qemu_opt_set(opts
, "type", device
);
1207 if (net_client_init(mon
, opts
, 0) < 0) {
1208 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1212 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1214 VLANClientState
*vc
;
1215 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1216 const char *device
= qdict_get_str(qdict
, "device");
1218 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1222 if (!net_host_check_device(vc
->model
)) {
1223 monitor_printf(mon
, "invalid host network device %s\n", device
);
1226 qemu_del_vlan_client(vc
);
1229 void net_set_boot_mask(int net_boot_mask
)
1233 /* Only the first four NICs may be bootable */
1234 net_boot_mask
= net_boot_mask
& 0xF;
1236 for (i
= 0; i
< nb_nics
; i
++) {
1237 if (net_boot_mask
& (1 << i
)) {
1238 nd_table
[i
].bootable
= 1;
1239 net_boot_mask
&= ~(1 << i
);
1243 if (net_boot_mask
) {
1244 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
1249 void do_info_network(Monitor
*mon
)
1253 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1254 VLANClientState
*vc
;
1256 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1258 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1259 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1264 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
1267 VLANClientState
*vc
= NULL
;
1268 const char *name
= qdict_get_str(qdict
, "name");
1269 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
1271 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1272 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1273 if (strcmp(vc
->name
, name
) == 0) {
1281 monitor_printf(mon
, "could not find network device '%s'\n", name
);
1285 if (strcmp(up_or_down
, "up") == 0)
1287 else if (strcmp(up_or_down
, "down") == 0)
1290 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
1291 "valid\n", up_or_down
);
1293 if (vc
->link_status_changed
)
1294 vc
->link_status_changed(vc
);
1297 void net_cleanup(void)
1300 VLANClientState
*vc
, *next_vc
;
1302 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1303 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1304 qemu_del_vlan_client(vc
);
1308 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1309 qemu_del_vlan_client(vc
);
1313 static void net_check_clients(void)
1317 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1318 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1320 if (vlan
->nb_guest_devs
== 0)
1321 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1322 if (vlan
->nb_host_devs
== 0)
1324 "Warning: vlan %d is not connected to host network\n",
1329 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1331 if (net_client_init(NULL
, opts
, 0) < 0)
1336 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1338 return net_client_init(NULL
, opts
, 1);
1341 int net_init_clients(void)
1343 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
1344 /* if no clients, we use a default config */
1345 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1347 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1351 QTAILQ_INIT(&vlans
);
1352 QTAILQ_INIT(&non_vlan_clients
);
1354 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1357 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1361 net_check_clients();
1366 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1368 #if defined(CONFIG_SLIRP)
1370 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1375 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {