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" 456b230b7dSAndrew Melnychenko #include "monitor/monitor.h" 46edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h" 474474e37aSYuri Benditovich #include "net_rx_pkt.h" 48108a6481SCindy Lu #include "hw/virtio/vhost.h" 491b529d90SLaurent Vivier #include "sysemu/qtest.h" 506e790746SPaolo Bonzini 516e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 526e790746SPaolo Bonzini 531c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 541c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 559b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 569b02e161SWei Wang 57441537f1SJason Wang /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */ 581c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 599b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 601c0fbfa3SMichael S. Tsirkin 612974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ 622974e916SYuri Benditovich 632974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG 0x3F 642974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 652974e916SYuri Benditovich 662974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */ 672974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header)) 682974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535 692974e916SYuri Benditovich 702974e916SYuri Benditovich /* header length value in ip header without option */ 712974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5 722974e916SYuri Benditovich 732974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */ 742974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD 752974e916SYuri Benditovich 762974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance 772974e916SYuri Benditovich a lot, and should be tuned carefully, '300000'(300us) is the recommended 782974e916SYuri Benditovich value to pass the WHQL test, '50000' can gain 2x netperf throughput with 792974e916SYuri Benditovich tso/gso/gro 'off'. */ 802974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000 812974e916SYuri Benditovich 8259079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ 8359079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ 8459079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ 8559079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ 8659079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ 8759079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \ 8859079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \ 8959079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \ 9059079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) 9159079029SYuri Benditovich 92ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = { 93127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 945d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mac)}, 95127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 965d5b33c0SMax Reitz .end = endof(struct virtio_net_config, status)}, 97127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 985d5b33c0SMax Reitz .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 99127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 1005d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mtu)}, 1019473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 1025d5b33c0SMax Reitz .end = endof(struct virtio_net_config, duplex)}, 103e22f0603SYuri Benditovich {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT), 10459079029SYuri Benditovich .end = endof(struct virtio_net_config, supported_hash_types)}, 1056e790746SPaolo Bonzini {} 1066e790746SPaolo Bonzini }; 1076e790746SPaolo Bonzini 108d74c30c8SDaniil Tatianin static const VirtIOConfigSizeParams cfg_size_params = { 109d74c30c8SDaniil Tatianin .min_size = endof(struct virtio_net_config, mac), 110d74c30c8SDaniil Tatianin .max_size = sizeof(struct virtio_net_config), 111d74c30c8SDaniil Tatianin .feature_sizes = feature_sizes 112d74c30c8SDaniil Tatianin }; 113d74c30c8SDaniil Tatianin 1146e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 1156e790746SPaolo Bonzini { 1166e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1176e790746SPaolo Bonzini 1186e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 1196e790746SPaolo Bonzini } 1206e790746SPaolo Bonzini 1216e790746SPaolo Bonzini static int vq2q(int queue_index) 1226e790746SPaolo Bonzini { 1236e790746SPaolo Bonzini return queue_index / 2; 1246e790746SPaolo Bonzini } 1256e790746SPaolo Bonzini 1264fdf69abSKangjie Xu static void flush_or_purge_queued_packets(NetClientState *nc) 1274fdf69abSKangjie Xu { 1284fdf69abSKangjie Xu if (!nc->peer) { 1294fdf69abSKangjie Xu return; 1304fdf69abSKangjie Xu } 1314fdf69abSKangjie Xu 1324fdf69abSKangjie Xu qemu_flush_or_purge_queued_packets(nc->peer, true); 1334fdf69abSKangjie Xu assert(!virtio_net_get_subqueue(nc)->async_tx.elem); 1344fdf69abSKangjie Xu } 1354fdf69abSKangjie Xu 1366e790746SPaolo Bonzini /* TODO 1376e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 1386e790746SPaolo Bonzini */ 1396e790746SPaolo Bonzini 1406e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 1416e790746SPaolo Bonzini { 14217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1436e790746SPaolo Bonzini struct virtio_net_config netcfg; 144c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 145fb592882SCindy Lu static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } }; 1466e790746SPaolo Bonzini 147108a6481SCindy Lu int ret = 0; 148108a6481SCindy Lu memset(&netcfg, 0 , sizeof(struct virtio_net_config)); 1491399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 150441537f1SJason Wang virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs); 151a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 1526e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 1539473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 1549473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 15559079029SYuri Benditovich netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE; 15659079029SYuri Benditovich virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length, 157e22f0603SYuri Benditovich virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ? 158e22f0603SYuri Benditovich VIRTIO_NET_RSS_MAX_TABLE_LEN : 1); 15959079029SYuri Benditovich virtio_stl_p(vdev, &netcfg.supported_hash_types, 16059079029SYuri Benditovich VIRTIO_NET_RSS_SUPPORTED_HASHES); 1616e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 162108a6481SCindy Lu 163c546ecf2SJason Wang /* 164c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 165c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 166c546ecf2SJason Wang */ 167c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 168108a6481SCindy Lu ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, 169108a6481SCindy Lu n->config_size); 170ebc141a6SEugenio Pérez if (ret == -1) { 171ebc141a6SEugenio Pérez return; 172ebc141a6SEugenio Pérez } 173ebc141a6SEugenio Pérez 174fb592882SCindy Lu /* 175ebc141a6SEugenio Pérez * Some NIC/kernel combinations present 0 as the mac address. As that 176ebc141a6SEugenio Pérez * is not a legal address, try to proceed with the address from the 177ebc141a6SEugenio Pérez * QEMU command line in the hope that the address has been configured 178ebc141a6SEugenio Pérez * correctly elsewhere - just not reported by the device. 179fb592882SCindy Lu */ 180fb592882SCindy Lu if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) { 181fb592882SCindy Lu info_report("Zero hardware mac address detected. Ignoring."); 182fb592882SCindy Lu memcpy(netcfg.mac, n->mac, ETH_ALEN); 183fb592882SCindy Lu } 184ebc141a6SEugenio Pérez 1854f93aafcSEugenio Pérez netcfg.status |= virtio_tswap16(vdev, 1864f93aafcSEugenio Pérez n->status & VIRTIO_NET_S_ANNOUNCE); 187108a6481SCindy Lu memcpy(config, &netcfg, n->config_size); 188108a6481SCindy Lu } 189108a6481SCindy Lu } 1906e790746SPaolo Bonzini 1916e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1926e790746SPaolo Bonzini { 19317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1946e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 195c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1966e790746SPaolo Bonzini 1976e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1986e790746SPaolo Bonzini 19995129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 20095129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 2016e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 2026e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 2036e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 2046e790746SPaolo Bonzini } 205108a6481SCindy Lu 206c546ecf2SJason Wang /* 207c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 208c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 209c546ecf2SJason Wang */ 210c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 211c546ecf2SJason Wang vhost_net_set_config(get_vhost_net(nc->peer), 212c546ecf2SJason Wang (uint8_t *)&netcfg, 0, n->config_size, 213f8ed3648SManos Pitsidianakis VHOST_SET_CONFIG_TYPE_FRONTEND); 214108a6481SCindy Lu } 2156e790746SPaolo Bonzini } 2166e790746SPaolo Bonzini 2176e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 2186e790746SPaolo Bonzini { 21917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2206e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 22117a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 2226e790746SPaolo Bonzini } 2236e790746SPaolo Bonzini 224b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net) 225b2c929f0SDr. David Alan Gilbert { 226b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(net); 227b2c929f0SDr. David Alan Gilbert trace_virtio_net_announce_notify(); 228b2c929f0SDr. David Alan Gilbert 229b2c929f0SDr. David Alan Gilbert net->status |= VIRTIO_NET_S_ANNOUNCE; 230b2c929f0SDr. David Alan Gilbert virtio_notify_config(vdev); 231b2c929f0SDr. David Alan Gilbert } 232b2c929f0SDr. David Alan Gilbert 233f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 234f57fcf70SJason Wang { 235f57fcf70SJason Wang VirtIONet *n = opaque; 2369d8c6a25SDr. David Alan Gilbert trace_virtio_net_announce_timer(n->announce_timer.round); 237f57fcf70SJason Wang 2389d8c6a25SDr. David Alan Gilbert n->announce_timer.round--; 239b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 240b2c929f0SDr. David Alan Gilbert } 241b2c929f0SDr. David Alan Gilbert 242b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc) 243b2c929f0SDr. David Alan Gilbert { 244b2c929f0SDr. David Alan Gilbert VirtIONet *n = qemu_get_nic_opaque(nc); 245b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 246b2c929f0SDr. David Alan Gilbert 247b2c929f0SDr. David Alan Gilbert /* 248b2c929f0SDr. David Alan Gilbert * Make sure the virtio migration announcement timer isn't running 249b2c929f0SDr. David Alan Gilbert * If it is, let it trigger announcement so that we do not cause 250b2c929f0SDr. David Alan Gilbert * confusion. 251b2c929f0SDr. David Alan Gilbert */ 252b2c929f0SDr. David Alan Gilbert if (n->announce_timer.round) { 253b2c929f0SDr. David Alan Gilbert return; 254b2c929f0SDr. David Alan Gilbert } 255b2c929f0SDr. David Alan Gilbert 256b2c929f0SDr. David Alan Gilbert if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 257b2c929f0SDr. David Alan Gilbert virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 258b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 259b2c929f0SDr. David Alan Gilbert } 260f57fcf70SJason Wang } 261f57fcf70SJason Wang 2626e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 2636e790746SPaolo Bonzini { 26417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2656e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 266441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 267aa858194SSi-Wei Liu int cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ? 268aa858194SSi-Wei Liu n->max_ncs - n->max_queue_pairs : 0; 2696e790746SPaolo Bonzini 270ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 2716e790746SPaolo Bonzini return; 2726e790746SPaolo Bonzini } 2736e790746SPaolo Bonzini 2748c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 2758c1ac475SRadim Krčmář !!n->vhost_started) { 2766e790746SPaolo Bonzini return; 2776e790746SPaolo Bonzini } 2786e790746SPaolo Bonzini if (!n->vhost_started) { 279086abc1cSMichael S. Tsirkin int r, i; 280086abc1cSMichael S. Tsirkin 2811bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2821bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 2831bfa316cSGreg Kurz "falling back on userspace virtio", 2841bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 2851bfa316cSGreg Kurz return; 2861bfa316cSGreg Kurz } 2871bfa316cSGreg Kurz 288086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 289086abc1cSMichael S. Tsirkin * when vhost is running. 290086abc1cSMichael S. Tsirkin */ 291441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 292086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 293086abc1cSMichael S. Tsirkin 294086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 295086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 296086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 297086abc1cSMichael S. Tsirkin } 298086abc1cSMichael S. Tsirkin 299a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 300a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 301a93e599dSMaxime Coquelin if (r < 0) { 302a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 303a93e599dSMaxime Coquelin n->net_conf.mtu); 304a93e599dSMaxime Coquelin 305a93e599dSMaxime Coquelin return; 306a93e599dSMaxime Coquelin } 307a93e599dSMaxime Coquelin } 308a93e599dSMaxime Coquelin 3096e790746SPaolo Bonzini n->vhost_started = 1; 31022288fe5SJason Wang r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq); 3116e790746SPaolo Bonzini if (r < 0) { 3126e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 3136e790746SPaolo Bonzini "falling back on userspace virtio", -r); 3146e790746SPaolo Bonzini n->vhost_started = 0; 3156e790746SPaolo Bonzini } 3166e790746SPaolo Bonzini } else { 31722288fe5SJason Wang vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq); 3186e790746SPaolo Bonzini n->vhost_started = 0; 3196e790746SPaolo Bonzini } 3206e790746SPaolo Bonzini } 3216e790746SPaolo Bonzini 3221bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 3231bfa316cSGreg Kurz NetClientState *peer, 3241bfa316cSGreg Kurz bool enable) 3251bfa316cSGreg Kurz { 3261bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 3271bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 3281bfa316cSGreg Kurz } else { 3291bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 3301bfa316cSGreg Kurz } 3311bfa316cSGreg Kurz } 3321bfa316cSGreg Kurz 3331bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 334441537f1SJason Wang int queue_pairs, bool enable) 3351bfa316cSGreg Kurz { 3361bfa316cSGreg Kurz int i; 3371bfa316cSGreg Kurz 338441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 3391bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 3401bfa316cSGreg Kurz enable) { 3411bfa316cSGreg Kurz while (--i >= 0) { 3421bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 3431bfa316cSGreg Kurz } 3441bfa316cSGreg Kurz 3451bfa316cSGreg Kurz return true; 3461bfa316cSGreg Kurz } 3471bfa316cSGreg Kurz } 3481bfa316cSGreg Kurz 3491bfa316cSGreg Kurz return false; 3501bfa316cSGreg Kurz } 3511bfa316cSGreg Kurz 3521bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 3531bfa316cSGreg Kurz { 3541bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 355441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3561bfa316cSGreg Kurz 3571bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 3581bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 3591bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 3601bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 3611bfa316cSGreg Kurz * virtio-net code. 3621bfa316cSGreg Kurz */ 363ad57f700SAkihiko Odaki n->needs_vnet_hdr_swap = n->has_vnet_hdr && 364ad57f700SAkihiko Odaki virtio_net_set_vnet_endian(vdev, n->nic->ncs, 365441537f1SJason Wang queue_pairs, true); 3661bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3671bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3681bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3691bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3701bfa316cSGreg Kurz * endianness. 3711bfa316cSGreg Kurz */ 372441537f1SJason Wang virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false); 3731bfa316cSGreg Kurz } 3741bfa316cSGreg Kurz } 3751bfa316cSGreg Kurz 376283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 377283e2c2aSYuri Benditovich { 378283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 379283e2c2aSYuri Benditovich if (dropped) { 380283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 381283e2c2aSYuri Benditovich } 382283e2c2aSYuri Benditovich } 383283e2c2aSYuri Benditovich 3846e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3856e790746SPaolo Bonzini { 38617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3876e790746SPaolo Bonzini VirtIONetQueue *q; 3886e790746SPaolo Bonzini int i; 3896e790746SPaolo Bonzini uint8_t queue_status; 3906e790746SPaolo Bonzini 3911bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3926e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3936e790746SPaolo Bonzini 394441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 39538705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 39638705bb5SFam Zheng bool queue_started; 3976e790746SPaolo Bonzini q = &n->vqs[i]; 3986e790746SPaolo Bonzini 399441537f1SJason Wang if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) { 4006e790746SPaolo Bonzini queue_status = 0; 4016e790746SPaolo Bonzini } else { 4026e790746SPaolo Bonzini queue_status = status; 4036e790746SPaolo Bonzini } 40438705bb5SFam Zheng queue_started = 40538705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 40638705bb5SFam Zheng 40738705bb5SFam Zheng if (queue_started) { 40838705bb5SFam Zheng qemu_flush_queued_packets(ncs); 40938705bb5SFam Zheng } 4106e790746SPaolo Bonzini 4116e790746SPaolo Bonzini if (!q->tx_waiting) { 4126e790746SPaolo Bonzini continue; 4136e790746SPaolo Bonzini } 4146e790746SPaolo Bonzini 41538705bb5SFam Zheng if (queue_started) { 4166e790746SPaolo Bonzini if (q->tx_timer) { 417bc72ad67SAlex Bligh timer_mod(q->tx_timer, 418bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 4196e790746SPaolo Bonzini } else { 4206e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 4216e790746SPaolo Bonzini } 4226e790746SPaolo Bonzini } else { 4236e790746SPaolo Bonzini if (q->tx_timer) { 424bc72ad67SAlex Bligh timer_del(q->tx_timer); 4256e790746SPaolo Bonzini } else { 4266e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 4276e790746SPaolo Bonzini } 428283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 42970e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 43070e53e6eSJason Wang vdev->vm_running) { 431283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 432283e2c2aSYuri Benditovich * and disabled notification */ 433283e2c2aSYuri Benditovich q->tx_waiting = 0; 434283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 435283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 436283e2c2aSYuri Benditovich } 4376e790746SPaolo Bonzini } 4386e790746SPaolo Bonzini } 4396e790746SPaolo Bonzini } 4406e790746SPaolo Bonzini 4416e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4426e790746SPaolo Bonzini { 4436e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 44417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4456e790746SPaolo Bonzini uint16_t old_status = n->status; 4466e790746SPaolo Bonzini 4476e790746SPaolo Bonzini if (nc->link_down) 4486e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4496e790746SPaolo Bonzini else 4506e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4516e790746SPaolo Bonzini 4526e790746SPaolo Bonzini if (n->status != old_status) 45317a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4546e790746SPaolo Bonzini 45517a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4566e790746SPaolo Bonzini } 4576e790746SPaolo Bonzini 458b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 459b1be4280SAmos Kong { 460b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 461b1be4280SAmos Kong 462b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 463ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 4647480874aSMarkus Armbruster qapi_event_send_nic_rx_filter_changed(n->netclient_name, path); 46596e35046SAmos Kong g_free(path); 466b1be4280SAmos Kong 467b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 468b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 469b1be4280SAmos Kong } 470b1be4280SAmos Kong } 471b1be4280SAmos Kong 472f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 473f7bc8ef8SAmos Kong { 47454aa3de7SEric Blake intList *list; 475f7bc8ef8SAmos Kong int i, j; 476f7bc8ef8SAmos Kong 477f7bc8ef8SAmos Kong list = NULL; 478f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 479f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 480f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 48154aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 482f7bc8ef8SAmos Kong } 483f7bc8ef8SAmos Kong } 484f7bc8ef8SAmos Kong } 485f7bc8ef8SAmos Kong 486f7bc8ef8SAmos Kong return list; 487f7bc8ef8SAmos Kong } 488f7bc8ef8SAmos Kong 489b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 490b1be4280SAmos Kong { 491b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 492f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 493b1be4280SAmos Kong RxFilterInfo *info; 49454aa3de7SEric Blake strList *str_list; 495f7bc8ef8SAmos Kong int i; 496b1be4280SAmos Kong 497b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 498b1be4280SAmos Kong info->name = g_strdup(nc->name); 499b1be4280SAmos Kong info->promiscuous = n->promisc; 500b1be4280SAmos Kong 501b1be4280SAmos Kong if (n->nouni) { 502b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 503b1be4280SAmos Kong } else if (n->alluni) { 504b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 505b1be4280SAmos Kong } else { 506b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 507b1be4280SAmos Kong } 508b1be4280SAmos Kong 509b1be4280SAmos Kong if (n->nomulti) { 510b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 511b1be4280SAmos Kong } else if (n->allmulti) { 512b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 513b1be4280SAmos Kong } else { 514b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 515b1be4280SAmos Kong } 516b1be4280SAmos Kong 517b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 518b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 519b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 520b1be4280SAmos Kong 521b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 522b1be4280SAmos Kong 523b1be4280SAmos Kong str_list = NULL; 524b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 52554aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 52654aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 527b1be4280SAmos Kong } 528b1be4280SAmos Kong info->unicast_table = str_list; 529b1be4280SAmos Kong 530b1be4280SAmos Kong str_list = NULL; 531b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 53254aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 53354aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 534b1be4280SAmos Kong } 535b1be4280SAmos Kong info->multicast_table = str_list; 536f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 537b1be4280SAmos Kong 53895129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 539f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 540f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 541f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 542f7bc8ef8SAmos Kong } else { 543f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 544b1be4280SAmos Kong } 545b1be4280SAmos Kong 546b1be4280SAmos Kong /* enable event notification after query */ 547b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 548b1be4280SAmos Kong 549b1be4280SAmos Kong return info; 550b1be4280SAmos Kong } 551b1be4280SAmos Kong 5527dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index) 5537dc6be52SXuan Zhuo { 5547dc6be52SXuan Zhuo VirtIONet *n = VIRTIO_NET(vdev); 555f47af0afSXuan Zhuo NetClientState *nc; 556f47af0afSXuan Zhuo 557f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 558f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 559f47af0afSXuan Zhuo return; 560f47af0afSXuan Zhuo } 561f47af0afSXuan Zhuo 562f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 5637dc6be52SXuan Zhuo 5647dc6be52SXuan Zhuo if (!nc->peer) { 5657dc6be52SXuan Zhuo return; 5667dc6be52SXuan Zhuo } 5677dc6be52SXuan Zhuo 5687dc6be52SXuan Zhuo if (get_vhost_net(nc->peer) && 5697dc6be52SXuan Zhuo nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5707dc6be52SXuan Zhuo vhost_net_virtqueue_reset(vdev, nc, queue_index); 5717dc6be52SXuan Zhuo } 5727dc6be52SXuan Zhuo 5737dc6be52SXuan Zhuo flush_or_purge_queued_packets(nc); 5747dc6be52SXuan Zhuo } 5757dc6be52SXuan Zhuo 5767f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index) 5777f863302SKangjie Xu { 5787f863302SKangjie Xu VirtIONet *n = VIRTIO_NET(vdev); 579f47af0afSXuan Zhuo NetClientState *nc; 5807f863302SKangjie Xu int r; 5817f863302SKangjie Xu 582f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 583f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 584f47af0afSXuan Zhuo return; 585f47af0afSXuan Zhuo } 586f47af0afSXuan Zhuo 587f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 588f47af0afSXuan Zhuo 5897f863302SKangjie Xu if (!nc->peer || !vdev->vhost_started) { 5907f863302SKangjie Xu return; 5917f863302SKangjie Xu } 5927f863302SKangjie Xu 5937f863302SKangjie Xu if (get_vhost_net(nc->peer) && 5947f863302SKangjie Xu nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5957f863302SKangjie Xu r = vhost_net_virtqueue_restart(vdev, nc, queue_index); 5967f863302SKangjie Xu if (r < 0) { 5977f863302SKangjie Xu error_report("unable to restart vhost net virtqueue: %d, " 5987f863302SKangjie Xu "when resetting the queue", queue_index); 5997f863302SKangjie Xu } 6007f863302SKangjie Xu } 6017f863302SKangjie Xu } 6027f863302SKangjie Xu 6036e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 6046e790746SPaolo Bonzini { 6056e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 6066e790746SPaolo Bonzini if (!nc->peer) { 6076e790746SPaolo Bonzini return; 6086e790746SPaolo Bonzini } 6096e790746SPaolo Bonzini 610d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 6116e790746SPaolo Bonzini } 6126e790746SPaolo Bonzini 6136e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 6146e790746SPaolo Bonzini { 6156e790746SPaolo Bonzini return n->has_vnet_hdr; 6166e790746SPaolo Bonzini } 6176e790746SPaolo Bonzini 6186e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 6196e790746SPaolo Bonzini { 6206e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 6216e790746SPaolo Bonzini return 0; 6226e790746SPaolo Bonzini 623d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 6246e790746SPaolo Bonzini 6256e790746SPaolo Bonzini return n->has_ufo; 6266e790746SPaolo Bonzini } 6276e790746SPaolo Bonzini 62853da8b5aSYuri Benditovich static int peer_has_uso(VirtIONet *n) 62953da8b5aSYuri Benditovich { 63053da8b5aSYuri Benditovich if (!peer_has_vnet_hdr(n)) { 63153da8b5aSYuri Benditovich return 0; 63253da8b5aSYuri Benditovich } 63353da8b5aSYuri Benditovich 63453da8b5aSYuri Benditovich return qemu_has_uso(qemu_get_queue(n->nic)->peer); 63553da8b5aSYuri Benditovich } 63653da8b5aSYuri Benditovich 637bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 638e22f0603SYuri Benditovich int version_1, int hash_report) 6396e790746SPaolo Bonzini { 6406e790746SPaolo Bonzini int i; 6416e790746SPaolo Bonzini NetClientState *nc; 6426e790746SPaolo Bonzini 6436e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6446e790746SPaolo Bonzini 645bb9d17f8SCornelia Huck if (version_1) { 646e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 647e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 648e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 649e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 650bb9d17f8SCornelia Huck } else { 6516e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 652bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 653bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 65413d40aa8SAkihiko Odaki n->rss_data.populate_hash = false; 655bb9d17f8SCornelia Huck } 6566e790746SPaolo Bonzini 657441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6586e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6596e790746SPaolo Bonzini 6606e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 661d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 662d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6636e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6646e790746SPaolo Bonzini } 6656e790746SPaolo Bonzini } 6666e790746SPaolo Bonzini } 6676e790746SPaolo Bonzini 6682eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6692eef278bSMichael S. Tsirkin { 6702eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6712eef278bSMichael S. Tsirkin 6722eef278bSMichael S. Tsirkin /* 6730ea5778fSEugenio Pérez * Backends other than vhost-user or vhost-vdpa don't support max queue 6740ea5778fSEugenio Pérez * size. 6752eef278bSMichael S. Tsirkin */ 6762eef278bSMichael S. Tsirkin if (!peer) { 6772eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6782eef278bSMichael S. Tsirkin } 6792eef278bSMichael S. Tsirkin 6800ea5778fSEugenio Pérez switch(peer->info->type) { 6810ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_USER: 6820ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_VDPA: 6832eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 6840ea5778fSEugenio Pérez default: 6850ea5778fSEugenio Pérez return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6860ea5778fSEugenio Pérez }; 6872eef278bSMichael S. Tsirkin } 6882eef278bSMichael S. Tsirkin 6896e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 6906e790746SPaolo Bonzini { 6916e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6926e790746SPaolo Bonzini 6936e790746SPaolo Bonzini if (!nc->peer) { 6946e790746SPaolo Bonzini return 0; 6956e790746SPaolo Bonzini } 6966e790746SPaolo Bonzini 697f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 6987263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 6997263a0adSChangchun Ouyang } 7007263a0adSChangchun Ouyang 701f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7026e790746SPaolo Bonzini return 0; 7036e790746SPaolo Bonzini } 7046e790746SPaolo Bonzini 705441537f1SJason Wang if (n->max_queue_pairs == 1) { 7061074b879SJason Wang return 0; 7071074b879SJason Wang } 7081074b879SJason Wang 7096e790746SPaolo Bonzini return tap_enable(nc->peer); 7106e790746SPaolo Bonzini } 7116e790746SPaolo Bonzini 7126e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 7136e790746SPaolo Bonzini { 7146e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7156e790746SPaolo Bonzini 7166e790746SPaolo Bonzini if (!nc->peer) { 7176e790746SPaolo Bonzini return 0; 7186e790746SPaolo Bonzini } 7196e790746SPaolo Bonzini 720f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7217263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 7227263a0adSChangchun Ouyang } 7237263a0adSChangchun Ouyang 724f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7256e790746SPaolo Bonzini return 0; 7266e790746SPaolo Bonzini } 7276e790746SPaolo Bonzini 7286e790746SPaolo Bonzini return tap_disable(nc->peer); 7296e790746SPaolo Bonzini } 7306e790746SPaolo Bonzini 731441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 7326e790746SPaolo Bonzini { 7336e790746SPaolo Bonzini int i; 734ddfa83eaSJoel Stanley int r; 7356e790746SPaolo Bonzini 73668b5f314SYuri Benditovich if (n->nic->peer_deleted) { 73768b5f314SYuri Benditovich return; 73868b5f314SYuri Benditovich } 73968b5f314SYuri Benditovich 740441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 741441537f1SJason Wang if (i < n->curr_queue_pairs) { 742ddfa83eaSJoel Stanley r = peer_attach(n, i); 743ddfa83eaSJoel Stanley assert(!r); 7446e790746SPaolo Bonzini } else { 745ddfa83eaSJoel Stanley r = peer_detach(n, i); 746ddfa83eaSJoel Stanley assert(!r); 7476e790746SPaolo Bonzini } 7486e790746SPaolo Bonzini } 7496e790746SPaolo Bonzini } 7506e790746SPaolo Bonzini 751ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7526e790746SPaolo Bonzini 7539d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7549d5b731dSJason Wang Error **errp) 7556e790746SPaolo Bonzini { 75617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7576e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7586e790746SPaolo Bonzini 759da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 760da3e8a23SShannon Zhao features |= n->host_features; 761da3e8a23SShannon Zhao 7620cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7636e790746SPaolo Bonzini 7646e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7650cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7660cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7670cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7680cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7696e790746SPaolo Bonzini 7700cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7710cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7720cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7730cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 774e22f0603SYuri Benditovich 77553da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO); 77653da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4); 77753da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6); 77853da8b5aSYuri Benditovich 779e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7806e790746SPaolo Bonzini } 7816e790746SPaolo Bonzini 7826e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7830cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 7840cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 7856e790746SPaolo Bonzini } 7866e790746SPaolo Bonzini 78753da8b5aSYuri Benditovich if (!peer_has_uso(n)) { 78853da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO); 78953da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4); 79053da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6); 79153da8b5aSYuri Benditovich } 79253da8b5aSYuri Benditovich 793ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 7946e790746SPaolo Bonzini return features; 7956e790746SPaolo Bonzini } 7962974e916SYuri Benditovich 7970145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 79859079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 7990145c393SAndrew Melnychenko } 80075ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 80175ebec11SMaxime Coquelin vdev->backend_features = features; 80275ebec11SMaxime Coquelin 80375ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 80475ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 80575ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 80675ebec11SMaxime Coquelin } 80775ebec11SMaxime Coquelin 808cd69d47cSEugenio Pérez /* 809cd69d47cSEugenio Pérez * Since GUEST_ANNOUNCE is emulated the feature bit could be set without 810cd69d47cSEugenio Pérez * enabled. This happens in the vDPA case. 811cd69d47cSEugenio Pérez * 812cd69d47cSEugenio Pérez * Make sure the feature set is not incoherent, as the driver could refuse 813cd69d47cSEugenio Pérez * to start. 814cd69d47cSEugenio Pérez * 815cd69d47cSEugenio Pérez * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes, 816cd69d47cSEugenio Pérez * helping guest to notify the new location with vDPA devices that does not 817cd69d47cSEugenio Pérez * support it. 818cd69d47cSEugenio Pérez */ 819cd69d47cSEugenio Pérez if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) { 820cd69d47cSEugenio Pérez virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE); 821cd69d47cSEugenio Pérez } 822cd69d47cSEugenio Pérez 82375ebec11SMaxime Coquelin return features; 8246e790746SPaolo Bonzini } 8256e790746SPaolo Bonzini 826019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 8276e790746SPaolo Bonzini { 828019a3edbSGerd Hoffmann uint64_t features = 0; 8296e790746SPaolo Bonzini 8306e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 8316e790746SPaolo Bonzini * but also these: */ 8320cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 8330cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 8340cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 8350cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 8360cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 8376e790746SPaolo Bonzini 8386e790746SPaolo Bonzini return features; 8396e790746SPaolo Bonzini } 8406e790746SPaolo Bonzini 841644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 842644c9858SDmitry Fleytman { 843ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 844644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 845644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 846644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 847644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 8482ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)), 8492ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)), 8502ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6))); 851644c9858SDmitry Fleytman } 852644c9858SDmitry Fleytman 85353da8b5aSYuri Benditovich static uint64_t virtio_net_guest_offloads_by_features(uint64_t features) 854644c9858SDmitry Fleytman { 855644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 856644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 857644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 858644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 859644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 86053da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_UFO) | 86153da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_USO4) | 86253da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_USO6); 863644c9858SDmitry Fleytman 864644c9858SDmitry Fleytman return guest_offloads_mask & features; 865644c9858SDmitry Fleytman } 866644c9858SDmitry Fleytman 8670b545b1eSHawkins Jiawei uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n) 868644c9858SDmitry Fleytman { 869644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 870644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 871644c9858SDmitry Fleytman } 872644c9858SDmitry Fleytman 873f5e1847bSJuan Quintela typedef struct { 874f5e1847bSJuan Quintela VirtIONet *n; 87512b2fad7SKevin Wolf DeviceState *dev; 87612b2fad7SKevin Wolf } FailoverDevice; 877f5e1847bSJuan Quintela 878f5e1847bSJuan Quintela /** 87912b2fad7SKevin Wolf * Set the failover primary device 880f5e1847bSJuan Quintela * 881f5e1847bSJuan Quintela * @opaque: FailoverId to setup 882f5e1847bSJuan Quintela * @opts: opts for device we are handling 883f5e1847bSJuan Quintela * @errp: returns an error if this function fails 884f5e1847bSJuan Quintela */ 88512b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 886f5e1847bSJuan Quintela { 88712b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 88812b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 88912b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 890f5e1847bSJuan Quintela 89112b2fad7SKevin Wolf if (!pci_dev) { 89212b2fad7SKevin Wolf return 0; 89312b2fad7SKevin Wolf } 89412b2fad7SKevin Wolf 89512b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 89612b2fad7SKevin Wolf fdev->dev = dev; 897f5e1847bSJuan Quintela return 1; 898f5e1847bSJuan Quintela } 899f5e1847bSJuan Quintela 900f5e1847bSJuan Quintela return 0; 901f5e1847bSJuan Quintela } 902f5e1847bSJuan Quintela 903f5e1847bSJuan Quintela /** 90485d3b931SJuan Quintela * Find the primary device for this failover virtio-net 90585d3b931SJuan Quintela * 90685d3b931SJuan Quintela * @n: VirtIONet device 90785d3b931SJuan Quintela * @errp: returns an error if this function fails 90885d3b931SJuan Quintela */ 9090a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 9109711cd0dSJens Freimann { 91112b2fad7SKevin Wolf FailoverDevice fdev = { 91212b2fad7SKevin Wolf .n = n, 91312b2fad7SKevin Wolf }; 9149711cd0dSJens Freimann 91512b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 91612b2fad7SKevin Wolf NULL, NULL, &fdev); 91712b2fad7SKevin Wolf return fdev.dev; 9189711cd0dSJens Freimann } 9199711cd0dSJens Freimann 92021e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 92121e8709bSJuan Quintela { 92221e8709bSJuan Quintela Error *err = NULL; 92321e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 92421e8709bSJuan Quintela 92521e8709bSJuan Quintela if (dev) { 92621e8709bSJuan Quintela return; 92721e8709bSJuan Quintela } 92821e8709bSJuan Quintela 929259a10dbSKevin Wolf if (!n->primary_opts) { 93097ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 93197ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 93297ca9c59SLaurent Vivier "sure primary device has parameter" 93397ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 93421e8709bSJuan Quintela return; 93521e8709bSJuan Quintela } 936259a10dbSKevin Wolf 937f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 938f3558b1bSKevin Wolf n->primary_opts_from_json, 939f3558b1bSKevin Wolf &err); 94021e8709bSJuan Quintela if (err) { 941f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 942259a10dbSKevin Wolf n->primary_opts = NULL; 94300e7b129SLaurent Vivier } else { 94400e7b129SLaurent Vivier object_unref(OBJECT(dev)); 94521e8709bSJuan Quintela } 94621e8709bSJuan Quintela error_propagate(errp, err); 94721e8709bSJuan Quintela } 94821e8709bSJuan Quintela 949d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 9506e790746SPaolo Bonzini { 95117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9529711cd0dSJens Freimann Error *err = NULL; 9536e790746SPaolo Bonzini int i; 9546e790746SPaolo Bonzini 95575ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 95675ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 95775ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 95875ebec11SMaxime Coquelin } 95975ebec11SMaxime Coquelin 960ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 96159079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 96295129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 9636e790746SPaolo Bonzini 964ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 96595129d6fSCornelia Huck virtio_has_feature(features, 966bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 96795129d6fSCornelia Huck virtio_has_feature(features, 968e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 969e22f0603SYuri Benditovich virtio_has_feature(features, 970e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 9716e790746SPaolo Bonzini 9722974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9732974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 9742974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9752974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 976e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 9772974e916SYuri Benditovich 9786e790746SPaolo Bonzini if (n->has_vnet_hdr) { 979644c9858SDmitry Fleytman n->curr_guest_offloads = 980644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 981644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 9826e790746SPaolo Bonzini } 9836e790746SPaolo Bonzini 984441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 9856e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9866e790746SPaolo Bonzini 987ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9886e790746SPaolo Bonzini continue; 9896e790746SPaolo Bonzini } 990ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 991c9bdc449SHyman Huang(黄勇) 992c9bdc449SHyman Huang(黄勇) /* 993c9bdc449SHyman Huang(黄勇) * keep acked_features in NetVhostUserState up-to-date so it 994c9bdc449SHyman Huang(黄勇) * can't miss any features configured by guest virtio driver. 995c9bdc449SHyman Huang(黄勇) */ 996c9bdc449SHyman Huang(黄勇) vhost_net_save_acked_features(nc->peer); 9976e790746SPaolo Bonzini } 9980b1eaa88SStefan Fritsch 99906b636a1SHawkins Jiawei if (!virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 10000b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 10010b1eaa88SStefan Fritsch } 10029711cd0dSJens Freimann 10039711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 10049711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 1005e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 10069711cd0dSJens Freimann failover_add_primary(n, &err); 10079711cd0dSJens Freimann if (err) { 10081b529d90SLaurent Vivier if (!qtest_enabled()) { 10099711cd0dSJens Freimann warn_report_err(err); 10101b529d90SLaurent Vivier } else { 10111b529d90SLaurent Vivier error_free(err); 10121b529d90SLaurent Vivier } 10139711cd0dSJens Freimann } 10146e790746SPaolo Bonzini } 101521e8709bSJuan Quintela } 10166e790746SPaolo Bonzini 10176e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 10186e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10196e790746SPaolo Bonzini { 10206e790746SPaolo Bonzini uint8_t on; 10216e790746SPaolo Bonzini size_t s; 1022b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10236e790746SPaolo Bonzini 10246e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 10256e790746SPaolo Bonzini if (s != sizeof(on)) { 10266e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10276e790746SPaolo Bonzini } 10286e790746SPaolo Bonzini 10296e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 10306e790746SPaolo Bonzini n->promisc = on; 10316e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 10326e790746SPaolo Bonzini n->allmulti = on; 10336e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 10346e790746SPaolo Bonzini n->alluni = on; 10356e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 10366e790746SPaolo Bonzini n->nomulti = on; 10376e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 10386e790746SPaolo Bonzini n->nouni = on; 10396e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 10406e790746SPaolo Bonzini n->nobcast = on; 10416e790746SPaolo Bonzini } else { 10426e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10436e790746SPaolo Bonzini } 10446e790746SPaolo Bonzini 1045b1be4280SAmos Kong rxfilter_notify(nc); 1046b1be4280SAmos Kong 10476e790746SPaolo Bonzini return VIRTIO_NET_OK; 10486e790746SPaolo Bonzini } 10496e790746SPaolo Bonzini 1050644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 1051644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 1052644c9858SDmitry Fleytman { 1053644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 1054644c9858SDmitry Fleytman uint64_t offloads; 1055644c9858SDmitry Fleytman size_t s; 1056644c9858SDmitry Fleytman 105795129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 1058644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1059644c9858SDmitry Fleytman } 1060644c9858SDmitry Fleytman 1061644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 1062644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 1063644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1064644c9858SDmitry Fleytman } 1065644c9858SDmitry Fleytman 1066644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 1067644c9858SDmitry Fleytman uint64_t supported_offloads; 1068644c9858SDmitry Fleytman 1069189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 1070189ae6bbSJason Wang 1071644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 1072644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1073644c9858SDmitry Fleytman } 1074644c9858SDmitry Fleytman 10752974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10762974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 10772974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10782974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 10792974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 10802974e916SYuri Benditovich 1081644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1082644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1083644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1084644c9858SDmitry Fleytman } 1085644c9858SDmitry Fleytman 1086644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1087644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1088644c9858SDmitry Fleytman 1089644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1090644c9858SDmitry Fleytman } else { 1091644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1092644c9858SDmitry Fleytman } 1093644c9858SDmitry Fleytman } 1094644c9858SDmitry Fleytman 10956e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 10966e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10976e790746SPaolo Bonzini { 10981399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 10996e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 11006e790746SPaolo Bonzini size_t s; 1101b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11026e790746SPaolo Bonzini 11036e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 11046e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 11056e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11066e790746SPaolo Bonzini } 11076e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 11086e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 11096e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1110b1be4280SAmos Kong rxfilter_notify(nc); 1111b1be4280SAmos Kong 11126e790746SPaolo Bonzini return VIRTIO_NET_OK; 11136e790746SPaolo Bonzini } 11146e790746SPaolo Bonzini 11156e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 11166e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11176e790746SPaolo Bonzini } 11186e790746SPaolo Bonzini 1119cae2e556SAmos Kong int in_use = 0; 1120cae2e556SAmos Kong int first_multi = 0; 1121cae2e556SAmos Kong uint8_t uni_overflow = 0; 1122cae2e556SAmos Kong uint8_t multi_overflow = 0; 1123cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 11246e790746SPaolo Bonzini 11256e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11266e790746SPaolo Bonzini sizeof(mac_data.entries)); 11271399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11286e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1129b1be4280SAmos Kong goto error; 11306e790746SPaolo Bonzini } 11316e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11326e790746SPaolo Bonzini 11336e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1134b1be4280SAmos Kong goto error; 11356e790746SPaolo Bonzini } 11366e790746SPaolo Bonzini 11376e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1138cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 11396e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11406e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1141b1be4280SAmos Kong goto error; 11426e790746SPaolo Bonzini } 1143cae2e556SAmos Kong in_use += mac_data.entries; 11446e790746SPaolo Bonzini } else { 1145cae2e556SAmos Kong uni_overflow = 1; 11466e790746SPaolo Bonzini } 11476e790746SPaolo Bonzini 11486e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 11496e790746SPaolo Bonzini 1150cae2e556SAmos Kong first_multi = in_use; 11516e790746SPaolo Bonzini 11526e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11536e790746SPaolo Bonzini sizeof(mac_data.entries)); 11541399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11556e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1156b1be4280SAmos Kong goto error; 11576e790746SPaolo Bonzini } 11586e790746SPaolo Bonzini 11596e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11606e790746SPaolo Bonzini 11616e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1162b1be4280SAmos Kong goto error; 11636e790746SPaolo Bonzini } 11646e790746SPaolo Bonzini 1165edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1166cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 11676e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11686e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1169b1be4280SAmos Kong goto error; 11706e790746SPaolo Bonzini } 1171cae2e556SAmos Kong in_use += mac_data.entries; 11726e790746SPaolo Bonzini } else { 1173cae2e556SAmos Kong multi_overflow = 1; 11746e790746SPaolo Bonzini } 11756e790746SPaolo Bonzini 1176cae2e556SAmos Kong n->mac_table.in_use = in_use; 1177cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1178cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1179cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1180cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1181cae2e556SAmos Kong g_free(macs); 1182b1be4280SAmos Kong rxfilter_notify(nc); 1183b1be4280SAmos Kong 11846e790746SPaolo Bonzini return VIRTIO_NET_OK; 1185b1be4280SAmos Kong 1186b1be4280SAmos Kong error: 1187cae2e556SAmos Kong g_free(macs); 1188b1be4280SAmos Kong return VIRTIO_NET_ERR; 11896e790746SPaolo Bonzini } 11906e790746SPaolo Bonzini 11916e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 11926e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11936e790746SPaolo Bonzini { 11941399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11956e790746SPaolo Bonzini uint16_t vid; 11966e790746SPaolo Bonzini size_t s; 1197b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11986e790746SPaolo Bonzini 11996e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 12001399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 12016e790746SPaolo Bonzini if (s != sizeof(vid)) { 12026e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12036e790746SPaolo Bonzini } 12046e790746SPaolo Bonzini 12056e790746SPaolo Bonzini if (vid >= MAX_VLAN) 12066e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12076e790746SPaolo Bonzini 12086e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 12096e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 12106e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 12116e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 12126e790746SPaolo Bonzini else 12136e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12146e790746SPaolo Bonzini 1215b1be4280SAmos Kong rxfilter_notify(nc); 1216b1be4280SAmos Kong 12176e790746SPaolo Bonzini return VIRTIO_NET_OK; 12186e790746SPaolo Bonzini } 12196e790746SPaolo Bonzini 1220f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1221f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1222f57fcf70SJason Wang { 12239d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1224f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1225f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1226f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 12279d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 12289d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1229f57fcf70SJason Wang } 1230f57fcf70SJason Wang return VIRTIO_NET_OK; 1231f57fcf70SJason Wang } else { 1232f57fcf70SJason Wang return VIRTIO_NET_ERR; 1233f57fcf70SJason Wang } 1234f57fcf70SJason Wang } 1235f57fcf70SJason Wang 12360145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 12370145c393SAndrew Melnychenko { 12380145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 12390145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 12400145c393SAndrew Melnychenko return false; 12410145c393SAndrew Melnychenko } 12420145c393SAndrew Melnychenko 12430145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 12440145c393SAndrew Melnychenko } 12450145c393SAndrew Melnychenko 12460145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 12470145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 12480145c393SAndrew Melnychenko { 12490145c393SAndrew Melnychenko config->redirect = data->redirect; 12500145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 12510145c393SAndrew Melnychenko config->hash_types = data->hash_types; 12520145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 12530145c393SAndrew Melnychenko config->default_queue = data->default_queue; 12540145c393SAndrew Melnychenko } 12550145c393SAndrew Melnychenko 12560145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 12570145c393SAndrew Melnychenko { 12580145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 12590145c393SAndrew Melnychenko 12600145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 12610145c393SAndrew Melnychenko return false; 12620145c393SAndrew Melnychenko } 12630145c393SAndrew Melnychenko 12640145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 12650145c393SAndrew Melnychenko 12660145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 12670145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 12680145c393SAndrew Melnychenko return false; 12690145c393SAndrew Melnychenko } 12700145c393SAndrew Melnychenko 12710145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 12720145c393SAndrew Melnychenko return false; 12730145c393SAndrew Melnychenko } 12740145c393SAndrew Melnychenko 12750145c393SAndrew Melnychenko return true; 12760145c393SAndrew Melnychenko } 12770145c393SAndrew Melnychenko 12780145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 12790145c393SAndrew Melnychenko { 12800145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12810145c393SAndrew Melnychenko } 12820145c393SAndrew Melnychenko 12830e07198eSAkihiko Odaki static void virtio_net_commit_rss_config(VirtIONet *n) 12840e07198eSAkihiko Odaki { 12850e07198eSAkihiko Odaki if (n->rss_data.enabled) { 12860e07198eSAkihiko Odaki n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 12870e07198eSAkihiko Odaki if (n->rss_data.populate_hash) { 12880e07198eSAkihiko Odaki virtio_net_detach_epbf_rss(n); 12890e07198eSAkihiko Odaki } else if (!virtio_net_attach_epbf_rss(n)) { 12900e07198eSAkihiko Odaki if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 12910e07198eSAkihiko Odaki warn_report("Can't load eBPF RSS for vhost"); 12920e07198eSAkihiko Odaki } else { 12930e07198eSAkihiko Odaki warn_report("Can't load eBPF RSS - fallback to software RSS"); 12940e07198eSAkihiko Odaki n->rss_data.enabled_software_rss = true; 12950e07198eSAkihiko Odaki } 12960e07198eSAkihiko Odaki } 12970e07198eSAkihiko Odaki 12980e07198eSAkihiko Odaki trace_virtio_net_rss_enable(n->rss_data.hash_types, 12990e07198eSAkihiko Odaki n->rss_data.indirections_len, 13000e07198eSAkihiko Odaki sizeof(n->rss_data.key)); 13010e07198eSAkihiko Odaki } else { 13020e07198eSAkihiko Odaki virtio_net_detach_epbf_rss(n); 13030e07198eSAkihiko Odaki trace_virtio_net_rss_disable(); 13040e07198eSAkihiko Odaki } 13050e07198eSAkihiko Odaki } 13060e07198eSAkihiko Odaki 13070e07198eSAkihiko Odaki static void virtio_net_disable_rss(VirtIONet *n) 13080e07198eSAkihiko Odaki { 13090e07198eSAkihiko Odaki if (!n->rss_data.enabled) { 13100e07198eSAkihiko Odaki return; 13110e07198eSAkihiko Odaki } 13120e07198eSAkihiko Odaki 13130e07198eSAkihiko Odaki n->rss_data.enabled = false; 13140e07198eSAkihiko Odaki virtio_net_commit_rss_config(n); 13150e07198eSAkihiko Odaki } 13160e07198eSAkihiko Odaki 1317283be596SAkihiko Odaki static bool virtio_net_load_ebpf_fds(VirtIONet *n) 13180145c393SAndrew Melnychenko { 13196b230b7dSAndrew Melnychenko int fds[EBPF_RSS_MAX_FDS] = { [0 ... EBPF_RSS_MAX_FDS - 1] = -1}; 13206b230b7dSAndrew Melnychenko int ret = true; 13216b230b7dSAndrew Melnychenko int i = 0; 13226b230b7dSAndrew Melnychenko 13236b230b7dSAndrew Melnychenko if (n->nr_ebpf_rss_fds != EBPF_RSS_MAX_FDS) { 1324283be596SAkihiko Odaki warn_report("Expected %d file descriptors but got %d", 13256b230b7dSAndrew Melnychenko EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds); 13260145c393SAndrew Melnychenko return false; 13270145c393SAndrew Melnychenko } 13280145c393SAndrew Melnychenko 13296b230b7dSAndrew Melnychenko for (i = 0; i < n->nr_ebpf_rss_fds; i++) { 1330283be596SAkihiko Odaki fds[i] = monitor_fd_param(monitor_cur(), n->ebpf_rss_fds[i], 1331283be596SAkihiko Odaki &error_warn); 1332283be596SAkihiko Odaki if (fds[i] < 0) { 13336b230b7dSAndrew Melnychenko ret = false; 13346b230b7dSAndrew Melnychenko goto exit; 13356b230b7dSAndrew Melnychenko } 13366b230b7dSAndrew Melnychenko } 13376b230b7dSAndrew Melnychenko 13386b230b7dSAndrew Melnychenko ret = ebpf_rss_load_fds(&n->ebpf_rss, fds[0], fds[1], fds[2], fds[3]); 13396b230b7dSAndrew Melnychenko 13406b230b7dSAndrew Melnychenko exit: 1341283be596SAkihiko Odaki if (!ret) { 13426b230b7dSAndrew Melnychenko for (i = 0; i < n->nr_ebpf_rss_fds && fds[i] != -1; i++) { 13436b230b7dSAndrew Melnychenko close(fds[i]); 13446b230b7dSAndrew Melnychenko } 13456b230b7dSAndrew Melnychenko } 13466b230b7dSAndrew Melnychenko 13476b230b7dSAndrew Melnychenko return ret; 13486b230b7dSAndrew Melnychenko } 13496b230b7dSAndrew Melnychenko 1350283be596SAkihiko Odaki static bool virtio_net_load_ebpf(VirtIONet *n) 13516b230b7dSAndrew Melnychenko { 13526b230b7dSAndrew Melnychenko bool ret = false; 13536b230b7dSAndrew Melnychenko 13546b230b7dSAndrew Melnychenko if (virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 1355283be596SAkihiko Odaki if (!(n->ebpf_rss_fds && virtio_net_load_ebpf_fds(n))) { 13566b230b7dSAndrew Melnychenko ret = ebpf_rss_load(&n->ebpf_rss); 13576b230b7dSAndrew Melnychenko } 13586b230b7dSAndrew Melnychenko } 13596b230b7dSAndrew Melnychenko 13606b230b7dSAndrew Melnychenko return ret; 13610145c393SAndrew Melnychenko } 13620145c393SAndrew Melnychenko 13630145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 13640145c393SAndrew Melnychenko { 13650145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 13660145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 136759079029SYuri Benditovich } 136859079029SYuri Benditovich 136959079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1370e22f0603SYuri Benditovich struct iovec *iov, 1371e22f0603SYuri Benditovich unsigned int iov_cnt, 1372e22f0603SYuri Benditovich bool do_rss) 137359079029SYuri Benditovich { 137459079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 137559079029SYuri Benditovich struct virtio_net_rss_config cfg; 137659079029SYuri Benditovich size_t s, offset = 0, size_get; 1377441537f1SJason Wang uint16_t queue_pairs, i; 137859079029SYuri Benditovich struct { 137959079029SYuri Benditovich uint16_t us; 138059079029SYuri Benditovich uint8_t b; 138159079029SYuri Benditovich } QEMU_PACKED temp; 138259079029SYuri Benditovich const char *err_msg = ""; 138359079029SYuri Benditovich uint32_t err_value = 0; 138459079029SYuri Benditovich 1385e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 138659079029SYuri Benditovich err_msg = "RSS is not negotiated"; 138759079029SYuri Benditovich goto error; 138859079029SYuri Benditovich } 1389e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1390e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1391e22f0603SYuri Benditovich goto error; 1392e22f0603SYuri Benditovich } 139359079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 139459079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 139559079029SYuri Benditovich if (s != size_get) { 139659079029SYuri Benditovich err_msg = "Short command buffer"; 139759079029SYuri Benditovich err_value = (uint32_t)s; 139859079029SYuri Benditovich goto error; 139959079029SYuri Benditovich } 140059079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 140159079029SYuri Benditovich n->rss_data.indirections_len = 140259079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 140359079029SYuri Benditovich n->rss_data.indirections_len++; 1404e22f0603SYuri Benditovich if (!do_rss) { 1405e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1406e22f0603SYuri Benditovich } 140759079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 140859079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 140959079029SYuri Benditovich err_value = n->rss_data.indirections_len; 141059079029SYuri Benditovich goto error; 141159079029SYuri Benditovich } 141259079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 141359079029SYuri Benditovich err_msg = "Too large indirection table"; 141459079029SYuri Benditovich err_value = n->rss_data.indirections_len; 141559079029SYuri Benditovich goto error; 141659079029SYuri Benditovich } 1417e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1418e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1419441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 142059079029SYuri Benditovich err_msg = "Invalid default queue"; 142159079029SYuri Benditovich err_value = n->rss_data.default_queue; 142259079029SYuri Benditovich goto error; 142359079029SYuri Benditovich } 142459079029SYuri Benditovich offset += size_get; 142559079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 142659079029SYuri Benditovich g_free(n->rss_data.indirections_table); 142759079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 142859079029SYuri Benditovich if (!n->rss_data.indirections_table) { 142959079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 143059079029SYuri Benditovich err_value = n->rss_data.indirections_len; 143159079029SYuri Benditovich goto error; 143259079029SYuri Benditovich } 143359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 143459079029SYuri Benditovich n->rss_data.indirections_table, size_get); 143559079029SYuri Benditovich if (s != size_get) { 143659079029SYuri Benditovich err_msg = "Short indirection table buffer"; 143759079029SYuri Benditovich err_value = (uint32_t)s; 143859079029SYuri Benditovich goto error; 143959079029SYuri Benditovich } 144059079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 144159079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 144259079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 144359079029SYuri Benditovich } 144459079029SYuri Benditovich offset += size_get; 144559079029SYuri Benditovich size_get = sizeof(temp); 144659079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 144759079029SYuri Benditovich if (s != size_get) { 1448441537f1SJason Wang err_msg = "Can't get queue_pairs"; 144959079029SYuri Benditovich err_value = (uint32_t)s; 145059079029SYuri Benditovich goto error; 145159079029SYuri Benditovich } 1452441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1453441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1454441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1455441537f1SJason Wang err_value = queue_pairs; 145659079029SYuri Benditovich goto error; 145759079029SYuri Benditovich } 145859079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 145959079029SYuri Benditovich err_msg = "Invalid key size"; 146059079029SYuri Benditovich err_value = temp.b; 146159079029SYuri Benditovich goto error; 146259079029SYuri Benditovich } 146359079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 146459079029SYuri Benditovich err_msg = "No key provided"; 146559079029SYuri Benditovich err_value = 0; 146659079029SYuri Benditovich goto error; 146759079029SYuri Benditovich } 146859079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 146959079029SYuri Benditovich virtio_net_disable_rss(n); 1470441537f1SJason Wang return queue_pairs; 147159079029SYuri Benditovich } 147259079029SYuri Benditovich offset += size_get; 147359079029SYuri Benditovich size_get = temp.b; 147459079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 147559079029SYuri Benditovich if (s != size_get) { 147659079029SYuri Benditovich err_msg = "Can get key buffer"; 147759079029SYuri Benditovich err_value = (uint32_t)s; 147859079029SYuri Benditovich goto error; 147959079029SYuri Benditovich } 148059079029SYuri Benditovich n->rss_data.enabled = true; 14810e07198eSAkihiko Odaki virtio_net_commit_rss_config(n); 1482441537f1SJason Wang return queue_pairs; 148359079029SYuri Benditovich error: 148459079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 148559079029SYuri Benditovich virtio_net_disable_rss(n); 148659079029SYuri Benditovich return 0; 148759079029SYuri Benditovich } 148859079029SYuri Benditovich 14896e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 14906e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 14916e790746SPaolo Bonzini { 149217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1493441537f1SJason Wang uint16_t queue_pairs; 14942a7888ccSSi-Wei Liu NetClientState *nc = qemu_get_queue(n->nic); 14956e790746SPaolo Bonzini 149659079029SYuri Benditovich virtio_net_disable_rss(n); 1497e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1498441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1499441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1500e22f0603SYuri Benditovich } 150159079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1502441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 150359079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 150459079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 150559079029SYuri Benditovich size_t s; 150659079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 150759079029SYuri Benditovich return VIRTIO_NET_ERR; 150859079029SYuri Benditovich } 15096e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 15106e790746SPaolo Bonzini if (s != sizeof(mq)) { 15116e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15126e790746SPaolo Bonzini } 1513441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 15146e790746SPaolo Bonzini 151559079029SYuri Benditovich } else { 15166e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15176e790746SPaolo Bonzini } 15186e790746SPaolo Bonzini 1519441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1520441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1521441537f1SJason Wang queue_pairs > n->max_queue_pairs || 15226e790746SPaolo Bonzini !n->multiqueue) { 15236e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15246e790746SPaolo Bonzini } 15256e790746SPaolo Bonzini 1526441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1527ca8717f9SEugenio Pérez if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 1528ca8717f9SEugenio Pérez /* 1529ca8717f9SEugenio Pérez * Avoid updating the backend for a vdpa device: We're only interested 1530ca8717f9SEugenio Pérez * in updating the device model queues. 1531ca8717f9SEugenio Pérez */ 1532ca8717f9SEugenio Pérez return VIRTIO_NET_OK; 1533ca8717f9SEugenio Pérez } 1534441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 15356e790746SPaolo Bonzini * disabled queue */ 153617a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1537441537f1SJason Wang virtio_net_set_queue_pairs(n); 15386e790746SPaolo Bonzini 15396e790746SPaolo Bonzini return VIRTIO_NET_OK; 15406e790746SPaolo Bonzini } 1541ba7eadb5SGreg Kurz 1542640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, 1543640b8a1cSEugenio Pérez const struct iovec *in_sg, unsigned in_num, 1544640b8a1cSEugenio Pérez const struct iovec *out_sg, 1545640b8a1cSEugenio Pérez unsigned out_num) 15466e790746SPaolo Bonzini { 154717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 15486e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 15496e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 15506e790746SPaolo Bonzini size_t s; 1551771b6ed3SJason Wang struct iovec *iov, *iov2; 1552640b8a1cSEugenio Pérez 1553640b8a1cSEugenio Pérez if (iov_size(in_sg, in_num) < sizeof(status) || 1554640b8a1cSEugenio Pérez iov_size(out_sg, out_num) < sizeof(ctrl)) { 1555640b8a1cSEugenio Pérez virtio_error(vdev, "virtio-net ctrl missing headers"); 1556640b8a1cSEugenio Pérez return 0; 1557640b8a1cSEugenio Pérez } 1558640b8a1cSEugenio Pérez 1559640b8a1cSEugenio Pérez iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num); 1560640b8a1cSEugenio Pérez s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl)); 1561640b8a1cSEugenio Pérez iov_discard_front(&iov, &out_num, sizeof(ctrl)); 1562640b8a1cSEugenio Pérez if (s != sizeof(ctrl)) { 1563640b8a1cSEugenio Pérez status = VIRTIO_NET_ERR; 1564640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 1565640b8a1cSEugenio Pérez status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num); 1566640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 1567640b8a1cSEugenio Pérez status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num); 1568640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 1569640b8a1cSEugenio Pérez status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num); 1570640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1571640b8a1cSEugenio Pérez status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num); 1572640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 1573640b8a1cSEugenio Pérez status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num); 1574640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1575640b8a1cSEugenio Pérez status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num); 1576640b8a1cSEugenio Pérez } 1577640b8a1cSEugenio Pérez 1578640b8a1cSEugenio Pérez s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status)); 1579640b8a1cSEugenio Pérez assert(s == sizeof(status)); 1580640b8a1cSEugenio Pérez 1581640b8a1cSEugenio Pérez g_free(iov2); 1582640b8a1cSEugenio Pérez return sizeof(status); 1583640b8a1cSEugenio Pérez } 1584640b8a1cSEugenio Pérez 1585640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 1586640b8a1cSEugenio Pérez { 1587640b8a1cSEugenio Pérez VirtQueueElement *elem; 15886e790746SPaolo Bonzini 158951b19ebeSPaolo Bonzini for (;;) { 1590640b8a1cSEugenio Pérez size_t written; 159151b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 159251b19ebeSPaolo Bonzini if (!elem) { 159351b19ebeSPaolo Bonzini break; 159451b19ebeSPaolo Bonzini } 1595640b8a1cSEugenio Pérez 1596640b8a1cSEugenio Pérez written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num, 1597640b8a1cSEugenio Pérez elem->out_sg, elem->out_num); 1598640b8a1cSEugenio Pérez if (written > 0) { 1599640b8a1cSEugenio Pérez virtqueue_push(vq, elem, written); 1600640b8a1cSEugenio Pérez virtio_notify(vdev, vq); 1601640b8a1cSEugenio Pérez g_free(elem); 1602640b8a1cSEugenio Pérez } else { 1603ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1604ba7eadb5SGreg Kurz g_free(elem); 1605ba7eadb5SGreg Kurz break; 16066e790746SPaolo Bonzini } 16076e790746SPaolo Bonzini } 16086e790746SPaolo Bonzini } 16096e790746SPaolo Bonzini 16106e790746SPaolo Bonzini /* RX */ 16116e790746SPaolo Bonzini 16126e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 16136e790746SPaolo Bonzini { 161417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 16156e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 16166e790746SPaolo Bonzini 16176e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 16186e790746SPaolo Bonzini } 16196e790746SPaolo Bonzini 1620b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 16216e790746SPaolo Bonzini { 16226e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 162317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 16246e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 16256e790746SPaolo Bonzini 162617a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1627b8c4b67eSPhilippe Mathieu-Daudé return false; 16286e790746SPaolo Bonzini } 16296e790746SPaolo Bonzini 1630441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1631b8c4b67eSPhilippe Mathieu-Daudé return false; 16326e790746SPaolo Bonzini } 16336e790746SPaolo Bonzini 16346e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 163517a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1636b8c4b67eSPhilippe Mathieu-Daudé return false; 16376e790746SPaolo Bonzini } 16386e790746SPaolo Bonzini 1639b8c4b67eSPhilippe Mathieu-Daudé return true; 16406e790746SPaolo Bonzini } 16416e790746SPaolo Bonzini 16426e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 16436e790746SPaolo Bonzini { 1644*f937309fSthomas int opaque; 1645*f937309fSthomas unsigned int in_bytes; 16466e790746SPaolo Bonzini VirtIONet *n = q->n; 16476e790746SPaolo Bonzini 1648*f937309fSthomas while (virtio_queue_empty(q->rx_vq) || n->mergeable_rx_bufs) { 1649*f937309fSthomas opaque = virtqueue_get_avail_bytes(q->rx_vq, &in_bytes, NULL, 1650*f937309fSthomas bufsize, 0); 1651*f937309fSthomas /* Buffer is enough, disable notifiaction */ 1652*f937309fSthomas if (bufsize <= in_bytes) { 1653*f937309fSthomas break; 1654*f937309fSthomas } 1655*f937309fSthomas 1656*f937309fSthomas if (virtio_queue_enable_notification_and_check(q->rx_vq, opaque)) { 1657*f937309fSthomas /* Guest has added some buffers, try again */ 1658*f937309fSthomas continue; 1659*f937309fSthomas } else { 16606e790746SPaolo Bonzini return 0; 16616e790746SPaolo Bonzini } 16626e790746SPaolo Bonzini } 16636e790746SPaolo Bonzini 16646e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 1665*f937309fSthomas 16666e790746SPaolo Bonzini return 1; 16676e790746SPaolo Bonzini } 16686e790746SPaolo Bonzini 16691399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1670032a74a1SCédric Le Goater { 16711399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 16721399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 16731399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 16741399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1675032a74a1SCédric Le Goater } 1676032a74a1SCédric Le Goater 16776e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 16786e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 16796e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 16806e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 16816e790746SPaolo Bonzini * dhclient yet. 16826e790746SPaolo Bonzini * 16836e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 16846e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 16856e790746SPaolo Bonzini * kernels. 16866e790746SPaolo Bonzini * 16876e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 16886e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 16896e790746SPaolo Bonzini * cache. 16906e790746SPaolo Bonzini */ 16916e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 16926e790746SPaolo Bonzini uint8_t *buf, size_t size) 16936e790746SPaolo Bonzini { 16946e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 16956e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 16966e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 16976e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 16986e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1699f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 17006e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 17016e790746SPaolo Bonzini } 17026e790746SPaolo Bonzini } 17036e790746SPaolo Bonzini 17046e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 17056e790746SPaolo Bonzini const void *buf, size_t size) 17066e790746SPaolo Bonzini { 17076e790746SPaolo Bonzini if (n->has_vnet_hdr) { 17086e790746SPaolo Bonzini /* FIXME this cast is evil */ 17096e790746SPaolo Bonzini void *wbuf = (void *)buf; 17106e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 17116e790746SPaolo Bonzini size - n->host_hdr_len); 17121bfa316cSGreg Kurz 17131bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 17141399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 17151bfa316cSGreg Kurz } 17166e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 17176e790746SPaolo Bonzini } else { 17186e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 17196e790746SPaolo Bonzini .flags = 0, 17206e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 17216e790746SPaolo Bonzini }; 17226e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 17236e790746SPaolo Bonzini } 17246e790746SPaolo Bonzini } 17256e790746SPaolo Bonzini 17266e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 17276e790746SPaolo Bonzini { 17286e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 17296e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 17306e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 17316e790746SPaolo Bonzini int i; 17326e790746SPaolo Bonzini 17336e790746SPaolo Bonzini if (n->promisc) 17346e790746SPaolo Bonzini return 1; 17356e790746SPaolo Bonzini 17366e790746SPaolo Bonzini ptr += n->host_hdr_len; 17376e790746SPaolo Bonzini 17386e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 17397542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 17406e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 17416e790746SPaolo Bonzini return 0; 17426e790746SPaolo Bonzini } 17436e790746SPaolo Bonzini 17446e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 17456e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 17466e790746SPaolo Bonzini return !n->nobcast; 17476e790746SPaolo Bonzini } else if (n->nomulti) { 17486e790746SPaolo Bonzini return 0; 17496e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 17506e790746SPaolo Bonzini return 1; 17516e790746SPaolo Bonzini } 17526e790746SPaolo Bonzini 17536e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 17546e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17556e790746SPaolo Bonzini return 1; 17566e790746SPaolo Bonzini } 17576e790746SPaolo Bonzini } 17586e790746SPaolo Bonzini } else { // unicast 17596e790746SPaolo Bonzini if (n->nouni) { 17606e790746SPaolo Bonzini return 0; 17616e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 17626e790746SPaolo Bonzini return 1; 17636e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 17646e790746SPaolo Bonzini return 1; 17656e790746SPaolo Bonzini } 17666e790746SPaolo Bonzini 17676e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 17686e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17696e790746SPaolo Bonzini return 1; 17706e790746SPaolo Bonzini } 17716e790746SPaolo Bonzini } 17726e790746SPaolo Bonzini } 17736e790746SPaolo Bonzini 17746e790746SPaolo Bonzini return 0; 17756e790746SPaolo Bonzini } 17766e790746SPaolo Bonzini 177769ff5ef8SAkihiko Odaki static uint8_t virtio_net_get_hash_type(bool hasip4, 177869ff5ef8SAkihiko Odaki bool hasip6, 177965f474bbSAkihiko Odaki EthL4HdrProto l4hdr_proto, 17804474e37aSYuri Benditovich uint32_t types) 17814474e37aSYuri Benditovich { 178269ff5ef8SAkihiko Odaki if (hasip4) { 178365f474bbSAkihiko Odaki switch (l4hdr_proto) { 178465f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_TCP: 178565f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 17864474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 17874474e37aSYuri Benditovich } 178865f474bbSAkihiko Odaki break; 178965f474bbSAkihiko Odaki 179065f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_UDP: 179165f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 17924474e37aSYuri Benditovich return NetPktRssIpV4Udp; 17934474e37aSYuri Benditovich } 179465f474bbSAkihiko Odaki break; 179565f474bbSAkihiko Odaki 179665f474bbSAkihiko Odaki default: 179765f474bbSAkihiko Odaki break; 179865f474bbSAkihiko Odaki } 179965f474bbSAkihiko Odaki 18004474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 18014474e37aSYuri Benditovich return NetPktRssIpV4; 18024474e37aSYuri Benditovich } 180369ff5ef8SAkihiko Odaki } else if (hasip6) { 180465f474bbSAkihiko Odaki switch (l4hdr_proto) { 180565f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_TCP: 180665f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) { 180765f474bbSAkihiko Odaki return NetPktRssIpV6TcpEx; 180865f474bbSAkihiko Odaki } 180965f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 181065f474bbSAkihiko Odaki return NetPktRssIpV6Tcp; 181165f474bbSAkihiko Odaki } 181265f474bbSAkihiko Odaki break; 18134474e37aSYuri Benditovich 181465f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_UDP: 181565f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) { 181665f474bbSAkihiko Odaki return NetPktRssIpV6UdpEx; 18174474e37aSYuri Benditovich } 181865f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 181965f474bbSAkihiko Odaki return NetPktRssIpV6Udp; 18204474e37aSYuri Benditovich } 182165f474bbSAkihiko Odaki break; 182265f474bbSAkihiko Odaki 182365f474bbSAkihiko Odaki default: 182465f474bbSAkihiko Odaki break; 182565f474bbSAkihiko Odaki } 182665f474bbSAkihiko Odaki 182765f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) { 182865f474bbSAkihiko Odaki return NetPktRssIpV6Ex; 182965f474bbSAkihiko Odaki } 183065f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 183165f474bbSAkihiko Odaki return NetPktRssIpV6; 18324474e37aSYuri Benditovich } 18334474e37aSYuri Benditovich } 18344474e37aSYuri Benditovich return 0xff; 18354474e37aSYuri Benditovich } 18364474e37aSYuri Benditovich 18374474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 1838a4c960eeSAkihiko Odaki size_t size, 1839a4c960eeSAkihiko Odaki struct virtio_net_hdr_v1_hash *hdr) 18406e790746SPaolo Bonzini { 18416e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1842e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 18434474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 18444474e37aSYuri Benditovich uint8_t net_hash_type; 18454474e37aSYuri Benditovich uint32_t hash; 184665f474bbSAkihiko Odaki bool hasip4, hasip6; 184765f474bbSAkihiko Odaki EthL4HdrProto l4hdr_proto; 1848e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1849e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1850e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1851e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1852e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1853e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1854e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1855e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1856e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1857e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1858e22f0603SYuri Benditovich }; 18592f0fa232SAkihiko Odaki struct iovec iov = { 18602f0fa232SAkihiko Odaki .iov_base = (void *)buf, 18612f0fa232SAkihiko Odaki .iov_len = size 18622f0fa232SAkihiko Odaki }; 18634474e37aSYuri Benditovich 18642f0fa232SAkihiko Odaki net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len); 186565f474bbSAkihiko Odaki net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto); 186665f474bbSAkihiko Odaki net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto, 18674474e37aSYuri Benditovich n->rss_data.hash_types); 18684474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1869e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1870a4c960eeSAkihiko Odaki hdr->hash_value = VIRTIO_NET_HASH_REPORT_NONE; 1871a4c960eeSAkihiko Odaki hdr->hash_report = 0; 1872e22f0603SYuri Benditovich } 1873e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 18744474e37aSYuri Benditovich } 18754474e37aSYuri Benditovich 18764474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1877e22f0603SYuri Benditovich 1878e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1879a4c960eeSAkihiko Odaki hdr->hash_value = hash; 1880a4c960eeSAkihiko Odaki hdr->hash_report = reports[net_hash_type]; 1881e22f0603SYuri Benditovich } 1882e22f0603SYuri Benditovich 1883e22f0603SYuri Benditovich if (n->rss_data.redirect) { 18844474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 18854474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 18864474e37aSYuri Benditovich } 1887e22f0603SYuri Benditovich 1888e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 18894474e37aSYuri Benditovich } 18904474e37aSYuri Benditovich 18914474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 18924474e37aSYuri Benditovich size_t size, bool no_rss) 18934474e37aSYuri Benditovich { 18944474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 18956e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 189617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1897bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1898bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 18996e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 1900a4c960eeSAkihiko Odaki struct virtio_net_hdr_v1_hash extra_hdr; 19016e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1902bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1903bedd7e93SJason Wang ssize_t err; 19046e790746SPaolo Bonzini 19056e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 19066e790746SPaolo Bonzini return -1; 19076e790746SPaolo Bonzini } 19086e790746SPaolo Bonzini 19090145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 1910a4c960eeSAkihiko Odaki int index = virtio_net_process_rss(nc, buf, size, &extra_hdr); 19114474e37aSYuri Benditovich if (index >= 0) { 1912f1595cebSAkihiko Odaki NetClientState *nc2 = 1913f1595cebSAkihiko Odaki qemu_get_subqueue(n->nic, index % n->curr_queue_pairs); 19144474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 19154474e37aSYuri Benditovich } 19164474e37aSYuri Benditovich } 19174474e37aSYuri Benditovich 19186e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 19196e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 19206e790746SPaolo Bonzini return 0; 19216e790746SPaolo Bonzini } 19226e790746SPaolo Bonzini 19236e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 19246e790746SPaolo Bonzini return size; 19256e790746SPaolo Bonzini 19266e790746SPaolo Bonzini offset = i = 0; 19276e790746SPaolo Bonzini 19286e790746SPaolo Bonzini while (offset < size) { 192951b19ebeSPaolo Bonzini VirtQueueElement *elem; 19306e790746SPaolo Bonzini int len, total; 193151b19ebeSPaolo Bonzini const struct iovec *sg; 19326e790746SPaolo Bonzini 19336e790746SPaolo Bonzini total = 0; 19346e790746SPaolo Bonzini 1935bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1936bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1937bedd7e93SJason Wang err = size; 1938bedd7e93SJason Wang goto err; 1939bedd7e93SJason Wang } 1940bedd7e93SJason Wang 194151b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 194251b19ebeSPaolo Bonzini if (!elem) { 1943ba10b9c0SGreg Kurz if (i) { 1944ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 19456e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1946019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1947019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 19486e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1949019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1950019a3edbSGerd Hoffmann vdev->guest_features); 1951ba10b9c0SGreg Kurz } 1952bedd7e93SJason Wang err = -1; 1953bedd7e93SJason Wang goto err; 19546e790746SPaolo Bonzini } 19556e790746SPaolo Bonzini 195651b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1957ba10b9c0SGreg Kurz virtio_error(vdev, 1958ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1959ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1960ba10b9c0SGreg Kurz g_free(elem); 1961bedd7e93SJason Wang err = -1; 1962bedd7e93SJason Wang goto err; 19636e790746SPaolo Bonzini } 19646e790746SPaolo Bonzini 196551b19ebeSPaolo Bonzini sg = elem->in_sg; 19666e790746SPaolo Bonzini if (i == 0) { 19676e790746SPaolo Bonzini assert(offset == 0); 19686e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 19696e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 197051b19ebeSPaolo Bonzini sg, elem->in_num, 1971a4c960eeSAkihiko Odaki offsetof(typeof(extra_hdr), hdr.num_buffers), 1972a4c960eeSAkihiko Odaki sizeof(extra_hdr.hdr.num_buffers)); 19736e790746SPaolo Bonzini } 19746e790746SPaolo Bonzini 197551b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1976e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1977a4c960eeSAkihiko Odaki offset = offsetof(typeof(extra_hdr), hash_value); 1978e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1979a4c960eeSAkihiko Odaki (char *)&extra_hdr + offset, 1980a4c960eeSAkihiko Odaki sizeof(extra_hdr.hash_value) + 1981a4c960eeSAkihiko Odaki sizeof(extra_hdr.hash_report)); 1982e22f0603SYuri Benditovich } 19836e790746SPaolo Bonzini offset = n->host_hdr_len; 19846e790746SPaolo Bonzini total += n->guest_hdr_len; 19856e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 19866e790746SPaolo Bonzini } else { 19876e790746SPaolo Bonzini guest_offset = 0; 19886e790746SPaolo Bonzini } 19896e790746SPaolo Bonzini 19906e790746SPaolo Bonzini /* copy in packet. ugh */ 199151b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 19926e790746SPaolo Bonzini buf + offset, size - offset); 19936e790746SPaolo Bonzini total += len; 19946e790746SPaolo Bonzini offset += len; 19956e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 19966e790746SPaolo Bonzini * must have consumed the complete packet. 19976e790746SPaolo Bonzini * Otherwise, drop it. */ 19986e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 199927e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 200051b19ebeSPaolo Bonzini g_free(elem); 2001bedd7e93SJason Wang err = size; 2002bedd7e93SJason Wang goto err; 20036e790746SPaolo Bonzini } 20046e790746SPaolo Bonzini 2005bedd7e93SJason Wang elems[i] = elem; 2006bedd7e93SJason Wang lens[i] = total; 2007bedd7e93SJason Wang i++; 20086e790746SPaolo Bonzini } 20096e790746SPaolo Bonzini 20106e790746SPaolo Bonzini if (mhdr_cnt) { 2011a4c960eeSAkihiko Odaki virtio_stw_p(vdev, &extra_hdr.hdr.num_buffers, i); 20126e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 20136e790746SPaolo Bonzini 0, 2014a4c960eeSAkihiko Odaki &extra_hdr.hdr.num_buffers, 2015a4c960eeSAkihiko Odaki sizeof extra_hdr.hdr.num_buffers); 20166e790746SPaolo Bonzini } 20176e790746SPaolo Bonzini 2018bedd7e93SJason Wang for (j = 0; j < i; j++) { 2019bedd7e93SJason Wang /* signal other side */ 2020bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 2021bedd7e93SJason Wang g_free(elems[j]); 2022bedd7e93SJason Wang } 2023bedd7e93SJason Wang 20246e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 202517a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 20266e790746SPaolo Bonzini 20276e790746SPaolo Bonzini return size; 2028bedd7e93SJason Wang 2029bedd7e93SJason Wang err: 2030bedd7e93SJason Wang for (j = 0; j < i; j++) { 2031abe300d9SJason Wang virtqueue_detach_element(q->rx_vq, elems[j], lens[j]); 2032bedd7e93SJason Wang g_free(elems[j]); 2033bedd7e93SJason Wang } 2034bedd7e93SJason Wang 2035bedd7e93SJason Wang return err; 20366e790746SPaolo Bonzini } 20376e790746SPaolo Bonzini 20382974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 203997cd965cSPaolo Bonzini size_t size) 204097cd965cSPaolo Bonzini { 2041068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 204297cd965cSPaolo Bonzini 20434474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 204497cd965cSPaolo Bonzini } 204597cd965cSPaolo Bonzini 20462974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 20472974e916SYuri Benditovich const uint8_t *buf, 20482974e916SYuri Benditovich VirtioNetRscUnit *unit) 20492974e916SYuri Benditovich { 20502974e916SYuri Benditovich uint16_t ip_hdrlen; 20512974e916SYuri Benditovich struct ip_header *ip; 20522974e916SYuri Benditovich 20532974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 20542974e916SYuri Benditovich + sizeof(struct eth_header)); 20552974e916SYuri Benditovich unit->ip = (void *)ip; 20562974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 20572974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 20582974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 20592974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 20602974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 20612974e916SYuri Benditovich } 20622974e916SYuri Benditovich 20632974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 20642974e916SYuri Benditovich const uint8_t *buf, 20652974e916SYuri Benditovich VirtioNetRscUnit *unit) 20662974e916SYuri Benditovich { 20672974e916SYuri Benditovich struct ip6_header *ip6; 20682974e916SYuri Benditovich 20692974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 20702974e916SYuri Benditovich + sizeof(struct eth_header)); 20712974e916SYuri Benditovich unit->ip = ip6; 20722974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 207378ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 20742974e916SYuri Benditovich + sizeof(struct ip6_header)); 20752974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 20762974e916SYuri Benditovich 20772431f4f1SMichael Tokarev /* There is a difference between payload length in ipv4 and v6, 20782974e916SYuri Benditovich ip header is excluded in ipv6 */ 20792974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 20802974e916SYuri Benditovich } 20812974e916SYuri Benditovich 20822974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 20832974e916SYuri Benditovich VirtioNetRscSeg *seg) 20842974e916SYuri Benditovich { 20852974e916SYuri Benditovich int ret; 2086dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 20872974e916SYuri Benditovich 2088dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 20892974e916SYuri Benditovich h->flags = 0; 20902974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 20912974e916SYuri Benditovich 20922974e916SYuri Benditovich if (seg->is_coalesced) { 2093dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 2094dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 20952974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 20962974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 20972974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 20982974e916SYuri Benditovich } else { 20992974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 21002974e916SYuri Benditovich } 21012974e916SYuri Benditovich } 21022974e916SYuri Benditovich 21032974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 21042974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 21052974e916SYuri Benditovich g_free(seg->buf); 21062974e916SYuri Benditovich g_free(seg); 21072974e916SYuri Benditovich 21082974e916SYuri Benditovich return ret; 21092974e916SYuri Benditovich } 21102974e916SYuri Benditovich 21112974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 21122974e916SYuri Benditovich { 21132974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 21142974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 21152974e916SYuri Benditovich 21162974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 21172974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 21182974e916SYuri Benditovich chain->stat.purge_failed++; 21192974e916SYuri Benditovich continue; 21202974e916SYuri Benditovich } 21212974e916SYuri Benditovich } 21222974e916SYuri Benditovich 21232974e916SYuri Benditovich chain->stat.timer++; 21242974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 21252974e916SYuri Benditovich timer_mod(chain->drain_timer, 21262974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 21272974e916SYuri Benditovich } 21282974e916SYuri Benditovich } 21292974e916SYuri Benditovich 21302974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 21312974e916SYuri Benditovich { 21322974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 21332974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 21342974e916SYuri Benditovich 21352974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 21362974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 21372974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 21382974e916SYuri Benditovich g_free(seg->buf); 21392974e916SYuri Benditovich g_free(seg); 21402974e916SYuri Benditovich } 21412974e916SYuri Benditovich 21422974e916SYuri Benditovich timer_free(chain->drain_timer); 21432974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 21442974e916SYuri Benditovich g_free(chain); 21452974e916SYuri Benditovich } 21462974e916SYuri Benditovich } 21472974e916SYuri Benditovich 21482974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 21492974e916SYuri Benditovich NetClientState *nc, 21502974e916SYuri Benditovich const uint8_t *buf, size_t size) 21512974e916SYuri Benditovich { 21522974e916SYuri Benditovich uint16_t hdr_len; 21532974e916SYuri Benditovich VirtioNetRscSeg *seg; 21542974e916SYuri Benditovich 21552974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 2156b21e2380SMarkus Armbruster seg = g_new(VirtioNetRscSeg, 1); 21572974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 21582974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 21592974e916SYuri Benditovich memcpy(seg->buf, buf, size); 21602974e916SYuri Benditovich seg->size = size; 21612974e916SYuri Benditovich seg->packets = 1; 21622974e916SYuri Benditovich seg->dup_ack = 0; 21632974e916SYuri Benditovich seg->is_coalesced = 0; 21642974e916SYuri Benditovich seg->nc = nc; 21652974e916SYuri Benditovich 21662974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 21672974e916SYuri Benditovich chain->stat.cache++; 21682974e916SYuri Benditovich 21692974e916SYuri Benditovich switch (chain->proto) { 21702974e916SYuri Benditovich case ETH_P_IP: 21712974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 21722974e916SYuri Benditovich break; 21732974e916SYuri Benditovich case ETH_P_IPV6: 21742974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 21752974e916SYuri Benditovich break; 21762974e916SYuri Benditovich default: 21772974e916SYuri Benditovich g_assert_not_reached(); 21782974e916SYuri Benditovich } 21792974e916SYuri Benditovich } 21802974e916SYuri Benditovich 21812974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 21822974e916SYuri Benditovich VirtioNetRscSeg *seg, 21832974e916SYuri Benditovich const uint8_t *buf, 21842974e916SYuri Benditovich struct tcp_header *n_tcp, 21852974e916SYuri Benditovich struct tcp_header *o_tcp) 21862974e916SYuri Benditovich { 21872974e916SYuri Benditovich uint32_t nack, oack; 21882974e916SYuri Benditovich uint16_t nwin, owin; 21892974e916SYuri Benditovich 21902974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 21912974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 21922974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 21932974e916SYuri Benditovich owin = htons(o_tcp->th_win); 21942974e916SYuri Benditovich 21952974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 21962974e916SYuri Benditovich chain->stat.ack_out_of_win++; 21972974e916SYuri Benditovich return RSC_FINAL; 21982974e916SYuri Benditovich } else if (nack == oack) { 21992974e916SYuri Benditovich /* duplicated ack or window probe */ 22002974e916SYuri Benditovich if (nwin == owin) { 22012974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 22022974e916SYuri Benditovich chain->stat.dup_ack++; 22032974e916SYuri Benditovich return RSC_FINAL; 22042974e916SYuri Benditovich } else { 22052974e916SYuri Benditovich /* Coalesce window update */ 22062974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 22072974e916SYuri Benditovich chain->stat.win_update++; 22082974e916SYuri Benditovich return RSC_COALESCE; 22092974e916SYuri Benditovich } 22102974e916SYuri Benditovich } else { 22112974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 22122974e916SYuri Benditovich chain->stat.pure_ack++; 22132974e916SYuri Benditovich return RSC_FINAL; 22142974e916SYuri Benditovich } 22152974e916SYuri Benditovich } 22162974e916SYuri Benditovich 22172974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 22182974e916SYuri Benditovich VirtioNetRscSeg *seg, 22192974e916SYuri Benditovich const uint8_t *buf, 22202974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 22212974e916SYuri Benditovich { 22222974e916SYuri Benditovich void *data; 22232974e916SYuri Benditovich uint16_t o_ip_len; 22242974e916SYuri Benditovich uint32_t nseq, oseq; 22252974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 22262974e916SYuri Benditovich 22272974e916SYuri Benditovich o_unit = &seg->unit; 22282974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 22292974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 22302974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 22312974e916SYuri Benditovich 22322974e916SYuri Benditovich /* out of order or retransmitted. */ 22332974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 22342974e916SYuri Benditovich chain->stat.data_out_of_win++; 22352974e916SYuri Benditovich return RSC_FINAL; 22362974e916SYuri Benditovich } 22372974e916SYuri Benditovich 22382974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 22392974e916SYuri Benditovich if (nseq == oseq) { 22402974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 22412974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 22422974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 22432974e916SYuri Benditovich goto coalesce; 22442974e916SYuri Benditovich } else { 22452974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 22462974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 22472974e916SYuri Benditovich } 22482974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 22492974e916SYuri Benditovich /* Not a consistent packet, out of order */ 22502974e916SYuri Benditovich chain->stat.data_out_of_order++; 22512974e916SYuri Benditovich return RSC_FINAL; 22522974e916SYuri Benditovich } else { 22532974e916SYuri Benditovich coalesce: 22542974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 22552974e916SYuri Benditovich chain->stat.over_size++; 22562974e916SYuri Benditovich return RSC_FINAL; 22572974e916SYuri Benditovich } 22582974e916SYuri Benditovich 22592974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 22602974e916SYuri Benditovich so use the field value to update and record the new data len */ 22612974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 22622974e916SYuri Benditovich 22632974e916SYuri Benditovich /* update field in ip header */ 22642974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 22652974e916SYuri Benditovich 22662974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 22672974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 22682974e916SYuri Benditovich guest (only if it uses RSC feature). */ 22692974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 22702974e916SYuri Benditovich 22712974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 22722974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 22732974e916SYuri Benditovich 22742974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 22752974e916SYuri Benditovich seg->size += n_unit->payload; 22762974e916SYuri Benditovich seg->packets++; 22772974e916SYuri Benditovich chain->stat.coalesced++; 22782974e916SYuri Benditovich return RSC_COALESCE; 22792974e916SYuri Benditovich } 22802974e916SYuri Benditovich } 22812974e916SYuri Benditovich 22822974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 22832974e916SYuri Benditovich VirtioNetRscSeg *seg, 22842974e916SYuri Benditovich const uint8_t *buf, size_t size, 22852974e916SYuri Benditovich VirtioNetRscUnit *unit) 22862974e916SYuri Benditovich { 22872974e916SYuri Benditovich struct ip_header *ip1, *ip2; 22882974e916SYuri Benditovich 22892974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 22902974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 22912974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 22922974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 22932974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 22942974e916SYuri Benditovich chain->stat.no_match++; 22952974e916SYuri Benditovich return RSC_NO_MATCH; 22962974e916SYuri Benditovich } 22972974e916SYuri Benditovich 22982974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 22992974e916SYuri Benditovich } 23002974e916SYuri Benditovich 23012974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 23022974e916SYuri Benditovich VirtioNetRscSeg *seg, 23032974e916SYuri Benditovich const uint8_t *buf, size_t size, 23042974e916SYuri Benditovich VirtioNetRscUnit *unit) 23052974e916SYuri Benditovich { 23062974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 23072974e916SYuri Benditovich 23082974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 23092974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 23102974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 23112974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 23122974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 23132974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 23142974e916SYuri Benditovich chain->stat.no_match++; 23152974e916SYuri Benditovich return RSC_NO_MATCH; 23162974e916SYuri Benditovich } 23172974e916SYuri Benditovich 23182974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 23192974e916SYuri Benditovich } 23202974e916SYuri Benditovich 23212974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 23222974e916SYuri Benditovich * to prevent out of order */ 23232974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 23242974e916SYuri Benditovich struct tcp_header *tcp) 23252974e916SYuri Benditovich { 23262974e916SYuri Benditovich uint16_t tcp_hdr; 23272974e916SYuri Benditovich uint16_t tcp_flag; 23282974e916SYuri Benditovich 23292974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 23302974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 23312974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 23322974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 23332974e916SYuri Benditovich chain->stat.tcp_syn++; 23342974e916SYuri Benditovich return RSC_BYPASS; 23352974e916SYuri Benditovich } 23362974e916SYuri Benditovich 23372974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 23382974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 23392974e916SYuri Benditovich return RSC_FINAL; 23402974e916SYuri Benditovich } 23412974e916SYuri Benditovich 23422974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 23432974e916SYuri Benditovich chain->stat.tcp_all_opt++; 23442974e916SYuri Benditovich return RSC_FINAL; 23452974e916SYuri Benditovich } 23462974e916SYuri Benditovich 23472974e916SYuri Benditovich return RSC_CANDIDATE; 23482974e916SYuri Benditovich } 23492974e916SYuri Benditovich 23502974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 23512974e916SYuri Benditovich NetClientState *nc, 23522974e916SYuri Benditovich const uint8_t *buf, size_t size, 23532974e916SYuri Benditovich VirtioNetRscUnit *unit) 23542974e916SYuri Benditovich { 23552974e916SYuri Benditovich int ret; 23562974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 23572974e916SYuri Benditovich 23582974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 23592974e916SYuri Benditovich chain->stat.empty_cache++; 23602974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 23612974e916SYuri Benditovich timer_mod(chain->drain_timer, 23622974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 23632974e916SYuri Benditovich return size; 23642974e916SYuri Benditovich } 23652974e916SYuri Benditovich 23662974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 23672974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 23682974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 23692974e916SYuri Benditovich } else { 23702974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 23712974e916SYuri Benditovich } 23722974e916SYuri Benditovich 23732974e916SYuri Benditovich if (ret == RSC_FINAL) { 23742974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 23752974e916SYuri Benditovich /* Send failed */ 23762974e916SYuri Benditovich chain->stat.final_failed++; 23772974e916SYuri Benditovich return 0; 23782974e916SYuri Benditovich } 23792974e916SYuri Benditovich 23802974e916SYuri Benditovich /* Send current packet */ 23812974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23822974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 23832974e916SYuri Benditovich continue; 23842974e916SYuri Benditovich } else { 23852974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 23862974e916SYuri Benditovich seg->is_coalesced = 1; 23872974e916SYuri Benditovich return size; 23882974e916SYuri Benditovich } 23892974e916SYuri Benditovich } 23902974e916SYuri Benditovich 23912974e916SYuri Benditovich chain->stat.no_match_cache++; 23922974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 23932974e916SYuri Benditovich return size; 23942974e916SYuri Benditovich } 23952974e916SYuri Benditovich 23962974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 23972974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 23982974e916SYuri Benditovich NetClientState *nc, 23992974e916SYuri Benditovich const uint8_t *buf, size_t size, 24002974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 24012974e916SYuri Benditovich uint16_t tcp_port) 24022974e916SYuri Benditovich { 24032974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 24042974e916SYuri Benditovich uint32_t ppair1, ppair2; 24052974e916SYuri Benditovich 24062974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 24072974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 24082974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 24092974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 24102974e916SYuri Benditovich || (ppair1 != ppair2)) { 24112974e916SYuri Benditovich continue; 24122974e916SYuri Benditovich } 24132974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 24142974e916SYuri Benditovich chain->stat.drain_failed++; 24152974e916SYuri Benditovich } 24162974e916SYuri Benditovich 24172974e916SYuri Benditovich break; 24182974e916SYuri Benditovich } 24192974e916SYuri Benditovich 24202974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24212974e916SYuri Benditovich } 24222974e916SYuri Benditovich 24232974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 24242974e916SYuri Benditovich struct ip_header *ip, 24252974e916SYuri Benditovich const uint8_t *buf, size_t size) 24262974e916SYuri Benditovich { 24272974e916SYuri Benditovich uint16_t ip_len; 24282974e916SYuri Benditovich 24292974e916SYuri Benditovich /* Not an ipv4 packet */ 24302974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 24312974e916SYuri Benditovich chain->stat.ip_option++; 24322974e916SYuri Benditovich return RSC_BYPASS; 24332974e916SYuri Benditovich } 24342974e916SYuri Benditovich 24352974e916SYuri Benditovich /* Don't handle packets with ip option */ 24362974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 24372974e916SYuri Benditovich chain->stat.ip_option++; 24382974e916SYuri Benditovich return RSC_BYPASS; 24392974e916SYuri Benditovich } 24402974e916SYuri Benditovich 24412974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 24422974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24432974e916SYuri Benditovich return RSC_BYPASS; 24442974e916SYuri Benditovich } 24452974e916SYuri Benditovich 24462974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 24472974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 24482974e916SYuri Benditovich chain->stat.ip_frag++; 24492974e916SYuri Benditovich return RSC_BYPASS; 24502974e916SYuri Benditovich } 24512974e916SYuri Benditovich 24522974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 24532974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 24542974e916SYuri Benditovich chain->stat.ip_ecn++; 24552974e916SYuri Benditovich return RSC_BYPASS; 24562974e916SYuri Benditovich } 24572974e916SYuri Benditovich 24582974e916SYuri Benditovich ip_len = htons(ip->ip_len); 24592974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 24602974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 24612974e916SYuri Benditovich sizeof(struct eth_header))) { 24622974e916SYuri Benditovich chain->stat.ip_hacked++; 24632974e916SYuri Benditovich return RSC_BYPASS; 24642974e916SYuri Benditovich } 24652974e916SYuri Benditovich 24662974e916SYuri Benditovich return RSC_CANDIDATE; 24672974e916SYuri Benditovich } 24682974e916SYuri Benditovich 24692974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 24702974e916SYuri Benditovich NetClientState *nc, 24712974e916SYuri Benditovich const uint8_t *buf, size_t size) 24722974e916SYuri Benditovich { 24732974e916SYuri Benditovich int32_t ret; 24742974e916SYuri Benditovich uint16_t hdr_len; 24752974e916SYuri Benditovich VirtioNetRscUnit unit; 24762974e916SYuri Benditovich 24772974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 24782974e916SYuri Benditovich 24792974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 24802974e916SYuri Benditovich + sizeof(struct tcp_header))) { 24812974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24822974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24832974e916SYuri Benditovich } 24842974e916SYuri Benditovich 24852974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 24862974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 24872974e916SYuri Benditovich != RSC_CANDIDATE) { 24882974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24892974e916SYuri Benditovich } 24902974e916SYuri Benditovich 24912974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24922974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24932974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24942974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24952974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24962974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 24972974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 24982974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 24992974e916SYuri Benditovich } 25002974e916SYuri Benditovich 25012974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 25022974e916SYuri Benditovich } 25032974e916SYuri Benditovich 25042974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 25052974e916SYuri Benditovich struct ip6_header *ip6, 25062974e916SYuri Benditovich const uint8_t *buf, size_t size) 25072974e916SYuri Benditovich { 25082974e916SYuri Benditovich uint16_t ip_len; 25092974e916SYuri Benditovich 25102974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 25112974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 25122974e916SYuri Benditovich return RSC_BYPASS; 25132974e916SYuri Benditovich } 25142974e916SYuri Benditovich 25152974e916SYuri Benditovich /* Both option and protocol is checked in this */ 25162974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 25172974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 25182974e916SYuri Benditovich return RSC_BYPASS; 25192974e916SYuri Benditovich } 25202974e916SYuri Benditovich 25212974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 25222974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 25232974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 25242974e916SYuri Benditovich - sizeof(struct ip6_header))) { 25252974e916SYuri Benditovich chain->stat.ip_hacked++; 25262974e916SYuri Benditovich return RSC_BYPASS; 25272974e916SYuri Benditovich } 25282974e916SYuri Benditovich 25292974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 25302974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 25312974e916SYuri Benditovich chain->stat.ip_ecn++; 25322974e916SYuri Benditovich return RSC_BYPASS; 25332974e916SYuri Benditovich } 25342974e916SYuri Benditovich 25352974e916SYuri Benditovich return RSC_CANDIDATE; 25362974e916SYuri Benditovich } 25372974e916SYuri Benditovich 25382974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 25392974e916SYuri Benditovich const uint8_t *buf, size_t size) 25402974e916SYuri Benditovich { 25412974e916SYuri Benditovich int32_t ret; 25422974e916SYuri Benditovich uint16_t hdr_len; 25432974e916SYuri Benditovich VirtioNetRscChain *chain; 25442974e916SYuri Benditovich VirtioNetRscUnit unit; 25452974e916SYuri Benditovich 25463d558330SMarkus Armbruster chain = opq; 25472974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 25482974e916SYuri Benditovich 25492974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 25502974e916SYuri Benditovich + sizeof(tcp_header))) { 25512974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25522974e916SYuri Benditovich } 25532974e916SYuri Benditovich 25542974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 25552974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 25562974e916SYuri Benditovich unit.ip, buf, size)) { 25572974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25582974e916SYuri Benditovich } 25592974e916SYuri Benditovich 25602974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 25612974e916SYuri Benditovich if (ret == RSC_BYPASS) { 25622974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25632974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 25642974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 25652974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 25662974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 25672974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 25682974e916SYuri Benditovich + sizeof(struct ip6_header)); 25692974e916SYuri Benditovich } 25702974e916SYuri Benditovich 25712974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 25722974e916SYuri Benditovich } 25732974e916SYuri Benditovich 25742974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 25752974e916SYuri Benditovich NetClientState *nc, 25762974e916SYuri Benditovich uint16_t proto) 25772974e916SYuri Benditovich { 25782974e916SYuri Benditovich VirtioNetRscChain *chain; 25792974e916SYuri Benditovich 25802974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 25812974e916SYuri Benditovich return NULL; 25822974e916SYuri Benditovich } 25832974e916SYuri Benditovich 25842974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 25852974e916SYuri Benditovich if (chain->proto == proto) { 25862974e916SYuri Benditovich return chain; 25872974e916SYuri Benditovich } 25882974e916SYuri Benditovich } 25892974e916SYuri Benditovich 25902974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 25912974e916SYuri Benditovich chain->n = n; 25922974e916SYuri Benditovich chain->proto = proto; 25932974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 25942974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 25952974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 25962974e916SYuri Benditovich } else { 25972974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 25982974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 25992974e916SYuri Benditovich } 26002974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 26012974e916SYuri Benditovich virtio_net_rsc_purge, chain); 26022974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 26032974e916SYuri Benditovich 26042974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 26052974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 26062974e916SYuri Benditovich 26072974e916SYuri Benditovich return chain; 26082974e916SYuri Benditovich } 26092974e916SYuri Benditovich 26102974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 26112974e916SYuri Benditovich const uint8_t *buf, 26122974e916SYuri Benditovich size_t size) 26132974e916SYuri Benditovich { 26142974e916SYuri Benditovich uint16_t proto; 26152974e916SYuri Benditovich VirtioNetRscChain *chain; 26162974e916SYuri Benditovich struct eth_header *eth; 26172974e916SYuri Benditovich VirtIONet *n; 26182974e916SYuri Benditovich 26192974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 26202974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 26212974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26222974e916SYuri Benditovich } 26232974e916SYuri Benditovich 26242974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 26252974e916SYuri Benditovich proto = htons(eth->h_proto); 26262974e916SYuri Benditovich 26272974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 26282974e916SYuri Benditovich if (chain) { 26292974e916SYuri Benditovich chain->stat.received++; 26302974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 26312974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 26322974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 26332974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 26342974e916SYuri Benditovich } 26352974e916SYuri Benditovich } 26362974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26372974e916SYuri Benditovich } 26382974e916SYuri Benditovich 26392974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 26402974e916SYuri Benditovich size_t size) 26412974e916SYuri Benditovich { 26422974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 26432974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 26442974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 26452974e916SYuri Benditovich } else { 26462974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26472974e916SYuri Benditovich } 26482974e916SYuri Benditovich } 26492974e916SYuri Benditovich 26506e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 26516e790746SPaolo Bonzini 26526e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 26536e790746SPaolo Bonzini { 26546e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 26556e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 265617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2657df8d0708SLaurent Vivier int ret; 26586e790746SPaolo Bonzini 265951b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 266017a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 26616e790746SPaolo Bonzini 266251b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 266351b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 26646e790746SPaolo Bonzini 26656e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2666df8d0708SLaurent Vivier ret = virtio_net_flush_tx(q); 26677550a822SLaurent Vivier if (ret >= n->tx_burst) { 2668df8d0708SLaurent Vivier /* 2669df8d0708SLaurent Vivier * the flush has been stopped by tx_burst 2670df8d0708SLaurent Vivier * we will not receive notification for the 2671df8d0708SLaurent Vivier * remainining part, so re-schedule 2672df8d0708SLaurent Vivier */ 2673df8d0708SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 26747550a822SLaurent Vivier if (q->tx_bh) { 2675df8d0708SLaurent Vivier qemu_bh_schedule(q->tx_bh); 26767550a822SLaurent Vivier } else { 26777550a822SLaurent Vivier timer_mod(q->tx_timer, 26787550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 26797550a822SLaurent Vivier } 2680df8d0708SLaurent Vivier q->tx_waiting = 1; 2681df8d0708SLaurent Vivier } 26826e790746SPaolo Bonzini } 26836e790746SPaolo Bonzini 26846e790746SPaolo Bonzini /* TX */ 26856e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 26866e790746SPaolo Bonzini { 26876e790746SPaolo Bonzini VirtIONet *n = q->n; 268817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 268951b19ebeSPaolo Bonzini VirtQueueElement *elem; 26906e790746SPaolo Bonzini int32_t num_packets = 0; 26916e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 269217a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 26936e790746SPaolo Bonzini return num_packets; 26946e790746SPaolo Bonzini } 26956e790746SPaolo Bonzini 269651b19ebeSPaolo Bonzini if (q->async_tx.elem) { 26976e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26986e790746SPaolo Bonzini return num_packets; 26996e790746SPaolo Bonzini } 27006e790746SPaolo Bonzini 270151b19ebeSPaolo Bonzini for (;;) { 2702bd89dd98SJason Wang ssize_t ret; 270351b19ebeSPaolo Bonzini unsigned int out_num; 270451b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2705942f420eSAkihiko Odaki struct virtio_net_hdr vhdr; 27066e790746SPaolo Bonzini 270751b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 270851b19ebeSPaolo Bonzini if (!elem) { 270951b19ebeSPaolo Bonzini break; 271051b19ebeSPaolo Bonzini } 271151b19ebeSPaolo Bonzini 271251b19ebeSPaolo Bonzini out_num = elem->out_num; 271351b19ebeSPaolo Bonzini out_sg = elem->out_sg; 27146e790746SPaolo Bonzini if (out_num < 1) { 2715fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 27162c3e4e2dSAlexey Dobriyan goto detach; 27176e790746SPaolo Bonzini } 27186e790746SPaolo Bonzini 2719ad57f700SAkihiko Odaki if (n->needs_vnet_hdr_swap) { 2720942f420eSAkihiko Odaki if (iov_to_buf(out_sg, out_num, 0, &vhdr, sizeof(vhdr)) < 2721942f420eSAkihiko Odaki sizeof(vhdr)) { 2722fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 27232c3e4e2dSAlexey Dobriyan goto detach; 2724032a74a1SCédric Le Goater } 2725942f420eSAkihiko Odaki virtio_net_hdr_swap(vdev, &vhdr); 27262220e818SJason Wang sg2[0].iov_base = &vhdr; 2727942f420eSAkihiko Odaki sg2[0].iov_len = sizeof(vhdr); 2728ad57f700SAkihiko Odaki out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, out_sg, out_num, 2729942f420eSAkihiko Odaki sizeof(vhdr), -1); 2730feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2731feb93f36SJason Wang goto drop; 2732032a74a1SCédric Le Goater } 2733feb93f36SJason Wang out_num += 1; 2734feb93f36SJason Wang out_sg = sg2; 2735feb93f36SJason Wang } 27366e790746SPaolo Bonzini /* 27376e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 27386e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 27396e790746SPaolo Bonzini * that host is interested in. 27406e790746SPaolo Bonzini */ 27416e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 27426e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 2743d4f471ebSDmitry Frolov if (iov_size(out_sg, out_num) < n->guest_hdr_len) { 2744d4f471ebSDmitry Frolov virtio_error(vdev, "virtio-net header is invalid"); 2745d4f471ebSDmitry Frolov goto detach; 2746d4f471ebSDmitry Frolov } 27476e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 27486e790746SPaolo Bonzini out_sg, out_num, 27496e790746SPaolo Bonzini 0, n->host_hdr_len); 27506e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 27516e790746SPaolo Bonzini out_sg, out_num, 27526e790746SPaolo Bonzini n->guest_hdr_len, -1); 27536e790746SPaolo Bonzini out_num = sg_num; 27546e790746SPaolo Bonzini out_sg = sg; 27552c3e4e2dSAlexey Dobriyan 27562c3e4e2dSAlexey Dobriyan if (out_num < 1) { 27572c3e4e2dSAlexey Dobriyan virtio_error(vdev, "virtio-net nothing to send"); 27582c3e4e2dSAlexey Dobriyan goto detach; 27592c3e4e2dSAlexey Dobriyan } 27606e790746SPaolo Bonzini } 27616e790746SPaolo Bonzini 27626e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 27636e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 27646e790746SPaolo Bonzini if (ret == 0) { 27656e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 27666e790746SPaolo Bonzini q->async_tx.elem = elem; 27676e790746SPaolo Bonzini return -EBUSY; 27686e790746SPaolo Bonzini } 27696e790746SPaolo Bonzini 2770feb93f36SJason Wang drop: 277151b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 277217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 277351b19ebeSPaolo Bonzini g_free(elem); 27746e790746SPaolo Bonzini 27756e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 27766e790746SPaolo Bonzini break; 27776e790746SPaolo Bonzini } 27786e790746SPaolo Bonzini } 27796e790746SPaolo Bonzini return num_packets; 27802c3e4e2dSAlexey Dobriyan 27812c3e4e2dSAlexey Dobriyan detach: 27822c3e4e2dSAlexey Dobriyan virtqueue_detach_element(q->tx_vq, elem, 0); 27832c3e4e2dSAlexey Dobriyan g_free(elem); 27842c3e4e2dSAlexey Dobriyan return -EINVAL; 27856e790746SPaolo Bonzini } 27866e790746SPaolo Bonzini 27877550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque); 27887550a822SLaurent Vivier 27896e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 27906e790746SPaolo Bonzini { 279117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 27926e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 27936e790746SPaolo Bonzini 2794283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2795283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2796283e2c2aSYuri Benditovich return; 2797283e2c2aSYuri Benditovich } 2798283e2c2aSYuri Benditovich 27996e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 280017a0ca55SKONRAD Frederic if (!vdev->vm_running) { 28016e790746SPaolo Bonzini q->tx_waiting = 1; 28026e790746SPaolo Bonzini return; 28036e790746SPaolo Bonzini } 28046e790746SPaolo Bonzini 28056e790746SPaolo Bonzini if (q->tx_waiting) { 28067550a822SLaurent Vivier /* We already have queued packets, immediately flush */ 2807bc72ad67SAlex Bligh timer_del(q->tx_timer); 28087550a822SLaurent Vivier virtio_net_tx_timer(q); 28096e790746SPaolo Bonzini } else { 28107550a822SLaurent Vivier /* re-arm timer to flush it (and more) on next tick */ 2811bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2812bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 28136e790746SPaolo Bonzini q->tx_waiting = 1; 28146e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 28156e790746SPaolo Bonzini } 28166e790746SPaolo Bonzini } 28176e790746SPaolo Bonzini 28186e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 28196e790746SPaolo Bonzini { 282017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 28216e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 28226e790746SPaolo Bonzini 28234c54f5bcSYajun Wu if (unlikely(n->vhost_started)) { 28244c54f5bcSYajun Wu return; 28254c54f5bcSYajun Wu } 28264c54f5bcSYajun Wu 2827283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2828283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2829283e2c2aSYuri Benditovich return; 2830283e2c2aSYuri Benditovich } 2831283e2c2aSYuri Benditovich 28326e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 28336e790746SPaolo Bonzini return; 28346e790746SPaolo Bonzini } 28356e790746SPaolo Bonzini q->tx_waiting = 1; 28366e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 283717a0ca55SKONRAD Frederic if (!vdev->vm_running) { 28386e790746SPaolo Bonzini return; 28396e790746SPaolo Bonzini } 28406e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 28416e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28426e790746SPaolo Bonzini } 28436e790746SPaolo Bonzini 28446e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 28456e790746SPaolo Bonzini { 28466e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 28476e790746SPaolo Bonzini VirtIONet *n = q->n; 284817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 28497550a822SLaurent Vivier int ret; 28507550a822SLaurent Vivier 2851e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2852e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2853e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2854e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2855e8bcf842SMichael S. Tsirkin return; 2856e8bcf842SMichael S. Tsirkin } 28576e790746SPaolo Bonzini 28586e790746SPaolo Bonzini q->tx_waiting = 0; 28596e790746SPaolo Bonzini 28606e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 286117a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 28626e790746SPaolo Bonzini return; 286317a0ca55SKONRAD Frederic } 28646e790746SPaolo Bonzini 28657550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 28667550a822SLaurent Vivier if (ret == -EBUSY || ret == -EINVAL) { 28677550a822SLaurent Vivier return; 28687550a822SLaurent Vivier } 28697550a822SLaurent Vivier /* 28707550a822SLaurent Vivier * If we flush a full burst of packets, assume there are 28717550a822SLaurent Vivier * more coming and immediately rearm 28727550a822SLaurent Vivier */ 28737550a822SLaurent Vivier if (ret >= n->tx_burst) { 28747550a822SLaurent Vivier q->tx_waiting = 1; 28757550a822SLaurent Vivier timer_mod(q->tx_timer, 28767550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 28777550a822SLaurent Vivier return; 28787550a822SLaurent Vivier } 28797550a822SLaurent Vivier /* 28807550a822SLaurent Vivier * If less than a full burst, re-enable notification and flush 28817550a822SLaurent Vivier * anything that may have come in while we weren't looking. If 28827550a822SLaurent Vivier * we find something, assume the guest is still active and rearm 28837550a822SLaurent Vivier */ 28846e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 28857550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 28867550a822SLaurent Vivier if (ret > 0) { 28877550a822SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 28887550a822SLaurent Vivier q->tx_waiting = 1; 28897550a822SLaurent Vivier timer_mod(q->tx_timer, 28907550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 28917550a822SLaurent Vivier } 28926e790746SPaolo Bonzini } 28936e790746SPaolo Bonzini 28946e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 28956e790746SPaolo Bonzini { 28966e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 28976e790746SPaolo Bonzini VirtIONet *n = q->n; 289817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 28996e790746SPaolo Bonzini int32_t ret; 29006e790746SPaolo Bonzini 2901e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2902e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2903e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2904e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2905e8bcf842SMichael S. Tsirkin return; 2906e8bcf842SMichael S. Tsirkin } 29076e790746SPaolo Bonzini 29086e790746SPaolo Bonzini q->tx_waiting = 0; 29096e790746SPaolo Bonzini 29106e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 291117a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 29126e790746SPaolo Bonzini return; 291317a0ca55SKONRAD Frederic } 29146e790746SPaolo Bonzini 29156e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2916fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2917fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2918fa5e56c2SGreg Kurz * broken */ 29196e790746SPaolo Bonzini } 29206e790746SPaolo Bonzini 29216e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 29226e790746SPaolo Bonzini * more coming and immediately reschedule */ 29236e790746SPaolo Bonzini if (ret >= n->tx_burst) { 29246e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 29256e790746SPaolo Bonzini q->tx_waiting = 1; 29266e790746SPaolo Bonzini return; 29276e790746SPaolo Bonzini } 29286e790746SPaolo Bonzini 29296e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 29306e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 29316e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 29326e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2933fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2934fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2935fa5e56c2SGreg Kurz return; 2936fa5e56c2SGreg Kurz } else if (ret > 0) { 29376e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 29386e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 29396e790746SPaolo Bonzini q->tx_waiting = 1; 29406e790746SPaolo Bonzini } 29416e790746SPaolo Bonzini } 29426e790746SPaolo Bonzini 2943f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2944f9d6dbf0SWen Congyang { 2945f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2946f9d6dbf0SWen Congyang 29471c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 29481c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 29499b02e161SWei Wang 2950f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2951f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 29529b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 29539b02e161SWei Wang virtio_net_handle_tx_timer); 2954f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2955f9d6dbf0SWen Congyang virtio_net_tx_timer, 2956f9d6dbf0SWen Congyang &n->vqs[index]); 2957f9d6dbf0SWen Congyang } else { 2958f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 29599b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 29609b02e161SWei Wang virtio_net_handle_tx_bh); 2961f63192b0SAlexander Bulekov n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index], 2962f63192b0SAlexander Bulekov &DEVICE(vdev)->mem_reentrancy_guard); 2963f9d6dbf0SWen Congyang } 2964f9d6dbf0SWen Congyang 2965f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2966f9d6dbf0SWen Congyang n->vqs[index].n = n; 2967f9d6dbf0SWen Congyang } 2968f9d6dbf0SWen Congyang 2969f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2970f9d6dbf0SWen Congyang { 2971f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2972f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2973f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2974f9d6dbf0SWen Congyang 2975f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2976f9d6dbf0SWen Congyang 2977f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2978f9d6dbf0SWen Congyang if (q->tx_timer) { 2979f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2980f989c30cSYunjian Wang q->tx_timer = NULL; 2981f9d6dbf0SWen Congyang } else { 2982f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2983f989c30cSYunjian Wang q->tx_bh = NULL; 2984f9d6dbf0SWen Congyang } 2985f989c30cSYunjian Wang q->tx_waiting = 0; 2986f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2987f9d6dbf0SWen Congyang } 2988f9d6dbf0SWen Congyang 2989441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 2990f9d6dbf0SWen Congyang { 2991f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2992f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2993441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 2994f9d6dbf0SWen Congyang int i; 2995f9d6dbf0SWen Congyang 2996f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2997f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2998f9d6dbf0SWen Congyang 2999f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 3000f9d6dbf0SWen Congyang return; 3001f9d6dbf0SWen Congyang } 3002f9d6dbf0SWen Congyang 3003f9d6dbf0SWen Congyang /* 3004f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 3005f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 300620f86a75SYuval Shaia * and then we only enter one of the following two loops. 3007f9d6dbf0SWen Congyang */ 3008f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 3009f9d6dbf0SWen Congyang 3010f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 3011f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 3012f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 3013f9d6dbf0SWen Congyang } 3014f9d6dbf0SWen Congyang 3015f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 3016f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 3017f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 3018f9d6dbf0SWen Congyang } 3019f9d6dbf0SWen Congyang 3020f9d6dbf0SWen Congyang /* add ctrl_vq last */ 3021f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 3022f9d6dbf0SWen Congyang } 3023f9d6dbf0SWen Congyang 3024ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 30256e790746SPaolo Bonzini { 3026441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 3027f9d6dbf0SWen Congyang 30286e790746SPaolo Bonzini n->multiqueue = multiqueue; 3029441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 30306e790746SPaolo Bonzini 3031441537f1SJason Wang virtio_net_set_queue_pairs(n); 30326e790746SPaolo Bonzini } 30336e790746SPaolo Bonzini 3034982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 3035037dab2fSGreg Kurz { 3036982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 3037982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 3038037dab2fSGreg Kurz int i, link_down; 3039037dab2fSGreg Kurz 30409d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 3041982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 304295129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 3043e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 3044e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 3045e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 30466e790746SPaolo Bonzini 30476e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 3048982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 30496e790746SPaolo Bonzini n->mac_table.in_use = 0; 30506e790746SPaolo Bonzini } 30516e790746SPaolo Bonzini 3052982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 30536c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 30546c666823SMichael S. Tsirkin } 30556c666823SMichael S. Tsirkin 30567788c3f2SMikhail Sennikovsky /* 30577788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 30587788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 30597788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 30607788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 30617788c3f2SMikhail Sennikovsky */ 30627788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 30636c666823SMichael S. Tsirkin 3064441537f1SJason Wang virtio_net_set_queue_pairs(n); 30656e790746SPaolo Bonzini 30666e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 30676e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 30686e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 30696e790746SPaolo Bonzini break; 30706e790746SPaolo Bonzini } 30716e790746SPaolo Bonzini } 30726e790746SPaolo Bonzini n->mac_table.first_multi = i; 30736e790746SPaolo Bonzini 30746e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 30756e790746SPaolo Bonzini * to link status bit in n->status */ 30766e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 3077441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 30786e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 30796e790746SPaolo Bonzini } 30806e790746SPaolo Bonzini 30816c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 30826c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 30839d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 30849d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 30859d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 30869d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 30879d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 30889d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 30899d8c6a25SDr. David Alan Gilbert } else { 3090944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 30919d8c6a25SDr. David Alan Gilbert } 30926c666823SMichael S. Tsirkin } 30936c666823SMichael S. Tsirkin 30940e07198eSAkihiko Odaki virtio_net_commit_rss_config(n); 30956e790746SPaolo Bonzini return 0; 30966e790746SPaolo Bonzini } 30976e790746SPaolo Bonzini 30987788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 30997788c3f2SMikhail Sennikovsky { 31007788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 31017788c3f2SMikhail Sennikovsky /* 31027788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 31037788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 31047788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 31057788c3f2SMikhail Sennikovsky */ 31067788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 31077788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 31087788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 31097788c3f2SMikhail Sennikovsky } 31107788c3f2SMikhail Sennikovsky 31117788c3f2SMikhail Sennikovsky return 0; 31127788c3f2SMikhail Sennikovsky } 31137788c3f2SMikhail Sennikovsky 3114982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 3115982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 3116982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 31171de81b42SRichard Henderson .fields = (const VMStateField[]) { 3118982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 3119982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3120982b78c5SDr. David Alan Gilbert }, 3121982b78c5SDr. David Alan Gilbert }; 3122982b78c5SDr. David Alan Gilbert 3123441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 3124982b78c5SDr. David Alan Gilbert { 3125441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 3126982b78c5SDr. David Alan Gilbert } 3127982b78c5SDr. David Alan Gilbert 3128982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 3129982b78c5SDr. David Alan Gilbert { 3130982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 3131982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 3132982b78c5SDr. David Alan Gilbert } 3133982b78c5SDr. David Alan Gilbert 3134982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 3135982b78c5SDr. David Alan Gilbert { 3136982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 3137982b78c5SDr. David Alan Gilbert } 3138982b78c5SDr. David Alan Gilbert 3139982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 3140982b78c5SDr. David Alan Gilbert { 3141982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 3142982b78c5SDr. David Alan Gilbert } 3143982b78c5SDr. David Alan Gilbert 3144982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 3145982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 3146982b78c5SDr. David Alan Gilbert */ 3147982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 3148982b78c5SDr. David Alan Gilbert VirtIONet *parent; 3149982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 3150441537f1SJason Wang uint16_t curr_queue_pairs_1; 3151982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 3152982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 3153982b78c5SDr. David Alan Gilbert }; 3154982b78c5SDr. David Alan Gilbert 3155982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 3156441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 3157982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 3158982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 3159982b78c5SDr. David Alan Gilbert */ 3160982b78c5SDr. David Alan Gilbert 316144b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 3162982b78c5SDr. David Alan Gilbert { 3163982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3164982b78c5SDr. David Alan Gilbert 3165982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 3166441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 3167441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 3168441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 3169982b78c5SDr. David Alan Gilbert } 317044b1ff31SDr. David Alan Gilbert 317144b1ff31SDr. David Alan Gilbert return 0; 3172982b78c5SDr. David Alan Gilbert } 3173982b78c5SDr. David Alan Gilbert 3174982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 3175982b78c5SDr. David Alan Gilbert { 3176982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3177982b78c5SDr. David Alan Gilbert 3178982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 3179982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 3180982b78c5SDr. David Alan Gilbert 3181441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 3182441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 3183441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 3184982b78c5SDr. David Alan Gilbert 3185982b78c5SDr. David Alan Gilbert return -EINVAL; 3186982b78c5SDr. David Alan Gilbert } 3187982b78c5SDr. David Alan Gilbert 3188982b78c5SDr. David Alan Gilbert return 0; /* all good */ 3189982b78c5SDr. David Alan Gilbert } 3190982b78c5SDr. David Alan Gilbert 3191982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 3192982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 3193982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 3194982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 31951de81b42SRichard Henderson .fields = (const VMStateField[]) { 3196982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 3197441537f1SJason Wang curr_queue_pairs_1, 3198982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3199982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3200982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3201982b78c5SDr. David Alan Gilbert }, 3202982b78c5SDr. David Alan Gilbert }; 3203982b78c5SDr. David Alan Gilbert 3204982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3205982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3206982b78c5SDr. David Alan Gilbert */ 3207982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3208982b78c5SDr. David Alan Gilbert { 3209982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3210982b78c5SDr. David Alan Gilbert 3211982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3212982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3213982b78c5SDr. David Alan Gilbert return -EINVAL; 3214982b78c5SDr. David Alan Gilbert } 3215982b78c5SDr. David Alan Gilbert 3216982b78c5SDr. David Alan Gilbert return 0; 3217982b78c5SDr. David Alan Gilbert } 3218982b78c5SDr. David Alan Gilbert 321944b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3220982b78c5SDr. David Alan Gilbert { 3221982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3222982b78c5SDr. David Alan Gilbert 3223982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 322444b1ff31SDr. David Alan Gilbert 322544b1ff31SDr. David Alan Gilbert return 0; 3226982b78c5SDr. David Alan Gilbert } 3227982b78c5SDr. David Alan Gilbert 3228982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3229982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3230982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3231982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 32321de81b42SRichard Henderson .fields = (const VMStateField[]) { 3233982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3234982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3235982b78c5SDr. David Alan Gilbert }, 3236982b78c5SDr. David Alan Gilbert }; 3237982b78c5SDr. David Alan Gilbert 3238982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3239982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3240982b78c5SDr. David Alan Gilbert */ 3241982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3242982b78c5SDr. David Alan Gilbert { 3243982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3244982b78c5SDr. David Alan Gilbert 3245982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3246982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3247982b78c5SDr. David Alan Gilbert return -EINVAL; 3248982b78c5SDr. David Alan Gilbert } 3249982b78c5SDr. David Alan Gilbert 3250982b78c5SDr. David Alan Gilbert return 0; 3251982b78c5SDr. David Alan Gilbert } 3252982b78c5SDr. David Alan Gilbert 325344b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3254982b78c5SDr. David Alan Gilbert { 3255982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3256982b78c5SDr. David Alan Gilbert 3257982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 325844b1ff31SDr. David Alan Gilbert 325944b1ff31SDr. David Alan Gilbert return 0; 3260982b78c5SDr. David Alan Gilbert } 3261982b78c5SDr. David Alan Gilbert 3262982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3263982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3264982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3265982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 32661de81b42SRichard Henderson .fields = (const VMStateField[]) { 3267982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3268982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3269982b78c5SDr. David Alan Gilbert }, 3270982b78c5SDr. David Alan Gilbert }; 3271982b78c5SDr. David Alan Gilbert 3272e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3273e41b7114SYuri Benditovich { 3274e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3275e41b7114SYuri Benditovich } 3276e41b7114SYuri Benditovich 3277e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3278e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3279e41b7114SYuri Benditovich .version_id = 1, 3280e41b7114SYuri Benditovich .minimum_version_id = 1, 3281e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 32821de81b42SRichard Henderson .fields = (const VMStateField[]) { 3283e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3284e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3285e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3286e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3287e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3288e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3289e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3290e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3291e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3292e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3293e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3294e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3295e41b7114SYuri Benditovich }, 3296e41b7114SYuri Benditovich }; 3297e41b7114SYuri Benditovich 3298982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3299982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3300982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3301982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3302982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 33031de81b42SRichard Henderson .fields = (const VMStateField[]) { 3304982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3305982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3306982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3307982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3308982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3309982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3310982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3311982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3312982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3313982b78c5SDr. David Alan Gilbert 3314982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3315982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3316982b78c5SDr. David Alan Gilbert * sets flags in this case. 3317982b78c5SDr. David Alan Gilbert */ 3318982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3319982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3320982b78c5SDr. David Alan Gilbert ETH_ALEN), 3321982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3322982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3323982b78c5SDr. David Alan Gilbert 3324982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3325982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3326982b78c5SDr. David Alan Gilbert * but based on the uint. 3327982b78c5SDr. David Alan Gilbert */ 3328982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3329982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3330982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3331982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3332982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3333982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3334982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3335982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3336982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3337982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3338982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3339441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3340982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3341441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3342982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3343982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3344982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3345982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3346982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3347982b78c5SDr. David Alan Gilbert }, 33481de81b42SRichard Henderson .subsections = (const VMStateDescription * const []) { 3349e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3350e41b7114SYuri Benditovich NULL 3351e41b7114SYuri Benditovich } 3352982b78c5SDr. David Alan Gilbert }; 3353982b78c5SDr. David Alan Gilbert 33546e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3355f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 33566e790746SPaolo Bonzini .size = sizeof(NICState), 33576e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 33586e790746SPaolo Bonzini .receive = virtio_net_receive, 33596e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3360b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3361b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 33626e790746SPaolo Bonzini }; 33636e790746SPaolo Bonzini 33646e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 33656e790746SPaolo Bonzini { 336617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 336768b0a639SSi-Wei Liu NetClientState *nc; 33686e790746SPaolo Bonzini assert(n->vhost_started); 33691c188fc8SAkihiko Odaki if (!n->multiqueue && idx == 2) { 337068b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 337168b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 337268b0a639SSi-Wei Liu * buggy migration stream. 337368b0a639SSi-Wei Liu */ 337468b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 337568b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 337668b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 337768b0a639SSi-Wei Liu return false; 337868b0a639SSi-Wei Liu } 337968b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 338068b0a639SSi-Wei Liu } else { 338168b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 338268b0a639SSi-Wei Liu } 3383544f0278SCindy Lu /* 3384544f0278SCindy Lu * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1 33857e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not 3386544f0278SCindy Lu * support, the function will return false 3387544f0278SCindy Lu */ 3388544f0278SCindy Lu 3389544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) { 33908aab0d1dSCindy Lu return vhost_net_config_pending(get_vhost_net(nc->peer)); 3391544f0278SCindy Lu } 3392ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 33936e790746SPaolo Bonzini } 33946e790746SPaolo Bonzini 33956e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 33966e790746SPaolo Bonzini bool mask) 33976e790746SPaolo Bonzini { 339817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 339968b0a639SSi-Wei Liu NetClientState *nc; 34006e790746SPaolo Bonzini assert(n->vhost_started); 34011c188fc8SAkihiko Odaki if (!n->multiqueue && idx == 2) { 340268b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 340368b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 340468b0a639SSi-Wei Liu * buggy migration stream. 340568b0a639SSi-Wei Liu */ 340668b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 340768b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 340868b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 340968b0a639SSi-Wei Liu return; 341068b0a639SSi-Wei Liu } 341168b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 341268b0a639SSi-Wei Liu } else { 341368b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 341468b0a639SSi-Wei Liu } 3415544f0278SCindy Lu /* 3416544f0278SCindy Lu *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1 34177e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not 3418544f0278SCindy Lu * support, the function will return 3419544f0278SCindy Lu */ 3420544f0278SCindy Lu 3421544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) { 34228aab0d1dSCindy Lu vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask); 3423544f0278SCindy Lu return; 3424544f0278SCindy Lu } 3425544f0278SCindy Lu vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask); 34266e790746SPaolo Bonzini } 34276e790746SPaolo Bonzini 3428019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 34296e790746SPaolo Bonzini { 34300cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3431a93e599dSMaxime Coquelin 3432d74c30c8SDaniil Tatianin n->config_size = virtio_get_config_size(&cfg_size_params, host_features); 343317ec5a86SKONRAD Frederic } 34346e790746SPaolo Bonzini 34358a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 34368a253ec2SKONRAD Frederic const char *type) 34378a253ec2SKONRAD Frederic { 34388a253ec2SKONRAD Frederic /* 34398a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 34408a253ec2SKONRAD Frederic */ 34418a253ec2SKONRAD Frederic assert(type != NULL); 34428a253ec2SKONRAD Frederic 34438a253ec2SKONRAD Frederic g_free(n->netclient_name); 34448a253ec2SKONRAD Frederic g_free(n->netclient_type); 34458a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 34468a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 34478a253ec2SKONRAD Frederic } 34488a253ec2SKONRAD Frederic 34490e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 34509711cd0dSJens Freimann { 34519711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 34529711cd0dSJens Freimann PCIDevice *pci_dev; 34539711cd0dSJens Freimann Error *err = NULL; 34549711cd0dSJens Freimann 34550e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 34569711cd0dSJens Freimann if (hotplug_ctrl) { 34570e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 34589711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 34590e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 34609711cd0dSJens Freimann if (err) { 34619711cd0dSJens Freimann error_report_err(err); 34629711cd0dSJens Freimann return false; 34639711cd0dSJens Freimann } 34649711cd0dSJens Freimann } else { 34659711cd0dSJens Freimann return false; 34669711cd0dSJens Freimann } 34679711cd0dSJens Freimann return true; 34689711cd0dSJens Freimann } 34699711cd0dSJens Freimann 34700e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 34710e9a65c5SJuan Quintela Error **errp) 34729711cd0dSJens Freimann { 34735a0948d3SMarkus Armbruster Error *err = NULL; 34749711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 34750e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 347678274682SJuan Quintela BusState *primary_bus; 34779711cd0dSJens Freimann 34789711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 34799711cd0dSJens Freimann return true; 34809711cd0dSJens Freimann } 34810e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 348278274682SJuan Quintela if (!primary_bus) { 3483150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 34845a0948d3SMarkus Armbruster return false; 34859711cd0dSJens Freimann } 34860e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3487e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 34880e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 34899711cd0dSJens Freimann if (hotplug_ctrl) { 34900e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 34915a0948d3SMarkus Armbruster if (err) { 34925a0948d3SMarkus Armbruster goto out; 34935a0948d3SMarkus Armbruster } 34940e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 34959711cd0dSJens Freimann } 3496109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3497150ab54aSJens Freimann 3498150ab54aSJens Freimann out: 34995a0948d3SMarkus Armbruster error_propagate(errp, err); 35005a0948d3SMarkus Armbruster return !err; 35019711cd0dSJens Freimann } 35029711cd0dSJens Freimann 35039d9babf7SSteve Sistare static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationEvent *e) 35049711cd0dSJens Freimann { 35059711cd0dSJens Freimann bool should_be_hidden; 35069711cd0dSJens Freimann Error *err = NULL; 350707a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 350807a5d816SJuan Quintela 350907a5d816SJuan Quintela if (!dev) { 351007a5d816SJuan Quintela return; 351107a5d816SJuan Quintela } 35129711cd0dSJens Freimann 3513e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 35149711cd0dSJens Freimann 35159d9babf7SSteve Sistare if (e->type == MIG_EVENT_PRECOPY_SETUP && !should_be_hidden) { 351607a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 351707a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 351807a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3519e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 35209711cd0dSJens Freimann } else { 35219711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 35229711cd0dSJens Freimann } 35239d9babf7SSteve Sistare } else if (e->type == MIG_EVENT_PRECOPY_FAILED) { 3524150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 352507a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 35269711cd0dSJens Freimann if (err) { 35279711cd0dSJens Freimann error_report_err(err); 35289711cd0dSJens Freimann } 35299711cd0dSJens Freimann } 35309711cd0dSJens Freimann } 35319711cd0dSJens Freimann } 35329711cd0dSJens Freimann 35333e775730SSteve Sistare static int virtio_net_migration_state_notifier(NotifierWithReturn *notifier, 35345663dd3fSSteve Sistare MigrationEvent *e, Error **errp) 35359711cd0dSJens Freimann { 35369711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 35379d9babf7SSteve Sistare virtio_net_handle_migration_primary(n, e); 35383e775730SSteve Sistare return 0; 35399711cd0dSJens Freimann } 35409711cd0dSJens Freimann 3541b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3542f3558b1bSKevin Wolf const QDict *device_opts, 3543f3558b1bSKevin Wolf bool from_json, 3544f3558b1bSKevin Wolf Error **errp) 35459711cd0dSJens Freimann { 35469711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 35474f0303aeSJuan Quintela const char *standby_id; 35489711cd0dSJens Freimann 35494d0e59acSJens Freimann if (!device_opts) { 355089631fedSJuan Quintela return false; 35514d0e59acSJens Freimann } 3552bcfc906bSLaurent Vivier 3553bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3554bcfc906bSLaurent Vivier return false; 3555bcfc906bSLaurent Vivier } 3556bcfc906bSLaurent Vivier 3557bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3558bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3559bcfc906bSLaurent Vivier return false; 3560bcfc906bSLaurent Vivier } 3561bcfc906bSLaurent Vivier 3562bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 356389631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 356489631fedSJuan Quintela return false; 35659711cd0dSJens Freimann } 35669711cd0dSJens Freimann 35677fe7791eSLaurent Vivier /* 35687fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 35697fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 35707fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 35717fe7791eSLaurent Vivier * device. 35727fe7791eSLaurent Vivier */ 3573259a10dbSKevin Wolf if (n->primary_opts) { 35747fe7791eSLaurent Vivier const char *old, *new; 35757fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 35767fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 35777fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 35787fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 35797fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 35807fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3581259a10dbSKevin Wolf return false; 3582259a10dbSKevin Wolf } 35837fe7791eSLaurent Vivier } else { 3584f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3585f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 35867fe7791eSLaurent Vivier } 3587259a10dbSKevin Wolf 3588e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 35893abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 35909711cd0dSJens Freimann } 35919711cd0dSJens Freimann 3592e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 359317ec5a86SKONRAD Frederic { 3594e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3595284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3596284a32f0SAndreas Färber NetClientState *nc; 35971773d9eeSKONRAD Frederic int i; 359817ec5a86SKONRAD Frederic 3599a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3600127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3601a93e599dSMaxime Coquelin } 3602a93e599dSMaxime Coquelin 36039473939eSJason Baron if (n->net_conf.duplex_str) { 36049473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 36059473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 36069473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 36079473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 36089473939eSJason Baron } else { 36099473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3610843c4cfcSMarkus Armbruster return; 36119473939eSJason Baron } 36129473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 36139473939eSJason Baron } else { 36149473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 36159473939eSJason Baron } 36169473939eSJason Baron 36179473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 36189473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3619843c4cfcSMarkus Armbruster return; 3620843c4cfcSMarkus Armbruster } 3621843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 36229473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 36239473939eSJason Baron } 36249473939eSJason Baron 36259711cd0dSJens Freimann if (n->failover) { 3626b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3627e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 36289711cd0dSJens Freimann device_listener_register(&n->primary_listener); 3629d9cda213SSteve Sistare migration_add_notifier(&n->migration_state, 3630d9cda213SSteve Sistare virtio_net_migration_state_notifier); 36319711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 36329711cd0dSJens Freimann } 36339711cd0dSJens Freimann 3634da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 36353857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_NET, n->config_size); 363617ec5a86SKONRAD Frederic 36371c0fbfa3SMichael S. Tsirkin /* 36381c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 36391c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 36401c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 36411c0fbfa3SMichael S. Tsirkin */ 36421c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 36431c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 36445f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 36451c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 36461c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 36471c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 36481c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 36491c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 36501c0fbfa3SMichael S. Tsirkin return; 36511c0fbfa3SMichael S. Tsirkin } 36521c0fbfa3SMichael S. Tsirkin 36539b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 36544271f403SLaurent Vivier n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) || 36559b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 36569b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 36579b02e161SWei Wang "must be a power of 2 between %d and %d", 36589b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 36594271f403SLaurent Vivier virtio_net_max_tx_queue_size(n)); 36609b02e161SWei Wang virtio_cleanup(vdev); 36619b02e161SWei Wang return; 36629b02e161SWei Wang } 36639b02e161SWei Wang 366422288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 366522288fe5SJason Wang 366622288fe5SJason Wang /* 366722288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 366822288fe5SJason Wang * provide control queue via peers as well. 366922288fe5SJason Wang */ 367022288fe5SJason Wang if (n->nic_conf.peers.queues) { 367122288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 367222288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 367322288fe5SJason Wang ++n->max_queue_pairs; 367422288fe5SJason Wang } 367522288fe5SJason Wang } 367622288fe5SJason Wang } 367722288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 367822288fe5SJason Wang 3679441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 368022288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3681631b22eaSStefan Weil "must be a positive integer less than %d.", 3682441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 36837e0e736eSJason Wang virtio_cleanup(vdev); 36847e0e736eSJason Wang return; 36857e0e736eSJason Wang } 3686b21e2380SMarkus Armbruster n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs); 3687441537f1SJason Wang n->curr_queue_pairs = 1; 36881773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 36896e790746SPaolo Bonzini 36901773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 36911773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 36920765691eSMarkus Armbruster warn_report("virtio-net: " 36936e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 36941773d9eeSKONRAD Frederic n->net_conf.tx); 36950765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 36966e790746SPaolo Bonzini } 36976e790746SPaolo Bonzini 36982eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 36992eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 37009b02e161SWei Wang 37018c497568SAkihiko Odaki virtio_net_add_queue(n, 0); 3702da51a335SJason Wang 370317a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 37041773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 37051773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 37066e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 37079d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 37089d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3709f57fcf70SJason Wang virtio_net_announce_timer, n); 3710b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 37116e790746SPaolo Bonzini 37128a253ec2SKONRAD Frederic if (n->netclient_type) { 37138a253ec2SKONRAD Frederic /* 37148a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 37158a253ec2SKONRAD Frederic */ 37168a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 37177d0fefdfSAkihiko Odaki n->netclient_type, n->netclient_name, 37187d0fefdfSAkihiko Odaki &dev->mem_reentrancy_guard, n); 37198a253ec2SKONRAD Frederic } else { 37201773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 37217d0fefdfSAkihiko Odaki object_get_typename(OBJECT(dev)), dev->id, 37227d0fefdfSAkihiko Odaki &dev->mem_reentrancy_guard, n); 37238a253ec2SKONRAD Frederic } 37248a253ec2SKONRAD Frederic 3725441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3726d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3727d4c62930SBin Meng } 3728d4c62930SBin Meng 37296e790746SPaolo Bonzini peer_test_vnet_hdr(n); 37306e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 37316e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 37326e790746SPaolo Bonzini } else { 37336e790746SPaolo Bonzini n->host_hdr_len = 0; 37346e790746SPaolo Bonzini } 37356e790746SPaolo Bonzini 37361773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 37376e790746SPaolo Bonzini 37386e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 37391773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3740e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 37416e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 37426e790746SPaolo Bonzini 37436e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 37446e790746SPaolo Bonzini 37456e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 37466e790746SPaolo Bonzini 3747b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3748b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3749b1be4280SAmos Kong 3750e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3751e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3752e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3753e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3754f8ed3648SManos Pitsidianakis (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND); 3755e87936eaSCindy Lu } 37562974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3757284a32f0SAndreas Färber n->qdev = dev; 37584474e37aSYuri Benditovich 3759aac8f89dSAkihiko Odaki net_rx_pkt_init(&n->rx_pkt); 37600145c393SAndrew Melnychenko 37610145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 3762283be596SAkihiko Odaki virtio_net_load_ebpf(n); 37630145c393SAndrew Melnychenko } 376417ec5a86SKONRAD Frederic } 376517ec5a86SKONRAD Frederic 3766b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 376717ec5a86SKONRAD Frederic { 3768306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3769306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3770441537f1SJason Wang int i, max_queue_pairs; 377117ec5a86SKONRAD Frederic 37720145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 37730145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 37740145c393SAndrew Melnychenko } 37750145c393SAndrew Melnychenko 377617ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 377717ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 377817ec5a86SKONRAD Frederic 37798a253ec2SKONRAD Frederic g_free(n->netclient_name); 37808a253ec2SKONRAD Frederic n->netclient_name = NULL; 37818a253ec2SKONRAD Frederic g_free(n->netclient_type); 37828a253ec2SKONRAD Frederic n->netclient_type = NULL; 37838a253ec2SKONRAD Frederic 378417ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 378517ec5a86SKONRAD Frederic g_free(n->vlans); 378617ec5a86SKONRAD Frederic 37879711cd0dSJens Freimann if (n->failover) { 3788f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 378965018100SJuan Quintela device_listener_unregister(&n->primary_listener); 3790d9cda213SSteve Sistare migration_remove_notifier(&n->migration_state); 3791f3558b1bSKevin Wolf } else { 3792f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 37939711cd0dSJens Freimann } 37949711cd0dSJens Freimann 3795441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3796441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3797f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 379817ec5a86SKONRAD Frederic } 3799d945d9f1SYuri Benditovich /* delete also control vq */ 3800441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3801944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 380217ec5a86SKONRAD Frederic g_free(n->vqs); 380317ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 38042974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 380559079029SYuri Benditovich g_free(n->rss_data.indirections_table); 38064474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 38076a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 380817ec5a86SKONRAD Frederic } 380917ec5a86SKONRAD Frederic 3810cef776c0SAkihiko Odaki static void virtio_net_reset(VirtIODevice *vdev) 3811cef776c0SAkihiko Odaki { 3812cef776c0SAkihiko Odaki VirtIONet *n = VIRTIO_NET(vdev); 3813cef776c0SAkihiko Odaki int i; 3814cef776c0SAkihiko Odaki 3815cef776c0SAkihiko Odaki /* Reset back to compatibility mode */ 3816cef776c0SAkihiko Odaki n->promisc = 1; 3817cef776c0SAkihiko Odaki n->allmulti = 0; 3818cef776c0SAkihiko Odaki n->alluni = 0; 3819cef776c0SAkihiko Odaki n->nomulti = 0; 3820cef776c0SAkihiko Odaki n->nouni = 0; 3821cef776c0SAkihiko Odaki n->nobcast = 0; 3822cef776c0SAkihiko Odaki /* multiqueue is disabled by default */ 3823cef776c0SAkihiko Odaki n->curr_queue_pairs = 1; 3824cef776c0SAkihiko Odaki timer_del(n->announce_timer.tm); 3825cef776c0SAkihiko Odaki n->announce_timer.round = 0; 3826cef776c0SAkihiko Odaki n->status &= ~VIRTIO_NET_S_ANNOUNCE; 3827cef776c0SAkihiko Odaki 3828cef776c0SAkihiko Odaki /* Flush any MAC and VLAN filter table state */ 3829cef776c0SAkihiko Odaki n->mac_table.in_use = 0; 3830cef776c0SAkihiko Odaki n->mac_table.first_multi = 0; 3831cef776c0SAkihiko Odaki n->mac_table.multi_overflow = 0; 3832cef776c0SAkihiko Odaki n->mac_table.uni_overflow = 0; 3833cef776c0SAkihiko Odaki memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 3834cef776c0SAkihiko Odaki memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 3835cef776c0SAkihiko Odaki qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 3836cef776c0SAkihiko Odaki memset(n->vlans, 0, MAX_VLAN >> 3); 3837cef776c0SAkihiko Odaki 3838cef776c0SAkihiko Odaki /* Flush any async TX */ 3839cef776c0SAkihiko Odaki for (i = 0; i < n->max_queue_pairs; i++) { 3840cef776c0SAkihiko Odaki flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i)); 3841cef776c0SAkihiko Odaki } 3842cef776c0SAkihiko Odaki 3843cef776c0SAkihiko Odaki virtio_net_disable_rss(n); 3844cef776c0SAkihiko Odaki } 3845cef776c0SAkihiko Odaki 384617ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 384717ec5a86SKONRAD Frederic { 384817ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 384917ec5a86SKONRAD Frederic 385017ec5a86SKONRAD Frederic /* 385117ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 38522431f4f1SMichael Tokarev * Can be overridden with virtio_net_set_config_size. 385317ec5a86SKONRAD Frederic */ 385417ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3855aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3856aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 385740c2281cSMarkus Armbruster DEVICE(n)); 38580145c393SAndrew Melnychenko 38590145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 386017ec5a86SKONRAD Frederic } 386117ec5a86SKONRAD Frederic 386244b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 38634d45dcfbSHalil Pasic { 38644d45dcfbSHalil Pasic VirtIONet *n = opaque; 38654d45dcfbSHalil Pasic 38664d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 38674d45dcfbSHalil Pasic * it might keep writing to memory. */ 38684d45dcfbSHalil Pasic assert(!n->vhost_started); 386944b1ff31SDr. David Alan Gilbert 387044b1ff31SDr. David Alan Gilbert return 0; 38714d45dcfbSHalil Pasic } 38724d45dcfbSHalil Pasic 38739711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 38749711cd0dSJens Freimann { 38759711cd0dSJens Freimann DeviceState *dev = opaque; 387621e8709bSJuan Quintela DeviceState *primary; 38779711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 38789711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 38799711cd0dSJens Freimann 3880284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3881284f42a5SJens Freimann return false; 3882284f42a5SJens Freimann } 388321e8709bSJuan Quintela primary = failover_find_primary_device(n); 388421e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 38859711cd0dSJens Freimann } 38869711cd0dSJens Freimann 38879711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 38889711cd0dSJens Freimann { 38899711cd0dSJens Freimann DeviceState *dev = opaque; 38909711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 38919711cd0dSJens Freimann 38929711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 38939711cd0dSJens Freimann } 38949711cd0dSJens Freimann 3895c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) 3896c255488dSJonah Palmer { 3897c255488dSJonah Palmer VirtIONet *n = VIRTIO_NET(vdev); 3898c255488dSJonah Palmer NetClientState *nc = qemu_get_queue(n->nic); 3899c255488dSJonah Palmer struct vhost_net *net = get_vhost_net(nc->peer); 3900c255488dSJonah Palmer return &net->dev; 3901c255488dSJonah Palmer } 3902c255488dSJonah Palmer 39034d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 39044d45dcfbSHalil Pasic .name = "virtio-net", 39054d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 39064d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 39071de81b42SRichard Henderson .fields = (const VMStateField[]) { 39084d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 39094d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 39104d45dcfbSHalil Pasic }, 39114d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 39129711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 39134d45dcfbSHalil Pasic }; 3914290c2428SDr. David Alan Gilbert 391517ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3916127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3917127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3918127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 391987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3920127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3921127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 392287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3923127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 392487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3925127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 392687108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3927127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 392887108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3929127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 393087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3931127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 393287108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3933127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 393487108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3935127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 393687108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3937127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 393887108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3939127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 394087108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3941127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 394287108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3943127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 394487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3945127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 394687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3947127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 394887108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3949127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 395087108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3951127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 395287108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3953127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 395487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3955127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 395659079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 395759079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3958e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3959e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 39606b230b7dSAndrew Melnychenko DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet, nr_ebpf_rss_fds, 39616b230b7dSAndrew Melnychenko ebpf_rss_fds, qdev_prop_string, char*), 39622974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 39632974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 39642974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 39652974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 396617ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 396717ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 396817ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 396917ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 397017ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 39711c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 39721c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 39739b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 39749b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3975a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 397675ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 397775ebec11SMaxime Coquelin true), 39789473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 39799473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 39809711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 398153da8b5aSYuri Benditovich DEFINE_PROP_BIT64("guest_uso4", VirtIONet, host_features, 398253da8b5aSYuri Benditovich VIRTIO_NET_F_GUEST_USO4, true), 398353da8b5aSYuri Benditovich DEFINE_PROP_BIT64("guest_uso6", VirtIONet, host_features, 398453da8b5aSYuri Benditovich VIRTIO_NET_F_GUEST_USO6, true), 398553da8b5aSYuri Benditovich DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features, 398653da8b5aSYuri Benditovich VIRTIO_NET_F_HOST_USO, true), 398717ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 398817ec5a86SKONRAD Frederic }; 398917ec5a86SKONRAD Frederic 399017ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 399117ec5a86SKONRAD Frederic { 399217ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 399317ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3994e6f746b3SAndreas Färber 39954f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3996290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3997125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3998e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3999306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 400017ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 400117ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 400217ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 400317ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 400417ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 400517ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 40067dc6be52SXuan Zhuo vdc->queue_reset = virtio_net_queue_reset; 40077f863302SKangjie Xu vdc->queue_enable = virtio_net_queue_enable; 400817ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 400917ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 401017ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 40112a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 40127788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 4013982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 40149711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 4015c255488dSJonah Palmer vdc->get_vhost = virtio_net_get_vhost; 4016cd9b8346SViktor Prutyanov vdc->toggle_device_iotlb = vhost_toggle_device_iotlb; 401717ec5a86SKONRAD Frederic } 401817ec5a86SKONRAD Frederic 401917ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 402017ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 402117ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 402217ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 402317ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 402417ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 402517ec5a86SKONRAD Frederic }; 402617ec5a86SKONRAD Frederic 402717ec5a86SKONRAD Frederic static void virtio_register_types(void) 402817ec5a86SKONRAD Frederic { 402917ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 403017ec5a86SKONRAD Frederic } 403117ec5a86SKONRAD Frederic 403217ec5a86SKONRAD Frederic type_init(virtio_register_types) 4033