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"
38 static QTAILQ_HEAD(, VLANState
) vlans
;
39 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
41 /***********************************************************/
42 /* network device redirectors */
44 #if defined(DEBUG_NET)
45 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
49 for(i
=0;i
<size
;i
+=16) {
53 fprintf(f
, "%08x ", i
);
56 fprintf(f
, " %02x", buf
[i
+j
]);
63 if (c
< ' ' || c
> '~')
72 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
79 offset
= strtol(p
, &last_char
, 0);
80 if (0 == errno
&& '\0' == *last_char
&&
81 offset
>= 0 && offset
<= 0xFFFFFF) {
82 macaddr
[3] = (offset
& 0xFF0000) >> 16;
83 macaddr
[4] = (offset
& 0xFF00) >> 8;
84 macaddr
[5] = offset
& 0xFF;
87 for(i
= 0; i
< 6; i
++) {
88 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
93 if (*p
!= ':' && *p
!= '-')
104 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
115 if (len
> buf_size
- 1)
124 int parse_host_src_port(struct sockaddr_in
*haddr
,
125 struct sockaddr_in
*saddr
,
126 const char *input_str
)
128 char *str
= strdup(input_str
);
129 char *host_str
= str
;
131 const char *src_str2
;
135 * Chop off any extra arguments at the end of the string which
136 * would start with a comma, then fill in the src port information
137 * if it was provided else use the "any address" and "any port".
139 if ((ptr
= strchr(str
,',')))
142 if ((src_str
= strchr(input_str
,'@'))) {
147 if (parse_host_port(haddr
, host_str
) < 0)
151 if (!src_str
|| *src_str
== '\0')
154 if (parse_host_port(saddr
, src_str2
) < 0)
165 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
173 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
175 saddr
->sin_family
= AF_INET
;
176 if (buf
[0] == '\0') {
177 saddr
->sin_addr
.s_addr
= 0;
179 if (qemu_isdigit(buf
[0])) {
180 if (!inet_aton(buf
, &saddr
->sin_addr
))
183 if ((he
= gethostbyname(buf
)) == NULL
)
185 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
188 port
= strtol(p
, (char **)&r
, 0);
191 saddr
->sin_port
= htons(port
);
195 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
197 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
198 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
200 macaddr
[0], macaddr
[1], macaddr
[2],
201 macaddr
[3], macaddr
[4], macaddr
[5]);
204 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
206 static int index
= 0;
207 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
209 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
211 macaddr
->a
[0] = 0x52;
212 macaddr
->a
[1] = 0x54;
213 macaddr
->a
[2] = 0x00;
214 macaddr
->a
[3] = 0x12;
215 macaddr
->a
[4] = 0x34;
216 macaddr
->a
[5] = 0x56 + index
++;
219 static char *assign_name(VLANClientState
*vc1
, const char *model
)
225 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
228 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
229 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
235 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
237 return qemu_strdup(buf
);
240 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
245 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
247 const struct iovec
*iov
,
251 VLANClientState
*qemu_new_net_client(NetClientInfo
*info
,
253 VLANClientState
*peer
,
259 assert(info
->size
>= sizeof(VLANClientState
));
261 vc
= qemu_mallocz(info
->size
);
263 vc
->type
= info
->type
;
264 vc
->model
= qemu_strdup(model
);
266 vc
->name
= qemu_strdup(name
);
268 vc
->name
= assign_name(vc
, model
);
270 vc
->can_receive
= info
->can_receive
;
271 vc
->receive
= info
->receive
;
272 vc
->receive_raw
= info
->receive_raw
;
273 vc
->receive_iov
= info
->receive_iov
;
274 vc
->cleanup
= info
->cleanup
;
275 vc
->link_status_changed
= info
->link_status_changed
;
280 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
286 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
288 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
289 qemu_deliver_packet_iov
,
296 NICState
*qemu_new_nic(NetClientInfo
*info
,
305 assert(info
->type
== NET_CLIENT_TYPE_NIC
);
306 assert(info
->size
>= sizeof(NICState
));
308 nc
= qemu_new_net_client(info
, conf
->vlan
, conf
->peer
, model
, name
);
310 nic
= DO_UPCAST(NICState
, nc
, nc
);
312 nic
->opaque
= opaque
;
317 VLANClientState
*qemu_new_vlan_client(net_client_type type
,
319 VLANClientState
*peer
,
322 NetCanReceive
*can_receive
,
324 NetReceive
*receive_raw
,
325 NetReceiveIOV
*receive_iov
,
329 VLANClientState
*ret
;
333 info
.size
= sizeof(VLANClientState
);
334 info
.can_receive
= can_receive
;
335 info
.receive
= receive
;
336 info
.receive_raw
= receive_raw
;
337 info
.receive_iov
= receive_iov
;
338 info
.cleanup
= cleanup
;
339 info
.link_status_changed
= NULL
;
341 ret
= qemu_new_net_client(&info
, vlan
, peer
, model
, name
);
343 ret
->opaque
= opaque
;
348 void qemu_del_vlan_client(VLANClientState
*vc
)
351 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
353 if (vc
->send_queue
) {
354 qemu_del_net_queue(vc
->send_queue
);
356 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
358 vc
->peer
->peer
= NULL
;
367 qemu_free(vc
->model
);
371 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
375 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
376 if (vc
->opaque
== opaque
) {
385 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
386 const char *client_str
)
391 vlan
= qemu_find_vlan(vlan_id
, 0);
393 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
397 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
398 if (!strcmp(vc
->name
, client_str
)) {
403 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
404 client_str
, vlan_id
);
410 int qemu_can_send_packet(VLANClientState
*sender
)
412 VLANState
*vlan
= sender
->vlan
;
416 if (sender
->peer
->receive_disabled
) {
418 } else if (sender
->peer
->can_receive
&&
419 !sender
->peer
->can_receive(sender
->peer
)) {
430 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
435 /* no can_receive() handler, they can always receive */
436 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
443 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
449 VLANClientState
*vc
= opaque
;
456 if (vc
->receive_disabled
) {
460 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
461 ret
= vc
->receive_raw(vc
, data
, size
);
463 ret
= vc
->receive(vc
, data
, size
);
467 vc
->receive_disabled
= 1;
473 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
479 VLANState
*vlan
= opaque
;
483 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
495 if (vc
->receive_disabled
) {
500 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
501 len
= vc
->receive_raw(vc
, buf
, size
);
503 len
= vc
->receive(vc
, buf
, size
);
507 vc
->receive_disabled
= 1;
510 ret
= (ret
>= 0) ? ret
: len
;
517 void qemu_purge_queued_packets(VLANClientState
*vc
)
521 if (!vc
->peer
&& !vc
->vlan
) {
526 queue
= vc
->peer
->send_queue
;
528 queue
= vc
->vlan
->send_queue
;
531 qemu_net_queue_purge(queue
, vc
);
534 void qemu_flush_queued_packets(VLANClientState
*vc
)
538 vc
->receive_disabled
= 0;
541 queue
= vc
->vlan
->send_queue
;
543 queue
= vc
->send_queue
;
546 qemu_net_queue_flush(queue
);
549 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
551 const uint8_t *buf
, int size
,
552 NetPacketSent
*sent_cb
)
557 printf("qemu_send_packet_async:\n");
558 hex_dump(stdout
, buf
, size
);
561 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
566 queue
= sender
->peer
->send_queue
;
568 queue
= sender
->vlan
->send_queue
;
571 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
574 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
575 const uint8_t *buf
, int size
,
576 NetPacketSent
*sent_cb
)
578 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
582 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
584 qemu_send_packet_async(vc
, buf
, size
, NULL
);
587 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
589 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
593 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
596 uint8_t buffer
[4096];
600 for (i
= 0; i
< iovcnt
; i
++) {
603 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
604 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
608 return vc
->receive(vc
, buffer
, offset
);
611 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
616 for (i
= 0; i
< iovcnt
; i
++)
617 offset
+= iov
[i
].iov_len
;
621 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
623 const struct iovec
*iov
,
627 VLANClientState
*vc
= opaque
;
630 return calc_iov_length(iov
, iovcnt
);
633 if (vc
->receive_iov
) {
634 return vc
->receive_iov(vc
, iov
, iovcnt
);
636 return vc_sendv_compat(vc
, iov
, iovcnt
);
640 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
642 const struct iovec
*iov
,
646 VLANState
*vlan
= opaque
;
650 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
658 ret
= calc_iov_length(iov
, iovcnt
);
662 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
664 if (vc
->receive_iov
) {
665 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
667 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
670 ret
= (ret
>= 0) ? ret
: len
;
676 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
677 const struct iovec
*iov
, int iovcnt
,
678 NetPacketSent
*sent_cb
)
682 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
683 return calc_iov_length(iov
, iovcnt
);
687 queue
= sender
->peer
->send_queue
;
689 queue
= sender
->vlan
->send_queue
;
692 return qemu_net_queue_send_iov(queue
, sender
,
693 QEMU_NET_PACKET_FLAG_NONE
,
694 iov
, iovcnt
, sent_cb
);
698 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
700 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
703 /* find or alloc a new VLAN */
704 VLANState
*qemu_find_vlan(int id
, int allocate
)
708 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
709 if (vlan
->id
== id
) {
718 vlan
= qemu_mallocz(sizeof(VLANState
));
720 QTAILQ_INIT(&vlan
->clients
);
722 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
723 qemu_vlan_deliver_packet_iov
,
726 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
731 VLANClientState
*qemu_find_netdev(const char *id
)
735 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
736 if (!strcmp(vc
->name
, id
)) {
744 static int nic_get_free_idx(void)
748 for (index
= 0; index
< MAX_NICS
; index
++)
749 if (!nd_table
[index
].used
)
754 int qemu_show_nic_models(const char *arg
, const char *const *models
)
758 if (!arg
|| strcmp(arg
, "?"))
761 fprintf(stderr
, "qemu: Supported NIC models: ");
762 for (i
= 0 ; models
[i
]; i
++)
763 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
767 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
769 const char *models
[2];
774 if (qemu_show_nic_models(nd
->model
, models
))
776 if (qemu_find_nic_model(nd
, models
, model
) < 0)
780 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
781 const char *default_model
)
786 nd
->model
= qemu_strdup(default_model
);
788 for (i
= 0 ; models
[i
]; i
++) {
789 if (strcmp(nd
->model
, models
[i
]) == 0)
793 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
797 int net_handle_fd_param(Monitor
*mon
, const char *param
)
799 if (!qemu_isdigit(param
[0])) {
802 fd
= monitor_get_fd(mon
, param
);
804 qemu_error("No file descriptor named %s found", param
);
810 return strtol(param
, NULL
, 0);
814 static int net_init_nic(QemuOpts
*opts
,
823 idx
= nic_get_free_idx();
824 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
825 qemu_error("Too Many NICs\n");
831 memset(nd
, 0, sizeof(*nd
));
833 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
834 nd
->netdev
= qemu_find_netdev(netdev
);
836 qemu_error("netdev '%s' not found\n", netdev
);
844 nd
->name
= qemu_strdup(name
);
846 if (qemu_opt_get(opts
, "model")) {
847 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
849 if (qemu_opt_get(opts
, "addr")) {
850 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
853 nd
->macaddr
[0] = 0x52;
854 nd
->macaddr
[1] = 0x54;
855 nd
->macaddr
[2] = 0x00;
856 nd
->macaddr
[3] = 0x12;
857 nd
->macaddr
[4] = 0x34;
858 nd
->macaddr
[5] = 0x56 + idx
;
860 if (qemu_opt_get(opts
, "macaddr") &&
861 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
862 qemu_error("invalid syntax for ethernet address\n");
866 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
867 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
868 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
869 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
875 nd
->vlan
->nb_guest_devs
++;
882 #define NET_COMMON_PARAMS_DESC \
885 .type = QEMU_OPT_STRING, \
886 .help = "net client type (nic, tap etc.)", \
889 .type = QEMU_OPT_NUMBER, \
890 .help = "vlan number", \
893 .type = QEMU_OPT_STRING, \
894 .help = "identifier for monitor commands", \
897 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
902 /* magic number, but compiler will warn if too small */
903 #define NET_MAX_DESC 20
907 net_client_init_func init
;
908 QemuOptDesc desc
[NET_MAX_DESC
];
909 } net_client_types
[] = {
913 NET_COMMON_PARAMS_DESC
,
914 { /* end of list */ }
918 .init
= net_init_nic
,
920 NET_COMMON_PARAMS_DESC
,
923 .type
= QEMU_OPT_STRING
,
924 .help
= "id of -netdev to connect to",
928 .type
= QEMU_OPT_STRING
,
929 .help
= "MAC address",
932 .type
= QEMU_OPT_STRING
,
933 .help
= "device model (e1000, rtl8139, virtio etc.)",
936 .type
= QEMU_OPT_STRING
,
937 .help
= "PCI device address",
940 .type
= QEMU_OPT_NUMBER
,
941 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
943 { /* end of list */ }
948 .init
= net_init_slirp
,
950 NET_COMMON_PARAMS_DESC
,
953 .type
= QEMU_OPT_STRING
,
954 .help
= "client hostname reported by the builtin DHCP server",
957 .type
= QEMU_OPT_STRING
,
958 .help
= "isolate the guest from the host (y|yes|n|no)",
961 .type
= QEMU_OPT_STRING
,
962 .help
= "legacy parameter, use net= instead",
965 .type
= QEMU_OPT_STRING
,
966 .help
= "IP address and optional netmask",
969 .type
= QEMU_OPT_STRING
,
970 .help
= "guest-visible address of the host",
973 .type
= QEMU_OPT_STRING
,
974 .help
= "root directory of the built-in TFTP server",
977 .type
= QEMU_OPT_STRING
,
978 .help
= "BOOTP filename, for use with tftp=",
981 .type
= QEMU_OPT_STRING
,
982 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
985 .type
= QEMU_OPT_STRING
,
986 .help
= "guest-visible address of the virtual nameserver",
989 .type
= QEMU_OPT_STRING
,
990 .help
= "root directory of the built-in SMB server",
993 .type
= QEMU_OPT_STRING
,
994 .help
= "IP address of the built-in SMB server",
997 .type
= QEMU_OPT_STRING
,
998 .help
= "guest port number to forward incoming TCP or UDP connections",
1001 .type
= QEMU_OPT_STRING
,
1002 .help
= "IP address and port to forward guest TCP connections",
1004 { /* end of list */ }
1009 .init
= net_init_tap
,
1011 NET_COMMON_PARAMS_DESC
,
1014 .type
= QEMU_OPT_STRING
,
1015 .help
= "interface name",
1020 .type
= QEMU_OPT_STRING
,
1021 .help
= "file descriptor of an already opened tap",
1024 .type
= QEMU_OPT_STRING
,
1025 .help
= "script to initialize the interface",
1027 .name
= "downscript",
1028 .type
= QEMU_OPT_STRING
,
1029 .help
= "script to shut down the interface",
1032 .type
= QEMU_OPT_SIZE
,
1033 .help
= "send buffer limit"
1036 .type
= QEMU_OPT_BOOL
,
1037 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
1040 { /* end of list */ }
1044 .init
= net_init_socket
,
1046 NET_COMMON_PARAMS_DESC
,
1049 .type
= QEMU_OPT_STRING
,
1050 .help
= "file descriptor of an already opened socket",
1053 .type
= QEMU_OPT_STRING
,
1054 .help
= "port number, and optional hostname, to listen on",
1057 .type
= QEMU_OPT_STRING
,
1058 .help
= "port number, and optional hostname, to connect to",
1061 .type
= QEMU_OPT_STRING
,
1062 .help
= "UDP multicast address and port number",
1064 { /* end of list */ }
1069 .init
= net_init_vde
,
1071 NET_COMMON_PARAMS_DESC
,
1074 .type
= QEMU_OPT_STRING
,
1075 .help
= "socket path",
1078 .type
= QEMU_OPT_NUMBER
,
1079 .help
= "port number",
1082 .type
= QEMU_OPT_STRING
,
1083 .help
= "group owner of socket",
1086 .type
= QEMU_OPT_NUMBER
,
1087 .help
= "permissions for socket",
1089 { /* end of list */ }
1094 .init
= net_init_dump
,
1096 NET_COMMON_PARAMS_DESC
,
1099 .type
= QEMU_OPT_SIZE
,
1100 .help
= "per-packet size limit (64k default)",
1103 .type
= QEMU_OPT_STRING
,
1104 .help
= "dump file path (default is qemu-vlan0.pcap)",
1106 { /* end of list */ }
1109 { /* end of list */ }
1112 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
1118 type
= qemu_opt_get(opts
, "type");
1120 qemu_error("No type specified for -net\n");
1125 if (strcmp(type
, "tap") != 0 &&
1127 strcmp(type
, "user") != 0 &&
1130 strcmp(type
, "vde") != 0 &&
1132 strcmp(type
, "socket") != 0) {
1133 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1138 if (qemu_opt_get(opts
, "vlan")) {
1139 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1142 if (qemu_opt_get(opts
, "name")) {
1143 qemu_error("The 'name' parameter is not valid with -netdev\n");
1146 if (!qemu_opts_id(opts
)) {
1147 qemu_error("The id= parameter is required with -netdev\n");
1152 name
= qemu_opts_id(opts
);
1154 name
= qemu_opt_get(opts
, "name");
1157 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
1158 if (!strcmp(net_client_types
[i
].type
, type
)) {
1159 VLANState
*vlan
= NULL
;
1161 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
1165 /* Do not add to a vlan if it's a -netdev or a nic with a
1166 * netdev= parameter. */
1168 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
1169 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
1172 if (net_client_types
[i
].init
) {
1173 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
1180 qemu_error("Invalid -net type '%s'\n", type
);
1184 void net_client_uninit(NICInfo
*nd
)
1187 nd
->vlan
->nb_guest_devs
--;
1191 qemu_free(nd
->model
);
1192 qemu_free(nd
->name
);
1193 qemu_free(nd
->devaddr
);
1198 static int net_host_check_device(const char *device
)
1201 const char *valid_param_list
[] = { "tap", "socket", "dump"
1209 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
1210 if (!strncmp(valid_param_list
[i
], device
,
1211 strlen(valid_param_list
[i
])))
1218 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
1220 const char *device
= qdict_get_str(qdict
, "device");
1221 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
1224 if (!net_host_check_device(device
)) {
1225 monitor_printf(mon
, "invalid host network device %s\n", device
);
1229 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
1231 monitor_printf(mon
, "parsing network options '%s' failed\n",
1232 opts_str
? opts_str
: "");
1236 qemu_opt_set(opts
, "type", device
);
1238 if (net_client_init(mon
, opts
, 0) < 0) {
1239 monitor_printf(mon
, "adding host network device %s failed\n", device
);
1243 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
1245 VLANClientState
*vc
;
1246 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
1247 const char *device
= qdict_get_str(qdict
, "device");
1249 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
1253 if (!net_host_check_device(vc
->model
)) {
1254 monitor_printf(mon
, "invalid host network device %s\n", device
);
1257 qemu_del_vlan_client(vc
);
1260 void net_set_boot_mask(int net_boot_mask
)
1264 /* Only the first four NICs may be bootable */
1265 net_boot_mask
= net_boot_mask
& 0xF;
1267 for (i
= 0; i
< nb_nics
; i
++) {
1268 if (net_boot_mask
& (1 << i
)) {
1269 nd_table
[i
].bootable
= 1;
1270 net_boot_mask
&= ~(1 << i
);
1274 if (net_boot_mask
) {
1275 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
1280 void do_info_network(Monitor
*mon
)
1284 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1285 VLANClientState
*vc
;
1287 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
1289 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1290 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
1295 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
1298 VLANClientState
*vc
= NULL
;
1299 const char *name
= qdict_get_str(qdict
, "name");
1300 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
1302 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1303 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
1304 if (strcmp(vc
->name
, name
) == 0) {
1312 monitor_printf(mon
, "could not find network device '%s'\n", name
);
1316 if (strcmp(up_or_down
, "up") == 0)
1318 else if (strcmp(up_or_down
, "down") == 0)
1321 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
1322 "valid\n", up_or_down
);
1324 if (vc
->link_status_changed
)
1325 vc
->link_status_changed(vc
);
1328 void net_cleanup(void)
1331 VLANClientState
*vc
, *next_vc
;
1333 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1334 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
1335 qemu_del_vlan_client(vc
);
1339 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
1340 qemu_del_vlan_client(vc
);
1344 static void net_check_clients(void)
1348 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1349 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1351 if (vlan
->nb_guest_devs
== 0)
1352 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1353 if (vlan
->nb_host_devs
== 0)
1355 "Warning: vlan %d is not connected to host network\n",
1360 static int net_init_client(QemuOpts
*opts
, void *dummy
)
1362 if (net_client_init(NULL
, opts
, 0) < 0)
1367 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
1369 return net_client_init(NULL
, opts
, 1);
1372 int net_init_clients(void)
1374 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
1375 /* if no clients, we use a default config */
1376 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
1378 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
1382 QTAILQ_INIT(&vlans
);
1383 QTAILQ_INIT(&non_vlan_clients
);
1385 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
1388 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
1392 net_check_clients();
1397 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
1399 #if defined(CONFIG_SLIRP)
1401 if (net_slirp_parse_legacy(opts_list
, optarg
, &ret
)) {
1406 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {