Statistics
| Branch: | Revision:

root / net.c @ f7c31d63

History | View | Annotate | Download (37.6 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 "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 "sysemu.h"
36
#include "qemu-common.h"
37
#include "qemu_socket.h"
38
#include "hw/qdev.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_src_port(struct sockaddr_in *haddr,
97
                        struct sockaddr_in *saddr,
98
                        const char *input_str)
99
{
100
    char *str = qemu_strdup(input_str);
101
    char *host_str = str;
102
    char *src_str;
103
    const char *src_str2;
104
    char *ptr;
105

    
106
    /*
107
     * Chop off any extra arguments at the end of the string which
108
     * would start with a comma, then fill in the src port information
109
     * if it was provided else use the "any address" and "any port".
110
     */
111
    if ((ptr = strchr(str,',')))
112
        *ptr = '\0';
113

    
114
    if ((src_str = strchr(input_str,'@'))) {
115
        *src_str = '\0';
116
        src_str++;
117
    }
118

    
119
    if (parse_host_port(haddr, host_str) < 0)
120
        goto fail;
121

    
122
    src_str2 = src_str;
123
    if (!src_str || *src_str == '\0')
124
        src_str2 = ":0";
125

    
126
    if (parse_host_port(saddr, src_str2) < 0)
127
        goto fail;
128

    
129
    free(str);
130
    return(0);
131

    
132
fail:
133
    free(str);
134
    return -1;
135
}
136

    
137
int parse_host_port(struct sockaddr_in *saddr, const char *str)
138
{
139
    char buf[512];
140
    struct hostent *he;
141
    const char *p, *r;
142
    int port;
143

    
144
    p = str;
145
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
146
        return -1;
147
    saddr->sin_family = AF_INET;
148
    if (buf[0] == '\0') {
149
        saddr->sin_addr.s_addr = 0;
150
    } else {
151
        if (qemu_isdigit(buf[0])) {
152
            if (!inet_aton(buf, &saddr->sin_addr))
153
                return -1;
154
        } else {
155
            if ((he = gethostbyname(buf)) == NULL)
156
                return - 1;
157
            saddr->sin_addr = *(struct in_addr *)he->h_addr;
158
        }
159
    }
160
    port = strtol(p, (char **)&r, 0);
161
    if (r == p)
162
        return -1;
163
    saddr->sin_port = htons(port);
164
    return 0;
165
}
166

    
167
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
168
{
169
    snprintf(vc->info_str, sizeof(vc->info_str),
170
             "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
171
             vc->model,
172
             macaddr[0], macaddr[1], macaddr[2],
173
             macaddr[3], macaddr[4], macaddr[5]);
174
}
175

    
176
void qemu_macaddr_default_if_unset(MACAddr *macaddr)
177
{
178
    static int index = 0;
179
    static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
180

    
181
    if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
182
        return;
183
    macaddr->a[0] = 0x52;
184
    macaddr->a[1] = 0x54;
185
    macaddr->a[2] = 0x00;
186
    macaddr->a[3] = 0x12;
187
    macaddr->a[4] = 0x34;
188
    macaddr->a[5] = 0x56 + index++;
189
}
190

    
191
static char *assign_name(VLANClientState *vc1, const char *model)
192
{
193
    VLANState *vlan;
194
    char buf[256];
195
    int id = 0;
196

    
197
    QTAILQ_FOREACH(vlan, &vlans, next) {
198
        VLANClientState *vc;
199

    
200
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
201
            if (vc != vc1 && strcmp(vc->model, model) == 0) {
202
                id++;
203
            }
204
        }
205
    }
206

    
207
    snprintf(buf, sizeof(buf), "%s.%d", model, id);
208

    
209
    return qemu_strdup(buf);
210
}
211

    
212
static ssize_t qemu_deliver_packet(VLANClientState *sender,
213
                                   unsigned flags,
214
                                   const uint8_t *data,
215
                                   size_t size,
216
                                   void *opaque);
217
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
218
                                       unsigned flags,
219
                                       const struct iovec *iov,
220
                                       int iovcnt,
221
                                       void *opaque);
222

    
223
VLANClientState *qemu_new_net_client(NetClientInfo *info,
224
                                     VLANState *vlan,
225
                                     VLANClientState *peer,
226
                                     const char *model,
227
                                     const char *name)
228
{
229
    VLANClientState *vc;
230

    
231
    assert(info->size >= sizeof(VLANClientState));
232

    
233
    vc = qemu_mallocz(info->size);
234

    
235
    vc->info = info;
236
    vc->model = qemu_strdup(model);
237
    if (name) {
238
        vc->name = qemu_strdup(name);
239
    } else {
240
        vc->name = assign_name(vc, model);
241
    }
242

    
243
    if (vlan) {
244
        assert(!peer);
245
        vc->vlan = vlan;
246
        QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
247
    } else {
248
        if (peer) {
249
            assert(!peer->peer);
250
            vc->peer = peer;
251
            peer->peer = vc;
252
        }
253
        QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
254

    
255
        vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
256
                                            qemu_deliver_packet_iov,
257
                                            vc);
258
    }
259

    
260
    return vc;
261
}
262

    
263
NICState *qemu_new_nic(NetClientInfo *info,
264
                       NICConf *conf,
265
                       const char *model,
266
                       const char *name,
267
                       void *opaque)
268
{
269
    VLANClientState *nc;
270
    NICState *nic;
271

    
272
    assert(info->type == NET_CLIENT_TYPE_NIC);
273
    assert(info->size >= sizeof(NICState));
274

    
275
    nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
276

    
277
    nic = DO_UPCAST(NICState, nc, nc);
278
    nic->conf = conf;
279
    nic->opaque = opaque;
280

    
281
    return nic;
282
}
283

    
284
static void qemu_cleanup_vlan_client(VLANClientState *vc)
285
{
286
    if (vc->vlan) {
287
        QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
288
    } else {
289
        QTAILQ_REMOVE(&non_vlan_clients, vc, next);
290
    }
291

    
292
    if (vc->info->cleanup) {
293
        vc->info->cleanup(vc);
294
    }
295
}
296

    
297
static void qemu_free_vlan_client(VLANClientState *vc)
298
{
299
    if (!vc->vlan) {
300
        if (vc->send_queue) {
301
            qemu_del_net_queue(vc->send_queue);
302
        }
303
        if (vc->peer) {
304
            vc->peer->peer = NULL;
305
        }
306
    }
307
    qemu_free(vc->name);
308
    qemu_free(vc->model);
309
    qemu_free(vc);
310
}
311

    
312
void qemu_del_vlan_client(VLANClientState *vc)
313
{
314
    /* If there is a peer NIC, delete and cleanup client, but do not free. */
315
    if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
316
        NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
317
        if (nic->peer_deleted) {
318
            return;
319
        }
320
        nic->peer_deleted = true;
321
        /* Let NIC know peer is gone. */
322
        vc->peer->link_down = true;
323
        if (vc->peer->info->link_status_changed) {
324
            vc->peer->info->link_status_changed(vc->peer);
325
        }
326
        qemu_cleanup_vlan_client(vc);
327
        return;
328
    }
329

    
330
    /* If this is a peer NIC and peer has already been deleted, free it now. */
331
    if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
332
        NICState *nic = DO_UPCAST(NICState, nc, vc);
333
        if (nic->peer_deleted) {
334
            qemu_free_vlan_client(vc->peer);
335
        }
336
    }
337

    
338
    qemu_cleanup_vlan_client(vc);
339
    qemu_free_vlan_client(vc);
340
}
341

    
342
VLANClientState *
343
qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
344
                              const char *client_str)
345
{
346
    VLANState *vlan;
347
    VLANClientState *vc;
348

    
349
    vlan = qemu_find_vlan(vlan_id, 0);
350
    if (!vlan) {
351
        monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
352
        return NULL;
353
    }
354

    
355
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
356
        if (!strcmp(vc->name, client_str)) {
357
            break;
358
        }
359
    }
360
    if (!vc) {
361
        monitor_printf(mon, "can't find device %s on VLAN %d\n",
362
                       client_str, vlan_id);
363
    }
364

    
365
    return vc;
366
}
367

    
368
void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
369
{
370
    VLANClientState *nc;
371
    VLANState *vlan;
372

    
373
    QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
374
        if (nc->info->type == NET_CLIENT_TYPE_NIC) {
375
            func(DO_UPCAST(NICState, nc, nc), opaque);
376
        }
377
    }
378

    
379
    QTAILQ_FOREACH(vlan, &vlans, next) {
380
        QTAILQ_FOREACH(nc, &vlan->clients, next) {
381
            if (nc->info->type == NET_CLIENT_TYPE_NIC) {
382
                func(DO_UPCAST(NICState, nc, nc), opaque);
383
            }
384
        }
385
    }
386
}
387

    
388
int qemu_can_send_packet(VLANClientState *sender)
389
{
390
    VLANState *vlan = sender->vlan;
391
    VLANClientState *vc;
392

    
393
    if (sender->peer) {
394
        if (sender->peer->receive_disabled) {
395
            return 0;
396
        } else if (sender->peer->info->can_receive &&
397
                   !sender->peer->info->can_receive(sender->peer)) {
398
            return 0;
399
        } else {
400
            return 1;
401
        }
402
    }
403

    
404
    if (!sender->vlan) {
405
        return 1;
406
    }
407

    
408
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
409
        if (vc == sender) {
410
            continue;
411
        }
412

    
413
        /* no can_receive() handler, they can always receive */
414
        if (!vc->info->can_receive || vc->info->can_receive(vc)) {
415
            return 1;
416
        }
417
    }
418
    return 0;
419
}
420

    
421
static ssize_t qemu_deliver_packet(VLANClientState *sender,
422
                                   unsigned flags,
423
                                   const uint8_t *data,
424
                                   size_t size,
425
                                   void *opaque)
426
{
427
    VLANClientState *vc = opaque;
428
    ssize_t ret;
429

    
430
    if (vc->link_down) {
431
        return size;
432
    }
433

    
434
    if (vc->receive_disabled) {
435
        return 0;
436
    }
437

    
438
    if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
439
        ret = vc->info->receive_raw(vc, data, size);
440
    } else {
441
        ret = vc->info->receive(vc, data, size);
442
    }
443

    
444
    if (ret == 0) {
445
        vc->receive_disabled = 1;
446
    };
447

    
448
    return ret;
449
}
450

    
451
static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
452
                                        unsigned flags,
453
                                        const uint8_t *buf,
454
                                        size_t size,
455
                                        void *opaque)
456
{
457
    VLANState *vlan = opaque;
458
    VLANClientState *vc;
459
    ssize_t ret = -1;
460

    
461
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
462
        ssize_t len;
463

    
464
        if (vc == sender) {
465
            continue;
466
        }
467

    
468
        if (vc->link_down) {
469
            ret = size;
470
            continue;
471
        }
472

    
473
        if (vc->receive_disabled) {
474
            ret = 0;
475
            continue;
476
        }
477

    
478
        if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
479
            len = vc->info->receive_raw(vc, buf, size);
480
        } else {
481
            len = vc->info->receive(vc, buf, size);
482
        }
483

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

    
488
        ret = (ret >= 0) ? ret : len;
489

    
490
    }
491

    
492
    return ret;
493
}
494

    
495
void qemu_purge_queued_packets(VLANClientState *vc)
496
{
497
    NetQueue *queue;
498

    
499
    if (!vc->peer && !vc->vlan) {
500
        return;
501
    }
502

    
503
    if (vc->peer) {
504
        queue = vc->peer->send_queue;
505
    } else {
506
        queue = vc->vlan->send_queue;
507
    }
508

    
509
    qemu_net_queue_purge(queue, vc);
510
}
511

    
512
void qemu_flush_queued_packets(VLANClientState *vc)
513
{
514
    NetQueue *queue;
515

    
516
    vc->receive_disabled = 0;
517

    
518
    if (vc->vlan) {
519
        queue = vc->vlan->send_queue;
520
    } else {
521
        queue = vc->send_queue;
522
    }
523

    
524
    qemu_net_queue_flush(queue);
525
}
526

    
527
static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
528
                                                 unsigned flags,
529
                                                 const uint8_t *buf, int size,
530
                                                 NetPacketSent *sent_cb)
531
{
532
    NetQueue *queue;
533

    
534
#ifdef DEBUG_NET
535
    printf("qemu_send_packet_async:\n");
536
    hex_dump(stdout, buf, size);
537
#endif
538

    
539
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
540
        return size;
541
    }
542

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

    
549
    return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
550
}
551

    
552
ssize_t qemu_send_packet_async(VLANClientState *sender,
553
                               const uint8_t *buf, int size,
554
                               NetPacketSent *sent_cb)
555
{
556
    return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
557
                                             buf, size, sent_cb);
558
}
559

    
560
void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
561
{
562
    qemu_send_packet_async(vc, buf, size, NULL);
563
}
564

    
565
ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
566
{
567
    return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
568
                                             buf, size, NULL);
569
}
570

    
571
static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
572
                               int iovcnt)
573
{
574
    uint8_t buffer[4096];
575
    size_t offset = 0;
576
    int i;
577

    
578
    for (i = 0; i < iovcnt; i++) {
579
        size_t len;
580

    
581
        len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
582
        memcpy(buffer + offset, iov[i].iov_base, len);
583
        offset += len;
584
    }
585

    
586
    return vc->info->receive(vc, buffer, offset);
587
}
588

    
589
static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
590
{
591
    size_t offset = 0;
592
    int i;
593

    
594
    for (i = 0; i < iovcnt; i++)
595
        offset += iov[i].iov_len;
596
    return offset;
597
}
598

    
599
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
600
                                       unsigned flags,
601
                                       const struct iovec *iov,
602
                                       int iovcnt,
603
                                       void *opaque)
604
{
605
    VLANClientState *vc = opaque;
606

    
607
    if (vc->link_down) {
608
        return calc_iov_length(iov, iovcnt);
609
    }
610

    
611
    if (vc->info->receive_iov) {
612
        return vc->info->receive_iov(vc, iov, iovcnt);
613
    } else {
614
        return vc_sendv_compat(vc, iov, iovcnt);
615
    }
616
}
617

    
618
static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
619
                                            unsigned flags,
620
                                            const struct iovec *iov,
621
                                            int iovcnt,
622
                                            void *opaque)
623
{
624
    VLANState *vlan = opaque;
625
    VLANClientState *vc;
626
    ssize_t ret = -1;
627

    
628
    QTAILQ_FOREACH(vc, &vlan->clients, next) {
629
        ssize_t len;
630

    
631
        if (vc == sender) {
632
            continue;
633
        }
634

    
635
        if (vc->link_down) {
636
            ret = calc_iov_length(iov, iovcnt);
637
            continue;
638
        }
639

    
640
        assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
641

    
642
        if (vc->info->receive_iov) {
643
            len = vc->info->receive_iov(vc, iov, iovcnt);
644
        } else {
645
            len = vc_sendv_compat(vc, iov, iovcnt);
646
        }
647

    
648
        ret = (ret >= 0) ? ret : len;
649
    }
650

    
651
    return ret;
652
}
653

    
654
ssize_t qemu_sendv_packet_async(VLANClientState *sender,
655
                                const struct iovec *iov, int iovcnt,
656
                                NetPacketSent *sent_cb)
657
{
658
    NetQueue *queue;
659

    
660
    if (sender->link_down || (!sender->peer && !sender->vlan)) {
661
        return calc_iov_length(iov, iovcnt);
662
    }
663

    
664
    if (sender->peer) {
665
        queue = sender->peer->send_queue;
666
    } else {
667
        queue = sender->vlan->send_queue;
668
    }
669

    
670
    return qemu_net_queue_send_iov(queue, sender,
671
                                   QEMU_NET_PACKET_FLAG_NONE,
672
                                   iov, iovcnt, sent_cb);
673
}
674

    
675
ssize_t
676
qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
677
{
678
    return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
679
}
680

    
681
/* find or alloc a new VLAN */
682
VLANState *qemu_find_vlan(int id, int allocate)
683
{
684
    VLANState *vlan;
685

    
686
    QTAILQ_FOREACH(vlan, &vlans, next) {
687
        if (vlan->id == id) {
688
            return vlan;
689
        }
690
    }
691

    
692
    if (!allocate) {
693
        return NULL;
694
    }
695

    
696
    vlan = qemu_mallocz(sizeof(VLANState));
697
    vlan->id = id;
698
    QTAILQ_INIT(&vlan->clients);
699

    
700
    vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
701
                                          qemu_vlan_deliver_packet_iov,
702
                                          vlan);
703

    
704
    QTAILQ_INSERT_TAIL(&vlans, vlan, next);
705

    
706
    return vlan;
707
}
708

    
709
VLANClientState *qemu_find_netdev(const char *id)
710
{
711
    VLANClientState *vc;
712

    
713
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
714
        if (!strcmp(vc->name, id)) {
715
            return vc;
716
        }
717
    }
718

    
719
    return NULL;
720
}
721

    
722
static int nic_get_free_idx(void)
723
{
724
    int index;
725

    
726
    for (index = 0; index < MAX_NICS; index++)
727
        if (!nd_table[index].used)
728
            return index;
729
    return -1;
730
}
731

    
732
int qemu_show_nic_models(const char *arg, const char *const *models)
733
{
734
    int i;
735

    
736
    if (!arg || strcmp(arg, "?"))
737
        return 0;
738

    
739
    fprintf(stderr, "qemu: Supported NIC models: ");
740
    for (i = 0 ; models[i]; i++)
741
        fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
742
    return 1;
743
}
744

    
745
void qemu_check_nic_model(NICInfo *nd, const char *model)
746
{
747
    const char *models[2];
748

    
749
    models[0] = model;
750
    models[1] = NULL;
751

    
752
    if (qemu_show_nic_models(nd->model, models))
753
        exit(0);
754
    if (qemu_find_nic_model(nd, models, model) < 0)
755
        exit(1);
756
}
757

    
758
int qemu_find_nic_model(NICInfo *nd, const char * const *models,
759
                        const char *default_model)
760
{
761
    int i;
762

    
763
    if (!nd->model)
764
        nd->model = qemu_strdup(default_model);
765

    
766
    for (i = 0 ; models[i]; i++) {
767
        if (strcmp(nd->model, models[i]) == 0)
768
            return i;
769
    }
770

    
771
    error_report("qemu: Unsupported NIC model: %s", nd->model);
772
    return -1;
773
}
774

    
775
int net_handle_fd_param(Monitor *mon, const char *param)
776
{
777
    int fd;
778

    
779
    if (!qemu_isdigit(param[0]) && mon) {
780

    
781
        fd = monitor_get_fd(mon, param);
782
        if (fd == -1) {
783
            error_report("No file descriptor named %s found", param);
784
            return -1;
785
        }
786
    } else {
787
        char *endptr = NULL;
788

    
789
        fd = strtol(param, &endptr, 10);
790
        if (*endptr || (fd == 0 && param == endptr)) {
791
            return -1;
792
        }
793
    }
794

    
795
    return fd;
796
}
797

    
798
static int net_init_nic(QemuOpts *opts,
799
                        Monitor *mon,
800
                        const char *name,
801
                        VLANState *vlan)
802
{
803
    int idx;
804
    NICInfo *nd;
805
    const char *netdev;
806

    
807
    idx = nic_get_free_idx();
808
    if (idx == -1 || nb_nics >= MAX_NICS) {
809
        error_report("Too Many NICs");
810
        return -1;
811
    }
812

    
813
    nd = &nd_table[idx];
814

    
815
    memset(nd, 0, sizeof(*nd));
816

    
817
    if ((netdev = qemu_opt_get(opts, "netdev"))) {
818
        nd->netdev = qemu_find_netdev(netdev);
819
        if (!nd->netdev) {
820
            error_report("netdev '%s' not found", netdev);
821
            return -1;
822
        }
823
    } else {
824
        assert(vlan);
825
        nd->vlan = vlan;
826
    }
827
    if (name) {
828
        nd->name = qemu_strdup(name);
829
    }
830
    if (qemu_opt_get(opts, "model")) {
831
        nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
832
    }
833
    if (qemu_opt_get(opts, "addr")) {
834
        nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
835
    }
836

    
837
    nd->macaddr[0] = 0x52;
838
    nd->macaddr[1] = 0x54;
839
    nd->macaddr[2] = 0x00;
840
    nd->macaddr[3] = 0x12;
841
    nd->macaddr[4] = 0x34;
842
    nd->macaddr[5] = 0x56 + idx;
843

    
844
    if (qemu_opt_get(opts, "macaddr") &&
845
        net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
846
        error_report("invalid syntax for ethernet address");
847
        return -1;
848
    }
849

    
850
    nd->nvectors = qemu_opt_get_number(opts, "vectors",
851
                                       DEV_NVECTORS_UNSPECIFIED);
852
    if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
853
        (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
854
        error_report("invalid # of vectors: %d", nd->nvectors);
855
        return -1;
856
    }
857

    
858
    nd->used = 1;
859
    nb_nics++;
860

    
861
    return idx;
862
}
863

    
864
#define NET_COMMON_PARAMS_DESC                     \
865
    {                                              \
866
        .name = "type",                            \
867
        .type = QEMU_OPT_STRING,                   \
868
        .help = "net client type (nic, tap etc.)", \
869
     }, {                                          \
870
        .name = "vlan",                            \
871
        .type = QEMU_OPT_NUMBER,                   \
872
        .help = "vlan number",                     \
873
     }, {                                          \
874
        .name = "name",                            \
875
        .type = QEMU_OPT_STRING,                   \
876
        .help = "identifier for monitor commands", \
877
     }
878

    
879
typedef int (*net_client_init_func)(QemuOpts *opts,
880
                                    Monitor *mon,
881
                                    const char *name,
882
                                    VLANState *vlan);
883

    
884
/* magic number, but compiler will warn if too small */
885
#define NET_MAX_DESC 20
886

    
887
static const struct {
888
    const char *type;
889
    net_client_init_func init;
890
    QemuOptDesc desc[NET_MAX_DESC];
891
} net_client_types[] = {
892
    {
893
        .type = "none",
894
        .desc = {
895
            NET_COMMON_PARAMS_DESC,
896
            { /* end of list */ }
897
        },
898
    }, {
899
        .type = "nic",
900
        .init = net_init_nic,
901
        .desc = {
902
            NET_COMMON_PARAMS_DESC,
903
            {
904
                .name = "netdev",
905
                .type = QEMU_OPT_STRING,
906
                .help = "id of -netdev to connect to",
907
            },
908
            {
909
                .name = "macaddr",
910
                .type = QEMU_OPT_STRING,
911
                .help = "MAC address",
912
            }, {
913
                .name = "model",
914
                .type = QEMU_OPT_STRING,
915
                .help = "device model (e1000, rtl8139, virtio etc.)",
916
            }, {
917
                .name = "addr",
918
                .type = QEMU_OPT_STRING,
919
                .help = "PCI device address",
920
            }, {
921
                .name = "vectors",
922
                .type = QEMU_OPT_NUMBER,
923
                .help = "number of MSI-x vectors, 0 to disable MSI-X",
924
            },
925
            { /* end of list */ }
926
        },
927
#ifdef CONFIG_SLIRP
928
    }, {
929
        .type = "user",
930
        .init = net_init_slirp,
931
        .desc = {
932
            NET_COMMON_PARAMS_DESC,
933
            {
934
                .name = "hostname",
935
                .type = QEMU_OPT_STRING,
936
                .help = "client hostname reported by the builtin DHCP server",
937
            }, {
938
                .name = "restrict",
939
                .type = QEMU_OPT_STRING,
940
                .help = "isolate the guest from the host (y|yes|n|no)",
941
            }, {
942
                .name = "ip",
943
                .type = QEMU_OPT_STRING,
944
                .help = "legacy parameter, use net= instead",
945
            }, {
946
                .name = "net",
947
                .type = QEMU_OPT_STRING,
948
                .help = "IP address and optional netmask",
949
            }, {
950
                .name = "host",
951
                .type = QEMU_OPT_STRING,
952
                .help = "guest-visible address of the host",
953
            }, {
954
                .name = "tftp",
955
                .type = QEMU_OPT_STRING,
956
                .help = "root directory of the built-in TFTP server",
957
            }, {
958
                .name = "bootfile",
959
                .type = QEMU_OPT_STRING,
960
                .help = "BOOTP filename, for use with tftp=",
961
            }, {
962
                .name = "dhcpstart",
963
                .type = QEMU_OPT_STRING,
964
                .help = "the first of the 16 IPs the built-in DHCP server can assign",
965
            }, {
966
                .name = "dns",
967
                .type = QEMU_OPT_STRING,
968
                .help = "guest-visible address of the virtual nameserver",
969
            }, {
970
                .name = "smb",
971
                .type = QEMU_OPT_STRING,
972
                .help = "root directory of the built-in SMB server",
973
            }, {
974
                .name = "smbserver",
975
                .type = QEMU_OPT_STRING,
976
                .help = "IP address of the built-in SMB server",
977
            }, {
978
                .name = "hostfwd",
979
                .type = QEMU_OPT_STRING,
980
                .help = "guest port number to forward incoming TCP or UDP connections",
981
            }, {
982
                .name = "guestfwd",
983
                .type = QEMU_OPT_STRING,
984
                .help = "IP address and port to forward guest TCP connections",
985
            },
986
            { /* end of list */ }
987
        },
988
#endif
989
    }, {
990
        .type = "tap",
991
        .init = net_init_tap,
992
        .desc = {
993
            NET_COMMON_PARAMS_DESC,
994
            {
995
                .name = "ifname",
996
                .type = QEMU_OPT_STRING,
997
                .help = "interface name",
998
            },
999
#ifndef _WIN32
1000
            {
1001
                .name = "fd",
1002
                .type = QEMU_OPT_STRING,
1003
                .help = "file descriptor of an already opened tap",
1004
            }, {
1005
                .name = "script",
1006
                .type = QEMU_OPT_STRING,
1007
                .help = "script to initialize the interface",
1008
            }, {
1009
                .name = "downscript",
1010
                .type = QEMU_OPT_STRING,
1011
                .help = "script to shut down the interface",
1012
            }, {
1013
                .name = "sndbuf",
1014
                .type = QEMU_OPT_SIZE,
1015
                .help = "send buffer limit"
1016
            }, {
1017
                .name = "vnet_hdr",
1018
                .type = QEMU_OPT_BOOL,
1019
                .help = "enable the IFF_VNET_HDR flag on the tap interface"
1020
            }, {
1021
                .name = "vhost",
1022
                .type = QEMU_OPT_BOOL,
1023
                .help = "enable vhost-net network accelerator",
1024
            }, {
1025
                .name = "vhostfd",
1026
                .type = QEMU_OPT_STRING,
1027
                .help = "file descriptor of an already opened vhost net device",
1028
            },
1029
#endif /* _WIN32 */
1030
            { /* end of list */ }
1031
        },
1032
    }, {
1033
        .type = "socket",
1034
        .init = net_init_socket,
1035
        .desc = {
1036
            NET_COMMON_PARAMS_DESC,
1037
            {
1038
                .name = "fd",
1039
                .type = QEMU_OPT_STRING,
1040
                .help = "file descriptor of an already opened socket",
1041
            }, {
1042
                .name = "listen",
1043
                .type = QEMU_OPT_STRING,
1044
                .help = "port number, and optional hostname, to listen on",
1045
            }, {
1046
                .name = "connect",
1047
                .type = QEMU_OPT_STRING,
1048
                .help = "port number, and optional hostname, to connect to",
1049
            }, {
1050
                .name = "mcast",
1051
                .type = QEMU_OPT_STRING,
1052
                .help = "UDP multicast address and port number",
1053
            },
1054
            { /* end of list */ }
1055
        },
1056
#ifdef CONFIG_VDE
1057
    }, {
1058
        .type = "vde",
1059
        .init = net_init_vde,
1060
        .desc = {
1061
            NET_COMMON_PARAMS_DESC,
1062
            {
1063
                .name = "sock",
1064
                .type = QEMU_OPT_STRING,
1065
                .help = "socket path",
1066
            }, {
1067
                .name = "port",
1068
                .type = QEMU_OPT_NUMBER,
1069
                .help = "port number",
1070
            }, {
1071
                .name = "group",
1072
                .type = QEMU_OPT_STRING,
1073
                .help = "group owner of socket",
1074
            }, {
1075
                .name = "mode",
1076
                .type = QEMU_OPT_NUMBER,
1077
                .help = "permissions for socket",
1078
            },
1079
            { /* end of list */ }
1080
        },
1081
#endif
1082
    }, {
1083
        .type = "dump",
1084
        .init = net_init_dump,
1085
        .desc = {
1086
            NET_COMMON_PARAMS_DESC,
1087
            {
1088
                .name = "len",
1089
                .type = QEMU_OPT_SIZE,
1090
                .help = "per-packet size limit (64k default)",
1091
            }, {
1092
                .name = "file",
1093
                .type = QEMU_OPT_STRING,
1094
                .help = "dump file path (default is qemu-vlan0.pcap)",
1095
            },
1096
            { /* end of list */ }
1097
        },
1098
    },
1099
    { /* end of list */ }
1100
};
1101

    
1102
int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1103
{
1104
    const char *name;
1105
    const char *type;
1106
    int i;
1107

    
1108
    type = qemu_opt_get(opts, "type");
1109
    if (!type) {
1110
        qerror_report(QERR_MISSING_PARAMETER, "type");
1111
        return -1;
1112
    }
1113

    
1114
    if (is_netdev) {
1115
        if (strcmp(type, "tap") != 0 &&
1116
#ifdef CONFIG_SLIRP
1117
            strcmp(type, "user") != 0 &&
1118
#endif
1119
#ifdef CONFIG_VDE
1120
            strcmp(type, "vde") != 0 &&
1121
#endif
1122
            strcmp(type, "socket") != 0) {
1123
            qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1124
                          "a netdev backend type");
1125
            return -1;
1126
        }
1127

    
1128
        if (qemu_opt_get(opts, "vlan")) {
1129
            qerror_report(QERR_INVALID_PARAMETER, "vlan");
1130
            return -1;
1131
        }
1132
        if (qemu_opt_get(opts, "name")) {
1133
            qerror_report(QERR_INVALID_PARAMETER, "name");
1134
            return -1;
1135
        }
1136
        if (!qemu_opts_id(opts)) {
1137
            qerror_report(QERR_MISSING_PARAMETER, "id");
1138
            return -1;
1139
        }
1140
    }
1141

    
1142
    name = qemu_opts_id(opts);
1143
    if (!name) {
1144
        name = qemu_opt_get(opts, "name");
1145
    }
1146

    
1147
    for (i = 0; net_client_types[i].type != NULL; i++) {
1148
        if (!strcmp(net_client_types[i].type, type)) {
1149
            VLANState *vlan = NULL;
1150
            int ret;
1151

    
1152
            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1153
                return -1;
1154
            }
1155

    
1156
            /* Do not add to a vlan if it's a -netdev or a nic with a
1157
             * netdev= parameter. */
1158
            if (!(is_netdev ||
1159
                  (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
1160
                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1161
            }
1162

    
1163
            ret = 0;
1164
            if (net_client_types[i].init) {
1165
                ret = net_client_types[i].init(opts, mon, name, vlan);
1166
                if (ret < 0) {
1167
                    /* TODO push error reporting into init() methods */
1168
                    qerror_report(QERR_DEVICE_INIT_FAILED, type);
1169
                    return -1;
1170
                }
1171
            }
1172
            return ret;
1173
        }
1174
    }
1175

    
1176
    qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1177
                  "a network client type");
1178
    return -1;
1179
}
1180

    
1181
static int net_host_check_device(const char *device)
1182
{
1183
    int i;
1184
    const char *valid_param_list[] = { "tap", "socket", "dump"
1185
#ifdef CONFIG_SLIRP
1186
                                       ,"user"
1187
#endif
1188
#ifdef CONFIG_VDE
1189
                                       ,"vde"
1190
#endif
1191
    };
1192
    for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1193
        if (!strncmp(valid_param_list[i], device,
1194
                     strlen(valid_param_list[i])))
1195
            return 1;
1196
    }
1197

    
1198
    return 0;
1199
}
1200

    
1201
void net_host_device_add(Monitor *mon, const QDict *qdict)
1202
{
1203
    const char *device = qdict_get_str(qdict, "device");
1204
    const char *opts_str = qdict_get_try_str(qdict, "opts");
1205
    QemuOpts *opts;
1206

    
1207
    if (!net_host_check_device(device)) {
1208
        monitor_printf(mon, "invalid host network device %s\n", device);
1209
        return;
1210
    }
1211

    
1212
    opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1213
    if (!opts) {
1214
        return;
1215
    }
1216

    
1217
    qemu_opt_set(opts, "type", device);
1218

    
1219
    if (net_client_init(mon, opts, 0) < 0) {
1220
        monitor_printf(mon, "adding host network device %s failed\n", device);
1221
    }
1222
}
1223

    
1224
void net_host_device_remove(Monitor *mon, const QDict *qdict)
1225
{
1226
    VLANClientState *vc;
1227
    int vlan_id = qdict_get_int(qdict, "vlan_id");
1228
    const char *device = qdict_get_str(qdict, "device");
1229

    
1230
    vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1231
    if (!vc) {
1232
        return;
1233
    }
1234
    if (!net_host_check_device(vc->model)) {
1235
        monitor_printf(mon, "invalid host network device %s\n", device);
1236
        return;
1237
    }
1238
    qemu_del_vlan_client(vc);
1239
}
1240

    
1241
int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1242
{
1243
    QemuOpts *opts;
1244
    int res;
1245

    
1246
    opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1247
    if (!opts) {
1248
        return -1;
1249
    }
1250

    
1251
    res = net_client_init(mon, opts, 1);
1252
    if (res < 0) {
1253
        qemu_opts_del(opts);
1254
    }
1255

    
1256
    return res;
1257
}
1258

    
1259
int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1260
{
1261
    const char *id = qdict_get_str(qdict, "id");
1262
    VLANClientState *vc;
1263

    
1264
    vc = qemu_find_netdev(id);
1265
    if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
1266
        qerror_report(QERR_DEVICE_NOT_FOUND, id);
1267
        return -1;
1268
    }
1269
    qemu_del_vlan_client(vc);
1270
    qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1271
    return 0;
1272
}
1273

    
1274
void do_info_network(Monitor *mon)
1275
{
1276
    VLANState *vlan;
1277
    VLANClientState *vc;
1278

    
1279
    QTAILQ_FOREACH(vlan, &vlans, next) {
1280
        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1281

    
1282
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1283
            monitor_printf(mon, "  %s: %s\n", vc->name, vc->info_str);
1284
        }
1285
    }
1286
    monitor_printf(mon, "Devices not on any VLAN:\n");
1287
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1288
        monitor_printf(mon, "  %s: %s", vc->name, vc->info_str);
1289
        if (vc->peer) {
1290
            monitor_printf(mon, " peer=%s", vc->peer->name);
1291
        }
1292
        monitor_printf(mon, "\n");
1293
    }
1294
}
1295

    
1296
int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
1297
{
1298
    VLANState *vlan;
1299
    VLANClientState *vc = NULL;
1300
    const char *name = qdict_get_str(qdict, "name");
1301
    int up = qdict_get_bool(qdict, "up");
1302

    
1303
    QTAILQ_FOREACH(vlan, &vlans, next) {
1304
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1305
            if (strcmp(vc->name, name) == 0) {
1306
                goto done;
1307
            }
1308
        }
1309
    }
1310
    vc = qemu_find_netdev(name);
1311
done:
1312

    
1313
    if (!vc) {
1314
        qerror_report(QERR_DEVICE_NOT_FOUND, name);
1315
        return -1;
1316
    }
1317

    
1318
    vc->link_down = !up;
1319

    
1320
    if (vc->info->link_status_changed) {
1321
        vc->info->link_status_changed(vc);
1322
    }
1323
    return 0;
1324
}
1325

    
1326
void net_cleanup(void)
1327
{
1328
    VLANState *vlan;
1329
    VLANClientState *vc, *next_vc;
1330

    
1331
    QTAILQ_FOREACH(vlan, &vlans, next) {
1332
        QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1333
            qemu_del_vlan_client(vc);
1334
        }
1335
    }
1336

    
1337
    QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1338
        qemu_del_vlan_client(vc);
1339
    }
1340
}
1341

    
1342
void net_check_clients(void)
1343
{
1344
    VLANState *vlan;
1345
    VLANClientState *vc;
1346
    int has_nic = 0, has_host_dev = 0;
1347

    
1348
    QTAILQ_FOREACH(vlan, &vlans, next) {
1349
        QTAILQ_FOREACH(vc, &vlan->clients, next) {
1350
            switch (vc->info->type) {
1351
            case NET_CLIENT_TYPE_NIC:
1352
                has_nic = 1;
1353
                break;
1354
            case NET_CLIENT_TYPE_SLIRP:
1355
            case NET_CLIENT_TYPE_TAP:
1356
            case NET_CLIENT_TYPE_SOCKET:
1357
            case NET_CLIENT_TYPE_VDE:
1358
                has_host_dev = 1;
1359
                break;
1360
            default: ;
1361
            }
1362
        }
1363
        if (has_host_dev && !has_nic)
1364
            fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1365
        if (has_nic && !has_host_dev)
1366
            fprintf(stderr,
1367
                    "Warning: vlan %d is not connected to host network\n",
1368
                    vlan->id);
1369
    }
1370
    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1371
        if (!vc->peer) {
1372
            fprintf(stderr, "Warning: %s %s has no peer\n",
1373
                    vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1374
                    vc->name);
1375
        }
1376
    }
1377
}
1378

    
1379
static int net_init_client(QemuOpts *opts, void *dummy)
1380
{
1381
    if (net_client_init(NULL, opts, 0) < 0)
1382
        return -1;
1383
    return 0;
1384
}
1385

    
1386
static int net_init_netdev(QemuOpts *opts, void *dummy)
1387
{
1388
    return net_client_init(NULL, opts, 1);
1389
}
1390

    
1391
int net_init_clients(void)
1392
{
1393
    QemuOptsList *net = qemu_find_opts("net");
1394

    
1395
    if (default_net) {
1396
        /* if no clients, we use a default config */
1397
        qemu_opts_set(net, NULL, "type", "nic");
1398
#ifdef CONFIG_SLIRP
1399
        qemu_opts_set(net, NULL, "type", "user");
1400
#endif
1401
    }
1402

    
1403
    QTAILQ_INIT(&vlans);
1404
    QTAILQ_INIT(&non_vlan_clients);
1405

    
1406
    if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1407
        return -1;
1408

    
1409
    if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1410
        return -1;
1411
    }
1412

    
1413
    return 0;
1414
}
1415

    
1416
int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1417
{
1418
#if defined(CONFIG_SLIRP)
1419
    int ret;
1420
    if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1421
        return ret;
1422
    }
1423
#endif
1424

    
1425
    if (!qemu_opts_parse(opts_list, optarg, 1)) {
1426
        return -1;
1427
    }
1428

    
1429
    default_net = 0;
1430
    return 0;
1431
}