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)); 460*7480874aSMarkus Armbruster qapi_event_send_nic_rx_filter_changed(n->netclient_name, path); 46196e35046SAmos Kong g_free(path); 462b1be4280SAmos Kong 463b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 464b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 465b1be4280SAmos Kong } 466b1be4280SAmos Kong } 467b1be4280SAmos Kong 468f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 469f7bc8ef8SAmos Kong { 47054aa3de7SEric Blake intList *list; 471f7bc8ef8SAmos Kong int i, j; 472f7bc8ef8SAmos Kong 473f7bc8ef8SAmos Kong list = NULL; 474f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 475f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 476f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 47754aa3de7SEric Blake QAPI_LIST_PREPEND(list, (i << 5) + j); 478f7bc8ef8SAmos Kong } 479f7bc8ef8SAmos Kong } 480f7bc8ef8SAmos Kong } 481f7bc8ef8SAmos Kong 482f7bc8ef8SAmos Kong return list; 483f7bc8ef8SAmos Kong } 484f7bc8ef8SAmos Kong 485b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 486b1be4280SAmos Kong { 487b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 488f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 489b1be4280SAmos Kong RxFilterInfo *info; 49054aa3de7SEric Blake strList *str_list; 491f7bc8ef8SAmos Kong int i; 492b1be4280SAmos Kong 493b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 494b1be4280SAmos Kong info->name = g_strdup(nc->name); 495b1be4280SAmos Kong info->promiscuous = n->promisc; 496b1be4280SAmos Kong 497b1be4280SAmos Kong if (n->nouni) { 498b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 499b1be4280SAmos Kong } else if (n->alluni) { 500b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 501b1be4280SAmos Kong } else { 502b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 503b1be4280SAmos Kong } 504b1be4280SAmos Kong 505b1be4280SAmos Kong if (n->nomulti) { 506b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 507b1be4280SAmos Kong } else if (n->allmulti) { 508b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 509b1be4280SAmos Kong } else { 510b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 511b1be4280SAmos Kong } 512b1be4280SAmos Kong 513b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 514b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 515b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 516b1be4280SAmos Kong 517b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 518b1be4280SAmos Kong 519b1be4280SAmos Kong str_list = NULL; 520b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 52154aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 52254aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 523b1be4280SAmos Kong } 524b1be4280SAmos Kong info->unicast_table = str_list; 525b1be4280SAmos Kong 526b1be4280SAmos Kong str_list = NULL; 527b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 52854aa3de7SEric Blake QAPI_LIST_PREPEND(str_list, 52954aa3de7SEric Blake qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN)); 530b1be4280SAmos Kong } 531b1be4280SAmos Kong info->multicast_table = str_list; 532f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 533b1be4280SAmos Kong 53495129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 535f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 536f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 537f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 538f7bc8ef8SAmos Kong } else { 539f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 540b1be4280SAmos Kong } 541b1be4280SAmos Kong 542b1be4280SAmos Kong /* enable event notification after query */ 543b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 544b1be4280SAmos Kong 545b1be4280SAmos Kong return info; 546b1be4280SAmos Kong } 547b1be4280SAmos Kong 5487dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index) 5497dc6be52SXuan Zhuo { 5507dc6be52SXuan Zhuo VirtIONet *n = VIRTIO_NET(vdev); 551f47af0afSXuan Zhuo NetClientState *nc; 552f47af0afSXuan Zhuo 553f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 554f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 555f47af0afSXuan Zhuo return; 556f47af0afSXuan Zhuo } 557f47af0afSXuan Zhuo 558f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 5597dc6be52SXuan Zhuo 5607dc6be52SXuan Zhuo if (!nc->peer) { 5617dc6be52SXuan Zhuo return; 5627dc6be52SXuan Zhuo } 5637dc6be52SXuan Zhuo 5647dc6be52SXuan Zhuo if (get_vhost_net(nc->peer) && 5657dc6be52SXuan Zhuo nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5667dc6be52SXuan Zhuo vhost_net_virtqueue_reset(vdev, nc, queue_index); 5677dc6be52SXuan Zhuo } 5687dc6be52SXuan Zhuo 5697dc6be52SXuan Zhuo flush_or_purge_queued_packets(nc); 5707dc6be52SXuan Zhuo } 5717dc6be52SXuan Zhuo 5727f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index) 5737f863302SKangjie Xu { 5747f863302SKangjie Xu VirtIONet *n = VIRTIO_NET(vdev); 575f47af0afSXuan Zhuo NetClientState *nc; 5767f863302SKangjie Xu int r; 5777f863302SKangjie Xu 578f47af0afSXuan Zhuo /* validate queue_index and skip for cvq */ 579f47af0afSXuan Zhuo if (queue_index >= n->max_queue_pairs * 2) { 580f47af0afSXuan Zhuo return; 581f47af0afSXuan Zhuo } 582f47af0afSXuan Zhuo 583f47af0afSXuan Zhuo nc = qemu_get_subqueue(n->nic, vq2q(queue_index)); 584f47af0afSXuan Zhuo 5857f863302SKangjie Xu if (!nc->peer || !vdev->vhost_started) { 5867f863302SKangjie Xu return; 5877f863302SKangjie Xu } 5887f863302SKangjie Xu 5897f863302SKangjie Xu if (get_vhost_net(nc->peer) && 5907f863302SKangjie Xu nc->peer->info->type == NET_CLIENT_DRIVER_TAP) { 5917f863302SKangjie Xu r = vhost_net_virtqueue_restart(vdev, nc, queue_index); 5927f863302SKangjie Xu if (r < 0) { 5937f863302SKangjie Xu error_report("unable to restart vhost net virtqueue: %d, " 5947f863302SKangjie Xu "when resetting the queue", queue_index); 5957f863302SKangjie Xu } 5967f863302SKangjie Xu } 5977f863302SKangjie Xu } 5987f863302SKangjie Xu 5996e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 6006e790746SPaolo Bonzini { 60117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 60294b52958SGreg Kurz int i; 6036e790746SPaolo Bonzini 6046e790746SPaolo Bonzini /* Reset back to compatibility mode */ 6056e790746SPaolo Bonzini n->promisc = 1; 6066e790746SPaolo Bonzini n->allmulti = 0; 6076e790746SPaolo Bonzini n->alluni = 0; 6086e790746SPaolo Bonzini n->nomulti = 0; 6096e790746SPaolo Bonzini n->nouni = 0; 6106e790746SPaolo Bonzini n->nobcast = 0; 6116e790746SPaolo Bonzini /* multiqueue is disabled by default */ 612441537f1SJason Wang n->curr_queue_pairs = 1; 6139d8c6a25SDr. David Alan Gilbert timer_del(n->announce_timer.tm); 6149d8c6a25SDr. David Alan Gilbert n->announce_timer.round = 0; 615f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 6166e790746SPaolo Bonzini 6176e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 6186e790746SPaolo Bonzini n->mac_table.in_use = 0; 6196e790746SPaolo Bonzini n->mac_table.first_multi = 0; 6206e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 6216e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 6226e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 6236e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 624702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 6256e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 62694b52958SGreg Kurz 62794b52958SGreg Kurz /* Flush any async TX */ 628441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6294fdf69abSKangjie Xu flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i)); 63094b52958SGreg Kurz } 6316e790746SPaolo Bonzini } 6326e790746SPaolo Bonzini 6336e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 6346e790746SPaolo Bonzini { 6356e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 6366e790746SPaolo Bonzini if (!nc->peer) { 6376e790746SPaolo Bonzini return; 6386e790746SPaolo Bonzini } 6396e790746SPaolo Bonzini 640d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 6416e790746SPaolo Bonzini } 6426e790746SPaolo Bonzini 6436e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 6446e790746SPaolo Bonzini { 6456e790746SPaolo Bonzini return n->has_vnet_hdr; 6466e790746SPaolo Bonzini } 6476e790746SPaolo Bonzini 6486e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 6496e790746SPaolo Bonzini { 6506e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 6516e790746SPaolo Bonzini return 0; 6526e790746SPaolo Bonzini 653d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 6546e790746SPaolo Bonzini 6556e790746SPaolo Bonzini return n->has_ufo; 6566e790746SPaolo Bonzini } 6576e790746SPaolo Bonzini 658bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 659e22f0603SYuri Benditovich int version_1, int hash_report) 6606e790746SPaolo Bonzini { 6616e790746SPaolo Bonzini int i; 6626e790746SPaolo Bonzini NetClientState *nc; 6636e790746SPaolo Bonzini 6646e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 6656e790746SPaolo Bonzini 666bb9d17f8SCornelia Huck if (version_1) { 667e22f0603SYuri Benditovich n->guest_hdr_len = hash_report ? 668e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_v1_hash) : 669e22f0603SYuri Benditovich sizeof(struct virtio_net_hdr_mrg_rxbuf); 670e22f0603SYuri Benditovich n->rss_data.populate_hash = !!hash_report; 671bb9d17f8SCornelia Huck } else { 6726e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 673bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 674bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 675bb9d17f8SCornelia Huck } 6766e790746SPaolo Bonzini 677441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 6786e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 6796e790746SPaolo Bonzini 6806e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 681d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 682d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 6836e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 6846e790746SPaolo Bonzini } 6856e790746SPaolo Bonzini } 6866e790746SPaolo Bonzini } 6876e790746SPaolo Bonzini 6882eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 6892eef278bSMichael S. Tsirkin { 6902eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 6912eef278bSMichael S. Tsirkin 6922eef278bSMichael S. Tsirkin /* 6930ea5778fSEugenio Pérez * Backends other than vhost-user or vhost-vdpa don't support max queue 6940ea5778fSEugenio Pérez * size. 6952eef278bSMichael S. Tsirkin */ 6962eef278bSMichael S. Tsirkin if (!peer) { 6972eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 6982eef278bSMichael S. Tsirkin } 6992eef278bSMichael S. Tsirkin 7000ea5778fSEugenio Pérez switch(peer->info->type) { 7010ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_USER: 7020ea5778fSEugenio Pérez case NET_CLIENT_DRIVER_VHOST_VDPA: 7032eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 7040ea5778fSEugenio Pérez default: 7050ea5778fSEugenio Pérez return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 7060ea5778fSEugenio Pérez }; 7072eef278bSMichael S. Tsirkin } 7082eef278bSMichael S. Tsirkin 7096e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 7106e790746SPaolo Bonzini { 7116e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7126e790746SPaolo Bonzini 7136e790746SPaolo Bonzini if (!nc->peer) { 7146e790746SPaolo Bonzini return 0; 7156e790746SPaolo Bonzini } 7166e790746SPaolo Bonzini 717f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7187263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 7197263a0adSChangchun Ouyang } 7207263a0adSChangchun Ouyang 721f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7226e790746SPaolo Bonzini return 0; 7236e790746SPaolo Bonzini } 7246e790746SPaolo Bonzini 725441537f1SJason Wang if (n->max_queue_pairs == 1) { 7261074b879SJason Wang return 0; 7271074b879SJason Wang } 7281074b879SJason Wang 7296e790746SPaolo Bonzini return tap_enable(nc->peer); 7306e790746SPaolo Bonzini } 7316e790746SPaolo Bonzini 7326e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 7336e790746SPaolo Bonzini { 7346e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 7356e790746SPaolo Bonzini 7366e790746SPaolo Bonzini if (!nc->peer) { 7376e790746SPaolo Bonzini return 0; 7386e790746SPaolo Bonzini } 7396e790746SPaolo Bonzini 740f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 7417263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 7427263a0adSChangchun Ouyang } 7437263a0adSChangchun Ouyang 744f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 7456e790746SPaolo Bonzini return 0; 7466e790746SPaolo Bonzini } 7476e790746SPaolo Bonzini 7486e790746SPaolo Bonzini return tap_disable(nc->peer); 7496e790746SPaolo Bonzini } 7506e790746SPaolo Bonzini 751441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n) 7526e790746SPaolo Bonzini { 7536e790746SPaolo Bonzini int i; 754ddfa83eaSJoel Stanley int r; 7556e790746SPaolo Bonzini 75668b5f314SYuri Benditovich if (n->nic->peer_deleted) { 75768b5f314SYuri Benditovich return; 75868b5f314SYuri Benditovich } 75968b5f314SYuri Benditovich 760441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 761441537f1SJason Wang if (i < n->curr_queue_pairs) { 762ddfa83eaSJoel Stanley r = peer_attach(n, i); 763ddfa83eaSJoel Stanley assert(!r); 7646e790746SPaolo Bonzini } else { 765ddfa83eaSJoel Stanley r = peer_detach(n, i); 766ddfa83eaSJoel Stanley assert(!r); 7676e790746SPaolo Bonzini } 7686e790746SPaolo Bonzini } 7696e790746SPaolo Bonzini } 7706e790746SPaolo Bonzini 771ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 7726e790746SPaolo Bonzini 7739d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 7749d5b731dSJason Wang Error **errp) 7756e790746SPaolo Bonzini { 77617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 7776e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 7786e790746SPaolo Bonzini 779da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 780da3e8a23SShannon Zhao features |= n->host_features; 781da3e8a23SShannon Zhao 7820cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 7836e790746SPaolo Bonzini 7846e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 7850cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 7860cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 7870cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 7880cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 7896e790746SPaolo Bonzini 7900cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 7910cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 7920cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 7930cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 794e22f0603SYuri Benditovich 795e22f0603SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT); 7966e790746SPaolo Bonzini } 7976e790746SPaolo Bonzini 7986e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 7990cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 8000cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 8016e790746SPaolo Bonzini } 8026e790746SPaolo Bonzini 803ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 80493a97dc5SXuan Zhuo virtio_add_feature(&features, VIRTIO_F_RING_RESET); 8056e790746SPaolo Bonzini return features; 8066e790746SPaolo Bonzini } 8072974e916SYuri Benditovich 8080145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 80959079029SYuri Benditovich virtio_clear_feature(&features, VIRTIO_NET_F_RSS); 8100145c393SAndrew Melnychenko } 81175ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 81275ebec11SMaxime Coquelin vdev->backend_features = features; 81375ebec11SMaxime Coquelin 81475ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 81575ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 81675ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 81775ebec11SMaxime Coquelin } 81875ebec11SMaxime Coquelin 81975ebec11SMaxime Coquelin return features; 8206e790746SPaolo Bonzini } 8216e790746SPaolo Bonzini 822019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 8236e790746SPaolo Bonzini { 824019a3edbSGerd Hoffmann uint64_t features = 0; 8256e790746SPaolo Bonzini 8266e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 8276e790746SPaolo Bonzini * but also these: */ 8280cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 8290cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 8300cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 8310cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 8320cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 8336e790746SPaolo Bonzini 8346e790746SPaolo Bonzini return features; 8356e790746SPaolo Bonzini } 8366e790746SPaolo Bonzini 837644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 838644c9858SDmitry Fleytman { 839ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 840644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 841644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 842644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 843644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 844644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 845644c9858SDmitry Fleytman } 846644c9858SDmitry Fleytman 847644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 848644c9858SDmitry Fleytman { 849644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 850644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 851644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 852644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 853644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 854644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 855644c9858SDmitry Fleytman 856644c9858SDmitry Fleytman return guest_offloads_mask & features; 857644c9858SDmitry Fleytman } 858644c9858SDmitry Fleytman 859644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 860644c9858SDmitry Fleytman { 861644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 862644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 863644c9858SDmitry Fleytman } 864644c9858SDmitry Fleytman 865f5e1847bSJuan Quintela typedef struct { 866f5e1847bSJuan Quintela VirtIONet *n; 86712b2fad7SKevin Wolf DeviceState *dev; 86812b2fad7SKevin Wolf } FailoverDevice; 869f5e1847bSJuan Quintela 870f5e1847bSJuan Quintela /** 87112b2fad7SKevin Wolf * Set the failover primary device 872f5e1847bSJuan Quintela * 873f5e1847bSJuan Quintela * @opaque: FailoverId to setup 874f5e1847bSJuan Quintela * @opts: opts for device we are handling 875f5e1847bSJuan Quintela * @errp: returns an error if this function fails 876f5e1847bSJuan Quintela */ 87712b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque) 878f5e1847bSJuan Quintela { 87912b2fad7SKevin Wolf FailoverDevice *fdev = opaque; 88012b2fad7SKevin Wolf PCIDevice *pci_dev = (PCIDevice *) 88112b2fad7SKevin Wolf object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE); 882f5e1847bSJuan Quintela 88312b2fad7SKevin Wolf if (!pci_dev) { 88412b2fad7SKevin Wolf return 0; 88512b2fad7SKevin Wolf } 88612b2fad7SKevin Wolf 88712b2fad7SKevin Wolf if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) { 88812b2fad7SKevin Wolf fdev->dev = dev; 889f5e1847bSJuan Quintela return 1; 890f5e1847bSJuan Quintela } 891f5e1847bSJuan Quintela 892f5e1847bSJuan Quintela return 0; 893f5e1847bSJuan Quintela } 894f5e1847bSJuan Quintela 895f5e1847bSJuan Quintela /** 89685d3b931SJuan Quintela * Find the primary device for this failover virtio-net 89785d3b931SJuan Quintela * 89885d3b931SJuan Quintela * @n: VirtIONet device 89985d3b931SJuan Quintela * @errp: returns an error if this function fails 90085d3b931SJuan Quintela */ 9010a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n) 9029711cd0dSJens Freimann { 90312b2fad7SKevin Wolf FailoverDevice fdev = { 90412b2fad7SKevin Wolf .n = n, 90512b2fad7SKevin Wolf }; 9069711cd0dSJens Freimann 90712b2fad7SKevin Wolf qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL, 90812b2fad7SKevin Wolf NULL, NULL, &fdev); 90912b2fad7SKevin Wolf return fdev.dev; 9109711cd0dSJens Freimann } 9119711cd0dSJens Freimann 91221e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp) 91321e8709bSJuan Quintela { 91421e8709bSJuan Quintela Error *err = NULL; 91521e8709bSJuan Quintela DeviceState *dev = failover_find_primary_device(n); 91621e8709bSJuan Quintela 91721e8709bSJuan Quintela if (dev) { 91821e8709bSJuan Quintela return; 91921e8709bSJuan Quintela } 92021e8709bSJuan Quintela 921259a10dbSKevin Wolf if (!n->primary_opts) { 92297ca9c59SLaurent Vivier error_setg(errp, "Primary device not found"); 92397ca9c59SLaurent Vivier error_append_hint(errp, "Virtio-net failover will not work. Make " 92497ca9c59SLaurent Vivier "sure primary device has parameter" 92597ca9c59SLaurent Vivier " failover_pair_id=%s\n", n->netclient_name); 92621e8709bSJuan Quintela return; 92721e8709bSJuan Quintela } 928259a10dbSKevin Wolf 929f3558b1bSKevin Wolf dev = qdev_device_add_from_qdict(n->primary_opts, 930f3558b1bSKevin Wolf n->primary_opts_from_json, 931f3558b1bSKevin Wolf &err); 93221e8709bSJuan Quintela if (err) { 933f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 934259a10dbSKevin Wolf n->primary_opts = NULL; 93500e7b129SLaurent Vivier } else { 93600e7b129SLaurent Vivier object_unref(OBJECT(dev)); 93721e8709bSJuan Quintela } 93821e8709bSJuan Quintela error_propagate(errp, err); 93921e8709bSJuan Quintela } 94021e8709bSJuan Quintela 941d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 9426e790746SPaolo Bonzini { 94317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9449711cd0dSJens Freimann Error *err = NULL; 9456e790746SPaolo Bonzini int i; 9466e790746SPaolo Bonzini 94775ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 94875ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 94975ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 95075ebec11SMaxime Coquelin } 95175ebec11SMaxime Coquelin 952ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 95359079029SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_RSS) || 95495129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 9556e790746SPaolo Bonzini 956ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 95795129d6fSCornelia Huck virtio_has_feature(features, 958bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 95995129d6fSCornelia Huck virtio_has_feature(features, 960e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 961e22f0603SYuri Benditovich virtio_has_feature(features, 962e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 9636e790746SPaolo Bonzini 9642974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9652974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4); 9662974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) && 9672974e916SYuri Benditovich virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6); 968e22f0603SYuri Benditovich n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS); 9692974e916SYuri Benditovich 9706e790746SPaolo Bonzini if (n->has_vnet_hdr) { 971644c9858SDmitry Fleytman n->curr_guest_offloads = 972644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 973644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 9746e790746SPaolo Bonzini } 9756e790746SPaolo Bonzini 976441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 9776e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 9786e790746SPaolo Bonzini 979ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 9806e790746SPaolo Bonzini continue; 9816e790746SPaolo Bonzini } 982ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 9836e790746SPaolo Bonzini } 9840b1eaa88SStefan Fritsch 98595129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 9860b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 9870b1eaa88SStefan Fritsch } else { 9880b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 9890b1eaa88SStefan Fritsch } 9909711cd0dSJens Freimann 9919711cd0dSJens Freimann if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) { 9929711cd0dSJens Freimann qapi_event_send_failover_negotiated(n->netclient_name); 993e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 9949711cd0dSJens Freimann failover_add_primary(n, &err); 9959711cd0dSJens Freimann if (err) { 9961b529d90SLaurent Vivier if (!qtest_enabled()) { 9979711cd0dSJens Freimann warn_report_err(err); 9981b529d90SLaurent Vivier } else { 9991b529d90SLaurent Vivier error_free(err); 10001b529d90SLaurent Vivier } 10019711cd0dSJens Freimann } 10026e790746SPaolo Bonzini } 100321e8709bSJuan Quintela } 10046e790746SPaolo Bonzini 10056e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 10066e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10076e790746SPaolo Bonzini { 10086e790746SPaolo Bonzini uint8_t on; 10096e790746SPaolo Bonzini size_t s; 1010b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10116e790746SPaolo Bonzini 10126e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 10136e790746SPaolo Bonzini if (s != sizeof(on)) { 10146e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10156e790746SPaolo Bonzini } 10166e790746SPaolo Bonzini 10176e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 10186e790746SPaolo Bonzini n->promisc = on; 10196e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 10206e790746SPaolo Bonzini n->allmulti = on; 10216e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 10226e790746SPaolo Bonzini n->alluni = on; 10236e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 10246e790746SPaolo Bonzini n->nomulti = on; 10256e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 10266e790746SPaolo Bonzini n->nouni = on; 10276e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 10286e790746SPaolo Bonzini n->nobcast = on; 10296e790746SPaolo Bonzini } else { 10306e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10316e790746SPaolo Bonzini } 10326e790746SPaolo Bonzini 1033b1be4280SAmos Kong rxfilter_notify(nc); 1034b1be4280SAmos Kong 10356e790746SPaolo Bonzini return VIRTIO_NET_OK; 10366e790746SPaolo Bonzini } 10376e790746SPaolo Bonzini 1038644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 1039644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 1040644c9858SDmitry Fleytman { 1041644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 1042644c9858SDmitry Fleytman uint64_t offloads; 1043644c9858SDmitry Fleytman size_t s; 1044644c9858SDmitry Fleytman 104595129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 1046644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1047644c9858SDmitry Fleytman } 1048644c9858SDmitry Fleytman 1049644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 1050644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 1051644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1052644c9858SDmitry Fleytman } 1053644c9858SDmitry Fleytman 1054644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 1055644c9858SDmitry Fleytman uint64_t supported_offloads; 1056644c9858SDmitry Fleytman 1057189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 1058189ae6bbSJason Wang 1059644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 1060644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1061644c9858SDmitry Fleytman } 1062644c9858SDmitry Fleytman 10632974e916SYuri Benditovich n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10642974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4); 10652974e916SYuri Benditovich n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) && 10662974e916SYuri Benditovich virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6); 10672974e916SYuri Benditovich virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT); 10682974e916SYuri Benditovich 1069644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 1070644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 1071644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1072644c9858SDmitry Fleytman } 1073644c9858SDmitry Fleytman 1074644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 1075644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 1076644c9858SDmitry Fleytman 1077644c9858SDmitry Fleytman return VIRTIO_NET_OK; 1078644c9858SDmitry Fleytman } else { 1079644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 1080644c9858SDmitry Fleytman } 1081644c9858SDmitry Fleytman } 1082644c9858SDmitry Fleytman 10836e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 10846e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 10856e790746SPaolo Bonzini { 10861399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 10876e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 10886e790746SPaolo Bonzini size_t s; 1089b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 10906e790746SPaolo Bonzini 10916e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 10926e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 10936e790746SPaolo Bonzini return VIRTIO_NET_ERR; 10946e790746SPaolo Bonzini } 10956e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 10966e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 10976e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1098b1be4280SAmos Kong rxfilter_notify(nc); 1099b1be4280SAmos Kong 11006e790746SPaolo Bonzini return VIRTIO_NET_OK; 11016e790746SPaolo Bonzini } 11026e790746SPaolo Bonzini 11036e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 11046e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11056e790746SPaolo Bonzini } 11066e790746SPaolo Bonzini 1107cae2e556SAmos Kong int in_use = 0; 1108cae2e556SAmos Kong int first_multi = 0; 1109cae2e556SAmos Kong uint8_t uni_overflow = 0; 1110cae2e556SAmos Kong uint8_t multi_overflow = 0; 1111cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 11126e790746SPaolo Bonzini 11136e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11146e790746SPaolo Bonzini sizeof(mac_data.entries)); 11151399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11166e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1117b1be4280SAmos Kong goto error; 11186e790746SPaolo Bonzini } 11196e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11206e790746SPaolo Bonzini 11216e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 1122b1be4280SAmos Kong goto error; 11236e790746SPaolo Bonzini } 11246e790746SPaolo Bonzini 11256e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 1126cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 11276e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11286e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1129b1be4280SAmos Kong goto error; 11306e790746SPaolo Bonzini } 1131cae2e556SAmos Kong in_use += mac_data.entries; 11326e790746SPaolo Bonzini } else { 1133cae2e556SAmos Kong uni_overflow = 1; 11346e790746SPaolo Bonzini } 11356e790746SPaolo Bonzini 11366e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 11376e790746SPaolo Bonzini 1138cae2e556SAmos Kong first_multi = in_use; 11396e790746SPaolo Bonzini 11406e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 11416e790746SPaolo Bonzini sizeof(mac_data.entries)); 11421399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 11436e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 1144b1be4280SAmos Kong goto error; 11456e790746SPaolo Bonzini } 11466e790746SPaolo Bonzini 11476e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 11486e790746SPaolo Bonzini 11496e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 1150b1be4280SAmos Kong goto error; 11516e790746SPaolo Bonzini } 11526e790746SPaolo Bonzini 1153edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 1154cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 11556e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 11566e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 1157b1be4280SAmos Kong goto error; 11586e790746SPaolo Bonzini } 1159cae2e556SAmos Kong in_use += mac_data.entries; 11606e790746SPaolo Bonzini } else { 1161cae2e556SAmos Kong multi_overflow = 1; 11626e790746SPaolo Bonzini } 11636e790746SPaolo Bonzini 1164cae2e556SAmos Kong n->mac_table.in_use = in_use; 1165cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 1166cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 1167cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 1168cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 1169cae2e556SAmos Kong g_free(macs); 1170b1be4280SAmos Kong rxfilter_notify(nc); 1171b1be4280SAmos Kong 11726e790746SPaolo Bonzini return VIRTIO_NET_OK; 1173b1be4280SAmos Kong 1174b1be4280SAmos Kong error: 1175cae2e556SAmos Kong g_free(macs); 1176b1be4280SAmos Kong return VIRTIO_NET_ERR; 11776e790746SPaolo Bonzini } 11786e790746SPaolo Bonzini 11796e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 11806e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 11816e790746SPaolo Bonzini { 11821399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 11836e790746SPaolo Bonzini uint16_t vid; 11846e790746SPaolo Bonzini size_t s; 1185b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 11866e790746SPaolo Bonzini 11876e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 11881399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 11896e790746SPaolo Bonzini if (s != sizeof(vid)) { 11906e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11916e790746SPaolo Bonzini } 11926e790746SPaolo Bonzini 11936e790746SPaolo Bonzini if (vid >= MAX_VLAN) 11946e790746SPaolo Bonzini return VIRTIO_NET_ERR; 11956e790746SPaolo Bonzini 11966e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 11976e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 11986e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 11996e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 12006e790746SPaolo Bonzini else 12016e790746SPaolo Bonzini return VIRTIO_NET_ERR; 12026e790746SPaolo Bonzini 1203b1be4280SAmos Kong rxfilter_notify(nc); 1204b1be4280SAmos Kong 12056e790746SPaolo Bonzini return VIRTIO_NET_OK; 12066e790746SPaolo Bonzini } 12076e790746SPaolo Bonzini 1208f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 1209f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 1210f57fcf70SJason Wang { 12119d8c6a25SDr. David Alan Gilbert trace_virtio_net_handle_announce(n->announce_timer.round); 1212f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 1213f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 1214f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 12159d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 12169d8c6a25SDr. David Alan Gilbert qemu_announce_timer_step(&n->announce_timer); 1217f57fcf70SJason Wang } 1218f57fcf70SJason Wang return VIRTIO_NET_OK; 1219f57fcf70SJason Wang } else { 1220f57fcf70SJason Wang return VIRTIO_NET_ERR; 1221f57fcf70SJason Wang } 1222f57fcf70SJason Wang } 1223f57fcf70SJason Wang 12240145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n); 12250145c393SAndrew Melnychenko 122659079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n) 122759079029SYuri Benditovich { 122859079029SYuri Benditovich if (n->rss_data.enabled) { 122959079029SYuri Benditovich trace_virtio_net_rss_disable(); 123059079029SYuri Benditovich } 123159079029SYuri Benditovich n->rss_data.enabled = false; 12320145c393SAndrew Melnychenko 12330145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 12340145c393SAndrew Melnychenko } 12350145c393SAndrew Melnychenko 12360145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd) 12370145c393SAndrew Melnychenko { 12380145c393SAndrew Melnychenko NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0); 12390145c393SAndrew Melnychenko if (nc == NULL || nc->info->set_steering_ebpf == NULL) { 12400145c393SAndrew Melnychenko return false; 12410145c393SAndrew Melnychenko } 12420145c393SAndrew Melnychenko 12430145c393SAndrew Melnychenko return nc->info->set_steering_ebpf(nc, prog_fd); 12440145c393SAndrew Melnychenko } 12450145c393SAndrew Melnychenko 12460145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data, 12470145c393SAndrew Melnychenko struct EBPFRSSConfig *config) 12480145c393SAndrew Melnychenko { 12490145c393SAndrew Melnychenko config->redirect = data->redirect; 12500145c393SAndrew Melnychenko config->populate_hash = data->populate_hash; 12510145c393SAndrew Melnychenko config->hash_types = data->hash_types; 12520145c393SAndrew Melnychenko config->indirections_len = data->indirections_len; 12530145c393SAndrew Melnychenko config->default_queue = data->default_queue; 12540145c393SAndrew Melnychenko } 12550145c393SAndrew Melnychenko 12560145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n) 12570145c393SAndrew Melnychenko { 12580145c393SAndrew Melnychenko struct EBPFRSSConfig config = {}; 12590145c393SAndrew Melnychenko 12600145c393SAndrew Melnychenko if (!ebpf_rss_is_loaded(&n->ebpf_rss)) { 12610145c393SAndrew Melnychenko return false; 12620145c393SAndrew Melnychenko } 12630145c393SAndrew Melnychenko 12640145c393SAndrew Melnychenko rss_data_to_rss_config(&n->rss_data, &config); 12650145c393SAndrew Melnychenko 12660145c393SAndrew Melnychenko if (!ebpf_rss_set_all(&n->ebpf_rss, &config, 12670145c393SAndrew Melnychenko n->rss_data.indirections_table, n->rss_data.key)) { 12680145c393SAndrew Melnychenko return false; 12690145c393SAndrew Melnychenko } 12700145c393SAndrew Melnychenko 12710145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) { 12720145c393SAndrew Melnychenko return false; 12730145c393SAndrew Melnychenko } 12740145c393SAndrew Melnychenko 12750145c393SAndrew Melnychenko return true; 12760145c393SAndrew Melnychenko } 12770145c393SAndrew Melnychenko 12780145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n) 12790145c393SAndrew Melnychenko { 12800145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12810145c393SAndrew Melnychenko } 12820145c393SAndrew Melnychenko 12830145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n) 12840145c393SAndrew Melnychenko { 12850145c393SAndrew Melnychenko if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) { 12860145c393SAndrew Melnychenko /* backend does't support steering ebpf */ 12870145c393SAndrew Melnychenko return false; 12880145c393SAndrew Melnychenko } 12890145c393SAndrew Melnychenko 12900145c393SAndrew Melnychenko return ebpf_rss_load(&n->ebpf_rss); 12910145c393SAndrew Melnychenko } 12920145c393SAndrew Melnychenko 12930145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n) 12940145c393SAndrew Melnychenko { 12950145c393SAndrew Melnychenko virtio_net_attach_ebpf_to_backend(n->nic, -1); 12960145c393SAndrew Melnychenko ebpf_rss_unload(&n->ebpf_rss); 129759079029SYuri Benditovich } 129859079029SYuri Benditovich 129959079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n, 1300e22f0603SYuri Benditovich struct iovec *iov, 1301e22f0603SYuri Benditovich unsigned int iov_cnt, 1302e22f0603SYuri Benditovich bool do_rss) 130359079029SYuri Benditovich { 130459079029SYuri Benditovich VirtIODevice *vdev = VIRTIO_DEVICE(n); 130559079029SYuri Benditovich struct virtio_net_rss_config cfg; 130659079029SYuri Benditovich size_t s, offset = 0, size_get; 1307441537f1SJason Wang uint16_t queue_pairs, i; 130859079029SYuri Benditovich struct { 130959079029SYuri Benditovich uint16_t us; 131059079029SYuri Benditovich uint8_t b; 131159079029SYuri Benditovich } QEMU_PACKED temp; 131259079029SYuri Benditovich const char *err_msg = ""; 131359079029SYuri Benditovich uint32_t err_value = 0; 131459079029SYuri Benditovich 1315e22f0603SYuri Benditovich if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) { 131659079029SYuri Benditovich err_msg = "RSS is not negotiated"; 131759079029SYuri Benditovich goto error; 131859079029SYuri Benditovich } 1319e22f0603SYuri Benditovich if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) { 1320e22f0603SYuri Benditovich err_msg = "Hash report is not negotiated"; 1321e22f0603SYuri Benditovich goto error; 1322e22f0603SYuri Benditovich } 132359079029SYuri Benditovich size_get = offsetof(struct virtio_net_rss_config, indirection_table); 132459079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get); 132559079029SYuri Benditovich if (s != size_get) { 132659079029SYuri Benditovich err_msg = "Short command buffer"; 132759079029SYuri Benditovich err_value = (uint32_t)s; 132859079029SYuri Benditovich goto error; 132959079029SYuri Benditovich } 133059079029SYuri Benditovich n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types); 133159079029SYuri Benditovich n->rss_data.indirections_len = 133259079029SYuri Benditovich virtio_lduw_p(vdev, &cfg.indirection_table_mask); 133359079029SYuri Benditovich n->rss_data.indirections_len++; 1334e22f0603SYuri Benditovich if (!do_rss) { 1335e22f0603SYuri Benditovich n->rss_data.indirections_len = 1; 1336e22f0603SYuri Benditovich } 133759079029SYuri Benditovich if (!is_power_of_2(n->rss_data.indirections_len)) { 133859079029SYuri Benditovich err_msg = "Invalid size of indirection table"; 133959079029SYuri Benditovich err_value = n->rss_data.indirections_len; 134059079029SYuri Benditovich goto error; 134159079029SYuri Benditovich } 134259079029SYuri Benditovich if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { 134359079029SYuri Benditovich err_msg = "Too large indirection table"; 134459079029SYuri Benditovich err_value = n->rss_data.indirections_len; 134559079029SYuri Benditovich goto error; 134659079029SYuri Benditovich } 1347e22f0603SYuri Benditovich n->rss_data.default_queue = do_rss ? 1348e22f0603SYuri Benditovich virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0; 1349441537f1SJason Wang if (n->rss_data.default_queue >= n->max_queue_pairs) { 135059079029SYuri Benditovich err_msg = "Invalid default queue"; 135159079029SYuri Benditovich err_value = n->rss_data.default_queue; 135259079029SYuri Benditovich goto error; 135359079029SYuri Benditovich } 135459079029SYuri Benditovich offset += size_get; 135559079029SYuri Benditovich size_get = sizeof(uint16_t) * n->rss_data.indirections_len; 135659079029SYuri Benditovich g_free(n->rss_data.indirections_table); 135759079029SYuri Benditovich n->rss_data.indirections_table = g_malloc(size_get); 135859079029SYuri Benditovich if (!n->rss_data.indirections_table) { 135959079029SYuri Benditovich err_msg = "Can't allocate indirections table"; 136059079029SYuri Benditovich err_value = n->rss_data.indirections_len; 136159079029SYuri Benditovich goto error; 136259079029SYuri Benditovich } 136359079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, 136459079029SYuri Benditovich n->rss_data.indirections_table, size_get); 136559079029SYuri Benditovich if (s != size_get) { 136659079029SYuri Benditovich err_msg = "Short indirection table buffer"; 136759079029SYuri Benditovich err_value = (uint32_t)s; 136859079029SYuri Benditovich goto error; 136959079029SYuri Benditovich } 137059079029SYuri Benditovich for (i = 0; i < n->rss_data.indirections_len; ++i) { 137159079029SYuri Benditovich uint16_t val = n->rss_data.indirections_table[i]; 137259079029SYuri Benditovich n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val); 137359079029SYuri Benditovich } 137459079029SYuri Benditovich offset += size_get; 137559079029SYuri Benditovich size_get = sizeof(temp); 137659079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get); 137759079029SYuri Benditovich if (s != size_get) { 1378441537f1SJason Wang err_msg = "Can't get queue_pairs"; 137959079029SYuri Benditovich err_value = (uint32_t)s; 138059079029SYuri Benditovich goto error; 138159079029SYuri Benditovich } 1382441537f1SJason Wang queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs; 1383441537f1SJason Wang if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) { 1384441537f1SJason Wang err_msg = "Invalid number of queue_pairs"; 1385441537f1SJason Wang err_value = queue_pairs; 138659079029SYuri Benditovich goto error; 138759079029SYuri Benditovich } 138859079029SYuri Benditovich if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) { 138959079029SYuri Benditovich err_msg = "Invalid key size"; 139059079029SYuri Benditovich err_value = temp.b; 139159079029SYuri Benditovich goto error; 139259079029SYuri Benditovich } 139359079029SYuri Benditovich if (!temp.b && n->rss_data.hash_types) { 139459079029SYuri Benditovich err_msg = "No key provided"; 139559079029SYuri Benditovich err_value = 0; 139659079029SYuri Benditovich goto error; 139759079029SYuri Benditovich } 139859079029SYuri Benditovich if (!temp.b && !n->rss_data.hash_types) { 139959079029SYuri Benditovich virtio_net_disable_rss(n); 1400441537f1SJason Wang return queue_pairs; 140159079029SYuri Benditovich } 140259079029SYuri Benditovich offset += size_get; 140359079029SYuri Benditovich size_get = temp.b; 140459079029SYuri Benditovich s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get); 140559079029SYuri Benditovich if (s != size_get) { 140659079029SYuri Benditovich err_msg = "Can get key buffer"; 140759079029SYuri Benditovich err_value = (uint32_t)s; 140859079029SYuri Benditovich goto error; 140959079029SYuri Benditovich } 141059079029SYuri Benditovich n->rss_data.enabled = true; 14110145c393SAndrew Melnychenko 14120145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 14130145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 14140145c393SAndrew Melnychenko /* EBPF must be loaded for vhost */ 14150145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 14160145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS for vhost"); 14170145c393SAndrew Melnychenko goto error; 14180145c393SAndrew Melnychenko } 14190145c393SAndrew Melnychenko /* fallback to software RSS */ 14200145c393SAndrew Melnychenko warn_report("Can't load eBPF RSS - fallback to software RSS"); 14210145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 14220145c393SAndrew Melnychenko } 14230145c393SAndrew Melnychenko } else { 14240145c393SAndrew Melnychenko /* use software RSS for hash populating */ 14250145c393SAndrew Melnychenko /* and detach eBPF if was loaded before */ 14260145c393SAndrew Melnychenko virtio_net_detach_epbf_rss(n); 14270145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 14280145c393SAndrew Melnychenko } 14290145c393SAndrew Melnychenko 143059079029SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 143159079029SYuri Benditovich n->rss_data.indirections_len, 143259079029SYuri Benditovich temp.b); 1433441537f1SJason Wang return queue_pairs; 143459079029SYuri Benditovich error: 143559079029SYuri Benditovich trace_virtio_net_rss_error(err_msg, err_value); 143659079029SYuri Benditovich virtio_net_disable_rss(n); 143759079029SYuri Benditovich return 0; 143859079029SYuri Benditovich } 143959079029SYuri Benditovich 14406e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 14416e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 14426e790746SPaolo Bonzini { 144317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1444441537f1SJason Wang uint16_t queue_pairs; 14452a7888ccSSi-Wei Liu NetClientState *nc = qemu_get_queue(n->nic); 14466e790746SPaolo Bonzini 144759079029SYuri Benditovich virtio_net_disable_rss(n); 1448e22f0603SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) { 1449441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false); 1450441537f1SJason Wang return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR; 1451e22f0603SYuri Benditovich } 145259079029SYuri Benditovich if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) { 1453441537f1SJason Wang queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true); 145459079029SYuri Benditovich } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 145559079029SYuri Benditovich struct virtio_net_ctrl_mq mq; 145659079029SYuri Benditovich size_t s; 145759079029SYuri Benditovich if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) { 145859079029SYuri Benditovich return VIRTIO_NET_ERR; 145959079029SYuri Benditovich } 14606e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 14616e790746SPaolo Bonzini if (s != sizeof(mq)) { 14626e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14636e790746SPaolo Bonzini } 1464441537f1SJason Wang queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 14656e790746SPaolo Bonzini 146659079029SYuri Benditovich } else { 14676e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14686e790746SPaolo Bonzini } 14696e790746SPaolo Bonzini 1470441537f1SJason Wang if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1471441537f1SJason Wang queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1472441537f1SJason Wang queue_pairs > n->max_queue_pairs || 14736e790746SPaolo Bonzini !n->multiqueue) { 14746e790746SPaolo Bonzini return VIRTIO_NET_ERR; 14756e790746SPaolo Bonzini } 14766e790746SPaolo Bonzini 1477441537f1SJason Wang n->curr_queue_pairs = queue_pairs; 1478ca8717f9SEugenio Pérez if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 1479ca8717f9SEugenio Pérez /* 1480ca8717f9SEugenio Pérez * Avoid updating the backend for a vdpa device: We're only interested 1481ca8717f9SEugenio Pérez * in updating the device model queues. 1482ca8717f9SEugenio Pérez */ 1483ca8717f9SEugenio Pérez return VIRTIO_NET_OK; 1484ca8717f9SEugenio Pérez } 1485441537f1SJason Wang /* stop the backend before changing the number of queue_pairs to avoid handling a 14866e790746SPaolo Bonzini * disabled queue */ 148717a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1488441537f1SJason Wang virtio_net_set_queue_pairs(n); 14896e790746SPaolo Bonzini 14906e790746SPaolo Bonzini return VIRTIO_NET_OK; 14916e790746SPaolo Bonzini } 1492ba7eadb5SGreg Kurz 1493640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, 1494640b8a1cSEugenio Pérez const struct iovec *in_sg, unsigned in_num, 1495640b8a1cSEugenio Pérez const struct iovec *out_sg, 1496640b8a1cSEugenio Pérez unsigned out_num) 14976e790746SPaolo Bonzini { 149817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14996e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 15006e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 15016e790746SPaolo Bonzini size_t s; 1502771b6ed3SJason Wang struct iovec *iov, *iov2; 1503640b8a1cSEugenio Pérez 1504640b8a1cSEugenio Pérez if (iov_size(in_sg, in_num) < sizeof(status) || 1505640b8a1cSEugenio Pérez iov_size(out_sg, out_num) < sizeof(ctrl)) { 1506640b8a1cSEugenio Pérez virtio_error(vdev, "virtio-net ctrl missing headers"); 1507640b8a1cSEugenio Pérez return 0; 1508640b8a1cSEugenio Pérez } 1509640b8a1cSEugenio Pérez 1510640b8a1cSEugenio Pérez iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num); 1511640b8a1cSEugenio Pérez s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl)); 1512640b8a1cSEugenio Pérez iov_discard_front(&iov, &out_num, sizeof(ctrl)); 1513640b8a1cSEugenio Pérez if (s != sizeof(ctrl)) { 1514640b8a1cSEugenio Pérez status = VIRTIO_NET_ERR; 1515640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 1516640b8a1cSEugenio Pérez status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num); 1517640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 1518640b8a1cSEugenio Pérez status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num); 1519640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 1520640b8a1cSEugenio Pérez status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num); 1521640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1522640b8a1cSEugenio Pérez status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num); 1523640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 1524640b8a1cSEugenio Pérez status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num); 1525640b8a1cSEugenio Pérez } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1526640b8a1cSEugenio Pérez status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num); 1527640b8a1cSEugenio Pérez } 1528640b8a1cSEugenio Pérez 1529640b8a1cSEugenio Pérez s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status)); 1530640b8a1cSEugenio Pérez assert(s == sizeof(status)); 1531640b8a1cSEugenio Pérez 1532640b8a1cSEugenio Pérez g_free(iov2); 1533640b8a1cSEugenio Pérez return sizeof(status); 1534640b8a1cSEugenio Pérez } 1535640b8a1cSEugenio Pérez 1536640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 1537640b8a1cSEugenio Pérez { 1538640b8a1cSEugenio Pérez VirtQueueElement *elem; 15396e790746SPaolo Bonzini 154051b19ebeSPaolo Bonzini for (;;) { 1541640b8a1cSEugenio Pérez size_t written; 154251b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 154351b19ebeSPaolo Bonzini if (!elem) { 154451b19ebeSPaolo Bonzini break; 154551b19ebeSPaolo Bonzini } 1546640b8a1cSEugenio Pérez 1547640b8a1cSEugenio Pérez written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num, 1548640b8a1cSEugenio Pérez elem->out_sg, elem->out_num); 1549640b8a1cSEugenio Pérez if (written > 0) { 1550640b8a1cSEugenio Pérez virtqueue_push(vq, elem, written); 1551640b8a1cSEugenio Pérez virtio_notify(vdev, vq); 1552640b8a1cSEugenio Pérez g_free(elem); 1553640b8a1cSEugenio Pérez } else { 1554ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 1555ba7eadb5SGreg Kurz g_free(elem); 1556ba7eadb5SGreg Kurz break; 15576e790746SPaolo Bonzini } 15586e790746SPaolo Bonzini } 15596e790746SPaolo Bonzini } 15606e790746SPaolo Bonzini 15616e790746SPaolo Bonzini /* RX */ 15626e790746SPaolo Bonzini 15636e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 15646e790746SPaolo Bonzini { 156517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 15666e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 15676e790746SPaolo Bonzini 15686e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 15696e790746SPaolo Bonzini } 15706e790746SPaolo Bonzini 1571b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc) 15726e790746SPaolo Bonzini { 15736e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 157417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 15756e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 15766e790746SPaolo Bonzini 157717a0ca55SKONRAD Frederic if (!vdev->vm_running) { 1578b8c4b67eSPhilippe Mathieu-Daudé return false; 15796e790746SPaolo Bonzini } 15806e790746SPaolo Bonzini 1581441537f1SJason Wang if (nc->queue_index >= n->curr_queue_pairs) { 1582b8c4b67eSPhilippe Mathieu-Daudé return false; 15836e790746SPaolo Bonzini } 15846e790746SPaolo Bonzini 15856e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 158617a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 1587b8c4b67eSPhilippe Mathieu-Daudé return false; 15886e790746SPaolo Bonzini } 15896e790746SPaolo Bonzini 1590b8c4b67eSPhilippe Mathieu-Daudé return true; 15916e790746SPaolo Bonzini } 15926e790746SPaolo Bonzini 15936e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 15946e790746SPaolo Bonzini { 15956e790746SPaolo Bonzini VirtIONet *n = q->n; 15966e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 15976e790746SPaolo Bonzini (n->mergeable_rx_bufs && 15986e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 15996e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 16006e790746SPaolo Bonzini 16016e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 16026e790746SPaolo Bonzini * available after the above check but before notification was 16036e790746SPaolo Bonzini * enabled, check for available buffers again. 16046e790746SPaolo Bonzini */ 16056e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 16066e790746SPaolo Bonzini (n->mergeable_rx_bufs && 16076e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 16086e790746SPaolo Bonzini return 0; 16096e790746SPaolo Bonzini } 16106e790746SPaolo Bonzini } 16116e790746SPaolo Bonzini 16126e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 16136e790746SPaolo Bonzini return 1; 16146e790746SPaolo Bonzini } 16156e790746SPaolo Bonzini 16161399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1617032a74a1SCédric Le Goater { 16181399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 16191399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 16201399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 16211399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1622032a74a1SCédric Le Goater } 1623032a74a1SCédric Le Goater 16246e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 16256e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 16266e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 16276e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 16286e790746SPaolo Bonzini * dhclient yet. 16296e790746SPaolo Bonzini * 16306e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 16316e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 16326e790746SPaolo Bonzini * kernels. 16336e790746SPaolo Bonzini * 16346e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 16356e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 16366e790746SPaolo Bonzini * cache. 16376e790746SPaolo Bonzini */ 16386e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 16396e790746SPaolo Bonzini uint8_t *buf, size_t size) 16406e790746SPaolo Bonzini { 16416e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 16426e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 16436e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 16446e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 16456e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 1646f5746335SBin Meng net_checksum_calculate(buf, size, CSUM_UDP); 16476e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 16486e790746SPaolo Bonzini } 16496e790746SPaolo Bonzini } 16506e790746SPaolo Bonzini 16516e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 16526e790746SPaolo Bonzini const void *buf, size_t size) 16536e790746SPaolo Bonzini { 16546e790746SPaolo Bonzini if (n->has_vnet_hdr) { 16556e790746SPaolo Bonzini /* FIXME this cast is evil */ 16566e790746SPaolo Bonzini void *wbuf = (void *)buf; 16576e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 16586e790746SPaolo Bonzini size - n->host_hdr_len); 16591bfa316cSGreg Kurz 16601bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 16611399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 16621bfa316cSGreg Kurz } 16636e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 16646e790746SPaolo Bonzini } else { 16656e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 16666e790746SPaolo Bonzini .flags = 0, 16676e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 16686e790746SPaolo Bonzini }; 16696e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 16706e790746SPaolo Bonzini } 16716e790746SPaolo Bonzini } 16726e790746SPaolo Bonzini 16736e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 16746e790746SPaolo Bonzini { 16756e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 16766e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 16776e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 16786e790746SPaolo Bonzini int i; 16796e790746SPaolo Bonzini 16806e790746SPaolo Bonzini if (n->promisc) 16816e790746SPaolo Bonzini return 1; 16826e790746SPaolo Bonzini 16836e790746SPaolo Bonzini ptr += n->host_hdr_len; 16846e790746SPaolo Bonzini 16856e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 16867542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 16876e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 16886e790746SPaolo Bonzini return 0; 16896e790746SPaolo Bonzini } 16906e790746SPaolo Bonzini 16916e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 16926e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 16936e790746SPaolo Bonzini return !n->nobcast; 16946e790746SPaolo Bonzini } else if (n->nomulti) { 16956e790746SPaolo Bonzini return 0; 16966e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 16976e790746SPaolo Bonzini return 1; 16986e790746SPaolo Bonzini } 16996e790746SPaolo Bonzini 17006e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 17016e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17026e790746SPaolo Bonzini return 1; 17036e790746SPaolo Bonzini } 17046e790746SPaolo Bonzini } 17056e790746SPaolo Bonzini } else { // unicast 17066e790746SPaolo Bonzini if (n->nouni) { 17076e790746SPaolo Bonzini return 0; 17086e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 17096e790746SPaolo Bonzini return 1; 17106e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 17116e790746SPaolo Bonzini return 1; 17126e790746SPaolo Bonzini } 17136e790746SPaolo Bonzini 17146e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 17156e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 17166e790746SPaolo Bonzini return 1; 17176e790746SPaolo Bonzini } 17186e790746SPaolo Bonzini } 17196e790746SPaolo Bonzini } 17206e790746SPaolo Bonzini 17216e790746SPaolo Bonzini return 0; 17226e790746SPaolo Bonzini } 17236e790746SPaolo Bonzini 17244474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4, 17254474e37aSYuri Benditovich bool isip6, 17264474e37aSYuri Benditovich bool isudp, 17274474e37aSYuri Benditovich bool istcp, 17284474e37aSYuri Benditovich uint32_t types) 17294474e37aSYuri Benditovich { 17304474e37aSYuri Benditovich if (isip4) { 17314474e37aSYuri Benditovich if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) { 17324474e37aSYuri Benditovich return NetPktRssIpV4Tcp; 17334474e37aSYuri Benditovich } 17344474e37aSYuri Benditovich if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) { 17354474e37aSYuri Benditovich return NetPktRssIpV4Udp; 17364474e37aSYuri Benditovich } 17374474e37aSYuri Benditovich if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { 17384474e37aSYuri Benditovich return NetPktRssIpV4; 17394474e37aSYuri Benditovich } 17404474e37aSYuri Benditovich } else if (isip6) { 17414474e37aSYuri Benditovich uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | 17424474e37aSYuri Benditovich VIRTIO_NET_RSS_HASH_TYPE_TCPv6; 17434474e37aSYuri Benditovich 17444474e37aSYuri Benditovich if (istcp && (types & mask)) { 17454474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ? 17464474e37aSYuri Benditovich NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp; 17474474e37aSYuri Benditovich } 17484474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6; 17494474e37aSYuri Benditovich if (isudp && (types & mask)) { 17504474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ? 17514474e37aSYuri Benditovich NetPktRssIpV6UdpEx : NetPktRssIpV6Udp; 17524474e37aSYuri Benditovich } 17534474e37aSYuri Benditovich mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6; 17544474e37aSYuri Benditovich if (types & mask) { 17554474e37aSYuri Benditovich return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ? 17564474e37aSYuri Benditovich NetPktRssIpV6Ex : NetPktRssIpV6; 17574474e37aSYuri Benditovich } 17584474e37aSYuri Benditovich } 17594474e37aSYuri Benditovich return 0xff; 17604474e37aSYuri Benditovich } 17614474e37aSYuri Benditovich 1762e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report, 1763e22f0603SYuri Benditovich uint32_t hash) 1764e22f0603SYuri Benditovich { 1765e22f0603SYuri Benditovich struct virtio_net_hdr_v1_hash *hdr = (void *)buf; 1766e22f0603SYuri Benditovich hdr->hash_value = hash; 1767e22f0603SYuri Benditovich hdr->hash_report = report; 1768e22f0603SYuri Benditovich } 1769e22f0603SYuri Benditovich 17704474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf, 177197cd965cSPaolo Bonzini size_t size) 17726e790746SPaolo Bonzini { 17736e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 1774e22f0603SYuri Benditovich unsigned int index = nc->queue_index, new_index = index; 17754474e37aSYuri Benditovich struct NetRxPkt *pkt = n->rx_pkt; 17764474e37aSYuri Benditovich uint8_t net_hash_type; 17774474e37aSYuri Benditovich uint32_t hash; 17784474e37aSYuri Benditovich bool isip4, isip6, isudp, istcp; 1779e22f0603SYuri Benditovich static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = { 1780e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv4, 1781e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv4, 1782e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6, 1783e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6, 1784e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_IPv6_EX, 1785e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_TCPv6_EX, 1786e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv4, 1787e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6, 1788e22f0603SYuri Benditovich VIRTIO_NET_HASH_REPORT_UDPv6_EX 1789e22f0603SYuri Benditovich }; 17904474e37aSYuri Benditovich 17914474e37aSYuri Benditovich net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len, 17924474e37aSYuri Benditovich size - n->host_hdr_len); 17934474e37aSYuri Benditovich net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp); 17944474e37aSYuri Benditovich if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) { 17954474e37aSYuri Benditovich istcp = isudp = false; 17964474e37aSYuri Benditovich } 17974474e37aSYuri Benditovich if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) { 17984474e37aSYuri Benditovich istcp = isudp = false; 17994474e37aSYuri Benditovich } 18004474e37aSYuri Benditovich net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp, 18014474e37aSYuri Benditovich n->rss_data.hash_types); 18024474e37aSYuri Benditovich if (net_hash_type > NetPktRssIpV6UdpEx) { 1803e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1804e22f0603SYuri Benditovich virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0); 1805e22f0603SYuri Benditovich } 1806e22f0603SYuri Benditovich return n->rss_data.redirect ? n->rss_data.default_queue : -1; 18074474e37aSYuri Benditovich } 18084474e37aSYuri Benditovich 18094474e37aSYuri Benditovich hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key); 1810e22f0603SYuri Benditovich 1811e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1812e22f0603SYuri Benditovich virtio_set_packet_hash(buf, reports[net_hash_type], hash); 1813e22f0603SYuri Benditovich } 1814e22f0603SYuri Benditovich 1815e22f0603SYuri Benditovich if (n->rss_data.redirect) { 18164474e37aSYuri Benditovich new_index = hash & (n->rss_data.indirections_len - 1); 18174474e37aSYuri Benditovich new_index = n->rss_data.indirections_table[new_index]; 18184474e37aSYuri Benditovich } 1819e22f0603SYuri Benditovich 1820e22f0603SYuri Benditovich return (index == new_index) ? -1 : new_index; 18214474e37aSYuri Benditovich } 18224474e37aSYuri Benditovich 18234474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 18244474e37aSYuri Benditovich size_t size, bool no_rss) 18254474e37aSYuri Benditovich { 18264474e37aSYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 18276e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 182817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1829bedd7e93SJason Wang VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE]; 1830bedd7e93SJason Wang size_t lens[VIRTQUEUE_MAX_SIZE]; 18316e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 18326e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 18336e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 1834bedd7e93SJason Wang size_t offset, i, guest_offset, j; 1835bedd7e93SJason Wang ssize_t err; 18366e790746SPaolo Bonzini 18376e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 18386e790746SPaolo Bonzini return -1; 18396e790746SPaolo Bonzini } 18406e790746SPaolo Bonzini 18410145c393SAndrew Melnychenko if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) { 18424474e37aSYuri Benditovich int index = virtio_net_process_rss(nc, buf, size); 18434474e37aSYuri Benditovich if (index >= 0) { 18444474e37aSYuri Benditovich NetClientState *nc2 = qemu_get_subqueue(n->nic, index); 18454474e37aSYuri Benditovich return virtio_net_receive_rcu(nc2, buf, size, true); 18464474e37aSYuri Benditovich } 18474474e37aSYuri Benditovich } 18484474e37aSYuri Benditovich 18496e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 18506e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 18516e790746SPaolo Bonzini return 0; 18526e790746SPaolo Bonzini } 18536e790746SPaolo Bonzini 18546e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 18556e790746SPaolo Bonzini return size; 18566e790746SPaolo Bonzini 18576e790746SPaolo Bonzini offset = i = 0; 18586e790746SPaolo Bonzini 18596e790746SPaolo Bonzini while (offset < size) { 186051b19ebeSPaolo Bonzini VirtQueueElement *elem; 18616e790746SPaolo Bonzini int len, total; 186251b19ebeSPaolo Bonzini const struct iovec *sg; 18636e790746SPaolo Bonzini 18646e790746SPaolo Bonzini total = 0; 18656e790746SPaolo Bonzini 1866bedd7e93SJason Wang if (i == VIRTQUEUE_MAX_SIZE) { 1867bedd7e93SJason Wang virtio_error(vdev, "virtio-net unexpected long buffer chain"); 1868bedd7e93SJason Wang err = size; 1869bedd7e93SJason Wang goto err; 1870bedd7e93SJason Wang } 1871bedd7e93SJason Wang 187251b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 187351b19ebeSPaolo Bonzini if (!elem) { 1874ba10b9c0SGreg Kurz if (i) { 1875ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 18766e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1877019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1878019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 18796e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1880019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1881019a3edbSGerd Hoffmann vdev->guest_features); 1882ba10b9c0SGreg Kurz } 1883bedd7e93SJason Wang err = -1; 1884bedd7e93SJason Wang goto err; 18856e790746SPaolo Bonzini } 18866e790746SPaolo Bonzini 188751b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1888ba10b9c0SGreg Kurz virtio_error(vdev, 1889ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1890ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1891ba10b9c0SGreg Kurz g_free(elem); 1892bedd7e93SJason Wang err = -1; 1893bedd7e93SJason Wang goto err; 18946e790746SPaolo Bonzini } 18956e790746SPaolo Bonzini 189651b19ebeSPaolo Bonzini sg = elem->in_sg; 18976e790746SPaolo Bonzini if (i == 0) { 18986e790746SPaolo Bonzini assert(offset == 0); 18996e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 19006e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 190151b19ebeSPaolo Bonzini sg, elem->in_num, 19026e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 19036e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 19046e790746SPaolo Bonzini } 19056e790746SPaolo Bonzini 190651b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 1907e22f0603SYuri Benditovich if (n->rss_data.populate_hash) { 1908e22f0603SYuri Benditovich offset = sizeof(mhdr); 1909e22f0603SYuri Benditovich iov_from_buf(sg, elem->in_num, offset, 1910e22f0603SYuri Benditovich buf + offset, n->host_hdr_len - sizeof(mhdr)); 1911e22f0603SYuri Benditovich } 19126e790746SPaolo Bonzini offset = n->host_hdr_len; 19136e790746SPaolo Bonzini total += n->guest_hdr_len; 19146e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 19156e790746SPaolo Bonzini } else { 19166e790746SPaolo Bonzini guest_offset = 0; 19176e790746SPaolo Bonzini } 19186e790746SPaolo Bonzini 19196e790746SPaolo Bonzini /* copy in packet. ugh */ 192051b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 19216e790746SPaolo Bonzini buf + offset, size - offset); 19226e790746SPaolo Bonzini total += len; 19236e790746SPaolo Bonzini offset += len; 19246e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 19256e790746SPaolo Bonzini * must have consumed the complete packet. 19266e790746SPaolo Bonzini * Otherwise, drop it. */ 19276e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 192827e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 192951b19ebeSPaolo Bonzini g_free(elem); 1930bedd7e93SJason Wang err = size; 1931bedd7e93SJason Wang goto err; 19326e790746SPaolo Bonzini } 19336e790746SPaolo Bonzini 1934bedd7e93SJason Wang elems[i] = elem; 1935bedd7e93SJason Wang lens[i] = total; 1936bedd7e93SJason Wang i++; 19376e790746SPaolo Bonzini } 19386e790746SPaolo Bonzini 19396e790746SPaolo Bonzini if (mhdr_cnt) { 19401399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 19416e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 19426e790746SPaolo Bonzini 0, 19436e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 19446e790746SPaolo Bonzini } 19456e790746SPaolo Bonzini 1946bedd7e93SJason Wang for (j = 0; j < i; j++) { 1947bedd7e93SJason Wang /* signal other side */ 1948bedd7e93SJason Wang virtqueue_fill(q->rx_vq, elems[j], lens[j], j); 1949bedd7e93SJason Wang g_free(elems[j]); 1950bedd7e93SJason Wang } 1951bedd7e93SJason Wang 19526e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 195317a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 19546e790746SPaolo Bonzini 19556e790746SPaolo Bonzini return size; 1956bedd7e93SJason Wang 1957bedd7e93SJason Wang err: 1958bedd7e93SJason Wang for (j = 0; j < i; j++) { 1959abe300d9SJason Wang virtqueue_detach_element(q->rx_vq, elems[j], lens[j]); 1960bedd7e93SJason Wang g_free(elems[j]); 1961bedd7e93SJason Wang } 1962bedd7e93SJason Wang 1963bedd7e93SJason Wang return err; 19646e790746SPaolo Bonzini } 19656e790746SPaolo Bonzini 19662974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf, 196797cd965cSPaolo Bonzini size_t size) 196897cd965cSPaolo Bonzini { 1969068ddfa9SDr. David Alan Gilbert RCU_READ_LOCK_GUARD(); 197097cd965cSPaolo Bonzini 19714474e37aSYuri Benditovich return virtio_net_receive_rcu(nc, buf, size, false); 197297cd965cSPaolo Bonzini } 197397cd965cSPaolo Bonzini 19742974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain, 19752974e916SYuri Benditovich const uint8_t *buf, 19762974e916SYuri Benditovich VirtioNetRscUnit *unit) 19772974e916SYuri Benditovich { 19782974e916SYuri Benditovich uint16_t ip_hdrlen; 19792974e916SYuri Benditovich struct ip_header *ip; 19802974e916SYuri Benditovich 19812974e916SYuri Benditovich ip = (struct ip_header *)(buf + chain->n->guest_hdr_len 19822974e916SYuri Benditovich + sizeof(struct eth_header)); 19832974e916SYuri Benditovich unit->ip = (void *)ip; 19842974e916SYuri Benditovich ip_hdrlen = (ip->ip_ver_len & 0xF) << 2; 19852974e916SYuri Benditovich unit->ip_plen = &ip->ip_len; 19862974e916SYuri Benditovich unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen); 19872974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 19882974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen; 19892974e916SYuri Benditovich } 19902974e916SYuri Benditovich 19912974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain, 19922974e916SYuri Benditovich const uint8_t *buf, 19932974e916SYuri Benditovich VirtioNetRscUnit *unit) 19942974e916SYuri Benditovich { 19952974e916SYuri Benditovich struct ip6_header *ip6; 19962974e916SYuri Benditovich 19972974e916SYuri Benditovich ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len 19982974e916SYuri Benditovich + sizeof(struct eth_header)); 19992974e916SYuri Benditovich unit->ip = ip6; 20002974e916SYuri Benditovich unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 200178ee6bd0SPhilippe Mathieu-Daudé unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) 20022974e916SYuri Benditovich + sizeof(struct ip6_header)); 20032974e916SYuri Benditovich unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10; 20042974e916SYuri Benditovich 20052974e916SYuri Benditovich /* There is a difference between payload lenght in ipv4 and v6, 20062974e916SYuri Benditovich ip header is excluded in ipv6 */ 20072974e916SYuri Benditovich unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen; 20082974e916SYuri Benditovich } 20092974e916SYuri Benditovich 20102974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain, 20112974e916SYuri Benditovich VirtioNetRscSeg *seg) 20122974e916SYuri Benditovich { 20132974e916SYuri Benditovich int ret; 2014dd3d85e8SYuri Benditovich struct virtio_net_hdr_v1 *h; 20152974e916SYuri Benditovich 2016dd3d85e8SYuri Benditovich h = (struct virtio_net_hdr_v1 *)seg->buf; 20172974e916SYuri Benditovich h->flags = 0; 20182974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_NONE; 20192974e916SYuri Benditovich 20202974e916SYuri Benditovich if (seg->is_coalesced) { 2021dd3d85e8SYuri Benditovich h->rsc.segments = seg->packets; 2022dd3d85e8SYuri Benditovich h->rsc.dup_acks = seg->dup_ack; 20232974e916SYuri Benditovich h->flags = VIRTIO_NET_HDR_F_RSC_INFO; 20242974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 20252974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 20262974e916SYuri Benditovich } else { 20272974e916SYuri Benditovich h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 20282974e916SYuri Benditovich } 20292974e916SYuri Benditovich } 20302974e916SYuri Benditovich 20312974e916SYuri Benditovich ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size); 20322974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 20332974e916SYuri Benditovich g_free(seg->buf); 20342974e916SYuri Benditovich g_free(seg); 20352974e916SYuri Benditovich 20362974e916SYuri Benditovich return ret; 20372974e916SYuri Benditovich } 20382974e916SYuri Benditovich 20392974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq) 20402974e916SYuri Benditovich { 20412974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn; 20422974e916SYuri Benditovich VirtioNetRscChain *chain = (VirtioNetRscChain *)opq; 20432974e916SYuri Benditovich 20442974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) { 20452974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 20462974e916SYuri Benditovich chain->stat.purge_failed++; 20472974e916SYuri Benditovich continue; 20482974e916SYuri Benditovich } 20492974e916SYuri Benditovich } 20502974e916SYuri Benditovich 20512974e916SYuri Benditovich chain->stat.timer++; 20522974e916SYuri Benditovich if (!QTAILQ_EMPTY(&chain->buffers)) { 20532974e916SYuri Benditovich timer_mod(chain->drain_timer, 20542974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 20552974e916SYuri Benditovich } 20562974e916SYuri Benditovich } 20572974e916SYuri Benditovich 20582974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n) 20592974e916SYuri Benditovich { 20602974e916SYuri Benditovich VirtioNetRscChain *chain, *rn_chain; 20612974e916SYuri Benditovich VirtioNetRscSeg *seg, *rn_seg; 20622974e916SYuri Benditovich 20632974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) { 20642974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) { 20652974e916SYuri Benditovich QTAILQ_REMOVE(&chain->buffers, seg, next); 20662974e916SYuri Benditovich g_free(seg->buf); 20672974e916SYuri Benditovich g_free(seg); 20682974e916SYuri Benditovich } 20692974e916SYuri Benditovich 20702974e916SYuri Benditovich timer_free(chain->drain_timer); 20712974e916SYuri Benditovich QTAILQ_REMOVE(&n->rsc_chains, chain, next); 20722974e916SYuri Benditovich g_free(chain); 20732974e916SYuri Benditovich } 20742974e916SYuri Benditovich } 20752974e916SYuri Benditovich 20762974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain, 20772974e916SYuri Benditovich NetClientState *nc, 20782974e916SYuri Benditovich const uint8_t *buf, size_t size) 20792974e916SYuri Benditovich { 20802974e916SYuri Benditovich uint16_t hdr_len; 20812974e916SYuri Benditovich VirtioNetRscSeg *seg; 20822974e916SYuri Benditovich 20832974e916SYuri Benditovich hdr_len = chain->n->guest_hdr_len; 2084b21e2380SMarkus Armbruster seg = g_new(VirtioNetRscSeg, 1); 20852974e916SYuri Benditovich seg->buf = g_malloc(hdr_len + sizeof(struct eth_header) 20862974e916SYuri Benditovich + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD); 20872974e916SYuri Benditovich memcpy(seg->buf, buf, size); 20882974e916SYuri Benditovich seg->size = size; 20892974e916SYuri Benditovich seg->packets = 1; 20902974e916SYuri Benditovich seg->dup_ack = 0; 20912974e916SYuri Benditovich seg->is_coalesced = 0; 20922974e916SYuri Benditovich seg->nc = nc; 20932974e916SYuri Benditovich 20942974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&chain->buffers, seg, next); 20952974e916SYuri Benditovich chain->stat.cache++; 20962974e916SYuri Benditovich 20972974e916SYuri Benditovich switch (chain->proto) { 20982974e916SYuri Benditovich case ETH_P_IP: 20992974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit); 21002974e916SYuri Benditovich break; 21012974e916SYuri Benditovich case ETH_P_IPV6: 21022974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit); 21032974e916SYuri Benditovich break; 21042974e916SYuri Benditovich default: 21052974e916SYuri Benditovich g_assert_not_reached(); 21062974e916SYuri Benditovich } 21072974e916SYuri Benditovich } 21082974e916SYuri Benditovich 21092974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain, 21102974e916SYuri Benditovich VirtioNetRscSeg *seg, 21112974e916SYuri Benditovich const uint8_t *buf, 21122974e916SYuri Benditovich struct tcp_header *n_tcp, 21132974e916SYuri Benditovich struct tcp_header *o_tcp) 21142974e916SYuri Benditovich { 21152974e916SYuri Benditovich uint32_t nack, oack; 21162974e916SYuri Benditovich uint16_t nwin, owin; 21172974e916SYuri Benditovich 21182974e916SYuri Benditovich nack = htonl(n_tcp->th_ack); 21192974e916SYuri Benditovich nwin = htons(n_tcp->th_win); 21202974e916SYuri Benditovich oack = htonl(o_tcp->th_ack); 21212974e916SYuri Benditovich owin = htons(o_tcp->th_win); 21222974e916SYuri Benditovich 21232974e916SYuri Benditovich if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) { 21242974e916SYuri Benditovich chain->stat.ack_out_of_win++; 21252974e916SYuri Benditovich return RSC_FINAL; 21262974e916SYuri Benditovich } else if (nack == oack) { 21272974e916SYuri Benditovich /* duplicated ack or window probe */ 21282974e916SYuri Benditovich if (nwin == owin) { 21292974e916SYuri Benditovich /* duplicated ack, add dup ack count due to whql test up to 1 */ 21302974e916SYuri Benditovich chain->stat.dup_ack++; 21312974e916SYuri Benditovich return RSC_FINAL; 21322974e916SYuri Benditovich } else { 21332974e916SYuri Benditovich /* Coalesce window update */ 21342974e916SYuri Benditovich o_tcp->th_win = n_tcp->th_win; 21352974e916SYuri Benditovich chain->stat.win_update++; 21362974e916SYuri Benditovich return RSC_COALESCE; 21372974e916SYuri Benditovich } 21382974e916SYuri Benditovich } else { 21392974e916SYuri Benditovich /* pure ack, go to 'C', finalize*/ 21402974e916SYuri Benditovich chain->stat.pure_ack++; 21412974e916SYuri Benditovich return RSC_FINAL; 21422974e916SYuri Benditovich } 21432974e916SYuri Benditovich } 21442974e916SYuri Benditovich 21452974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain, 21462974e916SYuri Benditovich VirtioNetRscSeg *seg, 21472974e916SYuri Benditovich const uint8_t *buf, 21482974e916SYuri Benditovich VirtioNetRscUnit *n_unit) 21492974e916SYuri Benditovich { 21502974e916SYuri Benditovich void *data; 21512974e916SYuri Benditovich uint16_t o_ip_len; 21522974e916SYuri Benditovich uint32_t nseq, oseq; 21532974e916SYuri Benditovich VirtioNetRscUnit *o_unit; 21542974e916SYuri Benditovich 21552974e916SYuri Benditovich o_unit = &seg->unit; 21562974e916SYuri Benditovich o_ip_len = htons(*o_unit->ip_plen); 21572974e916SYuri Benditovich nseq = htonl(n_unit->tcp->th_seq); 21582974e916SYuri Benditovich oseq = htonl(o_unit->tcp->th_seq); 21592974e916SYuri Benditovich 21602974e916SYuri Benditovich /* out of order or retransmitted. */ 21612974e916SYuri Benditovich if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) { 21622974e916SYuri Benditovich chain->stat.data_out_of_win++; 21632974e916SYuri Benditovich return RSC_FINAL; 21642974e916SYuri Benditovich } 21652974e916SYuri Benditovich 21662974e916SYuri Benditovich data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen; 21672974e916SYuri Benditovich if (nseq == oseq) { 21682974e916SYuri Benditovich if ((o_unit->payload == 0) && n_unit->payload) { 21692974e916SYuri Benditovich /* From no payload to payload, normal case, not a dup ack or etc */ 21702974e916SYuri Benditovich chain->stat.data_after_pure_ack++; 21712974e916SYuri Benditovich goto coalesce; 21722974e916SYuri Benditovich } else { 21732974e916SYuri Benditovich return virtio_net_rsc_handle_ack(chain, seg, buf, 21742974e916SYuri Benditovich n_unit->tcp, o_unit->tcp); 21752974e916SYuri Benditovich } 21762974e916SYuri Benditovich } else if ((nseq - oseq) != o_unit->payload) { 21772974e916SYuri Benditovich /* Not a consistent packet, out of order */ 21782974e916SYuri Benditovich chain->stat.data_out_of_order++; 21792974e916SYuri Benditovich return RSC_FINAL; 21802974e916SYuri Benditovich } else { 21812974e916SYuri Benditovich coalesce: 21822974e916SYuri Benditovich if ((o_ip_len + n_unit->payload) > chain->max_payload) { 21832974e916SYuri Benditovich chain->stat.over_size++; 21842974e916SYuri Benditovich return RSC_FINAL; 21852974e916SYuri Benditovich } 21862974e916SYuri Benditovich 21872974e916SYuri Benditovich /* Here comes the right data, the payload length in v4/v6 is different, 21882974e916SYuri Benditovich so use the field value to update and record the new data len */ 21892974e916SYuri Benditovich o_unit->payload += n_unit->payload; /* update new data len */ 21902974e916SYuri Benditovich 21912974e916SYuri Benditovich /* update field in ip header */ 21922974e916SYuri Benditovich *o_unit->ip_plen = htons(o_ip_len + n_unit->payload); 21932974e916SYuri Benditovich 21942974e916SYuri Benditovich /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced 21952974e916SYuri Benditovich for windows guest, while this may change the behavior for linux 21962974e916SYuri Benditovich guest (only if it uses RSC feature). */ 21972974e916SYuri Benditovich o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags; 21982974e916SYuri Benditovich 21992974e916SYuri Benditovich o_unit->tcp->th_ack = n_unit->tcp->th_ack; 22002974e916SYuri Benditovich o_unit->tcp->th_win = n_unit->tcp->th_win; 22012974e916SYuri Benditovich 22022974e916SYuri Benditovich memmove(seg->buf + seg->size, data, n_unit->payload); 22032974e916SYuri Benditovich seg->size += n_unit->payload; 22042974e916SYuri Benditovich seg->packets++; 22052974e916SYuri Benditovich chain->stat.coalesced++; 22062974e916SYuri Benditovich return RSC_COALESCE; 22072974e916SYuri Benditovich } 22082974e916SYuri Benditovich } 22092974e916SYuri Benditovich 22102974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain, 22112974e916SYuri Benditovich VirtioNetRscSeg *seg, 22122974e916SYuri Benditovich const uint8_t *buf, size_t size, 22132974e916SYuri Benditovich VirtioNetRscUnit *unit) 22142974e916SYuri Benditovich { 22152974e916SYuri Benditovich struct ip_header *ip1, *ip2; 22162974e916SYuri Benditovich 22172974e916SYuri Benditovich ip1 = (struct ip_header *)(unit->ip); 22182974e916SYuri Benditovich ip2 = (struct ip_header *)(seg->unit.ip); 22192974e916SYuri Benditovich if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst) 22202974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 22212974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 22222974e916SYuri Benditovich chain->stat.no_match++; 22232974e916SYuri Benditovich return RSC_NO_MATCH; 22242974e916SYuri Benditovich } 22252974e916SYuri Benditovich 22262974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 22272974e916SYuri Benditovich } 22282974e916SYuri Benditovich 22292974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain, 22302974e916SYuri Benditovich VirtioNetRscSeg *seg, 22312974e916SYuri Benditovich const uint8_t *buf, size_t size, 22322974e916SYuri Benditovich VirtioNetRscUnit *unit) 22332974e916SYuri Benditovich { 22342974e916SYuri Benditovich struct ip6_header *ip1, *ip2; 22352974e916SYuri Benditovich 22362974e916SYuri Benditovich ip1 = (struct ip6_header *)(unit->ip); 22372974e916SYuri Benditovich ip2 = (struct ip6_header *)(seg->unit.ip); 22382974e916SYuri Benditovich if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address)) 22392974e916SYuri Benditovich || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address)) 22402974e916SYuri Benditovich || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport) 22412974e916SYuri Benditovich || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) { 22422974e916SYuri Benditovich chain->stat.no_match++; 22432974e916SYuri Benditovich return RSC_NO_MATCH; 22442974e916SYuri Benditovich } 22452974e916SYuri Benditovich 22462974e916SYuri Benditovich return virtio_net_rsc_coalesce_data(chain, seg, buf, unit); 22472974e916SYuri Benditovich } 22482974e916SYuri Benditovich 22492974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain 22502974e916SYuri Benditovich * to prevent out of order */ 22512974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain, 22522974e916SYuri Benditovich struct tcp_header *tcp) 22532974e916SYuri Benditovich { 22542974e916SYuri Benditovich uint16_t tcp_hdr; 22552974e916SYuri Benditovich uint16_t tcp_flag; 22562974e916SYuri Benditovich 22572974e916SYuri Benditovich tcp_flag = htons(tcp->th_offset_flags); 22582974e916SYuri Benditovich tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10; 22592974e916SYuri Benditovich tcp_flag &= VIRTIO_NET_TCP_FLAG; 22602974e916SYuri Benditovich if (tcp_flag & TH_SYN) { 22612974e916SYuri Benditovich chain->stat.tcp_syn++; 22622974e916SYuri Benditovich return RSC_BYPASS; 22632974e916SYuri Benditovich } 22642974e916SYuri Benditovich 22652974e916SYuri Benditovich if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) { 22662974e916SYuri Benditovich chain->stat.tcp_ctrl_drain++; 22672974e916SYuri Benditovich return RSC_FINAL; 22682974e916SYuri Benditovich } 22692974e916SYuri Benditovich 22702974e916SYuri Benditovich if (tcp_hdr > sizeof(struct tcp_header)) { 22712974e916SYuri Benditovich chain->stat.tcp_all_opt++; 22722974e916SYuri Benditovich return RSC_FINAL; 22732974e916SYuri Benditovich } 22742974e916SYuri Benditovich 22752974e916SYuri Benditovich return RSC_CANDIDATE; 22762974e916SYuri Benditovich } 22772974e916SYuri Benditovich 22782974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain, 22792974e916SYuri Benditovich NetClientState *nc, 22802974e916SYuri Benditovich const uint8_t *buf, size_t size, 22812974e916SYuri Benditovich VirtioNetRscUnit *unit) 22822974e916SYuri Benditovich { 22832974e916SYuri Benditovich int ret; 22842974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 22852974e916SYuri Benditovich 22862974e916SYuri Benditovich if (QTAILQ_EMPTY(&chain->buffers)) { 22872974e916SYuri Benditovich chain->stat.empty_cache++; 22882974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 22892974e916SYuri Benditovich timer_mod(chain->drain_timer, 22902974e916SYuri Benditovich qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout); 22912974e916SYuri Benditovich return size; 22922974e916SYuri Benditovich } 22932974e916SYuri Benditovich 22942974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 22952974e916SYuri Benditovich if (chain->proto == ETH_P_IP) { 22962974e916SYuri Benditovich ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit); 22972974e916SYuri Benditovich } else { 22982974e916SYuri Benditovich ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit); 22992974e916SYuri Benditovich } 23002974e916SYuri Benditovich 23012974e916SYuri Benditovich if (ret == RSC_FINAL) { 23022974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 23032974e916SYuri Benditovich /* Send failed */ 23042974e916SYuri Benditovich chain->stat.final_failed++; 23052974e916SYuri Benditovich return 0; 23062974e916SYuri Benditovich } 23072974e916SYuri Benditovich 23082974e916SYuri Benditovich /* Send current packet */ 23092974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23102974e916SYuri Benditovich } else if (ret == RSC_NO_MATCH) { 23112974e916SYuri Benditovich continue; 23122974e916SYuri Benditovich } else { 23132974e916SYuri Benditovich /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */ 23142974e916SYuri Benditovich seg->is_coalesced = 1; 23152974e916SYuri Benditovich return size; 23162974e916SYuri Benditovich } 23172974e916SYuri Benditovich } 23182974e916SYuri Benditovich 23192974e916SYuri Benditovich chain->stat.no_match_cache++; 23202974e916SYuri Benditovich virtio_net_rsc_cache_buf(chain, nc, buf, size); 23212974e916SYuri Benditovich return size; 23222974e916SYuri Benditovich } 23232974e916SYuri Benditovich 23242974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */ 23252974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain, 23262974e916SYuri Benditovich NetClientState *nc, 23272974e916SYuri Benditovich const uint8_t *buf, size_t size, 23282974e916SYuri Benditovich uint16_t ip_start, uint16_t ip_size, 23292974e916SYuri Benditovich uint16_t tcp_port) 23302974e916SYuri Benditovich { 23312974e916SYuri Benditovich VirtioNetRscSeg *seg, *nseg; 23322974e916SYuri Benditovich uint32_t ppair1, ppair2; 23332974e916SYuri Benditovich 23342974e916SYuri Benditovich ppair1 = *(uint32_t *)(buf + tcp_port); 23352974e916SYuri Benditovich QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { 23362974e916SYuri Benditovich ppair2 = *(uint32_t *)(seg->buf + tcp_port); 23372974e916SYuri Benditovich if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) 23382974e916SYuri Benditovich || (ppair1 != ppair2)) { 23392974e916SYuri Benditovich continue; 23402974e916SYuri Benditovich } 23412974e916SYuri Benditovich if (virtio_net_rsc_drain_seg(chain, seg) == 0) { 23422974e916SYuri Benditovich chain->stat.drain_failed++; 23432974e916SYuri Benditovich } 23442974e916SYuri Benditovich 23452974e916SYuri Benditovich break; 23462974e916SYuri Benditovich } 23472974e916SYuri Benditovich 23482974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 23492974e916SYuri Benditovich } 23502974e916SYuri Benditovich 23512974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain, 23522974e916SYuri Benditovich struct ip_header *ip, 23532974e916SYuri Benditovich const uint8_t *buf, size_t size) 23542974e916SYuri Benditovich { 23552974e916SYuri Benditovich uint16_t ip_len; 23562974e916SYuri Benditovich 23572974e916SYuri Benditovich /* Not an ipv4 packet */ 23582974e916SYuri Benditovich if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) { 23592974e916SYuri Benditovich chain->stat.ip_option++; 23602974e916SYuri Benditovich return RSC_BYPASS; 23612974e916SYuri Benditovich } 23622974e916SYuri Benditovich 23632974e916SYuri Benditovich /* Don't handle packets with ip option */ 23642974e916SYuri Benditovich if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) { 23652974e916SYuri Benditovich chain->stat.ip_option++; 23662974e916SYuri Benditovich return RSC_BYPASS; 23672974e916SYuri Benditovich } 23682974e916SYuri Benditovich 23692974e916SYuri Benditovich if (ip->ip_p != IPPROTO_TCP) { 23702974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 23712974e916SYuri Benditovich return RSC_BYPASS; 23722974e916SYuri Benditovich } 23732974e916SYuri Benditovich 23742974e916SYuri Benditovich /* Don't handle packets with ip fragment */ 23752974e916SYuri Benditovich if (!(htons(ip->ip_off) & IP_DF)) { 23762974e916SYuri Benditovich chain->stat.ip_frag++; 23772974e916SYuri Benditovich return RSC_BYPASS; 23782974e916SYuri Benditovich } 23792974e916SYuri Benditovich 23802974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 23812974e916SYuri Benditovich if (IPTOS_ECN(ip->ip_tos)) { 23822974e916SYuri Benditovich chain->stat.ip_ecn++; 23832974e916SYuri Benditovich return RSC_BYPASS; 23842974e916SYuri Benditovich } 23852974e916SYuri Benditovich 23862974e916SYuri Benditovich ip_len = htons(ip->ip_len); 23872974e916SYuri Benditovich if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) 23882974e916SYuri Benditovich || ip_len > (size - chain->n->guest_hdr_len - 23892974e916SYuri Benditovich sizeof(struct eth_header))) { 23902974e916SYuri Benditovich chain->stat.ip_hacked++; 23912974e916SYuri Benditovich return RSC_BYPASS; 23922974e916SYuri Benditovich } 23932974e916SYuri Benditovich 23942974e916SYuri Benditovich return RSC_CANDIDATE; 23952974e916SYuri Benditovich } 23962974e916SYuri Benditovich 23972974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain, 23982974e916SYuri Benditovich NetClientState *nc, 23992974e916SYuri Benditovich const uint8_t *buf, size_t size) 24002974e916SYuri Benditovich { 24012974e916SYuri Benditovich int32_t ret; 24022974e916SYuri Benditovich uint16_t hdr_len; 24032974e916SYuri Benditovich VirtioNetRscUnit unit; 24042974e916SYuri Benditovich 24052974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 24062974e916SYuri Benditovich 24072974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header) 24082974e916SYuri Benditovich + sizeof(struct tcp_header))) { 24092974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24102974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24112974e916SYuri Benditovich } 24122974e916SYuri Benditovich 24132974e916SYuri Benditovich virtio_net_rsc_extract_unit4(chain, buf, &unit); 24142974e916SYuri Benditovich if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size) 24152974e916SYuri Benditovich != RSC_CANDIDATE) { 24162974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24172974e916SYuri Benditovich } 24182974e916SYuri Benditovich 24192974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24202974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24212974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24222974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24232974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24242974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 12), 24252974e916SYuri Benditovich VIRTIO_NET_IP4_ADDR_SIZE, 24262974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)); 24272974e916SYuri Benditovich } 24282974e916SYuri Benditovich 24292974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 24302974e916SYuri Benditovich } 24312974e916SYuri Benditovich 24322974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain, 24332974e916SYuri Benditovich struct ip6_header *ip6, 24342974e916SYuri Benditovich const uint8_t *buf, size_t size) 24352974e916SYuri Benditovich { 24362974e916SYuri Benditovich uint16_t ip_len; 24372974e916SYuri Benditovich 24382974e916SYuri Benditovich if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4) 24392974e916SYuri Benditovich != IP_HEADER_VERSION_6) { 24402974e916SYuri Benditovich return RSC_BYPASS; 24412974e916SYuri Benditovich } 24422974e916SYuri Benditovich 24432974e916SYuri Benditovich /* Both option and protocol is checked in this */ 24442974e916SYuri Benditovich if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) { 24452974e916SYuri Benditovich chain->stat.bypass_not_tcp++; 24462974e916SYuri Benditovich return RSC_BYPASS; 24472974e916SYuri Benditovich } 24482974e916SYuri Benditovich 24492974e916SYuri Benditovich ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen); 24502974e916SYuri Benditovich if (ip_len < sizeof(struct tcp_header) || 24512974e916SYuri Benditovich ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header) 24522974e916SYuri Benditovich - sizeof(struct ip6_header))) { 24532974e916SYuri Benditovich chain->stat.ip_hacked++; 24542974e916SYuri Benditovich return RSC_BYPASS; 24552974e916SYuri Benditovich } 24562974e916SYuri Benditovich 24572974e916SYuri Benditovich /* Don't handle packets with ecn flag */ 24582974e916SYuri Benditovich if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) { 24592974e916SYuri Benditovich chain->stat.ip_ecn++; 24602974e916SYuri Benditovich return RSC_BYPASS; 24612974e916SYuri Benditovich } 24622974e916SYuri Benditovich 24632974e916SYuri Benditovich return RSC_CANDIDATE; 24642974e916SYuri Benditovich } 24652974e916SYuri Benditovich 24662974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc, 24672974e916SYuri Benditovich const uint8_t *buf, size_t size) 24682974e916SYuri Benditovich { 24692974e916SYuri Benditovich int32_t ret; 24702974e916SYuri Benditovich uint16_t hdr_len; 24712974e916SYuri Benditovich VirtioNetRscChain *chain; 24722974e916SYuri Benditovich VirtioNetRscUnit unit; 24732974e916SYuri Benditovich 24742974e916SYuri Benditovich chain = (VirtioNetRscChain *)opq; 24752974e916SYuri Benditovich hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len; 24762974e916SYuri Benditovich 24772974e916SYuri Benditovich if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header) 24782974e916SYuri Benditovich + sizeof(tcp_header))) { 24792974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24802974e916SYuri Benditovich } 24812974e916SYuri Benditovich 24822974e916SYuri Benditovich virtio_net_rsc_extract_unit6(chain, buf, &unit); 24832974e916SYuri Benditovich if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain, 24842974e916SYuri Benditovich unit.ip, buf, size)) { 24852974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24862974e916SYuri Benditovich } 24872974e916SYuri Benditovich 24882974e916SYuri Benditovich ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp); 24892974e916SYuri Benditovich if (ret == RSC_BYPASS) { 24902974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 24912974e916SYuri Benditovich } else if (ret == RSC_FINAL) { 24922974e916SYuri Benditovich return virtio_net_rsc_drain_flow(chain, nc, buf, size, 24932974e916SYuri Benditovich ((hdr_len + sizeof(struct eth_header)) + 8), 24942974e916SYuri Benditovich VIRTIO_NET_IP6_ADDR_SIZE, 24952974e916SYuri Benditovich hdr_len + sizeof(struct eth_header) 24962974e916SYuri Benditovich + sizeof(struct ip6_header)); 24972974e916SYuri Benditovich } 24982974e916SYuri Benditovich 24992974e916SYuri Benditovich return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit); 25002974e916SYuri Benditovich } 25012974e916SYuri Benditovich 25022974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n, 25032974e916SYuri Benditovich NetClientState *nc, 25042974e916SYuri Benditovich uint16_t proto) 25052974e916SYuri Benditovich { 25062974e916SYuri Benditovich VirtioNetRscChain *chain; 25072974e916SYuri Benditovich 25082974e916SYuri Benditovich if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) { 25092974e916SYuri Benditovich return NULL; 25102974e916SYuri Benditovich } 25112974e916SYuri Benditovich 25122974e916SYuri Benditovich QTAILQ_FOREACH(chain, &n->rsc_chains, next) { 25132974e916SYuri Benditovich if (chain->proto == proto) { 25142974e916SYuri Benditovich return chain; 25152974e916SYuri Benditovich } 25162974e916SYuri Benditovich } 25172974e916SYuri Benditovich 25182974e916SYuri Benditovich chain = g_malloc(sizeof(*chain)); 25192974e916SYuri Benditovich chain->n = n; 25202974e916SYuri Benditovich chain->proto = proto; 25212974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP) { 25222974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD; 25232974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 25242974e916SYuri Benditovich } else { 25252974e916SYuri Benditovich chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD; 25262974e916SYuri Benditovich chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 25272974e916SYuri Benditovich } 25282974e916SYuri Benditovich chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST, 25292974e916SYuri Benditovich virtio_net_rsc_purge, chain); 25302974e916SYuri Benditovich memset(&chain->stat, 0, sizeof(chain->stat)); 25312974e916SYuri Benditovich 25322974e916SYuri Benditovich QTAILQ_INIT(&chain->buffers); 25332974e916SYuri Benditovich QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next); 25342974e916SYuri Benditovich 25352974e916SYuri Benditovich return chain; 25362974e916SYuri Benditovich } 25372974e916SYuri Benditovich 25382974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc, 25392974e916SYuri Benditovich const uint8_t *buf, 25402974e916SYuri Benditovich size_t size) 25412974e916SYuri Benditovich { 25422974e916SYuri Benditovich uint16_t proto; 25432974e916SYuri Benditovich VirtioNetRscChain *chain; 25442974e916SYuri Benditovich struct eth_header *eth; 25452974e916SYuri Benditovich VirtIONet *n; 25462974e916SYuri Benditovich 25472974e916SYuri Benditovich n = qemu_get_nic_opaque(nc); 25482974e916SYuri Benditovich if (size < (n->host_hdr_len + sizeof(struct eth_header))) { 25492974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25502974e916SYuri Benditovich } 25512974e916SYuri Benditovich 25522974e916SYuri Benditovich eth = (struct eth_header *)(buf + n->guest_hdr_len); 25532974e916SYuri Benditovich proto = htons(eth->h_proto); 25542974e916SYuri Benditovich 25552974e916SYuri Benditovich chain = virtio_net_rsc_lookup_chain(n, nc, proto); 25562974e916SYuri Benditovich if (chain) { 25572974e916SYuri Benditovich chain->stat.received++; 25582974e916SYuri Benditovich if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) { 25592974e916SYuri Benditovich return virtio_net_rsc_receive4(chain, nc, buf, size); 25602974e916SYuri Benditovich } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) { 25612974e916SYuri Benditovich return virtio_net_rsc_receive6(chain, nc, buf, size); 25622974e916SYuri Benditovich } 25632974e916SYuri Benditovich } 25642974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25652974e916SYuri Benditovich } 25662974e916SYuri Benditovich 25672974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 25682974e916SYuri Benditovich size_t size) 25692974e916SYuri Benditovich { 25702974e916SYuri Benditovich VirtIONet *n = qemu_get_nic_opaque(nc); 25712974e916SYuri Benditovich if ((n->rsc4_enabled || n->rsc6_enabled)) { 25722974e916SYuri Benditovich return virtio_net_rsc_receive(nc, buf, size); 25732974e916SYuri Benditovich } else { 25742974e916SYuri Benditovich return virtio_net_do_receive(nc, buf, size); 25752974e916SYuri Benditovich } 25762974e916SYuri Benditovich } 25772974e916SYuri Benditovich 25786e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 25796e790746SPaolo Bonzini 25806e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 25816e790746SPaolo Bonzini { 25826e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 25836e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 258417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2585df8d0708SLaurent Vivier int ret; 25866e790746SPaolo Bonzini 258751b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 258817a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 25896e790746SPaolo Bonzini 259051b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 259151b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 25926e790746SPaolo Bonzini 25936e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2594df8d0708SLaurent Vivier ret = virtio_net_flush_tx(q); 25957550a822SLaurent Vivier if (ret >= n->tx_burst) { 2596df8d0708SLaurent Vivier /* 2597df8d0708SLaurent Vivier * the flush has been stopped by tx_burst 2598df8d0708SLaurent Vivier * we will not receive notification for the 2599df8d0708SLaurent Vivier * remainining part, so re-schedule 2600df8d0708SLaurent Vivier */ 2601df8d0708SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 26027550a822SLaurent Vivier if (q->tx_bh) { 2603df8d0708SLaurent Vivier qemu_bh_schedule(q->tx_bh); 26047550a822SLaurent Vivier } else { 26057550a822SLaurent Vivier timer_mod(q->tx_timer, 26067550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 26077550a822SLaurent Vivier } 2608df8d0708SLaurent Vivier q->tx_waiting = 1; 2609df8d0708SLaurent Vivier } 26106e790746SPaolo Bonzini } 26116e790746SPaolo Bonzini 26126e790746SPaolo Bonzini /* TX */ 26136e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 26146e790746SPaolo Bonzini { 26156e790746SPaolo Bonzini VirtIONet *n = q->n; 261617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 261751b19ebeSPaolo Bonzini VirtQueueElement *elem; 26186e790746SPaolo Bonzini int32_t num_packets = 0; 26196e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 262017a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 26216e790746SPaolo Bonzini return num_packets; 26226e790746SPaolo Bonzini } 26236e790746SPaolo Bonzini 262451b19ebeSPaolo Bonzini if (q->async_tx.elem) { 26256e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26266e790746SPaolo Bonzini return num_packets; 26276e790746SPaolo Bonzini } 26286e790746SPaolo Bonzini 262951b19ebeSPaolo Bonzini for (;;) { 2630bd89dd98SJason Wang ssize_t ret; 263151b19ebeSPaolo Bonzini unsigned int out_num; 263251b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 2633feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 26346e790746SPaolo Bonzini 263551b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 263651b19ebeSPaolo Bonzini if (!elem) { 263751b19ebeSPaolo Bonzini break; 263851b19ebeSPaolo Bonzini } 263951b19ebeSPaolo Bonzini 264051b19ebeSPaolo Bonzini out_num = elem->out_num; 264151b19ebeSPaolo Bonzini out_sg = elem->out_sg; 26426e790746SPaolo Bonzini if (out_num < 1) { 2643fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 2644fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2645fa5e56c2SGreg Kurz g_free(elem); 2646fa5e56c2SGreg Kurz return -EINVAL; 26476e790746SPaolo Bonzini } 26486e790746SPaolo Bonzini 2649032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 2650feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 2651feb93f36SJason Wang n->guest_hdr_len) { 2652fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 2653fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 2654fa5e56c2SGreg Kurz g_free(elem); 2655fa5e56c2SGreg Kurz return -EINVAL; 2656032a74a1SCédric Le Goater } 26571bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 2658feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 2659feb93f36SJason Wang sg2[0].iov_base = &mhdr; 2660feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 2661feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 2662feb93f36SJason Wang out_sg, out_num, 2663feb93f36SJason Wang n->guest_hdr_len, -1); 2664feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 2665feb93f36SJason Wang goto drop; 2666032a74a1SCédric Le Goater } 2667feb93f36SJason Wang out_num += 1; 2668feb93f36SJason Wang out_sg = sg2; 2669feb93f36SJason Wang } 2670feb93f36SJason Wang } 26716e790746SPaolo Bonzini /* 26726e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 26736e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 26746e790746SPaolo Bonzini * that host is interested in. 26756e790746SPaolo Bonzini */ 26766e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 26776e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 26786e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 26796e790746SPaolo Bonzini out_sg, out_num, 26806e790746SPaolo Bonzini 0, n->host_hdr_len); 26816e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 26826e790746SPaolo Bonzini out_sg, out_num, 26836e790746SPaolo Bonzini n->guest_hdr_len, -1); 26846e790746SPaolo Bonzini out_num = sg_num; 26856e790746SPaolo Bonzini out_sg = sg; 26866e790746SPaolo Bonzini } 26876e790746SPaolo Bonzini 26886e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 26896e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 26906e790746SPaolo Bonzini if (ret == 0) { 26916e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 26926e790746SPaolo Bonzini q->async_tx.elem = elem; 26936e790746SPaolo Bonzini return -EBUSY; 26946e790746SPaolo Bonzini } 26956e790746SPaolo Bonzini 2696feb93f36SJason Wang drop: 269751b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 269817a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 269951b19ebeSPaolo Bonzini g_free(elem); 27006e790746SPaolo Bonzini 27016e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 27026e790746SPaolo Bonzini break; 27036e790746SPaolo Bonzini } 27046e790746SPaolo Bonzini } 27056e790746SPaolo Bonzini return num_packets; 27066e790746SPaolo Bonzini } 27076e790746SPaolo Bonzini 27087550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque); 27097550a822SLaurent Vivier 27106e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 27116e790746SPaolo Bonzini { 271217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 27136e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 27146e790746SPaolo Bonzini 2715283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2716283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2717283e2c2aSYuri Benditovich return; 2718283e2c2aSYuri Benditovich } 2719283e2c2aSYuri Benditovich 27206e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 272117a0ca55SKONRAD Frederic if (!vdev->vm_running) { 27226e790746SPaolo Bonzini q->tx_waiting = 1; 27236e790746SPaolo Bonzini return; 27246e790746SPaolo Bonzini } 27256e790746SPaolo Bonzini 27266e790746SPaolo Bonzini if (q->tx_waiting) { 27277550a822SLaurent Vivier /* We already have queued packets, immediately flush */ 2728bc72ad67SAlex Bligh timer_del(q->tx_timer); 27297550a822SLaurent Vivier virtio_net_tx_timer(q); 27306e790746SPaolo Bonzini } else { 27317550a822SLaurent Vivier /* re-arm timer to flush it (and more) on next tick */ 2732bc72ad67SAlex Bligh timer_mod(q->tx_timer, 2733bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27346e790746SPaolo Bonzini q->tx_waiting = 1; 27356e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 27366e790746SPaolo Bonzini } 27376e790746SPaolo Bonzini } 27386e790746SPaolo Bonzini 27396e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 27406e790746SPaolo Bonzini { 274117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 27426e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 27436e790746SPaolo Bonzini 2744283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 2745283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 2746283e2c2aSYuri Benditovich return; 2747283e2c2aSYuri Benditovich } 2748283e2c2aSYuri Benditovich 27496e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 27506e790746SPaolo Bonzini return; 27516e790746SPaolo Bonzini } 27526e790746SPaolo Bonzini q->tx_waiting = 1; 27536e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 275417a0ca55SKONRAD Frederic if (!vdev->vm_running) { 27556e790746SPaolo Bonzini return; 27566e790746SPaolo Bonzini } 27576e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 27586e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 27596e790746SPaolo Bonzini } 27606e790746SPaolo Bonzini 27616e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 27626e790746SPaolo Bonzini { 27636e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 27646e790746SPaolo Bonzini VirtIONet *n = q->n; 276517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 27667550a822SLaurent Vivier int ret; 27677550a822SLaurent Vivier 2768e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2769e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2770e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2771e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2772e8bcf842SMichael S. Tsirkin return; 2773e8bcf842SMichael S. Tsirkin } 27746e790746SPaolo Bonzini 27756e790746SPaolo Bonzini q->tx_waiting = 0; 27766e790746SPaolo Bonzini 27776e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 277817a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 27796e790746SPaolo Bonzini return; 278017a0ca55SKONRAD Frederic } 27816e790746SPaolo Bonzini 27827550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 27837550a822SLaurent Vivier if (ret == -EBUSY || ret == -EINVAL) { 27847550a822SLaurent Vivier return; 27857550a822SLaurent Vivier } 27867550a822SLaurent Vivier /* 27877550a822SLaurent Vivier * If we flush a full burst of packets, assume there are 27887550a822SLaurent Vivier * more coming and immediately rearm 27897550a822SLaurent Vivier */ 27907550a822SLaurent Vivier if (ret >= n->tx_burst) { 27917550a822SLaurent Vivier q->tx_waiting = 1; 27927550a822SLaurent Vivier timer_mod(q->tx_timer, 27937550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 27947550a822SLaurent Vivier return; 27957550a822SLaurent Vivier } 27967550a822SLaurent Vivier /* 27977550a822SLaurent Vivier * If less than a full burst, re-enable notification and flush 27987550a822SLaurent Vivier * anything that may have come in while we weren't looking. If 27997550a822SLaurent Vivier * we find something, assume the guest is still active and rearm 28007550a822SLaurent Vivier */ 28016e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 28027550a822SLaurent Vivier ret = virtio_net_flush_tx(q); 28037550a822SLaurent Vivier if (ret > 0) { 28047550a822SLaurent Vivier virtio_queue_set_notification(q->tx_vq, 0); 28057550a822SLaurent Vivier q->tx_waiting = 1; 28067550a822SLaurent Vivier timer_mod(q->tx_timer, 28077550a822SLaurent Vivier qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 28087550a822SLaurent Vivier } 28096e790746SPaolo Bonzini } 28106e790746SPaolo Bonzini 28116e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 28126e790746SPaolo Bonzini { 28136e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 28146e790746SPaolo Bonzini VirtIONet *n = q->n; 281517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 28166e790746SPaolo Bonzini int32_t ret; 28176e790746SPaolo Bonzini 2818e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 2819e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 2820e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 2821e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 2822e8bcf842SMichael S. Tsirkin return; 2823e8bcf842SMichael S. Tsirkin } 28246e790746SPaolo Bonzini 28256e790746SPaolo Bonzini q->tx_waiting = 0; 28266e790746SPaolo Bonzini 28276e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 282817a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 28296e790746SPaolo Bonzini return; 283017a0ca55SKONRAD Frederic } 28316e790746SPaolo Bonzini 28326e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 2833fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 2834fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 2835fa5e56c2SGreg Kurz * broken */ 28366e790746SPaolo Bonzini } 28376e790746SPaolo Bonzini 28386e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 28396e790746SPaolo Bonzini * more coming and immediately reschedule */ 28406e790746SPaolo Bonzini if (ret >= n->tx_burst) { 28416e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28426e790746SPaolo Bonzini q->tx_waiting = 1; 28436e790746SPaolo Bonzini return; 28446e790746SPaolo Bonzini } 28456e790746SPaolo Bonzini 28466e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 28476e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 28486e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 28496e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 2850fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 2851fa5e56c2SGreg Kurz if (ret == -EINVAL) { 2852fa5e56c2SGreg Kurz return; 2853fa5e56c2SGreg Kurz } else if (ret > 0) { 28546e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 28556e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 28566e790746SPaolo Bonzini q->tx_waiting = 1; 28576e790746SPaolo Bonzini } 28586e790746SPaolo Bonzini } 28596e790746SPaolo Bonzini 2860f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 2861f9d6dbf0SWen Congyang { 2862f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2863f9d6dbf0SWen Congyang 28641c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 28651c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 28669b02e161SWei Wang 2867f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 2868f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 28699b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 28709b02e161SWei Wang virtio_net_handle_tx_timer); 2871f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 2872f9d6dbf0SWen Congyang virtio_net_tx_timer, 2873f9d6dbf0SWen Congyang &n->vqs[index]); 2874f9d6dbf0SWen Congyang } else { 2875f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 28769b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 28779b02e161SWei Wang virtio_net_handle_tx_bh); 2878f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 2879f9d6dbf0SWen Congyang } 2880f9d6dbf0SWen Congyang 2881f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 2882f9d6dbf0SWen Congyang n->vqs[index].n = n; 2883f9d6dbf0SWen Congyang } 2884f9d6dbf0SWen Congyang 2885f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 2886f9d6dbf0SWen Congyang { 2887f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2888f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 2889f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 2890f9d6dbf0SWen Congyang 2891f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 2892f9d6dbf0SWen Congyang 2893f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 2894f9d6dbf0SWen Congyang if (q->tx_timer) { 2895f9d6dbf0SWen Congyang timer_free(q->tx_timer); 2896f989c30cSYunjian Wang q->tx_timer = NULL; 2897f9d6dbf0SWen Congyang } else { 2898f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 2899f989c30cSYunjian Wang q->tx_bh = NULL; 2900f9d6dbf0SWen Congyang } 2901f989c30cSYunjian Wang q->tx_waiting = 0; 2902f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 2903f9d6dbf0SWen Congyang } 2904f9d6dbf0SWen Congyang 2905441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs) 2906f9d6dbf0SWen Congyang { 2907f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 2908f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 2909441537f1SJason Wang int new_num_queues = new_max_queue_pairs * 2 + 1; 2910f9d6dbf0SWen Congyang int i; 2911f9d6dbf0SWen Congyang 2912f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 2913f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 2914f9d6dbf0SWen Congyang 2915f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 2916f9d6dbf0SWen Congyang return; 2917f9d6dbf0SWen Congyang } 2918f9d6dbf0SWen Congyang 2919f9d6dbf0SWen Congyang /* 2920f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 2921f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 292220f86a75SYuval Shaia * and then we only enter one of the following two loops. 2923f9d6dbf0SWen Congyang */ 2924f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 2925f9d6dbf0SWen Congyang 2926f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 2927f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 2928f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 2929f9d6dbf0SWen Congyang } 2930f9d6dbf0SWen Congyang 2931f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 2932f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 2933f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 2934f9d6dbf0SWen Congyang } 2935f9d6dbf0SWen Congyang 2936f9d6dbf0SWen Congyang /* add ctrl_vq last */ 2937f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 2938f9d6dbf0SWen Congyang } 2939f9d6dbf0SWen Congyang 2940ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 29416e790746SPaolo Bonzini { 2942441537f1SJason Wang int max = multiqueue ? n->max_queue_pairs : 1; 2943f9d6dbf0SWen Congyang 29446e790746SPaolo Bonzini n->multiqueue = multiqueue; 2945441537f1SJason Wang virtio_net_change_num_queue_pairs(n, max); 29466e790746SPaolo Bonzini 2947441537f1SJason Wang virtio_net_set_queue_pairs(n); 29486e790746SPaolo Bonzini } 29496e790746SPaolo Bonzini 2950982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 2951037dab2fSGreg Kurz { 2952982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 2953982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 2954037dab2fSGreg Kurz int i, link_down; 2955037dab2fSGreg Kurz 29569d8c6a25SDr. David Alan Gilbert trace_virtio_net_post_load_device(); 2957982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 295895129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 2959e22f0603SYuri Benditovich VIRTIO_F_VERSION_1), 2960e22f0603SYuri Benditovich virtio_vdev_has_feature(vdev, 2961e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT)); 29626e790746SPaolo Bonzini 29636e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 2964982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 29656e790746SPaolo Bonzini n->mac_table.in_use = 0; 29666e790746SPaolo Bonzini } 29676e790746SPaolo Bonzini 2968982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 29696c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 29706c666823SMichael S. Tsirkin } 29716c666823SMichael S. Tsirkin 29727788c3f2SMikhail Sennikovsky /* 29737788c3f2SMikhail Sennikovsky * curr_guest_offloads will be later overwritten by the 29747788c3f2SMikhail Sennikovsky * virtio_set_features_nocheck call done from the virtio_load. 29757788c3f2SMikhail Sennikovsky * Here we make sure it is preserved and restored accordingly 29767788c3f2SMikhail Sennikovsky * in the virtio_net_post_load_virtio callback. 29777788c3f2SMikhail Sennikovsky */ 29787788c3f2SMikhail Sennikovsky n->saved_guest_offloads = n->curr_guest_offloads; 29796c666823SMichael S. Tsirkin 2980441537f1SJason Wang virtio_net_set_queue_pairs(n); 29816e790746SPaolo Bonzini 29826e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 29836e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 29846e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 29856e790746SPaolo Bonzini break; 29866e790746SPaolo Bonzini } 29876e790746SPaolo Bonzini } 29886e790746SPaolo Bonzini n->mac_table.first_multi = i; 29896e790746SPaolo Bonzini 29906e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 29916e790746SPaolo Bonzini * to link status bit in n->status */ 29926e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 2993441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 29946e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 29956e790746SPaolo Bonzini } 29966e790746SPaolo Bonzini 29976c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 29986c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 29999d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 30009d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 30019d8c6a25SDr. David Alan Gilbert virtio_net_announce_timer, n); 30029d8c6a25SDr. David Alan Gilbert if (n->announce_timer.round) { 30039d8c6a25SDr. David Alan Gilbert timer_mod(n->announce_timer.tm, 30049d8c6a25SDr. David Alan Gilbert qemu_clock_get_ms(n->announce_timer.type)); 30059d8c6a25SDr. David Alan Gilbert } else { 3006944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 30079d8c6a25SDr. David Alan Gilbert } 30086c666823SMichael S. Tsirkin } 30096c666823SMichael S. Tsirkin 3010e41b7114SYuri Benditovich if (n->rss_data.enabled) { 30110145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = n->rss_data.populate_hash; 30120145c393SAndrew Melnychenko if (!n->rss_data.populate_hash) { 30130145c393SAndrew Melnychenko if (!virtio_net_attach_epbf_rss(n)) { 30140145c393SAndrew Melnychenko if (get_vhost_net(qemu_get_queue(n->nic)->peer)) { 30150145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS for vhost"); 30160145c393SAndrew Melnychenko } else { 30170145c393SAndrew Melnychenko warn_report("Can't post-load eBPF RSS - " 30180145c393SAndrew Melnychenko "fallback to software RSS"); 30190145c393SAndrew Melnychenko n->rss_data.enabled_software_rss = true; 30200145c393SAndrew Melnychenko } 30210145c393SAndrew Melnychenko } 30220145c393SAndrew Melnychenko } 30230145c393SAndrew Melnychenko 3024e41b7114SYuri Benditovich trace_virtio_net_rss_enable(n->rss_data.hash_types, 3025e41b7114SYuri Benditovich n->rss_data.indirections_len, 3026e41b7114SYuri Benditovich sizeof(n->rss_data.key)); 3027e41b7114SYuri Benditovich } else { 3028e41b7114SYuri Benditovich trace_virtio_net_rss_disable(); 3029e41b7114SYuri Benditovich } 30306e790746SPaolo Bonzini return 0; 30316e790746SPaolo Bonzini } 30326e790746SPaolo Bonzini 30337788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev) 30347788c3f2SMikhail Sennikovsky { 30357788c3f2SMikhail Sennikovsky VirtIONet *n = VIRTIO_NET(vdev); 30367788c3f2SMikhail Sennikovsky /* 30377788c3f2SMikhail Sennikovsky * The actual needed state is now in saved_guest_offloads, 30387788c3f2SMikhail Sennikovsky * see virtio_net_post_load_device for detail. 30397788c3f2SMikhail Sennikovsky * Restore it back and apply the desired offloads. 30407788c3f2SMikhail Sennikovsky */ 30417788c3f2SMikhail Sennikovsky n->curr_guest_offloads = n->saved_guest_offloads; 30427788c3f2SMikhail Sennikovsky if (peer_has_vnet_hdr(n)) { 30437788c3f2SMikhail Sennikovsky virtio_net_apply_guest_offloads(n); 30447788c3f2SMikhail Sennikovsky } 30457788c3f2SMikhail Sennikovsky 30467788c3f2SMikhail Sennikovsky return 0; 30477788c3f2SMikhail Sennikovsky } 30487788c3f2SMikhail Sennikovsky 3049982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 3050982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 3051982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 3052982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3053982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 3054982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3055982b78c5SDr. David Alan Gilbert }, 3056982b78c5SDr. David Alan Gilbert }; 3057982b78c5SDr. David Alan Gilbert 3058441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id) 3059982b78c5SDr. David Alan Gilbert { 3060441537f1SJason Wang return VIRTIO_NET(opaque)->max_queue_pairs > 1; 3061982b78c5SDr. David Alan Gilbert } 3062982b78c5SDr. David Alan Gilbert 3063982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 3064982b78c5SDr. David Alan Gilbert { 3065982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 3066982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 3067982b78c5SDr. David Alan Gilbert } 3068982b78c5SDr. David Alan Gilbert 3069982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 3070982b78c5SDr. David Alan Gilbert { 3071982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 3072982b78c5SDr. David Alan Gilbert } 3073982b78c5SDr. David Alan Gilbert 3074982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 3075982b78c5SDr. David Alan Gilbert { 3076982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 3077982b78c5SDr. David Alan Gilbert } 3078982b78c5SDr. David Alan Gilbert 3079982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 3080982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 3081982b78c5SDr. David Alan Gilbert */ 3082982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 3083982b78c5SDr. David Alan Gilbert VirtIONet *parent; 3084982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 3085441537f1SJason Wang uint16_t curr_queue_pairs_1; 3086982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 3087982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 3088982b78c5SDr. David Alan Gilbert }; 3089982b78c5SDr. David Alan Gilbert 3090982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 3091441537f1SJason Wang * the 1st entry in the queue_pairs and only if there's more than one 3092982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 3093982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 3094982b78c5SDr. David Alan Gilbert */ 3095982b78c5SDr. David Alan Gilbert 309644b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 3097982b78c5SDr. David Alan Gilbert { 3098982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3099982b78c5SDr. David Alan Gilbert 3100982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 3101441537f1SJason Wang tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1; 3102441537f1SJason Wang if (tmp->parent->curr_queue_pairs == 0) { 3103441537f1SJason Wang tmp->curr_queue_pairs_1 = 0; 3104982b78c5SDr. David Alan Gilbert } 310544b1ff31SDr. David Alan Gilbert 310644b1ff31SDr. David Alan Gilbert return 0; 3107982b78c5SDr. David Alan Gilbert } 3108982b78c5SDr. David Alan Gilbert 3109982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 3110982b78c5SDr. David Alan Gilbert { 3111982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3112982b78c5SDr. David Alan Gilbert 3113982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 3114982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 3115982b78c5SDr. David Alan Gilbert 3116441537f1SJason Wang if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) { 3117441537f1SJason Wang error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x", 3118441537f1SJason Wang tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs); 3119982b78c5SDr. David Alan Gilbert 3120982b78c5SDr. David Alan Gilbert return -EINVAL; 3121982b78c5SDr. David Alan Gilbert } 3122982b78c5SDr. David Alan Gilbert 3123982b78c5SDr. David Alan Gilbert return 0; /* all good */ 3124982b78c5SDr. David Alan Gilbert } 3125982b78c5SDr. David Alan Gilbert 3126982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 3127982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 3128982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 3129982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 3130982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3131982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 3132441537f1SJason Wang curr_queue_pairs_1, 3133982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3134982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 3135982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3136982b78c5SDr. David Alan Gilbert }, 3137982b78c5SDr. David Alan Gilbert }; 3138982b78c5SDr. David Alan Gilbert 3139982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 3140982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3141982b78c5SDr. David Alan Gilbert */ 3142982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 3143982b78c5SDr. David Alan Gilbert { 3144982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3145982b78c5SDr. David Alan Gilbert 3146982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 3147982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 3148982b78c5SDr. David Alan Gilbert return -EINVAL; 3149982b78c5SDr. David Alan Gilbert } 3150982b78c5SDr. David Alan Gilbert 3151982b78c5SDr. David Alan Gilbert return 0; 3152982b78c5SDr. David Alan Gilbert } 3153982b78c5SDr. David Alan Gilbert 315444b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 3155982b78c5SDr. David Alan Gilbert { 3156982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3157982b78c5SDr. David Alan Gilbert 3158982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 315944b1ff31SDr. David Alan Gilbert 316044b1ff31SDr. David Alan Gilbert return 0; 3161982b78c5SDr. David Alan Gilbert } 3162982b78c5SDr. David Alan Gilbert 3163982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 3164982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 3165982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 3166982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 3167982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3168982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 3169982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3170982b78c5SDr. David Alan Gilbert }, 3171982b78c5SDr. David Alan Gilbert }; 3172982b78c5SDr. David Alan Gilbert 3173982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 3174982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 3175982b78c5SDr. David Alan Gilbert */ 3176982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 3177982b78c5SDr. David Alan Gilbert { 3178982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3179982b78c5SDr. David Alan Gilbert 3180982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 3181982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 3182982b78c5SDr. David Alan Gilbert return -EINVAL; 3183982b78c5SDr. David Alan Gilbert } 3184982b78c5SDr. David Alan Gilbert 3185982b78c5SDr. David Alan Gilbert return 0; 3186982b78c5SDr. David Alan Gilbert } 3187982b78c5SDr. David Alan Gilbert 318844b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 3189982b78c5SDr. David Alan Gilbert { 3190982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 3191982b78c5SDr. David Alan Gilbert 3192982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 319344b1ff31SDr. David Alan Gilbert 319444b1ff31SDr. David Alan Gilbert return 0; 3195982b78c5SDr. David Alan Gilbert } 3196982b78c5SDr. David Alan Gilbert 3197982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 3198982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 3199982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 3200982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 3201982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3202982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 3203982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3204982b78c5SDr. David Alan Gilbert }, 3205982b78c5SDr. David Alan Gilbert }; 3206982b78c5SDr. David Alan Gilbert 3207e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque) 3208e41b7114SYuri Benditovich { 3209e41b7114SYuri Benditovich return VIRTIO_NET(opaque)->rss_data.enabled; 3210e41b7114SYuri Benditovich } 3211e41b7114SYuri Benditovich 3212e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = { 3213e41b7114SYuri Benditovich .name = "virtio-net-device/rss", 3214e41b7114SYuri Benditovich .version_id = 1, 3215e41b7114SYuri Benditovich .minimum_version_id = 1, 3216e41b7114SYuri Benditovich .needed = virtio_net_rss_needed, 3217e41b7114SYuri Benditovich .fields = (VMStateField[]) { 3218e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.enabled, VirtIONet), 3219e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.redirect, VirtIONet), 3220e41b7114SYuri Benditovich VMSTATE_BOOL(rss_data.populate_hash, VirtIONet), 3221e41b7114SYuri Benditovich VMSTATE_UINT32(rss_data.hash_types, VirtIONet), 3222e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.indirections_len, VirtIONet), 3223e41b7114SYuri Benditovich VMSTATE_UINT16(rss_data.default_queue, VirtIONet), 3224e41b7114SYuri Benditovich VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet, 3225e41b7114SYuri Benditovich VIRTIO_NET_RSS_MAX_KEY_SIZE), 3226e41b7114SYuri Benditovich VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet, 3227e41b7114SYuri Benditovich rss_data.indirections_len, 0, 3228e41b7114SYuri Benditovich vmstate_info_uint16, uint16_t), 3229e41b7114SYuri Benditovich VMSTATE_END_OF_LIST() 3230e41b7114SYuri Benditovich }, 3231e41b7114SYuri Benditovich }; 3232e41b7114SYuri Benditovich 3233982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 3234982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 3235982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 3236982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 3237982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 3238982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 3239982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 3240982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 3241982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 3242982b78c5SDr. David Alan Gilbert VirtIONetQueue), 3243982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 3244982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 3245982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 3246982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 3247982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 3248982b78c5SDr. David Alan Gilbert 3249982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 3250982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 3251982b78c5SDr. David Alan Gilbert * sets flags in this case. 3252982b78c5SDr. David Alan Gilbert */ 3253982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 3254982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 3255982b78c5SDr. David Alan Gilbert ETH_ALEN), 3256982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 3257982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 3258982b78c5SDr. David Alan Gilbert 3259982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 3260982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 3261982b78c5SDr. David Alan Gilbert * but based on the uint. 3262982b78c5SDr. David Alan Gilbert */ 3263982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 3264982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3265982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 3266982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 3267982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 3268982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 3269982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 3270982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 3271982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 3272982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3273982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 3274441537f1SJason Wang VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0, 3275982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 3276441537f1SJason Wang VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1), 3277982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 3278982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 3279982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 3280982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 3281982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 3282982b78c5SDr. David Alan Gilbert }, 3283e41b7114SYuri Benditovich .subsections = (const VMStateDescription * []) { 3284e41b7114SYuri Benditovich &vmstate_virtio_net_rss, 3285e41b7114SYuri Benditovich NULL 3286e41b7114SYuri Benditovich } 3287982b78c5SDr. David Alan Gilbert }; 3288982b78c5SDr. David Alan Gilbert 32896e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 3290f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 32916e790746SPaolo Bonzini .size = sizeof(NICState), 32926e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 32936e790746SPaolo Bonzini .receive = virtio_net_receive, 32946e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 3295b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 3296b2c929f0SDr. David Alan Gilbert .announce = virtio_net_announce, 32976e790746SPaolo Bonzini }; 32986e790746SPaolo Bonzini 32996e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 33006e790746SPaolo Bonzini { 330117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 330268b0a639SSi-Wei Liu NetClientState *nc; 33036e790746SPaolo Bonzini assert(n->vhost_started); 330468b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 330568b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 330668b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 330768b0a639SSi-Wei Liu * buggy migration stream. 330868b0a639SSi-Wei Liu */ 330968b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 331068b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 331168b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 331268b0a639SSi-Wei Liu return false; 331368b0a639SSi-Wei Liu } 331468b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 331568b0a639SSi-Wei Liu } else { 331668b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 331768b0a639SSi-Wei Liu } 3318ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 33196e790746SPaolo Bonzini } 33206e790746SPaolo Bonzini 33216e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 33226e790746SPaolo Bonzini bool mask) 33236e790746SPaolo Bonzini { 332417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 332568b0a639SSi-Wei Liu NetClientState *nc; 33266e790746SPaolo Bonzini assert(n->vhost_started); 332768b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) { 332868b0a639SSi-Wei Liu /* Must guard against invalid features and bogus queue index 332968b0a639SSi-Wei Liu * from being set by malicious guest, or penetrated through 333068b0a639SSi-Wei Liu * buggy migration stream. 333168b0a639SSi-Wei Liu */ 333268b0a639SSi-Wei Liu if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 333368b0a639SSi-Wei Liu qemu_log_mask(LOG_GUEST_ERROR, 333468b0a639SSi-Wei Liu "%s: bogus vq index ignored\n", __func__); 333568b0a639SSi-Wei Liu return; 333668b0a639SSi-Wei Liu } 333768b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, n->max_queue_pairs); 333868b0a639SSi-Wei Liu } else { 333968b0a639SSi-Wei Liu nc = qemu_get_subqueue(n->nic, vq2q(idx)); 334068b0a639SSi-Wei Liu } 3341a882b571SMichael S. Tsirkin vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 3342a882b571SMichael S. Tsirkin vdev, idx, mask); 33436e790746SPaolo Bonzini } 33446e790746SPaolo Bonzini 3345019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 33466e790746SPaolo Bonzini { 33470cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 3348a93e599dSMaxime Coquelin 3349d74c30c8SDaniil Tatianin n->config_size = virtio_get_config_size(&cfg_size_params, host_features); 335017ec5a86SKONRAD Frederic } 33516e790746SPaolo Bonzini 33528a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 33538a253ec2SKONRAD Frederic const char *type) 33548a253ec2SKONRAD Frederic { 33558a253ec2SKONRAD Frederic /* 33568a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 33578a253ec2SKONRAD Frederic */ 33588a253ec2SKONRAD Frederic assert(type != NULL); 33598a253ec2SKONRAD Frederic 33608a253ec2SKONRAD Frederic g_free(n->netclient_name); 33618a253ec2SKONRAD Frederic g_free(n->netclient_type); 33628a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 33638a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 33648a253ec2SKONRAD Frederic } 33658a253ec2SKONRAD Frederic 33660e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev) 33679711cd0dSJens Freimann { 33689711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 33699711cd0dSJens Freimann PCIDevice *pci_dev; 33709711cd0dSJens Freimann Error *err = NULL; 33719711cd0dSJens Freimann 33720e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 33739711cd0dSJens Freimann if (hotplug_ctrl) { 33740e9a65c5SJuan Quintela pci_dev = PCI_DEVICE(dev); 33759711cd0dSJens Freimann pci_dev->partially_hotplugged = true; 33760e9a65c5SJuan Quintela hotplug_handler_unplug_request(hotplug_ctrl, dev, &err); 33779711cd0dSJens Freimann if (err) { 33789711cd0dSJens Freimann error_report_err(err); 33799711cd0dSJens Freimann return false; 33809711cd0dSJens Freimann } 33819711cd0dSJens Freimann } else { 33829711cd0dSJens Freimann return false; 33839711cd0dSJens Freimann } 33849711cd0dSJens Freimann return true; 33859711cd0dSJens Freimann } 33869711cd0dSJens Freimann 33870e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev, 33880e9a65c5SJuan Quintela Error **errp) 33899711cd0dSJens Freimann { 33905a0948d3SMarkus Armbruster Error *err = NULL; 33919711cd0dSJens Freimann HotplugHandler *hotplug_ctrl; 33920e9a65c5SJuan Quintela PCIDevice *pdev = PCI_DEVICE(dev); 339378274682SJuan Quintela BusState *primary_bus; 33949711cd0dSJens Freimann 33959711cd0dSJens Freimann if (!pdev->partially_hotplugged) { 33969711cd0dSJens Freimann return true; 33979711cd0dSJens Freimann } 33980e9a65c5SJuan Quintela primary_bus = dev->parent_bus; 339978274682SJuan Quintela if (!primary_bus) { 3400150ab54aSJens Freimann error_setg(errp, "virtio_net: couldn't find primary bus"); 34015a0948d3SMarkus Armbruster return false; 34029711cd0dSJens Freimann } 34030e9a65c5SJuan Quintela qdev_set_parent_bus(dev, primary_bus, &error_abort); 3404e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, false); 34050e9a65c5SJuan Quintela hotplug_ctrl = qdev_get_hotplug_handler(dev); 34069711cd0dSJens Freimann if (hotplug_ctrl) { 34070e9a65c5SJuan Quintela hotplug_handler_pre_plug(hotplug_ctrl, dev, &err); 34085a0948d3SMarkus Armbruster if (err) { 34095a0948d3SMarkus Armbruster goto out; 34105a0948d3SMarkus Armbruster } 34110e9a65c5SJuan Quintela hotplug_handler_plug(hotplug_ctrl, dev, &err); 34129711cd0dSJens Freimann } 3413109c20eaSLaurent Vivier pdev->partially_hotplugged = false; 3414150ab54aSJens Freimann 3415150ab54aSJens Freimann out: 34165a0948d3SMarkus Armbruster error_propagate(errp, err); 34175a0948d3SMarkus Armbruster return !err; 34189711cd0dSJens Freimann } 34199711cd0dSJens Freimann 342007a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s) 34219711cd0dSJens Freimann { 34229711cd0dSJens Freimann bool should_be_hidden; 34239711cd0dSJens Freimann Error *err = NULL; 342407a5d816SJuan Quintela DeviceState *dev = failover_find_primary_device(n); 342507a5d816SJuan Quintela 342607a5d816SJuan Quintela if (!dev) { 342707a5d816SJuan Quintela return; 342807a5d816SJuan Quintela } 34299711cd0dSJens Freimann 3430e2bde83eSJuan Quintela should_be_hidden = qatomic_read(&n->failover_primary_hidden); 34319711cd0dSJens Freimann 34324dbac1aeSMarkus Armbruster if (migration_in_setup(s) && !should_be_hidden) { 343307a5d816SJuan Quintela if (failover_unplug_primary(n, dev)) { 343407a5d816SJuan Quintela vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 343507a5d816SJuan Quintela qapi_event_send_unplug_primary(dev->id); 3436e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 34379711cd0dSJens Freimann } else { 34389711cd0dSJens Freimann warn_report("couldn't unplug primary device"); 34399711cd0dSJens Freimann } 34409711cd0dSJens Freimann } else if (migration_has_failed(s)) { 3441150ab54aSJens Freimann /* We already unplugged the device let's plug it back */ 344207a5d816SJuan Quintela if (!failover_replug_primary(n, dev, &err)) { 34439711cd0dSJens Freimann if (err) { 34449711cd0dSJens Freimann error_report_err(err); 34459711cd0dSJens Freimann } 34469711cd0dSJens Freimann } 34479711cd0dSJens Freimann } 34489711cd0dSJens Freimann } 34499711cd0dSJens Freimann 34509711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) 34519711cd0dSJens Freimann { 34529711cd0dSJens Freimann MigrationState *s = data; 34539711cd0dSJens Freimann VirtIONet *n = container_of(notifier, VirtIONet, migration_state); 34549711cd0dSJens Freimann virtio_net_handle_migration_primary(n, s); 34559711cd0dSJens Freimann } 34569711cd0dSJens Freimann 3457b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener, 3458f3558b1bSKevin Wolf const QDict *device_opts, 3459f3558b1bSKevin Wolf bool from_json, 3460f3558b1bSKevin Wolf Error **errp) 34619711cd0dSJens Freimann { 34629711cd0dSJens Freimann VirtIONet *n = container_of(listener, VirtIONet, primary_listener); 34634f0303aeSJuan Quintela const char *standby_id; 34649711cd0dSJens Freimann 34654d0e59acSJens Freimann if (!device_opts) { 346689631fedSJuan Quintela return false; 34674d0e59acSJens Freimann } 3468bcfc906bSLaurent Vivier 3469bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "failover_pair_id")) { 3470bcfc906bSLaurent Vivier return false; 3471bcfc906bSLaurent Vivier } 3472bcfc906bSLaurent Vivier 3473bcfc906bSLaurent Vivier if (!qdict_haskey(device_opts, "id")) { 3474bcfc906bSLaurent Vivier error_setg(errp, "Device with failover_pair_id needs to have id"); 3475bcfc906bSLaurent Vivier return false; 3476bcfc906bSLaurent Vivier } 3477bcfc906bSLaurent Vivier 3478bcfc906bSLaurent Vivier standby_id = qdict_get_str(device_opts, "failover_pair_id"); 347989631fedSJuan Quintela if (g_strcmp0(standby_id, n->netclient_name) != 0) { 348089631fedSJuan Quintela return false; 34819711cd0dSJens Freimann } 34829711cd0dSJens Freimann 34837fe7791eSLaurent Vivier /* 34847fe7791eSLaurent Vivier * The hide helper can be called several times for a given device. 34857fe7791eSLaurent Vivier * Check there is only one primary for a virtio-net device but 34867fe7791eSLaurent Vivier * don't duplicate the qdict several times if it's called for the same 34877fe7791eSLaurent Vivier * device. 34887fe7791eSLaurent Vivier */ 3489259a10dbSKevin Wolf if (n->primary_opts) { 34907fe7791eSLaurent Vivier const char *old, *new; 34917fe7791eSLaurent Vivier /* devices with failover_pair_id always have an id */ 34927fe7791eSLaurent Vivier old = qdict_get_str(n->primary_opts, "id"); 34937fe7791eSLaurent Vivier new = qdict_get_str(device_opts, "id"); 34947fe7791eSLaurent Vivier if (strcmp(old, new) != 0) { 34957fe7791eSLaurent Vivier error_setg(errp, "Cannot attach more than one primary device to " 34967fe7791eSLaurent Vivier "'%s': '%s' and '%s'", n->netclient_name, old, new); 3497259a10dbSKevin Wolf return false; 3498259a10dbSKevin Wolf } 34997fe7791eSLaurent Vivier } else { 3500f3558b1bSKevin Wolf n->primary_opts = qdict_clone_shallow(device_opts); 3501f3558b1bSKevin Wolf n->primary_opts_from_json = from_json; 35027fe7791eSLaurent Vivier } 3503259a10dbSKevin Wolf 3504e2bde83eSJuan Quintela /* failover_primary_hidden is set during feature negotiation */ 35053abad4a2SJuan Quintela return qatomic_read(&n->failover_primary_hidden); 35069711cd0dSJens Freimann } 35079711cd0dSJens Freimann 3508e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 350917ec5a86SKONRAD Frederic { 3510e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3511284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3512284a32f0SAndreas Färber NetClientState *nc; 35131773d9eeSKONRAD Frederic int i; 351417ec5a86SKONRAD Frederic 3515a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 3516127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 3517a93e599dSMaxime Coquelin } 3518a93e599dSMaxime Coquelin 35199473939eSJason Baron if (n->net_conf.duplex_str) { 35209473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 35219473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 35229473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 35239473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 35249473939eSJason Baron } else { 35259473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 3526843c4cfcSMarkus Armbruster return; 35279473939eSJason Baron } 35289473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 35299473939eSJason Baron } else { 35309473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 35319473939eSJason Baron } 35329473939eSJason Baron 35339473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 35349473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 3535843c4cfcSMarkus Armbruster return; 3536843c4cfcSMarkus Armbruster } 3537843c4cfcSMarkus Armbruster if (n->net_conf.speed >= 0) { 35389473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 35399473939eSJason Baron } 35409473939eSJason Baron 35419711cd0dSJens Freimann if (n->failover) { 3542b91ad981SJuan Quintela n->primary_listener.hide_device = failover_hide_primary_device; 3543e2bde83eSJuan Quintela qatomic_set(&n->failover_primary_hidden, true); 35449711cd0dSJens Freimann device_listener_register(&n->primary_listener); 35459711cd0dSJens Freimann n->migration_state.notify = virtio_net_migration_state_notifier; 35469711cd0dSJens Freimann add_migration_state_change_notifier(&n->migration_state); 35479711cd0dSJens Freimann n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY); 35489711cd0dSJens Freimann } 35499711cd0dSJens Freimann 3550da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 35513857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_NET, n->config_size); 355217ec5a86SKONRAD Frederic 35531c0fbfa3SMichael S. Tsirkin /* 35541c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 35551c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 35561c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 35571c0fbfa3SMichael S. Tsirkin */ 35581c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 35591c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 35605f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 35611c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 35621c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 35631c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 35641c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 35651c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 35661c0fbfa3SMichael S. Tsirkin return; 35671c0fbfa3SMichael S. Tsirkin } 35681c0fbfa3SMichael S. Tsirkin 35699b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 35709b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 35719b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 35729b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 35739b02e161SWei Wang "must be a power of 2 between %d and %d", 35749b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 35759b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 35769b02e161SWei Wang virtio_cleanup(vdev); 35779b02e161SWei Wang return; 35789b02e161SWei Wang } 35799b02e161SWei Wang 358022288fe5SJason Wang n->max_ncs = MAX(n->nic_conf.peers.queues, 1); 358122288fe5SJason Wang 358222288fe5SJason Wang /* 358322288fe5SJason Wang * Figure out the datapath queue pairs since the backend could 358422288fe5SJason Wang * provide control queue via peers as well. 358522288fe5SJason Wang */ 358622288fe5SJason Wang if (n->nic_conf.peers.queues) { 358722288fe5SJason Wang for (i = 0; i < n->max_ncs; i++) { 358822288fe5SJason Wang if (n->nic_conf.peers.ncs[i]->is_datapath) { 358922288fe5SJason Wang ++n->max_queue_pairs; 359022288fe5SJason Wang } 359122288fe5SJason Wang } 359222288fe5SJason Wang } 359322288fe5SJason Wang n->max_queue_pairs = MAX(n->max_queue_pairs, 1); 359422288fe5SJason Wang 3595441537f1SJason Wang if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) { 359622288fe5SJason Wang error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), " 3597631b22eaSStefan Weil "must be a positive integer less than %d.", 3598441537f1SJason Wang n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2); 35997e0e736eSJason Wang virtio_cleanup(vdev); 36007e0e736eSJason Wang return; 36017e0e736eSJason Wang } 3602b21e2380SMarkus Armbruster n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs); 3603441537f1SJason Wang n->curr_queue_pairs = 1; 36041773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 36056e790746SPaolo Bonzini 36061773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 36071773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 36080765691eSMarkus Armbruster warn_report("virtio-net: " 36096e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 36101773d9eeSKONRAD Frederic n->net_conf.tx); 36110765691eSMarkus Armbruster error_printf("Defaulting to \"bh\""); 36126e790746SPaolo Bonzini } 36136e790746SPaolo Bonzini 36142eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 36152eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 36169b02e161SWei Wang 3617441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3618f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 3619da51a335SJason Wang } 3620da51a335SJason Wang 362117a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 36221773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 36231773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 36246e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 36259d8c6a25SDr. David Alan Gilbert qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(), 36269d8c6a25SDr. David Alan Gilbert QEMU_CLOCK_VIRTUAL, 3627f57fcf70SJason Wang virtio_net_announce_timer, n); 3628b2c929f0SDr. David Alan Gilbert n->announce_timer.round = 0; 36296e790746SPaolo Bonzini 36308a253ec2SKONRAD Frederic if (n->netclient_type) { 36318a253ec2SKONRAD Frederic /* 36328a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 36338a253ec2SKONRAD Frederic */ 36348a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 36358a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 36368a253ec2SKONRAD Frederic } else { 36371773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 3638284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 36398a253ec2SKONRAD Frederic } 36408a253ec2SKONRAD Frederic 3641441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3642d4c62930SBin Meng n->nic->ncs[i].do_not_pad = true; 3643d4c62930SBin Meng } 3644d4c62930SBin Meng 36456e790746SPaolo Bonzini peer_test_vnet_hdr(n); 36466e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 3647441537f1SJason Wang for (i = 0; i < n->max_queue_pairs; i++) { 3648d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 36496e790746SPaolo Bonzini } 36506e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 36516e790746SPaolo Bonzini } else { 36526e790746SPaolo Bonzini n->host_hdr_len = 0; 36536e790746SPaolo Bonzini } 36546e790746SPaolo Bonzini 36551773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 36566e790746SPaolo Bonzini 36576e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 36581773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 3659e22f0603SYuri Benditovich virtio_net_set_mrg_rx_bufs(n, 0, 0, 0); 36606e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 36616e790746SPaolo Bonzini 36626e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 36636e790746SPaolo Bonzini 36646e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 36656e790746SPaolo Bonzini 3666b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 3667b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 3668b1be4280SAmos Kong 3669e87936eaSCindy Lu if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { 3670e87936eaSCindy Lu struct virtio_net_config netcfg = {}; 3671e87936eaSCindy Lu memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN); 3672e87936eaSCindy Lu vhost_net_set_config(get_vhost_net(nc->peer), 3673e87936eaSCindy Lu (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER); 3674e87936eaSCindy Lu } 36752974e916SYuri Benditovich QTAILQ_INIT(&n->rsc_chains); 3676284a32f0SAndreas Färber n->qdev = dev; 36774474e37aSYuri Benditovich 36784474e37aSYuri Benditovich net_rx_pkt_init(&n->rx_pkt, false); 36790145c393SAndrew Melnychenko 36800145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 36810145c393SAndrew Melnychenko virtio_net_load_ebpf(n); 36820145c393SAndrew Melnychenko } 368317ec5a86SKONRAD Frederic } 368417ec5a86SKONRAD Frederic 3685b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev) 368617ec5a86SKONRAD Frederic { 3687306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 3688306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 3689441537f1SJason Wang int i, max_queue_pairs; 369017ec5a86SKONRAD Frederic 36910145c393SAndrew Melnychenko if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) { 36920145c393SAndrew Melnychenko virtio_net_unload_ebpf(n); 36930145c393SAndrew Melnychenko } 36940145c393SAndrew Melnychenko 369517ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 369617ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 369717ec5a86SKONRAD Frederic 36988a253ec2SKONRAD Frederic g_free(n->netclient_name); 36998a253ec2SKONRAD Frederic n->netclient_name = NULL; 37008a253ec2SKONRAD Frederic g_free(n->netclient_type); 37018a253ec2SKONRAD Frederic n->netclient_type = NULL; 37028a253ec2SKONRAD Frederic 370317ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 370417ec5a86SKONRAD Frederic g_free(n->vlans); 370517ec5a86SKONRAD Frederic 37069711cd0dSJens Freimann if (n->failover) { 3707f3558b1bSKevin Wolf qobject_unref(n->primary_opts); 370865018100SJuan Quintela device_listener_unregister(&n->primary_listener); 37091e157667SLaurent Vivier remove_migration_state_change_notifier(&n->migration_state); 3710f3558b1bSKevin Wolf } else { 3711f3558b1bSKevin Wolf assert(n->primary_opts == NULL); 37129711cd0dSJens Freimann } 37139711cd0dSJens Freimann 3714441537f1SJason Wang max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1; 3715441537f1SJason Wang for (i = 0; i < max_queue_pairs; i++) { 3716f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 371717ec5a86SKONRAD Frederic } 3718d945d9f1SYuri Benditovich /* delete also control vq */ 3719441537f1SJason Wang virtio_del_queue(vdev, max_queue_pairs * 2); 3720944458b6SDr. David Alan Gilbert qemu_announce_timer_del(&n->announce_timer, false); 372117ec5a86SKONRAD Frederic g_free(n->vqs); 372217ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 37232974e916SYuri Benditovich virtio_net_rsc_cleanup(n); 372459079029SYuri Benditovich g_free(n->rss_data.indirections_table); 37254474e37aSYuri Benditovich net_rx_pkt_uninit(n->rx_pkt); 37266a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 372717ec5a86SKONRAD Frederic } 372817ec5a86SKONRAD Frederic 372917ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 373017ec5a86SKONRAD Frederic { 373117ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 373217ec5a86SKONRAD Frederic 373317ec5a86SKONRAD Frederic /* 373417ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 373517ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 373617ec5a86SKONRAD Frederic */ 373717ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 3738aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 3739aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 374040c2281cSMarkus Armbruster DEVICE(n)); 37410145c393SAndrew Melnychenko 37420145c393SAndrew Melnychenko ebpf_rss_init(&n->ebpf_rss); 374317ec5a86SKONRAD Frederic } 374417ec5a86SKONRAD Frederic 374544b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 37464d45dcfbSHalil Pasic { 37474d45dcfbSHalil Pasic VirtIONet *n = opaque; 37484d45dcfbSHalil Pasic 37494d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 37504d45dcfbSHalil Pasic * it might keep writing to memory. */ 37514d45dcfbSHalil Pasic assert(!n->vhost_started); 375244b1ff31SDr. David Alan Gilbert 375344b1ff31SDr. David Alan Gilbert return 0; 37544d45dcfbSHalil Pasic } 37554d45dcfbSHalil Pasic 37569711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque) 37579711cd0dSJens Freimann { 37589711cd0dSJens Freimann DeviceState *dev = opaque; 375921e8709bSJuan Quintela DeviceState *primary; 37609711cd0dSJens Freimann VirtIODevice *vdev = VIRTIO_DEVICE(dev); 37619711cd0dSJens Freimann VirtIONet *n = VIRTIO_NET(vdev); 37629711cd0dSJens Freimann 3763284f42a5SJens Freimann if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) { 3764284f42a5SJens Freimann return false; 3765284f42a5SJens Freimann } 376621e8709bSJuan Quintela primary = failover_find_primary_device(n); 376721e8709bSJuan Quintela return primary ? primary->pending_deleted_event : false; 37689711cd0dSJens Freimann } 37699711cd0dSJens Freimann 37709711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque) 37719711cd0dSJens Freimann { 37729711cd0dSJens Freimann DeviceState *dev = opaque; 37739711cd0dSJens Freimann VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); 37749711cd0dSJens Freimann 37759711cd0dSJens Freimann return vdc->primary_unplug_pending(dev); 37769711cd0dSJens Freimann } 37779711cd0dSJens Freimann 3778c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev) 3779c255488dSJonah Palmer { 3780c255488dSJonah Palmer VirtIONet *n = VIRTIO_NET(vdev); 3781c255488dSJonah Palmer NetClientState *nc = qemu_get_queue(n->nic); 3782c255488dSJonah Palmer struct vhost_net *net = get_vhost_net(nc->peer); 3783c255488dSJonah Palmer return &net->dev; 3784c255488dSJonah Palmer } 3785c255488dSJonah Palmer 37864d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 37874d45dcfbSHalil Pasic .name = "virtio-net", 37884d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 37894d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 37904d45dcfbSHalil Pasic .fields = (VMStateField[]) { 37914d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 37924d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 37934d45dcfbSHalil Pasic }, 37944d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 37959711cd0dSJens Freimann .dev_unplug_pending = dev_unplug_pending, 37964d45dcfbSHalil Pasic }; 3797290c2428SDr. David Alan Gilbert 379817ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 3799127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 3800127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 3801127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 380287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 3803127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 3804127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 380587108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 3806127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 380787108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 3808127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 380987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 3810127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 381187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 3812127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 381387108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 3814127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 381587108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 3816127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 381787108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 3818127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 381987108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 3820127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 382187108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 3822127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 382387108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 3824127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 382587108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 3826127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 382787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 3828127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 382987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 3830127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 383187108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 3832127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 383387108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 3834127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 383587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 3836127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 383787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 3838127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 383959079029SYuri Benditovich DEFINE_PROP_BIT64("rss", VirtIONet, host_features, 384059079029SYuri Benditovich VIRTIO_NET_F_RSS, false), 3841e22f0603SYuri Benditovich DEFINE_PROP_BIT64("hash", VirtIONet, host_features, 3842e22f0603SYuri Benditovich VIRTIO_NET_F_HASH_REPORT, false), 38432974e916SYuri Benditovich DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features, 38442974e916SYuri Benditovich VIRTIO_NET_F_RSC_EXT, false), 38452974e916SYuri Benditovich DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout, 38462974e916SYuri Benditovich VIRTIO_NET_RSC_DEFAULT_INTERVAL), 384717ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 384817ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 384917ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 385017ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 385117ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 38521c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 38531c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 38549b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 38559b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 3856a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 385775ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 385875ebec11SMaxime Coquelin true), 38599473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 38609473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 38619711cd0dSJens Freimann DEFINE_PROP_BOOL("failover", VirtIONet, failover, false), 386217ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 386317ec5a86SKONRAD Frederic }; 386417ec5a86SKONRAD Frederic 386517ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 386617ec5a86SKONRAD Frederic { 386717ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 386817ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 3869e6f746b3SAndreas Färber 38704f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_net_properties); 3871290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 3872125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 3873e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 3874306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 387517ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 387617ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 387717ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 387817ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 387917ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 388017ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 38817dc6be52SXuan Zhuo vdc->queue_reset = virtio_net_queue_reset; 38827f863302SKangjie Xu vdc->queue_enable = virtio_net_queue_enable; 388317ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 388417ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 388517ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 38862a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 38877788c3f2SMikhail Sennikovsky vdc->post_load = virtio_net_post_load_virtio; 3888982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 38899711cd0dSJens Freimann vdc->primary_unplug_pending = primary_unplug_pending; 3890c255488dSJonah Palmer vdc->get_vhost = virtio_net_get_vhost; 389117ec5a86SKONRAD Frederic } 389217ec5a86SKONRAD Frederic 389317ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 389417ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 389517ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 389617ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 389717ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 389817ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 389917ec5a86SKONRAD Frederic }; 390017ec5a86SKONRAD Frederic 390117ec5a86SKONRAD Frederic static void virtio_register_types(void) 390217ec5a86SKONRAD Frederic { 390317ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 390417ec5a86SKONRAD Frederic } 390517ec5a86SKONRAD Frederic 390617ec5a86SKONRAD Frederic type_init(virtio_register_types) 3907