Enable UFO on virtio-net and tap devices
[qemu/ar7.git] / hw / virtio-net.c
blobbfe1366c5e261065eccd775892d3e89378aca345
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 10
21 #define MAC_TABLE_ENTRIES 64
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 uint32_t has_vnet_hdr;
36 struct {
37 VirtQueueElement elem;
38 ssize_t len;
39 } async_tx;
40 int mergeable_rx_bufs;
41 uint8_t promisc;
42 uint8_t allmulti;
43 uint8_t alluni;
44 uint8_t nomulti;
45 uint8_t nouni;
46 uint8_t nobcast;
47 struct {
48 int in_use;
49 int first_multi;
50 uint8_t multi_overflow;
51 uint8_t uni_overflow;
52 uint8_t *macs;
53 } mac_table;
54 uint32_t *vlans;
55 } VirtIONet;
57 /* TODO
58 * - we could suppress RX interrupt if we were so inclined.
61 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
63 return (VirtIONet *)vdev;
66 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
68 VirtIONet *n = to_virtio_net(vdev);
69 struct virtio_net_config netcfg;
71 netcfg.status = n->status;
72 memcpy(netcfg.mac, n->mac, ETH_ALEN);
73 memcpy(config, &netcfg, sizeof(netcfg));
76 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
78 VirtIONet *n = to_virtio_net(vdev);
79 struct virtio_net_config netcfg;
81 memcpy(&netcfg, config, sizeof(netcfg));
83 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
84 memcpy(n->mac, netcfg.mac, ETH_ALEN);
85 qemu_format_nic_info_str(n->vc, n->mac);
89 static void virtio_net_set_link_status(VLANClientState *vc)
91 VirtIONet *n = vc->opaque;
92 uint16_t old_status = n->status;
94 if (vc->link_down)
95 n->status &= ~VIRTIO_NET_S_LINK_UP;
96 else
97 n->status |= VIRTIO_NET_S_LINK_UP;
99 if (n->status != old_status)
100 virtio_notify_config(&n->vdev);
103 static void virtio_net_reset(VirtIODevice *vdev)
105 VirtIONet *n = to_virtio_net(vdev);
107 /* Reset back to compatibility mode */
108 n->promisc = 1;
109 n->allmulti = 0;
110 n->alluni = 0;
111 n->nomulti = 0;
112 n->nouni = 0;
113 n->nobcast = 0;
115 /* Flush any MAC and VLAN filter table state */
116 n->mac_table.in_use = 0;
117 n->mac_table.first_multi = 0;
118 n->mac_table.multi_overflow = 0;
119 n->mac_table.uni_overflow = 0;
120 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
121 memset(n->vlans, 0, MAX_VLAN >> 3);
124 static int peer_has_vnet_hdr(VirtIONet *n)
126 if (!n->vc->peer)
127 return 0;
129 if (n->vc->peer->type != NET_CLIENT_TYPE_TAP)
130 return 0;
132 n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer);
134 return n->has_vnet_hdr;
137 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
139 VirtIONet *n = to_virtio_net(vdev);
140 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
141 (1 << VIRTIO_NET_F_MRG_RXBUF) |
142 (1 << VIRTIO_NET_F_STATUS) |
143 (1 << VIRTIO_NET_F_CTRL_VQ) |
144 (1 << VIRTIO_NET_F_CTRL_RX) |
145 (1 << VIRTIO_NET_F_CTRL_VLAN) |
146 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
148 if (peer_has_vnet_hdr(n)) {
149 tap_using_vnet_hdr(n->vc->peer, 1);
151 features |= (1 << VIRTIO_NET_F_CSUM);
152 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
153 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
154 features |= (1 << VIRTIO_NET_F_HOST_ECN);
156 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
157 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
158 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
159 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
161 if (tap_has_ufo(n->vc->peer)) {
162 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
163 features |= (1 << VIRTIO_NET_F_HOST_UFO);
167 return features;
170 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
172 uint32_t features = 0;
174 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
175 * but also these: */
176 features |= (1 << VIRTIO_NET_F_MAC);
177 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
178 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
179 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
180 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
182 return features & virtio_net_get_features(vdev);
185 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
187 VirtIONet *n = to_virtio_net(vdev);
189 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
191 if (n->has_vnet_hdr) {
192 tap_set_offload(n->vc->peer,
193 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
194 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
195 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
196 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
197 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
201 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
202 VirtQueueElement *elem)
204 uint8_t on;
206 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
207 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
208 exit(1);
211 on = ldub_p(elem->out_sg[1].iov_base);
213 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
214 n->promisc = on;
215 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
216 n->allmulti = on;
217 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
218 n->alluni = on;
219 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
220 n->nomulti = on;
221 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
222 n->nouni = on;
223 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
224 n->nobcast = on;
225 else
226 return VIRTIO_NET_ERR;
228 return VIRTIO_NET_OK;
231 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
232 VirtQueueElement *elem)
234 struct virtio_net_ctrl_mac mac_data;
236 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
237 elem->out_sg[1].iov_len < sizeof(mac_data) ||
238 elem->out_sg[2].iov_len < sizeof(mac_data))
239 return VIRTIO_NET_ERR;
241 n->mac_table.in_use = 0;
242 n->mac_table.first_multi = 0;
243 n->mac_table.uni_overflow = 0;
244 n->mac_table.multi_overflow = 0;
245 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
247 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
249 if (sizeof(mac_data.entries) +
250 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
251 return VIRTIO_NET_ERR;
253 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
254 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
255 mac_data.entries * ETH_ALEN);
256 n->mac_table.in_use += mac_data.entries;
257 } else {
258 n->mac_table.uni_overflow = 1;
261 n->mac_table.first_multi = n->mac_table.in_use;
263 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
265 if (sizeof(mac_data.entries) +
266 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
267 return VIRTIO_NET_ERR;
269 if (mac_data.entries) {
270 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
271 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
272 elem->out_sg[2].iov_base + sizeof(mac_data),
273 mac_data.entries * ETH_ALEN);
274 n->mac_table.in_use += mac_data.entries;
275 } else {
276 n->mac_table.multi_overflow = 1;
280 return VIRTIO_NET_OK;
283 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
284 VirtQueueElement *elem)
286 uint16_t vid;
288 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
289 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
290 return VIRTIO_NET_ERR;
293 vid = lduw_le_p(elem->out_sg[1].iov_base);
295 if (vid >= MAX_VLAN)
296 return VIRTIO_NET_ERR;
298 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
299 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
300 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
301 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
302 else
303 return VIRTIO_NET_ERR;
305 return VIRTIO_NET_OK;
308 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
310 VirtIONet *n = to_virtio_net(vdev);
311 struct virtio_net_ctrl_hdr ctrl;
312 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
313 VirtQueueElement elem;
315 while (virtqueue_pop(vq, &elem)) {
316 if ((elem.in_num < 1) || (elem.out_num < 1)) {
317 fprintf(stderr, "virtio-net ctrl missing headers\n");
318 exit(1);
321 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
322 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
323 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
324 exit(1);
327 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
328 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
330 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
331 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
332 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
333 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
334 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
335 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
337 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
339 virtqueue_push(vq, &elem, sizeof(status));
340 virtio_notify(vdev, vq);
344 /* RX */
346 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
348 VirtIONet *n = to_virtio_net(vdev);
350 qemu_flush_queued_packets(n->vc);
352 /* We now have RX buffers, signal to the IO thread to break out of the
353 * select to re-poll the tap file descriptor */
354 qemu_notify_event();
357 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
359 if (!virtio_queue_ready(n->rx_vq) ||
360 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
361 return 0;
363 if (virtio_queue_empty(n->rx_vq) ||
364 (n->mergeable_rx_bufs &&
365 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
366 virtio_queue_set_notification(n->rx_vq, 1);
367 return 0;
370 virtio_queue_set_notification(n->rx_vq, 0);
371 return 1;
374 static int virtio_net_can_receive(VLANClientState *vc)
376 VirtIONet *n = vc->opaque;
378 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
381 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
382 * it never finds out that the packets don't have valid checksums. This
383 * causes dhclient to get upset. Fedora's carried a patch for ages to
384 * fix this with Xen but it hasn't appeared in an upstream release of
385 * dhclient yet.
387 * To avoid breaking existing guests, we catch udp packets and add
388 * checksums. This is terrible but it's better than hacking the guest
389 * kernels.
391 * N.B. if we introduce a zero-copy API, this operation is no longer free so
392 * we should provide a mechanism to disable it to avoid polluting the host
393 * cache.
395 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
396 const uint8_t *buf, size_t size)
398 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
399 (size > 27 && size < 1500) && /* normal sized MTU */
400 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
401 (buf[23] == 17) && /* ip.protocol == UDP */
402 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
403 /* FIXME this cast is evil */
404 net_checksum_calculate((uint8_t *)buf, size);
405 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
409 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
411 int offset, i;
413 offset = i = 0;
414 while (offset < count && i < iovcnt) {
415 int len = MIN(iov[i].iov_len, count - offset);
416 memcpy(iov[i].iov_base, buf + offset, len);
417 offset += len;
418 i++;
421 return offset;
424 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
425 const void *buf, size_t size, size_t hdr_len)
427 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
428 int offset = 0;
430 hdr->flags = 0;
431 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
433 if (n->has_vnet_hdr) {
434 memcpy(hdr, buf, sizeof(*hdr));
435 offset = sizeof(*hdr);
436 work_around_broken_dhclient(hdr, buf + offset, size - offset);
439 /* We only ever receive a struct virtio_net_hdr from the tapfd,
440 * but we may be passing along a larger header to the guest.
442 iov[0].iov_base += hdr_len;
443 iov[0].iov_len -= hdr_len;
445 return offset;
448 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
450 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
451 static const uint8_t vlan[] = {0x81, 0x00};
452 uint8_t *ptr = (uint8_t *)buf;
453 int i;
455 if (n->promisc)
456 return 1;
458 if (n->has_vnet_hdr) {
459 ptr += sizeof(struct virtio_net_hdr);
462 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
463 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
464 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
465 return 0;
468 if (ptr[0] & 1) { // multicast
469 if (!memcmp(ptr, bcast, sizeof(bcast))) {
470 return !n->nobcast;
471 } else if (n->nomulti) {
472 return 0;
473 } else if (n->allmulti || n->mac_table.multi_overflow) {
474 return 1;
477 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
478 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
479 return 1;
482 } else { // unicast
483 if (n->nouni) {
484 return 0;
485 } else if (n->alluni || n->mac_table.uni_overflow) {
486 return 1;
487 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
488 return 1;
491 for (i = 0; i < n->mac_table.first_multi; i++) {
492 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
493 return 1;
498 return 0;
501 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
503 VirtIONet *n = vc->opaque;
504 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
505 size_t hdr_len, offset, i;
507 if (!do_virtio_net_can_receive(n, size))
508 return 0;
510 if (!receive_filter(n, buf, size))
511 return size;
513 /* hdr_len refers to the header we supply to the guest */
514 hdr_len = n->mergeable_rx_bufs ?
515 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
517 offset = i = 0;
519 while (offset < size) {
520 VirtQueueElement elem;
521 int len, total;
522 struct iovec sg[VIRTQUEUE_MAX_SIZE];
524 len = total = 0;
526 if ((i != 0 && !n->mergeable_rx_bufs) ||
527 virtqueue_pop(n->rx_vq, &elem) == 0) {
528 if (i == 0)
529 return -1;
530 fprintf(stderr, "virtio-net truncating packet\n");
531 exit(1);
534 if (elem.in_num < 1) {
535 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
536 exit(1);
539 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
540 fprintf(stderr, "virtio-net header not in first element\n");
541 exit(1);
544 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
546 if (i == 0) {
547 if (n->mergeable_rx_bufs)
548 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
550 offset += receive_header(n, sg, elem.in_num,
551 buf + offset, size - offset, hdr_len);
552 total += hdr_len;
555 /* copy in packet. ugh */
556 len = iov_fill(sg, elem.in_num,
557 buf + offset, size - offset);
558 total += len;
560 /* signal other side */
561 virtqueue_fill(n->rx_vq, &elem, total, i++);
563 offset += len;
566 if (mhdr)
567 mhdr->num_buffers = i;
569 virtqueue_flush(n->rx_vq, i);
570 virtio_notify(&n->vdev, n->rx_vq);
572 return size;
575 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
577 static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
579 VirtIONet *n = vc->opaque;
581 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
582 virtio_notify(&n->vdev, n->tx_vq);
584 n->async_tx.elem.out_num = n->async_tx.len = 0;
586 virtio_queue_set_notification(n->tx_vq, 1);
587 virtio_net_flush_tx(n, n->tx_vq);
590 /* TX */
591 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
593 VirtQueueElement elem;
595 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
596 return;
598 if (n->async_tx.elem.out_num) {
599 virtio_queue_set_notification(n->tx_vq, 0);
600 return;
603 while (virtqueue_pop(vq, &elem)) {
604 ssize_t ret, len = 0;
605 unsigned int out_num = elem.out_num;
606 struct iovec *out_sg = &elem.out_sg[0];
607 unsigned hdr_len;
609 /* hdr_len refers to the header received from the guest */
610 hdr_len = n->mergeable_rx_bufs ?
611 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
612 sizeof(struct virtio_net_hdr);
614 if (out_num < 1 || out_sg->iov_len != hdr_len) {
615 fprintf(stderr, "virtio-net header not in first element\n");
616 exit(1);
619 /* ignore the header if GSO is not supported */
620 if (!n->has_vnet_hdr) {
621 out_num--;
622 out_sg++;
623 len += hdr_len;
624 } else if (n->mergeable_rx_bufs) {
625 /* tapfd expects a struct virtio_net_hdr */
626 hdr_len -= sizeof(struct virtio_net_hdr);
627 out_sg->iov_len -= hdr_len;
628 len += hdr_len;
631 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
632 virtio_net_tx_complete);
633 if (ret == 0) {
634 virtio_queue_set_notification(n->tx_vq, 0);
635 n->async_tx.elem = elem;
636 n->async_tx.len = len;
637 return;
640 len += ret;
642 virtqueue_push(vq, &elem, len);
643 virtio_notify(&n->vdev, vq);
647 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
649 VirtIONet *n = to_virtio_net(vdev);
651 if (n->tx_timer_active) {
652 virtio_queue_set_notification(vq, 1);
653 qemu_del_timer(n->tx_timer);
654 n->tx_timer_active = 0;
655 virtio_net_flush_tx(n, vq);
656 } else {
657 qemu_mod_timer(n->tx_timer,
658 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
659 n->tx_timer_active = 1;
660 virtio_queue_set_notification(vq, 0);
664 static void virtio_net_tx_timer(void *opaque)
666 VirtIONet *n = opaque;
668 n->tx_timer_active = 0;
670 /* Just in case the driver is not ready on more */
671 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
672 return;
674 virtio_queue_set_notification(n->tx_vq, 1);
675 virtio_net_flush_tx(n, n->tx_vq);
678 static void virtio_net_save(QEMUFile *f, void *opaque)
680 VirtIONet *n = opaque;
682 virtio_save(&n->vdev, f);
684 qemu_put_buffer(f, n->mac, ETH_ALEN);
685 qemu_put_be32(f, n->tx_timer_active);
686 qemu_put_be32(f, n->mergeable_rx_bufs);
687 qemu_put_be16(f, n->status);
688 qemu_put_byte(f, n->promisc);
689 qemu_put_byte(f, n->allmulti);
690 qemu_put_be32(f, n->mac_table.in_use);
691 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
692 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
693 qemu_put_be32(f, n->has_vnet_hdr);
694 qemu_put_byte(f, n->mac_table.multi_overflow);
695 qemu_put_byte(f, n->mac_table.uni_overflow);
696 qemu_put_byte(f, n->alluni);
697 qemu_put_byte(f, n->nomulti);
698 qemu_put_byte(f, n->nouni);
699 qemu_put_byte(f, n->nobcast);
702 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
704 VirtIONet *n = opaque;
705 int i;
707 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
708 return -EINVAL;
710 virtio_load(&n->vdev, f);
712 qemu_get_buffer(f, n->mac, ETH_ALEN);
713 n->tx_timer_active = qemu_get_be32(f);
714 n->mergeable_rx_bufs = qemu_get_be32(f);
716 if (version_id >= 3)
717 n->status = qemu_get_be16(f);
719 if (version_id >= 4) {
720 if (version_id < 8) {
721 n->promisc = qemu_get_be32(f);
722 n->allmulti = qemu_get_be32(f);
723 } else {
724 n->promisc = qemu_get_byte(f);
725 n->allmulti = qemu_get_byte(f);
729 if (version_id >= 5) {
730 n->mac_table.in_use = qemu_get_be32(f);
731 /* MAC_TABLE_ENTRIES may be different from the saved image */
732 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
733 qemu_get_buffer(f, n->mac_table.macs,
734 n->mac_table.in_use * ETH_ALEN);
735 } else if (n->mac_table.in_use) {
736 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
737 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
738 n->mac_table.in_use = 0;
742 if (version_id >= 6)
743 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
745 if (version_id >= 7) {
746 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
747 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
748 return -1;
751 if (n->has_vnet_hdr) {
752 tap_using_vnet_hdr(n->vc->peer, 1);
753 tap_set_offload(n->vc->peer,
754 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
755 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
756 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
757 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
758 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
762 if (version_id >= 9) {
763 n->mac_table.multi_overflow = qemu_get_byte(f);
764 n->mac_table.uni_overflow = qemu_get_byte(f);
767 if (version_id >= 10) {
768 n->alluni = qemu_get_byte(f);
769 n->nomulti = qemu_get_byte(f);
770 n->nouni = qemu_get_byte(f);
771 n->nobcast = qemu_get_byte(f);
774 /* Find the first multicast entry in the saved MAC filter */
775 for (i = 0; i < n->mac_table.in_use; i++) {
776 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
777 break;
780 n->mac_table.first_multi = i;
782 if (n->tx_timer_active) {
783 qemu_mod_timer(n->tx_timer,
784 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
787 return 0;
790 static void virtio_net_cleanup(VLANClientState *vc)
792 VirtIONet *n = vc->opaque;
794 n->vc = NULL;
797 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
799 VirtIONet *n;
800 static int virtio_net_id;
802 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
803 sizeof(struct virtio_net_config),
804 sizeof(VirtIONet));
806 n->vdev.get_config = virtio_net_get_config;
807 n->vdev.set_config = virtio_net_set_config;
808 n->vdev.get_features = virtio_net_get_features;
809 n->vdev.set_features = virtio_net_set_features;
810 n->vdev.bad_features = virtio_net_bad_features;
811 n->vdev.reset = virtio_net_reset;
812 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
813 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
814 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
815 qemu_macaddr_default_if_unset(&conf->macaddr);
816 n->status = VIRTIO_NET_S_LINK_UP;
817 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
818 dev->info->name, dev->id,
819 virtio_net_can_receive,
820 virtio_net_receive, NULL, NULL,
821 virtio_net_cleanup, n);
822 n->vc->link_status_changed = virtio_net_set_link_status;
824 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
826 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
827 n->tx_timer_active = 0;
828 n->mergeable_rx_bufs = 0;
829 n->promisc = 1; /* for compatibility */
831 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
833 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
835 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
836 virtio_net_save, virtio_net_load, n);
838 return &n->vdev;
841 void virtio_net_exit(VirtIODevice *vdev)
843 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
845 qemu_purge_queued_packets(n->vc);
847 unregister_savevm("virtio-net", n);
849 qemu_free(n->mac_table.macs);
850 qemu_free(n->vlans);
852 qemu_del_timer(n->tx_timer);
853 qemu_free_timer(n->tx_timer);
855 virtio_cleanup(&n->vdev);
856 qemu_del_vlan_client(n->vc);