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
;
44 /***********************************************************/
45 /* network device redirectors */
47 #if defined(DEBUG_NET)
48 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
52 for(i
=0;i
<size
;i
+=16) {
56 fprintf(f
, "%08x ", i
);
59 fprintf(f
, " %02x", buf
[i
+j
]);
66 if (c
< ' ' || c
> '~')
75 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
86 if (len
> buf_size
- 1)
95 int parse_host_src_port(struct sockaddr_in
*haddr
,
96 struct sockaddr_in
*saddr
,
97 const char *input_str
)
99 char *str
= qemu_strdup(input_str
);
100 char *host_str
= str
;
102 const char *src_str2
;
106 * Chop off any extra arguments at the end of the string which
107 * would start with a comma, then fill in the src port information
108 * if it was provided else use the "any address" and "any port".
110 if ((ptr
= strchr(str
,',')))
113 if ((src_str
= strchr(input_str
,'@'))) {
118 if (parse_host_port(haddr
, host_str
) < 0)
122 if (!src_str
|| *src_str
== '\0')
125 if (parse_host_port(saddr
, src_str2
) < 0)
136 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
144 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
146 saddr
->sin_family
= AF_INET
;
147 if (buf
[0] == '\0') {
148 saddr
->sin_addr
.s_addr
= 0;
150 if (qemu_isdigit(buf
[0])) {
151 if (!inet_aton(buf
, &saddr
->sin_addr
))
154 if ((he
= gethostbyname(buf
)) == NULL
)
156 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
159 port
= strtol(p
, (char **)&r
, 0);
162 saddr
->sin_port
= htons(port
);
166 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
168 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
169 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
171 macaddr
[0], macaddr
[1], macaddr
[2],
172 macaddr
[3], macaddr
[4], macaddr
[5]);
175 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
177 static int index
= 0;
178 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
180 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
182 macaddr
->a
[0] = 0x52;
183 macaddr
->a
[1] = 0x54;
184 macaddr
->a
[2] = 0x00;
185 macaddr
->a
[3] = 0x12;
186 macaddr
->a
[4] = 0x34;
187 macaddr
->a
[5] = 0x56 + index
++;
190 static char *assign_name(VLANClientState
*vc1
, const char *model
)
196 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
199 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
200 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
206 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
208 return qemu_strdup(buf
);
211 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
216 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
218 const struct iovec
*iov
,
222 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
224 VLANClientState
*peer
,
230 assert(info
->size
>= sizeof(VLANClientState
));
232 vc
= qemu_mallocz(info
->size
);
235 vc
->model
= qemu_strdup(model
);
237 vc
->name
= qemu_strdup(name
);
239 vc
->name
= assign_name(vc
, model
);
245 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
251 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
253 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
254 qemu_deliver_packet_iov
,
261 NICState
*qemu_new_nic(NetClientInfo
*info
,
270 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
271 assert(info
->size
>= sizeof(NICState
));
273 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
275 nic
= DO_UPCAST(NICState
, nc
, nc
);
277 nic
->opaque
= opaque
;
282 void qemu_del_vlan_client(VLANClientState
*vc
)
285 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
287 if (vc
->send_queue
) {
288 qemu_del_net_queue(vc
->send_queue
);
290 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
292 vc
->peer
->peer
= NULL
;
296 if (vc
->info
->cleanup
) {
297 vc
->info
->cleanup(vc
);
301 qemu_free(vc
->model
);
306 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
307 const char *client_str
)
312 vlan
= qemu_find_vlan(vlan_id
, 0);
314 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
318 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
319 if (!strcmp(vc
->name
, client_str
)) {
324 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
325 client_str
, vlan_id
);
331 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
336 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
337 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
338 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
342 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
343 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
344 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
345 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
351 int qemu_can_send_packet(VLANClientState
*sender
)
353 VLANState
*vlan
= sender
->vlan
;
357 if (sender
->peer
->receive_disabled
) {
359 } else if (sender
->peer
->info
->can_receive
&&
360 !sender
->peer
->info
->can_receive(sender
->peer
)) {
371 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
376 /* no can_receive() handler, they can always receive */
377 if (!vc
->info
->can_receive
|| vc
->info
->can_receive(vc
)) {
384 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
390 VLANClientState
*vc
= opaque
;
397 if (vc
->receive_disabled
) {
401 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
402 ret
= vc
->info
->receive_raw(vc
, data
, size
);
404 ret
= vc
->info
->receive(vc
, data
, size
);
408 vc
->receive_disabled
= 1;
414 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
420 VLANState
*vlan
= opaque
;
424 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
436 if (vc
->receive_disabled
) {
441 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
442 len
= vc
->info
->receive_raw(vc
, buf
, size
);
444 len
= vc
->info
->receive(vc
, buf
, size
);
448 vc
->receive_disabled
= 1;
451 ret
= (ret
>= 0) ? ret
: len
;
458 void qemu_purge_queued_packets(VLANClientState
*vc
)
462 if (!vc
->peer
&& !vc
->vlan
) {
467 queue
= vc
->peer
->send_queue
;
469 queue
= vc
->vlan
->send_queue
;
472 qemu_net_queue_purge(queue
, vc
);
475 void qemu_flush_queued_packets(VLANClientState
*vc
)
479 vc
->receive_disabled
= 0;
482 queue
= vc
->vlan
->send_queue
;
484 queue
= vc
->send_queue
;
487 qemu_net_queue_flush(queue
);
490 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
492 const uint8_t *buf
, int size
,
493 NetPacketSent
*sent_cb
)
498 printf("qemu_send_packet_async:\n");
499 hex_dump(stdout
, buf
, size
);
502 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
507 queue
= sender
->peer
->send_queue
;
509 queue
= sender
->vlan
->send_queue
;
512 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
515 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
516 const uint8_t *buf
, int size
,
517 NetPacketSent
*sent_cb
)
519 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
523 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
525 qemu_send_packet_async(vc
, buf
, size
, NULL
);
528 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
530 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
534 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
537 uint8_t buffer
[4096];
541 for (i
= 0; i
< iovcnt
; i
++) {
544 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
545 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
549 return vc
->info
->receive(vc
, buffer
, offset
);
552 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
557 for (i
= 0; i
< iovcnt
; i
++)
558 offset
+= iov
[i
].iov_len
;
562 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
564 const struct iovec
*iov
,
568 VLANClientState
*vc
= opaque
;
571 return calc_iov_length(iov
, iovcnt
);
574 if (vc
->info
->receive_iov
) {
575 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
577 return vc_sendv_compat(vc
, iov
, iovcnt
);
581 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
583 const struct iovec
*iov
,
587 VLANState
*vlan
= opaque
;
591 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
599 ret
= calc_iov_length(iov
, iovcnt
);
603 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
605 if (vc
->info
->receive_iov
) {
606 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
608 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
611 ret
= (ret
>= 0) ? ret
: len
;
617 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
618 const struct iovec
*iov
, int iovcnt
,
619 NetPacketSent
*sent_cb
)
623 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
624 return calc_iov_length(iov
, iovcnt
);
628 queue
= sender
->peer
->send_queue
;
630 queue
= sender
->vlan
->send_queue
;
633 return qemu_net_queue_send_iov(queue
, sender
,
634 QEMU_NET_PACKET_FLAG_NONE
,
635 iov
, iovcnt
, sent_cb
);
639 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
641 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
644 /* find or alloc a new VLAN */
645 VLANState
*qemu_find_vlan(int id
, int allocate
)
649 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
650 if (vlan
->id
== id
) {
659 vlan
= qemu_mallocz(sizeof(VLANState
));
661 QTAILQ_INIT(&vlan
->clients
);
663 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
664 qemu_vlan_deliver_packet_iov
,
667 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
672 VLANClientState
*qemu_find_netdev(const char *id
)
676 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
677 if (!strcmp(vc
->name
, id
)) {
685 static int nic_get_free_idx(void)
689 for (index
= 0; index
< MAX_NICS
; index
++)
690 if (!nd_table
[index
].used
)
695 int qemu_show_nic_models(const char *arg
, const char *const *models
)
699 if (!arg
|| strcmp(arg
, "?"))
702 fprintf(stderr
, "qemu: Supported NIC models: ");
703 for (i
= 0 ; models
[i
]; i
++)
704 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
708 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
710 const char *models
[2];
715 if (qemu_show_nic_models(nd
->model
, models
))
717 if (qemu_find_nic_model(nd
, models
, model
) < 0)
721 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
722 const char *default_model
)
727 nd
->model
= qemu_strdup(default_model
);
729 for (i
= 0 ; models
[i
]; i
++) {
730 if (strcmp(nd
->model
, models
[i
]) == 0)
734 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
738 int net_handle_fd_param(Monitor
*mon
, const char *param
)
740 if (!qemu_isdigit(param
[0])) {
743 fd
= monitor_get_fd(mon
, param
);
745 qemu_error("No file descriptor named %s found", param
);
751 return strtol(param
, NULL
, 0);
755 static int net_init_nic(QemuOpts
*opts
,
764 idx
= nic_get_free_idx();
765 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
766 qemu_error("Too Many NICs\n");
772 memset(nd
, 0, sizeof(*nd
));
774 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
775 nd
->netdev
= qemu_find_netdev(netdev
);
777 qemu_error("netdev '%s' not found\n", netdev
);
785 nd
->name
= qemu_strdup(name
);
787 if (qemu_opt_get(opts
, "model")) {
788 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
790 if (qemu_opt_get(opts
, "addr")) {
791 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
794 nd
->macaddr
[0] = 0x52;
795 nd
->macaddr
[1] = 0x54;
796 nd
->macaddr
[2] = 0x00;
797 nd
->macaddr
[3] = 0x12;
798 nd
->macaddr
[4] = 0x34;
799 nd
->macaddr
[5] = 0x56 + idx
;
801 if (qemu_opt_get(opts
, "macaddr") &&
802 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
803 qemu_error("invalid syntax for ethernet address\n");
807 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
808 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
809 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
810 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
820 #define NET_COMMON_PARAMS_DESC \
823 .type = QEMU_OPT_STRING, \
824 .help = "net client type (nic, tap etc.)", \
827 .type = QEMU_OPT_NUMBER, \
828 .help = "vlan number", \
831 .type = QEMU_OPT_STRING, \
832 .help = "identifier for monitor commands", \
835 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
840 /* magic number, but compiler will warn if too small */
841 #define NET_MAX_DESC 20
843 static const struct {
845 net_client_init_func init
;
846 QemuOptDesc desc
[NET_MAX_DESC
];
847 } net_client_types
[] = {
851 NET_COMMON_PARAMS_DESC
,
852 { /* end of list */ }
856 .init
= net_init_nic
,
858 NET_COMMON_PARAMS_DESC
,
861 .type
= QEMU_OPT_STRING
,
862 .help
= "id of -netdev to connect to",
866 .type
= QEMU_OPT_STRING
,
867 .help
= "MAC address",
870 .type
= QEMU_OPT_STRING
,
871 .help
= "device model (e1000, rtl8139, virtio etc.)",
874 .type
= QEMU_OPT_STRING
,
875 .help
= "PCI device address",
878 .type
= QEMU_OPT_NUMBER
,
879 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
881 { /* end of list */ }
886 .init
= net_init_slirp
,
888 NET_COMMON_PARAMS_DESC
,
891 .type
= QEMU_OPT_STRING
,
892 .help
= "client hostname reported by the builtin DHCP server",
895 .type
= QEMU_OPT_STRING
,
896 .help
= "isolate the guest from the host (y|yes|n|no)",
899 .type
= QEMU_OPT_STRING
,
900 .help
= "legacy parameter, use net= instead",
903 .type
= QEMU_OPT_STRING
,
904 .help
= "IP address and optional netmask",
907 .type
= QEMU_OPT_STRING
,
908 .help
= "guest-visible address of the host",
911 .type
= QEMU_OPT_STRING
,
912 .help
= "root directory of the built-in TFTP server",
915 .type
= QEMU_OPT_STRING
,
916 .help
= "BOOTP filename, for use with tftp=",
919 .type
= QEMU_OPT_STRING
,
920 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
923 .type
= QEMU_OPT_STRING
,
924 .help
= "guest-visible address of the virtual nameserver",
927 .type
= QEMU_OPT_STRING
,
928 .help
= "root directory of the built-in SMB server",
931 .type
= QEMU_OPT_STRING
,
932 .help
= "IP address of the built-in SMB server",
935 .type
= QEMU_OPT_STRING
,
936 .help
= "guest port number to forward incoming TCP or UDP connections",
939 .type
= QEMU_OPT_STRING
,
940 .help
= "IP address and port to forward guest TCP connections",
942 { /* end of list */ }
947 .init
= net_init_tap
,
949 NET_COMMON_PARAMS_DESC
,
952 .type
= QEMU_OPT_STRING
,
953 .help
= "interface name",
958 .type
= QEMU_OPT_STRING
,
959 .help
= "file descriptor of an already opened tap",
962 .type
= QEMU_OPT_STRING
,
963 .help
= "script to initialize the interface",
965 .name
= "downscript",
966 .type
= QEMU_OPT_STRING
,
967 .help
= "script to shut down the interface",
970 .type
= QEMU_OPT_SIZE
,
971 .help
= "send buffer limit"
974 .type
= QEMU_OPT_BOOL
,
975 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
978 { /* end of list */ }
982 .init
= net_init_socket
,
984 NET_COMMON_PARAMS_DESC
,
987 .type
= QEMU_OPT_STRING
,
988 .help
= "file descriptor of an already opened socket",
991 .type
= QEMU_OPT_STRING
,
992 .help
= "port number, and optional hostname, to listen on",
995 .type
= QEMU_OPT_STRING
,
996 .help
= "port number, and optional hostname, to connect to",
999 .type
= QEMU_OPT_STRING
,
1000 .help
= "UDP multicast address and port number",
1002 { /* end of list */ }
1007 .init
= net_init_vde
,
1009 NET_COMMON_PARAMS_DESC
,
1012 .type
= QEMU_OPT_STRING
,
1013 .help
= "socket path",
1016 .type
= QEMU_OPT_NUMBER
,
1017 .help
= "port number",
1020 .type
= QEMU_OPT_STRING
,
1021 .help
= "group owner of socket",
1024 .type
= QEMU_OPT_NUMBER
,
1025 .help
= "permissions for socket",
1027 { /* end of list */ }
1032 .init
= net_init_dump
,
1034 NET_COMMON_PARAMS_DESC
,
1037 .type
= QEMU_OPT_SIZE
,
1038 .help
= "per-packet size limit (64k default)",
1041 .type
= QEMU_OPT_STRING
,
1042 .help
= "dump file path (default is qemu-vlan0.pcap)",
1044 { /* end of list */ }
1047 { /* end of list */ }
1050 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1056 type
= qemu_opt_get(opts
, "type");
1060 qemu_error("No type specified for -net\n");
1065 qemu_error("No type specified for -netdev\n");
1069 if (strcmp(type
, "tap") != 0 &&
1071 strcmp(type
, "user") != 0 &&
1074 strcmp(type
, "vde") != 0 &&
1076 strcmp(type
, "socket") != 0) {
1077 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1082 if (qemu_opt_get(opts
, "vlan")) {
1083 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1086 if (qemu_opt_get(opts
, "name")) {
1087 qemu_error("The 'name' parameter is not valid with -netdev\n");
1090 if (!qemu_opts_id(opts
)) {
1091 qemu_error("The id= parameter is required with -netdev\n");
1096 name
= qemu_opts_id(opts
);
1098 name
= qemu_opt_get(opts
, "name");
1101 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1102 if (!strcmp(net_client_types
[i
].type
, type
)) {
1103 VLANState
*vlan
= NULL
;
1105 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1109 /* Do not add to a vlan if it's a -netdev or a nic with a
1110 * netdev= parameter. */
1112 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1113 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1116 if (net_client_types
[i
].init
) {
1117 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1124 qemu_error("Invalid -net type '%s'\n", type
);
1128 static int net_host_check_device(const char *device
)
1131 const char *valid_param_list
[] = { "tap", "socket", "dump"
1139 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1140 if (!strncmp(valid_param_list
[i
], device
,
1141 strlen(valid_param_list
[i
])))
1148 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1150 const char *device
= qdict_get_str(qdict
, "device");
1151 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1154 if (!net_host_check_device(device
)) {
1155 monitor_printf(mon
, "invalid host network device %s\n", device
);
1159 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
1161 monitor_printf(mon
, "parsing network options '%s' failed\n",
1162 opts_str
? opts_str
: "");
1166 qemu_opt_set(opts
, "type", device
);
1168 if (net_client_init(mon
, opts
, 0) < 0) {
1169 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1173 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1175 VLANClientState
*vc
;
1176 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1177 const char *device
= qdict_get_str(qdict
, "device");
1179 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1183 if (!net_host_check_device(vc
->model
)) {
1184 monitor_printf(mon
, "invalid host network device %s\n", device
);
1187 qemu_del_vlan_client(vc
);
1190 void net_set_boot_mask(int net_boot_mask
)
1194 /* Only the first four NICs may be bootable */
1195 net_boot_mask
= net_boot_mask
& 0xF;
1197 for (i
= 0; i
< nb_nics
; i
++) {
1198 if (net_boot_mask
& (1 << i
)) {
1199 nd_table
[i
].bootable
= 1;
1200 net_boot_mask
&= ~(1 << i
);
1204 if (net_boot_mask
) {
1205 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
1210 void do_info_network(Monitor
*mon
)
1213 VLANClientState
*vc
;
1215 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1216 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1218 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1219 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1222 monitor_printf(mon
, "Devices not on any VLAN:\n");
1223 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1224 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1226 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1228 monitor_printf(mon
, "\n");
1232 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
1235 VLANClientState
*vc
= NULL
;
1236 const char *name
= qdict_get_str(qdict
, "name");
1237 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
1239 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1240 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1241 if (strcmp(vc
->name
, name
) == 0) {
1246 vc
= qemu_find_netdev(name
);
1250 monitor_printf(mon
, "could not find network device '%s'\n", name
);
1254 if (strcmp(up_or_down
, "up") == 0)
1256 else if (strcmp(up_or_down
, "down") == 0)
1259 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
1260 "valid\n", up_or_down
);
1262 if (vc
->info
->link_status_changed
) {
1263 vc
->info
->link_status_changed(vc
);
1267 void net_cleanup(void)
1270 VLANClientState
*vc
, *next_vc
;
1272 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1273 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1274 qemu_del_vlan_client(vc
);
1278 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1279 qemu_del_vlan_client(vc
);
1283 void net_check_clients(void)
1286 VLANClientState
*vc
;
1287 int has_nic
= 0, has_host_dev
= 0;
1289 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1290 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1291 switch (vc
->info
->type
) {
1292 case NET_CLIENT_TYPE_NIC
:
1295 case NET_CLIENT_TYPE_SLIRP
:
1296 case NET_CLIENT_TYPE_TAP
:
1297 case NET_CLIENT_TYPE_SOCKET
:
1298 case NET_CLIENT_TYPE_VDE
:
1304 if (has_host_dev
&& !has_nic
)
1305 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1306 if (has_nic
&& !has_host_dev
)
1308 "Warning: vlan %d is not connected to host network\n",
1311 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1313 fprintf(stderr
, "Warning: %s %s has no peer\n",
1314 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1320 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1322 if (net_client_init(NULL
, opts
, 0) < 0)
1327 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1329 return net_client_init(NULL
, opts
, 1);
1332 int net_init_clients(void)
1335 /* if no clients, we use a default config */
1336 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1338 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1342 QTAILQ_INIT(&vlans
);
1343 QTAILQ_INIT(&non_vlan_clients
);
1345 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1348 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1355 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1357 #if defined(CONFIG_SLIRP)
1359 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1364 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {