Statistics
| Branch: | Revision:

root / hw / virtio-net.c @ 8172539d

History | View | Annotate | Download (25.1 kB)

1
/*
2
 * Virtio Network Device
3
 *
4
 * Copyright IBM, Corp. 2007
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13

    
14
#include "virtio.h"
15
#include "net.h"
16
#include "net/checksum.h"
17
#include "net/tap.h"
18
#include "qemu-timer.h"
19
#include "virtio-net.h"
20

    
21
#define VIRTIO_NET_VM_VERSION    11
22

    
23
#define MAC_TABLE_ENTRIES    64
24
#define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
25

    
26
typedef struct VirtIONet
27
{
28
    VirtIODevice vdev;
29
    uint8_t mac[ETH_ALEN];
30
    uint16_t status;
31
    VirtQueue *rx_vq;
32
    VirtQueue *tx_vq;
33
    VirtQueue *ctrl_vq;
34
    NICState *nic;
35
    QEMUTimer *tx_timer;
36
    int tx_timer_active;
37
    uint32_t has_vnet_hdr;
38
    uint8_t has_ufo;
39
    struct {
40
        VirtQueueElement elem;
41
        ssize_t len;
42
    } async_tx;
43
    int mergeable_rx_bufs;
44
    uint8_t promisc;
45
    uint8_t allmulti;
46
    uint8_t alluni;
47
    uint8_t nomulti;
48
    uint8_t nouni;
49
    uint8_t nobcast;
50
    struct {
51
        int in_use;
52
        int first_multi;
53
        uint8_t multi_overflow;
54
        uint8_t uni_overflow;
55
        uint8_t *macs;
56
    } mac_table;
57
    uint32_t *vlans;
58
} VirtIONet;
59

    
60
/* TODO
61
 * - we could suppress RX interrupt if we were so inclined.
62
 */
63

    
64
static VirtIONet *to_virtio_net(VirtIODevice *vdev)
65
{
66
    return (VirtIONet *)vdev;
67
}
68

    
69
static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
70
{
71
    VirtIONet *n = to_virtio_net(vdev);
72
    struct virtio_net_config netcfg;
73

    
74
    netcfg.status = n->status;
75
    memcpy(netcfg.mac, n->mac, ETH_ALEN);
76
    memcpy(config, &netcfg, sizeof(netcfg));
77
}
78

    
79
static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
80
{
81
    VirtIONet *n = to_virtio_net(vdev);
82
    struct virtio_net_config netcfg;
83

    
84
    memcpy(&netcfg, config, sizeof(netcfg));
85

    
86
    if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
87
        memcpy(n->mac, netcfg.mac, ETH_ALEN);
88
        qemu_format_nic_info_str(&n->nic->nc, n->mac);
89
    }
90
}
91

    
92
static void virtio_net_set_link_status(VLANClientState *nc)
93
{
94
    VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
95
    uint16_t old_status = n->status;
96

    
97
    if (nc->link_down)
98
        n->status &= ~VIRTIO_NET_S_LINK_UP;
99
    else
100
        n->status |= VIRTIO_NET_S_LINK_UP;
101

    
102
    if (n->status != old_status)
103
        virtio_notify_config(&n->vdev);
104
}
105

    
106
static void virtio_net_reset(VirtIODevice *vdev)
107
{
108
    VirtIONet *n = to_virtio_net(vdev);
109

    
110
    /* Reset back to compatibility mode */
111
    n->promisc = 1;
112
    n->allmulti = 0;
113
    n->alluni = 0;
114
    n->nomulti = 0;
115
    n->nouni = 0;
116
    n->nobcast = 0;
117

    
118
    /* Flush any MAC and VLAN filter table state */
119
    n->mac_table.in_use = 0;
120
    n->mac_table.first_multi = 0;
121
    n->mac_table.multi_overflow = 0;
122
    n->mac_table.uni_overflow = 0;
123
    memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
124
    memset(n->vlans, 0, MAX_VLAN >> 3);
125
}
126

    
127
static int peer_has_vnet_hdr(VirtIONet *n)
128
{
129
    if (!n->nic->nc.peer)
130
        return 0;
131

    
132
    if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
133
        return 0;
134

    
135
    n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
136

    
137
    return n->has_vnet_hdr;
138
}
139

    
140
static int peer_has_ufo(VirtIONet *n)
141
{
142
    if (!peer_has_vnet_hdr(n))
143
        return 0;
144

    
145
    n->has_ufo = tap_has_ufo(n->nic->nc.peer);
146

    
147
    return n->has_ufo;
148
}
149

    
150
static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
151
{
152
    VirtIONet *n = to_virtio_net(vdev);
153

    
154
    if (peer_has_vnet_hdr(n)) {
155
        tap_using_vnet_hdr(n->nic->nc.peer, 1);
156
    } else {
157
        features &= ~(0x1 << VIRTIO_NET_F_CSUM);
158
        features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
159
        features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
160
        features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
161

    
162
        features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
163
        features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
164
        features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
165
        features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
166
    }
167

    
168
    if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
169
        features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
170
        features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
171
    }
172

    
173
    return features;
174
}
175

    
176
static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
177
{
178
    uint32_t features = 0;
179

    
180
    /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
181
     * but also these: */
182
    features |= (1 << VIRTIO_NET_F_MAC);
183
    features |= (1 << VIRTIO_NET_F_CSUM);
184
    features |= (1 << VIRTIO_NET_F_HOST_TSO4);
185
    features |= (1 << VIRTIO_NET_F_HOST_TSO6);
186
    features |= (1 << VIRTIO_NET_F_HOST_ECN);
187

    
188
    return features;
189
}
190

    
191
static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
192
{
193
    VirtIONet *n = to_virtio_net(vdev);
194

    
195
    n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
196

    
197
    if (n->has_vnet_hdr) {
198
        tap_set_offload(n->nic->nc.peer,
199
                        (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
200
                        (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
201
                        (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
202
                        (features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
203
                        (features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
204
    }
205
}
206

    
207
static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
208
                                     VirtQueueElement *elem)
209
{
210
    uint8_t on;
211

    
212
    if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
213
        fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
214
        exit(1);
215
    }
216

    
217
    on = ldub_p(elem->out_sg[1].iov_base);
218

    
219
    if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
220
        n->promisc = on;
221
    else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
222
        n->allmulti = on;
223
    else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
224
        n->alluni = on;
225
    else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
226
        n->nomulti = on;
227
    else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
228
        n->nouni = on;
229
    else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
230
        n->nobcast = on;
231
    else
232
        return VIRTIO_NET_ERR;
233

    
234
    return VIRTIO_NET_OK;
235
}
236

    
237
static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
238
                                 VirtQueueElement *elem)
239
{
240
    struct virtio_net_ctrl_mac mac_data;
241

    
242
    if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
243
        elem->out_sg[1].iov_len < sizeof(mac_data) ||
244
        elem->out_sg[2].iov_len < sizeof(mac_data))
245
        return VIRTIO_NET_ERR;
246

    
247
    n->mac_table.in_use = 0;
248
    n->mac_table.first_multi = 0;
249
    n->mac_table.uni_overflow = 0;
250
    n->mac_table.multi_overflow = 0;
251
    memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
252

    
253
    mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
254

    
255
    if (sizeof(mac_data.entries) +
256
        (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
257
        return VIRTIO_NET_ERR;
258

    
259
    if (mac_data.entries <= MAC_TABLE_ENTRIES) {
260
        memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
261
               mac_data.entries * ETH_ALEN);
262
        n->mac_table.in_use += mac_data.entries;
263
    } else {
264
        n->mac_table.uni_overflow = 1;
265
    }
266

    
267
    n->mac_table.first_multi = n->mac_table.in_use;
268

    
269
    mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
270

    
271
    if (sizeof(mac_data.entries) +
272
        (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
273
        return VIRTIO_NET_ERR;
274

    
275
    if (mac_data.entries) {
276
        if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
277
            memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
278
                   elem->out_sg[2].iov_base + sizeof(mac_data),
279
                   mac_data.entries * ETH_ALEN);
280
            n->mac_table.in_use += mac_data.entries;
281
        } else {
282
            n->mac_table.multi_overflow = 1;
283
        }
284
    }
285

    
286
    return VIRTIO_NET_OK;
287
}
288

    
289
static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
290
                                        VirtQueueElement *elem)
291
{
292
    uint16_t vid;
293

    
294
    if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
295
        fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
296
        return VIRTIO_NET_ERR;
297
    }
298

    
299
    vid = lduw_le_p(elem->out_sg[1].iov_base);
300

    
301
    if (vid >= MAX_VLAN)
302
        return VIRTIO_NET_ERR;
303

    
304
    if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
305
        n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
306
    else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
307
        n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
308
    else
309
        return VIRTIO_NET_ERR;
310

    
311
    return VIRTIO_NET_OK;
312
}
313

    
314
static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
315
{
316
    VirtIONet *n = to_virtio_net(vdev);
317
    struct virtio_net_ctrl_hdr ctrl;
318
    virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
319
    VirtQueueElement elem;
320

    
321
    while (virtqueue_pop(vq, &elem)) {
322
        if ((elem.in_num < 1) || (elem.out_num < 1)) {
323
            fprintf(stderr, "virtio-net ctrl missing headers\n");
324
            exit(1);
325
        }
326

    
327
        if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
328
            elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
329
            fprintf(stderr, "virtio-net ctrl header not in correct element\n");
330
            exit(1);
331
        }
332

    
333
        ctrl.class = ldub_p(elem.out_sg[0].iov_base);
334
        ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
335

    
336
        if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
337
            status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
338
        else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
339
            status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
340
        else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
341
            status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
342

    
343
        stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
344

    
345
        virtqueue_push(vq, &elem, sizeof(status));
346
        virtio_notify(vdev, vq);
347
    }
348
}
349

    
350
/* RX */
351

    
352
static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
353
{
354
    VirtIONet *n = to_virtio_net(vdev);
355

    
356
    qemu_flush_queued_packets(&n->nic->nc);
357

    
358
    /* We now have RX buffers, signal to the IO thread to break out of the
359
     * select to re-poll the tap file descriptor */
360
    qemu_notify_event();
361
}
362

    
363
static int virtio_net_can_receive(VLANClientState *nc)
364
{
365
    VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
366

    
367
    if (!virtio_queue_ready(n->rx_vq) ||
368
        !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
369
        return 0;
370

    
371
    return 1;
372
}
373

    
374
static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
375
{
376
    if (virtio_queue_empty(n->rx_vq) ||
377
        (n->mergeable_rx_bufs &&
378
         !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
379
        virtio_queue_set_notification(n->rx_vq, 1);
380
        return 0;
381
    }
382

    
383
    virtio_queue_set_notification(n->rx_vq, 0);
384
    return 1;
385
}
386

    
387
/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
388
 * it never finds out that the packets don't have valid checksums.  This
389
 * causes dhclient to get upset.  Fedora's carried a patch for ages to
390
 * fix this with Xen but it hasn't appeared in an upstream release of
391
 * dhclient yet.
392
 *
393
 * To avoid breaking existing guests, we catch udp packets and add
394
 * checksums.  This is terrible but it's better than hacking the guest
395
 * kernels.
396
 *
397
 * N.B. if we introduce a zero-copy API, this operation is no longer free so
398
 * we should provide a mechanism to disable it to avoid polluting the host
399
 * cache.
400
 */
401
static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
402
                                        const uint8_t *buf, size_t size)
403
{
404
    if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
405
        (size > 27 && size < 1500) && /* normal sized MTU */
406
        (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
407
        (buf[23] == 17) && /* ip.protocol == UDP */
408
        (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
409
        /* FIXME this cast is evil */
410
        net_checksum_calculate((uint8_t *)buf, size);
411
        hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
412
    }
413
}
414

    
415
static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
416
{
417
    int offset, i;
418

    
419
    offset = i = 0;
420
    while (offset < count && i < iovcnt) {
421
        int len = MIN(iov[i].iov_len, count - offset);
422
        memcpy(iov[i].iov_base, buf + offset, len);
423
        offset += len;
424
        i++;
425
    }
426

    
427
    return offset;
428
}
429

    
430
static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
431
                          const void *buf, size_t size, size_t hdr_len)
432
{
433
    struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
434
    int offset = 0;
435

    
436
    hdr->flags = 0;
437
    hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
438

    
439
    if (n->has_vnet_hdr) {
440
        memcpy(hdr, buf, sizeof(*hdr));
441
        offset = sizeof(*hdr);
442
        work_around_broken_dhclient(hdr, buf + offset, size - offset);
443
    }
444

    
445
    /* We only ever receive a struct virtio_net_hdr from the tapfd,
446
     * but we may be passing along a larger header to the guest.
447
     */
448
    iov[0].iov_base += hdr_len;
449
    iov[0].iov_len  -= hdr_len;
450

    
451
    return offset;
452
}
453

    
454
static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
455
{
456
    static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
457
    static const uint8_t vlan[] = {0x81, 0x00};
458
    uint8_t *ptr = (uint8_t *)buf;
459
    int i;
460

    
461
    if (n->promisc)
462
        return 1;
463

    
464
    if (n->has_vnet_hdr) {
465
        ptr += sizeof(struct virtio_net_hdr);
466
    }
467

    
468
    if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
469
        int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
470
        if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
471
            return 0;
472
    }
473

    
474
    if (ptr[0] & 1) { // multicast
475
        if (!memcmp(ptr, bcast, sizeof(bcast))) {
476
            return !n->nobcast;
477
        } else if (n->nomulti) {
478
            return 0;
479
        } else if (n->allmulti || n->mac_table.multi_overflow) {
480
            return 1;
481
        }
482

    
483
        for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
484
            if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
485
                return 1;
486
            }
487
        }
488
    } else { // unicast
489
        if (n->nouni) {
490
            return 0;
491
        } else if (n->alluni || n->mac_table.uni_overflow) {
492
            return 1;
493
        } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
494
            return 1;
495
        }
496

    
497
        for (i = 0; i < n->mac_table.first_multi; i++) {
498
            if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
499
                return 1;
500
            }
501
        }
502
    }
503

    
504
    return 0;
505
}
506

    
507
static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
508
{
509
    VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
510
    struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
511
    size_t hdr_len, offset, i;
512

    
513
    if (!virtio_net_can_receive(&n->nic->nc))
514
        return -1;
515

    
516
    if (!virtio_net_has_buffers(n, size))
517
        return 0;
518

    
519
    if (!receive_filter(n, buf, size))
520
        return size;
521

    
522
    /* hdr_len refers to the header we supply to the guest */
523
    hdr_len = n->mergeable_rx_bufs ?
524
        sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
525

    
526
    offset = i = 0;
527

    
528
    while (offset < size) {
529
        VirtQueueElement elem;
530
        int len, total;
531
        struct iovec sg[VIRTQUEUE_MAX_SIZE];
532

    
533
        len = total = 0;
534

    
535
        if ((i != 0 && !n->mergeable_rx_bufs) ||
536
            virtqueue_pop(n->rx_vq, &elem) == 0) {
537
            if (i == 0)
538
                return -1;
539
            fprintf(stderr, "virtio-net truncating packet\n");
540
            exit(1);
541
        }
542

    
543
        if (elem.in_num < 1) {
544
            fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
545
            exit(1);
546
        }
547

    
548
        if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
549
            fprintf(stderr, "virtio-net header not in first element\n");
550
            exit(1);
551
        }
552

    
553
        memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
554

    
555
        if (i == 0) {
556
            if (n->mergeable_rx_bufs)
557
                mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
558

    
559
            offset += receive_header(n, sg, elem.in_num,
560
                                     buf + offset, size - offset, hdr_len);
561
            total += hdr_len;
562
        }
563

    
564
        /* copy in packet.  ugh */
565
        len = iov_fill(sg, elem.in_num,
566
                       buf + offset, size - offset);
567
        total += len;
568

    
569
        /* signal other side */
570
        virtqueue_fill(n->rx_vq, &elem, total, i++);
571

    
572
        offset += len;
573
    }
574

    
575
    if (mhdr)
576
        mhdr->num_buffers = i;
577

    
578
    virtqueue_flush(n->rx_vq, i);
579
    virtio_notify(&n->vdev, n->rx_vq);
580

    
581
    return size;
582
}
583

    
584
static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
585

    
586
static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
587
{
588
    VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
589

    
590
    virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
591
    virtio_notify(&n->vdev, n->tx_vq);
592

    
593
    n->async_tx.elem.out_num = n->async_tx.len = 0;
594

    
595
    virtio_queue_set_notification(n->tx_vq, 1);
596
    virtio_net_flush_tx(n, n->tx_vq);
597
}
598

    
599
/* TX */
600
static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
601
{
602
    VirtQueueElement elem;
603

    
604
    if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
605
        return;
606

    
607
    if (n->async_tx.elem.out_num) {
608
        virtio_queue_set_notification(n->tx_vq, 0);
609
        return;
610
    }
611

    
612
    while (virtqueue_pop(vq, &elem)) {
613
        ssize_t ret, len = 0;
614
        unsigned int out_num = elem.out_num;
615
        struct iovec *out_sg = &elem.out_sg[0];
616
        unsigned hdr_len;
617

    
618
        /* hdr_len refers to the header received from the guest */
619
        hdr_len = n->mergeable_rx_bufs ?
620
            sizeof(struct virtio_net_hdr_mrg_rxbuf) :
621
            sizeof(struct virtio_net_hdr);
622

    
623
        if (out_num < 1 || out_sg->iov_len != hdr_len) {
624
            fprintf(stderr, "virtio-net header not in first element\n");
625
            exit(1);
626
        }
627

    
628
        /* ignore the header if GSO is not supported */
629
        if (!n->has_vnet_hdr) {
630
            out_num--;
631
            out_sg++;
632
            len += hdr_len;
633
        } else if (n->mergeable_rx_bufs) {
634
            /* tapfd expects a struct virtio_net_hdr */
635
            hdr_len -= sizeof(struct virtio_net_hdr);
636
            out_sg->iov_len -= hdr_len;
637
            len += hdr_len;
638
        }
639

    
640
        ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
641
                                      virtio_net_tx_complete);
642
        if (ret == 0) {
643
            virtio_queue_set_notification(n->tx_vq, 0);
644
            n->async_tx.elem = elem;
645
            n->async_tx.len  = len;
646
            return;
647
        }
648

    
649
        len += ret;
650

    
651
        virtqueue_push(vq, &elem, len);
652
        virtio_notify(&n->vdev, vq);
653
    }
654
}
655

    
656
static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
657
{
658
    VirtIONet *n = to_virtio_net(vdev);
659

    
660
    if (n->tx_timer_active) {
661
        virtio_queue_set_notification(vq, 1);
662
        qemu_del_timer(n->tx_timer);
663
        n->tx_timer_active = 0;
664
        virtio_net_flush_tx(n, vq);
665
    } else {
666
        qemu_mod_timer(n->tx_timer,
667
                       qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
668
        n->tx_timer_active = 1;
669
        virtio_queue_set_notification(vq, 0);
670
    }
671
}
672

    
673
static void virtio_net_tx_timer(void *opaque)
674
{
675
    VirtIONet *n = opaque;
676

    
677
    n->tx_timer_active = 0;
678

    
679
    /* Just in case the driver is not ready on more */
680
    if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
681
        return;
682

    
683
    virtio_queue_set_notification(n->tx_vq, 1);
684
    virtio_net_flush_tx(n, n->tx_vq);
685
}
686

    
687
static void virtio_net_save(QEMUFile *f, void *opaque)
688
{
689
    VirtIONet *n = opaque;
690

    
691
    virtio_save(&n->vdev, f);
692

    
693
    qemu_put_buffer(f, n->mac, ETH_ALEN);
694
    qemu_put_be32(f, n->tx_timer_active);
695
    qemu_put_be32(f, n->mergeable_rx_bufs);
696
    qemu_put_be16(f, n->status);
697
    qemu_put_byte(f, n->promisc);
698
    qemu_put_byte(f, n->allmulti);
699
    qemu_put_be32(f, n->mac_table.in_use);
700
    qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
701
    qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
702
    qemu_put_be32(f, n->has_vnet_hdr);
703
    qemu_put_byte(f, n->mac_table.multi_overflow);
704
    qemu_put_byte(f, n->mac_table.uni_overflow);
705
    qemu_put_byte(f, n->alluni);
706
    qemu_put_byte(f, n->nomulti);
707
    qemu_put_byte(f, n->nouni);
708
    qemu_put_byte(f, n->nobcast);
709
    qemu_put_byte(f, n->has_ufo);
710
}
711

    
712
static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
713
{
714
    VirtIONet *n = opaque;
715
    int i;
716

    
717
    if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
718
        return -EINVAL;
719

    
720
    virtio_load(&n->vdev, f);
721

    
722
    qemu_get_buffer(f, n->mac, ETH_ALEN);
723
    n->tx_timer_active = qemu_get_be32(f);
724
    n->mergeable_rx_bufs = qemu_get_be32(f);
725

    
726
    if (version_id >= 3)
727
        n->status = qemu_get_be16(f);
728

    
729
    if (version_id >= 4) {
730
        if (version_id < 8) {
731
            n->promisc = qemu_get_be32(f);
732
            n->allmulti = qemu_get_be32(f);
733
        } else {
734
            n->promisc = qemu_get_byte(f);
735
            n->allmulti = qemu_get_byte(f);
736
        }
737
    }
738

    
739
    if (version_id >= 5) {
740
        n->mac_table.in_use = qemu_get_be32(f);
741
        /* MAC_TABLE_ENTRIES may be different from the saved image */
742
        if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
743
            qemu_get_buffer(f, n->mac_table.macs,
744
                            n->mac_table.in_use * ETH_ALEN);
745
        } else if (n->mac_table.in_use) {
746
            qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
747
            n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
748
            n->mac_table.in_use = 0;
749
        }
750
    }
751
 
752
    if (version_id >= 6)
753
        qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
754

    
755
    if (version_id >= 7) {
756
        if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
757
            qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
758
            return -1;
759
        }
760

    
761
        if (n->has_vnet_hdr) {
762
            tap_using_vnet_hdr(n->nic->nc.peer, 1);
763
            tap_set_offload(n->nic->nc.peer,
764
                    (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
765
                    (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
766
                    (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
767
                    (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
768
                    (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
769
        }
770
    }
771

    
772
    if (version_id >= 9) {
773
        n->mac_table.multi_overflow = qemu_get_byte(f);
774
        n->mac_table.uni_overflow = qemu_get_byte(f);
775
    }
776

    
777
    if (version_id >= 10) {
778
        n->alluni = qemu_get_byte(f);
779
        n->nomulti = qemu_get_byte(f);
780
        n->nouni = qemu_get_byte(f);
781
        n->nobcast = qemu_get_byte(f);
782
    }
783

    
784
    if (version_id >= 11) {
785
        if (qemu_get_byte(f) && !peer_has_ufo(n)) {
786
            qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
787
            return -1;
788
        }
789
    }
790

    
791
    /* Find the first multicast entry in the saved MAC filter */
792
    for (i = 0; i < n->mac_table.in_use; i++) {
793
        if (n->mac_table.macs[i * ETH_ALEN] & 1) {
794
            break;
795
        }
796
    }
797
    n->mac_table.first_multi = i;
798

    
799
    if (n->tx_timer_active) {
800
        qemu_mod_timer(n->tx_timer,
801
                       qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
802
    }
803

    
804
    return 0;
805
}
806

    
807
static void virtio_net_cleanup(VLANClientState *nc)
808
{
809
    VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
810

    
811
    n->nic = NULL;
812
}
813

    
814
static NetClientInfo net_virtio_info = {
815
    .type = NET_CLIENT_TYPE_NIC,
816
    .size = sizeof(NICState),
817
    .can_receive = virtio_net_can_receive,
818
    .receive = virtio_net_receive,
819
        .cleanup = virtio_net_cleanup,
820
    .link_status_changed = virtio_net_set_link_status,
821
};
822

    
823
VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
824
{
825
    VirtIONet *n;
826
    static int virtio_net_id;
827

    
828
    n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
829
                                        sizeof(struct virtio_net_config),
830
                                        sizeof(VirtIONet));
831

    
832
    n->vdev.get_config = virtio_net_get_config;
833
    n->vdev.set_config = virtio_net_set_config;
834
    n->vdev.get_features = virtio_net_get_features;
835
    n->vdev.set_features = virtio_net_set_features;
836
    n->vdev.bad_features = virtio_net_bad_features;
837
    n->vdev.reset = virtio_net_reset;
838
    n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
839
    n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
840
    n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
841
    qemu_macaddr_default_if_unset(&conf->macaddr);
842
    memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
843
    n->status = VIRTIO_NET_S_LINK_UP;
844

    
845
    n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
846

    
847
    qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
848

    
849
    n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
850
    n->tx_timer_active = 0;
851
    n->mergeable_rx_bufs = 0;
852
    n->promisc = 1; /* for compatibility */
853

    
854
    n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
855

    
856
    n->vlans = qemu_mallocz(MAX_VLAN >> 3);
857

    
858
    register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
859
                    virtio_net_save, virtio_net_load, n);
860

    
861
    return &n->vdev;
862
}
863

    
864
void virtio_net_exit(VirtIODevice *vdev)
865
{
866
    VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
867

    
868
    qemu_purge_queued_packets(&n->nic->nc);
869

    
870
    unregister_savevm("virtio-net", n);
871

    
872
    qemu_free(n->mac_table.macs);
873
    qemu_free(n->vlans);
874

    
875
    qemu_del_timer(n->tx_timer);
876
    qemu_free_timer(n->tx_timer);
877

    
878
    virtio_cleanup(&n->vdev);
879
    qemu_del_vlan_client(&n->nic->nc);
880
}