4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "config-host.h"
29 #include "net/socket.h"
31 #include "net/slirp.h"
35 #include "qemu-common.h"
36 #include "qemu_socket.h"
40 static QTAILQ_HEAD(, VLANState) vlans;
41 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
45 /***********************************************************/
46 /* network device redirectors */
48 #if defined(DEBUG_NET)
49 static void hex_dump(FILE *f, const uint8_t *buf, int size)
53 for(i=0;i<size;i+=16) {
57 fprintf(f, "%08x ", i);
60 fprintf(f, " %02x", buf[i+j]);
67 if (c < ' ' || c > '~')
76 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
87 if (len > buf_size - 1)
96 int parse_host_port(struct sockaddr_in *saddr, const char *str)
104 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
106 saddr->sin_family = AF_INET;
107 if (buf[0] == '\0') {
108 saddr->sin_addr.s_addr = 0;
110 if (qemu_isdigit(buf[0])) {
111 if (!inet_aton(buf, &saddr->sin_addr))
114 if ((he = gethostbyname(buf)) == NULL)
116 saddr->sin_addr = *(struct in_addr *)he->h_addr;
119 port = strtol(p, (char **)&r, 0);
122 saddr->sin_port = htons(port);
126 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
128 snprintf(vc->info_str, sizeof(vc->info_str),
129 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
131 macaddr[0], macaddr[1], macaddr[2],
132 macaddr[3], macaddr[4], macaddr[5]);
135 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
137 static int index = 0;
138 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
140 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
142 macaddr->a[0] = 0x52;
143 macaddr->a[1] = 0x54;
144 macaddr->a[2] = 0x00;
145 macaddr->a[3] = 0x12;
146 macaddr->a[4] = 0x34;
147 macaddr->a[5] = 0x56 + index++;
150 static char *assign_name(VLANClientState *vc1, const char *model)
157 QTAILQ_FOREACH(vlan, &vlans, next) {
158 QTAILQ_FOREACH(vc, &vlan->clients, next) {
159 if (vc != vc1 && strcmp(vc->model, model) == 0) {
165 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
166 if (vc != vc1 && strcmp(vc->model, model) == 0) {
171 snprintf(buf, sizeof(buf), "%s.%d", model, id);
173 return g_strdup(buf);
176 static ssize_t qemu_deliver_packet(VLANClientState *sender,
181 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
183 const struct iovec *iov,
187 VLANClientState *qemu_new_net_client(NetClientInfo *info,
189 VLANClientState *peer,
195 assert(info->size >= sizeof(VLANClientState));
197 vc = g_malloc0(info->size);
200 vc->model = g_strdup(model);
202 vc->name = g_strdup(name);
204 vc->name = assign_name(vc, model);
210 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
217 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
219 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
220 qemu_deliver_packet_iov,
227 NICState *qemu_new_nic(NetClientInfo *info,
236 assert(info->type == NET_CLIENT_TYPE_NIC);
237 assert(info->size >= sizeof(NICState));
239 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
241 nic = DO_UPCAST(NICState, nc, nc);
243 nic->opaque = opaque;
248 static void qemu_cleanup_vlan_client(VLANClientState *vc)
251 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
253 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
256 if (vc->info->cleanup) {
257 vc->info->cleanup(vc);
261 static void qemu_free_vlan_client(VLANClientState *vc)
264 if (vc->send_queue) {
265 qemu_del_net_queue(vc->send_queue);
268 vc->peer->peer = NULL;
276 void qemu_del_vlan_client(VLANClientState *vc)
278 /* If there is a peer NIC, delete and cleanup client, but do not free. */
279 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
280 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
281 if (nic->peer_deleted) {
284 nic->peer_deleted = true;
285 /* Let NIC know peer is gone. */
286 vc->peer->link_down = true;
287 if (vc->peer->info->link_status_changed) {
288 vc->peer->info->link_status_changed(vc->peer);
290 qemu_cleanup_vlan_client(vc);
294 /* If this is a peer NIC and peer has already been deleted, free it now. */
295 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
296 NICState *nic = DO_UPCAST(NICState, nc, vc);
297 if (nic->peer_deleted) {
298 qemu_free_vlan_client(vc->peer);
302 qemu_cleanup_vlan_client(vc);
303 qemu_free_vlan_client(vc);
307 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
308 const char *client_str)
313 vlan = qemu_find_vlan(vlan_id, 0);
315 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
319 QTAILQ_FOREACH(vc, &vlan->clients, next) {
320 if (!strcmp(vc->name, client_str)) {
325 monitor_printf(mon, "can't find device %s on VLAN %d\n",
326 client_str, vlan_id);
332 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
337 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
338 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
339 func(DO_UPCAST(NICState, nc, nc), opaque);
343 QTAILQ_FOREACH(vlan, &vlans, next) {
344 QTAILQ_FOREACH(nc, &vlan->clients, next) {
345 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
346 func(DO_UPCAST(NICState, nc, nc), opaque);
352 int qemu_can_send_packet(VLANClientState *sender)
354 VLANState *vlan = sender->vlan;
358 if (sender->peer->receive_disabled) {
360 } else if (sender->peer->info->can_receive &&
361 !sender->peer->info->can_receive(sender->peer)) {
372 QTAILQ_FOREACH(vc, &vlan->clients, next) {
377 /* no can_receive() handler, they can always receive */
378 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
385 static ssize_t qemu_deliver_packet(VLANClientState *sender,
391 VLANClientState *vc = opaque;
398 if (vc->receive_disabled) {
402 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
403 ret = vc->info->receive_raw(vc, data, size);
405 ret = vc->info->receive(vc, data, size);
409 vc->receive_disabled = 1;
415 static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
421 VLANState *vlan = opaque;
425 QTAILQ_FOREACH(vc, &vlan->clients, next) {
437 if (vc->receive_disabled) {
442 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
443 len = vc->info->receive_raw(vc, buf, size);
445 len = vc->info->receive(vc, buf, size);
449 vc->receive_disabled = 1;
452 ret = (ret >= 0) ? ret : len;
459 void qemu_purge_queued_packets(VLANClientState *vc)
463 if (!vc->peer && !vc->vlan) {
468 queue = vc->peer->send_queue;
470 queue = vc->vlan->send_queue;
473 qemu_net_queue_purge(queue, vc);
476 void qemu_flush_queued_packets(VLANClientState *vc)
480 vc->receive_disabled = 0;
483 queue = vc->vlan->send_queue;
485 queue = vc->send_queue;
488 qemu_net_queue_flush(queue);
491 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
493 const uint8_t *buf, int size,
494 NetPacketSent *sent_cb)
499 printf("qemu_send_packet_async:\n");
500 hex_dump(stdout, buf, size);
503 if (sender->link_down || (!sender->peer && !sender->vlan)) {
508 queue = sender->peer->send_queue;
510 queue = sender->vlan->send_queue;
513 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
516 ssize_t qemu_send_packet_async(VLANClientState *sender,
517 const uint8_t *buf, int size,
518 NetPacketSent *sent_cb)
520 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
524 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
526 qemu_send_packet_async(vc, buf, size, NULL);
529 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
531 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
535 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
538 uint8_t buffer[4096];
541 offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
543 return vc->info->receive(vc, buffer, offset);
546 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
548 const struct iovec *iov,
552 VLANClientState *vc = opaque;
555 return iov_size(iov, iovcnt);
558 if (vc->info->receive_iov) {
559 return vc->info->receive_iov(vc, iov, iovcnt);
561 return vc_sendv_compat(vc, iov, iovcnt);
565 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
567 const struct iovec *iov,
571 VLANState *vlan = opaque;
575 QTAILQ_FOREACH(vc, &vlan->clients, next) {
583 ret = iov_size(iov, iovcnt);
587 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
589 if (vc->info->receive_iov) {
590 len = vc->info->receive_iov(vc, iov, iovcnt);
592 len = vc_sendv_compat(vc, iov, iovcnt);
595 ret = (ret >= 0) ? ret : len;
601 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
602 const struct iovec *iov, int iovcnt,
603 NetPacketSent *sent_cb)
607 if (sender->link_down || (!sender->peer && !sender->vlan)) {
608 return iov_size(iov, iovcnt);
612 queue = sender->peer->send_queue;
614 queue = sender->vlan->send_queue;
617 return qemu_net_queue_send_iov(queue, sender,
618 QEMU_NET_PACKET_FLAG_NONE,
619 iov, iovcnt, sent_cb);
623 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
625 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
628 /* find or alloc a new VLAN */
629 VLANState *qemu_find_vlan(int id, int allocate)
633 QTAILQ_FOREACH(vlan, &vlans, next) {
634 if (vlan->id == id) {
643 vlan = g_malloc0(sizeof(VLANState));
645 QTAILQ_INIT(&vlan->clients);
647 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
648 qemu_vlan_deliver_packet_iov,
651 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
656 VLANClientState *qemu_find_netdev(const char *id)
660 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
661 if (vc->info->type == NET_CLIENT_TYPE_NIC)
663 if (!strcmp(vc->name, id)) {
671 static int nic_get_free_idx(void)
675 for (index = 0; index < MAX_NICS; index++)
676 if (!nd_table[index].used)
681 int qemu_show_nic_models(const char *arg, const char *const *models)
685 if (!arg || strcmp(arg, "?"))
688 fprintf(stderr, "qemu: Supported NIC models: ");
689 for (i = 0 ; models[i]; i++)
690 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
694 void qemu_check_nic_model(NICInfo *nd, const char *model)
696 const char *models[2];
701 if (qemu_show_nic_models(nd->model, models))
703 if (qemu_find_nic_model(nd, models, model) < 0)
707 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
708 const char *default_model)
713 nd->model = g_strdup(default_model);
715 for (i = 0 ; models[i]; i++) {
716 if (strcmp(nd->model, models[i]) == 0)
720 error_report("Unsupported NIC model: %s", nd->model);
724 int net_handle_fd_param(Monitor *mon, const char *param)
728 if (!qemu_isdigit(param[0]) && mon) {
730 fd = monitor_get_fd(mon, param);
732 error_report("No file descriptor named %s found", param);
738 fd = strtol(param, &endptr, 10);
739 if (*endptr || (fd == 0 && param == endptr)) {
747 static int net_init_nic(QemuOpts *opts,
756 idx = nic_get_free_idx();
757 if (idx == -1 || nb_nics >= MAX_NICS) {
758 error_report("Too Many NICs");
764 memset(nd, 0, sizeof(*nd));
766 if ((netdev = qemu_opt_get(opts, "netdev"))) {
767 nd->netdev = qemu_find_netdev(netdev);
769 error_report("netdev '%s' not found", netdev);
777 nd->name = g_strdup(name);
779 if (qemu_opt_get(opts, "model")) {
780 nd->model = g_strdup(qemu_opt_get(opts, "model"));
782 if (qemu_opt_get(opts, "addr")) {
783 nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
786 if (qemu_opt_get(opts, "macaddr") &&
787 net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
788 error_report("invalid syntax for ethernet address");
791 qemu_macaddr_default_if_unset(&nd->macaddr);
793 nd->nvectors = qemu_opt_get_number(opts, "vectors",
794 DEV_NVECTORS_UNSPECIFIED);
795 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
796 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
797 error_report("invalid # of vectors: %d", nd->nvectors);
807 #define NET_COMMON_PARAMS_DESC \
810 .type = QEMU_OPT_STRING, \
811 .help = "net client type (nic, tap etc.)", \
814 .type = QEMU_OPT_NUMBER, \
815 .help = "vlan number", \
818 .type = QEMU_OPT_STRING, \
819 .help = "identifier for monitor commands", \
822 typedef int (*net_client_init_func)(QemuOpts *opts,
827 /* magic number, but compiler will warn if too small */
828 #define NET_MAX_DESC 20
830 static const struct {
832 net_client_init_func init;
833 QemuOptDesc desc[NET_MAX_DESC];
834 } net_client_types[NET_CLIENT_TYPE_MAX] = {
835 [NET_CLIENT_TYPE_NONE] = {
838 NET_COMMON_PARAMS_DESC,
839 { /* end of list */ }
842 [NET_CLIENT_TYPE_NIC] = {
844 .init = net_init_nic,
846 NET_COMMON_PARAMS_DESC,
849 .type = QEMU_OPT_STRING,
850 .help = "id of -netdev to connect to",
854 .type = QEMU_OPT_STRING,
855 .help = "MAC address",
858 .type = QEMU_OPT_STRING,
859 .help = "device model (e1000, rtl8139, virtio etc.)",
862 .type = QEMU_OPT_STRING,
863 .help = "PCI device address",
866 .type = QEMU_OPT_NUMBER,
867 .help = "number of MSI-x vectors, 0 to disable MSI-X",
869 { /* end of list */ }
873 [NET_CLIENT_TYPE_USER] = {
875 .init = net_init_slirp,
877 NET_COMMON_PARAMS_DESC,
880 .type = QEMU_OPT_STRING,
881 .help = "client hostname reported by the builtin DHCP server",
884 .type = QEMU_OPT_STRING,
885 .help = "isolate the guest from the host (y|yes|n|no)",
888 .type = QEMU_OPT_STRING,
889 .help = "legacy parameter, use net= instead",
892 .type = QEMU_OPT_STRING,
893 .help = "IP address and optional netmask",
896 .type = QEMU_OPT_STRING,
897 .help = "guest-visible address of the host",
900 .type = QEMU_OPT_STRING,
901 .help = "root directory of the built-in TFTP server",
904 .type = QEMU_OPT_STRING,
905 .help = "BOOTP filename, for use with tftp=",
908 .type = QEMU_OPT_STRING,
909 .help = "the first of the 16 IPs the built-in DHCP server can assign",
912 .type = QEMU_OPT_STRING,
913 .help = "guest-visible address of the virtual nameserver",
916 .type = QEMU_OPT_STRING,
917 .help = "root directory of the built-in SMB server",
920 .type = QEMU_OPT_STRING,
921 .help = "IP address of the built-in SMB server",
924 .type = QEMU_OPT_STRING,
925 .help = "guest port number to forward incoming TCP or UDP connections",
928 .type = QEMU_OPT_STRING,
929 .help = "IP address and port to forward guest TCP connections",
931 { /* end of list */ }
935 [NET_CLIENT_TYPE_TAP] = {
937 .init = net_init_tap,
939 NET_COMMON_PARAMS_DESC,
942 .type = QEMU_OPT_STRING,
943 .help = "interface name",
948 .type = QEMU_OPT_STRING,
949 .help = "file descriptor of an already opened tap",
952 .type = QEMU_OPT_STRING,
953 .help = "script to initialize the interface",
955 .name = "downscript",
956 .type = QEMU_OPT_STRING,
957 .help = "script to shut down the interface",
960 .type = QEMU_OPT_SIZE,
961 .help = "send buffer limit"
964 .type = QEMU_OPT_BOOL,
965 .help = "enable the IFF_VNET_HDR flag on the tap interface"
968 .type = QEMU_OPT_BOOL,
969 .help = "enable vhost-net network accelerator",
972 .type = QEMU_OPT_STRING,
973 .help = "file descriptor of an already opened vhost net device",
975 .name = "vhostforce",
976 .type = QEMU_OPT_BOOL,
977 .help = "force vhost on for non-MSIX virtio guests",
980 { /* end of list */ }
983 [NET_CLIENT_TYPE_SOCKET] = {
985 .init = net_init_socket,
987 NET_COMMON_PARAMS_DESC,
990 .type = QEMU_OPT_STRING,
991 .help = "file descriptor of an already opened socket",
994 .type = QEMU_OPT_STRING,
995 .help = "port number, and optional hostname, to listen on",
998 .type = QEMU_OPT_STRING,
999 .help = "port number, and optional hostname, to connect to",
1002 .type = QEMU_OPT_STRING,
1003 .help = "UDP multicast address and port number",
1005 .name = "localaddr",
1006 .type = QEMU_OPT_STRING,
1007 .help = "source address for multicast packets",
1009 { /* end of list */ }
1013 [NET_CLIENT_TYPE_VDE] = {
1015 .init = net_init_vde,
1017 NET_COMMON_PARAMS_DESC,
1020 .type = QEMU_OPT_STRING,
1021 .help = "socket path",
1024 .type = QEMU_OPT_NUMBER,
1025 .help = "port number",
1028 .type = QEMU_OPT_STRING,
1029 .help = "group owner of socket",
1032 .type = QEMU_OPT_NUMBER,
1033 .help = "permissions for socket",
1035 { /* end of list */ }
1039 [NET_CLIENT_TYPE_DUMP] = {
1041 .init = net_init_dump,
1043 NET_COMMON_PARAMS_DESC,
1046 .type = QEMU_OPT_SIZE,
1047 .help = "per-packet size limit (64k default)",
1050 .type = QEMU_OPT_STRING,
1051 .help = "dump file path (default is qemu-vlan0.pcap)",
1053 { /* end of list */ }
1058 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1064 type = qemu_opt_get(opts, "type");
1066 qerror_report(QERR_MISSING_PARAMETER, "type");
1071 if (strcmp(type, "tap") != 0 &&
1073 strcmp(type, "user") != 0 &&
1076 strcmp(type, "vde") != 0 &&
1078 strcmp(type, "socket") != 0) {
1079 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1080 "a netdev backend type");
1084 if (qemu_opt_get(opts, "vlan")) {
1085 qerror_report(QERR_INVALID_PARAMETER, "vlan");
1088 if (qemu_opt_get(opts, "name")) {
1089 qerror_report(QERR_INVALID_PARAMETER, "name");
1092 if (!qemu_opts_id(opts)) {
1093 qerror_report(QERR_MISSING_PARAMETER, "id");
1098 name = qemu_opts_id(opts);
1100 name = qemu_opt_get(opts, "name");
1103 for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1104 if (net_client_types[i].type != NULL &&
1105 !strcmp(net_client_types[i].type, type)) {
1106 VLANState *vlan = NULL;
1109 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1113 /* Do not add to a vlan if it's a -netdev or a nic with a
1114 * netdev= parameter. */
1116 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1117 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1121 if (net_client_types[i].init) {
1122 ret = net_client_types[i].init(opts, mon, name, vlan);
1124 /* TODO push error reporting into init() methods */
1125 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1133 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1134 "a network client type");
1138 static int net_host_check_device(const char *device)
1141 const char *valid_param_list[] = { "tap", "socket", "dump"
1149 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1150 if (!strncmp(valid_param_list[i], device,
1151 strlen(valid_param_list[i])))
1158 void net_host_device_add(Monitor *mon, const QDict *qdict)
1160 const char *device = qdict_get_str(qdict, "device");
1161 const char *opts_str = qdict_get_try_str(qdict, "opts");
1164 if (!net_host_check_device(device)) {
1165 monitor_printf(mon, "invalid host network device %s\n", device);
1169 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1174 qemu_opt_set(opts, "type", device);
1176 if (net_client_init(mon, opts, 0) < 0) {
1177 monitor_printf(mon, "adding host network device %s failed\n", device);
1181 void net_host_device_remove(Monitor *mon, const QDict *qdict)
1183 VLANClientState *vc;
1184 int vlan_id = qdict_get_int(qdict, "vlan_id");
1185 const char *device = qdict_get_str(qdict, "device");
1187 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1191 if (!net_host_check_device(vc->model)) {
1192 monitor_printf(mon, "invalid host network device %s\n", device);
1195 qemu_del_vlan_client(vc);
1198 int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1203 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1208 res = net_client_init(mon, opts, 1);
1210 qemu_opts_del(opts);
1216 int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1218 const char *id = qdict_get_str(qdict, "id");
1219 VLANClientState *vc;
1221 vc = qemu_find_netdev(id);
1223 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1226 qemu_del_vlan_client(vc);
1227 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1231 static void print_net_client(Monitor *mon, VLANClientState *vc)
1233 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1234 net_client_types[vc->info->type].type, vc->info_str);
1237 void do_info_network(Monitor *mon)
1240 VLANClientState *vc, *peer;
1241 net_client_type type;
1243 QTAILQ_FOREACH(vlan, &vlans, next) {
1244 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1246 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1247 monitor_printf(mon, " ");
1248 print_net_client(mon, vc);
1251 monitor_printf(mon, "Devices not on any VLAN:\n");
1252 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1254 type = vc->info->type;
1255 if (!peer || type == NET_CLIENT_TYPE_NIC) {
1256 monitor_printf(mon, " ");
1257 print_net_client(mon, vc);
1258 } /* else it's a netdev connected to a NIC, printed with the NIC */
1259 if (peer && type == NET_CLIENT_TYPE_NIC) {
1260 monitor_printf(mon, " \\ ");
1261 print_net_client(mon, peer);
1266 int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
1269 VLANClientState *vc = NULL;
1270 const char *name = qdict_get_str(qdict, "name");
1271 int up = qdict_get_bool(qdict, "up");
1273 QTAILQ_FOREACH(vlan, &vlans, next) {
1274 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1275 if (strcmp(vc->name, name) == 0) {
1280 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1281 if (!strcmp(vc->name, name)) {
1288 qerror_report(QERR_DEVICE_NOT_FOUND, name);
1292 vc->link_down = !up;
1294 if (vc->info->link_status_changed) {
1295 vc->info->link_status_changed(vc);
1298 /* Notify peer. Don't update peer link status: this makes it possible to
1299 * disconnect from host network without notifying the guest.
1300 * FIXME: is disconnected link status change operation useful?
1302 * Current behaviour is compatible with qemu vlans where there could be
1303 * multiple clients that can still communicate with each other in
1304 * disconnected mode. For now maintain this compatibility. */
1305 if (vc->peer && vc->peer->info->link_status_changed) {
1306 vc->peer->info->link_status_changed(vc->peer);
1311 void net_cleanup(void)
1314 VLANClientState *vc, *next_vc;
1316 QTAILQ_FOREACH(vlan, &vlans, next) {
1317 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1318 qemu_del_vlan_client(vc);
1322 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1323 qemu_del_vlan_client(vc);
1327 void net_check_clients(void)
1330 VLANClientState *vc;
1333 /* Don't warn about the default network setup that you get if
1334 * no command line -net or -netdev options are specified. There
1335 * are two cases that we would otherwise complain about:
1336 * (1) board doesn't support a NIC but the implicit "-net nic"
1338 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1339 * sets up a nic that isn't connected to anything.
1345 QTAILQ_FOREACH(vlan, &vlans, next) {
1346 int has_nic = 0, has_host_dev = 0;
1348 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1349 switch (vc->info->type) {
1350 case NET_CLIENT_TYPE_NIC:
1353 case NET_CLIENT_TYPE_USER:
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",
1377 /* Check that all NICs requested via -net nic actually got created.
1378 * NICs created via -device don't need to be checked here because
1379 * they are always instantiated.
1381 for (i = 0; i < MAX_NICS; i++) {
1382 NICInfo *nd = &nd_table[i];
1383 if (nd->used && !nd->instantiated) {
1384 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1385 "was not created (not supported by this machine?)\n",
1386 nd->name ? nd->name : "anonymous",
1387 nd->model ? nd->model : "unspecified");
1392 static int net_init_client(QemuOpts *opts, void *dummy)
1394 if (net_client_init(NULL, opts, 0) < 0)
1399 static int net_init_netdev(QemuOpts *opts, void *dummy)
1401 return net_client_init(NULL, opts, 1);
1404 int net_init_clients(void)
1406 QemuOptsList *net = qemu_find_opts("net");
1409 /* if no clients, we use a default config */
1410 qemu_opts_set(net, NULL, "type", "nic");
1412 qemu_opts_set(net, NULL, "type", "user");
1416 QTAILQ_INIT(&vlans);
1417 QTAILQ_INIT(&non_vlan_clients);
1419 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1422 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1429 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1431 #if defined(CONFIG_SLIRP)
1433 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1438 if (!qemu_opts_parse(opts_list, optarg, 1)) {