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"
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_port(struct sockaddr_in
*saddr
, const char *str
)
104 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
106 saddr
->sin_family
= AF_INET
;
107 if (buf
[0] == '\0') {
108 saddr
->sin_addr
.s_addr
= 0;
110 if (qemu_isdigit(buf
[0])) {
111 if (!inet_aton(buf
, &saddr
->sin_addr
))
114 if ((he
= gethostbyname(buf
)) == NULL
)
116 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
119 port
= strtol(p
, (char **)&r
, 0);
122 saddr
->sin_port
= htons(port
);
126 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
128 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
129 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
131 macaddr
[0], macaddr
[1], macaddr
[2],
132 macaddr
[3], macaddr
[4], macaddr
[5]);
135 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
137 static int index
= 0;
138 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
140 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
142 macaddr
->a
[0] = 0x52;
143 macaddr
->a
[1] = 0x54;
144 macaddr
->a
[2] = 0x00;
145 macaddr
->a
[3] = 0x12;
146 macaddr
->a
[4] = 0x34;
147 macaddr
->a
[5] = 0x56 + index
++;
150 static char *assign_name(VLANClientState
*vc1
, const char *model
)
156 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
159 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
160 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
166 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
168 return qemu_strdup(buf
);
171 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
176 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
178 const struct iovec
*iov
,
182 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
184 VLANClientState
*peer
,
190 assert(info
->size
>= sizeof(VLANClientState
));
192 vc
= qemu_mallocz(info
->size
);
195 vc
->model
= qemu_strdup(model
);
197 vc
->name
= qemu_strdup(name
);
199 vc
->name
= assign_name(vc
, model
);
205 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
212 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
214 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
215 qemu_deliver_packet_iov
,
222 NICState
*qemu_new_nic(NetClientInfo
*info
,
231 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
232 assert(info
->size
>= sizeof(NICState
));
234 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
236 nic
= DO_UPCAST(NICState
, nc
, nc
);
238 nic
->opaque
= opaque
;
243 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
246 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
248 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
251 if (vc
->info
->cleanup
) {
252 vc
->info
->cleanup(vc
);
256 static void qemu_free_vlan_client(VLANClientState
*vc
)
259 if (vc
->send_queue
) {
260 qemu_del_net_queue(vc
->send_queue
);
263 vc
->peer
->peer
= NULL
;
267 qemu_free(vc
->model
);
271 void qemu_del_vlan_client(VLANClientState
*vc
)
273 /* If there is a peer NIC, delete and cleanup client, but do not free. */
274 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
275 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
276 if (nic
->peer_deleted
) {
279 nic
->peer_deleted
= true;
280 /* Let NIC know peer is gone. */
281 vc
->peer
->link_down
= true;
282 if (vc
->peer
->info
->link_status_changed
) {
283 vc
->peer
->info
->link_status_changed(vc
->peer
);
285 qemu_cleanup_vlan_client(vc
);
289 /* If this is a peer NIC and peer has already been deleted, free it now. */
290 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
291 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
292 if (nic
->peer_deleted
) {
293 qemu_free_vlan_client(vc
->peer
);
297 qemu_cleanup_vlan_client(vc
);
298 qemu_free_vlan_client(vc
);
302 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
303 const char *client_str
)
308 vlan
= qemu_find_vlan(vlan_id
, 0);
310 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
314 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
315 if (!strcmp(vc
->name
, client_str
)) {
320 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
321 client_str
, vlan_id
);
327 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
332 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
333 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
334 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
338 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
339 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
340 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
341 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
347 int qemu_can_send_packet(VLANClientState
*sender
)
349 VLANState
*vlan
= sender
->vlan
;
353 if (sender
->peer
->receive_disabled
) {
355 } else if (sender
->peer
->info
->can_receive
&&
356 !sender
->peer
->info
->can_receive(sender
->peer
)) {
367 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
372 /* no can_receive() handler, they can always receive */
373 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
380 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
386 VLANClientState
*vc
= opaque
;
393 if (vc
->receive_disabled
) {
397 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
398 ret
= vc
->info
->receive_raw(vc
, data
, size
);
400 ret
= vc
->info
->receive(vc
, data
, size
);
404 vc
->receive_disabled
= 1;
410 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
416 VLANState
*vlan
= opaque
;
420 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
432 if (vc
->receive_disabled
) {
437 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
438 len
= vc
->info
->receive_raw(vc
, buf
, size
);
440 len
= vc
->info
->receive(vc
, buf
, size
);
444 vc
->receive_disabled
= 1;
447 ret
= (ret
>= 0) ? ret
: len
;
454 void qemu_purge_queued_packets(VLANClientState
*vc
)
458 if (!vc
->peer
&& !vc
->vlan
) {
463 queue
= vc
->peer
->send_queue
;
465 queue
= vc
->vlan
->send_queue
;
468 qemu_net_queue_purge(queue
, vc
);
471 void qemu_flush_queued_packets(VLANClientState
*vc
)
475 vc
->receive_disabled
= 0;
478 queue
= vc
->vlan
->send_queue
;
480 queue
= vc
->send_queue
;
483 qemu_net_queue_flush(queue
);
486 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
488 const uint8_t *buf
, int size
,
489 NetPacketSent
*sent_cb
)
494 printf("qemu_send_packet_async:\n");
495 hex_dump(stdout
, buf
, size
);
498 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
503 queue
= sender
->peer
->send_queue
;
505 queue
= sender
->vlan
->send_queue
;
508 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
511 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
512 const uint8_t *buf
, int size
,
513 NetPacketSent
*sent_cb
)
515 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
519 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
521 qemu_send_packet_async(vc
, buf
, size
, NULL
);
524 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
526 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
530 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
533 uint8_t buffer
[4096];
536 offset
= iov_to_buf(iov
, iovcnt
, buffer
, 0, sizeof(buffer
));
538 return vc
->info
->receive(vc
, buffer
, offset
);
541 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
543 const struct iovec
*iov
,
547 VLANClientState
*vc
= opaque
;
550 return iov_size(iov
, iovcnt
);
553 if (vc
->info
->receive_iov
) {
554 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
556 return vc_sendv_compat(vc
, iov
, iovcnt
);
560 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
562 const struct iovec
*iov
,
566 VLANState
*vlan
= opaque
;
570 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
578 ret
= iov_size(iov
, iovcnt
);
582 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
584 if (vc
->info
->receive_iov
) {
585 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
587 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
590 ret
= (ret
>= 0) ? ret
: len
;
596 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
597 const struct iovec
*iov
, int iovcnt
,
598 NetPacketSent
*sent_cb
)
602 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
603 return iov_size(iov
, iovcnt
);
607 queue
= sender
->peer
->send_queue
;
609 queue
= sender
->vlan
->send_queue
;
612 return qemu_net_queue_send_iov(queue
, sender
,
613 QEMU_NET_PACKET_FLAG_NONE
,
614 iov
, iovcnt
, sent_cb
);
618 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
620 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
623 /* find or alloc a new VLAN */
624 VLANState
*qemu_find_vlan(int id
, int allocate
)
628 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
629 if (vlan
->id
== id
) {
638 vlan
= qemu_mallocz(sizeof(VLANState
));
640 QTAILQ_INIT(&vlan
->clients
);
642 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
643 qemu_vlan_deliver_packet_iov
,
646 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
651 VLANClientState
*qemu_find_netdev(const char *id
)
655 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
656 if (!strcmp(vc
->name
, id
)) {
664 static int nic_get_free_idx(void)
668 for (index
= 0; index
< MAX_NICS
; index
++)
669 if (!nd_table
[index
].used
)
674 int qemu_show_nic_models(const char *arg
, const char *const *models
)
678 if (!arg
|| strcmp(arg
, "?"))
681 fprintf(stderr
, "qemu: Supported NIC models: ");
682 for (i
= 0 ; models
[i
]; i
++)
683 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
687 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
689 const char *models
[2];
694 if (qemu_show_nic_models(nd
->model
, models
))
696 if (qemu_find_nic_model(nd
, models
, model
) < 0)
700 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
701 const char *default_model
)
706 nd
->model
= qemu_strdup(default_model
);
708 for (i
= 0 ; models
[i
]; i
++) {
709 if (strcmp(nd
->model
, models
[i
]) == 0)
713 error_report("qemu: Unsupported NIC model: %s", nd
->model
);
717 int net_handle_fd_param(Monitor
*mon
, const char *param
)
721 if (!qemu_isdigit(param
[0]) && mon
) {
723 fd
= monitor_get_fd(mon
, param
);
725 error_report("No file descriptor named %s found", param
);
731 fd
= strtol(param
, &endptr
, 10);
732 if (*endptr
|| (fd
== 0 && param
== endptr
)) {
740 static int net_init_nic(QemuOpts
*opts
,
749 idx
= nic_get_free_idx();
750 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
751 error_report("Too Many NICs");
757 memset(nd
, 0, sizeof(*nd
));
759 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
760 nd
->netdev
= qemu_find_netdev(netdev
);
762 error_report("netdev '%s' not found", netdev
);
770 nd
->name
= qemu_strdup(name
);
772 if (qemu_opt_get(opts
, "model")) {
773 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
775 if (qemu_opt_get(opts
, "addr")) {
776 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
779 nd
->macaddr
[0] = 0x52;
780 nd
->macaddr
[1] = 0x54;
781 nd
->macaddr
[2] = 0x00;
782 nd
->macaddr
[3] = 0x12;
783 nd
->macaddr
[4] = 0x34;
784 nd
->macaddr
[5] = 0x56 + idx
;
786 if (qemu_opt_get(opts
, "macaddr") &&
787 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
788 error_report("invalid syntax for ethernet address");
792 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
793 DEV_NVECTORS_UNSPECIFIED
);
794 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
795 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
796 error_report("invalid # of vectors: %d", nd
->nvectors
);
806 #define NET_COMMON_PARAMS_DESC \
809 .type = QEMU_OPT_STRING, \
810 .help = "net client type (nic, tap etc.)", \
813 .type = QEMU_OPT_NUMBER, \
814 .help = "vlan number", \
817 .type = QEMU_OPT_STRING, \
818 .help = "identifier for monitor commands", \
821 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
826 /* magic number, but compiler will warn if too small */
827 #define NET_MAX_DESC 20
829 static const struct {
831 net_client_init_func init
;
832 QemuOptDesc desc
[NET_MAX_DESC
];
833 } net_client_types
[] = {
837 NET_COMMON_PARAMS_DESC
,
838 { /* end of list */ }
842 .init
= net_init_nic
,
844 NET_COMMON_PARAMS_DESC
,
847 .type
= QEMU_OPT_STRING
,
848 .help
= "id of -netdev to connect to",
852 .type
= QEMU_OPT_STRING
,
853 .help
= "MAC address",
856 .type
= QEMU_OPT_STRING
,
857 .help
= "device model (e1000, rtl8139, virtio etc.)",
860 .type
= QEMU_OPT_STRING
,
861 .help
= "PCI device address",
864 .type
= QEMU_OPT_NUMBER
,
865 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
867 { /* end of list */ }
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 */ }
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_SIZE
,
957 .help
= "send buffer limit"
960 .type
= QEMU_OPT_BOOL
,
961 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
964 .type
= QEMU_OPT_BOOL
,
965 .help
= "enable vhost-net network accelerator",
968 .type
= QEMU_OPT_STRING
,
969 .help
= "file descriptor of an already opened vhost net device",
971 .name
= "vhostforce",
972 .type
= QEMU_OPT_BOOL
,
973 .help
= "force vhost on for non-MSIX virtio guests",
976 { /* end of list */ }
980 .init
= net_init_socket
,
982 NET_COMMON_PARAMS_DESC
,
985 .type
= QEMU_OPT_STRING
,
986 .help
= "file descriptor of an already opened socket",
989 .type
= QEMU_OPT_STRING
,
990 .help
= "port number, and optional hostname, to listen on",
993 .type
= QEMU_OPT_STRING
,
994 .help
= "port number, and optional hostname, to connect to",
997 .type
= QEMU_OPT_STRING
,
998 .help
= "UDP multicast address and port number",
1000 .name
= "localaddr",
1001 .type
= QEMU_OPT_STRING
,
1002 .help
= "source address for multicast packets",
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");
1060 qerror_report(QERR_MISSING_PARAMETER
, "type");
1065 if (strcmp(type
, "tap") != 0 &&
1067 strcmp(type
, "user") != 0 &&
1070 strcmp(type
, "vde") != 0 &&
1072 strcmp(type
, "socket") != 0) {
1073 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1074 "a netdev backend type");
1078 if (qemu_opt_get(opts
, "vlan")) {
1079 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1082 if (qemu_opt_get(opts
, "name")) {
1083 qerror_report(QERR_INVALID_PARAMETER
, "name");
1086 if (!qemu_opts_id(opts
)) {
1087 qerror_report(QERR_MISSING_PARAMETER
, "id");
1092 name
= qemu_opts_id(opts
);
1094 name
= qemu_opt_get(opts
, "name");
1097 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1098 if (!strcmp(net_client_types
[i
].type
, type
)) {
1099 VLANState
*vlan
= NULL
;
1102 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1106 /* Do not add to a vlan if it's a -netdev or a nic with a
1107 * netdev= parameter. */
1109 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1110 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1114 if (net_client_types
[i
].init
) {
1115 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1117 /* TODO push error reporting into init() methods */
1118 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1126 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1127 "a network client type");
1131 static int net_host_check_device(const char *device
)
1134 const char *valid_param_list
[] = { "tap", "socket", "dump"
1142 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1143 if (!strncmp(valid_param_list
[i
], device
,
1144 strlen(valid_param_list
[i
])))
1151 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1153 const char *device
= qdict_get_str(qdict
, "device");
1154 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1157 if (!net_host_check_device(device
)) {
1158 monitor_printf(mon
, "invalid host network device %s\n", device
);
1162 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
1167 qemu_opt_set(opts
, "type", device
);
1169 if (net_client_init(mon
, opts
, 0) < 0) {
1170 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1174 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1176 VLANClientState
*vc
;
1177 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1178 const char *device
= qdict_get_str(qdict
, "device");
1180 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1184 if (!net_host_check_device(vc
->model
)) {
1185 monitor_printf(mon
, "invalid host network device %s\n", device
);
1188 qemu_del_vlan_client(vc
);
1191 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1196 opts
= qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict
);
1201 res
= net_client_init(mon
, opts
, 1);
1203 qemu_opts_del(opts
);
1209 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1211 const char *id
= qdict_get_str(qdict
, "id");
1212 VLANClientState
*vc
;
1214 vc
= qemu_find_netdev(id
);
1215 if (!vc
|| vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1216 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1219 qemu_del_vlan_client(vc
);
1220 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id
));
1224 void do_info_network(Monitor
*mon
)
1227 VLANClientState
*vc
;
1229 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1230 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1232 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1233 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1236 monitor_printf(mon
, "Devices not on any VLAN:\n");
1237 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1238 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1240 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1242 monitor_printf(mon
, "\n");
1246 int do_set_link(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1249 VLANClientState
*vc
= NULL
;
1250 const char *name
= qdict_get_str(qdict
, "name");
1251 int up
= qdict_get_bool(qdict
, "up");
1253 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1254 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1255 if (strcmp(vc
->name
, name
) == 0) {
1260 vc
= qemu_find_netdev(name
);
1264 qerror_report(QERR_DEVICE_NOT_FOUND
, name
);
1268 vc
->link_down
= !up
;
1270 if (vc
->info
->link_status_changed
) {
1271 vc
->info
->link_status_changed(vc
);
1274 /* Notify peer. Don't update peer link status: this makes it possible to
1275 * disconnect from host network without notifying the guest.
1276 * FIXME: is disconnected link status change operation useful?
1278 * Current behaviour is compatible with qemu vlans where there could be
1279 * multiple clients that can still communicate with each other in
1280 * disconnected mode. For now maintain this compatibility. */
1281 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1282 vc
->peer
->info
->link_status_changed(vc
->peer
);
1287 void net_cleanup(void)
1290 VLANClientState
*vc
, *next_vc
;
1292 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1293 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1294 qemu_del_vlan_client(vc
);
1298 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1299 qemu_del_vlan_client(vc
);
1303 void net_check_clients(void)
1306 VLANClientState
*vc
;
1309 /* Don't warn about the default network setup that you get if
1310 * no command line -net options are specified. There are two
1311 * cases that we would otherwise complain about:
1312 * (1) board doesn't support a NIC but the implicit "-net nic"
1313 * requested one; we'd otherwise complain about more NICs being
1314 * specified than we support, and also that the vlan set up by
1315 * the implicit "-net user" didn't have any NICs connected to it
1316 * (2) CONFIG_SLIRP not set: we'd otherwise complain about the
1317 * implicit "-net nic" setting up a nic that wasn't connected to
1324 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1325 int has_nic
= 0, has_host_dev
= 0;
1327 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1328 switch (vc
->info
->type
) {
1329 case NET_CLIENT_TYPE_NIC
:
1333 case NET_CLIENT_TYPE_SLIRP
:
1334 case NET_CLIENT_TYPE_TAP
:
1335 case NET_CLIENT_TYPE_SOCKET
:
1336 case NET_CLIENT_TYPE_VDE
:
1342 if (has_host_dev
&& !has_nic
)
1343 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1344 if (has_nic
&& !has_host_dev
)
1346 "Warning: vlan %d is not connected to host network\n",
1349 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1350 if (vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1354 fprintf(stderr
, "Warning: %s %s has no peer\n",
1355 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1359 if (seen_nics
!= nb_nics
) {
1360 /* Number of NICs requested by user on command line doesn't match
1361 * the number the model actually registered with us.
1362 * This will generally only happen for models of embedded boards
1363 * with no PCI bus or similar. PCI based machines can instantiate
1364 * all requested NICs as PCI devices but usually embedded boards
1365 * only have a single NIC.
1367 fprintf(stderr
, "Warning: more nics requested than this machine "
1368 "supports; some have been ignored\n");
1372 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1374 if (net_client_init(NULL
, opts
, 0) < 0)
1379 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1381 return net_client_init(NULL
, opts
, 1);
1384 int net_init_clients(void)
1386 QemuOptsList
*net
= qemu_find_opts("net");
1389 /* if no clients, we use a default config */
1390 qemu_opts_set(net
, NULL
, "type", "nic");
1392 qemu_opts_set(net
, NULL
, "type", "user");
1396 QTAILQ_INIT(&vlans
);
1397 QTAILQ_INIT(&non_vlan_clients
);
1399 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1402 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1409 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1411 #if defined(CONFIG_SLIRP)
1413 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1418 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {