exec: Make stl_*_phys input an AddressSpace
[qemu/ar7.git] / net / net.c
blob2c3af202a60d72e40a871f467a3d7f04f4a431ba
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include "config-host.h"
26 #include "net/net.h"
27 #include "clients.h"
28 #include "hub.h"
29 #include "net/slirp.h"
30 #include "net/eth.h"
31 #include "util.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"
38 #include "hw/qdev.h"
39 #include "qemu/iov.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. */
46 #if !defined(_WIN32)
47 # define CONFIG_NET_BRIDGE
48 #endif
50 static QTAILQ_HEAD(, NetClientState) net_clients;
52 int default_net = 1;
54 /***********************************************************/
55 /* network device redirectors */
57 #if defined(DEBUG_NET)
58 static void hex_dump(FILE *f, const uint8_t *buf, int size)
60 int len, i, j, c;
62 for(i=0;i<size;i+=16) {
63 len = size - i;
64 if (len > 16)
65 len = 16;
66 fprintf(f, "%08x ", i);
67 for(j=0;j<16;j++) {
68 if (j < len)
69 fprintf(f, " %02x", buf[i+j]);
70 else
71 fprintf(f, " ");
73 fprintf(f, " ");
74 for(j=0;j<len;j++) {
75 c = buf[i+j];
76 if (c < ' ' || c > '~')
77 c = '.';
78 fprintf(f, "%c", c);
80 fprintf(f, "\n");
83 #endif
85 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
87 const char *p, *p1;
88 int len;
89 p = *pp;
90 p1 = strchr(p, sep);
91 if (!p1)
92 return -1;
93 len = p1 - p;
94 p1++;
95 if (buf_size > 0) {
96 if (len > buf_size - 1)
97 len = buf_size - 1;
98 memcpy(buf, p, len);
99 buf[len] = '\0';
101 *pp = p1;
102 return 0;
105 int parse_host_port(struct sockaddr_in *saddr, const char *str)
107 char buf[512];
108 struct hostent *he;
109 const char *p, *r;
110 int port;
112 p = str;
113 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
114 return -1;
115 saddr->sin_family = AF_INET;
116 if (buf[0] == '\0') {
117 saddr->sin_addr.s_addr = 0;
118 } else {
119 if (qemu_isdigit(buf[0])) {
120 if (!inet_aton(buf, &saddr->sin_addr))
121 return -1;
122 } else {
123 if ((he = gethostbyname(buf)) == NULL)
124 return - 1;
125 saddr->sin_addr = *(struct in_addr *)he->h_addr;
128 port = strtol(p, (char **)&r, 0);
129 if (r == p)
130 return -1;
131 saddr->sin_port = htons(port);
132 return 0;
135 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
137 snprintf(nc->info_str, sizeof(nc->info_str),
138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
139 nc->model,
140 macaddr[0], macaddr[1], macaddr[2],
141 macaddr[3], macaddr[4], macaddr[5]);
144 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
146 static int index = 0;
147 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
149 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
150 return;
151 macaddr->a[0] = 0x52;
152 macaddr->a[1] = 0x54;
153 macaddr->a[2] = 0x00;
154 macaddr->a[3] = 0x12;
155 macaddr->a[4] = 0x34;
156 macaddr->a[5] = 0x56 + index++;
160 * Generate a name for net client
162 * Only net clients created with the legacy -net option and NICs need this.
164 static char *assign_name(NetClientState *nc1, const char *model)
166 NetClientState *nc;
167 int id = 0;
169 QTAILQ_FOREACH(nc, &net_clients, next) {
170 if (nc == nc1) {
171 continue;
173 if (strcmp(nc->model, model) == 0) {
174 id++;
178 return g_strdup_printf("%s.%d", model, id);
181 static void qemu_net_client_destructor(NetClientState *nc)
183 g_free(nc);
186 static void qemu_net_client_setup(NetClientState *nc,
187 NetClientInfo *info,
188 NetClientState *peer,
189 const char *model,
190 const char *name,
191 NetClientDestructor *destructor)
193 nc->info = info;
194 nc->model = g_strdup(model);
195 if (name) {
196 nc->name = g_strdup(name);
197 } else {
198 nc->name = assign_name(nc, model);
201 if (peer) {
202 assert(!peer->peer);
203 nc->peer = peer;
204 peer->peer = nc;
206 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
208 nc->incoming_queue = qemu_new_net_queue(nc);
209 nc->destructor = destructor;
212 NetClientState *qemu_new_net_client(NetClientInfo *info,
213 NetClientState *peer,
214 const char *model,
215 const char *name)
217 NetClientState *nc;
219 assert(info->size >= sizeof(NetClientState));
221 nc = g_malloc0(info->size);
222 qemu_net_client_setup(nc, info, peer, model, name,
223 qemu_net_client_destructor);
225 return nc;
228 NICState *qemu_new_nic(NetClientInfo *info,
229 NICConf *conf,
230 const char *model,
231 const char *name,
232 void *opaque)
234 NetClientState **peers = conf->peers.ncs;
235 NICState *nic;
236 int i, queues = MAX(1, conf->queues);
238 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
239 assert(info->size >= sizeof(NICState));
241 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
242 nic->ncs = (void *)nic + info->size;
243 nic->conf = conf;
244 nic->opaque = opaque;
246 for (i = 0; i < queues; i++) {
247 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
248 NULL);
249 nic->ncs[i].queue_index = i;
252 return nic;
255 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
257 return nic->ncs + queue_index;
260 NetClientState *qemu_get_queue(NICState *nic)
262 return qemu_get_subqueue(nic, 0);
265 NICState *qemu_get_nic(NetClientState *nc)
267 NetClientState *nc0 = nc - nc->queue_index;
269 return (NICState *)((void *)nc0 - nc->info->size);
272 void *qemu_get_nic_opaque(NetClientState *nc)
274 NICState *nic = qemu_get_nic(nc);
276 return nic->opaque;
279 static void qemu_cleanup_net_client(NetClientState *nc)
281 QTAILQ_REMOVE(&net_clients, nc, next);
283 if (nc->info->cleanup) {
284 nc->info->cleanup(nc);
288 static void qemu_free_net_client(NetClientState *nc)
290 if (nc->incoming_queue) {
291 qemu_del_net_queue(nc->incoming_queue);
293 if (nc->peer) {
294 nc->peer->peer = NULL;
296 g_free(nc->name);
297 g_free(nc->model);
298 if (nc->destructor) {
299 nc->destructor(nc);
303 void qemu_del_net_client(NetClientState *nc)
305 NetClientState *ncs[MAX_QUEUE_NUM];
306 int queues, i;
308 /* If the NetClientState belongs to a multiqueue backend, we will change all
309 * other NetClientStates also.
311 queues = qemu_find_net_clients_except(nc->name, ncs,
312 NET_CLIENT_OPTIONS_KIND_NIC,
313 MAX_QUEUE_NUM);
314 assert(queues != 0);
316 /* If there is a peer NIC, delete and cleanup client, but do not free. */
317 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
318 NICState *nic = qemu_get_nic(nc->peer);
319 if (nic->peer_deleted) {
320 return;
322 nic->peer_deleted = true;
324 for (i = 0; i < queues; i++) {
325 ncs[i]->peer->link_down = true;
328 if (nc->peer->info->link_status_changed) {
329 nc->peer->info->link_status_changed(nc->peer);
332 for (i = 0; i < queues; i++) {
333 qemu_cleanup_net_client(ncs[i]);
336 return;
339 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
341 for (i = 0; i < queues; i++) {
342 qemu_cleanup_net_client(ncs[i]);
343 qemu_free_net_client(ncs[i]);
347 void qemu_del_nic(NICState *nic)
349 int i, queues = MAX(nic->conf->queues, 1);
351 /* If this is a peer NIC and peer has already been deleted, free it now. */
352 if (nic->peer_deleted) {
353 for (i = 0; i < queues; i++) {
354 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
358 for (i = queues - 1; i >= 0; i--) {
359 NetClientState *nc = qemu_get_subqueue(nic, i);
361 qemu_cleanup_net_client(nc);
362 qemu_free_net_client(nc);
365 g_free(nic);
368 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
370 NetClientState *nc;
372 QTAILQ_FOREACH(nc, &net_clients, next) {
373 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
374 if (nc->queue_index == 0) {
375 func(qemu_get_nic(nc), opaque);
381 int qemu_can_send_packet(NetClientState *sender)
383 if (!sender->peer) {
384 return 1;
387 if (sender->peer->receive_disabled) {
388 return 0;
389 } else if (sender->peer->info->can_receive &&
390 !sender->peer->info->can_receive(sender->peer)) {
391 return 0;
393 return 1;
396 ssize_t qemu_deliver_packet(NetClientState *sender,
397 unsigned flags,
398 const uint8_t *data,
399 size_t size,
400 void *opaque)
402 NetClientState *nc = opaque;
403 ssize_t ret;
405 if (nc->link_down) {
406 return size;
409 if (nc->receive_disabled) {
410 return 0;
413 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
414 ret = nc->info->receive_raw(nc, data, size);
415 } else {
416 ret = nc->info->receive(nc, data, size);
419 if (ret == 0) {
420 nc->receive_disabled = 1;
423 return ret;
426 void qemu_purge_queued_packets(NetClientState *nc)
428 if (!nc->peer) {
429 return;
432 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
435 void qemu_flush_queued_packets(NetClientState *nc)
437 nc->receive_disabled = 0;
439 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
440 if (net_hub_flush(nc->peer)) {
441 qemu_notify_event();
444 if (qemu_net_queue_flush(nc->incoming_queue)) {
445 /* We emptied the queue successfully, signal to the IO thread to repoll
446 * the file descriptor (for tap, for example).
448 qemu_notify_event();
452 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
453 unsigned flags,
454 const uint8_t *buf, int size,
455 NetPacketSent *sent_cb)
457 NetQueue *queue;
459 #ifdef DEBUG_NET
460 printf("qemu_send_packet_async:\n");
461 hex_dump(stdout, buf, size);
462 #endif
464 if (sender->link_down || !sender->peer) {
465 return size;
468 queue = sender->peer->incoming_queue;
470 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
473 ssize_t qemu_send_packet_async(NetClientState *sender,
474 const uint8_t *buf, int size,
475 NetPacketSent *sent_cb)
477 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
478 buf, size, sent_cb);
481 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
483 qemu_send_packet_async(nc, buf, size, NULL);
486 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
488 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
489 buf, size, NULL);
492 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
493 int iovcnt)
495 uint8_t buffer[NET_BUFSIZE];
496 size_t offset;
498 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
500 return nc->info->receive(nc, buffer, offset);
503 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
504 unsigned flags,
505 const struct iovec *iov,
506 int iovcnt,
507 void *opaque)
509 NetClientState *nc = opaque;
510 int ret;
512 if (nc->link_down) {
513 return iov_size(iov, iovcnt);
516 if (nc->receive_disabled) {
517 return 0;
520 if (nc->info->receive_iov) {
521 ret = nc->info->receive_iov(nc, iov, iovcnt);
522 } else {
523 ret = nc_sendv_compat(nc, iov, iovcnt);
526 if (ret == 0) {
527 nc->receive_disabled = 1;
530 return ret;
533 ssize_t qemu_sendv_packet_async(NetClientState *sender,
534 const struct iovec *iov, int iovcnt,
535 NetPacketSent *sent_cb)
537 NetQueue *queue;
539 if (sender->link_down || !sender->peer) {
540 return iov_size(iov, iovcnt);
543 queue = sender->peer->incoming_queue;
545 return qemu_net_queue_send_iov(queue, sender,
546 QEMU_NET_PACKET_FLAG_NONE,
547 iov, iovcnt, sent_cb);
550 ssize_t
551 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
553 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
556 NetClientState *qemu_find_netdev(const char *id)
558 NetClientState *nc;
560 QTAILQ_FOREACH(nc, &net_clients, next) {
561 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
562 continue;
563 if (!strcmp(nc->name, id)) {
564 return nc;
568 return NULL;
571 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
572 NetClientOptionsKind type, int max)
574 NetClientState *nc;
575 int ret = 0;
577 QTAILQ_FOREACH(nc, &net_clients, next) {
578 if (nc->info->type == type) {
579 continue;
581 if (!strcmp(nc->name, id)) {
582 if (ret < max) {
583 ncs[ret] = nc;
585 ret++;
589 return ret;
592 static int nic_get_free_idx(void)
594 int index;
596 for (index = 0; index < MAX_NICS; index++)
597 if (!nd_table[index].used)
598 return index;
599 return -1;
602 int qemu_show_nic_models(const char *arg, const char *const *models)
604 int i;
606 if (!arg || !is_help_option(arg)) {
607 return 0;
610 fprintf(stderr, "qemu: Supported NIC models: ");
611 for (i = 0 ; models[i]; i++)
612 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
613 return 1;
616 void qemu_check_nic_model(NICInfo *nd, const char *model)
618 const char *models[2];
620 models[0] = model;
621 models[1] = NULL;
623 if (qemu_show_nic_models(nd->model, models))
624 exit(0);
625 if (qemu_find_nic_model(nd, models, model) < 0)
626 exit(1);
629 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
630 const char *default_model)
632 int i;
634 if (!nd->model)
635 nd->model = g_strdup(default_model);
637 for (i = 0 ; models[i]; i++) {
638 if (strcmp(nd->model, models[i]) == 0)
639 return i;
642 error_report("Unsupported NIC model: %s", nd->model);
643 return -1;
646 static int net_init_nic(const NetClientOptions *opts, const char *name,
647 NetClientState *peer)
649 int idx;
650 NICInfo *nd;
651 const NetLegacyNicOptions *nic;
653 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
654 nic = opts->nic;
656 idx = nic_get_free_idx();
657 if (idx == -1 || nb_nics >= MAX_NICS) {
658 error_report("Too Many NICs");
659 return -1;
662 nd = &nd_table[idx];
664 memset(nd, 0, sizeof(*nd));
666 if (nic->has_netdev) {
667 nd->netdev = qemu_find_netdev(nic->netdev);
668 if (!nd->netdev) {
669 error_report("netdev '%s' not found", nic->netdev);
670 return -1;
672 } else {
673 assert(peer);
674 nd->netdev = peer;
676 nd->name = g_strdup(name);
677 if (nic->has_model) {
678 nd->model = g_strdup(nic->model);
680 if (nic->has_addr) {
681 nd->devaddr = g_strdup(nic->addr);
684 if (nic->has_macaddr &&
685 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
686 error_report("invalid syntax for ethernet address");
687 return -1;
689 if (nic->has_macaddr &&
690 is_multicast_ether_addr(nd->macaddr.a)) {
691 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
692 return -1;
694 qemu_macaddr_default_if_unset(&nd->macaddr);
696 if (nic->has_vectors) {
697 if (nic->vectors > 0x7ffffff) {
698 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
699 return -1;
701 nd->nvectors = nic->vectors;
702 } else {
703 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
706 nd->used = 1;
707 nb_nics++;
709 return idx;
713 static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
714 const NetClientOptions *opts,
715 const char *name,
716 NetClientState *peer) = {
717 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
718 #ifdef CONFIG_SLIRP
719 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
720 #endif
721 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
722 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
723 #ifdef CONFIG_VDE
724 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
725 #endif
726 #ifdef CONFIG_NETMAP
727 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
728 #endif
729 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
730 #ifdef CONFIG_NET_BRIDGE
731 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
732 #endif
733 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
737 static int net_client_init1(const void *object, int is_netdev, Error **errp)
739 union {
740 const Netdev *netdev;
741 const NetLegacy *net;
742 } u;
743 const NetClientOptions *opts;
744 const char *name;
746 if (is_netdev) {
747 u.netdev = object;
748 opts = u.netdev->opts;
749 name = u.netdev->id;
751 switch (opts->kind) {
752 #ifdef CONFIG_SLIRP
753 case NET_CLIENT_OPTIONS_KIND_USER:
754 #endif
755 case NET_CLIENT_OPTIONS_KIND_TAP:
756 case NET_CLIENT_OPTIONS_KIND_SOCKET:
757 #ifdef CONFIG_VDE
758 case NET_CLIENT_OPTIONS_KIND_VDE:
759 #endif
760 #ifdef CONFIG_NETMAP
761 case NET_CLIENT_OPTIONS_KIND_NETMAP:
762 #endif
763 #ifdef CONFIG_NET_BRIDGE
764 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
765 #endif
766 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
767 break;
769 default:
770 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
771 "a netdev backend type");
772 return -1;
774 } else {
775 u.net = object;
776 opts = u.net->opts;
777 /* missing optional values have been initialized to "all bits zero" */
778 name = u.net->has_id ? u.net->id : u.net->name;
781 if (net_client_init_fun[opts->kind]) {
782 NetClientState *peer = NULL;
784 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
785 * parameter. */
786 if (!is_netdev &&
787 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
788 !opts->nic->has_netdev)) {
789 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
792 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
793 /* TODO push error reporting into init() methods */
794 error_set(errp, QERR_DEVICE_INIT_FAILED,
795 NetClientOptionsKind_lookup[opts->kind]);
796 return -1;
799 return 0;
803 static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
805 if (is_netdev) {
806 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
807 } else {
808 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
813 int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
815 void *object = NULL;
816 Error *err = NULL;
817 int ret = -1;
820 OptsVisitor *ov = opts_visitor_new(opts);
822 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
823 opts_visitor_cleanup(ov);
826 if (!err) {
827 ret = net_client_init1(object, is_netdev, &err);
830 if (object) {
831 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
833 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
834 qapi_dealloc_visitor_cleanup(dv);
837 error_propagate(errp, err);
838 return ret;
842 static int net_host_check_device(const char *device)
844 int i;
845 const char *valid_param_list[] = { "tap", "socket", "dump"
846 #ifdef CONFIG_NET_BRIDGE
847 , "bridge"
848 #endif
849 #ifdef CONFIG_SLIRP
850 ,"user"
851 #endif
852 #ifdef CONFIG_VDE
853 ,"vde"
854 #endif
856 for (i = 0; i < ARRAY_SIZE(valid_param_list); i++) {
857 if (!strncmp(valid_param_list[i], device,
858 strlen(valid_param_list[i])))
859 return 1;
862 return 0;
865 void net_host_device_add(Monitor *mon, const QDict *qdict)
867 const char *device = qdict_get_str(qdict, "device");
868 const char *opts_str = qdict_get_try_str(qdict, "opts");
869 Error *local_err = NULL;
870 QemuOpts *opts;
872 if (!net_host_check_device(device)) {
873 monitor_printf(mon, "invalid host network device %s\n", device);
874 return;
877 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
878 if (!opts) {
879 return;
882 qemu_opt_set(opts, "type", device);
884 net_client_init(opts, 0, &local_err);
885 if (error_is_set(&local_err)) {
886 qerror_report_err(local_err);
887 error_free(local_err);
888 monitor_printf(mon, "adding host network device %s failed\n", device);
892 void net_host_device_remove(Monitor *mon, const QDict *qdict)
894 NetClientState *nc;
895 int vlan_id = qdict_get_int(qdict, "vlan_id");
896 const char *device = qdict_get_str(qdict, "device");
898 nc = net_hub_find_client_by_name(vlan_id, device);
899 if (!nc) {
900 return;
902 if (!net_host_check_device(nc->model)) {
903 monitor_printf(mon, "invalid host network device %s\n", device);
904 return;
906 qemu_del_net_client(nc);
909 void netdev_add(QemuOpts *opts, Error **errp)
911 net_client_init(opts, 1, errp);
914 int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
916 Error *local_err = NULL;
917 QemuOptsList *opts_list;
918 QemuOpts *opts;
920 opts_list = qemu_find_opts_err("netdev", &local_err);
921 if (error_is_set(&local_err)) {
922 goto exit_err;
925 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
926 if (error_is_set(&local_err)) {
927 goto exit_err;
930 netdev_add(opts, &local_err);
931 if (error_is_set(&local_err)) {
932 qemu_opts_del(opts);
933 goto exit_err;
936 return 0;
938 exit_err:
939 qerror_report_err(local_err);
940 error_free(local_err);
941 return -1;
944 void qmp_netdev_del(const char *id, Error **errp)
946 NetClientState *nc;
947 QemuOpts *opts;
949 nc = qemu_find_netdev(id);
950 if (!nc) {
951 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
952 return;
955 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
956 if (!opts) {
957 error_setg(errp, "Device '%s' is not a netdev", id);
958 return;
961 qemu_del_net_client(nc);
962 qemu_opts_del(opts);
965 void print_net_client(Monitor *mon, NetClientState *nc)
967 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
968 nc->queue_index,
969 NetClientOptionsKind_lookup[nc->info->type],
970 nc->info_str);
973 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
974 Error **errp)
976 NetClientState *nc;
977 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
979 QTAILQ_FOREACH(nc, &net_clients, next) {
980 RxFilterInfoList *entry;
981 RxFilterInfo *info;
983 if (has_name && strcmp(nc->name, name) != 0) {
984 continue;
987 /* only query rx-filter information of NIC */
988 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
989 if (has_name) {
990 error_setg(errp, "net client(%s) isn't a NIC", name);
991 break;
993 continue;
996 if (nc->info->query_rx_filter) {
997 info = nc->info->query_rx_filter(nc);
998 entry = g_malloc0(sizeof(*entry));
999 entry->value = info;
1001 if (!filter_list) {
1002 filter_list = entry;
1003 } else {
1004 last_entry->next = entry;
1006 last_entry = entry;
1007 } else if (has_name) {
1008 error_setg(errp, "net client(%s) doesn't support"
1009 " rx-filter querying", name);
1010 break;
1014 if (filter_list == NULL && !error_is_set(errp) && has_name) {
1015 error_setg(errp, "invalid net client name: %s", name);
1018 return filter_list;
1021 void do_info_network(Monitor *mon, const QDict *qdict)
1023 NetClientState *nc, *peer;
1024 NetClientOptionsKind type;
1026 net_hub_info(mon);
1028 QTAILQ_FOREACH(nc, &net_clients, next) {
1029 peer = nc->peer;
1030 type = nc->info->type;
1032 /* Skip if already printed in hub info */
1033 if (net_hub_id_for_client(nc, NULL) == 0) {
1034 continue;
1037 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
1038 print_net_client(mon, nc);
1039 } /* else it's a netdev connected to a NIC, printed with the NIC */
1040 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
1041 monitor_printf(mon, " \\ ");
1042 print_net_client(mon, peer);
1047 void qmp_set_link(const char *name, bool up, Error **errp)
1049 NetClientState *ncs[MAX_QUEUE_NUM];
1050 NetClientState *nc;
1051 int queues, i;
1053 queues = qemu_find_net_clients_except(name, ncs,
1054 NET_CLIENT_OPTIONS_KIND_MAX,
1055 MAX_QUEUE_NUM);
1057 if (queues == 0) {
1058 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1059 return;
1061 nc = ncs[0];
1063 for (i = 0; i < queues; i++) {
1064 ncs[i]->link_down = !up;
1067 if (nc->info->link_status_changed) {
1068 nc->info->link_status_changed(nc);
1071 if (nc->peer) {
1072 /* Change peer link only if the peer is NIC and then notify peer.
1073 * If the peer is a HUBPORT or a backend, we do not change the
1074 * link status.
1076 * This behavior is compatible with qemu vlans where there could be
1077 * multiple clients that can still communicate with each other in
1078 * disconnected mode. For now maintain this compatibility.
1080 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1081 for (i = 0; i < queues; i++) {
1082 ncs[i]->peer->link_down = !up;
1085 if (nc->peer->info->link_status_changed) {
1086 nc->peer->info->link_status_changed(nc->peer);
1091 void net_cleanup(void)
1093 NetClientState *nc;
1095 /* We may del multiple entries during qemu_del_net_client(),
1096 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1098 while (!QTAILQ_EMPTY(&net_clients)) {
1099 nc = QTAILQ_FIRST(&net_clients);
1100 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1101 qemu_del_nic(qemu_get_nic(nc));
1102 } else {
1103 qemu_del_net_client(nc);
1108 void net_check_clients(void)
1110 NetClientState *nc;
1111 int i;
1113 /* Don't warn about the default network setup that you get if
1114 * no command line -net or -netdev options are specified. There
1115 * are two cases that we would otherwise complain about:
1116 * (1) board doesn't support a NIC but the implicit "-net nic"
1117 * requested one
1118 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1119 * sets up a nic that isn't connected to anything.
1121 if (default_net) {
1122 return;
1125 net_hub_check_clients();
1127 QTAILQ_FOREACH(nc, &net_clients, next) {
1128 if (!nc->peer) {
1129 fprintf(stderr, "Warning: %s %s has no peer\n",
1130 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1131 "nic" : "netdev", nc->name);
1135 /* Check that all NICs requested via -net nic actually got created.
1136 * NICs created via -device don't need to be checked here because
1137 * they are always instantiated.
1139 for (i = 0; i < MAX_NICS; i++) {
1140 NICInfo *nd = &nd_table[i];
1141 if (nd->used && !nd->instantiated) {
1142 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1143 "was not created (not supported by this machine?)\n",
1144 nd->name ? nd->name : "anonymous",
1145 nd->model ? nd->model : "unspecified");
1150 static int net_init_client(QemuOpts *opts, void *dummy)
1152 Error *local_err = NULL;
1154 net_client_init(opts, 0, &local_err);
1155 if (error_is_set(&local_err)) {
1156 qerror_report_err(local_err);
1157 error_free(local_err);
1158 return -1;
1161 return 0;
1164 static int net_init_netdev(QemuOpts *opts, void *dummy)
1166 Error *local_err = NULL;
1167 int ret;
1169 ret = net_client_init(opts, 1, &local_err);
1170 if (error_is_set(&local_err)) {
1171 qerror_report_err(local_err);
1172 error_free(local_err);
1173 return -1;
1176 return ret;
1179 int net_init_clients(void)
1181 QemuOptsList *net = qemu_find_opts("net");
1183 if (default_net) {
1184 /* if no clients, we use a default config */
1185 qemu_opts_set(net, NULL, "type", "nic");
1186 #ifdef CONFIG_SLIRP
1187 qemu_opts_set(net, NULL, "type", "user");
1188 #endif
1191 QTAILQ_INIT(&net_clients);
1193 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1194 return -1;
1196 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1197 return -1;
1200 return 0;
1203 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1205 #if defined(CONFIG_SLIRP)
1206 int ret;
1207 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1208 return ret;
1210 #endif
1212 if (!qemu_opts_parse(opts_list, optarg, 1)) {
1213 return -1;
1216 default_net = 0;
1217 return 0;
1220 /* From FreeBSD */
1221 /* XXX: optimize */
1222 unsigned compute_mcast_idx(const uint8_t *ep)
1224 uint32_t crc;
1225 int carry, i, j;
1226 uint8_t b;
1228 crc = 0xffffffff;
1229 for (i = 0; i < 6; i++) {
1230 b = *ep++;
1231 for (j = 0; j < 8; j++) {
1232 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1233 crc <<= 1;
1234 b >>= 1;
1235 if (carry) {
1236 crc = ((crc ^ POLYNOMIAL) | carry);
1240 return crc >> 26;
1243 QemuOptsList qemu_netdev_opts = {
1244 .name = "netdev",
1245 .implied_opt_name = "type",
1246 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1247 .desc = {
1249 * no elements => accept any params
1250 * validation will happen later
1252 { /* end of list */ }
1256 QemuOptsList qemu_net_opts = {
1257 .name = "net",
1258 .implied_opt_name = "type",
1259 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1260 .desc = {
1262 * no elements => accept any params
1263 * validation will happen later
1265 { /* end of list */ }