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
24 #include "config-host.h"
29 #include "net/slirp.h"
33 #include "monitor/monitor.h"
34 #include "qemu-common.h"
35 #include "qemu/sockets.h"
36 #include "qemu/config-file.h"
37 #include "qmp-commands.h"
40 #include "qemu/main-loop.h"
41 #include "qapi-visit.h"
42 #include "qapi/opts-visitor.h"
43 #include "qapi/dealloc-visitor.h"
45 /* Net bridge is currently not supported for W32. */
47 # define CONFIG_NET_BRIDGE
50 static QTAILQ_HEAD(, NetClientState) net_clients;
52 const char *host_net_devices[] = {
56 #ifdef CONFIG_NET_BRIDGE
71 /***********************************************************/
72 /* network device redirectors */
74 #if defined(DEBUG_NET)
75 static void hex_dump(FILE *f, const uint8_t *buf, int size)
79 for(i=0;i<size;i+=16) {
83 fprintf(f, "%08x ", i);
86 fprintf(f, " %02x", buf[i+j]);
93 if (c < ' ' || c > '~')
102 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
113 if (len > buf_size - 1)
122 int parse_host_port(struct sockaddr_in *saddr, const char *str)
130 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
132 saddr->sin_family = AF_INET;
133 if (buf[0] == '\0') {
134 saddr->sin_addr.s_addr = 0;
136 if (qemu_isdigit(buf[0])) {
137 if (!inet_aton(buf, &saddr->sin_addr))
140 if ((he = gethostbyname(buf)) == NULL)
142 saddr->sin_addr = *(struct in_addr *)he->h_addr;
145 port = strtol(p, (char **)&r, 0);
148 saddr->sin_port = htons(port);
152 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
154 snprintf(nc->info_str, sizeof(nc->info_str),
155 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
157 macaddr[0], macaddr[1], macaddr[2],
158 macaddr[3], macaddr[4], macaddr[5]);
161 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
163 static int index = 0;
164 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
166 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
168 macaddr->a[0] = 0x52;
169 macaddr->a[1] = 0x54;
170 macaddr->a[2] = 0x00;
171 macaddr->a[3] = 0x12;
172 macaddr->a[4] = 0x34;
173 macaddr->a[5] = 0x56 + index++;
177 * Generate a name for net client
179 * Only net clients created with the legacy -net option and NICs need this.
181 static char *assign_name(NetClientState *nc1, const char *model)
186 QTAILQ_FOREACH(nc, &net_clients, next) {
190 if (strcmp(nc->model, model) == 0) {
195 return g_strdup_printf("%s.%d", model, id);
198 static void qemu_net_client_destructor(NetClientState *nc)
203 static void qemu_net_client_setup(NetClientState *nc,
205 NetClientState *peer,
208 NetClientDestructor *destructor)
211 nc->model = g_strdup(model);
213 nc->name = g_strdup(name);
215 nc->name = assign_name(nc, model);
223 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
225 nc->incoming_queue = qemu_new_net_queue(nc);
226 nc->destructor = destructor;
229 NetClientState *qemu_new_net_client(NetClientInfo *info,
230 NetClientState *peer,
236 assert(info->size >= sizeof(NetClientState));
238 nc = g_malloc0(info->size);
239 qemu_net_client_setup(nc, info, peer, model, name,
240 qemu_net_client_destructor);
245 NICState *qemu_new_nic(NetClientInfo *info,
251 NetClientState **peers = conf->peers.ncs;
253 int i, queues = MAX(1, conf->peers.queues);
255 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
256 assert(info->size >= sizeof(NICState));
258 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
259 nic->ncs = (void *)nic + info->size;
261 nic->opaque = opaque;
263 for (i = 0; i < queues; i++) {
264 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
266 nic->ncs[i].queue_index = i;
272 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
274 return nic->ncs + queue_index;
277 NetClientState *qemu_get_queue(NICState *nic)
279 return qemu_get_subqueue(nic, 0);
282 NICState *qemu_get_nic(NetClientState *nc)
284 NetClientState *nc0 = nc - nc->queue_index;
286 return (NICState *)((void *)nc0 - nc->info->size);
289 void *qemu_get_nic_opaque(NetClientState *nc)
291 NICState *nic = qemu_get_nic(nc);
296 static void qemu_cleanup_net_client(NetClientState *nc)
298 QTAILQ_REMOVE(&net_clients, nc, next);
300 if (nc->info->cleanup) {
301 nc->info->cleanup(nc);
305 static void qemu_free_net_client(NetClientState *nc)
307 if (nc->incoming_queue) {
308 qemu_del_net_queue(nc->incoming_queue);
311 nc->peer->peer = NULL;
315 if (nc->destructor) {
320 void qemu_del_net_client(NetClientState *nc)
322 NetClientState *ncs[MAX_QUEUE_NUM];
325 /* If the NetClientState belongs to a multiqueue backend, we will change all
326 * other NetClientStates also.
328 queues = qemu_find_net_clients_except(nc->name, ncs,
329 NET_CLIENT_OPTIONS_KIND_NIC,
333 /* If there is a peer NIC, delete and cleanup client, but do not free. */
334 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
335 NICState *nic = qemu_get_nic(nc->peer);
336 if (nic->peer_deleted) {
339 nic->peer_deleted = true;
341 for (i = 0; i < queues; i++) {
342 ncs[i]->peer->link_down = true;
345 if (nc->peer->info->link_status_changed) {
346 nc->peer->info->link_status_changed(nc->peer);
349 for (i = 0; i < queues; i++) {
350 qemu_cleanup_net_client(ncs[i]);
356 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
358 for (i = 0; i < queues; i++) {
359 qemu_cleanup_net_client(ncs[i]);
360 qemu_free_net_client(ncs[i]);
364 void qemu_del_nic(NICState *nic)
366 int i, queues = MAX(nic->conf->peers.queues, 1);
368 /* If this is a peer NIC and peer has already been deleted, free it now. */
369 if (nic->peer_deleted) {
370 for (i = 0; i < queues; i++) {
371 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
375 for (i = queues - 1; i >= 0; i--) {
376 NetClientState *nc = qemu_get_subqueue(nic, i);
378 qemu_cleanup_net_client(nc);
379 qemu_free_net_client(nc);
385 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
389 QTAILQ_FOREACH(nc, &net_clients, next) {
390 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
391 if (nc->queue_index == 0) {
392 func(qemu_get_nic(nc), opaque);
398 bool qemu_has_ufo(NetClientState *nc)
400 if (!nc || !nc->info->has_ufo) {
404 return nc->info->has_ufo(nc);
407 bool qemu_has_vnet_hdr(NetClientState *nc)
409 if (!nc || !nc->info->has_vnet_hdr) {
413 return nc->info->has_vnet_hdr(nc);
416 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
418 if (!nc || !nc->info->has_vnet_hdr_len) {
422 return nc->info->has_vnet_hdr_len(nc, len);
425 void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
427 if (!nc || !nc->info->using_vnet_hdr) {
431 nc->info->using_vnet_hdr(nc, enable);
434 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
437 if (!nc || !nc->info->set_offload) {
441 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
444 void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
446 if (!nc || !nc->info->set_vnet_hdr_len) {
450 nc->info->set_vnet_hdr_len(nc, len);
453 int qemu_can_send_packet(NetClientState *sender)
459 if (sender->peer->receive_disabled) {
461 } else if (sender->peer->info->can_receive &&
462 !sender->peer->info->can_receive(sender->peer)) {
468 ssize_t qemu_deliver_packet(NetClientState *sender,
474 NetClientState *nc = opaque;
481 if (nc->receive_disabled) {
485 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
486 ret = nc->info->receive_raw(nc, data, size);
488 ret = nc->info->receive(nc, data, size);
492 nc->receive_disabled = 1;
498 void qemu_purge_queued_packets(NetClientState *nc)
504 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
507 void qemu_flush_queued_packets(NetClientState *nc)
509 nc->receive_disabled = 0;
511 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
512 if (net_hub_flush(nc->peer)) {
516 if (qemu_net_queue_flush(nc->incoming_queue)) {
517 /* We emptied the queue successfully, signal to the IO thread to repoll
518 * the file descriptor (for tap, for example).
524 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
526 const uint8_t *buf, int size,
527 NetPacketSent *sent_cb)
532 printf("qemu_send_packet_async:\n");
533 hex_dump(stdout, buf, size);
536 if (sender->link_down || !sender->peer) {
540 queue = sender->peer->incoming_queue;
542 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
545 ssize_t qemu_send_packet_async(NetClientState *sender,
546 const uint8_t *buf, int size,
547 NetPacketSent *sent_cb)
549 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
553 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
555 qemu_send_packet_async(nc, buf, size, NULL);
558 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
560 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
564 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
567 uint8_t buffer[NET_BUFSIZE];
570 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
572 return nc->info->receive(nc, buffer, offset);
575 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
577 const struct iovec *iov,
581 NetClientState *nc = opaque;
585 return iov_size(iov, iovcnt);
588 if (nc->receive_disabled) {
592 if (nc->info->receive_iov) {
593 ret = nc->info->receive_iov(nc, iov, iovcnt);
595 ret = nc_sendv_compat(nc, iov, iovcnt);
599 nc->receive_disabled = 1;
605 ssize_t qemu_sendv_packet_async(NetClientState *sender,
606 const struct iovec *iov, int iovcnt,
607 NetPacketSent *sent_cb)
611 if (sender->link_down || !sender->peer) {
612 return iov_size(iov, iovcnt);
615 queue = sender->peer->incoming_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(NetClientState *nc, const struct iovec *iov, int iovcnt)
625 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
628 NetClientState *qemu_find_netdev(const char *id)
632 QTAILQ_FOREACH(nc, &net_clients, next) {
633 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
635 if (!strcmp(nc->name, id)) {
643 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
644 NetClientOptionsKind type, int max)
649 QTAILQ_FOREACH(nc, &net_clients, next) {
650 if (nc->info->type == type) {
653 if (!id || !strcmp(nc->name, id)) {
664 static int nic_get_free_idx(void)
668 for (index = 0; index < MAX_NICS; index++)
669 if (!nd_table[index].used)
674 int qemu_show_nic_models(const char *arg, const char *const *models)
678 if (!arg || !is_help_option(arg)) {
682 fprintf(stderr, "qemu: Supported NIC models: ");
683 for (i = 0 ; models[i]; i++)
684 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
688 void qemu_check_nic_model(NICInfo *nd, const char *model)
690 const char *models[2];
695 if (qemu_show_nic_models(nd->model, models))
697 if (qemu_find_nic_model(nd, models, model) < 0)
701 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
702 const char *default_model)
707 nd->model = g_strdup(default_model);
709 for (i = 0 ; models[i]; i++) {
710 if (strcmp(nd->model, models[i]) == 0)
714 error_report("Unsupported NIC model: %s", nd->model);
718 static int net_init_nic(const NetClientOptions *opts, const char *name,
719 NetClientState *peer)
723 const NetLegacyNicOptions *nic;
725 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
728 idx = nic_get_free_idx();
729 if (idx == -1 || nb_nics >= MAX_NICS) {
730 error_report("Too Many NICs");
736 memset(nd, 0, sizeof(*nd));
738 if (nic->has_netdev) {
739 nd->netdev = qemu_find_netdev(nic->netdev);
741 error_report("netdev '%s' not found", nic->netdev);
748 nd->name = g_strdup(name);
749 if (nic->has_model) {
750 nd->model = g_strdup(nic->model);
753 nd->devaddr = g_strdup(nic->addr);
756 if (nic->has_macaddr &&
757 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
758 error_report("invalid syntax for ethernet address");
761 if (nic->has_macaddr &&
762 is_multicast_ether_addr(nd->macaddr.a)) {
763 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
766 qemu_macaddr_default_if_unset(&nd->macaddr);
768 if (nic->has_vectors) {
769 if (nic->vectors > 0x7ffffff) {
770 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
773 nd->nvectors = nic->vectors;
775 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
785 static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
786 const NetClientOptions *opts,
788 NetClientState *peer) = {
789 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
791 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
793 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
794 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
796 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
799 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
801 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
802 #ifdef CONFIG_NET_BRIDGE
803 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
805 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
806 #ifdef CONFIG_VHOST_NET_USED
807 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
810 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
815 static int net_client_init1(const void *object, int is_netdev, Error **errp)
818 const Netdev *netdev;
819 const NetLegacy *net;
821 const NetClientOptions *opts;
826 opts = u.netdev->opts;
829 switch (opts->kind) {
831 case NET_CLIENT_OPTIONS_KIND_USER:
833 case NET_CLIENT_OPTIONS_KIND_TAP:
834 case NET_CLIENT_OPTIONS_KIND_SOCKET:
836 case NET_CLIENT_OPTIONS_KIND_VDE:
839 case NET_CLIENT_OPTIONS_KIND_NETMAP:
841 #ifdef CONFIG_NET_BRIDGE
842 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
844 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
845 #ifdef CONFIG_VHOST_NET_USED
846 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
849 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
854 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
855 "a netdev backend type");
861 /* missing optional values have been initialized to "all bits zero" */
862 name = u.net->has_id ? u.net->id : u.net->name;
865 if (net_client_init_fun[opts->kind]) {
866 NetClientState *peer = NULL;
868 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
871 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
872 !opts->nic->has_netdev)) {
873 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
876 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
877 /* TODO push error reporting into init() methods */
878 error_set(errp, QERR_DEVICE_INIT_FAILED,
879 NetClientOptionsKind_lookup[opts->kind]);
887 static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
890 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
892 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
897 int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
904 OptsVisitor *ov = opts_visitor_new(opts);
906 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
907 opts_visitor_cleanup(ov);
911 ret = net_client_init1(object, is_netdev, &err);
915 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
917 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
918 qapi_dealloc_visitor_cleanup(dv);
921 error_propagate(errp, err);
926 static int net_host_check_device(const char *device)
929 for (i = 0; host_net_devices[i]; i++) {
930 if (!strncmp(host_net_devices[i], device,
931 strlen(host_net_devices[i]))) {
939 void net_host_device_add(Monitor *mon, const QDict *qdict)
941 const char *device = qdict_get_str(qdict, "device");
942 const char *opts_str = qdict_get_try_str(qdict, "opts");
943 Error *local_err = NULL;
946 if (!net_host_check_device(device)) {
947 monitor_printf(mon, "invalid host network device %s\n", device);
951 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
956 qemu_opt_set(opts, "type", device);
958 net_client_init(opts, 0, &local_err);
960 qerror_report_err(local_err);
961 error_free(local_err);
962 monitor_printf(mon, "adding host network device %s failed\n", device);
966 void net_host_device_remove(Monitor *mon, const QDict *qdict)
969 int vlan_id = qdict_get_int(qdict, "vlan_id");
970 const char *device = qdict_get_str(qdict, "device");
972 nc = net_hub_find_client_by_name(vlan_id, device);
974 error_report("Host network device '%s' on hub '%d' not found",
978 if (!net_host_check_device(nc->model)) {
979 error_report("invalid host network device '%s'", device);
982 qemu_del_net_client(nc);
985 void netdev_add(QemuOpts *opts, Error **errp)
987 net_client_init(opts, 1, errp);
990 int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
992 Error *local_err = NULL;
993 QemuOptsList *opts_list;
996 opts_list = qemu_find_opts_err("netdev", &local_err);
1001 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
1006 netdev_add(opts, &local_err);
1008 qemu_opts_del(opts);
1015 qerror_report_err(local_err);
1016 error_free(local_err);
1020 void qmp_netdev_del(const char *id, Error **errp)
1025 nc = qemu_find_netdev(id);
1027 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1031 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1033 error_setg(errp, "Device '%s' is not a netdev", id);
1037 qemu_del_net_client(nc);
1038 qemu_opts_del(opts);
1041 void print_net_client(Monitor *mon, NetClientState *nc)
1043 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1045 NetClientOptionsKind_lookup[nc->info->type],
1049 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1053 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1055 QTAILQ_FOREACH(nc, &net_clients, next) {
1056 RxFilterInfoList *entry;
1059 if (has_name && strcmp(nc->name, name) != 0) {
1063 /* only query rx-filter information of NIC */
1064 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1066 error_setg(errp, "net client(%s) isn't a NIC", name);
1072 if (nc->info->query_rx_filter) {
1073 info = nc->info->query_rx_filter(nc);
1074 entry = g_malloc0(sizeof(*entry));
1075 entry->value = info;
1078 filter_list = entry;
1080 last_entry->next = entry;
1083 } else if (has_name) {
1084 error_setg(errp, "net client(%s) doesn't support"
1085 " rx-filter querying", name);
1094 if (filter_list == NULL && has_name) {
1095 error_setg(errp, "invalid net client name: %s", name);
1101 void do_info_network(Monitor *mon, const QDict *qdict)
1103 NetClientState *nc, *peer;
1104 NetClientOptionsKind type;
1108 QTAILQ_FOREACH(nc, &net_clients, next) {
1110 type = nc->info->type;
1112 /* Skip if already printed in hub info */
1113 if (net_hub_id_for_client(nc, NULL) == 0) {
1117 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
1118 print_net_client(mon, nc);
1119 } /* else it's a netdev connected to a NIC, printed with the NIC */
1120 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
1121 monitor_printf(mon, " \\ ");
1122 print_net_client(mon, peer);
1127 void qmp_set_link(const char *name, bool up, Error **errp)
1129 NetClientState *ncs[MAX_QUEUE_NUM];
1133 queues = qemu_find_net_clients_except(name, ncs,
1134 NET_CLIENT_OPTIONS_KIND_MAX,
1138 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1143 for (i = 0; i < queues; i++) {
1144 ncs[i]->link_down = !up;
1147 if (nc->info->link_status_changed) {
1148 nc->info->link_status_changed(nc);
1152 /* Change peer link only if the peer is NIC and then notify peer.
1153 * If the peer is a HUBPORT or a backend, we do not change the
1156 * This behavior is compatible with qemu vlans where there could be
1157 * multiple clients that can still communicate with each other in
1158 * disconnected mode. For now maintain this compatibility.
1160 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1161 for (i = 0; i < queues; i++) {
1162 ncs[i]->peer->link_down = !up;
1165 if (nc->peer->info->link_status_changed) {
1166 nc->peer->info->link_status_changed(nc->peer);
1171 void net_cleanup(void)
1175 /* We may del multiple entries during qemu_del_net_client(),
1176 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1178 while (!QTAILQ_EMPTY(&net_clients)) {
1179 nc = QTAILQ_FIRST(&net_clients);
1180 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1181 qemu_del_nic(qemu_get_nic(nc));
1183 qemu_del_net_client(nc);
1188 void net_check_clients(void)
1193 /* Don't warn about the default network setup that you get if
1194 * no command line -net or -netdev options are specified. There
1195 * are two cases that we would otherwise complain about:
1196 * (1) board doesn't support a NIC but the implicit "-net nic"
1198 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1199 * sets up a nic that isn't connected to anything.
1205 net_hub_check_clients();
1207 QTAILQ_FOREACH(nc, &net_clients, next) {
1209 fprintf(stderr, "Warning: %s %s has no peer\n",
1210 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1211 "nic" : "netdev", nc->name);
1215 /* Check that all NICs requested via -net nic actually got created.
1216 * NICs created via -device don't need to be checked here because
1217 * they are always instantiated.
1219 for (i = 0; i < MAX_NICS; i++) {
1220 NICInfo *nd = &nd_table[i];
1221 if (nd->used && !nd->instantiated) {
1222 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1223 "was not created (not supported by this machine?)\n",
1224 nd->name ? nd->name : "anonymous",
1225 nd->model ? nd->model : "unspecified");
1230 static int net_init_client(QemuOpts *opts, void *dummy)
1232 Error *local_err = NULL;
1234 net_client_init(opts, 0, &local_err);
1236 qerror_report_err(local_err);
1237 error_free(local_err);
1244 static int net_init_netdev(QemuOpts *opts, void *dummy)
1246 Error *local_err = NULL;
1249 ret = net_client_init(opts, 1, &local_err);
1251 qerror_report_err(local_err);
1252 error_free(local_err);
1259 int net_init_clients(void)
1261 QemuOptsList *net = qemu_find_opts("net");
1264 /* if no clients, we use a default config */
1265 qemu_opts_set(net, NULL, "type", "nic");
1267 qemu_opts_set(net, NULL, "type", "user");
1271 QTAILQ_INIT(&net_clients);
1273 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1276 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1283 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1285 #if defined(CONFIG_SLIRP)
1287 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1292 if (!qemu_opts_parse(opts_list, optarg, 1)) {
1302 unsigned compute_mcast_idx(const uint8_t *ep)
1309 for (i = 0; i < 6; i++) {
1311 for (j = 0; j < 8; j++) {
1312 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1316 crc = ((crc ^ POLYNOMIAL) | carry);
1323 QemuOptsList qemu_netdev_opts = {
1325 .implied_opt_name = "type",
1326 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1329 * no elements => accept any params
1330 * validation will happen later
1332 { /* end of list */ }
1336 QemuOptsList qemu_net_opts = {
1338 .implied_opt_name = "type",
1339 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1342 * no elements => accept any params
1343 * validation will happen later
1345 { /* end of list */ }