xref: /openbmc/qemu/hw/net/virtio-net.c (revision 87108bb26ce04637980c0897caeabee8901e72c9)
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 
146e790746SPaolo Bonzini #include "qemu/iov.h"
156e790746SPaolo Bonzini #include "hw/virtio/virtio.h"
166e790746SPaolo Bonzini #include "net/net.h"
176e790746SPaolo Bonzini #include "net/checksum.h"
186e790746SPaolo Bonzini #include "net/tap.h"
196e790746SPaolo Bonzini #include "qemu/error-report.h"
206e790746SPaolo Bonzini #include "qemu/timer.h"
216e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h"
226e790746SPaolo Bonzini #include "net/vhost_net.h"
2317ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h"
24b1be4280SAmos Kong #include "qapi/qmp/qjson.h"
2506150279SWenchao Xia #include "qapi-event.h"
261399c60dSRusty Russell #include "hw/virtio/virtio-access.h"
276e790746SPaolo Bonzini 
286e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
296e790746SPaolo Bonzini 
306e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES    64
316e790746SPaolo Bonzini #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
326e790746SPaolo Bonzini 
336e790746SPaolo Bonzini /*
346e790746SPaolo Bonzini  * Calculate the number of bytes up to and including the given 'field' of
356e790746SPaolo Bonzini  * 'container'.
366e790746SPaolo Bonzini  */
376e790746SPaolo Bonzini #define endof(container, field) \
386e790746SPaolo Bonzini     (offsetof(container, field) + sizeof(((container *)0)->field))
396e790746SPaolo Bonzini 
406e790746SPaolo Bonzini typedef struct VirtIOFeature {
416e790746SPaolo Bonzini     uint32_t flags;
426e790746SPaolo Bonzini     size_t end;
436e790746SPaolo Bonzini } VirtIOFeature;
446e790746SPaolo Bonzini 
456e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = {
466e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MAC,
476e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, mac)},
486e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_STATUS,
496e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, status)},
506e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MQ,
516e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
526e790746SPaolo Bonzini     {}
536e790746SPaolo Bonzini };
546e790746SPaolo Bonzini 
556e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
566e790746SPaolo Bonzini {
576e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
586e790746SPaolo Bonzini 
596e790746SPaolo Bonzini     return &n->vqs[nc->queue_index];
606e790746SPaolo Bonzini }
616e790746SPaolo Bonzini 
626e790746SPaolo Bonzini static int vq2q(int queue_index)
636e790746SPaolo Bonzini {
646e790746SPaolo Bonzini     return queue_index / 2;
656e790746SPaolo Bonzini }
666e790746SPaolo Bonzini 
676e790746SPaolo Bonzini /* TODO
686e790746SPaolo Bonzini  * - we could suppress RX interrupt if we were so inclined.
696e790746SPaolo Bonzini  */
706e790746SPaolo Bonzini 
716e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
726e790746SPaolo Bonzini {
7317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
746e790746SPaolo Bonzini     struct virtio_net_config netcfg;
756e790746SPaolo Bonzini 
761399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.status, n->status);
771399c60dSRusty Russell     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
786e790746SPaolo Bonzini     memcpy(netcfg.mac, n->mac, ETH_ALEN);
796e790746SPaolo Bonzini     memcpy(config, &netcfg, n->config_size);
806e790746SPaolo Bonzini }
816e790746SPaolo Bonzini 
826e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
836e790746SPaolo Bonzini {
8417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
856e790746SPaolo Bonzini     struct virtio_net_config netcfg = {};
866e790746SPaolo Bonzini 
876e790746SPaolo Bonzini     memcpy(&netcfg, config, n->config_size);
886e790746SPaolo Bonzini 
89ef546f12SCornelia Huck     if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
90b6a3cddbSCornelia Huck         !virtio_has_feature(vdev, VIRTIO_F_VERSION_1) &&
916e790746SPaolo Bonzini         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
926e790746SPaolo Bonzini         memcpy(n->mac, netcfg.mac, ETH_ALEN);
936e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
946e790746SPaolo Bonzini     }
956e790746SPaolo Bonzini }
966e790746SPaolo Bonzini 
976e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status)
986e790746SPaolo Bonzini {
9917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1006e790746SPaolo Bonzini     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
10117a0ca55SKONRAD Frederic         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
1026e790746SPaolo Bonzini }
1036e790746SPaolo Bonzini 
104f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque)
105f57fcf70SJason Wang {
106f57fcf70SJason Wang     VirtIONet *n = opaque;
107f57fcf70SJason Wang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
108f57fcf70SJason Wang 
109f57fcf70SJason Wang     n->announce_counter--;
110f57fcf70SJason Wang     n->status |= VIRTIO_NET_S_ANNOUNCE;
111f57fcf70SJason Wang     virtio_notify_config(vdev);
112f57fcf70SJason Wang }
113f57fcf70SJason Wang 
1146e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
1156e790746SPaolo Bonzini {
11617a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1176e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
1186e790746SPaolo Bonzini     int queues = n->multiqueue ? n->max_queues : 1;
1196e790746SPaolo Bonzini 
120ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
1216e790746SPaolo Bonzini         return;
1226e790746SPaolo Bonzini     }
1236e790746SPaolo Bonzini 
1248c1ac475SRadim Krčmář     if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
1258c1ac475SRadim Krčmář         !!n->vhost_started) {
1266e790746SPaolo Bonzini         return;
1276e790746SPaolo Bonzini     }
1286e790746SPaolo Bonzini     if (!n->vhost_started) {
129086abc1cSMichael S. Tsirkin         int r, i;
130086abc1cSMichael S. Tsirkin 
131ed8b4afeSNikolay Nikolaev         if (!vhost_net_query(get_vhost_net(nc->peer), vdev)) {
1326e790746SPaolo Bonzini             return;
1336e790746SPaolo Bonzini         }
134086abc1cSMichael S. Tsirkin 
135086abc1cSMichael S. Tsirkin         /* Any packets outstanding? Purge them to avoid touching rings
136086abc1cSMichael S. Tsirkin          * when vhost is running.
137086abc1cSMichael S. Tsirkin          */
138086abc1cSMichael S. Tsirkin         for (i = 0;  i < queues; i++) {
139086abc1cSMichael S. Tsirkin             NetClientState *qnc = qemu_get_subqueue(n->nic, i);
140086abc1cSMichael S. Tsirkin 
141086abc1cSMichael S. Tsirkin             /* Purge both directions: TX and RX. */
142086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
143086abc1cSMichael S. Tsirkin             qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
144086abc1cSMichael S. Tsirkin         }
145086abc1cSMichael S. Tsirkin 
1466e790746SPaolo Bonzini         n->vhost_started = 1;
14717a0ca55SKONRAD Frederic         r = vhost_net_start(vdev, n->nic->ncs, queues);
1486e790746SPaolo Bonzini         if (r < 0) {
1496e790746SPaolo Bonzini             error_report("unable to start vhost net: %d: "
1506e790746SPaolo Bonzini                          "falling back on userspace virtio", -r);
1516e790746SPaolo Bonzini             n->vhost_started = 0;
1526e790746SPaolo Bonzini         }
1536e790746SPaolo Bonzini     } else {
15417a0ca55SKONRAD Frederic         vhost_net_stop(vdev, n->nic->ncs, queues);
1556e790746SPaolo Bonzini         n->vhost_started = 0;
1566e790746SPaolo Bonzini     }
1576e790746SPaolo Bonzini }
1586e790746SPaolo Bonzini 
1596e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
1606e790746SPaolo Bonzini {
16117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1626e790746SPaolo Bonzini     VirtIONetQueue *q;
1636e790746SPaolo Bonzini     int i;
1646e790746SPaolo Bonzini     uint8_t queue_status;
1656e790746SPaolo Bonzini 
1666e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
1676e790746SPaolo Bonzini 
1686e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
1696e790746SPaolo Bonzini         q = &n->vqs[i];
1706e790746SPaolo Bonzini 
1716e790746SPaolo Bonzini         if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
1726e790746SPaolo Bonzini             queue_status = 0;
1736e790746SPaolo Bonzini         } else {
1746e790746SPaolo Bonzini             queue_status = status;
1756e790746SPaolo Bonzini         }
1766e790746SPaolo Bonzini 
1776e790746SPaolo Bonzini         if (!q->tx_waiting) {
1786e790746SPaolo Bonzini             continue;
1796e790746SPaolo Bonzini         }
1806e790746SPaolo Bonzini 
1816e790746SPaolo Bonzini         if (virtio_net_started(n, queue_status) && !n->vhost_started) {
1826e790746SPaolo Bonzini             if (q->tx_timer) {
183bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
184bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1856e790746SPaolo Bonzini             } else {
1866e790746SPaolo Bonzini                 qemu_bh_schedule(q->tx_bh);
1876e790746SPaolo Bonzini             }
1886e790746SPaolo Bonzini         } else {
1896e790746SPaolo Bonzini             if (q->tx_timer) {
190bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
1916e790746SPaolo Bonzini             } else {
1926e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
1936e790746SPaolo Bonzini             }
1946e790746SPaolo Bonzini         }
1956e790746SPaolo Bonzini     }
1966e790746SPaolo Bonzini }
1976e790746SPaolo Bonzini 
1986e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
1996e790746SPaolo Bonzini {
2006e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
20117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2026e790746SPaolo Bonzini     uint16_t old_status = n->status;
2036e790746SPaolo Bonzini 
2046e790746SPaolo Bonzini     if (nc->link_down)
2056e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
2066e790746SPaolo Bonzini     else
2076e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
2086e790746SPaolo Bonzini 
2096e790746SPaolo Bonzini     if (n->status != old_status)
21017a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
2116e790746SPaolo Bonzini 
21217a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
2136e790746SPaolo Bonzini }
2146e790746SPaolo Bonzini 
215b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
216b1be4280SAmos Kong {
217b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
218b1be4280SAmos Kong 
219b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
22096e35046SAmos Kong         gchar *path = object_get_canonical_path(OBJECT(n->qdev));
22106150279SWenchao Xia         qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
22206150279SWenchao Xia                                               n->netclient_name, path, &error_abort);
22396e35046SAmos Kong         g_free(path);
224b1be4280SAmos Kong 
225b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
226b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
227b1be4280SAmos Kong     }
228b1be4280SAmos Kong }
229b1be4280SAmos Kong 
230f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
231f7bc8ef8SAmos Kong {
232f7bc8ef8SAmos Kong     intList *list, *entry;
233f7bc8ef8SAmos Kong     int i, j;
234f7bc8ef8SAmos Kong 
235f7bc8ef8SAmos Kong     list = NULL;
236f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
237f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
238f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
239f7bc8ef8SAmos Kong                 entry = g_malloc0(sizeof(*entry));
240f7bc8ef8SAmos Kong                 entry->value = (i << 5) + j;
241f7bc8ef8SAmos Kong                 entry->next = list;
242f7bc8ef8SAmos Kong                 list = entry;
243f7bc8ef8SAmos Kong             }
244f7bc8ef8SAmos Kong         }
245f7bc8ef8SAmos Kong     }
246f7bc8ef8SAmos Kong 
247f7bc8ef8SAmos Kong     return list;
248f7bc8ef8SAmos Kong }
249f7bc8ef8SAmos Kong 
250b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
251b1be4280SAmos Kong {
252b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
253f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
254b1be4280SAmos Kong     RxFilterInfo *info;
255b1be4280SAmos Kong     strList *str_list, *entry;
256f7bc8ef8SAmos Kong     int i;
257b1be4280SAmos Kong 
258b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
259b1be4280SAmos Kong     info->name = g_strdup(nc->name);
260b1be4280SAmos Kong     info->promiscuous = n->promisc;
261b1be4280SAmos Kong 
262b1be4280SAmos Kong     if (n->nouni) {
263b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
264b1be4280SAmos Kong     } else if (n->alluni) {
265b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
266b1be4280SAmos Kong     } else {
267b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
268b1be4280SAmos Kong     }
269b1be4280SAmos Kong 
270b1be4280SAmos Kong     if (n->nomulti) {
271b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
272b1be4280SAmos Kong     } else if (n->allmulti) {
273b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
274b1be4280SAmos Kong     } else {
275b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
276b1be4280SAmos Kong     }
277b1be4280SAmos Kong 
278b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
279b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
280b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
281b1be4280SAmos Kong 
282b0575ba4SScott Feldman     info->main_mac = qemu_mac_strdup_printf(n->mac);
283b1be4280SAmos Kong 
284b1be4280SAmos Kong     str_list = NULL;
285b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
286b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
287b0575ba4SScott Feldman         entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
288b1be4280SAmos Kong         entry->next = str_list;
289b1be4280SAmos Kong         str_list = entry;
290b1be4280SAmos Kong     }
291b1be4280SAmos Kong     info->unicast_table = str_list;
292b1be4280SAmos Kong 
293b1be4280SAmos Kong     str_list = NULL;
294b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
295b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
296b0575ba4SScott Feldman         entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
297b1be4280SAmos Kong         entry->next = str_list;
298b1be4280SAmos Kong         str_list = entry;
299b1be4280SAmos Kong     }
300b1be4280SAmos Kong     info->multicast_table = str_list;
301f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
302b1be4280SAmos Kong 
303ef546f12SCornelia Huck     if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
304f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
305f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
306f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
307f7bc8ef8SAmos Kong     } else {
308f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
309b1be4280SAmos Kong     }
310b1be4280SAmos Kong 
311b1be4280SAmos Kong     /* enable event notification after query */
312b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
313b1be4280SAmos Kong 
314b1be4280SAmos Kong     return info;
315b1be4280SAmos Kong }
316b1be4280SAmos Kong 
3176e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev)
3186e790746SPaolo Bonzini {
31917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
3206e790746SPaolo Bonzini 
3216e790746SPaolo Bonzini     /* Reset back to compatibility mode */
3226e790746SPaolo Bonzini     n->promisc = 1;
3236e790746SPaolo Bonzini     n->allmulti = 0;
3246e790746SPaolo Bonzini     n->alluni = 0;
3256e790746SPaolo Bonzini     n->nomulti = 0;
3266e790746SPaolo Bonzini     n->nouni = 0;
3276e790746SPaolo Bonzini     n->nobcast = 0;
3286e790746SPaolo Bonzini     /* multiqueue is disabled by default */
3296e790746SPaolo Bonzini     n->curr_queues = 1;
330f57fcf70SJason Wang     timer_del(n->announce_timer);
331f57fcf70SJason Wang     n->announce_counter = 0;
332f57fcf70SJason Wang     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
3336e790746SPaolo Bonzini 
3346e790746SPaolo Bonzini     /* Flush any MAC and VLAN filter table state */
3356e790746SPaolo Bonzini     n->mac_table.in_use = 0;
3366e790746SPaolo Bonzini     n->mac_table.first_multi = 0;
3376e790746SPaolo Bonzini     n->mac_table.multi_overflow = 0;
3386e790746SPaolo Bonzini     n->mac_table.uni_overflow = 0;
3396e790746SPaolo Bonzini     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
3406e790746SPaolo Bonzini     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
341702d66a8SMichael S. Tsirkin     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
3426e790746SPaolo Bonzini     memset(n->vlans, 0, MAX_VLAN >> 3);
3436e790746SPaolo Bonzini }
3446e790746SPaolo Bonzini 
3456e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
3466e790746SPaolo Bonzini {
3476e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
3486e790746SPaolo Bonzini     if (!nc->peer) {
3496e790746SPaolo Bonzini         return;
3506e790746SPaolo Bonzini     }
3516e790746SPaolo Bonzini 
352d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
3536e790746SPaolo Bonzini }
3546e790746SPaolo Bonzini 
3556e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
3566e790746SPaolo Bonzini {
3576e790746SPaolo Bonzini     return n->has_vnet_hdr;
3586e790746SPaolo Bonzini }
3596e790746SPaolo Bonzini 
3606e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
3616e790746SPaolo Bonzini {
3626e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
3636e790746SPaolo Bonzini         return 0;
3646e790746SPaolo Bonzini 
365d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
3666e790746SPaolo Bonzini 
3676e790746SPaolo Bonzini     return n->has_ufo;
3686e790746SPaolo Bonzini }
3696e790746SPaolo Bonzini 
370bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
371bb9d17f8SCornelia Huck                                        int version_1)
3726e790746SPaolo Bonzini {
3736e790746SPaolo Bonzini     int i;
3746e790746SPaolo Bonzini     NetClientState *nc;
3756e790746SPaolo Bonzini 
3766e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
3776e790746SPaolo Bonzini 
378bb9d17f8SCornelia Huck     if (version_1) {
379bb9d17f8SCornelia Huck         n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
380bb9d17f8SCornelia Huck     } else {
3816e790746SPaolo Bonzini         n->guest_hdr_len = n->mergeable_rx_bufs ?
382bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
383bb9d17f8SCornelia Huck             sizeof(struct virtio_net_hdr);
384bb9d17f8SCornelia Huck     }
3856e790746SPaolo Bonzini 
3866e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
3876e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
3886e790746SPaolo Bonzini 
3896e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
390d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
391d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
3926e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
3936e790746SPaolo Bonzini         }
3946e790746SPaolo Bonzini     }
3956e790746SPaolo Bonzini }
3966e790746SPaolo Bonzini 
3976e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
3986e790746SPaolo Bonzini {
3996e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
4006e790746SPaolo Bonzini 
4016e790746SPaolo Bonzini     if (!nc->peer) {
4026e790746SPaolo Bonzini         return 0;
4036e790746SPaolo Bonzini     }
4046e790746SPaolo Bonzini 
4056e790746SPaolo Bonzini     if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
4066e790746SPaolo Bonzini         return 0;
4076e790746SPaolo Bonzini     }
4086e790746SPaolo Bonzini 
4096e790746SPaolo Bonzini     return tap_enable(nc->peer);
4106e790746SPaolo Bonzini }
4116e790746SPaolo Bonzini 
4126e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
4136e790746SPaolo Bonzini {
4146e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
4156e790746SPaolo Bonzini 
4166e790746SPaolo Bonzini     if (!nc->peer) {
4176e790746SPaolo Bonzini         return 0;
4186e790746SPaolo Bonzini     }
4196e790746SPaolo Bonzini 
4206e790746SPaolo Bonzini     if (nc->peer->info->type !=  NET_CLIENT_OPTIONS_KIND_TAP) {
4216e790746SPaolo Bonzini         return 0;
4226e790746SPaolo Bonzini     }
4236e790746SPaolo Bonzini 
4246e790746SPaolo Bonzini     return tap_disable(nc->peer);
4256e790746SPaolo Bonzini }
4266e790746SPaolo Bonzini 
4276e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n)
4286e790746SPaolo Bonzini {
4296e790746SPaolo Bonzini     int i;
430ddfa83eaSJoel Stanley     int r;
4316e790746SPaolo Bonzini 
4326e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
4336e790746SPaolo Bonzini         if (i < n->curr_queues) {
434ddfa83eaSJoel Stanley             r = peer_attach(n, i);
435ddfa83eaSJoel Stanley             assert(!r);
4366e790746SPaolo Bonzini         } else {
437ddfa83eaSJoel Stanley             r = peer_detach(n, i);
438ddfa83eaSJoel Stanley             assert(!r);
4396e790746SPaolo Bonzini         }
4406e790746SPaolo Bonzini     }
4416e790746SPaolo Bonzini }
4426e790746SPaolo Bonzini 
443ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
4446e790746SPaolo Bonzini 
445019a3edbSGerd Hoffmann static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features)
4466e790746SPaolo Bonzini {
44717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
4486e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
4496e790746SPaolo Bonzini 
450da3e8a23SShannon Zhao     /* Firstly sync all virtio-net possible supported features */
451da3e8a23SShannon Zhao     features |= n->host_features;
452da3e8a23SShannon Zhao 
4530cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
4546e790746SPaolo Bonzini 
4556e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
4560cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
4570cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
4580cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
4590cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
4606e790746SPaolo Bonzini 
4610cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
4620cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
4630cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
4640cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
4656e790746SPaolo Bonzini     }
4666e790746SPaolo Bonzini 
4676e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
4680cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
4690cd09c3aSCornelia Huck         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
4706e790746SPaolo Bonzini     }
4716e790746SPaolo Bonzini 
472ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
473df91055dSCornelia Huck         virtio_add_feature(&features, VIRTIO_F_VERSION_1);
4746e790746SPaolo Bonzini         return features;
4756e790746SPaolo Bonzini     }
476ed8b4afeSNikolay Nikolaev     return vhost_net_get_features(get_vhost_net(nc->peer), features);
4776e790746SPaolo Bonzini }
4786e790746SPaolo Bonzini 
479019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
4806e790746SPaolo Bonzini {
481019a3edbSGerd Hoffmann     uint64_t features = 0;
4826e790746SPaolo Bonzini 
4836e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
4846e790746SPaolo Bonzini      * but also these: */
4850cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
4860cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
4870cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
4880cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
4890cd09c3aSCornelia Huck     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
4906e790746SPaolo Bonzini 
4916e790746SPaolo Bonzini     return features;
4926e790746SPaolo Bonzini }
4936e790746SPaolo Bonzini 
494644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
495644c9858SDmitry Fleytman {
496ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
497644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
498644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
499644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
500644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
501644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
502644c9858SDmitry Fleytman }
503644c9858SDmitry Fleytman 
504644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
505644c9858SDmitry Fleytman {
506644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
507644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
508644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
509644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
510644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
511644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
512644c9858SDmitry Fleytman 
513644c9858SDmitry Fleytman     return guest_offloads_mask & features;
514644c9858SDmitry Fleytman }
515644c9858SDmitry Fleytman 
516644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
517644c9858SDmitry Fleytman {
518644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
519644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
520644c9858SDmitry Fleytman }
521644c9858SDmitry Fleytman 
522d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
5236e790746SPaolo Bonzini {
52417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
5256e790746SPaolo Bonzini     int i;
5266e790746SPaolo Bonzini 
527ef546f12SCornelia Huck     virtio_net_set_multiqueue(n,
528ef546f12SCornelia Huck                               __virtio_has_feature(features, VIRTIO_NET_F_MQ));
5296e790746SPaolo Bonzini 
530ef546f12SCornelia Huck     virtio_net_set_mrg_rx_bufs(n,
531ef546f12SCornelia Huck                                __virtio_has_feature(features,
532bb9d17f8SCornelia Huck                                                     VIRTIO_NET_F_MRG_RXBUF),
533bb9d17f8SCornelia Huck                                __virtio_has_feature(features,
534bb9d17f8SCornelia Huck                                                     VIRTIO_F_VERSION_1));
5356e790746SPaolo Bonzini 
5366e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
537644c9858SDmitry Fleytman         n->curr_guest_offloads =
538644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
539644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
5406e790746SPaolo Bonzini     }
5416e790746SPaolo Bonzini 
5426e790746SPaolo Bonzini     for (i = 0;  i < n->max_queues; i++) {
5436e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
5446e790746SPaolo Bonzini 
545ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
5466e790746SPaolo Bonzini             continue;
5476e790746SPaolo Bonzini         }
548ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
5496e790746SPaolo Bonzini     }
5500b1eaa88SStefan Fritsch 
551ef546f12SCornelia Huck     if (__virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
5520b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
5530b1eaa88SStefan Fritsch     } else {
5540b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
5550b1eaa88SStefan Fritsch     }
5566e790746SPaolo Bonzini }
5576e790746SPaolo Bonzini 
5586e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
5596e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
5606e790746SPaolo Bonzini {
5616e790746SPaolo Bonzini     uint8_t on;
5626e790746SPaolo Bonzini     size_t s;
563b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
5646e790746SPaolo Bonzini 
5656e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
5666e790746SPaolo Bonzini     if (s != sizeof(on)) {
5676e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
5686e790746SPaolo Bonzini     }
5696e790746SPaolo Bonzini 
5706e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
5716e790746SPaolo Bonzini         n->promisc = on;
5726e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
5736e790746SPaolo Bonzini         n->allmulti = on;
5746e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
5756e790746SPaolo Bonzini         n->alluni = on;
5766e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
5776e790746SPaolo Bonzini         n->nomulti = on;
5786e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
5796e790746SPaolo Bonzini         n->nouni = on;
5806e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
5816e790746SPaolo Bonzini         n->nobcast = on;
5826e790746SPaolo Bonzini     } else {
5836e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
5846e790746SPaolo Bonzini     }
5856e790746SPaolo Bonzini 
586b1be4280SAmos Kong     rxfilter_notify(nc);
587b1be4280SAmos Kong 
5886e790746SPaolo Bonzini     return VIRTIO_NET_OK;
5896e790746SPaolo Bonzini }
5906e790746SPaolo Bonzini 
591644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
592644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
593644c9858SDmitry Fleytman {
594644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
595644c9858SDmitry Fleytman     uint64_t offloads;
596644c9858SDmitry Fleytman     size_t s;
597644c9858SDmitry Fleytman 
598ef546f12SCornelia Huck     if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
599644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
600644c9858SDmitry Fleytman     }
601644c9858SDmitry Fleytman 
602644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
603644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
604644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
605644c9858SDmitry Fleytman     }
606644c9858SDmitry Fleytman 
607644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
608644c9858SDmitry Fleytman         uint64_t supported_offloads;
609644c9858SDmitry Fleytman 
610644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
611644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
612644c9858SDmitry Fleytman         }
613644c9858SDmitry Fleytman 
614644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
615644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
616644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
617644c9858SDmitry Fleytman         }
618644c9858SDmitry Fleytman 
619644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
620644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
621644c9858SDmitry Fleytman 
622644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
623644c9858SDmitry Fleytman     } else {
624644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
625644c9858SDmitry Fleytman     }
626644c9858SDmitry Fleytman }
627644c9858SDmitry Fleytman 
6286e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
6296e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
6306e790746SPaolo Bonzini {
6311399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
6326e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
6336e790746SPaolo Bonzini     size_t s;
634b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
6356e790746SPaolo Bonzini 
6366e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
6376e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
6386e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
6396e790746SPaolo Bonzini         }
6406e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
6416e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
6426e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
643b1be4280SAmos Kong         rxfilter_notify(nc);
644b1be4280SAmos Kong 
6456e790746SPaolo Bonzini         return VIRTIO_NET_OK;
6466e790746SPaolo Bonzini     }
6476e790746SPaolo Bonzini 
6486e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
6496e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
6506e790746SPaolo Bonzini     }
6516e790746SPaolo Bonzini 
652cae2e556SAmos Kong     int in_use = 0;
653cae2e556SAmos Kong     int first_multi = 0;
654cae2e556SAmos Kong     uint8_t uni_overflow = 0;
655cae2e556SAmos Kong     uint8_t multi_overflow = 0;
656cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
6576e790746SPaolo Bonzini 
6586e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
6596e790746SPaolo Bonzini                    sizeof(mac_data.entries));
6601399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
6616e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
662b1be4280SAmos Kong         goto error;
6636e790746SPaolo Bonzini     }
6646e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
6656e790746SPaolo Bonzini 
6666e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
667b1be4280SAmos Kong         goto error;
6686e790746SPaolo Bonzini     }
6696e790746SPaolo Bonzini 
6706e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
671cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
6726e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
6736e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
674b1be4280SAmos Kong             goto error;
6756e790746SPaolo Bonzini         }
676cae2e556SAmos Kong         in_use += mac_data.entries;
6776e790746SPaolo Bonzini     } else {
678cae2e556SAmos Kong         uni_overflow = 1;
6796e790746SPaolo Bonzini     }
6806e790746SPaolo Bonzini 
6816e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
6826e790746SPaolo Bonzini 
683cae2e556SAmos Kong     first_multi = in_use;
6846e790746SPaolo Bonzini 
6856e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
6866e790746SPaolo Bonzini                    sizeof(mac_data.entries));
6871399c60dSRusty Russell     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
6886e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
689b1be4280SAmos Kong         goto error;
6906e790746SPaolo Bonzini     }
6916e790746SPaolo Bonzini 
6926e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
6936e790746SPaolo Bonzini 
6946e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
695b1be4280SAmos Kong         goto error;
6966e790746SPaolo Bonzini     }
6976e790746SPaolo Bonzini 
698edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
699cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
7006e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
7016e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
702b1be4280SAmos Kong             goto error;
7036e790746SPaolo Bonzini         }
704cae2e556SAmos Kong         in_use += mac_data.entries;
7056e790746SPaolo Bonzini     } else {
706cae2e556SAmos Kong         multi_overflow = 1;
7076e790746SPaolo Bonzini     }
7086e790746SPaolo Bonzini 
709cae2e556SAmos Kong     n->mac_table.in_use = in_use;
710cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
711cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
712cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
713cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
714cae2e556SAmos Kong     g_free(macs);
715b1be4280SAmos Kong     rxfilter_notify(nc);
716b1be4280SAmos Kong 
7176e790746SPaolo Bonzini     return VIRTIO_NET_OK;
718b1be4280SAmos Kong 
719b1be4280SAmos Kong error:
720cae2e556SAmos Kong     g_free(macs);
721b1be4280SAmos Kong     return VIRTIO_NET_ERR;
7226e790746SPaolo Bonzini }
7236e790746SPaolo Bonzini 
7246e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
7256e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
7266e790746SPaolo Bonzini {
7271399c60dSRusty Russell     VirtIODevice *vdev = VIRTIO_DEVICE(n);
7286e790746SPaolo Bonzini     uint16_t vid;
7296e790746SPaolo Bonzini     size_t s;
730b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
7316e790746SPaolo Bonzini 
7326e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
7331399c60dSRusty Russell     vid = virtio_lduw_p(vdev, &vid);
7346e790746SPaolo Bonzini     if (s != sizeof(vid)) {
7356e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7366e790746SPaolo Bonzini     }
7376e790746SPaolo Bonzini 
7386e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
7396e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7406e790746SPaolo Bonzini 
7416e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
7426e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
7436e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
7446e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
7456e790746SPaolo Bonzini     else
7466e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7476e790746SPaolo Bonzini 
748b1be4280SAmos Kong     rxfilter_notify(nc);
749b1be4280SAmos Kong 
7506e790746SPaolo Bonzini     return VIRTIO_NET_OK;
7516e790746SPaolo Bonzini }
7526e790746SPaolo Bonzini 
753f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
754f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
755f57fcf70SJason Wang {
756f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
757f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
758f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
759f57fcf70SJason Wang         if (n->announce_counter) {
760f57fcf70SJason Wang             timer_mod(n->announce_timer,
761f57fcf70SJason Wang                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
762f57fcf70SJason Wang                       self_announce_delay(n->announce_counter));
763f57fcf70SJason Wang         }
764f57fcf70SJason Wang         return VIRTIO_NET_OK;
765f57fcf70SJason Wang     } else {
766f57fcf70SJason Wang         return VIRTIO_NET_ERR;
767f57fcf70SJason Wang     }
768f57fcf70SJason Wang }
769f57fcf70SJason Wang 
7706e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
7716e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
7726e790746SPaolo Bonzini {
77317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
7746e790746SPaolo Bonzini     struct virtio_net_ctrl_mq mq;
7756e790746SPaolo Bonzini     size_t s;
7766e790746SPaolo Bonzini     uint16_t queues;
7776e790746SPaolo Bonzini 
7786e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
7796e790746SPaolo Bonzini     if (s != sizeof(mq)) {
7806e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7816e790746SPaolo Bonzini     }
7826e790746SPaolo Bonzini 
7836e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
7846e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7856e790746SPaolo Bonzini     }
7866e790746SPaolo Bonzini 
7871399c60dSRusty Russell     queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
7886e790746SPaolo Bonzini 
7896e790746SPaolo Bonzini     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
7906e790746SPaolo Bonzini         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
7916e790746SPaolo Bonzini         queues > n->max_queues ||
7926e790746SPaolo Bonzini         !n->multiqueue) {
7936e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7946e790746SPaolo Bonzini     }
7956e790746SPaolo Bonzini 
7966e790746SPaolo Bonzini     n->curr_queues = queues;
7976e790746SPaolo Bonzini     /* stop the backend before changing the number of queues to avoid handling a
7986e790746SPaolo Bonzini      * disabled queue */
79917a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
8006e790746SPaolo Bonzini     virtio_net_set_queues(n);
8016e790746SPaolo Bonzini 
8026e790746SPaolo Bonzini     return VIRTIO_NET_OK;
8036e790746SPaolo Bonzini }
8046e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
8056e790746SPaolo Bonzini {
80617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
8076e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
8086e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
8096e790746SPaolo Bonzini     VirtQueueElement elem;
8106e790746SPaolo Bonzini     size_t s;
811771b6ed3SJason Wang     struct iovec *iov, *iov2;
8126e790746SPaolo Bonzini     unsigned int iov_cnt;
8136e790746SPaolo Bonzini 
8146e790746SPaolo Bonzini     while (virtqueue_pop(vq, &elem)) {
8156e790746SPaolo Bonzini         if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
8166e790746SPaolo Bonzini             iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
8176e790746SPaolo Bonzini             error_report("virtio-net ctrl missing headers");
8186e790746SPaolo Bonzini             exit(1);
8196e790746SPaolo Bonzini         }
8206e790746SPaolo Bonzini 
8216e790746SPaolo Bonzini         iov_cnt = elem.out_num;
822771b6ed3SJason Wang         iov2 = iov = g_memdup(elem.out_sg, sizeof(struct iovec) * elem.out_num);
8236e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
8246e790746SPaolo Bonzini         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
8256e790746SPaolo Bonzini         if (s != sizeof(ctrl)) {
8266e790746SPaolo Bonzini             status = VIRTIO_NET_ERR;
8276e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
8286e790746SPaolo Bonzini             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
8296e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
8306e790746SPaolo Bonzini             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
8316e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
8326e790746SPaolo Bonzini             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
833f57fcf70SJason Wang         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
834f57fcf70SJason Wang             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
8356e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
8366e790746SPaolo Bonzini             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
837644c9858SDmitry Fleytman         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
838644c9858SDmitry Fleytman             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
8396e790746SPaolo Bonzini         }
8406e790746SPaolo Bonzini 
8416e790746SPaolo Bonzini         s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
8426e790746SPaolo Bonzini         assert(s == sizeof(status));
8436e790746SPaolo Bonzini 
8446e790746SPaolo Bonzini         virtqueue_push(vq, &elem, sizeof(status));
8456e790746SPaolo Bonzini         virtio_notify(vdev, vq);
846771b6ed3SJason Wang         g_free(iov2);
8476e790746SPaolo Bonzini     }
8486e790746SPaolo Bonzini }
8496e790746SPaolo Bonzini 
8506e790746SPaolo Bonzini /* RX */
8516e790746SPaolo Bonzini 
8526e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
8536e790746SPaolo Bonzini {
85417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
8556e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
8566e790746SPaolo Bonzini 
8576e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
8586e790746SPaolo Bonzini }
8596e790746SPaolo Bonzini 
8606e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc)
8616e790746SPaolo Bonzini {
8626e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
86317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
8646e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
8656e790746SPaolo Bonzini 
86617a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
8676e790746SPaolo Bonzini         return 0;
8686e790746SPaolo Bonzini     }
8696e790746SPaolo Bonzini 
8706e790746SPaolo Bonzini     if (nc->queue_index >= n->curr_queues) {
8716e790746SPaolo Bonzini         return 0;
8726e790746SPaolo Bonzini     }
8736e790746SPaolo Bonzini 
8746e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
87517a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
8766e790746SPaolo Bonzini         return 0;
8776e790746SPaolo Bonzini     }
8786e790746SPaolo Bonzini 
8796e790746SPaolo Bonzini     return 1;
8806e790746SPaolo Bonzini }
8816e790746SPaolo Bonzini 
8826e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
8836e790746SPaolo Bonzini {
8846e790746SPaolo Bonzini     VirtIONet *n = q->n;
8856e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
8866e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
8876e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
8886e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
8896e790746SPaolo Bonzini 
8906e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
8916e790746SPaolo Bonzini          * available after the above check but before notification was
8926e790746SPaolo Bonzini          * enabled, check for available buffers again.
8936e790746SPaolo Bonzini          */
8946e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
8956e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
8966e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
8976e790746SPaolo Bonzini             return 0;
8986e790746SPaolo Bonzini         }
8996e790746SPaolo Bonzini     }
9006e790746SPaolo Bonzini 
9016e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
9026e790746SPaolo Bonzini     return 1;
9036e790746SPaolo Bonzini }
9046e790746SPaolo Bonzini 
9051399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
906032a74a1SCédric Le Goater {
9071399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->hdr_len);
9081399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->gso_size);
9091399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_start);
9101399c60dSRusty Russell     virtio_tswap16s(vdev, &hdr->csum_offset);
911032a74a1SCédric Le Goater }
912032a74a1SCédric Le Goater 
9136e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
9146e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
9156e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
9166e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
9176e790746SPaolo Bonzini  * dhclient yet.
9186e790746SPaolo Bonzini  *
9196e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
9206e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
9216e790746SPaolo Bonzini  * kernels.
9226e790746SPaolo Bonzini  *
9236e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
9246e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
9256e790746SPaolo Bonzini  * cache.
9266e790746SPaolo Bonzini  */
9276e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
9286e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
9296e790746SPaolo Bonzini {
9306e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
9316e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
9326e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
9336e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
9346e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
9356e790746SPaolo Bonzini         net_checksum_calculate(buf, size);
9366e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
9376e790746SPaolo Bonzini     }
9386e790746SPaolo Bonzini }
9396e790746SPaolo Bonzini 
9406e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
9416e790746SPaolo Bonzini                            const void *buf, size_t size)
9426e790746SPaolo Bonzini {
9436e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
9446e790746SPaolo Bonzini         /* FIXME this cast is evil */
9456e790746SPaolo Bonzini         void *wbuf = (void *)buf;
9466e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
9476e790746SPaolo Bonzini                                     size - n->host_hdr_len);
9481399c60dSRusty Russell         virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
9496e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
9506e790746SPaolo Bonzini     } else {
9516e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
9526e790746SPaolo Bonzini             .flags = 0,
9536e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
9546e790746SPaolo Bonzini         };
9556e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
9566e790746SPaolo Bonzini     }
9576e790746SPaolo Bonzini }
9586e790746SPaolo Bonzini 
9596e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
9606e790746SPaolo Bonzini {
9616e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
9626e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
9636e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
9646e790746SPaolo Bonzini     int i;
9656e790746SPaolo Bonzini 
9666e790746SPaolo Bonzini     if (n->promisc)
9676e790746SPaolo Bonzini         return 1;
9686e790746SPaolo Bonzini 
9696e790746SPaolo Bonzini     ptr += n->host_hdr_len;
9706e790746SPaolo Bonzini 
9716e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
9726e790746SPaolo Bonzini         int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
9736e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
9746e790746SPaolo Bonzini             return 0;
9756e790746SPaolo Bonzini     }
9766e790746SPaolo Bonzini 
9776e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
9786e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
9796e790746SPaolo Bonzini             return !n->nobcast;
9806e790746SPaolo Bonzini         } else if (n->nomulti) {
9816e790746SPaolo Bonzini             return 0;
9826e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
9836e790746SPaolo Bonzini             return 1;
9846e790746SPaolo Bonzini         }
9856e790746SPaolo Bonzini 
9866e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
9876e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
9886e790746SPaolo Bonzini                 return 1;
9896e790746SPaolo Bonzini             }
9906e790746SPaolo Bonzini         }
9916e790746SPaolo Bonzini     } else { // unicast
9926e790746SPaolo Bonzini         if (n->nouni) {
9936e790746SPaolo Bonzini             return 0;
9946e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
9956e790746SPaolo Bonzini             return 1;
9966e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
9976e790746SPaolo Bonzini             return 1;
9986e790746SPaolo Bonzini         }
9996e790746SPaolo Bonzini 
10006e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
10016e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
10026e790746SPaolo Bonzini                 return 1;
10036e790746SPaolo Bonzini             }
10046e790746SPaolo Bonzini         }
10056e790746SPaolo Bonzini     }
10066e790746SPaolo Bonzini 
10076e790746SPaolo Bonzini     return 0;
10086e790746SPaolo Bonzini }
10096e790746SPaolo Bonzini 
10106e790746SPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
10116e790746SPaolo Bonzini {
10126e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
10136e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
101417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
10156e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
10166e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
10176e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
10186e790746SPaolo Bonzini     size_t offset, i, guest_offset;
10196e790746SPaolo Bonzini 
10206e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
10216e790746SPaolo Bonzini         return -1;
10226e790746SPaolo Bonzini     }
10236e790746SPaolo Bonzini 
10246e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
10256e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
10266e790746SPaolo Bonzini         return 0;
10276e790746SPaolo Bonzini     }
10286e790746SPaolo Bonzini 
10296e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
10306e790746SPaolo Bonzini         return size;
10316e790746SPaolo Bonzini 
10326e790746SPaolo Bonzini     offset = i = 0;
10336e790746SPaolo Bonzini 
10346e790746SPaolo Bonzini     while (offset < size) {
10356e790746SPaolo Bonzini         VirtQueueElement elem;
10366e790746SPaolo Bonzini         int len, total;
10376e790746SPaolo Bonzini         const struct iovec *sg = elem.in_sg;
10386e790746SPaolo Bonzini 
10396e790746SPaolo Bonzini         total = 0;
10406e790746SPaolo Bonzini 
10416e790746SPaolo Bonzini         if (virtqueue_pop(q->rx_vq, &elem) == 0) {
10426e790746SPaolo Bonzini             if (i == 0)
10436e790746SPaolo Bonzini                 return -1;
10446e790746SPaolo Bonzini             error_report("virtio-net unexpected empty queue: "
10456e790746SPaolo Bonzini                          "i %zd mergeable %d offset %zd, size %zd, "
1046019a3edbSGerd Hoffmann                          "guest hdr len %zd, host hdr len %zd "
1047019a3edbSGerd Hoffmann                          "guest features 0x%" PRIx64,
10486e790746SPaolo Bonzini                          i, n->mergeable_rx_bufs, offset, size,
1049019a3edbSGerd Hoffmann                          n->guest_hdr_len, n->host_hdr_len,
1050019a3edbSGerd Hoffmann                          vdev->guest_features);
10516e790746SPaolo Bonzini             exit(1);
10526e790746SPaolo Bonzini         }
10536e790746SPaolo Bonzini 
10546e790746SPaolo Bonzini         if (elem.in_num < 1) {
10556e790746SPaolo Bonzini             error_report("virtio-net receive queue contains no in buffers");
10566e790746SPaolo Bonzini             exit(1);
10576e790746SPaolo Bonzini         }
10586e790746SPaolo Bonzini 
10596e790746SPaolo Bonzini         if (i == 0) {
10606e790746SPaolo Bonzini             assert(offset == 0);
10616e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
10626e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
10636e790746SPaolo Bonzini                                     sg, elem.in_num,
10646e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
10656e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
10666e790746SPaolo Bonzini             }
10676e790746SPaolo Bonzini 
10686e790746SPaolo Bonzini             receive_header(n, sg, elem.in_num, buf, size);
10696e790746SPaolo Bonzini             offset = n->host_hdr_len;
10706e790746SPaolo Bonzini             total += n->guest_hdr_len;
10716e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
10726e790746SPaolo Bonzini         } else {
10736e790746SPaolo Bonzini             guest_offset = 0;
10746e790746SPaolo Bonzini         }
10756e790746SPaolo Bonzini 
10766e790746SPaolo Bonzini         /* copy in packet.  ugh */
10776e790746SPaolo Bonzini         len = iov_from_buf(sg, elem.in_num, guest_offset,
10786e790746SPaolo Bonzini                            buf + offset, size - offset);
10796e790746SPaolo Bonzini         total += len;
10806e790746SPaolo Bonzini         offset += len;
10816e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
10826e790746SPaolo Bonzini          * must have consumed the complete packet.
10836e790746SPaolo Bonzini          * Otherwise, drop it. */
10846e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
10856e790746SPaolo Bonzini #if 0
10866e790746SPaolo Bonzini             error_report("virtio-net truncated non-mergeable packet: "
10876e790746SPaolo Bonzini                          "i %zd mergeable %d offset %zd, size %zd, "
10886e790746SPaolo Bonzini                          "guest hdr len %zd, host hdr len %zd",
10896e790746SPaolo Bonzini                          i, n->mergeable_rx_bufs,
10906e790746SPaolo Bonzini                          offset, size, n->guest_hdr_len, n->host_hdr_len);
10916e790746SPaolo Bonzini #endif
10926e790746SPaolo Bonzini             return size;
10936e790746SPaolo Bonzini         }
10946e790746SPaolo Bonzini 
10956e790746SPaolo Bonzini         /* signal other side */
10966e790746SPaolo Bonzini         virtqueue_fill(q->rx_vq, &elem, total, i++);
10976e790746SPaolo Bonzini     }
10986e790746SPaolo Bonzini 
10996e790746SPaolo Bonzini     if (mhdr_cnt) {
11001399c60dSRusty Russell         virtio_stw_p(vdev, &mhdr.num_buffers, i);
11016e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
11026e790746SPaolo Bonzini                      0,
11036e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
11046e790746SPaolo Bonzini     }
11056e790746SPaolo Bonzini 
11066e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
110717a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
11086e790746SPaolo Bonzini 
11096e790746SPaolo Bonzini     return size;
11106e790746SPaolo Bonzini }
11116e790746SPaolo Bonzini 
11126e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
11136e790746SPaolo Bonzini 
11146e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
11156e790746SPaolo Bonzini {
11166e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
11176e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
111817a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11196e790746SPaolo Bonzini 
11206e790746SPaolo Bonzini     virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
112117a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
11226e790746SPaolo Bonzini 
11236e790746SPaolo Bonzini     q->async_tx.elem.out_num = q->async_tx.len = 0;
11246e790746SPaolo Bonzini 
11256e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
11266e790746SPaolo Bonzini     virtio_net_flush_tx(q);
11276e790746SPaolo Bonzini }
11286e790746SPaolo Bonzini 
11296e790746SPaolo Bonzini /* TX */
11306e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
11316e790746SPaolo Bonzini {
11326e790746SPaolo Bonzini     VirtIONet *n = q->n;
113317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11346e790746SPaolo Bonzini     VirtQueueElement elem;
11356e790746SPaolo Bonzini     int32_t num_packets = 0;
11366e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
113717a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
11386e790746SPaolo Bonzini         return num_packets;
11396e790746SPaolo Bonzini     }
11406e790746SPaolo Bonzini 
11416e790746SPaolo Bonzini     if (q->async_tx.elem.out_num) {
11426e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
11436e790746SPaolo Bonzini         return num_packets;
11446e790746SPaolo Bonzini     }
11456e790746SPaolo Bonzini 
11466e790746SPaolo Bonzini     while (virtqueue_pop(q->tx_vq, &elem)) {
11476e790746SPaolo Bonzini         ssize_t ret, len;
11486e790746SPaolo Bonzini         unsigned int out_num = elem.out_num;
11496e790746SPaolo Bonzini         struct iovec *out_sg = &elem.out_sg[0];
11506e790746SPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE];
11516e790746SPaolo Bonzini 
11526e790746SPaolo Bonzini         if (out_num < 1) {
11536e790746SPaolo Bonzini             error_report("virtio-net header not in first element");
11546e790746SPaolo Bonzini             exit(1);
11556e790746SPaolo Bonzini         }
11566e790746SPaolo Bonzini 
1157032a74a1SCédric Le Goater         if (n->has_vnet_hdr) {
1158032a74a1SCédric Le Goater             if (out_sg[0].iov_len < n->guest_hdr_len) {
1159032a74a1SCédric Le Goater                 error_report("virtio-net header incorrect");
1160032a74a1SCédric Le Goater                 exit(1);
1161032a74a1SCédric Le Goater             }
11621399c60dSRusty Russell             virtio_net_hdr_swap(vdev, (void *) out_sg[0].iov_base);
1163032a74a1SCédric Le Goater         }
1164032a74a1SCédric Le Goater 
11656e790746SPaolo Bonzini         /*
11666e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
11676e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
11686e790746SPaolo Bonzini          * that host is interested in.
11696e790746SPaolo Bonzini          */
11706e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
11716e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
11726e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
11736e790746SPaolo Bonzini                                        out_sg, out_num,
11746e790746SPaolo Bonzini                                        0, n->host_hdr_len);
11756e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
11766e790746SPaolo Bonzini                              out_sg, out_num,
11776e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
11786e790746SPaolo Bonzini             out_num = sg_num;
11796e790746SPaolo Bonzini             out_sg = sg;
11806e790746SPaolo Bonzini         }
11816e790746SPaolo Bonzini 
11826e790746SPaolo Bonzini         len = n->guest_hdr_len;
11836e790746SPaolo Bonzini 
11846e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
11856e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
11866e790746SPaolo Bonzini         if (ret == 0) {
11876e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
11886e790746SPaolo Bonzini             q->async_tx.elem = elem;
11896e790746SPaolo Bonzini             q->async_tx.len  = len;
11906e790746SPaolo Bonzini             return -EBUSY;
11916e790746SPaolo Bonzini         }
11926e790746SPaolo Bonzini 
11936e790746SPaolo Bonzini         len += ret;
11946e790746SPaolo Bonzini 
11956e790746SPaolo Bonzini         virtqueue_push(q->tx_vq, &elem, 0);
119617a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
11976e790746SPaolo Bonzini 
11986e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
11996e790746SPaolo Bonzini             break;
12006e790746SPaolo Bonzini         }
12016e790746SPaolo Bonzini     }
12026e790746SPaolo Bonzini     return num_packets;
12036e790746SPaolo Bonzini }
12046e790746SPaolo Bonzini 
12056e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
12066e790746SPaolo Bonzini {
120717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
12086e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
12096e790746SPaolo Bonzini 
12106e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
121117a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
12126e790746SPaolo Bonzini         q->tx_waiting = 1;
12136e790746SPaolo Bonzini         return;
12146e790746SPaolo Bonzini     }
12156e790746SPaolo Bonzini 
12166e790746SPaolo Bonzini     if (q->tx_waiting) {
12176e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 1);
1218bc72ad67SAlex Bligh         timer_del(q->tx_timer);
12196e790746SPaolo Bonzini         q->tx_waiting = 0;
12206e790746SPaolo Bonzini         virtio_net_flush_tx(q);
12216e790746SPaolo Bonzini     } else {
1222bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
1223bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
12246e790746SPaolo Bonzini         q->tx_waiting = 1;
12256e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
12266e790746SPaolo Bonzini     }
12276e790746SPaolo Bonzini }
12286e790746SPaolo Bonzini 
12296e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
12306e790746SPaolo Bonzini {
123117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
12326e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
12336e790746SPaolo Bonzini 
12346e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
12356e790746SPaolo Bonzini         return;
12366e790746SPaolo Bonzini     }
12376e790746SPaolo Bonzini     q->tx_waiting = 1;
12386e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
123917a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
12406e790746SPaolo Bonzini         return;
12416e790746SPaolo Bonzini     }
12426e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
12436e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
12446e790746SPaolo Bonzini }
12456e790746SPaolo Bonzini 
12466e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
12476e790746SPaolo Bonzini {
12486e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
12496e790746SPaolo Bonzini     VirtIONet *n = q->n;
125017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1251e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
1252e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
1253e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
1254e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
1255e8bcf842SMichael S. Tsirkin         return;
1256e8bcf842SMichael S. Tsirkin     }
12576e790746SPaolo Bonzini 
12586e790746SPaolo Bonzini     q->tx_waiting = 0;
12596e790746SPaolo Bonzini 
12606e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
126117a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
12626e790746SPaolo Bonzini         return;
126317a0ca55SKONRAD Frederic     }
12646e790746SPaolo Bonzini 
12656e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
12666e790746SPaolo Bonzini     virtio_net_flush_tx(q);
12676e790746SPaolo Bonzini }
12686e790746SPaolo Bonzini 
12696e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
12706e790746SPaolo Bonzini {
12716e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
12726e790746SPaolo Bonzini     VirtIONet *n = q->n;
127317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12746e790746SPaolo Bonzini     int32_t ret;
12756e790746SPaolo Bonzini 
1276e8bcf842SMichael S. Tsirkin     /* This happens when device was stopped but BH wasn't. */
1277e8bcf842SMichael S. Tsirkin     if (!vdev->vm_running) {
1278e8bcf842SMichael S. Tsirkin         /* Make sure tx waiting is set, so we'll run when restarted. */
1279e8bcf842SMichael S. Tsirkin         assert(q->tx_waiting);
1280e8bcf842SMichael S. Tsirkin         return;
1281e8bcf842SMichael S. Tsirkin     }
12826e790746SPaolo Bonzini 
12836e790746SPaolo Bonzini     q->tx_waiting = 0;
12846e790746SPaolo Bonzini 
12856e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
128617a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
12876e790746SPaolo Bonzini         return;
128817a0ca55SKONRAD Frederic     }
12896e790746SPaolo Bonzini 
12906e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
12916e790746SPaolo Bonzini     if (ret == -EBUSY) {
12926e790746SPaolo Bonzini         return; /* Notification re-enable handled by tx_complete */
12936e790746SPaolo Bonzini     }
12946e790746SPaolo Bonzini 
12956e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
12966e790746SPaolo Bonzini      * more coming and immediately reschedule */
12976e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
12986e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
12996e790746SPaolo Bonzini         q->tx_waiting = 1;
13006e790746SPaolo Bonzini         return;
13016e790746SPaolo Bonzini     }
13026e790746SPaolo Bonzini 
13036e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
13046e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
13056e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
13066e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
13076e790746SPaolo Bonzini     if (virtio_net_flush_tx(q) > 0) {
13086e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
13096e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
13106e790746SPaolo Bonzini         q->tx_waiting = 1;
13116e790746SPaolo Bonzini     }
13126e790746SPaolo Bonzini }
13136e790746SPaolo Bonzini 
1314ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
13156e790746SPaolo Bonzini {
13166e790746SPaolo Bonzini     n->multiqueue = multiqueue;
13176e790746SPaolo Bonzini 
13186e790746SPaolo Bonzini     virtio_net_set_queues(n);
13196e790746SPaolo Bonzini }
13206e790746SPaolo Bonzini 
13216e790746SPaolo Bonzini static void virtio_net_save(QEMUFile *f, void *opaque)
13226e790746SPaolo Bonzini {
13236e790746SPaolo Bonzini     VirtIONet *n = opaque;
132417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13256e790746SPaolo Bonzini 
13266e790746SPaolo Bonzini     /* At this point, backend must be stopped, otherwise
13276e790746SPaolo Bonzini      * it might keep writing to memory. */
13286e790746SPaolo Bonzini     assert(!n->vhost_started);
132917a0ca55SKONRAD Frederic     virtio_save(vdev, f);
1330037dab2fSGreg Kurz }
1331037dab2fSGreg Kurz 
1332037dab2fSGreg Kurz static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f)
1333037dab2fSGreg Kurz {
1334037dab2fSGreg Kurz     VirtIONet *n = VIRTIO_NET(vdev);
1335037dab2fSGreg Kurz     int i;
13366e790746SPaolo Bonzini 
13376e790746SPaolo Bonzini     qemu_put_buffer(f, n->mac, ETH_ALEN);
13386e790746SPaolo Bonzini     qemu_put_be32(f, n->vqs[0].tx_waiting);
13396e790746SPaolo Bonzini     qemu_put_be32(f, n->mergeable_rx_bufs);
13406e790746SPaolo Bonzini     qemu_put_be16(f, n->status);
13416e790746SPaolo Bonzini     qemu_put_byte(f, n->promisc);
13426e790746SPaolo Bonzini     qemu_put_byte(f, n->allmulti);
13436e790746SPaolo Bonzini     qemu_put_be32(f, n->mac_table.in_use);
13446e790746SPaolo Bonzini     qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
13456e790746SPaolo Bonzini     qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
13466e790746SPaolo Bonzini     qemu_put_be32(f, n->has_vnet_hdr);
13476e790746SPaolo Bonzini     qemu_put_byte(f, n->mac_table.multi_overflow);
13486e790746SPaolo Bonzini     qemu_put_byte(f, n->mac_table.uni_overflow);
13496e790746SPaolo Bonzini     qemu_put_byte(f, n->alluni);
13506e790746SPaolo Bonzini     qemu_put_byte(f, n->nomulti);
13516e790746SPaolo Bonzini     qemu_put_byte(f, n->nouni);
13526e790746SPaolo Bonzini     qemu_put_byte(f, n->nobcast);
13536e790746SPaolo Bonzini     qemu_put_byte(f, n->has_ufo);
13546e790746SPaolo Bonzini     if (n->max_queues > 1) {
13556e790746SPaolo Bonzini         qemu_put_be16(f, n->max_queues);
13566e790746SPaolo Bonzini         qemu_put_be16(f, n->curr_queues);
13576e790746SPaolo Bonzini         for (i = 1; i < n->curr_queues; i++) {
13586e790746SPaolo Bonzini             qemu_put_be32(f, n->vqs[i].tx_waiting);
13596e790746SPaolo Bonzini         }
13606e790746SPaolo Bonzini     }
1361644c9858SDmitry Fleytman 
1362ef546f12SCornelia Huck     if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1363644c9858SDmitry Fleytman         qemu_put_be64(f, n->curr_guest_offloads);
1364644c9858SDmitry Fleytman     }
13656e790746SPaolo Bonzini }
13666e790746SPaolo Bonzini 
13676e790746SPaolo Bonzini static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
13686e790746SPaolo Bonzini {
13696e790746SPaolo Bonzini     VirtIONet *n = opaque;
137017a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13716e790746SPaolo Bonzini 
13726e790746SPaolo Bonzini     if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
13736e790746SPaolo Bonzini         return -EINVAL;
13746e790746SPaolo Bonzini 
1375037dab2fSGreg Kurz     return virtio_load(vdev, f, version_id);
13766e790746SPaolo Bonzini }
13776e790746SPaolo Bonzini 
1378037dab2fSGreg Kurz static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
1379037dab2fSGreg Kurz                                   int version_id)
1380037dab2fSGreg Kurz {
1381037dab2fSGreg Kurz     VirtIONet *n = VIRTIO_NET(vdev);
1382037dab2fSGreg Kurz     int i, link_down;
1383037dab2fSGreg Kurz 
13846e790746SPaolo Bonzini     qemu_get_buffer(f, n->mac, ETH_ALEN);
13856e790746SPaolo Bonzini     n->vqs[0].tx_waiting = qemu_get_be32(f);
13866e790746SPaolo Bonzini 
1387bb9d17f8SCornelia Huck     virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f),
1388bb9d17f8SCornelia Huck                                virtio_has_feature(vdev, VIRTIO_F_VERSION_1));
13896e790746SPaolo Bonzini 
13906e790746SPaolo Bonzini     if (version_id >= 3)
13916e790746SPaolo Bonzini         n->status = qemu_get_be16(f);
13926e790746SPaolo Bonzini 
13936e790746SPaolo Bonzini     if (version_id >= 4) {
13946e790746SPaolo Bonzini         if (version_id < 8) {
13956e790746SPaolo Bonzini             n->promisc = qemu_get_be32(f);
13966e790746SPaolo Bonzini             n->allmulti = qemu_get_be32(f);
13976e790746SPaolo Bonzini         } else {
13986e790746SPaolo Bonzini             n->promisc = qemu_get_byte(f);
13996e790746SPaolo Bonzini             n->allmulti = qemu_get_byte(f);
14006e790746SPaolo Bonzini         }
14016e790746SPaolo Bonzini     }
14026e790746SPaolo Bonzini 
14036e790746SPaolo Bonzini     if (version_id >= 5) {
14046e790746SPaolo Bonzini         n->mac_table.in_use = qemu_get_be32(f);
14056e790746SPaolo Bonzini         /* MAC_TABLE_ENTRIES may be different from the saved image */
14066e790746SPaolo Bonzini         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
14076e790746SPaolo Bonzini             qemu_get_buffer(f, n->mac_table.macs,
14086e790746SPaolo Bonzini                             n->mac_table.in_use * ETH_ALEN);
140998f93dddSMichael S. Tsirkin         } else {
141098f93dddSMichael S. Tsirkin             int64_t i;
141198f93dddSMichael S. Tsirkin 
141298f93dddSMichael S. Tsirkin             /* Overflow detected - can happen if source has a larger MAC table.
141398f93dddSMichael S. Tsirkin              * We simply set overflow flag so there's no need to maintain the
141498f93dddSMichael S. Tsirkin              * table of addresses, discard them all.
141598f93dddSMichael S. Tsirkin              * Note: 64 bit math to avoid integer overflow.
141698f93dddSMichael S. Tsirkin              */
141798f93dddSMichael S. Tsirkin             for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
141898f93dddSMichael S. Tsirkin                 qemu_get_byte(f);
141998f93dddSMichael S. Tsirkin             }
14206e790746SPaolo Bonzini             n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
14216e790746SPaolo Bonzini             n->mac_table.in_use = 0;
14226e790746SPaolo Bonzini         }
14236e790746SPaolo Bonzini     }
14246e790746SPaolo Bonzini 
14256e790746SPaolo Bonzini     if (version_id >= 6)
14266e790746SPaolo Bonzini         qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
14276e790746SPaolo Bonzini 
14286e790746SPaolo Bonzini     if (version_id >= 7) {
14296e790746SPaolo Bonzini         if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
14306e790746SPaolo Bonzini             error_report("virtio-net: saved image requires vnet_hdr=on");
14316e790746SPaolo Bonzini             return -1;
14326e790746SPaolo Bonzini         }
14336e790746SPaolo Bonzini     }
14346e790746SPaolo Bonzini 
14356e790746SPaolo Bonzini     if (version_id >= 9) {
14366e790746SPaolo Bonzini         n->mac_table.multi_overflow = qemu_get_byte(f);
14376e790746SPaolo Bonzini         n->mac_table.uni_overflow = qemu_get_byte(f);
14386e790746SPaolo Bonzini     }
14396e790746SPaolo Bonzini 
14406e790746SPaolo Bonzini     if (version_id >= 10) {
14416e790746SPaolo Bonzini         n->alluni = qemu_get_byte(f);
14426e790746SPaolo Bonzini         n->nomulti = qemu_get_byte(f);
14436e790746SPaolo Bonzini         n->nouni = qemu_get_byte(f);
14446e790746SPaolo Bonzini         n->nobcast = qemu_get_byte(f);
14456e790746SPaolo Bonzini     }
14466e790746SPaolo Bonzini 
14476e790746SPaolo Bonzini     if (version_id >= 11) {
14486e790746SPaolo Bonzini         if (qemu_get_byte(f) && !peer_has_ufo(n)) {
14496e790746SPaolo Bonzini             error_report("virtio-net: saved image requires TUN_F_UFO support");
14506e790746SPaolo Bonzini             return -1;
14516e790746SPaolo Bonzini         }
14526e790746SPaolo Bonzini     }
14536e790746SPaolo Bonzini 
14546e790746SPaolo Bonzini     if (n->max_queues > 1) {
14556e790746SPaolo Bonzini         if (n->max_queues != qemu_get_be16(f)) {
14566e790746SPaolo Bonzini             error_report("virtio-net: different max_queues ");
14576e790746SPaolo Bonzini             return -1;
14586e790746SPaolo Bonzini         }
14596e790746SPaolo Bonzini 
14606e790746SPaolo Bonzini         n->curr_queues = qemu_get_be16(f);
1461eea750a5SMichael S. Tsirkin         if (n->curr_queues > n->max_queues) {
1462eea750a5SMichael S. Tsirkin             error_report("virtio-net: curr_queues %x > max_queues %x",
1463eea750a5SMichael S. Tsirkin                          n->curr_queues, n->max_queues);
1464eea750a5SMichael S. Tsirkin             return -1;
1465eea750a5SMichael S. Tsirkin         }
14666e790746SPaolo Bonzini         for (i = 1; i < n->curr_queues; i++) {
14676e790746SPaolo Bonzini             n->vqs[i].tx_waiting = qemu_get_be32(f);
14686e790746SPaolo Bonzini         }
14696e790746SPaolo Bonzini     }
14706e790746SPaolo Bonzini 
1471ef546f12SCornelia Huck     if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1472644c9858SDmitry Fleytman         n->curr_guest_offloads = qemu_get_be64(f);
1473644c9858SDmitry Fleytman     } else {
1474644c9858SDmitry Fleytman         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1475644c9858SDmitry Fleytman     }
1476644c9858SDmitry Fleytman 
1477644c9858SDmitry Fleytman     if (peer_has_vnet_hdr(n)) {
1478644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1479644c9858SDmitry Fleytman     }
1480644c9858SDmitry Fleytman 
14816e790746SPaolo Bonzini     virtio_net_set_queues(n);
14826e790746SPaolo Bonzini 
14836e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
14846e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
14856e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
14866e790746SPaolo Bonzini             break;
14876e790746SPaolo Bonzini         }
14886e790746SPaolo Bonzini     }
14896e790746SPaolo Bonzini     n->mac_table.first_multi = i;
14906e790746SPaolo Bonzini 
14916e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
14926e790746SPaolo Bonzini      * to link status bit in n->status */
14936e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
14946e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
14956e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
14966e790746SPaolo Bonzini     }
14976e790746SPaolo Bonzini 
1498ef546f12SCornelia Huck     if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1499ef546f12SCornelia Huck         virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
1500f57fcf70SJason Wang         n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1501f57fcf70SJason Wang         timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1502f57fcf70SJason Wang     }
1503f57fcf70SJason Wang 
15046e790746SPaolo Bonzini     return 0;
15056e790746SPaolo Bonzini }
15066e790746SPaolo Bonzini 
15076e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
15086e790746SPaolo Bonzini     .type = NET_CLIENT_OPTIONS_KIND_NIC,
15096e790746SPaolo Bonzini     .size = sizeof(NICState),
15106e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
15116e790746SPaolo Bonzini     .receive = virtio_net_receive,
15126e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
1513b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
15146e790746SPaolo Bonzini };
15156e790746SPaolo Bonzini 
15166e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
15176e790746SPaolo Bonzini {
151817a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15196e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
15206e790746SPaolo Bonzini     assert(n->vhost_started);
1521ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
15226e790746SPaolo Bonzini }
15236e790746SPaolo Bonzini 
15246e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
15256e790746SPaolo Bonzini                                            bool mask)
15266e790746SPaolo Bonzini {
152717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15286e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
15296e790746SPaolo Bonzini     assert(n->vhost_started);
1530ed8b4afeSNikolay Nikolaev     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
15316e790746SPaolo Bonzini                              vdev, idx, mask);
15326e790746SPaolo Bonzini }
15336e790746SPaolo Bonzini 
1534019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
15356e790746SPaolo Bonzini {
15366e790746SPaolo Bonzini     int i, config_size = 0;
15370cd09c3aSCornelia Huck     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
15386e790746SPaolo Bonzini     for (i = 0; feature_sizes[i].flags != 0; i++) {
15396e790746SPaolo Bonzini         if (host_features & feature_sizes[i].flags) {
15406e790746SPaolo Bonzini             config_size = MAX(feature_sizes[i].end, config_size);
15416e790746SPaolo Bonzini         }
15426e790746SPaolo Bonzini     }
154317ec5a86SKONRAD Frederic     n->config_size = config_size;
154417ec5a86SKONRAD Frederic }
15456e790746SPaolo Bonzini 
15468a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
15478a253ec2SKONRAD Frederic                                    const char *type)
15488a253ec2SKONRAD Frederic {
15498a253ec2SKONRAD Frederic     /*
15508a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
15518a253ec2SKONRAD Frederic      */
15528a253ec2SKONRAD Frederic     assert(type != NULL);
15538a253ec2SKONRAD Frederic 
15548a253ec2SKONRAD Frederic     g_free(n->netclient_name);
15558a253ec2SKONRAD Frederic     g_free(n->netclient_type);
15568a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
15578a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
15588a253ec2SKONRAD Frederic }
15598a253ec2SKONRAD Frederic 
1560e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
156117ec5a86SKONRAD Frederic {
1562e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1563284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
1564284a32f0SAndreas Färber     NetClientState *nc;
15651773d9eeSKONRAD Frederic     int i;
156617ec5a86SKONRAD Frederic 
1567da3e8a23SShannon Zhao     virtio_net_set_config_size(n, n->host_features);
1568284a32f0SAndreas Färber     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
156917ec5a86SKONRAD Frederic 
1570575a1c0eSJiri Pirko     n->max_queues = MAX(n->nic_conf.peers.queues, 1);
157187b3bd1cSJason Wang     if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
15727e0e736eSJason Wang         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1573631b22eaSStefan Weil                    "must be a positive integer less than %d.",
157487b3bd1cSJason Wang                    n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
15757e0e736eSJason Wang         virtio_cleanup(vdev);
15767e0e736eSJason Wang         return;
15777e0e736eSJason Wang     }
15786e790746SPaolo Bonzini     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
15796e790746SPaolo Bonzini     n->curr_queues = 1;
15801773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
15816e790746SPaolo Bonzini 
15821773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
15831773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
15846e790746SPaolo Bonzini         error_report("virtio-net: "
15856e790746SPaolo Bonzini                      "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
15861773d9eeSKONRAD Frederic                      n->net_conf.tx);
15876e790746SPaolo Bonzini         error_report("Defaulting to \"bh\"");
15886e790746SPaolo Bonzini     }
15896e790746SPaolo Bonzini 
1590da51a335SJason Wang     for (i = 0; i < n->max_queues; i++) {
1591da51a335SJason Wang         n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
15921773d9eeSKONRAD Frederic         if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1593da51a335SJason Wang             n->vqs[i].tx_vq =
1594da51a335SJason Wang                 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1595da51a335SJason Wang             n->vqs[i].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1596da51a335SJason Wang                                               virtio_net_tx_timer,
1597da51a335SJason Wang                                               &n->vqs[i]);
15986e790746SPaolo Bonzini         } else {
1599da51a335SJason Wang             n->vqs[i].tx_vq =
1600da51a335SJason Wang                 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1601da51a335SJason Wang             n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]);
16026e790746SPaolo Bonzini         }
1603da51a335SJason Wang 
1604da51a335SJason Wang         n->vqs[i].tx_waiting = 0;
1605da51a335SJason Wang         n->vqs[i].n = n;
1606da51a335SJason Wang     }
1607da51a335SJason Wang 
160817a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
16091773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
16101773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
16116e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
1612f57fcf70SJason Wang     n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1613f57fcf70SJason Wang                                      virtio_net_announce_timer, n);
16146e790746SPaolo Bonzini 
16158a253ec2SKONRAD Frederic     if (n->netclient_type) {
16168a253ec2SKONRAD Frederic         /*
16178a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
16188a253ec2SKONRAD Frederic          */
16198a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
16208a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
16218a253ec2SKONRAD Frederic     } else {
16221773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1623284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
16248a253ec2SKONRAD Frederic     }
16258a253ec2SKONRAD Frederic 
16266e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
16276e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
16286e790746SPaolo Bonzini         for (i = 0; i < n->max_queues; i++) {
1629d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
16306e790746SPaolo Bonzini         }
16316e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
16326e790746SPaolo Bonzini     } else {
16336e790746SPaolo Bonzini         n->host_hdr_len = 0;
16346e790746SPaolo Bonzini     }
16356e790746SPaolo Bonzini 
16361773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
16376e790746SPaolo Bonzini 
16386e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
16391773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
1640bb9d17f8SCornelia Huck     virtio_net_set_mrg_rx_bufs(n, 0, 0);
16416e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
16426e790746SPaolo Bonzini 
16436e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
16446e790746SPaolo Bonzini 
16456e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
16466e790746SPaolo Bonzini 
1647b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
1648b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
1649b1be4280SAmos Kong 
1650284a32f0SAndreas Färber     n->qdev = dev;
1651284a32f0SAndreas Färber     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
16526e790746SPaolo Bonzini                     virtio_net_save, virtio_net_load, n);
165317ec5a86SKONRAD Frederic }
165417ec5a86SKONRAD Frederic 
1655306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
165617ec5a86SKONRAD Frederic {
1657306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1658306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
165917ec5a86SKONRAD Frederic     int i;
166017ec5a86SKONRAD Frederic 
166117ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
166217ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
166317ec5a86SKONRAD Frederic 
1664306ec6c3SAndreas Färber     unregister_savevm(dev, "virtio-net", n);
166517ec5a86SKONRAD Frederic 
16668a253ec2SKONRAD Frederic     g_free(n->netclient_name);
16678a253ec2SKONRAD Frederic     n->netclient_name = NULL;
16688a253ec2SKONRAD Frederic     g_free(n->netclient_type);
16698a253ec2SKONRAD Frederic     n->netclient_type = NULL;
16708a253ec2SKONRAD Frederic 
167117ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
167217ec5a86SKONRAD Frederic     g_free(n->vlans);
167317ec5a86SKONRAD Frederic 
167417ec5a86SKONRAD Frederic     for (i = 0; i < n->max_queues; i++) {
167517ec5a86SKONRAD Frederic         VirtIONetQueue *q = &n->vqs[i];
167617ec5a86SKONRAD Frederic         NetClientState *nc = qemu_get_subqueue(n->nic, i);
167717ec5a86SKONRAD Frederic 
167817ec5a86SKONRAD Frederic         qemu_purge_queued_packets(nc);
167917ec5a86SKONRAD Frederic 
168017ec5a86SKONRAD Frederic         if (q->tx_timer) {
1681bc72ad67SAlex Bligh             timer_del(q->tx_timer);
1682bc72ad67SAlex Bligh             timer_free(q->tx_timer);
1683fe2dafa0SJason Wang         } else if (q->tx_bh) {
168417ec5a86SKONRAD Frederic             qemu_bh_delete(q->tx_bh);
168517ec5a86SKONRAD Frederic         }
168617ec5a86SKONRAD Frederic     }
168717ec5a86SKONRAD Frederic 
1688f57fcf70SJason Wang     timer_del(n->announce_timer);
1689f57fcf70SJason Wang     timer_free(n->announce_timer);
169017ec5a86SKONRAD Frederic     g_free(n->vqs);
169117ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
16926a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
169317ec5a86SKONRAD Frederic }
169417ec5a86SKONRAD Frederic 
169517ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
169617ec5a86SKONRAD Frederic {
169717ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
169817ec5a86SKONRAD Frederic 
169917ec5a86SKONRAD Frederic     /*
170017ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
170117ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
170217ec5a86SKONRAD Frederic      */
170317ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
1704aa4197c3SGonglei     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
1705aa4197c3SGonglei                                   "bootindex", "/ethernet-phy@0",
1706aa4197c3SGonglei                                   DEVICE(n), NULL);
170717ec5a86SKONRAD Frederic }
170817ec5a86SKONRAD Frederic 
170917ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
1710*87108bb2SShannon Zhao     DEFINE_PROP_BIT("any_layout", VirtIONet, host_features,
1711*87108bb2SShannon Zhao                     VIRTIO_F_ANY_LAYOUT, true),
1712*87108bb2SShannon Zhao     DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
1713*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
1714*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_CSUM, true),
1715*87108bb2SShannon Zhao     DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
1716*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
1717*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO4, true),
1718*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
1719*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_TSO6, true),
1720*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
1721*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ECN, true),
1722*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
1723*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_UFO, true),
1724*87108bb2SShannon Zhao     DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
1725*87108bb2SShannon Zhao                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
1726*87108bb2SShannon Zhao     DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
1727*87108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO4, true),
1728*87108bb2SShannon Zhao     DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
1729*87108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_TSO6, true),
1730*87108bb2SShannon Zhao     DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
1731*87108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_ECN, true),
1732*87108bb2SShannon Zhao     DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
1733*87108bb2SShannon Zhao                     VIRTIO_NET_F_HOST_UFO, true),
1734*87108bb2SShannon Zhao     DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
1735*87108bb2SShannon Zhao                     VIRTIO_NET_F_MRG_RXBUF, true),
1736*87108bb2SShannon Zhao     DEFINE_PROP_BIT("status", VirtIONet, host_features,
1737*87108bb2SShannon Zhao                     VIRTIO_NET_F_STATUS, true),
1738*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
1739*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VQ, true),
1740*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
1741*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX, true),
1742*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
1743*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_VLAN, true),
1744*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
1745*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
1746*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
1747*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
1748*87108bb2SShannon Zhao     DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
1749*87108bb2SShannon Zhao                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
1750*87108bb2SShannon Zhao     DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
175117ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
175217ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
175317ec5a86SKONRAD Frederic                        TX_TIMER_INTERVAL),
175417ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
175517ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
175617ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
175717ec5a86SKONRAD Frederic };
175817ec5a86SKONRAD Frederic 
175917ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
176017ec5a86SKONRAD Frederic {
176117ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
176217ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1763e6f746b3SAndreas Färber 
176417ec5a86SKONRAD Frederic     dc->props = virtio_net_properties;
1765125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1766e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
1767306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
176817ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
176917ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
177017ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
177117ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
177217ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
177317ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
177417ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
177517ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
177617ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
1777037dab2fSGreg Kurz     vdc->load = virtio_net_load_device;
1778037dab2fSGreg Kurz     vdc->save = virtio_net_save_device;
177917ec5a86SKONRAD Frederic }
178017ec5a86SKONRAD Frederic 
178117ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
178217ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
178317ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
178417ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
178517ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
178617ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
178717ec5a86SKONRAD Frederic };
178817ec5a86SKONRAD Frederic 
178917ec5a86SKONRAD Frederic static void virtio_register_types(void)
179017ec5a86SKONRAD Frederic {
179117ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
179217ec5a86SKONRAD Frederic }
179317ec5a86SKONRAD Frederic 
179417ec5a86SKONRAD Frederic type_init(virtio_register_types)
1795