packing update
[qemu] / hw / virtio-net.c
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 "qemu-timer.h"
17 #include "virtio-net.h"
18
19 #define VIRTIO_NET_VM_VERSION    6
20
21 #define MAC_TABLE_ENTRIES    32
22 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
23
24 typedef struct VirtIONet
25 {
26     VirtIODevice vdev;
27     uint8_t mac[ETH_ALEN];
28     uint16_t status;
29     VirtQueue *rx_vq;
30     VirtQueue *tx_vq;
31     VirtQueue *ctrl_vq;
32     VLANClientState *vc;
33     QEMUTimer *tx_timer;
34     int tx_timer_active;
35     int mergeable_rx_bufs;
36     int promisc;
37     int allmulti;
38     struct {
39         int in_use;
40         uint8_t *macs;
41     } mac_table;
42     uint32_t *vlans;
43 } VirtIONet;
44
45 /* TODO
46  * - we could suppress RX interrupt if we were so inclined.
47  */
48
49 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
50 {
51     return (VirtIONet *)vdev;
52 }
53
54 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
55 {
56     VirtIONet *n = to_virtio_net(vdev);
57     struct virtio_net_config netcfg;
58
59     netcfg.status = n->status;
60     memcpy(netcfg.mac, n->mac, ETH_ALEN);
61     memcpy(config, &netcfg, sizeof(netcfg));
62 }
63
64 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
65 {
66     VirtIONet *n = to_virtio_net(vdev);
67     struct virtio_net_config netcfg;
68
69     memcpy(&netcfg, config, sizeof(netcfg));
70
71     if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
72         memcpy(n->mac, netcfg.mac, ETH_ALEN);
73         qemu_format_nic_info_str(n->vc, n->mac);
74     }
75 }
76
77 static void virtio_net_set_link_status(VLANClientState *vc)
78 {
79     VirtIONet *n = vc->opaque;
80     uint16_t old_status = n->status;
81
82     if (vc->link_down)
83         n->status &= ~VIRTIO_NET_S_LINK_UP;
84     else
85         n->status |= VIRTIO_NET_S_LINK_UP;
86
87     if (n->status != old_status)
88         virtio_notify_config(&n->vdev);
89 }
90
91 static void virtio_net_reset(VirtIODevice *vdev)
92 {
93     VirtIONet *n = to_virtio_net(vdev);
94
95     /* Reset back to compatibility mode */
96     n->promisc = 1;
97     n->allmulti = 0;
98
99     /* Flush any MAC and VLAN filter table state */
100     n->mac_table.in_use = 0;
101     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
102     memset(n->vlans, 0, MAX_VLAN >> 3);
103 }
104
105 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
106 {
107     uint32_t features = (1 << VIRTIO_NET_F_MAC) |
108                         (1 << VIRTIO_NET_F_STATUS) |
109                         (1 << VIRTIO_NET_F_CTRL_VQ) |
110                         (1 << VIRTIO_NET_F_CTRL_RX) |
111                         (1 << VIRTIO_NET_F_CTRL_VLAN);
112
113     return features;
114 }
115
116 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
117 {
118     VirtIONet *n = to_virtio_net(vdev);
119
120     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
121 }
122
123 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
124                                      VirtQueueElement *elem)
125 {
126     uint8_t on;
127
128     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
129         fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
130         exit(1);
131     }
132
133     on = ldub_p(elem->out_sg[1].iov_base);
134
135     if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
136         n->promisc = on;
137     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
138         n->allmulti = on;
139     else
140         return VIRTIO_NET_ERR;
141
142     return VIRTIO_NET_OK;
143 }
144
145 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
146                                  VirtQueueElement *elem)
147 {
148     struct virtio_net_ctrl_mac mac_data;
149
150     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
151         elem->out_sg[1].iov_len < sizeof(mac_data) ||
152         elem->out_sg[2].iov_len < sizeof(mac_data))
153         return VIRTIO_NET_ERR;
154
155     n->mac_table.in_use = 0;
156     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
157
158     mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
159
160     if (sizeof(mac_data.entries) +
161         (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
162         return VIRTIO_NET_ERR;
163
164     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
165         memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
166                mac_data.entries * ETH_ALEN);
167         n->mac_table.in_use += mac_data.entries;
168     } else {
169         n->promisc = 1;
170         return VIRTIO_NET_OK;
171     }
172
173     mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
174
175     if (sizeof(mac_data.entries) +
176         (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
177         return VIRTIO_NET_ERR;
178
179     if (mac_data.entries) {
180         if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
181             memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
182                    elem->out_sg[2].iov_base + sizeof(mac_data),
183                    mac_data.entries * ETH_ALEN);
184             n->mac_table.in_use += mac_data.entries;
185         } else
186             n->allmulti = 1;
187     }
188
189     return VIRTIO_NET_OK;
190 }
191
192 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
193                                         VirtQueueElement *elem)
194 {
195     uint16_t vid;
196
197     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
198         fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
199         return VIRTIO_NET_ERR;
200     }
201
202     vid = lduw_le_p(elem->out_sg[1].iov_base);
203
204     if (vid >= MAX_VLAN)
205         return VIRTIO_NET_ERR;
206
207     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
208         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
209     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
210         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
211     else
212         return VIRTIO_NET_ERR;
213
214     return VIRTIO_NET_OK;
215 }
216
217 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
218 {
219     VirtIONet *n = to_virtio_net(vdev);
220     struct virtio_net_ctrl_hdr ctrl;
221     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
222     VirtQueueElement elem;
223
224     while (virtqueue_pop(vq, &elem)) {
225         if ((elem.in_num < 1) || (elem.out_num < 1)) {
226             fprintf(stderr, "virtio-net ctrl missing headers\n");
227             exit(1);
228         }
229
230         if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
231             elem.out_sg[elem.in_num - 1].iov_len < sizeof(status)) {
232             fprintf(stderr, "virtio-net ctrl header not in correct element\n");
233             exit(1);
234         }
235
236         ctrl.class = ldub_p(elem.out_sg[0].iov_base);
237         ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
238
239         if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
240             status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
241         else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
242             status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
243         else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
244             status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
245
246         stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
247
248         virtqueue_push(vq, &elem, sizeof(status));
249         virtio_notify(vdev, vq);
250     }
251 }
252
253 /* RX */
254
255 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
256 {
257 }
258
259 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
260 {
261     if (!virtio_queue_ready(n->rx_vq) ||
262         !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
263         return 0;
264
265     if (virtio_queue_empty(n->rx_vq) ||
266         (n->mergeable_rx_bufs &&
267          !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
268         virtio_queue_set_notification(n->rx_vq, 1);
269         return 0;
270     }
271
272     virtio_queue_set_notification(n->rx_vq, 0);
273     return 1;
274 }
275
276 static int virtio_net_can_receive(void *opaque)
277 {
278     VirtIONet *n = opaque;
279
280     return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
281 }
282
283 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
284 {
285     int offset, i;
286
287     offset = i = 0;
288     while (offset < count && i < iovcnt) {
289         int len = MIN(iov[i].iov_len, count - offset);
290         memcpy(iov[i].iov_base, buf + offset, len);
291         offset += len;
292         i++;
293     }
294
295     return offset;
296 }
297
298 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
299                           const void *buf, size_t size, size_t hdr_len)
300 {
301     struct virtio_net_hdr *hdr = iov[0].iov_base;
302     int offset = 0;
303
304     hdr->flags = 0;
305     hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
306
307     /* We only ever receive a struct virtio_net_hdr from the tapfd,
308      * but we may be passing along a larger header to the guest.
309      */
310     iov[0].iov_base += hdr_len;
311     iov[0].iov_len  -= hdr_len;
312
313     return offset;
314 }
315
316 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
317 {
318     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
319     static const uint8_t vlan[] = {0x81, 0x00};
320     uint8_t *ptr = (uint8_t *)buf;
321     int i;
322
323     if (n->promisc)
324         return 1;
325
326 #ifdef TAP_VNET_HDR
327     if (tap_has_vnet_hdr(n->vc->vlan->first_client))
328         ptr += sizeof(struct virtio_net_hdr);
329 #endif
330
331     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
332         int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
333         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
334             return 0;
335     }
336
337     if ((ptr[0] & 1) && n->allmulti)
338         return 1;
339
340     if (!memcmp(ptr, bcast, sizeof(bcast)))
341         return 1;
342
343     if (!memcmp(ptr, n->mac, ETH_ALEN))
344         return 1;
345
346     for (i = 0; i < n->mac_table.in_use; i++) {
347         if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
348             return 1;
349     }
350
351     return 0;
352 }
353
354 static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
355 {
356     VirtIONet *n = opaque;
357     struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
358     size_t hdr_len, offset, i;
359
360     if (!do_virtio_net_can_receive(n, size))
361         return;
362
363     if (!receive_filter(n, buf, size))
364         return;
365
366     /* hdr_len refers to the header we supply to the guest */
367     hdr_len = n->mergeable_rx_bufs ?
368         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
369
370     offset = i = 0;
371
372     while (offset < size) {
373         VirtQueueElement elem;
374         int len, total;
375         struct iovec sg[VIRTQUEUE_MAX_SIZE];
376
377         len = total = 0;
378
379         if ((i != 0 && !n->mergeable_rx_bufs) ||
380             virtqueue_pop(n->rx_vq, &elem) == 0) {
381             if (i == 0)
382                 return;
383             fprintf(stderr, "virtio-net truncating packet\n");
384             exit(1);
385         }
386
387         if (elem.in_num < 1) {
388             fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
389             exit(1);
390         }
391
392         if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
393             fprintf(stderr, "virtio-net header not in first element\n");
394             exit(1);
395         }
396
397         memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
398
399         if (i == 0) {
400             if (n->mergeable_rx_bufs)
401                 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
402
403             offset += receive_header(n, sg, elem.in_num,
404                                      buf + offset, size - offset, hdr_len);
405             total += hdr_len;
406         }
407
408         /* copy in packet.  ugh */
409         len = iov_fill(sg, elem.in_num,
410                        buf + offset, size - offset);
411         total += len;
412
413         /* signal other side */
414         virtqueue_fill(n->rx_vq, &elem, total, i++);
415
416         offset += len;
417     }
418
419     if (mhdr)
420         mhdr->num_buffers = i;
421
422     virtqueue_flush(n->rx_vq, i);
423     virtio_notify(&n->vdev, n->rx_vq);
424 }
425
426 /* TX */
427 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
428 {
429     VirtQueueElement elem;
430     int has_vnet_hdr = 0;
431
432     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
433         return;
434
435     while (virtqueue_pop(vq, &elem)) {
436         ssize_t len = 0;
437         unsigned int out_num = elem.out_num;
438         struct iovec *out_sg = &elem.out_sg[0];
439         unsigned hdr_len;
440
441         /* hdr_len refers to the header received from the guest */
442         hdr_len = n->mergeable_rx_bufs ?
443             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
444             sizeof(struct virtio_net_hdr);
445
446         if (out_num < 1 || out_sg->iov_len != hdr_len) {
447             fprintf(stderr, "virtio-net header not in first element\n");
448             exit(1);
449         }
450
451         /* ignore the header if GSO is not supported */
452         if (!has_vnet_hdr) {
453             out_num--;
454             out_sg++;
455             len += hdr_len;
456         } else if (n->mergeable_rx_bufs) {
457             /* tapfd expects a struct virtio_net_hdr */
458             hdr_len -= sizeof(struct virtio_net_hdr);
459             out_sg->iov_len -= hdr_len;
460             len += hdr_len;
461         }
462
463         len += qemu_sendv_packet(n->vc, out_sg, out_num);
464
465         virtqueue_push(vq, &elem, len);
466         virtio_notify(&n->vdev, vq);
467     }
468 }
469
470 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
471 {
472     VirtIONet *n = to_virtio_net(vdev);
473
474     if (n->tx_timer_active) {
475         virtio_queue_set_notification(vq, 1);
476         qemu_del_timer(n->tx_timer);
477         n->tx_timer_active = 0;
478         virtio_net_flush_tx(n, vq);
479     } else {
480         qemu_mod_timer(n->tx_timer,
481                        qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
482         n->tx_timer_active = 1;
483         virtio_queue_set_notification(vq, 0);
484     }
485 }
486
487 static void virtio_net_tx_timer(void *opaque)
488 {
489     VirtIONet *n = opaque;
490
491     n->tx_timer_active = 0;
492
493     /* Just in case the driver is not ready on more */
494     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
495         return;
496
497     virtio_queue_set_notification(n->tx_vq, 1);
498     virtio_net_flush_tx(n, n->tx_vq);
499 }
500
501 static void virtio_net_save(QEMUFile *f, void *opaque)
502 {
503     VirtIONet *n = opaque;
504
505     virtio_save(&n->vdev, f);
506
507     qemu_put_buffer(f, n->mac, ETH_ALEN);
508     qemu_put_be32(f, n->tx_timer_active);
509     qemu_put_be32(f, n->mergeable_rx_bufs);
510     qemu_put_be16(f, n->status);
511     qemu_put_be32(f, n->promisc);
512     qemu_put_be32(f, n->allmulti);
513     qemu_put_be32(f, n->mac_table.in_use);
514     qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
515     qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
516 }
517
518 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
519 {
520     VirtIONet *n = opaque;
521
522     if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
523         return -EINVAL;
524
525     virtio_load(&n->vdev, f);
526
527     qemu_get_buffer(f, n->mac, ETH_ALEN);
528     n->tx_timer_active = qemu_get_be32(f);
529     n->mergeable_rx_bufs = qemu_get_be32(f);
530
531     if (version_id >= 3)
532         n->status = qemu_get_be16(f);
533
534     if (version_id >= 4) {
535         n->promisc = qemu_get_be32(f);
536         n->allmulti = qemu_get_be32(f);
537     }
538
539     if (version_id >= 5) {
540         n->mac_table.in_use = qemu_get_be32(f);
541         /* MAC_TABLE_ENTRIES may be different from the saved image */
542         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
543             qemu_get_buffer(f, n->mac_table.macs,
544                             n->mac_table.in_use * ETH_ALEN);
545         } else if (n->mac_table.in_use) {
546             qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
547             n->promisc = 1;
548             n->mac_table.in_use = 0;
549         }
550     }
551  
552     if (version_id >= 6)
553         qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
554
555     if (n->tx_timer_active) {
556         qemu_mod_timer(n->tx_timer,
557                        qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
558     }
559
560     return 0;
561 }
562
563 PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
564 {
565     VirtIONet *n;
566     static int virtio_net_id;
567
568     n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
569                                      PCI_VENDOR_ID_REDHAT_QUMRANET,
570                                      PCI_DEVICE_ID_VIRTIO_NET,
571                                      PCI_VENDOR_ID_REDHAT_QUMRANET,
572                                      VIRTIO_ID_NET,
573                                      PCI_CLASS_NETWORK_ETHERNET, 0x00,
574                                      sizeof(struct virtio_net_config),
575                                      sizeof(VirtIONet));
576     if (!n)
577         return NULL;
578
579     n->vdev.get_config = virtio_net_get_config;
580     n->vdev.set_config = virtio_net_set_config;
581     n->vdev.get_features = virtio_net_get_features;
582     n->vdev.set_features = virtio_net_set_features;
583     n->vdev.reset = virtio_net_reset;
584     n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
585     n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
586     n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
587     memcpy(n->mac, nd->macaddr, ETH_ALEN);
588     n->status = VIRTIO_NET_S_LINK_UP;
589     n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
590                                  virtio_net_receive, virtio_net_can_receive, n);
591     n->vc->link_status_changed = virtio_net_set_link_status;
592
593     qemu_format_nic_info_str(n->vc, n->mac);
594
595     n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
596     n->tx_timer_active = 0;
597     n->mergeable_rx_bufs = 0;
598     n->promisc = 1; /* for compatibility */
599
600     n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
601
602     n->vlans = qemu_mallocz(MAX_VLAN >> 3);
603
604     register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
605                     virtio_net_save, virtio_net_load, n);
606     return (PCIDevice *)n;
607 }