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" 47*1b529d90SLaurent Vivier #include "sysemu/qtest.h" 486e790746SPaolo Bonzini 496e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 506e790746SPaolo Bonzini 516e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 526e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 536e790746SPaolo Bonzini 541c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 551c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 569b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 579b02e161SWei Wang 58441537f1SJason Wang /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */ 591c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 609b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 611c0fbfa3SMichael S. Tsirkin 622974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ 632974e916SYuri Benditovich 642974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG 0x3F 652974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 662974e916SYuri Benditovich 672974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */ 682974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header)) 692974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535 702974e916SYuri Benditovich 712974e916SYuri Benditovich /* header length value in ip header without option */ 722974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5 732974e916SYuri Benditovich 742974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */ 752974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD 762974e916SYuri Benditovich 772974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance 782974e916SYuri Benditovich a lot, and should be tuned carefully, '300000'(300us) is the recommended 792974e916SYuri Benditovich value to pass the WHQL test, '50000' can gain 2x netperf throughput with 802974e916SYuri Benditovich tso/gso/gro 'off'. */ 812974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000 822974e916SYuri Benditovich 8359079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ 8459079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ 8559079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ 8659079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ 8759079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ 8859079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \ 8959079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \ 9059079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \ 9159079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) 9259079029SYuri Benditovich 93ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = { 94127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 955d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mac)}, 96127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 975d5b33c0SMax Reitz .end = endof(struct virtio_net_config, status)}, 98127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 995d5b33c0SMax Reitz .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 100127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 1015d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mtu)}, 1029473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 1035d5b33c0SMax Reitz .end = endof(struct virtio_net_config, duplex)}, 104e22f0603SYuri Benditovich {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT), 10559079029SYuri Benditovich .end = endof(struct virtio_net_config, supported_hash_types)}, 1066e790746SPaolo Bonzini {} 1076e790746SPaolo Bonzini }; 1086e790746SPaolo Bonzini 1096e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 1106e790746SPaolo Bonzini { 1116e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1126e790746SPaolo Bonzini 1136e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 1146e790746SPaolo Bonzini } 1156e790746SPaolo Bonzini 1166e790746SPaolo Bonzini static int vq2q(int queue_index) 1176e790746SPaolo Bonzini { 1186e790746SPaolo Bonzini return queue_index / 2; 1196e790746SPaolo Bonzini } 1206e790746SPaolo Bonzini 1216e790746SPaolo Bonzini /* TODO 1226e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 1236e790746SPaolo Bonzini */ 1246e790746SPaolo Bonzini 1256e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 1266e790746SPaolo Bonzini { 12717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1286e790746SPaolo Bonzini struct virtio_net_config netcfg; 129c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 130fb592882SCindy Lu static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } }; 1316e790746SPaolo Bonzini 132108a6481SCindy Lu int ret = 0; 133108a6481SCindy Lu memset(&netcfg, 0 , sizeof(struct virtio_net_config)); 1341399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 135441537f1SJason Wang virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs); 136a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 1376e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 1389473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 1399473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 14059079029SYuri Benditovich netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE; 14159079029SYuri Benditovich virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length, 142e22f0603SYuri Benditovich virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ? 143e22f0603SYuri Benditovich VIRTIO_NET_RSS_MAX_TABLE_LEN : 1); 14459079029SYuri Benditovich virtio_stl_p(vdev, &netcfg.supported_hash_types, 14559079029SYuri Benditovich VIRTIO_NET_RSS_SUPPORTED_HASHES); 1466e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 147108a6481SCindy Lu 148c546ecf2SJason Wang /* 149c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 150c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 151c546ecf2SJason Wang */ 152c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 153108a6481SCindy Lu ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, 154108a6481SCindy Lu n->config_size); 155108a6481SCindy Lu if (ret != -1) { 156fb592882SCindy Lu /* 157fb592882SCindy Lu * Some NIC/kernel combinations present 0 as the mac address. As 158fb592882SCindy Lu * that is not a legal address, try to proceed with the 159fb592882SCindy Lu * address from the QEMU command line in the hope that the 160fb592882SCindy Lu * address has been configured correctly elsewhere - just not 161fb592882SCindy Lu * reported by the device. 162fb592882SCindy Lu */ 163fb592882SCindy Lu if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) { 164fb592882SCindy Lu info_report("Zero hardware mac address detected. Ignoring."); 165fb592882SCindy Lu memcpy(netcfg.mac, n->mac, ETH_ALEN); 166fb592882SCindy Lu } 167108a6481SCindy Lu memcpy(config, &netcfg, n->config_size); 168108a6481SCindy Lu } 169108a6481SCindy Lu } 1706e790746SPaolo Bonzini } 1716e790746SPaolo Bonzini 1726e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1736e790746SPaolo Bonzini { 17417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1756e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 176c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1776e790746SPaolo Bonzini 1786e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1796e790746SPaolo Bonzini 18095129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 18195129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1826e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1836e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 1846e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1856e790746SPaolo Bonzini } 186108a6481SCindy Lu 187c546ecf2SJason Wang /* 188c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 189c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 190c546ecf2SJason Wang */ 191c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 192c546ecf2SJason Wang vhost_net_set_config(get_vhost_net(nc->peer), 193c546ecf2SJason Wang (uint8_t *)&netcfg, 0, n->config_size, 194108a6481SCindy Lu VHOST_SET_CONFIG_TYPE_MASTER); 195108a6481SCindy Lu } 1966e790746SPaolo Bonzini } 1976e790746SPaolo Bonzini 1986e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 1996e790746SPaolo Bonzini { 20017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2016e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 20217a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 2036e790746SPaolo Bonzini } 2046e790746SPaolo Bonzini 205b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net) 206b2c929f0SDr. David Alan Gilbert { 207b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(net); 208b2c929f0SDr. David Alan Gilbert trace_virtio_net_announce_notify(); 209b2c929f0SDr. David Alan Gilbert 210b2c929f0SDr. David Alan Gilbert net->status |= VIRTIO_NET_S_ANNOUNCE; 211b2c929f0SDr. David Alan Gilbert virtio_notify_config(vdev); 212b2c929f0SDr. David Alan Gilbert } 213b2c929f0SDr. David Alan Gilbert 214f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 215f57fcf70SJason Wang { 216f57fcf70SJason Wang VirtIONet *n = opaque; 2179d8c6a25SDr. David Alan Gilbert trace_virtio_net_announce_timer(n->announce_timer.round); 218f57fcf70SJason Wang 2199d8c6a25SDr. David Alan Gilbert n->announce_timer.round--; 220b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 221b2c929f0SDr. David Alan Gilbert } 222b2c929f0SDr. David Alan Gilbert 223b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc) 224b2c929f0SDr. David Alan Gilbert { 225b2c929f0SDr. David Alan Gilbert VirtIONet *n = qemu_get_nic_opaque(nc); 226b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 227b2c929f0SDr. David Alan Gilbert 228b2c929f0SDr. David Alan Gilbert /* 229b2c929f0SDr. David Alan Gilbert * Make sure the virtio migration announcement timer isn't running 230b2c929f0SDr. David Alan Gilbert * If it is, let it trigger announcement so that we do not cause 231b2c929f0SDr. David Alan Gilbert * confusion. 232b2c929f0SDr. David Alan Gilbert */ 233b2c929f0SDr. David Alan Gilbert if (n->announce_timer.round) { 234b2c929f0SDr. David Alan Gilbert return; 235b2c929f0SDr. David Alan Gilbert } 236b2c929f0SDr. David Alan Gilbert 237b2c929f0SDr. David Alan Gilbert if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 238b2c929f0SDr. David Alan Gilbert virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 239b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 240b2c929f0SDr. David Alan Gilbert } 241f57fcf70SJason Wang } 242f57fcf70SJason Wang 2436e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 2446e790746SPaolo Bonzini { 24517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2466e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 247441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 24822288fe5SJason Wang int cvq = n->max_ncs - n->max_queue_pairs; 2496e790746SPaolo Bonzini 250ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 2516e790746SPaolo Bonzini return; 2526e790746SPaolo Bonzini } 2536e790746SPaolo Bonzini 2548c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 2558c1ac475SRadim Krčmář !!n->vhost_started) { 2566e790746SPaolo Bonzini return; 2576e790746SPaolo Bonzini } 2586e790746SPaolo Bonzini if (!n->vhost_started) { 259086abc1cSMichael S. Tsirkin int r, i; 260086abc1cSMichael S. Tsirkin 2611bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2621bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 2631bfa316cSGreg Kurz "falling back on userspace virtio", 2641bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 2651bfa316cSGreg Kurz return; 2661bfa316cSGreg Kurz } 2671bfa316cSGreg Kurz 268086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 269086abc1cSMichael S. Tsirkin * when vhost is running. 270086abc1cSMichael S. Tsirkin */ 271441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 272086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 273086abc1cSMichael S. Tsirkin 274086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 275086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 276086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 277086abc1cSMichael S. Tsirkin } 278086abc1cSMichael S. Tsirkin 279a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 280a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 281a93e599dSMaxime Coquelin if (r < 0) { 282a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 283a93e599dSMaxime Coquelin n->net_conf.mtu); 284a93e599dSMaxime Coquelin 285a93e599dSMaxime Coquelin return; 286a93e599dSMaxime Coquelin } 287a93e599dSMaxime Coquelin } 288a93e599dSMaxime Coquelin 2896e790746SPaolo Bonzini n->vhost_started = 1; 29022288fe5SJason Wang r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq); 2916e790746SPaolo Bonzini if (r < 0) { 2926e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 2936e790746SPaolo Bonzini "falling back on userspace virtio", -r); 2946e790746SPaolo Bonzini n->vhost_started = 0; 2956e790746SPaolo Bonzini } 2966e790746SPaolo Bonzini } else { 29722288fe5SJason Wang vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq); 2986e790746SPaolo Bonzini n->vhost_started = 0; 2996e790746SPaolo Bonzini } 3006e790746SPaolo Bonzini } 3016e790746SPaolo Bonzini 3021bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 3031bfa316cSGreg Kurz NetClientState *peer, 3041bfa316cSGreg Kurz bool enable) 3051bfa316cSGreg Kurz { 3061bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 3071bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 3081bfa316cSGreg Kurz } else { 3091bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 3101bfa316cSGreg Kurz } 3111bfa316cSGreg Kurz } 3121bfa316cSGreg Kurz 3131bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 314441537f1SJason Wang int queue_pairs, bool enable) 3151bfa316cSGreg Kurz { 3161bfa316cSGreg Kurz int i; 3171bfa316cSGreg Kurz 318441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 3191bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 3201bfa316cSGreg Kurz enable) { 3211bfa316cSGreg Kurz while (--i >= 0) { 3221bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 3231bfa316cSGreg Kurz } 3241bfa316cSGreg Kurz 3251bfa316cSGreg Kurz return true; 3261bfa316cSGreg Kurz } 3271bfa316cSGreg Kurz } 3281bfa316cSGreg Kurz 3291bfa316cSGreg Kurz return false; 3301bfa316cSGreg Kurz } 3311bfa316cSGreg Kurz 3321bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 3331bfa316cSGreg Kurz { 3341bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 335441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3361bfa316cSGreg Kurz 3371bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 3381bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 3391bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 3401bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 3411bfa316cSGreg Kurz * virtio-net code. 3421bfa316cSGreg Kurz */ 3431bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 344441537f1SJason Wang queue_pairs, true); 3451bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3461bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3471bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3481bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3491bfa316cSGreg Kurz * endianness. 3501bfa316cSGreg Kurz */ 351441537f1SJason Wang virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false); 3521bfa316cSGreg Kurz } 3531bfa316cSGreg Kurz } 3541bfa316cSGreg Kurz 355283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 356283e2c2aSYuri Benditovich { 357283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 358283e2c2aSYuri Benditovich if (dropped) { 359283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 360283e2c2aSYuri Benditovich } 361283e2c2aSYuri Benditovich } 362283e2c2aSYuri Benditovich 3636e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3646e790746SPaolo Bonzini { 36517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3666e790746SPaolo Bonzini VirtIONetQueue *q; 3676e790746SPaolo Bonzini int i; 3686e790746SPaolo Bonzini uint8_t queue_status; 3696e790746SPaolo Bonzini 3701bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3716e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3726e790746SPaolo Bonzini 373441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 37438705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 37538705bb5SFam Zheng bool queue_started; 3766e790746SPaolo Bonzini q = &n->vqs[i]; 3776e790746SPaolo Bonzini 378441537f1SJason Wang if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) { 3796e790746SPaolo Bonzini queue_status = 0; 3806e790746SPaolo Bonzini } else { 3816e790746SPaolo Bonzini queue_status = status; 3826e790746SPaolo Bonzini } 38338705bb5SFam Zheng queue_started = 38438705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 38538705bb5SFam Zheng 38638705bb5SFam Zheng if (queue_started) { 38738705bb5SFam Zheng qemu_flush_queued_packets(ncs); 38838705bb5SFam Zheng } 3896e790746SPaolo Bonzini 3906e790746SPaolo Bonzini if (!q->tx_waiting) { 3916e790746SPaolo Bonzini continue; 3926e790746SPaolo Bonzini } 3936e790746SPaolo Bonzini 39438705bb5SFam Zheng if (queue_started) { 3956e790746SPaolo Bonzini if (q->tx_timer) { 396bc72ad67SAlex Bligh timer_mod(q->tx_timer, 397bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 3986e790746SPaolo Bonzini } else { 3996e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 4006e790746SPaolo Bonzini } 4016e790746SPaolo Bonzini } else { 4026e790746SPaolo Bonzini if (q->tx_timer) { 403bc72ad67SAlex Bligh timer_del(q->tx_timer); 4046e790746SPaolo Bonzini } else { 4056e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 4066e790746SPaolo Bonzini } 407283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 40870e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 40970e53e6eSJason Wang vdev->vm_running) { 410283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 411283e2c2aSYuri Benditovich * and disabled notification */ 412283e2c2aSYuri Benditovich q->tx_waiting = 0; 413283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 414283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 415283e2c2aSYuri Benditovich } 4166e790746SPaolo Bonzini } 4176e790746SPaolo Bonzini } 4186e790746SPaolo Bonzini } 4196e790746SPaolo Bonzini 4206e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4216e790746SPaolo Bonzini { 4226e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 42317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4246e790746SPaolo Bonzini uint16_t old_status = n->status; 4256e790746SPaolo Bonzini 4266e790746SPaolo Bonzini if (nc->link_down) 4276e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4286e790746SPaolo Bonzini else 4296e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4306e790746SPaolo Bonzini 4316e790746SPaolo Bonzini if (n->status != old_status) 43217a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4336e790746SPaolo Bonzini 43417a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4356e790746SPaolo Bonzini } 4366e790746SPaolo Bonzini 437b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 438b1be4280SAmos Kong { 439b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 440b1be4280SAmos Kong 441b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 442ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 44306150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 4443ab72385SPeter Xu n->netclient_name, path); 44596e35046SAmos Kong g_free(path); 446b1be4280SAmos Kong 447b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 448b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 449b1be4280SAmos Kong } 450b1be4280SAmos Kong } 451b1be4280SAmos Kong 452f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 453f7bc8ef8SAmos Kong { 45454aa3de7SEric Blake intList *list; 455f7bc8ef8SAmos Kong int i, j; 456f7bc8ef8SAmos Kong 457f7bc8ef8SAmos Kong list = NULL; 458f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 459f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 460f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 46154aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 462f7bc8ef8SAmos Kong } 463f7bc8ef8SAmos Kong } 464f7bc8ef8SAmos Kong } 465f7bc8ef8SAmos Kong 466f7bc8ef8SAmos Kong return list; 467f7bc8ef8SAmos Kong } 468f7bc8ef8SAmos Kong 469b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 470b1be4280SAmos Kong { 471b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 472f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 473b1be4280SAmos Kong RxFilterInfo *info; 47454aa3de7SEric Blake strList *str_list; 475f7bc8ef8SAmos Kong int i; 476b1be4280SAmos Kong 477b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 478b1be4280SAmos Kong info->name = g_strdup(nc->name); 479b1be4280SAmos Kong info->promiscuous = n->promisc; 480b1be4280SAmos Kong 481b1be4280SAmos Kong if (n->nouni) { 482b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 483b1be4280SAmos Kong } else if (n->alluni) { 484b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 485b1be4280SAmos Kong } else { 486b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 487b1be4280SAmos Kong } 488b1be4280SAmos Kong 489b1be4280SAmos Kong if (n->nomulti) { 490b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 491b1be4280SAmos Kong } else if (n->allmulti) { 492b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 493b1be4280SAmos Kong } else { 494b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 495b1be4280SAmos Kong } 496b1be4280SAmos Kong 497b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 498b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 499b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 500b1be4280SAmos Kong 501b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 502b1be4280SAmos Kong 503b1be4280SAmos Kong str_list = NULL; 504b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 50554aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 50654aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 507b1be4280SAmos Kong } 508b1be4280SAmos Kong info->unicast_table = str_list; 509b1be4280SAmos Kong 510b1be4280SAmos Kong str_list = NULL; 511b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 51254aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 51354aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 514b1be4280SAmos Kong } 515b1be4280SAmos Kong info->multicast_table = str_list; 516f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 517b1be4280SAmos Kong 51895129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 519f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 520f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 521f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 522f7bc8ef8SAmos Kong } else { 523f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 524b1be4280SAmos Kong } 525b1be4280SAmos Kong 526b1be4280SAmos Kong /* enable event notification after query */ 527b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 528b1be4280SAmos Kong 529b1be4280SAmos Kong return info; 530b1be4280SAmos Kong } 531b1be4280SAmos Kong 5326e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 5336e790746SPaolo Bonzini { 53417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 53594b52958SGreg Kurz int i; 5366e790746SPaolo Bonzini 5376e790746SPaolo Bonzini /* Reset back to compatibility mode */ 5386e790746SPaolo Bonzini n->promisc = 1; 5396e790746SPaolo Bonzini n->allmulti = 0; 5406e790746SPaolo Bonzini n->alluni = 0; 5416e790746SPaolo Bonzini n->nomulti = 0; 5426e790746SPaolo Bonzini n->nouni = 0; 5436e790746SPaolo Bonzini n->nobcast = 0; 5446e790746SPaolo Bonzini /* multiqueue is disabled by default */ 545441537f1SJason Wang n->curr_queue_pairs = 1; 5469d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 5479d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 548f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 5496e790746SPaolo Bonzini 5506e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 5516e790746SPaolo Bonzini n->mac_table.in_use = 0; 5526e790746SPaolo Bonzini n->mac_table.first_multi = 0; 5536e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 5546e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 5556e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 5566e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 557702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 5586e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 55994b52958SGreg Kurz 56094b52958SGreg Kurz /* Flush any async TX */ 561441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 56294b52958SGreg Kurz NetClientState *nc = qemu_get_subqueue(n->nic, i); 56394b52958SGreg Kurz 56494b52958SGreg Kurz if (nc->peer) { 56594b52958SGreg Kurz qemu_flush_or_purge_queued_packets(nc->peer, true); 56694b52958SGreg Kurz assert(!virtio_net_get_subqueue(nc)->async_tx.elem); 56794b52958SGreg Kurz } 56894b52958SGreg Kurz } 5696e790746SPaolo Bonzini } 5706e790746SPaolo Bonzini 5716e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 5726e790746SPaolo Bonzini { 5736e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5746e790746SPaolo Bonzini if (!nc->peer) { 5756e790746SPaolo Bonzini return; 5766e790746SPaolo Bonzini } 5776e790746SPaolo Bonzini 578d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 5796e790746SPaolo Bonzini } 5806e790746SPaolo Bonzini 5816e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 5826e790746SPaolo Bonzini { 5836e790746SPaolo Bonzini return n->has_vnet_hdr; 5846e790746SPaolo Bonzini } 5856e790746SPaolo Bonzini 5866e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 5876e790746SPaolo Bonzini { 5886e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 5896e790746SPaolo Bonzini return 0; 5906e790746SPaolo Bonzini 591d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 5926e790746SPaolo Bonzini 5936e790746SPaolo Bonzini return n->has_ufo; 5946e790746SPaolo Bonzini } 5956e790746SPaolo Bonzini 596bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 597e22f0603SYuri Benditovich int version_1, int hash_report) 5986e790746SPaolo Bonzini { 5996e790746SPaolo Bonzini int i; 6006e790746SPaolo Bonzini NetClientState *nc; 6016e790746SPaolo Bonzini 6026e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6036e790746SPaolo Bonzini 604bb9d17f8SCornelia Huck if (version_1) { 605e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 606e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 607e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 608e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 609bb9d17f8SCornelia Huck } else { 6106e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 611bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 612bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 613bb9d17f8SCornelia Huck } 6146e790746SPaolo Bonzini 615441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6166e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6176e790746SPaolo Bonzini 6186e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 619d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 620d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6216e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6226e790746SPaolo Bonzini } 6236e790746SPaolo Bonzini } 6246e790746SPaolo Bonzini } 6256e790746SPaolo Bonzini 6262eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6272eef278bSMichael S. Tsirkin { 6282eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6292eef278bSMichael S. Tsirkin 6302eef278bSMichael S. Tsirkin /* 6312eef278bSMichael S. Tsirkin * Backends other than vhost-user don't support max queue size. 6322eef278bSMichael S. Tsirkin */ 6332eef278bSMichael S. Tsirkin if (!peer) { 6342eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6352eef278bSMichael S. Tsirkin } 6362eef278bSMichael S. Tsirkin 6372eef278bSMichael S. Tsirkin if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) { 6382eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6392eef278bSMichael S. Tsirkin } 6402eef278bSMichael S. Tsirkin 6412eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 6422eef278bSMichael S. Tsirkin } 6432eef278bSMichael S. Tsirkin 6446e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 6456e790746SPaolo Bonzini { 6466e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6476e790746SPaolo Bonzini 6486e790746SPaolo Bonzini if (!nc->peer) { 6496e790746SPaolo Bonzini return 0; 6506e790746SPaolo Bonzini } 6516e790746SPaolo Bonzini 652f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6537263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 6547263a0adSChangchun Ouyang } 6557263a0adSChangchun Ouyang 656f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6576e790746SPaolo Bonzini return 0; 6586e790746SPaolo Bonzini } 6596e790746SPaolo Bonzini 660441537f1SJason Wang if (n->max_queue_pairs == 1) { 6611074b879SJason Wang return 0; 6621074b879SJason Wang } 6631074b879SJason Wang 6646e790746SPaolo Bonzini return tap_enable(nc->peer); 6656e790746SPaolo Bonzini } 6666e790746SPaolo Bonzini 6676e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 6686e790746SPaolo Bonzini { 6696e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6706e790746SPaolo Bonzini 6716e790746SPaolo Bonzini if (!nc->peer) { 6726e790746SPaolo Bonzini return 0; 6736e790746SPaolo Bonzini } 6746e790746SPaolo Bonzini 675f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6767263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 6777263a0adSChangchun Ouyang } 6787263a0adSChangchun Ouyang 679f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6806e790746SPaolo Bonzini return 0; 6816e790746SPaolo Bonzini } 6826e790746SPaolo Bonzini 6836e790746SPaolo Bonzini return tap_disable(nc->peer); 6846e790746SPaolo Bonzini } 6856e790746SPaolo Bonzini 686441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 6876e790746SPaolo Bonzini { 6886e790746SPaolo Bonzini int i; 689ddfa83eaSJoel Stanley int r; 6906e790746SPaolo Bonzini 69168b5f314SYuri Benditovich if (n->nic->peer_deleted) { 69268b5f314SYuri Benditovich return; 69368b5f314SYuri Benditovich } 69468b5f314SYuri Benditovich 695441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 696441537f1SJason Wang if (i < n->curr_queue_pairs) { 697ddfa83eaSJoel Stanley r = peer_attach(n, i); 698ddfa83eaSJoel Stanley assert(!r); 6996e790746SPaolo Bonzini } else { 700ddfa83eaSJoel Stanley r = peer_detach(n, i); 701ddfa83eaSJoel Stanley assert(!r); 7026e790746SPaolo Bonzini } 7036e790746SPaolo Bonzini } 7046e790746SPaolo Bonzini } 7056e790746SPaolo Bonzini 706ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7076e790746SPaolo Bonzini 7089d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7099d5b731dSJason Wang Error **errp) 7106e790746SPaolo Bonzini { 71117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7126e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7136e790746SPaolo Bonzini 714da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 715da3e8a23SShannon Zhao features |= n->host_features; 716da3e8a23SShannon Zhao 7170cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7186e790746SPaolo Bonzini 7196e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7200cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7210cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7220cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7230cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7246e790746SPaolo Bonzini 7250cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7260cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7270cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7280cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 729e22f0603SYuri Benditovich 730e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7316e790746SPaolo Bonzini } 7326e790746SPaolo Bonzini 7336e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7340cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 7350cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 7366e790746SPaolo Bonzini } 7376e790746SPaolo Bonzini 738ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 7396e790746SPaolo Bonzini return features; 7406e790746SPaolo Bonzini } 7412974e916SYuri Benditovich 7420145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 74359079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 7440145c393SAndrew Melnychenko } 74575ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 74675ebec11SMaxime Coquelin vdev->backend_features = features; 74775ebec11SMaxime Coquelin 74875ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 74975ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 75075ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 75175ebec11SMaxime Coquelin } 75275ebec11SMaxime Coquelin 75375ebec11SMaxime Coquelin return features; 7546e790746SPaolo Bonzini } 7556e790746SPaolo Bonzini 756019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 7576e790746SPaolo Bonzini { 758019a3edbSGerd Hoffmann uint64_t features = 0; 7596e790746SPaolo Bonzini 7606e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 7616e790746SPaolo Bonzini * but also these: */ 7620cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7630cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 7640cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7650cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7660cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 7676e790746SPaolo Bonzini 7686e790746SPaolo Bonzini return features; 7696e790746SPaolo Bonzini } 7706e790746SPaolo Bonzini 771644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 772644c9858SDmitry Fleytman { 773ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 774644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 775644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 776644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 777644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 778644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 779644c9858SDmitry Fleytman } 780644c9858SDmitry Fleytman 781644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 782644c9858SDmitry Fleytman { 783644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 784644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 785644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 786644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 787644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 788644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 789644c9858SDmitry Fleytman 790644c9858SDmitry Fleytman return guest_offloads_mask & features; 791644c9858SDmitry Fleytman } 792644c9858SDmitry Fleytman 793644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 794644c9858SDmitry Fleytman { 795644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 796644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 797644c9858SDmitry Fleytman } 798644c9858SDmitry Fleytman 799f5e1847bSJuan Quintela typedef struct { 800f5e1847bSJuan Quintela VirtIONet *n; 80112b2fad7SKevin Wolf DeviceState *dev; 80212b2fad7SKevin Wolf } FailoverDevice; 803f5e1847bSJuan Quintela 804f5e1847bSJuan Quintela /** 80512b2fad7SKevin Wolf * Set the failover primary device 806f5e1847bSJuan Quintela * 807f5e1847bSJuan Quintela * @opaque: FailoverId to setup 808f5e1847bSJuan Quintela * @opts: opts for device we are handling 809f5e1847bSJuan Quintela * @errp: returns an error if this function fails 810f5e1847bSJuan Quintela */ 81112b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 812f5e1847bSJuan Quintela { 81312b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 81412b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 81512b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 816f5e1847bSJuan Quintela 81712b2fad7SKevin Wolf if (!pci_dev) { 81812b2fad7SKevin Wolf return 0; 81912b2fad7SKevin Wolf } 82012b2fad7SKevin Wolf 82112b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 82212b2fad7SKevin Wolf fdev->dev = dev; 823f5e1847bSJuan Quintela return 1; 824f5e1847bSJuan Quintela } 825f5e1847bSJuan Quintela 826f5e1847bSJuan Quintela return 0; 827f5e1847bSJuan Quintela } 828f5e1847bSJuan Quintela 829f5e1847bSJuan Quintela /** 83085d3b931SJuan Quintela * Find the primary device for this failover virtio-net 83185d3b931SJuan Quintela * 83285d3b931SJuan Quintela * @n: VirtIONet device 83385d3b931SJuan Quintela * @errp: returns an error if this function fails 83485d3b931SJuan Quintela */ 8350a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 8369711cd0dSJens Freimann { 83712b2fad7SKevin Wolf FailoverDevice fdev = { 83812b2fad7SKevin Wolf .n = n, 83912b2fad7SKevin Wolf }; 8409711cd0dSJens Freimann 84112b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 84212b2fad7SKevin Wolf NULL, NULL, &fdev); 84312b2fad7SKevin Wolf return fdev.dev; 8449711cd0dSJens Freimann } 8459711cd0dSJens Freimann 84621e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 84721e8709bSJuan Quintela { 84821e8709bSJuan Quintela Error *err = NULL; 84921e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 85021e8709bSJuan Quintela 85121e8709bSJuan Quintela if (dev) { 85221e8709bSJuan Quintela return; 85321e8709bSJuan Quintela } 85421e8709bSJuan Quintela 855259a10dbSKevin Wolf if (!n->primary_opts) { 85697ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 85797ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 85897ca9c59SLaurent Vivier "sure primary device has parameter" 85997ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 86021e8709bSJuan Quintela return; 86121e8709bSJuan Quintela } 862259a10dbSKevin Wolf 863f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 864f3558b1bSKevin Wolf n->primary_opts_from_json, 865f3558b1bSKevin Wolf &err); 86621e8709bSJuan Quintela if (err) { 867f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 868259a10dbSKevin Wolf n->primary_opts = NULL; 86900e7b129SLaurent Vivier } else { 87000e7b129SLaurent Vivier object_unref(OBJECT(dev)); 87121e8709bSJuan Quintela } 87221e8709bSJuan Quintela error_propagate(errp, err); 87321e8709bSJuan Quintela } 87421e8709bSJuan Quintela 875d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 8766e790746SPaolo Bonzini { 87717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 8789711cd0dSJens Freimann Error *err = NULL; 8796e790746SPaolo Bonzini int i; 8806e790746SPaolo Bonzini 88175ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 88275ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 88375ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 88475ebec11SMaxime Coquelin } 88575ebec11SMaxime Coquelin 886ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 88759079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 88895129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 8896e790746SPaolo Bonzini 890ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 89195129d6fSCornelia Huck virtio_has_feature(features, 892bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 89395129d6fSCornelia Huck virtio_has_feature(features, 894e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 895e22f0603SYuri Benditovich virtio_has_feature(features, 896e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 8976e790746SPaolo Bonzini 8982974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 8992974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 9002974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9012974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 902e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 9032974e916SYuri Benditovich 9046e790746SPaolo Bonzini if (n->has_vnet_hdr) { 905644c9858SDmitry Fleytman n->curr_guest_offloads = 906644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 907644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 9086e790746SPaolo Bonzini } 9096e790746SPaolo Bonzini 910441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 9116e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9126e790746SPaolo Bonzini 913ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9146e790746SPaolo Bonzini continue; 9156e790746SPaolo Bonzini } 916ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 9176e790746SPaolo Bonzini } 9180b1eaa88SStefan Fritsch 91995129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 9200b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 9210b1eaa88SStefan Fritsch } else { 9220b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 9230b1eaa88SStefan Fritsch } 9249711cd0dSJens Freimann 9259711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 9269711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 927e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 9289711cd0dSJens Freimann failover_add_primary(n, &err); 9299711cd0dSJens Freimann if (err) { 930*1b529d90SLaurent Vivier if (!qtest_enabled()) { 9319711cd0dSJens Freimann warn_report_err(err); 932*1b529d90SLaurent Vivier } else { 933*1b529d90SLaurent Vivier error_free(err); 934*1b529d90SLaurent Vivier } 9359711cd0dSJens Freimann } 9366e790746SPaolo Bonzini } 93721e8709bSJuan Quintela } 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 11580145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n); 11590145c393SAndrew Melnychenko 116059079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 116159079029SYuri Benditovich { 116259079029SYuri Benditovich if (n->rss_data.enabled) { 116359079029SYuri Benditovich trace_virtio_net_rss_disable(); 116459079029SYuri Benditovich } 116559079029SYuri Benditovich n->rss_data.enabled = false; 11660145c393SAndrew Melnychenko 11670145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 11680145c393SAndrew Melnychenko } 11690145c393SAndrew Melnychenko 11700145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 11710145c393SAndrew Melnychenko { 11720145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 11730145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 11740145c393SAndrew Melnychenko return false; 11750145c393SAndrew Melnychenko } 11760145c393SAndrew Melnychenko 11770145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 11780145c393SAndrew Melnychenko } 11790145c393SAndrew Melnychenko 11800145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 11810145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 11820145c393SAndrew Melnychenko { 11830145c393SAndrew Melnychenko config->redirect = data->redirect; 11840145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 11850145c393SAndrew Melnychenko config->hash_types = data->hash_types; 11860145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 11870145c393SAndrew Melnychenko config->default_queue = data->default_queue; 11880145c393SAndrew Melnychenko } 11890145c393SAndrew Melnychenko 11900145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 11910145c393SAndrew Melnychenko { 11920145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 11930145c393SAndrew Melnychenko 11940145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 11950145c393SAndrew Melnychenko return false; 11960145c393SAndrew Melnychenko } 11970145c393SAndrew Melnychenko 11980145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 11990145c393SAndrew Melnychenko 12000145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 12010145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 12020145c393SAndrew Melnychenko return false; 12030145c393SAndrew Melnychenko } 12040145c393SAndrew Melnychenko 12050145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 12060145c393SAndrew Melnychenko return false; 12070145c393SAndrew Melnychenko } 12080145c393SAndrew Melnychenko 12090145c393SAndrew Melnychenko return true; 12100145c393SAndrew Melnychenko } 12110145c393SAndrew Melnychenko 12120145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 12130145c393SAndrew Melnychenko { 12140145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12150145c393SAndrew Melnychenko } 12160145c393SAndrew Melnychenko 12170145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n) 12180145c393SAndrew Melnychenko { 12190145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 12200145c393SAndrew Melnychenko /* backend does't support steering ebpf */ 12210145c393SAndrew Melnychenko return false; 12220145c393SAndrew Melnychenko } 12230145c393SAndrew Melnychenko 12240145c393SAndrew Melnychenko return ebpf_rss_load(&n->ebpf_rss); 12250145c393SAndrew Melnychenko } 12260145c393SAndrew Melnychenko 12270145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 12280145c393SAndrew Melnychenko { 12290145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12300145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 123159079029SYuri Benditovich } 123259079029SYuri Benditovich 123359079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1234e22f0603SYuri Benditovich struct iovec *iov, 1235e22f0603SYuri Benditovich unsigned int iov_cnt, 1236e22f0603SYuri Benditovich bool do_rss) 123759079029SYuri Benditovich { 123859079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 123959079029SYuri Benditovich struct virtio_net_rss_config cfg; 124059079029SYuri Benditovich size_t s, offset = 0, size_get; 1241441537f1SJason Wang uint16_t queue_pairs, i; 124259079029SYuri Benditovich struct { 124359079029SYuri Benditovich uint16_t us; 124459079029SYuri Benditovich uint8_t b; 124559079029SYuri Benditovich } QEMU_PACKED temp; 124659079029SYuri Benditovich const char *err_msg = ""; 124759079029SYuri Benditovich uint32_t err_value = 0; 124859079029SYuri Benditovich 1249e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 125059079029SYuri Benditovich err_msg = "RSS is not negotiated"; 125159079029SYuri Benditovich goto error; 125259079029SYuri Benditovich } 1253e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1254e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1255e22f0603SYuri Benditovich goto error; 1256e22f0603SYuri Benditovich } 125759079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 125859079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 125959079029SYuri Benditovich if (s != size_get) { 126059079029SYuri Benditovich err_msg = "Short command buffer"; 126159079029SYuri Benditovich err_value = (uint32_t)s; 126259079029SYuri Benditovich goto error; 126359079029SYuri Benditovich } 126459079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 126559079029SYuri Benditovich n->rss_data.indirections_len = 126659079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 126759079029SYuri Benditovich n->rss_data.indirections_len++; 1268e22f0603SYuri Benditovich if (!do_rss) { 1269e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1270e22f0603SYuri Benditovich } 127159079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 127259079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 127359079029SYuri Benditovich err_value = n->rss_data.indirections_len; 127459079029SYuri Benditovich goto error; 127559079029SYuri Benditovich } 127659079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 127759079029SYuri Benditovich err_msg = "Too large indirection table"; 127859079029SYuri Benditovich err_value = n->rss_data.indirections_len; 127959079029SYuri Benditovich goto error; 128059079029SYuri Benditovich } 1281e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1282e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1283441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 128459079029SYuri Benditovich err_msg = "Invalid default queue"; 128559079029SYuri Benditovich err_value = n->rss_data.default_queue; 128659079029SYuri Benditovich goto error; 128759079029SYuri Benditovich } 128859079029SYuri Benditovich offset += size_get; 128959079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 129059079029SYuri Benditovich g_free(n->rss_data.indirections_table); 129159079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 129259079029SYuri Benditovich if (!n->rss_data.indirections_table) { 129359079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 129459079029SYuri Benditovich err_value = n->rss_data.indirections_len; 129559079029SYuri Benditovich goto error; 129659079029SYuri Benditovich } 129759079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 129859079029SYuri Benditovich n->rss_data.indirections_table, size_get); 129959079029SYuri Benditovich if (s != size_get) { 130059079029SYuri Benditovich err_msg = "Short indirection table buffer"; 130159079029SYuri Benditovich err_value = (uint32_t)s; 130259079029SYuri Benditovich goto error; 130359079029SYuri Benditovich } 130459079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 130559079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 130659079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 130759079029SYuri Benditovich } 130859079029SYuri Benditovich offset += size_get; 130959079029SYuri Benditovich size_get = sizeof(temp); 131059079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 131159079029SYuri Benditovich if (s != size_get) { 1312441537f1SJason Wang err_msg = "Can't get queue_pairs"; 131359079029SYuri Benditovich err_value = (uint32_t)s; 131459079029SYuri Benditovich goto error; 131559079029SYuri Benditovich } 1316441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1317441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1318441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1319441537f1SJason Wang err_value = queue_pairs; 132059079029SYuri Benditovich goto error; 132159079029SYuri Benditovich } 132259079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 132359079029SYuri Benditovich err_msg = "Invalid key size"; 132459079029SYuri Benditovich err_value = temp.b; 132559079029SYuri Benditovich goto error; 132659079029SYuri Benditovich } 132759079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 132859079029SYuri Benditovich err_msg = "No key provided"; 132959079029SYuri Benditovich err_value = 0; 133059079029SYuri Benditovich goto error; 133159079029SYuri Benditovich } 133259079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 133359079029SYuri Benditovich virtio_net_disable_rss(n); 1334441537f1SJason Wang return queue_pairs; 133559079029SYuri Benditovich } 133659079029SYuri Benditovich offset += size_get; 133759079029SYuri Benditovich size_get = temp.b; 133859079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 133959079029SYuri Benditovich if (s != size_get) { 134059079029SYuri Benditovich err_msg = "Can get key buffer"; 134159079029SYuri Benditovich err_value = (uint32_t)s; 134259079029SYuri Benditovich goto error; 134359079029SYuri Benditovich } 134459079029SYuri Benditovich n->rss_data.enabled = true; 13450145c393SAndrew Melnychenko 13460145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 13470145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 13480145c393SAndrew Melnychenko /* EBPF must be loaded for vhost */ 13490145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 13500145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS for vhost"); 13510145c393SAndrew Melnychenko goto error; 13520145c393SAndrew Melnychenko } 13530145c393SAndrew Melnychenko /* fallback to software RSS */ 13540145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS - fallback to software RSS"); 13550145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 13560145c393SAndrew Melnychenko } 13570145c393SAndrew Melnychenko } else { 13580145c393SAndrew Melnychenko /* use software RSS for hash populating */ 13590145c393SAndrew Melnychenko /* and detach eBPF if was loaded before */ 13600145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 13610145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 13620145c393SAndrew Melnychenko } 13630145c393SAndrew Melnychenko 136459079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 136559079029SYuri Benditovich n->rss_data.indirections_len, 136659079029SYuri Benditovich temp.b); 1367441537f1SJason Wang return queue_pairs; 136859079029SYuri Benditovich error: 136959079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 137059079029SYuri Benditovich virtio_net_disable_rss(n); 137159079029SYuri Benditovich return 0; 137259079029SYuri Benditovich } 137359079029SYuri Benditovich 13746e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 13756e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 13766e790746SPaolo Bonzini { 137717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1378441537f1SJason Wang uint16_t queue_pairs; 13796e790746SPaolo Bonzini 138059079029SYuri Benditovich virtio_net_disable_rss(n); 1381e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1382441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1383441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1384e22f0603SYuri Benditovich } 138559079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1386441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 138759079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 138859079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 138959079029SYuri Benditovich size_t s; 139059079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 139159079029SYuri Benditovich return VIRTIO_NET_ERR; 139259079029SYuri Benditovich } 13936e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 13946e790746SPaolo Bonzini if (s != sizeof(mq)) { 13956e790746SPaolo Bonzini return VIRTIO_NET_ERR; 13966e790746SPaolo Bonzini } 1397441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 13986e790746SPaolo Bonzini 139959079029SYuri Benditovich } else { 14006e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14016e790746SPaolo Bonzini } 14026e790746SPaolo Bonzini 1403441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1404441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1405441537f1SJason Wang queue_pairs > n->max_queue_pairs || 14066e790746SPaolo Bonzini !n->multiqueue) { 14076e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14086e790746SPaolo Bonzini } 14096e790746SPaolo Bonzini 1410441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1411441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 14126e790746SPaolo Bonzini * disabled queue */ 141317a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1414441537f1SJason Wang virtio_net_set_queue_pairs(n); 14156e790746SPaolo Bonzini 14166e790746SPaolo Bonzini return VIRTIO_NET_OK; 14176e790746SPaolo Bonzini } 1418ba7eadb5SGreg Kurz 14196e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 14206e790746SPaolo Bonzini { 142117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14226e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 14236e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 142451b19ebeSPaolo Bonzini VirtQueueElement *elem; 14256e790746SPaolo Bonzini size_t s; 1426771b6ed3SJason Wang struct iovec *iov, *iov2; 14276e790746SPaolo Bonzini unsigned int iov_cnt; 14286e790746SPaolo Bonzini 142951b19ebeSPaolo Bonzini for (;;) { 143051b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 143151b19ebeSPaolo Bonzini if (!elem) { 143251b19ebeSPaolo Bonzini break; 143351b19ebeSPaolo Bonzini } 143451b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 143551b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 1436ba7eadb5SGreg Kurz virtio_error(vdev, "virtio-net ctrl missing headers"); 1437ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1438ba7eadb5SGreg Kurz g_free(elem); 1439ba7eadb5SGreg Kurz break; 14406e790746SPaolo Bonzini } 14416e790746SPaolo Bonzini 144251b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 144351b19ebeSPaolo Bonzini iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num); 14446e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 14456e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 14466e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 14476e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 14486e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 14496e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 14506e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 14516e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 14526e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 14536e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 1454f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1455f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 14566e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 14576e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 1458644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1459644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 14606e790746SPaolo Bonzini } 14616e790746SPaolo Bonzini 146251b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 14636e790746SPaolo Bonzini assert(s == sizeof(status)); 14646e790746SPaolo Bonzini 146551b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 14666e790746SPaolo Bonzini virtio_notify(vdev, vq); 1467771b6ed3SJason Wang g_free(iov2); 146851b19ebeSPaolo Bonzini g_free(elem); 14696e790746SPaolo Bonzini } 14706e790746SPaolo Bonzini } 14716e790746SPaolo Bonzini 14726e790746SPaolo Bonzini /* RX */ 14736e790746SPaolo Bonzini 14746e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 14756e790746SPaolo Bonzini { 147617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14776e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 14786e790746SPaolo Bonzini 14796e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 14806e790746SPaolo Bonzini } 14816e790746SPaolo Bonzini 1482b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 14836e790746SPaolo Bonzini { 14846e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 148517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 14866e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 14876e790746SPaolo Bonzini 148817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1489b8c4b67eSPhilippe Mathieu-Daudé return false; 14906e790746SPaolo Bonzini } 14916e790746SPaolo Bonzini 1492441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1493b8c4b67eSPhilippe Mathieu-Daudé return false; 14946e790746SPaolo Bonzini } 14956e790746SPaolo Bonzini 14966e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 149717a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1498b8c4b67eSPhilippe Mathieu-Daudé return false; 14996e790746SPaolo Bonzini } 15006e790746SPaolo Bonzini 1501b8c4b67eSPhilippe Mathieu-Daudé return true; 15026e790746SPaolo Bonzini } 15036e790746SPaolo Bonzini 15046e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 15056e790746SPaolo Bonzini { 15066e790746SPaolo Bonzini VirtIONet *n = q->n; 15076e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15086e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15096e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15106e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 15116e790746SPaolo Bonzini 15126e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 15136e790746SPaolo Bonzini * available after the above check but before notification was 15146e790746SPaolo Bonzini * enabled, check for available buffers again. 15156e790746SPaolo Bonzini */ 15166e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15176e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15186e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15196e790746SPaolo Bonzini return 0; 15206e790746SPaolo Bonzini } 15216e790746SPaolo Bonzini } 15226e790746SPaolo Bonzini 15236e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 15246e790746SPaolo Bonzini return 1; 15256e790746SPaolo Bonzini } 15266e790746SPaolo Bonzini 15271399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1528032a74a1SCédric Le Goater { 15291399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 15301399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 15311399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 15321399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1533032a74a1SCédric Le Goater } 1534032a74a1SCédric Le Goater 15356e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 15366e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 15376e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 15386e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 15396e790746SPaolo Bonzini * dhclient yet. 15406e790746SPaolo Bonzini * 15416e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 15426e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 15436e790746SPaolo Bonzini * kernels. 15446e790746SPaolo Bonzini * 15456e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 15466e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 15476e790746SPaolo Bonzini * cache. 15486e790746SPaolo Bonzini */ 15496e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 15506e790746SPaolo Bonzini uint8_t *buf, size_t size) 15516e790746SPaolo Bonzini { 15526e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 15536e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 15546e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 15556e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 15566e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1557f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 15586e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 15596e790746SPaolo Bonzini } 15606e790746SPaolo Bonzini } 15616e790746SPaolo Bonzini 15626e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 15636e790746SPaolo Bonzini const void *buf, size_t size) 15646e790746SPaolo Bonzini { 15656e790746SPaolo Bonzini if (n->has_vnet_hdr) { 15666e790746SPaolo Bonzini /* FIXME this cast is evil */ 15676e790746SPaolo Bonzini void *wbuf = (void *)buf; 15686e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 15696e790746SPaolo Bonzini size - n->host_hdr_len); 15701bfa316cSGreg Kurz 15711bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 15721399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 15731bfa316cSGreg Kurz } 15746e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 15756e790746SPaolo Bonzini } else { 15766e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 15776e790746SPaolo Bonzini .flags = 0, 15786e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 15796e790746SPaolo Bonzini }; 15806e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 15816e790746SPaolo Bonzini } 15826e790746SPaolo Bonzini } 15836e790746SPaolo Bonzini 15846e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 15856e790746SPaolo Bonzini { 15866e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 15876e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 15886e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 15896e790746SPaolo Bonzini int i; 15906e790746SPaolo Bonzini 15916e790746SPaolo Bonzini if (n->promisc) 15926e790746SPaolo Bonzini return 1; 15936e790746SPaolo Bonzini 15946e790746SPaolo Bonzini ptr += n->host_hdr_len; 15956e790746SPaolo Bonzini 15966e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 15977542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 15986e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 15996e790746SPaolo Bonzini return 0; 16006e790746SPaolo Bonzini } 16016e790746SPaolo Bonzini 16026e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 16036e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 16046e790746SPaolo Bonzini return !n->nobcast; 16056e790746SPaolo Bonzini } else if (n->nomulti) { 16066e790746SPaolo Bonzini return 0; 16076e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 16086e790746SPaolo Bonzini return 1; 16096e790746SPaolo Bonzini } 16106e790746SPaolo Bonzini 16116e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 16126e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 16136e790746SPaolo Bonzini return 1; 16146e790746SPaolo Bonzini } 16156e790746SPaolo Bonzini } 16166e790746SPaolo Bonzini } else { // unicast 16176e790746SPaolo Bonzini if (n->nouni) { 16186e790746SPaolo Bonzini return 0; 16196e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 16206e790746SPaolo Bonzini return 1; 16216e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 16226e790746SPaolo Bonzini return 1; 16236e790746SPaolo Bonzini } 16246e790746SPaolo Bonzini 16256e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 16266e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 16276e790746SPaolo Bonzini return 1; 16286e790746SPaolo Bonzini } 16296e790746SPaolo Bonzini } 16306e790746SPaolo Bonzini } 16316e790746SPaolo Bonzini 16326e790746SPaolo Bonzini return 0; 16336e790746SPaolo Bonzini } 16346e790746SPaolo Bonzini 16354474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4, 16364474e37aSYuri Benditovich bool isip6, 16374474e37aSYuri Benditovich bool isudp, 16384474e37aSYuri Benditovich bool istcp, 16394474e37aSYuri Benditovich uint32_t types) 16404474e37aSYuri Benditovich { 16414474e37aSYuri Benditovich if (isip4) { 16424474e37aSYuri Benditovich if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) { 16434474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 16444474e37aSYuri Benditovich } 16454474e37aSYuri Benditovich if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) { 16464474e37aSYuri Benditovich return NetPktRssIpV4Udp; 16474474e37aSYuri Benditovich } 16484474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 16494474e37aSYuri Benditovich return NetPktRssIpV4; 16504474e37aSYuri Benditovich } 16514474e37aSYuri Benditovich } else if (isip6) { 16524474e37aSYuri Benditovich uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 16534474e37aSYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6; 16544474e37aSYuri Benditovich 16554474e37aSYuri Benditovich if (istcp && (types & mask)) { 16564474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ? 16574474e37aSYuri Benditovich NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp; 16584474e37aSYuri Benditovich } 16594474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6; 16604474e37aSYuri Benditovich if (isudp && (types & mask)) { 16614474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ? 16624474e37aSYuri Benditovich NetPktRssIpV6UdpEx : NetPktRssIpV6Udp; 16634474e37aSYuri Benditovich } 16644474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6; 16654474e37aSYuri Benditovich if (types & mask) { 16664474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ? 16674474e37aSYuri Benditovich NetPktRssIpV6Ex : NetPktRssIpV6; 16684474e37aSYuri Benditovich } 16694474e37aSYuri Benditovich } 16704474e37aSYuri Benditovich return 0xff; 16714474e37aSYuri Benditovich } 16724474e37aSYuri Benditovich 1673e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1674e22f0603SYuri Benditovich uint32_t hash) 1675e22f0603SYuri Benditovich { 1676e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1677e22f0603SYuri Benditovich hdr->hash_value = hash; 1678e22f0603SYuri Benditovich hdr->hash_report = report; 1679e22f0603SYuri Benditovich } 1680e22f0603SYuri Benditovich 16814474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 168297cd965cSPaolo Bonzini size_t size) 16836e790746SPaolo Bonzini { 16846e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1685e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 16864474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 16874474e37aSYuri Benditovich uint8_t net_hash_type; 16884474e37aSYuri Benditovich uint32_t hash; 16894474e37aSYuri Benditovich bool isip4, isip6, isudp, istcp; 1690e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1691e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1692e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1693e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1694e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1695e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1696e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1697e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1698e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1699e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1700e22f0603SYuri Benditovich }; 17014474e37aSYuri Benditovich 17024474e37aSYuri Benditovich net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len, 17034474e37aSYuri Benditovich size - n->host_hdr_len); 17044474e37aSYuri Benditovich net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp); 17054474e37aSYuri Benditovich if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) { 17064474e37aSYuri Benditovich istcp = isudp = false; 17074474e37aSYuri Benditovich } 17084474e37aSYuri Benditovich if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) { 17094474e37aSYuri Benditovich istcp = isudp = false; 17104474e37aSYuri Benditovich } 17114474e37aSYuri Benditovich net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp, 17124474e37aSYuri Benditovich n->rss_data.hash_types); 17134474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1714e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1715e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1716e22f0603SYuri Benditovich } 1717e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 17184474e37aSYuri Benditovich } 17194474e37aSYuri Benditovich 17204474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1721e22f0603SYuri Benditovich 1722e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1723e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1724e22f0603SYuri Benditovich } 1725e22f0603SYuri Benditovich 1726e22f0603SYuri Benditovich if (n->rss_data.redirect) { 17274474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 17284474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 17294474e37aSYuri Benditovich } 1730e22f0603SYuri Benditovich 1731e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 17324474e37aSYuri Benditovich } 17334474e37aSYuri Benditovich 17344474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 17354474e37aSYuri Benditovich size_t size, bool no_rss) 17364474e37aSYuri Benditovich { 17374474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 17386e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 173917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1740bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1741bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 17426e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 17436e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 17446e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1745bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1746bedd7e93SJason Wang ssize_t err; 17476e790746SPaolo Bonzini 17486e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 17496e790746SPaolo Bonzini return -1; 17506e790746SPaolo Bonzini } 17516e790746SPaolo Bonzini 17520145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 17534474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 17544474e37aSYuri Benditovich if (index >= 0) { 17554474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 17564474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 17574474e37aSYuri Benditovich } 17584474e37aSYuri Benditovich } 17594474e37aSYuri Benditovich 17606e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 17616e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 17626e790746SPaolo Bonzini return 0; 17636e790746SPaolo Bonzini } 17646e790746SPaolo Bonzini 17656e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 17666e790746SPaolo Bonzini return size; 17676e790746SPaolo Bonzini 17686e790746SPaolo Bonzini offset = i = 0; 17696e790746SPaolo Bonzini 17706e790746SPaolo Bonzini while (offset < size) { 177151b19ebeSPaolo Bonzini VirtQueueElement *elem; 17726e790746SPaolo Bonzini int len, total; 177351b19ebeSPaolo Bonzini const struct iovec *sg; 17746e790746SPaolo Bonzini 17756e790746SPaolo Bonzini total = 0; 17766e790746SPaolo Bonzini 1777bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1778bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1779bedd7e93SJason Wang err = size; 1780bedd7e93SJason Wang goto err; 1781bedd7e93SJason Wang } 1782bedd7e93SJason Wang 178351b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 178451b19ebeSPaolo Bonzini if (!elem) { 1785ba10b9c0SGreg Kurz if (i) { 1786ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 17876e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1788019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1789019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 17906e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1791019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1792019a3edbSGerd Hoffmann vdev->guest_features); 1793ba10b9c0SGreg Kurz } 1794bedd7e93SJason Wang err = -1; 1795bedd7e93SJason Wang goto err; 17966e790746SPaolo Bonzini } 17976e790746SPaolo Bonzini 179851b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1799ba10b9c0SGreg Kurz virtio_error(vdev, 1800ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1801ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1802ba10b9c0SGreg Kurz g_free(elem); 1803bedd7e93SJason Wang err = -1; 1804bedd7e93SJason Wang goto err; 18056e790746SPaolo Bonzini } 18066e790746SPaolo Bonzini 180751b19ebeSPaolo Bonzini sg = elem->in_sg; 18086e790746SPaolo Bonzini if (i == 0) { 18096e790746SPaolo Bonzini assert(offset == 0); 18106e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 18116e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 181251b19ebeSPaolo Bonzini sg, elem->in_num, 18136e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 18146e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 18156e790746SPaolo Bonzini } 18166e790746SPaolo Bonzini 181751b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1818e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1819e22f0603SYuri Benditovich offset = sizeof(mhdr); 1820e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1821e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 1822e22f0603SYuri Benditovich } 18236e790746SPaolo Bonzini offset = n->host_hdr_len; 18246e790746SPaolo Bonzini total += n->guest_hdr_len; 18256e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 18266e790746SPaolo Bonzini } else { 18276e790746SPaolo Bonzini guest_offset = 0; 18286e790746SPaolo Bonzini } 18296e790746SPaolo Bonzini 18306e790746SPaolo Bonzini /* copy in packet. ugh */ 183151b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 18326e790746SPaolo Bonzini buf + offset, size - offset); 18336e790746SPaolo Bonzini total += len; 18346e790746SPaolo Bonzini offset += len; 18356e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 18366e790746SPaolo Bonzini * must have consumed the complete packet. 18376e790746SPaolo Bonzini * Otherwise, drop it. */ 18386e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 183927e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 184051b19ebeSPaolo Bonzini g_free(elem); 1841bedd7e93SJason Wang err = size; 1842bedd7e93SJason Wang goto err; 18436e790746SPaolo Bonzini } 18446e790746SPaolo Bonzini 1845bedd7e93SJason Wang elems[i] = elem; 1846bedd7e93SJason Wang lens[i] = total; 1847bedd7e93SJason Wang i++; 18486e790746SPaolo Bonzini } 18496e790746SPaolo Bonzini 18506e790746SPaolo Bonzini if (mhdr_cnt) { 18511399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 18526e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 18536e790746SPaolo Bonzini 0, 18546e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 18556e790746SPaolo Bonzini } 18566e790746SPaolo Bonzini 1857bedd7e93SJason Wang for (j = 0; j < i; j++) { 1858bedd7e93SJason Wang /* signal other side */ 1859bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 1860bedd7e93SJason Wang g_free(elems[j]); 1861bedd7e93SJason Wang } 1862bedd7e93SJason Wang 18636e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 186417a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 18656e790746SPaolo Bonzini 18666e790746SPaolo Bonzini return size; 1867bedd7e93SJason Wang 1868bedd7e93SJason Wang err: 1869bedd7e93SJason Wang for (j = 0; j < i; j++) { 1870bedd7e93SJason Wang g_free(elems[j]); 1871bedd7e93SJason Wang } 1872bedd7e93SJason Wang 1873bedd7e93SJason Wang return err; 18746e790746SPaolo Bonzini } 18756e790746SPaolo Bonzini 18762974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 187797cd965cSPaolo Bonzini size_t size) 187897cd965cSPaolo Bonzini { 1879068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 188097cd965cSPaolo Bonzini 18814474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 188297cd965cSPaolo Bonzini } 188397cd965cSPaolo Bonzini 18842974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 18852974e916SYuri Benditovich const uint8_t *buf, 18862974e916SYuri Benditovich VirtioNetRscUnit *unit) 18872974e916SYuri Benditovich { 18882974e916SYuri Benditovich uint16_t ip_hdrlen; 18892974e916SYuri Benditovich struct ip_header *ip; 18902974e916SYuri Benditovich 18912974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 18922974e916SYuri Benditovich + sizeof(struct eth_header)); 18932974e916SYuri Benditovich unit->ip = (void *)ip; 18942974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 18952974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 18962974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 18972974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 18982974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 18992974e916SYuri Benditovich } 19002974e916SYuri Benditovich 19012974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 19022974e916SYuri Benditovich const uint8_t *buf, 19032974e916SYuri Benditovich VirtioNetRscUnit *unit) 19042974e916SYuri Benditovich { 19052974e916SYuri Benditovich struct ip6_header *ip6; 19062974e916SYuri Benditovich 19072974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 19082974e916SYuri Benditovich + sizeof(struct eth_header)); 19092974e916SYuri Benditovich unit->ip = ip6; 19102974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 191178ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 19122974e916SYuri Benditovich + sizeof(struct ip6_header)); 19132974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19142974e916SYuri Benditovich 19152974e916SYuri Benditovich /* There is a difference between payload lenght in ipv4 and v6, 19162974e916SYuri Benditovich ip header is excluded in ipv6 */ 19172974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 19182974e916SYuri Benditovich } 19192974e916SYuri Benditovich 19202974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 19212974e916SYuri Benditovich VirtioNetRscSeg *seg) 19222974e916SYuri Benditovich { 19232974e916SYuri Benditovich int ret; 1924dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 19252974e916SYuri Benditovich 1926dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 19272974e916SYuri Benditovich h->flags = 0; 19282974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 19292974e916SYuri Benditovich 19302974e916SYuri Benditovich if (seg->is_coalesced) { 1931dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 1932dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 19332974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 19342974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 19352974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 19362974e916SYuri Benditovich } else { 19372974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 19382974e916SYuri Benditovich } 19392974e916SYuri Benditovich } 19402974e916SYuri Benditovich 19412974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 19422974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 19432974e916SYuri Benditovich g_free(seg->buf); 19442974e916SYuri Benditovich g_free(seg); 19452974e916SYuri Benditovich 19462974e916SYuri Benditovich return ret; 19472974e916SYuri Benditovich } 19482974e916SYuri Benditovich 19492974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 19502974e916SYuri Benditovich { 19512974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 19522974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 19532974e916SYuri Benditovich 19542974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 19552974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 19562974e916SYuri Benditovich chain->stat.purge_failed++; 19572974e916SYuri Benditovich continue; 19582974e916SYuri Benditovich } 19592974e916SYuri Benditovich } 19602974e916SYuri Benditovich 19612974e916SYuri Benditovich chain->stat.timer++; 19622974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 19632974e916SYuri Benditovich timer_mod(chain->drain_timer, 19642974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 19652974e916SYuri Benditovich } 19662974e916SYuri Benditovich } 19672974e916SYuri Benditovich 19682974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 19692974e916SYuri Benditovich { 19702974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 19712974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 19722974e916SYuri Benditovich 19732974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 19742974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 19752974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 19762974e916SYuri Benditovich g_free(seg->buf); 19772974e916SYuri Benditovich g_free(seg); 19782974e916SYuri Benditovich } 19792974e916SYuri Benditovich 19802974e916SYuri Benditovich timer_free(chain->drain_timer); 19812974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 19822974e916SYuri Benditovich g_free(chain); 19832974e916SYuri Benditovich } 19842974e916SYuri Benditovich } 19852974e916SYuri Benditovich 19862974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 19872974e916SYuri Benditovich NetClientState *nc, 19882974e916SYuri Benditovich const uint8_t *buf, size_t size) 19892974e916SYuri Benditovich { 19902974e916SYuri Benditovich uint16_t hdr_len; 19912974e916SYuri Benditovich VirtioNetRscSeg *seg; 19922974e916SYuri Benditovich 19932974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 19942974e916SYuri Benditovich seg = g_malloc(sizeof(VirtioNetRscSeg)); 19952974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 19962974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 19972974e916SYuri Benditovich memcpy(seg->buf, buf, size); 19982974e916SYuri Benditovich seg->size = size; 19992974e916SYuri Benditovich seg->packets = 1; 20002974e916SYuri Benditovich seg->dup_ack = 0; 20012974e916SYuri Benditovich seg->is_coalesced = 0; 20022974e916SYuri Benditovich seg->nc = nc; 20032974e916SYuri Benditovich 20042974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 20052974e916SYuri Benditovich chain->stat.cache++; 20062974e916SYuri Benditovich 20072974e916SYuri Benditovich switch (chain->proto) { 20082974e916SYuri Benditovich case ETH_P_IP: 20092974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 20102974e916SYuri Benditovich break; 20112974e916SYuri Benditovich case ETH_P_IPV6: 20122974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 20132974e916SYuri Benditovich break; 20142974e916SYuri Benditovich default: 20152974e916SYuri Benditovich g_assert_not_reached(); 20162974e916SYuri Benditovich } 20172974e916SYuri Benditovich } 20182974e916SYuri Benditovich 20192974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 20202974e916SYuri Benditovich VirtioNetRscSeg *seg, 20212974e916SYuri Benditovich const uint8_t *buf, 20222974e916SYuri Benditovich struct tcp_header *n_tcp, 20232974e916SYuri Benditovich struct tcp_header *o_tcp) 20242974e916SYuri Benditovich { 20252974e916SYuri Benditovich uint32_t nack, oack; 20262974e916SYuri Benditovich uint16_t nwin, owin; 20272974e916SYuri Benditovich 20282974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 20292974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 20302974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 20312974e916SYuri Benditovich owin = htons(o_tcp->th_win); 20322974e916SYuri Benditovich 20332974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 20342974e916SYuri Benditovich chain->stat.ack_out_of_win++; 20352974e916SYuri Benditovich return RSC_FINAL; 20362974e916SYuri Benditovich } else if (nack == oack) { 20372974e916SYuri Benditovich /* duplicated ack or window probe */ 20382974e916SYuri Benditovich if (nwin == owin) { 20392974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 20402974e916SYuri Benditovich chain->stat.dup_ack++; 20412974e916SYuri Benditovich return RSC_FINAL; 20422974e916SYuri Benditovich } else { 20432974e916SYuri Benditovich /* Coalesce window update */ 20442974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 20452974e916SYuri Benditovich chain->stat.win_update++; 20462974e916SYuri Benditovich return RSC_COALESCE; 20472974e916SYuri Benditovich } 20482974e916SYuri Benditovich } else { 20492974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 20502974e916SYuri Benditovich chain->stat.pure_ack++; 20512974e916SYuri Benditovich return RSC_FINAL; 20522974e916SYuri Benditovich } 20532974e916SYuri Benditovich } 20542974e916SYuri Benditovich 20552974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 20562974e916SYuri Benditovich VirtioNetRscSeg *seg, 20572974e916SYuri Benditovich const uint8_t *buf, 20582974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 20592974e916SYuri Benditovich { 20602974e916SYuri Benditovich void *data; 20612974e916SYuri Benditovich uint16_t o_ip_len; 20622974e916SYuri Benditovich uint32_t nseq, oseq; 20632974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 20642974e916SYuri Benditovich 20652974e916SYuri Benditovich o_unit = &seg->unit; 20662974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 20672974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 20682974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 20692974e916SYuri Benditovich 20702974e916SYuri Benditovich /* out of order or retransmitted. */ 20712974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 20722974e916SYuri Benditovich chain->stat.data_out_of_win++; 20732974e916SYuri Benditovich return RSC_FINAL; 20742974e916SYuri Benditovich } 20752974e916SYuri Benditovich 20762974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 20772974e916SYuri Benditovich if (nseq == oseq) { 20782974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 20792974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 20802974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 20812974e916SYuri Benditovich goto coalesce; 20822974e916SYuri Benditovich } else { 20832974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 20842974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 20852974e916SYuri Benditovich } 20862974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 20872974e916SYuri Benditovich /* Not a consistent packet, out of order */ 20882974e916SYuri Benditovich chain->stat.data_out_of_order++; 20892974e916SYuri Benditovich return RSC_FINAL; 20902974e916SYuri Benditovich } else { 20912974e916SYuri Benditovich coalesce: 20922974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 20932974e916SYuri Benditovich chain->stat.over_size++; 20942974e916SYuri Benditovich return RSC_FINAL; 20952974e916SYuri Benditovich } 20962974e916SYuri Benditovich 20972974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 20982974e916SYuri Benditovich so use the field value to update and record the new data len */ 20992974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 21002974e916SYuri Benditovich 21012974e916SYuri Benditovich /* update field in ip header */ 21022974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 21032974e916SYuri Benditovich 21042974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 21052974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 21062974e916SYuri Benditovich guest (only if it uses RSC feature). */ 21072974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 21082974e916SYuri Benditovich 21092974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 21102974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 21112974e916SYuri Benditovich 21122974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 21132974e916SYuri Benditovich seg->size += n_unit->payload; 21142974e916SYuri Benditovich seg->packets++; 21152974e916SYuri Benditovich chain->stat.coalesced++; 21162974e916SYuri Benditovich return RSC_COALESCE; 21172974e916SYuri Benditovich } 21182974e916SYuri Benditovich } 21192974e916SYuri Benditovich 21202974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 21212974e916SYuri Benditovich VirtioNetRscSeg *seg, 21222974e916SYuri Benditovich const uint8_t *buf, size_t size, 21232974e916SYuri Benditovich VirtioNetRscUnit *unit) 21242974e916SYuri Benditovich { 21252974e916SYuri Benditovich struct ip_header *ip1, *ip2; 21262974e916SYuri Benditovich 21272974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 21282974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 21292974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 21302974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 21312974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 21322974e916SYuri Benditovich chain->stat.no_match++; 21332974e916SYuri Benditovich return RSC_NO_MATCH; 21342974e916SYuri Benditovich } 21352974e916SYuri Benditovich 21362974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 21372974e916SYuri Benditovich } 21382974e916SYuri Benditovich 21392974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 21402974e916SYuri Benditovich VirtioNetRscSeg *seg, 21412974e916SYuri Benditovich const uint8_t *buf, size_t size, 21422974e916SYuri Benditovich VirtioNetRscUnit *unit) 21432974e916SYuri Benditovich { 21442974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 21452974e916SYuri Benditovich 21462974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 21472974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 21482974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 21492974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 21502974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 21512974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 21522974e916SYuri Benditovich chain->stat.no_match++; 21532974e916SYuri Benditovich return RSC_NO_MATCH; 21542974e916SYuri Benditovich } 21552974e916SYuri Benditovich 21562974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 21572974e916SYuri Benditovich } 21582974e916SYuri Benditovich 21592974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 21602974e916SYuri Benditovich * to prevent out of order */ 21612974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 21622974e916SYuri Benditovich struct tcp_header *tcp) 21632974e916SYuri Benditovich { 21642974e916SYuri Benditovich uint16_t tcp_hdr; 21652974e916SYuri Benditovich uint16_t tcp_flag; 21662974e916SYuri Benditovich 21672974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 21682974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 21692974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 21702974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 21712974e916SYuri Benditovich chain->stat.tcp_syn++; 21722974e916SYuri Benditovich return RSC_BYPASS; 21732974e916SYuri Benditovich } 21742974e916SYuri Benditovich 21752974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 21762974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 21772974e916SYuri Benditovich return RSC_FINAL; 21782974e916SYuri Benditovich } 21792974e916SYuri Benditovich 21802974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 21812974e916SYuri Benditovich chain->stat.tcp_all_opt++; 21822974e916SYuri Benditovich return RSC_FINAL; 21832974e916SYuri Benditovich } 21842974e916SYuri Benditovich 21852974e916SYuri Benditovich return RSC_CANDIDATE; 21862974e916SYuri Benditovich } 21872974e916SYuri Benditovich 21882974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 21892974e916SYuri Benditovich NetClientState *nc, 21902974e916SYuri Benditovich const uint8_t *buf, size_t size, 21912974e916SYuri Benditovich VirtioNetRscUnit *unit) 21922974e916SYuri Benditovich { 21932974e916SYuri Benditovich int ret; 21942974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 21952974e916SYuri Benditovich 21962974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 21972974e916SYuri Benditovich chain->stat.empty_cache++; 21982974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 21992974e916SYuri Benditovich timer_mod(chain->drain_timer, 22002974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 22012974e916SYuri Benditovich return size; 22022974e916SYuri Benditovich } 22032974e916SYuri Benditovich 22042974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22052974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 22062974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 22072974e916SYuri Benditovich } else { 22082974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 22092974e916SYuri Benditovich } 22102974e916SYuri Benditovich 22112974e916SYuri Benditovich if (ret == RSC_FINAL) { 22122974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 22132974e916SYuri Benditovich /* Send failed */ 22142974e916SYuri Benditovich chain->stat.final_failed++; 22152974e916SYuri Benditovich return 0; 22162974e916SYuri Benditovich } 22172974e916SYuri Benditovich 22182974e916SYuri Benditovich /* Send current packet */ 22192974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22202974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 22212974e916SYuri Benditovich continue; 22222974e916SYuri Benditovich } else { 22232974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 22242974e916SYuri Benditovich seg->is_coalesced = 1; 22252974e916SYuri Benditovich return size; 22262974e916SYuri Benditovich } 22272974e916SYuri Benditovich } 22282974e916SYuri Benditovich 22292974e916SYuri Benditovich chain->stat.no_match_cache++; 22302974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 22312974e916SYuri Benditovich return size; 22322974e916SYuri Benditovich } 22332974e916SYuri Benditovich 22342974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 22352974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 22362974e916SYuri Benditovich NetClientState *nc, 22372974e916SYuri Benditovich const uint8_t *buf, size_t size, 22382974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 22392974e916SYuri Benditovich uint16_t tcp_port) 22402974e916SYuri Benditovich { 22412974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 22422974e916SYuri Benditovich uint32_t ppair1, ppair2; 22432974e916SYuri Benditovich 22442974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 22452974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22462974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 22472974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 22482974e916SYuri Benditovich || (ppair1 != ppair2)) { 22492974e916SYuri Benditovich continue; 22502974e916SYuri Benditovich } 22512974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 22522974e916SYuri Benditovich chain->stat.drain_failed++; 22532974e916SYuri Benditovich } 22542974e916SYuri Benditovich 22552974e916SYuri Benditovich break; 22562974e916SYuri Benditovich } 22572974e916SYuri Benditovich 22582974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22592974e916SYuri Benditovich } 22602974e916SYuri Benditovich 22612974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 22622974e916SYuri Benditovich struct ip_header *ip, 22632974e916SYuri Benditovich const uint8_t *buf, size_t size) 22642974e916SYuri Benditovich { 22652974e916SYuri Benditovich uint16_t ip_len; 22662974e916SYuri Benditovich 22672974e916SYuri Benditovich /* Not an ipv4 packet */ 22682974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 22692974e916SYuri Benditovich chain->stat.ip_option++; 22702974e916SYuri Benditovich return RSC_BYPASS; 22712974e916SYuri Benditovich } 22722974e916SYuri Benditovich 22732974e916SYuri Benditovich /* Don't handle packets with ip option */ 22742974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 22752974e916SYuri Benditovich chain->stat.ip_option++; 22762974e916SYuri Benditovich return RSC_BYPASS; 22772974e916SYuri Benditovich } 22782974e916SYuri Benditovich 22792974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 22802974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 22812974e916SYuri Benditovich return RSC_BYPASS; 22822974e916SYuri Benditovich } 22832974e916SYuri Benditovich 22842974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 22852974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 22862974e916SYuri Benditovich chain->stat.ip_frag++; 22872974e916SYuri Benditovich return RSC_BYPASS; 22882974e916SYuri Benditovich } 22892974e916SYuri Benditovich 22902974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 22912974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 22922974e916SYuri Benditovich chain->stat.ip_ecn++; 22932974e916SYuri Benditovich return RSC_BYPASS; 22942974e916SYuri Benditovich } 22952974e916SYuri Benditovich 22962974e916SYuri Benditovich ip_len = htons(ip->ip_len); 22972974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 22982974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 22992974e916SYuri Benditovich sizeof(struct eth_header))) { 23002974e916SYuri Benditovich chain->stat.ip_hacked++; 23012974e916SYuri Benditovich return RSC_BYPASS; 23022974e916SYuri Benditovich } 23032974e916SYuri Benditovich 23042974e916SYuri Benditovich return RSC_CANDIDATE; 23052974e916SYuri Benditovich } 23062974e916SYuri Benditovich 23072974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 23082974e916SYuri Benditovich NetClientState *nc, 23092974e916SYuri Benditovich const uint8_t *buf, size_t size) 23102974e916SYuri Benditovich { 23112974e916SYuri Benditovich int32_t ret; 23122974e916SYuri Benditovich uint16_t hdr_len; 23132974e916SYuri Benditovich VirtioNetRscUnit unit; 23142974e916SYuri Benditovich 23152974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 23162974e916SYuri Benditovich 23172974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 23182974e916SYuri Benditovich + sizeof(struct tcp_header))) { 23192974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23202974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23212974e916SYuri Benditovich } 23222974e916SYuri Benditovich 23232974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 23242974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 23252974e916SYuri Benditovich != RSC_CANDIDATE) { 23262974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23272974e916SYuri Benditovich } 23282974e916SYuri Benditovich 23292974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 23302974e916SYuri Benditovich if (ret == RSC_BYPASS) { 23312974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23322974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 23332974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 23342974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 23352974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 23362974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 23372974e916SYuri Benditovich } 23382974e916SYuri Benditovich 23392974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 23402974e916SYuri Benditovich } 23412974e916SYuri Benditovich 23422974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 23432974e916SYuri Benditovich struct ip6_header *ip6, 23442974e916SYuri Benditovich const uint8_t *buf, size_t size) 23452974e916SYuri Benditovich { 23462974e916SYuri Benditovich uint16_t ip_len; 23472974e916SYuri Benditovich 23482974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 23492974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 23502974e916SYuri Benditovich return RSC_BYPASS; 23512974e916SYuri Benditovich } 23522974e916SYuri Benditovich 23532974e916SYuri Benditovich /* Both option and protocol is checked in this */ 23542974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 23552974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23562974e916SYuri Benditovich return RSC_BYPASS; 23572974e916SYuri Benditovich } 23582974e916SYuri Benditovich 23592974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 23602974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 23612974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 23622974e916SYuri Benditovich - sizeof(struct ip6_header))) { 23632974e916SYuri Benditovich chain->stat.ip_hacked++; 23642974e916SYuri Benditovich return RSC_BYPASS; 23652974e916SYuri Benditovich } 23662974e916SYuri Benditovich 23672974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 23682974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 23692974e916SYuri Benditovich chain->stat.ip_ecn++; 23702974e916SYuri Benditovich return RSC_BYPASS; 23712974e916SYuri Benditovich } 23722974e916SYuri Benditovich 23732974e916SYuri Benditovich return RSC_CANDIDATE; 23742974e916SYuri Benditovich } 23752974e916SYuri Benditovich 23762974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 23772974e916SYuri Benditovich const uint8_t *buf, size_t size) 23782974e916SYuri Benditovich { 23792974e916SYuri Benditovich int32_t ret; 23802974e916SYuri Benditovich uint16_t hdr_len; 23812974e916SYuri Benditovich VirtioNetRscChain *chain; 23822974e916SYuri Benditovich VirtioNetRscUnit unit; 23832974e916SYuri Benditovich 23842974e916SYuri Benditovich chain = (VirtioNetRscChain *)opq; 23852974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 23862974e916SYuri Benditovich 23872974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 23882974e916SYuri Benditovich + sizeof(tcp_header))) { 23892974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23902974e916SYuri Benditovich } 23912974e916SYuri Benditovich 23922974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 23932974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 23942974e916SYuri Benditovich unit.ip, buf, size)) { 23952974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23962974e916SYuri Benditovich } 23972974e916SYuri Benditovich 23982974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 23992974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24002974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24012974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24022974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24032974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 24042974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 24052974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 24062974e916SYuri Benditovich + sizeof(struct ip6_header)); 24072974e916SYuri Benditovich } 24082974e916SYuri Benditovich 24092974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 24102974e916SYuri Benditovich } 24112974e916SYuri Benditovich 24122974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 24132974e916SYuri Benditovich NetClientState *nc, 24142974e916SYuri Benditovich uint16_t proto) 24152974e916SYuri Benditovich { 24162974e916SYuri Benditovich VirtioNetRscChain *chain; 24172974e916SYuri Benditovich 24182974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 24192974e916SYuri Benditovich return NULL; 24202974e916SYuri Benditovich } 24212974e916SYuri Benditovich 24222974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 24232974e916SYuri Benditovich if (chain->proto == proto) { 24242974e916SYuri Benditovich return chain; 24252974e916SYuri Benditovich } 24262974e916SYuri Benditovich } 24272974e916SYuri Benditovich 24282974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 24292974e916SYuri Benditovich chain->n = n; 24302974e916SYuri Benditovich chain->proto = proto; 24312974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 24322974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 24332974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 24342974e916SYuri Benditovich } else { 24352974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 24362974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 24372974e916SYuri Benditovich } 24382974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 24392974e916SYuri Benditovich virtio_net_rsc_purge, chain); 24402974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 24412974e916SYuri Benditovich 24422974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 24432974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 24442974e916SYuri Benditovich 24452974e916SYuri Benditovich return chain; 24462974e916SYuri Benditovich } 24472974e916SYuri Benditovich 24482974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 24492974e916SYuri Benditovich const uint8_t *buf, 24502974e916SYuri Benditovich size_t size) 24512974e916SYuri Benditovich { 24522974e916SYuri Benditovich uint16_t proto; 24532974e916SYuri Benditovich VirtioNetRscChain *chain; 24542974e916SYuri Benditovich struct eth_header *eth; 24552974e916SYuri Benditovich VirtIONet *n; 24562974e916SYuri Benditovich 24572974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 24582974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 24592974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24602974e916SYuri Benditovich } 24612974e916SYuri Benditovich 24622974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 24632974e916SYuri Benditovich proto = htons(eth->h_proto); 24642974e916SYuri Benditovich 24652974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 24662974e916SYuri Benditovich if (chain) { 24672974e916SYuri Benditovich chain->stat.received++; 24682974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 24692974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 24702974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 24712974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 24722974e916SYuri Benditovich } 24732974e916SYuri Benditovich } 24742974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24752974e916SYuri Benditovich } 24762974e916SYuri Benditovich 24772974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 24782974e916SYuri Benditovich size_t size) 24792974e916SYuri Benditovich { 24802974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 24812974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 24822974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 24832974e916SYuri Benditovich } else { 24842974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24852974e916SYuri Benditovich } 24862974e916SYuri Benditovich } 24872974e916SYuri Benditovich 24886e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 24896e790746SPaolo Bonzini 24906e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 24916e790746SPaolo Bonzini { 24926e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 24936e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 249417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 24956e790746SPaolo Bonzini 249651b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 249717a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 24986e790746SPaolo Bonzini 249951b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 250051b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 25016e790746SPaolo Bonzini 25026e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 25036e790746SPaolo Bonzini virtio_net_flush_tx(q); 25046e790746SPaolo Bonzini } 25056e790746SPaolo Bonzini 25066e790746SPaolo Bonzini /* TX */ 25076e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 25086e790746SPaolo Bonzini { 25096e790746SPaolo Bonzini VirtIONet *n = q->n; 251017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 251151b19ebeSPaolo Bonzini VirtQueueElement *elem; 25126e790746SPaolo Bonzini int32_t num_packets = 0; 25136e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 251417a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 25156e790746SPaolo Bonzini return num_packets; 25166e790746SPaolo Bonzini } 25176e790746SPaolo Bonzini 251851b19ebeSPaolo Bonzini if (q->async_tx.elem) { 25196e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 25206e790746SPaolo Bonzini return num_packets; 25216e790746SPaolo Bonzini } 25226e790746SPaolo Bonzini 252351b19ebeSPaolo Bonzini for (;;) { 2524bd89dd98SJason Wang ssize_t ret; 252551b19ebeSPaolo Bonzini unsigned int out_num; 252651b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2527feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 25286e790746SPaolo Bonzini 252951b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 253051b19ebeSPaolo Bonzini if (!elem) { 253151b19ebeSPaolo Bonzini break; 253251b19ebeSPaolo Bonzini } 253351b19ebeSPaolo Bonzini 253451b19ebeSPaolo Bonzini out_num = elem->out_num; 253551b19ebeSPaolo Bonzini out_sg = elem->out_sg; 25366e790746SPaolo Bonzini if (out_num < 1) { 2537fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2538fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2539fa5e56c2SGreg Kurz g_free(elem); 2540fa5e56c2SGreg Kurz return -EINVAL; 25416e790746SPaolo Bonzini } 25426e790746SPaolo Bonzini 2543032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 2544feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 2545feb93f36SJason Wang n->guest_hdr_len) { 2546fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2547fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2548fa5e56c2SGreg Kurz g_free(elem); 2549fa5e56c2SGreg Kurz return -EINVAL; 2550032a74a1SCédric Le Goater } 25511bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2552feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 2553feb93f36SJason Wang sg2[0].iov_base = &mhdr; 2554feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2555feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2556feb93f36SJason Wang out_sg, out_num, 2557feb93f36SJason Wang n->guest_hdr_len, -1); 2558feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2559feb93f36SJason Wang goto drop; 2560032a74a1SCédric Le Goater } 2561feb93f36SJason Wang out_num += 1; 2562feb93f36SJason Wang out_sg = sg2; 2563feb93f36SJason Wang } 2564feb93f36SJason Wang } 25656e790746SPaolo Bonzini /* 25666e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 25676e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 25686e790746SPaolo Bonzini * that host is interested in. 25696e790746SPaolo Bonzini */ 25706e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 25716e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 25726e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 25736e790746SPaolo Bonzini out_sg, out_num, 25746e790746SPaolo Bonzini 0, n->host_hdr_len); 25756e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 25766e790746SPaolo Bonzini out_sg, out_num, 25776e790746SPaolo Bonzini n->guest_hdr_len, -1); 25786e790746SPaolo Bonzini out_num = sg_num; 25796e790746SPaolo Bonzini out_sg = sg; 25806e790746SPaolo Bonzini } 25816e790746SPaolo Bonzini 25826e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 25836e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 25846e790746SPaolo Bonzini if (ret == 0) { 25856e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 25866e790746SPaolo Bonzini q->async_tx.elem = elem; 25876e790746SPaolo Bonzini return -EBUSY; 25886e790746SPaolo Bonzini } 25896e790746SPaolo Bonzini 2590feb93f36SJason Wang drop: 259151b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 259217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 259351b19ebeSPaolo Bonzini g_free(elem); 25946e790746SPaolo Bonzini 25956e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 25966e790746SPaolo Bonzini break; 25976e790746SPaolo Bonzini } 25986e790746SPaolo Bonzini } 25996e790746SPaolo Bonzini return num_packets; 26006e790746SPaolo Bonzini } 26016e790746SPaolo Bonzini 26026e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 26036e790746SPaolo Bonzini { 260417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 26056e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 26066e790746SPaolo Bonzini 2607283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2608283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2609283e2c2aSYuri Benditovich return; 2610283e2c2aSYuri Benditovich } 2611283e2c2aSYuri Benditovich 26126e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 261317a0ca55SKONRAD Frederic if (!vdev->vm_running) { 26146e790746SPaolo Bonzini q->tx_waiting = 1; 26156e790746SPaolo Bonzini return; 26166e790746SPaolo Bonzini } 26176e790746SPaolo Bonzini 26186e790746SPaolo Bonzini if (q->tx_waiting) { 26196e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 2620bc72ad67SAlex Bligh timer_del(q->tx_timer); 26216e790746SPaolo Bonzini q->tx_waiting = 0; 2622fa5e56c2SGreg Kurz if (virtio_net_flush_tx(q) == -EINVAL) { 2623fa5e56c2SGreg Kurz return; 2624fa5e56c2SGreg Kurz } 26256e790746SPaolo Bonzini } else { 2626bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2627bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 26286e790746SPaolo Bonzini q->tx_waiting = 1; 26296e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 26306e790746SPaolo Bonzini } 26316e790746SPaolo Bonzini } 26326e790746SPaolo Bonzini 26336e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 26346e790746SPaolo Bonzini { 263517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 26366e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 26376e790746SPaolo Bonzini 2638283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2639283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2640283e2c2aSYuri Benditovich return; 2641283e2c2aSYuri Benditovich } 2642283e2c2aSYuri Benditovich 26436e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 26446e790746SPaolo Bonzini return; 26456e790746SPaolo Bonzini } 26466e790746SPaolo Bonzini q->tx_waiting = 1; 26476e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 264817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 26496e790746SPaolo Bonzini return; 26506e790746SPaolo Bonzini } 26516e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 26526e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 26536e790746SPaolo Bonzini } 26546e790746SPaolo Bonzini 26556e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 26566e790746SPaolo Bonzini { 26576e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 26586e790746SPaolo Bonzini VirtIONet *n = q->n; 265917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2660e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2661e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2662e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2663e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2664e8bcf842SMichael S. Tsirkin return; 2665e8bcf842SMichael S. Tsirkin } 26666e790746SPaolo Bonzini 26676e790746SPaolo Bonzini q->tx_waiting = 0; 26686e790746SPaolo Bonzini 26696e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 267017a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 26716e790746SPaolo Bonzini return; 267217a0ca55SKONRAD Frederic } 26736e790746SPaolo Bonzini 26746e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 26756e790746SPaolo Bonzini virtio_net_flush_tx(q); 26766e790746SPaolo Bonzini } 26776e790746SPaolo Bonzini 26786e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 26796e790746SPaolo Bonzini { 26806e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 26816e790746SPaolo Bonzini VirtIONet *n = q->n; 268217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 26836e790746SPaolo Bonzini int32_t ret; 26846e790746SPaolo Bonzini 2685e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2686e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2687e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2688e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2689e8bcf842SMichael S. Tsirkin return; 2690e8bcf842SMichael S. Tsirkin } 26916e790746SPaolo Bonzini 26926e790746SPaolo Bonzini q->tx_waiting = 0; 26936e790746SPaolo Bonzini 26946e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 269517a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 26966e790746SPaolo Bonzini return; 269717a0ca55SKONRAD Frederic } 26986e790746SPaolo Bonzini 26996e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2700fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2701fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2702fa5e56c2SGreg Kurz * broken */ 27036e790746SPaolo Bonzini } 27046e790746SPaolo Bonzini 27056e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 27066e790746SPaolo Bonzini * more coming and immediately reschedule */ 27076e790746SPaolo Bonzini if (ret >= n->tx_burst) { 27086e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27096e790746SPaolo Bonzini q->tx_waiting = 1; 27106e790746SPaolo Bonzini return; 27116e790746SPaolo Bonzini } 27126e790746SPaolo Bonzini 27136e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 27146e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 27156e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 27166e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2717fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2718fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2719fa5e56c2SGreg Kurz return; 2720fa5e56c2SGreg Kurz } else if (ret > 0) { 27216e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 27226e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27236e790746SPaolo Bonzini q->tx_waiting = 1; 27246e790746SPaolo Bonzini } 27256e790746SPaolo Bonzini } 27266e790746SPaolo Bonzini 2727f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2728f9d6dbf0SWen Congyang { 2729f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2730f9d6dbf0SWen Congyang 27311c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 27321c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 27339b02e161SWei Wang 2734f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2735f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 27369b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 27379b02e161SWei Wang virtio_net_handle_tx_timer); 2738f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2739f9d6dbf0SWen Congyang virtio_net_tx_timer, 2740f9d6dbf0SWen Congyang &n->vqs[index]); 2741f9d6dbf0SWen Congyang } else { 2742f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 27439b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 27449b02e161SWei Wang virtio_net_handle_tx_bh); 2745f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 2746f9d6dbf0SWen Congyang } 2747f9d6dbf0SWen Congyang 2748f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2749f9d6dbf0SWen Congyang n->vqs[index].n = n; 2750f9d6dbf0SWen Congyang } 2751f9d6dbf0SWen Congyang 2752f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2753f9d6dbf0SWen Congyang { 2754f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2755f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2756f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2757f9d6dbf0SWen Congyang 2758f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2759f9d6dbf0SWen Congyang 2760f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2761f9d6dbf0SWen Congyang if (q->tx_timer) { 2762f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2763f989c30cSYunjian Wang q->tx_timer = NULL; 2764f9d6dbf0SWen Congyang } else { 2765f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2766f989c30cSYunjian Wang q->tx_bh = NULL; 2767f9d6dbf0SWen Congyang } 2768f989c30cSYunjian Wang q->tx_waiting = 0; 2769f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2770f9d6dbf0SWen Congyang } 2771f9d6dbf0SWen Congyang 2772441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 2773f9d6dbf0SWen Congyang { 2774f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2775f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2776441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 2777f9d6dbf0SWen Congyang int i; 2778f9d6dbf0SWen Congyang 2779f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2780f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2781f9d6dbf0SWen Congyang 2782f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 2783f9d6dbf0SWen Congyang return; 2784f9d6dbf0SWen Congyang } 2785f9d6dbf0SWen Congyang 2786f9d6dbf0SWen Congyang /* 2787f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 2788f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 278920f86a75SYuval Shaia * and then we only enter one of the following two loops. 2790f9d6dbf0SWen Congyang */ 2791f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 2792f9d6dbf0SWen Congyang 2793f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 2794f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 2795f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 2796f9d6dbf0SWen Congyang } 2797f9d6dbf0SWen Congyang 2798f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 2799f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 2800f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 2801f9d6dbf0SWen Congyang } 2802f9d6dbf0SWen Congyang 2803f9d6dbf0SWen Congyang /* add ctrl_vq last */ 2804f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 2805f9d6dbf0SWen Congyang } 2806f9d6dbf0SWen Congyang 2807ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 28086e790746SPaolo Bonzini { 2809441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 2810f9d6dbf0SWen Congyang 28116e790746SPaolo Bonzini n->multiqueue = multiqueue; 2812441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 28136e790746SPaolo Bonzini 2814441537f1SJason Wang virtio_net_set_queue_pairs(n); 28156e790746SPaolo Bonzini } 28166e790746SPaolo Bonzini 2817982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 2818037dab2fSGreg Kurz { 2819982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 2820982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 2821037dab2fSGreg Kurz int i, link_down; 2822037dab2fSGreg Kurz 28239d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 2824982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 282595129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 2826e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 2827e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 2828e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 28296e790746SPaolo Bonzini 28306e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 2831982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 28326e790746SPaolo Bonzini n->mac_table.in_use = 0; 28336e790746SPaolo Bonzini } 28346e790746SPaolo Bonzini 2835982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 28366c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 28376c666823SMichael S. Tsirkin } 28386c666823SMichael S. Tsirkin 28397788c3f2SMikhail Sennikovsky /* 28407788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 28417788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 28427788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 28437788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 28447788c3f2SMikhail Sennikovsky */ 28457788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 28466c666823SMichael S. Tsirkin 2847441537f1SJason Wang virtio_net_set_queue_pairs(n); 28486e790746SPaolo Bonzini 28496e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 28506e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 28516e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 28526e790746SPaolo Bonzini break; 28536e790746SPaolo Bonzini } 28546e790746SPaolo Bonzini } 28556e790746SPaolo Bonzini n->mac_table.first_multi = i; 28566e790746SPaolo Bonzini 28576e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 28586e790746SPaolo Bonzini * to link status bit in n->status */ 28596e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 2860441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 28616e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 28626e790746SPaolo Bonzini } 28636e790746SPaolo Bonzini 28646c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 28656c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 28669d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 28679d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 28689d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 28699d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 28709d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 28719d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 28729d8c6a25SDr. David Alan Gilbert } else { 2873944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 28749d8c6a25SDr. David Alan Gilbert } 28756c666823SMichael S. Tsirkin } 28766c666823SMichael S. Tsirkin 2877e41b7114SYuri Benditovich if (n->rss_data.enabled) { 28780145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 28790145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 28800145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 28810145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 28820145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS for vhost"); 28830145c393SAndrew Melnychenko } else { 28840145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS - " 28850145c393SAndrew Melnychenko "fallback to software RSS"); 28860145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 28870145c393SAndrew Melnychenko } 28880145c393SAndrew Melnychenko } 28890145c393SAndrew Melnychenko } 28900145c393SAndrew Melnychenko 2891e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 2892e41b7114SYuri Benditovich n->rss_data.indirections_len, 2893e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 2894e41b7114SYuri Benditovich } else { 2895e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 2896e41b7114SYuri Benditovich } 28976e790746SPaolo Bonzini return 0; 28986e790746SPaolo Bonzini } 28996e790746SPaolo Bonzini 29007788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 29017788c3f2SMikhail Sennikovsky { 29027788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 29037788c3f2SMikhail Sennikovsky /* 29047788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 29057788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 29067788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 29077788c3f2SMikhail Sennikovsky */ 29087788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 29097788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 29107788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 29117788c3f2SMikhail Sennikovsky } 29127788c3f2SMikhail Sennikovsky 29137788c3f2SMikhail Sennikovsky return 0; 29147788c3f2SMikhail Sennikovsky } 29157788c3f2SMikhail Sennikovsky 2916982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 2917982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 2918982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 2919982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2920982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 2921982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2922982b78c5SDr. David Alan Gilbert }, 2923982b78c5SDr. David Alan Gilbert }; 2924982b78c5SDr. David Alan Gilbert 2925441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 2926982b78c5SDr. David Alan Gilbert { 2927441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 2928982b78c5SDr. David Alan Gilbert } 2929982b78c5SDr. David Alan Gilbert 2930982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 2931982b78c5SDr. David Alan Gilbert { 2932982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 2933982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 2934982b78c5SDr. David Alan Gilbert } 2935982b78c5SDr. David Alan Gilbert 2936982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 2937982b78c5SDr. David Alan Gilbert { 2938982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 2939982b78c5SDr. David Alan Gilbert } 2940982b78c5SDr. David Alan Gilbert 2941982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 2942982b78c5SDr. David Alan Gilbert { 2943982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 2944982b78c5SDr. David Alan Gilbert } 2945982b78c5SDr. David Alan Gilbert 2946982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 2947982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 2948982b78c5SDr. David Alan Gilbert */ 2949982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 2950982b78c5SDr. David Alan Gilbert VirtIONet *parent; 2951982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 2952441537f1SJason Wang uint16_t curr_queue_pairs_1; 2953982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 2954982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 2955982b78c5SDr. David Alan Gilbert }; 2956982b78c5SDr. David Alan Gilbert 2957982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 2958441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 2959982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 2960982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 2961982b78c5SDr. David Alan Gilbert */ 2962982b78c5SDr. David Alan Gilbert 296344b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 2964982b78c5SDr. David Alan Gilbert { 2965982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2966982b78c5SDr. David Alan Gilbert 2967982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 2968441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 2969441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 2970441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 2971982b78c5SDr. David Alan Gilbert } 297244b1ff31SDr. David Alan Gilbert 297344b1ff31SDr. David Alan Gilbert return 0; 2974982b78c5SDr. David Alan Gilbert } 2975982b78c5SDr. David Alan Gilbert 2976982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 2977982b78c5SDr. David Alan Gilbert { 2978982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2979982b78c5SDr. David Alan Gilbert 2980982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 2981982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 2982982b78c5SDr. David Alan Gilbert 2983441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 2984441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 2985441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 2986982b78c5SDr. David Alan Gilbert 2987982b78c5SDr. David Alan Gilbert return -EINVAL; 2988982b78c5SDr. David Alan Gilbert } 2989982b78c5SDr. David Alan Gilbert 2990982b78c5SDr. David Alan Gilbert return 0; /* all good */ 2991982b78c5SDr. David Alan Gilbert } 2992982b78c5SDr. David Alan Gilbert 2993982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 2994982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 2995982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 2996982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 2997982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2998982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 2999441537f1SJason Wang curr_queue_pairs_1, 3000982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3001982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3002982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3003982b78c5SDr. David Alan Gilbert }, 3004982b78c5SDr. David Alan Gilbert }; 3005982b78c5SDr. David Alan Gilbert 3006982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3007982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3008982b78c5SDr. David Alan Gilbert */ 3009982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3010982b78c5SDr. David Alan Gilbert { 3011982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3012982b78c5SDr. David Alan Gilbert 3013982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3014982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3015982b78c5SDr. David Alan Gilbert return -EINVAL; 3016982b78c5SDr. David Alan Gilbert } 3017982b78c5SDr. David Alan Gilbert 3018982b78c5SDr. David Alan Gilbert return 0; 3019982b78c5SDr. David Alan Gilbert } 3020982b78c5SDr. David Alan Gilbert 302144b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3022982b78c5SDr. David Alan Gilbert { 3023982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3024982b78c5SDr. David Alan Gilbert 3025982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 302644b1ff31SDr. David Alan Gilbert 302744b1ff31SDr. David Alan Gilbert return 0; 3028982b78c5SDr. David Alan Gilbert } 3029982b78c5SDr. David Alan Gilbert 3030982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3031982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3032982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3033982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 3034982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3035982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3036982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3037982b78c5SDr. David Alan Gilbert }, 3038982b78c5SDr. David Alan Gilbert }; 3039982b78c5SDr. David Alan Gilbert 3040982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3041982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3042982b78c5SDr. David Alan Gilbert */ 3043982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3044982b78c5SDr. David Alan Gilbert { 3045982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3046982b78c5SDr. David Alan Gilbert 3047982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3048982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3049982b78c5SDr. David Alan Gilbert return -EINVAL; 3050982b78c5SDr. David Alan Gilbert } 3051982b78c5SDr. David Alan Gilbert 3052982b78c5SDr. David Alan Gilbert return 0; 3053982b78c5SDr. David Alan Gilbert } 3054982b78c5SDr. David Alan Gilbert 305544b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3056982b78c5SDr. David Alan Gilbert { 3057982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3058982b78c5SDr. David Alan Gilbert 3059982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 306044b1ff31SDr. David Alan Gilbert 306144b1ff31SDr. David Alan Gilbert return 0; 3062982b78c5SDr. David Alan Gilbert } 3063982b78c5SDr. David Alan Gilbert 3064982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3065982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3066982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3067982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 3068982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3069982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3070982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3071982b78c5SDr. David Alan Gilbert }, 3072982b78c5SDr. David Alan Gilbert }; 3073982b78c5SDr. David Alan Gilbert 3074e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3075e41b7114SYuri Benditovich { 3076e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3077e41b7114SYuri Benditovich } 3078e41b7114SYuri Benditovich 3079e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3080e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3081e41b7114SYuri Benditovich .version_id = 1, 3082e41b7114SYuri Benditovich .minimum_version_id = 1, 3083e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 3084e41b7114SYuri Benditovich .fields = (VMStateField[]) { 3085e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3086e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3087e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3088e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3089e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3090e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3091e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3092e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3093e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3094e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3095e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3096e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3097e41b7114SYuri Benditovich }, 3098e41b7114SYuri Benditovich }; 3099e41b7114SYuri Benditovich 3100982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3101982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3102982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3103982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3104982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 3105982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3106982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3107982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3108982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3109982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3110982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3111982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3112982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3113982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3114982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3115982b78c5SDr. David Alan Gilbert 3116982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3117982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3118982b78c5SDr. David Alan Gilbert * sets flags in this case. 3119982b78c5SDr. David Alan Gilbert */ 3120982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3121982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3122982b78c5SDr. David Alan Gilbert ETH_ALEN), 3123982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3124982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3125982b78c5SDr. David Alan Gilbert 3126982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3127982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3128982b78c5SDr. David Alan Gilbert * but based on the uint. 3129982b78c5SDr. David Alan Gilbert */ 3130982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3131982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3132982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3133982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3134982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3135982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3136982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3137982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3138982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3139982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3140982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3141441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3142982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3143441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3144982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3145982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3146982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3147982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3148982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3149982b78c5SDr. David Alan Gilbert }, 3150e41b7114SYuri Benditovich .subsections = (const VMStateDescription * []) { 3151e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3152e41b7114SYuri Benditovich NULL 3153e41b7114SYuri Benditovich } 3154982b78c5SDr. David Alan Gilbert }; 3155982b78c5SDr. David Alan Gilbert 31566e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3157f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 31586e790746SPaolo Bonzini .size = sizeof(NICState), 31596e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 31606e790746SPaolo Bonzini .receive = virtio_net_receive, 31616e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3162b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3163b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 31646e790746SPaolo Bonzini }; 31656e790746SPaolo Bonzini 31666e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 31676e790746SPaolo Bonzini { 316817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 31696e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 31706e790746SPaolo Bonzini assert(n->vhost_started); 3171ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 31726e790746SPaolo Bonzini } 31736e790746SPaolo Bonzini 31746e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 31756e790746SPaolo Bonzini bool mask) 31766e790746SPaolo Bonzini { 317717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 31786e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 31796e790746SPaolo Bonzini assert(n->vhost_started); 3180ed8b4afeSNikolay Nikolaev vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 31816e790746SPaolo Bonzini vdev, idx, mask); 31826e790746SPaolo Bonzini } 31836e790746SPaolo Bonzini 3184019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 31856e790746SPaolo Bonzini { 31860cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3187a93e599dSMaxime Coquelin 3188ba550851SStefano Garzarella n->config_size = virtio_feature_get_config_size(feature_sizes, 3189ba550851SStefano Garzarella host_features); 319017ec5a86SKONRAD Frederic } 31916e790746SPaolo Bonzini 31928a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 31938a253ec2SKONRAD Frederic const char *type) 31948a253ec2SKONRAD Frederic { 31958a253ec2SKONRAD Frederic /* 31968a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 31978a253ec2SKONRAD Frederic */ 31988a253ec2SKONRAD Frederic assert(type != NULL); 31998a253ec2SKONRAD Frederic 32008a253ec2SKONRAD Frederic g_free(n->netclient_name); 32018a253ec2SKONRAD Frederic g_free(n->netclient_type); 32028a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 32038a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 32048a253ec2SKONRAD Frederic } 32058a253ec2SKONRAD Frederic 32060e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 32079711cd0dSJens Freimann { 32089711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 32099711cd0dSJens Freimann PCIDevice *pci_dev; 32109711cd0dSJens Freimann Error *err = NULL; 32119711cd0dSJens Freimann 32120e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 32139711cd0dSJens Freimann if (hotplug_ctrl) { 32140e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 32159711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 32160e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 32179711cd0dSJens Freimann if (err) { 32189711cd0dSJens Freimann error_report_err(err); 32199711cd0dSJens Freimann return false; 32209711cd0dSJens Freimann } 32219711cd0dSJens Freimann } else { 32229711cd0dSJens Freimann return false; 32239711cd0dSJens Freimann } 32249711cd0dSJens Freimann return true; 32259711cd0dSJens Freimann } 32269711cd0dSJens Freimann 32270e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 32280e9a65c5SJuan Quintela Error **errp) 32299711cd0dSJens Freimann { 32305a0948d3SMarkus Armbruster Error *err = NULL; 32319711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 32320e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 323378274682SJuan Quintela BusState *primary_bus; 32349711cd0dSJens Freimann 32359711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 32369711cd0dSJens Freimann return true; 32379711cd0dSJens Freimann } 32380e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 323978274682SJuan Quintela if (!primary_bus) { 3240150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 32415a0948d3SMarkus Armbruster return false; 32429711cd0dSJens Freimann } 32430e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3244e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 32450e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 32469711cd0dSJens Freimann if (hotplug_ctrl) { 32470e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 32485a0948d3SMarkus Armbruster if (err) { 32495a0948d3SMarkus Armbruster goto out; 32505a0948d3SMarkus Armbruster } 32510e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 32529711cd0dSJens Freimann } 3253109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3254150ab54aSJens Freimann 3255150ab54aSJens Freimann out: 32565a0948d3SMarkus Armbruster error_propagate(errp, err); 32575a0948d3SMarkus Armbruster return !err; 32589711cd0dSJens Freimann } 32599711cd0dSJens Freimann 326007a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s) 32619711cd0dSJens Freimann { 32629711cd0dSJens Freimann bool should_be_hidden; 32639711cd0dSJens Freimann Error *err = NULL; 326407a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 326507a5d816SJuan Quintela 326607a5d816SJuan Quintela if (!dev) { 326707a5d816SJuan Quintela return; 326807a5d816SJuan Quintela } 32699711cd0dSJens Freimann 3270e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 32719711cd0dSJens Freimann 32724dbac1aeSMarkus Armbruster if (migration_in_setup(s) && !should_be_hidden) { 327307a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 327407a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 327507a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3276e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 32779711cd0dSJens Freimann } else { 32789711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 32799711cd0dSJens Freimann } 32809711cd0dSJens Freimann } else if (migration_has_failed(s)) { 3281150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 328207a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 32839711cd0dSJens Freimann if (err) { 32849711cd0dSJens Freimann error_report_err(err); 32859711cd0dSJens Freimann } 32869711cd0dSJens Freimann } 32879711cd0dSJens Freimann } 32889711cd0dSJens Freimann } 32899711cd0dSJens Freimann 32909711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) 32919711cd0dSJens Freimann { 32929711cd0dSJens Freimann MigrationState *s = data; 32939711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 32949711cd0dSJens Freimann virtio_net_handle_migration_primary(n, s); 32959711cd0dSJens Freimann } 32969711cd0dSJens Freimann 3297b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3298f3558b1bSKevin Wolf const QDict *device_opts, 3299f3558b1bSKevin Wolf bool from_json, 3300f3558b1bSKevin Wolf Error **errp) 33019711cd0dSJens Freimann { 33029711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 33034f0303aeSJuan Quintela const char *standby_id; 33049711cd0dSJens Freimann 33054d0e59acSJens Freimann if (!device_opts) { 330689631fedSJuan Quintela return false; 33074d0e59acSJens Freimann } 3308bcfc906bSLaurent Vivier 3309bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3310bcfc906bSLaurent Vivier return false; 3311bcfc906bSLaurent Vivier } 3312bcfc906bSLaurent Vivier 3313bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3314bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3315bcfc906bSLaurent Vivier return false; 3316bcfc906bSLaurent Vivier } 3317bcfc906bSLaurent Vivier 3318bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 331989631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 332089631fedSJuan Quintela return false; 33219711cd0dSJens Freimann } 33229711cd0dSJens Freimann 33237fe7791eSLaurent Vivier /* 33247fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 33257fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 33267fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 33277fe7791eSLaurent Vivier * device. 33287fe7791eSLaurent Vivier */ 3329259a10dbSKevin Wolf if (n->primary_opts) { 33307fe7791eSLaurent Vivier const char *old, *new; 33317fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 33327fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 33337fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 33347fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 33357fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 33367fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3337259a10dbSKevin Wolf return false; 3338259a10dbSKevin Wolf } 33397fe7791eSLaurent Vivier } else { 3340f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3341f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 33427fe7791eSLaurent Vivier } 3343259a10dbSKevin Wolf 3344e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 33453abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 33469711cd0dSJens Freimann } 33479711cd0dSJens Freimann 3348e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 334917ec5a86SKONRAD Frederic { 3350e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3351284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3352284a32f0SAndreas Färber NetClientState *nc; 33531773d9eeSKONRAD Frederic int i; 335417ec5a86SKONRAD Frederic 3355a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3356127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3357a93e599dSMaxime Coquelin } 3358a93e599dSMaxime Coquelin 33599473939eSJason Baron if (n->net_conf.duplex_str) { 33609473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 33619473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 33629473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 33639473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 33649473939eSJason Baron } else { 33659473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3366843c4cfcSMarkus Armbruster return; 33679473939eSJason Baron } 33689473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 33699473939eSJason Baron } else { 33709473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 33719473939eSJason Baron } 33729473939eSJason Baron 33739473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 33749473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3375843c4cfcSMarkus Armbruster return; 3376843c4cfcSMarkus Armbruster } 3377843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 33789473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 33799473939eSJason Baron } 33809473939eSJason Baron 33819711cd0dSJens Freimann if (n->failover) { 3382b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3383e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 33849711cd0dSJens Freimann device_listener_register(&n->primary_listener); 33859711cd0dSJens Freimann n->migration_state.notify = virtio_net_migration_state_notifier; 33869711cd0dSJens Freimann add_migration_state_change_notifier(&n->migration_state); 33879711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 33889711cd0dSJens Freimann } 33899711cd0dSJens Freimann 3390da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 3391284a32f0SAndreas Färber virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); 339217ec5a86SKONRAD Frederic 33931c0fbfa3SMichael S. Tsirkin /* 33941c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 33951c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 33961c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 33971c0fbfa3SMichael S. Tsirkin */ 33981c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 33991c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 34005f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 34011c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 34021c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 34031c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 34041c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 34051c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 34061c0fbfa3SMichael S. Tsirkin return; 34071c0fbfa3SMichael S. Tsirkin } 34081c0fbfa3SMichael S. Tsirkin 34099b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 34109b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 34119b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 34129b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 34139b02e161SWei Wang "must be a power of 2 between %d and %d", 34149b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 34159b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 34169b02e161SWei Wang virtio_cleanup(vdev); 34179b02e161SWei Wang return; 34189b02e161SWei Wang } 34199b02e161SWei Wang 342022288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 342122288fe5SJason Wang 342222288fe5SJason Wang /* 342322288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 342422288fe5SJason Wang * provide control queue via peers as well. 342522288fe5SJason Wang */ 342622288fe5SJason Wang if (n->nic_conf.peers.queues) { 342722288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 342822288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 342922288fe5SJason Wang ++n->max_queue_pairs; 343022288fe5SJason Wang } 343122288fe5SJason Wang } 343222288fe5SJason Wang } 343322288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 343422288fe5SJason Wang 3435441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 343622288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3437631b22eaSStefan Weil "must be a positive integer less than %d.", 3438441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 34397e0e736eSJason Wang virtio_cleanup(vdev); 34407e0e736eSJason Wang return; 34417e0e736eSJason Wang } 3442441537f1SJason Wang n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queue_pairs); 3443441537f1SJason Wang n->curr_queue_pairs = 1; 34441773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 34456e790746SPaolo Bonzini 34461773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 34471773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 34480765691eSMarkus Armbruster warn_report("virtio-net: " 34496e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 34501773d9eeSKONRAD Frederic n->net_conf.tx); 34510765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 34526e790746SPaolo Bonzini } 34536e790746SPaolo Bonzini 34542eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 34552eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 34569b02e161SWei Wang 3457441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3458f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3459da51a335SJason Wang } 3460da51a335SJason Wang 346117a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 34621773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 34631773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 34646e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 34659d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 34669d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3467f57fcf70SJason Wang virtio_net_announce_timer, n); 3468b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 34696e790746SPaolo Bonzini 34708a253ec2SKONRAD Frederic if (n->netclient_type) { 34718a253ec2SKONRAD Frederic /* 34728a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 34738a253ec2SKONRAD Frederic */ 34748a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 34758a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 34768a253ec2SKONRAD Frederic } else { 34771773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 3478284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 34798a253ec2SKONRAD Frederic } 34808a253ec2SKONRAD Frederic 3481441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3482d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3483d4c62930SBin Meng } 3484d4c62930SBin Meng 34856e790746SPaolo Bonzini peer_test_vnet_hdr(n); 34866e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 3487441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3488d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 34896e790746SPaolo Bonzini } 34906e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 34916e790746SPaolo Bonzini } else { 34926e790746SPaolo Bonzini n->host_hdr_len = 0; 34936e790746SPaolo Bonzini } 34946e790746SPaolo Bonzini 34951773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 34966e790746SPaolo Bonzini 34976e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 34981773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3499e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 35006e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 35016e790746SPaolo Bonzini 35026e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 35036e790746SPaolo Bonzini 35046e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 35056e790746SPaolo Bonzini 3506b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3507b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3508b1be4280SAmos Kong 3509e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3510e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3511e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3512e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3513e87936eaSCindy Lu (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER); 3514e87936eaSCindy Lu } 35152974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3516284a32f0SAndreas Färber n->qdev = dev; 35174474e37aSYuri Benditovich 35184474e37aSYuri Benditovich net_rx_pkt_init(&n->rx_pkt, false); 35190145c393SAndrew Melnychenko 35200145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 35210145c393SAndrew Melnychenko virtio_net_load_ebpf(n); 35220145c393SAndrew Melnychenko } 352317ec5a86SKONRAD Frederic } 352417ec5a86SKONRAD Frederic 3525b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 352617ec5a86SKONRAD Frederic { 3527306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3528306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3529441537f1SJason Wang int i, max_queue_pairs; 353017ec5a86SKONRAD Frederic 35310145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 35320145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 35330145c393SAndrew Melnychenko } 35340145c393SAndrew Melnychenko 353517ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 353617ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 353717ec5a86SKONRAD Frederic 35388a253ec2SKONRAD Frederic g_free(n->netclient_name); 35398a253ec2SKONRAD Frederic n->netclient_name = NULL; 35408a253ec2SKONRAD Frederic g_free(n->netclient_type); 35418a253ec2SKONRAD Frederic n->netclient_type = NULL; 35428a253ec2SKONRAD Frederic 354317ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 354417ec5a86SKONRAD Frederic g_free(n->vlans); 354517ec5a86SKONRAD Frederic 35469711cd0dSJens Freimann if (n->failover) { 3547f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 354865018100SJuan Quintela device_listener_unregister(&n->primary_listener); 35491e157667SLaurent Vivier remove_migration_state_change_notifier(&n->migration_state); 3550f3558b1bSKevin Wolf } else { 3551f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 35529711cd0dSJens Freimann } 35539711cd0dSJens Freimann 3554441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3555441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3556f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 355717ec5a86SKONRAD Frederic } 3558d945d9f1SYuri Benditovich /* delete also control vq */ 3559441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3560944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 356117ec5a86SKONRAD Frederic g_free(n->vqs); 356217ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 35632974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 356459079029SYuri Benditovich g_free(n->rss_data.indirections_table); 35654474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 35666a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 356717ec5a86SKONRAD Frederic } 356817ec5a86SKONRAD Frederic 356917ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 357017ec5a86SKONRAD Frederic { 357117ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 357217ec5a86SKONRAD Frederic 357317ec5a86SKONRAD Frederic /* 357417ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 357517ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 357617ec5a86SKONRAD Frederic */ 357717ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3578aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3579aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 358040c2281cSMarkus Armbruster DEVICE(n)); 35810145c393SAndrew Melnychenko 35820145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 358317ec5a86SKONRAD Frederic } 358417ec5a86SKONRAD Frederic 358544b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 35864d45dcfbSHalil Pasic { 35874d45dcfbSHalil Pasic VirtIONet *n = opaque; 35884d45dcfbSHalil Pasic 35894d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 35904d45dcfbSHalil Pasic * it might keep writing to memory. */ 35914d45dcfbSHalil Pasic assert(!n->vhost_started); 359244b1ff31SDr. David Alan Gilbert 359344b1ff31SDr. David Alan Gilbert return 0; 35944d45dcfbSHalil Pasic } 35954d45dcfbSHalil Pasic 35969711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 35979711cd0dSJens Freimann { 35989711cd0dSJens Freimann DeviceState *dev = opaque; 359921e8709bSJuan Quintela DeviceState *primary; 36009711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 36019711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 36029711cd0dSJens Freimann 3603284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3604284f42a5SJens Freimann return false; 3605284f42a5SJens Freimann } 360621e8709bSJuan Quintela primary = failover_find_primary_device(n); 360721e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 36089711cd0dSJens Freimann } 36099711cd0dSJens Freimann 36109711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 36119711cd0dSJens Freimann { 36129711cd0dSJens Freimann DeviceState *dev = opaque; 36139711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 36149711cd0dSJens Freimann 36159711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 36169711cd0dSJens Freimann } 36179711cd0dSJens Freimann 36184d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 36194d45dcfbSHalil Pasic .name = "virtio-net", 36204d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 36214d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 36224d45dcfbSHalil Pasic .fields = (VMStateField[]) { 36234d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 36244d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 36254d45dcfbSHalil Pasic }, 36264d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 36279711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 36284d45dcfbSHalil Pasic }; 3629290c2428SDr. David Alan Gilbert 363017ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3631127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3632127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3633127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 363487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3635127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3636127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 363787108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3638127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 363987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3640127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 364187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3642127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 364387108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3644127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 364587108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3646127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 364787108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3648127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 364987108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3650127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 365187108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3652127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 365387108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3654127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 365587108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3656127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 365787108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3658127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 365987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3660127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 366187108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3662127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 366387108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3664127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 366587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3666127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 366787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3668127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 366987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3670127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 367159079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 367259079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3673e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3674e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 36752974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 36762974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 36772974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 36782974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 367917ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 368017ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 368117ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 368217ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 368317ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 36841c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 36851c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 36869b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 36879b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3688a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 368975ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 369075ebec11SMaxime Coquelin true), 36919473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 36929473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 36939711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 369417ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 369517ec5a86SKONRAD Frederic }; 369617ec5a86SKONRAD Frederic 369717ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 369817ec5a86SKONRAD Frederic { 369917ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 370017ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3701e6f746b3SAndreas Färber 37024f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3703290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3704125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3705e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3706306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 370717ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 370817ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 370917ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 371017ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 371117ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 371217ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 371317ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 371417ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 371517ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 37162a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 37177788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 3718982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 37199711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 372017ec5a86SKONRAD Frederic } 372117ec5a86SKONRAD Frederic 372217ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 372317ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 372417ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 372517ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 372617ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 372717ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 372817ec5a86SKONRAD Frederic }; 372917ec5a86SKONRAD Frederic 373017ec5a86SKONRAD Frederic static void virtio_register_types(void) 373117ec5a86SKONRAD Frederic { 373217ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 373317ec5a86SKONRAD Frederic } 373417ec5a86SKONRAD Frederic 373517ec5a86SKONRAD Frederic type_init(virtio_register_types) 3736