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