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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
46 #include <sys/select.h>
49 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <linux/rtc.h>
59 /* For the benefit of older linux systems which don't supply it,
60 we use a local copy of hpet.h. */
61 /* #include <linux/hpet.h> */
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
85 #if defined(__OpenBSD__)
89 #include "qemu-common.h"
92 #include "net/socket.h"
94 #include "net/slirp.h"
98 #include "qemu-timer.h"
99 #include "qemu-char.h"
100 #include "audio/audio.h"
101 #include "qemu_socket.h"
102 #include "qemu-log.h"
103 #include "qemu-config.h"
105 static QTAILQ_HEAD(, VLANState) vlans;
106 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
108 /***********************************************************/
109 /* network device redirectors */
111 #if defined(DEBUG_NET)
112 static void hex_dump(FILE *f, const uint8_t *buf, int size)
116 for(i=0;i<size;i+=16) {
120 fprintf(f, "%08x ", i);
123 fprintf(f, " %02x", buf[i+j]);
130 if (c < ' ' || c > '~')
139 static int parse_macaddr(uint8_t *macaddr, const char *p)
146 offset = strtol(p, &last_char, 0);
147 if (0 == errno && '\0' == *last_char &&
148 offset >= 0 && offset <= 0xFFFFFF) {
149 macaddr[3] = (offset & 0xFF0000) >> 16;
150 macaddr[4] = (offset & 0xFF00) >> 8;
151 macaddr[5] = offset & 0xFF;
154 for(i = 0; i < 6; i++) {
155 macaddr[i] = strtol(p, (char **)&p, 16);
160 if (*p != ':' && *p != '-')
171 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
182 if (len > buf_size - 1)
191 int parse_host_src_port(struct sockaddr_in *haddr,
192 struct sockaddr_in *saddr,
193 const char *input_str)
195 char *str = strdup(input_str);
196 char *host_str = str;
198 const char *src_str2;
202 * Chop off any extra arguments at the end of the string which
203 * would start with a comma, then fill in the src port information
204 * if it was provided else use the "any address" and "any port".
206 if ((ptr = strchr(str,',')))
209 if ((src_str = strchr(input_str,'@'))) {
214 if (parse_host_port(haddr, host_str) < 0)
218 if (!src_str || *src_str == '\0')
221 if (parse_host_port(saddr, src_str2) < 0)
232 int parse_host_port(struct sockaddr_in *saddr, const char *str)
240 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
242 saddr->sin_family = AF_INET;
243 if (buf[0] == '\0') {
244 saddr->sin_addr.s_addr = 0;
246 if (qemu_isdigit(buf[0])) {
247 if (!inet_aton(buf, &saddr->sin_addr))
250 if ((he = gethostbyname(buf)) == NULL)
252 saddr->sin_addr = *(struct in_addr *)he->h_addr;
255 port = strtol(p, (char **)&r, 0);
258 saddr->sin_port = htons(port);
262 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
264 snprintf(vc->info_str, sizeof(vc->info_str),
265 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
267 macaddr[0], macaddr[1], macaddr[2],
268 macaddr[3], macaddr[4], macaddr[5]);
271 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
273 static int index = 0;
274 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
276 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
278 macaddr->a[0] = 0x52;
279 macaddr->a[1] = 0x54;
280 macaddr->a[2] = 0x00;
281 macaddr->a[3] = 0x12;
282 macaddr->a[4] = 0x34;
283 macaddr->a[5] = 0x56 + index++;
286 static char *assign_name(VLANClientState *vc1, const char *model)
292 QTAILQ_FOREACH(vlan, &vlans, next) {
295 QTAILQ_FOREACH(vc, &vlan->clients, next) {
296 if (vc != vc1 && strcmp(vc->model, model) == 0) {
302 snprintf(buf, sizeof(buf), "%s.%d", model, id);
304 return qemu_strdup(buf);
307 static ssize_t qemu_deliver_packet(VLANClientState *sender,
312 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
314 const struct iovec *iov,
318 VLANClientState *qemu_new_vlan_client(net_client_type type,
320 VLANClientState *peer,
323 NetCanReceive *can_receive,
325 NetReceive *receive_raw,
326 NetReceiveIOV *receive_iov,
332 vc = qemu_mallocz(sizeof(VLANClientState));
335 vc->model = qemu_strdup(model);
337 vc->name = qemu_strdup(name);
339 vc->name = assign_name(vc, model);
340 vc->can_receive = can_receive;
341 vc->receive = receive;
342 vc->receive_raw = receive_raw;
343 vc->receive_iov = receive_iov;
344 vc->cleanup = cleanup;
350 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
356 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
358 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
359 qemu_deliver_packet_iov,
366 void qemu_del_vlan_client(VLANClientState *vc)
369 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
371 if (vc->send_queue) {
372 qemu_del_net_queue(vc->send_queue);
374 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
376 vc->peer->peer = NULL;
385 qemu_free(vc->model);
389 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
393 QTAILQ_FOREACH(vc, &vlan->clients, next) {
394 if (vc->opaque == opaque) {
403 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
404 const char *client_str)
409 vlan = qemu_find_vlan(vlan_id, 0);
411 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
415 QTAILQ_FOREACH(vc, &vlan->clients, next) {
416 if (!strcmp(vc->name, client_str)) {
421 monitor_printf(mon, "can't find device %s on VLAN %d\n",
422 client_str, vlan_id);
428 int qemu_can_send_packet(VLANClientState *sender)
430 VLANState *vlan = sender->vlan;
434 if (sender->peer->receive_disabled) {
436 } else if (sender->peer->can_receive &&
437 !sender->peer->can_receive(sender->peer)) {
448 QTAILQ_FOREACH(vc, &vlan->clients, next) {
453 /* no can_receive() handler, they can always receive */
454 if (!vc->can_receive || vc->can_receive(vc)) {
461 static ssize_t qemu_deliver_packet(VLANClientState *sender,
467 VLANClientState *vc = opaque;
474 if (vc->receive_disabled) {
478 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
479 ret = vc->receive_raw(vc, data, size);
481 ret = vc->receive(vc, data, size);
485 vc->receive_disabled = 1;
491 static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
497 VLANState *vlan = opaque;
501 QTAILQ_FOREACH(vc, &vlan->clients, next) {
513 if (vc->receive_disabled) {
518 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
519 len = vc->receive_raw(vc, buf, size);
521 len = vc->receive(vc, buf, size);
525 vc->receive_disabled = 1;
528 ret = (ret >= 0) ? ret : len;
535 void qemu_purge_queued_packets(VLANClientState *vc)
539 if (!vc->peer && !vc->vlan) {
544 queue = vc->peer->send_queue;
546 queue = vc->vlan->send_queue;
549 qemu_net_queue_purge(queue, vc);
552 void qemu_flush_queued_packets(VLANClientState *vc)
556 vc->receive_disabled = 0;
559 queue = vc->vlan->send_queue;
561 queue = vc->send_queue;
564 qemu_net_queue_flush(queue);
567 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
569 const uint8_t *buf, int size,
570 NetPacketSent *sent_cb)
575 printf("qemu_send_packet_async:\n");
576 hex_dump(stdout, buf, size);
579 if (sender->link_down || (!sender->peer && !sender->vlan)) {
584 queue = sender->peer->send_queue;
586 queue = sender->vlan->send_queue;
589 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
592 ssize_t qemu_send_packet_async(VLANClientState *sender,
593 const uint8_t *buf, int size,
594 NetPacketSent *sent_cb)
596 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
600 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
602 qemu_send_packet_async(vc, buf, size, NULL);
605 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
607 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
611 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
614 uint8_t buffer[4096];
618 for (i = 0; i < iovcnt; i++) {
621 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
622 memcpy(buffer + offset, iov[i].iov_base, len);
626 return vc->receive(vc, buffer, offset);
629 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
634 for (i = 0; i < iovcnt; i++)
635 offset += iov[i].iov_len;
639 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
641 const struct iovec *iov,
645 VLANClientState *vc = opaque;
648 return calc_iov_length(iov, iovcnt);
651 if (vc->receive_iov) {
652 return vc->receive_iov(vc, iov, iovcnt);
654 return vc_sendv_compat(vc, iov, iovcnt);
658 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
660 const struct iovec *iov,
664 VLANState *vlan = opaque;
668 QTAILQ_FOREACH(vc, &vlan->clients, next) {
676 ret = calc_iov_length(iov, iovcnt);
680 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
682 if (vc->receive_iov) {
683 len = vc->receive_iov(vc, iov, iovcnt);
685 len = vc_sendv_compat(vc, iov, iovcnt);
688 ret = (ret >= 0) ? ret : len;
694 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
695 const struct iovec *iov, int iovcnt,
696 NetPacketSent *sent_cb)
700 if (sender->link_down || (!sender->peer && !sender->vlan)) {
701 return calc_iov_length(iov, iovcnt);
705 queue = sender->peer->send_queue;
707 queue = sender->vlan->send_queue;
710 return qemu_net_queue_send_iov(queue, sender,
711 QEMU_NET_PACKET_FLAG_NONE,
712 iov, iovcnt, sent_cb);
716 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
718 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
721 /* find or alloc a new VLAN */
722 VLANState *qemu_find_vlan(int id, int allocate)
726 QTAILQ_FOREACH(vlan, &vlans, next) {
727 if (vlan->id == id) {
736 vlan = qemu_mallocz(sizeof(VLANState));
738 QTAILQ_INIT(&vlan->clients);
740 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
741 qemu_vlan_deliver_packet_iov,
744 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
749 VLANClientState *qemu_find_netdev(const char *id)
753 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
754 if (!strcmp(vc->name, id)) {
762 static int nic_get_free_idx(void)
766 for (index = 0; index < MAX_NICS; index++)
767 if (!nd_table[index].used)
772 int qemu_show_nic_models(const char *arg, const char *const *models)
776 if (!arg || strcmp(arg, "?"))
779 fprintf(stderr, "qemu: Supported NIC models: ");
780 for (i = 0 ; models[i]; i++)
781 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
785 void qemu_check_nic_model(NICInfo *nd, const char *model)
787 const char *models[2];
792 if (qemu_show_nic_models(nd->model, models))
794 if (qemu_find_nic_model(nd, models, model) < 0)
798 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
799 const char *default_model)
804 nd->model = qemu_strdup(default_model);
806 for (i = 0 ; models[i]; i++) {
807 if (strcmp(nd->model, models[i]) == 0)
811 qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
815 int net_handle_fd_param(Monitor *mon, const char *param)
817 if (!qemu_isdigit(param[0])) {
820 fd = monitor_get_fd(mon, param);
822 qemu_error("No file descriptor named %s found", param);
828 return strtol(param, NULL, 0);
832 static int net_init_nic(QemuOpts *opts,
841 idx = nic_get_free_idx();
842 if (idx == -1 || nb_nics >= MAX_NICS) {
843 qemu_error("Too Many NICs\n");
849 memset(nd, 0, sizeof(*nd));
851 if ((netdev = qemu_opt_get(opts, "netdev"))) {
852 nd->netdev = qemu_find_netdev(netdev);
854 qemu_error("netdev '%s' not found\n", netdev);
862 nd->name = qemu_strdup(name);
864 if (qemu_opt_get(opts, "model")) {
865 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
867 if (qemu_opt_get(opts, "addr")) {
868 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
871 nd->macaddr[0] = 0x52;
872 nd->macaddr[1] = 0x54;
873 nd->macaddr[2] = 0x00;
874 nd->macaddr[3] = 0x12;
875 nd->macaddr[4] = 0x34;
876 nd->macaddr[5] = 0x56 + idx;
878 if (qemu_opt_get(opts, "macaddr") &&
879 parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
880 qemu_error("invalid syntax for ethernet address\n");
884 nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
885 if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
886 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
887 qemu_error("invalid # of vectors: %d\n", nd->nvectors);
893 nd->vlan->nb_guest_devs++;
900 #define NET_COMMON_PARAMS_DESC \
903 .type = QEMU_OPT_STRING, \
904 .help = "net client type (nic, tap etc.)", \
907 .type = QEMU_OPT_NUMBER, \
908 .help = "vlan number", \
911 .type = QEMU_OPT_STRING, \
912 .help = "identifier for monitor commands", \
915 typedef int (*net_client_init_func)(QemuOpts *opts,
920 /* magic number, but compiler will warn if too small */
921 #define NET_MAX_DESC 20
925 net_client_init_func init;
926 QemuOptDesc desc[NET_MAX_DESC];
927 } net_client_types[] = {
931 NET_COMMON_PARAMS_DESC,
932 { /* end of list */ }
936 .init = net_init_nic,
938 NET_COMMON_PARAMS_DESC,
941 .type = QEMU_OPT_STRING,
942 .help = "id of -netdev to connect to",
946 .type = QEMU_OPT_STRING,
947 .help = "MAC address",
950 .type = QEMU_OPT_STRING,
951 .help = "device model (e1000, rtl8139, virtio etc.)",
954 .type = QEMU_OPT_STRING,
955 .help = "PCI device address",
958 .type = QEMU_OPT_NUMBER,
959 .help = "number of MSI-x vectors, 0 to disable MSI-X",
961 { /* end of list */ }
966 .init = net_init_slirp,
968 NET_COMMON_PARAMS_DESC,
971 .type = QEMU_OPT_STRING,
972 .help = "client hostname reported by the builtin DHCP server",
975 .type = QEMU_OPT_STRING,
976 .help = "isolate the guest from the host (y|yes|n|no)",
979 .type = QEMU_OPT_STRING,
980 .help = "legacy parameter, use net= instead",
983 .type = QEMU_OPT_STRING,
984 .help = "IP address and optional netmask",
987 .type = QEMU_OPT_STRING,
988 .help = "guest-visible address of the host",
991 .type = QEMU_OPT_STRING,
992 .help = "root directory of the built-in TFTP server",
995 .type = QEMU_OPT_STRING,
996 .help = "BOOTP filename, for use with tftp=",
999 .type = QEMU_OPT_STRING,
1000 .help = "the first of the 16 IPs the built-in DHCP server can assign",
1003 .type = QEMU_OPT_STRING,
1004 .help = "guest-visible address of the virtual nameserver",
1007 .type = QEMU_OPT_STRING,
1008 .help = "root directory of the built-in SMB server",
1010 .name = "smbserver",
1011 .type = QEMU_OPT_STRING,
1012 .help = "IP address of the built-in SMB server",
1015 .type = QEMU_OPT_STRING,
1016 .help = "guest port number to forward incoming TCP or UDP connections",
1019 .type = QEMU_OPT_STRING,
1020 .help = "IP address and port to forward guest TCP connections",
1022 { /* end of list */ }
1027 .init = net_init_tap,
1029 NET_COMMON_PARAMS_DESC,
1032 .type = QEMU_OPT_STRING,
1033 .help = "interface name",
1038 .type = QEMU_OPT_STRING,
1039 .help = "file descriptor of an already opened tap",
1042 .type = QEMU_OPT_STRING,
1043 .help = "script to initialize the interface",
1045 .name = "downscript",
1046 .type = QEMU_OPT_STRING,
1047 .help = "script to shut down the interface",
1050 .type = QEMU_OPT_SIZE,
1051 .help = "send buffer limit"
1054 .type = QEMU_OPT_BOOL,
1055 .help = "enable the IFF_VNET_HDR flag on the tap interface"
1058 { /* end of list */ }
1062 .init = net_init_socket,
1064 NET_COMMON_PARAMS_DESC,
1067 .type = QEMU_OPT_STRING,
1068 .help = "file descriptor of an already opened socket",
1071 .type = QEMU_OPT_STRING,
1072 .help = "port number, and optional hostname, to listen on",
1075 .type = QEMU_OPT_STRING,
1076 .help = "port number, and optional hostname, to connect to",
1079 .type = QEMU_OPT_STRING,
1080 .help = "UDP multicast address and port number",
1082 { /* end of list */ }
1087 .init = net_init_vde,
1089 NET_COMMON_PARAMS_DESC,
1092 .type = QEMU_OPT_STRING,
1093 .help = "socket path",
1096 .type = QEMU_OPT_NUMBER,
1097 .help = "port number",
1100 .type = QEMU_OPT_STRING,
1101 .help = "group owner of socket",
1104 .type = QEMU_OPT_NUMBER,
1105 .help = "permissions for socket",
1107 { /* end of list */ }
1112 .init = net_init_dump,
1114 NET_COMMON_PARAMS_DESC,
1117 .type = QEMU_OPT_SIZE,
1118 .help = "per-packet size limit (64k default)",
1121 .type = QEMU_OPT_STRING,
1122 .help = "dump file path (default is qemu-vlan0.pcap)",
1124 { /* end of list */ }
1127 { /* end of list */ }
1130 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1136 type = qemu_opt_get(opts, "type");
1138 qemu_error("No type specified for -net\n");
1143 if (strcmp(type, "tap") != 0 &&
1145 strcmp(type, "user") != 0 &&
1148 strcmp(type, "vde") != 0 &&
1150 strcmp(type, "socket") != 0) {
1151 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1156 if (qemu_opt_get(opts, "vlan")) {
1157 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1160 if (qemu_opt_get(opts, "name")) {
1161 qemu_error("The 'name' parameter is not valid with -netdev\n");
1164 if (!qemu_opts_id(opts)) {
1165 qemu_error("The id= parameter is required with -netdev\n");
1170 name = qemu_opts_id(opts);
1172 name = qemu_opt_get(opts, "name");
1175 for (i = 0; net_client_types[i].type != NULL; i++) {
1176 if (!strcmp(net_client_types[i].type, type)) {
1177 VLANState *vlan = NULL;
1179 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1183 /* Do not add to a vlan if it's a -netdev or a nic with a
1184 * netdev= parameter. */
1186 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1187 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1190 if (net_client_types[i].init) {
1191 return net_client_types[i].init(opts, mon, name, vlan);
1198 qemu_error("Invalid -net type '%s'\n", type);
1202 void net_client_uninit(NICInfo *nd)
1205 nd->vlan->nb_guest_devs--;
1209 qemu_free(nd->model);
1210 qemu_free(nd->name);
1211 qemu_free(nd->devaddr);
1216 static int net_host_check_device(const char *device)
1219 const char *valid_param_list[] = { "tap", "socket", "dump"
1227 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1228 if (!strncmp(valid_param_list[i], device,
1229 strlen(valid_param_list[i])))
1236 void net_host_device_add(Monitor *mon, const QDict *qdict)
1238 const char *device = qdict_get_str(qdict, "device");
1239 const char *opts_str = qdict_get_try_str(qdict, "opts");
1242 if (!net_host_check_device(device)) {
1243 monitor_printf(mon, "invalid host network device %s\n", device);
1247 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
1249 monitor_printf(mon, "parsing network options '%s' failed\n",
1250 opts_str ? opts_str : "");
1254 qemu_opt_set(opts, "type", device);
1256 if (net_client_init(mon, opts, 0) < 0) {
1257 monitor_printf(mon, "adding host network device %s failed\n", device);
1261 void net_host_device_remove(Monitor *mon, const QDict *qdict)
1263 VLANClientState *vc;
1264 int vlan_id = qdict_get_int(qdict, "vlan_id");
1265 const char *device = qdict_get_str(qdict, "device");
1267 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1271 if (!net_host_check_device(vc->model)) {
1272 monitor_printf(mon, "invalid host network device %s\n", device);
1275 qemu_del_vlan_client(vc);
1278 void net_set_boot_mask(int net_boot_mask)
1282 /* Only the first four NICs may be bootable */
1283 net_boot_mask = net_boot_mask & 0xF;
1285 for (i = 0; i < nb_nics; i++) {
1286 if (net_boot_mask & (1 << i)) {
1287 nd_table[i].bootable = 1;
1288 net_boot_mask &= ~(1 << i);
1292 if (net_boot_mask) {
1293 fprintf(stderr, "Cannot boot from non-existent NIC\n");
1298 void do_info_network(Monitor *mon)
1302 QTAILQ_FOREACH(vlan, &vlans, next) {
1303 VLANClientState *vc;
1305 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1307 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1308 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
1313 void do_set_link(Monitor *mon, const QDict *qdict)
1316 VLANClientState *vc = NULL;
1317 const char *name = qdict_get_str(qdict, "name");
1318 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
1320 QTAILQ_FOREACH(vlan, &vlans, next) {
1321 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1322 if (strcmp(vc->name, name) == 0) {
1330 monitor_printf(mon, "could not find network device '%s'\n", name);
1334 if (strcmp(up_or_down, "up") == 0)
1336 else if (strcmp(up_or_down, "down") == 0)
1339 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1340 "valid\n", up_or_down);
1342 if (vc->link_status_changed)
1343 vc->link_status_changed(vc);
1346 void net_cleanup(void)
1349 VLANClientState *vc, *next_vc;
1351 QTAILQ_FOREACH(vlan, &vlans, next) {
1352 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1353 qemu_del_vlan_client(vc);
1357 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1358 qemu_del_vlan_client(vc);
1362 static void net_check_clients(void)
1366 QTAILQ_FOREACH(vlan, &vlans, next) {
1367 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1369 if (vlan->nb_guest_devs == 0)
1370 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1371 if (vlan->nb_host_devs == 0)
1373 "Warning: vlan %d is not connected to host network\n",
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 if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
1393 /* if no clients, we use a default config */
1394 qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
1396 qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
1400 QTAILQ_INIT(&vlans);
1401 QTAILQ_INIT(&non_vlan_clients);
1403 if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
1406 if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
1410 net_check_clients();
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, "type")) {