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_src_port(struct sockaddr_in
*haddr
,
98 struct sockaddr_in
*saddr
,
99 const char *input_str
)
101 char *str
= qemu_strdup(input_str
);
102 char *host_str
= str
;
104 const char *src_str2
;
108 * Chop off any extra arguments at the end of the string which
109 * would start with a comma, then fill in the src port information
110 * if it was provided else use the "any address" and "any port".
112 if ((ptr
= strchr(str
,',')))
115 if ((src_str
= strchr(input_str
,'@'))) {
120 if (parse_host_port(haddr
, host_str
) < 0)
124 if (!src_str
|| *src_str
== '\0')
127 if (parse_host_port(saddr
, src_str2
) < 0)
138 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
146 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
148 saddr
->sin_family
= AF_INET
;
149 if (buf
[0] == '\0') {
150 saddr
->sin_addr
.s_addr
= 0;
152 if (qemu_isdigit(buf
[0])) {
153 if (!inet_aton(buf
, &saddr
->sin_addr
))
156 if ((he
= gethostbyname(buf
)) == NULL
)
158 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
161 port
= strtol(p
, (char **)&r
, 0);
164 saddr
->sin_port
= htons(port
);
168 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
170 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
171 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
173 macaddr
[0], macaddr
[1], macaddr
[2],
174 macaddr
[3], macaddr
[4], macaddr
[5]);
177 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
179 static int index
= 0;
180 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
182 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
184 macaddr
->a
[0] = 0x52;
185 macaddr
->a
[1] = 0x54;
186 macaddr
->a
[2] = 0x00;
187 macaddr
->a
[3] = 0x12;
188 macaddr
->a
[4] = 0x34;
189 macaddr
->a
[5] = 0x56 + index
++;
192 static char *assign_name(VLANClientState
*vc1
, const char *model
)
198 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
201 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
202 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
208 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
210 return qemu_strdup(buf
);
213 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
218 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
220 const struct iovec
*iov
,
224 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
226 VLANClientState
*peer
,
232 assert(info
->size
>= sizeof(VLANClientState
));
234 vc
= qemu_mallocz(info
->size
);
237 vc
->model
= qemu_strdup(model
);
239 vc
->name
= qemu_strdup(name
);
241 vc
->name
= assign_name(vc
, model
);
247 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
254 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
256 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
257 qemu_deliver_packet_iov
,
264 NICState
*qemu_new_nic(NetClientInfo
*info
,
273 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
274 assert(info
->size
>= sizeof(NICState
));
276 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
278 nic
= DO_UPCAST(NICState
, nc
, nc
);
280 nic
->opaque
= opaque
;
285 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
288 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
290 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
293 if (vc
->info
->cleanup
) {
294 vc
->info
->cleanup(vc
);
298 static void qemu_free_vlan_client(VLANClientState
*vc
)
301 if (vc
->send_queue
) {
302 qemu_del_net_queue(vc
->send_queue
);
305 vc
->peer
->peer
= NULL
;
309 qemu_free(vc
->model
);
313 void qemu_del_vlan_client(VLANClientState
*vc
)
315 /* If there is a peer NIC, delete and cleanup client, but do not free. */
316 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
317 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
318 if (nic
->peer_deleted
) {
321 nic
->peer_deleted
= true;
322 /* Let NIC know peer is gone. */
323 vc
->peer
->link_down
= true;
324 if (vc
->peer
->info
->link_status_changed
) {
325 vc
->peer
->info
->link_status_changed(vc
->peer
);
327 qemu_cleanup_vlan_client(vc
);
331 /* If this is a peer NIC and peer has already been deleted, free it now. */
332 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
333 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
334 if (nic
->peer_deleted
) {
335 qemu_free_vlan_client(vc
->peer
);
339 qemu_cleanup_vlan_client(vc
);
340 qemu_free_vlan_client(vc
);
344 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
345 const char *client_str
)
350 vlan
= qemu_find_vlan(vlan_id
, 0);
352 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
356 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
357 if (!strcmp(vc
->name
, client_str
)) {
362 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
363 client_str
, vlan_id
);
369 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
374 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
375 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
376 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
380 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
381 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
382 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
383 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
389 int qemu_can_send_packet(VLANClientState
*sender
)
391 VLANState
*vlan
= sender
->vlan
;
395 if (sender
->peer
->receive_disabled
) {
397 } else if (sender
->peer
->info
->can_receive
&&
398 !sender
->peer
->info
->can_receive(sender
->peer
)) {
409 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
414 /* no can_receive() handler, they can always receive */
415 if (vc
->info
->can_receive
&& !vc
->info
->can_receive(vc
)) {
422 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
428 VLANClientState
*vc
= opaque
;
435 if (vc
->receive_disabled
) {
439 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
440 ret
= vc
->info
->receive_raw(vc
, data
, size
);
442 ret
= vc
->info
->receive(vc
, data
, size
);
446 vc
->receive_disabled
= 1;
452 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
458 VLANState
*vlan
= opaque
;
462 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
474 if (vc
->receive_disabled
) {
479 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
480 len
= vc
->info
->receive_raw(vc
, buf
, size
);
482 len
= vc
->info
->receive(vc
, buf
, size
);
486 vc
->receive_disabled
= 1;
489 ret
= (ret
>= 0) ? ret
: len
;
496 void qemu_purge_queued_packets(VLANClientState
*vc
)
500 if (!vc
->peer
&& !vc
->vlan
) {
505 queue
= vc
->peer
->send_queue
;
507 queue
= vc
->vlan
->send_queue
;
510 qemu_net_queue_purge(queue
, vc
);
513 void qemu_flush_queued_packets(VLANClientState
*vc
)
517 vc
->receive_disabled
= 0;
520 queue
= vc
->vlan
->send_queue
;
522 queue
= vc
->send_queue
;
525 qemu_net_queue_flush(queue
);
528 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
530 const uint8_t *buf
, int size
,
531 NetPacketSent
*sent_cb
)
536 printf("qemu_send_packet_async:\n");
537 hex_dump(stdout
, buf
, size
);
540 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
545 queue
= sender
->peer
->send_queue
;
547 queue
= sender
->vlan
->send_queue
;
550 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
553 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
554 const uint8_t *buf
, int size
,
555 NetPacketSent
*sent_cb
)
557 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
561 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
563 qemu_send_packet_async(vc
, buf
, size
, NULL
);
566 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
568 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
572 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
575 uint8_t buffer
[4096];
578 offset
= iov_to_buf(iov
, iovcnt
, buffer
, 0, sizeof(buffer
));
580 return vc
->info
->receive(vc
, buffer
, offset
);
583 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
585 const struct iovec
*iov
,
589 VLANClientState
*vc
= opaque
;
592 return iov_size(iov
, iovcnt
);
595 if (vc
->info
->receive_iov
) {
596 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
598 return vc_sendv_compat(vc
, iov
, iovcnt
);
602 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
604 const struct iovec
*iov
,
608 VLANState
*vlan
= opaque
;
612 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
620 ret
= iov_size(iov
, iovcnt
);
624 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
626 if (vc
->info
->receive_iov
) {
627 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
629 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
632 ret
= (ret
>= 0) ? ret
: len
;
638 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
639 const struct iovec
*iov
, int iovcnt
,
640 NetPacketSent
*sent_cb
)
644 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
645 return iov_size(iov
, iovcnt
);
649 queue
= sender
->peer
->send_queue
;
651 queue
= sender
->vlan
->send_queue
;
654 return qemu_net_queue_send_iov(queue
, sender
,
655 QEMU_NET_PACKET_FLAG_NONE
,
656 iov
, iovcnt
, sent_cb
);
660 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
662 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
665 /* find or alloc a new VLAN */
666 VLANState
*qemu_find_vlan(int id
, int allocate
)
670 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
671 if (vlan
->id
== id
) {
680 vlan
= qemu_mallocz(sizeof(VLANState
));
682 QTAILQ_INIT(&vlan
->clients
);
684 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
685 qemu_vlan_deliver_packet_iov
,
688 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
693 VLANClientState
*qemu_find_netdev(const char *id
)
697 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
698 if (!strcmp(vc
->name
, id
)) {
706 static int nic_get_free_idx(void)
710 for (index
= 0; index
< MAX_NICS
; index
++)
711 if (!nd_table
[index
].used
)
716 int qemu_show_nic_models(const char *arg
, const char *const *models
)
720 if (!arg
|| strcmp(arg
, "?"))
723 fprintf(stderr
, "qemu: Supported NIC models: ");
724 for (i
= 0 ; models
[i
]; i
++)
725 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
729 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
731 const char *models
[2];
736 if (qemu_show_nic_models(nd
->model
, models
))
738 if (qemu_find_nic_model(nd
, models
, model
) < 0)
742 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
743 const char *default_model
)
748 nd
->model
= qemu_strdup(default_model
);
750 for (i
= 0 ; models
[i
]; i
++) {
751 if (strcmp(nd
->model
, models
[i
]) == 0)
755 error_report("qemu: Unsupported NIC model: %s", nd
->model
);
759 int net_handle_fd_param(Monitor
*mon
, const char *param
)
763 if (!qemu_isdigit(param
[0]) && mon
) {
765 fd
= monitor_get_fd(mon
, param
);
767 error_report("No file descriptor named %s found", param
);
773 fd
= strtol(param
, &endptr
, 10);
774 if (*endptr
|| (fd
== 0 && param
== endptr
)) {
782 static int net_init_nic(QemuOpts
*opts
,
791 idx
= nic_get_free_idx();
792 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
793 error_report("Too Many NICs");
799 memset(nd
, 0, sizeof(*nd
));
801 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
802 nd
->netdev
= qemu_find_netdev(netdev
);
804 error_report("netdev '%s' not found", netdev
);
812 nd
->name
= qemu_strdup(name
);
814 if (qemu_opt_get(opts
, "model")) {
815 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
817 if (qemu_opt_get(opts
, "addr")) {
818 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
821 nd
->macaddr
[0] = 0x52;
822 nd
->macaddr
[1] = 0x54;
823 nd
->macaddr
[2] = 0x00;
824 nd
->macaddr
[3] = 0x12;
825 nd
->macaddr
[4] = 0x34;
826 nd
->macaddr
[5] = 0x56 + idx
;
828 if (qemu_opt_get(opts
, "macaddr") &&
829 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
830 error_report("invalid syntax for ethernet address");
834 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
835 DEV_NVECTORS_UNSPECIFIED
);
836 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
837 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
838 error_report("invalid # of vectors: %d", nd
->nvectors
);
848 #define NET_COMMON_PARAMS_DESC \
851 .type = QEMU_OPT_STRING, \
852 .help = "net client type (nic, tap etc.)", \
855 .type = QEMU_OPT_NUMBER, \
856 .help = "vlan number", \
859 .type = QEMU_OPT_STRING, \
860 .help = "identifier for monitor commands", \
863 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
868 /* magic number, but compiler will warn if too small */
869 #define NET_MAX_DESC 20
871 static const struct {
873 net_client_init_func init
;
874 QemuOptDesc desc
[NET_MAX_DESC
];
875 } net_client_types
[] = {
879 NET_COMMON_PARAMS_DESC
,
880 { /* end of list */ }
884 .init
= net_init_nic
,
886 NET_COMMON_PARAMS_DESC
,
889 .type
= QEMU_OPT_STRING
,
890 .help
= "id of -netdev to connect to",
894 .type
= QEMU_OPT_STRING
,
895 .help
= "MAC address",
898 .type
= QEMU_OPT_STRING
,
899 .help
= "device model (e1000, rtl8139, virtio etc.)",
902 .type
= QEMU_OPT_STRING
,
903 .help
= "PCI device address",
906 .type
= QEMU_OPT_NUMBER
,
907 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
909 { /* end of list */ }
914 .init
= net_init_slirp
,
916 NET_COMMON_PARAMS_DESC
,
919 .type
= QEMU_OPT_STRING
,
920 .help
= "client hostname reported by the builtin DHCP server",
923 .type
= QEMU_OPT_STRING
,
924 .help
= "isolate the guest from the host (y|yes|n|no)",
927 .type
= QEMU_OPT_STRING
,
928 .help
= "legacy parameter, use net= instead",
931 .type
= QEMU_OPT_STRING
,
932 .help
= "IP address and optional netmask",
935 .type
= QEMU_OPT_STRING
,
936 .help
= "guest-visible address of the host",
939 .type
= QEMU_OPT_STRING
,
940 .help
= "root directory of the built-in TFTP server",
943 .type
= QEMU_OPT_STRING
,
944 .help
= "BOOTP filename, for use with tftp=",
947 .type
= QEMU_OPT_STRING
,
948 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
951 .type
= QEMU_OPT_STRING
,
952 .help
= "guest-visible address of the virtual nameserver",
955 .type
= QEMU_OPT_STRING
,
956 .help
= "root directory of the built-in SMB server",
959 .type
= QEMU_OPT_STRING
,
960 .help
= "IP address of the built-in SMB server",
963 .type
= QEMU_OPT_STRING
,
964 .help
= "guest port number to forward incoming TCP or UDP connections",
967 .type
= QEMU_OPT_STRING
,
968 .help
= "IP address and port to forward guest TCP connections",
970 { /* end of list */ }
975 .init
= net_init_tap
,
977 NET_COMMON_PARAMS_DESC
,
980 .type
= QEMU_OPT_STRING
,
981 .help
= "interface name",
986 .type
= QEMU_OPT_STRING
,
987 .help
= "file descriptor of an already opened tap",
990 .type
= QEMU_OPT_STRING
,
991 .help
= "script to initialize the interface",
993 .name
= "downscript",
994 .type
= QEMU_OPT_STRING
,
995 .help
= "script to shut down the interface",
998 .type
= QEMU_OPT_SIZE
,
999 .help
= "send buffer limit"
1002 .type
= QEMU_OPT_BOOL
,
1003 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
1006 .type
= QEMU_OPT_BOOL
,
1007 .help
= "enable vhost-net network accelerator",
1010 .type
= QEMU_OPT_STRING
,
1011 .help
= "file descriptor of an already opened vhost net device",
1014 { /* end of list */ }
1018 .init
= net_init_socket
,
1020 NET_COMMON_PARAMS_DESC
,
1023 .type
= QEMU_OPT_STRING
,
1024 .help
= "file descriptor of an already opened socket",
1027 .type
= QEMU_OPT_STRING
,
1028 .help
= "port number, and optional hostname, to listen on",
1031 .type
= QEMU_OPT_STRING
,
1032 .help
= "port number, and optional hostname, to connect to",
1035 .type
= QEMU_OPT_STRING
,
1036 .help
= "UDP multicast address and port number",
1038 .name
= "localaddr",
1039 .type
= QEMU_OPT_STRING
,
1040 .help
= "source address for multicast packets",
1042 { /* end of list */ }
1047 .init
= net_init_vde
,
1049 NET_COMMON_PARAMS_DESC
,
1052 .type
= QEMU_OPT_STRING
,
1053 .help
= "socket path",
1056 .type
= QEMU_OPT_NUMBER
,
1057 .help
= "port number",
1060 .type
= QEMU_OPT_STRING
,
1061 .help
= "group owner of socket",
1064 .type
= QEMU_OPT_NUMBER
,
1065 .help
= "permissions for socket",
1067 { /* end of list */ }
1072 .init
= net_init_dump
,
1074 NET_COMMON_PARAMS_DESC
,
1077 .type
= QEMU_OPT_SIZE
,
1078 .help
= "per-packet size limit (64k default)",
1081 .type
= QEMU_OPT_STRING
,
1082 .help
= "dump file path (default is qemu-vlan0.pcap)",
1084 { /* end of list */ }
1087 { /* end of list */ }
1090 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1096 type
= qemu_opt_get(opts
, "type");
1098 qerror_report(QERR_MISSING_PARAMETER
, "type");
1103 if (strcmp(type
, "tap") != 0 &&
1105 strcmp(type
, "user") != 0 &&
1108 strcmp(type
, "vde") != 0 &&
1110 strcmp(type
, "socket") != 0) {
1111 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1112 "a netdev backend type");
1116 if (qemu_opt_get(opts
, "vlan")) {
1117 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1120 if (qemu_opt_get(opts
, "name")) {
1121 qerror_report(QERR_INVALID_PARAMETER
, "name");
1124 if (!qemu_opts_id(opts
)) {
1125 qerror_report(QERR_MISSING_PARAMETER
, "id");
1130 name
= qemu_opts_id(opts
);
1132 name
= qemu_opt_get(opts
, "name");
1135 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1136 if (!strcmp(net_client_types
[i
].type
, type
)) {
1137 VLANState
*vlan
= NULL
;
1140 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1144 /* Do not add to a vlan if it's a -netdev or a nic with a
1145 * netdev= parameter. */
1147 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1148 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1152 if (net_client_types
[i
].init
) {
1153 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1155 /* TODO push error reporting into init() methods */
1156 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1164 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1165 "a network client type");
1169 static int net_host_check_device(const char *device
)
1172 const char *valid_param_list
[] = { "tap", "socket", "dump"
1180 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1181 if (!strncmp(valid_param_list
[i
], device
,
1182 strlen(valid_param_list
[i
])))
1189 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1191 const char *device
= qdict_get_str(qdict
, "device");
1192 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1195 if (!net_host_check_device(device
)) {
1196 monitor_printf(mon
, "invalid host network device %s\n", device
);
1200 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
1205 qemu_opt_set(opts
, "type", device
);
1207 if (net_client_init(mon
, opts
, 0) < 0) {
1208 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1212 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1214 VLANClientState
*vc
;
1215 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1216 const char *device
= qdict_get_str(qdict
, "device");
1218 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1222 if (!net_host_check_device(vc
->model
)) {
1223 monitor_printf(mon
, "invalid host network device %s\n", device
);
1226 qemu_del_vlan_client(vc
);
1229 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1234 opts
= qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict
);
1239 res
= net_client_init(mon
, opts
, 1);
1241 qemu_opts_del(opts
);
1247 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1249 const char *id
= qdict_get_str(qdict
, "id");
1250 VLANClientState
*vc
;
1252 vc
= qemu_find_netdev(id
);
1253 if (!vc
|| vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1254 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1257 qemu_del_vlan_client(vc
);
1258 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id
));
1262 void do_info_network(Monitor
*mon
)
1265 VLANClientState
*vc
;
1267 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1268 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1270 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1271 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1274 monitor_printf(mon
, "Devices not on any VLAN:\n");
1275 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1276 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1278 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1280 monitor_printf(mon
, "\n");
1284 int do_set_link(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1287 VLANClientState
*vc
= NULL
;
1288 const char *name
= qdict_get_str(qdict
, "name");
1289 int up
= qdict_get_bool(qdict
, "up");
1291 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1292 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1293 if (strcmp(vc
->name
, name
) == 0) {
1298 vc
= qemu_find_netdev(name
);
1302 qerror_report(QERR_DEVICE_NOT_FOUND
, name
);
1306 vc
->link_down
= !up
;
1308 if (vc
->info
->link_status_changed
) {
1309 vc
->info
->link_status_changed(vc
);
1312 /* Notify peer. Don't update peer link status: this makes it possible to
1313 * disconnect from host network without notifying the guest.
1314 * FIXME: is disconnected link status change operation useful?
1316 * Current behaviour is compatible with qemu vlans where there could be
1317 * multiple clients that can still communicate with each other in
1318 * disconnected mode. For now maintain this compatibility. */
1319 if (vc
->peer
&& vc
->peer
->info
->link_status_changed
) {
1320 vc
->peer
->info
->link_status_changed(vc
->peer
);
1325 void net_cleanup(void)
1328 VLANClientState
*vc
, *next_vc
;
1330 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1331 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1332 qemu_del_vlan_client(vc
);
1336 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1337 qemu_del_vlan_client(vc
);
1341 void net_check_clients(void)
1344 VLANClientState
*vc
;
1345 int has_nic
= 0, has_host_dev
= 0;
1347 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1348 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1349 switch (vc
->info
->type
) {
1350 case NET_CLIENT_TYPE_NIC
:
1353 case NET_CLIENT_TYPE_SLIRP
:
1354 case NET_CLIENT_TYPE_TAP
:
1355 case NET_CLIENT_TYPE_SOCKET
:
1356 case NET_CLIENT_TYPE_VDE
:
1362 if (has_host_dev
&& !has_nic
)
1363 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1364 if (has_nic
&& !has_host_dev
)
1366 "Warning: vlan %d is not connected to host network\n",
1369 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1371 fprintf(stderr
, "Warning: %s %s has no peer\n",
1372 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
1378 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1380 if (net_client_init(NULL
, opts
, 0) < 0)
1385 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1387 return net_client_init(NULL
, opts
, 1);
1390 int net_init_clients(void)
1392 QemuOptsList
*net
= qemu_find_opts("net");
1395 /* if no clients, we use a default config */
1396 qemu_opts_set(net
, NULL
, "type", "nic");
1398 qemu_opts_set(net
, NULL
, "type", "user");
1402 QTAILQ_INIT(&vlans
);
1403 QTAILQ_INIT(&non_vlan_clients
);
1405 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev
, NULL
, 1) == -1)
1408 if (qemu_opts_foreach(net
, net_init_client
, NULL
, 1) == -1) {
1415 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1417 #if defined(CONFIG_SLIRP)
1419 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1424 if (!qemu_opts_parse(opts_list
, optarg
, 1)) {