virtio-net: Fix MAC filter overflow handling
[qemu.git] / hw / virtio-net.c
blob8c38454ae12b1f9babee7abbe933a08799eb9a66
1 /*
2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "virtio.h"
15 #include "net.h"
16 #include "qemu-timer.h"
17 #include "virtio-net.h"
19 #define VIRTIO_NET_VM_VERSION 9
21 #define MAC_TABLE_ENTRIES 32
22 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
24 typedef struct VirtIONet
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 uint8_t promisc;
37 uint8_t allmulti;
38 struct {
39 int in_use;
40 uint8_t multi_overflow;
41 uint8_t uni_overflow;
42 uint8_t *macs;
43 } mac_table;
44 uint32_t *vlans;
45 } VirtIONet;
47 /* TODO
48 * - we could suppress RX interrupt if we were so inclined.
51 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
53 return (VirtIONet *)vdev;
56 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
58 VirtIONet *n = to_virtio_net(vdev);
59 struct virtio_net_config netcfg;
61 netcfg.status = n->status;
62 memcpy(netcfg.mac, n->mac, ETH_ALEN);
63 memcpy(config, &netcfg, sizeof(netcfg));
66 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
68 VirtIONet *n = to_virtio_net(vdev);
69 struct virtio_net_config netcfg;
71 memcpy(&netcfg, config, sizeof(netcfg));
73 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
74 memcpy(n->mac, netcfg.mac, ETH_ALEN);
75 qemu_format_nic_info_str(n->vc, n->mac);
79 static void virtio_net_set_link_status(VLANClientState *vc)
81 VirtIONet *n = vc->opaque;
82 uint16_t old_status = n->status;
84 if (vc->link_down)
85 n->status &= ~VIRTIO_NET_S_LINK_UP;
86 else
87 n->status |= VIRTIO_NET_S_LINK_UP;
89 if (n->status != old_status)
90 virtio_notify_config(&n->vdev);
93 static void virtio_net_reset(VirtIODevice *vdev)
95 VirtIONet *n = to_virtio_net(vdev);
97 /* Reset back to compatibility mode */
98 n->promisc = 1;
99 n->allmulti = 0;
101 /* Flush any MAC and VLAN filter table state */
102 n->mac_table.in_use = 0;
103 n->mac_table.multi_overflow = 0;
104 n->mac_table.uni_overflow = 0;
105 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
106 memset(n->vlans, 0, MAX_VLAN >> 3);
109 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
111 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
112 (1 << VIRTIO_NET_F_STATUS) |
113 (1 << VIRTIO_NET_F_CTRL_VQ) |
114 (1 << VIRTIO_NET_F_CTRL_RX) |
115 (1 << VIRTIO_NET_F_CTRL_VLAN);
117 return features;
120 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
122 uint32_t features = 0;
124 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
125 * but also these: */
126 features |= (1 << VIRTIO_NET_F_MAC);
127 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
128 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
129 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
130 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
132 return features & virtio_net_get_features(vdev);
135 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
137 VirtIONet *n = to_virtio_net(vdev);
139 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
142 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
143 VirtQueueElement *elem)
145 uint8_t on;
147 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
148 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
149 exit(1);
152 on = ldub_p(elem->out_sg[1].iov_base);
154 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
155 n->promisc = on;
156 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
157 n->allmulti = on;
158 else
159 return VIRTIO_NET_ERR;
161 return VIRTIO_NET_OK;
164 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
165 VirtQueueElement *elem)
167 struct virtio_net_ctrl_mac mac_data;
169 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
170 elem->out_sg[1].iov_len < sizeof(mac_data) ||
171 elem->out_sg[2].iov_len < sizeof(mac_data))
172 return VIRTIO_NET_ERR;
174 n->mac_table.in_use = 0;
175 n->mac_table.uni_overflow = 0;
176 n->mac_table.multi_overflow = 0;
177 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
179 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
181 if (sizeof(mac_data.entries) +
182 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
183 return VIRTIO_NET_ERR;
185 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
186 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
187 mac_data.entries * ETH_ALEN);
188 n->mac_table.in_use += mac_data.entries;
189 } else {
190 n->mac_table.uni_overflow = 1;
193 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
195 if (sizeof(mac_data.entries) +
196 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
197 return VIRTIO_NET_ERR;
199 if (mac_data.entries) {
200 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
201 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
202 elem->out_sg[2].iov_base + sizeof(mac_data),
203 mac_data.entries * ETH_ALEN);
204 n->mac_table.in_use += mac_data.entries;
205 } else {
206 n->mac_table.multi_overflow = 1;
210 return VIRTIO_NET_OK;
213 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
214 VirtQueueElement *elem)
216 uint16_t vid;
218 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
219 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
220 return VIRTIO_NET_ERR;
223 vid = lduw_le_p(elem->out_sg[1].iov_base);
225 if (vid >= MAX_VLAN)
226 return VIRTIO_NET_ERR;
228 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
229 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
230 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
231 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
232 else
233 return VIRTIO_NET_ERR;
235 return VIRTIO_NET_OK;
238 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
240 VirtIONet *n = to_virtio_net(vdev);
241 struct virtio_net_ctrl_hdr ctrl;
242 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
243 VirtQueueElement elem;
245 while (virtqueue_pop(vq, &elem)) {
246 if ((elem.in_num < 1) || (elem.out_num < 1)) {
247 fprintf(stderr, "virtio-net ctrl missing headers\n");
248 exit(1);
251 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
252 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
253 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
254 exit(1);
257 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
258 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
260 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
261 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
262 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
263 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
264 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
265 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
267 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
269 virtqueue_push(vq, &elem, sizeof(status));
270 virtio_notify(vdev, vq);
274 /* RX */
276 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
278 VirtIONet *n = to_virtio_net(vdev);
280 qemu_flush_queued_packets(n->vc);
283 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
285 if (!virtio_queue_ready(n->rx_vq) ||
286 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
287 return 0;
289 if (virtio_queue_empty(n->rx_vq) ||
290 (n->mergeable_rx_bufs &&
291 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
292 virtio_queue_set_notification(n->rx_vq, 1);
293 return 0;
296 virtio_queue_set_notification(n->rx_vq, 0);
297 return 1;
300 static int virtio_net_can_receive(VLANClientState *vc)
302 VirtIONet *n = vc->opaque;
304 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
307 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
309 int offset, i;
311 offset = i = 0;
312 while (offset < count && i < iovcnt) {
313 int len = MIN(iov[i].iov_len, count - offset);
314 memcpy(iov[i].iov_base, buf + offset, len);
315 offset += len;
316 i++;
319 return offset;
322 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
323 const void *buf, size_t size, size_t hdr_len)
325 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
326 int offset = 0;
328 hdr->flags = 0;
329 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
331 /* We only ever receive a struct virtio_net_hdr from the tapfd,
332 * but we may be passing along a larger header to the guest.
334 iov[0].iov_base += hdr_len;
335 iov[0].iov_len -= hdr_len;
337 return offset;
340 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
342 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
343 static const uint8_t vlan[] = {0x81, 0x00};
344 uint8_t *ptr = (uint8_t *)buf;
345 int i;
347 if (n->promisc)
348 return 1;
350 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
351 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
352 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
353 return 0;
356 if (ptr[0] & 1) { // multicast
357 if (!memcmp(ptr, bcast, sizeof(bcast))) {
358 return 1;
359 } else if (n->allmulti || n->mac_table.multi_overflow) {
360 return 1;
362 } else { // unicast
363 if (n->mac_table.uni_overflow) {
364 return 1;
365 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
366 return 1;
370 for (i = 0; i < n->mac_table.in_use; i++) {
371 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
372 return 1;
375 return 0;
378 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
380 VirtIONet *n = vc->opaque;
381 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
382 size_t hdr_len, offset, i;
384 if (!do_virtio_net_can_receive(n, size))
385 return 0;
387 if (!receive_filter(n, buf, size))
388 return size;
390 /* hdr_len refers to the header we supply to the guest */
391 hdr_len = n->mergeable_rx_bufs ?
392 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
394 offset = i = 0;
396 while (offset < size) {
397 VirtQueueElement elem;
398 int len, total;
399 struct iovec sg[VIRTQUEUE_MAX_SIZE];
401 len = total = 0;
403 if ((i != 0 && !n->mergeable_rx_bufs) ||
404 virtqueue_pop(n->rx_vq, &elem) == 0) {
405 if (i == 0)
406 return -1;
407 fprintf(stderr, "virtio-net truncating packet\n");
408 exit(1);
411 if (elem.in_num < 1) {
412 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
413 exit(1);
416 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
417 fprintf(stderr, "virtio-net header not in first element\n");
418 exit(1);
421 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
423 if (i == 0) {
424 if (n->mergeable_rx_bufs)
425 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
427 offset += receive_header(n, sg, elem.in_num,
428 buf + offset, size - offset, hdr_len);
429 total += hdr_len;
432 /* copy in packet. ugh */
433 len = iov_fill(sg, elem.in_num,
434 buf + offset, size - offset);
435 total += len;
437 /* signal other side */
438 virtqueue_fill(n->rx_vq, &elem, total, i++);
440 offset += len;
443 if (mhdr)
444 mhdr->num_buffers = i;
446 virtqueue_flush(n->rx_vq, i);
447 virtio_notify(&n->vdev, n->rx_vq);
449 return size;
452 /* TX */
453 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
455 VirtQueueElement elem;
456 int has_vnet_hdr = 0;
458 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
459 return;
461 while (virtqueue_pop(vq, &elem)) {
462 ssize_t len = 0;
463 unsigned int out_num = elem.out_num;
464 struct iovec *out_sg = &elem.out_sg[0];
465 unsigned hdr_len;
467 /* hdr_len refers to the header received from the guest */
468 hdr_len = n->mergeable_rx_bufs ?
469 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
470 sizeof(struct virtio_net_hdr);
472 if (out_num < 1 || out_sg->iov_len != hdr_len) {
473 fprintf(stderr, "virtio-net header not in first element\n");
474 exit(1);
477 /* ignore the header if GSO is not supported */
478 if (!has_vnet_hdr) {
479 out_num--;
480 out_sg++;
481 len += hdr_len;
482 } else if (n->mergeable_rx_bufs) {
483 /* tapfd expects a struct virtio_net_hdr */
484 hdr_len -= sizeof(struct virtio_net_hdr);
485 out_sg->iov_len -= hdr_len;
486 len += hdr_len;
489 len += qemu_sendv_packet(n->vc, out_sg, out_num);
491 virtqueue_push(vq, &elem, len);
492 virtio_notify(&n->vdev, vq);
496 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
498 VirtIONet *n = to_virtio_net(vdev);
500 if (n->tx_timer_active) {
501 virtio_queue_set_notification(vq, 1);
502 qemu_del_timer(n->tx_timer);
503 n->tx_timer_active = 0;
504 virtio_net_flush_tx(n, vq);
505 } else {
506 qemu_mod_timer(n->tx_timer,
507 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
508 n->tx_timer_active = 1;
509 virtio_queue_set_notification(vq, 0);
513 static void virtio_net_tx_timer(void *opaque)
515 VirtIONet *n = opaque;
517 n->tx_timer_active = 0;
519 /* Just in case the driver is not ready on more */
520 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
521 return;
523 virtio_queue_set_notification(n->tx_vq, 1);
524 virtio_net_flush_tx(n, n->tx_vq);
527 static void virtio_net_save(QEMUFile *f, void *opaque)
529 VirtIONet *n = opaque;
531 virtio_save(&n->vdev, f);
533 qemu_put_buffer(f, n->mac, ETH_ALEN);
534 qemu_put_be32(f, n->tx_timer_active);
535 qemu_put_be32(f, n->mergeable_rx_bufs);
536 qemu_put_be16(f, n->status);
537 qemu_put_byte(f, n->promisc);
538 qemu_put_byte(f, n->allmulti);
539 qemu_put_be32(f, n->mac_table.in_use);
540 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
541 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
542 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
543 qemu_put_byte(f, n->mac_table.multi_overflow);
544 qemu_put_byte(f, n->mac_table.uni_overflow);
547 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
549 VirtIONet *n = opaque;
551 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
552 return -EINVAL;
554 virtio_load(&n->vdev, f);
556 qemu_get_buffer(f, n->mac, ETH_ALEN);
557 n->tx_timer_active = qemu_get_be32(f);
558 n->mergeable_rx_bufs = qemu_get_be32(f);
560 if (version_id >= 3)
561 n->status = qemu_get_be16(f);
563 if (version_id >= 4) {
564 if (version_id < 8) {
565 n->promisc = qemu_get_be32(f);
566 n->allmulti = qemu_get_be32(f);
567 } else {
568 n->promisc = qemu_get_byte(f);
569 n->allmulti = qemu_get_byte(f);
573 if (version_id >= 5) {
574 n->mac_table.in_use = qemu_get_be32(f);
575 /* MAC_TABLE_ENTRIES may be different from the saved image */
576 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
577 qemu_get_buffer(f, n->mac_table.macs,
578 n->mac_table.in_use * ETH_ALEN);
579 } else if (n->mac_table.in_use) {
580 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
581 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
582 n->mac_table.in_use = 0;
586 if (version_id >= 6)
587 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
589 if (version_id >= 7 && qemu_get_be32(f)) {
590 fprintf(stderr,
591 "virtio-net: saved image requires vnet header support\n");
592 exit(1);
595 if (version_id >= 9) {
596 n->mac_table.multi_overflow = qemu_get_byte(f);
597 n->mac_table.uni_overflow = qemu_get_byte(f);
600 if (n->tx_timer_active) {
601 qemu_mod_timer(n->tx_timer,
602 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
605 return 0;
608 static void virtio_net_cleanup(VLANClientState *vc)
610 VirtIONet *n = vc->opaque;
612 unregister_savevm("virtio-net", n);
614 qemu_free(n->mac_table.macs);
615 qemu_free(n->vlans);
617 qemu_del_timer(n->tx_timer);
618 qemu_free_timer(n->tx_timer);
620 virtio_cleanup(&n->vdev);
623 VirtIODevice *virtio_net_init(DeviceState *dev)
625 VirtIONet *n;
626 static int virtio_net_id;
628 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
629 sizeof(struct virtio_net_config),
630 sizeof(VirtIONet));
632 n->vdev.get_config = virtio_net_get_config;
633 n->vdev.set_config = virtio_net_set_config;
634 n->vdev.get_features = virtio_net_get_features;
635 n->vdev.set_features = virtio_net_set_features;
636 n->vdev.bad_features = virtio_net_bad_features;
637 n->vdev.reset = virtio_net_reset;
638 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
639 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
640 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
641 qdev_get_macaddr(dev, n->mac);
642 n->status = VIRTIO_NET_S_LINK_UP;
643 n->vc = qdev_get_vlan_client(dev,
644 virtio_net_can_receive,
645 virtio_net_receive, NULL,
646 virtio_net_cleanup, n);
647 n->vc->link_status_changed = virtio_net_set_link_status;
649 qemu_format_nic_info_str(n->vc, n->mac);
651 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
652 n->tx_timer_active = 0;
653 n->mergeable_rx_bufs = 0;
654 n->promisc = 1; /* for compatibility */
656 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
658 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
660 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
661 virtio_net_save, virtio_net_load, n);
663 return &n->vdev;