virtio-net: Re-instate GSO code removed upstream
[qemu-kvm/fedora.git] / hw / virtio-net.c
blobaaab83b0b4d20870231ac8cc6dbbb07ab36c310f
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 USE_KVM
19 #include "qemu-kvm.h"
20 #endif
22 #define TAP_VNET_HDR
24 /* Version 7 has TAP_VNET_HDR support. This is reserved in upstream QEMU to
25 * avoid future conflict.
26 * We can't assume verisons > 7 have TAP_VNET_HDR support until this is merged
27 * in upstream QEMU.
29 #define VIRTIO_NET_VM_VERSION 7
31 #define MAC_TABLE_ENTRIES 32
32 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
34 typedef struct VirtIONet
36 VirtIODevice vdev;
37 uint8_t mac[ETH_ALEN];
38 uint16_t status;
39 VirtQueue *rx_vq;
40 VirtQueue *tx_vq;
41 VirtQueue *ctrl_vq;
42 VLANClientState *vc;
43 QEMUTimer *tx_timer;
44 int tx_timer_active;
45 int mergeable_rx_bufs;
46 int promisc;
47 int allmulti;
48 struct {
49 int in_use;
50 uint8_t *macs;
51 } mac_table;
52 uint32_t *vlans;
53 } VirtIONet;
55 /* TODO
56 * - we could suppress RX interrupt if we were so inclined.
59 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
61 return (VirtIONet *)vdev;
64 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
66 VirtIONet *n = to_virtio_net(vdev);
67 struct virtio_net_config netcfg;
69 netcfg.status = n->status;
70 memcpy(netcfg.mac, n->mac, ETH_ALEN);
71 memcpy(config, &netcfg, sizeof(netcfg));
74 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
76 VirtIONet *n = to_virtio_net(vdev);
77 struct virtio_net_config netcfg;
79 memcpy(&netcfg, config, sizeof(netcfg));
81 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
82 memcpy(n->mac, netcfg.mac, ETH_ALEN);
83 qemu_format_nic_info_str(n->vc, n->mac);
87 static void virtio_net_set_link_status(VLANClientState *vc)
89 VirtIONet *n = vc->opaque;
90 uint16_t old_status = n->status;
92 if (vc->link_down)
93 n->status &= ~VIRTIO_NET_S_LINK_UP;
94 else
95 n->status |= VIRTIO_NET_S_LINK_UP;
97 if (n->status != old_status)
98 virtio_notify_config(&n->vdev);
101 static void virtio_net_reset(VirtIODevice *vdev)
103 VirtIONet *n = to_virtio_net(vdev);
105 /* Reset back to compatibility mode */
106 n->promisc = 1;
107 n->allmulti = 0;
109 /* Flush any MAC and VLAN filter table state */
110 n->mac_table.in_use = 0;
111 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
112 memset(n->vlans, 0, MAX_VLAN >> 3);
115 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
117 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
118 (1 << VIRTIO_NET_F_STATUS) |
119 (1 << VIRTIO_NET_F_CTRL_VQ) |
120 (1 << VIRTIO_NET_F_CTRL_RX) |
121 (1 << VIRTIO_NET_F_CTRL_VLAN);
122 #ifdef TAP_VNET_HDR
123 VirtIONet *n = to_virtio_net(vdev);
124 VLANClientState *host = n->vc->vlan->first_client;
126 if (tap_has_vnet_hdr(host)) {
127 tap_using_vnet_hdr(host, 1);
128 features |= (1 << VIRTIO_NET_F_CSUM);
129 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
130 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
131 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
132 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
133 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
134 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
135 features |= (1 << VIRTIO_NET_F_HOST_ECN);
136 features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
137 /* Kernel can't actually handle UFO in software currently. */
139 #endif
141 return features;
144 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
146 uint32_t features = 0;
148 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
149 * but also these: */
150 features |= (1 << VIRTIO_NET_F_MAC);
151 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
152 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
153 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
154 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
156 return features & virtio_net_get_features(vdev);
159 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
161 VirtIONet *n = to_virtio_net(vdev);
162 #ifdef TAP_VNET_HDR
163 VLANClientState *host = n->vc->vlan->first_client;
164 #endif
166 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
168 #ifdef TAP_VNET_HDR
169 if (!tap_has_vnet_hdr(host) || !host->set_offload)
170 return;
172 host->set_offload(host,
173 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
174 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
175 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
176 (features >> VIRTIO_NET_F_GUEST_ECN) & 1);
177 #endif
180 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
181 VirtQueueElement *elem)
183 uint8_t on;
185 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
186 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
187 exit(1);
190 on = ldub_p(elem->out_sg[1].iov_base);
192 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
193 n->promisc = on;
194 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
195 n->allmulti = on;
196 else
197 return VIRTIO_NET_ERR;
199 return VIRTIO_NET_OK;
202 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
203 VirtQueueElement *elem)
205 struct virtio_net_ctrl_mac mac_data;
207 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
208 elem->out_sg[1].iov_len < sizeof(mac_data) ||
209 elem->out_sg[2].iov_len < sizeof(mac_data))
210 return VIRTIO_NET_ERR;
212 n->mac_table.in_use = 0;
213 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
215 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
217 if (sizeof(mac_data.entries) +
218 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
219 return VIRTIO_NET_ERR;
221 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
222 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
223 mac_data.entries * ETH_ALEN);
224 n->mac_table.in_use += mac_data.entries;
225 } else {
226 n->promisc = 1;
227 return VIRTIO_NET_OK;
230 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
232 if (sizeof(mac_data.entries) +
233 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
234 return VIRTIO_NET_ERR;
236 if (mac_data.entries) {
237 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
238 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
239 elem->out_sg[2].iov_base + sizeof(mac_data),
240 mac_data.entries * ETH_ALEN);
241 n->mac_table.in_use += mac_data.entries;
242 } else
243 n->allmulti = 1;
246 return VIRTIO_NET_OK;
249 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
250 VirtQueueElement *elem)
252 uint16_t vid;
254 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
255 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
256 return VIRTIO_NET_ERR;
259 vid = lduw_le_p(elem->out_sg[1].iov_base);
261 if (vid >= MAX_VLAN)
262 return VIRTIO_NET_ERR;
264 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
265 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
266 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
267 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
268 else
269 return VIRTIO_NET_ERR;
271 return VIRTIO_NET_OK;
274 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
276 VirtIONet *n = to_virtio_net(vdev);
277 struct virtio_net_ctrl_hdr ctrl;
278 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
279 VirtQueueElement elem;
281 while (virtqueue_pop(vq, &elem)) {
282 if ((elem.in_num < 1) || (elem.out_num < 1)) {
283 fprintf(stderr, "virtio-net ctrl missing headers\n");
284 exit(1);
287 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
288 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
289 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
290 exit(1);
293 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
294 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
296 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
297 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
298 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
299 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
300 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
301 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
303 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
305 virtqueue_push(vq, &elem, sizeof(status));
306 virtio_notify(vdev, vq);
310 /* RX */
312 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
314 #ifdef USE_KVM
315 /* We now have RX buffers, signal to the IO thread to break out of the
316 select to re-poll the tap file descriptor */
317 if (kvm_enabled())
318 qemu_kvm_notify_work();
319 #endif
322 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
324 if (!virtio_queue_ready(n->rx_vq) ||
325 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
326 return 0;
328 if (virtio_queue_empty(n->rx_vq) ||
329 (n->mergeable_rx_bufs &&
330 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
331 virtio_queue_set_notification(n->rx_vq, 1);
332 return 0;
335 virtio_queue_set_notification(n->rx_vq, 0);
336 return 1;
339 static int virtio_net_can_receive(void *opaque)
341 VirtIONet *n = opaque;
343 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
346 #ifdef TAP_VNET_HDR
347 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
348 * it never finds out that the packets don't have valid checksums. This
349 * causes dhclient to get upset. Fedora's carried a patch for ages to
350 * fix this with Xen but it hasn't appeared in an upstream release of
351 * dhclient yet.
353 * To avoid breaking existing guests, we catch udp packets and add
354 * checksums. This is terrible but it's better than hacking the guest
355 * kernels.
357 * N.B. if we introduce a zero-copy API, this operation is no longer free so
358 * we should provide a mechanism to disable it to avoid polluting the host
359 * cache.
361 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
362 const uint8_t *buf, size_t size)
364 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
365 (size > 27 && size < 1500) && /* normal sized MTU */
366 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
367 (buf[23] == 17) && /* ip.protocol == UDP */
368 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
369 /* FIXME this cast is evil */
370 net_checksum_calculate((uint8_t *)buf, size);
371 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
374 #endif
376 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
378 int offset, i;
380 offset = i = 0;
381 while (offset < count && i < iovcnt) {
382 int len = MIN(iov[i].iov_len, count - offset);
383 memcpy(iov[i].iov_base, buf + offset, len);
384 offset += len;
385 i++;
388 return offset;
391 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
392 const void *buf, size_t size, size_t hdr_len)
394 struct virtio_net_hdr *hdr = iov[0].iov_base;
395 int offset = 0;
397 hdr->flags = 0;
398 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
400 #ifdef TAP_VNET_HDR
401 if (tap_has_vnet_hdr(n->vc->vlan->first_client)) {
402 memcpy(hdr, buf, sizeof(*hdr));
403 offset = sizeof(*hdr);
404 work_around_broken_dhclient(hdr, buf + offset, size - offset);
406 #endif
408 /* We only ever receive a struct virtio_net_hdr from the tapfd,
409 * but we may be passing along a larger header to the guest.
411 iov[0].iov_base += hdr_len;
412 iov[0].iov_len -= hdr_len;
414 return offset;
417 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
419 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
420 static const uint8_t vlan[] = {0x81, 0x00};
421 uint8_t *ptr = (uint8_t *)buf;
422 int i;
424 if (n->promisc)
425 return 1;
427 #ifdef TAP_VNET_HDR
428 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
429 ptr += sizeof(struct virtio_net_hdr);
430 #endif
432 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
433 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
434 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
435 return 0;
438 if ((ptr[0] & 1) && n->allmulti)
439 return 1;
441 if (!memcmp(ptr, bcast, sizeof(bcast)))
442 return 1;
444 if (!memcmp(ptr, n->mac, ETH_ALEN))
445 return 1;
447 for (i = 0; i < n->mac_table.in_use; i++) {
448 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
449 return 1;
452 return 0;
455 static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
457 VirtIONet *n = opaque;
458 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
459 size_t hdr_len, offset, i;
461 if (!do_virtio_net_can_receive(n, size))
462 return;
464 if (!receive_filter(n, buf, size))
465 return;
467 /* hdr_len refers to the header we supply to the guest */
468 hdr_len = n->mergeable_rx_bufs ?
469 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
471 offset = i = 0;
473 while (offset < size) {
474 VirtQueueElement elem;
475 int len, total;
476 struct iovec sg[VIRTQUEUE_MAX_SIZE];
478 len = total = 0;
480 if ((i != 0 && !n->mergeable_rx_bufs) ||
481 virtqueue_pop(n->rx_vq, &elem) == 0) {
482 if (i == 0)
483 return;
484 fprintf(stderr, "virtio-net truncating packet\n");
485 exit(1);
488 if (elem.in_num < 1) {
489 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
490 exit(1);
493 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
494 fprintf(stderr, "virtio-net header not in first element\n");
495 exit(1);
498 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
500 if (i == 0) {
501 if (n->mergeable_rx_bufs)
502 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
504 offset += receive_header(n, sg, elem.in_num,
505 buf + offset, size - offset, hdr_len);
506 total += hdr_len;
509 /* copy in packet. ugh */
510 len = iov_fill(sg, elem.in_num,
511 buf + offset, size - offset);
512 total += len;
514 /* signal other side */
515 virtqueue_fill(n->rx_vq, &elem, total, i++);
517 offset += len;
520 if (mhdr)
521 mhdr->num_buffers = i;
523 virtqueue_flush(n->rx_vq, i);
524 virtio_notify(&n->vdev, n->rx_vq);
527 /* TX */
528 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
530 VirtQueueElement elem;
531 #ifdef TAP_VNET_HDR
532 int has_vnet_hdr = tap_has_vnet_hdr(n->vc->vlan->first_client);
533 #else
534 int has_vnet_hdr = 0;
535 #endif
537 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
538 return;
540 while (virtqueue_pop(vq, &elem)) {
541 ssize_t len = 0;
542 unsigned int out_num = elem.out_num;
543 struct iovec *out_sg = &elem.out_sg[0];
544 unsigned hdr_len;
546 /* hdr_len refers to the header received from the guest */
547 hdr_len = n->mergeable_rx_bufs ?
548 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
549 sizeof(struct virtio_net_hdr);
551 if (out_num < 1 || out_sg->iov_len != hdr_len) {
552 fprintf(stderr, "virtio-net header not in first element\n");
553 exit(1);
556 /* ignore the header if GSO is not supported */
557 if (!has_vnet_hdr) {
558 out_num--;
559 out_sg++;
560 len += hdr_len;
561 } else if (n->mergeable_rx_bufs) {
562 /* tapfd expects a struct virtio_net_hdr */
563 hdr_len -= sizeof(struct virtio_net_hdr);
564 out_sg->iov_len -= hdr_len;
565 len += hdr_len;
568 len += qemu_sendv_packet(n->vc, out_sg, out_num);
570 virtqueue_push(vq, &elem, len);
571 virtio_notify(&n->vdev, vq);
575 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
577 VirtIONet *n = to_virtio_net(vdev);
579 if (n->tx_timer_active) {
580 virtio_queue_set_notification(vq, 1);
581 qemu_del_timer(n->tx_timer);
582 n->tx_timer_active = 0;
583 virtio_net_flush_tx(n, vq);
584 } else {
585 qemu_mod_timer(n->tx_timer,
586 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
587 n->tx_timer_active = 1;
588 virtio_queue_set_notification(vq, 0);
592 static void virtio_net_tx_timer(void *opaque)
594 VirtIONet *n = opaque;
596 n->tx_timer_active = 0;
598 /* Just in case the driver is not ready on more */
599 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
600 return;
602 virtio_queue_set_notification(n->tx_vq, 1);
603 virtio_net_flush_tx(n, n->tx_vq);
606 static void virtio_net_save(QEMUFile *f, void *opaque)
608 VirtIONet *n = opaque;
610 virtio_save(&n->vdev, f);
612 qemu_put_buffer(f, n->mac, ETH_ALEN);
613 qemu_put_be32(f, n->tx_timer_active);
614 qemu_put_be32(f, n->mergeable_rx_bufs);
615 qemu_put_be16(f, n->status);
616 qemu_put_be32(f, n->promisc);
617 qemu_put_be32(f, n->allmulti);
618 qemu_put_be32(f, n->mac_table.in_use);
619 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
620 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
622 #ifdef TAP_VNET_HDR
623 qemu_put_be32(f, tap_has_vnet_hdr(n->vc->vlan->first_client));
624 #else
625 qemu_put_be32(f, 0);
626 #endif
629 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
631 VirtIONet *n = opaque;
633 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
634 return -EINVAL;
636 virtio_load(&n->vdev, f);
638 qemu_get_buffer(f, n->mac, ETH_ALEN);
639 n->tx_timer_active = qemu_get_be32(f);
640 n->mergeable_rx_bufs = qemu_get_be32(f);
642 if (version_id >= 3)
643 n->status = qemu_get_be16(f);
645 if (version_id >= 4) {
646 n->promisc = qemu_get_be32(f);
647 n->allmulti = qemu_get_be32(f);
650 if (version_id >= 5) {
651 n->mac_table.in_use = qemu_get_be32(f);
652 /* MAC_TABLE_ENTRIES may be different from the saved image */
653 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
654 qemu_get_buffer(f, n->mac_table.macs,
655 n->mac_table.in_use * ETH_ALEN);
656 } else if (n->mac_table.in_use) {
657 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
658 n->promisc = 1;
659 n->mac_table.in_use = 0;
663 if (version_id >= 6)
664 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
666 if (version_id == 7 && qemu_get_be32(f)) {
667 #ifdef TAP_VNET_HDR
668 tap_using_vnet_hdr(n->vc->vlan->first_client, 1);
669 #else
670 fprintf(stderr,
671 "virtio-net: saved image requires vnet header support\n");
672 exit(1);
673 #endif
676 if (n->tx_timer_active) {
677 qemu_mod_timer(n->tx_timer,
678 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
681 return 0;
684 static void virtio_net_cleanup(VLANClientState *vc)
686 VirtIONet *n = vc->opaque;
688 unregister_savevm("virtio-net", n);
690 qemu_free(n->mac_table.macs);
691 qemu_free(n->vlans);
693 qemu_del_timer(n->tx_timer);
694 qemu_free_timer(n->tx_timer);
696 virtio_cleanup(&n->vdev);
699 PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
701 VirtIONet *n;
702 static int virtio_net_id;
704 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
705 PCI_VENDOR_ID_REDHAT_QUMRANET,
706 PCI_DEVICE_ID_VIRTIO_NET,
707 PCI_VENDOR_ID_REDHAT_QUMRANET,
708 VIRTIO_ID_NET,
709 PCI_CLASS_NETWORK_ETHERNET, 0x00,
710 sizeof(struct virtio_net_config),
711 sizeof(VirtIONet));
712 if (!n)
713 return NULL;
715 n->vdev.get_config = virtio_net_get_config;
716 n->vdev.set_config = virtio_net_set_config;
717 n->vdev.get_features = virtio_net_get_features;
718 n->vdev.set_features = virtio_net_set_features;
719 n->vdev.bad_features = virtio_net_bad_features;
720 n->vdev.reset = virtio_net_reset;
721 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
722 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
723 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
724 memcpy(n->mac, nd->macaddr, ETH_ALEN);
725 n->status = VIRTIO_NET_S_LINK_UP;
726 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
727 virtio_net_receive,
728 virtio_net_can_receive,
729 virtio_net_cleanup, n);
730 n->vc->link_status_changed = virtio_net_set_link_status;
732 qemu_format_nic_info_str(n->vc, n->mac);
734 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
735 n->tx_timer_active = 0;
736 n->mergeable_rx_bufs = 0;
737 n->promisc = 1; /* for compatibility */
739 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
741 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
743 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
744 virtio_net_save, virtio_net_load, n);
745 return (PCIDevice *)n;