net: move net-checksum.c under net/
[qemu/ar7.git] / hw / virtio-net.c
blobac919e1e8aa0d88668edd4af8e4ecb48f822c5b2
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 "net/checksum.h"
17 #include "qemu-timer.h"
18 #include "virtio-net.h"
20 #define VIRTIO_NET_VM_VERSION 11
22 #define MAC_TABLE_ENTRIES 64
23 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
25 typedef struct VirtIONet
27 VirtIODevice vdev;
28 uint8_t mac[ETH_ALEN];
29 uint16_t status;
30 VirtQueue *rx_vq;
31 VirtQueue *tx_vq;
32 VirtQueue *ctrl_vq;
33 VLANClientState *vc;
34 QEMUTimer *tx_timer;
35 int tx_timer_active;
36 uint32_t has_vnet_hdr;
37 uint8_t has_ufo;
38 struct {
39 VirtQueueElement elem;
40 ssize_t len;
41 } async_tx;
42 int mergeable_rx_bufs;
43 uint8_t promisc;
44 uint8_t allmulti;
45 uint8_t alluni;
46 uint8_t nomulti;
47 uint8_t nouni;
48 uint8_t nobcast;
49 struct {
50 int in_use;
51 int first_multi;
52 uint8_t multi_overflow;
53 uint8_t uni_overflow;
54 uint8_t *macs;
55 } mac_table;
56 uint32_t *vlans;
57 } VirtIONet;
59 /* TODO
60 * - we could suppress RX interrupt if we were so inclined.
63 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
65 return (VirtIONet *)vdev;
68 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
70 VirtIONet *n = to_virtio_net(vdev);
71 struct virtio_net_config netcfg;
73 netcfg.status = n->status;
74 memcpy(netcfg.mac, n->mac, ETH_ALEN);
75 memcpy(config, &netcfg, sizeof(netcfg));
78 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
80 VirtIONet *n = to_virtio_net(vdev);
81 struct virtio_net_config netcfg;
83 memcpy(&netcfg, config, sizeof(netcfg));
85 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
86 memcpy(n->mac, netcfg.mac, ETH_ALEN);
87 qemu_format_nic_info_str(n->vc, n->mac);
91 static void virtio_net_set_link_status(VLANClientState *vc)
93 VirtIONet *n = vc->opaque;
94 uint16_t old_status = n->status;
96 if (vc->link_down)
97 n->status &= ~VIRTIO_NET_S_LINK_UP;
98 else
99 n->status |= VIRTIO_NET_S_LINK_UP;
101 if (n->status != old_status)
102 virtio_notify_config(&n->vdev);
105 static void virtio_net_reset(VirtIODevice *vdev)
107 VirtIONet *n = to_virtio_net(vdev);
109 /* Reset back to compatibility mode */
110 n->promisc = 1;
111 n->allmulti = 0;
112 n->alluni = 0;
113 n->nomulti = 0;
114 n->nouni = 0;
115 n->nobcast = 0;
117 /* Flush any MAC and VLAN filter table state */
118 n->mac_table.in_use = 0;
119 n->mac_table.first_multi = 0;
120 n->mac_table.multi_overflow = 0;
121 n->mac_table.uni_overflow = 0;
122 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
123 memset(n->vlans, 0, MAX_VLAN >> 3);
126 static int peer_has_vnet_hdr(VirtIONet *n)
128 if (!n->vc->peer)
129 return 0;
131 if (n->vc->peer->type != NET_CLIENT_TYPE_TAP)
132 return 0;
134 n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer);
136 return n->has_vnet_hdr;
139 static int peer_has_ufo(VirtIONet *n)
141 if (!peer_has_vnet_hdr(n))
142 return 0;
144 n->has_ufo = tap_has_ufo(n->vc->peer);
146 return n->has_ufo;
149 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
151 VirtIONet *n = to_virtio_net(vdev);
152 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
153 (1 << VIRTIO_NET_F_MRG_RXBUF) |
154 (1 << VIRTIO_NET_F_STATUS) |
155 (1 << VIRTIO_NET_F_CTRL_VQ) |
156 (1 << VIRTIO_NET_F_CTRL_RX) |
157 (1 << VIRTIO_NET_F_CTRL_VLAN) |
158 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
160 if (peer_has_vnet_hdr(n)) {
161 tap_using_vnet_hdr(n->vc->peer, 1);
163 features |= (1 << VIRTIO_NET_F_CSUM);
164 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
165 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
166 features |= (1 << VIRTIO_NET_F_HOST_ECN);
168 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
169 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
170 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
171 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
173 if (peer_has_ufo(n)) {
174 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
175 features |= (1 << VIRTIO_NET_F_HOST_UFO);
179 return features;
182 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
184 uint32_t features = 0;
186 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
187 * but also these: */
188 features |= (1 << VIRTIO_NET_F_MAC);
189 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
190 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
191 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
192 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
194 return features & virtio_net_get_features(vdev);
197 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
199 VirtIONet *n = to_virtio_net(vdev);
201 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
203 if (n->has_vnet_hdr) {
204 tap_set_offload(n->vc->peer,
205 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
206 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
207 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
208 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
209 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
213 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
214 VirtQueueElement *elem)
216 uint8_t on;
218 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
219 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
220 exit(1);
223 on = ldub_p(elem->out_sg[1].iov_base);
225 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
226 n->promisc = on;
227 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
228 n->allmulti = on;
229 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
230 n->alluni = on;
231 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
232 n->nomulti = on;
233 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
234 n->nouni = on;
235 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
236 n->nobcast = on;
237 else
238 return VIRTIO_NET_ERR;
240 return VIRTIO_NET_OK;
243 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
244 VirtQueueElement *elem)
246 struct virtio_net_ctrl_mac mac_data;
248 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
249 elem->out_sg[1].iov_len < sizeof(mac_data) ||
250 elem->out_sg[2].iov_len < sizeof(mac_data))
251 return VIRTIO_NET_ERR;
253 n->mac_table.in_use = 0;
254 n->mac_table.first_multi = 0;
255 n->mac_table.uni_overflow = 0;
256 n->mac_table.multi_overflow = 0;
257 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
259 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
261 if (sizeof(mac_data.entries) +
262 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
263 return VIRTIO_NET_ERR;
265 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
266 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
267 mac_data.entries * ETH_ALEN);
268 n->mac_table.in_use += mac_data.entries;
269 } else {
270 n->mac_table.uni_overflow = 1;
273 n->mac_table.first_multi = n->mac_table.in_use;
275 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
277 if (sizeof(mac_data.entries) +
278 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
279 return VIRTIO_NET_ERR;
281 if (mac_data.entries) {
282 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
283 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
284 elem->out_sg[2].iov_base + sizeof(mac_data),
285 mac_data.entries * ETH_ALEN);
286 n->mac_table.in_use += mac_data.entries;
287 } else {
288 n->mac_table.multi_overflow = 1;
292 return VIRTIO_NET_OK;
295 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
296 VirtQueueElement *elem)
298 uint16_t vid;
300 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
301 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
302 return VIRTIO_NET_ERR;
305 vid = lduw_le_p(elem->out_sg[1].iov_base);
307 if (vid >= MAX_VLAN)
308 return VIRTIO_NET_ERR;
310 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
311 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
312 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
313 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
314 else
315 return VIRTIO_NET_ERR;
317 return VIRTIO_NET_OK;
320 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
322 VirtIONet *n = to_virtio_net(vdev);
323 struct virtio_net_ctrl_hdr ctrl;
324 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
325 VirtQueueElement elem;
327 while (virtqueue_pop(vq, &elem)) {
328 if ((elem.in_num < 1) || (elem.out_num < 1)) {
329 fprintf(stderr, "virtio-net ctrl missing headers\n");
330 exit(1);
333 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
334 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
335 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
336 exit(1);
339 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
340 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
342 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
343 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
344 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
345 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
346 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
347 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
349 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
351 virtqueue_push(vq, &elem, sizeof(status));
352 virtio_notify(vdev, vq);
356 /* RX */
358 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
360 VirtIONet *n = to_virtio_net(vdev);
362 qemu_flush_queued_packets(n->vc);
364 /* We now have RX buffers, signal to the IO thread to break out of the
365 * select to re-poll the tap file descriptor */
366 qemu_notify_event();
369 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
371 if (!virtio_queue_ready(n->rx_vq) ||
372 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
373 return 0;
375 if (virtio_queue_empty(n->rx_vq) ||
376 (n->mergeable_rx_bufs &&
377 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
378 virtio_queue_set_notification(n->rx_vq, 1);
379 return 0;
382 virtio_queue_set_notification(n->rx_vq, 0);
383 return 1;
386 static int virtio_net_can_receive(VLANClientState *vc)
388 VirtIONet *n = vc->opaque;
390 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
393 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
394 * it never finds out that the packets don't have valid checksums. This
395 * causes dhclient to get upset. Fedora's carried a patch for ages to
396 * fix this with Xen but it hasn't appeared in an upstream release of
397 * dhclient yet.
399 * To avoid breaking existing guests, we catch udp packets and add
400 * checksums. This is terrible but it's better than hacking the guest
401 * kernels.
403 * N.B. if we introduce a zero-copy API, this operation is no longer free so
404 * we should provide a mechanism to disable it to avoid polluting the host
405 * cache.
407 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
408 const uint8_t *buf, size_t size)
410 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
411 (size > 27 && size < 1500) && /* normal sized MTU */
412 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
413 (buf[23] == 17) && /* ip.protocol == UDP */
414 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
415 /* FIXME this cast is evil */
416 net_checksum_calculate((uint8_t *)buf, size);
417 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
421 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
423 int offset, i;
425 offset = i = 0;
426 while (offset < count && i < iovcnt) {
427 int len = MIN(iov[i].iov_len, count - offset);
428 memcpy(iov[i].iov_base, buf + offset, len);
429 offset += len;
430 i++;
433 return offset;
436 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
437 const void *buf, size_t size, size_t hdr_len)
439 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
440 int offset = 0;
442 hdr->flags = 0;
443 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
445 if (n->has_vnet_hdr) {
446 memcpy(hdr, buf, sizeof(*hdr));
447 offset = sizeof(*hdr);
448 work_around_broken_dhclient(hdr, buf + offset, size - offset);
451 /* We only ever receive a struct virtio_net_hdr from the tapfd,
452 * but we may be passing along a larger header to the guest.
454 iov[0].iov_base += hdr_len;
455 iov[0].iov_len -= hdr_len;
457 return offset;
460 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
462 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
463 static const uint8_t vlan[] = {0x81, 0x00};
464 uint8_t *ptr = (uint8_t *)buf;
465 int i;
467 if (n->promisc)
468 return 1;
470 if (n->has_vnet_hdr) {
471 ptr += sizeof(struct virtio_net_hdr);
474 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
475 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
476 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
477 return 0;
480 if (ptr[0] & 1) { // multicast
481 if (!memcmp(ptr, bcast, sizeof(bcast))) {
482 return !n->nobcast;
483 } else if (n->nomulti) {
484 return 0;
485 } else if (n->allmulti || n->mac_table.multi_overflow) {
486 return 1;
489 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
490 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
491 return 1;
494 } else { // unicast
495 if (n->nouni) {
496 return 0;
497 } else if (n->alluni || n->mac_table.uni_overflow) {
498 return 1;
499 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
500 return 1;
503 for (i = 0; i < n->mac_table.first_multi; i++) {
504 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
505 return 1;
510 return 0;
513 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
515 VirtIONet *n = vc->opaque;
516 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
517 size_t hdr_len, offset, i;
519 if (!do_virtio_net_can_receive(n, size))
520 return 0;
522 if (!receive_filter(n, buf, size))
523 return size;
525 /* hdr_len refers to the header we supply to the guest */
526 hdr_len = n->mergeable_rx_bufs ?
527 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
529 offset = i = 0;
531 while (offset < size) {
532 VirtQueueElement elem;
533 int len, total;
534 struct iovec sg[VIRTQUEUE_MAX_SIZE];
536 len = total = 0;
538 if ((i != 0 && !n->mergeable_rx_bufs) ||
539 virtqueue_pop(n->rx_vq, &elem) == 0) {
540 if (i == 0)
541 return -1;
542 fprintf(stderr, "virtio-net truncating packet\n");
543 exit(1);
546 if (elem.in_num < 1) {
547 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
548 exit(1);
551 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
552 fprintf(stderr, "virtio-net header not in first element\n");
553 exit(1);
556 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
558 if (i == 0) {
559 if (n->mergeable_rx_bufs)
560 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
562 offset += receive_header(n, sg, elem.in_num,
563 buf + offset, size - offset, hdr_len);
564 total += hdr_len;
567 /* copy in packet. ugh */
568 len = iov_fill(sg, elem.in_num,
569 buf + offset, size - offset);
570 total += len;
572 /* signal other side */
573 virtqueue_fill(n->rx_vq, &elem, total, i++);
575 offset += len;
578 if (mhdr)
579 mhdr->num_buffers = i;
581 virtqueue_flush(n->rx_vq, i);
582 virtio_notify(&n->vdev, n->rx_vq);
584 return size;
587 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
589 static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
591 VirtIONet *n = vc->opaque;
593 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
594 virtio_notify(&n->vdev, n->tx_vq);
596 n->async_tx.elem.out_num = n->async_tx.len = 0;
598 virtio_queue_set_notification(n->tx_vq, 1);
599 virtio_net_flush_tx(n, n->tx_vq);
602 /* TX */
603 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
605 VirtQueueElement elem;
607 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
608 return;
610 if (n->async_tx.elem.out_num) {
611 virtio_queue_set_notification(n->tx_vq, 0);
612 return;
615 while (virtqueue_pop(vq, &elem)) {
616 ssize_t ret, len = 0;
617 unsigned int out_num = elem.out_num;
618 struct iovec *out_sg = &elem.out_sg[0];
619 unsigned hdr_len;
621 /* hdr_len refers to the header received from the guest */
622 hdr_len = n->mergeable_rx_bufs ?
623 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
624 sizeof(struct virtio_net_hdr);
626 if (out_num < 1 || out_sg->iov_len != hdr_len) {
627 fprintf(stderr, "virtio-net header not in first element\n");
628 exit(1);
631 /* ignore the header if GSO is not supported */
632 if (!n->has_vnet_hdr) {
633 out_num--;
634 out_sg++;
635 len += hdr_len;
636 } else if (n->mergeable_rx_bufs) {
637 /* tapfd expects a struct virtio_net_hdr */
638 hdr_len -= sizeof(struct virtio_net_hdr);
639 out_sg->iov_len -= hdr_len;
640 len += hdr_len;
643 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
644 virtio_net_tx_complete);
645 if (ret == 0) {
646 virtio_queue_set_notification(n->tx_vq, 0);
647 n->async_tx.elem = elem;
648 n->async_tx.len = len;
649 return;
652 len += ret;
654 virtqueue_push(vq, &elem, len);
655 virtio_notify(&n->vdev, vq);
659 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
661 VirtIONet *n = to_virtio_net(vdev);
663 if (n->tx_timer_active) {
664 virtio_queue_set_notification(vq, 1);
665 qemu_del_timer(n->tx_timer);
666 n->tx_timer_active = 0;
667 virtio_net_flush_tx(n, vq);
668 } else {
669 qemu_mod_timer(n->tx_timer,
670 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
671 n->tx_timer_active = 1;
672 virtio_queue_set_notification(vq, 0);
676 static void virtio_net_tx_timer(void *opaque)
678 VirtIONet *n = opaque;
680 n->tx_timer_active = 0;
682 /* Just in case the driver is not ready on more */
683 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
684 return;
686 virtio_queue_set_notification(n->tx_vq, 1);
687 virtio_net_flush_tx(n, n->tx_vq);
690 static void virtio_net_save(QEMUFile *f, void *opaque)
692 VirtIONet *n = opaque;
694 virtio_save(&n->vdev, f);
696 qemu_put_buffer(f, n->mac, ETH_ALEN);
697 qemu_put_be32(f, n->tx_timer_active);
698 qemu_put_be32(f, n->mergeable_rx_bufs);
699 qemu_put_be16(f, n->status);
700 qemu_put_byte(f, n->promisc);
701 qemu_put_byte(f, n->allmulti);
702 qemu_put_be32(f, n->mac_table.in_use);
703 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
704 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
705 qemu_put_be32(f, n->has_vnet_hdr);
706 qemu_put_byte(f, n->mac_table.multi_overflow);
707 qemu_put_byte(f, n->mac_table.uni_overflow);
708 qemu_put_byte(f, n->alluni);
709 qemu_put_byte(f, n->nomulti);
710 qemu_put_byte(f, n->nouni);
711 qemu_put_byte(f, n->nobcast);
712 qemu_put_byte(f, n->has_ufo);
715 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
717 VirtIONet *n = opaque;
718 int i;
720 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
721 return -EINVAL;
723 virtio_load(&n->vdev, f);
725 qemu_get_buffer(f, n->mac, ETH_ALEN);
726 n->tx_timer_active = qemu_get_be32(f);
727 n->mergeable_rx_bufs = qemu_get_be32(f);
729 if (version_id >= 3)
730 n->status = qemu_get_be16(f);
732 if (version_id >= 4) {
733 if (version_id < 8) {
734 n->promisc = qemu_get_be32(f);
735 n->allmulti = qemu_get_be32(f);
736 } else {
737 n->promisc = qemu_get_byte(f);
738 n->allmulti = qemu_get_byte(f);
742 if (version_id >= 5) {
743 n->mac_table.in_use = qemu_get_be32(f);
744 /* MAC_TABLE_ENTRIES may be different from the saved image */
745 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
746 qemu_get_buffer(f, n->mac_table.macs,
747 n->mac_table.in_use * ETH_ALEN);
748 } else if (n->mac_table.in_use) {
749 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
750 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
751 n->mac_table.in_use = 0;
755 if (version_id >= 6)
756 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
758 if (version_id >= 7) {
759 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
760 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
761 return -1;
764 if (n->has_vnet_hdr) {
765 tap_using_vnet_hdr(n->vc->peer, 1);
766 tap_set_offload(n->vc->peer,
767 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
768 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
769 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
770 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
771 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
775 if (version_id >= 9) {
776 n->mac_table.multi_overflow = qemu_get_byte(f);
777 n->mac_table.uni_overflow = qemu_get_byte(f);
780 if (version_id >= 10) {
781 n->alluni = qemu_get_byte(f);
782 n->nomulti = qemu_get_byte(f);
783 n->nouni = qemu_get_byte(f);
784 n->nobcast = qemu_get_byte(f);
787 if (version_id >= 11) {
788 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
789 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
790 return -1;
794 /* Find the first multicast entry in the saved MAC filter */
795 for (i = 0; i < n->mac_table.in_use; i++) {
796 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
797 break;
800 n->mac_table.first_multi = i;
802 if (n->tx_timer_active) {
803 qemu_mod_timer(n->tx_timer,
804 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
807 return 0;
810 static void virtio_net_cleanup(VLANClientState *vc)
812 VirtIONet *n = vc->opaque;
814 n->vc = NULL;
817 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
819 VirtIONet *n;
820 static int virtio_net_id;
822 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
823 sizeof(struct virtio_net_config),
824 sizeof(VirtIONet));
826 n->vdev.get_config = virtio_net_get_config;
827 n->vdev.set_config = virtio_net_set_config;
828 n->vdev.get_features = virtio_net_get_features;
829 n->vdev.set_features = virtio_net_set_features;
830 n->vdev.bad_features = virtio_net_bad_features;
831 n->vdev.reset = virtio_net_reset;
832 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
833 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
834 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
835 qemu_macaddr_default_if_unset(&conf->macaddr);
836 n->status = VIRTIO_NET_S_LINK_UP;
837 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
838 dev->info->name, dev->id,
839 virtio_net_can_receive,
840 virtio_net_receive, NULL, NULL,
841 virtio_net_cleanup, n);
842 n->vc->link_status_changed = virtio_net_set_link_status;
844 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
846 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
847 n->tx_timer_active = 0;
848 n->mergeable_rx_bufs = 0;
849 n->promisc = 1; /* for compatibility */
851 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
853 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
855 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
856 virtio_net_save, virtio_net_load, n);
858 return &n->vdev;
861 void virtio_net_exit(VirtIODevice *vdev)
863 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
865 qemu_purge_queued_packets(n->vc);
867 unregister_savevm("virtio-net", n);
869 qemu_free(n->mac_table.macs);
870 qemu_free(n->vlans);
872 qemu_del_timer(n->tx_timer);
873 qemu_free_timer(n->tx_timer);
875 virtio_cleanup(&n->vdev);
876 qemu_del_vlan_client(n->vc);