16e790746SPaolo Bonzini /* 26e790746SPaolo Bonzini * Virtio Network Device 36e790746SPaolo Bonzini * 46e790746SPaolo Bonzini * Copyright IBM, Corp. 2007 56e790746SPaolo Bonzini * 66e790746SPaolo Bonzini * Authors: 76e790746SPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 86e790746SPaolo Bonzini * 96e790746SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 106e790746SPaolo Bonzini * the COPYING file in the top-level directory. 116e790746SPaolo Bonzini * 126e790746SPaolo Bonzini */ 136e790746SPaolo Bonzini 149b8bfe21SPeter Maydell #include "qemu/osdep.h" 159711cd0dSJens Freimann #include "qemu/atomic.h" 166e790746SPaolo Bonzini #include "qemu/iov.h" 1768b0a639SSi-Wei Liu #include "qemu/log.h" 18db725815SMarkus Armbruster #include "qemu/main-loop.h" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 206e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 216e790746SPaolo Bonzini #include "net/net.h" 226e790746SPaolo Bonzini #include "net/checksum.h" 236e790746SPaolo Bonzini #include "net/tap.h" 246e790746SPaolo Bonzini #include "qemu/error-report.h" 256e790746SPaolo Bonzini #include "qemu/timer.h" 269711cd0dSJens Freimann #include "qemu/option.h" 279711cd0dSJens Freimann #include "qemu/option_int.h" 289711cd0dSJens Freimann #include "qemu/config-file.h" 299711cd0dSJens Freimann #include "qapi/qmp/qdict.h" 306e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 316e790746SPaolo Bonzini #include "net/vhost_net.h" 329d8c6a25SDr. David Alan Gilbert #include "net/announce.h" 3317ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 34e688df6bSMarkus Armbruster #include "qapi/error.h" 359af23989SMarkus Armbruster #include "qapi/qapi-events-net.h" 36a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 379711cd0dSJens Freimann #include "qapi/qapi-types-migration.h" 389711cd0dSJens Freimann #include "qapi/qapi-events-migration.h" 391399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 40f8d806c9SJuan Quintela #include "migration/misc.h" 419473939eSJason Baron #include "standard-headers/linux/ethtool.h" 422f780b6aSMarkus Armbruster #include "sysemu/sysemu.h" 439d8c6a25SDr. David Alan Gilbert #include "trace.h" 449711cd0dSJens Freimann #include "monitor/qdev.h" 459711cd0dSJens Freimann #include "hw/pci/pci.h" 464474e37aSYuri Benditovich #include "net_rx_pkt.h" 47108a6481SCindy Lu #include "hw/virtio/vhost.h" 481b529d90SLaurent Vivier #include "sysemu/qtest.h" 496e790746SPaolo Bonzini 506e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 516e790746SPaolo Bonzini 526e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 536e790746SPaolo Bonzini 541c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 551c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 569b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 579b02e161SWei Wang 58441537f1SJason Wang /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */ 591c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 609b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 611c0fbfa3SMichael S. Tsirkin 622974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ 632974e916SYuri Benditovich 642974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG 0x3F 652974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 662974e916SYuri Benditovich 672974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */ 682974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header)) 692974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535 702974e916SYuri Benditovich 712974e916SYuri Benditovich /* header length value in ip header without option */ 722974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5 732974e916SYuri Benditovich 742974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */ 752974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD 762974e916SYuri Benditovich 772974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance 782974e916SYuri Benditovich a lot, and should be tuned carefully, '300000'(300us) is the recommended 792974e916SYuri Benditovich value to pass the WHQL test, '50000' can gain 2x netperf throughput with 802974e916SYuri Benditovich tso/gso/gro 'off'. */ 812974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000 822974e916SYuri Benditovich 8359079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \ 8459079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \ 8559079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \ 8659079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \ 8759079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \ 8859079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \ 8959079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \ 9059079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \ 9159079029SYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) 9259079029SYuri Benditovich 93ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = { 94127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 955d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mac)}, 96127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 975d5b33c0SMax Reitz .end = endof(struct virtio_net_config, status)}, 98127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 995d5b33c0SMax Reitz .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 100127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 1015d5b33c0SMax Reitz .end = endof(struct virtio_net_config, mtu)}, 1029473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 1035d5b33c0SMax Reitz .end = endof(struct virtio_net_config, duplex)}, 104e22f0603SYuri Benditovich {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT), 10559079029SYuri Benditovich .end = endof(struct virtio_net_config, supported_hash_types)}, 1066e790746SPaolo Bonzini {} 1076e790746SPaolo Bonzini }; 1086e790746SPaolo Bonzini 109d74c30c8SDaniil Tatianin static const VirtIOConfigSizeParams cfg_size_params = { 110d74c30c8SDaniil Tatianin .min_size = endof(struct virtio_net_config, mac), 111d74c30c8SDaniil Tatianin .max_size = sizeof(struct virtio_net_config), 112d74c30c8SDaniil Tatianin .feature_sizes = feature_sizes 113d74c30c8SDaniil Tatianin }; 114d74c30c8SDaniil Tatianin 1156e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 1166e790746SPaolo Bonzini { 1176e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1186e790746SPaolo Bonzini 1196e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 1206e790746SPaolo Bonzini } 1216e790746SPaolo Bonzini 1226e790746SPaolo Bonzini static int vq2q(int queue_index) 1236e790746SPaolo Bonzini { 1246e790746SPaolo Bonzini return queue_index / 2; 1256e790746SPaolo Bonzini } 1266e790746SPaolo Bonzini 1274fdf69abSKangjie Xu static void flush_or_purge_queued_packets(NetClientState *nc) 1284fdf69abSKangjie Xu { 1294fdf69abSKangjie Xu if (!nc->peer) { 1304fdf69abSKangjie Xu return; 1314fdf69abSKangjie Xu } 1324fdf69abSKangjie Xu 1334fdf69abSKangjie Xu qemu_flush_or_purge_queued_packets(nc->peer, true); 1344fdf69abSKangjie Xu assert(!virtio_net_get_subqueue(nc)->async_tx.elem); 1354fdf69abSKangjie Xu } 1364fdf69abSKangjie Xu 1376e790746SPaolo Bonzini /* TODO 1386e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 1396e790746SPaolo Bonzini */ 1406e790746SPaolo Bonzini 1416e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 1426e790746SPaolo Bonzini { 14317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1446e790746SPaolo Bonzini struct virtio_net_config netcfg; 145c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 146fb592882SCindy Lu static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } }; 1476e790746SPaolo Bonzini 148108a6481SCindy Lu int ret = 0; 149108a6481SCindy Lu memset(&netcfg, 0 , sizeof(struct virtio_net_config)); 1501399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 151441537f1SJason Wang virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs); 152a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 1536e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 1549473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 1559473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 15659079029SYuri Benditovich netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE; 15759079029SYuri Benditovich virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length, 158e22f0603SYuri Benditovich virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ? 159e22f0603SYuri Benditovich VIRTIO_NET_RSS_MAX_TABLE_LEN : 1); 16059079029SYuri Benditovich virtio_stl_p(vdev, &netcfg.supported_hash_types, 16159079029SYuri Benditovich VIRTIO_NET_RSS_SUPPORTED_HASHES); 1626e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 163108a6481SCindy Lu 164c546ecf2SJason Wang /* 165c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 166c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 167c546ecf2SJason Wang */ 168c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 169108a6481SCindy Lu ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, 170108a6481SCindy Lu n->config_size); 171108a6481SCindy Lu if (ret != -1) { 172fb592882SCindy Lu /* 173fb592882SCindy Lu * Some NIC/kernel combinations present 0 as the mac address. As 174fb592882SCindy Lu * that is not a legal address, try to proceed with the 175fb592882SCindy Lu * address from the QEMU command line in the hope that the 176fb592882SCindy Lu * address has been configured correctly elsewhere - just not 177fb592882SCindy Lu * reported by the device. 178fb592882SCindy Lu */ 179fb592882SCindy Lu if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) { 180fb592882SCindy Lu info_report("Zero hardware mac address detected. Ignoring."); 181fb592882SCindy Lu memcpy(netcfg.mac, n->mac, ETH_ALEN); 182fb592882SCindy Lu } 183108a6481SCindy Lu memcpy(config, &netcfg, n->config_size); 184108a6481SCindy Lu } 185108a6481SCindy Lu } 1866e790746SPaolo Bonzini } 1876e790746SPaolo Bonzini 1886e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1896e790746SPaolo Bonzini { 19017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1916e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 192c546ecf2SJason Wang NetClientState *nc = qemu_get_queue(n->nic); 1936e790746SPaolo Bonzini 1946e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1956e790746SPaolo Bonzini 19695129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 19795129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1986e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1996e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 2006e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 2016e790746SPaolo Bonzini } 202108a6481SCindy Lu 203c546ecf2SJason Wang /* 204c546ecf2SJason Wang * Is this VDPA? No peer means not VDPA: there's no way to 205c546ecf2SJason Wang * disconnect/reconnect a VDPA peer. 206c546ecf2SJason Wang */ 207c546ecf2SJason Wang if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 208c546ecf2SJason Wang vhost_net_set_config(get_vhost_net(nc->peer), 209c546ecf2SJason Wang (uint8_t *)&netcfg, 0, n->config_size, 210108a6481SCindy Lu VHOST_SET_CONFIG_TYPE_MASTER); 211108a6481SCindy Lu } 2126e790746SPaolo Bonzini } 2136e790746SPaolo Bonzini 2146e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 2156e790746SPaolo Bonzini { 21617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2176e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 21817a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 2196e790746SPaolo Bonzini } 2206e790746SPaolo Bonzini 221b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net) 222b2c929f0SDr. David Alan Gilbert { 223b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(net); 224b2c929f0SDr. David Alan Gilbert trace_virtio_net_announce_notify(); 225b2c929f0SDr. David Alan Gilbert 226b2c929f0SDr. David Alan Gilbert net->status |= VIRTIO_NET_S_ANNOUNCE; 227b2c929f0SDr. David Alan Gilbert virtio_notify_config(vdev); 228b2c929f0SDr. David Alan Gilbert } 229b2c929f0SDr. David Alan Gilbert 230f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 231f57fcf70SJason Wang { 232f57fcf70SJason Wang VirtIONet *n = opaque; 2339d8c6a25SDr. David Alan Gilbert trace_virtio_net_announce_timer(n->announce_timer.round); 234f57fcf70SJason Wang 2359d8c6a25SDr. David Alan Gilbert n->announce_timer.round--; 236b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 237b2c929f0SDr. David Alan Gilbert } 238b2c929f0SDr. David Alan Gilbert 239b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc) 240b2c929f0SDr. David Alan Gilbert { 241b2c929f0SDr. David Alan Gilbert VirtIONet *n = qemu_get_nic_opaque(nc); 242b2c929f0SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 243b2c929f0SDr. David Alan Gilbert 244b2c929f0SDr. David Alan Gilbert /* 245b2c929f0SDr. David Alan Gilbert * Make sure the virtio migration announcement timer isn't running 246b2c929f0SDr. David Alan Gilbert * If it is, let it trigger announcement so that we do not cause 247b2c929f0SDr. David Alan Gilbert * confusion. 248b2c929f0SDr. David Alan Gilbert */ 249b2c929f0SDr. David Alan Gilbert if (n->announce_timer.round) { 250b2c929f0SDr. David Alan Gilbert return; 251b2c929f0SDr. David Alan Gilbert } 252b2c929f0SDr. David Alan Gilbert 253b2c929f0SDr. David Alan Gilbert if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 254b2c929f0SDr. David Alan Gilbert virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 255b2c929f0SDr. David Alan Gilbert virtio_net_announce_notify(n); 256b2c929f0SDr. David Alan Gilbert } 257f57fcf70SJason Wang } 258f57fcf70SJason Wang 2596e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 2606e790746SPaolo Bonzini { 26117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2626e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 263441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 264aa858194SSi-Wei Liu int cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ? 265aa858194SSi-Wei Liu n->max_ncs - n->max_queue_pairs : 0; 2666e790746SPaolo Bonzini 267ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 2686e790746SPaolo Bonzini return; 2696e790746SPaolo Bonzini } 2706e790746SPaolo Bonzini 2718c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 2728c1ac475SRadim Krčmář !!n->vhost_started) { 2736e790746SPaolo Bonzini return; 2746e790746SPaolo Bonzini } 2756e790746SPaolo Bonzini if (!n->vhost_started) { 276086abc1cSMichael S. Tsirkin int r, i; 277086abc1cSMichael S. Tsirkin 2781bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2791bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 2801bfa316cSGreg Kurz "falling back on userspace virtio", 2811bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 2821bfa316cSGreg Kurz return; 2831bfa316cSGreg Kurz } 2841bfa316cSGreg Kurz 285086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 286086abc1cSMichael S. Tsirkin * when vhost is running. 287086abc1cSMichael S. Tsirkin */ 288441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 289086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 290086abc1cSMichael S. Tsirkin 291086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 292086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 293086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 294086abc1cSMichael S. Tsirkin } 295086abc1cSMichael S. Tsirkin 296a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 297a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 298a93e599dSMaxime Coquelin if (r < 0) { 299a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 300a93e599dSMaxime Coquelin n->net_conf.mtu); 301a93e599dSMaxime Coquelin 302a93e599dSMaxime Coquelin return; 303a93e599dSMaxime Coquelin } 304a93e599dSMaxime Coquelin } 305a93e599dSMaxime Coquelin 3066e790746SPaolo Bonzini n->vhost_started = 1; 30722288fe5SJason Wang r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq); 3086e790746SPaolo Bonzini if (r < 0) { 3096e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 3106e790746SPaolo Bonzini "falling back on userspace virtio", -r); 3116e790746SPaolo Bonzini n->vhost_started = 0; 3126e790746SPaolo Bonzini } 3136e790746SPaolo Bonzini } else { 31422288fe5SJason Wang vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq); 3156e790746SPaolo Bonzini n->vhost_started = 0; 3166e790746SPaolo Bonzini } 3176e790746SPaolo Bonzini } 3186e790746SPaolo Bonzini 3191bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 3201bfa316cSGreg Kurz NetClientState *peer, 3211bfa316cSGreg Kurz bool enable) 3221bfa316cSGreg Kurz { 3231bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 3241bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 3251bfa316cSGreg Kurz } else { 3261bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 3271bfa316cSGreg Kurz } 3281bfa316cSGreg Kurz } 3291bfa316cSGreg Kurz 3301bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 331441537f1SJason Wang int queue_pairs, bool enable) 3321bfa316cSGreg Kurz { 3331bfa316cSGreg Kurz int i; 3341bfa316cSGreg Kurz 335441537f1SJason Wang for (i = 0; i < queue_pairs; i++) { 3361bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 3371bfa316cSGreg Kurz enable) { 3381bfa316cSGreg Kurz while (--i >= 0) { 3391bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 3401bfa316cSGreg Kurz } 3411bfa316cSGreg Kurz 3421bfa316cSGreg Kurz return true; 3431bfa316cSGreg Kurz } 3441bfa316cSGreg Kurz } 3451bfa316cSGreg Kurz 3461bfa316cSGreg Kurz return false; 3471bfa316cSGreg Kurz } 3481bfa316cSGreg Kurz 3491bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 3501bfa316cSGreg Kurz { 3511bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 352441537f1SJason Wang int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3531bfa316cSGreg Kurz 3541bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 3551bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 3561bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 3571bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 3581bfa316cSGreg Kurz * virtio-net code. 3591bfa316cSGreg Kurz */ 3601bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 361441537f1SJason Wang queue_pairs, true); 3621bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 3631bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 3641bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 3651bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 3661bfa316cSGreg Kurz * endianness. 3671bfa316cSGreg Kurz */ 368441537f1SJason Wang virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false); 3691bfa316cSGreg Kurz } 3701bfa316cSGreg Kurz } 3711bfa316cSGreg Kurz 372283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 373283e2c2aSYuri Benditovich { 374283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 375283e2c2aSYuri Benditovich if (dropped) { 376283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 377283e2c2aSYuri Benditovich } 378283e2c2aSYuri Benditovich } 379283e2c2aSYuri Benditovich 3806e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 3816e790746SPaolo Bonzini { 38217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3836e790746SPaolo Bonzini VirtIONetQueue *q; 3846e790746SPaolo Bonzini int i; 3856e790746SPaolo Bonzini uint8_t queue_status; 3866e790746SPaolo Bonzini 3871bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 3886e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 3896e790746SPaolo Bonzini 390441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 39138705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 39238705bb5SFam Zheng bool queue_started; 3936e790746SPaolo Bonzini q = &n->vqs[i]; 3946e790746SPaolo Bonzini 395441537f1SJason Wang if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) { 3966e790746SPaolo Bonzini queue_status = 0; 3976e790746SPaolo Bonzini } else { 3986e790746SPaolo Bonzini queue_status = status; 3996e790746SPaolo Bonzini } 40038705bb5SFam Zheng queue_started = 40138705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 40238705bb5SFam Zheng 40338705bb5SFam Zheng if (queue_started) { 40438705bb5SFam Zheng qemu_flush_queued_packets(ncs); 40538705bb5SFam Zheng } 4066e790746SPaolo Bonzini 4076e790746SPaolo Bonzini if (!q->tx_waiting) { 4086e790746SPaolo Bonzini continue; 4096e790746SPaolo Bonzini } 4106e790746SPaolo Bonzini 41138705bb5SFam Zheng if (queue_started) { 4126e790746SPaolo Bonzini if (q->tx_timer) { 413bc72ad67SAlex Bligh timer_mod(q->tx_timer, 414bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 4156e790746SPaolo Bonzini } else { 4166e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 4176e790746SPaolo Bonzini } 4186e790746SPaolo Bonzini } else { 4196e790746SPaolo Bonzini if (q->tx_timer) { 420bc72ad67SAlex Bligh timer_del(q->tx_timer); 4216e790746SPaolo Bonzini } else { 4226e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 4236e790746SPaolo Bonzini } 424283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 42570e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 42670e53e6eSJason Wang vdev->vm_running) { 427283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 428283e2c2aSYuri Benditovich * and disabled notification */ 429283e2c2aSYuri Benditovich q->tx_waiting = 0; 430283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 431283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 432283e2c2aSYuri Benditovich } 4336e790746SPaolo Bonzini } 4346e790746SPaolo Bonzini } 4356e790746SPaolo Bonzini } 4366e790746SPaolo Bonzini 4376e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 4386e790746SPaolo Bonzini { 4396e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 44017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 4416e790746SPaolo Bonzini uint16_t old_status = n->status; 4426e790746SPaolo Bonzini 4436e790746SPaolo Bonzini if (nc->link_down) 4446e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 4456e790746SPaolo Bonzini else 4466e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 4476e790746SPaolo Bonzini 4486e790746SPaolo Bonzini if (n->status != old_status) 44917a0ca55SKONRAD Frederic virtio_notify_config(vdev); 4506e790746SPaolo Bonzini 45117a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 4526e790746SPaolo Bonzini } 4536e790746SPaolo Bonzini 454b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 455b1be4280SAmos Kong { 456b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 457b1be4280SAmos Kong 458b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 459ddfb0baaSMarkus Armbruster char *path = object_get_canonical_path(OBJECT(n->qdev)); 46006150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 4613ab72385SPeter Xu n->netclient_name, path); 46296e35046SAmos Kong g_free(path); 463b1be4280SAmos Kong 464b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 465b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 466b1be4280SAmos Kong } 467b1be4280SAmos Kong } 468b1be4280SAmos Kong 469f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 470f7bc8ef8SAmos Kong { 47154aa3de7SEric Blake intList *list; 472f7bc8ef8SAmos Kong int i, j; 473f7bc8ef8SAmos Kong 474f7bc8ef8SAmos Kong list = NULL; 475f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 476f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 477f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 47854aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 479f7bc8ef8SAmos Kong } 480f7bc8ef8SAmos Kong } 481f7bc8ef8SAmos Kong } 482f7bc8ef8SAmos Kong 483f7bc8ef8SAmos Kong return list; 484f7bc8ef8SAmos Kong } 485f7bc8ef8SAmos Kong 486b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 487b1be4280SAmos Kong { 488b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 489f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 490b1be4280SAmos Kong RxFilterInfo *info; 49154aa3de7SEric Blake strList *str_list; 492f7bc8ef8SAmos Kong int i; 493b1be4280SAmos Kong 494b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 495b1be4280SAmos Kong info->name = g_strdup(nc->name); 496b1be4280SAmos Kong info->promiscuous = n->promisc; 497b1be4280SAmos Kong 498b1be4280SAmos Kong if (n->nouni) { 499b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 500b1be4280SAmos Kong } else if (n->alluni) { 501b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 502b1be4280SAmos Kong } else { 503b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 504b1be4280SAmos Kong } 505b1be4280SAmos Kong 506b1be4280SAmos Kong if (n->nomulti) { 507b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 508b1be4280SAmos Kong } else if (n->allmulti) { 509b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 510b1be4280SAmos Kong } else { 511b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 512b1be4280SAmos Kong } 513b1be4280SAmos Kong 514b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 515b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 516b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 517b1be4280SAmos Kong 518b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 519b1be4280SAmos Kong 520b1be4280SAmos Kong str_list = NULL; 521b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 52254aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 52354aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 524b1be4280SAmos Kong } 525b1be4280SAmos Kong info->unicast_table = str_list; 526b1be4280SAmos Kong 527b1be4280SAmos Kong str_list = NULL; 528b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 52954aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 53054aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 531b1be4280SAmos Kong } 532b1be4280SAmos Kong info->multicast_table = str_list; 533f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 534b1be4280SAmos Kong 53595129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 536f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 537f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 538f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 539f7bc8ef8SAmos Kong } else { 540f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 541b1be4280SAmos Kong } 542b1be4280SAmos Kong 543b1be4280SAmos Kong /* enable event notification after query */ 544b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 545b1be4280SAmos Kong 546b1be4280SAmos Kong return info; 547b1be4280SAmos Kong } 548b1be4280SAmos Kong 5497dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index) 5507dc6be52SXuan Zhuo { 5517dc6be52SXuan Zhuo VirtIONet *n = VIRTIO_NET(vdev); 5527dc6be52SXuan Zhuo NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 5537dc6be52SXuan Zhuo 5547dc6be52SXuan Zhuo if (!nc->peer) { 5557dc6be52SXuan Zhuo return; 5567dc6be52SXuan Zhuo } 5577dc6be52SXuan Zhuo 5587dc6be52SXuan Zhuo if (get_vhost_net(nc->peer) && 5597dc6be52SXuan Zhuo nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5607dc6be52SXuan Zhuo vhost_net_virtqueue_reset(vdev, nc, queue_index); 5617dc6be52SXuan Zhuo } 5627dc6be52SXuan Zhuo 5637dc6be52SXuan Zhuo flush_or_purge_queued_packets(nc); 5647dc6be52SXuan Zhuo } 5657dc6be52SXuan Zhuo 5667f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index) 5677f863302SKangjie Xu { 5687f863302SKangjie Xu VirtIONet *n = VIRTIO_NET(vdev); 5697f863302SKangjie Xu NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 5707f863302SKangjie Xu int r; 5717f863302SKangjie Xu 5727f863302SKangjie Xu if (!nc->peer || !vdev->vhost_started) { 5737f863302SKangjie Xu return; 5747f863302SKangjie Xu } 5757f863302SKangjie Xu 5767f863302SKangjie Xu if (get_vhost_net(nc->peer) && 5777f863302SKangjie Xu nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5787f863302SKangjie Xu r = vhost_net_virtqueue_restart(vdev, nc, queue_index); 5797f863302SKangjie Xu if (r < 0) { 5807f863302SKangjie Xu error_report("unable to restart vhost net virtqueue: %d, " 5817f863302SKangjie Xu "when resetting the queue", queue_index); 5827f863302SKangjie Xu } 5837f863302SKangjie Xu } 5847f863302SKangjie Xu } 5857f863302SKangjie Xu 5866e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 5876e790746SPaolo Bonzini { 58817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 58994b52958SGreg Kurz int i; 5906e790746SPaolo Bonzini 5916e790746SPaolo Bonzini /* Reset back to compatibility mode */ 5926e790746SPaolo Bonzini n->promisc = 1; 5936e790746SPaolo Bonzini n->allmulti = 0; 5946e790746SPaolo Bonzini n->alluni = 0; 5956e790746SPaolo Bonzini n->nomulti = 0; 5966e790746SPaolo Bonzini n->nouni = 0; 5976e790746SPaolo Bonzini n->nobcast = 0; 5986e790746SPaolo Bonzini /* multiqueue is disabled by default */ 599441537f1SJason Wang n->curr_queue_pairs = 1; 6009d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 6019d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 602f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 6036e790746SPaolo Bonzini 6046e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 6056e790746SPaolo Bonzini n->mac_table.in_use = 0; 6066e790746SPaolo Bonzini n->mac_table.first_multi = 0; 6076e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 6086e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 6096e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 6106e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 611702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 6126e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 61394b52958SGreg Kurz 61494b52958SGreg Kurz /* Flush any async TX */ 615441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6164fdf69abSKangjie Xu flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i)); 61794b52958SGreg Kurz } 6186e790746SPaolo Bonzini } 6196e790746SPaolo Bonzini 6206e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 6216e790746SPaolo Bonzini { 6226e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 6236e790746SPaolo Bonzini if (!nc->peer) { 6246e790746SPaolo Bonzini return; 6256e790746SPaolo Bonzini } 6266e790746SPaolo Bonzini 627d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 6286e790746SPaolo Bonzini } 6296e790746SPaolo Bonzini 6306e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 6316e790746SPaolo Bonzini { 6326e790746SPaolo Bonzini return n->has_vnet_hdr; 6336e790746SPaolo Bonzini } 6346e790746SPaolo Bonzini 6356e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 6366e790746SPaolo Bonzini { 6376e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 6386e790746SPaolo Bonzini return 0; 6396e790746SPaolo Bonzini 640d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 6416e790746SPaolo Bonzini 6426e790746SPaolo Bonzini return n->has_ufo; 6436e790746SPaolo Bonzini } 6446e790746SPaolo Bonzini 645bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 646e22f0603SYuri Benditovich int version_1, int hash_report) 6476e790746SPaolo Bonzini { 6486e790746SPaolo Bonzini int i; 6496e790746SPaolo Bonzini NetClientState *nc; 6506e790746SPaolo Bonzini 6516e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6526e790746SPaolo Bonzini 653bb9d17f8SCornelia Huck if (version_1) { 654e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 655e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 656e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 657e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 658bb9d17f8SCornelia Huck } else { 6596e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 660bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 661bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 662bb9d17f8SCornelia Huck } 6636e790746SPaolo Bonzini 664441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6656e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6666e790746SPaolo Bonzini 6676e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 668d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 669d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6706e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6716e790746SPaolo Bonzini } 6726e790746SPaolo Bonzini } 6736e790746SPaolo Bonzini } 6746e790746SPaolo Bonzini 6752eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6762eef278bSMichael S. Tsirkin { 6772eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6782eef278bSMichael S. Tsirkin 6792eef278bSMichael S. Tsirkin /* 6800ea5778fSEugenio Pérez * Backends other than vhost-user or vhost-vdpa don't support max queue 6810ea5778fSEugenio Pérez * size. 6822eef278bSMichael S. Tsirkin */ 6832eef278bSMichael S. Tsirkin if (!peer) { 6842eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6852eef278bSMichael S. Tsirkin } 6862eef278bSMichael S. Tsirkin 6870ea5778fSEugenio Pérez switch(peer->info->type) { 6880ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_USER: 6890ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_VDPA: 6902eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 6910ea5778fSEugenio Pérez default: 6920ea5778fSEugenio Pérez return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6930ea5778fSEugenio Pérez }; 6942eef278bSMichael S. Tsirkin } 6952eef278bSMichael S. Tsirkin 6966e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 6976e790746SPaolo Bonzini { 6986e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 6996e790746SPaolo Bonzini 7006e790746SPaolo Bonzini if (!nc->peer) { 7016e790746SPaolo Bonzini return 0; 7026e790746SPaolo Bonzini } 7036e790746SPaolo Bonzini 704f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7057263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 7067263a0adSChangchun Ouyang } 7077263a0adSChangchun Ouyang 708f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7096e790746SPaolo Bonzini return 0; 7106e790746SPaolo Bonzini } 7116e790746SPaolo Bonzini 712441537f1SJason Wang if (n->max_queue_pairs == 1) { 7131074b879SJason Wang return 0; 7141074b879SJason Wang } 7151074b879SJason Wang 7166e790746SPaolo Bonzini return tap_enable(nc->peer); 7176e790746SPaolo Bonzini } 7186e790746SPaolo Bonzini 7196e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 7206e790746SPaolo Bonzini { 7216e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7226e790746SPaolo Bonzini 7236e790746SPaolo Bonzini if (!nc->peer) { 7246e790746SPaolo Bonzini return 0; 7256e790746SPaolo Bonzini } 7266e790746SPaolo Bonzini 727f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7287263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 7297263a0adSChangchun Ouyang } 7307263a0adSChangchun Ouyang 731f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7326e790746SPaolo Bonzini return 0; 7336e790746SPaolo Bonzini } 7346e790746SPaolo Bonzini 7356e790746SPaolo Bonzini return tap_disable(nc->peer); 7366e790746SPaolo Bonzini } 7376e790746SPaolo Bonzini 738441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 7396e790746SPaolo Bonzini { 7406e790746SPaolo Bonzini int i; 741ddfa83eaSJoel Stanley int r; 7426e790746SPaolo Bonzini 74368b5f314SYuri Benditovich if (n->nic->peer_deleted) { 74468b5f314SYuri Benditovich return; 74568b5f314SYuri Benditovich } 74668b5f314SYuri Benditovich 747441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 748441537f1SJason Wang if (i < n->curr_queue_pairs) { 749ddfa83eaSJoel Stanley r = peer_attach(n, i); 750ddfa83eaSJoel Stanley assert(!r); 7516e790746SPaolo Bonzini } else { 752ddfa83eaSJoel Stanley r = peer_detach(n, i); 753ddfa83eaSJoel Stanley assert(!r); 7546e790746SPaolo Bonzini } 7556e790746SPaolo Bonzini } 7566e790746SPaolo Bonzini } 7576e790746SPaolo Bonzini 758ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7596e790746SPaolo Bonzini 7609d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7619d5b731dSJason Wang Error **errp) 7626e790746SPaolo Bonzini { 76317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7646e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7656e790746SPaolo Bonzini 766da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 767da3e8a23SShannon Zhao features |= n->host_features; 768da3e8a23SShannon Zhao 7690cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7706e790746SPaolo Bonzini 7716e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7720cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7730cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7740cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7750cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7766e790746SPaolo Bonzini 7770cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7780cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7790cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7800cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 781e22f0603SYuri Benditovich 782e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7836e790746SPaolo Bonzini } 7846e790746SPaolo Bonzini 7856e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7860cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 7870cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 7886e790746SPaolo Bonzini } 7896e790746SPaolo Bonzini 790ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 791*93a97dc5SXuan Zhuo virtio_add_feature(&features, VIRTIO_F_RING_RESET); 7926e790746SPaolo Bonzini return features; 7936e790746SPaolo Bonzini } 7942974e916SYuri Benditovich 7950145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 79659079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 7970145c393SAndrew Melnychenko } 79875ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 79975ebec11SMaxime Coquelin vdev->backend_features = features; 80075ebec11SMaxime Coquelin 80175ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 80275ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 80375ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 80475ebec11SMaxime Coquelin } 80575ebec11SMaxime Coquelin 80675ebec11SMaxime Coquelin return features; 8076e790746SPaolo Bonzini } 8086e790746SPaolo Bonzini 809019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 8106e790746SPaolo Bonzini { 811019a3edbSGerd Hoffmann uint64_t features = 0; 8126e790746SPaolo Bonzini 8136e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 8146e790746SPaolo Bonzini * but also these: */ 8150cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 8160cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 8170cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 8180cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 8190cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 8206e790746SPaolo Bonzini 8216e790746SPaolo Bonzini return features; 8226e790746SPaolo Bonzini } 8236e790746SPaolo Bonzini 824644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 825644c9858SDmitry Fleytman { 826ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 827644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 828644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 829644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 830644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 831644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 832644c9858SDmitry Fleytman } 833644c9858SDmitry Fleytman 834644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 835644c9858SDmitry Fleytman { 836644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 837644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 838644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 839644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 840644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 841644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 842644c9858SDmitry Fleytman 843644c9858SDmitry Fleytman return guest_offloads_mask & features; 844644c9858SDmitry Fleytman } 845644c9858SDmitry Fleytman 846644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 847644c9858SDmitry Fleytman { 848644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 849644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 850644c9858SDmitry Fleytman } 851644c9858SDmitry Fleytman 852f5e1847bSJuan Quintela typedef struct { 853f5e1847bSJuan Quintela VirtIONet *n; 85412b2fad7SKevin Wolf DeviceState *dev; 85512b2fad7SKevin Wolf } FailoverDevice; 856f5e1847bSJuan Quintela 857f5e1847bSJuan Quintela /** 85812b2fad7SKevin Wolf * Set the failover primary device 859f5e1847bSJuan Quintela * 860f5e1847bSJuan Quintela * @opaque: FailoverId to setup 861f5e1847bSJuan Quintela * @opts: opts for device we are handling 862f5e1847bSJuan Quintela * @errp: returns an error if this function fails 863f5e1847bSJuan Quintela */ 86412b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 865f5e1847bSJuan Quintela { 86612b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 86712b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 86812b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 869f5e1847bSJuan Quintela 87012b2fad7SKevin Wolf if (!pci_dev) { 87112b2fad7SKevin Wolf return 0; 87212b2fad7SKevin Wolf } 87312b2fad7SKevin Wolf 87412b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 87512b2fad7SKevin Wolf fdev->dev = dev; 876f5e1847bSJuan Quintela return 1; 877f5e1847bSJuan Quintela } 878f5e1847bSJuan Quintela 879f5e1847bSJuan Quintela return 0; 880f5e1847bSJuan Quintela } 881f5e1847bSJuan Quintela 882f5e1847bSJuan Quintela /** 88385d3b931SJuan Quintela * Find the primary device for this failover virtio-net 88485d3b931SJuan Quintela * 88585d3b931SJuan Quintela * @n: VirtIONet device 88685d3b931SJuan Quintela * @errp: returns an error if this function fails 88785d3b931SJuan Quintela */ 8880a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 8899711cd0dSJens Freimann { 89012b2fad7SKevin Wolf FailoverDevice fdev = { 89112b2fad7SKevin Wolf .n = n, 89212b2fad7SKevin Wolf }; 8939711cd0dSJens Freimann 89412b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 89512b2fad7SKevin Wolf NULL, NULL, &fdev); 89612b2fad7SKevin Wolf return fdev.dev; 8979711cd0dSJens Freimann } 8989711cd0dSJens Freimann 89921e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 90021e8709bSJuan Quintela { 90121e8709bSJuan Quintela Error *err = NULL; 90221e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 90321e8709bSJuan Quintela 90421e8709bSJuan Quintela if (dev) { 90521e8709bSJuan Quintela return; 90621e8709bSJuan Quintela } 90721e8709bSJuan Quintela 908259a10dbSKevin Wolf if (!n->primary_opts) { 90997ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 91097ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 91197ca9c59SLaurent Vivier "sure primary device has parameter" 91297ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 91321e8709bSJuan Quintela return; 91421e8709bSJuan Quintela } 915259a10dbSKevin Wolf 916f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 917f3558b1bSKevin Wolf n->primary_opts_from_json, 918f3558b1bSKevin Wolf &err); 91921e8709bSJuan Quintela if (err) { 920f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 921259a10dbSKevin Wolf n->primary_opts = NULL; 92200e7b129SLaurent Vivier } else { 92300e7b129SLaurent Vivier object_unref(OBJECT(dev)); 92421e8709bSJuan Quintela } 92521e8709bSJuan Quintela error_propagate(errp, err); 92621e8709bSJuan Quintela } 92721e8709bSJuan Quintela 928d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 9296e790746SPaolo Bonzini { 93017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9319711cd0dSJens Freimann Error *err = NULL; 9326e790746SPaolo Bonzini int i; 9336e790746SPaolo Bonzini 93475ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 93575ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 93675ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 93775ebec11SMaxime Coquelin } 93875ebec11SMaxime Coquelin 939ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 94059079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 94195129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 9426e790746SPaolo Bonzini 943ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 94495129d6fSCornelia Huck virtio_has_feature(features, 945bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 94695129d6fSCornelia Huck virtio_has_feature(features, 947e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 948e22f0603SYuri Benditovich virtio_has_feature(features, 949e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 9506e790746SPaolo Bonzini 9512974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9522974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 9532974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9542974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 955e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 9562974e916SYuri Benditovich 9576e790746SPaolo Bonzini if (n->has_vnet_hdr) { 958644c9858SDmitry Fleytman n->curr_guest_offloads = 959644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 960644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 9616e790746SPaolo Bonzini } 9626e790746SPaolo Bonzini 963441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 9646e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9656e790746SPaolo Bonzini 966ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9676e790746SPaolo Bonzini continue; 9686e790746SPaolo Bonzini } 969ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 9706e790746SPaolo Bonzini } 9710b1eaa88SStefan Fritsch 97295129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 9730b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 9740b1eaa88SStefan Fritsch } else { 9750b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 9760b1eaa88SStefan Fritsch } 9779711cd0dSJens Freimann 9789711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 9799711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 980e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 9819711cd0dSJens Freimann failover_add_primary(n, &err); 9829711cd0dSJens Freimann if (err) { 9831b529d90SLaurent Vivier if (!qtest_enabled()) { 9849711cd0dSJens Freimann warn_report_err(err); 9851b529d90SLaurent Vivier } else { 9861b529d90SLaurent Vivier error_free(err); 9871b529d90SLaurent Vivier } 9889711cd0dSJens Freimann } 9896e790746SPaolo Bonzini } 99021e8709bSJuan Quintela } 9916e790746SPaolo Bonzini 9926e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 9936e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 9946e790746SPaolo Bonzini { 9956e790746SPaolo Bonzini uint8_t on; 9966e790746SPaolo Bonzini size_t s; 997b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 9986e790746SPaolo Bonzini 9996e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 10006e790746SPaolo Bonzini if (s != sizeof(on)) { 10016e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10026e790746SPaolo Bonzini } 10036e790746SPaolo Bonzini 10046e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 10056e790746SPaolo Bonzini n->promisc = on; 10066e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 10076e790746SPaolo Bonzini n->allmulti = on; 10086e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 10096e790746SPaolo Bonzini n->alluni = on; 10106e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 10116e790746SPaolo Bonzini n->nomulti = on; 10126e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 10136e790746SPaolo Bonzini n->nouni = on; 10146e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 10156e790746SPaolo Bonzini n->nobcast = on; 10166e790746SPaolo Bonzini } else { 10176e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10186e790746SPaolo Bonzini } 10196e790746SPaolo Bonzini 1020b1be4280SAmos Kong rxfilter_notify(nc); 1021b1be4280SAmos Kong 10226e790746SPaolo Bonzini return VIRTIO_NET_OK; 10236e790746SPaolo Bonzini } 10246e790746SPaolo Bonzini 1025644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 1026644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 1027644c9858SDmitry Fleytman { 1028644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 1029644c9858SDmitry Fleytman uint64_t offloads; 1030644c9858SDmitry Fleytman size_t s; 1031644c9858SDmitry Fleytman 103295129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 1033644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1034644c9858SDmitry Fleytman } 1035644c9858SDmitry Fleytman 1036644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 1037644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 1038644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1039644c9858SDmitry Fleytman } 1040644c9858SDmitry Fleytman 1041644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 1042644c9858SDmitry Fleytman uint64_t supported_offloads; 1043644c9858SDmitry Fleytman 1044189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 1045189ae6bbSJason Wang 1046644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 1047644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1048644c9858SDmitry Fleytman } 1049644c9858SDmitry Fleytman 10502974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10512974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 10522974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10532974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 10542974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 10552974e916SYuri Benditovich 1056644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1057644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1058644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1059644c9858SDmitry Fleytman } 1060644c9858SDmitry Fleytman 1061644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1062644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1063644c9858SDmitry Fleytman 1064644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1065644c9858SDmitry Fleytman } else { 1066644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1067644c9858SDmitry Fleytman } 1068644c9858SDmitry Fleytman } 1069644c9858SDmitry Fleytman 10706e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 10716e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10726e790746SPaolo Bonzini { 10731399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 10746e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 10756e790746SPaolo Bonzini size_t s; 1076b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10776e790746SPaolo Bonzini 10786e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 10796e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 10806e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10816e790746SPaolo Bonzini } 10826e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 10836e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 10846e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1085b1be4280SAmos Kong rxfilter_notify(nc); 1086b1be4280SAmos Kong 10876e790746SPaolo Bonzini return VIRTIO_NET_OK; 10886e790746SPaolo Bonzini } 10896e790746SPaolo Bonzini 10906e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 10916e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10926e790746SPaolo Bonzini } 10936e790746SPaolo Bonzini 1094cae2e556SAmos Kong int in_use = 0; 1095cae2e556SAmos Kong int first_multi = 0; 1096cae2e556SAmos Kong uint8_t uni_overflow = 0; 1097cae2e556SAmos Kong uint8_t multi_overflow = 0; 1098cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 10996e790746SPaolo Bonzini 11006e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11016e790746SPaolo Bonzini sizeof(mac_data.entries)); 11021399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11036e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1104b1be4280SAmos Kong goto error; 11056e790746SPaolo Bonzini } 11066e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11076e790746SPaolo Bonzini 11086e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1109b1be4280SAmos Kong goto error; 11106e790746SPaolo Bonzini } 11116e790746SPaolo Bonzini 11126e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1113cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 11146e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11156e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1116b1be4280SAmos Kong goto error; 11176e790746SPaolo Bonzini } 1118cae2e556SAmos Kong in_use += mac_data.entries; 11196e790746SPaolo Bonzini } else { 1120cae2e556SAmos Kong uni_overflow = 1; 11216e790746SPaolo Bonzini } 11226e790746SPaolo Bonzini 11236e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 11246e790746SPaolo Bonzini 1125cae2e556SAmos Kong first_multi = in_use; 11266e790746SPaolo Bonzini 11276e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11286e790746SPaolo Bonzini sizeof(mac_data.entries)); 11291399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11306e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1131b1be4280SAmos Kong goto error; 11326e790746SPaolo Bonzini } 11336e790746SPaolo Bonzini 11346e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11356e790746SPaolo Bonzini 11366e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1137b1be4280SAmos Kong goto error; 11386e790746SPaolo Bonzini } 11396e790746SPaolo Bonzini 1140edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1141cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 11426e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11436e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1144b1be4280SAmos Kong goto error; 11456e790746SPaolo Bonzini } 1146cae2e556SAmos Kong in_use += mac_data.entries; 11476e790746SPaolo Bonzini } else { 1148cae2e556SAmos Kong multi_overflow = 1; 11496e790746SPaolo Bonzini } 11506e790746SPaolo Bonzini 1151cae2e556SAmos Kong n->mac_table.in_use = in_use; 1152cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1153cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1154cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1155cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1156cae2e556SAmos Kong g_free(macs); 1157b1be4280SAmos Kong rxfilter_notify(nc); 1158b1be4280SAmos Kong 11596e790746SPaolo Bonzini return VIRTIO_NET_OK; 1160b1be4280SAmos Kong 1161b1be4280SAmos Kong error: 1162cae2e556SAmos Kong g_free(macs); 1163b1be4280SAmos Kong return VIRTIO_NET_ERR; 11646e790746SPaolo Bonzini } 11656e790746SPaolo Bonzini 11666e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 11676e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11686e790746SPaolo Bonzini { 11691399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11706e790746SPaolo Bonzini uint16_t vid; 11716e790746SPaolo Bonzini size_t s; 1172b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11736e790746SPaolo Bonzini 11746e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 11751399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 11766e790746SPaolo Bonzini if (s != sizeof(vid)) { 11776e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11786e790746SPaolo Bonzini } 11796e790746SPaolo Bonzini 11806e790746SPaolo Bonzini if (vid >= MAX_VLAN) 11816e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11826e790746SPaolo Bonzini 11836e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 11846e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 11856e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 11866e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 11876e790746SPaolo Bonzini else 11886e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11896e790746SPaolo Bonzini 1190b1be4280SAmos Kong rxfilter_notify(nc); 1191b1be4280SAmos Kong 11926e790746SPaolo Bonzini return VIRTIO_NET_OK; 11936e790746SPaolo Bonzini } 11946e790746SPaolo Bonzini 1195f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1196f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1197f57fcf70SJason Wang { 11989d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1199f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1200f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1201f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 12029d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 12039d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1204f57fcf70SJason Wang } 1205f57fcf70SJason Wang return VIRTIO_NET_OK; 1206f57fcf70SJason Wang } else { 1207f57fcf70SJason Wang return VIRTIO_NET_ERR; 1208f57fcf70SJason Wang } 1209f57fcf70SJason Wang } 1210f57fcf70SJason Wang 12110145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n); 12120145c393SAndrew Melnychenko 121359079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 121459079029SYuri Benditovich { 121559079029SYuri Benditovich if (n->rss_data.enabled) { 121659079029SYuri Benditovich trace_virtio_net_rss_disable(); 121759079029SYuri Benditovich } 121859079029SYuri Benditovich n->rss_data.enabled = false; 12190145c393SAndrew Melnychenko 12200145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 12210145c393SAndrew Melnychenko } 12220145c393SAndrew Melnychenko 12230145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 12240145c393SAndrew Melnychenko { 12250145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 12260145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 12270145c393SAndrew Melnychenko return false; 12280145c393SAndrew Melnychenko } 12290145c393SAndrew Melnychenko 12300145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 12310145c393SAndrew Melnychenko } 12320145c393SAndrew Melnychenko 12330145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 12340145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 12350145c393SAndrew Melnychenko { 12360145c393SAndrew Melnychenko config->redirect = data->redirect; 12370145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 12380145c393SAndrew Melnychenko config->hash_types = data->hash_types; 12390145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 12400145c393SAndrew Melnychenko config->default_queue = data->default_queue; 12410145c393SAndrew Melnychenko } 12420145c393SAndrew Melnychenko 12430145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 12440145c393SAndrew Melnychenko { 12450145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 12460145c393SAndrew Melnychenko 12470145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 12480145c393SAndrew Melnychenko return false; 12490145c393SAndrew Melnychenko } 12500145c393SAndrew Melnychenko 12510145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 12520145c393SAndrew Melnychenko 12530145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 12540145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 12550145c393SAndrew Melnychenko return false; 12560145c393SAndrew Melnychenko } 12570145c393SAndrew Melnychenko 12580145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 12590145c393SAndrew Melnychenko return false; 12600145c393SAndrew Melnychenko } 12610145c393SAndrew Melnychenko 12620145c393SAndrew Melnychenko return true; 12630145c393SAndrew Melnychenko } 12640145c393SAndrew Melnychenko 12650145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 12660145c393SAndrew Melnychenko { 12670145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12680145c393SAndrew Melnychenko } 12690145c393SAndrew Melnychenko 12700145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n) 12710145c393SAndrew Melnychenko { 12720145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 12730145c393SAndrew Melnychenko /* backend does't support steering ebpf */ 12740145c393SAndrew Melnychenko return false; 12750145c393SAndrew Melnychenko } 12760145c393SAndrew Melnychenko 12770145c393SAndrew Melnychenko return ebpf_rss_load(&n->ebpf_rss); 12780145c393SAndrew Melnychenko } 12790145c393SAndrew Melnychenko 12800145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 12810145c393SAndrew Melnychenko { 12820145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12830145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 128459079029SYuri Benditovich } 128559079029SYuri Benditovich 128659079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1287e22f0603SYuri Benditovich struct iovec *iov, 1288e22f0603SYuri Benditovich unsigned int iov_cnt, 1289e22f0603SYuri Benditovich bool do_rss) 129059079029SYuri Benditovich { 129159079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 129259079029SYuri Benditovich struct virtio_net_rss_config cfg; 129359079029SYuri Benditovich size_t s, offset = 0, size_get; 1294441537f1SJason Wang uint16_t queue_pairs, i; 129559079029SYuri Benditovich struct { 129659079029SYuri Benditovich uint16_t us; 129759079029SYuri Benditovich uint8_t b; 129859079029SYuri Benditovich } QEMU_PACKED temp; 129959079029SYuri Benditovich const char *err_msg = ""; 130059079029SYuri Benditovich uint32_t err_value = 0; 130159079029SYuri Benditovich 1302e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 130359079029SYuri Benditovich err_msg = "RSS is not negotiated"; 130459079029SYuri Benditovich goto error; 130559079029SYuri Benditovich } 1306e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1307e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1308e22f0603SYuri Benditovich goto error; 1309e22f0603SYuri Benditovich } 131059079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 131159079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 131259079029SYuri Benditovich if (s != size_get) { 131359079029SYuri Benditovich err_msg = "Short command buffer"; 131459079029SYuri Benditovich err_value = (uint32_t)s; 131559079029SYuri Benditovich goto error; 131659079029SYuri Benditovich } 131759079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 131859079029SYuri Benditovich n->rss_data.indirections_len = 131959079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 132059079029SYuri Benditovich n->rss_data.indirections_len++; 1321e22f0603SYuri Benditovich if (!do_rss) { 1322e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1323e22f0603SYuri Benditovich } 132459079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 132559079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 132659079029SYuri Benditovich err_value = n->rss_data.indirections_len; 132759079029SYuri Benditovich goto error; 132859079029SYuri Benditovich } 132959079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 133059079029SYuri Benditovich err_msg = "Too large indirection table"; 133159079029SYuri Benditovich err_value = n->rss_data.indirections_len; 133259079029SYuri Benditovich goto error; 133359079029SYuri Benditovich } 1334e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1335e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1336441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 133759079029SYuri Benditovich err_msg = "Invalid default queue"; 133859079029SYuri Benditovich err_value = n->rss_data.default_queue; 133959079029SYuri Benditovich goto error; 134059079029SYuri Benditovich } 134159079029SYuri Benditovich offset += size_get; 134259079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 134359079029SYuri Benditovich g_free(n->rss_data.indirections_table); 134459079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 134559079029SYuri Benditovich if (!n->rss_data.indirections_table) { 134659079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 134759079029SYuri Benditovich err_value = n->rss_data.indirections_len; 134859079029SYuri Benditovich goto error; 134959079029SYuri Benditovich } 135059079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 135159079029SYuri Benditovich n->rss_data.indirections_table, size_get); 135259079029SYuri Benditovich if (s != size_get) { 135359079029SYuri Benditovich err_msg = "Short indirection table buffer"; 135459079029SYuri Benditovich err_value = (uint32_t)s; 135559079029SYuri Benditovich goto error; 135659079029SYuri Benditovich } 135759079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 135859079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 135959079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 136059079029SYuri Benditovich } 136159079029SYuri Benditovich offset += size_get; 136259079029SYuri Benditovich size_get = sizeof(temp); 136359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 136459079029SYuri Benditovich if (s != size_get) { 1365441537f1SJason Wang err_msg = "Can't get queue_pairs"; 136659079029SYuri Benditovich err_value = (uint32_t)s; 136759079029SYuri Benditovich goto error; 136859079029SYuri Benditovich } 1369441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1370441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1371441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1372441537f1SJason Wang err_value = queue_pairs; 137359079029SYuri Benditovich goto error; 137459079029SYuri Benditovich } 137559079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 137659079029SYuri Benditovich err_msg = "Invalid key size"; 137759079029SYuri Benditovich err_value = temp.b; 137859079029SYuri Benditovich goto error; 137959079029SYuri Benditovich } 138059079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 138159079029SYuri Benditovich err_msg = "No key provided"; 138259079029SYuri Benditovich err_value = 0; 138359079029SYuri Benditovich goto error; 138459079029SYuri Benditovich } 138559079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 138659079029SYuri Benditovich virtio_net_disable_rss(n); 1387441537f1SJason Wang return queue_pairs; 138859079029SYuri Benditovich } 138959079029SYuri Benditovich offset += size_get; 139059079029SYuri Benditovich size_get = temp.b; 139159079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 139259079029SYuri Benditovich if (s != size_get) { 139359079029SYuri Benditovich err_msg = "Can get key buffer"; 139459079029SYuri Benditovich err_value = (uint32_t)s; 139559079029SYuri Benditovich goto error; 139659079029SYuri Benditovich } 139759079029SYuri Benditovich n->rss_data.enabled = true; 13980145c393SAndrew Melnychenko 13990145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 14000145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 14010145c393SAndrew Melnychenko /* EBPF must be loaded for vhost */ 14020145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 14030145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS for vhost"); 14040145c393SAndrew Melnychenko goto error; 14050145c393SAndrew Melnychenko } 14060145c393SAndrew Melnychenko /* fallback to software RSS */ 14070145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS - fallback to software RSS"); 14080145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 14090145c393SAndrew Melnychenko } 14100145c393SAndrew Melnychenko } else { 14110145c393SAndrew Melnychenko /* use software RSS for hash populating */ 14120145c393SAndrew Melnychenko /* and detach eBPF if was loaded before */ 14130145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 14140145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 14150145c393SAndrew Melnychenko } 14160145c393SAndrew Melnychenko 141759079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 141859079029SYuri Benditovich n->rss_data.indirections_len, 141959079029SYuri Benditovich temp.b); 1420441537f1SJason Wang return queue_pairs; 142159079029SYuri Benditovich error: 142259079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 142359079029SYuri Benditovich virtio_net_disable_rss(n); 142459079029SYuri Benditovich return 0; 142559079029SYuri Benditovich } 142659079029SYuri Benditovich 14276e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 14286e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 14296e790746SPaolo Bonzini { 143017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1431441537f1SJason Wang uint16_t queue_pairs; 14322a7888ccSSi-Wei Liu NetClientState *nc = qemu_get_queue(n->nic); 14336e790746SPaolo Bonzini 143459079029SYuri Benditovich virtio_net_disable_rss(n); 1435e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1436441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1437441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1438e22f0603SYuri Benditovich } 143959079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1440441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 144159079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 144259079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 144359079029SYuri Benditovich size_t s; 144459079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 144559079029SYuri Benditovich return VIRTIO_NET_ERR; 144659079029SYuri Benditovich } 14476e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 14486e790746SPaolo Bonzini if (s != sizeof(mq)) { 14496e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14506e790746SPaolo Bonzini } 1451441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 14526e790746SPaolo Bonzini 145359079029SYuri Benditovich } else { 14546e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14556e790746SPaolo Bonzini } 14566e790746SPaolo Bonzini 1457441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1458441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1459441537f1SJason Wang queue_pairs > n->max_queue_pairs || 14606e790746SPaolo Bonzini !n->multiqueue) { 14616e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14626e790746SPaolo Bonzini } 14636e790746SPaolo Bonzini 1464441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1465ca8717f9SEugenio Pérez if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 1466ca8717f9SEugenio Pérez /* 1467ca8717f9SEugenio Pérez * Avoid updating the backend for a vdpa device: We're only interested 1468ca8717f9SEugenio Pérez * in updating the device model queues. 1469ca8717f9SEugenio Pérez */ 1470ca8717f9SEugenio Pérez return VIRTIO_NET_OK; 1471ca8717f9SEugenio Pérez } 1472441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 14736e790746SPaolo Bonzini * disabled queue */ 147417a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1475441537f1SJason Wang virtio_net_set_queue_pairs(n); 14766e790746SPaolo Bonzini 14776e790746SPaolo Bonzini return VIRTIO_NET_OK; 14786e790746SPaolo Bonzini } 1479ba7eadb5SGreg Kurz 1480640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, 1481640b8a1cSEugenio Pérez const struct iovec *in_sg, unsigned in_num, 1482640b8a1cSEugenio Pérez const struct iovec *out_sg, 1483640b8a1cSEugenio Pérez unsigned out_num) 14846e790746SPaolo Bonzini { 148517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14866e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 14876e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 14886e790746SPaolo Bonzini size_t s; 1489771b6ed3SJason Wang struct iovec *iov, *iov2; 1490640b8a1cSEugenio Pérez 1491640b8a1cSEugenio Pérez if (iov_size(in_sg, in_num) < sizeof(status) || 1492640b8a1cSEugenio Pérez iov_size(out_sg, out_num) < sizeof(ctrl)) { 1493640b8a1cSEugenio Pérez virtio_error(vdev, "virtio-net ctrl missing headers"); 1494640b8a1cSEugenio Pérez return 0; 1495640b8a1cSEugenio Pérez } 1496640b8a1cSEugenio Pérez 1497640b8a1cSEugenio Pérez iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num); 1498640b8a1cSEugenio Pérez s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl)); 1499640b8a1cSEugenio Pérez iov_discard_front(&iov, &out_num, sizeof(ctrl)); 1500640b8a1cSEugenio Pérez if (s != sizeof(ctrl)) { 1501640b8a1cSEugenio Pérez status = VIRTIO_NET_ERR; 1502640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 1503640b8a1cSEugenio Pérez status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num); 1504640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 1505640b8a1cSEugenio Pérez status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num); 1506640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 1507640b8a1cSEugenio Pérez status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num); 1508640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1509640b8a1cSEugenio Pérez status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num); 1510640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 1511640b8a1cSEugenio Pérez status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num); 1512640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1513640b8a1cSEugenio Pérez status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num); 1514640b8a1cSEugenio Pérez } 1515640b8a1cSEugenio Pérez 1516640b8a1cSEugenio Pérez s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status)); 1517640b8a1cSEugenio Pérez assert(s == sizeof(status)); 1518640b8a1cSEugenio Pérez 1519640b8a1cSEugenio Pérez g_free(iov2); 1520640b8a1cSEugenio Pérez return sizeof(status); 1521640b8a1cSEugenio Pérez } 1522640b8a1cSEugenio Pérez 1523640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 1524640b8a1cSEugenio Pérez { 1525640b8a1cSEugenio Pérez VirtQueueElement *elem; 15266e790746SPaolo Bonzini 152751b19ebeSPaolo Bonzini for (;;) { 1528640b8a1cSEugenio Pérez size_t written; 152951b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 153051b19ebeSPaolo Bonzini if (!elem) { 153151b19ebeSPaolo Bonzini break; 153251b19ebeSPaolo Bonzini } 1533640b8a1cSEugenio Pérez 1534640b8a1cSEugenio Pérez written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num, 1535640b8a1cSEugenio Pérez elem->out_sg, elem->out_num); 1536640b8a1cSEugenio Pérez if (written > 0) { 1537640b8a1cSEugenio Pérez virtqueue_push(vq, elem, written); 1538640b8a1cSEugenio Pérez virtio_notify(vdev, vq); 1539640b8a1cSEugenio Pérez g_free(elem); 1540640b8a1cSEugenio Pérez } else { 1541ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1542ba7eadb5SGreg Kurz g_free(elem); 1543ba7eadb5SGreg Kurz break; 15446e790746SPaolo Bonzini } 15456e790746SPaolo Bonzini } 15466e790746SPaolo Bonzini } 15476e790746SPaolo Bonzini 15486e790746SPaolo Bonzini /* RX */ 15496e790746SPaolo Bonzini 15506e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 15516e790746SPaolo Bonzini { 155217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 15536e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 15546e790746SPaolo Bonzini 15556e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 15566e790746SPaolo Bonzini } 15576e790746SPaolo Bonzini 1558b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 15596e790746SPaolo Bonzini { 15606e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 156117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 15626e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 15636e790746SPaolo Bonzini 156417a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1565b8c4b67eSPhilippe Mathieu-Daudé return false; 15666e790746SPaolo Bonzini } 15676e790746SPaolo Bonzini 1568441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1569b8c4b67eSPhilippe Mathieu-Daudé return false; 15706e790746SPaolo Bonzini } 15716e790746SPaolo Bonzini 15726e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 157317a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1574b8c4b67eSPhilippe Mathieu-Daudé return false; 15756e790746SPaolo Bonzini } 15766e790746SPaolo Bonzini 1577b8c4b67eSPhilippe Mathieu-Daudé return true; 15786e790746SPaolo Bonzini } 15796e790746SPaolo Bonzini 15806e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 15816e790746SPaolo Bonzini { 15826e790746SPaolo Bonzini VirtIONet *n = q->n; 15836e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15846e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15856e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15866e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 15876e790746SPaolo Bonzini 15886e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 15896e790746SPaolo Bonzini * available after the above check but before notification was 15906e790746SPaolo Bonzini * enabled, check for available buffers again. 15916e790746SPaolo Bonzini */ 15926e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15936e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15946e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15956e790746SPaolo Bonzini return 0; 15966e790746SPaolo Bonzini } 15976e790746SPaolo Bonzini } 15986e790746SPaolo Bonzini 15996e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 16006e790746SPaolo Bonzini return 1; 16016e790746SPaolo Bonzini } 16026e790746SPaolo Bonzini 16031399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1604032a74a1SCédric Le Goater { 16051399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 16061399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 16071399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 16081399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1609032a74a1SCédric Le Goater } 1610032a74a1SCédric Le Goater 16116e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 16126e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 16136e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 16146e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 16156e790746SPaolo Bonzini * dhclient yet. 16166e790746SPaolo Bonzini * 16176e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 16186e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 16196e790746SPaolo Bonzini * kernels. 16206e790746SPaolo Bonzini * 16216e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 16226e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 16236e790746SPaolo Bonzini * cache. 16246e790746SPaolo Bonzini */ 16256e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 16266e790746SPaolo Bonzini uint8_t *buf, size_t size) 16276e790746SPaolo Bonzini { 16286e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 16296e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 16306e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 16316e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 16326e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1633f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 16346e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 16356e790746SPaolo Bonzini } 16366e790746SPaolo Bonzini } 16376e790746SPaolo Bonzini 16386e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 16396e790746SPaolo Bonzini const void *buf, size_t size) 16406e790746SPaolo Bonzini { 16416e790746SPaolo Bonzini if (n->has_vnet_hdr) { 16426e790746SPaolo Bonzini /* FIXME this cast is evil */ 16436e790746SPaolo Bonzini void *wbuf = (void *)buf; 16446e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 16456e790746SPaolo Bonzini size - n->host_hdr_len); 16461bfa316cSGreg Kurz 16471bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 16481399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 16491bfa316cSGreg Kurz } 16506e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 16516e790746SPaolo Bonzini } else { 16526e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 16536e790746SPaolo Bonzini .flags = 0, 16546e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 16556e790746SPaolo Bonzini }; 16566e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 16576e790746SPaolo Bonzini } 16586e790746SPaolo Bonzini } 16596e790746SPaolo Bonzini 16606e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 16616e790746SPaolo Bonzini { 16626e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 16636e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 16646e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 16656e790746SPaolo Bonzini int i; 16666e790746SPaolo Bonzini 16676e790746SPaolo Bonzini if (n->promisc) 16686e790746SPaolo Bonzini return 1; 16696e790746SPaolo Bonzini 16706e790746SPaolo Bonzini ptr += n->host_hdr_len; 16716e790746SPaolo Bonzini 16726e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 16737542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 16746e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 16756e790746SPaolo Bonzini return 0; 16766e790746SPaolo Bonzini } 16776e790746SPaolo Bonzini 16786e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 16796e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 16806e790746SPaolo Bonzini return !n->nobcast; 16816e790746SPaolo Bonzini } else if (n->nomulti) { 16826e790746SPaolo Bonzini return 0; 16836e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 16846e790746SPaolo Bonzini return 1; 16856e790746SPaolo Bonzini } 16866e790746SPaolo Bonzini 16876e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 16886e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 16896e790746SPaolo Bonzini return 1; 16906e790746SPaolo Bonzini } 16916e790746SPaolo Bonzini } 16926e790746SPaolo Bonzini } else { // unicast 16936e790746SPaolo Bonzini if (n->nouni) { 16946e790746SPaolo Bonzini return 0; 16956e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 16966e790746SPaolo Bonzini return 1; 16976e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 16986e790746SPaolo Bonzini return 1; 16996e790746SPaolo Bonzini } 17006e790746SPaolo Bonzini 17016e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 17026e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17036e790746SPaolo Bonzini return 1; 17046e790746SPaolo Bonzini } 17056e790746SPaolo Bonzini } 17066e790746SPaolo Bonzini } 17076e790746SPaolo Bonzini 17086e790746SPaolo Bonzini return 0; 17096e790746SPaolo Bonzini } 17106e790746SPaolo Bonzini 17114474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4, 17124474e37aSYuri Benditovich bool isip6, 17134474e37aSYuri Benditovich bool isudp, 17144474e37aSYuri Benditovich bool istcp, 17154474e37aSYuri Benditovich uint32_t types) 17164474e37aSYuri Benditovich { 17174474e37aSYuri Benditovich if (isip4) { 17184474e37aSYuri Benditovich if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) { 17194474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 17204474e37aSYuri Benditovich } 17214474e37aSYuri Benditovich if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) { 17224474e37aSYuri Benditovich return NetPktRssIpV4Udp; 17234474e37aSYuri Benditovich } 17244474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 17254474e37aSYuri Benditovich return NetPktRssIpV4; 17264474e37aSYuri Benditovich } 17274474e37aSYuri Benditovich } else if (isip6) { 17284474e37aSYuri Benditovich uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 17294474e37aSYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6; 17304474e37aSYuri Benditovich 17314474e37aSYuri Benditovich if (istcp && (types & mask)) { 17324474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ? 17334474e37aSYuri Benditovich NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp; 17344474e37aSYuri Benditovich } 17354474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6; 17364474e37aSYuri Benditovich if (isudp && (types & mask)) { 17374474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ? 17384474e37aSYuri Benditovich NetPktRssIpV6UdpEx : NetPktRssIpV6Udp; 17394474e37aSYuri Benditovich } 17404474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6; 17414474e37aSYuri Benditovich if (types & mask) { 17424474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ? 17434474e37aSYuri Benditovich NetPktRssIpV6Ex : NetPktRssIpV6; 17444474e37aSYuri Benditovich } 17454474e37aSYuri Benditovich } 17464474e37aSYuri Benditovich return 0xff; 17474474e37aSYuri Benditovich } 17484474e37aSYuri Benditovich 1749e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1750e22f0603SYuri Benditovich uint32_t hash) 1751e22f0603SYuri Benditovich { 1752e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1753e22f0603SYuri Benditovich hdr->hash_value = hash; 1754e22f0603SYuri Benditovich hdr->hash_report = report; 1755e22f0603SYuri Benditovich } 1756e22f0603SYuri Benditovich 17574474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 175897cd965cSPaolo Bonzini size_t size) 17596e790746SPaolo Bonzini { 17606e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1761e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 17624474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 17634474e37aSYuri Benditovich uint8_t net_hash_type; 17644474e37aSYuri Benditovich uint32_t hash; 17654474e37aSYuri Benditovich bool isip4, isip6, isudp, istcp; 1766e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1767e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1768e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1769e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1770e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1771e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1772e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1773e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1774e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1775e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1776e22f0603SYuri Benditovich }; 17774474e37aSYuri Benditovich 17784474e37aSYuri Benditovich net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len, 17794474e37aSYuri Benditovich size - n->host_hdr_len); 17804474e37aSYuri Benditovich net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp); 17814474e37aSYuri Benditovich if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) { 17824474e37aSYuri Benditovich istcp = isudp = false; 17834474e37aSYuri Benditovich } 17844474e37aSYuri Benditovich if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) { 17854474e37aSYuri Benditovich istcp = isudp = false; 17864474e37aSYuri Benditovich } 17874474e37aSYuri Benditovich net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp, 17884474e37aSYuri Benditovich n->rss_data.hash_types); 17894474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1790e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1791e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1792e22f0603SYuri Benditovich } 1793e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 17944474e37aSYuri Benditovich } 17954474e37aSYuri Benditovich 17964474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1797e22f0603SYuri Benditovich 1798e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1799e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1800e22f0603SYuri Benditovich } 1801e22f0603SYuri Benditovich 1802e22f0603SYuri Benditovich if (n->rss_data.redirect) { 18034474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 18044474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 18054474e37aSYuri Benditovich } 1806e22f0603SYuri Benditovich 1807e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 18084474e37aSYuri Benditovich } 18094474e37aSYuri Benditovich 18104474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 18114474e37aSYuri Benditovich size_t size, bool no_rss) 18124474e37aSYuri Benditovich { 18134474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 18146e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 181517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1816bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1817bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 18186e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 18196e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 18206e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1821bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1822bedd7e93SJason Wang ssize_t err; 18236e790746SPaolo Bonzini 18246e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 18256e790746SPaolo Bonzini return -1; 18266e790746SPaolo Bonzini } 18276e790746SPaolo Bonzini 18280145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 18294474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 18304474e37aSYuri Benditovich if (index >= 0) { 18314474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 18324474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 18334474e37aSYuri Benditovich } 18344474e37aSYuri Benditovich } 18354474e37aSYuri Benditovich 18366e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 18376e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 18386e790746SPaolo Bonzini return 0; 18396e790746SPaolo Bonzini } 18406e790746SPaolo Bonzini 18416e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 18426e790746SPaolo Bonzini return size; 18436e790746SPaolo Bonzini 18446e790746SPaolo Bonzini offset = i = 0; 18456e790746SPaolo Bonzini 18466e790746SPaolo Bonzini while (offset < size) { 184751b19ebeSPaolo Bonzini VirtQueueElement *elem; 18486e790746SPaolo Bonzini int len, total; 184951b19ebeSPaolo Bonzini const struct iovec *sg; 18506e790746SPaolo Bonzini 18516e790746SPaolo Bonzini total = 0; 18526e790746SPaolo Bonzini 1853bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1854bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1855bedd7e93SJason Wang err = size; 1856bedd7e93SJason Wang goto err; 1857bedd7e93SJason Wang } 1858bedd7e93SJason Wang 185951b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 186051b19ebeSPaolo Bonzini if (!elem) { 1861ba10b9c0SGreg Kurz if (i) { 1862ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 18636e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1864019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1865019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 18666e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1867019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1868019a3edbSGerd Hoffmann vdev->guest_features); 1869ba10b9c0SGreg Kurz } 1870bedd7e93SJason Wang err = -1; 1871bedd7e93SJason Wang goto err; 18726e790746SPaolo Bonzini } 18736e790746SPaolo Bonzini 187451b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1875ba10b9c0SGreg Kurz virtio_error(vdev, 1876ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1877ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1878ba10b9c0SGreg Kurz g_free(elem); 1879bedd7e93SJason Wang err = -1; 1880bedd7e93SJason Wang goto err; 18816e790746SPaolo Bonzini } 18826e790746SPaolo Bonzini 188351b19ebeSPaolo Bonzini sg = elem->in_sg; 18846e790746SPaolo Bonzini if (i == 0) { 18856e790746SPaolo Bonzini assert(offset == 0); 18866e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 18876e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 188851b19ebeSPaolo Bonzini sg, elem->in_num, 18896e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 18906e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 18916e790746SPaolo Bonzini } 18926e790746SPaolo Bonzini 189351b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1894e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1895e22f0603SYuri Benditovich offset = sizeof(mhdr); 1896e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1897e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 1898e22f0603SYuri Benditovich } 18996e790746SPaolo Bonzini offset = n->host_hdr_len; 19006e790746SPaolo Bonzini total += n->guest_hdr_len; 19016e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 19026e790746SPaolo Bonzini } else { 19036e790746SPaolo Bonzini guest_offset = 0; 19046e790746SPaolo Bonzini } 19056e790746SPaolo Bonzini 19066e790746SPaolo Bonzini /* copy in packet. ugh */ 190751b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 19086e790746SPaolo Bonzini buf + offset, size - offset); 19096e790746SPaolo Bonzini total += len; 19106e790746SPaolo Bonzini offset += len; 19116e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 19126e790746SPaolo Bonzini * must have consumed the complete packet. 19136e790746SPaolo Bonzini * Otherwise, drop it. */ 19146e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 191527e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 191651b19ebeSPaolo Bonzini g_free(elem); 1917bedd7e93SJason Wang err = size; 1918bedd7e93SJason Wang goto err; 19196e790746SPaolo Bonzini } 19206e790746SPaolo Bonzini 1921bedd7e93SJason Wang elems[i] = elem; 1922bedd7e93SJason Wang lens[i] = total; 1923bedd7e93SJason Wang i++; 19246e790746SPaolo Bonzini } 19256e790746SPaolo Bonzini 19266e790746SPaolo Bonzini if (mhdr_cnt) { 19271399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 19286e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 19296e790746SPaolo Bonzini 0, 19306e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 19316e790746SPaolo Bonzini } 19326e790746SPaolo Bonzini 1933bedd7e93SJason Wang for (j = 0; j < i; j++) { 1934bedd7e93SJason Wang /* signal other side */ 1935bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 1936bedd7e93SJason Wang g_free(elems[j]); 1937bedd7e93SJason Wang } 1938bedd7e93SJason Wang 19396e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 194017a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 19416e790746SPaolo Bonzini 19426e790746SPaolo Bonzini return size; 1943bedd7e93SJason Wang 1944bedd7e93SJason Wang err: 1945bedd7e93SJason Wang for (j = 0; j < i; j++) { 1946abe300d9SJason Wang virtqueue_detach_element(q->rx_vq, elems[j], lens[j]); 1947bedd7e93SJason Wang g_free(elems[j]); 1948bedd7e93SJason Wang } 1949bedd7e93SJason Wang 1950bedd7e93SJason Wang return err; 19516e790746SPaolo Bonzini } 19526e790746SPaolo Bonzini 19532974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 195497cd965cSPaolo Bonzini size_t size) 195597cd965cSPaolo Bonzini { 1956068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 195797cd965cSPaolo Bonzini 19584474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 195997cd965cSPaolo Bonzini } 196097cd965cSPaolo Bonzini 19612974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 19622974e916SYuri Benditovich const uint8_t *buf, 19632974e916SYuri Benditovich VirtioNetRscUnit *unit) 19642974e916SYuri Benditovich { 19652974e916SYuri Benditovich uint16_t ip_hdrlen; 19662974e916SYuri Benditovich struct ip_header *ip; 19672974e916SYuri Benditovich 19682974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 19692974e916SYuri Benditovich + sizeof(struct eth_header)); 19702974e916SYuri Benditovich unit->ip = (void *)ip; 19712974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 19722974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 19732974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 19742974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19752974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 19762974e916SYuri Benditovich } 19772974e916SYuri Benditovich 19782974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 19792974e916SYuri Benditovich const uint8_t *buf, 19802974e916SYuri Benditovich VirtioNetRscUnit *unit) 19812974e916SYuri Benditovich { 19822974e916SYuri Benditovich struct ip6_header *ip6; 19832974e916SYuri Benditovich 19842974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 19852974e916SYuri Benditovich + sizeof(struct eth_header)); 19862974e916SYuri Benditovich unit->ip = ip6; 19872974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 198878ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 19892974e916SYuri Benditovich + sizeof(struct ip6_header)); 19902974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19912974e916SYuri Benditovich 19922974e916SYuri Benditovich /* There is a difference between payload lenght in ipv4 and v6, 19932974e916SYuri Benditovich ip header is excluded in ipv6 */ 19942974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 19952974e916SYuri Benditovich } 19962974e916SYuri Benditovich 19972974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 19982974e916SYuri Benditovich VirtioNetRscSeg *seg) 19992974e916SYuri Benditovich { 20002974e916SYuri Benditovich int ret; 2001dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 20022974e916SYuri Benditovich 2003dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 20042974e916SYuri Benditovich h->flags = 0; 20052974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 20062974e916SYuri Benditovich 20072974e916SYuri Benditovich if (seg->is_coalesced) { 2008dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 2009dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 20102974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 20112974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 20122974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 20132974e916SYuri Benditovich } else { 20142974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 20152974e916SYuri Benditovich } 20162974e916SYuri Benditovich } 20172974e916SYuri Benditovich 20182974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 20192974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 20202974e916SYuri Benditovich g_free(seg->buf); 20212974e916SYuri Benditovich g_free(seg); 20222974e916SYuri Benditovich 20232974e916SYuri Benditovich return ret; 20242974e916SYuri Benditovich } 20252974e916SYuri Benditovich 20262974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 20272974e916SYuri Benditovich { 20282974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 20292974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 20302974e916SYuri Benditovich 20312974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 20322974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 20332974e916SYuri Benditovich chain->stat.purge_failed++; 20342974e916SYuri Benditovich continue; 20352974e916SYuri Benditovich } 20362974e916SYuri Benditovich } 20372974e916SYuri Benditovich 20382974e916SYuri Benditovich chain->stat.timer++; 20392974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 20402974e916SYuri Benditovich timer_mod(chain->drain_timer, 20412974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 20422974e916SYuri Benditovich } 20432974e916SYuri Benditovich } 20442974e916SYuri Benditovich 20452974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 20462974e916SYuri Benditovich { 20472974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 20482974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 20492974e916SYuri Benditovich 20502974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 20512974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 20522974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 20532974e916SYuri Benditovich g_free(seg->buf); 20542974e916SYuri Benditovich g_free(seg); 20552974e916SYuri Benditovich } 20562974e916SYuri Benditovich 20572974e916SYuri Benditovich timer_free(chain->drain_timer); 20582974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 20592974e916SYuri Benditovich g_free(chain); 20602974e916SYuri Benditovich } 20612974e916SYuri Benditovich } 20622974e916SYuri Benditovich 20632974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 20642974e916SYuri Benditovich NetClientState *nc, 20652974e916SYuri Benditovich const uint8_t *buf, size_t size) 20662974e916SYuri Benditovich { 20672974e916SYuri Benditovich uint16_t hdr_len; 20682974e916SYuri Benditovich VirtioNetRscSeg *seg; 20692974e916SYuri Benditovich 20702974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 2071b21e2380SMarkus Armbruster seg = g_new(VirtioNetRscSeg, 1); 20722974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 20732974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 20742974e916SYuri Benditovich memcpy(seg->buf, buf, size); 20752974e916SYuri Benditovich seg->size = size; 20762974e916SYuri Benditovich seg->packets = 1; 20772974e916SYuri Benditovich seg->dup_ack = 0; 20782974e916SYuri Benditovich seg->is_coalesced = 0; 20792974e916SYuri Benditovich seg->nc = nc; 20802974e916SYuri Benditovich 20812974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 20822974e916SYuri Benditovich chain->stat.cache++; 20832974e916SYuri Benditovich 20842974e916SYuri Benditovich switch (chain->proto) { 20852974e916SYuri Benditovich case ETH_P_IP: 20862974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 20872974e916SYuri Benditovich break; 20882974e916SYuri Benditovich case ETH_P_IPV6: 20892974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 20902974e916SYuri Benditovich break; 20912974e916SYuri Benditovich default: 20922974e916SYuri Benditovich g_assert_not_reached(); 20932974e916SYuri Benditovich } 20942974e916SYuri Benditovich } 20952974e916SYuri Benditovich 20962974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 20972974e916SYuri Benditovich VirtioNetRscSeg *seg, 20982974e916SYuri Benditovich const uint8_t *buf, 20992974e916SYuri Benditovich struct tcp_header *n_tcp, 21002974e916SYuri Benditovich struct tcp_header *o_tcp) 21012974e916SYuri Benditovich { 21022974e916SYuri Benditovich uint32_t nack, oack; 21032974e916SYuri Benditovich uint16_t nwin, owin; 21042974e916SYuri Benditovich 21052974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 21062974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 21072974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 21082974e916SYuri Benditovich owin = htons(o_tcp->th_win); 21092974e916SYuri Benditovich 21102974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 21112974e916SYuri Benditovich chain->stat.ack_out_of_win++; 21122974e916SYuri Benditovich return RSC_FINAL; 21132974e916SYuri Benditovich } else if (nack == oack) { 21142974e916SYuri Benditovich /* duplicated ack or window probe */ 21152974e916SYuri Benditovich if (nwin == owin) { 21162974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 21172974e916SYuri Benditovich chain->stat.dup_ack++; 21182974e916SYuri Benditovich return RSC_FINAL; 21192974e916SYuri Benditovich } else { 21202974e916SYuri Benditovich /* Coalesce window update */ 21212974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 21222974e916SYuri Benditovich chain->stat.win_update++; 21232974e916SYuri Benditovich return RSC_COALESCE; 21242974e916SYuri Benditovich } 21252974e916SYuri Benditovich } else { 21262974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 21272974e916SYuri Benditovich chain->stat.pure_ack++; 21282974e916SYuri Benditovich return RSC_FINAL; 21292974e916SYuri Benditovich } 21302974e916SYuri Benditovich } 21312974e916SYuri Benditovich 21322974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 21332974e916SYuri Benditovich VirtioNetRscSeg *seg, 21342974e916SYuri Benditovich const uint8_t *buf, 21352974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 21362974e916SYuri Benditovich { 21372974e916SYuri Benditovich void *data; 21382974e916SYuri Benditovich uint16_t o_ip_len; 21392974e916SYuri Benditovich uint32_t nseq, oseq; 21402974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 21412974e916SYuri Benditovich 21422974e916SYuri Benditovich o_unit = &seg->unit; 21432974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 21442974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 21452974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 21462974e916SYuri Benditovich 21472974e916SYuri Benditovich /* out of order or retransmitted. */ 21482974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 21492974e916SYuri Benditovich chain->stat.data_out_of_win++; 21502974e916SYuri Benditovich return RSC_FINAL; 21512974e916SYuri Benditovich } 21522974e916SYuri Benditovich 21532974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 21542974e916SYuri Benditovich if (nseq == oseq) { 21552974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 21562974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 21572974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 21582974e916SYuri Benditovich goto coalesce; 21592974e916SYuri Benditovich } else { 21602974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 21612974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 21622974e916SYuri Benditovich } 21632974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 21642974e916SYuri Benditovich /* Not a consistent packet, out of order */ 21652974e916SYuri Benditovich chain->stat.data_out_of_order++; 21662974e916SYuri Benditovich return RSC_FINAL; 21672974e916SYuri Benditovich } else { 21682974e916SYuri Benditovich coalesce: 21692974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 21702974e916SYuri Benditovich chain->stat.over_size++; 21712974e916SYuri Benditovich return RSC_FINAL; 21722974e916SYuri Benditovich } 21732974e916SYuri Benditovich 21742974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 21752974e916SYuri Benditovich so use the field value to update and record the new data len */ 21762974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 21772974e916SYuri Benditovich 21782974e916SYuri Benditovich /* update field in ip header */ 21792974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 21802974e916SYuri Benditovich 21812974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 21822974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 21832974e916SYuri Benditovich guest (only if it uses RSC feature). */ 21842974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 21852974e916SYuri Benditovich 21862974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 21872974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 21882974e916SYuri Benditovich 21892974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 21902974e916SYuri Benditovich seg->size += n_unit->payload; 21912974e916SYuri Benditovich seg->packets++; 21922974e916SYuri Benditovich chain->stat.coalesced++; 21932974e916SYuri Benditovich return RSC_COALESCE; 21942974e916SYuri Benditovich } 21952974e916SYuri Benditovich } 21962974e916SYuri Benditovich 21972974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 21982974e916SYuri Benditovich VirtioNetRscSeg *seg, 21992974e916SYuri Benditovich const uint8_t *buf, size_t size, 22002974e916SYuri Benditovich VirtioNetRscUnit *unit) 22012974e916SYuri Benditovich { 22022974e916SYuri Benditovich struct ip_header *ip1, *ip2; 22032974e916SYuri Benditovich 22042974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 22052974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 22062974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 22072974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 22082974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 22092974e916SYuri Benditovich chain->stat.no_match++; 22102974e916SYuri Benditovich return RSC_NO_MATCH; 22112974e916SYuri Benditovich } 22122974e916SYuri Benditovich 22132974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 22142974e916SYuri Benditovich } 22152974e916SYuri Benditovich 22162974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 22172974e916SYuri Benditovich VirtioNetRscSeg *seg, 22182974e916SYuri Benditovich const uint8_t *buf, size_t size, 22192974e916SYuri Benditovich VirtioNetRscUnit *unit) 22202974e916SYuri Benditovich { 22212974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 22222974e916SYuri Benditovich 22232974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 22242974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 22252974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 22262974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 22272974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 22282974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 22292974e916SYuri Benditovich chain->stat.no_match++; 22302974e916SYuri Benditovich return RSC_NO_MATCH; 22312974e916SYuri Benditovich } 22322974e916SYuri Benditovich 22332974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 22342974e916SYuri Benditovich } 22352974e916SYuri Benditovich 22362974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 22372974e916SYuri Benditovich * to prevent out of order */ 22382974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 22392974e916SYuri Benditovich struct tcp_header *tcp) 22402974e916SYuri Benditovich { 22412974e916SYuri Benditovich uint16_t tcp_hdr; 22422974e916SYuri Benditovich uint16_t tcp_flag; 22432974e916SYuri Benditovich 22442974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 22452974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 22462974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 22472974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 22482974e916SYuri Benditovich chain->stat.tcp_syn++; 22492974e916SYuri Benditovich return RSC_BYPASS; 22502974e916SYuri Benditovich } 22512974e916SYuri Benditovich 22522974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 22532974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 22542974e916SYuri Benditovich return RSC_FINAL; 22552974e916SYuri Benditovich } 22562974e916SYuri Benditovich 22572974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 22582974e916SYuri Benditovich chain->stat.tcp_all_opt++; 22592974e916SYuri Benditovich return RSC_FINAL; 22602974e916SYuri Benditovich } 22612974e916SYuri Benditovich 22622974e916SYuri Benditovich return RSC_CANDIDATE; 22632974e916SYuri Benditovich } 22642974e916SYuri Benditovich 22652974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 22662974e916SYuri Benditovich NetClientState *nc, 22672974e916SYuri Benditovich const uint8_t *buf, size_t size, 22682974e916SYuri Benditovich VirtioNetRscUnit *unit) 22692974e916SYuri Benditovich { 22702974e916SYuri Benditovich int ret; 22712974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 22722974e916SYuri Benditovich 22732974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 22742974e916SYuri Benditovich chain->stat.empty_cache++; 22752974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 22762974e916SYuri Benditovich timer_mod(chain->drain_timer, 22772974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 22782974e916SYuri Benditovich return size; 22792974e916SYuri Benditovich } 22802974e916SYuri Benditovich 22812974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22822974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 22832974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 22842974e916SYuri Benditovich } else { 22852974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 22862974e916SYuri Benditovich } 22872974e916SYuri Benditovich 22882974e916SYuri Benditovich if (ret == RSC_FINAL) { 22892974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 22902974e916SYuri Benditovich /* Send failed */ 22912974e916SYuri Benditovich chain->stat.final_failed++; 22922974e916SYuri Benditovich return 0; 22932974e916SYuri Benditovich } 22942974e916SYuri Benditovich 22952974e916SYuri Benditovich /* Send current packet */ 22962974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 22972974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 22982974e916SYuri Benditovich continue; 22992974e916SYuri Benditovich } else { 23002974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 23012974e916SYuri Benditovich seg->is_coalesced = 1; 23022974e916SYuri Benditovich return size; 23032974e916SYuri Benditovich } 23042974e916SYuri Benditovich } 23052974e916SYuri Benditovich 23062974e916SYuri Benditovich chain->stat.no_match_cache++; 23072974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 23082974e916SYuri Benditovich return size; 23092974e916SYuri Benditovich } 23102974e916SYuri Benditovich 23112974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 23122974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 23132974e916SYuri Benditovich NetClientState *nc, 23142974e916SYuri Benditovich const uint8_t *buf, size_t size, 23152974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 23162974e916SYuri Benditovich uint16_t tcp_port) 23172974e916SYuri Benditovich { 23182974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 23192974e916SYuri Benditovich uint32_t ppair1, ppair2; 23202974e916SYuri Benditovich 23212974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 23222974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 23232974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 23242974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 23252974e916SYuri Benditovich || (ppair1 != ppair2)) { 23262974e916SYuri Benditovich continue; 23272974e916SYuri Benditovich } 23282974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 23292974e916SYuri Benditovich chain->stat.drain_failed++; 23302974e916SYuri Benditovich } 23312974e916SYuri Benditovich 23322974e916SYuri Benditovich break; 23332974e916SYuri Benditovich } 23342974e916SYuri Benditovich 23352974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23362974e916SYuri Benditovich } 23372974e916SYuri Benditovich 23382974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 23392974e916SYuri Benditovich struct ip_header *ip, 23402974e916SYuri Benditovich const uint8_t *buf, size_t size) 23412974e916SYuri Benditovich { 23422974e916SYuri Benditovich uint16_t ip_len; 23432974e916SYuri Benditovich 23442974e916SYuri Benditovich /* Not an ipv4 packet */ 23452974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 23462974e916SYuri Benditovich chain->stat.ip_option++; 23472974e916SYuri Benditovich return RSC_BYPASS; 23482974e916SYuri Benditovich } 23492974e916SYuri Benditovich 23502974e916SYuri Benditovich /* Don't handle packets with ip option */ 23512974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 23522974e916SYuri Benditovich chain->stat.ip_option++; 23532974e916SYuri Benditovich return RSC_BYPASS; 23542974e916SYuri Benditovich } 23552974e916SYuri Benditovich 23562974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 23572974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23582974e916SYuri Benditovich return RSC_BYPASS; 23592974e916SYuri Benditovich } 23602974e916SYuri Benditovich 23612974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 23622974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 23632974e916SYuri Benditovich chain->stat.ip_frag++; 23642974e916SYuri Benditovich return RSC_BYPASS; 23652974e916SYuri Benditovich } 23662974e916SYuri Benditovich 23672974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 23682974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 23692974e916SYuri Benditovich chain->stat.ip_ecn++; 23702974e916SYuri Benditovich return RSC_BYPASS; 23712974e916SYuri Benditovich } 23722974e916SYuri Benditovich 23732974e916SYuri Benditovich ip_len = htons(ip->ip_len); 23742974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 23752974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 23762974e916SYuri Benditovich sizeof(struct eth_header))) { 23772974e916SYuri Benditovich chain->stat.ip_hacked++; 23782974e916SYuri Benditovich return RSC_BYPASS; 23792974e916SYuri Benditovich } 23802974e916SYuri Benditovich 23812974e916SYuri Benditovich return RSC_CANDIDATE; 23822974e916SYuri Benditovich } 23832974e916SYuri Benditovich 23842974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 23852974e916SYuri Benditovich NetClientState *nc, 23862974e916SYuri Benditovich const uint8_t *buf, size_t size) 23872974e916SYuri Benditovich { 23882974e916SYuri Benditovich int32_t ret; 23892974e916SYuri Benditovich uint16_t hdr_len; 23902974e916SYuri Benditovich VirtioNetRscUnit unit; 23912974e916SYuri Benditovich 23922974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 23932974e916SYuri Benditovich 23942974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 23952974e916SYuri Benditovich + sizeof(struct tcp_header))) { 23962974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23972974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23982974e916SYuri Benditovich } 23992974e916SYuri Benditovich 24002974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 24012974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 24022974e916SYuri Benditovich != RSC_CANDIDATE) { 24032974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24042974e916SYuri Benditovich } 24052974e916SYuri Benditovich 24062974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24072974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24082974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24092974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24102974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24112974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 24122974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 24132974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 24142974e916SYuri Benditovich } 24152974e916SYuri Benditovich 24162974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 24172974e916SYuri Benditovich } 24182974e916SYuri Benditovich 24192974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 24202974e916SYuri Benditovich struct ip6_header *ip6, 24212974e916SYuri Benditovich const uint8_t *buf, size_t size) 24222974e916SYuri Benditovich { 24232974e916SYuri Benditovich uint16_t ip_len; 24242974e916SYuri Benditovich 24252974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 24262974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 24272974e916SYuri Benditovich return RSC_BYPASS; 24282974e916SYuri Benditovich } 24292974e916SYuri Benditovich 24302974e916SYuri Benditovich /* Both option and protocol is checked in this */ 24312974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 24322974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24332974e916SYuri Benditovich return RSC_BYPASS; 24342974e916SYuri Benditovich } 24352974e916SYuri Benditovich 24362974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 24372974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 24382974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 24392974e916SYuri Benditovich - sizeof(struct ip6_header))) { 24402974e916SYuri Benditovich chain->stat.ip_hacked++; 24412974e916SYuri Benditovich return RSC_BYPASS; 24422974e916SYuri Benditovich } 24432974e916SYuri Benditovich 24442974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 24452974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 24462974e916SYuri Benditovich chain->stat.ip_ecn++; 24472974e916SYuri Benditovich return RSC_BYPASS; 24482974e916SYuri Benditovich } 24492974e916SYuri Benditovich 24502974e916SYuri Benditovich return RSC_CANDIDATE; 24512974e916SYuri Benditovich } 24522974e916SYuri Benditovich 24532974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 24542974e916SYuri Benditovich const uint8_t *buf, size_t size) 24552974e916SYuri Benditovich { 24562974e916SYuri Benditovich int32_t ret; 24572974e916SYuri Benditovich uint16_t hdr_len; 24582974e916SYuri Benditovich VirtioNetRscChain *chain; 24592974e916SYuri Benditovich VirtioNetRscUnit unit; 24602974e916SYuri Benditovich 24612974e916SYuri Benditovich chain = (VirtioNetRscChain *)opq; 24622974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 24632974e916SYuri Benditovich 24642974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 24652974e916SYuri Benditovich + sizeof(tcp_header))) { 24662974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24672974e916SYuri Benditovich } 24682974e916SYuri Benditovich 24692974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 24702974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 24712974e916SYuri Benditovich unit.ip, buf, size)) { 24722974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24732974e916SYuri Benditovich } 24742974e916SYuri Benditovich 24752974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24762974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24772974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24782974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24792974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24802974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 24812974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 24822974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 24832974e916SYuri Benditovich + sizeof(struct ip6_header)); 24842974e916SYuri Benditovich } 24852974e916SYuri Benditovich 24862974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 24872974e916SYuri Benditovich } 24882974e916SYuri Benditovich 24892974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 24902974e916SYuri Benditovich NetClientState *nc, 24912974e916SYuri Benditovich uint16_t proto) 24922974e916SYuri Benditovich { 24932974e916SYuri Benditovich VirtioNetRscChain *chain; 24942974e916SYuri Benditovich 24952974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 24962974e916SYuri Benditovich return NULL; 24972974e916SYuri Benditovich } 24982974e916SYuri Benditovich 24992974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 25002974e916SYuri Benditovich if (chain->proto == proto) { 25012974e916SYuri Benditovich return chain; 25022974e916SYuri Benditovich } 25032974e916SYuri Benditovich } 25042974e916SYuri Benditovich 25052974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 25062974e916SYuri Benditovich chain->n = n; 25072974e916SYuri Benditovich chain->proto = proto; 25082974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 25092974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 25102974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 25112974e916SYuri Benditovich } else { 25122974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 25132974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 25142974e916SYuri Benditovich } 25152974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 25162974e916SYuri Benditovich virtio_net_rsc_purge, chain); 25172974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 25182974e916SYuri Benditovich 25192974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 25202974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 25212974e916SYuri Benditovich 25222974e916SYuri Benditovich return chain; 25232974e916SYuri Benditovich } 25242974e916SYuri Benditovich 25252974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 25262974e916SYuri Benditovich const uint8_t *buf, 25272974e916SYuri Benditovich size_t size) 25282974e916SYuri Benditovich { 25292974e916SYuri Benditovich uint16_t proto; 25302974e916SYuri Benditovich VirtioNetRscChain *chain; 25312974e916SYuri Benditovich struct eth_header *eth; 25322974e916SYuri Benditovich VirtIONet *n; 25332974e916SYuri Benditovich 25342974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 25352974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 25362974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25372974e916SYuri Benditovich } 25382974e916SYuri Benditovich 25392974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 25402974e916SYuri Benditovich proto = htons(eth->h_proto); 25412974e916SYuri Benditovich 25422974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 25432974e916SYuri Benditovich if (chain) { 25442974e916SYuri Benditovich chain->stat.received++; 25452974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 25462974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 25472974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 25482974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 25492974e916SYuri Benditovich } 25502974e916SYuri Benditovich } 25512974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25522974e916SYuri Benditovich } 25532974e916SYuri Benditovich 25542974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 25552974e916SYuri Benditovich size_t size) 25562974e916SYuri Benditovich { 25572974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 25582974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 25592974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 25602974e916SYuri Benditovich } else { 25612974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25622974e916SYuri Benditovich } 25632974e916SYuri Benditovich } 25642974e916SYuri Benditovich 25656e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 25666e790746SPaolo Bonzini 25676e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 25686e790746SPaolo Bonzini { 25696e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 25706e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 257117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2572df8d0708SLaurent Vivier int ret; 25736e790746SPaolo Bonzini 257451b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 257517a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 25766e790746SPaolo Bonzini 257751b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 257851b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 25796e790746SPaolo Bonzini 25806e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2581df8d0708SLaurent Vivier ret = virtio_net_flush_tx(q); 25827550a822SLaurent Vivier if (ret >= n->tx_burst) { 2583df8d0708SLaurent Vivier /* 2584df8d0708SLaurent Vivier * the flush has been stopped by tx_burst 2585df8d0708SLaurent Vivier * we will not receive notification for the 2586df8d0708SLaurent Vivier * remainining part, so re-schedule 2587df8d0708SLaurent Vivier */ 2588df8d0708SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 25897550a822SLaurent Vivier if (q->tx_bh) { 2590df8d0708SLaurent Vivier qemu_bh_schedule(q->tx_bh); 25917550a822SLaurent Vivier } else { 25927550a822SLaurent Vivier timer_mod(q->tx_timer, 25937550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 25947550a822SLaurent Vivier } 2595df8d0708SLaurent Vivier q->tx_waiting = 1; 2596df8d0708SLaurent Vivier } 25976e790746SPaolo Bonzini } 25986e790746SPaolo Bonzini 25996e790746SPaolo Bonzini /* TX */ 26006e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 26016e790746SPaolo Bonzini { 26026e790746SPaolo Bonzini VirtIONet *n = q->n; 260317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 260451b19ebeSPaolo Bonzini VirtQueueElement *elem; 26056e790746SPaolo Bonzini int32_t num_packets = 0; 26066e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 260717a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 26086e790746SPaolo Bonzini return num_packets; 26096e790746SPaolo Bonzini } 26106e790746SPaolo Bonzini 261151b19ebeSPaolo Bonzini if (q->async_tx.elem) { 26126e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26136e790746SPaolo Bonzini return num_packets; 26146e790746SPaolo Bonzini } 26156e790746SPaolo Bonzini 261651b19ebeSPaolo Bonzini for (;;) { 2617bd89dd98SJason Wang ssize_t ret; 261851b19ebeSPaolo Bonzini unsigned int out_num; 261951b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2620feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 26216e790746SPaolo Bonzini 262251b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 262351b19ebeSPaolo Bonzini if (!elem) { 262451b19ebeSPaolo Bonzini break; 262551b19ebeSPaolo Bonzini } 262651b19ebeSPaolo Bonzini 262751b19ebeSPaolo Bonzini out_num = elem->out_num; 262851b19ebeSPaolo Bonzini out_sg = elem->out_sg; 26296e790746SPaolo Bonzini if (out_num < 1) { 2630fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2631fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2632fa5e56c2SGreg Kurz g_free(elem); 2633fa5e56c2SGreg Kurz return -EINVAL; 26346e790746SPaolo Bonzini } 26356e790746SPaolo Bonzini 2636032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 2637feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 2638feb93f36SJason Wang n->guest_hdr_len) { 2639fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2640fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2641fa5e56c2SGreg Kurz g_free(elem); 2642fa5e56c2SGreg Kurz return -EINVAL; 2643032a74a1SCédric Le Goater } 26441bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2645feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 2646feb93f36SJason Wang sg2[0].iov_base = &mhdr; 2647feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2648feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2649feb93f36SJason Wang out_sg, out_num, 2650feb93f36SJason Wang n->guest_hdr_len, -1); 2651feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2652feb93f36SJason Wang goto drop; 2653032a74a1SCédric Le Goater } 2654feb93f36SJason Wang out_num += 1; 2655feb93f36SJason Wang out_sg = sg2; 2656feb93f36SJason Wang } 2657feb93f36SJason Wang } 26586e790746SPaolo Bonzini /* 26596e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 26606e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 26616e790746SPaolo Bonzini * that host is interested in. 26626e790746SPaolo Bonzini */ 26636e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 26646e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 26656e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 26666e790746SPaolo Bonzini out_sg, out_num, 26676e790746SPaolo Bonzini 0, n->host_hdr_len); 26686e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 26696e790746SPaolo Bonzini out_sg, out_num, 26706e790746SPaolo Bonzini n->guest_hdr_len, -1); 26716e790746SPaolo Bonzini out_num = sg_num; 26726e790746SPaolo Bonzini out_sg = sg; 26736e790746SPaolo Bonzini } 26746e790746SPaolo Bonzini 26756e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 26766e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 26776e790746SPaolo Bonzini if (ret == 0) { 26786e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26796e790746SPaolo Bonzini q->async_tx.elem = elem; 26806e790746SPaolo Bonzini return -EBUSY; 26816e790746SPaolo Bonzini } 26826e790746SPaolo Bonzini 2683feb93f36SJason Wang drop: 268451b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 268517a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 268651b19ebeSPaolo Bonzini g_free(elem); 26876e790746SPaolo Bonzini 26886e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 26896e790746SPaolo Bonzini break; 26906e790746SPaolo Bonzini } 26916e790746SPaolo Bonzini } 26926e790746SPaolo Bonzini return num_packets; 26936e790746SPaolo Bonzini } 26946e790746SPaolo Bonzini 26957550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque); 26967550a822SLaurent Vivier 26976e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 26986e790746SPaolo Bonzini { 269917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 27006e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 27016e790746SPaolo Bonzini 2702283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2703283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2704283e2c2aSYuri Benditovich return; 2705283e2c2aSYuri Benditovich } 2706283e2c2aSYuri Benditovich 27076e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 270817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 27096e790746SPaolo Bonzini q->tx_waiting = 1; 27106e790746SPaolo Bonzini return; 27116e790746SPaolo Bonzini } 27126e790746SPaolo Bonzini 27136e790746SPaolo Bonzini if (q->tx_waiting) { 27147550a822SLaurent Vivier /* We already have queued packets, immediately flush */ 2715bc72ad67SAlex Bligh timer_del(q->tx_timer); 27167550a822SLaurent Vivier virtio_net_tx_timer(q); 27176e790746SPaolo Bonzini } else { 27187550a822SLaurent Vivier /* re-arm timer to flush it (and more) on next tick */ 2719bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2720bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27216e790746SPaolo Bonzini q->tx_waiting = 1; 27226e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 27236e790746SPaolo Bonzini } 27246e790746SPaolo Bonzini } 27256e790746SPaolo Bonzini 27266e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 27276e790746SPaolo Bonzini { 272817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 27296e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 27306e790746SPaolo Bonzini 2731283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2732283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2733283e2c2aSYuri Benditovich return; 2734283e2c2aSYuri Benditovich } 2735283e2c2aSYuri Benditovich 27366e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 27376e790746SPaolo Bonzini return; 27386e790746SPaolo Bonzini } 27396e790746SPaolo Bonzini q->tx_waiting = 1; 27406e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 274117a0ca55SKONRAD Frederic if (!vdev->vm_running) { 27426e790746SPaolo Bonzini return; 27436e790746SPaolo Bonzini } 27446e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 27456e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27466e790746SPaolo Bonzini } 27476e790746SPaolo Bonzini 27486e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 27496e790746SPaolo Bonzini { 27506e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 27516e790746SPaolo Bonzini VirtIONet *n = q->n; 275217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 27537550a822SLaurent Vivier int ret; 27547550a822SLaurent Vivier 2755e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2756e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2757e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2758e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2759e8bcf842SMichael S. Tsirkin return; 2760e8bcf842SMichael S. Tsirkin } 27616e790746SPaolo Bonzini 27626e790746SPaolo Bonzini q->tx_waiting = 0; 27636e790746SPaolo Bonzini 27646e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 276517a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 27666e790746SPaolo Bonzini return; 276717a0ca55SKONRAD Frederic } 27686e790746SPaolo Bonzini 27697550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 27707550a822SLaurent Vivier if (ret == -EBUSY || ret == -EINVAL) { 27717550a822SLaurent Vivier return; 27727550a822SLaurent Vivier } 27737550a822SLaurent Vivier /* 27747550a822SLaurent Vivier * If we flush a full burst of packets, assume there are 27757550a822SLaurent Vivier * more coming and immediately rearm 27767550a822SLaurent Vivier */ 27777550a822SLaurent Vivier if (ret >= n->tx_burst) { 27787550a822SLaurent Vivier q->tx_waiting = 1; 27797550a822SLaurent Vivier timer_mod(q->tx_timer, 27807550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27817550a822SLaurent Vivier return; 27827550a822SLaurent Vivier } 27837550a822SLaurent Vivier /* 27847550a822SLaurent Vivier * If less than a full burst, re-enable notification and flush 27857550a822SLaurent Vivier * anything that may have come in while we weren't looking. If 27867550a822SLaurent Vivier * we find something, assume the guest is still active and rearm 27877550a822SLaurent Vivier */ 27886e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 27897550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 27907550a822SLaurent Vivier if (ret > 0) { 27917550a822SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 27927550a822SLaurent Vivier q->tx_waiting = 1; 27937550a822SLaurent Vivier timer_mod(q->tx_timer, 27947550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27957550a822SLaurent Vivier } 27966e790746SPaolo Bonzini } 27976e790746SPaolo Bonzini 27986e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 27996e790746SPaolo Bonzini { 28006e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 28016e790746SPaolo Bonzini VirtIONet *n = q->n; 280217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 28036e790746SPaolo Bonzini int32_t ret; 28046e790746SPaolo Bonzini 2805e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2806e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2807e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2808e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2809e8bcf842SMichael S. Tsirkin return; 2810e8bcf842SMichael S. Tsirkin } 28116e790746SPaolo Bonzini 28126e790746SPaolo Bonzini q->tx_waiting = 0; 28136e790746SPaolo Bonzini 28146e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 281517a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 28166e790746SPaolo Bonzini return; 281717a0ca55SKONRAD Frederic } 28186e790746SPaolo Bonzini 28196e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2820fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2821fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2822fa5e56c2SGreg Kurz * broken */ 28236e790746SPaolo Bonzini } 28246e790746SPaolo Bonzini 28256e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 28266e790746SPaolo Bonzini * more coming and immediately reschedule */ 28276e790746SPaolo Bonzini if (ret >= n->tx_burst) { 28286e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28296e790746SPaolo Bonzini q->tx_waiting = 1; 28306e790746SPaolo Bonzini return; 28316e790746SPaolo Bonzini } 28326e790746SPaolo Bonzini 28336e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 28346e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 28356e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 28366e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2837fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2838fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2839fa5e56c2SGreg Kurz return; 2840fa5e56c2SGreg Kurz } else if (ret > 0) { 28416e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 28426e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28436e790746SPaolo Bonzini q->tx_waiting = 1; 28446e790746SPaolo Bonzini } 28456e790746SPaolo Bonzini } 28466e790746SPaolo Bonzini 2847f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2848f9d6dbf0SWen Congyang { 2849f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2850f9d6dbf0SWen Congyang 28511c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 28521c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 28539b02e161SWei Wang 2854f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2855f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 28569b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 28579b02e161SWei Wang virtio_net_handle_tx_timer); 2858f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2859f9d6dbf0SWen Congyang virtio_net_tx_timer, 2860f9d6dbf0SWen Congyang &n->vqs[index]); 2861f9d6dbf0SWen Congyang } else { 2862f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 28639b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 28649b02e161SWei Wang virtio_net_handle_tx_bh); 2865f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 2866f9d6dbf0SWen Congyang } 2867f9d6dbf0SWen Congyang 2868f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2869f9d6dbf0SWen Congyang n->vqs[index].n = n; 2870f9d6dbf0SWen Congyang } 2871f9d6dbf0SWen Congyang 2872f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2873f9d6dbf0SWen Congyang { 2874f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2875f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2876f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2877f9d6dbf0SWen Congyang 2878f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2879f9d6dbf0SWen Congyang 2880f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2881f9d6dbf0SWen Congyang if (q->tx_timer) { 2882f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2883f989c30cSYunjian Wang q->tx_timer = NULL; 2884f9d6dbf0SWen Congyang } else { 2885f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2886f989c30cSYunjian Wang q->tx_bh = NULL; 2887f9d6dbf0SWen Congyang } 2888f989c30cSYunjian Wang q->tx_waiting = 0; 2889f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2890f9d6dbf0SWen Congyang } 2891f9d6dbf0SWen Congyang 2892441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 2893f9d6dbf0SWen Congyang { 2894f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2895f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2896441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 2897f9d6dbf0SWen Congyang int i; 2898f9d6dbf0SWen Congyang 2899f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2900f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2901f9d6dbf0SWen Congyang 2902f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 2903f9d6dbf0SWen Congyang return; 2904f9d6dbf0SWen Congyang } 2905f9d6dbf0SWen Congyang 2906f9d6dbf0SWen Congyang /* 2907f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 2908f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 290920f86a75SYuval Shaia * and then we only enter one of the following two loops. 2910f9d6dbf0SWen Congyang */ 2911f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 2912f9d6dbf0SWen Congyang 2913f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 2914f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 2915f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 2916f9d6dbf0SWen Congyang } 2917f9d6dbf0SWen Congyang 2918f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 2919f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 2920f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 2921f9d6dbf0SWen Congyang } 2922f9d6dbf0SWen Congyang 2923f9d6dbf0SWen Congyang /* add ctrl_vq last */ 2924f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 2925f9d6dbf0SWen Congyang } 2926f9d6dbf0SWen Congyang 2927ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 29286e790746SPaolo Bonzini { 2929441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 2930f9d6dbf0SWen Congyang 29316e790746SPaolo Bonzini n->multiqueue = multiqueue; 2932441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 29336e790746SPaolo Bonzini 2934441537f1SJason Wang virtio_net_set_queue_pairs(n); 29356e790746SPaolo Bonzini } 29366e790746SPaolo Bonzini 2937982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 2938037dab2fSGreg Kurz { 2939982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 2940982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 2941037dab2fSGreg Kurz int i, link_down; 2942037dab2fSGreg Kurz 29439d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 2944982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 294595129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 2946e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 2947e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 2948e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 29496e790746SPaolo Bonzini 29506e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 2951982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 29526e790746SPaolo Bonzini n->mac_table.in_use = 0; 29536e790746SPaolo Bonzini } 29546e790746SPaolo Bonzini 2955982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 29566c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 29576c666823SMichael S. Tsirkin } 29586c666823SMichael S. Tsirkin 29597788c3f2SMikhail Sennikovsky /* 29607788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 29617788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 29627788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 29637788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 29647788c3f2SMikhail Sennikovsky */ 29657788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 29666c666823SMichael S. Tsirkin 2967441537f1SJason Wang virtio_net_set_queue_pairs(n); 29686e790746SPaolo Bonzini 29696e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 29706e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 29716e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 29726e790746SPaolo Bonzini break; 29736e790746SPaolo Bonzini } 29746e790746SPaolo Bonzini } 29756e790746SPaolo Bonzini n->mac_table.first_multi = i; 29766e790746SPaolo Bonzini 29776e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 29786e790746SPaolo Bonzini * to link status bit in n->status */ 29796e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 2980441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 29816e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 29826e790746SPaolo Bonzini } 29836e790746SPaolo Bonzini 29846c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 29856c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 29869d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 29879d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 29889d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 29899d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 29909d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 29919d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 29929d8c6a25SDr. David Alan Gilbert } else { 2993944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 29949d8c6a25SDr. David Alan Gilbert } 29956c666823SMichael S. Tsirkin } 29966c666823SMichael S. Tsirkin 2997e41b7114SYuri Benditovich if (n->rss_data.enabled) { 29980145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 29990145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 30000145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 30010145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 30020145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS for vhost"); 30030145c393SAndrew Melnychenko } else { 30040145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS - " 30050145c393SAndrew Melnychenko "fallback to software RSS"); 30060145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 30070145c393SAndrew Melnychenko } 30080145c393SAndrew Melnychenko } 30090145c393SAndrew Melnychenko } 30100145c393SAndrew Melnychenko 3011e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 3012e41b7114SYuri Benditovich n->rss_data.indirections_len, 3013e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 3014e41b7114SYuri Benditovich } else { 3015e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 3016e41b7114SYuri Benditovich } 30176e790746SPaolo Bonzini return 0; 30186e790746SPaolo Bonzini } 30196e790746SPaolo Bonzini 30207788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 30217788c3f2SMikhail Sennikovsky { 30227788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 30237788c3f2SMikhail Sennikovsky /* 30247788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 30257788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 30267788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 30277788c3f2SMikhail Sennikovsky */ 30287788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 30297788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 30307788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 30317788c3f2SMikhail Sennikovsky } 30327788c3f2SMikhail Sennikovsky 30337788c3f2SMikhail Sennikovsky return 0; 30347788c3f2SMikhail Sennikovsky } 30357788c3f2SMikhail Sennikovsky 3036982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 3037982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 3038982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 3039982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3040982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 3041982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3042982b78c5SDr. David Alan Gilbert }, 3043982b78c5SDr. David Alan Gilbert }; 3044982b78c5SDr. David Alan Gilbert 3045441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 3046982b78c5SDr. David Alan Gilbert { 3047441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 3048982b78c5SDr. David Alan Gilbert } 3049982b78c5SDr. David Alan Gilbert 3050982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 3051982b78c5SDr. David Alan Gilbert { 3052982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 3053982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 3054982b78c5SDr. David Alan Gilbert } 3055982b78c5SDr. David Alan Gilbert 3056982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 3057982b78c5SDr. David Alan Gilbert { 3058982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 3059982b78c5SDr. David Alan Gilbert } 3060982b78c5SDr. David Alan Gilbert 3061982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 3062982b78c5SDr. David Alan Gilbert { 3063982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 3064982b78c5SDr. David Alan Gilbert } 3065982b78c5SDr. David Alan Gilbert 3066982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 3067982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 3068982b78c5SDr. David Alan Gilbert */ 3069982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 3070982b78c5SDr. David Alan Gilbert VirtIONet *parent; 3071982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 3072441537f1SJason Wang uint16_t curr_queue_pairs_1; 3073982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 3074982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 3075982b78c5SDr. David Alan Gilbert }; 3076982b78c5SDr. David Alan Gilbert 3077982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 3078441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 3079982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 3080982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 3081982b78c5SDr. David Alan Gilbert */ 3082982b78c5SDr. David Alan Gilbert 308344b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 3084982b78c5SDr. David Alan Gilbert { 3085982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3086982b78c5SDr. David Alan Gilbert 3087982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 3088441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 3089441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 3090441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 3091982b78c5SDr. David Alan Gilbert } 309244b1ff31SDr. David Alan Gilbert 309344b1ff31SDr. David Alan Gilbert return 0; 3094982b78c5SDr. David Alan Gilbert } 3095982b78c5SDr. David Alan Gilbert 3096982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 3097982b78c5SDr. David Alan Gilbert { 3098982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3099982b78c5SDr. David Alan Gilbert 3100982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 3101982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 3102982b78c5SDr. David Alan Gilbert 3103441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 3104441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 3105441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 3106982b78c5SDr. David Alan Gilbert 3107982b78c5SDr. David Alan Gilbert return -EINVAL; 3108982b78c5SDr. David Alan Gilbert } 3109982b78c5SDr. David Alan Gilbert 3110982b78c5SDr. David Alan Gilbert return 0; /* all good */ 3111982b78c5SDr. David Alan Gilbert } 3112982b78c5SDr. David Alan Gilbert 3113982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 3114982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 3115982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 3116982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 3117982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3118982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 3119441537f1SJason Wang curr_queue_pairs_1, 3120982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3121982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3122982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3123982b78c5SDr. David Alan Gilbert }, 3124982b78c5SDr. David Alan Gilbert }; 3125982b78c5SDr. David Alan Gilbert 3126982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3127982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3128982b78c5SDr. David Alan Gilbert */ 3129982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3130982b78c5SDr. David Alan Gilbert { 3131982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3132982b78c5SDr. David Alan Gilbert 3133982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3134982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3135982b78c5SDr. David Alan Gilbert return -EINVAL; 3136982b78c5SDr. David Alan Gilbert } 3137982b78c5SDr. David Alan Gilbert 3138982b78c5SDr. David Alan Gilbert return 0; 3139982b78c5SDr. David Alan Gilbert } 3140982b78c5SDr. David Alan Gilbert 314144b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3142982b78c5SDr. David Alan Gilbert { 3143982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3144982b78c5SDr. David Alan Gilbert 3145982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 314644b1ff31SDr. David Alan Gilbert 314744b1ff31SDr. David Alan Gilbert return 0; 3148982b78c5SDr. David Alan Gilbert } 3149982b78c5SDr. David Alan Gilbert 3150982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3151982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3152982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3153982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 3154982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3155982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3156982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3157982b78c5SDr. David Alan Gilbert }, 3158982b78c5SDr. David Alan Gilbert }; 3159982b78c5SDr. David Alan Gilbert 3160982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3161982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3162982b78c5SDr. David Alan Gilbert */ 3163982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3164982b78c5SDr. David Alan Gilbert { 3165982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3166982b78c5SDr. David Alan Gilbert 3167982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3168982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3169982b78c5SDr. David Alan Gilbert return -EINVAL; 3170982b78c5SDr. David Alan Gilbert } 3171982b78c5SDr. David Alan Gilbert 3172982b78c5SDr. David Alan Gilbert return 0; 3173982b78c5SDr. David Alan Gilbert } 3174982b78c5SDr. David Alan Gilbert 317544b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3176982b78c5SDr. David Alan Gilbert { 3177982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3178982b78c5SDr. David Alan Gilbert 3179982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 318044b1ff31SDr. David Alan Gilbert 318144b1ff31SDr. David Alan Gilbert return 0; 3182982b78c5SDr. David Alan Gilbert } 3183982b78c5SDr. David Alan Gilbert 3184982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3185982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3186982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3187982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 3188982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3189982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3190982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3191982b78c5SDr. David Alan Gilbert }, 3192982b78c5SDr. David Alan Gilbert }; 3193982b78c5SDr. David Alan Gilbert 3194e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3195e41b7114SYuri Benditovich { 3196e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3197e41b7114SYuri Benditovich } 3198e41b7114SYuri Benditovich 3199e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3200e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3201e41b7114SYuri Benditovich .version_id = 1, 3202e41b7114SYuri Benditovich .minimum_version_id = 1, 3203e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 3204e41b7114SYuri Benditovich .fields = (VMStateField[]) { 3205e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3206e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3207e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3208e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3209e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3210e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3211e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3212e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3213e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3214e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3215e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3216e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3217e41b7114SYuri Benditovich }, 3218e41b7114SYuri Benditovich }; 3219e41b7114SYuri Benditovich 3220982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3221982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3222982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3223982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3224982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 3225982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3226982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3227982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3228982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3229982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3230982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3231982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3232982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3233982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3234982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3235982b78c5SDr. David Alan Gilbert 3236982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3237982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3238982b78c5SDr. David Alan Gilbert * sets flags in this case. 3239982b78c5SDr. David Alan Gilbert */ 3240982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3241982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3242982b78c5SDr. David Alan Gilbert ETH_ALEN), 3243982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3244982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3245982b78c5SDr. David Alan Gilbert 3246982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3247982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3248982b78c5SDr. David Alan Gilbert * but based on the uint. 3249982b78c5SDr. David Alan Gilbert */ 3250982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3251982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3252982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3253982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3254982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3255982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3256982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3257982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3258982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3259982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3260982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3261441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3262982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3263441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3264982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3265982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3266982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3267982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3268982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3269982b78c5SDr. David Alan Gilbert }, 3270e41b7114SYuri Benditovich .subsections = (const VMStateDescription * []) { 3271e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3272e41b7114SYuri Benditovich NULL 3273e41b7114SYuri Benditovich } 3274982b78c5SDr. David Alan Gilbert }; 3275982b78c5SDr. David Alan Gilbert 32766e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3277f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 32786e790746SPaolo Bonzini .size = sizeof(NICState), 32796e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 32806e790746SPaolo Bonzini .receive = virtio_net_receive, 32816e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3282b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3283b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 32846e790746SPaolo Bonzini }; 32856e790746SPaolo Bonzini 32866e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 32876e790746SPaolo Bonzini { 328817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 328968b0a639SSi-Wei Liu NetClientState *nc; 32906e790746SPaolo Bonzini assert(n->vhost_started); 329168b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 329268b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 329368b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 329468b0a639SSi-Wei Liu * buggy migration stream. 329568b0a639SSi-Wei Liu */ 329668b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 329768b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 329868b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 329968b0a639SSi-Wei Liu return false; 330068b0a639SSi-Wei Liu } 330168b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 330268b0a639SSi-Wei Liu } else { 330368b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 330468b0a639SSi-Wei Liu } 3305ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 33066e790746SPaolo Bonzini } 33076e790746SPaolo Bonzini 33086e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 33096e790746SPaolo Bonzini bool mask) 33106e790746SPaolo Bonzini { 331117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 331268b0a639SSi-Wei Liu NetClientState *nc; 33136e790746SPaolo Bonzini assert(n->vhost_started); 331468b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 331568b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 331668b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 331768b0a639SSi-Wei Liu * buggy migration stream. 331868b0a639SSi-Wei Liu */ 331968b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 332068b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 332168b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 332268b0a639SSi-Wei Liu return; 332368b0a639SSi-Wei Liu } 332468b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 332568b0a639SSi-Wei Liu } else { 332668b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 332768b0a639SSi-Wei Liu } 3328a882b571SMichael S. Tsirkin vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 3329a882b571SMichael S. Tsirkin vdev, idx, mask); 33306e790746SPaolo Bonzini } 33316e790746SPaolo Bonzini 3332019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 33336e790746SPaolo Bonzini { 33340cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3335a93e599dSMaxime Coquelin 3336d74c30c8SDaniil Tatianin n->config_size = virtio_get_config_size(&cfg_size_params, host_features); 333717ec5a86SKONRAD Frederic } 33386e790746SPaolo Bonzini 33398a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 33408a253ec2SKONRAD Frederic const char *type) 33418a253ec2SKONRAD Frederic { 33428a253ec2SKONRAD Frederic /* 33438a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 33448a253ec2SKONRAD Frederic */ 33458a253ec2SKONRAD Frederic assert(type != NULL); 33468a253ec2SKONRAD Frederic 33478a253ec2SKONRAD Frederic g_free(n->netclient_name); 33488a253ec2SKONRAD Frederic g_free(n->netclient_type); 33498a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 33508a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 33518a253ec2SKONRAD Frederic } 33528a253ec2SKONRAD Frederic 33530e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 33549711cd0dSJens Freimann { 33559711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 33569711cd0dSJens Freimann PCIDevice *pci_dev; 33579711cd0dSJens Freimann Error *err = NULL; 33589711cd0dSJens Freimann 33590e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 33609711cd0dSJens Freimann if (hotplug_ctrl) { 33610e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 33629711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 33630e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 33649711cd0dSJens Freimann if (err) { 33659711cd0dSJens Freimann error_report_err(err); 33669711cd0dSJens Freimann return false; 33679711cd0dSJens Freimann } 33689711cd0dSJens Freimann } else { 33699711cd0dSJens Freimann return false; 33709711cd0dSJens Freimann } 33719711cd0dSJens Freimann return true; 33729711cd0dSJens Freimann } 33739711cd0dSJens Freimann 33740e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 33750e9a65c5SJuan Quintela Error **errp) 33769711cd0dSJens Freimann { 33775a0948d3SMarkus Armbruster Error *err = NULL; 33789711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 33790e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 338078274682SJuan Quintela BusState *primary_bus; 33819711cd0dSJens Freimann 33829711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 33839711cd0dSJens Freimann return true; 33849711cd0dSJens Freimann } 33850e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 338678274682SJuan Quintela if (!primary_bus) { 3387150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 33885a0948d3SMarkus Armbruster return false; 33899711cd0dSJens Freimann } 33900e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3391e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 33920e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 33939711cd0dSJens Freimann if (hotplug_ctrl) { 33940e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 33955a0948d3SMarkus Armbruster if (err) { 33965a0948d3SMarkus Armbruster goto out; 33975a0948d3SMarkus Armbruster } 33980e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 33999711cd0dSJens Freimann } 3400109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3401150ab54aSJens Freimann 3402150ab54aSJens Freimann out: 34035a0948d3SMarkus Armbruster error_propagate(errp, err); 34045a0948d3SMarkus Armbruster return !err; 34059711cd0dSJens Freimann } 34069711cd0dSJens Freimann 340707a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s) 34089711cd0dSJens Freimann { 34099711cd0dSJens Freimann bool should_be_hidden; 34109711cd0dSJens Freimann Error *err = NULL; 341107a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 341207a5d816SJuan Quintela 341307a5d816SJuan Quintela if (!dev) { 341407a5d816SJuan Quintela return; 341507a5d816SJuan Quintela } 34169711cd0dSJens Freimann 3417e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 34189711cd0dSJens Freimann 34194dbac1aeSMarkus Armbruster if (migration_in_setup(s) && !should_be_hidden) { 342007a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 342107a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 342207a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3423e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 34249711cd0dSJens Freimann } else { 34259711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 34269711cd0dSJens Freimann } 34279711cd0dSJens Freimann } else if (migration_has_failed(s)) { 3428150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 342907a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 34309711cd0dSJens Freimann if (err) { 34319711cd0dSJens Freimann error_report_err(err); 34329711cd0dSJens Freimann } 34339711cd0dSJens Freimann } 34349711cd0dSJens Freimann } 34359711cd0dSJens Freimann } 34369711cd0dSJens Freimann 34379711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) 34389711cd0dSJens Freimann { 34399711cd0dSJens Freimann MigrationState *s = data; 34409711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 34419711cd0dSJens Freimann virtio_net_handle_migration_primary(n, s); 34429711cd0dSJens Freimann } 34439711cd0dSJens Freimann 3444b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3445f3558b1bSKevin Wolf const QDict *device_opts, 3446f3558b1bSKevin Wolf bool from_json, 3447f3558b1bSKevin Wolf Error **errp) 34489711cd0dSJens Freimann { 34499711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 34504f0303aeSJuan Quintela const char *standby_id; 34519711cd0dSJens Freimann 34524d0e59acSJens Freimann if (!device_opts) { 345389631fedSJuan Quintela return false; 34544d0e59acSJens Freimann } 3455bcfc906bSLaurent Vivier 3456bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3457bcfc906bSLaurent Vivier return false; 3458bcfc906bSLaurent Vivier } 3459bcfc906bSLaurent Vivier 3460bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3461bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3462bcfc906bSLaurent Vivier return false; 3463bcfc906bSLaurent Vivier } 3464bcfc906bSLaurent Vivier 3465bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 346689631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 346789631fedSJuan Quintela return false; 34689711cd0dSJens Freimann } 34699711cd0dSJens Freimann 34707fe7791eSLaurent Vivier /* 34717fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 34727fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 34737fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 34747fe7791eSLaurent Vivier * device. 34757fe7791eSLaurent Vivier */ 3476259a10dbSKevin Wolf if (n->primary_opts) { 34777fe7791eSLaurent Vivier const char *old, *new; 34787fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 34797fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 34807fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 34817fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 34827fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 34837fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3484259a10dbSKevin Wolf return false; 3485259a10dbSKevin Wolf } 34867fe7791eSLaurent Vivier } else { 3487f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3488f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 34897fe7791eSLaurent Vivier } 3490259a10dbSKevin Wolf 3491e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 34923abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 34939711cd0dSJens Freimann } 34949711cd0dSJens Freimann 3495e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 349617ec5a86SKONRAD Frederic { 3497e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3498284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3499284a32f0SAndreas Färber NetClientState *nc; 35001773d9eeSKONRAD Frederic int i; 350117ec5a86SKONRAD Frederic 3502a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3503127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3504a93e599dSMaxime Coquelin } 3505a93e599dSMaxime Coquelin 35069473939eSJason Baron if (n->net_conf.duplex_str) { 35079473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 35089473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 35099473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 35109473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 35119473939eSJason Baron } else { 35129473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3513843c4cfcSMarkus Armbruster return; 35149473939eSJason Baron } 35159473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 35169473939eSJason Baron } else { 35179473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 35189473939eSJason Baron } 35199473939eSJason Baron 35209473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 35219473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3522843c4cfcSMarkus Armbruster return; 3523843c4cfcSMarkus Armbruster } 3524843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 35259473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 35269473939eSJason Baron } 35279473939eSJason Baron 35289711cd0dSJens Freimann if (n->failover) { 3529b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3530e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 35319711cd0dSJens Freimann device_listener_register(&n->primary_listener); 35329711cd0dSJens Freimann n->migration_state.notify = virtio_net_migration_state_notifier; 35339711cd0dSJens Freimann add_migration_state_change_notifier(&n->migration_state); 35349711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 35359711cd0dSJens Freimann } 35369711cd0dSJens Freimann 3537da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 35383857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_NET, n->config_size); 353917ec5a86SKONRAD Frederic 35401c0fbfa3SMichael S. Tsirkin /* 35411c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 35421c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 35431c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 35441c0fbfa3SMichael S. Tsirkin */ 35451c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 35461c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 35475f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 35481c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 35491c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 35501c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 35511c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 35521c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 35531c0fbfa3SMichael S. Tsirkin return; 35541c0fbfa3SMichael S. Tsirkin } 35551c0fbfa3SMichael S. Tsirkin 35569b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 35579b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 35589b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 35599b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 35609b02e161SWei Wang "must be a power of 2 between %d and %d", 35619b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 35629b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 35639b02e161SWei Wang virtio_cleanup(vdev); 35649b02e161SWei Wang return; 35659b02e161SWei Wang } 35669b02e161SWei Wang 356722288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 356822288fe5SJason Wang 356922288fe5SJason Wang /* 357022288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 357122288fe5SJason Wang * provide control queue via peers as well. 357222288fe5SJason Wang */ 357322288fe5SJason Wang if (n->nic_conf.peers.queues) { 357422288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 357522288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 357622288fe5SJason Wang ++n->max_queue_pairs; 357722288fe5SJason Wang } 357822288fe5SJason Wang } 357922288fe5SJason Wang } 358022288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 358122288fe5SJason Wang 3582441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 358322288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3584631b22eaSStefan Weil "must be a positive integer less than %d.", 3585441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 35867e0e736eSJason Wang virtio_cleanup(vdev); 35877e0e736eSJason Wang return; 35887e0e736eSJason Wang } 3589b21e2380SMarkus Armbruster n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs); 3590441537f1SJason Wang n->curr_queue_pairs = 1; 35911773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 35926e790746SPaolo Bonzini 35931773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 35941773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 35950765691eSMarkus Armbruster warn_report("virtio-net: " 35966e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 35971773d9eeSKONRAD Frederic n->net_conf.tx); 35980765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 35996e790746SPaolo Bonzini } 36006e790746SPaolo Bonzini 36012eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 36022eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 36039b02e161SWei Wang 3604441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3605f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3606da51a335SJason Wang } 3607da51a335SJason Wang 360817a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 36091773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 36101773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 36116e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 36129d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 36139d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3614f57fcf70SJason Wang virtio_net_announce_timer, n); 3615b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 36166e790746SPaolo Bonzini 36178a253ec2SKONRAD Frederic if (n->netclient_type) { 36188a253ec2SKONRAD Frederic /* 36198a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 36208a253ec2SKONRAD Frederic */ 36218a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 36228a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 36238a253ec2SKONRAD Frederic } else { 36241773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 3625284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 36268a253ec2SKONRAD Frederic } 36278a253ec2SKONRAD Frederic 3628441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3629d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3630d4c62930SBin Meng } 3631d4c62930SBin Meng 36326e790746SPaolo Bonzini peer_test_vnet_hdr(n); 36336e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 3634441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3635d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 36366e790746SPaolo Bonzini } 36376e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 36386e790746SPaolo Bonzini } else { 36396e790746SPaolo Bonzini n->host_hdr_len = 0; 36406e790746SPaolo Bonzini } 36416e790746SPaolo Bonzini 36421773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 36436e790746SPaolo Bonzini 36446e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 36451773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3646e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 36476e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 36486e790746SPaolo Bonzini 36496e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 36506e790746SPaolo Bonzini 36516e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 36526e790746SPaolo Bonzini 3653b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3654b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3655b1be4280SAmos Kong 3656e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3657e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3658e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3659e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3660e87936eaSCindy Lu (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER); 3661e87936eaSCindy Lu } 36622974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3663284a32f0SAndreas Färber n->qdev = dev; 36644474e37aSYuri Benditovich 36654474e37aSYuri Benditovich net_rx_pkt_init(&n->rx_pkt, false); 36660145c393SAndrew Melnychenko 36670145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 36680145c393SAndrew Melnychenko virtio_net_load_ebpf(n); 36690145c393SAndrew Melnychenko } 367017ec5a86SKONRAD Frederic } 367117ec5a86SKONRAD Frederic 3672b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 367317ec5a86SKONRAD Frederic { 3674306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3675306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3676441537f1SJason Wang int i, max_queue_pairs; 367717ec5a86SKONRAD Frederic 36780145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 36790145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 36800145c393SAndrew Melnychenko } 36810145c393SAndrew Melnychenko 368217ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 368317ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 368417ec5a86SKONRAD Frederic 36858a253ec2SKONRAD Frederic g_free(n->netclient_name); 36868a253ec2SKONRAD Frederic n->netclient_name = NULL; 36878a253ec2SKONRAD Frederic g_free(n->netclient_type); 36888a253ec2SKONRAD Frederic n->netclient_type = NULL; 36898a253ec2SKONRAD Frederic 369017ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 369117ec5a86SKONRAD Frederic g_free(n->vlans); 369217ec5a86SKONRAD Frederic 36939711cd0dSJens Freimann if (n->failover) { 3694f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 369565018100SJuan Quintela device_listener_unregister(&n->primary_listener); 36961e157667SLaurent Vivier remove_migration_state_change_notifier(&n->migration_state); 3697f3558b1bSKevin Wolf } else { 3698f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 36999711cd0dSJens Freimann } 37009711cd0dSJens Freimann 3701441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3702441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3703f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 370417ec5a86SKONRAD Frederic } 3705d945d9f1SYuri Benditovich /* delete also control vq */ 3706441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3707944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 370817ec5a86SKONRAD Frederic g_free(n->vqs); 370917ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 37102974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 371159079029SYuri Benditovich g_free(n->rss_data.indirections_table); 37124474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 37136a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 371417ec5a86SKONRAD Frederic } 371517ec5a86SKONRAD Frederic 371617ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 371717ec5a86SKONRAD Frederic { 371817ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 371917ec5a86SKONRAD Frederic 372017ec5a86SKONRAD Frederic /* 372117ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 372217ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 372317ec5a86SKONRAD Frederic */ 372417ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3725aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3726aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 372740c2281cSMarkus Armbruster DEVICE(n)); 37280145c393SAndrew Melnychenko 37290145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 373017ec5a86SKONRAD Frederic } 373117ec5a86SKONRAD Frederic 373244b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 37334d45dcfbSHalil Pasic { 37344d45dcfbSHalil Pasic VirtIONet *n = opaque; 37354d45dcfbSHalil Pasic 37364d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 37374d45dcfbSHalil Pasic * it might keep writing to memory. */ 37384d45dcfbSHalil Pasic assert(!n->vhost_started); 373944b1ff31SDr. David Alan Gilbert 374044b1ff31SDr. David Alan Gilbert return 0; 37414d45dcfbSHalil Pasic } 37424d45dcfbSHalil Pasic 37439711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 37449711cd0dSJens Freimann { 37459711cd0dSJens Freimann DeviceState *dev = opaque; 374621e8709bSJuan Quintela DeviceState *primary; 37479711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 37489711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 37499711cd0dSJens Freimann 3750284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3751284f42a5SJens Freimann return false; 3752284f42a5SJens Freimann } 375321e8709bSJuan Quintela primary = failover_find_primary_device(n); 375421e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 37559711cd0dSJens Freimann } 37569711cd0dSJens Freimann 37579711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 37589711cd0dSJens Freimann { 37599711cd0dSJens Freimann DeviceState *dev = opaque; 37609711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 37619711cd0dSJens Freimann 37629711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 37639711cd0dSJens Freimann } 37649711cd0dSJens Freimann 3765c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) 3766c255488dSJonah Palmer { 3767c255488dSJonah Palmer VirtIONet *n = VIRTIO_NET(vdev); 3768c255488dSJonah Palmer NetClientState *nc = qemu_get_queue(n->nic); 3769c255488dSJonah Palmer struct vhost_net *net = get_vhost_net(nc->peer); 3770c255488dSJonah Palmer return &net->dev; 3771c255488dSJonah Palmer } 3772c255488dSJonah Palmer 37734d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 37744d45dcfbSHalil Pasic .name = "virtio-net", 37754d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 37764d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 37774d45dcfbSHalil Pasic .fields = (VMStateField[]) { 37784d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 37794d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 37804d45dcfbSHalil Pasic }, 37814d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 37829711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 37834d45dcfbSHalil Pasic }; 3784290c2428SDr. David Alan Gilbert 378517ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3786127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3787127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3788127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 378987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3790127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3791127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 379287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3793127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 379487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3795127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 379687108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3797127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 379887108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3799127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 380087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3801127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 380287108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3803127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 380487108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3805127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 380687108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3807127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 380887108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3809127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 381087108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3811127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 381287108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3813127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 381487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3815127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 381687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3817127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 381887108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3819127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 382087108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3821127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 382287108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3823127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 382487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3825127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 382659079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 382759079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3828e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3829e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 38302974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 38312974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 38322974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 38332974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 383417ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 383517ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 383617ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 383717ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 383817ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 38391c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 38401c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 38419b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 38429b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3843a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 384475ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 384575ebec11SMaxime Coquelin true), 38469473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 38479473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 38489711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 384917ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 385017ec5a86SKONRAD Frederic }; 385117ec5a86SKONRAD Frederic 385217ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 385317ec5a86SKONRAD Frederic { 385417ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 385517ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3856e6f746b3SAndreas Färber 38574f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3858290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3859125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3860e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3861306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 386217ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 386317ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 386417ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 386517ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 386617ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 386717ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 38687dc6be52SXuan Zhuo vdc->queue_reset = virtio_net_queue_reset; 38697f863302SKangjie Xu vdc->queue_enable = virtio_net_queue_enable; 387017ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 387117ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 387217ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 38732a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 38747788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 3875982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 38769711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 3877c255488dSJonah Palmer vdc->get_vhost = virtio_net_get_vhost; 387817ec5a86SKONRAD Frederic } 387917ec5a86SKONRAD Frederic 388017ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 388117ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 388217ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 388317ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 388417ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 388517ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 388617ec5a86SKONRAD Frederic }; 388717ec5a86SKONRAD Frederic 388817ec5a86SKONRAD Frederic static void virtio_register_types(void) 388917ec5a86SKONRAD Frederic { 389017ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 389117ec5a86SKONRAD Frederic } 389217ec5a86SKONRAD Frederic 389317ec5a86SKONRAD Frederic type_init(virtio_register_types) 3894