xref: /openbmc/qemu/hw/net/virtio-net.c (revision 0145c3934895e2cb2b8bd679ca274bc5f7e76de7)
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"
17db725815SMarkus Armbruster #include "qemu/main-loop.h"
180b8fa32fSMarkus Armbruster #include "qemu/module.h"
196e790746SPaolo Bonzini #include "hw/virtio/virtio.h"
206e790746SPaolo Bonzini #include "net/net.h"
216e790746SPaolo Bonzini #include "net/checksum.h"
226e790746SPaolo Bonzini #include "net/tap.h"
236e790746SPaolo Bonzini #include "qemu/error-report.h"
246e790746SPaolo Bonzini #include "qemu/timer.h"
259711cd0dSJens Freimann #include "qemu/option.h"
269711cd0dSJens Freimann #include "qemu/option_int.h"
279711cd0dSJens Freimann #include "qemu/config-file.h"
289711cd0dSJens Freimann #include "qapi/qmp/qdict.h"
296e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h"
306e790746SPaolo Bonzini #include "net/vhost_net.h"
319d8c6a25SDr. David Alan Gilbert #include "net/announce.h"
3217ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h"
33e688df6bSMarkus Armbruster #include "qapi/error.h"
349af23989SMarkus Armbruster #include "qapi/qapi-events-net.h"
35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
369711cd0dSJens Freimann #include "qapi/qapi-types-migration.h"
379711cd0dSJens Freimann #include "qapi/qapi-events-migration.h"
381399c60dSRusty Russell #include "hw/virtio/virtio-access.h"
39f8d806c9SJuan Quintela #include "migration/misc.h"
409473939eSJason Baron #include "standard-headers/linux/ethtool.h"
412f780b6aSMarkus Armbruster #include "sysemu/sysemu.h"
429d8c6a25SDr. David Alan Gilbert #include "trace.h"
439711cd0dSJens Freimann #include "monitor/qdev.h"
449711cd0dSJens Freimann #include "hw/pci/pci.h"
454474e37aSYuri Benditovich #include "net_rx_pkt.h"
46108a6481SCindy Lu #include "hw/virtio/vhost.h"
476e790746SPaolo Bonzini 
486e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
496e790746SPaolo Bonzini 
506e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES    64
516e790746SPaolo Bonzini #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
526e790746SPaolo Bonzini 
531c0fbfa3SMichael S. Tsirkin /* previously fixed value */
541c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
559b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
569b02e161SWei Wang 
571c0fbfa3SMichael S. Tsirkin /* for now, only allow larger queues; with virtio-1, guest can downsize */
581c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
599b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
601c0fbfa3SMichael S. Tsirkin 
612974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE   8        /* ipv4 saddr + daddr */
622974e916SYuri Benditovich 
632974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG         0x3F
642974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH   0xF000
652974e916SYuri Benditovich 
662974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */
672974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
682974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
692974e916SYuri Benditovich 
702974e916SYuri Benditovich /* header length value in ip header without option */
712974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5
722974e916SYuri Benditovich 
732974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
742974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
752974e916SYuri Benditovich 
762974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance
772974e916SYuri Benditovich    a lot, and should be tuned carefully, '300000'(300us) is the recommended
782974e916SYuri Benditovich    value to pass the WHQL test, '50000' can gain 2x netperf throughput with
792974e916SYuri Benditovich    tso/gso/gro 'off'. */
802974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
812974e916SYuri Benditovich 
8259079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
8359079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
8459079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
8559079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
8659079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
8759079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
8859079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
8959079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
9059079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
9159079029SYuri Benditovich 
92ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = {
93127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MAC,
945d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, mac)},
95127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_STATUS,
965d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, status)},
97127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MQ,
985d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
99127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MTU,
1005d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, mtu)},
1019473939eSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX,
1025d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, duplex)},
103e22f0603SYuri Benditovich     {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT),
10459079029SYuri Benditovich      .end = endof(struct virtio_net_config, supported_hash_types)},
1056e790746SPaolo Bonzini     {}
1066e790746SPaolo Bonzini };
1076e790746SPaolo Bonzini 
1086e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
1096e790746SPaolo Bonzini {
1106e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1116e790746SPaolo Bonzini 
1126e790746SPaolo Bonzini     return &n->vqs[nc->queue_index];
1136e790746SPaolo Bonzini }
1146e790746SPaolo Bonzini 
1156e790746SPaolo Bonzini static int vq2q(int queue_index)
1166e790746SPaolo Bonzini {
1176e790746SPaolo Bonzini     return queue_index / 2;
1186e790746SPaolo Bonzini }
1196e790746SPaolo Bonzini 
1206e790746SPaolo Bonzini /* TODO
1216e790746SPaolo Bonzini  * - we could suppress RX interrupt if we were so inclined.
1226e790746SPaolo Bonzini  */
1236e790746SPaolo Bonzini 
1246e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
1256e790746SPaolo Bonzini {
12617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1276e790746SPaolo Bonzini     struct virtio_net_config netcfg;
128c546ecf2SJason Wang     NetClientState *nc = qemu_get_queue(n->nic);
129fb592882SCindy Lu     static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
1306e790746SPaolo Bonzini 
131108a6481SCindy Lu     int ret = 0;
132108a6481SCindy Lu     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
1331399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.status, n->status);
1341399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
135a93e599dSMaxime Coquelin     virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
1366e790746SPaolo Bonzini     memcpy(netcfg.mac, n->mac, ETH_ALEN);
1379473939eSJason Baron     virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed);
1389473939eSJason Baron     netcfg.duplex = n->net_conf.duplex;
13959079029SYuri Benditovich     netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE;
14059079029SYuri Benditovich     virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length,
141e22f0603SYuri Benditovich                  virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ?
142e22f0603SYuri Benditovich                  VIRTIO_NET_RSS_MAX_TABLE_LEN : 1);
14359079029SYuri Benditovich     virtio_stl_p(vdev, &netcfg.supported_hash_types,
14459079029SYuri Benditovich                  VIRTIO_NET_RSS_SUPPORTED_HASHES);
1456e790746SPaolo Bonzini     memcpy(config, &netcfg, n->config_size);
146108a6481SCindy Lu 
147c546ecf2SJason Wang     /*
148c546ecf2SJason Wang      * Is this VDPA? No peer means not VDPA: there's no way to
149c546ecf2SJason Wang      * disconnect/reconnect a VDPA peer.
150c546ecf2SJason Wang      */
151c546ecf2SJason Wang     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
152108a6481SCindy Lu         ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
153108a6481SCindy Lu                                    n->config_size);
154108a6481SCindy Lu         if (ret != -1) {
155fb592882SCindy Lu             /*
156fb592882SCindy Lu              * Some NIC/kernel combinations present 0 as the mac address.  As
157fb592882SCindy Lu              * that is not a legal address, try to proceed with the
158fb592882SCindy Lu              * address from the QEMU command line in the hope that the
159fb592882SCindy Lu              * address has been configured correctly elsewhere - just not
160fb592882SCindy Lu              * reported by the device.
161fb592882SCindy Lu              */
162fb592882SCindy Lu             if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
163fb592882SCindy Lu                 info_report("Zero hardware mac address detected. Ignoring.");
164fb592882SCindy Lu                 memcpy(netcfg.mac, n->mac, ETH_ALEN);
165fb592882SCindy Lu             }
166108a6481SCindy Lu             memcpy(config, &netcfg, n->config_size);
167108a6481SCindy Lu         }
168108a6481SCindy Lu     }
1696e790746SPaolo Bonzini }
1706e790746SPaolo Bonzini 
1716e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
1726e790746SPaolo Bonzini {
17317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1746e790746SPaolo Bonzini     struct virtio_net_config netcfg = {};
175c546ecf2SJason Wang     NetClientState *nc = qemu_get_queue(n->nic);
1766e790746SPaolo Bonzini 
1776e790746SPaolo Bonzini     memcpy(&netcfg, config, n->config_size);
1786e790746SPaolo Bonzini 
17995129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
18095129d6fSCornelia Huck         !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
1816e790746SPaolo Bonzini         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
1826e790746SPaolo Bonzini         memcpy(n->mac, netcfg.mac, ETH_ALEN);
1836e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1846e790746SPaolo Bonzini     }
185108a6481SCindy Lu 
186c546ecf2SJason Wang     /*
187c546ecf2SJason Wang      * Is this VDPA? No peer means not VDPA: there's no way to
188c546ecf2SJason Wang      * disconnect/reconnect a VDPA peer.
189c546ecf2SJason Wang      */
190c546ecf2SJason Wang     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
191c546ecf2SJason Wang         vhost_net_set_config(get_vhost_net(nc->peer),
192c546ecf2SJason Wang                              (uint8_t *)&netcfg, 0, n->config_size,
193108a6481SCindy Lu                              VHOST_SET_CONFIG_TYPE_MASTER);
194108a6481SCindy Lu       }
1956e790746SPaolo Bonzini }
1966e790746SPaolo Bonzini 
1976e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status)
1986e790746SPaolo Bonzini {
19917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2006e790746SPaolo Bonzini     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
20117a0ca55SKONRAD Frederic         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
2026e790746SPaolo Bonzini }
2036e790746SPaolo Bonzini 
204b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net)
205b2c929f0SDr. David Alan Gilbert {
206b2c929f0SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(net);
207b2c929f0SDr. David Alan Gilbert     trace_virtio_net_announce_notify();
208b2c929f0SDr. David Alan Gilbert 
209b2c929f0SDr. David Alan Gilbert     net->status |= VIRTIO_NET_S_ANNOUNCE;
210b2c929f0SDr. David Alan Gilbert     virtio_notify_config(vdev);
211b2c929f0SDr. David Alan Gilbert }
212b2c929f0SDr. David Alan Gilbert 
213f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque)
214f57fcf70SJason Wang {
215f57fcf70SJason Wang     VirtIONet *n = opaque;
2169d8c6a25SDr. David Alan Gilbert     trace_virtio_net_announce_timer(n->announce_timer.round);
217f57fcf70SJason Wang 
2189d8c6a25SDr. David Alan Gilbert     n->announce_timer.round--;
219b2c929f0SDr. David Alan Gilbert     virtio_net_announce_notify(n);
220b2c929f0SDr. David Alan Gilbert }
221b2c929f0SDr. David Alan Gilbert 
222b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc)
223b2c929f0SDr. David Alan Gilbert {
224b2c929f0SDr. David Alan Gilbert     VirtIONet *n = qemu_get_nic_opaque(nc);
225b2c929f0SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
226b2c929f0SDr. David Alan Gilbert 
227b2c929f0SDr. David Alan Gilbert     /*
228b2c929f0SDr. David Alan Gilbert      * Make sure the virtio migration announcement timer isn't running
229b2c929f0SDr. David Alan Gilbert      * If it is, let it trigger announcement so that we do not cause
230b2c929f0SDr. David Alan Gilbert      * confusion.
231b2c929f0SDr. David Alan Gilbert      */
232b2c929f0SDr. David Alan Gilbert     if (n->announce_timer.round) {
233b2c929f0SDr. David Alan Gilbert         return;
234b2c929f0SDr. David Alan Gilbert     }
235b2c929f0SDr. David Alan Gilbert 
236b2c929f0SDr. David Alan Gilbert     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
237b2c929f0SDr. David Alan Gilbert         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
238b2c929f0SDr. David Alan Gilbert             virtio_net_announce_notify(n);
239b2c929f0SDr. David Alan Gilbert     }
240f57fcf70SJason Wang }
241f57fcf70SJason Wang 
2426e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
2436e790746SPaolo Bonzini {
24417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2456e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
2466e790746SPaolo Bonzini     int queues = n->multiqueue ? n->max_queues : 1;
2476e790746SPaolo Bonzini 
248ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
2496e790746SPaolo Bonzini         return;
2506e790746SPaolo Bonzini     }
2516e790746SPaolo Bonzini 
2528c1ac475SRadim Krčmář     if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
2538c1ac475SRadim Krčmář         !!n->vhost_started) {
2546e790746SPaolo Bonzini         return;
2556e790746SPaolo Bonzini     }
2566e790746SPaolo Bonzini     if (!n->vhost_started) {
257086abc1cSMichael S. Tsirkin         int r, i;
258086abc1cSMichael S. Tsirkin 
2591bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
2601bfa316cSGreg Kurz             error_report("backend does not support %s vnet headers; "
2611bfa316cSGreg Kurz                          "falling back on userspace virtio",
2621bfa316cSGreg Kurz                          virtio_is_big_endian(vdev) ? "BE" : "LE");
2631bfa316cSGreg Kurz             return;
2641bfa316cSGreg Kurz         }
2651bfa316cSGreg Kurz 
266086abc1cSMichael S. Tsirkin         /* Any packets outstanding? Purge them to avoid touching rings
267086abc1cSMichael S. Tsirkin          * when vhost is running.
268086abc1cSMichael S. Tsirkin          */
269086abc1cSMichael S. Tsirkin         for (i = 0;  i < queues; i++) {
270086abc1cSMichael S. Tsirkin             NetClientState *qnc = qemu_get_subqueue(n->nic, i);
271086abc1cSMichael S. Tsirkin 
272086abc1cSMichael S. Tsirkin             /* Purge both directions: TX and RX. */
273086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
274086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
275086abc1cSMichael S. Tsirkin         }
276086abc1cSMichael S. Tsirkin 
277a93e599dSMaxime Coquelin         if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
278a93e599dSMaxime Coquelin             r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
279a93e599dSMaxime Coquelin             if (r < 0) {
280a93e599dSMaxime Coquelin                 error_report("%uBytes MTU not supported by the backend",
281a93e599dSMaxime Coquelin                              n->net_conf.mtu);
282a93e599dSMaxime Coquelin 
283a93e599dSMaxime Coquelin                 return;
284a93e599dSMaxime Coquelin             }
285a93e599dSMaxime Coquelin         }
286a93e599dSMaxime Coquelin 
2876e790746SPaolo Bonzini         n->vhost_started = 1;
28817a0ca55SKONRAD Frederic         r = vhost_net_start(vdev, n->nic->ncs, queues);
2896e790746SPaolo Bonzini         if (r < 0) {
2906e790746SPaolo Bonzini             error_report("unable to start vhost net: %d: "
2916e790746SPaolo Bonzini                          "falling back on userspace virtio", -r);
2926e790746SPaolo Bonzini             n->vhost_started = 0;
2936e790746SPaolo Bonzini         }
2946e790746SPaolo Bonzini     } else {
29517a0ca55SKONRAD Frederic         vhost_net_stop(vdev, n->nic->ncs, queues);
2966e790746SPaolo Bonzini         n->vhost_started = 0;
2976e790746SPaolo Bonzini     }
2986e790746SPaolo Bonzini }
2996e790746SPaolo Bonzini 
3001bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
3011bfa316cSGreg Kurz                                           NetClientState *peer,
3021bfa316cSGreg Kurz                                           bool enable)
3031bfa316cSGreg Kurz {
3041bfa316cSGreg Kurz     if (virtio_is_big_endian(vdev)) {
3051bfa316cSGreg Kurz         return qemu_set_vnet_be(peer, enable);
3061bfa316cSGreg Kurz     } else {
3071bfa316cSGreg Kurz         return qemu_set_vnet_le(peer, enable);
3081bfa316cSGreg Kurz     }
3091bfa316cSGreg Kurz }
3101bfa316cSGreg Kurz 
3111bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
3121bfa316cSGreg Kurz                                        int queues, bool enable)
3131bfa316cSGreg Kurz {
3141bfa316cSGreg Kurz     int i;
3151bfa316cSGreg Kurz 
3161bfa316cSGreg Kurz     for (i = 0; i < queues; i++) {
3171bfa316cSGreg Kurz         if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
3181bfa316cSGreg Kurz             enable) {
3191bfa316cSGreg Kurz             while (--i >= 0) {
3201bfa316cSGreg Kurz                 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
3211bfa316cSGreg Kurz             }
3221bfa316cSGreg Kurz 
3231bfa316cSGreg Kurz             return true;
3241bfa316cSGreg Kurz         }
3251bfa316cSGreg Kurz     }
3261bfa316cSGreg Kurz 
3271bfa316cSGreg Kurz     return false;
3281bfa316cSGreg Kurz }
3291bfa316cSGreg Kurz 
3301bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
3311bfa316cSGreg Kurz {
3321bfa316cSGreg Kurz     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3331bfa316cSGreg Kurz     int queues = n->multiqueue ? n->max_queues : 1;
3341bfa316cSGreg Kurz 
3351bfa316cSGreg Kurz     if (virtio_net_started(n, status)) {
3361bfa316cSGreg Kurz         /* Before using the device, we tell the network backend about the
3371bfa316cSGreg Kurz          * endianness to use when parsing vnet headers. If the backend
3381bfa316cSGreg Kurz          * can't do it, we fallback onto fixing the headers in the core
3391bfa316cSGreg Kurz          * virtio-net code.
3401bfa316cSGreg Kurz          */
3411bfa316cSGreg Kurz         n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
3421bfa316cSGreg Kurz                                                             queues, true);
3431bfa316cSGreg Kurz     } else if (virtio_net_started(n, vdev->status)) {
3441bfa316cSGreg Kurz         /* After using the device, we need to reset the network backend to
3451bfa316cSGreg Kurz          * the default (guest native endianness), otherwise the guest may
3461bfa316cSGreg Kurz          * lose network connectivity if it is rebooted into a different
3471bfa316cSGreg Kurz          * endianness.
3481bfa316cSGreg Kurz          */
3491bfa316cSGreg Kurz         virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
3501bfa316cSGreg Kurz     }
3511bfa316cSGreg Kurz }
3521bfa316cSGreg Kurz 
353283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
354283e2c2aSYuri Benditovich {
355283e2c2aSYuri Benditovich     unsigned int dropped = virtqueue_drop_all(vq);
356283e2c2aSYuri Benditovich     if (dropped) {
357283e2c2aSYuri Benditovich         virtio_notify(vdev, vq);
358283e2c2aSYuri Benditovich     }
359283e2c2aSYuri Benditovich }
360283e2c2aSYuri Benditovich 
3616e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
3626e790746SPaolo Bonzini {
36317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
3646e790746SPaolo Bonzini     VirtIONetQueue *q;
3656e790746SPaolo Bonzini     int i;
3666e790746SPaolo Bonzini     uint8_t queue_status;
3676e790746SPaolo Bonzini 
3681bfa316cSGreg Kurz     virtio_net_vnet_endian_status(n, status);
3696e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
3706e790746SPaolo Bonzini 
3716e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
37238705bb5SFam Zheng         NetClientState *ncs = qemu_get_subqueue(n->nic, i);
37338705bb5SFam Zheng         bool queue_started;
3746e790746SPaolo Bonzini         q = &n->vqs[i];
3756e790746SPaolo Bonzini 
3766e790746SPaolo Bonzini         if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
3776e790746SPaolo Bonzini             queue_status = 0;
3786e790746SPaolo Bonzini         } else {
3796e790746SPaolo Bonzini             queue_status = status;
3806e790746SPaolo Bonzini         }
38138705bb5SFam Zheng         queue_started =
38238705bb5SFam Zheng             virtio_net_started(n, queue_status) && !n->vhost_started;
38338705bb5SFam Zheng 
38438705bb5SFam Zheng         if (queue_started) {
38538705bb5SFam Zheng             qemu_flush_queued_packets(ncs);
38638705bb5SFam Zheng         }
3876e790746SPaolo Bonzini 
3886e790746SPaolo Bonzini         if (!q->tx_waiting) {
3896e790746SPaolo Bonzini             continue;
3906e790746SPaolo Bonzini         }
3916e790746SPaolo Bonzini 
39238705bb5SFam Zheng         if (queue_started) {
3936e790746SPaolo Bonzini             if (q->tx_timer) {
394bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
395bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
3966e790746SPaolo Bonzini             } else {
3976e790746SPaolo Bonzini                 qemu_bh_schedule(q->tx_bh);
3986e790746SPaolo Bonzini             }
3996e790746SPaolo Bonzini         } else {
4006e790746SPaolo Bonzini             if (q->tx_timer) {
401bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
4026e790746SPaolo Bonzini             } else {
4036e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
4046e790746SPaolo Bonzini             }
405283e2c2aSYuri Benditovich             if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
40670e53e6eSJason Wang                 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
40770e53e6eSJason Wang                 vdev->vm_running) {
408283e2c2aSYuri Benditovich                 /* if tx is waiting we are likely have some packets in tx queue
409283e2c2aSYuri Benditovich                  * and disabled notification */
410283e2c2aSYuri Benditovich                 q->tx_waiting = 0;
411283e2c2aSYuri Benditovich                 virtio_queue_set_notification(q->tx_vq, 1);
412283e2c2aSYuri Benditovich                 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
413283e2c2aSYuri Benditovich             }
4146e790746SPaolo Bonzini         }
4156e790746SPaolo Bonzini     }
4166e790746SPaolo Bonzini }
4176e790746SPaolo Bonzini 
4186e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
4196e790746SPaolo Bonzini {
4206e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
42117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
4226e790746SPaolo Bonzini     uint16_t old_status = n->status;
4236e790746SPaolo Bonzini 
4246e790746SPaolo Bonzini     if (nc->link_down)
4256e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
4266e790746SPaolo Bonzini     else
4276e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
4286e790746SPaolo Bonzini 
4296e790746SPaolo Bonzini     if (n->status != old_status)
43017a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
4316e790746SPaolo Bonzini 
43217a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
4336e790746SPaolo Bonzini }
4346e790746SPaolo Bonzini 
435b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
436b1be4280SAmos Kong {
437b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
438b1be4280SAmos Kong 
439b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
440ddfb0baaSMarkus Armbruster         char *path = object_get_canonical_path(OBJECT(n->qdev));
44106150279SWenchao Xia         qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
4423ab72385SPeter Xu                                               n->netclient_name, path);
44396e35046SAmos Kong         g_free(path);
444b1be4280SAmos Kong 
445b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
446b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
447b1be4280SAmos Kong     }
448b1be4280SAmos Kong }
449b1be4280SAmos Kong 
450f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
451f7bc8ef8SAmos Kong {
45254aa3de7SEric Blake     intList *list;
453f7bc8ef8SAmos Kong     int i, j;
454f7bc8ef8SAmos Kong 
455f7bc8ef8SAmos Kong     list = NULL;
456f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
457f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
458f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
45954aa3de7SEric Blake                 QAPI_LIST_PREPEND(list, (i << 5) + j);
460f7bc8ef8SAmos Kong             }
461f7bc8ef8SAmos Kong         }
462f7bc8ef8SAmos Kong     }
463f7bc8ef8SAmos Kong 
464f7bc8ef8SAmos Kong     return list;
465f7bc8ef8SAmos Kong }
466f7bc8ef8SAmos Kong 
467b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
468b1be4280SAmos Kong {
469b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
470f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
471b1be4280SAmos Kong     RxFilterInfo *info;
47254aa3de7SEric Blake     strList *str_list;
473f7bc8ef8SAmos Kong     int i;
474b1be4280SAmos Kong 
475b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
476b1be4280SAmos Kong     info->name = g_strdup(nc->name);
477b1be4280SAmos Kong     info->promiscuous = n->promisc;
478b1be4280SAmos Kong 
479b1be4280SAmos Kong     if (n->nouni) {
480b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
481b1be4280SAmos Kong     } else if (n->alluni) {
482b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
483b1be4280SAmos Kong     } else {
484b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
485b1be4280SAmos Kong     }
486b1be4280SAmos Kong 
487b1be4280SAmos Kong     if (n->nomulti) {
488b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
489b1be4280SAmos Kong     } else if (n->allmulti) {
490b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
491b1be4280SAmos Kong     } else {
492b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
493b1be4280SAmos Kong     }
494b1be4280SAmos Kong 
495b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
496b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
497b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
498b1be4280SAmos Kong 
499b0575ba4SScott Feldman     info->main_mac = qemu_mac_strdup_printf(n->mac);
500b1be4280SAmos Kong 
501b1be4280SAmos Kong     str_list = NULL;
502b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
50354aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
50454aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
505b1be4280SAmos Kong     }
506b1be4280SAmos Kong     info->unicast_table = str_list;
507b1be4280SAmos Kong 
508b1be4280SAmos Kong     str_list = NULL;
509b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
51054aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
51154aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
512b1be4280SAmos Kong     }
513b1be4280SAmos Kong     info->multicast_table = str_list;
514f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
515b1be4280SAmos Kong 
51695129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
517f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
518f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
519f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
520f7bc8ef8SAmos Kong     } else {
521f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
522b1be4280SAmos Kong     }
523b1be4280SAmos Kong 
524b1be4280SAmos Kong     /* enable event notification after query */
525b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
526b1be4280SAmos Kong 
527b1be4280SAmos Kong     return info;
528b1be4280SAmos Kong }
529b1be4280SAmos Kong 
5306e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev)
5316e790746SPaolo Bonzini {
53217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
53394b52958SGreg Kurz     int i;
5346e790746SPaolo Bonzini 
5356e790746SPaolo Bonzini     /* Reset back to compatibility mode */
5366e790746SPaolo Bonzini     n->promisc = 1;
5376e790746SPaolo Bonzini     n->allmulti = 0;
5386e790746SPaolo Bonzini     n->alluni = 0;
5396e790746SPaolo Bonzini     n->nomulti = 0;
5406e790746SPaolo Bonzini     n->nouni = 0;
5416e790746SPaolo Bonzini     n->nobcast = 0;
5426e790746SPaolo Bonzini     /* multiqueue is disabled by default */
5436e790746SPaolo Bonzini     n->curr_queues = 1;
5449d8c6a25SDr. David Alan Gilbert     timer_del(n->announce_timer.tm);
5459d8c6a25SDr. David Alan Gilbert     n->announce_timer.round = 0;
546f57fcf70SJason Wang     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
5476e790746SPaolo Bonzini 
5486e790746SPaolo Bonzini     /* Flush any MAC and VLAN filter table state */
5496e790746SPaolo Bonzini     n->mac_table.in_use = 0;
5506e790746SPaolo Bonzini     n->mac_table.first_multi = 0;
5516e790746SPaolo Bonzini     n->mac_table.multi_overflow = 0;
5526e790746SPaolo Bonzini     n->mac_table.uni_overflow = 0;
5536e790746SPaolo Bonzini     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
5546e790746SPaolo Bonzini     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
555702d66a8SMichael S. Tsirkin     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
5566e790746SPaolo Bonzini     memset(n->vlans, 0, MAX_VLAN >> 3);
55794b52958SGreg Kurz 
55894b52958SGreg Kurz     /* Flush any async TX */
55994b52958SGreg Kurz     for (i = 0;  i < n->max_queues; i++) {
56094b52958SGreg Kurz         NetClientState *nc = qemu_get_subqueue(n->nic, i);
56194b52958SGreg Kurz 
56294b52958SGreg Kurz         if (nc->peer) {
56394b52958SGreg Kurz             qemu_flush_or_purge_queued_packets(nc->peer, true);
56494b52958SGreg Kurz             assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
56594b52958SGreg Kurz         }
56694b52958SGreg Kurz     }
5676e790746SPaolo Bonzini }
5686e790746SPaolo Bonzini 
5696e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
5706e790746SPaolo Bonzini {
5716e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
5726e790746SPaolo Bonzini     if (!nc->peer) {
5736e790746SPaolo Bonzini         return;
5746e790746SPaolo Bonzini     }
5756e790746SPaolo Bonzini 
576d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
5776e790746SPaolo Bonzini }
5786e790746SPaolo Bonzini 
5796e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
5806e790746SPaolo Bonzini {
5816e790746SPaolo Bonzini     return n->has_vnet_hdr;
5826e790746SPaolo Bonzini }
5836e790746SPaolo Bonzini 
5846e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
5856e790746SPaolo Bonzini {
5866e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
5876e790746SPaolo Bonzini         return 0;
5886e790746SPaolo Bonzini 
589d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
5906e790746SPaolo Bonzini 
5916e790746SPaolo Bonzini     return n->has_ufo;
5926e790746SPaolo Bonzini }
5936e790746SPaolo Bonzini 
594bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
595e22f0603SYuri Benditovich                                        int version_1, int hash_report)
5966e790746SPaolo Bonzini {
5976e790746SPaolo Bonzini     int i;
5986e790746SPaolo Bonzini     NetClientState *nc;
5996e790746SPaolo Bonzini 
6006e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
6016e790746SPaolo Bonzini 
602bb9d17f8SCornelia Huck     if (version_1) {
603e22f0603SYuri Benditovich         n->guest_hdr_len = hash_report ?
604e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_v1_hash) :
605e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_mrg_rxbuf);
606e22f0603SYuri Benditovich         n->rss_data.populate_hash = !!hash_report;
607bb9d17f8SCornelia Huck     } else {
6086e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
609bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
610bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
611bb9d17f8SCornelia Huck     }
6126e790746SPaolo Bonzini 
6136e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
6146e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
6156e790746SPaolo Bonzini 
6166e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
617d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
618d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
6196e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
6206e790746SPaolo Bonzini         }
6216e790746SPaolo Bonzini     }
6226e790746SPaolo Bonzini }
6236e790746SPaolo Bonzini 
6242eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n)
6252eef278bSMichael S. Tsirkin {
6262eef278bSMichael S. Tsirkin     NetClientState *peer = n->nic_conf.peers.ncs[0];
6272eef278bSMichael S. Tsirkin 
6282eef278bSMichael S. Tsirkin     /*
6292eef278bSMichael S. Tsirkin      * Backends other than vhost-user don't support max queue size.
6302eef278bSMichael S. Tsirkin      */
6312eef278bSMichael S. Tsirkin     if (!peer) {
6322eef278bSMichael S. Tsirkin         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
6332eef278bSMichael S. Tsirkin     }
6342eef278bSMichael S. Tsirkin 
6352eef278bSMichael S. Tsirkin     if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
6362eef278bSMichael S. Tsirkin         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
6372eef278bSMichael S. Tsirkin     }
6382eef278bSMichael S. Tsirkin 
6392eef278bSMichael S. Tsirkin     return VIRTQUEUE_MAX_SIZE;
6402eef278bSMichael S. Tsirkin }
6412eef278bSMichael S. Tsirkin 
6426e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
6436e790746SPaolo Bonzini {
6446e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
6456e790746SPaolo Bonzini 
6466e790746SPaolo Bonzini     if (!nc->peer) {
6476e790746SPaolo Bonzini         return 0;
6486e790746SPaolo Bonzini     }
6496e790746SPaolo Bonzini 
650f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
6517263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 1);
6527263a0adSChangchun Ouyang     }
6537263a0adSChangchun Ouyang 
654f394b2e2SEric Blake     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
6556e790746SPaolo Bonzini         return 0;
6566e790746SPaolo Bonzini     }
6576e790746SPaolo Bonzini 
6581074b879SJason Wang     if (n->max_queues == 1) {
6591074b879SJason Wang         return 0;
6601074b879SJason Wang     }
6611074b879SJason Wang 
6626e790746SPaolo Bonzini     return tap_enable(nc->peer);
6636e790746SPaolo Bonzini }
6646e790746SPaolo Bonzini 
6656e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
6666e790746SPaolo Bonzini {
6676e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
6686e790746SPaolo Bonzini 
6696e790746SPaolo Bonzini     if (!nc->peer) {
6706e790746SPaolo Bonzini         return 0;
6716e790746SPaolo Bonzini     }
6726e790746SPaolo Bonzini 
673f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
6747263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 0);
6757263a0adSChangchun Ouyang     }
6767263a0adSChangchun Ouyang 
677f394b2e2SEric Blake     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
6786e790746SPaolo Bonzini         return 0;
6796e790746SPaolo Bonzini     }
6806e790746SPaolo Bonzini 
6816e790746SPaolo Bonzini     return tap_disable(nc->peer);
6826e790746SPaolo Bonzini }
6836e790746SPaolo Bonzini 
6846e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n)
6856e790746SPaolo Bonzini {
6866e790746SPaolo Bonzini     int i;
687ddfa83eaSJoel Stanley     int r;
6886e790746SPaolo Bonzini 
68968b5f314SYuri Benditovich     if (n->nic->peer_deleted) {
69068b5f314SYuri Benditovich         return;
69168b5f314SYuri Benditovich     }
69268b5f314SYuri Benditovich 
6936e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
6946e790746SPaolo Bonzini         if (i < n->curr_queues) {
695ddfa83eaSJoel Stanley             r = peer_attach(n, i);
696ddfa83eaSJoel Stanley             assert(!r);
6976e790746SPaolo Bonzini         } else {
698ddfa83eaSJoel Stanley             r = peer_detach(n, i);
699ddfa83eaSJoel Stanley             assert(!r);
7006e790746SPaolo Bonzini         }
7016e790746SPaolo Bonzini     }
7026e790746SPaolo Bonzini }
7036e790746SPaolo Bonzini 
704ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
7056e790746SPaolo Bonzini 
7069d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
7079d5b731dSJason Wang                                         Error **errp)
7086e790746SPaolo Bonzini {
70917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
7106e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
7116e790746SPaolo Bonzini 
712da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
713da3e8a23SShannon Zhao     features |= n->host_features;
714da3e8a23SShannon Zhao 
7150cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7166e790746SPaolo Bonzini 
7176e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
7180cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
7190cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
7200cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
7210cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
7226e790746SPaolo Bonzini 
7230cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
7240cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
7250cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
7260cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
727e22f0603SYuri Benditovich 
728e22f0603SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
7296e790746SPaolo Bonzini     }
7306e790746SPaolo Bonzini 
7316e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
7320cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
7330cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
7346e790746SPaolo Bonzini     }
7356e790746SPaolo Bonzini 
736ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
7376e790746SPaolo Bonzini         return features;
7386e790746SPaolo Bonzini     }
7392974e916SYuri Benditovich 
740*0145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
74159079029SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
742*0145c393SAndrew Melnychenko     }
74375ebec11SMaxime Coquelin     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
74475ebec11SMaxime Coquelin     vdev->backend_features = features;
74575ebec11SMaxime Coquelin 
74675ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
74775ebec11SMaxime Coquelin             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
74875ebec11SMaxime Coquelin         features |= (1ULL << VIRTIO_NET_F_MTU);
74975ebec11SMaxime Coquelin     }
75075ebec11SMaxime Coquelin 
75175ebec11SMaxime Coquelin     return features;
7526e790746SPaolo Bonzini }
7536e790746SPaolo Bonzini 
754019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
7556e790746SPaolo Bonzini {
756019a3edbSGerd Hoffmann     uint64_t features = 0;
7576e790746SPaolo Bonzini 
7586e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
7596e790746SPaolo Bonzini      * but also these: */
7600cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7610cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
7620cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
7630cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
7640cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
7656e790746SPaolo Bonzini 
7666e790746SPaolo Bonzini     return features;
7676e790746SPaolo Bonzini }
7686e790746SPaolo Bonzini 
769644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
770644c9858SDmitry Fleytman {
771ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
772644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
773644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
774644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
775644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
776644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
777644c9858SDmitry Fleytman }
778644c9858SDmitry Fleytman 
779644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
780644c9858SDmitry Fleytman {
781644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
782644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
783644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
784644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
785644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
786644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
787644c9858SDmitry Fleytman 
788644c9858SDmitry Fleytman     return guest_offloads_mask & features;
789644c9858SDmitry Fleytman }
790644c9858SDmitry Fleytman 
791644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
792644c9858SDmitry Fleytman {
793644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
794644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
795644c9858SDmitry Fleytman }
796644c9858SDmitry Fleytman 
797f5e1847bSJuan Quintela typedef struct {
798f5e1847bSJuan Quintela     VirtIONet *n;
799f5e1847bSJuan Quintela     char *id;
800f5e1847bSJuan Quintela } FailoverId;
801f5e1847bSJuan Quintela 
802f5e1847bSJuan Quintela /**
803f5e1847bSJuan Quintela  * Set the id of the failover primary device
804f5e1847bSJuan Quintela  *
805f5e1847bSJuan Quintela  * @opaque: FailoverId to setup
806f5e1847bSJuan Quintela  * @opts: opts for device we are handling
807f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
808f5e1847bSJuan Quintela  */
809f5e1847bSJuan Quintela static int failover_set_primary(void *opaque, QemuOpts *opts, Error **errp)
810f5e1847bSJuan Quintela {
811f5e1847bSJuan Quintela     FailoverId *fid = opaque;
812f5e1847bSJuan Quintela     const char *standby_id = qemu_opt_get(opts, "failover_pair_id");
813f5e1847bSJuan Quintela 
814f5e1847bSJuan Quintela     if (g_strcmp0(standby_id, fid->n->netclient_name) == 0) {
815f5e1847bSJuan Quintela         fid->id = g_strdup(opts->id);
816f5e1847bSJuan Quintela         return 1;
817f5e1847bSJuan Quintela     }
818f5e1847bSJuan Quintela 
819f5e1847bSJuan Quintela     return 0;
820f5e1847bSJuan Quintela }
821f5e1847bSJuan Quintela 
822f5e1847bSJuan Quintela /**
823f5e1847bSJuan Quintela  * Find the primary device id for this failover virtio-net
824f5e1847bSJuan Quintela  *
825f5e1847bSJuan Quintela  * @n: VirtIONet device
826f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
827f5e1847bSJuan Quintela  */
828f5e1847bSJuan Quintela static char *failover_find_primary_device_id(VirtIONet *n)
829f5e1847bSJuan Quintela {
830f5e1847bSJuan Quintela     Error *err = NULL;
831f5e1847bSJuan Quintela     FailoverId fid;
832f5e1847bSJuan Quintela 
8333abad4a2SJuan Quintela     fid.n = n;
834f5e1847bSJuan Quintela     if (!qemu_opts_foreach(qemu_find_opts("device"),
835f5e1847bSJuan Quintela                            failover_set_primary, &fid, &err)) {
836f5e1847bSJuan Quintela         return NULL;
837f5e1847bSJuan Quintela     }
838f5e1847bSJuan Quintela     return fid.id;
839f5e1847bSJuan Quintela }
840f5e1847bSJuan Quintela 
84185d3b931SJuan Quintela /**
84285d3b931SJuan Quintela  * Find the primary device for this failover virtio-net
84385d3b931SJuan Quintela  *
84485d3b931SJuan Quintela  * @n: VirtIONet device
84585d3b931SJuan Quintela  * @errp: returns an error if this function fails
84685d3b931SJuan Quintela  */
8470a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n)
8489711cd0dSJens Freimann {
849f5e1847bSJuan Quintela     char *id = failover_find_primary_device_id(n);
8509711cd0dSJens Freimann 
851f5e1847bSJuan Quintela     if (!id) {
8529711cd0dSJens Freimann         return NULL;
8539711cd0dSJens Freimann     }
854f5e1847bSJuan Quintela 
8553abad4a2SJuan Quintela     return qdev_find_recursive(sysbus_get_default(), id);
8569711cd0dSJens Freimann }
8579711cd0dSJens Freimann 
85821e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp)
85921e8709bSJuan Quintela {
86021e8709bSJuan Quintela     Error *err = NULL;
86121e8709bSJuan Quintela     QemuOpts *opts;
86221e8709bSJuan Quintela     char *id;
86321e8709bSJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
86421e8709bSJuan Quintela 
86521e8709bSJuan Quintela     if (dev) {
86621e8709bSJuan Quintela         return;
86721e8709bSJuan Quintela     }
86821e8709bSJuan Quintela 
86921e8709bSJuan Quintela     id = failover_find_primary_device_id(n);
87021e8709bSJuan Quintela     if (!id) {
87197ca9c59SLaurent Vivier         error_setg(errp, "Primary device not found");
87297ca9c59SLaurent Vivier         error_append_hint(errp, "Virtio-net failover will not work. Make "
87397ca9c59SLaurent Vivier                           "sure primary device has parameter"
87497ca9c59SLaurent Vivier                           " failover_pair_id=%s\n", n->netclient_name);
87521e8709bSJuan Quintela         return;
87621e8709bSJuan Quintela     }
87721e8709bSJuan Quintela     opts = qemu_opts_find(qemu_find_opts("device"), id);
87897ca9c59SLaurent Vivier     g_assert(opts); /* cannot be NULL because id was found using opts list */
87921e8709bSJuan Quintela     dev = qdev_device_add(opts, &err);
88021e8709bSJuan Quintela     if (err) {
88121e8709bSJuan Quintela         qemu_opts_del(opts);
88200e7b129SLaurent Vivier     } else {
88300e7b129SLaurent Vivier         object_unref(OBJECT(dev));
88421e8709bSJuan Quintela     }
88521e8709bSJuan Quintela     error_propagate(errp, err);
88621e8709bSJuan Quintela }
88721e8709bSJuan Quintela 
888d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
8896e790746SPaolo Bonzini {
89017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
8919711cd0dSJens Freimann     Error *err = NULL;
8926e790746SPaolo Bonzini     int i;
8936e790746SPaolo Bonzini 
89475ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
89575ebec11SMaxime Coquelin             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
89675ebec11SMaxime Coquelin         features &= ~(1ULL << VIRTIO_NET_F_MTU);
89775ebec11SMaxime Coquelin     }
89875ebec11SMaxime Coquelin 
899ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
90059079029SYuri Benditovich                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
90195129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
9026e790746SPaolo Bonzini 
903ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
90495129d6fSCornelia Huck                                virtio_has_feature(features,
905bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
90695129d6fSCornelia Huck                                virtio_has_feature(features,
907e22f0603SYuri Benditovich                                                   VIRTIO_F_VERSION_1),
908e22f0603SYuri Benditovich                                virtio_has_feature(features,
909e22f0603SYuri Benditovich                                                   VIRTIO_NET_F_HASH_REPORT));
9106e790746SPaolo Bonzini 
9112974e916SYuri Benditovich     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9122974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
9132974e916SYuri Benditovich     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9142974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
915e22f0603SYuri Benditovich     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
9162974e916SYuri Benditovich 
9176e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
918644c9858SDmitry Fleytman         n->curr_guest_offloads =
919644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
920644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
9216e790746SPaolo Bonzini     }
9226e790746SPaolo Bonzini 
9236e790746SPaolo Bonzini     for (i = 0;  i < n->max_queues; i++) {
9246e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
9256e790746SPaolo Bonzini 
926ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
9276e790746SPaolo Bonzini             continue;
9286e790746SPaolo Bonzini         }
929ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
9306e790746SPaolo Bonzini     }
9310b1eaa88SStefan Fritsch 
93295129d6fSCornelia Huck     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
9330b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
9340b1eaa88SStefan Fritsch     } else {
9350b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
9360b1eaa88SStefan Fritsch     }
9379711cd0dSJens Freimann 
9389711cd0dSJens Freimann     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
9399711cd0dSJens Freimann         qapi_event_send_failover_negotiated(n->netclient_name);
940e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, false);
9419711cd0dSJens Freimann         failover_add_primary(n, &err);
9429711cd0dSJens Freimann         if (err) {
9439711cd0dSJens Freimann             warn_report_err(err);
9449711cd0dSJens Freimann         }
9456e790746SPaolo Bonzini     }
94621e8709bSJuan Quintela }
9476e790746SPaolo Bonzini 
9486e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
9496e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
9506e790746SPaolo Bonzini {
9516e790746SPaolo Bonzini     uint8_t on;
9526e790746SPaolo Bonzini     size_t s;
953b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
9546e790746SPaolo Bonzini 
9556e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
9566e790746SPaolo Bonzini     if (s != sizeof(on)) {
9576e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
9586e790746SPaolo Bonzini     }
9596e790746SPaolo Bonzini 
9606e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
9616e790746SPaolo Bonzini         n->promisc = on;
9626e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
9636e790746SPaolo Bonzini         n->allmulti = on;
9646e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
9656e790746SPaolo Bonzini         n->alluni = on;
9666e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
9676e790746SPaolo Bonzini         n->nomulti = on;
9686e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
9696e790746SPaolo Bonzini         n->nouni = on;
9706e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
9716e790746SPaolo Bonzini         n->nobcast = on;
9726e790746SPaolo Bonzini     } else {
9736e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
9746e790746SPaolo Bonzini     }
9756e790746SPaolo Bonzini 
976b1be4280SAmos Kong     rxfilter_notify(nc);
977b1be4280SAmos Kong 
9786e790746SPaolo Bonzini     return VIRTIO_NET_OK;
9796e790746SPaolo Bonzini }
9806e790746SPaolo Bonzini 
981644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
982644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
983644c9858SDmitry Fleytman {
984644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
985644c9858SDmitry Fleytman     uint64_t offloads;
986644c9858SDmitry Fleytman     size_t s;
987644c9858SDmitry Fleytman 
98895129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
989644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
990644c9858SDmitry Fleytman     }
991644c9858SDmitry Fleytman 
992644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
993644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
994644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
995644c9858SDmitry Fleytman     }
996644c9858SDmitry Fleytman 
997644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
998644c9858SDmitry Fleytman         uint64_t supported_offloads;
999644c9858SDmitry Fleytman 
1000189ae6bbSJason Wang         offloads = virtio_ldq_p(vdev, &offloads);
1001189ae6bbSJason Wang 
1002644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
1003644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1004644c9858SDmitry Fleytman         }
1005644c9858SDmitry Fleytman 
10062974e916SYuri Benditovich         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10072974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
10082974e916SYuri Benditovich         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10092974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
10102974e916SYuri Benditovich         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
10112974e916SYuri Benditovich 
1012644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
1013644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
1014644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1015644c9858SDmitry Fleytman         }
1016644c9858SDmitry Fleytman 
1017644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
1018644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1019644c9858SDmitry Fleytman 
1020644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
1021644c9858SDmitry Fleytman     } else {
1022644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1023644c9858SDmitry Fleytman     }
1024644c9858SDmitry Fleytman }
1025644c9858SDmitry Fleytman 
10266e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
10276e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
10286e790746SPaolo Bonzini {
10291399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
10306e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
10316e790746SPaolo Bonzini     size_t s;
1032b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
10336e790746SPaolo Bonzini 
10346e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
10356e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
10366e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
10376e790746SPaolo Bonzini         }
10386e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
10396e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
10406e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1041b1be4280SAmos Kong         rxfilter_notify(nc);
1042b1be4280SAmos Kong 
10436e790746SPaolo Bonzini         return VIRTIO_NET_OK;
10446e790746SPaolo Bonzini     }
10456e790746SPaolo Bonzini 
10466e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
10476e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10486e790746SPaolo Bonzini     }
10496e790746SPaolo Bonzini 
1050cae2e556SAmos Kong     int in_use = 0;
1051cae2e556SAmos Kong     int first_multi = 0;
1052cae2e556SAmos Kong     uint8_t uni_overflow = 0;
1053cae2e556SAmos Kong     uint8_t multi_overflow = 0;
1054cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
10556e790746SPaolo Bonzini 
10566e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
10576e790746SPaolo Bonzini                    sizeof(mac_data.entries));
10581399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
10596e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1060b1be4280SAmos Kong         goto error;
10616e790746SPaolo Bonzini     }
10626e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
10636e790746SPaolo Bonzini 
10646e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1065b1be4280SAmos Kong         goto error;
10666e790746SPaolo Bonzini     }
10676e790746SPaolo Bonzini 
10686e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1069cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
10706e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
10716e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1072b1be4280SAmos Kong             goto error;
10736e790746SPaolo Bonzini         }
1074cae2e556SAmos Kong         in_use += mac_data.entries;
10756e790746SPaolo Bonzini     } else {
1076cae2e556SAmos Kong         uni_overflow = 1;
10776e790746SPaolo Bonzini     }
10786e790746SPaolo Bonzini 
10796e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
10806e790746SPaolo Bonzini 
1081cae2e556SAmos Kong     first_multi = in_use;
10826e790746SPaolo Bonzini 
10836e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
10846e790746SPaolo Bonzini                    sizeof(mac_data.entries));
10851399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
10866e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1087b1be4280SAmos Kong         goto error;
10886e790746SPaolo Bonzini     }
10896e790746SPaolo Bonzini 
10906e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
10916e790746SPaolo Bonzini 
10926e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1093b1be4280SAmos Kong         goto error;
10946e790746SPaolo Bonzini     }
10956e790746SPaolo Bonzini 
1096edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1097cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
10986e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
10996e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1100b1be4280SAmos Kong             goto error;
11016e790746SPaolo Bonzini         }
1102cae2e556SAmos Kong         in_use += mac_data.entries;
11036e790746SPaolo Bonzini     } else {
1104cae2e556SAmos Kong         multi_overflow = 1;
11056e790746SPaolo Bonzini     }
11066e790746SPaolo Bonzini 
1107cae2e556SAmos Kong     n->mac_table.in_use = in_use;
1108cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
1109cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
1110cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
1111cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1112cae2e556SAmos Kong     g_free(macs);
1113b1be4280SAmos Kong     rxfilter_notify(nc);
1114b1be4280SAmos Kong 
11156e790746SPaolo Bonzini     return VIRTIO_NET_OK;
1116b1be4280SAmos Kong 
1117b1be4280SAmos Kong error:
1118cae2e556SAmos Kong     g_free(macs);
1119b1be4280SAmos Kong     return VIRTIO_NET_ERR;
11206e790746SPaolo Bonzini }
11216e790746SPaolo Bonzini 
11226e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
11236e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
11246e790746SPaolo Bonzini {
11251399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11266e790746SPaolo Bonzini     uint16_t vid;
11276e790746SPaolo Bonzini     size_t s;
1128b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11296e790746SPaolo Bonzini 
11306e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
11311399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
11326e790746SPaolo Bonzini     if (s != sizeof(vid)) {
11336e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11346e790746SPaolo Bonzini     }
11356e790746SPaolo Bonzini 
11366e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
11376e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11386e790746SPaolo Bonzini 
11396e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
11406e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
11416e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
11426e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
11436e790746SPaolo Bonzini     else
11446e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11456e790746SPaolo Bonzini 
1146b1be4280SAmos Kong     rxfilter_notify(nc);
1147b1be4280SAmos Kong 
11486e790746SPaolo Bonzini     return VIRTIO_NET_OK;
11496e790746SPaolo Bonzini }
11506e790746SPaolo Bonzini 
1151f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1152f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
1153f57fcf70SJason Wang {
11549d8c6a25SDr. David Alan Gilbert     trace_virtio_net_handle_announce(n->announce_timer.round);
1155f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1156f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
1157f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
11589d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
11599d8c6a25SDr. David Alan Gilbert             qemu_announce_timer_step(&n->announce_timer);
1160f57fcf70SJason Wang         }
1161f57fcf70SJason Wang         return VIRTIO_NET_OK;
1162f57fcf70SJason Wang     } else {
1163f57fcf70SJason Wang         return VIRTIO_NET_ERR;
1164f57fcf70SJason Wang     }
1165f57fcf70SJason Wang }
1166f57fcf70SJason Wang 
1167*0145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n);
1168*0145c393SAndrew Melnychenko 
116959079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n)
117059079029SYuri Benditovich {
117159079029SYuri Benditovich     if (n->rss_data.enabled) {
117259079029SYuri Benditovich         trace_virtio_net_rss_disable();
117359079029SYuri Benditovich     }
117459079029SYuri Benditovich     n->rss_data.enabled = false;
1175*0145c393SAndrew Melnychenko 
1176*0145c393SAndrew Melnychenko     virtio_net_detach_epbf_rss(n);
1177*0145c393SAndrew Melnychenko }
1178*0145c393SAndrew Melnychenko 
1179*0145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd)
1180*0145c393SAndrew Melnychenko {
1181*0145c393SAndrew Melnychenko     NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0);
1182*0145c393SAndrew Melnychenko     if (nc == NULL || nc->info->set_steering_ebpf == NULL) {
1183*0145c393SAndrew Melnychenko         return false;
1184*0145c393SAndrew Melnychenko     }
1185*0145c393SAndrew Melnychenko 
1186*0145c393SAndrew Melnychenko     return nc->info->set_steering_ebpf(nc, prog_fd);
1187*0145c393SAndrew Melnychenko }
1188*0145c393SAndrew Melnychenko 
1189*0145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data,
1190*0145c393SAndrew Melnychenko                                    struct EBPFRSSConfig *config)
1191*0145c393SAndrew Melnychenko {
1192*0145c393SAndrew Melnychenko     config->redirect = data->redirect;
1193*0145c393SAndrew Melnychenko     config->populate_hash = data->populate_hash;
1194*0145c393SAndrew Melnychenko     config->hash_types = data->hash_types;
1195*0145c393SAndrew Melnychenko     config->indirections_len = data->indirections_len;
1196*0145c393SAndrew Melnychenko     config->default_queue = data->default_queue;
1197*0145c393SAndrew Melnychenko }
1198*0145c393SAndrew Melnychenko 
1199*0145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n)
1200*0145c393SAndrew Melnychenko {
1201*0145c393SAndrew Melnychenko     struct EBPFRSSConfig config = {};
1202*0145c393SAndrew Melnychenko 
1203*0145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
1204*0145c393SAndrew Melnychenko         return false;
1205*0145c393SAndrew Melnychenko     }
1206*0145c393SAndrew Melnychenko 
1207*0145c393SAndrew Melnychenko     rss_data_to_rss_config(&n->rss_data, &config);
1208*0145c393SAndrew Melnychenko 
1209*0145c393SAndrew Melnychenko     if (!ebpf_rss_set_all(&n->ebpf_rss, &config,
1210*0145c393SAndrew Melnychenko                           n->rss_data.indirections_table, n->rss_data.key)) {
1211*0145c393SAndrew Melnychenko         return false;
1212*0145c393SAndrew Melnychenko     }
1213*0145c393SAndrew Melnychenko 
1214*0145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) {
1215*0145c393SAndrew Melnychenko         return false;
1216*0145c393SAndrew Melnychenko     }
1217*0145c393SAndrew Melnychenko 
1218*0145c393SAndrew Melnychenko     return true;
1219*0145c393SAndrew Melnychenko }
1220*0145c393SAndrew Melnychenko 
1221*0145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n)
1222*0145c393SAndrew Melnychenko {
1223*0145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
1224*0145c393SAndrew Melnychenko }
1225*0145c393SAndrew Melnychenko 
1226*0145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n)
1227*0145c393SAndrew Melnychenko {
1228*0145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
1229*0145c393SAndrew Melnychenko         /* backend does't support steering ebpf */
1230*0145c393SAndrew Melnychenko         return false;
1231*0145c393SAndrew Melnychenko     }
1232*0145c393SAndrew Melnychenko 
1233*0145c393SAndrew Melnychenko     return ebpf_rss_load(&n->ebpf_rss);
1234*0145c393SAndrew Melnychenko }
1235*0145c393SAndrew Melnychenko 
1236*0145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n)
1237*0145c393SAndrew Melnychenko {
1238*0145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
1239*0145c393SAndrew Melnychenko     ebpf_rss_unload(&n->ebpf_rss);
124059079029SYuri Benditovich }
124159079029SYuri Benditovich 
124259079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n,
1243e22f0603SYuri Benditovich                                       struct iovec *iov,
1244e22f0603SYuri Benditovich                                       unsigned int iov_cnt,
1245e22f0603SYuri Benditovich                                       bool do_rss)
124659079029SYuri Benditovich {
124759079029SYuri Benditovich     VirtIODevice *vdev = VIRTIO_DEVICE(n);
124859079029SYuri Benditovich     struct virtio_net_rss_config cfg;
124959079029SYuri Benditovich     size_t s, offset = 0, size_get;
125059079029SYuri Benditovich     uint16_t queues, i;
125159079029SYuri Benditovich     struct {
125259079029SYuri Benditovich         uint16_t us;
125359079029SYuri Benditovich         uint8_t b;
125459079029SYuri Benditovich     } QEMU_PACKED temp;
125559079029SYuri Benditovich     const char *err_msg = "";
125659079029SYuri Benditovich     uint32_t err_value = 0;
125759079029SYuri Benditovich 
1258e22f0603SYuri Benditovich     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
125959079029SYuri Benditovich         err_msg = "RSS is not negotiated";
126059079029SYuri Benditovich         goto error;
126159079029SYuri Benditovich     }
1262e22f0603SYuri Benditovich     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1263e22f0603SYuri Benditovich         err_msg = "Hash report is not negotiated";
1264e22f0603SYuri Benditovich         goto error;
1265e22f0603SYuri Benditovich     }
126659079029SYuri Benditovich     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
126759079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
126859079029SYuri Benditovich     if (s != size_get) {
126959079029SYuri Benditovich         err_msg = "Short command buffer";
127059079029SYuri Benditovich         err_value = (uint32_t)s;
127159079029SYuri Benditovich         goto error;
127259079029SYuri Benditovich     }
127359079029SYuri Benditovich     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
127459079029SYuri Benditovich     n->rss_data.indirections_len =
127559079029SYuri Benditovich         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
127659079029SYuri Benditovich     n->rss_data.indirections_len++;
1277e22f0603SYuri Benditovich     if (!do_rss) {
1278e22f0603SYuri Benditovich         n->rss_data.indirections_len = 1;
1279e22f0603SYuri Benditovich     }
128059079029SYuri Benditovich     if (!is_power_of_2(n->rss_data.indirections_len)) {
128159079029SYuri Benditovich         err_msg = "Invalid size of indirection table";
128259079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
128359079029SYuri Benditovich         goto error;
128459079029SYuri Benditovich     }
128559079029SYuri Benditovich     if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
128659079029SYuri Benditovich         err_msg = "Too large indirection table";
128759079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
128859079029SYuri Benditovich         goto error;
128959079029SYuri Benditovich     }
1290e22f0603SYuri Benditovich     n->rss_data.default_queue = do_rss ?
1291e22f0603SYuri Benditovich         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
129259079029SYuri Benditovich     if (n->rss_data.default_queue >= n->max_queues) {
129359079029SYuri Benditovich         err_msg = "Invalid default queue";
129459079029SYuri Benditovich         err_value = n->rss_data.default_queue;
129559079029SYuri Benditovich         goto error;
129659079029SYuri Benditovich     }
129759079029SYuri Benditovich     offset += size_get;
129859079029SYuri Benditovich     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
129959079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
130059079029SYuri Benditovich     n->rss_data.indirections_table = g_malloc(size_get);
130159079029SYuri Benditovich     if (!n->rss_data.indirections_table) {
130259079029SYuri Benditovich         err_msg = "Can't allocate indirections table";
130359079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
130459079029SYuri Benditovich         goto error;
130559079029SYuri Benditovich     }
130659079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset,
130759079029SYuri Benditovich                    n->rss_data.indirections_table, size_get);
130859079029SYuri Benditovich     if (s != size_get) {
130959079029SYuri Benditovich         err_msg = "Short indirection table buffer";
131059079029SYuri Benditovich         err_value = (uint32_t)s;
131159079029SYuri Benditovich         goto error;
131259079029SYuri Benditovich     }
131359079029SYuri Benditovich     for (i = 0; i < n->rss_data.indirections_len; ++i) {
131459079029SYuri Benditovich         uint16_t val = n->rss_data.indirections_table[i];
131559079029SYuri Benditovich         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
131659079029SYuri Benditovich     }
131759079029SYuri Benditovich     offset += size_get;
131859079029SYuri Benditovich     size_get = sizeof(temp);
131959079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
132059079029SYuri Benditovich     if (s != size_get) {
132159079029SYuri Benditovich         err_msg = "Can't get queues";
132259079029SYuri Benditovich         err_value = (uint32_t)s;
132359079029SYuri Benditovich         goto error;
132459079029SYuri Benditovich     }
1325e22f0603SYuri Benditovich     queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues;
132659079029SYuri Benditovich     if (queues == 0 || queues > n->max_queues) {
132759079029SYuri Benditovich         err_msg = "Invalid number of queues";
132859079029SYuri Benditovich         err_value = queues;
132959079029SYuri Benditovich         goto error;
133059079029SYuri Benditovich     }
133159079029SYuri Benditovich     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
133259079029SYuri Benditovich         err_msg = "Invalid key size";
133359079029SYuri Benditovich         err_value = temp.b;
133459079029SYuri Benditovich         goto error;
133559079029SYuri Benditovich     }
133659079029SYuri Benditovich     if (!temp.b && n->rss_data.hash_types) {
133759079029SYuri Benditovich         err_msg = "No key provided";
133859079029SYuri Benditovich         err_value = 0;
133959079029SYuri Benditovich         goto error;
134059079029SYuri Benditovich     }
134159079029SYuri Benditovich     if (!temp.b && !n->rss_data.hash_types) {
134259079029SYuri Benditovich         virtio_net_disable_rss(n);
134359079029SYuri Benditovich         return queues;
134459079029SYuri Benditovich     }
134559079029SYuri Benditovich     offset += size_get;
134659079029SYuri Benditovich     size_get = temp.b;
134759079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
134859079029SYuri Benditovich     if (s != size_get) {
134959079029SYuri Benditovich         err_msg = "Can get key buffer";
135059079029SYuri Benditovich         err_value = (uint32_t)s;
135159079029SYuri Benditovich         goto error;
135259079029SYuri Benditovich     }
135359079029SYuri Benditovich     n->rss_data.enabled = true;
1354*0145c393SAndrew Melnychenko 
1355*0145c393SAndrew Melnychenko     if (!n->rss_data.populate_hash) {
1356*0145c393SAndrew Melnychenko         if (!virtio_net_attach_epbf_rss(n)) {
1357*0145c393SAndrew Melnychenko             /* EBPF must be loaded for vhost */
1358*0145c393SAndrew Melnychenko             if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
1359*0145c393SAndrew Melnychenko                 warn_report("Can't load eBPF RSS for vhost");
1360*0145c393SAndrew Melnychenko                 goto error;
1361*0145c393SAndrew Melnychenko             }
1362*0145c393SAndrew Melnychenko             /* fallback to software RSS */
1363*0145c393SAndrew Melnychenko             warn_report("Can't load eBPF RSS - fallback to software RSS");
1364*0145c393SAndrew Melnychenko             n->rss_data.enabled_software_rss = true;
1365*0145c393SAndrew Melnychenko         }
1366*0145c393SAndrew Melnychenko     } else {
1367*0145c393SAndrew Melnychenko         /* use software RSS for hash populating */
1368*0145c393SAndrew Melnychenko         /* and detach eBPF if was loaded before */
1369*0145c393SAndrew Melnychenko         virtio_net_detach_epbf_rss(n);
1370*0145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = true;
1371*0145c393SAndrew Melnychenko     }
1372*0145c393SAndrew Melnychenko 
137359079029SYuri Benditovich     trace_virtio_net_rss_enable(n->rss_data.hash_types,
137459079029SYuri Benditovich                                 n->rss_data.indirections_len,
137559079029SYuri Benditovich                                 temp.b);
137659079029SYuri Benditovich     return queues;
137759079029SYuri Benditovich error:
137859079029SYuri Benditovich     trace_virtio_net_rss_error(err_msg, err_value);
137959079029SYuri Benditovich     virtio_net_disable_rss(n);
138059079029SYuri Benditovich     return 0;
138159079029SYuri Benditovich }
138259079029SYuri Benditovich 
13836e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
13846e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
13856e790746SPaolo Bonzini {
138617a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13876e790746SPaolo Bonzini     uint16_t queues;
13886e790746SPaolo Bonzini 
138959079029SYuri Benditovich     virtio_net_disable_rss(n);
1390e22f0603SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1391e22f0603SYuri Benditovich         queues = virtio_net_handle_rss(n, iov, iov_cnt, false);
1392e22f0603SYuri Benditovich         return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1393e22f0603SYuri Benditovich     }
139459079029SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1395e22f0603SYuri Benditovich         queues = virtio_net_handle_rss(n, iov, iov_cnt, true);
139659079029SYuri Benditovich     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
139759079029SYuri Benditovich         struct virtio_net_ctrl_mq mq;
139859079029SYuri Benditovich         size_t s;
139959079029SYuri Benditovich         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
140059079029SYuri Benditovich             return VIRTIO_NET_ERR;
140159079029SYuri Benditovich         }
14026e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
14036e790746SPaolo Bonzini         if (s != sizeof(mq)) {
14046e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
14056e790746SPaolo Bonzini         }
140659079029SYuri Benditovich         queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
14076e790746SPaolo Bonzini 
140859079029SYuri Benditovich     } else {
14096e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
14106e790746SPaolo Bonzini     }
14116e790746SPaolo Bonzini 
14126e790746SPaolo Bonzini     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
14136e790746SPaolo Bonzini         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
14146e790746SPaolo Bonzini         queues > n->max_queues ||
14156e790746SPaolo Bonzini         !n->multiqueue) {
14166e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
14176e790746SPaolo Bonzini     }
14186e790746SPaolo Bonzini 
14196e790746SPaolo Bonzini     n->curr_queues = queues;
14206e790746SPaolo Bonzini     /* stop the backend before changing the number of queues to avoid handling a
14216e790746SPaolo Bonzini      * disabled queue */
142217a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
14236e790746SPaolo Bonzini     virtio_net_set_queues(n);
14246e790746SPaolo Bonzini 
14256e790746SPaolo Bonzini     return VIRTIO_NET_OK;
14266e790746SPaolo Bonzini }
1427ba7eadb5SGreg Kurz 
14286e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
14296e790746SPaolo Bonzini {
143017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
14316e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
14326e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
143351b19ebeSPaolo Bonzini     VirtQueueElement *elem;
14346e790746SPaolo Bonzini     size_t s;
1435771b6ed3SJason Wang     struct iovec *iov, *iov2;
14366e790746SPaolo Bonzini     unsigned int iov_cnt;
14376e790746SPaolo Bonzini 
143851b19ebeSPaolo Bonzini     for (;;) {
143951b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
144051b19ebeSPaolo Bonzini         if (!elem) {
144151b19ebeSPaolo Bonzini             break;
144251b19ebeSPaolo Bonzini         }
144351b19ebeSPaolo Bonzini         if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
144451b19ebeSPaolo Bonzini             iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
1445ba7eadb5SGreg Kurz             virtio_error(vdev, "virtio-net ctrl missing headers");
1446ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
1447ba7eadb5SGreg Kurz             g_free(elem);
1448ba7eadb5SGreg Kurz             break;
14496e790746SPaolo Bonzini         }
14506e790746SPaolo Bonzini 
145151b19ebeSPaolo Bonzini         iov_cnt = elem->out_num;
145251b19ebeSPaolo Bonzini         iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
14536e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
14546e790746SPaolo Bonzini         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
14556e790746SPaolo Bonzini         if (s != sizeof(ctrl)) {
14566e790746SPaolo Bonzini             status = VIRTIO_NET_ERR;
14576e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
14586e790746SPaolo Bonzini             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
14596e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
14606e790746SPaolo Bonzini             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
14616e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
14626e790746SPaolo Bonzini             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
1463f57fcf70SJason Wang         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1464f57fcf70SJason Wang             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
14656e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
14666e790746SPaolo Bonzini             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
1467644c9858SDmitry Fleytman         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1468644c9858SDmitry Fleytman             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
14696e790746SPaolo Bonzini         }
14706e790746SPaolo Bonzini 
147151b19ebeSPaolo Bonzini         s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
14726e790746SPaolo Bonzini         assert(s == sizeof(status));
14736e790746SPaolo Bonzini 
147451b19ebeSPaolo Bonzini         virtqueue_push(vq, elem, sizeof(status));
14756e790746SPaolo Bonzini         virtio_notify(vdev, vq);
1476771b6ed3SJason Wang         g_free(iov2);
147751b19ebeSPaolo Bonzini         g_free(elem);
14786e790746SPaolo Bonzini     }
14796e790746SPaolo Bonzini }
14806e790746SPaolo Bonzini 
14816e790746SPaolo Bonzini /* RX */
14826e790746SPaolo Bonzini 
14836e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
14846e790746SPaolo Bonzini {
148517a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
14866e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
14876e790746SPaolo Bonzini 
14886e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
14896e790746SPaolo Bonzini }
14906e790746SPaolo Bonzini 
1491b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc)
14926e790746SPaolo Bonzini {
14936e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
149417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
14956e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
14966e790746SPaolo Bonzini 
149717a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
1498b8c4b67eSPhilippe Mathieu-Daudé         return false;
14996e790746SPaolo Bonzini     }
15006e790746SPaolo Bonzini 
15016e790746SPaolo Bonzini     if (nc->queue_index >= n->curr_queues) {
1502b8c4b67eSPhilippe Mathieu-Daudé         return false;
15036e790746SPaolo Bonzini     }
15046e790746SPaolo Bonzini 
15056e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
150617a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1507b8c4b67eSPhilippe Mathieu-Daudé         return false;
15086e790746SPaolo Bonzini     }
15096e790746SPaolo Bonzini 
1510b8c4b67eSPhilippe Mathieu-Daudé     return true;
15116e790746SPaolo Bonzini }
15126e790746SPaolo Bonzini 
15136e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
15146e790746SPaolo Bonzini {
15156e790746SPaolo Bonzini     VirtIONet *n = q->n;
15166e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
15176e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
15186e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
15196e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
15206e790746SPaolo Bonzini 
15216e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
15226e790746SPaolo Bonzini          * available after the above check but before notification was
15236e790746SPaolo Bonzini          * enabled, check for available buffers again.
15246e790746SPaolo Bonzini          */
15256e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
15266e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
15276e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
15286e790746SPaolo Bonzini             return 0;
15296e790746SPaolo Bonzini         }
15306e790746SPaolo Bonzini     }
15316e790746SPaolo Bonzini 
15326e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
15336e790746SPaolo Bonzini     return 1;
15346e790746SPaolo Bonzini }
15356e790746SPaolo Bonzini 
15361399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1537032a74a1SCédric Le Goater {
15381399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
15391399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
15401399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
15411399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1542032a74a1SCédric Le Goater }
1543032a74a1SCédric Le Goater 
15446e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
15456e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
15466e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
15476e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
15486e790746SPaolo Bonzini  * dhclient yet.
15496e790746SPaolo Bonzini  *
15506e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
15516e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
15526e790746SPaolo Bonzini  * kernels.
15536e790746SPaolo Bonzini  *
15546e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
15556e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
15566e790746SPaolo Bonzini  * cache.
15576e790746SPaolo Bonzini  */
15586e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
15596e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
15606e790746SPaolo Bonzini {
15616e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
15626e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
15636e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
15646e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
15656e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1566f5746335SBin Meng         net_checksum_calculate(buf, size, CSUM_UDP);
15676e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
15686e790746SPaolo Bonzini     }
15696e790746SPaolo Bonzini }
15706e790746SPaolo Bonzini 
15716e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
15726e790746SPaolo Bonzini                            const void *buf, size_t size)
15736e790746SPaolo Bonzini {
15746e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
15756e790746SPaolo Bonzini         /* FIXME this cast is evil */
15766e790746SPaolo Bonzini         void *wbuf = (void *)buf;
15776e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
15786e790746SPaolo Bonzini                                     size - n->host_hdr_len);
15791bfa316cSGreg Kurz 
15801bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
15811399c60dSRusty Russell             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
15821bfa316cSGreg Kurz         }
15836e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
15846e790746SPaolo Bonzini     } else {
15856e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
15866e790746SPaolo Bonzini             .flags = 0,
15876e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
15886e790746SPaolo Bonzini         };
15896e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
15906e790746SPaolo Bonzini     }
15916e790746SPaolo Bonzini }
15926e790746SPaolo Bonzini 
15936e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
15946e790746SPaolo Bonzini {
15956e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
15966e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
15976e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
15986e790746SPaolo Bonzini     int i;
15996e790746SPaolo Bonzini 
16006e790746SPaolo Bonzini     if (n->promisc)
16016e790746SPaolo Bonzini         return 1;
16026e790746SPaolo Bonzini 
16036e790746SPaolo Bonzini     ptr += n->host_hdr_len;
16046e790746SPaolo Bonzini 
16056e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
16067542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
16076e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
16086e790746SPaolo Bonzini             return 0;
16096e790746SPaolo Bonzini     }
16106e790746SPaolo Bonzini 
16116e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
16126e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
16136e790746SPaolo Bonzini             return !n->nobcast;
16146e790746SPaolo Bonzini         } else if (n->nomulti) {
16156e790746SPaolo Bonzini             return 0;
16166e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
16176e790746SPaolo Bonzini             return 1;
16186e790746SPaolo Bonzini         }
16196e790746SPaolo Bonzini 
16206e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
16216e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
16226e790746SPaolo Bonzini                 return 1;
16236e790746SPaolo Bonzini             }
16246e790746SPaolo Bonzini         }
16256e790746SPaolo Bonzini     } else { // unicast
16266e790746SPaolo Bonzini         if (n->nouni) {
16276e790746SPaolo Bonzini             return 0;
16286e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
16296e790746SPaolo Bonzini             return 1;
16306e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
16316e790746SPaolo Bonzini             return 1;
16326e790746SPaolo Bonzini         }
16336e790746SPaolo Bonzini 
16346e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
16356e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
16366e790746SPaolo Bonzini                 return 1;
16376e790746SPaolo Bonzini             }
16386e790746SPaolo Bonzini         }
16396e790746SPaolo Bonzini     }
16406e790746SPaolo Bonzini 
16416e790746SPaolo Bonzini     return 0;
16426e790746SPaolo Bonzini }
16436e790746SPaolo Bonzini 
16444474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4,
16454474e37aSYuri Benditovich                                         bool isip6,
16464474e37aSYuri Benditovich                                         bool isudp,
16474474e37aSYuri Benditovich                                         bool istcp,
16484474e37aSYuri Benditovich                                         uint32_t types)
16494474e37aSYuri Benditovich {
16504474e37aSYuri Benditovich     if (isip4) {
16514474e37aSYuri Benditovich         if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) {
16524474e37aSYuri Benditovich             return NetPktRssIpV4Tcp;
16534474e37aSYuri Benditovich         }
16544474e37aSYuri Benditovich         if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) {
16554474e37aSYuri Benditovich             return NetPktRssIpV4Udp;
16564474e37aSYuri Benditovich         }
16574474e37aSYuri Benditovich         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
16584474e37aSYuri Benditovich             return NetPktRssIpV4;
16594474e37aSYuri Benditovich         }
16604474e37aSYuri Benditovich     } else if (isip6) {
16614474e37aSYuri Benditovich         uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
16624474e37aSYuri Benditovich                         VIRTIO_NET_RSS_HASH_TYPE_TCPv6;
16634474e37aSYuri Benditovich 
16644474e37aSYuri Benditovich         if (istcp && (types & mask)) {
16654474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ?
16664474e37aSYuri Benditovich                 NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp;
16674474e37aSYuri Benditovich         }
16684474e37aSYuri Benditovich         mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6;
16694474e37aSYuri Benditovich         if (isudp && (types & mask)) {
16704474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ?
16714474e37aSYuri Benditovich                 NetPktRssIpV6UdpEx : NetPktRssIpV6Udp;
16724474e37aSYuri Benditovich         }
16734474e37aSYuri Benditovich         mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6;
16744474e37aSYuri Benditovich         if (types & mask) {
16754474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ?
16764474e37aSYuri Benditovich                 NetPktRssIpV6Ex : NetPktRssIpV6;
16774474e37aSYuri Benditovich         }
16784474e37aSYuri Benditovich     }
16794474e37aSYuri Benditovich     return 0xff;
16804474e37aSYuri Benditovich }
16814474e37aSYuri Benditovich 
1682e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1683e22f0603SYuri Benditovich                                    uint32_t hash)
1684e22f0603SYuri Benditovich {
1685e22f0603SYuri Benditovich     struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1686e22f0603SYuri Benditovich     hdr->hash_value = hash;
1687e22f0603SYuri Benditovich     hdr->hash_report = report;
1688e22f0603SYuri Benditovich }
1689e22f0603SYuri Benditovich 
16904474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
169197cd965cSPaolo Bonzini                                   size_t size)
16926e790746SPaolo Bonzini {
16936e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1694e22f0603SYuri Benditovich     unsigned int index = nc->queue_index, new_index = index;
16954474e37aSYuri Benditovich     struct NetRxPkt *pkt = n->rx_pkt;
16964474e37aSYuri Benditovich     uint8_t net_hash_type;
16974474e37aSYuri Benditovich     uint32_t hash;
16984474e37aSYuri Benditovich     bool isip4, isip6, isudp, istcp;
1699e22f0603SYuri Benditovich     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1700e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv4,
1701e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv4,
1702e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6,
1703e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6,
1704e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1705e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1706e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv4,
1707e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6,
1708e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1709e22f0603SYuri Benditovich     };
17104474e37aSYuri Benditovich 
17114474e37aSYuri Benditovich     net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len,
17124474e37aSYuri Benditovich                              size - n->host_hdr_len);
17134474e37aSYuri Benditovich     net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
17144474e37aSYuri Benditovich     if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) {
17154474e37aSYuri Benditovich         istcp = isudp = false;
17164474e37aSYuri Benditovich     }
17174474e37aSYuri Benditovich     if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) {
17184474e37aSYuri Benditovich         istcp = isudp = false;
17194474e37aSYuri Benditovich     }
17204474e37aSYuri Benditovich     net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp,
17214474e37aSYuri Benditovich                                              n->rss_data.hash_types);
17224474e37aSYuri Benditovich     if (net_hash_type > NetPktRssIpV6UdpEx) {
1723e22f0603SYuri Benditovich         if (n->rss_data.populate_hash) {
1724e22f0603SYuri Benditovich             virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1725e22f0603SYuri Benditovich         }
1726e22f0603SYuri Benditovich         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
17274474e37aSYuri Benditovich     }
17284474e37aSYuri Benditovich 
17294474e37aSYuri Benditovich     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1730e22f0603SYuri Benditovich 
1731e22f0603SYuri Benditovich     if (n->rss_data.populate_hash) {
1732e22f0603SYuri Benditovich         virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1733e22f0603SYuri Benditovich     }
1734e22f0603SYuri Benditovich 
1735e22f0603SYuri Benditovich     if (n->rss_data.redirect) {
17364474e37aSYuri Benditovich         new_index = hash & (n->rss_data.indirections_len - 1);
17374474e37aSYuri Benditovich         new_index = n->rss_data.indirections_table[new_index];
17384474e37aSYuri Benditovich     }
1739e22f0603SYuri Benditovich 
1740e22f0603SYuri Benditovich     return (index == new_index) ? -1 : new_index;
17414474e37aSYuri Benditovich }
17424474e37aSYuri Benditovich 
17434474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
17444474e37aSYuri Benditovich                                       size_t size, bool no_rss)
17454474e37aSYuri Benditovich {
17464474e37aSYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
17476e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
174817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
17496e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
17506e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
17516e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
17526e790746SPaolo Bonzini     size_t offset, i, guest_offset;
17536e790746SPaolo Bonzini 
17546e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
17556e790746SPaolo Bonzini         return -1;
17566e790746SPaolo Bonzini     }
17576e790746SPaolo Bonzini 
1758*0145c393SAndrew Melnychenko     if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) {
17594474e37aSYuri Benditovich         int index = virtio_net_process_rss(nc, buf, size);
17604474e37aSYuri Benditovich         if (index >= 0) {
17614474e37aSYuri Benditovich             NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
17624474e37aSYuri Benditovich             return virtio_net_receive_rcu(nc2, buf, size, true);
17634474e37aSYuri Benditovich         }
17644474e37aSYuri Benditovich     }
17654474e37aSYuri Benditovich 
17666e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
17676e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
17686e790746SPaolo Bonzini         return 0;
17696e790746SPaolo Bonzini     }
17706e790746SPaolo Bonzini 
17716e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
17726e790746SPaolo Bonzini         return size;
17736e790746SPaolo Bonzini 
17746e790746SPaolo Bonzini     offset = i = 0;
17756e790746SPaolo Bonzini 
17766e790746SPaolo Bonzini     while (offset < size) {
177751b19ebeSPaolo Bonzini         VirtQueueElement *elem;
17786e790746SPaolo Bonzini         int len, total;
177951b19ebeSPaolo Bonzini         const struct iovec *sg;
17806e790746SPaolo Bonzini 
17816e790746SPaolo Bonzini         total = 0;
17826e790746SPaolo Bonzini 
178351b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
178451b19ebeSPaolo Bonzini         if (!elem) {
1785ba10b9c0SGreg Kurz             if (i) {
1786ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
17876e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1788019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1789019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
17906e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1791019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1792019a3edbSGerd Hoffmann                              vdev->guest_features);
1793ba10b9c0SGreg Kurz             }
1794ba10b9c0SGreg Kurz             return -1;
17956e790746SPaolo Bonzini         }
17966e790746SPaolo Bonzini 
179751b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1798ba10b9c0SGreg Kurz             virtio_error(vdev,
1799ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1800ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1801ba10b9c0SGreg Kurz             g_free(elem);
1802ba10b9c0SGreg Kurz             return -1;
18036e790746SPaolo Bonzini         }
18046e790746SPaolo Bonzini 
180551b19ebeSPaolo Bonzini         sg = elem->in_sg;
18066e790746SPaolo Bonzini         if (i == 0) {
18076e790746SPaolo Bonzini             assert(offset == 0);
18086e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
18096e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
181051b19ebeSPaolo Bonzini                                     sg, elem->in_num,
18116e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
18126e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
18136e790746SPaolo Bonzini             }
18146e790746SPaolo Bonzini 
181551b19ebeSPaolo Bonzini             receive_header(n, sg, elem->in_num, buf, size);
1816e22f0603SYuri Benditovich             if (n->rss_data.populate_hash) {
1817e22f0603SYuri Benditovich                 offset = sizeof(mhdr);
1818e22f0603SYuri Benditovich                 iov_from_buf(sg, elem->in_num, offset,
1819e22f0603SYuri Benditovich                              buf + offset, n->host_hdr_len - sizeof(mhdr));
1820e22f0603SYuri Benditovich             }
18216e790746SPaolo Bonzini             offset = n->host_hdr_len;
18226e790746SPaolo Bonzini             total += n->guest_hdr_len;
18236e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
18246e790746SPaolo Bonzini         } else {
18256e790746SPaolo Bonzini             guest_offset = 0;
18266e790746SPaolo Bonzini         }
18276e790746SPaolo Bonzini 
18286e790746SPaolo Bonzini         /* copy in packet.  ugh */
182951b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
18306e790746SPaolo Bonzini                            buf + offset, size - offset);
18316e790746SPaolo Bonzini         total += len;
18326e790746SPaolo Bonzini         offset += len;
18336e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
18346e790746SPaolo Bonzini          * must have consumed the complete packet.
18356e790746SPaolo Bonzini          * Otherwise, drop it. */
18366e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
183727e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
183851b19ebeSPaolo Bonzini             g_free(elem);
18396e790746SPaolo Bonzini             return size;
18406e790746SPaolo Bonzini         }
18416e790746SPaolo Bonzini 
18426e790746SPaolo Bonzini         /* signal other side */
184351b19ebeSPaolo Bonzini         virtqueue_fill(q->rx_vq, elem, total, i++);
184451b19ebeSPaolo Bonzini         g_free(elem);
18456e790746SPaolo Bonzini     }
18466e790746SPaolo Bonzini 
18476e790746SPaolo Bonzini     if (mhdr_cnt) {
18481399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
18496e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
18506e790746SPaolo Bonzini                      0,
18516e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
18526e790746SPaolo Bonzini     }
18536e790746SPaolo Bonzini 
18546e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
185517a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
18566e790746SPaolo Bonzini 
18576e790746SPaolo Bonzini     return size;
18586e790746SPaolo Bonzini }
18596e790746SPaolo Bonzini 
18602974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
186197cd965cSPaolo Bonzini                                   size_t size)
186297cd965cSPaolo Bonzini {
1863068ddfa9SDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
186497cd965cSPaolo Bonzini 
18654474e37aSYuri Benditovich     return virtio_net_receive_rcu(nc, buf, size, false);
186697cd965cSPaolo Bonzini }
186797cd965cSPaolo Bonzini 
18682974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
18692974e916SYuri Benditovich                                          const uint8_t *buf,
18702974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
18712974e916SYuri Benditovich {
18722974e916SYuri Benditovich     uint16_t ip_hdrlen;
18732974e916SYuri Benditovich     struct ip_header *ip;
18742974e916SYuri Benditovich 
18752974e916SYuri Benditovich     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
18762974e916SYuri Benditovich                               + sizeof(struct eth_header));
18772974e916SYuri Benditovich     unit->ip = (void *)ip;
18782974e916SYuri Benditovich     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
18792974e916SYuri Benditovich     unit->ip_plen = &ip->ip_len;
18802974e916SYuri Benditovich     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
18812974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
18822974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
18832974e916SYuri Benditovich }
18842974e916SYuri Benditovich 
18852974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
18862974e916SYuri Benditovich                                          const uint8_t *buf,
18872974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
18882974e916SYuri Benditovich {
18892974e916SYuri Benditovich     struct ip6_header *ip6;
18902974e916SYuri Benditovich 
18912974e916SYuri Benditovich     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
18922974e916SYuri Benditovich                                  + sizeof(struct eth_header));
18932974e916SYuri Benditovich     unit->ip = ip6;
18942974e916SYuri Benditovich     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
189578ee6bd0SPhilippe Mathieu-Daudé     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
18962974e916SYuri Benditovich                                         + sizeof(struct ip6_header));
18972974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
18982974e916SYuri Benditovich 
18992974e916SYuri Benditovich     /* There is a difference between payload lenght in ipv4 and v6,
19002974e916SYuri Benditovich        ip header is excluded in ipv6 */
19012974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
19022974e916SYuri Benditovich }
19032974e916SYuri Benditovich 
19042974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
19052974e916SYuri Benditovich                                        VirtioNetRscSeg *seg)
19062974e916SYuri Benditovich {
19072974e916SYuri Benditovich     int ret;
1908dd3d85e8SYuri Benditovich     struct virtio_net_hdr_v1 *h;
19092974e916SYuri Benditovich 
1910dd3d85e8SYuri Benditovich     h = (struct virtio_net_hdr_v1 *)seg->buf;
19112974e916SYuri Benditovich     h->flags = 0;
19122974e916SYuri Benditovich     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
19132974e916SYuri Benditovich 
19142974e916SYuri Benditovich     if (seg->is_coalesced) {
1915dd3d85e8SYuri Benditovich         h->rsc.segments = seg->packets;
1916dd3d85e8SYuri Benditovich         h->rsc.dup_acks = seg->dup_ack;
19172974e916SYuri Benditovich         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
19182974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
19192974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
19202974e916SYuri Benditovich         } else {
19212974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
19222974e916SYuri Benditovich         }
19232974e916SYuri Benditovich     }
19242974e916SYuri Benditovich 
19252974e916SYuri Benditovich     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
19262974e916SYuri Benditovich     QTAILQ_REMOVE(&chain->buffers, seg, next);
19272974e916SYuri Benditovich     g_free(seg->buf);
19282974e916SYuri Benditovich     g_free(seg);
19292974e916SYuri Benditovich 
19302974e916SYuri Benditovich     return ret;
19312974e916SYuri Benditovich }
19322974e916SYuri Benditovich 
19332974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq)
19342974e916SYuri Benditovich {
19352974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn;
19362974e916SYuri Benditovich     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
19372974e916SYuri Benditovich 
19382974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
19392974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
19402974e916SYuri Benditovich             chain->stat.purge_failed++;
19412974e916SYuri Benditovich             continue;
19422974e916SYuri Benditovich         }
19432974e916SYuri Benditovich     }
19442974e916SYuri Benditovich 
19452974e916SYuri Benditovich     chain->stat.timer++;
19462974e916SYuri Benditovich     if (!QTAILQ_EMPTY(&chain->buffers)) {
19472974e916SYuri Benditovich         timer_mod(chain->drain_timer,
19482974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
19492974e916SYuri Benditovich     }
19502974e916SYuri Benditovich }
19512974e916SYuri Benditovich 
19522974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n)
19532974e916SYuri Benditovich {
19542974e916SYuri Benditovich     VirtioNetRscChain *chain, *rn_chain;
19552974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn_seg;
19562974e916SYuri Benditovich 
19572974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
19582974e916SYuri Benditovich         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
19592974e916SYuri Benditovich             QTAILQ_REMOVE(&chain->buffers, seg, next);
19602974e916SYuri Benditovich             g_free(seg->buf);
19612974e916SYuri Benditovich             g_free(seg);
19622974e916SYuri Benditovich         }
19632974e916SYuri Benditovich 
19642974e916SYuri Benditovich         timer_free(chain->drain_timer);
19652974e916SYuri Benditovich         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
19662974e916SYuri Benditovich         g_free(chain);
19672974e916SYuri Benditovich     }
19682974e916SYuri Benditovich }
19692974e916SYuri Benditovich 
19702974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
19712974e916SYuri Benditovich                                      NetClientState *nc,
19722974e916SYuri Benditovich                                      const uint8_t *buf, size_t size)
19732974e916SYuri Benditovich {
19742974e916SYuri Benditovich     uint16_t hdr_len;
19752974e916SYuri Benditovich     VirtioNetRscSeg *seg;
19762974e916SYuri Benditovich 
19772974e916SYuri Benditovich     hdr_len = chain->n->guest_hdr_len;
19782974e916SYuri Benditovich     seg = g_malloc(sizeof(VirtioNetRscSeg));
19792974e916SYuri Benditovich     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
19802974e916SYuri Benditovich         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
19812974e916SYuri Benditovich     memcpy(seg->buf, buf, size);
19822974e916SYuri Benditovich     seg->size = size;
19832974e916SYuri Benditovich     seg->packets = 1;
19842974e916SYuri Benditovich     seg->dup_ack = 0;
19852974e916SYuri Benditovich     seg->is_coalesced = 0;
19862974e916SYuri Benditovich     seg->nc = nc;
19872974e916SYuri Benditovich 
19882974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
19892974e916SYuri Benditovich     chain->stat.cache++;
19902974e916SYuri Benditovich 
19912974e916SYuri Benditovich     switch (chain->proto) {
19922974e916SYuri Benditovich     case ETH_P_IP:
19932974e916SYuri Benditovich         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
19942974e916SYuri Benditovich         break;
19952974e916SYuri Benditovich     case ETH_P_IPV6:
19962974e916SYuri Benditovich         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
19972974e916SYuri Benditovich         break;
19982974e916SYuri Benditovich     default:
19992974e916SYuri Benditovich         g_assert_not_reached();
20002974e916SYuri Benditovich     }
20012974e916SYuri Benditovich }
20022974e916SYuri Benditovich 
20032974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
20042974e916SYuri Benditovich                                          VirtioNetRscSeg *seg,
20052974e916SYuri Benditovich                                          const uint8_t *buf,
20062974e916SYuri Benditovich                                          struct tcp_header *n_tcp,
20072974e916SYuri Benditovich                                          struct tcp_header *o_tcp)
20082974e916SYuri Benditovich {
20092974e916SYuri Benditovich     uint32_t nack, oack;
20102974e916SYuri Benditovich     uint16_t nwin, owin;
20112974e916SYuri Benditovich 
20122974e916SYuri Benditovich     nack = htonl(n_tcp->th_ack);
20132974e916SYuri Benditovich     nwin = htons(n_tcp->th_win);
20142974e916SYuri Benditovich     oack = htonl(o_tcp->th_ack);
20152974e916SYuri Benditovich     owin = htons(o_tcp->th_win);
20162974e916SYuri Benditovich 
20172974e916SYuri Benditovich     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
20182974e916SYuri Benditovich         chain->stat.ack_out_of_win++;
20192974e916SYuri Benditovich         return RSC_FINAL;
20202974e916SYuri Benditovich     } else if (nack == oack) {
20212974e916SYuri Benditovich         /* duplicated ack or window probe */
20222974e916SYuri Benditovich         if (nwin == owin) {
20232974e916SYuri Benditovich             /* duplicated ack, add dup ack count due to whql test up to 1 */
20242974e916SYuri Benditovich             chain->stat.dup_ack++;
20252974e916SYuri Benditovich             return RSC_FINAL;
20262974e916SYuri Benditovich         } else {
20272974e916SYuri Benditovich             /* Coalesce window update */
20282974e916SYuri Benditovich             o_tcp->th_win = n_tcp->th_win;
20292974e916SYuri Benditovich             chain->stat.win_update++;
20302974e916SYuri Benditovich             return RSC_COALESCE;
20312974e916SYuri Benditovich         }
20322974e916SYuri Benditovich     } else {
20332974e916SYuri Benditovich         /* pure ack, go to 'C', finalize*/
20342974e916SYuri Benditovich         chain->stat.pure_ack++;
20352974e916SYuri Benditovich         return RSC_FINAL;
20362974e916SYuri Benditovich     }
20372974e916SYuri Benditovich }
20382974e916SYuri Benditovich 
20392974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
20402974e916SYuri Benditovich                                             VirtioNetRscSeg *seg,
20412974e916SYuri Benditovich                                             const uint8_t *buf,
20422974e916SYuri Benditovich                                             VirtioNetRscUnit *n_unit)
20432974e916SYuri Benditovich {
20442974e916SYuri Benditovich     void *data;
20452974e916SYuri Benditovich     uint16_t o_ip_len;
20462974e916SYuri Benditovich     uint32_t nseq, oseq;
20472974e916SYuri Benditovich     VirtioNetRscUnit *o_unit;
20482974e916SYuri Benditovich 
20492974e916SYuri Benditovich     o_unit = &seg->unit;
20502974e916SYuri Benditovich     o_ip_len = htons(*o_unit->ip_plen);
20512974e916SYuri Benditovich     nseq = htonl(n_unit->tcp->th_seq);
20522974e916SYuri Benditovich     oseq = htonl(o_unit->tcp->th_seq);
20532974e916SYuri Benditovich 
20542974e916SYuri Benditovich     /* out of order or retransmitted. */
20552974e916SYuri Benditovich     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
20562974e916SYuri Benditovich         chain->stat.data_out_of_win++;
20572974e916SYuri Benditovich         return RSC_FINAL;
20582974e916SYuri Benditovich     }
20592974e916SYuri Benditovich 
20602974e916SYuri Benditovich     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
20612974e916SYuri Benditovich     if (nseq == oseq) {
20622974e916SYuri Benditovich         if ((o_unit->payload == 0) && n_unit->payload) {
20632974e916SYuri Benditovich             /* From no payload to payload, normal case, not a dup ack or etc */
20642974e916SYuri Benditovich             chain->stat.data_after_pure_ack++;
20652974e916SYuri Benditovich             goto coalesce;
20662974e916SYuri Benditovich         } else {
20672974e916SYuri Benditovich             return virtio_net_rsc_handle_ack(chain, seg, buf,
20682974e916SYuri Benditovich                                              n_unit->tcp, o_unit->tcp);
20692974e916SYuri Benditovich         }
20702974e916SYuri Benditovich     } else if ((nseq - oseq) != o_unit->payload) {
20712974e916SYuri Benditovich         /* Not a consistent packet, out of order */
20722974e916SYuri Benditovich         chain->stat.data_out_of_order++;
20732974e916SYuri Benditovich         return RSC_FINAL;
20742974e916SYuri Benditovich     } else {
20752974e916SYuri Benditovich coalesce:
20762974e916SYuri Benditovich         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
20772974e916SYuri Benditovich             chain->stat.over_size++;
20782974e916SYuri Benditovich             return RSC_FINAL;
20792974e916SYuri Benditovich         }
20802974e916SYuri Benditovich 
20812974e916SYuri Benditovich         /* Here comes the right data, the payload length in v4/v6 is different,
20822974e916SYuri Benditovich            so use the field value to update and record the new data len */
20832974e916SYuri Benditovich         o_unit->payload += n_unit->payload; /* update new data len */
20842974e916SYuri Benditovich 
20852974e916SYuri Benditovich         /* update field in ip header */
20862974e916SYuri Benditovich         *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
20872974e916SYuri Benditovich 
20882974e916SYuri Benditovich         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
20892974e916SYuri Benditovich            for windows guest, while this may change the behavior for linux
20902974e916SYuri Benditovich            guest (only if it uses RSC feature). */
20912974e916SYuri Benditovich         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
20922974e916SYuri Benditovich 
20932974e916SYuri Benditovich         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
20942974e916SYuri Benditovich         o_unit->tcp->th_win = n_unit->tcp->th_win;
20952974e916SYuri Benditovich 
20962974e916SYuri Benditovich         memmove(seg->buf + seg->size, data, n_unit->payload);
20972974e916SYuri Benditovich         seg->size += n_unit->payload;
20982974e916SYuri Benditovich         seg->packets++;
20992974e916SYuri Benditovich         chain->stat.coalesced++;
21002974e916SYuri Benditovich         return RSC_COALESCE;
21012974e916SYuri Benditovich     }
21022974e916SYuri Benditovich }
21032974e916SYuri Benditovich 
21042974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
21052974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
21062974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
21072974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
21082974e916SYuri Benditovich {
21092974e916SYuri Benditovich     struct ip_header *ip1, *ip2;
21102974e916SYuri Benditovich 
21112974e916SYuri Benditovich     ip1 = (struct ip_header *)(unit->ip);
21122974e916SYuri Benditovich     ip2 = (struct ip_header *)(seg->unit.ip);
21132974e916SYuri Benditovich     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
21142974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
21152974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
21162974e916SYuri Benditovich         chain->stat.no_match++;
21172974e916SYuri Benditovich         return RSC_NO_MATCH;
21182974e916SYuri Benditovich     }
21192974e916SYuri Benditovich 
21202974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
21212974e916SYuri Benditovich }
21222974e916SYuri Benditovich 
21232974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
21242974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
21252974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
21262974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
21272974e916SYuri Benditovich {
21282974e916SYuri Benditovich     struct ip6_header *ip1, *ip2;
21292974e916SYuri Benditovich 
21302974e916SYuri Benditovich     ip1 = (struct ip6_header *)(unit->ip);
21312974e916SYuri Benditovich     ip2 = (struct ip6_header *)(seg->unit.ip);
21322974e916SYuri Benditovich     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
21332974e916SYuri Benditovich         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
21342974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
21352974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
21362974e916SYuri Benditovich             chain->stat.no_match++;
21372974e916SYuri Benditovich             return RSC_NO_MATCH;
21382974e916SYuri Benditovich     }
21392974e916SYuri Benditovich 
21402974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
21412974e916SYuri Benditovich }
21422974e916SYuri Benditovich 
21432974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain
21442974e916SYuri Benditovich  * to prevent out of order */
21452974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
21462974e916SYuri Benditovich                                          struct tcp_header *tcp)
21472974e916SYuri Benditovich {
21482974e916SYuri Benditovich     uint16_t tcp_hdr;
21492974e916SYuri Benditovich     uint16_t tcp_flag;
21502974e916SYuri Benditovich 
21512974e916SYuri Benditovich     tcp_flag = htons(tcp->th_offset_flags);
21522974e916SYuri Benditovich     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
21532974e916SYuri Benditovich     tcp_flag &= VIRTIO_NET_TCP_FLAG;
21542974e916SYuri Benditovich     if (tcp_flag & TH_SYN) {
21552974e916SYuri Benditovich         chain->stat.tcp_syn++;
21562974e916SYuri Benditovich         return RSC_BYPASS;
21572974e916SYuri Benditovich     }
21582974e916SYuri Benditovich 
21592974e916SYuri Benditovich     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
21602974e916SYuri Benditovich         chain->stat.tcp_ctrl_drain++;
21612974e916SYuri Benditovich         return RSC_FINAL;
21622974e916SYuri Benditovich     }
21632974e916SYuri Benditovich 
21642974e916SYuri Benditovich     if (tcp_hdr > sizeof(struct tcp_header)) {
21652974e916SYuri Benditovich         chain->stat.tcp_all_opt++;
21662974e916SYuri Benditovich         return RSC_FINAL;
21672974e916SYuri Benditovich     }
21682974e916SYuri Benditovich 
21692974e916SYuri Benditovich     return RSC_CANDIDATE;
21702974e916SYuri Benditovich }
21712974e916SYuri Benditovich 
21722974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
21732974e916SYuri Benditovich                                          NetClientState *nc,
21742974e916SYuri Benditovich                                          const uint8_t *buf, size_t size,
21752974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
21762974e916SYuri Benditovich {
21772974e916SYuri Benditovich     int ret;
21782974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
21792974e916SYuri Benditovich 
21802974e916SYuri Benditovich     if (QTAILQ_EMPTY(&chain->buffers)) {
21812974e916SYuri Benditovich         chain->stat.empty_cache++;
21822974e916SYuri Benditovich         virtio_net_rsc_cache_buf(chain, nc, buf, size);
21832974e916SYuri Benditovich         timer_mod(chain->drain_timer,
21842974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
21852974e916SYuri Benditovich         return size;
21862974e916SYuri Benditovich     }
21872974e916SYuri Benditovich 
21882974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
21892974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
21902974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
21912974e916SYuri Benditovich         } else {
21922974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
21932974e916SYuri Benditovich         }
21942974e916SYuri Benditovich 
21952974e916SYuri Benditovich         if (ret == RSC_FINAL) {
21962974e916SYuri Benditovich             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
21972974e916SYuri Benditovich                 /* Send failed */
21982974e916SYuri Benditovich                 chain->stat.final_failed++;
21992974e916SYuri Benditovich                 return 0;
22002974e916SYuri Benditovich             }
22012974e916SYuri Benditovich 
22022974e916SYuri Benditovich             /* Send current packet */
22032974e916SYuri Benditovich             return virtio_net_do_receive(nc, buf, size);
22042974e916SYuri Benditovich         } else if (ret == RSC_NO_MATCH) {
22052974e916SYuri Benditovich             continue;
22062974e916SYuri Benditovich         } else {
22072974e916SYuri Benditovich             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
22082974e916SYuri Benditovich             seg->is_coalesced = 1;
22092974e916SYuri Benditovich             return size;
22102974e916SYuri Benditovich         }
22112974e916SYuri Benditovich     }
22122974e916SYuri Benditovich 
22132974e916SYuri Benditovich     chain->stat.no_match_cache++;
22142974e916SYuri Benditovich     virtio_net_rsc_cache_buf(chain, nc, buf, size);
22152974e916SYuri Benditovich     return size;
22162974e916SYuri Benditovich }
22172974e916SYuri Benditovich 
22182974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */
22192974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
22202974e916SYuri Benditovich                                         NetClientState *nc,
22212974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
22222974e916SYuri Benditovich                                         uint16_t ip_start, uint16_t ip_size,
22232974e916SYuri Benditovich                                         uint16_t tcp_port)
22242974e916SYuri Benditovich {
22252974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
22262974e916SYuri Benditovich     uint32_t ppair1, ppair2;
22272974e916SYuri Benditovich 
22282974e916SYuri Benditovich     ppair1 = *(uint32_t *)(buf + tcp_port);
22292974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
22302974e916SYuri Benditovich         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
22312974e916SYuri Benditovich         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
22322974e916SYuri Benditovich             || (ppair1 != ppair2)) {
22332974e916SYuri Benditovich             continue;
22342974e916SYuri Benditovich         }
22352974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
22362974e916SYuri Benditovich             chain->stat.drain_failed++;
22372974e916SYuri Benditovich         }
22382974e916SYuri Benditovich 
22392974e916SYuri Benditovich         break;
22402974e916SYuri Benditovich     }
22412974e916SYuri Benditovich 
22422974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
22432974e916SYuri Benditovich }
22442974e916SYuri Benditovich 
22452974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
22462974e916SYuri Benditovich                                             struct ip_header *ip,
22472974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
22482974e916SYuri Benditovich {
22492974e916SYuri Benditovich     uint16_t ip_len;
22502974e916SYuri Benditovich 
22512974e916SYuri Benditovich     /* Not an ipv4 packet */
22522974e916SYuri Benditovich     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
22532974e916SYuri Benditovich         chain->stat.ip_option++;
22542974e916SYuri Benditovich         return RSC_BYPASS;
22552974e916SYuri Benditovich     }
22562974e916SYuri Benditovich 
22572974e916SYuri Benditovich     /* Don't handle packets with ip option */
22582974e916SYuri Benditovich     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
22592974e916SYuri Benditovich         chain->stat.ip_option++;
22602974e916SYuri Benditovich         return RSC_BYPASS;
22612974e916SYuri Benditovich     }
22622974e916SYuri Benditovich 
22632974e916SYuri Benditovich     if (ip->ip_p != IPPROTO_TCP) {
22642974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
22652974e916SYuri Benditovich         return RSC_BYPASS;
22662974e916SYuri Benditovich     }
22672974e916SYuri Benditovich 
22682974e916SYuri Benditovich     /* Don't handle packets with ip fragment */
22692974e916SYuri Benditovich     if (!(htons(ip->ip_off) & IP_DF)) {
22702974e916SYuri Benditovich         chain->stat.ip_frag++;
22712974e916SYuri Benditovich         return RSC_BYPASS;
22722974e916SYuri Benditovich     }
22732974e916SYuri Benditovich 
22742974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
22752974e916SYuri Benditovich     if (IPTOS_ECN(ip->ip_tos)) {
22762974e916SYuri Benditovich         chain->stat.ip_ecn++;
22772974e916SYuri Benditovich         return RSC_BYPASS;
22782974e916SYuri Benditovich     }
22792974e916SYuri Benditovich 
22802974e916SYuri Benditovich     ip_len = htons(ip->ip_len);
22812974e916SYuri Benditovich     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
22822974e916SYuri Benditovich         || ip_len > (size - chain->n->guest_hdr_len -
22832974e916SYuri Benditovich                      sizeof(struct eth_header))) {
22842974e916SYuri Benditovich         chain->stat.ip_hacked++;
22852974e916SYuri Benditovich         return RSC_BYPASS;
22862974e916SYuri Benditovich     }
22872974e916SYuri Benditovich 
22882974e916SYuri Benditovich     return RSC_CANDIDATE;
22892974e916SYuri Benditovich }
22902974e916SYuri Benditovich 
22912974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
22922974e916SYuri Benditovich                                       NetClientState *nc,
22932974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
22942974e916SYuri Benditovich {
22952974e916SYuri Benditovich     int32_t ret;
22962974e916SYuri Benditovich     uint16_t hdr_len;
22972974e916SYuri Benditovich     VirtioNetRscUnit unit;
22982974e916SYuri Benditovich 
22992974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
23002974e916SYuri Benditovich 
23012974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
23022974e916SYuri Benditovich         + sizeof(struct tcp_header))) {
23032974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
23042974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23052974e916SYuri Benditovich     }
23062974e916SYuri Benditovich 
23072974e916SYuri Benditovich     virtio_net_rsc_extract_unit4(chain, buf, &unit);
23082974e916SYuri Benditovich     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
23092974e916SYuri Benditovich         != RSC_CANDIDATE) {
23102974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23112974e916SYuri Benditovich     }
23122974e916SYuri Benditovich 
23132974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
23142974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
23152974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23162974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
23172974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
23182974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 12),
23192974e916SYuri Benditovich                 VIRTIO_NET_IP4_ADDR_SIZE,
23202974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
23212974e916SYuri Benditovich     }
23222974e916SYuri Benditovich 
23232974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
23242974e916SYuri Benditovich }
23252974e916SYuri Benditovich 
23262974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
23272974e916SYuri Benditovich                                             struct ip6_header *ip6,
23282974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
23292974e916SYuri Benditovich {
23302974e916SYuri Benditovich     uint16_t ip_len;
23312974e916SYuri Benditovich 
23322974e916SYuri Benditovich     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
23332974e916SYuri Benditovich         != IP_HEADER_VERSION_6) {
23342974e916SYuri Benditovich         return RSC_BYPASS;
23352974e916SYuri Benditovich     }
23362974e916SYuri Benditovich 
23372974e916SYuri Benditovich     /* Both option and protocol is checked in this */
23382974e916SYuri Benditovich     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
23392974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
23402974e916SYuri Benditovich         return RSC_BYPASS;
23412974e916SYuri Benditovich     }
23422974e916SYuri Benditovich 
23432974e916SYuri Benditovich     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
23442974e916SYuri Benditovich     if (ip_len < sizeof(struct tcp_header) ||
23452974e916SYuri Benditovich         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
23462974e916SYuri Benditovich                   - sizeof(struct ip6_header))) {
23472974e916SYuri Benditovich         chain->stat.ip_hacked++;
23482974e916SYuri Benditovich         return RSC_BYPASS;
23492974e916SYuri Benditovich     }
23502974e916SYuri Benditovich 
23512974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
23522974e916SYuri Benditovich     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
23532974e916SYuri Benditovich         chain->stat.ip_ecn++;
23542974e916SYuri Benditovich         return RSC_BYPASS;
23552974e916SYuri Benditovich     }
23562974e916SYuri Benditovich 
23572974e916SYuri Benditovich     return RSC_CANDIDATE;
23582974e916SYuri Benditovich }
23592974e916SYuri Benditovich 
23602974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
23612974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
23622974e916SYuri Benditovich {
23632974e916SYuri Benditovich     int32_t ret;
23642974e916SYuri Benditovich     uint16_t hdr_len;
23652974e916SYuri Benditovich     VirtioNetRscChain *chain;
23662974e916SYuri Benditovich     VirtioNetRscUnit unit;
23672974e916SYuri Benditovich 
23682974e916SYuri Benditovich     chain = (VirtioNetRscChain *)opq;
23692974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
23702974e916SYuri Benditovich 
23712974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
23722974e916SYuri Benditovich         + sizeof(tcp_header))) {
23732974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23742974e916SYuri Benditovich     }
23752974e916SYuri Benditovich 
23762974e916SYuri Benditovich     virtio_net_rsc_extract_unit6(chain, buf, &unit);
23772974e916SYuri Benditovich     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
23782974e916SYuri Benditovich                                                  unit.ip, buf, size)) {
23792974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23802974e916SYuri Benditovich     }
23812974e916SYuri Benditovich 
23822974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
23832974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
23842974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23852974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
23862974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
23872974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 8),
23882974e916SYuri Benditovich                 VIRTIO_NET_IP6_ADDR_SIZE,
23892974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header)
23902974e916SYuri Benditovich                 + sizeof(struct ip6_header));
23912974e916SYuri Benditovich     }
23922974e916SYuri Benditovich 
23932974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
23942974e916SYuri Benditovich }
23952974e916SYuri Benditovich 
23962974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
23972974e916SYuri Benditovich                                                       NetClientState *nc,
23982974e916SYuri Benditovich                                                       uint16_t proto)
23992974e916SYuri Benditovich {
24002974e916SYuri Benditovich     VirtioNetRscChain *chain;
24012974e916SYuri Benditovich 
24022974e916SYuri Benditovich     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
24032974e916SYuri Benditovich         return NULL;
24042974e916SYuri Benditovich     }
24052974e916SYuri Benditovich 
24062974e916SYuri Benditovich     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
24072974e916SYuri Benditovich         if (chain->proto == proto) {
24082974e916SYuri Benditovich             return chain;
24092974e916SYuri Benditovich         }
24102974e916SYuri Benditovich     }
24112974e916SYuri Benditovich 
24122974e916SYuri Benditovich     chain = g_malloc(sizeof(*chain));
24132974e916SYuri Benditovich     chain->n = n;
24142974e916SYuri Benditovich     chain->proto = proto;
24152974e916SYuri Benditovich     if (proto == (uint16_t)ETH_P_IP) {
24162974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
24172974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
24182974e916SYuri Benditovich     } else {
24192974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
24202974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
24212974e916SYuri Benditovich     }
24222974e916SYuri Benditovich     chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
24232974e916SYuri Benditovich                                       virtio_net_rsc_purge, chain);
24242974e916SYuri Benditovich     memset(&chain->stat, 0, sizeof(chain->stat));
24252974e916SYuri Benditovich 
24262974e916SYuri Benditovich     QTAILQ_INIT(&chain->buffers);
24272974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
24282974e916SYuri Benditovich 
24292974e916SYuri Benditovich     return chain;
24302974e916SYuri Benditovich }
24312974e916SYuri Benditovich 
24322974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc,
24332974e916SYuri Benditovich                                       const uint8_t *buf,
24342974e916SYuri Benditovich                                       size_t size)
24352974e916SYuri Benditovich {
24362974e916SYuri Benditovich     uint16_t proto;
24372974e916SYuri Benditovich     VirtioNetRscChain *chain;
24382974e916SYuri Benditovich     struct eth_header *eth;
24392974e916SYuri Benditovich     VirtIONet *n;
24402974e916SYuri Benditovich 
24412974e916SYuri Benditovich     n = qemu_get_nic_opaque(nc);
24422974e916SYuri Benditovich     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
24432974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24442974e916SYuri Benditovich     }
24452974e916SYuri Benditovich 
24462974e916SYuri Benditovich     eth = (struct eth_header *)(buf + n->guest_hdr_len);
24472974e916SYuri Benditovich     proto = htons(eth->h_proto);
24482974e916SYuri Benditovich 
24492974e916SYuri Benditovich     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
24502974e916SYuri Benditovich     if (chain) {
24512974e916SYuri Benditovich         chain->stat.received++;
24522974e916SYuri Benditovich         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
24532974e916SYuri Benditovich             return virtio_net_rsc_receive4(chain, nc, buf, size);
24542974e916SYuri Benditovich         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
24552974e916SYuri Benditovich             return virtio_net_rsc_receive6(chain, nc, buf, size);
24562974e916SYuri Benditovich         }
24572974e916SYuri Benditovich     }
24582974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
24592974e916SYuri Benditovich }
24602974e916SYuri Benditovich 
24612974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
24622974e916SYuri Benditovich                                   size_t size)
24632974e916SYuri Benditovich {
24642974e916SYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
24652974e916SYuri Benditovich     if ((n->rsc4_enabled || n->rsc6_enabled)) {
24662974e916SYuri Benditovich         return virtio_net_rsc_receive(nc, buf, size);
24672974e916SYuri Benditovich     } else {
24682974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24692974e916SYuri Benditovich     }
24702974e916SYuri Benditovich }
24712974e916SYuri Benditovich 
24726e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
24736e790746SPaolo Bonzini 
24746e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
24756e790746SPaolo Bonzini {
24766e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
24776e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
247817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
24796e790746SPaolo Bonzini 
248051b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
248117a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
24826e790746SPaolo Bonzini 
248351b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
248451b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
24856e790746SPaolo Bonzini 
24866e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
24876e790746SPaolo Bonzini     virtio_net_flush_tx(q);
24886e790746SPaolo Bonzini }
24896e790746SPaolo Bonzini 
24906e790746SPaolo Bonzini /* TX */
24916e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
24926e790746SPaolo Bonzini {
24936e790746SPaolo Bonzini     VirtIONet *n = q->n;
249417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
249551b19ebeSPaolo Bonzini     VirtQueueElement *elem;
24966e790746SPaolo Bonzini     int32_t num_packets = 0;
24976e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
249817a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
24996e790746SPaolo Bonzini         return num_packets;
25006e790746SPaolo Bonzini     }
25016e790746SPaolo Bonzini 
250251b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
25036e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
25046e790746SPaolo Bonzini         return num_packets;
25056e790746SPaolo Bonzini     }
25066e790746SPaolo Bonzini 
250751b19ebeSPaolo Bonzini     for (;;) {
2508bd89dd98SJason Wang         ssize_t ret;
250951b19ebeSPaolo Bonzini         unsigned int out_num;
251051b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2511feb93f36SJason Wang         struct virtio_net_hdr_mrg_rxbuf mhdr;
25126e790746SPaolo Bonzini 
251351b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
251451b19ebeSPaolo Bonzini         if (!elem) {
251551b19ebeSPaolo Bonzini             break;
251651b19ebeSPaolo Bonzini         }
251751b19ebeSPaolo Bonzini 
251851b19ebeSPaolo Bonzini         out_num = elem->out_num;
251951b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
25206e790746SPaolo Bonzini         if (out_num < 1) {
2521fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
2522fa5e56c2SGreg Kurz             virtqueue_detach_element(q->tx_vq, elem, 0);
2523fa5e56c2SGreg Kurz             g_free(elem);
2524fa5e56c2SGreg Kurz             return -EINVAL;
25256e790746SPaolo Bonzini         }
25266e790746SPaolo Bonzini 
2527032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
2528feb93f36SJason Wang             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2529feb93f36SJason Wang                 n->guest_hdr_len) {
2530fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
2531fa5e56c2SGreg Kurz                 virtqueue_detach_element(q->tx_vq, elem, 0);
2532fa5e56c2SGreg Kurz                 g_free(elem);
2533fa5e56c2SGreg Kurz                 return -EINVAL;
2534032a74a1SCédric Le Goater             }
25351bfa316cSGreg Kurz             if (n->needs_vnet_hdr_swap) {
2536feb93f36SJason Wang                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2537feb93f36SJason Wang                 sg2[0].iov_base = &mhdr;
2538feb93f36SJason Wang                 sg2[0].iov_len = n->guest_hdr_len;
2539feb93f36SJason Wang                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2540feb93f36SJason Wang                                    out_sg, out_num,
2541feb93f36SJason Wang                                    n->guest_hdr_len, -1);
2542feb93f36SJason Wang                 if (out_num == VIRTQUEUE_MAX_SIZE) {
2543feb93f36SJason Wang                     goto drop;
2544032a74a1SCédric Le Goater                 }
2545feb93f36SJason Wang                 out_num += 1;
2546feb93f36SJason Wang                 out_sg = sg2;
2547feb93f36SJason Wang             }
2548feb93f36SJason Wang         }
25496e790746SPaolo Bonzini         /*
25506e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
25516e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
25526e790746SPaolo Bonzini          * that host is interested in.
25536e790746SPaolo Bonzini          */
25546e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
25556e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
25566e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
25576e790746SPaolo Bonzini                                        out_sg, out_num,
25586e790746SPaolo Bonzini                                        0, n->host_hdr_len);
25596e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
25606e790746SPaolo Bonzini                              out_sg, out_num,
25616e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
25626e790746SPaolo Bonzini             out_num = sg_num;
25636e790746SPaolo Bonzini             out_sg = sg;
25646e790746SPaolo Bonzini         }
25656e790746SPaolo Bonzini 
25666e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
25676e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
25686e790746SPaolo Bonzini         if (ret == 0) {
25696e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
25706e790746SPaolo Bonzini             q->async_tx.elem = elem;
25716e790746SPaolo Bonzini             return -EBUSY;
25726e790746SPaolo Bonzini         }
25736e790746SPaolo Bonzini 
2574feb93f36SJason Wang drop:
257551b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
257617a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
257751b19ebeSPaolo Bonzini         g_free(elem);
25786e790746SPaolo Bonzini 
25796e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
25806e790746SPaolo Bonzini             break;
25816e790746SPaolo Bonzini         }
25826e790746SPaolo Bonzini     }
25836e790746SPaolo Bonzini     return num_packets;
25846e790746SPaolo Bonzini }
25856e790746SPaolo Bonzini 
25866e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
25876e790746SPaolo Bonzini {
258817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
25896e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
25906e790746SPaolo Bonzini 
2591283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2592283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2593283e2c2aSYuri Benditovich         return;
2594283e2c2aSYuri Benditovich     }
2595283e2c2aSYuri Benditovich 
25966e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
259717a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
25986e790746SPaolo Bonzini         q->tx_waiting = 1;
25996e790746SPaolo Bonzini         return;
26006e790746SPaolo Bonzini     }
26016e790746SPaolo Bonzini 
26026e790746SPaolo Bonzini     if (q->tx_waiting) {
26036e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 1);
2604bc72ad67SAlex Bligh         timer_del(q->tx_timer);
26056e790746SPaolo Bonzini         q->tx_waiting = 0;
2606fa5e56c2SGreg Kurz         if (virtio_net_flush_tx(q) == -EINVAL) {
2607fa5e56c2SGreg Kurz             return;
2608fa5e56c2SGreg Kurz         }
26096e790746SPaolo Bonzini     } else {
2610bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
2611bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
26126e790746SPaolo Bonzini         q->tx_waiting = 1;
26136e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
26146e790746SPaolo Bonzini     }
26156e790746SPaolo Bonzini }
26166e790746SPaolo Bonzini 
26176e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
26186e790746SPaolo Bonzini {
261917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
26206e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
26216e790746SPaolo Bonzini 
2622283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2623283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2624283e2c2aSYuri Benditovich         return;
2625283e2c2aSYuri Benditovich     }
2626283e2c2aSYuri Benditovich 
26276e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
26286e790746SPaolo Bonzini         return;
26296e790746SPaolo Bonzini     }
26306e790746SPaolo Bonzini     q->tx_waiting = 1;
26316e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
263217a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
26336e790746SPaolo Bonzini         return;
26346e790746SPaolo Bonzini     }
26356e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
26366e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
26376e790746SPaolo Bonzini }
26386e790746SPaolo Bonzini 
26396e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
26406e790746SPaolo Bonzini {
26416e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
26426e790746SPaolo Bonzini     VirtIONet *n = q->n;
264317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2644e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2645e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2646e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2647e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2648e8bcf842SMichael S. Tsirkin         return;
2649e8bcf842SMichael S. Tsirkin     }
26506e790746SPaolo Bonzini 
26516e790746SPaolo Bonzini     q->tx_waiting = 0;
26526e790746SPaolo Bonzini 
26536e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
265417a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
26556e790746SPaolo Bonzini         return;
265617a0ca55SKONRAD Frederic     }
26576e790746SPaolo Bonzini 
26586e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
26596e790746SPaolo Bonzini     virtio_net_flush_tx(q);
26606e790746SPaolo Bonzini }
26616e790746SPaolo Bonzini 
26626e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
26636e790746SPaolo Bonzini {
26646e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
26656e790746SPaolo Bonzini     VirtIONet *n = q->n;
266617a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
26676e790746SPaolo Bonzini     int32_t ret;
26686e790746SPaolo Bonzini 
2669e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2670e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2671e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2672e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2673e8bcf842SMichael S. Tsirkin         return;
2674e8bcf842SMichael S. Tsirkin     }
26756e790746SPaolo Bonzini 
26766e790746SPaolo Bonzini     q->tx_waiting = 0;
26776e790746SPaolo Bonzini 
26786e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
267917a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
26806e790746SPaolo Bonzini         return;
268117a0ca55SKONRAD Frederic     }
26826e790746SPaolo Bonzini 
26836e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
2684fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
2685fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
2686fa5e56c2SGreg Kurz                  * broken */
26876e790746SPaolo Bonzini     }
26886e790746SPaolo Bonzini 
26896e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
26906e790746SPaolo Bonzini      * more coming and immediately reschedule */
26916e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
26926e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
26936e790746SPaolo Bonzini         q->tx_waiting = 1;
26946e790746SPaolo Bonzini         return;
26956e790746SPaolo Bonzini     }
26966e790746SPaolo Bonzini 
26976e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
26986e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
26996e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
27006e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2701fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
2702fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
2703fa5e56c2SGreg Kurz         return;
2704fa5e56c2SGreg Kurz     } else if (ret > 0) {
27056e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
27066e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
27076e790746SPaolo Bonzini         q->tx_waiting = 1;
27086e790746SPaolo Bonzini     }
27096e790746SPaolo Bonzini }
27106e790746SPaolo Bonzini 
2711f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
2712f9d6dbf0SWen Congyang {
2713f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2714f9d6dbf0SWen Congyang 
27151c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
27161c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
27179b02e161SWei Wang 
2718f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2719f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
27209b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
27219b02e161SWei Wang                              virtio_net_handle_tx_timer);
2722f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2723f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
2724f9d6dbf0SWen Congyang                                               &n->vqs[index]);
2725f9d6dbf0SWen Congyang     } else {
2726f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
27279b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
27289b02e161SWei Wang                              virtio_net_handle_tx_bh);
2729f9d6dbf0SWen Congyang         n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
2730f9d6dbf0SWen Congyang     }
2731f9d6dbf0SWen Congyang 
2732f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
2733f9d6dbf0SWen Congyang     n->vqs[index].n = n;
2734f9d6dbf0SWen Congyang }
2735f9d6dbf0SWen Congyang 
2736f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
2737f9d6dbf0SWen Congyang {
2738f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2739f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
2740f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
2741f9d6dbf0SWen Congyang 
2742f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
2743f9d6dbf0SWen Congyang 
2744f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
2745f9d6dbf0SWen Congyang     if (q->tx_timer) {
2746f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
2747f989c30cSYunjian Wang         q->tx_timer = NULL;
2748f9d6dbf0SWen Congyang     } else {
2749f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
2750f989c30cSYunjian Wang         q->tx_bh = NULL;
2751f9d6dbf0SWen Congyang     }
2752f989c30cSYunjian Wang     q->tx_waiting = 0;
2753f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
2754f9d6dbf0SWen Congyang }
2755f9d6dbf0SWen Congyang 
2756f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
2757f9d6dbf0SWen Congyang {
2758f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2759f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
2760f9d6dbf0SWen Congyang     int new_num_queues = new_max_queues * 2 + 1;
2761f9d6dbf0SWen Congyang     int i;
2762f9d6dbf0SWen Congyang 
2763f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
2764f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
2765f9d6dbf0SWen Congyang 
2766f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
2767f9d6dbf0SWen Congyang         return;
2768f9d6dbf0SWen Congyang     }
2769f9d6dbf0SWen Congyang 
2770f9d6dbf0SWen Congyang     /*
2771f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
2772f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
277320f86a75SYuval Shaia      * and then we only enter one of the following two loops.
2774f9d6dbf0SWen Congyang      */
2775f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
2776f9d6dbf0SWen Congyang 
2777f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2778f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
2779f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
2780f9d6dbf0SWen Congyang     }
2781f9d6dbf0SWen Congyang 
2782f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2783f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
2784f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
2785f9d6dbf0SWen Congyang     }
2786f9d6dbf0SWen Congyang 
2787f9d6dbf0SWen Congyang     /* add ctrl_vq last */
2788f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2789f9d6dbf0SWen Congyang }
2790f9d6dbf0SWen Congyang 
2791ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
27926e790746SPaolo Bonzini {
2793f9d6dbf0SWen Congyang     int max = multiqueue ? n->max_queues : 1;
2794f9d6dbf0SWen Congyang 
27956e790746SPaolo Bonzini     n->multiqueue = multiqueue;
2796f9d6dbf0SWen Congyang     virtio_net_change_num_queues(n, max);
27976e790746SPaolo Bonzini 
27986e790746SPaolo Bonzini     virtio_net_set_queues(n);
27996e790746SPaolo Bonzini }
28006e790746SPaolo Bonzini 
2801982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
2802037dab2fSGreg Kurz {
2803982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
2804982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2805037dab2fSGreg Kurz     int i, link_down;
2806037dab2fSGreg Kurz 
28079d8c6a25SDr. David Alan Gilbert     trace_virtio_net_post_load_device();
2808982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
280995129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
2810e22f0603SYuri Benditovich                                                        VIRTIO_F_VERSION_1),
2811e22f0603SYuri Benditovich                                virtio_vdev_has_feature(vdev,
2812e22f0603SYuri Benditovich                                                        VIRTIO_NET_F_HASH_REPORT));
28136e790746SPaolo Bonzini 
28146e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
2815982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
28166e790746SPaolo Bonzini         n->mac_table.in_use = 0;
28176e790746SPaolo Bonzini     }
28186e790746SPaolo Bonzini 
2819982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
28206c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
28216c666823SMichael S. Tsirkin     }
28226c666823SMichael S. Tsirkin 
28237788c3f2SMikhail Sennikovsky     /*
28247788c3f2SMikhail Sennikovsky      * curr_guest_offloads will be later overwritten by the
28257788c3f2SMikhail Sennikovsky      * virtio_set_features_nocheck call done from the virtio_load.
28267788c3f2SMikhail Sennikovsky      * Here we make sure it is preserved and restored accordingly
28277788c3f2SMikhail Sennikovsky      * in the virtio_net_post_load_virtio callback.
28287788c3f2SMikhail Sennikovsky      */
28297788c3f2SMikhail Sennikovsky     n->saved_guest_offloads = n->curr_guest_offloads;
28306c666823SMichael S. Tsirkin 
28316e790746SPaolo Bonzini     virtio_net_set_queues(n);
28326e790746SPaolo Bonzini 
28336e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
28346e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
28356e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
28366e790746SPaolo Bonzini             break;
28376e790746SPaolo Bonzini         }
28386e790746SPaolo Bonzini     }
28396e790746SPaolo Bonzini     n->mac_table.first_multi = i;
28406e790746SPaolo Bonzini 
28416e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
28426e790746SPaolo Bonzini      * to link status bit in n->status */
28436e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
28446e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
28456e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
28466e790746SPaolo Bonzini     }
28476e790746SPaolo Bonzini 
28486c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
28496c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
28509d8c6a25SDr. David Alan Gilbert         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
28519d8c6a25SDr. David Alan Gilbert                                   QEMU_CLOCK_VIRTUAL,
28529d8c6a25SDr. David Alan Gilbert                                   virtio_net_announce_timer, n);
28539d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
28549d8c6a25SDr. David Alan Gilbert             timer_mod(n->announce_timer.tm,
28559d8c6a25SDr. David Alan Gilbert                       qemu_clock_get_ms(n->announce_timer.type));
28569d8c6a25SDr. David Alan Gilbert         } else {
2857944458b6SDr. David Alan Gilbert             qemu_announce_timer_del(&n->announce_timer, false);
28589d8c6a25SDr. David Alan Gilbert         }
28596c666823SMichael S. Tsirkin     }
28606c666823SMichael S. Tsirkin 
2861e41b7114SYuri Benditovich     if (n->rss_data.enabled) {
2862*0145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
2863*0145c393SAndrew Melnychenko         if (!n->rss_data.populate_hash) {
2864*0145c393SAndrew Melnychenko             if (!virtio_net_attach_epbf_rss(n)) {
2865*0145c393SAndrew Melnychenko                 if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
2866*0145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS for vhost");
2867*0145c393SAndrew Melnychenko                 } else {
2868*0145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS - "
2869*0145c393SAndrew Melnychenko                                 "fallback to software RSS");
2870*0145c393SAndrew Melnychenko                     n->rss_data.enabled_software_rss = true;
2871*0145c393SAndrew Melnychenko                 }
2872*0145c393SAndrew Melnychenko             }
2873*0145c393SAndrew Melnychenko         }
2874*0145c393SAndrew Melnychenko 
2875e41b7114SYuri Benditovich         trace_virtio_net_rss_enable(n->rss_data.hash_types,
2876e41b7114SYuri Benditovich                                     n->rss_data.indirections_len,
2877e41b7114SYuri Benditovich                                     sizeof(n->rss_data.key));
2878e41b7114SYuri Benditovich     } else {
2879e41b7114SYuri Benditovich         trace_virtio_net_rss_disable();
2880e41b7114SYuri Benditovich     }
28816e790746SPaolo Bonzini     return 0;
28826e790746SPaolo Bonzini }
28836e790746SPaolo Bonzini 
28847788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev)
28857788c3f2SMikhail Sennikovsky {
28867788c3f2SMikhail Sennikovsky     VirtIONet *n = VIRTIO_NET(vdev);
28877788c3f2SMikhail Sennikovsky     /*
28887788c3f2SMikhail Sennikovsky      * The actual needed state is now in saved_guest_offloads,
28897788c3f2SMikhail Sennikovsky      * see virtio_net_post_load_device for detail.
28907788c3f2SMikhail Sennikovsky      * Restore it back and apply the desired offloads.
28917788c3f2SMikhail Sennikovsky      */
28927788c3f2SMikhail Sennikovsky     n->curr_guest_offloads = n->saved_guest_offloads;
28937788c3f2SMikhail Sennikovsky     if (peer_has_vnet_hdr(n)) {
28947788c3f2SMikhail Sennikovsky         virtio_net_apply_guest_offloads(n);
28957788c3f2SMikhail Sennikovsky     }
28967788c3f2SMikhail Sennikovsky 
28977788c3f2SMikhail Sennikovsky     return 0;
28987788c3f2SMikhail Sennikovsky }
28997788c3f2SMikhail Sennikovsky 
2900982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
2901982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
2902982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
2903982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
2904982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
2905982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2906982b78c5SDr. David Alan Gilbert    },
2907982b78c5SDr. David Alan Gilbert };
2908982b78c5SDr. David Alan Gilbert 
2909982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id)
2910982b78c5SDr. David Alan Gilbert {
2911982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->max_queues > 1;
2912982b78c5SDr. David Alan Gilbert }
2913982b78c5SDr. David Alan Gilbert 
2914982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
2915982b78c5SDr. David Alan Gilbert {
2916982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
2917982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2918982b78c5SDr. David Alan Gilbert }
2919982b78c5SDr. David Alan Gilbert 
2920982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
2921982b78c5SDr. David Alan Gilbert {
2922982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
2923982b78c5SDr. David Alan Gilbert }
2924982b78c5SDr. David Alan Gilbert 
2925982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
2926982b78c5SDr. David Alan Gilbert {
2927982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
2928982b78c5SDr. David Alan Gilbert }
2929982b78c5SDr. David Alan Gilbert 
2930982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
2931982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
2932982b78c5SDr. David Alan Gilbert  */
2933982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
2934982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
2935982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
2936982b78c5SDr. David Alan Gilbert     uint16_t        curr_queues_1;
2937982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
2938982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
2939982b78c5SDr. David Alan Gilbert };
2940982b78c5SDr. David Alan Gilbert 
2941982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
2942982b78c5SDr. David Alan Gilbert  * the 1st entry in the queues and only if there's more than one
2943982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
2944982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
2945982b78c5SDr. David Alan Gilbert  */
2946982b78c5SDr. David Alan Gilbert 
294744b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque)
2948982b78c5SDr. David Alan Gilbert {
2949982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2950982b78c5SDr. David Alan Gilbert 
2951982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
2952982b78c5SDr. David Alan Gilbert     tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
2953982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues == 0) {
2954982b78c5SDr. David Alan Gilbert         tmp->curr_queues_1 = 0;
2955982b78c5SDr. David Alan Gilbert     }
295644b1ff31SDr. David Alan Gilbert 
295744b1ff31SDr. David Alan Gilbert     return 0;
2958982b78c5SDr. David Alan Gilbert }
2959982b78c5SDr. David Alan Gilbert 
2960982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
2961982b78c5SDr. David Alan Gilbert {
2962982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2963982b78c5SDr. David Alan Gilbert 
2964982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
2965982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
2966982b78c5SDr. David Alan Gilbert 
2967982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues > tmp->parent->max_queues) {
2968982b78c5SDr. David Alan Gilbert         error_report("virtio-net: curr_queues %x > max_queues %x",
2969982b78c5SDr. David Alan Gilbert             tmp->parent->curr_queues, tmp->parent->max_queues);
2970982b78c5SDr. David Alan Gilbert 
2971982b78c5SDr. David Alan Gilbert         return -EINVAL;
2972982b78c5SDr. David Alan Gilbert     }
2973982b78c5SDr. David Alan Gilbert 
2974982b78c5SDr. David Alan Gilbert     return 0; /* all good */
2975982b78c5SDr. David Alan Gilbert }
2976982b78c5SDr. David Alan Gilbert 
2977982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
2978982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
2979982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
2980982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
2981982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
2982982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
2983982b78c5SDr. David Alan Gilbert                                      curr_queues_1,
2984982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
2985982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
2986982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2987982b78c5SDr. David Alan Gilbert     },
2988982b78c5SDr. David Alan Gilbert };
2989982b78c5SDr. David Alan Gilbert 
2990982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
2991982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
2992982b78c5SDr. David Alan Gilbert  */
2993982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
2994982b78c5SDr. David Alan Gilbert {
2995982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2996982b78c5SDr. David Alan Gilbert 
2997982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
2998982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
2999982b78c5SDr. David Alan Gilbert         return -EINVAL;
3000982b78c5SDr. David Alan Gilbert     }
3001982b78c5SDr. David Alan Gilbert 
3002982b78c5SDr. David Alan Gilbert     return 0;
3003982b78c5SDr. David Alan Gilbert }
3004982b78c5SDr. David Alan Gilbert 
300544b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque)
3006982b78c5SDr. David Alan Gilbert {
3007982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3008982b78c5SDr. David Alan Gilbert 
3009982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
301044b1ff31SDr. David Alan Gilbert 
301144b1ff31SDr. David Alan Gilbert     return 0;
3012982b78c5SDr. David Alan Gilbert }
3013982b78c5SDr. David Alan Gilbert 
3014982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
3015982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
3016982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
3017982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
3018982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3019982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
3020982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3021982b78c5SDr. David Alan Gilbert     },
3022982b78c5SDr. David Alan Gilbert };
3023982b78c5SDr. David Alan Gilbert 
3024982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3025982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3026982b78c5SDr. David Alan Gilbert  */
3027982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
3028982b78c5SDr. David Alan Gilbert {
3029982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3030982b78c5SDr. David Alan Gilbert 
3031982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
3032982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
3033982b78c5SDr. David Alan Gilbert         return -EINVAL;
3034982b78c5SDr. David Alan Gilbert     }
3035982b78c5SDr. David Alan Gilbert 
3036982b78c5SDr. David Alan Gilbert     return 0;
3037982b78c5SDr. David Alan Gilbert }
3038982b78c5SDr. David Alan Gilbert 
303944b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque)
3040982b78c5SDr. David Alan Gilbert {
3041982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3042982b78c5SDr. David Alan Gilbert 
3043982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
304444b1ff31SDr. David Alan Gilbert 
304544b1ff31SDr. David Alan Gilbert     return 0;
3046982b78c5SDr. David Alan Gilbert }
3047982b78c5SDr. David Alan Gilbert 
3048982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
3049982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
3050982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
3051982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
3052982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3053982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
3054982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3055982b78c5SDr. David Alan Gilbert     },
3056982b78c5SDr. David Alan Gilbert };
3057982b78c5SDr. David Alan Gilbert 
3058e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque)
3059e41b7114SYuri Benditovich {
3060e41b7114SYuri Benditovich     return VIRTIO_NET(opaque)->rss_data.enabled;
3061e41b7114SYuri Benditovich }
3062e41b7114SYuri Benditovich 
3063e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = {
3064e41b7114SYuri Benditovich     .name      = "virtio-net-device/rss",
3065e41b7114SYuri Benditovich     .version_id = 1,
3066e41b7114SYuri Benditovich     .minimum_version_id = 1,
3067e41b7114SYuri Benditovich     .needed = virtio_net_rss_needed,
3068e41b7114SYuri Benditovich     .fields = (VMStateField[]) {
3069e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
3070e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
3071e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
3072e41b7114SYuri Benditovich         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
3073e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
3074e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
3075e41b7114SYuri Benditovich         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
3076e41b7114SYuri Benditovich                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
3077e41b7114SYuri Benditovich         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
3078e41b7114SYuri Benditovich                                     rss_data.indirections_len, 0,
3079e41b7114SYuri Benditovich                                     vmstate_info_uint16, uint16_t),
3080e41b7114SYuri Benditovich         VMSTATE_END_OF_LIST()
3081e41b7114SYuri Benditovich     },
3082e41b7114SYuri Benditovich };
3083e41b7114SYuri Benditovich 
3084982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
3085982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
3086982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
3087982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
3088982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
3089982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
3090982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
3091982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
3092982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
3093982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
3094982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
3095982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
3096982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
3097982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
3098982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3099982b78c5SDr. David Alan Gilbert 
3100982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
3101982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
3102982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
3103982b78c5SDr. David Alan Gilbert          */
3104982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3105982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
3106982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
3107982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3108982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
3109982b78c5SDr. David Alan Gilbert 
3110982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
3111982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3112982b78c5SDr. David Alan Gilbert          * but based on the uint.
3113982b78c5SDr. David Alan Gilbert          */
3114982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3115982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3116982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
3117982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3118982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3119982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
3120982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
3121982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
3122982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
3123982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3124982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
3125982b78c5SDr. David Alan Gilbert         VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
3126982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
3127982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
3128982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3129982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
3130982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3131982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
3132982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3133982b78c5SDr. David Alan Gilbert    },
3134e41b7114SYuri Benditovich     .subsections = (const VMStateDescription * []) {
3135e41b7114SYuri Benditovich         &vmstate_virtio_net_rss,
3136e41b7114SYuri Benditovich         NULL
3137e41b7114SYuri Benditovich     }
3138982b78c5SDr. David Alan Gilbert };
3139982b78c5SDr. David Alan Gilbert 
31406e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
3141f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
31426e790746SPaolo Bonzini     .size = sizeof(NICState),
31436e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
31446e790746SPaolo Bonzini     .receive = virtio_net_receive,
31456e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
3146b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
3147b2c929f0SDr. David Alan Gilbert     .announce = virtio_net_announce,
31486e790746SPaolo Bonzini };
31496e790746SPaolo Bonzini 
31506e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
31516e790746SPaolo Bonzini {
315217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
31536e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
31546e790746SPaolo Bonzini     assert(n->vhost_started);
3155ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
31566e790746SPaolo Bonzini }
31576e790746SPaolo Bonzini 
31586e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
31596e790746SPaolo Bonzini                                            bool mask)
31606e790746SPaolo Bonzini {
316117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
31626e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
31636e790746SPaolo Bonzini     assert(n->vhost_started);
3164ed8b4afeSNikolay Nikolaev     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
31656e790746SPaolo Bonzini                              vdev, idx, mask);
31666e790746SPaolo Bonzini }
31676e790746SPaolo Bonzini 
3168019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
31696e790746SPaolo Bonzini {
31700cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3171a93e599dSMaxime Coquelin 
3172ba550851SStefano Garzarella     n->config_size = virtio_feature_get_config_size(feature_sizes,
3173ba550851SStefano Garzarella                                                     host_features);
317417ec5a86SKONRAD Frederic }
31756e790746SPaolo Bonzini 
31768a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
31778a253ec2SKONRAD Frederic                                    const char *type)
31788a253ec2SKONRAD Frederic {
31798a253ec2SKONRAD Frederic     /*
31808a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
31818a253ec2SKONRAD Frederic      */
31828a253ec2SKONRAD Frederic     assert(type != NULL);
31838a253ec2SKONRAD Frederic 
31848a253ec2SKONRAD Frederic     g_free(n->netclient_name);
31858a253ec2SKONRAD Frederic     g_free(n->netclient_type);
31868a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
31878a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
31888a253ec2SKONRAD Frederic }
31898a253ec2SKONRAD Frederic 
31900e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
31919711cd0dSJens Freimann {
31929711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
31939711cd0dSJens Freimann     PCIDevice *pci_dev;
31949711cd0dSJens Freimann     Error *err = NULL;
31959711cd0dSJens Freimann 
31960e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
31979711cd0dSJens Freimann     if (hotplug_ctrl) {
31980e9a65c5SJuan Quintela         pci_dev = PCI_DEVICE(dev);
31999711cd0dSJens Freimann         pci_dev->partially_hotplugged = true;
32000e9a65c5SJuan Quintela         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
32019711cd0dSJens Freimann         if (err) {
32029711cd0dSJens Freimann             error_report_err(err);
32039711cd0dSJens Freimann             return false;
32049711cd0dSJens Freimann         }
32059711cd0dSJens Freimann     } else {
32069711cd0dSJens Freimann         return false;
32079711cd0dSJens Freimann     }
32089711cd0dSJens Freimann     return true;
32099711cd0dSJens Freimann }
32109711cd0dSJens Freimann 
32110e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
32120e9a65c5SJuan Quintela                                     Error **errp)
32139711cd0dSJens Freimann {
32145a0948d3SMarkus Armbruster     Error *err = NULL;
32159711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
32160e9a65c5SJuan Quintela     PCIDevice *pdev = PCI_DEVICE(dev);
321778274682SJuan Quintela     BusState *primary_bus;
32189711cd0dSJens Freimann 
32199711cd0dSJens Freimann     if (!pdev->partially_hotplugged) {
32209711cd0dSJens Freimann         return true;
32219711cd0dSJens Freimann     }
32220e9a65c5SJuan Quintela     primary_bus = dev->parent_bus;
322378274682SJuan Quintela     if (!primary_bus) {
3224150ab54aSJens Freimann         error_setg(errp, "virtio_net: couldn't find primary bus");
32255a0948d3SMarkus Armbruster         return false;
32269711cd0dSJens Freimann     }
32270e9a65c5SJuan Quintela     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3228e2bde83eSJuan Quintela     qatomic_set(&n->failover_primary_hidden, false);
32290e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
32309711cd0dSJens Freimann     if (hotplug_ctrl) {
32310e9a65c5SJuan Quintela         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
32325a0948d3SMarkus Armbruster         if (err) {
32335a0948d3SMarkus Armbruster             goto out;
32345a0948d3SMarkus Armbruster         }
32350e9a65c5SJuan Quintela         hotplug_handler_plug(hotplug_ctrl, dev, &err);
32369711cd0dSJens Freimann     }
3237150ab54aSJens Freimann 
3238150ab54aSJens Freimann out:
32395a0948d3SMarkus Armbruster     error_propagate(errp, err);
32405a0948d3SMarkus Armbruster     return !err;
32419711cd0dSJens Freimann }
32429711cd0dSJens Freimann 
324307a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
32449711cd0dSJens Freimann {
32459711cd0dSJens Freimann     bool should_be_hidden;
32469711cd0dSJens Freimann     Error *err = NULL;
324707a5d816SJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
324807a5d816SJuan Quintela 
324907a5d816SJuan Quintela     if (!dev) {
325007a5d816SJuan Quintela         return;
325107a5d816SJuan Quintela     }
32529711cd0dSJens Freimann 
3253e2bde83eSJuan Quintela     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
32549711cd0dSJens Freimann 
32554dbac1aeSMarkus Armbruster     if (migration_in_setup(s) && !should_be_hidden) {
325607a5d816SJuan Quintela         if (failover_unplug_primary(n, dev)) {
325707a5d816SJuan Quintela             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
325807a5d816SJuan Quintela             qapi_event_send_unplug_primary(dev->id);
3259e2bde83eSJuan Quintela             qatomic_set(&n->failover_primary_hidden, true);
32609711cd0dSJens Freimann         } else {
32619711cd0dSJens Freimann             warn_report("couldn't unplug primary device");
32629711cd0dSJens Freimann         }
32639711cd0dSJens Freimann     } else if (migration_has_failed(s)) {
3264150ab54aSJens Freimann         /* We already unplugged the device let's plug it back */
326507a5d816SJuan Quintela         if (!failover_replug_primary(n, dev, &err)) {
32669711cd0dSJens Freimann             if (err) {
32679711cd0dSJens Freimann                 error_report_err(err);
32689711cd0dSJens Freimann             }
32699711cd0dSJens Freimann         }
32709711cd0dSJens Freimann     }
32719711cd0dSJens Freimann }
32729711cd0dSJens Freimann 
32739711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
32749711cd0dSJens Freimann {
32759711cd0dSJens Freimann     MigrationState *s = data;
32769711cd0dSJens Freimann     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
32779711cd0dSJens Freimann     virtio_net_handle_migration_primary(n, s);
32789711cd0dSJens Freimann }
32799711cd0dSJens Freimann 
3280b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener,
32819711cd0dSJens Freimann                                          QemuOpts *device_opts)
32829711cd0dSJens Freimann {
32839711cd0dSJens Freimann     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
32844f0303aeSJuan Quintela     const char *standby_id;
32859711cd0dSJens Freimann 
32864d0e59acSJens Freimann     if (!device_opts) {
328789631fedSJuan Quintela         return false;
32884d0e59acSJens Freimann     }
32894f0303aeSJuan Quintela     standby_id = qemu_opt_get(device_opts, "failover_pair_id");
329089631fedSJuan Quintela     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
329189631fedSJuan Quintela         return false;
32929711cd0dSJens Freimann     }
32939711cd0dSJens Freimann 
3294e2bde83eSJuan Quintela     /* failover_primary_hidden is set during feature negotiation */
32953abad4a2SJuan Quintela     return qatomic_read(&n->failover_primary_hidden);
32969711cd0dSJens Freimann }
32979711cd0dSJens Freimann 
3298e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
329917ec5a86SKONRAD Frederic {
3300e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3301284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3302284a32f0SAndreas Färber     NetClientState *nc;
33031773d9eeSKONRAD Frederic     int i;
330417ec5a86SKONRAD Frederic 
3305a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
3306127833eeSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3307a93e599dSMaxime Coquelin     }
3308a93e599dSMaxime Coquelin 
33099473939eSJason Baron     if (n->net_conf.duplex_str) {
33109473939eSJason Baron         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
33119473939eSJason Baron             n->net_conf.duplex = DUPLEX_HALF;
33129473939eSJason Baron         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
33139473939eSJason Baron             n->net_conf.duplex = DUPLEX_FULL;
33149473939eSJason Baron         } else {
33159473939eSJason Baron             error_setg(errp, "'duplex' must be 'half' or 'full'");
3316843c4cfcSMarkus Armbruster             return;
33179473939eSJason Baron         }
33189473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
33199473939eSJason Baron     } else {
33209473939eSJason Baron         n->net_conf.duplex = DUPLEX_UNKNOWN;
33219473939eSJason Baron     }
33229473939eSJason Baron 
33239473939eSJason Baron     if (n->net_conf.speed < SPEED_UNKNOWN) {
33249473939eSJason Baron         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3325843c4cfcSMarkus Armbruster         return;
3326843c4cfcSMarkus Armbruster     }
3327843c4cfcSMarkus Armbruster     if (n->net_conf.speed >= 0) {
33289473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
33299473939eSJason Baron     }
33309473939eSJason Baron 
33319711cd0dSJens Freimann     if (n->failover) {
3332b91ad981SJuan Quintela         n->primary_listener.hide_device = failover_hide_primary_device;
3333e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, true);
33349711cd0dSJens Freimann         device_listener_register(&n->primary_listener);
33359711cd0dSJens Freimann         n->migration_state.notify = virtio_net_migration_state_notifier;
33369711cd0dSJens Freimann         add_migration_state_change_notifier(&n->migration_state);
33379711cd0dSJens Freimann         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
33389711cd0dSJens Freimann     }
33399711cd0dSJens Freimann 
3340da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
3341284a32f0SAndreas Färber     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
334217ec5a86SKONRAD Frederic 
33431c0fbfa3SMichael S. Tsirkin     /*
33441c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
33451c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
33461c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
33471c0fbfa3SMichael S. Tsirkin      */
33481c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
33491c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
33505f997fd1SMichal Privoznik         !is_power_of_2(n->net_conf.rx_queue_size)) {
33511c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
33521c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
33531c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
33541c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
33551c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
33561c0fbfa3SMichael S. Tsirkin         return;
33571c0fbfa3SMichael S. Tsirkin     }
33581c0fbfa3SMichael S. Tsirkin 
33599b02e161SWei Wang     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
33609b02e161SWei Wang         n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
33619b02e161SWei Wang         !is_power_of_2(n->net_conf.tx_queue_size)) {
33629b02e161SWei Wang         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
33639b02e161SWei Wang                    "must be a power of 2 between %d and %d",
33649b02e161SWei Wang                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
33659b02e161SWei Wang                    VIRTQUEUE_MAX_SIZE);
33669b02e161SWei Wang         virtio_cleanup(vdev);
33679b02e161SWei Wang         return;
33689b02e161SWei Wang     }
33699b02e161SWei Wang 
3370575a1c0eSJiri Pirko     n->max_queues = MAX(n->nic_conf.peers.queues, 1);
337187b3bd1cSJason Wang     if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
33727e0e736eSJason Wang         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
3373631b22eaSStefan Weil                    "must be a positive integer less than %d.",
337487b3bd1cSJason Wang                    n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
33757e0e736eSJason Wang         virtio_cleanup(vdev);
33767e0e736eSJason Wang         return;
33777e0e736eSJason Wang     }
33786e790746SPaolo Bonzini     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
33796e790746SPaolo Bonzini     n->curr_queues = 1;
33801773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
33816e790746SPaolo Bonzini 
33821773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
33831773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
33840765691eSMarkus Armbruster         warn_report("virtio-net: "
33856e790746SPaolo Bonzini                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
33861773d9eeSKONRAD Frederic                     n->net_conf.tx);
33870765691eSMarkus Armbruster         error_printf("Defaulting to \"bh\"");
33886e790746SPaolo Bonzini     }
33896e790746SPaolo Bonzini 
33902eef278bSMichael S. Tsirkin     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
33912eef278bSMichael S. Tsirkin                                     n->net_conf.tx_queue_size);
33929b02e161SWei Wang 
3393da51a335SJason Wang     for (i = 0; i < n->max_queues; i++) {
3394f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i);
3395da51a335SJason Wang     }
3396da51a335SJason Wang 
339717a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
33981773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
33991773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
34006e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
34019d8c6a25SDr. David Alan Gilbert     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
34029d8c6a25SDr. David Alan Gilbert                               QEMU_CLOCK_VIRTUAL,
3403f57fcf70SJason Wang                               virtio_net_announce_timer, n);
3404b2c929f0SDr. David Alan Gilbert     n->announce_timer.round = 0;
34056e790746SPaolo Bonzini 
34068a253ec2SKONRAD Frederic     if (n->netclient_type) {
34078a253ec2SKONRAD Frederic         /*
34088a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
34098a253ec2SKONRAD Frederic          */
34108a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
34118a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
34128a253ec2SKONRAD Frederic     } else {
34131773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3414284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
34158a253ec2SKONRAD Frederic     }
34168a253ec2SKONRAD Frederic 
3417d4c62930SBin Meng     for (i = 0; i < n->max_queues; i++) {
3418d4c62930SBin Meng         n->nic->ncs[i].do_not_pad = true;
3419d4c62930SBin Meng     }
3420d4c62930SBin Meng 
34216e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
34226e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
34236e790746SPaolo Bonzini         for (i = 0; i < n->max_queues; i++) {
3424d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
34256e790746SPaolo Bonzini         }
34266e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
34276e790746SPaolo Bonzini     } else {
34286e790746SPaolo Bonzini         n->host_hdr_len = 0;
34296e790746SPaolo Bonzini     }
34306e790746SPaolo Bonzini 
34311773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
34326e790746SPaolo Bonzini 
34336e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
34341773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
3435e22f0603SYuri Benditovich     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
34366e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
34376e790746SPaolo Bonzini 
34386e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
34396e790746SPaolo Bonzini 
34406e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
34416e790746SPaolo Bonzini 
3442b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
3443b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
3444b1be4280SAmos Kong 
3445e87936eaSCindy Lu    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3446e87936eaSCindy Lu         struct virtio_net_config netcfg = {};
3447e87936eaSCindy Lu         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3448e87936eaSCindy Lu         vhost_net_set_config(get_vhost_net(nc->peer),
3449e87936eaSCindy Lu             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER);
3450e87936eaSCindy Lu     }
34512974e916SYuri Benditovich     QTAILQ_INIT(&n->rsc_chains);
3452284a32f0SAndreas Färber     n->qdev = dev;
34534474e37aSYuri Benditovich 
34544474e37aSYuri Benditovich     net_rx_pkt_init(&n->rx_pkt, false);
3455*0145c393SAndrew Melnychenko 
3456*0145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
3457*0145c393SAndrew Melnychenko         virtio_net_load_ebpf(n);
3458*0145c393SAndrew Melnychenko     }
345917ec5a86SKONRAD Frederic }
346017ec5a86SKONRAD Frederic 
3461b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev)
346217ec5a86SKONRAD Frederic {
3463306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3464306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3465f9d6dbf0SWen Congyang     int i, max_queues;
346617ec5a86SKONRAD Frederic 
3467*0145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
3468*0145c393SAndrew Melnychenko         virtio_net_unload_ebpf(n);
3469*0145c393SAndrew Melnychenko     }
3470*0145c393SAndrew Melnychenko 
347117ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
347217ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
347317ec5a86SKONRAD Frederic 
34748a253ec2SKONRAD Frederic     g_free(n->netclient_name);
34758a253ec2SKONRAD Frederic     n->netclient_name = NULL;
34768a253ec2SKONRAD Frederic     g_free(n->netclient_type);
34778a253ec2SKONRAD Frederic     n->netclient_type = NULL;
34788a253ec2SKONRAD Frederic 
347917ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
348017ec5a86SKONRAD Frederic     g_free(n->vlans);
348117ec5a86SKONRAD Frederic 
34829711cd0dSJens Freimann     if (n->failover) {
348365018100SJuan Quintela         device_listener_unregister(&n->primary_listener);
34849711cd0dSJens Freimann     }
34859711cd0dSJens Freimann 
3486f9d6dbf0SWen Congyang     max_queues = n->multiqueue ? n->max_queues : 1;
3487f9d6dbf0SWen Congyang     for (i = 0; i < max_queues; i++) {
3488f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
348917ec5a86SKONRAD Frederic     }
3490d945d9f1SYuri Benditovich     /* delete also control vq */
3491d945d9f1SYuri Benditovich     virtio_del_queue(vdev, max_queues * 2);
3492944458b6SDr. David Alan Gilbert     qemu_announce_timer_del(&n->announce_timer, false);
349317ec5a86SKONRAD Frederic     g_free(n->vqs);
349417ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
34952974e916SYuri Benditovich     virtio_net_rsc_cleanup(n);
349659079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
34974474e37aSYuri Benditovich     net_rx_pkt_uninit(n->rx_pkt);
34986a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
349917ec5a86SKONRAD Frederic }
350017ec5a86SKONRAD Frederic 
350117ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
350217ec5a86SKONRAD Frederic {
350317ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
350417ec5a86SKONRAD Frederic 
350517ec5a86SKONRAD Frederic     /*
350617ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
350717ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
350817ec5a86SKONRAD Frederic      */
350917ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
3510aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3511aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
351240c2281cSMarkus Armbruster                                   DEVICE(n));
3513*0145c393SAndrew Melnychenko 
3514*0145c393SAndrew Melnychenko     ebpf_rss_init(&n->ebpf_rss);
351517ec5a86SKONRAD Frederic }
351617ec5a86SKONRAD Frederic 
351744b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque)
35184d45dcfbSHalil Pasic {
35194d45dcfbSHalil Pasic     VirtIONet *n = opaque;
35204d45dcfbSHalil Pasic 
35214d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
35224d45dcfbSHalil Pasic      * it might keep writing to memory. */
35234d45dcfbSHalil Pasic     assert(!n->vhost_started);
352444b1ff31SDr. David Alan Gilbert 
352544b1ff31SDr. David Alan Gilbert     return 0;
35264d45dcfbSHalil Pasic }
35274d45dcfbSHalil Pasic 
35289711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque)
35299711cd0dSJens Freimann {
35309711cd0dSJens Freimann     DeviceState *dev = opaque;
353121e8709bSJuan Quintela     DeviceState *primary;
35329711cd0dSJens Freimann     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
35339711cd0dSJens Freimann     VirtIONet *n = VIRTIO_NET(vdev);
35349711cd0dSJens Freimann 
3535284f42a5SJens Freimann     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3536284f42a5SJens Freimann         return false;
3537284f42a5SJens Freimann     }
353821e8709bSJuan Quintela     primary = failover_find_primary_device(n);
353921e8709bSJuan Quintela     return primary ? primary->pending_deleted_event : false;
35409711cd0dSJens Freimann }
35419711cd0dSJens Freimann 
35429711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque)
35439711cd0dSJens Freimann {
35449711cd0dSJens Freimann     DeviceState *dev = opaque;
35459711cd0dSJens Freimann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
35469711cd0dSJens Freimann 
35479711cd0dSJens Freimann     return vdc->primary_unplug_pending(dev);
35489711cd0dSJens Freimann }
35499711cd0dSJens Freimann 
35504d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
35514d45dcfbSHalil Pasic     .name = "virtio-net",
35524d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
35534d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
35544d45dcfbSHalil Pasic     .fields = (VMStateField[]) {
35554d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
35564d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
35574d45dcfbSHalil Pasic     },
35584d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
35599711cd0dSJens Freimann     .dev_unplug_pending = dev_unplug_pending,
35604d45dcfbSHalil Pasic };
3561290c2428SDr. David Alan Gilbert 
356217ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
3563127833eeSJason Baron     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3564127833eeSJason Baron                     VIRTIO_NET_F_CSUM, true),
3565127833eeSJason Baron     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
356687108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
3567127833eeSJason Baron     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3568127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
356987108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
3570127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
357187108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
3572127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
357387108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
3574127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
357587108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
3576127833eeSJason Baron     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
357787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3578127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
357987108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
3580127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
358187108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
3582127833eeSJason Baron     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
358387108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
3584127833eeSJason Baron     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
358587108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
3586127833eeSJason Baron     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
358787108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
3588127833eeSJason Baron     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
358987108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
3590127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
359187108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
3592127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
359387108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
3594127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
359587108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
3596127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
359787108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3598127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
359987108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3600127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
360187108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3602127833eeSJason Baron     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
360359079029SYuri Benditovich     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
360459079029SYuri Benditovich                     VIRTIO_NET_F_RSS, false),
3605e22f0603SYuri Benditovich     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3606e22f0603SYuri Benditovich                     VIRTIO_NET_F_HASH_REPORT, false),
36072974e916SYuri Benditovich     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
36082974e916SYuri Benditovich                     VIRTIO_NET_F_RSC_EXT, false),
36092974e916SYuri Benditovich     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
36102974e916SYuri Benditovich                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
361117ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
361217ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
361317ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
361417ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
361517ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
36161c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
36171c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
36189b02e161SWei Wang     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
36199b02e161SWei Wang                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3620a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
362175ebec11SMaxime Coquelin     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
362275ebec11SMaxime Coquelin                      true),
36239473939eSJason Baron     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
36249473939eSJason Baron     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
36259711cd0dSJens Freimann     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
362617ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
362717ec5a86SKONRAD Frederic };
362817ec5a86SKONRAD Frederic 
362917ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
363017ec5a86SKONRAD Frederic {
363117ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
363217ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3633e6f746b3SAndreas Färber 
36344f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
3635290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
3636125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3637e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
3638306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
363917ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
364017ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
364117ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
364217ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
364317ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
364417ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
364517ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
364617ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
364717ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
36482a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
36497788c3f2SMikhail Sennikovsky     vdc->post_load = virtio_net_post_load_virtio;
3650982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
36519711cd0dSJens Freimann     vdc->primary_unplug_pending = primary_unplug_pending;
365217ec5a86SKONRAD Frederic }
365317ec5a86SKONRAD Frederic 
365417ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
365517ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
365617ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
365717ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
365817ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
365917ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
366017ec5a86SKONRAD Frederic };
366117ec5a86SKONRAD Frederic 
366217ec5a86SKONRAD Frederic static void virtio_register_types(void)
366317ec5a86SKONRAD Frederic {
366417ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
366517ec5a86SKONRAD Frederic }
366617ec5a86SKONRAD Frederic 
366717ec5a86SKONRAD Frederic type_init(virtio_register_types)
3668