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"
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
)
157 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
160 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
161 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
167 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
169 return qemu_strdup(buf
);
172 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
177 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
179 const struct iovec
*iov
,
183 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
185 VLANClientState
*peer
,
191 assert(info
->size
>= sizeof(VLANClientState
));
193 vc
= qemu_mallocz(info
->size
);
196 vc
->model
= qemu_strdup(model
);
198 vc
->name
= qemu_strdup(name
);
200 vc
->name
= assign_name(vc
, model
);
206 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
213 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
215 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
216 qemu_deliver_packet_iov
,
223 NICState
*qemu_new_nic(NetClientInfo
*info
,
232 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
233 assert(info
->size
>= sizeof(NICState
));
235 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
237 nic
= DO_UPCAST(NICState
, nc
, nc
);
239 nic
->opaque
= opaque
;
244 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
247 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
249 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
252 if (vc
->info
->cleanup
) {
253 vc
->info
->cleanup(vc
);
257 static void qemu_free_vlan_client(VLANClientState
*vc
)
260 if (vc
->send_queue
) {
261 qemu_del_net_queue(vc
->send_queue
);
264 vc
->peer
->peer
= NULL
;
268 qemu_free(vc
->model
);
272 void qemu_del_vlan_client(VLANClientState
*vc
)
274 /* If there is a peer NIC, delete and cleanup client, but do not free. */
275 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
276 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
277 if (nic
->peer_deleted
) {
280 nic
->peer_deleted
= true;
281 /* Let NIC know peer is gone. */
282 vc
->peer
->link_down
= true;
283 if (vc
->peer
->info
->link_status_changed
) {
284 vc
->peer
->info
->link_status_changed(vc
->peer
);
286 qemu_cleanup_vlan_client(vc
);
290 /* If this is a peer NIC and peer has already been deleted, free it now. */
291 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
292 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
293 if (nic
->peer_deleted
) {
294 qemu_free_vlan_client(vc
->peer
);
298 qemu_cleanup_vlan_client(vc
);
299 qemu_free_vlan_client(vc
);
303 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
304 const char *client_str
)
309 vlan
= qemu_find_vlan(vlan_id
, 0);
311 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
315 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
316 if (!strcmp(vc
->name
, client_str
)) {
321 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
322 client_str
, vlan_id
);
328 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
333 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
334 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
335 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
339 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
340 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
341 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
342 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
348 int qemu_can_send_packet(VLANClientState
*sender
)
350 VLANState
*vlan
= sender
->vlan
;
354 if (sender
->peer
->receive_disabled
) {
356 } else if (sender
->peer
->info
->can_receive
&&
357 !sender
->peer
->info
->can_receive(sender
->peer
)) {
368 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
373 /* no can_receive() handler, they can always receive */
374 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
381 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
387 VLANClientState
*vc
= opaque
;
394 if (vc
->receive_disabled
) {
398 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
399 ret
= vc
->info
->receive_raw(vc
, data
, size
);
401 ret
= vc
->info
->receive(vc
, data
, size
);
405 vc
->receive_disabled
= 1;
411 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
417 VLANState
*vlan
= opaque
;
421 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
433 if (vc
->receive_disabled
) {
438 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
439 len
= vc
->info
->receive_raw(vc
, buf
, size
);
441 len
= vc
->info
->receive(vc
, buf
, size
);
445 vc
->receive_disabled
= 1;
448 ret
= (ret
>= 0) ? ret
: len
;
455 void qemu_purge_queued_packets(VLANClientState
*vc
)
459 if (!vc
->peer
&& !vc
->vlan
) {
464 queue
= vc
->peer
->send_queue
;
466 queue
= vc
->vlan
->send_queue
;
469 qemu_net_queue_purge(queue
, vc
);
472 void qemu_flush_queued_packets(VLANClientState
*vc
)
476 vc
->receive_disabled
= 0;
479 queue
= vc
->vlan
->send_queue
;
481 queue
= vc
->send_queue
;
484 qemu_net_queue_flush(queue
);
487 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
489 const uint8_t *buf
, int size
,
490 NetPacketSent
*sent_cb
)
495 printf("qemu_send_packet_async:\n");
496 hex_dump(stdout
, buf
, size
);
499 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
504 queue
= sender
->peer
->send_queue
;
506 queue
= sender
->vlan
->send_queue
;
509 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
512 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
513 const uint8_t *buf
, int size
,
514 NetPacketSent
*sent_cb
)
516 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
520 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
522 qemu_send_packet_async(vc
, buf
, size
, NULL
);
525 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
527 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
531 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
534 uint8_t buffer
[4096];
537 offset
= iov_to_buf(iov
, iovcnt
, buffer
, 0, sizeof(buffer
));
539 return vc
->info
->receive(vc
, buffer
, offset
);
542 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
544 const struct iovec
*iov
,
548 VLANClientState
*vc
= opaque
;
551 return iov_size(iov
, iovcnt
);
554 if (vc
->info
->receive_iov
) {
555 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
557 return vc_sendv_compat(vc
, iov
, iovcnt
);
561 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
563 const struct iovec
*iov
,
567 VLANState
*vlan
= opaque
;
571 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
579 ret
= iov_size(iov
, iovcnt
);
583 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
585 if (vc
->info
->receive_iov
) {
586 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
588 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
591 ret
= (ret
>= 0) ? ret
: len
;
597 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
598 const struct iovec
*iov
, int iovcnt
,
599 NetPacketSent
*sent_cb
)
603 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
604 return iov_size(iov
, iovcnt
);
608 queue
= sender
->peer
->send_queue
;
610 queue
= sender
->vlan
->send_queue
;
613 return qemu_net_queue_send_iov(queue
, sender
,
614 QEMU_NET_PACKET_FLAG_NONE
,
615 iov
, iovcnt
, sent_cb
);
619 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
621 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
624 /* find or alloc a new VLAN */
625 VLANState
*qemu_find_vlan(int id
, int allocate
)
629 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
630 if (vlan
->id
== id
) {
639 vlan
= qemu_mallocz(sizeof(VLANState
));
641 QTAILQ_INIT(&vlan
->clients
);
643 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
644 qemu_vlan_deliver_packet_iov
,
647 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
652 VLANClientState
*qemu_find_netdev(const char *id
)
656 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
657 if (!strcmp(vc
->name
, id
)) {
665 static int nic_get_free_idx(void)
669 for (index
= 0; index
< MAX_NICS
; index
++)
670 if (!nd_table
[index
].used
)
675 int qemu_show_nic_models(const char *arg
, const char *const *models
)
679 if (!arg
|| strcmp(arg
, "?"))
682 fprintf(stderr
, "qemu: Supported NIC models: ");
683 for (i
= 0 ; models
[i
]; i
++)
684 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
688 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
690 const char *models
[2];
695 if (qemu_show_nic_models(nd
->model
, models
))
697 if (qemu_find_nic_model(nd
, models
, model
) < 0)
701 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
702 const char *default_model
)
707 nd
->model
= qemu_strdup(default_model
);
709 for (i
= 0 ; models
[i
]; i
++) {
710 if (strcmp(nd
->model
, models
[i
]) == 0)
714 error_report("qemu: Unsupported NIC model: %s", nd
->model
);
718 int net_handle_fd_param(Monitor
*mon
, const char *param
)
722 if (!qemu_isdigit(param
[0]) && mon
) {
724 fd
= monitor_get_fd(mon
, param
);
726 error_report("No file descriptor named %s found", param
);
732 fd
= strtol(param
, &endptr
, 10);
733 if (*endptr
|| (fd
== 0 && param
== endptr
)) {
741 static int net_init_nic(QemuOpts
*opts
,
750 idx
= nic_get_free_idx();
751 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
752 error_report("Too Many NICs");
758 memset(nd
, 0, sizeof(*nd
));
760 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
761 nd
->netdev
= qemu_find_netdev(netdev
);
763 error_report("netdev '%s' not found", netdev
);
771 nd
->name
= qemu_strdup(name
);
773 if (qemu_opt_get(opts
, "model")) {
774 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
776 if (qemu_opt_get(opts
, "addr")) {
777 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
780 nd
->macaddr
[0] = 0x52;
781 nd
->macaddr
[1] = 0x54;
782 nd
->macaddr
[2] = 0x00;
783 nd
->macaddr
[3] = 0x12;
784 nd
->macaddr
[4] = 0x34;
785 nd
->macaddr
[5] = 0x56 + idx
;
787 if (qemu_opt_get(opts
, "macaddr") &&
788 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
789 error_report("invalid syntax for ethernet address");
793 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
794 DEV_NVECTORS_UNSPECIFIED
);
795 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
796 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
797 error_report("invalid # of vectors: %d", nd
->nvectors
);
807 #define NET_COMMON_PARAMS_DESC \
810 .type = QEMU_OPT_STRING, \
811 .help = "net client type (nic, tap etc.)", \
814 .type = QEMU_OPT_NUMBER, \
815 .help = "vlan number", \
818 .type = QEMU_OPT_STRING, \
819 .help = "identifier for monitor commands", \
822 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
827 /* magic number, but compiler will warn if too small */
828 #define NET_MAX_DESC 20
830 static const struct {
832 net_client_init_func init
;
833 QemuOptDesc desc
[NET_MAX_DESC
];
834 } net_client_types
[] = {
838 NET_COMMON_PARAMS_DESC
,
839 { /* end of list */ }
843 .init
= net_init_nic
,
845 NET_COMMON_PARAMS_DESC
,
848 .type
= QEMU_OPT_STRING
,
849 .help
= "id of -netdev to connect to",
853 .type
= QEMU_OPT_STRING
,
854 .help
= "MAC address",
857 .type
= QEMU_OPT_STRING
,
858 .help
= "device model (e1000, rtl8139, virtio etc.)",
861 .type
= QEMU_OPT_STRING
,
862 .help
= "PCI device address",
865 .type
= QEMU_OPT_NUMBER
,
866 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
868 { /* end of list */ }
873 .init
= net_init_slirp
,
875 NET_COMMON_PARAMS_DESC
,
878 .type
= QEMU_OPT_STRING
,
879 .help
= "client hostname reported by the builtin DHCP server",
882 .type
= QEMU_OPT_STRING
,
883 .help
= "isolate the guest from the host (y|yes|n|no)",
886 .type
= QEMU_OPT_STRING
,
887 .help
= "legacy parameter, use net= instead",
890 .type
= QEMU_OPT_STRING
,
891 .help
= "IP address and optional netmask",
894 .type
= QEMU_OPT_STRING
,
895 .help
= "guest-visible address of the host",
898 .type
= QEMU_OPT_STRING
,
899 .help
= "root directory of the built-in TFTP server",
902 .type
= QEMU_OPT_STRING
,
903 .help
= "BOOTP filename, for use with tftp=",
906 .type
= QEMU_OPT_STRING
,
907 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
910 .type
= QEMU_OPT_STRING
,
911 .help
= "guest-visible address of the virtual nameserver",
914 .type
= QEMU_OPT_STRING
,
915 .help
= "root directory of the built-in SMB server",
918 .type
= QEMU_OPT_STRING
,
919 .help
= "IP address of the built-in SMB server",
922 .type
= QEMU_OPT_STRING
,
923 .help
= "guest port number to forward incoming TCP or UDP connections",
926 .type
= QEMU_OPT_STRING
,
927 .help
= "IP address and port to forward guest TCP connections",
929 { /* end of list */ }
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",
957 .type
= QEMU_OPT_SIZE
,
958 .help
= "send buffer limit"
961 .type
= QEMU_OPT_BOOL
,
962 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
965 .type
= QEMU_OPT_BOOL
,
966 .help
= "enable vhost-net network accelerator",
969 .type
= QEMU_OPT_STRING
,
970 .help
= "file descriptor of an already opened vhost net device",
972 .name
= "vhostforce",
973 .type
= QEMU_OPT_BOOL
,
974 .help
= "force vhost on for non-MSIX virtio guests",
977 { /* end of list */ }
981 .init
= net_init_socket
,
983 NET_COMMON_PARAMS_DESC
,
986 .type
= QEMU_OPT_STRING
,
987 .help
= "file descriptor of an already opened socket",
990 .type
= QEMU_OPT_STRING
,
991 .help
= "port number, and optional hostname, to listen on",
994 .type
= QEMU_OPT_STRING
,
995 .help
= "port number, and optional hostname, to connect to",
998 .type
= QEMU_OPT_STRING
,
999 .help
= "UDP multicast address and port number",
1001 .name
= "localaddr",
1002 .type
= QEMU_OPT_STRING
,
1003 .help
= "source address for multicast packets",
1005 { /* end of list */ }
1010 .init
= net_init_vde
,
1012 NET_COMMON_PARAMS_DESC
,
1015 .type
= QEMU_OPT_STRING
,
1016 .help
= "socket path",
1019 .type
= QEMU_OPT_NUMBER
,
1020 .help
= "port number",
1023 .type
= QEMU_OPT_STRING
,
1024 .help
= "group owner of socket",
1027 .type
= QEMU_OPT_NUMBER
,
1028 .help
= "permissions for socket",
1030 { /* end of list */ }
1035 .init
= net_init_dump
,
1037 NET_COMMON_PARAMS_DESC
,
1040 .type
= QEMU_OPT_SIZE
,
1041 .help
= "per-packet size limit (64k default)",
1044 .type
= QEMU_OPT_STRING
,
1045 .help
= "dump file path (default is qemu-vlan0.pcap)",
1047 { /* end of list */ }
1050 { /* end of list */ }
1053 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1059 type
= qemu_opt_get(opts
, "type");
1061 qerror_report(QERR_MISSING_PARAMETER
, "type");
1066 if (strcmp(type
, "tap") != 0 &&
1068 strcmp(type
, "user") != 0 &&
1071 strcmp(type
, "vde") != 0 &&
1073 strcmp(type
, "socket") != 0) {
1074 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1075 "a netdev backend type");
1079 if (qemu_opt_get(opts
, "vlan")) {
1080 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1083 if (qemu_opt_get(opts
, "name")) {
1084 qerror_report(QERR_INVALID_PARAMETER
, "name");
1087 if (!qemu_opts_id(opts
)) {
1088 qerror_report(QERR_MISSING_PARAMETER
, "id");
1093 name
= qemu_opts_id(opts
);
1095 name
= qemu_opt_get(opts
, "name");
1098 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1099 if (!strcmp(net_client_types
[i
].type
, type
)) {
1100 VLANState
*vlan
= NULL
;
1103 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1107 /* Do not add to a vlan if it's a -netdev or a nic with a
1108 * netdev= parameter. */
1110 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1111 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1115 if (net_client_types
[i
].init
) {
1116 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1118 /* TODO push error reporting into init() methods */
1119 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1127 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1128 "a network client type");
1132 static int net_host_check_device(const char *device
)
1135 const char *valid_param_list
[] = { "tap", "socket", "dump"
1143 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1144 if (!strncmp(valid_param_list
[i
], device
,
1145 strlen(valid_param_list
[i
])))
1152 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1154 const char *device
= qdict_get_str(qdict
, "device");
1155 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1158 if (!net_host_check_device(device
)) {
1159 monitor_printf(mon
, "invalid host network device %s\n", device
);
1163 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
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 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1197 opts
= qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict
);
1202 res
= net_client_init(mon
, opts
, 1);
1204 qemu_opts_del(opts
);
1210 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1212 const char *id
= qdict_get_str(qdict
, "id");
1213 VLANClientState
*vc
;
1215 vc
= qemu_find_netdev(id
);
1216 if (!vc
|| vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1217 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1220 qemu_del_vlan_client(vc
);
1221 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id
));
1225 void do_info_network(Monitor
*mon
)
1228 VLANClientState
*vc
;
1230 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1231 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1233 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1234 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1237 monitor_printf(mon
, "Devices not on any VLAN:\n");
1238 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1239 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1241 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1243 monitor_printf(mon
, "\n");
1247 int do_set_link(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1250 VLANClientState
*vc
= NULL
;
1251 const char *name
= qdict_get_str(qdict
, "name");
1252 int up
= qdict_get_bool(qdict
, "up");
1254 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1255 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1256 if (strcmp(vc
->name
, name
) == 0) {
1261 vc
= qemu_find_netdev(name
);
1265 qerror_report(QERR_DEVICE_NOT_FOUND
, name
);
1269 vc
->link_down
= !up
;
1271 if (vc
->info
->link_status_changed
) {
1272 vc
->info
->link_status_changed(vc
);
1275 /* Notify peer. Don't update peer link status: this makes it possible to
1276 * disconnect from host network without notifying the guest.
1277 * FIXME: is disconnected link status change operation useful?
1279 * Current behaviour is compatible with qemu vlans where there could be
1280 * multiple clients that can still communicate with each other in
1281 * disconnected mode. For now maintain this compatibility. */
1282 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1283 vc
->peer
->info
->link_status_changed(vc
->peer
);
1288 void net_cleanup(void)
1291 VLANClientState
*vc
, *next_vc
;
1293 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1294 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1295 qemu_del_vlan_client(vc
);
1299 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1300 qemu_del_vlan_client(vc
);
1304 void net_check_clients(void)
1307 VLANClientState
*vc
;
1310 /* Don't warn about the default network setup that you get if
1311 * no command line -net options are specified. There are two
1312 * cases that we would otherwise complain about:
1313 * (1) board doesn't support a NIC but the implicit "-net nic"
1314 * requested one; we'd otherwise complain about more NICs being
1315 * specified than we support, and also that the vlan set up by
1316 * the implicit "-net user" didn't have any NICs connected to it
1317 * (2) CONFIG_SLIRP not set: we'd otherwise complain about the
1318 * implicit "-net nic" setting up a nic that wasn't connected to
1325 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1326 int has_nic
= 0, has_host_dev
= 0;
1328 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1329 switch (vc
->info
->type
) {
1330 case NET_CLIENT_TYPE_NIC
:
1334 case NET_CLIENT_TYPE_SLIRP
:
1335 case NET_CLIENT_TYPE_TAP
:
1336 case NET_CLIENT_TYPE_SOCKET
:
1337 case NET_CLIENT_TYPE_VDE
:
1343 if (has_host_dev
&& !has_nic
)
1344 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1345 if (has_nic
&& !has_host_dev
)
1347 "Warning: vlan %d is not connected to host network\n",
1350 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1351 if (vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1355 fprintf(stderr
, "Warning: %s %s has no peer\n",
1356 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1360 if (seen_nics
!= nb_nics
) {
1361 /* Number of NICs requested by user on command line doesn't match
1362 * the number the model actually registered with us.
1363 * This will generally only happen for models of embedded boards
1364 * with no PCI bus or similar. PCI based machines can instantiate
1365 * all requested NICs as PCI devices but usually embedded boards
1366 * only have a single NIC.
1368 fprintf(stderr
, "Warning: more nics requested than this machine "
1369 "supports; some have been ignored\n");
1373 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1375 if (net_client_init(NULL
, opts
, 0) < 0)
1380 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1382 return net_client_init(NULL
, opts
, 1);
1385 int net_init_clients(void)
1387 QemuOptsList
*net
= qemu_find_opts("net");
1390 /* if no clients, we use a default config */
1391 qemu_opts_set(net
, NULL
, "type", "nic");
1393 qemu_opts_set(net
, NULL
, "type", "user");
1397 QTAILQ_INIT(&vlans
);
1398 QTAILQ_INIT(&non_vlan_clients
);
1400 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1403 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1410 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1412 #if defined(CONFIG_SLIRP)
1414 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1419 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {