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"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
37 #include "qmp-commands.h"
41 static QTAILQ_HEAD(, VLANState
) vlans
;
42 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
46 /***********************************************************/
47 /* network device redirectors */
49 #if defined(DEBUG_NET)
50 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
54 for(i
=0;i
<size
;i
+=16) {
58 fprintf(f
, "%08x ", i
);
61 fprintf(f
, " %02x", buf
[i
+j
]);
68 if (c
< ' ' || c
> '~')
77 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
88 if (len
> buf_size
- 1)
97 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
105 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
107 saddr
->sin_family
= AF_INET
;
108 if (buf
[0] == '\0') {
109 saddr
->sin_addr
.s_addr
= 0;
111 if (qemu_isdigit(buf
[0])) {
112 if (!inet_aton(buf
, &saddr
->sin_addr
))
115 if ((he
= gethostbyname(buf
)) == NULL
)
117 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
120 port
= strtol(p
, (char **)&r
, 0);
123 saddr
->sin_port
= htons(port
);
127 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
129 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
130 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
132 macaddr
[0], macaddr
[1], macaddr
[2],
133 macaddr
[3], macaddr
[4], macaddr
[5]);
136 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
138 static int index
= 0;
139 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
141 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
143 macaddr
->a
[0] = 0x52;
144 macaddr
->a
[1] = 0x54;
145 macaddr
->a
[2] = 0x00;
146 macaddr
->a
[3] = 0x12;
147 macaddr
->a
[4] = 0x34;
148 macaddr
->a
[5] = 0x56 + index
++;
151 static char *assign_name(VLANClientState
*vc1
, const char *model
)
158 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
159 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
160 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
166 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
167 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
172 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
174 return g_strdup(buf
);
177 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
182 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
184 const struct iovec
*iov
,
188 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
190 VLANClientState
*peer
,
196 assert(info
->size
>= sizeof(VLANClientState
));
198 vc
= g_malloc0(info
->size
);
201 vc
->model
= g_strdup(model
);
203 vc
->name
= g_strdup(name
);
205 vc
->name
= assign_name(vc
, model
);
211 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
218 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
220 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
221 qemu_deliver_packet_iov
,
228 NICState
*qemu_new_nic(NetClientInfo
*info
,
237 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
238 assert(info
->size
>= sizeof(NICState
));
240 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
242 nic
= DO_UPCAST(NICState
, nc
, nc
);
244 nic
->opaque
= opaque
;
249 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
252 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
254 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
257 if (vc
->info
->cleanup
) {
258 vc
->info
->cleanup(vc
);
262 static void qemu_free_vlan_client(VLANClientState
*vc
)
265 if (vc
->send_queue
) {
266 qemu_del_net_queue(vc
->send_queue
);
269 vc
->peer
->peer
= NULL
;
277 void qemu_del_vlan_client(VLANClientState
*vc
)
279 /* If there is a peer NIC, delete and cleanup client, but do not free. */
280 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
281 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
282 if (nic
->peer_deleted
) {
285 nic
->peer_deleted
= true;
286 /* Let NIC know peer is gone. */
287 vc
->peer
->link_down
= true;
288 if (vc
->peer
->info
->link_status_changed
) {
289 vc
->peer
->info
->link_status_changed(vc
->peer
);
291 qemu_cleanup_vlan_client(vc
);
295 /* If this is a peer NIC and peer has already been deleted, free it now. */
296 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
297 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
298 if (nic
->peer_deleted
) {
299 qemu_free_vlan_client(vc
->peer
);
303 qemu_cleanup_vlan_client(vc
);
304 qemu_free_vlan_client(vc
);
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];
542 offset
= iov_to_buf(iov
, iovcnt
, buffer
, 0, sizeof(buffer
));
544 return vc
->info
->receive(vc
, buffer
, offset
);
547 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
549 const struct iovec
*iov
,
553 VLANClientState
*vc
= opaque
;
556 return iov_size(iov
, iovcnt
);
559 if (vc
->info
->receive_iov
) {
560 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
562 return vc_sendv_compat(vc
, iov
, iovcnt
);
566 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
568 const struct iovec
*iov
,
572 VLANState
*vlan
= opaque
;
576 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
584 ret
= iov_size(iov
, iovcnt
);
588 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
590 if (vc
->info
->receive_iov
) {
591 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
593 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
596 ret
= (ret
>= 0) ? ret
: len
;
602 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
603 const struct iovec
*iov
, int iovcnt
,
604 NetPacketSent
*sent_cb
)
608 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
609 return iov_size(iov
, iovcnt
);
613 queue
= sender
->peer
->send_queue
;
615 queue
= sender
->vlan
->send_queue
;
618 return qemu_net_queue_send_iov(queue
, sender
,
619 QEMU_NET_PACKET_FLAG_NONE
,
620 iov
, iovcnt
, sent_cb
);
624 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
626 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
629 /* find or alloc a new VLAN */
630 VLANState
*qemu_find_vlan(int id
, int allocate
)
634 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
635 if (vlan
->id
== id
) {
644 vlan
= g_malloc0(sizeof(VLANState
));
646 QTAILQ_INIT(&vlan
->clients
);
648 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
649 qemu_vlan_deliver_packet_iov
,
652 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
657 VLANClientState
*qemu_find_netdev(const char *id
)
661 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
662 if (vc
->info
->type
== NET_CLIENT_TYPE_NIC
)
664 if (!strcmp(vc
->name
, id
)) {
672 static int nic_get_free_idx(void)
676 for (index
= 0; index
< MAX_NICS
; index
++)
677 if (!nd_table
[index
].used
)
682 int qemu_show_nic_models(const char *arg
, const char *const *models
)
686 if (!arg
|| strcmp(arg
, "?"))
689 fprintf(stderr
, "qemu: Supported NIC models: ");
690 for (i
= 0 ; models
[i
]; i
++)
691 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
695 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
697 const char *models
[2];
702 if (qemu_show_nic_models(nd
->model
, models
))
704 if (qemu_find_nic_model(nd
, models
, model
) < 0)
708 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
709 const char *default_model
)
714 nd
->model
= g_strdup(default_model
);
716 for (i
= 0 ; models
[i
]; i
++) {
717 if (strcmp(nd
->model
, models
[i
]) == 0)
721 error_report("Unsupported NIC model: %s", nd
->model
);
725 int net_handle_fd_param(Monitor
*mon
, const char *param
)
729 if (!qemu_isdigit(param
[0]) && mon
) {
731 fd
= monitor_get_fd(mon
, param
);
733 error_report("No file descriptor named %s found", param
);
737 fd
= qemu_parse_fd(param
);
743 static int net_init_nic(QemuOpts
*opts
,
752 idx
= nic_get_free_idx();
753 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
754 error_report("Too Many NICs");
760 memset(nd
, 0, sizeof(*nd
));
762 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
763 nd
->netdev
= qemu_find_netdev(netdev
);
765 error_report("netdev '%s' not found", netdev
);
773 nd
->name
= g_strdup(name
);
775 if (qemu_opt_get(opts
, "model")) {
776 nd
->model
= g_strdup(qemu_opt_get(opts
, "model"));
778 if (qemu_opt_get(opts
, "addr")) {
779 nd
->devaddr
= g_strdup(qemu_opt_get(opts
, "addr"));
782 if (qemu_opt_get(opts
, "macaddr") &&
783 net_parse_macaddr(nd
->macaddr
.a
, qemu_opt_get(opts
, "macaddr")) < 0) {
784 error_report("invalid syntax for ethernet address");
787 qemu_macaddr_default_if_unset(&nd
->macaddr
);
789 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
790 DEV_NVECTORS_UNSPECIFIED
);
791 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
792 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
793 error_report("invalid # of vectors: %d", nd
->nvectors
);
803 #define NET_COMMON_PARAMS_DESC \
806 .type = QEMU_OPT_STRING, \
807 .help = "net client type (nic, tap etc.)", \
810 .type = QEMU_OPT_NUMBER, \
811 .help = "vlan number", \
814 .type = QEMU_OPT_STRING, \
815 .help = "identifier for monitor commands", \
818 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
823 /* magic number, but compiler will warn if too small */
824 #define NET_MAX_DESC 20
826 static const struct {
828 net_client_init_func init
;
829 QemuOptDesc desc
[NET_MAX_DESC
];
830 } net_client_types
[NET_CLIENT_TYPE_MAX
] = {
831 [NET_CLIENT_TYPE_NONE
] = {
834 NET_COMMON_PARAMS_DESC
,
835 { /* end of list */ }
838 [NET_CLIENT_TYPE_NIC
] = {
840 .init
= net_init_nic
,
842 NET_COMMON_PARAMS_DESC
,
845 .type
= QEMU_OPT_STRING
,
846 .help
= "id of -netdev to connect to",
850 .type
= QEMU_OPT_STRING
,
851 .help
= "MAC address",
854 .type
= QEMU_OPT_STRING
,
855 .help
= "device model (e1000, rtl8139, virtio etc.)",
858 .type
= QEMU_OPT_STRING
,
859 .help
= "PCI device address",
862 .type
= QEMU_OPT_NUMBER
,
863 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
865 { /* end of list */ }
869 [NET_CLIENT_TYPE_USER
] = {
871 .init
= net_init_slirp
,
873 NET_COMMON_PARAMS_DESC
,
876 .type
= QEMU_OPT_STRING
,
877 .help
= "client hostname reported by the builtin DHCP server",
880 .type
= QEMU_OPT_STRING
,
881 .help
= "isolate the guest from the host (y|yes|n|no)",
884 .type
= QEMU_OPT_STRING
,
885 .help
= "legacy parameter, use net= instead",
888 .type
= QEMU_OPT_STRING
,
889 .help
= "IP address and optional netmask",
892 .type
= QEMU_OPT_STRING
,
893 .help
= "guest-visible address of the host",
896 .type
= QEMU_OPT_STRING
,
897 .help
= "root directory of the built-in TFTP server",
900 .type
= QEMU_OPT_STRING
,
901 .help
= "BOOTP filename, for use with tftp=",
904 .type
= QEMU_OPT_STRING
,
905 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
908 .type
= QEMU_OPT_STRING
,
909 .help
= "guest-visible address of the virtual nameserver",
912 .type
= QEMU_OPT_STRING
,
913 .help
= "root directory of the built-in SMB server",
916 .type
= QEMU_OPT_STRING
,
917 .help
= "IP address of the built-in SMB server",
920 .type
= QEMU_OPT_STRING
,
921 .help
= "guest port number to forward incoming TCP or UDP connections",
924 .type
= QEMU_OPT_STRING
,
925 .help
= "IP address and port to forward guest TCP connections",
927 { /* end of list */ }
931 [NET_CLIENT_TYPE_TAP
] = {
933 .init
= net_init_tap
,
935 NET_COMMON_PARAMS_DESC
,
938 .type
= QEMU_OPT_STRING
,
939 .help
= "interface name",
944 .type
= QEMU_OPT_STRING
,
945 .help
= "file descriptor of an already opened tap",
948 .type
= QEMU_OPT_STRING
,
949 .help
= "script to initialize the interface",
951 .name
= "downscript",
952 .type
= QEMU_OPT_STRING
,
953 .help
= "script to shut down the interface",
956 .type
= QEMU_OPT_STRING
,
957 .help
= "command to execute to configure bridge",
960 .type
= QEMU_OPT_SIZE
,
961 .help
= "send buffer limit"
964 .type
= QEMU_OPT_BOOL
,
965 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
968 .type
= QEMU_OPT_BOOL
,
969 .help
= "enable vhost-net network accelerator",
972 .type
= QEMU_OPT_STRING
,
973 .help
= "file descriptor of an already opened vhost net device",
975 .name
= "vhostforce",
976 .type
= QEMU_OPT_BOOL
,
977 .help
= "force vhost on for non-MSIX virtio guests",
980 { /* end of list */ }
983 [NET_CLIENT_TYPE_SOCKET
] = {
985 .init
= net_init_socket
,
987 NET_COMMON_PARAMS_DESC
,
990 .type
= QEMU_OPT_STRING
,
991 .help
= "file descriptor of an already opened socket",
994 .type
= QEMU_OPT_STRING
,
995 .help
= "port number, and optional hostname, to listen on",
998 .type
= QEMU_OPT_STRING
,
999 .help
= "port number, and optional hostname, to connect to",
1002 .type
= QEMU_OPT_STRING
,
1003 .help
= "UDP multicast address and port number",
1005 .name
= "localaddr",
1006 .type
= QEMU_OPT_STRING
,
1007 .help
= "source address and port for multicast and udp packets",
1010 .type
= QEMU_OPT_STRING
,
1011 .help
= "UDP unicast address and port number",
1013 { /* end of list */ }
1017 [NET_CLIENT_TYPE_VDE
] = {
1019 .init
= net_init_vde
,
1021 NET_COMMON_PARAMS_DESC
,
1024 .type
= QEMU_OPT_STRING
,
1025 .help
= "socket path",
1028 .type
= QEMU_OPT_NUMBER
,
1029 .help
= "port number",
1032 .type
= QEMU_OPT_STRING
,
1033 .help
= "group owner of socket",
1036 .type
= QEMU_OPT_NUMBER
,
1037 .help
= "permissions for socket",
1039 { /* end of list */ }
1043 [NET_CLIENT_TYPE_DUMP
] = {
1045 .init
= net_init_dump
,
1047 NET_COMMON_PARAMS_DESC
,
1050 .type
= QEMU_OPT_SIZE
,
1051 .help
= "per-packet size limit (64k default)",
1054 .type
= QEMU_OPT_STRING
,
1055 .help
= "dump file path (default is qemu-vlan0.pcap)",
1057 { /* end of list */ }
1060 [NET_CLIENT_TYPE_BRIDGE
] = {
1062 .init
= net_init_bridge
,
1064 NET_COMMON_PARAMS_DESC
,
1067 .type
= QEMU_OPT_STRING
,
1068 .help
= "bridge name",
1071 .type
= QEMU_OPT_STRING
,
1072 .help
= "command to execute to configure bridge",
1074 { /* end of list */ }
1079 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1085 type
= qemu_opt_get(opts
, "type");
1087 qerror_report(QERR_MISSING_PARAMETER
, "type");
1092 if (strcmp(type
, "tap") != 0 &&
1094 strcmp(type
, "user") != 0 &&
1097 strcmp(type
, "vde") != 0 &&
1099 strcmp(type
, "socket") != 0 &&
1100 strcmp(type
, "bridge") != 0) {
1101 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1102 "a netdev backend type");
1106 if (qemu_opt_get(opts
, "vlan")) {
1107 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1110 if (qemu_opt_get(opts
, "name")) {
1111 qerror_report(QERR_INVALID_PARAMETER
, "name");
1114 if (!qemu_opts_id(opts
)) {
1115 qerror_report(QERR_MISSING_PARAMETER
, "id");
1120 name
= qemu_opts_id(opts
);
1122 name
= qemu_opt_get(opts
, "name");
1125 for (i
= 0; i
< NET_CLIENT_TYPE_MAX
; i
++) {
1126 if (net_client_types
[i
].type
!= NULL
&&
1127 !strcmp(net_client_types
[i
].type
, type
)) {
1128 VLANState
*vlan
= NULL
;
1131 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1135 /* Do not add to a vlan if it's a -netdev or a nic with a
1136 * netdev= parameter. */
1138 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1139 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1143 if (net_client_types
[i
].init
) {
1144 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1146 /* TODO push error reporting into init() methods */
1147 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1155 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1156 "a network client type");
1160 static int net_host_check_device(const char *device
)
1163 const char *valid_param_list
[] = { "tap", "socket", "dump"
1172 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1173 if (!strncmp(valid_param_list
[i
], device
,
1174 strlen(valid_param_list
[i
])))
1181 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1183 const char *device
= qdict_get_str(qdict
, "device");
1184 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1187 if (!net_host_check_device(device
)) {
1188 monitor_printf(mon
, "invalid host network device %s\n", device
);
1192 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
1197 qemu_opt_set(opts
, "type", device
);
1199 if (net_client_init(mon
, opts
, 0) < 0) {
1200 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1204 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1206 VLANClientState
*vc
;
1207 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1208 const char *device
= qdict_get_str(qdict
, "device");
1210 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1214 if (!net_host_check_device(vc
->model
)) {
1215 monitor_printf(mon
, "invalid host network device %s\n", device
);
1218 qemu_del_vlan_client(vc
);
1221 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1226 opts
= qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict
);
1231 res
= net_client_init(mon
, opts
, 1);
1233 qemu_opts_del(opts
);
1239 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1241 const char *id
= qdict_get_str(qdict
, "id");
1242 VLANClientState
*vc
;
1244 vc
= qemu_find_netdev(id
);
1246 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1249 qemu_del_vlan_client(vc
);
1250 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id
));
1254 static void print_net_client(Monitor
*mon
, VLANClientState
*vc
)
1256 monitor_printf(mon
, "%s: type=%s,%s\n", vc
->name
,
1257 net_client_types
[vc
->info
->type
].type
, vc
->info_str
);
1260 void do_info_network(Monitor
*mon
)
1263 VLANClientState
*vc
, *peer
;
1264 net_client_type type
;
1266 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1267 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1269 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1270 monitor_printf(mon
, " ");
1271 print_net_client(mon
, vc
);
1274 monitor_printf(mon
, "Devices not on any VLAN:\n");
1275 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1277 type
= vc
->info
->type
;
1278 if (!peer
|| type
== NET_CLIENT_TYPE_NIC
) {
1279 monitor_printf(mon
, " ");
1280 print_net_client(mon
, vc
);
1281 } /* else it's a netdev connected to a NIC, printed with the NIC */
1282 if (peer
&& type
== NET_CLIENT_TYPE_NIC
) {
1283 monitor_printf(mon
, " \\ ");
1284 print_net_client(mon
, peer
);
1289 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1292 VLANClientState
*vc
= NULL
;
1294 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1295 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1296 if (strcmp(vc
->name
, name
) == 0) {
1301 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1302 if (!strcmp(vc
->name
, name
)) {
1309 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1313 vc
->link_down
= !up
;
1315 if (vc
->info
->link_status_changed
) {
1316 vc
->info
->link_status_changed(vc
);
1319 /* Notify peer. Don't update peer link status: this makes it possible to
1320 * disconnect from host network without notifying the guest.
1321 * FIXME: is disconnected link status change operation useful?
1323 * Current behaviour is compatible with qemu vlans where there could be
1324 * multiple clients that can still communicate with each other in
1325 * disconnected mode. For now maintain this compatibility. */
1326 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1327 vc
->peer
->info
->link_status_changed(vc
->peer
);
1331 void net_cleanup(void)
1334 VLANClientState
*vc
, *next_vc
;
1336 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1337 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1338 qemu_del_vlan_client(vc
);
1342 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1343 qemu_del_vlan_client(vc
);
1347 void net_check_clients(void)
1350 VLANClientState
*vc
;
1353 /* Don't warn about the default network setup that you get if
1354 * no command line -net or -netdev options are specified. There
1355 * are two cases that we would otherwise complain about:
1356 * (1) board doesn't support a NIC but the implicit "-net nic"
1358 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1359 * sets up a nic that isn't connected to anything.
1365 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1366 int has_nic
= 0, has_host_dev
= 0;
1368 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1369 switch (vc
->info
->type
) {
1370 case NET_CLIENT_TYPE_NIC
:
1373 case NET_CLIENT_TYPE_USER
:
1374 case NET_CLIENT_TYPE_TAP
:
1375 case NET_CLIENT_TYPE_SOCKET
:
1376 case NET_CLIENT_TYPE_VDE
:
1382 if (has_host_dev
&& !has_nic
)
1383 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1384 if (has_nic
&& !has_host_dev
)
1386 "Warning: vlan %d is not connected to host network\n",
1389 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1391 fprintf(stderr
, "Warning: %s %s has no peer\n",
1392 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1397 /* Check that all NICs requested via -net nic actually got created.
1398 * NICs created via -device don't need to be checked here because
1399 * they are always instantiated.
1401 for (i
= 0; i
< MAX_NICS
; i
++) {
1402 NICInfo
*nd
= &nd_table
[i
];
1403 if (nd
->used
&& !nd
->instantiated
) {
1404 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1405 "was not created (not supported by this machine?)\n",
1406 nd
->name
? nd
->name
: "anonymous",
1407 nd
->model
? nd
->model
: "unspecified");
1412 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1414 if (net_client_init(NULL
, opts
, 0) < 0)
1419 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1421 return net_client_init(NULL
, opts
, 1);
1424 int net_init_clients(void)
1426 QemuOptsList
*net
= qemu_find_opts("net");
1429 /* if no clients, we use a default config */
1430 qemu_opts_set(net
, NULL
, "type", "nic");
1432 qemu_opts_set(net
, NULL
, "type", "user");
1436 QTAILQ_INIT(&vlans
);
1437 QTAILQ_INIT(&non_vlan_clients
);
1439 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1442 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1449 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1451 #if defined(CONFIG_SLIRP)
1453 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1458 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {