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