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 /* Net bridge is currently not supported for W32. */
43 # define CONFIG_NET_BRIDGE
46 static QTAILQ_HEAD(, VLANState
) vlans
;
47 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
51 /***********************************************************/
52 /* network device redirectors */
54 #if defined(DEBUG_NET)
55 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
59 for(i
=0;i
<size
;i
+=16) {
63 fprintf(f
, "%08x ", i
);
66 fprintf(f
, " %02x", buf
[i
+j
]);
73 if (c
< ' ' || c
> '~')
82 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
93 if (len
> buf_size
- 1)
102 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
110 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
112 saddr
->sin_family
= AF_INET
;
113 if (buf
[0] == '\0') {
114 saddr
->sin_addr
.s_addr
= 0;
116 if (qemu_isdigit(buf
[0])) {
117 if (!inet_aton(buf
, &saddr
->sin_addr
))
120 if ((he
= gethostbyname(buf
)) == NULL
)
122 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
125 port
= strtol(p
, (char **)&r
, 0);
128 saddr
->sin_port
= htons(port
);
132 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
134 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
135 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
137 macaddr
[0], macaddr
[1], macaddr
[2],
138 macaddr
[3], macaddr
[4], macaddr
[5]);
141 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
143 static int index
= 0;
144 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
146 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
148 macaddr
->a
[0] = 0x52;
149 macaddr
->a
[1] = 0x54;
150 macaddr
->a
[2] = 0x00;
151 macaddr
->a
[3] = 0x12;
152 macaddr
->a
[4] = 0x34;
153 macaddr
->a
[5] = 0x56 + index
++;
156 static char *assign_name(VLANClientState
*vc1
, const char *model
)
163 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
164 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
165 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
171 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
172 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
177 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
179 return g_strdup(buf
);
182 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
187 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
189 const struct iovec
*iov
,
193 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
195 VLANClientState
*peer
,
201 assert(info
->size
>= sizeof(VLANClientState
));
203 vc
= g_malloc0(info
->size
);
206 vc
->model
= g_strdup(model
);
208 vc
->name
= g_strdup(name
);
210 vc
->name
= assign_name(vc
, model
);
216 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
223 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
225 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
226 qemu_deliver_packet_iov
,
233 NICState
*qemu_new_nic(NetClientInfo
*info
,
242 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
243 assert(info
->size
>= sizeof(NICState
));
245 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
247 nic
= DO_UPCAST(NICState
, nc
, nc
);
249 nic
->opaque
= opaque
;
254 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
257 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
259 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
262 if (vc
->info
->cleanup
) {
263 vc
->info
->cleanup(vc
);
267 static void qemu_free_vlan_client(VLANClientState
*vc
)
270 if (vc
->send_queue
) {
271 qemu_del_net_queue(vc
->send_queue
);
274 vc
->peer
->peer
= NULL
;
282 void qemu_del_vlan_client(VLANClientState
*vc
)
284 /* If there is a peer NIC, delete and cleanup client, but do not free. */
285 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
286 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
287 if (nic
->peer_deleted
) {
290 nic
->peer_deleted
= true;
291 /* Let NIC know peer is gone. */
292 vc
->peer
->link_down
= true;
293 if (vc
->peer
->info
->link_status_changed
) {
294 vc
->peer
->info
->link_status_changed(vc
->peer
);
296 qemu_cleanup_vlan_client(vc
);
300 /* If this is a peer NIC and peer has already been deleted, free it now. */
301 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
302 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
303 if (nic
->peer_deleted
) {
304 qemu_free_vlan_client(vc
->peer
);
308 qemu_cleanup_vlan_client(vc
);
309 qemu_free_vlan_client(vc
);
313 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
314 const char *client_str
)
319 vlan
= qemu_find_vlan(vlan_id
, 0);
321 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
325 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
326 if (!strcmp(vc
->name
, client_str
)) {
331 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
332 client_str
, vlan_id
);
338 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
343 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
344 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
345 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
349 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
350 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
351 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
352 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
358 int qemu_can_send_packet(VLANClientState
*sender
)
360 VLANState
*vlan
= sender
->vlan
;
364 if (sender
->peer
->receive_disabled
) {
366 } else if (sender
->peer
->info
->can_receive
&&
367 !sender
->peer
->info
->can_receive(sender
->peer
)) {
378 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
383 /* no can_receive() handler, they can always receive */
384 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
391 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
397 VLANClientState
*vc
= opaque
;
404 if (vc
->receive_disabled
) {
408 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
409 ret
= vc
->info
->receive_raw(vc
, data
, size
);
411 ret
= vc
->info
->receive(vc
, data
, size
);
415 vc
->receive_disabled
= 1;
421 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
427 VLANState
*vlan
= opaque
;
431 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
443 if (vc
->receive_disabled
) {
448 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
449 len
= vc
->info
->receive_raw(vc
, buf
, size
);
451 len
= vc
->info
->receive(vc
, buf
, size
);
455 vc
->receive_disabled
= 1;
458 ret
= (ret
>= 0) ? ret
: len
;
465 void qemu_purge_queued_packets(VLANClientState
*vc
)
469 if (!vc
->peer
&& !vc
->vlan
) {
474 queue
= vc
->peer
->send_queue
;
476 queue
= vc
->vlan
->send_queue
;
479 qemu_net_queue_purge(queue
, vc
);
482 void qemu_flush_queued_packets(VLANClientState
*vc
)
486 vc
->receive_disabled
= 0;
489 queue
= vc
->vlan
->send_queue
;
491 queue
= vc
->send_queue
;
494 qemu_net_queue_flush(queue
);
497 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
499 const uint8_t *buf
, int size
,
500 NetPacketSent
*sent_cb
)
505 printf("qemu_send_packet_async:\n");
506 hex_dump(stdout
, buf
, size
);
509 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
514 queue
= sender
->peer
->send_queue
;
516 queue
= sender
->vlan
->send_queue
;
519 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
522 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
523 const uint8_t *buf
, int size
,
524 NetPacketSent
*sent_cb
)
526 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
530 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
532 qemu_send_packet_async(vc
, buf
, size
, NULL
);
535 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
537 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
541 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
544 uint8_t buffer
[4096];
547 offset
= iov_to_buf(iov
, iovcnt
, buffer
, 0, sizeof(buffer
));
549 return vc
->info
->receive(vc
, buffer
, offset
);
552 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
554 const struct iovec
*iov
,
558 VLANClientState
*vc
= opaque
;
561 return iov_size(iov
, iovcnt
);
564 if (vc
->info
->receive_iov
) {
565 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
567 return vc_sendv_compat(vc
, iov
, iovcnt
);
571 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
573 const struct iovec
*iov
,
577 VLANState
*vlan
= opaque
;
581 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
589 ret
= iov_size(iov
, iovcnt
);
593 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
595 if (vc
->info
->receive_iov
) {
596 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
598 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
601 ret
= (ret
>= 0) ? ret
: len
;
607 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
608 const struct iovec
*iov
, int iovcnt
,
609 NetPacketSent
*sent_cb
)
613 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
614 return iov_size(iov
, iovcnt
);
618 queue
= sender
->peer
->send_queue
;
620 queue
= sender
->vlan
->send_queue
;
623 return qemu_net_queue_send_iov(queue
, sender
,
624 QEMU_NET_PACKET_FLAG_NONE
,
625 iov
, iovcnt
, sent_cb
);
629 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
631 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
634 /* find or alloc a new VLAN */
635 VLANState
*qemu_find_vlan(int id
, int allocate
)
639 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
640 if (vlan
->id
== id
) {
649 vlan
= g_malloc0(sizeof(VLANState
));
651 QTAILQ_INIT(&vlan
->clients
);
653 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
654 qemu_vlan_deliver_packet_iov
,
657 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
662 VLANClientState
*qemu_find_netdev(const char *id
)
666 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
667 if (vc
->info
->type
== NET_CLIENT_TYPE_NIC
)
669 if (!strcmp(vc
->name
, id
)) {
677 static int nic_get_free_idx(void)
681 for (index
= 0; index
< MAX_NICS
; index
++)
682 if (!nd_table
[index
].used
)
687 int qemu_show_nic_models(const char *arg
, const char *const *models
)
691 if (!arg
|| strcmp(arg
, "?"))
694 fprintf(stderr
, "qemu: Supported NIC models: ");
695 for (i
= 0 ; models
[i
]; i
++)
696 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
700 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
702 const char *models
[2];
707 if (qemu_show_nic_models(nd
->model
, models
))
709 if (qemu_find_nic_model(nd
, models
, model
) < 0)
713 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
714 const char *default_model
)
719 nd
->model
= g_strdup(default_model
);
721 for (i
= 0 ; models
[i
]; i
++) {
722 if (strcmp(nd
->model
, models
[i
]) == 0)
726 error_report("Unsupported NIC model: %s", nd
->model
);
730 int net_handle_fd_param(Monitor
*mon
, const char *param
)
734 if (!qemu_isdigit(param
[0]) && mon
) {
736 fd
= monitor_get_fd(mon
, param
);
738 error_report("No file descriptor named %s found", param
);
742 fd
= qemu_parse_fd(param
);
748 static int net_init_nic(QemuOpts
*opts
, const char *name
, VLANState
*vlan
)
754 idx
= nic_get_free_idx();
755 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
756 error_report("Too Many NICs");
762 memset(nd
, 0, sizeof(*nd
));
764 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
765 nd
->netdev
= qemu_find_netdev(netdev
);
767 error_report("netdev '%s' not found", netdev
);
775 nd
->name
= g_strdup(name
);
777 if (qemu_opt_get(opts
, "model")) {
778 nd
->model
= g_strdup(qemu_opt_get(opts
, "model"));
780 if (qemu_opt_get(opts
, "addr")) {
781 nd
->devaddr
= g_strdup(qemu_opt_get(opts
, "addr"));
784 if (qemu_opt_get(opts
, "macaddr") &&
785 net_parse_macaddr(nd
->macaddr
.a
, qemu_opt_get(opts
, "macaddr")) < 0) {
786 error_report("invalid syntax for ethernet address");
789 qemu_macaddr_default_if_unset(&nd
->macaddr
);
791 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
792 DEV_NVECTORS_UNSPECIFIED
);
793 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
794 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
795 error_report("invalid # of vectors: %d", nd
->nvectors
);
805 #define NET_COMMON_PARAMS_DESC \
808 .type = QEMU_OPT_STRING, \
809 .help = "net client type (nic, tap etc.)", \
812 .type = QEMU_OPT_NUMBER, \
813 .help = "vlan number", \
816 .type = QEMU_OPT_STRING, \
817 .help = "identifier for monitor commands", \
820 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
824 /* magic number, but compiler will warn if too small */
825 #define NET_MAX_DESC 20
827 static const struct {
829 net_client_init_func init
;
830 QemuOptDesc desc
[NET_MAX_DESC
];
831 } net_client_types
[NET_CLIENT_TYPE_MAX
] = {
832 [NET_CLIENT_TYPE_NONE
] = {
835 NET_COMMON_PARAMS_DESC
,
836 { /* end of list */ }
839 [NET_CLIENT_TYPE_NIC
] = {
841 .init
= net_init_nic
,
843 NET_COMMON_PARAMS_DESC
,
846 .type
= QEMU_OPT_STRING
,
847 .help
= "id of -netdev to connect to",
851 .type
= QEMU_OPT_STRING
,
852 .help
= "MAC address",
855 .type
= QEMU_OPT_STRING
,
856 .help
= "device model (e1000, rtl8139, virtio etc.)",
859 .type
= QEMU_OPT_STRING
,
860 .help
= "PCI device address",
863 .type
= QEMU_OPT_NUMBER
,
864 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
866 { /* end of list */ }
870 [NET_CLIENT_TYPE_USER
] = {
872 .init
= net_init_slirp
,
874 NET_COMMON_PARAMS_DESC
,
877 .type
= QEMU_OPT_STRING
,
878 .help
= "client hostname reported by the builtin DHCP server",
881 .type
= QEMU_OPT_STRING
,
882 .help
= "isolate the guest from the host (y|yes|n|no)",
885 .type
= QEMU_OPT_STRING
,
886 .help
= "legacy parameter, use net= instead",
889 .type
= QEMU_OPT_STRING
,
890 .help
= "IP address and optional netmask",
893 .type
= QEMU_OPT_STRING
,
894 .help
= "guest-visible address of the host",
897 .type
= QEMU_OPT_STRING
,
898 .help
= "root directory of the built-in TFTP server",
901 .type
= QEMU_OPT_STRING
,
902 .help
= "BOOTP filename, for use with tftp=",
905 .type
= QEMU_OPT_STRING
,
906 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
909 .type
= QEMU_OPT_STRING
,
910 .help
= "guest-visible address of the virtual nameserver",
913 .type
= QEMU_OPT_STRING
,
914 .help
= "root directory of the built-in SMB server",
917 .type
= QEMU_OPT_STRING
,
918 .help
= "IP address of the built-in SMB server",
921 .type
= QEMU_OPT_STRING
,
922 .help
= "guest port number to forward incoming TCP or UDP connections",
925 .type
= QEMU_OPT_STRING
,
926 .help
= "IP address and port to forward guest TCP connections",
928 { /* end of list */ }
932 [NET_CLIENT_TYPE_TAP
] = {
934 .init
= net_init_tap
,
936 NET_COMMON_PARAMS_DESC
,
939 .type
= QEMU_OPT_STRING
,
940 .help
= "interface name",
945 .type
= QEMU_OPT_STRING
,
946 .help
= "file descriptor of an already opened tap",
949 .type
= QEMU_OPT_STRING
,
950 .help
= "script to initialize the interface",
952 .name
= "downscript",
953 .type
= QEMU_OPT_STRING
,
954 .help
= "script to shut down the interface",
956 #ifdef CONFIG_NET_BRIDGE
958 .type
= QEMU_OPT_STRING
,
959 .help
= "command to execute to configure bridge",
963 .type
= QEMU_OPT_SIZE
,
964 .help
= "send buffer limit"
967 .type
= QEMU_OPT_BOOL
,
968 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
971 .type
= QEMU_OPT_BOOL
,
972 .help
= "enable vhost-net network accelerator",
975 .type
= QEMU_OPT_STRING
,
976 .help
= "file descriptor of an already opened vhost net device",
978 .name
= "vhostforce",
979 .type
= QEMU_OPT_BOOL
,
980 .help
= "force vhost on for non-MSIX virtio guests",
983 { /* end of list */ }
986 [NET_CLIENT_TYPE_SOCKET
] = {
988 .init
= net_init_socket
,
990 NET_COMMON_PARAMS_DESC
,
993 .type
= QEMU_OPT_STRING
,
994 .help
= "file descriptor of an already opened socket",
997 .type
= QEMU_OPT_STRING
,
998 .help
= "port number, and optional hostname, to listen on",
1001 .type
= QEMU_OPT_STRING
,
1002 .help
= "port number, and optional hostname, to connect to",
1005 .type
= QEMU_OPT_STRING
,
1006 .help
= "UDP multicast address and port number",
1008 .name
= "localaddr",
1009 .type
= QEMU_OPT_STRING
,
1010 .help
= "source address and port for multicast and udp packets",
1013 .type
= QEMU_OPT_STRING
,
1014 .help
= "UDP unicast address and port number",
1016 { /* end of list */ }
1020 [NET_CLIENT_TYPE_VDE
] = {
1022 .init
= net_init_vde
,
1024 NET_COMMON_PARAMS_DESC
,
1027 .type
= QEMU_OPT_STRING
,
1028 .help
= "socket path",
1031 .type
= QEMU_OPT_NUMBER
,
1032 .help
= "port number",
1035 .type
= QEMU_OPT_STRING
,
1036 .help
= "group owner of socket",
1039 .type
= QEMU_OPT_NUMBER
,
1040 .help
= "permissions for socket",
1042 { /* end of list */ }
1046 [NET_CLIENT_TYPE_DUMP
] = {
1048 .init
= net_init_dump
,
1050 NET_COMMON_PARAMS_DESC
,
1053 .type
= QEMU_OPT_SIZE
,
1054 .help
= "per-packet size limit (64k default)",
1057 .type
= QEMU_OPT_STRING
,
1058 .help
= "dump file path (default is qemu-vlan0.pcap)",
1060 { /* end of list */ }
1063 #ifdef CONFIG_NET_BRIDGE
1064 [NET_CLIENT_TYPE_BRIDGE
] = {
1066 .init
= net_init_bridge
,
1068 NET_COMMON_PARAMS_DESC
,
1071 .type
= QEMU_OPT_STRING
,
1072 .help
= "bridge name",
1075 .type
= QEMU_OPT_STRING
,
1076 .help
= "command to execute to configure bridge",
1078 { /* end of list */ }
1081 #endif /* CONFIG_NET_BRIDGE */
1084 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
)
1090 type
= qemu_opt_get(opts
, "type");
1092 error_set(errp
, QERR_MISSING_PARAMETER
, "type");
1097 if (strcmp(type
, "tap") != 0 &&
1098 #ifdef CONFIG_NET_BRIDGE
1099 strcmp(type
, "bridge") != 0 &&
1102 strcmp(type
, "user") != 0 &&
1105 strcmp(type
, "vde") != 0 &&
1107 strcmp(type
, "socket") != 0) {
1108 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1109 "a netdev backend type");
1113 if (qemu_opt_get(opts
, "vlan")) {
1114 error_set(errp
, QERR_INVALID_PARAMETER
, "vlan");
1117 if (qemu_opt_get(opts
, "name")) {
1118 error_set(errp
, QERR_INVALID_PARAMETER
, "name");
1121 if (!qemu_opts_id(opts
)) {
1122 error_set(errp
, QERR_MISSING_PARAMETER
, "id");
1127 name
= qemu_opts_id(opts
);
1129 name
= qemu_opt_get(opts
, "name");
1132 for (i
= 0; i
< NET_CLIENT_TYPE_MAX
; i
++) {
1133 if (net_client_types
[i
].type
!= NULL
&&
1134 !strcmp(net_client_types
[i
].type
, type
)) {
1135 Error
*local_err
= NULL
;
1136 VLANState
*vlan
= NULL
;
1139 qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0], &local_err
);
1140 if (error_is_set(&local_err
)) {
1141 error_propagate(errp
, local_err
);
1145 /* Do not add to a vlan if it's a -netdev or a nic with a
1146 * netdev= parameter. */
1148 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1149 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1153 if (net_client_types
[i
].init
) {
1154 ret
= net_client_types
[i
].init(opts
, name
, vlan
);
1156 /* TODO push error reporting into init() methods */
1157 error_set(errp
, QERR_DEVICE_INIT_FAILED
, type
);
1165 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "type",
1166 "a network client type");
1170 static int net_host_check_device(const char *device
)
1173 const char *valid_param_list
[] = { "tap", "socket", "dump"
1174 #ifdef CONFIG_NET_BRIDGE
1184 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1185 if (!strncmp(valid_param_list
[i
], device
,
1186 strlen(valid_param_list
[i
])))
1193 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1195 const char *device
= qdict_get_str(qdict
, "device");
1196 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1197 Error
*local_err
= NULL
;
1200 if (!net_host_check_device(device
)) {
1201 monitor_printf(mon
, "invalid host network device %s\n", device
);
1205 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
1210 qemu_opt_set(opts
, "type", device
);
1212 net_client_init(opts
, 0, &local_err
);
1213 if (error_is_set(&local_err
)) {
1214 qerror_report_err(local_err
);
1215 error_free(local_err
);
1216 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1220 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1222 VLANClientState
*vc
;
1223 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1224 const char *device
= qdict_get_str(qdict
, "device");
1226 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1230 if (!net_host_check_device(vc
->model
)) {
1231 monitor_printf(mon
, "invalid host network device %s\n", device
);
1234 qemu_del_vlan_client(vc
);
1237 void netdev_add(QemuOpts
*opts
, Error
**errp
)
1239 net_client_init(opts
, 1, errp
);
1242 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
1244 Error
*local_err
= NULL
;
1245 QemuOptsList
*opts_list
;
1248 opts_list
= qemu_find_opts_err("netdev", &local_err
);
1249 if (error_is_set(&local_err
)) {
1253 opts
= qemu_opts_from_qdict(opts_list
, qdict
, &local_err
);
1254 if (error_is_set(&local_err
)) {
1258 netdev_add(opts
, &local_err
);
1259 if (error_is_set(&local_err
)) {
1260 qemu_opts_del(opts
);
1267 qerror_report_err(local_err
);
1268 error_free(local_err
);
1272 void qmp_netdev_del(const char *id
, Error
**errp
)
1274 VLANClientState
*vc
;
1276 vc
= qemu_find_netdev(id
);
1278 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
1282 qemu_del_vlan_client(vc
);
1283 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp
), id
));
1286 static void print_net_client(Monitor
*mon
, VLANClientState
*vc
)
1288 monitor_printf(mon
, "%s: type=%s,%s\n", vc
->name
,
1289 net_client_types
[vc
->info
->type
].type
, vc
->info_str
);
1292 void do_info_network(Monitor
*mon
)
1295 VLANClientState
*vc
, *peer
;
1296 net_client_type type
;
1298 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1299 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1301 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1302 monitor_printf(mon
, " ");
1303 print_net_client(mon
, vc
);
1306 monitor_printf(mon
, "Devices not on any VLAN:\n");
1307 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1309 type
= vc
->info
->type
;
1310 if (!peer
|| type
== NET_CLIENT_TYPE_NIC
) {
1311 monitor_printf(mon
, " ");
1312 print_net_client(mon
, vc
);
1313 } /* else it's a netdev connected to a NIC, printed with the NIC */
1314 if (peer
&& type
== NET_CLIENT_TYPE_NIC
) {
1315 monitor_printf(mon
, " \\ ");
1316 print_net_client(mon
, peer
);
1321 void qmp_set_link(const char *name
, bool up
, Error
**errp
)
1324 VLANClientState
*vc
= NULL
;
1326 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1327 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1328 if (strcmp(vc
->name
, name
) == 0) {
1333 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1334 if (!strcmp(vc
->name
, name
)) {
1341 error_set(errp
, QERR_DEVICE_NOT_FOUND
, name
);
1345 vc
->link_down
= !up
;
1347 if (vc
->info
->link_status_changed
) {
1348 vc
->info
->link_status_changed(vc
);
1351 /* Notify peer. Don't update peer link status: this makes it possible to
1352 * disconnect from host network without notifying the guest.
1353 * FIXME: is disconnected link status change operation useful?
1355 * Current behaviour is compatible with qemu vlans where there could be
1356 * multiple clients that can still communicate with each other in
1357 * disconnected mode. For now maintain this compatibility. */
1358 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1359 vc
->peer
->info
->link_status_changed(vc
->peer
);
1363 void net_cleanup(void)
1366 VLANClientState
*vc
, *next_vc
;
1368 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1369 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1370 qemu_del_vlan_client(vc
);
1374 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1375 qemu_del_vlan_client(vc
);
1379 void net_check_clients(void)
1382 VLANClientState
*vc
;
1385 /* Don't warn about the default network setup that you get if
1386 * no command line -net or -netdev options are specified. There
1387 * are two cases that we would otherwise complain about:
1388 * (1) board doesn't support a NIC but the implicit "-net nic"
1390 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1391 * sets up a nic that isn't connected to anything.
1397 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1398 int has_nic
= 0, has_host_dev
= 0;
1400 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1401 switch (vc
->info
->type
) {
1402 case NET_CLIENT_TYPE_NIC
:
1405 case NET_CLIENT_TYPE_USER
:
1406 case NET_CLIENT_TYPE_TAP
:
1407 case NET_CLIENT_TYPE_SOCKET
:
1408 case NET_CLIENT_TYPE_VDE
:
1414 if (has_host_dev
&& !has_nic
)
1415 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1416 if (has_nic
&& !has_host_dev
)
1418 "Warning: vlan %d is not connected to host network\n",
1421 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1423 fprintf(stderr
, "Warning: %s %s has no peer\n",
1424 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1429 /* Check that all NICs requested via -net nic actually got created.
1430 * NICs created via -device don't need to be checked here because
1431 * they are always instantiated.
1433 for (i
= 0; i
< MAX_NICS
; i
++) {
1434 NICInfo
*nd
= &nd_table
[i
];
1435 if (nd
->used
&& !nd
->instantiated
) {
1436 fprintf(stderr
, "Warning: requested NIC (%s, model %s) "
1437 "was not created (not supported by this machine?)\n",
1438 nd
->name
? nd
->name
: "anonymous",
1439 nd
->model
? nd
->model
: "unspecified");
1444 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1446 Error
*local_err
= NULL
;
1448 net_client_init(opts
, 0, &local_err
);
1449 if (error_is_set(&local_err
)) {
1450 qerror_report_err(local_err
);
1451 error_free(local_err
);
1458 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1460 Error
*local_err
= NULL
;
1463 ret
= net_client_init(opts
, 1, &local_err
);
1464 if (error_is_set(&local_err
)) {
1465 qerror_report_err(local_err
);
1466 error_free(local_err
);
1473 int net_init_clients(void)
1475 QemuOptsList
*net
= qemu_find_opts("net");
1478 /* if no clients, we use a default config */
1479 qemu_opts_set(net
, NULL
, "type", "nic");
1481 qemu_opts_set(net
, NULL
, "type", "user");
1485 QTAILQ_INIT(&vlans
);
1486 QTAILQ_INIT(&non_vlan_clients
);
1488 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1491 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1498 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1500 #if defined(CONFIG_SLIRP)
1502 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1507 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {
1517 unsigned compute_mcast_idx(const uint8_t *ep
)
1524 for (i
= 0; i
< 6; i
++) {
1526 for (j
= 0; j
< 8; j
++) {
1527 carry
= ((crc
& 0x80000000L
) ? 1 : 0) ^ (b
& 0x01);
1531 crc
= ((crc
^ POLYNOMIAL
) | carry
);