xref: /openbmc/qemu/hw/net/virtio-net.c (revision 53da8b5a992deab25d4f53233891b1aeed7104a6)
16e790746SPaolo Bonzini /*
26e790746SPaolo Bonzini  * Virtio Network Device
36e790746SPaolo Bonzini  *
46e790746SPaolo Bonzini  * Copyright IBM, Corp. 2007
56e790746SPaolo Bonzini  *
66e790746SPaolo Bonzini  * Authors:
76e790746SPaolo Bonzini  *  Anthony Liguori   <aliguori@us.ibm.com>
86e790746SPaolo Bonzini  *
96e790746SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2.  See
106e790746SPaolo Bonzini  * the COPYING file in the top-level directory.
116e790746SPaolo Bonzini  *
126e790746SPaolo Bonzini  */
136e790746SPaolo Bonzini 
149b8bfe21SPeter Maydell #include "qemu/osdep.h"
159711cd0dSJens Freimann #include "qemu/atomic.h"
166e790746SPaolo Bonzini #include "qemu/iov.h"
1768b0a639SSi-Wei Liu #include "qemu/log.h"
18db725815SMarkus Armbruster #include "qemu/main-loop.h"
190b8fa32fSMarkus Armbruster #include "qemu/module.h"
206e790746SPaolo Bonzini #include "hw/virtio/virtio.h"
216e790746SPaolo Bonzini #include "net/net.h"
226e790746SPaolo Bonzini #include "net/checksum.h"
236e790746SPaolo Bonzini #include "net/tap.h"
246e790746SPaolo Bonzini #include "qemu/error-report.h"
256e790746SPaolo Bonzini #include "qemu/timer.h"
269711cd0dSJens Freimann #include "qemu/option.h"
279711cd0dSJens Freimann #include "qemu/option_int.h"
289711cd0dSJens Freimann #include "qemu/config-file.h"
299711cd0dSJens Freimann #include "qapi/qmp/qdict.h"
306e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h"
316e790746SPaolo Bonzini #include "net/vhost_net.h"
329d8c6a25SDr. David Alan Gilbert #include "net/announce.h"
3317ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h"
34e688df6bSMarkus Armbruster #include "qapi/error.h"
359af23989SMarkus Armbruster #include "qapi/qapi-events-net.h"
36a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
379711cd0dSJens Freimann #include "qapi/qapi-types-migration.h"
389711cd0dSJens Freimann #include "qapi/qapi-events-migration.h"
391399c60dSRusty Russell #include "hw/virtio/virtio-access.h"
40f8d806c9SJuan Quintela #include "migration/misc.h"
419473939eSJason Baron #include "standard-headers/linux/ethtool.h"
422f780b6aSMarkus Armbruster #include "sysemu/sysemu.h"
439d8c6a25SDr. David Alan Gilbert #include "trace.h"
449711cd0dSJens Freimann #include "monitor/qdev.h"
45edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
464474e37aSYuri Benditovich #include "net_rx_pkt.h"
47108a6481SCindy Lu #include "hw/virtio/vhost.h"
481b529d90SLaurent Vivier #include "sysemu/qtest.h"
496e790746SPaolo Bonzini 
506e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
516e790746SPaolo Bonzini 
526e790746SPaolo Bonzini #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
536e790746SPaolo Bonzini 
541c0fbfa3SMichael S. Tsirkin /* previously fixed value */
551c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
569b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
579b02e161SWei Wang 
58441537f1SJason Wang /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */
591c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
609b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
611c0fbfa3SMichael S. Tsirkin 
622974e916SYuri Benditovich #define VIRTIO_NET_IP4_ADDR_SIZE   8        /* ipv4 saddr + daddr */
632974e916SYuri Benditovich 
642974e916SYuri Benditovich #define VIRTIO_NET_TCP_FLAG         0x3F
652974e916SYuri Benditovich #define VIRTIO_NET_TCP_HDR_LENGTH   0xF000
662974e916SYuri Benditovich 
672974e916SYuri Benditovich /* IPv4 max payload, 16 bits in the header */
682974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
692974e916SYuri Benditovich #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
702974e916SYuri Benditovich 
712974e916SYuri Benditovich /* header length value in ip header without option */
722974e916SYuri Benditovich #define VIRTIO_NET_IP4_HEADER_LENGTH 5
732974e916SYuri Benditovich 
742974e916SYuri Benditovich #define VIRTIO_NET_IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
752974e916SYuri Benditovich #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
762974e916SYuri Benditovich 
772974e916SYuri Benditovich /* Purge coalesced packets timer interval, This value affects the performance
782974e916SYuri Benditovich    a lot, and should be tuned carefully, '300000'(300us) is the recommended
792974e916SYuri Benditovich    value to pass the WHQL test, '50000' can gain 2x netperf throughput with
802974e916SYuri Benditovich    tso/gso/gro 'off'. */
812974e916SYuri Benditovich #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
822974e916SYuri Benditovich 
8359079029SYuri Benditovich #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
8459079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
8559079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
8659079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
8759079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
8859079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
8959079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
9059079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
9159079029SYuri Benditovich                                          VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
9259079029SYuri Benditovich 
93ad6461adSPhilippe Mathieu-Daudé static const VirtIOFeature feature_sizes[] = {
94127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MAC,
955d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, mac)},
96127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_STATUS,
975d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, status)},
98127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MQ,
995d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
100127833eeSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_MTU,
1015d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, mtu)},
1029473939eSJason Baron     {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX,
1035d5b33c0SMax Reitz      .end = endof(struct virtio_net_config, duplex)},
104e22f0603SYuri Benditovich     {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT),
10559079029SYuri Benditovich      .end = endof(struct virtio_net_config, supported_hash_types)},
1066e790746SPaolo Bonzini     {}
1076e790746SPaolo Bonzini };
1086e790746SPaolo Bonzini 
109d74c30c8SDaniil Tatianin static const VirtIOConfigSizeParams cfg_size_params = {
110d74c30c8SDaniil Tatianin     .min_size = endof(struct virtio_net_config, mac),
111d74c30c8SDaniil Tatianin     .max_size = sizeof(struct virtio_net_config),
112d74c30c8SDaniil Tatianin     .feature_sizes = feature_sizes
113d74c30c8SDaniil Tatianin };
114d74c30c8SDaniil Tatianin 
1156e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
1166e790746SPaolo Bonzini {
1176e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1186e790746SPaolo Bonzini 
1196e790746SPaolo Bonzini     return &n->vqs[nc->queue_index];
1206e790746SPaolo Bonzini }
1216e790746SPaolo Bonzini 
1226e790746SPaolo Bonzini static int vq2q(int queue_index)
1236e790746SPaolo Bonzini {
1246e790746SPaolo Bonzini     return queue_index / 2;
1256e790746SPaolo Bonzini }
1266e790746SPaolo Bonzini 
1274fdf69abSKangjie Xu static void flush_or_purge_queued_packets(NetClientState *nc)
1284fdf69abSKangjie Xu {
1294fdf69abSKangjie Xu     if (!nc->peer) {
1304fdf69abSKangjie Xu         return;
1314fdf69abSKangjie Xu     }
1324fdf69abSKangjie Xu 
1334fdf69abSKangjie Xu     qemu_flush_or_purge_queued_packets(nc->peer, true);
1344fdf69abSKangjie Xu     assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
1354fdf69abSKangjie Xu }
1364fdf69abSKangjie Xu 
1376e790746SPaolo Bonzini /* TODO
1386e790746SPaolo Bonzini  * - we could suppress RX interrupt if we were so inclined.
1396e790746SPaolo Bonzini  */
1406e790746SPaolo Bonzini 
1416e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
1426e790746SPaolo Bonzini {
14317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1446e790746SPaolo Bonzini     struct virtio_net_config netcfg;
145c546ecf2SJason Wang     NetClientState *nc = qemu_get_queue(n->nic);
146fb592882SCindy Lu     static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
1476e790746SPaolo Bonzini 
148108a6481SCindy Lu     int ret = 0;
149108a6481SCindy Lu     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
1501399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.status, n->status);
151441537f1SJason Wang     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs);
152a93e599dSMaxime Coquelin     virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
1536e790746SPaolo Bonzini     memcpy(netcfg.mac, n->mac, ETH_ALEN);
1549473939eSJason Baron     virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed);
1559473939eSJason Baron     netcfg.duplex = n->net_conf.duplex;
15659079029SYuri Benditovich     netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE;
15759079029SYuri Benditovich     virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length,
158e22f0603SYuri Benditovich                  virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ?
159e22f0603SYuri Benditovich                  VIRTIO_NET_RSS_MAX_TABLE_LEN : 1);
16059079029SYuri Benditovich     virtio_stl_p(vdev, &netcfg.supported_hash_types,
16159079029SYuri Benditovich                  VIRTIO_NET_RSS_SUPPORTED_HASHES);
1626e790746SPaolo Bonzini     memcpy(config, &netcfg, n->config_size);
163108a6481SCindy Lu 
164c546ecf2SJason Wang     /*
165c546ecf2SJason Wang      * Is this VDPA? No peer means not VDPA: there's no way to
166c546ecf2SJason Wang      * disconnect/reconnect a VDPA peer.
167c546ecf2SJason Wang      */
168c546ecf2SJason Wang     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
169108a6481SCindy Lu         ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
170108a6481SCindy Lu                                    n->config_size);
171ebc141a6SEugenio Pérez         if (ret == -1) {
172ebc141a6SEugenio Pérez             return;
173ebc141a6SEugenio Pérez         }
174ebc141a6SEugenio Pérez 
175fb592882SCindy Lu         /*
176ebc141a6SEugenio Pérez          * Some NIC/kernel combinations present 0 as the mac address.  As that
177ebc141a6SEugenio Pérez          * is not a legal address, try to proceed with the address from the
178ebc141a6SEugenio Pérez          * QEMU command line in the hope that the address has been configured
179ebc141a6SEugenio Pérez          * correctly elsewhere - just not reported by the device.
180fb592882SCindy Lu          */
181fb592882SCindy Lu         if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
182fb592882SCindy Lu             info_report("Zero hardware mac address detected. Ignoring.");
183fb592882SCindy Lu             memcpy(netcfg.mac, n->mac, ETH_ALEN);
184fb592882SCindy Lu         }
185ebc141a6SEugenio Pérez 
1864f93aafcSEugenio Pérez         netcfg.status |= virtio_tswap16(vdev,
1874f93aafcSEugenio Pérez                                         n->status & VIRTIO_NET_S_ANNOUNCE);
188108a6481SCindy Lu         memcpy(config, &netcfg, n->config_size);
189108a6481SCindy Lu     }
190108a6481SCindy Lu }
1916e790746SPaolo Bonzini 
1926e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
1936e790746SPaolo Bonzini {
19417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1956e790746SPaolo Bonzini     struct virtio_net_config netcfg = {};
196c546ecf2SJason Wang     NetClientState *nc = qemu_get_queue(n->nic);
1976e790746SPaolo Bonzini 
1986e790746SPaolo Bonzini     memcpy(&netcfg, config, n->config_size);
1996e790746SPaolo Bonzini 
20095129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
20195129d6fSCornelia Huck         !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
2026e790746SPaolo Bonzini         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
2036e790746SPaolo Bonzini         memcpy(n->mac, netcfg.mac, ETH_ALEN);
2046e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
2056e790746SPaolo Bonzini     }
206108a6481SCindy Lu 
207c546ecf2SJason Wang     /*
208c546ecf2SJason Wang      * Is this VDPA? No peer means not VDPA: there's no way to
209c546ecf2SJason Wang      * disconnect/reconnect a VDPA peer.
210c546ecf2SJason Wang      */
211c546ecf2SJason Wang     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
212c546ecf2SJason Wang         vhost_net_set_config(get_vhost_net(nc->peer),
213c546ecf2SJason Wang                              (uint8_t *)&netcfg, 0, n->config_size,
214f8ed3648SManos Pitsidianakis                              VHOST_SET_CONFIG_TYPE_FRONTEND);
215108a6481SCindy Lu       }
2166e790746SPaolo Bonzini }
2176e790746SPaolo Bonzini 
2186e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status)
2196e790746SPaolo Bonzini {
22017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2216e790746SPaolo Bonzini     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
22217a0ca55SKONRAD Frederic         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
2236e790746SPaolo Bonzini }
2246e790746SPaolo Bonzini 
225b2c929f0SDr. David Alan Gilbert static void virtio_net_announce_notify(VirtIONet *net)
226b2c929f0SDr. David Alan Gilbert {
227b2c929f0SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(net);
228b2c929f0SDr. David Alan Gilbert     trace_virtio_net_announce_notify();
229b2c929f0SDr. David Alan Gilbert 
230b2c929f0SDr. David Alan Gilbert     net->status |= VIRTIO_NET_S_ANNOUNCE;
231b2c929f0SDr. David Alan Gilbert     virtio_notify_config(vdev);
232b2c929f0SDr. David Alan Gilbert }
233b2c929f0SDr. David Alan Gilbert 
234f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque)
235f57fcf70SJason Wang {
236f57fcf70SJason Wang     VirtIONet *n = opaque;
2379d8c6a25SDr. David Alan Gilbert     trace_virtio_net_announce_timer(n->announce_timer.round);
238f57fcf70SJason Wang 
2399d8c6a25SDr. David Alan Gilbert     n->announce_timer.round--;
240b2c929f0SDr. David Alan Gilbert     virtio_net_announce_notify(n);
241b2c929f0SDr. David Alan Gilbert }
242b2c929f0SDr. David Alan Gilbert 
243b2c929f0SDr. David Alan Gilbert static void virtio_net_announce(NetClientState *nc)
244b2c929f0SDr. David Alan Gilbert {
245b2c929f0SDr. David Alan Gilbert     VirtIONet *n = qemu_get_nic_opaque(nc);
246b2c929f0SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
247b2c929f0SDr. David Alan Gilbert 
248b2c929f0SDr. David Alan Gilbert     /*
249b2c929f0SDr. David Alan Gilbert      * Make sure the virtio migration announcement timer isn't running
250b2c929f0SDr. David Alan Gilbert      * If it is, let it trigger announcement so that we do not cause
251b2c929f0SDr. David Alan Gilbert      * confusion.
252b2c929f0SDr. David Alan Gilbert      */
253b2c929f0SDr. David Alan Gilbert     if (n->announce_timer.round) {
254b2c929f0SDr. David Alan Gilbert         return;
255b2c929f0SDr. David Alan Gilbert     }
256b2c929f0SDr. David Alan Gilbert 
257b2c929f0SDr. David Alan Gilbert     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
258b2c929f0SDr. David Alan Gilbert         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
259b2c929f0SDr. David Alan Gilbert             virtio_net_announce_notify(n);
260b2c929f0SDr. David Alan Gilbert     }
261f57fcf70SJason Wang }
262f57fcf70SJason Wang 
2636e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
2646e790746SPaolo Bonzini {
26517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2666e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
267441537f1SJason Wang     int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
268aa858194SSi-Wei Liu     int cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
269aa858194SSi-Wei Liu               n->max_ncs - n->max_queue_pairs : 0;
2706e790746SPaolo Bonzini 
271ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
2726e790746SPaolo Bonzini         return;
2736e790746SPaolo Bonzini     }
2746e790746SPaolo Bonzini 
2758c1ac475SRadim Krčmář     if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
2768c1ac475SRadim Krčmář         !!n->vhost_started) {
2776e790746SPaolo Bonzini         return;
2786e790746SPaolo Bonzini     }
2796e790746SPaolo Bonzini     if (!n->vhost_started) {
280086abc1cSMichael S. Tsirkin         int r, i;
281086abc1cSMichael S. Tsirkin 
2821bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
2831bfa316cSGreg Kurz             error_report("backend does not support %s vnet headers; "
2841bfa316cSGreg Kurz                          "falling back on userspace virtio",
2851bfa316cSGreg Kurz                          virtio_is_big_endian(vdev) ? "BE" : "LE");
2861bfa316cSGreg Kurz             return;
2871bfa316cSGreg Kurz         }
2881bfa316cSGreg Kurz 
289086abc1cSMichael S. Tsirkin         /* Any packets outstanding? Purge them to avoid touching rings
290086abc1cSMichael S. Tsirkin          * when vhost is running.
291086abc1cSMichael S. Tsirkin          */
292441537f1SJason Wang         for (i = 0;  i < queue_pairs; i++) {
293086abc1cSMichael S. Tsirkin             NetClientState *qnc = qemu_get_subqueue(n->nic, i);
294086abc1cSMichael S. Tsirkin 
295086abc1cSMichael S. Tsirkin             /* Purge both directions: TX and RX. */
296086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
297086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
298086abc1cSMichael S. Tsirkin         }
299086abc1cSMichael S. Tsirkin 
300a93e599dSMaxime Coquelin         if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
301a93e599dSMaxime Coquelin             r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
302a93e599dSMaxime Coquelin             if (r < 0) {
303a93e599dSMaxime Coquelin                 error_report("%uBytes MTU not supported by the backend",
304a93e599dSMaxime Coquelin                              n->net_conf.mtu);
305a93e599dSMaxime Coquelin 
306a93e599dSMaxime Coquelin                 return;
307a93e599dSMaxime Coquelin             }
308a93e599dSMaxime Coquelin         }
309a93e599dSMaxime Coquelin 
3106e790746SPaolo Bonzini         n->vhost_started = 1;
31122288fe5SJason Wang         r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq);
3126e790746SPaolo Bonzini         if (r < 0) {
3136e790746SPaolo Bonzini             error_report("unable to start vhost net: %d: "
3146e790746SPaolo Bonzini                          "falling back on userspace virtio", -r);
3156e790746SPaolo Bonzini             n->vhost_started = 0;
3166e790746SPaolo Bonzini         }
3176e790746SPaolo Bonzini     } else {
31822288fe5SJason Wang         vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq);
3196e790746SPaolo Bonzini         n->vhost_started = 0;
3206e790746SPaolo Bonzini     }
3216e790746SPaolo Bonzini }
3226e790746SPaolo Bonzini 
3231bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
3241bfa316cSGreg Kurz                                           NetClientState *peer,
3251bfa316cSGreg Kurz                                           bool enable)
3261bfa316cSGreg Kurz {
3271bfa316cSGreg Kurz     if (virtio_is_big_endian(vdev)) {
3281bfa316cSGreg Kurz         return qemu_set_vnet_be(peer, enable);
3291bfa316cSGreg Kurz     } else {
3301bfa316cSGreg Kurz         return qemu_set_vnet_le(peer, enable);
3311bfa316cSGreg Kurz     }
3321bfa316cSGreg Kurz }
3331bfa316cSGreg Kurz 
3341bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
335441537f1SJason Wang                                        int queue_pairs, bool enable)
3361bfa316cSGreg Kurz {
3371bfa316cSGreg Kurz     int i;
3381bfa316cSGreg Kurz 
339441537f1SJason Wang     for (i = 0; i < queue_pairs; i++) {
3401bfa316cSGreg Kurz         if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
3411bfa316cSGreg Kurz             enable) {
3421bfa316cSGreg Kurz             while (--i >= 0) {
3431bfa316cSGreg Kurz                 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
3441bfa316cSGreg Kurz             }
3451bfa316cSGreg Kurz 
3461bfa316cSGreg Kurz             return true;
3471bfa316cSGreg Kurz         }
3481bfa316cSGreg Kurz     }
3491bfa316cSGreg Kurz 
3501bfa316cSGreg Kurz     return false;
3511bfa316cSGreg Kurz }
3521bfa316cSGreg Kurz 
3531bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
3541bfa316cSGreg Kurz {
3551bfa316cSGreg Kurz     VirtIODevice *vdev = VIRTIO_DEVICE(n);
356441537f1SJason Wang     int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
3571bfa316cSGreg Kurz 
3581bfa316cSGreg Kurz     if (virtio_net_started(n, status)) {
3591bfa316cSGreg Kurz         /* Before using the device, we tell the network backend about the
3601bfa316cSGreg Kurz          * endianness to use when parsing vnet headers. If the backend
3611bfa316cSGreg Kurz          * can't do it, we fallback onto fixing the headers in the core
3621bfa316cSGreg Kurz          * virtio-net code.
3631bfa316cSGreg Kurz          */
3641bfa316cSGreg Kurz         n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
365441537f1SJason Wang                                                             queue_pairs, true);
3661bfa316cSGreg Kurz     } else if (virtio_net_started(n, vdev->status)) {
3671bfa316cSGreg Kurz         /* After using the device, we need to reset the network backend to
3681bfa316cSGreg Kurz          * the default (guest native endianness), otherwise the guest may
3691bfa316cSGreg Kurz          * lose network connectivity if it is rebooted into a different
3701bfa316cSGreg Kurz          * endianness.
3711bfa316cSGreg Kurz          */
372441537f1SJason Wang         virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false);
3731bfa316cSGreg Kurz     }
3741bfa316cSGreg Kurz }
3751bfa316cSGreg Kurz 
376283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
377283e2c2aSYuri Benditovich {
378283e2c2aSYuri Benditovich     unsigned int dropped = virtqueue_drop_all(vq);
379283e2c2aSYuri Benditovich     if (dropped) {
380283e2c2aSYuri Benditovich         virtio_notify(vdev, vq);
381283e2c2aSYuri Benditovich     }
382283e2c2aSYuri Benditovich }
383283e2c2aSYuri Benditovich 
3846e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
3856e790746SPaolo Bonzini {
38617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
3876e790746SPaolo Bonzini     VirtIONetQueue *q;
3886e790746SPaolo Bonzini     int i;
3896e790746SPaolo Bonzini     uint8_t queue_status;
3906e790746SPaolo Bonzini 
3911bfa316cSGreg Kurz     virtio_net_vnet_endian_status(n, status);
3926e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
3936e790746SPaolo Bonzini 
394441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
39538705bb5SFam Zheng         NetClientState *ncs = qemu_get_subqueue(n->nic, i);
39638705bb5SFam Zheng         bool queue_started;
3976e790746SPaolo Bonzini         q = &n->vqs[i];
3986e790746SPaolo Bonzini 
399441537f1SJason Wang         if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) {
4006e790746SPaolo Bonzini             queue_status = 0;
4016e790746SPaolo Bonzini         } else {
4026e790746SPaolo Bonzini             queue_status = status;
4036e790746SPaolo Bonzini         }
40438705bb5SFam Zheng         queue_started =
40538705bb5SFam Zheng             virtio_net_started(n, queue_status) && !n->vhost_started;
40638705bb5SFam Zheng 
40738705bb5SFam Zheng         if (queue_started) {
40838705bb5SFam Zheng             qemu_flush_queued_packets(ncs);
40938705bb5SFam Zheng         }
4106e790746SPaolo Bonzini 
4116e790746SPaolo Bonzini         if (!q->tx_waiting) {
4126e790746SPaolo Bonzini             continue;
4136e790746SPaolo Bonzini         }
4146e790746SPaolo Bonzini 
41538705bb5SFam Zheng         if (queue_started) {
4166e790746SPaolo Bonzini             if (q->tx_timer) {
417bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
418bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
4196e790746SPaolo Bonzini             } else {
4206e790746SPaolo Bonzini                 qemu_bh_schedule(q->tx_bh);
4216e790746SPaolo Bonzini             }
4226e790746SPaolo Bonzini         } else {
4236e790746SPaolo Bonzini             if (q->tx_timer) {
424bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
4256e790746SPaolo Bonzini             } else {
4266e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
4276e790746SPaolo Bonzini             }
428283e2c2aSYuri Benditovich             if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
42970e53e6eSJason Wang                 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
43070e53e6eSJason Wang                 vdev->vm_running) {
431283e2c2aSYuri Benditovich                 /* if tx is waiting we are likely have some packets in tx queue
432283e2c2aSYuri Benditovich                  * and disabled notification */
433283e2c2aSYuri Benditovich                 q->tx_waiting = 0;
434283e2c2aSYuri Benditovich                 virtio_queue_set_notification(q->tx_vq, 1);
435283e2c2aSYuri Benditovich                 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
436283e2c2aSYuri Benditovich             }
4376e790746SPaolo Bonzini         }
4386e790746SPaolo Bonzini     }
4396e790746SPaolo Bonzini }
4406e790746SPaolo Bonzini 
4416e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
4426e790746SPaolo Bonzini {
4436e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
44417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
4456e790746SPaolo Bonzini     uint16_t old_status = n->status;
4466e790746SPaolo Bonzini 
4476e790746SPaolo Bonzini     if (nc->link_down)
4486e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
4496e790746SPaolo Bonzini     else
4506e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
4516e790746SPaolo Bonzini 
4526e790746SPaolo Bonzini     if (n->status != old_status)
45317a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
4546e790746SPaolo Bonzini 
45517a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
4566e790746SPaolo Bonzini }
4576e790746SPaolo Bonzini 
458b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
459b1be4280SAmos Kong {
460b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
461b1be4280SAmos Kong 
462b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
463ddfb0baaSMarkus Armbruster         char *path = object_get_canonical_path(OBJECT(n->qdev));
4647480874aSMarkus Armbruster         qapi_event_send_nic_rx_filter_changed(n->netclient_name, path);
46596e35046SAmos Kong         g_free(path);
466b1be4280SAmos Kong 
467b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
468b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
469b1be4280SAmos Kong     }
470b1be4280SAmos Kong }
471b1be4280SAmos Kong 
472f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
473f7bc8ef8SAmos Kong {
47454aa3de7SEric Blake     intList *list;
475f7bc8ef8SAmos Kong     int i, j;
476f7bc8ef8SAmos Kong 
477f7bc8ef8SAmos Kong     list = NULL;
478f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
479f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
480f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
48154aa3de7SEric Blake                 QAPI_LIST_PREPEND(list, (i << 5) + j);
482f7bc8ef8SAmos Kong             }
483f7bc8ef8SAmos Kong         }
484f7bc8ef8SAmos Kong     }
485f7bc8ef8SAmos Kong 
486f7bc8ef8SAmos Kong     return list;
487f7bc8ef8SAmos Kong }
488f7bc8ef8SAmos Kong 
489b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
490b1be4280SAmos Kong {
491b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
492f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
493b1be4280SAmos Kong     RxFilterInfo *info;
49454aa3de7SEric Blake     strList *str_list;
495f7bc8ef8SAmos Kong     int i;
496b1be4280SAmos Kong 
497b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
498b1be4280SAmos Kong     info->name = g_strdup(nc->name);
499b1be4280SAmos Kong     info->promiscuous = n->promisc;
500b1be4280SAmos Kong 
501b1be4280SAmos Kong     if (n->nouni) {
502b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
503b1be4280SAmos Kong     } else if (n->alluni) {
504b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
505b1be4280SAmos Kong     } else {
506b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
507b1be4280SAmos Kong     }
508b1be4280SAmos Kong 
509b1be4280SAmos Kong     if (n->nomulti) {
510b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
511b1be4280SAmos Kong     } else if (n->allmulti) {
512b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
513b1be4280SAmos Kong     } else {
514b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
515b1be4280SAmos Kong     }
516b1be4280SAmos Kong 
517b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
518b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
519b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
520b1be4280SAmos Kong 
521b0575ba4SScott Feldman     info->main_mac = qemu_mac_strdup_printf(n->mac);
522b1be4280SAmos Kong 
523b1be4280SAmos Kong     str_list = NULL;
524b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
52554aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
52654aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
527b1be4280SAmos Kong     }
528b1be4280SAmos Kong     info->unicast_table = str_list;
529b1be4280SAmos Kong 
530b1be4280SAmos Kong     str_list = NULL;
531b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
53254aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
53354aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
534b1be4280SAmos Kong     }
535b1be4280SAmos Kong     info->multicast_table = str_list;
536f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
537b1be4280SAmos Kong 
53895129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
539f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
540f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
541f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
542f7bc8ef8SAmos Kong     } else {
543f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
544b1be4280SAmos Kong     }
545b1be4280SAmos Kong 
546b1be4280SAmos Kong     /* enable event notification after query */
547b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
548b1be4280SAmos Kong 
549b1be4280SAmos Kong     return info;
550b1be4280SAmos Kong }
551b1be4280SAmos Kong 
5527dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index)
5537dc6be52SXuan Zhuo {
5547dc6be52SXuan Zhuo     VirtIONet *n = VIRTIO_NET(vdev);
555f47af0afSXuan Zhuo     NetClientState *nc;
556f47af0afSXuan Zhuo 
557f47af0afSXuan Zhuo     /* validate queue_index and skip for cvq */
558f47af0afSXuan Zhuo     if (queue_index >= n->max_queue_pairs * 2) {
559f47af0afSXuan Zhuo         return;
560f47af0afSXuan Zhuo     }
561f47af0afSXuan Zhuo 
562f47af0afSXuan Zhuo     nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
5637dc6be52SXuan Zhuo 
5647dc6be52SXuan Zhuo     if (!nc->peer) {
5657dc6be52SXuan Zhuo         return;
5667dc6be52SXuan Zhuo     }
5677dc6be52SXuan Zhuo 
5687dc6be52SXuan Zhuo     if (get_vhost_net(nc->peer) &&
5697dc6be52SXuan Zhuo         nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
5707dc6be52SXuan Zhuo         vhost_net_virtqueue_reset(vdev, nc, queue_index);
5717dc6be52SXuan Zhuo     }
5727dc6be52SXuan Zhuo 
5737dc6be52SXuan Zhuo     flush_or_purge_queued_packets(nc);
5747dc6be52SXuan Zhuo }
5757dc6be52SXuan Zhuo 
5767f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index)
5777f863302SKangjie Xu {
5787f863302SKangjie Xu     VirtIONet *n = VIRTIO_NET(vdev);
579f47af0afSXuan Zhuo     NetClientState *nc;
5807f863302SKangjie Xu     int r;
5817f863302SKangjie Xu 
582f47af0afSXuan Zhuo     /* validate queue_index and skip for cvq */
583f47af0afSXuan Zhuo     if (queue_index >= n->max_queue_pairs * 2) {
584f47af0afSXuan Zhuo         return;
585f47af0afSXuan Zhuo     }
586f47af0afSXuan Zhuo 
587f47af0afSXuan Zhuo     nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
588f47af0afSXuan Zhuo 
5897f863302SKangjie Xu     if (!nc->peer || !vdev->vhost_started) {
5907f863302SKangjie Xu         return;
5917f863302SKangjie Xu     }
5927f863302SKangjie Xu 
5937f863302SKangjie Xu     if (get_vhost_net(nc->peer) &&
5947f863302SKangjie Xu         nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
5957f863302SKangjie Xu         r = vhost_net_virtqueue_restart(vdev, nc, queue_index);
5967f863302SKangjie Xu         if (r < 0) {
5977f863302SKangjie Xu             error_report("unable to restart vhost net virtqueue: %d, "
5987f863302SKangjie Xu                             "when resetting the queue", queue_index);
5997f863302SKangjie Xu         }
6007f863302SKangjie Xu     }
6017f863302SKangjie Xu }
6027f863302SKangjie Xu 
6036e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev)
6046e790746SPaolo Bonzini {
60517a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
60694b52958SGreg Kurz     int i;
6076e790746SPaolo Bonzini 
6086e790746SPaolo Bonzini     /* Reset back to compatibility mode */
6096e790746SPaolo Bonzini     n->promisc = 1;
6106e790746SPaolo Bonzini     n->allmulti = 0;
6116e790746SPaolo Bonzini     n->alluni = 0;
6126e790746SPaolo Bonzini     n->nomulti = 0;
6136e790746SPaolo Bonzini     n->nouni = 0;
6146e790746SPaolo Bonzini     n->nobcast = 0;
6156e790746SPaolo Bonzini     /* multiqueue is disabled by default */
616441537f1SJason Wang     n->curr_queue_pairs = 1;
6179d8c6a25SDr. David Alan Gilbert     timer_del(n->announce_timer.tm);
6189d8c6a25SDr. David Alan Gilbert     n->announce_timer.round = 0;
619f57fcf70SJason Wang     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
6206e790746SPaolo Bonzini 
6216e790746SPaolo Bonzini     /* Flush any MAC and VLAN filter table state */
6226e790746SPaolo Bonzini     n->mac_table.in_use = 0;
6236e790746SPaolo Bonzini     n->mac_table.first_multi = 0;
6246e790746SPaolo Bonzini     n->mac_table.multi_overflow = 0;
6256e790746SPaolo Bonzini     n->mac_table.uni_overflow = 0;
6266e790746SPaolo Bonzini     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
6276e790746SPaolo Bonzini     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
628702d66a8SMichael S. Tsirkin     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
6296e790746SPaolo Bonzini     memset(n->vlans, 0, MAX_VLAN >> 3);
63094b52958SGreg Kurz 
63194b52958SGreg Kurz     /* Flush any async TX */
632441537f1SJason Wang     for (i = 0;  i < n->max_queue_pairs; i++) {
6334fdf69abSKangjie Xu         flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i));
63494b52958SGreg Kurz     }
6356e790746SPaolo Bonzini }
6366e790746SPaolo Bonzini 
6376e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
6386e790746SPaolo Bonzini {
6396e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
6406e790746SPaolo Bonzini     if (!nc->peer) {
6416e790746SPaolo Bonzini         return;
6426e790746SPaolo Bonzini     }
6436e790746SPaolo Bonzini 
644d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
6456e790746SPaolo Bonzini }
6466e790746SPaolo Bonzini 
6476e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
6486e790746SPaolo Bonzini {
6496e790746SPaolo Bonzini     return n->has_vnet_hdr;
6506e790746SPaolo Bonzini }
6516e790746SPaolo Bonzini 
6526e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
6536e790746SPaolo Bonzini {
6546e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
6556e790746SPaolo Bonzini         return 0;
6566e790746SPaolo Bonzini 
657d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
6586e790746SPaolo Bonzini 
6596e790746SPaolo Bonzini     return n->has_ufo;
6606e790746SPaolo Bonzini }
6616e790746SPaolo Bonzini 
662*53da8b5aSYuri Benditovich static int peer_has_uso(VirtIONet *n)
663*53da8b5aSYuri Benditovich {
664*53da8b5aSYuri Benditovich     if (!peer_has_vnet_hdr(n)) {
665*53da8b5aSYuri Benditovich         return 0;
666*53da8b5aSYuri Benditovich     }
667*53da8b5aSYuri Benditovich 
668*53da8b5aSYuri Benditovich     return qemu_has_uso(qemu_get_queue(n->nic)->peer);
669*53da8b5aSYuri Benditovich }
670*53da8b5aSYuri Benditovich 
671bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
672e22f0603SYuri Benditovich                                        int version_1, int hash_report)
6736e790746SPaolo Bonzini {
6746e790746SPaolo Bonzini     int i;
6756e790746SPaolo Bonzini     NetClientState *nc;
6766e790746SPaolo Bonzini 
6776e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
6786e790746SPaolo Bonzini 
679bb9d17f8SCornelia Huck     if (version_1) {
680e22f0603SYuri Benditovich         n->guest_hdr_len = hash_report ?
681e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_v1_hash) :
682e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_mrg_rxbuf);
683e22f0603SYuri Benditovich         n->rss_data.populate_hash = !!hash_report;
684bb9d17f8SCornelia Huck     } else {
6856e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
686bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
687bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
688bb9d17f8SCornelia Huck     }
6896e790746SPaolo Bonzini 
690441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
6916e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
6926e790746SPaolo Bonzini 
6936e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
694d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
695d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
6966e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
6976e790746SPaolo Bonzini         }
6986e790746SPaolo Bonzini     }
6996e790746SPaolo Bonzini }
7006e790746SPaolo Bonzini 
7012eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n)
7022eef278bSMichael S. Tsirkin {
7032eef278bSMichael S. Tsirkin     NetClientState *peer = n->nic_conf.peers.ncs[0];
7042eef278bSMichael S. Tsirkin 
7052eef278bSMichael S. Tsirkin     /*
7060ea5778fSEugenio Pérez      * Backends other than vhost-user or vhost-vdpa don't support max queue
7070ea5778fSEugenio Pérez      * size.
7082eef278bSMichael S. Tsirkin      */
7092eef278bSMichael S. Tsirkin     if (!peer) {
7102eef278bSMichael S. Tsirkin         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
7112eef278bSMichael S. Tsirkin     }
7122eef278bSMichael S. Tsirkin 
7130ea5778fSEugenio Pérez     switch(peer->info->type) {
7140ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_USER:
7150ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_VDPA:
7162eef278bSMichael S. Tsirkin         return VIRTQUEUE_MAX_SIZE;
7170ea5778fSEugenio Pérez     default:
7180ea5778fSEugenio Pérez         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
7190ea5778fSEugenio Pérez     };
7202eef278bSMichael S. Tsirkin }
7212eef278bSMichael S. Tsirkin 
7226e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
7236e790746SPaolo Bonzini {
7246e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
7256e790746SPaolo Bonzini 
7266e790746SPaolo Bonzini     if (!nc->peer) {
7276e790746SPaolo Bonzini         return 0;
7286e790746SPaolo Bonzini     }
7296e790746SPaolo Bonzini 
730f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7317263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 1);
7327263a0adSChangchun Ouyang     }
7337263a0adSChangchun Ouyang 
734f394b2e2SEric Blake     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
7356e790746SPaolo Bonzini         return 0;
7366e790746SPaolo Bonzini     }
7376e790746SPaolo Bonzini 
738441537f1SJason Wang     if (n->max_queue_pairs == 1) {
7391074b879SJason Wang         return 0;
7401074b879SJason Wang     }
7411074b879SJason Wang 
7426e790746SPaolo Bonzini     return tap_enable(nc->peer);
7436e790746SPaolo Bonzini }
7446e790746SPaolo Bonzini 
7456e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
7466e790746SPaolo Bonzini {
7476e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
7486e790746SPaolo Bonzini 
7496e790746SPaolo Bonzini     if (!nc->peer) {
7506e790746SPaolo Bonzini         return 0;
7516e790746SPaolo Bonzini     }
7526e790746SPaolo Bonzini 
753f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7547263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 0);
7557263a0adSChangchun Ouyang     }
7567263a0adSChangchun Ouyang 
757f394b2e2SEric Blake     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
7586e790746SPaolo Bonzini         return 0;
7596e790746SPaolo Bonzini     }
7606e790746SPaolo Bonzini 
7616e790746SPaolo Bonzini     return tap_disable(nc->peer);
7626e790746SPaolo Bonzini }
7636e790746SPaolo Bonzini 
764441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n)
7656e790746SPaolo Bonzini {
7666e790746SPaolo Bonzini     int i;
767ddfa83eaSJoel Stanley     int r;
7686e790746SPaolo Bonzini 
76968b5f314SYuri Benditovich     if (n->nic->peer_deleted) {
77068b5f314SYuri Benditovich         return;
77168b5f314SYuri Benditovich     }
77268b5f314SYuri Benditovich 
773441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
774441537f1SJason Wang         if (i < n->curr_queue_pairs) {
775ddfa83eaSJoel Stanley             r = peer_attach(n, i);
776ddfa83eaSJoel Stanley             assert(!r);
7776e790746SPaolo Bonzini         } else {
778ddfa83eaSJoel Stanley             r = peer_detach(n, i);
779ddfa83eaSJoel Stanley             assert(!r);
7806e790746SPaolo Bonzini         }
7816e790746SPaolo Bonzini     }
7826e790746SPaolo Bonzini }
7836e790746SPaolo Bonzini 
784ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
7856e790746SPaolo Bonzini 
7869d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
7879d5b731dSJason Wang                                         Error **errp)
7886e790746SPaolo Bonzini {
78917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
7906e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
7916e790746SPaolo Bonzini 
792da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
793da3e8a23SShannon Zhao     features |= n->host_features;
794da3e8a23SShannon Zhao 
7950cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7966e790746SPaolo Bonzini 
7976e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
7980cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
7990cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
8000cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
8010cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
8026e790746SPaolo Bonzini 
8030cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
8040cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
8050cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
8060cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
807e22f0603SYuri Benditovich 
808*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
809*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
810*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
811*53da8b5aSYuri Benditovich 
812e22f0603SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
8136e790746SPaolo Bonzini     }
8146e790746SPaolo Bonzini 
8156e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
8160cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
8170cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
8186e790746SPaolo Bonzini     }
8196e790746SPaolo Bonzini 
820*53da8b5aSYuri Benditovich     if (!peer_has_uso(n)) {
821*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
822*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
823*53da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
824*53da8b5aSYuri Benditovich     }
825*53da8b5aSYuri Benditovich 
826ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
8276e790746SPaolo Bonzini         return features;
8286e790746SPaolo Bonzini     }
8292974e916SYuri Benditovich 
8300145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
83159079029SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
8320145c393SAndrew Melnychenko     }
83375ebec11SMaxime Coquelin     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
83475ebec11SMaxime Coquelin     vdev->backend_features = features;
83575ebec11SMaxime Coquelin 
83675ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
83775ebec11SMaxime Coquelin             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
83875ebec11SMaxime Coquelin         features |= (1ULL << VIRTIO_NET_F_MTU);
83975ebec11SMaxime Coquelin     }
84075ebec11SMaxime Coquelin 
841cd69d47cSEugenio Pérez     /*
842cd69d47cSEugenio Pérez      * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
843cd69d47cSEugenio Pérez      * enabled. This happens in the vDPA case.
844cd69d47cSEugenio Pérez      *
845cd69d47cSEugenio Pérez      * Make sure the feature set is not incoherent, as the driver could refuse
846cd69d47cSEugenio Pérez      * to start.
847cd69d47cSEugenio Pérez      *
848cd69d47cSEugenio Pérez      * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
849cd69d47cSEugenio Pérez      * helping guest to notify the new location with vDPA devices that does not
850cd69d47cSEugenio Pérez      * support it.
851cd69d47cSEugenio Pérez      */
852cd69d47cSEugenio Pérez     if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) {
853cd69d47cSEugenio Pérez         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE);
854cd69d47cSEugenio Pérez     }
855cd69d47cSEugenio Pérez 
85675ebec11SMaxime Coquelin     return features;
8576e790746SPaolo Bonzini }
8586e790746SPaolo Bonzini 
859019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
8606e790746SPaolo Bonzini {
861019a3edbSGerd Hoffmann     uint64_t features = 0;
8626e790746SPaolo Bonzini 
8636e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
8646e790746SPaolo Bonzini      * but also these: */
8650cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
8660cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
8670cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
8680cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
8690cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
8706e790746SPaolo Bonzini 
8716e790746SPaolo Bonzini     return features;
8726e790746SPaolo Bonzini }
8736e790746SPaolo Bonzini 
874644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
875644c9858SDmitry Fleytman {
876ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
877644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
878644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
879644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
880644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
8812ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)),
8822ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)),
8832ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6)));
884644c9858SDmitry Fleytman }
885644c9858SDmitry Fleytman 
886*53da8b5aSYuri Benditovich static uint64_t virtio_net_guest_offloads_by_features(uint64_t features)
887644c9858SDmitry Fleytman {
888644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
889644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
890644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
891644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
892644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
893*53da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_UFO)  |
894*53da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_USO4) |
895*53da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_USO6);
896644c9858SDmitry Fleytman 
897644c9858SDmitry Fleytman     return guest_offloads_mask & features;
898644c9858SDmitry Fleytman }
899644c9858SDmitry Fleytman 
9000b545b1eSHawkins Jiawei uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n)
901644c9858SDmitry Fleytman {
902644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
903644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
904644c9858SDmitry Fleytman }
905644c9858SDmitry Fleytman 
906f5e1847bSJuan Quintela typedef struct {
907f5e1847bSJuan Quintela     VirtIONet *n;
90812b2fad7SKevin Wolf     DeviceState *dev;
90912b2fad7SKevin Wolf } FailoverDevice;
910f5e1847bSJuan Quintela 
911f5e1847bSJuan Quintela /**
91212b2fad7SKevin Wolf  * Set the failover primary device
913f5e1847bSJuan Quintela  *
914f5e1847bSJuan Quintela  * @opaque: FailoverId to setup
915f5e1847bSJuan Quintela  * @opts: opts for device we are handling
916f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
917f5e1847bSJuan Quintela  */
91812b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque)
919f5e1847bSJuan Quintela {
92012b2fad7SKevin Wolf     FailoverDevice *fdev = opaque;
92112b2fad7SKevin Wolf     PCIDevice *pci_dev = (PCIDevice *)
92212b2fad7SKevin Wolf         object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE);
923f5e1847bSJuan Quintela 
92412b2fad7SKevin Wolf     if (!pci_dev) {
92512b2fad7SKevin Wolf         return 0;
92612b2fad7SKevin Wolf     }
92712b2fad7SKevin Wolf 
92812b2fad7SKevin Wolf     if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) {
92912b2fad7SKevin Wolf         fdev->dev = dev;
930f5e1847bSJuan Quintela         return 1;
931f5e1847bSJuan Quintela     }
932f5e1847bSJuan Quintela 
933f5e1847bSJuan Quintela     return 0;
934f5e1847bSJuan Quintela }
935f5e1847bSJuan Quintela 
936f5e1847bSJuan Quintela /**
93785d3b931SJuan Quintela  * Find the primary device for this failover virtio-net
93885d3b931SJuan Quintela  *
93985d3b931SJuan Quintela  * @n: VirtIONet device
94085d3b931SJuan Quintela  * @errp: returns an error if this function fails
94185d3b931SJuan Quintela  */
9420a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n)
9439711cd0dSJens Freimann {
94412b2fad7SKevin Wolf     FailoverDevice fdev = {
94512b2fad7SKevin Wolf         .n = n,
94612b2fad7SKevin Wolf     };
9479711cd0dSJens Freimann 
94812b2fad7SKevin Wolf     qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL,
94912b2fad7SKevin Wolf                        NULL, NULL, &fdev);
95012b2fad7SKevin Wolf     return fdev.dev;
9519711cd0dSJens Freimann }
9529711cd0dSJens Freimann 
95321e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp)
95421e8709bSJuan Quintela {
95521e8709bSJuan Quintela     Error *err = NULL;
95621e8709bSJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
95721e8709bSJuan Quintela 
95821e8709bSJuan Quintela     if (dev) {
95921e8709bSJuan Quintela         return;
96021e8709bSJuan Quintela     }
96121e8709bSJuan Quintela 
962259a10dbSKevin Wolf     if (!n->primary_opts) {
96397ca9c59SLaurent Vivier         error_setg(errp, "Primary device not found");
96497ca9c59SLaurent Vivier         error_append_hint(errp, "Virtio-net failover will not work. Make "
96597ca9c59SLaurent Vivier                           "sure primary device has parameter"
96697ca9c59SLaurent Vivier                           " failover_pair_id=%s\n", n->netclient_name);
96721e8709bSJuan Quintela         return;
96821e8709bSJuan Quintela     }
969259a10dbSKevin Wolf 
970f3558b1bSKevin Wolf     dev = qdev_device_add_from_qdict(n->primary_opts,
971f3558b1bSKevin Wolf                                      n->primary_opts_from_json,
972f3558b1bSKevin Wolf                                      &err);
97321e8709bSJuan Quintela     if (err) {
974f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
975259a10dbSKevin Wolf         n->primary_opts = NULL;
97600e7b129SLaurent Vivier     } else {
97700e7b129SLaurent Vivier         object_unref(OBJECT(dev));
97821e8709bSJuan Quintela     }
97921e8709bSJuan Quintela     error_propagate(errp, err);
98021e8709bSJuan Quintela }
98121e8709bSJuan Quintela 
982d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
9836e790746SPaolo Bonzini {
98417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
9859711cd0dSJens Freimann     Error *err = NULL;
9866e790746SPaolo Bonzini     int i;
9876e790746SPaolo Bonzini 
98875ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
98975ebec11SMaxime Coquelin             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
99075ebec11SMaxime Coquelin         features &= ~(1ULL << VIRTIO_NET_F_MTU);
99175ebec11SMaxime Coquelin     }
99275ebec11SMaxime Coquelin 
993ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
99459079029SYuri Benditovich                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
99595129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
9966e790746SPaolo Bonzini 
997ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
99895129d6fSCornelia Huck                                virtio_has_feature(features,
999bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
100095129d6fSCornelia Huck                                virtio_has_feature(features,
1001e22f0603SYuri Benditovich                                                   VIRTIO_F_VERSION_1),
1002e22f0603SYuri Benditovich                                virtio_has_feature(features,
1003e22f0603SYuri Benditovich                                                   VIRTIO_NET_F_HASH_REPORT));
10046e790746SPaolo Bonzini 
10052974e916SYuri Benditovich     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
10062974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
10072974e916SYuri Benditovich     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
10082974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
1009e22f0603SYuri Benditovich     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
10102974e916SYuri Benditovich 
10116e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
1012644c9858SDmitry Fleytman         n->curr_guest_offloads =
1013644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
1014644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
10156e790746SPaolo Bonzini     }
10166e790746SPaolo Bonzini 
1017441537f1SJason Wang     for (i = 0;  i < n->max_queue_pairs; i++) {
10186e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
10196e790746SPaolo Bonzini 
1020ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
10216e790746SPaolo Bonzini             continue;
10226e790746SPaolo Bonzini         }
1023ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
1024c9bdc449SHyman Huang(黄勇) 
1025c9bdc449SHyman Huang(黄勇)         /*
1026c9bdc449SHyman Huang(黄勇)          * keep acked_features in NetVhostUserState up-to-date so it
1027c9bdc449SHyman Huang(黄勇)          * can't miss any features configured by guest virtio driver.
1028c9bdc449SHyman Huang(黄勇)          */
1029c9bdc449SHyman Huang(黄勇)         vhost_net_save_acked_features(nc->peer);
10306e790746SPaolo Bonzini     }
10310b1eaa88SStefan Fritsch 
103295129d6fSCornelia Huck     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
10330b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
10340b1eaa88SStefan Fritsch     } else {
10350b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
10360b1eaa88SStefan Fritsch     }
10379711cd0dSJens Freimann 
10389711cd0dSJens Freimann     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
10399711cd0dSJens Freimann         qapi_event_send_failover_negotiated(n->netclient_name);
1040e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, false);
10419711cd0dSJens Freimann         failover_add_primary(n, &err);
10429711cd0dSJens Freimann         if (err) {
10431b529d90SLaurent Vivier             if (!qtest_enabled()) {
10449711cd0dSJens Freimann                 warn_report_err(err);
10451b529d90SLaurent Vivier             } else {
10461b529d90SLaurent Vivier                 error_free(err);
10471b529d90SLaurent Vivier             }
10489711cd0dSJens Freimann         }
10496e790746SPaolo Bonzini     }
105021e8709bSJuan Quintela }
10516e790746SPaolo Bonzini 
10526e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
10536e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
10546e790746SPaolo Bonzini {
10556e790746SPaolo Bonzini     uint8_t on;
10566e790746SPaolo Bonzini     size_t s;
1057b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
10586e790746SPaolo Bonzini 
10596e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
10606e790746SPaolo Bonzini     if (s != sizeof(on)) {
10616e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10626e790746SPaolo Bonzini     }
10636e790746SPaolo Bonzini 
10646e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
10656e790746SPaolo Bonzini         n->promisc = on;
10666e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
10676e790746SPaolo Bonzini         n->allmulti = on;
10686e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
10696e790746SPaolo Bonzini         n->alluni = on;
10706e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
10716e790746SPaolo Bonzini         n->nomulti = on;
10726e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
10736e790746SPaolo Bonzini         n->nouni = on;
10746e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
10756e790746SPaolo Bonzini         n->nobcast = on;
10766e790746SPaolo Bonzini     } else {
10776e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10786e790746SPaolo Bonzini     }
10796e790746SPaolo Bonzini 
1080b1be4280SAmos Kong     rxfilter_notify(nc);
1081b1be4280SAmos Kong 
10826e790746SPaolo Bonzini     return VIRTIO_NET_OK;
10836e790746SPaolo Bonzini }
10846e790746SPaolo Bonzini 
1085644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
1086644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
1087644c9858SDmitry Fleytman {
1088644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1089644c9858SDmitry Fleytman     uint64_t offloads;
1090644c9858SDmitry Fleytman     size_t s;
1091644c9858SDmitry Fleytman 
109295129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1093644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1094644c9858SDmitry Fleytman     }
1095644c9858SDmitry Fleytman 
1096644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
1097644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
1098644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1099644c9858SDmitry Fleytman     }
1100644c9858SDmitry Fleytman 
1101644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
1102644c9858SDmitry Fleytman         uint64_t supported_offloads;
1103644c9858SDmitry Fleytman 
1104189ae6bbSJason Wang         offloads = virtio_ldq_p(vdev, &offloads);
1105189ae6bbSJason Wang 
1106644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
1107644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1108644c9858SDmitry Fleytman         }
1109644c9858SDmitry Fleytman 
11102974e916SYuri Benditovich         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
11112974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
11122974e916SYuri Benditovich         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
11132974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
11142974e916SYuri Benditovich         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
11152974e916SYuri Benditovich 
1116644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
1117644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
1118644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1119644c9858SDmitry Fleytman         }
1120644c9858SDmitry Fleytman 
1121644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
1122644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1123644c9858SDmitry Fleytman 
1124644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
1125644c9858SDmitry Fleytman     } else {
1126644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1127644c9858SDmitry Fleytman     }
1128644c9858SDmitry Fleytman }
1129644c9858SDmitry Fleytman 
11306e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
11316e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
11326e790746SPaolo Bonzini {
11331399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11346e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
11356e790746SPaolo Bonzini     size_t s;
1136b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11376e790746SPaolo Bonzini 
11386e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
11396e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
11406e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
11416e790746SPaolo Bonzini         }
11426e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
11436e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
11446e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1145b1be4280SAmos Kong         rxfilter_notify(nc);
1146b1be4280SAmos Kong 
11476e790746SPaolo Bonzini         return VIRTIO_NET_OK;
11486e790746SPaolo Bonzini     }
11496e790746SPaolo Bonzini 
11506e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
11516e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11526e790746SPaolo Bonzini     }
11536e790746SPaolo Bonzini 
1154cae2e556SAmos Kong     int in_use = 0;
1155cae2e556SAmos Kong     int first_multi = 0;
1156cae2e556SAmos Kong     uint8_t uni_overflow = 0;
1157cae2e556SAmos Kong     uint8_t multi_overflow = 0;
1158cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
11596e790746SPaolo Bonzini 
11606e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11616e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11621399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11636e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1164b1be4280SAmos Kong         goto error;
11656e790746SPaolo Bonzini     }
11666e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11676e790746SPaolo Bonzini 
11686e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1169b1be4280SAmos Kong         goto error;
11706e790746SPaolo Bonzini     }
11716e790746SPaolo Bonzini 
11726e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1173cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
11746e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
11756e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1176b1be4280SAmos Kong             goto error;
11776e790746SPaolo Bonzini         }
1178cae2e556SAmos Kong         in_use += mac_data.entries;
11796e790746SPaolo Bonzini     } else {
1180cae2e556SAmos Kong         uni_overflow = 1;
11816e790746SPaolo Bonzini     }
11826e790746SPaolo Bonzini 
11836e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
11846e790746SPaolo Bonzini 
1185cae2e556SAmos Kong     first_multi = in_use;
11866e790746SPaolo Bonzini 
11876e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11886e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11891399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11906e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1191b1be4280SAmos Kong         goto error;
11926e790746SPaolo Bonzini     }
11936e790746SPaolo Bonzini 
11946e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11956e790746SPaolo Bonzini 
11966e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1197b1be4280SAmos Kong         goto error;
11986e790746SPaolo Bonzini     }
11996e790746SPaolo Bonzini 
1200edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1201cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
12026e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
12036e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1204b1be4280SAmos Kong             goto error;
12056e790746SPaolo Bonzini         }
1206cae2e556SAmos Kong         in_use += mac_data.entries;
12076e790746SPaolo Bonzini     } else {
1208cae2e556SAmos Kong         multi_overflow = 1;
12096e790746SPaolo Bonzini     }
12106e790746SPaolo Bonzini 
1211cae2e556SAmos Kong     n->mac_table.in_use = in_use;
1212cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
1213cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
1214cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
1215cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1216cae2e556SAmos Kong     g_free(macs);
1217b1be4280SAmos Kong     rxfilter_notify(nc);
1218b1be4280SAmos Kong 
12196e790746SPaolo Bonzini     return VIRTIO_NET_OK;
1220b1be4280SAmos Kong 
1221b1be4280SAmos Kong error:
1222cae2e556SAmos Kong     g_free(macs);
1223b1be4280SAmos Kong     return VIRTIO_NET_ERR;
12246e790746SPaolo Bonzini }
12256e790746SPaolo Bonzini 
12266e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
12276e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
12286e790746SPaolo Bonzini {
12291399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12306e790746SPaolo Bonzini     uint16_t vid;
12316e790746SPaolo Bonzini     size_t s;
1232b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
12336e790746SPaolo Bonzini 
12346e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
12351399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
12366e790746SPaolo Bonzini     if (s != sizeof(vid)) {
12376e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12386e790746SPaolo Bonzini     }
12396e790746SPaolo Bonzini 
12406e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
12416e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12426e790746SPaolo Bonzini 
12436e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
12446e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
12456e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
12466e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
12476e790746SPaolo Bonzini     else
12486e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12496e790746SPaolo Bonzini 
1250b1be4280SAmos Kong     rxfilter_notify(nc);
1251b1be4280SAmos Kong 
12526e790746SPaolo Bonzini     return VIRTIO_NET_OK;
12536e790746SPaolo Bonzini }
12546e790746SPaolo Bonzini 
1255f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1256f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
1257f57fcf70SJason Wang {
12589d8c6a25SDr. David Alan Gilbert     trace_virtio_net_handle_announce(n->announce_timer.round);
1259f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1260f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
1261f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
12629d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
12639d8c6a25SDr. David Alan Gilbert             qemu_announce_timer_step(&n->announce_timer);
1264f57fcf70SJason Wang         }
1265f57fcf70SJason Wang         return VIRTIO_NET_OK;
1266f57fcf70SJason Wang     } else {
1267f57fcf70SJason Wang         return VIRTIO_NET_ERR;
1268f57fcf70SJason Wang     }
1269f57fcf70SJason Wang }
1270f57fcf70SJason Wang 
12710145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n);
12720145c393SAndrew Melnychenko 
127359079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n)
127459079029SYuri Benditovich {
127559079029SYuri Benditovich     if (n->rss_data.enabled) {
127659079029SYuri Benditovich         trace_virtio_net_rss_disable();
127759079029SYuri Benditovich     }
127859079029SYuri Benditovich     n->rss_data.enabled = false;
12790145c393SAndrew Melnychenko 
12800145c393SAndrew Melnychenko     virtio_net_detach_epbf_rss(n);
12810145c393SAndrew Melnychenko }
12820145c393SAndrew Melnychenko 
12830145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd)
12840145c393SAndrew Melnychenko {
12850145c393SAndrew Melnychenko     NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0);
12860145c393SAndrew Melnychenko     if (nc == NULL || nc->info->set_steering_ebpf == NULL) {
12870145c393SAndrew Melnychenko         return false;
12880145c393SAndrew Melnychenko     }
12890145c393SAndrew Melnychenko 
12900145c393SAndrew Melnychenko     return nc->info->set_steering_ebpf(nc, prog_fd);
12910145c393SAndrew Melnychenko }
12920145c393SAndrew Melnychenko 
12930145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data,
12940145c393SAndrew Melnychenko                                    struct EBPFRSSConfig *config)
12950145c393SAndrew Melnychenko {
12960145c393SAndrew Melnychenko     config->redirect = data->redirect;
12970145c393SAndrew Melnychenko     config->populate_hash = data->populate_hash;
12980145c393SAndrew Melnychenko     config->hash_types = data->hash_types;
12990145c393SAndrew Melnychenko     config->indirections_len = data->indirections_len;
13000145c393SAndrew Melnychenko     config->default_queue = data->default_queue;
13010145c393SAndrew Melnychenko }
13020145c393SAndrew Melnychenko 
13030145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n)
13040145c393SAndrew Melnychenko {
13050145c393SAndrew Melnychenko     struct EBPFRSSConfig config = {};
13060145c393SAndrew Melnychenko 
13070145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
13080145c393SAndrew Melnychenko         return false;
13090145c393SAndrew Melnychenko     }
13100145c393SAndrew Melnychenko 
13110145c393SAndrew Melnychenko     rss_data_to_rss_config(&n->rss_data, &config);
13120145c393SAndrew Melnychenko 
13130145c393SAndrew Melnychenko     if (!ebpf_rss_set_all(&n->ebpf_rss, &config,
13140145c393SAndrew Melnychenko                           n->rss_data.indirections_table, n->rss_data.key)) {
13150145c393SAndrew Melnychenko         return false;
13160145c393SAndrew Melnychenko     }
13170145c393SAndrew Melnychenko 
13180145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) {
13190145c393SAndrew Melnychenko         return false;
13200145c393SAndrew Melnychenko     }
13210145c393SAndrew Melnychenko 
13220145c393SAndrew Melnychenko     return true;
13230145c393SAndrew Melnychenko }
13240145c393SAndrew Melnychenko 
13250145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n)
13260145c393SAndrew Melnychenko {
13270145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
13280145c393SAndrew Melnychenko }
13290145c393SAndrew Melnychenko 
13300145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n)
13310145c393SAndrew Melnychenko {
13320145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
13330145c393SAndrew Melnychenko         /* backend does't support steering ebpf */
13340145c393SAndrew Melnychenko         return false;
13350145c393SAndrew Melnychenko     }
13360145c393SAndrew Melnychenko 
13370145c393SAndrew Melnychenko     return ebpf_rss_load(&n->ebpf_rss);
13380145c393SAndrew Melnychenko }
13390145c393SAndrew Melnychenko 
13400145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n)
13410145c393SAndrew Melnychenko {
13420145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
13430145c393SAndrew Melnychenko     ebpf_rss_unload(&n->ebpf_rss);
134459079029SYuri Benditovich }
134559079029SYuri Benditovich 
134659079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n,
1347e22f0603SYuri Benditovich                                       struct iovec *iov,
1348e22f0603SYuri Benditovich                                       unsigned int iov_cnt,
1349e22f0603SYuri Benditovich                                       bool do_rss)
135059079029SYuri Benditovich {
135159079029SYuri Benditovich     VirtIODevice *vdev = VIRTIO_DEVICE(n);
135259079029SYuri Benditovich     struct virtio_net_rss_config cfg;
135359079029SYuri Benditovich     size_t s, offset = 0, size_get;
1354441537f1SJason Wang     uint16_t queue_pairs, i;
135559079029SYuri Benditovich     struct {
135659079029SYuri Benditovich         uint16_t us;
135759079029SYuri Benditovich         uint8_t b;
135859079029SYuri Benditovich     } QEMU_PACKED temp;
135959079029SYuri Benditovich     const char *err_msg = "";
136059079029SYuri Benditovich     uint32_t err_value = 0;
136159079029SYuri Benditovich 
1362e22f0603SYuri Benditovich     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
136359079029SYuri Benditovich         err_msg = "RSS is not negotiated";
136459079029SYuri Benditovich         goto error;
136559079029SYuri Benditovich     }
1366e22f0603SYuri Benditovich     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1367e22f0603SYuri Benditovich         err_msg = "Hash report is not negotiated";
1368e22f0603SYuri Benditovich         goto error;
1369e22f0603SYuri Benditovich     }
137059079029SYuri Benditovich     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
137159079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
137259079029SYuri Benditovich     if (s != size_get) {
137359079029SYuri Benditovich         err_msg = "Short command buffer";
137459079029SYuri Benditovich         err_value = (uint32_t)s;
137559079029SYuri Benditovich         goto error;
137659079029SYuri Benditovich     }
137759079029SYuri Benditovich     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
137859079029SYuri Benditovich     n->rss_data.indirections_len =
137959079029SYuri Benditovich         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
138059079029SYuri Benditovich     n->rss_data.indirections_len++;
1381e22f0603SYuri Benditovich     if (!do_rss) {
1382e22f0603SYuri Benditovich         n->rss_data.indirections_len = 1;
1383e22f0603SYuri Benditovich     }
138459079029SYuri Benditovich     if (!is_power_of_2(n->rss_data.indirections_len)) {
138559079029SYuri Benditovich         err_msg = "Invalid size of indirection table";
138659079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
138759079029SYuri Benditovich         goto error;
138859079029SYuri Benditovich     }
138959079029SYuri Benditovich     if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
139059079029SYuri Benditovich         err_msg = "Too large indirection table";
139159079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
139259079029SYuri Benditovich         goto error;
139359079029SYuri Benditovich     }
1394e22f0603SYuri Benditovich     n->rss_data.default_queue = do_rss ?
1395e22f0603SYuri Benditovich         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1396441537f1SJason Wang     if (n->rss_data.default_queue >= n->max_queue_pairs) {
139759079029SYuri Benditovich         err_msg = "Invalid default queue";
139859079029SYuri Benditovich         err_value = n->rss_data.default_queue;
139959079029SYuri Benditovich         goto error;
140059079029SYuri Benditovich     }
140159079029SYuri Benditovich     offset += size_get;
140259079029SYuri Benditovich     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
140359079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
140459079029SYuri Benditovich     n->rss_data.indirections_table = g_malloc(size_get);
140559079029SYuri Benditovich     if (!n->rss_data.indirections_table) {
140659079029SYuri Benditovich         err_msg = "Can't allocate indirections table";
140759079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
140859079029SYuri Benditovich         goto error;
140959079029SYuri Benditovich     }
141059079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset,
141159079029SYuri Benditovich                    n->rss_data.indirections_table, size_get);
141259079029SYuri Benditovich     if (s != size_get) {
141359079029SYuri Benditovich         err_msg = "Short indirection table buffer";
141459079029SYuri Benditovich         err_value = (uint32_t)s;
141559079029SYuri Benditovich         goto error;
141659079029SYuri Benditovich     }
141759079029SYuri Benditovich     for (i = 0; i < n->rss_data.indirections_len; ++i) {
141859079029SYuri Benditovich         uint16_t val = n->rss_data.indirections_table[i];
141959079029SYuri Benditovich         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
142059079029SYuri Benditovich     }
142159079029SYuri Benditovich     offset += size_get;
142259079029SYuri Benditovich     size_get = sizeof(temp);
142359079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
142459079029SYuri Benditovich     if (s != size_get) {
1425441537f1SJason Wang         err_msg = "Can't get queue_pairs";
142659079029SYuri Benditovich         err_value = (uint32_t)s;
142759079029SYuri Benditovich         goto error;
142859079029SYuri Benditovich     }
1429441537f1SJason Wang     queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs;
1430441537f1SJason Wang     if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) {
1431441537f1SJason Wang         err_msg = "Invalid number of queue_pairs";
1432441537f1SJason Wang         err_value = queue_pairs;
143359079029SYuri Benditovich         goto error;
143459079029SYuri Benditovich     }
143559079029SYuri Benditovich     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
143659079029SYuri Benditovich         err_msg = "Invalid key size";
143759079029SYuri Benditovich         err_value = temp.b;
143859079029SYuri Benditovich         goto error;
143959079029SYuri Benditovich     }
144059079029SYuri Benditovich     if (!temp.b && n->rss_data.hash_types) {
144159079029SYuri Benditovich         err_msg = "No key provided";
144259079029SYuri Benditovich         err_value = 0;
144359079029SYuri Benditovich         goto error;
144459079029SYuri Benditovich     }
144559079029SYuri Benditovich     if (!temp.b && !n->rss_data.hash_types) {
144659079029SYuri Benditovich         virtio_net_disable_rss(n);
1447441537f1SJason Wang         return queue_pairs;
144859079029SYuri Benditovich     }
144959079029SYuri Benditovich     offset += size_get;
145059079029SYuri Benditovich     size_get = temp.b;
145159079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
145259079029SYuri Benditovich     if (s != size_get) {
145359079029SYuri Benditovich         err_msg = "Can get key buffer";
145459079029SYuri Benditovich         err_value = (uint32_t)s;
145559079029SYuri Benditovich         goto error;
145659079029SYuri Benditovich     }
145759079029SYuri Benditovich     n->rss_data.enabled = true;
14580145c393SAndrew Melnychenko 
14590145c393SAndrew Melnychenko     if (!n->rss_data.populate_hash) {
14600145c393SAndrew Melnychenko         if (!virtio_net_attach_epbf_rss(n)) {
14610145c393SAndrew Melnychenko             /* EBPF must be loaded for vhost */
14620145c393SAndrew Melnychenko             if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
14630145c393SAndrew Melnychenko                 warn_report("Can't load eBPF RSS for vhost");
14640145c393SAndrew Melnychenko                 goto error;
14650145c393SAndrew Melnychenko             }
14660145c393SAndrew Melnychenko             /* fallback to software RSS */
14670145c393SAndrew Melnychenko             warn_report("Can't load eBPF RSS - fallback to software RSS");
14680145c393SAndrew Melnychenko             n->rss_data.enabled_software_rss = true;
14690145c393SAndrew Melnychenko         }
14700145c393SAndrew Melnychenko     } else {
14710145c393SAndrew Melnychenko         /* use software RSS for hash populating */
14720145c393SAndrew Melnychenko         /* and detach eBPF if was loaded before */
14730145c393SAndrew Melnychenko         virtio_net_detach_epbf_rss(n);
14740145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = true;
14750145c393SAndrew Melnychenko     }
14760145c393SAndrew Melnychenko 
147759079029SYuri Benditovich     trace_virtio_net_rss_enable(n->rss_data.hash_types,
147859079029SYuri Benditovich                                 n->rss_data.indirections_len,
147959079029SYuri Benditovich                                 temp.b);
1480441537f1SJason Wang     return queue_pairs;
148159079029SYuri Benditovich error:
148259079029SYuri Benditovich     trace_virtio_net_rss_error(err_msg, err_value);
148359079029SYuri Benditovich     virtio_net_disable_rss(n);
148459079029SYuri Benditovich     return 0;
148559079029SYuri Benditovich }
148659079029SYuri Benditovich 
14876e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
14886e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
14896e790746SPaolo Bonzini {
149017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1491441537f1SJason Wang     uint16_t queue_pairs;
14922a7888ccSSi-Wei Liu     NetClientState *nc = qemu_get_queue(n->nic);
14936e790746SPaolo Bonzini 
149459079029SYuri Benditovich     virtio_net_disable_rss(n);
1495e22f0603SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1496441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false);
1497441537f1SJason Wang         return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1498e22f0603SYuri Benditovich     }
149959079029SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1500441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true);
150159079029SYuri Benditovich     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
150259079029SYuri Benditovich         struct virtio_net_ctrl_mq mq;
150359079029SYuri Benditovich         size_t s;
150459079029SYuri Benditovich         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
150559079029SYuri Benditovich             return VIRTIO_NET_ERR;
150659079029SYuri Benditovich         }
15076e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
15086e790746SPaolo Bonzini         if (s != sizeof(mq)) {
15096e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
15106e790746SPaolo Bonzini         }
1511441537f1SJason Wang         queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
15126e790746SPaolo Bonzini 
151359079029SYuri Benditovich     } else {
15146e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
15156e790746SPaolo Bonzini     }
15166e790746SPaolo Bonzini 
1517441537f1SJason Wang     if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1518441537f1SJason Wang         queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1519441537f1SJason Wang         queue_pairs > n->max_queue_pairs ||
15206e790746SPaolo Bonzini         !n->multiqueue) {
15216e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
15226e790746SPaolo Bonzini     }
15236e790746SPaolo Bonzini 
1524441537f1SJason Wang     n->curr_queue_pairs = queue_pairs;
1525ca8717f9SEugenio Pérez     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
1526ca8717f9SEugenio Pérez         /*
1527ca8717f9SEugenio Pérez          * Avoid updating the backend for a vdpa device: We're only interested
1528ca8717f9SEugenio Pérez          * in updating the device model queues.
1529ca8717f9SEugenio Pérez          */
1530ca8717f9SEugenio Pérez         return VIRTIO_NET_OK;
1531ca8717f9SEugenio Pérez     }
1532441537f1SJason Wang     /* stop the backend before changing the number of queue_pairs to avoid handling a
15336e790746SPaolo Bonzini      * disabled queue */
153417a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
1535441537f1SJason Wang     virtio_net_set_queue_pairs(n);
15366e790746SPaolo Bonzini 
15376e790746SPaolo Bonzini     return VIRTIO_NET_OK;
15386e790746SPaolo Bonzini }
1539ba7eadb5SGreg Kurz 
1540640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
1541640b8a1cSEugenio Pérez                                   const struct iovec *in_sg, unsigned in_num,
1542640b8a1cSEugenio Pérez                                   const struct iovec *out_sg,
1543640b8a1cSEugenio Pérez                                   unsigned out_num)
15446e790746SPaolo Bonzini {
154517a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15466e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
15476e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
15486e790746SPaolo Bonzini     size_t s;
1549771b6ed3SJason Wang     struct iovec *iov, *iov2;
1550640b8a1cSEugenio Pérez 
1551640b8a1cSEugenio Pérez     if (iov_size(in_sg, in_num) < sizeof(status) ||
1552640b8a1cSEugenio Pérez         iov_size(out_sg, out_num) < sizeof(ctrl)) {
1553640b8a1cSEugenio Pérez         virtio_error(vdev, "virtio-net ctrl missing headers");
1554640b8a1cSEugenio Pérez         return 0;
1555640b8a1cSEugenio Pérez     }
1556640b8a1cSEugenio Pérez 
1557640b8a1cSEugenio Pérez     iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num);
1558640b8a1cSEugenio Pérez     s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl));
1559640b8a1cSEugenio Pérez     iov_discard_front(&iov, &out_num, sizeof(ctrl));
1560640b8a1cSEugenio Pérez     if (s != sizeof(ctrl)) {
1561640b8a1cSEugenio Pérez         status = VIRTIO_NET_ERR;
1562640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1563640b8a1cSEugenio Pérez         status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num);
1564640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1565640b8a1cSEugenio Pérez         status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num);
1566640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1567640b8a1cSEugenio Pérez         status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num);
1568640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1569640b8a1cSEugenio Pérez         status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num);
1570640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1571640b8a1cSEugenio Pérez         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
1572640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1573640b8a1cSEugenio Pérez         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
1574640b8a1cSEugenio Pérez     }
1575640b8a1cSEugenio Pérez 
1576640b8a1cSEugenio Pérez     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
1577640b8a1cSEugenio Pérez     assert(s == sizeof(status));
1578640b8a1cSEugenio Pérez 
1579640b8a1cSEugenio Pérez     g_free(iov2);
1580640b8a1cSEugenio Pérez     return sizeof(status);
1581640b8a1cSEugenio Pérez }
1582640b8a1cSEugenio Pérez 
1583640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1584640b8a1cSEugenio Pérez {
1585640b8a1cSEugenio Pérez     VirtQueueElement *elem;
15866e790746SPaolo Bonzini 
158751b19ebeSPaolo Bonzini     for (;;) {
1588640b8a1cSEugenio Pérez         size_t written;
158951b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
159051b19ebeSPaolo Bonzini         if (!elem) {
159151b19ebeSPaolo Bonzini             break;
159251b19ebeSPaolo Bonzini         }
1593640b8a1cSEugenio Pérez 
1594640b8a1cSEugenio Pérez         written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num,
1595640b8a1cSEugenio Pérez                                              elem->out_sg, elem->out_num);
1596640b8a1cSEugenio Pérez         if (written > 0) {
1597640b8a1cSEugenio Pérez             virtqueue_push(vq, elem, written);
1598640b8a1cSEugenio Pérez             virtio_notify(vdev, vq);
1599640b8a1cSEugenio Pérez             g_free(elem);
1600640b8a1cSEugenio Pérez         } else {
1601ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
1602ba7eadb5SGreg Kurz             g_free(elem);
1603ba7eadb5SGreg Kurz             break;
16046e790746SPaolo Bonzini         }
16056e790746SPaolo Bonzini     }
16066e790746SPaolo Bonzini }
16076e790746SPaolo Bonzini 
16086e790746SPaolo Bonzini /* RX */
16096e790746SPaolo Bonzini 
16106e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
16116e790746SPaolo Bonzini {
161217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
16136e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
16146e790746SPaolo Bonzini 
16156e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
16166e790746SPaolo Bonzini }
16176e790746SPaolo Bonzini 
1618b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc)
16196e790746SPaolo Bonzini {
16206e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
162117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
16226e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
16236e790746SPaolo Bonzini 
162417a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
1625b8c4b67eSPhilippe Mathieu-Daudé         return false;
16266e790746SPaolo Bonzini     }
16276e790746SPaolo Bonzini 
1628441537f1SJason Wang     if (nc->queue_index >= n->curr_queue_pairs) {
1629b8c4b67eSPhilippe Mathieu-Daudé         return false;
16306e790746SPaolo Bonzini     }
16316e790746SPaolo Bonzini 
16326e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
163317a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1634b8c4b67eSPhilippe Mathieu-Daudé         return false;
16356e790746SPaolo Bonzini     }
16366e790746SPaolo Bonzini 
1637b8c4b67eSPhilippe Mathieu-Daudé     return true;
16386e790746SPaolo Bonzini }
16396e790746SPaolo Bonzini 
16406e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
16416e790746SPaolo Bonzini {
16426e790746SPaolo Bonzini     VirtIONet *n = q->n;
16436e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
16446e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
16456e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
16466e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
16476e790746SPaolo Bonzini 
16486e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
16496e790746SPaolo Bonzini          * available after the above check but before notification was
16506e790746SPaolo Bonzini          * enabled, check for available buffers again.
16516e790746SPaolo Bonzini          */
16526e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
16536e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
16546e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
16556e790746SPaolo Bonzini             return 0;
16566e790746SPaolo Bonzini         }
16576e790746SPaolo Bonzini     }
16586e790746SPaolo Bonzini 
16596e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
16606e790746SPaolo Bonzini     return 1;
16616e790746SPaolo Bonzini }
16626e790746SPaolo Bonzini 
16631399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1664032a74a1SCédric Le Goater {
16651399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
16661399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
16671399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
16681399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1669032a74a1SCédric Le Goater }
1670032a74a1SCédric Le Goater 
16716e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
16726e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
16736e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
16746e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
16756e790746SPaolo Bonzini  * dhclient yet.
16766e790746SPaolo Bonzini  *
16776e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
16786e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
16796e790746SPaolo Bonzini  * kernels.
16806e790746SPaolo Bonzini  *
16816e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
16826e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
16836e790746SPaolo Bonzini  * cache.
16846e790746SPaolo Bonzini  */
16856e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
16866e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
16876e790746SPaolo Bonzini {
16886e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
16896e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
16906e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
16916e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
16926e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1693f5746335SBin Meng         net_checksum_calculate(buf, size, CSUM_UDP);
16946e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
16956e790746SPaolo Bonzini     }
16966e790746SPaolo Bonzini }
16976e790746SPaolo Bonzini 
16986e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
16996e790746SPaolo Bonzini                            const void *buf, size_t size)
17006e790746SPaolo Bonzini {
17016e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
17026e790746SPaolo Bonzini         /* FIXME this cast is evil */
17036e790746SPaolo Bonzini         void *wbuf = (void *)buf;
17046e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
17056e790746SPaolo Bonzini                                     size - n->host_hdr_len);
17061bfa316cSGreg Kurz 
17071bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
17081399c60dSRusty Russell             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
17091bfa316cSGreg Kurz         }
17106e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
17116e790746SPaolo Bonzini     } else {
17126e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
17136e790746SPaolo Bonzini             .flags = 0,
17146e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
17156e790746SPaolo Bonzini         };
17166e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
17176e790746SPaolo Bonzini     }
17186e790746SPaolo Bonzini }
17196e790746SPaolo Bonzini 
17206e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
17216e790746SPaolo Bonzini {
17226e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
17236e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
17246e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
17256e790746SPaolo Bonzini     int i;
17266e790746SPaolo Bonzini 
17276e790746SPaolo Bonzini     if (n->promisc)
17286e790746SPaolo Bonzini         return 1;
17296e790746SPaolo Bonzini 
17306e790746SPaolo Bonzini     ptr += n->host_hdr_len;
17316e790746SPaolo Bonzini 
17326e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
17337542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
17346e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
17356e790746SPaolo Bonzini             return 0;
17366e790746SPaolo Bonzini     }
17376e790746SPaolo Bonzini 
17386e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
17396e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
17406e790746SPaolo Bonzini             return !n->nobcast;
17416e790746SPaolo Bonzini         } else if (n->nomulti) {
17426e790746SPaolo Bonzini             return 0;
17436e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
17446e790746SPaolo Bonzini             return 1;
17456e790746SPaolo Bonzini         }
17466e790746SPaolo Bonzini 
17476e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
17486e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17496e790746SPaolo Bonzini                 return 1;
17506e790746SPaolo Bonzini             }
17516e790746SPaolo Bonzini         }
17526e790746SPaolo Bonzini     } else { // unicast
17536e790746SPaolo Bonzini         if (n->nouni) {
17546e790746SPaolo Bonzini             return 0;
17556e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
17566e790746SPaolo Bonzini             return 1;
17576e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
17586e790746SPaolo Bonzini             return 1;
17596e790746SPaolo Bonzini         }
17606e790746SPaolo Bonzini 
17616e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
17626e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17636e790746SPaolo Bonzini                 return 1;
17646e790746SPaolo Bonzini             }
17656e790746SPaolo Bonzini         }
17666e790746SPaolo Bonzini     }
17676e790746SPaolo Bonzini 
17686e790746SPaolo Bonzini     return 0;
17696e790746SPaolo Bonzini }
17706e790746SPaolo Bonzini 
177169ff5ef8SAkihiko Odaki static uint8_t virtio_net_get_hash_type(bool hasip4,
177269ff5ef8SAkihiko Odaki                                         bool hasip6,
177365f474bbSAkihiko Odaki                                         EthL4HdrProto l4hdr_proto,
17744474e37aSYuri Benditovich                                         uint32_t types)
17754474e37aSYuri Benditovich {
177669ff5ef8SAkihiko Odaki     if (hasip4) {
177765f474bbSAkihiko Odaki         switch (l4hdr_proto) {
177865f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
177965f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
17804474e37aSYuri Benditovich                 return NetPktRssIpV4Tcp;
17814474e37aSYuri Benditovich             }
178265f474bbSAkihiko Odaki             break;
178365f474bbSAkihiko Odaki 
178465f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
178565f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
17864474e37aSYuri Benditovich                 return NetPktRssIpV4Udp;
17874474e37aSYuri Benditovich             }
178865f474bbSAkihiko Odaki             break;
178965f474bbSAkihiko Odaki 
179065f474bbSAkihiko Odaki         default:
179165f474bbSAkihiko Odaki             break;
179265f474bbSAkihiko Odaki         }
179365f474bbSAkihiko Odaki 
17944474e37aSYuri Benditovich         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
17954474e37aSYuri Benditovich             return NetPktRssIpV4;
17964474e37aSYuri Benditovich         }
179769ff5ef8SAkihiko Odaki     } else if (hasip6) {
179865f474bbSAkihiko Odaki         switch (l4hdr_proto) {
179965f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
180065f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) {
180165f474bbSAkihiko Odaki                 return NetPktRssIpV6TcpEx;
180265f474bbSAkihiko Odaki             }
180365f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
180465f474bbSAkihiko Odaki                 return NetPktRssIpV6Tcp;
180565f474bbSAkihiko Odaki             }
180665f474bbSAkihiko Odaki             break;
18074474e37aSYuri Benditovich 
180865f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
180965f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) {
181065f474bbSAkihiko Odaki                 return NetPktRssIpV6UdpEx;
18114474e37aSYuri Benditovich             }
181265f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
181365f474bbSAkihiko Odaki                 return NetPktRssIpV6Udp;
18144474e37aSYuri Benditovich             }
181565f474bbSAkihiko Odaki             break;
181665f474bbSAkihiko Odaki 
181765f474bbSAkihiko Odaki         default:
181865f474bbSAkihiko Odaki             break;
181965f474bbSAkihiko Odaki         }
182065f474bbSAkihiko Odaki 
182165f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) {
182265f474bbSAkihiko Odaki             return NetPktRssIpV6Ex;
182365f474bbSAkihiko Odaki         }
182465f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
182565f474bbSAkihiko Odaki             return NetPktRssIpV6;
18264474e37aSYuri Benditovich         }
18274474e37aSYuri Benditovich     }
18284474e37aSYuri Benditovich     return 0xff;
18294474e37aSYuri Benditovich }
18304474e37aSYuri Benditovich 
1831e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1832e22f0603SYuri Benditovich                                    uint32_t hash)
1833e22f0603SYuri Benditovich {
1834e22f0603SYuri Benditovich     struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1835e22f0603SYuri Benditovich     hdr->hash_value = hash;
1836e22f0603SYuri Benditovich     hdr->hash_report = report;
1837e22f0603SYuri Benditovich }
1838e22f0603SYuri Benditovich 
18394474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
184097cd965cSPaolo Bonzini                                   size_t size)
18416e790746SPaolo Bonzini {
18426e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1843e22f0603SYuri Benditovich     unsigned int index = nc->queue_index, new_index = index;
18444474e37aSYuri Benditovich     struct NetRxPkt *pkt = n->rx_pkt;
18454474e37aSYuri Benditovich     uint8_t net_hash_type;
18464474e37aSYuri Benditovich     uint32_t hash;
184765f474bbSAkihiko Odaki     bool hasip4, hasip6;
184865f474bbSAkihiko Odaki     EthL4HdrProto l4hdr_proto;
1849e22f0603SYuri Benditovich     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1850e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv4,
1851e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv4,
1852e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6,
1853e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6,
1854e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1855e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1856e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv4,
1857e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6,
1858e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1859e22f0603SYuri Benditovich     };
18602f0fa232SAkihiko Odaki     struct iovec iov = {
18612f0fa232SAkihiko Odaki         .iov_base = (void *)buf,
18622f0fa232SAkihiko Odaki         .iov_len = size
18632f0fa232SAkihiko Odaki     };
18644474e37aSYuri Benditovich 
18652f0fa232SAkihiko Odaki     net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len);
186665f474bbSAkihiko Odaki     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
186765f474bbSAkihiko Odaki     net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto,
18684474e37aSYuri Benditovich                                              n->rss_data.hash_types);
18694474e37aSYuri Benditovich     if (net_hash_type > NetPktRssIpV6UdpEx) {
1870e22f0603SYuri Benditovich         if (n->rss_data.populate_hash) {
1871e22f0603SYuri Benditovich             virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1872e22f0603SYuri Benditovich         }
1873e22f0603SYuri Benditovich         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
18744474e37aSYuri Benditovich     }
18754474e37aSYuri Benditovich 
18764474e37aSYuri Benditovich     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1877e22f0603SYuri Benditovich 
1878e22f0603SYuri Benditovich     if (n->rss_data.populate_hash) {
1879e22f0603SYuri Benditovich         virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1880e22f0603SYuri Benditovich     }
1881e22f0603SYuri Benditovich 
1882e22f0603SYuri Benditovich     if (n->rss_data.redirect) {
18834474e37aSYuri Benditovich         new_index = hash & (n->rss_data.indirections_len - 1);
18844474e37aSYuri Benditovich         new_index = n->rss_data.indirections_table[new_index];
18854474e37aSYuri Benditovich     }
1886e22f0603SYuri Benditovich 
1887e22f0603SYuri Benditovich     return (index == new_index) ? -1 : new_index;
18884474e37aSYuri Benditovich }
18894474e37aSYuri Benditovich 
18904474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
18914474e37aSYuri Benditovich                                       size_t size, bool no_rss)
18924474e37aSYuri Benditovich {
18934474e37aSYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
18946e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
189517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1896bedd7e93SJason Wang     VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE];
1897bedd7e93SJason Wang     size_t lens[VIRTQUEUE_MAX_SIZE];
18986e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
18996e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
19006e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
1901bedd7e93SJason Wang     size_t offset, i, guest_offset, j;
1902bedd7e93SJason Wang     ssize_t err;
19036e790746SPaolo Bonzini 
19046e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
19056e790746SPaolo Bonzini         return -1;
19066e790746SPaolo Bonzini     }
19076e790746SPaolo Bonzini 
19080145c393SAndrew Melnychenko     if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) {
19094474e37aSYuri Benditovich         int index = virtio_net_process_rss(nc, buf, size);
19104474e37aSYuri Benditovich         if (index >= 0) {
19114474e37aSYuri Benditovich             NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
19124474e37aSYuri Benditovich             return virtio_net_receive_rcu(nc2, buf, size, true);
19134474e37aSYuri Benditovich         }
19144474e37aSYuri Benditovich     }
19154474e37aSYuri Benditovich 
19166e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
19176e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
19186e790746SPaolo Bonzini         return 0;
19196e790746SPaolo Bonzini     }
19206e790746SPaolo Bonzini 
19216e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
19226e790746SPaolo Bonzini         return size;
19236e790746SPaolo Bonzini 
19246e790746SPaolo Bonzini     offset = i = 0;
19256e790746SPaolo Bonzini 
19266e790746SPaolo Bonzini     while (offset < size) {
192751b19ebeSPaolo Bonzini         VirtQueueElement *elem;
19286e790746SPaolo Bonzini         int len, total;
192951b19ebeSPaolo Bonzini         const struct iovec *sg;
19306e790746SPaolo Bonzini 
19316e790746SPaolo Bonzini         total = 0;
19326e790746SPaolo Bonzini 
1933bedd7e93SJason Wang         if (i == VIRTQUEUE_MAX_SIZE) {
1934bedd7e93SJason Wang             virtio_error(vdev, "virtio-net unexpected long buffer chain");
1935bedd7e93SJason Wang             err = size;
1936bedd7e93SJason Wang             goto err;
1937bedd7e93SJason Wang         }
1938bedd7e93SJason Wang 
193951b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
194051b19ebeSPaolo Bonzini         if (!elem) {
1941ba10b9c0SGreg Kurz             if (i) {
1942ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
19436e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1944019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1945019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
19466e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1947019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1948019a3edbSGerd Hoffmann                              vdev->guest_features);
1949ba10b9c0SGreg Kurz             }
1950bedd7e93SJason Wang             err = -1;
1951bedd7e93SJason Wang             goto err;
19526e790746SPaolo Bonzini         }
19536e790746SPaolo Bonzini 
195451b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1955ba10b9c0SGreg Kurz             virtio_error(vdev,
1956ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1957ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1958ba10b9c0SGreg Kurz             g_free(elem);
1959bedd7e93SJason Wang             err = -1;
1960bedd7e93SJason Wang             goto err;
19616e790746SPaolo Bonzini         }
19626e790746SPaolo Bonzini 
196351b19ebeSPaolo Bonzini         sg = elem->in_sg;
19646e790746SPaolo Bonzini         if (i == 0) {
19656e790746SPaolo Bonzini             assert(offset == 0);
19666e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
19676e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
196851b19ebeSPaolo Bonzini                                     sg, elem->in_num,
19696e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
19706e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
19716e790746SPaolo Bonzini             }
19726e790746SPaolo Bonzini 
197351b19ebeSPaolo Bonzini             receive_header(n, sg, elem->in_num, buf, size);
1974e22f0603SYuri Benditovich             if (n->rss_data.populate_hash) {
1975e22f0603SYuri Benditovich                 offset = sizeof(mhdr);
1976e22f0603SYuri Benditovich                 iov_from_buf(sg, elem->in_num, offset,
1977e22f0603SYuri Benditovich                              buf + offset, n->host_hdr_len - sizeof(mhdr));
1978e22f0603SYuri Benditovich             }
19796e790746SPaolo Bonzini             offset = n->host_hdr_len;
19806e790746SPaolo Bonzini             total += n->guest_hdr_len;
19816e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
19826e790746SPaolo Bonzini         } else {
19836e790746SPaolo Bonzini             guest_offset = 0;
19846e790746SPaolo Bonzini         }
19856e790746SPaolo Bonzini 
19866e790746SPaolo Bonzini         /* copy in packet.  ugh */
198751b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
19886e790746SPaolo Bonzini                            buf + offset, size - offset);
19896e790746SPaolo Bonzini         total += len;
19906e790746SPaolo Bonzini         offset += len;
19916e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
19926e790746SPaolo Bonzini          * must have consumed the complete packet.
19936e790746SPaolo Bonzini          * Otherwise, drop it. */
19946e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
199527e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
199651b19ebeSPaolo Bonzini             g_free(elem);
1997bedd7e93SJason Wang             err = size;
1998bedd7e93SJason Wang             goto err;
19996e790746SPaolo Bonzini         }
20006e790746SPaolo Bonzini 
2001bedd7e93SJason Wang         elems[i] = elem;
2002bedd7e93SJason Wang         lens[i] = total;
2003bedd7e93SJason Wang         i++;
20046e790746SPaolo Bonzini     }
20056e790746SPaolo Bonzini 
20066e790746SPaolo Bonzini     if (mhdr_cnt) {
20071399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
20086e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
20096e790746SPaolo Bonzini                      0,
20106e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
20116e790746SPaolo Bonzini     }
20126e790746SPaolo Bonzini 
2013bedd7e93SJason Wang     for (j = 0; j < i; j++) {
2014bedd7e93SJason Wang         /* signal other side */
2015bedd7e93SJason Wang         virtqueue_fill(q->rx_vq, elems[j], lens[j], j);
2016bedd7e93SJason Wang         g_free(elems[j]);
2017bedd7e93SJason Wang     }
2018bedd7e93SJason Wang 
20196e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
202017a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
20216e790746SPaolo Bonzini 
20226e790746SPaolo Bonzini     return size;
2023bedd7e93SJason Wang 
2024bedd7e93SJason Wang err:
2025bedd7e93SJason Wang     for (j = 0; j < i; j++) {
2026abe300d9SJason Wang         virtqueue_detach_element(q->rx_vq, elems[j], lens[j]);
2027bedd7e93SJason Wang         g_free(elems[j]);
2028bedd7e93SJason Wang     }
2029bedd7e93SJason Wang 
2030bedd7e93SJason Wang     return err;
20316e790746SPaolo Bonzini }
20326e790746SPaolo Bonzini 
20332974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
203497cd965cSPaolo Bonzini                                   size_t size)
203597cd965cSPaolo Bonzini {
2036068ddfa9SDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
203797cd965cSPaolo Bonzini 
20384474e37aSYuri Benditovich     return virtio_net_receive_rcu(nc, buf, size, false);
203997cd965cSPaolo Bonzini }
204097cd965cSPaolo Bonzini 
20412974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
20422974e916SYuri Benditovich                                          const uint8_t *buf,
20432974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20442974e916SYuri Benditovich {
20452974e916SYuri Benditovich     uint16_t ip_hdrlen;
20462974e916SYuri Benditovich     struct ip_header *ip;
20472974e916SYuri Benditovich 
20482974e916SYuri Benditovich     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
20492974e916SYuri Benditovich                               + sizeof(struct eth_header));
20502974e916SYuri Benditovich     unit->ip = (void *)ip;
20512974e916SYuri Benditovich     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
20522974e916SYuri Benditovich     unit->ip_plen = &ip->ip_len;
20532974e916SYuri Benditovich     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
20542974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
20552974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
20562974e916SYuri Benditovich }
20572974e916SYuri Benditovich 
20582974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
20592974e916SYuri Benditovich                                          const uint8_t *buf,
20602974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20612974e916SYuri Benditovich {
20622974e916SYuri Benditovich     struct ip6_header *ip6;
20632974e916SYuri Benditovich 
20642974e916SYuri Benditovich     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
20652974e916SYuri Benditovich                                  + sizeof(struct eth_header));
20662974e916SYuri Benditovich     unit->ip = ip6;
20672974e916SYuri Benditovich     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
206878ee6bd0SPhilippe Mathieu-Daudé     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
20692974e916SYuri Benditovich                                         + sizeof(struct ip6_header));
20702974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
20712974e916SYuri Benditovich 
20722974e916SYuri Benditovich     /* There is a difference between payload lenght in ipv4 and v6,
20732974e916SYuri Benditovich        ip header is excluded in ipv6 */
20742974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
20752974e916SYuri Benditovich }
20762974e916SYuri Benditovich 
20772974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
20782974e916SYuri Benditovich                                        VirtioNetRscSeg *seg)
20792974e916SYuri Benditovich {
20802974e916SYuri Benditovich     int ret;
2081dd3d85e8SYuri Benditovich     struct virtio_net_hdr_v1 *h;
20822974e916SYuri Benditovich 
2083dd3d85e8SYuri Benditovich     h = (struct virtio_net_hdr_v1 *)seg->buf;
20842974e916SYuri Benditovich     h->flags = 0;
20852974e916SYuri Benditovich     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
20862974e916SYuri Benditovich 
20872974e916SYuri Benditovich     if (seg->is_coalesced) {
2088dd3d85e8SYuri Benditovich         h->rsc.segments = seg->packets;
2089dd3d85e8SYuri Benditovich         h->rsc.dup_acks = seg->dup_ack;
20902974e916SYuri Benditovich         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
20912974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
20922974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
20932974e916SYuri Benditovich         } else {
20942974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
20952974e916SYuri Benditovich         }
20962974e916SYuri Benditovich     }
20972974e916SYuri Benditovich 
20982974e916SYuri Benditovich     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
20992974e916SYuri Benditovich     QTAILQ_REMOVE(&chain->buffers, seg, next);
21002974e916SYuri Benditovich     g_free(seg->buf);
21012974e916SYuri Benditovich     g_free(seg);
21022974e916SYuri Benditovich 
21032974e916SYuri Benditovich     return ret;
21042974e916SYuri Benditovich }
21052974e916SYuri Benditovich 
21062974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq)
21072974e916SYuri Benditovich {
21082974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn;
21092974e916SYuri Benditovich     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
21102974e916SYuri Benditovich 
21112974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
21122974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
21132974e916SYuri Benditovich             chain->stat.purge_failed++;
21142974e916SYuri Benditovich             continue;
21152974e916SYuri Benditovich         }
21162974e916SYuri Benditovich     }
21172974e916SYuri Benditovich 
21182974e916SYuri Benditovich     chain->stat.timer++;
21192974e916SYuri Benditovich     if (!QTAILQ_EMPTY(&chain->buffers)) {
21202974e916SYuri Benditovich         timer_mod(chain->drain_timer,
21212974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
21222974e916SYuri Benditovich     }
21232974e916SYuri Benditovich }
21242974e916SYuri Benditovich 
21252974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n)
21262974e916SYuri Benditovich {
21272974e916SYuri Benditovich     VirtioNetRscChain *chain, *rn_chain;
21282974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn_seg;
21292974e916SYuri Benditovich 
21302974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
21312974e916SYuri Benditovich         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
21322974e916SYuri Benditovich             QTAILQ_REMOVE(&chain->buffers, seg, next);
21332974e916SYuri Benditovich             g_free(seg->buf);
21342974e916SYuri Benditovich             g_free(seg);
21352974e916SYuri Benditovich         }
21362974e916SYuri Benditovich 
21372974e916SYuri Benditovich         timer_free(chain->drain_timer);
21382974e916SYuri Benditovich         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
21392974e916SYuri Benditovich         g_free(chain);
21402974e916SYuri Benditovich     }
21412974e916SYuri Benditovich }
21422974e916SYuri Benditovich 
21432974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
21442974e916SYuri Benditovich                                      NetClientState *nc,
21452974e916SYuri Benditovich                                      const uint8_t *buf, size_t size)
21462974e916SYuri Benditovich {
21472974e916SYuri Benditovich     uint16_t hdr_len;
21482974e916SYuri Benditovich     VirtioNetRscSeg *seg;
21492974e916SYuri Benditovich 
21502974e916SYuri Benditovich     hdr_len = chain->n->guest_hdr_len;
2151b21e2380SMarkus Armbruster     seg = g_new(VirtioNetRscSeg, 1);
21522974e916SYuri Benditovich     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
21532974e916SYuri Benditovich         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
21542974e916SYuri Benditovich     memcpy(seg->buf, buf, size);
21552974e916SYuri Benditovich     seg->size = size;
21562974e916SYuri Benditovich     seg->packets = 1;
21572974e916SYuri Benditovich     seg->dup_ack = 0;
21582974e916SYuri Benditovich     seg->is_coalesced = 0;
21592974e916SYuri Benditovich     seg->nc = nc;
21602974e916SYuri Benditovich 
21612974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
21622974e916SYuri Benditovich     chain->stat.cache++;
21632974e916SYuri Benditovich 
21642974e916SYuri Benditovich     switch (chain->proto) {
21652974e916SYuri Benditovich     case ETH_P_IP:
21662974e916SYuri Benditovich         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
21672974e916SYuri Benditovich         break;
21682974e916SYuri Benditovich     case ETH_P_IPV6:
21692974e916SYuri Benditovich         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
21702974e916SYuri Benditovich         break;
21712974e916SYuri Benditovich     default:
21722974e916SYuri Benditovich         g_assert_not_reached();
21732974e916SYuri Benditovich     }
21742974e916SYuri Benditovich }
21752974e916SYuri Benditovich 
21762974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
21772974e916SYuri Benditovich                                          VirtioNetRscSeg *seg,
21782974e916SYuri Benditovich                                          const uint8_t *buf,
21792974e916SYuri Benditovich                                          struct tcp_header *n_tcp,
21802974e916SYuri Benditovich                                          struct tcp_header *o_tcp)
21812974e916SYuri Benditovich {
21822974e916SYuri Benditovich     uint32_t nack, oack;
21832974e916SYuri Benditovich     uint16_t nwin, owin;
21842974e916SYuri Benditovich 
21852974e916SYuri Benditovich     nack = htonl(n_tcp->th_ack);
21862974e916SYuri Benditovich     nwin = htons(n_tcp->th_win);
21872974e916SYuri Benditovich     oack = htonl(o_tcp->th_ack);
21882974e916SYuri Benditovich     owin = htons(o_tcp->th_win);
21892974e916SYuri Benditovich 
21902974e916SYuri Benditovich     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
21912974e916SYuri Benditovich         chain->stat.ack_out_of_win++;
21922974e916SYuri Benditovich         return RSC_FINAL;
21932974e916SYuri Benditovich     } else if (nack == oack) {
21942974e916SYuri Benditovich         /* duplicated ack or window probe */
21952974e916SYuri Benditovich         if (nwin == owin) {
21962974e916SYuri Benditovich             /* duplicated ack, add dup ack count due to whql test up to 1 */
21972974e916SYuri Benditovich             chain->stat.dup_ack++;
21982974e916SYuri Benditovich             return RSC_FINAL;
21992974e916SYuri Benditovich         } else {
22002974e916SYuri Benditovich             /* Coalesce window update */
22012974e916SYuri Benditovich             o_tcp->th_win = n_tcp->th_win;
22022974e916SYuri Benditovich             chain->stat.win_update++;
22032974e916SYuri Benditovich             return RSC_COALESCE;
22042974e916SYuri Benditovich         }
22052974e916SYuri Benditovich     } else {
22062974e916SYuri Benditovich         /* pure ack, go to 'C', finalize*/
22072974e916SYuri Benditovich         chain->stat.pure_ack++;
22082974e916SYuri Benditovich         return RSC_FINAL;
22092974e916SYuri Benditovich     }
22102974e916SYuri Benditovich }
22112974e916SYuri Benditovich 
22122974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
22132974e916SYuri Benditovich                                             VirtioNetRscSeg *seg,
22142974e916SYuri Benditovich                                             const uint8_t *buf,
22152974e916SYuri Benditovich                                             VirtioNetRscUnit *n_unit)
22162974e916SYuri Benditovich {
22172974e916SYuri Benditovich     void *data;
22182974e916SYuri Benditovich     uint16_t o_ip_len;
22192974e916SYuri Benditovich     uint32_t nseq, oseq;
22202974e916SYuri Benditovich     VirtioNetRscUnit *o_unit;
22212974e916SYuri Benditovich 
22222974e916SYuri Benditovich     o_unit = &seg->unit;
22232974e916SYuri Benditovich     o_ip_len = htons(*o_unit->ip_plen);
22242974e916SYuri Benditovich     nseq = htonl(n_unit->tcp->th_seq);
22252974e916SYuri Benditovich     oseq = htonl(o_unit->tcp->th_seq);
22262974e916SYuri Benditovich 
22272974e916SYuri Benditovich     /* out of order or retransmitted. */
22282974e916SYuri Benditovich     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
22292974e916SYuri Benditovich         chain->stat.data_out_of_win++;
22302974e916SYuri Benditovich         return RSC_FINAL;
22312974e916SYuri Benditovich     }
22322974e916SYuri Benditovich 
22332974e916SYuri Benditovich     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
22342974e916SYuri Benditovich     if (nseq == oseq) {
22352974e916SYuri Benditovich         if ((o_unit->payload == 0) && n_unit->payload) {
22362974e916SYuri Benditovich             /* From no payload to payload, normal case, not a dup ack or etc */
22372974e916SYuri Benditovich             chain->stat.data_after_pure_ack++;
22382974e916SYuri Benditovich             goto coalesce;
22392974e916SYuri Benditovich         } else {
22402974e916SYuri Benditovich             return virtio_net_rsc_handle_ack(chain, seg, buf,
22412974e916SYuri Benditovich                                              n_unit->tcp, o_unit->tcp);
22422974e916SYuri Benditovich         }
22432974e916SYuri Benditovich     } else if ((nseq - oseq) != o_unit->payload) {
22442974e916SYuri Benditovich         /* Not a consistent packet, out of order */
22452974e916SYuri Benditovich         chain->stat.data_out_of_order++;
22462974e916SYuri Benditovich         return RSC_FINAL;
22472974e916SYuri Benditovich     } else {
22482974e916SYuri Benditovich coalesce:
22492974e916SYuri Benditovich         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
22502974e916SYuri Benditovich             chain->stat.over_size++;
22512974e916SYuri Benditovich             return RSC_FINAL;
22522974e916SYuri Benditovich         }
22532974e916SYuri Benditovich 
22542974e916SYuri Benditovich         /* Here comes the right data, the payload length in v4/v6 is different,
22552974e916SYuri Benditovich            so use the field value to update and record the new data len */
22562974e916SYuri Benditovich         o_unit->payload += n_unit->payload; /* update new data len */
22572974e916SYuri Benditovich 
22582974e916SYuri Benditovich         /* update field in ip header */
22592974e916SYuri Benditovich         *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
22602974e916SYuri Benditovich 
22612974e916SYuri Benditovich         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
22622974e916SYuri Benditovich            for windows guest, while this may change the behavior for linux
22632974e916SYuri Benditovich            guest (only if it uses RSC feature). */
22642974e916SYuri Benditovich         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
22652974e916SYuri Benditovich 
22662974e916SYuri Benditovich         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
22672974e916SYuri Benditovich         o_unit->tcp->th_win = n_unit->tcp->th_win;
22682974e916SYuri Benditovich 
22692974e916SYuri Benditovich         memmove(seg->buf + seg->size, data, n_unit->payload);
22702974e916SYuri Benditovich         seg->size += n_unit->payload;
22712974e916SYuri Benditovich         seg->packets++;
22722974e916SYuri Benditovich         chain->stat.coalesced++;
22732974e916SYuri Benditovich         return RSC_COALESCE;
22742974e916SYuri Benditovich     }
22752974e916SYuri Benditovich }
22762974e916SYuri Benditovich 
22772974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
22782974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
22792974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
22802974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
22812974e916SYuri Benditovich {
22822974e916SYuri Benditovich     struct ip_header *ip1, *ip2;
22832974e916SYuri Benditovich 
22842974e916SYuri Benditovich     ip1 = (struct ip_header *)(unit->ip);
22852974e916SYuri Benditovich     ip2 = (struct ip_header *)(seg->unit.ip);
22862974e916SYuri Benditovich     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
22872974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
22882974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
22892974e916SYuri Benditovich         chain->stat.no_match++;
22902974e916SYuri Benditovich         return RSC_NO_MATCH;
22912974e916SYuri Benditovich     }
22922974e916SYuri Benditovich 
22932974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
22942974e916SYuri Benditovich }
22952974e916SYuri Benditovich 
22962974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
22972974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
22982974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
22992974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
23002974e916SYuri Benditovich {
23012974e916SYuri Benditovich     struct ip6_header *ip1, *ip2;
23022974e916SYuri Benditovich 
23032974e916SYuri Benditovich     ip1 = (struct ip6_header *)(unit->ip);
23042974e916SYuri Benditovich     ip2 = (struct ip6_header *)(seg->unit.ip);
23052974e916SYuri Benditovich     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
23062974e916SYuri Benditovich         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
23072974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
23082974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
23092974e916SYuri Benditovich             chain->stat.no_match++;
23102974e916SYuri Benditovich             return RSC_NO_MATCH;
23112974e916SYuri Benditovich     }
23122974e916SYuri Benditovich 
23132974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
23142974e916SYuri Benditovich }
23152974e916SYuri Benditovich 
23162974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain
23172974e916SYuri Benditovich  * to prevent out of order */
23182974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
23192974e916SYuri Benditovich                                          struct tcp_header *tcp)
23202974e916SYuri Benditovich {
23212974e916SYuri Benditovich     uint16_t tcp_hdr;
23222974e916SYuri Benditovich     uint16_t tcp_flag;
23232974e916SYuri Benditovich 
23242974e916SYuri Benditovich     tcp_flag = htons(tcp->th_offset_flags);
23252974e916SYuri Benditovich     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
23262974e916SYuri Benditovich     tcp_flag &= VIRTIO_NET_TCP_FLAG;
23272974e916SYuri Benditovich     if (tcp_flag & TH_SYN) {
23282974e916SYuri Benditovich         chain->stat.tcp_syn++;
23292974e916SYuri Benditovich         return RSC_BYPASS;
23302974e916SYuri Benditovich     }
23312974e916SYuri Benditovich 
23322974e916SYuri Benditovich     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
23332974e916SYuri Benditovich         chain->stat.tcp_ctrl_drain++;
23342974e916SYuri Benditovich         return RSC_FINAL;
23352974e916SYuri Benditovich     }
23362974e916SYuri Benditovich 
23372974e916SYuri Benditovich     if (tcp_hdr > sizeof(struct tcp_header)) {
23382974e916SYuri Benditovich         chain->stat.tcp_all_opt++;
23392974e916SYuri Benditovich         return RSC_FINAL;
23402974e916SYuri Benditovich     }
23412974e916SYuri Benditovich 
23422974e916SYuri Benditovich     return RSC_CANDIDATE;
23432974e916SYuri Benditovich }
23442974e916SYuri Benditovich 
23452974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
23462974e916SYuri Benditovich                                          NetClientState *nc,
23472974e916SYuri Benditovich                                          const uint8_t *buf, size_t size,
23482974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
23492974e916SYuri Benditovich {
23502974e916SYuri Benditovich     int ret;
23512974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
23522974e916SYuri Benditovich 
23532974e916SYuri Benditovich     if (QTAILQ_EMPTY(&chain->buffers)) {
23542974e916SYuri Benditovich         chain->stat.empty_cache++;
23552974e916SYuri Benditovich         virtio_net_rsc_cache_buf(chain, nc, buf, size);
23562974e916SYuri Benditovich         timer_mod(chain->drain_timer,
23572974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
23582974e916SYuri Benditovich         return size;
23592974e916SYuri Benditovich     }
23602974e916SYuri Benditovich 
23612974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
23622974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
23632974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
23642974e916SYuri Benditovich         } else {
23652974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
23662974e916SYuri Benditovich         }
23672974e916SYuri Benditovich 
23682974e916SYuri Benditovich         if (ret == RSC_FINAL) {
23692974e916SYuri Benditovich             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
23702974e916SYuri Benditovich                 /* Send failed */
23712974e916SYuri Benditovich                 chain->stat.final_failed++;
23722974e916SYuri Benditovich                 return 0;
23732974e916SYuri Benditovich             }
23742974e916SYuri Benditovich 
23752974e916SYuri Benditovich             /* Send current packet */
23762974e916SYuri Benditovich             return virtio_net_do_receive(nc, buf, size);
23772974e916SYuri Benditovich         } else if (ret == RSC_NO_MATCH) {
23782974e916SYuri Benditovich             continue;
23792974e916SYuri Benditovich         } else {
23802974e916SYuri Benditovich             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
23812974e916SYuri Benditovich             seg->is_coalesced = 1;
23822974e916SYuri Benditovich             return size;
23832974e916SYuri Benditovich         }
23842974e916SYuri Benditovich     }
23852974e916SYuri Benditovich 
23862974e916SYuri Benditovich     chain->stat.no_match_cache++;
23872974e916SYuri Benditovich     virtio_net_rsc_cache_buf(chain, nc, buf, size);
23882974e916SYuri Benditovich     return size;
23892974e916SYuri Benditovich }
23902974e916SYuri Benditovich 
23912974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */
23922974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
23932974e916SYuri Benditovich                                         NetClientState *nc,
23942974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
23952974e916SYuri Benditovich                                         uint16_t ip_start, uint16_t ip_size,
23962974e916SYuri Benditovich                                         uint16_t tcp_port)
23972974e916SYuri Benditovich {
23982974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
23992974e916SYuri Benditovich     uint32_t ppair1, ppair2;
24002974e916SYuri Benditovich 
24012974e916SYuri Benditovich     ppair1 = *(uint32_t *)(buf + tcp_port);
24022974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
24032974e916SYuri Benditovich         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
24042974e916SYuri Benditovich         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
24052974e916SYuri Benditovich             || (ppair1 != ppair2)) {
24062974e916SYuri Benditovich             continue;
24072974e916SYuri Benditovich         }
24082974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
24092974e916SYuri Benditovich             chain->stat.drain_failed++;
24102974e916SYuri Benditovich         }
24112974e916SYuri Benditovich 
24122974e916SYuri Benditovich         break;
24132974e916SYuri Benditovich     }
24142974e916SYuri Benditovich 
24152974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
24162974e916SYuri Benditovich }
24172974e916SYuri Benditovich 
24182974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
24192974e916SYuri Benditovich                                             struct ip_header *ip,
24202974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
24212974e916SYuri Benditovich {
24222974e916SYuri Benditovich     uint16_t ip_len;
24232974e916SYuri Benditovich 
24242974e916SYuri Benditovich     /* Not an ipv4 packet */
24252974e916SYuri Benditovich     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
24262974e916SYuri Benditovich         chain->stat.ip_option++;
24272974e916SYuri Benditovich         return RSC_BYPASS;
24282974e916SYuri Benditovich     }
24292974e916SYuri Benditovich 
24302974e916SYuri Benditovich     /* Don't handle packets with ip option */
24312974e916SYuri Benditovich     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
24322974e916SYuri Benditovich         chain->stat.ip_option++;
24332974e916SYuri Benditovich         return RSC_BYPASS;
24342974e916SYuri Benditovich     }
24352974e916SYuri Benditovich 
24362974e916SYuri Benditovich     if (ip->ip_p != IPPROTO_TCP) {
24372974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24382974e916SYuri Benditovich         return RSC_BYPASS;
24392974e916SYuri Benditovich     }
24402974e916SYuri Benditovich 
24412974e916SYuri Benditovich     /* Don't handle packets with ip fragment */
24422974e916SYuri Benditovich     if (!(htons(ip->ip_off) & IP_DF)) {
24432974e916SYuri Benditovich         chain->stat.ip_frag++;
24442974e916SYuri Benditovich         return RSC_BYPASS;
24452974e916SYuri Benditovich     }
24462974e916SYuri Benditovich 
24472974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
24482974e916SYuri Benditovich     if (IPTOS_ECN(ip->ip_tos)) {
24492974e916SYuri Benditovich         chain->stat.ip_ecn++;
24502974e916SYuri Benditovich         return RSC_BYPASS;
24512974e916SYuri Benditovich     }
24522974e916SYuri Benditovich 
24532974e916SYuri Benditovich     ip_len = htons(ip->ip_len);
24542974e916SYuri Benditovich     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
24552974e916SYuri Benditovich         || ip_len > (size - chain->n->guest_hdr_len -
24562974e916SYuri Benditovich                      sizeof(struct eth_header))) {
24572974e916SYuri Benditovich         chain->stat.ip_hacked++;
24582974e916SYuri Benditovich         return RSC_BYPASS;
24592974e916SYuri Benditovich     }
24602974e916SYuri Benditovich 
24612974e916SYuri Benditovich     return RSC_CANDIDATE;
24622974e916SYuri Benditovich }
24632974e916SYuri Benditovich 
24642974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
24652974e916SYuri Benditovich                                       NetClientState *nc,
24662974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
24672974e916SYuri Benditovich {
24682974e916SYuri Benditovich     int32_t ret;
24692974e916SYuri Benditovich     uint16_t hdr_len;
24702974e916SYuri Benditovich     VirtioNetRscUnit unit;
24712974e916SYuri Benditovich 
24722974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
24732974e916SYuri Benditovich 
24742974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
24752974e916SYuri Benditovich         + sizeof(struct tcp_header))) {
24762974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24772974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24782974e916SYuri Benditovich     }
24792974e916SYuri Benditovich 
24802974e916SYuri Benditovich     virtio_net_rsc_extract_unit4(chain, buf, &unit);
24812974e916SYuri Benditovich     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
24822974e916SYuri Benditovich         != RSC_CANDIDATE) {
24832974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24842974e916SYuri Benditovich     }
24852974e916SYuri Benditovich 
24862974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
24872974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
24882974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24892974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
24902974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
24912974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 12),
24922974e916SYuri Benditovich                 VIRTIO_NET_IP4_ADDR_SIZE,
24932974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
24942974e916SYuri Benditovich     }
24952974e916SYuri Benditovich 
24962974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
24972974e916SYuri Benditovich }
24982974e916SYuri Benditovich 
24992974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
25002974e916SYuri Benditovich                                             struct ip6_header *ip6,
25012974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
25022974e916SYuri Benditovich {
25032974e916SYuri Benditovich     uint16_t ip_len;
25042974e916SYuri Benditovich 
25052974e916SYuri Benditovich     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
25062974e916SYuri Benditovich         != IP_HEADER_VERSION_6) {
25072974e916SYuri Benditovich         return RSC_BYPASS;
25082974e916SYuri Benditovich     }
25092974e916SYuri Benditovich 
25102974e916SYuri Benditovich     /* Both option and protocol is checked in this */
25112974e916SYuri Benditovich     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
25122974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
25132974e916SYuri Benditovich         return RSC_BYPASS;
25142974e916SYuri Benditovich     }
25152974e916SYuri Benditovich 
25162974e916SYuri Benditovich     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
25172974e916SYuri Benditovich     if (ip_len < sizeof(struct tcp_header) ||
25182974e916SYuri Benditovich         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
25192974e916SYuri Benditovich                   - sizeof(struct ip6_header))) {
25202974e916SYuri Benditovich         chain->stat.ip_hacked++;
25212974e916SYuri Benditovich         return RSC_BYPASS;
25222974e916SYuri Benditovich     }
25232974e916SYuri Benditovich 
25242974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
25252974e916SYuri Benditovich     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
25262974e916SYuri Benditovich         chain->stat.ip_ecn++;
25272974e916SYuri Benditovich         return RSC_BYPASS;
25282974e916SYuri Benditovich     }
25292974e916SYuri Benditovich 
25302974e916SYuri Benditovich     return RSC_CANDIDATE;
25312974e916SYuri Benditovich }
25322974e916SYuri Benditovich 
25332974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
25342974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
25352974e916SYuri Benditovich {
25362974e916SYuri Benditovich     int32_t ret;
25372974e916SYuri Benditovich     uint16_t hdr_len;
25382974e916SYuri Benditovich     VirtioNetRscChain *chain;
25392974e916SYuri Benditovich     VirtioNetRscUnit unit;
25402974e916SYuri Benditovich 
25413d558330SMarkus Armbruster     chain = opq;
25422974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
25432974e916SYuri Benditovich 
25442974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
25452974e916SYuri Benditovich         + sizeof(tcp_header))) {
25462974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25472974e916SYuri Benditovich     }
25482974e916SYuri Benditovich 
25492974e916SYuri Benditovich     virtio_net_rsc_extract_unit6(chain, buf, &unit);
25502974e916SYuri Benditovich     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
25512974e916SYuri Benditovich                                                  unit.ip, buf, size)) {
25522974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25532974e916SYuri Benditovich     }
25542974e916SYuri Benditovich 
25552974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
25562974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
25572974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25582974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
25592974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
25602974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 8),
25612974e916SYuri Benditovich                 VIRTIO_NET_IP6_ADDR_SIZE,
25622974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header)
25632974e916SYuri Benditovich                 + sizeof(struct ip6_header));
25642974e916SYuri Benditovich     }
25652974e916SYuri Benditovich 
25662974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
25672974e916SYuri Benditovich }
25682974e916SYuri Benditovich 
25692974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
25702974e916SYuri Benditovich                                                       NetClientState *nc,
25712974e916SYuri Benditovich                                                       uint16_t proto)
25722974e916SYuri Benditovich {
25732974e916SYuri Benditovich     VirtioNetRscChain *chain;
25742974e916SYuri Benditovich 
25752974e916SYuri Benditovich     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
25762974e916SYuri Benditovich         return NULL;
25772974e916SYuri Benditovich     }
25782974e916SYuri Benditovich 
25792974e916SYuri Benditovich     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
25802974e916SYuri Benditovich         if (chain->proto == proto) {
25812974e916SYuri Benditovich             return chain;
25822974e916SYuri Benditovich         }
25832974e916SYuri Benditovich     }
25842974e916SYuri Benditovich 
25852974e916SYuri Benditovich     chain = g_malloc(sizeof(*chain));
25862974e916SYuri Benditovich     chain->n = n;
25872974e916SYuri Benditovich     chain->proto = proto;
25882974e916SYuri Benditovich     if (proto == (uint16_t)ETH_P_IP) {
25892974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
25902974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
25912974e916SYuri Benditovich     } else {
25922974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
25932974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
25942974e916SYuri Benditovich     }
25952974e916SYuri Benditovich     chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
25962974e916SYuri Benditovich                                       virtio_net_rsc_purge, chain);
25972974e916SYuri Benditovich     memset(&chain->stat, 0, sizeof(chain->stat));
25982974e916SYuri Benditovich 
25992974e916SYuri Benditovich     QTAILQ_INIT(&chain->buffers);
26002974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
26012974e916SYuri Benditovich 
26022974e916SYuri Benditovich     return chain;
26032974e916SYuri Benditovich }
26042974e916SYuri Benditovich 
26052974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc,
26062974e916SYuri Benditovich                                       const uint8_t *buf,
26072974e916SYuri Benditovich                                       size_t size)
26082974e916SYuri Benditovich {
26092974e916SYuri Benditovich     uint16_t proto;
26102974e916SYuri Benditovich     VirtioNetRscChain *chain;
26112974e916SYuri Benditovich     struct eth_header *eth;
26122974e916SYuri Benditovich     VirtIONet *n;
26132974e916SYuri Benditovich 
26142974e916SYuri Benditovich     n = qemu_get_nic_opaque(nc);
26152974e916SYuri Benditovich     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
26162974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
26172974e916SYuri Benditovich     }
26182974e916SYuri Benditovich 
26192974e916SYuri Benditovich     eth = (struct eth_header *)(buf + n->guest_hdr_len);
26202974e916SYuri Benditovich     proto = htons(eth->h_proto);
26212974e916SYuri Benditovich 
26222974e916SYuri Benditovich     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
26232974e916SYuri Benditovich     if (chain) {
26242974e916SYuri Benditovich         chain->stat.received++;
26252974e916SYuri Benditovich         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
26262974e916SYuri Benditovich             return virtio_net_rsc_receive4(chain, nc, buf, size);
26272974e916SYuri Benditovich         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
26282974e916SYuri Benditovich             return virtio_net_rsc_receive6(chain, nc, buf, size);
26292974e916SYuri Benditovich         }
26302974e916SYuri Benditovich     }
26312974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
26322974e916SYuri Benditovich }
26332974e916SYuri Benditovich 
26342974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
26352974e916SYuri Benditovich                                   size_t size)
26362974e916SYuri Benditovich {
26372974e916SYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
26382974e916SYuri Benditovich     if ((n->rsc4_enabled || n->rsc6_enabled)) {
26392974e916SYuri Benditovich         return virtio_net_rsc_receive(nc, buf, size);
26402974e916SYuri Benditovich     } else {
26412974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
26422974e916SYuri Benditovich     }
26432974e916SYuri Benditovich }
26442974e916SYuri Benditovich 
26456e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
26466e790746SPaolo Bonzini 
26476e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
26486e790746SPaolo Bonzini {
26496e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
26506e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
265117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2652df8d0708SLaurent Vivier     int ret;
26536e790746SPaolo Bonzini 
265451b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
265517a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
26566e790746SPaolo Bonzini 
265751b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
265851b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
26596e790746SPaolo Bonzini 
26606e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2661df8d0708SLaurent Vivier     ret = virtio_net_flush_tx(q);
26627550a822SLaurent Vivier     if (ret >= n->tx_burst) {
2663df8d0708SLaurent Vivier         /*
2664df8d0708SLaurent Vivier          * the flush has been stopped by tx_burst
2665df8d0708SLaurent Vivier          * we will not receive notification for the
2666df8d0708SLaurent Vivier          * remainining part, so re-schedule
2667df8d0708SLaurent Vivier          */
2668df8d0708SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
26697550a822SLaurent Vivier         if (q->tx_bh) {
2670df8d0708SLaurent Vivier             qemu_bh_schedule(q->tx_bh);
26717550a822SLaurent Vivier         } else {
26727550a822SLaurent Vivier             timer_mod(q->tx_timer,
26737550a822SLaurent Vivier                       qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
26747550a822SLaurent Vivier         }
2675df8d0708SLaurent Vivier         q->tx_waiting = 1;
2676df8d0708SLaurent Vivier     }
26776e790746SPaolo Bonzini }
26786e790746SPaolo Bonzini 
26796e790746SPaolo Bonzini /* TX */
26806e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
26816e790746SPaolo Bonzini {
26826e790746SPaolo Bonzini     VirtIONet *n = q->n;
268317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
268451b19ebeSPaolo Bonzini     VirtQueueElement *elem;
26856e790746SPaolo Bonzini     int32_t num_packets = 0;
26866e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
268717a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
26886e790746SPaolo Bonzini         return num_packets;
26896e790746SPaolo Bonzini     }
26906e790746SPaolo Bonzini 
269151b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
26926e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
26936e790746SPaolo Bonzini         return num_packets;
26946e790746SPaolo Bonzini     }
26956e790746SPaolo Bonzini 
269651b19ebeSPaolo Bonzini     for (;;) {
2697bd89dd98SJason Wang         ssize_t ret;
269851b19ebeSPaolo Bonzini         unsigned int out_num;
269951b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2700feb93f36SJason Wang         struct virtio_net_hdr_mrg_rxbuf mhdr;
27016e790746SPaolo Bonzini 
270251b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
270351b19ebeSPaolo Bonzini         if (!elem) {
270451b19ebeSPaolo Bonzini             break;
270551b19ebeSPaolo Bonzini         }
270651b19ebeSPaolo Bonzini 
270751b19ebeSPaolo Bonzini         out_num = elem->out_num;
270851b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
27096e790746SPaolo Bonzini         if (out_num < 1) {
2710fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
2711fa5e56c2SGreg Kurz             virtqueue_detach_element(q->tx_vq, elem, 0);
2712fa5e56c2SGreg Kurz             g_free(elem);
2713fa5e56c2SGreg Kurz             return -EINVAL;
27146e790746SPaolo Bonzini         }
27156e790746SPaolo Bonzini 
2716032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
2717feb93f36SJason Wang             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2718feb93f36SJason Wang                 n->guest_hdr_len) {
2719fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
2720fa5e56c2SGreg Kurz                 virtqueue_detach_element(q->tx_vq, elem, 0);
2721fa5e56c2SGreg Kurz                 g_free(elem);
2722fa5e56c2SGreg Kurz                 return -EINVAL;
2723032a74a1SCédric Le Goater             }
27241bfa316cSGreg Kurz             if (n->needs_vnet_hdr_swap) {
2725feb93f36SJason Wang                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2726feb93f36SJason Wang                 sg2[0].iov_base = &mhdr;
2727feb93f36SJason Wang                 sg2[0].iov_len = n->guest_hdr_len;
2728feb93f36SJason Wang                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2729feb93f36SJason Wang                                    out_sg, out_num,
2730feb93f36SJason Wang                                    n->guest_hdr_len, -1);
2731feb93f36SJason Wang                 if (out_num == VIRTQUEUE_MAX_SIZE) {
2732feb93f36SJason Wang                     goto drop;
2733032a74a1SCédric Le Goater                 }
2734feb93f36SJason Wang                 out_num += 1;
2735feb93f36SJason Wang                 out_sg = sg2;
2736feb93f36SJason Wang             }
2737feb93f36SJason Wang         }
27386e790746SPaolo Bonzini         /*
27396e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
27406e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
27416e790746SPaolo Bonzini          * that host is interested in.
27426e790746SPaolo Bonzini          */
27436e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
27446e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
27456e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
27466e790746SPaolo Bonzini                                        out_sg, out_num,
27476e790746SPaolo Bonzini                                        0, n->host_hdr_len);
27486e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
27496e790746SPaolo Bonzini                              out_sg, out_num,
27506e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
27516e790746SPaolo Bonzini             out_num = sg_num;
27526e790746SPaolo Bonzini             out_sg = sg;
27536e790746SPaolo Bonzini         }
27546e790746SPaolo Bonzini 
27556e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
27566e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
27576e790746SPaolo Bonzini         if (ret == 0) {
27586e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
27596e790746SPaolo Bonzini             q->async_tx.elem = elem;
27606e790746SPaolo Bonzini             return -EBUSY;
27616e790746SPaolo Bonzini         }
27626e790746SPaolo Bonzini 
2763feb93f36SJason Wang drop:
276451b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
276517a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
276651b19ebeSPaolo Bonzini         g_free(elem);
27676e790746SPaolo Bonzini 
27686e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
27696e790746SPaolo Bonzini             break;
27706e790746SPaolo Bonzini         }
27716e790746SPaolo Bonzini     }
27726e790746SPaolo Bonzini     return num_packets;
27736e790746SPaolo Bonzini }
27746e790746SPaolo Bonzini 
27757550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque);
27767550a822SLaurent Vivier 
27776e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
27786e790746SPaolo Bonzini {
277917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
27806e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
27816e790746SPaolo Bonzini 
2782283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2783283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2784283e2c2aSYuri Benditovich         return;
2785283e2c2aSYuri Benditovich     }
2786283e2c2aSYuri Benditovich 
27876e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
278817a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
27896e790746SPaolo Bonzini         q->tx_waiting = 1;
27906e790746SPaolo Bonzini         return;
27916e790746SPaolo Bonzini     }
27926e790746SPaolo Bonzini 
27936e790746SPaolo Bonzini     if (q->tx_waiting) {
27947550a822SLaurent Vivier         /* We already have queued packets, immediately flush */
2795bc72ad67SAlex Bligh         timer_del(q->tx_timer);
27967550a822SLaurent Vivier         virtio_net_tx_timer(q);
27976e790746SPaolo Bonzini     } else {
27987550a822SLaurent Vivier         /* re-arm timer to flush it (and more) on next tick */
2799bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
2800bc72ad67SAlex Bligh                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28016e790746SPaolo Bonzini         q->tx_waiting = 1;
28026e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
28036e790746SPaolo Bonzini     }
28046e790746SPaolo Bonzini }
28056e790746SPaolo Bonzini 
28066e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
28076e790746SPaolo Bonzini {
280817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
28096e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
28106e790746SPaolo Bonzini 
2811283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2812283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2813283e2c2aSYuri Benditovich         return;
2814283e2c2aSYuri Benditovich     }
2815283e2c2aSYuri Benditovich 
28166e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
28176e790746SPaolo Bonzini         return;
28186e790746SPaolo Bonzini     }
28196e790746SPaolo Bonzini     q->tx_waiting = 1;
28206e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
282117a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
28226e790746SPaolo Bonzini         return;
28236e790746SPaolo Bonzini     }
28246e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
28256e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
28266e790746SPaolo Bonzini }
28276e790746SPaolo Bonzini 
28286e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
28296e790746SPaolo Bonzini {
28306e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
28316e790746SPaolo Bonzini     VirtIONet *n = q->n;
283217a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
28337550a822SLaurent Vivier     int ret;
28347550a822SLaurent Vivier 
2835e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2836e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2837e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2838e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2839e8bcf842SMichael S. Tsirkin         return;
2840e8bcf842SMichael S. Tsirkin     }
28416e790746SPaolo Bonzini 
28426e790746SPaolo Bonzini     q->tx_waiting = 0;
28436e790746SPaolo Bonzini 
28446e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
284517a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
28466e790746SPaolo Bonzini         return;
284717a0ca55SKONRAD Frederic     }
28486e790746SPaolo Bonzini 
28497550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
28507550a822SLaurent Vivier     if (ret == -EBUSY || ret == -EINVAL) {
28517550a822SLaurent Vivier         return;
28527550a822SLaurent Vivier     }
28537550a822SLaurent Vivier     /*
28547550a822SLaurent Vivier      * If we flush a full burst of packets, assume there are
28557550a822SLaurent Vivier      * more coming and immediately rearm
28567550a822SLaurent Vivier      */
28577550a822SLaurent Vivier     if (ret >= n->tx_burst) {
28587550a822SLaurent Vivier         q->tx_waiting = 1;
28597550a822SLaurent Vivier         timer_mod(q->tx_timer,
28607550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28617550a822SLaurent Vivier         return;
28627550a822SLaurent Vivier     }
28637550a822SLaurent Vivier     /*
28647550a822SLaurent Vivier      * If less than a full burst, re-enable notification and flush
28657550a822SLaurent Vivier      * anything that may have come in while we weren't looking.  If
28667550a822SLaurent Vivier      * we find something, assume the guest is still active and rearm
28677550a822SLaurent Vivier      */
28686e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
28697550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
28707550a822SLaurent Vivier     if (ret > 0) {
28717550a822SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
28727550a822SLaurent Vivier         q->tx_waiting = 1;
28737550a822SLaurent Vivier         timer_mod(q->tx_timer,
28747550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28757550a822SLaurent Vivier     }
28766e790746SPaolo Bonzini }
28776e790746SPaolo Bonzini 
28786e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
28796e790746SPaolo Bonzini {
28806e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
28816e790746SPaolo Bonzini     VirtIONet *n = q->n;
288217a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
28836e790746SPaolo Bonzini     int32_t ret;
28846e790746SPaolo Bonzini 
2885e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2886e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2887e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2888e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2889e8bcf842SMichael S. Tsirkin         return;
2890e8bcf842SMichael S. Tsirkin     }
28916e790746SPaolo Bonzini 
28926e790746SPaolo Bonzini     q->tx_waiting = 0;
28936e790746SPaolo Bonzini 
28946e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
289517a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
28966e790746SPaolo Bonzini         return;
289717a0ca55SKONRAD Frederic     }
28986e790746SPaolo Bonzini 
28996e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
2900fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
2901fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
2902fa5e56c2SGreg Kurz                  * broken */
29036e790746SPaolo Bonzini     }
29046e790746SPaolo Bonzini 
29056e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
29066e790746SPaolo Bonzini      * more coming and immediately reschedule */
29076e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
29086e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
29096e790746SPaolo Bonzini         q->tx_waiting = 1;
29106e790746SPaolo Bonzini         return;
29116e790746SPaolo Bonzini     }
29126e790746SPaolo Bonzini 
29136e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
29146e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
29156e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
29166e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2917fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
2918fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
2919fa5e56c2SGreg Kurz         return;
2920fa5e56c2SGreg Kurz     } else if (ret > 0) {
29216e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
29226e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
29236e790746SPaolo Bonzini         q->tx_waiting = 1;
29246e790746SPaolo Bonzini     }
29256e790746SPaolo Bonzini }
29266e790746SPaolo Bonzini 
2927f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
2928f9d6dbf0SWen Congyang {
2929f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2930f9d6dbf0SWen Congyang 
29311c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
29321c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
29339b02e161SWei Wang 
2934f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2935f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29369b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29379b02e161SWei Wang                              virtio_net_handle_tx_timer);
2938f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2939f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
2940f9d6dbf0SWen Congyang                                               &n->vqs[index]);
2941f9d6dbf0SWen Congyang     } else {
2942f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29439b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29449b02e161SWei Wang                              virtio_net_handle_tx_bh);
2945f63192b0SAlexander Bulekov         n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
2946f63192b0SAlexander Bulekov                                                   &DEVICE(vdev)->mem_reentrancy_guard);
2947f9d6dbf0SWen Congyang     }
2948f9d6dbf0SWen Congyang 
2949f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
2950f9d6dbf0SWen Congyang     n->vqs[index].n = n;
2951f9d6dbf0SWen Congyang }
2952f9d6dbf0SWen Congyang 
2953f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
2954f9d6dbf0SWen Congyang {
2955f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2956f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
2957f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
2958f9d6dbf0SWen Congyang 
2959f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
2960f9d6dbf0SWen Congyang 
2961f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
2962f9d6dbf0SWen Congyang     if (q->tx_timer) {
2963f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
2964f989c30cSYunjian Wang         q->tx_timer = NULL;
2965f9d6dbf0SWen Congyang     } else {
2966f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
2967f989c30cSYunjian Wang         q->tx_bh = NULL;
2968f9d6dbf0SWen Congyang     }
2969f989c30cSYunjian Wang     q->tx_waiting = 0;
2970f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
2971f9d6dbf0SWen Congyang }
2972f9d6dbf0SWen Congyang 
2973441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs)
2974f9d6dbf0SWen Congyang {
2975f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2976f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
2977441537f1SJason Wang     int new_num_queues = new_max_queue_pairs * 2 + 1;
2978f9d6dbf0SWen Congyang     int i;
2979f9d6dbf0SWen Congyang 
2980f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
2981f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
2982f9d6dbf0SWen Congyang 
2983f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
2984f9d6dbf0SWen Congyang         return;
2985f9d6dbf0SWen Congyang     }
2986f9d6dbf0SWen Congyang 
2987f9d6dbf0SWen Congyang     /*
2988f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
2989f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
299020f86a75SYuval Shaia      * and then we only enter one of the following two loops.
2991f9d6dbf0SWen Congyang      */
2992f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
2993f9d6dbf0SWen Congyang 
2994f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2995f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
2996f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
2997f9d6dbf0SWen Congyang     }
2998f9d6dbf0SWen Congyang 
2999f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
3000f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
3001f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
3002f9d6dbf0SWen Congyang     }
3003f9d6dbf0SWen Congyang 
3004f9d6dbf0SWen Congyang     /* add ctrl_vq last */
3005f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3006f9d6dbf0SWen Congyang }
3007f9d6dbf0SWen Congyang 
3008ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
30096e790746SPaolo Bonzini {
3010441537f1SJason Wang     int max = multiqueue ? n->max_queue_pairs : 1;
3011f9d6dbf0SWen Congyang 
30126e790746SPaolo Bonzini     n->multiqueue = multiqueue;
3013441537f1SJason Wang     virtio_net_change_num_queue_pairs(n, max);
30146e790746SPaolo Bonzini 
3015441537f1SJason Wang     virtio_net_set_queue_pairs(n);
30166e790746SPaolo Bonzini }
30176e790746SPaolo Bonzini 
3018982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
3019037dab2fSGreg Kurz {
3020982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
3021982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3022037dab2fSGreg Kurz     int i, link_down;
3023037dab2fSGreg Kurz 
30249d8c6a25SDr. David Alan Gilbert     trace_virtio_net_post_load_device();
3025982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
302695129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
3027e22f0603SYuri Benditovich                                                        VIRTIO_F_VERSION_1),
3028e22f0603SYuri Benditovich                                virtio_vdev_has_feature(vdev,
3029e22f0603SYuri Benditovich                                                        VIRTIO_NET_F_HASH_REPORT));
30306e790746SPaolo Bonzini 
30316e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
3032982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
30336e790746SPaolo Bonzini         n->mac_table.in_use = 0;
30346e790746SPaolo Bonzini     }
30356e790746SPaolo Bonzini 
3036982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
30376c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
30386c666823SMichael S. Tsirkin     }
30396c666823SMichael S. Tsirkin 
30407788c3f2SMikhail Sennikovsky     /*
30417788c3f2SMikhail Sennikovsky      * curr_guest_offloads will be later overwritten by the
30427788c3f2SMikhail Sennikovsky      * virtio_set_features_nocheck call done from the virtio_load.
30437788c3f2SMikhail Sennikovsky      * Here we make sure it is preserved and restored accordingly
30447788c3f2SMikhail Sennikovsky      * in the virtio_net_post_load_virtio callback.
30457788c3f2SMikhail Sennikovsky      */
30467788c3f2SMikhail Sennikovsky     n->saved_guest_offloads = n->curr_guest_offloads;
30476c666823SMichael S. Tsirkin 
3048441537f1SJason Wang     virtio_net_set_queue_pairs(n);
30496e790746SPaolo Bonzini 
30506e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
30516e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
30526e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
30536e790746SPaolo Bonzini             break;
30546e790746SPaolo Bonzini         }
30556e790746SPaolo Bonzini     }
30566e790746SPaolo Bonzini     n->mac_table.first_multi = i;
30576e790746SPaolo Bonzini 
30586e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
30596e790746SPaolo Bonzini      * to link status bit in n->status */
30606e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
3061441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
30626e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
30636e790746SPaolo Bonzini     }
30646e790746SPaolo Bonzini 
30656c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
30666c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
30679d8c6a25SDr. David Alan Gilbert         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
30689d8c6a25SDr. David Alan Gilbert                                   QEMU_CLOCK_VIRTUAL,
30699d8c6a25SDr. David Alan Gilbert                                   virtio_net_announce_timer, n);
30709d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
30719d8c6a25SDr. David Alan Gilbert             timer_mod(n->announce_timer.tm,
30729d8c6a25SDr. David Alan Gilbert                       qemu_clock_get_ms(n->announce_timer.type));
30739d8c6a25SDr. David Alan Gilbert         } else {
3074944458b6SDr. David Alan Gilbert             qemu_announce_timer_del(&n->announce_timer, false);
30759d8c6a25SDr. David Alan Gilbert         }
30766c666823SMichael S. Tsirkin     }
30776c666823SMichael S. Tsirkin 
3078e41b7114SYuri Benditovich     if (n->rss_data.enabled) {
30790145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
30800145c393SAndrew Melnychenko         if (!n->rss_data.populate_hash) {
30810145c393SAndrew Melnychenko             if (!virtio_net_attach_epbf_rss(n)) {
30820145c393SAndrew Melnychenko                 if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
30830145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS for vhost");
30840145c393SAndrew Melnychenko                 } else {
30850145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS - "
30860145c393SAndrew Melnychenko                                 "fallback to software RSS");
30870145c393SAndrew Melnychenko                     n->rss_data.enabled_software_rss = true;
30880145c393SAndrew Melnychenko                 }
30890145c393SAndrew Melnychenko             }
30900145c393SAndrew Melnychenko         }
30910145c393SAndrew Melnychenko 
3092e41b7114SYuri Benditovich         trace_virtio_net_rss_enable(n->rss_data.hash_types,
3093e41b7114SYuri Benditovich                                     n->rss_data.indirections_len,
3094e41b7114SYuri Benditovich                                     sizeof(n->rss_data.key));
3095e41b7114SYuri Benditovich     } else {
3096e41b7114SYuri Benditovich         trace_virtio_net_rss_disable();
3097e41b7114SYuri Benditovich     }
30986e790746SPaolo Bonzini     return 0;
30996e790746SPaolo Bonzini }
31006e790746SPaolo Bonzini 
31017788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev)
31027788c3f2SMikhail Sennikovsky {
31037788c3f2SMikhail Sennikovsky     VirtIONet *n = VIRTIO_NET(vdev);
31047788c3f2SMikhail Sennikovsky     /*
31057788c3f2SMikhail Sennikovsky      * The actual needed state is now in saved_guest_offloads,
31067788c3f2SMikhail Sennikovsky      * see virtio_net_post_load_device for detail.
31077788c3f2SMikhail Sennikovsky      * Restore it back and apply the desired offloads.
31087788c3f2SMikhail Sennikovsky      */
31097788c3f2SMikhail Sennikovsky     n->curr_guest_offloads = n->saved_guest_offloads;
31107788c3f2SMikhail Sennikovsky     if (peer_has_vnet_hdr(n)) {
31117788c3f2SMikhail Sennikovsky         virtio_net_apply_guest_offloads(n);
31127788c3f2SMikhail Sennikovsky     }
31137788c3f2SMikhail Sennikovsky 
31147788c3f2SMikhail Sennikovsky     return 0;
31157788c3f2SMikhail Sennikovsky }
31167788c3f2SMikhail Sennikovsky 
3117982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
3118982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
3119982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
3120982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
3121982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
3122982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3123982b78c5SDr. David Alan Gilbert    },
3124982b78c5SDr. David Alan Gilbert };
3125982b78c5SDr. David Alan Gilbert 
3126441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id)
3127982b78c5SDr. David Alan Gilbert {
3128441537f1SJason Wang     return VIRTIO_NET(opaque)->max_queue_pairs > 1;
3129982b78c5SDr. David Alan Gilbert }
3130982b78c5SDr. David Alan Gilbert 
3131982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
3132982b78c5SDr. David Alan Gilbert {
3133982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
3134982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
3135982b78c5SDr. David Alan Gilbert }
3136982b78c5SDr. David Alan Gilbert 
3137982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
3138982b78c5SDr. David Alan Gilbert {
3139982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
3140982b78c5SDr. David Alan Gilbert }
3141982b78c5SDr. David Alan Gilbert 
3142982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
3143982b78c5SDr. David Alan Gilbert {
3144982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
3145982b78c5SDr. David Alan Gilbert }
3146982b78c5SDr. David Alan Gilbert 
3147982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
3148982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
3149982b78c5SDr. David Alan Gilbert  */
3150982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
3151982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
3152982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
3153441537f1SJason Wang     uint16_t        curr_queue_pairs_1;
3154982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
3155982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
3156982b78c5SDr. David Alan Gilbert };
3157982b78c5SDr. David Alan Gilbert 
3158982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
3159441537f1SJason Wang  * the 1st entry in the queue_pairs and only if there's more than one
3160982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
3161982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
3162982b78c5SDr. David Alan Gilbert  */
3163982b78c5SDr. David Alan Gilbert 
316444b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque)
3165982b78c5SDr. David Alan Gilbert {
3166982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3167982b78c5SDr. David Alan Gilbert 
3168982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
3169441537f1SJason Wang     tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1;
3170441537f1SJason Wang     if (tmp->parent->curr_queue_pairs == 0) {
3171441537f1SJason Wang         tmp->curr_queue_pairs_1 = 0;
3172982b78c5SDr. David Alan Gilbert     }
317344b1ff31SDr. David Alan Gilbert 
317444b1ff31SDr. David Alan Gilbert     return 0;
3175982b78c5SDr. David Alan Gilbert }
3176982b78c5SDr. David Alan Gilbert 
3177982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
3178982b78c5SDr. David Alan Gilbert {
3179982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3180982b78c5SDr. David Alan Gilbert 
3181982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
3182982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
3183982b78c5SDr. David Alan Gilbert 
3184441537f1SJason Wang     if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) {
3185441537f1SJason Wang         error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3186441537f1SJason Wang             tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs);
3187982b78c5SDr. David Alan Gilbert 
3188982b78c5SDr. David Alan Gilbert         return -EINVAL;
3189982b78c5SDr. David Alan Gilbert     }
3190982b78c5SDr. David Alan Gilbert 
3191982b78c5SDr. David Alan Gilbert     return 0; /* all good */
3192982b78c5SDr. David Alan Gilbert }
3193982b78c5SDr. David Alan Gilbert 
3194982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
3195982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
3196982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
3197982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
3198982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3199982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
3200441537f1SJason Wang                                      curr_queue_pairs_1,
3201982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
3202982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
3203982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3204982b78c5SDr. David Alan Gilbert     },
3205982b78c5SDr. David Alan Gilbert };
3206982b78c5SDr. David Alan Gilbert 
3207982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
3208982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3209982b78c5SDr. David Alan Gilbert  */
3210982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
3211982b78c5SDr. David Alan Gilbert {
3212982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3213982b78c5SDr. David Alan Gilbert 
3214982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
3215982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
3216982b78c5SDr. David Alan Gilbert         return -EINVAL;
3217982b78c5SDr. David Alan Gilbert     }
3218982b78c5SDr. David Alan Gilbert 
3219982b78c5SDr. David Alan Gilbert     return 0;
3220982b78c5SDr. David Alan Gilbert }
3221982b78c5SDr. David Alan Gilbert 
322244b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque)
3223982b78c5SDr. David Alan Gilbert {
3224982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3225982b78c5SDr. David Alan Gilbert 
3226982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
322744b1ff31SDr. David Alan Gilbert 
322844b1ff31SDr. David Alan Gilbert     return 0;
3229982b78c5SDr. David Alan Gilbert }
3230982b78c5SDr. David Alan Gilbert 
3231982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
3232982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
3233982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
3234982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
3235982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3236982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
3237982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3238982b78c5SDr. David Alan Gilbert     },
3239982b78c5SDr. David Alan Gilbert };
3240982b78c5SDr. David Alan Gilbert 
3241982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3242982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3243982b78c5SDr. David Alan Gilbert  */
3244982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
3245982b78c5SDr. David Alan Gilbert {
3246982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3247982b78c5SDr. David Alan Gilbert 
3248982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
3249982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
3250982b78c5SDr. David Alan Gilbert         return -EINVAL;
3251982b78c5SDr. David Alan Gilbert     }
3252982b78c5SDr. David Alan Gilbert 
3253982b78c5SDr. David Alan Gilbert     return 0;
3254982b78c5SDr. David Alan Gilbert }
3255982b78c5SDr. David Alan Gilbert 
325644b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque)
3257982b78c5SDr. David Alan Gilbert {
3258982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3259982b78c5SDr. David Alan Gilbert 
3260982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
326144b1ff31SDr. David Alan Gilbert 
326244b1ff31SDr. David Alan Gilbert     return 0;
3263982b78c5SDr. David Alan Gilbert }
3264982b78c5SDr. David Alan Gilbert 
3265982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
3266982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
3267982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
3268982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
3269982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3270982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
3271982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3272982b78c5SDr. David Alan Gilbert     },
3273982b78c5SDr. David Alan Gilbert };
3274982b78c5SDr. David Alan Gilbert 
3275e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque)
3276e41b7114SYuri Benditovich {
3277e41b7114SYuri Benditovich     return VIRTIO_NET(opaque)->rss_data.enabled;
3278e41b7114SYuri Benditovich }
3279e41b7114SYuri Benditovich 
3280e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = {
3281e41b7114SYuri Benditovich     .name      = "virtio-net-device/rss",
3282e41b7114SYuri Benditovich     .version_id = 1,
3283e41b7114SYuri Benditovich     .minimum_version_id = 1,
3284e41b7114SYuri Benditovich     .needed = virtio_net_rss_needed,
3285e41b7114SYuri Benditovich     .fields = (VMStateField[]) {
3286e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
3287e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
3288e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
3289e41b7114SYuri Benditovich         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
3290e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
3291e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
3292e41b7114SYuri Benditovich         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
3293e41b7114SYuri Benditovich                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
3294e41b7114SYuri Benditovich         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
3295e41b7114SYuri Benditovich                                     rss_data.indirections_len, 0,
3296e41b7114SYuri Benditovich                                     vmstate_info_uint16, uint16_t),
3297e41b7114SYuri Benditovich         VMSTATE_END_OF_LIST()
3298e41b7114SYuri Benditovich     },
3299e41b7114SYuri Benditovich };
3300e41b7114SYuri Benditovich 
3301982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
3302982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
3303982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
3304982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
3305982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
3306982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
3307982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
3308982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
3309982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
3310982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
3311982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
3312982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
3313982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
3314982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
3315982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3316982b78c5SDr. David Alan Gilbert 
3317982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
3318982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
3319982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
3320982b78c5SDr. David Alan Gilbert          */
3321982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3322982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
3323982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
3324982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3325982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
3326982b78c5SDr. David Alan Gilbert 
3327982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
3328982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3329982b78c5SDr. David Alan Gilbert          * but based on the uint.
3330982b78c5SDr. David Alan Gilbert          */
3331982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3332982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3333982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
3334982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3335982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3336982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
3337982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
3338982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
3339982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
3340982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3341982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
3342441537f1SJason Wang         VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0,
3343982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
3344441537f1SJason Wang         VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1),
3345982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3346982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
3347982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3348982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
3349982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3350982b78c5SDr. David Alan Gilbert    },
3351e41b7114SYuri Benditovich     .subsections = (const VMStateDescription * []) {
3352e41b7114SYuri Benditovich         &vmstate_virtio_net_rss,
3353e41b7114SYuri Benditovich         NULL
3354e41b7114SYuri Benditovich     }
3355982b78c5SDr. David Alan Gilbert };
3356982b78c5SDr. David Alan Gilbert 
33576e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
3358f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
33596e790746SPaolo Bonzini     .size = sizeof(NICState),
33606e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
33616e790746SPaolo Bonzini     .receive = virtio_net_receive,
33626e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
3363b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
3364b2c929f0SDr. David Alan Gilbert     .announce = virtio_net_announce,
33656e790746SPaolo Bonzini };
33666e790746SPaolo Bonzini 
33676e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
33686e790746SPaolo Bonzini {
336917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
337068b0a639SSi-Wei Liu     NetClientState *nc;
33716e790746SPaolo Bonzini     assert(n->vhost_started);
337268b0a639SSi-Wei Liu     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) {
337368b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
337468b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
337568b0a639SSi-Wei Liu          * buggy migration stream.
337668b0a639SSi-Wei Liu          */
337768b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
337868b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
337968b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
338068b0a639SSi-Wei Liu             return false;
338168b0a639SSi-Wei Liu         }
338268b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
338368b0a639SSi-Wei Liu     } else {
338468b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
338568b0a639SSi-Wei Liu     }
3386544f0278SCindy Lu     /*
3387544f0278SCindy Lu      * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
33887e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3389544f0278SCindy Lu      * support, the function will return false
3390544f0278SCindy Lu      */
3391544f0278SCindy Lu 
3392544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
33938aab0d1dSCindy Lu         return vhost_net_config_pending(get_vhost_net(nc->peer));
3394544f0278SCindy Lu     }
3395ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
33966e790746SPaolo Bonzini }
33976e790746SPaolo Bonzini 
33986e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
33996e790746SPaolo Bonzini                                            bool mask)
34006e790746SPaolo Bonzini {
340117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
340268b0a639SSi-Wei Liu     NetClientState *nc;
34036e790746SPaolo Bonzini     assert(n->vhost_started);
340468b0a639SSi-Wei Liu     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) {
340568b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
340668b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
340768b0a639SSi-Wei Liu          * buggy migration stream.
340868b0a639SSi-Wei Liu          */
340968b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
341068b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
341168b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
341268b0a639SSi-Wei Liu             return;
341368b0a639SSi-Wei Liu         }
341468b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
341568b0a639SSi-Wei Liu     } else {
341668b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
341768b0a639SSi-Wei Liu     }
3418544f0278SCindy Lu     /*
3419544f0278SCindy Lu      *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
34207e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3421544f0278SCindy Lu      * support, the function will return
3422544f0278SCindy Lu      */
3423544f0278SCindy Lu 
3424544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
34258aab0d1dSCindy Lu         vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
3426544f0278SCindy Lu         return;
3427544f0278SCindy Lu     }
3428544f0278SCindy Lu     vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
34296e790746SPaolo Bonzini }
34306e790746SPaolo Bonzini 
3431019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
34326e790746SPaolo Bonzini {
34330cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3434a93e599dSMaxime Coquelin 
3435d74c30c8SDaniil Tatianin     n->config_size = virtio_get_config_size(&cfg_size_params, host_features);
343617ec5a86SKONRAD Frederic }
34376e790746SPaolo Bonzini 
34388a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
34398a253ec2SKONRAD Frederic                                    const char *type)
34408a253ec2SKONRAD Frederic {
34418a253ec2SKONRAD Frederic     /*
34428a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
34438a253ec2SKONRAD Frederic      */
34448a253ec2SKONRAD Frederic     assert(type != NULL);
34458a253ec2SKONRAD Frederic 
34468a253ec2SKONRAD Frederic     g_free(n->netclient_name);
34478a253ec2SKONRAD Frederic     g_free(n->netclient_type);
34488a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
34498a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
34508a253ec2SKONRAD Frederic }
34518a253ec2SKONRAD Frederic 
34520e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
34539711cd0dSJens Freimann {
34549711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
34559711cd0dSJens Freimann     PCIDevice *pci_dev;
34569711cd0dSJens Freimann     Error *err = NULL;
34579711cd0dSJens Freimann 
34580e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
34599711cd0dSJens Freimann     if (hotplug_ctrl) {
34600e9a65c5SJuan Quintela         pci_dev = PCI_DEVICE(dev);
34619711cd0dSJens Freimann         pci_dev->partially_hotplugged = true;
34620e9a65c5SJuan Quintela         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
34639711cd0dSJens Freimann         if (err) {
34649711cd0dSJens Freimann             error_report_err(err);
34659711cd0dSJens Freimann             return false;
34669711cd0dSJens Freimann         }
34679711cd0dSJens Freimann     } else {
34689711cd0dSJens Freimann         return false;
34699711cd0dSJens Freimann     }
34709711cd0dSJens Freimann     return true;
34719711cd0dSJens Freimann }
34729711cd0dSJens Freimann 
34730e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
34740e9a65c5SJuan Quintela                                     Error **errp)
34759711cd0dSJens Freimann {
34765a0948d3SMarkus Armbruster     Error *err = NULL;
34779711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
34780e9a65c5SJuan Quintela     PCIDevice *pdev = PCI_DEVICE(dev);
347978274682SJuan Quintela     BusState *primary_bus;
34809711cd0dSJens Freimann 
34819711cd0dSJens Freimann     if (!pdev->partially_hotplugged) {
34829711cd0dSJens Freimann         return true;
34839711cd0dSJens Freimann     }
34840e9a65c5SJuan Quintela     primary_bus = dev->parent_bus;
348578274682SJuan Quintela     if (!primary_bus) {
3486150ab54aSJens Freimann         error_setg(errp, "virtio_net: couldn't find primary bus");
34875a0948d3SMarkus Armbruster         return false;
34889711cd0dSJens Freimann     }
34890e9a65c5SJuan Quintela     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3490e2bde83eSJuan Quintela     qatomic_set(&n->failover_primary_hidden, false);
34910e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
34929711cd0dSJens Freimann     if (hotplug_ctrl) {
34930e9a65c5SJuan Quintela         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
34945a0948d3SMarkus Armbruster         if (err) {
34955a0948d3SMarkus Armbruster             goto out;
34965a0948d3SMarkus Armbruster         }
34970e9a65c5SJuan Quintela         hotplug_handler_plug(hotplug_ctrl, dev, &err);
34989711cd0dSJens Freimann     }
3499109c20eaSLaurent Vivier     pdev->partially_hotplugged = false;
3500150ab54aSJens Freimann 
3501150ab54aSJens Freimann out:
35025a0948d3SMarkus Armbruster     error_propagate(errp, err);
35035a0948d3SMarkus Armbruster     return !err;
35049711cd0dSJens Freimann }
35059711cd0dSJens Freimann 
350607a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
35079711cd0dSJens Freimann {
35089711cd0dSJens Freimann     bool should_be_hidden;
35099711cd0dSJens Freimann     Error *err = NULL;
351007a5d816SJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
351107a5d816SJuan Quintela 
351207a5d816SJuan Quintela     if (!dev) {
351307a5d816SJuan Quintela         return;
351407a5d816SJuan Quintela     }
35159711cd0dSJens Freimann 
3516e2bde83eSJuan Quintela     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
35179711cd0dSJens Freimann 
35184dbac1aeSMarkus Armbruster     if (migration_in_setup(s) && !should_be_hidden) {
351907a5d816SJuan Quintela         if (failover_unplug_primary(n, dev)) {
352007a5d816SJuan Quintela             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
352107a5d816SJuan Quintela             qapi_event_send_unplug_primary(dev->id);
3522e2bde83eSJuan Quintela             qatomic_set(&n->failover_primary_hidden, true);
35239711cd0dSJens Freimann         } else {
35249711cd0dSJens Freimann             warn_report("couldn't unplug primary device");
35259711cd0dSJens Freimann         }
35269711cd0dSJens Freimann     } else if (migration_has_failed(s)) {
3527150ab54aSJens Freimann         /* We already unplugged the device let's plug it back */
352807a5d816SJuan Quintela         if (!failover_replug_primary(n, dev, &err)) {
35299711cd0dSJens Freimann             if (err) {
35309711cd0dSJens Freimann                 error_report_err(err);
35319711cd0dSJens Freimann             }
35329711cd0dSJens Freimann         }
35339711cd0dSJens Freimann     }
35349711cd0dSJens Freimann }
35359711cd0dSJens Freimann 
35369711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
35379711cd0dSJens Freimann {
35389711cd0dSJens Freimann     MigrationState *s = data;
35399711cd0dSJens Freimann     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
35409711cd0dSJens Freimann     virtio_net_handle_migration_primary(n, s);
35419711cd0dSJens Freimann }
35429711cd0dSJens Freimann 
3543b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener,
3544f3558b1bSKevin Wolf                                          const QDict *device_opts,
3545f3558b1bSKevin Wolf                                          bool from_json,
3546f3558b1bSKevin Wolf                                          Error **errp)
35479711cd0dSJens Freimann {
35489711cd0dSJens Freimann     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
35494f0303aeSJuan Quintela     const char *standby_id;
35509711cd0dSJens Freimann 
35514d0e59acSJens Freimann     if (!device_opts) {
355289631fedSJuan Quintela         return false;
35534d0e59acSJens Freimann     }
3554bcfc906bSLaurent Vivier 
3555bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "failover_pair_id")) {
3556bcfc906bSLaurent Vivier         return false;
3557bcfc906bSLaurent Vivier     }
3558bcfc906bSLaurent Vivier 
3559bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "id")) {
3560bcfc906bSLaurent Vivier         error_setg(errp, "Device with failover_pair_id needs to have id");
3561bcfc906bSLaurent Vivier         return false;
3562bcfc906bSLaurent Vivier     }
3563bcfc906bSLaurent Vivier 
3564bcfc906bSLaurent Vivier     standby_id = qdict_get_str(device_opts, "failover_pair_id");
356589631fedSJuan Quintela     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
356689631fedSJuan Quintela         return false;
35679711cd0dSJens Freimann     }
35689711cd0dSJens Freimann 
35697fe7791eSLaurent Vivier     /*
35707fe7791eSLaurent Vivier      * The hide helper can be called several times for a given device.
35717fe7791eSLaurent Vivier      * Check there is only one primary for a virtio-net device but
35727fe7791eSLaurent Vivier      * don't duplicate the qdict several times if it's called for the same
35737fe7791eSLaurent Vivier      * device.
35747fe7791eSLaurent Vivier      */
3575259a10dbSKevin Wolf     if (n->primary_opts) {
35767fe7791eSLaurent Vivier         const char *old, *new;
35777fe7791eSLaurent Vivier         /* devices with failover_pair_id always have an id */
35787fe7791eSLaurent Vivier         old = qdict_get_str(n->primary_opts, "id");
35797fe7791eSLaurent Vivier         new = qdict_get_str(device_opts, "id");
35807fe7791eSLaurent Vivier         if (strcmp(old, new) != 0) {
35817fe7791eSLaurent Vivier             error_setg(errp, "Cannot attach more than one primary device to "
35827fe7791eSLaurent Vivier                        "'%s': '%s' and '%s'", n->netclient_name, old, new);
3583259a10dbSKevin Wolf             return false;
3584259a10dbSKevin Wolf         }
35857fe7791eSLaurent Vivier     } else {
3586f3558b1bSKevin Wolf         n->primary_opts = qdict_clone_shallow(device_opts);
3587f3558b1bSKevin Wolf         n->primary_opts_from_json = from_json;
35887fe7791eSLaurent Vivier     }
3589259a10dbSKevin Wolf 
3590e2bde83eSJuan Quintela     /* failover_primary_hidden is set during feature negotiation */
35913abad4a2SJuan Quintela     return qatomic_read(&n->failover_primary_hidden);
35929711cd0dSJens Freimann }
35939711cd0dSJens Freimann 
3594e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
359517ec5a86SKONRAD Frederic {
3596e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3597284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3598284a32f0SAndreas Färber     NetClientState *nc;
35991773d9eeSKONRAD Frederic     int i;
360017ec5a86SKONRAD Frederic 
3601a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
3602127833eeSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3603a93e599dSMaxime Coquelin     }
3604a93e599dSMaxime Coquelin 
36059473939eSJason Baron     if (n->net_conf.duplex_str) {
36069473939eSJason Baron         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
36079473939eSJason Baron             n->net_conf.duplex = DUPLEX_HALF;
36089473939eSJason Baron         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
36099473939eSJason Baron             n->net_conf.duplex = DUPLEX_FULL;
36109473939eSJason Baron         } else {
36119473939eSJason Baron             error_setg(errp, "'duplex' must be 'half' or 'full'");
3612843c4cfcSMarkus Armbruster             return;
36139473939eSJason Baron         }
36149473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
36159473939eSJason Baron     } else {
36169473939eSJason Baron         n->net_conf.duplex = DUPLEX_UNKNOWN;
36179473939eSJason Baron     }
36189473939eSJason Baron 
36199473939eSJason Baron     if (n->net_conf.speed < SPEED_UNKNOWN) {
36209473939eSJason Baron         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3621843c4cfcSMarkus Armbruster         return;
3622843c4cfcSMarkus Armbruster     }
3623843c4cfcSMarkus Armbruster     if (n->net_conf.speed >= 0) {
36249473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
36259473939eSJason Baron     }
36269473939eSJason Baron 
36279711cd0dSJens Freimann     if (n->failover) {
3628b91ad981SJuan Quintela         n->primary_listener.hide_device = failover_hide_primary_device;
3629e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, true);
36309711cd0dSJens Freimann         device_listener_register(&n->primary_listener);
36319711cd0dSJens Freimann         n->migration_state.notify = virtio_net_migration_state_notifier;
36329711cd0dSJens Freimann         add_migration_state_change_notifier(&n->migration_state);
36339711cd0dSJens Freimann         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
36349711cd0dSJens Freimann     }
36359711cd0dSJens Freimann 
3636da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
36373857cd5cSJonah Palmer     virtio_init(vdev, VIRTIO_ID_NET, n->config_size);
363817ec5a86SKONRAD Frederic 
36391c0fbfa3SMichael S. Tsirkin     /*
36401c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
36411c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
36421c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
36431c0fbfa3SMichael S. Tsirkin      */
36441c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
36451c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
36465f997fd1SMichal Privoznik         !is_power_of_2(n->net_conf.rx_queue_size)) {
36471c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
36481c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
36491c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
36501c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
36511c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
36521c0fbfa3SMichael S. Tsirkin         return;
36531c0fbfa3SMichael S. Tsirkin     }
36541c0fbfa3SMichael S. Tsirkin 
36559b02e161SWei Wang     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
36564271f403SLaurent Vivier         n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) ||
36579b02e161SWei Wang         !is_power_of_2(n->net_conf.tx_queue_size)) {
36589b02e161SWei Wang         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
36599b02e161SWei Wang                    "must be a power of 2 between %d and %d",
36609b02e161SWei Wang                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
36614271f403SLaurent Vivier                    virtio_net_max_tx_queue_size(n));
36629b02e161SWei Wang         virtio_cleanup(vdev);
36639b02e161SWei Wang         return;
36649b02e161SWei Wang     }
36659b02e161SWei Wang 
366622288fe5SJason Wang     n->max_ncs = MAX(n->nic_conf.peers.queues, 1);
366722288fe5SJason Wang 
366822288fe5SJason Wang     /*
366922288fe5SJason Wang      * Figure out the datapath queue pairs since the backend could
367022288fe5SJason Wang      * provide control queue via peers as well.
367122288fe5SJason Wang      */
367222288fe5SJason Wang     if (n->nic_conf.peers.queues) {
367322288fe5SJason Wang         for (i = 0; i < n->max_ncs; i++) {
367422288fe5SJason Wang             if (n->nic_conf.peers.ncs[i]->is_datapath) {
367522288fe5SJason Wang                 ++n->max_queue_pairs;
367622288fe5SJason Wang             }
367722288fe5SJason Wang         }
367822288fe5SJason Wang     }
367922288fe5SJason Wang     n->max_queue_pairs = MAX(n->max_queue_pairs, 1);
368022288fe5SJason Wang 
3681441537f1SJason Wang     if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) {
368222288fe5SJason Wang         error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), "
3683631b22eaSStefan Weil                    "must be a positive integer less than %d.",
3684441537f1SJason Wang                    n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2);
36857e0e736eSJason Wang         virtio_cleanup(vdev);
36867e0e736eSJason Wang         return;
36877e0e736eSJason Wang     }
3688b21e2380SMarkus Armbruster     n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs);
3689441537f1SJason Wang     n->curr_queue_pairs = 1;
36901773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
36916e790746SPaolo Bonzini 
36921773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
36931773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
36940765691eSMarkus Armbruster         warn_report("virtio-net: "
36956e790746SPaolo Bonzini                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
36961773d9eeSKONRAD Frederic                     n->net_conf.tx);
36970765691eSMarkus Armbruster         error_printf("Defaulting to \"bh\"");
36986e790746SPaolo Bonzini     }
36996e790746SPaolo Bonzini 
37002eef278bSMichael S. Tsirkin     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
37012eef278bSMichael S. Tsirkin                                     n->net_conf.tx_queue_size);
37029b02e161SWei Wang 
3703441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
3704f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i);
3705da51a335SJason Wang     }
3706da51a335SJason Wang 
370717a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
37081773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
37091773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
37106e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
37119d8c6a25SDr. David Alan Gilbert     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
37129d8c6a25SDr. David Alan Gilbert                               QEMU_CLOCK_VIRTUAL,
3713f57fcf70SJason Wang                               virtio_net_announce_timer, n);
3714b2c929f0SDr. David Alan Gilbert     n->announce_timer.round = 0;
37156e790746SPaolo Bonzini 
37168a253ec2SKONRAD Frederic     if (n->netclient_type) {
37178a253ec2SKONRAD Frederic         /*
37188a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
37198a253ec2SKONRAD Frederic          */
37208a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
37218a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
37228a253ec2SKONRAD Frederic     } else {
37231773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3724284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
37258a253ec2SKONRAD Frederic     }
37268a253ec2SKONRAD Frederic 
3727441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
3728d4c62930SBin Meng         n->nic->ncs[i].do_not_pad = true;
3729d4c62930SBin Meng     }
3730d4c62930SBin Meng 
37316e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
37326e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
3733441537f1SJason Wang         for (i = 0; i < n->max_queue_pairs; i++) {
3734d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
37356e790746SPaolo Bonzini         }
37366e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
37376e790746SPaolo Bonzini     } else {
37386e790746SPaolo Bonzini         n->host_hdr_len = 0;
37396e790746SPaolo Bonzini     }
37406e790746SPaolo Bonzini 
37411773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
37426e790746SPaolo Bonzini 
37436e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
37441773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
3745e22f0603SYuri Benditovich     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
37466e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
37476e790746SPaolo Bonzini 
37486e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
37496e790746SPaolo Bonzini 
37506e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
37516e790746SPaolo Bonzini 
3752b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
3753b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
3754b1be4280SAmos Kong 
3755e87936eaSCindy Lu    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3756e87936eaSCindy Lu         struct virtio_net_config netcfg = {};
3757e87936eaSCindy Lu         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3758e87936eaSCindy Lu         vhost_net_set_config(get_vhost_net(nc->peer),
3759f8ed3648SManos Pitsidianakis             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND);
3760e87936eaSCindy Lu     }
37612974e916SYuri Benditovich     QTAILQ_INIT(&n->rsc_chains);
3762284a32f0SAndreas Färber     n->qdev = dev;
37634474e37aSYuri Benditovich 
3764aac8f89dSAkihiko Odaki     net_rx_pkt_init(&n->rx_pkt);
37650145c393SAndrew Melnychenko 
37660145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
37670145c393SAndrew Melnychenko         virtio_net_load_ebpf(n);
37680145c393SAndrew Melnychenko     }
376917ec5a86SKONRAD Frederic }
377017ec5a86SKONRAD Frederic 
3771b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev)
377217ec5a86SKONRAD Frederic {
3773306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3774306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3775441537f1SJason Wang     int i, max_queue_pairs;
377617ec5a86SKONRAD Frederic 
37770145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
37780145c393SAndrew Melnychenko         virtio_net_unload_ebpf(n);
37790145c393SAndrew Melnychenko     }
37800145c393SAndrew Melnychenko 
378117ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
378217ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
378317ec5a86SKONRAD Frederic 
37848a253ec2SKONRAD Frederic     g_free(n->netclient_name);
37858a253ec2SKONRAD Frederic     n->netclient_name = NULL;
37868a253ec2SKONRAD Frederic     g_free(n->netclient_type);
37878a253ec2SKONRAD Frederic     n->netclient_type = NULL;
37888a253ec2SKONRAD Frederic 
378917ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
379017ec5a86SKONRAD Frederic     g_free(n->vlans);
379117ec5a86SKONRAD Frederic 
37929711cd0dSJens Freimann     if (n->failover) {
3793f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
379465018100SJuan Quintela         device_listener_unregister(&n->primary_listener);
37951e157667SLaurent Vivier         remove_migration_state_change_notifier(&n->migration_state);
3796f3558b1bSKevin Wolf     } else {
3797f3558b1bSKevin Wolf         assert(n->primary_opts == NULL);
37989711cd0dSJens Freimann     }
37999711cd0dSJens Freimann 
3800441537f1SJason Wang     max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
3801441537f1SJason Wang     for (i = 0; i < max_queue_pairs; i++) {
3802f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
380317ec5a86SKONRAD Frederic     }
3804d945d9f1SYuri Benditovich     /* delete also control vq */
3805441537f1SJason Wang     virtio_del_queue(vdev, max_queue_pairs * 2);
3806944458b6SDr. David Alan Gilbert     qemu_announce_timer_del(&n->announce_timer, false);
380717ec5a86SKONRAD Frederic     g_free(n->vqs);
380817ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
38092974e916SYuri Benditovich     virtio_net_rsc_cleanup(n);
381059079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
38114474e37aSYuri Benditovich     net_rx_pkt_uninit(n->rx_pkt);
38126a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
381317ec5a86SKONRAD Frederic }
381417ec5a86SKONRAD Frederic 
381517ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
381617ec5a86SKONRAD Frederic {
381717ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
381817ec5a86SKONRAD Frederic 
381917ec5a86SKONRAD Frederic     /*
382017ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
382117ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
382217ec5a86SKONRAD Frederic      */
382317ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
3824aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3825aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
382640c2281cSMarkus Armbruster                                   DEVICE(n));
38270145c393SAndrew Melnychenko 
38280145c393SAndrew Melnychenko     ebpf_rss_init(&n->ebpf_rss);
382917ec5a86SKONRAD Frederic }
383017ec5a86SKONRAD Frederic 
383144b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque)
38324d45dcfbSHalil Pasic {
38334d45dcfbSHalil Pasic     VirtIONet *n = opaque;
38344d45dcfbSHalil Pasic 
38354d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
38364d45dcfbSHalil Pasic      * it might keep writing to memory. */
38374d45dcfbSHalil Pasic     assert(!n->vhost_started);
383844b1ff31SDr. David Alan Gilbert 
383944b1ff31SDr. David Alan Gilbert     return 0;
38404d45dcfbSHalil Pasic }
38414d45dcfbSHalil Pasic 
38429711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque)
38439711cd0dSJens Freimann {
38449711cd0dSJens Freimann     DeviceState *dev = opaque;
384521e8709bSJuan Quintela     DeviceState *primary;
38469711cd0dSJens Freimann     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
38479711cd0dSJens Freimann     VirtIONet *n = VIRTIO_NET(vdev);
38489711cd0dSJens Freimann 
3849284f42a5SJens Freimann     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3850284f42a5SJens Freimann         return false;
3851284f42a5SJens Freimann     }
385221e8709bSJuan Quintela     primary = failover_find_primary_device(n);
385321e8709bSJuan Quintela     return primary ? primary->pending_deleted_event : false;
38549711cd0dSJens Freimann }
38559711cd0dSJens Freimann 
38569711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque)
38579711cd0dSJens Freimann {
38589711cd0dSJens Freimann     DeviceState *dev = opaque;
38599711cd0dSJens Freimann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
38609711cd0dSJens Freimann 
38619711cd0dSJens Freimann     return vdc->primary_unplug_pending(dev);
38629711cd0dSJens Freimann }
38639711cd0dSJens Freimann 
3864c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
3865c255488dSJonah Palmer {
3866c255488dSJonah Palmer     VirtIONet *n = VIRTIO_NET(vdev);
3867c255488dSJonah Palmer     NetClientState *nc = qemu_get_queue(n->nic);
3868c255488dSJonah Palmer     struct vhost_net *net = get_vhost_net(nc->peer);
3869c255488dSJonah Palmer     return &net->dev;
3870c255488dSJonah Palmer }
3871c255488dSJonah Palmer 
38724d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
38734d45dcfbSHalil Pasic     .name = "virtio-net",
38744d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
38754d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
38764d45dcfbSHalil Pasic     .fields = (VMStateField[]) {
38774d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
38784d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
38794d45dcfbSHalil Pasic     },
38804d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
38819711cd0dSJens Freimann     .dev_unplug_pending = dev_unplug_pending,
38824d45dcfbSHalil Pasic };
3883290c2428SDr. David Alan Gilbert 
388417ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
3885127833eeSJason Baron     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3886127833eeSJason Baron                     VIRTIO_NET_F_CSUM, true),
3887127833eeSJason Baron     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
388887108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
3889127833eeSJason Baron     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3890127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
389187108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
3892127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
389387108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
3894127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
389587108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
3896127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
389787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
3898127833eeSJason Baron     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
389987108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3900127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
390187108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
3902127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
390387108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
3904127833eeSJason Baron     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
390587108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
3906127833eeSJason Baron     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
390787108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
3908127833eeSJason Baron     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
390987108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
3910127833eeSJason Baron     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
391187108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
3912127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
391387108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
3914127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
391587108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
3916127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
391787108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
3918127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
391987108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3920127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
392187108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3922127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
392387108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3924127833eeSJason Baron     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
392559079029SYuri Benditovich     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
392659079029SYuri Benditovich                     VIRTIO_NET_F_RSS, false),
3927e22f0603SYuri Benditovich     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3928e22f0603SYuri Benditovich                     VIRTIO_NET_F_HASH_REPORT, false),
39292974e916SYuri Benditovich     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
39302974e916SYuri Benditovich                     VIRTIO_NET_F_RSC_EXT, false),
39312974e916SYuri Benditovich     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
39322974e916SYuri Benditovich                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
393317ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
393417ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
393517ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
393617ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
393717ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
39381c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
39391c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
39409b02e161SWei Wang     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
39419b02e161SWei Wang                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3942a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
394375ebec11SMaxime Coquelin     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
394475ebec11SMaxime Coquelin                      true),
39459473939eSJason Baron     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
39469473939eSJason Baron     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
39479711cd0dSJens Freimann     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
3948*53da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("guest_uso4", VirtIONet, host_features,
3949*53da8b5aSYuri Benditovich                       VIRTIO_NET_F_GUEST_USO4, true),
3950*53da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("guest_uso6", VirtIONet, host_features,
3951*53da8b5aSYuri Benditovich                       VIRTIO_NET_F_GUEST_USO6, true),
3952*53da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
3953*53da8b5aSYuri Benditovich                       VIRTIO_NET_F_HOST_USO, true),
395417ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
395517ec5a86SKONRAD Frederic };
395617ec5a86SKONRAD Frederic 
395717ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
395817ec5a86SKONRAD Frederic {
395917ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
396017ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3961e6f746b3SAndreas Färber 
39624f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
3963290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
3964125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3965e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
3966306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
396717ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
396817ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
396917ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
397017ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
397117ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
397217ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
39737dc6be52SXuan Zhuo     vdc->queue_reset = virtio_net_queue_reset;
39747f863302SKangjie Xu     vdc->queue_enable = virtio_net_queue_enable;
397517ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
397617ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
397717ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
39782a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
39797788c3f2SMikhail Sennikovsky     vdc->post_load = virtio_net_post_load_virtio;
3980982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
39819711cd0dSJens Freimann     vdc->primary_unplug_pending = primary_unplug_pending;
3982c255488dSJonah Palmer     vdc->get_vhost = virtio_net_get_vhost;
3983cd9b8346SViktor Prutyanov     vdc->toggle_device_iotlb = vhost_toggle_device_iotlb;
398417ec5a86SKONRAD Frederic }
398517ec5a86SKONRAD Frederic 
398617ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
398717ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
398817ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
398917ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
399017ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
399117ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
399217ec5a86SKONRAD Frederic };
399317ec5a86SKONRAD Frederic 
399417ec5a86SKONRAD Frederic static void virtio_register_types(void)
399517ec5a86SKONRAD Frederic {
399617ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
399717ec5a86SKONRAD Frederic }
399817ec5a86SKONRAD Frederic 
399917ec5a86SKONRAD Frederic type_init(virtio_register_types)
4000