hw/usb-msd: fix some usb requests
[qemu.git] / hw / virtio-net.c
blobbe33c68bbc76d95a714bda49ec5a6cf9d5ba5660
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 "net/tap.h"
18 #include "qemu-error.h"
19 #include "qemu-timer.h"
20 #include "virtio-net.h"
22 #define VIRTIO_NET_VM_VERSION 11
24 #define MAC_TABLE_ENTRIES 64
25 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
27 typedef struct VirtIONet
29 VirtIODevice vdev;
30 uint8_t mac[ETH_ALEN];
31 uint16_t status;
32 VirtQueue *rx_vq;
33 VirtQueue *tx_vq;
34 VirtQueue *ctrl_vq;
35 NICState *nic;
36 QEMUTimer *tx_timer;
37 int tx_timer_active;
38 uint32_t has_vnet_hdr;
39 uint8_t has_ufo;
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->nic->nc, n->mac);
93 static void virtio_net_set_link_status(VLANClientState *nc)
95 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
96 uint16_t old_status = n->status;
98 if (nc->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 int peer_has_vnet_hdr(VirtIONet *n)
130 if (!n->nic->nc.peer)
131 return 0;
133 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
134 return 0;
136 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
138 return n->has_vnet_hdr;
141 static int peer_has_ufo(VirtIONet *n)
143 if (!peer_has_vnet_hdr(n))
144 return 0;
146 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
148 return n->has_ufo;
151 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
153 VirtIONet *n = to_virtio_net(vdev);
155 features |= (1 << VIRTIO_NET_F_MAC);
157 if (peer_has_vnet_hdr(n)) {
158 tap_using_vnet_hdr(n->nic->nc.peer, 1);
159 } else {
160 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
161 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
162 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
163 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
165 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
166 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
167 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
168 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
171 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
172 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
173 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
176 return features;
179 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
181 uint32_t features = 0;
183 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
184 * but also these: */
185 features |= (1 << VIRTIO_NET_F_MAC);
186 features |= (1 << VIRTIO_NET_F_CSUM);
187 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
188 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
189 features |= (1 << VIRTIO_NET_F_HOST_ECN);
191 return features;
194 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
196 VirtIONet *n = to_virtio_net(vdev);
198 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
200 if (n->has_vnet_hdr) {
201 tap_set_offload(n->nic->nc.peer,
202 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
203 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
204 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
205 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
206 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
210 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
211 VirtQueueElement *elem)
213 uint8_t on;
215 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
216 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
217 exit(1);
220 on = ldub_p(elem->out_sg[1].iov_base);
222 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
223 n->promisc = on;
224 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
225 n->allmulti = on;
226 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
227 n->alluni = on;
228 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
229 n->nomulti = on;
230 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
231 n->nouni = on;
232 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
233 n->nobcast = on;
234 else
235 return VIRTIO_NET_ERR;
237 return VIRTIO_NET_OK;
240 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
241 VirtQueueElement *elem)
243 struct virtio_net_ctrl_mac mac_data;
245 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
246 elem->out_sg[1].iov_len < sizeof(mac_data) ||
247 elem->out_sg[2].iov_len < sizeof(mac_data))
248 return VIRTIO_NET_ERR;
250 n->mac_table.in_use = 0;
251 n->mac_table.first_multi = 0;
252 n->mac_table.uni_overflow = 0;
253 n->mac_table.multi_overflow = 0;
254 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
256 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
258 if (sizeof(mac_data.entries) +
259 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
260 return VIRTIO_NET_ERR;
262 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
263 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
264 mac_data.entries * ETH_ALEN);
265 n->mac_table.in_use += mac_data.entries;
266 } else {
267 n->mac_table.uni_overflow = 1;
270 n->mac_table.first_multi = n->mac_table.in_use;
272 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
274 if (sizeof(mac_data.entries) +
275 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
276 return VIRTIO_NET_ERR;
278 if (mac_data.entries) {
279 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
280 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
281 elem->out_sg[2].iov_base + sizeof(mac_data),
282 mac_data.entries * ETH_ALEN);
283 n->mac_table.in_use += mac_data.entries;
284 } else {
285 n->mac_table.multi_overflow = 1;
289 return VIRTIO_NET_OK;
292 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
293 VirtQueueElement *elem)
295 uint16_t vid;
297 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
298 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
299 return VIRTIO_NET_ERR;
302 vid = lduw_le_p(elem->out_sg[1].iov_base);
304 if (vid >= MAX_VLAN)
305 return VIRTIO_NET_ERR;
307 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
308 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
309 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
310 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
311 else
312 return VIRTIO_NET_ERR;
314 return VIRTIO_NET_OK;
317 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
319 VirtIONet *n = to_virtio_net(vdev);
320 struct virtio_net_ctrl_hdr ctrl;
321 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
322 VirtQueueElement elem;
324 while (virtqueue_pop(vq, &elem)) {
325 if ((elem.in_num < 1) || (elem.out_num < 1)) {
326 fprintf(stderr, "virtio-net ctrl missing headers\n");
327 exit(1);
330 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
331 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
332 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
333 exit(1);
336 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
337 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
339 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
340 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
341 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
342 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
343 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
344 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
346 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
348 virtqueue_push(vq, &elem, sizeof(status));
349 virtio_notify(vdev, vq);
353 /* RX */
355 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
357 VirtIONet *n = to_virtio_net(vdev);
359 qemu_flush_queued_packets(&n->nic->nc);
361 /* We now have RX buffers, signal to the IO thread to break out of the
362 * select to re-poll the tap file descriptor */
363 qemu_notify_event();
366 static int virtio_net_can_receive(VLANClientState *nc)
368 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
370 if (!virtio_queue_ready(n->rx_vq) ||
371 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
372 return 0;
374 return 1;
377 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
379 if (virtio_queue_empty(n->rx_vq) ||
380 (n->mergeable_rx_bufs &&
381 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
382 virtio_queue_set_notification(n->rx_vq, 1);
384 /* To avoid a race condition where the guest has made some buffers
385 * available after the above check but before notification was
386 * enabled, check for available buffers again.
388 if (virtio_queue_empty(n->rx_vq) ||
389 (n->mergeable_rx_bufs &&
390 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
391 return 0;
394 virtio_queue_set_notification(n->rx_vq, 0);
395 return 1;
398 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
399 * it never finds out that the packets don't have valid checksums. This
400 * causes dhclient to get upset. Fedora's carried a patch for ages to
401 * fix this with Xen but it hasn't appeared in an upstream release of
402 * dhclient yet.
404 * To avoid breaking existing guests, we catch udp packets and add
405 * checksums. This is terrible but it's better than hacking the guest
406 * kernels.
408 * N.B. if we introduce a zero-copy API, this operation is no longer free so
409 * we should provide a mechanism to disable it to avoid polluting the host
410 * cache.
412 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
413 const uint8_t *buf, size_t size)
415 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
416 (size > 27 && size < 1500) && /* normal sized MTU */
417 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
418 (buf[23] == 17) && /* ip.protocol == UDP */
419 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
420 /* FIXME this cast is evil */
421 net_checksum_calculate((uint8_t *)buf, size);
422 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
426 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
428 int offset, i;
430 offset = i = 0;
431 while (offset < count && i < iovcnt) {
432 int len = MIN(iov[i].iov_len, count - offset);
433 memcpy(iov[i].iov_base, buf + offset, len);
434 offset += len;
435 i++;
438 return offset;
441 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
442 const void *buf, size_t size, size_t hdr_len)
444 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
445 int offset = 0;
447 hdr->flags = 0;
448 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
450 if (n->has_vnet_hdr) {
451 memcpy(hdr, buf, sizeof(*hdr));
452 offset = sizeof(*hdr);
453 work_around_broken_dhclient(hdr, buf + offset, size - offset);
456 /* We only ever receive a struct virtio_net_hdr from the tapfd,
457 * but we may be passing along a larger header to the guest.
459 iov[0].iov_base += hdr_len;
460 iov[0].iov_len -= hdr_len;
462 return offset;
465 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
467 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
468 static const uint8_t vlan[] = {0x81, 0x00};
469 uint8_t *ptr = (uint8_t *)buf;
470 int i;
472 if (n->promisc)
473 return 1;
475 if (n->has_vnet_hdr) {
476 ptr += sizeof(struct virtio_net_hdr);
479 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
480 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
481 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
482 return 0;
485 if (ptr[0] & 1) { // multicast
486 if (!memcmp(ptr, bcast, sizeof(bcast))) {
487 return !n->nobcast;
488 } else if (n->nomulti) {
489 return 0;
490 } else if (n->allmulti || n->mac_table.multi_overflow) {
491 return 1;
494 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
495 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
496 return 1;
499 } else { // unicast
500 if (n->nouni) {
501 return 0;
502 } else if (n->alluni || n->mac_table.uni_overflow) {
503 return 1;
504 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
505 return 1;
508 for (i = 0; i < n->mac_table.first_multi; i++) {
509 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
510 return 1;
515 return 0;
518 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
520 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
521 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
522 size_t hdr_len, offset, i;
524 if (!virtio_net_can_receive(&n->nic->nc))
525 return -1;
527 if (!virtio_net_has_buffers(n, size))
528 return 0;
530 if (!receive_filter(n, buf, size))
531 return size;
533 /* hdr_len refers to the header we supply to the guest */
534 hdr_len = n->mergeable_rx_bufs ?
535 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
537 offset = i = 0;
539 while (offset < size) {
540 VirtQueueElement elem;
541 int len, total;
542 struct iovec sg[VIRTQUEUE_MAX_SIZE];
544 total = 0;
546 if ((i != 0 && !n->mergeable_rx_bufs) ||
547 virtqueue_pop(n->rx_vq, &elem) == 0) {
548 if (i == 0)
549 return -1;
550 fprintf(stderr, "virtio-net truncating packet\n");
551 exit(1);
554 if (elem.in_num < 1) {
555 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
556 exit(1);
559 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
560 fprintf(stderr, "virtio-net header not in first element\n");
561 exit(1);
564 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
566 if (i == 0) {
567 if (n->mergeable_rx_bufs)
568 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
570 offset += receive_header(n, sg, elem.in_num,
571 buf + offset, size - offset, hdr_len);
572 total += hdr_len;
575 /* copy in packet. ugh */
576 len = iov_fill(sg, elem.in_num,
577 buf + offset, size - offset);
578 total += len;
580 /* signal other side */
581 virtqueue_fill(n->rx_vq, &elem, total, i++);
583 offset += len;
586 if (mhdr)
587 mhdr->num_buffers = i;
589 virtqueue_flush(n->rx_vq, i);
590 virtio_notify(&n->vdev, n->rx_vq);
592 return size;
595 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
597 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
599 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
601 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
602 virtio_notify(&n->vdev, n->tx_vq);
604 n->async_tx.elem.out_num = n->async_tx.len = 0;
606 virtio_queue_set_notification(n->tx_vq, 1);
607 virtio_net_flush_tx(n, n->tx_vq);
610 /* TX */
611 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
613 VirtQueueElement elem;
615 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
616 return;
618 if (n->async_tx.elem.out_num) {
619 virtio_queue_set_notification(n->tx_vq, 0);
620 return;
623 while (virtqueue_pop(vq, &elem)) {
624 ssize_t ret, len = 0;
625 unsigned int out_num = elem.out_num;
626 struct iovec *out_sg = &elem.out_sg[0];
627 unsigned hdr_len;
629 /* hdr_len refers to the header received from the guest */
630 hdr_len = n->mergeable_rx_bufs ?
631 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
632 sizeof(struct virtio_net_hdr);
634 if (out_num < 1 || out_sg->iov_len != hdr_len) {
635 fprintf(stderr, "virtio-net header not in first element\n");
636 exit(1);
639 /* ignore the header if GSO is not supported */
640 if (!n->has_vnet_hdr) {
641 out_num--;
642 out_sg++;
643 len += hdr_len;
644 } else if (n->mergeable_rx_bufs) {
645 /* tapfd expects a struct virtio_net_hdr */
646 hdr_len -= sizeof(struct virtio_net_hdr);
647 out_sg->iov_len -= hdr_len;
648 len += hdr_len;
651 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
652 virtio_net_tx_complete);
653 if (ret == 0) {
654 virtio_queue_set_notification(n->tx_vq, 0);
655 n->async_tx.elem = elem;
656 n->async_tx.len = len;
657 return;
660 len += ret;
662 virtqueue_push(vq, &elem, len);
663 virtio_notify(&n->vdev, vq);
667 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
669 VirtIONet *n = to_virtio_net(vdev);
671 if (n->tx_timer_active) {
672 virtio_queue_set_notification(vq, 1);
673 qemu_del_timer(n->tx_timer);
674 n->tx_timer_active = 0;
675 virtio_net_flush_tx(n, vq);
676 } else {
677 qemu_mod_timer(n->tx_timer,
678 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
679 n->tx_timer_active = 1;
680 virtio_queue_set_notification(vq, 0);
684 static void virtio_net_tx_timer(void *opaque)
686 VirtIONet *n = opaque;
688 n->tx_timer_active = 0;
690 /* Just in case the driver is not ready on more */
691 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
692 return;
694 virtio_queue_set_notification(n->tx_vq, 1);
695 virtio_net_flush_tx(n, n->tx_vq);
698 static void virtio_net_save(QEMUFile *f, void *opaque)
700 VirtIONet *n = opaque;
702 virtio_save(&n->vdev, f);
704 qemu_put_buffer(f, n->mac, ETH_ALEN);
705 qemu_put_be32(f, n->tx_timer_active);
706 qemu_put_be32(f, n->mergeable_rx_bufs);
707 qemu_put_be16(f, n->status);
708 qemu_put_byte(f, n->promisc);
709 qemu_put_byte(f, n->allmulti);
710 qemu_put_be32(f, n->mac_table.in_use);
711 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
712 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
713 qemu_put_be32(f, n->has_vnet_hdr);
714 qemu_put_byte(f, n->mac_table.multi_overflow);
715 qemu_put_byte(f, n->mac_table.uni_overflow);
716 qemu_put_byte(f, n->alluni);
717 qemu_put_byte(f, n->nomulti);
718 qemu_put_byte(f, n->nouni);
719 qemu_put_byte(f, n->nobcast);
720 qemu_put_byte(f, n->has_ufo);
723 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
725 VirtIONet *n = opaque;
726 int i;
728 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
729 return -EINVAL;
731 virtio_load(&n->vdev, f);
733 qemu_get_buffer(f, n->mac, ETH_ALEN);
734 n->tx_timer_active = qemu_get_be32(f);
735 n->mergeable_rx_bufs = qemu_get_be32(f);
737 if (version_id >= 3)
738 n->status = qemu_get_be16(f);
740 if (version_id >= 4) {
741 if (version_id < 8) {
742 n->promisc = qemu_get_be32(f);
743 n->allmulti = qemu_get_be32(f);
744 } else {
745 n->promisc = qemu_get_byte(f);
746 n->allmulti = qemu_get_byte(f);
750 if (version_id >= 5) {
751 n->mac_table.in_use = qemu_get_be32(f);
752 /* MAC_TABLE_ENTRIES may be different from the saved image */
753 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
754 qemu_get_buffer(f, n->mac_table.macs,
755 n->mac_table.in_use * ETH_ALEN);
756 } else if (n->mac_table.in_use) {
757 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
758 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
759 n->mac_table.in_use = 0;
763 if (version_id >= 6)
764 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
766 if (version_id >= 7) {
767 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
768 error_report("virtio-net: saved image requires vnet_hdr=on");
769 return -1;
772 if (n->has_vnet_hdr) {
773 tap_using_vnet_hdr(n->nic->nc.peer, 1);
774 tap_set_offload(n->nic->nc.peer,
775 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
776 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
777 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
778 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
779 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
783 if (version_id >= 9) {
784 n->mac_table.multi_overflow = qemu_get_byte(f);
785 n->mac_table.uni_overflow = qemu_get_byte(f);
788 if (version_id >= 10) {
789 n->alluni = qemu_get_byte(f);
790 n->nomulti = qemu_get_byte(f);
791 n->nouni = qemu_get_byte(f);
792 n->nobcast = qemu_get_byte(f);
795 if (version_id >= 11) {
796 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
797 error_report("virtio-net: saved image requires TUN_F_UFO support");
798 return -1;
802 /* Find the first multicast entry in the saved MAC filter */
803 for (i = 0; i < n->mac_table.in_use; i++) {
804 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
805 break;
808 n->mac_table.first_multi = i;
810 if (n->tx_timer_active) {
811 qemu_mod_timer(n->tx_timer,
812 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
815 return 0;
818 static void virtio_net_cleanup(VLANClientState *nc)
820 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
822 n->nic = NULL;
825 static NetClientInfo net_virtio_info = {
826 .type = NET_CLIENT_TYPE_NIC,
827 .size = sizeof(NICState),
828 .can_receive = virtio_net_can_receive,
829 .receive = virtio_net_receive,
830 .cleanup = virtio_net_cleanup,
831 .link_status_changed = virtio_net_set_link_status,
834 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
836 VirtIONet *n;
837 static int virtio_net_id;
839 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
840 sizeof(struct virtio_net_config),
841 sizeof(VirtIONet));
843 n->vdev.get_config = virtio_net_get_config;
844 n->vdev.set_config = virtio_net_set_config;
845 n->vdev.get_features = virtio_net_get_features;
846 n->vdev.set_features = virtio_net_set_features;
847 n->vdev.bad_features = virtio_net_bad_features;
848 n->vdev.reset = virtio_net_reset;
849 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
850 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
851 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
852 qemu_macaddr_default_if_unset(&conf->macaddr);
853 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
854 n->status = VIRTIO_NET_S_LINK_UP;
856 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
858 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
860 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
861 n->tx_timer_active = 0;
862 n->mergeable_rx_bufs = 0;
863 n->promisc = 1; /* for compatibility */
865 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
867 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
869 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
870 virtio_net_save, virtio_net_load, n);
872 return &n->vdev;
875 void virtio_net_exit(VirtIODevice *vdev)
877 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
879 qemu_purge_queued_packets(&n->nic->nc);
881 unregister_savevm("virtio-net", n);
883 qemu_free(n->mac_table.macs);
884 qemu_free(n->vlans);
886 qemu_del_timer(n->tx_timer);
887 qemu_free_timer(n->tx_timer);
889 virtio_cleanup(&n->vdev);
890 qemu_del_vlan_client(&n->nic->nc);