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" 1768b0a639SSi-Wei Liu #include "qemu/log.h" 18db725815SMarkus Armbruster #include "qemu/main-loop.h" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 206e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 216e790746SPaolo Bonzini #include "net/net.h" 226e790746SPaolo Bonzini #include "net/checksum.h" 236e790746SPaolo Bonzini #include "net/tap.h" 246e790746SPaolo Bonzini #include "qemu/error-report.h" 256e790746SPaolo Bonzini #include "qemu/timer.h" 269711cd0dSJens Freimann #include "qemu/option.h" 279711cd0dSJens Freimann #include "qemu/option_int.h" 289711cd0dSJens Freimann #include "qemu/config-file.h" 299711cd0dSJens Freimann #include "qapi/qmp/qdict.h" 306e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 316e790746SPaolo Bonzini #include "net/vhost_net.h" 329d8c6a25SDr. David Alan Gilbert #include "net/announce.h" 3317ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 34e688df6bSMarkus Armbruster #include "qapi/error.h" 359af23989SMarkus Armbruster #include "qapi/qapi-events-net.h" 36a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 379711cd0dSJens Freimann #include "qapi/qapi-types-migration.h" 389711cd0dSJens Freimann #include "qapi/qapi-events-migration.h" 391399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 40f8d806c9SJuan Quintela #include "migration/misc.h" 419473939eSJason Baron #include "standard-headers/linux/ethtool.h" 422f780b6aSMarkus Armbruster #include "sysemu/sysemu.h" 439d8c6a25SDr. David Alan Gilbert #include "trace.h" 449711cd0dSJens Freimann #include "monitor/qdev.h" 459711cd0dSJens Freimann #include "hw/pci/pci.h" 464474e37aSYuri Benditovich #include "net_rx_pkt.h" 47108a6481SCindy Lu #include "hw/virtio/vhost.h" 481b529d90SLaurent Vivier #include "sysemu/qtest.h" 496e790746SPaolo Bonzini 506e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 516e790746SPaolo Bonzini 526e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 536e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 546e790746SPaolo Bonzini 551c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 561c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 579b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 589b02e161SWei Wang 59441537f1SJason Wang /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */ 601c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 619b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 621c0fbfa3SMichael S. Tsirkin 632974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ 642974e916SYuri Benditovich 652974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG 0x3F 662974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 672974e916SYuri Benditovich 682974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */ 692974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header)) 702974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535 712974e916SYuri Benditovich 722974e916SYuri Benditovich /* header length value in ip header without option */ 732974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5 742974e916SYuri Benditovich 752974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */ 762974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD 772974e916SYuri Benditovich 782974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance 792974e916SYuri Benditovich a lot, and should be tuned carefully, '300000'(300us) is the recommended 802974e916SYuri Benditovich value to pass the WHQL test, '50000' can gain 2x netperf throughput with 812974e916SYuri Benditovich tso/gso/gro 'off'. */ 822974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000 832974e916SYuri Benditovich 8459079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ 8559079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ 8659079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ 8759079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ 8859079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ 8959079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \ 9059079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \ 9159079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \ 9259079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) 9359079029SYuri Benditovich 94ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = { 95127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 965d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mac)}, 97127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 985d5b33c0SMax Reitz .end = endof(struct virtio_net_config, status)}, 99127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 1005d5b33c0SMax Reitz .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 101127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 1025d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mtu)}, 1039473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 1045d5b33c0SMax Reitz .end = endof(struct virtio_net_config, duplex)}, 105e22f0603SYuri Benditovich {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT), 10659079029SYuri Benditovich .end = endof(struct virtio_net_config, supported_hash_types)}, 1076e790746SPaolo Bonzini {} 1086e790746SPaolo Bonzini }; 1096e790746SPaolo Bonzini 1106e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 1116e790746SPaolo Bonzini { 1126e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1136e790746SPaolo Bonzini 1146e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 1156e790746SPaolo Bonzini } 1166e790746SPaolo Bonzini 1176e790746SPaolo Bonzini static int vq2q(int queue_index) 1186e790746SPaolo Bonzini { 1196e790746SPaolo Bonzini return queue_index / 2; 1206e790746SPaolo Bonzini } 1216e790746SPaolo Bonzini 1226e790746SPaolo Bonzini /* TODO 1236e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 1246e790746SPaolo Bonzini */ 1256e790746SPaolo Bonzini 1266e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 1276e790746SPaolo Bonzini { 12817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1296e790746SPaolo Bonzini struct virtio_net_config netcfg; 130c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 131fb592882SCindy Lu static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } }; 1326e790746SPaolo Bonzini 133108a6481SCindy Lu int ret = 0; 134108a6481SCindy Lu memset(&netcfg, 0 , sizeof(struct virtio_net_config)); 1351399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 136441537f1SJason Wang virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs); 137a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 1386e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 1399473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 1409473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 14159079029SYuri Benditovich netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE; 14259079029SYuri Benditovich virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length, 143e22f0603SYuri Benditovich virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ? 144e22f0603SYuri Benditovich VIRTIO_NET_RSS_MAX_TABLE_LEN : 1); 14559079029SYuri Benditovich virtio_stl_p(vdev, &netcfg.supported_hash_types, 14659079029SYuri Benditovich VIRTIO_NET_RSS_SUPPORTED_HASHES); 1476e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 148108a6481SCindy Lu 149c546ecf2SJason Wang /* 150c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 151c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 152c546ecf2SJason Wang */ 153c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 154108a6481SCindy Lu ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, 155108a6481SCindy Lu n->config_size); 156108a6481SCindy Lu if (ret != -1) { 157fb592882SCindy Lu /* 158fb592882SCindy Lu * Some NIC/kernel combinations present 0 as the mac address. As 159fb592882SCindy Lu * that is not a legal address, try to proceed with the 160fb592882SCindy Lu * address from the QEMU command line in the hope that the 161fb592882SCindy Lu * address has been configured correctly elsewhere - just not 162fb592882SCindy Lu * reported by the device. 163fb592882SCindy Lu */ 164fb592882SCindy Lu if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) { 165fb592882SCindy Lu info_report("Zero hardware mac address detected. Ignoring."); 166fb592882SCindy Lu memcpy(netcfg.mac, n->mac, ETH_ALEN); 167fb592882SCindy Lu } 168108a6481SCindy Lu memcpy(config, &netcfg, n->config_size); 169108a6481SCindy Lu } 170108a6481SCindy Lu } 1716e790746SPaolo Bonzini } 1726e790746SPaolo Bonzini 1736e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1746e790746SPaolo Bonzini { 17517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1766e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 177c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1786e790746SPaolo Bonzini 1796e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1806e790746SPaolo Bonzini 18195129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 18295129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1836e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1846e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 1856e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1866e790746SPaolo Bonzini } 187108a6481SCindy Lu 188c546ecf2SJason Wang /* 189c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 190c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 191c546ecf2SJason Wang */ 192c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 193c546ecf2SJason Wang vhost_net_set_config(get_vhost_net(nc->peer), 194c546ecf2SJason Wang (uint8_t *)&netcfg, 0, n->config_size, 195108a6481SCindy Lu VHOST_SET_CONFIG_TYPE_MASTER); 196108a6481SCindy Lu } 1976e790746SPaolo Bonzini } 1986e790746SPaolo Bonzini 1996e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 2006e790746SPaolo Bonzini { 20117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2026e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 20317a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 2046e790746SPaolo Bonzini } 2056e790746SPaolo Bonzini 206b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net) 207b2c929f0SDr. David Alan Gilbert { 208b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(net); 209b2c929f0SDr. David Alan Gilbert trace_virtio_net_announce_notify(); 210b2c929f0SDr. David Alan Gilbert 211b2c929f0SDr. David Alan Gilbert net->status |= VIRTIO_NET_S_ANNOUNCE; 212b2c929f0SDr. David Alan Gilbert virtio_notify_config(vdev); 213b2c929f0SDr. David Alan Gilbert } 214b2c929f0SDr. David Alan Gilbert 215f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 216f57fcf70SJason Wang { 217f57fcf70SJason Wang VirtIONet *n = opaque; 2189d8c6a25SDr. David Alan Gilbert trace_virtio_net_announce_timer(n->announce_timer.round); 219f57fcf70SJason Wang 2209d8c6a25SDr. David Alan Gilbert n->announce_timer.round--; 221b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 222b2c929f0SDr. David Alan Gilbert } 223b2c929f0SDr. David Alan Gilbert 224b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc) 225b2c929f0SDr. David Alan Gilbert { 226b2c929f0SDr. David Alan Gilbert VirtIONet *n = qemu_get_nic_opaque(nc); 227b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 228b2c929f0SDr. David Alan Gilbert 229b2c929f0SDr. David Alan Gilbert /* 230b2c929f0SDr. David Alan Gilbert * Make sure the virtio migration announcement timer isn't running 231b2c929f0SDr. David Alan Gilbert * If it is, let it trigger announcement so that we do not cause 232b2c929f0SDr. David Alan Gilbert * confusion. 233b2c929f0SDr. David Alan Gilbert */ 234b2c929f0SDr. David Alan Gilbert if (n->announce_timer.round) { 235b2c929f0SDr. David Alan Gilbert return; 236b2c929f0SDr. David Alan Gilbert } 237b2c929f0SDr. David Alan Gilbert 238b2c929f0SDr. David Alan Gilbert if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 239b2c929f0SDr. David Alan Gilbert virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 240b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 241b2c929f0SDr. David Alan Gilbert } 242f57fcf70SJason Wang } 243f57fcf70SJason Wang 2446e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 2456e790746SPaolo Bonzini { 24617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2476e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 248441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 249aa858194SSi-Wei Liu int cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ? 250aa858194SSi-Wei Liu n->max_ncs - n->max_queue_pairs : 0; 2516e790746SPaolo Bonzini 252ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 2536e790746SPaolo Bonzini return; 2546e790746SPaolo Bonzini } 2556e790746SPaolo Bonzini 2568c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 2578c1ac475SRadim Krčmář !!n->vhost_started) { 2586e790746SPaolo Bonzini return; 2596e790746SPaolo Bonzini } 2606e790746SPaolo Bonzini if (!n->vhost_started) { 261086abc1cSMichael S. Tsirkin int r, i; 262086abc1cSMichael S. Tsirkin 2631bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2641bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 2651bfa316cSGreg Kurz "falling back on userspace virtio", 2661bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 2671bfa316cSGreg Kurz return; 2681bfa316cSGreg Kurz } 2691bfa316cSGreg Kurz 270086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 271086abc1cSMichael S. Tsirkin * when vhost is running. 272086abc1cSMichael S. Tsirkin */ 273441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 274086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 275086abc1cSMichael S. Tsirkin 276086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 277086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 278086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 279086abc1cSMichael S. Tsirkin } 280086abc1cSMichael S. Tsirkin 281a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 282a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 283a93e599dSMaxime Coquelin if (r < 0) { 284a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 285a93e599dSMaxime Coquelin n->net_conf.mtu); 286a93e599dSMaxime Coquelin 287a93e599dSMaxime Coquelin return; 288a93e599dSMaxime Coquelin } 289a93e599dSMaxime Coquelin } 290a93e599dSMaxime Coquelin 2916e790746SPaolo Bonzini n->vhost_started = 1; 29222288fe5SJason Wang r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq); 2936e790746SPaolo Bonzini if (r < 0) { 2946e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 2956e790746SPaolo Bonzini "falling back on userspace virtio", -r); 2966e790746SPaolo Bonzini n->vhost_started = 0; 2976e790746SPaolo Bonzini } 2986e790746SPaolo Bonzini } else { 29922288fe5SJason Wang vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq); 3006e790746SPaolo Bonzini n->vhost_started = 0; 3016e790746SPaolo Bonzini } 3026e790746SPaolo Bonzini } 3036e790746SPaolo Bonzini 3041bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 3051bfa316cSGreg Kurz NetClientState *peer, 3061bfa316cSGreg Kurz bool enable) 3071bfa316cSGreg Kurz { 3081bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 3091bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 3101bfa316cSGreg Kurz } else { 3111bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 3121bfa316cSGreg Kurz } 3131bfa316cSGreg Kurz } 3141bfa316cSGreg Kurz 3151bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 316441537f1SJason Wang int queue_pairs, bool enable) 3171bfa316cSGreg Kurz { 3181bfa316cSGreg Kurz int i; 3191bfa316cSGreg Kurz 320441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 3211bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 3221bfa316cSGreg Kurz enable) { 3231bfa316cSGreg Kurz while (--i >= 0) { 3241bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 3251bfa316cSGreg Kurz } 3261bfa316cSGreg Kurz 3271bfa316cSGreg Kurz return true; 3281bfa316cSGreg Kurz } 3291bfa316cSGreg Kurz } 3301bfa316cSGreg Kurz 3311bfa316cSGreg Kurz return false; 3321bfa316cSGreg Kurz } 3331bfa316cSGreg Kurz 3341bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 3351bfa316cSGreg Kurz { 3361bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 337441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3381bfa316cSGreg Kurz 3391bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 3401bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 3411bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 3421bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 3431bfa316cSGreg Kurz * virtio-net code. 3441bfa316cSGreg Kurz */ 3451bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 346441537f1SJason Wang queue_pairs, true); 3471bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3481bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3491bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3501bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3511bfa316cSGreg Kurz * endianness. 3521bfa316cSGreg Kurz */ 353441537f1SJason Wang virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false); 3541bfa316cSGreg Kurz } 3551bfa316cSGreg Kurz } 3561bfa316cSGreg Kurz 357283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 358283e2c2aSYuri Benditovich { 359283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 360283e2c2aSYuri Benditovich if (dropped) { 361283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 362283e2c2aSYuri Benditovich } 363283e2c2aSYuri Benditovich } 364283e2c2aSYuri Benditovich 3656e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3666e790746SPaolo Bonzini { 36717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3686e790746SPaolo Bonzini VirtIONetQueue *q; 3696e790746SPaolo Bonzini int i; 3706e790746SPaolo Bonzini uint8_t queue_status; 3716e790746SPaolo Bonzini 3721bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3736e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3746e790746SPaolo Bonzini 375441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 37638705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 37738705bb5SFam Zheng bool queue_started; 3786e790746SPaolo Bonzini q = &n->vqs[i]; 3796e790746SPaolo Bonzini 380441537f1SJason Wang if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) { 3816e790746SPaolo Bonzini queue_status = 0; 3826e790746SPaolo Bonzini } else { 3836e790746SPaolo Bonzini queue_status = status; 3846e790746SPaolo Bonzini } 38538705bb5SFam Zheng queue_started = 38638705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 38738705bb5SFam Zheng 38838705bb5SFam Zheng if (queue_started) { 38938705bb5SFam Zheng qemu_flush_queued_packets(ncs); 39038705bb5SFam Zheng } 3916e790746SPaolo Bonzini 3926e790746SPaolo Bonzini if (!q->tx_waiting) { 3936e790746SPaolo Bonzini continue; 3946e790746SPaolo Bonzini } 3956e790746SPaolo Bonzini 39638705bb5SFam Zheng if (queue_started) { 3976e790746SPaolo Bonzini if (q->tx_timer) { 398bc72ad67SAlex Bligh timer_mod(q->tx_timer, 399bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 4006e790746SPaolo Bonzini } else { 4016e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 4026e790746SPaolo Bonzini } 4036e790746SPaolo Bonzini } else { 4046e790746SPaolo Bonzini if (q->tx_timer) { 405bc72ad67SAlex Bligh timer_del(q->tx_timer); 4066e790746SPaolo Bonzini } else { 4076e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 4086e790746SPaolo Bonzini } 409283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 41070e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 41170e53e6eSJason Wang vdev->vm_running) { 412283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 413283e2c2aSYuri Benditovich * and disabled notification */ 414283e2c2aSYuri Benditovich q->tx_waiting = 0; 415283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 416283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 417283e2c2aSYuri Benditovich } 4186e790746SPaolo Bonzini } 4196e790746SPaolo Bonzini } 4206e790746SPaolo Bonzini } 4216e790746SPaolo Bonzini 4226e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4236e790746SPaolo Bonzini { 4246e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 42517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4266e790746SPaolo Bonzini uint16_t old_status = n->status; 4276e790746SPaolo Bonzini 4286e790746SPaolo Bonzini if (nc->link_down) 4296e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4306e790746SPaolo Bonzini else 4316e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4326e790746SPaolo Bonzini 4336e790746SPaolo Bonzini if (n->status != old_status) 43417a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4356e790746SPaolo Bonzini 43617a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4376e790746SPaolo Bonzini } 4386e790746SPaolo Bonzini 439b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 440b1be4280SAmos Kong { 441b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 442b1be4280SAmos Kong 443b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 444ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 44506150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 4463ab72385SPeter Xu n->netclient_name, path); 44796e35046SAmos Kong g_free(path); 448b1be4280SAmos Kong 449b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 450b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 451b1be4280SAmos Kong } 452b1be4280SAmos Kong } 453b1be4280SAmos Kong 454f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 455f7bc8ef8SAmos Kong { 45654aa3de7SEric Blake intList *list; 457f7bc8ef8SAmos Kong int i, j; 458f7bc8ef8SAmos Kong 459f7bc8ef8SAmos Kong list = NULL; 460f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 461f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 462f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 46354aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 464f7bc8ef8SAmos Kong } 465f7bc8ef8SAmos Kong } 466f7bc8ef8SAmos Kong } 467f7bc8ef8SAmos Kong 468f7bc8ef8SAmos Kong return list; 469f7bc8ef8SAmos Kong } 470f7bc8ef8SAmos Kong 471b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 472b1be4280SAmos Kong { 473b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 474f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 475b1be4280SAmos Kong RxFilterInfo *info; 47654aa3de7SEric Blake strList *str_list; 477f7bc8ef8SAmos Kong int i; 478b1be4280SAmos Kong 479b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 480b1be4280SAmos Kong info->name = g_strdup(nc->name); 481b1be4280SAmos Kong info->promiscuous = n->promisc; 482b1be4280SAmos Kong 483b1be4280SAmos Kong if (n->nouni) { 484b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 485b1be4280SAmos Kong } else if (n->alluni) { 486b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 487b1be4280SAmos Kong } else { 488b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 489b1be4280SAmos Kong } 490b1be4280SAmos Kong 491b1be4280SAmos Kong if (n->nomulti) { 492b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 493b1be4280SAmos Kong } else if (n->allmulti) { 494b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 495b1be4280SAmos Kong } else { 496b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 497b1be4280SAmos Kong } 498b1be4280SAmos Kong 499b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 500b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 501b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 502b1be4280SAmos Kong 503b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 504b1be4280SAmos Kong 505b1be4280SAmos Kong str_list = NULL; 506b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 50754aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 50854aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 509b1be4280SAmos Kong } 510b1be4280SAmos Kong info->unicast_table = str_list; 511b1be4280SAmos Kong 512b1be4280SAmos Kong str_list = NULL; 513b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 51454aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 51554aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 516b1be4280SAmos Kong } 517b1be4280SAmos Kong info->multicast_table = str_list; 518f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 519b1be4280SAmos Kong 52095129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 521f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 522f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 523f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 524f7bc8ef8SAmos Kong } else { 525f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 526b1be4280SAmos Kong } 527b1be4280SAmos Kong 528b1be4280SAmos Kong /* enable event notification after query */ 529b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 530b1be4280SAmos Kong 531b1be4280SAmos Kong return info; 532b1be4280SAmos Kong } 533b1be4280SAmos Kong 5346e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 5356e790746SPaolo Bonzini { 53617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 53794b52958SGreg Kurz int i; 5386e790746SPaolo Bonzini 5396e790746SPaolo Bonzini /* Reset back to compatibility mode */ 5406e790746SPaolo Bonzini n->promisc = 1; 5416e790746SPaolo Bonzini n->allmulti = 0; 5426e790746SPaolo Bonzini n->alluni = 0; 5436e790746SPaolo Bonzini n->nomulti = 0; 5446e790746SPaolo Bonzini n->nouni = 0; 5456e790746SPaolo Bonzini n->nobcast = 0; 5466e790746SPaolo Bonzini /* multiqueue is disabled by default */ 547441537f1SJason Wang n->curr_queue_pairs = 1; 5489d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 5499d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 550f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 5516e790746SPaolo Bonzini 5526e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 5536e790746SPaolo Bonzini n->mac_table.in_use = 0; 5546e790746SPaolo Bonzini n->mac_table.first_multi = 0; 5556e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 5566e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 5576e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 5586e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 559702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 5606e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 56194b52958SGreg Kurz 56294b52958SGreg Kurz /* Flush any async TX */ 563441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 56494b52958SGreg Kurz NetClientState *nc = qemu_get_subqueue(n->nic, i); 56594b52958SGreg Kurz 56694b52958SGreg Kurz if (nc->peer) { 56794b52958SGreg Kurz qemu_flush_or_purge_queued_packets(nc->peer, true); 56894b52958SGreg Kurz assert(!virtio_net_get_subqueue(nc)->async_tx.elem); 56994b52958SGreg Kurz } 57094b52958SGreg Kurz } 5716e790746SPaolo Bonzini } 5726e790746SPaolo Bonzini 5736e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 5746e790746SPaolo Bonzini { 5756e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5766e790746SPaolo Bonzini if (!nc->peer) { 5776e790746SPaolo Bonzini return; 5786e790746SPaolo Bonzini } 5796e790746SPaolo Bonzini 580d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 5816e790746SPaolo Bonzini } 5826e790746SPaolo Bonzini 5836e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 5846e790746SPaolo Bonzini { 5856e790746SPaolo Bonzini return n->has_vnet_hdr; 5866e790746SPaolo Bonzini } 5876e790746SPaolo Bonzini 5886e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 5896e790746SPaolo Bonzini { 5906e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 5916e790746SPaolo Bonzini return 0; 5926e790746SPaolo Bonzini 593d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 5946e790746SPaolo Bonzini 5956e790746SPaolo Bonzini return n->has_ufo; 5966e790746SPaolo Bonzini } 5976e790746SPaolo Bonzini 598bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 599e22f0603SYuri Benditovich int version_1, int hash_report) 6006e790746SPaolo Bonzini { 6016e790746SPaolo Bonzini int i; 6026e790746SPaolo Bonzini NetClientState *nc; 6036e790746SPaolo Bonzini 6046e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6056e790746SPaolo Bonzini 606bb9d17f8SCornelia Huck if (version_1) { 607e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 608e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 609e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 610e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 611bb9d17f8SCornelia Huck } else { 6126e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 613bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 614bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 615bb9d17f8SCornelia Huck } 6166e790746SPaolo Bonzini 617441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6186e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6196e790746SPaolo Bonzini 6206e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 621d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 622d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6236e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6246e790746SPaolo Bonzini } 6256e790746SPaolo Bonzini } 6266e790746SPaolo Bonzini } 6276e790746SPaolo Bonzini 6282eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6292eef278bSMichael S. Tsirkin { 6302eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6312eef278bSMichael S. Tsirkin 6322eef278bSMichael S. Tsirkin /* 6330ea5778fSEugenio Pérez * Backends other than vhost-user or vhost-vdpa don't support max queue 6340ea5778fSEugenio Pérez * size. 6352eef278bSMichael S. Tsirkin */ 6362eef278bSMichael S. Tsirkin if (!peer) { 6372eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6382eef278bSMichael S. Tsirkin } 6392eef278bSMichael S. Tsirkin 6400ea5778fSEugenio Pérez switch(peer->info->type) { 6410ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_USER: 6420ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_VDPA: 6432eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 6440ea5778fSEugenio Pérez default: 6450ea5778fSEugenio Pérez return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6460ea5778fSEugenio Pérez }; 6472eef278bSMichael S. Tsirkin } 6482eef278bSMichael S. Tsirkin 6496e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 6506e790746SPaolo Bonzini { 6516e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6526e790746SPaolo Bonzini 6536e790746SPaolo Bonzini if (!nc->peer) { 6546e790746SPaolo Bonzini return 0; 6556e790746SPaolo Bonzini } 6566e790746SPaolo Bonzini 657f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6587263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 6597263a0adSChangchun Ouyang } 6607263a0adSChangchun Ouyang 661f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6626e790746SPaolo Bonzini return 0; 6636e790746SPaolo Bonzini } 6646e790746SPaolo Bonzini 665441537f1SJason Wang if (n->max_queue_pairs == 1) { 6661074b879SJason Wang return 0; 6671074b879SJason Wang } 6681074b879SJason Wang 6696e790746SPaolo Bonzini return tap_enable(nc->peer); 6706e790746SPaolo Bonzini } 6716e790746SPaolo Bonzini 6726e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 6736e790746SPaolo Bonzini { 6746e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6756e790746SPaolo Bonzini 6766e790746SPaolo Bonzini if (!nc->peer) { 6776e790746SPaolo Bonzini return 0; 6786e790746SPaolo Bonzini } 6796e790746SPaolo Bonzini 680f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6817263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 6827263a0adSChangchun Ouyang } 6837263a0adSChangchun Ouyang 684f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 6856e790746SPaolo Bonzini return 0; 6866e790746SPaolo Bonzini } 6876e790746SPaolo Bonzini 6886e790746SPaolo Bonzini return tap_disable(nc->peer); 6896e790746SPaolo Bonzini } 6906e790746SPaolo Bonzini 691441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 6926e790746SPaolo Bonzini { 6936e790746SPaolo Bonzini int i; 694ddfa83eaSJoel Stanley int r; 6956e790746SPaolo Bonzini 69668b5f314SYuri Benditovich if (n->nic->peer_deleted) { 69768b5f314SYuri Benditovich return; 69868b5f314SYuri Benditovich } 69968b5f314SYuri Benditovich 700441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 701441537f1SJason Wang if (i < n->curr_queue_pairs) { 702ddfa83eaSJoel Stanley r = peer_attach(n, i); 703ddfa83eaSJoel Stanley assert(!r); 7046e790746SPaolo Bonzini } else { 705ddfa83eaSJoel Stanley r = peer_detach(n, i); 706ddfa83eaSJoel Stanley assert(!r); 7076e790746SPaolo Bonzini } 7086e790746SPaolo Bonzini } 7096e790746SPaolo Bonzini } 7106e790746SPaolo Bonzini 711ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7126e790746SPaolo Bonzini 7139d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7149d5b731dSJason Wang Error **errp) 7156e790746SPaolo Bonzini { 71617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7176e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7186e790746SPaolo Bonzini 719da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 720da3e8a23SShannon Zhao features |= n->host_features; 721da3e8a23SShannon Zhao 7220cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7236e790746SPaolo Bonzini 7246e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7250cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7260cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7270cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7280cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7296e790746SPaolo Bonzini 7300cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7310cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7320cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7330cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 734e22f0603SYuri Benditovich 735e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7366e790746SPaolo Bonzini } 7376e790746SPaolo Bonzini 7386e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7390cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 7400cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 7416e790746SPaolo Bonzini } 7426e790746SPaolo Bonzini 743ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 7446e790746SPaolo Bonzini return features; 7456e790746SPaolo Bonzini } 7462974e916SYuri Benditovich 7470145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 74859079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 7490145c393SAndrew Melnychenko } 75075ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 75175ebec11SMaxime Coquelin vdev->backend_features = features; 75275ebec11SMaxime Coquelin 75375ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 75475ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 75575ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 75675ebec11SMaxime Coquelin } 75775ebec11SMaxime Coquelin 75875ebec11SMaxime Coquelin return features; 7596e790746SPaolo Bonzini } 7606e790746SPaolo Bonzini 761019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 7626e790746SPaolo Bonzini { 763019a3edbSGerd Hoffmann uint64_t features = 0; 7646e790746SPaolo Bonzini 7656e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 7666e790746SPaolo Bonzini * but also these: */ 7670cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7680cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 7690cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7700cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7710cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 7726e790746SPaolo Bonzini 7736e790746SPaolo Bonzini return features; 7746e790746SPaolo Bonzini } 7756e790746SPaolo Bonzini 776644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 777644c9858SDmitry Fleytman { 778ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 779644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 780644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 781644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 782644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 783644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 784644c9858SDmitry Fleytman } 785644c9858SDmitry Fleytman 786644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 787644c9858SDmitry Fleytman { 788644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 789644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 790644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 791644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 792644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 793644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 794644c9858SDmitry Fleytman 795644c9858SDmitry Fleytman return guest_offloads_mask & features; 796644c9858SDmitry Fleytman } 797644c9858SDmitry Fleytman 798644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 799644c9858SDmitry Fleytman { 800644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 801644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 802644c9858SDmitry Fleytman } 803644c9858SDmitry Fleytman 804f5e1847bSJuan Quintela typedef struct { 805f5e1847bSJuan Quintela VirtIONet *n; 80612b2fad7SKevin Wolf DeviceState *dev; 80712b2fad7SKevin Wolf } FailoverDevice; 808f5e1847bSJuan Quintela 809f5e1847bSJuan Quintela /** 81012b2fad7SKevin Wolf * Set the failover primary device 811f5e1847bSJuan Quintela * 812f5e1847bSJuan Quintela * @opaque: FailoverId to setup 813f5e1847bSJuan Quintela * @opts: opts for device we are handling 814f5e1847bSJuan Quintela * @errp: returns an error if this function fails 815f5e1847bSJuan Quintela */ 81612b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 817f5e1847bSJuan Quintela { 81812b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 81912b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 82012b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 821f5e1847bSJuan Quintela 82212b2fad7SKevin Wolf if (!pci_dev) { 82312b2fad7SKevin Wolf return 0; 82412b2fad7SKevin Wolf } 82512b2fad7SKevin Wolf 82612b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 82712b2fad7SKevin Wolf fdev->dev = dev; 828f5e1847bSJuan Quintela return 1; 829f5e1847bSJuan Quintela } 830f5e1847bSJuan Quintela 831f5e1847bSJuan Quintela return 0; 832f5e1847bSJuan Quintela } 833f5e1847bSJuan Quintela 834f5e1847bSJuan Quintela /** 83585d3b931SJuan Quintela * Find the primary device for this failover virtio-net 83685d3b931SJuan Quintela * 83785d3b931SJuan Quintela * @n: VirtIONet device 83885d3b931SJuan Quintela * @errp: returns an error if this function fails 83985d3b931SJuan Quintela */ 8400a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 8419711cd0dSJens Freimann { 84212b2fad7SKevin Wolf FailoverDevice fdev = { 84312b2fad7SKevin Wolf .n = n, 84412b2fad7SKevin Wolf }; 8459711cd0dSJens Freimann 84612b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 84712b2fad7SKevin Wolf NULL, NULL, &fdev); 84812b2fad7SKevin Wolf return fdev.dev; 8499711cd0dSJens Freimann } 8509711cd0dSJens Freimann 85121e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 85221e8709bSJuan Quintela { 85321e8709bSJuan Quintela Error *err = NULL; 85421e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 85521e8709bSJuan Quintela 85621e8709bSJuan Quintela if (dev) { 85721e8709bSJuan Quintela return; 85821e8709bSJuan Quintela } 85921e8709bSJuan Quintela 860259a10dbSKevin Wolf if (!n->primary_opts) { 86197ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 86297ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 86397ca9c59SLaurent Vivier "sure primary device has parameter" 86497ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 86521e8709bSJuan Quintela return; 86621e8709bSJuan Quintela } 867259a10dbSKevin Wolf 868f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 869f3558b1bSKevin Wolf n->primary_opts_from_json, 870f3558b1bSKevin Wolf &err); 87121e8709bSJuan Quintela if (err) { 872f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 873259a10dbSKevin Wolf n->primary_opts = NULL; 87400e7b129SLaurent Vivier } else { 87500e7b129SLaurent Vivier object_unref(OBJECT(dev)); 87621e8709bSJuan Quintela } 87721e8709bSJuan Quintela error_propagate(errp, err); 87821e8709bSJuan Quintela } 87921e8709bSJuan Quintela 880d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 8816e790746SPaolo Bonzini { 88217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 8839711cd0dSJens Freimann Error *err = NULL; 8846e790746SPaolo Bonzini int i; 8856e790746SPaolo Bonzini 88675ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 88775ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 88875ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 88975ebec11SMaxime Coquelin } 89075ebec11SMaxime Coquelin 891ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 89259079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 89395129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 8946e790746SPaolo Bonzini 895ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 89695129d6fSCornelia Huck virtio_has_feature(features, 897bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 89895129d6fSCornelia Huck virtio_has_feature(features, 899e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 900e22f0603SYuri Benditovich virtio_has_feature(features, 901e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 9026e790746SPaolo Bonzini 9032974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9042974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 9052974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9062974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 907e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 9082974e916SYuri Benditovich 9096e790746SPaolo Bonzini if (n->has_vnet_hdr) { 910644c9858SDmitry Fleytman n->curr_guest_offloads = 911644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 912644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 9136e790746SPaolo Bonzini } 9146e790746SPaolo Bonzini 915441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 9166e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9176e790746SPaolo Bonzini 918ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9196e790746SPaolo Bonzini continue; 9206e790746SPaolo Bonzini } 921ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 9226e790746SPaolo Bonzini } 9230b1eaa88SStefan Fritsch 92495129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 9250b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 9260b1eaa88SStefan Fritsch } else { 9270b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 9280b1eaa88SStefan Fritsch } 9299711cd0dSJens Freimann 9309711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 9319711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 932e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 9339711cd0dSJens Freimann failover_add_primary(n, &err); 9349711cd0dSJens Freimann if (err) { 9351b529d90SLaurent Vivier if (!qtest_enabled()) { 9369711cd0dSJens Freimann warn_report_err(err); 9371b529d90SLaurent Vivier } else { 9381b529d90SLaurent Vivier error_free(err); 9391b529d90SLaurent Vivier } 9409711cd0dSJens Freimann } 9416e790746SPaolo Bonzini } 94221e8709bSJuan Quintela } 9436e790746SPaolo Bonzini 9446e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 9456e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 9466e790746SPaolo Bonzini { 9476e790746SPaolo Bonzini uint8_t on; 9486e790746SPaolo Bonzini size_t s; 949b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 9506e790746SPaolo Bonzini 9516e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 9526e790746SPaolo Bonzini if (s != sizeof(on)) { 9536e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9546e790746SPaolo Bonzini } 9556e790746SPaolo Bonzini 9566e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 9576e790746SPaolo Bonzini n->promisc = on; 9586e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 9596e790746SPaolo Bonzini n->allmulti = on; 9606e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 9616e790746SPaolo Bonzini n->alluni = on; 9626e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 9636e790746SPaolo Bonzini n->nomulti = on; 9646e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 9656e790746SPaolo Bonzini n->nouni = on; 9666e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 9676e790746SPaolo Bonzini n->nobcast = on; 9686e790746SPaolo Bonzini } else { 9696e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9706e790746SPaolo Bonzini } 9716e790746SPaolo Bonzini 972b1be4280SAmos Kong rxfilter_notify(nc); 973b1be4280SAmos Kong 9746e790746SPaolo Bonzini return VIRTIO_NET_OK; 9756e790746SPaolo Bonzini } 9766e790746SPaolo Bonzini 977644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 978644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 979644c9858SDmitry Fleytman { 980644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 981644c9858SDmitry Fleytman uint64_t offloads; 982644c9858SDmitry Fleytman size_t s; 983644c9858SDmitry Fleytman 98495129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 985644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 986644c9858SDmitry Fleytman } 987644c9858SDmitry Fleytman 988644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 989644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 990644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 991644c9858SDmitry Fleytman } 992644c9858SDmitry Fleytman 993644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 994644c9858SDmitry Fleytman uint64_t supported_offloads; 995644c9858SDmitry Fleytman 996189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 997189ae6bbSJason Wang 998644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 999644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1000644c9858SDmitry Fleytman } 1001644c9858SDmitry Fleytman 10022974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10032974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 10042974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10052974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 10062974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 10072974e916SYuri Benditovich 1008644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1009644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1010644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1011644c9858SDmitry Fleytman } 1012644c9858SDmitry Fleytman 1013644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1014644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1015644c9858SDmitry Fleytman 1016644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1017644c9858SDmitry Fleytman } else { 1018644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1019644c9858SDmitry Fleytman } 1020644c9858SDmitry Fleytman } 1021644c9858SDmitry Fleytman 10226e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 10236e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10246e790746SPaolo Bonzini { 10251399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 10266e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 10276e790746SPaolo Bonzini size_t s; 1028b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10296e790746SPaolo Bonzini 10306e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 10316e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 10326e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10336e790746SPaolo Bonzini } 10346e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 10356e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 10366e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1037b1be4280SAmos Kong rxfilter_notify(nc); 1038b1be4280SAmos Kong 10396e790746SPaolo Bonzini return VIRTIO_NET_OK; 10406e790746SPaolo Bonzini } 10416e790746SPaolo Bonzini 10426e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 10436e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10446e790746SPaolo Bonzini } 10456e790746SPaolo Bonzini 1046cae2e556SAmos Kong int in_use = 0; 1047cae2e556SAmos Kong int first_multi = 0; 1048cae2e556SAmos Kong uint8_t uni_overflow = 0; 1049cae2e556SAmos Kong uint8_t multi_overflow = 0; 1050cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 10516e790746SPaolo Bonzini 10526e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 10536e790746SPaolo Bonzini sizeof(mac_data.entries)); 10541399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 10556e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1056b1be4280SAmos Kong goto error; 10576e790746SPaolo Bonzini } 10586e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 10596e790746SPaolo Bonzini 10606e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1061b1be4280SAmos Kong goto error; 10626e790746SPaolo Bonzini } 10636e790746SPaolo Bonzini 10646e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1065cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 10666e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 10676e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1068b1be4280SAmos Kong goto error; 10696e790746SPaolo Bonzini } 1070cae2e556SAmos Kong in_use += mac_data.entries; 10716e790746SPaolo Bonzini } else { 1072cae2e556SAmos Kong uni_overflow = 1; 10736e790746SPaolo Bonzini } 10746e790746SPaolo Bonzini 10756e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 10766e790746SPaolo Bonzini 1077cae2e556SAmos Kong first_multi = in_use; 10786e790746SPaolo Bonzini 10796e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 10806e790746SPaolo Bonzini sizeof(mac_data.entries)); 10811399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 10826e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1083b1be4280SAmos Kong goto error; 10846e790746SPaolo Bonzini } 10856e790746SPaolo Bonzini 10866e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 10876e790746SPaolo Bonzini 10886e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1089b1be4280SAmos Kong goto error; 10906e790746SPaolo Bonzini } 10916e790746SPaolo Bonzini 1092edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1093cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 10946e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 10956e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1096b1be4280SAmos Kong goto error; 10976e790746SPaolo Bonzini } 1098cae2e556SAmos Kong in_use += mac_data.entries; 10996e790746SPaolo Bonzini } else { 1100cae2e556SAmos Kong multi_overflow = 1; 11016e790746SPaolo Bonzini } 11026e790746SPaolo Bonzini 1103cae2e556SAmos Kong n->mac_table.in_use = in_use; 1104cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1105cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1106cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1107cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1108cae2e556SAmos Kong g_free(macs); 1109b1be4280SAmos Kong rxfilter_notify(nc); 1110b1be4280SAmos Kong 11116e790746SPaolo Bonzini return VIRTIO_NET_OK; 1112b1be4280SAmos Kong 1113b1be4280SAmos Kong error: 1114cae2e556SAmos Kong g_free(macs); 1115b1be4280SAmos Kong return VIRTIO_NET_ERR; 11166e790746SPaolo Bonzini } 11176e790746SPaolo Bonzini 11186e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 11196e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11206e790746SPaolo Bonzini { 11211399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11226e790746SPaolo Bonzini uint16_t vid; 11236e790746SPaolo Bonzini size_t s; 1124b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11256e790746SPaolo Bonzini 11266e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 11271399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 11286e790746SPaolo Bonzini if (s != sizeof(vid)) { 11296e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11306e790746SPaolo Bonzini } 11316e790746SPaolo Bonzini 11326e790746SPaolo Bonzini if (vid >= MAX_VLAN) 11336e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11346e790746SPaolo Bonzini 11356e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 11366e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 11376e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 11386e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 11396e790746SPaolo Bonzini else 11406e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11416e790746SPaolo Bonzini 1142b1be4280SAmos Kong rxfilter_notify(nc); 1143b1be4280SAmos Kong 11446e790746SPaolo Bonzini return VIRTIO_NET_OK; 11456e790746SPaolo Bonzini } 11466e790746SPaolo Bonzini 1147f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1148f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1149f57fcf70SJason Wang { 11509d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1151f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1152f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1153f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 11549d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 11559d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1156f57fcf70SJason Wang } 1157f57fcf70SJason Wang return VIRTIO_NET_OK; 1158f57fcf70SJason Wang } else { 1159f57fcf70SJason Wang return VIRTIO_NET_ERR; 1160f57fcf70SJason Wang } 1161f57fcf70SJason Wang } 1162f57fcf70SJason Wang 11630145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n); 11640145c393SAndrew Melnychenko 116559079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 116659079029SYuri Benditovich { 116759079029SYuri Benditovich if (n->rss_data.enabled) { 116859079029SYuri Benditovich trace_virtio_net_rss_disable(); 116959079029SYuri Benditovich } 117059079029SYuri Benditovich n->rss_data.enabled = false; 11710145c393SAndrew Melnychenko 11720145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 11730145c393SAndrew Melnychenko } 11740145c393SAndrew Melnychenko 11750145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 11760145c393SAndrew Melnychenko { 11770145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 11780145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 11790145c393SAndrew Melnychenko return false; 11800145c393SAndrew Melnychenko } 11810145c393SAndrew Melnychenko 11820145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 11830145c393SAndrew Melnychenko } 11840145c393SAndrew Melnychenko 11850145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 11860145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 11870145c393SAndrew Melnychenko { 11880145c393SAndrew Melnychenko config->redirect = data->redirect; 11890145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 11900145c393SAndrew Melnychenko config->hash_types = data->hash_types; 11910145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 11920145c393SAndrew Melnychenko config->default_queue = data->default_queue; 11930145c393SAndrew Melnychenko } 11940145c393SAndrew Melnychenko 11950145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 11960145c393SAndrew Melnychenko { 11970145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 11980145c393SAndrew Melnychenko 11990145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 12000145c393SAndrew Melnychenko return false; 12010145c393SAndrew Melnychenko } 12020145c393SAndrew Melnychenko 12030145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 12040145c393SAndrew Melnychenko 12050145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 12060145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 12070145c393SAndrew Melnychenko return false; 12080145c393SAndrew Melnychenko } 12090145c393SAndrew Melnychenko 12100145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 12110145c393SAndrew Melnychenko return false; 12120145c393SAndrew Melnychenko } 12130145c393SAndrew Melnychenko 12140145c393SAndrew Melnychenko return true; 12150145c393SAndrew Melnychenko } 12160145c393SAndrew Melnychenko 12170145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 12180145c393SAndrew Melnychenko { 12190145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12200145c393SAndrew Melnychenko } 12210145c393SAndrew Melnychenko 12220145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n) 12230145c393SAndrew Melnychenko { 12240145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 12250145c393SAndrew Melnychenko /* backend does't support steering ebpf */ 12260145c393SAndrew Melnychenko return false; 12270145c393SAndrew Melnychenko } 12280145c393SAndrew Melnychenko 12290145c393SAndrew Melnychenko return ebpf_rss_load(&n->ebpf_rss); 12300145c393SAndrew Melnychenko } 12310145c393SAndrew Melnychenko 12320145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 12330145c393SAndrew Melnychenko { 12340145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12350145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 123659079029SYuri Benditovich } 123759079029SYuri Benditovich 123859079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1239e22f0603SYuri Benditovich struct iovec *iov, 1240e22f0603SYuri Benditovich unsigned int iov_cnt, 1241e22f0603SYuri Benditovich bool do_rss) 124259079029SYuri Benditovich { 124359079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 124459079029SYuri Benditovich struct virtio_net_rss_config cfg; 124559079029SYuri Benditovich size_t s, offset = 0, size_get; 1246441537f1SJason Wang uint16_t queue_pairs, i; 124759079029SYuri Benditovich struct { 124859079029SYuri Benditovich uint16_t us; 124959079029SYuri Benditovich uint8_t b; 125059079029SYuri Benditovich } QEMU_PACKED temp; 125159079029SYuri Benditovich const char *err_msg = ""; 125259079029SYuri Benditovich uint32_t err_value = 0; 125359079029SYuri Benditovich 1254e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 125559079029SYuri Benditovich err_msg = "RSS is not negotiated"; 125659079029SYuri Benditovich goto error; 125759079029SYuri Benditovich } 1258e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1259e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1260e22f0603SYuri Benditovich goto error; 1261e22f0603SYuri Benditovich } 126259079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 126359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 126459079029SYuri Benditovich if (s != size_get) { 126559079029SYuri Benditovich err_msg = "Short command buffer"; 126659079029SYuri Benditovich err_value = (uint32_t)s; 126759079029SYuri Benditovich goto error; 126859079029SYuri Benditovich } 126959079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 127059079029SYuri Benditovich n->rss_data.indirections_len = 127159079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 127259079029SYuri Benditovich n->rss_data.indirections_len++; 1273e22f0603SYuri Benditovich if (!do_rss) { 1274e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1275e22f0603SYuri Benditovich } 127659079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 127759079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 127859079029SYuri Benditovich err_value = n->rss_data.indirections_len; 127959079029SYuri Benditovich goto error; 128059079029SYuri Benditovich } 128159079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 128259079029SYuri Benditovich err_msg = "Too large indirection table"; 128359079029SYuri Benditovich err_value = n->rss_data.indirections_len; 128459079029SYuri Benditovich goto error; 128559079029SYuri Benditovich } 1286e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1287e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1288441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 128959079029SYuri Benditovich err_msg = "Invalid default queue"; 129059079029SYuri Benditovich err_value = n->rss_data.default_queue; 129159079029SYuri Benditovich goto error; 129259079029SYuri Benditovich } 129359079029SYuri Benditovich offset += size_get; 129459079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 129559079029SYuri Benditovich g_free(n->rss_data.indirections_table); 129659079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 129759079029SYuri Benditovich if (!n->rss_data.indirections_table) { 129859079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 129959079029SYuri Benditovich err_value = n->rss_data.indirections_len; 130059079029SYuri Benditovich goto error; 130159079029SYuri Benditovich } 130259079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 130359079029SYuri Benditovich n->rss_data.indirections_table, size_get); 130459079029SYuri Benditovich if (s != size_get) { 130559079029SYuri Benditovich err_msg = "Short indirection table buffer"; 130659079029SYuri Benditovich err_value = (uint32_t)s; 130759079029SYuri Benditovich goto error; 130859079029SYuri Benditovich } 130959079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 131059079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 131159079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 131259079029SYuri Benditovich } 131359079029SYuri Benditovich offset += size_get; 131459079029SYuri Benditovich size_get = sizeof(temp); 131559079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 131659079029SYuri Benditovich if (s != size_get) { 1317441537f1SJason Wang err_msg = "Can't get queue_pairs"; 131859079029SYuri Benditovich err_value = (uint32_t)s; 131959079029SYuri Benditovich goto error; 132059079029SYuri Benditovich } 1321441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1322441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1323441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1324441537f1SJason Wang err_value = queue_pairs; 132559079029SYuri Benditovich goto error; 132659079029SYuri Benditovich } 132759079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 132859079029SYuri Benditovich err_msg = "Invalid key size"; 132959079029SYuri Benditovich err_value = temp.b; 133059079029SYuri Benditovich goto error; 133159079029SYuri Benditovich } 133259079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 133359079029SYuri Benditovich err_msg = "No key provided"; 133459079029SYuri Benditovich err_value = 0; 133559079029SYuri Benditovich goto error; 133659079029SYuri Benditovich } 133759079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 133859079029SYuri Benditovich virtio_net_disable_rss(n); 1339441537f1SJason Wang return queue_pairs; 134059079029SYuri Benditovich } 134159079029SYuri Benditovich offset += size_get; 134259079029SYuri Benditovich size_get = temp.b; 134359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 134459079029SYuri Benditovich if (s != size_get) { 134559079029SYuri Benditovich err_msg = "Can get key buffer"; 134659079029SYuri Benditovich err_value = (uint32_t)s; 134759079029SYuri Benditovich goto error; 134859079029SYuri Benditovich } 134959079029SYuri Benditovich n->rss_data.enabled = true; 13500145c393SAndrew Melnychenko 13510145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 13520145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 13530145c393SAndrew Melnychenko /* EBPF must be loaded for vhost */ 13540145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 13550145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS for vhost"); 13560145c393SAndrew Melnychenko goto error; 13570145c393SAndrew Melnychenko } 13580145c393SAndrew Melnychenko /* fallback to software RSS */ 13590145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS - fallback to software RSS"); 13600145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 13610145c393SAndrew Melnychenko } 13620145c393SAndrew Melnychenko } else { 13630145c393SAndrew Melnychenko /* use software RSS for hash populating */ 13640145c393SAndrew Melnychenko /* and detach eBPF if was loaded before */ 13650145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 13660145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 13670145c393SAndrew Melnychenko } 13680145c393SAndrew Melnychenko 136959079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 137059079029SYuri Benditovich n->rss_data.indirections_len, 137159079029SYuri Benditovich temp.b); 1372441537f1SJason Wang return queue_pairs; 137359079029SYuri Benditovich error: 137459079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 137559079029SYuri Benditovich virtio_net_disable_rss(n); 137659079029SYuri Benditovich return 0; 137759079029SYuri Benditovich } 137859079029SYuri Benditovich 13796e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 13806e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 13816e790746SPaolo Bonzini { 138217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1383441537f1SJason Wang uint16_t queue_pairs; 1384*2a7888ccSSi-Wei Liu NetClientState *nc = qemu_get_queue(n->nic); 13856e790746SPaolo Bonzini 138659079029SYuri Benditovich virtio_net_disable_rss(n); 1387e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1388441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1389441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1390e22f0603SYuri Benditovich } 139159079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1392441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 139359079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 139459079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 139559079029SYuri Benditovich size_t s; 139659079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 139759079029SYuri Benditovich return VIRTIO_NET_ERR; 139859079029SYuri Benditovich } 13996e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 14006e790746SPaolo Bonzini if (s != sizeof(mq)) { 14016e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14026e790746SPaolo Bonzini } 1403441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 14046e790746SPaolo Bonzini 140559079029SYuri Benditovich } else { 14066e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14076e790746SPaolo Bonzini } 14086e790746SPaolo Bonzini 1409441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1410441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1411441537f1SJason Wang queue_pairs > n->max_queue_pairs || 14126e790746SPaolo Bonzini !n->multiqueue) { 14136e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14146e790746SPaolo Bonzini } 14156e790746SPaolo Bonzini 1416*2a7888ccSSi-Wei Liu /* Avoid changing the number of queue_pairs for vdpa device in 1417*2a7888ccSSi-Wei Liu * userspace handler. A future fix is needed to handle the mq 1418*2a7888ccSSi-Wei Liu * change in userspace handler with vhost-vdpa. Let's disable 1419*2a7888ccSSi-Wei Liu * the mq handling from userspace for now and only allow get 1420*2a7888ccSSi-Wei Liu * done through the kernel. Ripples may be seen when falling 1421*2a7888ccSSi-Wei Liu * back to userspace, but without doing it qemu process would 1422*2a7888ccSSi-Wei Liu * crash on a recursive entry to virtio_net_set_status(). 1423*2a7888ccSSi-Wei Liu */ 1424*2a7888ccSSi-Wei Liu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 1425*2a7888ccSSi-Wei Liu return VIRTIO_NET_ERR; 1426*2a7888ccSSi-Wei Liu } 1427*2a7888ccSSi-Wei Liu 1428441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1429441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 14306e790746SPaolo Bonzini * disabled queue */ 143117a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1432441537f1SJason Wang virtio_net_set_queue_pairs(n); 14336e790746SPaolo Bonzini 14346e790746SPaolo Bonzini return VIRTIO_NET_OK; 14356e790746SPaolo Bonzini } 1436ba7eadb5SGreg Kurz 14376e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 14386e790746SPaolo Bonzini { 143917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14406e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 14416e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 144251b19ebeSPaolo Bonzini VirtQueueElement *elem; 14436e790746SPaolo Bonzini size_t s; 1444771b6ed3SJason Wang struct iovec *iov, *iov2; 14456e790746SPaolo Bonzini unsigned int iov_cnt; 14466e790746SPaolo Bonzini 144751b19ebeSPaolo Bonzini for (;;) { 144851b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 144951b19ebeSPaolo Bonzini if (!elem) { 145051b19ebeSPaolo Bonzini break; 145151b19ebeSPaolo Bonzini } 145251b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 145351b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 1454ba7eadb5SGreg Kurz virtio_error(vdev, "virtio-net ctrl missing headers"); 1455ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1456ba7eadb5SGreg Kurz g_free(elem); 1457ba7eadb5SGreg Kurz break; 14586e790746SPaolo Bonzini } 14596e790746SPaolo Bonzini 146051b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 1461d792199dSPhilippe Mathieu-Daudé iov2 = iov = g_memdup2(elem->out_sg, 1462d792199dSPhilippe Mathieu-Daudé sizeof(struct iovec) * elem->out_num); 14636e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 14646e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 14656e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 14666e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 14676e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 14686e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 14696e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 14706e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 14716e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 14726e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 1473f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1474f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 14756e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 14766e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 1477644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1478644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 14796e790746SPaolo Bonzini } 14806e790746SPaolo Bonzini 148151b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 14826e790746SPaolo Bonzini assert(s == sizeof(status)); 14836e790746SPaolo Bonzini 148451b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 14856e790746SPaolo Bonzini virtio_notify(vdev, vq); 1486771b6ed3SJason Wang g_free(iov2); 148751b19ebeSPaolo Bonzini g_free(elem); 14886e790746SPaolo Bonzini } 14896e790746SPaolo Bonzini } 14906e790746SPaolo Bonzini 14916e790746SPaolo Bonzini /* RX */ 14926e790746SPaolo Bonzini 14936e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 14946e790746SPaolo Bonzini { 149517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14966e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 14976e790746SPaolo Bonzini 14986e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 14996e790746SPaolo Bonzini } 15006e790746SPaolo Bonzini 1501b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 15026e790746SPaolo Bonzini { 15036e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 150417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 15056e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 15066e790746SPaolo Bonzini 150717a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1508b8c4b67eSPhilippe Mathieu-Daudé return false; 15096e790746SPaolo Bonzini } 15106e790746SPaolo Bonzini 1511441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1512b8c4b67eSPhilippe Mathieu-Daudé return false; 15136e790746SPaolo Bonzini } 15146e790746SPaolo Bonzini 15156e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 151617a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1517b8c4b67eSPhilippe Mathieu-Daudé return false; 15186e790746SPaolo Bonzini } 15196e790746SPaolo Bonzini 1520b8c4b67eSPhilippe Mathieu-Daudé return true; 15216e790746SPaolo Bonzini } 15226e790746SPaolo Bonzini 15236e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 15246e790746SPaolo Bonzini { 15256e790746SPaolo Bonzini VirtIONet *n = q->n; 15266e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15276e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15286e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15296e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 15306e790746SPaolo Bonzini 15316e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 15326e790746SPaolo Bonzini * available after the above check but before notification was 15336e790746SPaolo Bonzini * enabled, check for available buffers again. 15346e790746SPaolo Bonzini */ 15356e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15366e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15376e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15386e790746SPaolo Bonzini return 0; 15396e790746SPaolo Bonzini } 15406e790746SPaolo Bonzini } 15416e790746SPaolo Bonzini 15426e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 15436e790746SPaolo Bonzini return 1; 15446e790746SPaolo Bonzini } 15456e790746SPaolo Bonzini 15461399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1547032a74a1SCédric Le Goater { 15481399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 15491399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 15501399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 15511399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1552032a74a1SCédric Le Goater } 1553032a74a1SCédric Le Goater 15546e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 15556e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 15566e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 15576e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 15586e790746SPaolo Bonzini * dhclient yet. 15596e790746SPaolo Bonzini * 15606e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 15616e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 15626e790746SPaolo Bonzini * kernels. 15636e790746SPaolo Bonzini * 15646e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 15656e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 15666e790746SPaolo Bonzini * cache. 15676e790746SPaolo Bonzini */ 15686e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 15696e790746SPaolo Bonzini uint8_t *buf, size_t size) 15706e790746SPaolo Bonzini { 15716e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 15726e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 15736e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 15746e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 15756e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1576f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 15776e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 15786e790746SPaolo Bonzini } 15796e790746SPaolo Bonzini } 15806e790746SPaolo Bonzini 15816e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 15826e790746SPaolo Bonzini const void *buf, size_t size) 15836e790746SPaolo Bonzini { 15846e790746SPaolo Bonzini if (n->has_vnet_hdr) { 15856e790746SPaolo Bonzini /* FIXME this cast is evil */ 15866e790746SPaolo Bonzini void *wbuf = (void *)buf; 15876e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 15886e790746SPaolo Bonzini size - n->host_hdr_len); 15891bfa316cSGreg Kurz 15901bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 15911399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 15921bfa316cSGreg Kurz } 15936e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 15946e790746SPaolo Bonzini } else { 15956e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 15966e790746SPaolo Bonzini .flags = 0, 15976e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 15986e790746SPaolo Bonzini }; 15996e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 16006e790746SPaolo Bonzini } 16016e790746SPaolo Bonzini } 16026e790746SPaolo Bonzini 16036e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 16046e790746SPaolo Bonzini { 16056e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 16066e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 16076e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 16086e790746SPaolo Bonzini int i; 16096e790746SPaolo Bonzini 16106e790746SPaolo Bonzini if (n->promisc) 16116e790746SPaolo Bonzini return 1; 16126e790746SPaolo Bonzini 16136e790746SPaolo Bonzini ptr += n->host_hdr_len; 16146e790746SPaolo Bonzini 16156e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 16167542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 16176e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 16186e790746SPaolo Bonzini return 0; 16196e790746SPaolo Bonzini } 16206e790746SPaolo Bonzini 16216e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 16226e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 16236e790746SPaolo Bonzini return !n->nobcast; 16246e790746SPaolo Bonzini } else if (n->nomulti) { 16256e790746SPaolo Bonzini return 0; 16266e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 16276e790746SPaolo Bonzini return 1; 16286e790746SPaolo Bonzini } 16296e790746SPaolo Bonzini 16306e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 16316e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 16326e790746SPaolo Bonzini return 1; 16336e790746SPaolo Bonzini } 16346e790746SPaolo Bonzini } 16356e790746SPaolo Bonzini } else { // unicast 16366e790746SPaolo Bonzini if (n->nouni) { 16376e790746SPaolo Bonzini return 0; 16386e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 16396e790746SPaolo Bonzini return 1; 16406e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 16416e790746SPaolo Bonzini return 1; 16426e790746SPaolo Bonzini } 16436e790746SPaolo Bonzini 16446e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 16456e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 16466e790746SPaolo Bonzini return 1; 16476e790746SPaolo Bonzini } 16486e790746SPaolo Bonzini } 16496e790746SPaolo Bonzini } 16506e790746SPaolo Bonzini 16516e790746SPaolo Bonzini return 0; 16526e790746SPaolo Bonzini } 16536e790746SPaolo Bonzini 16544474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4, 16554474e37aSYuri Benditovich bool isip6, 16564474e37aSYuri Benditovich bool isudp, 16574474e37aSYuri Benditovich bool istcp, 16584474e37aSYuri Benditovich uint32_t types) 16594474e37aSYuri Benditovich { 16604474e37aSYuri Benditovich if (isip4) { 16614474e37aSYuri Benditovich if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) { 16624474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 16634474e37aSYuri Benditovich } 16644474e37aSYuri Benditovich if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) { 16654474e37aSYuri Benditovich return NetPktRssIpV4Udp; 16664474e37aSYuri Benditovich } 16674474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 16684474e37aSYuri Benditovich return NetPktRssIpV4; 16694474e37aSYuri Benditovich } 16704474e37aSYuri Benditovich } else if (isip6) { 16714474e37aSYuri Benditovich uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 16724474e37aSYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6; 16734474e37aSYuri Benditovich 16744474e37aSYuri Benditovich if (istcp && (types & mask)) { 16754474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ? 16764474e37aSYuri Benditovich NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp; 16774474e37aSYuri Benditovich } 16784474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6; 16794474e37aSYuri Benditovich if (isudp && (types & mask)) { 16804474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ? 16814474e37aSYuri Benditovich NetPktRssIpV6UdpEx : NetPktRssIpV6Udp; 16824474e37aSYuri Benditovich } 16834474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6; 16844474e37aSYuri Benditovich if (types & mask) { 16854474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ? 16864474e37aSYuri Benditovich NetPktRssIpV6Ex : NetPktRssIpV6; 16874474e37aSYuri Benditovich } 16884474e37aSYuri Benditovich } 16894474e37aSYuri Benditovich return 0xff; 16904474e37aSYuri Benditovich } 16914474e37aSYuri Benditovich 1692e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1693e22f0603SYuri Benditovich uint32_t hash) 1694e22f0603SYuri Benditovich { 1695e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1696e22f0603SYuri Benditovich hdr->hash_value = hash; 1697e22f0603SYuri Benditovich hdr->hash_report = report; 1698e22f0603SYuri Benditovich } 1699e22f0603SYuri Benditovich 17004474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 170197cd965cSPaolo Bonzini size_t size) 17026e790746SPaolo Bonzini { 17036e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1704e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 17054474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 17064474e37aSYuri Benditovich uint8_t net_hash_type; 17074474e37aSYuri Benditovich uint32_t hash; 17084474e37aSYuri Benditovich bool isip4, isip6, isudp, istcp; 1709e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1710e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1711e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1712e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1713e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1714e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1715e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1716e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1717e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1718e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1719e22f0603SYuri Benditovich }; 17204474e37aSYuri Benditovich 17214474e37aSYuri Benditovich net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len, 17224474e37aSYuri Benditovich size - n->host_hdr_len); 17234474e37aSYuri Benditovich net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp); 17244474e37aSYuri Benditovich if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) { 17254474e37aSYuri Benditovich istcp = isudp = false; 17264474e37aSYuri Benditovich } 17274474e37aSYuri Benditovich if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) { 17284474e37aSYuri Benditovich istcp = isudp = false; 17294474e37aSYuri Benditovich } 17304474e37aSYuri Benditovich net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp, 17314474e37aSYuri Benditovich n->rss_data.hash_types); 17324474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1733e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1734e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1735e22f0603SYuri Benditovich } 1736e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 17374474e37aSYuri Benditovich } 17384474e37aSYuri Benditovich 17394474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1740e22f0603SYuri Benditovich 1741e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1742e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1743e22f0603SYuri Benditovich } 1744e22f0603SYuri Benditovich 1745e22f0603SYuri Benditovich if (n->rss_data.redirect) { 17464474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 17474474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 17484474e37aSYuri Benditovich } 1749e22f0603SYuri Benditovich 1750e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 17514474e37aSYuri Benditovich } 17524474e37aSYuri Benditovich 17534474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 17544474e37aSYuri Benditovich size_t size, bool no_rss) 17554474e37aSYuri Benditovich { 17564474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 17576e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 175817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1759bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1760bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 17616e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 17626e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 17636e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1764bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1765bedd7e93SJason Wang ssize_t err; 17666e790746SPaolo Bonzini 17676e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 17686e790746SPaolo Bonzini return -1; 17696e790746SPaolo Bonzini } 17706e790746SPaolo Bonzini 17710145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 17724474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 17734474e37aSYuri Benditovich if (index >= 0) { 17744474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 17754474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 17764474e37aSYuri Benditovich } 17774474e37aSYuri Benditovich } 17784474e37aSYuri Benditovich 17796e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 17806e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 17816e790746SPaolo Bonzini return 0; 17826e790746SPaolo Bonzini } 17836e790746SPaolo Bonzini 17846e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 17856e790746SPaolo Bonzini return size; 17866e790746SPaolo Bonzini 17876e790746SPaolo Bonzini offset = i = 0; 17886e790746SPaolo Bonzini 17896e790746SPaolo Bonzini while (offset < size) { 179051b19ebeSPaolo Bonzini VirtQueueElement *elem; 17916e790746SPaolo Bonzini int len, total; 179251b19ebeSPaolo Bonzini const struct iovec *sg; 17936e790746SPaolo Bonzini 17946e790746SPaolo Bonzini total = 0; 17956e790746SPaolo Bonzini 1796bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1797bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1798bedd7e93SJason Wang err = size; 1799bedd7e93SJason Wang goto err; 1800bedd7e93SJason Wang } 1801bedd7e93SJason Wang 180251b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 180351b19ebeSPaolo Bonzini if (!elem) { 1804ba10b9c0SGreg Kurz if (i) { 1805ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 18066e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1807019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1808019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 18096e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1810019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1811019a3edbSGerd Hoffmann vdev->guest_features); 1812ba10b9c0SGreg Kurz } 1813bedd7e93SJason Wang err = -1; 1814bedd7e93SJason Wang goto err; 18156e790746SPaolo Bonzini } 18166e790746SPaolo Bonzini 181751b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1818ba10b9c0SGreg Kurz virtio_error(vdev, 1819ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1820ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1821ba10b9c0SGreg Kurz g_free(elem); 1822bedd7e93SJason Wang err = -1; 1823bedd7e93SJason Wang goto err; 18246e790746SPaolo Bonzini } 18256e790746SPaolo Bonzini 182651b19ebeSPaolo Bonzini sg = elem->in_sg; 18276e790746SPaolo Bonzini if (i == 0) { 18286e790746SPaolo Bonzini assert(offset == 0); 18296e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 18306e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 183151b19ebeSPaolo Bonzini sg, elem->in_num, 18326e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 18336e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 18346e790746SPaolo Bonzini } 18356e790746SPaolo Bonzini 183651b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1837e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1838e22f0603SYuri Benditovich offset = sizeof(mhdr); 1839e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1840e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 1841e22f0603SYuri Benditovich } 18426e790746SPaolo Bonzini offset = n->host_hdr_len; 18436e790746SPaolo Bonzini total += n->guest_hdr_len; 18446e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 18456e790746SPaolo Bonzini } else { 18466e790746SPaolo Bonzini guest_offset = 0; 18476e790746SPaolo Bonzini } 18486e790746SPaolo Bonzini 18496e790746SPaolo Bonzini /* copy in packet. ugh */ 185051b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 18516e790746SPaolo Bonzini buf + offset, size - offset); 18526e790746SPaolo Bonzini total += len; 18536e790746SPaolo Bonzini offset += len; 18546e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 18556e790746SPaolo Bonzini * must have consumed the complete packet. 18566e790746SPaolo Bonzini * Otherwise, drop it. */ 18576e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 185827e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 185951b19ebeSPaolo Bonzini g_free(elem); 1860bedd7e93SJason Wang err = size; 1861bedd7e93SJason Wang goto err; 18626e790746SPaolo Bonzini } 18636e790746SPaolo Bonzini 1864bedd7e93SJason Wang elems[i] = elem; 1865bedd7e93SJason Wang lens[i] = total; 1866bedd7e93SJason Wang i++; 18676e790746SPaolo Bonzini } 18686e790746SPaolo Bonzini 18696e790746SPaolo Bonzini if (mhdr_cnt) { 18701399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 18716e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 18726e790746SPaolo Bonzini 0, 18736e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 18746e790746SPaolo Bonzini } 18756e790746SPaolo Bonzini 1876bedd7e93SJason Wang for (j = 0; j < i; j++) { 1877bedd7e93SJason Wang /* signal other side */ 1878bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 1879bedd7e93SJason Wang g_free(elems[j]); 1880bedd7e93SJason Wang } 1881bedd7e93SJason Wang 18826e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 188317a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 18846e790746SPaolo Bonzini 18856e790746SPaolo Bonzini return size; 1886bedd7e93SJason Wang 1887bedd7e93SJason Wang err: 1888bedd7e93SJason Wang for (j = 0; j < i; j++) { 1889abe300d9SJason Wang virtqueue_detach_element(q->rx_vq, elems[j], lens[j]); 1890bedd7e93SJason Wang g_free(elems[j]); 1891bedd7e93SJason Wang } 1892bedd7e93SJason Wang 1893bedd7e93SJason Wang return err; 18946e790746SPaolo Bonzini } 18956e790746SPaolo Bonzini 18962974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 189797cd965cSPaolo Bonzini size_t size) 189897cd965cSPaolo Bonzini { 1899068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 190097cd965cSPaolo Bonzini 19014474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 190297cd965cSPaolo Bonzini } 190397cd965cSPaolo Bonzini 19042974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 19052974e916SYuri Benditovich const uint8_t *buf, 19062974e916SYuri Benditovich VirtioNetRscUnit *unit) 19072974e916SYuri Benditovich { 19082974e916SYuri Benditovich uint16_t ip_hdrlen; 19092974e916SYuri Benditovich struct ip_header *ip; 19102974e916SYuri Benditovich 19112974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 19122974e916SYuri Benditovich + sizeof(struct eth_header)); 19132974e916SYuri Benditovich unit->ip = (void *)ip; 19142974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 19152974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 19162974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 19172974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19182974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 19192974e916SYuri Benditovich } 19202974e916SYuri Benditovich 19212974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 19222974e916SYuri Benditovich const uint8_t *buf, 19232974e916SYuri Benditovich VirtioNetRscUnit *unit) 19242974e916SYuri Benditovich { 19252974e916SYuri Benditovich struct ip6_header *ip6; 19262974e916SYuri Benditovich 19272974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 19282974e916SYuri Benditovich + sizeof(struct eth_header)); 19292974e916SYuri Benditovich unit->ip = ip6; 19302974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 193178ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 19322974e916SYuri Benditovich + sizeof(struct ip6_header)); 19332974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19342974e916SYuri Benditovich 19352974e916SYuri Benditovich /* There is a difference between payload lenght in ipv4 and v6, 19362974e916SYuri Benditovich ip header is excluded in ipv6 */ 19372974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 19382974e916SYuri Benditovich } 19392974e916SYuri Benditovich 19402974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 19412974e916SYuri Benditovich VirtioNetRscSeg *seg) 19422974e916SYuri Benditovich { 19432974e916SYuri Benditovich int ret; 1944dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 19452974e916SYuri Benditovich 1946dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 19472974e916SYuri Benditovich h->flags = 0; 19482974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 19492974e916SYuri Benditovich 19502974e916SYuri Benditovich if (seg->is_coalesced) { 1951dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 1952dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 19532974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 19542974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 19552974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 19562974e916SYuri Benditovich } else { 19572974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 19582974e916SYuri Benditovich } 19592974e916SYuri Benditovich } 19602974e916SYuri Benditovich 19612974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 19622974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 19632974e916SYuri Benditovich g_free(seg->buf); 19642974e916SYuri Benditovich g_free(seg); 19652974e916SYuri Benditovich 19662974e916SYuri Benditovich return ret; 19672974e916SYuri Benditovich } 19682974e916SYuri Benditovich 19692974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 19702974e916SYuri Benditovich { 19712974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 19722974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 19732974e916SYuri Benditovich 19742974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 19752974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 19762974e916SYuri Benditovich chain->stat.purge_failed++; 19772974e916SYuri Benditovich continue; 19782974e916SYuri Benditovich } 19792974e916SYuri Benditovich } 19802974e916SYuri Benditovich 19812974e916SYuri Benditovich chain->stat.timer++; 19822974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 19832974e916SYuri Benditovich timer_mod(chain->drain_timer, 19842974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 19852974e916SYuri Benditovich } 19862974e916SYuri Benditovich } 19872974e916SYuri Benditovich 19882974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 19892974e916SYuri Benditovich { 19902974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 19912974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 19922974e916SYuri Benditovich 19932974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 19942974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 19952974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 19962974e916SYuri Benditovich g_free(seg->buf); 19972974e916SYuri Benditovich g_free(seg); 19982974e916SYuri Benditovich } 19992974e916SYuri Benditovich 20002974e916SYuri Benditovich timer_free(chain->drain_timer); 20012974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 20022974e916SYuri Benditovich g_free(chain); 20032974e916SYuri Benditovich } 20042974e916SYuri Benditovich } 20052974e916SYuri Benditovich 20062974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 20072974e916SYuri Benditovich NetClientState *nc, 20082974e916SYuri Benditovich const uint8_t *buf, size_t size) 20092974e916SYuri Benditovich { 20102974e916SYuri Benditovich uint16_t hdr_len; 20112974e916SYuri Benditovich VirtioNetRscSeg *seg; 20122974e916SYuri Benditovich 20132974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 2014b21e2380SMarkus Armbruster seg = g_new(VirtioNetRscSeg, 1); 20152974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 20162974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 20172974e916SYuri Benditovich memcpy(seg->buf, buf, size); 20182974e916SYuri Benditovich seg->size = size; 20192974e916SYuri Benditovich seg->packets = 1; 20202974e916SYuri Benditovich seg->dup_ack = 0; 20212974e916SYuri Benditovich seg->is_coalesced = 0; 20222974e916SYuri Benditovich seg->nc = nc; 20232974e916SYuri Benditovich 20242974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 20252974e916SYuri Benditovich chain->stat.cache++; 20262974e916SYuri Benditovich 20272974e916SYuri Benditovich switch (chain->proto) { 20282974e916SYuri Benditovich case ETH_P_IP: 20292974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 20302974e916SYuri Benditovich break; 20312974e916SYuri Benditovich case ETH_P_IPV6: 20322974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 20332974e916SYuri Benditovich break; 20342974e916SYuri Benditovich default: 20352974e916SYuri Benditovich g_assert_not_reached(); 20362974e916SYuri Benditovich } 20372974e916SYuri Benditovich } 20382974e916SYuri Benditovich 20392974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 20402974e916SYuri Benditovich VirtioNetRscSeg *seg, 20412974e916SYuri Benditovich const uint8_t *buf, 20422974e916SYuri Benditovich struct tcp_header *n_tcp, 20432974e916SYuri Benditovich struct tcp_header *o_tcp) 20442974e916SYuri Benditovich { 20452974e916SYuri Benditovich uint32_t nack, oack; 20462974e916SYuri Benditovich uint16_t nwin, owin; 20472974e916SYuri Benditovich 20482974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 20492974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 20502974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 20512974e916SYuri Benditovich owin = htons(o_tcp->th_win); 20522974e916SYuri Benditovich 20532974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 20542974e916SYuri Benditovich chain->stat.ack_out_of_win++; 20552974e916SYuri Benditovich return RSC_FINAL; 20562974e916SYuri Benditovich } else if (nack == oack) { 20572974e916SYuri Benditovich /* duplicated ack or window probe */ 20582974e916SYuri Benditovich if (nwin == owin) { 20592974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 20602974e916SYuri Benditovich chain->stat.dup_ack++; 20612974e916SYuri Benditovich return RSC_FINAL; 20622974e916SYuri Benditovich } else { 20632974e916SYuri Benditovich /* Coalesce window update */ 20642974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 20652974e916SYuri Benditovich chain->stat.win_update++; 20662974e916SYuri Benditovich return RSC_COALESCE; 20672974e916SYuri Benditovich } 20682974e916SYuri Benditovich } else { 20692974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 20702974e916SYuri Benditovich chain->stat.pure_ack++; 20712974e916SYuri Benditovich return RSC_FINAL; 20722974e916SYuri Benditovich } 20732974e916SYuri Benditovich } 20742974e916SYuri Benditovich 20752974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 20762974e916SYuri Benditovich VirtioNetRscSeg *seg, 20772974e916SYuri Benditovich const uint8_t *buf, 20782974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 20792974e916SYuri Benditovich { 20802974e916SYuri Benditovich void *data; 20812974e916SYuri Benditovich uint16_t o_ip_len; 20822974e916SYuri Benditovich uint32_t nseq, oseq; 20832974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 20842974e916SYuri Benditovich 20852974e916SYuri Benditovich o_unit = &seg->unit; 20862974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 20872974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 20882974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 20892974e916SYuri Benditovich 20902974e916SYuri Benditovich /* out of order or retransmitted. */ 20912974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 20922974e916SYuri Benditovich chain->stat.data_out_of_win++; 20932974e916SYuri Benditovich return RSC_FINAL; 20942974e916SYuri Benditovich } 20952974e916SYuri Benditovich 20962974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 20972974e916SYuri Benditovich if (nseq == oseq) { 20982974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 20992974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 21002974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 21012974e916SYuri Benditovich goto coalesce; 21022974e916SYuri Benditovich } else { 21032974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 21042974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 21052974e916SYuri Benditovich } 21062974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 21072974e916SYuri Benditovich /* Not a consistent packet, out of order */ 21082974e916SYuri Benditovich chain->stat.data_out_of_order++; 21092974e916SYuri Benditovich return RSC_FINAL; 21102974e916SYuri Benditovich } else { 21112974e916SYuri Benditovich coalesce: 21122974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 21132974e916SYuri Benditovich chain->stat.over_size++; 21142974e916SYuri Benditovich return RSC_FINAL; 21152974e916SYuri Benditovich } 21162974e916SYuri Benditovich 21172974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 21182974e916SYuri Benditovich so use the field value to update and record the new data len */ 21192974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 21202974e916SYuri Benditovich 21212974e916SYuri Benditovich /* update field in ip header */ 21222974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 21232974e916SYuri Benditovich 21242974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 21252974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 21262974e916SYuri Benditovich guest (only if it uses RSC feature). */ 21272974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 21282974e916SYuri Benditovich 21292974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 21302974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 21312974e916SYuri Benditovich 21322974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 21332974e916SYuri Benditovich seg->size += n_unit->payload; 21342974e916SYuri Benditovich seg->packets++; 21352974e916SYuri Benditovich chain->stat.coalesced++; 21362974e916SYuri Benditovich return RSC_COALESCE; 21372974e916SYuri Benditovich } 21382974e916SYuri Benditovich } 21392974e916SYuri Benditovich 21402974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 21412974e916SYuri Benditovich VirtioNetRscSeg *seg, 21422974e916SYuri Benditovich const uint8_t *buf, size_t size, 21432974e916SYuri Benditovich VirtioNetRscUnit *unit) 21442974e916SYuri Benditovich { 21452974e916SYuri Benditovich struct ip_header *ip1, *ip2; 21462974e916SYuri Benditovich 21472974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 21482974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 21492974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 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 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 21602974e916SYuri Benditovich VirtioNetRscSeg *seg, 21612974e916SYuri Benditovich const uint8_t *buf, size_t size, 21622974e916SYuri Benditovich VirtioNetRscUnit *unit) 21632974e916SYuri Benditovich { 21642974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 21652974e916SYuri Benditovich 21662974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 21672974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 21682974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 21692974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 21702974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 21712974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 21722974e916SYuri Benditovich chain->stat.no_match++; 21732974e916SYuri Benditovich return RSC_NO_MATCH; 21742974e916SYuri Benditovich } 21752974e916SYuri Benditovich 21762974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 21772974e916SYuri Benditovich } 21782974e916SYuri Benditovich 21792974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 21802974e916SYuri Benditovich * to prevent out of order */ 21812974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 21822974e916SYuri Benditovich struct tcp_header *tcp) 21832974e916SYuri Benditovich { 21842974e916SYuri Benditovich uint16_t tcp_hdr; 21852974e916SYuri Benditovich uint16_t tcp_flag; 21862974e916SYuri Benditovich 21872974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 21882974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 21892974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 21902974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 21912974e916SYuri Benditovich chain->stat.tcp_syn++; 21922974e916SYuri Benditovich return RSC_BYPASS; 21932974e916SYuri Benditovich } 21942974e916SYuri Benditovich 21952974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 21962974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 21972974e916SYuri Benditovich return RSC_FINAL; 21982974e916SYuri Benditovich } 21992974e916SYuri Benditovich 22002974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 22012974e916SYuri Benditovich chain->stat.tcp_all_opt++; 22022974e916SYuri Benditovich return RSC_FINAL; 22032974e916SYuri Benditovich } 22042974e916SYuri Benditovich 22052974e916SYuri Benditovich return RSC_CANDIDATE; 22062974e916SYuri Benditovich } 22072974e916SYuri Benditovich 22082974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 22092974e916SYuri Benditovich NetClientState *nc, 22102974e916SYuri Benditovich const uint8_t *buf, size_t size, 22112974e916SYuri Benditovich VirtioNetRscUnit *unit) 22122974e916SYuri Benditovich { 22132974e916SYuri Benditovich int ret; 22142974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 22152974e916SYuri Benditovich 22162974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 22172974e916SYuri Benditovich chain->stat.empty_cache++; 22182974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 22192974e916SYuri Benditovich timer_mod(chain->drain_timer, 22202974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 22212974e916SYuri Benditovich return size; 22222974e916SYuri Benditovich } 22232974e916SYuri Benditovich 22242974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22252974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 22262974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 22272974e916SYuri Benditovich } else { 22282974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 22292974e916SYuri Benditovich } 22302974e916SYuri Benditovich 22312974e916SYuri Benditovich if (ret == RSC_FINAL) { 22322974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 22332974e916SYuri Benditovich /* Send failed */ 22342974e916SYuri Benditovich chain->stat.final_failed++; 22352974e916SYuri Benditovich return 0; 22362974e916SYuri Benditovich } 22372974e916SYuri Benditovich 22382974e916SYuri Benditovich /* Send current packet */ 22392974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22402974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 22412974e916SYuri Benditovich continue; 22422974e916SYuri Benditovich } else { 22432974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 22442974e916SYuri Benditovich seg->is_coalesced = 1; 22452974e916SYuri Benditovich return size; 22462974e916SYuri Benditovich } 22472974e916SYuri Benditovich } 22482974e916SYuri Benditovich 22492974e916SYuri Benditovich chain->stat.no_match_cache++; 22502974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 22512974e916SYuri Benditovich return size; 22522974e916SYuri Benditovich } 22532974e916SYuri Benditovich 22542974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 22552974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 22562974e916SYuri Benditovich NetClientState *nc, 22572974e916SYuri Benditovich const uint8_t *buf, size_t size, 22582974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 22592974e916SYuri Benditovich uint16_t tcp_port) 22602974e916SYuri Benditovich { 22612974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 22622974e916SYuri Benditovich uint32_t ppair1, ppair2; 22632974e916SYuri Benditovich 22642974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 22652974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22662974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 22672974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 22682974e916SYuri Benditovich || (ppair1 != ppair2)) { 22692974e916SYuri Benditovich continue; 22702974e916SYuri Benditovich } 22712974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 22722974e916SYuri Benditovich chain->stat.drain_failed++; 22732974e916SYuri Benditovich } 22742974e916SYuri Benditovich 22752974e916SYuri Benditovich break; 22762974e916SYuri Benditovich } 22772974e916SYuri Benditovich 22782974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22792974e916SYuri Benditovich } 22802974e916SYuri Benditovich 22812974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 22822974e916SYuri Benditovich struct ip_header *ip, 22832974e916SYuri Benditovich const uint8_t *buf, size_t size) 22842974e916SYuri Benditovich { 22852974e916SYuri Benditovich uint16_t ip_len; 22862974e916SYuri Benditovich 22872974e916SYuri Benditovich /* Not an ipv4 packet */ 22882974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 22892974e916SYuri Benditovich chain->stat.ip_option++; 22902974e916SYuri Benditovich return RSC_BYPASS; 22912974e916SYuri Benditovich } 22922974e916SYuri Benditovich 22932974e916SYuri Benditovich /* Don't handle packets with ip option */ 22942974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 22952974e916SYuri Benditovich chain->stat.ip_option++; 22962974e916SYuri Benditovich return RSC_BYPASS; 22972974e916SYuri Benditovich } 22982974e916SYuri Benditovich 22992974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 23002974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23012974e916SYuri Benditovich return RSC_BYPASS; 23022974e916SYuri Benditovich } 23032974e916SYuri Benditovich 23042974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 23052974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 23062974e916SYuri Benditovich chain->stat.ip_frag++; 23072974e916SYuri Benditovich return RSC_BYPASS; 23082974e916SYuri Benditovich } 23092974e916SYuri Benditovich 23102974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 23112974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 23122974e916SYuri Benditovich chain->stat.ip_ecn++; 23132974e916SYuri Benditovich return RSC_BYPASS; 23142974e916SYuri Benditovich } 23152974e916SYuri Benditovich 23162974e916SYuri Benditovich ip_len = htons(ip->ip_len); 23172974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 23182974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 23192974e916SYuri Benditovich sizeof(struct eth_header))) { 23202974e916SYuri Benditovich chain->stat.ip_hacked++; 23212974e916SYuri Benditovich return RSC_BYPASS; 23222974e916SYuri Benditovich } 23232974e916SYuri Benditovich 23242974e916SYuri Benditovich return RSC_CANDIDATE; 23252974e916SYuri Benditovich } 23262974e916SYuri Benditovich 23272974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 23282974e916SYuri Benditovich NetClientState *nc, 23292974e916SYuri Benditovich const uint8_t *buf, size_t size) 23302974e916SYuri Benditovich { 23312974e916SYuri Benditovich int32_t ret; 23322974e916SYuri Benditovich uint16_t hdr_len; 23332974e916SYuri Benditovich VirtioNetRscUnit unit; 23342974e916SYuri Benditovich 23352974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 23362974e916SYuri Benditovich 23372974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 23382974e916SYuri Benditovich + sizeof(struct tcp_header))) { 23392974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23402974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23412974e916SYuri Benditovich } 23422974e916SYuri Benditovich 23432974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 23442974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 23452974e916SYuri Benditovich != RSC_CANDIDATE) { 23462974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23472974e916SYuri Benditovich } 23482974e916SYuri Benditovich 23492974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 23502974e916SYuri Benditovich if (ret == RSC_BYPASS) { 23512974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23522974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 23532974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 23542974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 23552974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 23562974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 23572974e916SYuri Benditovich } 23582974e916SYuri Benditovich 23592974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 23602974e916SYuri Benditovich } 23612974e916SYuri Benditovich 23622974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 23632974e916SYuri Benditovich struct ip6_header *ip6, 23642974e916SYuri Benditovich const uint8_t *buf, size_t size) 23652974e916SYuri Benditovich { 23662974e916SYuri Benditovich uint16_t ip_len; 23672974e916SYuri Benditovich 23682974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 23692974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 23702974e916SYuri Benditovich return RSC_BYPASS; 23712974e916SYuri Benditovich } 23722974e916SYuri Benditovich 23732974e916SYuri Benditovich /* Both option and protocol is checked in this */ 23742974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 23752974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23762974e916SYuri Benditovich return RSC_BYPASS; 23772974e916SYuri Benditovich } 23782974e916SYuri Benditovich 23792974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 23802974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 23812974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 23822974e916SYuri Benditovich - sizeof(struct ip6_header))) { 23832974e916SYuri Benditovich chain->stat.ip_hacked++; 23842974e916SYuri Benditovich return RSC_BYPASS; 23852974e916SYuri Benditovich } 23862974e916SYuri Benditovich 23872974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 23882974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 23892974e916SYuri Benditovich chain->stat.ip_ecn++; 23902974e916SYuri Benditovich return RSC_BYPASS; 23912974e916SYuri Benditovich } 23922974e916SYuri Benditovich 23932974e916SYuri Benditovich return RSC_CANDIDATE; 23942974e916SYuri Benditovich } 23952974e916SYuri Benditovich 23962974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 23972974e916SYuri Benditovich const uint8_t *buf, size_t size) 23982974e916SYuri Benditovich { 23992974e916SYuri Benditovich int32_t ret; 24002974e916SYuri Benditovich uint16_t hdr_len; 24012974e916SYuri Benditovich VirtioNetRscChain *chain; 24022974e916SYuri Benditovich VirtioNetRscUnit unit; 24032974e916SYuri Benditovich 24042974e916SYuri Benditovich chain = (VirtioNetRscChain *)opq; 24052974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 24062974e916SYuri Benditovich 24072974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 24082974e916SYuri Benditovich + sizeof(tcp_header))) { 24092974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24102974e916SYuri Benditovich } 24112974e916SYuri Benditovich 24122974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 24132974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 24142974e916SYuri Benditovich unit.ip, buf, size)) { 24152974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24162974e916SYuri Benditovich } 24172974e916SYuri Benditovich 24182974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24192974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24202974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24212974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24222974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24232974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 24242974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 24252974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 24262974e916SYuri Benditovich + sizeof(struct ip6_header)); 24272974e916SYuri Benditovich } 24282974e916SYuri Benditovich 24292974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 24302974e916SYuri Benditovich } 24312974e916SYuri Benditovich 24322974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 24332974e916SYuri Benditovich NetClientState *nc, 24342974e916SYuri Benditovich uint16_t proto) 24352974e916SYuri Benditovich { 24362974e916SYuri Benditovich VirtioNetRscChain *chain; 24372974e916SYuri Benditovich 24382974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 24392974e916SYuri Benditovich return NULL; 24402974e916SYuri Benditovich } 24412974e916SYuri Benditovich 24422974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 24432974e916SYuri Benditovich if (chain->proto == proto) { 24442974e916SYuri Benditovich return chain; 24452974e916SYuri Benditovich } 24462974e916SYuri Benditovich } 24472974e916SYuri Benditovich 24482974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 24492974e916SYuri Benditovich chain->n = n; 24502974e916SYuri Benditovich chain->proto = proto; 24512974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 24522974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 24532974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 24542974e916SYuri Benditovich } else { 24552974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 24562974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 24572974e916SYuri Benditovich } 24582974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 24592974e916SYuri Benditovich virtio_net_rsc_purge, chain); 24602974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 24612974e916SYuri Benditovich 24622974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 24632974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 24642974e916SYuri Benditovich 24652974e916SYuri Benditovich return chain; 24662974e916SYuri Benditovich } 24672974e916SYuri Benditovich 24682974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 24692974e916SYuri Benditovich const uint8_t *buf, 24702974e916SYuri Benditovich size_t size) 24712974e916SYuri Benditovich { 24722974e916SYuri Benditovich uint16_t proto; 24732974e916SYuri Benditovich VirtioNetRscChain *chain; 24742974e916SYuri Benditovich struct eth_header *eth; 24752974e916SYuri Benditovich VirtIONet *n; 24762974e916SYuri Benditovich 24772974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 24782974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 24792974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24802974e916SYuri Benditovich } 24812974e916SYuri Benditovich 24822974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 24832974e916SYuri Benditovich proto = htons(eth->h_proto); 24842974e916SYuri Benditovich 24852974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 24862974e916SYuri Benditovich if (chain) { 24872974e916SYuri Benditovich chain->stat.received++; 24882974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 24892974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 24902974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 24912974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 24922974e916SYuri Benditovich } 24932974e916SYuri Benditovich } 24942974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24952974e916SYuri Benditovich } 24962974e916SYuri Benditovich 24972974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 24982974e916SYuri Benditovich size_t size) 24992974e916SYuri Benditovich { 25002974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 25012974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 25022974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 25032974e916SYuri Benditovich } else { 25042974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25052974e916SYuri Benditovich } 25062974e916SYuri Benditovich } 25072974e916SYuri Benditovich 25086e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 25096e790746SPaolo Bonzini 25106e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 25116e790746SPaolo Bonzini { 25126e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 25136e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 251417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 25156e790746SPaolo Bonzini 251651b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 251717a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 25186e790746SPaolo Bonzini 251951b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 252051b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 25216e790746SPaolo Bonzini 25226e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 25236e790746SPaolo Bonzini virtio_net_flush_tx(q); 25246e790746SPaolo Bonzini } 25256e790746SPaolo Bonzini 25266e790746SPaolo Bonzini /* TX */ 25276e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 25286e790746SPaolo Bonzini { 25296e790746SPaolo Bonzini VirtIONet *n = q->n; 253017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 253151b19ebeSPaolo Bonzini VirtQueueElement *elem; 25326e790746SPaolo Bonzini int32_t num_packets = 0; 25336e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 253417a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 25356e790746SPaolo Bonzini return num_packets; 25366e790746SPaolo Bonzini } 25376e790746SPaolo Bonzini 253851b19ebeSPaolo Bonzini if (q->async_tx.elem) { 25396e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 25406e790746SPaolo Bonzini return num_packets; 25416e790746SPaolo Bonzini } 25426e790746SPaolo Bonzini 254351b19ebeSPaolo Bonzini for (;;) { 2544bd89dd98SJason Wang ssize_t ret; 254551b19ebeSPaolo Bonzini unsigned int out_num; 254651b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2547feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 25486e790746SPaolo Bonzini 254951b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 255051b19ebeSPaolo Bonzini if (!elem) { 255151b19ebeSPaolo Bonzini break; 255251b19ebeSPaolo Bonzini } 255351b19ebeSPaolo Bonzini 255451b19ebeSPaolo Bonzini out_num = elem->out_num; 255551b19ebeSPaolo Bonzini out_sg = elem->out_sg; 25566e790746SPaolo Bonzini if (out_num < 1) { 2557fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2558fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2559fa5e56c2SGreg Kurz g_free(elem); 2560fa5e56c2SGreg Kurz return -EINVAL; 25616e790746SPaolo Bonzini } 25626e790746SPaolo Bonzini 2563032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 2564feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 2565feb93f36SJason Wang n->guest_hdr_len) { 2566fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2567fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2568fa5e56c2SGreg Kurz g_free(elem); 2569fa5e56c2SGreg Kurz return -EINVAL; 2570032a74a1SCédric Le Goater } 25711bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2572feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 2573feb93f36SJason Wang sg2[0].iov_base = &mhdr; 2574feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2575feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2576feb93f36SJason Wang out_sg, out_num, 2577feb93f36SJason Wang n->guest_hdr_len, -1); 2578feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2579feb93f36SJason Wang goto drop; 2580032a74a1SCédric Le Goater } 2581feb93f36SJason Wang out_num += 1; 2582feb93f36SJason Wang out_sg = sg2; 2583feb93f36SJason Wang } 2584feb93f36SJason Wang } 25856e790746SPaolo Bonzini /* 25866e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 25876e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 25886e790746SPaolo Bonzini * that host is interested in. 25896e790746SPaolo Bonzini */ 25906e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 25916e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 25926e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 25936e790746SPaolo Bonzini out_sg, out_num, 25946e790746SPaolo Bonzini 0, n->host_hdr_len); 25956e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 25966e790746SPaolo Bonzini out_sg, out_num, 25976e790746SPaolo Bonzini n->guest_hdr_len, -1); 25986e790746SPaolo Bonzini out_num = sg_num; 25996e790746SPaolo Bonzini out_sg = sg; 26006e790746SPaolo Bonzini } 26016e790746SPaolo Bonzini 26026e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 26036e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 26046e790746SPaolo Bonzini if (ret == 0) { 26056e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26066e790746SPaolo Bonzini q->async_tx.elem = elem; 26076e790746SPaolo Bonzini return -EBUSY; 26086e790746SPaolo Bonzini } 26096e790746SPaolo Bonzini 2610feb93f36SJason Wang drop: 261151b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 261217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 261351b19ebeSPaolo Bonzini g_free(elem); 26146e790746SPaolo Bonzini 26156e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 26166e790746SPaolo Bonzini break; 26176e790746SPaolo Bonzini } 26186e790746SPaolo Bonzini } 26196e790746SPaolo Bonzini return num_packets; 26206e790746SPaolo Bonzini } 26216e790746SPaolo Bonzini 26226e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 26236e790746SPaolo Bonzini { 262417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 26256e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 26266e790746SPaolo Bonzini 2627283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2628283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2629283e2c2aSYuri Benditovich return; 2630283e2c2aSYuri Benditovich } 2631283e2c2aSYuri Benditovich 26326e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 263317a0ca55SKONRAD Frederic if (!vdev->vm_running) { 26346e790746SPaolo Bonzini q->tx_waiting = 1; 26356e790746SPaolo Bonzini return; 26366e790746SPaolo Bonzini } 26376e790746SPaolo Bonzini 26386e790746SPaolo Bonzini if (q->tx_waiting) { 26396e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 2640bc72ad67SAlex Bligh timer_del(q->tx_timer); 26416e790746SPaolo Bonzini q->tx_waiting = 0; 2642fa5e56c2SGreg Kurz if (virtio_net_flush_tx(q) == -EINVAL) { 2643fa5e56c2SGreg Kurz return; 2644fa5e56c2SGreg Kurz } 26456e790746SPaolo Bonzini } else { 2646bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2647bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 26486e790746SPaolo Bonzini q->tx_waiting = 1; 26496e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 26506e790746SPaolo Bonzini } 26516e790746SPaolo Bonzini } 26526e790746SPaolo Bonzini 26536e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 26546e790746SPaolo Bonzini { 265517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 26566e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 26576e790746SPaolo Bonzini 2658283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2659283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2660283e2c2aSYuri Benditovich return; 2661283e2c2aSYuri Benditovich } 2662283e2c2aSYuri Benditovich 26636e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 26646e790746SPaolo Bonzini return; 26656e790746SPaolo Bonzini } 26666e790746SPaolo Bonzini q->tx_waiting = 1; 26676e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 266817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 26696e790746SPaolo Bonzini return; 26706e790746SPaolo Bonzini } 26716e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 26726e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 26736e790746SPaolo Bonzini } 26746e790746SPaolo Bonzini 26756e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 26766e790746SPaolo Bonzini { 26776e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 26786e790746SPaolo Bonzini VirtIONet *n = q->n; 267917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2680e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2681e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2682e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2683e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2684e8bcf842SMichael S. Tsirkin return; 2685e8bcf842SMichael S. Tsirkin } 26866e790746SPaolo Bonzini 26876e790746SPaolo Bonzini q->tx_waiting = 0; 26886e790746SPaolo Bonzini 26896e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 269017a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 26916e790746SPaolo Bonzini return; 269217a0ca55SKONRAD Frederic } 26936e790746SPaolo Bonzini 26946e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 26956e790746SPaolo Bonzini virtio_net_flush_tx(q); 26966e790746SPaolo Bonzini } 26976e790746SPaolo Bonzini 26986e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 26996e790746SPaolo Bonzini { 27006e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 27016e790746SPaolo Bonzini VirtIONet *n = q->n; 270217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 27036e790746SPaolo Bonzini int32_t ret; 27046e790746SPaolo Bonzini 2705e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2706e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2707e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2708e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2709e8bcf842SMichael S. Tsirkin return; 2710e8bcf842SMichael S. Tsirkin } 27116e790746SPaolo Bonzini 27126e790746SPaolo Bonzini q->tx_waiting = 0; 27136e790746SPaolo Bonzini 27146e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 271517a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 27166e790746SPaolo Bonzini return; 271717a0ca55SKONRAD Frederic } 27186e790746SPaolo Bonzini 27196e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2720fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2721fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2722fa5e56c2SGreg Kurz * broken */ 27236e790746SPaolo Bonzini } 27246e790746SPaolo Bonzini 27256e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 27266e790746SPaolo Bonzini * more coming and immediately reschedule */ 27276e790746SPaolo Bonzini if (ret >= n->tx_burst) { 27286e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27296e790746SPaolo Bonzini q->tx_waiting = 1; 27306e790746SPaolo Bonzini return; 27316e790746SPaolo Bonzini } 27326e790746SPaolo Bonzini 27336e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 27346e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 27356e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 27366e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2737fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2738fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2739fa5e56c2SGreg Kurz return; 2740fa5e56c2SGreg Kurz } else if (ret > 0) { 27416e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 27426e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27436e790746SPaolo Bonzini q->tx_waiting = 1; 27446e790746SPaolo Bonzini } 27456e790746SPaolo Bonzini } 27466e790746SPaolo Bonzini 2747f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2748f9d6dbf0SWen Congyang { 2749f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2750f9d6dbf0SWen Congyang 27511c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 27521c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 27539b02e161SWei Wang 2754f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2755f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 27569b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 27579b02e161SWei Wang virtio_net_handle_tx_timer); 2758f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2759f9d6dbf0SWen Congyang virtio_net_tx_timer, 2760f9d6dbf0SWen Congyang &n->vqs[index]); 2761f9d6dbf0SWen Congyang } else { 2762f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 27639b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 27649b02e161SWei Wang virtio_net_handle_tx_bh); 2765f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 2766f9d6dbf0SWen Congyang } 2767f9d6dbf0SWen Congyang 2768f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2769f9d6dbf0SWen Congyang n->vqs[index].n = n; 2770f9d6dbf0SWen Congyang } 2771f9d6dbf0SWen Congyang 2772f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2773f9d6dbf0SWen Congyang { 2774f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2775f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2776f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2777f9d6dbf0SWen Congyang 2778f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2779f9d6dbf0SWen Congyang 2780f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2781f9d6dbf0SWen Congyang if (q->tx_timer) { 2782f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2783f989c30cSYunjian Wang q->tx_timer = NULL; 2784f9d6dbf0SWen Congyang } else { 2785f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2786f989c30cSYunjian Wang q->tx_bh = NULL; 2787f9d6dbf0SWen Congyang } 2788f989c30cSYunjian Wang q->tx_waiting = 0; 2789f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2790f9d6dbf0SWen Congyang } 2791f9d6dbf0SWen Congyang 2792441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 2793f9d6dbf0SWen Congyang { 2794f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2795f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2796441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 2797f9d6dbf0SWen Congyang int i; 2798f9d6dbf0SWen Congyang 2799f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2800f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2801f9d6dbf0SWen Congyang 2802f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 2803f9d6dbf0SWen Congyang return; 2804f9d6dbf0SWen Congyang } 2805f9d6dbf0SWen Congyang 2806f9d6dbf0SWen Congyang /* 2807f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 2808f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 280920f86a75SYuval Shaia * and then we only enter one of the following two loops. 2810f9d6dbf0SWen Congyang */ 2811f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 2812f9d6dbf0SWen Congyang 2813f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 2814f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 2815f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 2816f9d6dbf0SWen Congyang } 2817f9d6dbf0SWen Congyang 2818f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 2819f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 2820f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 2821f9d6dbf0SWen Congyang } 2822f9d6dbf0SWen Congyang 2823f9d6dbf0SWen Congyang /* add ctrl_vq last */ 2824f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 2825f9d6dbf0SWen Congyang } 2826f9d6dbf0SWen Congyang 2827ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 28286e790746SPaolo Bonzini { 2829441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 2830f9d6dbf0SWen Congyang 28316e790746SPaolo Bonzini n->multiqueue = multiqueue; 2832441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 28336e790746SPaolo Bonzini 2834441537f1SJason Wang virtio_net_set_queue_pairs(n); 28356e790746SPaolo Bonzini } 28366e790746SPaolo Bonzini 2837982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 2838037dab2fSGreg Kurz { 2839982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 2840982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 2841037dab2fSGreg Kurz int i, link_down; 2842037dab2fSGreg Kurz 28439d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 2844982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 284595129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 2846e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 2847e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 2848e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 28496e790746SPaolo Bonzini 28506e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 2851982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 28526e790746SPaolo Bonzini n->mac_table.in_use = 0; 28536e790746SPaolo Bonzini } 28546e790746SPaolo Bonzini 2855982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 28566c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 28576c666823SMichael S. Tsirkin } 28586c666823SMichael S. Tsirkin 28597788c3f2SMikhail Sennikovsky /* 28607788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 28617788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 28627788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 28637788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 28647788c3f2SMikhail Sennikovsky */ 28657788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 28666c666823SMichael S. Tsirkin 2867441537f1SJason Wang virtio_net_set_queue_pairs(n); 28686e790746SPaolo Bonzini 28696e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 28706e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 28716e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 28726e790746SPaolo Bonzini break; 28736e790746SPaolo Bonzini } 28746e790746SPaolo Bonzini } 28756e790746SPaolo Bonzini n->mac_table.first_multi = i; 28766e790746SPaolo Bonzini 28776e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 28786e790746SPaolo Bonzini * to link status bit in n->status */ 28796e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 2880441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 28816e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 28826e790746SPaolo Bonzini } 28836e790746SPaolo Bonzini 28846c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 28856c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 28869d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 28879d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 28889d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 28899d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 28909d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 28919d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 28929d8c6a25SDr. David Alan Gilbert } else { 2893944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 28949d8c6a25SDr. David Alan Gilbert } 28956c666823SMichael S. Tsirkin } 28966c666823SMichael S. Tsirkin 2897e41b7114SYuri Benditovich if (n->rss_data.enabled) { 28980145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 28990145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 29000145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 29010145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 29020145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS for vhost"); 29030145c393SAndrew Melnychenko } else { 29040145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS - " 29050145c393SAndrew Melnychenko "fallback to software RSS"); 29060145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 29070145c393SAndrew Melnychenko } 29080145c393SAndrew Melnychenko } 29090145c393SAndrew Melnychenko } 29100145c393SAndrew Melnychenko 2911e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 2912e41b7114SYuri Benditovich n->rss_data.indirections_len, 2913e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 2914e41b7114SYuri Benditovich } else { 2915e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 2916e41b7114SYuri Benditovich } 29176e790746SPaolo Bonzini return 0; 29186e790746SPaolo Bonzini } 29196e790746SPaolo Bonzini 29207788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 29217788c3f2SMikhail Sennikovsky { 29227788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 29237788c3f2SMikhail Sennikovsky /* 29247788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 29257788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 29267788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 29277788c3f2SMikhail Sennikovsky */ 29287788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 29297788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 29307788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 29317788c3f2SMikhail Sennikovsky } 29327788c3f2SMikhail Sennikovsky 29337788c3f2SMikhail Sennikovsky return 0; 29347788c3f2SMikhail Sennikovsky } 29357788c3f2SMikhail Sennikovsky 2936982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 2937982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 2938982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 2939982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 2940982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 2941982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 2942982b78c5SDr. David Alan Gilbert }, 2943982b78c5SDr. David Alan Gilbert }; 2944982b78c5SDr. David Alan Gilbert 2945441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 2946982b78c5SDr. David Alan Gilbert { 2947441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 2948982b78c5SDr. David Alan Gilbert } 2949982b78c5SDr. David Alan Gilbert 2950982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 2951982b78c5SDr. David Alan Gilbert { 2952982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 2953982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 2954982b78c5SDr. David Alan Gilbert } 2955982b78c5SDr. David Alan Gilbert 2956982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 2957982b78c5SDr. David Alan Gilbert { 2958982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 2959982b78c5SDr. David Alan Gilbert } 2960982b78c5SDr. David Alan Gilbert 2961982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 2962982b78c5SDr. David Alan Gilbert { 2963982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 2964982b78c5SDr. David Alan Gilbert } 2965982b78c5SDr. David Alan Gilbert 2966982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 2967982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 2968982b78c5SDr. David Alan Gilbert */ 2969982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 2970982b78c5SDr. David Alan Gilbert VirtIONet *parent; 2971982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 2972441537f1SJason Wang uint16_t curr_queue_pairs_1; 2973982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 2974982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 2975982b78c5SDr. David Alan Gilbert }; 2976982b78c5SDr. David Alan Gilbert 2977982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 2978441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 2979982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 2980982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 2981982b78c5SDr. David Alan Gilbert */ 2982982b78c5SDr. David Alan Gilbert 298344b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 2984982b78c5SDr. David Alan Gilbert { 2985982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2986982b78c5SDr. David Alan Gilbert 2987982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 2988441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 2989441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 2990441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 2991982b78c5SDr. David Alan Gilbert } 299244b1ff31SDr. David Alan Gilbert 299344b1ff31SDr. David Alan Gilbert return 0; 2994982b78c5SDr. David Alan Gilbert } 2995982b78c5SDr. David Alan Gilbert 2996982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 2997982b78c5SDr. David Alan Gilbert { 2998982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 2999982b78c5SDr. David Alan Gilbert 3000982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 3001982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 3002982b78c5SDr. David Alan Gilbert 3003441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 3004441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 3005441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 3006982b78c5SDr. David Alan Gilbert 3007982b78c5SDr. David Alan Gilbert return -EINVAL; 3008982b78c5SDr. David Alan Gilbert } 3009982b78c5SDr. David Alan Gilbert 3010982b78c5SDr. David Alan Gilbert return 0; /* all good */ 3011982b78c5SDr. David Alan Gilbert } 3012982b78c5SDr. David Alan Gilbert 3013982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 3014982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 3015982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 3016982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 3017982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3018982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 3019441537f1SJason Wang curr_queue_pairs_1, 3020982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3021982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3022982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3023982b78c5SDr. David Alan Gilbert }, 3024982b78c5SDr. David Alan Gilbert }; 3025982b78c5SDr. David Alan Gilbert 3026982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3027982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3028982b78c5SDr. David Alan Gilbert */ 3029982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3030982b78c5SDr. David Alan Gilbert { 3031982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3032982b78c5SDr. David Alan Gilbert 3033982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3034982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3035982b78c5SDr. David Alan Gilbert return -EINVAL; 3036982b78c5SDr. David Alan Gilbert } 3037982b78c5SDr. David Alan Gilbert 3038982b78c5SDr. David Alan Gilbert return 0; 3039982b78c5SDr. David Alan Gilbert } 3040982b78c5SDr. David Alan Gilbert 304144b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3042982b78c5SDr. David Alan Gilbert { 3043982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3044982b78c5SDr. David Alan Gilbert 3045982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 304644b1ff31SDr. David Alan Gilbert 304744b1ff31SDr. David Alan Gilbert return 0; 3048982b78c5SDr. David Alan Gilbert } 3049982b78c5SDr. David Alan Gilbert 3050982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3051982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3052982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3053982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 3054982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3055982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3056982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3057982b78c5SDr. David Alan Gilbert }, 3058982b78c5SDr. David Alan Gilbert }; 3059982b78c5SDr. David Alan Gilbert 3060982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3061982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3062982b78c5SDr. David Alan Gilbert */ 3063982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3064982b78c5SDr. David Alan Gilbert { 3065982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3066982b78c5SDr. David Alan Gilbert 3067982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3068982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3069982b78c5SDr. David Alan Gilbert return -EINVAL; 3070982b78c5SDr. David Alan Gilbert } 3071982b78c5SDr. David Alan Gilbert 3072982b78c5SDr. David Alan Gilbert return 0; 3073982b78c5SDr. David Alan Gilbert } 3074982b78c5SDr. David Alan Gilbert 307544b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3076982b78c5SDr. David Alan Gilbert { 3077982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3078982b78c5SDr. David Alan Gilbert 3079982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 308044b1ff31SDr. David Alan Gilbert 308144b1ff31SDr. David Alan Gilbert return 0; 3082982b78c5SDr. David Alan Gilbert } 3083982b78c5SDr. David Alan Gilbert 3084982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3085982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3086982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3087982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 3088982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3089982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3090982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3091982b78c5SDr. David Alan Gilbert }, 3092982b78c5SDr. David Alan Gilbert }; 3093982b78c5SDr. David Alan Gilbert 3094e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3095e41b7114SYuri Benditovich { 3096e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3097e41b7114SYuri Benditovich } 3098e41b7114SYuri Benditovich 3099e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3100e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3101e41b7114SYuri Benditovich .version_id = 1, 3102e41b7114SYuri Benditovich .minimum_version_id = 1, 3103e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 3104e41b7114SYuri Benditovich .fields = (VMStateField[]) { 3105e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3106e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3107e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3108e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3109e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3110e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3111e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3112e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3113e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3114e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3115e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3116e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3117e41b7114SYuri Benditovich }, 3118e41b7114SYuri Benditovich }; 3119e41b7114SYuri Benditovich 3120982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3121982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3122982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3123982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3124982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 3125982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3126982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3127982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3128982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3129982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3130982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3131982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3132982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3133982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3134982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3135982b78c5SDr. David Alan Gilbert 3136982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3137982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3138982b78c5SDr. David Alan Gilbert * sets flags in this case. 3139982b78c5SDr. David Alan Gilbert */ 3140982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3141982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3142982b78c5SDr. David Alan Gilbert ETH_ALEN), 3143982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3144982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3145982b78c5SDr. David Alan Gilbert 3146982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3147982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3148982b78c5SDr. David Alan Gilbert * but based on the uint. 3149982b78c5SDr. David Alan Gilbert */ 3150982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3151982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3152982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3153982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3154982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3155982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3156982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3157982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3158982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3159982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3160982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3161441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3162982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3163441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3164982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3165982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3166982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3167982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3168982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3169982b78c5SDr. David Alan Gilbert }, 3170e41b7114SYuri Benditovich .subsections = (const VMStateDescription * []) { 3171e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3172e41b7114SYuri Benditovich NULL 3173e41b7114SYuri Benditovich } 3174982b78c5SDr. David Alan Gilbert }; 3175982b78c5SDr. David Alan Gilbert 31766e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3177f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 31786e790746SPaolo Bonzini .size = sizeof(NICState), 31796e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 31806e790746SPaolo Bonzini .receive = virtio_net_receive, 31816e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3182b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3183b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 31846e790746SPaolo Bonzini }; 31856e790746SPaolo Bonzini 31866e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 31876e790746SPaolo Bonzini { 318817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 318968b0a639SSi-Wei Liu NetClientState *nc; 31906e790746SPaolo Bonzini assert(n->vhost_started); 319168b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 319268b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 319368b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 319468b0a639SSi-Wei Liu * buggy migration stream. 319568b0a639SSi-Wei Liu */ 319668b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 319768b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 319868b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 319968b0a639SSi-Wei Liu return false; 320068b0a639SSi-Wei Liu } 320168b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 320268b0a639SSi-Wei Liu } else { 320368b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 320468b0a639SSi-Wei Liu } 3205ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 32066e790746SPaolo Bonzini } 32076e790746SPaolo Bonzini 32086e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 32096e790746SPaolo Bonzini bool mask) 32106e790746SPaolo Bonzini { 321117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 321268b0a639SSi-Wei Liu NetClientState *nc; 32136e790746SPaolo Bonzini assert(n->vhost_started); 321468b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 321568b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 321668b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 321768b0a639SSi-Wei Liu * buggy migration stream. 321868b0a639SSi-Wei Liu */ 321968b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 322068b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 322168b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 322268b0a639SSi-Wei Liu return; 322368b0a639SSi-Wei Liu } 322468b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 322568b0a639SSi-Wei Liu } else { 322668b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 322768b0a639SSi-Wei Liu } 3228a882b571SMichael S. Tsirkin vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 3229a882b571SMichael S. Tsirkin vdev, idx, mask); 32306e790746SPaolo Bonzini } 32316e790746SPaolo Bonzini 3232019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 32336e790746SPaolo Bonzini { 32340cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3235a93e599dSMaxime Coquelin 3236ba550851SStefano Garzarella n->config_size = virtio_feature_get_config_size(feature_sizes, 3237ba550851SStefano Garzarella host_features); 323817ec5a86SKONRAD Frederic } 32396e790746SPaolo Bonzini 32408a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 32418a253ec2SKONRAD Frederic const char *type) 32428a253ec2SKONRAD Frederic { 32438a253ec2SKONRAD Frederic /* 32448a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 32458a253ec2SKONRAD Frederic */ 32468a253ec2SKONRAD Frederic assert(type != NULL); 32478a253ec2SKONRAD Frederic 32488a253ec2SKONRAD Frederic g_free(n->netclient_name); 32498a253ec2SKONRAD Frederic g_free(n->netclient_type); 32508a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 32518a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 32528a253ec2SKONRAD Frederic } 32538a253ec2SKONRAD Frederic 32540e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 32559711cd0dSJens Freimann { 32569711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 32579711cd0dSJens Freimann PCIDevice *pci_dev; 32589711cd0dSJens Freimann Error *err = NULL; 32599711cd0dSJens Freimann 32600e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 32619711cd0dSJens Freimann if (hotplug_ctrl) { 32620e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 32639711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 32640e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 32659711cd0dSJens Freimann if (err) { 32669711cd0dSJens Freimann error_report_err(err); 32679711cd0dSJens Freimann return false; 32689711cd0dSJens Freimann } 32699711cd0dSJens Freimann } else { 32709711cd0dSJens Freimann return false; 32719711cd0dSJens Freimann } 32729711cd0dSJens Freimann return true; 32739711cd0dSJens Freimann } 32749711cd0dSJens Freimann 32750e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 32760e9a65c5SJuan Quintela Error **errp) 32779711cd0dSJens Freimann { 32785a0948d3SMarkus Armbruster Error *err = NULL; 32799711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 32800e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 328178274682SJuan Quintela BusState *primary_bus; 32829711cd0dSJens Freimann 32839711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 32849711cd0dSJens Freimann return true; 32859711cd0dSJens Freimann } 32860e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 328778274682SJuan Quintela if (!primary_bus) { 3288150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 32895a0948d3SMarkus Armbruster return false; 32909711cd0dSJens Freimann } 32910e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3292e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 32930e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 32949711cd0dSJens Freimann if (hotplug_ctrl) { 32950e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 32965a0948d3SMarkus Armbruster if (err) { 32975a0948d3SMarkus Armbruster goto out; 32985a0948d3SMarkus Armbruster } 32990e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 33009711cd0dSJens Freimann } 3301109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3302150ab54aSJens Freimann 3303150ab54aSJens Freimann out: 33045a0948d3SMarkus Armbruster error_propagate(errp, err); 33055a0948d3SMarkus Armbruster return !err; 33069711cd0dSJens Freimann } 33079711cd0dSJens Freimann 330807a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s) 33099711cd0dSJens Freimann { 33109711cd0dSJens Freimann bool should_be_hidden; 33119711cd0dSJens Freimann Error *err = NULL; 331207a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 331307a5d816SJuan Quintela 331407a5d816SJuan Quintela if (!dev) { 331507a5d816SJuan Quintela return; 331607a5d816SJuan Quintela } 33179711cd0dSJens Freimann 3318e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 33199711cd0dSJens Freimann 33204dbac1aeSMarkus Armbruster if (migration_in_setup(s) && !should_be_hidden) { 332107a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 332207a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 332307a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3324e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 33259711cd0dSJens Freimann } else { 33269711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 33279711cd0dSJens Freimann } 33289711cd0dSJens Freimann } else if (migration_has_failed(s)) { 3329150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 333007a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 33319711cd0dSJens Freimann if (err) { 33329711cd0dSJens Freimann error_report_err(err); 33339711cd0dSJens Freimann } 33349711cd0dSJens Freimann } 33359711cd0dSJens Freimann } 33369711cd0dSJens Freimann } 33379711cd0dSJens Freimann 33389711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) 33399711cd0dSJens Freimann { 33409711cd0dSJens Freimann MigrationState *s = data; 33419711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 33429711cd0dSJens Freimann virtio_net_handle_migration_primary(n, s); 33439711cd0dSJens Freimann } 33449711cd0dSJens Freimann 3345b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3346f3558b1bSKevin Wolf const QDict *device_opts, 3347f3558b1bSKevin Wolf bool from_json, 3348f3558b1bSKevin Wolf Error **errp) 33499711cd0dSJens Freimann { 33509711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 33514f0303aeSJuan Quintela const char *standby_id; 33529711cd0dSJens Freimann 33534d0e59acSJens Freimann if (!device_opts) { 335489631fedSJuan Quintela return false; 33554d0e59acSJens Freimann } 3356bcfc906bSLaurent Vivier 3357bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3358bcfc906bSLaurent Vivier return false; 3359bcfc906bSLaurent Vivier } 3360bcfc906bSLaurent Vivier 3361bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3362bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3363bcfc906bSLaurent Vivier return false; 3364bcfc906bSLaurent Vivier } 3365bcfc906bSLaurent Vivier 3366bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 336789631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 336889631fedSJuan Quintela return false; 33699711cd0dSJens Freimann } 33709711cd0dSJens Freimann 33717fe7791eSLaurent Vivier /* 33727fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 33737fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 33747fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 33757fe7791eSLaurent Vivier * device. 33767fe7791eSLaurent Vivier */ 3377259a10dbSKevin Wolf if (n->primary_opts) { 33787fe7791eSLaurent Vivier const char *old, *new; 33797fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 33807fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 33817fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 33827fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 33837fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 33847fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3385259a10dbSKevin Wolf return false; 3386259a10dbSKevin Wolf } 33877fe7791eSLaurent Vivier } else { 3388f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3389f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 33907fe7791eSLaurent Vivier } 3391259a10dbSKevin Wolf 3392e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 33933abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 33949711cd0dSJens Freimann } 33959711cd0dSJens Freimann 3396e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 339717ec5a86SKONRAD Frederic { 3398e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3399284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3400284a32f0SAndreas Färber NetClientState *nc; 34011773d9eeSKONRAD Frederic int i; 340217ec5a86SKONRAD Frederic 3403a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3404127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3405a93e599dSMaxime Coquelin } 3406a93e599dSMaxime Coquelin 34079473939eSJason Baron if (n->net_conf.duplex_str) { 34089473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 34099473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 34109473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 34119473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 34129473939eSJason Baron } else { 34139473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3414843c4cfcSMarkus Armbruster return; 34159473939eSJason Baron } 34169473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 34179473939eSJason Baron } else { 34189473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 34199473939eSJason Baron } 34209473939eSJason Baron 34219473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 34229473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3423843c4cfcSMarkus Armbruster return; 3424843c4cfcSMarkus Armbruster } 3425843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 34269473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 34279473939eSJason Baron } 34289473939eSJason Baron 34299711cd0dSJens Freimann if (n->failover) { 3430b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3431e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 34329711cd0dSJens Freimann device_listener_register(&n->primary_listener); 34339711cd0dSJens Freimann n->migration_state.notify = virtio_net_migration_state_notifier; 34349711cd0dSJens Freimann add_migration_state_change_notifier(&n->migration_state); 34359711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 34369711cd0dSJens Freimann } 34379711cd0dSJens Freimann 3438da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 34393857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_NET, n->config_size); 344017ec5a86SKONRAD Frederic 34411c0fbfa3SMichael S. Tsirkin /* 34421c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 34431c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 34441c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 34451c0fbfa3SMichael S. Tsirkin */ 34461c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 34471c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 34485f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 34491c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 34501c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 34511c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 34521c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 34531c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 34541c0fbfa3SMichael S. Tsirkin return; 34551c0fbfa3SMichael S. Tsirkin } 34561c0fbfa3SMichael S. Tsirkin 34579b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 34589b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 34599b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 34609b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 34619b02e161SWei Wang "must be a power of 2 between %d and %d", 34629b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 34639b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 34649b02e161SWei Wang virtio_cleanup(vdev); 34659b02e161SWei Wang return; 34669b02e161SWei Wang } 34679b02e161SWei Wang 346822288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 346922288fe5SJason Wang 347022288fe5SJason Wang /* 347122288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 347222288fe5SJason Wang * provide control queue via peers as well. 347322288fe5SJason Wang */ 347422288fe5SJason Wang if (n->nic_conf.peers.queues) { 347522288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 347622288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 347722288fe5SJason Wang ++n->max_queue_pairs; 347822288fe5SJason Wang } 347922288fe5SJason Wang } 348022288fe5SJason Wang } 348122288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 348222288fe5SJason Wang 3483441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 348422288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3485631b22eaSStefan Weil "must be a positive integer less than %d.", 3486441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 34877e0e736eSJason Wang virtio_cleanup(vdev); 34887e0e736eSJason Wang return; 34897e0e736eSJason Wang } 3490b21e2380SMarkus Armbruster n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs); 3491441537f1SJason Wang n->curr_queue_pairs = 1; 34921773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 34936e790746SPaolo Bonzini 34941773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 34951773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 34960765691eSMarkus Armbruster warn_report("virtio-net: " 34976e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 34981773d9eeSKONRAD Frederic n->net_conf.tx); 34990765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 35006e790746SPaolo Bonzini } 35016e790746SPaolo Bonzini 35022eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 35032eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 35049b02e161SWei Wang 3505441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3506f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3507da51a335SJason Wang } 3508da51a335SJason Wang 350917a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 35101773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 35111773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 35126e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 35139d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 35149d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3515f57fcf70SJason Wang virtio_net_announce_timer, n); 3516b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 35176e790746SPaolo Bonzini 35188a253ec2SKONRAD Frederic if (n->netclient_type) { 35198a253ec2SKONRAD Frederic /* 35208a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 35218a253ec2SKONRAD Frederic */ 35228a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 35238a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 35248a253ec2SKONRAD Frederic } else { 35251773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 3526284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 35278a253ec2SKONRAD Frederic } 35288a253ec2SKONRAD Frederic 3529441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3530d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3531d4c62930SBin Meng } 3532d4c62930SBin Meng 35336e790746SPaolo Bonzini peer_test_vnet_hdr(n); 35346e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 3535441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3536d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 35376e790746SPaolo Bonzini } 35386e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 35396e790746SPaolo Bonzini } else { 35406e790746SPaolo Bonzini n->host_hdr_len = 0; 35416e790746SPaolo Bonzini } 35426e790746SPaolo Bonzini 35431773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 35446e790746SPaolo Bonzini 35456e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 35461773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3547e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 35486e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 35496e790746SPaolo Bonzini 35506e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 35516e790746SPaolo Bonzini 35526e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 35536e790746SPaolo Bonzini 3554b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3555b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3556b1be4280SAmos Kong 3557e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3558e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3559e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3560e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3561e87936eaSCindy Lu (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER); 3562e87936eaSCindy Lu } 35632974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3564284a32f0SAndreas Färber n->qdev = dev; 35654474e37aSYuri Benditovich 35664474e37aSYuri Benditovich net_rx_pkt_init(&n->rx_pkt, false); 35670145c393SAndrew Melnychenko 35680145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 35690145c393SAndrew Melnychenko virtio_net_load_ebpf(n); 35700145c393SAndrew Melnychenko } 357117ec5a86SKONRAD Frederic } 357217ec5a86SKONRAD Frederic 3573b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 357417ec5a86SKONRAD Frederic { 3575306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3576306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3577441537f1SJason Wang int i, max_queue_pairs; 357817ec5a86SKONRAD Frederic 35790145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 35800145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 35810145c393SAndrew Melnychenko } 35820145c393SAndrew Melnychenko 358317ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 358417ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 358517ec5a86SKONRAD Frederic 35868a253ec2SKONRAD Frederic g_free(n->netclient_name); 35878a253ec2SKONRAD Frederic n->netclient_name = NULL; 35888a253ec2SKONRAD Frederic g_free(n->netclient_type); 35898a253ec2SKONRAD Frederic n->netclient_type = NULL; 35908a253ec2SKONRAD Frederic 359117ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 359217ec5a86SKONRAD Frederic g_free(n->vlans); 359317ec5a86SKONRAD Frederic 35949711cd0dSJens Freimann if (n->failover) { 3595f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 359665018100SJuan Quintela device_listener_unregister(&n->primary_listener); 35971e157667SLaurent Vivier remove_migration_state_change_notifier(&n->migration_state); 3598f3558b1bSKevin Wolf } else { 3599f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 36009711cd0dSJens Freimann } 36019711cd0dSJens Freimann 3602441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3603441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3604f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 360517ec5a86SKONRAD Frederic } 3606d945d9f1SYuri Benditovich /* delete also control vq */ 3607441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3608944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 360917ec5a86SKONRAD Frederic g_free(n->vqs); 361017ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 36112974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 361259079029SYuri Benditovich g_free(n->rss_data.indirections_table); 36134474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 36146a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 361517ec5a86SKONRAD Frederic } 361617ec5a86SKONRAD Frederic 361717ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 361817ec5a86SKONRAD Frederic { 361917ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 362017ec5a86SKONRAD Frederic 362117ec5a86SKONRAD Frederic /* 362217ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 362317ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 362417ec5a86SKONRAD Frederic */ 362517ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3626aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3627aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 362840c2281cSMarkus Armbruster DEVICE(n)); 36290145c393SAndrew Melnychenko 36300145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 363117ec5a86SKONRAD Frederic } 363217ec5a86SKONRAD Frederic 363344b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 36344d45dcfbSHalil Pasic { 36354d45dcfbSHalil Pasic VirtIONet *n = opaque; 36364d45dcfbSHalil Pasic 36374d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 36384d45dcfbSHalil Pasic * it might keep writing to memory. */ 36394d45dcfbSHalil Pasic assert(!n->vhost_started); 364044b1ff31SDr. David Alan Gilbert 364144b1ff31SDr. David Alan Gilbert return 0; 36424d45dcfbSHalil Pasic } 36434d45dcfbSHalil Pasic 36449711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 36459711cd0dSJens Freimann { 36469711cd0dSJens Freimann DeviceState *dev = opaque; 364721e8709bSJuan Quintela DeviceState *primary; 36489711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 36499711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 36509711cd0dSJens Freimann 3651284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3652284f42a5SJens Freimann return false; 3653284f42a5SJens Freimann } 365421e8709bSJuan Quintela primary = failover_find_primary_device(n); 365521e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 36569711cd0dSJens Freimann } 36579711cd0dSJens Freimann 36589711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 36599711cd0dSJens Freimann { 36609711cd0dSJens Freimann DeviceState *dev = opaque; 36619711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 36629711cd0dSJens Freimann 36639711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 36649711cd0dSJens Freimann } 36659711cd0dSJens Freimann 3666c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) 3667c255488dSJonah Palmer { 3668c255488dSJonah Palmer VirtIONet *n = VIRTIO_NET(vdev); 3669c255488dSJonah Palmer NetClientState *nc = qemu_get_queue(n->nic); 3670c255488dSJonah Palmer struct vhost_net *net = get_vhost_net(nc->peer); 3671c255488dSJonah Palmer return &net->dev; 3672c255488dSJonah Palmer } 3673c255488dSJonah Palmer 36744d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 36754d45dcfbSHalil Pasic .name = "virtio-net", 36764d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 36774d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 36784d45dcfbSHalil Pasic .fields = (VMStateField[]) { 36794d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 36804d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 36814d45dcfbSHalil Pasic }, 36824d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 36839711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 36844d45dcfbSHalil Pasic }; 3685290c2428SDr. David Alan Gilbert 368617ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3687127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3688127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3689127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 369087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3691127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3692127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 369387108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3694127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 369587108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3696127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 369787108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3698127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 369987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3700127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 370187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3702127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 370387108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3704127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 370587108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3706127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 370787108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3708127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 370987108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3710127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 371187108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3712127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 371387108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3714127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 371587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3716127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 371787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3718127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 371987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3720127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 372187108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3722127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 372387108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3724127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 372587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3726127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 372759079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 372859079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3729e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3730e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 37312974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 37322974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 37332974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 37342974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 373517ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 373617ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 373717ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 373817ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 373917ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 37401c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 37411c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 37429b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 37439b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3744a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 374575ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 374675ebec11SMaxime Coquelin true), 37479473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 37489473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 37499711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 375017ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 375117ec5a86SKONRAD Frederic }; 375217ec5a86SKONRAD Frederic 375317ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 375417ec5a86SKONRAD Frederic { 375517ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 375617ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3757e6f746b3SAndreas Färber 37584f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3759290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3760125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3761e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3762306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 376317ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 376417ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 376517ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 376617ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 376717ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 376817ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 376917ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 377017ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 377117ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 37722a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 37737788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 3774982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 37759711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 3776c255488dSJonah Palmer vdc->get_vhost = virtio_net_get_vhost; 377717ec5a86SKONRAD Frederic } 377817ec5a86SKONRAD Frederic 377917ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 378017ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 378117ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 378217ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 378317ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 378417ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 378517ec5a86SKONRAD Frederic }; 378617ec5a86SKONRAD Frederic 378717ec5a86SKONRAD Frederic static void virtio_register_types(void) 378817ec5a86SKONRAD Frederic { 378917ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 379017ec5a86SKONRAD Frederic } 379117ec5a86SKONRAD Frederic 379217ec5a86SKONRAD Frederic type_init(virtio_register_types) 3793