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;
48 VirtQueueElement elem;
51 int mergeable_rx_bufs;
58 uint8_t vhost_started;
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 stw_p(&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 bool virtio_net_started(VirtIONet *n, uint8_t status)
104 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
105 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
108 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
110 if (!n->nic->nc.peer) {
113 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
117 if (!tap_get_vhost_net(n->nic->nc.peer)) {
120 if (!!n->vhost_started == virtio_net_started(n, status) &&
121 !n->nic->nc.peer->link_down) {
124 if (!n->vhost_started) {
126 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
129 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
131 error_report("unable to start vhost net: %d: "
132 "falling back on userspace virtio", -r);
134 n->vhost_started = 1;
137 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
138 n->vhost_started = 0;
142 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
144 VirtIONet *n = to_virtio_net(vdev);
146 virtio_net_vhost_status(n, status);
148 if (!n->tx_waiting) {
152 if (virtio_net_started(n, status) && !n->vhost_started) {
154 qemu_mod_timer(n->tx_timer,
155 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
157 qemu_bh_schedule(n->tx_bh);
161 qemu_del_timer(n->tx_timer);
163 qemu_bh_cancel(n->tx_bh);
168 static void virtio_net_set_link_status(NetClientState *nc)
170 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
171 uint16_t old_status = n->status;
174 n->status &= ~VIRTIO_NET_S_LINK_UP;
176 n->status |= VIRTIO_NET_S_LINK_UP;
178 if (n->status != old_status)
179 virtio_notify_config(&n->vdev);
181 virtio_net_set_status(&n->vdev, n->vdev.status);
184 static void virtio_net_reset(VirtIODevice *vdev)
186 VirtIONet *n = to_virtio_net(vdev);
188 /* Reset back to compatibility mode */
196 /* Flush any MAC and VLAN filter table state */
197 n->mac_table.in_use = 0;
198 n->mac_table.first_multi = 0;
199 n->mac_table.multi_overflow = 0;
200 n->mac_table.uni_overflow = 0;
201 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
202 memset(n->vlans, 0, MAX_VLAN >> 3);
205 static int peer_has_vnet_hdr(VirtIONet *n)
207 if (!n->nic->nc.peer)
210 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
213 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
215 return n->has_vnet_hdr;
218 static int peer_has_ufo(VirtIONet *n)
220 if (!peer_has_vnet_hdr(n))
223 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
228 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
230 VirtIONet *n = to_virtio_net(vdev);
232 features |= (1 << VIRTIO_NET_F_MAC);
234 if (peer_has_vnet_hdr(n)) {
235 tap_using_vnet_hdr(n->nic->nc.peer, 1);
236 n->host_hdr_len = sizeof(struct virtio_net_hdr);
238 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
239 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
240 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
241 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
243 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
244 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
245 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
246 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
249 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
250 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
251 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
254 if (!n->nic->nc.peer ||
255 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
258 if (!tap_get_vhost_net(n->nic->nc.peer)) {
261 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
264 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
266 uint32_t features = 0;
268 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
270 features |= (1 << VIRTIO_NET_F_MAC);
271 features |= (1 << VIRTIO_NET_F_CSUM);
272 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
273 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
274 features |= (1 << VIRTIO_NET_F_HOST_ECN);
279 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
281 VirtIONet *n = to_virtio_net(vdev);
283 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
284 n->guest_hdr_len = n->mergeable_rx_bufs ?
285 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
287 if (n->has_vnet_hdr) {
288 tap_set_offload(n->nic->nc.peer,
289 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
290 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
291 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
292 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
293 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
295 if (!n->nic->nc.peer ||
296 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
299 if (!tap_get_vhost_net(n->nic->nc.peer)) {
302 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
305 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
306 VirtQueueElement *elem)
310 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
311 error_report("virtio-net ctrl invalid rx mode command");
315 on = ldub_p(elem->out_sg[1].iov_base);
317 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
319 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
321 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
323 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
325 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
327 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
330 return VIRTIO_NET_ERR;
332 return VIRTIO_NET_OK;
335 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
336 VirtQueueElement *elem)
338 struct virtio_net_ctrl_mac mac_data;
340 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
341 elem->out_sg[1].iov_len < sizeof(mac_data) ||
342 elem->out_sg[2].iov_len < sizeof(mac_data))
343 return VIRTIO_NET_ERR;
345 n->mac_table.in_use = 0;
346 n->mac_table.first_multi = 0;
347 n->mac_table.uni_overflow = 0;
348 n->mac_table.multi_overflow = 0;
349 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
351 mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
353 if (sizeof(mac_data.entries) +
354 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
355 return VIRTIO_NET_ERR;
357 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
358 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
359 mac_data.entries * ETH_ALEN);
360 n->mac_table.in_use += mac_data.entries;
362 n->mac_table.uni_overflow = 1;
365 n->mac_table.first_multi = n->mac_table.in_use;
367 mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
369 if (sizeof(mac_data.entries) +
370 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
371 return VIRTIO_NET_ERR;
373 if (mac_data.entries) {
374 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
375 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
376 elem->out_sg[2].iov_base + sizeof(mac_data),
377 mac_data.entries * ETH_ALEN);
378 n->mac_table.in_use += mac_data.entries;
380 n->mac_table.multi_overflow = 1;
384 return VIRTIO_NET_OK;
387 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
388 VirtQueueElement *elem)
392 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
393 error_report("virtio-net ctrl invalid vlan command");
394 return VIRTIO_NET_ERR;
397 vid = lduw_p(elem->out_sg[1].iov_base);
400 return VIRTIO_NET_ERR;
402 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
403 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
404 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
405 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
407 return VIRTIO_NET_ERR;
409 return VIRTIO_NET_OK;
412 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
414 VirtIONet *n = to_virtio_net(vdev);
415 struct virtio_net_ctrl_hdr ctrl;
416 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
417 VirtQueueElement elem;
419 while (virtqueue_pop(vq, &elem)) {
420 if ((elem.in_num < 1) || (elem.out_num < 1)) {
421 error_report("virtio-net ctrl missing headers");
425 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
426 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
427 error_report("virtio-net ctrl header not in correct element");
431 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
432 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
434 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
435 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
436 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
437 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
438 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
439 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
441 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
443 virtqueue_push(vq, &elem, sizeof(status));
444 virtio_notify(vdev, vq);
450 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
452 VirtIONet *n = to_virtio_net(vdev);
454 qemu_flush_queued_packets(&n->nic->nc);
457 static int virtio_net_can_receive(NetClientState *nc)
459 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
460 if (!n->vdev.vm_running) {
464 if (!virtio_queue_ready(n->rx_vq) ||
465 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
471 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
473 if (virtio_queue_empty(n->rx_vq) ||
474 (n->mergeable_rx_bufs &&
475 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
476 virtio_queue_set_notification(n->rx_vq, 1);
478 /* To avoid a race condition where the guest has made some buffers
479 * available after the above check but before notification was
480 * enabled, check for available buffers again.
482 if (virtio_queue_empty(n->rx_vq) ||
483 (n->mergeable_rx_bufs &&
484 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
488 virtio_queue_set_notification(n->rx_vq, 0);
492 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
493 * it never finds out that the packets don't have valid checksums. This
494 * causes dhclient to get upset. Fedora's carried a patch for ages to
495 * fix this with Xen but it hasn't appeared in an upstream release of
498 * To avoid breaking existing guests, we catch udp packets and add
499 * checksums. This is terrible but it's better than hacking the guest
502 * N.B. if we introduce a zero-copy API, this operation is no longer free so
503 * we should provide a mechanism to disable it to avoid polluting the host
506 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
507 uint8_t *buf, size_t size)
509 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
510 (size > 27 && size < 1500) && /* normal sized MTU */
511 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
512 (buf[23] == 17) && /* ip.protocol == UDP */
513 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
514 net_checksum_calculate(buf, size);
515 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
519 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
520 const void *buf, size_t size)
522 if (n->has_vnet_hdr) {
523 /* FIXME this cast is evil */
524 void *wbuf = (void *)buf;
525 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
526 size - n->host_hdr_len);
527 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
529 struct virtio_net_hdr hdr = {
531 .gso_type = VIRTIO_NET_HDR_GSO_NONE
533 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
537 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
539 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
540 static const uint8_t vlan[] = {0x81, 0x00};
541 uint8_t *ptr = (uint8_t *)buf;
547 if (n->has_vnet_hdr) {
548 ptr += sizeof(struct virtio_net_hdr);
551 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
552 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
553 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
557 if (ptr[0] & 1) { // multicast
558 if (!memcmp(ptr, bcast, sizeof(bcast))) {
560 } else if (n->nomulti) {
562 } else if (n->allmulti || n->mac_table.multi_overflow) {
566 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
567 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
574 } else if (n->alluni || n->mac_table.uni_overflow) {
576 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
580 for (i = 0; i < n->mac_table.first_multi; i++) {
581 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
590 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
592 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
593 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
594 struct virtio_net_hdr_mrg_rxbuf mhdr;
595 unsigned mhdr_cnt = 0;
596 size_t offset, i, guest_offset;
598 if (!virtio_net_can_receive(&n->nic->nc))
601 /* hdr_len refers to the header we supply to the guest */
602 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
605 if (!receive_filter(n, buf, size))
610 while (offset < size) {
611 VirtQueueElement elem;
613 const struct iovec *sg = elem.in_sg;
617 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
620 error_report("virtio-net unexpected empty queue: "
621 "i %zd mergeable %d offset %zd, size %zd, "
622 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
623 i, n->mergeable_rx_bufs, offset, size,
624 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
628 if (elem.in_num < 1) {
629 error_report("virtio-net receive queue contains no in buffers");
635 if (n->mergeable_rx_bufs) {
636 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
638 offsetof(typeof(mhdr), num_buffers),
639 sizeof(mhdr.num_buffers));
642 receive_header(n, sg, elem.in_num, buf, size);
643 offset = n->host_hdr_len;
644 total += n->guest_hdr_len;
645 guest_offset = n->guest_hdr_len;
650 /* copy in packet. ugh */
651 len = iov_from_buf(sg, elem.in_num, guest_offset,
652 buf + offset, size - offset);
655 /* If buffers can't be merged, at this point we
656 * must have consumed the complete packet.
657 * Otherwise, drop it. */
658 if (!n->mergeable_rx_bufs && offset < size) {
660 error_report("virtio-net truncated non-mergeable packet: "
661 "i %zd mergeable %d offset %zd, size %zd, "
662 "guest hdr len %zd, host hdr len %zd",
663 i, n->mergeable_rx_bufs,
664 offset, size, n->guest_hdr_len, n->host_hdr_len);
669 /* signal other side */
670 virtqueue_fill(n->rx_vq, &elem, total, i++);
674 stw_p(&mhdr.num_buffers, i);
675 iov_from_buf(mhdr_sg, mhdr_cnt,
677 &mhdr.num_buffers, sizeof mhdr.num_buffers);
680 virtqueue_flush(n->rx_vq, i);
681 virtio_notify(&n->vdev, n->rx_vq);
686 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
688 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
690 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
692 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
693 virtio_notify(&n->vdev, n->tx_vq);
695 n->async_tx.elem.out_num = n->async_tx.len = 0;
697 virtio_queue_set_notification(n->tx_vq, 1);
698 virtio_net_flush_tx(n, n->tx_vq);
702 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
704 VirtQueueElement elem;
705 int32_t num_packets = 0;
706 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
710 assert(n->vdev.vm_running);
712 if (n->async_tx.elem.out_num) {
713 virtio_queue_set_notification(n->tx_vq, 0);
717 while (virtqueue_pop(vq, &elem)) {
719 unsigned int out_num = elem.out_num;
720 struct iovec *out_sg = &elem.out_sg[0];
721 struct iovec sg[VIRTQUEUE_MAX_SIZE];
724 error_report("virtio-net header not in first element");
729 * If host wants to see the guest header as is, we can
730 * pass it on unchanged. Otherwise, copy just the parts
731 * that host is interested in.
733 assert(n->host_hdr_len <= n->guest_hdr_len);
734 if (n->host_hdr_len != n->guest_hdr_len) {
735 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
738 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
740 n->guest_hdr_len, -1);
745 len = n->guest_hdr_len;
747 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
748 virtio_net_tx_complete);
750 virtio_queue_set_notification(n->tx_vq, 0);
751 n->async_tx.elem = elem;
752 n->async_tx.len = len;
758 virtqueue_push(vq, &elem, 0);
759 virtio_notify(&n->vdev, vq);
761 if (++num_packets >= n->tx_burst) {
768 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
770 VirtIONet *n = to_virtio_net(vdev);
772 /* This happens when device was stopped but VCPU wasn't. */
773 if (!n->vdev.vm_running) {
779 virtio_queue_set_notification(vq, 1);
780 qemu_del_timer(n->tx_timer);
782 virtio_net_flush_tx(n, vq);
784 qemu_mod_timer(n->tx_timer,
785 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
787 virtio_queue_set_notification(vq, 0);
791 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
793 VirtIONet *n = to_virtio_net(vdev);
795 if (unlikely(n->tx_waiting)) {
799 /* This happens when device was stopped but VCPU wasn't. */
800 if (!n->vdev.vm_running) {
803 virtio_queue_set_notification(vq, 0);
804 qemu_bh_schedule(n->tx_bh);
807 static void virtio_net_tx_timer(void *opaque)
809 VirtIONet *n = opaque;
810 assert(n->vdev.vm_running);
814 /* Just in case the driver is not ready on more */
815 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
818 virtio_queue_set_notification(n->tx_vq, 1);
819 virtio_net_flush_tx(n, n->tx_vq);
822 static void virtio_net_tx_bh(void *opaque)
824 VirtIONet *n = opaque;
827 assert(n->vdev.vm_running);
831 /* Just in case the driver is not ready on more */
832 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
835 ret = virtio_net_flush_tx(n, n->tx_vq);
837 return; /* Notification re-enable handled by tx_complete */
840 /* If we flush a full burst of packets, assume there are
841 * more coming and immediately reschedule */
842 if (ret >= n->tx_burst) {
843 qemu_bh_schedule(n->tx_bh);
848 /* If less than a full burst, re-enable notification and flush
849 * anything that may have come in while we weren't looking. If
850 * we find something, assume the guest is still active and reschedule */
851 virtio_queue_set_notification(n->tx_vq, 1);
852 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
853 virtio_queue_set_notification(n->tx_vq, 0);
854 qemu_bh_schedule(n->tx_bh);
859 static void virtio_net_save(QEMUFile *f, void *opaque)
861 VirtIONet *n = opaque;
863 /* At this point, backend must be stopped, otherwise
864 * it might keep writing to memory. */
865 assert(!n->vhost_started);
866 virtio_save(&n->vdev, f);
868 qemu_put_buffer(f, n->mac, ETH_ALEN);
869 qemu_put_be32(f, n->tx_waiting);
870 qemu_put_be32(f, n->mergeable_rx_bufs);
871 qemu_put_be16(f, n->status);
872 qemu_put_byte(f, n->promisc);
873 qemu_put_byte(f, n->allmulti);
874 qemu_put_be32(f, n->mac_table.in_use);
875 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
876 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
877 qemu_put_be32(f, n->has_vnet_hdr);
878 qemu_put_byte(f, n->mac_table.multi_overflow);
879 qemu_put_byte(f, n->mac_table.uni_overflow);
880 qemu_put_byte(f, n->alluni);
881 qemu_put_byte(f, n->nomulti);
882 qemu_put_byte(f, n->nouni);
883 qemu_put_byte(f, n->nobcast);
884 qemu_put_byte(f, n->has_ufo);
887 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
889 VirtIONet *n = opaque;
893 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
896 ret = virtio_load(&n->vdev, f);
901 qemu_get_buffer(f, n->mac, ETH_ALEN);
902 n->tx_waiting = qemu_get_be32(f);
903 n->mergeable_rx_bufs = qemu_get_be32(f);
904 n->guest_hdr_len = n->mergeable_rx_bufs ?
905 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
908 n->status = qemu_get_be16(f);
910 if (version_id >= 4) {
911 if (version_id < 8) {
912 n->promisc = qemu_get_be32(f);
913 n->allmulti = qemu_get_be32(f);
915 n->promisc = qemu_get_byte(f);
916 n->allmulti = qemu_get_byte(f);
920 if (version_id >= 5) {
921 n->mac_table.in_use = qemu_get_be32(f);
922 /* MAC_TABLE_ENTRIES may be different from the saved image */
923 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
924 qemu_get_buffer(f, n->mac_table.macs,
925 n->mac_table.in_use * ETH_ALEN);
926 } else if (n->mac_table.in_use) {
927 uint8_t *buf = g_malloc0(n->mac_table.in_use);
928 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
930 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
931 n->mac_table.in_use = 0;
936 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
938 if (version_id >= 7) {
939 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
940 error_report("virtio-net: saved image requires vnet_hdr=on");
944 if (n->has_vnet_hdr) {
945 tap_using_vnet_hdr(n->nic->nc.peer, 1);
946 n->host_hdr_len = sizeof(struct virtio_net_hdr);
947 tap_set_offload(n->nic->nc.peer,
948 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
949 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
950 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
951 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
952 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
956 if (version_id >= 9) {
957 n->mac_table.multi_overflow = qemu_get_byte(f);
958 n->mac_table.uni_overflow = qemu_get_byte(f);
961 if (version_id >= 10) {
962 n->alluni = qemu_get_byte(f);
963 n->nomulti = qemu_get_byte(f);
964 n->nouni = qemu_get_byte(f);
965 n->nobcast = qemu_get_byte(f);
968 if (version_id >= 11) {
969 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
970 error_report("virtio-net: saved image requires TUN_F_UFO support");
975 /* Find the first multicast entry in the saved MAC filter */
976 for (i = 0; i < n->mac_table.in_use; i++) {
977 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
981 n->mac_table.first_multi = i;
983 /* nc.link_down can't be migrated, so infer link_down according
984 * to link status bit in n->status */
985 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
990 static void virtio_net_cleanup(NetClientState *nc)
992 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
997 static NetClientInfo net_virtio_info = {
998 .type = NET_CLIENT_OPTIONS_KIND_NIC,
999 .size = sizeof(NICState),
1000 .can_receive = virtio_net_can_receive,
1001 .receive = virtio_net_receive,
1002 .cleanup = virtio_net_cleanup,
1003 .link_status_changed = virtio_net_set_link_status,
1006 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1007 virtio_net_conf *net)
1011 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1012 sizeof(struct virtio_net_config),
1015 n->vdev.get_config = virtio_net_get_config;
1016 n->vdev.set_config = virtio_net_set_config;
1017 n->vdev.get_features = virtio_net_get_features;
1018 n->vdev.set_features = virtio_net_set_features;
1019 n->vdev.bad_features = virtio_net_bad_features;
1020 n->vdev.reset = virtio_net_reset;
1021 n->vdev.set_status = virtio_net_set_status;
1022 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1024 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
1025 error_report("virtio-net: "
1026 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1028 error_report("Defaulting to \"bh\"");
1031 if (net->tx && !strcmp(net->tx, "timer")) {
1032 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1033 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
1034 n->tx_timeout = net->txtimer;
1036 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1037 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1039 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1040 qemu_macaddr_default_if_unset(&conf->macaddr);
1041 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1042 n->status = VIRTIO_NET_S_LINK_UP;
1044 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
1046 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1049 n->tx_burst = net->txburst;
1050 n->mergeable_rx_bufs = 0;
1051 n->guest_hdr_len = sizeof(struct virtio_net_hdr);
1052 n->promisc = 1; /* for compatibility */
1054 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1056 n->vlans = g_malloc0(MAX_VLAN >> 3);
1059 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1060 virtio_net_save, virtio_net_load, n);
1062 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1067 void virtio_net_exit(VirtIODevice *vdev)
1069 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1071 /* This will stop vhost backend if appropriate. */
1072 virtio_net_set_status(vdev, 0);
1074 qemu_purge_queued_packets(&n->nic->nc);
1076 unregister_savevm(n->qdev, "virtio-net", n);
1078 g_free(n->mac_table.macs);
1082 qemu_del_timer(n->tx_timer);
1083 qemu_free_timer(n->tx_timer);
1085 qemu_bh_delete(n->tx_bh);
1088 qemu_del_net_client(&n->nic->nc);
1089 virtio_cleanup(&n->vdev);