2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #include "net/checksum.h"
19 #include "qemu-error.h"
20 #include "qemu-timer.h"
21 #include "virtio-net.h"
22 #include "vhost_net.h"
24 #define VIRTIO_NET_VM_VERSION 11
26 #define MAC_TABLE_ENTRIES 64
27 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
29 typedef struct VirtIONet
32 uint8_t mac[ETH_ALEN];
43 uint32_t has_vnet_hdr;
46 VirtQueueElement elem;
49 int mergeable_rx_bufs;
56 uint8_t vhost_started;
58 VMChangeStateEntry *vmstate;
62 uint8_t multi_overflow;
71 * - we could suppress RX interrupt if we were so inclined.
74 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
76 return (VirtIONet *)vdev;
79 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
84 netcfg.status = n->status;
85 memcpy(netcfg.mac, n->mac, ETH_ALEN);
86 memcpy(config, &netcfg, sizeof(netcfg));
89 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
91 VirtIONet *n = to_virtio_net(vdev);
92 struct virtio_net_config netcfg;
94 memcpy(&netcfg, config, sizeof(netcfg));
96 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
97 memcpy(n->mac, netcfg.mac, ETH_ALEN);
98 qemu_format_nic_info_str(&n->nic->nc, n->mac);
102 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
104 VirtIONet *n = to_virtio_net(vdev);
105 if (!n->nic->nc.peer) {
108 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
112 if (!tap_get_vhost_net(n->nic->nc.peer)) {
115 if (!!n->vhost_started == ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
116 (n->status & VIRTIO_NET_S_LINK_UP) &&
120 if (!n->vhost_started) {
121 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
123 fprintf(stderr, "unable to start vhost net: %d: "
124 "falling back on userspace virtio\n", -r);
126 n->vhost_started = 1;
129 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
130 n->vhost_started = 0;
134 static void virtio_net_set_link_status(VLANClientState *nc)
136 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
137 uint16_t old_status = n->status;
140 n->status &= ~VIRTIO_NET_S_LINK_UP;
142 n->status |= VIRTIO_NET_S_LINK_UP;
144 if (n->status != old_status)
145 virtio_notify_config(&n->vdev);
147 virtio_net_set_status(&n->vdev, n->vdev.status);
150 static void virtio_net_reset(VirtIODevice *vdev)
152 VirtIONet *n = to_virtio_net(vdev);
154 /* Reset back to compatibility mode */
162 /* Flush any MAC and VLAN filter table state */
163 n->mac_table.in_use = 0;
164 n->mac_table.first_multi = 0;
165 n->mac_table.multi_overflow = 0;
166 n->mac_table.uni_overflow = 0;
167 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
168 memset(n->vlans, 0, MAX_VLAN >> 3);
171 static int peer_has_vnet_hdr(VirtIONet *n)
173 if (!n->nic->nc.peer)
176 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
179 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
181 return n->has_vnet_hdr;
184 static int peer_has_ufo(VirtIONet *n)
186 if (!peer_has_vnet_hdr(n))
189 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
194 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
196 VirtIONet *n = to_virtio_net(vdev);
198 features |= (1 << VIRTIO_NET_F_MAC);
200 if (peer_has_vnet_hdr(n)) {
201 tap_using_vnet_hdr(n->nic->nc.peer, 1);
203 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
204 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
205 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
206 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
208 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
209 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
210 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
211 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
214 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
215 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
216 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
219 if (!n->nic->nc.peer ||
220 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
223 if (!tap_get_vhost_net(n->nic->nc.peer)) {
226 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
229 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
231 uint32_t features = 0;
233 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
235 features |= (1 << VIRTIO_NET_F_MAC);
236 features |= (1 << VIRTIO_NET_F_CSUM);
237 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
238 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
239 features |= (1 << VIRTIO_NET_F_HOST_ECN);
244 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
246 VirtIONet *n = to_virtio_net(vdev);
248 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
250 if (n->has_vnet_hdr) {
251 tap_set_offload(n->nic->nc.peer,
252 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
253 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
254 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
255 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
256 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
258 if (!n->nic->nc.peer ||
259 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
262 if (!tap_get_vhost_net(n->nic->nc.peer)) {
265 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
268 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
269 VirtQueueElement *elem)
273 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
274 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
278 on = ldub_p(elem->out_sg[1].iov_base);
280 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
282 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
284 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
286 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
288 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
290 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
293 return VIRTIO_NET_ERR;
295 return VIRTIO_NET_OK;
298 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
299 VirtQueueElement *elem)
301 struct virtio_net_ctrl_mac mac_data;
303 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
304 elem->out_sg[1].iov_len < sizeof(mac_data) ||
305 elem->out_sg[2].iov_len < sizeof(mac_data))
306 return VIRTIO_NET_ERR;
308 n->mac_table.in_use = 0;
309 n->mac_table.first_multi = 0;
310 n->mac_table.uni_overflow = 0;
311 n->mac_table.multi_overflow = 0;
312 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
314 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
316 if (sizeof(mac_data.entries) +
317 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
318 return VIRTIO_NET_ERR;
320 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
321 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
322 mac_data.entries * ETH_ALEN);
323 n->mac_table.in_use += mac_data.entries;
325 n->mac_table.uni_overflow = 1;
328 n->mac_table.first_multi = n->mac_table.in_use;
330 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
332 if (sizeof(mac_data.entries) +
333 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
334 return VIRTIO_NET_ERR;
336 if (mac_data.entries) {
337 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
338 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
339 elem->out_sg[2].iov_base + sizeof(mac_data),
340 mac_data.entries * ETH_ALEN);
341 n->mac_table.in_use += mac_data.entries;
343 n->mac_table.multi_overflow = 1;
347 return VIRTIO_NET_OK;
350 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
351 VirtQueueElement *elem)
355 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
356 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
357 return VIRTIO_NET_ERR;
360 vid = lduw_le_p(elem->out_sg[1].iov_base);
363 return VIRTIO_NET_ERR;
365 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
366 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
367 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
368 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
370 return VIRTIO_NET_ERR;
372 return VIRTIO_NET_OK;
375 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
377 VirtIONet *n = to_virtio_net(vdev);
378 struct virtio_net_ctrl_hdr ctrl;
379 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
380 VirtQueueElement elem;
382 while (virtqueue_pop(vq, &elem)) {
383 if ((elem.in_num < 1) || (elem.out_num < 1)) {
384 fprintf(stderr, "virtio-net ctrl missing headers\n");
388 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
389 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
390 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
394 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
395 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
397 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
398 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
399 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
400 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
401 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
402 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
404 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
406 virtqueue_push(vq, &elem, sizeof(status));
407 virtio_notify(vdev, vq);
413 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
415 VirtIONet *n = to_virtio_net(vdev);
417 qemu_flush_queued_packets(&n->nic->nc);
419 /* We now have RX buffers, signal to the IO thread to break out of the
420 * select to re-poll the tap file descriptor */
424 static int virtio_net_can_receive(VLANClientState *nc)
426 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
428 if (!virtio_queue_ready(n->rx_vq) ||
429 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
435 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
437 if (virtio_queue_empty(n->rx_vq) ||
438 (n->mergeable_rx_bufs &&
439 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
440 virtio_queue_set_notification(n->rx_vq, 1);
442 /* To avoid a race condition where the guest has made some buffers
443 * available after the above check but before notification was
444 * enabled, check for available buffers again.
446 if (virtio_queue_empty(n->rx_vq) ||
447 (n->mergeable_rx_bufs &&
448 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
452 virtio_queue_set_notification(n->rx_vq, 0);
456 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
457 * it never finds out that the packets don't have valid checksums. This
458 * causes dhclient to get upset. Fedora's carried a patch for ages to
459 * fix this with Xen but it hasn't appeared in an upstream release of
462 * To avoid breaking existing guests, we catch udp packets and add
463 * checksums. This is terrible but it's better than hacking the guest
466 * N.B. if we introduce a zero-copy API, this operation is no longer free so
467 * we should provide a mechanism to disable it to avoid polluting the host
470 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
471 const uint8_t *buf, size_t size)
473 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
474 (size > 27 && size < 1500) && /* normal sized MTU */
475 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
476 (buf[23] == 17) && /* ip.protocol == UDP */
477 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
478 /* FIXME this cast is evil */
479 net_checksum_calculate((uint8_t *)buf, size);
480 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
484 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
485 const void *buf, size_t size, size_t hdr_len)
487 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
491 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
493 if (n->has_vnet_hdr) {
494 memcpy(hdr, buf, sizeof(*hdr));
495 offset = sizeof(*hdr);
496 work_around_broken_dhclient(hdr, buf + offset, size - offset);
499 /* We only ever receive a struct virtio_net_hdr from the tapfd,
500 * but we may be passing along a larger header to the guest.
502 iov[0].iov_base += hdr_len;
503 iov[0].iov_len -= hdr_len;
508 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
510 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
511 static const uint8_t vlan[] = {0x81, 0x00};
512 uint8_t *ptr = (uint8_t *)buf;
518 if (n->has_vnet_hdr) {
519 ptr += sizeof(struct virtio_net_hdr);
522 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
523 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
524 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
528 if (ptr[0] & 1) { // multicast
529 if (!memcmp(ptr, bcast, sizeof(bcast))) {
531 } else if (n->nomulti) {
533 } else if (n->allmulti || n->mac_table.multi_overflow) {
537 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
538 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
545 } else if (n->alluni || n->mac_table.uni_overflow) {
547 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
551 for (i = 0; i < n->mac_table.first_multi; i++) {
552 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
561 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
563 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
564 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
565 size_t guest_hdr_len, offset, i, host_hdr_len;
567 if (!virtio_net_can_receive(&n->nic->nc))
570 /* hdr_len refers to the header we supply to the guest */
571 guest_hdr_len = n->mergeable_rx_bufs ?
572 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
575 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
576 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
579 if (!receive_filter(n, buf, size))
584 while (offset < size) {
585 VirtQueueElement elem;
587 struct iovec sg[VIRTQUEUE_MAX_SIZE];
591 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
594 fprintf(stderr, "virtio-net unexpected empty queue: "
595 "i %zd mergeable %d offset %zd, size %zd, "
596 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
597 i, n->mergeable_rx_bufs, offset, size,
598 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
602 if (elem.in_num < 1) {
603 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
607 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
608 fprintf(stderr, "virtio-net header not in first element\n");
612 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
615 if (n->mergeable_rx_bufs)
616 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
618 offset += receive_header(n, sg, elem.in_num,
619 buf + offset, size - offset, guest_hdr_len);
620 total += guest_hdr_len;
623 /* copy in packet. ugh */
624 len = iov_from_buf(sg, elem.in_num,
625 buf + offset, size - offset);
628 /* If buffers can't be merged, at this point we
629 * must have consumed the complete packet.
630 * Otherwise, drop it. */
631 if (!n->mergeable_rx_bufs && offset < size) {
633 fprintf(stderr, "virtio-net truncated non-mergeable packet: "
635 "i %zd mergeable %d offset %zd, size %zd, "
636 "guest hdr len %zd, host hdr len %zd\n",
637 i, n->mergeable_rx_bufs,
638 offset, size, guest_hdr_len, host_hdr_len);
643 /* signal other side */
644 virtqueue_fill(n->rx_vq, &elem, total, i++);
648 mhdr->num_buffers = i;
650 virtqueue_flush(n->rx_vq, i);
651 virtio_notify(&n->vdev, n->rx_vq);
656 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
658 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
660 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
662 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
663 virtio_notify(&n->vdev, n->tx_vq);
665 n->async_tx.elem.out_num = n->async_tx.len = 0;
667 virtio_queue_set_notification(n->tx_vq, 1);
668 virtio_net_flush_tx(n, n->tx_vq);
672 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
674 VirtQueueElement elem;
675 int32_t num_packets = 0;
677 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
681 if (n->async_tx.elem.out_num) {
682 virtio_queue_set_notification(n->tx_vq, 0);
686 while (virtqueue_pop(vq, &elem)) {
687 ssize_t ret, len = 0;
688 unsigned int out_num = elem.out_num;
689 struct iovec *out_sg = &elem.out_sg[0];
692 /* hdr_len refers to the header received from the guest */
693 hdr_len = n->mergeable_rx_bufs ?
694 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
695 sizeof(struct virtio_net_hdr);
697 if (out_num < 1 || out_sg->iov_len != hdr_len) {
698 fprintf(stderr, "virtio-net header not in first element\n");
702 /* ignore the header if GSO is not supported */
703 if (!n->has_vnet_hdr) {
707 } else if (n->mergeable_rx_bufs) {
708 /* tapfd expects a struct virtio_net_hdr */
709 hdr_len -= sizeof(struct virtio_net_hdr);
710 out_sg->iov_len -= hdr_len;
714 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
715 virtio_net_tx_complete);
717 virtio_queue_set_notification(n->tx_vq, 0);
718 n->async_tx.elem = elem;
719 n->async_tx.len = len;
725 virtqueue_push(vq, &elem, len);
726 virtio_notify(&n->vdev, vq);
728 if (++num_packets >= n->tx_burst) {
735 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
737 VirtIONet *n = to_virtio_net(vdev);
740 virtio_queue_set_notification(vq, 1);
741 qemu_del_timer(n->tx_timer);
743 virtio_net_flush_tx(n, vq);
745 qemu_mod_timer(n->tx_timer,
746 qemu_get_clock(vm_clock) + n->tx_timeout);
748 virtio_queue_set_notification(vq, 0);
752 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
754 VirtIONet *n = to_virtio_net(vdev);
756 if (unlikely(n->tx_waiting)) {
759 virtio_queue_set_notification(vq, 0);
760 qemu_bh_schedule(n->tx_bh);
764 static void virtio_net_tx_timer(void *opaque)
766 VirtIONet *n = opaque;
770 /* Just in case the driver is not ready on more */
771 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
774 virtio_queue_set_notification(n->tx_vq, 1);
775 virtio_net_flush_tx(n, n->tx_vq);
778 static void virtio_net_tx_bh(void *opaque)
780 VirtIONet *n = opaque;
785 /* Just in case the driver is not ready on more */
786 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
789 ret = virtio_net_flush_tx(n, n->tx_vq);
791 return; /* Notification re-enable handled by tx_complete */
794 /* If we flush a full burst of packets, assume there are
795 * more coming and immediately reschedule */
796 if (ret >= n->tx_burst) {
797 qemu_bh_schedule(n->tx_bh);
802 /* If less than a full burst, re-enable notification and flush
803 * anything that may have come in while we weren't looking. If
804 * we find something, assume the guest is still active and reschedule */
805 virtio_queue_set_notification(n->tx_vq, 1);
806 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
807 virtio_queue_set_notification(n->tx_vq, 0);
808 qemu_bh_schedule(n->tx_bh);
813 static void virtio_net_save(QEMUFile *f, void *opaque)
815 VirtIONet *n = opaque;
817 /* At this point, backend must be stopped, otherwise
818 * it might keep writing to memory. */
819 assert(!n->vhost_started);
820 virtio_save(&n->vdev, f);
822 qemu_put_buffer(f, n->mac, ETH_ALEN);
823 qemu_put_be32(f, n->tx_waiting);
824 qemu_put_be32(f, n->mergeable_rx_bufs);
825 qemu_put_be16(f, n->status);
826 qemu_put_byte(f, n->promisc);
827 qemu_put_byte(f, n->allmulti);
828 qemu_put_be32(f, n->mac_table.in_use);
829 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
830 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
831 qemu_put_be32(f, n->has_vnet_hdr);
832 qemu_put_byte(f, n->mac_table.multi_overflow);
833 qemu_put_byte(f, n->mac_table.uni_overflow);
834 qemu_put_byte(f, n->alluni);
835 qemu_put_byte(f, n->nomulti);
836 qemu_put_byte(f, n->nouni);
837 qemu_put_byte(f, n->nobcast);
838 qemu_put_byte(f, n->has_ufo);
841 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
843 VirtIONet *n = opaque;
846 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
849 virtio_load(&n->vdev, f);
851 qemu_get_buffer(f, n->mac, ETH_ALEN);
852 n->tx_waiting = qemu_get_be32(f);
853 n->mergeable_rx_bufs = qemu_get_be32(f);
856 n->status = qemu_get_be16(f);
858 if (version_id >= 4) {
859 if (version_id < 8) {
860 n->promisc = qemu_get_be32(f);
861 n->allmulti = qemu_get_be32(f);
863 n->promisc = qemu_get_byte(f);
864 n->allmulti = qemu_get_byte(f);
868 if (version_id >= 5) {
869 n->mac_table.in_use = qemu_get_be32(f);
870 /* MAC_TABLE_ENTRIES may be different from the saved image */
871 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
872 qemu_get_buffer(f, n->mac_table.macs,
873 n->mac_table.in_use * ETH_ALEN);
874 } else if (n->mac_table.in_use) {
875 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
876 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
877 n->mac_table.in_use = 0;
882 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
884 if (version_id >= 7) {
885 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
886 error_report("virtio-net: saved image requires vnet_hdr=on");
890 if (n->has_vnet_hdr) {
891 tap_using_vnet_hdr(n->nic->nc.peer, 1);
892 tap_set_offload(n->nic->nc.peer,
893 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
894 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
895 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
896 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
897 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
901 if (version_id >= 9) {
902 n->mac_table.multi_overflow = qemu_get_byte(f);
903 n->mac_table.uni_overflow = qemu_get_byte(f);
906 if (version_id >= 10) {
907 n->alluni = qemu_get_byte(f);
908 n->nomulti = qemu_get_byte(f);
909 n->nouni = qemu_get_byte(f);
910 n->nobcast = qemu_get_byte(f);
913 if (version_id >= 11) {
914 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
915 error_report("virtio-net: saved image requires TUN_F_UFO support");
920 /* Find the first multicast entry in the saved MAC filter */
921 for (i = 0; i < n->mac_table.in_use; i++) {
922 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
926 n->mac_table.first_multi = i;
930 qemu_mod_timer(n->tx_timer,
931 qemu_get_clock(vm_clock) + n->tx_timeout);
933 qemu_bh_schedule(n->tx_bh);
939 static void virtio_net_cleanup(VLANClientState *nc)
941 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
946 static NetClientInfo net_virtio_info = {
947 .type = NET_CLIENT_TYPE_NIC,
948 .size = sizeof(NICState),
949 .can_receive = virtio_net_can_receive,
950 .receive = virtio_net_receive,
951 .cleanup = virtio_net_cleanup,
952 .link_status_changed = virtio_net_set_link_status,
955 static void virtio_net_vmstate_change(void *opaque, int running, int reason)
957 VirtIONet *n = opaque;
958 n->vm_running = running;
959 /* This is called when vm is started/stopped,
960 * it will start/stop vhost backend if appropriate
961 * e.g. after migration. */
962 virtio_net_set_status(&n->vdev, n->vdev.status);
965 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
966 virtio_net_conf *net)
970 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
971 sizeof(struct virtio_net_config),
974 n->vdev.get_config = virtio_net_get_config;
975 n->vdev.set_config = virtio_net_set_config;
976 n->vdev.get_features = virtio_net_get_features;
977 n->vdev.set_features = virtio_net_set_features;
978 n->vdev.bad_features = virtio_net_bad_features;
979 n->vdev.reset = virtio_net_reset;
980 n->vdev.set_status = virtio_net_set_status;
981 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
983 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
984 fprintf(stderr, "virtio-net: "
985 "Unknown option tx=%s, valid options: \"timer\" \"bh\"\n",
987 fprintf(stderr, "Defaulting to \"bh\"\n");
990 if (net->tx && !strcmp(net->tx, "timer")) {
991 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
992 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
993 n->tx_timeout = net->txtimer;
995 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
996 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
998 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
999 qemu_macaddr_default_if_unset(&conf->macaddr);
1000 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1001 n->status = VIRTIO_NET_S_LINK_UP;
1003 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1005 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1008 n->tx_burst = net->txburst;
1009 n->mergeable_rx_bufs = 0;
1010 n->promisc = 1; /* for compatibility */
1012 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
1014 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
1017 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1018 virtio_net_save, virtio_net_load, n);
1019 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
1024 void virtio_net_exit(VirtIODevice *vdev)
1026 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1027 qemu_del_vm_change_state_handler(n->vmstate);
1029 /* This will stop vhost backend if appropriate. */
1030 virtio_net_set_status(vdev, 0);
1032 qemu_purge_queued_packets(&n->nic->nc);
1034 unregister_savevm(n->qdev, "virtio-net", n);
1036 qemu_free(n->mac_table.macs);
1037 qemu_free(n->vlans);
1040 qemu_del_timer(n->tx_timer);
1041 qemu_free_timer(n->tx_timer);
1043 qemu_bh_delete(n->tx_bh);
1046 virtio_cleanup(&n->vdev);
1047 qemu_del_vlan_client(&n->nic->nc);