Statistics
| Branch: | Revision:

root / net.c @ 68ac40d2

History | View | Annotate | Download (56.9 kB)

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 <unistd.h>
25
#include <fcntl.h>
26
#include <signal.h>
27
#include <time.h>
28
#include <errno.h>
29
#include <sys/time.h>
30
#include <zlib.h>
31

    
32
/* Needed early for CONFIG_BSD etc. */
33
#include "config-host.h"
34

    
35
#ifndef _WIN32
36
#include <sys/times.h>
37
#include <sys/wait.h>
38
#include <termios.h>
39
#include <sys/mman.h>
40
#include <sys/ioctl.h>
41
#include <sys/resource.h>
42
#include <sys/socket.h>
43
#include <net/if.h>
44
#include <dirent.h>
45
#include <netdb.h>
46
#include <sys/select.h>
47
#ifdef CONFIG_BSD
48
#include <sys/stat.h>
49
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
50
#include <libutil.h>
51
#else
52
#include <util.h>
53
#endif
54
#ifdef __linux__
55
#include <pty.h>
56
#include <malloc.h>
57
#include <linux/rtc.h>
58

    
59
/* For the benefit of older linux systems which don't supply it,
60
   we use a local copy of hpet.h. */
61
/* #include <linux/hpet.h> */
62
#include "hpet.h"
63

    
64
#include <linux/ppdev.h>
65
#include <linux/parport.h>
66
#endif
67
#ifdef __sun__
68
#include <sys/stat.h>
69
#include <sys/ethernet.h>
70
#include <sys/sockio.h>
71
#include <netinet/arp.h>
72
#include <netinet/in.h>
73
#include <netinet/in_systm.h>
74
#include <netinet/ip.h>
75
#include <netinet/ip_icmp.h> // must come after ip.h
76
#include <netinet/udp.h>
77
#include <netinet/tcp.h>
78
#include <net/if.h>
79
#include <syslog.h>
80
#include <stropts.h>
81
#endif
82
#endif
83
#endif
84

    
85
#if defined(__OpenBSD__)
86
#include <util.h>
87
#endif
88

    
89
#if defined(CONFIG_VDE)
90
#include <libvdeplug.h>
91
#endif
92

    
93
#include "qemu-common.h"
94
#include "net.h"
95
#include "net/tap.h"
96
#include "net/slirp.h"
97
#include "monitor.h"
98
#include "sysemu.h"
99
#include "qemu-timer.h"
100
#include "qemu-char.h"
101
#include "audio/audio.h"
102
#include "qemu_socket.h"
103
#include "qemu-log.h"
104
#include "qemu-config.h"
105

    
106
static QTAILQ_HEAD(, VLANState) vlans;
107
static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
108

    
109
/***********************************************************/
110
/* network device redirectors */
111

    
112
#if defined(DEBUG_NET)
113
static void hex_dump(FILE *f, const uint8_t *buf, int size)
114
{
115
    int len, i, j, c;
116

    
117
    for(i=0;i<size;i+=16) {
118
        len = size - i;
119
        if (len > 16)
120
            len = 16;
121
        fprintf(f, "%08x ", i);
122
        for(j=0;j<16;j++) {
123
            if (j < len)
124
                fprintf(f, " %02x", buf[i+j]);
125
            else
126
                fprintf(f, "   ");
127
        }
128
        fprintf(f, " ");
129
        for(j=0;j<len;j++) {
130
            c = buf[i+j];
131
            if (c < ' ' || c > '~')
132
                c = '.';
133
            fprintf(f, "%c", c);
134
        }
135
        fprintf(f, "\n");
136
    }
137
}
138
#endif
139

    
140
static int parse_macaddr(uint8_t *macaddr, const char *p)
141
{
142
    int i;
143
    char *last_char;
144
    long int offset;
145

    
146
    errno = 0;
147
    offset = strtol(p, &last_char, 0);    
148
    if (0 == errno && '\0' == *last_char &&
149
            offset >= 0 && offset <= 0xFFFFFF) {
150
        macaddr[3] = (offset & 0xFF0000) >> 16;
151
        macaddr[4] = (offset & 0xFF00) >> 8;
152
        macaddr[5] = offset & 0xFF;
153
        return 0;
154
    } else {
155
        for(i = 0; i < 6; i++) {
156
            macaddr[i] = strtol(p, (char **)&p, 16);
157
            if (i == 5) {
158
                if (*p != '\0')
159
                    return -1;
160
            } else {
161
                if (*p != ':' && *p != '-')
162
                    return -1;
163
                p++;
164
            }
165
        }
166
        return 0;    
167
    }
168

    
169
    return -1;
170
}
171

    
172
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
173
{
174
    const char *p, *p1;
175
    int len;
176
    p = *pp;
177
    p1 = strchr(p, sep);
178
    if (!p1)
179
        return -1;
180
    len = p1 - p;
181
    p1++;
182
    if (buf_size > 0) {
183
        if (len > buf_size - 1)
184
            len = buf_size - 1;
185
        memcpy(buf, p, len);
186
        buf[len] = '\0';
187
    }
188
    *pp = p1;
189
    return 0;
190
}
191

    
192
int parse_host_src_port(struct sockaddr_in *haddr,
193
                        struct sockaddr_in *saddr,
194
                        const char *input_str)
195
{
196
    char *str = strdup(input_str);
197
    char *host_str = str;
198
    char *src_str;
199
    const char *src_str2;
200
    char *ptr;
201

    
202
    /*
203
     * Chop off any extra arguments at the end of the string which
204
     * would start with a comma, then fill in the src port information
205
     * if it was provided else use the "any address" and "any port".
206
     */
207
    if ((ptr = strchr(str,',')))
208
        *ptr = '\0';
209

    
210
    if ((src_str = strchr(input_str,'@'))) {
211
        *src_str = '\0';
212
        src_str++;
213
    }
214

    
215
    if (parse_host_port(haddr, host_str) < 0)
216
        goto fail;
217

    
218
    src_str2 = src_str;
219
    if (!src_str || *src_str == '\0')
220
        src_str2 = ":0";
221

    
222
    if (parse_host_port(saddr, src_str2) < 0)
223
        goto fail;
224

    
225
    free(str);
226
    return(0);
227

    
228
fail:
229
    free(str);
230
    return -1;
231
}
232

    
233
int parse_host_port(struct sockaddr_in *saddr, const char *str)
234
{
235
    char buf[512];
236
    struct hostent *he;
237
    const char *p, *r;
238
    int port;
239

    
240
    p = str;
241
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
242
        return -1;
243
    saddr->sin_family = AF_INET;
244
    if (buf[0] == '\0') {
245
        saddr->sin_addr.s_addr = 0;
246
    } else {
247
        if (qemu_isdigit(buf[0])) {
248
            if (!inet_aton(buf, &saddr->sin_addr))
249
                return -1;
250
        } else {
251
            if ((he = gethostbyname(buf)) == NULL)
252
                return - 1;
253
            saddr->sin_addr = *(struct in_addr *)he->h_addr;
254
        }
255
    }
256
    port = strtol(p, (char **)&r, 0);
257
    if (r == p)
258
        return -1;
259
    saddr->sin_port = htons(port);
260
    return 0;
261
}
262

    
263
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
264
{
265
    snprintf(vc->info_str, sizeof(vc->info_str),
266
             "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
267
             vc->model,
268
             macaddr[0], macaddr[1], macaddr[2],
269
             macaddr[3], macaddr[4], macaddr[5]);
270
}
271

    
272
void qemu_macaddr_default_if_unset(MACAddr *macaddr)
273
{
274
    static int index = 0;
275
    static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
276

    
277
    if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
278
        return;
279
    macaddr->a[0] = 0x52;
280
    macaddr->a[1] = 0x54;
281
    macaddr->a[2] = 0x00;
282
    macaddr->a[3] = 0x12;
283
    macaddr->a[4] = 0x34;
284
    macaddr->a[5] = 0x56 + index++;
285
}
286

    
287
static char *assign_name(VLANClientState *vc1, const char *model)
288
{
289
    VLANState *vlan;
290
    char buf[256];
291
    int id = 0;
292

    
293
    QTAILQ_FOREACH(vlan, &vlans, next) {
294
        VLANClientState *vc;
295

    
296
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
297
            if (vc != vc1 && strcmp(vc->model, model) == 0) {
298
                id++;
299
            }
300
        }
301
    }
302

    
303
    snprintf(buf, sizeof(buf), "%s.%d", model, id);
304

    
305
    return qemu_strdup(buf);
306
}
307

    
308
static ssize_t qemu_deliver_packet(VLANClientState *sender,
309
                                   unsigned flags,
310
                                   const uint8_t *data,
311
                                   size_t size,
312
                                   void *opaque);
313
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
314
                                       unsigned flags,
315
                                       const struct iovec *iov,
316
                                       int iovcnt,
317
                                       void *opaque);
318

    
319
VLANClientState *qemu_new_vlan_client(net_client_type type,
320
                                      VLANState *vlan,
321
                                      VLANClientState *peer,
322
                                      const char *model,
323
                                      const char *name,
324
                                      NetCanReceive *can_receive,
325
                                      NetReceive *receive,
326
                                      NetReceive *receive_raw,
327
                                      NetReceiveIOV *receive_iov,
328
                                      NetCleanup *cleanup,
329
                                      void *opaque)
330
{
331
    VLANClientState *vc;
332

    
333
    vc = qemu_mallocz(sizeof(VLANClientState));
334

    
335
    vc->type = type;
336
    vc->model = qemu_strdup(model);
337
    if (name)
338
        vc->name = qemu_strdup(name);
339
    else
340
        vc->name = assign_name(vc, model);
341
    vc->can_receive = can_receive;
342
    vc->receive = receive;
343
    vc->receive_raw = receive_raw;
344
    vc->receive_iov = receive_iov;
345
    vc->cleanup = cleanup;
346
    vc->opaque = opaque;
347

    
348
    if (vlan) {
349
        assert(!peer);
350
        vc->vlan = vlan;
351
        QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
352
    } else {
353
        if (peer) {
354
            vc->peer = peer;
355
            peer->peer = vc;
356
        }
357
        QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
358

    
359
        vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
360
                                            qemu_deliver_packet_iov,
361
                                            vc);
362
    }
363

    
364
    return vc;
365
}
366

    
367
void qemu_del_vlan_client(VLANClientState *vc)
368
{
369
    if (vc->vlan) {
370
        QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
371
    } else {
372
        if (vc->send_queue) {
373
            qemu_del_net_queue(vc->send_queue);
374
        }
375
        QTAILQ_REMOVE(&non_vlan_clients, vc, next);
376
        if (vc->peer) {
377
            vc->peer->peer = NULL;
378
        }
379
    }
380

    
381
    if (vc->cleanup) {
382
        vc->cleanup(vc);
383
    }
384

    
385
    qemu_free(vc->name);
386
    qemu_free(vc->model);
387
    qemu_free(vc);
388
}
389

    
390
VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
391
{
392
    VLANClientState *vc;
393

    
394
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
395
        if (vc->opaque == opaque) {
396
            return vc;
397
        }
398
    }
399

    
400
    return NULL;
401
}
402

    
403
VLANClientState *
404
qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
405
                              const char *client_str)
406
{
407
    VLANState *vlan;
408
    VLANClientState *vc;
409

    
410
    vlan = qemu_find_vlan(vlan_id, 0);
411
    if (!vlan) {
412
        monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
413
        return NULL;
414
    }
415

    
416
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
417
        if (!strcmp(vc->name, client_str)) {
418
            break;
419
        }
420
    }
421
    if (!vc) {
422
        monitor_printf(mon, "can't find device %s on VLAN %d\n",
423
                       client_str, vlan_id);
424
    }
425

    
426
    return vc;
427
}
428

    
429
int qemu_can_send_packet(VLANClientState *sender)
430
{
431
    VLANState *vlan = sender->vlan;
432
    VLANClientState *vc;
433

    
434
    if (sender->peer) {
435
        if (sender->peer->receive_disabled) {
436
            return 0;
437
        } else if (sender->peer->can_receive &&
438
                   !sender->peer->can_receive(sender->peer)) {
439
            return 0;
440
        } else {
441
            return 1;
442
        }
443
    }
444

    
445
    if (!sender->vlan) {
446
        return 1;
447
    }
448

    
449
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
450
        if (vc == sender) {
451
            continue;
452
        }
453

    
454
        /* no can_receive() handler, they can always receive */
455
        if (!vc->can_receive || vc->can_receive(vc)) {
456
            return 1;
457
        }
458
    }
459
    return 0;
460
}
461

    
462
static ssize_t qemu_deliver_packet(VLANClientState *sender,
463
                                   unsigned flags,
464
                                   const uint8_t *data,
465
                                   size_t size,
466
                                   void *opaque)
467
{
468
    VLANClientState *vc = opaque;
469
    ssize_t ret;
470

    
471
    if (vc->link_down) {
472
        return size;
473
    }
474

    
475
    if (vc->receive_disabled) {
476
        return 0;
477
    }
478

    
479
    if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
480
        ret = vc->receive_raw(vc, data, size);
481
    } else {
482
        ret = vc->receive(vc, data, size);
483
    }
484

    
485
    if (ret == 0) {
486
        vc->receive_disabled = 1;
487
    };
488

    
489
    return ret;
490
}
491

    
492
static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
493
                                        unsigned flags,
494
                                        const uint8_t *buf,
495
                                        size_t size,
496
                                        void *opaque)
497
{
498
    VLANState *vlan = opaque;
499
    VLANClientState *vc;
500
    ssize_t ret = -1;
501

    
502
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
503
        ssize_t len;
504

    
505
        if (vc == sender) {
506
            continue;
507
        }
508

    
509
        if (vc->link_down) {
510
            ret = size;
511
            continue;
512
        }
513

    
514
        if (vc->receive_disabled) {
515
            ret = 0;
516
            continue;
517
        }
518

    
519
        if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
520
            len = vc->receive_raw(vc, buf, size);
521
        } else {
522
            len = vc->receive(vc, buf, size);
523
        }
524

    
525
        if (len == 0) {
526
            vc->receive_disabled = 1;
527
        }
528

    
529
        ret = (ret >= 0) ? ret : len;
530

    
531
    }
532

    
533
    return ret;
534
}
535

    
536
void qemu_purge_queued_packets(VLANClientState *vc)
537
{
538
    NetQueue *queue;
539

    
540
    if (!vc->peer && !vc->vlan) {
541
        return;
542
    }
543

    
544
    if (vc->peer) {
545
        queue = vc->peer->send_queue;
546
    } else {
547
        queue = vc->vlan->send_queue;
548
    }
549

    
550
    qemu_net_queue_purge(queue, vc);
551
}
552

    
553
void qemu_flush_queued_packets(VLANClientState *vc)
554
{
555
    NetQueue *queue;
556

    
557
    vc->receive_disabled = 0;
558

    
559
    if (vc->vlan) {
560
        queue = vc->vlan->send_queue;
561
    } else {
562
        queue = vc->send_queue;
563
    }
564

    
565
    qemu_net_queue_flush(queue);
566
}
567

    
568
static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
569
                                                 unsigned flags,
570
                                                 const uint8_t *buf, int size,
571
                                                 NetPacketSent *sent_cb)
572
{
573
    NetQueue *queue;
574

    
575
#ifdef DEBUG_NET
576
    printf("qemu_send_packet_async:\n");
577
    hex_dump(stdout, buf, size);
578
#endif
579

    
580
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
581
        return size;
582
    }
583

    
584
    if (sender->peer) {
585
        queue = sender->peer->send_queue;
586
    } else {
587
        queue = sender->vlan->send_queue;
588
    }
589

    
590
    return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
591
}
592

    
593
ssize_t qemu_send_packet_async(VLANClientState *sender,
594
                               const uint8_t *buf, int size,
595
                               NetPacketSent *sent_cb)
596
{
597
    return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
598
                                             buf, size, sent_cb);
599
}
600

    
601
void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
602
{
603
    qemu_send_packet_async(vc, buf, size, NULL);
604
}
605

    
606
ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
607
{
608
    return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
609
                                             buf, size, NULL);
610
}
611

    
612
static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
613
                               int iovcnt)
614
{
615
    uint8_t buffer[4096];
616
    size_t offset = 0;
617
    int i;
618

    
619
    for (i = 0; i < iovcnt; i++) {
620
        size_t len;
621

    
622
        len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
623
        memcpy(buffer + offset, iov[i].iov_base, len);
624
        offset += len;
625
    }
626

    
627
    return vc->receive(vc, buffer, offset);
628
}
629

    
630
static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
631
{
632
    size_t offset = 0;
633
    int i;
634

    
635
    for (i = 0; i < iovcnt; i++)
636
        offset += iov[i].iov_len;
637
    return offset;
638
}
639

    
640
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
641
                                       unsigned flags,
642
                                       const struct iovec *iov,
643
                                       int iovcnt,
644
                                       void *opaque)
645
{
646
    VLANClientState *vc = opaque;
647

    
648
    if (vc->link_down) {
649
        return calc_iov_length(iov, iovcnt);
650
    }
651

    
652
    if (vc->receive_iov) {
653
        return vc->receive_iov(vc, iov, iovcnt);
654
    } else {
655
        return vc_sendv_compat(vc, iov, iovcnt);
656
    }
657
}
658

    
659
static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
660
                                            unsigned flags,
661
                                            const struct iovec *iov,
662
                                            int iovcnt,
663
                                            void *opaque)
664
{
665
    VLANState *vlan = opaque;
666
    VLANClientState *vc;
667
    ssize_t ret = -1;
668

    
669
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
670
        ssize_t len;
671

    
672
        if (vc == sender) {
673
            continue;
674
        }
675

    
676
        if (vc->link_down) {
677
            ret = calc_iov_length(iov, iovcnt);
678
            continue;
679
        }
680

    
681
        assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
682

    
683
        if (vc->receive_iov) {
684
            len = vc->receive_iov(vc, iov, iovcnt);
685
        } else {
686
            len = vc_sendv_compat(vc, iov, iovcnt);
687
        }
688

    
689
        ret = (ret >= 0) ? ret : len;
690
    }
691

    
692
    return ret;
693
}
694

    
695
ssize_t qemu_sendv_packet_async(VLANClientState *sender,
696
                                const struct iovec *iov, int iovcnt,
697
                                NetPacketSent *sent_cb)
698
{
699
    NetQueue *queue;
700

    
701
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
702
        return calc_iov_length(iov, iovcnt);
703
    }
704

    
705
    if (sender->peer) {
706
        queue = sender->peer->send_queue;
707
    } else {
708
        queue = sender->vlan->send_queue;
709
    }
710

    
711
    return qemu_net_queue_send_iov(queue, sender,
712
                                   QEMU_NET_PACKET_FLAG_NONE,
713
                                   iov, iovcnt, sent_cb);
714
}
715

    
716
ssize_t
717
qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
718
{
719
    return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
720
}
721

    
722
#if defined(CONFIG_VDE)
723
typedef struct VDEState {
724
    VLANClientState *vc;
725
    VDECONN *vde;
726
} VDEState;
727

    
728
static void vde_to_qemu(void *opaque)
729
{
730
    VDEState *s = opaque;
731
    uint8_t buf[4096];
732
    int size;
733

    
734
    size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
735
    if (size > 0) {
736
        qemu_send_packet(s->vc, buf, size);
737
    }
738
}
739

    
740
static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
741
{
742
    VDEState *s = vc->opaque;
743
    ssize_t ret;
744

    
745
    do {
746
      ret = vde_send(s->vde, (const char *)buf, size, 0);
747
    } while (ret < 0 && errno == EINTR);
748

    
749
    return ret;
750
}
751

    
752
static void vde_cleanup(VLANClientState *vc)
753
{
754
    VDEState *s = vc->opaque;
755
    qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
756
    vde_close(s->vde);
757
    qemu_free(s);
758
}
759

    
760
static int net_vde_init(VLANState *vlan, const char *model,
761
                        const char *name, const char *sock,
762
                        int port, const char *group, int mode)
763
{
764
    VDEState *s;
765
    char *init_group = (char *)group;
766
    char *init_sock = (char *)sock;
767

    
768
    struct vde_open_args args = {
769
        .port = port,
770
        .group = init_group,
771
        .mode = mode,
772
    };
773

    
774
    s = qemu_mallocz(sizeof(VDEState));
775
    s->vde = vde_open(init_sock, (char *)"QEMU", &args);
776
    if (!s->vde){
777
        free(s);
778
        return -1;
779
    }
780
    s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_VDE,
781
                                 vlan, NULL, model, name, NULL,
782
                                 vde_receive, NULL, NULL,
783
                                 vde_cleanup, s);
784
    qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
785
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
786
             sock, vde_datafd(s->vde));
787
    return 0;
788
}
789
#endif
790

    
791
/* network connection */
792
typedef struct NetSocketState {
793
    VLANClientState *vc;
794
    int fd;
795
    int state; /* 0 = getting length, 1 = getting data */
796
    unsigned int index;
797
    unsigned int packet_len;
798
    uint8_t buf[4096];
799
    struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
800
} NetSocketState;
801

    
802
typedef struct NetSocketListenState {
803
    VLANState *vlan;
804
    char *model;
805
    char *name;
806
    int fd;
807
} NetSocketListenState;
808

    
809
/* XXX: we consider we can send the whole packet without blocking */
810
static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
811
{
812
    NetSocketState *s = vc->opaque;
813
    uint32_t len;
814
    len = htonl(size);
815

    
816
    send_all(s->fd, (const uint8_t *)&len, sizeof(len));
817
    return send_all(s->fd, buf, size);
818
}
819

    
820
static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
821
{
822
    NetSocketState *s = vc->opaque;
823

    
824
    return sendto(s->fd, (const void *)buf, size, 0,
825
                  (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
826
}
827

    
828
static void net_socket_send(void *opaque)
829
{
830
    NetSocketState *s = opaque;
831
    int size, err;
832
    unsigned l;
833
    uint8_t buf1[4096];
834
    const uint8_t *buf;
835

    
836
    size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
837
    if (size < 0) {
838
        err = socket_error();
839
        if (err != EWOULDBLOCK)
840
            goto eoc;
841
    } else if (size == 0) {
842
        /* end of connection */
843
    eoc:
844
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
845
        closesocket(s->fd);
846
        return;
847
    }
848
    buf = buf1;
849
    while (size > 0) {
850
        /* reassemble a packet from the network */
851
        switch(s->state) {
852
        case 0:
853
            l = 4 - s->index;
854
            if (l > size)
855
                l = size;
856
            memcpy(s->buf + s->index, buf, l);
857
            buf += l;
858
            size -= l;
859
            s->index += l;
860
            if (s->index == 4) {
861
                /* got length */
862
                s->packet_len = ntohl(*(uint32_t *)s->buf);
863
                s->index = 0;
864
                s->state = 1;
865
            }
866
            break;
867
        case 1:
868
            l = s->packet_len - s->index;
869
            if (l > size)
870
                l = size;
871
            if (s->index + l <= sizeof(s->buf)) {
872
                memcpy(s->buf + s->index, buf, l);
873
            } else {
874
                fprintf(stderr, "serious error: oversized packet received,"
875
                    "connection terminated.\n");
876
                s->state = 0;
877
                goto eoc;
878
            }
879

    
880
            s->index += l;
881
            buf += l;
882
            size -= l;
883
            if (s->index >= s->packet_len) {
884
                qemu_send_packet(s->vc, s->buf, s->packet_len);
885
                s->index = 0;
886
                s->state = 0;
887
            }
888
            break;
889
        }
890
    }
891
}
892

    
893
static void net_socket_send_dgram(void *opaque)
894
{
895
    NetSocketState *s = opaque;
896
    int size;
897

    
898
    size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
899
    if (size < 0)
900
        return;
901
    if (size == 0) {
902
        /* end of connection */
903
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
904
        return;
905
    }
906
    qemu_send_packet(s->vc, s->buf, size);
907
}
908

    
909
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
910
{
911
    struct ip_mreq imr;
912
    int fd;
913
    int val, ret;
914
    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
915
        fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
916
                inet_ntoa(mcastaddr->sin_addr),
917
                (int)ntohl(mcastaddr->sin_addr.s_addr));
918
        return -1;
919

    
920
    }
921
    fd = socket(PF_INET, SOCK_DGRAM, 0);
922
    if (fd < 0) {
923
        perror("socket(PF_INET, SOCK_DGRAM)");
924
        return -1;
925
    }
926

    
927
    val = 1;
928
    ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
929
                   (const char *)&val, sizeof(val));
930
    if (ret < 0) {
931
        perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
932
        goto fail;
933
    }
934

    
935
    ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
936
    if (ret < 0) {
937
        perror("bind");
938
        goto fail;
939
    }
940

    
941
    /* Add host to multicast group */
942
    imr.imr_multiaddr = mcastaddr->sin_addr;
943
    imr.imr_interface.s_addr = htonl(INADDR_ANY);
944

    
945
    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
946
                     (const char *)&imr, sizeof(struct ip_mreq));
947
    if (ret < 0) {
948
        perror("setsockopt(IP_ADD_MEMBERSHIP)");
949
        goto fail;
950
    }
951

    
952
    /* Force mcast msgs to loopback (eg. several QEMUs in same host */
953
    val = 1;
954
    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
955
                   (const char *)&val, sizeof(val));
956
    if (ret < 0) {
957
        perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
958
        goto fail;
959
    }
960

    
961
    socket_set_nonblock(fd);
962
    return fd;
963
fail:
964
    if (fd >= 0)
965
        closesocket(fd);
966
    return -1;
967
}
968

    
969
static void net_socket_cleanup(VLANClientState *vc)
970
{
971
    NetSocketState *s = vc->opaque;
972
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
973
    close(s->fd);
974
    qemu_free(s);
975
}
976

    
977
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
978
                                                const char *model,
979
                                                const char *name,
980
                                                int fd, int is_connected)
981
{
982
    struct sockaddr_in saddr;
983
    int newfd;
984
    socklen_t saddr_len;
985
    NetSocketState *s;
986

    
987
    /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
988
     * Because this may be "shared" socket from a "master" process, datagrams would be recv()
989
     * by ONLY ONE process: we must "clone" this dgram socket --jjo
990
     */
991

    
992
    if (is_connected) {
993
        if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
994
            /* must be bound */
995
            if (saddr.sin_addr.s_addr==0) {
996
                fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
997
                        fd);
998
                return NULL;
999
            }
1000
            /* clone dgram socket */
1001
            newfd = net_socket_mcast_create(&saddr);
1002
            if (newfd < 0) {
1003
                /* error already reported by net_socket_mcast_create() */
1004
                close(fd);
1005
                return NULL;
1006
            }
1007
            /* clone newfd to fd, close newfd */
1008
            dup2(newfd, fd);
1009
            close(newfd);
1010

    
1011
        } else {
1012
            fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1013
                    fd, strerror(errno));
1014
            return NULL;
1015
        }
1016
    }
1017

    
1018
    s = qemu_mallocz(sizeof(NetSocketState));
1019
    s->fd = fd;
1020

    
1021
    s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET,
1022
                                 vlan, NULL, model, name, NULL,
1023
                                 net_socket_receive_dgram, NULL, NULL,
1024
                                 net_socket_cleanup, s);
1025
    qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1026

    
1027
    /* mcast: save bound address as dst */
1028
    if (is_connected) s->dgram_dst=saddr;
1029

    
1030
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1031
            "socket: fd=%d (%s mcast=%s:%d)",
1032
            fd, is_connected? "cloned" : "",
1033
            inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1034
    return s;
1035
}
1036

    
1037
static void net_socket_connect(void *opaque)
1038
{
1039
    NetSocketState *s = opaque;
1040
    qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1041
}
1042

    
1043
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1044
                                                 const char *model,
1045
                                                 const char *name,
1046
                                                 int fd, int is_connected)
1047
{
1048
    NetSocketState *s;
1049
    s = qemu_mallocz(sizeof(NetSocketState));
1050
    s->fd = fd;
1051
    s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET,
1052
                                 vlan, NULL, model, name, NULL,
1053
                                 net_socket_receive, NULL, NULL,
1054
                                 net_socket_cleanup, s);
1055
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1056
             "socket: fd=%d", fd);
1057
    if (is_connected) {
1058
        net_socket_connect(s);
1059
    } else {
1060
        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1061
    }
1062
    return s;
1063
}
1064

    
1065
static NetSocketState *net_socket_fd_init(VLANState *vlan,
1066
                                          const char *model, const char *name,
1067
                                          int fd, int is_connected)
1068
{
1069
    int so_type = -1, optlen=sizeof(so_type);
1070

    
1071
    if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1072
        (socklen_t *)&optlen)< 0) {
1073
        fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1074
        return NULL;
1075
    }
1076
    switch(so_type) {
1077
    case SOCK_DGRAM:
1078
        return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1079
    case SOCK_STREAM:
1080
        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1081
    default:
1082
        /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1083
        fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1084
        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1085
    }
1086
    return NULL;
1087
}
1088

    
1089
static void net_socket_accept(void *opaque)
1090
{
1091
    NetSocketListenState *s = opaque;
1092
    NetSocketState *s1;
1093
    struct sockaddr_in saddr;
1094
    socklen_t len;
1095
    int fd;
1096

    
1097
    for(;;) {
1098
        len = sizeof(saddr);
1099
        fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1100
        if (fd < 0 && errno != EINTR) {
1101
            return;
1102
        } else if (fd >= 0) {
1103
            break;
1104
        }
1105
    }
1106
    s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1107
    if (!s1) {
1108
        closesocket(fd);
1109
    } else {
1110
        snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1111
                 "socket: connection from %s:%d",
1112
                 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1113
    }
1114
}
1115

    
1116
static int net_socket_listen_init(VLANState *vlan,
1117
                                  const char *model,
1118
                                  const char *name,
1119
                                  const char *host_str)
1120
{
1121
    NetSocketListenState *s;
1122
    int fd, val, ret;
1123
    struct sockaddr_in saddr;
1124

    
1125
    if (parse_host_port(&saddr, host_str) < 0)
1126
        return -1;
1127

    
1128
    s = qemu_mallocz(sizeof(NetSocketListenState));
1129

    
1130
    fd = socket(PF_INET, SOCK_STREAM, 0);
1131
    if (fd < 0) {
1132
        perror("socket");
1133
        return -1;
1134
    }
1135
    socket_set_nonblock(fd);
1136

    
1137
    /* allow fast reuse */
1138
    val = 1;
1139
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1140

    
1141
    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1142
    if (ret < 0) {
1143
        perror("bind");
1144
        return -1;
1145
    }
1146
    ret = listen(fd, 0);
1147
    if (ret < 0) {
1148
        perror("listen");
1149
        return -1;
1150
    }
1151
    s->vlan = vlan;
1152
    s->model = qemu_strdup(model);
1153
    s->name = name ? qemu_strdup(name) : NULL;
1154
    s->fd = fd;
1155
    qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1156
    return 0;
1157
}
1158

    
1159
static int net_socket_connect_init(VLANState *vlan,
1160
                                   const char *model,
1161
                                   const char *name,
1162
                                   const char *host_str)
1163
{
1164
    NetSocketState *s;
1165
    int fd, connected, ret, err;
1166
    struct sockaddr_in saddr;
1167

    
1168
    if (parse_host_port(&saddr, host_str) < 0)
1169
        return -1;
1170

    
1171
    fd = socket(PF_INET, SOCK_STREAM, 0);
1172
    if (fd < 0) {
1173
        perror("socket");
1174
        return -1;
1175
    }
1176
    socket_set_nonblock(fd);
1177

    
1178
    connected = 0;
1179
    for(;;) {
1180
        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1181
        if (ret < 0) {
1182
            err = socket_error();
1183
            if (err == EINTR || err == EWOULDBLOCK) {
1184
            } else if (err == EINPROGRESS) {
1185
                break;
1186
#ifdef _WIN32
1187
            } else if (err == WSAEALREADY) {
1188
                break;
1189
#endif
1190
            } else {
1191
                perror("connect");
1192
                closesocket(fd);
1193
                return -1;
1194
            }
1195
        } else {
1196
            connected = 1;
1197
            break;
1198
        }
1199
    }
1200
    s = net_socket_fd_init(vlan, model, name, fd, connected);
1201
    if (!s)
1202
        return -1;
1203
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1204
             "socket: connect to %s:%d",
1205
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1206
    return 0;
1207
}
1208

    
1209
static int net_socket_mcast_init(VLANState *vlan,
1210
                                 const char *model,
1211
                                 const char *name,
1212
                                 const char *host_str)
1213
{
1214
    NetSocketState *s;
1215
    int fd;
1216
    struct sockaddr_in saddr;
1217

    
1218
    if (parse_host_port(&saddr, host_str) < 0)
1219
        return -1;
1220

    
1221

    
1222
    fd = net_socket_mcast_create(&saddr);
1223
    if (fd < 0)
1224
        return -1;
1225

    
1226
    s = net_socket_fd_init(vlan, model, name, fd, 0);
1227
    if (!s)
1228
        return -1;
1229

    
1230
    s->dgram_dst = saddr;
1231

    
1232
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1233
             "socket: mcast=%s:%d",
1234
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1235
    return 0;
1236

    
1237
}
1238

    
1239
typedef struct DumpState {
1240
    VLANClientState *pcap_vc;
1241
    int fd;
1242
    int pcap_caplen;
1243
} DumpState;
1244

    
1245
#define PCAP_MAGIC 0xa1b2c3d4
1246

    
1247
struct pcap_file_hdr {
1248
    uint32_t magic;
1249
    uint16_t version_major;
1250
    uint16_t version_minor;
1251
    int32_t thiszone;
1252
    uint32_t sigfigs;
1253
    uint32_t snaplen;
1254
    uint32_t linktype;
1255
};
1256

    
1257
struct pcap_sf_pkthdr {
1258
    struct {
1259
        int32_t tv_sec;
1260
        int32_t tv_usec;
1261
    } ts;
1262
    uint32_t caplen;
1263
    uint32_t len;
1264
};
1265

    
1266
static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1267
{
1268
    DumpState *s = vc->opaque;
1269
    struct pcap_sf_pkthdr hdr;
1270
    int64_t ts;
1271
    int caplen;
1272

    
1273
    /* Early return in case of previous error. */
1274
    if (s->fd < 0) {
1275
        return size;
1276
    }
1277

    
1278
    ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
1279
    caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
1280

    
1281
    hdr.ts.tv_sec = ts / 1000000;
1282
    hdr.ts.tv_usec = ts % 1000000;
1283
    hdr.caplen = caplen;
1284
    hdr.len = size;
1285
    if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
1286
        write(s->fd, buf, caplen) != caplen) {
1287
        qemu_log("-net dump write error - stop dump\n");
1288
        close(s->fd);
1289
        s->fd = -1;
1290
    }
1291

    
1292
    return size;
1293
}
1294

    
1295
static void net_dump_cleanup(VLANClientState *vc)
1296
{
1297
    DumpState *s = vc->opaque;
1298

    
1299
    close(s->fd);
1300
    qemu_free(s);
1301
}
1302

    
1303
static int net_dump_init(VLANState *vlan, const char *device,
1304
                         const char *name, const char *filename, int len)
1305
{
1306
    struct pcap_file_hdr hdr;
1307
    DumpState *s;
1308

    
1309
    s = qemu_malloc(sizeof(DumpState));
1310

    
1311
    s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
1312
    if (s->fd < 0) {
1313
        qemu_error("-net dump: can't open %s\n", filename);
1314
        return -1;
1315
    }
1316

    
1317
    s->pcap_caplen = len;
1318

    
1319
    hdr.magic = PCAP_MAGIC;
1320
    hdr.version_major = 2;
1321
    hdr.version_minor = 4;
1322
    hdr.thiszone = 0;
1323
    hdr.sigfigs = 0;
1324
    hdr.snaplen = s->pcap_caplen;
1325
    hdr.linktype = 1;
1326

    
1327
    if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
1328
        qemu_error("-net dump write error: %s\n", strerror(errno));
1329
        close(s->fd);
1330
        qemu_free(s);
1331
        return -1;
1332
    }
1333

    
1334
    s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP,
1335
                                      vlan, NULL, device, name, NULL,
1336
                                      dump_receive, NULL, NULL,
1337
                                      net_dump_cleanup, s);
1338
    snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
1339
             "dump to %s (len=%d)", filename, len);
1340
    return 0;
1341
}
1342

    
1343
/* find or alloc a new VLAN */
1344
VLANState *qemu_find_vlan(int id, int allocate)
1345
{
1346
    VLANState *vlan;
1347

    
1348
    QTAILQ_FOREACH(vlan, &vlans, next) {
1349
        if (vlan->id == id) {
1350
            return vlan;
1351
        }
1352
    }
1353

    
1354
    if (!allocate) {
1355
        return NULL;
1356
    }
1357

    
1358
    vlan = qemu_mallocz(sizeof(VLANState));
1359
    vlan->id = id;
1360
    QTAILQ_INIT(&vlan->clients);
1361

    
1362
    vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
1363
                                          qemu_vlan_deliver_packet_iov,
1364
                                          vlan);
1365

    
1366
    QTAILQ_INSERT_TAIL(&vlans, vlan, next);
1367

    
1368
    return vlan;
1369
}
1370

    
1371
VLANClientState *qemu_find_netdev(const char *id)
1372
{
1373
    VLANClientState *vc;
1374

    
1375
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1376
        if (!strcmp(vc->name, id)) {
1377
            return vc;
1378
        }
1379
    }
1380

    
1381
    return NULL;
1382
}
1383

    
1384
static int nic_get_free_idx(void)
1385
{
1386
    int index;
1387

    
1388
    for (index = 0; index < MAX_NICS; index++)
1389
        if (!nd_table[index].used)
1390
            return index;
1391
    return -1;
1392
}
1393

    
1394
int qemu_show_nic_models(const char *arg, const char *const *models)
1395
{
1396
    int i;
1397

    
1398
    if (!arg || strcmp(arg, "?"))
1399
        return 0;
1400

    
1401
    fprintf(stderr, "qemu: Supported NIC models: ");
1402
    for (i = 0 ; models[i]; i++)
1403
        fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1404
    return 1;
1405
}
1406

    
1407
void qemu_check_nic_model(NICInfo *nd, const char *model)
1408
{
1409
    const char *models[2];
1410

    
1411
    models[0] = model;
1412
    models[1] = NULL;
1413

    
1414
    if (qemu_show_nic_models(nd->model, models))
1415
        exit(0);
1416
    if (qemu_find_nic_model(nd, models, model) < 0)
1417
        exit(1);
1418
}
1419

    
1420
int qemu_find_nic_model(NICInfo *nd, const char * const *models,
1421
                        const char *default_model)
1422
{
1423
    int i;
1424

    
1425
    if (!nd->model)
1426
        nd->model = qemu_strdup(default_model);
1427

    
1428
    for (i = 0 ; models[i]; i++) {
1429
        if (strcmp(nd->model, models[i]) == 0)
1430
            return i;
1431
    }
1432

    
1433
    qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
1434
    return -1;
1435
}
1436

    
1437
int net_handle_fd_param(Monitor *mon, const char *param)
1438
{
1439
    if (!qemu_isdigit(param[0])) {
1440
        int fd;
1441

    
1442
        fd = monitor_get_fd(mon, param);
1443
        if (fd == -1) {
1444
            qemu_error("No file descriptor named %s found", param);
1445
            return -1;
1446
        }
1447

    
1448
        return fd;
1449
    } else {
1450
        return strtol(param, NULL, 0);
1451
    }
1452
}
1453

    
1454
static int net_init_nic(QemuOpts *opts,
1455
                        Monitor *mon,
1456
                        const char *name,
1457
                        VLANState *vlan)
1458
{
1459
    int idx;
1460
    NICInfo *nd;
1461
    const char *netdev;
1462

    
1463
    idx = nic_get_free_idx();
1464
    if (idx == -1 || nb_nics >= MAX_NICS) {
1465
        qemu_error("Too Many NICs\n");
1466
        return -1;
1467
    }
1468

    
1469
    nd = &nd_table[idx];
1470

    
1471
    memset(nd, 0, sizeof(*nd));
1472

    
1473
    if ((netdev = qemu_opt_get(opts, "netdev"))) {
1474
        nd->netdev = qemu_find_netdev(netdev);
1475
        if (!nd->netdev) {
1476
            qemu_error("netdev '%s' not found\n", netdev);
1477
            return -1;
1478
        }
1479
    } else {
1480
        assert(vlan);
1481
        nd->vlan = vlan;
1482
    }
1483
    if (name) {
1484
        nd->name = qemu_strdup(name);
1485
    }
1486
    if (qemu_opt_get(opts, "model")) {
1487
        nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
1488
    }
1489
    if (qemu_opt_get(opts, "addr")) {
1490
        nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
1491
    }
1492

    
1493
    nd->macaddr[0] = 0x52;
1494
    nd->macaddr[1] = 0x54;
1495
    nd->macaddr[2] = 0x00;
1496
    nd->macaddr[3] = 0x12;
1497
    nd->macaddr[4] = 0x34;
1498
    nd->macaddr[5] = 0x56 + idx;
1499

    
1500
    if (qemu_opt_get(opts, "macaddr") &&
1501
        parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
1502
        qemu_error("invalid syntax for ethernet address\n");
1503
        return -1;
1504
    }
1505

    
1506
    nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
1507
    if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
1508
        (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
1509
        qemu_error("invalid # of vectors: %d\n", nd->nvectors);
1510
        return -1;
1511
    }
1512

    
1513
    nd->used = 1;
1514
    if (vlan) {
1515
        nd->vlan->nb_guest_devs++;
1516
    }
1517
    nb_nics++;
1518

    
1519
    return idx;
1520
}
1521

    
1522
static int net_init_socket(QemuOpts *opts,
1523
                           Monitor *mon,
1524
                           const char *name,
1525
                           VLANState *vlan)
1526
{
1527
    if (qemu_opt_get(opts, "fd")) {
1528
        int fd;
1529

    
1530
        if (qemu_opt_get(opts, "listen") ||
1531
            qemu_opt_get(opts, "connect") ||
1532
            qemu_opt_get(opts, "mcast")) {
1533
            qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
1534
            return -1;
1535
        }
1536

    
1537
        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
1538
        if (fd == -1) {
1539
            return -1;
1540
        }
1541

    
1542
        if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
1543
            close(fd);
1544
            return -1;
1545
        }
1546
    } else if (qemu_opt_get(opts, "listen")) {
1547
        const char *listen;
1548

    
1549
        if (qemu_opt_get(opts, "fd") ||
1550
            qemu_opt_get(opts, "connect") ||
1551
            qemu_opt_get(opts, "mcast")) {
1552
            qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
1553
            return -1;
1554
        }
1555

    
1556
        listen = qemu_opt_get(opts, "listen");
1557

    
1558
        if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
1559
            return -1;
1560
        }
1561
    } else if (qemu_opt_get(opts, "connect")) {
1562
        const char *connect;
1563

    
1564
        if (qemu_opt_get(opts, "fd") ||
1565
            qemu_opt_get(opts, "listen") ||
1566
            qemu_opt_get(opts, "mcast")) {
1567
            qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
1568
            return -1;
1569
        }
1570

    
1571
        connect = qemu_opt_get(opts, "connect");
1572

    
1573
        if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
1574
            return -1;
1575
        }
1576
    } else if (qemu_opt_get(opts, "mcast")) {
1577
        const char *mcast;
1578

    
1579
        if (qemu_opt_get(opts, "fd") ||
1580
            qemu_opt_get(opts, "connect") ||
1581
            qemu_opt_get(opts, "listen")) {
1582
            qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
1583
            return -1;
1584
        }
1585

    
1586
        mcast = qemu_opt_get(opts, "mcast");
1587

    
1588
        if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) {
1589
            return -1;
1590
        }
1591
    } else {
1592
        qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
1593
        return -1;
1594
    }
1595

    
1596
    if (vlan) {
1597
        vlan->nb_host_devs++;
1598
    }
1599

    
1600
    return 0;
1601
}
1602

    
1603
#ifdef CONFIG_VDE
1604
static int net_init_vde(QemuOpts *opts,
1605
                        Monitor *mon,
1606
                        const char *name,
1607
                        VLANState *vlan)
1608
{
1609
    const char *sock;
1610
    const char *group;
1611
    int port, mode;
1612

    
1613
    sock  = qemu_opt_get(opts, "sock");
1614
    group = qemu_opt_get(opts, "group");
1615

    
1616
    port = qemu_opt_get_number(opts, "port", 0);
1617
    mode = qemu_opt_get_number(opts, "mode", 0700);
1618

    
1619
    if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) {
1620
        return -1;
1621
    }
1622

    
1623
    if (vlan) {
1624
        vlan->nb_host_devs++;
1625
    }
1626

    
1627
    return 0;
1628
}
1629
#endif
1630

    
1631
static int net_init_dump(QemuOpts *opts,
1632
                         Monitor *mon,
1633
                         const char *name,
1634
                         VLANState *vlan)
1635
{
1636
    int len;
1637
    const char *file;
1638
    char def_file[128];
1639

    
1640
    assert(vlan);
1641

    
1642
    file = qemu_opt_get(opts, "file");
1643
    if (!file) {
1644
        snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id);
1645
        file = def_file;
1646
    }
1647

    
1648
    len = qemu_opt_get_size(opts, "len", 65536);
1649

    
1650
    return net_dump_init(vlan, "dump", name, file, len);
1651
}
1652

    
1653
#define NET_COMMON_PARAMS_DESC                     \
1654
    {                                              \
1655
        .name = "type",                            \
1656
        .type = QEMU_OPT_STRING,                   \
1657
        .help = "net client type (nic, tap etc.)", \
1658
     }, {                                          \
1659
        .name = "vlan",                            \
1660
        .type = QEMU_OPT_NUMBER,                   \
1661
        .help = "vlan number",                     \
1662
     }, {                                          \
1663
        .name = "name",                            \
1664
        .type = QEMU_OPT_STRING,                   \
1665
        .help = "identifier for monitor commands", \
1666
     }
1667

    
1668
typedef int (*net_client_init_func)(QemuOpts *opts,
1669
                                    Monitor *mon,
1670
                                    const char *name,
1671
                                    VLANState *vlan);
1672

    
1673
/* magic number, but compiler will warn if too small */
1674
#define NET_MAX_DESC 20
1675

    
1676
static struct {
1677
    const char *type;
1678
    net_client_init_func init;
1679
    QemuOptDesc desc[NET_MAX_DESC];
1680
} net_client_types[] = {
1681
    {
1682
        .type = "none",
1683
        .desc = {
1684
            NET_COMMON_PARAMS_DESC,
1685
            { /* end of list */ }
1686
        },
1687
    }, {
1688
        .type = "nic",
1689
        .init = net_init_nic,
1690
        .desc = {
1691
            NET_COMMON_PARAMS_DESC,
1692
            {
1693
                .name = "netdev",
1694
                .type = QEMU_OPT_STRING,
1695
                .help = "id of -netdev to connect to",
1696
            },
1697
            {
1698
                .name = "macaddr",
1699
                .type = QEMU_OPT_STRING,
1700
                .help = "MAC address",
1701
            }, {
1702
                .name = "model",
1703
                .type = QEMU_OPT_STRING,
1704
                .help = "device model (e1000, rtl8139, virtio etc.)",
1705
            }, {
1706
                .name = "addr",
1707
                .type = QEMU_OPT_STRING,
1708
                .help = "PCI device address",
1709
            }, {
1710
                .name = "vectors",
1711
                .type = QEMU_OPT_NUMBER,
1712
                .help = "number of MSI-x vectors, 0 to disable MSI-X",
1713
            },
1714
            { /* end of list */ }
1715
        },
1716
#ifdef CONFIG_SLIRP
1717
    }, {
1718
        .type = "user",
1719
        .init = net_init_slirp,
1720
        .desc = {
1721
            NET_COMMON_PARAMS_DESC,
1722
            {
1723
                .name = "hostname",
1724
                .type = QEMU_OPT_STRING,
1725
                .help = "client hostname reported by the builtin DHCP server",
1726
            }, {
1727
                .name = "restrict",
1728
                .type = QEMU_OPT_STRING,
1729
                .help = "isolate the guest from the host (y|yes|n|no)",
1730
            }, {
1731
                .name = "ip",
1732
                .type = QEMU_OPT_STRING,
1733
                .help = "legacy parameter, use net= instead",
1734
            }, {
1735
                .name = "net",
1736
                .type = QEMU_OPT_STRING,
1737
                .help = "IP address and optional netmask",
1738
            }, {
1739
                .name = "host",
1740
                .type = QEMU_OPT_STRING,
1741
                .help = "guest-visible address of the host",
1742
            }, {
1743
                .name = "tftp",
1744
                .type = QEMU_OPT_STRING,
1745
                .help = "root directory of the built-in TFTP server",
1746
            }, {
1747
                .name = "bootfile",
1748
                .type = QEMU_OPT_STRING,
1749
                .help = "BOOTP filename, for use with tftp=",
1750
            }, {
1751
                .name = "dhcpstart",
1752
                .type = QEMU_OPT_STRING,
1753
                .help = "the first of the 16 IPs the built-in DHCP server can assign",
1754
            }, {
1755
                .name = "dns",
1756
                .type = QEMU_OPT_STRING,
1757
                .help = "guest-visible address of the virtual nameserver",
1758
            }, {
1759
                .name = "smb",
1760
                .type = QEMU_OPT_STRING,
1761
                .help = "root directory of the built-in SMB server",
1762
            }, {
1763
                .name = "smbserver",
1764
                .type = QEMU_OPT_STRING,
1765
                .help = "IP address of the built-in SMB server",
1766
            }, {
1767
                .name = "hostfwd",
1768
                .type = QEMU_OPT_STRING,
1769
                .help = "guest port number to forward incoming TCP or UDP connections",
1770
            }, {
1771
                .name = "guestfwd",
1772
                .type = QEMU_OPT_STRING,
1773
                .help = "IP address and port to forward guest TCP connections",
1774
            },
1775
            { /* end of list */ }
1776
        },
1777
#endif
1778
    }, {
1779
        .type = "tap",
1780
        .init = net_init_tap,
1781
        .desc = {
1782
            NET_COMMON_PARAMS_DESC,
1783
            {
1784
                .name = "ifname",
1785
                .type = QEMU_OPT_STRING,
1786
                .help = "interface name",
1787
            },
1788
#ifndef _WIN32
1789
            {
1790
                .name = "fd",
1791
                .type = QEMU_OPT_STRING,
1792
                .help = "file descriptor of an already opened tap",
1793
            }, {
1794
                .name = "script",
1795
                .type = QEMU_OPT_STRING,
1796
                .help = "script to initialize the interface",
1797
            }, {
1798
                .name = "downscript",
1799
                .type = QEMU_OPT_STRING,
1800
                .help = "script to shut down the interface",
1801
            }, {
1802
                .name = "sndbuf",
1803
                .type = QEMU_OPT_SIZE,
1804
                .help = "send buffer limit"
1805
            }, {
1806
                .name = "vnet_hdr",
1807
                .type = QEMU_OPT_BOOL,
1808
                .help = "enable the IFF_VNET_HDR flag on the tap interface"
1809
            },
1810
#endif /* _WIN32 */
1811
            { /* end of list */ }
1812
        },
1813
    }, {
1814
        .type = "socket",
1815
        .init = net_init_socket,
1816
        .desc = {
1817
            NET_COMMON_PARAMS_DESC,
1818
            {
1819
                .name = "fd",
1820
                .type = QEMU_OPT_STRING,
1821
                .help = "file descriptor of an already opened socket",
1822
            }, {
1823
                .name = "listen",
1824
                .type = QEMU_OPT_STRING,
1825
                .help = "port number, and optional hostname, to listen on",
1826
            }, {
1827
                .name = "connect",
1828
                .type = QEMU_OPT_STRING,
1829
                .help = "port number, and optional hostname, to connect to",
1830
            }, {
1831
                .name = "mcast",
1832
                .type = QEMU_OPT_STRING,
1833
                .help = "UDP multicast address and port number",
1834
            },
1835
            { /* end of list */ }
1836
        },
1837
#ifdef CONFIG_VDE
1838
    }, {
1839
        .type = "vde",
1840
        .init = net_init_vde,
1841
        .desc = {
1842
            NET_COMMON_PARAMS_DESC,
1843
            {
1844
                .name = "sock",
1845
                .type = QEMU_OPT_STRING,
1846
                .help = "socket path",
1847
            }, {
1848
                .name = "port",
1849
                .type = QEMU_OPT_NUMBER,
1850
                .help = "port number",
1851
            }, {
1852
                .name = "group",
1853
                .type = QEMU_OPT_STRING,
1854
                .help = "group owner of socket",
1855
            }, {
1856
                .name = "mode",
1857
                .type = QEMU_OPT_NUMBER,
1858
                .help = "permissions for socket",
1859
            },
1860
            { /* end of list */ }
1861
        },
1862
#endif
1863
    }, {
1864
        .type = "dump",
1865
        .init = net_init_dump,
1866
        .desc = {
1867
            NET_COMMON_PARAMS_DESC,
1868
            {
1869
                .name = "len",
1870
                .type = QEMU_OPT_SIZE,
1871
                .help = "per-packet size limit (64k default)",
1872
            }, {
1873
                .name = "file",
1874
                .type = QEMU_OPT_STRING,
1875
                .help = "dump file path (default is qemu-vlan0.pcap)",
1876
            },
1877
            { /* end of list */ }
1878
        },
1879
    },
1880
    { /* end of list */ }
1881
};
1882

    
1883
int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1884
{
1885
    const char *name;
1886
    const char *type;
1887
    int i;
1888

    
1889
    type = qemu_opt_get(opts, "type");
1890
    if (!type) {
1891
        qemu_error("No type specified for -net\n");
1892
        return -1;
1893
    }
1894

    
1895
    if (is_netdev) {
1896
        if (strcmp(type, "tap") != 0 &&
1897
#ifdef CONFIG_SLIRP
1898
            strcmp(type, "user") != 0 &&
1899
#endif
1900
#ifdef CONFIG_VDE
1901
            strcmp(type, "vde") != 0 &&
1902
#endif
1903
            strcmp(type, "socket") != 0) {
1904
            qemu_error("The '%s' network backend type is not valid with -netdev\n",
1905
                       type);
1906
            return -1;
1907
        }
1908

    
1909
        if (qemu_opt_get(opts, "vlan")) {
1910
            qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1911
            return -1;
1912
        }
1913
        if (qemu_opt_get(opts, "name")) {
1914
            qemu_error("The 'name' parameter is not valid with -netdev\n");
1915
            return -1;
1916
        }
1917
        if (!qemu_opts_id(opts)) {
1918
            qemu_error("The id= parameter is required with -netdev\n");
1919
            return -1;
1920
        }
1921
    }
1922

    
1923
    name = qemu_opts_id(opts);
1924
    if (!name) {
1925
        name = qemu_opt_get(opts, "name");
1926
    }
1927

    
1928
    for (i = 0; net_client_types[i].type != NULL; i++) {
1929
        if (!strcmp(net_client_types[i].type, type)) {
1930
            VLANState *vlan = NULL;
1931

    
1932
            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1933
                return -1;
1934
            }
1935

    
1936
            /* Do not add to a vlan if it's a -netdev or a nic with a
1937
             * netdev= parameter. */
1938
            if (!(is_netdev ||
1939
                  (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1940
                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1941
            }
1942

    
1943
            if (net_client_types[i].init) {
1944
                return net_client_types[i].init(opts, mon, name, vlan);
1945
            } else {
1946
                return 0;
1947
            }
1948
        }
1949
    }
1950

    
1951
    qemu_error("Invalid -net type '%s'\n", type);
1952
    return -1;
1953
}
1954

    
1955
void net_client_uninit(NICInfo *nd)
1956
{
1957
    if (nd->vlan) {
1958
        nd->vlan->nb_guest_devs--;
1959
    }
1960
    nb_nics--;
1961

    
1962
    qemu_free(nd->model);
1963
    qemu_free(nd->name);
1964
    qemu_free(nd->devaddr);
1965

    
1966
    nd->used = 0;
1967
}
1968

    
1969
static int net_host_check_device(const char *device)
1970
{
1971
    int i;
1972
    const char *valid_param_list[] = { "tap", "socket", "dump"
1973
#ifdef CONFIG_SLIRP
1974
                                       ,"user"
1975
#endif
1976
#ifdef CONFIG_VDE
1977
                                       ,"vde"
1978
#endif
1979
    };
1980
    for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1981
        if (!strncmp(valid_param_list[i], device,
1982
                     strlen(valid_param_list[i])))
1983
            return 1;
1984
    }
1985

    
1986
    return 0;
1987
}
1988

    
1989
void net_host_device_add(Monitor *mon, const QDict *qdict)
1990
{
1991
    const char *device = qdict_get_str(qdict, "device");
1992
    const char *opts_str = qdict_get_try_str(qdict, "opts");
1993
    QemuOpts *opts;
1994

    
1995
    if (!net_host_check_device(device)) {
1996
        monitor_printf(mon, "invalid host network device %s\n", device);
1997
        return;
1998
    }
1999

    
2000
    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
2001
    if (!opts) {
2002
        monitor_printf(mon, "parsing network options '%s' failed\n",
2003
                       opts_str ? opts_str : "");
2004
        return;
2005
    }
2006

    
2007
    qemu_opt_set(opts, "type", device);
2008

    
2009
    if (net_client_init(mon, opts, 0) < 0) {
2010
        monitor_printf(mon, "adding host network device %s failed\n", device);
2011
    }
2012
}
2013

    
2014
void net_host_device_remove(Monitor *mon, const QDict *qdict)
2015
{
2016
    VLANClientState *vc;
2017
    int vlan_id = qdict_get_int(qdict, "vlan_id");
2018
    const char *device = qdict_get_str(qdict, "device");
2019

    
2020
    vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
2021
    if (!vc) {
2022
        return;
2023
    }
2024
    if (!net_host_check_device(vc->model)) {
2025
        monitor_printf(mon, "invalid host network device %s\n", device);
2026
        return;
2027
    }
2028
    qemu_del_vlan_client(vc);
2029
}
2030

    
2031
void net_set_boot_mask(int net_boot_mask)
2032
{
2033
    int i;
2034

    
2035
    /* Only the first four NICs may be bootable */
2036
    net_boot_mask = net_boot_mask & 0xF;
2037

    
2038
    for (i = 0; i < nb_nics; i++) {
2039
        if (net_boot_mask & (1 << i)) {
2040
            nd_table[i].bootable = 1;
2041
            net_boot_mask &= ~(1 << i);
2042
        }
2043
    }
2044

    
2045
    if (net_boot_mask) {
2046
        fprintf(stderr, "Cannot boot from non-existent NIC\n");
2047
        exit(1);
2048
    }
2049
}
2050

    
2051
void do_info_network(Monitor *mon)
2052
{
2053
    VLANState *vlan;
2054

    
2055
    QTAILQ_FOREACH(vlan, &vlans, next) {
2056
        VLANClientState *vc;
2057

    
2058
        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2059

    
2060
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
2061
            monitor_printf(mon, "  %s: %s\n", vc->name, vc->info_str);
2062
        }
2063
    }
2064
}
2065

    
2066
void do_set_link(Monitor *mon, const QDict *qdict)
2067
{
2068
    VLANState *vlan;
2069
    VLANClientState *vc = NULL;
2070
    const char *name = qdict_get_str(qdict, "name");
2071
    const char *up_or_down = qdict_get_str(qdict, "up_or_down");
2072

    
2073
    QTAILQ_FOREACH(vlan, &vlans, next) {
2074
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
2075
            if (strcmp(vc->name, name) == 0) {
2076
                goto done;
2077
            }
2078
        }
2079
    }
2080
done:
2081

    
2082
    if (!vc) {
2083
        monitor_printf(mon, "could not find network device '%s'\n", name);
2084
        return;
2085
    }
2086

    
2087
    if (strcmp(up_or_down, "up") == 0)
2088
        vc->link_down = 0;
2089
    else if (strcmp(up_or_down, "down") == 0)
2090
        vc->link_down = 1;
2091
    else
2092
        monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2093
                       "valid\n", up_or_down);
2094

    
2095
    if (vc->link_status_changed)
2096
        vc->link_status_changed(vc);
2097
}
2098

    
2099
void net_cleanup(void)
2100
{
2101
    VLANState *vlan;
2102
    VLANClientState *vc, *next_vc;
2103

    
2104
    QTAILQ_FOREACH(vlan, &vlans, next) {
2105
        QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
2106
            qemu_del_vlan_client(vc);
2107
        }
2108
    }
2109

    
2110
    QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
2111
        qemu_del_vlan_client(vc);
2112
    }
2113
}
2114

    
2115
static void net_check_clients(void)
2116
{
2117
    VLANState *vlan;
2118

    
2119
    QTAILQ_FOREACH(vlan, &vlans, next) {
2120
        if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2121
            continue;
2122
        if (vlan->nb_guest_devs == 0)
2123
            fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2124
        if (vlan->nb_host_devs == 0)
2125
            fprintf(stderr,
2126
                    "Warning: vlan %d is not connected to host network\n",
2127
                    vlan->id);
2128
    }
2129
}
2130

    
2131
static int net_init_client(QemuOpts *opts, void *dummy)
2132
{
2133
    if (net_client_init(NULL, opts, 0) < 0)
2134
        return -1;
2135
    return 0;
2136
}
2137

    
2138
static int net_init_netdev(QemuOpts *opts, void *dummy)
2139
{
2140
    return net_client_init(NULL, opts, 1);
2141
}
2142

    
2143
int net_init_clients(void)
2144
{
2145
    if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
2146
        /* if no clients, we use a default config */
2147
        qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
2148
#ifdef CONFIG_SLIRP
2149
        qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
2150
#endif
2151
    }
2152

    
2153
    QTAILQ_INIT(&vlans);
2154
    QTAILQ_INIT(&non_vlan_clients);
2155

    
2156
    if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
2157
        return -1;
2158

    
2159
    if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
2160
        return -1;
2161
    }
2162

    
2163
    net_check_clients();
2164

    
2165
    return 0;
2166
}
2167

    
2168
int net_client_parse(QemuOptsList *opts_list, const char *optarg)
2169
{
2170
#if defined(CONFIG_SLIRP)
2171
    int ret;
2172
    if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
2173
        return ret;
2174
    }
2175
#endif
2176

    
2177
    if (!qemu_opts_parse(opts_list, optarg, "type")) {
2178
        return -1;
2179
    }
2180

    
2181
    return 0;
2182
}