kvm: qemu: propagate errors on failed migration.
[kvm-userspace.git] / qemu / hw / virtio-net.c
blobef7789b05d766a526f19972deae47bf997a7fb65
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 uint32_t virtio_net_bad_features(VirtIODevice *vdev)
141 uint32_t features = 0;
143 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
144 * but also these: */
145 features |= (1 << VIRTIO_NET_F_MAC);
146 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
147 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
148 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
149 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
151 return features & virtio_net_get_features(vdev);
154 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
156 VirtIONet *n = to_virtio_net(vdev);
157 #ifdef TAP_VNET_HDR
158 VLANClientState *host = n->vc->vlan->first_client;
159 #endif
161 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
163 #ifdef TAP_VNET_HDR
164 if (!tap_has_vnet_hdr(host) || !host->set_offload)
165 return;
167 host->set_offload(host,
168 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
169 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
170 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
171 (features >> VIRTIO_NET_F_GUEST_ECN) & 1);
172 #endif
175 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
176 VirtQueueElement *elem)
178 uint8_t on;
180 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
181 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
182 exit(1);
185 on = ldub_p(elem->out_sg[1].iov_base);
187 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
188 n->promisc = on;
189 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
190 n->allmulti = on;
191 else
192 return VIRTIO_NET_ERR;
194 return VIRTIO_NET_OK;
197 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
198 VirtQueueElement *elem)
200 struct virtio_net_ctrl_mac mac_data;
202 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
203 elem->out_sg[1].iov_len < sizeof(mac_data) ||
204 elem->out_sg[2].iov_len < sizeof(mac_data))
205 return VIRTIO_NET_ERR;
207 n->mac_table.in_use = 0;
208 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
210 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
212 if (sizeof(mac_data.entries) +
213 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
214 return VIRTIO_NET_ERR;
216 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
217 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
218 mac_data.entries * ETH_ALEN);
219 n->mac_table.in_use += mac_data.entries;
220 } else {
221 n->promisc = 1;
222 return VIRTIO_NET_OK;
225 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
227 if (sizeof(mac_data.entries) +
228 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
229 return VIRTIO_NET_ERR;
231 if (mac_data.entries) {
232 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
233 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
234 elem->out_sg[2].iov_base + sizeof(mac_data),
235 mac_data.entries * ETH_ALEN);
236 n->mac_table.in_use += mac_data.entries;
237 } else
238 n->allmulti = 1;
241 return VIRTIO_NET_OK;
244 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
245 VirtQueueElement *elem)
247 uint16_t vid;
249 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
250 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
251 return VIRTIO_NET_ERR;
254 vid = lduw_le_p(elem->out_sg[1].iov_base);
256 if (vid >= MAX_VLAN)
257 return VIRTIO_NET_ERR;
259 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
260 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
261 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
262 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
263 else
264 return VIRTIO_NET_ERR;
266 return VIRTIO_NET_OK;
269 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
271 VirtIONet *n = to_virtio_net(vdev);
272 struct virtio_net_ctrl_hdr ctrl;
273 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
274 VirtQueueElement elem;
276 while (virtqueue_pop(vq, &elem)) {
277 if ((elem.in_num < 1) || (elem.out_num < 1)) {
278 fprintf(stderr, "virtio-net ctrl missing headers\n");
279 exit(1);
282 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
283 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
284 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
285 exit(1);
288 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
289 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
291 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
292 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
293 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
294 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
295 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
296 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
298 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
300 virtqueue_push(vq, &elem, sizeof(status));
301 virtio_notify(vdev, vq);
305 /* RX */
307 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
309 #ifdef USE_KVM
310 /* We now have RX buffers, signal to the IO thread to break out of the
311 select to re-poll the tap file descriptor */
312 if (kvm_enabled())
313 qemu_kvm_notify_work();
314 #endif
317 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
319 if (!virtio_queue_ready(n->rx_vq) ||
320 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
321 return 0;
323 if (virtio_queue_empty(n->rx_vq) ||
324 (n->mergeable_rx_bufs &&
325 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
326 virtio_queue_set_notification(n->rx_vq, 1);
327 return 0;
330 virtio_queue_set_notification(n->rx_vq, 0);
331 return 1;
334 static int virtio_net_can_receive(void *opaque)
336 VirtIONet *n = opaque;
338 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
341 #ifdef TAP_VNET_HDR
342 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
343 * it never finds out that the packets don't have valid checksums. This
344 * causes dhclient to get upset. Fedora's carried a patch for ages to
345 * fix this with Xen but it hasn't appeared in an upstream release of
346 * dhclient yet.
348 * To avoid breaking existing guests, we catch udp packets and add
349 * checksums. This is terrible but it's better than hacking the guest
350 * kernels.
352 * N.B. if we introduce a zero-copy API, this operation is no longer free so
353 * we should provide a mechanism to disable it to avoid polluting the host
354 * cache.
356 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
357 const uint8_t *buf, size_t size)
359 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
360 (size > 27 && size < 1500) && /* normal sized MTU */
361 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
362 (buf[23] == 17) && /* ip.protocol == UDP */
363 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
364 /* FIXME this cast is evil */
365 net_checksum_calculate((uint8_t *)buf, size);
366 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
369 #endif
371 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
373 int offset, i;
375 offset = i = 0;
376 while (offset < count && i < iovcnt) {
377 int len = MIN(iov[i].iov_len, count - offset);
378 memcpy(iov[i].iov_base, buf + offset, len);
379 offset += len;
380 i++;
383 return offset;
386 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
387 const void *buf, size_t size, size_t hdr_len)
389 struct virtio_net_hdr *hdr = iov[0].iov_base;
390 int offset = 0;
392 hdr->flags = 0;
393 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
395 #ifdef TAP_VNET_HDR
396 if (tap_has_vnet_hdr(n->vc->vlan->first_client)) {
397 memcpy(hdr, buf, sizeof(*hdr));
398 offset = sizeof(*hdr);
399 work_around_broken_dhclient(hdr, buf + offset, size - offset);
401 #endif
403 /* We only ever receive a struct virtio_net_hdr from the tapfd,
404 * but we may be passing along a larger header to the guest.
406 iov[0].iov_base += hdr_len;
407 iov[0].iov_len -= hdr_len;
409 return offset;
412 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
414 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
415 static const uint8_t vlan[] = {0x81, 0x00};
416 uint8_t *ptr = (uint8_t *)buf;
417 int i;
419 if (n->promisc)
420 return 1;
422 #ifdef TAP_VNET_HDR
423 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
424 ptr += sizeof(struct virtio_net_hdr);
425 #endif
427 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
428 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
429 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
430 return 0;
433 if ((ptr[0] & 1) && n->allmulti)
434 return 1;
436 if (!memcmp(ptr, bcast, sizeof(bcast)))
437 return 1;
439 if (!memcmp(ptr, n->mac, ETH_ALEN))
440 return 1;
442 for (i = 0; i < n->mac_table.in_use; i++) {
443 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
444 return 1;
447 return 0;
450 static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
452 VirtIONet *n = opaque;
453 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
454 size_t hdr_len, offset, i;
456 if (!do_virtio_net_can_receive(n, size))
457 return;
459 if (!receive_filter(n, buf, size))
460 return;
462 /* hdr_len refers to the header we supply to the guest */
463 hdr_len = n->mergeable_rx_bufs ?
464 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
466 offset = i = 0;
468 while (offset < size) {
469 VirtQueueElement elem;
470 int len, total;
471 struct iovec sg[VIRTQUEUE_MAX_SIZE];
473 len = total = 0;
475 if ((i != 0 && !n->mergeable_rx_bufs) ||
476 virtqueue_pop(n->rx_vq, &elem) == 0) {
477 if (i == 0)
478 return;
479 fprintf(stderr, "virtio-net truncating packet\n");
480 exit(1);
483 if (elem.in_num < 1) {
484 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
485 exit(1);
488 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
489 fprintf(stderr, "virtio-net header not in first element\n");
490 exit(1);
493 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
495 if (i == 0) {
496 if (n->mergeable_rx_bufs)
497 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
499 offset += receive_header(n, sg, elem.in_num,
500 buf + offset, size - offset, hdr_len);
501 total += hdr_len;
504 /* copy in packet. ugh */
505 len = iov_fill(sg, elem.in_num,
506 buf + offset, size - offset);
507 total += len;
509 /* signal other side */
510 virtqueue_fill(n->rx_vq, &elem, total, i++);
512 offset += len;
515 if (mhdr)
516 mhdr->num_buffers = i;
518 virtqueue_flush(n->rx_vq, i);
519 virtio_notify(&n->vdev, n->rx_vq);
522 /* TX */
523 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
525 VirtQueueElement elem;
526 #ifdef TAP_VNET_HDR
527 int has_vnet_hdr = tap_has_vnet_hdr(n->vc->vlan->first_client);
528 #else
529 int has_vnet_hdr = 0;
530 #endif
532 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
533 return;
535 while (virtqueue_pop(vq, &elem)) {
536 ssize_t len = 0;
537 unsigned int out_num = elem.out_num;
538 struct iovec *out_sg = &elem.out_sg[0];
539 unsigned hdr_len;
541 /* hdr_len refers to the header received from the guest */
542 hdr_len = n->mergeable_rx_bufs ?
543 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
544 sizeof(struct virtio_net_hdr);
546 if (out_num < 1 || out_sg->iov_len != hdr_len) {
547 fprintf(stderr, "virtio-net header not in first element\n");
548 exit(1);
551 /* ignore the header if GSO is not supported */
552 if (!has_vnet_hdr) {
553 out_num--;
554 out_sg++;
555 len += hdr_len;
556 } else if (n->mergeable_rx_bufs) {
557 /* tapfd expects a struct virtio_net_hdr */
558 hdr_len -= sizeof(struct virtio_net_hdr);
559 out_sg->iov_len -= hdr_len;
560 len += hdr_len;
563 len += qemu_sendv_packet(n->vc, out_sg, out_num);
565 virtqueue_push(vq, &elem, len);
566 virtio_notify(&n->vdev, vq);
570 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
572 VirtIONet *n = to_virtio_net(vdev);
574 if (n->tx_timer_active) {
575 virtio_queue_set_notification(vq, 1);
576 qemu_del_timer(n->tx_timer);
577 n->tx_timer_active = 0;
578 virtio_net_flush_tx(n, vq);
579 } else {
580 qemu_mod_timer(n->tx_timer,
581 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
582 n->tx_timer_active = 1;
583 virtio_queue_set_notification(vq, 0);
587 static void virtio_net_tx_timer(void *opaque)
589 VirtIONet *n = opaque;
591 n->tx_timer_active = 0;
593 /* Just in case the driver is not ready on more */
594 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
595 return;
597 virtio_queue_set_notification(n->tx_vq, 1);
598 virtio_net_flush_tx(n, n->tx_vq);
601 static void virtio_net_save(QEMUFile *f, void *opaque)
603 VirtIONet *n = opaque;
605 virtio_save(&n->vdev, f);
607 qemu_put_buffer(f, n->mac, ETH_ALEN);
608 qemu_put_be32(f, n->tx_timer_active);
609 qemu_put_be32(f, n->mergeable_rx_bufs);
610 qemu_put_be16(f, n->status);
611 qemu_put_be32(f, n->promisc);
612 qemu_put_be32(f, n->allmulti);
613 qemu_put_be32(f, n->mac_table.in_use);
614 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
615 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
617 #ifdef TAP_VNET_HDR
618 qemu_put_be32(f, tap_has_vnet_hdr(n->vc->vlan->first_client));
619 #endif
622 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
624 VirtIONet *n = opaque;
626 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
627 return -EINVAL;
629 virtio_load(&n->vdev, f);
631 qemu_get_buffer(f, n->mac, ETH_ALEN);
632 n->tx_timer_active = qemu_get_be32(f);
633 n->mergeable_rx_bufs = qemu_get_be32(f);
635 if (version_id >= 3)
636 n->status = qemu_get_be16(f);
638 if (version_id >= 4) {
639 n->promisc = qemu_get_be32(f);
640 n->allmulti = qemu_get_be32(f);
643 if (version_id >= 5) {
644 n->mac_table.in_use = qemu_get_be32(f);
645 /* MAC_TABLE_ENTRIES may be different from the saved image */
646 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
647 qemu_get_buffer(f, n->mac_table.macs,
648 n->mac_table.in_use * ETH_ALEN);
649 } else if (n->mac_table.in_use) {
650 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
651 n->promisc = 1;
652 n->mac_table.in_use = 0;
656 if (version_id >= 6)
657 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
659 #ifdef TAP_VNET_HDR
660 if (qemu_get_be32(f))
661 tap_using_vnet_hdr(n->vc->vlan->first_client, 1);
662 #endif
664 if (n->tx_timer_active) {
665 qemu_mod_timer(n->tx_timer,
666 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
669 return 0;
672 PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
674 VirtIONet *n;
675 static int virtio_net_id;
677 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
678 PCI_VENDOR_ID_REDHAT_QUMRANET,
679 PCI_DEVICE_ID_VIRTIO_NET,
680 PCI_VENDOR_ID_REDHAT_QUMRANET,
681 VIRTIO_ID_NET,
682 PCI_CLASS_NETWORK_ETHERNET, 0x00,
683 sizeof(struct virtio_net_config),
684 sizeof(VirtIONet));
685 if (!n)
686 return NULL;
688 n->vdev.get_config = virtio_net_get_config;
689 n->vdev.set_config = virtio_net_set_config;
690 n->vdev.get_features = virtio_net_get_features;
691 n->vdev.set_features = virtio_net_set_features;
692 n->vdev.bad_features = virtio_net_bad_features;
693 n->vdev.reset = virtio_net_reset;
694 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
695 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
696 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
697 memcpy(n->mac, nd->macaddr, ETH_ALEN);
698 n->status = VIRTIO_NET_S_LINK_UP;
699 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
700 virtio_net_receive, virtio_net_can_receive, n);
701 n->vc->link_status_changed = virtio_net_set_link_status;
703 qemu_format_nic_info_str(n->vc, n->mac);
705 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
706 n->tx_timer_active = 0;
707 n->mergeable_rx_bufs = 0;
708 n->promisc = 1; /* for compatibility */
710 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
712 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
714 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
715 virtio_net_save, virtio_net_load, n);
716 return (PCIDevice *)n;