]> git.proxmox.com Git - mirror_qemu.git/blob - net/net.c
net: Remove the deprecated way of dumping network packets
[mirror_qemu.git] / net / net.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "qemu/osdep.h"
26
27 #include "net/net.h"
28 #include "clients.h"
29 #include "hub.h"
30 #include "net/slirp.h"
31 #include "net/eth.h"
32 #include "util.h"
33
34 #include "monitor/monitor.h"
35 #include "qemu/help_option.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qerror.h"
38 #include "qemu/error-report.h"
39 #include "qemu/sockets.h"
40 #include "qemu/cutils.h"
41 #include "qemu/config-file.h"
42 #include "qmp-commands.h"
43 #include "hw/qdev.h"
44 #include "qemu/iov.h"
45 #include "qemu/main-loop.h"
46 #include "qemu/option.h"
47 #include "qapi-visit.h"
48 #include "qapi/error.h"
49 #include "qapi/opts-visitor.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/qtest.h"
52 #include "net/filter.h"
53 #include "qapi/string-output-visitor.h"
54
55 /* Net bridge is currently not supported for W32. */
56 #if !defined(_WIN32)
57 # define CONFIG_NET_BRIDGE
58 #endif
59
60 static VMChangeStateEntry *net_change_state_entry;
61 static QTAILQ_HEAD(, NetClientState) net_clients;
62
63 const char *host_net_devices[] = {
64 "tap",
65 "socket",
66 #ifdef CONFIG_NET_BRIDGE
67 "bridge",
68 #endif
69 #ifdef CONFIG_NETMAP
70 "netmap",
71 #endif
72 #ifdef CONFIG_SLIRP
73 "user",
74 #endif
75 #ifdef CONFIG_VDE
76 "vde",
77 #endif
78 "vhost-user",
79 NULL,
80 };
81
82 /***********************************************************/
83 /* network device redirectors */
84
85 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
86 {
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';
100 }
101 *pp = p1;
102 return 0;
103 }
104
105 int parse_host_port(struct sockaddr_in *saddr, const char *str,
106 Error **errp)
107 {
108 char buf[512];
109 struct hostent *he;
110 const char *p, *r;
111 int port;
112
113 p = str;
114 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
115 error_setg(errp, "host address '%s' doesn't contain ':' "
116 "separating host from port", str);
117 return -1;
118 }
119 saddr->sin_family = AF_INET;
120 if (buf[0] == '\0') {
121 saddr->sin_addr.s_addr = 0;
122 } else {
123 if (qemu_isdigit(buf[0])) {
124 if (!inet_aton(buf, &saddr->sin_addr)) {
125 error_setg(errp, "host address '%s' is not a valid "
126 "IPv4 address", buf);
127 return -1;
128 }
129 } else {
130 he = gethostbyname(buf);
131 if (he == NULL) {
132 error_setg(errp, "can't resolve host address '%s'", buf);
133 return - 1;
134 }
135 saddr->sin_addr = *(struct in_addr *)he->h_addr;
136 }
137 }
138 port = strtol(p, (char **)&r, 0);
139 if (r == p) {
140 error_setg(errp, "port number '%s' is invalid", p);
141 return -1;
142 }
143 saddr->sin_port = htons(port);
144 return 0;
145 }
146
147 char *qemu_mac_strdup_printf(const uint8_t *macaddr)
148 {
149 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
150 macaddr[0], macaddr[1], macaddr[2],
151 macaddr[3], macaddr[4], macaddr[5]);
152 }
153
154 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
155 {
156 snprintf(nc->info_str, sizeof(nc->info_str),
157 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
158 nc->model,
159 macaddr[0], macaddr[1], macaddr[2],
160 macaddr[3], macaddr[4], macaddr[5]);
161 }
162
163 static int mac_table[256] = {0};
164
165 static void qemu_macaddr_set_used(MACAddr *macaddr)
166 {
167 int index;
168
169 for (index = 0x56; index < 0xFF; index++) {
170 if (macaddr->a[5] == index) {
171 mac_table[index]++;
172 }
173 }
174 }
175
176 static void qemu_macaddr_set_free(MACAddr *macaddr)
177 {
178 int index;
179 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
180
181 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
182 return;
183 }
184 for (index = 0x56; index < 0xFF; index++) {
185 if (macaddr->a[5] == index) {
186 mac_table[index]--;
187 }
188 }
189 }
190
191 static int qemu_macaddr_get_free(void)
192 {
193 int index;
194
195 for (index = 0x56; index < 0xFF; index++) {
196 if (mac_table[index] == 0) {
197 return index;
198 }
199 }
200
201 return -1;
202 }
203
204 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
205 {
206 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
207 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
208
209 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
210 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
211 return;
212 } else {
213 qemu_macaddr_set_used(macaddr);
214 return;
215 }
216 }
217
218 macaddr->a[0] = 0x52;
219 macaddr->a[1] = 0x54;
220 macaddr->a[2] = 0x00;
221 macaddr->a[3] = 0x12;
222 macaddr->a[4] = 0x34;
223 macaddr->a[5] = qemu_macaddr_get_free();
224 qemu_macaddr_set_used(macaddr);
225 }
226
227 /**
228 * Generate a name for net client
229 *
230 * Only net clients created with the legacy -net option and NICs need this.
231 */
232 static char *assign_name(NetClientState *nc1, const char *model)
233 {
234 NetClientState *nc;
235 int id = 0;
236
237 QTAILQ_FOREACH(nc, &net_clients, next) {
238 if (nc == nc1) {
239 continue;
240 }
241 if (strcmp(nc->model, model) == 0) {
242 id++;
243 }
244 }
245
246 return g_strdup_printf("%s.%d", model, id);
247 }
248
249 static void qemu_net_client_destructor(NetClientState *nc)
250 {
251 g_free(nc);
252 }
253
254 static void qemu_net_client_setup(NetClientState *nc,
255 NetClientInfo *info,
256 NetClientState *peer,
257 const char *model,
258 const char *name,
259 NetClientDestructor *destructor)
260 {
261 nc->info = info;
262 nc->model = g_strdup(model);
263 if (name) {
264 nc->name = g_strdup(name);
265 } else {
266 nc->name = assign_name(nc, model);
267 }
268
269 if (peer) {
270 assert(!peer->peer);
271 nc->peer = peer;
272 peer->peer = nc;
273 }
274 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
275
276 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
277 nc->destructor = destructor;
278 QTAILQ_INIT(&nc->filters);
279 }
280
281 NetClientState *qemu_new_net_client(NetClientInfo *info,
282 NetClientState *peer,
283 const char *model,
284 const char *name)
285 {
286 NetClientState *nc;
287
288 assert(info->size >= sizeof(NetClientState));
289
290 nc = g_malloc0(info->size);
291 qemu_net_client_setup(nc, info, peer, model, name,
292 qemu_net_client_destructor);
293
294 return nc;
295 }
296
297 NICState *qemu_new_nic(NetClientInfo *info,
298 NICConf *conf,
299 const char *model,
300 const char *name,
301 void *opaque)
302 {
303 NetClientState **peers = conf->peers.ncs;
304 NICState *nic;
305 int i, queues = MAX(1, conf->peers.queues);
306
307 assert(info->type == NET_CLIENT_DRIVER_NIC);
308 assert(info->size >= sizeof(NICState));
309
310 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
311 nic->ncs = (void *)nic + info->size;
312 nic->conf = conf;
313 nic->opaque = opaque;
314
315 for (i = 0; i < queues; i++) {
316 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
317 NULL);
318 nic->ncs[i].queue_index = i;
319 }
320
321 return nic;
322 }
323
324 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
325 {
326 return nic->ncs + queue_index;
327 }
328
329 NetClientState *qemu_get_queue(NICState *nic)
330 {
331 return qemu_get_subqueue(nic, 0);
332 }
333
334 NICState *qemu_get_nic(NetClientState *nc)
335 {
336 NetClientState *nc0 = nc - nc->queue_index;
337
338 return (NICState *)((void *)nc0 - nc->info->size);
339 }
340
341 void *qemu_get_nic_opaque(NetClientState *nc)
342 {
343 NICState *nic = qemu_get_nic(nc);
344
345 return nic->opaque;
346 }
347
348 static void qemu_cleanup_net_client(NetClientState *nc)
349 {
350 QTAILQ_REMOVE(&net_clients, nc, next);
351
352 if (nc->info->cleanup) {
353 nc->info->cleanup(nc);
354 }
355 }
356
357 static void qemu_free_net_client(NetClientState *nc)
358 {
359 if (nc->incoming_queue) {
360 qemu_del_net_queue(nc->incoming_queue);
361 }
362 if (nc->peer) {
363 nc->peer->peer = NULL;
364 }
365 g_free(nc->name);
366 g_free(nc->model);
367 if (nc->destructor) {
368 nc->destructor(nc);
369 }
370 }
371
372 void qemu_del_net_client(NetClientState *nc)
373 {
374 NetClientState *ncs[MAX_QUEUE_NUM];
375 int queues, i;
376 NetFilterState *nf, *next;
377
378 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
379
380 /* If the NetClientState belongs to a multiqueue backend, we will change all
381 * other NetClientStates also.
382 */
383 queues = qemu_find_net_clients_except(nc->name, ncs,
384 NET_CLIENT_DRIVER_NIC,
385 MAX_QUEUE_NUM);
386 assert(queues != 0);
387
388 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
389 object_unparent(OBJECT(nf));
390 }
391
392 /* If there is a peer NIC, delete and cleanup client, but do not free. */
393 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
394 NICState *nic = qemu_get_nic(nc->peer);
395 if (nic->peer_deleted) {
396 return;
397 }
398 nic->peer_deleted = true;
399
400 for (i = 0; i < queues; i++) {
401 ncs[i]->peer->link_down = true;
402 }
403
404 if (nc->peer->info->link_status_changed) {
405 nc->peer->info->link_status_changed(nc->peer);
406 }
407
408 for (i = 0; i < queues; i++) {
409 qemu_cleanup_net_client(ncs[i]);
410 }
411
412 return;
413 }
414
415 for (i = 0; i < queues; i++) {
416 qemu_cleanup_net_client(ncs[i]);
417 qemu_free_net_client(ncs[i]);
418 }
419 }
420
421 void qemu_del_nic(NICState *nic)
422 {
423 int i, queues = MAX(nic->conf->peers.queues, 1);
424
425 qemu_macaddr_set_free(&nic->conf->macaddr);
426
427 /* If this is a peer NIC and peer has already been deleted, free it now. */
428 if (nic->peer_deleted) {
429 for (i = 0; i < queues; i++) {
430 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
431 }
432 }
433
434 for (i = queues - 1; i >= 0; i--) {
435 NetClientState *nc = qemu_get_subqueue(nic, i);
436
437 qemu_cleanup_net_client(nc);
438 qemu_free_net_client(nc);
439 }
440
441 g_free(nic);
442 }
443
444 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
445 {
446 NetClientState *nc;
447
448 QTAILQ_FOREACH(nc, &net_clients, next) {
449 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
450 if (nc->queue_index == 0) {
451 func(qemu_get_nic(nc), opaque);
452 }
453 }
454 }
455 }
456
457 bool qemu_has_ufo(NetClientState *nc)
458 {
459 if (!nc || !nc->info->has_ufo) {
460 return false;
461 }
462
463 return nc->info->has_ufo(nc);
464 }
465
466 bool qemu_has_vnet_hdr(NetClientState *nc)
467 {
468 if (!nc || !nc->info->has_vnet_hdr) {
469 return false;
470 }
471
472 return nc->info->has_vnet_hdr(nc);
473 }
474
475 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
476 {
477 if (!nc || !nc->info->has_vnet_hdr_len) {
478 return false;
479 }
480
481 return nc->info->has_vnet_hdr_len(nc, len);
482 }
483
484 void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
485 {
486 if (!nc || !nc->info->using_vnet_hdr) {
487 return;
488 }
489
490 nc->info->using_vnet_hdr(nc, enable);
491 }
492
493 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
494 int ecn, int ufo)
495 {
496 if (!nc || !nc->info->set_offload) {
497 return;
498 }
499
500 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
501 }
502
503 void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
504 {
505 if (!nc || !nc->info->set_vnet_hdr_len) {
506 return;
507 }
508
509 nc->vnet_hdr_len = len;
510 nc->info->set_vnet_hdr_len(nc, len);
511 }
512
513 int qemu_set_vnet_le(NetClientState *nc, bool is_le)
514 {
515 #ifdef HOST_WORDS_BIGENDIAN
516 if (!nc || !nc->info->set_vnet_le) {
517 return -ENOSYS;
518 }
519
520 return nc->info->set_vnet_le(nc, is_le);
521 #else
522 return 0;
523 #endif
524 }
525
526 int qemu_set_vnet_be(NetClientState *nc, bool is_be)
527 {
528 #ifdef HOST_WORDS_BIGENDIAN
529 return 0;
530 #else
531 if (!nc || !nc->info->set_vnet_be) {
532 return -ENOSYS;
533 }
534
535 return nc->info->set_vnet_be(nc, is_be);
536 #endif
537 }
538
539 int qemu_can_send_packet(NetClientState *sender)
540 {
541 int vm_running = runstate_is_running();
542
543 if (!vm_running) {
544 return 0;
545 }
546
547 if (!sender->peer) {
548 return 1;
549 }
550
551 if (sender->peer->receive_disabled) {
552 return 0;
553 } else if (sender->peer->info->can_receive &&
554 !sender->peer->info->can_receive(sender->peer)) {
555 return 0;
556 }
557 return 1;
558 }
559
560 static ssize_t filter_receive_iov(NetClientState *nc,
561 NetFilterDirection direction,
562 NetClientState *sender,
563 unsigned flags,
564 const struct iovec *iov,
565 int iovcnt,
566 NetPacketSent *sent_cb)
567 {
568 ssize_t ret = 0;
569 NetFilterState *nf = NULL;
570
571 if (direction == NET_FILTER_DIRECTION_TX) {
572 QTAILQ_FOREACH(nf, &nc->filters, next) {
573 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
574 iovcnt, sent_cb);
575 if (ret) {
576 return ret;
577 }
578 }
579 } else {
580 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, NetFilterHead, next) {
581 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
582 iovcnt, sent_cb);
583 if (ret) {
584 return ret;
585 }
586 }
587 }
588
589 return ret;
590 }
591
592 static ssize_t filter_receive(NetClientState *nc,
593 NetFilterDirection direction,
594 NetClientState *sender,
595 unsigned flags,
596 const uint8_t *data,
597 size_t size,
598 NetPacketSent *sent_cb)
599 {
600 struct iovec iov = {
601 .iov_base = (void *)data,
602 .iov_len = size
603 };
604
605 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
606 }
607
608 void qemu_purge_queued_packets(NetClientState *nc)
609 {
610 if (!nc->peer) {
611 return;
612 }
613
614 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
615 }
616
617 static
618 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
619 {
620 nc->receive_disabled = 0;
621
622 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
623 if (net_hub_flush(nc->peer)) {
624 qemu_notify_event();
625 }
626 }
627 if (qemu_net_queue_flush(nc->incoming_queue)) {
628 /* We emptied the queue successfully, signal to the IO thread to repoll
629 * the file descriptor (for tap, for example).
630 */
631 qemu_notify_event();
632 } else if (purge) {
633 /* Unable to empty the queue, purge remaining packets */
634 qemu_net_queue_purge(nc->incoming_queue, nc);
635 }
636 }
637
638 void qemu_flush_queued_packets(NetClientState *nc)
639 {
640 qemu_flush_or_purge_queued_packets(nc, false);
641 }
642
643 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
644 unsigned flags,
645 const uint8_t *buf, int size,
646 NetPacketSent *sent_cb)
647 {
648 NetQueue *queue;
649 int ret;
650
651 #ifdef DEBUG_NET
652 printf("qemu_send_packet_async:\n");
653 qemu_hexdump((const char *)buf, stdout, "net", size);
654 #endif
655
656 if (sender->link_down || !sender->peer) {
657 return size;
658 }
659
660 /* Let filters handle the packet first */
661 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
662 sender, flags, buf, size, sent_cb);
663 if (ret) {
664 return ret;
665 }
666
667 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
668 sender, flags, buf, size, sent_cb);
669 if (ret) {
670 return ret;
671 }
672
673 queue = sender->peer->incoming_queue;
674
675 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
676 }
677
678 ssize_t qemu_send_packet_async(NetClientState *sender,
679 const uint8_t *buf, int size,
680 NetPacketSent *sent_cb)
681 {
682 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
683 buf, size, sent_cb);
684 }
685
686 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
687 {
688 qemu_send_packet_async(nc, buf, size, NULL);
689 }
690
691 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
692 {
693 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
694 buf, size, NULL);
695 }
696
697 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
698 int iovcnt, unsigned flags)
699 {
700 uint8_t *buf = NULL;
701 uint8_t *buffer;
702 size_t offset;
703 ssize_t ret;
704
705 if (iovcnt == 1) {
706 buffer = iov[0].iov_base;
707 offset = iov[0].iov_len;
708 } else {
709 offset = iov_size(iov, iovcnt);
710 if (offset > NET_BUFSIZE) {
711 return -1;
712 }
713 buf = g_malloc(offset);
714 buffer = buf;
715 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
716 }
717
718 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
719 ret = nc->info->receive_raw(nc, buffer, offset);
720 } else {
721 ret = nc->info->receive(nc, buffer, offset);
722 }
723
724 g_free(buf);
725 return ret;
726 }
727
728 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
729 unsigned flags,
730 const struct iovec *iov,
731 int iovcnt,
732 void *opaque)
733 {
734 NetClientState *nc = opaque;
735 int ret;
736
737 if (nc->link_down) {
738 return iov_size(iov, iovcnt);
739 }
740
741 if (nc->receive_disabled) {
742 return 0;
743 }
744
745 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
746 ret = nc->info->receive_iov(nc, iov, iovcnt);
747 } else {
748 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
749 }
750
751 if (ret == 0) {
752 nc->receive_disabled = 1;
753 }
754
755 return ret;
756 }
757
758 ssize_t qemu_sendv_packet_async(NetClientState *sender,
759 const struct iovec *iov, int iovcnt,
760 NetPacketSent *sent_cb)
761 {
762 NetQueue *queue;
763 int ret;
764
765 if (sender->link_down || !sender->peer) {
766 return iov_size(iov, iovcnt);
767 }
768
769 /* Let filters handle the packet first */
770 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
771 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
772 if (ret) {
773 return ret;
774 }
775
776 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
777 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
778 if (ret) {
779 return ret;
780 }
781
782 queue = sender->peer->incoming_queue;
783
784 return qemu_net_queue_send_iov(queue, sender,
785 QEMU_NET_PACKET_FLAG_NONE,
786 iov, iovcnt, sent_cb);
787 }
788
789 ssize_t
790 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
791 {
792 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
793 }
794
795 NetClientState *qemu_find_netdev(const char *id)
796 {
797 NetClientState *nc;
798
799 QTAILQ_FOREACH(nc, &net_clients, next) {
800 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
801 continue;
802 if (!strcmp(nc->name, id)) {
803 return nc;
804 }
805 }
806
807 return NULL;
808 }
809
810 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
811 NetClientDriver type, int max)
812 {
813 NetClientState *nc;
814 int ret = 0;
815
816 QTAILQ_FOREACH(nc, &net_clients, next) {
817 if (nc->info->type == type) {
818 continue;
819 }
820 if (!id || !strcmp(nc->name, id)) {
821 if (ret < max) {
822 ncs[ret] = nc;
823 }
824 ret++;
825 }
826 }
827
828 return ret;
829 }
830
831 static int nic_get_free_idx(void)
832 {
833 int index;
834
835 for (index = 0; index < MAX_NICS; index++)
836 if (!nd_table[index].used)
837 return index;
838 return -1;
839 }
840
841 int qemu_show_nic_models(const char *arg, const char *const *models)
842 {
843 int i;
844
845 if (!arg || !is_help_option(arg)) {
846 return 0;
847 }
848
849 fprintf(stderr, "qemu: Supported NIC models: ");
850 for (i = 0 ; models[i]; i++)
851 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
852 return 1;
853 }
854
855 void qemu_check_nic_model(NICInfo *nd, const char *model)
856 {
857 const char *models[2];
858
859 models[0] = model;
860 models[1] = NULL;
861
862 if (qemu_show_nic_models(nd->model, models))
863 exit(0);
864 if (qemu_find_nic_model(nd, models, model) < 0)
865 exit(1);
866 }
867
868 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
869 const char *default_model)
870 {
871 int i;
872
873 if (!nd->model)
874 nd->model = g_strdup(default_model);
875
876 for (i = 0 ; models[i]; i++) {
877 if (strcmp(nd->model, models[i]) == 0)
878 return i;
879 }
880
881 error_report("Unsupported NIC model: %s", nd->model);
882 return -1;
883 }
884
885 static int net_init_nic(const Netdev *netdev, const char *name,
886 NetClientState *peer, Error **errp)
887 {
888 int idx;
889 NICInfo *nd;
890 const NetLegacyNicOptions *nic;
891
892 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
893 nic = &netdev->u.nic;
894
895 idx = nic_get_free_idx();
896 if (idx == -1 || nb_nics >= MAX_NICS) {
897 error_setg(errp, "too many NICs");
898 return -1;
899 }
900
901 nd = &nd_table[idx];
902
903 memset(nd, 0, sizeof(*nd));
904
905 if (nic->has_netdev) {
906 nd->netdev = qemu_find_netdev(nic->netdev);
907 if (!nd->netdev) {
908 error_setg(errp, "netdev '%s' not found", nic->netdev);
909 return -1;
910 }
911 } else {
912 assert(peer);
913 nd->netdev = peer;
914 }
915 nd->name = g_strdup(name);
916 if (nic->has_model) {
917 nd->model = g_strdup(nic->model);
918 }
919 if (nic->has_addr) {
920 nd->devaddr = g_strdup(nic->addr);
921 }
922
923 if (nic->has_macaddr &&
924 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
925 error_setg(errp, "invalid syntax for ethernet address");
926 return -1;
927 }
928 if (nic->has_macaddr &&
929 is_multicast_ether_addr(nd->macaddr.a)) {
930 error_setg(errp,
931 "NIC cannot have multicast MAC address (odd 1st byte)");
932 return -1;
933 }
934 qemu_macaddr_default_if_unset(&nd->macaddr);
935
936 if (nic->has_vectors) {
937 if (nic->vectors > 0x7ffffff) {
938 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
939 return -1;
940 }
941 nd->nvectors = nic->vectors;
942 } else {
943 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
944 }
945
946 nd->used = 1;
947 nb_nics++;
948
949 return idx;
950 }
951
952
953 static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
954 const Netdev *netdev,
955 const char *name,
956 NetClientState *peer, Error **errp) = {
957 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
958 #ifdef CONFIG_SLIRP
959 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
960 #endif
961 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
962 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
963 #ifdef CONFIG_VDE
964 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
965 #endif
966 #ifdef CONFIG_NETMAP
967 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
968 #endif
969 #ifdef CONFIG_NET_BRIDGE
970 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
971 #endif
972 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
973 #ifdef CONFIG_VHOST_NET_USED
974 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
975 #endif
976 #ifdef CONFIG_L2TPV3
977 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
978 #endif
979 };
980
981
982 static int net_client_init1(const void *object, bool is_netdev, Error **errp)
983 {
984 Netdev legacy = {0};
985 const Netdev *netdev;
986 const char *name;
987 NetClientState *peer = NULL;
988 static bool vlan_warned;
989
990 if (is_netdev) {
991 netdev = object;
992 name = netdev->id;
993
994 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
995 !net_client_init_fun[netdev->type]) {
996 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
997 "a netdev backend type");
998 return -1;
999 }
1000 } else {
1001 const NetLegacy *net = object;
1002 const NetLegacyOptions *opts = net->opts;
1003 legacy.id = net->id;
1004 netdev = &legacy;
1005 /* missing optional values have been initialized to "all bits zero" */
1006 name = net->has_id ? net->id : net->name;
1007
1008 /* Map the old options to the new flat type */
1009 switch (opts->type) {
1010 case NET_LEGACY_OPTIONS_TYPE_NONE:
1011 return 0; /* nothing to do */
1012 case NET_LEGACY_OPTIONS_TYPE_NIC:
1013 legacy.type = NET_CLIENT_DRIVER_NIC;
1014 legacy.u.nic = opts->u.nic;
1015 break;
1016 case NET_LEGACY_OPTIONS_TYPE_USER:
1017 legacy.type = NET_CLIENT_DRIVER_USER;
1018 legacy.u.user = opts->u.user;
1019 break;
1020 case NET_LEGACY_OPTIONS_TYPE_TAP:
1021 legacy.type = NET_CLIENT_DRIVER_TAP;
1022 legacy.u.tap = opts->u.tap;
1023 break;
1024 case NET_LEGACY_OPTIONS_TYPE_L2TPV3:
1025 legacy.type = NET_CLIENT_DRIVER_L2TPV3;
1026 legacy.u.l2tpv3 = opts->u.l2tpv3;
1027 break;
1028 case NET_LEGACY_OPTIONS_TYPE_SOCKET:
1029 legacy.type = NET_CLIENT_DRIVER_SOCKET;
1030 legacy.u.socket = opts->u.socket;
1031 break;
1032 case NET_LEGACY_OPTIONS_TYPE_VDE:
1033 legacy.type = NET_CLIENT_DRIVER_VDE;
1034 legacy.u.vde = opts->u.vde;
1035 break;
1036 case NET_LEGACY_OPTIONS_TYPE_BRIDGE:
1037 legacy.type = NET_CLIENT_DRIVER_BRIDGE;
1038 legacy.u.bridge = opts->u.bridge;
1039 break;
1040 case NET_LEGACY_OPTIONS_TYPE_NETMAP:
1041 legacy.type = NET_CLIENT_DRIVER_NETMAP;
1042 legacy.u.netmap = opts->u.netmap;
1043 break;
1044 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER:
1045 legacy.type = NET_CLIENT_DRIVER_VHOST_USER;
1046 legacy.u.vhost_user = opts->u.vhost_user;
1047 break;
1048 default:
1049 abort();
1050 }
1051
1052 if (!net_client_init_fun[netdev->type]) {
1053 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1054 "a net backend type (maybe it is not compiled "
1055 "into this binary)");
1056 return -1;
1057 }
1058
1059 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1060 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
1061 !opts->u.nic.has_netdev) {
1062 peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL, NULL);
1063 }
1064
1065 if (net->has_vlan && !vlan_warned) {
1066 error_report("'vlan' is deprecated. Please use 'netdev' instead.");
1067 vlan_warned = true;
1068 }
1069 }
1070
1071 if (net_client_init_fun[netdev->type](netdev, name, peer, errp) < 0) {
1072 /* FIXME drop when all init functions store an Error */
1073 if (errp && !*errp) {
1074 error_setg(errp, QERR_DEVICE_INIT_FAILED,
1075 NetClientDriver_str(netdev->type));
1076 }
1077 return -1;
1078 }
1079 return 0;
1080 }
1081
1082 static void show_netdevs(void)
1083 {
1084 int idx;
1085 const char *available_netdevs[] = {
1086 "socket",
1087 "hubport",
1088 "tap",
1089 #ifdef CONFIG_SLIRP
1090 "user",
1091 #endif
1092 #ifdef CONFIG_L2TPV3
1093 "l2tpv3",
1094 #endif
1095 #ifdef CONFIG_VDE
1096 "vde",
1097 #endif
1098 #ifdef CONFIG_NET_BRIDGE
1099 "bridge",
1100 #endif
1101 #ifdef CONFIG_NETMAP
1102 "netmap",
1103 #endif
1104 #ifdef CONFIG_POSIX
1105 "vhost-user",
1106 #endif
1107 };
1108
1109 printf("Available netdev backend types:\n");
1110 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
1111 puts(available_netdevs[idx]);
1112 }
1113 }
1114
1115 static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
1116 {
1117 void *object = NULL;
1118 Error *err = NULL;
1119 int ret = -1;
1120 Visitor *v = opts_visitor_new(opts);
1121
1122 if (is_netdev && is_help_option(qemu_opt_get(opts, "type"))) {
1123 show_netdevs();
1124 exit(0);
1125 } else {
1126 /* Parse convenience option format ip6-net=fec0::0[/64] */
1127 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
1128
1129 if (ip6_net) {
1130 char buf[strlen(ip6_net) + 1];
1131
1132 if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) {
1133 /* Default 64bit prefix length. */
1134 qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort);
1135 qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort);
1136 } else {
1137 /* User-specified prefix length. */
1138 unsigned long len;
1139 int err;
1140
1141 qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort);
1142 err = qemu_strtoul(ip6_net, NULL, 10, &len);
1143
1144 if (err) {
1145 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1146 "ipv6-prefix", "a number");
1147 } else {
1148 qemu_opt_set_number(opts, "ipv6-prefixlen", len,
1149 &error_abort);
1150 }
1151 }
1152 qemu_opt_unset(opts, "ipv6-net");
1153 }
1154 }
1155
1156 if (is_netdev) {
1157 visit_type_Netdev(v, NULL, (Netdev **)&object, &err);
1158 } else {
1159 visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err);
1160 }
1161
1162 if (!err) {
1163 ret = net_client_init1(object, is_netdev, &err);
1164 }
1165
1166 if (is_netdev) {
1167 qapi_free_Netdev(object);
1168 } else {
1169 qapi_free_NetLegacy(object);
1170 }
1171
1172 error_propagate(errp, err);
1173 visit_free(v);
1174 return ret;
1175 }
1176
1177
1178 static int net_host_check_device(const char *device)
1179 {
1180 int i;
1181 for (i = 0; host_net_devices[i]; i++) {
1182 if (!strncmp(host_net_devices[i], device,
1183 strlen(host_net_devices[i]))) {
1184 return 1;
1185 }
1186 }
1187
1188 return 0;
1189 }
1190
1191 void hmp_host_net_add(Monitor *mon, const QDict *qdict)
1192 {
1193 const char *device = qdict_get_str(qdict, "device");
1194 const char *opts_str = qdict_get_try_str(qdict, "opts");
1195 Error *local_err = NULL;
1196 QemuOpts *opts;
1197 static bool warned;
1198
1199 if (!warned && !qtest_enabled()) {
1200 error_report("host_net_add is deprecated, use netdev_add instead");
1201 warned = true;
1202 }
1203
1204 if (!net_host_check_device(device)) {
1205 monitor_printf(mon, "invalid host network device %s\n", device);
1206 return;
1207 }
1208
1209 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1210 opts_str ? opts_str : "", false);
1211 if (!opts) {
1212 return;
1213 }
1214
1215 qemu_opt_set(opts, "type", device, &error_abort);
1216
1217 net_client_init(opts, false, &local_err);
1218 if (local_err) {
1219 error_report_err(local_err);
1220 monitor_printf(mon, "adding host network device %s failed\n", device);
1221 }
1222 }
1223
1224 void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
1225 {
1226 NetClientState *nc;
1227 int vlan_id = qdict_get_int(qdict, "vlan_id");
1228 const char *device = qdict_get_str(qdict, "device");
1229 static bool warned;
1230
1231 if (!warned && !qtest_enabled()) {
1232 error_report("host_net_remove is deprecated, use netdev_del instead");
1233 warned = true;
1234 }
1235
1236 nc = net_hub_find_client_by_name(vlan_id, device);
1237 if (!nc) {
1238 error_report("Host network device '%s' on hub '%d' not found",
1239 device, vlan_id);
1240 return;
1241 }
1242 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
1243 error_report("invalid host network device '%s'", device);
1244 return;
1245 }
1246
1247 qemu_del_net_client(nc->peer);
1248 qemu_del_net_client(nc);
1249 qemu_opts_del(qemu_opts_find(qemu_find_opts("net"), device));
1250 }
1251
1252 void netdev_add(QemuOpts *opts, Error **errp)
1253 {
1254 net_client_init(opts, true, errp);
1255 }
1256
1257 void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
1258 {
1259 Error *local_err = NULL;
1260 QemuOptsList *opts_list;
1261 QemuOpts *opts;
1262
1263 opts_list = qemu_find_opts_err("netdev", &local_err);
1264 if (local_err) {
1265 goto out;
1266 }
1267
1268 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
1269 if (local_err) {
1270 goto out;
1271 }
1272
1273 netdev_add(opts, &local_err);
1274 if (local_err) {
1275 qemu_opts_del(opts);
1276 goto out;
1277 }
1278
1279 out:
1280 error_propagate(errp, local_err);
1281 }
1282
1283 void qmp_netdev_del(const char *id, Error **errp)
1284 {
1285 NetClientState *nc;
1286 QemuOpts *opts;
1287
1288 nc = qemu_find_netdev(id);
1289 if (!nc) {
1290 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1291 "Device '%s' not found", id);
1292 return;
1293 }
1294
1295 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1296 if (!opts) {
1297 error_setg(errp, "Device '%s' is not a netdev", id);
1298 return;
1299 }
1300
1301 qemu_del_net_client(nc);
1302 qemu_opts_del(opts);
1303 }
1304
1305 static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1306 {
1307 char *str;
1308 ObjectProperty *prop;
1309 ObjectPropertyIterator iter;
1310 Visitor *v;
1311
1312 /* generate info str */
1313 object_property_iter_init(&iter, OBJECT(nf));
1314 while ((prop = object_property_iter_next(&iter))) {
1315 if (!strcmp(prop->name, "type")) {
1316 continue;
1317 }
1318 v = string_output_visitor_new(false, &str);
1319 object_property_get(OBJECT(nf), v, prop->name, NULL);
1320 visit_complete(v, &str);
1321 visit_free(v);
1322 monitor_printf(mon, ",%s=%s", prop->name, str);
1323 g_free(str);
1324 }
1325 monitor_printf(mon, "\n");
1326 }
1327
1328 void print_net_client(Monitor *mon, NetClientState *nc)
1329 {
1330 NetFilterState *nf;
1331
1332 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1333 nc->queue_index,
1334 NetClientDriver_str(nc->info->type),
1335 nc->info_str);
1336 if (!QTAILQ_EMPTY(&nc->filters)) {
1337 monitor_printf(mon, "filters:\n");
1338 }
1339 QTAILQ_FOREACH(nf, &nc->filters, next) {
1340 char *path = object_get_canonical_path_component(OBJECT(nf));
1341
1342 monitor_printf(mon, " - %s: type=%s", path,
1343 object_get_typename(OBJECT(nf)));
1344 netfilter_print_info(mon, nf);
1345 g_free(path);
1346 }
1347 }
1348
1349 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1350 Error **errp)
1351 {
1352 NetClientState *nc;
1353 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1354
1355 QTAILQ_FOREACH(nc, &net_clients, next) {
1356 RxFilterInfoList *entry;
1357 RxFilterInfo *info;
1358
1359 if (has_name && strcmp(nc->name, name) != 0) {
1360 continue;
1361 }
1362
1363 /* only query rx-filter information of NIC */
1364 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
1365 if (has_name) {
1366 error_setg(errp, "net client(%s) isn't a NIC", name);
1367 return NULL;
1368 }
1369 continue;
1370 }
1371
1372 /* only query information on queue 0 since the info is per nic,
1373 * not per queue
1374 */
1375 if (nc->queue_index != 0)
1376 continue;
1377
1378 if (nc->info->query_rx_filter) {
1379 info = nc->info->query_rx_filter(nc);
1380 entry = g_malloc0(sizeof(*entry));
1381 entry->value = info;
1382
1383 if (!filter_list) {
1384 filter_list = entry;
1385 } else {
1386 last_entry->next = entry;
1387 }
1388 last_entry = entry;
1389 } else if (has_name) {
1390 error_setg(errp, "net client(%s) doesn't support"
1391 " rx-filter querying", name);
1392 return NULL;
1393 }
1394
1395 if (has_name) {
1396 break;
1397 }
1398 }
1399
1400 if (filter_list == NULL && has_name) {
1401 error_setg(errp, "invalid net client name: %s", name);
1402 }
1403
1404 return filter_list;
1405 }
1406
1407 void hmp_info_network(Monitor *mon, const QDict *qdict)
1408 {
1409 NetClientState *nc, *peer;
1410 NetClientDriver type;
1411
1412 net_hub_info(mon);
1413
1414 QTAILQ_FOREACH(nc, &net_clients, next) {
1415 peer = nc->peer;
1416 type = nc->info->type;
1417
1418 /* Skip if already printed in hub info */
1419 if (net_hub_id_for_client(nc, NULL) == 0) {
1420 continue;
1421 }
1422
1423 if (!peer || type == NET_CLIENT_DRIVER_NIC) {
1424 print_net_client(mon, nc);
1425 } /* else it's a netdev connected to a NIC, printed with the NIC */
1426 if (peer && type == NET_CLIENT_DRIVER_NIC) {
1427 monitor_printf(mon, " \\ ");
1428 print_net_client(mon, peer);
1429 }
1430 }
1431 }
1432
1433 void qmp_set_link(const char *name, bool up, Error **errp)
1434 {
1435 NetClientState *ncs[MAX_QUEUE_NUM];
1436 NetClientState *nc;
1437 int queues, i;
1438
1439 queues = qemu_find_net_clients_except(name, ncs,
1440 NET_CLIENT_DRIVER__MAX,
1441 MAX_QUEUE_NUM);
1442
1443 if (queues == 0) {
1444 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1445 "Device '%s' not found", name);
1446 return;
1447 }
1448 nc = ncs[0];
1449
1450 for (i = 0; i < queues; i++) {
1451 ncs[i]->link_down = !up;
1452 }
1453
1454 if (nc->info->link_status_changed) {
1455 nc->info->link_status_changed(nc);
1456 }
1457
1458 if (nc->peer) {
1459 /* Change peer link only if the peer is NIC and then notify peer.
1460 * If the peer is a HUBPORT or a backend, we do not change the
1461 * link status.
1462 *
1463 * This behavior is compatible with qemu vlans where there could be
1464 * multiple clients that can still communicate with each other in
1465 * disconnected mode. For now maintain this compatibility.
1466 */
1467 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
1468 for (i = 0; i < queues; i++) {
1469 ncs[i]->peer->link_down = !up;
1470 }
1471 }
1472 if (nc->peer->info->link_status_changed) {
1473 nc->peer->info->link_status_changed(nc->peer);
1474 }
1475 }
1476 }
1477
1478 static void net_vm_change_state_handler(void *opaque, int running,
1479 RunState state)
1480 {
1481 NetClientState *nc;
1482 NetClientState *tmp;
1483
1484 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1485 if (running) {
1486 /* Flush queued packets and wake up backends. */
1487 if (nc->peer && qemu_can_send_packet(nc)) {
1488 qemu_flush_queued_packets(nc->peer);
1489 }
1490 } else {
1491 /* Complete all queued packets, to guarantee we don't modify
1492 * state later when VM is not running.
1493 */
1494 qemu_flush_or_purge_queued_packets(nc, true);
1495 }
1496 }
1497 }
1498
1499 void net_cleanup(void)
1500 {
1501 NetClientState *nc;
1502
1503 /* We may del multiple entries during qemu_del_net_client(),
1504 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1505 */
1506 while (!QTAILQ_EMPTY(&net_clients)) {
1507 nc = QTAILQ_FIRST(&net_clients);
1508 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
1509 qemu_del_nic(qemu_get_nic(nc));
1510 } else {
1511 qemu_del_net_client(nc);
1512 }
1513 }
1514
1515 qemu_del_vm_change_state_handler(net_change_state_entry);
1516 }
1517
1518 void net_check_clients(void)
1519 {
1520 NetClientState *nc;
1521 int i;
1522
1523 net_hub_check_clients();
1524
1525 QTAILQ_FOREACH(nc, &net_clients, next) {
1526 if (!nc->peer) {
1527 warn_report("%s %s has no peer",
1528 nc->info->type == NET_CLIENT_DRIVER_NIC
1529 ? "nic" : "netdev",
1530 nc->name);
1531 }
1532 }
1533
1534 /* Check that all NICs requested via -net nic actually got created.
1535 * NICs created via -device don't need to be checked here because
1536 * they are always instantiated.
1537 */
1538 for (i = 0; i < MAX_NICS; i++) {
1539 NICInfo *nd = &nd_table[i];
1540 if (nd->used && !nd->instantiated) {
1541 warn_report("requested NIC (%s, model %s) "
1542 "was not created (not supported by this machine?)",
1543 nd->name ? nd->name : "anonymous",
1544 nd->model ? nd->model : "unspecified");
1545 }
1546 }
1547 }
1548
1549 static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
1550 {
1551 return net_client_init(opts, false, errp);
1552 }
1553
1554 static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
1555 {
1556 return net_client_init(opts, true, errp);
1557 }
1558
1559 int net_init_clients(Error **errp)
1560 {
1561 net_change_state_entry =
1562 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1563
1564 QTAILQ_INIT(&net_clients);
1565
1566 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1567 net_init_netdev, NULL, errp)) {
1568 return -1;
1569 }
1570
1571 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) {
1572 return -1;
1573 }
1574
1575 return 0;
1576 }
1577
1578 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1579 {
1580 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
1581 return -1;
1582 }
1583
1584 return 0;
1585 }
1586
1587 /* From FreeBSD */
1588 /* XXX: optimize */
1589 uint32_t net_crc32(const uint8_t *p, int len)
1590 {
1591 uint32_t crc;
1592 int carry, i, j;
1593 uint8_t b;
1594
1595 crc = 0xffffffff;
1596 for (i = 0; i < len; i++) {
1597 b = *p++;
1598 for (j = 0; j < 8; j++) {
1599 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1600 crc <<= 1;
1601 b >>= 1;
1602 if (carry) {
1603 crc = ((crc ^ POLYNOMIAL_BE) | carry);
1604 }
1605 }
1606 }
1607
1608 return crc;
1609 }
1610
1611 uint32_t net_crc32_le(const uint8_t *p, int len)
1612 {
1613 uint32_t crc;
1614 int carry, i, j;
1615 uint8_t b;
1616
1617 crc = 0xffffffff;
1618 for (i = 0; i < len; i++) {
1619 b = *p++;
1620 for (j = 0; j < 8; j++) {
1621 carry = (crc & 0x1) ^ (b & 0x01);
1622 crc >>= 1;
1623 b >>= 1;
1624 if (carry) {
1625 crc ^= POLYNOMIAL_LE;
1626 }
1627 }
1628 }
1629
1630 return crc;
1631 }
1632
1633 QemuOptsList qemu_netdev_opts = {
1634 .name = "netdev",
1635 .implied_opt_name = "type",
1636 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1637 .desc = {
1638 /*
1639 * no elements => accept any params
1640 * validation will happen later
1641 */
1642 { /* end of list */ }
1643 },
1644 };
1645
1646 QemuOptsList qemu_net_opts = {
1647 .name = "net",
1648 .implied_opt_name = "type",
1649 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1650 .desc = {
1651 /*
1652 * no elements => accept any params
1653 * validation will happen later
1654 */
1655 { /* end of list */ }
1656 },
1657 };
1658
1659 void net_socket_rs_init(SocketReadState *rs,
1660 SocketReadStateFinalize *finalize,
1661 bool vnet_hdr)
1662 {
1663 rs->state = 0;
1664 rs->vnet_hdr = vnet_hdr;
1665 rs->index = 0;
1666 rs->packet_len = 0;
1667 rs->vnet_hdr_len = 0;
1668 memset(rs->buf, 0, sizeof(rs->buf));
1669 rs->finalize = finalize;
1670 }
1671
1672 /*
1673 * Returns
1674 * 0: success
1675 * -1: error occurs
1676 */
1677 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
1678 {
1679 unsigned int l;
1680
1681 while (size > 0) {
1682 /* Reassemble a packet from the network.
1683 * 0 = getting length.
1684 * 1 = getting vnet header length.
1685 * 2 = getting data.
1686 */
1687 switch (rs->state) {
1688 case 0:
1689 l = 4 - rs->index;
1690 if (l > size) {
1691 l = size;
1692 }
1693 memcpy(rs->buf + rs->index, buf, l);
1694 buf += l;
1695 size -= l;
1696 rs->index += l;
1697 if (rs->index == 4) {
1698 /* got length */
1699 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
1700 rs->index = 0;
1701 if (rs->vnet_hdr) {
1702 rs->state = 1;
1703 } else {
1704 rs->state = 2;
1705 rs->vnet_hdr_len = 0;
1706 }
1707 }
1708 break;
1709 case 1:
1710 l = 4 - rs->index;
1711 if (l > size) {
1712 l = size;
1713 }
1714 memcpy(rs->buf + rs->index, buf, l);
1715 buf += l;
1716 size -= l;
1717 rs->index += l;
1718 if (rs->index == 4) {
1719 /* got vnet header length */
1720 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
1721 rs->index = 0;
1722 rs->state = 2;
1723 }
1724 break;
1725 case 2:
1726 l = rs->packet_len - rs->index;
1727 if (l > size) {
1728 l = size;
1729 }
1730 if (rs->index + l <= sizeof(rs->buf)) {
1731 memcpy(rs->buf + rs->index, buf, l);
1732 } else {
1733 fprintf(stderr, "serious error: oversized packet received,"
1734 "connection terminated.\n");
1735 rs->index = rs->state = 0;
1736 return -1;
1737 }
1738
1739 rs->index += l;
1740 buf += l;
1741 size -= l;
1742 if (rs->index >= rs->packet_len) {
1743 rs->index = 0;
1744 rs->state = 0;
1745 assert(rs->finalize);
1746 rs->finalize(rs);
1747 }
1748 break;
1749 }
1750 }
1751
1752 assert(size == 0);
1753 return 0;
1754 }