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" 45*6b230b7dSAndrew 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 */ 3631bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 364441537f1SJason Wang queue_pairs, true); 3651bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3661bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3671bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3681bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3691bfa316cSGreg Kurz * endianness. 3701bfa316cSGreg Kurz */ 371441537f1SJason Wang virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false); 3721bfa316cSGreg Kurz } 3731bfa316cSGreg Kurz } 3741bfa316cSGreg Kurz 375283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 376283e2c2aSYuri Benditovich { 377283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 378283e2c2aSYuri Benditovich if (dropped) { 379283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 380283e2c2aSYuri Benditovich } 381283e2c2aSYuri Benditovich } 382283e2c2aSYuri Benditovich 3836e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3846e790746SPaolo Bonzini { 38517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3866e790746SPaolo Bonzini VirtIONetQueue *q; 3876e790746SPaolo Bonzini int i; 3886e790746SPaolo Bonzini uint8_t queue_status; 3896e790746SPaolo Bonzini 3901bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3916e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3926e790746SPaolo Bonzini 393441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 39438705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 39538705bb5SFam Zheng bool queue_started; 3966e790746SPaolo Bonzini q = &n->vqs[i]; 3976e790746SPaolo Bonzini 398441537f1SJason Wang if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) { 3996e790746SPaolo Bonzini queue_status = 0; 4006e790746SPaolo Bonzini } else { 4016e790746SPaolo Bonzini queue_status = status; 4026e790746SPaolo Bonzini } 40338705bb5SFam Zheng queue_started = 40438705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 40538705bb5SFam Zheng 40638705bb5SFam Zheng if (queue_started) { 40738705bb5SFam Zheng qemu_flush_queued_packets(ncs); 40838705bb5SFam Zheng } 4096e790746SPaolo Bonzini 4106e790746SPaolo Bonzini if (!q->tx_waiting) { 4116e790746SPaolo Bonzini continue; 4126e790746SPaolo Bonzini } 4136e790746SPaolo Bonzini 41438705bb5SFam Zheng if (queue_started) { 4156e790746SPaolo Bonzini if (q->tx_timer) { 416bc72ad67SAlex Bligh timer_mod(q->tx_timer, 417bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 4186e790746SPaolo Bonzini } else { 4196e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 4206e790746SPaolo Bonzini } 4216e790746SPaolo Bonzini } else { 4226e790746SPaolo Bonzini if (q->tx_timer) { 423bc72ad67SAlex Bligh timer_del(q->tx_timer); 4246e790746SPaolo Bonzini } else { 4256e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 4266e790746SPaolo Bonzini } 427283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 42870e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 42970e53e6eSJason Wang vdev->vm_running) { 430283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 431283e2c2aSYuri Benditovich * and disabled notification */ 432283e2c2aSYuri Benditovich q->tx_waiting = 0; 433283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 434283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 435283e2c2aSYuri Benditovich } 4366e790746SPaolo Bonzini } 4376e790746SPaolo Bonzini } 4386e790746SPaolo Bonzini } 4396e790746SPaolo Bonzini 4406e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4416e790746SPaolo Bonzini { 4426e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 44317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4446e790746SPaolo Bonzini uint16_t old_status = n->status; 4456e790746SPaolo Bonzini 4466e790746SPaolo Bonzini if (nc->link_down) 4476e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4486e790746SPaolo Bonzini else 4496e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4506e790746SPaolo Bonzini 4516e790746SPaolo Bonzini if (n->status != old_status) 45217a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4536e790746SPaolo Bonzini 45417a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4556e790746SPaolo Bonzini } 4566e790746SPaolo Bonzini 457b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 458b1be4280SAmos Kong { 459b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 460b1be4280SAmos Kong 461b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 462ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 4637480874aSMarkus Armbruster qapi_event_send_nic_rx_filter_changed(n->netclient_name, path); 46496e35046SAmos Kong g_free(path); 465b1be4280SAmos Kong 466b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 467b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 468b1be4280SAmos Kong } 469b1be4280SAmos Kong } 470b1be4280SAmos Kong 471f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 472f7bc8ef8SAmos Kong { 47354aa3de7SEric Blake intList *list; 474f7bc8ef8SAmos Kong int i, j; 475f7bc8ef8SAmos Kong 476f7bc8ef8SAmos Kong list = NULL; 477f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 478f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 479f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 48054aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 481f7bc8ef8SAmos Kong } 482f7bc8ef8SAmos Kong } 483f7bc8ef8SAmos Kong } 484f7bc8ef8SAmos Kong 485f7bc8ef8SAmos Kong return list; 486f7bc8ef8SAmos Kong } 487f7bc8ef8SAmos Kong 488b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 489b1be4280SAmos Kong { 490b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 491f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 492b1be4280SAmos Kong RxFilterInfo *info; 49354aa3de7SEric Blake strList *str_list; 494f7bc8ef8SAmos Kong int i; 495b1be4280SAmos Kong 496b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 497b1be4280SAmos Kong info->name = g_strdup(nc->name); 498b1be4280SAmos Kong info->promiscuous = n->promisc; 499b1be4280SAmos Kong 500b1be4280SAmos Kong if (n->nouni) { 501b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 502b1be4280SAmos Kong } else if (n->alluni) { 503b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 504b1be4280SAmos Kong } else { 505b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 506b1be4280SAmos Kong } 507b1be4280SAmos Kong 508b1be4280SAmos Kong if (n->nomulti) { 509b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 510b1be4280SAmos Kong } else if (n->allmulti) { 511b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 512b1be4280SAmos Kong } else { 513b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 514b1be4280SAmos Kong } 515b1be4280SAmos Kong 516b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 517b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 518b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 519b1be4280SAmos Kong 520b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 521b1be4280SAmos Kong 522b1be4280SAmos Kong str_list = NULL; 523b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 52454aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 52554aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 526b1be4280SAmos Kong } 527b1be4280SAmos Kong info->unicast_table = str_list; 528b1be4280SAmos Kong 529b1be4280SAmos Kong str_list = NULL; 530b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 53154aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 53254aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 533b1be4280SAmos Kong } 534b1be4280SAmos Kong info->multicast_table = str_list; 535f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 536b1be4280SAmos Kong 53795129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 538f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 539f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 540f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 541f7bc8ef8SAmos Kong } else { 542f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 543b1be4280SAmos Kong } 544b1be4280SAmos Kong 545b1be4280SAmos Kong /* enable event notification after query */ 546b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 547b1be4280SAmos Kong 548b1be4280SAmos Kong return info; 549b1be4280SAmos Kong } 550b1be4280SAmos Kong 5517dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index) 5527dc6be52SXuan Zhuo { 5537dc6be52SXuan Zhuo VirtIONet *n = VIRTIO_NET(vdev); 554f47af0afSXuan Zhuo NetClientState *nc; 555f47af0afSXuan Zhuo 556f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 557f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 558f47af0afSXuan Zhuo return; 559f47af0afSXuan Zhuo } 560f47af0afSXuan Zhuo 561f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 5627dc6be52SXuan Zhuo 5637dc6be52SXuan Zhuo if (!nc->peer) { 5647dc6be52SXuan Zhuo return; 5657dc6be52SXuan Zhuo } 5667dc6be52SXuan Zhuo 5677dc6be52SXuan Zhuo if (get_vhost_net(nc->peer) && 5687dc6be52SXuan Zhuo nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5697dc6be52SXuan Zhuo vhost_net_virtqueue_reset(vdev, nc, queue_index); 5707dc6be52SXuan Zhuo } 5717dc6be52SXuan Zhuo 5727dc6be52SXuan Zhuo flush_or_purge_queued_packets(nc); 5737dc6be52SXuan Zhuo } 5747dc6be52SXuan Zhuo 5757f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index) 5767f863302SKangjie Xu { 5777f863302SKangjie Xu VirtIONet *n = VIRTIO_NET(vdev); 578f47af0afSXuan Zhuo NetClientState *nc; 5797f863302SKangjie Xu int r; 5807f863302SKangjie Xu 581f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 582f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 583f47af0afSXuan Zhuo return; 584f47af0afSXuan Zhuo } 585f47af0afSXuan Zhuo 586f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 587f47af0afSXuan Zhuo 5887f863302SKangjie Xu if (!nc->peer || !vdev->vhost_started) { 5897f863302SKangjie Xu return; 5907f863302SKangjie Xu } 5917f863302SKangjie Xu 5927f863302SKangjie Xu if (get_vhost_net(nc->peer) && 5937f863302SKangjie Xu nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5947f863302SKangjie Xu r = vhost_net_virtqueue_restart(vdev, nc, queue_index); 5957f863302SKangjie Xu if (r < 0) { 5967f863302SKangjie Xu error_report("unable to restart vhost net virtqueue: %d, " 5977f863302SKangjie Xu "when resetting the queue", queue_index); 5987f863302SKangjie Xu } 5997f863302SKangjie Xu } 6007f863302SKangjie Xu } 6017f863302SKangjie Xu 6026e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 6036e790746SPaolo Bonzini { 60417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 60594b52958SGreg Kurz int i; 6066e790746SPaolo Bonzini 6076e790746SPaolo Bonzini /* Reset back to compatibility mode */ 6086e790746SPaolo Bonzini n->promisc = 1; 6096e790746SPaolo Bonzini n->allmulti = 0; 6106e790746SPaolo Bonzini n->alluni = 0; 6116e790746SPaolo Bonzini n->nomulti = 0; 6126e790746SPaolo Bonzini n->nouni = 0; 6136e790746SPaolo Bonzini n->nobcast = 0; 6146e790746SPaolo Bonzini /* multiqueue is disabled by default */ 615441537f1SJason Wang n->curr_queue_pairs = 1; 6169d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 6179d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 618f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 6196e790746SPaolo Bonzini 6206e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 6216e790746SPaolo Bonzini n->mac_table.in_use = 0; 6226e790746SPaolo Bonzini n->mac_table.first_multi = 0; 6236e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 6246e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 6256e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 6266e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 627702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 6286e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 62994b52958SGreg Kurz 63094b52958SGreg Kurz /* Flush any async TX */ 631441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6324fdf69abSKangjie Xu flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i)); 63394b52958SGreg Kurz } 6346e790746SPaolo Bonzini } 6356e790746SPaolo Bonzini 6366e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 6376e790746SPaolo Bonzini { 6386e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 6396e790746SPaolo Bonzini if (!nc->peer) { 6406e790746SPaolo Bonzini return; 6416e790746SPaolo Bonzini } 6426e790746SPaolo Bonzini 643d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 6446e790746SPaolo Bonzini } 6456e790746SPaolo Bonzini 6466e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 6476e790746SPaolo Bonzini { 6486e790746SPaolo Bonzini return n->has_vnet_hdr; 6496e790746SPaolo Bonzini } 6506e790746SPaolo Bonzini 6516e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 6526e790746SPaolo Bonzini { 6536e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 6546e790746SPaolo Bonzini return 0; 6556e790746SPaolo Bonzini 656d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 6576e790746SPaolo Bonzini 6586e790746SPaolo Bonzini return n->has_ufo; 6596e790746SPaolo Bonzini } 6606e790746SPaolo Bonzini 66153da8b5aSYuri Benditovich static int peer_has_uso(VirtIONet *n) 66253da8b5aSYuri Benditovich { 66353da8b5aSYuri Benditovich if (!peer_has_vnet_hdr(n)) { 66453da8b5aSYuri Benditovich return 0; 66553da8b5aSYuri Benditovich } 66653da8b5aSYuri Benditovich 66753da8b5aSYuri Benditovich return qemu_has_uso(qemu_get_queue(n->nic)->peer); 66853da8b5aSYuri Benditovich } 66953da8b5aSYuri Benditovich 670bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 671e22f0603SYuri Benditovich int version_1, int hash_report) 6726e790746SPaolo Bonzini { 6736e790746SPaolo Bonzini int i; 6746e790746SPaolo Bonzini NetClientState *nc; 6756e790746SPaolo Bonzini 6766e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6776e790746SPaolo Bonzini 6782220e818SJason Wang /* 6792220e818SJason Wang * Note: when extending the vnet header, please make sure to 6802220e818SJason Wang * change the vnet header copying logic in virtio_net_flush_tx() 6812220e818SJason Wang * as well. 6822220e818SJason Wang */ 683bb9d17f8SCornelia Huck if (version_1) { 684e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 685e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 686e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 687e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 688bb9d17f8SCornelia Huck } else { 6896e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 690bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 691bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 692bb9d17f8SCornelia Huck } 6936e790746SPaolo Bonzini 694441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6956e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6966e790746SPaolo Bonzini 6976e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 698d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 699d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 7006e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 7016e790746SPaolo Bonzini } 7026e790746SPaolo Bonzini } 7036e790746SPaolo Bonzini } 7046e790746SPaolo Bonzini 7052eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 7062eef278bSMichael S. Tsirkin { 7072eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 7082eef278bSMichael S. Tsirkin 7092eef278bSMichael S. Tsirkin /* 7100ea5778fSEugenio Pérez * Backends other than vhost-user or vhost-vdpa don't support max queue 7110ea5778fSEugenio Pérez * size. 7122eef278bSMichael S. Tsirkin */ 7132eef278bSMichael S. Tsirkin if (!peer) { 7142eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 7152eef278bSMichael S. Tsirkin } 7162eef278bSMichael S. Tsirkin 7170ea5778fSEugenio Pérez switch(peer->info->type) { 7180ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_USER: 7190ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_VDPA: 7202eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 7210ea5778fSEugenio Pérez default: 7220ea5778fSEugenio Pérez return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 7230ea5778fSEugenio Pérez }; 7242eef278bSMichael S. Tsirkin } 7252eef278bSMichael S. Tsirkin 7266e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 7276e790746SPaolo Bonzini { 7286e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7296e790746SPaolo Bonzini 7306e790746SPaolo Bonzini if (!nc->peer) { 7316e790746SPaolo Bonzini return 0; 7326e790746SPaolo Bonzini } 7336e790746SPaolo Bonzini 734f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7357263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 7367263a0adSChangchun Ouyang } 7377263a0adSChangchun Ouyang 738f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7396e790746SPaolo Bonzini return 0; 7406e790746SPaolo Bonzini } 7416e790746SPaolo Bonzini 742441537f1SJason Wang if (n->max_queue_pairs == 1) { 7431074b879SJason Wang return 0; 7441074b879SJason Wang } 7451074b879SJason Wang 7466e790746SPaolo Bonzini return tap_enable(nc->peer); 7476e790746SPaolo Bonzini } 7486e790746SPaolo Bonzini 7496e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 7506e790746SPaolo Bonzini { 7516e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7526e790746SPaolo Bonzini 7536e790746SPaolo Bonzini if (!nc->peer) { 7546e790746SPaolo Bonzini return 0; 7556e790746SPaolo Bonzini } 7566e790746SPaolo Bonzini 757f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7587263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 7597263a0adSChangchun Ouyang } 7607263a0adSChangchun Ouyang 761f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7626e790746SPaolo Bonzini return 0; 7636e790746SPaolo Bonzini } 7646e790746SPaolo Bonzini 7656e790746SPaolo Bonzini return tap_disable(nc->peer); 7666e790746SPaolo Bonzini } 7676e790746SPaolo Bonzini 768441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 7696e790746SPaolo Bonzini { 7706e790746SPaolo Bonzini int i; 771ddfa83eaSJoel Stanley int r; 7726e790746SPaolo Bonzini 77368b5f314SYuri Benditovich if (n->nic->peer_deleted) { 77468b5f314SYuri Benditovich return; 77568b5f314SYuri Benditovich } 77668b5f314SYuri Benditovich 777441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 778441537f1SJason Wang if (i < n->curr_queue_pairs) { 779ddfa83eaSJoel Stanley r = peer_attach(n, i); 780ddfa83eaSJoel Stanley assert(!r); 7816e790746SPaolo Bonzini } else { 782ddfa83eaSJoel Stanley r = peer_detach(n, i); 783ddfa83eaSJoel Stanley assert(!r); 7846e790746SPaolo Bonzini } 7856e790746SPaolo Bonzini } 7866e790746SPaolo Bonzini } 7876e790746SPaolo Bonzini 788ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7896e790746SPaolo Bonzini 7909d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7919d5b731dSJason Wang Error **errp) 7926e790746SPaolo Bonzini { 79317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7946e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7956e790746SPaolo Bonzini 796da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 797da3e8a23SShannon Zhao features |= n->host_features; 798da3e8a23SShannon Zhao 7990cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 8006e790746SPaolo Bonzini 8016e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 8020cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 8030cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 8040cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 8050cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 8066e790746SPaolo Bonzini 8070cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 8080cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 8090cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 8100cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 811e22f0603SYuri Benditovich 81253da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO); 81353da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4); 81453da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6); 81553da8b5aSYuri Benditovich 816e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 8176e790746SPaolo Bonzini } 8186e790746SPaolo Bonzini 8196e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 8200cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 8210cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 8226e790746SPaolo Bonzini } 8236e790746SPaolo Bonzini 82453da8b5aSYuri Benditovich if (!peer_has_uso(n)) { 82553da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO); 82653da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4); 82753da8b5aSYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6); 82853da8b5aSYuri Benditovich } 82953da8b5aSYuri Benditovich 830ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 8316e790746SPaolo Bonzini return features; 8326e790746SPaolo Bonzini } 8332974e916SYuri Benditovich 8340145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 83559079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 8360145c393SAndrew Melnychenko } 83775ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 83875ebec11SMaxime Coquelin vdev->backend_features = features; 83975ebec11SMaxime Coquelin 84075ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 84175ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 84275ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 84375ebec11SMaxime Coquelin } 84475ebec11SMaxime Coquelin 845cd69d47cSEugenio Pérez /* 846cd69d47cSEugenio Pérez * Since GUEST_ANNOUNCE is emulated the feature bit could be set without 847cd69d47cSEugenio Pérez * enabled. This happens in the vDPA case. 848cd69d47cSEugenio Pérez * 849cd69d47cSEugenio Pérez * Make sure the feature set is not incoherent, as the driver could refuse 850cd69d47cSEugenio Pérez * to start. 851cd69d47cSEugenio Pérez * 852cd69d47cSEugenio Pérez * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes, 853cd69d47cSEugenio Pérez * helping guest to notify the new location with vDPA devices that does not 854cd69d47cSEugenio Pérez * support it. 855cd69d47cSEugenio Pérez */ 856cd69d47cSEugenio Pérez if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) { 857cd69d47cSEugenio Pérez virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE); 858cd69d47cSEugenio Pérez } 859cd69d47cSEugenio Pérez 86075ebec11SMaxime Coquelin return features; 8616e790746SPaolo Bonzini } 8626e790746SPaolo Bonzini 863019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 8646e790746SPaolo Bonzini { 865019a3edbSGerd Hoffmann uint64_t features = 0; 8666e790746SPaolo Bonzini 8676e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 8686e790746SPaolo Bonzini * but also these: */ 8690cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 8700cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 8710cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 8720cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 8730cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 8746e790746SPaolo Bonzini 8756e790746SPaolo Bonzini return features; 8766e790746SPaolo Bonzini } 8776e790746SPaolo Bonzini 878644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 879644c9858SDmitry Fleytman { 880ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 881644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 882644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 883644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 884644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 8852ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)), 8862ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)), 8872ab0ec31SAndrew Melnychenko !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6))); 888644c9858SDmitry Fleytman } 889644c9858SDmitry Fleytman 89053da8b5aSYuri Benditovich static uint64_t virtio_net_guest_offloads_by_features(uint64_t features) 891644c9858SDmitry Fleytman { 892644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 893644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 894644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 895644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 896644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 89753da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_UFO) | 89853da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_USO4) | 89953da8b5aSYuri Benditovich (1ULL << VIRTIO_NET_F_GUEST_USO6); 900644c9858SDmitry Fleytman 901644c9858SDmitry Fleytman return guest_offloads_mask & features; 902644c9858SDmitry Fleytman } 903644c9858SDmitry Fleytman 9040b545b1eSHawkins Jiawei uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n) 905644c9858SDmitry Fleytman { 906644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 907644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 908644c9858SDmitry Fleytman } 909644c9858SDmitry Fleytman 910f5e1847bSJuan Quintela typedef struct { 911f5e1847bSJuan Quintela VirtIONet *n; 91212b2fad7SKevin Wolf DeviceState *dev; 91312b2fad7SKevin Wolf } FailoverDevice; 914f5e1847bSJuan Quintela 915f5e1847bSJuan Quintela /** 91612b2fad7SKevin Wolf * Set the failover primary device 917f5e1847bSJuan Quintela * 918f5e1847bSJuan Quintela * @opaque: FailoverId to setup 919f5e1847bSJuan Quintela * @opts: opts for device we are handling 920f5e1847bSJuan Quintela * @errp: returns an error if this function fails 921f5e1847bSJuan Quintela */ 92212b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 923f5e1847bSJuan Quintela { 92412b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 92512b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 92612b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 927f5e1847bSJuan Quintela 92812b2fad7SKevin Wolf if (!pci_dev) { 92912b2fad7SKevin Wolf return 0; 93012b2fad7SKevin Wolf } 93112b2fad7SKevin Wolf 93212b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 93312b2fad7SKevin Wolf fdev->dev = dev; 934f5e1847bSJuan Quintela return 1; 935f5e1847bSJuan Quintela } 936f5e1847bSJuan Quintela 937f5e1847bSJuan Quintela return 0; 938f5e1847bSJuan Quintela } 939f5e1847bSJuan Quintela 940f5e1847bSJuan Quintela /** 94185d3b931SJuan Quintela * Find the primary device for this failover virtio-net 94285d3b931SJuan Quintela * 94385d3b931SJuan Quintela * @n: VirtIONet device 94485d3b931SJuan Quintela * @errp: returns an error if this function fails 94585d3b931SJuan Quintela */ 9460a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 9479711cd0dSJens Freimann { 94812b2fad7SKevin Wolf FailoverDevice fdev = { 94912b2fad7SKevin Wolf .n = n, 95012b2fad7SKevin Wolf }; 9519711cd0dSJens Freimann 95212b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 95312b2fad7SKevin Wolf NULL, NULL, &fdev); 95412b2fad7SKevin Wolf return fdev.dev; 9559711cd0dSJens Freimann } 9569711cd0dSJens Freimann 95721e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 95821e8709bSJuan Quintela { 95921e8709bSJuan Quintela Error *err = NULL; 96021e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 96121e8709bSJuan Quintela 96221e8709bSJuan Quintela if (dev) { 96321e8709bSJuan Quintela return; 96421e8709bSJuan Quintela } 96521e8709bSJuan Quintela 966259a10dbSKevin Wolf if (!n->primary_opts) { 96797ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 96897ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 96997ca9c59SLaurent Vivier "sure primary device has parameter" 97097ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 97121e8709bSJuan Quintela return; 97221e8709bSJuan Quintela } 973259a10dbSKevin Wolf 974f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 975f3558b1bSKevin Wolf n->primary_opts_from_json, 976f3558b1bSKevin Wolf &err); 97721e8709bSJuan Quintela if (err) { 978f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 979259a10dbSKevin Wolf n->primary_opts = NULL; 98000e7b129SLaurent Vivier } else { 98100e7b129SLaurent Vivier object_unref(OBJECT(dev)); 98221e8709bSJuan Quintela } 98321e8709bSJuan Quintela error_propagate(errp, err); 98421e8709bSJuan Quintela } 98521e8709bSJuan Quintela 986d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 9876e790746SPaolo Bonzini { 98817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9899711cd0dSJens Freimann Error *err = NULL; 9906e790746SPaolo Bonzini int i; 9916e790746SPaolo Bonzini 99275ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 99375ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 99475ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 99575ebec11SMaxime Coquelin } 99675ebec11SMaxime Coquelin 997ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 99859079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 99995129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 10006e790746SPaolo Bonzini 1001ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 100295129d6fSCornelia Huck virtio_has_feature(features, 1003bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 100495129d6fSCornelia Huck virtio_has_feature(features, 1005e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 1006e22f0603SYuri Benditovich virtio_has_feature(features, 1007e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 10086e790746SPaolo Bonzini 10092974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 10102974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 10112974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 10122974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 1013e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 10142974e916SYuri Benditovich 10156e790746SPaolo Bonzini if (n->has_vnet_hdr) { 1016644c9858SDmitry Fleytman n->curr_guest_offloads = 1017644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 1018644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 10196e790746SPaolo Bonzini } 10206e790746SPaolo Bonzini 1021441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 10226e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 10236e790746SPaolo Bonzini 1024ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 10256e790746SPaolo Bonzini continue; 10266e790746SPaolo Bonzini } 1027ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 1028c9bdc449SHyman Huang(黄勇) 1029c9bdc449SHyman Huang(黄勇) /* 1030c9bdc449SHyman Huang(黄勇) * keep acked_features in NetVhostUserState up-to-date so it 1031c9bdc449SHyman Huang(黄勇) * can't miss any features configured by guest virtio driver. 1032c9bdc449SHyman Huang(黄勇) */ 1033c9bdc449SHyman Huang(黄勇) vhost_net_save_acked_features(nc->peer); 10346e790746SPaolo Bonzini } 10350b1eaa88SStefan Fritsch 103606b636a1SHawkins Jiawei if (!virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 10370b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 10380b1eaa88SStefan Fritsch } 10399711cd0dSJens Freimann 10409711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 10419711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 1042e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 10439711cd0dSJens Freimann failover_add_primary(n, &err); 10449711cd0dSJens Freimann if (err) { 10451b529d90SLaurent Vivier if (!qtest_enabled()) { 10469711cd0dSJens Freimann warn_report_err(err); 10471b529d90SLaurent Vivier } else { 10481b529d90SLaurent Vivier error_free(err); 10491b529d90SLaurent Vivier } 10509711cd0dSJens Freimann } 10516e790746SPaolo Bonzini } 105221e8709bSJuan Quintela } 10536e790746SPaolo Bonzini 10546e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 10556e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10566e790746SPaolo Bonzini { 10576e790746SPaolo Bonzini uint8_t on; 10586e790746SPaolo Bonzini size_t s; 1059b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10606e790746SPaolo Bonzini 10616e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 10626e790746SPaolo Bonzini if (s != sizeof(on)) { 10636e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10646e790746SPaolo Bonzini } 10656e790746SPaolo Bonzini 10666e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 10676e790746SPaolo Bonzini n->promisc = on; 10686e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 10696e790746SPaolo Bonzini n->allmulti = on; 10706e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 10716e790746SPaolo Bonzini n->alluni = on; 10726e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 10736e790746SPaolo Bonzini n->nomulti = on; 10746e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 10756e790746SPaolo Bonzini n->nouni = on; 10766e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 10776e790746SPaolo Bonzini n->nobcast = on; 10786e790746SPaolo Bonzini } else { 10796e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10806e790746SPaolo Bonzini } 10816e790746SPaolo Bonzini 1082b1be4280SAmos Kong rxfilter_notify(nc); 1083b1be4280SAmos Kong 10846e790746SPaolo Bonzini return VIRTIO_NET_OK; 10856e790746SPaolo Bonzini } 10866e790746SPaolo Bonzini 1087644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 1088644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 1089644c9858SDmitry Fleytman { 1090644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 1091644c9858SDmitry Fleytman uint64_t offloads; 1092644c9858SDmitry Fleytman size_t s; 1093644c9858SDmitry Fleytman 109495129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 1095644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1096644c9858SDmitry Fleytman } 1097644c9858SDmitry Fleytman 1098644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 1099644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 1100644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1101644c9858SDmitry Fleytman } 1102644c9858SDmitry Fleytman 1103644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 1104644c9858SDmitry Fleytman uint64_t supported_offloads; 1105644c9858SDmitry Fleytman 1106189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 1107189ae6bbSJason Wang 1108644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 1109644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1110644c9858SDmitry Fleytman } 1111644c9858SDmitry Fleytman 11122974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 11132974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 11142974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 11152974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 11162974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 11172974e916SYuri Benditovich 1118644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1119644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1120644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1121644c9858SDmitry Fleytman } 1122644c9858SDmitry Fleytman 1123644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1124644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1125644c9858SDmitry Fleytman 1126644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1127644c9858SDmitry Fleytman } else { 1128644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1129644c9858SDmitry Fleytman } 1130644c9858SDmitry Fleytman } 1131644c9858SDmitry Fleytman 11326e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 11336e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11346e790746SPaolo Bonzini { 11351399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11366e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 11376e790746SPaolo Bonzini size_t s; 1138b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11396e790746SPaolo Bonzini 11406e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 11416e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 11426e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11436e790746SPaolo Bonzini } 11446e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 11456e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 11466e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1147b1be4280SAmos Kong rxfilter_notify(nc); 1148b1be4280SAmos Kong 11496e790746SPaolo Bonzini return VIRTIO_NET_OK; 11506e790746SPaolo Bonzini } 11516e790746SPaolo Bonzini 11526e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 11536e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11546e790746SPaolo Bonzini } 11556e790746SPaolo Bonzini 1156cae2e556SAmos Kong int in_use = 0; 1157cae2e556SAmos Kong int first_multi = 0; 1158cae2e556SAmos Kong uint8_t uni_overflow = 0; 1159cae2e556SAmos Kong uint8_t multi_overflow = 0; 1160cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 11616e790746SPaolo Bonzini 11626e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11636e790746SPaolo Bonzini sizeof(mac_data.entries)); 11641399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11656e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1166b1be4280SAmos Kong goto error; 11676e790746SPaolo Bonzini } 11686e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11696e790746SPaolo Bonzini 11706e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1171b1be4280SAmos Kong goto error; 11726e790746SPaolo Bonzini } 11736e790746SPaolo Bonzini 11746e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1175cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 11766e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11776e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1178b1be4280SAmos Kong goto error; 11796e790746SPaolo Bonzini } 1180cae2e556SAmos Kong in_use += mac_data.entries; 11816e790746SPaolo Bonzini } else { 1182cae2e556SAmos Kong uni_overflow = 1; 11836e790746SPaolo Bonzini } 11846e790746SPaolo Bonzini 11856e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 11866e790746SPaolo Bonzini 1187cae2e556SAmos Kong first_multi = in_use; 11886e790746SPaolo Bonzini 11896e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11906e790746SPaolo Bonzini sizeof(mac_data.entries)); 11911399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11926e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1193b1be4280SAmos Kong goto error; 11946e790746SPaolo Bonzini } 11956e790746SPaolo Bonzini 11966e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11976e790746SPaolo Bonzini 11986e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1199b1be4280SAmos Kong goto error; 12006e790746SPaolo Bonzini } 12016e790746SPaolo Bonzini 1202edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1203cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 12046e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 12056e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1206b1be4280SAmos Kong goto error; 12076e790746SPaolo Bonzini } 1208cae2e556SAmos Kong in_use += mac_data.entries; 12096e790746SPaolo Bonzini } else { 1210cae2e556SAmos Kong multi_overflow = 1; 12116e790746SPaolo Bonzini } 12126e790746SPaolo Bonzini 1213cae2e556SAmos Kong n->mac_table.in_use = in_use; 1214cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1215cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1216cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1217cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1218cae2e556SAmos Kong g_free(macs); 1219b1be4280SAmos Kong rxfilter_notify(nc); 1220b1be4280SAmos Kong 12216e790746SPaolo Bonzini return VIRTIO_NET_OK; 1222b1be4280SAmos Kong 1223b1be4280SAmos Kong error: 1224cae2e556SAmos Kong g_free(macs); 1225b1be4280SAmos Kong return VIRTIO_NET_ERR; 12266e790746SPaolo Bonzini } 12276e790746SPaolo Bonzini 12286e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 12296e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 12306e790746SPaolo Bonzini { 12311399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 12326e790746SPaolo Bonzini uint16_t vid; 12336e790746SPaolo Bonzini size_t s; 1234b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 12356e790746SPaolo Bonzini 12366e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 12371399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 12386e790746SPaolo Bonzini if (s != sizeof(vid)) { 12396e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12406e790746SPaolo Bonzini } 12416e790746SPaolo Bonzini 12426e790746SPaolo Bonzini if (vid >= MAX_VLAN) 12436e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12446e790746SPaolo Bonzini 12456e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 12466e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 12476e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 12486e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 12496e790746SPaolo Bonzini else 12506e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12516e790746SPaolo Bonzini 1252b1be4280SAmos Kong rxfilter_notify(nc); 1253b1be4280SAmos Kong 12546e790746SPaolo Bonzini return VIRTIO_NET_OK; 12556e790746SPaolo Bonzini } 12566e790746SPaolo Bonzini 1257f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1258f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1259f57fcf70SJason Wang { 12609d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1261f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1262f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1263f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 12649d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 12659d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1266f57fcf70SJason Wang } 1267f57fcf70SJason Wang return VIRTIO_NET_OK; 1268f57fcf70SJason Wang } else { 1269f57fcf70SJason Wang return VIRTIO_NET_ERR; 1270f57fcf70SJason Wang } 1271f57fcf70SJason Wang } 1272f57fcf70SJason Wang 12730145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n); 12740145c393SAndrew Melnychenko 127559079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 127659079029SYuri Benditovich { 127759079029SYuri Benditovich if (n->rss_data.enabled) { 127859079029SYuri Benditovich trace_virtio_net_rss_disable(); 127959079029SYuri Benditovich } 128059079029SYuri Benditovich n->rss_data.enabled = false; 12810145c393SAndrew Melnychenko 12820145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 12830145c393SAndrew Melnychenko } 12840145c393SAndrew Melnychenko 12850145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 12860145c393SAndrew Melnychenko { 12870145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 12880145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 12890145c393SAndrew Melnychenko return false; 12900145c393SAndrew Melnychenko } 12910145c393SAndrew Melnychenko 12920145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 12930145c393SAndrew Melnychenko } 12940145c393SAndrew Melnychenko 12950145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 12960145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 12970145c393SAndrew Melnychenko { 12980145c393SAndrew Melnychenko config->redirect = data->redirect; 12990145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 13000145c393SAndrew Melnychenko config->hash_types = data->hash_types; 13010145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 13020145c393SAndrew Melnychenko config->default_queue = data->default_queue; 13030145c393SAndrew Melnychenko } 13040145c393SAndrew Melnychenko 13050145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 13060145c393SAndrew Melnychenko { 13070145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 13080145c393SAndrew Melnychenko 13090145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 13100145c393SAndrew Melnychenko return false; 13110145c393SAndrew Melnychenko } 13120145c393SAndrew Melnychenko 13130145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 13140145c393SAndrew Melnychenko 13150145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 13160145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 13170145c393SAndrew Melnychenko return false; 13180145c393SAndrew Melnychenko } 13190145c393SAndrew Melnychenko 13200145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 13210145c393SAndrew Melnychenko return false; 13220145c393SAndrew Melnychenko } 13230145c393SAndrew Melnychenko 13240145c393SAndrew Melnychenko return true; 13250145c393SAndrew Melnychenko } 13260145c393SAndrew Melnychenko 13270145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 13280145c393SAndrew Melnychenko { 13290145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 13300145c393SAndrew Melnychenko } 13310145c393SAndrew Melnychenko 1332*6b230b7dSAndrew Melnychenko static bool virtio_net_load_ebpf_fds(VirtIONet *n, Error **errp) 13330145c393SAndrew Melnychenko { 1334*6b230b7dSAndrew Melnychenko int fds[EBPF_RSS_MAX_FDS] = { [0 ... EBPF_RSS_MAX_FDS - 1] = -1}; 1335*6b230b7dSAndrew Melnychenko int ret = true; 1336*6b230b7dSAndrew Melnychenko int i = 0; 1337*6b230b7dSAndrew Melnychenko 1338*6b230b7dSAndrew Melnychenko ERRP_GUARD(); 1339*6b230b7dSAndrew Melnychenko 1340*6b230b7dSAndrew Melnychenko if (n->nr_ebpf_rss_fds != EBPF_RSS_MAX_FDS) { 1341*6b230b7dSAndrew Melnychenko error_setg(errp, 1342*6b230b7dSAndrew Melnychenko "Expected %d file descriptors but got %d", 1343*6b230b7dSAndrew Melnychenko EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds); 13440145c393SAndrew Melnychenko return false; 13450145c393SAndrew Melnychenko } 13460145c393SAndrew Melnychenko 1347*6b230b7dSAndrew Melnychenko for (i = 0; i < n->nr_ebpf_rss_fds; i++) { 1348*6b230b7dSAndrew Melnychenko fds[i] = monitor_fd_param(monitor_cur(), n->ebpf_rss_fds[i], errp); 1349*6b230b7dSAndrew Melnychenko if (*errp) { 1350*6b230b7dSAndrew Melnychenko ret = false; 1351*6b230b7dSAndrew Melnychenko goto exit; 1352*6b230b7dSAndrew Melnychenko } 1353*6b230b7dSAndrew Melnychenko } 1354*6b230b7dSAndrew Melnychenko 1355*6b230b7dSAndrew Melnychenko ret = ebpf_rss_load_fds(&n->ebpf_rss, fds[0], fds[1], fds[2], fds[3]); 1356*6b230b7dSAndrew Melnychenko 1357*6b230b7dSAndrew Melnychenko exit: 1358*6b230b7dSAndrew Melnychenko if (!ret || *errp) { 1359*6b230b7dSAndrew Melnychenko for (i = 0; i < n->nr_ebpf_rss_fds && fds[i] != -1; i++) { 1360*6b230b7dSAndrew Melnychenko close(fds[i]); 1361*6b230b7dSAndrew Melnychenko } 1362*6b230b7dSAndrew Melnychenko } 1363*6b230b7dSAndrew Melnychenko 1364*6b230b7dSAndrew Melnychenko return ret; 1365*6b230b7dSAndrew Melnychenko } 1366*6b230b7dSAndrew Melnychenko 1367*6b230b7dSAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n, Error **errp) 1368*6b230b7dSAndrew Melnychenko { 1369*6b230b7dSAndrew Melnychenko bool ret = false; 1370*6b230b7dSAndrew Melnychenko 1371*6b230b7dSAndrew Melnychenko if (virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 1372*6b230b7dSAndrew Melnychenko if (!(n->ebpf_rss_fds 1373*6b230b7dSAndrew Melnychenko && virtio_net_load_ebpf_fds(n, errp))) { 1374*6b230b7dSAndrew Melnychenko ret = ebpf_rss_load(&n->ebpf_rss); 1375*6b230b7dSAndrew Melnychenko } 1376*6b230b7dSAndrew Melnychenko } 1377*6b230b7dSAndrew Melnychenko 1378*6b230b7dSAndrew Melnychenko return ret; 13790145c393SAndrew Melnychenko } 13800145c393SAndrew Melnychenko 13810145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 13820145c393SAndrew Melnychenko { 13830145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 13840145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 138559079029SYuri Benditovich } 138659079029SYuri Benditovich 138759079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1388e22f0603SYuri Benditovich struct iovec *iov, 1389e22f0603SYuri Benditovich unsigned int iov_cnt, 1390e22f0603SYuri Benditovich bool do_rss) 139159079029SYuri Benditovich { 139259079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 139359079029SYuri Benditovich struct virtio_net_rss_config cfg; 139459079029SYuri Benditovich size_t s, offset = 0, size_get; 1395441537f1SJason Wang uint16_t queue_pairs, i; 139659079029SYuri Benditovich struct { 139759079029SYuri Benditovich uint16_t us; 139859079029SYuri Benditovich uint8_t b; 139959079029SYuri Benditovich } QEMU_PACKED temp; 140059079029SYuri Benditovich const char *err_msg = ""; 140159079029SYuri Benditovich uint32_t err_value = 0; 140259079029SYuri Benditovich 1403e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 140459079029SYuri Benditovich err_msg = "RSS is not negotiated"; 140559079029SYuri Benditovich goto error; 140659079029SYuri Benditovich } 1407e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1408e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1409e22f0603SYuri Benditovich goto error; 1410e22f0603SYuri Benditovich } 141159079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 141259079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 141359079029SYuri Benditovich if (s != size_get) { 141459079029SYuri Benditovich err_msg = "Short command buffer"; 141559079029SYuri Benditovich err_value = (uint32_t)s; 141659079029SYuri Benditovich goto error; 141759079029SYuri Benditovich } 141859079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 141959079029SYuri Benditovich n->rss_data.indirections_len = 142059079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 142159079029SYuri Benditovich n->rss_data.indirections_len++; 1422e22f0603SYuri Benditovich if (!do_rss) { 1423e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1424e22f0603SYuri Benditovich } 142559079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 142659079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 142759079029SYuri Benditovich err_value = n->rss_data.indirections_len; 142859079029SYuri Benditovich goto error; 142959079029SYuri Benditovich } 143059079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 143159079029SYuri Benditovich err_msg = "Too large indirection table"; 143259079029SYuri Benditovich err_value = n->rss_data.indirections_len; 143359079029SYuri Benditovich goto error; 143459079029SYuri Benditovich } 1435e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1436e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1437441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 143859079029SYuri Benditovich err_msg = "Invalid default queue"; 143959079029SYuri Benditovich err_value = n->rss_data.default_queue; 144059079029SYuri Benditovich goto error; 144159079029SYuri Benditovich } 144259079029SYuri Benditovich offset += size_get; 144359079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 144459079029SYuri Benditovich g_free(n->rss_data.indirections_table); 144559079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 144659079029SYuri Benditovich if (!n->rss_data.indirections_table) { 144759079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 144859079029SYuri Benditovich err_value = n->rss_data.indirections_len; 144959079029SYuri Benditovich goto error; 145059079029SYuri Benditovich } 145159079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 145259079029SYuri Benditovich n->rss_data.indirections_table, size_get); 145359079029SYuri Benditovich if (s != size_get) { 145459079029SYuri Benditovich err_msg = "Short indirection table buffer"; 145559079029SYuri Benditovich err_value = (uint32_t)s; 145659079029SYuri Benditovich goto error; 145759079029SYuri Benditovich } 145859079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 145959079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 146059079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 146159079029SYuri Benditovich } 146259079029SYuri Benditovich offset += size_get; 146359079029SYuri Benditovich size_get = sizeof(temp); 146459079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 146559079029SYuri Benditovich if (s != size_get) { 1466441537f1SJason Wang err_msg = "Can't get queue_pairs"; 146759079029SYuri Benditovich err_value = (uint32_t)s; 146859079029SYuri Benditovich goto error; 146959079029SYuri Benditovich } 1470441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1471441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1472441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1473441537f1SJason Wang err_value = queue_pairs; 147459079029SYuri Benditovich goto error; 147559079029SYuri Benditovich } 147659079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 147759079029SYuri Benditovich err_msg = "Invalid key size"; 147859079029SYuri Benditovich err_value = temp.b; 147959079029SYuri Benditovich goto error; 148059079029SYuri Benditovich } 148159079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 148259079029SYuri Benditovich err_msg = "No key provided"; 148359079029SYuri Benditovich err_value = 0; 148459079029SYuri Benditovich goto error; 148559079029SYuri Benditovich } 148659079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 148759079029SYuri Benditovich virtio_net_disable_rss(n); 1488441537f1SJason Wang return queue_pairs; 148959079029SYuri Benditovich } 149059079029SYuri Benditovich offset += size_get; 149159079029SYuri Benditovich size_get = temp.b; 149259079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 149359079029SYuri Benditovich if (s != size_get) { 149459079029SYuri Benditovich err_msg = "Can get key buffer"; 149559079029SYuri Benditovich err_value = (uint32_t)s; 149659079029SYuri Benditovich goto error; 149759079029SYuri Benditovich } 149859079029SYuri Benditovich n->rss_data.enabled = true; 14990145c393SAndrew Melnychenko 15000145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 15010145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 15020145c393SAndrew Melnychenko /* EBPF must be loaded for vhost */ 15030145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 15040145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS for vhost"); 15050145c393SAndrew Melnychenko goto error; 15060145c393SAndrew Melnychenko } 15070145c393SAndrew Melnychenko /* fallback to software RSS */ 15080145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS - fallback to software RSS"); 15090145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 15100145c393SAndrew Melnychenko } 15110145c393SAndrew Melnychenko } else { 15120145c393SAndrew Melnychenko /* use software RSS for hash populating */ 15130145c393SAndrew Melnychenko /* and detach eBPF if was loaded before */ 15140145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 15150145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 15160145c393SAndrew Melnychenko } 15170145c393SAndrew Melnychenko 151859079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 151959079029SYuri Benditovich n->rss_data.indirections_len, 152059079029SYuri Benditovich temp.b); 1521441537f1SJason Wang return queue_pairs; 152259079029SYuri Benditovich error: 152359079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 152459079029SYuri Benditovich virtio_net_disable_rss(n); 152559079029SYuri Benditovich return 0; 152659079029SYuri Benditovich } 152759079029SYuri Benditovich 15286e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 15296e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 15306e790746SPaolo Bonzini { 153117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1532441537f1SJason Wang uint16_t queue_pairs; 15332a7888ccSSi-Wei Liu NetClientState *nc = qemu_get_queue(n->nic); 15346e790746SPaolo Bonzini 153559079029SYuri Benditovich virtio_net_disable_rss(n); 1536e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1537441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1538441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1539e22f0603SYuri Benditovich } 154059079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1541441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 154259079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 154359079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 154459079029SYuri Benditovich size_t s; 154559079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 154659079029SYuri Benditovich return VIRTIO_NET_ERR; 154759079029SYuri Benditovich } 15486e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 15496e790746SPaolo Bonzini if (s != sizeof(mq)) { 15506e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15516e790746SPaolo Bonzini } 1552441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 15536e790746SPaolo Bonzini 155459079029SYuri Benditovich } else { 15556e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15566e790746SPaolo Bonzini } 15576e790746SPaolo Bonzini 1558441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1559441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1560441537f1SJason Wang queue_pairs > n->max_queue_pairs || 15616e790746SPaolo Bonzini !n->multiqueue) { 15626e790746SPaolo Bonzini return VIRTIO_NET_ERR; 15636e790746SPaolo Bonzini } 15646e790746SPaolo Bonzini 1565441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1566ca8717f9SEugenio Pérez if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 1567ca8717f9SEugenio Pérez /* 1568ca8717f9SEugenio Pérez * Avoid updating the backend for a vdpa device: We're only interested 1569ca8717f9SEugenio Pérez * in updating the device model queues. 1570ca8717f9SEugenio Pérez */ 1571ca8717f9SEugenio Pérez return VIRTIO_NET_OK; 1572ca8717f9SEugenio Pérez } 1573441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 15746e790746SPaolo Bonzini * disabled queue */ 157517a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1576441537f1SJason Wang virtio_net_set_queue_pairs(n); 15776e790746SPaolo Bonzini 15786e790746SPaolo Bonzini return VIRTIO_NET_OK; 15796e790746SPaolo Bonzini } 1580ba7eadb5SGreg Kurz 1581640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, 1582640b8a1cSEugenio Pérez const struct iovec *in_sg, unsigned in_num, 1583640b8a1cSEugenio Pérez const struct iovec *out_sg, 1584640b8a1cSEugenio Pérez unsigned out_num) 15856e790746SPaolo Bonzini { 158617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 15876e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 15886e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 15896e790746SPaolo Bonzini size_t s; 1590771b6ed3SJason Wang struct iovec *iov, *iov2; 1591640b8a1cSEugenio Pérez 1592640b8a1cSEugenio Pérez if (iov_size(in_sg, in_num) < sizeof(status) || 1593640b8a1cSEugenio Pérez iov_size(out_sg, out_num) < sizeof(ctrl)) { 1594640b8a1cSEugenio Pérez virtio_error(vdev, "virtio-net ctrl missing headers"); 1595640b8a1cSEugenio Pérez return 0; 1596640b8a1cSEugenio Pérez } 1597640b8a1cSEugenio Pérez 1598640b8a1cSEugenio Pérez iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num); 1599640b8a1cSEugenio Pérez s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl)); 1600640b8a1cSEugenio Pérez iov_discard_front(&iov, &out_num, sizeof(ctrl)); 1601640b8a1cSEugenio Pérez if (s != sizeof(ctrl)) { 1602640b8a1cSEugenio Pérez status = VIRTIO_NET_ERR; 1603640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 1604640b8a1cSEugenio Pérez status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num); 1605640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 1606640b8a1cSEugenio Pérez status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num); 1607640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 1608640b8a1cSEugenio Pérez status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num); 1609640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1610640b8a1cSEugenio Pérez status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num); 1611640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 1612640b8a1cSEugenio Pérez status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num); 1613640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1614640b8a1cSEugenio Pérez status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num); 1615640b8a1cSEugenio Pérez } 1616640b8a1cSEugenio Pérez 1617640b8a1cSEugenio Pérez s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status)); 1618640b8a1cSEugenio Pérez assert(s == sizeof(status)); 1619640b8a1cSEugenio Pérez 1620640b8a1cSEugenio Pérez g_free(iov2); 1621640b8a1cSEugenio Pérez return sizeof(status); 1622640b8a1cSEugenio Pérez } 1623640b8a1cSEugenio Pérez 1624640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 1625640b8a1cSEugenio Pérez { 1626640b8a1cSEugenio Pérez VirtQueueElement *elem; 16276e790746SPaolo Bonzini 162851b19ebeSPaolo Bonzini for (;;) { 1629640b8a1cSEugenio Pérez size_t written; 163051b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 163151b19ebeSPaolo Bonzini if (!elem) { 163251b19ebeSPaolo Bonzini break; 163351b19ebeSPaolo Bonzini } 1634640b8a1cSEugenio Pérez 1635640b8a1cSEugenio Pérez written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num, 1636640b8a1cSEugenio Pérez elem->out_sg, elem->out_num); 1637640b8a1cSEugenio Pérez if (written > 0) { 1638640b8a1cSEugenio Pérez virtqueue_push(vq, elem, written); 1639640b8a1cSEugenio Pérez virtio_notify(vdev, vq); 1640640b8a1cSEugenio Pérez g_free(elem); 1641640b8a1cSEugenio Pérez } else { 1642ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1643ba7eadb5SGreg Kurz g_free(elem); 1644ba7eadb5SGreg Kurz break; 16456e790746SPaolo Bonzini } 16466e790746SPaolo Bonzini } 16476e790746SPaolo Bonzini } 16486e790746SPaolo Bonzini 16496e790746SPaolo Bonzini /* RX */ 16506e790746SPaolo Bonzini 16516e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 16526e790746SPaolo Bonzini { 165317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 16546e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 16556e790746SPaolo Bonzini 16566e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 16576e790746SPaolo Bonzini } 16586e790746SPaolo Bonzini 1659b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 16606e790746SPaolo Bonzini { 16616e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 166217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 16636e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 16646e790746SPaolo Bonzini 166517a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1666b8c4b67eSPhilippe Mathieu-Daudé return false; 16676e790746SPaolo Bonzini } 16686e790746SPaolo Bonzini 1669441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1670b8c4b67eSPhilippe Mathieu-Daudé return false; 16716e790746SPaolo Bonzini } 16726e790746SPaolo Bonzini 16736e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 167417a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1675b8c4b67eSPhilippe Mathieu-Daudé return false; 16766e790746SPaolo Bonzini } 16776e790746SPaolo Bonzini 1678b8c4b67eSPhilippe Mathieu-Daudé return true; 16796e790746SPaolo Bonzini } 16806e790746SPaolo Bonzini 16816e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 16826e790746SPaolo Bonzini { 16836e790746SPaolo Bonzini VirtIONet *n = q->n; 16846e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 16856e790746SPaolo Bonzini (n->mergeable_rx_bufs && 16866e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 16876e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 16886e790746SPaolo Bonzini 16896e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 16906e790746SPaolo Bonzini * available after the above check but before notification was 16916e790746SPaolo Bonzini * enabled, check for available buffers again. 16926e790746SPaolo Bonzini */ 16936e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 16946e790746SPaolo Bonzini (n->mergeable_rx_bufs && 16956e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 16966e790746SPaolo Bonzini return 0; 16976e790746SPaolo Bonzini } 16986e790746SPaolo Bonzini } 16996e790746SPaolo Bonzini 17006e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 17016e790746SPaolo Bonzini return 1; 17026e790746SPaolo Bonzini } 17036e790746SPaolo Bonzini 17041399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1705032a74a1SCédric Le Goater { 17061399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 17071399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 17081399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 17091399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1710032a74a1SCédric Le Goater } 1711032a74a1SCédric Le Goater 17126e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 17136e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 17146e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 17156e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 17166e790746SPaolo Bonzini * dhclient yet. 17176e790746SPaolo Bonzini * 17186e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 17196e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 17206e790746SPaolo Bonzini * kernels. 17216e790746SPaolo Bonzini * 17226e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 17236e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 17246e790746SPaolo Bonzini * cache. 17256e790746SPaolo Bonzini */ 17266e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 17276e790746SPaolo Bonzini uint8_t *buf, size_t size) 17286e790746SPaolo Bonzini { 17296e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 17306e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 17316e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 17326e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 17336e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1734f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 17356e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 17366e790746SPaolo Bonzini } 17376e790746SPaolo Bonzini } 17386e790746SPaolo Bonzini 17396e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 17406e790746SPaolo Bonzini const void *buf, size_t size) 17416e790746SPaolo Bonzini { 17426e790746SPaolo Bonzini if (n->has_vnet_hdr) { 17436e790746SPaolo Bonzini /* FIXME this cast is evil */ 17446e790746SPaolo Bonzini void *wbuf = (void *)buf; 17456e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 17466e790746SPaolo Bonzini size - n->host_hdr_len); 17471bfa316cSGreg Kurz 17481bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 17491399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 17501bfa316cSGreg Kurz } 17516e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 17526e790746SPaolo Bonzini } else { 17536e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 17546e790746SPaolo Bonzini .flags = 0, 17556e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 17566e790746SPaolo Bonzini }; 17576e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 17586e790746SPaolo Bonzini } 17596e790746SPaolo Bonzini } 17606e790746SPaolo Bonzini 17616e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 17626e790746SPaolo Bonzini { 17636e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 17646e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 17656e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 17666e790746SPaolo Bonzini int i; 17676e790746SPaolo Bonzini 17686e790746SPaolo Bonzini if (n->promisc) 17696e790746SPaolo Bonzini return 1; 17706e790746SPaolo Bonzini 17716e790746SPaolo Bonzini ptr += n->host_hdr_len; 17726e790746SPaolo Bonzini 17736e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 17747542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 17756e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 17766e790746SPaolo Bonzini return 0; 17776e790746SPaolo Bonzini } 17786e790746SPaolo Bonzini 17796e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 17806e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 17816e790746SPaolo Bonzini return !n->nobcast; 17826e790746SPaolo Bonzini } else if (n->nomulti) { 17836e790746SPaolo Bonzini return 0; 17846e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 17856e790746SPaolo Bonzini return 1; 17866e790746SPaolo Bonzini } 17876e790746SPaolo Bonzini 17886e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 17896e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17906e790746SPaolo Bonzini return 1; 17916e790746SPaolo Bonzini } 17926e790746SPaolo Bonzini } 17936e790746SPaolo Bonzini } else { // unicast 17946e790746SPaolo Bonzini if (n->nouni) { 17956e790746SPaolo Bonzini return 0; 17966e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 17976e790746SPaolo Bonzini return 1; 17986e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 17996e790746SPaolo Bonzini return 1; 18006e790746SPaolo Bonzini } 18016e790746SPaolo Bonzini 18026e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 18036e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 18046e790746SPaolo Bonzini return 1; 18056e790746SPaolo Bonzini } 18066e790746SPaolo Bonzini } 18076e790746SPaolo Bonzini } 18086e790746SPaolo Bonzini 18096e790746SPaolo Bonzini return 0; 18106e790746SPaolo Bonzini } 18116e790746SPaolo Bonzini 181269ff5ef8SAkihiko Odaki static uint8_t virtio_net_get_hash_type(bool hasip4, 181369ff5ef8SAkihiko Odaki bool hasip6, 181465f474bbSAkihiko Odaki EthL4HdrProto l4hdr_proto, 18154474e37aSYuri Benditovich uint32_t types) 18164474e37aSYuri Benditovich { 181769ff5ef8SAkihiko Odaki if (hasip4) { 181865f474bbSAkihiko Odaki switch (l4hdr_proto) { 181965f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_TCP: 182065f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { 18214474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 18224474e37aSYuri Benditovich } 182365f474bbSAkihiko Odaki break; 182465f474bbSAkihiko Odaki 182565f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_UDP: 182665f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { 18274474e37aSYuri Benditovich return NetPktRssIpV4Udp; 18284474e37aSYuri Benditovich } 182965f474bbSAkihiko Odaki break; 183065f474bbSAkihiko Odaki 183165f474bbSAkihiko Odaki default: 183265f474bbSAkihiko Odaki break; 183365f474bbSAkihiko Odaki } 183465f474bbSAkihiko Odaki 18354474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 18364474e37aSYuri Benditovich return NetPktRssIpV4; 18374474e37aSYuri Benditovich } 183869ff5ef8SAkihiko Odaki } else if (hasip6) { 183965f474bbSAkihiko Odaki switch (l4hdr_proto) { 184065f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_TCP: 184165f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) { 184265f474bbSAkihiko Odaki return NetPktRssIpV6TcpEx; 184365f474bbSAkihiko Odaki } 184465f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { 184565f474bbSAkihiko Odaki return NetPktRssIpV6Tcp; 184665f474bbSAkihiko Odaki } 184765f474bbSAkihiko Odaki break; 18484474e37aSYuri Benditovich 184965f474bbSAkihiko Odaki case ETH_L4_HDR_PROTO_UDP: 185065f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) { 185165f474bbSAkihiko Odaki return NetPktRssIpV6UdpEx; 18524474e37aSYuri Benditovich } 185365f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { 185465f474bbSAkihiko Odaki return NetPktRssIpV6Udp; 18554474e37aSYuri Benditovich } 185665f474bbSAkihiko Odaki break; 185765f474bbSAkihiko Odaki 185865f474bbSAkihiko Odaki default: 185965f474bbSAkihiko Odaki break; 186065f474bbSAkihiko Odaki } 186165f474bbSAkihiko Odaki 186265f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) { 186365f474bbSAkihiko Odaki return NetPktRssIpV6Ex; 186465f474bbSAkihiko Odaki } 186565f474bbSAkihiko Odaki if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { 186665f474bbSAkihiko Odaki return NetPktRssIpV6; 18674474e37aSYuri Benditovich } 18684474e37aSYuri Benditovich } 18694474e37aSYuri Benditovich return 0xff; 18704474e37aSYuri Benditovich } 18714474e37aSYuri Benditovich 1872e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1873e22f0603SYuri Benditovich uint32_t hash) 1874e22f0603SYuri Benditovich { 1875e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1876e22f0603SYuri Benditovich hdr->hash_value = hash; 1877e22f0603SYuri Benditovich hdr->hash_report = report; 1878e22f0603SYuri Benditovich } 1879e22f0603SYuri Benditovich 18804474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 188197cd965cSPaolo Bonzini size_t size) 18826e790746SPaolo Bonzini { 18836e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1884e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 18854474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 18864474e37aSYuri Benditovich uint8_t net_hash_type; 18874474e37aSYuri Benditovich uint32_t hash; 188865f474bbSAkihiko Odaki bool hasip4, hasip6; 188965f474bbSAkihiko Odaki EthL4HdrProto l4hdr_proto; 1890e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1891e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1892e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1893e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1894e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1895e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1896e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1897e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1898e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1899e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1900e22f0603SYuri Benditovich }; 19012f0fa232SAkihiko Odaki struct iovec iov = { 19022f0fa232SAkihiko Odaki .iov_base = (void *)buf, 19032f0fa232SAkihiko Odaki .iov_len = size 19042f0fa232SAkihiko Odaki }; 19054474e37aSYuri Benditovich 19062f0fa232SAkihiko Odaki net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len); 190765f474bbSAkihiko Odaki net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto); 190865f474bbSAkihiko Odaki net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto, 19094474e37aSYuri Benditovich n->rss_data.hash_types); 19104474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1911e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1912e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1913e22f0603SYuri Benditovich } 1914e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 19154474e37aSYuri Benditovich } 19164474e37aSYuri Benditovich 19174474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1918e22f0603SYuri Benditovich 1919e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1920e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1921e22f0603SYuri Benditovich } 1922e22f0603SYuri Benditovich 1923e22f0603SYuri Benditovich if (n->rss_data.redirect) { 19244474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 19254474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 19264474e37aSYuri Benditovich } 1927e22f0603SYuri Benditovich 1928e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 19294474e37aSYuri Benditovich } 19304474e37aSYuri Benditovich 19314474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 19324474e37aSYuri Benditovich size_t size, bool no_rss) 19334474e37aSYuri Benditovich { 19344474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 19356e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 193617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1937bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1938bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 19396e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 19406e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 19416e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1942bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1943bedd7e93SJason Wang ssize_t err; 19446e790746SPaolo Bonzini 19456e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 19466e790746SPaolo Bonzini return -1; 19476e790746SPaolo Bonzini } 19486e790746SPaolo Bonzini 19490145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 19504474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 19514474e37aSYuri Benditovich if (index >= 0) { 19524474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 19534474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 19544474e37aSYuri Benditovich } 19554474e37aSYuri Benditovich } 19564474e37aSYuri Benditovich 19576e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 19586e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 19596e790746SPaolo Bonzini return 0; 19606e790746SPaolo Bonzini } 19616e790746SPaolo Bonzini 19626e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 19636e790746SPaolo Bonzini return size; 19646e790746SPaolo Bonzini 19656e790746SPaolo Bonzini offset = i = 0; 19666e790746SPaolo Bonzini 19676e790746SPaolo Bonzini while (offset < size) { 196851b19ebeSPaolo Bonzini VirtQueueElement *elem; 19696e790746SPaolo Bonzini int len, total; 197051b19ebeSPaolo Bonzini const struct iovec *sg; 19716e790746SPaolo Bonzini 19726e790746SPaolo Bonzini total = 0; 19736e790746SPaolo Bonzini 1974bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1975bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1976bedd7e93SJason Wang err = size; 1977bedd7e93SJason Wang goto err; 1978bedd7e93SJason Wang } 1979bedd7e93SJason Wang 198051b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 198151b19ebeSPaolo Bonzini if (!elem) { 1982ba10b9c0SGreg Kurz if (i) { 1983ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 19846e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1985019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1986019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 19876e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1988019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1989019a3edbSGerd Hoffmann vdev->guest_features); 1990ba10b9c0SGreg Kurz } 1991bedd7e93SJason Wang err = -1; 1992bedd7e93SJason Wang goto err; 19936e790746SPaolo Bonzini } 19946e790746SPaolo Bonzini 199551b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1996ba10b9c0SGreg Kurz virtio_error(vdev, 1997ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1998ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1999ba10b9c0SGreg Kurz g_free(elem); 2000bedd7e93SJason Wang err = -1; 2001bedd7e93SJason Wang goto err; 20026e790746SPaolo Bonzini } 20036e790746SPaolo Bonzini 200451b19ebeSPaolo Bonzini sg = elem->in_sg; 20056e790746SPaolo Bonzini if (i == 0) { 20066e790746SPaolo Bonzini assert(offset == 0); 20076e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 20086e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 200951b19ebeSPaolo Bonzini sg, elem->in_num, 20106e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 20116e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 20126e790746SPaolo Bonzini } 20136e790746SPaolo Bonzini 201451b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 2015e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 2016e22f0603SYuri Benditovich offset = sizeof(mhdr); 2017e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 2018e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 2019e22f0603SYuri Benditovich } 20206e790746SPaolo Bonzini offset = n->host_hdr_len; 20216e790746SPaolo Bonzini total += n->guest_hdr_len; 20226e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 20236e790746SPaolo Bonzini } else { 20246e790746SPaolo Bonzini guest_offset = 0; 20256e790746SPaolo Bonzini } 20266e790746SPaolo Bonzini 20276e790746SPaolo Bonzini /* copy in packet. ugh */ 202851b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 20296e790746SPaolo Bonzini buf + offset, size - offset); 20306e790746SPaolo Bonzini total += len; 20316e790746SPaolo Bonzini offset += len; 20326e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 20336e790746SPaolo Bonzini * must have consumed the complete packet. 20346e790746SPaolo Bonzini * Otherwise, drop it. */ 20356e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 203627e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 203751b19ebeSPaolo Bonzini g_free(elem); 2038bedd7e93SJason Wang err = size; 2039bedd7e93SJason Wang goto err; 20406e790746SPaolo Bonzini } 20416e790746SPaolo Bonzini 2042bedd7e93SJason Wang elems[i] = elem; 2043bedd7e93SJason Wang lens[i] = total; 2044bedd7e93SJason Wang i++; 20456e790746SPaolo Bonzini } 20466e790746SPaolo Bonzini 20476e790746SPaolo Bonzini if (mhdr_cnt) { 20481399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 20496e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 20506e790746SPaolo Bonzini 0, 20516e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 20526e790746SPaolo Bonzini } 20536e790746SPaolo Bonzini 2054bedd7e93SJason Wang for (j = 0; j < i; j++) { 2055bedd7e93SJason Wang /* signal other side */ 2056bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 2057bedd7e93SJason Wang g_free(elems[j]); 2058bedd7e93SJason Wang } 2059bedd7e93SJason Wang 20606e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 206117a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 20626e790746SPaolo Bonzini 20636e790746SPaolo Bonzini return size; 2064bedd7e93SJason Wang 2065bedd7e93SJason Wang err: 2066bedd7e93SJason Wang for (j = 0; j < i; j++) { 2067abe300d9SJason Wang virtqueue_detach_element(q->rx_vq, elems[j], lens[j]); 2068bedd7e93SJason Wang g_free(elems[j]); 2069bedd7e93SJason Wang } 2070bedd7e93SJason Wang 2071bedd7e93SJason Wang return err; 20726e790746SPaolo Bonzini } 20736e790746SPaolo Bonzini 20742974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 207597cd965cSPaolo Bonzini size_t size) 207697cd965cSPaolo Bonzini { 2077068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 207897cd965cSPaolo Bonzini 20794474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 208097cd965cSPaolo Bonzini } 208197cd965cSPaolo Bonzini 20822974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 20832974e916SYuri Benditovich const uint8_t *buf, 20842974e916SYuri Benditovich VirtioNetRscUnit *unit) 20852974e916SYuri Benditovich { 20862974e916SYuri Benditovich uint16_t ip_hdrlen; 20872974e916SYuri Benditovich struct ip_header *ip; 20882974e916SYuri Benditovich 20892974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 20902974e916SYuri Benditovich + sizeof(struct eth_header)); 20912974e916SYuri Benditovich unit->ip = (void *)ip; 20922974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 20932974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 20942974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 20952974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 20962974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 20972974e916SYuri Benditovich } 20982974e916SYuri Benditovich 20992974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 21002974e916SYuri Benditovich const uint8_t *buf, 21012974e916SYuri Benditovich VirtioNetRscUnit *unit) 21022974e916SYuri Benditovich { 21032974e916SYuri Benditovich struct ip6_header *ip6; 21042974e916SYuri Benditovich 21052974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 21062974e916SYuri Benditovich + sizeof(struct eth_header)); 21072974e916SYuri Benditovich unit->ip = ip6; 21082974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 210978ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 21102974e916SYuri Benditovich + sizeof(struct ip6_header)); 21112974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 21122974e916SYuri Benditovich 21132431f4f1SMichael Tokarev /* There is a difference between payload length in ipv4 and v6, 21142974e916SYuri Benditovich ip header is excluded in ipv6 */ 21152974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 21162974e916SYuri Benditovich } 21172974e916SYuri Benditovich 21182974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 21192974e916SYuri Benditovich VirtioNetRscSeg *seg) 21202974e916SYuri Benditovich { 21212974e916SYuri Benditovich int ret; 2122dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 21232974e916SYuri Benditovich 2124dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 21252974e916SYuri Benditovich h->flags = 0; 21262974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 21272974e916SYuri Benditovich 21282974e916SYuri Benditovich if (seg->is_coalesced) { 2129dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 2130dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 21312974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 21322974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 21332974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 21342974e916SYuri Benditovich } else { 21352974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 21362974e916SYuri Benditovich } 21372974e916SYuri Benditovich } 21382974e916SYuri Benditovich 21392974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 21402974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 21412974e916SYuri Benditovich g_free(seg->buf); 21422974e916SYuri Benditovich g_free(seg); 21432974e916SYuri Benditovich 21442974e916SYuri Benditovich return ret; 21452974e916SYuri Benditovich } 21462974e916SYuri Benditovich 21472974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 21482974e916SYuri Benditovich { 21492974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 21502974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 21512974e916SYuri Benditovich 21522974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 21532974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 21542974e916SYuri Benditovich chain->stat.purge_failed++; 21552974e916SYuri Benditovich continue; 21562974e916SYuri Benditovich } 21572974e916SYuri Benditovich } 21582974e916SYuri Benditovich 21592974e916SYuri Benditovich chain->stat.timer++; 21602974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 21612974e916SYuri Benditovich timer_mod(chain->drain_timer, 21622974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 21632974e916SYuri Benditovich } 21642974e916SYuri Benditovich } 21652974e916SYuri Benditovich 21662974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 21672974e916SYuri Benditovich { 21682974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 21692974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 21702974e916SYuri Benditovich 21712974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 21722974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 21732974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 21742974e916SYuri Benditovich g_free(seg->buf); 21752974e916SYuri Benditovich g_free(seg); 21762974e916SYuri Benditovich } 21772974e916SYuri Benditovich 21782974e916SYuri Benditovich timer_free(chain->drain_timer); 21792974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 21802974e916SYuri Benditovich g_free(chain); 21812974e916SYuri Benditovich } 21822974e916SYuri Benditovich } 21832974e916SYuri Benditovich 21842974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 21852974e916SYuri Benditovich NetClientState *nc, 21862974e916SYuri Benditovich const uint8_t *buf, size_t size) 21872974e916SYuri Benditovich { 21882974e916SYuri Benditovich uint16_t hdr_len; 21892974e916SYuri Benditovich VirtioNetRscSeg *seg; 21902974e916SYuri Benditovich 21912974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 2192b21e2380SMarkus Armbruster seg = g_new(VirtioNetRscSeg, 1); 21932974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 21942974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 21952974e916SYuri Benditovich memcpy(seg->buf, buf, size); 21962974e916SYuri Benditovich seg->size = size; 21972974e916SYuri Benditovich seg->packets = 1; 21982974e916SYuri Benditovich seg->dup_ack = 0; 21992974e916SYuri Benditovich seg->is_coalesced = 0; 22002974e916SYuri Benditovich seg->nc = nc; 22012974e916SYuri Benditovich 22022974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 22032974e916SYuri Benditovich chain->stat.cache++; 22042974e916SYuri Benditovich 22052974e916SYuri Benditovich switch (chain->proto) { 22062974e916SYuri Benditovich case ETH_P_IP: 22072974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 22082974e916SYuri Benditovich break; 22092974e916SYuri Benditovich case ETH_P_IPV6: 22102974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 22112974e916SYuri Benditovich break; 22122974e916SYuri Benditovich default: 22132974e916SYuri Benditovich g_assert_not_reached(); 22142974e916SYuri Benditovich } 22152974e916SYuri Benditovich } 22162974e916SYuri Benditovich 22172974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 22182974e916SYuri Benditovich VirtioNetRscSeg *seg, 22192974e916SYuri Benditovich const uint8_t *buf, 22202974e916SYuri Benditovich struct tcp_header *n_tcp, 22212974e916SYuri Benditovich struct tcp_header *o_tcp) 22222974e916SYuri Benditovich { 22232974e916SYuri Benditovich uint32_t nack, oack; 22242974e916SYuri Benditovich uint16_t nwin, owin; 22252974e916SYuri Benditovich 22262974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 22272974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 22282974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 22292974e916SYuri Benditovich owin = htons(o_tcp->th_win); 22302974e916SYuri Benditovich 22312974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 22322974e916SYuri Benditovich chain->stat.ack_out_of_win++; 22332974e916SYuri Benditovich return RSC_FINAL; 22342974e916SYuri Benditovich } else if (nack == oack) { 22352974e916SYuri Benditovich /* duplicated ack or window probe */ 22362974e916SYuri Benditovich if (nwin == owin) { 22372974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 22382974e916SYuri Benditovich chain->stat.dup_ack++; 22392974e916SYuri Benditovich return RSC_FINAL; 22402974e916SYuri Benditovich } else { 22412974e916SYuri Benditovich /* Coalesce window update */ 22422974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 22432974e916SYuri Benditovich chain->stat.win_update++; 22442974e916SYuri Benditovich return RSC_COALESCE; 22452974e916SYuri Benditovich } 22462974e916SYuri Benditovich } else { 22472974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 22482974e916SYuri Benditovich chain->stat.pure_ack++; 22492974e916SYuri Benditovich return RSC_FINAL; 22502974e916SYuri Benditovich } 22512974e916SYuri Benditovich } 22522974e916SYuri Benditovich 22532974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 22542974e916SYuri Benditovich VirtioNetRscSeg *seg, 22552974e916SYuri Benditovich const uint8_t *buf, 22562974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 22572974e916SYuri Benditovich { 22582974e916SYuri Benditovich void *data; 22592974e916SYuri Benditovich uint16_t o_ip_len; 22602974e916SYuri Benditovich uint32_t nseq, oseq; 22612974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 22622974e916SYuri Benditovich 22632974e916SYuri Benditovich o_unit = &seg->unit; 22642974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 22652974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 22662974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 22672974e916SYuri Benditovich 22682974e916SYuri Benditovich /* out of order or retransmitted. */ 22692974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 22702974e916SYuri Benditovich chain->stat.data_out_of_win++; 22712974e916SYuri Benditovich return RSC_FINAL; 22722974e916SYuri Benditovich } 22732974e916SYuri Benditovich 22742974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 22752974e916SYuri Benditovich if (nseq == oseq) { 22762974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 22772974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 22782974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 22792974e916SYuri Benditovich goto coalesce; 22802974e916SYuri Benditovich } else { 22812974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 22822974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 22832974e916SYuri Benditovich } 22842974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 22852974e916SYuri Benditovich /* Not a consistent packet, out of order */ 22862974e916SYuri Benditovich chain->stat.data_out_of_order++; 22872974e916SYuri Benditovich return RSC_FINAL; 22882974e916SYuri Benditovich } else { 22892974e916SYuri Benditovich coalesce: 22902974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 22912974e916SYuri Benditovich chain->stat.over_size++; 22922974e916SYuri Benditovich return RSC_FINAL; 22932974e916SYuri Benditovich } 22942974e916SYuri Benditovich 22952974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 22962974e916SYuri Benditovich so use the field value to update and record the new data len */ 22972974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 22982974e916SYuri Benditovich 22992974e916SYuri Benditovich /* update field in ip header */ 23002974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 23012974e916SYuri Benditovich 23022974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 23032974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 23042974e916SYuri Benditovich guest (only if it uses RSC feature). */ 23052974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 23062974e916SYuri Benditovich 23072974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 23082974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 23092974e916SYuri Benditovich 23102974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 23112974e916SYuri Benditovich seg->size += n_unit->payload; 23122974e916SYuri Benditovich seg->packets++; 23132974e916SYuri Benditovich chain->stat.coalesced++; 23142974e916SYuri Benditovich return RSC_COALESCE; 23152974e916SYuri Benditovich } 23162974e916SYuri Benditovich } 23172974e916SYuri Benditovich 23182974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 23192974e916SYuri Benditovich VirtioNetRscSeg *seg, 23202974e916SYuri Benditovich const uint8_t *buf, size_t size, 23212974e916SYuri Benditovich VirtioNetRscUnit *unit) 23222974e916SYuri Benditovich { 23232974e916SYuri Benditovich struct ip_header *ip1, *ip2; 23242974e916SYuri Benditovich 23252974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 23262974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 23272974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 23282974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 23292974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 23302974e916SYuri Benditovich chain->stat.no_match++; 23312974e916SYuri Benditovich return RSC_NO_MATCH; 23322974e916SYuri Benditovich } 23332974e916SYuri Benditovich 23342974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 23352974e916SYuri Benditovich } 23362974e916SYuri Benditovich 23372974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 23382974e916SYuri Benditovich VirtioNetRscSeg *seg, 23392974e916SYuri Benditovich const uint8_t *buf, size_t size, 23402974e916SYuri Benditovich VirtioNetRscUnit *unit) 23412974e916SYuri Benditovich { 23422974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 23432974e916SYuri Benditovich 23442974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 23452974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 23462974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 23472974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 23482974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 23492974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 23502974e916SYuri Benditovich chain->stat.no_match++; 23512974e916SYuri Benditovich return RSC_NO_MATCH; 23522974e916SYuri Benditovich } 23532974e916SYuri Benditovich 23542974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 23552974e916SYuri Benditovich } 23562974e916SYuri Benditovich 23572974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 23582974e916SYuri Benditovich * to prevent out of order */ 23592974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 23602974e916SYuri Benditovich struct tcp_header *tcp) 23612974e916SYuri Benditovich { 23622974e916SYuri Benditovich uint16_t tcp_hdr; 23632974e916SYuri Benditovich uint16_t tcp_flag; 23642974e916SYuri Benditovich 23652974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 23662974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 23672974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 23682974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 23692974e916SYuri Benditovich chain->stat.tcp_syn++; 23702974e916SYuri Benditovich return RSC_BYPASS; 23712974e916SYuri Benditovich } 23722974e916SYuri Benditovich 23732974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 23742974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 23752974e916SYuri Benditovich return RSC_FINAL; 23762974e916SYuri Benditovich } 23772974e916SYuri Benditovich 23782974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 23792974e916SYuri Benditovich chain->stat.tcp_all_opt++; 23802974e916SYuri Benditovich return RSC_FINAL; 23812974e916SYuri Benditovich } 23822974e916SYuri Benditovich 23832974e916SYuri Benditovich return RSC_CANDIDATE; 23842974e916SYuri Benditovich } 23852974e916SYuri Benditovich 23862974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 23872974e916SYuri Benditovich NetClientState *nc, 23882974e916SYuri Benditovich const uint8_t *buf, size_t size, 23892974e916SYuri Benditovich VirtioNetRscUnit *unit) 23902974e916SYuri Benditovich { 23912974e916SYuri Benditovich int ret; 23922974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 23932974e916SYuri Benditovich 23942974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 23952974e916SYuri Benditovich chain->stat.empty_cache++; 23962974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 23972974e916SYuri Benditovich timer_mod(chain->drain_timer, 23982974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 23992974e916SYuri Benditovich return size; 24002974e916SYuri Benditovich } 24012974e916SYuri Benditovich 24022974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 24032974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 24042974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 24052974e916SYuri Benditovich } else { 24062974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 24072974e916SYuri Benditovich } 24082974e916SYuri Benditovich 24092974e916SYuri Benditovich if (ret == RSC_FINAL) { 24102974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 24112974e916SYuri Benditovich /* Send failed */ 24122974e916SYuri Benditovich chain->stat.final_failed++; 24132974e916SYuri Benditovich return 0; 24142974e916SYuri Benditovich } 24152974e916SYuri Benditovich 24162974e916SYuri Benditovich /* Send current packet */ 24172974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24182974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 24192974e916SYuri Benditovich continue; 24202974e916SYuri Benditovich } else { 24212974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 24222974e916SYuri Benditovich seg->is_coalesced = 1; 24232974e916SYuri Benditovich return size; 24242974e916SYuri Benditovich } 24252974e916SYuri Benditovich } 24262974e916SYuri Benditovich 24272974e916SYuri Benditovich chain->stat.no_match_cache++; 24282974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 24292974e916SYuri Benditovich return size; 24302974e916SYuri Benditovich } 24312974e916SYuri Benditovich 24322974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 24332974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 24342974e916SYuri Benditovich NetClientState *nc, 24352974e916SYuri Benditovich const uint8_t *buf, size_t size, 24362974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 24372974e916SYuri Benditovich uint16_t tcp_port) 24382974e916SYuri Benditovich { 24392974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 24402974e916SYuri Benditovich uint32_t ppair1, ppair2; 24412974e916SYuri Benditovich 24422974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 24432974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 24442974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 24452974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 24462974e916SYuri Benditovich || (ppair1 != ppair2)) { 24472974e916SYuri Benditovich continue; 24482974e916SYuri Benditovich } 24492974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 24502974e916SYuri Benditovich chain->stat.drain_failed++; 24512974e916SYuri Benditovich } 24522974e916SYuri Benditovich 24532974e916SYuri Benditovich break; 24542974e916SYuri Benditovich } 24552974e916SYuri Benditovich 24562974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24572974e916SYuri Benditovich } 24582974e916SYuri Benditovich 24592974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 24602974e916SYuri Benditovich struct ip_header *ip, 24612974e916SYuri Benditovich const uint8_t *buf, size_t size) 24622974e916SYuri Benditovich { 24632974e916SYuri Benditovich uint16_t ip_len; 24642974e916SYuri Benditovich 24652974e916SYuri Benditovich /* Not an ipv4 packet */ 24662974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 24672974e916SYuri Benditovich chain->stat.ip_option++; 24682974e916SYuri Benditovich return RSC_BYPASS; 24692974e916SYuri Benditovich } 24702974e916SYuri Benditovich 24712974e916SYuri Benditovich /* Don't handle packets with ip option */ 24722974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 24732974e916SYuri Benditovich chain->stat.ip_option++; 24742974e916SYuri Benditovich return RSC_BYPASS; 24752974e916SYuri Benditovich } 24762974e916SYuri Benditovich 24772974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 24782974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24792974e916SYuri Benditovich return RSC_BYPASS; 24802974e916SYuri Benditovich } 24812974e916SYuri Benditovich 24822974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 24832974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 24842974e916SYuri Benditovich chain->stat.ip_frag++; 24852974e916SYuri Benditovich return RSC_BYPASS; 24862974e916SYuri Benditovich } 24872974e916SYuri Benditovich 24882974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 24892974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 24902974e916SYuri Benditovich chain->stat.ip_ecn++; 24912974e916SYuri Benditovich return RSC_BYPASS; 24922974e916SYuri Benditovich } 24932974e916SYuri Benditovich 24942974e916SYuri Benditovich ip_len = htons(ip->ip_len); 24952974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 24962974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 24972974e916SYuri Benditovich sizeof(struct eth_header))) { 24982974e916SYuri Benditovich chain->stat.ip_hacked++; 24992974e916SYuri Benditovich return RSC_BYPASS; 25002974e916SYuri Benditovich } 25012974e916SYuri Benditovich 25022974e916SYuri Benditovich return RSC_CANDIDATE; 25032974e916SYuri Benditovich } 25042974e916SYuri Benditovich 25052974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 25062974e916SYuri Benditovich NetClientState *nc, 25072974e916SYuri Benditovich const uint8_t *buf, size_t size) 25082974e916SYuri Benditovich { 25092974e916SYuri Benditovich int32_t ret; 25102974e916SYuri Benditovich uint16_t hdr_len; 25112974e916SYuri Benditovich VirtioNetRscUnit unit; 25122974e916SYuri Benditovich 25132974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 25142974e916SYuri Benditovich 25152974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 25162974e916SYuri Benditovich + sizeof(struct tcp_header))) { 25172974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 25182974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25192974e916SYuri Benditovich } 25202974e916SYuri Benditovich 25212974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 25222974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 25232974e916SYuri Benditovich != RSC_CANDIDATE) { 25242974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25252974e916SYuri Benditovich } 25262974e916SYuri Benditovich 25272974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 25282974e916SYuri Benditovich if (ret == RSC_BYPASS) { 25292974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25302974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 25312974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 25322974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 25332974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 25342974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 25352974e916SYuri Benditovich } 25362974e916SYuri Benditovich 25372974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 25382974e916SYuri Benditovich } 25392974e916SYuri Benditovich 25402974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 25412974e916SYuri Benditovich struct ip6_header *ip6, 25422974e916SYuri Benditovich const uint8_t *buf, size_t size) 25432974e916SYuri Benditovich { 25442974e916SYuri Benditovich uint16_t ip_len; 25452974e916SYuri Benditovich 25462974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 25472974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 25482974e916SYuri Benditovich return RSC_BYPASS; 25492974e916SYuri Benditovich } 25502974e916SYuri Benditovich 25512974e916SYuri Benditovich /* Both option and protocol is checked in this */ 25522974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 25532974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 25542974e916SYuri Benditovich return RSC_BYPASS; 25552974e916SYuri Benditovich } 25562974e916SYuri Benditovich 25572974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 25582974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 25592974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 25602974e916SYuri Benditovich - sizeof(struct ip6_header))) { 25612974e916SYuri Benditovich chain->stat.ip_hacked++; 25622974e916SYuri Benditovich return RSC_BYPASS; 25632974e916SYuri Benditovich } 25642974e916SYuri Benditovich 25652974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 25662974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 25672974e916SYuri Benditovich chain->stat.ip_ecn++; 25682974e916SYuri Benditovich return RSC_BYPASS; 25692974e916SYuri Benditovich } 25702974e916SYuri Benditovich 25712974e916SYuri Benditovich return RSC_CANDIDATE; 25722974e916SYuri Benditovich } 25732974e916SYuri Benditovich 25742974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 25752974e916SYuri Benditovich const uint8_t *buf, size_t size) 25762974e916SYuri Benditovich { 25772974e916SYuri Benditovich int32_t ret; 25782974e916SYuri Benditovich uint16_t hdr_len; 25792974e916SYuri Benditovich VirtioNetRscChain *chain; 25802974e916SYuri Benditovich VirtioNetRscUnit unit; 25812974e916SYuri Benditovich 25823d558330SMarkus Armbruster chain = opq; 25832974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 25842974e916SYuri Benditovich 25852974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 25862974e916SYuri Benditovich + sizeof(tcp_header))) { 25872974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25882974e916SYuri Benditovich } 25892974e916SYuri Benditovich 25902974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 25912974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 25922974e916SYuri Benditovich unit.ip, buf, size)) { 25932974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25942974e916SYuri Benditovich } 25952974e916SYuri Benditovich 25962974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 25972974e916SYuri Benditovich if (ret == RSC_BYPASS) { 25982974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25992974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 26002974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 26012974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 26022974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 26032974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 26042974e916SYuri Benditovich + sizeof(struct ip6_header)); 26052974e916SYuri Benditovich } 26062974e916SYuri Benditovich 26072974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 26082974e916SYuri Benditovich } 26092974e916SYuri Benditovich 26102974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 26112974e916SYuri Benditovich NetClientState *nc, 26122974e916SYuri Benditovich uint16_t proto) 26132974e916SYuri Benditovich { 26142974e916SYuri Benditovich VirtioNetRscChain *chain; 26152974e916SYuri Benditovich 26162974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 26172974e916SYuri Benditovich return NULL; 26182974e916SYuri Benditovich } 26192974e916SYuri Benditovich 26202974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 26212974e916SYuri Benditovich if (chain->proto == proto) { 26222974e916SYuri Benditovich return chain; 26232974e916SYuri Benditovich } 26242974e916SYuri Benditovich } 26252974e916SYuri Benditovich 26262974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 26272974e916SYuri Benditovich chain->n = n; 26282974e916SYuri Benditovich chain->proto = proto; 26292974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 26302974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 26312974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 26322974e916SYuri Benditovich } else { 26332974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 26342974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 26352974e916SYuri Benditovich } 26362974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 26372974e916SYuri Benditovich virtio_net_rsc_purge, chain); 26382974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 26392974e916SYuri Benditovich 26402974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 26412974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 26422974e916SYuri Benditovich 26432974e916SYuri Benditovich return chain; 26442974e916SYuri Benditovich } 26452974e916SYuri Benditovich 26462974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 26472974e916SYuri Benditovich const uint8_t *buf, 26482974e916SYuri Benditovich size_t size) 26492974e916SYuri Benditovich { 26502974e916SYuri Benditovich uint16_t proto; 26512974e916SYuri Benditovich VirtioNetRscChain *chain; 26522974e916SYuri Benditovich struct eth_header *eth; 26532974e916SYuri Benditovich VirtIONet *n; 26542974e916SYuri Benditovich 26552974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 26562974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 26572974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26582974e916SYuri Benditovich } 26592974e916SYuri Benditovich 26602974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 26612974e916SYuri Benditovich proto = htons(eth->h_proto); 26622974e916SYuri Benditovich 26632974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 26642974e916SYuri Benditovich if (chain) { 26652974e916SYuri Benditovich chain->stat.received++; 26662974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 26672974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 26682974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 26692974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 26702974e916SYuri Benditovich } 26712974e916SYuri Benditovich } 26722974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26732974e916SYuri Benditovich } 26742974e916SYuri Benditovich 26752974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 26762974e916SYuri Benditovich size_t size) 26772974e916SYuri Benditovich { 26782974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 26792974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 26802974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 26812974e916SYuri Benditovich } else { 26822974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 26832974e916SYuri Benditovich } 26842974e916SYuri Benditovich } 26852974e916SYuri Benditovich 26866e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 26876e790746SPaolo Bonzini 26886e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 26896e790746SPaolo Bonzini { 26906e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 26916e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 269217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2693df8d0708SLaurent Vivier int ret; 26946e790746SPaolo Bonzini 269551b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 269617a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 26976e790746SPaolo Bonzini 269851b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 269951b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 27006e790746SPaolo Bonzini 27016e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2702df8d0708SLaurent Vivier ret = virtio_net_flush_tx(q); 27037550a822SLaurent Vivier if (ret >= n->tx_burst) { 2704df8d0708SLaurent Vivier /* 2705df8d0708SLaurent Vivier * the flush has been stopped by tx_burst 2706df8d0708SLaurent Vivier * we will not receive notification for the 2707df8d0708SLaurent Vivier * remainining part, so re-schedule 2708df8d0708SLaurent Vivier */ 2709df8d0708SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 27107550a822SLaurent Vivier if (q->tx_bh) { 2711df8d0708SLaurent Vivier qemu_bh_schedule(q->tx_bh); 27127550a822SLaurent Vivier } else { 27137550a822SLaurent Vivier timer_mod(q->tx_timer, 27147550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27157550a822SLaurent Vivier } 2716df8d0708SLaurent Vivier q->tx_waiting = 1; 2717df8d0708SLaurent Vivier } 27186e790746SPaolo Bonzini } 27196e790746SPaolo Bonzini 27206e790746SPaolo Bonzini /* TX */ 27216e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 27226e790746SPaolo Bonzini { 27236e790746SPaolo Bonzini VirtIONet *n = q->n; 272417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 272551b19ebeSPaolo Bonzini VirtQueueElement *elem; 27266e790746SPaolo Bonzini int32_t num_packets = 0; 27276e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 272817a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 27296e790746SPaolo Bonzini return num_packets; 27306e790746SPaolo Bonzini } 27316e790746SPaolo Bonzini 273251b19ebeSPaolo Bonzini if (q->async_tx.elem) { 27336e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 27346e790746SPaolo Bonzini return num_packets; 27356e790746SPaolo Bonzini } 27366e790746SPaolo Bonzini 273751b19ebeSPaolo Bonzini for (;;) { 2738bd89dd98SJason Wang ssize_t ret; 273951b19ebeSPaolo Bonzini unsigned int out_num; 274051b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 27412220e818SJason Wang struct virtio_net_hdr_v1_hash vhdr; 27426e790746SPaolo Bonzini 274351b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 274451b19ebeSPaolo Bonzini if (!elem) { 274551b19ebeSPaolo Bonzini break; 274651b19ebeSPaolo Bonzini } 274751b19ebeSPaolo Bonzini 274851b19ebeSPaolo Bonzini out_num = elem->out_num; 274951b19ebeSPaolo Bonzini out_sg = elem->out_sg; 27506e790746SPaolo Bonzini if (out_num < 1) { 2751fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2752fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2753fa5e56c2SGreg Kurz g_free(elem); 2754fa5e56c2SGreg Kurz return -EINVAL; 27556e790746SPaolo Bonzini } 27566e790746SPaolo Bonzini 2757032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 27582220e818SJason Wang if (iov_to_buf(out_sg, out_num, 0, &vhdr, n->guest_hdr_len) < 2759feb93f36SJason Wang n->guest_hdr_len) { 2760fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2761fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2762fa5e56c2SGreg Kurz g_free(elem); 2763fa5e56c2SGreg Kurz return -EINVAL; 2764032a74a1SCédric Le Goater } 27651bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 27662220e818SJason Wang virtio_net_hdr_swap(vdev, (void *) &vhdr); 27672220e818SJason Wang sg2[0].iov_base = &vhdr; 2768feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2769feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2770feb93f36SJason Wang out_sg, out_num, 2771feb93f36SJason Wang n->guest_hdr_len, -1); 2772feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2773feb93f36SJason Wang goto drop; 2774032a74a1SCédric Le Goater } 2775feb93f36SJason Wang out_num += 1; 2776feb93f36SJason Wang out_sg = sg2; 2777feb93f36SJason Wang } 2778feb93f36SJason Wang } 27796e790746SPaolo Bonzini /* 27806e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 27816e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 27826e790746SPaolo Bonzini * that host is interested in. 27836e790746SPaolo Bonzini */ 27846e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 27856e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 27866e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 27876e790746SPaolo Bonzini out_sg, out_num, 27886e790746SPaolo Bonzini 0, n->host_hdr_len); 27896e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 27906e790746SPaolo Bonzini out_sg, out_num, 27916e790746SPaolo Bonzini n->guest_hdr_len, -1); 27926e790746SPaolo Bonzini out_num = sg_num; 27936e790746SPaolo Bonzini out_sg = sg; 27946e790746SPaolo Bonzini } 27956e790746SPaolo Bonzini 27966e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 27976e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 27986e790746SPaolo Bonzini if (ret == 0) { 27996e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 28006e790746SPaolo Bonzini q->async_tx.elem = elem; 28016e790746SPaolo Bonzini return -EBUSY; 28026e790746SPaolo Bonzini } 28036e790746SPaolo Bonzini 2804feb93f36SJason Wang drop: 280551b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 280617a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 280751b19ebeSPaolo Bonzini g_free(elem); 28086e790746SPaolo Bonzini 28096e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 28106e790746SPaolo Bonzini break; 28116e790746SPaolo Bonzini } 28126e790746SPaolo Bonzini } 28136e790746SPaolo Bonzini return num_packets; 28146e790746SPaolo Bonzini } 28156e790746SPaolo Bonzini 28167550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque); 28177550a822SLaurent Vivier 28186e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(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 2823283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2824283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2825283e2c2aSYuri Benditovich return; 2826283e2c2aSYuri Benditovich } 2827283e2c2aSYuri Benditovich 28286e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 282917a0ca55SKONRAD Frederic if (!vdev->vm_running) { 28306e790746SPaolo Bonzini q->tx_waiting = 1; 28316e790746SPaolo Bonzini return; 28326e790746SPaolo Bonzini } 28336e790746SPaolo Bonzini 28346e790746SPaolo Bonzini if (q->tx_waiting) { 28357550a822SLaurent Vivier /* We already have queued packets, immediately flush */ 2836bc72ad67SAlex Bligh timer_del(q->tx_timer); 28377550a822SLaurent Vivier virtio_net_tx_timer(q); 28386e790746SPaolo Bonzini } else { 28397550a822SLaurent Vivier /* re-arm timer to flush it (and more) on next tick */ 2840bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2841bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 28426e790746SPaolo Bonzini q->tx_waiting = 1; 28436e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 28446e790746SPaolo Bonzini } 28456e790746SPaolo Bonzini } 28466e790746SPaolo Bonzini 28476e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 28486e790746SPaolo Bonzini { 284917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 28506e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 28516e790746SPaolo Bonzini 2852283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2853283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2854283e2c2aSYuri Benditovich return; 2855283e2c2aSYuri Benditovich } 2856283e2c2aSYuri Benditovich 28576e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 28586e790746SPaolo Bonzini return; 28596e790746SPaolo Bonzini } 28606e790746SPaolo Bonzini q->tx_waiting = 1; 28616e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 286217a0ca55SKONRAD Frederic if (!vdev->vm_running) { 28636e790746SPaolo Bonzini return; 28646e790746SPaolo Bonzini } 28656e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 28666e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28676e790746SPaolo Bonzini } 28686e790746SPaolo Bonzini 28696e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 28706e790746SPaolo Bonzini { 28716e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 28726e790746SPaolo Bonzini VirtIONet *n = q->n; 287317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 28747550a822SLaurent Vivier int ret; 28757550a822SLaurent Vivier 2876e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2877e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2878e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2879e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2880e8bcf842SMichael S. Tsirkin return; 2881e8bcf842SMichael S. Tsirkin } 28826e790746SPaolo Bonzini 28836e790746SPaolo Bonzini q->tx_waiting = 0; 28846e790746SPaolo Bonzini 28856e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 288617a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 28876e790746SPaolo Bonzini return; 288817a0ca55SKONRAD Frederic } 28896e790746SPaolo Bonzini 28907550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 28917550a822SLaurent Vivier if (ret == -EBUSY || ret == -EINVAL) { 28927550a822SLaurent Vivier return; 28937550a822SLaurent Vivier } 28947550a822SLaurent Vivier /* 28957550a822SLaurent Vivier * If we flush a full burst of packets, assume there are 28967550a822SLaurent Vivier * more coming and immediately rearm 28977550a822SLaurent Vivier */ 28987550a822SLaurent Vivier if (ret >= n->tx_burst) { 28997550a822SLaurent Vivier q->tx_waiting = 1; 29007550a822SLaurent Vivier timer_mod(q->tx_timer, 29017550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 29027550a822SLaurent Vivier return; 29037550a822SLaurent Vivier } 29047550a822SLaurent Vivier /* 29057550a822SLaurent Vivier * If less than a full burst, re-enable notification and flush 29067550a822SLaurent Vivier * anything that may have come in while we weren't looking. If 29077550a822SLaurent Vivier * we find something, assume the guest is still active and rearm 29087550a822SLaurent Vivier */ 29096e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 29107550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 29117550a822SLaurent Vivier if (ret > 0) { 29127550a822SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 29137550a822SLaurent Vivier q->tx_waiting = 1; 29147550a822SLaurent Vivier timer_mod(q->tx_timer, 29157550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 29167550a822SLaurent Vivier } 29176e790746SPaolo Bonzini } 29186e790746SPaolo Bonzini 29196e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 29206e790746SPaolo Bonzini { 29216e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 29226e790746SPaolo Bonzini VirtIONet *n = q->n; 292317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 29246e790746SPaolo Bonzini int32_t ret; 29256e790746SPaolo Bonzini 2926e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2927e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2928e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2929e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2930e8bcf842SMichael S. Tsirkin return; 2931e8bcf842SMichael S. Tsirkin } 29326e790746SPaolo Bonzini 29336e790746SPaolo Bonzini q->tx_waiting = 0; 29346e790746SPaolo Bonzini 29356e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 293617a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 29376e790746SPaolo Bonzini return; 293817a0ca55SKONRAD Frederic } 29396e790746SPaolo Bonzini 29406e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2941fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2942fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2943fa5e56c2SGreg Kurz * broken */ 29446e790746SPaolo Bonzini } 29456e790746SPaolo Bonzini 29466e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 29476e790746SPaolo Bonzini * more coming and immediately reschedule */ 29486e790746SPaolo Bonzini if (ret >= n->tx_burst) { 29496e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 29506e790746SPaolo Bonzini q->tx_waiting = 1; 29516e790746SPaolo Bonzini return; 29526e790746SPaolo Bonzini } 29536e790746SPaolo Bonzini 29546e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 29556e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 29566e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 29576e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2958fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2959fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2960fa5e56c2SGreg Kurz return; 2961fa5e56c2SGreg Kurz } else if (ret > 0) { 29626e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 29636e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 29646e790746SPaolo Bonzini q->tx_waiting = 1; 29656e790746SPaolo Bonzini } 29666e790746SPaolo Bonzini } 29676e790746SPaolo Bonzini 2968f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2969f9d6dbf0SWen Congyang { 2970f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2971f9d6dbf0SWen Congyang 29721c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 29731c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 29749b02e161SWei Wang 2975f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2976f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 29779b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 29789b02e161SWei Wang virtio_net_handle_tx_timer); 2979f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2980f9d6dbf0SWen Congyang virtio_net_tx_timer, 2981f9d6dbf0SWen Congyang &n->vqs[index]); 2982f9d6dbf0SWen Congyang } else { 2983f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 29849b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 29859b02e161SWei Wang virtio_net_handle_tx_bh); 2986f63192b0SAlexander Bulekov n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index], 2987f63192b0SAlexander Bulekov &DEVICE(vdev)->mem_reentrancy_guard); 2988f9d6dbf0SWen Congyang } 2989f9d6dbf0SWen Congyang 2990f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2991f9d6dbf0SWen Congyang n->vqs[index].n = n; 2992f9d6dbf0SWen Congyang } 2993f9d6dbf0SWen Congyang 2994f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2995f9d6dbf0SWen Congyang { 2996f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2997f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2998f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2999f9d6dbf0SWen Congyang 3000f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 3001f9d6dbf0SWen Congyang 3002f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 3003f9d6dbf0SWen Congyang if (q->tx_timer) { 3004f9d6dbf0SWen Congyang timer_free(q->tx_timer); 3005f989c30cSYunjian Wang q->tx_timer = NULL; 3006f9d6dbf0SWen Congyang } else { 3007f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 3008f989c30cSYunjian Wang q->tx_bh = NULL; 3009f9d6dbf0SWen Congyang } 3010f989c30cSYunjian Wang q->tx_waiting = 0; 3011f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 3012f9d6dbf0SWen Congyang } 3013f9d6dbf0SWen Congyang 3014441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 3015f9d6dbf0SWen Congyang { 3016f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 3017f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 3018441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 3019f9d6dbf0SWen Congyang int i; 3020f9d6dbf0SWen Congyang 3021f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 3022f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 3023f9d6dbf0SWen Congyang 3024f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 3025f9d6dbf0SWen Congyang return; 3026f9d6dbf0SWen Congyang } 3027f9d6dbf0SWen Congyang 3028f9d6dbf0SWen Congyang /* 3029f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 3030f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 303120f86a75SYuval Shaia * and then we only enter one of the following two loops. 3032f9d6dbf0SWen Congyang */ 3033f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 3034f9d6dbf0SWen Congyang 3035f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 3036f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 3037f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 3038f9d6dbf0SWen Congyang } 3039f9d6dbf0SWen Congyang 3040f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 3041f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 3042f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 3043f9d6dbf0SWen Congyang } 3044f9d6dbf0SWen Congyang 3045f9d6dbf0SWen Congyang /* add ctrl_vq last */ 3046f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 3047f9d6dbf0SWen Congyang } 3048f9d6dbf0SWen Congyang 3049ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 30506e790746SPaolo Bonzini { 3051441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 3052f9d6dbf0SWen Congyang 30536e790746SPaolo Bonzini n->multiqueue = multiqueue; 3054441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 30556e790746SPaolo Bonzini 3056441537f1SJason Wang virtio_net_set_queue_pairs(n); 30576e790746SPaolo Bonzini } 30586e790746SPaolo Bonzini 3059982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 3060037dab2fSGreg Kurz { 3061982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 3062982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 3063037dab2fSGreg Kurz int i, link_down; 3064037dab2fSGreg Kurz 30659d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 3066982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 306795129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 3068e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 3069e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 3070e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 30716e790746SPaolo Bonzini 30726e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 3073982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 30746e790746SPaolo Bonzini n->mac_table.in_use = 0; 30756e790746SPaolo Bonzini } 30766e790746SPaolo Bonzini 3077982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 30786c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 30796c666823SMichael S. Tsirkin } 30806c666823SMichael S. Tsirkin 30817788c3f2SMikhail Sennikovsky /* 30827788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 30837788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 30847788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 30857788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 30867788c3f2SMikhail Sennikovsky */ 30877788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 30886c666823SMichael S. Tsirkin 3089441537f1SJason Wang virtio_net_set_queue_pairs(n); 30906e790746SPaolo Bonzini 30916e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 30926e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 30936e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 30946e790746SPaolo Bonzini break; 30956e790746SPaolo Bonzini } 30966e790746SPaolo Bonzini } 30976e790746SPaolo Bonzini n->mac_table.first_multi = i; 30986e790746SPaolo Bonzini 30996e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 31006e790746SPaolo Bonzini * to link status bit in n->status */ 31016e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 3102441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 31036e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 31046e790746SPaolo Bonzini } 31056e790746SPaolo Bonzini 31066c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 31076c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 31089d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 31099d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 31109d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 31119d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 31129d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 31139d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 31149d8c6a25SDr. David Alan Gilbert } else { 3115944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 31169d8c6a25SDr. David Alan Gilbert } 31176c666823SMichael S. Tsirkin } 31186c666823SMichael S. Tsirkin 3119e41b7114SYuri Benditovich if (n->rss_data.enabled) { 31200145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 31210145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 31220145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 31230145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 31240145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS for vhost"); 31250145c393SAndrew Melnychenko } else { 31260145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS - " 31270145c393SAndrew Melnychenko "fallback to software RSS"); 31280145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 31290145c393SAndrew Melnychenko } 31300145c393SAndrew Melnychenko } 31310145c393SAndrew Melnychenko } 31320145c393SAndrew Melnychenko 3133e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 3134e41b7114SYuri Benditovich n->rss_data.indirections_len, 3135e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 3136e41b7114SYuri Benditovich } else { 3137e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 3138e41b7114SYuri Benditovich } 31396e790746SPaolo Bonzini return 0; 31406e790746SPaolo Bonzini } 31416e790746SPaolo Bonzini 31427788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 31437788c3f2SMikhail Sennikovsky { 31447788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 31457788c3f2SMikhail Sennikovsky /* 31467788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 31477788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 31487788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 31497788c3f2SMikhail Sennikovsky */ 31507788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 31517788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 31527788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 31537788c3f2SMikhail Sennikovsky } 31547788c3f2SMikhail Sennikovsky 31557788c3f2SMikhail Sennikovsky return 0; 31567788c3f2SMikhail Sennikovsky } 31577788c3f2SMikhail Sennikovsky 3158982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 3159982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 3160982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 31611de81b42SRichard Henderson .fields = (const VMStateField[]) { 3162982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 3163982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3164982b78c5SDr. David Alan Gilbert }, 3165982b78c5SDr. David Alan Gilbert }; 3166982b78c5SDr. David Alan Gilbert 3167441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 3168982b78c5SDr. David Alan Gilbert { 3169441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 3170982b78c5SDr. David Alan Gilbert } 3171982b78c5SDr. David Alan Gilbert 3172982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 3173982b78c5SDr. David Alan Gilbert { 3174982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 3175982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 3176982b78c5SDr. David Alan Gilbert } 3177982b78c5SDr. David Alan Gilbert 3178982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 3179982b78c5SDr. David Alan Gilbert { 3180982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 3181982b78c5SDr. David Alan Gilbert } 3182982b78c5SDr. David Alan Gilbert 3183982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 3184982b78c5SDr. David Alan Gilbert { 3185982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 3186982b78c5SDr. David Alan Gilbert } 3187982b78c5SDr. David Alan Gilbert 3188982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 3189982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 3190982b78c5SDr. David Alan Gilbert */ 3191982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 3192982b78c5SDr. David Alan Gilbert VirtIONet *parent; 3193982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 3194441537f1SJason Wang uint16_t curr_queue_pairs_1; 3195982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 3196982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 3197982b78c5SDr. David Alan Gilbert }; 3198982b78c5SDr. David Alan Gilbert 3199982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 3200441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 3201982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 3202982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 3203982b78c5SDr. David Alan Gilbert */ 3204982b78c5SDr. David Alan Gilbert 320544b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 3206982b78c5SDr. David Alan Gilbert { 3207982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3208982b78c5SDr. David Alan Gilbert 3209982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 3210441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 3211441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 3212441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 3213982b78c5SDr. David Alan Gilbert } 321444b1ff31SDr. David Alan Gilbert 321544b1ff31SDr. David Alan Gilbert return 0; 3216982b78c5SDr. David Alan Gilbert } 3217982b78c5SDr. David Alan Gilbert 3218982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 3219982b78c5SDr. David Alan Gilbert { 3220982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3221982b78c5SDr. David Alan Gilbert 3222982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 3223982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 3224982b78c5SDr. David Alan Gilbert 3225441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 3226441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 3227441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 3228982b78c5SDr. David Alan Gilbert 3229982b78c5SDr. David Alan Gilbert return -EINVAL; 3230982b78c5SDr. David Alan Gilbert } 3231982b78c5SDr. David Alan Gilbert 3232982b78c5SDr. David Alan Gilbert return 0; /* all good */ 3233982b78c5SDr. David Alan Gilbert } 3234982b78c5SDr. David Alan Gilbert 3235982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 3236982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 3237982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 3238982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 32391de81b42SRichard Henderson .fields = (const VMStateField[]) { 3240982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 3241441537f1SJason Wang curr_queue_pairs_1, 3242982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3243982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3244982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3245982b78c5SDr. David Alan Gilbert }, 3246982b78c5SDr. David Alan Gilbert }; 3247982b78c5SDr. David Alan Gilbert 3248982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3249982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3250982b78c5SDr. David Alan Gilbert */ 3251982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3252982b78c5SDr. David Alan Gilbert { 3253982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3254982b78c5SDr. David Alan Gilbert 3255982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3256982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3257982b78c5SDr. David Alan Gilbert return -EINVAL; 3258982b78c5SDr. David Alan Gilbert } 3259982b78c5SDr. David Alan Gilbert 3260982b78c5SDr. David Alan Gilbert return 0; 3261982b78c5SDr. David Alan Gilbert } 3262982b78c5SDr. David Alan Gilbert 326344b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3264982b78c5SDr. David Alan Gilbert { 3265982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3266982b78c5SDr. David Alan Gilbert 3267982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 326844b1ff31SDr. David Alan Gilbert 326944b1ff31SDr. David Alan Gilbert return 0; 3270982b78c5SDr. David Alan Gilbert } 3271982b78c5SDr. David Alan Gilbert 3272982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3273982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3274982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3275982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 32761de81b42SRichard Henderson .fields = (const VMStateField[]) { 3277982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3278982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3279982b78c5SDr. David Alan Gilbert }, 3280982b78c5SDr. David Alan Gilbert }; 3281982b78c5SDr. David Alan Gilbert 3282982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3283982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3284982b78c5SDr. David Alan Gilbert */ 3285982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3286982b78c5SDr. David Alan Gilbert { 3287982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3288982b78c5SDr. David Alan Gilbert 3289982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3290982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3291982b78c5SDr. David Alan Gilbert return -EINVAL; 3292982b78c5SDr. David Alan Gilbert } 3293982b78c5SDr. David Alan Gilbert 3294982b78c5SDr. David Alan Gilbert return 0; 3295982b78c5SDr. David Alan Gilbert } 3296982b78c5SDr. David Alan Gilbert 329744b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3298982b78c5SDr. David Alan Gilbert { 3299982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3300982b78c5SDr. David Alan Gilbert 3301982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 330244b1ff31SDr. David Alan Gilbert 330344b1ff31SDr. David Alan Gilbert return 0; 3304982b78c5SDr. David Alan Gilbert } 3305982b78c5SDr. David Alan Gilbert 3306982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3307982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3308982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3309982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 33101de81b42SRichard Henderson .fields = (const VMStateField[]) { 3311982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3312982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3313982b78c5SDr. David Alan Gilbert }, 3314982b78c5SDr. David Alan Gilbert }; 3315982b78c5SDr. David Alan Gilbert 3316e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3317e41b7114SYuri Benditovich { 3318e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3319e41b7114SYuri Benditovich } 3320e41b7114SYuri Benditovich 3321e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3322e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3323e41b7114SYuri Benditovich .version_id = 1, 3324e41b7114SYuri Benditovich .minimum_version_id = 1, 3325e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 33261de81b42SRichard Henderson .fields = (const VMStateField[]) { 3327e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3328e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3329e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3330e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3331e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3332e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3333e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3334e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3335e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3336e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3337e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3338e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3339e41b7114SYuri Benditovich }, 3340e41b7114SYuri Benditovich }; 3341e41b7114SYuri Benditovich 3342982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3343982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3344982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3345982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3346982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 33471de81b42SRichard Henderson .fields = (const VMStateField[]) { 3348982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3349982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3350982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3351982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3352982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3353982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3354982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3355982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3356982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3357982b78c5SDr. David Alan Gilbert 3358982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3359982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3360982b78c5SDr. David Alan Gilbert * sets flags in this case. 3361982b78c5SDr. David Alan Gilbert */ 3362982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3363982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3364982b78c5SDr. David Alan Gilbert ETH_ALEN), 3365982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3366982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3367982b78c5SDr. David Alan Gilbert 3368982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3369982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3370982b78c5SDr. David Alan Gilbert * but based on the uint. 3371982b78c5SDr. David Alan Gilbert */ 3372982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3373982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3374982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3375982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3376982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3377982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3378982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3379982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3380982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3381982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3382982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3383441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3384982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3385441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3386982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3387982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3388982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3389982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3390982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3391982b78c5SDr. David Alan Gilbert }, 33921de81b42SRichard Henderson .subsections = (const VMStateDescription * const []) { 3393e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3394e41b7114SYuri Benditovich NULL 3395e41b7114SYuri Benditovich } 3396982b78c5SDr. David Alan Gilbert }; 3397982b78c5SDr. David Alan Gilbert 33986e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3399f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 34006e790746SPaolo Bonzini .size = sizeof(NICState), 34016e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 34026e790746SPaolo Bonzini .receive = virtio_net_receive, 34036e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3404b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3405b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 34066e790746SPaolo Bonzini }; 34076e790746SPaolo Bonzini 34086e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 34096e790746SPaolo Bonzini { 341017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 341168b0a639SSi-Wei Liu NetClientState *nc; 34126e790746SPaolo Bonzini assert(n->vhost_started); 341368b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 341468b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 341568b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 341668b0a639SSi-Wei Liu * buggy migration stream. 341768b0a639SSi-Wei Liu */ 341868b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 341968b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 342068b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 342168b0a639SSi-Wei Liu return false; 342268b0a639SSi-Wei Liu } 342368b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 342468b0a639SSi-Wei Liu } else { 342568b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 342668b0a639SSi-Wei Liu } 3427544f0278SCindy Lu /* 3428544f0278SCindy Lu * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1 34297e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not 3430544f0278SCindy Lu * support, the function will return false 3431544f0278SCindy Lu */ 3432544f0278SCindy Lu 3433544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) { 34348aab0d1dSCindy Lu return vhost_net_config_pending(get_vhost_net(nc->peer)); 3435544f0278SCindy Lu } 3436ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 34376e790746SPaolo Bonzini } 34386e790746SPaolo Bonzini 34396e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 34406e790746SPaolo Bonzini bool mask) 34416e790746SPaolo Bonzini { 344217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 344368b0a639SSi-Wei Liu NetClientState *nc; 34446e790746SPaolo Bonzini assert(n->vhost_started); 344568b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 344668b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 344768b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 344868b0a639SSi-Wei Liu * buggy migration stream. 344968b0a639SSi-Wei Liu */ 345068b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 345168b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 345268b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 345368b0a639SSi-Wei Liu return; 345468b0a639SSi-Wei Liu } 345568b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 345668b0a639SSi-Wei Liu } else { 345768b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 345868b0a639SSi-Wei Liu } 3459544f0278SCindy Lu /* 3460544f0278SCindy Lu *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1 34617e8094f0SAlex Bennée * as the macro of configure interrupt's IDX, If this driver does not 3462544f0278SCindy Lu * support, the function will return 3463544f0278SCindy Lu */ 3464544f0278SCindy Lu 3465544f0278SCindy Lu if (idx == VIRTIO_CONFIG_IRQ_IDX) { 34668aab0d1dSCindy Lu vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask); 3467544f0278SCindy Lu return; 3468544f0278SCindy Lu } 3469544f0278SCindy Lu vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask); 34706e790746SPaolo Bonzini } 34716e790746SPaolo Bonzini 3472019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 34736e790746SPaolo Bonzini { 34740cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3475a93e599dSMaxime Coquelin 3476d74c30c8SDaniil Tatianin n->config_size = virtio_get_config_size(&cfg_size_params, host_features); 347717ec5a86SKONRAD Frederic } 34786e790746SPaolo Bonzini 34798a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 34808a253ec2SKONRAD Frederic const char *type) 34818a253ec2SKONRAD Frederic { 34828a253ec2SKONRAD Frederic /* 34838a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 34848a253ec2SKONRAD Frederic */ 34858a253ec2SKONRAD Frederic assert(type != NULL); 34868a253ec2SKONRAD Frederic 34878a253ec2SKONRAD Frederic g_free(n->netclient_name); 34888a253ec2SKONRAD Frederic g_free(n->netclient_type); 34898a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 34908a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 34918a253ec2SKONRAD Frederic } 34928a253ec2SKONRAD Frederic 34930e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 34949711cd0dSJens Freimann { 34959711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 34969711cd0dSJens Freimann PCIDevice *pci_dev; 34979711cd0dSJens Freimann Error *err = NULL; 34989711cd0dSJens Freimann 34990e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 35009711cd0dSJens Freimann if (hotplug_ctrl) { 35010e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 35029711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 35030e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 35049711cd0dSJens Freimann if (err) { 35059711cd0dSJens Freimann error_report_err(err); 35069711cd0dSJens Freimann return false; 35079711cd0dSJens Freimann } 35089711cd0dSJens Freimann } else { 35099711cd0dSJens Freimann return false; 35109711cd0dSJens Freimann } 35119711cd0dSJens Freimann return true; 35129711cd0dSJens Freimann } 35139711cd0dSJens Freimann 35140e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 35150e9a65c5SJuan Quintela Error **errp) 35169711cd0dSJens Freimann { 35175a0948d3SMarkus Armbruster Error *err = NULL; 35189711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 35190e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 352078274682SJuan Quintela BusState *primary_bus; 35219711cd0dSJens Freimann 35229711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 35239711cd0dSJens Freimann return true; 35249711cd0dSJens Freimann } 35250e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 352678274682SJuan Quintela if (!primary_bus) { 3527150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 35285a0948d3SMarkus Armbruster return false; 35299711cd0dSJens Freimann } 35300e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3531e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 35320e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 35339711cd0dSJens Freimann if (hotplug_ctrl) { 35340e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 35355a0948d3SMarkus Armbruster if (err) { 35365a0948d3SMarkus Armbruster goto out; 35375a0948d3SMarkus Armbruster } 35380e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 35399711cd0dSJens Freimann } 3540109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3541150ab54aSJens Freimann 3542150ab54aSJens Freimann out: 35435a0948d3SMarkus Armbruster error_propagate(errp, err); 35445a0948d3SMarkus Armbruster return !err; 35459711cd0dSJens Freimann } 35469711cd0dSJens Freimann 35479d9babf7SSteve Sistare static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationEvent *e) 35489711cd0dSJens Freimann { 35499711cd0dSJens Freimann bool should_be_hidden; 35509711cd0dSJens Freimann Error *err = NULL; 355107a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 355207a5d816SJuan Quintela 355307a5d816SJuan Quintela if (!dev) { 355407a5d816SJuan Quintela return; 355507a5d816SJuan Quintela } 35569711cd0dSJens Freimann 3557e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 35589711cd0dSJens Freimann 35599d9babf7SSteve Sistare if (e->type == MIG_EVENT_PRECOPY_SETUP && !should_be_hidden) { 356007a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 356107a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 356207a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3563e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 35649711cd0dSJens Freimann } else { 35659711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 35669711cd0dSJens Freimann } 35679d9babf7SSteve Sistare } else if (e->type == MIG_EVENT_PRECOPY_FAILED) { 3568150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 356907a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 35709711cd0dSJens Freimann if (err) { 35719711cd0dSJens Freimann error_report_err(err); 35729711cd0dSJens Freimann } 35739711cd0dSJens Freimann } 35749711cd0dSJens Freimann } 35759711cd0dSJens Freimann } 35769711cd0dSJens Freimann 35773e775730SSteve Sistare static int virtio_net_migration_state_notifier(NotifierWithReturn *notifier, 35785663dd3fSSteve Sistare MigrationEvent *e, Error **errp) 35799711cd0dSJens Freimann { 35809711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 35819d9babf7SSteve Sistare virtio_net_handle_migration_primary(n, e); 35823e775730SSteve Sistare return 0; 35839711cd0dSJens Freimann } 35849711cd0dSJens Freimann 3585b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3586f3558b1bSKevin Wolf const QDict *device_opts, 3587f3558b1bSKevin Wolf bool from_json, 3588f3558b1bSKevin Wolf Error **errp) 35899711cd0dSJens Freimann { 35909711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 35914f0303aeSJuan Quintela const char *standby_id; 35929711cd0dSJens Freimann 35934d0e59acSJens Freimann if (!device_opts) { 359489631fedSJuan Quintela return false; 35954d0e59acSJens Freimann } 3596bcfc906bSLaurent Vivier 3597bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3598bcfc906bSLaurent Vivier return false; 3599bcfc906bSLaurent Vivier } 3600bcfc906bSLaurent Vivier 3601bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3602bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3603bcfc906bSLaurent Vivier return false; 3604bcfc906bSLaurent Vivier } 3605bcfc906bSLaurent Vivier 3606bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 360789631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 360889631fedSJuan Quintela return false; 36099711cd0dSJens Freimann } 36109711cd0dSJens Freimann 36117fe7791eSLaurent Vivier /* 36127fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 36137fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 36147fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 36157fe7791eSLaurent Vivier * device. 36167fe7791eSLaurent Vivier */ 3617259a10dbSKevin Wolf if (n->primary_opts) { 36187fe7791eSLaurent Vivier const char *old, *new; 36197fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 36207fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 36217fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 36227fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 36237fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 36247fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3625259a10dbSKevin Wolf return false; 3626259a10dbSKevin Wolf } 36277fe7791eSLaurent Vivier } else { 3628f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3629f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 36307fe7791eSLaurent Vivier } 3631259a10dbSKevin Wolf 3632e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 36333abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 36349711cd0dSJens Freimann } 36359711cd0dSJens Freimann 3636e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 363717ec5a86SKONRAD Frederic { 3638e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3639284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3640284a32f0SAndreas Färber NetClientState *nc; 36411773d9eeSKONRAD Frederic int i; 364217ec5a86SKONRAD Frederic 3643a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3644127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3645a93e599dSMaxime Coquelin } 3646a93e599dSMaxime Coquelin 36479473939eSJason Baron if (n->net_conf.duplex_str) { 36489473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 36499473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 36509473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 36519473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 36529473939eSJason Baron } else { 36539473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3654843c4cfcSMarkus Armbruster return; 36559473939eSJason Baron } 36569473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 36579473939eSJason Baron } else { 36589473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 36599473939eSJason Baron } 36609473939eSJason Baron 36619473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 36629473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3663843c4cfcSMarkus Armbruster return; 3664843c4cfcSMarkus Armbruster } 3665843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 36669473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 36679473939eSJason Baron } 36689473939eSJason Baron 36699711cd0dSJens Freimann if (n->failover) { 3670b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3671e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 36729711cd0dSJens Freimann device_listener_register(&n->primary_listener); 3673d9cda213SSteve Sistare migration_add_notifier(&n->migration_state, 3674d9cda213SSteve Sistare virtio_net_migration_state_notifier); 36759711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 36769711cd0dSJens Freimann } 36779711cd0dSJens Freimann 3678da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 36793857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_NET, n->config_size); 368017ec5a86SKONRAD Frederic 36811c0fbfa3SMichael S. Tsirkin /* 36821c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 36831c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 36841c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 36851c0fbfa3SMichael S. Tsirkin */ 36861c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 36871c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 36885f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 36891c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 36901c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 36911c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 36921c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 36931c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 36941c0fbfa3SMichael S. Tsirkin return; 36951c0fbfa3SMichael S. Tsirkin } 36961c0fbfa3SMichael S. Tsirkin 36979b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 36984271f403SLaurent Vivier n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) || 36999b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 37009b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 37019b02e161SWei Wang "must be a power of 2 between %d and %d", 37029b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 37034271f403SLaurent Vivier virtio_net_max_tx_queue_size(n)); 37049b02e161SWei Wang virtio_cleanup(vdev); 37059b02e161SWei Wang return; 37069b02e161SWei Wang } 37079b02e161SWei Wang 370822288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 370922288fe5SJason Wang 371022288fe5SJason Wang /* 371122288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 371222288fe5SJason Wang * provide control queue via peers as well. 371322288fe5SJason Wang */ 371422288fe5SJason Wang if (n->nic_conf.peers.queues) { 371522288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 371622288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 371722288fe5SJason Wang ++n->max_queue_pairs; 371822288fe5SJason Wang } 371922288fe5SJason Wang } 372022288fe5SJason Wang } 372122288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 372222288fe5SJason Wang 3723441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 372422288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3725631b22eaSStefan Weil "must be a positive integer less than %d.", 3726441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 37277e0e736eSJason Wang virtio_cleanup(vdev); 37287e0e736eSJason Wang return; 37297e0e736eSJason Wang } 3730b21e2380SMarkus Armbruster n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs); 3731441537f1SJason Wang n->curr_queue_pairs = 1; 37321773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 37336e790746SPaolo Bonzini 37341773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 37351773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 37360765691eSMarkus Armbruster warn_report("virtio-net: " 37376e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 37381773d9eeSKONRAD Frederic n->net_conf.tx); 37390765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 37406e790746SPaolo Bonzini } 37416e790746SPaolo Bonzini 37422eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 37432eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 37449b02e161SWei Wang 3745441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3746f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3747da51a335SJason Wang } 3748da51a335SJason Wang 374917a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 37501773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 37511773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 37526e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 37539d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 37549d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3755f57fcf70SJason Wang virtio_net_announce_timer, n); 3756b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 37576e790746SPaolo Bonzini 37588a253ec2SKONRAD Frederic if (n->netclient_type) { 37598a253ec2SKONRAD Frederic /* 37608a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 37618a253ec2SKONRAD Frederic */ 37628a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 37637d0fefdfSAkihiko Odaki n->netclient_type, n->netclient_name, 37647d0fefdfSAkihiko Odaki &dev->mem_reentrancy_guard, n); 37658a253ec2SKONRAD Frederic } else { 37661773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 37677d0fefdfSAkihiko Odaki object_get_typename(OBJECT(dev)), dev->id, 37687d0fefdfSAkihiko Odaki &dev->mem_reentrancy_guard, n); 37698a253ec2SKONRAD Frederic } 37708a253ec2SKONRAD Frederic 3771441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3772d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3773d4c62930SBin Meng } 3774d4c62930SBin Meng 37756e790746SPaolo Bonzini peer_test_vnet_hdr(n); 37766e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 3777441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3778d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 37796e790746SPaolo Bonzini } 37806e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 37816e790746SPaolo Bonzini } else { 37826e790746SPaolo Bonzini n->host_hdr_len = 0; 37836e790746SPaolo Bonzini } 37846e790746SPaolo Bonzini 37851773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 37866e790746SPaolo Bonzini 37876e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 37881773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3789e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 37906e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 37916e790746SPaolo Bonzini 37926e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 37936e790746SPaolo Bonzini 37946e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 37956e790746SPaolo Bonzini 3796b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3797b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3798b1be4280SAmos Kong 3799e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3800e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3801e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3802e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3803f8ed3648SManos Pitsidianakis (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND); 3804e87936eaSCindy Lu } 38052974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3806284a32f0SAndreas Färber n->qdev = dev; 38074474e37aSYuri Benditovich 3808aac8f89dSAkihiko Odaki net_rx_pkt_init(&n->rx_pkt); 38090145c393SAndrew Melnychenko 38100145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 3811*6b230b7dSAndrew Melnychenko virtio_net_load_ebpf(n, errp); 38120145c393SAndrew Melnychenko } 381317ec5a86SKONRAD Frederic } 381417ec5a86SKONRAD Frederic 3815b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 381617ec5a86SKONRAD Frederic { 3817306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3818306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3819441537f1SJason Wang int i, max_queue_pairs; 382017ec5a86SKONRAD Frederic 38210145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 38220145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 38230145c393SAndrew Melnychenko } 38240145c393SAndrew Melnychenko 382517ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 382617ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 382717ec5a86SKONRAD Frederic 38288a253ec2SKONRAD Frederic g_free(n->netclient_name); 38298a253ec2SKONRAD Frederic n->netclient_name = NULL; 38308a253ec2SKONRAD Frederic g_free(n->netclient_type); 38318a253ec2SKONRAD Frederic n->netclient_type = NULL; 38328a253ec2SKONRAD Frederic 383317ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 383417ec5a86SKONRAD Frederic g_free(n->vlans); 383517ec5a86SKONRAD Frederic 38369711cd0dSJens Freimann if (n->failover) { 3837f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 383865018100SJuan Quintela device_listener_unregister(&n->primary_listener); 3839d9cda213SSteve Sistare migration_remove_notifier(&n->migration_state); 3840f3558b1bSKevin Wolf } else { 3841f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 38429711cd0dSJens Freimann } 38439711cd0dSJens Freimann 3844441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3845441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3846f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 384717ec5a86SKONRAD Frederic } 3848d945d9f1SYuri Benditovich /* delete also control vq */ 3849441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3850944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 385117ec5a86SKONRAD Frederic g_free(n->vqs); 385217ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 38532974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 385459079029SYuri Benditovich g_free(n->rss_data.indirections_table); 38554474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 38566a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 385717ec5a86SKONRAD Frederic } 385817ec5a86SKONRAD Frederic 385917ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 386017ec5a86SKONRAD Frederic { 386117ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 386217ec5a86SKONRAD Frederic 386317ec5a86SKONRAD Frederic /* 386417ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 38652431f4f1SMichael Tokarev * Can be overridden with virtio_net_set_config_size. 386617ec5a86SKONRAD Frederic */ 386717ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3868aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3869aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 387040c2281cSMarkus Armbruster DEVICE(n)); 38710145c393SAndrew Melnychenko 38720145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 387317ec5a86SKONRAD Frederic } 387417ec5a86SKONRAD Frederic 387544b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 38764d45dcfbSHalil Pasic { 38774d45dcfbSHalil Pasic VirtIONet *n = opaque; 38784d45dcfbSHalil Pasic 38794d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 38804d45dcfbSHalil Pasic * it might keep writing to memory. */ 38814d45dcfbSHalil Pasic assert(!n->vhost_started); 388244b1ff31SDr. David Alan Gilbert 388344b1ff31SDr. David Alan Gilbert return 0; 38844d45dcfbSHalil Pasic } 38854d45dcfbSHalil Pasic 38869711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 38879711cd0dSJens Freimann { 38889711cd0dSJens Freimann DeviceState *dev = opaque; 388921e8709bSJuan Quintela DeviceState *primary; 38909711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 38919711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 38929711cd0dSJens Freimann 3893284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3894284f42a5SJens Freimann return false; 3895284f42a5SJens Freimann } 389621e8709bSJuan Quintela primary = failover_find_primary_device(n); 389721e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 38989711cd0dSJens Freimann } 38999711cd0dSJens Freimann 39009711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 39019711cd0dSJens Freimann { 39029711cd0dSJens Freimann DeviceState *dev = opaque; 39039711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 39049711cd0dSJens Freimann 39059711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 39069711cd0dSJens Freimann } 39079711cd0dSJens Freimann 3908c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) 3909c255488dSJonah Palmer { 3910c255488dSJonah Palmer VirtIONet *n = VIRTIO_NET(vdev); 3911c255488dSJonah Palmer NetClientState *nc = qemu_get_queue(n->nic); 3912c255488dSJonah Palmer struct vhost_net *net = get_vhost_net(nc->peer); 3913c255488dSJonah Palmer return &net->dev; 3914c255488dSJonah Palmer } 3915c255488dSJonah Palmer 39164d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 39174d45dcfbSHalil Pasic .name = "virtio-net", 39184d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 39194d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 39201de81b42SRichard Henderson .fields = (const VMStateField[]) { 39214d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 39224d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 39234d45dcfbSHalil Pasic }, 39244d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 39259711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 39264d45dcfbSHalil Pasic }; 3927290c2428SDr. David Alan Gilbert 392817ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3929127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3930127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3931127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 393287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3933127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3934127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 393587108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3936127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 393787108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3938127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 393987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3940127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 394187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3942127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 394387108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3944127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 394587108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3946127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 394787108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3948127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 394987108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3950127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 395187108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3952127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 395387108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3954127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 395587108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3956127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 395787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3958127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 395987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3960127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 396187108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3962127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 396387108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3964127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 396587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3966127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 396787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3968127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 396959079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 397059079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3971e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3972e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 3973*6b230b7dSAndrew Melnychenko DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet, nr_ebpf_rss_fds, 3974*6b230b7dSAndrew Melnychenko ebpf_rss_fds, qdev_prop_string, char*), 39752974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 39762974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 39772974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 39782974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 397917ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 398017ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 398117ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 398217ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 398317ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 39841c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 39851c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 39869b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 39879b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3988a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 398975ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 399075ebec11SMaxime Coquelin true), 39919473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 39929473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 39939711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 399453da8b5aSYuri Benditovich DEFINE_PROP_BIT64("guest_uso4", VirtIONet, host_features, 399553da8b5aSYuri Benditovich VIRTIO_NET_F_GUEST_USO4, true), 399653da8b5aSYuri Benditovich DEFINE_PROP_BIT64("guest_uso6", VirtIONet, host_features, 399753da8b5aSYuri Benditovich VIRTIO_NET_F_GUEST_USO6, true), 399853da8b5aSYuri Benditovich DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features, 399953da8b5aSYuri Benditovich VIRTIO_NET_F_HOST_USO, true), 400017ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 400117ec5a86SKONRAD Frederic }; 400217ec5a86SKONRAD Frederic 400317ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 400417ec5a86SKONRAD Frederic { 400517ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 400617ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 4007e6f746b3SAndreas Färber 40084f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 4009290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 4010125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 4011e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 4012306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 401317ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 401417ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 401517ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 401617ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 401717ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 401817ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 40197dc6be52SXuan Zhuo vdc->queue_reset = virtio_net_queue_reset; 40207f863302SKangjie Xu vdc->queue_enable = virtio_net_queue_enable; 402117ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 402217ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 402317ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 40242a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 40257788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 4026982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 40279711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 4028c255488dSJonah Palmer vdc->get_vhost = virtio_net_get_vhost; 4029cd9b8346SViktor Prutyanov vdc->toggle_device_iotlb = vhost_toggle_device_iotlb; 403017ec5a86SKONRAD Frederic } 403117ec5a86SKONRAD Frederic 403217ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 403317ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 403417ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 403517ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 403617ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 403717ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 403817ec5a86SKONRAD Frederic }; 403917ec5a86SKONRAD Frederic 404017ec5a86SKONRAD Frederic static void virtio_register_types(void) 404117ec5a86SKONRAD Frederic { 404217ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 404317ec5a86SKONRAD Frederic } 404417ec5a86SKONRAD Frederic 404517ec5a86SKONRAD Frederic type_init(virtio_register_types) 4046