File:  [Qemu by Fabrice Bellard] / qemu / net.c
Revision 1.1.1.12 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:56:07 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1000, qemu0151, HEAD
qemu 0.15.1

    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 "net.h"
   25: 
   26: #include "config-host.h"
   27: 
   28: #include "net/tap.h"
   29: #include "net/socket.h"
   30: #include "net/dump.h"
   31: #include "net/slirp.h"
   32: #include "net/vde.h"
   33: #include "net/util.h"
   34: #include "monitor.h"
   35: #include "qemu-common.h"
   36: #include "qemu_socket.h"
   37: #include "hw/qdev.h"
   38: #include "iov.h"
   39: 
   40: static QTAILQ_HEAD(, VLANState) vlans;
   41: static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
   42: 
   43: int default_net = 1;
   44: 
   45: /***********************************************************/
   46: /* network device redirectors */
   47: 
   48: #if defined(DEBUG_NET)
   49: static void hex_dump(FILE *f, const uint8_t *buf, int size)
   50: {
   51:     int len, i, j, c;
   52: 
   53:     for(i=0;i<size;i+=16) {
   54:         len = size - i;
   55:         if (len > 16)
   56:             len = 16;
   57:         fprintf(f, "%08x ", i);
   58:         for(j=0;j<16;j++) {
   59:             if (j < len)
   60:                 fprintf(f, " %02x", buf[i+j]);
   61:             else
   62:                 fprintf(f, "   ");
   63:         }
   64:         fprintf(f, " ");
   65:         for(j=0;j<len;j++) {
   66:             c = buf[i+j];
   67:             if (c < ' ' || c > '~')
   68:                 c = '.';
   69:             fprintf(f, "%c", c);
   70:         }
   71:         fprintf(f, "\n");
   72:     }
   73: }
   74: #endif
   75: 
   76: static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
   77: {
   78:     const char *p, *p1;
   79:     int len;
   80:     p = *pp;
   81:     p1 = strchr(p, sep);
   82:     if (!p1)
   83:         return -1;
   84:     len = p1 - p;
   85:     p1++;
   86:     if (buf_size > 0) {
   87:         if (len > buf_size - 1)
   88:             len = buf_size - 1;
   89:         memcpy(buf, p, len);
   90:         buf[len] = '\0';
   91:     }
   92:     *pp = p1;
   93:     return 0;
   94: }
   95: 
   96: int parse_host_port(struct sockaddr_in *saddr, const char *str)
   97: {
   98:     char buf[512];
   99:     struct hostent *he;
  100:     const char *p, *r;
  101:     int port;
  102: 
  103:     p = str;
  104:     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  105:         return -1;
  106:     saddr->sin_family = AF_INET;
  107:     if (buf[0] == '\0') {
  108:         saddr->sin_addr.s_addr = 0;
  109:     } else {
  110:         if (qemu_isdigit(buf[0])) {
  111:             if (!inet_aton(buf, &saddr->sin_addr))
  112:                 return -1;
  113:         } else {
  114:             if ((he = gethostbyname(buf)) == NULL)
  115:                 return - 1;
  116:             saddr->sin_addr = *(struct in_addr *)he->h_addr;
  117:         }
  118:     }
  119:     port = strtol(p, (char **)&r, 0);
  120:     if (r == p)
  121:         return -1;
  122:     saddr->sin_port = htons(port);
  123:     return 0;
  124: }
  125: 
  126: void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
  127: {
  128:     snprintf(vc->info_str, sizeof(vc->info_str),
  129:              "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
  130:              vc->model,
  131:              macaddr[0], macaddr[1], macaddr[2],
  132:              macaddr[3], macaddr[4], macaddr[5]);
  133: }
  134: 
  135: void qemu_macaddr_default_if_unset(MACAddr *macaddr)
  136: {
  137:     static int index = 0;
  138:     static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
  139: 
  140:     if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
  141:         return;
  142:     macaddr->a[0] = 0x52;
  143:     macaddr->a[1] = 0x54;
  144:     macaddr->a[2] = 0x00;
  145:     macaddr->a[3] = 0x12;
  146:     macaddr->a[4] = 0x34;
  147:     macaddr->a[5] = 0x56 + index++;
  148: }
  149: 
  150: static char *assign_name(VLANClientState *vc1, const char *model)
  151: {
  152:     VLANState *vlan;
  153:     char buf[256];
  154:     int id = 0;
  155: 
  156:     QTAILQ_FOREACH(vlan, &vlans, next) {
  157:         VLANClientState *vc;
  158: 
  159:         QTAILQ_FOREACH(vc, &vlan->clients, next) {
  160:             if (vc != vc1 && strcmp(vc->model, model) == 0) {
  161:                 id++;
  162:             }
  163:         }
  164:     }
  165: 
  166:     snprintf(buf, sizeof(buf), "%s.%d", model, id);
  167: 
  168:     return qemu_strdup(buf);
  169: }
  170: 
  171: static ssize_t qemu_deliver_packet(VLANClientState *sender,
  172:                                    unsigned flags,
  173:                                    const uint8_t *data,
  174:                                    size_t size,
  175:                                    void *opaque);
  176: static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
  177:                                        unsigned flags,
  178:                                        const struct iovec *iov,
  179:                                        int iovcnt,
  180:                                        void *opaque);
  181: 
  182: VLANClientState *qemu_new_net_client(NetClientInfo *info,
  183:                                      VLANState *vlan,
  184:                                      VLANClientState *peer,
  185:                                      const char *model,
  186:                                      const char *name)
  187: {
  188:     VLANClientState *vc;
  189: 
  190:     assert(info->size >= sizeof(VLANClientState));
  191: 
  192:     vc = qemu_mallocz(info->size);
  193: 
  194:     vc->info = info;
  195:     vc->model = qemu_strdup(model);
  196:     if (name) {
  197:         vc->name = qemu_strdup(name);
  198:     } else {
  199:         vc->name = assign_name(vc, model);
  200:     }
  201: 
  202:     if (vlan) {
  203:         assert(!peer);
  204:         vc->vlan = vlan;
  205:         QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
  206:     } else {
  207:         if (peer) {
  208:             assert(!peer->peer);
  209:             vc->peer = peer;
  210:             peer->peer = vc;
  211:         }
  212:         QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
  213: 
  214:         vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
  215:                                             qemu_deliver_packet_iov,
  216:                                             vc);
  217:     }
  218: 
  219:     return vc;
  220: }
  221: 
  222: NICState *qemu_new_nic(NetClientInfo *info,
  223:                        NICConf *conf,
  224:                        const char *model,
  225:                        const char *name,
  226:                        void *opaque)
  227: {
  228:     VLANClientState *nc;
  229:     NICState *nic;
  230: 
  231:     assert(info->type == NET_CLIENT_TYPE_NIC);
  232:     assert(info->size >= sizeof(NICState));
  233: 
  234:     nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
  235: 
  236:     nic = DO_UPCAST(NICState, nc, nc);
  237:     nic->conf = conf;
  238:     nic->opaque = opaque;
  239: 
  240:     return nic;
  241: }
  242: 
  243: static void qemu_cleanup_vlan_client(VLANClientState *vc)
  244: {
  245:     if (vc->vlan) {
  246:         QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
  247:     } else {
  248:         QTAILQ_REMOVE(&non_vlan_clients, vc, next);
  249:     }
  250: 
  251:     if (vc->info->cleanup) {
  252:         vc->info->cleanup(vc);
  253:     }
  254: }
  255: 
  256: static void qemu_free_vlan_client(VLANClientState *vc)
  257: {
  258:     if (!vc->vlan) {
  259:         if (vc->send_queue) {
  260:             qemu_del_net_queue(vc->send_queue);
  261:         }
  262:         if (vc->peer) {
  263:             vc->peer->peer = NULL;
  264:         }
  265:     }
  266:     qemu_free(vc->name);
  267:     qemu_free(vc->model);
  268:     qemu_free(vc);
  269: }
  270: 
  271: void qemu_del_vlan_client(VLANClientState *vc)
  272: {
  273:     /* If there is a peer NIC, delete and cleanup client, but do not free. */
  274:     if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
  275:         NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
  276:         if (nic->peer_deleted) {
  277:             return;
  278:         }
  279:         nic->peer_deleted = true;
  280:         /* Let NIC know peer is gone. */
  281:         vc->peer->link_down = true;
  282:         if (vc->peer->info->link_status_changed) {
  283:             vc->peer->info->link_status_changed(vc->peer);
  284:         }
  285:         qemu_cleanup_vlan_client(vc);
  286:         return;
  287:     }
  288: 
  289:     /* If this is a peer NIC and peer has already been deleted, free it now. */
  290:     if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
  291:         NICState *nic = DO_UPCAST(NICState, nc, vc);
  292:         if (nic->peer_deleted) {
  293:             qemu_free_vlan_client(vc->peer);
  294:         }
  295:     }
  296: 
  297:     qemu_cleanup_vlan_client(vc);
  298:     qemu_free_vlan_client(vc);
  299: }
  300: 
  301: VLANClientState *
  302: qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
  303:                               const char *client_str)
  304: {
  305:     VLANState *vlan;
  306:     VLANClientState *vc;
  307: 
  308:     vlan = qemu_find_vlan(vlan_id, 0);
  309:     if (!vlan) {
  310:         monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
  311:         return NULL;
  312:     }
  313: 
  314:     QTAILQ_FOREACH(vc, &vlan->clients, next) {
  315:         if (!strcmp(vc->name, client_str)) {
  316:             break;
  317:         }
  318:     }
  319:     if (!vc) {
  320:         monitor_printf(mon, "can't find device %s on VLAN %d\n",
  321:                        client_str, vlan_id);
  322:     }
  323: 
  324:     return vc;
  325: }
  326: 
  327: void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
  328: {
  329:     VLANClientState *nc;
  330:     VLANState *vlan;
  331: 
  332:     QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
  333:         if (nc->info->type == NET_CLIENT_TYPE_NIC) {
  334:             func(DO_UPCAST(NICState, nc, nc), opaque);
  335:         }
  336:     }
  337: 
  338:     QTAILQ_FOREACH(vlan, &vlans, next) {
  339:         QTAILQ_FOREACH(nc, &vlan->clients, next) {
  340:             if (nc->info->type == NET_CLIENT_TYPE_NIC) {
  341:                 func(DO_UPCAST(NICState, nc, nc), opaque);
  342:             }
  343:         }
  344:     }
  345: }
  346: 
  347: int qemu_can_send_packet(VLANClientState *sender)
  348: {
  349:     VLANState *vlan = sender->vlan;
  350:     VLANClientState *vc;
  351: 
  352:     if (sender->peer) {
  353:         if (sender->peer->receive_disabled) {
  354:             return 0;
  355:         } else if (sender->peer->info->can_receive &&
  356:                    !sender->peer->info->can_receive(sender->peer)) {
  357:             return 0;
  358:         } else {
  359:             return 1;
  360:         }
  361:     }
  362: 
  363:     if (!sender->vlan) {
  364:         return 1;
  365:     }
  366: 
  367:     QTAILQ_FOREACH(vc, &vlan->clients, next) {
  368:         if (vc == sender) {
  369:             continue;
  370:         }
  371: 
  372:         /* no can_receive() handler, they can always receive */
  373:         if (vc->info->can_receive && !vc->info->can_receive(vc)) {
  374:             return 0;
  375:         }
  376:     }
  377:     return 1;
  378: }
  379: 
  380: static ssize_t qemu_deliver_packet(VLANClientState *sender,
  381:                                    unsigned flags,
  382:                                    const uint8_t *data,
  383:                                    size_t size,
  384:                                    void *opaque)
  385: {
  386:     VLANClientState *vc = opaque;
  387:     ssize_t ret;
  388: 
  389:     if (vc->link_down) {
  390:         return size;
  391:     }
  392: 
  393:     if (vc->receive_disabled) {
  394:         return 0;
  395:     }
  396: 
  397:     if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
  398:         ret = vc->info->receive_raw(vc, data, size);
  399:     } else {
  400:         ret = vc->info->receive(vc, data, size);
  401:     }
  402: 
  403:     if (ret == 0) {
  404:         vc->receive_disabled = 1;
  405:     };
  406: 
  407:     return ret;
  408: }
  409: 
  410: static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
  411:                                         unsigned flags,
  412:                                         const uint8_t *buf,
  413:                                         size_t size,
  414:                                         void *opaque)
  415: {
  416:     VLANState *vlan = opaque;
  417:     VLANClientState *vc;
  418:     ssize_t ret = -1;
  419: 
  420:     QTAILQ_FOREACH(vc, &vlan->clients, next) {
  421:         ssize_t len;
  422: 
  423:         if (vc == sender) {
  424:             continue;
  425:         }
  426: 
  427:         if (vc->link_down) {
  428:             ret = size;
  429:             continue;
  430:         }
  431: 
  432:         if (vc->receive_disabled) {
  433:             ret = 0;
  434:             continue;
  435:         }
  436: 
  437:         if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
  438:             len = vc->info->receive_raw(vc, buf, size);
  439:         } else {
  440:             len = vc->info->receive(vc, buf, size);
  441:         }
  442: 
  443:         if (len == 0) {
  444:             vc->receive_disabled = 1;
  445:         }
  446: 
  447:         ret = (ret >= 0) ? ret : len;
  448: 
  449:     }
  450: 
  451:     return ret;
  452: }
  453: 
  454: void qemu_purge_queued_packets(VLANClientState *vc)
  455: {
  456:     NetQueue *queue;
  457: 
  458:     if (!vc->peer && !vc->vlan) {
  459:         return;
  460:     }
  461: 
  462:     if (vc->peer) {
  463:         queue = vc->peer->send_queue;
  464:     } else {
  465:         queue = vc->vlan->send_queue;
  466:     }
  467: 
  468:     qemu_net_queue_purge(queue, vc);
  469: }
  470: 
  471: void qemu_flush_queued_packets(VLANClientState *vc)
  472: {
  473:     NetQueue *queue;
  474: 
  475:     vc->receive_disabled = 0;
  476: 
  477:     if (vc->vlan) {
  478:         queue = vc->vlan->send_queue;
  479:     } else {
  480:         queue = vc->send_queue;
  481:     }
  482: 
  483:     qemu_net_queue_flush(queue);
  484: }
  485: 
  486: static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
  487:                                                  unsigned flags,
  488:                                                  const uint8_t *buf, int size,
  489:                                                  NetPacketSent *sent_cb)
  490: {
  491:     NetQueue *queue;
  492: 
  493: #ifdef DEBUG_NET
  494:     printf("qemu_send_packet_async:\n");
  495:     hex_dump(stdout, buf, size);
  496: #endif
  497: 
  498:     if (sender->link_down || (!sender->peer && !sender->vlan)) {
  499:         return size;
  500:     }
  501: 
  502:     if (sender->peer) {
  503:         queue = sender->peer->send_queue;
  504:     } else {
  505:         queue = sender->vlan->send_queue;
  506:     }
  507: 
  508:     return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
  509: }
  510: 
  511: ssize_t qemu_send_packet_async(VLANClientState *sender,
  512:                                const uint8_t *buf, int size,
  513:                                NetPacketSent *sent_cb)
  514: {
  515:     return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
  516:                                              buf, size, sent_cb);
  517: }
  518: 
  519: void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
  520: {
  521:     qemu_send_packet_async(vc, buf, size, NULL);
  522: }
  523: 
  524: ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
  525: {
  526:     return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
  527:                                              buf, size, NULL);
  528: }
  529: 
  530: static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
  531:                                int iovcnt)
  532: {
  533:     uint8_t buffer[4096];
  534:     size_t offset;
  535: 
  536:     offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
  537: 
  538:     return vc->info->receive(vc, buffer, offset);
  539: }
  540: 
  541: static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
  542:                                        unsigned flags,
  543:                                        const struct iovec *iov,
  544:                                        int iovcnt,
  545:                                        void *opaque)
  546: {
  547:     VLANClientState *vc = opaque;
  548: 
  549:     if (vc->link_down) {
  550:         return iov_size(iov, iovcnt);
  551:     }
  552: 
  553:     if (vc->info->receive_iov) {
  554:         return vc->info->receive_iov(vc, iov, iovcnt);
  555:     } else {
  556:         return vc_sendv_compat(vc, iov, iovcnt);
  557:     }
  558: }
  559: 
  560: static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
  561:                                             unsigned flags,
  562:                                             const struct iovec *iov,
  563:                                             int iovcnt,
  564:                                             void *opaque)
  565: {
  566:     VLANState *vlan = opaque;
  567:     VLANClientState *vc;
  568:     ssize_t ret = -1;
  569: 
  570:     QTAILQ_FOREACH(vc, &vlan->clients, next) {
  571:         ssize_t len;
  572: 
  573:         if (vc == sender) {
  574:             continue;
  575:         }
  576: 
  577:         if (vc->link_down) {
  578:             ret = iov_size(iov, iovcnt);
  579:             continue;
  580:         }
  581: 
  582:         assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
  583: 
  584:         if (vc->info->receive_iov) {
  585:             len = vc->info->receive_iov(vc, iov, iovcnt);
  586:         } else {
  587:             len = vc_sendv_compat(vc, iov, iovcnt);
  588:         }
  589: 
  590:         ret = (ret >= 0) ? ret : len;
  591:     }
  592: 
  593:     return ret;
  594: }
  595: 
  596: ssize_t qemu_sendv_packet_async(VLANClientState *sender,
  597:                                 const struct iovec *iov, int iovcnt,
  598:                                 NetPacketSent *sent_cb)
  599: {
  600:     NetQueue *queue;
  601: 
  602:     if (sender->link_down || (!sender->peer && !sender->vlan)) {
  603:         return iov_size(iov, iovcnt);
  604:     }
  605: 
  606:     if (sender->peer) {
  607:         queue = sender->peer->send_queue;
  608:     } else {
  609:         queue = sender->vlan->send_queue;
  610:     }
  611: 
  612:     return qemu_net_queue_send_iov(queue, sender,
  613:                                    QEMU_NET_PACKET_FLAG_NONE,
  614:                                    iov, iovcnt, sent_cb);
  615: }
  616: 
  617: ssize_t
  618: qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
  619: {
  620:     return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
  621: }
  622: 
  623: /* find or alloc a new VLAN */
  624: VLANState *qemu_find_vlan(int id, int allocate)
  625: {
  626:     VLANState *vlan;
  627: 
  628:     QTAILQ_FOREACH(vlan, &vlans, next) {
  629:         if (vlan->id == id) {
  630:             return vlan;
  631:         }
  632:     }
  633: 
  634:     if (!allocate) {
  635:         return NULL;
  636:     }
  637: 
  638:     vlan = qemu_mallocz(sizeof(VLANState));
  639:     vlan->id = id;
  640:     QTAILQ_INIT(&vlan->clients);
  641: 
  642:     vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
  643:                                           qemu_vlan_deliver_packet_iov,
  644:                                           vlan);
  645: 
  646:     QTAILQ_INSERT_TAIL(&vlans, vlan, next);
  647: 
  648:     return vlan;
  649: }
  650: 
  651: VLANClientState *qemu_find_netdev(const char *id)
  652: {
  653:     VLANClientState *vc;
  654: 
  655:     QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
  656:         if (!strcmp(vc->name, id)) {
  657:             return vc;
  658:         }
  659:     }
  660: 
  661:     return NULL;
  662: }
  663: 
  664: static int nic_get_free_idx(void)
  665: {
  666:     int index;
  667: 
  668:     for (index = 0; index < MAX_NICS; index++)
  669:         if (!nd_table[index].used)
  670:             return index;
  671:     return -1;
  672: }
  673: 
  674: int qemu_show_nic_models(const char *arg, const char *const *models)
  675: {
  676:     int i;
  677: 
  678:     if (!arg || strcmp(arg, "?"))
  679:         return 0;
  680: 
  681:     fprintf(stderr, "qemu: Supported NIC models: ");
  682:     for (i = 0 ; models[i]; i++)
  683:         fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
  684:     return 1;
  685: }
  686: 
  687: void qemu_check_nic_model(NICInfo *nd, const char *model)
  688: {
  689:     const char *models[2];
  690: 
  691:     models[0] = model;
  692:     models[1] = NULL;
  693: 
  694:     if (qemu_show_nic_models(nd->model, models))
  695:         exit(0);
  696:     if (qemu_find_nic_model(nd, models, model) < 0)
  697:         exit(1);
  698: }
  699: 
  700: int qemu_find_nic_model(NICInfo *nd, const char * const *models,
  701:                         const char *default_model)
  702: {
  703:     int i;
  704: 
  705:     if (!nd->model)
  706:         nd->model = qemu_strdup(default_model);
  707: 
  708:     for (i = 0 ; models[i]; i++) {
  709:         if (strcmp(nd->model, models[i]) == 0)
  710:             return i;
  711:     }
  712: 
  713:     error_report("Unsupported NIC model: %s", nd->model);
  714:     return -1;
  715: }
  716: 
  717: int net_handle_fd_param(Monitor *mon, const char *param)
  718: {
  719:     int fd;
  720: 
  721:     if (!qemu_isdigit(param[0]) && mon) {
  722: 
  723:         fd = monitor_get_fd(mon, param);
  724:         if (fd == -1) {
  725:             error_report("No file descriptor named %s found", param);
  726:             return -1;
  727:         }
  728:     } else {
  729:         char *endptr = NULL;
  730: 
  731:         fd = strtol(param, &endptr, 10);
  732:         if (*endptr || (fd == 0 && param == endptr)) {
  733:             return -1;
  734:         }
  735:     }
  736: 
  737:     return fd;
  738: }
  739: 
  740: static int net_init_nic(QemuOpts *opts,
  741:                         Monitor *mon,
  742:                         const char *name,
  743:                         VLANState *vlan)
  744: {
  745:     int idx;
  746:     NICInfo *nd;
  747:     const char *netdev;
  748: 
  749:     idx = nic_get_free_idx();
  750:     if (idx == -1 || nb_nics >= MAX_NICS) {
  751:         error_report("Too Many NICs");
  752:         return -1;
  753:     }
  754: 
  755:     nd = &nd_table[idx];
  756: 
  757:     memset(nd, 0, sizeof(*nd));
  758: 
  759:     if ((netdev = qemu_opt_get(opts, "netdev"))) {
  760:         nd->netdev = qemu_find_netdev(netdev);
  761:         if (!nd->netdev) {
  762:             error_report("netdev '%s' not found", netdev);
  763:             return -1;
  764:         }
  765:     } else {
  766:         assert(vlan);
  767:         nd->vlan = vlan;
  768:     }
  769:     if (name) {
  770:         nd->name = qemu_strdup(name);
  771:     }
  772:     if (qemu_opt_get(opts, "model")) {
  773:         nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
  774:     }
  775:     if (qemu_opt_get(opts, "addr")) {
  776:         nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
  777:     }
  778: 
  779:     if (qemu_opt_get(opts, "macaddr") &&
  780:         net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
  781:         error_report("invalid syntax for ethernet address");
  782:         return -1;
  783:     }
  784:     qemu_macaddr_default_if_unset(&nd->macaddr);
  785: 
  786:     nd->nvectors = qemu_opt_get_number(opts, "vectors",
  787:                                        DEV_NVECTORS_UNSPECIFIED);
  788:     if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
  789:         (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
  790:         error_report("invalid # of vectors: %d", nd->nvectors);
  791:         return -1;
  792:     }
  793: 
  794:     nd->used = 1;
  795:     nb_nics++;
  796: 
  797:     return idx;
  798: }
  799: 
  800: #define NET_COMMON_PARAMS_DESC                     \
  801:     {                                              \
  802:         .name = "type",                            \
  803:         .type = QEMU_OPT_STRING,                   \
  804:         .help = "net client type (nic, tap etc.)", \
  805:      }, {                                          \
  806:         .name = "vlan",                            \
  807:         .type = QEMU_OPT_NUMBER,                   \
  808:         .help = "vlan number",                     \
  809:      }, {                                          \
  810:         .name = "name",                            \
  811:         .type = QEMU_OPT_STRING,                   \
  812:         .help = "identifier for monitor commands", \
  813:      }
  814: 
  815: typedef int (*net_client_init_func)(QemuOpts *opts,
  816:                                     Monitor *mon,
  817:                                     const char *name,
  818:                                     VLANState *vlan);
  819: 
  820: /* magic number, but compiler will warn if too small */
  821: #define NET_MAX_DESC 20
  822: 
  823: static const struct {
  824:     const char *type;
  825:     net_client_init_func init;
  826:     QemuOptDesc desc[NET_MAX_DESC];
  827: } net_client_types[NET_CLIENT_TYPE_MAX] = {
  828:     [NET_CLIENT_TYPE_NONE] = {
  829:         .type = "none",
  830:         .desc = {
  831:             NET_COMMON_PARAMS_DESC,
  832:             { /* end of list */ }
  833:         },
  834:     },
  835:     [NET_CLIENT_TYPE_NIC] = {
  836:         .type = "nic",
  837:         .init = net_init_nic,
  838:         .desc = {
  839:             NET_COMMON_PARAMS_DESC,
  840:             {
  841:                 .name = "netdev",
  842:                 .type = QEMU_OPT_STRING,
  843:                 .help = "id of -netdev to connect to",
  844:             },
  845:             {
  846:                 .name = "macaddr",
  847:                 .type = QEMU_OPT_STRING,
  848:                 .help = "MAC address",
  849:             }, {
  850:                 .name = "model",
  851:                 .type = QEMU_OPT_STRING,
  852:                 .help = "device model (e1000, rtl8139, virtio etc.)",
  853:             }, {
  854:                 .name = "addr",
  855:                 .type = QEMU_OPT_STRING,
  856:                 .help = "PCI device address",
  857:             }, {
  858:                 .name = "vectors",
  859:                 .type = QEMU_OPT_NUMBER,
  860:                 .help = "number of MSI-x vectors, 0 to disable MSI-X",
  861:             },
  862:             { /* end of list */ }
  863:         },
  864:     },
  865: #ifdef CONFIG_SLIRP
  866:     [NET_CLIENT_TYPE_USER] = {
  867:         .type = "user",
  868:         .init = net_init_slirp,
  869:         .desc = {
  870:             NET_COMMON_PARAMS_DESC,
  871:             {
  872:                 .name = "hostname",
  873:                 .type = QEMU_OPT_STRING,
  874:                 .help = "client hostname reported by the builtin DHCP server",
  875:             }, {
  876:                 .name = "restrict",
  877:                 .type = QEMU_OPT_STRING,
  878:                 .help = "isolate the guest from the host (y|yes|n|no)",
  879:             }, {
  880:                 .name = "ip",
  881:                 .type = QEMU_OPT_STRING,
  882:                 .help = "legacy parameter, use net= instead",
  883:             }, {
  884:                 .name = "net",
  885:                 .type = QEMU_OPT_STRING,
  886:                 .help = "IP address and optional netmask",
  887:             }, {
  888:                 .name = "host",
  889:                 .type = QEMU_OPT_STRING,
  890:                 .help = "guest-visible address of the host",
  891:             }, {
  892:                 .name = "tftp",
  893:                 .type = QEMU_OPT_STRING,
  894:                 .help = "root directory of the built-in TFTP server",
  895:             }, {
  896:                 .name = "bootfile",
  897:                 .type = QEMU_OPT_STRING,
  898:                 .help = "BOOTP filename, for use with tftp=",
  899:             }, {
  900:                 .name = "dhcpstart",
  901:                 .type = QEMU_OPT_STRING,
  902:                 .help = "the first of the 16 IPs the built-in DHCP server can assign",
  903:             }, {
  904:                 .name = "dns",
  905:                 .type = QEMU_OPT_STRING,
  906:                 .help = "guest-visible address of the virtual nameserver",
  907:             }, {
  908:                 .name = "smb",
  909:                 .type = QEMU_OPT_STRING,
  910:                 .help = "root directory of the built-in SMB server",
  911:             }, {
  912:                 .name = "smbserver",
  913:                 .type = QEMU_OPT_STRING,
  914:                 .help = "IP address of the built-in SMB server",
  915:             }, {
  916:                 .name = "hostfwd",
  917:                 .type = QEMU_OPT_STRING,
  918:                 .help = "guest port number to forward incoming TCP or UDP connections",
  919:             }, {
  920:                 .name = "guestfwd",
  921:                 .type = QEMU_OPT_STRING,
  922:                 .help = "IP address and port to forward guest TCP connections",
  923:             },
  924:             { /* end of list */ }
  925:         },
  926:     },
  927: #endif
  928:     [NET_CLIENT_TYPE_TAP] = {
  929:         .type = "tap",
  930:         .init = net_init_tap,
  931:         .desc = {
  932:             NET_COMMON_PARAMS_DESC,
  933:             {
  934:                 .name = "ifname",
  935:                 .type = QEMU_OPT_STRING,
  936:                 .help = "interface name",
  937:             },
  938: #ifndef _WIN32
  939:             {
  940:                 .name = "fd",
  941:                 .type = QEMU_OPT_STRING,
  942:                 .help = "file descriptor of an already opened tap",
  943:             }, {
  944:                 .name = "script",
  945:                 .type = QEMU_OPT_STRING,
  946:                 .help = "script to initialize the interface",
  947:             }, {
  948:                 .name = "downscript",
  949:                 .type = QEMU_OPT_STRING,
  950:                 .help = "script to shut down the interface",
  951:             }, {
  952:                 .name = "sndbuf",
  953:                 .type = QEMU_OPT_SIZE,
  954:                 .help = "send buffer limit"
  955:             }, {
  956:                 .name = "vnet_hdr",
  957:                 .type = QEMU_OPT_BOOL,
  958:                 .help = "enable the IFF_VNET_HDR flag on the tap interface"
  959:             }, {
  960:                 .name = "vhost",
  961:                 .type = QEMU_OPT_BOOL,
  962:                 .help = "enable vhost-net network accelerator",
  963:             }, {
  964:                 .name = "vhostfd",
  965:                 .type = QEMU_OPT_STRING,
  966:                 .help = "file descriptor of an already opened vhost net device",
  967:             }, {
  968:                 .name = "vhostforce",
  969:                 .type = QEMU_OPT_BOOL,
  970:                 .help = "force vhost on for non-MSIX virtio guests",
  971:         },
  972: #endif /* _WIN32 */
  973:             { /* end of list */ }
  974:         },
  975:     },
  976:     [NET_CLIENT_TYPE_SOCKET] = {
  977:         .type = "socket",
  978:         .init = net_init_socket,
  979:         .desc = {
  980:             NET_COMMON_PARAMS_DESC,
  981:             {
  982:                 .name = "fd",
  983:                 .type = QEMU_OPT_STRING,
  984:                 .help = "file descriptor of an already opened socket",
  985:             }, {
  986:                 .name = "listen",
  987:                 .type = QEMU_OPT_STRING,
  988:                 .help = "port number, and optional hostname, to listen on",
  989:             }, {
  990:                 .name = "connect",
  991:                 .type = QEMU_OPT_STRING,
  992:                 .help = "port number, and optional hostname, to connect to",
  993:             }, {
  994:                 .name = "mcast",
  995:                 .type = QEMU_OPT_STRING,
  996:                 .help = "UDP multicast address and port number",
  997:             }, {
  998:                 .name = "localaddr",
  999:                 .type = QEMU_OPT_STRING,
 1000:                 .help = "source address for multicast packets",
 1001:             },
 1002:             { /* end of list */ }
 1003:         },
 1004:     },
 1005: #ifdef CONFIG_VDE
 1006:     [NET_CLIENT_TYPE_VDE] = {
 1007:         .type = "vde",
 1008:         .init = net_init_vde,
 1009:         .desc = {
 1010:             NET_COMMON_PARAMS_DESC,
 1011:             {
 1012:                 .name = "sock",
 1013:                 .type = QEMU_OPT_STRING,
 1014:                 .help = "socket path",
 1015:             }, {
 1016:                 .name = "port",
 1017:                 .type = QEMU_OPT_NUMBER,
 1018:                 .help = "port number",
 1019:             }, {
 1020:                 .name = "group",
 1021:                 .type = QEMU_OPT_STRING,
 1022:                 .help = "group owner of socket",
 1023:             }, {
 1024:                 .name = "mode",
 1025:                 .type = QEMU_OPT_NUMBER,
 1026:                 .help = "permissions for socket",
 1027:             },
 1028:             { /* end of list */ }
 1029:         },
 1030:     },
 1031: #endif
 1032:     [NET_CLIENT_TYPE_DUMP] = {
 1033:         .type = "dump",
 1034:         .init = net_init_dump,
 1035:         .desc = {
 1036:             NET_COMMON_PARAMS_DESC,
 1037:             {
 1038:                 .name = "len",
 1039:                 .type = QEMU_OPT_SIZE,
 1040:                 .help = "per-packet size limit (64k default)",
 1041:             }, {
 1042:                 .name = "file",
 1043:                 .type = QEMU_OPT_STRING,
 1044:                 .help = "dump file path (default is qemu-vlan0.pcap)",
 1045:             },
 1046:             { /* end of list */ }
 1047:         },
 1048:     },
 1049: };
 1050: 
 1051: int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
 1052: {
 1053:     const char *name;
 1054:     const char *type;
 1055:     int i;
 1056: 
 1057:     type = qemu_opt_get(opts, "type");
 1058:     if (!type) {
 1059:         qerror_report(QERR_MISSING_PARAMETER, "type");
 1060:         return -1;
 1061:     }
 1062: 
 1063:     if (is_netdev) {
 1064:         if (strcmp(type, "tap") != 0 &&
 1065: #ifdef CONFIG_SLIRP
 1066:             strcmp(type, "user") != 0 &&
 1067: #endif
 1068: #ifdef CONFIG_VDE
 1069:             strcmp(type, "vde") != 0 &&
 1070: #endif
 1071:             strcmp(type, "socket") != 0) {
 1072:             qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
 1073:                           "a netdev backend type");
 1074:             return -1;
 1075:         }
 1076: 
 1077:         if (qemu_opt_get(opts, "vlan")) {
 1078:             qerror_report(QERR_INVALID_PARAMETER, "vlan");
 1079:             return -1;
 1080:         }
 1081:         if (qemu_opt_get(opts, "name")) {
 1082:             qerror_report(QERR_INVALID_PARAMETER, "name");
 1083:             return -1;
 1084:         }
 1085:         if (!qemu_opts_id(opts)) {
 1086:             qerror_report(QERR_MISSING_PARAMETER, "id");
 1087:             return -1;
 1088:         }
 1089:     }
 1090: 
 1091:     name = qemu_opts_id(opts);
 1092:     if (!name) {
 1093:         name = qemu_opt_get(opts, "name");
 1094:     }
 1095: 
 1096:     for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
 1097:         if (net_client_types[i].type != NULL &&
 1098:             !strcmp(net_client_types[i].type, type)) {
 1099:             VLANState *vlan = NULL;
 1100:             int ret;
 1101: 
 1102:             if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
 1103:                 return -1;
 1104:             }
 1105: 
 1106:             /* Do not add to a vlan if it's a -netdev or a nic with a
 1107:              * netdev= parameter. */
 1108:             if (!(is_netdev ||
 1109:                   (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
 1110:                 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
 1111:             }
 1112: 
 1113:             ret = 0;
 1114:             if (net_client_types[i].init) {
 1115:                 ret = net_client_types[i].init(opts, mon, name, vlan);
 1116:                 if (ret < 0) {
 1117:                     /* TODO push error reporting into init() methods */
 1118:                     qerror_report(QERR_DEVICE_INIT_FAILED, type);
 1119:                     return -1;
 1120:                 }
 1121:             }
 1122:             return ret;
 1123:         }
 1124:     }
 1125: 
 1126:     qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
 1127:                   "a network client type");
 1128:     return -1;
 1129: }
 1130: 
 1131: static int net_host_check_device(const char *device)
 1132: {
 1133:     int i;
 1134:     const char *valid_param_list[] = { "tap", "socket", "dump"
 1135: #ifdef CONFIG_SLIRP
 1136:                                        ,"user"
 1137: #endif
 1138: #ifdef CONFIG_VDE
 1139:                                        ,"vde"
 1140: #endif
 1141:     };
 1142:     for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
 1143:         if (!strncmp(valid_param_list[i], device,
 1144:                      strlen(valid_param_list[i])))
 1145:             return 1;
 1146:     }
 1147: 
 1148:     return 0;
 1149: }
 1150: 
 1151: void net_host_device_add(Monitor *mon, const QDict *qdict)
 1152: {
 1153:     const char *device = qdict_get_str(qdict, "device");
 1154:     const char *opts_str = qdict_get_try_str(qdict, "opts");
 1155:     QemuOpts *opts;
 1156: 
 1157:     if (!net_host_check_device(device)) {
 1158:         monitor_printf(mon, "invalid host network device %s\n", device);
 1159:         return;
 1160:     }
 1161: 
 1162:     opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
 1163:     if (!opts) {
 1164:         return;
 1165:     }
 1166: 
 1167:     qemu_opt_set(opts, "type", device);
 1168: 
 1169:     if (net_client_init(mon, opts, 0) < 0) {
 1170:         monitor_printf(mon, "adding host network device %s failed\n", device);
 1171:     }
 1172: }
 1173: 
 1174: void net_host_device_remove(Monitor *mon, const QDict *qdict)
 1175: {
 1176:     VLANClientState *vc;
 1177:     int vlan_id = qdict_get_int(qdict, "vlan_id");
 1178:     const char *device = qdict_get_str(qdict, "device");
 1179: 
 1180:     vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
 1181:     if (!vc) {
 1182:         return;
 1183:     }
 1184:     if (!net_host_check_device(vc->model)) {
 1185:         monitor_printf(mon, "invalid host network device %s\n", device);
 1186:         return;
 1187:     }
 1188:     qemu_del_vlan_client(vc);
 1189: }
 1190: 
 1191: int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 1192: {
 1193:     QemuOpts *opts;
 1194:     int res;
 1195: 
 1196:     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
 1197:     if (!opts) {
 1198:         return -1;
 1199:     }
 1200: 
 1201:     res = net_client_init(mon, opts, 1);
 1202:     if (res < 0) {
 1203:         qemu_opts_del(opts);
 1204:     }
 1205: 
 1206:     return res;
 1207: }
 1208: 
 1209: int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
 1210: {
 1211:     const char *id = qdict_get_str(qdict, "id");
 1212:     VLANClientState *vc;
 1213: 
 1214:     vc = qemu_find_netdev(id);
 1215:     if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
 1216:         qerror_report(QERR_DEVICE_NOT_FOUND, id);
 1217:         return -1;
 1218:     }
 1219:     qemu_del_vlan_client(vc);
 1220:     qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
 1221:     return 0;
 1222: }
 1223: 
 1224: static void print_net_client(Monitor *mon, VLANClientState *vc)
 1225: {
 1226:     monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
 1227:                    net_client_types[vc->info->type].type, vc->info_str);
 1228: }
 1229: 
 1230: void do_info_network(Monitor *mon)
 1231: {
 1232:     VLANState *vlan;
 1233:     VLANClientState *vc, *peer;
 1234:     net_client_type type;
 1235: 
 1236:     QTAILQ_FOREACH(vlan, &vlans, next) {
 1237:         monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
 1238: 
 1239:         QTAILQ_FOREACH(vc, &vlan->clients, next) {
 1240:             monitor_printf(mon, "  ");
 1241:             print_net_client(mon, vc);
 1242:         }
 1243:     }
 1244:     monitor_printf(mon, "Devices not on any VLAN:\n");
 1245:     QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
 1246:         peer = vc->peer;
 1247:         type = vc->info->type;
 1248:         if (!peer || type == NET_CLIENT_TYPE_NIC) {
 1249:             monitor_printf(mon, "  ");
 1250:             print_net_client(mon, vc);
 1251:         } /* else it's a netdev connected to a NIC, printed with the NIC */
 1252:         if (peer && type == NET_CLIENT_TYPE_NIC) {
 1253:             monitor_printf(mon, "   \\ ");
 1254:             print_net_client(mon, peer);
 1255:         }
 1256:     }
 1257: }
 1258: 
 1259: int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
 1260: {
 1261:     VLANState *vlan;
 1262:     VLANClientState *vc = NULL;
 1263:     const char *name = qdict_get_str(qdict, "name");
 1264:     int up = qdict_get_bool(qdict, "up");
 1265: 
 1266:     QTAILQ_FOREACH(vlan, &vlans, next) {
 1267:         QTAILQ_FOREACH(vc, &vlan->clients, next) {
 1268:             if (strcmp(vc->name, name) == 0) {
 1269:                 goto done;
 1270:             }
 1271:         }
 1272:     }
 1273:     vc = qemu_find_netdev(name);
 1274: done:
 1275: 
 1276:     if (!vc) {
 1277:         qerror_report(QERR_DEVICE_NOT_FOUND, name);
 1278:         return -1;
 1279:     }
 1280: 
 1281:     vc->link_down = !up;
 1282: 
 1283:     if (vc->info->link_status_changed) {
 1284:         vc->info->link_status_changed(vc);
 1285:     }
 1286: 
 1287:     /* Notify peer. Don't update peer link status: this makes it possible to
 1288:      * disconnect from host network without notifying the guest.
 1289:      * FIXME: is disconnected link status change operation useful?
 1290:      *
 1291:      * Current behaviour is compatible with qemu vlans where there could be
 1292:      * multiple clients that can still communicate with each other in
 1293:      * disconnected mode. For now maintain this compatibility. */
 1294:     if (vc->peer && vc->peer->info->link_status_changed) {
 1295:         vc->peer->info->link_status_changed(vc->peer);
 1296:     }
 1297:     return 0;
 1298: }
 1299: 
 1300: void net_cleanup(void)
 1301: {
 1302:     VLANState *vlan;
 1303:     VLANClientState *vc, *next_vc;
 1304: 
 1305:     QTAILQ_FOREACH(vlan, &vlans, next) {
 1306:         QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
 1307:             qemu_del_vlan_client(vc);
 1308:         }
 1309:     }
 1310: 
 1311:     QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
 1312:         qemu_del_vlan_client(vc);
 1313:     }
 1314: }
 1315: 
 1316: void net_check_clients(void)
 1317: {
 1318:     VLANState *vlan;
 1319:     VLANClientState *vc;
 1320:     int i;
 1321: 
 1322:     /* Don't warn about the default network setup that you get if
 1323:      * no command line -net or -netdev options are specified. There
 1324:      * are two cases that we would otherwise complain about:
 1325:      * (1) board doesn't support a NIC but the implicit "-net nic"
 1326:      * requested one
 1327:      * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
 1328:      * sets up a nic that isn't connected to anything.
 1329:      */
 1330:     if (default_net) {
 1331:         return;
 1332:     }
 1333: 
 1334:     QTAILQ_FOREACH(vlan, &vlans, next) {
 1335:         int has_nic = 0, has_host_dev = 0;
 1336: 
 1337:         QTAILQ_FOREACH(vc, &vlan->clients, next) {
 1338:             switch (vc->info->type) {
 1339:             case NET_CLIENT_TYPE_NIC:
 1340:                 has_nic = 1;
 1341:                 break;
 1342:             case NET_CLIENT_TYPE_USER:
 1343:             case NET_CLIENT_TYPE_TAP:
 1344:             case NET_CLIENT_TYPE_SOCKET:
 1345:             case NET_CLIENT_TYPE_VDE:
 1346:                 has_host_dev = 1;
 1347:                 break;
 1348:             default: ;
 1349:             }
 1350:         }
 1351:         if (has_host_dev && !has_nic)
 1352:             fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
 1353:         if (has_nic && !has_host_dev)
 1354:             fprintf(stderr,
 1355:                     "Warning: vlan %d is not connected to host network\n",
 1356:                     vlan->id);
 1357:     }
 1358:     QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
 1359:         if (!vc->peer) {
 1360:             fprintf(stderr, "Warning: %s %s has no peer\n",
 1361:                     vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
 1362:                     vc->name);
 1363:         }
 1364:     }
 1365: 
 1366:     /* Check that all NICs requested via -net nic actually got created.
 1367:      * NICs created via -device don't need to be checked here because
 1368:      * they are always instantiated.
 1369:      */
 1370:     for (i = 0; i < MAX_NICS; i++) {
 1371:         NICInfo *nd = &nd_table[i];
 1372:         if (nd->used && !nd->instantiated) {
 1373:             fprintf(stderr, "Warning: requested NIC (%s, model %s) "
 1374:                     "was not created (not supported by this machine?)\n",
 1375:                     nd->name ? nd->name : "anonymous",
 1376:                     nd->model ? nd->model : "unspecified");
 1377:         }
 1378:     }
 1379: }
 1380: 
 1381: static int net_init_client(QemuOpts *opts, void *dummy)
 1382: {
 1383:     if (net_client_init(NULL, opts, 0) < 0)
 1384:         return -1;
 1385:     return 0;
 1386: }
 1387: 
 1388: static int net_init_netdev(QemuOpts *opts, void *dummy)
 1389: {
 1390:     return net_client_init(NULL, opts, 1);
 1391: }
 1392: 
 1393: int net_init_clients(void)
 1394: {
 1395:     QemuOptsList *net = qemu_find_opts("net");
 1396: 
 1397:     if (default_net) {
 1398:         /* if no clients, we use a default config */
 1399:         qemu_opts_set(net, NULL, "type", "nic");
 1400: #ifdef CONFIG_SLIRP
 1401:         qemu_opts_set(net, NULL, "type", "user");
 1402: #endif
 1403:     }
 1404: 
 1405:     QTAILQ_INIT(&vlans);
 1406:     QTAILQ_INIT(&non_vlan_clients);
 1407: 
 1408:     if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
 1409:         return -1;
 1410: 
 1411:     if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
 1412:         return -1;
 1413:     }
 1414: 
 1415:     return 0;
 1416: }
 1417: 
 1418: int net_client_parse(QemuOptsList *opts_list, const char *optarg)
 1419: {
 1420: #if defined(CONFIG_SLIRP)
 1421:     int ret;
 1422:     if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
 1423:         return ret;
 1424:     }
 1425: #endif
 1426: 
 1427:     if (!qemu_opts_parse(opts_list, optarg, 1)) {
 1428:         return -1;
 1429:     }
 1430: 
 1431:     default_net = 0;
 1432:     return 0;
 1433: }

unix.superglobalmegacorp.com