xref: /openbmc/qemu/hw/net/virtio-net.c (revision 2ab0ec31215e68f7af6b21b62e49141eb7c083e2)
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 
662bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
663e22f0603SYuri Benditovich                                        int version_1, int hash_report)
6646e790746SPaolo Bonzini {
6656e790746SPaolo Bonzini     int i;
6666e790746SPaolo Bonzini     NetClientState *nc;
6676e790746SPaolo Bonzini 
6686e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
6696e790746SPaolo Bonzini 
670bb9d17f8SCornelia Huck     if (version_1) {
671e22f0603SYuri Benditovich         n->guest_hdr_len = hash_report ?
672e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_v1_hash) :
673e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_mrg_rxbuf);
674e22f0603SYuri Benditovich         n->rss_data.populate_hash = !!hash_report;
675bb9d17f8SCornelia Huck     } else {
6766e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
677bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
678bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
679bb9d17f8SCornelia Huck     }
6806e790746SPaolo Bonzini 
681441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
6826e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
6836e790746SPaolo Bonzini 
6846e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
685d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
686d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
6876e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
6886e790746SPaolo Bonzini         }
6896e790746SPaolo Bonzini     }
6906e790746SPaolo Bonzini }
6916e790746SPaolo Bonzini 
6922eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n)
6932eef278bSMichael S. Tsirkin {
6942eef278bSMichael S. Tsirkin     NetClientState *peer = n->nic_conf.peers.ncs[0];
6952eef278bSMichael S. Tsirkin 
6962eef278bSMichael S. Tsirkin     /*
6970ea5778fSEugenio Pérez      * Backends other than vhost-user or vhost-vdpa don't support max queue
6980ea5778fSEugenio Pérez      * size.
6992eef278bSMichael S. Tsirkin      */
7002eef278bSMichael S. Tsirkin     if (!peer) {
7012eef278bSMichael S. Tsirkin         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
7022eef278bSMichael S. Tsirkin     }
7032eef278bSMichael S. Tsirkin 
7040ea5778fSEugenio Pérez     switch(peer->info->type) {
7050ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_USER:
7060ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_VDPA:
7072eef278bSMichael S. Tsirkin         return VIRTQUEUE_MAX_SIZE;
7080ea5778fSEugenio Pérez     default:
7090ea5778fSEugenio Pérez         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
7100ea5778fSEugenio Pérez     };
7112eef278bSMichael S. Tsirkin }
7122eef278bSMichael S. Tsirkin 
7136e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
7146e790746SPaolo Bonzini {
7156e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
7166e790746SPaolo Bonzini 
7176e790746SPaolo Bonzini     if (!nc->peer) {
7186e790746SPaolo Bonzini         return 0;
7196e790746SPaolo Bonzini     }
7206e790746SPaolo Bonzini 
721f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7227263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 1);
7237263a0adSChangchun Ouyang     }
7247263a0adSChangchun Ouyang 
725f394b2e2SEric Blake     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
7266e790746SPaolo Bonzini         return 0;
7276e790746SPaolo Bonzini     }
7286e790746SPaolo Bonzini 
729441537f1SJason Wang     if (n->max_queue_pairs == 1) {
7301074b879SJason Wang         return 0;
7311074b879SJason Wang     }
7321074b879SJason Wang 
7336e790746SPaolo Bonzini     return tap_enable(nc->peer);
7346e790746SPaolo Bonzini }
7356e790746SPaolo Bonzini 
7366e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
7376e790746SPaolo Bonzini {
7386e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
7396e790746SPaolo Bonzini 
7406e790746SPaolo Bonzini     if (!nc->peer) {
7416e790746SPaolo Bonzini         return 0;
7426e790746SPaolo Bonzini     }
7436e790746SPaolo Bonzini 
744f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7457263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 0);
7467263a0adSChangchun Ouyang     }
7477263a0adSChangchun Ouyang 
748f394b2e2SEric Blake     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
7496e790746SPaolo Bonzini         return 0;
7506e790746SPaolo Bonzini     }
7516e790746SPaolo Bonzini 
7526e790746SPaolo Bonzini     return tap_disable(nc->peer);
7536e790746SPaolo Bonzini }
7546e790746SPaolo Bonzini 
755441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n)
7566e790746SPaolo Bonzini {
7576e790746SPaolo Bonzini     int i;
758ddfa83eaSJoel Stanley     int r;
7596e790746SPaolo Bonzini 
76068b5f314SYuri Benditovich     if (n->nic->peer_deleted) {
76168b5f314SYuri Benditovich         return;
76268b5f314SYuri Benditovich     }
76368b5f314SYuri Benditovich 
764441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
765441537f1SJason Wang         if (i < n->curr_queue_pairs) {
766ddfa83eaSJoel Stanley             r = peer_attach(n, i);
767ddfa83eaSJoel Stanley             assert(!r);
7686e790746SPaolo Bonzini         } else {
769ddfa83eaSJoel Stanley             r = peer_detach(n, i);
770ddfa83eaSJoel Stanley             assert(!r);
7716e790746SPaolo Bonzini         }
7726e790746SPaolo Bonzini     }
7736e790746SPaolo Bonzini }
7746e790746SPaolo Bonzini 
775ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
7766e790746SPaolo Bonzini 
7779d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
7789d5b731dSJason Wang                                         Error **errp)
7796e790746SPaolo Bonzini {
78017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
7816e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
7826e790746SPaolo Bonzini 
783da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
784da3e8a23SShannon Zhao     features |= n->host_features;
785da3e8a23SShannon Zhao 
7860cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7876e790746SPaolo Bonzini 
7886e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
7890cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
7900cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
7910cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
7920cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
7936e790746SPaolo Bonzini 
7940cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
7950cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
7960cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
7970cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
798e22f0603SYuri Benditovich 
799e22f0603SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
8006e790746SPaolo Bonzini     }
8016e790746SPaolo Bonzini 
8026e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
8030cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
8040cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
8056e790746SPaolo Bonzini     }
8066e790746SPaolo Bonzini 
807ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
8086e790746SPaolo Bonzini         return features;
8096e790746SPaolo Bonzini     }
8102974e916SYuri Benditovich 
8110145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
81259079029SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
8130145c393SAndrew Melnychenko     }
81475ebec11SMaxime Coquelin     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
81575ebec11SMaxime Coquelin     vdev->backend_features = features;
81675ebec11SMaxime Coquelin 
81775ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
81875ebec11SMaxime Coquelin             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
81975ebec11SMaxime Coquelin         features |= (1ULL << VIRTIO_NET_F_MTU);
82075ebec11SMaxime Coquelin     }
82175ebec11SMaxime Coquelin 
822cd69d47cSEugenio Pérez     /*
823cd69d47cSEugenio Pérez      * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
824cd69d47cSEugenio Pérez      * enabled. This happens in the vDPA case.
825cd69d47cSEugenio Pérez      *
826cd69d47cSEugenio Pérez      * Make sure the feature set is not incoherent, as the driver could refuse
827cd69d47cSEugenio Pérez      * to start.
828cd69d47cSEugenio Pérez      *
829cd69d47cSEugenio Pérez      * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
830cd69d47cSEugenio Pérez      * helping guest to notify the new location with vDPA devices that does not
831cd69d47cSEugenio Pérez      * support it.
832cd69d47cSEugenio Pérez      */
833cd69d47cSEugenio Pérez     if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) {
834cd69d47cSEugenio Pérez         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE);
835cd69d47cSEugenio Pérez     }
836cd69d47cSEugenio Pérez 
83775ebec11SMaxime Coquelin     return features;
8386e790746SPaolo Bonzini }
8396e790746SPaolo Bonzini 
840019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
8416e790746SPaolo Bonzini {
842019a3edbSGerd Hoffmann     uint64_t features = 0;
8436e790746SPaolo Bonzini 
8446e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
8456e790746SPaolo Bonzini      * but also these: */
8460cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
8470cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
8480cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
8490cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
8500cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
8516e790746SPaolo Bonzini 
8526e790746SPaolo Bonzini     return features;
8536e790746SPaolo Bonzini }
8546e790746SPaolo Bonzini 
855644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
856644c9858SDmitry Fleytman {
857ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
858644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
859644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
860644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
861644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
862*2ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)),
863*2ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)),
864*2ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6)));
865644c9858SDmitry Fleytman }
866644c9858SDmitry Fleytman 
867644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
868644c9858SDmitry Fleytman {
869644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
870644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
871644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
872644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
873644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
874644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
875644c9858SDmitry Fleytman 
876644c9858SDmitry Fleytman     return guest_offloads_mask & features;
877644c9858SDmitry Fleytman }
878644c9858SDmitry Fleytman 
8790b545b1eSHawkins Jiawei uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n)
880644c9858SDmitry Fleytman {
881644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
882644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
883644c9858SDmitry Fleytman }
884644c9858SDmitry Fleytman 
885f5e1847bSJuan Quintela typedef struct {
886f5e1847bSJuan Quintela     VirtIONet *n;
88712b2fad7SKevin Wolf     DeviceState *dev;
88812b2fad7SKevin Wolf } FailoverDevice;
889f5e1847bSJuan Quintela 
890f5e1847bSJuan Quintela /**
89112b2fad7SKevin Wolf  * Set the failover primary device
892f5e1847bSJuan Quintela  *
893f5e1847bSJuan Quintela  * @opaque: FailoverId to setup
894f5e1847bSJuan Quintela  * @opts: opts for device we are handling
895f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
896f5e1847bSJuan Quintela  */
89712b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque)
898f5e1847bSJuan Quintela {
89912b2fad7SKevin Wolf     FailoverDevice *fdev = opaque;
90012b2fad7SKevin Wolf     PCIDevice *pci_dev = (PCIDevice *)
90112b2fad7SKevin Wolf         object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE);
902f5e1847bSJuan Quintela 
90312b2fad7SKevin Wolf     if (!pci_dev) {
90412b2fad7SKevin Wolf         return 0;
90512b2fad7SKevin Wolf     }
90612b2fad7SKevin Wolf 
90712b2fad7SKevin Wolf     if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) {
90812b2fad7SKevin Wolf         fdev->dev = dev;
909f5e1847bSJuan Quintela         return 1;
910f5e1847bSJuan Quintela     }
911f5e1847bSJuan Quintela 
912f5e1847bSJuan Quintela     return 0;
913f5e1847bSJuan Quintela }
914f5e1847bSJuan Quintela 
915f5e1847bSJuan Quintela /**
91685d3b931SJuan Quintela  * Find the primary device for this failover virtio-net
91785d3b931SJuan Quintela  *
91885d3b931SJuan Quintela  * @n: VirtIONet device
91985d3b931SJuan Quintela  * @errp: returns an error if this function fails
92085d3b931SJuan Quintela  */
9210a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n)
9229711cd0dSJens Freimann {
92312b2fad7SKevin Wolf     FailoverDevice fdev = {
92412b2fad7SKevin Wolf         .n = n,
92512b2fad7SKevin Wolf     };
9269711cd0dSJens Freimann 
92712b2fad7SKevin Wolf     qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL,
92812b2fad7SKevin Wolf                        NULL, NULL, &fdev);
92912b2fad7SKevin Wolf     return fdev.dev;
9309711cd0dSJens Freimann }
9319711cd0dSJens Freimann 
93221e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp)
93321e8709bSJuan Quintela {
93421e8709bSJuan Quintela     Error *err = NULL;
93521e8709bSJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
93621e8709bSJuan Quintela 
93721e8709bSJuan Quintela     if (dev) {
93821e8709bSJuan Quintela         return;
93921e8709bSJuan Quintela     }
94021e8709bSJuan Quintela 
941259a10dbSKevin Wolf     if (!n->primary_opts) {
94297ca9c59SLaurent Vivier         error_setg(errp, "Primary device not found");
94397ca9c59SLaurent Vivier         error_append_hint(errp, "Virtio-net failover will not work. Make "
94497ca9c59SLaurent Vivier                           "sure primary device has parameter"
94597ca9c59SLaurent Vivier                           " failover_pair_id=%s\n", n->netclient_name);
94621e8709bSJuan Quintela         return;
94721e8709bSJuan Quintela     }
948259a10dbSKevin Wolf 
949f3558b1bSKevin Wolf     dev = qdev_device_add_from_qdict(n->primary_opts,
950f3558b1bSKevin Wolf                                      n->primary_opts_from_json,
951f3558b1bSKevin Wolf                                      &err);
95221e8709bSJuan Quintela     if (err) {
953f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
954259a10dbSKevin Wolf         n->primary_opts = NULL;
95500e7b129SLaurent Vivier     } else {
95600e7b129SLaurent Vivier         object_unref(OBJECT(dev));
95721e8709bSJuan Quintela     }
95821e8709bSJuan Quintela     error_propagate(errp, err);
95921e8709bSJuan Quintela }
96021e8709bSJuan Quintela 
961d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
9626e790746SPaolo Bonzini {
96317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
9649711cd0dSJens Freimann     Error *err = NULL;
9656e790746SPaolo Bonzini     int i;
9666e790746SPaolo Bonzini 
96775ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
96875ebec11SMaxime Coquelin             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
96975ebec11SMaxime Coquelin         features &= ~(1ULL << VIRTIO_NET_F_MTU);
97075ebec11SMaxime Coquelin     }
97175ebec11SMaxime Coquelin 
972ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
97359079029SYuri Benditovich                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
97495129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
9756e790746SPaolo Bonzini 
976ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
97795129d6fSCornelia Huck                                virtio_has_feature(features,
978bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
97995129d6fSCornelia Huck                                virtio_has_feature(features,
980e22f0603SYuri Benditovich                                                   VIRTIO_F_VERSION_1),
981e22f0603SYuri Benditovich                                virtio_has_feature(features,
982e22f0603SYuri Benditovich                                                   VIRTIO_NET_F_HASH_REPORT));
9836e790746SPaolo Bonzini 
9842974e916SYuri Benditovich     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9852974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
9862974e916SYuri Benditovich     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9872974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
988e22f0603SYuri Benditovich     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
9892974e916SYuri Benditovich 
9906e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
991644c9858SDmitry Fleytman         n->curr_guest_offloads =
992644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
993644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
9946e790746SPaolo Bonzini     }
9956e790746SPaolo Bonzini 
996441537f1SJason Wang     for (i = 0;  i < n->max_queue_pairs; i++) {
9976e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
9986e790746SPaolo Bonzini 
999ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
10006e790746SPaolo Bonzini             continue;
10016e790746SPaolo Bonzini         }
1002ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
1003c9bdc449SHyman Huang(黄勇) 
1004c9bdc449SHyman Huang(黄勇)         /*
1005c9bdc449SHyman Huang(黄勇)          * keep acked_features in NetVhostUserState up-to-date so it
1006c9bdc449SHyman Huang(黄勇)          * can't miss any features configured by guest virtio driver.
1007c9bdc449SHyman Huang(黄勇)          */
1008c9bdc449SHyman Huang(黄勇)         vhost_net_save_acked_features(nc->peer);
10096e790746SPaolo Bonzini     }
10100b1eaa88SStefan Fritsch 
101195129d6fSCornelia Huck     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
10120b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
10130b1eaa88SStefan Fritsch     } else {
10140b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
10150b1eaa88SStefan Fritsch     }
10169711cd0dSJens Freimann 
10179711cd0dSJens Freimann     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
10189711cd0dSJens Freimann         qapi_event_send_failover_negotiated(n->netclient_name);
1019e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, false);
10209711cd0dSJens Freimann         failover_add_primary(n, &err);
10219711cd0dSJens Freimann         if (err) {
10221b529d90SLaurent Vivier             if (!qtest_enabled()) {
10239711cd0dSJens Freimann                 warn_report_err(err);
10241b529d90SLaurent Vivier             } else {
10251b529d90SLaurent Vivier                 error_free(err);
10261b529d90SLaurent Vivier             }
10279711cd0dSJens Freimann         }
10286e790746SPaolo Bonzini     }
102921e8709bSJuan Quintela }
10306e790746SPaolo Bonzini 
10316e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
10326e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
10336e790746SPaolo Bonzini {
10346e790746SPaolo Bonzini     uint8_t on;
10356e790746SPaolo Bonzini     size_t s;
1036b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
10376e790746SPaolo Bonzini 
10386e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
10396e790746SPaolo Bonzini     if (s != sizeof(on)) {
10406e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10416e790746SPaolo Bonzini     }
10426e790746SPaolo Bonzini 
10436e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
10446e790746SPaolo Bonzini         n->promisc = on;
10456e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
10466e790746SPaolo Bonzini         n->allmulti = on;
10476e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
10486e790746SPaolo Bonzini         n->alluni = on;
10496e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
10506e790746SPaolo Bonzini         n->nomulti = on;
10516e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
10526e790746SPaolo Bonzini         n->nouni = on;
10536e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
10546e790746SPaolo Bonzini         n->nobcast = on;
10556e790746SPaolo Bonzini     } else {
10566e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10576e790746SPaolo Bonzini     }
10586e790746SPaolo Bonzini 
1059b1be4280SAmos Kong     rxfilter_notify(nc);
1060b1be4280SAmos Kong 
10616e790746SPaolo Bonzini     return VIRTIO_NET_OK;
10626e790746SPaolo Bonzini }
10636e790746SPaolo Bonzini 
1064644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
1065644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
1066644c9858SDmitry Fleytman {
1067644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1068644c9858SDmitry Fleytman     uint64_t offloads;
1069644c9858SDmitry Fleytman     size_t s;
1070644c9858SDmitry Fleytman 
107195129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1072644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1073644c9858SDmitry Fleytman     }
1074644c9858SDmitry Fleytman 
1075644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
1076644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
1077644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1078644c9858SDmitry Fleytman     }
1079644c9858SDmitry Fleytman 
1080644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
1081644c9858SDmitry Fleytman         uint64_t supported_offloads;
1082644c9858SDmitry Fleytman 
1083189ae6bbSJason Wang         offloads = virtio_ldq_p(vdev, &offloads);
1084189ae6bbSJason Wang 
1085644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
1086644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1087644c9858SDmitry Fleytman         }
1088644c9858SDmitry Fleytman 
10892974e916SYuri Benditovich         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10902974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
10912974e916SYuri Benditovich         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10922974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
10932974e916SYuri Benditovich         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
10942974e916SYuri Benditovich 
1095644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
1096644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
1097644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1098644c9858SDmitry Fleytman         }
1099644c9858SDmitry Fleytman 
1100644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
1101644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1102644c9858SDmitry Fleytman 
1103644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
1104644c9858SDmitry Fleytman     } else {
1105644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1106644c9858SDmitry Fleytman     }
1107644c9858SDmitry Fleytman }
1108644c9858SDmitry Fleytman 
11096e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
11106e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
11116e790746SPaolo Bonzini {
11121399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11136e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
11146e790746SPaolo Bonzini     size_t s;
1115b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11166e790746SPaolo Bonzini 
11176e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
11186e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
11196e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
11206e790746SPaolo Bonzini         }
11216e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
11226e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
11236e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1124b1be4280SAmos Kong         rxfilter_notify(nc);
1125b1be4280SAmos Kong 
11266e790746SPaolo Bonzini         return VIRTIO_NET_OK;
11276e790746SPaolo Bonzini     }
11286e790746SPaolo Bonzini 
11296e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
11306e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11316e790746SPaolo Bonzini     }
11326e790746SPaolo Bonzini 
1133cae2e556SAmos Kong     int in_use = 0;
1134cae2e556SAmos Kong     int first_multi = 0;
1135cae2e556SAmos Kong     uint8_t uni_overflow = 0;
1136cae2e556SAmos Kong     uint8_t multi_overflow = 0;
1137cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
11386e790746SPaolo Bonzini 
11396e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11406e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11411399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11426e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1143b1be4280SAmos Kong         goto error;
11446e790746SPaolo Bonzini     }
11456e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11466e790746SPaolo Bonzini 
11476e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1148b1be4280SAmos Kong         goto error;
11496e790746SPaolo Bonzini     }
11506e790746SPaolo Bonzini 
11516e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1152cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
11536e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
11546e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1155b1be4280SAmos Kong             goto error;
11566e790746SPaolo Bonzini         }
1157cae2e556SAmos Kong         in_use += mac_data.entries;
11586e790746SPaolo Bonzini     } else {
1159cae2e556SAmos Kong         uni_overflow = 1;
11606e790746SPaolo Bonzini     }
11616e790746SPaolo Bonzini 
11626e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
11636e790746SPaolo Bonzini 
1164cae2e556SAmos Kong     first_multi = in_use;
11656e790746SPaolo Bonzini 
11666e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11676e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11681399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11696e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1170b1be4280SAmos Kong         goto error;
11716e790746SPaolo Bonzini     }
11726e790746SPaolo Bonzini 
11736e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11746e790746SPaolo Bonzini 
11756e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1176b1be4280SAmos Kong         goto error;
11776e790746SPaolo Bonzini     }
11786e790746SPaolo Bonzini 
1179edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1180cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
11816e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
11826e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1183b1be4280SAmos Kong             goto error;
11846e790746SPaolo Bonzini         }
1185cae2e556SAmos Kong         in_use += mac_data.entries;
11866e790746SPaolo Bonzini     } else {
1187cae2e556SAmos Kong         multi_overflow = 1;
11886e790746SPaolo Bonzini     }
11896e790746SPaolo Bonzini 
1190cae2e556SAmos Kong     n->mac_table.in_use = in_use;
1191cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
1192cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
1193cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
1194cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1195cae2e556SAmos Kong     g_free(macs);
1196b1be4280SAmos Kong     rxfilter_notify(nc);
1197b1be4280SAmos Kong 
11986e790746SPaolo Bonzini     return VIRTIO_NET_OK;
1199b1be4280SAmos Kong 
1200b1be4280SAmos Kong error:
1201cae2e556SAmos Kong     g_free(macs);
1202b1be4280SAmos Kong     return VIRTIO_NET_ERR;
12036e790746SPaolo Bonzini }
12046e790746SPaolo Bonzini 
12056e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
12066e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
12076e790746SPaolo Bonzini {
12081399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12096e790746SPaolo Bonzini     uint16_t vid;
12106e790746SPaolo Bonzini     size_t s;
1211b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
12126e790746SPaolo Bonzini 
12136e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
12141399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
12156e790746SPaolo Bonzini     if (s != sizeof(vid)) {
12166e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12176e790746SPaolo Bonzini     }
12186e790746SPaolo Bonzini 
12196e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
12206e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12216e790746SPaolo Bonzini 
12226e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
12236e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
12246e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
12256e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
12266e790746SPaolo Bonzini     else
12276e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12286e790746SPaolo Bonzini 
1229b1be4280SAmos Kong     rxfilter_notify(nc);
1230b1be4280SAmos Kong 
12316e790746SPaolo Bonzini     return VIRTIO_NET_OK;
12326e790746SPaolo Bonzini }
12336e790746SPaolo Bonzini 
1234f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1235f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
1236f57fcf70SJason Wang {
12379d8c6a25SDr. David Alan Gilbert     trace_virtio_net_handle_announce(n->announce_timer.round);
1238f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1239f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
1240f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
12419d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
12429d8c6a25SDr. David Alan Gilbert             qemu_announce_timer_step(&n->announce_timer);
1243f57fcf70SJason Wang         }
1244f57fcf70SJason Wang         return VIRTIO_NET_OK;
1245f57fcf70SJason Wang     } else {
1246f57fcf70SJason Wang         return VIRTIO_NET_ERR;
1247f57fcf70SJason Wang     }
1248f57fcf70SJason Wang }
1249f57fcf70SJason Wang 
12500145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n);
12510145c393SAndrew Melnychenko 
125259079029SYuri Benditovich static void virtio_net_disable_rss(VirtIONet *n)
125359079029SYuri Benditovich {
125459079029SYuri Benditovich     if (n->rss_data.enabled) {
125559079029SYuri Benditovich         trace_virtio_net_rss_disable();
125659079029SYuri Benditovich     }
125759079029SYuri Benditovich     n->rss_data.enabled = false;
12580145c393SAndrew Melnychenko 
12590145c393SAndrew Melnychenko     virtio_net_detach_epbf_rss(n);
12600145c393SAndrew Melnychenko }
12610145c393SAndrew Melnychenko 
12620145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd)
12630145c393SAndrew Melnychenko {
12640145c393SAndrew Melnychenko     NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0);
12650145c393SAndrew Melnychenko     if (nc == NULL || nc->info->set_steering_ebpf == NULL) {
12660145c393SAndrew Melnychenko         return false;
12670145c393SAndrew Melnychenko     }
12680145c393SAndrew Melnychenko 
12690145c393SAndrew Melnychenko     return nc->info->set_steering_ebpf(nc, prog_fd);
12700145c393SAndrew Melnychenko }
12710145c393SAndrew Melnychenko 
12720145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data,
12730145c393SAndrew Melnychenko                                    struct EBPFRSSConfig *config)
12740145c393SAndrew Melnychenko {
12750145c393SAndrew Melnychenko     config->redirect = data->redirect;
12760145c393SAndrew Melnychenko     config->populate_hash = data->populate_hash;
12770145c393SAndrew Melnychenko     config->hash_types = data->hash_types;
12780145c393SAndrew Melnychenko     config->indirections_len = data->indirections_len;
12790145c393SAndrew Melnychenko     config->default_queue = data->default_queue;
12800145c393SAndrew Melnychenko }
12810145c393SAndrew Melnychenko 
12820145c393SAndrew Melnychenko static bool virtio_net_attach_epbf_rss(VirtIONet *n)
12830145c393SAndrew Melnychenko {
12840145c393SAndrew Melnychenko     struct EBPFRSSConfig config = {};
12850145c393SAndrew Melnychenko 
12860145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
12870145c393SAndrew Melnychenko         return false;
12880145c393SAndrew Melnychenko     }
12890145c393SAndrew Melnychenko 
12900145c393SAndrew Melnychenko     rss_data_to_rss_config(&n->rss_data, &config);
12910145c393SAndrew Melnychenko 
12920145c393SAndrew Melnychenko     if (!ebpf_rss_set_all(&n->ebpf_rss, &config,
12930145c393SAndrew Melnychenko                           n->rss_data.indirections_table, n->rss_data.key)) {
12940145c393SAndrew Melnychenko         return false;
12950145c393SAndrew Melnychenko     }
12960145c393SAndrew Melnychenko 
12970145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) {
12980145c393SAndrew Melnychenko         return false;
12990145c393SAndrew Melnychenko     }
13000145c393SAndrew Melnychenko 
13010145c393SAndrew Melnychenko     return true;
13020145c393SAndrew Melnychenko }
13030145c393SAndrew Melnychenko 
13040145c393SAndrew Melnychenko static void virtio_net_detach_epbf_rss(VirtIONet *n)
13050145c393SAndrew Melnychenko {
13060145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
13070145c393SAndrew Melnychenko }
13080145c393SAndrew Melnychenko 
13090145c393SAndrew Melnychenko static bool virtio_net_load_ebpf(VirtIONet *n)
13100145c393SAndrew Melnychenko {
13110145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
13120145c393SAndrew Melnychenko         /* backend does't support steering ebpf */
13130145c393SAndrew Melnychenko         return false;
13140145c393SAndrew Melnychenko     }
13150145c393SAndrew Melnychenko 
13160145c393SAndrew Melnychenko     return ebpf_rss_load(&n->ebpf_rss);
13170145c393SAndrew Melnychenko }
13180145c393SAndrew Melnychenko 
13190145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n)
13200145c393SAndrew Melnychenko {
13210145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
13220145c393SAndrew Melnychenko     ebpf_rss_unload(&n->ebpf_rss);
132359079029SYuri Benditovich }
132459079029SYuri Benditovich 
132559079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n,
1326e22f0603SYuri Benditovich                                       struct iovec *iov,
1327e22f0603SYuri Benditovich                                       unsigned int iov_cnt,
1328e22f0603SYuri Benditovich                                       bool do_rss)
132959079029SYuri Benditovich {
133059079029SYuri Benditovich     VirtIODevice *vdev = VIRTIO_DEVICE(n);
133159079029SYuri Benditovich     struct virtio_net_rss_config cfg;
133259079029SYuri Benditovich     size_t s, offset = 0, size_get;
1333441537f1SJason Wang     uint16_t queue_pairs, i;
133459079029SYuri Benditovich     struct {
133559079029SYuri Benditovich         uint16_t us;
133659079029SYuri Benditovich         uint8_t b;
133759079029SYuri Benditovich     } QEMU_PACKED temp;
133859079029SYuri Benditovich     const char *err_msg = "";
133959079029SYuri Benditovich     uint32_t err_value = 0;
134059079029SYuri Benditovich 
1341e22f0603SYuri Benditovich     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
134259079029SYuri Benditovich         err_msg = "RSS is not negotiated";
134359079029SYuri Benditovich         goto error;
134459079029SYuri Benditovich     }
1345e22f0603SYuri Benditovich     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1346e22f0603SYuri Benditovich         err_msg = "Hash report is not negotiated";
1347e22f0603SYuri Benditovich         goto error;
1348e22f0603SYuri Benditovich     }
134959079029SYuri Benditovich     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
135059079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
135159079029SYuri Benditovich     if (s != size_get) {
135259079029SYuri Benditovich         err_msg = "Short command buffer";
135359079029SYuri Benditovich         err_value = (uint32_t)s;
135459079029SYuri Benditovich         goto error;
135559079029SYuri Benditovich     }
135659079029SYuri Benditovich     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
135759079029SYuri Benditovich     n->rss_data.indirections_len =
135859079029SYuri Benditovich         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
135959079029SYuri Benditovich     n->rss_data.indirections_len++;
1360e22f0603SYuri Benditovich     if (!do_rss) {
1361e22f0603SYuri Benditovich         n->rss_data.indirections_len = 1;
1362e22f0603SYuri Benditovich     }
136359079029SYuri Benditovich     if (!is_power_of_2(n->rss_data.indirections_len)) {
136459079029SYuri Benditovich         err_msg = "Invalid size of indirection table";
136559079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
136659079029SYuri Benditovich         goto error;
136759079029SYuri Benditovich     }
136859079029SYuri Benditovich     if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
136959079029SYuri Benditovich         err_msg = "Too large indirection table";
137059079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
137159079029SYuri Benditovich         goto error;
137259079029SYuri Benditovich     }
1373e22f0603SYuri Benditovich     n->rss_data.default_queue = do_rss ?
1374e22f0603SYuri Benditovich         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1375441537f1SJason Wang     if (n->rss_data.default_queue >= n->max_queue_pairs) {
137659079029SYuri Benditovich         err_msg = "Invalid default queue";
137759079029SYuri Benditovich         err_value = n->rss_data.default_queue;
137859079029SYuri Benditovich         goto error;
137959079029SYuri Benditovich     }
138059079029SYuri Benditovich     offset += size_get;
138159079029SYuri Benditovich     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
138259079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
138359079029SYuri Benditovich     n->rss_data.indirections_table = g_malloc(size_get);
138459079029SYuri Benditovich     if (!n->rss_data.indirections_table) {
138559079029SYuri Benditovich         err_msg = "Can't allocate indirections table";
138659079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
138759079029SYuri Benditovich         goto error;
138859079029SYuri Benditovich     }
138959079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset,
139059079029SYuri Benditovich                    n->rss_data.indirections_table, size_get);
139159079029SYuri Benditovich     if (s != size_get) {
139259079029SYuri Benditovich         err_msg = "Short indirection table buffer";
139359079029SYuri Benditovich         err_value = (uint32_t)s;
139459079029SYuri Benditovich         goto error;
139559079029SYuri Benditovich     }
139659079029SYuri Benditovich     for (i = 0; i < n->rss_data.indirections_len; ++i) {
139759079029SYuri Benditovich         uint16_t val = n->rss_data.indirections_table[i];
139859079029SYuri Benditovich         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
139959079029SYuri Benditovich     }
140059079029SYuri Benditovich     offset += size_get;
140159079029SYuri Benditovich     size_get = sizeof(temp);
140259079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
140359079029SYuri Benditovich     if (s != size_get) {
1404441537f1SJason Wang         err_msg = "Can't get queue_pairs";
140559079029SYuri Benditovich         err_value = (uint32_t)s;
140659079029SYuri Benditovich         goto error;
140759079029SYuri Benditovich     }
1408441537f1SJason Wang     queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs;
1409441537f1SJason Wang     if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) {
1410441537f1SJason Wang         err_msg = "Invalid number of queue_pairs";
1411441537f1SJason Wang         err_value = queue_pairs;
141259079029SYuri Benditovich         goto error;
141359079029SYuri Benditovich     }
141459079029SYuri Benditovich     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
141559079029SYuri Benditovich         err_msg = "Invalid key size";
141659079029SYuri Benditovich         err_value = temp.b;
141759079029SYuri Benditovich         goto error;
141859079029SYuri Benditovich     }
141959079029SYuri Benditovich     if (!temp.b && n->rss_data.hash_types) {
142059079029SYuri Benditovich         err_msg = "No key provided";
142159079029SYuri Benditovich         err_value = 0;
142259079029SYuri Benditovich         goto error;
142359079029SYuri Benditovich     }
142459079029SYuri Benditovich     if (!temp.b && !n->rss_data.hash_types) {
142559079029SYuri Benditovich         virtio_net_disable_rss(n);
1426441537f1SJason Wang         return queue_pairs;
142759079029SYuri Benditovich     }
142859079029SYuri Benditovich     offset += size_get;
142959079029SYuri Benditovich     size_get = temp.b;
143059079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
143159079029SYuri Benditovich     if (s != size_get) {
143259079029SYuri Benditovich         err_msg = "Can get key buffer";
143359079029SYuri Benditovich         err_value = (uint32_t)s;
143459079029SYuri Benditovich         goto error;
143559079029SYuri Benditovich     }
143659079029SYuri Benditovich     n->rss_data.enabled = true;
14370145c393SAndrew Melnychenko 
14380145c393SAndrew Melnychenko     if (!n->rss_data.populate_hash) {
14390145c393SAndrew Melnychenko         if (!virtio_net_attach_epbf_rss(n)) {
14400145c393SAndrew Melnychenko             /* EBPF must be loaded for vhost */
14410145c393SAndrew Melnychenko             if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
14420145c393SAndrew Melnychenko                 warn_report("Can't load eBPF RSS for vhost");
14430145c393SAndrew Melnychenko                 goto error;
14440145c393SAndrew Melnychenko             }
14450145c393SAndrew Melnychenko             /* fallback to software RSS */
14460145c393SAndrew Melnychenko             warn_report("Can't load eBPF RSS - fallback to software RSS");
14470145c393SAndrew Melnychenko             n->rss_data.enabled_software_rss = true;
14480145c393SAndrew Melnychenko         }
14490145c393SAndrew Melnychenko     } else {
14500145c393SAndrew Melnychenko         /* use software RSS for hash populating */
14510145c393SAndrew Melnychenko         /* and detach eBPF if was loaded before */
14520145c393SAndrew Melnychenko         virtio_net_detach_epbf_rss(n);
14530145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = true;
14540145c393SAndrew Melnychenko     }
14550145c393SAndrew Melnychenko 
145659079029SYuri Benditovich     trace_virtio_net_rss_enable(n->rss_data.hash_types,
145759079029SYuri Benditovich                                 n->rss_data.indirections_len,
145859079029SYuri Benditovich                                 temp.b);
1459441537f1SJason Wang     return queue_pairs;
146059079029SYuri Benditovich error:
146159079029SYuri Benditovich     trace_virtio_net_rss_error(err_msg, err_value);
146259079029SYuri Benditovich     virtio_net_disable_rss(n);
146359079029SYuri Benditovich     return 0;
146459079029SYuri Benditovich }
146559079029SYuri Benditovich 
14666e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
14676e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
14686e790746SPaolo Bonzini {
146917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1470441537f1SJason Wang     uint16_t queue_pairs;
14712a7888ccSSi-Wei Liu     NetClientState *nc = qemu_get_queue(n->nic);
14726e790746SPaolo Bonzini 
147359079029SYuri Benditovich     virtio_net_disable_rss(n);
1474e22f0603SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1475441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false);
1476441537f1SJason Wang         return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1477e22f0603SYuri Benditovich     }
147859079029SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1479441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true);
148059079029SYuri Benditovich     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
148159079029SYuri Benditovich         struct virtio_net_ctrl_mq mq;
148259079029SYuri Benditovich         size_t s;
148359079029SYuri Benditovich         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
148459079029SYuri Benditovich             return VIRTIO_NET_ERR;
148559079029SYuri Benditovich         }
14866e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
14876e790746SPaolo Bonzini         if (s != sizeof(mq)) {
14886e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
14896e790746SPaolo Bonzini         }
1490441537f1SJason Wang         queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
14916e790746SPaolo Bonzini 
149259079029SYuri Benditovich     } else {
14936e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
14946e790746SPaolo Bonzini     }
14956e790746SPaolo Bonzini 
1496441537f1SJason Wang     if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1497441537f1SJason Wang         queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1498441537f1SJason Wang         queue_pairs > n->max_queue_pairs ||
14996e790746SPaolo Bonzini         !n->multiqueue) {
15006e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
15016e790746SPaolo Bonzini     }
15026e790746SPaolo Bonzini 
1503441537f1SJason Wang     n->curr_queue_pairs = queue_pairs;
1504ca8717f9SEugenio Pérez     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
1505ca8717f9SEugenio Pérez         /*
1506ca8717f9SEugenio Pérez          * Avoid updating the backend for a vdpa device: We're only interested
1507ca8717f9SEugenio Pérez          * in updating the device model queues.
1508ca8717f9SEugenio Pérez          */
1509ca8717f9SEugenio Pérez         return VIRTIO_NET_OK;
1510ca8717f9SEugenio Pérez     }
1511441537f1SJason Wang     /* stop the backend before changing the number of queue_pairs to avoid handling a
15126e790746SPaolo Bonzini      * disabled queue */
151317a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
1514441537f1SJason Wang     virtio_net_set_queue_pairs(n);
15156e790746SPaolo Bonzini 
15166e790746SPaolo Bonzini     return VIRTIO_NET_OK;
15176e790746SPaolo Bonzini }
1518ba7eadb5SGreg Kurz 
1519640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
1520640b8a1cSEugenio Pérez                                   const struct iovec *in_sg, unsigned in_num,
1521640b8a1cSEugenio Pérez                                   const struct iovec *out_sg,
1522640b8a1cSEugenio Pérez                                   unsigned out_num)
15236e790746SPaolo Bonzini {
152417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15256e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
15266e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
15276e790746SPaolo Bonzini     size_t s;
1528771b6ed3SJason Wang     struct iovec *iov, *iov2;
1529640b8a1cSEugenio Pérez 
1530640b8a1cSEugenio Pérez     if (iov_size(in_sg, in_num) < sizeof(status) ||
1531640b8a1cSEugenio Pérez         iov_size(out_sg, out_num) < sizeof(ctrl)) {
1532640b8a1cSEugenio Pérez         virtio_error(vdev, "virtio-net ctrl missing headers");
1533640b8a1cSEugenio Pérez         return 0;
1534640b8a1cSEugenio Pérez     }
1535640b8a1cSEugenio Pérez 
1536640b8a1cSEugenio Pérez     iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num);
1537640b8a1cSEugenio Pérez     s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl));
1538640b8a1cSEugenio Pérez     iov_discard_front(&iov, &out_num, sizeof(ctrl));
1539640b8a1cSEugenio Pérez     if (s != sizeof(ctrl)) {
1540640b8a1cSEugenio Pérez         status = VIRTIO_NET_ERR;
1541640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1542640b8a1cSEugenio Pérez         status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num);
1543640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1544640b8a1cSEugenio Pérez         status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num);
1545640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1546640b8a1cSEugenio Pérez         status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num);
1547640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1548640b8a1cSEugenio Pérez         status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num);
1549640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1550640b8a1cSEugenio Pérez         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
1551640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1552640b8a1cSEugenio Pérez         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
1553640b8a1cSEugenio Pérez     }
1554640b8a1cSEugenio Pérez 
1555640b8a1cSEugenio Pérez     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
1556640b8a1cSEugenio Pérez     assert(s == sizeof(status));
1557640b8a1cSEugenio Pérez 
1558640b8a1cSEugenio Pérez     g_free(iov2);
1559640b8a1cSEugenio Pérez     return sizeof(status);
1560640b8a1cSEugenio Pérez }
1561640b8a1cSEugenio Pérez 
1562640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1563640b8a1cSEugenio Pérez {
1564640b8a1cSEugenio Pérez     VirtQueueElement *elem;
15656e790746SPaolo Bonzini 
156651b19ebeSPaolo Bonzini     for (;;) {
1567640b8a1cSEugenio Pérez         size_t written;
156851b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
156951b19ebeSPaolo Bonzini         if (!elem) {
157051b19ebeSPaolo Bonzini             break;
157151b19ebeSPaolo Bonzini         }
1572640b8a1cSEugenio Pérez 
1573640b8a1cSEugenio Pérez         written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num,
1574640b8a1cSEugenio Pérez                                              elem->out_sg, elem->out_num);
1575640b8a1cSEugenio Pérez         if (written > 0) {
1576640b8a1cSEugenio Pérez             virtqueue_push(vq, elem, written);
1577640b8a1cSEugenio Pérez             virtio_notify(vdev, vq);
1578640b8a1cSEugenio Pérez             g_free(elem);
1579640b8a1cSEugenio Pérez         } else {
1580ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
1581ba7eadb5SGreg Kurz             g_free(elem);
1582ba7eadb5SGreg Kurz             break;
15836e790746SPaolo Bonzini         }
15846e790746SPaolo Bonzini     }
15856e790746SPaolo Bonzini }
15866e790746SPaolo Bonzini 
15876e790746SPaolo Bonzini /* RX */
15886e790746SPaolo Bonzini 
15896e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
15906e790746SPaolo Bonzini {
159117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15926e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
15936e790746SPaolo Bonzini 
15946e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
15956e790746SPaolo Bonzini }
15966e790746SPaolo Bonzini 
1597b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc)
15986e790746SPaolo Bonzini {
15996e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
160017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
16016e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
16026e790746SPaolo Bonzini 
160317a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
1604b8c4b67eSPhilippe Mathieu-Daudé         return false;
16056e790746SPaolo Bonzini     }
16066e790746SPaolo Bonzini 
1607441537f1SJason Wang     if (nc->queue_index >= n->curr_queue_pairs) {
1608b8c4b67eSPhilippe Mathieu-Daudé         return false;
16096e790746SPaolo Bonzini     }
16106e790746SPaolo Bonzini 
16116e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
161217a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1613b8c4b67eSPhilippe Mathieu-Daudé         return false;
16146e790746SPaolo Bonzini     }
16156e790746SPaolo Bonzini 
1616b8c4b67eSPhilippe Mathieu-Daudé     return true;
16176e790746SPaolo Bonzini }
16186e790746SPaolo Bonzini 
16196e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
16206e790746SPaolo Bonzini {
16216e790746SPaolo Bonzini     VirtIONet *n = q->n;
16226e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
16236e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
16246e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
16256e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
16266e790746SPaolo Bonzini 
16276e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
16286e790746SPaolo Bonzini          * available after the above check but before notification was
16296e790746SPaolo Bonzini          * enabled, check for available buffers again.
16306e790746SPaolo Bonzini          */
16316e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
16326e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
16336e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
16346e790746SPaolo Bonzini             return 0;
16356e790746SPaolo Bonzini         }
16366e790746SPaolo Bonzini     }
16376e790746SPaolo Bonzini 
16386e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
16396e790746SPaolo Bonzini     return 1;
16406e790746SPaolo Bonzini }
16416e790746SPaolo Bonzini 
16421399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1643032a74a1SCédric Le Goater {
16441399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
16451399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
16461399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
16471399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1648032a74a1SCédric Le Goater }
1649032a74a1SCédric Le Goater 
16506e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
16516e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
16526e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
16536e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
16546e790746SPaolo Bonzini  * dhclient yet.
16556e790746SPaolo Bonzini  *
16566e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
16576e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
16586e790746SPaolo Bonzini  * kernels.
16596e790746SPaolo Bonzini  *
16606e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
16616e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
16626e790746SPaolo Bonzini  * cache.
16636e790746SPaolo Bonzini  */
16646e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
16656e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
16666e790746SPaolo Bonzini {
16676e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
16686e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
16696e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
16706e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
16716e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1672f5746335SBin Meng         net_checksum_calculate(buf, size, CSUM_UDP);
16736e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
16746e790746SPaolo Bonzini     }
16756e790746SPaolo Bonzini }
16766e790746SPaolo Bonzini 
16776e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
16786e790746SPaolo Bonzini                            const void *buf, size_t size)
16796e790746SPaolo Bonzini {
16806e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
16816e790746SPaolo Bonzini         /* FIXME this cast is evil */
16826e790746SPaolo Bonzini         void *wbuf = (void *)buf;
16836e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
16846e790746SPaolo Bonzini                                     size - n->host_hdr_len);
16851bfa316cSGreg Kurz 
16861bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
16871399c60dSRusty Russell             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
16881bfa316cSGreg Kurz         }
16896e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
16906e790746SPaolo Bonzini     } else {
16916e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
16926e790746SPaolo Bonzini             .flags = 0,
16936e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
16946e790746SPaolo Bonzini         };
16956e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
16966e790746SPaolo Bonzini     }
16976e790746SPaolo Bonzini }
16986e790746SPaolo Bonzini 
16996e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
17006e790746SPaolo Bonzini {
17016e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
17026e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
17036e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
17046e790746SPaolo Bonzini     int i;
17056e790746SPaolo Bonzini 
17066e790746SPaolo Bonzini     if (n->promisc)
17076e790746SPaolo Bonzini         return 1;
17086e790746SPaolo Bonzini 
17096e790746SPaolo Bonzini     ptr += n->host_hdr_len;
17106e790746SPaolo Bonzini 
17116e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
17127542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
17136e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
17146e790746SPaolo Bonzini             return 0;
17156e790746SPaolo Bonzini     }
17166e790746SPaolo Bonzini 
17176e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
17186e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
17196e790746SPaolo Bonzini             return !n->nobcast;
17206e790746SPaolo Bonzini         } else if (n->nomulti) {
17216e790746SPaolo Bonzini             return 0;
17226e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
17236e790746SPaolo Bonzini             return 1;
17246e790746SPaolo Bonzini         }
17256e790746SPaolo Bonzini 
17266e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
17276e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17286e790746SPaolo Bonzini                 return 1;
17296e790746SPaolo Bonzini             }
17306e790746SPaolo Bonzini         }
17316e790746SPaolo Bonzini     } else { // unicast
17326e790746SPaolo Bonzini         if (n->nouni) {
17336e790746SPaolo Bonzini             return 0;
17346e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
17356e790746SPaolo Bonzini             return 1;
17366e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
17376e790746SPaolo Bonzini             return 1;
17386e790746SPaolo Bonzini         }
17396e790746SPaolo Bonzini 
17406e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
17416e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17426e790746SPaolo Bonzini                 return 1;
17436e790746SPaolo Bonzini             }
17446e790746SPaolo Bonzini         }
17456e790746SPaolo Bonzini     }
17466e790746SPaolo Bonzini 
17476e790746SPaolo Bonzini     return 0;
17486e790746SPaolo Bonzini }
17496e790746SPaolo Bonzini 
175069ff5ef8SAkihiko Odaki static uint8_t virtio_net_get_hash_type(bool hasip4,
175169ff5ef8SAkihiko Odaki                                         bool hasip6,
175265f474bbSAkihiko Odaki                                         EthL4HdrProto l4hdr_proto,
17534474e37aSYuri Benditovich                                         uint32_t types)
17544474e37aSYuri Benditovich {
175569ff5ef8SAkihiko Odaki     if (hasip4) {
175665f474bbSAkihiko Odaki         switch (l4hdr_proto) {
175765f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
175865f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
17594474e37aSYuri Benditovich                 return NetPktRssIpV4Tcp;
17604474e37aSYuri Benditovich             }
176165f474bbSAkihiko Odaki             break;
176265f474bbSAkihiko Odaki 
176365f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
176465f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
17654474e37aSYuri Benditovich                 return NetPktRssIpV4Udp;
17664474e37aSYuri Benditovich             }
176765f474bbSAkihiko Odaki             break;
176865f474bbSAkihiko Odaki 
176965f474bbSAkihiko Odaki         default:
177065f474bbSAkihiko Odaki             break;
177165f474bbSAkihiko Odaki         }
177265f474bbSAkihiko Odaki 
17734474e37aSYuri Benditovich         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
17744474e37aSYuri Benditovich             return NetPktRssIpV4;
17754474e37aSYuri Benditovich         }
177669ff5ef8SAkihiko Odaki     } else if (hasip6) {
177765f474bbSAkihiko Odaki         switch (l4hdr_proto) {
177865f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
177965f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) {
178065f474bbSAkihiko Odaki                 return NetPktRssIpV6TcpEx;
178165f474bbSAkihiko Odaki             }
178265f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
178365f474bbSAkihiko Odaki                 return NetPktRssIpV6Tcp;
178465f474bbSAkihiko Odaki             }
178565f474bbSAkihiko Odaki             break;
17864474e37aSYuri Benditovich 
178765f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
178865f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) {
178965f474bbSAkihiko Odaki                 return NetPktRssIpV6UdpEx;
17904474e37aSYuri Benditovich             }
179165f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
179265f474bbSAkihiko Odaki                 return NetPktRssIpV6Udp;
17934474e37aSYuri Benditovich             }
179465f474bbSAkihiko Odaki             break;
179565f474bbSAkihiko Odaki 
179665f474bbSAkihiko Odaki         default:
179765f474bbSAkihiko Odaki             break;
179865f474bbSAkihiko Odaki         }
179965f474bbSAkihiko Odaki 
180065f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) {
180165f474bbSAkihiko Odaki             return NetPktRssIpV6Ex;
180265f474bbSAkihiko Odaki         }
180365f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
180465f474bbSAkihiko Odaki             return NetPktRssIpV6;
18054474e37aSYuri Benditovich         }
18064474e37aSYuri Benditovich     }
18074474e37aSYuri Benditovich     return 0xff;
18084474e37aSYuri Benditovich }
18094474e37aSYuri Benditovich 
1810e22f0603SYuri Benditovich static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1811e22f0603SYuri Benditovich                                    uint32_t hash)
1812e22f0603SYuri Benditovich {
1813e22f0603SYuri Benditovich     struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1814e22f0603SYuri Benditovich     hdr->hash_value = hash;
1815e22f0603SYuri Benditovich     hdr->hash_report = report;
1816e22f0603SYuri Benditovich }
1817e22f0603SYuri Benditovich 
18184474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
181997cd965cSPaolo Bonzini                                   size_t size)
18206e790746SPaolo Bonzini {
18216e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1822e22f0603SYuri Benditovich     unsigned int index = nc->queue_index, new_index = index;
18234474e37aSYuri Benditovich     struct NetRxPkt *pkt = n->rx_pkt;
18244474e37aSYuri Benditovich     uint8_t net_hash_type;
18254474e37aSYuri Benditovich     uint32_t hash;
182665f474bbSAkihiko Odaki     bool hasip4, hasip6;
182765f474bbSAkihiko Odaki     EthL4HdrProto l4hdr_proto;
1828e22f0603SYuri Benditovich     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1829e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv4,
1830e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv4,
1831e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6,
1832e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6,
1833e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1834e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1835e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv4,
1836e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6,
1837e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1838e22f0603SYuri Benditovich     };
18392f0fa232SAkihiko Odaki     struct iovec iov = {
18402f0fa232SAkihiko Odaki         .iov_base = (void *)buf,
18412f0fa232SAkihiko Odaki         .iov_len = size
18422f0fa232SAkihiko Odaki     };
18434474e37aSYuri Benditovich 
18442f0fa232SAkihiko Odaki     net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len);
184565f474bbSAkihiko Odaki     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
184665f474bbSAkihiko Odaki     net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto,
18474474e37aSYuri Benditovich                                              n->rss_data.hash_types);
18484474e37aSYuri Benditovich     if (net_hash_type > NetPktRssIpV6UdpEx) {
1849e22f0603SYuri Benditovich         if (n->rss_data.populate_hash) {
1850e22f0603SYuri Benditovich             virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1851e22f0603SYuri Benditovich         }
1852e22f0603SYuri Benditovich         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
18534474e37aSYuri Benditovich     }
18544474e37aSYuri Benditovich 
18554474e37aSYuri Benditovich     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1856e22f0603SYuri Benditovich 
1857e22f0603SYuri Benditovich     if (n->rss_data.populate_hash) {
1858e22f0603SYuri Benditovich         virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1859e22f0603SYuri Benditovich     }
1860e22f0603SYuri Benditovich 
1861e22f0603SYuri Benditovich     if (n->rss_data.redirect) {
18624474e37aSYuri Benditovich         new_index = hash & (n->rss_data.indirections_len - 1);
18634474e37aSYuri Benditovich         new_index = n->rss_data.indirections_table[new_index];
18644474e37aSYuri Benditovich     }
1865e22f0603SYuri Benditovich 
1866e22f0603SYuri Benditovich     return (index == new_index) ? -1 : new_index;
18674474e37aSYuri Benditovich }
18684474e37aSYuri Benditovich 
18694474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
18704474e37aSYuri Benditovich                                       size_t size, bool no_rss)
18714474e37aSYuri Benditovich {
18724474e37aSYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
18736e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
187417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1875bedd7e93SJason Wang     VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE];
1876bedd7e93SJason Wang     size_t lens[VIRTQUEUE_MAX_SIZE];
18776e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
18786e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
18796e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
1880bedd7e93SJason Wang     size_t offset, i, guest_offset, j;
1881bedd7e93SJason Wang     ssize_t err;
18826e790746SPaolo Bonzini 
18836e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
18846e790746SPaolo Bonzini         return -1;
18856e790746SPaolo Bonzini     }
18866e790746SPaolo Bonzini 
18870145c393SAndrew Melnychenko     if (!no_rss && n->rss_data.enabled && n->rss_data.enabled_software_rss) {
18884474e37aSYuri Benditovich         int index = virtio_net_process_rss(nc, buf, size);
18894474e37aSYuri Benditovich         if (index >= 0) {
18904474e37aSYuri Benditovich             NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
18914474e37aSYuri Benditovich             return virtio_net_receive_rcu(nc2, buf, size, true);
18924474e37aSYuri Benditovich         }
18934474e37aSYuri Benditovich     }
18944474e37aSYuri Benditovich 
18956e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
18966e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
18976e790746SPaolo Bonzini         return 0;
18986e790746SPaolo Bonzini     }
18996e790746SPaolo Bonzini 
19006e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
19016e790746SPaolo Bonzini         return size;
19026e790746SPaolo Bonzini 
19036e790746SPaolo Bonzini     offset = i = 0;
19046e790746SPaolo Bonzini 
19056e790746SPaolo Bonzini     while (offset < size) {
190651b19ebeSPaolo Bonzini         VirtQueueElement *elem;
19076e790746SPaolo Bonzini         int len, total;
190851b19ebeSPaolo Bonzini         const struct iovec *sg;
19096e790746SPaolo Bonzini 
19106e790746SPaolo Bonzini         total = 0;
19116e790746SPaolo Bonzini 
1912bedd7e93SJason Wang         if (i == VIRTQUEUE_MAX_SIZE) {
1913bedd7e93SJason Wang             virtio_error(vdev, "virtio-net unexpected long buffer chain");
1914bedd7e93SJason Wang             err = size;
1915bedd7e93SJason Wang             goto err;
1916bedd7e93SJason Wang         }
1917bedd7e93SJason Wang 
191851b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
191951b19ebeSPaolo Bonzini         if (!elem) {
1920ba10b9c0SGreg Kurz             if (i) {
1921ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
19226e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1923019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1924019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
19256e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1926019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1927019a3edbSGerd Hoffmann                              vdev->guest_features);
1928ba10b9c0SGreg Kurz             }
1929bedd7e93SJason Wang             err = -1;
1930bedd7e93SJason Wang             goto err;
19316e790746SPaolo Bonzini         }
19326e790746SPaolo Bonzini 
193351b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1934ba10b9c0SGreg Kurz             virtio_error(vdev,
1935ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1936ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1937ba10b9c0SGreg Kurz             g_free(elem);
1938bedd7e93SJason Wang             err = -1;
1939bedd7e93SJason Wang             goto err;
19406e790746SPaolo Bonzini         }
19416e790746SPaolo Bonzini 
194251b19ebeSPaolo Bonzini         sg = elem->in_sg;
19436e790746SPaolo Bonzini         if (i == 0) {
19446e790746SPaolo Bonzini             assert(offset == 0);
19456e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
19466e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
194751b19ebeSPaolo Bonzini                                     sg, elem->in_num,
19486e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
19496e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
19506e790746SPaolo Bonzini             }
19516e790746SPaolo Bonzini 
195251b19ebeSPaolo Bonzini             receive_header(n, sg, elem->in_num, buf, size);
1953e22f0603SYuri Benditovich             if (n->rss_data.populate_hash) {
1954e22f0603SYuri Benditovich                 offset = sizeof(mhdr);
1955e22f0603SYuri Benditovich                 iov_from_buf(sg, elem->in_num, offset,
1956e22f0603SYuri Benditovich                              buf + offset, n->host_hdr_len - sizeof(mhdr));
1957e22f0603SYuri Benditovich             }
19586e790746SPaolo Bonzini             offset = n->host_hdr_len;
19596e790746SPaolo Bonzini             total += n->guest_hdr_len;
19606e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
19616e790746SPaolo Bonzini         } else {
19626e790746SPaolo Bonzini             guest_offset = 0;
19636e790746SPaolo Bonzini         }
19646e790746SPaolo Bonzini 
19656e790746SPaolo Bonzini         /* copy in packet.  ugh */
196651b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
19676e790746SPaolo Bonzini                            buf + offset, size - offset);
19686e790746SPaolo Bonzini         total += len;
19696e790746SPaolo Bonzini         offset += len;
19706e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
19716e790746SPaolo Bonzini          * must have consumed the complete packet.
19726e790746SPaolo Bonzini          * Otherwise, drop it. */
19736e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
197427e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
197551b19ebeSPaolo Bonzini             g_free(elem);
1976bedd7e93SJason Wang             err = size;
1977bedd7e93SJason Wang             goto err;
19786e790746SPaolo Bonzini         }
19796e790746SPaolo Bonzini 
1980bedd7e93SJason Wang         elems[i] = elem;
1981bedd7e93SJason Wang         lens[i] = total;
1982bedd7e93SJason Wang         i++;
19836e790746SPaolo Bonzini     }
19846e790746SPaolo Bonzini 
19856e790746SPaolo Bonzini     if (mhdr_cnt) {
19861399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
19876e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
19886e790746SPaolo Bonzini                      0,
19896e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
19906e790746SPaolo Bonzini     }
19916e790746SPaolo Bonzini 
1992bedd7e93SJason Wang     for (j = 0; j < i; j++) {
1993bedd7e93SJason Wang         /* signal other side */
1994bedd7e93SJason Wang         virtqueue_fill(q->rx_vq, elems[j], lens[j], j);
1995bedd7e93SJason Wang         g_free(elems[j]);
1996bedd7e93SJason Wang     }
1997bedd7e93SJason Wang 
19986e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
199917a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
20006e790746SPaolo Bonzini 
20016e790746SPaolo Bonzini     return size;
2002bedd7e93SJason Wang 
2003bedd7e93SJason Wang err:
2004bedd7e93SJason Wang     for (j = 0; j < i; j++) {
2005abe300d9SJason Wang         virtqueue_detach_element(q->rx_vq, elems[j], lens[j]);
2006bedd7e93SJason Wang         g_free(elems[j]);
2007bedd7e93SJason Wang     }
2008bedd7e93SJason Wang 
2009bedd7e93SJason Wang     return err;
20106e790746SPaolo Bonzini }
20116e790746SPaolo Bonzini 
20122974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
201397cd965cSPaolo Bonzini                                   size_t size)
201497cd965cSPaolo Bonzini {
2015068ddfa9SDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
201697cd965cSPaolo Bonzini 
20174474e37aSYuri Benditovich     return virtio_net_receive_rcu(nc, buf, size, false);
201897cd965cSPaolo Bonzini }
201997cd965cSPaolo Bonzini 
20202974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
20212974e916SYuri Benditovich                                          const uint8_t *buf,
20222974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20232974e916SYuri Benditovich {
20242974e916SYuri Benditovich     uint16_t ip_hdrlen;
20252974e916SYuri Benditovich     struct ip_header *ip;
20262974e916SYuri Benditovich 
20272974e916SYuri Benditovich     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
20282974e916SYuri Benditovich                               + sizeof(struct eth_header));
20292974e916SYuri Benditovich     unit->ip = (void *)ip;
20302974e916SYuri Benditovich     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
20312974e916SYuri Benditovich     unit->ip_plen = &ip->ip_len;
20322974e916SYuri Benditovich     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
20332974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
20342974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
20352974e916SYuri Benditovich }
20362974e916SYuri Benditovich 
20372974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
20382974e916SYuri Benditovich                                          const uint8_t *buf,
20392974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20402974e916SYuri Benditovich {
20412974e916SYuri Benditovich     struct ip6_header *ip6;
20422974e916SYuri Benditovich 
20432974e916SYuri Benditovich     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
20442974e916SYuri Benditovich                                  + sizeof(struct eth_header));
20452974e916SYuri Benditovich     unit->ip = ip6;
20462974e916SYuri Benditovich     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
204778ee6bd0SPhilippe Mathieu-Daudé     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
20482974e916SYuri Benditovich                                         + sizeof(struct ip6_header));
20492974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
20502974e916SYuri Benditovich 
20512974e916SYuri Benditovich     /* There is a difference between payload lenght in ipv4 and v6,
20522974e916SYuri Benditovich        ip header is excluded in ipv6 */
20532974e916SYuri Benditovich     unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
20542974e916SYuri Benditovich }
20552974e916SYuri Benditovich 
20562974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
20572974e916SYuri Benditovich                                        VirtioNetRscSeg *seg)
20582974e916SYuri Benditovich {
20592974e916SYuri Benditovich     int ret;
2060dd3d85e8SYuri Benditovich     struct virtio_net_hdr_v1 *h;
20612974e916SYuri Benditovich 
2062dd3d85e8SYuri Benditovich     h = (struct virtio_net_hdr_v1 *)seg->buf;
20632974e916SYuri Benditovich     h->flags = 0;
20642974e916SYuri Benditovich     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
20652974e916SYuri Benditovich 
20662974e916SYuri Benditovich     if (seg->is_coalesced) {
2067dd3d85e8SYuri Benditovich         h->rsc.segments = seg->packets;
2068dd3d85e8SYuri Benditovich         h->rsc.dup_acks = seg->dup_ack;
20692974e916SYuri Benditovich         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
20702974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
20712974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
20722974e916SYuri Benditovich         } else {
20732974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
20742974e916SYuri Benditovich         }
20752974e916SYuri Benditovich     }
20762974e916SYuri Benditovich 
20772974e916SYuri Benditovich     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
20782974e916SYuri Benditovich     QTAILQ_REMOVE(&chain->buffers, seg, next);
20792974e916SYuri Benditovich     g_free(seg->buf);
20802974e916SYuri Benditovich     g_free(seg);
20812974e916SYuri Benditovich 
20822974e916SYuri Benditovich     return ret;
20832974e916SYuri Benditovich }
20842974e916SYuri Benditovich 
20852974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq)
20862974e916SYuri Benditovich {
20872974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn;
20882974e916SYuri Benditovich     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
20892974e916SYuri Benditovich 
20902974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
20912974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
20922974e916SYuri Benditovich             chain->stat.purge_failed++;
20932974e916SYuri Benditovich             continue;
20942974e916SYuri Benditovich         }
20952974e916SYuri Benditovich     }
20962974e916SYuri Benditovich 
20972974e916SYuri Benditovich     chain->stat.timer++;
20982974e916SYuri Benditovich     if (!QTAILQ_EMPTY(&chain->buffers)) {
20992974e916SYuri Benditovich         timer_mod(chain->drain_timer,
21002974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
21012974e916SYuri Benditovich     }
21022974e916SYuri Benditovich }
21032974e916SYuri Benditovich 
21042974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n)
21052974e916SYuri Benditovich {
21062974e916SYuri Benditovich     VirtioNetRscChain *chain, *rn_chain;
21072974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn_seg;
21082974e916SYuri Benditovich 
21092974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
21102974e916SYuri Benditovich         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
21112974e916SYuri Benditovich             QTAILQ_REMOVE(&chain->buffers, seg, next);
21122974e916SYuri Benditovich             g_free(seg->buf);
21132974e916SYuri Benditovich             g_free(seg);
21142974e916SYuri Benditovich         }
21152974e916SYuri Benditovich 
21162974e916SYuri Benditovich         timer_free(chain->drain_timer);
21172974e916SYuri Benditovich         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
21182974e916SYuri Benditovich         g_free(chain);
21192974e916SYuri Benditovich     }
21202974e916SYuri Benditovich }
21212974e916SYuri Benditovich 
21222974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
21232974e916SYuri Benditovich                                      NetClientState *nc,
21242974e916SYuri Benditovich                                      const uint8_t *buf, size_t size)
21252974e916SYuri Benditovich {
21262974e916SYuri Benditovich     uint16_t hdr_len;
21272974e916SYuri Benditovich     VirtioNetRscSeg *seg;
21282974e916SYuri Benditovich 
21292974e916SYuri Benditovich     hdr_len = chain->n->guest_hdr_len;
2130b21e2380SMarkus Armbruster     seg = g_new(VirtioNetRscSeg, 1);
21312974e916SYuri Benditovich     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
21322974e916SYuri Benditovich         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
21332974e916SYuri Benditovich     memcpy(seg->buf, buf, size);
21342974e916SYuri Benditovich     seg->size = size;
21352974e916SYuri Benditovich     seg->packets = 1;
21362974e916SYuri Benditovich     seg->dup_ack = 0;
21372974e916SYuri Benditovich     seg->is_coalesced = 0;
21382974e916SYuri Benditovich     seg->nc = nc;
21392974e916SYuri Benditovich 
21402974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
21412974e916SYuri Benditovich     chain->stat.cache++;
21422974e916SYuri Benditovich 
21432974e916SYuri Benditovich     switch (chain->proto) {
21442974e916SYuri Benditovich     case ETH_P_IP:
21452974e916SYuri Benditovich         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
21462974e916SYuri Benditovich         break;
21472974e916SYuri Benditovich     case ETH_P_IPV6:
21482974e916SYuri Benditovich         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
21492974e916SYuri Benditovich         break;
21502974e916SYuri Benditovich     default:
21512974e916SYuri Benditovich         g_assert_not_reached();
21522974e916SYuri Benditovich     }
21532974e916SYuri Benditovich }
21542974e916SYuri Benditovich 
21552974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
21562974e916SYuri Benditovich                                          VirtioNetRscSeg *seg,
21572974e916SYuri Benditovich                                          const uint8_t *buf,
21582974e916SYuri Benditovich                                          struct tcp_header *n_tcp,
21592974e916SYuri Benditovich                                          struct tcp_header *o_tcp)
21602974e916SYuri Benditovich {
21612974e916SYuri Benditovich     uint32_t nack, oack;
21622974e916SYuri Benditovich     uint16_t nwin, owin;
21632974e916SYuri Benditovich 
21642974e916SYuri Benditovich     nack = htonl(n_tcp->th_ack);
21652974e916SYuri Benditovich     nwin = htons(n_tcp->th_win);
21662974e916SYuri Benditovich     oack = htonl(o_tcp->th_ack);
21672974e916SYuri Benditovich     owin = htons(o_tcp->th_win);
21682974e916SYuri Benditovich 
21692974e916SYuri Benditovich     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
21702974e916SYuri Benditovich         chain->stat.ack_out_of_win++;
21712974e916SYuri Benditovich         return RSC_FINAL;
21722974e916SYuri Benditovich     } else if (nack == oack) {
21732974e916SYuri Benditovich         /* duplicated ack or window probe */
21742974e916SYuri Benditovich         if (nwin == owin) {
21752974e916SYuri Benditovich             /* duplicated ack, add dup ack count due to whql test up to 1 */
21762974e916SYuri Benditovich             chain->stat.dup_ack++;
21772974e916SYuri Benditovich             return RSC_FINAL;
21782974e916SYuri Benditovich         } else {
21792974e916SYuri Benditovich             /* Coalesce window update */
21802974e916SYuri Benditovich             o_tcp->th_win = n_tcp->th_win;
21812974e916SYuri Benditovich             chain->stat.win_update++;
21822974e916SYuri Benditovich             return RSC_COALESCE;
21832974e916SYuri Benditovich         }
21842974e916SYuri Benditovich     } else {
21852974e916SYuri Benditovich         /* pure ack, go to 'C', finalize*/
21862974e916SYuri Benditovich         chain->stat.pure_ack++;
21872974e916SYuri Benditovich         return RSC_FINAL;
21882974e916SYuri Benditovich     }
21892974e916SYuri Benditovich }
21902974e916SYuri Benditovich 
21912974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
21922974e916SYuri Benditovich                                             VirtioNetRscSeg *seg,
21932974e916SYuri Benditovich                                             const uint8_t *buf,
21942974e916SYuri Benditovich                                             VirtioNetRscUnit *n_unit)
21952974e916SYuri Benditovich {
21962974e916SYuri Benditovich     void *data;
21972974e916SYuri Benditovich     uint16_t o_ip_len;
21982974e916SYuri Benditovich     uint32_t nseq, oseq;
21992974e916SYuri Benditovich     VirtioNetRscUnit *o_unit;
22002974e916SYuri Benditovich 
22012974e916SYuri Benditovich     o_unit = &seg->unit;
22022974e916SYuri Benditovich     o_ip_len = htons(*o_unit->ip_plen);
22032974e916SYuri Benditovich     nseq = htonl(n_unit->tcp->th_seq);
22042974e916SYuri Benditovich     oseq = htonl(o_unit->tcp->th_seq);
22052974e916SYuri Benditovich 
22062974e916SYuri Benditovich     /* out of order or retransmitted. */
22072974e916SYuri Benditovich     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
22082974e916SYuri Benditovich         chain->stat.data_out_of_win++;
22092974e916SYuri Benditovich         return RSC_FINAL;
22102974e916SYuri Benditovich     }
22112974e916SYuri Benditovich 
22122974e916SYuri Benditovich     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
22132974e916SYuri Benditovich     if (nseq == oseq) {
22142974e916SYuri Benditovich         if ((o_unit->payload == 0) && n_unit->payload) {
22152974e916SYuri Benditovich             /* From no payload to payload, normal case, not a dup ack or etc */
22162974e916SYuri Benditovich             chain->stat.data_after_pure_ack++;
22172974e916SYuri Benditovich             goto coalesce;
22182974e916SYuri Benditovich         } else {
22192974e916SYuri Benditovich             return virtio_net_rsc_handle_ack(chain, seg, buf,
22202974e916SYuri Benditovich                                              n_unit->tcp, o_unit->tcp);
22212974e916SYuri Benditovich         }
22222974e916SYuri Benditovich     } else if ((nseq - oseq) != o_unit->payload) {
22232974e916SYuri Benditovich         /* Not a consistent packet, out of order */
22242974e916SYuri Benditovich         chain->stat.data_out_of_order++;
22252974e916SYuri Benditovich         return RSC_FINAL;
22262974e916SYuri Benditovich     } else {
22272974e916SYuri Benditovich coalesce:
22282974e916SYuri Benditovich         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
22292974e916SYuri Benditovich             chain->stat.over_size++;
22302974e916SYuri Benditovich             return RSC_FINAL;
22312974e916SYuri Benditovich         }
22322974e916SYuri Benditovich 
22332974e916SYuri Benditovich         /* Here comes the right data, the payload length in v4/v6 is different,
22342974e916SYuri Benditovich            so use the field value to update and record the new data len */
22352974e916SYuri Benditovich         o_unit->payload += n_unit->payload; /* update new data len */
22362974e916SYuri Benditovich 
22372974e916SYuri Benditovich         /* update field in ip header */
22382974e916SYuri Benditovich         *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
22392974e916SYuri Benditovich 
22402974e916SYuri Benditovich         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
22412974e916SYuri Benditovich            for windows guest, while this may change the behavior for linux
22422974e916SYuri Benditovich            guest (only if it uses RSC feature). */
22432974e916SYuri Benditovich         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
22442974e916SYuri Benditovich 
22452974e916SYuri Benditovich         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
22462974e916SYuri Benditovich         o_unit->tcp->th_win = n_unit->tcp->th_win;
22472974e916SYuri Benditovich 
22482974e916SYuri Benditovich         memmove(seg->buf + seg->size, data, n_unit->payload);
22492974e916SYuri Benditovich         seg->size += n_unit->payload;
22502974e916SYuri Benditovich         seg->packets++;
22512974e916SYuri Benditovich         chain->stat.coalesced++;
22522974e916SYuri Benditovich         return RSC_COALESCE;
22532974e916SYuri Benditovich     }
22542974e916SYuri Benditovich }
22552974e916SYuri Benditovich 
22562974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
22572974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
22582974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
22592974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
22602974e916SYuri Benditovich {
22612974e916SYuri Benditovich     struct ip_header *ip1, *ip2;
22622974e916SYuri Benditovich 
22632974e916SYuri Benditovich     ip1 = (struct ip_header *)(unit->ip);
22642974e916SYuri Benditovich     ip2 = (struct ip_header *)(seg->unit.ip);
22652974e916SYuri Benditovich     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
22662974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
22672974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
22682974e916SYuri Benditovich         chain->stat.no_match++;
22692974e916SYuri Benditovich         return RSC_NO_MATCH;
22702974e916SYuri Benditovich     }
22712974e916SYuri Benditovich 
22722974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
22732974e916SYuri Benditovich }
22742974e916SYuri Benditovich 
22752974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
22762974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
22772974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
22782974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
22792974e916SYuri Benditovich {
22802974e916SYuri Benditovich     struct ip6_header *ip1, *ip2;
22812974e916SYuri Benditovich 
22822974e916SYuri Benditovich     ip1 = (struct ip6_header *)(unit->ip);
22832974e916SYuri Benditovich     ip2 = (struct ip6_header *)(seg->unit.ip);
22842974e916SYuri Benditovich     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
22852974e916SYuri Benditovich         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
22862974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
22872974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
22882974e916SYuri Benditovich             chain->stat.no_match++;
22892974e916SYuri Benditovich             return RSC_NO_MATCH;
22902974e916SYuri Benditovich     }
22912974e916SYuri Benditovich 
22922974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
22932974e916SYuri Benditovich }
22942974e916SYuri Benditovich 
22952974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain
22962974e916SYuri Benditovich  * to prevent out of order */
22972974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
22982974e916SYuri Benditovich                                          struct tcp_header *tcp)
22992974e916SYuri Benditovich {
23002974e916SYuri Benditovich     uint16_t tcp_hdr;
23012974e916SYuri Benditovich     uint16_t tcp_flag;
23022974e916SYuri Benditovich 
23032974e916SYuri Benditovich     tcp_flag = htons(tcp->th_offset_flags);
23042974e916SYuri Benditovich     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
23052974e916SYuri Benditovich     tcp_flag &= VIRTIO_NET_TCP_FLAG;
23062974e916SYuri Benditovich     if (tcp_flag & TH_SYN) {
23072974e916SYuri Benditovich         chain->stat.tcp_syn++;
23082974e916SYuri Benditovich         return RSC_BYPASS;
23092974e916SYuri Benditovich     }
23102974e916SYuri Benditovich 
23112974e916SYuri Benditovich     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
23122974e916SYuri Benditovich         chain->stat.tcp_ctrl_drain++;
23132974e916SYuri Benditovich         return RSC_FINAL;
23142974e916SYuri Benditovich     }
23152974e916SYuri Benditovich 
23162974e916SYuri Benditovich     if (tcp_hdr > sizeof(struct tcp_header)) {
23172974e916SYuri Benditovich         chain->stat.tcp_all_opt++;
23182974e916SYuri Benditovich         return RSC_FINAL;
23192974e916SYuri Benditovich     }
23202974e916SYuri Benditovich 
23212974e916SYuri Benditovich     return RSC_CANDIDATE;
23222974e916SYuri Benditovich }
23232974e916SYuri Benditovich 
23242974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
23252974e916SYuri Benditovich                                          NetClientState *nc,
23262974e916SYuri Benditovich                                          const uint8_t *buf, size_t size,
23272974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
23282974e916SYuri Benditovich {
23292974e916SYuri Benditovich     int ret;
23302974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
23312974e916SYuri Benditovich 
23322974e916SYuri Benditovich     if (QTAILQ_EMPTY(&chain->buffers)) {
23332974e916SYuri Benditovich         chain->stat.empty_cache++;
23342974e916SYuri Benditovich         virtio_net_rsc_cache_buf(chain, nc, buf, size);
23352974e916SYuri Benditovich         timer_mod(chain->drain_timer,
23362974e916SYuri Benditovich               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
23372974e916SYuri Benditovich         return size;
23382974e916SYuri Benditovich     }
23392974e916SYuri Benditovich 
23402974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
23412974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
23422974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
23432974e916SYuri Benditovich         } else {
23442974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
23452974e916SYuri Benditovich         }
23462974e916SYuri Benditovich 
23472974e916SYuri Benditovich         if (ret == RSC_FINAL) {
23482974e916SYuri Benditovich             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
23492974e916SYuri Benditovich                 /* Send failed */
23502974e916SYuri Benditovich                 chain->stat.final_failed++;
23512974e916SYuri Benditovich                 return 0;
23522974e916SYuri Benditovich             }
23532974e916SYuri Benditovich 
23542974e916SYuri Benditovich             /* Send current packet */
23552974e916SYuri Benditovich             return virtio_net_do_receive(nc, buf, size);
23562974e916SYuri Benditovich         } else if (ret == RSC_NO_MATCH) {
23572974e916SYuri Benditovich             continue;
23582974e916SYuri Benditovich         } else {
23592974e916SYuri Benditovich             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
23602974e916SYuri Benditovich             seg->is_coalesced = 1;
23612974e916SYuri Benditovich             return size;
23622974e916SYuri Benditovich         }
23632974e916SYuri Benditovich     }
23642974e916SYuri Benditovich 
23652974e916SYuri Benditovich     chain->stat.no_match_cache++;
23662974e916SYuri Benditovich     virtio_net_rsc_cache_buf(chain, nc, buf, size);
23672974e916SYuri Benditovich     return size;
23682974e916SYuri Benditovich }
23692974e916SYuri Benditovich 
23702974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */
23712974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
23722974e916SYuri Benditovich                                         NetClientState *nc,
23732974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
23742974e916SYuri Benditovich                                         uint16_t ip_start, uint16_t ip_size,
23752974e916SYuri Benditovich                                         uint16_t tcp_port)
23762974e916SYuri Benditovich {
23772974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
23782974e916SYuri Benditovich     uint32_t ppair1, ppair2;
23792974e916SYuri Benditovich 
23802974e916SYuri Benditovich     ppair1 = *(uint32_t *)(buf + tcp_port);
23812974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
23822974e916SYuri Benditovich         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
23832974e916SYuri Benditovich         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
23842974e916SYuri Benditovich             || (ppair1 != ppair2)) {
23852974e916SYuri Benditovich             continue;
23862974e916SYuri Benditovich         }
23872974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
23882974e916SYuri Benditovich             chain->stat.drain_failed++;
23892974e916SYuri Benditovich         }
23902974e916SYuri Benditovich 
23912974e916SYuri Benditovich         break;
23922974e916SYuri Benditovich     }
23932974e916SYuri Benditovich 
23942974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
23952974e916SYuri Benditovich }
23962974e916SYuri Benditovich 
23972974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
23982974e916SYuri Benditovich                                             struct ip_header *ip,
23992974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
24002974e916SYuri Benditovich {
24012974e916SYuri Benditovich     uint16_t ip_len;
24022974e916SYuri Benditovich 
24032974e916SYuri Benditovich     /* Not an ipv4 packet */
24042974e916SYuri Benditovich     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
24052974e916SYuri Benditovich         chain->stat.ip_option++;
24062974e916SYuri Benditovich         return RSC_BYPASS;
24072974e916SYuri Benditovich     }
24082974e916SYuri Benditovich 
24092974e916SYuri Benditovich     /* Don't handle packets with ip option */
24102974e916SYuri Benditovich     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
24112974e916SYuri Benditovich         chain->stat.ip_option++;
24122974e916SYuri Benditovich         return RSC_BYPASS;
24132974e916SYuri Benditovich     }
24142974e916SYuri Benditovich 
24152974e916SYuri Benditovich     if (ip->ip_p != IPPROTO_TCP) {
24162974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24172974e916SYuri Benditovich         return RSC_BYPASS;
24182974e916SYuri Benditovich     }
24192974e916SYuri Benditovich 
24202974e916SYuri Benditovich     /* Don't handle packets with ip fragment */
24212974e916SYuri Benditovich     if (!(htons(ip->ip_off) & IP_DF)) {
24222974e916SYuri Benditovich         chain->stat.ip_frag++;
24232974e916SYuri Benditovich         return RSC_BYPASS;
24242974e916SYuri Benditovich     }
24252974e916SYuri Benditovich 
24262974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
24272974e916SYuri Benditovich     if (IPTOS_ECN(ip->ip_tos)) {
24282974e916SYuri Benditovich         chain->stat.ip_ecn++;
24292974e916SYuri Benditovich         return RSC_BYPASS;
24302974e916SYuri Benditovich     }
24312974e916SYuri Benditovich 
24322974e916SYuri Benditovich     ip_len = htons(ip->ip_len);
24332974e916SYuri Benditovich     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
24342974e916SYuri Benditovich         || ip_len > (size - chain->n->guest_hdr_len -
24352974e916SYuri Benditovich                      sizeof(struct eth_header))) {
24362974e916SYuri Benditovich         chain->stat.ip_hacked++;
24372974e916SYuri Benditovich         return RSC_BYPASS;
24382974e916SYuri Benditovich     }
24392974e916SYuri Benditovich 
24402974e916SYuri Benditovich     return RSC_CANDIDATE;
24412974e916SYuri Benditovich }
24422974e916SYuri Benditovich 
24432974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
24442974e916SYuri Benditovich                                       NetClientState *nc,
24452974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
24462974e916SYuri Benditovich {
24472974e916SYuri Benditovich     int32_t ret;
24482974e916SYuri Benditovich     uint16_t hdr_len;
24492974e916SYuri Benditovich     VirtioNetRscUnit unit;
24502974e916SYuri Benditovich 
24512974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
24522974e916SYuri Benditovich 
24532974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
24542974e916SYuri Benditovich         + sizeof(struct tcp_header))) {
24552974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24562974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24572974e916SYuri Benditovich     }
24582974e916SYuri Benditovich 
24592974e916SYuri Benditovich     virtio_net_rsc_extract_unit4(chain, buf, &unit);
24602974e916SYuri Benditovich     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
24612974e916SYuri Benditovich         != RSC_CANDIDATE) {
24622974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24632974e916SYuri Benditovich     }
24642974e916SYuri Benditovich 
24652974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
24662974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
24672974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
24682974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
24692974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
24702974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 12),
24712974e916SYuri Benditovich                 VIRTIO_NET_IP4_ADDR_SIZE,
24722974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
24732974e916SYuri Benditovich     }
24742974e916SYuri Benditovich 
24752974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
24762974e916SYuri Benditovich }
24772974e916SYuri Benditovich 
24782974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
24792974e916SYuri Benditovich                                             struct ip6_header *ip6,
24802974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
24812974e916SYuri Benditovich {
24822974e916SYuri Benditovich     uint16_t ip_len;
24832974e916SYuri Benditovich 
24842974e916SYuri Benditovich     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
24852974e916SYuri Benditovich         != IP_HEADER_VERSION_6) {
24862974e916SYuri Benditovich         return RSC_BYPASS;
24872974e916SYuri Benditovich     }
24882974e916SYuri Benditovich 
24892974e916SYuri Benditovich     /* Both option and protocol is checked in this */
24902974e916SYuri Benditovich     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
24912974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24922974e916SYuri Benditovich         return RSC_BYPASS;
24932974e916SYuri Benditovich     }
24942974e916SYuri Benditovich 
24952974e916SYuri Benditovich     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
24962974e916SYuri Benditovich     if (ip_len < sizeof(struct tcp_header) ||
24972974e916SYuri Benditovich         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
24982974e916SYuri Benditovich                   - sizeof(struct ip6_header))) {
24992974e916SYuri Benditovich         chain->stat.ip_hacked++;
25002974e916SYuri Benditovich         return RSC_BYPASS;
25012974e916SYuri Benditovich     }
25022974e916SYuri Benditovich 
25032974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
25042974e916SYuri Benditovich     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
25052974e916SYuri Benditovich         chain->stat.ip_ecn++;
25062974e916SYuri Benditovich         return RSC_BYPASS;
25072974e916SYuri Benditovich     }
25082974e916SYuri Benditovich 
25092974e916SYuri Benditovich     return RSC_CANDIDATE;
25102974e916SYuri Benditovich }
25112974e916SYuri Benditovich 
25122974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
25132974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
25142974e916SYuri Benditovich {
25152974e916SYuri Benditovich     int32_t ret;
25162974e916SYuri Benditovich     uint16_t hdr_len;
25172974e916SYuri Benditovich     VirtioNetRscChain *chain;
25182974e916SYuri Benditovich     VirtioNetRscUnit unit;
25192974e916SYuri Benditovich 
25203d558330SMarkus Armbruster     chain = opq;
25212974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
25222974e916SYuri Benditovich 
25232974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
25242974e916SYuri Benditovich         + sizeof(tcp_header))) {
25252974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25262974e916SYuri Benditovich     }
25272974e916SYuri Benditovich 
25282974e916SYuri Benditovich     virtio_net_rsc_extract_unit6(chain, buf, &unit);
25292974e916SYuri Benditovich     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
25302974e916SYuri Benditovich                                                  unit.ip, buf, size)) {
25312974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25322974e916SYuri Benditovich     }
25332974e916SYuri Benditovich 
25342974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
25352974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
25362974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25372974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
25382974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
25392974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 8),
25402974e916SYuri Benditovich                 VIRTIO_NET_IP6_ADDR_SIZE,
25412974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header)
25422974e916SYuri Benditovich                 + sizeof(struct ip6_header));
25432974e916SYuri Benditovich     }
25442974e916SYuri Benditovich 
25452974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
25462974e916SYuri Benditovich }
25472974e916SYuri Benditovich 
25482974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
25492974e916SYuri Benditovich                                                       NetClientState *nc,
25502974e916SYuri Benditovich                                                       uint16_t proto)
25512974e916SYuri Benditovich {
25522974e916SYuri Benditovich     VirtioNetRscChain *chain;
25532974e916SYuri Benditovich 
25542974e916SYuri Benditovich     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
25552974e916SYuri Benditovich         return NULL;
25562974e916SYuri Benditovich     }
25572974e916SYuri Benditovich 
25582974e916SYuri Benditovich     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
25592974e916SYuri Benditovich         if (chain->proto == proto) {
25602974e916SYuri Benditovich             return chain;
25612974e916SYuri Benditovich         }
25622974e916SYuri Benditovich     }
25632974e916SYuri Benditovich 
25642974e916SYuri Benditovich     chain = g_malloc(sizeof(*chain));
25652974e916SYuri Benditovich     chain->n = n;
25662974e916SYuri Benditovich     chain->proto = proto;
25672974e916SYuri Benditovich     if (proto == (uint16_t)ETH_P_IP) {
25682974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
25692974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
25702974e916SYuri Benditovich     } else {
25712974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
25722974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
25732974e916SYuri Benditovich     }
25742974e916SYuri Benditovich     chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
25752974e916SYuri Benditovich                                       virtio_net_rsc_purge, chain);
25762974e916SYuri Benditovich     memset(&chain->stat, 0, sizeof(chain->stat));
25772974e916SYuri Benditovich 
25782974e916SYuri Benditovich     QTAILQ_INIT(&chain->buffers);
25792974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
25802974e916SYuri Benditovich 
25812974e916SYuri Benditovich     return chain;
25822974e916SYuri Benditovich }
25832974e916SYuri Benditovich 
25842974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc,
25852974e916SYuri Benditovich                                       const uint8_t *buf,
25862974e916SYuri Benditovich                                       size_t size)
25872974e916SYuri Benditovich {
25882974e916SYuri Benditovich     uint16_t proto;
25892974e916SYuri Benditovich     VirtioNetRscChain *chain;
25902974e916SYuri Benditovich     struct eth_header *eth;
25912974e916SYuri Benditovich     VirtIONet *n;
25922974e916SYuri Benditovich 
25932974e916SYuri Benditovich     n = qemu_get_nic_opaque(nc);
25942974e916SYuri Benditovich     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
25952974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25962974e916SYuri Benditovich     }
25972974e916SYuri Benditovich 
25982974e916SYuri Benditovich     eth = (struct eth_header *)(buf + n->guest_hdr_len);
25992974e916SYuri Benditovich     proto = htons(eth->h_proto);
26002974e916SYuri Benditovich 
26012974e916SYuri Benditovich     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
26022974e916SYuri Benditovich     if (chain) {
26032974e916SYuri Benditovich         chain->stat.received++;
26042974e916SYuri Benditovich         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
26052974e916SYuri Benditovich             return virtio_net_rsc_receive4(chain, nc, buf, size);
26062974e916SYuri Benditovich         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
26072974e916SYuri Benditovich             return virtio_net_rsc_receive6(chain, nc, buf, size);
26082974e916SYuri Benditovich         }
26092974e916SYuri Benditovich     }
26102974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
26112974e916SYuri Benditovich }
26122974e916SYuri Benditovich 
26132974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
26142974e916SYuri Benditovich                                   size_t size)
26152974e916SYuri Benditovich {
26162974e916SYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
26172974e916SYuri Benditovich     if ((n->rsc4_enabled || n->rsc6_enabled)) {
26182974e916SYuri Benditovich         return virtio_net_rsc_receive(nc, buf, size);
26192974e916SYuri Benditovich     } else {
26202974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
26212974e916SYuri Benditovich     }
26222974e916SYuri Benditovich }
26232974e916SYuri Benditovich 
26246e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
26256e790746SPaolo Bonzini 
26266e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
26276e790746SPaolo Bonzini {
26286e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
26296e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
263017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2631df8d0708SLaurent Vivier     int ret;
26326e790746SPaolo Bonzini 
263351b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
263417a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
26356e790746SPaolo Bonzini 
263651b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
263751b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
26386e790746SPaolo Bonzini 
26396e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2640df8d0708SLaurent Vivier     ret = virtio_net_flush_tx(q);
26417550a822SLaurent Vivier     if (ret >= n->tx_burst) {
2642df8d0708SLaurent Vivier         /*
2643df8d0708SLaurent Vivier          * the flush has been stopped by tx_burst
2644df8d0708SLaurent Vivier          * we will not receive notification for the
2645df8d0708SLaurent Vivier          * remainining part, so re-schedule
2646df8d0708SLaurent Vivier          */
2647df8d0708SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
26487550a822SLaurent Vivier         if (q->tx_bh) {
2649df8d0708SLaurent Vivier             qemu_bh_schedule(q->tx_bh);
26507550a822SLaurent Vivier         } else {
26517550a822SLaurent Vivier             timer_mod(q->tx_timer,
26527550a822SLaurent Vivier                       qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
26537550a822SLaurent Vivier         }
2654df8d0708SLaurent Vivier         q->tx_waiting = 1;
2655df8d0708SLaurent Vivier     }
26566e790746SPaolo Bonzini }
26576e790746SPaolo Bonzini 
26586e790746SPaolo Bonzini /* TX */
26596e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
26606e790746SPaolo Bonzini {
26616e790746SPaolo Bonzini     VirtIONet *n = q->n;
266217a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
266351b19ebeSPaolo Bonzini     VirtQueueElement *elem;
26646e790746SPaolo Bonzini     int32_t num_packets = 0;
26656e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
266617a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
26676e790746SPaolo Bonzini         return num_packets;
26686e790746SPaolo Bonzini     }
26696e790746SPaolo Bonzini 
267051b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
26716e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
26726e790746SPaolo Bonzini         return num_packets;
26736e790746SPaolo Bonzini     }
26746e790746SPaolo Bonzini 
267551b19ebeSPaolo Bonzini     for (;;) {
2676bd89dd98SJason Wang         ssize_t ret;
267751b19ebeSPaolo Bonzini         unsigned int out_num;
267851b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2679feb93f36SJason Wang         struct virtio_net_hdr_mrg_rxbuf mhdr;
26806e790746SPaolo Bonzini 
268151b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
268251b19ebeSPaolo Bonzini         if (!elem) {
268351b19ebeSPaolo Bonzini             break;
268451b19ebeSPaolo Bonzini         }
268551b19ebeSPaolo Bonzini 
268651b19ebeSPaolo Bonzini         out_num = elem->out_num;
268751b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
26886e790746SPaolo Bonzini         if (out_num < 1) {
2689fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
2690fa5e56c2SGreg Kurz             virtqueue_detach_element(q->tx_vq, elem, 0);
2691fa5e56c2SGreg Kurz             g_free(elem);
2692fa5e56c2SGreg Kurz             return -EINVAL;
26936e790746SPaolo Bonzini         }
26946e790746SPaolo Bonzini 
2695032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
2696feb93f36SJason Wang             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2697feb93f36SJason Wang                 n->guest_hdr_len) {
2698fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
2699fa5e56c2SGreg Kurz                 virtqueue_detach_element(q->tx_vq, elem, 0);
2700fa5e56c2SGreg Kurz                 g_free(elem);
2701fa5e56c2SGreg Kurz                 return -EINVAL;
2702032a74a1SCédric Le Goater             }
27031bfa316cSGreg Kurz             if (n->needs_vnet_hdr_swap) {
2704feb93f36SJason Wang                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2705feb93f36SJason Wang                 sg2[0].iov_base = &mhdr;
2706feb93f36SJason Wang                 sg2[0].iov_len = n->guest_hdr_len;
2707feb93f36SJason Wang                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2708feb93f36SJason Wang                                    out_sg, out_num,
2709feb93f36SJason Wang                                    n->guest_hdr_len, -1);
2710feb93f36SJason Wang                 if (out_num == VIRTQUEUE_MAX_SIZE) {
2711feb93f36SJason Wang                     goto drop;
2712032a74a1SCédric Le Goater                 }
2713feb93f36SJason Wang                 out_num += 1;
2714feb93f36SJason Wang                 out_sg = sg2;
2715feb93f36SJason Wang             }
2716feb93f36SJason Wang         }
27176e790746SPaolo Bonzini         /*
27186e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
27196e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
27206e790746SPaolo Bonzini          * that host is interested in.
27216e790746SPaolo Bonzini          */
27226e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
27236e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
27246e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
27256e790746SPaolo Bonzini                                        out_sg, out_num,
27266e790746SPaolo Bonzini                                        0, n->host_hdr_len);
27276e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
27286e790746SPaolo Bonzini                              out_sg, out_num,
27296e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
27306e790746SPaolo Bonzini             out_num = sg_num;
27316e790746SPaolo Bonzini             out_sg = sg;
27326e790746SPaolo Bonzini         }
27336e790746SPaolo Bonzini 
27346e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
27356e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
27366e790746SPaolo Bonzini         if (ret == 0) {
27376e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
27386e790746SPaolo Bonzini             q->async_tx.elem = elem;
27396e790746SPaolo Bonzini             return -EBUSY;
27406e790746SPaolo Bonzini         }
27416e790746SPaolo Bonzini 
2742feb93f36SJason Wang drop:
274351b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
274417a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
274551b19ebeSPaolo Bonzini         g_free(elem);
27466e790746SPaolo Bonzini 
27476e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
27486e790746SPaolo Bonzini             break;
27496e790746SPaolo Bonzini         }
27506e790746SPaolo Bonzini     }
27516e790746SPaolo Bonzini     return num_packets;
27526e790746SPaolo Bonzini }
27536e790746SPaolo Bonzini 
27547550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque);
27557550a822SLaurent Vivier 
27566e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
27576e790746SPaolo Bonzini {
275817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
27596e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
27606e790746SPaolo Bonzini 
2761283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2762283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2763283e2c2aSYuri Benditovich         return;
2764283e2c2aSYuri Benditovich     }
2765283e2c2aSYuri Benditovich 
27666e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
276717a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
27686e790746SPaolo Bonzini         q->tx_waiting = 1;
27696e790746SPaolo Bonzini         return;
27706e790746SPaolo Bonzini     }
27716e790746SPaolo Bonzini 
27726e790746SPaolo Bonzini     if (q->tx_waiting) {
27737550a822SLaurent Vivier         /* We already have queued packets, immediately flush */
2774bc72ad67SAlex Bligh         timer_del(q->tx_timer);
27757550a822SLaurent Vivier         virtio_net_tx_timer(q);
27766e790746SPaolo Bonzini     } else {
27777550a822SLaurent Vivier         /* re-arm timer to flush it (and more) on next tick */
2778bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
2779bc72ad67SAlex Bligh                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
27806e790746SPaolo Bonzini         q->tx_waiting = 1;
27816e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
27826e790746SPaolo Bonzini     }
27836e790746SPaolo Bonzini }
27846e790746SPaolo Bonzini 
27856e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
27866e790746SPaolo Bonzini {
278717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
27886e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
27896e790746SPaolo Bonzini 
2790283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2791283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2792283e2c2aSYuri Benditovich         return;
2793283e2c2aSYuri Benditovich     }
2794283e2c2aSYuri Benditovich 
27956e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
27966e790746SPaolo Bonzini         return;
27976e790746SPaolo Bonzini     }
27986e790746SPaolo Bonzini     q->tx_waiting = 1;
27996e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
280017a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
28016e790746SPaolo Bonzini         return;
28026e790746SPaolo Bonzini     }
28036e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
28046e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
28056e790746SPaolo Bonzini }
28066e790746SPaolo Bonzini 
28076e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
28086e790746SPaolo Bonzini {
28096e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
28106e790746SPaolo Bonzini     VirtIONet *n = q->n;
281117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
28127550a822SLaurent Vivier     int ret;
28137550a822SLaurent Vivier 
2814e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2815e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2816e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2817e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2818e8bcf842SMichael S. Tsirkin         return;
2819e8bcf842SMichael S. Tsirkin     }
28206e790746SPaolo Bonzini 
28216e790746SPaolo Bonzini     q->tx_waiting = 0;
28226e790746SPaolo Bonzini 
28236e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
282417a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
28256e790746SPaolo Bonzini         return;
282617a0ca55SKONRAD Frederic     }
28276e790746SPaolo Bonzini 
28287550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
28297550a822SLaurent Vivier     if (ret == -EBUSY || ret == -EINVAL) {
28307550a822SLaurent Vivier         return;
28317550a822SLaurent Vivier     }
28327550a822SLaurent Vivier     /*
28337550a822SLaurent Vivier      * If we flush a full burst of packets, assume there are
28347550a822SLaurent Vivier      * more coming and immediately rearm
28357550a822SLaurent Vivier      */
28367550a822SLaurent Vivier     if (ret >= n->tx_burst) {
28377550a822SLaurent Vivier         q->tx_waiting = 1;
28387550a822SLaurent Vivier         timer_mod(q->tx_timer,
28397550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28407550a822SLaurent Vivier         return;
28417550a822SLaurent Vivier     }
28427550a822SLaurent Vivier     /*
28437550a822SLaurent Vivier      * If less than a full burst, re-enable notification and flush
28447550a822SLaurent Vivier      * anything that may have come in while we weren't looking.  If
28457550a822SLaurent Vivier      * we find something, assume the guest is still active and rearm
28467550a822SLaurent Vivier      */
28476e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
28487550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
28497550a822SLaurent Vivier     if (ret > 0) {
28507550a822SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
28517550a822SLaurent Vivier         q->tx_waiting = 1;
28527550a822SLaurent Vivier         timer_mod(q->tx_timer,
28537550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28547550a822SLaurent Vivier     }
28556e790746SPaolo Bonzini }
28566e790746SPaolo Bonzini 
28576e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
28586e790746SPaolo Bonzini {
28596e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
28606e790746SPaolo Bonzini     VirtIONet *n = q->n;
286117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
28626e790746SPaolo Bonzini     int32_t ret;
28636e790746SPaolo Bonzini 
2864e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2865e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2866e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2867e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2868e8bcf842SMichael S. Tsirkin         return;
2869e8bcf842SMichael S. Tsirkin     }
28706e790746SPaolo Bonzini 
28716e790746SPaolo Bonzini     q->tx_waiting = 0;
28726e790746SPaolo Bonzini 
28736e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
287417a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
28756e790746SPaolo Bonzini         return;
287617a0ca55SKONRAD Frederic     }
28776e790746SPaolo Bonzini 
28786e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
2879fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
2880fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
2881fa5e56c2SGreg Kurz                  * broken */
28826e790746SPaolo Bonzini     }
28836e790746SPaolo Bonzini 
28846e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
28856e790746SPaolo Bonzini      * more coming and immediately reschedule */
28866e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
28876e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
28886e790746SPaolo Bonzini         q->tx_waiting = 1;
28896e790746SPaolo Bonzini         return;
28906e790746SPaolo Bonzini     }
28916e790746SPaolo Bonzini 
28926e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
28936e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
28946e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
28956e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2896fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
2897fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
2898fa5e56c2SGreg Kurz         return;
2899fa5e56c2SGreg Kurz     } else if (ret > 0) {
29006e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
29016e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
29026e790746SPaolo Bonzini         q->tx_waiting = 1;
29036e790746SPaolo Bonzini     }
29046e790746SPaolo Bonzini }
29056e790746SPaolo Bonzini 
2906f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
2907f9d6dbf0SWen Congyang {
2908f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2909f9d6dbf0SWen Congyang 
29101c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
29111c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
29129b02e161SWei Wang 
2913f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2914f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29159b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29169b02e161SWei Wang                              virtio_net_handle_tx_timer);
2917f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2918f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
2919f9d6dbf0SWen Congyang                                               &n->vqs[index]);
2920f9d6dbf0SWen Congyang     } else {
2921f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29229b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29239b02e161SWei Wang                              virtio_net_handle_tx_bh);
2924f63192b0SAlexander Bulekov         n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
2925f63192b0SAlexander Bulekov                                                   &DEVICE(vdev)->mem_reentrancy_guard);
2926f9d6dbf0SWen Congyang     }
2927f9d6dbf0SWen Congyang 
2928f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
2929f9d6dbf0SWen Congyang     n->vqs[index].n = n;
2930f9d6dbf0SWen Congyang }
2931f9d6dbf0SWen Congyang 
2932f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
2933f9d6dbf0SWen Congyang {
2934f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2935f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
2936f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
2937f9d6dbf0SWen Congyang 
2938f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
2939f9d6dbf0SWen Congyang 
2940f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
2941f9d6dbf0SWen Congyang     if (q->tx_timer) {
2942f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
2943f989c30cSYunjian Wang         q->tx_timer = NULL;
2944f9d6dbf0SWen Congyang     } else {
2945f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
2946f989c30cSYunjian Wang         q->tx_bh = NULL;
2947f9d6dbf0SWen Congyang     }
2948f989c30cSYunjian Wang     q->tx_waiting = 0;
2949f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
2950f9d6dbf0SWen Congyang }
2951f9d6dbf0SWen Congyang 
2952441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs)
2953f9d6dbf0SWen Congyang {
2954f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2955f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
2956441537f1SJason Wang     int new_num_queues = new_max_queue_pairs * 2 + 1;
2957f9d6dbf0SWen Congyang     int i;
2958f9d6dbf0SWen Congyang 
2959f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
2960f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
2961f9d6dbf0SWen Congyang 
2962f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
2963f9d6dbf0SWen Congyang         return;
2964f9d6dbf0SWen Congyang     }
2965f9d6dbf0SWen Congyang 
2966f9d6dbf0SWen Congyang     /*
2967f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
2968f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
296920f86a75SYuval Shaia      * and then we only enter one of the following two loops.
2970f9d6dbf0SWen Congyang      */
2971f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
2972f9d6dbf0SWen Congyang 
2973f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2974f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
2975f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
2976f9d6dbf0SWen Congyang     }
2977f9d6dbf0SWen Congyang 
2978f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2979f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
2980f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
2981f9d6dbf0SWen Congyang     }
2982f9d6dbf0SWen Congyang 
2983f9d6dbf0SWen Congyang     /* add ctrl_vq last */
2984f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2985f9d6dbf0SWen Congyang }
2986f9d6dbf0SWen Congyang 
2987ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
29886e790746SPaolo Bonzini {
2989441537f1SJason Wang     int max = multiqueue ? n->max_queue_pairs : 1;
2990f9d6dbf0SWen Congyang 
29916e790746SPaolo Bonzini     n->multiqueue = multiqueue;
2992441537f1SJason Wang     virtio_net_change_num_queue_pairs(n, max);
29936e790746SPaolo Bonzini 
2994441537f1SJason Wang     virtio_net_set_queue_pairs(n);
29956e790746SPaolo Bonzini }
29966e790746SPaolo Bonzini 
2997982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
2998037dab2fSGreg Kurz {
2999982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
3000982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3001037dab2fSGreg Kurz     int i, link_down;
3002037dab2fSGreg Kurz 
30039d8c6a25SDr. David Alan Gilbert     trace_virtio_net_post_load_device();
3004982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
300595129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
3006e22f0603SYuri Benditovich                                                        VIRTIO_F_VERSION_1),
3007e22f0603SYuri Benditovich                                virtio_vdev_has_feature(vdev,
3008e22f0603SYuri Benditovich                                                        VIRTIO_NET_F_HASH_REPORT));
30096e790746SPaolo Bonzini 
30106e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
3011982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
30126e790746SPaolo Bonzini         n->mac_table.in_use = 0;
30136e790746SPaolo Bonzini     }
30146e790746SPaolo Bonzini 
3015982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
30166c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
30176c666823SMichael S. Tsirkin     }
30186c666823SMichael S. Tsirkin 
30197788c3f2SMikhail Sennikovsky     /*
30207788c3f2SMikhail Sennikovsky      * curr_guest_offloads will be later overwritten by the
30217788c3f2SMikhail Sennikovsky      * virtio_set_features_nocheck call done from the virtio_load.
30227788c3f2SMikhail Sennikovsky      * Here we make sure it is preserved and restored accordingly
30237788c3f2SMikhail Sennikovsky      * in the virtio_net_post_load_virtio callback.
30247788c3f2SMikhail Sennikovsky      */
30257788c3f2SMikhail Sennikovsky     n->saved_guest_offloads = n->curr_guest_offloads;
30266c666823SMichael S. Tsirkin 
3027441537f1SJason Wang     virtio_net_set_queue_pairs(n);
30286e790746SPaolo Bonzini 
30296e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
30306e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
30316e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
30326e790746SPaolo Bonzini             break;
30336e790746SPaolo Bonzini         }
30346e790746SPaolo Bonzini     }
30356e790746SPaolo Bonzini     n->mac_table.first_multi = i;
30366e790746SPaolo Bonzini 
30376e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
30386e790746SPaolo Bonzini      * to link status bit in n->status */
30396e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
3040441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
30416e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
30426e790746SPaolo Bonzini     }
30436e790746SPaolo Bonzini 
30446c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
30456c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
30469d8c6a25SDr. David Alan Gilbert         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
30479d8c6a25SDr. David Alan Gilbert                                   QEMU_CLOCK_VIRTUAL,
30489d8c6a25SDr. David Alan Gilbert                                   virtio_net_announce_timer, n);
30499d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
30509d8c6a25SDr. David Alan Gilbert             timer_mod(n->announce_timer.tm,
30519d8c6a25SDr. David Alan Gilbert                       qemu_clock_get_ms(n->announce_timer.type));
30529d8c6a25SDr. David Alan Gilbert         } else {
3053944458b6SDr. David Alan Gilbert             qemu_announce_timer_del(&n->announce_timer, false);
30549d8c6a25SDr. David Alan Gilbert         }
30556c666823SMichael S. Tsirkin     }
30566c666823SMichael S. Tsirkin 
3057e41b7114SYuri Benditovich     if (n->rss_data.enabled) {
30580145c393SAndrew Melnychenko         n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
30590145c393SAndrew Melnychenko         if (!n->rss_data.populate_hash) {
30600145c393SAndrew Melnychenko             if (!virtio_net_attach_epbf_rss(n)) {
30610145c393SAndrew Melnychenko                 if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
30620145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS for vhost");
30630145c393SAndrew Melnychenko                 } else {
30640145c393SAndrew Melnychenko                     warn_report("Can't post-load eBPF RSS - "
30650145c393SAndrew Melnychenko                                 "fallback to software RSS");
30660145c393SAndrew Melnychenko                     n->rss_data.enabled_software_rss = true;
30670145c393SAndrew Melnychenko                 }
30680145c393SAndrew Melnychenko             }
30690145c393SAndrew Melnychenko         }
30700145c393SAndrew Melnychenko 
3071e41b7114SYuri Benditovich         trace_virtio_net_rss_enable(n->rss_data.hash_types,
3072e41b7114SYuri Benditovich                                     n->rss_data.indirections_len,
3073e41b7114SYuri Benditovich                                     sizeof(n->rss_data.key));
3074e41b7114SYuri Benditovich     } else {
3075e41b7114SYuri Benditovich         trace_virtio_net_rss_disable();
3076e41b7114SYuri Benditovich     }
30776e790746SPaolo Bonzini     return 0;
30786e790746SPaolo Bonzini }
30796e790746SPaolo Bonzini 
30807788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev)
30817788c3f2SMikhail Sennikovsky {
30827788c3f2SMikhail Sennikovsky     VirtIONet *n = VIRTIO_NET(vdev);
30837788c3f2SMikhail Sennikovsky     /*
30847788c3f2SMikhail Sennikovsky      * The actual needed state is now in saved_guest_offloads,
30857788c3f2SMikhail Sennikovsky      * see virtio_net_post_load_device for detail.
30867788c3f2SMikhail Sennikovsky      * Restore it back and apply the desired offloads.
30877788c3f2SMikhail Sennikovsky      */
30887788c3f2SMikhail Sennikovsky     n->curr_guest_offloads = n->saved_guest_offloads;
30897788c3f2SMikhail Sennikovsky     if (peer_has_vnet_hdr(n)) {
30907788c3f2SMikhail Sennikovsky         virtio_net_apply_guest_offloads(n);
30917788c3f2SMikhail Sennikovsky     }
30927788c3f2SMikhail Sennikovsky 
30937788c3f2SMikhail Sennikovsky     return 0;
30947788c3f2SMikhail Sennikovsky }
30957788c3f2SMikhail Sennikovsky 
3096982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
3097982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
3098982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
3099982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
3100982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
3101982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3102982b78c5SDr. David Alan Gilbert    },
3103982b78c5SDr. David Alan Gilbert };
3104982b78c5SDr. David Alan Gilbert 
3105441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id)
3106982b78c5SDr. David Alan Gilbert {
3107441537f1SJason Wang     return VIRTIO_NET(opaque)->max_queue_pairs > 1;
3108982b78c5SDr. David Alan Gilbert }
3109982b78c5SDr. David Alan Gilbert 
3110982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
3111982b78c5SDr. David Alan Gilbert {
3112982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
3113982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
3114982b78c5SDr. David Alan Gilbert }
3115982b78c5SDr. David Alan Gilbert 
3116982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
3117982b78c5SDr. David Alan Gilbert {
3118982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
3119982b78c5SDr. David Alan Gilbert }
3120982b78c5SDr. David Alan Gilbert 
3121982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
3122982b78c5SDr. David Alan Gilbert {
3123982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
3124982b78c5SDr. David Alan Gilbert }
3125982b78c5SDr. David Alan Gilbert 
3126982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
3127982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
3128982b78c5SDr. David Alan Gilbert  */
3129982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
3130982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
3131982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
3132441537f1SJason Wang     uint16_t        curr_queue_pairs_1;
3133982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
3134982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
3135982b78c5SDr. David Alan Gilbert };
3136982b78c5SDr. David Alan Gilbert 
3137982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
3138441537f1SJason Wang  * the 1st entry in the queue_pairs and only if there's more than one
3139982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
3140982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
3141982b78c5SDr. David Alan Gilbert  */
3142982b78c5SDr. David Alan Gilbert 
314344b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque)
3144982b78c5SDr. David Alan Gilbert {
3145982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3146982b78c5SDr. David Alan Gilbert 
3147982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
3148441537f1SJason Wang     tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1;
3149441537f1SJason Wang     if (tmp->parent->curr_queue_pairs == 0) {
3150441537f1SJason Wang         tmp->curr_queue_pairs_1 = 0;
3151982b78c5SDr. David Alan Gilbert     }
315244b1ff31SDr. David Alan Gilbert 
315344b1ff31SDr. David Alan Gilbert     return 0;
3154982b78c5SDr. David Alan Gilbert }
3155982b78c5SDr. David Alan Gilbert 
3156982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
3157982b78c5SDr. David Alan Gilbert {
3158982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3159982b78c5SDr. David Alan Gilbert 
3160982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
3161982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
3162982b78c5SDr. David Alan Gilbert 
3163441537f1SJason Wang     if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) {
3164441537f1SJason Wang         error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3165441537f1SJason Wang             tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs);
3166982b78c5SDr. David Alan Gilbert 
3167982b78c5SDr. David Alan Gilbert         return -EINVAL;
3168982b78c5SDr. David Alan Gilbert     }
3169982b78c5SDr. David Alan Gilbert 
3170982b78c5SDr. David Alan Gilbert     return 0; /* all good */
3171982b78c5SDr. David Alan Gilbert }
3172982b78c5SDr. David Alan Gilbert 
3173982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
3174982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
3175982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
3176982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
3177982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3178982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
3179441537f1SJason Wang                                      curr_queue_pairs_1,
3180982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
3181982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
3182982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3183982b78c5SDr. David Alan Gilbert     },
3184982b78c5SDr. David Alan Gilbert };
3185982b78c5SDr. David Alan Gilbert 
3186982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
3187982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3188982b78c5SDr. David Alan Gilbert  */
3189982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
3190982b78c5SDr. David Alan Gilbert {
3191982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3192982b78c5SDr. David Alan Gilbert 
3193982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
3194982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
3195982b78c5SDr. David Alan Gilbert         return -EINVAL;
3196982b78c5SDr. David Alan Gilbert     }
3197982b78c5SDr. David Alan Gilbert 
3198982b78c5SDr. David Alan Gilbert     return 0;
3199982b78c5SDr. David Alan Gilbert }
3200982b78c5SDr. David Alan Gilbert 
320144b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque)
3202982b78c5SDr. David Alan Gilbert {
3203982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3204982b78c5SDr. David Alan Gilbert 
3205982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
320644b1ff31SDr. David Alan Gilbert 
320744b1ff31SDr. David Alan Gilbert     return 0;
3208982b78c5SDr. David Alan Gilbert }
3209982b78c5SDr. David Alan Gilbert 
3210982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
3211982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
3212982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
3213982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
3214982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3215982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
3216982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3217982b78c5SDr. David Alan Gilbert     },
3218982b78c5SDr. David Alan Gilbert };
3219982b78c5SDr. David Alan Gilbert 
3220982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3221982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3222982b78c5SDr. David Alan Gilbert  */
3223982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
3224982b78c5SDr. David Alan Gilbert {
3225982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3226982b78c5SDr. David Alan Gilbert 
3227982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
3228982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
3229982b78c5SDr. David Alan Gilbert         return -EINVAL;
3230982b78c5SDr. David Alan Gilbert     }
3231982b78c5SDr. David Alan Gilbert 
3232982b78c5SDr. David Alan Gilbert     return 0;
3233982b78c5SDr. David Alan Gilbert }
3234982b78c5SDr. David Alan Gilbert 
323544b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque)
3236982b78c5SDr. David Alan Gilbert {
3237982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3238982b78c5SDr. David Alan Gilbert 
3239982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
324044b1ff31SDr. David Alan Gilbert 
324144b1ff31SDr. David Alan Gilbert     return 0;
3242982b78c5SDr. David Alan Gilbert }
3243982b78c5SDr. David Alan Gilbert 
3244982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
3245982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
3246982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
3247982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
3248982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
3249982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
3250982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3251982b78c5SDr. David Alan Gilbert     },
3252982b78c5SDr. David Alan Gilbert };
3253982b78c5SDr. David Alan Gilbert 
3254e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque)
3255e41b7114SYuri Benditovich {
3256e41b7114SYuri Benditovich     return VIRTIO_NET(opaque)->rss_data.enabled;
3257e41b7114SYuri Benditovich }
3258e41b7114SYuri Benditovich 
3259e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = {
3260e41b7114SYuri Benditovich     .name      = "virtio-net-device/rss",
3261e41b7114SYuri Benditovich     .version_id = 1,
3262e41b7114SYuri Benditovich     .minimum_version_id = 1,
3263e41b7114SYuri Benditovich     .needed = virtio_net_rss_needed,
3264e41b7114SYuri Benditovich     .fields = (VMStateField[]) {
3265e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
3266e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
3267e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
3268e41b7114SYuri Benditovich         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
3269e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
3270e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
3271e41b7114SYuri Benditovich         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
3272e41b7114SYuri Benditovich                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
3273e41b7114SYuri Benditovich         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
3274e41b7114SYuri Benditovich                                     rss_data.indirections_len, 0,
3275e41b7114SYuri Benditovich                                     vmstate_info_uint16, uint16_t),
3276e41b7114SYuri Benditovich         VMSTATE_END_OF_LIST()
3277e41b7114SYuri Benditovich     },
3278e41b7114SYuri Benditovich };
3279e41b7114SYuri Benditovich 
3280982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
3281982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
3282982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
3283982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
3284982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
3285982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
3286982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
3287982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
3288982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
3289982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
3290982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
3291982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
3292982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
3293982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
3294982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3295982b78c5SDr. David Alan Gilbert 
3296982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
3297982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
3298982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
3299982b78c5SDr. David Alan Gilbert          */
3300982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3301982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
3302982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
3303982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3304982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
3305982b78c5SDr. David Alan Gilbert 
3306982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
3307982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3308982b78c5SDr. David Alan Gilbert          * but based on the uint.
3309982b78c5SDr. David Alan Gilbert          */
3310982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3311982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3312982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
3313982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3314982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3315982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
3316982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
3317982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
3318982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
3319982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3320982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
3321441537f1SJason Wang         VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0,
3322982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
3323441537f1SJason Wang         VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1),
3324982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3325982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
3326982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3327982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
3328982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3329982b78c5SDr. David Alan Gilbert    },
3330e41b7114SYuri Benditovich     .subsections = (const VMStateDescription * []) {
3331e41b7114SYuri Benditovich         &vmstate_virtio_net_rss,
3332e41b7114SYuri Benditovich         NULL
3333e41b7114SYuri Benditovich     }
3334982b78c5SDr. David Alan Gilbert };
3335982b78c5SDr. David Alan Gilbert 
33366e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
3337f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
33386e790746SPaolo Bonzini     .size = sizeof(NICState),
33396e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
33406e790746SPaolo Bonzini     .receive = virtio_net_receive,
33416e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
3342b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
3343b2c929f0SDr. David Alan Gilbert     .announce = virtio_net_announce,
33446e790746SPaolo Bonzini };
33456e790746SPaolo Bonzini 
33466e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
33476e790746SPaolo Bonzini {
334817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
334968b0a639SSi-Wei Liu     NetClientState *nc;
33506e790746SPaolo Bonzini     assert(n->vhost_started);
335168b0a639SSi-Wei Liu     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) {
335268b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
335368b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
335468b0a639SSi-Wei Liu          * buggy migration stream.
335568b0a639SSi-Wei Liu          */
335668b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
335768b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
335868b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
335968b0a639SSi-Wei Liu             return false;
336068b0a639SSi-Wei Liu         }
336168b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
336268b0a639SSi-Wei Liu     } else {
336368b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
336468b0a639SSi-Wei Liu     }
3365544f0278SCindy Lu     /*
3366544f0278SCindy Lu      * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
33677e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3368544f0278SCindy Lu      * support, the function will return false
3369544f0278SCindy Lu      */
3370544f0278SCindy Lu 
3371544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
33728aab0d1dSCindy Lu         return vhost_net_config_pending(get_vhost_net(nc->peer));
3373544f0278SCindy Lu     }
3374ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
33756e790746SPaolo Bonzini }
33766e790746SPaolo Bonzini 
33776e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
33786e790746SPaolo Bonzini                                            bool mask)
33796e790746SPaolo Bonzini {
338017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
338168b0a639SSi-Wei Liu     NetClientState *nc;
33826e790746SPaolo Bonzini     assert(n->vhost_started);
338368b0a639SSi-Wei Liu     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx == 2) {
338468b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
338568b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
338668b0a639SSi-Wei Liu          * buggy migration stream.
338768b0a639SSi-Wei Liu          */
338868b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
338968b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
339068b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
339168b0a639SSi-Wei Liu             return;
339268b0a639SSi-Wei Liu         }
339368b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
339468b0a639SSi-Wei Liu     } else {
339568b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
339668b0a639SSi-Wei Liu     }
3397544f0278SCindy Lu     /*
3398544f0278SCindy Lu      *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
33997e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3400544f0278SCindy Lu      * support, the function will return
3401544f0278SCindy Lu      */
3402544f0278SCindy Lu 
3403544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
34048aab0d1dSCindy Lu         vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
3405544f0278SCindy Lu         return;
3406544f0278SCindy Lu     }
3407544f0278SCindy Lu     vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
34086e790746SPaolo Bonzini }
34096e790746SPaolo Bonzini 
3410019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
34116e790746SPaolo Bonzini {
34120cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3413a93e599dSMaxime Coquelin 
3414d74c30c8SDaniil Tatianin     n->config_size = virtio_get_config_size(&cfg_size_params, host_features);
341517ec5a86SKONRAD Frederic }
34166e790746SPaolo Bonzini 
34178a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
34188a253ec2SKONRAD Frederic                                    const char *type)
34198a253ec2SKONRAD Frederic {
34208a253ec2SKONRAD Frederic     /*
34218a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
34228a253ec2SKONRAD Frederic      */
34238a253ec2SKONRAD Frederic     assert(type != NULL);
34248a253ec2SKONRAD Frederic 
34258a253ec2SKONRAD Frederic     g_free(n->netclient_name);
34268a253ec2SKONRAD Frederic     g_free(n->netclient_type);
34278a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
34288a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
34298a253ec2SKONRAD Frederic }
34308a253ec2SKONRAD Frederic 
34310e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
34329711cd0dSJens Freimann {
34339711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
34349711cd0dSJens Freimann     PCIDevice *pci_dev;
34359711cd0dSJens Freimann     Error *err = NULL;
34369711cd0dSJens Freimann 
34370e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
34389711cd0dSJens Freimann     if (hotplug_ctrl) {
34390e9a65c5SJuan Quintela         pci_dev = PCI_DEVICE(dev);
34409711cd0dSJens Freimann         pci_dev->partially_hotplugged = true;
34410e9a65c5SJuan Quintela         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
34429711cd0dSJens Freimann         if (err) {
34439711cd0dSJens Freimann             error_report_err(err);
34449711cd0dSJens Freimann             return false;
34459711cd0dSJens Freimann         }
34469711cd0dSJens Freimann     } else {
34479711cd0dSJens Freimann         return false;
34489711cd0dSJens Freimann     }
34499711cd0dSJens Freimann     return true;
34509711cd0dSJens Freimann }
34519711cd0dSJens Freimann 
34520e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
34530e9a65c5SJuan Quintela                                     Error **errp)
34549711cd0dSJens Freimann {
34555a0948d3SMarkus Armbruster     Error *err = NULL;
34569711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
34570e9a65c5SJuan Quintela     PCIDevice *pdev = PCI_DEVICE(dev);
345878274682SJuan Quintela     BusState *primary_bus;
34599711cd0dSJens Freimann 
34609711cd0dSJens Freimann     if (!pdev->partially_hotplugged) {
34619711cd0dSJens Freimann         return true;
34629711cd0dSJens Freimann     }
34630e9a65c5SJuan Quintela     primary_bus = dev->parent_bus;
346478274682SJuan Quintela     if (!primary_bus) {
3465150ab54aSJens Freimann         error_setg(errp, "virtio_net: couldn't find primary bus");
34665a0948d3SMarkus Armbruster         return false;
34679711cd0dSJens Freimann     }
34680e9a65c5SJuan Quintela     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3469e2bde83eSJuan Quintela     qatomic_set(&n->failover_primary_hidden, false);
34700e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
34719711cd0dSJens Freimann     if (hotplug_ctrl) {
34720e9a65c5SJuan Quintela         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
34735a0948d3SMarkus Armbruster         if (err) {
34745a0948d3SMarkus Armbruster             goto out;
34755a0948d3SMarkus Armbruster         }
34760e9a65c5SJuan Quintela         hotplug_handler_plug(hotplug_ctrl, dev, &err);
34779711cd0dSJens Freimann     }
3478109c20eaSLaurent Vivier     pdev->partially_hotplugged = false;
3479150ab54aSJens Freimann 
3480150ab54aSJens Freimann out:
34815a0948d3SMarkus Armbruster     error_propagate(errp, err);
34825a0948d3SMarkus Armbruster     return !err;
34839711cd0dSJens Freimann }
34849711cd0dSJens Freimann 
348507a5d816SJuan Quintela static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
34869711cd0dSJens Freimann {
34879711cd0dSJens Freimann     bool should_be_hidden;
34889711cd0dSJens Freimann     Error *err = NULL;
348907a5d816SJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
349007a5d816SJuan Quintela 
349107a5d816SJuan Quintela     if (!dev) {
349207a5d816SJuan Quintela         return;
349307a5d816SJuan Quintela     }
34949711cd0dSJens Freimann 
3495e2bde83eSJuan Quintela     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
34969711cd0dSJens Freimann 
34974dbac1aeSMarkus Armbruster     if (migration_in_setup(s) && !should_be_hidden) {
349807a5d816SJuan Quintela         if (failover_unplug_primary(n, dev)) {
349907a5d816SJuan Quintela             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
350007a5d816SJuan Quintela             qapi_event_send_unplug_primary(dev->id);
3501e2bde83eSJuan Quintela             qatomic_set(&n->failover_primary_hidden, true);
35029711cd0dSJens Freimann         } else {
35039711cd0dSJens Freimann             warn_report("couldn't unplug primary device");
35049711cd0dSJens Freimann         }
35059711cd0dSJens Freimann     } else if (migration_has_failed(s)) {
3506150ab54aSJens Freimann         /* We already unplugged the device let's plug it back */
350707a5d816SJuan Quintela         if (!failover_replug_primary(n, dev, &err)) {
35089711cd0dSJens Freimann             if (err) {
35099711cd0dSJens Freimann                 error_report_err(err);
35109711cd0dSJens Freimann             }
35119711cd0dSJens Freimann         }
35129711cd0dSJens Freimann     }
35139711cd0dSJens Freimann }
35149711cd0dSJens Freimann 
35159711cd0dSJens Freimann static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
35169711cd0dSJens Freimann {
35179711cd0dSJens Freimann     MigrationState *s = data;
35189711cd0dSJens Freimann     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
35199711cd0dSJens Freimann     virtio_net_handle_migration_primary(n, s);
35209711cd0dSJens Freimann }
35219711cd0dSJens Freimann 
3522b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener,
3523f3558b1bSKevin Wolf                                          const QDict *device_opts,
3524f3558b1bSKevin Wolf                                          bool from_json,
3525f3558b1bSKevin Wolf                                          Error **errp)
35269711cd0dSJens Freimann {
35279711cd0dSJens Freimann     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
35284f0303aeSJuan Quintela     const char *standby_id;
35299711cd0dSJens Freimann 
35304d0e59acSJens Freimann     if (!device_opts) {
353189631fedSJuan Quintela         return false;
35324d0e59acSJens Freimann     }
3533bcfc906bSLaurent Vivier 
3534bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "failover_pair_id")) {
3535bcfc906bSLaurent Vivier         return false;
3536bcfc906bSLaurent Vivier     }
3537bcfc906bSLaurent Vivier 
3538bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "id")) {
3539bcfc906bSLaurent Vivier         error_setg(errp, "Device with failover_pair_id needs to have id");
3540bcfc906bSLaurent Vivier         return false;
3541bcfc906bSLaurent Vivier     }
3542bcfc906bSLaurent Vivier 
3543bcfc906bSLaurent Vivier     standby_id = qdict_get_str(device_opts, "failover_pair_id");
354489631fedSJuan Quintela     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
354589631fedSJuan Quintela         return false;
35469711cd0dSJens Freimann     }
35479711cd0dSJens Freimann 
35487fe7791eSLaurent Vivier     /*
35497fe7791eSLaurent Vivier      * The hide helper can be called several times for a given device.
35507fe7791eSLaurent Vivier      * Check there is only one primary for a virtio-net device but
35517fe7791eSLaurent Vivier      * don't duplicate the qdict several times if it's called for the same
35527fe7791eSLaurent Vivier      * device.
35537fe7791eSLaurent Vivier      */
3554259a10dbSKevin Wolf     if (n->primary_opts) {
35557fe7791eSLaurent Vivier         const char *old, *new;
35567fe7791eSLaurent Vivier         /* devices with failover_pair_id always have an id */
35577fe7791eSLaurent Vivier         old = qdict_get_str(n->primary_opts, "id");
35587fe7791eSLaurent Vivier         new = qdict_get_str(device_opts, "id");
35597fe7791eSLaurent Vivier         if (strcmp(old, new) != 0) {
35607fe7791eSLaurent Vivier             error_setg(errp, "Cannot attach more than one primary device to "
35617fe7791eSLaurent Vivier                        "'%s': '%s' and '%s'", n->netclient_name, old, new);
3562259a10dbSKevin Wolf             return false;
3563259a10dbSKevin Wolf         }
35647fe7791eSLaurent Vivier     } else {
3565f3558b1bSKevin Wolf         n->primary_opts = qdict_clone_shallow(device_opts);
3566f3558b1bSKevin Wolf         n->primary_opts_from_json = from_json;
35677fe7791eSLaurent Vivier     }
3568259a10dbSKevin Wolf 
3569e2bde83eSJuan Quintela     /* failover_primary_hidden is set during feature negotiation */
35703abad4a2SJuan Quintela     return qatomic_read(&n->failover_primary_hidden);
35719711cd0dSJens Freimann }
35729711cd0dSJens Freimann 
3573e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
357417ec5a86SKONRAD Frederic {
3575e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3576284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3577284a32f0SAndreas Färber     NetClientState *nc;
35781773d9eeSKONRAD Frederic     int i;
357917ec5a86SKONRAD Frederic 
3580a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
3581127833eeSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3582a93e599dSMaxime Coquelin     }
3583a93e599dSMaxime Coquelin 
35849473939eSJason Baron     if (n->net_conf.duplex_str) {
35859473939eSJason Baron         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
35869473939eSJason Baron             n->net_conf.duplex = DUPLEX_HALF;
35879473939eSJason Baron         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
35889473939eSJason Baron             n->net_conf.duplex = DUPLEX_FULL;
35899473939eSJason Baron         } else {
35909473939eSJason Baron             error_setg(errp, "'duplex' must be 'half' or 'full'");
3591843c4cfcSMarkus Armbruster             return;
35929473939eSJason Baron         }
35939473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
35949473939eSJason Baron     } else {
35959473939eSJason Baron         n->net_conf.duplex = DUPLEX_UNKNOWN;
35969473939eSJason Baron     }
35979473939eSJason Baron 
35989473939eSJason Baron     if (n->net_conf.speed < SPEED_UNKNOWN) {
35999473939eSJason Baron         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3600843c4cfcSMarkus Armbruster         return;
3601843c4cfcSMarkus Armbruster     }
3602843c4cfcSMarkus Armbruster     if (n->net_conf.speed >= 0) {
36039473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
36049473939eSJason Baron     }
36059473939eSJason Baron 
36069711cd0dSJens Freimann     if (n->failover) {
3607b91ad981SJuan Quintela         n->primary_listener.hide_device = failover_hide_primary_device;
3608e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, true);
36099711cd0dSJens Freimann         device_listener_register(&n->primary_listener);
36109711cd0dSJens Freimann         n->migration_state.notify = virtio_net_migration_state_notifier;
36119711cd0dSJens Freimann         add_migration_state_change_notifier(&n->migration_state);
36129711cd0dSJens Freimann         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
36139711cd0dSJens Freimann     }
36149711cd0dSJens Freimann 
3615da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
36163857cd5cSJonah Palmer     virtio_init(vdev, VIRTIO_ID_NET, n->config_size);
361717ec5a86SKONRAD Frederic 
36181c0fbfa3SMichael S. Tsirkin     /*
36191c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
36201c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
36211c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
36221c0fbfa3SMichael S. Tsirkin      */
36231c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
36241c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
36255f997fd1SMichal Privoznik         !is_power_of_2(n->net_conf.rx_queue_size)) {
36261c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
36271c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
36281c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
36291c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
36301c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
36311c0fbfa3SMichael S. Tsirkin         return;
36321c0fbfa3SMichael S. Tsirkin     }
36331c0fbfa3SMichael S. Tsirkin 
36349b02e161SWei Wang     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
36354271f403SLaurent Vivier         n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) ||
36369b02e161SWei Wang         !is_power_of_2(n->net_conf.tx_queue_size)) {
36379b02e161SWei Wang         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
36389b02e161SWei Wang                    "must be a power of 2 between %d and %d",
36399b02e161SWei Wang                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
36404271f403SLaurent Vivier                    virtio_net_max_tx_queue_size(n));
36419b02e161SWei Wang         virtio_cleanup(vdev);
36429b02e161SWei Wang         return;
36439b02e161SWei Wang     }
36449b02e161SWei Wang 
364522288fe5SJason Wang     n->max_ncs = MAX(n->nic_conf.peers.queues, 1);
364622288fe5SJason Wang 
364722288fe5SJason Wang     /*
364822288fe5SJason Wang      * Figure out the datapath queue pairs since the backend could
364922288fe5SJason Wang      * provide control queue via peers as well.
365022288fe5SJason Wang      */
365122288fe5SJason Wang     if (n->nic_conf.peers.queues) {
365222288fe5SJason Wang         for (i = 0; i < n->max_ncs; i++) {
365322288fe5SJason Wang             if (n->nic_conf.peers.ncs[i]->is_datapath) {
365422288fe5SJason Wang                 ++n->max_queue_pairs;
365522288fe5SJason Wang             }
365622288fe5SJason Wang         }
365722288fe5SJason Wang     }
365822288fe5SJason Wang     n->max_queue_pairs = MAX(n->max_queue_pairs, 1);
365922288fe5SJason Wang 
3660441537f1SJason Wang     if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) {
366122288fe5SJason Wang         error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), "
3662631b22eaSStefan Weil                    "must be a positive integer less than %d.",
3663441537f1SJason Wang                    n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2);
36647e0e736eSJason Wang         virtio_cleanup(vdev);
36657e0e736eSJason Wang         return;
36667e0e736eSJason Wang     }
3667b21e2380SMarkus Armbruster     n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs);
3668441537f1SJason Wang     n->curr_queue_pairs = 1;
36691773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
36706e790746SPaolo Bonzini 
36711773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
36721773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
36730765691eSMarkus Armbruster         warn_report("virtio-net: "
36746e790746SPaolo Bonzini                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
36751773d9eeSKONRAD Frederic                     n->net_conf.tx);
36760765691eSMarkus Armbruster         error_printf("Defaulting to \"bh\"");
36776e790746SPaolo Bonzini     }
36786e790746SPaolo Bonzini 
36792eef278bSMichael S. Tsirkin     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
36802eef278bSMichael S. Tsirkin                                     n->net_conf.tx_queue_size);
36819b02e161SWei Wang 
3682441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
3683f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i);
3684da51a335SJason Wang     }
3685da51a335SJason Wang 
368617a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
36871773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
36881773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
36896e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
36909d8c6a25SDr. David Alan Gilbert     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
36919d8c6a25SDr. David Alan Gilbert                               QEMU_CLOCK_VIRTUAL,
3692f57fcf70SJason Wang                               virtio_net_announce_timer, n);
3693b2c929f0SDr. David Alan Gilbert     n->announce_timer.round = 0;
36946e790746SPaolo Bonzini 
36958a253ec2SKONRAD Frederic     if (n->netclient_type) {
36968a253ec2SKONRAD Frederic         /*
36978a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
36988a253ec2SKONRAD Frederic          */
36998a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
37008a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
37018a253ec2SKONRAD Frederic     } else {
37021773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3703284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
37048a253ec2SKONRAD Frederic     }
37058a253ec2SKONRAD Frederic 
3706441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
3707d4c62930SBin Meng         n->nic->ncs[i].do_not_pad = true;
3708d4c62930SBin Meng     }
3709d4c62930SBin Meng 
37106e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
37116e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
3712441537f1SJason Wang         for (i = 0; i < n->max_queue_pairs; i++) {
3713d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
37146e790746SPaolo Bonzini         }
37156e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
37166e790746SPaolo Bonzini     } else {
37176e790746SPaolo Bonzini         n->host_hdr_len = 0;
37186e790746SPaolo Bonzini     }
37196e790746SPaolo Bonzini 
37201773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
37216e790746SPaolo Bonzini 
37226e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
37231773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
3724e22f0603SYuri Benditovich     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
37256e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
37266e790746SPaolo Bonzini 
37276e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
37286e790746SPaolo Bonzini 
37296e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
37306e790746SPaolo Bonzini 
3731b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
3732b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
3733b1be4280SAmos Kong 
3734e87936eaSCindy Lu    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3735e87936eaSCindy Lu         struct virtio_net_config netcfg = {};
3736e87936eaSCindy Lu         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3737e87936eaSCindy Lu         vhost_net_set_config(get_vhost_net(nc->peer),
3738f8ed3648SManos Pitsidianakis             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND);
3739e87936eaSCindy Lu     }
37402974e916SYuri Benditovich     QTAILQ_INIT(&n->rsc_chains);
3741284a32f0SAndreas Färber     n->qdev = dev;
37424474e37aSYuri Benditovich 
3743aac8f89dSAkihiko Odaki     net_rx_pkt_init(&n->rx_pkt);
37440145c393SAndrew Melnychenko 
37450145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
37460145c393SAndrew Melnychenko         virtio_net_load_ebpf(n);
37470145c393SAndrew Melnychenko     }
374817ec5a86SKONRAD Frederic }
374917ec5a86SKONRAD Frederic 
3750b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev)
375117ec5a86SKONRAD Frederic {
3752306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3753306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3754441537f1SJason Wang     int i, max_queue_pairs;
375517ec5a86SKONRAD Frederic 
37560145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
37570145c393SAndrew Melnychenko         virtio_net_unload_ebpf(n);
37580145c393SAndrew Melnychenko     }
37590145c393SAndrew Melnychenko 
376017ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
376117ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
376217ec5a86SKONRAD Frederic 
37638a253ec2SKONRAD Frederic     g_free(n->netclient_name);
37648a253ec2SKONRAD Frederic     n->netclient_name = NULL;
37658a253ec2SKONRAD Frederic     g_free(n->netclient_type);
37668a253ec2SKONRAD Frederic     n->netclient_type = NULL;
37678a253ec2SKONRAD Frederic 
376817ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
376917ec5a86SKONRAD Frederic     g_free(n->vlans);
377017ec5a86SKONRAD Frederic 
37719711cd0dSJens Freimann     if (n->failover) {
3772f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
377365018100SJuan Quintela         device_listener_unregister(&n->primary_listener);
37741e157667SLaurent Vivier         remove_migration_state_change_notifier(&n->migration_state);
3775f3558b1bSKevin Wolf     } else {
3776f3558b1bSKevin Wolf         assert(n->primary_opts == NULL);
37779711cd0dSJens Freimann     }
37789711cd0dSJens Freimann 
3779441537f1SJason Wang     max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
3780441537f1SJason Wang     for (i = 0; i < max_queue_pairs; i++) {
3781f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
378217ec5a86SKONRAD Frederic     }
3783d945d9f1SYuri Benditovich     /* delete also control vq */
3784441537f1SJason Wang     virtio_del_queue(vdev, max_queue_pairs * 2);
3785944458b6SDr. David Alan Gilbert     qemu_announce_timer_del(&n->announce_timer, false);
378617ec5a86SKONRAD Frederic     g_free(n->vqs);
378717ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
37882974e916SYuri Benditovich     virtio_net_rsc_cleanup(n);
378959079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
37904474e37aSYuri Benditovich     net_rx_pkt_uninit(n->rx_pkt);
37916a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
379217ec5a86SKONRAD Frederic }
379317ec5a86SKONRAD Frederic 
379417ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
379517ec5a86SKONRAD Frederic {
379617ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
379717ec5a86SKONRAD Frederic 
379817ec5a86SKONRAD Frederic     /*
379917ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
380017ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
380117ec5a86SKONRAD Frederic      */
380217ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
3803aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3804aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
380540c2281cSMarkus Armbruster                                   DEVICE(n));
38060145c393SAndrew Melnychenko 
38070145c393SAndrew Melnychenko     ebpf_rss_init(&n->ebpf_rss);
380817ec5a86SKONRAD Frederic }
380917ec5a86SKONRAD Frederic 
381044b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque)
38114d45dcfbSHalil Pasic {
38124d45dcfbSHalil Pasic     VirtIONet *n = opaque;
38134d45dcfbSHalil Pasic 
38144d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
38154d45dcfbSHalil Pasic      * it might keep writing to memory. */
38164d45dcfbSHalil Pasic     assert(!n->vhost_started);
381744b1ff31SDr. David Alan Gilbert 
381844b1ff31SDr. David Alan Gilbert     return 0;
38194d45dcfbSHalil Pasic }
38204d45dcfbSHalil Pasic 
38219711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque)
38229711cd0dSJens Freimann {
38239711cd0dSJens Freimann     DeviceState *dev = opaque;
382421e8709bSJuan Quintela     DeviceState *primary;
38259711cd0dSJens Freimann     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
38269711cd0dSJens Freimann     VirtIONet *n = VIRTIO_NET(vdev);
38279711cd0dSJens Freimann 
3828284f42a5SJens Freimann     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3829284f42a5SJens Freimann         return false;
3830284f42a5SJens Freimann     }
383121e8709bSJuan Quintela     primary = failover_find_primary_device(n);
383221e8709bSJuan Quintela     return primary ? primary->pending_deleted_event : false;
38339711cd0dSJens Freimann }
38349711cd0dSJens Freimann 
38359711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque)
38369711cd0dSJens Freimann {
38379711cd0dSJens Freimann     DeviceState *dev = opaque;
38389711cd0dSJens Freimann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
38399711cd0dSJens Freimann 
38409711cd0dSJens Freimann     return vdc->primary_unplug_pending(dev);
38419711cd0dSJens Freimann }
38429711cd0dSJens Freimann 
3843c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
3844c255488dSJonah Palmer {
3845c255488dSJonah Palmer     VirtIONet *n = VIRTIO_NET(vdev);
3846c255488dSJonah Palmer     NetClientState *nc = qemu_get_queue(n->nic);
3847c255488dSJonah Palmer     struct vhost_net *net = get_vhost_net(nc->peer);
3848c255488dSJonah Palmer     return &net->dev;
3849c255488dSJonah Palmer }
3850c255488dSJonah Palmer 
38514d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
38524d45dcfbSHalil Pasic     .name = "virtio-net",
38534d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
38544d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
38554d45dcfbSHalil Pasic     .fields = (VMStateField[]) {
38564d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
38574d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
38584d45dcfbSHalil Pasic     },
38594d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
38609711cd0dSJens Freimann     .dev_unplug_pending = dev_unplug_pending,
38614d45dcfbSHalil Pasic };
3862290c2428SDr. David Alan Gilbert 
386317ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
3864127833eeSJason Baron     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3865127833eeSJason Baron                     VIRTIO_NET_F_CSUM, true),
3866127833eeSJason Baron     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
386787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
3868127833eeSJason Baron     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3869127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
387087108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
3871127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
387287108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
3873127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
387487108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
3875127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
387687108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
3877127833eeSJason Baron     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
387887108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3879127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
388087108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
3881127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
388287108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
3883127833eeSJason Baron     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
388487108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
3885127833eeSJason Baron     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
388687108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
3887127833eeSJason Baron     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
388887108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
3889127833eeSJason Baron     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
389087108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
3891127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
389287108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
3893127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
389487108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
3895127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
389687108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
3897127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
389887108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3899127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
390087108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3901127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
390287108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3903127833eeSJason Baron     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
390459079029SYuri Benditovich     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
390559079029SYuri Benditovich                     VIRTIO_NET_F_RSS, false),
3906e22f0603SYuri Benditovich     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3907e22f0603SYuri Benditovich                     VIRTIO_NET_F_HASH_REPORT, false),
39082974e916SYuri Benditovich     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
39092974e916SYuri Benditovich                     VIRTIO_NET_F_RSC_EXT, false),
39102974e916SYuri Benditovich     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
39112974e916SYuri Benditovich                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
391217ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
391317ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
391417ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
391517ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
391617ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
39171c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
39181c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
39199b02e161SWei Wang     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
39209b02e161SWei Wang                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3921a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
392275ebec11SMaxime Coquelin     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
392375ebec11SMaxime Coquelin                      true),
39249473939eSJason Baron     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
39259473939eSJason Baron     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
39269711cd0dSJens Freimann     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
392717ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
392817ec5a86SKONRAD Frederic };
392917ec5a86SKONRAD Frederic 
393017ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
393117ec5a86SKONRAD Frederic {
393217ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
393317ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3934e6f746b3SAndreas Färber 
39354f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
3936290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
3937125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3938e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
3939306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
394017ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
394117ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
394217ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
394317ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
394417ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
394517ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
39467dc6be52SXuan Zhuo     vdc->queue_reset = virtio_net_queue_reset;
39477f863302SKangjie Xu     vdc->queue_enable = virtio_net_queue_enable;
394817ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
394917ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
395017ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
39512a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
39527788c3f2SMikhail Sennikovsky     vdc->post_load = virtio_net_post_load_virtio;
3953982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
39549711cd0dSJens Freimann     vdc->primary_unplug_pending = primary_unplug_pending;
3955c255488dSJonah Palmer     vdc->get_vhost = virtio_net_get_vhost;
3956cd9b8346SViktor Prutyanov     vdc->toggle_device_iotlb = vhost_toggle_device_iotlb;
395717ec5a86SKONRAD Frederic }
395817ec5a86SKONRAD Frederic 
395917ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
396017ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
396117ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
396217ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
396317ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
396417ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
396517ec5a86SKONRAD Frederic };
396617ec5a86SKONRAD Frederic 
396717ec5a86SKONRAD Frederic static void virtio_register_types(void)
396817ec5a86SKONRAD Frederic {
396917ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
397017ec5a86SKONRAD Frederic }
397117ec5a86SKONRAD Frederic 
397217ec5a86SKONRAD Frederic type_init(virtio_register_types)
3973