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