16e790746SPaolo Bonzini /* 26e790746SPaolo Bonzini * Virtio Network Device 36e790746SPaolo Bonzini * 46e790746SPaolo Bonzini * Copyright IBM, Corp. 2007 56e790746SPaolo Bonzini * 66e790746SPaolo Bonzini * Authors: 76e790746SPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 86e790746SPaolo Bonzini * 96e790746SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 106e790746SPaolo Bonzini * the COPYING file in the top-level directory. 116e790746SPaolo Bonzini * 126e790746SPaolo Bonzini */ 136e790746SPaolo Bonzini 149b8bfe21SPeter Maydell #include "qemu/osdep.h" 159711cd0dSJens Freimann #include "qemu/atomic.h" 166e790746SPaolo Bonzini #include "qemu/iov.h" 17db725815SMarkus Armbruster #include "qemu/main-loop.h" 180b8fa32fSMarkus Armbruster #include "qemu/module.h" 196e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 206e790746SPaolo Bonzini #include "net/net.h" 216e790746SPaolo Bonzini #include "net/checksum.h" 226e790746SPaolo Bonzini #include "net/tap.h" 236e790746SPaolo Bonzini #include "qemu/error-report.h" 246e790746SPaolo Bonzini #include "qemu/timer.h" 259711cd0dSJens Freimann #include "qemu/option.h" 269711cd0dSJens Freimann #include "qemu/option_int.h" 279711cd0dSJens Freimann #include "qemu/config-file.h" 289711cd0dSJens Freimann #include "qapi/qmp/qdict.h" 296e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 306e790746SPaolo Bonzini #include "net/vhost_net.h" 319d8c6a25SDr. David Alan Gilbert #include "net/announce.h" 3217ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 33e688df6bSMarkus Armbruster #include "qapi/error.h" 349af23989SMarkus Armbruster #include "qapi/qapi-events-net.h" 35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 369711cd0dSJens Freimann #include "qapi/qapi-types-migration.h" 379711cd0dSJens Freimann #include "qapi/qapi-events-migration.h" 381399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 39f8d806c9SJuan Quintela #include "migration/misc.h" 409473939eSJason Baron #include "standard-headers/linux/ethtool.h" 412f780b6aSMarkus Armbruster #include "sysemu/sysemu.h" 429d8c6a25SDr. David Alan Gilbert #include "trace.h" 439711cd0dSJens Freimann #include "monitor/qdev.h" 449711cd0dSJens Freimann #include "hw/pci/pci.h" 454474e37aSYuri Benditovich #include "net_rx_pkt.h" 46108a6481SCindy Lu #include "hw/virtio/vhost.h" 476e790746SPaolo Bonzini 486e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 496e790746SPaolo Bonzini 506e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 516e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 526e790746SPaolo Bonzini 531c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 541c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 559b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 569b02e161SWei Wang 571c0fbfa3SMichael S. Tsirkin /* for now, only allow larger queues; with virtio-1, guest can downsize */ 581c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 599b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 601c0fbfa3SMichael S. Tsirkin 612974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ 622974e916SYuri Benditovich 632974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG 0x3F 642974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 652974e916SYuri Benditovich 662974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */ 672974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header)) 682974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535 692974e916SYuri Benditovich 702974e916SYuri Benditovich /* header length value in ip header without option */ 712974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5 722974e916SYuri Benditovich 732974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */ 742974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD 752974e916SYuri Benditovich 762974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance 772974e916SYuri Benditovich a lot, and should be tuned carefully, '300000'(300us) is the recommended 782974e916SYuri Benditovich value to pass the WHQL test, '50000' can gain 2x netperf throughput with 792974e916SYuri Benditovich tso/gso/gro 'off'. */ 802974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000 812974e916SYuri Benditovich 8259079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ 8359079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ 8459079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ 8559079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ 8659079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ 8759079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \ 8859079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \ 8959079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \ 9059079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) 9159079029SYuri Benditovich 926e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = { 93127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 945d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mac)}, 95127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 965d5b33c0SMax Reitz .end = endof(struct virtio_net_config, status)}, 97127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 985d5b33c0SMax Reitz .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 99127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 1005d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mtu)}, 1019473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 1025d5b33c0SMax Reitz .end = endof(struct virtio_net_config, duplex)}, 103e22f0603SYuri Benditovich {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT), 10459079029SYuri Benditovich .end = endof(struct virtio_net_config, supported_hash_types)}, 1056e790746SPaolo Bonzini {} 1066e790746SPaolo Bonzini }; 1076e790746SPaolo Bonzini 1086e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 1096e790746SPaolo Bonzini { 1106e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1116e790746SPaolo Bonzini 1126e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 1136e790746SPaolo Bonzini } 1146e790746SPaolo Bonzini 1156e790746SPaolo Bonzini static int vq2q(int queue_index) 1166e790746SPaolo Bonzini { 1176e790746SPaolo Bonzini return queue_index / 2; 1186e790746SPaolo Bonzini } 1196e790746SPaolo Bonzini 1206e790746SPaolo Bonzini /* TODO 1216e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 1226e790746SPaolo Bonzini */ 1236e790746SPaolo Bonzini 1246e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 1256e790746SPaolo Bonzini { 12617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1276e790746SPaolo Bonzini struct virtio_net_config netcfg; 128c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1296e790746SPaolo Bonzini 130108a6481SCindy Lu int ret = 0; 131108a6481SCindy Lu memset(&netcfg, 0 , sizeof(struct virtio_net_config)); 1321399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 1331399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues); 134a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 1356e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 1369473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 1379473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 13859079029SYuri Benditovich netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE; 13959079029SYuri Benditovich virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length, 140e22f0603SYuri Benditovich virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ? 141e22f0603SYuri Benditovich VIRTIO_NET_RSS_MAX_TABLE_LEN : 1); 14259079029SYuri Benditovich virtio_stl_p(vdev, &netcfg.supported_hash_types, 14359079029SYuri Benditovich VIRTIO_NET_RSS_SUPPORTED_HASHES); 1446e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 145108a6481SCindy Lu 146c546ecf2SJason Wang /* 147c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 148c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 149c546ecf2SJason Wang */ 150c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 151108a6481SCindy Lu ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, 152108a6481SCindy Lu n->config_size); 153108a6481SCindy Lu if (ret != -1) { 154108a6481SCindy Lu memcpy(config, &netcfg, n->config_size); 155108a6481SCindy Lu } 156108a6481SCindy Lu } 1576e790746SPaolo Bonzini } 1586e790746SPaolo Bonzini 1596e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1606e790746SPaolo Bonzini { 16117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1626e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 163c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1646e790746SPaolo Bonzini 1656e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1666e790746SPaolo Bonzini 16795129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 16895129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1696e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1706e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 1716e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1726e790746SPaolo Bonzini } 173108a6481SCindy Lu 174c546ecf2SJason Wang /* 175c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 176c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 177c546ecf2SJason Wang */ 178c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 179c546ecf2SJason Wang vhost_net_set_config(get_vhost_net(nc->peer), 180c546ecf2SJason Wang (uint8_t *)&netcfg, 0, n->config_size, 181108a6481SCindy Lu VHOST_SET_CONFIG_TYPE_MASTER); 182108a6481SCindy Lu } 1836e790746SPaolo Bonzini } 1846e790746SPaolo Bonzini 1856e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 1866e790746SPaolo Bonzini { 18717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1886e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 18917a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 1906e790746SPaolo Bonzini } 1916e790746SPaolo Bonzini 192b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net) 193b2c929f0SDr. David Alan Gilbert { 194b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(net); 195b2c929f0SDr. David Alan Gilbert trace_virtio_net_announce_notify(); 196b2c929f0SDr. David Alan Gilbert 197b2c929f0SDr. David Alan Gilbert net->status |= VIRTIO_NET_S_ANNOUNCE; 198b2c929f0SDr. David Alan Gilbert virtio_notify_config(vdev); 199b2c929f0SDr. David Alan Gilbert } 200b2c929f0SDr. David Alan Gilbert 201f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 202f57fcf70SJason Wang { 203f57fcf70SJason Wang VirtIONet *n = opaque; 2049d8c6a25SDr. David Alan Gilbert trace_virtio_net_announce_timer(n->announce_timer.round); 205f57fcf70SJason Wang 2069d8c6a25SDr. David Alan Gilbert n->announce_timer.round--; 207b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 208b2c929f0SDr. David Alan Gilbert } 209b2c929f0SDr. David Alan Gilbert 210b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc) 211b2c929f0SDr. David Alan Gilbert { 212b2c929f0SDr. David Alan Gilbert VirtIONet *n = qemu_get_nic_opaque(nc); 213b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 214b2c929f0SDr. David Alan Gilbert 215b2c929f0SDr. David Alan Gilbert /* 216b2c929f0SDr. David Alan Gilbert * Make sure the virtio migration announcement timer isn't running 217b2c929f0SDr. David Alan Gilbert * If it is, let it trigger announcement so that we do not cause 218b2c929f0SDr. David Alan Gilbert * confusion. 219b2c929f0SDr. David Alan Gilbert */ 220b2c929f0SDr. David Alan Gilbert if (n->announce_timer.round) { 221b2c929f0SDr. David Alan Gilbert return; 222b2c929f0SDr. David Alan Gilbert } 223b2c929f0SDr. David Alan Gilbert 224b2c929f0SDr. David Alan Gilbert if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 225b2c929f0SDr. David Alan Gilbert virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 226b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 227b2c929f0SDr. David Alan Gilbert } 228f57fcf70SJason Wang } 229f57fcf70SJason Wang 2306e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 2316e790746SPaolo Bonzini { 23217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2336e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 2346e790746SPaolo Bonzini int queues = n->multiqueue ? n->max_queues : 1; 2356e790746SPaolo Bonzini 236ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 2376e790746SPaolo Bonzini return; 2386e790746SPaolo Bonzini } 2396e790746SPaolo Bonzini 2408c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 2418c1ac475SRadim Krčmář !!n->vhost_started) { 2426e790746SPaolo Bonzini return; 2436e790746SPaolo Bonzini } 2446e790746SPaolo Bonzini if (!n->vhost_started) { 245086abc1cSMichael S. Tsirkin int r, i; 246086abc1cSMichael S. Tsirkin 2471bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2481bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 2491bfa316cSGreg Kurz "falling back on userspace virtio", 2501bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 2511bfa316cSGreg Kurz return; 2521bfa316cSGreg Kurz } 2531bfa316cSGreg Kurz 254086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 255086abc1cSMichael S. Tsirkin * when vhost is running. 256086abc1cSMichael S. Tsirkin */ 257086abc1cSMichael S. Tsirkin for (i = 0; i < queues; i++) { 258086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 259086abc1cSMichael S. Tsirkin 260086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 261086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 262086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 263086abc1cSMichael S. Tsirkin } 264086abc1cSMichael S. Tsirkin 265a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 266a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 267a93e599dSMaxime Coquelin if (r < 0) { 268a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 269a93e599dSMaxime Coquelin n->net_conf.mtu); 270a93e599dSMaxime Coquelin 271a93e599dSMaxime Coquelin return; 272a93e599dSMaxime Coquelin } 273a93e599dSMaxime Coquelin } 274a93e599dSMaxime Coquelin 2756e790746SPaolo Bonzini n->vhost_started = 1; 27617a0ca55SKONRAD Frederic r = vhost_net_start(vdev, n->nic->ncs, queues); 2776e790746SPaolo Bonzini if (r < 0) { 2786e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 2796e790746SPaolo Bonzini "falling back on userspace virtio", -r); 2806e790746SPaolo Bonzini n->vhost_started = 0; 2816e790746SPaolo Bonzini } 2826e790746SPaolo Bonzini } else { 28317a0ca55SKONRAD Frederic vhost_net_stop(vdev, n->nic->ncs, queues); 2846e790746SPaolo Bonzini n->vhost_started = 0; 2856e790746SPaolo Bonzini } 2866e790746SPaolo Bonzini } 2876e790746SPaolo Bonzini 2881bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 2891bfa316cSGreg Kurz NetClientState *peer, 2901bfa316cSGreg Kurz bool enable) 2911bfa316cSGreg Kurz { 2921bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 2931bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 2941bfa316cSGreg Kurz } else { 2951bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 2961bfa316cSGreg Kurz } 2971bfa316cSGreg Kurz } 2981bfa316cSGreg Kurz 2991bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 3001bfa316cSGreg Kurz int queues, bool enable) 3011bfa316cSGreg Kurz { 3021bfa316cSGreg Kurz int i; 3031bfa316cSGreg Kurz 3041bfa316cSGreg Kurz for (i = 0; i < queues; i++) { 3051bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 3061bfa316cSGreg Kurz enable) { 3071bfa316cSGreg Kurz while (--i >= 0) { 3081bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 3091bfa316cSGreg Kurz } 3101bfa316cSGreg Kurz 3111bfa316cSGreg Kurz return true; 3121bfa316cSGreg Kurz } 3131bfa316cSGreg Kurz } 3141bfa316cSGreg Kurz 3151bfa316cSGreg Kurz return false; 3161bfa316cSGreg Kurz } 3171bfa316cSGreg Kurz 3181bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 3191bfa316cSGreg Kurz { 3201bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 3211bfa316cSGreg Kurz int queues = n->multiqueue ? n->max_queues : 1; 3221bfa316cSGreg Kurz 3231bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 3241bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 3251bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 3261bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 3271bfa316cSGreg Kurz * virtio-net code. 3281bfa316cSGreg Kurz */ 3291bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 3301bfa316cSGreg Kurz queues, true); 3311bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3321bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3331bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3341bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3351bfa316cSGreg Kurz * endianness. 3361bfa316cSGreg Kurz */ 3371bfa316cSGreg Kurz virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false); 3381bfa316cSGreg Kurz } 3391bfa316cSGreg Kurz } 3401bfa316cSGreg Kurz 341283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 342283e2c2aSYuri Benditovich { 343283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 344283e2c2aSYuri Benditovich if (dropped) { 345283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 346283e2c2aSYuri Benditovich } 347283e2c2aSYuri Benditovich } 348283e2c2aSYuri Benditovich 3496e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3506e790746SPaolo Bonzini { 35117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3526e790746SPaolo Bonzini VirtIONetQueue *q; 3536e790746SPaolo Bonzini int i; 3546e790746SPaolo Bonzini uint8_t queue_status; 3556e790746SPaolo Bonzini 3561bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3576e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3586e790746SPaolo Bonzini 3596e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 36038705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 36138705bb5SFam Zheng bool queue_started; 3626e790746SPaolo Bonzini q = &n->vqs[i]; 3636e790746SPaolo Bonzini 3646e790746SPaolo Bonzini if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { 3656e790746SPaolo Bonzini queue_status = 0; 3666e790746SPaolo Bonzini } else { 3676e790746SPaolo Bonzini queue_status = status; 3686e790746SPaolo Bonzini } 36938705bb5SFam Zheng queue_started = 37038705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 37138705bb5SFam Zheng 37238705bb5SFam Zheng if (queue_started) { 37338705bb5SFam Zheng qemu_flush_queued_packets(ncs); 37438705bb5SFam Zheng } 3756e790746SPaolo Bonzini 3766e790746SPaolo Bonzini if (!q->tx_waiting) { 3776e790746SPaolo Bonzini continue; 3786e790746SPaolo Bonzini } 3796e790746SPaolo Bonzini 38038705bb5SFam Zheng if (queue_started) { 3816e790746SPaolo Bonzini if (q->tx_timer) { 382bc72ad67SAlex Bligh timer_mod(q->tx_timer, 383bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 3846e790746SPaolo Bonzini } else { 3856e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 3866e790746SPaolo Bonzini } 3876e790746SPaolo Bonzini } else { 3886e790746SPaolo Bonzini if (q->tx_timer) { 389bc72ad67SAlex Bligh timer_del(q->tx_timer); 3906e790746SPaolo Bonzini } else { 3916e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 3926e790746SPaolo Bonzini } 393283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 39470e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 39570e53e6eSJason Wang vdev->vm_running) { 396283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 397283e2c2aSYuri Benditovich * and disabled notification */ 398283e2c2aSYuri Benditovich q->tx_waiting = 0; 399283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 400283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 401283e2c2aSYuri Benditovich } 4026e790746SPaolo Bonzini } 4036e790746SPaolo Bonzini } 4046e790746SPaolo Bonzini } 4056e790746SPaolo Bonzini 4066e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4076e790746SPaolo Bonzini { 4086e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 40917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4106e790746SPaolo Bonzini uint16_t old_status = n->status; 4116e790746SPaolo Bonzini 4126e790746SPaolo Bonzini if (nc->link_down) 4136e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4146e790746SPaolo Bonzini else 4156e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4166e790746SPaolo Bonzini 4176e790746SPaolo Bonzini if (n->status != old_status) 41817a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4196e790746SPaolo Bonzini 42017a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4216e790746SPaolo Bonzini } 4226e790746SPaolo Bonzini 423b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 424b1be4280SAmos Kong { 425b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 426b1be4280SAmos Kong 427b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 428ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 42906150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 4303ab72385SPeter Xu n->netclient_name, path); 43196e35046SAmos Kong g_free(path); 432b1be4280SAmos Kong 433b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 434b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 435b1be4280SAmos Kong } 436b1be4280SAmos Kong } 437b1be4280SAmos Kong 438f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 439f7bc8ef8SAmos Kong { 440f7bc8ef8SAmos Kong intList *list, *entry; 441f7bc8ef8SAmos Kong int i, j; 442f7bc8ef8SAmos Kong 443f7bc8ef8SAmos Kong list = NULL; 444f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 445f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 446f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 447f7bc8ef8SAmos Kong entry = g_malloc0(sizeof(*entry)); 448f7bc8ef8SAmos Kong entry->value = (i << 5) + j; 449f7bc8ef8SAmos Kong entry->next = list; 450f7bc8ef8SAmos Kong list = entry; 451f7bc8ef8SAmos Kong } 452f7bc8ef8SAmos Kong } 453f7bc8ef8SAmos Kong } 454f7bc8ef8SAmos Kong 455f7bc8ef8SAmos Kong return list; 456f7bc8ef8SAmos Kong } 457f7bc8ef8SAmos Kong 458b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 459b1be4280SAmos Kong { 460b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 461f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 462b1be4280SAmos Kong RxFilterInfo *info; 463b1be4280SAmos Kong strList *str_list, *entry; 464f7bc8ef8SAmos Kong int i; 465b1be4280SAmos Kong 466b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 467b1be4280SAmos Kong info->name = g_strdup(nc->name); 468b1be4280SAmos Kong info->promiscuous = n->promisc; 469b1be4280SAmos Kong 470b1be4280SAmos Kong if (n->nouni) { 471b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 472b1be4280SAmos Kong } else if (n->alluni) { 473b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 474b1be4280SAmos Kong } else { 475b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 476b1be4280SAmos Kong } 477b1be4280SAmos Kong 478b1be4280SAmos Kong if (n->nomulti) { 479b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 480b1be4280SAmos Kong } else if (n->allmulti) { 481b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 482b1be4280SAmos Kong } else { 483b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 484b1be4280SAmos Kong } 485b1be4280SAmos Kong 486b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 487b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 488b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 489b1be4280SAmos Kong 490b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 491b1be4280SAmos Kong 492b1be4280SAmos Kong str_list = NULL; 493b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 494b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 495b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 496b1be4280SAmos Kong entry->next = str_list; 497b1be4280SAmos Kong str_list = entry; 498b1be4280SAmos Kong } 499b1be4280SAmos Kong info->unicast_table = str_list; 500b1be4280SAmos Kong 501b1be4280SAmos Kong str_list = NULL; 502b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 503b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 504b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 505b1be4280SAmos Kong entry->next = str_list; 506b1be4280SAmos Kong str_list = entry; 507b1be4280SAmos Kong } 508b1be4280SAmos Kong info->multicast_table = str_list; 509f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 510b1be4280SAmos Kong 51195129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 512f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 513f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 514f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 515f7bc8ef8SAmos Kong } else { 516f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 517b1be4280SAmos Kong } 518b1be4280SAmos Kong 519b1be4280SAmos Kong /* enable event notification after query */ 520b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 521b1be4280SAmos Kong 522b1be4280SAmos Kong return info; 523b1be4280SAmos Kong } 524b1be4280SAmos Kong 5256e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 5266e790746SPaolo Bonzini { 52717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 52894b52958SGreg Kurz int i; 5296e790746SPaolo Bonzini 5306e790746SPaolo Bonzini /* Reset back to compatibility mode */ 5316e790746SPaolo Bonzini n->promisc = 1; 5326e790746SPaolo Bonzini n->allmulti = 0; 5336e790746SPaolo Bonzini n->alluni = 0; 5346e790746SPaolo Bonzini n->nomulti = 0; 5356e790746SPaolo Bonzini n->nouni = 0; 5366e790746SPaolo Bonzini n->nobcast = 0; 5376e790746SPaolo Bonzini /* multiqueue is disabled by default */ 5386e790746SPaolo Bonzini n->curr_queues = 1; 5399d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 5409d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 541f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 5426e790746SPaolo Bonzini 5436e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 5446e790746SPaolo Bonzini n->mac_table.in_use = 0; 5456e790746SPaolo Bonzini n->mac_table.first_multi = 0; 5466e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 5476e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 5486e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 5496e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 550702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 5516e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 55294b52958SGreg Kurz 55394b52958SGreg Kurz /* Flush any async TX */ 55494b52958SGreg Kurz for (i = 0; i < n->max_queues; i++) { 55594b52958SGreg Kurz NetClientState *nc = qemu_get_subqueue(n->nic, i); 55694b52958SGreg Kurz 55794b52958SGreg Kurz if (nc->peer) { 55894b52958SGreg Kurz qemu_flush_or_purge_queued_packets(nc->peer, true); 55994b52958SGreg Kurz assert(!virtio_net_get_subqueue(nc)->async_tx.elem); 56094b52958SGreg Kurz } 56194b52958SGreg Kurz } 5626e790746SPaolo Bonzini } 5636e790746SPaolo Bonzini 5646e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 5656e790746SPaolo Bonzini { 5666e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5676e790746SPaolo Bonzini if (!nc->peer) { 5686e790746SPaolo Bonzini return; 5696e790746SPaolo Bonzini } 5706e790746SPaolo Bonzini 571d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 5726e790746SPaolo Bonzini } 5736e790746SPaolo Bonzini 5746e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 5756e790746SPaolo Bonzini { 5766e790746SPaolo Bonzini return n->has_vnet_hdr; 5776e790746SPaolo Bonzini } 5786e790746SPaolo Bonzini 5796e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 5806e790746SPaolo Bonzini { 5816e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 5826e790746SPaolo Bonzini return 0; 5836e790746SPaolo Bonzini 584d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 5856e790746SPaolo Bonzini 5866e790746SPaolo Bonzini return n->has_ufo; 5876e790746SPaolo Bonzini } 5886e790746SPaolo Bonzini 589bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 590e22f0603SYuri Benditovich int version_1, int hash_report) 5916e790746SPaolo Bonzini { 5926e790746SPaolo Bonzini int i; 5936e790746SPaolo Bonzini NetClientState *nc; 5946e790746SPaolo Bonzini 5956e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 5966e790746SPaolo Bonzini 597bb9d17f8SCornelia Huck if (version_1) { 598e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 599e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 600e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 601e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 602bb9d17f8SCornelia Huck } else { 6036e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 604bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 605bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 606bb9d17f8SCornelia Huck } 6076e790746SPaolo Bonzini 6086e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 6096e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6106e790746SPaolo Bonzini 6116e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 612d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 613d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6146e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6156e790746SPaolo Bonzini } 6166e790746SPaolo Bonzini } 6176e790746SPaolo Bonzini } 6186e790746SPaolo Bonzini 6192eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6202eef278bSMichael S. Tsirkin { 6212eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6222eef278bSMichael S. Tsirkin 6232eef278bSMichael S. Tsirkin /* 6242eef278bSMichael S. Tsirkin * Backends other than vhost-user don't support max queue size. 6252eef278bSMichael S. Tsirkin */ 6262eef278bSMichael S. Tsirkin if (!peer) { 6272eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6282eef278bSMichael S. Tsirkin } 6292eef278bSMichael S. Tsirkin 6302eef278bSMichael S. Tsirkin if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) { 6312eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6322eef278bSMichael S. Tsirkin } 6332eef278bSMichael S. Tsirkin 6342eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 6352eef278bSMichael S. Tsirkin } 6362eef278bSMichael S. Tsirkin 6376e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 6386e790746SPaolo Bonzini { 6396e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6406e790746SPaolo Bonzini 6416e790746SPaolo Bonzini if (!nc->peer) { 6426e790746SPaolo Bonzini return 0; 6436e790746SPaolo Bonzini } 6446e790746SPaolo Bonzini 645f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6467263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 6477263a0adSChangchun Ouyang } 6487263a0adSChangchun Ouyang 649f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6506e790746SPaolo Bonzini return 0; 6516e790746SPaolo Bonzini } 6526e790746SPaolo Bonzini 6531074b879SJason Wang if (n->max_queues == 1) { 6541074b879SJason Wang return 0; 6551074b879SJason Wang } 6561074b879SJason Wang 6576e790746SPaolo Bonzini return tap_enable(nc->peer); 6586e790746SPaolo Bonzini } 6596e790746SPaolo Bonzini 6606e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 6616e790746SPaolo Bonzini { 6626e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6636e790746SPaolo Bonzini 6646e790746SPaolo Bonzini if (!nc->peer) { 6656e790746SPaolo Bonzini return 0; 6666e790746SPaolo Bonzini } 6676e790746SPaolo Bonzini 668f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6697263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 6707263a0adSChangchun Ouyang } 6717263a0adSChangchun Ouyang 672f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6736e790746SPaolo Bonzini return 0; 6746e790746SPaolo Bonzini } 6756e790746SPaolo Bonzini 6766e790746SPaolo Bonzini return tap_disable(nc->peer); 6776e790746SPaolo Bonzini } 6786e790746SPaolo Bonzini 6796e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n) 6806e790746SPaolo Bonzini { 6816e790746SPaolo Bonzini int i; 682ddfa83eaSJoel Stanley int r; 6836e790746SPaolo Bonzini 68468b5f314SYuri Benditovich if (n->nic->peer_deleted) { 68568b5f314SYuri Benditovich return; 68668b5f314SYuri Benditovich } 68768b5f314SYuri Benditovich 6886e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 6896e790746SPaolo Bonzini if (i < n->curr_queues) { 690ddfa83eaSJoel Stanley r = peer_attach(n, i); 691ddfa83eaSJoel Stanley assert(!r); 6926e790746SPaolo Bonzini } else { 693ddfa83eaSJoel Stanley r = peer_detach(n, i); 694ddfa83eaSJoel Stanley assert(!r); 6956e790746SPaolo Bonzini } 6966e790746SPaolo Bonzini } 6976e790746SPaolo Bonzini } 6986e790746SPaolo Bonzini 699ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7006e790746SPaolo Bonzini 7019d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7029d5b731dSJason Wang Error **errp) 7036e790746SPaolo Bonzini { 70417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7056e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7066e790746SPaolo Bonzini 707da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 708da3e8a23SShannon Zhao features |= n->host_features; 709da3e8a23SShannon Zhao 7100cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7116e790746SPaolo Bonzini 7126e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7130cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7140cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7150cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7160cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7176e790746SPaolo Bonzini 7180cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7190cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7200cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7210cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 722e22f0603SYuri Benditovich 723e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7246e790746SPaolo Bonzini } 7256e790746SPaolo Bonzini 7266e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7270cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 7280cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 7296e790746SPaolo Bonzini } 7306e790746SPaolo Bonzini 731ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 7326e790746SPaolo Bonzini return features; 7336e790746SPaolo Bonzini } 7342974e916SYuri Benditovich 73559079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 736e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 73775ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 73875ebec11SMaxime Coquelin vdev->backend_features = features; 73975ebec11SMaxime Coquelin 74075ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 74175ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 74275ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 74375ebec11SMaxime Coquelin } 74475ebec11SMaxime Coquelin 74575ebec11SMaxime Coquelin return features; 7466e790746SPaolo Bonzini } 7476e790746SPaolo Bonzini 748019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 7496e790746SPaolo Bonzini { 750019a3edbSGerd Hoffmann uint64_t features = 0; 7516e790746SPaolo Bonzini 7526e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 7536e790746SPaolo Bonzini * but also these: */ 7540cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7550cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 7560cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7570cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7580cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 7596e790746SPaolo Bonzini 7606e790746SPaolo Bonzini return features; 7616e790746SPaolo Bonzini } 7626e790746SPaolo Bonzini 763644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 764644c9858SDmitry Fleytman { 765ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 766644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 767644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 768644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 769644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 770644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 771644c9858SDmitry Fleytman } 772644c9858SDmitry Fleytman 773644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 774644c9858SDmitry Fleytman { 775644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 776644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 777644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 778644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 779644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 780644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 781644c9858SDmitry Fleytman 782644c9858SDmitry Fleytman return guest_offloads_mask & features; 783644c9858SDmitry Fleytman } 784644c9858SDmitry Fleytman 785644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 786644c9858SDmitry Fleytman { 787644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 788644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 789644c9858SDmitry Fleytman } 790644c9858SDmitry Fleytman 7919711cd0dSJens Freimann static void failover_add_primary(VirtIONet *n, Error **errp) 7929711cd0dSJens Freimann { 7939711cd0dSJens Freimann Error *err = NULL; 79419e49bc2SJuan Quintela QemuOpts *opts; 7959711cd0dSJens Freimann 796117378bfSJens Freimann if (n->primary_dev) { 797117378bfSJens Freimann return; 798117378bfSJens Freimann } 799117378bfSJens Freimann 80019e49bc2SJuan Quintela opts = qemu_opts_find(qemu_find_opts("device"), n->primary_device_id); 80119e49bc2SJuan Quintela if (opts) { 80219e49bc2SJuan Quintela n->primary_dev = qdev_device_add(opts, &err); 8039711cd0dSJens Freimann if (err) { 80419e49bc2SJuan Quintela qemu_opts_del(opts); 8059711cd0dSJens Freimann } 8069711cd0dSJens Freimann } else { 8079711cd0dSJens Freimann error_setg(errp, "Primary device not found"); 8089711cd0dSJens Freimann error_append_hint(errp, "Virtio-net failover will not work. Make " 8099711cd0dSJens Freimann "sure primary device has parameter" 8109711cd0dSJens Freimann " failover_pair_id=<virtio-net-id>\n"); 8119711cd0dSJens Freimann } 8129711cd0dSJens Freimann error_propagate(errp, err); 8139711cd0dSJens Freimann } 8149711cd0dSJens Freimann 8159711cd0dSJens Freimann static int is_my_primary(void *opaque, QemuOpts *opts, Error **errp) 8169711cd0dSJens Freimann { 8179711cd0dSJens Freimann VirtIONet *n = opaque; 8189711cd0dSJens Freimann int ret = 0; 8199711cd0dSJens Freimann const char *standby_id = qemu_opt_get(opts, "failover_pair_id"); 8209711cd0dSJens Freimann 821518eda9fSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) == 0) { 8229711cd0dSJens Freimann n->primary_device_id = g_strdup(opts->id); 8239711cd0dSJens Freimann ret = 1; 8249711cd0dSJens Freimann } 8259711cd0dSJens Freimann 8269711cd0dSJens Freimann return ret; 8279711cd0dSJens Freimann } 8289711cd0dSJens Freimann 8299711cd0dSJens Freimann static DeviceState *virtio_net_find_primary(VirtIONet *n, Error **errp) 8309711cd0dSJens Freimann { 8319711cd0dSJens Freimann DeviceState *dev = NULL; 8329711cd0dSJens Freimann Error *err = NULL; 8339711cd0dSJens Freimann 8349711cd0dSJens Freimann if (qemu_opts_foreach(qemu_find_opts("device"), 8359711cd0dSJens Freimann is_my_primary, n, &err)) { 8369711cd0dSJens Freimann if (err) { 8379711cd0dSJens Freimann error_propagate(errp, err); 8389711cd0dSJens Freimann return NULL; 8399711cd0dSJens Freimann } 8409711cd0dSJens Freimann if (n->primary_device_id) { 8419711cd0dSJens Freimann dev = qdev_find_recursive(sysbus_get_default(), 8429711cd0dSJens Freimann n->primary_device_id); 8439711cd0dSJens Freimann } else { 8449711cd0dSJens Freimann error_setg(errp, "Primary device id not found"); 8459711cd0dSJens Freimann return NULL; 8469711cd0dSJens Freimann } 8479711cd0dSJens Freimann } 8489711cd0dSJens Freimann return dev; 8499711cd0dSJens Freimann } 8509711cd0dSJens Freimann 85182ceb657SJuan Quintela static DeviceState *virtio_connect_failover_devices(VirtIONet *n, Error **errp) 8529711cd0dSJens Freimann { 8539711cd0dSJens Freimann DeviceState *prim_dev = NULL; 8549711cd0dSJens Freimann Error *err = NULL; 8559711cd0dSJens Freimann 8569711cd0dSJens Freimann prim_dev = virtio_net_find_primary(n, &err); 8579711cd0dSJens Freimann if (prim_dev) { 8589711cd0dSJens Freimann n->primary_device_id = g_strdup(prim_dev->id); 8599711cd0dSJens Freimann } else { 8609711cd0dSJens Freimann error_propagate(errp, err); 8619711cd0dSJens Freimann } 8629711cd0dSJens Freimann 8639711cd0dSJens Freimann return prim_dev; 8649711cd0dSJens Freimann } 8659711cd0dSJens Freimann 866d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 8676e790746SPaolo Bonzini { 86817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 8699711cd0dSJens Freimann Error *err = NULL; 8706e790746SPaolo Bonzini int i; 8716e790746SPaolo Bonzini 87275ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 87375ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 87475ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 87575ebec11SMaxime Coquelin } 87675ebec11SMaxime Coquelin 877ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 87859079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 87995129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 8806e790746SPaolo Bonzini 881ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 88295129d6fSCornelia Huck virtio_has_feature(features, 883bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 88495129d6fSCornelia Huck virtio_has_feature(features, 885e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 886e22f0603SYuri Benditovich virtio_has_feature(features, 887e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 8886e790746SPaolo Bonzini 8892974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 8902974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 8912974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 8922974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 893e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 8942974e916SYuri Benditovich 8956e790746SPaolo Bonzini if (n->has_vnet_hdr) { 896644c9858SDmitry Fleytman n->curr_guest_offloads = 897644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 898644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 8996e790746SPaolo Bonzini } 9006e790746SPaolo Bonzini 9016e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 9026e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9036e790746SPaolo Bonzini 904ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9056e790746SPaolo Bonzini continue; 9066e790746SPaolo Bonzini } 907ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 9086e790746SPaolo Bonzini } 9090b1eaa88SStefan Fritsch 91095129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 9110b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 9120b1eaa88SStefan Fritsch } else { 9130b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 9140b1eaa88SStefan Fritsch } 9159711cd0dSJens Freimann 9169711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 9179711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 918e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 9199711cd0dSJens Freimann failover_add_primary(n, &err); 9209711cd0dSJens Freimann if (err) { 92182ceb657SJuan Quintela n->primary_dev = virtio_connect_failover_devices(n, &err); 9229711cd0dSJens Freimann if (err) { 9239711cd0dSJens Freimann goto out_err; 9249711cd0dSJens Freimann } 9259711cd0dSJens Freimann failover_add_primary(n, &err); 9269711cd0dSJens Freimann if (err) { 9279711cd0dSJens Freimann goto out_err; 9289711cd0dSJens Freimann } 9299711cd0dSJens Freimann } 9309711cd0dSJens Freimann } 9319711cd0dSJens Freimann return; 9329711cd0dSJens Freimann 9339711cd0dSJens Freimann out_err: 9349711cd0dSJens Freimann if (err) { 9359711cd0dSJens Freimann warn_report_err(err); 9369711cd0dSJens Freimann } 9376e790746SPaolo Bonzini } 9386e790746SPaolo Bonzini 9396e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 9406e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 9416e790746SPaolo Bonzini { 9426e790746SPaolo Bonzini uint8_t on; 9436e790746SPaolo Bonzini size_t s; 944b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 9456e790746SPaolo Bonzini 9466e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 9476e790746SPaolo Bonzini if (s != sizeof(on)) { 9486e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9496e790746SPaolo Bonzini } 9506e790746SPaolo Bonzini 9516e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 9526e790746SPaolo Bonzini n->promisc = on; 9536e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 9546e790746SPaolo Bonzini n->allmulti = on; 9556e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 9566e790746SPaolo Bonzini n->alluni = on; 9576e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 9586e790746SPaolo Bonzini n->nomulti = on; 9596e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 9606e790746SPaolo Bonzini n->nouni = on; 9616e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 9626e790746SPaolo Bonzini n->nobcast = on; 9636e790746SPaolo Bonzini } else { 9646e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9656e790746SPaolo Bonzini } 9666e790746SPaolo Bonzini 967b1be4280SAmos Kong rxfilter_notify(nc); 968b1be4280SAmos Kong 9696e790746SPaolo Bonzini return VIRTIO_NET_OK; 9706e790746SPaolo Bonzini } 9716e790746SPaolo Bonzini 972644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 973644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 974644c9858SDmitry Fleytman { 975644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 976644c9858SDmitry Fleytman uint64_t offloads; 977644c9858SDmitry Fleytman size_t s; 978644c9858SDmitry Fleytman 97995129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 980644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 981644c9858SDmitry Fleytman } 982644c9858SDmitry Fleytman 983644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 984644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 985644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 986644c9858SDmitry Fleytman } 987644c9858SDmitry Fleytman 988644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 989644c9858SDmitry Fleytman uint64_t supported_offloads; 990644c9858SDmitry Fleytman 991189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 992189ae6bbSJason Wang 993644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 994644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 995644c9858SDmitry Fleytman } 996644c9858SDmitry Fleytman 9972974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 9982974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 9992974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10002974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 10012974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 10022974e916SYuri Benditovich 1003644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1004644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1005644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1006644c9858SDmitry Fleytman } 1007644c9858SDmitry Fleytman 1008644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1009644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1010644c9858SDmitry Fleytman 1011644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1012644c9858SDmitry Fleytman } else { 1013644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1014644c9858SDmitry Fleytman } 1015644c9858SDmitry Fleytman } 1016644c9858SDmitry Fleytman 10176e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 10186e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10196e790746SPaolo Bonzini { 10201399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 10216e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 10226e790746SPaolo Bonzini size_t s; 1023b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10246e790746SPaolo Bonzini 10256e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 10266e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 10276e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10286e790746SPaolo Bonzini } 10296e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 10306e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 10316e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1032b1be4280SAmos Kong rxfilter_notify(nc); 1033b1be4280SAmos Kong 10346e790746SPaolo Bonzini return VIRTIO_NET_OK; 10356e790746SPaolo Bonzini } 10366e790746SPaolo Bonzini 10376e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 10386e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10396e790746SPaolo Bonzini } 10406e790746SPaolo Bonzini 1041cae2e556SAmos Kong int in_use = 0; 1042cae2e556SAmos Kong int first_multi = 0; 1043cae2e556SAmos Kong uint8_t uni_overflow = 0; 1044cae2e556SAmos Kong uint8_t multi_overflow = 0; 1045cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 10466e790746SPaolo Bonzini 10476e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 10486e790746SPaolo Bonzini sizeof(mac_data.entries)); 10491399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 10506e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1051b1be4280SAmos Kong goto error; 10526e790746SPaolo Bonzini } 10536e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 10546e790746SPaolo Bonzini 10556e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1056b1be4280SAmos Kong goto error; 10576e790746SPaolo Bonzini } 10586e790746SPaolo Bonzini 10596e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1060cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 10616e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 10626e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1063b1be4280SAmos Kong goto error; 10646e790746SPaolo Bonzini } 1065cae2e556SAmos Kong in_use += mac_data.entries; 10666e790746SPaolo Bonzini } else { 1067cae2e556SAmos Kong uni_overflow = 1; 10686e790746SPaolo Bonzini } 10696e790746SPaolo Bonzini 10706e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 10716e790746SPaolo Bonzini 1072cae2e556SAmos Kong first_multi = in_use; 10736e790746SPaolo Bonzini 10746e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 10756e790746SPaolo Bonzini sizeof(mac_data.entries)); 10761399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 10776e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1078b1be4280SAmos Kong goto error; 10796e790746SPaolo Bonzini } 10806e790746SPaolo Bonzini 10816e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 10826e790746SPaolo Bonzini 10836e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1084b1be4280SAmos Kong goto error; 10856e790746SPaolo Bonzini } 10866e790746SPaolo Bonzini 1087edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1088cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 10896e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 10906e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1091b1be4280SAmos Kong goto error; 10926e790746SPaolo Bonzini } 1093cae2e556SAmos Kong in_use += mac_data.entries; 10946e790746SPaolo Bonzini } else { 1095cae2e556SAmos Kong multi_overflow = 1; 10966e790746SPaolo Bonzini } 10976e790746SPaolo Bonzini 1098cae2e556SAmos Kong n->mac_table.in_use = in_use; 1099cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1100cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1101cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1102cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1103cae2e556SAmos Kong g_free(macs); 1104b1be4280SAmos Kong rxfilter_notify(nc); 1105b1be4280SAmos Kong 11066e790746SPaolo Bonzini return VIRTIO_NET_OK; 1107b1be4280SAmos Kong 1108b1be4280SAmos Kong error: 1109cae2e556SAmos Kong g_free(macs); 1110b1be4280SAmos Kong return VIRTIO_NET_ERR; 11116e790746SPaolo Bonzini } 11126e790746SPaolo Bonzini 11136e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 11146e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11156e790746SPaolo Bonzini { 11161399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11176e790746SPaolo Bonzini uint16_t vid; 11186e790746SPaolo Bonzini size_t s; 1119b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11206e790746SPaolo Bonzini 11216e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 11221399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 11236e790746SPaolo Bonzini if (s != sizeof(vid)) { 11246e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11256e790746SPaolo Bonzini } 11266e790746SPaolo Bonzini 11276e790746SPaolo Bonzini if (vid >= MAX_VLAN) 11286e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11296e790746SPaolo Bonzini 11306e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 11316e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 11326e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 11336e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 11346e790746SPaolo Bonzini else 11356e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11366e790746SPaolo Bonzini 1137b1be4280SAmos Kong rxfilter_notify(nc); 1138b1be4280SAmos Kong 11396e790746SPaolo Bonzini return VIRTIO_NET_OK; 11406e790746SPaolo Bonzini } 11416e790746SPaolo Bonzini 1142f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1143f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1144f57fcf70SJason Wang { 11459d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1146f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1147f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1148f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 11499d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 11509d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1151f57fcf70SJason Wang } 1152f57fcf70SJason Wang return VIRTIO_NET_OK; 1153f57fcf70SJason Wang } else { 1154f57fcf70SJason Wang return VIRTIO_NET_ERR; 1155f57fcf70SJason Wang } 1156f57fcf70SJason Wang } 1157f57fcf70SJason Wang 115859079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 115959079029SYuri Benditovich { 116059079029SYuri Benditovich if (n->rss_data.enabled) { 116159079029SYuri Benditovich trace_virtio_net_rss_disable(); 116259079029SYuri Benditovich } 116359079029SYuri Benditovich n->rss_data.enabled = false; 116459079029SYuri Benditovich } 116559079029SYuri Benditovich 116659079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1167e22f0603SYuri Benditovich struct iovec *iov, 1168e22f0603SYuri Benditovich unsigned int iov_cnt, 1169e22f0603SYuri Benditovich bool do_rss) 117059079029SYuri Benditovich { 117159079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 117259079029SYuri Benditovich struct virtio_net_rss_config cfg; 117359079029SYuri Benditovich size_t s, offset = 0, size_get; 117459079029SYuri Benditovich uint16_t queues, i; 117559079029SYuri Benditovich struct { 117659079029SYuri Benditovich uint16_t us; 117759079029SYuri Benditovich uint8_t b; 117859079029SYuri Benditovich } QEMU_PACKED temp; 117959079029SYuri Benditovich const char *err_msg = ""; 118059079029SYuri Benditovich uint32_t err_value = 0; 118159079029SYuri Benditovich 1182e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 118359079029SYuri Benditovich err_msg = "RSS is not negotiated"; 118459079029SYuri Benditovich goto error; 118559079029SYuri Benditovich } 1186e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1187e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1188e22f0603SYuri Benditovich goto error; 1189e22f0603SYuri Benditovich } 119059079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 119159079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 119259079029SYuri Benditovich if (s != size_get) { 119359079029SYuri Benditovich err_msg = "Short command buffer"; 119459079029SYuri Benditovich err_value = (uint32_t)s; 119559079029SYuri Benditovich goto error; 119659079029SYuri Benditovich } 119759079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 119859079029SYuri Benditovich n->rss_data.indirections_len = 119959079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 120059079029SYuri Benditovich n->rss_data.indirections_len++; 1201e22f0603SYuri Benditovich if (!do_rss) { 1202e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1203e22f0603SYuri Benditovich } 120459079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 120559079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 120659079029SYuri Benditovich err_value = n->rss_data.indirections_len; 120759079029SYuri Benditovich goto error; 120859079029SYuri Benditovich } 120959079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 121059079029SYuri Benditovich err_msg = "Too large indirection table"; 121159079029SYuri Benditovich err_value = n->rss_data.indirections_len; 121259079029SYuri Benditovich goto error; 121359079029SYuri Benditovich } 1214e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1215e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 121659079029SYuri Benditovich if (n->rss_data.default_queue >= n->max_queues) { 121759079029SYuri Benditovich err_msg = "Invalid default queue"; 121859079029SYuri Benditovich err_value = n->rss_data.default_queue; 121959079029SYuri Benditovich goto error; 122059079029SYuri Benditovich } 122159079029SYuri Benditovich offset += size_get; 122259079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 122359079029SYuri Benditovich g_free(n->rss_data.indirections_table); 122459079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 122559079029SYuri Benditovich if (!n->rss_data.indirections_table) { 122659079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 122759079029SYuri Benditovich err_value = n->rss_data.indirections_len; 122859079029SYuri Benditovich goto error; 122959079029SYuri Benditovich } 123059079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 123159079029SYuri Benditovich n->rss_data.indirections_table, size_get); 123259079029SYuri Benditovich if (s != size_get) { 123359079029SYuri Benditovich err_msg = "Short indirection table buffer"; 123459079029SYuri Benditovich err_value = (uint32_t)s; 123559079029SYuri Benditovich goto error; 123659079029SYuri Benditovich } 123759079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 123859079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 123959079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 124059079029SYuri Benditovich } 124159079029SYuri Benditovich offset += size_get; 124259079029SYuri Benditovich size_get = sizeof(temp); 124359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 124459079029SYuri Benditovich if (s != size_get) { 124559079029SYuri Benditovich err_msg = "Can't get queues"; 124659079029SYuri Benditovich err_value = (uint32_t)s; 124759079029SYuri Benditovich goto error; 124859079029SYuri Benditovich } 1249e22f0603SYuri Benditovich queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues; 125059079029SYuri Benditovich if (queues == 0 || queues > n->max_queues) { 125159079029SYuri Benditovich err_msg = "Invalid number of queues"; 125259079029SYuri Benditovich err_value = queues; 125359079029SYuri Benditovich goto error; 125459079029SYuri Benditovich } 125559079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 125659079029SYuri Benditovich err_msg = "Invalid key size"; 125759079029SYuri Benditovich err_value = temp.b; 125859079029SYuri Benditovich goto error; 125959079029SYuri Benditovich } 126059079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 126159079029SYuri Benditovich err_msg = "No key provided"; 126259079029SYuri Benditovich err_value = 0; 126359079029SYuri Benditovich goto error; 126459079029SYuri Benditovich } 126559079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 126659079029SYuri Benditovich virtio_net_disable_rss(n); 126759079029SYuri Benditovich return queues; 126859079029SYuri Benditovich } 126959079029SYuri Benditovich offset += size_get; 127059079029SYuri Benditovich size_get = temp.b; 127159079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 127259079029SYuri Benditovich if (s != size_get) { 127359079029SYuri Benditovich err_msg = "Can get key buffer"; 127459079029SYuri Benditovich err_value = (uint32_t)s; 127559079029SYuri Benditovich goto error; 127659079029SYuri Benditovich } 127759079029SYuri Benditovich n->rss_data.enabled = true; 127859079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 127959079029SYuri Benditovich n->rss_data.indirections_len, 128059079029SYuri Benditovich temp.b); 128159079029SYuri Benditovich return queues; 128259079029SYuri Benditovich error: 128359079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 128459079029SYuri Benditovich virtio_net_disable_rss(n); 128559079029SYuri Benditovich return 0; 128659079029SYuri Benditovich } 128759079029SYuri Benditovich 12886e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 12896e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 12906e790746SPaolo Bonzini { 129117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 12926e790746SPaolo Bonzini uint16_t queues; 12936e790746SPaolo Bonzini 129459079029SYuri Benditovich virtio_net_disable_rss(n); 1295e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1296e22f0603SYuri Benditovich queues = virtio_net_handle_rss(n, iov, iov_cnt, false); 1297e22f0603SYuri Benditovich return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1298e22f0603SYuri Benditovich } 129959079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1300e22f0603SYuri Benditovich queues = virtio_net_handle_rss(n, iov, iov_cnt, true); 130159079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 130259079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 130359079029SYuri Benditovich size_t s; 130459079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 130559079029SYuri Benditovich return VIRTIO_NET_ERR; 130659079029SYuri Benditovich } 13076e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 13086e790746SPaolo Bonzini if (s != sizeof(mq)) { 13096e790746SPaolo Bonzini return VIRTIO_NET_ERR; 13106e790746SPaolo Bonzini } 131159079029SYuri Benditovich queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 13126e790746SPaolo Bonzini 131359079029SYuri Benditovich } else { 13146e790746SPaolo Bonzini return VIRTIO_NET_ERR; 13156e790746SPaolo Bonzini } 13166e790746SPaolo Bonzini 13176e790746SPaolo Bonzini if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 13186e790746SPaolo Bonzini queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 13196e790746SPaolo Bonzini queues > n->max_queues || 13206e790746SPaolo Bonzini !n->multiqueue) { 13216e790746SPaolo Bonzini return VIRTIO_NET_ERR; 13226e790746SPaolo Bonzini } 13236e790746SPaolo Bonzini 13246e790746SPaolo Bonzini n->curr_queues = queues; 13256e790746SPaolo Bonzini /* stop the backend before changing the number of queues to avoid handling a 13266e790746SPaolo Bonzini * disabled queue */ 132717a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 13286e790746SPaolo Bonzini virtio_net_set_queues(n); 13296e790746SPaolo Bonzini 13306e790746SPaolo Bonzini return VIRTIO_NET_OK; 13316e790746SPaolo Bonzini } 1332ba7eadb5SGreg Kurz 13336e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 13346e790746SPaolo Bonzini { 133517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 13366e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 13376e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 133851b19ebeSPaolo Bonzini VirtQueueElement *elem; 13396e790746SPaolo Bonzini size_t s; 1340771b6ed3SJason Wang struct iovec *iov, *iov2; 13416e790746SPaolo Bonzini unsigned int iov_cnt; 13426e790746SPaolo Bonzini 134351b19ebeSPaolo Bonzini for (;;) { 134451b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 134551b19ebeSPaolo Bonzini if (!elem) { 134651b19ebeSPaolo Bonzini break; 134751b19ebeSPaolo Bonzini } 134851b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 134951b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 1350ba7eadb5SGreg Kurz virtio_error(vdev, "virtio-net ctrl missing headers"); 1351ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1352ba7eadb5SGreg Kurz g_free(elem); 1353ba7eadb5SGreg Kurz break; 13546e790746SPaolo Bonzini } 13556e790746SPaolo Bonzini 135651b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 135751b19ebeSPaolo Bonzini iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num); 13586e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 13596e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 13606e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 13616e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 13626e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 13636e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 13646e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 13656e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 13666e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 13676e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 1368f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1369f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 13706e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 13716e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 1372644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1373644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 13746e790746SPaolo Bonzini } 13756e790746SPaolo Bonzini 137651b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 13776e790746SPaolo Bonzini assert(s == sizeof(status)); 13786e790746SPaolo Bonzini 137951b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 13806e790746SPaolo Bonzini virtio_notify(vdev, vq); 1381771b6ed3SJason Wang g_free(iov2); 138251b19ebeSPaolo Bonzini g_free(elem); 13836e790746SPaolo Bonzini } 13846e790746SPaolo Bonzini } 13856e790746SPaolo Bonzini 13866e790746SPaolo Bonzini /* RX */ 13876e790746SPaolo Bonzini 13886e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 13896e790746SPaolo Bonzini { 139017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 13916e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 13926e790746SPaolo Bonzini 13936e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 13946e790746SPaolo Bonzini } 13956e790746SPaolo Bonzini 1396b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 13976e790746SPaolo Bonzini { 13986e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 139917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 14006e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 14016e790746SPaolo Bonzini 140217a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1403b8c4b67eSPhilippe Mathieu-Daudé return false; 14046e790746SPaolo Bonzini } 14056e790746SPaolo Bonzini 14066e790746SPaolo Bonzini if (nc->queue_index >= n->curr_queues) { 1407b8c4b67eSPhilippe Mathieu-Daudé return false; 14086e790746SPaolo Bonzini } 14096e790746SPaolo Bonzini 14106e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 141117a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1412b8c4b67eSPhilippe Mathieu-Daudé return false; 14136e790746SPaolo Bonzini } 14146e790746SPaolo Bonzini 1415b8c4b67eSPhilippe Mathieu-Daudé return true; 14166e790746SPaolo Bonzini } 14176e790746SPaolo Bonzini 14186e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 14196e790746SPaolo Bonzini { 14206e790746SPaolo Bonzini VirtIONet *n = q->n; 14216e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 14226e790746SPaolo Bonzini (n->mergeable_rx_bufs && 14236e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 14246e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 14256e790746SPaolo Bonzini 14266e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 14276e790746SPaolo Bonzini * available after the above check but before notification was 14286e790746SPaolo Bonzini * enabled, check for available buffers again. 14296e790746SPaolo Bonzini */ 14306e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 14316e790746SPaolo Bonzini (n->mergeable_rx_bufs && 14326e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 14336e790746SPaolo Bonzini return 0; 14346e790746SPaolo Bonzini } 14356e790746SPaolo Bonzini } 14366e790746SPaolo Bonzini 14376e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 14386e790746SPaolo Bonzini return 1; 14396e790746SPaolo Bonzini } 14406e790746SPaolo Bonzini 14411399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1442032a74a1SCédric Le Goater { 14431399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 14441399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 14451399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 14461399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1447032a74a1SCédric Le Goater } 1448032a74a1SCédric Le Goater 14496e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 14506e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 14516e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 14526e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 14536e790746SPaolo Bonzini * dhclient yet. 14546e790746SPaolo Bonzini * 14556e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 14566e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 14576e790746SPaolo Bonzini * kernels. 14586e790746SPaolo Bonzini * 14596e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 14606e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 14616e790746SPaolo Bonzini * cache. 14626e790746SPaolo Bonzini */ 14636e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 14646e790746SPaolo Bonzini uint8_t *buf, size_t size) 14656e790746SPaolo Bonzini { 14666e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 14676e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 14686e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 14696e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 14706e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 14716e790746SPaolo Bonzini net_checksum_calculate(buf, size); 14726e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 14736e790746SPaolo Bonzini } 14746e790746SPaolo Bonzini } 14756e790746SPaolo Bonzini 14766e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 14776e790746SPaolo Bonzini const void *buf, size_t size) 14786e790746SPaolo Bonzini { 14796e790746SPaolo Bonzini if (n->has_vnet_hdr) { 14806e790746SPaolo Bonzini /* FIXME this cast is evil */ 14816e790746SPaolo Bonzini void *wbuf = (void *)buf; 14826e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 14836e790746SPaolo Bonzini size - n->host_hdr_len); 14841bfa316cSGreg Kurz 14851bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 14861399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 14871bfa316cSGreg Kurz } 14886e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 14896e790746SPaolo Bonzini } else { 14906e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 14916e790746SPaolo Bonzini .flags = 0, 14926e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 14936e790746SPaolo Bonzini }; 14946e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 14956e790746SPaolo Bonzini } 14966e790746SPaolo Bonzini } 14976e790746SPaolo Bonzini 14986e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 14996e790746SPaolo Bonzini { 15006e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 15016e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 15026e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 15036e790746SPaolo Bonzini int i; 15046e790746SPaolo Bonzini 15056e790746SPaolo Bonzini if (n->promisc) 15066e790746SPaolo Bonzini return 1; 15076e790746SPaolo Bonzini 15086e790746SPaolo Bonzini ptr += n->host_hdr_len; 15096e790746SPaolo Bonzini 15106e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 15117542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 15126e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 15136e790746SPaolo Bonzini return 0; 15146e790746SPaolo Bonzini } 15156e790746SPaolo Bonzini 15166e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 15176e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 15186e790746SPaolo Bonzini return !n->nobcast; 15196e790746SPaolo Bonzini } else if (n->nomulti) { 15206e790746SPaolo Bonzini return 0; 15216e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 15226e790746SPaolo Bonzini return 1; 15236e790746SPaolo Bonzini } 15246e790746SPaolo Bonzini 15256e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 15266e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 15276e790746SPaolo Bonzini return 1; 15286e790746SPaolo Bonzini } 15296e790746SPaolo Bonzini } 15306e790746SPaolo Bonzini } else { // unicast 15316e790746SPaolo Bonzini if (n->nouni) { 15326e790746SPaolo Bonzini return 0; 15336e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 15346e790746SPaolo Bonzini return 1; 15356e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 15366e790746SPaolo Bonzini return 1; 15376e790746SPaolo Bonzini } 15386e790746SPaolo Bonzini 15396e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 15406e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 15416e790746SPaolo Bonzini return 1; 15426e790746SPaolo Bonzini } 15436e790746SPaolo Bonzini } 15446e790746SPaolo Bonzini } 15456e790746SPaolo Bonzini 15466e790746SPaolo Bonzini return 0; 15476e790746SPaolo Bonzini } 15486e790746SPaolo Bonzini 15494474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4, 15504474e37aSYuri Benditovich bool isip6, 15514474e37aSYuri Benditovich bool isudp, 15524474e37aSYuri Benditovich bool istcp, 15534474e37aSYuri Benditovich uint32_t types) 15544474e37aSYuri Benditovich { 15554474e37aSYuri Benditovich if (isip4) { 15564474e37aSYuri Benditovich if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) { 15574474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 15584474e37aSYuri Benditovich } 15594474e37aSYuri Benditovich if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) { 15604474e37aSYuri Benditovich return NetPktRssIpV4Udp; 15614474e37aSYuri Benditovich } 15624474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 15634474e37aSYuri Benditovich return NetPktRssIpV4; 15644474e37aSYuri Benditovich } 15654474e37aSYuri Benditovich } else if (isip6) { 15664474e37aSYuri Benditovich uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 15674474e37aSYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6; 15684474e37aSYuri Benditovich 15694474e37aSYuri Benditovich if (istcp && (types & mask)) { 15704474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ? 15714474e37aSYuri Benditovich NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp; 15724474e37aSYuri Benditovich } 15734474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6; 15744474e37aSYuri Benditovich if (isudp && (types & mask)) { 15754474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ? 15764474e37aSYuri Benditovich NetPktRssIpV6UdpEx : NetPktRssIpV6Udp; 15774474e37aSYuri Benditovich } 15784474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6; 15794474e37aSYuri Benditovich if (types & mask) { 15804474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ? 15814474e37aSYuri Benditovich NetPktRssIpV6Ex : NetPktRssIpV6; 15824474e37aSYuri Benditovich } 15834474e37aSYuri Benditovich } 15844474e37aSYuri Benditovich return 0xff; 15854474e37aSYuri Benditovich } 15864474e37aSYuri Benditovich 1587e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1588e22f0603SYuri Benditovich uint32_t hash) 1589e22f0603SYuri Benditovich { 1590e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1591e22f0603SYuri Benditovich hdr->hash_value = hash; 1592e22f0603SYuri Benditovich hdr->hash_report = report; 1593e22f0603SYuri Benditovich } 1594e22f0603SYuri Benditovich 15954474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 159697cd965cSPaolo Bonzini size_t size) 15976e790746SPaolo Bonzini { 15986e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1599e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 16004474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 16014474e37aSYuri Benditovich uint8_t net_hash_type; 16024474e37aSYuri Benditovich uint32_t hash; 16034474e37aSYuri Benditovich bool isip4, isip6, isudp, istcp; 1604e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1605e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1606e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1607e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1608e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1609e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1610e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1611e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1612e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1613e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1614e22f0603SYuri Benditovich }; 16154474e37aSYuri Benditovich 16164474e37aSYuri Benditovich net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len, 16174474e37aSYuri Benditovich size - n->host_hdr_len); 16184474e37aSYuri Benditovich net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp); 16194474e37aSYuri Benditovich if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) { 16204474e37aSYuri Benditovich istcp = isudp = false; 16214474e37aSYuri Benditovich } 16224474e37aSYuri Benditovich if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) { 16234474e37aSYuri Benditovich istcp = isudp = false; 16244474e37aSYuri Benditovich } 16254474e37aSYuri Benditovich net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp, 16264474e37aSYuri Benditovich n->rss_data.hash_types); 16274474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1628e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1629e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1630e22f0603SYuri Benditovich } 1631e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 16324474e37aSYuri Benditovich } 16334474e37aSYuri Benditovich 16344474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1635e22f0603SYuri Benditovich 1636e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1637e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1638e22f0603SYuri Benditovich } 1639e22f0603SYuri Benditovich 1640e22f0603SYuri Benditovich if (n->rss_data.redirect) { 16414474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 16424474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 16434474e37aSYuri Benditovich } 1644e22f0603SYuri Benditovich 1645e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 16464474e37aSYuri Benditovich } 16474474e37aSYuri Benditovich 16484474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 16494474e37aSYuri Benditovich size_t size, bool no_rss) 16504474e37aSYuri Benditovich { 16514474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 16526e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 165317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 16546e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 16556e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 16566e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 16576e790746SPaolo Bonzini size_t offset, i, guest_offset; 16586e790746SPaolo Bonzini 16596e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 16606e790746SPaolo Bonzini return -1; 16616e790746SPaolo Bonzini } 16626e790746SPaolo Bonzini 16634474e37aSYuri Benditovich if (!no_rss && n->rss_data.enabled) { 16644474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 16654474e37aSYuri Benditovich if (index >= 0) { 16664474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 16674474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 16684474e37aSYuri Benditovich } 16694474e37aSYuri Benditovich } 16704474e37aSYuri Benditovich 16716e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 16726e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 16736e790746SPaolo Bonzini return 0; 16746e790746SPaolo Bonzini } 16756e790746SPaolo Bonzini 16766e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 16776e790746SPaolo Bonzini return size; 16786e790746SPaolo Bonzini 16796e790746SPaolo Bonzini offset = i = 0; 16806e790746SPaolo Bonzini 16816e790746SPaolo Bonzini while (offset < size) { 168251b19ebeSPaolo Bonzini VirtQueueElement *elem; 16836e790746SPaolo Bonzini int len, total; 168451b19ebeSPaolo Bonzini const struct iovec *sg; 16856e790746SPaolo Bonzini 16866e790746SPaolo Bonzini total = 0; 16876e790746SPaolo Bonzini 168851b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 168951b19ebeSPaolo Bonzini if (!elem) { 1690ba10b9c0SGreg Kurz if (i) { 1691ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 16926e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1693019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1694019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 16956e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1696019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1697019a3edbSGerd Hoffmann vdev->guest_features); 1698ba10b9c0SGreg Kurz } 1699ba10b9c0SGreg Kurz return -1; 17006e790746SPaolo Bonzini } 17016e790746SPaolo Bonzini 170251b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1703ba10b9c0SGreg Kurz virtio_error(vdev, 1704ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1705ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1706ba10b9c0SGreg Kurz g_free(elem); 1707ba10b9c0SGreg Kurz return -1; 17086e790746SPaolo Bonzini } 17096e790746SPaolo Bonzini 171051b19ebeSPaolo Bonzini sg = elem->in_sg; 17116e790746SPaolo Bonzini if (i == 0) { 17126e790746SPaolo Bonzini assert(offset == 0); 17136e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 17146e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 171551b19ebeSPaolo Bonzini sg, elem->in_num, 17166e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 17176e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 17186e790746SPaolo Bonzini } 17196e790746SPaolo Bonzini 172051b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1721e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1722e22f0603SYuri Benditovich offset = sizeof(mhdr); 1723e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1724e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 1725e22f0603SYuri Benditovich } 17266e790746SPaolo Bonzini offset = n->host_hdr_len; 17276e790746SPaolo Bonzini total += n->guest_hdr_len; 17286e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 17296e790746SPaolo Bonzini } else { 17306e790746SPaolo Bonzini guest_offset = 0; 17316e790746SPaolo Bonzini } 17326e790746SPaolo Bonzini 17336e790746SPaolo Bonzini /* copy in packet. ugh */ 173451b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 17356e790746SPaolo Bonzini buf + offset, size - offset); 17366e790746SPaolo Bonzini total += len; 17376e790746SPaolo Bonzini offset += len; 17386e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 17396e790746SPaolo Bonzini * must have consumed the complete packet. 17406e790746SPaolo Bonzini * Otherwise, drop it. */ 17416e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 174227e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 174351b19ebeSPaolo Bonzini g_free(elem); 17446e790746SPaolo Bonzini return size; 17456e790746SPaolo Bonzini } 17466e790746SPaolo Bonzini 17476e790746SPaolo Bonzini /* signal other side */ 174851b19ebeSPaolo Bonzini virtqueue_fill(q->rx_vq, elem, total, i++); 174951b19ebeSPaolo Bonzini g_free(elem); 17506e790746SPaolo Bonzini } 17516e790746SPaolo Bonzini 17526e790746SPaolo Bonzini if (mhdr_cnt) { 17531399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 17546e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 17556e790746SPaolo Bonzini 0, 17566e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 17576e790746SPaolo Bonzini } 17586e790746SPaolo Bonzini 17596e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 176017a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 17616e790746SPaolo Bonzini 17626e790746SPaolo Bonzini return size; 17636e790746SPaolo Bonzini } 17646e790746SPaolo Bonzini 17652974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 176697cd965cSPaolo Bonzini size_t size) 176797cd965cSPaolo Bonzini { 1768068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 176997cd965cSPaolo Bonzini 17704474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 177197cd965cSPaolo Bonzini } 177297cd965cSPaolo Bonzini 17732974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 17742974e916SYuri Benditovich const uint8_t *buf, 17752974e916SYuri Benditovich VirtioNetRscUnit *unit) 17762974e916SYuri Benditovich { 17772974e916SYuri Benditovich uint16_t ip_hdrlen; 17782974e916SYuri Benditovich struct ip_header *ip; 17792974e916SYuri Benditovich 17802974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 17812974e916SYuri Benditovich + sizeof(struct eth_header)); 17822974e916SYuri Benditovich unit->ip = (void *)ip; 17832974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 17842974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 17852974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 17862974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 17872974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 17882974e916SYuri Benditovich } 17892974e916SYuri Benditovich 17902974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 17912974e916SYuri Benditovich const uint8_t *buf, 17922974e916SYuri Benditovich VirtioNetRscUnit *unit) 17932974e916SYuri Benditovich { 17942974e916SYuri Benditovich struct ip6_header *ip6; 17952974e916SYuri Benditovich 17962974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 17972974e916SYuri Benditovich + sizeof(struct eth_header)); 17982974e916SYuri Benditovich unit->ip = ip6; 17992974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 180078ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 18012974e916SYuri Benditovich + sizeof(struct ip6_header)); 18022974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 18032974e916SYuri Benditovich 18042974e916SYuri Benditovich /* There is a difference between payload lenght in ipv4 and v6, 18052974e916SYuri Benditovich ip header is excluded in ipv6 */ 18062974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 18072974e916SYuri Benditovich } 18082974e916SYuri Benditovich 18092974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 18102974e916SYuri Benditovich VirtioNetRscSeg *seg) 18112974e916SYuri Benditovich { 18122974e916SYuri Benditovich int ret; 1813dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 18142974e916SYuri Benditovich 1815dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 18162974e916SYuri Benditovich h->flags = 0; 18172974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 18182974e916SYuri Benditovich 18192974e916SYuri Benditovich if (seg->is_coalesced) { 1820dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 1821dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 18222974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 18232974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 18242974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 18252974e916SYuri Benditovich } else { 18262974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 18272974e916SYuri Benditovich } 18282974e916SYuri Benditovich } 18292974e916SYuri Benditovich 18302974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 18312974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 18322974e916SYuri Benditovich g_free(seg->buf); 18332974e916SYuri Benditovich g_free(seg); 18342974e916SYuri Benditovich 18352974e916SYuri Benditovich return ret; 18362974e916SYuri Benditovich } 18372974e916SYuri Benditovich 18382974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 18392974e916SYuri Benditovich { 18402974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 18412974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 18422974e916SYuri Benditovich 18432974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 18442974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 18452974e916SYuri Benditovich chain->stat.purge_failed++; 18462974e916SYuri Benditovich continue; 18472974e916SYuri Benditovich } 18482974e916SYuri Benditovich } 18492974e916SYuri Benditovich 18502974e916SYuri Benditovich chain->stat.timer++; 18512974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 18522974e916SYuri Benditovich timer_mod(chain->drain_timer, 18532974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 18542974e916SYuri Benditovich } 18552974e916SYuri Benditovich } 18562974e916SYuri Benditovich 18572974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 18582974e916SYuri Benditovich { 18592974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 18602974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 18612974e916SYuri Benditovich 18622974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 18632974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 18642974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 18652974e916SYuri Benditovich g_free(seg->buf); 18662974e916SYuri Benditovich g_free(seg); 18672974e916SYuri Benditovich } 18682974e916SYuri Benditovich 18692974e916SYuri Benditovich timer_del(chain->drain_timer); 18702974e916SYuri Benditovich timer_free(chain->drain_timer); 18712974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 18722974e916SYuri Benditovich g_free(chain); 18732974e916SYuri Benditovich } 18742974e916SYuri Benditovich } 18752974e916SYuri Benditovich 18762974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 18772974e916SYuri Benditovich NetClientState *nc, 18782974e916SYuri Benditovich const uint8_t *buf, size_t size) 18792974e916SYuri Benditovich { 18802974e916SYuri Benditovich uint16_t hdr_len; 18812974e916SYuri Benditovich VirtioNetRscSeg *seg; 18822974e916SYuri Benditovich 18832974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 18842974e916SYuri Benditovich seg = g_malloc(sizeof(VirtioNetRscSeg)); 18852974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 18862974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 18872974e916SYuri Benditovich memcpy(seg->buf, buf, size); 18882974e916SYuri Benditovich seg->size = size; 18892974e916SYuri Benditovich seg->packets = 1; 18902974e916SYuri Benditovich seg->dup_ack = 0; 18912974e916SYuri Benditovich seg->is_coalesced = 0; 18922974e916SYuri Benditovich seg->nc = nc; 18932974e916SYuri Benditovich 18942974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 18952974e916SYuri Benditovich chain->stat.cache++; 18962974e916SYuri Benditovich 18972974e916SYuri Benditovich switch (chain->proto) { 18982974e916SYuri Benditovich case ETH_P_IP: 18992974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 19002974e916SYuri Benditovich break; 19012974e916SYuri Benditovich case ETH_P_IPV6: 19022974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 19032974e916SYuri Benditovich break; 19042974e916SYuri Benditovich default: 19052974e916SYuri Benditovich g_assert_not_reached(); 19062974e916SYuri Benditovich } 19072974e916SYuri Benditovich } 19082974e916SYuri Benditovich 19092974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 19102974e916SYuri Benditovich VirtioNetRscSeg *seg, 19112974e916SYuri Benditovich const uint8_t *buf, 19122974e916SYuri Benditovich struct tcp_header *n_tcp, 19132974e916SYuri Benditovich struct tcp_header *o_tcp) 19142974e916SYuri Benditovich { 19152974e916SYuri Benditovich uint32_t nack, oack; 19162974e916SYuri Benditovich uint16_t nwin, owin; 19172974e916SYuri Benditovich 19182974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 19192974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 19202974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 19212974e916SYuri Benditovich owin = htons(o_tcp->th_win); 19222974e916SYuri Benditovich 19232974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 19242974e916SYuri Benditovich chain->stat.ack_out_of_win++; 19252974e916SYuri Benditovich return RSC_FINAL; 19262974e916SYuri Benditovich } else if (nack == oack) { 19272974e916SYuri Benditovich /* duplicated ack or window probe */ 19282974e916SYuri Benditovich if (nwin == owin) { 19292974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 19302974e916SYuri Benditovich chain->stat.dup_ack++; 19312974e916SYuri Benditovich return RSC_FINAL; 19322974e916SYuri Benditovich } else { 19332974e916SYuri Benditovich /* Coalesce window update */ 19342974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 19352974e916SYuri Benditovich chain->stat.win_update++; 19362974e916SYuri Benditovich return RSC_COALESCE; 19372974e916SYuri Benditovich } 19382974e916SYuri Benditovich } else { 19392974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 19402974e916SYuri Benditovich chain->stat.pure_ack++; 19412974e916SYuri Benditovich return RSC_FINAL; 19422974e916SYuri Benditovich } 19432974e916SYuri Benditovich } 19442974e916SYuri Benditovich 19452974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 19462974e916SYuri Benditovich VirtioNetRscSeg *seg, 19472974e916SYuri Benditovich const uint8_t *buf, 19482974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 19492974e916SYuri Benditovich { 19502974e916SYuri Benditovich void *data; 19512974e916SYuri Benditovich uint16_t o_ip_len; 19522974e916SYuri Benditovich uint32_t nseq, oseq; 19532974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 19542974e916SYuri Benditovich 19552974e916SYuri Benditovich o_unit = &seg->unit; 19562974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 19572974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 19582974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 19592974e916SYuri Benditovich 19602974e916SYuri Benditovich /* out of order or retransmitted. */ 19612974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 19622974e916SYuri Benditovich chain->stat.data_out_of_win++; 19632974e916SYuri Benditovich return RSC_FINAL; 19642974e916SYuri Benditovich } 19652974e916SYuri Benditovich 19662974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 19672974e916SYuri Benditovich if (nseq == oseq) { 19682974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 19692974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 19702974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 19712974e916SYuri Benditovich goto coalesce; 19722974e916SYuri Benditovich } else { 19732974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 19742974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 19752974e916SYuri Benditovich } 19762974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 19772974e916SYuri Benditovich /* Not a consistent packet, out of order */ 19782974e916SYuri Benditovich chain->stat.data_out_of_order++; 19792974e916SYuri Benditovich return RSC_FINAL; 19802974e916SYuri Benditovich } else { 19812974e916SYuri Benditovich coalesce: 19822974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 19832974e916SYuri Benditovich chain->stat.over_size++; 19842974e916SYuri Benditovich return RSC_FINAL; 19852974e916SYuri Benditovich } 19862974e916SYuri Benditovich 19872974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 19882974e916SYuri Benditovich so use the field value to update and record the new data len */ 19892974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 19902974e916SYuri Benditovich 19912974e916SYuri Benditovich /* update field in ip header */ 19922974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 19932974e916SYuri Benditovich 19942974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 19952974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 19962974e916SYuri Benditovich guest (only if it uses RSC feature). */ 19972974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 19982974e916SYuri Benditovich 19992974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 20002974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 20012974e916SYuri Benditovich 20022974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 20032974e916SYuri Benditovich seg->size += n_unit->payload; 20042974e916SYuri Benditovich seg->packets++; 20052974e916SYuri Benditovich chain->stat.coalesced++; 20062974e916SYuri Benditovich return RSC_COALESCE; 20072974e916SYuri Benditovich } 20082974e916SYuri Benditovich } 20092974e916SYuri Benditovich 20102974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 20112974e916SYuri Benditovich VirtioNetRscSeg *seg, 20122974e916SYuri Benditovich const uint8_t *buf, size_t size, 20132974e916SYuri Benditovich VirtioNetRscUnit *unit) 20142974e916SYuri Benditovich { 20152974e916SYuri Benditovich struct ip_header *ip1, *ip2; 20162974e916SYuri Benditovich 20172974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 20182974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 20192974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 20202974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 20212974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 20222974e916SYuri Benditovich chain->stat.no_match++; 20232974e916SYuri Benditovich return RSC_NO_MATCH; 20242974e916SYuri Benditovich } 20252974e916SYuri Benditovich 20262974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 20272974e916SYuri Benditovich } 20282974e916SYuri Benditovich 20292974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 20302974e916SYuri Benditovich VirtioNetRscSeg *seg, 20312974e916SYuri Benditovich const uint8_t *buf, size_t size, 20322974e916SYuri Benditovich VirtioNetRscUnit *unit) 20332974e916SYuri Benditovich { 20342974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 20352974e916SYuri Benditovich 20362974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 20372974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 20382974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 20392974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 20402974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 20412974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 20422974e916SYuri Benditovich chain->stat.no_match++; 20432974e916SYuri Benditovich return RSC_NO_MATCH; 20442974e916SYuri Benditovich } 20452974e916SYuri Benditovich 20462974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 20472974e916SYuri Benditovich } 20482974e916SYuri Benditovich 20492974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 20502974e916SYuri Benditovich * to prevent out of order */ 20512974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 20522974e916SYuri Benditovich struct tcp_header *tcp) 20532974e916SYuri Benditovich { 20542974e916SYuri Benditovich uint16_t tcp_hdr; 20552974e916SYuri Benditovich uint16_t tcp_flag; 20562974e916SYuri Benditovich 20572974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 20582974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 20592974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 20602974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 20612974e916SYuri Benditovich chain->stat.tcp_syn++; 20622974e916SYuri Benditovich return RSC_BYPASS; 20632974e916SYuri Benditovich } 20642974e916SYuri Benditovich 20652974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 20662974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 20672974e916SYuri Benditovich return RSC_FINAL; 20682974e916SYuri Benditovich } 20692974e916SYuri Benditovich 20702974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 20712974e916SYuri Benditovich chain->stat.tcp_all_opt++; 20722974e916SYuri Benditovich return RSC_FINAL; 20732974e916SYuri Benditovich } 20742974e916SYuri Benditovich 20752974e916SYuri Benditovich return RSC_CANDIDATE; 20762974e916SYuri Benditovich } 20772974e916SYuri Benditovich 20782974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 20792974e916SYuri Benditovich NetClientState *nc, 20802974e916SYuri Benditovich const uint8_t *buf, size_t size, 20812974e916SYuri Benditovich VirtioNetRscUnit *unit) 20822974e916SYuri Benditovich { 20832974e916SYuri Benditovich int ret; 20842974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 20852974e916SYuri Benditovich 20862974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 20872974e916SYuri Benditovich chain->stat.empty_cache++; 20882974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 20892974e916SYuri Benditovich timer_mod(chain->drain_timer, 20902974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 20912974e916SYuri Benditovich return size; 20922974e916SYuri Benditovich } 20932974e916SYuri Benditovich 20942974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 20952974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 20962974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 20972974e916SYuri Benditovich } else { 20982974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 20992974e916SYuri Benditovich } 21002974e916SYuri Benditovich 21012974e916SYuri Benditovich if (ret == RSC_FINAL) { 21022974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 21032974e916SYuri Benditovich /* Send failed */ 21042974e916SYuri Benditovich chain->stat.final_failed++; 21052974e916SYuri Benditovich return 0; 21062974e916SYuri Benditovich } 21072974e916SYuri Benditovich 21082974e916SYuri Benditovich /* Send current packet */ 21092974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 21102974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 21112974e916SYuri Benditovich continue; 21122974e916SYuri Benditovich } else { 21132974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 21142974e916SYuri Benditovich seg->is_coalesced = 1; 21152974e916SYuri Benditovich return size; 21162974e916SYuri Benditovich } 21172974e916SYuri Benditovich } 21182974e916SYuri Benditovich 21192974e916SYuri Benditovich chain->stat.no_match_cache++; 21202974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 21212974e916SYuri Benditovich return size; 21222974e916SYuri Benditovich } 21232974e916SYuri Benditovich 21242974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 21252974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 21262974e916SYuri Benditovich NetClientState *nc, 21272974e916SYuri Benditovich const uint8_t *buf, size_t size, 21282974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 21292974e916SYuri Benditovich uint16_t tcp_port) 21302974e916SYuri Benditovich { 21312974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 21322974e916SYuri Benditovich uint32_t ppair1, ppair2; 21332974e916SYuri Benditovich 21342974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 21352974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 21362974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 21372974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 21382974e916SYuri Benditovich || (ppair1 != ppair2)) { 21392974e916SYuri Benditovich continue; 21402974e916SYuri Benditovich } 21412974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 21422974e916SYuri Benditovich chain->stat.drain_failed++; 21432974e916SYuri Benditovich } 21442974e916SYuri Benditovich 21452974e916SYuri Benditovich break; 21462974e916SYuri Benditovich } 21472974e916SYuri Benditovich 21482974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 21492974e916SYuri Benditovich } 21502974e916SYuri Benditovich 21512974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 21522974e916SYuri Benditovich struct ip_header *ip, 21532974e916SYuri Benditovich const uint8_t *buf, size_t size) 21542974e916SYuri Benditovich { 21552974e916SYuri Benditovich uint16_t ip_len; 21562974e916SYuri Benditovich 21572974e916SYuri Benditovich /* Not an ipv4 packet */ 21582974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 21592974e916SYuri Benditovich chain->stat.ip_option++; 21602974e916SYuri Benditovich return RSC_BYPASS; 21612974e916SYuri Benditovich } 21622974e916SYuri Benditovich 21632974e916SYuri Benditovich /* Don't handle packets with ip option */ 21642974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 21652974e916SYuri Benditovich chain->stat.ip_option++; 21662974e916SYuri Benditovich return RSC_BYPASS; 21672974e916SYuri Benditovich } 21682974e916SYuri Benditovich 21692974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 21702974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 21712974e916SYuri Benditovich return RSC_BYPASS; 21722974e916SYuri Benditovich } 21732974e916SYuri Benditovich 21742974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 21752974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 21762974e916SYuri Benditovich chain->stat.ip_frag++; 21772974e916SYuri Benditovich return RSC_BYPASS; 21782974e916SYuri Benditovich } 21792974e916SYuri Benditovich 21802974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 21812974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 21822974e916SYuri Benditovich chain->stat.ip_ecn++; 21832974e916SYuri Benditovich return RSC_BYPASS; 21842974e916SYuri Benditovich } 21852974e916SYuri Benditovich 21862974e916SYuri Benditovich ip_len = htons(ip->ip_len); 21872974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 21882974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 21892974e916SYuri Benditovich sizeof(struct eth_header))) { 21902974e916SYuri Benditovich chain->stat.ip_hacked++; 21912974e916SYuri Benditovich return RSC_BYPASS; 21922974e916SYuri Benditovich } 21932974e916SYuri Benditovich 21942974e916SYuri Benditovich return RSC_CANDIDATE; 21952974e916SYuri Benditovich } 21962974e916SYuri Benditovich 21972974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 21982974e916SYuri Benditovich NetClientState *nc, 21992974e916SYuri Benditovich const uint8_t *buf, size_t size) 22002974e916SYuri Benditovich { 22012974e916SYuri Benditovich int32_t ret; 22022974e916SYuri Benditovich uint16_t hdr_len; 22032974e916SYuri Benditovich VirtioNetRscUnit unit; 22042974e916SYuri Benditovich 22052974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 22062974e916SYuri Benditovich 22072974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 22082974e916SYuri Benditovich + sizeof(struct tcp_header))) { 22092974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 22102974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22112974e916SYuri Benditovich } 22122974e916SYuri Benditovich 22132974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 22142974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 22152974e916SYuri Benditovich != RSC_CANDIDATE) { 22162974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22172974e916SYuri Benditovich } 22182974e916SYuri Benditovich 22192974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 22202974e916SYuri Benditovich if (ret == RSC_BYPASS) { 22212974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22222974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 22232974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 22242974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 22252974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 22262974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 22272974e916SYuri Benditovich } 22282974e916SYuri Benditovich 22292974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 22302974e916SYuri Benditovich } 22312974e916SYuri Benditovich 22322974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 22332974e916SYuri Benditovich struct ip6_header *ip6, 22342974e916SYuri Benditovich const uint8_t *buf, size_t size) 22352974e916SYuri Benditovich { 22362974e916SYuri Benditovich uint16_t ip_len; 22372974e916SYuri Benditovich 22382974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 22392974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 22402974e916SYuri Benditovich return RSC_BYPASS; 22412974e916SYuri Benditovich } 22422974e916SYuri Benditovich 22432974e916SYuri Benditovich /* Both option and protocol is checked in this */ 22442974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 22452974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 22462974e916SYuri Benditovich return RSC_BYPASS; 22472974e916SYuri Benditovich } 22482974e916SYuri Benditovich 22492974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 22502974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 22512974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 22522974e916SYuri Benditovich - sizeof(struct ip6_header))) { 22532974e916SYuri Benditovich chain->stat.ip_hacked++; 22542974e916SYuri Benditovich return RSC_BYPASS; 22552974e916SYuri Benditovich } 22562974e916SYuri Benditovich 22572974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 22582974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 22592974e916SYuri Benditovich chain->stat.ip_ecn++; 22602974e916SYuri Benditovich return RSC_BYPASS; 22612974e916SYuri Benditovich } 22622974e916SYuri Benditovich 22632974e916SYuri Benditovich return RSC_CANDIDATE; 22642974e916SYuri Benditovich } 22652974e916SYuri Benditovich 22662974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 22672974e916SYuri Benditovich const uint8_t *buf, size_t size) 22682974e916SYuri Benditovich { 22692974e916SYuri Benditovich int32_t ret; 22702974e916SYuri Benditovich uint16_t hdr_len; 22712974e916SYuri Benditovich VirtioNetRscChain *chain; 22722974e916SYuri Benditovich VirtioNetRscUnit unit; 22732974e916SYuri Benditovich 22742974e916SYuri Benditovich chain = (VirtioNetRscChain *)opq; 22752974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 22762974e916SYuri Benditovich 22772974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 22782974e916SYuri Benditovich + sizeof(tcp_header))) { 22792974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22802974e916SYuri Benditovich } 22812974e916SYuri Benditovich 22822974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 22832974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 22842974e916SYuri Benditovich unit.ip, buf, size)) { 22852974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22862974e916SYuri Benditovich } 22872974e916SYuri Benditovich 22882974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 22892974e916SYuri Benditovich if (ret == RSC_BYPASS) { 22902974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22912974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 22922974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 22932974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 22942974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 22952974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 22962974e916SYuri Benditovich + sizeof(struct ip6_header)); 22972974e916SYuri Benditovich } 22982974e916SYuri Benditovich 22992974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 23002974e916SYuri Benditovich } 23012974e916SYuri Benditovich 23022974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 23032974e916SYuri Benditovich NetClientState *nc, 23042974e916SYuri Benditovich uint16_t proto) 23052974e916SYuri Benditovich { 23062974e916SYuri Benditovich VirtioNetRscChain *chain; 23072974e916SYuri Benditovich 23082974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 23092974e916SYuri Benditovich return NULL; 23102974e916SYuri Benditovich } 23112974e916SYuri Benditovich 23122974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 23132974e916SYuri Benditovich if (chain->proto == proto) { 23142974e916SYuri Benditovich return chain; 23152974e916SYuri Benditovich } 23162974e916SYuri Benditovich } 23172974e916SYuri Benditovich 23182974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 23192974e916SYuri Benditovich chain->n = n; 23202974e916SYuri Benditovich chain->proto = proto; 23212974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 23222974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 23232974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 23242974e916SYuri Benditovich } else { 23252974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 23262974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 23272974e916SYuri Benditovich } 23282974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 23292974e916SYuri Benditovich virtio_net_rsc_purge, chain); 23302974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 23312974e916SYuri Benditovich 23322974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 23332974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 23342974e916SYuri Benditovich 23352974e916SYuri Benditovich return chain; 23362974e916SYuri Benditovich } 23372974e916SYuri Benditovich 23382974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 23392974e916SYuri Benditovich const uint8_t *buf, 23402974e916SYuri Benditovich size_t size) 23412974e916SYuri Benditovich { 23422974e916SYuri Benditovich uint16_t proto; 23432974e916SYuri Benditovich VirtioNetRscChain *chain; 23442974e916SYuri Benditovich struct eth_header *eth; 23452974e916SYuri Benditovich VirtIONet *n; 23462974e916SYuri Benditovich 23472974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 23482974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 23492974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23502974e916SYuri Benditovich } 23512974e916SYuri Benditovich 23522974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 23532974e916SYuri Benditovich proto = htons(eth->h_proto); 23542974e916SYuri Benditovich 23552974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 23562974e916SYuri Benditovich if (chain) { 23572974e916SYuri Benditovich chain->stat.received++; 23582974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 23592974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 23602974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 23612974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 23622974e916SYuri Benditovich } 23632974e916SYuri Benditovich } 23642974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23652974e916SYuri Benditovich } 23662974e916SYuri Benditovich 23672974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 23682974e916SYuri Benditovich size_t size) 23692974e916SYuri Benditovich { 23702974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 23712974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 23722974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 23732974e916SYuri Benditovich } else { 23742974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23752974e916SYuri Benditovich } 23762974e916SYuri Benditovich } 23772974e916SYuri Benditovich 23786e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 23796e790746SPaolo Bonzini 23806e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 23816e790746SPaolo Bonzini { 23826e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 23836e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 238417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 23856e790746SPaolo Bonzini 238651b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 238717a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 23886e790746SPaolo Bonzini 238951b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 239051b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 23916e790746SPaolo Bonzini 23926e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 23936e790746SPaolo Bonzini virtio_net_flush_tx(q); 23946e790746SPaolo Bonzini } 23956e790746SPaolo Bonzini 23966e790746SPaolo Bonzini /* TX */ 23976e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 23986e790746SPaolo Bonzini { 23996e790746SPaolo Bonzini VirtIONet *n = q->n; 240017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 240151b19ebeSPaolo Bonzini VirtQueueElement *elem; 24026e790746SPaolo Bonzini int32_t num_packets = 0; 24036e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 240417a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 24056e790746SPaolo Bonzini return num_packets; 24066e790746SPaolo Bonzini } 24076e790746SPaolo Bonzini 240851b19ebeSPaolo Bonzini if (q->async_tx.elem) { 24096e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 24106e790746SPaolo Bonzini return num_packets; 24116e790746SPaolo Bonzini } 24126e790746SPaolo Bonzini 241351b19ebeSPaolo Bonzini for (;;) { 2414bd89dd98SJason Wang ssize_t ret; 241551b19ebeSPaolo Bonzini unsigned int out_num; 241651b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2417feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 24186e790746SPaolo Bonzini 241951b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 242051b19ebeSPaolo Bonzini if (!elem) { 242151b19ebeSPaolo Bonzini break; 242251b19ebeSPaolo Bonzini } 242351b19ebeSPaolo Bonzini 242451b19ebeSPaolo Bonzini out_num = elem->out_num; 242551b19ebeSPaolo Bonzini out_sg = elem->out_sg; 24266e790746SPaolo Bonzini if (out_num < 1) { 2427fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2428fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2429fa5e56c2SGreg Kurz g_free(elem); 2430fa5e56c2SGreg Kurz return -EINVAL; 24316e790746SPaolo Bonzini } 24326e790746SPaolo Bonzini 2433032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 2434feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 2435feb93f36SJason Wang n->guest_hdr_len) { 2436fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2437fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2438fa5e56c2SGreg Kurz g_free(elem); 2439fa5e56c2SGreg Kurz return -EINVAL; 2440032a74a1SCédric Le Goater } 24411bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2442feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 2443feb93f36SJason Wang sg2[0].iov_base = &mhdr; 2444feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2445feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2446feb93f36SJason Wang out_sg, out_num, 2447feb93f36SJason Wang n->guest_hdr_len, -1); 2448feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2449feb93f36SJason Wang goto drop; 2450032a74a1SCédric Le Goater } 2451feb93f36SJason Wang out_num += 1; 2452feb93f36SJason Wang out_sg = sg2; 2453feb93f36SJason Wang } 2454feb93f36SJason Wang } 24556e790746SPaolo Bonzini /* 24566e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 24576e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 24586e790746SPaolo Bonzini * that host is interested in. 24596e790746SPaolo Bonzini */ 24606e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 24616e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 24626e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 24636e790746SPaolo Bonzini out_sg, out_num, 24646e790746SPaolo Bonzini 0, n->host_hdr_len); 24656e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 24666e790746SPaolo Bonzini out_sg, out_num, 24676e790746SPaolo Bonzini n->guest_hdr_len, -1); 24686e790746SPaolo Bonzini out_num = sg_num; 24696e790746SPaolo Bonzini out_sg = sg; 24706e790746SPaolo Bonzini } 24716e790746SPaolo Bonzini 24726e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 24736e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 24746e790746SPaolo Bonzini if (ret == 0) { 24756e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 24766e790746SPaolo Bonzini q->async_tx.elem = elem; 24776e790746SPaolo Bonzini return -EBUSY; 24786e790746SPaolo Bonzini } 24796e790746SPaolo Bonzini 2480feb93f36SJason Wang drop: 248151b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 248217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 248351b19ebeSPaolo Bonzini g_free(elem); 24846e790746SPaolo Bonzini 24856e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 24866e790746SPaolo Bonzini break; 24876e790746SPaolo Bonzini } 24886e790746SPaolo Bonzini } 24896e790746SPaolo Bonzini return num_packets; 24906e790746SPaolo Bonzini } 24916e790746SPaolo Bonzini 24926e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 24936e790746SPaolo Bonzini { 249417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 24956e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 24966e790746SPaolo Bonzini 2497283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2498283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2499283e2c2aSYuri Benditovich return; 2500283e2c2aSYuri Benditovich } 2501283e2c2aSYuri Benditovich 25026e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 250317a0ca55SKONRAD Frederic if (!vdev->vm_running) { 25046e790746SPaolo Bonzini q->tx_waiting = 1; 25056e790746SPaolo Bonzini return; 25066e790746SPaolo Bonzini } 25076e790746SPaolo Bonzini 25086e790746SPaolo Bonzini if (q->tx_waiting) { 25096e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 2510bc72ad67SAlex Bligh timer_del(q->tx_timer); 25116e790746SPaolo Bonzini q->tx_waiting = 0; 2512fa5e56c2SGreg Kurz if (virtio_net_flush_tx(q) == -EINVAL) { 2513fa5e56c2SGreg Kurz return; 2514fa5e56c2SGreg Kurz } 25156e790746SPaolo Bonzini } else { 2516bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2517bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 25186e790746SPaolo Bonzini q->tx_waiting = 1; 25196e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 25206e790746SPaolo Bonzini } 25216e790746SPaolo Bonzini } 25226e790746SPaolo Bonzini 25236e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 25246e790746SPaolo Bonzini { 252517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 25266e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 25276e790746SPaolo Bonzini 2528283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2529283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2530283e2c2aSYuri Benditovich return; 2531283e2c2aSYuri Benditovich } 2532283e2c2aSYuri Benditovich 25336e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 25346e790746SPaolo Bonzini return; 25356e790746SPaolo Bonzini } 25366e790746SPaolo Bonzini q->tx_waiting = 1; 25376e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 253817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 25396e790746SPaolo Bonzini return; 25406e790746SPaolo Bonzini } 25416e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 25426e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 25436e790746SPaolo Bonzini } 25446e790746SPaolo Bonzini 25456e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 25466e790746SPaolo Bonzini { 25476e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 25486e790746SPaolo Bonzini VirtIONet *n = q->n; 254917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2550e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2551e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2552e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2553e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2554e8bcf842SMichael S. Tsirkin return; 2555e8bcf842SMichael S. Tsirkin } 25566e790746SPaolo Bonzini 25576e790746SPaolo Bonzini q->tx_waiting = 0; 25586e790746SPaolo Bonzini 25596e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 256017a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 25616e790746SPaolo Bonzini return; 256217a0ca55SKONRAD Frederic } 25636e790746SPaolo Bonzini 25646e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 25656e790746SPaolo Bonzini virtio_net_flush_tx(q); 25666e790746SPaolo Bonzini } 25676e790746SPaolo Bonzini 25686e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 25696e790746SPaolo Bonzini { 25706e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 25716e790746SPaolo Bonzini VirtIONet *n = q->n; 257217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 25736e790746SPaolo Bonzini int32_t ret; 25746e790746SPaolo Bonzini 2575e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2576e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2577e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2578e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2579e8bcf842SMichael S. Tsirkin return; 2580e8bcf842SMichael S. Tsirkin } 25816e790746SPaolo Bonzini 25826e790746SPaolo Bonzini q->tx_waiting = 0; 25836e790746SPaolo Bonzini 25846e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 258517a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 25866e790746SPaolo Bonzini return; 258717a0ca55SKONRAD Frederic } 25886e790746SPaolo Bonzini 25896e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2590fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2591fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2592fa5e56c2SGreg Kurz * broken */ 25936e790746SPaolo Bonzini } 25946e790746SPaolo Bonzini 25956e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 25966e790746SPaolo Bonzini * more coming and immediately reschedule */ 25976e790746SPaolo Bonzini if (ret >= n->tx_burst) { 25986e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 25996e790746SPaolo Bonzini q->tx_waiting = 1; 26006e790746SPaolo Bonzini return; 26016e790746SPaolo Bonzini } 26026e790746SPaolo Bonzini 26036e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 26046e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 26056e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 26066e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2607fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2608fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2609fa5e56c2SGreg Kurz return; 2610fa5e56c2SGreg Kurz } else if (ret > 0) { 26116e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26126e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 26136e790746SPaolo Bonzini q->tx_waiting = 1; 26146e790746SPaolo Bonzini } 26156e790746SPaolo Bonzini } 26166e790746SPaolo Bonzini 2617f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2618f9d6dbf0SWen Congyang { 2619f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2620f9d6dbf0SWen Congyang 26211c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 26221c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 26239b02e161SWei Wang 2624f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2625f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 26269b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 26279b02e161SWei Wang virtio_net_handle_tx_timer); 2628f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2629f9d6dbf0SWen Congyang virtio_net_tx_timer, 2630f9d6dbf0SWen Congyang &n->vqs[index]); 2631f9d6dbf0SWen Congyang } else { 2632f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 26339b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 26349b02e161SWei Wang virtio_net_handle_tx_bh); 2635f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 2636f9d6dbf0SWen Congyang } 2637f9d6dbf0SWen Congyang 2638f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2639f9d6dbf0SWen Congyang n->vqs[index].n = n; 2640f9d6dbf0SWen Congyang } 2641f9d6dbf0SWen Congyang 2642f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2643f9d6dbf0SWen Congyang { 2644f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2645f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2646f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2647f9d6dbf0SWen Congyang 2648f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2649f9d6dbf0SWen Congyang 2650f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2651f9d6dbf0SWen Congyang if (q->tx_timer) { 2652f9d6dbf0SWen Congyang timer_del(q->tx_timer); 2653f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2654f989c30cSYunjian Wang q->tx_timer = NULL; 2655f9d6dbf0SWen Congyang } else { 2656f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2657f989c30cSYunjian Wang q->tx_bh = NULL; 2658f9d6dbf0SWen Congyang } 2659f989c30cSYunjian Wang q->tx_waiting = 0; 2660f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2661f9d6dbf0SWen Congyang } 2662f9d6dbf0SWen Congyang 2663f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues) 2664f9d6dbf0SWen Congyang { 2665f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2666f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2667f9d6dbf0SWen Congyang int new_num_queues = new_max_queues * 2 + 1; 2668f9d6dbf0SWen Congyang int i; 2669f9d6dbf0SWen Congyang 2670f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2671f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2672f9d6dbf0SWen Congyang 2673f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 2674f9d6dbf0SWen Congyang return; 2675f9d6dbf0SWen Congyang } 2676f9d6dbf0SWen Congyang 2677f9d6dbf0SWen Congyang /* 2678f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 2679f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 268020f86a75SYuval Shaia * and then we only enter one of the following two loops. 2681f9d6dbf0SWen Congyang */ 2682f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 2683f9d6dbf0SWen Congyang 2684f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 2685f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 2686f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 2687f9d6dbf0SWen Congyang } 2688f9d6dbf0SWen Congyang 2689f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 2690f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 2691f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 2692f9d6dbf0SWen Congyang } 2693f9d6dbf0SWen Congyang 2694f9d6dbf0SWen Congyang /* add ctrl_vq last */ 2695f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 2696f9d6dbf0SWen Congyang } 2697f9d6dbf0SWen Congyang 2698ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 26996e790746SPaolo Bonzini { 2700f9d6dbf0SWen Congyang int max = multiqueue ? n->max_queues : 1; 2701f9d6dbf0SWen Congyang 27026e790746SPaolo Bonzini n->multiqueue = multiqueue; 2703f9d6dbf0SWen Congyang virtio_net_change_num_queues(n, max); 27046e790746SPaolo Bonzini 27056e790746SPaolo Bonzini virtio_net_set_queues(n); 27066e790746SPaolo Bonzini } 27076e790746SPaolo Bonzini 2708982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 2709037dab2fSGreg Kurz { 2710982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 2711982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 2712037dab2fSGreg Kurz int i, link_down; 2713037dab2fSGreg Kurz 27149d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 2715982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 271695129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 2717e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 2718e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 2719e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 27206e790746SPaolo Bonzini 27216e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 2722982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 27236e790746SPaolo Bonzini n->mac_table.in_use = 0; 27246e790746SPaolo Bonzini } 27256e790746SPaolo Bonzini 2726982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 27276c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 27286c666823SMichael S. Tsirkin } 27296c666823SMichael S. Tsirkin 27307788c3f2SMikhail Sennikovsky /* 27317788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 27327788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 27337788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 27347788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 27357788c3f2SMikhail Sennikovsky */ 27367788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 27376c666823SMichael S. Tsirkin 27386e790746SPaolo Bonzini virtio_net_set_queues(n); 27396e790746SPaolo Bonzini 27406e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 27416e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 27426e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 27436e790746SPaolo Bonzini break; 27446e790746SPaolo Bonzini } 27456e790746SPaolo Bonzini } 27466e790746SPaolo Bonzini n->mac_table.first_multi = i; 27476e790746SPaolo Bonzini 27486e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 27496e790746SPaolo Bonzini * to link status bit in n->status */ 27506e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 27516e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 27526e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 27536e790746SPaolo Bonzini } 27546e790746SPaolo Bonzini 27556c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 27566c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 27579d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 27589d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 27599d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 27609d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 27619d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 27629d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 27639d8c6a25SDr. David Alan Gilbert } else { 2764944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 27659d8c6a25SDr. David Alan Gilbert } 27666c666823SMichael S. Tsirkin } 27676c666823SMichael S. Tsirkin 2768e41b7114SYuri Benditovich if (n->rss_data.enabled) { 2769e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 2770e41b7114SYuri Benditovich n->rss_data.indirections_len, 2771e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 2772e41b7114SYuri Benditovich } else { 2773e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 2774e41b7114SYuri Benditovich } 27756e790746SPaolo Bonzini return 0; 27766e790746SPaolo Bonzini } 27776e790746SPaolo Bonzini 27787788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 27797788c3f2SMikhail Sennikovsky { 27807788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 27817788c3f2SMikhail Sennikovsky /* 27827788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 27837788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 27847788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 27857788c3f2SMikhail Sennikovsky */ 27867788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 27877788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 27887788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 27897788c3f2SMikhail Sennikovsky } 27907788c3f2SMikhail Sennikovsky 27917788c3f2SMikhail Sennikovsky return 0; 27927788c3f2SMikhail Sennikovsky } 27937788c3f2SMikhail Sennikovsky 2794982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 2795982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 2796982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 2797982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2798982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 2799982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2800982b78c5SDr. David Alan Gilbert }, 2801982b78c5SDr. David Alan Gilbert }; 2802982b78c5SDr. David Alan Gilbert 2803982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id) 2804982b78c5SDr. David Alan Gilbert { 2805982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->max_queues > 1; 2806982b78c5SDr. David Alan Gilbert } 2807982b78c5SDr. David Alan Gilbert 2808982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 2809982b78c5SDr. David Alan Gilbert { 2810982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 2811982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 2812982b78c5SDr. David Alan Gilbert } 2813982b78c5SDr. David Alan Gilbert 2814982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 2815982b78c5SDr. David Alan Gilbert { 2816982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 2817982b78c5SDr. David Alan Gilbert } 2818982b78c5SDr. David Alan Gilbert 2819982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 2820982b78c5SDr. David Alan Gilbert { 2821982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 2822982b78c5SDr. David Alan Gilbert } 2823982b78c5SDr. David Alan Gilbert 2824982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 2825982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 2826982b78c5SDr. David Alan Gilbert */ 2827982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 2828982b78c5SDr. David Alan Gilbert VirtIONet *parent; 2829982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 2830982b78c5SDr. David Alan Gilbert uint16_t curr_queues_1; 2831982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 2832982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 2833982b78c5SDr. David Alan Gilbert }; 2834982b78c5SDr. David Alan Gilbert 2835982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 2836982b78c5SDr. David Alan Gilbert * the 1st entry in the queues and only if there's more than one 2837982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 2838982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 2839982b78c5SDr. David Alan Gilbert */ 2840982b78c5SDr. David Alan Gilbert 284144b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 2842982b78c5SDr. David Alan Gilbert { 2843982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2844982b78c5SDr. David Alan Gilbert 2845982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 2846982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = tmp->parent->curr_queues - 1; 2847982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues == 0) { 2848982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = 0; 2849982b78c5SDr. David Alan Gilbert } 285044b1ff31SDr. David Alan Gilbert 285144b1ff31SDr. David Alan Gilbert return 0; 2852982b78c5SDr. David Alan Gilbert } 2853982b78c5SDr. David Alan Gilbert 2854982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 2855982b78c5SDr. David Alan Gilbert { 2856982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2857982b78c5SDr. David Alan Gilbert 2858982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 2859982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 2860982b78c5SDr. David Alan Gilbert 2861982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues > tmp->parent->max_queues) { 2862982b78c5SDr. David Alan Gilbert error_report("virtio-net: curr_queues %x > max_queues %x", 2863982b78c5SDr. David Alan Gilbert tmp->parent->curr_queues, tmp->parent->max_queues); 2864982b78c5SDr. David Alan Gilbert 2865982b78c5SDr. David Alan Gilbert return -EINVAL; 2866982b78c5SDr. David Alan Gilbert } 2867982b78c5SDr. David Alan Gilbert 2868982b78c5SDr. David Alan Gilbert return 0; /* all good */ 2869982b78c5SDr. David Alan Gilbert } 2870982b78c5SDr. David Alan Gilbert 2871982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 2872982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 2873982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 2874982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 2875982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2876982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 2877982b78c5SDr. David Alan Gilbert curr_queues_1, 2878982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 2879982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 2880982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2881982b78c5SDr. David Alan Gilbert }, 2882982b78c5SDr. David Alan Gilbert }; 2883982b78c5SDr. David Alan Gilbert 2884982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 2885982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 2886982b78c5SDr. David Alan Gilbert */ 2887982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 2888982b78c5SDr. David Alan Gilbert { 2889982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2890982b78c5SDr. David Alan Gilbert 2891982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 2892982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 2893982b78c5SDr. David Alan Gilbert return -EINVAL; 2894982b78c5SDr. David Alan Gilbert } 2895982b78c5SDr. David Alan Gilbert 2896982b78c5SDr. David Alan Gilbert return 0; 2897982b78c5SDr. David Alan Gilbert } 2898982b78c5SDr. David Alan Gilbert 289944b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 2900982b78c5SDr. David Alan Gilbert { 2901982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2902982b78c5SDr. David Alan Gilbert 2903982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 290444b1ff31SDr. David Alan Gilbert 290544b1ff31SDr. David Alan Gilbert return 0; 2906982b78c5SDr. David Alan Gilbert } 2907982b78c5SDr. David Alan Gilbert 2908982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 2909982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 2910982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 2911982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 2912982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2913982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 2914982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2915982b78c5SDr. David Alan Gilbert }, 2916982b78c5SDr. David Alan Gilbert }; 2917982b78c5SDr. David Alan Gilbert 2918982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 2919982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 2920982b78c5SDr. David Alan Gilbert */ 2921982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 2922982b78c5SDr. David Alan Gilbert { 2923982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2924982b78c5SDr. David Alan Gilbert 2925982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 2926982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 2927982b78c5SDr. David Alan Gilbert return -EINVAL; 2928982b78c5SDr. David Alan Gilbert } 2929982b78c5SDr. David Alan Gilbert 2930982b78c5SDr. David Alan Gilbert return 0; 2931982b78c5SDr. David Alan Gilbert } 2932982b78c5SDr. David Alan Gilbert 293344b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 2934982b78c5SDr. David Alan Gilbert { 2935982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2936982b78c5SDr. David Alan Gilbert 2937982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 293844b1ff31SDr. David Alan Gilbert 293944b1ff31SDr. David Alan Gilbert return 0; 2940982b78c5SDr. David Alan Gilbert } 2941982b78c5SDr. David Alan Gilbert 2942982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 2943982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 2944982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 2945982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 2946982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2947982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 2948982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2949982b78c5SDr. David Alan Gilbert }, 2950982b78c5SDr. David Alan Gilbert }; 2951982b78c5SDr. David Alan Gilbert 2952e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 2953e41b7114SYuri Benditovich { 2954e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 2955e41b7114SYuri Benditovich } 2956e41b7114SYuri Benditovich 2957e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 2958e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 2959e41b7114SYuri Benditovich .version_id = 1, 2960e41b7114SYuri Benditovich .minimum_version_id = 1, 2961e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 2962e41b7114SYuri Benditovich .fields = (VMStateField[]) { 2963e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 2964e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 2965e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 2966e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 2967e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 2968e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 2969e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 2970e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 2971e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 2972e41b7114SYuri Benditovich rss_data.indirections_len, 0, 2973e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 2974e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 2975e41b7114SYuri Benditovich }, 2976e41b7114SYuri Benditovich }; 2977e41b7114SYuri Benditovich 2978982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 2979982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 2980982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 2981982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 2982982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 2983982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2984982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 2985982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 2986982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 2987982b78c5SDr. David Alan Gilbert VirtIONetQueue), 2988982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 2989982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 2990982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 2991982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 2992982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 2993982b78c5SDr. David Alan Gilbert 2994982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 2995982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 2996982b78c5SDr. David Alan Gilbert * sets flags in this case. 2997982b78c5SDr. David Alan Gilbert */ 2998982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 2999982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3000982b78c5SDr. David Alan Gilbert ETH_ALEN), 3001982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3002982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3003982b78c5SDr. David Alan Gilbert 3004982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3005982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3006982b78c5SDr. David Alan Gilbert * but based on the uint. 3007982b78c5SDr. David Alan Gilbert */ 3008982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3009982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3010982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3011982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3012982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3013982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3014982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3015982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3016982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3017982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3018982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3019982b78c5SDr. David Alan Gilbert VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0, 3020982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3021982b78c5SDr. David Alan Gilbert VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1), 3022982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3023982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3024982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3025982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3026982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3027982b78c5SDr. David Alan Gilbert }, 3028e41b7114SYuri Benditovich .subsections = (const VMStateDescription * []) { 3029e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3030e41b7114SYuri Benditovich NULL 3031e41b7114SYuri Benditovich } 3032982b78c5SDr. David Alan Gilbert }; 3033982b78c5SDr. David Alan Gilbert 30346e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3035f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 30366e790746SPaolo Bonzini .size = sizeof(NICState), 30376e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 30386e790746SPaolo Bonzini .receive = virtio_net_receive, 30396e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3040b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3041b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 30426e790746SPaolo Bonzini }; 30436e790746SPaolo Bonzini 30446e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 30456e790746SPaolo Bonzini { 304617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 30476e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 30486e790746SPaolo Bonzini assert(n->vhost_started); 3049ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 30506e790746SPaolo Bonzini } 30516e790746SPaolo Bonzini 30526e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 30536e790746SPaolo Bonzini bool mask) 30546e790746SPaolo Bonzini { 305517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 30566e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 30576e790746SPaolo Bonzini assert(n->vhost_started); 3058ed8b4afeSNikolay Nikolaev vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 30596e790746SPaolo Bonzini vdev, idx, mask); 30606e790746SPaolo Bonzini } 30616e790746SPaolo Bonzini 3062019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 30636e790746SPaolo Bonzini { 30640cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3065a93e599dSMaxime Coquelin 3066ba550851SStefano Garzarella n->config_size = virtio_feature_get_config_size(feature_sizes, 3067ba550851SStefano Garzarella host_features); 306817ec5a86SKONRAD Frederic } 30696e790746SPaolo Bonzini 30708a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 30718a253ec2SKONRAD Frederic const char *type) 30728a253ec2SKONRAD Frederic { 30738a253ec2SKONRAD Frederic /* 30748a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 30758a253ec2SKONRAD Frederic */ 30768a253ec2SKONRAD Frederic assert(type != NULL); 30778a253ec2SKONRAD Frederic 30788a253ec2SKONRAD Frederic g_free(n->netclient_name); 30798a253ec2SKONRAD Frederic g_free(n->netclient_type); 30808a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 30818a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 30828a253ec2SKONRAD Frederic } 30838a253ec2SKONRAD Frederic 30849711cd0dSJens Freimann static bool failover_unplug_primary(VirtIONet *n) 30859711cd0dSJens Freimann { 30869711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 30879711cd0dSJens Freimann PCIDevice *pci_dev; 30889711cd0dSJens Freimann Error *err = NULL; 30899711cd0dSJens Freimann 30909711cd0dSJens Freimann hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev); 30919711cd0dSJens Freimann if (hotplug_ctrl) { 30929711cd0dSJens Freimann pci_dev = PCI_DEVICE(n->primary_dev); 30939711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 30949711cd0dSJens Freimann hotplug_handler_unplug_request(hotplug_ctrl, n->primary_dev, &err); 30959711cd0dSJens Freimann if (err) { 30969711cd0dSJens Freimann error_report_err(err); 30979711cd0dSJens Freimann return false; 30989711cd0dSJens Freimann } 30999711cd0dSJens Freimann } else { 31009711cd0dSJens Freimann return false; 31019711cd0dSJens Freimann } 31029711cd0dSJens Freimann return true; 31039711cd0dSJens Freimann } 31049711cd0dSJens Freimann 31059711cd0dSJens Freimann static bool failover_replug_primary(VirtIONet *n, Error **errp) 31069711cd0dSJens Freimann { 31075a0948d3SMarkus Armbruster Error *err = NULL; 31089711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 31099711cd0dSJens Freimann PCIDevice *pdev = PCI_DEVICE(n->primary_dev); 311078274682SJuan Quintela BusState *primary_bus; 31119711cd0dSJens Freimann 31129711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 31139711cd0dSJens Freimann return true; 31149711cd0dSJens Freimann } 311578274682SJuan Quintela primary_bus = n->primary_dev->parent_bus; 311678274682SJuan Quintela if (!primary_bus) { 3117150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 31185a0948d3SMarkus Armbruster return false; 31199711cd0dSJens Freimann } 312078274682SJuan Quintela qdev_set_parent_bus(n->primary_dev, primary_bus, &error_abort); 3121e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 31229711cd0dSJens Freimann hotplug_ctrl = qdev_get_hotplug_handler(n->primary_dev); 31239711cd0dSJens Freimann if (hotplug_ctrl) { 31245a0948d3SMarkus Armbruster hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, &err); 31255a0948d3SMarkus Armbruster if (err) { 31265a0948d3SMarkus Armbruster goto out; 31275a0948d3SMarkus Armbruster } 3128ca72efccSMarkus Armbruster hotplug_handler_plug(hotplug_ctrl, n->primary_dev, &err); 31299711cd0dSJens Freimann } 3130150ab54aSJens Freimann 3131150ab54aSJens Freimann out: 31325a0948d3SMarkus Armbruster error_propagate(errp, err); 31335a0948d3SMarkus Armbruster return !err; 31349711cd0dSJens Freimann } 31359711cd0dSJens Freimann 31369711cd0dSJens Freimann static void virtio_net_handle_migration_primary(VirtIONet *n, 31379711cd0dSJens Freimann MigrationState *s) 31389711cd0dSJens Freimann { 31399711cd0dSJens Freimann bool should_be_hidden; 31409711cd0dSJens Freimann Error *err = NULL; 31419711cd0dSJens Freimann 3142e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 31439711cd0dSJens Freimann 31449711cd0dSJens Freimann if (!n->primary_dev) { 314582ceb657SJuan Quintela n->primary_dev = virtio_connect_failover_devices(n, &err); 31469711cd0dSJens Freimann if (!n->primary_dev) { 31479711cd0dSJens Freimann return; 31489711cd0dSJens Freimann } 31499711cd0dSJens Freimann } 31509711cd0dSJens Freimann 31514dbac1aeSMarkus Armbruster if (migration_in_setup(s) && !should_be_hidden) { 31529711cd0dSJens Freimann if (failover_unplug_primary(n)) { 31533cad405bSMarc-André Lureau vmstate_unregister(VMSTATE_IF(n->primary_dev), 31543cad405bSMarc-André Lureau qdev_get_vmsd(n->primary_dev), 31559711cd0dSJens Freimann n->primary_dev); 31569711cd0dSJens Freimann qapi_event_send_unplug_primary(n->primary_device_id); 3157e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 31589711cd0dSJens Freimann } else { 31599711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 31609711cd0dSJens Freimann } 31619711cd0dSJens Freimann } else if (migration_has_failed(s)) { 3162150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 31639711cd0dSJens Freimann if (!failover_replug_primary(n, &err)) { 31649711cd0dSJens Freimann if (err) { 31659711cd0dSJens Freimann error_report_err(err); 31669711cd0dSJens Freimann } 31679711cd0dSJens Freimann } 31689711cd0dSJens Freimann } 31699711cd0dSJens Freimann } 31709711cd0dSJens Freimann 31719711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) 31729711cd0dSJens Freimann { 31739711cd0dSJens Freimann MigrationState *s = data; 31749711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 31759711cd0dSJens Freimann virtio_net_handle_migration_primary(n, s); 31769711cd0dSJens Freimann } 31779711cd0dSJens Freimann 31789711cd0dSJens Freimann static int virtio_net_primary_should_be_hidden(DeviceListener *listener, 31799711cd0dSJens Freimann QemuOpts *device_opts) 31809711cd0dSJens Freimann { 31819711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 31824d0e59acSJens Freimann bool match_found = false; 31834d0e59acSJens Freimann bool hide = false; 3184*4f0303aeSJuan Quintela const char *standby_id; 31859711cd0dSJens Freimann 31864d0e59acSJens Freimann if (!device_opts) { 31874d0e59acSJens Freimann return -1; 31884d0e59acSJens Freimann } 31899711cd0dSJens Freimann n->primary_device_dict = qemu_opts_to_qdict(device_opts, 31909711cd0dSJens Freimann n->primary_device_dict); 3191*4f0303aeSJuan Quintela standby_id = qemu_opt_get(device_opts, "failover_pair_id"); 3192*4f0303aeSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) == 0) { 31939711cd0dSJens Freimann match_found = true; 31949711cd0dSJens Freimann } else { 31959711cd0dSJens Freimann match_found = false; 31969711cd0dSJens Freimann hide = false; 31979711cd0dSJens Freimann n->primary_device_dict = NULL; 31989711cd0dSJens Freimann goto out; 31999711cd0dSJens Freimann } 32009711cd0dSJens Freimann 3201e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 3202e2bde83eSJuan Quintela hide = qatomic_read(&n->failover_primary_hidden); 32039711cd0dSJens Freimann 32049711cd0dSJens Freimann if (n->primary_device_dict) { 32059711cd0dSJens Freimann g_free(n->primary_device_id); 32069711cd0dSJens Freimann n->primary_device_id = g_strdup(qdict_get_try_str( 32079711cd0dSJens Freimann n->primary_device_dict, "id")); 32089711cd0dSJens Freimann if (!n->primary_device_id) { 32099711cd0dSJens Freimann warn_report("primary_device_id not set"); 32109711cd0dSJens Freimann } 32119711cd0dSJens Freimann } 32129711cd0dSJens Freimann 32139711cd0dSJens Freimann out: 32149711cd0dSJens Freimann if (match_found && hide) { 32159711cd0dSJens Freimann return 1; 32169711cd0dSJens Freimann } else if (match_found && !hide) { 32179711cd0dSJens Freimann return 0; 32189711cd0dSJens Freimann } else { 32199711cd0dSJens Freimann return -1; 32209711cd0dSJens Freimann } 32219711cd0dSJens Freimann } 32229711cd0dSJens Freimann 3223e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 322417ec5a86SKONRAD Frederic { 3225e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3226284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3227284a32f0SAndreas Färber NetClientState *nc; 32281773d9eeSKONRAD Frederic int i; 322917ec5a86SKONRAD Frederic 3230a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3231127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3232a93e599dSMaxime Coquelin } 3233a93e599dSMaxime Coquelin 32349473939eSJason Baron if (n->net_conf.duplex_str) { 32359473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 32369473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 32379473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 32389473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 32399473939eSJason Baron } else { 32409473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3241843c4cfcSMarkus Armbruster return; 32429473939eSJason Baron } 32439473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 32449473939eSJason Baron } else { 32459473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 32469473939eSJason Baron } 32479473939eSJason Baron 32489473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 32499473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3250843c4cfcSMarkus Armbruster return; 3251843c4cfcSMarkus Armbruster } 3252843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 32539473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 32549473939eSJason Baron } 32559473939eSJason Baron 32569711cd0dSJens Freimann if (n->failover) { 32579711cd0dSJens Freimann n->primary_listener.should_be_hidden = 32589711cd0dSJens Freimann virtio_net_primary_should_be_hidden; 3259e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 32609711cd0dSJens Freimann device_listener_register(&n->primary_listener); 32619711cd0dSJens Freimann n->migration_state.notify = virtio_net_migration_state_notifier; 32629711cd0dSJens Freimann add_migration_state_change_notifier(&n->migration_state); 32639711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 32649711cd0dSJens Freimann } 32659711cd0dSJens Freimann 3266da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 3267284a32f0SAndreas Färber virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); 326817ec5a86SKONRAD Frederic 32691c0fbfa3SMichael S. Tsirkin /* 32701c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 32711c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 32721c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 32731c0fbfa3SMichael S. Tsirkin */ 32741c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 32751c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 32765f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 32771c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 32781c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 32791c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 32801c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 32811c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 32821c0fbfa3SMichael S. Tsirkin return; 32831c0fbfa3SMichael S. Tsirkin } 32841c0fbfa3SMichael S. Tsirkin 32859b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 32869b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 32879b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 32889b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 32899b02e161SWei Wang "must be a power of 2 between %d and %d", 32909b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 32919b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 32929b02e161SWei Wang virtio_cleanup(vdev); 32939b02e161SWei Wang return; 32949b02e161SWei Wang } 32959b02e161SWei Wang 3296575a1c0eSJiri Pirko n->max_queues = MAX(n->nic_conf.peers.queues, 1); 329787b3bd1cSJason Wang if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) { 32987e0e736eSJason Wang error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 3299631b22eaSStefan Weil "must be a positive integer less than %d.", 330087b3bd1cSJason Wang n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2); 33017e0e736eSJason Wang virtio_cleanup(vdev); 33027e0e736eSJason Wang return; 33037e0e736eSJason Wang } 33046e790746SPaolo Bonzini n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); 33056e790746SPaolo Bonzini n->curr_queues = 1; 33061773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 33076e790746SPaolo Bonzini 33081773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 33091773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 33100765691eSMarkus Armbruster warn_report("virtio-net: " 33116e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 33121773d9eeSKONRAD Frederic n->net_conf.tx); 33130765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 33146e790746SPaolo Bonzini } 33156e790746SPaolo Bonzini 33162eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 33172eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 33189b02e161SWei Wang 3319da51a335SJason Wang for (i = 0; i < n->max_queues; i++) { 3320f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3321da51a335SJason Wang } 3322da51a335SJason Wang 332317a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 33241773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 33251773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 33266e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 33279d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 33289d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3329f57fcf70SJason Wang virtio_net_announce_timer, n); 3330b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 33316e790746SPaolo Bonzini 33328a253ec2SKONRAD Frederic if (n->netclient_type) { 33338a253ec2SKONRAD Frederic /* 33348a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 33358a253ec2SKONRAD Frederic */ 33368a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 33378a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 33388a253ec2SKONRAD Frederic } else { 33391773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 3340284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 33418a253ec2SKONRAD Frederic } 33428a253ec2SKONRAD Frederic 33436e790746SPaolo Bonzini peer_test_vnet_hdr(n); 33446e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 33456e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 3346d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 33476e790746SPaolo Bonzini } 33486e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 33496e790746SPaolo Bonzini } else { 33506e790746SPaolo Bonzini n->host_hdr_len = 0; 33516e790746SPaolo Bonzini } 33526e790746SPaolo Bonzini 33531773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 33546e790746SPaolo Bonzini 33556e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 33561773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3357e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 33586e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 33596e790746SPaolo Bonzini 33606e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 33616e790746SPaolo Bonzini 33626e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 33636e790746SPaolo Bonzini 3364b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3365b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3366b1be4280SAmos Kong 3367e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3368e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3369e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3370e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3371e87936eaSCindy Lu (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER); 3372e87936eaSCindy Lu } 33732974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3374284a32f0SAndreas Färber n->qdev = dev; 33754474e37aSYuri Benditovich 33764474e37aSYuri Benditovich net_rx_pkt_init(&n->rx_pkt, false); 337717ec5a86SKONRAD Frederic } 337817ec5a86SKONRAD Frederic 3379b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 338017ec5a86SKONRAD Frederic { 3381306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3382306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3383f9d6dbf0SWen Congyang int i, max_queues; 338417ec5a86SKONRAD Frederic 338517ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 338617ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 338717ec5a86SKONRAD Frederic 33888a253ec2SKONRAD Frederic g_free(n->netclient_name); 33898a253ec2SKONRAD Frederic n->netclient_name = NULL; 33908a253ec2SKONRAD Frederic g_free(n->netclient_type); 33918a253ec2SKONRAD Frederic n->netclient_type = NULL; 33928a253ec2SKONRAD Frederic 339317ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 339417ec5a86SKONRAD Frederic g_free(n->vlans); 339517ec5a86SKONRAD Frederic 33969711cd0dSJens Freimann if (n->failover) { 339765018100SJuan Quintela device_listener_unregister(&n->primary_listener); 33989711cd0dSJens Freimann g_free(n->primary_device_id); 33999711cd0dSJens Freimann qobject_unref(n->primary_device_dict); 34009711cd0dSJens Freimann n->primary_device_dict = NULL; 34019711cd0dSJens Freimann } 34029711cd0dSJens Freimann 3403f9d6dbf0SWen Congyang max_queues = n->multiqueue ? n->max_queues : 1; 3404f9d6dbf0SWen Congyang for (i = 0; i < max_queues; i++) { 3405f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 340617ec5a86SKONRAD Frederic } 3407d945d9f1SYuri Benditovich /* delete also control vq */ 3408d945d9f1SYuri Benditovich virtio_del_queue(vdev, max_queues * 2); 3409944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 341017ec5a86SKONRAD Frederic g_free(n->vqs); 341117ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 34122974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 341359079029SYuri Benditovich g_free(n->rss_data.indirections_table); 34144474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 34156a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 341617ec5a86SKONRAD Frederic } 341717ec5a86SKONRAD Frederic 341817ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 341917ec5a86SKONRAD Frederic { 342017ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 342117ec5a86SKONRAD Frederic 342217ec5a86SKONRAD Frederic /* 342317ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 342417ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 342517ec5a86SKONRAD Frederic */ 342617ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3427aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3428aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 342940c2281cSMarkus Armbruster DEVICE(n)); 343017ec5a86SKONRAD Frederic } 343117ec5a86SKONRAD Frederic 343244b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 34334d45dcfbSHalil Pasic { 34344d45dcfbSHalil Pasic VirtIONet *n = opaque; 34354d45dcfbSHalil Pasic 34364d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 34374d45dcfbSHalil Pasic * it might keep writing to memory. */ 34384d45dcfbSHalil Pasic assert(!n->vhost_started); 343944b1ff31SDr. David Alan Gilbert 344044b1ff31SDr. David Alan Gilbert return 0; 34414d45dcfbSHalil Pasic } 34424d45dcfbSHalil Pasic 34439711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 34449711cd0dSJens Freimann { 34459711cd0dSJens Freimann DeviceState *dev = opaque; 34469711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 34479711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 34489711cd0dSJens Freimann 3449284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3450284f42a5SJens Freimann return false; 3451284f42a5SJens Freimann } 34529711cd0dSJens Freimann return n->primary_dev ? n->primary_dev->pending_deleted_event : false; 34539711cd0dSJens Freimann } 34549711cd0dSJens Freimann 34559711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 34569711cd0dSJens Freimann { 34579711cd0dSJens Freimann DeviceState *dev = opaque; 34589711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 34599711cd0dSJens Freimann 34609711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 34619711cd0dSJens Freimann } 34629711cd0dSJens Freimann 34634d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 34644d45dcfbSHalil Pasic .name = "virtio-net", 34654d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 34664d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 34674d45dcfbSHalil Pasic .fields = (VMStateField[]) { 34684d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 34694d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 34704d45dcfbSHalil Pasic }, 34714d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 34729711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 34734d45dcfbSHalil Pasic }; 3474290c2428SDr. David Alan Gilbert 347517ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3476127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3477127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3478127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 347987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3480127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3481127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 348287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3483127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 348487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3485127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 348687108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3487127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 348887108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3489127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 349087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3491127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 349287108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3493127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 349487108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3495127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 349687108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3497127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 349887108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3499127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 350087108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3501127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 350287108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3503127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 350487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3505127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 350687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3507127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 350887108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3509127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 351087108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3511127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 351287108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3513127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 351487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3515127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 351659079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 351759079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3518e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3519e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 35202974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 35212974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 35222974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 35232974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 352417ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 352517ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 352617ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 352717ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 352817ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 35291c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 35301c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 35319b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 35329b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3533a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 353475ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 353575ebec11SMaxime Coquelin true), 35369473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 35379473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 35389711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 353917ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 354017ec5a86SKONRAD Frederic }; 354117ec5a86SKONRAD Frederic 354217ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 354317ec5a86SKONRAD Frederic { 354417ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 354517ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3546e6f746b3SAndreas Färber 35474f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3548290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3549125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3550e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3551306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 355217ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 355317ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 355417ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 355517ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 355617ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 355717ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 355817ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 355917ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 356017ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 35612a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 35627788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 3563982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 35649711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 356517ec5a86SKONRAD Frederic } 356617ec5a86SKONRAD Frederic 356717ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 356817ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 356917ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 357017ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 357117ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 357217ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 357317ec5a86SKONRAD Frederic }; 357417ec5a86SKONRAD Frederic 357517ec5a86SKONRAD Frederic static void virtio_register_types(void) 357617ec5a86SKONRAD Frederic { 357717ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 357817ec5a86SKONRAD Frederic } 357917ec5a86SKONRAD Frederic 358017ec5a86SKONRAD Frederic type_init(virtio_register_types) 3581