Merge commit '97b156213e0e38b29da4480a32d4ec33d14d3012' into upstream-merge
[qemu-kvm/markmc.git] / hw / virtio-net.c
blob2bd4fa8a89da82d74c258534f9640a650e7b68ae
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"
18 #ifdef CONFIG_KVM
19 #include "qemu-kvm.h"
20 #endif
22 #define TAP_VNET_HDR
24 #define VIRTIO_NET_VM_VERSION 10
26 #define MAC_TABLE_ENTRIES 64
27 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
29 typedef struct VirtIONet
31 VirtIODevice vdev;
32 uint8_t mac[ETH_ALEN];
33 uint16_t status;
34 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
36 VirtQueue *ctrl_vq;
37 VLANClientState *vc;
38 QEMUTimer *tx_timer;
39 int tx_timer_active;
40 struct {
41 VirtQueueElement elem;
42 ssize_t len;
43 } async_tx;
44 int mergeable_rx_bufs;
45 uint8_t promisc;
46 uint8_t allmulti;
47 uint8_t alluni;
48 uint8_t nomulti;
49 uint8_t nouni;
50 uint8_t nobcast;
51 struct {
52 int in_use;
53 int first_multi;
54 uint8_t multi_overflow;
55 uint8_t uni_overflow;
56 uint8_t *macs;
57 } mac_table;
58 uint32_t *vlans;
59 } VirtIONet;
61 /* TODO
62 * - we could suppress RX interrupt if we were so inclined.
65 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
67 return (VirtIONet *)vdev;
70 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
72 VirtIONet *n = to_virtio_net(vdev);
73 struct virtio_net_config netcfg;
75 netcfg.status = n->status;
76 memcpy(netcfg.mac, n->mac, ETH_ALEN);
77 memcpy(config, &netcfg, sizeof(netcfg));
80 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
82 VirtIONet *n = to_virtio_net(vdev);
83 struct virtio_net_config netcfg;
85 memcpy(&netcfg, config, sizeof(netcfg));
87 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
88 memcpy(n->mac, netcfg.mac, ETH_ALEN);
89 qemu_format_nic_info_str(n->vc, n->mac);
93 static void virtio_net_set_link_status(VLANClientState *vc)
95 VirtIONet *n = vc->opaque;
96 uint16_t old_status = n->status;
98 if (vc->link_down)
99 n->status &= ~VIRTIO_NET_S_LINK_UP;
100 else
101 n->status |= VIRTIO_NET_S_LINK_UP;
103 if (n->status != old_status)
104 virtio_notify_config(&n->vdev);
107 static void virtio_net_reset(VirtIODevice *vdev)
109 VirtIONet *n = to_virtio_net(vdev);
111 /* Reset back to compatibility mode */
112 n->promisc = 1;
113 n->allmulti = 0;
114 n->alluni = 0;
115 n->nomulti = 0;
116 n->nouni = 0;
117 n->nobcast = 0;
119 /* Flush any MAC and VLAN filter table state */
120 n->mac_table.in_use = 0;
121 n->mac_table.first_multi = 0;
122 n->mac_table.multi_overflow = 0;
123 n->mac_table.uni_overflow = 0;
124 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
125 memset(n->vlans, 0, MAX_VLAN >> 3);
128 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
130 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
131 (1 << VIRTIO_NET_F_MRG_RXBUF) |
132 (1 << VIRTIO_NET_F_STATUS) |
133 (1 << VIRTIO_NET_F_CTRL_VQ) |
134 (1 << VIRTIO_NET_F_CTRL_RX) |
135 (1 << VIRTIO_NET_F_CTRL_VLAN) |
136 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
138 #ifdef TAP_VNET_HDR
139 VirtIONet *n = to_virtio_net(vdev);
140 VLANClientState *host = QTAILQ_FIRST(&n->vc->vlan->clients);
142 if (tap_has_vnet_hdr(host)) {
143 tap_using_vnet_hdr(host, 1);
144 features |= (1 << VIRTIO_NET_F_CSUM);
145 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
146 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
147 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
148 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
149 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
150 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
151 features |= (1 << VIRTIO_NET_F_HOST_ECN);
152 features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
153 if (tap_has_ufo(host)) {
154 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
155 features |= (1 << VIRTIO_NET_F_HOST_UFO);
158 #endif
160 return features;
163 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
165 uint32_t features = 0;
167 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
168 * but also these: */
169 features |= (1 << VIRTIO_NET_F_MAC);
170 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
171 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
172 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
173 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
175 return features & virtio_net_get_features(vdev);
178 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
180 VirtIONet *n = to_virtio_net(vdev);
181 #ifdef TAP_VNET_HDR
182 VLANClientState *host = QTAILQ_FIRST(&n->vc->vlan->clients);
183 #endif
185 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
187 #ifdef TAP_VNET_HDR
188 if (!tap_has_vnet_hdr(host) || !host->set_offload)
189 return;
191 host->set_offload(host,
192 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
193 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
194 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
195 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
196 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
197 #endif
200 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
201 VirtQueueElement *elem)
203 uint8_t on;
205 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
206 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
207 exit(1);
210 on = ldub_p(elem->out_sg[1].iov_base);
212 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
213 n->promisc = on;
214 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
215 n->allmulti = on;
216 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
217 n->alluni = on;
218 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
219 n->nomulti = on;
220 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
221 n->nouni = on;
222 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
223 n->nobcast = on;
224 else
225 return VIRTIO_NET_ERR;
227 return VIRTIO_NET_OK;
230 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
231 VirtQueueElement *elem)
233 struct virtio_net_ctrl_mac mac_data;
235 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
236 elem->out_sg[1].iov_len < sizeof(mac_data) ||
237 elem->out_sg[2].iov_len < sizeof(mac_data))
238 return VIRTIO_NET_ERR;
240 n->mac_table.in_use = 0;
241 n->mac_table.first_multi = 0;
242 n->mac_table.uni_overflow = 0;
243 n->mac_table.multi_overflow = 0;
244 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
246 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
248 if (sizeof(mac_data.entries) +
249 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
250 return VIRTIO_NET_ERR;
252 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
253 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
254 mac_data.entries * ETH_ALEN);
255 n->mac_table.in_use += mac_data.entries;
256 } else {
257 n->mac_table.uni_overflow = 1;
260 n->mac_table.first_multi = n->mac_table.in_use;
262 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
264 if (sizeof(mac_data.entries) +
265 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
266 return VIRTIO_NET_ERR;
268 if (mac_data.entries) {
269 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
270 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
271 elem->out_sg[2].iov_base + sizeof(mac_data),
272 mac_data.entries * ETH_ALEN);
273 n->mac_table.in_use += mac_data.entries;
274 } else {
275 n->mac_table.multi_overflow = 1;
279 return VIRTIO_NET_OK;
282 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
283 VirtQueueElement *elem)
285 uint16_t vid;
287 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
288 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
289 return VIRTIO_NET_ERR;
292 vid = lduw_le_p(elem->out_sg[1].iov_base);
294 if (vid >= MAX_VLAN)
295 return VIRTIO_NET_ERR;
297 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
298 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
299 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
300 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
301 else
302 return VIRTIO_NET_ERR;
304 return VIRTIO_NET_OK;
307 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
309 VirtIONet *n = to_virtio_net(vdev);
310 struct virtio_net_ctrl_hdr ctrl;
311 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
312 VirtQueueElement elem;
314 while (virtqueue_pop(vq, &elem)) {
315 if ((elem.in_num < 1) || (elem.out_num < 1)) {
316 fprintf(stderr, "virtio-net ctrl missing headers\n");
317 exit(1);
320 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
321 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
322 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
323 exit(1);
326 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
327 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
329 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
330 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
331 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
332 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
333 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
334 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
336 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
338 virtqueue_push(vq, &elem, sizeof(status));
339 virtio_notify(vdev, vq);
343 /* RX */
345 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
347 VirtIONet *n = to_virtio_net(vdev);
349 qemu_flush_queued_packets(n->vc);
351 /* We now have RX buffers, signal to the IO thread to break out of the
352 * select to re-poll the tap file descriptor */
353 qemu_notify_event();
356 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
358 if (!virtio_queue_ready(n->rx_vq) ||
359 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
360 return 0;
362 if (virtio_queue_empty(n->rx_vq) ||
363 (n->mergeable_rx_bufs &&
364 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
365 virtio_queue_set_notification(n->rx_vq, 1);
366 return 0;
369 virtio_queue_set_notification(n->rx_vq, 0);
370 return 1;
373 static int virtio_net_can_receive(VLANClientState *vc)
375 VirtIONet *n = vc->opaque;
377 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
380 #ifdef TAP_VNET_HDR
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;
408 #endif
410 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
412 int offset, i;
414 offset = i = 0;
415 while (offset < count && i < iovcnt) {
416 int len = MIN(iov[i].iov_len, count - offset);
417 memcpy(iov[i].iov_base, buf + offset, len);
418 offset += len;
419 i++;
422 return offset;
425 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
426 const void *buf, size_t size, size_t hdr_len, int raw)
428 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
429 int offset = 0;
431 hdr->flags = 0;
432 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
434 #ifdef TAP_VNET_HDR
435 if (tap_has_vnet_hdr(QTAILQ_FIRST(&n->vc->vlan->clients))) {
436 if (!raw) {
437 memcpy(hdr, buf, sizeof(*hdr));
438 } else {
439 memset(hdr, 0, sizeof(*hdr));
441 offset = sizeof(*hdr);
442 work_around_broken_dhclient(hdr, buf + offset, size - offset);
444 #endif
446 /* We only ever receive a struct virtio_net_hdr from the tapfd,
447 * but we may be passing along a larger header to the guest.
449 iov[0].iov_base += hdr_len;
450 iov[0].iov_len -= hdr_len;
452 return offset;
455 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
457 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
458 static const uint8_t vlan[] = {0x81, 0x00};
459 uint8_t *ptr = (uint8_t *)buf;
460 int i;
462 if (n->promisc)
463 return 1;
465 #ifdef TAP_VNET_HDR
466 if (tap_has_vnet_hdr(QTAILQ_FIRST(&n->vc->vlan->clients)))
467 ptr += sizeof(struct virtio_net_hdr);
468 #endif
470 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
471 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
472 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
473 return 0;
476 if (ptr[0] & 1) { // multicast
477 if (!memcmp(ptr, bcast, sizeof(bcast))) {
478 return !n->nobcast;
479 } else if (n->nomulti) {
480 return 0;
481 } else if (n->allmulti || n->mac_table.multi_overflow) {
482 return 1;
485 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
486 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
487 return 1;
490 } else { // unicast
491 if (n->nouni) {
492 return 0;
493 } else if (n->alluni || n->mac_table.uni_overflow) {
494 return 1;
495 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
496 return 1;
499 for (i = 0; i < n->mac_table.first_multi; i++) {
500 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
501 return 1;
506 return 0;
509 static ssize_t virtio_net_receive2(VLANClientState *vc, const uint8_t *buf, size_t size, int raw)
511 VirtIONet *n = vc->opaque;
512 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
513 size_t hdr_len, offset, i;
515 if (!do_virtio_net_can_receive(n, size))
516 return 0;
518 if (!receive_filter(n, buf, size))
519 return size;
521 /* hdr_len refers to the header we supply to the guest */
522 hdr_len = n->mergeable_rx_bufs ?
523 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
525 offset = i = 0;
527 while (offset < size) {
528 VirtQueueElement elem;
529 int len, total;
530 struct iovec sg[VIRTQUEUE_MAX_SIZE];
532 len = total = 0;
534 if ((i != 0 && !n->mergeable_rx_bufs) ||
535 virtqueue_pop(n->rx_vq, &elem) == 0) {
536 if (i == 0)
537 return -1;
538 fprintf(stderr, "virtio-net truncating packet\n");
539 exit(1);
542 if (elem.in_num < 1) {
543 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
544 exit(1);
547 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
548 fprintf(stderr, "virtio-net header not in first element\n");
549 exit(1);
552 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
554 if (i == 0) {
555 if (n->mergeable_rx_bufs)
556 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
558 offset += receive_header(n, sg, elem.in_num,
559 buf + offset, size - offset, hdr_len, raw);
560 total += hdr_len;
563 /* copy in packet. ugh */
564 len = iov_fill(sg, elem.in_num,
565 buf + offset, size - offset);
566 total += len;
568 /* signal other side */
569 virtqueue_fill(n->rx_vq, &elem, total, i++);
571 offset += len;
574 if (mhdr)
575 mhdr->num_buffers = i;
577 virtqueue_flush(n->rx_vq, i);
578 virtio_notify(&n->vdev, n->rx_vq);
580 return size;
583 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
585 static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
587 VirtIONet *n = vc->opaque;
589 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
590 virtio_notify(&n->vdev, n->tx_vq);
592 n->async_tx.elem.out_num = n->async_tx.len = 0;
594 virtio_queue_set_notification(n->tx_vq, 1);
595 virtio_net_flush_tx(n, n->tx_vq);
598 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
600 return virtio_net_receive2(vc, buf, size, 0);
603 static ssize_t virtio_net_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
605 return virtio_net_receive2(vc, buf, size, 1);
608 /* TX */
609 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
611 VirtQueueElement elem;
612 #ifdef TAP_VNET_HDR
613 int has_vnet_hdr = tap_has_vnet_hdr(QTAILQ_FIRST(&n->vc->vlan->clients));
614 #else
615 int has_vnet_hdr = 0;
616 #endif
618 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
619 return;
621 if (n->async_tx.elem.out_num) {
622 virtio_queue_set_notification(n->tx_vq, 0);
623 return;
626 while (virtqueue_pop(vq, &elem)) {
627 ssize_t ret, len = 0;
628 unsigned int out_num = elem.out_num;
629 struct iovec *out_sg = &elem.out_sg[0];
630 unsigned hdr_len;
632 /* hdr_len refers to the header received from the guest */
633 hdr_len = n->mergeable_rx_bufs ?
634 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
635 sizeof(struct virtio_net_hdr);
637 if (out_num < 1 || out_sg->iov_len != hdr_len) {
638 fprintf(stderr, "virtio-net header not in first element\n");
639 exit(1);
642 /* ignore the header if GSO is not supported */
643 if (!has_vnet_hdr) {
644 out_num--;
645 out_sg++;
646 len += hdr_len;
647 } else if (n->mergeable_rx_bufs) {
648 /* tapfd expects a struct virtio_net_hdr */
649 hdr_len -= sizeof(struct virtio_net_hdr);
650 out_sg->iov_len -= hdr_len;
651 len += hdr_len;
654 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
655 virtio_net_tx_complete);
656 if (ret == 0) {
657 virtio_queue_set_notification(n->tx_vq, 0);
658 n->async_tx.elem = elem;
659 n->async_tx.len = len;
660 return;
663 len += ret;
665 virtqueue_push(vq, &elem, len);
666 virtio_notify(&n->vdev, vq);
670 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
672 VirtIONet *n = to_virtio_net(vdev);
674 if (n->tx_timer_active) {
675 virtio_queue_set_notification(vq, 1);
676 qemu_del_timer(n->tx_timer);
677 n->tx_timer_active = 0;
678 virtio_net_flush_tx(n, vq);
679 } else {
680 qemu_mod_timer(n->tx_timer,
681 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
682 n->tx_timer_active = 1;
683 virtio_queue_set_notification(vq, 0);
687 static void virtio_net_tx_timer(void *opaque)
689 VirtIONet *n = opaque;
691 n->tx_timer_active = 0;
693 /* Just in case the driver is not ready on more */
694 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
695 return;
697 virtio_queue_set_notification(n->tx_vq, 1);
698 virtio_net_flush_tx(n, n->tx_vq);
701 static void virtio_net_save(QEMUFile *f, void *opaque)
703 VirtIONet *n = opaque;
705 virtio_save(&n->vdev, f);
707 qemu_put_buffer(f, n->mac, ETH_ALEN);
708 qemu_put_be32(f, n->tx_timer_active);
709 qemu_put_be32(f, n->mergeable_rx_bufs);
710 qemu_put_be16(f, n->status);
711 qemu_put_byte(f, n->promisc);
712 qemu_put_byte(f, n->allmulti);
713 qemu_put_be32(f, n->mac_table.in_use);
714 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
715 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
717 #ifdef TAP_VNET_HDR
718 qemu_put_be32(f, tap_has_vnet_hdr(QTAILQ_FIRST(&n->vc->vlan->clients)));
719 #else
720 qemu_put_be32(f, 0);
721 #endif
723 qemu_put_byte(f, n->mac_table.multi_overflow);
724 qemu_put_byte(f, n->mac_table.uni_overflow);
725 qemu_put_byte(f, n->alluni);
726 qemu_put_byte(f, n->nomulti);
727 qemu_put_byte(f, n->nouni);
728 qemu_put_byte(f, n->nobcast);
731 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
733 VirtIONet *n = opaque;
734 int i;
736 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
737 return -EINVAL;
739 virtio_load(&n->vdev, f);
741 qemu_get_buffer(f, n->mac, ETH_ALEN);
742 n->tx_timer_active = qemu_get_be32(f);
743 n->mergeable_rx_bufs = qemu_get_be32(f);
745 if (version_id >= 3)
746 n->status = qemu_get_be16(f);
748 if (version_id >= 4) {
749 if (version_id < 8) {
750 n->promisc = qemu_get_be32(f);
751 n->allmulti = qemu_get_be32(f);
752 } else {
753 n->promisc = qemu_get_byte(f);
754 n->allmulti = qemu_get_byte(f);
758 if (version_id >= 5) {
759 n->mac_table.in_use = qemu_get_be32(f);
760 /* MAC_TABLE_ENTRIES may be different from the saved image */
761 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
762 qemu_get_buffer(f, n->mac_table.macs,
763 n->mac_table.in_use * ETH_ALEN);
764 } else if (n->mac_table.in_use) {
765 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
766 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
767 n->mac_table.in_use = 0;
771 if (version_id >= 6)
772 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
774 if (version_id >= 7 && qemu_get_be32(f)) {
775 #ifdef TAP_VNET_HDR
776 tap_using_vnet_hdr(QTAILQ_FIRST(&n->vc->vlan->clients), 1);
777 #else
778 fprintf(stderr,
779 "virtio-net: saved image requires vnet header support\n");
780 exit(1);
781 #endif
784 if (version_id >= 9) {
785 n->mac_table.multi_overflow = qemu_get_byte(f);
786 n->mac_table.uni_overflow = qemu_get_byte(f);
789 if (version_id >= 10) {
790 n->alluni = qemu_get_byte(f);
791 n->nomulti = qemu_get_byte(f);
792 n->nouni = qemu_get_byte(f);
793 n->nobcast = qemu_get_byte(f);
796 /* Find the first multicast entry in the saved MAC filter */
797 for (i = 0; i < n->mac_table.in_use; i++) {
798 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
799 break;
802 n->mac_table.first_multi = i;
804 if (n->tx_timer_active) {
805 qemu_mod_timer(n->tx_timer,
806 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
809 return 0;
812 static void virtio_net_cleanup(VLANClientState *vc)
814 VirtIONet *n = vc->opaque;
816 n->vc = NULL;
819 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
821 VirtIONet *n;
822 static int virtio_net_id;
824 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
825 sizeof(struct virtio_net_config),
826 sizeof(VirtIONet));
828 n->vdev.get_config = virtio_net_get_config;
829 n->vdev.set_config = virtio_net_set_config;
830 n->vdev.get_features = virtio_net_get_features;
831 n->vdev.set_features = virtio_net_set_features;
832 n->vdev.bad_features = virtio_net_bad_features;
833 n->vdev.reset = virtio_net_reset;
834 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
835 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
836 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
837 qemu_macaddr_default_if_unset(&conf->macaddr);
838 n->status = VIRTIO_NET_S_LINK_UP;
839 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
840 dev->info->name, dev->id,
841 virtio_net_can_receive,
842 virtio_net_receive, NULL, NULL,
843 virtio_net_cleanup, n);
844 n->vc->link_status_changed = virtio_net_set_link_status;
845 n->vc->receive_raw = virtio_net_receive_raw;
847 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
849 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
850 n->tx_timer_active = 0;
851 n->mergeable_rx_bufs = 0;
852 n->promisc = 1; /* for compatibility */
854 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
856 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
858 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
859 virtio_net_save, virtio_net_load, n);
861 return &n->vdev;
864 void virtio_net_exit(VirtIODevice *vdev)
866 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
868 qemu_purge_queued_packets(n->vc);
870 unregister_savevm("virtio-net", n);
872 qemu_free(n->mac_table.macs);
873 qemu_free(n->vlans);
875 qemu_del_timer(n->tx_timer);
876 qemu_free_timer(n->tx_timer);
878 virtio_cleanup(&n->vdev);
879 qemu_del_vlan_client(n->vc);