xref: /openbmc/qemu/hw/net/virtio-net.c (revision 1e157667d7657418b68fadb5cc016c6804e17501)
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 
926e790746SPaolo Bonzini static 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 
74059079029SYuri Benditovich     virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
741e22f0603SYuri Benditovich     virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
74275ebec11SMaxime Coquelin     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
74375ebec11SMaxime Coquelin     vdev->backend_features = features;
74475ebec11SMaxime Coquelin 
74575ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
74675ebec11SMaxime Coquelin             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
74775ebec11SMaxime Coquelin         features |= (1ULL << VIRTIO_NET_F_MTU);
74875ebec11SMaxime Coquelin     }
74975ebec11SMaxime Coquelin 
75075ebec11SMaxime Coquelin     return features;
7516e790746SPaolo Bonzini }
7526e790746SPaolo Bonzini 
753019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
7546e790746SPaolo Bonzini {
755019a3edbSGerd Hoffmann     uint64_t features = 0;
7566e790746SPaolo Bonzini 
7576e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
7586e790746SPaolo Bonzini      * but also these: */
7590cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7600cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
7610cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
7620cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
7630cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
7646e790746SPaolo Bonzini 
7656e790746SPaolo Bonzini     return features;
7666e790746SPaolo Bonzini }
7676e790746SPaolo Bonzini 
768644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
769644c9858SDmitry Fleytman {
770ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
771644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
772644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
773644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
774644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
775644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
776644c9858SDmitry Fleytman }
777644c9858SDmitry Fleytman 
778644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
779644c9858SDmitry Fleytman {
780644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
781644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
782644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
783644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
784644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
785644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
786644c9858SDmitry Fleytman 
787644c9858SDmitry Fleytman     return guest_offloads_mask & features;
788644c9858SDmitry Fleytman }
789644c9858SDmitry Fleytman 
790644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
791644c9858SDmitry Fleytman {
792644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
793644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
794644c9858SDmitry Fleytman }
795644c9858SDmitry Fleytman 
796f5e1847bSJuan Quintela typedef struct {
797f5e1847bSJuan Quintela     VirtIONet *n;
798f5e1847bSJuan Quintela     char *id;
799f5e1847bSJuan Quintela } FailoverId;
800f5e1847bSJuan Quintela 
801f5e1847bSJuan Quintela /**
802f5e1847bSJuan Quintela  * Set the id of the failover primary device
803f5e1847bSJuan Quintela  *
804f5e1847bSJuan Quintela  * @opaque: FailoverId to setup
805f5e1847bSJuan Quintela  * @opts: opts for device we are handling
806f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
807f5e1847bSJuan Quintela  */
808f5e1847bSJuan Quintela static int failover_set_primary(void *opaque, QemuOpts *opts, Error **errp)
809f5e1847bSJuan Quintela {
810f5e1847bSJuan Quintela     FailoverId *fid = opaque;
811f5e1847bSJuan Quintela     const char *standby_id = qemu_opt_get(opts, "failover_pair_id");
812f5e1847bSJuan Quintela 
813f5e1847bSJuan Quintela     if (g_strcmp0(standby_id, fid->n->netclient_name) == 0) {
814f5e1847bSJuan Quintela         fid->id = g_strdup(opts->id);
815f5e1847bSJuan Quintela         return 1;
816f5e1847bSJuan Quintela     }
817f5e1847bSJuan Quintela 
818f5e1847bSJuan Quintela     return 0;
819f5e1847bSJuan Quintela }
820f5e1847bSJuan Quintela 
821f5e1847bSJuan Quintela /**
822f5e1847bSJuan Quintela  * Find the primary device id for this failover virtio-net
823f5e1847bSJuan Quintela  *
824f5e1847bSJuan Quintela  * @n: VirtIONet device
825f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
826f5e1847bSJuan Quintela  */
827f5e1847bSJuan Quintela static char *failover_find_primary_device_id(VirtIONet *n)
828f5e1847bSJuan Quintela {
829f5e1847bSJuan Quintela     Error *err = NULL;
830f5e1847bSJuan Quintela     FailoverId fid;
831f5e1847bSJuan Quintela 
8323abad4a2SJuan Quintela     fid.n = n;
833f5e1847bSJuan Quintela     if (!qemu_opts_foreach(qemu_find_opts("device"),
834f5e1847bSJuan Quintela                            failover_set_primary, &fid, &err)) {
835f5e1847bSJuan Quintela         return NULL;
836f5e1847bSJuan Quintela     }
837f5e1847bSJuan Quintela     return fid.id;
838f5e1847bSJuan Quintela }
839f5e1847bSJuan Quintela 
84085d3b931SJuan Quintela /**
84185d3b931SJuan Quintela  * Find the primary device for this failover virtio-net
84285d3b931SJuan Quintela  *
84385d3b931SJuan Quintela  * @n: VirtIONet device
84485d3b931SJuan Quintela  * @errp: returns an error if this function fails
84585d3b931SJuan Quintela  */
8460a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n)
8479711cd0dSJens Freimann {
848f5e1847bSJuan Quintela     char *id = failover_find_primary_device_id(n);
8499711cd0dSJens Freimann 
850f5e1847bSJuan Quintela     if (!id) {
8519711cd0dSJens Freimann         return NULL;
8529711cd0dSJens Freimann     }
853f5e1847bSJuan Quintela 
8543abad4a2SJuan Quintela     return qdev_find_recursive(sysbus_get_default(), id);
8559711cd0dSJens Freimann }
8569711cd0dSJens Freimann 
85721e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp)
85821e8709bSJuan Quintela {
85921e8709bSJuan Quintela     Error *err = NULL;
86021e8709bSJuan Quintela     QemuOpts *opts;
86121e8709bSJuan Quintela     char *id;
86221e8709bSJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
86321e8709bSJuan Quintela 
86421e8709bSJuan Quintela     if (dev) {
86521e8709bSJuan Quintela         return;
86621e8709bSJuan Quintela     }
86721e8709bSJuan Quintela 
86821e8709bSJuan Quintela     id = failover_find_primary_device_id(n);
86921e8709bSJuan Quintela     if (!id) {
87097ca9c59SLaurent Vivier         error_setg(errp, "Primary device not found");
87197ca9c59SLaurent Vivier         error_append_hint(errp, "Virtio-net failover will not work. Make "
87297ca9c59SLaurent Vivier                           "sure primary device has parameter"
87397ca9c59SLaurent Vivier                           " failover_pair_id=%s\n", n->netclient_name);
87421e8709bSJuan Quintela         return;
87521e8709bSJuan Quintela     }
87621e8709bSJuan Quintela     opts = qemu_opts_find(qemu_find_opts("device"), id);
87797ca9c59SLaurent Vivier     g_assert(opts); /* cannot be NULL because id was found using opts list */
87821e8709bSJuan Quintela     dev = qdev_device_add(opts, &err);
87921e8709bSJuan Quintela     if (err) {
88021e8709bSJuan Quintela         qemu_opts_del(opts);
88100e7b129SLaurent Vivier     } else {
88200e7b129SLaurent Vivier         object_unref(OBJECT(dev));
88321e8709bSJuan Quintela     }
88421e8709bSJuan Quintela     error_propagate(errp, err);
88521e8709bSJuan Quintela }
88621e8709bSJuan Quintela 
887d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
8886e790746SPaolo Bonzini {
88917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
8909711cd0dSJens Freimann     Error *err = NULL;
8916e790746SPaolo Bonzini     int i;
8926e790746SPaolo Bonzini 
89375ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
89475ebec11SMaxime Coquelin             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
89575ebec11SMaxime Coquelin         features &= ~(1ULL << VIRTIO_NET_F_MTU);
89675ebec11SMaxime Coquelin     }
89775ebec11SMaxime Coquelin 
898ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
89959079029SYuri Benditovich                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
90095129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
9016e790746SPaolo Bonzini 
902ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
90395129d6fSCornelia Huck                                virtio_has_feature(features,
904bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
90595129d6fSCornelia Huck                                virtio_has_feature(features,
906e22f0603SYuri Benditovich                                                   VIRTIO_F_VERSION_1),
907e22f0603SYuri Benditovich                                virtio_has_feature(features,
908e22f0603SYuri Benditovich                                                   VIRTIO_NET_F_HASH_REPORT));
9096e790746SPaolo Bonzini 
9102974e916SYuri Benditovich     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9112974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
9122974e916SYuri Benditovich     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9132974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
914e22f0603SYuri Benditovich     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
9152974e916SYuri Benditovich 
9166e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
917644c9858SDmitry Fleytman         n->curr_guest_offloads =
918644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
919644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
9206e790746SPaolo Bonzini     }
9216e790746SPaolo Bonzini 
9226e790746SPaolo Bonzini     for (i = 0;  i < n->max_queues; i++) {
9236e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
9246e790746SPaolo Bonzini 
925ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
9266e790746SPaolo Bonzini             continue;
9276e790746SPaolo Bonzini         }
928ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
9296e790746SPaolo Bonzini     }
9300b1eaa88SStefan Fritsch 
93195129d6fSCornelia Huck     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
9320b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
9330b1eaa88SStefan Fritsch     } else {
9340b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
9350b1eaa88SStefan Fritsch     }
9369711cd0dSJens Freimann 
9379711cd0dSJens Freimann     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
9389711cd0dSJens Freimann         qapi_event_send_failover_negotiated(n->netclient_name);
939e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, false);
9409711cd0dSJens Freimann         failover_add_primary(n, &err);
9419711cd0dSJens Freimann         if (err) {
9429711cd0dSJens Freimann             warn_report_err(err);
9439711cd0dSJens Freimann         }
9446e790746SPaolo Bonzini     }
94521e8709bSJuan Quintela }
9466e790746SPaolo Bonzini 
9476e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
9486e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
9496e790746SPaolo Bonzini {
9506e790746SPaolo Bonzini     uint8_t on;
9516e790746SPaolo Bonzini     size_t s;
952b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
9536e790746SPaolo Bonzini 
9546e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
9556e790746SPaolo Bonzini     if (s != sizeof(on)) {
9566e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
9576e790746SPaolo Bonzini     }
9586e790746SPaolo Bonzini 
9596e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
9606e790746SPaolo Bonzini         n->promisc = on;
9616e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
9626e790746SPaolo Bonzini         n->allmulti = on;
9636e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
9646e790746SPaolo Bonzini         n->alluni = on;
9656e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
9666e790746SPaolo Bonzini         n->nomulti = on;
9676e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
9686e790746SPaolo Bonzini         n->nouni = on;
9696e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
9706e790746SPaolo Bonzini         n->nobcast = on;
9716e790746SPaolo Bonzini     } else {
9726e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
9736e790746SPaolo Bonzini     }
9746e790746SPaolo Bonzini 
975b1be4280SAmos Kong     rxfilter_notify(nc);
976b1be4280SAmos Kong 
9776e790746SPaolo Bonzini     return VIRTIO_NET_OK;
9786e790746SPaolo Bonzini }
9796e790746SPaolo Bonzini 
980644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
981644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
982644c9858SDmitry Fleytman {
983644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
984644c9858SDmitry Fleytman     uint64_t offloads;
985644c9858SDmitry Fleytman     size_t s;
986644c9858SDmitry Fleytman 
98795129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
988644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
989644c9858SDmitry Fleytman     }
990644c9858SDmitry Fleytman 
991644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
992644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
993644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
994644c9858SDmitry Fleytman     }
995644c9858SDmitry Fleytman 
996644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
997644c9858SDmitry Fleytman         uint64_t supported_offloads;
998644c9858SDmitry Fleytman 
999189ae6bbSJason Wang         offloads = virtio_ldq_p(vdev, &offloads);
1000189ae6bbSJason Wang 
1001644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
1002644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1003644c9858SDmitry Fleytman         }
1004644c9858SDmitry Fleytman 
10052974e916SYuri Benditovich         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10062974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
10072974e916SYuri Benditovich         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10082974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
10092974e916SYuri Benditovich         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
10102974e916SYuri Benditovich 
1011644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
1012644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
1013644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1014644c9858SDmitry Fleytman         }
1015644c9858SDmitry Fleytman 
1016644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
1017644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1018644c9858SDmitry Fleytman 
1019644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
1020644c9858SDmitry Fleytman     } else {
1021644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1022644c9858SDmitry Fleytman     }
1023644c9858SDmitry Fleytman }
1024644c9858SDmitry Fleytman 
10256e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
10266e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
10276e790746SPaolo Bonzini {
10281399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
10296e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
10306e790746SPaolo Bonzini     size_t s;
1031b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
10326e790746SPaolo Bonzini 
10336e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
10346e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
10356e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
10366e790746SPaolo Bonzini         }
10376e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
10386e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
10396e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1040b1be4280SAmos Kong         rxfilter_notify(nc);
1041b1be4280SAmos Kong 
10426e790746SPaolo Bonzini         return VIRTIO_NET_OK;
10436e790746SPaolo Bonzini     }
10446e790746SPaolo Bonzini 
10456e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
10466e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10476e790746SPaolo Bonzini     }
10486e790746SPaolo Bonzini 
1049cae2e556SAmos Kong     int in_use = 0;
1050cae2e556SAmos Kong     int first_multi = 0;
1051cae2e556SAmos Kong     uint8_t uni_overflow = 0;
1052cae2e556SAmos Kong     uint8_t multi_overflow = 0;
1053cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
10546e790746SPaolo Bonzini 
10556e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
10566e790746SPaolo Bonzini                    sizeof(mac_data.entries));
10571399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
10586e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1059b1be4280SAmos Kong         goto error;
10606e790746SPaolo Bonzini     }
10616e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
10626e790746SPaolo Bonzini 
10636e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1064b1be4280SAmos Kong         goto error;
10656e790746SPaolo Bonzini     }
10666e790746SPaolo Bonzini 
10676e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1068cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
10696e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
10706e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1071b1be4280SAmos Kong             goto error;
10726e790746SPaolo Bonzini         }
1073cae2e556SAmos Kong         in_use += mac_data.entries;
10746e790746SPaolo Bonzini     } else {
1075cae2e556SAmos Kong         uni_overflow = 1;
10766e790746SPaolo Bonzini     }
10776e790746SPaolo Bonzini 
10786e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
10796e790746SPaolo Bonzini 
1080cae2e556SAmos Kong     first_multi = in_use;
10816e790746SPaolo Bonzini 
10826e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
10836e790746SPaolo Bonzini                    sizeof(mac_data.entries));
10841399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
10856e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1086b1be4280SAmos Kong         goto error;
10876e790746SPaolo Bonzini     }
10886e790746SPaolo Bonzini 
10896e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
10906e790746SPaolo Bonzini 
10916e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1092b1be4280SAmos Kong         goto error;
10936e790746SPaolo Bonzini     }
10946e790746SPaolo Bonzini 
1095edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1096cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
10976e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
10986e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1099b1be4280SAmos Kong             goto error;
11006e790746SPaolo Bonzini         }
1101cae2e556SAmos Kong         in_use += mac_data.entries;
11026e790746SPaolo Bonzini     } else {
1103cae2e556SAmos Kong         multi_overflow = 1;
11046e790746SPaolo Bonzini     }
11056e790746SPaolo Bonzini 
1106cae2e556SAmos Kong     n->mac_table.in_use = in_use;
1107cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
1108cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
1109cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
1110cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1111cae2e556SAmos Kong     g_free(macs);
1112b1be4280SAmos Kong     rxfilter_notify(nc);
1113b1be4280SAmos Kong 
11146e790746SPaolo Bonzini     return VIRTIO_NET_OK;
1115b1be4280SAmos Kong 
1116b1be4280SAmos Kong error:
1117cae2e556SAmos Kong     g_free(macs);
1118b1be4280SAmos Kong     return VIRTIO_NET_ERR;
11196e790746SPaolo Bonzini }
11206e790746SPaolo Bonzini 
11216e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
11226e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
11236e790746SPaolo Bonzini {
11241399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11256e790746SPaolo Bonzini     uint16_t vid;
11266e790746SPaolo Bonzini     size_t s;
1127b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11286e790746SPaolo Bonzini 
11296e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
11301399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
11316e790746SPaolo Bonzini     if (s != sizeof(vid)) {
11326e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11336e790746SPaolo Bonzini     }
11346e790746SPaolo Bonzini 
11356e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
11366e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11376e790746SPaolo Bonzini 
11386e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
11396e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
11406e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
11416e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
11426e790746SPaolo Bonzini     else
11436e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11446e790746SPaolo Bonzini 
1145b1be4280SAmos Kong     rxfilter_notify(nc);
1146b1be4280SAmos Kong 
11476e790746SPaolo Bonzini     return VIRTIO_NET_OK;
11486e790746SPaolo Bonzini }
11496e790746SPaolo Bonzini 
1150f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1151f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
1152f57fcf70SJason Wang {
11539d8c6a25SDr. David Alan Gilbert     trace_virtio_net_handle_announce(n->announce_timer.round);
1154f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1155f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
1156f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
11579d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
11589d8c6a25SDr. David Alan Gilbert             qemu_announce_timer_step(&n->announce_timer);
1159f57fcf70SJason Wang         }
1160f57fcf70SJason Wang         return VIRTIO_NET_OK;
1161f57fcf70SJason Wang     } else {
1162f57fcf70SJason Wang         return VIRTIO_NET_ERR;
1163f57fcf70SJason Wang     }
1164f57fcf70SJason Wang }
1165f57fcf70SJason Wang 
116659079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n)
116759079029SYuri Benditovich {
116859079029SYuri Benditovich     if (n->rss_data.enabled) {
116959079029SYuri Benditovich         trace_virtio_net_rss_disable();
117059079029SYuri Benditovich     }
117159079029SYuri Benditovich     n->rss_data.enabled = false;
117259079029SYuri Benditovich }
117359079029SYuri Benditovich 
117459079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n,
1175e22f0603SYuri Benditovich                                       struct iovec *iov,
1176e22f0603SYuri Benditovich                                       unsigned int iov_cnt,
1177e22f0603SYuri Benditovich                                       bool do_rss)
117859079029SYuri Benditovich {
117959079029SYuri Benditovich     VirtIODevice *vdev = VIRTIO_DEVICE(n);
118059079029SYuri Benditovich     struct virtio_net_rss_config cfg;
118159079029SYuri Benditovich     size_t s, offset = 0, size_get;
118259079029SYuri Benditovich     uint16_t queues, i;
118359079029SYuri Benditovich     struct {
118459079029SYuri Benditovich         uint16_t us;
118559079029SYuri Benditovich         uint8_t b;
118659079029SYuri Benditovich     } QEMU_PACKED temp;
118759079029SYuri Benditovich     const char *err_msg = "";
118859079029SYuri Benditovich     uint32_t err_value = 0;
118959079029SYuri Benditovich 
1190e22f0603SYuri Benditovich     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
119159079029SYuri Benditovich         err_msg = "RSS is not negotiated";
119259079029SYuri Benditovich         goto error;
119359079029SYuri Benditovich     }
1194e22f0603SYuri Benditovich     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1195e22f0603SYuri Benditovich         err_msg = "Hash report is not negotiated";
1196e22f0603SYuri Benditovich         goto error;
1197e22f0603SYuri Benditovich     }
119859079029SYuri Benditovich     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
119959079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
120059079029SYuri Benditovich     if (s != size_get) {
120159079029SYuri Benditovich         err_msg = "Short command buffer";
120259079029SYuri Benditovich         err_value = (uint32_t)s;
120359079029SYuri Benditovich         goto error;
120459079029SYuri Benditovich     }
120559079029SYuri Benditovich     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
120659079029SYuri Benditovich     n->rss_data.indirections_len =
120759079029SYuri Benditovich         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
120859079029SYuri Benditovich     n->rss_data.indirections_len++;
1209e22f0603SYuri Benditovich     if (!do_rss) {
1210e22f0603SYuri Benditovich         n->rss_data.indirections_len = 1;
1211e22f0603SYuri Benditovich     }
121259079029SYuri Benditovich     if (!is_power_of_2(n->rss_data.indirections_len)) {
121359079029SYuri Benditovich         err_msg = "Invalid size of indirection table";
121459079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
121559079029SYuri Benditovich         goto error;
121659079029SYuri Benditovich     }
121759079029SYuri Benditovich     if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
121859079029SYuri Benditovich         err_msg = "Too large indirection table";
121959079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
122059079029SYuri Benditovich         goto error;
122159079029SYuri Benditovich     }
1222e22f0603SYuri Benditovich     n->rss_data.default_queue = do_rss ?
1223e22f0603SYuri Benditovich         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
122459079029SYuri Benditovich     if (n->rss_data.default_queue >= n->max_queues) {
122559079029SYuri Benditovich         err_msg = "Invalid default queue";
122659079029SYuri Benditovich         err_value = n->rss_data.default_queue;
122759079029SYuri Benditovich         goto error;
122859079029SYuri Benditovich     }
122959079029SYuri Benditovich     offset += size_get;
123059079029SYuri Benditovich     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
123159079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
123259079029SYuri Benditovich     n->rss_data.indirections_table = g_malloc(size_get);
123359079029SYuri Benditovich     if (!n->rss_data.indirections_table) {
123459079029SYuri Benditovich         err_msg = "Can't allocate indirections table";
123559079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
123659079029SYuri Benditovich         goto error;
123759079029SYuri Benditovich     }
123859079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset,
123959079029SYuri Benditovich                    n->rss_data.indirections_table, size_get);
124059079029SYuri Benditovich     if (s != size_get) {
124159079029SYuri Benditovich         err_msg = "Short indirection table buffer";
124259079029SYuri Benditovich         err_value = (uint32_t)s;
124359079029SYuri Benditovich         goto error;
124459079029SYuri Benditovich     }
124559079029SYuri Benditovich     for (i = 0; i < n->rss_data.indirections_len; ++i) {
124659079029SYuri Benditovich         uint16_t val = n->rss_data.indirections_table[i];
124759079029SYuri Benditovich         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
124859079029SYuri Benditovich     }
124959079029SYuri Benditovich     offset += size_get;
125059079029SYuri Benditovich     size_get = sizeof(temp);
125159079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
125259079029SYuri Benditovich     if (s != size_get) {
125359079029SYuri Benditovich         err_msg = "Can't get queues";
125459079029SYuri Benditovich         err_value = (uint32_t)s;
125559079029SYuri Benditovich         goto error;
125659079029SYuri Benditovich     }
1257e22f0603SYuri Benditovich     queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues;
125859079029SYuri Benditovich     if (queues == 0 || queues > n->max_queues) {
125959079029SYuri Benditovich         err_msg = "Invalid number of queues";
126059079029SYuri Benditovich         err_value = queues;
126159079029SYuri Benditovich         goto error;
126259079029SYuri Benditovich     }
126359079029SYuri Benditovich     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
126459079029SYuri Benditovich         err_msg = "Invalid key size";
126559079029SYuri Benditovich         err_value = temp.b;
126659079029SYuri Benditovich         goto error;
126759079029SYuri Benditovich     }
126859079029SYuri Benditovich     if (!temp.b && n->rss_data.hash_types) {
126959079029SYuri Benditovich         err_msg = "No key provided";
127059079029SYuri Benditovich         err_value = 0;
127159079029SYuri Benditovich         goto error;
127259079029SYuri Benditovich     }
127359079029SYuri Benditovich     if (!temp.b && !n->rss_data.hash_types) {
127459079029SYuri Benditovich         virtio_net_disable_rss(n);
127559079029SYuri Benditovich         return queues;
127659079029SYuri Benditovich     }
127759079029SYuri Benditovich     offset += size_get;
127859079029SYuri Benditovich     size_get = temp.b;
127959079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
128059079029SYuri Benditovich     if (s != size_get) {
128159079029SYuri Benditovich         err_msg = "Can get key buffer";
128259079029SYuri Benditovich         err_value = (uint32_t)s;
128359079029SYuri Benditovich         goto error;
128459079029SYuri Benditovich     }
128559079029SYuri Benditovich     n->rss_data.enabled = true;
128659079029SYuri Benditovich     trace_virtio_net_rss_enable(n->rss_data.hash_types,
128759079029SYuri Benditovich                                 n->rss_data.indirections_len,
128859079029SYuri Benditovich                                 temp.b);
128959079029SYuri Benditovich     return queues;
129059079029SYuri Benditovich error:
129159079029SYuri Benditovich     trace_virtio_net_rss_error(err_msg, err_value);
129259079029SYuri Benditovich     virtio_net_disable_rss(n);
129359079029SYuri Benditovich     return 0;
129459079029SYuri Benditovich }
129559079029SYuri Benditovich 
12966e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
12976e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
12986e790746SPaolo Bonzini {
129917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13006e790746SPaolo Bonzini     uint16_t queues;
13016e790746SPaolo Bonzini 
130259079029SYuri Benditovich     virtio_net_disable_rss(n);
1303e22f0603SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1304e22f0603SYuri Benditovich         queues = virtio_net_handle_rss(n, iov, iov_cnt, false);
1305e22f0603SYuri Benditovich         return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1306e22f0603SYuri Benditovich     }
130759079029SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1308e22f0603SYuri Benditovich         queues = virtio_net_handle_rss(n, iov, iov_cnt, true);
130959079029SYuri Benditovich     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
131059079029SYuri Benditovich         struct virtio_net_ctrl_mq mq;
131159079029SYuri Benditovich         size_t s;
131259079029SYuri Benditovich         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
131359079029SYuri Benditovich             return VIRTIO_NET_ERR;
131459079029SYuri Benditovich         }
13156e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
13166e790746SPaolo Bonzini         if (s != sizeof(mq)) {
13176e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
13186e790746SPaolo Bonzini         }
131959079029SYuri Benditovich         queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
13206e790746SPaolo Bonzini 
132159079029SYuri Benditovich     } else {
13226e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
13236e790746SPaolo Bonzini     }
13246e790746SPaolo Bonzini 
13256e790746SPaolo Bonzini     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
13266e790746SPaolo Bonzini         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
13276e790746SPaolo Bonzini         queues > n->max_queues ||
13286e790746SPaolo Bonzini         !n->multiqueue) {
13296e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
13306e790746SPaolo Bonzini     }
13316e790746SPaolo Bonzini 
13326e790746SPaolo Bonzini     n->curr_queues = queues;
13336e790746SPaolo Bonzini     /* stop the backend before changing the number of queues to avoid handling a
13346e790746SPaolo Bonzini      * disabled queue */
133517a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
13366e790746SPaolo Bonzini     virtio_net_set_queues(n);
13376e790746SPaolo Bonzini 
13386e790746SPaolo Bonzini     return VIRTIO_NET_OK;
13396e790746SPaolo Bonzini }
1340ba7eadb5SGreg Kurz 
13416e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
13426e790746SPaolo Bonzini {
134317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
13446e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
13456e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
134651b19ebeSPaolo Bonzini     VirtQueueElement *elem;
13476e790746SPaolo Bonzini     size_t s;
1348771b6ed3SJason Wang     struct iovec *iov, *iov2;
13496e790746SPaolo Bonzini     unsigned int iov_cnt;
13506e790746SPaolo Bonzini 
135151b19ebeSPaolo Bonzini     for (;;) {
135251b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
135351b19ebeSPaolo Bonzini         if (!elem) {
135451b19ebeSPaolo Bonzini             break;
135551b19ebeSPaolo Bonzini         }
135651b19ebeSPaolo Bonzini         if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
135751b19ebeSPaolo Bonzini             iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
1358ba7eadb5SGreg Kurz             virtio_error(vdev, "virtio-net ctrl missing headers");
1359ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
1360ba7eadb5SGreg Kurz             g_free(elem);
1361ba7eadb5SGreg Kurz             break;
13626e790746SPaolo Bonzini         }
13636e790746SPaolo Bonzini 
136451b19ebeSPaolo Bonzini         iov_cnt = elem->out_num;
136551b19ebeSPaolo Bonzini         iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
13666e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
13676e790746SPaolo Bonzini         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
13686e790746SPaolo Bonzini         if (s != sizeof(ctrl)) {
13696e790746SPaolo Bonzini             status = VIRTIO_NET_ERR;
13706e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
13716e790746SPaolo Bonzini             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
13726e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
13736e790746SPaolo Bonzini             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
13746e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
13756e790746SPaolo Bonzini             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
1376f57fcf70SJason Wang         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1377f57fcf70SJason Wang             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
13786e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
13796e790746SPaolo Bonzini             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
1380644c9858SDmitry Fleytman         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1381644c9858SDmitry Fleytman             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
13826e790746SPaolo Bonzini         }
13836e790746SPaolo Bonzini 
138451b19ebeSPaolo Bonzini         s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
13856e790746SPaolo Bonzini         assert(s == sizeof(status));
13866e790746SPaolo Bonzini 
138751b19ebeSPaolo Bonzini         virtqueue_push(vq, elem, sizeof(status));
13886e790746SPaolo Bonzini         virtio_notify(vdev, vq);
1389771b6ed3SJason Wang         g_free(iov2);
139051b19ebeSPaolo Bonzini         g_free(elem);
13916e790746SPaolo Bonzini     }
13926e790746SPaolo Bonzini }
13936e790746SPaolo Bonzini 
13946e790746SPaolo Bonzini /* RX */
13956e790746SPaolo Bonzini 
13966e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
13976e790746SPaolo Bonzini {
139817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
13996e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
14006e790746SPaolo Bonzini 
14016e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
14026e790746SPaolo Bonzini }
14036e790746SPaolo Bonzini 
1404b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc)
14056e790746SPaolo Bonzini {
14066e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
140717a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
14086e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
14096e790746SPaolo Bonzini 
141017a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
1411b8c4b67eSPhilippe Mathieu-Daudé         return false;
14126e790746SPaolo Bonzini     }
14136e790746SPaolo Bonzini 
14146e790746SPaolo Bonzini     if (nc->queue_index >= n->curr_queues) {
1415b8c4b67eSPhilippe Mathieu-Daudé         return false;
14166e790746SPaolo Bonzini     }
14176e790746SPaolo Bonzini 
14186e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
141917a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1420b8c4b67eSPhilippe Mathieu-Daudé         return false;
14216e790746SPaolo Bonzini     }
14226e790746SPaolo Bonzini 
1423b8c4b67eSPhilippe Mathieu-Daudé     return true;
14246e790746SPaolo Bonzini }
14256e790746SPaolo Bonzini 
14266e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
14276e790746SPaolo Bonzini {
14286e790746SPaolo Bonzini     VirtIONet *n = q->n;
14296e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
14306e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
14316e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
14326e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
14336e790746SPaolo Bonzini 
14346e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
14356e790746SPaolo Bonzini          * available after the above check but before notification was
14366e790746SPaolo Bonzini          * enabled, check for available buffers again.
14376e790746SPaolo Bonzini          */
14386e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
14396e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
14406e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
14416e790746SPaolo Bonzini             return 0;
14426e790746SPaolo Bonzini         }
14436e790746SPaolo Bonzini     }
14446e790746SPaolo Bonzini 
14456e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
14466e790746SPaolo Bonzini     return 1;
14476e790746SPaolo Bonzini }
14486e790746SPaolo Bonzini 
14491399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1450032a74a1SCédric Le Goater {
14511399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
14521399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
14531399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
14541399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1455032a74a1SCédric Le Goater }
1456032a74a1SCédric Le Goater 
14576e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
14586e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
14596e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
14606e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
14616e790746SPaolo Bonzini  * dhclient yet.
14626e790746SPaolo Bonzini  *
14636e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
14646e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
14656e790746SPaolo Bonzini  * kernels.
14666e790746SPaolo Bonzini  *
14676e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
14686e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
14696e790746SPaolo Bonzini  * cache.
14706e790746SPaolo Bonzini  */
14716e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
14726e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
14736e790746SPaolo Bonzini {
14746e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
14756e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
14766e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
14776e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
14786e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1479f5746335SBin Meng         net_checksum_calculate(buf, size, CSUM_UDP);
14806e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
14816e790746SPaolo Bonzini     }
14826e790746SPaolo Bonzini }
14836e790746SPaolo Bonzini 
14846e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
14856e790746SPaolo Bonzini                            const void *buf, size_t size)
14866e790746SPaolo Bonzini {
14876e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
14886e790746SPaolo Bonzini         /* FIXME this cast is evil */
14896e790746SPaolo Bonzini         void *wbuf = (void *)buf;
14906e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
14916e790746SPaolo Bonzini                                     size - n->host_hdr_len);
14921bfa316cSGreg Kurz 
14931bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
14941399c60dSRusty Russell             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
14951bfa316cSGreg Kurz         }
14966e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
14976e790746SPaolo Bonzini     } else {
14986e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
14996e790746SPaolo Bonzini             .flags = 0,
15006e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
15016e790746SPaolo Bonzini         };
15026e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
15036e790746SPaolo Bonzini     }
15046e790746SPaolo Bonzini }
15056e790746SPaolo Bonzini 
15066e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
15076e790746SPaolo Bonzini {
15086e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
15096e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
15106e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
15116e790746SPaolo Bonzini     int i;
15126e790746SPaolo Bonzini 
15136e790746SPaolo Bonzini     if (n->promisc)
15146e790746SPaolo Bonzini         return 1;
15156e790746SPaolo Bonzini 
15166e790746SPaolo Bonzini     ptr += n->host_hdr_len;
15176e790746SPaolo Bonzini 
15186e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
15197542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
15206e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
15216e790746SPaolo Bonzini             return 0;
15226e790746SPaolo Bonzini     }
15236e790746SPaolo Bonzini 
15246e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
15256e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
15266e790746SPaolo Bonzini             return !n->nobcast;
15276e790746SPaolo Bonzini         } else if (n->nomulti) {
15286e790746SPaolo Bonzini             return 0;
15296e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
15306e790746SPaolo Bonzini             return 1;
15316e790746SPaolo Bonzini         }
15326e790746SPaolo Bonzini 
15336e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
15346e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
15356e790746SPaolo Bonzini                 return 1;
15366e790746SPaolo Bonzini             }
15376e790746SPaolo Bonzini         }
15386e790746SPaolo Bonzini     } else { // unicast
15396e790746SPaolo Bonzini         if (n->nouni) {
15406e790746SPaolo Bonzini             return 0;
15416e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
15426e790746SPaolo Bonzini             return 1;
15436e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
15446e790746SPaolo Bonzini             return 1;
15456e790746SPaolo Bonzini         }
15466e790746SPaolo Bonzini 
15476e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
15486e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
15496e790746SPaolo Bonzini                 return 1;
15506e790746SPaolo Bonzini             }
15516e790746SPaolo Bonzini         }
15526e790746SPaolo Bonzini     }
15536e790746SPaolo Bonzini 
15546e790746SPaolo Bonzini     return 0;
15556e790746SPaolo Bonzini }
15566e790746SPaolo Bonzini 
15574474e37aSYuri Benditovich static uint8_t virtio_net_get_hash_type(bool isip4,
15584474e37aSYuri Benditovich                                         bool isip6,
15594474e37aSYuri Benditovich                                         bool isudp,
15604474e37aSYuri Benditovich                                         bool istcp,
15614474e37aSYuri Benditovich                                         uint32_t types)
15624474e37aSYuri Benditovich {
15634474e37aSYuri Benditovich     if (isip4) {
15644474e37aSYuri Benditovich         if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) {
15654474e37aSYuri Benditovich             return NetPktRssIpV4Tcp;
15664474e37aSYuri Benditovich         }
15674474e37aSYuri Benditovich         if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) {
15684474e37aSYuri Benditovich             return NetPktRssIpV4Udp;
15694474e37aSYuri Benditovich         }
15704474e37aSYuri Benditovich         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
15714474e37aSYuri Benditovich             return NetPktRssIpV4;
15724474e37aSYuri Benditovich         }
15734474e37aSYuri Benditovich     } else if (isip6) {
15744474e37aSYuri Benditovich         uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
15754474e37aSYuri Benditovich                         VIRTIO_NET_RSS_HASH_TYPE_TCPv6;
15764474e37aSYuri Benditovich 
15774474e37aSYuri Benditovich         if (istcp && (types & mask)) {
15784474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ?
15794474e37aSYuri Benditovich                 NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp;
15804474e37aSYuri Benditovich         }
15814474e37aSYuri Benditovich         mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6;
15824474e37aSYuri Benditovich         if (isudp && (types & mask)) {
15834474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ?
15844474e37aSYuri Benditovich                 NetPktRssIpV6UdpEx : NetPktRssIpV6Udp;
15854474e37aSYuri Benditovich         }
15864474e37aSYuri Benditovich         mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6;
15874474e37aSYuri Benditovich         if (types & mask) {
15884474e37aSYuri Benditovich             return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ?
15894474e37aSYuri Benditovich                 NetPktRssIpV6Ex : NetPktRssIpV6;
15904474e37aSYuri Benditovich         }
15914474e37aSYuri Benditovich     }
15924474e37aSYuri Benditovich     return 0xff;
15934474e37aSYuri Benditovich }
15944474e37aSYuri Benditovich 
1595e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1596e22f0603SYuri Benditovich                                    uint32_t hash)
1597e22f0603SYuri Benditovich {
1598e22f0603SYuri Benditovich     struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1599e22f0603SYuri Benditovich     hdr->hash_value = hash;
1600e22f0603SYuri Benditovich     hdr->hash_report = report;
1601e22f0603SYuri Benditovich }
1602e22f0603SYuri Benditovich 
16034474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
160497cd965cSPaolo Bonzini                                   size_t size)
16056e790746SPaolo Bonzini {
16066e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1607e22f0603SYuri Benditovich     unsigned int index = nc->queue_index, new_index = index;
16084474e37aSYuri Benditovich     struct NetRxPkt *pkt = n->rx_pkt;
16094474e37aSYuri Benditovich     uint8_t net_hash_type;
16104474e37aSYuri Benditovich     uint32_t hash;
16114474e37aSYuri Benditovich     bool isip4, isip6, isudp, istcp;
1612e22f0603SYuri Benditovich     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1613e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv4,
1614e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv4,
1615e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6,
1616e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6,
1617e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1618e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1619e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv4,
1620e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6,
1621e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1622e22f0603SYuri Benditovich     };
16234474e37aSYuri Benditovich 
16244474e37aSYuri Benditovich     net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len,
16254474e37aSYuri Benditovich                              size - n->host_hdr_len);
16264474e37aSYuri Benditovich     net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
16274474e37aSYuri Benditovich     if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) {
16284474e37aSYuri Benditovich         istcp = isudp = false;
16294474e37aSYuri Benditovich     }
16304474e37aSYuri Benditovich     if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) {
16314474e37aSYuri Benditovich         istcp = isudp = false;
16324474e37aSYuri Benditovich     }
16334474e37aSYuri Benditovich     net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp,
16344474e37aSYuri Benditovich                                              n->rss_data.hash_types);
16354474e37aSYuri Benditovich     if (net_hash_type > NetPktRssIpV6UdpEx) {
1636e22f0603SYuri Benditovich         if (n->rss_data.populate_hash) {
1637e22f0603SYuri Benditovich             virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1638e22f0603SYuri Benditovich         }
1639e22f0603SYuri Benditovich         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
16404474e37aSYuri Benditovich     }
16414474e37aSYuri Benditovich 
16424474e37aSYuri Benditovich     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1643e22f0603SYuri Benditovich 
1644e22f0603SYuri Benditovich     if (n->rss_data.populate_hash) {
1645e22f0603SYuri Benditovich         virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1646e22f0603SYuri Benditovich     }
1647e22f0603SYuri Benditovich 
1648e22f0603SYuri Benditovich     if (n->rss_data.redirect) {
16494474e37aSYuri Benditovich         new_index = hash & (n->rss_data.indirections_len - 1);
16504474e37aSYuri Benditovich         new_index = n->rss_data.indirections_table[new_index];
16514474e37aSYuri Benditovich     }
1652e22f0603SYuri Benditovich 
1653e22f0603SYuri Benditovich     return (index == new_index) ? -1 : new_index;
16544474e37aSYuri Benditovich }
16554474e37aSYuri Benditovich 
16564474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
16574474e37aSYuri Benditovich                                       size_t size, bool no_rss)
16584474e37aSYuri Benditovich {
16594474e37aSYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
16606e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
166117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
16626e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
16636e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
16646e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
16656e790746SPaolo Bonzini     size_t offset, i, guest_offset;
16666e790746SPaolo Bonzini 
16676e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
16686e790746SPaolo Bonzini         return -1;
16696e790746SPaolo Bonzini     }
16706e790746SPaolo Bonzini 
16714474e37aSYuri Benditovich     if (!no_rss && n->rss_data.enabled) {
16724474e37aSYuri Benditovich         int index = virtio_net_process_rss(nc, buf, size);
16734474e37aSYuri Benditovich         if (index >= 0) {
16744474e37aSYuri Benditovich             NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
16754474e37aSYuri Benditovich             return virtio_net_receive_rcu(nc2, buf, size, true);
16764474e37aSYuri Benditovich         }
16774474e37aSYuri Benditovich     }
16784474e37aSYuri Benditovich 
16796e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
16806e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
16816e790746SPaolo Bonzini         return 0;
16826e790746SPaolo Bonzini     }
16836e790746SPaolo Bonzini 
16846e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
16856e790746SPaolo Bonzini         return size;
16866e790746SPaolo Bonzini 
16876e790746SPaolo Bonzini     offset = i = 0;
16886e790746SPaolo Bonzini 
16896e790746SPaolo Bonzini     while (offset < size) {
169051b19ebeSPaolo Bonzini         VirtQueueElement *elem;
16916e790746SPaolo Bonzini         int len, total;
169251b19ebeSPaolo Bonzini         const struct iovec *sg;
16936e790746SPaolo Bonzini 
16946e790746SPaolo Bonzini         total = 0;
16956e790746SPaolo Bonzini 
169651b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
169751b19ebeSPaolo Bonzini         if (!elem) {
1698ba10b9c0SGreg Kurz             if (i) {
1699ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
17006e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1701019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1702019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
17036e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1704019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1705019a3edbSGerd Hoffmann                              vdev->guest_features);
1706ba10b9c0SGreg Kurz             }
1707ba10b9c0SGreg Kurz             return -1;
17086e790746SPaolo Bonzini         }
17096e790746SPaolo Bonzini 
171051b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1711ba10b9c0SGreg Kurz             virtio_error(vdev,
1712ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1713ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1714ba10b9c0SGreg Kurz             g_free(elem);
1715ba10b9c0SGreg Kurz             return -1;
17166e790746SPaolo Bonzini         }
17176e790746SPaolo Bonzini 
171851b19ebeSPaolo Bonzini         sg = elem->in_sg;
17196e790746SPaolo Bonzini         if (i == 0) {
17206e790746SPaolo Bonzini             assert(offset == 0);
17216e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
17226e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
172351b19ebeSPaolo Bonzini                                     sg, elem->in_num,
17246e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
17256e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
17266e790746SPaolo Bonzini             }
17276e790746SPaolo Bonzini 
172851b19ebeSPaolo Bonzini             receive_header(n, sg, elem->in_num, buf, size);
1729e22f0603SYuri Benditovich             if (n->rss_data.populate_hash) {
1730e22f0603SYuri Benditovich                 offset = sizeof(mhdr);
1731e22f0603SYuri Benditovich                 iov_from_buf(sg, elem->in_num, offset,
1732e22f0603SYuri Benditovich                              buf + offset, n->host_hdr_len - sizeof(mhdr));
1733e22f0603SYuri Benditovich             }
17346e790746SPaolo Bonzini             offset = n->host_hdr_len;
17356e790746SPaolo Bonzini             total += n->guest_hdr_len;
17366e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
17376e790746SPaolo Bonzini         } else {
17386e790746SPaolo Bonzini             guest_offset = 0;
17396e790746SPaolo Bonzini         }
17406e790746SPaolo Bonzini 
17416e790746SPaolo Bonzini         /* copy in packet.  ugh */
174251b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
17436e790746SPaolo Bonzini                            buf + offset, size - offset);
17446e790746SPaolo Bonzini         total += len;
17456e790746SPaolo Bonzini         offset += len;
17466e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
17476e790746SPaolo Bonzini          * must have consumed the complete packet.
17486e790746SPaolo Bonzini          * Otherwise, drop it. */
17496e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
175027e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
175151b19ebeSPaolo Bonzini             g_free(elem);
17526e790746SPaolo Bonzini             return size;
17536e790746SPaolo Bonzini         }
17546e790746SPaolo Bonzini 
17556e790746SPaolo Bonzini         /* signal other side */
175651b19ebeSPaolo Bonzini         virtqueue_fill(q->rx_vq, elem, total, i++);
175751b19ebeSPaolo Bonzini         g_free(elem);
17586e790746SPaolo Bonzini     }
17596e790746SPaolo Bonzini 
17606e790746SPaolo Bonzini     if (mhdr_cnt) {
17611399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
17626e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
17636e790746SPaolo Bonzini                      0,
17646e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
17656e790746SPaolo Bonzini     }
17666e790746SPaolo Bonzini 
17676e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
176817a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
17696e790746SPaolo Bonzini 
17706e790746SPaolo Bonzini     return size;
17716e790746SPaolo Bonzini }
17726e790746SPaolo Bonzini 
17732974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
177497cd965cSPaolo Bonzini                                   size_t size)
177597cd965cSPaolo Bonzini {
1776068ddfa9SDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
177797cd965cSPaolo Bonzini 
17784474e37aSYuri Benditovich     return virtio_net_receive_rcu(nc, buf, size, false);
177997cd965cSPaolo Bonzini }
178097cd965cSPaolo Bonzini 
17812974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
17822974e916SYuri Benditovich                                          const uint8_t *buf,
17832974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
17842974e916SYuri Benditovich {
17852974e916SYuri Benditovich     uint16_t ip_hdrlen;
17862974e916SYuri Benditovich     struct ip_header *ip;
17872974e916SYuri Benditovich 
17882974e916SYuri Benditovich     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
17892974e916SYuri Benditovich                               + sizeof(struct eth_header));
17902974e916SYuri Benditovich     unit->ip = (void *)ip;
17912974e916SYuri Benditovich     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
17922974e916SYuri Benditovich     unit->ip_plen = &ip->ip_len;
17932974e916SYuri Benditovich     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
17942974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
17952974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
17962974e916SYuri Benditovich }
17972974e916SYuri Benditovich 
17982974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
17992974e916SYuri Benditovich                                          const uint8_t *buf,
18002974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
18012974e916SYuri Benditovich {
18022974e916SYuri Benditovich     struct ip6_header *ip6;
18032974e916SYuri Benditovich 
18042974e916SYuri Benditovich     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
18052974e916SYuri Benditovich                                  + sizeof(struct eth_header));
18062974e916SYuri Benditovich     unit->ip = ip6;
18072974e916SYuri Benditovich     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
180878ee6bd0SPhilippe Mathieu-Daudé     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
18092974e916SYuri Benditovich                                         + sizeof(struct ip6_header));
18102974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
18112974e916SYuri Benditovich 
18122974e916SYuri Benditovich     /* There is a difference between payload lenght in ipv4 and v6,
18132974e916SYuri Benditovich        ip header is excluded in ipv6 */
18142974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
18152974e916SYuri Benditovich }
18162974e916SYuri Benditovich 
18172974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
18182974e916SYuri Benditovich                                        VirtioNetRscSeg *seg)
18192974e916SYuri Benditovich {
18202974e916SYuri Benditovich     int ret;
1821dd3d85e8SYuri Benditovich     struct virtio_net_hdr_v1 *h;
18222974e916SYuri Benditovich 
1823dd3d85e8SYuri Benditovich     h = (struct virtio_net_hdr_v1 *)seg->buf;
18242974e916SYuri Benditovich     h->flags = 0;
18252974e916SYuri Benditovich     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
18262974e916SYuri Benditovich 
18272974e916SYuri Benditovich     if (seg->is_coalesced) {
1828dd3d85e8SYuri Benditovich         h->rsc.segments = seg->packets;
1829dd3d85e8SYuri Benditovich         h->rsc.dup_acks = seg->dup_ack;
18302974e916SYuri Benditovich         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
18312974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
18322974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
18332974e916SYuri Benditovich         } else {
18342974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
18352974e916SYuri Benditovich         }
18362974e916SYuri Benditovich     }
18372974e916SYuri Benditovich 
18382974e916SYuri Benditovich     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
18392974e916SYuri Benditovich     QTAILQ_REMOVE(&chain->buffers, seg, next);
18402974e916SYuri Benditovich     g_free(seg->buf);
18412974e916SYuri Benditovich     g_free(seg);
18422974e916SYuri Benditovich 
18432974e916SYuri Benditovich     return ret;
18442974e916SYuri Benditovich }
18452974e916SYuri Benditovich 
18462974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq)
18472974e916SYuri Benditovich {
18482974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn;
18492974e916SYuri Benditovich     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
18502974e916SYuri Benditovich 
18512974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
18522974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
18532974e916SYuri Benditovich             chain->stat.purge_failed++;
18542974e916SYuri Benditovich             continue;
18552974e916SYuri Benditovich         }
18562974e916SYuri Benditovich     }
18572974e916SYuri Benditovich 
18582974e916SYuri Benditovich     chain->stat.timer++;
18592974e916SYuri Benditovich     if (!QTAILQ_EMPTY(&chain->buffers)) {
18602974e916SYuri Benditovich         timer_mod(chain->drain_timer,
18612974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
18622974e916SYuri Benditovich     }
18632974e916SYuri Benditovich }
18642974e916SYuri Benditovich 
18652974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n)
18662974e916SYuri Benditovich {
18672974e916SYuri Benditovich     VirtioNetRscChain *chain, *rn_chain;
18682974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn_seg;
18692974e916SYuri Benditovich 
18702974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
18712974e916SYuri Benditovich         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
18722974e916SYuri Benditovich             QTAILQ_REMOVE(&chain->buffers, seg, next);
18732974e916SYuri Benditovich             g_free(seg->buf);
18742974e916SYuri Benditovich             g_free(seg);
18752974e916SYuri Benditovich         }
18762974e916SYuri Benditovich 
18772974e916SYuri Benditovich         timer_free(chain->drain_timer);
18782974e916SYuri Benditovich         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
18792974e916SYuri Benditovich         g_free(chain);
18802974e916SYuri Benditovich     }
18812974e916SYuri Benditovich }
18822974e916SYuri Benditovich 
18832974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
18842974e916SYuri Benditovich                                      NetClientState *nc,
18852974e916SYuri Benditovich                                      const uint8_t *buf, size_t size)
18862974e916SYuri Benditovich {
18872974e916SYuri Benditovich     uint16_t hdr_len;
18882974e916SYuri Benditovich     VirtioNetRscSeg *seg;
18892974e916SYuri Benditovich 
18902974e916SYuri Benditovich     hdr_len = chain->n->guest_hdr_len;
18912974e916SYuri Benditovich     seg = g_malloc(sizeof(VirtioNetRscSeg));
18922974e916SYuri Benditovich     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
18932974e916SYuri Benditovich         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
18942974e916SYuri Benditovich     memcpy(seg->buf, buf, size);
18952974e916SYuri Benditovich     seg->size = size;
18962974e916SYuri Benditovich     seg->packets = 1;
18972974e916SYuri Benditovich     seg->dup_ack = 0;
18982974e916SYuri Benditovich     seg->is_coalesced = 0;
18992974e916SYuri Benditovich     seg->nc = nc;
19002974e916SYuri Benditovich 
19012974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
19022974e916SYuri Benditovich     chain->stat.cache++;
19032974e916SYuri Benditovich 
19042974e916SYuri Benditovich     switch (chain->proto) {
19052974e916SYuri Benditovich     case ETH_P_IP:
19062974e916SYuri Benditovich         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
19072974e916SYuri Benditovich         break;
19082974e916SYuri Benditovich     case ETH_P_IPV6:
19092974e916SYuri Benditovich         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
19102974e916SYuri Benditovich         break;
19112974e916SYuri Benditovich     default:
19122974e916SYuri Benditovich         g_assert_not_reached();
19132974e916SYuri Benditovich     }
19142974e916SYuri Benditovich }
19152974e916SYuri Benditovich 
19162974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
19172974e916SYuri Benditovich                                          VirtioNetRscSeg *seg,
19182974e916SYuri Benditovich                                          const uint8_t *buf,
19192974e916SYuri Benditovich                                          struct tcp_header *n_tcp,
19202974e916SYuri Benditovich                                          struct tcp_header *o_tcp)
19212974e916SYuri Benditovich {
19222974e916SYuri Benditovich     uint32_t nack, oack;
19232974e916SYuri Benditovich     uint16_t nwin, owin;
19242974e916SYuri Benditovich 
19252974e916SYuri Benditovich     nack = htonl(n_tcp->th_ack);
19262974e916SYuri Benditovich     nwin = htons(n_tcp->th_win);
19272974e916SYuri Benditovich     oack = htonl(o_tcp->th_ack);
19282974e916SYuri Benditovich     owin = htons(o_tcp->th_win);
19292974e916SYuri Benditovich 
19302974e916SYuri Benditovich     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
19312974e916SYuri Benditovich         chain->stat.ack_out_of_win++;
19322974e916SYuri Benditovich         return RSC_FINAL;
19332974e916SYuri Benditovich     } else if (nack == oack) {
19342974e916SYuri Benditovich         /* duplicated ack or window probe */
19352974e916SYuri Benditovich         if (nwin == owin) {
19362974e916SYuri Benditovich             /* duplicated ack, add dup ack count due to whql test up to 1 */
19372974e916SYuri Benditovich             chain->stat.dup_ack++;
19382974e916SYuri Benditovich             return RSC_FINAL;
19392974e916SYuri Benditovich         } else {
19402974e916SYuri Benditovich             /* Coalesce window update */
19412974e916SYuri Benditovich             o_tcp->th_win = n_tcp->th_win;
19422974e916SYuri Benditovich             chain->stat.win_update++;
19432974e916SYuri Benditovich             return RSC_COALESCE;
19442974e916SYuri Benditovich         }
19452974e916SYuri Benditovich     } else {
19462974e916SYuri Benditovich         /* pure ack, go to 'C', finalize*/
19472974e916SYuri Benditovich         chain->stat.pure_ack++;
19482974e916SYuri Benditovich         return RSC_FINAL;
19492974e916SYuri Benditovich     }
19502974e916SYuri Benditovich }
19512974e916SYuri Benditovich 
19522974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
19532974e916SYuri Benditovich                                             VirtioNetRscSeg *seg,
19542974e916SYuri Benditovich                                             const uint8_t *buf,
19552974e916SYuri Benditovich                                             VirtioNetRscUnit *n_unit)
19562974e916SYuri Benditovich {
19572974e916SYuri Benditovich     void *data;
19582974e916SYuri Benditovich     uint16_t o_ip_len;
19592974e916SYuri Benditovich     uint32_t nseq, oseq;
19602974e916SYuri Benditovich     VirtioNetRscUnit *o_unit;
19612974e916SYuri Benditovich 
19622974e916SYuri Benditovich     o_unit = &seg->unit;
19632974e916SYuri Benditovich     o_ip_len = htons(*o_unit->ip_plen);
19642974e916SYuri Benditovich     nseq = htonl(n_unit->tcp->th_seq);
19652974e916SYuri Benditovich     oseq = htonl(o_unit->tcp->th_seq);
19662974e916SYuri Benditovich 
19672974e916SYuri Benditovich     /* out of order or retransmitted. */
19682974e916SYuri Benditovich     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
19692974e916SYuri Benditovich         chain->stat.data_out_of_win++;
19702974e916SYuri Benditovich         return RSC_FINAL;
19712974e916SYuri Benditovich     }
19722974e916SYuri Benditovich 
19732974e916SYuri Benditovich     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
19742974e916SYuri Benditovich     if (nseq == oseq) {
19752974e916SYuri Benditovich         if ((o_unit->payload == 0) && n_unit->payload) {
19762974e916SYuri Benditovich             /* From no payload to payload, normal case, not a dup ack or etc */
19772974e916SYuri Benditovich             chain->stat.data_after_pure_ack++;
19782974e916SYuri Benditovich             goto coalesce;
19792974e916SYuri Benditovich         } else {
19802974e916SYuri Benditovich             return virtio_net_rsc_handle_ack(chain, seg, buf,
19812974e916SYuri Benditovich                                              n_unit->tcp, o_unit->tcp);
19822974e916SYuri Benditovich         }
19832974e916SYuri Benditovich     } else if ((nseq - oseq) != o_unit->payload) {
19842974e916SYuri Benditovich         /* Not a consistent packet, out of order */
19852974e916SYuri Benditovich         chain->stat.data_out_of_order++;
19862974e916SYuri Benditovich         return RSC_FINAL;
19872974e916SYuri Benditovich     } else {
19882974e916SYuri Benditovich coalesce:
19892974e916SYuri Benditovich         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
19902974e916SYuri Benditovich             chain->stat.over_size++;
19912974e916SYuri Benditovich             return RSC_FINAL;
19922974e916SYuri Benditovich         }
19932974e916SYuri Benditovich 
19942974e916SYuri Benditovich         /* Here comes the right data, the payload length in v4/v6 is different,
19952974e916SYuri Benditovich            so use the field value to update and record the new data len */
19962974e916SYuri Benditovich         o_unit->payload += n_unit->payload; /* update new data len */
19972974e916SYuri Benditovich 
19982974e916SYuri Benditovich         /* update field in ip header */
19992974e916SYuri Benditovich         *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
20002974e916SYuri Benditovich 
20012974e916SYuri Benditovich         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
20022974e916SYuri Benditovich            for windows guest, while this may change the behavior for linux
20032974e916SYuri Benditovich            guest (only if it uses RSC feature). */
20042974e916SYuri Benditovich         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
20052974e916SYuri Benditovich 
20062974e916SYuri Benditovich         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
20072974e916SYuri Benditovich         o_unit->tcp->th_win = n_unit->tcp->th_win;
20082974e916SYuri Benditovich 
20092974e916SYuri Benditovich         memmove(seg->buf + seg->size, data, n_unit->payload);
20102974e916SYuri Benditovich         seg->size += n_unit->payload;
20112974e916SYuri Benditovich         seg->packets++;
20122974e916SYuri Benditovich         chain->stat.coalesced++;
20132974e916SYuri Benditovich         return RSC_COALESCE;
20142974e916SYuri Benditovich     }
20152974e916SYuri Benditovich }
20162974e916SYuri Benditovich 
20172974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
20182974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
20192974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
20202974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
20212974e916SYuri Benditovich {
20222974e916SYuri Benditovich     struct ip_header *ip1, *ip2;
20232974e916SYuri Benditovich 
20242974e916SYuri Benditovich     ip1 = (struct ip_header *)(unit->ip);
20252974e916SYuri Benditovich     ip2 = (struct ip_header *)(seg->unit.ip);
20262974e916SYuri Benditovich     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
20272974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
20282974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
20292974e916SYuri Benditovich         chain->stat.no_match++;
20302974e916SYuri Benditovich         return RSC_NO_MATCH;
20312974e916SYuri Benditovich     }
20322974e916SYuri Benditovich 
20332974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
20342974e916SYuri Benditovich }
20352974e916SYuri Benditovich 
20362974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
20372974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
20382974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
20392974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
20402974e916SYuri Benditovich {
20412974e916SYuri Benditovich     struct ip6_header *ip1, *ip2;
20422974e916SYuri Benditovich 
20432974e916SYuri Benditovich     ip1 = (struct ip6_header *)(unit->ip);
20442974e916SYuri Benditovich     ip2 = (struct ip6_header *)(seg->unit.ip);
20452974e916SYuri Benditovich     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
20462974e916SYuri Benditovich         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
20472974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
20482974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
20492974e916SYuri Benditovich             chain->stat.no_match++;
20502974e916SYuri Benditovich             return RSC_NO_MATCH;
20512974e916SYuri Benditovich     }
20522974e916SYuri Benditovich 
20532974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
20542974e916SYuri Benditovich }
20552974e916SYuri Benditovich 
20562974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain
20572974e916SYuri Benditovich  * to prevent out of order */
20582974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
20592974e916SYuri Benditovich                                          struct tcp_header *tcp)
20602974e916SYuri Benditovich {
20612974e916SYuri Benditovich     uint16_t tcp_hdr;
20622974e916SYuri Benditovich     uint16_t tcp_flag;
20632974e916SYuri Benditovich 
20642974e916SYuri Benditovich     tcp_flag = htons(tcp->th_offset_flags);
20652974e916SYuri Benditovich     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
20662974e916SYuri Benditovich     tcp_flag &= VIRTIO_NET_TCP_FLAG;
20672974e916SYuri Benditovich     if (tcp_flag & TH_SYN) {
20682974e916SYuri Benditovich         chain->stat.tcp_syn++;
20692974e916SYuri Benditovich         return RSC_BYPASS;
20702974e916SYuri Benditovich     }
20712974e916SYuri Benditovich 
20722974e916SYuri Benditovich     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
20732974e916SYuri Benditovich         chain->stat.tcp_ctrl_drain++;
20742974e916SYuri Benditovich         return RSC_FINAL;
20752974e916SYuri Benditovich     }
20762974e916SYuri Benditovich 
20772974e916SYuri Benditovich     if (tcp_hdr > sizeof(struct tcp_header)) {
20782974e916SYuri Benditovich         chain->stat.tcp_all_opt++;
20792974e916SYuri Benditovich         return RSC_FINAL;
20802974e916SYuri Benditovich     }
20812974e916SYuri Benditovich 
20822974e916SYuri Benditovich     return RSC_CANDIDATE;
20832974e916SYuri Benditovich }
20842974e916SYuri Benditovich 
20852974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
20862974e916SYuri Benditovich                                          NetClientState *nc,
20872974e916SYuri Benditovich                                          const uint8_t *buf, size_t size,
20882974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20892974e916SYuri Benditovich {
20902974e916SYuri Benditovich     int ret;
20912974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
20922974e916SYuri Benditovich 
20932974e916SYuri Benditovich     if (QTAILQ_EMPTY(&chain->buffers)) {
20942974e916SYuri Benditovich         chain->stat.empty_cache++;
20952974e916SYuri Benditovich         virtio_net_rsc_cache_buf(chain, nc, buf, size);
20962974e916SYuri Benditovich         timer_mod(chain->drain_timer,
20972974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
20982974e916SYuri Benditovich         return size;
20992974e916SYuri Benditovich     }
21002974e916SYuri Benditovich 
21012974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
21022974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
21032974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
21042974e916SYuri Benditovich         } else {
21052974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
21062974e916SYuri Benditovich         }
21072974e916SYuri Benditovich 
21082974e916SYuri Benditovich         if (ret == RSC_FINAL) {
21092974e916SYuri Benditovich             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
21102974e916SYuri Benditovich                 /* Send failed */
21112974e916SYuri Benditovich                 chain->stat.final_failed++;
21122974e916SYuri Benditovich                 return 0;
21132974e916SYuri Benditovich             }
21142974e916SYuri Benditovich 
21152974e916SYuri Benditovich             /* Send current packet */
21162974e916SYuri Benditovich             return virtio_net_do_receive(nc, buf, size);
21172974e916SYuri Benditovich         } else if (ret == RSC_NO_MATCH) {
21182974e916SYuri Benditovich             continue;
21192974e916SYuri Benditovich         } else {
21202974e916SYuri Benditovich             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
21212974e916SYuri Benditovich             seg->is_coalesced = 1;
21222974e916SYuri Benditovich             return size;
21232974e916SYuri Benditovich         }
21242974e916SYuri Benditovich     }
21252974e916SYuri Benditovich 
21262974e916SYuri Benditovich     chain->stat.no_match_cache++;
21272974e916SYuri Benditovich     virtio_net_rsc_cache_buf(chain, nc, buf, size);
21282974e916SYuri Benditovich     return size;
21292974e916SYuri Benditovich }
21302974e916SYuri Benditovich 
21312974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */
21322974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
21332974e916SYuri Benditovich                                         NetClientState *nc,
21342974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
21352974e916SYuri Benditovich                                         uint16_t ip_start, uint16_t ip_size,
21362974e916SYuri Benditovich                                         uint16_t tcp_port)
21372974e916SYuri Benditovich {
21382974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
21392974e916SYuri Benditovich     uint32_t ppair1, ppair2;
21402974e916SYuri Benditovich 
21412974e916SYuri Benditovich     ppair1 = *(uint32_t *)(buf + tcp_port);
21422974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
21432974e916SYuri Benditovich         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
21442974e916SYuri Benditovich         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
21452974e916SYuri Benditovich             || (ppair1 != ppair2)) {
21462974e916SYuri Benditovich             continue;
21472974e916SYuri Benditovich         }
21482974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
21492974e916SYuri Benditovich             chain->stat.drain_failed++;
21502974e916SYuri Benditovich         }
21512974e916SYuri Benditovich 
21522974e916SYuri Benditovich         break;
21532974e916SYuri Benditovich     }
21542974e916SYuri Benditovich 
21552974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
21562974e916SYuri Benditovich }
21572974e916SYuri Benditovich 
21582974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
21592974e916SYuri Benditovich                                             struct ip_header *ip,
21602974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
21612974e916SYuri Benditovich {
21622974e916SYuri Benditovich     uint16_t ip_len;
21632974e916SYuri Benditovich 
21642974e916SYuri Benditovich     /* Not an ipv4 packet */
21652974e916SYuri Benditovich     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
21662974e916SYuri Benditovich         chain->stat.ip_option++;
21672974e916SYuri Benditovich         return RSC_BYPASS;
21682974e916SYuri Benditovich     }
21692974e916SYuri Benditovich 
21702974e916SYuri Benditovich     /* Don't handle packets with ip option */
21712974e916SYuri Benditovich     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
21722974e916SYuri Benditovich         chain->stat.ip_option++;
21732974e916SYuri Benditovich         return RSC_BYPASS;
21742974e916SYuri Benditovich     }
21752974e916SYuri Benditovich 
21762974e916SYuri Benditovich     if (ip->ip_p != IPPROTO_TCP) {
21772974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
21782974e916SYuri Benditovich         return RSC_BYPASS;
21792974e916SYuri Benditovich     }
21802974e916SYuri Benditovich 
21812974e916SYuri Benditovich     /* Don't handle packets with ip fragment */
21822974e916SYuri Benditovich     if (!(htons(ip->ip_off) & IP_DF)) {
21832974e916SYuri Benditovich         chain->stat.ip_frag++;
21842974e916SYuri Benditovich         return RSC_BYPASS;
21852974e916SYuri Benditovich     }
21862974e916SYuri Benditovich 
21872974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
21882974e916SYuri Benditovich     if (IPTOS_ECN(ip->ip_tos)) {
21892974e916SYuri Benditovich         chain->stat.ip_ecn++;
21902974e916SYuri Benditovich         return RSC_BYPASS;
21912974e916SYuri Benditovich     }
21922974e916SYuri Benditovich 
21932974e916SYuri Benditovich     ip_len = htons(ip->ip_len);
21942974e916SYuri Benditovich     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
21952974e916SYuri Benditovich         || ip_len > (size - chain->n->guest_hdr_len -
21962974e916SYuri Benditovich                      sizeof(struct eth_header))) {
21972974e916SYuri Benditovich         chain->stat.ip_hacked++;
21982974e916SYuri Benditovich         return RSC_BYPASS;
21992974e916SYuri Benditovich     }
22002974e916SYuri Benditovich 
22012974e916SYuri Benditovich     return RSC_CANDIDATE;
22022974e916SYuri Benditovich }
22032974e916SYuri Benditovich 
22042974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
22052974e916SYuri Benditovich                                       NetClientState *nc,
22062974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
22072974e916SYuri Benditovich {
22082974e916SYuri Benditovich     int32_t ret;
22092974e916SYuri Benditovich     uint16_t hdr_len;
22102974e916SYuri Benditovich     VirtioNetRscUnit unit;
22112974e916SYuri Benditovich 
22122974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
22132974e916SYuri Benditovich 
22142974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
22152974e916SYuri Benditovich         + sizeof(struct tcp_header))) {
22162974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
22172974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22182974e916SYuri Benditovich     }
22192974e916SYuri Benditovich 
22202974e916SYuri Benditovich     virtio_net_rsc_extract_unit4(chain, buf, &unit);
22212974e916SYuri Benditovich     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
22222974e916SYuri Benditovich         != RSC_CANDIDATE) {
22232974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22242974e916SYuri Benditovich     }
22252974e916SYuri Benditovich 
22262974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
22272974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
22282974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22292974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
22302974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
22312974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 12),
22322974e916SYuri Benditovich                 VIRTIO_NET_IP4_ADDR_SIZE,
22332974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
22342974e916SYuri Benditovich     }
22352974e916SYuri Benditovich 
22362974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
22372974e916SYuri Benditovich }
22382974e916SYuri Benditovich 
22392974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
22402974e916SYuri Benditovich                                             struct ip6_header *ip6,
22412974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
22422974e916SYuri Benditovich {
22432974e916SYuri Benditovich     uint16_t ip_len;
22442974e916SYuri Benditovich 
22452974e916SYuri Benditovich     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
22462974e916SYuri Benditovich         != IP_HEADER_VERSION_6) {
22472974e916SYuri Benditovich         return RSC_BYPASS;
22482974e916SYuri Benditovich     }
22492974e916SYuri Benditovich 
22502974e916SYuri Benditovich     /* Both option and protocol is checked in this */
22512974e916SYuri Benditovich     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
22522974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
22532974e916SYuri Benditovich         return RSC_BYPASS;
22542974e916SYuri Benditovich     }
22552974e916SYuri Benditovich 
22562974e916SYuri Benditovich     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
22572974e916SYuri Benditovich     if (ip_len < sizeof(struct tcp_header) ||
22582974e916SYuri Benditovich         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
22592974e916SYuri Benditovich                   - sizeof(struct ip6_header))) {
22602974e916SYuri Benditovich         chain->stat.ip_hacked++;
22612974e916SYuri Benditovich         return RSC_BYPASS;
22622974e916SYuri Benditovich     }
22632974e916SYuri Benditovich 
22642974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
22652974e916SYuri Benditovich     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
22662974e916SYuri Benditovich         chain->stat.ip_ecn++;
22672974e916SYuri Benditovich         return RSC_BYPASS;
22682974e916SYuri Benditovich     }
22692974e916SYuri Benditovich 
22702974e916SYuri Benditovich     return RSC_CANDIDATE;
22712974e916SYuri Benditovich }
22722974e916SYuri Benditovich 
22732974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
22742974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
22752974e916SYuri Benditovich {
22762974e916SYuri Benditovich     int32_t ret;
22772974e916SYuri Benditovich     uint16_t hdr_len;
22782974e916SYuri Benditovich     VirtioNetRscChain *chain;
22792974e916SYuri Benditovich     VirtioNetRscUnit unit;
22802974e916SYuri Benditovich 
22812974e916SYuri Benditovich     chain = (VirtioNetRscChain *)opq;
22822974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
22832974e916SYuri Benditovich 
22842974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
22852974e916SYuri Benditovich         + sizeof(tcp_header))) {
22862974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22872974e916SYuri Benditovich     }
22882974e916SYuri Benditovich 
22892974e916SYuri Benditovich     virtio_net_rsc_extract_unit6(chain, buf, &unit);
22902974e916SYuri Benditovich     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
22912974e916SYuri Benditovich                                                  unit.ip, buf, size)) {
22922974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22932974e916SYuri Benditovich     }
22942974e916SYuri Benditovich 
22952974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
22962974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
22972974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
22982974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
22992974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
23002974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 8),
23012974e916SYuri Benditovich                 VIRTIO_NET_IP6_ADDR_SIZE,
23022974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header)
23032974e916SYuri Benditovich                 + sizeof(struct ip6_header));
23042974e916SYuri Benditovich     }
23052974e916SYuri Benditovich 
23062974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
23072974e916SYuri Benditovich }
23082974e916SYuri Benditovich 
23092974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
23102974e916SYuri Benditovich                                                       NetClientState *nc,
23112974e916SYuri Benditovich                                                       uint16_t proto)
23122974e916SYuri Benditovich {
23132974e916SYuri Benditovich     VirtioNetRscChain *chain;
23142974e916SYuri Benditovich 
23152974e916SYuri Benditovich     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
23162974e916SYuri Benditovich         return NULL;
23172974e916SYuri Benditovich     }
23182974e916SYuri Benditovich 
23192974e916SYuri Benditovich     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
23202974e916SYuri Benditovich         if (chain->proto == proto) {
23212974e916SYuri Benditovich             return chain;
23222974e916SYuri Benditovich         }
23232974e916SYuri Benditovich     }
23242974e916SYuri Benditovich 
23252974e916SYuri Benditovich     chain = g_malloc(sizeof(*chain));
23262974e916SYuri Benditovich     chain->n = n;
23272974e916SYuri Benditovich     chain->proto = proto;
23282974e916SYuri Benditovich     if (proto == (uint16_t)ETH_P_IP) {
23292974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
23302974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
23312974e916SYuri Benditovich     } else {
23322974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
23332974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
23342974e916SYuri Benditovich     }
23352974e916SYuri Benditovich     chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
23362974e916SYuri Benditovich                                       virtio_net_rsc_purge, chain);
23372974e916SYuri Benditovich     memset(&chain->stat, 0, sizeof(chain->stat));
23382974e916SYuri Benditovich 
23392974e916SYuri Benditovich     QTAILQ_INIT(&chain->buffers);
23402974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
23412974e916SYuri Benditovich 
23422974e916SYuri Benditovich     return chain;
23432974e916SYuri Benditovich }
23442974e916SYuri Benditovich 
23452974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc,
23462974e916SYuri Benditovich                                       const uint8_t *buf,
23472974e916SYuri Benditovich                                       size_t size)
23482974e916SYuri Benditovich {
23492974e916SYuri Benditovich     uint16_t proto;
23502974e916SYuri Benditovich     VirtioNetRscChain *chain;
23512974e916SYuri Benditovich     struct eth_header *eth;
23522974e916SYuri Benditovich     VirtIONet *n;
23532974e916SYuri Benditovich 
23542974e916SYuri Benditovich     n = qemu_get_nic_opaque(nc);
23552974e916SYuri Benditovich     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
23562974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23572974e916SYuri Benditovich     }
23582974e916SYuri Benditovich 
23592974e916SYuri Benditovich     eth = (struct eth_header *)(buf + n->guest_hdr_len);
23602974e916SYuri Benditovich     proto = htons(eth->h_proto);
23612974e916SYuri Benditovich 
23622974e916SYuri Benditovich     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
23632974e916SYuri Benditovich     if (chain) {
23642974e916SYuri Benditovich         chain->stat.received++;
23652974e916SYuri Benditovich         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
23662974e916SYuri Benditovich             return virtio_net_rsc_receive4(chain, nc, buf, size);
23672974e916SYuri Benditovich         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
23682974e916SYuri Benditovich             return virtio_net_rsc_receive6(chain, nc, buf, size);
23692974e916SYuri Benditovich         }
23702974e916SYuri Benditovich     }
23712974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
23722974e916SYuri Benditovich }
23732974e916SYuri Benditovich 
23742974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
23752974e916SYuri Benditovich                                   size_t size)
23762974e916SYuri Benditovich {
23772974e916SYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
23782974e916SYuri Benditovich     if ((n->rsc4_enabled || n->rsc6_enabled)) {
23792974e916SYuri Benditovich         return virtio_net_rsc_receive(nc, buf, size);
23802974e916SYuri Benditovich     } else {
23812974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
23822974e916SYuri Benditovich     }
23832974e916SYuri Benditovich }
23842974e916SYuri Benditovich 
23856e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
23866e790746SPaolo Bonzini 
23876e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
23886e790746SPaolo Bonzini {
23896e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
23906e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
239117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
23926e790746SPaolo Bonzini 
239351b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
239417a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
23956e790746SPaolo Bonzini 
239651b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
239751b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
23986e790746SPaolo Bonzini 
23996e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
24006e790746SPaolo Bonzini     virtio_net_flush_tx(q);
24016e790746SPaolo Bonzini }
24026e790746SPaolo Bonzini 
24036e790746SPaolo Bonzini /* TX */
24046e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
24056e790746SPaolo Bonzini {
24066e790746SPaolo Bonzini     VirtIONet *n = q->n;
240717a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
240851b19ebeSPaolo Bonzini     VirtQueueElement *elem;
24096e790746SPaolo Bonzini     int32_t num_packets = 0;
24106e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
241117a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
24126e790746SPaolo Bonzini         return num_packets;
24136e790746SPaolo Bonzini     }
24146e790746SPaolo Bonzini 
241551b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
24166e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
24176e790746SPaolo Bonzini         return num_packets;
24186e790746SPaolo Bonzini     }
24196e790746SPaolo Bonzini 
242051b19ebeSPaolo Bonzini     for (;;) {
2421bd89dd98SJason Wang         ssize_t ret;
242251b19ebeSPaolo Bonzini         unsigned int out_num;
242351b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2424feb93f36SJason Wang         struct virtio_net_hdr_mrg_rxbuf mhdr;
24256e790746SPaolo Bonzini 
242651b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
242751b19ebeSPaolo Bonzini         if (!elem) {
242851b19ebeSPaolo Bonzini             break;
242951b19ebeSPaolo Bonzini         }
243051b19ebeSPaolo Bonzini 
243151b19ebeSPaolo Bonzini         out_num = elem->out_num;
243251b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
24336e790746SPaolo Bonzini         if (out_num < 1) {
2434fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
2435fa5e56c2SGreg Kurz             virtqueue_detach_element(q->tx_vq, elem, 0);
2436fa5e56c2SGreg Kurz             g_free(elem);
2437fa5e56c2SGreg Kurz             return -EINVAL;
24386e790746SPaolo Bonzini         }
24396e790746SPaolo Bonzini 
2440032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
2441feb93f36SJason Wang             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2442feb93f36SJason Wang                 n->guest_hdr_len) {
2443fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
2444fa5e56c2SGreg Kurz                 virtqueue_detach_element(q->tx_vq, elem, 0);
2445fa5e56c2SGreg Kurz                 g_free(elem);
2446fa5e56c2SGreg Kurz                 return -EINVAL;
2447032a74a1SCédric Le Goater             }
24481bfa316cSGreg Kurz             if (n->needs_vnet_hdr_swap) {
2449feb93f36SJason Wang                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2450feb93f36SJason Wang                 sg2[0].iov_base = &mhdr;
2451feb93f36SJason Wang                 sg2[0].iov_len = n->guest_hdr_len;
2452feb93f36SJason Wang                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2453feb93f36SJason Wang                                    out_sg, out_num,
2454feb93f36SJason Wang                                    n->guest_hdr_len, -1);
2455feb93f36SJason Wang                 if (out_num == VIRTQUEUE_MAX_SIZE) {
2456feb93f36SJason Wang                     goto drop;
2457032a74a1SCédric Le Goater                 }
2458feb93f36SJason Wang                 out_num += 1;
2459feb93f36SJason Wang                 out_sg = sg2;
2460feb93f36SJason Wang             }
2461feb93f36SJason Wang         }
24626e790746SPaolo Bonzini         /*
24636e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
24646e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
24656e790746SPaolo Bonzini          * that host is interested in.
24666e790746SPaolo Bonzini          */
24676e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
24686e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
24696e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
24706e790746SPaolo Bonzini                                        out_sg, out_num,
24716e790746SPaolo Bonzini                                        0, n->host_hdr_len);
24726e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
24736e790746SPaolo Bonzini                              out_sg, out_num,
24746e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
24756e790746SPaolo Bonzini             out_num = sg_num;
24766e790746SPaolo Bonzini             out_sg = sg;
24776e790746SPaolo Bonzini         }
24786e790746SPaolo Bonzini 
24796e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
24806e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
24816e790746SPaolo Bonzini         if (ret == 0) {
24826e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
24836e790746SPaolo Bonzini             q->async_tx.elem = elem;
24846e790746SPaolo Bonzini             return -EBUSY;
24856e790746SPaolo Bonzini         }
24866e790746SPaolo Bonzini 
2487feb93f36SJason Wang drop:
248851b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
248917a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
249051b19ebeSPaolo Bonzini         g_free(elem);
24916e790746SPaolo Bonzini 
24926e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
24936e790746SPaolo Bonzini             break;
24946e790746SPaolo Bonzini         }
24956e790746SPaolo Bonzini     }
24966e790746SPaolo Bonzini     return num_packets;
24976e790746SPaolo Bonzini }
24986e790746SPaolo Bonzini 
24996e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
25006e790746SPaolo Bonzini {
250117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
25026e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
25036e790746SPaolo Bonzini 
2504283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2505283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2506283e2c2aSYuri Benditovich         return;
2507283e2c2aSYuri Benditovich     }
2508283e2c2aSYuri Benditovich 
25096e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
251017a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
25116e790746SPaolo Bonzini         q->tx_waiting = 1;
25126e790746SPaolo Bonzini         return;
25136e790746SPaolo Bonzini     }
25146e790746SPaolo Bonzini 
25156e790746SPaolo Bonzini     if (q->tx_waiting) {
25166e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 1);
2517bc72ad67SAlex Bligh         timer_del(q->tx_timer);
25186e790746SPaolo Bonzini         q->tx_waiting = 0;
2519fa5e56c2SGreg Kurz         if (virtio_net_flush_tx(q) == -EINVAL) {
2520fa5e56c2SGreg Kurz             return;
2521fa5e56c2SGreg Kurz         }
25226e790746SPaolo Bonzini     } else {
2523bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
2524bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
25256e790746SPaolo Bonzini         q->tx_waiting = 1;
25266e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
25276e790746SPaolo Bonzini     }
25286e790746SPaolo Bonzini }
25296e790746SPaolo Bonzini 
25306e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
25316e790746SPaolo Bonzini {
253217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
25336e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
25346e790746SPaolo Bonzini 
2535283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2536283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2537283e2c2aSYuri Benditovich         return;
2538283e2c2aSYuri Benditovich     }
2539283e2c2aSYuri Benditovich 
25406e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
25416e790746SPaolo Bonzini         return;
25426e790746SPaolo Bonzini     }
25436e790746SPaolo Bonzini     q->tx_waiting = 1;
25446e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
254517a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
25466e790746SPaolo Bonzini         return;
25476e790746SPaolo Bonzini     }
25486e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
25496e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
25506e790746SPaolo Bonzini }
25516e790746SPaolo Bonzini 
25526e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
25536e790746SPaolo Bonzini {
25546e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
25556e790746SPaolo Bonzini     VirtIONet *n = q->n;
255617a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2557e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2558e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2559e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2560e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2561e8bcf842SMichael S. Tsirkin         return;
2562e8bcf842SMichael S. Tsirkin     }
25636e790746SPaolo Bonzini 
25646e790746SPaolo Bonzini     q->tx_waiting = 0;
25656e790746SPaolo Bonzini 
25666e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
256717a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
25686e790746SPaolo Bonzini         return;
256917a0ca55SKONRAD Frederic     }
25706e790746SPaolo Bonzini 
25716e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
25726e790746SPaolo Bonzini     virtio_net_flush_tx(q);
25736e790746SPaolo Bonzini }
25746e790746SPaolo Bonzini 
25756e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
25766e790746SPaolo Bonzini {
25776e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
25786e790746SPaolo Bonzini     VirtIONet *n = q->n;
257917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
25806e790746SPaolo Bonzini     int32_t ret;
25816e790746SPaolo Bonzini 
2582e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2583e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2584e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2585e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2586e8bcf842SMichael S. Tsirkin         return;
2587e8bcf842SMichael S. Tsirkin     }
25886e790746SPaolo Bonzini 
25896e790746SPaolo Bonzini     q->tx_waiting = 0;
25906e790746SPaolo Bonzini 
25916e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
259217a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
25936e790746SPaolo Bonzini         return;
259417a0ca55SKONRAD Frederic     }
25956e790746SPaolo Bonzini 
25966e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
2597fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
2598fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
2599fa5e56c2SGreg Kurz                  * broken */
26006e790746SPaolo Bonzini     }
26016e790746SPaolo Bonzini 
26026e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
26036e790746SPaolo Bonzini      * more coming and immediately reschedule */
26046e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
26056e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
26066e790746SPaolo Bonzini         q->tx_waiting = 1;
26076e790746SPaolo Bonzini         return;
26086e790746SPaolo Bonzini     }
26096e790746SPaolo Bonzini 
26106e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
26116e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
26126e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
26136e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2614fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
2615fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
2616fa5e56c2SGreg Kurz         return;
2617fa5e56c2SGreg Kurz     } else if (ret > 0) {
26186e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
26196e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
26206e790746SPaolo Bonzini         q->tx_waiting = 1;
26216e790746SPaolo Bonzini     }
26226e790746SPaolo Bonzini }
26236e790746SPaolo Bonzini 
2624f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
2625f9d6dbf0SWen Congyang {
2626f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2627f9d6dbf0SWen Congyang 
26281c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
26291c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
26309b02e161SWei Wang 
2631f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2632f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
26339b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
26349b02e161SWei Wang                              virtio_net_handle_tx_timer);
2635f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2636f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
2637f9d6dbf0SWen Congyang                                               &n->vqs[index]);
2638f9d6dbf0SWen Congyang     } else {
2639f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
26409b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
26419b02e161SWei Wang                              virtio_net_handle_tx_bh);
2642f9d6dbf0SWen Congyang         n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
2643f9d6dbf0SWen Congyang     }
2644f9d6dbf0SWen Congyang 
2645f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
2646f9d6dbf0SWen Congyang     n->vqs[index].n = n;
2647f9d6dbf0SWen Congyang }
2648f9d6dbf0SWen Congyang 
2649f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
2650f9d6dbf0SWen Congyang {
2651f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2652f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
2653f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
2654f9d6dbf0SWen Congyang 
2655f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
2656f9d6dbf0SWen Congyang 
2657f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
2658f9d6dbf0SWen Congyang     if (q->tx_timer) {
2659f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
2660f989c30cSYunjian Wang         q->tx_timer = NULL;
2661f9d6dbf0SWen Congyang     } else {
2662f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
2663f989c30cSYunjian Wang         q->tx_bh = NULL;
2664f9d6dbf0SWen Congyang     }
2665f989c30cSYunjian Wang     q->tx_waiting = 0;
2666f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
2667f9d6dbf0SWen Congyang }
2668f9d6dbf0SWen Congyang 
2669f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
2670f9d6dbf0SWen Congyang {
2671f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2672f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
2673f9d6dbf0SWen Congyang     int new_num_queues = new_max_queues * 2 + 1;
2674f9d6dbf0SWen Congyang     int i;
2675f9d6dbf0SWen Congyang 
2676f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
2677f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
2678f9d6dbf0SWen Congyang 
2679f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
2680f9d6dbf0SWen Congyang         return;
2681f9d6dbf0SWen Congyang     }
2682f9d6dbf0SWen Congyang 
2683f9d6dbf0SWen Congyang     /*
2684f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
2685f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
268620f86a75SYuval Shaia      * and then we only enter one of the following two loops.
2687f9d6dbf0SWen Congyang      */
2688f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
2689f9d6dbf0SWen Congyang 
2690f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2691f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
2692f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
2693f9d6dbf0SWen Congyang     }
2694f9d6dbf0SWen Congyang 
2695f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2696f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
2697f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
2698f9d6dbf0SWen Congyang     }
2699f9d6dbf0SWen Congyang 
2700f9d6dbf0SWen Congyang     /* add ctrl_vq last */
2701f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2702f9d6dbf0SWen Congyang }
2703f9d6dbf0SWen Congyang 
2704ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
27056e790746SPaolo Bonzini {
2706f9d6dbf0SWen Congyang     int max = multiqueue ? n->max_queues : 1;
2707f9d6dbf0SWen Congyang 
27086e790746SPaolo Bonzini     n->multiqueue = multiqueue;
2709f9d6dbf0SWen Congyang     virtio_net_change_num_queues(n, max);
27106e790746SPaolo Bonzini 
27116e790746SPaolo Bonzini     virtio_net_set_queues(n);
27126e790746SPaolo Bonzini }
27136e790746SPaolo Bonzini 
2714982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
2715037dab2fSGreg Kurz {
2716982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
2717982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2718037dab2fSGreg Kurz     int i, link_down;
2719037dab2fSGreg Kurz 
27209d8c6a25SDr. David Alan Gilbert     trace_virtio_net_post_load_device();
2721982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
272295129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
2723e22f0603SYuri Benditovich                                                        VIRTIO_F_VERSION_1),
2724e22f0603SYuri Benditovich                                virtio_vdev_has_feature(vdev,
2725e22f0603SYuri Benditovich                                                        VIRTIO_NET_F_HASH_REPORT));
27266e790746SPaolo Bonzini 
27276e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
2728982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
27296e790746SPaolo Bonzini         n->mac_table.in_use = 0;
27306e790746SPaolo Bonzini     }
27316e790746SPaolo Bonzini 
2732982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
27336c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
27346c666823SMichael S. Tsirkin     }
27356c666823SMichael S. Tsirkin 
27367788c3f2SMikhail Sennikovsky     /*
27377788c3f2SMikhail Sennikovsky      * curr_guest_offloads will be later overwritten by the
27387788c3f2SMikhail Sennikovsky      * virtio_set_features_nocheck call done from the virtio_load.
27397788c3f2SMikhail Sennikovsky      * Here we make sure it is preserved and restored accordingly
27407788c3f2SMikhail Sennikovsky      * in the virtio_net_post_load_virtio callback.
27417788c3f2SMikhail Sennikovsky      */
27427788c3f2SMikhail Sennikovsky     n->saved_guest_offloads = n->curr_guest_offloads;
27436c666823SMichael S. Tsirkin 
27446e790746SPaolo Bonzini     virtio_net_set_queues(n);
27456e790746SPaolo Bonzini 
27466e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
27476e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
27486e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
27496e790746SPaolo Bonzini             break;
27506e790746SPaolo Bonzini         }
27516e790746SPaolo Bonzini     }
27526e790746SPaolo Bonzini     n->mac_table.first_multi = i;
27536e790746SPaolo Bonzini 
27546e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
27556e790746SPaolo Bonzini      * to link status bit in n->status */
27566e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
27576e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
27586e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
27596e790746SPaolo Bonzini     }
27606e790746SPaolo Bonzini 
27616c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
27626c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
27639d8c6a25SDr. David Alan Gilbert         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
27649d8c6a25SDr. David Alan Gilbert                                   QEMU_CLOCK_VIRTUAL,
27659d8c6a25SDr. David Alan Gilbert                                   virtio_net_announce_timer, n);
27669d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
27679d8c6a25SDr. David Alan Gilbert             timer_mod(n->announce_timer.tm,
27689d8c6a25SDr. David Alan Gilbert                       qemu_clock_get_ms(n->announce_timer.type));
27699d8c6a25SDr. David Alan Gilbert         } else {
2770944458b6SDr. David Alan Gilbert             qemu_announce_timer_del(&n->announce_timer, false);
27719d8c6a25SDr. David Alan Gilbert         }
27726c666823SMichael S. Tsirkin     }
27736c666823SMichael S. Tsirkin 
2774e41b7114SYuri Benditovich     if (n->rss_data.enabled) {
2775e41b7114SYuri Benditovich         trace_virtio_net_rss_enable(n->rss_data.hash_types,
2776e41b7114SYuri Benditovich                                     n->rss_data.indirections_len,
2777e41b7114SYuri Benditovich                                     sizeof(n->rss_data.key));
2778e41b7114SYuri Benditovich     } else {
2779e41b7114SYuri Benditovich         trace_virtio_net_rss_disable();
2780e41b7114SYuri Benditovich     }
27816e790746SPaolo Bonzini     return 0;
27826e790746SPaolo Bonzini }
27836e790746SPaolo Bonzini 
27847788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev)
27857788c3f2SMikhail Sennikovsky {
27867788c3f2SMikhail Sennikovsky     VirtIONet *n = VIRTIO_NET(vdev);
27877788c3f2SMikhail Sennikovsky     /*
27887788c3f2SMikhail Sennikovsky      * The actual needed state is now in saved_guest_offloads,
27897788c3f2SMikhail Sennikovsky      * see virtio_net_post_load_device for detail.
27907788c3f2SMikhail Sennikovsky      * Restore it back and apply the desired offloads.
27917788c3f2SMikhail Sennikovsky      */
27927788c3f2SMikhail Sennikovsky     n->curr_guest_offloads = n->saved_guest_offloads;
27937788c3f2SMikhail Sennikovsky     if (peer_has_vnet_hdr(n)) {
27947788c3f2SMikhail Sennikovsky         virtio_net_apply_guest_offloads(n);
27957788c3f2SMikhail Sennikovsky     }
27967788c3f2SMikhail Sennikovsky 
27977788c3f2SMikhail Sennikovsky     return 0;
27987788c3f2SMikhail Sennikovsky }
27997788c3f2SMikhail Sennikovsky 
2800982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
2801982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
2802982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
2803982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
2804982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
2805982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2806982b78c5SDr. David Alan Gilbert    },
2807982b78c5SDr. David Alan Gilbert };
2808982b78c5SDr. David Alan Gilbert 
2809982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id)
2810982b78c5SDr. David Alan Gilbert {
2811982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->max_queues > 1;
2812982b78c5SDr. David Alan Gilbert }
2813982b78c5SDr. David Alan Gilbert 
2814982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
2815982b78c5SDr. David Alan Gilbert {
2816982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
2817982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2818982b78c5SDr. David Alan Gilbert }
2819982b78c5SDr. David Alan Gilbert 
2820982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
2821982b78c5SDr. David Alan Gilbert {
2822982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
2823982b78c5SDr. David Alan Gilbert }
2824982b78c5SDr. David Alan Gilbert 
2825982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
2826982b78c5SDr. David Alan Gilbert {
2827982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
2828982b78c5SDr. David Alan Gilbert }
2829982b78c5SDr. David Alan Gilbert 
2830982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
2831982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
2832982b78c5SDr. David Alan Gilbert  */
2833982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
2834982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
2835982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
2836982b78c5SDr. David Alan Gilbert     uint16_t        curr_queues_1;
2837982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
2838982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
2839982b78c5SDr. David Alan Gilbert };
2840982b78c5SDr. David Alan Gilbert 
2841982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
2842982b78c5SDr. David Alan Gilbert  * the 1st entry in the queues and only if there's more than one
2843982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
2844982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
2845982b78c5SDr. David Alan Gilbert  */
2846982b78c5SDr. David Alan Gilbert 
284744b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque)
2848982b78c5SDr. David Alan Gilbert {
2849982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2850982b78c5SDr. David Alan Gilbert 
2851982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
2852982b78c5SDr. David Alan Gilbert     tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
2853982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues == 0) {
2854982b78c5SDr. David Alan Gilbert         tmp->curr_queues_1 = 0;
2855982b78c5SDr. David Alan Gilbert     }
285644b1ff31SDr. David Alan Gilbert 
285744b1ff31SDr. David Alan Gilbert     return 0;
2858982b78c5SDr. David Alan Gilbert }
2859982b78c5SDr. David Alan Gilbert 
2860982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
2861982b78c5SDr. David Alan Gilbert {
2862982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2863982b78c5SDr. David Alan Gilbert 
2864982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
2865982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
2866982b78c5SDr. David Alan Gilbert 
2867982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues > tmp->parent->max_queues) {
2868982b78c5SDr. David Alan Gilbert         error_report("virtio-net: curr_queues %x > max_queues %x",
2869982b78c5SDr. David Alan Gilbert             tmp->parent->curr_queues, tmp->parent->max_queues);
2870982b78c5SDr. David Alan Gilbert 
2871982b78c5SDr. David Alan Gilbert         return -EINVAL;
2872982b78c5SDr. David Alan Gilbert     }
2873982b78c5SDr. David Alan Gilbert 
2874982b78c5SDr. David Alan Gilbert     return 0; /* all good */
2875982b78c5SDr. David Alan Gilbert }
2876982b78c5SDr. David Alan Gilbert 
2877982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
2878982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
2879982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
2880982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
2881982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
2882982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
2883982b78c5SDr. David Alan Gilbert                                      curr_queues_1,
2884982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
2885982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
2886982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2887982b78c5SDr. David Alan Gilbert     },
2888982b78c5SDr. David Alan Gilbert };
2889982b78c5SDr. David Alan Gilbert 
2890982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
2891982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
2892982b78c5SDr. David Alan Gilbert  */
2893982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
2894982b78c5SDr. David Alan Gilbert {
2895982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2896982b78c5SDr. David Alan Gilbert 
2897982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
2898982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
2899982b78c5SDr. David Alan Gilbert         return -EINVAL;
2900982b78c5SDr. David Alan Gilbert     }
2901982b78c5SDr. David Alan Gilbert 
2902982b78c5SDr. David Alan Gilbert     return 0;
2903982b78c5SDr. David Alan Gilbert }
2904982b78c5SDr. David Alan Gilbert 
290544b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque)
2906982b78c5SDr. David Alan Gilbert {
2907982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2908982b78c5SDr. David Alan Gilbert 
2909982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
291044b1ff31SDr. David Alan Gilbert 
291144b1ff31SDr. David Alan Gilbert     return 0;
2912982b78c5SDr. David Alan Gilbert }
2913982b78c5SDr. David Alan Gilbert 
2914982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
2915982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
2916982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
2917982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
2918982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
2919982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
2920982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2921982b78c5SDr. David Alan Gilbert     },
2922982b78c5SDr. David Alan Gilbert };
2923982b78c5SDr. David Alan Gilbert 
2924982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
2925982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
2926982b78c5SDr. David Alan Gilbert  */
2927982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
2928982b78c5SDr. David Alan Gilbert {
2929982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2930982b78c5SDr. David Alan Gilbert 
2931982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
2932982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
2933982b78c5SDr. David Alan Gilbert         return -EINVAL;
2934982b78c5SDr. David Alan Gilbert     }
2935982b78c5SDr. David Alan Gilbert 
2936982b78c5SDr. David Alan Gilbert     return 0;
2937982b78c5SDr. David Alan Gilbert }
2938982b78c5SDr. David Alan Gilbert 
293944b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque)
2940982b78c5SDr. David Alan Gilbert {
2941982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
2942982b78c5SDr. David Alan Gilbert 
2943982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
294444b1ff31SDr. David Alan Gilbert 
294544b1ff31SDr. David Alan Gilbert     return 0;
2946982b78c5SDr. David Alan Gilbert }
2947982b78c5SDr. David Alan Gilbert 
2948982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
2949982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
2950982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
2951982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
2952982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
2953982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
2954982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2955982b78c5SDr. David Alan Gilbert     },
2956982b78c5SDr. David Alan Gilbert };
2957982b78c5SDr. David Alan Gilbert 
2958e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque)
2959e41b7114SYuri Benditovich {
2960e41b7114SYuri Benditovich     return VIRTIO_NET(opaque)->rss_data.enabled;
2961e41b7114SYuri Benditovich }
2962e41b7114SYuri Benditovich 
2963e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = {
2964e41b7114SYuri Benditovich     .name      = "virtio-net-device/rss",
2965e41b7114SYuri Benditovich     .version_id = 1,
2966e41b7114SYuri Benditovich     .minimum_version_id = 1,
2967e41b7114SYuri Benditovich     .needed = virtio_net_rss_needed,
2968e41b7114SYuri Benditovich     .fields = (VMStateField[]) {
2969e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
2970e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
2971e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
2972e41b7114SYuri Benditovich         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
2973e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
2974e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
2975e41b7114SYuri Benditovich         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
2976e41b7114SYuri Benditovich                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
2977e41b7114SYuri Benditovich         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
2978e41b7114SYuri Benditovich                                     rss_data.indirections_len, 0,
2979e41b7114SYuri Benditovich                                     vmstate_info_uint16, uint16_t),
2980e41b7114SYuri Benditovich         VMSTATE_END_OF_LIST()
2981e41b7114SYuri Benditovich     },
2982e41b7114SYuri Benditovich };
2983e41b7114SYuri Benditovich 
2984982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
2985982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
2986982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
2987982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
2988982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
2989982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
2990982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
2991982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
2992982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
2993982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
2994982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
2995982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
2996982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
2997982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
2998982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
2999982b78c5SDr. David Alan Gilbert 
3000982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
3001982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
3002982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
3003982b78c5SDr. David Alan Gilbert          */
3004982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3005982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
3006982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
3007982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3008982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
3009982b78c5SDr. David Alan Gilbert 
3010982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
3011982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3012982b78c5SDr. David Alan Gilbert          * but based on the uint.
3013982b78c5SDr. David Alan Gilbert          */
3014982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3015982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3016982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
3017982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3018982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3019982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
3020982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
3021982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
3022982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
3023982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3024982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
3025982b78c5SDr. David Alan Gilbert         VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
3026982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
3027982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
3028982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3029982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
3030982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3031982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
3032982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3033982b78c5SDr. David Alan Gilbert    },
3034e41b7114SYuri Benditovich     .subsections = (const VMStateDescription * []) {
3035e41b7114SYuri Benditovich         &vmstate_virtio_net_rss,
3036e41b7114SYuri Benditovich         NULL
3037e41b7114SYuri Benditovich     }
3038982b78c5SDr. David Alan Gilbert };
3039982b78c5SDr. David Alan Gilbert 
30406e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
3041f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
30426e790746SPaolo Bonzini     .size = sizeof(NICState),
30436e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
30446e790746SPaolo Bonzini     .receive = virtio_net_receive,
30456e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
3046b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
3047b2c929f0SDr. David Alan Gilbert     .announce = virtio_net_announce,
30486e790746SPaolo Bonzini };
30496e790746SPaolo Bonzini 
30506e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
30516e790746SPaolo Bonzini {
305217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
30536e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
30546e790746SPaolo Bonzini     assert(n->vhost_started);
3055ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
30566e790746SPaolo Bonzini }
30576e790746SPaolo Bonzini 
30586e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
30596e790746SPaolo Bonzini                                            bool mask)
30606e790746SPaolo Bonzini {
306117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
30626e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
30636e790746SPaolo Bonzini     assert(n->vhost_started);
3064ed8b4afeSNikolay Nikolaev     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
30656e790746SPaolo Bonzini                              vdev, idx, mask);
30666e790746SPaolo Bonzini }
30676e790746SPaolo Bonzini 
3068019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
30696e790746SPaolo Bonzini {
30700cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3071a93e599dSMaxime Coquelin 
3072ba550851SStefano Garzarella     n->config_size = virtio_feature_get_config_size(feature_sizes,
3073ba550851SStefano Garzarella                                                     host_features);
307417ec5a86SKONRAD Frederic }
30756e790746SPaolo Bonzini 
30768a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
30778a253ec2SKONRAD Frederic                                    const char *type)
30788a253ec2SKONRAD Frederic {
30798a253ec2SKONRAD Frederic     /*
30808a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
30818a253ec2SKONRAD Frederic      */
30828a253ec2SKONRAD Frederic     assert(type != NULL);
30838a253ec2SKONRAD Frederic 
30848a253ec2SKONRAD Frederic     g_free(n->netclient_name);
30858a253ec2SKONRAD Frederic     g_free(n->netclient_type);
30868a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
30878a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
30888a253ec2SKONRAD Frederic }
30898a253ec2SKONRAD Frederic 
30900e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
30919711cd0dSJens Freimann {
30929711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
30939711cd0dSJens Freimann     PCIDevice *pci_dev;
30949711cd0dSJens Freimann     Error *err = NULL;
30959711cd0dSJens Freimann 
30960e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
30979711cd0dSJens Freimann     if (hotplug_ctrl) {
30980e9a65c5SJuan Quintela         pci_dev = PCI_DEVICE(dev);
30999711cd0dSJens Freimann         pci_dev->partially_hotplugged = true;
31000e9a65c5SJuan Quintela         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
31019711cd0dSJens Freimann         if (err) {
31029711cd0dSJens Freimann             error_report_err(err);
31039711cd0dSJens Freimann             return false;
31049711cd0dSJens Freimann         }
31059711cd0dSJens Freimann     } else {
31069711cd0dSJens Freimann         return false;
31079711cd0dSJens Freimann     }
31089711cd0dSJens Freimann     return true;
31099711cd0dSJens Freimann }
31109711cd0dSJens Freimann 
31110e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
31120e9a65c5SJuan Quintela                                     Error **errp)
31139711cd0dSJens Freimann {
31145a0948d3SMarkus Armbruster     Error *err = NULL;
31159711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
31160e9a65c5SJuan Quintela     PCIDevice *pdev = PCI_DEVICE(dev);
311778274682SJuan Quintela     BusState *primary_bus;
31189711cd0dSJens Freimann 
31199711cd0dSJens Freimann     if (!pdev->partially_hotplugged) {
31209711cd0dSJens Freimann         return true;
31219711cd0dSJens Freimann     }
31220e9a65c5SJuan Quintela     primary_bus = dev->parent_bus;
312378274682SJuan Quintela     if (!primary_bus) {
3124150ab54aSJens Freimann         error_setg(errp, "virtio_net: couldn't find primary bus");
31255a0948d3SMarkus Armbruster         return false;
31269711cd0dSJens Freimann     }
31270e9a65c5SJuan Quintela     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3128e2bde83eSJuan Quintela     qatomic_set(&n->failover_primary_hidden, false);
31290e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
31309711cd0dSJens Freimann     if (hotplug_ctrl) {
31310e9a65c5SJuan Quintela         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
31325a0948d3SMarkus Armbruster         if (err) {
31335a0948d3SMarkus Armbruster             goto out;
31345a0948d3SMarkus Armbruster         }
31350e9a65c5SJuan Quintela         hotplug_handler_plug(hotplug_ctrl, dev, &err);
31369711cd0dSJens Freimann     }
3137150ab54aSJens Freimann 
3138150ab54aSJens Freimann out:
31395a0948d3SMarkus Armbruster     error_propagate(errp, err);
31405a0948d3SMarkus Armbruster     return !err;
31419711cd0dSJens Freimann }
31429711cd0dSJens Freimann 
314307a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
31449711cd0dSJens Freimann {
31459711cd0dSJens Freimann     bool should_be_hidden;
31469711cd0dSJens Freimann     Error *err = NULL;
314707a5d816SJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
314807a5d816SJuan Quintela 
314907a5d816SJuan Quintela     if (!dev) {
315007a5d816SJuan Quintela         return;
315107a5d816SJuan Quintela     }
31529711cd0dSJens Freimann 
3153e2bde83eSJuan Quintela     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
31549711cd0dSJens Freimann 
31554dbac1aeSMarkus Armbruster     if (migration_in_setup(s) && !should_be_hidden) {
315607a5d816SJuan Quintela         if (failover_unplug_primary(n, dev)) {
315707a5d816SJuan Quintela             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
315807a5d816SJuan Quintela             qapi_event_send_unplug_primary(dev->id);
3159e2bde83eSJuan Quintela             qatomic_set(&n->failover_primary_hidden, true);
31609711cd0dSJens Freimann         } else {
31619711cd0dSJens Freimann             warn_report("couldn't unplug primary device");
31629711cd0dSJens Freimann         }
31639711cd0dSJens Freimann     } else if (migration_has_failed(s)) {
3164150ab54aSJens Freimann         /* We already unplugged the device let's plug it back */
316507a5d816SJuan Quintela         if (!failover_replug_primary(n, dev, &err)) {
31669711cd0dSJens Freimann             if (err) {
31679711cd0dSJens Freimann                 error_report_err(err);
31689711cd0dSJens Freimann             }
31699711cd0dSJens Freimann         }
31709711cd0dSJens Freimann     }
31719711cd0dSJens Freimann }
31729711cd0dSJens Freimann 
31739711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
31749711cd0dSJens Freimann {
31759711cd0dSJens Freimann     MigrationState *s = data;
31769711cd0dSJens Freimann     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
31779711cd0dSJens Freimann     virtio_net_handle_migration_primary(n, s);
31789711cd0dSJens Freimann }
31799711cd0dSJens Freimann 
3180b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener,
31819711cd0dSJens Freimann                                          QemuOpts *device_opts)
31829711cd0dSJens Freimann {
31839711cd0dSJens Freimann     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
31844f0303aeSJuan Quintela     const char *standby_id;
31859711cd0dSJens Freimann 
31864d0e59acSJens Freimann     if (!device_opts) {
318789631fedSJuan Quintela         return false;
31884d0e59acSJens Freimann     }
31894f0303aeSJuan Quintela     standby_id = qemu_opt_get(device_opts, "failover_pair_id");
319089631fedSJuan Quintela     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
319189631fedSJuan Quintela         return false;
31929711cd0dSJens Freimann     }
31939711cd0dSJens Freimann 
3194e2bde83eSJuan Quintela     /* failover_primary_hidden is set during feature negotiation */
31953abad4a2SJuan Quintela     return qatomic_read(&n->failover_primary_hidden);
31969711cd0dSJens Freimann }
31979711cd0dSJens Freimann 
3198e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
319917ec5a86SKONRAD Frederic {
3200e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3201284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3202284a32f0SAndreas Färber     NetClientState *nc;
32031773d9eeSKONRAD Frederic     int i;
320417ec5a86SKONRAD Frederic 
3205a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
3206127833eeSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3207a93e599dSMaxime Coquelin     }
3208a93e599dSMaxime Coquelin 
32099473939eSJason Baron     if (n->net_conf.duplex_str) {
32109473939eSJason Baron         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
32119473939eSJason Baron             n->net_conf.duplex = DUPLEX_HALF;
32129473939eSJason Baron         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
32139473939eSJason Baron             n->net_conf.duplex = DUPLEX_FULL;
32149473939eSJason Baron         } else {
32159473939eSJason Baron             error_setg(errp, "'duplex' must be 'half' or 'full'");
3216843c4cfcSMarkus Armbruster             return;
32179473939eSJason Baron         }
32189473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
32199473939eSJason Baron     } else {
32209473939eSJason Baron         n->net_conf.duplex = DUPLEX_UNKNOWN;
32219473939eSJason Baron     }
32229473939eSJason Baron 
32239473939eSJason Baron     if (n->net_conf.speed < SPEED_UNKNOWN) {
32249473939eSJason Baron         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3225843c4cfcSMarkus Armbruster         return;
3226843c4cfcSMarkus Armbruster     }
3227843c4cfcSMarkus Armbruster     if (n->net_conf.speed >= 0) {
32289473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
32299473939eSJason Baron     }
32309473939eSJason Baron 
32319711cd0dSJens Freimann     if (n->failover) {
3232b91ad981SJuan Quintela         n->primary_listener.hide_device = failover_hide_primary_device;
3233e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, true);
32349711cd0dSJens Freimann         device_listener_register(&n->primary_listener);
32359711cd0dSJens Freimann         n->migration_state.notify = virtio_net_migration_state_notifier;
32369711cd0dSJens Freimann         add_migration_state_change_notifier(&n->migration_state);
32379711cd0dSJens Freimann         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
32389711cd0dSJens Freimann     }
32399711cd0dSJens Freimann 
3240da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
3241284a32f0SAndreas Färber     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
324217ec5a86SKONRAD Frederic 
32431c0fbfa3SMichael S. Tsirkin     /*
32441c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
32451c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
32461c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
32471c0fbfa3SMichael S. Tsirkin      */
32481c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
32491c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
32505f997fd1SMichal Privoznik         !is_power_of_2(n->net_conf.rx_queue_size)) {
32511c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
32521c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
32531c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
32541c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
32551c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
32561c0fbfa3SMichael S. Tsirkin         return;
32571c0fbfa3SMichael S. Tsirkin     }
32581c0fbfa3SMichael S. Tsirkin 
32599b02e161SWei Wang     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
32609b02e161SWei Wang         n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
32619b02e161SWei Wang         !is_power_of_2(n->net_conf.tx_queue_size)) {
32629b02e161SWei Wang         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
32639b02e161SWei Wang                    "must be a power of 2 between %d and %d",
32649b02e161SWei Wang                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
32659b02e161SWei Wang                    VIRTQUEUE_MAX_SIZE);
32669b02e161SWei Wang         virtio_cleanup(vdev);
32679b02e161SWei Wang         return;
32689b02e161SWei Wang     }
32699b02e161SWei Wang 
3270575a1c0eSJiri Pirko     n->max_queues = MAX(n->nic_conf.peers.queues, 1);
327187b3bd1cSJason Wang     if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
32727e0e736eSJason Wang         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
3273631b22eaSStefan Weil                    "must be a positive integer less than %d.",
327487b3bd1cSJason Wang                    n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
32757e0e736eSJason Wang         virtio_cleanup(vdev);
32767e0e736eSJason Wang         return;
32777e0e736eSJason Wang     }
32786e790746SPaolo Bonzini     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
32796e790746SPaolo Bonzini     n->curr_queues = 1;
32801773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
32816e790746SPaolo Bonzini 
32821773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
32831773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
32840765691eSMarkus Armbruster         warn_report("virtio-net: "
32856e790746SPaolo Bonzini                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
32861773d9eeSKONRAD Frederic                     n->net_conf.tx);
32870765691eSMarkus Armbruster         error_printf("Defaulting to \"bh\"");
32886e790746SPaolo Bonzini     }
32896e790746SPaolo Bonzini 
32902eef278bSMichael S. Tsirkin     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
32912eef278bSMichael S. Tsirkin                                     n->net_conf.tx_queue_size);
32929b02e161SWei Wang 
3293da51a335SJason Wang     for (i = 0; i < n->max_queues; i++) {
3294f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i);
3295da51a335SJason Wang     }
3296da51a335SJason Wang 
329717a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
32981773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
32991773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
33006e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
33019d8c6a25SDr. David Alan Gilbert     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
33029d8c6a25SDr. David Alan Gilbert                               QEMU_CLOCK_VIRTUAL,
3303f57fcf70SJason Wang                               virtio_net_announce_timer, n);
3304b2c929f0SDr. David Alan Gilbert     n->announce_timer.round = 0;
33056e790746SPaolo Bonzini 
33068a253ec2SKONRAD Frederic     if (n->netclient_type) {
33078a253ec2SKONRAD Frederic         /*
33088a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
33098a253ec2SKONRAD Frederic          */
33108a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
33118a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
33128a253ec2SKONRAD Frederic     } else {
33131773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3314284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
33158a253ec2SKONRAD Frederic     }
33168a253ec2SKONRAD Frederic 
3317d4c62930SBin Meng     for (i = 0; i < n->max_queues; i++) {
3318d4c62930SBin Meng         n->nic->ncs[i].do_not_pad = true;
3319d4c62930SBin Meng     }
3320d4c62930SBin Meng 
33216e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
33226e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
33236e790746SPaolo Bonzini         for (i = 0; i < n->max_queues; i++) {
3324d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
33256e790746SPaolo Bonzini         }
33266e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
33276e790746SPaolo Bonzini     } else {
33286e790746SPaolo Bonzini         n->host_hdr_len = 0;
33296e790746SPaolo Bonzini     }
33306e790746SPaolo Bonzini 
33311773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
33326e790746SPaolo Bonzini 
33336e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
33341773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
3335e22f0603SYuri Benditovich     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
33366e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
33376e790746SPaolo Bonzini 
33386e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
33396e790746SPaolo Bonzini 
33406e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
33416e790746SPaolo Bonzini 
3342b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
3343b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
3344b1be4280SAmos Kong 
3345e87936eaSCindy Lu    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3346e87936eaSCindy Lu         struct virtio_net_config netcfg = {};
3347e87936eaSCindy Lu         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3348e87936eaSCindy Lu         vhost_net_set_config(get_vhost_net(nc->peer),
3349e87936eaSCindy Lu             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER);
3350e87936eaSCindy Lu     }
33512974e916SYuri Benditovich     QTAILQ_INIT(&n->rsc_chains);
3352284a32f0SAndreas Färber     n->qdev = dev;
33534474e37aSYuri Benditovich 
33544474e37aSYuri Benditovich     net_rx_pkt_init(&n->rx_pkt, false);
335517ec5a86SKONRAD Frederic }
335617ec5a86SKONRAD Frederic 
3357b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev)
335817ec5a86SKONRAD Frederic {
3359306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3360306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3361f9d6dbf0SWen Congyang     int i, max_queues;
336217ec5a86SKONRAD Frederic 
336317ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
336417ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
336517ec5a86SKONRAD Frederic 
33668a253ec2SKONRAD Frederic     g_free(n->netclient_name);
33678a253ec2SKONRAD Frederic     n->netclient_name = NULL;
33688a253ec2SKONRAD Frederic     g_free(n->netclient_type);
33698a253ec2SKONRAD Frederic     n->netclient_type = NULL;
33708a253ec2SKONRAD Frederic 
337117ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
337217ec5a86SKONRAD Frederic     g_free(n->vlans);
337317ec5a86SKONRAD Frederic 
33749711cd0dSJens Freimann     if (n->failover) {
337565018100SJuan Quintela         device_listener_unregister(&n->primary_listener);
3376*1e157667SLaurent Vivier         remove_migration_state_change_notifier(&n->migration_state);
33779711cd0dSJens Freimann     }
33789711cd0dSJens Freimann 
3379f9d6dbf0SWen Congyang     max_queues = n->multiqueue ? n->max_queues : 1;
3380f9d6dbf0SWen Congyang     for (i = 0; i < max_queues; i++) {
3381f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
338217ec5a86SKONRAD Frederic     }
3383d945d9f1SYuri Benditovich     /* delete also control vq */
3384d945d9f1SYuri Benditovich     virtio_del_queue(vdev, max_queues * 2);
3385944458b6SDr. David Alan Gilbert     qemu_announce_timer_del(&n->announce_timer, false);
338617ec5a86SKONRAD Frederic     g_free(n->vqs);
338717ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
33882974e916SYuri Benditovich     virtio_net_rsc_cleanup(n);
338959079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
33904474e37aSYuri Benditovich     net_rx_pkt_uninit(n->rx_pkt);
33916a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
339217ec5a86SKONRAD Frederic }
339317ec5a86SKONRAD Frederic 
339417ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
339517ec5a86SKONRAD Frederic {
339617ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
339717ec5a86SKONRAD Frederic 
339817ec5a86SKONRAD Frederic     /*
339917ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
340017ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
340117ec5a86SKONRAD Frederic      */
340217ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
3403aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3404aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
340540c2281cSMarkus Armbruster                                   DEVICE(n));
340617ec5a86SKONRAD Frederic }
340717ec5a86SKONRAD Frederic 
340844b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque)
34094d45dcfbSHalil Pasic {
34104d45dcfbSHalil Pasic     VirtIONet *n = opaque;
34114d45dcfbSHalil Pasic 
34124d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
34134d45dcfbSHalil Pasic      * it might keep writing to memory. */
34144d45dcfbSHalil Pasic     assert(!n->vhost_started);
341544b1ff31SDr. David Alan Gilbert 
341644b1ff31SDr. David Alan Gilbert     return 0;
34174d45dcfbSHalil Pasic }
34184d45dcfbSHalil Pasic 
34199711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque)
34209711cd0dSJens Freimann {
34219711cd0dSJens Freimann     DeviceState *dev = opaque;
342221e8709bSJuan Quintela     DeviceState *primary;
34239711cd0dSJens Freimann     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
34249711cd0dSJens Freimann     VirtIONet *n = VIRTIO_NET(vdev);
34259711cd0dSJens Freimann 
3426284f42a5SJens Freimann     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3427284f42a5SJens Freimann         return false;
3428284f42a5SJens Freimann     }
342921e8709bSJuan Quintela     primary = failover_find_primary_device(n);
343021e8709bSJuan Quintela     return primary ? primary->pending_deleted_event : false;
34319711cd0dSJens Freimann }
34329711cd0dSJens Freimann 
34339711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque)
34349711cd0dSJens Freimann {
34359711cd0dSJens Freimann     DeviceState *dev = opaque;
34369711cd0dSJens Freimann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
34379711cd0dSJens Freimann 
34389711cd0dSJens Freimann     return vdc->primary_unplug_pending(dev);
34399711cd0dSJens Freimann }
34409711cd0dSJens Freimann 
34414d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
34424d45dcfbSHalil Pasic     .name = "virtio-net",
34434d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
34444d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
34454d45dcfbSHalil Pasic     .fields = (VMStateField[]) {
34464d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
34474d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
34484d45dcfbSHalil Pasic     },
34494d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
34509711cd0dSJens Freimann     .dev_unplug_pending = dev_unplug_pending,
34514d45dcfbSHalil Pasic };
3452290c2428SDr. David Alan Gilbert 
345317ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
3454127833eeSJason Baron     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3455127833eeSJason Baron                     VIRTIO_NET_F_CSUM, true),
3456127833eeSJason Baron     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
345787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
3458127833eeSJason Baron     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3459127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
346087108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
3461127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
346287108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
3463127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
346487108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
3465127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
346687108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
3467127833eeSJason Baron     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
346887108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3469127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
347087108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
3471127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
347287108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
3473127833eeSJason Baron     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
347487108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
3475127833eeSJason Baron     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
347687108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
3477127833eeSJason Baron     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
347887108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
3479127833eeSJason Baron     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
348087108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
3481127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
348287108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
3483127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
348487108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
3485127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
348687108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
3487127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
348887108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3489127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
349087108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3491127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
349287108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3493127833eeSJason Baron     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
349459079029SYuri Benditovich     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
349559079029SYuri Benditovich                     VIRTIO_NET_F_RSS, false),
3496e22f0603SYuri Benditovich     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3497e22f0603SYuri Benditovich                     VIRTIO_NET_F_HASH_REPORT, false),
34982974e916SYuri Benditovich     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
34992974e916SYuri Benditovich                     VIRTIO_NET_F_RSC_EXT, false),
35002974e916SYuri Benditovich     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
35012974e916SYuri Benditovich                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
350217ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
350317ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
350417ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
350517ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
350617ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
35071c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
35081c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
35099b02e161SWei Wang     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
35109b02e161SWei Wang                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3511a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
351275ebec11SMaxime Coquelin     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
351375ebec11SMaxime Coquelin                      true),
35149473939eSJason Baron     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
35159473939eSJason Baron     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
35169711cd0dSJens Freimann     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
351717ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
351817ec5a86SKONRAD Frederic };
351917ec5a86SKONRAD Frederic 
352017ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
352117ec5a86SKONRAD Frederic {
352217ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
352317ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3524e6f746b3SAndreas Färber 
35254f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
3526290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
3527125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3528e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
3529306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
353017ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
353117ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
353217ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
353317ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
353417ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
353517ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
353617ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
353717ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
353817ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
35392a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
35407788c3f2SMikhail Sennikovsky     vdc->post_load = virtio_net_post_load_virtio;
3541982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
35429711cd0dSJens Freimann     vdc->primary_unplug_pending = primary_unplug_pending;
354317ec5a86SKONRAD Frederic }
354417ec5a86SKONRAD Frederic 
354517ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
354617ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
354717ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
354817ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
354917ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
355017ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
355117ec5a86SKONRAD Frederic };
355217ec5a86SKONRAD Frederic 
355317ec5a86SKONRAD Frederic static void virtio_register_types(void)
355417ec5a86SKONRAD Frederic {
355517ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
355617ec5a86SKONRAD Frederic }
355717ec5a86SKONRAD Frederic 
355817ec5a86SKONRAD Frederic type_init(virtio_register_types)
3559