failover: Rename to failover_find_primary_device()
[qemu/ar7.git] / hw / net / virtio-net.c
blobff82f1017dbbb495b6f4adaa20f390d6d55c88aa
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 "qemu/osdep.h"
15 #include "qemu/atomic.h"
16 #include "qemu/iov.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
19 #include "hw/virtio/virtio.h"
20 #include "net/net.h"
21 #include "net/checksum.h"
22 #include "net/tap.h"
23 #include "qemu/error-report.h"
24 #include "qemu/timer.h"
25 #include "qemu/option.h"
26 #include "qemu/option_int.h"
27 #include "qemu/config-file.h"
28 #include "qapi/qmp/qdict.h"
29 #include "hw/virtio/virtio-net.h"
30 #include "net/vhost_net.h"
31 #include "net/announce.h"
32 #include "hw/virtio/virtio-bus.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-net.h"
35 #include "hw/qdev-properties.h"
36 #include "qapi/qapi-types-migration.h"
37 #include "qapi/qapi-events-migration.h"
38 #include "hw/virtio/virtio-access.h"
39 #include "migration/misc.h"
40 #include "standard-headers/linux/ethtool.h"
41 #include "sysemu/sysemu.h"
42 #include "trace.h"
43 #include "monitor/qdev.h"
44 #include "hw/pci/pci.h"
45 #include "net_rx_pkt.h"
46 #include "hw/virtio/vhost.h"
48 #define VIRTIO_NET_VM_VERSION 11
50 #define MAC_TABLE_ENTRIES 64
51 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
53 /* previously fixed value */
54 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
55 #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
57 /* for now, only allow larger queues; with virtio-1, guest can downsize */
58 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
59 #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
61 #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */
63 #define VIRTIO_NET_TCP_FLAG 0x3F
64 #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000
66 /* IPv4 max payload, 16 bits in the header */
67 #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
68 #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
70 /* header length value in ip header without option */
71 #define VIRTIO_NET_IP4_HEADER_LENGTH 5
73 #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */
74 #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
76 /* Purge coalesced packets timer interval, This value affects the performance
77 a lot, and should be tuned carefully, '300000'(300us) is the recommended
78 value to pass the WHQL test, '50000' can gain 2x netperf throughput with
79 tso/gso/gro 'off'. */
80 #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
82 #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
83 VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
84 VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
85 VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
86 VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
87 VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
88 VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
89 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
90 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
92 static VirtIOFeature feature_sizes[] = {
93 {.flags = 1ULL << VIRTIO_NET_F_MAC,
94 .end = endof(struct virtio_net_config, mac)},
95 {.flags = 1ULL << VIRTIO_NET_F_STATUS,
96 .end = endof(struct virtio_net_config, status)},
97 {.flags = 1ULL << VIRTIO_NET_F_MQ,
98 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
99 {.flags = 1ULL << VIRTIO_NET_F_MTU,
100 .end = endof(struct virtio_net_config, mtu)},
101 {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX,
102 .end = endof(struct virtio_net_config, duplex)},
103 {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT),
104 .end = endof(struct virtio_net_config, supported_hash_types)},
108 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
110 VirtIONet *n = qemu_get_nic_opaque(nc);
112 return &n->vqs[nc->queue_index];
115 static int vq2q(int queue_index)
117 return queue_index / 2;
120 /* TODO
121 * - we could suppress RX interrupt if we were so inclined.
124 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
126 VirtIONet *n = VIRTIO_NET(vdev);
127 struct virtio_net_config netcfg;
128 NetClientState *nc = qemu_get_queue(n->nic);
130 int ret = 0;
131 memset(&netcfg, 0 , sizeof(struct virtio_net_config));
132 virtio_stw_p(vdev, &netcfg.status, n->status);
133 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
134 virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
135 memcpy(netcfg.mac, n->mac, ETH_ALEN);
136 virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed);
137 netcfg.duplex = n->net_conf.duplex;
138 netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE;
139 virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length,
140 virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ?
141 VIRTIO_NET_RSS_MAX_TABLE_LEN : 1);
142 virtio_stl_p(vdev, &netcfg.supported_hash_types,
143 VIRTIO_NET_RSS_SUPPORTED_HASHES);
144 memcpy(config, &netcfg, n->config_size);
147 * Is this VDPA? No peer means not VDPA: there's no way to
148 * disconnect/reconnect a VDPA peer.
150 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
151 ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
152 n->config_size);
153 if (ret != -1) {
154 memcpy(config, &netcfg, n->config_size);
159 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
161 VirtIONet *n = VIRTIO_NET(vdev);
162 struct virtio_net_config netcfg = {};
163 NetClientState *nc = qemu_get_queue(n->nic);
165 memcpy(&netcfg, config, n->config_size);
167 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
168 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
169 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
170 memcpy(n->mac, netcfg.mac, ETH_ALEN);
171 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
175 * Is this VDPA? No peer means not VDPA: there's no way to
176 * disconnect/reconnect a VDPA peer.
178 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
179 vhost_net_set_config(get_vhost_net(nc->peer),
180 (uint8_t *)&netcfg, 0, n->config_size,
181 VHOST_SET_CONFIG_TYPE_MASTER);
185 static bool virtio_net_started(VirtIONet *n, uint8_t status)
187 VirtIODevice *vdev = VIRTIO_DEVICE(n);
188 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
189 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
192 static void virtio_net_announce_notify(VirtIONet *net)
194 VirtIODevice *vdev = VIRTIO_DEVICE(net);
195 trace_virtio_net_announce_notify();
197 net->status |= VIRTIO_NET_S_ANNOUNCE;
198 virtio_notify_config(vdev);
201 static void virtio_net_announce_timer(void *opaque)
203 VirtIONet *n = opaque;
204 trace_virtio_net_announce_timer(n->announce_timer.round);
206 n->announce_timer.round--;
207 virtio_net_announce_notify(n);
210 static void virtio_net_announce(NetClientState *nc)
212 VirtIONet *n = qemu_get_nic_opaque(nc);
213 VirtIODevice *vdev = VIRTIO_DEVICE(n);
216 * Make sure the virtio migration announcement timer isn't running
217 * If it is, let it trigger announcement so that we do not cause
218 * confusion.
220 if (n->announce_timer.round) {
221 return;
224 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
225 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
226 virtio_net_announce_notify(n);
230 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
232 VirtIODevice *vdev = VIRTIO_DEVICE(n);
233 NetClientState *nc = qemu_get_queue(n->nic);
234 int queues = n->multiqueue ? n->max_queues : 1;
236 if (!get_vhost_net(nc->peer)) {
237 return;
240 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
241 !!n->vhost_started) {
242 return;
244 if (!n->vhost_started) {
245 int r, i;
247 if (n->needs_vnet_hdr_swap) {
248 error_report("backend does not support %s vnet headers; "
249 "falling back on userspace virtio",
250 virtio_is_big_endian(vdev) ? "BE" : "LE");
251 return;
254 /* Any packets outstanding? Purge them to avoid touching rings
255 * when vhost is running.
257 for (i = 0; i < queues; i++) {
258 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
260 /* Purge both directions: TX and RX. */
261 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
262 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
265 if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
266 r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
267 if (r < 0) {
268 error_report("%uBytes MTU not supported by the backend",
269 n->net_conf.mtu);
271 return;
275 n->vhost_started = 1;
276 r = vhost_net_start(vdev, n->nic->ncs, queues);
277 if (r < 0) {
278 error_report("unable to start vhost net: %d: "
279 "falling back on userspace virtio", -r);
280 n->vhost_started = 0;
282 } else {
283 vhost_net_stop(vdev, n->nic->ncs, queues);
284 n->vhost_started = 0;
288 static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
289 NetClientState *peer,
290 bool enable)
292 if (virtio_is_big_endian(vdev)) {
293 return qemu_set_vnet_be(peer, enable);
294 } else {
295 return qemu_set_vnet_le(peer, enable);
299 static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
300 int queues, bool enable)
302 int i;
304 for (i = 0; i < queues; i++) {
305 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
306 enable) {
307 while (--i >= 0) {
308 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
311 return true;
315 return false;
318 static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
320 VirtIODevice *vdev = VIRTIO_DEVICE(n);
321 int queues = n->multiqueue ? n->max_queues : 1;
323 if (virtio_net_started(n, status)) {
324 /* Before using the device, we tell the network backend about the
325 * endianness to use when parsing vnet headers. If the backend
326 * can't do it, we fallback onto fixing the headers in the core
327 * virtio-net code.
329 n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
330 queues, true);
331 } else if (virtio_net_started(n, vdev->status)) {
332 /* After using the device, we need to reset the network backend to
333 * the default (guest native endianness), otherwise the guest may
334 * lose network connectivity if it is rebooted into a different
335 * endianness.
337 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
341 static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
343 unsigned int dropped = virtqueue_drop_all(vq);
344 if (dropped) {
345 virtio_notify(vdev, vq);
349 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
351 VirtIONet *n = VIRTIO_NET(vdev);
352 VirtIONetQueue *q;
353 int i;
354 uint8_t queue_status;
356 virtio_net_vnet_endian_status(n, status);
357 virtio_net_vhost_status(n, status);
359 for (i = 0; i < n->max_queues; i++) {
360 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
361 bool queue_started;
362 q = &n->vqs[i];
364 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
365 queue_status = 0;
366 } else {
367 queue_status = status;
369 queue_started =
370 virtio_net_started(n, queue_status) && !n->vhost_started;
372 if (queue_started) {
373 qemu_flush_queued_packets(ncs);
376 if (!q->tx_waiting) {
377 continue;
380 if (queue_started) {
381 if (q->tx_timer) {
382 timer_mod(q->tx_timer,
383 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
384 } else {
385 qemu_bh_schedule(q->tx_bh);
387 } else {
388 if (q->tx_timer) {
389 timer_del(q->tx_timer);
390 } else {
391 qemu_bh_cancel(q->tx_bh);
393 if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
394 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
395 vdev->vm_running) {
396 /* if tx is waiting we are likely have some packets in tx queue
397 * and disabled notification */
398 q->tx_waiting = 0;
399 virtio_queue_set_notification(q->tx_vq, 1);
400 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
406 static void virtio_net_set_link_status(NetClientState *nc)
408 VirtIONet *n = qemu_get_nic_opaque(nc);
409 VirtIODevice *vdev = VIRTIO_DEVICE(n);
410 uint16_t old_status = n->status;
412 if (nc->link_down)
413 n->status &= ~VIRTIO_NET_S_LINK_UP;
414 else
415 n->status |= VIRTIO_NET_S_LINK_UP;
417 if (n->status != old_status)
418 virtio_notify_config(vdev);
420 virtio_net_set_status(vdev, vdev->status);
423 static void rxfilter_notify(NetClientState *nc)
425 VirtIONet *n = qemu_get_nic_opaque(nc);
427 if (nc->rxfilter_notify_enabled) {
428 char *path = object_get_canonical_path(OBJECT(n->qdev));
429 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
430 n->netclient_name, path);
431 g_free(path);
433 /* disable event notification to avoid events flooding */
434 nc->rxfilter_notify_enabled = 0;
438 static intList *get_vlan_table(VirtIONet *n)
440 intList *list, *entry;
441 int i, j;
443 list = NULL;
444 for (i = 0; i < MAX_VLAN >> 5; i++) {
445 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
446 if (n->vlans[i] & (1U << j)) {
447 entry = g_malloc0(sizeof(*entry));
448 entry->value = (i << 5) + j;
449 entry->next = list;
450 list = entry;
455 return list;
458 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
460 VirtIONet *n = qemu_get_nic_opaque(nc);
461 VirtIODevice *vdev = VIRTIO_DEVICE(n);
462 RxFilterInfo *info;
463 strList *str_list, *entry;
464 int i;
466 info = g_malloc0(sizeof(*info));
467 info->name = g_strdup(nc->name);
468 info->promiscuous = n->promisc;
470 if (n->nouni) {
471 info->unicast = RX_STATE_NONE;
472 } else if (n->alluni) {
473 info->unicast = RX_STATE_ALL;
474 } else {
475 info->unicast = RX_STATE_NORMAL;
478 if (n->nomulti) {
479 info->multicast = RX_STATE_NONE;
480 } else if (n->allmulti) {
481 info->multicast = RX_STATE_ALL;
482 } else {
483 info->multicast = RX_STATE_NORMAL;
486 info->broadcast_allowed = n->nobcast;
487 info->multicast_overflow = n->mac_table.multi_overflow;
488 info->unicast_overflow = n->mac_table.uni_overflow;
490 info->main_mac = qemu_mac_strdup_printf(n->mac);
492 str_list = NULL;
493 for (i = 0; i < n->mac_table.first_multi; i++) {
494 entry = g_malloc0(sizeof(*entry));
495 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
496 entry->next = str_list;
497 str_list = entry;
499 info->unicast_table = str_list;
501 str_list = NULL;
502 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
503 entry = g_malloc0(sizeof(*entry));
504 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
505 entry->next = str_list;
506 str_list = entry;
508 info->multicast_table = str_list;
509 info->vlan_table = get_vlan_table(n);
511 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
512 info->vlan = RX_STATE_ALL;
513 } else if (!info->vlan_table) {
514 info->vlan = RX_STATE_NONE;
515 } else {
516 info->vlan = RX_STATE_NORMAL;
519 /* enable event notification after query */
520 nc->rxfilter_notify_enabled = 1;
522 return info;
525 static void virtio_net_reset(VirtIODevice *vdev)
527 VirtIONet *n = VIRTIO_NET(vdev);
528 int i;
530 /* Reset back to compatibility mode */
531 n->promisc = 1;
532 n->allmulti = 0;
533 n->alluni = 0;
534 n->nomulti = 0;
535 n->nouni = 0;
536 n->nobcast = 0;
537 /* multiqueue is disabled by default */
538 n->curr_queues = 1;
539 timer_del(n->announce_timer.tm);
540 n->announce_timer.round = 0;
541 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
543 /* Flush any MAC and VLAN filter table state */
544 n->mac_table.in_use = 0;
545 n->mac_table.first_multi = 0;
546 n->mac_table.multi_overflow = 0;
547 n->mac_table.uni_overflow = 0;
548 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
549 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
550 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
551 memset(n->vlans, 0, MAX_VLAN >> 3);
553 /* Flush any async TX */
554 for (i = 0; i < n->max_queues; i++) {
555 NetClientState *nc = qemu_get_subqueue(n->nic, i);
557 if (nc->peer) {
558 qemu_flush_or_purge_queued_packets(nc->peer, true);
559 assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
564 static void peer_test_vnet_hdr(VirtIONet *n)
566 NetClientState *nc = qemu_get_queue(n->nic);
567 if (!nc->peer) {
568 return;
571 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
574 static int peer_has_vnet_hdr(VirtIONet *n)
576 return n->has_vnet_hdr;
579 static int peer_has_ufo(VirtIONet *n)
581 if (!peer_has_vnet_hdr(n))
582 return 0;
584 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
586 return n->has_ufo;
589 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
590 int version_1, int hash_report)
592 int i;
593 NetClientState *nc;
595 n->mergeable_rx_bufs = mergeable_rx_bufs;
597 if (version_1) {
598 n->guest_hdr_len = hash_report ?
599 sizeof(struct virtio_net_hdr_v1_hash) :
600 sizeof(struct virtio_net_hdr_mrg_rxbuf);
601 n->rss_data.populate_hash = !!hash_report;
602 } else {
603 n->guest_hdr_len = n->mergeable_rx_bufs ?
604 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
605 sizeof(struct virtio_net_hdr);
608 for (i = 0; i < n->max_queues; i++) {
609 nc = qemu_get_subqueue(n->nic, i);
611 if (peer_has_vnet_hdr(n) &&
612 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
613 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
614 n->host_hdr_len = n->guest_hdr_len;
619 static int virtio_net_max_tx_queue_size(VirtIONet *n)
621 NetClientState *peer = n->nic_conf.peers.ncs[0];
624 * Backends other than vhost-user don't support max queue size.
626 if (!peer) {
627 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
630 if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
631 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
634 return VIRTQUEUE_MAX_SIZE;
637 static int peer_attach(VirtIONet *n, int index)
639 NetClientState *nc = qemu_get_subqueue(n->nic, index);
641 if (!nc->peer) {
642 return 0;
645 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
646 vhost_set_vring_enable(nc->peer, 1);
649 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
650 return 0;
653 if (n->max_queues == 1) {
654 return 0;
657 return tap_enable(nc->peer);
660 static int peer_detach(VirtIONet *n, int index)
662 NetClientState *nc = qemu_get_subqueue(n->nic, index);
664 if (!nc->peer) {
665 return 0;
668 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
669 vhost_set_vring_enable(nc->peer, 0);
672 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
673 return 0;
676 return tap_disable(nc->peer);
679 static void virtio_net_set_queues(VirtIONet *n)
681 int i;
682 int r;
684 if (n->nic->peer_deleted) {
685 return;
688 for (i = 0; i < n->max_queues; i++) {
689 if (i < n->curr_queues) {
690 r = peer_attach(n, i);
691 assert(!r);
692 } else {
693 r = peer_detach(n, i);
694 assert(!r);
699 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
701 static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
702 Error **errp)
704 VirtIONet *n = VIRTIO_NET(vdev);
705 NetClientState *nc = qemu_get_queue(n->nic);
707 /* Firstly sync all virtio-net possible supported features */
708 features |= n->host_features;
710 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
712 if (!peer_has_vnet_hdr(n)) {
713 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
714 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
715 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
716 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
718 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
719 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
720 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
721 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
723 virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
726 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
727 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
728 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
731 if (!get_vhost_net(nc->peer)) {
732 return features;
735 virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
736 virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
737 features = vhost_net_get_features(get_vhost_net(nc->peer), features);
738 vdev->backend_features = features;
740 if (n->mtu_bypass_backend &&
741 (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
742 features |= (1ULL << VIRTIO_NET_F_MTU);
745 return features;
748 static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
750 uint64_t features = 0;
752 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
753 * but also these: */
754 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
755 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
756 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
757 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
758 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
760 return features;
763 static void virtio_net_apply_guest_offloads(VirtIONet *n)
765 qemu_set_offload(qemu_get_queue(n->nic)->peer,
766 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
767 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
768 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
769 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
770 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
773 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
775 static const uint64_t guest_offloads_mask =
776 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
777 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
778 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
779 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
780 (1ULL << VIRTIO_NET_F_GUEST_UFO);
782 return guest_offloads_mask & features;
785 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
787 VirtIODevice *vdev = VIRTIO_DEVICE(n);
788 return virtio_net_guest_offloads_by_features(vdev->guest_features);
791 static void failover_add_primary(VirtIONet *n, Error **errp)
793 Error *err = NULL;
794 QemuOpts *opts;
796 if (n->primary_dev) {
797 return;
800 opts = qemu_opts_find(qemu_find_opts("device"), n->primary_device_id);
801 if (opts) {
802 n->primary_dev = qdev_device_add(opts, &err);
803 if (err) {
804 qemu_opts_del(opts);
806 } else {
807 error_setg(errp, "Primary device not found");
808 error_append_hint(errp, "Virtio-net failover will not work. Make "
809 "sure primary device has parameter"
810 " failover_pair_id=<virtio-net-id>\n");
812 error_propagate(errp, err);
815 static int is_my_primary(void *opaque, QemuOpts *opts, Error **errp)
817 VirtIONet *n = opaque;
818 int ret = 0;
819 const char *standby_id = qemu_opt_get(opts, "failover_pair_id");
821 if (g_strcmp0(standby_id, n->netclient_name) == 0) {
822 n->primary_device_id = g_strdup(opts->id);
823 ret = 1;
826 return ret;
830 * Find the primary device for this failover virtio-net
832 * @n: VirtIONet device
833 * @errp: returns an error if this function fails
835 static DeviceState *failover_find_primary_device(VirtIONet *n, Error **errp)
837 Error *err = NULL;
839 if (!qemu_opts_foreach(qemu_find_opts("device"), is_my_primary, n, &err)) {
840 return NULL;
842 return qdev_find_recursive(sysbus_get_default(), n->primary_device_id);
845 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
847 VirtIONet *n = VIRTIO_NET(vdev);
848 Error *err = NULL;
849 int i;
851 if (n->mtu_bypass_backend &&
852 !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
853 features &= ~(1ULL << VIRTIO_NET_F_MTU);
856 virtio_net_set_multiqueue(n,
857 virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
858 virtio_has_feature(features, VIRTIO_NET_F_MQ));
860 virtio_net_set_mrg_rx_bufs(n,
861 virtio_has_feature(features,
862 VIRTIO_NET_F_MRG_RXBUF),
863 virtio_has_feature(features,
864 VIRTIO_F_VERSION_1),
865 virtio_has_feature(features,
866 VIRTIO_NET_F_HASH_REPORT));
868 n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
869 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
870 n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
871 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
872 n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
874 if (n->has_vnet_hdr) {
875 n->curr_guest_offloads =
876 virtio_net_guest_offloads_by_features(features);
877 virtio_net_apply_guest_offloads(n);
880 for (i = 0; i < n->max_queues; i++) {
881 NetClientState *nc = qemu_get_subqueue(n->nic, i);
883 if (!get_vhost_net(nc->peer)) {
884 continue;
886 vhost_net_ack_features(get_vhost_net(nc->peer), features);
889 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
890 memset(n->vlans, 0, MAX_VLAN >> 3);
891 } else {
892 memset(n->vlans, 0xff, MAX_VLAN >> 3);
895 if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
896 qapi_event_send_failover_negotiated(n->netclient_name);
897 qatomic_set(&n->failover_primary_hidden, false);
898 failover_add_primary(n, &err);
899 if (err) {
900 n->primary_dev = failover_find_primary_device(n, &err);
901 if (err) {
902 goto out_err;
904 failover_add_primary(n, &err);
905 if (err) {
906 goto out_err;
910 return;
912 out_err:
913 if (err) {
914 warn_report_err(err);
918 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
919 struct iovec *iov, unsigned int iov_cnt)
921 uint8_t on;
922 size_t s;
923 NetClientState *nc = qemu_get_queue(n->nic);
925 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
926 if (s != sizeof(on)) {
927 return VIRTIO_NET_ERR;
930 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
931 n->promisc = on;
932 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
933 n->allmulti = on;
934 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
935 n->alluni = on;
936 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
937 n->nomulti = on;
938 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
939 n->nouni = on;
940 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
941 n->nobcast = on;
942 } else {
943 return VIRTIO_NET_ERR;
946 rxfilter_notify(nc);
948 return VIRTIO_NET_OK;
951 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
952 struct iovec *iov, unsigned int iov_cnt)
954 VirtIODevice *vdev = VIRTIO_DEVICE(n);
955 uint64_t offloads;
956 size_t s;
958 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
959 return VIRTIO_NET_ERR;
962 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
963 if (s != sizeof(offloads)) {
964 return VIRTIO_NET_ERR;
967 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
968 uint64_t supported_offloads;
970 offloads = virtio_ldq_p(vdev, &offloads);
972 if (!n->has_vnet_hdr) {
973 return VIRTIO_NET_ERR;
976 n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
977 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
978 n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
979 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
980 virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
982 supported_offloads = virtio_net_supported_guest_offloads(n);
983 if (offloads & ~supported_offloads) {
984 return VIRTIO_NET_ERR;
987 n->curr_guest_offloads = offloads;
988 virtio_net_apply_guest_offloads(n);
990 return VIRTIO_NET_OK;
991 } else {
992 return VIRTIO_NET_ERR;
996 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
997 struct iovec *iov, unsigned int iov_cnt)
999 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1000 struct virtio_net_ctrl_mac mac_data;
1001 size_t s;
1002 NetClientState *nc = qemu_get_queue(n->nic);
1004 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
1005 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
1006 return VIRTIO_NET_ERR;
1008 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
1009 assert(s == sizeof(n->mac));
1010 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1011 rxfilter_notify(nc);
1013 return VIRTIO_NET_OK;
1016 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
1017 return VIRTIO_NET_ERR;
1020 int in_use = 0;
1021 int first_multi = 0;
1022 uint8_t uni_overflow = 0;
1023 uint8_t multi_overflow = 0;
1024 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1026 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1027 sizeof(mac_data.entries));
1028 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1029 if (s != sizeof(mac_data.entries)) {
1030 goto error;
1032 iov_discard_front(&iov, &iov_cnt, s);
1034 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1035 goto error;
1038 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1039 s = iov_to_buf(iov, iov_cnt, 0, macs,
1040 mac_data.entries * ETH_ALEN);
1041 if (s != mac_data.entries * ETH_ALEN) {
1042 goto error;
1044 in_use += mac_data.entries;
1045 } else {
1046 uni_overflow = 1;
1049 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
1051 first_multi = in_use;
1053 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1054 sizeof(mac_data.entries));
1055 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1056 if (s != sizeof(mac_data.entries)) {
1057 goto error;
1060 iov_discard_front(&iov, &iov_cnt, s);
1062 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1063 goto error;
1066 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1067 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
1068 mac_data.entries * ETH_ALEN);
1069 if (s != mac_data.entries * ETH_ALEN) {
1070 goto error;
1072 in_use += mac_data.entries;
1073 } else {
1074 multi_overflow = 1;
1077 n->mac_table.in_use = in_use;
1078 n->mac_table.first_multi = first_multi;
1079 n->mac_table.uni_overflow = uni_overflow;
1080 n->mac_table.multi_overflow = multi_overflow;
1081 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1082 g_free(macs);
1083 rxfilter_notify(nc);
1085 return VIRTIO_NET_OK;
1087 error:
1088 g_free(macs);
1089 return VIRTIO_NET_ERR;
1092 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
1093 struct iovec *iov, unsigned int iov_cnt)
1095 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1096 uint16_t vid;
1097 size_t s;
1098 NetClientState *nc = qemu_get_queue(n->nic);
1100 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
1101 vid = virtio_lduw_p(vdev, &vid);
1102 if (s != sizeof(vid)) {
1103 return VIRTIO_NET_ERR;
1106 if (vid >= MAX_VLAN)
1107 return VIRTIO_NET_ERR;
1109 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
1110 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
1111 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
1112 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
1113 else
1114 return VIRTIO_NET_ERR;
1116 rxfilter_notify(nc);
1118 return VIRTIO_NET_OK;
1121 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1122 struct iovec *iov, unsigned int iov_cnt)
1124 trace_virtio_net_handle_announce(n->announce_timer.round);
1125 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1126 n->status & VIRTIO_NET_S_ANNOUNCE) {
1127 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
1128 if (n->announce_timer.round) {
1129 qemu_announce_timer_step(&n->announce_timer);
1131 return VIRTIO_NET_OK;
1132 } else {
1133 return VIRTIO_NET_ERR;
1137 static void virtio_net_disable_rss(VirtIONet *n)
1139 if (n->rss_data.enabled) {
1140 trace_virtio_net_rss_disable();
1142 n->rss_data.enabled = false;
1145 static uint16_t virtio_net_handle_rss(VirtIONet *n,
1146 struct iovec *iov,
1147 unsigned int iov_cnt,
1148 bool do_rss)
1150 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1151 struct virtio_net_rss_config cfg;
1152 size_t s, offset = 0, size_get;
1153 uint16_t queues, i;
1154 struct {
1155 uint16_t us;
1156 uint8_t b;
1157 } QEMU_PACKED temp;
1158 const char *err_msg = "";
1159 uint32_t err_value = 0;
1161 if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
1162 err_msg = "RSS is not negotiated";
1163 goto error;
1165 if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1166 err_msg = "Hash report is not negotiated";
1167 goto error;
1169 size_get = offsetof(struct virtio_net_rss_config, indirection_table);
1170 s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
1171 if (s != size_get) {
1172 err_msg = "Short command buffer";
1173 err_value = (uint32_t)s;
1174 goto error;
1176 n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
1177 n->rss_data.indirections_len =
1178 virtio_lduw_p(vdev, &cfg.indirection_table_mask);
1179 n->rss_data.indirections_len++;
1180 if (!do_rss) {
1181 n->rss_data.indirections_len = 1;
1183 if (!is_power_of_2(n->rss_data.indirections_len)) {
1184 err_msg = "Invalid size of indirection table";
1185 err_value = n->rss_data.indirections_len;
1186 goto error;
1188 if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
1189 err_msg = "Too large indirection table";
1190 err_value = n->rss_data.indirections_len;
1191 goto error;
1193 n->rss_data.default_queue = do_rss ?
1194 virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1195 if (n->rss_data.default_queue >= n->max_queues) {
1196 err_msg = "Invalid default queue";
1197 err_value = n->rss_data.default_queue;
1198 goto error;
1200 offset += size_get;
1201 size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
1202 g_free(n->rss_data.indirections_table);
1203 n->rss_data.indirections_table = g_malloc(size_get);
1204 if (!n->rss_data.indirections_table) {
1205 err_msg = "Can't allocate indirections table";
1206 err_value = n->rss_data.indirections_len;
1207 goto error;
1209 s = iov_to_buf(iov, iov_cnt, offset,
1210 n->rss_data.indirections_table, size_get);
1211 if (s != size_get) {
1212 err_msg = "Short indirection table buffer";
1213 err_value = (uint32_t)s;
1214 goto error;
1216 for (i = 0; i < n->rss_data.indirections_len; ++i) {
1217 uint16_t val = n->rss_data.indirections_table[i];
1218 n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
1220 offset += size_get;
1221 size_get = sizeof(temp);
1222 s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
1223 if (s != size_get) {
1224 err_msg = "Can't get queues";
1225 err_value = (uint32_t)s;
1226 goto error;
1228 queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues;
1229 if (queues == 0 || queues > n->max_queues) {
1230 err_msg = "Invalid number of queues";
1231 err_value = queues;
1232 goto error;
1234 if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
1235 err_msg = "Invalid key size";
1236 err_value = temp.b;
1237 goto error;
1239 if (!temp.b && n->rss_data.hash_types) {
1240 err_msg = "No key provided";
1241 err_value = 0;
1242 goto error;
1244 if (!temp.b && !n->rss_data.hash_types) {
1245 virtio_net_disable_rss(n);
1246 return queues;
1248 offset += size_get;
1249 size_get = temp.b;
1250 s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
1251 if (s != size_get) {
1252 err_msg = "Can get key buffer";
1253 err_value = (uint32_t)s;
1254 goto error;
1256 n->rss_data.enabled = true;
1257 trace_virtio_net_rss_enable(n->rss_data.hash_types,
1258 n->rss_data.indirections_len,
1259 temp.b);
1260 return queues;
1261 error:
1262 trace_virtio_net_rss_error(err_msg, err_value);
1263 virtio_net_disable_rss(n);
1264 return 0;
1267 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
1268 struct iovec *iov, unsigned int iov_cnt)
1270 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1271 uint16_t queues;
1273 virtio_net_disable_rss(n);
1274 if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1275 queues = virtio_net_handle_rss(n, iov, iov_cnt, false);
1276 return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1278 if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1279 queues = virtio_net_handle_rss(n, iov, iov_cnt, true);
1280 } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
1281 struct virtio_net_ctrl_mq mq;
1282 size_t s;
1283 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
1284 return VIRTIO_NET_ERR;
1286 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
1287 if (s != sizeof(mq)) {
1288 return VIRTIO_NET_ERR;
1290 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
1292 } else {
1293 return VIRTIO_NET_ERR;
1296 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1297 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1298 queues > n->max_queues ||
1299 !n->multiqueue) {
1300 return VIRTIO_NET_ERR;
1303 n->curr_queues = queues;
1304 /* stop the backend before changing the number of queues to avoid handling a
1305 * disabled queue */
1306 virtio_net_set_status(vdev, vdev->status);
1307 virtio_net_set_queues(n);
1309 return VIRTIO_NET_OK;
1312 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1314 VirtIONet *n = VIRTIO_NET(vdev);
1315 struct virtio_net_ctrl_hdr ctrl;
1316 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1317 VirtQueueElement *elem;
1318 size_t s;
1319 struct iovec *iov, *iov2;
1320 unsigned int iov_cnt;
1322 for (;;) {
1323 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
1324 if (!elem) {
1325 break;
1327 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
1328 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
1329 virtio_error(vdev, "virtio-net ctrl missing headers");
1330 virtqueue_detach_element(vq, elem, 0);
1331 g_free(elem);
1332 break;
1335 iov_cnt = elem->out_num;
1336 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
1337 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
1338 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
1339 if (s != sizeof(ctrl)) {
1340 status = VIRTIO_NET_ERR;
1341 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1342 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
1343 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1344 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
1345 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1346 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
1347 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1348 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
1349 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1350 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
1351 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1352 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
1355 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
1356 assert(s == sizeof(status));
1358 virtqueue_push(vq, elem, sizeof(status));
1359 virtio_notify(vdev, vq);
1360 g_free(iov2);
1361 g_free(elem);
1365 /* RX */
1367 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
1369 VirtIONet *n = VIRTIO_NET(vdev);
1370 int queue_index = vq2q(virtio_get_queue_index(vq));
1372 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
1375 static bool virtio_net_can_receive(NetClientState *nc)
1377 VirtIONet *n = qemu_get_nic_opaque(nc);
1378 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1379 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1381 if (!vdev->vm_running) {
1382 return false;
1385 if (nc->queue_index >= n->curr_queues) {
1386 return false;
1389 if (!virtio_queue_ready(q->rx_vq) ||
1390 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1391 return false;
1394 return true;
1397 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
1399 VirtIONet *n = q->n;
1400 if (virtio_queue_empty(q->rx_vq) ||
1401 (n->mergeable_rx_bufs &&
1402 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1403 virtio_queue_set_notification(q->rx_vq, 1);
1405 /* To avoid a race condition where the guest has made some buffers
1406 * available after the above check but before notification was
1407 * enabled, check for available buffers again.
1409 if (virtio_queue_empty(q->rx_vq) ||
1410 (n->mergeable_rx_bufs &&
1411 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1412 return 0;
1416 virtio_queue_set_notification(q->rx_vq, 0);
1417 return 1;
1420 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1422 virtio_tswap16s(vdev, &hdr->hdr_len);
1423 virtio_tswap16s(vdev, &hdr->gso_size);
1424 virtio_tswap16s(vdev, &hdr->csum_start);
1425 virtio_tswap16s(vdev, &hdr->csum_offset);
1428 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1429 * it never finds out that the packets don't have valid checksums. This
1430 * causes dhclient to get upset. Fedora's carried a patch for ages to
1431 * fix this with Xen but it hasn't appeared in an upstream release of
1432 * dhclient yet.
1434 * To avoid breaking existing guests, we catch udp packets and add
1435 * checksums. This is terrible but it's better than hacking the guest
1436 * kernels.
1438 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1439 * we should provide a mechanism to disable it to avoid polluting the host
1440 * cache.
1442 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1443 uint8_t *buf, size_t size)
1445 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1446 (size > 27 && size < 1500) && /* normal sized MTU */
1447 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1448 (buf[23] == 17) && /* ip.protocol == UDP */
1449 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1450 net_checksum_calculate(buf, size);
1451 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1455 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1456 const void *buf, size_t size)
1458 if (n->has_vnet_hdr) {
1459 /* FIXME this cast is evil */
1460 void *wbuf = (void *)buf;
1461 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1462 size - n->host_hdr_len);
1464 if (n->needs_vnet_hdr_swap) {
1465 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1467 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1468 } else {
1469 struct virtio_net_hdr hdr = {
1470 .flags = 0,
1471 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1473 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1477 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1479 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1480 static const uint8_t vlan[] = {0x81, 0x00};
1481 uint8_t *ptr = (uint8_t *)buf;
1482 int i;
1484 if (n->promisc)
1485 return 1;
1487 ptr += n->host_hdr_len;
1489 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1490 int vid = lduw_be_p(ptr + 14) & 0xfff;
1491 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1492 return 0;
1495 if (ptr[0] & 1) { // multicast
1496 if (!memcmp(ptr, bcast, sizeof(bcast))) {
1497 return !n->nobcast;
1498 } else if (n->nomulti) {
1499 return 0;
1500 } else if (n->allmulti || n->mac_table.multi_overflow) {
1501 return 1;
1504 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1505 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1506 return 1;
1509 } else { // unicast
1510 if (n->nouni) {
1511 return 0;
1512 } else if (n->alluni || n->mac_table.uni_overflow) {
1513 return 1;
1514 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1515 return 1;
1518 for (i = 0; i < n->mac_table.first_multi; i++) {
1519 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1520 return 1;
1525 return 0;
1528 static uint8_t virtio_net_get_hash_type(bool isip4,
1529 bool isip6,
1530 bool isudp,
1531 bool istcp,
1532 uint32_t types)
1534 if (isip4) {
1535 if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) {
1536 return NetPktRssIpV4Tcp;
1538 if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) {
1539 return NetPktRssIpV4Udp;
1541 if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
1542 return NetPktRssIpV4;
1544 } else if (isip6) {
1545 uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
1546 VIRTIO_NET_RSS_HASH_TYPE_TCPv6;
1548 if (istcp && (types & mask)) {
1549 return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ?
1550 NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp;
1552 mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6;
1553 if (isudp && (types & mask)) {
1554 return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ?
1555 NetPktRssIpV6UdpEx : NetPktRssIpV6Udp;
1557 mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6;
1558 if (types & mask) {
1559 return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ?
1560 NetPktRssIpV6Ex : NetPktRssIpV6;
1563 return 0xff;
1566 static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1567 uint32_t hash)
1569 struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1570 hdr->hash_value = hash;
1571 hdr->hash_report = report;
1574 static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
1575 size_t size)
1577 VirtIONet *n = qemu_get_nic_opaque(nc);
1578 unsigned int index = nc->queue_index, new_index = index;
1579 struct NetRxPkt *pkt = n->rx_pkt;
1580 uint8_t net_hash_type;
1581 uint32_t hash;
1582 bool isip4, isip6, isudp, istcp;
1583 static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1584 VIRTIO_NET_HASH_REPORT_IPv4,
1585 VIRTIO_NET_HASH_REPORT_TCPv4,
1586 VIRTIO_NET_HASH_REPORT_TCPv6,
1587 VIRTIO_NET_HASH_REPORT_IPv6,
1588 VIRTIO_NET_HASH_REPORT_IPv6_EX,
1589 VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1590 VIRTIO_NET_HASH_REPORT_UDPv4,
1591 VIRTIO_NET_HASH_REPORT_UDPv6,
1592 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1595 net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len,
1596 size - n->host_hdr_len);
1597 net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
1598 if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) {
1599 istcp = isudp = false;
1601 if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) {
1602 istcp = isudp = false;
1604 net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp,
1605 n->rss_data.hash_types);
1606 if (net_hash_type > NetPktRssIpV6UdpEx) {
1607 if (n->rss_data.populate_hash) {
1608 virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1610 return n->rss_data.redirect ? n->rss_data.default_queue : -1;
1613 hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1615 if (n->rss_data.populate_hash) {
1616 virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1619 if (n->rss_data.redirect) {
1620 new_index = hash & (n->rss_data.indirections_len - 1);
1621 new_index = n->rss_data.indirections_table[new_index];
1624 return (index == new_index) ? -1 : new_index;
1627 static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1628 size_t size, bool no_rss)
1630 VirtIONet *n = qemu_get_nic_opaque(nc);
1631 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1632 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1633 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1634 struct virtio_net_hdr_mrg_rxbuf mhdr;
1635 unsigned mhdr_cnt = 0;
1636 size_t offset, i, guest_offset;
1638 if (!virtio_net_can_receive(nc)) {
1639 return -1;
1642 if (!no_rss && n->rss_data.enabled) {
1643 int index = virtio_net_process_rss(nc, buf, size);
1644 if (index >= 0) {
1645 NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
1646 return virtio_net_receive_rcu(nc2, buf, size, true);
1650 /* hdr_len refers to the header we supply to the guest */
1651 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1652 return 0;
1655 if (!receive_filter(n, buf, size))
1656 return size;
1658 offset = i = 0;
1660 while (offset < size) {
1661 VirtQueueElement *elem;
1662 int len, total;
1663 const struct iovec *sg;
1665 total = 0;
1667 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1668 if (!elem) {
1669 if (i) {
1670 virtio_error(vdev, "virtio-net unexpected empty queue: "
1671 "i %zd mergeable %d offset %zd, size %zd, "
1672 "guest hdr len %zd, host hdr len %zd "
1673 "guest features 0x%" PRIx64,
1674 i, n->mergeable_rx_bufs, offset, size,
1675 n->guest_hdr_len, n->host_hdr_len,
1676 vdev->guest_features);
1678 return -1;
1681 if (elem->in_num < 1) {
1682 virtio_error(vdev,
1683 "virtio-net receive queue contains no in buffers");
1684 virtqueue_detach_element(q->rx_vq, elem, 0);
1685 g_free(elem);
1686 return -1;
1689 sg = elem->in_sg;
1690 if (i == 0) {
1691 assert(offset == 0);
1692 if (n->mergeable_rx_bufs) {
1693 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1694 sg, elem->in_num,
1695 offsetof(typeof(mhdr), num_buffers),
1696 sizeof(mhdr.num_buffers));
1699 receive_header(n, sg, elem->in_num, buf, size);
1700 if (n->rss_data.populate_hash) {
1701 offset = sizeof(mhdr);
1702 iov_from_buf(sg, elem->in_num, offset,
1703 buf + offset, n->host_hdr_len - sizeof(mhdr));
1705 offset = n->host_hdr_len;
1706 total += n->guest_hdr_len;
1707 guest_offset = n->guest_hdr_len;
1708 } else {
1709 guest_offset = 0;
1712 /* copy in packet. ugh */
1713 len = iov_from_buf(sg, elem->in_num, guest_offset,
1714 buf + offset, size - offset);
1715 total += len;
1716 offset += len;
1717 /* If buffers can't be merged, at this point we
1718 * must have consumed the complete packet.
1719 * Otherwise, drop it. */
1720 if (!n->mergeable_rx_bufs && offset < size) {
1721 virtqueue_unpop(q->rx_vq, elem, total);
1722 g_free(elem);
1723 return size;
1726 /* signal other side */
1727 virtqueue_fill(q->rx_vq, elem, total, i++);
1728 g_free(elem);
1731 if (mhdr_cnt) {
1732 virtio_stw_p(vdev, &mhdr.num_buffers, i);
1733 iov_from_buf(mhdr_sg, mhdr_cnt,
1735 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1738 virtqueue_flush(q->rx_vq, i);
1739 virtio_notify(vdev, q->rx_vq);
1741 return size;
1744 static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
1745 size_t size)
1747 RCU_READ_LOCK_GUARD();
1749 return virtio_net_receive_rcu(nc, buf, size, false);
1752 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
1753 const uint8_t *buf,
1754 VirtioNetRscUnit *unit)
1756 uint16_t ip_hdrlen;
1757 struct ip_header *ip;
1759 ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
1760 + sizeof(struct eth_header));
1761 unit->ip = (void *)ip;
1762 ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
1763 unit->ip_plen = &ip->ip_len;
1764 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
1765 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1766 unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
1769 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
1770 const uint8_t *buf,
1771 VirtioNetRscUnit *unit)
1773 struct ip6_header *ip6;
1775 ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
1776 + sizeof(struct eth_header));
1777 unit->ip = ip6;
1778 unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
1779 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
1780 + sizeof(struct ip6_header));
1781 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1783 /* There is a difference between payload lenght in ipv4 and v6,
1784 ip header is excluded in ipv6 */
1785 unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
1788 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
1789 VirtioNetRscSeg *seg)
1791 int ret;
1792 struct virtio_net_hdr_v1 *h;
1794 h = (struct virtio_net_hdr_v1 *)seg->buf;
1795 h->flags = 0;
1796 h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
1798 if (seg->is_coalesced) {
1799 h->rsc.segments = seg->packets;
1800 h->rsc.dup_acks = seg->dup_ack;
1801 h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
1802 if (chain->proto == ETH_P_IP) {
1803 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1804 } else {
1805 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1809 ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
1810 QTAILQ_REMOVE(&chain->buffers, seg, next);
1811 g_free(seg->buf);
1812 g_free(seg);
1814 return ret;
1817 static void virtio_net_rsc_purge(void *opq)
1819 VirtioNetRscSeg *seg, *rn;
1820 VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
1822 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
1823 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
1824 chain->stat.purge_failed++;
1825 continue;
1829 chain->stat.timer++;
1830 if (!QTAILQ_EMPTY(&chain->buffers)) {
1831 timer_mod(chain->drain_timer,
1832 qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
1836 static void virtio_net_rsc_cleanup(VirtIONet *n)
1838 VirtioNetRscChain *chain, *rn_chain;
1839 VirtioNetRscSeg *seg, *rn_seg;
1841 QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
1842 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
1843 QTAILQ_REMOVE(&chain->buffers, seg, next);
1844 g_free(seg->buf);
1845 g_free(seg);
1848 timer_del(chain->drain_timer);
1849 timer_free(chain->drain_timer);
1850 QTAILQ_REMOVE(&n->rsc_chains, chain, next);
1851 g_free(chain);
1855 static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
1856 NetClientState *nc,
1857 const uint8_t *buf, size_t size)
1859 uint16_t hdr_len;
1860 VirtioNetRscSeg *seg;
1862 hdr_len = chain->n->guest_hdr_len;
1863 seg = g_malloc(sizeof(VirtioNetRscSeg));
1864 seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
1865 + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
1866 memcpy(seg->buf, buf, size);
1867 seg->size = size;
1868 seg->packets = 1;
1869 seg->dup_ack = 0;
1870 seg->is_coalesced = 0;
1871 seg->nc = nc;
1873 QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
1874 chain->stat.cache++;
1876 switch (chain->proto) {
1877 case ETH_P_IP:
1878 virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
1879 break;
1880 case ETH_P_IPV6:
1881 virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
1882 break;
1883 default:
1884 g_assert_not_reached();
1888 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
1889 VirtioNetRscSeg *seg,
1890 const uint8_t *buf,
1891 struct tcp_header *n_tcp,
1892 struct tcp_header *o_tcp)
1894 uint32_t nack, oack;
1895 uint16_t nwin, owin;
1897 nack = htonl(n_tcp->th_ack);
1898 nwin = htons(n_tcp->th_win);
1899 oack = htonl(o_tcp->th_ack);
1900 owin = htons(o_tcp->th_win);
1902 if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
1903 chain->stat.ack_out_of_win++;
1904 return RSC_FINAL;
1905 } else if (nack == oack) {
1906 /* duplicated ack or window probe */
1907 if (nwin == owin) {
1908 /* duplicated ack, add dup ack count due to whql test up to 1 */
1909 chain->stat.dup_ack++;
1910 return RSC_FINAL;
1911 } else {
1912 /* Coalesce window update */
1913 o_tcp->th_win = n_tcp->th_win;
1914 chain->stat.win_update++;
1915 return RSC_COALESCE;
1917 } else {
1918 /* pure ack, go to 'C', finalize*/
1919 chain->stat.pure_ack++;
1920 return RSC_FINAL;
1924 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
1925 VirtioNetRscSeg *seg,
1926 const uint8_t *buf,
1927 VirtioNetRscUnit *n_unit)
1929 void *data;
1930 uint16_t o_ip_len;
1931 uint32_t nseq, oseq;
1932 VirtioNetRscUnit *o_unit;
1934 o_unit = &seg->unit;
1935 o_ip_len = htons(*o_unit->ip_plen);
1936 nseq = htonl(n_unit->tcp->th_seq);
1937 oseq = htonl(o_unit->tcp->th_seq);
1939 /* out of order or retransmitted. */
1940 if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
1941 chain->stat.data_out_of_win++;
1942 return RSC_FINAL;
1945 data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
1946 if (nseq == oseq) {
1947 if ((o_unit->payload == 0) && n_unit->payload) {
1948 /* From no payload to payload, normal case, not a dup ack or etc */
1949 chain->stat.data_after_pure_ack++;
1950 goto coalesce;
1951 } else {
1952 return virtio_net_rsc_handle_ack(chain, seg, buf,
1953 n_unit->tcp, o_unit->tcp);
1955 } else if ((nseq - oseq) != o_unit->payload) {
1956 /* Not a consistent packet, out of order */
1957 chain->stat.data_out_of_order++;
1958 return RSC_FINAL;
1959 } else {
1960 coalesce:
1961 if ((o_ip_len + n_unit->payload) > chain->max_payload) {
1962 chain->stat.over_size++;
1963 return RSC_FINAL;
1966 /* Here comes the right data, the payload length in v4/v6 is different,
1967 so use the field value to update and record the new data len */
1968 o_unit->payload += n_unit->payload; /* update new data len */
1970 /* update field in ip header */
1971 *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
1973 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
1974 for windows guest, while this may change the behavior for linux
1975 guest (only if it uses RSC feature). */
1976 o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
1978 o_unit->tcp->th_ack = n_unit->tcp->th_ack;
1979 o_unit->tcp->th_win = n_unit->tcp->th_win;
1981 memmove(seg->buf + seg->size, data, n_unit->payload);
1982 seg->size += n_unit->payload;
1983 seg->packets++;
1984 chain->stat.coalesced++;
1985 return RSC_COALESCE;
1989 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
1990 VirtioNetRscSeg *seg,
1991 const uint8_t *buf, size_t size,
1992 VirtioNetRscUnit *unit)
1994 struct ip_header *ip1, *ip2;
1996 ip1 = (struct ip_header *)(unit->ip);
1997 ip2 = (struct ip_header *)(seg->unit.ip);
1998 if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
1999 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2000 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2001 chain->stat.no_match++;
2002 return RSC_NO_MATCH;
2005 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2008 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
2009 VirtioNetRscSeg *seg,
2010 const uint8_t *buf, size_t size,
2011 VirtioNetRscUnit *unit)
2013 struct ip6_header *ip1, *ip2;
2015 ip1 = (struct ip6_header *)(unit->ip);
2016 ip2 = (struct ip6_header *)(seg->unit.ip);
2017 if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
2018 || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
2019 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2020 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2021 chain->stat.no_match++;
2022 return RSC_NO_MATCH;
2025 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2028 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2029 * to prevent out of order */
2030 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
2031 struct tcp_header *tcp)
2033 uint16_t tcp_hdr;
2034 uint16_t tcp_flag;
2036 tcp_flag = htons(tcp->th_offset_flags);
2037 tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
2038 tcp_flag &= VIRTIO_NET_TCP_FLAG;
2039 if (tcp_flag & TH_SYN) {
2040 chain->stat.tcp_syn++;
2041 return RSC_BYPASS;
2044 if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
2045 chain->stat.tcp_ctrl_drain++;
2046 return RSC_FINAL;
2049 if (tcp_hdr > sizeof(struct tcp_header)) {
2050 chain->stat.tcp_all_opt++;
2051 return RSC_FINAL;
2054 return RSC_CANDIDATE;
2057 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
2058 NetClientState *nc,
2059 const uint8_t *buf, size_t size,
2060 VirtioNetRscUnit *unit)
2062 int ret;
2063 VirtioNetRscSeg *seg, *nseg;
2065 if (QTAILQ_EMPTY(&chain->buffers)) {
2066 chain->stat.empty_cache++;
2067 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2068 timer_mod(chain->drain_timer,
2069 qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
2070 return size;
2073 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2074 if (chain->proto == ETH_P_IP) {
2075 ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
2076 } else {
2077 ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
2080 if (ret == RSC_FINAL) {
2081 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2082 /* Send failed */
2083 chain->stat.final_failed++;
2084 return 0;
2087 /* Send current packet */
2088 return virtio_net_do_receive(nc, buf, size);
2089 } else if (ret == RSC_NO_MATCH) {
2090 continue;
2091 } else {
2092 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2093 seg->is_coalesced = 1;
2094 return size;
2098 chain->stat.no_match_cache++;
2099 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2100 return size;
2103 /* Drain a connection data, this is to avoid out of order segments */
2104 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
2105 NetClientState *nc,
2106 const uint8_t *buf, size_t size,
2107 uint16_t ip_start, uint16_t ip_size,
2108 uint16_t tcp_port)
2110 VirtioNetRscSeg *seg, *nseg;
2111 uint32_t ppair1, ppair2;
2113 ppair1 = *(uint32_t *)(buf + tcp_port);
2114 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2115 ppair2 = *(uint32_t *)(seg->buf + tcp_port);
2116 if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
2117 || (ppair1 != ppair2)) {
2118 continue;
2120 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2121 chain->stat.drain_failed++;
2124 break;
2127 return virtio_net_do_receive(nc, buf, size);
2130 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
2131 struct ip_header *ip,
2132 const uint8_t *buf, size_t size)
2134 uint16_t ip_len;
2136 /* Not an ipv4 packet */
2137 if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
2138 chain->stat.ip_option++;
2139 return RSC_BYPASS;
2142 /* Don't handle packets with ip option */
2143 if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
2144 chain->stat.ip_option++;
2145 return RSC_BYPASS;
2148 if (ip->ip_p != IPPROTO_TCP) {
2149 chain->stat.bypass_not_tcp++;
2150 return RSC_BYPASS;
2153 /* Don't handle packets with ip fragment */
2154 if (!(htons(ip->ip_off) & IP_DF)) {
2155 chain->stat.ip_frag++;
2156 return RSC_BYPASS;
2159 /* Don't handle packets with ecn flag */
2160 if (IPTOS_ECN(ip->ip_tos)) {
2161 chain->stat.ip_ecn++;
2162 return RSC_BYPASS;
2165 ip_len = htons(ip->ip_len);
2166 if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
2167 || ip_len > (size - chain->n->guest_hdr_len -
2168 sizeof(struct eth_header))) {
2169 chain->stat.ip_hacked++;
2170 return RSC_BYPASS;
2173 return RSC_CANDIDATE;
2176 static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
2177 NetClientState *nc,
2178 const uint8_t *buf, size_t size)
2180 int32_t ret;
2181 uint16_t hdr_len;
2182 VirtioNetRscUnit unit;
2184 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2186 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
2187 + sizeof(struct tcp_header))) {
2188 chain->stat.bypass_not_tcp++;
2189 return virtio_net_do_receive(nc, buf, size);
2192 virtio_net_rsc_extract_unit4(chain, buf, &unit);
2193 if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
2194 != RSC_CANDIDATE) {
2195 return virtio_net_do_receive(nc, buf, size);
2198 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2199 if (ret == RSC_BYPASS) {
2200 return virtio_net_do_receive(nc, buf, size);
2201 } else if (ret == RSC_FINAL) {
2202 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2203 ((hdr_len + sizeof(struct eth_header)) + 12),
2204 VIRTIO_NET_IP4_ADDR_SIZE,
2205 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
2208 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2211 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
2212 struct ip6_header *ip6,
2213 const uint8_t *buf, size_t size)
2215 uint16_t ip_len;
2217 if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
2218 != IP_HEADER_VERSION_6) {
2219 return RSC_BYPASS;
2222 /* Both option and protocol is checked in this */
2223 if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
2224 chain->stat.bypass_not_tcp++;
2225 return RSC_BYPASS;
2228 ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
2229 if (ip_len < sizeof(struct tcp_header) ||
2230 ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
2231 - sizeof(struct ip6_header))) {
2232 chain->stat.ip_hacked++;
2233 return RSC_BYPASS;
2236 /* Don't handle packets with ecn flag */
2237 if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
2238 chain->stat.ip_ecn++;
2239 return RSC_BYPASS;
2242 return RSC_CANDIDATE;
2245 static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
2246 const uint8_t *buf, size_t size)
2248 int32_t ret;
2249 uint16_t hdr_len;
2250 VirtioNetRscChain *chain;
2251 VirtioNetRscUnit unit;
2253 chain = (VirtioNetRscChain *)opq;
2254 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2256 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
2257 + sizeof(tcp_header))) {
2258 return virtio_net_do_receive(nc, buf, size);
2261 virtio_net_rsc_extract_unit6(chain, buf, &unit);
2262 if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
2263 unit.ip, buf, size)) {
2264 return virtio_net_do_receive(nc, buf, size);
2267 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2268 if (ret == RSC_BYPASS) {
2269 return virtio_net_do_receive(nc, buf, size);
2270 } else if (ret == RSC_FINAL) {
2271 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2272 ((hdr_len + sizeof(struct eth_header)) + 8),
2273 VIRTIO_NET_IP6_ADDR_SIZE,
2274 hdr_len + sizeof(struct eth_header)
2275 + sizeof(struct ip6_header));
2278 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2281 static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
2282 NetClientState *nc,
2283 uint16_t proto)
2285 VirtioNetRscChain *chain;
2287 if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
2288 return NULL;
2291 QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
2292 if (chain->proto == proto) {
2293 return chain;
2297 chain = g_malloc(sizeof(*chain));
2298 chain->n = n;
2299 chain->proto = proto;
2300 if (proto == (uint16_t)ETH_P_IP) {
2301 chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
2302 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2303 } else {
2304 chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
2305 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2307 chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
2308 virtio_net_rsc_purge, chain);
2309 memset(&chain->stat, 0, sizeof(chain->stat));
2311 QTAILQ_INIT(&chain->buffers);
2312 QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
2314 return chain;
2317 static ssize_t virtio_net_rsc_receive(NetClientState *nc,
2318 const uint8_t *buf,
2319 size_t size)
2321 uint16_t proto;
2322 VirtioNetRscChain *chain;
2323 struct eth_header *eth;
2324 VirtIONet *n;
2326 n = qemu_get_nic_opaque(nc);
2327 if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
2328 return virtio_net_do_receive(nc, buf, size);
2331 eth = (struct eth_header *)(buf + n->guest_hdr_len);
2332 proto = htons(eth->h_proto);
2334 chain = virtio_net_rsc_lookup_chain(n, nc, proto);
2335 if (chain) {
2336 chain->stat.received++;
2337 if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
2338 return virtio_net_rsc_receive4(chain, nc, buf, size);
2339 } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
2340 return virtio_net_rsc_receive6(chain, nc, buf, size);
2343 return virtio_net_do_receive(nc, buf, size);
2346 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
2347 size_t size)
2349 VirtIONet *n = qemu_get_nic_opaque(nc);
2350 if ((n->rsc4_enabled || n->rsc6_enabled)) {
2351 return virtio_net_rsc_receive(nc, buf, size);
2352 } else {
2353 return virtio_net_do_receive(nc, buf, size);
2357 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
2359 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
2361 VirtIONet *n = qemu_get_nic_opaque(nc);
2362 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
2363 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2365 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
2366 virtio_notify(vdev, q->tx_vq);
2368 g_free(q->async_tx.elem);
2369 q->async_tx.elem = NULL;
2371 virtio_queue_set_notification(q->tx_vq, 1);
2372 virtio_net_flush_tx(q);
2375 /* TX */
2376 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
2378 VirtIONet *n = q->n;
2379 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2380 VirtQueueElement *elem;
2381 int32_t num_packets = 0;
2382 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
2383 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2384 return num_packets;
2387 if (q->async_tx.elem) {
2388 virtio_queue_set_notification(q->tx_vq, 0);
2389 return num_packets;
2392 for (;;) {
2393 ssize_t ret;
2394 unsigned int out_num;
2395 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2396 struct virtio_net_hdr_mrg_rxbuf mhdr;
2398 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
2399 if (!elem) {
2400 break;
2403 out_num = elem->out_num;
2404 out_sg = elem->out_sg;
2405 if (out_num < 1) {
2406 virtio_error(vdev, "virtio-net header not in first element");
2407 virtqueue_detach_element(q->tx_vq, elem, 0);
2408 g_free(elem);
2409 return -EINVAL;
2412 if (n->has_vnet_hdr) {
2413 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2414 n->guest_hdr_len) {
2415 virtio_error(vdev, "virtio-net header incorrect");
2416 virtqueue_detach_element(q->tx_vq, elem, 0);
2417 g_free(elem);
2418 return -EINVAL;
2420 if (n->needs_vnet_hdr_swap) {
2421 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2422 sg2[0].iov_base = &mhdr;
2423 sg2[0].iov_len = n->guest_hdr_len;
2424 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2425 out_sg, out_num,
2426 n->guest_hdr_len, -1);
2427 if (out_num == VIRTQUEUE_MAX_SIZE) {
2428 goto drop;
2430 out_num += 1;
2431 out_sg = sg2;
2435 * If host wants to see the guest header as is, we can
2436 * pass it on unchanged. Otherwise, copy just the parts
2437 * that host is interested in.
2439 assert(n->host_hdr_len <= n->guest_hdr_len);
2440 if (n->host_hdr_len != n->guest_hdr_len) {
2441 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
2442 out_sg, out_num,
2443 0, n->host_hdr_len);
2444 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
2445 out_sg, out_num,
2446 n->guest_hdr_len, -1);
2447 out_num = sg_num;
2448 out_sg = sg;
2451 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
2452 out_sg, out_num, virtio_net_tx_complete);
2453 if (ret == 0) {
2454 virtio_queue_set_notification(q->tx_vq, 0);
2455 q->async_tx.elem = elem;
2456 return -EBUSY;
2459 drop:
2460 virtqueue_push(q->tx_vq, elem, 0);
2461 virtio_notify(vdev, q->tx_vq);
2462 g_free(elem);
2464 if (++num_packets >= n->tx_burst) {
2465 break;
2468 return num_packets;
2471 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
2473 VirtIONet *n = VIRTIO_NET(vdev);
2474 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2476 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2477 virtio_net_drop_tx_queue_data(vdev, vq);
2478 return;
2481 /* This happens when device was stopped but VCPU wasn't. */
2482 if (!vdev->vm_running) {
2483 q->tx_waiting = 1;
2484 return;
2487 if (q->tx_waiting) {
2488 virtio_queue_set_notification(vq, 1);
2489 timer_del(q->tx_timer);
2490 q->tx_waiting = 0;
2491 if (virtio_net_flush_tx(q) == -EINVAL) {
2492 return;
2494 } else {
2495 timer_mod(q->tx_timer,
2496 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2497 q->tx_waiting = 1;
2498 virtio_queue_set_notification(vq, 0);
2502 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
2504 VirtIONet *n = VIRTIO_NET(vdev);
2505 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2507 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2508 virtio_net_drop_tx_queue_data(vdev, vq);
2509 return;
2512 if (unlikely(q->tx_waiting)) {
2513 return;
2515 q->tx_waiting = 1;
2516 /* This happens when device was stopped but VCPU wasn't. */
2517 if (!vdev->vm_running) {
2518 return;
2520 virtio_queue_set_notification(vq, 0);
2521 qemu_bh_schedule(q->tx_bh);
2524 static void virtio_net_tx_timer(void *opaque)
2526 VirtIONetQueue *q = opaque;
2527 VirtIONet *n = q->n;
2528 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2529 /* This happens when device was stopped but BH wasn't. */
2530 if (!vdev->vm_running) {
2531 /* Make sure tx waiting is set, so we'll run when restarted. */
2532 assert(q->tx_waiting);
2533 return;
2536 q->tx_waiting = 0;
2538 /* Just in case the driver is not ready on more */
2539 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2540 return;
2543 virtio_queue_set_notification(q->tx_vq, 1);
2544 virtio_net_flush_tx(q);
2547 static void virtio_net_tx_bh(void *opaque)
2549 VirtIONetQueue *q = opaque;
2550 VirtIONet *n = q->n;
2551 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2552 int32_t ret;
2554 /* This happens when device was stopped but BH wasn't. */
2555 if (!vdev->vm_running) {
2556 /* Make sure tx waiting is set, so we'll run when restarted. */
2557 assert(q->tx_waiting);
2558 return;
2561 q->tx_waiting = 0;
2563 /* Just in case the driver is not ready on more */
2564 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
2565 return;
2568 ret = virtio_net_flush_tx(q);
2569 if (ret == -EBUSY || ret == -EINVAL) {
2570 return; /* Notification re-enable handled by tx_complete or device
2571 * broken */
2574 /* If we flush a full burst of packets, assume there are
2575 * more coming and immediately reschedule */
2576 if (ret >= n->tx_burst) {
2577 qemu_bh_schedule(q->tx_bh);
2578 q->tx_waiting = 1;
2579 return;
2582 /* If less than a full burst, re-enable notification and flush
2583 * anything that may have come in while we weren't looking. If
2584 * we find something, assume the guest is still active and reschedule */
2585 virtio_queue_set_notification(q->tx_vq, 1);
2586 ret = virtio_net_flush_tx(q);
2587 if (ret == -EINVAL) {
2588 return;
2589 } else if (ret > 0) {
2590 virtio_queue_set_notification(q->tx_vq, 0);
2591 qemu_bh_schedule(q->tx_bh);
2592 q->tx_waiting = 1;
2596 static void virtio_net_add_queue(VirtIONet *n, int index)
2598 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2600 n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
2601 virtio_net_handle_rx);
2603 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2604 n->vqs[index].tx_vq =
2605 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2606 virtio_net_handle_tx_timer);
2607 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2608 virtio_net_tx_timer,
2609 &n->vqs[index]);
2610 } else {
2611 n->vqs[index].tx_vq =
2612 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2613 virtio_net_handle_tx_bh);
2614 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
2617 n->vqs[index].tx_waiting = 0;
2618 n->vqs[index].n = n;
2621 static void virtio_net_del_queue(VirtIONet *n, int index)
2623 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2624 VirtIONetQueue *q = &n->vqs[index];
2625 NetClientState *nc = qemu_get_subqueue(n->nic, index);
2627 qemu_purge_queued_packets(nc);
2629 virtio_del_queue(vdev, index * 2);
2630 if (q->tx_timer) {
2631 timer_del(q->tx_timer);
2632 timer_free(q->tx_timer);
2633 q->tx_timer = NULL;
2634 } else {
2635 qemu_bh_delete(q->tx_bh);
2636 q->tx_bh = NULL;
2638 q->tx_waiting = 0;
2639 virtio_del_queue(vdev, index * 2 + 1);
2642 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
2644 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2645 int old_num_queues = virtio_get_num_queues(vdev);
2646 int new_num_queues = new_max_queues * 2 + 1;
2647 int i;
2649 assert(old_num_queues >= 3);
2650 assert(old_num_queues % 2 == 1);
2652 if (old_num_queues == new_num_queues) {
2653 return;
2657 * We always need to remove and add ctrl vq if
2658 * old_num_queues != new_num_queues. Remove ctrl_vq first,
2659 * and then we only enter one of the following two loops.
2661 virtio_del_queue(vdev, old_num_queues - 1);
2663 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2664 /* new_num_queues < old_num_queues */
2665 virtio_net_del_queue(n, i / 2);
2668 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2669 /* new_num_queues > old_num_queues */
2670 virtio_net_add_queue(n, i / 2);
2673 /* add ctrl_vq last */
2674 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2677 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
2679 int max = multiqueue ? n->max_queues : 1;
2681 n->multiqueue = multiqueue;
2682 virtio_net_change_num_queues(n, max);
2684 virtio_net_set_queues(n);
2687 static int virtio_net_post_load_device(void *opaque, int version_id)
2689 VirtIONet *n = opaque;
2690 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2691 int i, link_down;
2693 trace_virtio_net_post_load_device();
2694 virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
2695 virtio_vdev_has_feature(vdev,
2696 VIRTIO_F_VERSION_1),
2697 virtio_vdev_has_feature(vdev,
2698 VIRTIO_NET_F_HASH_REPORT));
2700 /* MAC_TABLE_ENTRIES may be different from the saved image */
2701 if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
2702 n->mac_table.in_use = 0;
2705 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
2706 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
2710 * curr_guest_offloads will be later overwritten by the
2711 * virtio_set_features_nocheck call done from the virtio_load.
2712 * Here we make sure it is preserved and restored accordingly
2713 * in the virtio_net_post_load_virtio callback.
2715 n->saved_guest_offloads = n->curr_guest_offloads;
2717 virtio_net_set_queues(n);
2719 /* Find the first multicast entry in the saved MAC filter */
2720 for (i = 0; i < n->mac_table.in_use; i++) {
2721 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
2722 break;
2725 n->mac_table.first_multi = i;
2727 /* nc.link_down can't be migrated, so infer link_down according
2728 * to link status bit in n->status */
2729 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
2730 for (i = 0; i < n->max_queues; i++) {
2731 qemu_get_subqueue(n->nic, i)->link_down = link_down;
2734 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
2735 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
2736 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
2737 QEMU_CLOCK_VIRTUAL,
2738 virtio_net_announce_timer, n);
2739 if (n->announce_timer.round) {
2740 timer_mod(n->announce_timer.tm,
2741 qemu_clock_get_ms(n->announce_timer.type));
2742 } else {
2743 qemu_announce_timer_del(&n->announce_timer, false);
2747 if (n->rss_data.enabled) {
2748 trace_virtio_net_rss_enable(n->rss_data.hash_types,
2749 n->rss_data.indirections_len,
2750 sizeof(n->rss_data.key));
2751 } else {
2752 trace_virtio_net_rss_disable();
2754 return 0;
2757 static int virtio_net_post_load_virtio(VirtIODevice *vdev)
2759 VirtIONet *n = VIRTIO_NET(vdev);
2761 * The actual needed state is now in saved_guest_offloads,
2762 * see virtio_net_post_load_device for detail.
2763 * Restore it back and apply the desired offloads.
2765 n->curr_guest_offloads = n->saved_guest_offloads;
2766 if (peer_has_vnet_hdr(n)) {
2767 virtio_net_apply_guest_offloads(n);
2770 return 0;
2773 /* tx_waiting field of a VirtIONetQueue */
2774 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
2775 .name = "virtio-net-queue-tx_waiting",
2776 .fields = (VMStateField[]) {
2777 VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
2778 VMSTATE_END_OF_LIST()
2782 static bool max_queues_gt_1(void *opaque, int version_id)
2784 return VIRTIO_NET(opaque)->max_queues > 1;
2787 static bool has_ctrl_guest_offloads(void *opaque, int version_id)
2789 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
2790 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2793 static bool mac_table_fits(void *opaque, int version_id)
2795 return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
2798 static bool mac_table_doesnt_fit(void *opaque, int version_id)
2800 return !mac_table_fits(opaque, version_id);
2803 /* This temporary type is shared by all the WITH_TMP methods
2804 * although only some fields are used by each.
2806 struct VirtIONetMigTmp {
2807 VirtIONet *parent;
2808 VirtIONetQueue *vqs_1;
2809 uint16_t curr_queues_1;
2810 uint8_t has_ufo;
2811 uint32_t has_vnet_hdr;
2814 /* The 2nd and subsequent tx_waiting flags are loaded later than
2815 * the 1st entry in the queues and only if there's more than one
2816 * entry. We use the tmp mechanism to calculate a temporary
2817 * pointer and count and also validate the count.
2820 static int virtio_net_tx_waiting_pre_save(void *opaque)
2822 struct VirtIONetMigTmp *tmp = opaque;
2824 tmp->vqs_1 = tmp->parent->vqs + 1;
2825 tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
2826 if (tmp->parent->curr_queues == 0) {
2827 tmp->curr_queues_1 = 0;
2830 return 0;
2833 static int virtio_net_tx_waiting_pre_load(void *opaque)
2835 struct VirtIONetMigTmp *tmp = opaque;
2837 /* Reuse the pointer setup from save */
2838 virtio_net_tx_waiting_pre_save(opaque);
2840 if (tmp->parent->curr_queues > tmp->parent->max_queues) {
2841 error_report("virtio-net: curr_queues %x > max_queues %x",
2842 tmp->parent->curr_queues, tmp->parent->max_queues);
2844 return -EINVAL;
2847 return 0; /* all good */
2850 static const VMStateDescription vmstate_virtio_net_tx_waiting = {
2851 .name = "virtio-net-tx_waiting",
2852 .pre_load = virtio_net_tx_waiting_pre_load,
2853 .pre_save = virtio_net_tx_waiting_pre_save,
2854 .fields = (VMStateField[]) {
2855 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
2856 curr_queues_1,
2857 vmstate_virtio_net_queue_tx_waiting,
2858 struct VirtIONetQueue),
2859 VMSTATE_END_OF_LIST()
2863 /* the 'has_ufo' flag is just tested; if the incoming stream has the
2864 * flag set we need to check that we have it
2866 static int virtio_net_ufo_post_load(void *opaque, int version_id)
2868 struct VirtIONetMigTmp *tmp = opaque;
2870 if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
2871 error_report("virtio-net: saved image requires TUN_F_UFO support");
2872 return -EINVAL;
2875 return 0;
2878 static int virtio_net_ufo_pre_save(void *opaque)
2880 struct VirtIONetMigTmp *tmp = opaque;
2882 tmp->has_ufo = tmp->parent->has_ufo;
2884 return 0;
2887 static const VMStateDescription vmstate_virtio_net_has_ufo = {
2888 .name = "virtio-net-ufo",
2889 .post_load = virtio_net_ufo_post_load,
2890 .pre_save = virtio_net_ufo_pre_save,
2891 .fields = (VMStateField[]) {
2892 VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
2893 VMSTATE_END_OF_LIST()
2897 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
2898 * flag set we need to check that we have it
2900 static int virtio_net_vnet_post_load(void *opaque, int version_id)
2902 struct VirtIONetMigTmp *tmp = opaque;
2904 if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
2905 error_report("virtio-net: saved image requires vnet_hdr=on");
2906 return -EINVAL;
2909 return 0;
2912 static int virtio_net_vnet_pre_save(void *opaque)
2914 struct VirtIONetMigTmp *tmp = opaque;
2916 tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
2918 return 0;
2921 static const VMStateDescription vmstate_virtio_net_has_vnet = {
2922 .name = "virtio-net-vnet",
2923 .post_load = virtio_net_vnet_post_load,
2924 .pre_save = virtio_net_vnet_pre_save,
2925 .fields = (VMStateField[]) {
2926 VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
2927 VMSTATE_END_OF_LIST()
2931 static bool virtio_net_rss_needed(void *opaque)
2933 return VIRTIO_NET(opaque)->rss_data.enabled;
2936 static const VMStateDescription vmstate_virtio_net_rss = {
2937 .name = "virtio-net-device/rss",
2938 .version_id = 1,
2939 .minimum_version_id = 1,
2940 .needed = virtio_net_rss_needed,
2941 .fields = (VMStateField[]) {
2942 VMSTATE_BOOL(rss_data.enabled, VirtIONet),
2943 VMSTATE_BOOL(rss_data.redirect, VirtIONet),
2944 VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
2945 VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
2946 VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
2947 VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
2948 VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
2949 VIRTIO_NET_RSS_MAX_KEY_SIZE),
2950 VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
2951 rss_data.indirections_len, 0,
2952 vmstate_info_uint16, uint16_t),
2953 VMSTATE_END_OF_LIST()
2957 static const VMStateDescription vmstate_virtio_net_device = {
2958 .name = "virtio-net-device",
2959 .version_id = VIRTIO_NET_VM_VERSION,
2960 .minimum_version_id = VIRTIO_NET_VM_VERSION,
2961 .post_load = virtio_net_post_load_device,
2962 .fields = (VMStateField[]) {
2963 VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
2964 VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
2965 vmstate_virtio_net_queue_tx_waiting,
2966 VirtIONetQueue),
2967 VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
2968 VMSTATE_UINT16(status, VirtIONet),
2969 VMSTATE_UINT8(promisc, VirtIONet),
2970 VMSTATE_UINT8(allmulti, VirtIONet),
2971 VMSTATE_UINT32(mac_table.in_use, VirtIONet),
2973 /* Guarded pair: If it fits we load it, else we throw it away
2974 * - can happen if source has a larger MAC table.; post-load
2975 * sets flags in this case.
2977 VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
2978 0, mac_table_fits, mac_table.in_use,
2979 ETH_ALEN),
2980 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
2981 mac_table.in_use, ETH_ALEN),
2983 /* Note: This is an array of uint32's that's always been saved as a
2984 * buffer; hold onto your endiannesses; it's actually used as a bitmap
2985 * but based on the uint.
2987 VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
2988 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
2989 vmstate_virtio_net_has_vnet),
2990 VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
2991 VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
2992 VMSTATE_UINT8(alluni, VirtIONet),
2993 VMSTATE_UINT8(nomulti, VirtIONet),
2994 VMSTATE_UINT8(nouni, VirtIONet),
2995 VMSTATE_UINT8(nobcast, VirtIONet),
2996 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
2997 vmstate_virtio_net_has_ufo),
2998 VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
2999 vmstate_info_uint16_equal, uint16_t),
3000 VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
3001 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3002 vmstate_virtio_net_tx_waiting),
3003 VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3004 has_ctrl_guest_offloads),
3005 VMSTATE_END_OF_LIST()
3007 .subsections = (const VMStateDescription * []) {
3008 &vmstate_virtio_net_rss,
3009 NULL
3013 static NetClientInfo net_virtio_info = {
3014 .type = NET_CLIENT_DRIVER_NIC,
3015 .size = sizeof(NICState),
3016 .can_receive = virtio_net_can_receive,
3017 .receive = virtio_net_receive,
3018 .link_status_changed = virtio_net_set_link_status,
3019 .query_rx_filter = virtio_net_query_rxfilter,
3020 .announce = virtio_net_announce,
3023 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
3025 VirtIONet *n = VIRTIO_NET(vdev);
3026 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3027 assert(n->vhost_started);
3028 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
3031 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
3032 bool mask)
3034 VirtIONet *n = VIRTIO_NET(vdev);
3035 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3036 assert(n->vhost_started);
3037 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
3038 vdev, idx, mask);
3041 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
3043 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3045 n->config_size = virtio_feature_get_config_size(feature_sizes,
3046 host_features);
3049 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
3050 const char *type)
3053 * The name can be NULL, the netclient name will be type.x.
3055 assert(type != NULL);
3057 g_free(n->netclient_name);
3058 g_free(n->netclient_type);
3059 n->netclient_name = g_strdup(name);
3060 n->netclient_type = g_strdup(type);
3063 static bool failover_unplug_primary(VirtIONet *n)
3065 HotplugHandler *hotplug_ctrl;
3066 PCIDevice *pci_dev;
3067 Error *err = NULL;
3069 hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev);
3070 if (hotplug_ctrl) {
3071 pci_dev = PCI_DEVICE(n->primary_dev);
3072 pci_dev->partially_hotplugged = true;
3073 hotplug_handler_unplug_request(hotplug_ctrl, n->primary_dev, &err);
3074 if (err) {
3075 error_report_err(err);
3076 return false;
3078 } else {
3079 return false;
3081 return true;
3084 static bool failover_replug_primary(VirtIONet *n, Error **errp)
3086 Error *err = NULL;
3087 HotplugHandler *hotplug_ctrl;
3088 PCIDevice *pdev = PCI_DEVICE(n->primary_dev);
3089 BusState *primary_bus;
3091 if (!pdev->partially_hotplugged) {
3092 return true;
3094 primary_bus = n->primary_dev->parent_bus;
3095 if (!primary_bus) {
3096 error_setg(errp, "virtio_net: couldn't find primary bus");
3097 return false;
3099 qdev_set_parent_bus(n->primary_dev, primary_bus, &error_abort);
3100 qatomic_set(&n->failover_primary_hidden, false);
3101 hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev);
3102 if (hotplug_ctrl) {
3103 hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, &err);
3104 if (err) {
3105 goto out;
3107 hotplug_handler_plug(hotplug_ctrl, n->primary_dev, &err);
3110 out:
3111 error_propagate(errp, err);
3112 return !err;
3115 static void virtio_net_handle_migration_primary(VirtIONet *n,
3116 MigrationState *s)
3118 bool should_be_hidden;
3119 Error *err = NULL;
3121 should_be_hidden = qatomic_read(&n->failover_primary_hidden);
3123 if (!n->primary_dev) {
3124 n->primary_dev = failover_find_primary_device(n, &err);
3125 if (!n->primary_dev) {
3126 return;
3130 if (migration_in_setup(s) && !should_be_hidden) {
3131 if (failover_unplug_primary(n)) {
3132 vmstate_unregister(VMSTATE_IF(n->primary_dev),
3133 qdev_get_vmsd(n->primary_dev),
3134 n->primary_dev);
3135 qapi_event_send_unplug_primary(n->primary_device_id);
3136 qatomic_set(&n->failover_primary_hidden, true);
3137 } else {
3138 warn_report("couldn't unplug primary device");
3140 } else if (migration_has_failed(s)) {
3141 /* We already unplugged the device let's plug it back */
3142 if (!failover_replug_primary(n, &err)) {
3143 if (err) {
3144 error_report_err(err);
3150 static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
3152 MigrationState *s = data;
3153 VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
3154 virtio_net_handle_migration_primary(n, s);
3157 static bool failover_hide_primary_device(DeviceListener *listener,
3158 QemuOpts *device_opts)
3160 VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
3161 bool hide;
3162 const char *standby_id;
3164 if (!device_opts) {
3165 return false;
3167 standby_id = qemu_opt_get(device_opts, "failover_pair_id");
3168 if (g_strcmp0(standby_id, n->netclient_name) != 0) {
3169 return false;
3172 /* failover_primary_hidden is set during feature negotiation */
3173 hide = qatomic_read(&n->failover_primary_hidden);
3174 g_free(n->primary_device_id);
3175 n->primary_device_id = g_strdup(device_opts->id);
3176 if (!n->primary_device_id) {
3177 warn_report("primary_device_id not set");
3179 return hide;
3182 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
3184 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3185 VirtIONet *n = VIRTIO_NET(dev);
3186 NetClientState *nc;
3187 int i;
3189 if (n->net_conf.mtu) {
3190 n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3193 if (n->net_conf.duplex_str) {
3194 if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
3195 n->net_conf.duplex = DUPLEX_HALF;
3196 } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
3197 n->net_conf.duplex = DUPLEX_FULL;
3198 } else {
3199 error_setg(errp, "'duplex' must be 'half' or 'full'");
3200 return;
3202 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3203 } else {
3204 n->net_conf.duplex = DUPLEX_UNKNOWN;
3207 if (n->net_conf.speed < SPEED_UNKNOWN) {
3208 error_setg(errp, "'speed' must be between 0 and INT_MAX");
3209 return;
3211 if (n->net_conf.speed >= 0) {
3212 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3215 if (n->failover) {
3216 n->primary_listener.hide_device = failover_hide_primary_device;
3217 qatomic_set(&n->failover_primary_hidden, true);
3218 device_listener_register(&n->primary_listener);
3219 n->migration_state.notify = virtio_net_migration_state_notifier;
3220 add_migration_state_change_notifier(&n->migration_state);
3221 n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
3224 virtio_net_set_config_size(n, n->host_features);
3225 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
3228 * We set a lower limit on RX queue size to what it always was.
3229 * Guests that want a smaller ring can always resize it without
3230 * help from us (using virtio 1 and up).
3232 if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
3233 n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
3234 !is_power_of_2(n->net_conf.rx_queue_size)) {
3235 error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
3236 "must be a power of 2 between %d and %d.",
3237 n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
3238 VIRTQUEUE_MAX_SIZE);
3239 virtio_cleanup(vdev);
3240 return;
3243 if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
3244 n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
3245 !is_power_of_2(n->net_conf.tx_queue_size)) {
3246 error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
3247 "must be a power of 2 between %d and %d",
3248 n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
3249 VIRTQUEUE_MAX_SIZE);
3250 virtio_cleanup(vdev);
3251 return;
3254 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
3255 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
3256 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
3257 "must be a positive integer less than %d.",
3258 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
3259 virtio_cleanup(vdev);
3260 return;
3262 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
3263 n->curr_queues = 1;
3264 n->tx_timeout = n->net_conf.txtimer;
3266 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
3267 && strcmp(n->net_conf.tx, "bh")) {
3268 warn_report("virtio-net: "
3269 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3270 n->net_conf.tx);
3271 error_printf("Defaulting to \"bh\"");
3274 n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
3275 n->net_conf.tx_queue_size);
3277 for (i = 0; i < n->max_queues; i++) {
3278 virtio_net_add_queue(n, i);
3281 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3282 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
3283 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
3284 n->status = VIRTIO_NET_S_LINK_UP;
3285 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
3286 QEMU_CLOCK_VIRTUAL,
3287 virtio_net_announce_timer, n);
3288 n->announce_timer.round = 0;
3290 if (n->netclient_type) {
3292 * Happen when virtio_net_set_netclient_name has been called.
3294 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3295 n->netclient_type, n->netclient_name, n);
3296 } else {
3297 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3298 object_get_typename(OBJECT(dev)), dev->id, n);
3301 peer_test_vnet_hdr(n);
3302 if (peer_has_vnet_hdr(n)) {
3303 for (i = 0; i < n->max_queues; i++) {
3304 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
3306 n->host_hdr_len = sizeof(struct virtio_net_hdr);
3307 } else {
3308 n->host_hdr_len = 0;
3311 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
3313 n->vqs[0].tx_waiting = 0;
3314 n->tx_burst = n->net_conf.txburst;
3315 virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
3316 n->promisc = 1; /* for compatibility */
3318 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
3320 n->vlans = g_malloc0(MAX_VLAN >> 3);
3322 nc = qemu_get_queue(n->nic);
3323 nc->rxfilter_notify_enabled = 1;
3325 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3326 struct virtio_net_config netcfg = {};
3327 memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3328 vhost_net_set_config(get_vhost_net(nc->peer),
3329 (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER);
3331 QTAILQ_INIT(&n->rsc_chains);
3332 n->qdev = dev;
3334 net_rx_pkt_init(&n->rx_pkt, false);
3337 static void virtio_net_device_unrealize(DeviceState *dev)
3339 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3340 VirtIONet *n = VIRTIO_NET(dev);
3341 int i, max_queues;
3343 /* This will stop vhost backend if appropriate. */
3344 virtio_net_set_status(vdev, 0);
3346 g_free(n->netclient_name);
3347 n->netclient_name = NULL;
3348 g_free(n->netclient_type);
3349 n->netclient_type = NULL;
3351 g_free(n->mac_table.macs);
3352 g_free(n->vlans);
3354 if (n->failover) {
3355 device_listener_unregister(&n->primary_listener);
3356 g_free(n->primary_device_id);
3359 max_queues = n->multiqueue ? n->max_queues : 1;
3360 for (i = 0; i < max_queues; i++) {
3361 virtio_net_del_queue(n, i);
3363 /* delete also control vq */
3364 virtio_del_queue(vdev, max_queues * 2);
3365 qemu_announce_timer_del(&n->announce_timer, false);
3366 g_free(n->vqs);
3367 qemu_del_nic(n->nic);
3368 virtio_net_rsc_cleanup(n);
3369 g_free(n->rss_data.indirections_table);
3370 net_rx_pkt_uninit(n->rx_pkt);
3371 virtio_cleanup(vdev);
3374 static void virtio_net_instance_init(Object *obj)
3376 VirtIONet *n = VIRTIO_NET(obj);
3379 * The default config_size is sizeof(struct virtio_net_config).
3380 * Can be overriden with virtio_net_set_config_size.
3382 n->config_size = sizeof(struct virtio_net_config);
3383 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3384 "bootindex", "/ethernet-phy@0",
3385 DEVICE(n));
3388 static int virtio_net_pre_save(void *opaque)
3390 VirtIONet *n = opaque;
3392 /* At this point, backend must be stopped, otherwise
3393 * it might keep writing to memory. */
3394 assert(!n->vhost_started);
3396 return 0;
3399 static bool primary_unplug_pending(void *opaque)
3401 DeviceState *dev = opaque;
3402 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3403 VirtIONet *n = VIRTIO_NET(vdev);
3405 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3406 return false;
3408 return n->primary_dev ? n->primary_dev->pending_deleted_event : false;
3411 static bool dev_unplug_pending(void *opaque)
3413 DeviceState *dev = opaque;
3414 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
3416 return vdc->primary_unplug_pending(dev);
3419 static const VMStateDescription vmstate_virtio_net = {
3420 .name = "virtio-net",
3421 .minimum_version_id = VIRTIO_NET_VM_VERSION,
3422 .version_id = VIRTIO_NET_VM_VERSION,
3423 .fields = (VMStateField[]) {
3424 VMSTATE_VIRTIO_DEVICE,
3425 VMSTATE_END_OF_LIST()
3427 .pre_save = virtio_net_pre_save,
3428 .dev_unplug_pending = dev_unplug_pending,
3431 static Property virtio_net_properties[] = {
3432 DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3433 VIRTIO_NET_F_CSUM, true),
3434 DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
3435 VIRTIO_NET_F_GUEST_CSUM, true),
3436 DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3437 DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
3438 VIRTIO_NET_F_GUEST_TSO4, true),
3439 DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
3440 VIRTIO_NET_F_GUEST_TSO6, true),
3441 DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
3442 VIRTIO_NET_F_GUEST_ECN, true),
3443 DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
3444 VIRTIO_NET_F_GUEST_UFO, true),
3445 DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
3446 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3447 DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
3448 VIRTIO_NET_F_HOST_TSO4, true),
3449 DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
3450 VIRTIO_NET_F_HOST_TSO6, true),
3451 DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
3452 VIRTIO_NET_F_HOST_ECN, true),
3453 DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
3454 VIRTIO_NET_F_HOST_UFO, true),
3455 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
3456 VIRTIO_NET_F_MRG_RXBUF, true),
3457 DEFINE_PROP_BIT64("status", VirtIONet, host_features,
3458 VIRTIO_NET_F_STATUS, true),
3459 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
3460 VIRTIO_NET_F_CTRL_VQ, true),
3461 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
3462 VIRTIO_NET_F_CTRL_RX, true),
3463 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
3464 VIRTIO_NET_F_CTRL_VLAN, true),
3465 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
3466 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3467 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
3468 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3469 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
3470 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3471 DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
3472 DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
3473 VIRTIO_NET_F_RSS, false),
3474 DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3475 VIRTIO_NET_F_HASH_REPORT, false),
3476 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
3477 VIRTIO_NET_F_RSC_EXT, false),
3478 DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
3479 VIRTIO_NET_RSC_DEFAULT_INTERVAL),
3480 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
3481 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
3482 TX_TIMER_INTERVAL),
3483 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
3484 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
3485 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
3486 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
3487 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
3488 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3489 DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
3490 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
3491 true),
3492 DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
3493 DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
3494 DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
3495 DEFINE_PROP_END_OF_LIST(),
3498 static void virtio_net_class_init(ObjectClass *klass, void *data)
3500 DeviceClass *dc = DEVICE_CLASS(klass);
3501 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3503 device_class_set_props(dc, virtio_net_properties);
3504 dc->vmsd = &vmstate_virtio_net;
3505 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3506 vdc->realize = virtio_net_device_realize;
3507 vdc->unrealize = virtio_net_device_unrealize;
3508 vdc->get_config = virtio_net_get_config;
3509 vdc->set_config = virtio_net_set_config;
3510 vdc->get_features = virtio_net_get_features;
3511 vdc->set_features = virtio_net_set_features;
3512 vdc->bad_features = virtio_net_bad_features;
3513 vdc->reset = virtio_net_reset;
3514 vdc->set_status = virtio_net_set_status;
3515 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
3516 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
3517 vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
3518 vdc->post_load = virtio_net_post_load_virtio;
3519 vdc->vmsd = &vmstate_virtio_net_device;
3520 vdc->primary_unplug_pending = primary_unplug_pending;
3523 static const TypeInfo virtio_net_info = {
3524 .name = TYPE_VIRTIO_NET,
3525 .parent = TYPE_VIRTIO_DEVICE,
3526 .instance_size = sizeof(VirtIONet),
3527 .instance_init = virtio_net_instance_init,
3528 .class_init = virtio_net_class_init,
3531 static void virtio_register_types(void)
3533 type_register_static(&virtio_net_info);
3536 type_init(virtio_register_types)