xref: /openbmc/qemu/hw/net/virtio-net.c (revision 97cd965c070152bc626c7507df9fb356bbe1cd81)
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"
156e790746SPaolo Bonzini #include "qemu/iov.h"
166e790746SPaolo Bonzini #include "hw/virtio/virtio.h"
176e790746SPaolo Bonzini #include "net/net.h"
186e790746SPaolo Bonzini #include "net/checksum.h"
196e790746SPaolo Bonzini #include "net/tap.h"
206e790746SPaolo Bonzini #include "qemu/error-report.h"
216e790746SPaolo Bonzini #include "qemu/timer.h"
226e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h"
236e790746SPaolo Bonzini #include "net/vhost_net.h"
2417ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h"
25b1be4280SAmos Kong #include "qapi/qmp/qjson.h"
2606150279SWenchao Xia #include "qapi-event.h"
271399c60dSRusty Russell #include "hw/virtio/virtio-access.h"
286e790746SPaolo Bonzini 
296e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
306e790746SPaolo Bonzini 
316e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES    64
326e790746SPaolo Bonzini #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
336e790746SPaolo Bonzini 
341c0fbfa3SMichael S. Tsirkin /* previously fixed value */
351c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
361c0fbfa3SMichael S. Tsirkin /* for now, only allow larger queues; with virtio-1, guest can downsize */
371c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
381c0fbfa3SMichael S. Tsirkin 
396e790746SPaolo Bonzini /*
406e790746SPaolo Bonzini  * Calculate the number of bytes up to and including the given 'field' of
416e790746SPaolo Bonzini  * 'container'.
426e790746SPaolo Bonzini  */
436e790746SPaolo Bonzini #define endof(container, field) \
446e790746SPaolo Bonzini     (offsetof(container, field) + sizeof(((container *)0)->field))
456e790746SPaolo Bonzini 
466e790746SPaolo Bonzini typedef struct VirtIOFeature {
476e790746SPaolo Bonzini     uint32_t flags;
486e790746SPaolo Bonzini     size_t end;
496e790746SPaolo Bonzini } VirtIOFeature;
506e790746SPaolo Bonzini 
516e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = {
526e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MAC,
536e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, mac)},
546e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_STATUS,
556e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, status)},
566e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MQ,
576e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
58a93e599dSMaxime Coquelin     {.flags = 1 << VIRTIO_NET_F_MTU,
59a93e599dSMaxime Coquelin      .end = endof(struct virtio_net_config, mtu)},
606e790746SPaolo Bonzini     {}
616e790746SPaolo Bonzini };
626e790746SPaolo Bonzini 
636e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
646e790746SPaolo Bonzini {
656e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
666e790746SPaolo Bonzini 
676e790746SPaolo Bonzini     return &n->vqs[nc->queue_index];
686e790746SPaolo Bonzini }
696e790746SPaolo Bonzini 
706e790746SPaolo Bonzini static int vq2q(int queue_index)
716e790746SPaolo Bonzini {
726e790746SPaolo Bonzini     return queue_index / 2;
736e790746SPaolo Bonzini }
746e790746SPaolo Bonzini 
756e790746SPaolo Bonzini /* TODO
766e790746SPaolo Bonzini  * - we could suppress RX interrupt if we were so inclined.
776e790746SPaolo Bonzini  */
786e790746SPaolo Bonzini 
796e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
806e790746SPaolo Bonzini {
8117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
826e790746SPaolo Bonzini     struct virtio_net_config netcfg;
836e790746SPaolo Bonzini 
841399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.status, n->status);
851399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
86a93e599dSMaxime Coquelin     virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
876e790746SPaolo Bonzini     memcpy(netcfg.mac, n->mac, ETH_ALEN);
886e790746SPaolo Bonzini     memcpy(config, &netcfg, n->config_size);
896e790746SPaolo Bonzini }
906e790746SPaolo Bonzini 
916e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
926e790746SPaolo Bonzini {
9317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
946e790746SPaolo Bonzini     struct virtio_net_config netcfg = {};
956e790746SPaolo Bonzini 
966e790746SPaolo Bonzini     memcpy(&netcfg, config, n->config_size);
976e790746SPaolo Bonzini 
9895129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
9995129d6fSCornelia Huck         !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
1006e790746SPaolo Bonzini         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
1016e790746SPaolo Bonzini         memcpy(n->mac, netcfg.mac, ETH_ALEN);
1026e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1036e790746SPaolo Bonzini     }
1046e790746SPaolo Bonzini }
1056e790746SPaolo Bonzini 
1066e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status)
1076e790746SPaolo Bonzini {
10817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1096e790746SPaolo Bonzini     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
11017a0ca55SKONRAD Frederic         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
1116e790746SPaolo Bonzini }
1126e790746SPaolo Bonzini 
113f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque)
114f57fcf70SJason Wang {
115f57fcf70SJason Wang     VirtIONet *n = opaque;
116f57fcf70SJason Wang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
117f57fcf70SJason Wang 
118f57fcf70SJason Wang     n->announce_counter--;
119f57fcf70SJason Wang     n->status |= VIRTIO_NET_S_ANNOUNCE;
120f57fcf70SJason Wang     virtio_notify_config(vdev);
121f57fcf70SJason Wang }
122f57fcf70SJason Wang 
1236e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
1246e790746SPaolo Bonzini {
12517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1266e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
1276e790746SPaolo Bonzini     int queues = n->multiqueue ? n->max_queues : 1;
1286e790746SPaolo Bonzini 
129ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
1306e790746SPaolo Bonzini         return;
1316e790746SPaolo Bonzini     }
1326e790746SPaolo Bonzini 
1338c1ac475SRadim Krčmář     if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
1348c1ac475SRadim Krčmář         !!n->vhost_started) {
1356e790746SPaolo Bonzini         return;
1366e790746SPaolo Bonzini     }
1376e790746SPaolo Bonzini     if (!n->vhost_started) {
138086abc1cSMichael S. Tsirkin         int r, i;
139086abc1cSMichael S. Tsirkin 
1401bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
1411bfa316cSGreg Kurz             error_report("backend does not support %s vnet headers; "
1421bfa316cSGreg Kurz                          "falling back on userspace virtio",
1431bfa316cSGreg Kurz                          virtio_is_big_endian(vdev) ? "BE" : "LE");
1441bfa316cSGreg Kurz             return;
1451bfa316cSGreg Kurz         }
1461bfa316cSGreg Kurz 
147086abc1cSMichael S. Tsirkin         /* Any packets outstanding? Purge them to avoid touching rings
148086abc1cSMichael S. Tsirkin          * when vhost is running.
149086abc1cSMichael S. Tsirkin          */
150086abc1cSMichael S. Tsirkin         for (i = 0;  i < queues; i++) {
151086abc1cSMichael S. Tsirkin             NetClientState *qnc = qemu_get_subqueue(n->nic, i);
152086abc1cSMichael S. Tsirkin 
153086abc1cSMichael S. Tsirkin             /* Purge both directions: TX and RX. */
154086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
155086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
156086abc1cSMichael S. Tsirkin         }
157086abc1cSMichael S. Tsirkin 
158a93e599dSMaxime Coquelin         if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
159a93e599dSMaxime Coquelin             r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
160a93e599dSMaxime Coquelin             if (r < 0) {
161a93e599dSMaxime Coquelin                 error_report("%uBytes MTU not supported by the backend",
162a93e599dSMaxime Coquelin                              n->net_conf.mtu);
163a93e599dSMaxime Coquelin 
164a93e599dSMaxime Coquelin                 return;
165a93e599dSMaxime Coquelin             }
166a93e599dSMaxime Coquelin         }
167a93e599dSMaxime Coquelin 
1686e790746SPaolo Bonzini         n->vhost_started = 1;
16917a0ca55SKONRAD Frederic         r = vhost_net_start(vdev, n->nic->ncs, queues);
1706e790746SPaolo Bonzini         if (r < 0) {
1716e790746SPaolo Bonzini             error_report("unable to start vhost net: %d: "
1726e790746SPaolo Bonzini                          "falling back on userspace virtio", -r);
1736e790746SPaolo Bonzini             n->vhost_started = 0;
1746e790746SPaolo Bonzini         }
1756e790746SPaolo Bonzini     } else {
17617a0ca55SKONRAD Frederic         vhost_net_stop(vdev, n->nic->ncs, queues);
1776e790746SPaolo Bonzini         n->vhost_started = 0;
1786e790746SPaolo Bonzini     }
1796e790746SPaolo Bonzini }
1806e790746SPaolo Bonzini 
1811bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
1821bfa316cSGreg Kurz                                           NetClientState *peer,
1831bfa316cSGreg Kurz                                           bool enable)
1841bfa316cSGreg Kurz {
1851bfa316cSGreg Kurz     if (virtio_is_big_endian(vdev)) {
1861bfa316cSGreg Kurz         return qemu_set_vnet_be(peer, enable);
1871bfa316cSGreg Kurz     } else {
1881bfa316cSGreg Kurz         return qemu_set_vnet_le(peer, enable);
1891bfa316cSGreg Kurz     }
1901bfa316cSGreg Kurz }
1911bfa316cSGreg Kurz 
1921bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
1931bfa316cSGreg Kurz                                        int queues, bool enable)
1941bfa316cSGreg Kurz {
1951bfa316cSGreg Kurz     int i;
1961bfa316cSGreg Kurz 
1971bfa316cSGreg Kurz     for (i = 0; i < queues; i++) {
1981bfa316cSGreg Kurz         if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
1991bfa316cSGreg Kurz             enable) {
2001bfa316cSGreg Kurz             while (--i >= 0) {
2011bfa316cSGreg Kurz                 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
2021bfa316cSGreg Kurz             }
2031bfa316cSGreg Kurz 
2041bfa316cSGreg Kurz             return true;
2051bfa316cSGreg Kurz         }
2061bfa316cSGreg Kurz     }
2071bfa316cSGreg Kurz 
2081bfa316cSGreg Kurz     return false;
2091bfa316cSGreg Kurz }
2101bfa316cSGreg Kurz 
2111bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
2121bfa316cSGreg Kurz {
2131bfa316cSGreg Kurz     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2141bfa316cSGreg Kurz     int queues = n->multiqueue ? n->max_queues : 1;
2151bfa316cSGreg Kurz 
2161bfa316cSGreg Kurz     if (virtio_net_started(n, status)) {
2171bfa316cSGreg Kurz         /* Before using the device, we tell the network backend about the
2181bfa316cSGreg Kurz          * endianness to use when parsing vnet headers. If the backend
2191bfa316cSGreg Kurz          * can't do it, we fallback onto fixing the headers in the core
2201bfa316cSGreg Kurz          * virtio-net code.
2211bfa316cSGreg Kurz          */
2221bfa316cSGreg Kurz         n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
2231bfa316cSGreg Kurz                                                             queues, true);
2241bfa316cSGreg Kurz     } else if (virtio_net_started(n, vdev->status)) {
2251bfa316cSGreg Kurz         /* After using the device, we need to reset the network backend to
2261bfa316cSGreg Kurz          * the default (guest native endianness), otherwise the guest may
2271bfa316cSGreg Kurz          * lose network connectivity if it is rebooted into a different
2281bfa316cSGreg Kurz          * endianness.
2291bfa316cSGreg Kurz          */
2301bfa316cSGreg Kurz         virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
2311bfa316cSGreg Kurz     }
2321bfa316cSGreg Kurz }
2331bfa316cSGreg Kurz 
234283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
235283e2c2aSYuri Benditovich {
236283e2c2aSYuri Benditovich     unsigned int dropped = virtqueue_drop_all(vq);
237283e2c2aSYuri Benditovich     if (dropped) {
238283e2c2aSYuri Benditovich         virtio_notify(vdev, vq);
239283e2c2aSYuri Benditovich     }
240283e2c2aSYuri Benditovich }
241283e2c2aSYuri Benditovich 
2426e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
2436e790746SPaolo Bonzini {
24417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
2456e790746SPaolo Bonzini     VirtIONetQueue *q;
2466e790746SPaolo Bonzini     int i;
2476e790746SPaolo Bonzini     uint8_t queue_status;
2486e790746SPaolo Bonzini 
2491bfa316cSGreg Kurz     virtio_net_vnet_endian_status(n, status);
2506e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
2516e790746SPaolo Bonzini 
2526e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
25338705bb5SFam Zheng         NetClientState *ncs = qemu_get_subqueue(n->nic, i);
25438705bb5SFam Zheng         bool queue_started;
2556e790746SPaolo Bonzini         q = &n->vqs[i];
2566e790746SPaolo Bonzini 
2576e790746SPaolo Bonzini         if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
2586e790746SPaolo Bonzini             queue_status = 0;
2596e790746SPaolo Bonzini         } else {
2606e790746SPaolo Bonzini             queue_status = status;
2616e790746SPaolo Bonzini         }
26238705bb5SFam Zheng         queue_started =
26338705bb5SFam Zheng             virtio_net_started(n, queue_status) && !n->vhost_started;
26438705bb5SFam Zheng 
26538705bb5SFam Zheng         if (queue_started) {
26638705bb5SFam Zheng             qemu_flush_queued_packets(ncs);
26738705bb5SFam Zheng         }
2686e790746SPaolo Bonzini 
2696e790746SPaolo Bonzini         if (!q->tx_waiting) {
2706e790746SPaolo Bonzini             continue;
2716e790746SPaolo Bonzini         }
2726e790746SPaolo Bonzini 
27338705bb5SFam Zheng         if (queue_started) {
2746e790746SPaolo Bonzini             if (q->tx_timer) {
275bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
276bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2776e790746SPaolo Bonzini             } else {
2786e790746SPaolo Bonzini                 qemu_bh_schedule(q->tx_bh);
2796e790746SPaolo Bonzini             }
2806e790746SPaolo Bonzini         } else {
2816e790746SPaolo Bonzini             if (q->tx_timer) {
282bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
2836e790746SPaolo Bonzini             } else {
2846e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
2856e790746SPaolo Bonzini             }
286283e2c2aSYuri Benditovich             if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
287283e2c2aSYuri Benditovich                 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK)) {
288283e2c2aSYuri Benditovich                 /* if tx is waiting we are likely have some packets in tx queue
289283e2c2aSYuri Benditovich                  * and disabled notification */
290283e2c2aSYuri Benditovich                 q->tx_waiting = 0;
291283e2c2aSYuri Benditovich                 virtio_queue_set_notification(q->tx_vq, 1);
292283e2c2aSYuri Benditovich                 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
293283e2c2aSYuri Benditovich             }
2946e790746SPaolo Bonzini         }
2956e790746SPaolo Bonzini     }
2966e790746SPaolo Bonzini }
2976e790746SPaolo Bonzini 
2986e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
2996e790746SPaolo Bonzini {
3006e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
30117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
3026e790746SPaolo Bonzini     uint16_t old_status = n->status;
3036e790746SPaolo Bonzini 
3046e790746SPaolo Bonzini     if (nc->link_down)
3056e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
3066e790746SPaolo Bonzini     else
3076e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
3086e790746SPaolo Bonzini 
3096e790746SPaolo Bonzini     if (n->status != old_status)
31017a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
3116e790746SPaolo Bonzini 
31217a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
3136e790746SPaolo Bonzini }
3146e790746SPaolo Bonzini 
315b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
316b1be4280SAmos Kong {
317b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
318b1be4280SAmos Kong 
319b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
32096e35046SAmos Kong         gchar *path = object_get_canonical_path(OBJECT(n->qdev));
32106150279SWenchao Xia         qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
32206150279SWenchao Xia                                               n->netclient_name, path, &error_abort);
32396e35046SAmos Kong         g_free(path);
324b1be4280SAmos Kong 
325b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
326b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
327b1be4280SAmos Kong     }
328b1be4280SAmos Kong }
329b1be4280SAmos Kong 
330f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
331f7bc8ef8SAmos Kong {
332f7bc8ef8SAmos Kong     intList *list, *entry;
333f7bc8ef8SAmos Kong     int i, j;
334f7bc8ef8SAmos Kong 
335f7bc8ef8SAmos Kong     list = NULL;
336f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
337f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
338f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
339f7bc8ef8SAmos Kong                 entry = g_malloc0(sizeof(*entry));
340f7bc8ef8SAmos Kong                 entry->value = (i << 5) + j;
341f7bc8ef8SAmos Kong                 entry->next = list;
342f7bc8ef8SAmos Kong                 list = entry;
343f7bc8ef8SAmos Kong             }
344f7bc8ef8SAmos Kong         }
345f7bc8ef8SAmos Kong     }
346f7bc8ef8SAmos Kong 
347f7bc8ef8SAmos Kong     return list;
348f7bc8ef8SAmos Kong }
349f7bc8ef8SAmos Kong 
350b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
351b1be4280SAmos Kong {
352b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
353f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
354b1be4280SAmos Kong     RxFilterInfo *info;
355b1be4280SAmos Kong     strList *str_list, *entry;
356f7bc8ef8SAmos Kong     int i;
357b1be4280SAmos Kong 
358b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
359b1be4280SAmos Kong     info->name = g_strdup(nc->name);
360b1be4280SAmos Kong     info->promiscuous = n->promisc;
361b1be4280SAmos Kong 
362b1be4280SAmos Kong     if (n->nouni) {
363b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
364b1be4280SAmos Kong     } else if (n->alluni) {
365b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
366b1be4280SAmos Kong     } else {
367b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
368b1be4280SAmos Kong     }
369b1be4280SAmos Kong 
370b1be4280SAmos Kong     if (n->nomulti) {
371b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
372b1be4280SAmos Kong     } else if (n->allmulti) {
373b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
374b1be4280SAmos Kong     } else {
375b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
376b1be4280SAmos Kong     }
377b1be4280SAmos Kong 
378b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
379b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
380b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
381b1be4280SAmos Kong 
382b0575ba4SScott Feldman     info->main_mac = qemu_mac_strdup_printf(n->mac);
383b1be4280SAmos Kong 
384b1be4280SAmos Kong     str_list = NULL;
385b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
386b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
387b0575ba4SScott Feldman         entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
388b1be4280SAmos Kong         entry->next = str_list;
389b1be4280SAmos Kong         str_list = entry;
390b1be4280SAmos Kong     }
391b1be4280SAmos Kong     info->unicast_table = str_list;
392b1be4280SAmos Kong 
393b1be4280SAmos Kong     str_list = NULL;
394b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
395b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
396b0575ba4SScott Feldman         entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
397b1be4280SAmos Kong         entry->next = str_list;
398b1be4280SAmos Kong         str_list = entry;
399b1be4280SAmos Kong     }
400b1be4280SAmos Kong     info->multicast_table = str_list;
401f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
402b1be4280SAmos Kong 
40395129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
404f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
405f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
406f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
407f7bc8ef8SAmos Kong     } else {
408f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
409b1be4280SAmos Kong     }
410b1be4280SAmos Kong 
411b1be4280SAmos Kong     /* enable event notification after query */
412b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
413b1be4280SAmos Kong 
414b1be4280SAmos Kong     return info;
415b1be4280SAmos Kong }
416b1be4280SAmos Kong 
4176e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev)
4186e790746SPaolo Bonzini {
41917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
4206e790746SPaolo Bonzini 
4216e790746SPaolo Bonzini     /* Reset back to compatibility mode */
4226e790746SPaolo Bonzini     n->promisc = 1;
4236e790746SPaolo Bonzini     n->allmulti = 0;
4246e790746SPaolo Bonzini     n->alluni = 0;
4256e790746SPaolo Bonzini     n->nomulti = 0;
4266e790746SPaolo Bonzini     n->nouni = 0;
4276e790746SPaolo Bonzini     n->nobcast = 0;
4286e790746SPaolo Bonzini     /* multiqueue is disabled by default */
4296e790746SPaolo Bonzini     n->curr_queues = 1;
430f57fcf70SJason Wang     timer_del(n->announce_timer);
431f57fcf70SJason Wang     n->announce_counter = 0;
432f57fcf70SJason Wang     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
4336e790746SPaolo Bonzini 
4346e790746SPaolo Bonzini     /* Flush any MAC and VLAN filter table state */
4356e790746SPaolo Bonzini     n->mac_table.in_use = 0;
4366e790746SPaolo Bonzini     n->mac_table.first_multi = 0;
4376e790746SPaolo Bonzini     n->mac_table.multi_overflow = 0;
4386e790746SPaolo Bonzini     n->mac_table.uni_overflow = 0;
4396e790746SPaolo Bonzini     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
4406e790746SPaolo Bonzini     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
441702d66a8SMichael S. Tsirkin     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
4426e790746SPaolo Bonzini     memset(n->vlans, 0, MAX_VLAN >> 3);
4436e790746SPaolo Bonzini }
4446e790746SPaolo Bonzini 
4456e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
4466e790746SPaolo Bonzini {
4476e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
4486e790746SPaolo Bonzini     if (!nc->peer) {
4496e790746SPaolo Bonzini         return;
4506e790746SPaolo Bonzini     }
4516e790746SPaolo Bonzini 
452d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
4536e790746SPaolo Bonzini }
4546e790746SPaolo Bonzini 
4556e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
4566e790746SPaolo Bonzini {
4576e790746SPaolo Bonzini     return n->has_vnet_hdr;
4586e790746SPaolo Bonzini }
4596e790746SPaolo Bonzini 
4606e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
4616e790746SPaolo Bonzini {
4626e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
4636e790746SPaolo Bonzini         return 0;
4646e790746SPaolo Bonzini 
465d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
4666e790746SPaolo Bonzini 
4676e790746SPaolo Bonzini     return n->has_ufo;
4686e790746SPaolo Bonzini }
4696e790746SPaolo Bonzini 
470bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
471bb9d17f8SCornelia Huck                                        int version_1)
4726e790746SPaolo Bonzini {
4736e790746SPaolo Bonzini     int i;
4746e790746SPaolo Bonzini     NetClientState *nc;
4756e790746SPaolo Bonzini 
4766e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
4776e790746SPaolo Bonzini 
478bb9d17f8SCornelia Huck     if (version_1) {
479bb9d17f8SCornelia Huck         n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
480bb9d17f8SCornelia Huck     } else {
4816e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
482bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
483bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
484bb9d17f8SCornelia Huck     }
4856e790746SPaolo Bonzini 
4866e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
4876e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
4886e790746SPaolo Bonzini 
4896e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
490d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
491d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
4926e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
4936e790746SPaolo Bonzini         }
4946e790746SPaolo Bonzini     }
4956e790746SPaolo Bonzini }
4966e790746SPaolo Bonzini 
4976e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
4986e790746SPaolo Bonzini {
4996e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
5006e790746SPaolo Bonzini 
5016e790746SPaolo Bonzini     if (!nc->peer) {
5026e790746SPaolo Bonzini         return 0;
5036e790746SPaolo Bonzini     }
5046e790746SPaolo Bonzini 
505f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
5067263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 1);
5077263a0adSChangchun Ouyang     }
5087263a0adSChangchun Ouyang 
509f394b2e2SEric Blake     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
5106e790746SPaolo Bonzini         return 0;
5116e790746SPaolo Bonzini     }
5126e790746SPaolo Bonzini 
5136e790746SPaolo Bonzini     return tap_enable(nc->peer);
5146e790746SPaolo Bonzini }
5156e790746SPaolo Bonzini 
5166e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
5176e790746SPaolo Bonzini {
5186e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
5196e790746SPaolo Bonzini 
5206e790746SPaolo Bonzini     if (!nc->peer) {
5216e790746SPaolo Bonzini         return 0;
5226e790746SPaolo Bonzini     }
5236e790746SPaolo Bonzini 
524f394b2e2SEric Blake     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
5257263a0adSChangchun Ouyang         vhost_set_vring_enable(nc->peer, 0);
5267263a0adSChangchun Ouyang     }
5277263a0adSChangchun Ouyang 
528f394b2e2SEric Blake     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
5296e790746SPaolo Bonzini         return 0;
5306e790746SPaolo Bonzini     }
5316e790746SPaolo Bonzini 
5326e790746SPaolo Bonzini     return tap_disable(nc->peer);
5336e790746SPaolo Bonzini }
5346e790746SPaolo Bonzini 
5356e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n)
5366e790746SPaolo Bonzini {
5376e790746SPaolo Bonzini     int i;
538ddfa83eaSJoel Stanley     int r;
5396e790746SPaolo Bonzini 
54068b5f314SYuri Benditovich     if (n->nic->peer_deleted) {
54168b5f314SYuri Benditovich         return;
54268b5f314SYuri Benditovich     }
54368b5f314SYuri Benditovich 
5446e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
5456e790746SPaolo Bonzini         if (i < n->curr_queues) {
546ddfa83eaSJoel Stanley             r = peer_attach(n, i);
547ddfa83eaSJoel Stanley             assert(!r);
5486e790746SPaolo Bonzini         } else {
549ddfa83eaSJoel Stanley             r = peer_detach(n, i);
550ddfa83eaSJoel Stanley             assert(!r);
5516e790746SPaolo Bonzini         }
5526e790746SPaolo Bonzini     }
5536e790746SPaolo Bonzini }
5546e790746SPaolo Bonzini 
555ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
5566e790746SPaolo Bonzini 
5579d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
5589d5b731dSJason Wang                                         Error **errp)
5596e790746SPaolo Bonzini {
56017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
5616e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
5626e790746SPaolo Bonzini 
563da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
564da3e8a23SShannon Zhao     features |= n->host_features;
565da3e8a23SShannon Zhao 
5660cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
5676e790746SPaolo Bonzini 
5686e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
5690cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
5700cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
5710cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
5720cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
5736e790746SPaolo Bonzini 
5740cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
5750cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
5760cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
5770cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
5786e790746SPaolo Bonzini     }
5796e790746SPaolo Bonzini 
5806e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
5810cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
5820cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
5836e790746SPaolo Bonzini     }
5846e790746SPaolo Bonzini 
585ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
5866e790746SPaolo Bonzini         return features;
5876e790746SPaolo Bonzini     }
588ed8b4afeSNikolay Nikolaev     return vhost_net_get_features(get_vhost_net(nc->peer), features);
5896e790746SPaolo Bonzini }
5906e790746SPaolo Bonzini 
591019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
5926e790746SPaolo Bonzini {
593019a3edbSGerd Hoffmann     uint64_t features = 0;
5946e790746SPaolo Bonzini 
5956e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
5966e790746SPaolo Bonzini      * but also these: */
5970cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
5980cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
5990cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
6000cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
6010cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
6026e790746SPaolo Bonzini 
6036e790746SPaolo Bonzini     return features;
6046e790746SPaolo Bonzini }
6056e790746SPaolo Bonzini 
606644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
607644c9858SDmitry Fleytman {
608ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
609644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
610644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
611644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
612644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
613644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
614644c9858SDmitry Fleytman }
615644c9858SDmitry Fleytman 
616644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
617644c9858SDmitry Fleytman {
618644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
619644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
620644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
621644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
622644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
623644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
624644c9858SDmitry Fleytman 
625644c9858SDmitry Fleytman     return guest_offloads_mask & features;
626644c9858SDmitry Fleytman }
627644c9858SDmitry Fleytman 
628644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
629644c9858SDmitry Fleytman {
630644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
631644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
632644c9858SDmitry Fleytman }
633644c9858SDmitry Fleytman 
634d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
6356e790746SPaolo Bonzini {
63617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
6376e790746SPaolo Bonzini     int i;
6386e790746SPaolo Bonzini 
639ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
64095129d6fSCornelia Huck                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
6416e790746SPaolo Bonzini 
642ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
64395129d6fSCornelia Huck                                virtio_has_feature(features,
644bb9d17f8SCornelia Huck                                                   VIRTIO_NET_F_MRG_RXBUF),
64595129d6fSCornelia Huck                                virtio_has_feature(features,
646bb9d17f8SCornelia Huck                                                   VIRTIO_F_VERSION_1));
6476e790746SPaolo Bonzini 
6486e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
649644c9858SDmitry Fleytman         n->curr_guest_offloads =
650644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
651644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
6526e790746SPaolo Bonzini     }
6536e790746SPaolo Bonzini 
6546e790746SPaolo Bonzini     for (i = 0;  i < n->max_queues; i++) {
6556e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
6566e790746SPaolo Bonzini 
657ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
6586e790746SPaolo Bonzini             continue;
6596e790746SPaolo Bonzini         }
660ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
6616e790746SPaolo Bonzini     }
6620b1eaa88SStefan Fritsch 
66395129d6fSCornelia Huck     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
6640b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
6650b1eaa88SStefan Fritsch     } else {
6660b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
6670b1eaa88SStefan Fritsch     }
6686e790746SPaolo Bonzini }
6696e790746SPaolo Bonzini 
6706e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
6716e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
6726e790746SPaolo Bonzini {
6736e790746SPaolo Bonzini     uint8_t on;
6746e790746SPaolo Bonzini     size_t s;
675b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
6766e790746SPaolo Bonzini 
6776e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
6786e790746SPaolo Bonzini     if (s != sizeof(on)) {
6796e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
6806e790746SPaolo Bonzini     }
6816e790746SPaolo Bonzini 
6826e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
6836e790746SPaolo Bonzini         n->promisc = on;
6846e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
6856e790746SPaolo Bonzini         n->allmulti = on;
6866e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
6876e790746SPaolo Bonzini         n->alluni = on;
6886e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
6896e790746SPaolo Bonzini         n->nomulti = on;
6906e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
6916e790746SPaolo Bonzini         n->nouni = on;
6926e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
6936e790746SPaolo Bonzini         n->nobcast = on;
6946e790746SPaolo Bonzini     } else {
6956e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
6966e790746SPaolo Bonzini     }
6976e790746SPaolo Bonzini 
698b1be4280SAmos Kong     rxfilter_notify(nc);
699b1be4280SAmos Kong 
7006e790746SPaolo Bonzini     return VIRTIO_NET_OK;
7016e790746SPaolo Bonzini }
7026e790746SPaolo Bonzini 
703644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
704644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
705644c9858SDmitry Fleytman {
706644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
707644c9858SDmitry Fleytman     uint64_t offloads;
708644c9858SDmitry Fleytman     size_t s;
709644c9858SDmitry Fleytman 
71095129d6fSCornelia Huck     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
711644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
712644c9858SDmitry Fleytman     }
713644c9858SDmitry Fleytman 
714644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
715644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
716644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
717644c9858SDmitry Fleytman     }
718644c9858SDmitry Fleytman 
719644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
720644c9858SDmitry Fleytman         uint64_t supported_offloads;
721644c9858SDmitry Fleytman 
722644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
723644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
724644c9858SDmitry Fleytman         }
725644c9858SDmitry Fleytman 
726644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
727644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
728644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
729644c9858SDmitry Fleytman         }
730644c9858SDmitry Fleytman 
731644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
732644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
733644c9858SDmitry Fleytman 
734644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
735644c9858SDmitry Fleytman     } else {
736644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
737644c9858SDmitry Fleytman     }
738644c9858SDmitry Fleytman }
739644c9858SDmitry Fleytman 
7406e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
7416e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
7426e790746SPaolo Bonzini {
7431399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
7446e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
7456e790746SPaolo Bonzini     size_t s;
746b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
7476e790746SPaolo Bonzini 
7486e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
7496e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
7506e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
7516e790746SPaolo Bonzini         }
7526e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
7536e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
7546e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
755b1be4280SAmos Kong         rxfilter_notify(nc);
756b1be4280SAmos Kong 
7576e790746SPaolo Bonzini         return VIRTIO_NET_OK;
7586e790746SPaolo Bonzini     }
7596e790746SPaolo Bonzini 
7606e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
7616e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7626e790746SPaolo Bonzini     }
7636e790746SPaolo Bonzini 
764cae2e556SAmos Kong     int in_use = 0;
765cae2e556SAmos Kong     int first_multi = 0;
766cae2e556SAmos Kong     uint8_t uni_overflow = 0;
767cae2e556SAmos Kong     uint8_t multi_overflow = 0;
768cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
7696e790746SPaolo Bonzini 
7706e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
7716e790746SPaolo Bonzini                    sizeof(mac_data.entries));
7721399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
7736e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
774b1be4280SAmos Kong         goto error;
7756e790746SPaolo Bonzini     }
7766e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
7776e790746SPaolo Bonzini 
7786e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
779b1be4280SAmos Kong         goto error;
7806e790746SPaolo Bonzini     }
7816e790746SPaolo Bonzini 
7826e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
783cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
7846e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
7856e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
786b1be4280SAmos Kong             goto error;
7876e790746SPaolo Bonzini         }
788cae2e556SAmos Kong         in_use += mac_data.entries;
7896e790746SPaolo Bonzini     } else {
790cae2e556SAmos Kong         uni_overflow = 1;
7916e790746SPaolo Bonzini     }
7926e790746SPaolo Bonzini 
7936e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
7946e790746SPaolo Bonzini 
795cae2e556SAmos Kong     first_multi = in_use;
7966e790746SPaolo Bonzini 
7976e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
7986e790746SPaolo Bonzini                    sizeof(mac_data.entries));
7991399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
8006e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
801b1be4280SAmos Kong         goto error;
8026e790746SPaolo Bonzini     }
8036e790746SPaolo Bonzini 
8046e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
8056e790746SPaolo Bonzini 
8066e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
807b1be4280SAmos Kong         goto error;
8086e790746SPaolo Bonzini     }
8096e790746SPaolo Bonzini 
810edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
811cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
8126e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
8136e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
814b1be4280SAmos Kong             goto error;
8156e790746SPaolo Bonzini         }
816cae2e556SAmos Kong         in_use += mac_data.entries;
8176e790746SPaolo Bonzini     } else {
818cae2e556SAmos Kong         multi_overflow = 1;
8196e790746SPaolo Bonzini     }
8206e790746SPaolo Bonzini 
821cae2e556SAmos Kong     n->mac_table.in_use = in_use;
822cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
823cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
824cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
825cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
826cae2e556SAmos Kong     g_free(macs);
827b1be4280SAmos Kong     rxfilter_notify(nc);
828b1be4280SAmos Kong 
8296e790746SPaolo Bonzini     return VIRTIO_NET_OK;
830b1be4280SAmos Kong 
831b1be4280SAmos Kong error:
832cae2e556SAmos Kong     g_free(macs);
833b1be4280SAmos Kong     return VIRTIO_NET_ERR;
8346e790746SPaolo Bonzini }
8356e790746SPaolo Bonzini 
8366e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
8376e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
8386e790746SPaolo Bonzini {
8391399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
8406e790746SPaolo Bonzini     uint16_t vid;
8416e790746SPaolo Bonzini     size_t s;
842b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
8436e790746SPaolo Bonzini 
8446e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
8451399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
8466e790746SPaolo Bonzini     if (s != sizeof(vid)) {
8476e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
8486e790746SPaolo Bonzini     }
8496e790746SPaolo Bonzini 
8506e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
8516e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
8526e790746SPaolo Bonzini 
8536e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
8546e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
8556e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
8566e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
8576e790746SPaolo Bonzini     else
8586e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
8596e790746SPaolo Bonzini 
860b1be4280SAmos Kong     rxfilter_notify(nc);
861b1be4280SAmos Kong 
8626e790746SPaolo Bonzini     return VIRTIO_NET_OK;
8636e790746SPaolo Bonzini }
8646e790746SPaolo Bonzini 
865f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
866f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
867f57fcf70SJason Wang {
868f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
869f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
870f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
871f57fcf70SJason Wang         if (n->announce_counter) {
872f57fcf70SJason Wang             timer_mod(n->announce_timer,
873f57fcf70SJason Wang                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
874f57fcf70SJason Wang                       self_announce_delay(n->announce_counter));
875f57fcf70SJason Wang         }
876f57fcf70SJason Wang         return VIRTIO_NET_OK;
877f57fcf70SJason Wang     } else {
878f57fcf70SJason Wang         return VIRTIO_NET_ERR;
879f57fcf70SJason Wang     }
880f57fcf70SJason Wang }
881f57fcf70SJason Wang 
8826e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
8836e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
8846e790746SPaolo Bonzini {
88517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
8866e790746SPaolo Bonzini     struct virtio_net_ctrl_mq mq;
8876e790746SPaolo Bonzini     size_t s;
8886e790746SPaolo Bonzini     uint16_t queues;
8896e790746SPaolo Bonzini 
8906e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
8916e790746SPaolo Bonzini     if (s != sizeof(mq)) {
8926e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
8936e790746SPaolo Bonzini     }
8946e790746SPaolo Bonzini 
8956e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
8966e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
8976e790746SPaolo Bonzini     }
8986e790746SPaolo Bonzini 
8991399c60dSRusty Russell     queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
9006e790746SPaolo Bonzini 
9016e790746SPaolo Bonzini     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
9026e790746SPaolo Bonzini         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
9036e790746SPaolo Bonzini         queues > n->max_queues ||
9046e790746SPaolo Bonzini         !n->multiqueue) {
9056e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
9066e790746SPaolo Bonzini     }
9076e790746SPaolo Bonzini 
9086e790746SPaolo Bonzini     n->curr_queues = queues;
9096e790746SPaolo Bonzini     /* stop the backend before changing the number of queues to avoid handling a
9106e790746SPaolo Bonzini      * disabled queue */
91117a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
9126e790746SPaolo Bonzini     virtio_net_set_queues(n);
9136e790746SPaolo Bonzini 
9146e790746SPaolo Bonzini     return VIRTIO_NET_OK;
9156e790746SPaolo Bonzini }
916ba7eadb5SGreg Kurz 
9176e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
9186e790746SPaolo Bonzini {
91917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
9206e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
9216e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
92251b19ebeSPaolo Bonzini     VirtQueueElement *elem;
9236e790746SPaolo Bonzini     size_t s;
924771b6ed3SJason Wang     struct iovec *iov, *iov2;
9256e790746SPaolo Bonzini     unsigned int iov_cnt;
9266e790746SPaolo Bonzini 
92751b19ebeSPaolo Bonzini     for (;;) {
92851b19ebeSPaolo Bonzini         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
92951b19ebeSPaolo Bonzini         if (!elem) {
93051b19ebeSPaolo Bonzini             break;
93151b19ebeSPaolo Bonzini         }
93251b19ebeSPaolo Bonzini         if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
93351b19ebeSPaolo Bonzini             iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
934ba7eadb5SGreg Kurz             virtio_error(vdev, "virtio-net ctrl missing headers");
935ba7eadb5SGreg Kurz             virtqueue_detach_element(vq, elem, 0);
936ba7eadb5SGreg Kurz             g_free(elem);
937ba7eadb5SGreg Kurz             break;
9386e790746SPaolo Bonzini         }
9396e790746SPaolo Bonzini 
94051b19ebeSPaolo Bonzini         iov_cnt = elem->out_num;
94151b19ebeSPaolo Bonzini         iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
9426e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
9436e790746SPaolo Bonzini         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
9446e790746SPaolo Bonzini         if (s != sizeof(ctrl)) {
9456e790746SPaolo Bonzini             status = VIRTIO_NET_ERR;
9466e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
9476e790746SPaolo Bonzini             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
9486e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
9496e790746SPaolo Bonzini             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
9506e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
9516e790746SPaolo Bonzini             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
952f57fcf70SJason Wang         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
953f57fcf70SJason Wang             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
9546e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
9556e790746SPaolo Bonzini             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
956644c9858SDmitry Fleytman         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
957644c9858SDmitry Fleytman             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
9586e790746SPaolo Bonzini         }
9596e790746SPaolo Bonzini 
96051b19ebeSPaolo Bonzini         s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
9616e790746SPaolo Bonzini         assert(s == sizeof(status));
9626e790746SPaolo Bonzini 
96351b19ebeSPaolo Bonzini         virtqueue_push(vq, elem, sizeof(status));
9646e790746SPaolo Bonzini         virtio_notify(vdev, vq);
965771b6ed3SJason Wang         g_free(iov2);
96651b19ebeSPaolo Bonzini         g_free(elem);
9676e790746SPaolo Bonzini     }
9686e790746SPaolo Bonzini }
9696e790746SPaolo Bonzini 
9706e790746SPaolo Bonzini /* RX */
9716e790746SPaolo Bonzini 
9726e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
9736e790746SPaolo Bonzini {
97417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
9756e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
9766e790746SPaolo Bonzini 
9776e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
9786e790746SPaolo Bonzini }
9796e790746SPaolo Bonzini 
9806e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc)
9816e790746SPaolo Bonzini {
9826e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
98317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
9846e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
9856e790746SPaolo Bonzini 
98617a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
9876e790746SPaolo Bonzini         return 0;
9886e790746SPaolo Bonzini     }
9896e790746SPaolo Bonzini 
9906e790746SPaolo Bonzini     if (nc->queue_index >= n->curr_queues) {
9916e790746SPaolo Bonzini         return 0;
9926e790746SPaolo Bonzini     }
9936e790746SPaolo Bonzini 
9946e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
99517a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
9966e790746SPaolo Bonzini         return 0;
9976e790746SPaolo Bonzini     }
9986e790746SPaolo Bonzini 
9996e790746SPaolo Bonzini     return 1;
10006e790746SPaolo Bonzini }
10016e790746SPaolo Bonzini 
10026e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
10036e790746SPaolo Bonzini {
10046e790746SPaolo Bonzini     VirtIONet *n = q->n;
10056e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
10066e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
10076e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
10086e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
10096e790746SPaolo Bonzini 
10106e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
10116e790746SPaolo Bonzini          * available after the above check but before notification was
10126e790746SPaolo Bonzini          * enabled, check for available buffers again.
10136e790746SPaolo Bonzini          */
10146e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
10156e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
10166e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
10176e790746SPaolo Bonzini             return 0;
10186e790746SPaolo Bonzini         }
10196e790746SPaolo Bonzini     }
10206e790746SPaolo Bonzini 
10216e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
10226e790746SPaolo Bonzini     return 1;
10236e790746SPaolo Bonzini }
10246e790746SPaolo Bonzini 
10251399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1026032a74a1SCédric Le Goater {
10271399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
10281399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
10291399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
10301399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
1031032a74a1SCédric Le Goater }
1032032a74a1SCédric Le Goater 
10336e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
10346e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
10356e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
10366e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
10376e790746SPaolo Bonzini  * dhclient yet.
10386e790746SPaolo Bonzini  *
10396e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
10406e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
10416e790746SPaolo Bonzini  * kernels.
10426e790746SPaolo Bonzini  *
10436e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
10446e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
10456e790746SPaolo Bonzini  * cache.
10466e790746SPaolo Bonzini  */
10476e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
10486e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
10496e790746SPaolo Bonzini {
10506e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
10516e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
10526e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
10536e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
10546e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
10556e790746SPaolo Bonzini         net_checksum_calculate(buf, size);
10566e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
10576e790746SPaolo Bonzini     }
10586e790746SPaolo Bonzini }
10596e790746SPaolo Bonzini 
10606e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
10616e790746SPaolo Bonzini                            const void *buf, size_t size)
10626e790746SPaolo Bonzini {
10636e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
10646e790746SPaolo Bonzini         /* FIXME this cast is evil */
10656e790746SPaolo Bonzini         void *wbuf = (void *)buf;
10666e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
10676e790746SPaolo Bonzini                                     size - n->host_hdr_len);
10681bfa316cSGreg Kurz 
10691bfa316cSGreg Kurz         if (n->needs_vnet_hdr_swap) {
10701399c60dSRusty Russell             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
10711bfa316cSGreg Kurz         }
10726e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
10736e790746SPaolo Bonzini     } else {
10746e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
10756e790746SPaolo Bonzini             .flags = 0,
10766e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
10776e790746SPaolo Bonzini         };
10786e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
10796e790746SPaolo Bonzini     }
10806e790746SPaolo Bonzini }
10816e790746SPaolo Bonzini 
10826e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
10836e790746SPaolo Bonzini {
10846e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
10856e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
10866e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
10876e790746SPaolo Bonzini     int i;
10886e790746SPaolo Bonzini 
10896e790746SPaolo Bonzini     if (n->promisc)
10906e790746SPaolo Bonzini         return 1;
10916e790746SPaolo Bonzini 
10926e790746SPaolo Bonzini     ptr += n->host_hdr_len;
10936e790746SPaolo Bonzini 
10946e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
10957542d3e7SPeter Maydell         int vid = lduw_be_p(ptr + 14) & 0xfff;
10966e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
10976e790746SPaolo Bonzini             return 0;
10986e790746SPaolo Bonzini     }
10996e790746SPaolo Bonzini 
11006e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
11016e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
11026e790746SPaolo Bonzini             return !n->nobcast;
11036e790746SPaolo Bonzini         } else if (n->nomulti) {
11046e790746SPaolo Bonzini             return 0;
11056e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
11066e790746SPaolo Bonzini             return 1;
11076e790746SPaolo Bonzini         }
11086e790746SPaolo Bonzini 
11096e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
11106e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
11116e790746SPaolo Bonzini                 return 1;
11126e790746SPaolo Bonzini             }
11136e790746SPaolo Bonzini         }
11146e790746SPaolo Bonzini     } else { // unicast
11156e790746SPaolo Bonzini         if (n->nouni) {
11166e790746SPaolo Bonzini             return 0;
11176e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
11186e790746SPaolo Bonzini             return 1;
11196e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
11206e790746SPaolo Bonzini             return 1;
11216e790746SPaolo Bonzini         }
11226e790746SPaolo Bonzini 
11236e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
11246e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
11256e790746SPaolo Bonzini                 return 1;
11266e790746SPaolo Bonzini             }
11276e790746SPaolo Bonzini         }
11286e790746SPaolo Bonzini     }
11296e790746SPaolo Bonzini 
11306e790746SPaolo Bonzini     return 0;
11316e790746SPaolo Bonzini }
11326e790746SPaolo Bonzini 
1133*97cd965cSPaolo Bonzini static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1134*97cd965cSPaolo Bonzini                                       size_t size)
11356e790746SPaolo Bonzini {
11366e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
11376e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
113817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11396e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
11406e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
11416e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
11426e790746SPaolo Bonzini     size_t offset, i, guest_offset;
11436e790746SPaolo Bonzini 
11446e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
11456e790746SPaolo Bonzini         return -1;
11466e790746SPaolo Bonzini     }
11476e790746SPaolo Bonzini 
11486e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
11496e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
11506e790746SPaolo Bonzini         return 0;
11516e790746SPaolo Bonzini     }
11526e790746SPaolo Bonzini 
11536e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
11546e790746SPaolo Bonzini         return size;
11556e790746SPaolo Bonzini 
11566e790746SPaolo Bonzini     offset = i = 0;
11576e790746SPaolo Bonzini 
11586e790746SPaolo Bonzini     while (offset < size) {
115951b19ebeSPaolo Bonzini         VirtQueueElement *elem;
11606e790746SPaolo Bonzini         int len, total;
116151b19ebeSPaolo Bonzini         const struct iovec *sg;
11626e790746SPaolo Bonzini 
11636e790746SPaolo Bonzini         total = 0;
11646e790746SPaolo Bonzini 
116551b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
116651b19ebeSPaolo Bonzini         if (!elem) {
1167ba10b9c0SGreg Kurz             if (i) {
1168ba10b9c0SGreg Kurz                 virtio_error(vdev, "virtio-net unexpected empty queue: "
11696e790746SPaolo Bonzini                              "i %zd mergeable %d offset %zd, size %zd, "
1170019a3edbSGerd Hoffmann                              "guest hdr len %zd, host hdr len %zd "
1171019a3edbSGerd Hoffmann                              "guest features 0x%" PRIx64,
11726e790746SPaolo Bonzini                              i, n->mergeable_rx_bufs, offset, size,
1173019a3edbSGerd Hoffmann                              n->guest_hdr_len, n->host_hdr_len,
1174019a3edbSGerd Hoffmann                              vdev->guest_features);
1175ba10b9c0SGreg Kurz             }
1176ba10b9c0SGreg Kurz             return -1;
11776e790746SPaolo Bonzini         }
11786e790746SPaolo Bonzini 
117951b19ebeSPaolo Bonzini         if (elem->in_num < 1) {
1180ba10b9c0SGreg Kurz             virtio_error(vdev,
1181ba10b9c0SGreg Kurz                          "virtio-net receive queue contains no in buffers");
1182ba10b9c0SGreg Kurz             virtqueue_detach_element(q->rx_vq, elem, 0);
1183ba10b9c0SGreg Kurz             g_free(elem);
1184ba10b9c0SGreg Kurz             return -1;
11856e790746SPaolo Bonzini         }
11866e790746SPaolo Bonzini 
118751b19ebeSPaolo Bonzini         sg = elem->in_sg;
11886e790746SPaolo Bonzini         if (i == 0) {
11896e790746SPaolo Bonzini             assert(offset == 0);
11906e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
11916e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
119251b19ebeSPaolo Bonzini                                     sg, elem->in_num,
11936e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
11946e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
11956e790746SPaolo Bonzini             }
11966e790746SPaolo Bonzini 
119751b19ebeSPaolo Bonzini             receive_header(n, sg, elem->in_num, buf, size);
11986e790746SPaolo Bonzini             offset = n->host_hdr_len;
11996e790746SPaolo Bonzini             total += n->guest_hdr_len;
12006e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
12016e790746SPaolo Bonzini         } else {
12026e790746SPaolo Bonzini             guest_offset = 0;
12036e790746SPaolo Bonzini         }
12046e790746SPaolo Bonzini 
12056e790746SPaolo Bonzini         /* copy in packet.  ugh */
120651b19ebeSPaolo Bonzini         len = iov_from_buf(sg, elem->in_num, guest_offset,
12076e790746SPaolo Bonzini                            buf + offset, size - offset);
12086e790746SPaolo Bonzini         total += len;
12096e790746SPaolo Bonzini         offset += len;
12106e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
12116e790746SPaolo Bonzini          * must have consumed the complete packet.
12126e790746SPaolo Bonzini          * Otherwise, drop it. */
12136e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
121427e57efeSLadi Prosek             virtqueue_unpop(q->rx_vq, elem, total);
121551b19ebeSPaolo Bonzini             g_free(elem);
12166e790746SPaolo Bonzini             return size;
12176e790746SPaolo Bonzini         }
12186e790746SPaolo Bonzini 
12196e790746SPaolo Bonzini         /* signal other side */
122051b19ebeSPaolo Bonzini         virtqueue_fill(q->rx_vq, elem, total, i++);
122151b19ebeSPaolo Bonzini         g_free(elem);
12226e790746SPaolo Bonzini     }
12236e790746SPaolo Bonzini 
12246e790746SPaolo Bonzini     if (mhdr_cnt) {
12251399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
12266e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
12276e790746SPaolo Bonzini                      0,
12286e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
12296e790746SPaolo Bonzini     }
12306e790746SPaolo Bonzini 
12316e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
123217a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
12336e790746SPaolo Bonzini 
12346e790746SPaolo Bonzini     return size;
12356e790746SPaolo Bonzini }
12366e790746SPaolo Bonzini 
1237*97cd965cSPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
1238*97cd965cSPaolo Bonzini                                   size_t size)
1239*97cd965cSPaolo Bonzini {
1240*97cd965cSPaolo Bonzini     ssize_t r;
1241*97cd965cSPaolo Bonzini 
1242*97cd965cSPaolo Bonzini     rcu_read_lock();
1243*97cd965cSPaolo Bonzini     r = virtio_net_receive_rcu(nc, buf, size);
1244*97cd965cSPaolo Bonzini     rcu_read_unlock();
1245*97cd965cSPaolo Bonzini     return r;
1246*97cd965cSPaolo Bonzini }
1247*97cd965cSPaolo Bonzini 
12486e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
12496e790746SPaolo Bonzini 
12506e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
12516e790746SPaolo Bonzini {
12526e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
12536e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
125417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12556e790746SPaolo Bonzini 
125651b19ebeSPaolo Bonzini     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
125717a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
12586e790746SPaolo Bonzini 
125951b19ebeSPaolo Bonzini     g_free(q->async_tx.elem);
126051b19ebeSPaolo Bonzini     q->async_tx.elem = NULL;
12616e790746SPaolo Bonzini 
12626e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
12636e790746SPaolo Bonzini     virtio_net_flush_tx(q);
12646e790746SPaolo Bonzini }
12656e790746SPaolo Bonzini 
12666e790746SPaolo Bonzini /* TX */
12676e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
12686e790746SPaolo Bonzini {
12696e790746SPaolo Bonzini     VirtIONet *n = q->n;
127017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
127151b19ebeSPaolo Bonzini     VirtQueueElement *elem;
12726e790746SPaolo Bonzini     int32_t num_packets = 0;
12736e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
127417a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
12756e790746SPaolo Bonzini         return num_packets;
12766e790746SPaolo Bonzini     }
12776e790746SPaolo Bonzini 
127851b19ebeSPaolo Bonzini     if (q->async_tx.elem) {
12796e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
12806e790746SPaolo Bonzini         return num_packets;
12816e790746SPaolo Bonzini     }
12826e790746SPaolo Bonzini 
128351b19ebeSPaolo Bonzini     for (;;) {
1284bd89dd98SJason Wang         ssize_t ret;
128551b19ebeSPaolo Bonzini         unsigned int out_num;
128651b19ebeSPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
1287feb93f36SJason Wang         struct virtio_net_hdr_mrg_rxbuf mhdr;
12886e790746SPaolo Bonzini 
128951b19ebeSPaolo Bonzini         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
129051b19ebeSPaolo Bonzini         if (!elem) {
129151b19ebeSPaolo Bonzini             break;
129251b19ebeSPaolo Bonzini         }
129351b19ebeSPaolo Bonzini 
129451b19ebeSPaolo Bonzini         out_num = elem->out_num;
129551b19ebeSPaolo Bonzini         out_sg = elem->out_sg;
12966e790746SPaolo Bonzini         if (out_num < 1) {
1297fa5e56c2SGreg Kurz             virtio_error(vdev, "virtio-net header not in first element");
1298fa5e56c2SGreg Kurz             virtqueue_detach_element(q->tx_vq, elem, 0);
1299fa5e56c2SGreg Kurz             g_free(elem);
1300fa5e56c2SGreg Kurz             return -EINVAL;
13016e790746SPaolo Bonzini         }
13026e790746SPaolo Bonzini 
1303032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
1304feb93f36SJason Wang             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
1305feb93f36SJason Wang                 n->guest_hdr_len) {
1306fa5e56c2SGreg Kurz                 virtio_error(vdev, "virtio-net header incorrect");
1307fa5e56c2SGreg Kurz                 virtqueue_detach_element(q->tx_vq, elem, 0);
1308fa5e56c2SGreg Kurz                 g_free(elem);
1309fa5e56c2SGreg Kurz                 return -EINVAL;
1310032a74a1SCédric Le Goater             }
13111bfa316cSGreg Kurz             if (n->needs_vnet_hdr_swap) {
1312feb93f36SJason Wang                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
1313feb93f36SJason Wang                 sg2[0].iov_base = &mhdr;
1314feb93f36SJason Wang                 sg2[0].iov_len = n->guest_hdr_len;
1315feb93f36SJason Wang                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
1316feb93f36SJason Wang                                    out_sg, out_num,
1317feb93f36SJason Wang                                    n->guest_hdr_len, -1);
1318feb93f36SJason Wang                 if (out_num == VIRTQUEUE_MAX_SIZE) {
1319feb93f36SJason Wang                     goto drop;
1320032a74a1SCédric Le Goater 		}
1321feb93f36SJason Wang                 out_num += 1;
1322feb93f36SJason Wang                 out_sg = sg2;
1323feb93f36SJason Wang 	    }
1324feb93f36SJason Wang         }
13256e790746SPaolo Bonzini         /*
13266e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
13276e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
13286e790746SPaolo Bonzini          * that host is interested in.
13296e790746SPaolo Bonzini          */
13306e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
13316e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
13326e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
13336e790746SPaolo Bonzini                                        out_sg, out_num,
13346e790746SPaolo Bonzini                                        0, n->host_hdr_len);
13356e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
13366e790746SPaolo Bonzini                              out_sg, out_num,
13376e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
13386e790746SPaolo Bonzini             out_num = sg_num;
13396e790746SPaolo Bonzini             out_sg = sg;
13406e790746SPaolo Bonzini         }
13416e790746SPaolo Bonzini 
13426e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
13436e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
13446e790746SPaolo Bonzini         if (ret == 0) {
13456e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
13466e790746SPaolo Bonzini             q->async_tx.elem = elem;
13476e790746SPaolo Bonzini             return -EBUSY;
13486e790746SPaolo Bonzini         }
13496e790746SPaolo Bonzini 
1350feb93f36SJason Wang drop:
135151b19ebeSPaolo Bonzini         virtqueue_push(q->tx_vq, elem, 0);
135217a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
135351b19ebeSPaolo Bonzini         g_free(elem);
13546e790746SPaolo Bonzini 
13556e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
13566e790746SPaolo Bonzini             break;
13576e790746SPaolo Bonzini         }
13586e790746SPaolo Bonzini     }
13596e790746SPaolo Bonzini     return num_packets;
13606e790746SPaolo Bonzini }
13616e790746SPaolo Bonzini 
13626e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
13636e790746SPaolo Bonzini {
136417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
13656e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
13666e790746SPaolo Bonzini 
1367283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
1368283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
1369283e2c2aSYuri Benditovich         return;
1370283e2c2aSYuri Benditovich     }
1371283e2c2aSYuri Benditovich 
13726e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
137317a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
13746e790746SPaolo Bonzini         q->tx_waiting = 1;
13756e790746SPaolo Bonzini         return;
13766e790746SPaolo Bonzini     }
13776e790746SPaolo Bonzini 
13786e790746SPaolo Bonzini     if (q->tx_waiting) {
13796e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 1);
1380bc72ad67SAlex Bligh         timer_del(q->tx_timer);
13816e790746SPaolo Bonzini         q->tx_waiting = 0;
1382fa5e56c2SGreg Kurz         if (virtio_net_flush_tx(q) == -EINVAL) {
1383fa5e56c2SGreg Kurz             return;
1384fa5e56c2SGreg Kurz         }
13856e790746SPaolo Bonzini     } else {
1386bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
1387bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
13886e790746SPaolo Bonzini         q->tx_waiting = 1;
13896e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
13906e790746SPaolo Bonzini     }
13916e790746SPaolo Bonzini }
13926e790746SPaolo Bonzini 
13936e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
13946e790746SPaolo Bonzini {
139517a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
13966e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
13976e790746SPaolo Bonzini 
1398283e2c2aSYuri Benditovich     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
1399283e2c2aSYuri Benditovich         virtio_net_drop_tx_queue_data(vdev, vq);
1400283e2c2aSYuri Benditovich         return;
1401283e2c2aSYuri Benditovich     }
1402283e2c2aSYuri Benditovich 
14036e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
14046e790746SPaolo Bonzini         return;
14056e790746SPaolo Bonzini     }
14066e790746SPaolo Bonzini     q->tx_waiting = 1;
14076e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
140817a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
14096e790746SPaolo Bonzini         return;
14106e790746SPaolo Bonzini     }
14116e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
14126e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
14136e790746SPaolo Bonzini }
14146e790746SPaolo Bonzini 
14156e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
14166e790746SPaolo Bonzini {
14176e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
14186e790746SPaolo Bonzini     VirtIONet *n = q->n;
141917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1420e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
1421e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
1422e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
1423e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
1424e8bcf842SMichael S. Tsirkin         return;
1425e8bcf842SMichael S. Tsirkin     }
14266e790746SPaolo Bonzini 
14276e790746SPaolo Bonzini     q->tx_waiting = 0;
14286e790746SPaolo Bonzini 
14296e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
143017a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
14316e790746SPaolo Bonzini         return;
143217a0ca55SKONRAD Frederic     }
14336e790746SPaolo Bonzini 
14346e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
14356e790746SPaolo Bonzini     virtio_net_flush_tx(q);
14366e790746SPaolo Bonzini }
14376e790746SPaolo Bonzini 
14386e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
14396e790746SPaolo Bonzini {
14406e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
14416e790746SPaolo Bonzini     VirtIONet *n = q->n;
144217a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
14436e790746SPaolo Bonzini     int32_t ret;
14446e790746SPaolo Bonzini 
1445e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
1446e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
1447e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
1448e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
1449e8bcf842SMichael S. Tsirkin         return;
1450e8bcf842SMichael S. Tsirkin     }
14516e790746SPaolo Bonzini 
14526e790746SPaolo Bonzini     q->tx_waiting = 0;
14536e790746SPaolo Bonzini 
14546e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
145517a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
14566e790746SPaolo Bonzini         return;
145717a0ca55SKONRAD Frederic     }
14586e790746SPaolo Bonzini 
14596e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
1460fa5e56c2SGreg Kurz     if (ret == -EBUSY || ret == -EINVAL) {
1461fa5e56c2SGreg Kurz         return; /* Notification re-enable handled by tx_complete or device
1462fa5e56c2SGreg Kurz                  * broken */
14636e790746SPaolo Bonzini     }
14646e790746SPaolo Bonzini 
14656e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
14666e790746SPaolo Bonzini      * more coming and immediately reschedule */
14676e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
14686e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
14696e790746SPaolo Bonzini         q->tx_waiting = 1;
14706e790746SPaolo Bonzini         return;
14716e790746SPaolo Bonzini     }
14726e790746SPaolo Bonzini 
14736e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
14746e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
14756e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
14766e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
1477fa5e56c2SGreg Kurz     ret = virtio_net_flush_tx(q);
1478fa5e56c2SGreg Kurz     if (ret == -EINVAL) {
1479fa5e56c2SGreg Kurz         return;
1480fa5e56c2SGreg Kurz     } else if (ret > 0) {
14816e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
14826e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
14836e790746SPaolo Bonzini         q->tx_waiting = 1;
14846e790746SPaolo Bonzini     }
14856e790746SPaolo Bonzini }
14866e790746SPaolo Bonzini 
1487f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index)
1488f9d6dbf0SWen Congyang {
1489f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1490f9d6dbf0SWen Congyang 
14911c0fbfa3SMichael S. Tsirkin     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
14921c0fbfa3SMichael S. Tsirkin                                            virtio_net_handle_rx);
1493f9d6dbf0SWen Congyang     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1494f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
1495f9d6dbf0SWen Congyang             virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1496f9d6dbf0SWen Congyang         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1497f9d6dbf0SWen Congyang                                               virtio_net_tx_timer,
1498f9d6dbf0SWen Congyang                                               &n->vqs[index]);
1499f9d6dbf0SWen Congyang     } else {
1500f9d6dbf0SWen Congyang         n->vqs[index].tx_vq =
1501f9d6dbf0SWen Congyang             virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1502f9d6dbf0SWen Congyang         n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
1503f9d6dbf0SWen Congyang     }
1504f9d6dbf0SWen Congyang 
1505f9d6dbf0SWen Congyang     n->vqs[index].tx_waiting = 0;
1506f9d6dbf0SWen Congyang     n->vqs[index].n = n;
1507f9d6dbf0SWen Congyang }
1508f9d6dbf0SWen Congyang 
1509f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index)
1510f9d6dbf0SWen Congyang {
1511f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1512f9d6dbf0SWen Congyang     VirtIONetQueue *q = &n->vqs[index];
1513f9d6dbf0SWen Congyang     NetClientState *nc = qemu_get_subqueue(n->nic, index);
1514f9d6dbf0SWen Congyang 
1515f9d6dbf0SWen Congyang     qemu_purge_queued_packets(nc);
1516f9d6dbf0SWen Congyang 
1517f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2);
1518f9d6dbf0SWen Congyang     if (q->tx_timer) {
1519f9d6dbf0SWen Congyang         timer_del(q->tx_timer);
1520f9d6dbf0SWen Congyang         timer_free(q->tx_timer);
1521f9d6dbf0SWen Congyang     } else {
1522f9d6dbf0SWen Congyang         qemu_bh_delete(q->tx_bh);
1523f9d6dbf0SWen Congyang     }
1524f9d6dbf0SWen Congyang     virtio_del_queue(vdev, index * 2 + 1);
1525f9d6dbf0SWen Congyang }
1526f9d6dbf0SWen Congyang 
1527f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
1528f9d6dbf0SWen Congyang {
1529f9d6dbf0SWen Congyang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1530f9d6dbf0SWen Congyang     int old_num_queues = virtio_get_num_queues(vdev);
1531f9d6dbf0SWen Congyang     int new_num_queues = new_max_queues * 2 + 1;
1532f9d6dbf0SWen Congyang     int i;
1533f9d6dbf0SWen Congyang 
1534f9d6dbf0SWen Congyang     assert(old_num_queues >= 3);
1535f9d6dbf0SWen Congyang     assert(old_num_queues % 2 == 1);
1536f9d6dbf0SWen Congyang 
1537f9d6dbf0SWen Congyang     if (old_num_queues == new_num_queues) {
1538f9d6dbf0SWen Congyang         return;
1539f9d6dbf0SWen Congyang     }
1540f9d6dbf0SWen Congyang 
1541f9d6dbf0SWen Congyang     /*
1542f9d6dbf0SWen Congyang      * We always need to remove and add ctrl vq if
1543f9d6dbf0SWen Congyang      * old_num_queues != new_num_queues. Remove ctrl_vq first,
1544f9d6dbf0SWen Congyang      * and then we only enter one of the following too loops.
1545f9d6dbf0SWen Congyang      */
1546f9d6dbf0SWen Congyang     virtio_del_queue(vdev, old_num_queues - 1);
1547f9d6dbf0SWen Congyang 
1548f9d6dbf0SWen Congyang     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
1549f9d6dbf0SWen Congyang         /* new_num_queues < old_num_queues */
1550f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i / 2);
1551f9d6dbf0SWen Congyang     }
1552f9d6dbf0SWen Congyang 
1553f9d6dbf0SWen Congyang     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
1554f9d6dbf0SWen Congyang         /* new_num_queues > old_num_queues */
1555f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i / 2);
1556f9d6dbf0SWen Congyang     }
1557f9d6dbf0SWen Congyang 
1558f9d6dbf0SWen Congyang     /* add ctrl_vq last */
1559f9d6dbf0SWen Congyang     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1560f9d6dbf0SWen Congyang }
1561f9d6dbf0SWen Congyang 
1562ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
15636e790746SPaolo Bonzini {
1564f9d6dbf0SWen Congyang     int max = multiqueue ? n->max_queues : 1;
1565f9d6dbf0SWen Congyang 
15666e790746SPaolo Bonzini     n->multiqueue = multiqueue;
1567f9d6dbf0SWen Congyang     virtio_net_change_num_queues(n, max);
15686e790746SPaolo Bonzini 
15696e790746SPaolo Bonzini     virtio_net_set_queues(n);
15706e790746SPaolo Bonzini }
15716e790746SPaolo Bonzini 
1572982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id)
1573037dab2fSGreg Kurz {
1574982b78c5SDr. David Alan Gilbert     VirtIONet *n = opaque;
1575982b78c5SDr. David Alan Gilbert     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1576037dab2fSGreg Kurz     int i, link_down;
1577037dab2fSGreg Kurz 
1578982b78c5SDr. David Alan Gilbert     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
157995129d6fSCornelia Huck                                virtio_vdev_has_feature(vdev,
158095129d6fSCornelia Huck                                                        VIRTIO_F_VERSION_1));
15816e790746SPaolo Bonzini 
15826e790746SPaolo Bonzini     /* MAC_TABLE_ENTRIES may be different from the saved image */
1583982b78c5SDr. David Alan Gilbert     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
15846e790746SPaolo Bonzini         n->mac_table.in_use = 0;
15856e790746SPaolo Bonzini     }
15866e790746SPaolo Bonzini 
1587982b78c5SDr. David Alan Gilbert     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
15886c666823SMichael S. Tsirkin         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
15896c666823SMichael S. Tsirkin     }
15906c666823SMichael S. Tsirkin 
15916c666823SMichael S. Tsirkin     if (peer_has_vnet_hdr(n)) {
15926c666823SMichael S. Tsirkin         virtio_net_apply_guest_offloads(n);
15936c666823SMichael S. Tsirkin     }
15946c666823SMichael S. Tsirkin 
15956e790746SPaolo Bonzini     virtio_net_set_queues(n);
15966e790746SPaolo Bonzini 
15976e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
15986e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
15996e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
16006e790746SPaolo Bonzini             break;
16016e790746SPaolo Bonzini         }
16026e790746SPaolo Bonzini     }
16036e790746SPaolo Bonzini     n->mac_table.first_multi = i;
16046e790746SPaolo Bonzini 
16056e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
16066e790746SPaolo Bonzini      * to link status bit in n->status */
16076e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
16086e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
16096e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
16106e790746SPaolo Bonzini     }
16116e790746SPaolo Bonzini 
16126c666823SMichael S. Tsirkin     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
16136c666823SMichael S. Tsirkin         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
16146c666823SMichael S. Tsirkin         n->announce_counter = SELF_ANNOUNCE_ROUNDS;
16156c666823SMichael S. Tsirkin         timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
16166c666823SMichael S. Tsirkin     }
16176c666823SMichael S. Tsirkin 
16186e790746SPaolo Bonzini     return 0;
16196e790746SPaolo Bonzini }
16206e790746SPaolo Bonzini 
1621982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */
1622982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
1623982b78c5SDr. David Alan Gilbert     .name = "virtio-net-queue-tx_waiting",
1624982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
1625982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
1626982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
1627982b78c5SDr. David Alan Gilbert    },
1628982b78c5SDr. David Alan Gilbert };
1629982b78c5SDr. David Alan Gilbert 
1630982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id)
1631982b78c5SDr. David Alan Gilbert {
1632982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->max_queues > 1;
1633982b78c5SDr. David Alan Gilbert }
1634982b78c5SDr. David Alan Gilbert 
1635982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id)
1636982b78c5SDr. David Alan Gilbert {
1637982b78c5SDr. David Alan Gilbert     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
1638982b78c5SDr. David Alan Gilbert                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
1639982b78c5SDr. David Alan Gilbert }
1640982b78c5SDr. David Alan Gilbert 
1641982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id)
1642982b78c5SDr. David Alan Gilbert {
1643982b78c5SDr. David Alan Gilbert     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
1644982b78c5SDr. David Alan Gilbert }
1645982b78c5SDr. David Alan Gilbert 
1646982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id)
1647982b78c5SDr. David Alan Gilbert {
1648982b78c5SDr. David Alan Gilbert     return !mac_table_fits(opaque, version_id);
1649982b78c5SDr. David Alan Gilbert }
1650982b78c5SDr. David Alan Gilbert 
1651982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods
1652982b78c5SDr. David Alan Gilbert  * although only some fields are used by each.
1653982b78c5SDr. David Alan Gilbert  */
1654982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp {
1655982b78c5SDr. David Alan Gilbert     VirtIONet      *parent;
1656982b78c5SDr. David Alan Gilbert     VirtIONetQueue *vqs_1;
1657982b78c5SDr. David Alan Gilbert     uint16_t        curr_queues_1;
1658982b78c5SDr. David Alan Gilbert     uint8_t         has_ufo;
1659982b78c5SDr. David Alan Gilbert     uint32_t        has_vnet_hdr;
1660982b78c5SDr. David Alan Gilbert };
1661982b78c5SDr. David Alan Gilbert 
1662982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than
1663982b78c5SDr. David Alan Gilbert  * the 1st entry in the queues and only if there's more than one
1664982b78c5SDr. David Alan Gilbert  * entry.  We use the tmp mechanism to calculate a temporary
1665982b78c5SDr. David Alan Gilbert  * pointer and count and also validate the count.
1666982b78c5SDr. David Alan Gilbert  */
1667982b78c5SDr. David Alan Gilbert 
1668982b78c5SDr. David Alan Gilbert static void virtio_net_tx_waiting_pre_save(void *opaque)
1669982b78c5SDr. David Alan Gilbert {
1670982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1671982b78c5SDr. David Alan Gilbert 
1672982b78c5SDr. David Alan Gilbert     tmp->vqs_1 = tmp->parent->vqs + 1;
1673982b78c5SDr. David Alan Gilbert     tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
1674982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues == 0) {
1675982b78c5SDr. David Alan Gilbert         tmp->curr_queues_1 = 0;
1676982b78c5SDr. David Alan Gilbert     }
1677982b78c5SDr. David Alan Gilbert }
1678982b78c5SDr. David Alan Gilbert 
1679982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque)
1680982b78c5SDr. David Alan Gilbert {
1681982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1682982b78c5SDr. David Alan Gilbert 
1683982b78c5SDr. David Alan Gilbert     /* Reuse the pointer setup from save */
1684982b78c5SDr. David Alan Gilbert     virtio_net_tx_waiting_pre_save(opaque);
1685982b78c5SDr. David Alan Gilbert 
1686982b78c5SDr. David Alan Gilbert     if (tmp->parent->curr_queues > tmp->parent->max_queues) {
1687982b78c5SDr. David Alan Gilbert         error_report("virtio-net: curr_queues %x > max_queues %x",
1688982b78c5SDr. David Alan Gilbert             tmp->parent->curr_queues, tmp->parent->max_queues);
1689982b78c5SDr. David Alan Gilbert 
1690982b78c5SDr. David Alan Gilbert         return -EINVAL;
1691982b78c5SDr. David Alan Gilbert     }
1692982b78c5SDr. David Alan Gilbert 
1693982b78c5SDr. David Alan Gilbert     return 0; /* all good */
1694982b78c5SDr. David Alan Gilbert }
1695982b78c5SDr. David Alan Gilbert 
1696982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = {
1697982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-tx_waiting",
1698982b78c5SDr. David Alan Gilbert     .pre_load  = virtio_net_tx_waiting_pre_load,
1699982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_tx_waiting_pre_save,
1700982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
1701982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
1702982b78c5SDr. David Alan Gilbert                                      curr_queues_1,
1703982b78c5SDr. David Alan Gilbert                                      vmstate_virtio_net_queue_tx_waiting,
1704982b78c5SDr. David Alan Gilbert                                      struct VirtIONetQueue),
1705982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
1706982b78c5SDr. David Alan Gilbert     },
1707982b78c5SDr. David Alan Gilbert };
1708982b78c5SDr. David Alan Gilbert 
1709982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the
1710982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
1711982b78c5SDr. David Alan Gilbert  */
1712982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id)
1713982b78c5SDr. David Alan Gilbert {
1714982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1715982b78c5SDr. David Alan Gilbert 
1716982b78c5SDr. David Alan Gilbert     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
1717982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires TUN_F_UFO support");
1718982b78c5SDr. David Alan Gilbert         return -EINVAL;
1719982b78c5SDr. David Alan Gilbert     }
1720982b78c5SDr. David Alan Gilbert 
1721982b78c5SDr. David Alan Gilbert     return 0;
1722982b78c5SDr. David Alan Gilbert }
1723982b78c5SDr. David Alan Gilbert 
1724982b78c5SDr. David Alan Gilbert static void virtio_net_ufo_pre_save(void *opaque)
1725982b78c5SDr. David Alan Gilbert {
1726982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1727982b78c5SDr. David Alan Gilbert 
1728982b78c5SDr. David Alan Gilbert     tmp->has_ufo = tmp->parent->has_ufo;
1729982b78c5SDr. David Alan Gilbert }
1730982b78c5SDr. David Alan Gilbert 
1731982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = {
1732982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-ufo",
1733982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_ufo_post_load,
1734982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_ufo_pre_save,
1735982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
1736982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
1737982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
1738982b78c5SDr. David Alan Gilbert     },
1739982b78c5SDr. David Alan Gilbert };
1740982b78c5SDr. David Alan Gilbert 
1741982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
1742982b78c5SDr. David Alan Gilbert  * flag set we need to check that we have it
1743982b78c5SDr. David Alan Gilbert  */
1744982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id)
1745982b78c5SDr. David Alan Gilbert {
1746982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1747982b78c5SDr. David Alan Gilbert 
1748982b78c5SDr. David Alan Gilbert     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
1749982b78c5SDr. David Alan Gilbert         error_report("virtio-net: saved image requires vnet_hdr=on");
1750982b78c5SDr. David Alan Gilbert         return -EINVAL;
1751982b78c5SDr. David Alan Gilbert     }
1752982b78c5SDr. David Alan Gilbert 
1753982b78c5SDr. David Alan Gilbert     return 0;
1754982b78c5SDr. David Alan Gilbert }
1755982b78c5SDr. David Alan Gilbert 
1756982b78c5SDr. David Alan Gilbert static void virtio_net_vnet_pre_save(void *opaque)
1757982b78c5SDr. David Alan Gilbert {
1758982b78c5SDr. David Alan Gilbert     struct VirtIONetMigTmp *tmp = opaque;
1759982b78c5SDr. David Alan Gilbert 
1760982b78c5SDr. David Alan Gilbert     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
1761982b78c5SDr. David Alan Gilbert }
1762982b78c5SDr. David Alan Gilbert 
1763982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = {
1764982b78c5SDr. David Alan Gilbert     .name      = "virtio-net-vnet",
1765982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_vnet_post_load,
1766982b78c5SDr. David Alan Gilbert     .pre_save  = virtio_net_vnet_pre_save,
1767982b78c5SDr. David Alan Gilbert     .fields    = (VMStateField[]) {
1768982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
1769982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
1770982b78c5SDr. David Alan Gilbert     },
1771982b78c5SDr. David Alan Gilbert };
1772982b78c5SDr. David Alan Gilbert 
1773982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = {
1774982b78c5SDr. David Alan Gilbert     .name = "virtio-net-device",
1775982b78c5SDr. David Alan Gilbert     .version_id = VIRTIO_NET_VM_VERSION,
1776982b78c5SDr. David Alan Gilbert     .minimum_version_id = VIRTIO_NET_VM_VERSION,
1777982b78c5SDr. David Alan Gilbert     .post_load = virtio_net_post_load_device,
1778982b78c5SDr. David Alan Gilbert     .fields = (VMStateField[]) {
1779982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
1780982b78c5SDr. David Alan Gilbert         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
1781982b78c5SDr. David Alan Gilbert                                vmstate_virtio_net_queue_tx_waiting,
1782982b78c5SDr. David Alan Gilbert                                VirtIONetQueue),
1783982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
1784982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16(status, VirtIONet),
1785982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(promisc, VirtIONet),
1786982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(allmulti, VirtIONet),
1787982b78c5SDr. David Alan Gilbert         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
1788982b78c5SDr. David Alan Gilbert 
1789982b78c5SDr. David Alan Gilbert         /* Guarded pair: If it fits we load it, else we throw it away
1790982b78c5SDr. David Alan Gilbert          * - can happen if source has a larger MAC table.; post-load
1791982b78c5SDr. David Alan Gilbert          *  sets flags in this case.
1792982b78c5SDr. David Alan Gilbert          */
1793982b78c5SDr. David Alan Gilbert         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
1794982b78c5SDr. David Alan Gilbert                                 0, mac_table_fits, mac_table.in_use,
1795982b78c5SDr. David Alan Gilbert                                  ETH_ALEN),
1796982b78c5SDr. David Alan Gilbert         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
1797982b78c5SDr. David Alan Gilbert                                      mac_table.in_use, ETH_ALEN),
1798982b78c5SDr. David Alan Gilbert 
1799982b78c5SDr. David Alan Gilbert         /* Note: This is an array of uint32's that's always been saved as a
1800982b78c5SDr. David Alan Gilbert          * buffer; hold onto your endiannesses; it's actually used as a bitmap
1801982b78c5SDr. David Alan Gilbert          * but based on the uint.
1802982b78c5SDr. David Alan Gilbert          */
1803982b78c5SDr. David Alan Gilbert         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
1804982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1805982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_vnet),
1806982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
1807982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
1808982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(alluni, VirtIONet),
1809982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nomulti, VirtIONet),
1810982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nouni, VirtIONet),
1811982b78c5SDr. David Alan Gilbert         VMSTATE_UINT8(nobcast, VirtIONet),
1812982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1813982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_has_ufo),
1814982b78c5SDr. David Alan Gilbert         VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
1815982b78c5SDr. David Alan Gilbert                             vmstate_info_uint16_equal, uint16_t),
1816982b78c5SDr. David Alan Gilbert         VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
1817982b78c5SDr. David Alan Gilbert         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1818982b78c5SDr. David Alan Gilbert                          vmstate_virtio_net_tx_waiting),
1819982b78c5SDr. David Alan Gilbert         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
1820982b78c5SDr. David Alan Gilbert                             has_ctrl_guest_offloads),
1821982b78c5SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
1822982b78c5SDr. David Alan Gilbert    },
1823982b78c5SDr. David Alan Gilbert };
1824982b78c5SDr. David Alan Gilbert 
18256e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
1826f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
18276e790746SPaolo Bonzini     .size = sizeof(NICState),
18286e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
18296e790746SPaolo Bonzini     .receive = virtio_net_receive,
18306e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
1831b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
18326e790746SPaolo Bonzini };
18336e790746SPaolo Bonzini 
18346e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
18356e790746SPaolo Bonzini {
183617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
18376e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
18386e790746SPaolo Bonzini     assert(n->vhost_started);
1839ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
18406e790746SPaolo Bonzini }
18416e790746SPaolo Bonzini 
18426e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
18436e790746SPaolo Bonzini                                            bool mask)
18446e790746SPaolo Bonzini {
184517a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
18466e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
18476e790746SPaolo Bonzini     assert(n->vhost_started);
1848ed8b4afeSNikolay Nikolaev     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
18496e790746SPaolo Bonzini                              vdev, idx, mask);
18506e790746SPaolo Bonzini }
18516e790746SPaolo Bonzini 
1852019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
18536e790746SPaolo Bonzini {
18546e790746SPaolo Bonzini     int i, config_size = 0;
18550cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
1856a93e599dSMaxime Coquelin 
18576e790746SPaolo Bonzini     for (i = 0; feature_sizes[i].flags != 0; i++) {
18586e790746SPaolo Bonzini         if (host_features & feature_sizes[i].flags) {
18596e790746SPaolo Bonzini             config_size = MAX(feature_sizes[i].end, config_size);
18606e790746SPaolo Bonzini         }
18616e790746SPaolo Bonzini     }
186217ec5a86SKONRAD Frederic     n->config_size = config_size;
186317ec5a86SKONRAD Frederic }
18646e790746SPaolo Bonzini 
18658a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
18668a253ec2SKONRAD Frederic                                    const char *type)
18678a253ec2SKONRAD Frederic {
18688a253ec2SKONRAD Frederic     /*
18698a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
18708a253ec2SKONRAD Frederic      */
18718a253ec2SKONRAD Frederic     assert(type != NULL);
18728a253ec2SKONRAD Frederic 
18738a253ec2SKONRAD Frederic     g_free(n->netclient_name);
18748a253ec2SKONRAD Frederic     g_free(n->netclient_type);
18758a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
18768a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
18778a253ec2SKONRAD Frederic }
18788a253ec2SKONRAD Frederic 
1879e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
188017ec5a86SKONRAD Frederic {
1881e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1882284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
1883284a32f0SAndreas Färber     NetClientState *nc;
18841773d9eeSKONRAD Frederic     int i;
188517ec5a86SKONRAD Frederic 
1886a93e599dSMaxime Coquelin     if (n->net_conf.mtu) {
1887a93e599dSMaxime Coquelin         n->host_features |= (0x1 << VIRTIO_NET_F_MTU);
1888a93e599dSMaxime Coquelin     }
1889a93e599dSMaxime Coquelin 
1890da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
1891284a32f0SAndreas Färber     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
189217ec5a86SKONRAD Frederic 
18931c0fbfa3SMichael S. Tsirkin     /*
18941c0fbfa3SMichael S. Tsirkin      * We set a lower limit on RX queue size to what it always was.
18951c0fbfa3SMichael S. Tsirkin      * Guests that want a smaller ring can always resize it without
18961c0fbfa3SMichael S. Tsirkin      * help from us (using virtio 1 and up).
18971c0fbfa3SMichael S. Tsirkin      */
18981c0fbfa3SMichael S. Tsirkin     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
18991c0fbfa3SMichael S. Tsirkin         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
19001c0fbfa3SMichael S. Tsirkin         (n->net_conf.rx_queue_size & (n->net_conf.rx_queue_size - 1))) {
19011c0fbfa3SMichael S. Tsirkin         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
19021c0fbfa3SMichael S. Tsirkin                    "must be a power of 2 between %d and %d.",
19031c0fbfa3SMichael S. Tsirkin                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
19041c0fbfa3SMichael S. Tsirkin                    VIRTQUEUE_MAX_SIZE);
19051c0fbfa3SMichael S. Tsirkin         virtio_cleanup(vdev);
19061c0fbfa3SMichael S. Tsirkin         return;
19071c0fbfa3SMichael S. Tsirkin     }
19081c0fbfa3SMichael S. Tsirkin 
1909575a1c0eSJiri Pirko     n->max_queues = MAX(n->nic_conf.peers.queues, 1);
191087b3bd1cSJason Wang     if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
19117e0e736eSJason Wang         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1912631b22eaSStefan Weil                    "must be a positive integer less than %d.",
191387b3bd1cSJason Wang                    n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
19147e0e736eSJason Wang         virtio_cleanup(vdev);
19157e0e736eSJason Wang         return;
19167e0e736eSJason Wang     }
19176e790746SPaolo Bonzini     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
19186e790746SPaolo Bonzini     n->curr_queues = 1;
19191773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
19206e790746SPaolo Bonzini 
19211773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
19221773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
19236e790746SPaolo Bonzini         error_report("virtio-net: "
19246e790746SPaolo Bonzini                      "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
19251773d9eeSKONRAD Frederic                      n->net_conf.tx);
19266e790746SPaolo Bonzini         error_report("Defaulting to \"bh\"");
19276e790746SPaolo Bonzini     }
19286e790746SPaolo Bonzini 
1929da51a335SJason Wang     for (i = 0; i < n->max_queues; i++) {
1930f9d6dbf0SWen Congyang         virtio_net_add_queue(n, i);
1931da51a335SJason Wang     }
1932da51a335SJason Wang 
193317a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
19341773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
19351773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
19366e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
1937f57fcf70SJason Wang     n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1938f57fcf70SJason Wang                                      virtio_net_announce_timer, n);
19396e790746SPaolo Bonzini 
19408a253ec2SKONRAD Frederic     if (n->netclient_type) {
19418a253ec2SKONRAD Frederic         /*
19428a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
19438a253ec2SKONRAD Frederic          */
19448a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
19458a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
19468a253ec2SKONRAD Frederic     } else {
19471773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1948284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
19498a253ec2SKONRAD Frederic     }
19508a253ec2SKONRAD Frederic 
19516e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
19526e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
19536e790746SPaolo Bonzini         for (i = 0; i < n->max_queues; i++) {
1954d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
19556e790746SPaolo Bonzini         }
19566e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
19576e790746SPaolo Bonzini     } else {
19586e790746SPaolo Bonzini         n->host_hdr_len = 0;
19596e790746SPaolo Bonzini     }
19606e790746SPaolo Bonzini 
19611773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
19626e790746SPaolo Bonzini 
19636e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
19641773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
1965bb9d17f8SCornelia Huck     virtio_net_set_mrg_rx_bufs(n, 0, 0);
19666e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
19676e790746SPaolo Bonzini 
19686e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
19696e790746SPaolo Bonzini 
19706e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
19716e790746SPaolo Bonzini 
1972b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
1973b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
1974b1be4280SAmos Kong 
1975284a32f0SAndreas Färber     n->qdev = dev;
197617ec5a86SKONRAD Frederic }
197717ec5a86SKONRAD Frederic 
1978306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
197917ec5a86SKONRAD Frederic {
1980306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1981306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
1982f9d6dbf0SWen Congyang     int i, max_queues;
198317ec5a86SKONRAD Frederic 
198417ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
198517ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
198617ec5a86SKONRAD Frederic 
19878a253ec2SKONRAD Frederic     g_free(n->netclient_name);
19888a253ec2SKONRAD Frederic     n->netclient_name = NULL;
19898a253ec2SKONRAD Frederic     g_free(n->netclient_type);
19908a253ec2SKONRAD Frederic     n->netclient_type = NULL;
19918a253ec2SKONRAD Frederic 
199217ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
199317ec5a86SKONRAD Frederic     g_free(n->vlans);
199417ec5a86SKONRAD Frederic 
1995f9d6dbf0SWen Congyang     max_queues = n->multiqueue ? n->max_queues : 1;
1996f9d6dbf0SWen Congyang     for (i = 0; i < max_queues; i++) {
1997f9d6dbf0SWen Congyang         virtio_net_del_queue(n, i);
199817ec5a86SKONRAD Frederic     }
199917ec5a86SKONRAD Frederic 
2000f57fcf70SJason Wang     timer_del(n->announce_timer);
2001f57fcf70SJason Wang     timer_free(n->announce_timer);
200217ec5a86SKONRAD Frederic     g_free(n->vqs);
200317ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
20046a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
200517ec5a86SKONRAD Frederic }
200617ec5a86SKONRAD Frederic 
200717ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
200817ec5a86SKONRAD Frederic {
200917ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
201017ec5a86SKONRAD Frederic 
201117ec5a86SKONRAD Frederic     /*
201217ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
201317ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
201417ec5a86SKONRAD Frederic      */
201517ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
2016aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
2017aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
2018aa4197c3SGonglei                                   DEVICE(n), NULL);
201917ec5a86SKONRAD Frederic }
202017ec5a86SKONRAD Frederic 
20214d45dcfbSHalil Pasic static void virtio_net_pre_save(void *opaque)
20224d45dcfbSHalil Pasic {
20234d45dcfbSHalil Pasic     VirtIONet *n = opaque;
20244d45dcfbSHalil Pasic 
20254d45dcfbSHalil Pasic     /* At this point, backend must be stopped, otherwise
20264d45dcfbSHalil Pasic      * it might keep writing to memory. */
20274d45dcfbSHalil Pasic     assert(!n->vhost_started);
20284d45dcfbSHalil Pasic }
20294d45dcfbSHalil Pasic 
20304d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = {
20314d45dcfbSHalil Pasic     .name = "virtio-net",
20324d45dcfbSHalil Pasic     .minimum_version_id = VIRTIO_NET_VM_VERSION,
20334d45dcfbSHalil Pasic     .version_id = VIRTIO_NET_VM_VERSION,
20344d45dcfbSHalil Pasic     .fields = (VMStateField[]) {
20354d45dcfbSHalil Pasic         VMSTATE_VIRTIO_DEVICE,
20364d45dcfbSHalil Pasic         VMSTATE_END_OF_LIST()
20374d45dcfbSHalil Pasic     },
20384d45dcfbSHalil Pasic     .pre_save = virtio_net_pre_save,
20394d45dcfbSHalil Pasic };
2040290c2428SDr. David Alan Gilbert 
204117ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
204287108bb2SShannon Zhao     DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
204387108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
204487108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
204587108bb2SShannon Zhao     DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
204687108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
204787108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
204887108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
204987108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
205087108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
205187108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
205287108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
205387108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
205487108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
205587108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
205687108bb2SShannon Zhao     DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
205787108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
205887108bb2SShannon Zhao     DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
205987108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
206087108bb2SShannon Zhao     DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
206187108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
206287108bb2SShannon Zhao     DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
206387108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
206487108bb2SShannon Zhao     DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
206587108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
206687108bb2SShannon Zhao     DEFINE_PROP_BIT("status", VirtIONet, host_features,
206787108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
206887108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
206987108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
207087108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
207187108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
207287108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
207387108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
207487108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
207587108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
207687108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
207787108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
207887108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
207987108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
208087108bb2SShannon Zhao     DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
208117ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
208217ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
208317ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
208417ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
208517ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
20861c0fbfa3SMichael S. Tsirkin     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
20871c0fbfa3SMichael S. Tsirkin                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
2088a93e599dSMaxime Coquelin     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
208917ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
209017ec5a86SKONRAD Frederic };
209117ec5a86SKONRAD Frederic 
209217ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
209317ec5a86SKONRAD Frederic {
209417ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
209517ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
2096e6f746b3SAndreas Färber 
209717ec5a86SKONRAD Frederic     dc->props = virtio_net_properties;
2098290c2428SDr. David Alan Gilbert     dc->vmsd = &vmstate_virtio_net;
2099125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2100e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
2101306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
210217ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
210317ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
210417ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
210517ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
210617ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
210717ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
210817ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
210917ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
211017ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
21112a083ffdSMichael S. Tsirkin     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
2112982b78c5SDr. David Alan Gilbert     vdc->vmsd = &vmstate_virtio_net_device;
211317ec5a86SKONRAD Frederic }
211417ec5a86SKONRAD Frederic 
211517ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
211617ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
211717ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
211817ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
211917ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
212017ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
212117ec5a86SKONRAD Frederic };
212217ec5a86SKONRAD Frederic 
212317ec5a86SKONRAD Frederic static void virtio_register_types(void)
212417ec5a86SKONRAD Frederic {
212517ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
212617ec5a86SKONRAD Frederic }
212717ec5a86SKONRAD Frederic 
212817ec5a86SKONRAD Frederic type_init(virtio_register_types)
2129