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
);
252 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
254 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
255 qemu_deliver_packet_iov
,
262 NICState
*qemu_new_nic(NetClientInfo
*info
,
271 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
272 assert(info
->size
>= sizeof(NICState
));
274 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
276 nic
= DO_UPCAST(NICState
, nc
, nc
);
278 nic
->opaque
= opaque
;
283 void qemu_del_vlan_client(VLANClientState
*vc
)
286 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
288 if (vc
->send_queue
) {
289 qemu_del_net_queue(vc
->send_queue
);
291 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
293 vc
->peer
->peer
= NULL
;
297 if (vc
->info
->cleanup
) {
298 vc
->info
->cleanup(vc
);
302 qemu_free(vc
->model
);
307 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
308 const char *client_str
)
313 vlan
= qemu_find_vlan(vlan_id
, 0);
315 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
319 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
320 if (!strcmp(vc
->name
, client_str
)) {
325 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
326 client_str
, vlan_id
);
332 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
337 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
338 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
339 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
343 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
344 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
345 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
346 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
352 int qemu_can_send_packet(VLANClientState
*sender
)
354 VLANState
*vlan
= sender
->vlan
;
358 if (sender
->peer
->receive_disabled
) {
360 } else if (sender
->peer
->info
->can_receive
&&
361 !sender
->peer
->info
->can_receive(sender
->peer
)) {
372 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
377 /* no can_receive() handler, they can always receive */
378 if (!vc
->info
->can_receive
|| vc
->info
->can_receive(vc
)) {
385 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
391 VLANClientState
*vc
= opaque
;
398 if (vc
->receive_disabled
) {
402 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
403 ret
= vc
->info
->receive_raw(vc
, data
, size
);
405 ret
= vc
->info
->receive(vc
, data
, size
);
409 vc
->receive_disabled
= 1;
415 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
421 VLANState
*vlan
= opaque
;
425 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
437 if (vc
->receive_disabled
) {
442 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
443 len
= vc
->info
->receive_raw(vc
, buf
, size
);
445 len
= vc
->info
->receive(vc
, buf
, size
);
449 vc
->receive_disabled
= 1;
452 ret
= (ret
>= 0) ? ret
: len
;
459 void qemu_purge_queued_packets(VLANClientState
*vc
)
463 if (!vc
->peer
&& !vc
->vlan
) {
468 queue
= vc
->peer
->send_queue
;
470 queue
= vc
->vlan
->send_queue
;
473 qemu_net_queue_purge(queue
, vc
);
476 void qemu_flush_queued_packets(VLANClientState
*vc
)
480 vc
->receive_disabled
= 0;
483 queue
= vc
->vlan
->send_queue
;
485 queue
= vc
->send_queue
;
488 qemu_net_queue_flush(queue
);
491 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
493 const uint8_t *buf
, int size
,
494 NetPacketSent
*sent_cb
)
499 printf("qemu_send_packet_async:\n");
500 hex_dump(stdout
, buf
, size
);
503 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
508 queue
= sender
->peer
->send_queue
;
510 queue
= sender
->vlan
->send_queue
;
513 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
516 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
517 const uint8_t *buf
, int size
,
518 NetPacketSent
*sent_cb
)
520 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
524 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
526 qemu_send_packet_async(vc
, buf
, size
, NULL
);
529 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
531 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
535 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
538 uint8_t buffer
[4096];
542 for (i
= 0; i
< iovcnt
; i
++) {
545 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
546 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
550 return vc
->info
->receive(vc
, buffer
, offset
);
553 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
558 for (i
= 0; i
< iovcnt
; i
++)
559 offset
+= iov
[i
].iov_len
;
563 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
565 const struct iovec
*iov
,
569 VLANClientState
*vc
= opaque
;
572 return calc_iov_length(iov
, iovcnt
);
575 if (vc
->info
->receive_iov
) {
576 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
578 return vc_sendv_compat(vc
, iov
, iovcnt
);
582 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
584 const struct iovec
*iov
,
588 VLANState
*vlan
= opaque
;
592 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
600 ret
= calc_iov_length(iov
, iovcnt
);
604 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
606 if (vc
->info
->receive_iov
) {
607 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
609 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
612 ret
= (ret
>= 0) ? ret
: len
;
618 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
619 const struct iovec
*iov
, int iovcnt
,
620 NetPacketSent
*sent_cb
)
624 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
625 return calc_iov_length(iov
, iovcnt
);
629 queue
= sender
->peer
->send_queue
;
631 queue
= sender
->vlan
->send_queue
;
634 return qemu_net_queue_send_iov(queue
, sender
,
635 QEMU_NET_PACKET_FLAG_NONE
,
636 iov
, iovcnt
, sent_cb
);
640 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
642 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
645 /* find or alloc a new VLAN */
646 VLANState
*qemu_find_vlan(int id
, int allocate
)
650 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
651 if (vlan
->id
== id
) {
660 vlan
= qemu_mallocz(sizeof(VLANState
));
662 QTAILQ_INIT(&vlan
->clients
);
664 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
665 qemu_vlan_deliver_packet_iov
,
668 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
673 VLANClientState
*qemu_find_netdev(const char *id
)
677 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
678 if (!strcmp(vc
->name
, id
)) {
686 static int nic_get_free_idx(void)
690 for (index
= 0; index
< MAX_NICS
; index
++)
691 if (!nd_table
[index
].used
)
696 int qemu_show_nic_models(const char *arg
, const char *const *models
)
700 if (!arg
|| strcmp(arg
, "?"))
703 fprintf(stderr
, "qemu: Supported NIC models: ");
704 for (i
= 0 ; models
[i
]; i
++)
705 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
709 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
711 const char *models
[2];
716 if (qemu_show_nic_models(nd
->model
, models
))
718 if (qemu_find_nic_model(nd
, models
, model
) < 0)
722 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
723 const char *default_model
)
728 nd
->model
= qemu_strdup(default_model
);
730 for (i
= 0 ; models
[i
]; i
++) {
731 if (strcmp(nd
->model
, models
[i
]) == 0)
735 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
739 int net_handle_fd_param(Monitor
*mon
, const char *param
)
741 if (!qemu_isdigit(param
[0])) {
744 fd
= monitor_get_fd(mon
, param
);
746 qemu_error("No file descriptor named %s found", param
);
752 return strtol(param
, NULL
, 0);
756 static int net_init_nic(QemuOpts
*opts
,
765 idx
= nic_get_free_idx();
766 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
767 qemu_error("Too Many NICs\n");
773 memset(nd
, 0, sizeof(*nd
));
775 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
776 nd
->netdev
= qemu_find_netdev(netdev
);
778 qemu_error("netdev '%s' not found\n", netdev
);
786 nd
->name
= qemu_strdup(name
);
788 if (qemu_opt_get(opts
, "model")) {
789 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
791 if (qemu_opt_get(opts
, "addr")) {
792 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
795 nd
->macaddr
[0] = 0x52;
796 nd
->macaddr
[1] = 0x54;
797 nd
->macaddr
[2] = 0x00;
798 nd
->macaddr
[3] = 0x12;
799 nd
->macaddr
[4] = 0x34;
800 nd
->macaddr
[5] = 0x56 + idx
;
802 if (qemu_opt_get(opts
, "macaddr") &&
803 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
804 qemu_error("invalid syntax for ethernet address\n");
808 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
809 DEV_NVECTORS_UNSPECIFIED
);
810 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
811 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
812 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
822 #define NET_COMMON_PARAMS_DESC \
825 .type = QEMU_OPT_STRING, \
826 .help = "net client type (nic, tap etc.)", \
829 .type = QEMU_OPT_NUMBER, \
830 .help = "vlan number", \
833 .type = QEMU_OPT_STRING, \
834 .help = "identifier for monitor commands", \
837 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
842 /* magic number, but compiler will warn if too small */
843 #define NET_MAX_DESC 20
845 static const struct {
847 net_client_init_func init
;
848 QemuOptDesc desc
[NET_MAX_DESC
];
849 } net_client_types
[] = {
853 NET_COMMON_PARAMS_DESC
,
854 { /* end of list */ }
858 .init
= net_init_nic
,
860 NET_COMMON_PARAMS_DESC
,
863 .type
= QEMU_OPT_STRING
,
864 .help
= "id of -netdev to connect to",
868 .type
= QEMU_OPT_STRING
,
869 .help
= "MAC address",
872 .type
= QEMU_OPT_STRING
,
873 .help
= "device model (e1000, rtl8139, virtio etc.)",
876 .type
= QEMU_OPT_STRING
,
877 .help
= "PCI device address",
880 .type
= QEMU_OPT_NUMBER
,
881 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
883 { /* end of list */ }
888 .init
= net_init_slirp
,
890 NET_COMMON_PARAMS_DESC
,
893 .type
= QEMU_OPT_STRING
,
894 .help
= "client hostname reported by the builtin DHCP server",
897 .type
= QEMU_OPT_STRING
,
898 .help
= "isolate the guest from the host (y|yes|n|no)",
901 .type
= QEMU_OPT_STRING
,
902 .help
= "legacy parameter, use net= instead",
905 .type
= QEMU_OPT_STRING
,
906 .help
= "IP address and optional netmask",
909 .type
= QEMU_OPT_STRING
,
910 .help
= "guest-visible address of the host",
913 .type
= QEMU_OPT_STRING
,
914 .help
= "root directory of the built-in TFTP server",
917 .type
= QEMU_OPT_STRING
,
918 .help
= "BOOTP filename, for use with tftp=",
921 .type
= QEMU_OPT_STRING
,
922 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
925 .type
= QEMU_OPT_STRING
,
926 .help
= "guest-visible address of the virtual nameserver",
929 .type
= QEMU_OPT_STRING
,
930 .help
= "root directory of the built-in SMB server",
933 .type
= QEMU_OPT_STRING
,
934 .help
= "IP address of the built-in SMB server",
937 .type
= QEMU_OPT_STRING
,
938 .help
= "guest port number to forward incoming TCP or UDP connections",
941 .type
= QEMU_OPT_STRING
,
942 .help
= "IP address and port to forward guest TCP connections",
944 { /* end of list */ }
949 .init
= net_init_tap
,
951 NET_COMMON_PARAMS_DESC
,
954 .type
= QEMU_OPT_STRING
,
955 .help
= "interface name",
960 .type
= QEMU_OPT_STRING
,
961 .help
= "file descriptor of an already opened tap",
964 .type
= QEMU_OPT_STRING
,
965 .help
= "script to initialize the interface",
967 .name
= "downscript",
968 .type
= QEMU_OPT_STRING
,
969 .help
= "script to shut down the interface",
972 .type
= QEMU_OPT_SIZE
,
973 .help
= "send buffer limit"
976 .type
= QEMU_OPT_BOOL
,
977 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
980 { /* end of list */ }
984 .init
= net_init_socket
,
986 NET_COMMON_PARAMS_DESC
,
989 .type
= QEMU_OPT_STRING
,
990 .help
= "file descriptor of an already opened socket",
993 .type
= QEMU_OPT_STRING
,
994 .help
= "port number, and optional hostname, to listen on",
997 .type
= QEMU_OPT_STRING
,
998 .help
= "port number, and optional hostname, to connect to",
1001 .type
= QEMU_OPT_STRING
,
1002 .help
= "UDP multicast address and port number",
1004 { /* end of list */ }
1009 .init
= net_init_vde
,
1011 NET_COMMON_PARAMS_DESC
,
1014 .type
= QEMU_OPT_STRING
,
1015 .help
= "socket path",
1018 .type
= QEMU_OPT_NUMBER
,
1019 .help
= "port number",
1022 .type
= QEMU_OPT_STRING
,
1023 .help
= "group owner of socket",
1026 .type
= QEMU_OPT_NUMBER
,
1027 .help
= "permissions for socket",
1029 { /* end of list */ }
1034 .init
= net_init_dump
,
1036 NET_COMMON_PARAMS_DESC
,
1039 .type
= QEMU_OPT_SIZE
,
1040 .help
= "per-packet size limit (64k default)",
1043 .type
= QEMU_OPT_STRING
,
1044 .help
= "dump file path (default is qemu-vlan0.pcap)",
1046 { /* end of list */ }
1049 { /* end of list */ }
1052 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1058 type
= qemu_opt_get(opts
, "type");
1062 qemu_error("No type specified for -net\n");
1067 qemu_error("No type specified for -netdev\n");
1071 if (strcmp(type
, "tap") != 0 &&
1073 strcmp(type
, "user") != 0 &&
1076 strcmp(type
, "vde") != 0 &&
1078 strcmp(type
, "socket") != 0) {
1079 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1084 if (qemu_opt_get(opts
, "vlan")) {
1085 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1088 if (qemu_opt_get(opts
, "name")) {
1089 qemu_error("The 'name' parameter is not valid with -netdev\n");
1092 if (!qemu_opts_id(opts
)) {
1093 qemu_error("The id= parameter is required with -netdev\n");
1098 name
= qemu_opts_id(opts
);
1100 name
= qemu_opt_get(opts
, "name");
1103 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1104 if (!strcmp(net_client_types
[i
].type
, type
)) {
1105 VLANState
*vlan
= NULL
;
1107 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1111 /* Do not add to a vlan if it's a -netdev or a nic with a
1112 * netdev= parameter. */
1114 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1115 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1118 if (net_client_types
[i
].init
) {
1119 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1126 qemu_error("Invalid -net type '%s'\n", type
);
1130 static int net_host_check_device(const char *device
)
1133 const char *valid_param_list
[] = { "tap", "socket", "dump"
1141 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1142 if (!strncmp(valid_param_list
[i
], device
,
1143 strlen(valid_param_list
[i
])))
1150 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1152 const char *device
= qdict_get_str(qdict
, "device");
1153 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1156 if (!net_host_check_device(device
)) {
1157 monitor_printf(mon
, "invalid host network device %s\n", device
);
1161 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
1163 monitor_printf(mon
, "parsing network options '%s' failed\n",
1164 opts_str
? opts_str
: "");
1168 qemu_opt_set(opts
, "type", device
);
1170 if (net_client_init(mon
, opts
, 0) < 0) {
1171 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1175 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1177 VLANClientState
*vc
;
1178 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1179 const char *device
= qdict_get_str(qdict
, "device");
1181 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1185 if (!net_host_check_device(vc
->model
)) {
1186 monitor_printf(mon
, "invalid host network device %s\n", device
);
1189 qemu_del_vlan_client(vc
);
1192 void net_set_boot_mask(int net_boot_mask
)
1196 /* Only the first four NICs may be bootable */
1197 net_boot_mask
= net_boot_mask
& 0xF;
1199 for (i
= 0; i
< nb_nics
; i
++) {
1200 if (net_boot_mask
& (1 << i
)) {
1201 nd_table
[i
].bootable
= 1;
1202 net_boot_mask
&= ~(1 << i
);
1206 if (net_boot_mask
) {
1207 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
1212 void do_info_network(Monitor
*mon
)
1215 VLANClientState
*vc
;
1217 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1218 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1220 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1221 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1224 monitor_printf(mon
, "Devices not on any VLAN:\n");
1225 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1226 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1228 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1230 monitor_printf(mon
, "\n");
1234 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
1237 VLANClientState
*vc
= NULL
;
1238 const char *name
= qdict_get_str(qdict
, "name");
1239 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
1241 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1242 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1243 if (strcmp(vc
->name
, name
) == 0) {
1248 vc
= qemu_find_netdev(name
);
1252 monitor_printf(mon
, "could not find network device '%s'\n", name
);
1256 if (strcmp(up_or_down
, "up") == 0)
1258 else if (strcmp(up_or_down
, "down") == 0)
1261 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
1262 "valid\n", up_or_down
);
1264 if (vc
->info
->link_status_changed
) {
1265 vc
->info
->link_status_changed(vc
);
1269 void net_cleanup(void)
1272 VLANClientState
*vc
, *next_vc
;
1274 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1275 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1276 qemu_del_vlan_client(vc
);
1280 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1281 qemu_del_vlan_client(vc
);
1285 void net_check_clients(void)
1288 VLANClientState
*vc
;
1289 int has_nic
= 0, has_host_dev
= 0;
1291 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1292 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1293 switch (vc
->info
->type
) {
1294 case NET_CLIENT_TYPE_NIC
:
1297 case NET_CLIENT_TYPE_SLIRP
:
1298 case NET_CLIENT_TYPE_TAP
:
1299 case NET_CLIENT_TYPE_SOCKET
:
1300 case NET_CLIENT_TYPE_VDE
:
1306 if (has_host_dev
&& !has_nic
)
1307 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1308 if (has_nic
&& !has_host_dev
)
1310 "Warning: vlan %d is not connected to host network\n",
1313 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1315 fprintf(stderr
, "Warning: %s %s has no peer\n",
1316 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1322 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1324 if (net_client_init(NULL
, opts
, 0) < 0)
1329 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1331 return net_client_init(NULL
, opts
, 1);
1334 int net_init_clients(void)
1337 /* if no clients, we use a default config */
1338 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1340 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1344 QTAILQ_INIT(&vlans
);
1345 QTAILQ_INIT(&non_vlan_clients
);
1347 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1350 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1357 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1359 #if defined(CONFIG_SLIRP)
1361 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1366 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {