Fix 32-bit overflow in parallels image support
[qemu-kvm/fedora.git] / hw / virtio-net.c
blobce8e6cb7afb5b49173ed1a9ac05354e73eb8cd6d
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 = n->vc->vlan->first_client;
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 /* Kernel can't actually handle UFO in software currently. */
155 #endif
157 return features;
160 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
162 uint32_t features = 0;
164 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
165 * but also these: */
166 features |= (1 << VIRTIO_NET_F_MAC);
167 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
168 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
169 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
170 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
172 return features & virtio_net_get_features(vdev);
175 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
177 VirtIONet *n = to_virtio_net(vdev);
178 #ifdef TAP_VNET_HDR
179 VLANClientState *host = n->vc->vlan->first_client;
180 #endif
182 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
184 #ifdef TAP_VNET_HDR
185 if (!tap_has_vnet_hdr(host) || !host->set_offload)
186 return;
188 host->set_offload(host,
189 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
190 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
191 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
192 (features >> VIRTIO_NET_F_GUEST_ECN) & 1);
193 #endif
196 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
197 VirtQueueElement *elem)
199 uint8_t on;
201 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
202 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
203 exit(1);
206 on = ldub_p(elem->out_sg[1].iov_base);
208 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
209 n->promisc = on;
210 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
211 n->allmulti = on;
212 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
213 n->alluni = on;
214 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
215 n->nomulti = on;
216 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
217 n->nouni = on;
218 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
219 n->nobcast = on;
220 else
221 return VIRTIO_NET_ERR;
223 return VIRTIO_NET_OK;
226 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
227 VirtQueueElement *elem)
229 struct virtio_net_ctrl_mac mac_data;
231 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
232 elem->out_sg[1].iov_len < sizeof(mac_data) ||
233 elem->out_sg[2].iov_len < sizeof(mac_data))
234 return VIRTIO_NET_ERR;
236 n->mac_table.in_use = 0;
237 n->mac_table.first_multi = 0;
238 n->mac_table.uni_overflow = 0;
239 n->mac_table.multi_overflow = 0;
240 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
242 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
244 if (sizeof(mac_data.entries) +
245 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
246 return VIRTIO_NET_ERR;
248 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
249 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
250 mac_data.entries * ETH_ALEN);
251 n->mac_table.in_use += mac_data.entries;
252 } else {
253 n->mac_table.uni_overflow = 1;
256 n->mac_table.first_multi = n->mac_table.in_use;
258 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
260 if (sizeof(mac_data.entries) +
261 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
262 return VIRTIO_NET_ERR;
264 if (mac_data.entries) {
265 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
266 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
267 elem->out_sg[2].iov_base + sizeof(mac_data),
268 mac_data.entries * ETH_ALEN);
269 n->mac_table.in_use += mac_data.entries;
270 } else {
271 n->mac_table.multi_overflow = 1;
275 return VIRTIO_NET_OK;
278 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
279 VirtQueueElement *elem)
281 uint16_t vid;
283 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
284 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
285 return VIRTIO_NET_ERR;
288 vid = lduw_le_p(elem->out_sg[1].iov_base);
290 if (vid >= MAX_VLAN)
291 return VIRTIO_NET_ERR;
293 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
294 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
295 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
296 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
297 else
298 return VIRTIO_NET_ERR;
300 return VIRTIO_NET_OK;
303 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
305 VirtIONet *n = to_virtio_net(vdev);
306 struct virtio_net_ctrl_hdr ctrl;
307 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
308 VirtQueueElement elem;
310 while (virtqueue_pop(vq, &elem)) {
311 if ((elem.in_num < 1) || (elem.out_num < 1)) {
312 fprintf(stderr, "virtio-net ctrl missing headers\n");
313 exit(1);
316 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
317 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
318 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
319 exit(1);
322 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
323 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
325 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
326 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
327 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
328 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
329 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
330 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
332 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
334 virtqueue_push(vq, &elem, sizeof(status));
335 virtio_notify(vdev, vq);
339 /* RX */
341 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
343 VirtIONet *n = to_virtio_net(vdev);
345 qemu_flush_queued_packets(n->vc);
347 /* We now have RX buffers, signal to the IO thread to break out of the
348 * select to re-poll the tap file descriptor */
349 qemu_notify_event();
352 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
354 if (!virtio_queue_ready(n->rx_vq) ||
355 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
356 return 0;
358 if (virtio_queue_empty(n->rx_vq) ||
359 (n->mergeable_rx_bufs &&
360 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
361 virtio_queue_set_notification(n->rx_vq, 1);
362 return 0;
365 virtio_queue_set_notification(n->rx_vq, 0);
366 return 1;
369 static int virtio_net_can_receive(VLANClientState *vc)
371 VirtIONet *n = vc->opaque;
373 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
376 #ifdef TAP_VNET_HDR
377 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
378 * it never finds out that the packets don't have valid checksums. This
379 * causes dhclient to get upset. Fedora's carried a patch for ages to
380 * fix this with Xen but it hasn't appeared in an upstream release of
381 * dhclient yet.
383 * To avoid breaking existing guests, we catch udp packets and add
384 * checksums. This is terrible but it's better than hacking the guest
385 * kernels.
387 * N.B. if we introduce a zero-copy API, this operation is no longer free so
388 * we should provide a mechanism to disable it to avoid polluting the host
389 * cache.
391 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
392 const uint8_t *buf, size_t size)
394 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
395 (size > 27 && size < 1500) && /* normal sized MTU */
396 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
397 (buf[23] == 17) && /* ip.protocol == UDP */
398 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
399 /* FIXME this cast is evil */
400 net_checksum_calculate((uint8_t *)buf, size);
401 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
404 #endif
406 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
408 int offset, i;
410 offset = i = 0;
411 while (offset < count && i < iovcnt) {
412 int len = MIN(iov[i].iov_len, count - offset);
413 memcpy(iov[i].iov_base, buf + offset, len);
414 offset += len;
415 i++;
418 return offset;
421 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
422 const void *buf, size_t size, size_t hdr_len, int raw)
424 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
425 int offset = 0;
427 hdr->flags = 0;
428 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
430 #ifdef TAP_VNET_HDR
431 if (tap_has_vnet_hdr(n->vc->vlan->first_client)) {
432 if (!raw) {
433 memcpy(hdr, buf, sizeof(*hdr));
434 } else {
435 memset(hdr, 0, sizeof(*hdr));
437 offset = sizeof(*hdr);
438 work_around_broken_dhclient(hdr, buf + offset, size - offset);
440 #endif
442 /* We only ever receive a struct virtio_net_hdr from the tapfd,
443 * but we may be passing along a larger header to the guest.
445 iov[0].iov_base += hdr_len;
446 iov[0].iov_len -= hdr_len;
448 return offset;
451 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
453 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
454 static const uint8_t vlan[] = {0x81, 0x00};
455 uint8_t *ptr = (uint8_t *)buf;
456 int i;
458 if (n->promisc)
459 return 1;
461 #ifdef TAP_VNET_HDR
462 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
463 ptr += sizeof(struct virtio_net_hdr);
464 #endif
466 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
467 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
468 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
469 return 0;
472 if (ptr[0] & 1) { // multicast
473 if (!memcmp(ptr, bcast, sizeof(bcast))) {
474 return !n->nobcast;
475 } else if (n->nomulti) {
476 return 0;
477 } else if (n->allmulti || n->mac_table.multi_overflow) {
478 return 1;
481 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
482 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
483 return 1;
486 } else { // unicast
487 if (n->nouni) {
488 return 0;
489 } else if (n->alluni || n->mac_table.uni_overflow) {
490 return 1;
491 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
492 return 1;
495 for (i = 0; i < n->mac_table.first_multi; i++) {
496 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
497 return 1;
502 return 0;
505 static ssize_t virtio_net_receive2(VLANClientState *vc, const uint8_t *buf, size_t size, int raw)
507 VirtIONet *n = vc->opaque;
508 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
509 size_t hdr_len, offset, i;
511 if (!do_virtio_net_can_receive(n, size))
512 return 0;
514 if (!receive_filter(n, buf, size))
515 return size;
517 /* hdr_len refers to the header we supply to the guest */
518 hdr_len = n->mergeable_rx_bufs ?
519 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
521 offset = i = 0;
523 while (offset < size) {
524 VirtQueueElement elem;
525 int len, total;
526 struct iovec sg[VIRTQUEUE_MAX_SIZE];
528 len = total = 0;
530 if ((i != 0 && !n->mergeable_rx_bufs) ||
531 virtqueue_pop(n->rx_vq, &elem) == 0) {
532 if (i == 0)
533 return -1;
534 fprintf(stderr, "virtio-net truncating packet\n");
535 exit(1);
538 if (elem.in_num < 1) {
539 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
540 exit(1);
543 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
544 fprintf(stderr, "virtio-net header not in first element\n");
545 exit(1);
548 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
550 if (i == 0) {
551 if (n->mergeable_rx_bufs)
552 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
554 offset += receive_header(n, sg, elem.in_num,
555 buf + offset, size - offset, hdr_len, raw);
556 total += hdr_len;
559 /* copy in packet. ugh */
560 len = iov_fill(sg, elem.in_num,
561 buf + offset, size - offset);
562 total += len;
564 /* signal other side */
565 virtqueue_fill(n->rx_vq, &elem, total, i++);
567 offset += len;
570 if (mhdr)
571 mhdr->num_buffers = i;
573 virtqueue_flush(n->rx_vq, i);
574 virtio_notify(&n->vdev, n->rx_vq);
576 return size;
579 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
581 static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
583 VirtIONet *n = vc->opaque;
585 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
586 virtio_notify(&n->vdev, n->tx_vq);
588 n->async_tx.elem.out_num = n->async_tx.len = 0;
590 virtio_queue_set_notification(n->tx_vq, 1);
591 virtio_net_flush_tx(n, n->tx_vq);
594 static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
596 return virtio_net_receive2(vc, buf, size, 0);
599 static ssize_t virtio_net_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
601 return virtio_net_receive2(vc, buf, size, 1);
604 /* TX */
605 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
607 VirtQueueElement elem;
608 #ifdef TAP_VNET_HDR
609 int has_vnet_hdr = tap_has_vnet_hdr(n->vc->vlan->first_client);
610 #else
611 int has_vnet_hdr = 0;
612 #endif
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 (!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->vc, 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);
713 #ifdef TAP_VNET_HDR
714 qemu_put_be32(f, tap_has_vnet_hdr(n->vc->vlan->first_client));
715 #else
716 qemu_put_be32(f, 0);
717 #endif
719 qemu_put_byte(f, n->mac_table.multi_overflow);
720 qemu_put_byte(f, n->mac_table.uni_overflow);
721 qemu_put_byte(f, n->alluni);
722 qemu_put_byte(f, n->nomulti);
723 qemu_put_byte(f, n->nouni);
724 qemu_put_byte(f, n->nobcast);
727 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
729 VirtIONet *n = opaque;
730 int i;
732 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
733 return -EINVAL;
735 virtio_load(&n->vdev, f);
737 qemu_get_buffer(f, n->mac, ETH_ALEN);
738 n->tx_timer_active = qemu_get_be32(f);
739 n->mergeable_rx_bufs = qemu_get_be32(f);
741 if (version_id >= 3)
742 n->status = qemu_get_be16(f);
744 if (version_id >= 4) {
745 if (version_id < 8) {
746 n->promisc = qemu_get_be32(f);
747 n->allmulti = qemu_get_be32(f);
748 } else {
749 n->promisc = qemu_get_byte(f);
750 n->allmulti = qemu_get_byte(f);
754 if (version_id >= 5) {
755 n->mac_table.in_use = qemu_get_be32(f);
756 /* MAC_TABLE_ENTRIES may be different from the saved image */
757 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
758 qemu_get_buffer(f, n->mac_table.macs,
759 n->mac_table.in_use * ETH_ALEN);
760 } else if (n->mac_table.in_use) {
761 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
762 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
763 n->mac_table.in_use = 0;
767 if (version_id >= 6)
768 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
770 if (version_id >= 7 && qemu_get_be32(f)) {
771 #ifdef TAP_VNET_HDR
772 tap_using_vnet_hdr(n->vc->vlan->first_client, 1);
773 #else
774 fprintf(stderr,
775 "virtio-net: saved image requires vnet header support\n");
776 exit(1);
777 #endif
780 if (version_id >= 9) {
781 n->mac_table.multi_overflow = qemu_get_byte(f);
782 n->mac_table.uni_overflow = qemu_get_byte(f);
785 if (version_id >= 10) {
786 n->alluni = qemu_get_byte(f);
787 n->nomulti = qemu_get_byte(f);
788 n->nouni = qemu_get_byte(f);
789 n->nobcast = qemu_get_byte(f);
792 /* Find the first multicast entry in the saved MAC filter */
793 for (i = 0; i < n->mac_table.in_use; i++) {
794 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
795 break;
798 n->mac_table.first_multi = i;
800 if (n->tx_timer_active) {
801 qemu_mod_timer(n->tx_timer,
802 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
805 return 0;
808 static void virtio_net_cleanup(VLANClientState *vc)
810 VirtIONet *n = vc->opaque;
812 qemu_purge_queued_packets(vc);
814 unregister_savevm("virtio-net", n);
816 qemu_free(n->mac_table.macs);
817 qemu_free(n->vlans);
819 qemu_del_timer(n->tx_timer);
820 qemu_free_timer(n->tx_timer);
822 virtio_cleanup(&n->vdev);
825 VirtIODevice *virtio_net_init(DeviceState *dev)
827 VirtIONet *n;
828 static int virtio_net_id;
830 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
831 sizeof(struct virtio_net_config),
832 sizeof(VirtIONet));
834 n->vdev.get_config = virtio_net_get_config;
835 n->vdev.set_config = virtio_net_set_config;
836 n->vdev.get_features = virtio_net_get_features;
837 n->vdev.set_features = virtio_net_set_features;
838 n->vdev.bad_features = virtio_net_bad_features;
839 n->vdev.reset = virtio_net_reset;
840 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
841 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
842 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
843 qdev_get_macaddr(dev, n->mac);
844 n->status = VIRTIO_NET_S_LINK_UP;
845 n->vc = qdev_get_vlan_client(dev,
846 virtio_net_can_receive,
847 virtio_net_receive, NULL,
848 virtio_net_cleanup, n);
849 n->vc->link_status_changed = virtio_net_set_link_status;
850 n->vc->receive_raw = virtio_net_receive_raw;
852 qemu_format_nic_info_str(n->vc, n->mac);
854 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
855 n->tx_timer_active = 0;
856 n->mergeable_rx_bufs = 0;
857 n->promisc = 1; /* for compatibility */
859 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
861 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
862 if (dev->nd->nvectors == NIC_NVECTORS_UNSPECIFIED)
863 n->vdev.nvectors = 3;
864 else
865 n->vdev.nvectors = dev->nd->nvectors;
867 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
868 virtio_net_save, virtio_net_load, n);
870 return &n->vdev;