xref: /openbmc/qemu/hw/net/virtio-net.c (revision ba54a7e6b86884e43bed2d2f5a79c719059652a8)
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"
43a0bf401bSNicholas Piggin #include "sysemu/replay.h"
449d8c6a25SDr. David Alan Gilbert #include "trace.h"
459711cd0dSJens Freimann #include "monitor/qdev.h"
466b230b7dSAndrew Melnychenko #include "monitor/monitor.h"
47edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
484474e37aSYuri Benditovich #include "net_rx_pkt.h"
49108a6481SCindy Lu #include "hw/virtio/vhost.h"
501b529d90SLaurent Vivier #include "sysemu/qtest.h"
516e790746SPaolo Bonzini 
526e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
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 
virtio_net_get_subqueue(NetClientState * nc)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 
vq2q(int queue_index)1226e790746SPaolo Bonzini static int vq2q(int queue_index)
1236e790746SPaolo Bonzini {
1246e790746SPaolo Bonzini     return queue_index / 2;
1256e790746SPaolo Bonzini }
1266e790746SPaolo Bonzini 
flush_or_purge_queued_packets(NetClientState * nc)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 
virtio_net_get_config(VirtIODevice * vdev,uint8_t * config)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 
virtio_net_set_config(VirtIODevice * vdev,const uint8_t * config)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 
virtio_net_started(VirtIONet * n,uint8_t status)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 
virtio_net_announce_notify(VirtIONet * net)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 
virtio_net_announce_timer(void * opaque)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 
virtio_net_announce(NetClientState * nc)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 
virtio_net_vhost_status(VirtIONet * n,uint8_t status)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 
virtio_net_set_vnet_endian_one(VirtIODevice * vdev,NetClientState * peer,bool enable)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 
virtio_net_set_vnet_endian(VirtIODevice * vdev,NetClientState * ncs,int queue_pairs,bool enable)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 
virtio_net_vnet_endian_status(VirtIONet * n,uint8_t status)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          */
364ad57f700SAkihiko Odaki         n->needs_vnet_hdr_swap = n->has_vnet_hdr &&
365ad57f700SAkihiko Odaki                                  virtio_net_set_vnet_endian(vdev, n->nic->ncs,
366441537f1SJason Wang                                                             queue_pairs, true);
3671bfa316cSGreg Kurz     } else if (virtio_net_started(n, vdev->status)) {
3681bfa316cSGreg Kurz         /* After using the device, we need to reset the network backend to
3691bfa316cSGreg Kurz          * the default (guest native endianness), otherwise the guest may
3701bfa316cSGreg Kurz          * lose network connectivity if it is rebooted into a different
3711bfa316cSGreg Kurz          * endianness.
3721bfa316cSGreg Kurz          */
373441537f1SJason Wang         virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false);
3741bfa316cSGreg Kurz     }
3751bfa316cSGreg Kurz }
3761bfa316cSGreg Kurz 
virtio_net_drop_tx_queue_data(VirtIODevice * vdev,VirtQueue * vq)377283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
378283e2c2aSYuri Benditovich {
379283e2c2aSYuri Benditovich     unsigned int dropped = virtqueue_drop_all(vq);
380283e2c2aSYuri Benditovich     if (dropped) {
381283e2c2aSYuri Benditovich         virtio_notify(vdev, vq);
382283e2c2aSYuri Benditovich     }
383283e2c2aSYuri Benditovich }
384283e2c2aSYuri Benditovich 
virtio_net_set_status(struct VirtIODevice * vdev,uint8_t status)3856e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
3866e790746SPaolo Bonzini {
38717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
3886e790746SPaolo Bonzini     VirtIONetQueue *q;
3896e790746SPaolo Bonzini     int i;
3906e790746SPaolo Bonzini     uint8_t queue_status;
3916e790746SPaolo Bonzini 
3921bfa316cSGreg Kurz     virtio_net_vnet_endian_status(n, status);
3936e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
3946e790746SPaolo Bonzini 
395441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
39638705bb5SFam Zheng         NetClientState *ncs = qemu_get_subqueue(n->nic, i);
39738705bb5SFam Zheng         bool queue_started;
3986e790746SPaolo Bonzini         q = &n->vqs[i];
3996e790746SPaolo Bonzini 
400441537f1SJason Wang         if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) {
4016e790746SPaolo Bonzini             queue_status = 0;
4026e790746SPaolo Bonzini         } else {
4036e790746SPaolo Bonzini             queue_status = status;
4046e790746SPaolo Bonzini         }
40538705bb5SFam Zheng         queue_started =
40638705bb5SFam Zheng             virtio_net_started(n, queue_status) && !n->vhost_started;
40738705bb5SFam Zheng 
40838705bb5SFam Zheng         if (queue_started) {
40938705bb5SFam Zheng             qemu_flush_queued_packets(ncs);
41038705bb5SFam Zheng         }
4116e790746SPaolo Bonzini 
4126e790746SPaolo Bonzini         if (!q->tx_waiting) {
4136e790746SPaolo Bonzini             continue;
4146e790746SPaolo Bonzini         }
4156e790746SPaolo Bonzini 
41638705bb5SFam Zheng         if (queue_started) {
4176e790746SPaolo Bonzini             if (q->tx_timer) {
418bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
419bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
4206e790746SPaolo Bonzini             } else {
421a0bf401bSNicholas Piggin                 replay_bh_schedule_event(q->tx_bh);
4226e790746SPaolo Bonzini             }
4236e790746SPaolo Bonzini         } else {
4246e790746SPaolo Bonzini             if (q->tx_timer) {
425bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
4266e790746SPaolo Bonzini             } else {
4276e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
4286e790746SPaolo Bonzini             }
429283e2c2aSYuri Benditovich             if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
43070e53e6eSJason Wang                 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
43170e53e6eSJason Wang                 vdev->vm_running) {
432283e2c2aSYuri Benditovich                 /* if tx is waiting we are likely have some packets in tx queue
433283e2c2aSYuri Benditovich                  * and disabled notification */
434283e2c2aSYuri Benditovich                 q->tx_waiting = 0;
435283e2c2aSYuri Benditovich                 virtio_queue_set_notification(q->tx_vq, 1);
436283e2c2aSYuri Benditovich                 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
437283e2c2aSYuri Benditovich             }
4386e790746SPaolo Bonzini         }
4396e790746SPaolo Bonzini     }
4406e790746SPaolo Bonzini }
4416e790746SPaolo Bonzini 
virtio_net_set_link_status(NetClientState * nc)4426e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
4436e790746SPaolo Bonzini {
4446e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
44517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
4466e790746SPaolo Bonzini     uint16_t old_status = n->status;
4476e790746SPaolo Bonzini 
4486e790746SPaolo Bonzini     if (nc->link_down)
4496e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
4506e790746SPaolo Bonzini     else
4516e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
4526e790746SPaolo Bonzini 
4536e790746SPaolo Bonzini     if (n->status != old_status)
45417a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
4556e790746SPaolo Bonzini 
45617a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
4576e790746SPaolo Bonzini }
4586e790746SPaolo Bonzini 
rxfilter_notify(NetClientState * nc)459b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
460b1be4280SAmos Kong {
461b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
462b1be4280SAmos Kong 
463b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
464ddfb0baaSMarkus Armbruster         char *path = object_get_canonical_path(OBJECT(n->qdev));
4657480874aSMarkus Armbruster         qapi_event_send_nic_rx_filter_changed(n->netclient_name, path);
46696e35046SAmos Kong         g_free(path);
467b1be4280SAmos Kong 
468b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
469b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
470b1be4280SAmos Kong     }
471b1be4280SAmos Kong }
472b1be4280SAmos Kong 
get_vlan_table(VirtIONet * n)473f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
474f7bc8ef8SAmos Kong {
47554aa3de7SEric Blake     intList *list;
476f7bc8ef8SAmos Kong     int i, j;
477f7bc8ef8SAmos Kong 
478f7bc8ef8SAmos Kong     list = NULL;
479f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
480f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
481f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
48254aa3de7SEric Blake                 QAPI_LIST_PREPEND(list, (i << 5) + j);
483f7bc8ef8SAmos Kong             }
484f7bc8ef8SAmos Kong         }
485f7bc8ef8SAmos Kong     }
486f7bc8ef8SAmos Kong 
487f7bc8ef8SAmos Kong     return list;
488f7bc8ef8SAmos Kong }
489f7bc8ef8SAmos Kong 
virtio_net_query_rxfilter(NetClientState * nc)490b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
491b1be4280SAmos Kong {
492b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
493f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
494b1be4280SAmos Kong     RxFilterInfo *info;
49554aa3de7SEric Blake     strList *str_list;
496f7bc8ef8SAmos Kong     int i;
497b1be4280SAmos Kong 
498b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
499b1be4280SAmos Kong     info->name = g_strdup(nc->name);
500b1be4280SAmos Kong     info->promiscuous = n->promisc;
501b1be4280SAmos Kong 
502b1be4280SAmos Kong     if (n->nouni) {
503b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
504b1be4280SAmos Kong     } else if (n->alluni) {
505b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
506b1be4280SAmos Kong     } else {
507b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
508b1be4280SAmos Kong     }
509b1be4280SAmos Kong 
510b1be4280SAmos Kong     if (n->nomulti) {
511b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
512b1be4280SAmos Kong     } else if (n->allmulti) {
513b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
514b1be4280SAmos Kong     } else {
515b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
516b1be4280SAmos Kong     }
517b1be4280SAmos Kong 
518b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
519b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
520b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
521b1be4280SAmos Kong 
522b0575ba4SScott Feldman     info->main_mac = qemu_mac_strdup_printf(n->mac);
523b1be4280SAmos Kong 
524b1be4280SAmos Kong     str_list = NULL;
525b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
52654aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
52754aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
528b1be4280SAmos Kong     }
529b1be4280SAmos Kong     info->unicast_table = str_list;
530b1be4280SAmos Kong 
531b1be4280SAmos Kong     str_list = NULL;
532b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
53354aa3de7SEric Blake         QAPI_LIST_PREPEND(str_list,
53454aa3de7SEric Blake                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
535b1be4280SAmos Kong     }
536b1be4280SAmos Kong     info->multicast_table = str_list;
537f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
538b1be4280SAmos Kong 
53995129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
540f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
541f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
542f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
543f7bc8ef8SAmos Kong     } else {
544f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
545b1be4280SAmos Kong     }
546b1be4280SAmos Kong 
547b1be4280SAmos Kong     /* enable event notification after query */
548b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
549b1be4280SAmos Kong 
550b1be4280SAmos Kong     return info;
551b1be4280SAmos Kong }
552b1be4280SAmos Kong 
virtio_net_queue_reset(VirtIODevice * vdev,uint32_t queue_index)5537dc6be52SXuan Zhuo static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index)
5547dc6be52SXuan Zhuo {
5557dc6be52SXuan Zhuo     VirtIONet *n = VIRTIO_NET(vdev);
556f47af0afSXuan Zhuo     NetClientState *nc;
557f47af0afSXuan Zhuo 
558f47af0afSXuan Zhuo     /* validate queue_index and skip for cvq */
559f47af0afSXuan Zhuo     if (queue_index >= n->max_queue_pairs * 2) {
560f47af0afSXuan Zhuo         return;
561f47af0afSXuan Zhuo     }
562f47af0afSXuan Zhuo 
563f47af0afSXuan Zhuo     nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
5647dc6be52SXuan Zhuo 
5657dc6be52SXuan Zhuo     if (!nc->peer) {
5667dc6be52SXuan Zhuo         return;
5677dc6be52SXuan Zhuo     }
5687dc6be52SXuan Zhuo 
5697dc6be52SXuan Zhuo     if (get_vhost_net(nc->peer) &&
5707dc6be52SXuan Zhuo         nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
5717dc6be52SXuan Zhuo         vhost_net_virtqueue_reset(vdev, nc, queue_index);
5727dc6be52SXuan Zhuo     }
5737dc6be52SXuan Zhuo 
5747dc6be52SXuan Zhuo     flush_or_purge_queued_packets(nc);
5757dc6be52SXuan Zhuo }
5767dc6be52SXuan Zhuo 
virtio_net_queue_enable(VirtIODevice * vdev,uint32_t queue_index)5777f863302SKangjie Xu static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index)
5787f863302SKangjie Xu {
5797f863302SKangjie Xu     VirtIONet *n = VIRTIO_NET(vdev);
580f47af0afSXuan Zhuo     NetClientState *nc;
5817f863302SKangjie Xu     int r;
5827f863302SKangjie Xu 
583f47af0afSXuan Zhuo     /* validate queue_index and skip for cvq */
584f47af0afSXuan Zhuo     if (queue_index >= n->max_queue_pairs * 2) {
585f47af0afSXuan Zhuo         return;
586f47af0afSXuan Zhuo     }
587f47af0afSXuan Zhuo 
588f47af0afSXuan Zhuo     nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
589f47af0afSXuan Zhuo 
5907f863302SKangjie Xu     if (!nc->peer || !vdev->vhost_started) {
5917f863302SKangjie Xu         return;
5927f863302SKangjie Xu     }
5937f863302SKangjie Xu 
5947f863302SKangjie Xu     if (get_vhost_net(nc->peer) &&
5957f863302SKangjie Xu         nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
5967f863302SKangjie Xu         r = vhost_net_virtqueue_restart(vdev, nc, queue_index);
5977f863302SKangjie Xu         if (r < 0) {
5987f863302SKangjie Xu             error_report("unable to restart vhost net virtqueue: %d, "
5997f863302SKangjie Xu                             "when resetting the queue", queue_index);
6007f863302SKangjie Xu         }
6017f863302SKangjie Xu     }
6027f863302SKangjie Xu }
6037f863302SKangjie Xu 
peer_test_vnet_hdr(VirtIONet * n)6046e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
6056e790746SPaolo Bonzini {
6066e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
6076e790746SPaolo Bonzini     if (!nc->peer) {
6086e790746SPaolo Bonzini         return;
6096e790746SPaolo Bonzini     }
6106e790746SPaolo Bonzini 
611d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
6126e790746SPaolo Bonzini }
6136e790746SPaolo Bonzini 
peer_has_vnet_hdr(VirtIONet * n)6146e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
6156e790746SPaolo Bonzini {
6166e790746SPaolo Bonzini     return n->has_vnet_hdr;
6176e790746SPaolo Bonzini }
6186e790746SPaolo Bonzini 
peer_has_ufo(VirtIONet * n)6196e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
6206e790746SPaolo Bonzini {
6216e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
6226e790746SPaolo Bonzini         return 0;
6236e790746SPaolo Bonzini 
624d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
6256e790746SPaolo Bonzini 
6266e790746SPaolo Bonzini     return n->has_ufo;
6276e790746SPaolo Bonzini }
6286e790746SPaolo Bonzini 
peer_has_uso(VirtIONet * n)62953da8b5aSYuri Benditovich static int peer_has_uso(VirtIONet *n)
63053da8b5aSYuri Benditovich {
63153da8b5aSYuri Benditovich     if (!peer_has_vnet_hdr(n)) {
63253da8b5aSYuri Benditovich         return 0;
63353da8b5aSYuri Benditovich     }
63453da8b5aSYuri Benditovich 
63553da8b5aSYuri Benditovich     return qemu_has_uso(qemu_get_queue(n->nic)->peer);
63653da8b5aSYuri Benditovich }
63753da8b5aSYuri Benditovich 
virtio_net_set_mrg_rx_bufs(VirtIONet * n,int mergeable_rx_bufs,int version_1,int hash_report)638bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
639e22f0603SYuri Benditovich                                        int version_1, int hash_report)
6406e790746SPaolo Bonzini {
6416e790746SPaolo Bonzini     int i;
6426e790746SPaolo Bonzini     NetClientState *nc;
6436e790746SPaolo Bonzini 
6446e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
6456e790746SPaolo Bonzini 
646bb9d17f8SCornelia Huck     if (version_1) {
647e22f0603SYuri Benditovich         n->guest_hdr_len = hash_report ?
648e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_v1_hash) :
649e22f0603SYuri Benditovich             sizeof(struct virtio_net_hdr_mrg_rxbuf);
650e22f0603SYuri Benditovich         n->rss_data.populate_hash = !!hash_report;
651bb9d17f8SCornelia Huck     } else {
6526e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
653bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
654bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
65513d40aa8SAkihiko Odaki         n->rss_data.populate_hash = false;
656bb9d17f8SCornelia Huck     }
6576e790746SPaolo Bonzini 
658441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
6596e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
6606e790746SPaolo Bonzini 
6616e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
662d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
663d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
6646e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
6656e790746SPaolo Bonzini         }
6666e790746SPaolo Bonzini     }
6676e790746SPaolo Bonzini }
6686e790746SPaolo Bonzini 
virtio_net_max_tx_queue_size(VirtIONet * n)6692eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n)
6702eef278bSMichael S. Tsirkin {
6712eef278bSMichael S. Tsirkin     NetClientState *peer = n->nic_conf.peers.ncs[0];
6722eef278bSMichael S. Tsirkin 
6732eef278bSMichael S. Tsirkin     /*
6740ea5778fSEugenio Pérez      * Backends other than vhost-user or vhost-vdpa don't support max queue
6750ea5778fSEugenio Pérez      * size.
6762eef278bSMichael S. Tsirkin      */
6772eef278bSMichael S. Tsirkin     if (!peer) {
6782eef278bSMichael S. Tsirkin         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
6792eef278bSMichael S. Tsirkin     }
6802eef278bSMichael S. Tsirkin 
6810ea5778fSEugenio Pérez     switch(peer->info->type) {
6820ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_USER:
6830ea5778fSEugenio Pérez     case NET_CLIENT_DRIVER_VHOST_VDPA:
6842eef278bSMichael S. Tsirkin         return VIRTQUEUE_MAX_SIZE;
6850ea5778fSEugenio Pérez     default:
6860ea5778fSEugenio Pérez         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
6870ea5778fSEugenio Pérez     };
6882eef278bSMichael S. Tsirkin }
6892eef278bSMichael S. Tsirkin 
peer_attach(VirtIONet * n,int index)6906e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
6916e790746SPaolo Bonzini {
6926e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
6936e790746SPaolo Bonzini 
6946e790746SPaolo Bonzini     if (!nc->peer) {
6956e790746SPaolo Bonzini         return 0;
6966e790746SPaolo Bonzini     }
6976e790746SPaolo Bonzini 
698f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
6997263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 1);
7007263a0adSChangchun Ouyang     }
7017263a0adSChangchun Ouyang 
702f394b2e2SEric Blake     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
7036e790746SPaolo Bonzini         return 0;
7046e790746SPaolo Bonzini     }
7056e790746SPaolo Bonzini 
706441537f1SJason Wang     if (n->max_queue_pairs == 1) {
7071074b879SJason Wang         return 0;
7081074b879SJason Wang     }
7091074b879SJason Wang 
7106e790746SPaolo Bonzini     return tap_enable(nc->peer);
7116e790746SPaolo Bonzini }
7126e790746SPaolo Bonzini 
peer_detach(VirtIONet * n,int index)7136e790746SPaolo Bonzini static int peer_detach(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, 0);
7237263a0adSChangchun Ouyang     }
7247263a0adSChangchun Ouyang 
725f394b2e2SEric Blake     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
7266e790746SPaolo Bonzini         return 0;
7276e790746SPaolo Bonzini     }
7286e790746SPaolo Bonzini 
7296e790746SPaolo Bonzini     return tap_disable(nc->peer);
7306e790746SPaolo Bonzini }
7316e790746SPaolo Bonzini 
virtio_net_set_queue_pairs(VirtIONet * n)732441537f1SJason Wang static void virtio_net_set_queue_pairs(VirtIONet *n)
7336e790746SPaolo Bonzini {
7346e790746SPaolo Bonzini     int i;
735ddfa83eaSJoel Stanley     int r;
7366e790746SPaolo Bonzini 
73768b5f314SYuri Benditovich     if (n->nic->peer_deleted) {
73868b5f314SYuri Benditovich         return;
73968b5f314SYuri Benditovich     }
74068b5f314SYuri Benditovich 
741441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
742441537f1SJason Wang         if (i < n->curr_queue_pairs) {
743ddfa83eaSJoel Stanley             r = peer_attach(n, i);
744ddfa83eaSJoel Stanley             assert(!r);
7456e790746SPaolo Bonzini         } else {
746ddfa83eaSJoel Stanley             r = peer_detach(n, i);
747ddfa83eaSJoel Stanley             assert(!r);
7486e790746SPaolo Bonzini         }
7496e790746SPaolo Bonzini     }
7506e790746SPaolo Bonzini }
7516e790746SPaolo Bonzini 
752ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
7536e790746SPaolo Bonzini 
virtio_net_get_features(VirtIODevice * vdev,uint64_t features,Error ** errp)7549d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
7559d5b731dSJason Wang                                         Error **errp)
7566e790746SPaolo Bonzini {
75717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
7586e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
7596e790746SPaolo Bonzini 
760da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
761da3e8a23SShannon Zhao     features |= n->host_features;
762da3e8a23SShannon Zhao 
7630cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
7646e790746SPaolo Bonzini 
7656e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
7660cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
7670cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
7680cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
7690cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
7706e790746SPaolo Bonzini 
7710cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
7720cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
7730cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
7740cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
775e22f0603SYuri Benditovich 
77653da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
77753da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
77853da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
77953da8b5aSYuri Benditovich 
780e22f0603SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
7816e790746SPaolo Bonzini     }
7826e790746SPaolo Bonzini 
7836e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
7840cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
7850cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
7866e790746SPaolo Bonzini     }
7876e790746SPaolo Bonzini 
78853da8b5aSYuri Benditovich     if (!peer_has_uso(n)) {
78953da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
79053da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
79153da8b5aSYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
79253da8b5aSYuri Benditovich     }
79353da8b5aSYuri Benditovich 
794ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
7956e790746SPaolo Bonzini         return features;
7966e790746SPaolo Bonzini     }
7972974e916SYuri Benditovich 
7980145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
79959079029SYuri Benditovich         virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
8000145c393SAndrew Melnychenko     }
80175ebec11SMaxime Coquelin     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
80275ebec11SMaxime Coquelin     vdev->backend_features = features;
80375ebec11SMaxime Coquelin 
80475ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
80575ebec11SMaxime Coquelin             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
80675ebec11SMaxime Coquelin         features |= (1ULL << VIRTIO_NET_F_MTU);
80775ebec11SMaxime Coquelin     }
80875ebec11SMaxime Coquelin 
809cd69d47cSEugenio Pérez     /*
810cd69d47cSEugenio Pérez      * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
811cd69d47cSEugenio Pérez      * enabled. This happens in the vDPA case.
812cd69d47cSEugenio Pérez      *
813cd69d47cSEugenio Pérez      * Make sure the feature set is not incoherent, as the driver could refuse
814cd69d47cSEugenio Pérez      * to start.
815cd69d47cSEugenio Pérez      *
816cd69d47cSEugenio Pérez      * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
817cd69d47cSEugenio Pérez      * helping guest to notify the new location with vDPA devices that does not
818cd69d47cSEugenio Pérez      * support it.
819cd69d47cSEugenio Pérez      */
820cd69d47cSEugenio Pérez     if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) {
821cd69d47cSEugenio Pérez         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE);
822cd69d47cSEugenio Pérez     }
823cd69d47cSEugenio Pérez 
82475ebec11SMaxime Coquelin     return features;
8256e790746SPaolo Bonzini }
8266e790746SPaolo Bonzini 
virtio_net_bad_features(VirtIODevice * vdev)827019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
8286e790746SPaolo Bonzini {
829019a3edbSGerd Hoffmann     uint64_t features = 0;
8306e790746SPaolo Bonzini 
8316e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
8326e790746SPaolo Bonzini      * but also these: */
8330cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
8340cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
8350cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
8360cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
8370cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
8386e790746SPaolo Bonzini 
8396e790746SPaolo Bonzini     return features;
8406e790746SPaolo Bonzini }
8416e790746SPaolo Bonzini 
virtio_net_apply_guest_offloads(VirtIONet * n)842644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
843644c9858SDmitry Fleytman {
844ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
845644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
846644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
847644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
848644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
8492ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)),
8502ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)),
8512ab0ec31SAndrew Melnychenko             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6)));
852644c9858SDmitry Fleytman }
853644c9858SDmitry Fleytman 
virtio_net_guest_offloads_by_features(uint64_t features)85453da8b5aSYuri Benditovich static uint64_t virtio_net_guest_offloads_by_features(uint64_t features)
855644c9858SDmitry Fleytman {
856644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
857644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
858644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
859644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
860644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
86153da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_UFO)  |
86253da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_USO4) |
86353da8b5aSYuri Benditovich         (1ULL << VIRTIO_NET_F_GUEST_USO6);
864644c9858SDmitry Fleytman 
865644c9858SDmitry Fleytman     return guest_offloads_mask & features;
866644c9858SDmitry Fleytman }
867644c9858SDmitry Fleytman 
virtio_net_supported_guest_offloads(const VirtIONet * n)8680b545b1eSHawkins Jiawei uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n)
869644c9858SDmitry Fleytman {
870644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
871644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
872644c9858SDmitry Fleytman }
873644c9858SDmitry Fleytman 
874f5e1847bSJuan Quintela typedef struct {
875f5e1847bSJuan Quintela     VirtIONet *n;
87612b2fad7SKevin Wolf     DeviceState *dev;
87712b2fad7SKevin Wolf } FailoverDevice;
878f5e1847bSJuan Quintela 
879f5e1847bSJuan Quintela /**
88012b2fad7SKevin Wolf  * Set the failover primary device
881f5e1847bSJuan Quintela  *
882f5e1847bSJuan Quintela  * @opaque: FailoverId to setup
883f5e1847bSJuan Quintela  * @opts: opts for device we are handling
884f5e1847bSJuan Quintela  * @errp: returns an error if this function fails
885f5e1847bSJuan Quintela  */
failover_set_primary(DeviceState * dev,void * opaque)88612b2fad7SKevin Wolf static int failover_set_primary(DeviceState *dev, void *opaque)
887f5e1847bSJuan Quintela {
88812b2fad7SKevin Wolf     FailoverDevice *fdev = opaque;
88912b2fad7SKevin Wolf     PCIDevice *pci_dev = (PCIDevice *)
89012b2fad7SKevin Wolf         object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE);
891f5e1847bSJuan Quintela 
89212b2fad7SKevin Wolf     if (!pci_dev) {
89312b2fad7SKevin Wolf         return 0;
89412b2fad7SKevin Wolf     }
89512b2fad7SKevin Wolf 
89612b2fad7SKevin Wolf     if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) {
89712b2fad7SKevin Wolf         fdev->dev = dev;
898f5e1847bSJuan Quintela         return 1;
899f5e1847bSJuan Quintela     }
900f5e1847bSJuan Quintela 
901f5e1847bSJuan Quintela     return 0;
902f5e1847bSJuan Quintela }
903f5e1847bSJuan Quintela 
904f5e1847bSJuan Quintela /**
90585d3b931SJuan Quintela  * Find the primary device for this failover virtio-net
90685d3b931SJuan Quintela  *
90785d3b931SJuan Quintela  * @n: VirtIONet device
90885d3b931SJuan Quintela  * @errp: returns an error if this function fails
90985d3b931SJuan Quintela  */
failover_find_primary_device(VirtIONet * n)9100a0a27d6SJuan Quintela static DeviceState *failover_find_primary_device(VirtIONet *n)
9119711cd0dSJens Freimann {
91212b2fad7SKevin Wolf     FailoverDevice fdev = {
91312b2fad7SKevin Wolf         .n = n,
91412b2fad7SKevin Wolf     };
9159711cd0dSJens Freimann 
91612b2fad7SKevin Wolf     qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL,
91712b2fad7SKevin Wolf                        NULL, NULL, &fdev);
91812b2fad7SKevin Wolf     return fdev.dev;
9199711cd0dSJens Freimann }
9209711cd0dSJens Freimann 
failover_add_primary(VirtIONet * n,Error ** errp)92121e8709bSJuan Quintela static void failover_add_primary(VirtIONet *n, Error **errp)
92221e8709bSJuan Quintela {
92321e8709bSJuan Quintela     Error *err = NULL;
92421e8709bSJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
92521e8709bSJuan Quintela 
92621e8709bSJuan Quintela     if (dev) {
92721e8709bSJuan Quintela         return;
92821e8709bSJuan Quintela     }
92921e8709bSJuan Quintela 
930259a10dbSKevin Wolf     if (!n->primary_opts) {
93197ca9c59SLaurent Vivier         error_setg(errp, "Primary device not found");
93297ca9c59SLaurent Vivier         error_append_hint(errp, "Virtio-net failover will not work. Make "
93397ca9c59SLaurent Vivier                           "sure primary device has parameter"
93497ca9c59SLaurent Vivier                           " failover_pair_id=%s\n", n->netclient_name);
93521e8709bSJuan Quintela         return;
93621e8709bSJuan Quintela     }
937259a10dbSKevin Wolf 
938f3558b1bSKevin Wolf     dev = qdev_device_add_from_qdict(n->primary_opts,
939f3558b1bSKevin Wolf                                      n->primary_opts_from_json,
940f3558b1bSKevin Wolf                                      &err);
94121e8709bSJuan Quintela     if (err) {
942f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
943259a10dbSKevin Wolf         n->primary_opts = NULL;
94400e7b129SLaurent Vivier     } else {
94500e7b129SLaurent Vivier         object_unref(OBJECT(dev));
94621e8709bSJuan Quintela     }
94721e8709bSJuan Quintela     error_propagate(errp, err);
94821e8709bSJuan Quintela }
94921e8709bSJuan Quintela 
virtio_net_set_features(VirtIODevice * vdev,uint64_t features)950d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
9516e790746SPaolo Bonzini {
95217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
9539711cd0dSJens Freimann     Error *err = NULL;
9546e790746SPaolo Bonzini     int i;
9556e790746SPaolo Bonzini 
95675ebec11SMaxime Coquelin     if (n->mtu_bypass_backend &&
95775ebec11SMaxime Coquelin             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
95875ebec11SMaxime Coquelin         features &= ~(1ULL << VIRTIO_NET_F_MTU);
95975ebec11SMaxime Coquelin     }
96075ebec11SMaxime Coquelin 
961ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
96259079029SYuri Benditovich                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
96395129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
9646e790746SPaolo Bonzini 
965ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
96695129d6fSCornelia Huck                                virtio_has_feature(features,
967bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
96895129d6fSCornelia Huck                                virtio_has_feature(features,
969e22f0603SYuri Benditovich                                                   VIRTIO_F_VERSION_1),
970e22f0603SYuri Benditovich                                virtio_has_feature(features,
971e22f0603SYuri Benditovich                                                   VIRTIO_NET_F_HASH_REPORT));
9726e790746SPaolo Bonzini 
9732974e916SYuri Benditovich     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9742974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
9752974e916SYuri Benditovich     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
9762974e916SYuri Benditovich         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
977e22f0603SYuri Benditovich     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
9782974e916SYuri Benditovich 
9796e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
980644c9858SDmitry Fleytman         n->curr_guest_offloads =
981644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
982644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
9836e790746SPaolo Bonzini     }
9846e790746SPaolo Bonzini 
985441537f1SJason Wang     for (i = 0;  i < n->max_queue_pairs; i++) {
9866e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
9876e790746SPaolo Bonzini 
988ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
9896e790746SPaolo Bonzini             continue;
9906e790746SPaolo Bonzini         }
991ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
992c9bdc449SHyman Huang(黄勇) 
993c9bdc449SHyman Huang(黄勇)         /*
994c9bdc449SHyman Huang(黄勇)          * keep acked_features in NetVhostUserState up-to-date so it
995c9bdc449SHyman Huang(黄勇)          * can't miss any features configured by guest virtio driver.
996c9bdc449SHyman Huang(黄勇)          */
997c9bdc449SHyman Huang(黄勇)         vhost_net_save_acked_features(nc->peer);
9986e790746SPaolo Bonzini     }
9990b1eaa88SStefan Fritsch 
100006b636a1SHawkins Jiawei     if (!virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
10010b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
10020b1eaa88SStefan Fritsch     }
10039711cd0dSJens Freimann 
10049711cd0dSJens Freimann     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
10059711cd0dSJens Freimann         qapi_event_send_failover_negotiated(n->netclient_name);
1006e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, false);
10079711cd0dSJens Freimann         failover_add_primary(n, &err);
10089711cd0dSJens Freimann         if (err) {
10091b529d90SLaurent Vivier             if (!qtest_enabled()) {
10109711cd0dSJens Freimann                 warn_report_err(err);
10111b529d90SLaurent Vivier             } else {
10121b529d90SLaurent Vivier                 error_free(err);
10131b529d90SLaurent Vivier             }
10149711cd0dSJens Freimann         }
10156e790746SPaolo Bonzini     }
101621e8709bSJuan Quintela }
10176e790746SPaolo Bonzini 
virtio_net_handle_rx_mode(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)10186e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
10196e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
10206e790746SPaolo Bonzini {
10216e790746SPaolo Bonzini     uint8_t on;
10226e790746SPaolo Bonzini     size_t s;
1023b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
10246e790746SPaolo Bonzini 
10256e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
10266e790746SPaolo Bonzini     if (s != sizeof(on)) {
10276e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10286e790746SPaolo Bonzini     }
10296e790746SPaolo Bonzini 
10306e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
10316e790746SPaolo Bonzini         n->promisc = on;
10326e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
10336e790746SPaolo Bonzini         n->allmulti = on;
10346e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
10356e790746SPaolo Bonzini         n->alluni = on;
10366e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
10376e790746SPaolo Bonzini         n->nomulti = on;
10386e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
10396e790746SPaolo Bonzini         n->nouni = on;
10406e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
10416e790746SPaolo Bonzini         n->nobcast = on;
10426e790746SPaolo Bonzini     } else {
10436e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
10446e790746SPaolo Bonzini     }
10456e790746SPaolo Bonzini 
1046b1be4280SAmos Kong     rxfilter_notify(nc);
1047b1be4280SAmos Kong 
10486e790746SPaolo Bonzini     return VIRTIO_NET_OK;
10496e790746SPaolo Bonzini }
10506e790746SPaolo Bonzini 
virtio_net_handle_offloads(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)1051644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
1052644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
1053644c9858SDmitry Fleytman {
1054644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1055644c9858SDmitry Fleytman     uint64_t offloads;
1056644c9858SDmitry Fleytman     size_t s;
1057644c9858SDmitry Fleytman 
105895129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1059644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1060644c9858SDmitry Fleytman     }
1061644c9858SDmitry Fleytman 
1062644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
1063644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
1064644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1065644c9858SDmitry Fleytman     }
1066644c9858SDmitry Fleytman 
1067644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
1068644c9858SDmitry Fleytman         uint64_t supported_offloads;
1069644c9858SDmitry Fleytman 
1070189ae6bbSJason Wang         offloads = virtio_ldq_p(vdev, &offloads);
1071189ae6bbSJason Wang 
1072644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
1073644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1074644c9858SDmitry Fleytman         }
1075644c9858SDmitry Fleytman 
10762974e916SYuri Benditovich         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10772974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
10782974e916SYuri Benditovich         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
10792974e916SYuri Benditovich             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
10802974e916SYuri Benditovich         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
10812974e916SYuri Benditovich 
1082644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
1083644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
1084644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
1085644c9858SDmitry Fleytman         }
1086644c9858SDmitry Fleytman 
1087644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
1088644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1089644c9858SDmitry Fleytman 
1090644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
1091644c9858SDmitry Fleytman     } else {
1092644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
1093644c9858SDmitry Fleytman     }
1094644c9858SDmitry Fleytman }
1095644c9858SDmitry Fleytman 
virtio_net_handle_mac(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)10966e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
10976e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
10986e790746SPaolo Bonzini {
10991399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11006e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
11016e790746SPaolo Bonzini     size_t s;
1102b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11036e790746SPaolo Bonzini 
11046e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
11056e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
11066e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
11076e790746SPaolo Bonzini         }
11086e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
11096e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
11106e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1111b1be4280SAmos Kong         rxfilter_notify(nc);
1112b1be4280SAmos Kong 
11136e790746SPaolo Bonzini         return VIRTIO_NET_OK;
11146e790746SPaolo Bonzini     }
11156e790746SPaolo Bonzini 
11166e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
11176e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
11186e790746SPaolo Bonzini     }
11196e790746SPaolo Bonzini 
1120cae2e556SAmos Kong     int in_use = 0;
1121cae2e556SAmos Kong     int first_multi = 0;
1122cae2e556SAmos Kong     uint8_t uni_overflow = 0;
1123cae2e556SAmos Kong     uint8_t multi_overflow = 0;
1124cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
11256e790746SPaolo Bonzini 
11266e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11276e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11281399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11296e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1130b1be4280SAmos Kong         goto error;
11316e790746SPaolo Bonzini     }
11326e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11336e790746SPaolo Bonzini 
11346e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1135b1be4280SAmos Kong         goto error;
11366e790746SPaolo Bonzini     }
11376e790746SPaolo Bonzini 
11386e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1139cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
11406e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
11416e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1142b1be4280SAmos Kong             goto error;
11436e790746SPaolo Bonzini         }
1144cae2e556SAmos Kong         in_use += mac_data.entries;
11456e790746SPaolo Bonzini     } else {
1146cae2e556SAmos Kong         uni_overflow = 1;
11476e790746SPaolo Bonzini     }
11486e790746SPaolo Bonzini 
11496e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
11506e790746SPaolo Bonzini 
1151cae2e556SAmos Kong     first_multi = in_use;
11526e790746SPaolo Bonzini 
11536e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
11546e790746SPaolo Bonzini                    sizeof(mac_data.entries));
11551399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
11566e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
1157b1be4280SAmos Kong         goto error;
11586e790746SPaolo Bonzini     }
11596e790746SPaolo Bonzini 
11606e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
11616e790746SPaolo Bonzini 
11626e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1163b1be4280SAmos Kong         goto error;
11646e790746SPaolo Bonzini     }
11656e790746SPaolo Bonzini 
1166edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1167cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
11686e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
11696e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
1170b1be4280SAmos Kong             goto error;
11716e790746SPaolo Bonzini         }
1172cae2e556SAmos Kong         in_use += mac_data.entries;
11736e790746SPaolo Bonzini     } else {
1174cae2e556SAmos Kong         multi_overflow = 1;
11756e790746SPaolo Bonzini     }
11766e790746SPaolo Bonzini 
1177cae2e556SAmos Kong     n->mac_table.in_use = in_use;
1178cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
1179cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
1180cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
1181cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1182cae2e556SAmos Kong     g_free(macs);
1183b1be4280SAmos Kong     rxfilter_notify(nc);
1184b1be4280SAmos Kong 
11856e790746SPaolo Bonzini     return VIRTIO_NET_OK;
1186b1be4280SAmos Kong 
1187b1be4280SAmos Kong error:
1188cae2e556SAmos Kong     g_free(macs);
1189b1be4280SAmos Kong     return VIRTIO_NET_ERR;
11906e790746SPaolo Bonzini }
11916e790746SPaolo Bonzini 
virtio_net_handle_vlan_table(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)11926e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
11936e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
11946e790746SPaolo Bonzini {
11951399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11966e790746SPaolo Bonzini     uint16_t vid;
11976e790746SPaolo Bonzini     size_t s;
1198b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
11996e790746SPaolo Bonzini 
12006e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
12011399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
12026e790746SPaolo Bonzini     if (s != sizeof(vid)) {
12036e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12046e790746SPaolo Bonzini     }
12056e790746SPaolo Bonzini 
12066e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
12076e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12086e790746SPaolo Bonzini 
12096e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
12106e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
12116e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
12126e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
12136e790746SPaolo Bonzini     else
12146e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
12156e790746SPaolo Bonzini 
1216b1be4280SAmos Kong     rxfilter_notify(nc);
1217b1be4280SAmos Kong 
12186e790746SPaolo Bonzini     return VIRTIO_NET_OK;
12196e790746SPaolo Bonzini }
12206e790746SPaolo Bonzini 
virtio_net_handle_announce(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)1221f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1222f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
1223f57fcf70SJason Wang {
12249d8c6a25SDr. David Alan Gilbert     trace_virtio_net_handle_announce(n->announce_timer.round);
1225f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1226f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
1227f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
12289d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
12299d8c6a25SDr. David Alan Gilbert             qemu_announce_timer_step(&n->announce_timer);
1230f57fcf70SJason Wang         }
1231f57fcf70SJason Wang         return VIRTIO_NET_OK;
1232f57fcf70SJason Wang     } else {
1233f57fcf70SJason Wang         return VIRTIO_NET_ERR;
1234f57fcf70SJason Wang     }
1235f57fcf70SJason Wang }
1236f57fcf70SJason Wang 
virtio_net_attach_ebpf_to_backend(NICState * nic,int prog_fd)12370145c393SAndrew Melnychenko static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd)
12380145c393SAndrew Melnychenko {
12390145c393SAndrew Melnychenko     NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0);
12400145c393SAndrew Melnychenko     if (nc == NULL || nc->info->set_steering_ebpf == NULL) {
12410145c393SAndrew Melnychenko         return false;
12420145c393SAndrew Melnychenko     }
12430145c393SAndrew Melnychenko 
1244ae311fb3SDaniel P. Berrangé     trace_virtio_net_rss_attach_ebpf(nic, prog_fd);
12450145c393SAndrew Melnychenko     return nc->info->set_steering_ebpf(nc, prog_fd);
12460145c393SAndrew Melnychenko }
12470145c393SAndrew Melnychenko 
rss_data_to_rss_config(struct VirtioNetRssData * data,struct EBPFRSSConfig * config)12480145c393SAndrew Melnychenko static void rss_data_to_rss_config(struct VirtioNetRssData *data,
12490145c393SAndrew Melnychenko                                    struct EBPFRSSConfig *config)
12500145c393SAndrew Melnychenko {
12510145c393SAndrew Melnychenko     config->redirect = data->redirect;
12520145c393SAndrew Melnychenko     config->populate_hash = data->populate_hash;
12530145c393SAndrew Melnychenko     config->hash_types = data->hash_types;
12540145c393SAndrew Melnychenko     config->indirections_len = data->indirections_len;
12550145c393SAndrew Melnychenko     config->default_queue = data->default_queue;
12560145c393SAndrew Melnychenko }
12570145c393SAndrew Melnychenko 
virtio_net_attach_ebpf_rss(VirtIONet * n)1258493a2403SDaniel P. Berrangé static bool virtio_net_attach_ebpf_rss(VirtIONet *n)
12590145c393SAndrew Melnychenko {
12600145c393SAndrew Melnychenko     struct EBPFRSSConfig config = {};
12610145c393SAndrew Melnychenko 
12620145c393SAndrew Melnychenko     if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
12630145c393SAndrew Melnychenko         return false;
12640145c393SAndrew Melnychenko     }
12650145c393SAndrew Melnychenko 
12660145c393SAndrew Melnychenko     rss_data_to_rss_config(&n->rss_data, &config);
12670145c393SAndrew Melnychenko 
12680145c393SAndrew Melnychenko     if (!ebpf_rss_set_all(&n->ebpf_rss, &config,
126900b69f1dSDaniel P. Berrangé                           n->rss_data.indirections_table, n->rss_data.key,
127000b69f1dSDaniel P. Berrangé                           NULL)) {
12710145c393SAndrew Melnychenko         return false;
12720145c393SAndrew Melnychenko     }
12730145c393SAndrew Melnychenko 
12740145c393SAndrew Melnychenko     if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) {
12750145c393SAndrew Melnychenko         return false;
12760145c393SAndrew Melnychenko     }
12770145c393SAndrew Melnychenko 
12780145c393SAndrew Melnychenko     return true;
12790145c393SAndrew Melnychenko }
12800145c393SAndrew Melnychenko 
virtio_net_detach_ebpf_rss(VirtIONet * n)1281493a2403SDaniel P. Berrangé static void virtio_net_detach_ebpf_rss(VirtIONet *n)
12820145c393SAndrew Melnychenko {
12830145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
12840145c393SAndrew Melnychenko }
12850145c393SAndrew Melnychenko 
virtio_net_commit_rss_config(VirtIONet * n)12860e07198eSAkihiko Odaki static void virtio_net_commit_rss_config(VirtIONet *n)
12870e07198eSAkihiko Odaki {
12880e07198eSAkihiko Odaki     if (n->rss_data.enabled) {
12890e07198eSAkihiko Odaki         n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
12900e07198eSAkihiko Odaki         if (n->rss_data.populate_hash) {
1291493a2403SDaniel P. Berrangé             virtio_net_detach_ebpf_rss(n);
1292493a2403SDaniel P. Berrangé         } else if (!virtio_net_attach_ebpf_rss(n)) {
12930e07198eSAkihiko Odaki             if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
12940e07198eSAkihiko Odaki                 warn_report("Can't load eBPF RSS for vhost");
12950e07198eSAkihiko Odaki             } else {
12960e07198eSAkihiko Odaki                 warn_report("Can't load eBPF RSS - fallback to software RSS");
12970e07198eSAkihiko Odaki                 n->rss_data.enabled_software_rss = true;
12980e07198eSAkihiko Odaki             }
12990e07198eSAkihiko Odaki         }
13000e07198eSAkihiko Odaki 
1301ae311fb3SDaniel P. Berrangé         trace_virtio_net_rss_enable(n,
1302ae311fb3SDaniel P. Berrangé                                     n->rss_data.hash_types,
13030e07198eSAkihiko Odaki                                     n->rss_data.indirections_len,
13040e07198eSAkihiko Odaki                                     sizeof(n->rss_data.key));
13050e07198eSAkihiko Odaki     } else {
1306493a2403SDaniel P. Berrangé         virtio_net_detach_ebpf_rss(n);
1307ae311fb3SDaniel P. Berrangé         trace_virtio_net_rss_disable(n);
13080e07198eSAkihiko Odaki     }
13090e07198eSAkihiko Odaki }
13100e07198eSAkihiko Odaki 
virtio_net_disable_rss(VirtIONet * n)13110e07198eSAkihiko Odaki static void virtio_net_disable_rss(VirtIONet *n)
13120e07198eSAkihiko Odaki {
13130e07198eSAkihiko Odaki     if (!n->rss_data.enabled) {
13140e07198eSAkihiko Odaki         return;
13150e07198eSAkihiko Odaki     }
13160e07198eSAkihiko Odaki 
13170e07198eSAkihiko Odaki     n->rss_data.enabled = false;
13180e07198eSAkihiko Odaki     virtio_net_commit_rss_config(n);
13190e07198eSAkihiko Odaki }
13200e07198eSAkihiko Odaki 
virtio_net_load_ebpf_fds(VirtIONet * n,Error ** errp)1321b5900dffSDaniel P. Berrangé static bool virtio_net_load_ebpf_fds(VirtIONet *n, Error **errp)
13220145c393SAndrew Melnychenko {
13236b230b7dSAndrew Melnychenko     int fds[EBPF_RSS_MAX_FDS] = { [0 ... EBPF_RSS_MAX_FDS - 1] = -1};
13246b230b7dSAndrew Melnychenko     int ret = true;
13256b230b7dSAndrew Melnychenko     int i = 0;
13266b230b7dSAndrew Melnychenko 
13276b230b7dSAndrew Melnychenko     if (n->nr_ebpf_rss_fds != EBPF_RSS_MAX_FDS) {
1328b5900dffSDaniel P. Berrangé         error_setg(errp, "Expected %d file descriptors but got %d",
13296b230b7dSAndrew Melnychenko                    EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds);
13300145c393SAndrew Melnychenko         return false;
13310145c393SAndrew Melnychenko     }
13320145c393SAndrew Melnychenko 
13336b230b7dSAndrew Melnychenko     for (i = 0; i < n->nr_ebpf_rss_fds; i++) {
1334b5900dffSDaniel P. Berrangé         fds[i] = monitor_fd_param(monitor_cur(), n->ebpf_rss_fds[i], errp);
1335283be596SAkihiko Odaki         if (fds[i] < 0) {
13366b230b7dSAndrew Melnychenko             ret = false;
13376b230b7dSAndrew Melnychenko             goto exit;
13386b230b7dSAndrew Melnychenko         }
13396b230b7dSAndrew Melnychenko     }
13406b230b7dSAndrew Melnychenko 
1341b5900dffSDaniel P. Berrangé     ret = ebpf_rss_load_fds(&n->ebpf_rss, fds[0], fds[1], fds[2], fds[3], errp);
13426b230b7dSAndrew Melnychenko 
13436b230b7dSAndrew Melnychenko exit:
1344283be596SAkihiko Odaki     if (!ret) {
13456b230b7dSAndrew Melnychenko         for (i = 0; i < n->nr_ebpf_rss_fds && fds[i] != -1; i++) {
13466b230b7dSAndrew Melnychenko             close(fds[i]);
13476b230b7dSAndrew Melnychenko         }
13486b230b7dSAndrew Melnychenko     }
13496b230b7dSAndrew Melnychenko 
13506b230b7dSAndrew Melnychenko     return ret;
13516b230b7dSAndrew Melnychenko }
13526b230b7dSAndrew Melnychenko 
virtio_net_load_ebpf(VirtIONet * n,Error ** errp)1353b5900dffSDaniel P. Berrangé static bool virtio_net_load_ebpf(VirtIONet *n, Error **errp)
13546b230b7dSAndrew Melnychenko {
13556b230b7dSAndrew Melnychenko     bool ret = false;
13566b230b7dSAndrew Melnychenko 
13576b230b7dSAndrew Melnychenko     if (virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
1358ae311fb3SDaniel P. Berrangé         trace_virtio_net_rss_load(n, n->nr_ebpf_rss_fds, n->ebpf_rss_fds);
1359b5900dffSDaniel P. Berrangé         if (n->ebpf_rss_fds) {
1360b5900dffSDaniel P. Berrangé             ret = virtio_net_load_ebpf_fds(n, errp);
1361b5900dffSDaniel P. Berrangé         } else {
1362b5900dffSDaniel P. Berrangé             ret = ebpf_rss_load(&n->ebpf_rss, errp);
13636b230b7dSAndrew Melnychenko         }
13646b230b7dSAndrew Melnychenko     }
13656b230b7dSAndrew Melnychenko 
13666b230b7dSAndrew Melnychenko     return ret;
13670145c393SAndrew Melnychenko }
13680145c393SAndrew Melnychenko 
virtio_net_unload_ebpf(VirtIONet * n)13690145c393SAndrew Melnychenko static void virtio_net_unload_ebpf(VirtIONet *n)
13700145c393SAndrew Melnychenko {
13710145c393SAndrew Melnychenko     virtio_net_attach_ebpf_to_backend(n->nic, -1);
13720145c393SAndrew Melnychenko     ebpf_rss_unload(&n->ebpf_rss);
137359079029SYuri Benditovich }
137459079029SYuri Benditovich 
virtio_net_handle_rss(VirtIONet * n,struct iovec * iov,unsigned int iov_cnt,bool do_rss)137559079029SYuri Benditovich static uint16_t virtio_net_handle_rss(VirtIONet *n,
1376e22f0603SYuri Benditovich                                       struct iovec *iov,
1377e22f0603SYuri Benditovich                                       unsigned int iov_cnt,
1378e22f0603SYuri Benditovich                                       bool do_rss)
137959079029SYuri Benditovich {
138059079029SYuri Benditovich     VirtIODevice *vdev = VIRTIO_DEVICE(n);
138159079029SYuri Benditovich     struct virtio_net_rss_config cfg;
138259079029SYuri Benditovich     size_t s, offset = 0, size_get;
1383441537f1SJason Wang     uint16_t queue_pairs, i;
138459079029SYuri Benditovich     struct {
138559079029SYuri Benditovich         uint16_t us;
138659079029SYuri Benditovich         uint8_t b;
138759079029SYuri Benditovich     } QEMU_PACKED temp;
138859079029SYuri Benditovich     const char *err_msg = "";
138959079029SYuri Benditovich     uint32_t err_value = 0;
139059079029SYuri Benditovich 
1391e22f0603SYuri Benditovich     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
139259079029SYuri Benditovich         err_msg = "RSS is not negotiated";
139359079029SYuri Benditovich         goto error;
139459079029SYuri Benditovich     }
1395e22f0603SYuri Benditovich     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1396e22f0603SYuri Benditovich         err_msg = "Hash report is not negotiated";
1397e22f0603SYuri Benditovich         goto error;
1398e22f0603SYuri Benditovich     }
139959079029SYuri Benditovich     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
140059079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
140159079029SYuri Benditovich     if (s != size_get) {
140259079029SYuri Benditovich         err_msg = "Short command buffer";
140359079029SYuri Benditovich         err_value = (uint32_t)s;
140459079029SYuri Benditovich         goto error;
140559079029SYuri Benditovich     }
140659079029SYuri Benditovich     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
140759079029SYuri Benditovich     n->rss_data.indirections_len =
140859079029SYuri Benditovich         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
1409e22f0603SYuri Benditovich     if (!do_rss) {
1410cd76e8fcSAkihiko Odaki         n->rss_data.indirections_len = 0;
1411e22f0603SYuri Benditovich     }
1412cd76e8fcSAkihiko Odaki     if (n->rss_data.indirections_len >= VIRTIO_NET_RSS_MAX_TABLE_LEN) {
1413cd76e8fcSAkihiko Odaki         err_msg = "Too large indirection table";
141459079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
141559079029SYuri Benditovich         goto error;
141659079029SYuri Benditovich     }
1417cd76e8fcSAkihiko Odaki     n->rss_data.indirections_len++;
1418cd76e8fcSAkihiko Odaki     if (!is_power_of_2(n->rss_data.indirections_len)) {
1419cd76e8fcSAkihiko Odaki         err_msg = "Invalid size of indirection table";
142059079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
142159079029SYuri Benditovich         goto error;
142259079029SYuri Benditovich     }
1423e22f0603SYuri Benditovich     n->rss_data.default_queue = do_rss ?
1424e22f0603SYuri Benditovich         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1425441537f1SJason Wang     if (n->rss_data.default_queue >= n->max_queue_pairs) {
142659079029SYuri Benditovich         err_msg = "Invalid default queue";
142759079029SYuri Benditovich         err_value = n->rss_data.default_queue;
142859079029SYuri Benditovich         goto error;
142959079029SYuri Benditovich     }
143059079029SYuri Benditovich     offset += size_get;
143159079029SYuri Benditovich     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
143259079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
143359079029SYuri Benditovich     n->rss_data.indirections_table = g_malloc(size_get);
143459079029SYuri Benditovich     if (!n->rss_data.indirections_table) {
143559079029SYuri Benditovich         err_msg = "Can't allocate indirections table";
143659079029SYuri Benditovich         err_value = n->rss_data.indirections_len;
143759079029SYuri Benditovich         goto error;
143859079029SYuri Benditovich     }
143959079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset,
144059079029SYuri Benditovich                    n->rss_data.indirections_table, size_get);
144159079029SYuri Benditovich     if (s != size_get) {
144259079029SYuri Benditovich         err_msg = "Short indirection table buffer";
144359079029SYuri Benditovich         err_value = (uint32_t)s;
144459079029SYuri Benditovich         goto error;
144559079029SYuri Benditovich     }
144659079029SYuri Benditovich     for (i = 0; i < n->rss_data.indirections_len; ++i) {
144759079029SYuri Benditovich         uint16_t val = n->rss_data.indirections_table[i];
144859079029SYuri Benditovich         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
144959079029SYuri Benditovich     }
145059079029SYuri Benditovich     offset += size_get;
145159079029SYuri Benditovich     size_get = sizeof(temp);
145259079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
145359079029SYuri Benditovich     if (s != size_get) {
1454441537f1SJason Wang         err_msg = "Can't get queue_pairs";
145559079029SYuri Benditovich         err_value = (uint32_t)s;
145659079029SYuri Benditovich         goto error;
145759079029SYuri Benditovich     }
1458441537f1SJason Wang     queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs;
1459441537f1SJason Wang     if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) {
1460441537f1SJason Wang         err_msg = "Invalid number of queue_pairs";
1461441537f1SJason Wang         err_value = queue_pairs;
146259079029SYuri Benditovich         goto error;
146359079029SYuri Benditovich     }
146459079029SYuri Benditovich     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
146559079029SYuri Benditovich         err_msg = "Invalid key size";
146659079029SYuri Benditovich         err_value = temp.b;
146759079029SYuri Benditovich         goto error;
146859079029SYuri Benditovich     }
146959079029SYuri Benditovich     if (!temp.b && n->rss_data.hash_types) {
147059079029SYuri Benditovich         err_msg = "No key provided";
147159079029SYuri Benditovich         err_value = 0;
147259079029SYuri Benditovich         goto error;
147359079029SYuri Benditovich     }
147459079029SYuri Benditovich     if (!temp.b && !n->rss_data.hash_types) {
147559079029SYuri Benditovich         virtio_net_disable_rss(n);
1476441537f1SJason Wang         return queue_pairs;
147759079029SYuri Benditovich     }
147859079029SYuri Benditovich     offset += size_get;
147959079029SYuri Benditovich     size_get = temp.b;
148059079029SYuri Benditovich     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
148159079029SYuri Benditovich     if (s != size_get) {
148259079029SYuri Benditovich         err_msg = "Can get key buffer";
148359079029SYuri Benditovich         err_value = (uint32_t)s;
148459079029SYuri Benditovich         goto error;
148559079029SYuri Benditovich     }
148659079029SYuri Benditovich     n->rss_data.enabled = true;
14870e07198eSAkihiko Odaki     virtio_net_commit_rss_config(n);
1488441537f1SJason Wang     return queue_pairs;
148959079029SYuri Benditovich error:
1490ae311fb3SDaniel P. Berrangé     trace_virtio_net_rss_error(n, err_msg, err_value);
149159079029SYuri Benditovich     virtio_net_disable_rss(n);
149259079029SYuri Benditovich     return 0;
149359079029SYuri Benditovich }
149459079029SYuri Benditovich 
virtio_net_handle_mq(VirtIONet * n,uint8_t cmd,struct iovec * iov,unsigned int iov_cnt)14956e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
14966e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
14976e790746SPaolo Bonzini {
149817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1499441537f1SJason Wang     uint16_t queue_pairs;
15002a7888ccSSi-Wei Liu     NetClientState *nc = qemu_get_queue(n->nic);
15016e790746SPaolo Bonzini 
150259079029SYuri Benditovich     virtio_net_disable_rss(n);
1503e22f0603SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1504441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false);
1505441537f1SJason Wang         return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1506e22f0603SYuri Benditovich     }
150759079029SYuri Benditovich     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1508441537f1SJason Wang         queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true);
150959079029SYuri Benditovich     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
151059079029SYuri Benditovich         struct virtio_net_ctrl_mq mq;
151159079029SYuri Benditovich         size_t s;
151259079029SYuri Benditovich         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
151359079029SYuri Benditovich             return VIRTIO_NET_ERR;
151459079029SYuri Benditovich         }
15156e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
15166e790746SPaolo Bonzini         if (s != sizeof(mq)) {
15176e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
15186e790746SPaolo Bonzini         }
1519441537f1SJason Wang         queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
15206e790746SPaolo Bonzini 
152159079029SYuri Benditovich     } else {
15226e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
15236e790746SPaolo Bonzini     }
15246e790746SPaolo Bonzini 
1525441537f1SJason Wang     if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1526441537f1SJason Wang         queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1527441537f1SJason Wang         queue_pairs > n->max_queue_pairs ||
15286e790746SPaolo Bonzini         !n->multiqueue) {
15296e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
15306e790746SPaolo Bonzini     }
15316e790746SPaolo Bonzini 
1532441537f1SJason Wang     n->curr_queue_pairs = queue_pairs;
1533ca8717f9SEugenio Pérez     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
1534ca8717f9SEugenio Pérez         /*
1535ca8717f9SEugenio Pérez          * Avoid updating the backend for a vdpa device: We're only interested
1536ca8717f9SEugenio Pérez          * in updating the device model queues.
1537ca8717f9SEugenio Pérez          */
1538ca8717f9SEugenio Pérez         return VIRTIO_NET_OK;
1539ca8717f9SEugenio Pérez     }
1540441537f1SJason Wang     /* stop the backend before changing the number of queue_pairs to avoid handling a
15416e790746SPaolo Bonzini      * disabled queue */
154217a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
1543441537f1SJason Wang     virtio_net_set_queue_pairs(n);
15446e790746SPaolo Bonzini 
15456e790746SPaolo Bonzini     return VIRTIO_NET_OK;
15466e790746SPaolo Bonzini }
1547ba7eadb5SGreg Kurz 
virtio_net_handle_ctrl_iov(VirtIODevice * vdev,const struct iovec * in_sg,unsigned in_num,const struct iovec * out_sg,unsigned out_num)1548640b8a1cSEugenio Pérez size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
1549640b8a1cSEugenio Pérez                                   const struct iovec *in_sg, unsigned in_num,
1550640b8a1cSEugenio Pérez                                   const struct iovec *out_sg,
1551640b8a1cSEugenio Pérez                                   unsigned out_num)
15526e790746SPaolo Bonzini {
155317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15546e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
15556e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
15566e790746SPaolo Bonzini     size_t s;
1557771b6ed3SJason Wang     struct iovec *iov, *iov2;
1558640b8a1cSEugenio Pérez 
1559640b8a1cSEugenio Pérez     if (iov_size(in_sg, in_num) < sizeof(status) ||
1560640b8a1cSEugenio Pérez         iov_size(out_sg, out_num) < sizeof(ctrl)) {
1561640b8a1cSEugenio Pérez         virtio_error(vdev, "virtio-net ctrl missing headers");
1562640b8a1cSEugenio Pérez         return 0;
1563640b8a1cSEugenio Pérez     }
1564640b8a1cSEugenio Pérez 
1565640b8a1cSEugenio Pérez     iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num);
1566640b8a1cSEugenio Pérez     s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl));
1567640b8a1cSEugenio Pérez     iov_discard_front(&iov, &out_num, sizeof(ctrl));
1568640b8a1cSEugenio Pérez     if (s != sizeof(ctrl)) {
1569640b8a1cSEugenio Pérez         status = VIRTIO_NET_ERR;
1570640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1571640b8a1cSEugenio Pérez         status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num);
1572640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1573640b8a1cSEugenio Pérez         status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num);
1574640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1575640b8a1cSEugenio Pérez         status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num);
1576640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1577640b8a1cSEugenio Pérez         status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num);
1578640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1579640b8a1cSEugenio Pérez         status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
1580640b8a1cSEugenio Pérez     } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1581640b8a1cSEugenio Pérez         status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
1582640b8a1cSEugenio Pérez     }
1583640b8a1cSEugenio Pérez 
1584640b8a1cSEugenio Pérez     s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
1585640b8a1cSEugenio Pérez     assert(s == sizeof(status));
1586640b8a1cSEugenio Pérez 
1587640b8a1cSEugenio Pérez     g_free(iov2);
1588640b8a1cSEugenio Pérez     return sizeof(status);
1589640b8a1cSEugenio Pérez }
1590640b8a1cSEugenio Pérez 
virtio_net_handle_ctrl(VirtIODevice * vdev,VirtQueue * vq)1591640b8a1cSEugenio Pérez static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1592640b8a1cSEugenio Pérez {
1593640b8a1cSEugenio Pérez     VirtQueueElement *elem;
15946e790746SPaolo Bonzini 
159551b19ebeSPaolo Bonzini     for (;;) {
1596640b8a1cSEugenio Pérez         size_t written;
159751b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
159851b19ebeSPaolo Bonzini         if (!elem) {
159951b19ebeSPaolo Bonzini             break;
160051b19ebeSPaolo Bonzini         }
1601640b8a1cSEugenio Pérez 
1602640b8a1cSEugenio Pérez         written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num,
1603640b8a1cSEugenio Pérez                                              elem->out_sg, elem->out_num);
1604640b8a1cSEugenio Pérez         if (written > 0) {
1605640b8a1cSEugenio Pérez             virtqueue_push(vq, elem, written);
1606640b8a1cSEugenio Pérez             virtio_notify(vdev, vq);
1607640b8a1cSEugenio Pérez             g_free(elem);
1608640b8a1cSEugenio Pérez         } else {
1609ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
1610ba7eadb5SGreg Kurz             g_free(elem);
1611ba7eadb5SGreg Kurz             break;
16126e790746SPaolo Bonzini         }
16136e790746SPaolo Bonzini     }
16146e790746SPaolo Bonzini }
16156e790746SPaolo Bonzini 
16166e790746SPaolo Bonzini /* RX */
16176e790746SPaolo Bonzini 
virtio_net_handle_rx(VirtIODevice * vdev,VirtQueue * vq)16186e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
16196e790746SPaolo Bonzini {
162017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
16216e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
16226e790746SPaolo Bonzini 
16236e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
16246e790746SPaolo Bonzini }
16256e790746SPaolo Bonzini 
virtio_net_can_receive(NetClientState * nc)1626b8c4b67eSPhilippe Mathieu-Daudé static bool virtio_net_can_receive(NetClientState *nc)
16276e790746SPaolo Bonzini {
16286e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
162917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
16306e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
16316e790746SPaolo Bonzini 
163217a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
1633b8c4b67eSPhilippe Mathieu-Daudé         return false;
16346e790746SPaolo Bonzini     }
16356e790746SPaolo Bonzini 
1636441537f1SJason Wang     if (nc->queue_index >= n->curr_queue_pairs) {
1637b8c4b67eSPhilippe Mathieu-Daudé         return false;
16386e790746SPaolo Bonzini     }
16396e790746SPaolo Bonzini 
16406e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
164117a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1642b8c4b67eSPhilippe Mathieu-Daudé         return false;
16436e790746SPaolo Bonzini     }
16446e790746SPaolo Bonzini 
1645b8c4b67eSPhilippe Mathieu-Daudé     return true;
16466e790746SPaolo Bonzini }
16476e790746SPaolo Bonzini 
virtio_net_has_buffers(VirtIONetQueue * q,int bufsize)16486e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
16496e790746SPaolo Bonzini {
1650f937309fSthomas     int opaque;
1651f937309fSthomas     unsigned int in_bytes;
16526e790746SPaolo Bonzini     VirtIONet *n = q->n;
16536e790746SPaolo Bonzini 
1654f937309fSthomas     while (virtio_queue_empty(q->rx_vq) || n->mergeable_rx_bufs) {
1655f937309fSthomas         opaque = virtqueue_get_avail_bytes(q->rx_vq, &in_bytes, NULL,
1656f937309fSthomas                                            bufsize, 0);
1657f937309fSthomas         /* Buffer is enough, disable notifiaction */
1658f937309fSthomas         if (bufsize <= in_bytes) {
1659f937309fSthomas             break;
1660f937309fSthomas         }
1661f937309fSthomas 
1662f937309fSthomas         if (virtio_queue_enable_notification_and_check(q->rx_vq, opaque)) {
1663f937309fSthomas             /* Guest has added some buffers, try again */
1664f937309fSthomas             continue;
1665f937309fSthomas         } else {
16666e790746SPaolo Bonzini             return 0;
16676e790746SPaolo Bonzini         }
16686e790746SPaolo Bonzini     }
16696e790746SPaolo Bonzini 
16706e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
1671f937309fSthomas 
16726e790746SPaolo Bonzini     return 1;
16736e790746SPaolo Bonzini }
16746e790746SPaolo Bonzini 
virtio_net_hdr_swap(VirtIODevice * vdev,struct virtio_net_hdr * hdr)16751399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1676032a74a1SCédric Le Goater {
16771399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
16781399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
16791399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
16801399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1681032a74a1SCédric Le Goater }
1682032a74a1SCédric Le Goater 
16836e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
16846e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
16856e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
16866e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
16876e790746SPaolo Bonzini  * dhclient yet.
16886e790746SPaolo Bonzini  *
16896e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
16906e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
16916e790746SPaolo Bonzini  * kernels.
16926e790746SPaolo Bonzini  *
16936e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
16946e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
16956e790746SPaolo Bonzini  * cache.
16966e790746SPaolo Bonzini  */
work_around_broken_dhclient(struct virtio_net_hdr * hdr,size_t * hdr_len,const uint8_t * buf,size_t buf_size,size_t * buf_offset)16976e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
16987987d2beSAkihiko Odaki                                         size_t *hdr_len, const uint8_t *buf,
16997987d2beSAkihiko Odaki                                         size_t buf_size, size_t *buf_offset)
17006e790746SPaolo Bonzini {
1701a8575f7fSAkihiko Odaki     size_t csum_size = ETH_HLEN + sizeof(struct ip_header) +
1702a8575f7fSAkihiko Odaki                        sizeof(struct udp_header);
1703a8575f7fSAkihiko Odaki 
17047987d2beSAkihiko Odaki     buf += *buf_offset;
17057987d2beSAkihiko Odaki     buf_size -= *buf_offset;
17067987d2beSAkihiko Odaki 
17076e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
17087987d2beSAkihiko Odaki         (buf_size >= csum_size && buf_size < 1500) && /* normal sized MTU */
17096e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
17106e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
17116e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
17127987d2beSAkihiko Odaki         memcpy((uint8_t *)hdr + *hdr_len, buf, csum_size);
17137987d2beSAkihiko Odaki         net_checksum_calculate((uint8_t *)hdr + *hdr_len, csum_size, CSUM_UDP);
17146e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
17157987d2beSAkihiko Odaki         *hdr_len += csum_size;
17167987d2beSAkihiko Odaki         *buf_offset += csum_size;
17176e790746SPaolo Bonzini     }
17186e790746SPaolo Bonzini }
17196e790746SPaolo Bonzini 
receive_header(VirtIONet * n,struct virtio_net_hdr * hdr,const void * buf,size_t buf_size,size_t * buf_offset)17207987d2beSAkihiko Odaki static size_t receive_header(VirtIONet *n, struct virtio_net_hdr *hdr,
17217987d2beSAkihiko Odaki                              const void *buf, size_t buf_size,
17227987d2beSAkihiko Odaki                              size_t *buf_offset)
17236e790746SPaolo Bonzini {
17247987d2beSAkihiko Odaki     size_t hdr_len = n->guest_hdr_len;
17257987d2beSAkihiko Odaki 
17267987d2beSAkihiko Odaki     memcpy(hdr, buf, sizeof(struct virtio_net_hdr));
17277987d2beSAkihiko Odaki 
17287987d2beSAkihiko Odaki     *buf_offset = n->host_hdr_len;
17297987d2beSAkihiko Odaki     work_around_broken_dhclient(hdr, &hdr_len, buf, buf_size, buf_offset);
17301bfa316cSGreg Kurz 
17311bfa316cSGreg Kurz     if (n->needs_vnet_hdr_swap) {
17327987d2beSAkihiko Odaki         virtio_net_hdr_swap(VIRTIO_DEVICE(n), hdr);
17331bfa316cSGreg Kurz     }
17347987d2beSAkihiko Odaki 
17357987d2beSAkihiko Odaki     return hdr_len;
17366e790746SPaolo Bonzini }
17376e790746SPaolo Bonzini 
receive_filter(VirtIONet * n,const uint8_t * buf,int size)17386e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
17396e790746SPaolo Bonzini {
17406e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
17416e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
17426e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
17436e790746SPaolo Bonzini     int i;
17446e790746SPaolo Bonzini 
17456e790746SPaolo Bonzini     if (n->promisc)
17466e790746SPaolo Bonzini         return 1;
17476e790746SPaolo Bonzini 
17486e790746SPaolo Bonzini     ptr += n->host_hdr_len;
17496e790746SPaolo Bonzini 
17506e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
17517542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
17526e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
17536e790746SPaolo Bonzini             return 0;
17546e790746SPaolo Bonzini     }
17556e790746SPaolo Bonzini 
17566e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
17576e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
17586e790746SPaolo Bonzini             return !n->nobcast;
17596e790746SPaolo Bonzini         } else if (n->nomulti) {
17606e790746SPaolo Bonzini             return 0;
17616e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
17626e790746SPaolo Bonzini             return 1;
17636e790746SPaolo Bonzini         }
17646e790746SPaolo Bonzini 
17656e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
17666e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17676e790746SPaolo Bonzini                 return 1;
17686e790746SPaolo Bonzini             }
17696e790746SPaolo Bonzini         }
17706e790746SPaolo Bonzini     } else { // unicast
17716e790746SPaolo Bonzini         if (n->nouni) {
17726e790746SPaolo Bonzini             return 0;
17736e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
17746e790746SPaolo Bonzini             return 1;
17756e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
17766e790746SPaolo Bonzini             return 1;
17776e790746SPaolo Bonzini         }
17786e790746SPaolo Bonzini 
17796e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
17806e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
17816e790746SPaolo Bonzini                 return 1;
17826e790746SPaolo Bonzini             }
17836e790746SPaolo Bonzini         }
17846e790746SPaolo Bonzini     }
17856e790746SPaolo Bonzini 
17866e790746SPaolo Bonzini     return 0;
17876e790746SPaolo Bonzini }
17886e790746SPaolo Bonzini 
virtio_net_get_hash_type(bool hasip4,bool hasip6,EthL4HdrProto l4hdr_proto,uint32_t types)178969ff5ef8SAkihiko Odaki static uint8_t virtio_net_get_hash_type(bool hasip4,
179069ff5ef8SAkihiko Odaki                                         bool hasip6,
179165f474bbSAkihiko Odaki                                         EthL4HdrProto l4hdr_proto,
17924474e37aSYuri Benditovich                                         uint32_t types)
17934474e37aSYuri Benditovich {
179469ff5ef8SAkihiko Odaki     if (hasip4) {
179565f474bbSAkihiko Odaki         switch (l4hdr_proto) {
179665f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
179765f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
17984474e37aSYuri Benditovich                 return NetPktRssIpV4Tcp;
17994474e37aSYuri Benditovich             }
180065f474bbSAkihiko Odaki             break;
180165f474bbSAkihiko Odaki 
180265f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
180365f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
18044474e37aSYuri Benditovich                 return NetPktRssIpV4Udp;
18054474e37aSYuri Benditovich             }
180665f474bbSAkihiko Odaki             break;
180765f474bbSAkihiko Odaki 
180865f474bbSAkihiko Odaki         default:
180965f474bbSAkihiko Odaki             break;
181065f474bbSAkihiko Odaki         }
181165f474bbSAkihiko Odaki 
18124474e37aSYuri Benditovich         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
18134474e37aSYuri Benditovich             return NetPktRssIpV4;
18144474e37aSYuri Benditovich         }
181569ff5ef8SAkihiko Odaki     } else if (hasip6) {
181665f474bbSAkihiko Odaki         switch (l4hdr_proto) {
181765f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_TCP:
181865f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) {
181965f474bbSAkihiko Odaki                 return NetPktRssIpV6TcpEx;
182065f474bbSAkihiko Odaki             }
182165f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
182265f474bbSAkihiko Odaki                 return NetPktRssIpV6Tcp;
182365f474bbSAkihiko Odaki             }
182465f474bbSAkihiko Odaki             break;
18254474e37aSYuri Benditovich 
182665f474bbSAkihiko Odaki         case ETH_L4_HDR_PROTO_UDP:
182765f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) {
182865f474bbSAkihiko Odaki                 return NetPktRssIpV6UdpEx;
18294474e37aSYuri Benditovich             }
183065f474bbSAkihiko Odaki             if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
183165f474bbSAkihiko Odaki                 return NetPktRssIpV6Udp;
18324474e37aSYuri Benditovich             }
183365f474bbSAkihiko Odaki             break;
183465f474bbSAkihiko Odaki 
183565f474bbSAkihiko Odaki         default:
183665f474bbSAkihiko Odaki             break;
183765f474bbSAkihiko Odaki         }
183865f474bbSAkihiko Odaki 
183965f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) {
184065f474bbSAkihiko Odaki             return NetPktRssIpV6Ex;
184165f474bbSAkihiko Odaki         }
184265f474bbSAkihiko Odaki         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
184365f474bbSAkihiko Odaki             return NetPktRssIpV6;
18444474e37aSYuri Benditovich         }
18454474e37aSYuri Benditovich     }
18464474e37aSYuri Benditovich     return 0xff;
18474474e37aSYuri Benditovich }
18484474e37aSYuri Benditovich 
virtio_net_process_rss(NetClientState * nc,const uint8_t * buf,size_t size,struct virtio_net_hdr_v1_hash * hdr)18494474e37aSYuri Benditovich static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
1850a4c960eeSAkihiko Odaki                                   size_t size,
1851a4c960eeSAkihiko Odaki                                   struct virtio_net_hdr_v1_hash *hdr)
18526e790746SPaolo Bonzini {
18536e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
1854e22f0603SYuri Benditovich     unsigned int index = nc->queue_index, new_index = index;
18554474e37aSYuri Benditovich     struct NetRxPkt *pkt = n->rx_pkt;
18564474e37aSYuri Benditovich     uint8_t net_hash_type;
18574474e37aSYuri Benditovich     uint32_t hash;
185865f474bbSAkihiko Odaki     bool hasip4, hasip6;
185965f474bbSAkihiko Odaki     EthL4HdrProto l4hdr_proto;
1860e22f0603SYuri Benditovich     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1861e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv4,
1862e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv4,
1863e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6,
1864e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6,
1865e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1866e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1867e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv4,
1868e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6,
1869e22f0603SYuri Benditovich         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1870e22f0603SYuri Benditovich     };
18712f0fa232SAkihiko Odaki     struct iovec iov = {
18722f0fa232SAkihiko Odaki         .iov_base = (void *)buf,
18732f0fa232SAkihiko Odaki         .iov_len = size
18742f0fa232SAkihiko Odaki     };
18754474e37aSYuri Benditovich 
18762f0fa232SAkihiko Odaki     net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len);
187765f474bbSAkihiko Odaki     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
187865f474bbSAkihiko Odaki     net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto,
18794474e37aSYuri Benditovich                                              n->rss_data.hash_types);
18804474e37aSYuri Benditovich     if (net_hash_type > NetPktRssIpV6UdpEx) {
1881e22f0603SYuri Benditovich         if (n->rss_data.populate_hash) {
1882a4c960eeSAkihiko Odaki             hdr->hash_value = VIRTIO_NET_HASH_REPORT_NONE;
1883a4c960eeSAkihiko Odaki             hdr->hash_report = 0;
1884e22f0603SYuri Benditovich         }
1885e22f0603SYuri Benditovich         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
18864474e37aSYuri Benditovich     }
18874474e37aSYuri Benditovich 
18884474e37aSYuri Benditovich     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1889e22f0603SYuri Benditovich 
1890e22f0603SYuri Benditovich     if (n->rss_data.populate_hash) {
1891a4c960eeSAkihiko Odaki         hdr->hash_value = hash;
1892a4c960eeSAkihiko Odaki         hdr->hash_report = reports[net_hash_type];
1893e22f0603SYuri Benditovich     }
1894e22f0603SYuri Benditovich 
1895e22f0603SYuri Benditovich     if (n->rss_data.redirect) {
18964474e37aSYuri Benditovich         new_index = hash & (n->rss_data.indirections_len - 1);
18974474e37aSYuri Benditovich         new_index = n->rss_data.indirections_table[new_index];
18984474e37aSYuri Benditovich     }
1899e22f0603SYuri Benditovich 
1900e22f0603SYuri Benditovich     return (index == new_index) ? -1 : new_index;
19014474e37aSYuri Benditovich }
19024474e37aSYuri Benditovich 
19037987d2beSAkihiko Odaki typedef struct Header {
19047987d2beSAkihiko Odaki     struct virtio_net_hdr_v1_hash virtio_net;
19057987d2beSAkihiko Odaki     struct eth_header eth;
19067987d2beSAkihiko Odaki     struct ip_header ip;
19077987d2beSAkihiko Odaki     struct udp_header udp;
19087987d2beSAkihiko Odaki } Header;
19097987d2beSAkihiko Odaki 
virtio_net_receive_rcu(NetClientState * nc,const uint8_t * buf,size_t size)19104474e37aSYuri Benditovich static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
19111981fa9dSAkihiko Odaki                                       size_t size)
19124474e37aSYuri Benditovich {
19134474e37aSYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
19141981fa9dSAkihiko Odaki     VirtIONetQueue *q;
191517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1916bedd7e93SJason Wang     VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE];
1917bedd7e93SJason Wang     size_t lens[VIRTQUEUE_MAX_SIZE];
19186e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
19197987d2beSAkihiko Odaki     Header hdr;
19206e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
1921bedd7e93SJason Wang     size_t offset, i, guest_offset, j;
1922bedd7e93SJason Wang     ssize_t err;
19236e790746SPaolo Bonzini 
19247987d2beSAkihiko Odaki     memset(&hdr.virtio_net, 0, sizeof(hdr.virtio_net));
192517437418SAkihiko Odaki 
19261981fa9dSAkihiko Odaki     if (n->rss_data.enabled && n->rss_data.enabled_software_rss) {
19277987d2beSAkihiko Odaki         int index = virtio_net_process_rss(nc, buf, size, &hdr.virtio_net);
19284474e37aSYuri Benditovich         if (index >= 0) {
19291981fa9dSAkihiko Odaki             nc = qemu_get_subqueue(n->nic, index % n->curr_queue_pairs);
19304474e37aSYuri Benditovich         }
19314474e37aSYuri Benditovich     }
19324474e37aSYuri Benditovich 
1933162bdb81SAkihiko Odaki     if (!virtio_net_can_receive(nc)) {
1934162bdb81SAkihiko Odaki         return -1;
1935162bdb81SAkihiko Odaki     }
1936162bdb81SAkihiko Odaki 
19371981fa9dSAkihiko Odaki     q = virtio_net_get_subqueue(nc);
19381981fa9dSAkihiko Odaki 
19396e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
19406e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
19416e790746SPaolo Bonzini         return 0;
19426e790746SPaolo Bonzini     }
19436e790746SPaolo Bonzini 
19446e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
19456e790746SPaolo Bonzini         return size;
19466e790746SPaolo Bonzini 
19476e790746SPaolo Bonzini     offset = i = 0;
19486e790746SPaolo Bonzini 
19496e790746SPaolo Bonzini     while (offset < size) {
195051b19ebeSPaolo Bonzini         VirtQueueElement *elem;
19516e790746SPaolo Bonzini         int len, total;
195251b19ebeSPaolo Bonzini         const struct iovec *sg;
19536e790746SPaolo Bonzini 
19546e790746SPaolo Bonzini         total = 0;
19556e790746SPaolo Bonzini 
1956bedd7e93SJason Wang         if (i == VIRTQUEUE_MAX_SIZE) {
1957bedd7e93SJason Wang             virtio_error(vdev, "virtio-net unexpected long buffer chain");
1958bedd7e93SJason Wang             err = size;
1959bedd7e93SJason Wang             goto err;
1960bedd7e93SJason Wang         }
1961bedd7e93SJason Wang 
196251b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
196351b19ebeSPaolo Bonzini         if (!elem) {
1964ba10b9c0SGreg Kurz             if (i) {
1965ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
19666e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1967019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1968019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
19696e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1970019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1971019a3edbSGerd Hoffmann                              vdev->guest_features);
1972ba10b9c0SGreg Kurz             }
1973bedd7e93SJason Wang             err = -1;
1974bedd7e93SJason Wang             goto err;
19756e790746SPaolo Bonzini         }
19766e790746SPaolo Bonzini 
197751b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1978ba10b9c0SGreg Kurz             virtio_error(vdev,
1979ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1980ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1981ba10b9c0SGreg Kurz             g_free(elem);
1982bedd7e93SJason Wang             err = -1;
1983bedd7e93SJason Wang             goto err;
19846e790746SPaolo Bonzini         }
19856e790746SPaolo Bonzini 
198651b19ebeSPaolo Bonzini         sg = elem->in_sg;
19876e790746SPaolo Bonzini         if (i == 0) {
19886e790746SPaolo Bonzini             assert(offset == 0);
19896e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
19906e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
199151b19ebeSPaolo Bonzini                                     sg, elem->in_num,
19927987d2beSAkihiko Odaki                                     offsetof(typeof(hdr),
19937987d2beSAkihiko Odaki                                              virtio_net.hdr.num_buffers),
19947987d2beSAkihiko Odaki                                     sizeof(hdr.virtio_net.hdr.num_buffers));
19956e790746SPaolo Bonzini             }
19966e790746SPaolo Bonzini 
19977987d2beSAkihiko Odaki             guest_offset = n->has_vnet_hdr ?
19987987d2beSAkihiko Odaki                            receive_header(n, (struct virtio_net_hdr *)&hdr,
19997987d2beSAkihiko Odaki                                           buf, size, &offset) :
20007987d2beSAkihiko Odaki                            n->guest_hdr_len;
20017987d2beSAkihiko Odaki 
20027987d2beSAkihiko Odaki             iov_from_buf(sg, elem->in_num, 0, &hdr, guest_offset);
20037987d2beSAkihiko Odaki             total += guest_offset;
20046e790746SPaolo Bonzini         } else {
20056e790746SPaolo Bonzini             guest_offset = 0;
20066e790746SPaolo Bonzini         }
20076e790746SPaolo Bonzini 
20086e790746SPaolo Bonzini         /* copy in packet.  ugh */
200951b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
20106e790746SPaolo Bonzini                            buf + offset, size - offset);
20116e790746SPaolo Bonzini         total += len;
20126e790746SPaolo Bonzini         offset += len;
20136e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
20146e790746SPaolo Bonzini          * must have consumed the complete packet.
20156e790746SPaolo Bonzini          * Otherwise, drop it. */
20166e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
201727e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
201851b19ebeSPaolo Bonzini             g_free(elem);
2019bedd7e93SJason Wang             err = size;
2020bedd7e93SJason Wang             goto err;
20216e790746SPaolo Bonzini         }
20226e790746SPaolo Bonzini 
2023bedd7e93SJason Wang         elems[i] = elem;
2024bedd7e93SJason Wang         lens[i] = total;
2025bedd7e93SJason Wang         i++;
20266e790746SPaolo Bonzini     }
20276e790746SPaolo Bonzini 
20286e790746SPaolo Bonzini     if (mhdr_cnt) {
20297987d2beSAkihiko Odaki         virtio_stw_p(vdev, &hdr.virtio_net.hdr.num_buffers, i);
20306e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
20316e790746SPaolo Bonzini                      0,
20327987d2beSAkihiko Odaki                      &hdr.virtio_net.hdr.num_buffers,
20337987d2beSAkihiko Odaki                      sizeof hdr.virtio_net.hdr.num_buffers);
20346e790746SPaolo Bonzini     }
20356e790746SPaolo Bonzini 
2036bedd7e93SJason Wang     for (j = 0; j < i; j++) {
2037bedd7e93SJason Wang         /* signal other side */
2038bedd7e93SJason Wang         virtqueue_fill(q->rx_vq, elems[j], lens[j], j);
2039bedd7e93SJason Wang         g_free(elems[j]);
2040bedd7e93SJason Wang     }
2041bedd7e93SJason Wang 
20426e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
204317a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
20446e790746SPaolo Bonzini 
20456e790746SPaolo Bonzini     return size;
2046bedd7e93SJason Wang 
2047bedd7e93SJason Wang err:
2048bedd7e93SJason Wang     for (j = 0; j < i; j++) {
2049abe300d9SJason Wang         virtqueue_detach_element(q->rx_vq, elems[j], lens[j]);
2050bedd7e93SJason Wang         g_free(elems[j]);
2051bedd7e93SJason Wang     }
2052bedd7e93SJason Wang 
2053bedd7e93SJason Wang     return err;
20546e790746SPaolo Bonzini }
20556e790746SPaolo Bonzini 
virtio_net_do_receive(NetClientState * nc,const uint8_t * buf,size_t size)20562974e916SYuri Benditovich static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
205797cd965cSPaolo Bonzini                                   size_t size)
205897cd965cSPaolo Bonzini {
2059068ddfa9SDr. David Alan Gilbert     RCU_READ_LOCK_GUARD();
206097cd965cSPaolo Bonzini 
20611981fa9dSAkihiko Odaki     return virtio_net_receive_rcu(nc, buf, size);
206297cd965cSPaolo Bonzini }
206397cd965cSPaolo Bonzini 
20645814c084SPeter Maydell /*
20655814c084SPeter Maydell  * Accessors to read and write the IP packet data length field. This
20665814c084SPeter Maydell  * is a potentially unaligned network-byte-order 16 bit unsigned integer
20675814c084SPeter Maydell  * pointed to by unit->ip_len.
20685814c084SPeter Maydell  */
read_unit_ip_len(VirtioNetRscUnit * unit)20695814c084SPeter Maydell static uint16_t read_unit_ip_len(VirtioNetRscUnit *unit)
20705814c084SPeter Maydell {
20715814c084SPeter Maydell     return lduw_be_p(unit->ip_plen);
20725814c084SPeter Maydell }
20735814c084SPeter Maydell 
write_unit_ip_len(VirtioNetRscUnit * unit,uint16_t l)20745814c084SPeter Maydell static void write_unit_ip_len(VirtioNetRscUnit *unit, uint16_t l)
20755814c084SPeter Maydell {
20765814c084SPeter Maydell     stw_be_p(unit->ip_plen, l);
20775814c084SPeter Maydell }
20785814c084SPeter Maydell 
virtio_net_rsc_extract_unit4(VirtioNetRscChain * chain,const uint8_t * buf,VirtioNetRscUnit * unit)20792974e916SYuri Benditovich static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
20802974e916SYuri Benditovich                                          const uint8_t *buf,
20812974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20822974e916SYuri Benditovich {
20832974e916SYuri Benditovich     uint16_t ip_hdrlen;
20842974e916SYuri Benditovich     struct ip_header *ip;
20852974e916SYuri Benditovich 
20862974e916SYuri Benditovich     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
20872974e916SYuri Benditovich                               + sizeof(struct eth_header));
20882974e916SYuri Benditovich     unit->ip = (void *)ip;
20892974e916SYuri Benditovich     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
20902974e916SYuri Benditovich     unit->ip_plen = &ip->ip_len;
20912974e916SYuri Benditovich     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
20922974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
20935814c084SPeter Maydell     unit->payload = read_unit_ip_len(unit) - ip_hdrlen - unit->tcp_hdrlen;
20942974e916SYuri Benditovich }
20952974e916SYuri Benditovich 
virtio_net_rsc_extract_unit6(VirtioNetRscChain * chain,const uint8_t * buf,VirtioNetRscUnit * unit)20962974e916SYuri Benditovich static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
20972974e916SYuri Benditovich                                          const uint8_t *buf,
20982974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
20992974e916SYuri Benditovich {
21002974e916SYuri Benditovich     struct ip6_header *ip6;
21012974e916SYuri Benditovich 
21022974e916SYuri Benditovich     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
21032974e916SYuri Benditovich                                  + sizeof(struct eth_header));
21042974e916SYuri Benditovich     unit->ip = ip6;
21052974e916SYuri Benditovich     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
210678ee6bd0SPhilippe Mathieu-Daudé     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
21072974e916SYuri Benditovich                                         + sizeof(struct ip6_header));
21082974e916SYuri Benditovich     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
21092974e916SYuri Benditovich 
21102431f4f1SMichael Tokarev     /* There is a difference between payload length in ipv4 and v6,
21112974e916SYuri Benditovich        ip header is excluded in ipv6 */
21125814c084SPeter Maydell     unit->payload = read_unit_ip_len(unit) - unit->tcp_hdrlen;
21132974e916SYuri Benditovich }
21142974e916SYuri Benditovich 
virtio_net_rsc_drain_seg(VirtioNetRscChain * chain,VirtioNetRscSeg * seg)21152974e916SYuri Benditovich static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
21162974e916SYuri Benditovich                                        VirtioNetRscSeg *seg)
21172974e916SYuri Benditovich {
21182974e916SYuri Benditovich     int ret;
2119dd3d85e8SYuri Benditovich     struct virtio_net_hdr_v1 *h;
21202974e916SYuri Benditovich 
2121dd3d85e8SYuri Benditovich     h = (struct virtio_net_hdr_v1 *)seg->buf;
21222974e916SYuri Benditovich     h->flags = 0;
21232974e916SYuri Benditovich     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
21242974e916SYuri Benditovich 
21252974e916SYuri Benditovich     if (seg->is_coalesced) {
2126dd3d85e8SYuri Benditovich         h->rsc.segments = seg->packets;
2127dd3d85e8SYuri Benditovich         h->rsc.dup_acks = seg->dup_ack;
21282974e916SYuri Benditovich         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
21292974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
21302974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
21312974e916SYuri Benditovich         } else {
21322974e916SYuri Benditovich             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
21332974e916SYuri Benditovich         }
21342974e916SYuri Benditovich     }
21352974e916SYuri Benditovich 
21362974e916SYuri Benditovich     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
21372974e916SYuri Benditovich     QTAILQ_REMOVE(&chain->buffers, seg, next);
21382974e916SYuri Benditovich     g_free(seg->buf);
21392974e916SYuri Benditovich     g_free(seg);
21402974e916SYuri Benditovich 
21412974e916SYuri Benditovich     return ret;
21422974e916SYuri Benditovich }
21432974e916SYuri Benditovich 
virtio_net_rsc_purge(void * opq)21442974e916SYuri Benditovich static void virtio_net_rsc_purge(void *opq)
21452974e916SYuri Benditovich {
21462974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn;
21472974e916SYuri Benditovich     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
21482974e916SYuri Benditovich 
21492974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
21502974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
21512974e916SYuri Benditovich             chain->stat.purge_failed++;
21522974e916SYuri Benditovich             continue;
21532974e916SYuri Benditovich         }
21542974e916SYuri Benditovich     }
21552974e916SYuri Benditovich 
21562974e916SYuri Benditovich     chain->stat.timer++;
21572974e916SYuri Benditovich     if (!QTAILQ_EMPTY(&chain->buffers)) {
21582974e916SYuri Benditovich         timer_mod(chain->drain_timer,
215944bc14faSNicholas Piggin               qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + chain->n->rsc_timeout);
21602974e916SYuri Benditovich     }
21612974e916SYuri Benditovich }
21622974e916SYuri Benditovich 
virtio_net_rsc_cleanup(VirtIONet * n)21632974e916SYuri Benditovich static void virtio_net_rsc_cleanup(VirtIONet *n)
21642974e916SYuri Benditovich {
21652974e916SYuri Benditovich     VirtioNetRscChain *chain, *rn_chain;
21662974e916SYuri Benditovich     VirtioNetRscSeg *seg, *rn_seg;
21672974e916SYuri Benditovich 
21682974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
21692974e916SYuri Benditovich         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
21702974e916SYuri Benditovich             QTAILQ_REMOVE(&chain->buffers, seg, next);
21712974e916SYuri Benditovich             g_free(seg->buf);
21722974e916SYuri Benditovich             g_free(seg);
21732974e916SYuri Benditovich         }
21742974e916SYuri Benditovich 
21752974e916SYuri Benditovich         timer_free(chain->drain_timer);
21762974e916SYuri Benditovich         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
21772974e916SYuri Benditovich         g_free(chain);
21782974e916SYuri Benditovich     }
21792974e916SYuri Benditovich }
21802974e916SYuri Benditovich 
virtio_net_rsc_cache_buf(VirtioNetRscChain * chain,NetClientState * nc,const uint8_t * buf,size_t size)21812974e916SYuri Benditovich static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
21822974e916SYuri Benditovich                                      NetClientState *nc,
21832974e916SYuri Benditovich                                      const uint8_t *buf, size_t size)
21842974e916SYuri Benditovich {
21852974e916SYuri Benditovich     uint16_t hdr_len;
21862974e916SYuri Benditovich     VirtioNetRscSeg *seg;
21872974e916SYuri Benditovich 
21882974e916SYuri Benditovich     hdr_len = chain->n->guest_hdr_len;
2189b21e2380SMarkus Armbruster     seg = g_new(VirtioNetRscSeg, 1);
21902974e916SYuri Benditovich     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
21912974e916SYuri Benditovich         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
21922974e916SYuri Benditovich     memcpy(seg->buf, buf, size);
21932974e916SYuri Benditovich     seg->size = size;
21942974e916SYuri Benditovich     seg->packets = 1;
21952974e916SYuri Benditovich     seg->dup_ack = 0;
21962974e916SYuri Benditovich     seg->is_coalesced = 0;
21972974e916SYuri Benditovich     seg->nc = nc;
21982974e916SYuri Benditovich 
21992974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
22002974e916SYuri Benditovich     chain->stat.cache++;
22012974e916SYuri Benditovich 
22022974e916SYuri Benditovich     switch (chain->proto) {
22032974e916SYuri Benditovich     case ETH_P_IP:
22042974e916SYuri Benditovich         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
22052974e916SYuri Benditovich         break;
22062974e916SYuri Benditovich     case ETH_P_IPV6:
22072974e916SYuri Benditovich         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
22082974e916SYuri Benditovich         break;
22092974e916SYuri Benditovich     default:
22102974e916SYuri Benditovich         g_assert_not_reached();
22112974e916SYuri Benditovich     }
22122974e916SYuri Benditovich }
22132974e916SYuri Benditovich 
virtio_net_rsc_handle_ack(VirtioNetRscChain * chain,VirtioNetRscSeg * seg,const uint8_t * buf,struct tcp_header * n_tcp,struct tcp_header * o_tcp)22142974e916SYuri Benditovich static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
22152974e916SYuri Benditovich                                          VirtioNetRscSeg *seg,
22162974e916SYuri Benditovich                                          const uint8_t *buf,
22172974e916SYuri Benditovich                                          struct tcp_header *n_tcp,
22182974e916SYuri Benditovich                                          struct tcp_header *o_tcp)
22192974e916SYuri Benditovich {
22202974e916SYuri Benditovich     uint32_t nack, oack;
22212974e916SYuri Benditovich     uint16_t nwin, owin;
22222974e916SYuri Benditovich 
22232974e916SYuri Benditovich     nack = htonl(n_tcp->th_ack);
22242974e916SYuri Benditovich     nwin = htons(n_tcp->th_win);
22252974e916SYuri Benditovich     oack = htonl(o_tcp->th_ack);
22262974e916SYuri Benditovich     owin = htons(o_tcp->th_win);
22272974e916SYuri Benditovich 
22282974e916SYuri Benditovich     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
22292974e916SYuri Benditovich         chain->stat.ack_out_of_win++;
22302974e916SYuri Benditovich         return RSC_FINAL;
22312974e916SYuri Benditovich     } else if (nack == oack) {
22322974e916SYuri Benditovich         /* duplicated ack or window probe */
22332974e916SYuri Benditovich         if (nwin == owin) {
22342974e916SYuri Benditovich             /* duplicated ack, add dup ack count due to whql test up to 1 */
22352974e916SYuri Benditovich             chain->stat.dup_ack++;
22362974e916SYuri Benditovich             return RSC_FINAL;
22372974e916SYuri Benditovich         } else {
22382974e916SYuri Benditovich             /* Coalesce window update */
22392974e916SYuri Benditovich             o_tcp->th_win = n_tcp->th_win;
22402974e916SYuri Benditovich             chain->stat.win_update++;
22412974e916SYuri Benditovich             return RSC_COALESCE;
22422974e916SYuri Benditovich         }
22432974e916SYuri Benditovich     } else {
22442974e916SYuri Benditovich         /* pure ack, go to 'C', finalize*/
22452974e916SYuri Benditovich         chain->stat.pure_ack++;
22462974e916SYuri Benditovich         return RSC_FINAL;
22472974e916SYuri Benditovich     }
22482974e916SYuri Benditovich }
22492974e916SYuri Benditovich 
virtio_net_rsc_coalesce_data(VirtioNetRscChain * chain,VirtioNetRscSeg * seg,const uint8_t * buf,VirtioNetRscUnit * n_unit)22502974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
22512974e916SYuri Benditovich                                             VirtioNetRscSeg *seg,
22522974e916SYuri Benditovich                                             const uint8_t *buf,
22532974e916SYuri Benditovich                                             VirtioNetRscUnit *n_unit)
22542974e916SYuri Benditovich {
22552974e916SYuri Benditovich     void *data;
22562974e916SYuri Benditovich     uint16_t o_ip_len;
22572974e916SYuri Benditovich     uint32_t nseq, oseq;
22582974e916SYuri Benditovich     VirtioNetRscUnit *o_unit;
22592974e916SYuri Benditovich 
22602974e916SYuri Benditovich     o_unit = &seg->unit;
22615814c084SPeter Maydell     o_ip_len = read_unit_ip_len(o_unit);
22622974e916SYuri Benditovich     nseq = htonl(n_unit->tcp->th_seq);
22632974e916SYuri Benditovich     oseq = htonl(o_unit->tcp->th_seq);
22642974e916SYuri Benditovich 
22652974e916SYuri Benditovich     /* out of order or retransmitted. */
22662974e916SYuri Benditovich     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
22672974e916SYuri Benditovich         chain->stat.data_out_of_win++;
22682974e916SYuri Benditovich         return RSC_FINAL;
22692974e916SYuri Benditovich     }
22702974e916SYuri Benditovich 
22712974e916SYuri Benditovich     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
22722974e916SYuri Benditovich     if (nseq == oseq) {
22732974e916SYuri Benditovich         if ((o_unit->payload == 0) && n_unit->payload) {
22742974e916SYuri Benditovich             /* From no payload to payload, normal case, not a dup ack or etc */
22752974e916SYuri Benditovich             chain->stat.data_after_pure_ack++;
22762974e916SYuri Benditovich             goto coalesce;
22772974e916SYuri Benditovich         } else {
22782974e916SYuri Benditovich             return virtio_net_rsc_handle_ack(chain, seg, buf,
22792974e916SYuri Benditovich                                              n_unit->tcp, o_unit->tcp);
22802974e916SYuri Benditovich         }
22812974e916SYuri Benditovich     } else if ((nseq - oseq) != o_unit->payload) {
22822974e916SYuri Benditovich         /* Not a consistent packet, out of order */
22832974e916SYuri Benditovich         chain->stat.data_out_of_order++;
22842974e916SYuri Benditovich         return RSC_FINAL;
22852974e916SYuri Benditovich     } else {
22862974e916SYuri Benditovich coalesce:
22872974e916SYuri Benditovich         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
22882974e916SYuri Benditovich             chain->stat.over_size++;
22892974e916SYuri Benditovich             return RSC_FINAL;
22902974e916SYuri Benditovich         }
22912974e916SYuri Benditovich 
22922974e916SYuri Benditovich         /* Here comes the right data, the payload length in v4/v6 is different,
22932974e916SYuri Benditovich            so use the field value to update and record the new data len */
22942974e916SYuri Benditovich         o_unit->payload += n_unit->payload; /* update new data len */
22952974e916SYuri Benditovich 
22962974e916SYuri Benditovich         /* update field in ip header */
22975814c084SPeter Maydell         write_unit_ip_len(o_unit, o_ip_len + n_unit->payload);
22982974e916SYuri Benditovich 
22992974e916SYuri Benditovich         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
23002974e916SYuri Benditovich            for windows guest, while this may change the behavior for linux
23012974e916SYuri Benditovich            guest (only if it uses RSC feature). */
23022974e916SYuri Benditovich         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
23032974e916SYuri Benditovich 
23042974e916SYuri Benditovich         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
23052974e916SYuri Benditovich         o_unit->tcp->th_win = n_unit->tcp->th_win;
23062974e916SYuri Benditovich 
23072974e916SYuri Benditovich         memmove(seg->buf + seg->size, data, n_unit->payload);
23082974e916SYuri Benditovich         seg->size += n_unit->payload;
23092974e916SYuri Benditovich         seg->packets++;
23102974e916SYuri Benditovich         chain->stat.coalesced++;
23112974e916SYuri Benditovich         return RSC_COALESCE;
23122974e916SYuri Benditovich     }
23132974e916SYuri Benditovich }
23142974e916SYuri Benditovich 
virtio_net_rsc_coalesce4(VirtioNetRscChain * chain,VirtioNetRscSeg * seg,const uint8_t * buf,size_t size,VirtioNetRscUnit * unit)23152974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
23162974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
23172974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
23182974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
23192974e916SYuri Benditovich {
23202974e916SYuri Benditovich     struct ip_header *ip1, *ip2;
23212974e916SYuri Benditovich 
23222974e916SYuri Benditovich     ip1 = (struct ip_header *)(unit->ip);
23232974e916SYuri Benditovich     ip2 = (struct ip_header *)(seg->unit.ip);
23242974e916SYuri Benditovich     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
23252974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
23262974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
23272974e916SYuri Benditovich         chain->stat.no_match++;
23282974e916SYuri Benditovich         return RSC_NO_MATCH;
23292974e916SYuri Benditovich     }
23302974e916SYuri Benditovich 
23312974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
23322974e916SYuri Benditovich }
23332974e916SYuri Benditovich 
virtio_net_rsc_coalesce6(VirtioNetRscChain * chain,VirtioNetRscSeg * seg,const uint8_t * buf,size_t size,VirtioNetRscUnit * unit)23342974e916SYuri Benditovich static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
23352974e916SYuri Benditovich                                         VirtioNetRscSeg *seg,
23362974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
23372974e916SYuri Benditovich                                         VirtioNetRscUnit *unit)
23382974e916SYuri Benditovich {
23392974e916SYuri Benditovich     struct ip6_header *ip1, *ip2;
23402974e916SYuri Benditovich 
23412974e916SYuri Benditovich     ip1 = (struct ip6_header *)(unit->ip);
23422974e916SYuri Benditovich     ip2 = (struct ip6_header *)(seg->unit.ip);
23432974e916SYuri Benditovich     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
23442974e916SYuri Benditovich         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
23452974e916SYuri Benditovich         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
23462974e916SYuri Benditovich         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
23472974e916SYuri Benditovich             chain->stat.no_match++;
23482974e916SYuri Benditovich             return RSC_NO_MATCH;
23492974e916SYuri Benditovich     }
23502974e916SYuri Benditovich 
23512974e916SYuri Benditovich     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
23522974e916SYuri Benditovich }
23532974e916SYuri Benditovich 
23542974e916SYuri Benditovich /* Packets with 'SYN' should bypass, other flag should be sent after drain
23552974e916SYuri Benditovich  * to prevent out of order */
virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain * chain,struct tcp_header * tcp)23562974e916SYuri Benditovich static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
23572974e916SYuri Benditovich                                          struct tcp_header *tcp)
23582974e916SYuri Benditovich {
23592974e916SYuri Benditovich     uint16_t tcp_hdr;
23602974e916SYuri Benditovich     uint16_t tcp_flag;
23612974e916SYuri Benditovich 
23622974e916SYuri Benditovich     tcp_flag = htons(tcp->th_offset_flags);
23632974e916SYuri Benditovich     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
23642974e916SYuri Benditovich     tcp_flag &= VIRTIO_NET_TCP_FLAG;
23652974e916SYuri Benditovich     if (tcp_flag & TH_SYN) {
23662974e916SYuri Benditovich         chain->stat.tcp_syn++;
23672974e916SYuri Benditovich         return RSC_BYPASS;
23682974e916SYuri Benditovich     }
23692974e916SYuri Benditovich 
23702974e916SYuri Benditovich     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
23712974e916SYuri Benditovich         chain->stat.tcp_ctrl_drain++;
23722974e916SYuri Benditovich         return RSC_FINAL;
23732974e916SYuri Benditovich     }
23742974e916SYuri Benditovich 
23752974e916SYuri Benditovich     if (tcp_hdr > sizeof(struct tcp_header)) {
23762974e916SYuri Benditovich         chain->stat.tcp_all_opt++;
23772974e916SYuri Benditovich         return RSC_FINAL;
23782974e916SYuri Benditovich     }
23792974e916SYuri Benditovich 
23802974e916SYuri Benditovich     return RSC_CANDIDATE;
23812974e916SYuri Benditovich }
23822974e916SYuri Benditovich 
virtio_net_rsc_do_coalesce(VirtioNetRscChain * chain,NetClientState * nc,const uint8_t * buf,size_t size,VirtioNetRscUnit * unit)23832974e916SYuri Benditovich static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
23842974e916SYuri Benditovich                                          NetClientState *nc,
23852974e916SYuri Benditovich                                          const uint8_t *buf, size_t size,
23862974e916SYuri Benditovich                                          VirtioNetRscUnit *unit)
23872974e916SYuri Benditovich {
23882974e916SYuri Benditovich     int ret;
23892974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
23902974e916SYuri Benditovich 
23912974e916SYuri Benditovich     if (QTAILQ_EMPTY(&chain->buffers)) {
23922974e916SYuri Benditovich         chain->stat.empty_cache++;
23932974e916SYuri Benditovich         virtio_net_rsc_cache_buf(chain, nc, buf, size);
23942974e916SYuri Benditovich         timer_mod(chain->drain_timer,
239544bc14faSNicholas Piggin               qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + chain->n->rsc_timeout);
23962974e916SYuri Benditovich         return size;
23972974e916SYuri Benditovich     }
23982974e916SYuri Benditovich 
23992974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
24002974e916SYuri Benditovich         if (chain->proto == ETH_P_IP) {
24012974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
24022974e916SYuri Benditovich         } else {
24032974e916SYuri Benditovich             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
24042974e916SYuri Benditovich         }
24052974e916SYuri Benditovich 
24062974e916SYuri Benditovich         if (ret == RSC_FINAL) {
24072974e916SYuri Benditovich             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
24082974e916SYuri Benditovich                 /* Send failed */
24092974e916SYuri Benditovich                 chain->stat.final_failed++;
24102974e916SYuri Benditovich                 return 0;
24112974e916SYuri Benditovich             }
24122974e916SYuri Benditovich 
24132974e916SYuri Benditovich             /* Send current packet */
24142974e916SYuri Benditovich             return virtio_net_do_receive(nc, buf, size);
24152974e916SYuri Benditovich         } else if (ret == RSC_NO_MATCH) {
24162974e916SYuri Benditovich             continue;
24172974e916SYuri Benditovich         } else {
24182974e916SYuri Benditovich             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
24192974e916SYuri Benditovich             seg->is_coalesced = 1;
24202974e916SYuri Benditovich             return size;
24212974e916SYuri Benditovich         }
24222974e916SYuri Benditovich     }
24232974e916SYuri Benditovich 
24242974e916SYuri Benditovich     chain->stat.no_match_cache++;
24252974e916SYuri Benditovich     virtio_net_rsc_cache_buf(chain, nc, buf, size);
24262974e916SYuri Benditovich     return size;
24272974e916SYuri Benditovich }
24282974e916SYuri Benditovich 
24292974e916SYuri Benditovich /* Drain a connection data, this is to avoid out of order segments */
virtio_net_rsc_drain_flow(VirtioNetRscChain * chain,NetClientState * nc,const uint8_t * buf,size_t size,uint16_t ip_start,uint16_t ip_size,uint16_t tcp_port)24302974e916SYuri Benditovich static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
24312974e916SYuri Benditovich                                         NetClientState *nc,
24322974e916SYuri Benditovich                                         const uint8_t *buf, size_t size,
24332974e916SYuri Benditovich                                         uint16_t ip_start, uint16_t ip_size,
24342974e916SYuri Benditovich                                         uint16_t tcp_port)
24352974e916SYuri Benditovich {
24362974e916SYuri Benditovich     VirtioNetRscSeg *seg, *nseg;
24372974e916SYuri Benditovich     uint32_t ppair1, ppair2;
24382974e916SYuri Benditovich 
24392974e916SYuri Benditovich     ppair1 = *(uint32_t *)(buf + tcp_port);
24402974e916SYuri Benditovich     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
24412974e916SYuri Benditovich         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
24422974e916SYuri Benditovich         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
24432974e916SYuri Benditovich             || (ppair1 != ppair2)) {
24442974e916SYuri Benditovich             continue;
24452974e916SYuri Benditovich         }
24462974e916SYuri Benditovich         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
24472974e916SYuri Benditovich             chain->stat.drain_failed++;
24482974e916SYuri Benditovich         }
24492974e916SYuri Benditovich 
24502974e916SYuri Benditovich         break;
24512974e916SYuri Benditovich     }
24522974e916SYuri Benditovich 
24532974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
24542974e916SYuri Benditovich }
24552974e916SYuri Benditovich 
virtio_net_rsc_sanity_check4(VirtioNetRscChain * chain,struct ip_header * ip,const uint8_t * buf,size_t size)24562974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
24572974e916SYuri Benditovich                                             struct ip_header *ip,
24582974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
24592974e916SYuri Benditovich {
24602974e916SYuri Benditovich     uint16_t ip_len;
24612974e916SYuri Benditovich 
24622974e916SYuri Benditovich     /* Not an ipv4 packet */
24632974e916SYuri Benditovich     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
24642974e916SYuri Benditovich         chain->stat.ip_option++;
24652974e916SYuri Benditovich         return RSC_BYPASS;
24662974e916SYuri Benditovich     }
24672974e916SYuri Benditovich 
24682974e916SYuri Benditovich     /* Don't handle packets with ip option */
24692974e916SYuri Benditovich     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
24702974e916SYuri Benditovich         chain->stat.ip_option++;
24712974e916SYuri Benditovich         return RSC_BYPASS;
24722974e916SYuri Benditovich     }
24732974e916SYuri Benditovich 
24742974e916SYuri Benditovich     if (ip->ip_p != IPPROTO_TCP) {
24752974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
24762974e916SYuri Benditovich         return RSC_BYPASS;
24772974e916SYuri Benditovich     }
24782974e916SYuri Benditovich 
24792974e916SYuri Benditovich     /* Don't handle packets with ip fragment */
24802974e916SYuri Benditovich     if (!(htons(ip->ip_off) & IP_DF)) {
24812974e916SYuri Benditovich         chain->stat.ip_frag++;
24822974e916SYuri Benditovich         return RSC_BYPASS;
24832974e916SYuri Benditovich     }
24842974e916SYuri Benditovich 
24852974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
24862974e916SYuri Benditovich     if (IPTOS_ECN(ip->ip_tos)) {
24872974e916SYuri Benditovich         chain->stat.ip_ecn++;
24882974e916SYuri Benditovich         return RSC_BYPASS;
24892974e916SYuri Benditovich     }
24902974e916SYuri Benditovich 
24912974e916SYuri Benditovich     ip_len = htons(ip->ip_len);
24922974e916SYuri Benditovich     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
24932974e916SYuri Benditovich         || ip_len > (size - chain->n->guest_hdr_len -
24942974e916SYuri Benditovich                      sizeof(struct eth_header))) {
24952974e916SYuri Benditovich         chain->stat.ip_hacked++;
24962974e916SYuri Benditovich         return RSC_BYPASS;
24972974e916SYuri Benditovich     }
24982974e916SYuri Benditovich 
24992974e916SYuri Benditovich     return RSC_CANDIDATE;
25002974e916SYuri Benditovich }
25012974e916SYuri Benditovich 
virtio_net_rsc_receive4(VirtioNetRscChain * chain,NetClientState * nc,const uint8_t * buf,size_t size)25022974e916SYuri Benditovich static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
25032974e916SYuri Benditovich                                       NetClientState *nc,
25042974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
25052974e916SYuri Benditovich {
25062974e916SYuri Benditovich     int32_t ret;
25072974e916SYuri Benditovich     uint16_t hdr_len;
25082974e916SYuri Benditovich     VirtioNetRscUnit unit;
25092974e916SYuri Benditovich 
25102974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
25112974e916SYuri Benditovich 
25122974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
25132974e916SYuri Benditovich         + sizeof(struct tcp_header))) {
25142974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
25152974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25162974e916SYuri Benditovich     }
25172974e916SYuri Benditovich 
25182974e916SYuri Benditovich     virtio_net_rsc_extract_unit4(chain, buf, &unit);
25192974e916SYuri Benditovich     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
25202974e916SYuri Benditovich         != RSC_CANDIDATE) {
25212974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25222974e916SYuri Benditovich     }
25232974e916SYuri Benditovich 
25242974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
25252974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
25262974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25272974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
25282974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
25292974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 12),
25302974e916SYuri Benditovich                 VIRTIO_NET_IP4_ADDR_SIZE,
25312974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
25322974e916SYuri Benditovich     }
25332974e916SYuri Benditovich 
25342974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
25352974e916SYuri Benditovich }
25362974e916SYuri Benditovich 
virtio_net_rsc_sanity_check6(VirtioNetRscChain * chain,struct ip6_header * ip6,const uint8_t * buf,size_t size)25372974e916SYuri Benditovich static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
25382974e916SYuri Benditovich                                             struct ip6_header *ip6,
25392974e916SYuri Benditovich                                             const uint8_t *buf, size_t size)
25402974e916SYuri Benditovich {
25412974e916SYuri Benditovich     uint16_t ip_len;
25422974e916SYuri Benditovich 
25432974e916SYuri Benditovich     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
25442974e916SYuri Benditovich         != IP_HEADER_VERSION_6) {
25452974e916SYuri Benditovich         return RSC_BYPASS;
25462974e916SYuri Benditovich     }
25472974e916SYuri Benditovich 
25482974e916SYuri Benditovich     /* Both option and protocol is checked in this */
25492974e916SYuri Benditovich     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
25502974e916SYuri Benditovich         chain->stat.bypass_not_tcp++;
25512974e916SYuri Benditovich         return RSC_BYPASS;
25522974e916SYuri Benditovich     }
25532974e916SYuri Benditovich 
25542974e916SYuri Benditovich     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
25552974e916SYuri Benditovich     if (ip_len < sizeof(struct tcp_header) ||
25562974e916SYuri Benditovich         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
25572974e916SYuri Benditovich                   - sizeof(struct ip6_header))) {
25582974e916SYuri Benditovich         chain->stat.ip_hacked++;
25592974e916SYuri Benditovich         return RSC_BYPASS;
25602974e916SYuri Benditovich     }
25612974e916SYuri Benditovich 
25622974e916SYuri Benditovich     /* Don't handle packets with ecn flag */
25632974e916SYuri Benditovich     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
25642974e916SYuri Benditovich         chain->stat.ip_ecn++;
25652974e916SYuri Benditovich         return RSC_BYPASS;
25662974e916SYuri Benditovich     }
25672974e916SYuri Benditovich 
25682974e916SYuri Benditovich     return RSC_CANDIDATE;
25692974e916SYuri Benditovich }
25702974e916SYuri Benditovich 
virtio_net_rsc_receive6(void * opq,NetClientState * nc,const uint8_t * buf,size_t size)25712974e916SYuri Benditovich static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
25722974e916SYuri Benditovich                                       const uint8_t *buf, size_t size)
25732974e916SYuri Benditovich {
25742974e916SYuri Benditovich     int32_t ret;
25752974e916SYuri Benditovich     uint16_t hdr_len;
25762974e916SYuri Benditovich     VirtioNetRscChain *chain;
25772974e916SYuri Benditovich     VirtioNetRscUnit unit;
25782974e916SYuri Benditovich 
25793d558330SMarkus Armbruster     chain = opq;
25802974e916SYuri Benditovich     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
25812974e916SYuri Benditovich 
25822974e916SYuri Benditovich     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
25832974e916SYuri Benditovich         + sizeof(tcp_header))) {
25842974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25852974e916SYuri Benditovich     }
25862974e916SYuri Benditovich 
25872974e916SYuri Benditovich     virtio_net_rsc_extract_unit6(chain, buf, &unit);
25882974e916SYuri Benditovich     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
25892974e916SYuri Benditovich                                                  unit.ip, buf, size)) {
25902974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25912974e916SYuri Benditovich     }
25922974e916SYuri Benditovich 
25932974e916SYuri Benditovich     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
25942974e916SYuri Benditovich     if (ret == RSC_BYPASS) {
25952974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
25962974e916SYuri Benditovich     } else if (ret == RSC_FINAL) {
25972974e916SYuri Benditovich         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
25982974e916SYuri Benditovich                 ((hdr_len + sizeof(struct eth_header)) + 8),
25992974e916SYuri Benditovich                 VIRTIO_NET_IP6_ADDR_SIZE,
26002974e916SYuri Benditovich                 hdr_len + sizeof(struct eth_header)
26012974e916SYuri Benditovich                 + sizeof(struct ip6_header));
26022974e916SYuri Benditovich     }
26032974e916SYuri Benditovich 
26042974e916SYuri Benditovich     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
26052974e916SYuri Benditovich }
26062974e916SYuri Benditovich 
virtio_net_rsc_lookup_chain(VirtIONet * n,NetClientState * nc,uint16_t proto)26072974e916SYuri Benditovich static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
26082974e916SYuri Benditovich                                                       NetClientState *nc,
26092974e916SYuri Benditovich                                                       uint16_t proto)
26102974e916SYuri Benditovich {
26112974e916SYuri Benditovich     VirtioNetRscChain *chain;
26122974e916SYuri Benditovich 
26132974e916SYuri Benditovich     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
26142974e916SYuri Benditovich         return NULL;
26152974e916SYuri Benditovich     }
26162974e916SYuri Benditovich 
26172974e916SYuri Benditovich     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
26182974e916SYuri Benditovich         if (chain->proto == proto) {
26192974e916SYuri Benditovich             return chain;
26202974e916SYuri Benditovich         }
26212974e916SYuri Benditovich     }
26222974e916SYuri Benditovich 
26232974e916SYuri Benditovich     chain = g_malloc(sizeof(*chain));
26242974e916SYuri Benditovich     chain->n = n;
26252974e916SYuri Benditovich     chain->proto = proto;
26262974e916SYuri Benditovich     if (proto == (uint16_t)ETH_P_IP) {
26272974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
26282974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
26292974e916SYuri Benditovich     } else {
26302974e916SYuri Benditovich         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
26312974e916SYuri Benditovich         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
26322974e916SYuri Benditovich     }
263344bc14faSNicholas Piggin     chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
26342974e916SYuri Benditovich                                       virtio_net_rsc_purge, chain);
26352974e916SYuri Benditovich     memset(&chain->stat, 0, sizeof(chain->stat));
26362974e916SYuri Benditovich 
26372974e916SYuri Benditovich     QTAILQ_INIT(&chain->buffers);
26382974e916SYuri Benditovich     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
26392974e916SYuri Benditovich 
26402974e916SYuri Benditovich     return chain;
26412974e916SYuri Benditovich }
26422974e916SYuri Benditovich 
virtio_net_rsc_receive(NetClientState * nc,const uint8_t * buf,size_t size)26432974e916SYuri Benditovich static ssize_t virtio_net_rsc_receive(NetClientState *nc,
26442974e916SYuri Benditovich                                       const uint8_t *buf,
26452974e916SYuri Benditovich                                       size_t size)
26462974e916SYuri Benditovich {
26472974e916SYuri Benditovich     uint16_t proto;
26482974e916SYuri Benditovich     VirtioNetRscChain *chain;
26492974e916SYuri Benditovich     struct eth_header *eth;
26502974e916SYuri Benditovich     VirtIONet *n;
26512974e916SYuri Benditovich 
26522974e916SYuri Benditovich     n = qemu_get_nic_opaque(nc);
26532974e916SYuri Benditovich     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
26542974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
26552974e916SYuri Benditovich     }
26562974e916SYuri Benditovich 
26572974e916SYuri Benditovich     eth = (struct eth_header *)(buf + n->guest_hdr_len);
26582974e916SYuri Benditovich     proto = htons(eth->h_proto);
26592974e916SYuri Benditovich 
26602974e916SYuri Benditovich     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
26612974e916SYuri Benditovich     if (chain) {
26622974e916SYuri Benditovich         chain->stat.received++;
26632974e916SYuri Benditovich         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
26642974e916SYuri Benditovich             return virtio_net_rsc_receive4(chain, nc, buf, size);
26652974e916SYuri Benditovich         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
26662974e916SYuri Benditovich             return virtio_net_rsc_receive6(chain, nc, buf, size);
26672974e916SYuri Benditovich         }
26682974e916SYuri Benditovich     }
26692974e916SYuri Benditovich     return virtio_net_do_receive(nc, buf, size);
26702974e916SYuri Benditovich }
26712974e916SYuri Benditovich 
virtio_net_receive(NetClientState * nc,const uint8_t * buf,size_t size)26722974e916SYuri Benditovich static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
26732974e916SYuri Benditovich                                   size_t size)
26742974e916SYuri Benditovich {
26752974e916SYuri Benditovich     VirtIONet *n = qemu_get_nic_opaque(nc);
26762974e916SYuri Benditovich     if ((n->rsc4_enabled || n->rsc6_enabled)) {
26772974e916SYuri Benditovich         return virtio_net_rsc_receive(nc, buf, size);
26782974e916SYuri Benditovich     } else {
26792974e916SYuri Benditovich         return virtio_net_do_receive(nc, buf, size);
26802974e916SYuri Benditovich     }
26812974e916SYuri Benditovich }
26822974e916SYuri Benditovich 
26836e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
26846e790746SPaolo Bonzini 
virtio_net_tx_complete(NetClientState * nc,ssize_t len)26856e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
26866e790746SPaolo Bonzini {
26876e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
26886e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
268917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2690df8d0708SLaurent Vivier     int ret;
26916e790746SPaolo Bonzini 
269251b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
269317a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
26946e790746SPaolo Bonzini 
269551b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
269651b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
26976e790746SPaolo Bonzini 
26986e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2699df8d0708SLaurent Vivier     ret = virtio_net_flush_tx(q);
27007550a822SLaurent Vivier     if (ret >= n->tx_burst) {
2701df8d0708SLaurent Vivier         /*
2702df8d0708SLaurent Vivier          * the flush has been stopped by tx_burst
2703df8d0708SLaurent Vivier          * we will not receive notification for the
2704df8d0708SLaurent Vivier          * remainining part, so re-schedule
2705df8d0708SLaurent Vivier          */
2706df8d0708SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
27077550a822SLaurent Vivier         if (q->tx_bh) {
2708a0bf401bSNicholas Piggin             replay_bh_schedule_event(q->tx_bh);
27097550a822SLaurent Vivier         } else {
27107550a822SLaurent Vivier             timer_mod(q->tx_timer,
27117550a822SLaurent Vivier                       qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
27127550a822SLaurent Vivier         }
2713df8d0708SLaurent Vivier         q->tx_waiting = 1;
2714df8d0708SLaurent Vivier     }
27156e790746SPaolo Bonzini }
27166e790746SPaolo Bonzini 
27176e790746SPaolo Bonzini /* TX */
virtio_net_flush_tx(VirtIONetQueue * q)27186e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
27196e790746SPaolo Bonzini {
27206e790746SPaolo Bonzini     VirtIONet *n = q->n;
272117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
272251b19ebeSPaolo Bonzini     VirtQueueElement *elem;
27236e790746SPaolo Bonzini     int32_t num_packets = 0;
27246e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
272517a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
27266e790746SPaolo Bonzini         return num_packets;
27276e790746SPaolo Bonzini     }
27286e790746SPaolo Bonzini 
272951b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
27306e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
27316e790746SPaolo Bonzini         return num_packets;
27326e790746SPaolo Bonzini     }
27336e790746SPaolo Bonzini 
273451b19ebeSPaolo Bonzini     for (;;) {
2735bd89dd98SJason Wang         ssize_t ret;
273651b19ebeSPaolo Bonzini         unsigned int out_num;
273751b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2738942f420eSAkihiko Odaki         struct virtio_net_hdr vhdr;
27396e790746SPaolo Bonzini 
274051b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
274151b19ebeSPaolo Bonzini         if (!elem) {
274251b19ebeSPaolo Bonzini             break;
274351b19ebeSPaolo Bonzini         }
274451b19ebeSPaolo Bonzini 
274551b19ebeSPaolo Bonzini         out_num = elem->out_num;
274651b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
27476e790746SPaolo Bonzini         if (out_num < 1) {
2748fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
27492c3e4e2dSAlexey Dobriyan             goto detach;
27506e790746SPaolo Bonzini         }
27516e790746SPaolo Bonzini 
2752ad57f700SAkihiko Odaki         if (n->needs_vnet_hdr_swap) {
2753942f420eSAkihiko Odaki             if (iov_to_buf(out_sg, out_num, 0, &vhdr, sizeof(vhdr)) <
2754942f420eSAkihiko Odaki                 sizeof(vhdr)) {
2755fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
27562c3e4e2dSAlexey Dobriyan                 goto detach;
2757032a74a1SCédric Le Goater             }
2758942f420eSAkihiko Odaki             virtio_net_hdr_swap(vdev, &vhdr);
27592220e818SJason Wang             sg2[0].iov_base = &vhdr;
2760942f420eSAkihiko Odaki             sg2[0].iov_len = sizeof(vhdr);
2761ad57f700SAkihiko Odaki             out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, out_sg, out_num,
2762942f420eSAkihiko Odaki                                sizeof(vhdr), -1);
2763feb93f36SJason Wang             if (out_num == VIRTQUEUE_MAX_SIZE) {
2764feb93f36SJason Wang                 goto drop;
2765032a74a1SCédric Le Goater             }
2766feb93f36SJason Wang             out_num += 1;
2767feb93f36SJason Wang             out_sg = sg2;
2768feb93f36SJason Wang         }
27696e790746SPaolo Bonzini         /*
27706e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
27716e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
27726e790746SPaolo Bonzini          * that host is interested in.
27736e790746SPaolo Bonzini          */
27746e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
27756e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
2776d4f471ebSDmitry Frolov             if (iov_size(out_sg, out_num) < n->guest_hdr_len) {
2777d4f471ebSDmitry Frolov                 virtio_error(vdev, "virtio-net header is invalid");
2778d4f471ebSDmitry Frolov                 goto detach;
2779d4f471ebSDmitry Frolov             }
27806e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
27816e790746SPaolo Bonzini                                        out_sg, out_num,
27826e790746SPaolo Bonzini                                        0, n->host_hdr_len);
27836e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
27846e790746SPaolo Bonzini                              out_sg, out_num,
27856e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
27866e790746SPaolo Bonzini             out_num = sg_num;
27876e790746SPaolo Bonzini             out_sg = sg;
27882c3e4e2dSAlexey Dobriyan 
27892c3e4e2dSAlexey Dobriyan             if (out_num < 1) {
27902c3e4e2dSAlexey Dobriyan                 virtio_error(vdev, "virtio-net nothing to send");
27912c3e4e2dSAlexey Dobriyan                 goto detach;
27922c3e4e2dSAlexey Dobriyan             }
27936e790746SPaolo Bonzini         }
27946e790746SPaolo Bonzini 
27956e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
27966e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
27976e790746SPaolo Bonzini         if (ret == 0) {
27986e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
27996e790746SPaolo Bonzini             q->async_tx.elem = elem;
28006e790746SPaolo Bonzini             return -EBUSY;
28016e790746SPaolo Bonzini         }
28026e790746SPaolo Bonzini 
2803feb93f36SJason Wang drop:
280451b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
280517a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
280651b19ebeSPaolo Bonzini         g_free(elem);
28076e790746SPaolo Bonzini 
28086e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
28096e790746SPaolo Bonzini             break;
28106e790746SPaolo Bonzini         }
28116e790746SPaolo Bonzini     }
28126e790746SPaolo Bonzini     return num_packets;
28132c3e4e2dSAlexey Dobriyan 
28142c3e4e2dSAlexey Dobriyan detach:
28152c3e4e2dSAlexey Dobriyan     virtqueue_detach_element(q->tx_vq, elem, 0);
28162c3e4e2dSAlexey Dobriyan     g_free(elem);
28172c3e4e2dSAlexey Dobriyan     return -EINVAL;
28186e790746SPaolo Bonzini }
28196e790746SPaolo Bonzini 
28207550a822SLaurent Vivier static void virtio_net_tx_timer(void *opaque);
28217550a822SLaurent Vivier 
virtio_net_handle_tx_timer(VirtIODevice * vdev,VirtQueue * vq)28226e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
28236e790746SPaolo Bonzini {
282417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
28256e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
28266e790746SPaolo Bonzini 
2827283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2828283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2829283e2c2aSYuri Benditovich         return;
2830283e2c2aSYuri Benditovich     }
2831283e2c2aSYuri Benditovich 
28326e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
283317a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
28346e790746SPaolo Bonzini         q->tx_waiting = 1;
28356e790746SPaolo Bonzini         return;
28366e790746SPaolo Bonzini     }
28376e790746SPaolo Bonzini 
28386e790746SPaolo Bonzini     if (q->tx_waiting) {
28397550a822SLaurent Vivier         /* We already have queued packets, immediately flush */
2840bc72ad67SAlex Bligh         timer_del(q->tx_timer);
28417550a822SLaurent Vivier         virtio_net_tx_timer(q);
28426e790746SPaolo Bonzini     } else {
28437550a822SLaurent Vivier         /* re-arm timer to flush it (and more) on next tick */
2844bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
2845bc72ad67SAlex Bligh                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
28466e790746SPaolo Bonzini         q->tx_waiting = 1;
28476e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
28486e790746SPaolo Bonzini     }
28496e790746SPaolo Bonzini }
28506e790746SPaolo Bonzini 
virtio_net_handle_tx_bh(VirtIODevice * vdev,VirtQueue * vq)28516e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
28526e790746SPaolo Bonzini {
285317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
28546e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
28556e790746SPaolo Bonzini 
28564c54f5bcSYajun Wu     if (unlikely(n->vhost_started)) {
28574c54f5bcSYajun Wu         return;
28584c54f5bcSYajun Wu     }
28594c54f5bcSYajun Wu 
2860283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2861283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
2862283e2c2aSYuri Benditovich         return;
2863283e2c2aSYuri Benditovich     }
2864283e2c2aSYuri Benditovich 
28656e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
28666e790746SPaolo Bonzini         return;
28676e790746SPaolo Bonzini     }
28686e790746SPaolo Bonzini     q->tx_waiting = 1;
28696e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
287017a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
28716e790746SPaolo Bonzini         return;
28726e790746SPaolo Bonzini     }
28736e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
2874a0bf401bSNicholas Piggin     replay_bh_schedule_event(q->tx_bh);
28756e790746SPaolo Bonzini }
28766e790746SPaolo Bonzini 
virtio_net_tx_timer(void * opaque)28776e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
28786e790746SPaolo Bonzini {
28796e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
28806e790746SPaolo Bonzini     VirtIONet *n = q->n;
288117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
28827550a822SLaurent Vivier     int ret;
28837550a822SLaurent Vivier 
2884e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2885e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2886e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2887e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2888e8bcf842SMichael S. Tsirkin         return;
2889e8bcf842SMichael S. Tsirkin     }
28906e790746SPaolo Bonzini 
28916e790746SPaolo Bonzini     q->tx_waiting = 0;
28926e790746SPaolo Bonzini 
28936e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
289417a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
28956e790746SPaolo Bonzini         return;
289617a0ca55SKONRAD Frederic     }
28976e790746SPaolo Bonzini 
28987550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
28997550a822SLaurent Vivier     if (ret == -EBUSY || ret == -EINVAL) {
29007550a822SLaurent Vivier         return;
29017550a822SLaurent Vivier     }
29027550a822SLaurent Vivier     /*
29037550a822SLaurent Vivier      * If we flush a full burst of packets, assume there are
29047550a822SLaurent Vivier      * more coming and immediately rearm
29057550a822SLaurent Vivier      */
29067550a822SLaurent Vivier     if (ret >= n->tx_burst) {
29077550a822SLaurent Vivier         q->tx_waiting = 1;
29087550a822SLaurent Vivier         timer_mod(q->tx_timer,
29097550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
29107550a822SLaurent Vivier         return;
29117550a822SLaurent Vivier     }
29127550a822SLaurent Vivier     /*
29137550a822SLaurent Vivier      * If less than a full burst, re-enable notification and flush
29147550a822SLaurent Vivier      * anything that may have come in while we weren't looking.  If
29157550a822SLaurent Vivier      * we find something, assume the guest is still active and rearm
29167550a822SLaurent Vivier      */
29176e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
29187550a822SLaurent Vivier     ret = virtio_net_flush_tx(q);
29197550a822SLaurent Vivier     if (ret > 0) {
29207550a822SLaurent Vivier         virtio_queue_set_notification(q->tx_vq, 0);
29217550a822SLaurent Vivier         q->tx_waiting = 1;
29227550a822SLaurent Vivier         timer_mod(q->tx_timer,
29237550a822SLaurent Vivier                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
29247550a822SLaurent Vivier     }
29256e790746SPaolo Bonzini }
29266e790746SPaolo Bonzini 
virtio_net_tx_bh(void * opaque)29276e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
29286e790746SPaolo Bonzini {
29296e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
29306e790746SPaolo Bonzini     VirtIONet *n = q->n;
293117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
29326e790746SPaolo Bonzini     int32_t ret;
29336e790746SPaolo Bonzini 
2934e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
2935e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
2936e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
2937e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
2938e8bcf842SMichael S. Tsirkin         return;
2939e8bcf842SMichael S. Tsirkin     }
29406e790746SPaolo Bonzini 
29416e790746SPaolo Bonzini     q->tx_waiting = 0;
29426e790746SPaolo Bonzini 
29436e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
294417a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
29456e790746SPaolo Bonzini         return;
294617a0ca55SKONRAD Frederic     }
29476e790746SPaolo Bonzini 
29486e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
2949fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
2950fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
2951fa5e56c2SGreg Kurz                  * broken */
29526e790746SPaolo Bonzini     }
29536e790746SPaolo Bonzini 
29546e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
29556e790746SPaolo Bonzini      * more coming and immediately reschedule */
29566e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
2957a0bf401bSNicholas Piggin         replay_bh_schedule_event(q->tx_bh);
29586e790746SPaolo Bonzini         q->tx_waiting = 1;
29596e790746SPaolo Bonzini         return;
29606e790746SPaolo Bonzini     }
29616e790746SPaolo Bonzini 
29626e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
29636e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
29646e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
29656e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
2966fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
2967fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
2968fa5e56c2SGreg Kurz         return;
2969fa5e56c2SGreg Kurz     } else if (ret > 0) {
29706e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
2971a0bf401bSNicholas Piggin         replay_bh_schedule_event(q->tx_bh);
29726e790746SPaolo Bonzini         q->tx_waiting = 1;
29736e790746SPaolo Bonzini     }
29746e790746SPaolo Bonzini }
29756e790746SPaolo Bonzini 
virtio_net_add_queue(VirtIONet * n,int index)2976f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
2977f9d6dbf0SWen Congyang {
2978f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2979f9d6dbf0SWen Congyang 
29801c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
29811c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
29829b02e161SWei Wang 
2983f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2984f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29859b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29869b02e161SWei Wang                              virtio_net_handle_tx_timer);
2987f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2988f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
2989f9d6dbf0SWen Congyang                                               &n->vqs[index]);
2990f9d6dbf0SWen Congyang     } else {
2991f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
29929b02e161SWei Wang             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
29939b02e161SWei Wang                              virtio_net_handle_tx_bh);
2994f63192b0SAlexander Bulekov         n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
2995f63192b0SAlexander Bulekov                                                   &DEVICE(vdev)->mem_reentrancy_guard);
2996f9d6dbf0SWen Congyang     }
2997f9d6dbf0SWen Congyang 
2998f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
2999f9d6dbf0SWen Congyang     n->vqs[index].n = n;
3000f9d6dbf0SWen Congyang }
3001f9d6dbf0SWen Congyang 
virtio_net_del_queue(VirtIONet * n,int index)3002f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
3003f9d6dbf0SWen Congyang {
3004f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3005f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
3006f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
3007f9d6dbf0SWen Congyang 
3008f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
3009f9d6dbf0SWen Congyang 
3010f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
3011f9d6dbf0SWen Congyang     if (q->tx_timer) {
3012f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
3013f989c30cSYunjian Wang         q->tx_timer = NULL;
3014f9d6dbf0SWen Congyang     } else {
3015f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
3016f989c30cSYunjian Wang         q->tx_bh = NULL;
3017f9d6dbf0SWen Congyang     }
3018f989c30cSYunjian Wang     q->tx_waiting = 0;
3019f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
3020f9d6dbf0SWen Congyang }
3021f9d6dbf0SWen Congyang 
virtio_net_change_num_queue_pairs(VirtIONet * n,int new_max_queue_pairs)3022441537f1SJason Wang static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs)
3023f9d6dbf0SWen Congyang {
3024f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3025f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
3026441537f1SJason Wang     int new_num_queues = new_max_queue_pairs * 2 + 1;
3027f9d6dbf0SWen Congyang     int i;
3028f9d6dbf0SWen Congyang 
3029f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
3030f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
3031f9d6dbf0SWen Congyang 
3032f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
3033f9d6dbf0SWen Congyang         return;
3034f9d6dbf0SWen Congyang     }
3035f9d6dbf0SWen Congyang 
3036f9d6dbf0SWen Congyang     /*
3037f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
3038f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
303920f86a75SYuval Shaia      * and then we only enter one of the following two loops.
3040f9d6dbf0SWen Congyang      */
3041f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
3042f9d6dbf0SWen Congyang 
3043f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
3044f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
3045f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
3046f9d6dbf0SWen Congyang     }
3047f9d6dbf0SWen Congyang 
3048f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
3049f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
3050f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
3051f9d6dbf0SWen Congyang     }
3052f9d6dbf0SWen Congyang 
3053f9d6dbf0SWen Congyang     /* add ctrl_vq last */
3054f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3055f9d6dbf0SWen Congyang }
3056f9d6dbf0SWen Congyang 
virtio_net_set_multiqueue(VirtIONet * n,int multiqueue)3057ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
30586e790746SPaolo Bonzini {
3059441537f1SJason Wang     int max = multiqueue ? n->max_queue_pairs : 1;
3060f9d6dbf0SWen Congyang 
30616e790746SPaolo Bonzini     n->multiqueue = multiqueue;
3062441537f1SJason Wang     virtio_net_change_num_queue_pairs(n, max);
30636e790746SPaolo Bonzini 
3064441537f1SJason Wang     virtio_net_set_queue_pairs(n);
30656e790746SPaolo Bonzini }
30666e790746SPaolo Bonzini 
virtio_net_pre_load_queues(VirtIODevice * vdev)3067*9379ea9dSAkihiko Odaki static int virtio_net_pre_load_queues(VirtIODevice *vdev)
3068*9379ea9dSAkihiko Odaki {
3069*9379ea9dSAkihiko Odaki     virtio_net_set_multiqueue(VIRTIO_NET(vdev),
3070*9379ea9dSAkihiko Odaki                               virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_RSS) ||
3071*9379ea9dSAkihiko Odaki                               virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MQ));
3072*9379ea9dSAkihiko Odaki 
3073*9379ea9dSAkihiko Odaki     return 0;
3074*9379ea9dSAkihiko Odaki }
3075*9379ea9dSAkihiko Odaki 
virtio_net_post_load_device(void * opaque,int version_id)3076982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
3077037dab2fSGreg Kurz {
3078982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
3079982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3080037dab2fSGreg Kurz     int i, link_down;
3081037dab2fSGreg Kurz 
30829d8c6a25SDr. David Alan Gilbert     trace_virtio_net_post_load_device();
3083982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
308495129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
3085e22f0603SYuri Benditovich                                                        VIRTIO_F_VERSION_1),
3086e22f0603SYuri Benditovich                                virtio_vdev_has_feature(vdev,
3087e22f0603SYuri Benditovich                                                        VIRTIO_NET_F_HASH_REPORT));
30886e790746SPaolo Bonzini 
30896e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
3090982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
30916e790746SPaolo Bonzini         n->mac_table.in_use = 0;
30926e790746SPaolo Bonzini     }
30936e790746SPaolo Bonzini 
3094982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
30956c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
30966c666823SMichael S. Tsirkin     }
30976c666823SMichael S. Tsirkin 
30987788c3f2SMikhail Sennikovsky     /*
30997788c3f2SMikhail Sennikovsky      * curr_guest_offloads will be later overwritten by the
31007788c3f2SMikhail Sennikovsky      * virtio_set_features_nocheck call done from the virtio_load.
31017788c3f2SMikhail Sennikovsky      * Here we make sure it is preserved and restored accordingly
31027788c3f2SMikhail Sennikovsky      * in the virtio_net_post_load_virtio callback.
31037788c3f2SMikhail Sennikovsky      */
31047788c3f2SMikhail Sennikovsky     n->saved_guest_offloads = n->curr_guest_offloads;
31056c666823SMichael S. Tsirkin 
3106441537f1SJason Wang     virtio_net_set_queue_pairs(n);
31076e790746SPaolo Bonzini 
31086e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
31096e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
31106e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
31116e790746SPaolo Bonzini             break;
31126e790746SPaolo Bonzini         }
31136e790746SPaolo Bonzini     }
31146e790746SPaolo Bonzini     n->mac_table.first_multi = i;
31156e790746SPaolo Bonzini 
31166e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
31176e790746SPaolo Bonzini      * to link status bit in n->status */
31186e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
3119441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
31206e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
31216e790746SPaolo Bonzini     }
31226e790746SPaolo Bonzini 
31236c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
31246c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
31259d8c6a25SDr. David Alan Gilbert         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
31269d8c6a25SDr. David Alan Gilbert                                   QEMU_CLOCK_VIRTUAL,
31279d8c6a25SDr. David Alan Gilbert                                   virtio_net_announce_timer, n);
31289d8c6a25SDr. David Alan Gilbert         if (n->announce_timer.round) {
31299d8c6a25SDr. David Alan Gilbert             timer_mod(n->announce_timer.tm,
31309d8c6a25SDr. David Alan Gilbert                       qemu_clock_get_ms(n->announce_timer.type));
31319d8c6a25SDr. David Alan Gilbert         } else {
3132944458b6SDr. David Alan Gilbert             qemu_announce_timer_del(&n->announce_timer, false);
31339d8c6a25SDr. David Alan Gilbert         }
31346c666823SMichael S. Tsirkin     }
31356c666823SMichael S. Tsirkin 
31360e07198eSAkihiko Odaki     virtio_net_commit_rss_config(n);
31376e790746SPaolo Bonzini     return 0;
31386e790746SPaolo Bonzini }
31396e790746SPaolo Bonzini 
virtio_net_post_load_virtio(VirtIODevice * vdev)31407788c3f2SMikhail Sennikovsky static int virtio_net_post_load_virtio(VirtIODevice *vdev)
31417788c3f2SMikhail Sennikovsky {
31427788c3f2SMikhail Sennikovsky     VirtIONet *n = VIRTIO_NET(vdev);
31437788c3f2SMikhail Sennikovsky     /*
31447788c3f2SMikhail Sennikovsky      * The actual needed state is now in saved_guest_offloads,
31457788c3f2SMikhail Sennikovsky      * see virtio_net_post_load_device for detail.
31467788c3f2SMikhail Sennikovsky      * Restore it back and apply the desired offloads.
31477788c3f2SMikhail Sennikovsky      */
31487788c3f2SMikhail Sennikovsky     n->curr_guest_offloads = n->saved_guest_offloads;
31497788c3f2SMikhail Sennikovsky     if (peer_has_vnet_hdr(n)) {
31507788c3f2SMikhail Sennikovsky         virtio_net_apply_guest_offloads(n);
31517788c3f2SMikhail Sennikovsky     }
31527788c3f2SMikhail Sennikovsky 
31537788c3f2SMikhail Sennikovsky     return 0;
31547788c3f2SMikhail Sennikovsky }
31557788c3f2SMikhail Sennikovsky 
3156982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
3157982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
3158982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
31591de81b42SRichard Henderson     .fields = (const VMStateField[]) {
3160982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
3161982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3162982b78c5SDr. David Alan Gilbert    },
3163982b78c5SDr. David Alan Gilbert };
3164982b78c5SDr. David Alan Gilbert 
max_queue_pairs_gt_1(void * opaque,int version_id)3165441537f1SJason Wang static bool max_queue_pairs_gt_1(void *opaque, int version_id)
3166982b78c5SDr. David Alan Gilbert {
3167441537f1SJason Wang     return VIRTIO_NET(opaque)->max_queue_pairs > 1;
3168982b78c5SDr. David Alan Gilbert }
3169982b78c5SDr. David Alan Gilbert 
has_ctrl_guest_offloads(void * opaque,int version_id)3170982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
3171982b78c5SDr. David Alan Gilbert {
3172982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
3173982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
3174982b78c5SDr. David Alan Gilbert }
3175982b78c5SDr. David Alan Gilbert 
mac_table_fits(void * opaque,int version_id)3176982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
3177982b78c5SDr. David Alan Gilbert {
3178982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
3179982b78c5SDr. David Alan Gilbert }
3180982b78c5SDr. David Alan Gilbert 
mac_table_doesnt_fit(void * opaque,int version_id)3181982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
3182982b78c5SDr. David Alan Gilbert {
3183982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
3184982b78c5SDr. David Alan Gilbert }
3185982b78c5SDr. David Alan Gilbert 
3186982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
3187982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
3188982b78c5SDr. David Alan Gilbert  */
3189982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
3190982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
3191982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
3192441537f1SJason Wang     uint16_t        curr_queue_pairs_1;
3193982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
3194982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
3195982b78c5SDr. David Alan Gilbert };
3196982b78c5SDr. David Alan Gilbert 
3197982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
3198441537f1SJason Wang  * the 1st entry in the queue_pairs and only if there's more than one
3199982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
3200982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
3201982b78c5SDr. David Alan Gilbert  */
3202982b78c5SDr. David Alan Gilbert 
virtio_net_tx_waiting_pre_save(void * opaque)320344b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque)
3204982b78c5SDr. David Alan Gilbert {
3205982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3206982b78c5SDr. David Alan Gilbert 
3207982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
3208441537f1SJason Wang     tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1;
3209441537f1SJason Wang     if (tmp->parent->curr_queue_pairs == 0) {
3210441537f1SJason Wang         tmp->curr_queue_pairs_1 = 0;
3211982b78c5SDr. David Alan Gilbert     }
321244b1ff31SDr. David Alan Gilbert 
321344b1ff31SDr. David Alan Gilbert     return 0;
3214982b78c5SDr. David Alan Gilbert }
3215982b78c5SDr. David Alan Gilbert 
virtio_net_tx_waiting_pre_load(void * opaque)3216982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
3217982b78c5SDr. David Alan Gilbert {
3218982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3219982b78c5SDr. David Alan Gilbert 
3220982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
3221982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
3222982b78c5SDr. David Alan Gilbert 
3223441537f1SJason Wang     if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) {
3224441537f1SJason Wang         error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3225441537f1SJason Wang             tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs);
3226982b78c5SDr. David Alan Gilbert 
3227982b78c5SDr. David Alan Gilbert         return -EINVAL;
3228982b78c5SDr. David Alan Gilbert     }
3229982b78c5SDr. David Alan Gilbert 
3230982b78c5SDr. David Alan Gilbert     return 0; /* all good */
3231982b78c5SDr. David Alan Gilbert }
3232982b78c5SDr. David Alan Gilbert 
3233982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
3234982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
3235982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
3236982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
32371de81b42SRichard Henderson     .fields    = (const VMStateField[]) {
3238982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
3239441537f1SJason Wang                                      curr_queue_pairs_1,
3240982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
3241982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
3242982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3243982b78c5SDr. David Alan Gilbert     },
3244982b78c5SDr. David Alan Gilbert };
3245982b78c5SDr. David Alan Gilbert 
3246982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
3247982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3248982b78c5SDr. David Alan Gilbert  */
virtio_net_ufo_post_load(void * opaque,int version_id)3249982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
3250982b78c5SDr. David Alan Gilbert {
3251982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3252982b78c5SDr. David Alan Gilbert 
3253982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
3254982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
3255982b78c5SDr. David Alan Gilbert         return -EINVAL;
3256982b78c5SDr. David Alan Gilbert     }
3257982b78c5SDr. David Alan Gilbert 
3258982b78c5SDr. David Alan Gilbert     return 0;
3259982b78c5SDr. David Alan Gilbert }
3260982b78c5SDr. David Alan Gilbert 
virtio_net_ufo_pre_save(void * opaque)326144b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque)
3262982b78c5SDr. David Alan Gilbert {
3263982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3264982b78c5SDr. David Alan Gilbert 
3265982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
326644b1ff31SDr. David Alan Gilbert 
326744b1ff31SDr. David Alan Gilbert     return 0;
3268982b78c5SDr. David Alan Gilbert }
3269982b78c5SDr. David Alan Gilbert 
3270982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
3271982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
3272982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
3273982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
32741de81b42SRichard Henderson     .fields    = (const VMStateField[]) {
3275982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
3276982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3277982b78c5SDr. David Alan Gilbert     },
3278982b78c5SDr. David Alan Gilbert };
3279982b78c5SDr. David Alan Gilbert 
3280982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3281982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
3282982b78c5SDr. David Alan Gilbert  */
virtio_net_vnet_post_load(void * opaque,int version_id)3283982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
3284982b78c5SDr. David Alan Gilbert {
3285982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3286982b78c5SDr. David Alan Gilbert 
3287982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
3288982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
3289982b78c5SDr. David Alan Gilbert         return -EINVAL;
3290982b78c5SDr. David Alan Gilbert     }
3291982b78c5SDr. David Alan Gilbert 
3292982b78c5SDr. David Alan Gilbert     return 0;
3293982b78c5SDr. David Alan Gilbert }
3294982b78c5SDr. David Alan Gilbert 
virtio_net_vnet_pre_save(void * opaque)329544b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque)
3296982b78c5SDr. David Alan Gilbert {
3297982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
3298982b78c5SDr. David Alan Gilbert 
3299982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
330044b1ff31SDr. David Alan Gilbert 
330144b1ff31SDr. David Alan Gilbert     return 0;
3302982b78c5SDr. David Alan Gilbert }
3303982b78c5SDr. David Alan Gilbert 
3304982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
3305982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
3306982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
3307982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
33081de81b42SRichard Henderson     .fields    = (const VMStateField[]) {
3309982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
3310982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3311982b78c5SDr. David Alan Gilbert     },
3312982b78c5SDr. David Alan Gilbert };
3313982b78c5SDr. David Alan Gilbert 
virtio_net_rss_needed(void * opaque)3314e41b7114SYuri Benditovich static bool virtio_net_rss_needed(void *opaque)
3315e41b7114SYuri Benditovich {
3316e41b7114SYuri Benditovich     return VIRTIO_NET(opaque)->rss_data.enabled;
3317e41b7114SYuri Benditovich }
3318e41b7114SYuri Benditovich 
3319e41b7114SYuri Benditovich static const VMStateDescription vmstate_virtio_net_rss = {
3320e41b7114SYuri Benditovich     .name      = "virtio-net-device/rss",
3321e41b7114SYuri Benditovich     .version_id = 1,
3322e41b7114SYuri Benditovich     .minimum_version_id = 1,
3323e41b7114SYuri Benditovich     .needed = virtio_net_rss_needed,
33241de81b42SRichard Henderson     .fields = (const VMStateField[]) {
3325e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
3326e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
3327e41b7114SYuri Benditovich         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
3328e41b7114SYuri Benditovich         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
3329e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
3330e41b7114SYuri Benditovich         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
3331e41b7114SYuri Benditovich         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
3332e41b7114SYuri Benditovich                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
3333e41b7114SYuri Benditovich         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
3334e41b7114SYuri Benditovich                                     rss_data.indirections_len, 0,
3335e41b7114SYuri Benditovich                                     vmstate_info_uint16, uint16_t),
3336e41b7114SYuri Benditovich         VMSTATE_END_OF_LIST()
3337e41b7114SYuri Benditovich     },
3338e41b7114SYuri Benditovich };
3339e41b7114SYuri Benditovich 
3340982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
3341982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
3342982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
3343982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
3344982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
33451de81b42SRichard Henderson     .fields = (const VMStateField[]) {
3346982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
3347982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
3348982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
3349982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
3350982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
3351982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
3352982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
3353982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
3354982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3355982b78c5SDr. David Alan Gilbert 
3356982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
3357982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
3358982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
3359982b78c5SDr. David Alan Gilbert          */
3360982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3361982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
3362982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
3363982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3364982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
3365982b78c5SDr. David Alan Gilbert 
3366982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
3367982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3368982b78c5SDr. David Alan Gilbert          * but based on the uint.
3369982b78c5SDr. David Alan Gilbert          */
3370982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3371982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3372982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
3373982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3374982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3375982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
3376982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
3377982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
3378982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
3379982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3380982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
3381441537f1SJason Wang         VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0,
3382982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
3383441537f1SJason Wang         VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1),
3384982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3385982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
3386982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3387982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
3388982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
3389982b78c5SDr. David Alan Gilbert     },
33901de81b42SRichard Henderson     .subsections = (const VMStateDescription * const []) {
3391e41b7114SYuri Benditovich         &vmstate_virtio_net_rss,
3392e41b7114SYuri Benditovich         NULL
3393e41b7114SYuri Benditovich     }
3394982b78c5SDr. David Alan Gilbert };
3395982b78c5SDr. David Alan Gilbert 
33966e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
3397f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
33986e790746SPaolo Bonzini     .size = sizeof(NICState),
33996e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
34006e790746SPaolo Bonzini     .receive = virtio_net_receive,
34016e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
3402b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
3403b2c929f0SDr. David Alan Gilbert     .announce = virtio_net_announce,
34046e790746SPaolo Bonzini };
34056e790746SPaolo Bonzini 
virtio_net_guest_notifier_pending(VirtIODevice * vdev,int idx)34066e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
34076e790746SPaolo Bonzini {
340817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
340968b0a639SSi-Wei Liu     NetClientState *nc;
34106e790746SPaolo Bonzini     assert(n->vhost_started);
34111c188fc8SAkihiko Odaki     if (!n->multiqueue && idx == 2) {
341268b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
341368b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
341468b0a639SSi-Wei Liu          * buggy migration stream.
341568b0a639SSi-Wei Liu          */
341668b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
341768b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
341868b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
341968b0a639SSi-Wei Liu             return false;
342068b0a639SSi-Wei Liu         }
342168b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
342268b0a639SSi-Wei Liu     } else {
342368b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
342468b0a639SSi-Wei Liu     }
3425544f0278SCindy Lu     /*
3426544f0278SCindy Lu      * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
34277e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3428544f0278SCindy Lu      * support, the function will return false
3429544f0278SCindy Lu      */
3430544f0278SCindy Lu 
3431544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
34328aab0d1dSCindy Lu         return vhost_net_config_pending(get_vhost_net(nc->peer));
3433544f0278SCindy Lu     }
3434ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
34356e790746SPaolo Bonzini }
34366e790746SPaolo Bonzini 
virtio_net_guest_notifier_mask(VirtIODevice * vdev,int idx,bool mask)34376e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
34386e790746SPaolo Bonzini                                            bool mask)
34396e790746SPaolo Bonzini {
344017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
344168b0a639SSi-Wei Liu     NetClientState *nc;
34426e790746SPaolo Bonzini     assert(n->vhost_started);
34431c188fc8SAkihiko Odaki     if (!n->multiqueue && idx == 2) {
344468b0a639SSi-Wei Liu         /* Must guard against invalid features and bogus queue index
344568b0a639SSi-Wei Liu          * from being set by malicious guest, or penetrated through
344668b0a639SSi-Wei Liu          * buggy migration stream.
344768b0a639SSi-Wei Liu          */
344868b0a639SSi-Wei Liu         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
344968b0a639SSi-Wei Liu             qemu_log_mask(LOG_GUEST_ERROR,
345068b0a639SSi-Wei Liu                           "%s: bogus vq index ignored\n", __func__);
345168b0a639SSi-Wei Liu             return;
345268b0a639SSi-Wei Liu         }
345368b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
345468b0a639SSi-Wei Liu     } else {
345568b0a639SSi-Wei Liu         nc = qemu_get_subqueue(n->nic, vq2q(idx));
345668b0a639SSi-Wei Liu     }
3457544f0278SCindy Lu     /*
3458544f0278SCindy Lu      *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
34597e8094f0SAlex Bennée      * as the macro of configure interrupt's IDX, If this driver does not
3460544f0278SCindy Lu      * support, the function will return
3461544f0278SCindy Lu      */
3462544f0278SCindy Lu 
3463544f0278SCindy Lu     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
34648aab0d1dSCindy Lu         vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
3465544f0278SCindy Lu         return;
3466544f0278SCindy Lu     }
3467544f0278SCindy Lu     vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
34686e790746SPaolo Bonzini }
34696e790746SPaolo Bonzini 
virtio_net_set_config_size(VirtIONet * n,uint64_t host_features)3470019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
34716e790746SPaolo Bonzini {
34720cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3473a93e599dSMaxime Coquelin 
3474d74c30c8SDaniil Tatianin     n->config_size = virtio_get_config_size(&cfg_size_params, host_features);
347517ec5a86SKONRAD Frederic }
34766e790746SPaolo Bonzini 
virtio_net_set_netclient_name(VirtIONet * n,const char * name,const char * type)34778a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
34788a253ec2SKONRAD Frederic                                    const char *type)
34798a253ec2SKONRAD Frederic {
34808a253ec2SKONRAD Frederic     /*
34818a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
34828a253ec2SKONRAD Frederic      */
34838a253ec2SKONRAD Frederic     assert(type != NULL);
34848a253ec2SKONRAD Frederic 
34858a253ec2SKONRAD Frederic     g_free(n->netclient_name);
34868a253ec2SKONRAD Frederic     g_free(n->netclient_type);
34878a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
34888a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
34898a253ec2SKONRAD Frederic }
34908a253ec2SKONRAD Frederic 
failover_unplug_primary(VirtIONet * n,DeviceState * dev)34910e9a65c5SJuan Quintela static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
34929711cd0dSJens Freimann {
34939711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
34949711cd0dSJens Freimann     PCIDevice *pci_dev;
34959711cd0dSJens Freimann     Error *err = NULL;
34969711cd0dSJens Freimann 
34970e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
34989711cd0dSJens Freimann     if (hotplug_ctrl) {
34990e9a65c5SJuan Quintela         pci_dev = PCI_DEVICE(dev);
35009711cd0dSJens Freimann         pci_dev->partially_hotplugged = true;
35010e9a65c5SJuan Quintela         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
35029711cd0dSJens Freimann         if (err) {
35039711cd0dSJens Freimann             error_report_err(err);
35049711cd0dSJens Freimann             return false;
35059711cd0dSJens Freimann         }
35069711cd0dSJens Freimann     } else {
35079711cd0dSJens Freimann         return false;
35089711cd0dSJens Freimann     }
35099711cd0dSJens Freimann     return true;
35109711cd0dSJens Freimann }
35119711cd0dSJens Freimann 
failover_replug_primary(VirtIONet * n,DeviceState * dev,Error ** errp)35120e9a65c5SJuan Quintela static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
35130e9a65c5SJuan Quintela                                     Error **errp)
35149711cd0dSJens Freimann {
35155a0948d3SMarkus Armbruster     Error *err = NULL;
35169711cd0dSJens Freimann     HotplugHandler *hotplug_ctrl;
35170e9a65c5SJuan Quintela     PCIDevice *pdev = PCI_DEVICE(dev);
351878274682SJuan Quintela     BusState *primary_bus;
35199711cd0dSJens Freimann 
35209711cd0dSJens Freimann     if (!pdev->partially_hotplugged) {
35219711cd0dSJens Freimann         return true;
35229711cd0dSJens Freimann     }
35230e9a65c5SJuan Quintela     primary_bus = dev->parent_bus;
352478274682SJuan Quintela     if (!primary_bus) {
3525150ab54aSJens Freimann         error_setg(errp, "virtio_net: couldn't find primary bus");
35265a0948d3SMarkus Armbruster         return false;
35279711cd0dSJens Freimann     }
35280e9a65c5SJuan Quintela     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3529e2bde83eSJuan Quintela     qatomic_set(&n->failover_primary_hidden, false);
35300e9a65c5SJuan Quintela     hotplug_ctrl = qdev_get_hotplug_handler(dev);
35319711cd0dSJens Freimann     if (hotplug_ctrl) {
35320e9a65c5SJuan Quintela         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
35335a0948d3SMarkus Armbruster         if (err) {
35345a0948d3SMarkus Armbruster             goto out;
35355a0948d3SMarkus Armbruster         }
35360e9a65c5SJuan Quintela         hotplug_handler_plug(hotplug_ctrl, dev, &err);
35379711cd0dSJens Freimann     }
3538109c20eaSLaurent Vivier     pdev->partially_hotplugged = false;
3539150ab54aSJens Freimann 
3540150ab54aSJens Freimann out:
35415a0948d3SMarkus Armbruster     error_propagate(errp, err);
35425a0948d3SMarkus Armbruster     return !err;
35439711cd0dSJens Freimann }
35449711cd0dSJens Freimann 
virtio_net_handle_migration_primary(VirtIONet * n,MigrationEvent * e)35459d9babf7SSteve Sistare static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationEvent *e)
35469711cd0dSJens Freimann {
35479711cd0dSJens Freimann     bool should_be_hidden;
35489711cd0dSJens Freimann     Error *err = NULL;
354907a5d816SJuan Quintela     DeviceState *dev = failover_find_primary_device(n);
355007a5d816SJuan Quintela 
355107a5d816SJuan Quintela     if (!dev) {
355207a5d816SJuan Quintela         return;
355307a5d816SJuan Quintela     }
35549711cd0dSJens Freimann 
3555e2bde83eSJuan Quintela     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
35569711cd0dSJens Freimann 
35579d9babf7SSteve Sistare     if (e->type == MIG_EVENT_PRECOPY_SETUP && !should_be_hidden) {
355807a5d816SJuan Quintela         if (failover_unplug_primary(n, dev)) {
355907a5d816SJuan Quintela             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
356007a5d816SJuan Quintela             qapi_event_send_unplug_primary(dev->id);
3561e2bde83eSJuan Quintela             qatomic_set(&n->failover_primary_hidden, true);
35629711cd0dSJens Freimann         } else {
35639711cd0dSJens Freimann             warn_report("couldn't unplug primary device");
35649711cd0dSJens Freimann         }
35659d9babf7SSteve Sistare     } else if (e->type == MIG_EVENT_PRECOPY_FAILED) {
3566150ab54aSJens Freimann         /* We already unplugged the device let's plug it back */
356707a5d816SJuan Quintela         if (!failover_replug_primary(n, dev, &err)) {
35689711cd0dSJens Freimann             if (err) {
35699711cd0dSJens Freimann                 error_report_err(err);
35709711cd0dSJens Freimann             }
35719711cd0dSJens Freimann         }
35729711cd0dSJens Freimann     }
35739711cd0dSJens Freimann }
35749711cd0dSJens Freimann 
virtio_net_migration_state_notifier(NotifierWithReturn * notifier,MigrationEvent * e,Error ** errp)35753e775730SSteve Sistare static int virtio_net_migration_state_notifier(NotifierWithReturn *notifier,
35765663dd3fSSteve Sistare                                                MigrationEvent *e, Error **errp)
35779711cd0dSJens Freimann {
35789711cd0dSJens Freimann     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
35799d9babf7SSteve Sistare     virtio_net_handle_migration_primary(n, e);
35803e775730SSteve Sistare     return 0;
35819711cd0dSJens Freimann }
35829711cd0dSJens Freimann 
failover_hide_primary_device(DeviceListener * listener,const QDict * device_opts,bool from_json,Error ** errp)3583b91ad981SJuan Quintela static bool failover_hide_primary_device(DeviceListener *listener,
3584f3558b1bSKevin Wolf                                          const QDict *device_opts,
3585f3558b1bSKevin Wolf                                          bool from_json,
3586f3558b1bSKevin Wolf                                          Error **errp)
35879711cd0dSJens Freimann {
35889711cd0dSJens Freimann     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
35894f0303aeSJuan Quintela     const char *standby_id;
35909711cd0dSJens Freimann 
35914d0e59acSJens Freimann     if (!device_opts) {
359289631fedSJuan Quintela         return false;
35934d0e59acSJens Freimann     }
3594bcfc906bSLaurent Vivier 
3595bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "failover_pair_id")) {
3596bcfc906bSLaurent Vivier         return false;
3597bcfc906bSLaurent Vivier     }
3598bcfc906bSLaurent Vivier 
3599bcfc906bSLaurent Vivier     if (!qdict_haskey(device_opts, "id")) {
3600bcfc906bSLaurent Vivier         error_setg(errp, "Device with failover_pair_id needs to have id");
3601bcfc906bSLaurent Vivier         return false;
3602bcfc906bSLaurent Vivier     }
3603bcfc906bSLaurent Vivier 
3604bcfc906bSLaurent Vivier     standby_id = qdict_get_str(device_opts, "failover_pair_id");
360589631fedSJuan Quintela     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
360689631fedSJuan Quintela         return false;
36079711cd0dSJens Freimann     }
36089711cd0dSJens Freimann 
36097fe7791eSLaurent Vivier     /*
36107fe7791eSLaurent Vivier      * The hide helper can be called several times for a given device.
36117fe7791eSLaurent Vivier      * Check there is only one primary for a virtio-net device but
36127fe7791eSLaurent Vivier      * don't duplicate the qdict several times if it's called for the same
36137fe7791eSLaurent Vivier      * device.
36147fe7791eSLaurent Vivier      */
3615259a10dbSKevin Wolf     if (n->primary_opts) {
36167fe7791eSLaurent Vivier         const char *old, *new;
36177fe7791eSLaurent Vivier         /* devices with failover_pair_id always have an id */
36187fe7791eSLaurent Vivier         old = qdict_get_str(n->primary_opts, "id");
36197fe7791eSLaurent Vivier         new = qdict_get_str(device_opts, "id");
36207fe7791eSLaurent Vivier         if (strcmp(old, new) != 0) {
36217fe7791eSLaurent Vivier             error_setg(errp, "Cannot attach more than one primary device to "
36227fe7791eSLaurent Vivier                        "'%s': '%s' and '%s'", n->netclient_name, old, new);
3623259a10dbSKevin Wolf             return false;
3624259a10dbSKevin Wolf         }
36257fe7791eSLaurent Vivier     } else {
3626f3558b1bSKevin Wolf         n->primary_opts = qdict_clone_shallow(device_opts);
3627f3558b1bSKevin Wolf         n->primary_opts_from_json = from_json;
36287fe7791eSLaurent Vivier     }
3629259a10dbSKevin Wolf 
3630e2bde83eSJuan Quintela     /* failover_primary_hidden is set during feature negotiation */
36313abad4a2SJuan Quintela     return qatomic_read(&n->failover_primary_hidden);
36329711cd0dSJens Freimann }
36339711cd0dSJens Freimann 
virtio_net_device_realize(DeviceState * dev,Error ** errp)3634e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
363517ec5a86SKONRAD Frederic {
3636e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3637284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3638284a32f0SAndreas Färber     NetClientState *nc;
36391773d9eeSKONRAD Frederic     int i;
364017ec5a86SKONRAD Frederic 
3641a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
3642127833eeSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3643a93e599dSMaxime Coquelin     }
3644a93e599dSMaxime Coquelin 
36459473939eSJason Baron     if (n->net_conf.duplex_str) {
36469473939eSJason Baron         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
36479473939eSJason Baron             n->net_conf.duplex = DUPLEX_HALF;
36489473939eSJason Baron         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
36499473939eSJason Baron             n->net_conf.duplex = DUPLEX_FULL;
36509473939eSJason Baron         } else {
36519473939eSJason Baron             error_setg(errp, "'duplex' must be 'half' or 'full'");
3652843c4cfcSMarkus Armbruster             return;
36539473939eSJason Baron         }
36549473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
36559473939eSJason Baron     } else {
36569473939eSJason Baron         n->net_conf.duplex = DUPLEX_UNKNOWN;
36579473939eSJason Baron     }
36589473939eSJason Baron 
36599473939eSJason Baron     if (n->net_conf.speed < SPEED_UNKNOWN) {
36609473939eSJason Baron         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3661843c4cfcSMarkus Armbruster         return;
3662843c4cfcSMarkus Armbruster     }
3663843c4cfcSMarkus Armbruster     if (n->net_conf.speed >= 0) {
36649473939eSJason Baron         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
36659473939eSJason Baron     }
36669473939eSJason Baron 
36679711cd0dSJens Freimann     if (n->failover) {
3668b91ad981SJuan Quintela         n->primary_listener.hide_device = failover_hide_primary_device;
3669e2bde83eSJuan Quintela         qatomic_set(&n->failover_primary_hidden, true);
36709711cd0dSJens Freimann         device_listener_register(&n->primary_listener);
3671d9cda213SSteve Sistare         migration_add_notifier(&n->migration_state,
3672d9cda213SSteve Sistare                                virtio_net_migration_state_notifier);
36739711cd0dSJens Freimann         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
36749711cd0dSJens Freimann     }
36759711cd0dSJens Freimann 
3676da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
36773857cd5cSJonah Palmer     virtio_init(vdev, VIRTIO_ID_NET, n->config_size);
367817ec5a86SKONRAD Frederic 
36791c0fbfa3SMichael S. Tsirkin     /*
36801c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
36811c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
36821c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
36831c0fbfa3SMichael S. Tsirkin      */
36841c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
36851c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
36865f997fd1SMichal Privoznik         !is_power_of_2(n->net_conf.rx_queue_size)) {
36871c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
36881c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
36891c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
36901c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
36911c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
36921c0fbfa3SMichael S. Tsirkin         return;
36931c0fbfa3SMichael S. Tsirkin     }
36941c0fbfa3SMichael S. Tsirkin 
36959b02e161SWei Wang     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
36964271f403SLaurent Vivier         n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) ||
36979b02e161SWei Wang         !is_power_of_2(n->net_conf.tx_queue_size)) {
36989b02e161SWei Wang         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
36999b02e161SWei Wang                    "must be a power of 2 between %d and %d",
37009b02e161SWei Wang                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
37014271f403SLaurent Vivier                    virtio_net_max_tx_queue_size(n));
37029b02e161SWei Wang         virtio_cleanup(vdev);
37039b02e161SWei Wang         return;
37049b02e161SWei Wang     }
37059b02e161SWei Wang 
370622288fe5SJason Wang     n->max_ncs = MAX(n->nic_conf.peers.queues, 1);
370722288fe5SJason Wang 
370822288fe5SJason Wang     /*
370922288fe5SJason Wang      * Figure out the datapath queue pairs since the backend could
371022288fe5SJason Wang      * provide control queue via peers as well.
371122288fe5SJason Wang      */
371222288fe5SJason Wang     if (n->nic_conf.peers.queues) {
371322288fe5SJason Wang         for (i = 0; i < n->max_ncs; i++) {
371422288fe5SJason Wang             if (n->nic_conf.peers.ncs[i]->is_datapath) {
371522288fe5SJason Wang                 ++n->max_queue_pairs;
371622288fe5SJason Wang             }
371722288fe5SJason Wang         }
371822288fe5SJason Wang     }
371922288fe5SJason Wang     n->max_queue_pairs = MAX(n->max_queue_pairs, 1);
372022288fe5SJason Wang 
3721441537f1SJason Wang     if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) {
372222288fe5SJason Wang         error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), "
3723631b22eaSStefan Weil                    "must be a positive integer less than %d.",
3724441537f1SJason Wang                    n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2);
37257e0e736eSJason Wang         virtio_cleanup(vdev);
37267e0e736eSJason Wang         return;
37277e0e736eSJason Wang     }
3728b21e2380SMarkus Armbruster     n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs);
3729441537f1SJason Wang     n->curr_queue_pairs = 1;
37301773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
37316e790746SPaolo Bonzini 
37321773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
37331773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
37340765691eSMarkus Armbruster         warn_report("virtio-net: "
37356e790746SPaolo Bonzini                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
37361773d9eeSKONRAD Frederic                     n->net_conf.tx);
37370765691eSMarkus Armbruster         error_printf("Defaulting to \"bh\"");
37386e790746SPaolo Bonzini     }
37396e790746SPaolo Bonzini 
37402eef278bSMichael S. Tsirkin     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
37412eef278bSMichael S. Tsirkin                                     n->net_conf.tx_queue_size);
37429b02e161SWei Wang 
37438c497568SAkihiko Odaki     virtio_net_add_queue(n, 0);
3744da51a335SJason Wang 
374517a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
37461773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
37471773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
37486e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
37499d8c6a25SDr. David Alan Gilbert     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
37509d8c6a25SDr. David Alan Gilbert                               QEMU_CLOCK_VIRTUAL,
3751f57fcf70SJason Wang                               virtio_net_announce_timer, n);
3752b2c929f0SDr. David Alan Gilbert     n->announce_timer.round = 0;
37536e790746SPaolo Bonzini 
37548a253ec2SKONRAD Frederic     if (n->netclient_type) {
37558a253ec2SKONRAD Frederic         /*
37568a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
37578a253ec2SKONRAD Frederic          */
37588a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
37597d0fefdfSAkihiko Odaki                               n->netclient_type, n->netclient_name,
37607d0fefdfSAkihiko Odaki                               &dev->mem_reentrancy_guard, n);
37618a253ec2SKONRAD Frederic     } else {
37621773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
37637d0fefdfSAkihiko Odaki                               object_get_typename(OBJECT(dev)), dev->id,
37647d0fefdfSAkihiko Odaki                               &dev->mem_reentrancy_guard, n);
37658a253ec2SKONRAD Frederic     }
37668a253ec2SKONRAD Frederic 
3767441537f1SJason Wang     for (i = 0; i < n->max_queue_pairs; i++) {
3768d4c62930SBin Meng         n->nic->ncs[i].do_not_pad = true;
3769d4c62930SBin Meng     }
3770d4c62930SBin Meng 
37716e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
37726e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
37736e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
37746e790746SPaolo Bonzini     } else {
37756e790746SPaolo Bonzini         n->host_hdr_len = 0;
37766e790746SPaolo Bonzini     }
37776e790746SPaolo Bonzini 
37781773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
37796e790746SPaolo Bonzini 
37806e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
37811773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
3782e22f0603SYuri Benditovich     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
37836e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
37846e790746SPaolo Bonzini 
37856e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
37866e790746SPaolo Bonzini 
37876e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
37886e790746SPaolo Bonzini 
3789b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
3790b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
3791b1be4280SAmos Kong 
3792e87936eaSCindy Lu    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3793e87936eaSCindy Lu         struct virtio_net_config netcfg = {};
3794e87936eaSCindy Lu         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3795e87936eaSCindy Lu         vhost_net_set_config(get_vhost_net(nc->peer),
3796f8ed3648SManos Pitsidianakis             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND);
3797e87936eaSCindy Lu     }
37982974e916SYuri Benditovich     QTAILQ_INIT(&n->rsc_chains);
3799284a32f0SAndreas Färber     n->qdev = dev;
38004474e37aSYuri Benditovich 
3801aac8f89dSAkihiko Odaki     net_rx_pkt_init(&n->rx_pkt);
38020145c393SAndrew Melnychenko 
38030145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
3804b5900dffSDaniel P. Berrangé         Error *err = NULL;
3805b5900dffSDaniel P. Berrangé         if (!virtio_net_load_ebpf(n, &err)) {
3806b5900dffSDaniel P. Berrangé             /*
3807b5900dffSDaniel P. Berrangé              * If user explicitly gave QEMU RSS FDs to use, then
3808b5900dffSDaniel P. Berrangé              * failing to use them must be considered a fatal
3809b5900dffSDaniel P. Berrangé              * error. If no RSS FDs were provided, QEMU is trying
3810b5900dffSDaniel P. Berrangé              * eBPF on a "best effort" basis only, so report a
3811b5900dffSDaniel P. Berrangé              * warning and allow fallback to software RSS.
3812b5900dffSDaniel P. Berrangé              */
3813b5900dffSDaniel P. Berrangé             if (n->ebpf_rss_fds) {
3814b5900dffSDaniel P. Berrangé                 error_propagate(errp, err);
3815b5900dffSDaniel P. Berrangé             } else {
3816b5900dffSDaniel P. Berrangé                 warn_report("unable to load eBPF RSS: %s",
3817b5900dffSDaniel P. Berrangé                             error_get_pretty(err));
3818b5900dffSDaniel P. Berrangé                 error_free(err);
3819b5900dffSDaniel P. Berrangé             }
3820b5900dffSDaniel P. Berrangé         }
38210145c393SAndrew Melnychenko     }
382217ec5a86SKONRAD Frederic }
382317ec5a86SKONRAD Frederic 
virtio_net_device_unrealize(DeviceState * dev)3824b69c3c21SMarkus Armbruster static void virtio_net_device_unrealize(DeviceState *dev)
382517ec5a86SKONRAD Frederic {
3826306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3827306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
3828441537f1SJason Wang     int i, max_queue_pairs;
382917ec5a86SKONRAD Frederic 
38300145c393SAndrew Melnychenko     if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
38310145c393SAndrew Melnychenko         virtio_net_unload_ebpf(n);
38320145c393SAndrew Melnychenko     }
38330145c393SAndrew Melnychenko 
383417ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
383517ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
383617ec5a86SKONRAD Frederic 
38378a253ec2SKONRAD Frederic     g_free(n->netclient_name);
38388a253ec2SKONRAD Frederic     n->netclient_name = NULL;
38398a253ec2SKONRAD Frederic     g_free(n->netclient_type);
38408a253ec2SKONRAD Frederic     n->netclient_type = NULL;
38418a253ec2SKONRAD Frederic 
384217ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
384317ec5a86SKONRAD Frederic     g_free(n->vlans);
384417ec5a86SKONRAD Frederic 
38459711cd0dSJens Freimann     if (n->failover) {
3846f3558b1bSKevin Wolf         qobject_unref(n->primary_opts);
384765018100SJuan Quintela         device_listener_unregister(&n->primary_listener);
3848d9cda213SSteve Sistare         migration_remove_notifier(&n->migration_state);
3849f3558b1bSKevin Wolf     } else {
3850f3558b1bSKevin Wolf         assert(n->primary_opts == NULL);
38519711cd0dSJens Freimann     }
38529711cd0dSJens Freimann 
3853441537f1SJason Wang     max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
3854441537f1SJason Wang     for (i = 0; i < max_queue_pairs; i++) {
3855f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
385617ec5a86SKONRAD Frederic     }
3857d945d9f1SYuri Benditovich     /* delete also control vq */
3858441537f1SJason Wang     virtio_del_queue(vdev, max_queue_pairs * 2);
3859944458b6SDr. David Alan Gilbert     qemu_announce_timer_del(&n->announce_timer, false);
386017ec5a86SKONRAD Frederic     g_free(n->vqs);
386117ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
38622974e916SYuri Benditovich     virtio_net_rsc_cleanup(n);
386359079029SYuri Benditovich     g_free(n->rss_data.indirections_table);
38644474e37aSYuri Benditovich     net_rx_pkt_uninit(n->rx_pkt);
38656a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
386617ec5a86SKONRAD Frederic }
386717ec5a86SKONRAD Frederic 
virtio_net_reset(VirtIODevice * vdev)3868cef776c0SAkihiko Odaki static void virtio_net_reset(VirtIODevice *vdev)
3869cef776c0SAkihiko Odaki {
3870cef776c0SAkihiko Odaki     VirtIONet *n = VIRTIO_NET(vdev);
3871cef776c0SAkihiko Odaki     int i;
3872cef776c0SAkihiko Odaki 
3873cef776c0SAkihiko Odaki     /* Reset back to compatibility mode */
3874cef776c0SAkihiko Odaki     n->promisc = 1;
3875cef776c0SAkihiko Odaki     n->allmulti = 0;
3876cef776c0SAkihiko Odaki     n->alluni = 0;
3877cef776c0SAkihiko Odaki     n->nomulti = 0;
3878cef776c0SAkihiko Odaki     n->nouni = 0;
3879cef776c0SAkihiko Odaki     n->nobcast = 0;
3880cef776c0SAkihiko Odaki     /* multiqueue is disabled by default */
3881cef776c0SAkihiko Odaki     n->curr_queue_pairs = 1;
3882cef776c0SAkihiko Odaki     timer_del(n->announce_timer.tm);
3883cef776c0SAkihiko Odaki     n->announce_timer.round = 0;
3884cef776c0SAkihiko Odaki     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
3885cef776c0SAkihiko Odaki 
3886cef776c0SAkihiko Odaki     /* Flush any MAC and VLAN filter table state */
3887cef776c0SAkihiko Odaki     n->mac_table.in_use = 0;
3888cef776c0SAkihiko Odaki     n->mac_table.first_multi = 0;
3889cef776c0SAkihiko Odaki     n->mac_table.multi_overflow = 0;
3890cef776c0SAkihiko Odaki     n->mac_table.uni_overflow = 0;
3891cef776c0SAkihiko Odaki     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
3892cef776c0SAkihiko Odaki     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
3893cef776c0SAkihiko Odaki     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
3894cef776c0SAkihiko Odaki     memset(n->vlans, 0, MAX_VLAN >> 3);
3895cef776c0SAkihiko Odaki 
3896cef776c0SAkihiko Odaki     /* Flush any async TX */
3897cef776c0SAkihiko Odaki     for (i = 0;  i < n->max_queue_pairs; i++) {
3898cef776c0SAkihiko Odaki         flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i));
3899cef776c0SAkihiko Odaki     }
3900cef776c0SAkihiko Odaki 
3901cef776c0SAkihiko Odaki     virtio_net_disable_rss(n);
3902cef776c0SAkihiko Odaki }
3903cef776c0SAkihiko Odaki 
virtio_net_instance_init(Object * obj)390417ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
390517ec5a86SKONRAD Frederic {
390617ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
390717ec5a86SKONRAD Frederic 
390817ec5a86SKONRAD Frederic     /*
390917ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
39102431f4f1SMichael Tokarev      * Can be overridden with virtio_net_set_config_size.
391117ec5a86SKONRAD Frederic      */
391217ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
3913aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3914aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
391540c2281cSMarkus Armbruster                                   DEVICE(n));
39160145c393SAndrew Melnychenko 
39170145c393SAndrew Melnychenko     ebpf_rss_init(&n->ebpf_rss);
391817ec5a86SKONRAD Frederic }
391917ec5a86SKONRAD Frederic 
virtio_net_pre_save(void * opaque)392044b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque)
39214d45dcfbSHalil Pasic {
39224d45dcfbSHalil Pasic     VirtIONet *n = opaque;
39234d45dcfbSHalil Pasic 
39244d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
39254d45dcfbSHalil Pasic      * it might keep writing to memory. */
39264d45dcfbSHalil Pasic     assert(!n->vhost_started);
392744b1ff31SDr. David Alan Gilbert 
392844b1ff31SDr. David Alan Gilbert     return 0;
39294d45dcfbSHalil Pasic }
39304d45dcfbSHalil Pasic 
primary_unplug_pending(void * opaque)39319711cd0dSJens Freimann static bool primary_unplug_pending(void *opaque)
39329711cd0dSJens Freimann {
39339711cd0dSJens Freimann     DeviceState *dev = opaque;
393421e8709bSJuan Quintela     DeviceState *primary;
39359711cd0dSJens Freimann     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
39369711cd0dSJens Freimann     VirtIONet *n = VIRTIO_NET(vdev);
39379711cd0dSJens Freimann 
3938284f42a5SJens Freimann     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3939284f42a5SJens Freimann         return false;
3940284f42a5SJens Freimann     }
394121e8709bSJuan Quintela     primary = failover_find_primary_device(n);
394221e8709bSJuan Quintela     return primary ? primary->pending_deleted_event : false;
39439711cd0dSJens Freimann }
39449711cd0dSJens Freimann 
dev_unplug_pending(void * opaque)39459711cd0dSJens Freimann static bool dev_unplug_pending(void *opaque)
39469711cd0dSJens Freimann {
39479711cd0dSJens Freimann     DeviceState *dev = opaque;
39489711cd0dSJens Freimann     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
39499711cd0dSJens Freimann 
39509711cd0dSJens Freimann     return vdc->primary_unplug_pending(dev);
39519711cd0dSJens Freimann }
39529711cd0dSJens Freimann 
virtio_net_get_vhost(VirtIODevice * vdev)3953c255488dSJonah Palmer static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
3954c255488dSJonah Palmer {
3955c255488dSJonah Palmer     VirtIONet *n = VIRTIO_NET(vdev);
395600adced5SHanna Czenczek     NetClientState *nc;
395700adced5SHanna Czenczek     struct vhost_net *net;
395800adced5SHanna Czenczek 
395900adced5SHanna Czenczek     if (!n->nic) {
396000adced5SHanna Czenczek         return NULL;
396100adced5SHanna Czenczek     }
396200adced5SHanna Czenczek 
396300adced5SHanna Czenczek     nc = qemu_get_queue(n->nic);
396400adced5SHanna Czenczek     if (!nc) {
396500adced5SHanna Czenczek         return NULL;
396600adced5SHanna Czenczek     }
396700adced5SHanna Czenczek 
396800adced5SHanna Czenczek     net = get_vhost_net(nc->peer);
396900adced5SHanna Czenczek     if (!net) {
397000adced5SHanna Czenczek         return NULL;
397100adced5SHanna Czenczek     }
397200adced5SHanna Czenczek 
3973c255488dSJonah Palmer     return &net->dev;
3974c255488dSJonah Palmer }
3975c255488dSJonah Palmer 
39764d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
39774d45dcfbSHalil Pasic     .name = "virtio-net",
39784d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
39794d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
39801de81b42SRichard Henderson     .fields = (const VMStateField[]) {
39814d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
39824d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
39834d45dcfbSHalil Pasic     },
39844d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
39859711cd0dSJens Freimann     .dev_unplug_pending = dev_unplug_pending,
39864d45dcfbSHalil Pasic };
3987290c2428SDr. David Alan Gilbert 
398817ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
3989127833eeSJason Baron     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3990127833eeSJason Baron                     VIRTIO_NET_F_CSUM, true),
3991127833eeSJason Baron     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
399287108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
3993127833eeSJason Baron     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3994127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
399587108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
3996127833eeSJason Baron     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
399787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
3998127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
399987108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
4000127833eeSJason Baron     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
400187108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
4002127833eeSJason Baron     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
400387108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
4004127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
400587108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
4006127833eeSJason Baron     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
400787108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
4008127833eeSJason Baron     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
400987108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
4010127833eeSJason Baron     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
401187108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
4012127833eeSJason Baron     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
401387108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
4014127833eeSJason Baron     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
401587108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
4016127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
401787108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
4018127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
401987108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
4020127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
402187108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
4022127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
402387108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
4024127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
402587108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
4026127833eeSJason Baron     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
402787108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
4028127833eeSJason Baron     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
402959079029SYuri Benditovich     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
403059079029SYuri Benditovich                     VIRTIO_NET_F_RSS, false),
4031e22f0603SYuri Benditovich     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
4032e22f0603SYuri Benditovich                     VIRTIO_NET_F_HASH_REPORT, false),
40336b230b7dSAndrew Melnychenko     DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet, nr_ebpf_rss_fds,
40346b230b7dSAndrew Melnychenko                       ebpf_rss_fds, qdev_prop_string, char*),
40352974e916SYuri Benditovich     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
40362974e916SYuri Benditovich                     VIRTIO_NET_F_RSC_EXT, false),
40372974e916SYuri Benditovich     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
40382974e916SYuri Benditovich                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
403917ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
404017ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
404117ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
404217ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
404317ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
40441c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
40451c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
40469b02e161SWei Wang     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
40479b02e161SWei Wang                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
4048a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
404975ebec11SMaxime Coquelin     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
405075ebec11SMaxime Coquelin                      true),
40519473939eSJason Baron     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
40529473939eSJason Baron     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
40539711cd0dSJens Freimann     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
405453da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("guest_uso4", VirtIONet, host_features,
405553da8b5aSYuri Benditovich                       VIRTIO_NET_F_GUEST_USO4, true),
405653da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("guest_uso6", VirtIONet, host_features,
405753da8b5aSYuri Benditovich                       VIRTIO_NET_F_GUEST_USO6, true),
405853da8b5aSYuri Benditovich     DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
405953da8b5aSYuri Benditovich                       VIRTIO_NET_F_HOST_USO, true),
406017ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
406117ec5a86SKONRAD Frederic };
406217ec5a86SKONRAD Frederic 
virtio_net_class_init(ObjectClass * klass,void * data)406317ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
406417ec5a86SKONRAD Frederic {
406517ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
406617ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
4067e6f746b3SAndreas Färber 
40684f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
4069290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
4070125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
4071e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
4072306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
407317ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
407417ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
407517ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
407617ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
407717ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
407817ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
40797dc6be52SXuan Zhuo     vdc->queue_reset = virtio_net_queue_reset;
40807f863302SKangjie Xu     vdc->queue_enable = virtio_net_queue_enable;
408117ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
408217ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
408317ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
40842a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
4085*9379ea9dSAkihiko Odaki     vdc->pre_load_queues = virtio_net_pre_load_queues;
40867788c3f2SMikhail Sennikovsky     vdc->post_load = virtio_net_post_load_virtio;
4087982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
40889711cd0dSJens Freimann     vdc->primary_unplug_pending = primary_unplug_pending;
4089c255488dSJonah Palmer     vdc->get_vhost = virtio_net_get_vhost;
4090cd9b8346SViktor Prutyanov     vdc->toggle_device_iotlb = vhost_toggle_device_iotlb;
409117ec5a86SKONRAD Frederic }
409217ec5a86SKONRAD Frederic 
409317ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
409417ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
409517ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
409617ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
409717ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
409817ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
409917ec5a86SKONRAD Frederic };
410017ec5a86SKONRAD Frederic 
virtio_register_types(void)410117ec5a86SKONRAD Frederic static void virtio_register_types(void)
410217ec5a86SKONRAD Frederic {
410317ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
410417ec5a86SKONRAD Frederic }
410517ec5a86SKONRAD Frederic 
410617ec5a86SKONRAD Frederic type_init(virtio_register_types)
4107