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"
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_src_port(struct sockaddr_in
*haddr
,
97 struct sockaddr_in
*saddr
,
98 const char *input_str
)
100 char *str
= qemu_strdup(input_str
);
101 char *host_str
= str
;
103 const char *src_str2
;
107 * Chop off any extra arguments at the end of the string which
108 * would start with a comma, then fill in the src port information
109 * if it was provided else use the "any address" and "any port".
111 if ((ptr
= strchr(str
,',')))
114 if ((src_str
= strchr(input_str
,'@'))) {
119 if (parse_host_port(haddr
, host_str
) < 0)
123 if (!src_str
|| *src_str
== '\0')
126 if (parse_host_port(saddr
, src_str2
) < 0)
137 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
145 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
147 saddr
->sin_family
= AF_INET
;
148 if (buf
[0] == '\0') {
149 saddr
->sin_addr
.s_addr
= 0;
151 if (qemu_isdigit(buf
[0])) {
152 if (!inet_aton(buf
, &saddr
->sin_addr
))
155 if ((he
= gethostbyname(buf
)) == NULL
)
157 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
160 port
= strtol(p
, (char **)&r
, 0);
163 saddr
->sin_port
= htons(port
);
167 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
169 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
170 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
172 macaddr
[0], macaddr
[1], macaddr
[2],
173 macaddr
[3], macaddr
[4], macaddr
[5]);
176 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
178 static int index
= 0;
179 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
181 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
183 macaddr
->a
[0] = 0x52;
184 macaddr
->a
[1] = 0x54;
185 macaddr
->a
[2] = 0x00;
186 macaddr
->a
[3] = 0x12;
187 macaddr
->a
[4] = 0x34;
188 macaddr
->a
[5] = 0x56 + index
++;
191 static char *assign_name(VLANClientState
*vc1
, const char *model
)
197 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
200 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
201 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
207 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
209 return qemu_strdup(buf
);
212 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
217 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
219 const struct iovec
*iov
,
223 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
225 VLANClientState
*peer
,
231 assert(info
->size
>= sizeof(VLANClientState
));
233 vc
= qemu_mallocz(info
->size
);
236 vc
->model
= qemu_strdup(model
);
238 vc
->name
= qemu_strdup(name
);
240 vc
->name
= assign_name(vc
, model
);
246 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
253 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
255 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
256 qemu_deliver_packet_iov
,
263 NICState
*qemu_new_nic(NetClientInfo
*info
,
272 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
273 assert(info
->size
>= sizeof(NICState
));
275 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
277 nic
= DO_UPCAST(NICState
, nc
, nc
);
279 nic
->opaque
= opaque
;
284 static void qemu_cleanup_vlan_client(VLANClientState
*vc
)
287 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
289 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
292 if (vc
->info
->cleanup
) {
293 vc
->info
->cleanup(vc
);
297 static void qemu_free_vlan_client(VLANClientState
*vc
)
300 if (vc
->send_queue
) {
301 qemu_del_net_queue(vc
->send_queue
);
304 vc
->peer
->peer
= NULL
;
308 qemu_free(vc
->model
);
312 void qemu_del_vlan_client(VLANClientState
*vc
)
314 /* If there is a peer NIC, delete and cleanup client, but do not free. */
315 if (!vc
->vlan
&& vc
->peer
&& vc
->peer
->info
->type
== NET_CLIENT_TYPE_NIC
) {
316 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
->peer
);
317 if (nic
->peer_deleted
) {
320 nic
->peer_deleted
= true;
321 /* Let NIC know peer is gone. */
322 vc
->peer
->link_down
= true;
323 if (vc
->peer
->info
->link_status_changed
) {
324 vc
->peer
->info
->link_status_changed(vc
->peer
);
326 qemu_cleanup_vlan_client(vc
);
330 /* If this is a peer NIC and peer has already been deleted, free it now. */
331 if (!vc
->vlan
&& vc
->peer
&& vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
332 NICState
*nic
= DO_UPCAST(NICState
, nc
, vc
);
333 if (nic
->peer_deleted
) {
334 qemu_free_vlan_client(vc
->peer
);
338 qemu_cleanup_vlan_client(vc
);
339 qemu_free_vlan_client(vc
);
343 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
344 const char *client_str
)
349 vlan
= qemu_find_vlan(vlan_id
, 0);
351 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
355 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
356 if (!strcmp(vc
->name
, client_str
)) {
361 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
362 client_str
, vlan_id
);
368 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
)
373 QTAILQ_FOREACH(nc
, &non_vlan_clients
, next
) {
374 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
375 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
379 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
380 QTAILQ_FOREACH(nc
, &vlan
->clients
, next
) {
381 if (nc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
382 func(DO_UPCAST(NICState
, nc
, nc
), opaque
);
388 int qemu_can_send_packet(VLANClientState
*sender
)
390 VLANState
*vlan
= sender
->vlan
;
394 if (sender
->peer
->receive_disabled
) {
396 } else if (sender
->peer
->info
->can_receive
&&
397 !sender
->peer
->info
->can_receive(sender
->peer
)) {
408 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
413 /* no can_receive() handler, they can always receive */
414 if (!vc
->info
->can_receive
|| vc
->info
->can_receive(vc
)) {
421 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
427 VLANClientState
*vc
= opaque
;
434 if (vc
->receive_disabled
) {
438 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
439 ret
= vc
->info
->receive_raw(vc
, data
, size
);
441 ret
= vc
->info
->receive(vc
, data
, size
);
445 vc
->receive_disabled
= 1;
451 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
457 VLANState
*vlan
= opaque
;
461 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
473 if (vc
->receive_disabled
) {
478 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->info
->receive_raw
) {
479 len
= vc
->info
->receive_raw(vc
, buf
, size
);
481 len
= vc
->info
->receive(vc
, buf
, size
);
485 vc
->receive_disabled
= 1;
488 ret
= (ret
>= 0) ? ret
: len
;
495 void qemu_purge_queued_packets(VLANClientState
*vc
)
499 if (!vc
->peer
&& !vc
->vlan
) {
504 queue
= vc
->peer
->send_queue
;
506 queue
= vc
->vlan
->send_queue
;
509 qemu_net_queue_purge(queue
, vc
);
512 void qemu_flush_queued_packets(VLANClientState
*vc
)
516 vc
->receive_disabled
= 0;
519 queue
= vc
->vlan
->send_queue
;
521 queue
= vc
->send_queue
;
524 qemu_net_queue_flush(queue
);
527 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
529 const uint8_t *buf
, int size
,
530 NetPacketSent
*sent_cb
)
535 printf("qemu_send_packet_async:\n");
536 hex_dump(stdout
, buf
, size
);
539 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
544 queue
= sender
->peer
->send_queue
;
546 queue
= sender
->vlan
->send_queue
;
549 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
552 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
553 const uint8_t *buf
, int size
,
554 NetPacketSent
*sent_cb
)
556 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
560 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
562 qemu_send_packet_async(vc
, buf
, size
, NULL
);
565 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
567 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
571 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
574 uint8_t buffer
[4096];
578 for (i
= 0; i
< iovcnt
; i
++) {
581 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
582 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
586 return vc
->info
->receive(vc
, buffer
, offset
);
589 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
594 for (i
= 0; i
< iovcnt
; i
++)
595 offset
+= iov
[i
].iov_len
;
599 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
601 const struct iovec
*iov
,
605 VLANClientState
*vc
= opaque
;
608 return calc_iov_length(iov
, iovcnt
);
611 if (vc
->info
->receive_iov
) {
612 return vc
->info
->receive_iov(vc
, iov
, iovcnt
);
614 return vc_sendv_compat(vc
, iov
, iovcnt
);
618 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
620 const struct iovec
*iov
,
624 VLANState
*vlan
= opaque
;
628 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
636 ret
= calc_iov_length(iov
, iovcnt
);
640 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
642 if (vc
->info
->receive_iov
) {
643 len
= vc
->info
->receive_iov(vc
, iov
, iovcnt
);
645 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
648 ret
= (ret
>= 0) ? ret
: len
;
654 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
655 const struct iovec
*iov
, int iovcnt
,
656 NetPacketSent
*sent_cb
)
660 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
661 return calc_iov_length(iov
, iovcnt
);
665 queue
= sender
->peer
->send_queue
;
667 queue
= sender
->vlan
->send_queue
;
670 return qemu_net_queue_send_iov(queue
, sender
,
671 QEMU_NET_PACKET_FLAG_NONE
,
672 iov
, iovcnt
, sent_cb
);
676 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
678 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
681 /* find or alloc a new VLAN */
682 VLANState
*qemu_find_vlan(int id
, int allocate
)
686 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
687 if (vlan
->id
== id
) {
696 vlan
= qemu_mallocz(sizeof(VLANState
));
698 QTAILQ_INIT(&vlan
->clients
);
700 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
701 qemu_vlan_deliver_packet_iov
,
704 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
709 VLANClientState
*qemu_find_netdev(const char *id
)
713 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
714 if (!strcmp(vc
->name
, id
)) {
722 static int nic_get_free_idx(void)
726 for (index
= 0; index
< MAX_NICS
; index
++)
727 if (!nd_table
[index
].used
)
732 int qemu_show_nic_models(const char *arg
, const char *const *models
)
736 if (!arg
|| strcmp(arg
, "?"))
739 fprintf(stderr
, "qemu: Supported NIC models: ");
740 for (i
= 0 ; models
[i
]; i
++)
741 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
745 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
747 const char *models
[2];
752 if (qemu_show_nic_models(nd
->model
, models
))
754 if (qemu_find_nic_model(nd
, models
, model
) < 0)
758 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
759 const char *default_model
)
764 nd
->model
= qemu_strdup(default_model
);
766 for (i
= 0 ; models
[i
]; i
++) {
767 if (strcmp(nd
->model
, models
[i
]) == 0)
771 error_report("qemu: Unsupported NIC model: %s", nd
->model
);
775 int net_handle_fd_param(Monitor
*mon
, const char *param
)
777 if (!qemu_isdigit(param
[0])) {
780 fd
= monitor_get_fd(mon
, param
);
782 error_report("No file descriptor named %s found", param
);
788 return strtol(param
, NULL
, 0);
792 static int net_init_nic(QemuOpts
*opts
,
801 idx
= nic_get_free_idx();
802 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
803 error_report("Too Many NICs");
809 memset(nd
, 0, sizeof(*nd
));
811 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
812 nd
->netdev
= qemu_find_netdev(netdev
);
814 error_report("netdev '%s' not found", netdev
);
822 nd
->name
= qemu_strdup(name
);
824 if (qemu_opt_get(opts
, "model")) {
825 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
827 if (qemu_opt_get(opts
, "addr")) {
828 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
831 nd
->macaddr
[0] = 0x52;
832 nd
->macaddr
[1] = 0x54;
833 nd
->macaddr
[2] = 0x00;
834 nd
->macaddr
[3] = 0x12;
835 nd
->macaddr
[4] = 0x34;
836 nd
->macaddr
[5] = 0x56 + idx
;
838 if (qemu_opt_get(opts
, "macaddr") &&
839 net_parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
840 error_report("invalid syntax for ethernet address");
844 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors",
845 DEV_NVECTORS_UNSPECIFIED
);
846 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
847 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
848 error_report("invalid # of vectors: %d", nd
->nvectors
);
858 #define NET_COMMON_PARAMS_DESC \
861 .type = QEMU_OPT_STRING, \
862 .help = "net client type (nic, tap etc.)", \
865 .type = QEMU_OPT_NUMBER, \
866 .help = "vlan number", \
869 .type = QEMU_OPT_STRING, \
870 .help = "identifier for monitor commands", \
873 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
878 /* magic number, but compiler will warn if too small */
879 #define NET_MAX_DESC 20
881 static const struct {
883 net_client_init_func init
;
884 QemuOptDesc desc
[NET_MAX_DESC
];
885 } net_client_types
[] = {
889 NET_COMMON_PARAMS_DESC
,
890 { /* end of list */ }
894 .init
= net_init_nic
,
896 NET_COMMON_PARAMS_DESC
,
899 .type
= QEMU_OPT_STRING
,
900 .help
= "id of -netdev to connect to",
904 .type
= QEMU_OPT_STRING
,
905 .help
= "MAC address",
908 .type
= QEMU_OPT_STRING
,
909 .help
= "device model (e1000, rtl8139, virtio etc.)",
912 .type
= QEMU_OPT_STRING
,
913 .help
= "PCI device address",
916 .type
= QEMU_OPT_NUMBER
,
917 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
919 { /* end of list */ }
924 .init
= net_init_slirp
,
926 NET_COMMON_PARAMS_DESC
,
929 .type
= QEMU_OPT_STRING
,
930 .help
= "client hostname reported by the builtin DHCP server",
933 .type
= QEMU_OPT_STRING
,
934 .help
= "isolate the guest from the host (y|yes|n|no)",
937 .type
= QEMU_OPT_STRING
,
938 .help
= "legacy parameter, use net= instead",
941 .type
= QEMU_OPT_STRING
,
942 .help
= "IP address and optional netmask",
945 .type
= QEMU_OPT_STRING
,
946 .help
= "guest-visible address of the host",
949 .type
= QEMU_OPT_STRING
,
950 .help
= "root directory of the built-in TFTP server",
953 .type
= QEMU_OPT_STRING
,
954 .help
= "BOOTP filename, for use with tftp=",
957 .type
= QEMU_OPT_STRING
,
958 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
961 .type
= QEMU_OPT_STRING
,
962 .help
= "guest-visible address of the virtual nameserver",
965 .type
= QEMU_OPT_STRING
,
966 .help
= "root directory of the built-in SMB server",
969 .type
= QEMU_OPT_STRING
,
970 .help
= "IP address of the built-in SMB server",
973 .type
= QEMU_OPT_STRING
,
974 .help
= "guest port number to forward incoming TCP or UDP connections",
977 .type
= QEMU_OPT_STRING
,
978 .help
= "IP address and port to forward guest TCP connections",
980 { /* end of list */ }
985 .init
= net_init_tap
,
987 NET_COMMON_PARAMS_DESC
,
990 .type
= QEMU_OPT_STRING
,
991 .help
= "interface name",
996 .type
= QEMU_OPT_STRING
,
997 .help
= "file descriptor of an already opened tap",
1000 .type
= QEMU_OPT_STRING
,
1001 .help
= "script to initialize the interface",
1003 .name
= "downscript",
1004 .type
= QEMU_OPT_STRING
,
1005 .help
= "script to shut down the interface",
1008 .type
= QEMU_OPT_SIZE
,
1009 .help
= "send buffer limit"
1012 .type
= QEMU_OPT_BOOL
,
1013 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
1016 .type
= QEMU_OPT_BOOL
,
1017 .help
= "enable vhost-net network accelerator",
1020 .type
= QEMU_OPT_STRING
,
1021 .help
= "file descriptor of an already opened vhost net device",
1024 { /* end of list */ }
1028 .init
= net_init_socket
,
1030 NET_COMMON_PARAMS_DESC
,
1033 .type
= QEMU_OPT_STRING
,
1034 .help
= "file descriptor of an already opened socket",
1037 .type
= QEMU_OPT_STRING
,
1038 .help
= "port number, and optional hostname, to listen on",
1041 .type
= QEMU_OPT_STRING
,
1042 .help
= "port number, and optional hostname, to connect to",
1045 .type
= QEMU_OPT_STRING
,
1046 .help
= "UDP multicast address and port number",
1048 { /* end of list */ }
1053 .init
= net_init_vde
,
1055 NET_COMMON_PARAMS_DESC
,
1058 .type
= QEMU_OPT_STRING
,
1059 .help
= "socket path",
1062 .type
= QEMU_OPT_NUMBER
,
1063 .help
= "port number",
1066 .type
= QEMU_OPT_STRING
,
1067 .help
= "group owner of socket",
1070 .type
= QEMU_OPT_NUMBER
,
1071 .help
= "permissions for socket",
1073 { /* end of list */ }
1078 .init
= net_init_dump
,
1080 NET_COMMON_PARAMS_DESC
,
1083 .type
= QEMU_OPT_SIZE
,
1084 .help
= "per-packet size limit (64k default)",
1087 .type
= QEMU_OPT_STRING
,
1088 .help
= "dump file path (default is qemu-vlan0.pcap)",
1090 { /* end of list */ }
1093 { /* end of list */ }
1096 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1102 type
= qemu_opt_get(opts
, "type");
1104 qerror_report(QERR_MISSING_PARAMETER
, "type");
1109 if (strcmp(type
, "tap") != 0 &&
1111 strcmp(type
, "user") != 0 &&
1114 strcmp(type
, "vde") != 0 &&
1116 strcmp(type
, "socket") != 0) {
1117 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1118 "a netdev backend type");
1122 if (qemu_opt_get(opts
, "vlan")) {
1123 qerror_report(QERR_INVALID_PARAMETER
, "vlan");
1126 if (qemu_opt_get(opts
, "name")) {
1127 qerror_report(QERR_INVALID_PARAMETER
, "name");
1130 if (!qemu_opts_id(opts
)) {
1131 qerror_report(QERR_MISSING_PARAMETER
, "id");
1136 name
= qemu_opts_id(opts
);
1138 name
= qemu_opt_get(opts
, "name");
1141 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1142 if (!strcmp(net_client_types
[i
].type
, type
)) {
1143 VLANState
*vlan
= NULL
;
1146 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1150 /* Do not add to a vlan if it's a -netdev or a nic with a
1151 * netdev= parameter. */
1153 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1154 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1158 if (net_client_types
[i
].init
) {
1159 ret
= net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1161 /* TODO push error reporting into init() methods */
1162 qerror_report(QERR_DEVICE_INIT_FAILED
, type
);
1170 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "type",
1171 "a network client type");
1175 static int net_host_check_device(const char *device
)
1178 const char *valid_param_list
[] = { "tap", "socket", "dump"
1186 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1187 if (!strncmp(valid_param_list
[i
], device
,
1188 strlen(valid_param_list
[i
])))
1195 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1197 const char *device
= qdict_get_str(qdict
, "device");
1198 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1201 if (!net_host_check_device(device
)) {
1202 monitor_printf(mon
, "invalid host network device %s\n", device
);
1206 opts
= qemu_opts_parse(qemu_find_opts("net"), opts_str
? opts_str
: "", 0);
1211 qemu_opt_set(opts
, "type", device
);
1213 if (net_client_init(mon
, opts
, 0) < 0) {
1214 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1218 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1220 VLANClientState
*vc
;
1221 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1222 const char *device
= qdict_get_str(qdict
, "device");
1224 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1228 if (!net_host_check_device(vc
->model
)) {
1229 monitor_printf(mon
, "invalid host network device %s\n", device
);
1232 qemu_del_vlan_client(vc
);
1235 int do_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1240 opts
= qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict
);
1245 res
= net_client_init(mon
, opts
, 1);
1247 qemu_opts_del(opts
);
1253 int do_netdev_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1255 const char *id
= qdict_get_str(qdict
, "id");
1256 VLANClientState
*vc
;
1258 vc
= qemu_find_netdev(id
);
1259 if (!vc
|| vc
->info
->type
== NET_CLIENT_TYPE_NIC
) {
1260 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1263 qemu_del_vlan_client(vc
);
1264 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id
));
1268 void do_info_network(Monitor
*mon
)
1271 VLANClientState
*vc
;
1273 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1274 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1276 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1277 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1280 monitor_printf(mon
, "Devices not on any VLAN:\n");
1281 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1282 monitor_printf(mon
, " %s: %s", vc
->name
, vc
->info_str
);
1284 monitor_printf(mon
, " peer=%s", vc
->peer
->name
);
1286 monitor_printf(mon
, "\n");
1290 int do_set_link(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1293 VLANClientState
*vc
= NULL
;
1294 const char *name
= qdict_get_str(qdict
, "name");
1295 int up
= qdict_get_bool(qdict
, "up");
1297 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1298 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1299 if (strcmp(vc
->name
, name
) == 0) {
1304 vc
= qemu_find_netdev(name
);
1308 qerror_report(QERR_DEVICE_NOT_FOUND
, name
);
1312 vc
->link_down
= !up
;
1314 if (vc
->info
->link_status_changed
) {
1315 vc
->info
->link_status_changed(vc
);
1320 void net_cleanup(void)
1323 VLANClientState
*vc
, *next_vc
;
1325 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1326 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1327 qemu_del_vlan_client(vc
);
1331 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1332 qemu_del_vlan_client(vc
);
1336 void net_check_clients(void)
1339 VLANClientState
*vc
;
1340 int has_nic
= 0, has_host_dev
= 0;
1342 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1343 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1344 switch (vc
->info
->type
) {
1345 case NET_CLIENT_TYPE_NIC
:
1348 case NET_CLIENT_TYPE_SLIRP
:
1349 case NET_CLIENT_TYPE_TAP
:
1350 case NET_CLIENT_TYPE_SOCKET
:
1351 case NET_CLIENT_TYPE_VDE
:
1357 if (has_host_dev
&& !has_nic
)
1358 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1359 if (has_nic
&& !has_host_dev
)
1361 "Warning: vlan %d is not connected to host network\n",
1364 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1366 fprintf(stderr
, "Warning: %s %s has no peer\n",
1367 vc
->info
->type
== NET_CLIENT_TYPE_NIC
? "nic" : "netdev",
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)) {