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