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