Merge remote branch 'mst/for_anthony' into staging
[qemu/aliguori-queue.git] / hw / virtio-net.c
blob5c0093e879df5dde1f94fef47efceb099cac427e
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-timer.h"
19 #include "virtio-net.h"
21 #define VIRTIO_NET_VM_VERSION 11
23 #define MAC_TABLE_ENTRIES 64
24 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
26 typedef struct VirtIONet
28 VirtIODevice vdev;
29 uint8_t mac[ETH_ALEN];
30 uint16_t status;
31 VirtQueue *rx_vq;
32 VirtQueue *tx_vq;
33 VirtQueue *ctrl_vq;
34 NICState *nic;
35 QEMUTimer *tx_timer;
36 int tx_timer_active;
37 uint32_t has_vnet_hdr;
38 uint8_t has_ufo;
39 struct {
40 VirtQueueElement elem;
41 ssize_t len;
42 } async_tx;
43 int mergeable_rx_bufs;
44 uint8_t promisc;
45 uint8_t allmulti;
46 uint8_t alluni;
47 uint8_t nomulti;
48 uint8_t nouni;
49 uint8_t nobcast;
50 struct {
51 int in_use;
52 int first_multi;
53 uint8_t multi_overflow;
54 uint8_t uni_overflow;
55 uint8_t *macs;
56 } mac_table;
57 uint32_t *vlans;
58 } VirtIONet;
60 /* TODO
61 * - we could suppress RX interrupt if we were so inclined.
64 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
66 return (VirtIONet *)vdev;
69 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
71 VirtIONet *n = to_virtio_net(vdev);
72 struct virtio_net_config netcfg;
74 netcfg.status = n->status;
75 memcpy(netcfg.mac, n->mac, ETH_ALEN);
76 memcpy(config, &netcfg, sizeof(netcfg));
79 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
84 memcpy(&netcfg, config, sizeof(netcfg));
86 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
87 memcpy(n->mac, netcfg.mac, ETH_ALEN);
88 qemu_format_nic_info_str(&n->nic->nc, n->mac);
92 static void virtio_net_set_link_status(VLANClientState *nc)
94 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
95 uint16_t old_status = n->status;
97 if (nc->link_down)
98 n->status &= ~VIRTIO_NET_S_LINK_UP;
99 else
100 n->status |= VIRTIO_NET_S_LINK_UP;
102 if (n->status != old_status)
103 virtio_notify_config(&n->vdev);
106 static void virtio_net_reset(VirtIODevice *vdev)
108 VirtIONet *n = to_virtio_net(vdev);
110 /* Reset back to compatibility mode */
111 n->promisc = 1;
112 n->allmulti = 0;
113 n->alluni = 0;
114 n->nomulti = 0;
115 n->nouni = 0;
116 n->nobcast = 0;
118 /* Flush any MAC and VLAN filter table state */
119 n->mac_table.in_use = 0;
120 n->mac_table.first_multi = 0;
121 n->mac_table.multi_overflow = 0;
122 n->mac_table.uni_overflow = 0;
123 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
124 memset(n->vlans, 0, MAX_VLAN >> 3);
127 static int peer_has_vnet_hdr(VirtIONet *n)
129 if (!n->nic->nc.peer)
130 return 0;
132 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
133 return 0;
135 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
137 return n->has_vnet_hdr;
140 static int peer_has_ufo(VirtIONet *n)
142 if (!peer_has_vnet_hdr(n))
143 return 0;
145 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
147 return n->has_ufo;
150 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
152 VirtIONet *n = to_virtio_net(vdev);
154 features |= (1 << VIRTIO_NET_F_MAC);
156 if (peer_has_vnet_hdr(n)) {
157 tap_using_vnet_hdr(n->nic->nc.peer, 1);
158 } else {
159 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
160 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
161 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
162 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
164 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
165 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
166 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
167 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
170 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
171 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
172 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
175 return features;
178 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
180 uint32_t features = 0;
182 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
183 * but also these: */
184 features |= (1 << VIRTIO_NET_F_MAC);
185 features |= (1 << VIRTIO_NET_F_CSUM);
186 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
187 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
188 features |= (1 << VIRTIO_NET_F_HOST_ECN);
190 return features;
193 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
195 VirtIONet *n = to_virtio_net(vdev);
197 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
199 if (n->has_vnet_hdr) {
200 tap_set_offload(n->nic->nc.peer,
201 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
202 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
203 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
204 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
205 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
209 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
210 VirtQueueElement *elem)
212 uint8_t on;
214 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
215 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
216 exit(1);
219 on = ldub_p(elem->out_sg[1].iov_base);
221 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
222 n->promisc = on;
223 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
224 n->allmulti = on;
225 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
226 n->alluni = on;
227 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
228 n->nomulti = on;
229 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
230 n->nouni = on;
231 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
232 n->nobcast = on;
233 else
234 return VIRTIO_NET_ERR;
236 return VIRTIO_NET_OK;
239 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
240 VirtQueueElement *elem)
242 struct virtio_net_ctrl_mac mac_data;
244 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
245 elem->out_sg[1].iov_len < sizeof(mac_data) ||
246 elem->out_sg[2].iov_len < sizeof(mac_data))
247 return VIRTIO_NET_ERR;
249 n->mac_table.in_use = 0;
250 n->mac_table.first_multi = 0;
251 n->mac_table.uni_overflow = 0;
252 n->mac_table.multi_overflow = 0;
253 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
255 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
257 if (sizeof(mac_data.entries) +
258 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
259 return VIRTIO_NET_ERR;
261 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
262 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
263 mac_data.entries * ETH_ALEN);
264 n->mac_table.in_use += mac_data.entries;
265 } else {
266 n->mac_table.uni_overflow = 1;
269 n->mac_table.first_multi = n->mac_table.in_use;
271 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
273 if (sizeof(mac_data.entries) +
274 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
275 return VIRTIO_NET_ERR;
277 if (mac_data.entries) {
278 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
279 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
280 elem->out_sg[2].iov_base + sizeof(mac_data),
281 mac_data.entries * ETH_ALEN);
282 n->mac_table.in_use += mac_data.entries;
283 } else {
284 n->mac_table.multi_overflow = 1;
288 return VIRTIO_NET_OK;
291 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
292 VirtQueueElement *elem)
294 uint16_t vid;
296 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
297 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
298 return VIRTIO_NET_ERR;
301 vid = lduw_le_p(elem->out_sg[1].iov_base);
303 if (vid >= MAX_VLAN)
304 return VIRTIO_NET_ERR;
306 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
307 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
308 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
309 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
310 else
311 return VIRTIO_NET_ERR;
313 return VIRTIO_NET_OK;
316 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
318 VirtIONet *n = to_virtio_net(vdev);
319 struct virtio_net_ctrl_hdr ctrl;
320 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
321 VirtQueueElement elem;
323 while (virtqueue_pop(vq, &elem)) {
324 if ((elem.in_num < 1) || (elem.out_num < 1)) {
325 fprintf(stderr, "virtio-net ctrl missing headers\n");
326 exit(1);
329 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
330 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
331 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
332 exit(1);
335 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
336 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
338 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
339 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
340 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
341 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
342 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
343 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
345 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
347 virtqueue_push(vq, &elem, sizeof(status));
348 virtio_notify(vdev, vq);
352 /* RX */
354 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
356 VirtIONet *n = to_virtio_net(vdev);
358 qemu_flush_queued_packets(&n->nic->nc);
360 /* We now have RX buffers, signal to the IO thread to break out of the
361 * select to re-poll the tap file descriptor */
362 qemu_notify_event();
365 static int virtio_net_can_receive(VLANClientState *nc)
367 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
369 if (!virtio_queue_ready(n->rx_vq) ||
370 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
371 return 0;
373 return 1;
376 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
378 if (virtio_queue_empty(n->rx_vq) ||
379 (n->mergeable_rx_bufs &&
380 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
381 virtio_queue_set_notification(n->rx_vq, 1);
383 /* To avoid a race condition where the guest has made some buffers
384 * available after the above check but before notification was
385 * enabled, check for available buffers again.
387 if (virtio_queue_empty(n->rx_vq) ||
388 (n->mergeable_rx_bufs &&
389 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
390 return 0;
393 virtio_queue_set_notification(n->rx_vq, 0);
394 return 1;
397 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
398 * it never finds out that the packets don't have valid checksums. This
399 * causes dhclient to get upset. Fedora's carried a patch for ages to
400 * fix this with Xen but it hasn't appeared in an upstream release of
401 * dhclient yet.
403 * To avoid breaking existing guests, we catch udp packets and add
404 * checksums. This is terrible but it's better than hacking the guest
405 * kernels.
407 * N.B. if we introduce a zero-copy API, this operation is no longer free so
408 * we should provide a mechanism to disable it to avoid polluting the host
409 * cache.
411 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
412 const uint8_t *buf, size_t size)
414 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
415 (size > 27 && size < 1500) && /* normal sized MTU */
416 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
417 (buf[23] == 17) && /* ip.protocol == UDP */
418 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
419 /* FIXME this cast is evil */
420 net_checksum_calculate((uint8_t *)buf, size);
421 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
425 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
427 int offset, i;
429 offset = i = 0;
430 while (offset < count && i < iovcnt) {
431 int len = MIN(iov[i].iov_len, count - offset);
432 memcpy(iov[i].iov_base, buf + offset, len);
433 offset += len;
434 i++;
437 return offset;
440 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
441 const void *buf, size_t size, size_t hdr_len)
443 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
444 int offset = 0;
446 hdr->flags = 0;
447 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
449 if (n->has_vnet_hdr) {
450 memcpy(hdr, buf, sizeof(*hdr));
451 offset = sizeof(*hdr);
452 work_around_broken_dhclient(hdr, buf + offset, size - offset);
455 /* We only ever receive a struct virtio_net_hdr from the tapfd,
456 * but we may be passing along a larger header to the guest.
458 iov[0].iov_base += hdr_len;
459 iov[0].iov_len -= hdr_len;
461 return offset;
464 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
466 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
467 static const uint8_t vlan[] = {0x81, 0x00};
468 uint8_t *ptr = (uint8_t *)buf;
469 int i;
471 if (n->promisc)
472 return 1;
474 if (n->has_vnet_hdr) {
475 ptr += sizeof(struct virtio_net_hdr);
478 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
479 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
480 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
481 return 0;
484 if (ptr[0] & 1) { // multicast
485 if (!memcmp(ptr, bcast, sizeof(bcast))) {
486 return !n->nobcast;
487 } else if (n->nomulti) {
488 return 0;
489 } else if (n->allmulti || n->mac_table.multi_overflow) {
490 return 1;
493 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
494 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
495 return 1;
498 } else { // unicast
499 if (n->nouni) {
500 return 0;
501 } else if (n->alluni || n->mac_table.uni_overflow) {
502 return 1;
503 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
504 return 1;
507 for (i = 0; i < n->mac_table.first_multi; i++) {
508 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
509 return 1;
514 return 0;
517 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
519 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
520 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
521 size_t hdr_len, offset, i;
523 if (!virtio_net_can_receive(&n->nic->nc))
524 return -1;
526 if (!virtio_net_has_buffers(n, size))
527 return 0;
529 if (!receive_filter(n, buf, size))
530 return size;
532 /* hdr_len refers to the header we supply to the guest */
533 hdr_len = n->mergeable_rx_bufs ?
534 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
536 offset = i = 0;
538 while (offset < size) {
539 VirtQueueElement elem;
540 int len, total;
541 struct iovec sg[VIRTQUEUE_MAX_SIZE];
543 total = 0;
545 if ((i != 0 && !n->mergeable_rx_bufs) ||
546 virtqueue_pop(n->rx_vq, &elem) == 0) {
547 if (i == 0)
548 return -1;
549 fprintf(stderr, "virtio-net truncating packet\n");
550 exit(1);
553 if (elem.in_num < 1) {
554 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
555 exit(1);
558 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
559 fprintf(stderr, "virtio-net header not in first element\n");
560 exit(1);
563 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
565 if (i == 0) {
566 if (n->mergeable_rx_bufs)
567 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
569 offset += receive_header(n, sg, elem.in_num,
570 buf + offset, size - offset, hdr_len);
571 total += hdr_len;
574 /* copy in packet. ugh */
575 len = iov_fill(sg, elem.in_num,
576 buf + offset, size - offset);
577 total += len;
579 /* signal other side */
580 virtqueue_fill(n->rx_vq, &elem, total, i++);
582 offset += len;
585 if (mhdr)
586 mhdr->num_buffers = i;
588 virtqueue_flush(n->rx_vq, i);
589 virtio_notify(&n->vdev, n->rx_vq);
591 return size;
594 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
596 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
598 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
600 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
601 virtio_notify(&n->vdev, n->tx_vq);
603 n->async_tx.elem.out_num = n->async_tx.len = 0;
605 virtio_queue_set_notification(n->tx_vq, 1);
606 virtio_net_flush_tx(n, n->tx_vq);
609 /* TX */
610 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
612 VirtQueueElement elem;
614 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
615 return;
617 if (n->async_tx.elem.out_num) {
618 virtio_queue_set_notification(n->tx_vq, 0);
619 return;
622 while (virtqueue_pop(vq, &elem)) {
623 ssize_t ret, len = 0;
624 unsigned int out_num = elem.out_num;
625 struct iovec *out_sg = &elem.out_sg[0];
626 unsigned hdr_len;
628 /* hdr_len refers to the header received from the guest */
629 hdr_len = n->mergeable_rx_bufs ?
630 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
631 sizeof(struct virtio_net_hdr);
633 if (out_num < 1 || out_sg->iov_len != hdr_len) {
634 fprintf(stderr, "virtio-net header not in first element\n");
635 exit(1);
638 /* ignore the header if GSO is not supported */
639 if (!n->has_vnet_hdr) {
640 out_num--;
641 out_sg++;
642 len += hdr_len;
643 } else if (n->mergeable_rx_bufs) {
644 /* tapfd expects a struct virtio_net_hdr */
645 hdr_len -= sizeof(struct virtio_net_hdr);
646 out_sg->iov_len -= hdr_len;
647 len += hdr_len;
650 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
651 virtio_net_tx_complete);
652 if (ret == 0) {
653 virtio_queue_set_notification(n->tx_vq, 0);
654 n->async_tx.elem = elem;
655 n->async_tx.len = len;
656 return;
659 len += ret;
661 virtqueue_push(vq, &elem, len);
662 virtio_notify(&n->vdev, vq);
666 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
668 VirtIONet *n = to_virtio_net(vdev);
670 if (n->tx_timer_active) {
671 virtio_queue_set_notification(vq, 1);
672 qemu_del_timer(n->tx_timer);
673 n->tx_timer_active = 0;
674 virtio_net_flush_tx(n, vq);
675 } else {
676 qemu_mod_timer(n->tx_timer,
677 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
678 n->tx_timer_active = 1;
679 virtio_queue_set_notification(vq, 0);
683 static void virtio_net_tx_timer(void *opaque)
685 VirtIONet *n = opaque;
687 n->tx_timer_active = 0;
689 /* Just in case the driver is not ready on more */
690 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
691 return;
693 virtio_queue_set_notification(n->tx_vq, 1);
694 virtio_net_flush_tx(n, n->tx_vq);
697 static void virtio_net_save(QEMUFile *f, void *opaque)
699 VirtIONet *n = opaque;
701 virtio_save(&n->vdev, f);
703 qemu_put_buffer(f, n->mac, ETH_ALEN);
704 qemu_put_be32(f, n->tx_timer_active);
705 qemu_put_be32(f, n->mergeable_rx_bufs);
706 qemu_put_be16(f, n->status);
707 qemu_put_byte(f, n->promisc);
708 qemu_put_byte(f, n->allmulti);
709 qemu_put_be32(f, n->mac_table.in_use);
710 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
711 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
712 qemu_put_be32(f, n->has_vnet_hdr);
713 qemu_put_byte(f, n->mac_table.multi_overflow);
714 qemu_put_byte(f, n->mac_table.uni_overflow);
715 qemu_put_byte(f, n->alluni);
716 qemu_put_byte(f, n->nomulti);
717 qemu_put_byte(f, n->nouni);
718 qemu_put_byte(f, n->nobcast);
719 qemu_put_byte(f, n->has_ufo);
722 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
724 VirtIONet *n = opaque;
725 int i;
727 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
728 return -EINVAL;
730 virtio_load(&n->vdev, f);
732 qemu_get_buffer(f, n->mac, ETH_ALEN);
733 n->tx_timer_active = qemu_get_be32(f);
734 n->mergeable_rx_bufs = qemu_get_be32(f);
736 if (version_id >= 3)
737 n->status = qemu_get_be16(f);
739 if (version_id >= 4) {
740 if (version_id < 8) {
741 n->promisc = qemu_get_be32(f);
742 n->allmulti = qemu_get_be32(f);
743 } else {
744 n->promisc = qemu_get_byte(f);
745 n->allmulti = qemu_get_byte(f);
749 if (version_id >= 5) {
750 n->mac_table.in_use = qemu_get_be32(f);
751 /* MAC_TABLE_ENTRIES may be different from the saved image */
752 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
753 qemu_get_buffer(f, n->mac_table.macs,
754 n->mac_table.in_use * ETH_ALEN);
755 } else if (n->mac_table.in_use) {
756 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
757 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
758 n->mac_table.in_use = 0;
762 if (version_id >= 6)
763 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
765 if (version_id >= 7) {
766 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
767 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
768 return -1;
771 if (n->has_vnet_hdr) {
772 tap_using_vnet_hdr(n->nic->nc.peer, 1);
773 tap_set_offload(n->nic->nc.peer,
774 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
775 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
776 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
777 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
778 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
782 if (version_id >= 9) {
783 n->mac_table.multi_overflow = qemu_get_byte(f);
784 n->mac_table.uni_overflow = qemu_get_byte(f);
787 if (version_id >= 10) {
788 n->alluni = qemu_get_byte(f);
789 n->nomulti = qemu_get_byte(f);
790 n->nouni = qemu_get_byte(f);
791 n->nobcast = qemu_get_byte(f);
794 if (version_id >= 11) {
795 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
796 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
797 return -1;
801 /* Find the first multicast entry in the saved MAC filter */
802 for (i = 0; i < n->mac_table.in_use; i++) {
803 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
804 break;
807 n->mac_table.first_multi = i;
809 if (n->tx_timer_active) {
810 qemu_mod_timer(n->tx_timer,
811 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
814 return 0;
817 static void virtio_net_cleanup(VLANClientState *nc)
819 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
821 n->nic = NULL;
824 static NetClientInfo net_virtio_info = {
825 .type = NET_CLIENT_TYPE_NIC,
826 .size = sizeof(NICState),
827 .can_receive = virtio_net_can_receive,
828 .receive = virtio_net_receive,
829 .cleanup = virtio_net_cleanup,
830 .link_status_changed = virtio_net_set_link_status,
833 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
835 VirtIONet *n;
836 static int virtio_net_id;
838 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
839 sizeof(struct virtio_net_config),
840 sizeof(VirtIONet));
842 n->vdev.get_config = virtio_net_get_config;
843 n->vdev.set_config = virtio_net_set_config;
844 n->vdev.get_features = virtio_net_get_features;
845 n->vdev.set_features = virtio_net_set_features;
846 n->vdev.bad_features = virtio_net_bad_features;
847 n->vdev.reset = virtio_net_reset;
848 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
849 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
850 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
851 qemu_macaddr_default_if_unset(&conf->macaddr);
852 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
853 n->status = VIRTIO_NET_S_LINK_UP;
855 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
857 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
859 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
860 n->tx_timer_active = 0;
861 n->mergeable_rx_bufs = 0;
862 n->promisc = 1; /* for compatibility */
864 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
866 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
868 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
869 virtio_net_save, virtio_net_load, n);
871 return &n->vdev;
874 void virtio_net_exit(VirtIODevice *vdev)
876 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
878 qemu_purge_queued_packets(&n->nic->nc);
880 unregister_savevm("virtio-net", n);
882 qemu_free(n->mac_table.macs);
883 qemu_free(n->vlans);
885 qemu_del_timer(n->tx_timer);
886 qemu_free_timer(n->tx_timer);
888 virtio_cleanup(&n->vdev);
889 qemu_del_vlan_client(&n->nic->nc);