Merge branch 'qemu-cvs'
[kvm-userspace.git] / qemu / hw / virtio-net.c
blobf125edca0dbdd3f46a0f20cd67586fa2f31699e6
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 #define VIRTIO_NET_VM_VERSION 6
26 #define MAC_TABLE_ENTRIES 32
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 int mergeable_rx_bufs;
41 int promisc;
42 int allmulti;
43 struct {
44 int in_use;
45 uint8_t *macs;
46 } mac_table;
47 uint32_t *vlans;
48 } VirtIONet;
50 /* TODO
51 * - we could suppress RX interrupt if we were so inclined.
54 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
56 return (VirtIONet *)vdev;
59 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
61 VirtIONet *n = to_virtio_net(vdev);
62 struct virtio_net_config netcfg;
64 netcfg.status = n->status;
65 memcpy(netcfg.mac, n->mac, ETH_ALEN);
66 memcpy(config, &netcfg, sizeof(netcfg));
69 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
71 VirtIONet *n = to_virtio_net(vdev);
72 struct virtio_net_config netcfg;
74 memcpy(&netcfg, config, sizeof(netcfg));
76 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
77 memcpy(n->mac, netcfg.mac, ETH_ALEN);
78 qemu_format_nic_info_str(n->vc, n->mac);
82 static void virtio_net_set_link_status(VLANClientState *vc)
84 VirtIONet *n = vc->opaque;
85 uint16_t old_status = n->status;
87 if (vc->link_down)
88 n->status &= ~VIRTIO_NET_S_LINK_UP;
89 else
90 n->status |= VIRTIO_NET_S_LINK_UP;
92 if (n->status != old_status)
93 virtio_notify_config(&n->vdev);
96 static void virtio_net_reset(VirtIODevice *vdev)
98 VirtIONet *n = to_virtio_net(vdev);
100 /* Reset back to compatibility mode */
101 n->promisc = 1;
102 n->allmulti = 0;
104 /* Flush any MAC and VLAN filter table state */
105 n->mac_table.in_use = 0;
106 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
107 memset(n->vlans, 0, MAX_VLAN >> 3);
110 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
112 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
113 (1 << VIRTIO_NET_F_STATUS) |
114 (1 << VIRTIO_NET_F_CTRL_VQ) |
115 (1 << VIRTIO_NET_F_CTRL_RX) |
116 (1 << VIRTIO_NET_F_CTRL_VLAN);
117 #ifdef TAP_VNET_HDR
118 VirtIONet *n = to_virtio_net(vdev);
119 VLANClientState *host = n->vc->vlan->first_client;
121 if (tap_has_vnet_hdr(host)) {
122 tap_using_vnet_hdr(host, 1);
123 features |= (1 << VIRTIO_NET_F_CSUM);
124 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
125 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
126 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
127 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
128 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
129 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
130 features |= (1 << VIRTIO_NET_F_HOST_ECN);
131 features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
132 /* Kernel can't actually handle UFO in software currently. */
134 #endif
136 return features;
139 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
141 VirtIONet *n = to_virtio_net(vdev);
142 #ifdef TAP_VNET_HDR
143 VLANClientState *host = n->vc->vlan->first_client;
144 #endif
146 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
148 #ifdef TAP_VNET_HDR
149 if (!tap_has_vnet_hdr(host) || !host->set_offload)
150 return;
152 host->set_offload(host,
153 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
154 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
155 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
156 (features >> VIRTIO_NET_F_GUEST_ECN) & 1);
157 #endif
160 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
161 VirtQueueElement *elem)
163 uint8_t on;
165 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
166 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
167 exit(1);
170 on = ldub_p(elem->out_sg[1].iov_base);
172 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
173 n->promisc = on;
174 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
175 n->allmulti = on;
176 else
177 return VIRTIO_NET_ERR;
179 return VIRTIO_NET_OK;
182 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
183 VirtQueueElement *elem)
185 struct virtio_net_ctrl_mac mac_data;
187 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
188 elem->out_sg[1].iov_len < sizeof(mac_data) ||
189 elem->out_sg[2].iov_len < sizeof(mac_data))
190 return VIRTIO_NET_ERR;
192 n->mac_table.in_use = 0;
193 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
195 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
197 if (sizeof(mac_data.entries) +
198 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
199 return VIRTIO_NET_ERR;
201 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
202 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
203 mac_data.entries * ETH_ALEN);
204 n->mac_table.in_use += mac_data.entries;
205 } else {
206 n->promisc = 1;
207 return VIRTIO_NET_OK;
210 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
212 if (sizeof(mac_data.entries) +
213 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
214 return VIRTIO_NET_ERR;
216 if (mac_data.entries) {
217 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
218 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
219 elem->out_sg[2].iov_base + sizeof(mac_data),
220 mac_data.entries * ETH_ALEN);
221 n->mac_table.in_use += mac_data.entries;
222 } else
223 n->allmulti = 1;
226 return VIRTIO_NET_OK;
229 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
230 VirtQueueElement *elem)
232 uint16_t vid;
234 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
235 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
236 return VIRTIO_NET_ERR;
239 vid = lduw_le_p(elem->out_sg[1].iov_base);
241 if (vid >= MAX_VLAN)
242 return VIRTIO_NET_ERR;
244 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
245 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
246 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
247 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
248 else
249 return VIRTIO_NET_ERR;
251 return VIRTIO_NET_OK;
254 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
256 VirtIONet *n = to_virtio_net(vdev);
257 struct virtio_net_ctrl_hdr ctrl;
258 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
259 VirtQueueElement elem;
261 while (virtqueue_pop(vq, &elem)) {
262 if ((elem.in_num < 1) || (elem.out_num < 1)) {
263 fprintf(stderr, "virtio-net ctrl missing headers\n");
264 exit(1);
267 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
268 elem.out_sg[elem.in_num - 1].iov_len < sizeof(status)) {
269 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
270 exit(1);
273 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
274 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
276 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
277 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
278 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
279 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
280 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
281 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
283 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
285 virtqueue_push(vq, &elem, sizeof(status));
286 virtio_notify(vdev, vq);
290 /* RX */
292 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
294 #ifdef USE_KVM
295 /* We now have RX buffers, signal to the IO thread to break out of the
296 select to re-poll the tap file descriptor */
297 if (kvm_enabled())
298 qemu_kvm_notify_work();
299 #endif
302 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
304 if (!virtio_queue_ready(n->rx_vq) ||
305 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
306 return 0;
308 if (virtio_queue_empty(n->rx_vq) ||
309 (n->mergeable_rx_bufs &&
310 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
311 virtio_queue_set_notification(n->rx_vq, 1);
312 return 0;
315 virtio_queue_set_notification(n->rx_vq, 0);
316 return 1;
319 static int virtio_net_can_receive(void *opaque)
321 VirtIONet *n = opaque;
323 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
326 #ifdef TAP_VNET_HDR
327 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
328 * it never finds out that the packets don't have valid checksums. This
329 * causes dhclient to get upset. Fedora's carried a patch for ages to
330 * fix this with Xen but it hasn't appeared in an upstream release of
331 * dhclient yet.
333 * To avoid breaking existing guests, we catch udp packets and add
334 * checksums. This is terrible but it's better than hacking the guest
335 * kernels.
337 * N.B. if we introduce a zero-copy API, this operation is no longer free so
338 * we should provide a mechanism to disable it to avoid polluting the host
339 * cache.
341 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
342 const uint8_t *buf, size_t size)
344 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
345 (size > 27 && size < 1500) && /* normal sized MTU */
346 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
347 (buf[23] == 17) && /* ip.protocol == UDP */
348 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
349 /* FIXME this cast is evil */
350 net_checksum_calculate((uint8_t *)buf, size);
351 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
354 #endif
356 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
358 int offset, i;
360 offset = i = 0;
361 while (offset < count && i < iovcnt) {
362 int len = MIN(iov[i].iov_len, count - offset);
363 memcpy(iov[i].iov_base, buf + offset, len);
364 offset += len;
365 i++;
368 return offset;
371 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
372 const void *buf, size_t size, size_t hdr_len)
374 struct virtio_net_hdr *hdr = iov[0].iov_base;
375 int offset = 0;
377 hdr->flags = 0;
378 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
380 #ifdef TAP_VNET_HDR
381 if (tap_has_vnet_hdr(n->vc->vlan->first_client)) {
382 memcpy(hdr, buf, sizeof(*hdr));
383 offset = sizeof(*hdr);
384 work_around_broken_dhclient(hdr, buf + offset, size - offset);
386 #endif
388 /* We only ever receive a struct virtio_net_hdr from the tapfd,
389 * but we may be passing along a larger header to the guest.
391 iov[0].iov_base += hdr_len;
392 iov[0].iov_len -= hdr_len;
394 return offset;
397 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
399 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
400 static const uint8_t vlan[] = {0x81, 0x00};
401 uint8_t *ptr = (uint8_t *)buf;
402 int i;
404 if (n->promisc)
405 return 1;
407 #ifdef TAP_VNET_HDR
408 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
409 ptr += sizeof(struct virtio_net_hdr);
410 #endif
412 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
413 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
414 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
415 return 0;
418 if ((ptr[0] & 1) && n->allmulti)
419 return 1;
421 if (!memcmp(ptr, bcast, sizeof(bcast)))
422 return 1;
424 if (!memcmp(ptr, n->mac, ETH_ALEN))
425 return 1;
427 for (i = 0; i < n->mac_table.in_use; i++) {
428 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
429 return 1;
432 return 0;
435 static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
437 VirtIONet *n = opaque;
438 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
439 size_t hdr_len, offset, i;
441 if (!do_virtio_net_can_receive(n, size))
442 return;
444 if (!receive_filter(n, buf, size))
445 return;
447 /* hdr_len refers to the header we supply to the guest */
448 hdr_len = n->mergeable_rx_bufs ?
449 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
451 offset = i = 0;
453 while (offset < size) {
454 VirtQueueElement elem;
455 int len, total;
456 struct iovec sg[VIRTQUEUE_MAX_SIZE];
458 len = total = 0;
460 if ((i != 0 && !n->mergeable_rx_bufs) ||
461 virtqueue_pop(n->rx_vq, &elem) == 0) {
462 if (i == 0)
463 return;
464 fprintf(stderr, "virtio-net truncating packet\n");
465 exit(1);
468 if (elem.in_num < 1) {
469 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
470 exit(1);
473 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
474 fprintf(stderr, "virtio-net header not in first element\n");
475 exit(1);
478 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
480 if (i == 0) {
481 if (n->mergeable_rx_bufs)
482 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
484 offset += receive_header(n, sg, elem.in_num,
485 buf + offset, size - offset, hdr_len);
486 total += hdr_len;
489 /* copy in packet. ugh */
490 len = iov_fill(sg, elem.in_num,
491 buf + offset, size - offset);
492 total += len;
494 /* signal other side */
495 virtqueue_fill(n->rx_vq, &elem, total, i++);
497 offset += len;
500 if (mhdr)
501 mhdr->num_buffers = i;
503 virtqueue_flush(n->rx_vq, i);
504 virtio_notify(&n->vdev, n->rx_vq);
507 /* TX */
508 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
510 VirtQueueElement elem;
511 #ifdef TAP_VNET_HDR
512 int has_vnet_hdr = tap_has_vnet_hdr(n->vc->vlan->first_client);
513 #else
514 int has_vnet_hdr = 0;
515 #endif
517 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
518 return;
520 while (virtqueue_pop(vq, &elem)) {
521 ssize_t len = 0;
522 unsigned int out_num = elem.out_num;
523 struct iovec *out_sg = &elem.out_sg[0];
524 unsigned hdr_len;
526 /* hdr_len refers to the header received from the guest */
527 hdr_len = n->mergeable_rx_bufs ?
528 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
529 sizeof(struct virtio_net_hdr);
531 if (out_num < 1 || out_sg->iov_len != hdr_len) {
532 fprintf(stderr, "virtio-net header not in first element\n");
533 exit(1);
536 /* ignore the header if GSO is not supported */
537 if (!has_vnet_hdr) {
538 out_num--;
539 out_sg++;
540 len += hdr_len;
541 } else if (n->mergeable_rx_bufs) {
542 /* tapfd expects a struct virtio_net_hdr */
543 hdr_len -= sizeof(struct virtio_net_hdr);
544 out_sg->iov_len -= hdr_len;
545 len += hdr_len;
548 len += qemu_sendv_packet(n->vc, out_sg, out_num);
550 virtqueue_push(vq, &elem, len);
551 virtio_notify(&n->vdev, vq);
555 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
557 VirtIONet *n = to_virtio_net(vdev);
559 if (n->tx_timer_active) {
560 virtio_queue_set_notification(vq, 1);
561 qemu_del_timer(n->tx_timer);
562 n->tx_timer_active = 0;
563 virtio_net_flush_tx(n, vq);
564 } else {
565 qemu_mod_timer(n->tx_timer,
566 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
567 n->tx_timer_active = 1;
568 virtio_queue_set_notification(vq, 0);
572 static void virtio_net_tx_timer(void *opaque)
574 VirtIONet *n = opaque;
576 n->tx_timer_active = 0;
578 /* Just in case the driver is not ready on more */
579 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
580 return;
582 virtio_queue_set_notification(n->tx_vq, 1);
583 virtio_net_flush_tx(n, n->tx_vq);
586 static void virtio_net_save(QEMUFile *f, void *opaque)
588 VirtIONet *n = opaque;
590 virtio_save(&n->vdev, f);
592 qemu_put_buffer(f, n->mac, ETH_ALEN);
593 qemu_put_be32(f, n->tx_timer_active);
594 qemu_put_be32(f, n->mergeable_rx_bufs);
595 qemu_put_be16(f, n->status);
596 qemu_put_be32(f, n->promisc);
597 qemu_put_be32(f, n->allmulti);
598 qemu_put_be32(f, n->mac_table.in_use);
599 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
600 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
602 #ifdef TAP_VNET_HDR
603 qemu_put_be32(f, tap_has_vnet_hdr(n->vc->vlan->first_client));
604 #endif
607 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
609 VirtIONet *n = opaque;
611 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
612 return -EINVAL;
614 virtio_load(&n->vdev, f);
616 qemu_get_buffer(f, n->mac, ETH_ALEN);
617 n->tx_timer_active = qemu_get_be32(f);
618 n->mergeable_rx_bufs = qemu_get_be32(f);
620 if (version_id >= 3)
621 n->status = qemu_get_be16(f);
623 if (version_id >= 4) {
624 n->promisc = qemu_get_be32(f);
625 n->allmulti = qemu_get_be32(f);
628 if (version_id >= 5) {
629 n->mac_table.in_use = qemu_get_be32(f);
630 /* MAC_TABLE_ENTRIES may be different from the saved image */
631 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
632 qemu_get_buffer(f, n->mac_table.macs,
633 n->mac_table.in_use * ETH_ALEN);
634 } else if (n->mac_table.in_use) {
635 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
636 n->promisc = 1;
637 n->mac_table.in_use = 0;
641 if (version_id >= 6)
642 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
644 #ifdef TAP_VNET_HDR
645 if (qemu_get_be32(f))
646 tap_using_vnet_hdr(n->vc->vlan->first_client, 1);
647 #endif
649 if (n->tx_timer_active) {
650 qemu_mod_timer(n->tx_timer,
651 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
654 return 0;
657 PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
659 VirtIONet *n;
660 static int virtio_net_id;
662 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
663 PCI_VENDOR_ID_REDHAT_QUMRANET,
664 PCI_DEVICE_ID_VIRTIO_NET,
665 PCI_VENDOR_ID_REDHAT_QUMRANET,
666 VIRTIO_ID_NET,
667 PCI_CLASS_NETWORK_ETHERNET, 0x00,
668 sizeof(struct virtio_net_config),
669 sizeof(VirtIONet));
670 if (!n)
671 return NULL;
673 n->vdev.get_config = virtio_net_get_config;
674 n->vdev.set_config = virtio_net_set_config;
675 n->vdev.get_features = virtio_net_get_features;
676 n->vdev.set_features = virtio_net_set_features;
677 n->vdev.reset = virtio_net_reset;
678 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
679 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
680 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
681 memcpy(n->mac, nd->macaddr, ETH_ALEN);
682 n->status = VIRTIO_NET_S_LINK_UP;
683 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
684 virtio_net_receive, virtio_net_can_receive, n);
685 n->vc->link_status_changed = virtio_net_set_link_status;
687 qemu_format_nic_info_str(n->vc, n->mac);
689 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
690 n->tx_timer_active = 0;
691 n->mergeable_rx_bufs = 0;
692 n->promisc = 1; /* for compatibility */
694 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
695 if (!n->mac_table.macs)
696 return;
698 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
699 if (!n->vlans)
700 return;
702 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
703 virtio_net_save, virtio_net_load, n);
705 return (PCIDevice *)n;