exec: Rename NEED_CPU_H -> COMPILING_PER_TARGET
[qemu/armbru.git] / net / net.c
bloba2f0c828bbf2f55f4226f561b95da4f2519df03a
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.
25 #include "qemu/osdep.h"
27 #include "net/net.h"
28 #include "clients.h"
29 #include "hub.h"
30 #include "hw/qdev-properties.h"
31 #include "net/slirp.h"
32 #include "net/eth.h"
33 #include "util.h"
35 #include "monitor/monitor.h"
36 #include "qemu/help_option.h"
37 #include "qapi/qapi-commands-net.h"
38 #include "qapi/qapi-visit-net.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qmp/qerror.h"
41 #include "qemu/error-report.h"
42 #include "qemu/sockets.h"
43 #include "qemu/cutils.h"
44 #include "qemu/config-file.h"
45 #include "qemu/ctype.h"
46 #include "qemu/id.h"
47 #include "qemu/iov.h"
48 #include "qemu/qemu-print.h"
49 #include "qemu/main-loop.h"
50 #include "qemu/option.h"
51 #include "qemu/keyval.h"
52 #include "qapi/error.h"
53 #include "qapi/opts-visitor.h"
54 #include "sysemu/runstate.h"
55 #include "net/colo-compare.h"
56 #include "net/filter.h"
57 #include "qapi/string-output-visitor.h"
58 #include "qapi/qobject-input-visitor.h"
60 /* Net bridge is currently not supported for W32. */
61 #if !defined(_WIN32)
62 # define CONFIG_NET_BRIDGE
63 #endif
65 static VMChangeStateEntry *net_change_state_entry;
66 NetClientStateList net_clients;
68 typedef struct NetdevQueueEntry {
69 Netdev *nd;
70 Location loc;
71 QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
72 } NetdevQueueEntry;
74 typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
76 static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
78 static GHashTable *nic_model_help;
80 static int nb_nics;
81 static NICInfo nd_table[MAX_NICS];
83 /***********************************************************/
84 /* network device redirectors */
86 int convert_host_port(struct sockaddr_in *saddr, const char *host,
87 const char *port, Error **errp)
89 struct hostent *he;
90 const char *r;
91 long p;
93 memset(saddr, 0, sizeof(*saddr));
95 saddr->sin_family = AF_INET;
96 if (host[0] == '\0') {
97 saddr->sin_addr.s_addr = 0;
98 } else {
99 if (qemu_isdigit(host[0])) {
100 if (!inet_aton(host, &saddr->sin_addr)) {
101 error_setg(errp, "host address '%s' is not a valid "
102 "IPv4 address", host);
103 return -1;
105 } else {
106 he = gethostbyname(host);
107 if (he == NULL) {
108 error_setg(errp, "can't resolve host address '%s'", host);
109 return -1;
111 saddr->sin_addr = *(struct in_addr *)he->h_addr;
114 if (qemu_strtol(port, &r, 0, &p) != 0) {
115 error_setg(errp, "port number '%s' is invalid", port);
116 return -1;
118 saddr->sin_port = htons(p);
119 return 0;
122 int parse_host_port(struct sockaddr_in *saddr, const char *str,
123 Error **errp)
125 gchar **substrings;
126 int ret;
128 substrings = g_strsplit(str, ":", 2);
129 if (!substrings || !substrings[0] || !substrings[1]) {
130 error_setg(errp, "host address '%s' doesn't contain ':' "
131 "separating host from port", str);
132 ret = -1;
133 goto out;
136 ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
138 out:
139 g_strfreev(substrings);
140 return ret;
143 char *qemu_mac_strdup_printf(const uint8_t *macaddr)
145 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
146 macaddr[0], macaddr[1], macaddr[2],
147 macaddr[3], macaddr[4], macaddr[5]);
150 void qemu_set_info_str(NetClientState *nc, const char *fmt, ...)
152 va_list ap;
154 va_start(ap, fmt);
155 vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap);
156 va_end(ap);
159 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
161 qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
162 nc->model, macaddr[0], macaddr[1], macaddr[2],
163 macaddr[3], macaddr[4], macaddr[5]);
166 static int mac_table[256] = {0};
168 static void qemu_macaddr_set_used(MACAddr *macaddr)
170 int index;
172 for (index = 0x56; index < 0xFF; index++) {
173 if (macaddr->a[5] == index) {
174 mac_table[index]++;
179 static void qemu_macaddr_set_free(MACAddr *macaddr)
181 int index;
182 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
184 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
185 return;
187 for (index = 0x56; index < 0xFF; index++) {
188 if (macaddr->a[5] == index) {
189 mac_table[index]--;
194 static int qemu_macaddr_get_free(void)
196 int index;
198 for (index = 0x56; index < 0xFF; index++) {
199 if (mac_table[index] == 0) {
200 return index;
204 return -1;
207 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
209 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
210 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
212 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
213 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
214 return;
215 } else {
216 qemu_macaddr_set_used(macaddr);
217 return;
221 macaddr->a[0] = 0x52;
222 macaddr->a[1] = 0x54;
223 macaddr->a[2] = 0x00;
224 macaddr->a[3] = 0x12;
225 macaddr->a[4] = 0x34;
226 macaddr->a[5] = qemu_macaddr_get_free();
227 qemu_macaddr_set_used(macaddr);
231 * Generate a name for net client
233 * Only net clients created with the legacy -net option and NICs need this.
235 static char *assign_name(NetClientState *nc1, const char *model)
237 NetClientState *nc;
238 int id = 0;
240 QTAILQ_FOREACH(nc, &net_clients, next) {
241 if (nc == nc1) {
242 continue;
244 if (strcmp(nc->model, model) == 0) {
245 id++;
249 return g_strdup_printf("%s.%d", model, id);
252 static void qemu_net_client_destructor(NetClientState *nc)
254 g_free(nc);
256 static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
257 unsigned flags,
258 const struct iovec *iov,
259 int iovcnt,
260 void *opaque);
262 static void qemu_net_client_setup(NetClientState *nc,
263 NetClientInfo *info,
264 NetClientState *peer,
265 const char *model,
266 const char *name,
267 NetClientDestructor *destructor,
268 bool is_datapath)
270 nc->info = info;
271 nc->model = g_strdup(model);
272 if (name) {
273 nc->name = g_strdup(name);
274 } else {
275 nc->name = assign_name(nc, model);
278 if (peer) {
279 assert(!peer->peer);
280 nc->peer = peer;
281 peer->peer = nc;
283 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
285 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
286 nc->destructor = destructor;
287 nc->is_datapath = is_datapath;
288 QTAILQ_INIT(&nc->filters);
291 NetClientState *qemu_new_net_client(NetClientInfo *info,
292 NetClientState *peer,
293 const char *model,
294 const char *name)
296 NetClientState *nc;
298 assert(info->size >= sizeof(NetClientState));
300 nc = g_malloc0(info->size);
301 qemu_net_client_setup(nc, info, peer, model, name,
302 qemu_net_client_destructor, true);
304 return nc;
307 NetClientState *qemu_new_net_control_client(NetClientInfo *info,
308 NetClientState *peer,
309 const char *model,
310 const char *name)
312 NetClientState *nc;
314 assert(info->size >= sizeof(NetClientState));
316 nc = g_malloc0(info->size);
317 qemu_net_client_setup(nc, info, peer, model, name,
318 qemu_net_client_destructor, false);
320 return nc;
323 NICState *qemu_new_nic(NetClientInfo *info,
324 NICConf *conf,
325 const char *model,
326 const char *name,
327 MemReentrancyGuard *reentrancy_guard,
328 void *opaque)
330 NetClientState **peers = conf->peers.ncs;
331 NICState *nic;
332 int i, queues = MAX(1, conf->peers.queues);
334 assert(info->type == NET_CLIENT_DRIVER_NIC);
335 assert(info->size >= sizeof(NICState));
337 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
338 nic->ncs = (void *)nic + info->size;
339 nic->conf = conf;
340 nic->reentrancy_guard = reentrancy_guard,
341 nic->opaque = opaque;
343 for (i = 0; i < queues; i++) {
344 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
345 NULL, true);
346 nic->ncs[i].queue_index = i;
349 return nic;
352 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
354 return nic->ncs + queue_index;
357 NetClientState *qemu_get_queue(NICState *nic)
359 return qemu_get_subqueue(nic, 0);
362 NICState *qemu_get_nic(NetClientState *nc)
364 NetClientState *nc0 = nc - nc->queue_index;
366 return (NICState *)((void *)nc0 - nc->info->size);
369 void *qemu_get_nic_opaque(NetClientState *nc)
371 NICState *nic = qemu_get_nic(nc);
373 return nic->opaque;
376 NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
378 assert(nc != NULL);
379 NetClientState *ncs = nc + queue_index;
380 return ncs->peer;
383 static void qemu_cleanup_net_client(NetClientState *nc)
385 QTAILQ_REMOVE(&net_clients, nc, next);
387 if (nc->info->cleanup) {
388 nc->info->cleanup(nc);
392 static void qemu_free_net_client(NetClientState *nc)
394 if (nc->incoming_queue) {
395 qemu_del_net_queue(nc->incoming_queue);
397 if (nc->peer) {
398 nc->peer->peer = NULL;
400 g_free(nc->name);
401 g_free(nc->model);
402 if (nc->destructor) {
403 nc->destructor(nc);
407 void qemu_del_net_client(NetClientState *nc)
409 NetClientState *ncs[MAX_QUEUE_NUM];
410 int queues, i;
411 NetFilterState *nf, *next;
413 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
415 /* If the NetClientState belongs to a multiqueue backend, we will change all
416 * other NetClientStates also.
418 queues = qemu_find_net_clients_except(nc->name, ncs,
419 NET_CLIENT_DRIVER_NIC,
420 MAX_QUEUE_NUM);
421 assert(queues != 0);
423 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
424 object_unparent(OBJECT(nf));
427 /* If there is a peer NIC, delete and cleanup client, but do not free. */
428 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
429 NICState *nic = qemu_get_nic(nc->peer);
430 if (nic->peer_deleted) {
431 return;
433 nic->peer_deleted = true;
435 for (i = 0; i < queues; i++) {
436 ncs[i]->peer->link_down = true;
439 if (nc->peer->info->link_status_changed) {
440 nc->peer->info->link_status_changed(nc->peer);
443 for (i = 0; i < queues; i++) {
444 qemu_cleanup_net_client(ncs[i]);
447 return;
450 for (i = 0; i < queues; i++) {
451 qemu_cleanup_net_client(ncs[i]);
452 qemu_free_net_client(ncs[i]);
456 void qemu_del_nic(NICState *nic)
458 int i, queues = MAX(nic->conf->peers.queues, 1);
460 qemu_macaddr_set_free(&nic->conf->macaddr);
462 for (i = 0; i < queues; i++) {
463 NetClientState *nc = qemu_get_subqueue(nic, i);
464 /* If this is a peer NIC and peer has already been deleted, free it now. */
465 if (nic->peer_deleted) {
466 qemu_free_net_client(nc->peer);
467 } else if (nc->peer) {
468 /* if there are RX packets pending, complete them */
469 qemu_purge_queued_packets(nc->peer);
473 for (i = queues - 1; i >= 0; i--) {
474 NetClientState *nc = qemu_get_subqueue(nic, i);
476 qemu_cleanup_net_client(nc);
477 qemu_free_net_client(nc);
480 g_free(nic);
483 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
485 NetClientState *nc;
487 QTAILQ_FOREACH(nc, &net_clients, next) {
488 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
489 if (nc->queue_index == 0) {
490 func(qemu_get_nic(nc), opaque);
496 bool qemu_has_ufo(NetClientState *nc)
498 if (!nc || !nc->info->has_ufo) {
499 return false;
502 return nc->info->has_ufo(nc);
505 bool qemu_has_uso(NetClientState *nc)
507 if (!nc || !nc->info->has_uso) {
508 return false;
511 return nc->info->has_uso(nc);
514 bool qemu_has_vnet_hdr(NetClientState *nc)
516 if (!nc || !nc->info->has_vnet_hdr) {
517 return false;
520 return nc->info->has_vnet_hdr(nc);
523 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
525 if (!nc || !nc->info->has_vnet_hdr_len) {
526 return false;
529 return nc->info->has_vnet_hdr_len(nc, len);
532 bool qemu_get_using_vnet_hdr(NetClientState *nc)
534 if (!nc || !nc->info->get_using_vnet_hdr) {
535 return false;
538 return nc->info->get_using_vnet_hdr(nc);
541 void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
543 if (!nc || !nc->info->using_vnet_hdr) {
544 return;
547 nc->info->using_vnet_hdr(nc, enable);
550 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
551 int ecn, int ufo, int uso4, int uso6)
553 if (!nc || !nc->info->set_offload) {
554 return;
557 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6);
560 int qemu_get_vnet_hdr_len(NetClientState *nc)
562 if (!nc || !nc->info->get_vnet_hdr_len) {
563 return 0;
566 return nc->info->get_vnet_hdr_len(nc);
569 void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
571 if (!nc || !nc->info->set_vnet_hdr_len) {
572 return;
575 nc->vnet_hdr_len = len;
576 nc->info->set_vnet_hdr_len(nc, len);
579 int qemu_set_vnet_le(NetClientState *nc, bool is_le)
581 #if HOST_BIG_ENDIAN
582 if (!nc || !nc->info->set_vnet_le) {
583 return -ENOSYS;
586 return nc->info->set_vnet_le(nc, is_le);
587 #else
588 return 0;
589 #endif
592 int qemu_set_vnet_be(NetClientState *nc, bool is_be)
594 #if HOST_BIG_ENDIAN
595 return 0;
596 #else
597 if (!nc || !nc->info->set_vnet_be) {
598 return -ENOSYS;
601 return nc->info->set_vnet_be(nc, is_be);
602 #endif
605 int qemu_can_receive_packet(NetClientState *nc)
607 if (nc->receive_disabled) {
608 return 0;
609 } else if (nc->info->can_receive &&
610 !nc->info->can_receive(nc)) {
611 return 0;
613 return 1;
616 int qemu_can_send_packet(NetClientState *sender)
618 int vm_running = runstate_is_running();
620 if (!vm_running) {
621 return 0;
624 if (!sender->peer) {
625 return 1;
628 return qemu_can_receive_packet(sender->peer);
631 static ssize_t filter_receive_iov(NetClientState *nc,
632 NetFilterDirection direction,
633 NetClientState *sender,
634 unsigned flags,
635 const struct iovec *iov,
636 int iovcnt,
637 NetPacketSent *sent_cb)
639 ssize_t ret = 0;
640 NetFilterState *nf = NULL;
642 if (direction == NET_FILTER_DIRECTION_TX) {
643 QTAILQ_FOREACH(nf, &nc->filters, next) {
644 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
645 iovcnt, sent_cb);
646 if (ret) {
647 return ret;
650 } else {
651 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
652 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
653 iovcnt, sent_cb);
654 if (ret) {
655 return ret;
660 return ret;
663 static ssize_t filter_receive(NetClientState *nc,
664 NetFilterDirection direction,
665 NetClientState *sender,
666 unsigned flags,
667 const uint8_t *data,
668 size_t size,
669 NetPacketSent *sent_cb)
671 struct iovec iov = {
672 .iov_base = (void *)data,
673 .iov_len = size
676 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
679 void qemu_purge_queued_packets(NetClientState *nc)
681 if (!nc->peer) {
682 return;
685 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
688 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
690 nc->receive_disabled = 0;
692 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
693 if (net_hub_flush(nc->peer)) {
694 qemu_notify_event();
697 if (qemu_net_queue_flush(nc->incoming_queue)) {
698 /* We emptied the queue successfully, signal to the IO thread to repoll
699 * the file descriptor (for tap, for example).
701 qemu_notify_event();
702 } else if (purge) {
703 /* Unable to empty the queue, purge remaining packets */
704 qemu_net_queue_purge(nc->incoming_queue, nc->peer);
708 void qemu_flush_queued_packets(NetClientState *nc)
710 qemu_flush_or_purge_queued_packets(nc, false);
713 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
714 unsigned flags,
715 const uint8_t *buf, int size,
716 NetPacketSent *sent_cb)
718 NetQueue *queue;
719 int ret;
721 #ifdef DEBUG_NET
722 printf("qemu_send_packet_async:\n");
723 qemu_hexdump(stdout, "net", buf, size);
724 #endif
726 if (sender->link_down || !sender->peer) {
727 return size;
730 /* Let filters handle the packet first */
731 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
732 sender, flags, buf, size, sent_cb);
733 if (ret) {
734 return ret;
737 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
738 sender, flags, buf, size, sent_cb);
739 if (ret) {
740 return ret;
743 queue = sender->peer->incoming_queue;
745 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
748 ssize_t qemu_send_packet_async(NetClientState *sender,
749 const uint8_t *buf, int size,
750 NetPacketSent *sent_cb)
752 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
753 buf, size, sent_cb);
756 ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
758 return qemu_send_packet_async(nc, buf, size, NULL);
761 ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
763 if (!qemu_can_receive_packet(nc)) {
764 return 0;
767 return qemu_net_queue_receive(nc->incoming_queue, buf, size);
770 ssize_t qemu_receive_packet_iov(NetClientState *nc, const struct iovec *iov,
771 int iovcnt)
773 if (!qemu_can_receive_packet(nc)) {
774 return 0;
777 return qemu_net_queue_receive_iov(nc->incoming_queue, iov, iovcnt);
780 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
782 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
783 buf, size, NULL);
786 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
787 int iovcnt, unsigned flags)
789 uint8_t *buf = NULL;
790 uint8_t *buffer;
791 size_t offset;
792 ssize_t ret;
794 if (iovcnt == 1) {
795 buffer = iov[0].iov_base;
796 offset = iov[0].iov_len;
797 } else {
798 offset = iov_size(iov, iovcnt);
799 if (offset > NET_BUFSIZE) {
800 return -1;
802 buf = g_malloc(offset);
803 buffer = buf;
804 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
807 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
808 ret = nc->info->receive_raw(nc, buffer, offset);
809 } else {
810 ret = nc->info->receive(nc, buffer, offset);
813 g_free(buf);
814 return ret;
817 static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
818 unsigned flags,
819 const struct iovec *iov,
820 int iovcnt,
821 void *opaque)
823 MemReentrancyGuard *owned_reentrancy_guard;
824 NetClientState *nc = opaque;
825 int ret;
828 if (nc->link_down) {
829 return iov_size(iov, iovcnt);
832 if (nc->receive_disabled) {
833 return 0;
836 if (nc->info->type != NET_CLIENT_DRIVER_NIC ||
837 qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) {
838 owned_reentrancy_guard = NULL;
839 } else {
840 owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard;
841 owned_reentrancy_guard->engaged_in_io = true;
844 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
845 ret = nc->info->receive_iov(nc, iov, iovcnt);
846 } else {
847 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
850 if (owned_reentrancy_guard) {
851 owned_reentrancy_guard->engaged_in_io = false;
854 if (ret == 0) {
855 nc->receive_disabled = 1;
858 return ret;
861 ssize_t qemu_sendv_packet_async(NetClientState *sender,
862 const struct iovec *iov, int iovcnt,
863 NetPacketSent *sent_cb)
865 NetQueue *queue;
866 size_t size = iov_size(iov, iovcnt);
867 int ret;
869 if (size > NET_BUFSIZE) {
870 return size;
873 if (sender->link_down || !sender->peer) {
874 return size;
877 /* Let filters handle the packet first */
878 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
879 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
880 if (ret) {
881 return ret;
884 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
885 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
886 if (ret) {
887 return ret;
890 queue = sender->peer->incoming_queue;
892 return qemu_net_queue_send_iov(queue, sender,
893 QEMU_NET_PACKET_FLAG_NONE,
894 iov, iovcnt, sent_cb);
897 ssize_t
898 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
900 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
903 NetClientState *qemu_find_netdev(const char *id)
905 NetClientState *nc;
907 QTAILQ_FOREACH(nc, &net_clients, next) {
908 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
909 continue;
910 if (!strcmp(nc->name, id)) {
911 return nc;
915 return NULL;
918 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
919 NetClientDriver type, int max)
921 NetClientState *nc;
922 int ret = 0;
924 QTAILQ_FOREACH(nc, &net_clients, next) {
925 if (nc->info->type == type) {
926 continue;
928 if (!id || !strcmp(nc->name, id)) {
929 if (ret < max) {
930 ncs[ret] = nc;
932 ret++;
936 return ret;
939 static int nic_get_free_idx(void)
941 int index;
943 for (index = 0; index < MAX_NICS; index++)
944 if (!nd_table[index].used)
945 return index;
946 return -1;
949 GPtrArray *qemu_get_nic_models(const char *device_type)
951 GPtrArray *nic_models = g_ptr_array_new();
952 GSList *list = object_class_get_list_sorted(device_type, false);
954 while (list) {
955 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
956 TYPE_DEVICE);
957 GSList *next;
958 if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
959 dc->user_creatable) {
960 const char *name = object_class_get_name(list->data);
962 * A network device might also be something else than a NIC, see
963 * e.g. the "rocker" device. Thus we have to look for the "netdev"
964 * property, too. Unfortunately, some devices like virtio-net only
965 * create this property during instance_init, so we have to create
966 * a temporary instance here to be able to check it.
968 Object *obj = object_new_with_class(OBJECT_CLASS(dc));
969 if (object_property_find(obj, "netdev")) {
970 g_ptr_array_add(nic_models, (gpointer)name);
972 object_unref(obj);
974 next = list->next;
975 g_slist_free_1(list);
976 list = next;
978 g_ptr_array_add(nic_models, NULL);
980 return nic_models;
983 static int net_init_nic(const Netdev *netdev, const char *name,
984 NetClientState *peer, Error **errp)
986 int idx;
987 NICInfo *nd;
988 const NetLegacyNicOptions *nic;
990 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
991 nic = &netdev->u.nic;
993 idx = nic_get_free_idx();
994 if (idx == -1 || nb_nics >= MAX_NICS) {
995 error_setg(errp, "too many NICs");
996 return -1;
999 nd = &nd_table[idx];
1001 memset(nd, 0, sizeof(*nd));
1003 if (nic->netdev) {
1004 nd->netdev = qemu_find_netdev(nic->netdev);
1005 if (!nd->netdev) {
1006 error_setg(errp, "netdev '%s' not found", nic->netdev);
1007 return -1;
1009 } else {
1010 assert(peer);
1011 nd->netdev = peer;
1013 nd->name = g_strdup(name);
1014 if (nic->model) {
1015 nd->model = g_strdup(nic->model);
1017 if (nic->addr) {
1018 nd->devaddr = g_strdup(nic->addr);
1021 if (nic->macaddr &&
1022 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
1023 error_setg(errp, "invalid syntax for ethernet address");
1024 return -1;
1026 if (nic->macaddr &&
1027 is_multicast_ether_addr(nd->macaddr.a)) {
1028 error_setg(errp,
1029 "NIC cannot have multicast MAC address (odd 1st byte)");
1030 return -1;
1032 qemu_macaddr_default_if_unset(&nd->macaddr);
1034 if (nic->has_vectors) {
1035 if (nic->vectors > 0x7ffffff) {
1036 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
1037 return -1;
1039 nd->nvectors = nic->vectors;
1040 } else {
1041 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
1044 nd->used = 1;
1045 nb_nics++;
1047 return idx;
1050 static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data)
1052 GPtrArray *results = user_data;
1053 GPtrArray *alias_list = value;
1054 const char *model = key;
1055 char *result;
1057 if (!alias_list) {
1058 result = g_strdup(model);
1059 } else {
1060 GString *result_str = g_string_new(model);
1061 int i;
1063 g_string_append(result_str, " (aka ");
1064 for (i = 0; i < alias_list->len; i++) {
1065 if (i) {
1066 g_string_append(result_str, ", ");
1068 g_string_append(result_str, alias_list->pdata[i]);
1070 g_string_append(result_str, ")");
1071 result = result_str->str;
1072 g_string_free(result_str, false);
1073 g_ptr_array_unref(alias_list);
1075 g_ptr_array_add(results, result);
1076 return true;
1079 static int model_cmp(char **a, char **b)
1081 return strcmp(*a, *b);
1084 static void show_nic_models(void)
1086 GPtrArray *results = g_ptr_array_new();
1087 int i;
1089 g_hash_table_foreach_remove(nic_model_help, add_nic_result, results);
1090 g_ptr_array_sort(results, (GCompareFunc)model_cmp);
1092 printf("Available NIC models for this configuration:\n");
1093 for (i = 0 ; i < results->len; i++) {
1094 printf("%s\n", (char *)results->pdata[i]);
1096 g_hash_table_unref(nic_model_help);
1097 nic_model_help = NULL;
1100 static void add_nic_model_help(const char *model, const char *alias)
1102 GPtrArray *alias_list = NULL;
1104 if (g_hash_table_lookup_extended(nic_model_help, model, NULL,
1105 (gpointer *)&alias_list)) {
1106 /* Already exists, no alias to add: return */
1107 if (!alias) {
1108 return;
1110 if (alias_list) {
1111 /* Check if this alias is already in the list. Add if not. */
1112 if (!g_ptr_array_find_with_equal_func(alias_list, alias,
1113 g_str_equal, NULL)) {
1114 g_ptr_array_add(alias_list, g_strdup(alias));
1116 return;
1119 /* Either this model wasn't in the list already, or a first alias added */
1120 if (alias) {
1121 alias_list = g_ptr_array_new();
1122 g_ptr_array_set_free_func(alias_list, g_free);
1123 g_ptr_array_add(alias_list, g_strdup(alias));
1125 g_hash_table_replace(nic_model_help, g_strdup(model), alias_list);
1128 NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
1129 const char *alias)
1131 NICInfo *nd;
1132 int i;
1134 if (nic_model_help) {
1135 add_nic_model_help(typename, alias);
1138 for (i = 0; i < nb_nics; i++) {
1139 nd = &nd_table[i];
1141 if (!nd->used || nd->instantiated) {
1142 continue;
1145 if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename)
1146 || (alias && !g_strcmp0(nd->model, alias))) {
1147 return nd;
1150 return NULL;
1154 /* "I have created a device. Please configure it if you can" */
1155 bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
1156 const char *alias)
1158 NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)),
1159 match_default, alias);
1161 if (nd) {
1162 qdev_set_nic_properties(dev, nd);
1163 return true;
1165 return false;
1168 /* "Please create a device, if you have a configuration for it" */
1169 DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
1170 const char *alias)
1172 NICInfo *nd = qemu_find_nic_info(typename, match_default, alias);
1173 DeviceState *dev;
1175 if (!nd) {
1176 return NULL;
1179 dev = qdev_new(typename);
1180 qdev_set_nic_properties(dev, nd);
1181 return dev;
1184 void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
1185 const char *default_model,
1186 const char *alias, const char *alias_target)
1188 GPtrArray *nic_models = qemu_get_nic_models(parent_type);
1189 const char *model;
1190 DeviceState *dev;
1191 NICInfo *nd;
1192 int i;
1194 if (nic_model_help) {
1195 if (alias_target) {
1196 add_nic_model_help(alias_target, alias);
1198 for (i = 0; i < nic_models->len - 1; i++) {
1199 add_nic_model_help(nic_models->pdata[i], NULL);
1203 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1204 nic_models->len--;
1206 for (i = 0; i < nb_nics; i++) {
1207 nd = &nd_table[i];
1209 if (!nd->used || nd->instantiated) {
1210 continue;
1213 model = nd->model ? nd->model : default_model;
1214 if (!model) {
1215 continue;
1218 /* Each bus type is allowed *one* substitution */
1219 if (g_str_equal(model, alias)) {
1220 model = alias_target;
1223 if (!g_ptr_array_find_with_equal_func(nic_models, model,
1224 g_str_equal, NULL)) {
1225 /* This NIC does not live on this bus. */
1226 continue;
1229 dev = qdev_new(model);
1230 qdev_set_nic_properties(dev, nd);
1231 qdev_realize_and_unref(dev, bus, &error_fatal);
1234 g_ptr_array_free(nic_models, true);
1237 static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
1238 const Netdev *netdev,
1239 const char *name,
1240 NetClientState *peer, Error **errp) = {
1241 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
1242 #ifdef CONFIG_SLIRP
1243 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
1244 #endif
1245 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
1246 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
1247 [NET_CLIENT_DRIVER_STREAM] = net_init_stream,
1248 [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
1249 #ifdef CONFIG_VDE
1250 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
1251 #endif
1252 #ifdef CONFIG_NETMAP
1253 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
1254 #endif
1255 #ifdef CONFIG_AF_XDP
1256 [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp,
1257 #endif
1258 #ifdef CONFIG_NET_BRIDGE
1259 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
1260 #endif
1261 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
1262 #ifdef CONFIG_VHOST_NET_USER
1263 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
1264 #endif
1265 #ifdef CONFIG_VHOST_NET_VDPA
1266 [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
1267 #endif
1268 #ifdef CONFIG_L2TPV3
1269 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
1270 #endif
1271 #ifdef CONFIG_VMNET
1272 [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
1273 [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
1274 [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
1275 #endif /* CONFIG_VMNET */
1279 static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
1281 NetClientState *peer = NULL;
1282 NetClientState *nc;
1284 if (is_netdev) {
1285 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
1286 !net_client_init_fun[netdev->type]) {
1287 error_setg(errp, "network backend '%s' is not compiled into this binary",
1288 NetClientDriver_str(netdev->type));
1289 return -1;
1291 } else {
1292 if (netdev->type == NET_CLIENT_DRIVER_NONE) {
1293 return 0; /* nothing to do */
1295 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
1296 error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
1297 NetClientDriver_str(netdev->type));
1298 return -1;
1301 if (!net_client_init_fun[netdev->type]) {
1302 error_setg(errp, "network backend '%s' is not compiled into this binary",
1303 NetClientDriver_str(netdev->type));
1304 return -1;
1307 /* Do not add to a hub if it's a nic with a netdev= parameter. */
1308 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
1309 !netdev->u.nic.netdev) {
1310 peer = net_hub_add_port(0, NULL, NULL);
1314 nc = qemu_find_netdev(netdev->id);
1315 if (nc) {
1316 error_setg(errp, "Duplicate ID '%s'", netdev->id);
1317 return -1;
1320 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
1321 /* FIXME drop when all init functions store an Error */
1322 if (errp && !*errp) {
1323 error_setg(errp, "Device '%s' could not be initialized",
1324 NetClientDriver_str(netdev->type));
1326 return -1;
1329 if (is_netdev) {
1330 nc = qemu_find_netdev(netdev->id);
1331 assert(nc);
1332 nc->is_netdev = true;
1335 return 0;
1338 void show_netdevs(void)
1340 int idx;
1341 const char *available_netdevs[] = {
1342 "socket",
1343 "stream",
1344 "dgram",
1345 "hubport",
1346 "tap",
1347 #ifdef CONFIG_SLIRP
1348 "user",
1349 #endif
1350 #ifdef CONFIG_L2TPV3
1351 "l2tpv3",
1352 #endif
1353 #ifdef CONFIG_VDE
1354 "vde",
1355 #endif
1356 #ifdef CONFIG_NET_BRIDGE
1357 "bridge",
1358 #endif
1359 #ifdef CONFIG_NETMAP
1360 "netmap",
1361 #endif
1362 #ifdef CONFIG_AF_XDP
1363 "af-xdp",
1364 #endif
1365 #ifdef CONFIG_POSIX
1366 "vhost-user",
1367 #endif
1368 #ifdef CONFIG_VHOST_VDPA
1369 "vhost-vdpa",
1370 #endif
1371 #ifdef CONFIG_VMNET
1372 "vmnet-host",
1373 "vmnet-shared",
1374 "vmnet-bridged",
1375 #endif
1378 qemu_printf("Available netdev backend types:\n");
1379 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
1380 qemu_printf("%s\n", available_netdevs[idx]);
1384 static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
1386 gchar **substrings = NULL;
1387 Netdev *object = NULL;
1388 int ret = -1;
1389 Visitor *v = opts_visitor_new(opts);
1391 /* Parse convenience option format ipv6-net=fec0::0[/64] */
1392 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
1394 if (ip6_net) {
1395 char *prefix_addr;
1396 unsigned long prefix_len = 64; /* Default 64bit prefix length. */
1398 substrings = g_strsplit(ip6_net, "/", 2);
1399 if (!substrings || !substrings[0]) {
1400 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
1401 "a valid IPv6 prefix");
1402 goto out;
1405 prefix_addr = substrings[0];
1407 /* Handle user-specified prefix length. */
1408 if (substrings[1] &&
1409 qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
1411 error_setg(errp,
1412 "parameter 'ipv6-net' expects a number after '/'");
1413 goto out;
1416 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
1417 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
1418 &error_abort);
1419 qemu_opt_unset(opts, "ipv6-net");
1422 /* Create an ID for -net if the user did not specify one */
1423 if (!is_netdev && !qemu_opts_id(opts)) {
1424 qemu_opts_set_id(opts, id_generate(ID_NET));
1427 if (visit_type_Netdev(v, NULL, &object, errp)) {
1428 ret = net_client_init1(object, is_netdev, errp);
1431 qapi_free_Netdev(object);
1433 out:
1434 g_strfreev(substrings);
1435 visit_free(v);
1436 return ret;
1439 void netdev_add(QemuOpts *opts, Error **errp)
1441 net_client_init(opts, true, errp);
1444 void qmp_netdev_add(Netdev *netdev, Error **errp)
1446 if (!id_wellformed(netdev->id)) {
1447 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
1448 return;
1451 net_client_init1(netdev, true, errp);
1454 void qmp_netdev_del(const char *id, Error **errp)
1456 NetClientState *nc;
1457 QemuOpts *opts;
1459 nc = qemu_find_netdev(id);
1460 if (!nc) {
1461 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1462 "Device '%s' not found", id);
1463 return;
1466 if (!nc->is_netdev) {
1467 error_setg(errp, "Device '%s' is not a netdev", id);
1468 return;
1471 qemu_del_net_client(nc);
1474 * Wart: we need to delete the QemuOpts associated with netdevs
1475 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1476 * HMP netdev_add.
1478 opts = qemu_opts_find(qemu_find_opts("netdev"), id);
1479 if (opts) {
1480 qemu_opts_del(opts);
1484 static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1486 char *str;
1487 ObjectProperty *prop;
1488 ObjectPropertyIterator iter;
1489 Visitor *v;
1491 /* generate info str */
1492 object_property_iter_init(&iter, OBJECT(nf));
1493 while ((prop = object_property_iter_next(&iter))) {
1494 if (!strcmp(prop->name, "type")) {
1495 continue;
1497 v = string_output_visitor_new(false, &str);
1498 object_property_get(OBJECT(nf), prop->name, v, NULL);
1499 visit_complete(v, &str);
1500 visit_free(v);
1501 monitor_printf(mon, ",%s=%s", prop->name, str);
1502 g_free(str);
1504 monitor_printf(mon, "\n");
1507 void print_net_client(Monitor *mon, NetClientState *nc)
1509 NetFilterState *nf;
1511 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1512 nc->queue_index,
1513 NetClientDriver_str(nc->info->type),
1514 nc->info_str);
1515 if (!QTAILQ_EMPTY(&nc->filters)) {
1516 monitor_printf(mon, "filters:\n");
1518 QTAILQ_FOREACH(nf, &nc->filters, next) {
1519 monitor_printf(mon, " - %s: type=%s",
1520 object_get_canonical_path_component(OBJECT(nf)),
1521 object_get_typename(OBJECT(nf)));
1522 netfilter_print_info(mon, nf);
1526 RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
1528 NetClientState *nc;
1529 RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
1531 QTAILQ_FOREACH(nc, &net_clients, next) {
1532 RxFilterInfo *info;
1534 if (name && strcmp(nc->name, name) != 0) {
1535 continue;
1538 /* only query rx-filter information of NIC */
1539 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
1540 if (name) {
1541 error_setg(errp, "net client(%s) isn't a NIC", name);
1542 assert(!filter_list);
1543 return NULL;
1545 continue;
1548 /* only query information on queue 0 since the info is per nic,
1549 * not per queue
1551 if (nc->queue_index != 0)
1552 continue;
1554 if (nc->info->query_rx_filter) {
1555 info = nc->info->query_rx_filter(nc);
1556 QAPI_LIST_APPEND(tail, info);
1557 } else if (name) {
1558 error_setg(errp, "net client(%s) doesn't support"
1559 " rx-filter querying", name);
1560 assert(!filter_list);
1561 return NULL;
1564 if (name) {
1565 break;
1569 if (filter_list == NULL && name) {
1570 error_setg(errp, "invalid net client name: %s", name);
1573 return filter_list;
1576 void colo_notify_filters_event(int event, Error **errp)
1578 NetClientState *nc;
1579 NetFilterState *nf;
1580 NetFilterClass *nfc = NULL;
1581 Error *local_err = NULL;
1583 QTAILQ_FOREACH(nc, &net_clients, next) {
1584 QTAILQ_FOREACH(nf, &nc->filters, next) {
1585 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1586 nfc->handle_event(nf, event, &local_err);
1587 if (local_err) {
1588 error_propagate(errp, local_err);
1589 return;
1595 void qmp_set_link(const char *name, bool up, Error **errp)
1597 NetClientState *ncs[MAX_QUEUE_NUM];
1598 NetClientState *nc;
1599 int queues, i;
1601 queues = qemu_find_net_clients_except(name, ncs,
1602 NET_CLIENT_DRIVER__MAX,
1603 MAX_QUEUE_NUM);
1605 if (queues == 0) {
1606 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1607 "Device '%s' not found", name);
1608 return;
1610 nc = ncs[0];
1612 for (i = 0; i < queues; i++) {
1613 ncs[i]->link_down = !up;
1616 if (nc->info->link_status_changed) {
1617 nc->info->link_status_changed(nc);
1620 if (nc->peer) {
1621 /* Change peer link only if the peer is NIC and then notify peer.
1622 * If the peer is a HUBPORT or a backend, we do not change the
1623 * link status.
1625 * This behavior is compatible with qemu hubs where there could be
1626 * multiple clients that can still communicate with each other in
1627 * disconnected mode. For now maintain this compatibility.
1629 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
1630 for (i = 0; i < queues; i++) {
1631 ncs[i]->peer->link_down = !up;
1634 if (nc->peer->info->link_status_changed) {
1635 nc->peer->info->link_status_changed(nc->peer);
1640 static void net_vm_change_state_handler(void *opaque, bool running,
1641 RunState state)
1643 NetClientState *nc;
1644 NetClientState *tmp;
1646 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1647 if (running) {
1648 /* Flush queued packets and wake up backends. */
1649 if (nc->peer && qemu_can_send_packet(nc)) {
1650 qemu_flush_queued_packets(nc->peer);
1652 } else {
1653 /* Complete all queued packets, to guarantee we don't modify
1654 * state later when VM is not running.
1656 qemu_flush_or_purge_queued_packets(nc, true);
1661 void net_cleanup(void)
1663 NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients);
1665 /*cleanup colo compare module for COLO*/
1666 colo_compare_cleanup();
1669 * Walk the net_clients list and remove the netdevs but *not* any
1670 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1671 * model which created them, and in some cases (e.g. xen-net-device)
1672 * the device itself may do cleanup at exit and will be upset if we
1673 * just delete its NIC from underneath it.
1675 * Since qemu_del_net_client() may delete multiple entries, using
1676 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1677 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1678 * 'p' pointing to either the head of the list, or the 'next' field
1679 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1680 * the list.
1682 * The 'nc' variable isn't part of the list traversal; it's purely
1683 * for convenience as too much '(*p)->' has a tendency to make the
1684 * readers' eyes bleed.
1686 while (*p) {
1687 nc = *p;
1688 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
1689 /* Skip NET_CLIENT_DRIVER_NIC entries */
1690 p = &QTAILQ_NEXT(nc, next);
1691 } else {
1692 qemu_del_net_client(nc);
1696 qemu_del_vm_change_state_handler(net_change_state_entry);
1699 void net_check_clients(void)
1701 NetClientState *nc;
1702 int i;
1704 if (nic_model_help) {
1705 show_nic_models();
1706 exit(0);
1708 net_hub_check_clients();
1710 QTAILQ_FOREACH(nc, &net_clients, next) {
1711 if (!nc->peer) {
1712 warn_report("%s %s has no peer",
1713 nc->info->type == NET_CLIENT_DRIVER_NIC
1714 ? "nic" : "netdev",
1715 nc->name);
1719 /* Check that all NICs requested via -net nic actually got created.
1720 * NICs created via -device don't need to be checked here because
1721 * they are always instantiated.
1723 for (i = 0; i < MAX_NICS; i++) {
1724 NICInfo *nd = &nd_table[i];
1725 if (nd->used && !nd->instantiated) {
1726 warn_report("requested NIC (%s, model %s) "
1727 "was not created (not supported by this machine?)",
1728 nd->name ? nd->name : "anonymous",
1729 nd->model ? nd->model : "unspecified");
1734 static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
1736 return net_client_init(opts, false, errp);
1739 static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
1741 const char *type = qemu_opt_get(opts, "type");
1743 if (type && is_help_option(type)) {
1744 show_netdevs();
1745 exit(0);
1747 return net_client_init(opts, true, errp);
1750 /* For the convenience "--nic" parameter */
1751 static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1753 char *mac, *nd_id;
1754 int idx, ret;
1755 NICInfo *ni;
1756 const char *type;
1758 type = qemu_opt_get(opts, "type");
1759 if (type) {
1760 if (g_str_equal(type, "none")) {
1761 return 0; /* Nothing to do, default_net is cleared in vl.c */
1763 if (is_help_option(type)) {
1764 GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
1765 int i;
1766 show_netdevs();
1767 printf("\n");
1768 printf("Available NIC models "
1769 "(use -nic model=help for a filtered list):\n");
1770 for (i = 0 ; nic_models->pdata[i]; i++) {
1771 printf("%s\n", (char *)nic_models->pdata[i]);
1773 g_ptr_array_free(nic_models, true);
1774 exit(0);
1778 idx = nic_get_free_idx();
1779 if (idx == -1 || nb_nics >= MAX_NICS) {
1780 error_setg(errp, "no more on-board/default NIC slots available");
1781 return -1;
1784 if (!type) {
1785 qemu_opt_set(opts, "type", "user", &error_abort);
1788 ni = &nd_table[idx];
1789 memset(ni, 0, sizeof(*ni));
1790 ni->model = qemu_opt_get_del(opts, "model");
1792 if (!nic_model_help && !g_strcmp0(ni->model, "help")) {
1793 nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal,
1794 g_free, NULL);
1795 return 0;
1798 /* Create an ID if the user did not specify one */
1799 nd_id = g_strdup(qemu_opts_id(opts));
1800 if (!nd_id) {
1801 nd_id = id_generate(ID_NET);
1802 qemu_opts_set_id(opts, nd_id);
1805 /* Handle MAC address */
1806 mac = qemu_opt_get_del(opts, "mac");
1807 if (mac) {
1808 ret = net_parse_macaddr(ni->macaddr.a, mac);
1809 g_free(mac);
1810 if (ret) {
1811 error_setg(errp, "invalid syntax for ethernet address");
1812 goto out;
1814 if (is_multicast_ether_addr(ni->macaddr.a)) {
1815 error_setg(errp, "NIC cannot have multicast MAC address");
1816 ret = -1;
1817 goto out;
1820 qemu_macaddr_default_if_unset(&ni->macaddr);
1822 ret = net_client_init(opts, true, errp);
1823 if (ret == 0) {
1824 ni->netdev = qemu_find_netdev(nd_id);
1825 ni->used = true;
1826 nb_nics++;
1829 out:
1830 g_free(nd_id);
1831 return ret;
1834 static void netdev_init_modern(void)
1836 while (!QSIMPLEQ_EMPTY(&nd_queue)) {
1837 NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
1839 QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
1840 loc_push_restore(&nd->loc);
1841 net_client_init1(nd->nd, true, &error_fatal);
1842 loc_pop(&nd->loc);
1843 qapi_free_Netdev(nd->nd);
1844 g_free(nd);
1848 void net_init_clients(void)
1850 net_change_state_entry =
1851 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1853 QTAILQ_INIT(&net_clients);
1855 netdev_init_modern();
1857 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
1858 &error_fatal);
1860 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
1861 &error_fatal);
1863 qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
1864 &error_fatal);
1868 * Does this -netdev argument use modern rather than traditional syntax?
1869 * Modern syntax is to be parsed with netdev_parse_modern().
1870 * Traditional syntax is to be parsed with net_client_parse().
1872 bool netdev_is_modern(const char *optstr)
1874 QemuOpts *opts;
1875 bool is_modern;
1876 const char *type;
1877 static QemuOptsList dummy_opts = {
1878 .name = "netdev",
1879 .implied_opt_name = "type",
1880 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
1881 .desc = { { } },
1884 if (optstr[0] == '{') {
1885 /* This is JSON, which means it's modern syntax */
1886 return true;
1889 opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
1890 qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name,
1891 &error_abort);
1892 type = qemu_opt_get(opts, "type");
1893 is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
1895 qemu_opts_reset(&dummy_opts);
1897 return is_modern;
1901 * netdev_parse_modern() uses modern, more expressive syntax than
1902 * net_client_parse(), but supports only the -netdev option.
1903 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1904 * appends to @qemu_netdev_opts.
1906 void netdev_parse_modern(const char *optstr)
1908 Visitor *v;
1909 NetdevQueueEntry *nd;
1911 v = qobject_input_visitor_new_str(optstr, "type", &error_fatal);
1912 nd = g_new(NetdevQueueEntry, 1);
1913 visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
1914 visit_free(v);
1915 loc_save(&nd->loc);
1917 QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
1920 void net_client_parse(QemuOptsList *opts_list, const char *optstr)
1922 if (!qemu_opts_parse_noisily(opts_list, optstr, true)) {
1923 exit(1);
1927 /* From FreeBSD */
1928 /* XXX: optimize */
1929 uint32_t net_crc32(const uint8_t *p, int len)
1931 uint32_t crc;
1932 int carry, i, j;
1933 uint8_t b;
1935 crc = 0xffffffff;
1936 for (i = 0; i < len; i++) {
1937 b = *p++;
1938 for (j = 0; j < 8; j++) {
1939 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1940 crc <<= 1;
1941 b >>= 1;
1942 if (carry) {
1943 crc = ((crc ^ POLYNOMIAL_BE) | carry);
1948 return crc;
1951 uint32_t net_crc32_le(const uint8_t *p, int len)
1953 uint32_t crc;
1954 int carry, i, j;
1955 uint8_t b;
1957 crc = 0xffffffff;
1958 for (i = 0; i < len; i++) {
1959 b = *p++;
1960 for (j = 0; j < 8; j++) {
1961 carry = (crc & 0x1) ^ (b & 0x01);
1962 crc >>= 1;
1963 b >>= 1;
1964 if (carry) {
1965 crc ^= POLYNOMIAL_LE;
1970 return crc;
1973 QemuOptsList qemu_netdev_opts = {
1974 .name = "netdev",
1975 .implied_opt_name = "type",
1976 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1977 .desc = {
1979 * no elements => accept any params
1980 * validation will happen later
1982 { /* end of list */ }
1986 QemuOptsList qemu_nic_opts = {
1987 .name = "nic",
1988 .implied_opt_name = "type",
1989 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
1990 .desc = {
1992 * no elements => accept any params
1993 * validation will happen later
1995 { /* end of list */ }
1999 QemuOptsList qemu_net_opts = {
2000 .name = "net",
2001 .implied_opt_name = "type",
2002 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
2003 .desc = {
2005 * no elements => accept any params
2006 * validation will happen later
2008 { /* end of list */ }
2012 void net_socket_rs_init(SocketReadState *rs,
2013 SocketReadStateFinalize *finalize,
2014 bool vnet_hdr)
2016 rs->state = 0;
2017 rs->vnet_hdr = vnet_hdr;
2018 rs->index = 0;
2019 rs->packet_len = 0;
2020 rs->vnet_hdr_len = 0;
2021 memset(rs->buf, 0, sizeof(rs->buf));
2022 rs->finalize = finalize;
2026 * Returns
2027 * 0: success
2028 * -1: error occurs
2030 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
2032 unsigned int l;
2034 while (size > 0) {
2035 /* Reassemble a packet from the network.
2036 * 0 = getting length.
2037 * 1 = getting vnet header length.
2038 * 2 = getting data.
2040 switch (rs->state) {
2041 case 0:
2042 l = 4 - rs->index;
2043 if (l > size) {
2044 l = size;
2046 memcpy(rs->buf + rs->index, buf, l);
2047 buf += l;
2048 size -= l;
2049 rs->index += l;
2050 if (rs->index == 4) {
2051 /* got length */
2052 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
2053 rs->index = 0;
2054 if (rs->vnet_hdr) {
2055 rs->state = 1;
2056 } else {
2057 rs->state = 2;
2058 rs->vnet_hdr_len = 0;
2061 break;
2062 case 1:
2063 l = 4 - rs->index;
2064 if (l > size) {
2065 l = size;
2067 memcpy(rs->buf + rs->index, buf, l);
2068 buf += l;
2069 size -= l;
2070 rs->index += l;
2071 if (rs->index == 4) {
2072 /* got vnet header length */
2073 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
2074 rs->index = 0;
2075 rs->state = 2;
2077 break;
2078 case 2:
2079 l = rs->packet_len - rs->index;
2080 if (l > size) {
2081 l = size;
2083 if (rs->index + l <= sizeof(rs->buf)) {
2084 memcpy(rs->buf + rs->index, buf, l);
2085 } else {
2086 fprintf(stderr, "serious error: oversized packet received,"
2087 "connection terminated.\n");
2088 rs->index = rs->state = 0;
2089 return -1;
2092 rs->index += l;
2093 buf += l;
2094 size -= l;
2095 if (rs->index >= rs->packet_len) {
2096 rs->index = 0;
2097 rs->state = 0;
2098 assert(rs->finalize);
2099 rs->finalize(rs);
2101 break;
2105 assert(size == 0);
2106 return 0;