virtio-net: reorganize receive_filter()
[qemu-kvm/fedora.git] / hw / virtio-net.c
blob445976a41a46bc442932775d4c87e40a782039a4
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 8
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 *macs;
41 } mac_table;
42 uint32_t *vlans;
43 } VirtIONet;
45 /* TODO
46 * - we could suppress RX interrupt if we were so inclined.
49 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
51 return (VirtIONet *)vdev;
54 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
56 VirtIONet *n = to_virtio_net(vdev);
57 struct virtio_net_config netcfg;
59 netcfg.status = n->status;
60 memcpy(netcfg.mac, n->mac, ETH_ALEN);
61 memcpy(config, &netcfg, sizeof(netcfg));
64 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
66 VirtIONet *n = to_virtio_net(vdev);
67 struct virtio_net_config netcfg;
69 memcpy(&netcfg, config, sizeof(netcfg));
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);
77 static void virtio_net_set_link_status(VLANClientState *vc)
79 VirtIONet *n = vc->opaque;
80 uint16_t old_status = n->status;
82 if (vc->link_down)
83 n->status &= ~VIRTIO_NET_S_LINK_UP;
84 else
85 n->status |= VIRTIO_NET_S_LINK_UP;
87 if (n->status != old_status)
88 virtio_notify_config(&n->vdev);
91 static void virtio_net_reset(VirtIODevice *vdev)
93 VirtIONet *n = to_virtio_net(vdev);
95 /* Reset back to compatibility mode */
96 n->promisc = 1;
97 n->allmulti = 0;
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);
105 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
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);
113 return features;
116 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
118 uint32_t features = 0;
120 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
121 * but also these: */
122 features |= (1 << VIRTIO_NET_F_MAC);
123 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
124 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
125 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
126 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
128 return features & virtio_net_get_features(vdev);
131 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
133 VirtIONet *n = to_virtio_net(vdev);
135 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
138 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
139 VirtQueueElement *elem)
141 uint8_t on;
143 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
144 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
145 exit(1);
148 on = ldub_p(elem->out_sg[1].iov_base);
150 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
151 n->promisc = on;
152 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
153 n->allmulti = on;
154 else
155 return VIRTIO_NET_ERR;
157 return VIRTIO_NET_OK;
160 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
161 VirtQueueElement *elem)
163 struct virtio_net_ctrl_mac mac_data;
165 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
166 elem->out_sg[1].iov_len < sizeof(mac_data) ||
167 elem->out_sg[2].iov_len < sizeof(mac_data))
168 return VIRTIO_NET_ERR;
170 n->mac_table.in_use = 0;
171 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
173 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
175 if (sizeof(mac_data.entries) +
176 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
177 return VIRTIO_NET_ERR;
179 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
180 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
181 mac_data.entries * ETH_ALEN);
182 n->mac_table.in_use += mac_data.entries;
183 } else {
184 n->promisc = 1;
185 return VIRTIO_NET_OK;
188 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
190 if (sizeof(mac_data.entries) +
191 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
192 return VIRTIO_NET_ERR;
194 if (mac_data.entries) {
195 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
196 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
197 elem->out_sg[2].iov_base + sizeof(mac_data),
198 mac_data.entries * ETH_ALEN);
199 n->mac_table.in_use += mac_data.entries;
200 } else
201 n->allmulti = 1;
204 return VIRTIO_NET_OK;
207 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
208 VirtQueueElement *elem)
210 uint16_t vid;
212 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
213 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
214 return VIRTIO_NET_ERR;
217 vid = lduw_le_p(elem->out_sg[1].iov_base);
219 if (vid >= MAX_VLAN)
220 return VIRTIO_NET_ERR;
222 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
223 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
224 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
225 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
226 else
227 return VIRTIO_NET_ERR;
229 return VIRTIO_NET_OK;
232 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
234 VirtIONet *n = to_virtio_net(vdev);
235 struct virtio_net_ctrl_hdr ctrl;
236 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
237 VirtQueueElement elem;
239 while (virtqueue_pop(vq, &elem)) {
240 if ((elem.in_num < 1) || (elem.out_num < 1)) {
241 fprintf(stderr, "virtio-net ctrl missing headers\n");
242 exit(1);
245 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
246 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
247 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
248 exit(1);
251 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
252 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
254 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
255 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
256 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
257 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
258 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
259 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
261 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
263 virtqueue_push(vq, &elem, sizeof(status));
264 virtio_notify(vdev, vq);
268 /* RX */
270 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
272 VirtIONet *n = to_virtio_net(vdev);
274 qemu_flush_queued_packets(n->vc);
277 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
279 if (!virtio_queue_ready(n->rx_vq) ||
280 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
281 return 0;
283 if (virtio_queue_empty(n->rx_vq) ||
284 (n->mergeable_rx_bufs &&
285 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
286 virtio_queue_set_notification(n->rx_vq, 1);
287 return 0;
290 virtio_queue_set_notification(n->rx_vq, 0);
291 return 1;
294 static int virtio_net_can_receive(VLANClientState *vc)
296 VirtIONet *n = vc->opaque;
298 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
301 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
303 int offset, i;
305 offset = i = 0;
306 while (offset < count && i < iovcnt) {
307 int len = MIN(iov[i].iov_len, count - offset);
308 memcpy(iov[i].iov_base, buf + offset, len);
309 offset += len;
310 i++;
313 return offset;
316 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
317 const void *buf, size_t size, size_t hdr_len)
319 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
320 int offset = 0;
322 hdr->flags = 0;
323 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
325 /* We only ever receive a struct virtio_net_hdr from the tapfd,
326 * but we may be passing along a larger header to the guest.
328 iov[0].iov_base += hdr_len;
329 iov[0].iov_len -= hdr_len;
331 return offset;
334 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
336 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
337 static const uint8_t vlan[] = {0x81, 0x00};
338 uint8_t *ptr = (uint8_t *)buf;
339 int i;
341 if (n->promisc)
342 return 1;
344 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
345 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
346 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
347 return 0;
350 if (ptr[0] & 1) { // multicast
351 if (!memcmp(ptr, bcast, sizeof(bcast))) {
352 return 1;
353 } else if (n->allmulti) {
354 return 1;
356 } else { // unicast
357 if (!memcmp(ptr, n->mac, ETH_ALEN)) {
358 return 1;
362 for (i = 0; i < n->mac_table.in_use; i++) {
363 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
364 return 1;
367 return 0;
370 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
372 VirtIONet *n = vc->opaque;
373 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
374 size_t hdr_len, offset, i;
376 if (!do_virtio_net_can_receive(n, size))
377 return 0;
379 if (!receive_filter(n, buf, size))
380 return size;
382 /* hdr_len refers to the header we supply to the guest */
383 hdr_len = n->mergeable_rx_bufs ?
384 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
386 offset = i = 0;
388 while (offset < size) {
389 VirtQueueElement elem;
390 int len, total;
391 struct iovec sg[VIRTQUEUE_MAX_SIZE];
393 len = total = 0;
395 if ((i != 0 && !n->mergeable_rx_bufs) ||
396 virtqueue_pop(n->rx_vq, &elem) == 0) {
397 if (i == 0)
398 return -1;
399 fprintf(stderr, "virtio-net truncating packet\n");
400 exit(1);
403 if (elem.in_num < 1) {
404 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
405 exit(1);
408 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
409 fprintf(stderr, "virtio-net header not in first element\n");
410 exit(1);
413 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
415 if (i == 0) {
416 if (n->mergeable_rx_bufs)
417 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
419 offset += receive_header(n, sg, elem.in_num,
420 buf + offset, size - offset, hdr_len);
421 total += hdr_len;
424 /* copy in packet. ugh */
425 len = iov_fill(sg, elem.in_num,
426 buf + offset, size - offset);
427 total += len;
429 /* signal other side */
430 virtqueue_fill(n->rx_vq, &elem, total, i++);
432 offset += len;
435 if (mhdr)
436 mhdr->num_buffers = i;
438 virtqueue_flush(n->rx_vq, i);
439 virtio_notify(&n->vdev, n->rx_vq);
441 return size;
444 /* TX */
445 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
447 VirtQueueElement elem;
448 int has_vnet_hdr = 0;
450 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
451 return;
453 while (virtqueue_pop(vq, &elem)) {
454 ssize_t len = 0;
455 unsigned int out_num = elem.out_num;
456 struct iovec *out_sg = &elem.out_sg[0];
457 unsigned hdr_len;
459 /* hdr_len refers to the header received from the guest */
460 hdr_len = n->mergeable_rx_bufs ?
461 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
462 sizeof(struct virtio_net_hdr);
464 if (out_num < 1 || out_sg->iov_len != hdr_len) {
465 fprintf(stderr, "virtio-net header not in first element\n");
466 exit(1);
469 /* ignore the header if GSO is not supported */
470 if (!has_vnet_hdr) {
471 out_num--;
472 out_sg++;
473 len += hdr_len;
474 } else if (n->mergeable_rx_bufs) {
475 /* tapfd expects a struct virtio_net_hdr */
476 hdr_len -= sizeof(struct virtio_net_hdr);
477 out_sg->iov_len -= hdr_len;
478 len += hdr_len;
481 len += qemu_sendv_packet(n->vc, out_sg, out_num);
483 virtqueue_push(vq, &elem, len);
484 virtio_notify(&n->vdev, vq);
488 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
490 VirtIONet *n = to_virtio_net(vdev);
492 if (n->tx_timer_active) {
493 virtio_queue_set_notification(vq, 1);
494 qemu_del_timer(n->tx_timer);
495 n->tx_timer_active = 0;
496 virtio_net_flush_tx(n, vq);
497 } else {
498 qemu_mod_timer(n->tx_timer,
499 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
500 n->tx_timer_active = 1;
501 virtio_queue_set_notification(vq, 0);
505 static void virtio_net_tx_timer(void *opaque)
507 VirtIONet *n = opaque;
509 n->tx_timer_active = 0;
511 /* Just in case the driver is not ready on more */
512 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
513 return;
515 virtio_queue_set_notification(n->tx_vq, 1);
516 virtio_net_flush_tx(n, n->tx_vq);
519 static void virtio_net_save(QEMUFile *f, void *opaque)
521 VirtIONet *n = opaque;
523 virtio_save(&n->vdev, f);
525 qemu_put_buffer(f, n->mac, ETH_ALEN);
526 qemu_put_be32(f, n->tx_timer_active);
527 qemu_put_be32(f, n->mergeable_rx_bufs);
528 qemu_put_be16(f, n->status);
529 qemu_put_byte(f, n->promisc);
530 qemu_put_byte(f, n->allmulti);
531 qemu_put_be32(f, n->mac_table.in_use);
532 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
533 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
534 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
537 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
539 VirtIONet *n = opaque;
541 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
542 return -EINVAL;
544 virtio_load(&n->vdev, f);
546 qemu_get_buffer(f, n->mac, ETH_ALEN);
547 n->tx_timer_active = qemu_get_be32(f);
548 n->mergeable_rx_bufs = qemu_get_be32(f);
550 if (version_id >= 3)
551 n->status = qemu_get_be16(f);
553 if (version_id >= 4) {
554 if (version_id < 8) {
555 n->promisc = qemu_get_be32(f);
556 n->allmulti = qemu_get_be32(f);
557 } else {
558 n->promisc = qemu_get_byte(f);
559 n->allmulti = qemu_get_byte(f);
563 if (version_id >= 5) {
564 n->mac_table.in_use = qemu_get_be32(f);
565 /* MAC_TABLE_ENTRIES may be different from the saved image */
566 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
567 qemu_get_buffer(f, n->mac_table.macs,
568 n->mac_table.in_use * ETH_ALEN);
569 } else if (n->mac_table.in_use) {
570 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
571 n->promisc = 1;
572 n->mac_table.in_use = 0;
576 if (version_id >= 6)
577 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
579 if (version_id >= 7 && qemu_get_be32(f)) {
580 fprintf(stderr,
581 "virtio-net: saved image requires vnet header support\n");
582 exit(1);
585 if (n->tx_timer_active) {
586 qemu_mod_timer(n->tx_timer,
587 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
590 return 0;
593 static void virtio_net_cleanup(VLANClientState *vc)
595 VirtIONet *n = vc->opaque;
597 unregister_savevm("virtio-net", n);
599 qemu_free(n->mac_table.macs);
600 qemu_free(n->vlans);
602 qemu_del_timer(n->tx_timer);
603 qemu_free_timer(n->tx_timer);
605 virtio_cleanup(&n->vdev);
608 VirtIODevice *virtio_net_init(DeviceState *dev)
610 VirtIONet *n;
611 static int virtio_net_id;
613 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
614 sizeof(struct virtio_net_config),
615 sizeof(VirtIONet));
617 n->vdev.get_config = virtio_net_get_config;
618 n->vdev.set_config = virtio_net_set_config;
619 n->vdev.get_features = virtio_net_get_features;
620 n->vdev.set_features = virtio_net_set_features;
621 n->vdev.bad_features = virtio_net_bad_features;
622 n->vdev.reset = virtio_net_reset;
623 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
624 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
625 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
626 qdev_get_macaddr(dev, n->mac);
627 n->status = VIRTIO_NET_S_LINK_UP;
628 n->vc = qdev_get_vlan_client(dev,
629 virtio_net_can_receive,
630 virtio_net_receive, NULL,
631 virtio_net_cleanup, n);
632 n->vc->link_status_changed = virtio_net_set_link_status;
634 qemu_format_nic_info_str(n->vc, n->mac);
636 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
637 n->tx_timer_active = 0;
638 n->mergeable_rx_bufs = 0;
639 n->promisc = 1; /* for compatibility */
641 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
643 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
645 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
646 virtio_net_save, virtio_net_load, n);
648 return &n->vdev;