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