failover: Remove external partially_hotplugged property
[qemu/ar7.git] / hw / net / virtio-net.c
blob6ca85627d84f449189600ca91f2bdb9bcf665119
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;
795 if (n->primary_dev) {
796 return;
799 n->primary_device_opts = qemu_opts_find(qemu_find_opts("device"),
800 n->primary_device_id);
801 if (n->primary_device_opts) {
802 n->primary_dev = qdev_device_add(n->primary_device_opts, &err);
803 if (err) {
804 qemu_opts_del(n->primary_device_opts);
806 if (n->primary_dev) {
807 if (err) {
808 qdev_unplug(n->primary_dev, &err);
809 qdev_set_id(n->primary_dev, "");
813 } else {
814 error_setg(errp, "Primary device not found");
815 error_append_hint(errp, "Virtio-net failover will not work. Make "
816 "sure primary device has parameter"
817 " failover_pair_id=<virtio-net-id>\n");
819 error_propagate(errp, err);
822 static int is_my_primary(void *opaque, QemuOpts *opts, Error **errp)
824 VirtIONet *n = opaque;
825 int ret = 0;
826 const char *standby_id = qemu_opt_get(opts, "failover_pair_id");
828 if (standby_id != NULL && (g_strcmp0(standby_id, n->netclient_name) == 0)) {
829 n->primary_device_id = g_strdup(opts->id);
830 ret = 1;
833 return ret;
836 static DeviceState *virtio_net_find_primary(VirtIONet *n, Error **errp)
838 DeviceState *dev = NULL;
839 Error *err = NULL;
841 if (qemu_opts_foreach(qemu_find_opts("device"),
842 is_my_primary, n, &err)) {
843 if (err) {
844 error_propagate(errp, err);
845 return NULL;
847 if (n->primary_device_id) {
848 dev = qdev_find_recursive(sysbus_get_default(),
849 n->primary_device_id);
850 } else {
851 error_setg(errp, "Primary device id not found");
852 return NULL;
855 return dev;
858 static DeviceState *virtio_connect_failover_devices(VirtIONet *n, Error **errp)
860 DeviceState *prim_dev = NULL;
861 Error *err = NULL;
863 prim_dev = virtio_net_find_primary(n, &err);
864 if (prim_dev) {
865 n->primary_device_id = g_strdup(prim_dev->id);
866 n->primary_device_opts = prim_dev->opts;
867 } else {
868 error_propagate(errp, err);
871 return prim_dev;
874 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
876 VirtIONet *n = VIRTIO_NET(vdev);
877 Error *err = NULL;
878 int i;
880 if (n->mtu_bypass_backend &&
881 !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
882 features &= ~(1ULL << VIRTIO_NET_F_MTU);
885 virtio_net_set_multiqueue(n,
886 virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
887 virtio_has_feature(features, VIRTIO_NET_F_MQ));
889 virtio_net_set_mrg_rx_bufs(n,
890 virtio_has_feature(features,
891 VIRTIO_NET_F_MRG_RXBUF),
892 virtio_has_feature(features,
893 VIRTIO_F_VERSION_1),
894 virtio_has_feature(features,
895 VIRTIO_NET_F_HASH_REPORT));
897 n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
898 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
899 n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
900 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
901 n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
903 if (n->has_vnet_hdr) {
904 n->curr_guest_offloads =
905 virtio_net_guest_offloads_by_features(features);
906 virtio_net_apply_guest_offloads(n);
909 for (i = 0; i < n->max_queues; i++) {
910 NetClientState *nc = qemu_get_subqueue(n->nic, i);
912 if (!get_vhost_net(nc->peer)) {
913 continue;
915 vhost_net_ack_features(get_vhost_net(nc->peer), features);
918 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
919 memset(n->vlans, 0, MAX_VLAN >> 3);
920 } else {
921 memset(n->vlans, 0xff, MAX_VLAN >> 3);
924 if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
925 qapi_event_send_failover_negotiated(n->netclient_name);
926 qatomic_set(&n->primary_should_be_hidden, false);
927 failover_add_primary(n, &err);
928 if (err) {
929 n->primary_dev = virtio_connect_failover_devices(n, &err);
930 if (err) {
931 goto out_err;
933 failover_add_primary(n, &err);
934 if (err) {
935 goto out_err;
939 return;
941 out_err:
942 if (err) {
943 warn_report_err(err);
947 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
948 struct iovec *iov, unsigned int iov_cnt)
950 uint8_t on;
951 size_t s;
952 NetClientState *nc = qemu_get_queue(n->nic);
954 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
955 if (s != sizeof(on)) {
956 return VIRTIO_NET_ERR;
959 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
960 n->promisc = on;
961 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
962 n->allmulti = on;
963 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
964 n->alluni = on;
965 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
966 n->nomulti = on;
967 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
968 n->nouni = on;
969 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
970 n->nobcast = on;
971 } else {
972 return VIRTIO_NET_ERR;
975 rxfilter_notify(nc);
977 return VIRTIO_NET_OK;
980 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
981 struct iovec *iov, unsigned int iov_cnt)
983 VirtIODevice *vdev = VIRTIO_DEVICE(n);
984 uint64_t offloads;
985 size_t s;
987 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
988 return VIRTIO_NET_ERR;
991 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
992 if (s != sizeof(offloads)) {
993 return VIRTIO_NET_ERR;
996 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
997 uint64_t supported_offloads;
999 offloads = virtio_ldq_p(vdev, &offloads);
1001 if (!n->has_vnet_hdr) {
1002 return VIRTIO_NET_ERR;
1005 n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
1006 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
1007 n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
1008 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
1009 virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
1011 supported_offloads = virtio_net_supported_guest_offloads(n);
1012 if (offloads & ~supported_offloads) {
1013 return VIRTIO_NET_ERR;
1016 n->curr_guest_offloads = offloads;
1017 virtio_net_apply_guest_offloads(n);
1019 return VIRTIO_NET_OK;
1020 } else {
1021 return VIRTIO_NET_ERR;
1025 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
1026 struct iovec *iov, unsigned int iov_cnt)
1028 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1029 struct virtio_net_ctrl_mac mac_data;
1030 size_t s;
1031 NetClientState *nc = qemu_get_queue(n->nic);
1033 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
1034 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
1035 return VIRTIO_NET_ERR;
1037 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
1038 assert(s == sizeof(n->mac));
1039 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1040 rxfilter_notify(nc);
1042 return VIRTIO_NET_OK;
1045 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
1046 return VIRTIO_NET_ERR;
1049 int in_use = 0;
1050 int first_multi = 0;
1051 uint8_t uni_overflow = 0;
1052 uint8_t multi_overflow = 0;
1053 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1055 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1056 sizeof(mac_data.entries));
1057 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1058 if (s != sizeof(mac_data.entries)) {
1059 goto error;
1061 iov_discard_front(&iov, &iov_cnt, s);
1063 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1064 goto error;
1067 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1068 s = iov_to_buf(iov, iov_cnt, 0, macs,
1069 mac_data.entries * ETH_ALEN);
1070 if (s != mac_data.entries * ETH_ALEN) {
1071 goto error;
1073 in_use += mac_data.entries;
1074 } else {
1075 uni_overflow = 1;
1078 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
1080 first_multi = in_use;
1082 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1083 sizeof(mac_data.entries));
1084 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1085 if (s != sizeof(mac_data.entries)) {
1086 goto error;
1089 iov_discard_front(&iov, &iov_cnt, s);
1091 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1092 goto error;
1095 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1096 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
1097 mac_data.entries * ETH_ALEN);
1098 if (s != mac_data.entries * ETH_ALEN) {
1099 goto error;
1101 in_use += mac_data.entries;
1102 } else {
1103 multi_overflow = 1;
1106 n->mac_table.in_use = in_use;
1107 n->mac_table.first_multi = first_multi;
1108 n->mac_table.uni_overflow = uni_overflow;
1109 n->mac_table.multi_overflow = multi_overflow;
1110 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1111 g_free(macs);
1112 rxfilter_notify(nc);
1114 return VIRTIO_NET_OK;
1116 error:
1117 g_free(macs);
1118 return VIRTIO_NET_ERR;
1121 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
1122 struct iovec *iov, unsigned int iov_cnt)
1124 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1125 uint16_t vid;
1126 size_t s;
1127 NetClientState *nc = qemu_get_queue(n->nic);
1129 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
1130 vid = virtio_lduw_p(vdev, &vid);
1131 if (s != sizeof(vid)) {
1132 return VIRTIO_NET_ERR;
1135 if (vid >= MAX_VLAN)
1136 return VIRTIO_NET_ERR;
1138 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
1139 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
1140 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
1141 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
1142 else
1143 return VIRTIO_NET_ERR;
1145 rxfilter_notify(nc);
1147 return VIRTIO_NET_OK;
1150 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1151 struct iovec *iov, unsigned int iov_cnt)
1153 trace_virtio_net_handle_announce(n->announce_timer.round);
1154 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1155 n->status & VIRTIO_NET_S_ANNOUNCE) {
1156 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
1157 if (n->announce_timer.round) {
1158 qemu_announce_timer_step(&n->announce_timer);
1160 return VIRTIO_NET_OK;
1161 } else {
1162 return VIRTIO_NET_ERR;
1166 static void virtio_net_disable_rss(VirtIONet *n)
1168 if (n->rss_data.enabled) {
1169 trace_virtio_net_rss_disable();
1171 n->rss_data.enabled = false;
1174 static uint16_t virtio_net_handle_rss(VirtIONet *n,
1175 struct iovec *iov,
1176 unsigned int iov_cnt,
1177 bool do_rss)
1179 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1180 struct virtio_net_rss_config cfg;
1181 size_t s, offset = 0, size_get;
1182 uint16_t queues, i;
1183 struct {
1184 uint16_t us;
1185 uint8_t b;
1186 } QEMU_PACKED temp;
1187 const char *err_msg = "";
1188 uint32_t err_value = 0;
1190 if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
1191 err_msg = "RSS is not negotiated";
1192 goto error;
1194 if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1195 err_msg = "Hash report is not negotiated";
1196 goto error;
1198 size_get = offsetof(struct virtio_net_rss_config, indirection_table);
1199 s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
1200 if (s != size_get) {
1201 err_msg = "Short command buffer";
1202 err_value = (uint32_t)s;
1203 goto error;
1205 n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
1206 n->rss_data.indirections_len =
1207 virtio_lduw_p(vdev, &cfg.indirection_table_mask);
1208 n->rss_data.indirections_len++;
1209 if (!do_rss) {
1210 n->rss_data.indirections_len = 1;
1212 if (!is_power_of_2(n->rss_data.indirections_len)) {
1213 err_msg = "Invalid size of indirection table";
1214 err_value = n->rss_data.indirections_len;
1215 goto error;
1217 if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
1218 err_msg = "Too large indirection table";
1219 err_value = n->rss_data.indirections_len;
1220 goto error;
1222 n->rss_data.default_queue = do_rss ?
1223 virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1224 if (n->rss_data.default_queue >= n->max_queues) {
1225 err_msg = "Invalid default queue";
1226 err_value = n->rss_data.default_queue;
1227 goto error;
1229 offset += size_get;
1230 size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
1231 g_free(n->rss_data.indirections_table);
1232 n->rss_data.indirections_table = g_malloc(size_get);
1233 if (!n->rss_data.indirections_table) {
1234 err_msg = "Can't allocate indirections table";
1235 err_value = n->rss_data.indirections_len;
1236 goto error;
1238 s = iov_to_buf(iov, iov_cnt, offset,
1239 n->rss_data.indirections_table, size_get);
1240 if (s != size_get) {
1241 err_msg = "Short indirection table buffer";
1242 err_value = (uint32_t)s;
1243 goto error;
1245 for (i = 0; i < n->rss_data.indirections_len; ++i) {
1246 uint16_t val = n->rss_data.indirections_table[i];
1247 n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
1249 offset += size_get;
1250 size_get = sizeof(temp);
1251 s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
1252 if (s != size_get) {
1253 err_msg = "Can't get queues";
1254 err_value = (uint32_t)s;
1255 goto error;
1257 queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues;
1258 if (queues == 0 || queues > n->max_queues) {
1259 err_msg = "Invalid number of queues";
1260 err_value = queues;
1261 goto error;
1263 if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
1264 err_msg = "Invalid key size";
1265 err_value = temp.b;
1266 goto error;
1268 if (!temp.b && n->rss_data.hash_types) {
1269 err_msg = "No key provided";
1270 err_value = 0;
1271 goto error;
1273 if (!temp.b && !n->rss_data.hash_types) {
1274 virtio_net_disable_rss(n);
1275 return queues;
1277 offset += size_get;
1278 size_get = temp.b;
1279 s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
1280 if (s != size_get) {
1281 err_msg = "Can get key buffer";
1282 err_value = (uint32_t)s;
1283 goto error;
1285 n->rss_data.enabled = true;
1286 trace_virtio_net_rss_enable(n->rss_data.hash_types,
1287 n->rss_data.indirections_len,
1288 temp.b);
1289 return queues;
1290 error:
1291 trace_virtio_net_rss_error(err_msg, err_value);
1292 virtio_net_disable_rss(n);
1293 return 0;
1296 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
1297 struct iovec *iov, unsigned int iov_cnt)
1299 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1300 uint16_t queues;
1302 virtio_net_disable_rss(n);
1303 if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1304 queues = virtio_net_handle_rss(n, iov, iov_cnt, false);
1305 return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1307 if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1308 queues = virtio_net_handle_rss(n, iov, iov_cnt, true);
1309 } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
1310 struct virtio_net_ctrl_mq mq;
1311 size_t s;
1312 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
1313 return VIRTIO_NET_ERR;
1315 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
1316 if (s != sizeof(mq)) {
1317 return VIRTIO_NET_ERR;
1319 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
1321 } else {
1322 return VIRTIO_NET_ERR;
1325 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1326 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1327 queues > n->max_queues ||
1328 !n->multiqueue) {
1329 return VIRTIO_NET_ERR;
1332 n->curr_queues = queues;
1333 /* stop the backend before changing the number of queues to avoid handling a
1334 * disabled queue */
1335 virtio_net_set_status(vdev, vdev->status);
1336 virtio_net_set_queues(n);
1338 return VIRTIO_NET_OK;
1341 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1343 VirtIONet *n = VIRTIO_NET(vdev);
1344 struct virtio_net_ctrl_hdr ctrl;
1345 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1346 VirtQueueElement *elem;
1347 size_t s;
1348 struct iovec *iov, *iov2;
1349 unsigned int iov_cnt;
1351 for (;;) {
1352 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
1353 if (!elem) {
1354 break;
1356 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
1357 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
1358 virtio_error(vdev, "virtio-net ctrl missing headers");
1359 virtqueue_detach_element(vq, elem, 0);
1360 g_free(elem);
1361 break;
1364 iov_cnt = elem->out_num;
1365 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
1366 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
1367 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
1368 if (s != sizeof(ctrl)) {
1369 status = VIRTIO_NET_ERR;
1370 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1371 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
1372 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1373 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
1374 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1375 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
1376 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1377 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
1378 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1379 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
1380 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1381 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
1384 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
1385 assert(s == sizeof(status));
1387 virtqueue_push(vq, elem, sizeof(status));
1388 virtio_notify(vdev, vq);
1389 g_free(iov2);
1390 g_free(elem);
1394 /* RX */
1396 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
1398 VirtIONet *n = VIRTIO_NET(vdev);
1399 int queue_index = vq2q(virtio_get_queue_index(vq));
1401 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
1404 static bool virtio_net_can_receive(NetClientState *nc)
1406 VirtIONet *n = qemu_get_nic_opaque(nc);
1407 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1408 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1410 if (!vdev->vm_running) {
1411 return false;
1414 if (nc->queue_index >= n->curr_queues) {
1415 return false;
1418 if (!virtio_queue_ready(q->rx_vq) ||
1419 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1420 return false;
1423 return true;
1426 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
1428 VirtIONet *n = q->n;
1429 if (virtio_queue_empty(q->rx_vq) ||
1430 (n->mergeable_rx_bufs &&
1431 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1432 virtio_queue_set_notification(q->rx_vq, 1);
1434 /* To avoid a race condition where the guest has made some buffers
1435 * available after the above check but before notification was
1436 * enabled, check for available buffers again.
1438 if (virtio_queue_empty(q->rx_vq) ||
1439 (n->mergeable_rx_bufs &&
1440 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1441 return 0;
1445 virtio_queue_set_notification(q->rx_vq, 0);
1446 return 1;
1449 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1451 virtio_tswap16s(vdev, &hdr->hdr_len);
1452 virtio_tswap16s(vdev, &hdr->gso_size);
1453 virtio_tswap16s(vdev, &hdr->csum_start);
1454 virtio_tswap16s(vdev, &hdr->csum_offset);
1457 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1458 * it never finds out that the packets don't have valid checksums. This
1459 * causes dhclient to get upset. Fedora's carried a patch for ages to
1460 * fix this with Xen but it hasn't appeared in an upstream release of
1461 * dhclient yet.
1463 * To avoid breaking existing guests, we catch udp packets and add
1464 * checksums. This is terrible but it's better than hacking the guest
1465 * kernels.
1467 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1468 * we should provide a mechanism to disable it to avoid polluting the host
1469 * cache.
1471 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1472 uint8_t *buf, size_t size)
1474 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1475 (size > 27 && size < 1500) && /* normal sized MTU */
1476 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1477 (buf[23] == 17) && /* ip.protocol == UDP */
1478 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1479 net_checksum_calculate(buf, size);
1480 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1484 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1485 const void *buf, size_t size)
1487 if (n->has_vnet_hdr) {
1488 /* FIXME this cast is evil */
1489 void *wbuf = (void *)buf;
1490 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1491 size - n->host_hdr_len);
1493 if (n->needs_vnet_hdr_swap) {
1494 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1496 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1497 } else {
1498 struct virtio_net_hdr hdr = {
1499 .flags = 0,
1500 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1502 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1506 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1508 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1509 static const uint8_t vlan[] = {0x81, 0x00};
1510 uint8_t *ptr = (uint8_t *)buf;
1511 int i;
1513 if (n->promisc)
1514 return 1;
1516 ptr += n->host_hdr_len;
1518 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1519 int vid = lduw_be_p(ptr + 14) & 0xfff;
1520 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1521 return 0;
1524 if (ptr[0] & 1) { // multicast
1525 if (!memcmp(ptr, bcast, sizeof(bcast))) {
1526 return !n->nobcast;
1527 } else if (n->nomulti) {
1528 return 0;
1529 } else if (n->allmulti || n->mac_table.multi_overflow) {
1530 return 1;
1533 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1534 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1535 return 1;
1538 } else { // unicast
1539 if (n->nouni) {
1540 return 0;
1541 } else if (n->alluni || n->mac_table.uni_overflow) {
1542 return 1;
1543 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1544 return 1;
1547 for (i = 0; i < n->mac_table.first_multi; i++) {
1548 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1549 return 1;
1554 return 0;
1557 static uint8_t virtio_net_get_hash_type(bool isip4,
1558 bool isip6,
1559 bool isudp,
1560 bool istcp,
1561 uint32_t types)
1563 if (isip4) {
1564 if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) {
1565 return NetPktRssIpV4Tcp;
1567 if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) {
1568 return NetPktRssIpV4Udp;
1570 if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
1571 return NetPktRssIpV4;
1573 } else if (isip6) {
1574 uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
1575 VIRTIO_NET_RSS_HASH_TYPE_TCPv6;
1577 if (istcp && (types & mask)) {
1578 return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ?
1579 NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp;
1581 mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6;
1582 if (isudp && (types & mask)) {
1583 return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ?
1584 NetPktRssIpV6UdpEx : NetPktRssIpV6Udp;
1586 mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6;
1587 if (types & mask) {
1588 return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ?
1589 NetPktRssIpV6Ex : NetPktRssIpV6;
1592 return 0xff;
1595 static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1596 uint32_t hash)
1598 struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1599 hdr->hash_value = hash;
1600 hdr->hash_report = report;
1603 static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
1604 size_t size)
1606 VirtIONet *n = qemu_get_nic_opaque(nc);
1607 unsigned int index = nc->queue_index, new_index = index;
1608 struct NetRxPkt *pkt = n->rx_pkt;
1609 uint8_t net_hash_type;
1610 uint32_t hash;
1611 bool isip4, isip6, isudp, istcp;
1612 static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1613 VIRTIO_NET_HASH_REPORT_IPv4,
1614 VIRTIO_NET_HASH_REPORT_TCPv4,
1615 VIRTIO_NET_HASH_REPORT_TCPv6,
1616 VIRTIO_NET_HASH_REPORT_IPv6,
1617 VIRTIO_NET_HASH_REPORT_IPv6_EX,
1618 VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1619 VIRTIO_NET_HASH_REPORT_UDPv4,
1620 VIRTIO_NET_HASH_REPORT_UDPv6,
1621 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1624 net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len,
1625 size - n->host_hdr_len);
1626 net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
1627 if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) {
1628 istcp = isudp = false;
1630 if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) {
1631 istcp = isudp = false;
1633 net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp,
1634 n->rss_data.hash_types);
1635 if (net_hash_type > NetPktRssIpV6UdpEx) {
1636 if (n->rss_data.populate_hash) {
1637 virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1639 return n->rss_data.redirect ? n->rss_data.default_queue : -1;
1642 hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1644 if (n->rss_data.populate_hash) {
1645 virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1648 if (n->rss_data.redirect) {
1649 new_index = hash & (n->rss_data.indirections_len - 1);
1650 new_index = n->rss_data.indirections_table[new_index];
1653 return (index == new_index) ? -1 : new_index;
1656 static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1657 size_t size, bool no_rss)
1659 VirtIONet *n = qemu_get_nic_opaque(nc);
1660 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1661 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1662 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1663 struct virtio_net_hdr_mrg_rxbuf mhdr;
1664 unsigned mhdr_cnt = 0;
1665 size_t offset, i, guest_offset;
1667 if (!virtio_net_can_receive(nc)) {
1668 return -1;
1671 if (!no_rss && n->rss_data.enabled) {
1672 int index = virtio_net_process_rss(nc, buf, size);
1673 if (index >= 0) {
1674 NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
1675 return virtio_net_receive_rcu(nc2, buf, size, true);
1679 /* hdr_len refers to the header we supply to the guest */
1680 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1681 return 0;
1684 if (!receive_filter(n, buf, size))
1685 return size;
1687 offset = i = 0;
1689 while (offset < size) {
1690 VirtQueueElement *elem;
1691 int len, total;
1692 const struct iovec *sg;
1694 total = 0;
1696 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1697 if (!elem) {
1698 if (i) {
1699 virtio_error(vdev, "virtio-net unexpected empty queue: "
1700 "i %zd mergeable %d offset %zd, size %zd, "
1701 "guest hdr len %zd, host hdr len %zd "
1702 "guest features 0x%" PRIx64,
1703 i, n->mergeable_rx_bufs, offset, size,
1704 n->guest_hdr_len, n->host_hdr_len,
1705 vdev->guest_features);
1707 return -1;
1710 if (elem->in_num < 1) {
1711 virtio_error(vdev,
1712 "virtio-net receive queue contains no in buffers");
1713 virtqueue_detach_element(q->rx_vq, elem, 0);
1714 g_free(elem);
1715 return -1;
1718 sg = elem->in_sg;
1719 if (i == 0) {
1720 assert(offset == 0);
1721 if (n->mergeable_rx_bufs) {
1722 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1723 sg, elem->in_num,
1724 offsetof(typeof(mhdr), num_buffers),
1725 sizeof(mhdr.num_buffers));
1728 receive_header(n, sg, elem->in_num, buf, size);
1729 if (n->rss_data.populate_hash) {
1730 offset = sizeof(mhdr);
1731 iov_from_buf(sg, elem->in_num, offset,
1732 buf + offset, n->host_hdr_len - sizeof(mhdr));
1734 offset = n->host_hdr_len;
1735 total += n->guest_hdr_len;
1736 guest_offset = n->guest_hdr_len;
1737 } else {
1738 guest_offset = 0;
1741 /* copy in packet. ugh */
1742 len = iov_from_buf(sg, elem->in_num, guest_offset,
1743 buf + offset, size - offset);
1744 total += len;
1745 offset += len;
1746 /* If buffers can't be merged, at this point we
1747 * must have consumed the complete packet.
1748 * Otherwise, drop it. */
1749 if (!n->mergeable_rx_bufs && offset < size) {
1750 virtqueue_unpop(q->rx_vq, elem, total);
1751 g_free(elem);
1752 return size;
1755 /* signal other side */
1756 virtqueue_fill(q->rx_vq, elem, total, i++);
1757 g_free(elem);
1760 if (mhdr_cnt) {
1761 virtio_stw_p(vdev, &mhdr.num_buffers, i);
1762 iov_from_buf(mhdr_sg, mhdr_cnt,
1764 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1767 virtqueue_flush(q->rx_vq, i);
1768 virtio_notify(vdev, q->rx_vq);
1770 return size;
1773 static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
1774 size_t size)
1776 RCU_READ_LOCK_GUARD();
1778 return virtio_net_receive_rcu(nc, buf, size, false);
1781 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
1782 const uint8_t *buf,
1783 VirtioNetRscUnit *unit)
1785 uint16_t ip_hdrlen;
1786 struct ip_header *ip;
1788 ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
1789 + sizeof(struct eth_header));
1790 unit->ip = (void *)ip;
1791 ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
1792 unit->ip_plen = &ip->ip_len;
1793 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
1794 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1795 unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
1798 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
1799 const uint8_t *buf,
1800 VirtioNetRscUnit *unit)
1802 struct ip6_header *ip6;
1804 ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
1805 + sizeof(struct eth_header));
1806 unit->ip = ip6;
1807 unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
1808 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
1809 + sizeof(struct ip6_header));
1810 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1812 /* There is a difference between payload lenght in ipv4 and v6,
1813 ip header is excluded in ipv6 */
1814 unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
1817 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
1818 VirtioNetRscSeg *seg)
1820 int ret;
1821 struct virtio_net_hdr_v1 *h;
1823 h = (struct virtio_net_hdr_v1 *)seg->buf;
1824 h->flags = 0;
1825 h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
1827 if (seg->is_coalesced) {
1828 h->rsc.segments = seg->packets;
1829 h->rsc.dup_acks = seg->dup_ack;
1830 h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
1831 if (chain->proto == ETH_P_IP) {
1832 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1833 } else {
1834 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1838 ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
1839 QTAILQ_REMOVE(&chain->buffers, seg, next);
1840 g_free(seg->buf);
1841 g_free(seg);
1843 return ret;
1846 static void virtio_net_rsc_purge(void *opq)
1848 VirtioNetRscSeg *seg, *rn;
1849 VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
1851 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
1852 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
1853 chain->stat.purge_failed++;
1854 continue;
1858 chain->stat.timer++;
1859 if (!QTAILQ_EMPTY(&chain->buffers)) {
1860 timer_mod(chain->drain_timer,
1861 qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
1865 static void virtio_net_rsc_cleanup(VirtIONet *n)
1867 VirtioNetRscChain *chain, *rn_chain;
1868 VirtioNetRscSeg *seg, *rn_seg;
1870 QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
1871 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
1872 QTAILQ_REMOVE(&chain->buffers, seg, next);
1873 g_free(seg->buf);
1874 g_free(seg);
1877 timer_del(chain->drain_timer);
1878 timer_free(chain->drain_timer);
1879 QTAILQ_REMOVE(&n->rsc_chains, chain, next);
1880 g_free(chain);
1884 static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
1885 NetClientState *nc,
1886 const uint8_t *buf, size_t size)
1888 uint16_t hdr_len;
1889 VirtioNetRscSeg *seg;
1891 hdr_len = chain->n->guest_hdr_len;
1892 seg = g_malloc(sizeof(VirtioNetRscSeg));
1893 seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
1894 + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
1895 memcpy(seg->buf, buf, size);
1896 seg->size = size;
1897 seg->packets = 1;
1898 seg->dup_ack = 0;
1899 seg->is_coalesced = 0;
1900 seg->nc = nc;
1902 QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
1903 chain->stat.cache++;
1905 switch (chain->proto) {
1906 case ETH_P_IP:
1907 virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
1908 break;
1909 case ETH_P_IPV6:
1910 virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
1911 break;
1912 default:
1913 g_assert_not_reached();
1917 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
1918 VirtioNetRscSeg *seg,
1919 const uint8_t *buf,
1920 struct tcp_header *n_tcp,
1921 struct tcp_header *o_tcp)
1923 uint32_t nack, oack;
1924 uint16_t nwin, owin;
1926 nack = htonl(n_tcp->th_ack);
1927 nwin = htons(n_tcp->th_win);
1928 oack = htonl(o_tcp->th_ack);
1929 owin = htons(o_tcp->th_win);
1931 if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
1932 chain->stat.ack_out_of_win++;
1933 return RSC_FINAL;
1934 } else if (nack == oack) {
1935 /* duplicated ack or window probe */
1936 if (nwin == owin) {
1937 /* duplicated ack, add dup ack count due to whql test up to 1 */
1938 chain->stat.dup_ack++;
1939 return RSC_FINAL;
1940 } else {
1941 /* Coalesce window update */
1942 o_tcp->th_win = n_tcp->th_win;
1943 chain->stat.win_update++;
1944 return RSC_COALESCE;
1946 } else {
1947 /* pure ack, go to 'C', finalize*/
1948 chain->stat.pure_ack++;
1949 return RSC_FINAL;
1953 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
1954 VirtioNetRscSeg *seg,
1955 const uint8_t *buf,
1956 VirtioNetRscUnit *n_unit)
1958 void *data;
1959 uint16_t o_ip_len;
1960 uint32_t nseq, oseq;
1961 VirtioNetRscUnit *o_unit;
1963 o_unit = &seg->unit;
1964 o_ip_len = htons(*o_unit->ip_plen);
1965 nseq = htonl(n_unit->tcp->th_seq);
1966 oseq = htonl(o_unit->tcp->th_seq);
1968 /* out of order or retransmitted. */
1969 if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
1970 chain->stat.data_out_of_win++;
1971 return RSC_FINAL;
1974 data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
1975 if (nseq == oseq) {
1976 if ((o_unit->payload == 0) && n_unit->payload) {
1977 /* From no payload to payload, normal case, not a dup ack or etc */
1978 chain->stat.data_after_pure_ack++;
1979 goto coalesce;
1980 } else {
1981 return virtio_net_rsc_handle_ack(chain, seg, buf,
1982 n_unit->tcp, o_unit->tcp);
1984 } else if ((nseq - oseq) != o_unit->payload) {
1985 /* Not a consistent packet, out of order */
1986 chain->stat.data_out_of_order++;
1987 return RSC_FINAL;
1988 } else {
1989 coalesce:
1990 if ((o_ip_len + n_unit->payload) > chain->max_payload) {
1991 chain->stat.over_size++;
1992 return RSC_FINAL;
1995 /* Here comes the right data, the payload length in v4/v6 is different,
1996 so use the field value to update and record the new data len */
1997 o_unit->payload += n_unit->payload; /* update new data len */
1999 /* update field in ip header */
2000 *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
2002 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
2003 for windows guest, while this may change the behavior for linux
2004 guest (only if it uses RSC feature). */
2005 o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
2007 o_unit->tcp->th_ack = n_unit->tcp->th_ack;
2008 o_unit->tcp->th_win = n_unit->tcp->th_win;
2010 memmove(seg->buf + seg->size, data, n_unit->payload);
2011 seg->size += n_unit->payload;
2012 seg->packets++;
2013 chain->stat.coalesced++;
2014 return RSC_COALESCE;
2018 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
2019 VirtioNetRscSeg *seg,
2020 const uint8_t *buf, size_t size,
2021 VirtioNetRscUnit *unit)
2023 struct ip_header *ip1, *ip2;
2025 ip1 = (struct ip_header *)(unit->ip);
2026 ip2 = (struct ip_header *)(seg->unit.ip);
2027 if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
2028 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2029 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2030 chain->stat.no_match++;
2031 return RSC_NO_MATCH;
2034 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2037 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
2038 VirtioNetRscSeg *seg,
2039 const uint8_t *buf, size_t size,
2040 VirtioNetRscUnit *unit)
2042 struct ip6_header *ip1, *ip2;
2044 ip1 = (struct ip6_header *)(unit->ip);
2045 ip2 = (struct ip6_header *)(seg->unit.ip);
2046 if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
2047 || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
2048 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2049 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2050 chain->stat.no_match++;
2051 return RSC_NO_MATCH;
2054 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2057 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2058 * to prevent out of order */
2059 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
2060 struct tcp_header *tcp)
2062 uint16_t tcp_hdr;
2063 uint16_t tcp_flag;
2065 tcp_flag = htons(tcp->th_offset_flags);
2066 tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
2067 tcp_flag &= VIRTIO_NET_TCP_FLAG;
2068 if (tcp_flag & TH_SYN) {
2069 chain->stat.tcp_syn++;
2070 return RSC_BYPASS;
2073 if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
2074 chain->stat.tcp_ctrl_drain++;
2075 return RSC_FINAL;
2078 if (tcp_hdr > sizeof(struct tcp_header)) {
2079 chain->stat.tcp_all_opt++;
2080 return RSC_FINAL;
2083 return RSC_CANDIDATE;
2086 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
2087 NetClientState *nc,
2088 const uint8_t *buf, size_t size,
2089 VirtioNetRscUnit *unit)
2091 int ret;
2092 VirtioNetRscSeg *seg, *nseg;
2094 if (QTAILQ_EMPTY(&chain->buffers)) {
2095 chain->stat.empty_cache++;
2096 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2097 timer_mod(chain->drain_timer,
2098 qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
2099 return size;
2102 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2103 if (chain->proto == ETH_P_IP) {
2104 ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
2105 } else {
2106 ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
2109 if (ret == RSC_FINAL) {
2110 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2111 /* Send failed */
2112 chain->stat.final_failed++;
2113 return 0;
2116 /* Send current packet */
2117 return virtio_net_do_receive(nc, buf, size);
2118 } else if (ret == RSC_NO_MATCH) {
2119 continue;
2120 } else {
2121 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2122 seg->is_coalesced = 1;
2123 return size;
2127 chain->stat.no_match_cache++;
2128 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2129 return size;
2132 /* Drain a connection data, this is to avoid out of order segments */
2133 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
2134 NetClientState *nc,
2135 const uint8_t *buf, size_t size,
2136 uint16_t ip_start, uint16_t ip_size,
2137 uint16_t tcp_port)
2139 VirtioNetRscSeg *seg, *nseg;
2140 uint32_t ppair1, ppair2;
2142 ppair1 = *(uint32_t *)(buf + tcp_port);
2143 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2144 ppair2 = *(uint32_t *)(seg->buf + tcp_port);
2145 if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
2146 || (ppair1 != ppair2)) {
2147 continue;
2149 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2150 chain->stat.drain_failed++;
2153 break;
2156 return virtio_net_do_receive(nc, buf, size);
2159 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
2160 struct ip_header *ip,
2161 const uint8_t *buf, size_t size)
2163 uint16_t ip_len;
2165 /* Not an ipv4 packet */
2166 if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
2167 chain->stat.ip_option++;
2168 return RSC_BYPASS;
2171 /* Don't handle packets with ip option */
2172 if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
2173 chain->stat.ip_option++;
2174 return RSC_BYPASS;
2177 if (ip->ip_p != IPPROTO_TCP) {
2178 chain->stat.bypass_not_tcp++;
2179 return RSC_BYPASS;
2182 /* Don't handle packets with ip fragment */
2183 if (!(htons(ip->ip_off) & IP_DF)) {
2184 chain->stat.ip_frag++;
2185 return RSC_BYPASS;
2188 /* Don't handle packets with ecn flag */
2189 if (IPTOS_ECN(ip->ip_tos)) {
2190 chain->stat.ip_ecn++;
2191 return RSC_BYPASS;
2194 ip_len = htons(ip->ip_len);
2195 if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
2196 || ip_len > (size - chain->n->guest_hdr_len -
2197 sizeof(struct eth_header))) {
2198 chain->stat.ip_hacked++;
2199 return RSC_BYPASS;
2202 return RSC_CANDIDATE;
2205 static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
2206 NetClientState *nc,
2207 const uint8_t *buf, size_t size)
2209 int32_t ret;
2210 uint16_t hdr_len;
2211 VirtioNetRscUnit unit;
2213 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2215 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
2216 + sizeof(struct tcp_header))) {
2217 chain->stat.bypass_not_tcp++;
2218 return virtio_net_do_receive(nc, buf, size);
2221 virtio_net_rsc_extract_unit4(chain, buf, &unit);
2222 if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
2223 != RSC_CANDIDATE) {
2224 return virtio_net_do_receive(nc, buf, size);
2227 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2228 if (ret == RSC_BYPASS) {
2229 return virtio_net_do_receive(nc, buf, size);
2230 } else if (ret == RSC_FINAL) {
2231 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2232 ((hdr_len + sizeof(struct eth_header)) + 12),
2233 VIRTIO_NET_IP4_ADDR_SIZE,
2234 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
2237 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2240 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
2241 struct ip6_header *ip6,
2242 const uint8_t *buf, size_t size)
2244 uint16_t ip_len;
2246 if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
2247 != IP_HEADER_VERSION_6) {
2248 return RSC_BYPASS;
2251 /* Both option and protocol is checked in this */
2252 if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
2253 chain->stat.bypass_not_tcp++;
2254 return RSC_BYPASS;
2257 ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
2258 if (ip_len < sizeof(struct tcp_header) ||
2259 ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
2260 - sizeof(struct ip6_header))) {
2261 chain->stat.ip_hacked++;
2262 return RSC_BYPASS;
2265 /* Don't handle packets with ecn flag */
2266 if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
2267 chain->stat.ip_ecn++;
2268 return RSC_BYPASS;
2271 return RSC_CANDIDATE;
2274 static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
2275 const uint8_t *buf, size_t size)
2277 int32_t ret;
2278 uint16_t hdr_len;
2279 VirtioNetRscChain *chain;
2280 VirtioNetRscUnit unit;
2282 chain = (VirtioNetRscChain *)opq;
2283 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2285 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
2286 + sizeof(tcp_header))) {
2287 return virtio_net_do_receive(nc, buf, size);
2290 virtio_net_rsc_extract_unit6(chain, buf, &unit);
2291 if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
2292 unit.ip, buf, size)) {
2293 return virtio_net_do_receive(nc, buf, size);
2296 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2297 if (ret == RSC_BYPASS) {
2298 return virtio_net_do_receive(nc, buf, size);
2299 } else if (ret == RSC_FINAL) {
2300 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2301 ((hdr_len + sizeof(struct eth_header)) + 8),
2302 VIRTIO_NET_IP6_ADDR_SIZE,
2303 hdr_len + sizeof(struct eth_header)
2304 + sizeof(struct ip6_header));
2307 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2310 static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
2311 NetClientState *nc,
2312 uint16_t proto)
2314 VirtioNetRscChain *chain;
2316 if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
2317 return NULL;
2320 QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
2321 if (chain->proto == proto) {
2322 return chain;
2326 chain = g_malloc(sizeof(*chain));
2327 chain->n = n;
2328 chain->proto = proto;
2329 if (proto == (uint16_t)ETH_P_IP) {
2330 chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
2331 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2332 } else {
2333 chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
2334 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2336 chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
2337 virtio_net_rsc_purge, chain);
2338 memset(&chain->stat, 0, sizeof(chain->stat));
2340 QTAILQ_INIT(&chain->buffers);
2341 QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
2343 return chain;
2346 static ssize_t virtio_net_rsc_receive(NetClientState *nc,
2347 const uint8_t *buf,
2348 size_t size)
2350 uint16_t proto;
2351 VirtioNetRscChain *chain;
2352 struct eth_header *eth;
2353 VirtIONet *n;
2355 n = qemu_get_nic_opaque(nc);
2356 if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
2357 return virtio_net_do_receive(nc, buf, size);
2360 eth = (struct eth_header *)(buf + n->guest_hdr_len);
2361 proto = htons(eth->h_proto);
2363 chain = virtio_net_rsc_lookup_chain(n, nc, proto);
2364 if (chain) {
2365 chain->stat.received++;
2366 if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
2367 return virtio_net_rsc_receive4(chain, nc, buf, size);
2368 } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
2369 return virtio_net_rsc_receive6(chain, nc, buf, size);
2372 return virtio_net_do_receive(nc, buf, size);
2375 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
2376 size_t size)
2378 VirtIONet *n = qemu_get_nic_opaque(nc);
2379 if ((n->rsc4_enabled || n->rsc6_enabled)) {
2380 return virtio_net_rsc_receive(nc, buf, size);
2381 } else {
2382 return virtio_net_do_receive(nc, buf, size);
2386 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
2388 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
2390 VirtIONet *n = qemu_get_nic_opaque(nc);
2391 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
2392 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2394 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
2395 virtio_notify(vdev, q->tx_vq);
2397 g_free(q->async_tx.elem);
2398 q->async_tx.elem = NULL;
2400 virtio_queue_set_notification(q->tx_vq, 1);
2401 virtio_net_flush_tx(q);
2404 /* TX */
2405 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
2407 VirtIONet *n = q->n;
2408 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2409 VirtQueueElement *elem;
2410 int32_t num_packets = 0;
2411 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
2412 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2413 return num_packets;
2416 if (q->async_tx.elem) {
2417 virtio_queue_set_notification(q->tx_vq, 0);
2418 return num_packets;
2421 for (;;) {
2422 ssize_t ret;
2423 unsigned int out_num;
2424 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2425 struct virtio_net_hdr_mrg_rxbuf mhdr;
2427 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
2428 if (!elem) {
2429 break;
2432 out_num = elem->out_num;
2433 out_sg = elem->out_sg;
2434 if (out_num < 1) {
2435 virtio_error(vdev, "virtio-net header not in first element");
2436 virtqueue_detach_element(q->tx_vq, elem, 0);
2437 g_free(elem);
2438 return -EINVAL;
2441 if (n->has_vnet_hdr) {
2442 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2443 n->guest_hdr_len) {
2444 virtio_error(vdev, "virtio-net header incorrect");
2445 virtqueue_detach_element(q->tx_vq, elem, 0);
2446 g_free(elem);
2447 return -EINVAL;
2449 if (n->needs_vnet_hdr_swap) {
2450 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2451 sg2[0].iov_base = &mhdr;
2452 sg2[0].iov_len = n->guest_hdr_len;
2453 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2454 out_sg, out_num,
2455 n->guest_hdr_len, -1);
2456 if (out_num == VIRTQUEUE_MAX_SIZE) {
2457 goto drop;
2459 out_num += 1;
2460 out_sg = sg2;
2464 * If host wants to see the guest header as is, we can
2465 * pass it on unchanged. Otherwise, copy just the parts
2466 * that host is interested in.
2468 assert(n->host_hdr_len <= n->guest_hdr_len);
2469 if (n->host_hdr_len != n->guest_hdr_len) {
2470 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
2471 out_sg, out_num,
2472 0, n->host_hdr_len);
2473 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
2474 out_sg, out_num,
2475 n->guest_hdr_len, -1);
2476 out_num = sg_num;
2477 out_sg = sg;
2480 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
2481 out_sg, out_num, virtio_net_tx_complete);
2482 if (ret == 0) {
2483 virtio_queue_set_notification(q->tx_vq, 0);
2484 q->async_tx.elem = elem;
2485 return -EBUSY;
2488 drop:
2489 virtqueue_push(q->tx_vq, elem, 0);
2490 virtio_notify(vdev, q->tx_vq);
2491 g_free(elem);
2493 if (++num_packets >= n->tx_burst) {
2494 break;
2497 return num_packets;
2500 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
2502 VirtIONet *n = VIRTIO_NET(vdev);
2503 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2505 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2506 virtio_net_drop_tx_queue_data(vdev, vq);
2507 return;
2510 /* This happens when device was stopped but VCPU wasn't. */
2511 if (!vdev->vm_running) {
2512 q->tx_waiting = 1;
2513 return;
2516 if (q->tx_waiting) {
2517 virtio_queue_set_notification(vq, 1);
2518 timer_del(q->tx_timer);
2519 q->tx_waiting = 0;
2520 if (virtio_net_flush_tx(q) == -EINVAL) {
2521 return;
2523 } else {
2524 timer_mod(q->tx_timer,
2525 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2526 q->tx_waiting = 1;
2527 virtio_queue_set_notification(vq, 0);
2531 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
2533 VirtIONet *n = VIRTIO_NET(vdev);
2534 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2536 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2537 virtio_net_drop_tx_queue_data(vdev, vq);
2538 return;
2541 if (unlikely(q->tx_waiting)) {
2542 return;
2544 q->tx_waiting = 1;
2545 /* This happens when device was stopped but VCPU wasn't. */
2546 if (!vdev->vm_running) {
2547 return;
2549 virtio_queue_set_notification(vq, 0);
2550 qemu_bh_schedule(q->tx_bh);
2553 static void virtio_net_tx_timer(void *opaque)
2555 VirtIONetQueue *q = opaque;
2556 VirtIONet *n = q->n;
2557 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2558 /* This happens when device was stopped but BH wasn't. */
2559 if (!vdev->vm_running) {
2560 /* Make sure tx waiting is set, so we'll run when restarted. */
2561 assert(q->tx_waiting);
2562 return;
2565 q->tx_waiting = 0;
2567 /* Just in case the driver is not ready on more */
2568 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2569 return;
2572 virtio_queue_set_notification(q->tx_vq, 1);
2573 virtio_net_flush_tx(q);
2576 static void virtio_net_tx_bh(void *opaque)
2578 VirtIONetQueue *q = opaque;
2579 VirtIONet *n = q->n;
2580 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2581 int32_t ret;
2583 /* This happens when device was stopped but BH wasn't. */
2584 if (!vdev->vm_running) {
2585 /* Make sure tx waiting is set, so we'll run when restarted. */
2586 assert(q->tx_waiting);
2587 return;
2590 q->tx_waiting = 0;
2592 /* Just in case the driver is not ready on more */
2593 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
2594 return;
2597 ret = virtio_net_flush_tx(q);
2598 if (ret == -EBUSY || ret == -EINVAL) {
2599 return; /* Notification re-enable handled by tx_complete or device
2600 * broken */
2603 /* If we flush a full burst of packets, assume there are
2604 * more coming and immediately reschedule */
2605 if (ret >= n->tx_burst) {
2606 qemu_bh_schedule(q->tx_bh);
2607 q->tx_waiting = 1;
2608 return;
2611 /* If less than a full burst, re-enable notification and flush
2612 * anything that may have come in while we weren't looking. If
2613 * we find something, assume the guest is still active and reschedule */
2614 virtio_queue_set_notification(q->tx_vq, 1);
2615 ret = virtio_net_flush_tx(q);
2616 if (ret == -EINVAL) {
2617 return;
2618 } else if (ret > 0) {
2619 virtio_queue_set_notification(q->tx_vq, 0);
2620 qemu_bh_schedule(q->tx_bh);
2621 q->tx_waiting = 1;
2625 static void virtio_net_add_queue(VirtIONet *n, int index)
2627 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2629 n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
2630 virtio_net_handle_rx);
2632 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2633 n->vqs[index].tx_vq =
2634 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2635 virtio_net_handle_tx_timer);
2636 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2637 virtio_net_tx_timer,
2638 &n->vqs[index]);
2639 } else {
2640 n->vqs[index].tx_vq =
2641 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2642 virtio_net_handle_tx_bh);
2643 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
2646 n->vqs[index].tx_waiting = 0;
2647 n->vqs[index].n = n;
2650 static void virtio_net_del_queue(VirtIONet *n, int index)
2652 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2653 VirtIONetQueue *q = &n->vqs[index];
2654 NetClientState *nc = qemu_get_subqueue(n->nic, index);
2656 qemu_purge_queued_packets(nc);
2658 virtio_del_queue(vdev, index * 2);
2659 if (q->tx_timer) {
2660 timer_del(q->tx_timer);
2661 timer_free(q->tx_timer);
2662 q->tx_timer = NULL;
2663 } else {
2664 qemu_bh_delete(q->tx_bh);
2665 q->tx_bh = NULL;
2667 q->tx_waiting = 0;
2668 virtio_del_queue(vdev, index * 2 + 1);
2671 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
2673 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2674 int old_num_queues = virtio_get_num_queues(vdev);
2675 int new_num_queues = new_max_queues * 2 + 1;
2676 int i;
2678 assert(old_num_queues >= 3);
2679 assert(old_num_queues % 2 == 1);
2681 if (old_num_queues == new_num_queues) {
2682 return;
2686 * We always need to remove and add ctrl vq if
2687 * old_num_queues != new_num_queues. Remove ctrl_vq first,
2688 * and then we only enter one of the following two loops.
2690 virtio_del_queue(vdev, old_num_queues - 1);
2692 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2693 /* new_num_queues < old_num_queues */
2694 virtio_net_del_queue(n, i / 2);
2697 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2698 /* new_num_queues > old_num_queues */
2699 virtio_net_add_queue(n, i / 2);
2702 /* add ctrl_vq last */
2703 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2706 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
2708 int max = multiqueue ? n->max_queues : 1;
2710 n->multiqueue = multiqueue;
2711 virtio_net_change_num_queues(n, max);
2713 virtio_net_set_queues(n);
2716 static int virtio_net_post_load_device(void *opaque, int version_id)
2718 VirtIONet *n = opaque;
2719 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2720 int i, link_down;
2722 trace_virtio_net_post_load_device();
2723 virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
2724 virtio_vdev_has_feature(vdev,
2725 VIRTIO_F_VERSION_1),
2726 virtio_vdev_has_feature(vdev,
2727 VIRTIO_NET_F_HASH_REPORT));
2729 /* MAC_TABLE_ENTRIES may be different from the saved image */
2730 if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
2731 n->mac_table.in_use = 0;
2734 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
2735 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
2739 * curr_guest_offloads will be later overwritten by the
2740 * virtio_set_features_nocheck call done from the virtio_load.
2741 * Here we make sure it is preserved and restored accordingly
2742 * in the virtio_net_post_load_virtio callback.
2744 n->saved_guest_offloads = n->curr_guest_offloads;
2746 virtio_net_set_queues(n);
2748 /* Find the first multicast entry in the saved MAC filter */
2749 for (i = 0; i < n->mac_table.in_use; i++) {
2750 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
2751 break;
2754 n->mac_table.first_multi = i;
2756 /* nc.link_down can't be migrated, so infer link_down according
2757 * to link status bit in n->status */
2758 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
2759 for (i = 0; i < n->max_queues; i++) {
2760 qemu_get_subqueue(n->nic, i)->link_down = link_down;
2763 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
2764 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
2765 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
2766 QEMU_CLOCK_VIRTUAL,
2767 virtio_net_announce_timer, n);
2768 if (n->announce_timer.round) {
2769 timer_mod(n->announce_timer.tm,
2770 qemu_clock_get_ms(n->announce_timer.type));
2771 } else {
2772 qemu_announce_timer_del(&n->announce_timer, false);
2776 if (n->rss_data.enabled) {
2777 trace_virtio_net_rss_enable(n->rss_data.hash_types,
2778 n->rss_data.indirections_len,
2779 sizeof(n->rss_data.key));
2780 } else {
2781 trace_virtio_net_rss_disable();
2783 return 0;
2786 static int virtio_net_post_load_virtio(VirtIODevice *vdev)
2788 VirtIONet *n = VIRTIO_NET(vdev);
2790 * The actual needed state is now in saved_guest_offloads,
2791 * see virtio_net_post_load_device for detail.
2792 * Restore it back and apply the desired offloads.
2794 n->curr_guest_offloads = n->saved_guest_offloads;
2795 if (peer_has_vnet_hdr(n)) {
2796 virtio_net_apply_guest_offloads(n);
2799 return 0;
2802 /* tx_waiting field of a VirtIONetQueue */
2803 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
2804 .name = "virtio-net-queue-tx_waiting",
2805 .fields = (VMStateField[]) {
2806 VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
2807 VMSTATE_END_OF_LIST()
2811 static bool max_queues_gt_1(void *opaque, int version_id)
2813 return VIRTIO_NET(opaque)->max_queues > 1;
2816 static bool has_ctrl_guest_offloads(void *opaque, int version_id)
2818 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
2819 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2822 static bool mac_table_fits(void *opaque, int version_id)
2824 return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
2827 static bool mac_table_doesnt_fit(void *opaque, int version_id)
2829 return !mac_table_fits(opaque, version_id);
2832 /* This temporary type is shared by all the WITH_TMP methods
2833 * although only some fields are used by each.
2835 struct VirtIONetMigTmp {
2836 VirtIONet *parent;
2837 VirtIONetQueue *vqs_1;
2838 uint16_t curr_queues_1;
2839 uint8_t has_ufo;
2840 uint32_t has_vnet_hdr;
2843 /* The 2nd and subsequent tx_waiting flags are loaded later than
2844 * the 1st entry in the queues and only if there's more than one
2845 * entry. We use the tmp mechanism to calculate a temporary
2846 * pointer and count and also validate the count.
2849 static int virtio_net_tx_waiting_pre_save(void *opaque)
2851 struct VirtIONetMigTmp *tmp = opaque;
2853 tmp->vqs_1 = tmp->parent->vqs + 1;
2854 tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
2855 if (tmp->parent->curr_queues == 0) {
2856 tmp->curr_queues_1 = 0;
2859 return 0;
2862 static int virtio_net_tx_waiting_pre_load(void *opaque)
2864 struct VirtIONetMigTmp *tmp = opaque;
2866 /* Reuse the pointer setup from save */
2867 virtio_net_tx_waiting_pre_save(opaque);
2869 if (tmp->parent->curr_queues > tmp->parent->max_queues) {
2870 error_report("virtio-net: curr_queues %x > max_queues %x",
2871 tmp->parent->curr_queues, tmp->parent->max_queues);
2873 return -EINVAL;
2876 return 0; /* all good */
2879 static const VMStateDescription vmstate_virtio_net_tx_waiting = {
2880 .name = "virtio-net-tx_waiting",
2881 .pre_load = virtio_net_tx_waiting_pre_load,
2882 .pre_save = virtio_net_tx_waiting_pre_save,
2883 .fields = (VMStateField[]) {
2884 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
2885 curr_queues_1,
2886 vmstate_virtio_net_queue_tx_waiting,
2887 struct VirtIONetQueue),
2888 VMSTATE_END_OF_LIST()
2892 /* the 'has_ufo' flag is just tested; if the incoming stream has the
2893 * flag set we need to check that we have it
2895 static int virtio_net_ufo_post_load(void *opaque, int version_id)
2897 struct VirtIONetMigTmp *tmp = opaque;
2899 if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
2900 error_report("virtio-net: saved image requires TUN_F_UFO support");
2901 return -EINVAL;
2904 return 0;
2907 static int virtio_net_ufo_pre_save(void *opaque)
2909 struct VirtIONetMigTmp *tmp = opaque;
2911 tmp->has_ufo = tmp->parent->has_ufo;
2913 return 0;
2916 static const VMStateDescription vmstate_virtio_net_has_ufo = {
2917 .name = "virtio-net-ufo",
2918 .post_load = virtio_net_ufo_post_load,
2919 .pre_save = virtio_net_ufo_pre_save,
2920 .fields = (VMStateField[]) {
2921 VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
2922 VMSTATE_END_OF_LIST()
2926 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
2927 * flag set we need to check that we have it
2929 static int virtio_net_vnet_post_load(void *opaque, int version_id)
2931 struct VirtIONetMigTmp *tmp = opaque;
2933 if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
2934 error_report("virtio-net: saved image requires vnet_hdr=on");
2935 return -EINVAL;
2938 return 0;
2941 static int virtio_net_vnet_pre_save(void *opaque)
2943 struct VirtIONetMigTmp *tmp = opaque;
2945 tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
2947 return 0;
2950 static const VMStateDescription vmstate_virtio_net_has_vnet = {
2951 .name = "virtio-net-vnet",
2952 .post_load = virtio_net_vnet_post_load,
2953 .pre_save = virtio_net_vnet_pre_save,
2954 .fields = (VMStateField[]) {
2955 VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
2956 VMSTATE_END_OF_LIST()
2960 static bool virtio_net_rss_needed(void *opaque)
2962 return VIRTIO_NET(opaque)->rss_data.enabled;
2965 static const VMStateDescription vmstate_virtio_net_rss = {
2966 .name = "virtio-net-device/rss",
2967 .version_id = 1,
2968 .minimum_version_id = 1,
2969 .needed = virtio_net_rss_needed,
2970 .fields = (VMStateField[]) {
2971 VMSTATE_BOOL(rss_data.enabled, VirtIONet),
2972 VMSTATE_BOOL(rss_data.redirect, VirtIONet),
2973 VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
2974 VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
2975 VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
2976 VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
2977 VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
2978 VIRTIO_NET_RSS_MAX_KEY_SIZE),
2979 VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
2980 rss_data.indirections_len, 0,
2981 vmstate_info_uint16, uint16_t),
2982 VMSTATE_END_OF_LIST()
2986 static const VMStateDescription vmstate_virtio_net_device = {
2987 .name = "virtio-net-device",
2988 .version_id = VIRTIO_NET_VM_VERSION,
2989 .minimum_version_id = VIRTIO_NET_VM_VERSION,
2990 .post_load = virtio_net_post_load_device,
2991 .fields = (VMStateField[]) {
2992 VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
2993 VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
2994 vmstate_virtio_net_queue_tx_waiting,
2995 VirtIONetQueue),
2996 VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
2997 VMSTATE_UINT16(status, VirtIONet),
2998 VMSTATE_UINT8(promisc, VirtIONet),
2999 VMSTATE_UINT8(allmulti, VirtIONet),
3000 VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3002 /* Guarded pair: If it fits we load it, else we throw it away
3003 * - can happen if source has a larger MAC table.; post-load
3004 * sets flags in this case.
3006 VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3007 0, mac_table_fits, mac_table.in_use,
3008 ETH_ALEN),
3009 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3010 mac_table.in_use, ETH_ALEN),
3012 /* Note: This is an array of uint32's that's always been saved as a
3013 * buffer; hold onto your endiannesses; it's actually used as a bitmap
3014 * but based on the uint.
3016 VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3017 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3018 vmstate_virtio_net_has_vnet),
3019 VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3020 VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3021 VMSTATE_UINT8(alluni, VirtIONet),
3022 VMSTATE_UINT8(nomulti, VirtIONet),
3023 VMSTATE_UINT8(nouni, VirtIONet),
3024 VMSTATE_UINT8(nobcast, VirtIONet),
3025 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3026 vmstate_virtio_net_has_ufo),
3027 VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
3028 vmstate_info_uint16_equal, uint16_t),
3029 VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
3030 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3031 vmstate_virtio_net_tx_waiting),
3032 VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3033 has_ctrl_guest_offloads),
3034 VMSTATE_END_OF_LIST()
3036 .subsections = (const VMStateDescription * []) {
3037 &vmstate_virtio_net_rss,
3038 NULL
3042 static NetClientInfo net_virtio_info = {
3043 .type = NET_CLIENT_DRIVER_NIC,
3044 .size = sizeof(NICState),
3045 .can_receive = virtio_net_can_receive,
3046 .receive = virtio_net_receive,
3047 .link_status_changed = virtio_net_set_link_status,
3048 .query_rx_filter = virtio_net_query_rxfilter,
3049 .announce = virtio_net_announce,
3052 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
3054 VirtIONet *n = VIRTIO_NET(vdev);
3055 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3056 assert(n->vhost_started);
3057 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
3060 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
3061 bool mask)
3063 VirtIONet *n = VIRTIO_NET(vdev);
3064 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3065 assert(n->vhost_started);
3066 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
3067 vdev, idx, mask);
3070 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
3072 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3074 n->config_size = virtio_feature_get_config_size(feature_sizes,
3075 host_features);
3078 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
3079 const char *type)
3082 * The name can be NULL, the netclient name will be type.x.
3084 assert(type != NULL);
3086 g_free(n->netclient_name);
3087 g_free(n->netclient_type);
3088 n->netclient_name = g_strdup(name);
3089 n->netclient_type = g_strdup(type);
3092 static bool failover_unplug_primary(VirtIONet *n)
3094 HotplugHandler *hotplug_ctrl;
3095 PCIDevice *pci_dev;
3096 Error *err = NULL;
3098 hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev);
3099 if (hotplug_ctrl) {
3100 pci_dev = PCI_DEVICE(n->primary_dev);
3101 pci_dev->partially_hotplugged = true;
3102 hotplug_handler_unplug_request(hotplug_ctrl, n->primary_dev, &err);
3103 if (err) {
3104 error_report_err(err);
3105 return false;
3107 } else {
3108 return false;
3110 return true;
3113 static bool failover_replug_primary(VirtIONet *n, Error **errp)
3115 Error *err = NULL;
3116 HotplugHandler *hotplug_ctrl;
3117 PCIDevice *pdev = PCI_DEVICE(n->primary_dev);
3118 BusState *primary_bus;
3120 if (!pdev->partially_hotplugged) {
3121 return true;
3123 if (!n->primary_device_opts) {
3124 n->primary_device_opts = qemu_opts_from_qdict(qemu_find_opts("device"),
3125 n->primary_device_dict,
3126 errp);
3127 if (!n->primary_device_opts) {
3128 return false;
3131 primary_bus = n->primary_dev->parent_bus;
3132 if (!primary_bus) {
3133 error_setg(errp, "virtio_net: couldn't find primary bus");
3134 return false;
3136 qdev_set_parent_bus(n->primary_dev, primary_bus, &error_abort);
3137 qatomic_set(&n->primary_should_be_hidden, false);
3138 hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev);
3139 if (hotplug_ctrl) {
3140 hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, &err);
3141 if (err) {
3142 goto out;
3144 hotplug_handler_plug(hotplug_ctrl, n->primary_dev, &err);
3147 out:
3148 error_propagate(errp, err);
3149 return !err;
3152 static void virtio_net_handle_migration_primary(VirtIONet *n,
3153 MigrationState *s)
3155 bool should_be_hidden;
3156 Error *err = NULL;
3158 should_be_hidden = qatomic_read(&n->primary_should_be_hidden);
3160 if (!n->primary_dev) {
3161 n->primary_dev = virtio_connect_failover_devices(n, &err);
3162 if (!n->primary_dev) {
3163 return;
3167 if (migration_in_setup(s) && !should_be_hidden) {
3168 if (failover_unplug_primary(n)) {
3169 vmstate_unregister(VMSTATE_IF(n->primary_dev),
3170 qdev_get_vmsd(n->primary_dev),
3171 n->primary_dev);
3172 qapi_event_send_unplug_primary(n->primary_device_id);
3173 qatomic_set(&n->primary_should_be_hidden, true);
3174 } else {
3175 warn_report("couldn't unplug primary device");
3177 } else if (migration_has_failed(s)) {
3178 /* We already unplugged the device let's plug it back */
3179 if (!failover_replug_primary(n, &err)) {
3180 if (err) {
3181 error_report_err(err);
3187 static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
3189 MigrationState *s = data;
3190 VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
3191 virtio_net_handle_migration_primary(n, s);
3194 static int virtio_net_primary_should_be_hidden(DeviceListener *listener,
3195 QemuOpts *device_opts)
3197 VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
3198 bool match_found = false;
3199 bool hide = false;
3201 if (!device_opts) {
3202 return -1;
3204 n->primary_device_dict = qemu_opts_to_qdict(device_opts,
3205 n->primary_device_dict);
3206 if (n->primary_device_dict) {
3207 g_free(n->standby_id);
3208 n->standby_id = g_strdup(qdict_get_try_str(n->primary_device_dict,
3209 "failover_pair_id"));
3211 if (g_strcmp0(n->standby_id, n->netclient_name) == 0) {
3212 match_found = true;
3213 } else {
3214 match_found = false;
3215 hide = false;
3216 g_free(n->standby_id);
3217 n->primary_device_dict = NULL;
3218 goto out;
3221 n->primary_device_opts = device_opts;
3223 /* primary_should_be_hidden is set during feature negotiation */
3224 hide = qatomic_read(&n->primary_should_be_hidden);
3226 if (n->primary_device_dict) {
3227 g_free(n->primary_device_id);
3228 n->primary_device_id = g_strdup(qdict_get_try_str(
3229 n->primary_device_dict, "id"));
3230 if (!n->primary_device_id) {
3231 warn_report("primary_device_id not set");
3235 out:
3236 if (match_found && hide) {
3237 return 1;
3238 } else if (match_found && !hide) {
3239 return 0;
3240 } else {
3241 return -1;
3245 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
3247 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3248 VirtIONet *n = VIRTIO_NET(dev);
3249 NetClientState *nc;
3250 int i;
3252 if (n->net_conf.mtu) {
3253 n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3256 if (n->net_conf.duplex_str) {
3257 if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
3258 n->net_conf.duplex = DUPLEX_HALF;
3259 } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
3260 n->net_conf.duplex = DUPLEX_FULL;
3261 } else {
3262 error_setg(errp, "'duplex' must be 'half' or 'full'");
3263 return;
3265 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3266 } else {
3267 n->net_conf.duplex = DUPLEX_UNKNOWN;
3270 if (n->net_conf.speed < SPEED_UNKNOWN) {
3271 error_setg(errp, "'speed' must be between 0 and INT_MAX");
3272 return;
3274 if (n->net_conf.speed >= 0) {
3275 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3278 if (n->failover) {
3279 n->primary_listener.should_be_hidden =
3280 virtio_net_primary_should_be_hidden;
3281 qatomic_set(&n->primary_should_be_hidden, true);
3282 device_listener_register(&n->primary_listener);
3283 n->migration_state.notify = virtio_net_migration_state_notifier;
3284 add_migration_state_change_notifier(&n->migration_state);
3285 n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
3288 virtio_net_set_config_size(n, n->host_features);
3289 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
3292 * We set a lower limit on RX queue size to what it always was.
3293 * Guests that want a smaller ring can always resize it without
3294 * help from us (using virtio 1 and up).
3296 if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
3297 n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
3298 !is_power_of_2(n->net_conf.rx_queue_size)) {
3299 error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
3300 "must be a power of 2 between %d and %d.",
3301 n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
3302 VIRTQUEUE_MAX_SIZE);
3303 virtio_cleanup(vdev);
3304 return;
3307 if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
3308 n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
3309 !is_power_of_2(n->net_conf.tx_queue_size)) {
3310 error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
3311 "must be a power of 2 between %d and %d",
3312 n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
3313 VIRTQUEUE_MAX_SIZE);
3314 virtio_cleanup(vdev);
3315 return;
3318 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
3319 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
3320 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
3321 "must be a positive integer less than %d.",
3322 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
3323 virtio_cleanup(vdev);
3324 return;
3326 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
3327 n->curr_queues = 1;
3328 n->tx_timeout = n->net_conf.txtimer;
3330 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
3331 && strcmp(n->net_conf.tx, "bh")) {
3332 warn_report("virtio-net: "
3333 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3334 n->net_conf.tx);
3335 error_printf("Defaulting to \"bh\"");
3338 n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
3339 n->net_conf.tx_queue_size);
3341 for (i = 0; i < n->max_queues; i++) {
3342 virtio_net_add_queue(n, i);
3345 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3346 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
3347 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
3348 n->status = VIRTIO_NET_S_LINK_UP;
3349 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
3350 QEMU_CLOCK_VIRTUAL,
3351 virtio_net_announce_timer, n);
3352 n->announce_timer.round = 0;
3354 if (n->netclient_type) {
3356 * Happen when virtio_net_set_netclient_name has been called.
3358 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3359 n->netclient_type, n->netclient_name, n);
3360 } else {
3361 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3362 object_get_typename(OBJECT(dev)), dev->id, n);
3365 peer_test_vnet_hdr(n);
3366 if (peer_has_vnet_hdr(n)) {
3367 for (i = 0; i < n->max_queues; i++) {
3368 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
3370 n->host_hdr_len = sizeof(struct virtio_net_hdr);
3371 } else {
3372 n->host_hdr_len = 0;
3375 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
3377 n->vqs[0].tx_waiting = 0;
3378 n->tx_burst = n->net_conf.txburst;
3379 virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
3380 n->promisc = 1; /* for compatibility */
3382 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
3384 n->vlans = g_malloc0(MAX_VLAN >> 3);
3386 nc = qemu_get_queue(n->nic);
3387 nc->rxfilter_notify_enabled = 1;
3389 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3390 struct virtio_net_config netcfg = {};
3391 memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3392 vhost_net_set_config(get_vhost_net(nc->peer),
3393 (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER);
3395 QTAILQ_INIT(&n->rsc_chains);
3396 n->qdev = dev;
3398 net_rx_pkt_init(&n->rx_pkt, false);
3401 static void virtio_net_device_unrealize(DeviceState *dev)
3403 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3404 VirtIONet *n = VIRTIO_NET(dev);
3405 int i, max_queues;
3407 /* This will stop vhost backend if appropriate. */
3408 virtio_net_set_status(vdev, 0);
3410 g_free(n->netclient_name);
3411 n->netclient_name = NULL;
3412 g_free(n->netclient_type);
3413 n->netclient_type = NULL;
3415 g_free(n->mac_table.macs);
3416 g_free(n->vlans);
3418 if (n->failover) {
3419 device_listener_unregister(&n->primary_listener);
3420 g_free(n->primary_device_id);
3421 g_free(n->standby_id);
3422 qobject_unref(n->primary_device_dict);
3423 n->primary_device_dict = NULL;
3426 max_queues = n->multiqueue ? n->max_queues : 1;
3427 for (i = 0; i < max_queues; i++) {
3428 virtio_net_del_queue(n, i);
3430 /* delete also control vq */
3431 virtio_del_queue(vdev, max_queues * 2);
3432 qemu_announce_timer_del(&n->announce_timer, false);
3433 g_free(n->vqs);
3434 qemu_del_nic(n->nic);
3435 virtio_net_rsc_cleanup(n);
3436 g_free(n->rss_data.indirections_table);
3437 net_rx_pkt_uninit(n->rx_pkt);
3438 virtio_cleanup(vdev);
3441 static void virtio_net_instance_init(Object *obj)
3443 VirtIONet *n = VIRTIO_NET(obj);
3446 * The default config_size is sizeof(struct virtio_net_config).
3447 * Can be overriden with virtio_net_set_config_size.
3449 n->config_size = sizeof(struct virtio_net_config);
3450 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3451 "bootindex", "/ethernet-phy@0",
3452 DEVICE(n));
3455 static int virtio_net_pre_save(void *opaque)
3457 VirtIONet *n = opaque;
3459 /* At this point, backend must be stopped, otherwise
3460 * it might keep writing to memory. */
3461 assert(!n->vhost_started);
3463 return 0;
3466 static bool primary_unplug_pending(void *opaque)
3468 DeviceState *dev = opaque;
3469 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3470 VirtIONet *n = VIRTIO_NET(vdev);
3472 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3473 return false;
3475 return n->primary_dev ? n->primary_dev->pending_deleted_event : false;
3478 static bool dev_unplug_pending(void *opaque)
3480 DeviceState *dev = opaque;
3481 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
3483 return vdc->primary_unplug_pending(dev);
3486 static const VMStateDescription vmstate_virtio_net = {
3487 .name = "virtio-net",
3488 .minimum_version_id = VIRTIO_NET_VM_VERSION,
3489 .version_id = VIRTIO_NET_VM_VERSION,
3490 .fields = (VMStateField[]) {
3491 VMSTATE_VIRTIO_DEVICE,
3492 VMSTATE_END_OF_LIST()
3494 .pre_save = virtio_net_pre_save,
3495 .dev_unplug_pending = dev_unplug_pending,
3498 static Property virtio_net_properties[] = {
3499 DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3500 VIRTIO_NET_F_CSUM, true),
3501 DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
3502 VIRTIO_NET_F_GUEST_CSUM, true),
3503 DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3504 DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
3505 VIRTIO_NET_F_GUEST_TSO4, true),
3506 DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
3507 VIRTIO_NET_F_GUEST_TSO6, true),
3508 DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
3509 VIRTIO_NET_F_GUEST_ECN, true),
3510 DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
3511 VIRTIO_NET_F_GUEST_UFO, true),
3512 DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
3513 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3514 DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
3515 VIRTIO_NET_F_HOST_TSO4, true),
3516 DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
3517 VIRTIO_NET_F_HOST_TSO6, true),
3518 DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
3519 VIRTIO_NET_F_HOST_ECN, true),
3520 DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
3521 VIRTIO_NET_F_HOST_UFO, true),
3522 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
3523 VIRTIO_NET_F_MRG_RXBUF, true),
3524 DEFINE_PROP_BIT64("status", VirtIONet, host_features,
3525 VIRTIO_NET_F_STATUS, true),
3526 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
3527 VIRTIO_NET_F_CTRL_VQ, true),
3528 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
3529 VIRTIO_NET_F_CTRL_RX, true),
3530 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
3531 VIRTIO_NET_F_CTRL_VLAN, true),
3532 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
3533 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3534 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
3535 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3536 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
3537 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3538 DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
3539 DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
3540 VIRTIO_NET_F_RSS, false),
3541 DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3542 VIRTIO_NET_F_HASH_REPORT, false),
3543 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
3544 VIRTIO_NET_F_RSC_EXT, false),
3545 DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
3546 VIRTIO_NET_RSC_DEFAULT_INTERVAL),
3547 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
3548 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
3549 TX_TIMER_INTERVAL),
3550 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
3551 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
3552 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
3553 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
3554 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
3555 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3556 DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
3557 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
3558 true),
3559 DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
3560 DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
3561 DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
3562 DEFINE_PROP_END_OF_LIST(),
3565 static void virtio_net_class_init(ObjectClass *klass, void *data)
3567 DeviceClass *dc = DEVICE_CLASS(klass);
3568 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3570 device_class_set_props(dc, virtio_net_properties);
3571 dc->vmsd = &vmstate_virtio_net;
3572 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3573 vdc->realize = virtio_net_device_realize;
3574 vdc->unrealize = virtio_net_device_unrealize;
3575 vdc->get_config = virtio_net_get_config;
3576 vdc->set_config = virtio_net_set_config;
3577 vdc->get_features = virtio_net_get_features;
3578 vdc->set_features = virtio_net_set_features;
3579 vdc->bad_features = virtio_net_bad_features;
3580 vdc->reset = virtio_net_reset;
3581 vdc->set_status = virtio_net_set_status;
3582 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
3583 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
3584 vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
3585 vdc->post_load = virtio_net_post_load_virtio;
3586 vdc->vmsd = &vmstate_virtio_net_device;
3587 vdc->primary_unplug_pending = primary_unplug_pending;
3590 static const TypeInfo virtio_net_info = {
3591 .name = TYPE_VIRTIO_NET,
3592 .parent = TYPE_VIRTIO_DEVICE,
3593 .instance_size = sizeof(VirtIONet),
3594 .instance_init = virtio_net_instance_init,
3595 .class_init = virtio_net_class_init,
3598 static void virtio_register_types(void)
3600 type_register_static(&virtio_net_info);
3603 type_init(virtio_register_types)