xref: /openbmc/qemu/hw/net/virtio-net.c (revision ed8b4afe5ff290ae119237e4fa2142e9f1832230)
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"
25b1be4280SAmos Kong #include "monitor/monitor.h"
266e790746SPaolo Bonzini 
276e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION    11
286e790746SPaolo Bonzini 
296e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES    64
306e790746SPaolo Bonzini #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
316e790746SPaolo Bonzini 
326e790746SPaolo Bonzini /*
336e790746SPaolo Bonzini  * Calculate the number of bytes up to and including the given 'field' of
346e790746SPaolo Bonzini  * 'container'.
356e790746SPaolo Bonzini  */
366e790746SPaolo Bonzini #define endof(container, field) \
376e790746SPaolo Bonzini     (offsetof(container, field) + sizeof(((container *)0)->field))
386e790746SPaolo Bonzini 
396e790746SPaolo Bonzini typedef struct VirtIOFeature {
406e790746SPaolo Bonzini     uint32_t flags;
416e790746SPaolo Bonzini     size_t end;
426e790746SPaolo Bonzini } VirtIOFeature;
436e790746SPaolo Bonzini 
446e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = {
456e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MAC,
466e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, mac)},
476e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_STATUS,
486e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, status)},
496e790746SPaolo Bonzini     {.flags = 1 << VIRTIO_NET_F_MQ,
506e790746SPaolo Bonzini      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
516e790746SPaolo Bonzini     {}
526e790746SPaolo Bonzini };
536e790746SPaolo Bonzini 
546e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
556e790746SPaolo Bonzini {
566e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
576e790746SPaolo Bonzini 
586e790746SPaolo Bonzini     return &n->vqs[nc->queue_index];
596e790746SPaolo Bonzini }
606e790746SPaolo Bonzini 
616e790746SPaolo Bonzini static int vq2q(int queue_index)
626e790746SPaolo Bonzini {
636e790746SPaolo Bonzini     return queue_index / 2;
646e790746SPaolo Bonzini }
656e790746SPaolo Bonzini 
666e790746SPaolo Bonzini /* TODO
676e790746SPaolo Bonzini  * - we could suppress RX interrupt if we were so inclined.
686e790746SPaolo Bonzini  */
696e790746SPaolo Bonzini 
706e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
716e790746SPaolo Bonzini {
7217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
736e790746SPaolo Bonzini     struct virtio_net_config netcfg;
746e790746SPaolo Bonzini 
756e790746SPaolo Bonzini     stw_p(&netcfg.status, n->status);
766e790746SPaolo Bonzini     stw_p(&netcfg.max_virtqueue_pairs, n->max_queues);
776e790746SPaolo Bonzini     memcpy(netcfg.mac, n->mac, ETH_ALEN);
786e790746SPaolo Bonzini     memcpy(config, &netcfg, n->config_size);
796e790746SPaolo Bonzini }
806e790746SPaolo Bonzini 
816e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
826e790746SPaolo Bonzini {
8317a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
846e790746SPaolo Bonzini     struct virtio_net_config netcfg = {};
856e790746SPaolo Bonzini 
866e790746SPaolo Bonzini     memcpy(&netcfg, config, n->config_size);
876e790746SPaolo Bonzini 
8817a0ca55SKONRAD Frederic     if (!(vdev->guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
896e790746SPaolo Bonzini         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
906e790746SPaolo Bonzini         memcpy(n->mac, netcfg.mac, ETH_ALEN);
916e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
926e790746SPaolo Bonzini     }
936e790746SPaolo Bonzini }
946e790746SPaolo Bonzini 
956e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status)
966e790746SPaolo Bonzini {
9717a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
986e790746SPaolo Bonzini     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
9917a0ca55SKONRAD Frederic         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
1006e790746SPaolo Bonzini }
1016e790746SPaolo Bonzini 
102f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque)
103f57fcf70SJason Wang {
104f57fcf70SJason Wang     VirtIONet *n = opaque;
105f57fcf70SJason Wang     VirtIODevice *vdev = VIRTIO_DEVICE(n);
106f57fcf70SJason Wang 
107f57fcf70SJason Wang     n->announce_counter--;
108f57fcf70SJason Wang     n->status |= VIRTIO_NET_S_ANNOUNCE;
109f57fcf70SJason Wang     virtio_notify_config(vdev);
110f57fcf70SJason Wang }
111f57fcf70SJason Wang 
1126e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
1136e790746SPaolo Bonzini {
11417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1156e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
1166e790746SPaolo Bonzini     int queues = n->multiqueue ? n->max_queues : 1;
1176e790746SPaolo Bonzini 
118*ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
1196e790746SPaolo Bonzini         return;
1206e790746SPaolo Bonzini     }
1216e790746SPaolo Bonzini 
122d7108d90SJason Wang     if (!!n->vhost_started ==
123d7108d90SJason Wang         (virtio_net_started(n, status) && !nc->peer->link_down)) {
1246e790746SPaolo Bonzini         return;
1256e790746SPaolo Bonzini     }
1266e790746SPaolo Bonzini     if (!n->vhost_started) {
1276e790746SPaolo Bonzini         int r;
128*ed8b4afeSNikolay Nikolaev         if (!vhost_net_query(get_vhost_net(nc->peer), vdev)) {
1296e790746SPaolo Bonzini             return;
1306e790746SPaolo Bonzini         }
1316e790746SPaolo Bonzini         n->vhost_started = 1;
13217a0ca55SKONRAD Frederic         r = vhost_net_start(vdev, n->nic->ncs, queues);
1336e790746SPaolo Bonzini         if (r < 0) {
1346e790746SPaolo Bonzini             error_report("unable to start vhost net: %d: "
1356e790746SPaolo Bonzini                          "falling back on userspace virtio", -r);
1366e790746SPaolo Bonzini             n->vhost_started = 0;
1376e790746SPaolo Bonzini         }
1386e790746SPaolo Bonzini     } else {
13917a0ca55SKONRAD Frederic         vhost_net_stop(vdev, n->nic->ncs, queues);
1406e790746SPaolo Bonzini         n->vhost_started = 0;
1416e790746SPaolo Bonzini     }
1426e790746SPaolo Bonzini }
1436e790746SPaolo Bonzini 
1446e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
1456e790746SPaolo Bonzini {
14617a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
1476e790746SPaolo Bonzini     VirtIONetQueue *q;
1486e790746SPaolo Bonzini     int i;
1496e790746SPaolo Bonzini     uint8_t queue_status;
1506e790746SPaolo Bonzini 
1516e790746SPaolo Bonzini     virtio_net_vhost_status(n, status);
1526e790746SPaolo Bonzini 
1536e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
1546e790746SPaolo Bonzini         q = &n->vqs[i];
1556e790746SPaolo Bonzini 
1566e790746SPaolo Bonzini         if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
1576e790746SPaolo Bonzini             queue_status = 0;
1586e790746SPaolo Bonzini         } else {
1596e790746SPaolo Bonzini             queue_status = status;
1606e790746SPaolo Bonzini         }
1616e790746SPaolo Bonzini 
1626e790746SPaolo Bonzini         if (!q->tx_waiting) {
1636e790746SPaolo Bonzini             continue;
1646e790746SPaolo Bonzini         }
1656e790746SPaolo Bonzini 
1666e790746SPaolo Bonzini         if (virtio_net_started(n, queue_status) && !n->vhost_started) {
1676e790746SPaolo Bonzini             if (q->tx_timer) {
168bc72ad67SAlex Bligh                 timer_mod(q->tx_timer,
169bc72ad67SAlex Bligh                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1706e790746SPaolo Bonzini             } else {
1716e790746SPaolo Bonzini                 qemu_bh_schedule(q->tx_bh);
1726e790746SPaolo Bonzini             }
1736e790746SPaolo Bonzini         } else {
1746e790746SPaolo Bonzini             if (q->tx_timer) {
175bc72ad67SAlex Bligh                 timer_del(q->tx_timer);
1766e790746SPaolo Bonzini             } else {
1776e790746SPaolo Bonzini                 qemu_bh_cancel(q->tx_bh);
1786e790746SPaolo Bonzini             }
1796e790746SPaolo Bonzini         }
1806e790746SPaolo Bonzini     }
1816e790746SPaolo Bonzini }
1826e790746SPaolo Bonzini 
1836e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc)
1846e790746SPaolo Bonzini {
1856e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
18617a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1876e790746SPaolo Bonzini     uint16_t old_status = n->status;
1886e790746SPaolo Bonzini 
1896e790746SPaolo Bonzini     if (nc->link_down)
1906e790746SPaolo Bonzini         n->status &= ~VIRTIO_NET_S_LINK_UP;
1916e790746SPaolo Bonzini     else
1926e790746SPaolo Bonzini         n->status |= VIRTIO_NET_S_LINK_UP;
1936e790746SPaolo Bonzini 
1946e790746SPaolo Bonzini     if (n->status != old_status)
19517a0ca55SKONRAD Frederic         virtio_notify_config(vdev);
1966e790746SPaolo Bonzini 
19717a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
1986e790746SPaolo Bonzini }
1996e790746SPaolo Bonzini 
200b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc)
201b1be4280SAmos Kong {
202b1be4280SAmos Kong     QObject *event_data;
203b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
204b1be4280SAmos Kong 
205b1be4280SAmos Kong     if (nc->rxfilter_notify_enabled) {
20696e35046SAmos Kong         gchar *path = object_get_canonical_path(OBJECT(n->qdev));
207b1be4280SAmos Kong         if (n->netclient_name) {
208b1be4280SAmos Kong             event_data = qobject_from_jsonf("{ 'name': %s, 'path': %s }",
20996e35046SAmos Kong                                     n->netclient_name, path);
210b1be4280SAmos Kong         } else {
21196e35046SAmos Kong             event_data = qobject_from_jsonf("{ 'path': %s }", path);
212b1be4280SAmos Kong         }
213b1be4280SAmos Kong         monitor_protocol_event(QEVENT_NIC_RX_FILTER_CHANGED, event_data);
214b1be4280SAmos Kong         qobject_decref(event_data);
21596e35046SAmos Kong         g_free(path);
216b1be4280SAmos Kong 
217b1be4280SAmos Kong         /* disable event notification to avoid events flooding */
218b1be4280SAmos Kong         nc->rxfilter_notify_enabled = 0;
219b1be4280SAmos Kong     }
220b1be4280SAmos Kong }
221b1be4280SAmos Kong 
222b1be4280SAmos Kong static char *mac_strdup_printf(const uint8_t *mac)
223b1be4280SAmos Kong {
224b1be4280SAmos Kong     return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", mac[0],
225b1be4280SAmos Kong                             mac[1], mac[2], mac[3], mac[4], mac[5]);
226b1be4280SAmos Kong }
227b1be4280SAmos Kong 
228f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n)
229f7bc8ef8SAmos Kong {
230f7bc8ef8SAmos Kong     intList *list, *entry;
231f7bc8ef8SAmos Kong     int i, j;
232f7bc8ef8SAmos Kong 
233f7bc8ef8SAmos Kong     list = NULL;
234f7bc8ef8SAmos Kong     for (i = 0; i < MAX_VLAN >> 5; i++) {
235f7bc8ef8SAmos Kong         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
236f7bc8ef8SAmos Kong             if (n->vlans[i] & (1U << j)) {
237f7bc8ef8SAmos Kong                 entry = g_malloc0(sizeof(*entry));
238f7bc8ef8SAmos Kong                 entry->value = (i << 5) + j;
239f7bc8ef8SAmos Kong                 entry->next = list;
240f7bc8ef8SAmos Kong                 list = entry;
241f7bc8ef8SAmos Kong             }
242f7bc8ef8SAmos Kong         }
243f7bc8ef8SAmos Kong     }
244f7bc8ef8SAmos Kong 
245f7bc8ef8SAmos Kong     return list;
246f7bc8ef8SAmos Kong }
247f7bc8ef8SAmos Kong 
248b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
249b1be4280SAmos Kong {
250b1be4280SAmos Kong     VirtIONet *n = qemu_get_nic_opaque(nc);
251f7bc8ef8SAmos Kong     VirtIODevice *vdev = VIRTIO_DEVICE(n);
252b1be4280SAmos Kong     RxFilterInfo *info;
253b1be4280SAmos Kong     strList *str_list, *entry;
254f7bc8ef8SAmos Kong     int i;
255b1be4280SAmos Kong 
256b1be4280SAmos Kong     info = g_malloc0(sizeof(*info));
257b1be4280SAmos Kong     info->name = g_strdup(nc->name);
258b1be4280SAmos Kong     info->promiscuous = n->promisc;
259b1be4280SAmos Kong 
260b1be4280SAmos Kong     if (n->nouni) {
261b1be4280SAmos Kong         info->unicast = RX_STATE_NONE;
262b1be4280SAmos Kong     } else if (n->alluni) {
263b1be4280SAmos Kong         info->unicast = RX_STATE_ALL;
264b1be4280SAmos Kong     } else {
265b1be4280SAmos Kong         info->unicast = RX_STATE_NORMAL;
266b1be4280SAmos Kong     }
267b1be4280SAmos Kong 
268b1be4280SAmos Kong     if (n->nomulti) {
269b1be4280SAmos Kong         info->multicast = RX_STATE_NONE;
270b1be4280SAmos Kong     } else if (n->allmulti) {
271b1be4280SAmos Kong         info->multicast = RX_STATE_ALL;
272b1be4280SAmos Kong     } else {
273b1be4280SAmos Kong         info->multicast = RX_STATE_NORMAL;
274b1be4280SAmos Kong     }
275b1be4280SAmos Kong 
276b1be4280SAmos Kong     info->broadcast_allowed = n->nobcast;
277b1be4280SAmos Kong     info->multicast_overflow = n->mac_table.multi_overflow;
278b1be4280SAmos Kong     info->unicast_overflow = n->mac_table.uni_overflow;
279b1be4280SAmos Kong 
280b1be4280SAmos Kong     info->main_mac = mac_strdup_printf(n->mac);
281b1be4280SAmos Kong 
282b1be4280SAmos Kong     str_list = NULL;
283b1be4280SAmos Kong     for (i = 0; i < n->mac_table.first_multi; i++) {
284b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
285b1be4280SAmos Kong         entry->value = mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
286b1be4280SAmos Kong         entry->next = str_list;
287b1be4280SAmos Kong         str_list = entry;
288b1be4280SAmos Kong     }
289b1be4280SAmos Kong     info->unicast_table = str_list;
290b1be4280SAmos Kong 
291b1be4280SAmos Kong     str_list = NULL;
292b1be4280SAmos Kong     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
293b1be4280SAmos Kong         entry = g_malloc0(sizeof(*entry));
294b1be4280SAmos Kong         entry->value = mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
295b1be4280SAmos Kong         entry->next = str_list;
296b1be4280SAmos Kong         str_list = entry;
297b1be4280SAmos Kong     }
298b1be4280SAmos Kong     info->multicast_table = str_list;
299f7bc8ef8SAmos Kong     info->vlan_table = get_vlan_table(n);
300b1be4280SAmos Kong 
301f7bc8ef8SAmos Kong     if (!((1 << VIRTIO_NET_F_CTRL_VLAN) & vdev->guest_features)) {
302f7bc8ef8SAmos Kong         info->vlan = RX_STATE_ALL;
303f7bc8ef8SAmos Kong     } else if (!info->vlan_table) {
304f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NONE;
305f7bc8ef8SAmos Kong     } else {
306f7bc8ef8SAmos Kong         info->vlan = RX_STATE_NORMAL;
307b1be4280SAmos Kong     }
308b1be4280SAmos Kong 
309b1be4280SAmos Kong     /* enable event notification after query */
310b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
311b1be4280SAmos Kong 
312b1be4280SAmos Kong     return info;
313b1be4280SAmos Kong }
314b1be4280SAmos Kong 
3156e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev)
3166e790746SPaolo Bonzini {
31717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
3186e790746SPaolo Bonzini 
3196e790746SPaolo Bonzini     /* Reset back to compatibility mode */
3206e790746SPaolo Bonzini     n->promisc = 1;
3216e790746SPaolo Bonzini     n->allmulti = 0;
3226e790746SPaolo Bonzini     n->alluni = 0;
3236e790746SPaolo Bonzini     n->nomulti = 0;
3246e790746SPaolo Bonzini     n->nouni = 0;
3256e790746SPaolo Bonzini     n->nobcast = 0;
3266e790746SPaolo Bonzini     /* multiqueue is disabled by default */
3276e790746SPaolo Bonzini     n->curr_queues = 1;
328f57fcf70SJason Wang     timer_del(n->announce_timer);
329f57fcf70SJason Wang     n->announce_counter = 0;
330f57fcf70SJason Wang     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
3316e790746SPaolo Bonzini 
3326e790746SPaolo Bonzini     /* Flush any MAC and VLAN filter table state */
3336e790746SPaolo Bonzini     n->mac_table.in_use = 0;
3346e790746SPaolo Bonzini     n->mac_table.first_multi = 0;
3356e790746SPaolo Bonzini     n->mac_table.multi_overflow = 0;
3366e790746SPaolo Bonzini     n->mac_table.uni_overflow = 0;
3376e790746SPaolo Bonzini     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
3386e790746SPaolo Bonzini     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
339702d66a8SMichael S. Tsirkin     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
3406e790746SPaolo Bonzini     memset(n->vlans, 0, MAX_VLAN >> 3);
3416e790746SPaolo Bonzini }
3426e790746SPaolo Bonzini 
3436e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n)
3446e790746SPaolo Bonzini {
3456e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
3466e790746SPaolo Bonzini     if (!nc->peer) {
3476e790746SPaolo Bonzini         return;
3486e790746SPaolo Bonzini     }
3496e790746SPaolo Bonzini 
350d6085e3aSStefan Hajnoczi     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
3516e790746SPaolo Bonzini }
3526e790746SPaolo Bonzini 
3536e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n)
3546e790746SPaolo Bonzini {
3556e790746SPaolo Bonzini     return n->has_vnet_hdr;
3566e790746SPaolo Bonzini }
3576e790746SPaolo Bonzini 
3586e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n)
3596e790746SPaolo Bonzini {
3606e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n))
3616e790746SPaolo Bonzini         return 0;
3626e790746SPaolo Bonzini 
363d6085e3aSStefan Hajnoczi     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
3646e790746SPaolo Bonzini 
3656e790746SPaolo Bonzini     return n->has_ufo;
3666e790746SPaolo Bonzini }
3676e790746SPaolo Bonzini 
3686e790746SPaolo Bonzini static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
3696e790746SPaolo Bonzini {
3706e790746SPaolo Bonzini     int i;
3716e790746SPaolo Bonzini     NetClientState *nc;
3726e790746SPaolo Bonzini 
3736e790746SPaolo Bonzini     n->mergeable_rx_bufs = mergeable_rx_bufs;
3746e790746SPaolo Bonzini 
3756e790746SPaolo Bonzini     n->guest_hdr_len = n->mergeable_rx_bufs ?
3766e790746SPaolo Bonzini         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
3776e790746SPaolo Bonzini 
3786e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
3796e790746SPaolo Bonzini         nc = qemu_get_subqueue(n->nic, i);
3806e790746SPaolo Bonzini 
3816e790746SPaolo Bonzini         if (peer_has_vnet_hdr(n) &&
382d6085e3aSStefan Hajnoczi             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
383d6085e3aSStefan Hajnoczi             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
3846e790746SPaolo Bonzini             n->host_hdr_len = n->guest_hdr_len;
3856e790746SPaolo Bonzini         }
3866e790746SPaolo Bonzini     }
3876e790746SPaolo Bonzini }
3886e790746SPaolo Bonzini 
3896e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index)
3906e790746SPaolo Bonzini {
3916e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
3926e790746SPaolo Bonzini 
3936e790746SPaolo Bonzini     if (!nc->peer) {
3946e790746SPaolo Bonzini         return 0;
3956e790746SPaolo Bonzini     }
3966e790746SPaolo Bonzini 
3976e790746SPaolo Bonzini     if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
3986e790746SPaolo Bonzini         return 0;
3996e790746SPaolo Bonzini     }
4006e790746SPaolo Bonzini 
4016e790746SPaolo Bonzini     return tap_enable(nc->peer);
4026e790746SPaolo Bonzini }
4036e790746SPaolo Bonzini 
4046e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index)
4056e790746SPaolo Bonzini {
4066e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, index);
4076e790746SPaolo Bonzini 
4086e790746SPaolo Bonzini     if (!nc->peer) {
4096e790746SPaolo Bonzini         return 0;
4106e790746SPaolo Bonzini     }
4116e790746SPaolo Bonzini 
4126e790746SPaolo Bonzini     if (nc->peer->info->type !=  NET_CLIENT_OPTIONS_KIND_TAP) {
4136e790746SPaolo Bonzini         return 0;
4146e790746SPaolo Bonzini     }
4156e790746SPaolo Bonzini 
4166e790746SPaolo Bonzini     return tap_disable(nc->peer);
4176e790746SPaolo Bonzini }
4186e790746SPaolo Bonzini 
4196e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n)
4206e790746SPaolo Bonzini {
4216e790746SPaolo Bonzini     int i;
422ddfa83eaSJoel Stanley     int r;
4236e790746SPaolo Bonzini 
4246e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
4256e790746SPaolo Bonzini         if (i < n->curr_queues) {
426ddfa83eaSJoel Stanley             r = peer_attach(n, i);
427ddfa83eaSJoel Stanley             assert(!r);
4286e790746SPaolo Bonzini         } else {
429ddfa83eaSJoel Stanley             r = peer_detach(n, i);
430ddfa83eaSJoel Stanley             assert(!r);
4316e790746SPaolo Bonzini         }
4326e790746SPaolo Bonzini     }
4336e790746SPaolo Bonzini }
4346e790746SPaolo Bonzini 
435ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
4366e790746SPaolo Bonzini 
4376e790746SPaolo Bonzini static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
4386e790746SPaolo Bonzini {
43917a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
4406e790746SPaolo Bonzini     NetClientState *nc = qemu_get_queue(n->nic);
4416e790746SPaolo Bonzini 
4426e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_MAC);
4436e790746SPaolo Bonzini 
4446e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n)) {
4456e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_CSUM);
4466e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
4476e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
4486e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
4496e790746SPaolo Bonzini 
4506e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
4516e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
4526e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
4536e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
4546e790746SPaolo Bonzini     }
4556e790746SPaolo Bonzini 
4566e790746SPaolo Bonzini     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
4576e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
4586e790746SPaolo Bonzini         features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
4596e790746SPaolo Bonzini     }
4606e790746SPaolo Bonzini 
461*ed8b4afeSNikolay Nikolaev     if (!get_vhost_net(nc->peer)) {
4626e790746SPaolo Bonzini         return features;
4636e790746SPaolo Bonzini     }
464*ed8b4afeSNikolay Nikolaev     return vhost_net_get_features(get_vhost_net(nc->peer), features);
4656e790746SPaolo Bonzini }
4666e790746SPaolo Bonzini 
4676e790746SPaolo Bonzini static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
4686e790746SPaolo Bonzini {
4696e790746SPaolo Bonzini     uint32_t features = 0;
4706e790746SPaolo Bonzini 
4716e790746SPaolo Bonzini     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
4726e790746SPaolo Bonzini      * but also these: */
4736e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_MAC);
4746e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_CSUM);
4756e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_HOST_TSO4);
4766e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_HOST_TSO6);
4776e790746SPaolo Bonzini     features |= (1 << VIRTIO_NET_F_HOST_ECN);
4786e790746SPaolo Bonzini 
4796e790746SPaolo Bonzini     return features;
4806e790746SPaolo Bonzini }
4816e790746SPaolo Bonzini 
482644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n)
483644c9858SDmitry Fleytman {
484ad37bb3bSStefan Hajnoczi     qemu_set_offload(qemu_get_queue(n->nic)->peer,
485644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
486644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
487644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
488644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
489644c9858SDmitry Fleytman             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
490644c9858SDmitry Fleytman }
491644c9858SDmitry Fleytman 
492644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
493644c9858SDmitry Fleytman {
494644c9858SDmitry Fleytman     static const uint64_t guest_offloads_mask =
495644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
496644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
497644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
498644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
499644c9858SDmitry Fleytman         (1ULL << VIRTIO_NET_F_GUEST_UFO);
500644c9858SDmitry Fleytman 
501644c9858SDmitry Fleytman     return guest_offloads_mask & features;
502644c9858SDmitry Fleytman }
503644c9858SDmitry Fleytman 
504644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
505644c9858SDmitry Fleytman {
506644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
507644c9858SDmitry Fleytman     return virtio_net_guest_offloads_by_features(vdev->guest_features);
508644c9858SDmitry Fleytman }
509644c9858SDmitry Fleytman 
5106e790746SPaolo Bonzini static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
5116e790746SPaolo Bonzini {
51217a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
5136e790746SPaolo Bonzini     int i;
5146e790746SPaolo Bonzini 
515ec57db16SJason Wang     virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ)));
5166e790746SPaolo Bonzini 
5176e790746SPaolo Bonzini     virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
5186e790746SPaolo Bonzini 
5196e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
520644c9858SDmitry Fleytman         n->curr_guest_offloads =
521644c9858SDmitry Fleytman             virtio_net_guest_offloads_by_features(features);
522644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
5236e790746SPaolo Bonzini     }
5246e790746SPaolo Bonzini 
5256e790746SPaolo Bonzini     for (i = 0;  i < n->max_queues; i++) {
5266e790746SPaolo Bonzini         NetClientState *nc = qemu_get_subqueue(n->nic, i);
5276e790746SPaolo Bonzini 
528*ed8b4afeSNikolay Nikolaev         if (!get_vhost_net(nc->peer)) {
5296e790746SPaolo Bonzini             continue;
5306e790746SPaolo Bonzini         }
531*ed8b4afeSNikolay Nikolaev         vhost_net_ack_features(get_vhost_net(nc->peer), features);
5326e790746SPaolo Bonzini     }
5330b1eaa88SStefan Fritsch 
5340b1eaa88SStefan Fritsch     if ((1 << VIRTIO_NET_F_CTRL_VLAN) & features) {
5350b1eaa88SStefan Fritsch         memset(n->vlans, 0, MAX_VLAN >> 3);
5360b1eaa88SStefan Fritsch     } else {
5370b1eaa88SStefan Fritsch         memset(n->vlans, 0xff, MAX_VLAN >> 3);
5380b1eaa88SStefan Fritsch     }
5396e790746SPaolo Bonzini }
5406e790746SPaolo Bonzini 
5416e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
5426e790746SPaolo Bonzini                                      struct iovec *iov, unsigned int iov_cnt)
5436e790746SPaolo Bonzini {
5446e790746SPaolo Bonzini     uint8_t on;
5456e790746SPaolo Bonzini     size_t s;
546b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
5476e790746SPaolo Bonzini 
5486e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
5496e790746SPaolo Bonzini     if (s != sizeof(on)) {
5506e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
5516e790746SPaolo Bonzini     }
5526e790746SPaolo Bonzini 
5536e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
5546e790746SPaolo Bonzini         n->promisc = on;
5556e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
5566e790746SPaolo Bonzini         n->allmulti = on;
5576e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
5586e790746SPaolo Bonzini         n->alluni = on;
5596e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
5606e790746SPaolo Bonzini         n->nomulti = on;
5616e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
5626e790746SPaolo Bonzini         n->nouni = on;
5636e790746SPaolo Bonzini     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
5646e790746SPaolo Bonzini         n->nobcast = on;
5656e790746SPaolo Bonzini     } else {
5666e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
5676e790746SPaolo Bonzini     }
5686e790746SPaolo Bonzini 
569b1be4280SAmos Kong     rxfilter_notify(nc);
570b1be4280SAmos Kong 
5716e790746SPaolo Bonzini     return VIRTIO_NET_OK;
5726e790746SPaolo Bonzini }
5736e790746SPaolo Bonzini 
574644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
575644c9858SDmitry Fleytman                                      struct iovec *iov, unsigned int iov_cnt)
576644c9858SDmitry Fleytman {
577644c9858SDmitry Fleytman     VirtIODevice *vdev = VIRTIO_DEVICE(n);
578644c9858SDmitry Fleytman     uint64_t offloads;
579644c9858SDmitry Fleytman     size_t s;
580644c9858SDmitry Fleytman 
581644c9858SDmitry Fleytman     if (!((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features)) {
582644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
583644c9858SDmitry Fleytman     }
584644c9858SDmitry Fleytman 
585644c9858SDmitry Fleytman     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
586644c9858SDmitry Fleytman     if (s != sizeof(offloads)) {
587644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
588644c9858SDmitry Fleytman     }
589644c9858SDmitry Fleytman 
590644c9858SDmitry Fleytman     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
591644c9858SDmitry Fleytman         uint64_t supported_offloads;
592644c9858SDmitry Fleytman 
593644c9858SDmitry Fleytman         if (!n->has_vnet_hdr) {
594644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
595644c9858SDmitry Fleytman         }
596644c9858SDmitry Fleytman 
597644c9858SDmitry Fleytman         supported_offloads = virtio_net_supported_guest_offloads(n);
598644c9858SDmitry Fleytman         if (offloads & ~supported_offloads) {
599644c9858SDmitry Fleytman             return VIRTIO_NET_ERR;
600644c9858SDmitry Fleytman         }
601644c9858SDmitry Fleytman 
602644c9858SDmitry Fleytman         n->curr_guest_offloads = offloads;
603644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
604644c9858SDmitry Fleytman 
605644c9858SDmitry Fleytman         return VIRTIO_NET_OK;
606644c9858SDmitry Fleytman     } else {
607644c9858SDmitry Fleytman         return VIRTIO_NET_ERR;
608644c9858SDmitry Fleytman     }
609644c9858SDmitry Fleytman }
610644c9858SDmitry Fleytman 
6116e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
6126e790746SPaolo Bonzini                                  struct iovec *iov, unsigned int iov_cnt)
6136e790746SPaolo Bonzini {
6146e790746SPaolo Bonzini     struct virtio_net_ctrl_mac mac_data;
6156e790746SPaolo Bonzini     size_t s;
616b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
6176e790746SPaolo Bonzini 
6186e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
6196e790746SPaolo Bonzini         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
6206e790746SPaolo Bonzini             return VIRTIO_NET_ERR;
6216e790746SPaolo Bonzini         }
6226e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
6236e790746SPaolo Bonzini         assert(s == sizeof(n->mac));
6246e790746SPaolo Bonzini         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
625b1be4280SAmos Kong         rxfilter_notify(nc);
626b1be4280SAmos Kong 
6276e790746SPaolo Bonzini         return VIRTIO_NET_OK;
6286e790746SPaolo Bonzini     }
6296e790746SPaolo Bonzini 
6306e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
6316e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
6326e790746SPaolo Bonzini     }
6336e790746SPaolo Bonzini 
634cae2e556SAmos Kong     int in_use = 0;
635cae2e556SAmos Kong     int first_multi = 0;
636cae2e556SAmos Kong     uint8_t uni_overflow = 0;
637cae2e556SAmos Kong     uint8_t multi_overflow = 0;
638cae2e556SAmos Kong     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
6396e790746SPaolo Bonzini 
6406e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
6416e790746SPaolo Bonzini                    sizeof(mac_data.entries));
6426e790746SPaolo Bonzini     mac_data.entries = ldl_p(&mac_data.entries);
6436e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
644b1be4280SAmos Kong         goto error;
6456e790746SPaolo Bonzini     }
6466e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
6476e790746SPaolo Bonzini 
6486e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
649b1be4280SAmos Kong         goto error;
6506e790746SPaolo Bonzini     }
6516e790746SPaolo Bonzini 
6526e790746SPaolo Bonzini     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
653cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, macs,
6546e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
6556e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
656b1be4280SAmos Kong             goto error;
6576e790746SPaolo Bonzini         }
658cae2e556SAmos Kong         in_use += mac_data.entries;
6596e790746SPaolo Bonzini     } else {
660cae2e556SAmos Kong         uni_overflow = 1;
6616e790746SPaolo Bonzini     }
6626e790746SPaolo Bonzini 
6636e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
6646e790746SPaolo Bonzini 
665cae2e556SAmos Kong     first_multi = in_use;
6666e790746SPaolo Bonzini 
6676e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
6686e790746SPaolo Bonzini                    sizeof(mac_data.entries));
6696e790746SPaolo Bonzini     mac_data.entries = ldl_p(&mac_data.entries);
6706e790746SPaolo Bonzini     if (s != sizeof(mac_data.entries)) {
671b1be4280SAmos Kong         goto error;
6726e790746SPaolo Bonzini     }
6736e790746SPaolo Bonzini 
6746e790746SPaolo Bonzini     iov_discard_front(&iov, &iov_cnt, s);
6756e790746SPaolo Bonzini 
6766e790746SPaolo Bonzini     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
677b1be4280SAmos Kong         goto error;
6786e790746SPaolo Bonzini     }
6796e790746SPaolo Bonzini 
680edc24385SMichael S. Tsirkin     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
681cae2e556SAmos Kong         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
6826e790746SPaolo Bonzini                        mac_data.entries * ETH_ALEN);
6836e790746SPaolo Bonzini         if (s != mac_data.entries * ETH_ALEN) {
684b1be4280SAmos Kong             goto error;
6856e790746SPaolo Bonzini         }
686cae2e556SAmos Kong         in_use += mac_data.entries;
6876e790746SPaolo Bonzini     } else {
688cae2e556SAmos Kong         multi_overflow = 1;
6896e790746SPaolo Bonzini     }
6906e790746SPaolo Bonzini 
691cae2e556SAmos Kong     n->mac_table.in_use = in_use;
692cae2e556SAmos Kong     n->mac_table.first_multi = first_multi;
693cae2e556SAmos Kong     n->mac_table.uni_overflow = uni_overflow;
694cae2e556SAmos Kong     n->mac_table.multi_overflow = multi_overflow;
695cae2e556SAmos Kong     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
696cae2e556SAmos Kong     g_free(macs);
697b1be4280SAmos Kong     rxfilter_notify(nc);
698b1be4280SAmos Kong 
6996e790746SPaolo Bonzini     return VIRTIO_NET_OK;
700b1be4280SAmos Kong 
701b1be4280SAmos Kong error:
702cae2e556SAmos Kong     g_free(macs);
703b1be4280SAmos Kong     return VIRTIO_NET_ERR;
7046e790746SPaolo Bonzini }
7056e790746SPaolo Bonzini 
7066e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
7076e790746SPaolo Bonzini                                         struct iovec *iov, unsigned int iov_cnt)
7086e790746SPaolo Bonzini {
7096e790746SPaolo Bonzini     uint16_t vid;
7106e790746SPaolo Bonzini     size_t s;
711b1be4280SAmos Kong     NetClientState *nc = qemu_get_queue(n->nic);
7126e790746SPaolo Bonzini 
7136e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
7146e790746SPaolo Bonzini     vid = lduw_p(&vid);
7156e790746SPaolo Bonzini     if (s != sizeof(vid)) {
7166e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7176e790746SPaolo Bonzini     }
7186e790746SPaolo Bonzini 
7196e790746SPaolo Bonzini     if (vid >= MAX_VLAN)
7206e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7216e790746SPaolo Bonzini 
7226e790746SPaolo Bonzini     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
7236e790746SPaolo Bonzini         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
7246e790746SPaolo Bonzini     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
7256e790746SPaolo Bonzini         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
7266e790746SPaolo Bonzini     else
7276e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7286e790746SPaolo Bonzini 
729b1be4280SAmos Kong     rxfilter_notify(nc);
730b1be4280SAmos Kong 
7316e790746SPaolo Bonzini     return VIRTIO_NET_OK;
7326e790746SPaolo Bonzini }
7336e790746SPaolo Bonzini 
734f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
735f57fcf70SJason Wang                                       struct iovec *iov, unsigned int iov_cnt)
736f57fcf70SJason Wang {
737f57fcf70SJason Wang     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
738f57fcf70SJason Wang         n->status & VIRTIO_NET_S_ANNOUNCE) {
739f57fcf70SJason Wang         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
740f57fcf70SJason Wang         if (n->announce_counter) {
741f57fcf70SJason Wang             timer_mod(n->announce_timer,
742f57fcf70SJason Wang                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
743f57fcf70SJason Wang                       self_announce_delay(n->announce_counter));
744f57fcf70SJason Wang         }
745f57fcf70SJason Wang         return VIRTIO_NET_OK;
746f57fcf70SJason Wang     } else {
747f57fcf70SJason Wang         return VIRTIO_NET_ERR;
748f57fcf70SJason Wang     }
749f57fcf70SJason Wang }
750f57fcf70SJason Wang 
7516e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
7526e790746SPaolo Bonzini                                 struct iovec *iov, unsigned int iov_cnt)
7536e790746SPaolo Bonzini {
75417a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
7556e790746SPaolo Bonzini     struct virtio_net_ctrl_mq mq;
7566e790746SPaolo Bonzini     size_t s;
7576e790746SPaolo Bonzini     uint16_t queues;
7586e790746SPaolo Bonzini 
7596e790746SPaolo Bonzini     s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
7606e790746SPaolo Bonzini     if (s != sizeof(mq)) {
7616e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7626e790746SPaolo Bonzini     }
7636e790746SPaolo Bonzini 
7646e790746SPaolo Bonzini     if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
7656e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7666e790746SPaolo Bonzini     }
7676e790746SPaolo Bonzini 
7686e790746SPaolo Bonzini     queues = lduw_p(&mq.virtqueue_pairs);
7696e790746SPaolo Bonzini 
7706e790746SPaolo Bonzini     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
7716e790746SPaolo Bonzini         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
7726e790746SPaolo Bonzini         queues > n->max_queues ||
7736e790746SPaolo Bonzini         !n->multiqueue) {
7746e790746SPaolo Bonzini         return VIRTIO_NET_ERR;
7756e790746SPaolo Bonzini     }
7766e790746SPaolo Bonzini 
7776e790746SPaolo Bonzini     n->curr_queues = queues;
7786e790746SPaolo Bonzini     /* stop the backend before changing the number of queues to avoid handling a
7796e790746SPaolo Bonzini      * disabled queue */
78017a0ca55SKONRAD Frederic     virtio_net_set_status(vdev, vdev->status);
7816e790746SPaolo Bonzini     virtio_net_set_queues(n);
7826e790746SPaolo Bonzini 
7836e790746SPaolo Bonzini     return VIRTIO_NET_OK;
7846e790746SPaolo Bonzini }
7856e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
7866e790746SPaolo Bonzini {
78717a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
7886e790746SPaolo Bonzini     struct virtio_net_ctrl_hdr ctrl;
7896e790746SPaolo Bonzini     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
7906e790746SPaolo Bonzini     VirtQueueElement elem;
7916e790746SPaolo Bonzini     size_t s;
7926e790746SPaolo Bonzini     struct iovec *iov;
7936e790746SPaolo Bonzini     unsigned int iov_cnt;
7946e790746SPaolo Bonzini 
7956e790746SPaolo Bonzini     while (virtqueue_pop(vq, &elem)) {
7966e790746SPaolo Bonzini         if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
7976e790746SPaolo Bonzini             iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
7986e790746SPaolo Bonzini             error_report("virtio-net ctrl missing headers");
7996e790746SPaolo Bonzini             exit(1);
8006e790746SPaolo Bonzini         }
8016e790746SPaolo Bonzini 
8026e790746SPaolo Bonzini         iov = elem.out_sg;
8036e790746SPaolo Bonzini         iov_cnt = elem.out_num;
8046e790746SPaolo Bonzini         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
8056e790746SPaolo Bonzini         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
8066e790746SPaolo Bonzini         if (s != sizeof(ctrl)) {
8076e790746SPaolo Bonzini             status = VIRTIO_NET_ERR;
8086e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
8096e790746SPaolo Bonzini             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
8106e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
8116e790746SPaolo Bonzini             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
8126e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
8136e790746SPaolo Bonzini             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
814f57fcf70SJason Wang         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
815f57fcf70SJason Wang             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
8166e790746SPaolo Bonzini         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
8176e790746SPaolo Bonzini             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
818644c9858SDmitry Fleytman         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
819644c9858SDmitry Fleytman             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
8206e790746SPaolo Bonzini         }
8216e790746SPaolo Bonzini 
8226e790746SPaolo Bonzini         s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
8236e790746SPaolo Bonzini         assert(s == sizeof(status));
8246e790746SPaolo Bonzini 
8256e790746SPaolo Bonzini         virtqueue_push(vq, &elem, sizeof(status));
8266e790746SPaolo Bonzini         virtio_notify(vdev, vq);
8276e790746SPaolo Bonzini     }
8286e790746SPaolo Bonzini }
8296e790746SPaolo Bonzini 
8306e790746SPaolo Bonzini /* RX */
8316e790746SPaolo Bonzini 
8326e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
8336e790746SPaolo Bonzini {
83417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
8356e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(vq));
8366e790746SPaolo Bonzini 
8376e790746SPaolo Bonzini     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
8386e790746SPaolo Bonzini }
8396e790746SPaolo Bonzini 
8406e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc)
8416e790746SPaolo Bonzini {
8426e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
84317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
8446e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
8456e790746SPaolo Bonzini 
84617a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
8476e790746SPaolo Bonzini         return 0;
8486e790746SPaolo Bonzini     }
8496e790746SPaolo Bonzini 
8506e790746SPaolo Bonzini     if (nc->queue_index >= n->curr_queues) {
8516e790746SPaolo Bonzini         return 0;
8526e790746SPaolo Bonzini     }
8536e790746SPaolo Bonzini 
8546e790746SPaolo Bonzini     if (!virtio_queue_ready(q->rx_vq) ||
85517a0ca55SKONRAD Frederic         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
8566e790746SPaolo Bonzini         return 0;
8576e790746SPaolo Bonzini     }
8586e790746SPaolo Bonzini 
8596e790746SPaolo Bonzini     return 1;
8606e790746SPaolo Bonzini }
8616e790746SPaolo Bonzini 
8626e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
8636e790746SPaolo Bonzini {
8646e790746SPaolo Bonzini     VirtIONet *n = q->n;
8656e790746SPaolo Bonzini     if (virtio_queue_empty(q->rx_vq) ||
8666e790746SPaolo Bonzini         (n->mergeable_rx_bufs &&
8676e790746SPaolo Bonzini          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
8686e790746SPaolo Bonzini         virtio_queue_set_notification(q->rx_vq, 1);
8696e790746SPaolo Bonzini 
8706e790746SPaolo Bonzini         /* To avoid a race condition where the guest has made some buffers
8716e790746SPaolo Bonzini          * available after the above check but before notification was
8726e790746SPaolo Bonzini          * enabled, check for available buffers again.
8736e790746SPaolo Bonzini          */
8746e790746SPaolo Bonzini         if (virtio_queue_empty(q->rx_vq) ||
8756e790746SPaolo Bonzini             (n->mergeable_rx_bufs &&
8766e790746SPaolo Bonzini              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
8776e790746SPaolo Bonzini             return 0;
8786e790746SPaolo Bonzini         }
8796e790746SPaolo Bonzini     }
8806e790746SPaolo Bonzini 
8816e790746SPaolo Bonzini     virtio_queue_set_notification(q->rx_vq, 0);
8826e790746SPaolo Bonzini     return 1;
8836e790746SPaolo Bonzini }
8846e790746SPaolo Bonzini 
8856e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
8866e790746SPaolo Bonzini  * it never finds out that the packets don't have valid checksums.  This
8876e790746SPaolo Bonzini  * causes dhclient to get upset.  Fedora's carried a patch for ages to
8886e790746SPaolo Bonzini  * fix this with Xen but it hasn't appeared in an upstream release of
8896e790746SPaolo Bonzini  * dhclient yet.
8906e790746SPaolo Bonzini  *
8916e790746SPaolo Bonzini  * To avoid breaking existing guests, we catch udp packets and add
8926e790746SPaolo Bonzini  * checksums.  This is terrible but it's better than hacking the guest
8936e790746SPaolo Bonzini  * kernels.
8946e790746SPaolo Bonzini  *
8956e790746SPaolo Bonzini  * N.B. if we introduce a zero-copy API, this operation is no longer free so
8966e790746SPaolo Bonzini  * we should provide a mechanism to disable it to avoid polluting the host
8976e790746SPaolo Bonzini  * cache.
8986e790746SPaolo Bonzini  */
8996e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
9006e790746SPaolo Bonzini                                         uint8_t *buf, size_t size)
9016e790746SPaolo Bonzini {
9026e790746SPaolo Bonzini     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
9036e790746SPaolo Bonzini         (size > 27 && size < 1500) && /* normal sized MTU */
9046e790746SPaolo Bonzini         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
9056e790746SPaolo Bonzini         (buf[23] == 17) && /* ip.protocol == UDP */
9066e790746SPaolo Bonzini         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
9076e790746SPaolo Bonzini         net_checksum_calculate(buf, size);
9086e790746SPaolo Bonzini         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
9096e790746SPaolo Bonzini     }
9106e790746SPaolo Bonzini }
9116e790746SPaolo Bonzini 
9126e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
9136e790746SPaolo Bonzini                            const void *buf, size_t size)
9146e790746SPaolo Bonzini {
9156e790746SPaolo Bonzini     if (n->has_vnet_hdr) {
9166e790746SPaolo Bonzini         /* FIXME this cast is evil */
9176e790746SPaolo Bonzini         void *wbuf = (void *)buf;
9186e790746SPaolo Bonzini         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
9196e790746SPaolo Bonzini                                     size - n->host_hdr_len);
9206e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
9216e790746SPaolo Bonzini     } else {
9226e790746SPaolo Bonzini         struct virtio_net_hdr hdr = {
9236e790746SPaolo Bonzini             .flags = 0,
9246e790746SPaolo Bonzini             .gso_type = VIRTIO_NET_HDR_GSO_NONE
9256e790746SPaolo Bonzini         };
9266e790746SPaolo Bonzini         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
9276e790746SPaolo Bonzini     }
9286e790746SPaolo Bonzini }
9296e790746SPaolo Bonzini 
9306e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
9316e790746SPaolo Bonzini {
9326e790746SPaolo Bonzini     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
9336e790746SPaolo Bonzini     static const uint8_t vlan[] = {0x81, 0x00};
9346e790746SPaolo Bonzini     uint8_t *ptr = (uint8_t *)buf;
9356e790746SPaolo Bonzini     int i;
9366e790746SPaolo Bonzini 
9376e790746SPaolo Bonzini     if (n->promisc)
9386e790746SPaolo Bonzini         return 1;
9396e790746SPaolo Bonzini 
9406e790746SPaolo Bonzini     ptr += n->host_hdr_len;
9416e790746SPaolo Bonzini 
9426e790746SPaolo Bonzini     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
9436e790746SPaolo Bonzini         int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
9446e790746SPaolo Bonzini         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
9456e790746SPaolo Bonzini             return 0;
9466e790746SPaolo Bonzini     }
9476e790746SPaolo Bonzini 
9486e790746SPaolo Bonzini     if (ptr[0] & 1) { // multicast
9496e790746SPaolo Bonzini         if (!memcmp(ptr, bcast, sizeof(bcast))) {
9506e790746SPaolo Bonzini             return !n->nobcast;
9516e790746SPaolo Bonzini         } else if (n->nomulti) {
9526e790746SPaolo Bonzini             return 0;
9536e790746SPaolo Bonzini         } else if (n->allmulti || n->mac_table.multi_overflow) {
9546e790746SPaolo Bonzini             return 1;
9556e790746SPaolo Bonzini         }
9566e790746SPaolo Bonzini 
9576e790746SPaolo Bonzini         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
9586e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
9596e790746SPaolo Bonzini                 return 1;
9606e790746SPaolo Bonzini             }
9616e790746SPaolo Bonzini         }
9626e790746SPaolo Bonzini     } else { // unicast
9636e790746SPaolo Bonzini         if (n->nouni) {
9646e790746SPaolo Bonzini             return 0;
9656e790746SPaolo Bonzini         } else if (n->alluni || n->mac_table.uni_overflow) {
9666e790746SPaolo Bonzini             return 1;
9676e790746SPaolo Bonzini         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
9686e790746SPaolo Bonzini             return 1;
9696e790746SPaolo Bonzini         }
9706e790746SPaolo Bonzini 
9716e790746SPaolo Bonzini         for (i = 0; i < n->mac_table.first_multi; i++) {
9726e790746SPaolo Bonzini             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
9736e790746SPaolo Bonzini                 return 1;
9746e790746SPaolo Bonzini             }
9756e790746SPaolo Bonzini         }
9766e790746SPaolo Bonzini     }
9776e790746SPaolo Bonzini 
9786e790746SPaolo Bonzini     return 0;
9796e790746SPaolo Bonzini }
9806e790746SPaolo Bonzini 
9816e790746SPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
9826e790746SPaolo Bonzini {
9836e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
9846e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
98517a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
9866e790746SPaolo Bonzini     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
9876e790746SPaolo Bonzini     struct virtio_net_hdr_mrg_rxbuf mhdr;
9886e790746SPaolo Bonzini     unsigned mhdr_cnt = 0;
9896e790746SPaolo Bonzini     size_t offset, i, guest_offset;
9906e790746SPaolo Bonzini 
9916e790746SPaolo Bonzini     if (!virtio_net_can_receive(nc)) {
9926e790746SPaolo Bonzini         return -1;
9936e790746SPaolo Bonzini     }
9946e790746SPaolo Bonzini 
9956e790746SPaolo Bonzini     /* hdr_len refers to the header we supply to the guest */
9966e790746SPaolo Bonzini     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
9976e790746SPaolo Bonzini         return 0;
9986e790746SPaolo Bonzini     }
9996e790746SPaolo Bonzini 
10006e790746SPaolo Bonzini     if (!receive_filter(n, buf, size))
10016e790746SPaolo Bonzini         return size;
10026e790746SPaolo Bonzini 
10036e790746SPaolo Bonzini     offset = i = 0;
10046e790746SPaolo Bonzini 
10056e790746SPaolo Bonzini     while (offset < size) {
10066e790746SPaolo Bonzini         VirtQueueElement elem;
10076e790746SPaolo Bonzini         int len, total;
10086e790746SPaolo Bonzini         const struct iovec *sg = elem.in_sg;
10096e790746SPaolo Bonzini 
10106e790746SPaolo Bonzini         total = 0;
10116e790746SPaolo Bonzini 
10126e790746SPaolo Bonzini         if (virtqueue_pop(q->rx_vq, &elem) == 0) {
10136e790746SPaolo Bonzini             if (i == 0)
10146e790746SPaolo Bonzini                 return -1;
10156e790746SPaolo Bonzini             error_report("virtio-net unexpected empty queue: "
10166e790746SPaolo Bonzini                     "i %zd mergeable %d offset %zd, size %zd, "
10176e790746SPaolo Bonzini                     "guest hdr len %zd, host hdr len %zd guest features 0x%x",
10186e790746SPaolo Bonzini                     i, n->mergeable_rx_bufs, offset, size,
101917a0ca55SKONRAD Frederic                     n->guest_hdr_len, n->host_hdr_len, vdev->guest_features);
10206e790746SPaolo Bonzini             exit(1);
10216e790746SPaolo Bonzini         }
10226e790746SPaolo Bonzini 
10236e790746SPaolo Bonzini         if (elem.in_num < 1) {
10246e790746SPaolo Bonzini             error_report("virtio-net receive queue contains no in buffers");
10256e790746SPaolo Bonzini             exit(1);
10266e790746SPaolo Bonzini         }
10276e790746SPaolo Bonzini 
10286e790746SPaolo Bonzini         if (i == 0) {
10296e790746SPaolo Bonzini             assert(offset == 0);
10306e790746SPaolo Bonzini             if (n->mergeable_rx_bufs) {
10316e790746SPaolo Bonzini                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
10326e790746SPaolo Bonzini                                     sg, elem.in_num,
10336e790746SPaolo Bonzini                                     offsetof(typeof(mhdr), num_buffers),
10346e790746SPaolo Bonzini                                     sizeof(mhdr.num_buffers));
10356e790746SPaolo Bonzini             }
10366e790746SPaolo Bonzini 
10376e790746SPaolo Bonzini             receive_header(n, sg, elem.in_num, buf, size);
10386e790746SPaolo Bonzini             offset = n->host_hdr_len;
10396e790746SPaolo Bonzini             total += n->guest_hdr_len;
10406e790746SPaolo Bonzini             guest_offset = n->guest_hdr_len;
10416e790746SPaolo Bonzini         } else {
10426e790746SPaolo Bonzini             guest_offset = 0;
10436e790746SPaolo Bonzini         }
10446e790746SPaolo Bonzini 
10456e790746SPaolo Bonzini         /* copy in packet.  ugh */
10466e790746SPaolo Bonzini         len = iov_from_buf(sg, elem.in_num, guest_offset,
10476e790746SPaolo Bonzini                            buf + offset, size - offset);
10486e790746SPaolo Bonzini         total += len;
10496e790746SPaolo Bonzini         offset += len;
10506e790746SPaolo Bonzini         /* If buffers can't be merged, at this point we
10516e790746SPaolo Bonzini          * must have consumed the complete packet.
10526e790746SPaolo Bonzini          * Otherwise, drop it. */
10536e790746SPaolo Bonzini         if (!n->mergeable_rx_bufs && offset < size) {
10546e790746SPaolo Bonzini #if 0
10556e790746SPaolo Bonzini             error_report("virtio-net truncated non-mergeable packet: "
10566e790746SPaolo Bonzini                          "i %zd mergeable %d offset %zd, size %zd, "
10576e790746SPaolo Bonzini                          "guest hdr len %zd, host hdr len %zd",
10586e790746SPaolo Bonzini                          i, n->mergeable_rx_bufs,
10596e790746SPaolo Bonzini                          offset, size, n->guest_hdr_len, n->host_hdr_len);
10606e790746SPaolo Bonzini #endif
10616e790746SPaolo Bonzini             return size;
10626e790746SPaolo Bonzini         }
10636e790746SPaolo Bonzini 
10646e790746SPaolo Bonzini         /* signal other side */
10656e790746SPaolo Bonzini         virtqueue_fill(q->rx_vq, &elem, total, i++);
10666e790746SPaolo Bonzini     }
10676e790746SPaolo Bonzini 
10686e790746SPaolo Bonzini     if (mhdr_cnt) {
10696e790746SPaolo Bonzini         stw_p(&mhdr.num_buffers, i);
10706e790746SPaolo Bonzini         iov_from_buf(mhdr_sg, mhdr_cnt,
10716e790746SPaolo Bonzini                      0,
10726e790746SPaolo Bonzini                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
10736e790746SPaolo Bonzini     }
10746e790746SPaolo Bonzini 
10756e790746SPaolo Bonzini     virtqueue_flush(q->rx_vq, i);
107617a0ca55SKONRAD Frederic     virtio_notify(vdev, q->rx_vq);
10776e790746SPaolo Bonzini 
10786e790746SPaolo Bonzini     return size;
10796e790746SPaolo Bonzini }
10806e790746SPaolo Bonzini 
10816e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
10826e790746SPaolo Bonzini 
10836e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
10846e790746SPaolo Bonzini {
10856e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
10866e790746SPaolo Bonzini     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
108717a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
10886e790746SPaolo Bonzini 
10896e790746SPaolo Bonzini     virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
109017a0ca55SKONRAD Frederic     virtio_notify(vdev, q->tx_vq);
10916e790746SPaolo Bonzini 
10926e790746SPaolo Bonzini     q->async_tx.elem.out_num = q->async_tx.len = 0;
10936e790746SPaolo Bonzini 
10946e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
10956e790746SPaolo Bonzini     virtio_net_flush_tx(q);
10966e790746SPaolo Bonzini }
10976e790746SPaolo Bonzini 
10986e790746SPaolo Bonzini /* TX */
10996e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
11006e790746SPaolo Bonzini {
11016e790746SPaolo Bonzini     VirtIONet *n = q->n;
110217a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
11036e790746SPaolo Bonzini     VirtQueueElement elem;
11046e790746SPaolo Bonzini     int32_t num_packets = 0;
11056e790746SPaolo Bonzini     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
110617a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
11076e790746SPaolo Bonzini         return num_packets;
11086e790746SPaolo Bonzini     }
11096e790746SPaolo Bonzini 
111017a0ca55SKONRAD Frederic     assert(vdev->vm_running);
11116e790746SPaolo Bonzini 
11126e790746SPaolo Bonzini     if (q->async_tx.elem.out_num) {
11136e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
11146e790746SPaolo Bonzini         return num_packets;
11156e790746SPaolo Bonzini     }
11166e790746SPaolo Bonzini 
11176e790746SPaolo Bonzini     while (virtqueue_pop(q->tx_vq, &elem)) {
11186e790746SPaolo Bonzini         ssize_t ret, len;
11196e790746SPaolo Bonzini         unsigned int out_num = elem.out_num;
11206e790746SPaolo Bonzini         struct iovec *out_sg = &elem.out_sg[0];
11216e790746SPaolo Bonzini         struct iovec sg[VIRTQUEUE_MAX_SIZE];
11226e790746SPaolo Bonzini 
11236e790746SPaolo Bonzini         if (out_num < 1) {
11246e790746SPaolo Bonzini             error_report("virtio-net header not in first element");
11256e790746SPaolo Bonzini             exit(1);
11266e790746SPaolo Bonzini         }
11276e790746SPaolo Bonzini 
11286e790746SPaolo Bonzini         /*
11296e790746SPaolo Bonzini          * If host wants to see the guest header as is, we can
11306e790746SPaolo Bonzini          * pass it on unchanged. Otherwise, copy just the parts
11316e790746SPaolo Bonzini          * that host is interested in.
11326e790746SPaolo Bonzini          */
11336e790746SPaolo Bonzini         assert(n->host_hdr_len <= n->guest_hdr_len);
11346e790746SPaolo Bonzini         if (n->host_hdr_len != n->guest_hdr_len) {
11356e790746SPaolo Bonzini             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
11366e790746SPaolo Bonzini                                        out_sg, out_num,
11376e790746SPaolo Bonzini                                        0, n->host_hdr_len);
11386e790746SPaolo Bonzini             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
11396e790746SPaolo Bonzini                              out_sg, out_num,
11406e790746SPaolo Bonzini                              n->guest_hdr_len, -1);
11416e790746SPaolo Bonzini             out_num = sg_num;
11426e790746SPaolo Bonzini             out_sg = sg;
11436e790746SPaolo Bonzini         }
11446e790746SPaolo Bonzini 
11456e790746SPaolo Bonzini         len = n->guest_hdr_len;
11466e790746SPaolo Bonzini 
11476e790746SPaolo Bonzini         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
11486e790746SPaolo Bonzini                                       out_sg, out_num, virtio_net_tx_complete);
11496e790746SPaolo Bonzini         if (ret == 0) {
11506e790746SPaolo Bonzini             virtio_queue_set_notification(q->tx_vq, 0);
11516e790746SPaolo Bonzini             q->async_tx.elem = elem;
11526e790746SPaolo Bonzini             q->async_tx.len  = len;
11536e790746SPaolo Bonzini             return -EBUSY;
11546e790746SPaolo Bonzini         }
11556e790746SPaolo Bonzini 
11566e790746SPaolo Bonzini         len += ret;
11576e790746SPaolo Bonzini 
11586e790746SPaolo Bonzini         virtqueue_push(q->tx_vq, &elem, 0);
115917a0ca55SKONRAD Frederic         virtio_notify(vdev, q->tx_vq);
11606e790746SPaolo Bonzini 
11616e790746SPaolo Bonzini         if (++num_packets >= n->tx_burst) {
11626e790746SPaolo Bonzini             break;
11636e790746SPaolo Bonzini         }
11646e790746SPaolo Bonzini     }
11656e790746SPaolo Bonzini     return num_packets;
11666e790746SPaolo Bonzini }
11676e790746SPaolo Bonzini 
11686e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
11696e790746SPaolo Bonzini {
117017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
11716e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
11726e790746SPaolo Bonzini 
11736e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
117417a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
11756e790746SPaolo Bonzini         q->tx_waiting = 1;
11766e790746SPaolo Bonzini         return;
11776e790746SPaolo Bonzini     }
11786e790746SPaolo Bonzini 
11796e790746SPaolo Bonzini     if (q->tx_waiting) {
11806e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 1);
1181bc72ad67SAlex Bligh         timer_del(q->tx_timer);
11826e790746SPaolo Bonzini         q->tx_waiting = 0;
11836e790746SPaolo Bonzini         virtio_net_flush_tx(q);
11846e790746SPaolo Bonzini     } else {
1185bc72ad67SAlex Bligh         timer_mod(q->tx_timer,
1186bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
11876e790746SPaolo Bonzini         q->tx_waiting = 1;
11886e790746SPaolo Bonzini         virtio_queue_set_notification(vq, 0);
11896e790746SPaolo Bonzini     }
11906e790746SPaolo Bonzini }
11916e790746SPaolo Bonzini 
11926e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
11936e790746SPaolo Bonzini {
119417a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
11956e790746SPaolo Bonzini     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
11966e790746SPaolo Bonzini 
11976e790746SPaolo Bonzini     if (unlikely(q->tx_waiting)) {
11986e790746SPaolo Bonzini         return;
11996e790746SPaolo Bonzini     }
12006e790746SPaolo Bonzini     q->tx_waiting = 1;
12016e790746SPaolo Bonzini     /* This happens when device was stopped but VCPU wasn't. */
120217a0ca55SKONRAD Frederic     if (!vdev->vm_running) {
12036e790746SPaolo Bonzini         return;
12046e790746SPaolo Bonzini     }
12056e790746SPaolo Bonzini     virtio_queue_set_notification(vq, 0);
12066e790746SPaolo Bonzini     qemu_bh_schedule(q->tx_bh);
12076e790746SPaolo Bonzini }
12086e790746SPaolo Bonzini 
12096e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque)
12106e790746SPaolo Bonzini {
12116e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
12126e790746SPaolo Bonzini     VirtIONet *n = q->n;
121317a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
121417a0ca55SKONRAD Frederic     assert(vdev->vm_running);
12156e790746SPaolo Bonzini 
12166e790746SPaolo Bonzini     q->tx_waiting = 0;
12176e790746SPaolo Bonzini 
12186e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
121917a0ca55SKONRAD Frederic     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
12206e790746SPaolo Bonzini         return;
122117a0ca55SKONRAD Frederic     }
12226e790746SPaolo Bonzini 
12236e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
12246e790746SPaolo Bonzini     virtio_net_flush_tx(q);
12256e790746SPaolo Bonzini }
12266e790746SPaolo Bonzini 
12276e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque)
12286e790746SPaolo Bonzini {
12296e790746SPaolo Bonzini     VirtIONetQueue *q = opaque;
12306e790746SPaolo Bonzini     VirtIONet *n = q->n;
123117a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12326e790746SPaolo Bonzini     int32_t ret;
12336e790746SPaolo Bonzini 
123417a0ca55SKONRAD Frederic     assert(vdev->vm_running);
12356e790746SPaolo Bonzini 
12366e790746SPaolo Bonzini     q->tx_waiting = 0;
12376e790746SPaolo Bonzini 
12386e790746SPaolo Bonzini     /* Just in case the driver is not ready on more */
123917a0ca55SKONRAD Frederic     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
12406e790746SPaolo Bonzini         return;
124117a0ca55SKONRAD Frederic     }
12426e790746SPaolo Bonzini 
12436e790746SPaolo Bonzini     ret = virtio_net_flush_tx(q);
12446e790746SPaolo Bonzini     if (ret == -EBUSY) {
12456e790746SPaolo Bonzini         return; /* Notification re-enable handled by tx_complete */
12466e790746SPaolo Bonzini     }
12476e790746SPaolo Bonzini 
12486e790746SPaolo Bonzini     /* If we flush a full burst of packets, assume there are
12496e790746SPaolo Bonzini      * more coming and immediately reschedule */
12506e790746SPaolo Bonzini     if (ret >= n->tx_burst) {
12516e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
12526e790746SPaolo Bonzini         q->tx_waiting = 1;
12536e790746SPaolo Bonzini         return;
12546e790746SPaolo Bonzini     }
12556e790746SPaolo Bonzini 
12566e790746SPaolo Bonzini     /* If less than a full burst, re-enable notification and flush
12576e790746SPaolo Bonzini      * anything that may have come in while we weren't looking.  If
12586e790746SPaolo Bonzini      * we find something, assume the guest is still active and reschedule */
12596e790746SPaolo Bonzini     virtio_queue_set_notification(q->tx_vq, 1);
12606e790746SPaolo Bonzini     if (virtio_net_flush_tx(q) > 0) {
12616e790746SPaolo Bonzini         virtio_queue_set_notification(q->tx_vq, 0);
12626e790746SPaolo Bonzini         qemu_bh_schedule(q->tx_bh);
12636e790746SPaolo Bonzini         q->tx_waiting = 1;
12646e790746SPaolo Bonzini     }
12656e790746SPaolo Bonzini }
12666e790746SPaolo Bonzini 
1267ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
12686e790746SPaolo Bonzini {
126917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
12706e790746SPaolo Bonzini     int i, max = multiqueue ? n->max_queues : 1;
12716e790746SPaolo Bonzini 
12726e790746SPaolo Bonzini     n->multiqueue = multiqueue;
12736e790746SPaolo Bonzini 
12746e790746SPaolo Bonzini     for (i = 2; i <= n->max_queues * 2 + 1; i++) {
12756e790746SPaolo Bonzini         virtio_del_queue(vdev, i);
12766e790746SPaolo Bonzini     }
12776e790746SPaolo Bonzini 
12786e790746SPaolo Bonzini     for (i = 1; i < max; i++) {
12796e790746SPaolo Bonzini         n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
12806e790746SPaolo Bonzini         if (n->vqs[i].tx_timer) {
12816e790746SPaolo Bonzini             n->vqs[i].tx_vq =
12826e790746SPaolo Bonzini                 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1283bc72ad67SAlex Bligh             n->vqs[i].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
12846e790746SPaolo Bonzini                                                    virtio_net_tx_timer,
12856e790746SPaolo Bonzini                                                    &n->vqs[i]);
12866e790746SPaolo Bonzini         } else {
12876e790746SPaolo Bonzini             n->vqs[i].tx_vq =
12886e790746SPaolo Bonzini                 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
12896e790746SPaolo Bonzini             n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]);
12906e790746SPaolo Bonzini         }
12916e790746SPaolo Bonzini 
12926e790746SPaolo Bonzini         n->vqs[i].tx_waiting = 0;
12936e790746SPaolo Bonzini         n->vqs[i].n = n;
12946e790746SPaolo Bonzini     }
12956e790746SPaolo Bonzini 
1296ec57db16SJason Wang     /* Note: Minux Guests (version 3.2.1) use ctrl vq but don't ack
1297ec57db16SJason Wang      * VIRTIO_NET_F_CTRL_VQ. Create ctrl vq unconditionally to avoid
1298ec57db16SJason Wang      * breaking them.
1299ec57db16SJason Wang      */
13006e790746SPaolo Bonzini     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
13016e790746SPaolo Bonzini 
13026e790746SPaolo Bonzini     virtio_net_set_queues(n);
13036e790746SPaolo Bonzini }
13046e790746SPaolo Bonzini 
13056e790746SPaolo Bonzini static void virtio_net_save(QEMUFile *f, void *opaque)
13066e790746SPaolo Bonzini {
13076e790746SPaolo Bonzini     int i;
13086e790746SPaolo Bonzini     VirtIONet *n = opaque;
130917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13106e790746SPaolo Bonzini 
13116e790746SPaolo Bonzini     /* At this point, backend must be stopped, otherwise
13126e790746SPaolo Bonzini      * it might keep writing to memory. */
13136e790746SPaolo Bonzini     assert(!n->vhost_started);
131417a0ca55SKONRAD Frederic     virtio_save(vdev, f);
13156e790746SPaolo Bonzini 
13166e790746SPaolo Bonzini     qemu_put_buffer(f, n->mac, ETH_ALEN);
13176e790746SPaolo Bonzini     qemu_put_be32(f, n->vqs[0].tx_waiting);
13186e790746SPaolo Bonzini     qemu_put_be32(f, n->mergeable_rx_bufs);
13196e790746SPaolo Bonzini     qemu_put_be16(f, n->status);
13206e790746SPaolo Bonzini     qemu_put_byte(f, n->promisc);
13216e790746SPaolo Bonzini     qemu_put_byte(f, n->allmulti);
13226e790746SPaolo Bonzini     qemu_put_be32(f, n->mac_table.in_use);
13236e790746SPaolo Bonzini     qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
13246e790746SPaolo Bonzini     qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
13256e790746SPaolo Bonzini     qemu_put_be32(f, n->has_vnet_hdr);
13266e790746SPaolo Bonzini     qemu_put_byte(f, n->mac_table.multi_overflow);
13276e790746SPaolo Bonzini     qemu_put_byte(f, n->mac_table.uni_overflow);
13286e790746SPaolo Bonzini     qemu_put_byte(f, n->alluni);
13296e790746SPaolo Bonzini     qemu_put_byte(f, n->nomulti);
13306e790746SPaolo Bonzini     qemu_put_byte(f, n->nouni);
13316e790746SPaolo Bonzini     qemu_put_byte(f, n->nobcast);
13326e790746SPaolo Bonzini     qemu_put_byte(f, n->has_ufo);
13336e790746SPaolo Bonzini     if (n->max_queues > 1) {
13346e790746SPaolo Bonzini         qemu_put_be16(f, n->max_queues);
13356e790746SPaolo Bonzini         qemu_put_be16(f, n->curr_queues);
13366e790746SPaolo Bonzini         for (i = 1; i < n->curr_queues; i++) {
13376e790746SPaolo Bonzini             qemu_put_be32(f, n->vqs[i].tx_waiting);
13386e790746SPaolo Bonzini         }
13396e790746SPaolo Bonzini     }
1340644c9858SDmitry Fleytman 
1341644c9858SDmitry Fleytman     if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) {
1342644c9858SDmitry Fleytman         qemu_put_be64(f, n->curr_guest_offloads);
1343644c9858SDmitry Fleytman     }
13446e790746SPaolo Bonzini }
13456e790746SPaolo Bonzini 
13466e790746SPaolo Bonzini static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
13476e790746SPaolo Bonzini {
13486e790746SPaolo Bonzini     VirtIONet *n = opaque;
134917a0ca55SKONRAD Frederic     VirtIODevice *vdev = VIRTIO_DEVICE(n);
13506e790746SPaolo Bonzini     int ret, i, link_down;
13516e790746SPaolo Bonzini 
13526e790746SPaolo Bonzini     if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
13536e790746SPaolo Bonzini         return -EINVAL;
13546e790746SPaolo Bonzini 
135517a0ca55SKONRAD Frederic     ret = virtio_load(vdev, f);
13566e790746SPaolo Bonzini     if (ret) {
13576e790746SPaolo Bonzini         return ret;
13586e790746SPaolo Bonzini     }
13596e790746SPaolo Bonzini 
13606e790746SPaolo Bonzini     qemu_get_buffer(f, n->mac, ETH_ALEN);
13616e790746SPaolo Bonzini     n->vqs[0].tx_waiting = qemu_get_be32(f);
13626e790746SPaolo Bonzini 
13636e790746SPaolo Bonzini     virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
13646e790746SPaolo Bonzini 
13656e790746SPaolo Bonzini     if (version_id >= 3)
13666e790746SPaolo Bonzini         n->status = qemu_get_be16(f);
13676e790746SPaolo Bonzini 
13686e790746SPaolo Bonzini     if (version_id >= 4) {
13696e790746SPaolo Bonzini         if (version_id < 8) {
13706e790746SPaolo Bonzini             n->promisc = qemu_get_be32(f);
13716e790746SPaolo Bonzini             n->allmulti = qemu_get_be32(f);
13726e790746SPaolo Bonzini         } else {
13736e790746SPaolo Bonzini             n->promisc = qemu_get_byte(f);
13746e790746SPaolo Bonzini             n->allmulti = qemu_get_byte(f);
13756e790746SPaolo Bonzini         }
13766e790746SPaolo Bonzini     }
13776e790746SPaolo Bonzini 
13786e790746SPaolo Bonzini     if (version_id >= 5) {
13796e790746SPaolo Bonzini         n->mac_table.in_use = qemu_get_be32(f);
13806e790746SPaolo Bonzini         /* MAC_TABLE_ENTRIES may be different from the saved image */
13816e790746SPaolo Bonzini         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
13826e790746SPaolo Bonzini             qemu_get_buffer(f, n->mac_table.macs,
13836e790746SPaolo Bonzini                             n->mac_table.in_use * ETH_ALEN);
138498f93dddSMichael S. Tsirkin         } else {
138598f93dddSMichael S. Tsirkin             int64_t i;
138698f93dddSMichael S. Tsirkin 
138798f93dddSMichael S. Tsirkin             /* Overflow detected - can happen if source has a larger MAC table.
138898f93dddSMichael S. Tsirkin              * We simply set overflow flag so there's no need to maintain the
138998f93dddSMichael S. Tsirkin              * table of addresses, discard them all.
139098f93dddSMichael S. Tsirkin              * Note: 64 bit math to avoid integer overflow.
139198f93dddSMichael S. Tsirkin              */
139298f93dddSMichael S. Tsirkin             for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
139398f93dddSMichael S. Tsirkin                 qemu_get_byte(f);
139498f93dddSMichael S. Tsirkin             }
13956e790746SPaolo Bonzini             n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
13966e790746SPaolo Bonzini             n->mac_table.in_use = 0;
13976e790746SPaolo Bonzini         }
13986e790746SPaolo Bonzini     }
13996e790746SPaolo Bonzini 
14006e790746SPaolo Bonzini     if (version_id >= 6)
14016e790746SPaolo Bonzini         qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
14026e790746SPaolo Bonzini 
14036e790746SPaolo Bonzini     if (version_id >= 7) {
14046e790746SPaolo Bonzini         if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
14056e790746SPaolo Bonzini             error_report("virtio-net: saved image requires vnet_hdr=on");
14066e790746SPaolo Bonzini             return -1;
14076e790746SPaolo Bonzini         }
14086e790746SPaolo Bonzini     }
14096e790746SPaolo Bonzini 
14106e790746SPaolo Bonzini     if (version_id >= 9) {
14116e790746SPaolo Bonzini         n->mac_table.multi_overflow = qemu_get_byte(f);
14126e790746SPaolo Bonzini         n->mac_table.uni_overflow = qemu_get_byte(f);
14136e790746SPaolo Bonzini     }
14146e790746SPaolo Bonzini 
14156e790746SPaolo Bonzini     if (version_id >= 10) {
14166e790746SPaolo Bonzini         n->alluni = qemu_get_byte(f);
14176e790746SPaolo Bonzini         n->nomulti = qemu_get_byte(f);
14186e790746SPaolo Bonzini         n->nouni = qemu_get_byte(f);
14196e790746SPaolo Bonzini         n->nobcast = qemu_get_byte(f);
14206e790746SPaolo Bonzini     }
14216e790746SPaolo Bonzini 
14226e790746SPaolo Bonzini     if (version_id >= 11) {
14236e790746SPaolo Bonzini         if (qemu_get_byte(f) && !peer_has_ufo(n)) {
14246e790746SPaolo Bonzini             error_report("virtio-net: saved image requires TUN_F_UFO support");
14256e790746SPaolo Bonzini             return -1;
14266e790746SPaolo Bonzini         }
14276e790746SPaolo Bonzini     }
14286e790746SPaolo Bonzini 
14296e790746SPaolo Bonzini     if (n->max_queues > 1) {
14306e790746SPaolo Bonzini         if (n->max_queues != qemu_get_be16(f)) {
14316e790746SPaolo Bonzini             error_report("virtio-net: different max_queues ");
14326e790746SPaolo Bonzini             return -1;
14336e790746SPaolo Bonzini         }
14346e790746SPaolo Bonzini 
14356e790746SPaolo Bonzini         n->curr_queues = qemu_get_be16(f);
1436eea750a5SMichael S. Tsirkin         if (n->curr_queues > n->max_queues) {
1437eea750a5SMichael S. Tsirkin             error_report("virtio-net: curr_queues %x > max_queues %x",
1438eea750a5SMichael S. Tsirkin                          n->curr_queues, n->max_queues);
1439eea750a5SMichael S. Tsirkin             return -1;
1440eea750a5SMichael S. Tsirkin         }
14416e790746SPaolo Bonzini         for (i = 1; i < n->curr_queues; i++) {
14426e790746SPaolo Bonzini             n->vqs[i].tx_waiting = qemu_get_be32(f);
14436e790746SPaolo Bonzini         }
14446e790746SPaolo Bonzini     }
14456e790746SPaolo Bonzini 
1446644c9858SDmitry Fleytman     if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) {
1447644c9858SDmitry Fleytman         n->curr_guest_offloads = qemu_get_be64(f);
1448644c9858SDmitry Fleytman     } else {
1449644c9858SDmitry Fleytman         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1450644c9858SDmitry Fleytman     }
1451644c9858SDmitry Fleytman 
1452644c9858SDmitry Fleytman     if (peer_has_vnet_hdr(n)) {
1453644c9858SDmitry Fleytman         virtio_net_apply_guest_offloads(n);
1454644c9858SDmitry Fleytman     }
1455644c9858SDmitry Fleytman 
14566e790746SPaolo Bonzini     virtio_net_set_queues(n);
14576e790746SPaolo Bonzini 
14586e790746SPaolo Bonzini     /* Find the first multicast entry in the saved MAC filter */
14596e790746SPaolo Bonzini     for (i = 0; i < n->mac_table.in_use; i++) {
14606e790746SPaolo Bonzini         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
14616e790746SPaolo Bonzini             break;
14626e790746SPaolo Bonzini         }
14636e790746SPaolo Bonzini     }
14646e790746SPaolo Bonzini     n->mac_table.first_multi = i;
14656e790746SPaolo Bonzini 
14666e790746SPaolo Bonzini     /* nc.link_down can't be migrated, so infer link_down according
14676e790746SPaolo Bonzini      * to link status bit in n->status */
14686e790746SPaolo Bonzini     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
14696e790746SPaolo Bonzini     for (i = 0; i < n->max_queues; i++) {
14706e790746SPaolo Bonzini         qemu_get_subqueue(n->nic, i)->link_down = link_down;
14716e790746SPaolo Bonzini     }
14726e790746SPaolo Bonzini 
1473f57fcf70SJason Wang     if (vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1474f57fcf70SJason Wang         vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
1475f57fcf70SJason Wang         n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1476f57fcf70SJason Wang         timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1477f57fcf70SJason Wang     }
1478f57fcf70SJason Wang 
14796e790746SPaolo Bonzini     return 0;
14806e790746SPaolo Bonzini }
14816e790746SPaolo Bonzini 
14826e790746SPaolo Bonzini static void virtio_net_cleanup(NetClientState *nc)
14836e790746SPaolo Bonzini {
14846e790746SPaolo Bonzini     VirtIONet *n = qemu_get_nic_opaque(nc);
14856e790746SPaolo Bonzini 
14866e790746SPaolo Bonzini     n->nic = NULL;
14876e790746SPaolo Bonzini }
14886e790746SPaolo Bonzini 
14896e790746SPaolo Bonzini static NetClientInfo net_virtio_info = {
14906e790746SPaolo Bonzini     .type = NET_CLIENT_OPTIONS_KIND_NIC,
14916e790746SPaolo Bonzini     .size = sizeof(NICState),
14926e790746SPaolo Bonzini     .can_receive = virtio_net_can_receive,
14936e790746SPaolo Bonzini     .receive = virtio_net_receive,
14946e790746SPaolo Bonzini     .cleanup = virtio_net_cleanup,
14956e790746SPaolo Bonzini     .link_status_changed = virtio_net_set_link_status,
1496b1be4280SAmos Kong     .query_rx_filter = virtio_net_query_rxfilter,
14976e790746SPaolo Bonzini };
14986e790746SPaolo Bonzini 
14996e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
15006e790746SPaolo Bonzini {
150117a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15026e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
15036e790746SPaolo Bonzini     assert(n->vhost_started);
1504*ed8b4afeSNikolay Nikolaev     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
15056e790746SPaolo Bonzini }
15066e790746SPaolo Bonzini 
15076e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
15086e790746SPaolo Bonzini                                            bool mask)
15096e790746SPaolo Bonzini {
151017a0ca55SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(vdev);
15116e790746SPaolo Bonzini     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
15126e790746SPaolo Bonzini     assert(n->vhost_started);
1513*ed8b4afeSNikolay Nikolaev     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
15146e790746SPaolo Bonzini                              vdev, idx, mask);
15156e790746SPaolo Bonzini }
15166e790746SPaolo Bonzini 
151717ec5a86SKONRAD Frederic void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features)
15186e790746SPaolo Bonzini {
15196e790746SPaolo Bonzini     int i, config_size = 0;
1520e9016ee2SJason Wang     host_features |= (1 << VIRTIO_NET_F_MAC);
15216e790746SPaolo Bonzini     for (i = 0; feature_sizes[i].flags != 0; i++) {
15226e790746SPaolo Bonzini         if (host_features & feature_sizes[i].flags) {
15236e790746SPaolo Bonzini             config_size = MAX(feature_sizes[i].end, config_size);
15246e790746SPaolo Bonzini         }
15256e790746SPaolo Bonzini     }
152617ec5a86SKONRAD Frederic     n->config_size = config_size;
152717ec5a86SKONRAD Frederic }
15286e790746SPaolo Bonzini 
15298a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
15308a253ec2SKONRAD Frederic                                    const char *type)
15318a253ec2SKONRAD Frederic {
15328a253ec2SKONRAD Frederic     /*
15338a253ec2SKONRAD Frederic      * The name can be NULL, the netclient name will be type.x.
15348a253ec2SKONRAD Frederic      */
15358a253ec2SKONRAD Frederic     assert(type != NULL);
15368a253ec2SKONRAD Frederic 
15378a253ec2SKONRAD Frederic     g_free(n->netclient_name);
15388a253ec2SKONRAD Frederic     g_free(n->netclient_type);
15398a253ec2SKONRAD Frederic     n->netclient_name = g_strdup(name);
15408a253ec2SKONRAD Frederic     n->netclient_type = g_strdup(type);
15418a253ec2SKONRAD Frederic }
15428a253ec2SKONRAD Frederic 
1543e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp)
154417ec5a86SKONRAD Frederic {
1545e6f746b3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1546284a32f0SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
1547284a32f0SAndreas Färber     NetClientState *nc;
15481773d9eeSKONRAD Frederic     int i;
154917ec5a86SKONRAD Frederic 
1550284a32f0SAndreas Färber     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
155117ec5a86SKONRAD Frederic 
15521773d9eeSKONRAD Frederic     n->max_queues = MAX(n->nic_conf.queues, 1);
15536e790746SPaolo Bonzini     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
155417a0ca55SKONRAD Frederic     n->vqs[0].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
15556e790746SPaolo Bonzini     n->curr_queues = 1;
15566e790746SPaolo Bonzini     n->vqs[0].n = n;
15571773d9eeSKONRAD Frederic     n->tx_timeout = n->net_conf.txtimer;
15586e790746SPaolo Bonzini 
15591773d9eeSKONRAD Frederic     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
15601773d9eeSKONRAD Frederic                        && strcmp(n->net_conf.tx, "bh")) {
15616e790746SPaolo Bonzini         error_report("virtio-net: "
15626e790746SPaolo Bonzini                      "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
15631773d9eeSKONRAD Frederic                      n->net_conf.tx);
15646e790746SPaolo Bonzini         error_report("Defaulting to \"bh\"");
15656e790746SPaolo Bonzini     }
15666e790746SPaolo Bonzini 
15671773d9eeSKONRAD Frederic     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
156817a0ca55SKONRAD Frederic         n->vqs[0].tx_vq = virtio_add_queue(vdev, 256,
15696e790746SPaolo Bonzini                                            virtio_net_handle_tx_timer);
1570bc72ad67SAlex Bligh         n->vqs[0].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, virtio_net_tx_timer,
15716e790746SPaolo Bonzini                                                &n->vqs[0]);
15726e790746SPaolo Bonzini     } else {
157317a0ca55SKONRAD Frederic         n->vqs[0].tx_vq = virtio_add_queue(vdev, 256,
15746e790746SPaolo Bonzini                                            virtio_net_handle_tx_bh);
15756e790746SPaolo Bonzini         n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]);
15766e790746SPaolo Bonzini     }
157717a0ca55SKONRAD Frederic     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
15781773d9eeSKONRAD Frederic     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
15791773d9eeSKONRAD Frederic     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
15806e790746SPaolo Bonzini     n->status = VIRTIO_NET_S_LINK_UP;
1581f57fcf70SJason Wang     n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1582f57fcf70SJason Wang                                      virtio_net_announce_timer, n);
15836e790746SPaolo Bonzini 
15848a253ec2SKONRAD Frederic     if (n->netclient_type) {
15858a253ec2SKONRAD Frederic         /*
15868a253ec2SKONRAD Frederic          * Happen when virtio_net_set_netclient_name has been called.
15878a253ec2SKONRAD Frederic          */
15888a253ec2SKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
15898a253ec2SKONRAD Frederic                               n->netclient_type, n->netclient_name, n);
15908a253ec2SKONRAD Frederic     } else {
15911773d9eeSKONRAD Frederic         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1592284a32f0SAndreas Färber                               object_get_typename(OBJECT(dev)), dev->id, n);
15938a253ec2SKONRAD Frederic     }
15948a253ec2SKONRAD Frederic 
15956e790746SPaolo Bonzini     peer_test_vnet_hdr(n);
15966e790746SPaolo Bonzini     if (peer_has_vnet_hdr(n)) {
15976e790746SPaolo Bonzini         for (i = 0; i < n->max_queues; i++) {
1598d6085e3aSStefan Hajnoczi             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
15996e790746SPaolo Bonzini         }
16006e790746SPaolo Bonzini         n->host_hdr_len = sizeof(struct virtio_net_hdr);
16016e790746SPaolo Bonzini     } else {
16026e790746SPaolo Bonzini         n->host_hdr_len = 0;
16036e790746SPaolo Bonzini     }
16046e790746SPaolo Bonzini 
16051773d9eeSKONRAD Frederic     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
16066e790746SPaolo Bonzini 
16076e790746SPaolo Bonzini     n->vqs[0].tx_waiting = 0;
16081773d9eeSKONRAD Frederic     n->tx_burst = n->net_conf.txburst;
16096e790746SPaolo Bonzini     virtio_net_set_mrg_rx_bufs(n, 0);
16106e790746SPaolo Bonzini     n->promisc = 1; /* for compatibility */
16116e790746SPaolo Bonzini 
16126e790746SPaolo Bonzini     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
16136e790746SPaolo Bonzini 
16146e790746SPaolo Bonzini     n->vlans = g_malloc0(MAX_VLAN >> 3);
16156e790746SPaolo Bonzini 
1616b1be4280SAmos Kong     nc = qemu_get_queue(n->nic);
1617b1be4280SAmos Kong     nc->rxfilter_notify_enabled = 1;
1618b1be4280SAmos Kong 
1619284a32f0SAndreas Färber     n->qdev = dev;
1620284a32f0SAndreas Färber     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
16216e790746SPaolo Bonzini                     virtio_net_save, virtio_net_load, n);
16226e790746SPaolo Bonzini 
1623284a32f0SAndreas Färber     add_boot_device_path(n->nic_conf.bootindex, dev, "/ethernet-phy@0");
162417ec5a86SKONRAD Frederic }
162517ec5a86SKONRAD Frederic 
1626306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
162717ec5a86SKONRAD Frederic {
1628306ec6c3SAndreas Färber     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1629306ec6c3SAndreas Färber     VirtIONet *n = VIRTIO_NET(dev);
163017ec5a86SKONRAD Frederic     int i;
163117ec5a86SKONRAD Frederic 
163217ec5a86SKONRAD Frederic     /* This will stop vhost backend if appropriate. */
163317ec5a86SKONRAD Frederic     virtio_net_set_status(vdev, 0);
163417ec5a86SKONRAD Frederic 
1635306ec6c3SAndreas Färber     unregister_savevm(dev, "virtio-net", n);
163617ec5a86SKONRAD Frederic 
16378a253ec2SKONRAD Frederic     g_free(n->netclient_name);
16388a253ec2SKONRAD Frederic     n->netclient_name = NULL;
16398a253ec2SKONRAD Frederic     g_free(n->netclient_type);
16408a253ec2SKONRAD Frederic     n->netclient_type = NULL;
16418a253ec2SKONRAD Frederic 
164217ec5a86SKONRAD Frederic     g_free(n->mac_table.macs);
164317ec5a86SKONRAD Frederic     g_free(n->vlans);
164417ec5a86SKONRAD Frederic 
164517ec5a86SKONRAD Frederic     for (i = 0; i < n->max_queues; i++) {
164617ec5a86SKONRAD Frederic         VirtIONetQueue *q = &n->vqs[i];
164717ec5a86SKONRAD Frederic         NetClientState *nc = qemu_get_subqueue(n->nic, i);
164817ec5a86SKONRAD Frederic 
164917ec5a86SKONRAD Frederic         qemu_purge_queued_packets(nc);
165017ec5a86SKONRAD Frederic 
165117ec5a86SKONRAD Frederic         if (q->tx_timer) {
1652bc72ad67SAlex Bligh             timer_del(q->tx_timer);
1653bc72ad67SAlex Bligh             timer_free(q->tx_timer);
1654fe2dafa0SJason Wang         } else if (q->tx_bh) {
165517ec5a86SKONRAD Frederic             qemu_bh_delete(q->tx_bh);
165617ec5a86SKONRAD Frederic         }
165717ec5a86SKONRAD Frederic     }
165817ec5a86SKONRAD Frederic 
1659f57fcf70SJason Wang     timer_del(n->announce_timer);
1660f57fcf70SJason Wang     timer_free(n->announce_timer);
166117ec5a86SKONRAD Frederic     g_free(n->vqs);
166217ec5a86SKONRAD Frederic     qemu_del_nic(n->nic);
16636a1a8cc7SKONRAD Frederic     virtio_cleanup(vdev);
166417ec5a86SKONRAD Frederic }
166517ec5a86SKONRAD Frederic 
166617ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj)
166717ec5a86SKONRAD Frederic {
166817ec5a86SKONRAD Frederic     VirtIONet *n = VIRTIO_NET(obj);
166917ec5a86SKONRAD Frederic 
167017ec5a86SKONRAD Frederic     /*
167117ec5a86SKONRAD Frederic      * The default config_size is sizeof(struct virtio_net_config).
167217ec5a86SKONRAD Frederic      * Can be overriden with virtio_net_set_config_size.
167317ec5a86SKONRAD Frederic      */
167417ec5a86SKONRAD Frederic     n->config_size = sizeof(struct virtio_net_config);
167517ec5a86SKONRAD Frederic }
167617ec5a86SKONRAD Frederic 
167717ec5a86SKONRAD Frederic static Property virtio_net_properties[] = {
167817ec5a86SKONRAD Frederic     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
167917ec5a86SKONRAD Frederic     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
168017ec5a86SKONRAD Frederic                                                TX_TIMER_INTERVAL),
168117ec5a86SKONRAD Frederic     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
168217ec5a86SKONRAD Frederic     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
168317ec5a86SKONRAD Frederic     DEFINE_PROP_END_OF_LIST(),
168417ec5a86SKONRAD Frederic };
168517ec5a86SKONRAD Frederic 
168617ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data)
168717ec5a86SKONRAD Frederic {
168817ec5a86SKONRAD Frederic     DeviceClass *dc = DEVICE_CLASS(klass);
168917ec5a86SKONRAD Frederic     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1690e6f746b3SAndreas Färber 
169117ec5a86SKONRAD Frederic     dc->props = virtio_net_properties;
1692125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1693e6f746b3SAndreas Färber     vdc->realize = virtio_net_device_realize;
1694306ec6c3SAndreas Färber     vdc->unrealize = virtio_net_device_unrealize;
169517ec5a86SKONRAD Frederic     vdc->get_config = virtio_net_get_config;
169617ec5a86SKONRAD Frederic     vdc->set_config = virtio_net_set_config;
169717ec5a86SKONRAD Frederic     vdc->get_features = virtio_net_get_features;
169817ec5a86SKONRAD Frederic     vdc->set_features = virtio_net_set_features;
169917ec5a86SKONRAD Frederic     vdc->bad_features = virtio_net_bad_features;
170017ec5a86SKONRAD Frederic     vdc->reset = virtio_net_reset;
170117ec5a86SKONRAD Frederic     vdc->set_status = virtio_net_set_status;
170217ec5a86SKONRAD Frederic     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
170317ec5a86SKONRAD Frederic     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
170417ec5a86SKONRAD Frederic }
170517ec5a86SKONRAD Frederic 
170617ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = {
170717ec5a86SKONRAD Frederic     .name = TYPE_VIRTIO_NET,
170817ec5a86SKONRAD Frederic     .parent = TYPE_VIRTIO_DEVICE,
170917ec5a86SKONRAD Frederic     .instance_size = sizeof(VirtIONet),
171017ec5a86SKONRAD Frederic     .instance_init = virtio_net_instance_init,
171117ec5a86SKONRAD Frederic     .class_init = virtio_net_class_init,
171217ec5a86SKONRAD Frederic };
171317ec5a86SKONRAD Frederic 
171417ec5a86SKONRAD Frederic static void virtio_register_types(void)
171517ec5a86SKONRAD Frederic {
171617ec5a86SKONRAD Frederic     type_register_static(&virtio_net_info);
171717ec5a86SKONRAD Frederic }
171817ec5a86SKONRAD Frederic 
171917ec5a86SKONRAD Frederic type_init(virtio_register_types)
1720