16e790746SPaolo Bonzini /* 26e790746SPaolo Bonzini * Virtio Network Device 36e790746SPaolo Bonzini * 46e790746SPaolo Bonzini * Copyright IBM, Corp. 2007 56e790746SPaolo Bonzini * 66e790746SPaolo Bonzini * Authors: 76e790746SPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 86e790746SPaolo Bonzini * 96e790746SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 106e790746SPaolo Bonzini * the COPYING file in the top-level directory. 116e790746SPaolo Bonzini * 126e790746SPaolo Bonzini */ 136e790746SPaolo Bonzini 149b8bfe21SPeter Maydell #include "qemu/osdep.h" 156e790746SPaolo Bonzini #include "qemu/iov.h" 166e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 176e790746SPaolo Bonzini #include "net/net.h" 186e790746SPaolo Bonzini #include "net/checksum.h" 196e790746SPaolo Bonzini #include "net/tap.h" 206e790746SPaolo Bonzini #include "qemu/error-report.h" 216e790746SPaolo Bonzini #include "qemu/timer.h" 226e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 236e790746SPaolo Bonzini #include "net/vhost_net.h" 2417ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 25b1be4280SAmos Kong #include "qapi/qmp/qjson.h" 2606150279SWenchao Xia #include "qapi-event.h" 271399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 286e790746SPaolo Bonzini 296e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 306e790746SPaolo Bonzini 316e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 326e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 336e790746SPaolo Bonzini 341c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 351c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 361c0fbfa3SMichael S. Tsirkin /* for now, only allow larger queues; with virtio-1, guest can downsize */ 371c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 381c0fbfa3SMichael S. Tsirkin 396e790746SPaolo Bonzini /* 406e790746SPaolo Bonzini * Calculate the number of bytes up to and including the given 'field' of 416e790746SPaolo Bonzini * 'container'. 426e790746SPaolo Bonzini */ 436e790746SPaolo Bonzini #define endof(container, field) \ 446e790746SPaolo Bonzini (offsetof(container, field) + sizeof(((container *)0)->field)) 456e790746SPaolo Bonzini 466e790746SPaolo Bonzini typedef struct VirtIOFeature { 476e790746SPaolo Bonzini uint32_t flags; 486e790746SPaolo Bonzini size_t end; 496e790746SPaolo Bonzini } VirtIOFeature; 506e790746SPaolo Bonzini 516e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = { 526e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MAC, 536e790746SPaolo Bonzini .end = endof(struct virtio_net_config, mac)}, 546e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_STATUS, 556e790746SPaolo Bonzini .end = endof(struct virtio_net_config, status)}, 566e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MQ, 576e790746SPaolo Bonzini .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 58a93e599dSMaxime Coquelin {.flags = 1 << VIRTIO_NET_F_MTU, 59a93e599dSMaxime Coquelin .end = endof(struct virtio_net_config, mtu)}, 606e790746SPaolo Bonzini {} 616e790746SPaolo Bonzini }; 626e790746SPaolo Bonzini 636e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 646e790746SPaolo Bonzini { 656e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 666e790746SPaolo Bonzini 676e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 686e790746SPaolo Bonzini } 696e790746SPaolo Bonzini 706e790746SPaolo Bonzini static int vq2q(int queue_index) 716e790746SPaolo Bonzini { 726e790746SPaolo Bonzini return queue_index / 2; 736e790746SPaolo Bonzini } 746e790746SPaolo Bonzini 756e790746SPaolo Bonzini /* TODO 766e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 776e790746SPaolo Bonzini */ 786e790746SPaolo Bonzini 796e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 806e790746SPaolo Bonzini { 8117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 826e790746SPaolo Bonzini struct virtio_net_config netcfg; 836e790746SPaolo Bonzini 841399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 851399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues); 86a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 876e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 886e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 896e790746SPaolo Bonzini } 906e790746SPaolo Bonzini 916e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 926e790746SPaolo Bonzini { 9317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 946e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 956e790746SPaolo Bonzini 966e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 976e790746SPaolo Bonzini 9895129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 9995129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1006e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1016e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 1026e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1036e790746SPaolo Bonzini } 1046e790746SPaolo Bonzini } 1056e790746SPaolo Bonzini 1066e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 1076e790746SPaolo Bonzini { 10817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1096e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 11017a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 1116e790746SPaolo Bonzini } 1126e790746SPaolo Bonzini 113f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 114f57fcf70SJason Wang { 115f57fcf70SJason Wang VirtIONet *n = opaque; 116f57fcf70SJason Wang VirtIODevice *vdev = VIRTIO_DEVICE(n); 117f57fcf70SJason Wang 118f57fcf70SJason Wang n->announce_counter--; 119f57fcf70SJason Wang n->status |= VIRTIO_NET_S_ANNOUNCE; 120f57fcf70SJason Wang virtio_notify_config(vdev); 121f57fcf70SJason Wang } 122f57fcf70SJason Wang 1236e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 1246e790746SPaolo Bonzini { 12517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1266e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 1276e790746SPaolo Bonzini int queues = n->multiqueue ? n->max_queues : 1; 1286e790746SPaolo Bonzini 129ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 1306e790746SPaolo Bonzini return; 1316e790746SPaolo Bonzini } 1326e790746SPaolo Bonzini 1338c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 1348c1ac475SRadim Krčmář !!n->vhost_started) { 1356e790746SPaolo Bonzini return; 1366e790746SPaolo Bonzini } 1376e790746SPaolo Bonzini if (!n->vhost_started) { 138086abc1cSMichael S. Tsirkin int r, i; 139086abc1cSMichael S. Tsirkin 1401bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1411bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 1421bfa316cSGreg Kurz "falling back on userspace virtio", 1431bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 1441bfa316cSGreg Kurz return; 1451bfa316cSGreg Kurz } 1461bfa316cSGreg Kurz 147086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 148086abc1cSMichael S. Tsirkin * when vhost is running. 149086abc1cSMichael S. Tsirkin */ 150086abc1cSMichael S. Tsirkin for (i = 0; i < queues; i++) { 151086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 152086abc1cSMichael S. Tsirkin 153086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 154086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 155086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 156086abc1cSMichael S. Tsirkin } 157086abc1cSMichael S. Tsirkin 158a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 159a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 160a93e599dSMaxime Coquelin if (r < 0) { 161a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 162a93e599dSMaxime Coquelin n->net_conf.mtu); 163a93e599dSMaxime Coquelin 164a93e599dSMaxime Coquelin return; 165a93e599dSMaxime Coquelin } 166a93e599dSMaxime Coquelin } 167a93e599dSMaxime Coquelin 1686e790746SPaolo Bonzini n->vhost_started = 1; 16917a0ca55SKONRAD Frederic r = vhost_net_start(vdev, n->nic->ncs, queues); 1706e790746SPaolo Bonzini if (r < 0) { 1716e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 1726e790746SPaolo Bonzini "falling back on userspace virtio", -r); 1736e790746SPaolo Bonzini n->vhost_started = 0; 1746e790746SPaolo Bonzini } 1756e790746SPaolo Bonzini } else { 17617a0ca55SKONRAD Frederic vhost_net_stop(vdev, n->nic->ncs, queues); 1776e790746SPaolo Bonzini n->vhost_started = 0; 1786e790746SPaolo Bonzini } 1796e790746SPaolo Bonzini } 1806e790746SPaolo Bonzini 1811bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 1821bfa316cSGreg Kurz NetClientState *peer, 1831bfa316cSGreg Kurz bool enable) 1841bfa316cSGreg Kurz { 1851bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 1861bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 1871bfa316cSGreg Kurz } else { 1881bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 1891bfa316cSGreg Kurz } 1901bfa316cSGreg Kurz } 1911bfa316cSGreg Kurz 1921bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 1931bfa316cSGreg Kurz int queues, bool enable) 1941bfa316cSGreg Kurz { 1951bfa316cSGreg Kurz int i; 1961bfa316cSGreg Kurz 1971bfa316cSGreg Kurz for (i = 0; i < queues; i++) { 1981bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 1991bfa316cSGreg Kurz enable) { 2001bfa316cSGreg Kurz while (--i >= 0) { 2011bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 2021bfa316cSGreg Kurz } 2031bfa316cSGreg Kurz 2041bfa316cSGreg Kurz return true; 2051bfa316cSGreg Kurz } 2061bfa316cSGreg Kurz } 2071bfa316cSGreg Kurz 2081bfa316cSGreg Kurz return false; 2091bfa316cSGreg Kurz } 2101bfa316cSGreg Kurz 2111bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 2121bfa316cSGreg Kurz { 2131bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 2141bfa316cSGreg Kurz int queues = n->multiqueue ? n->max_queues : 1; 2151bfa316cSGreg Kurz 2161bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 2171bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 2181bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 2191bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 2201bfa316cSGreg Kurz * virtio-net code. 2211bfa316cSGreg Kurz */ 2221bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 2231bfa316cSGreg Kurz queues, true); 2241bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 2251bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 2261bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 2271bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 2281bfa316cSGreg Kurz * endianness. 2291bfa316cSGreg Kurz */ 2301bfa316cSGreg Kurz virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false); 2311bfa316cSGreg Kurz } 2321bfa316cSGreg Kurz } 2331bfa316cSGreg Kurz 234283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 235283e2c2aSYuri Benditovich { 236283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 237283e2c2aSYuri Benditovich if (dropped) { 238283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 239283e2c2aSYuri Benditovich } 240283e2c2aSYuri Benditovich } 241283e2c2aSYuri Benditovich 2426e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 2436e790746SPaolo Bonzini { 24417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 2456e790746SPaolo Bonzini VirtIONetQueue *q; 2466e790746SPaolo Bonzini int i; 2476e790746SPaolo Bonzini uint8_t queue_status; 2486e790746SPaolo Bonzini 2491bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 2506e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 2516e790746SPaolo Bonzini 2526e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 25338705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 25438705bb5SFam Zheng bool queue_started; 2556e790746SPaolo Bonzini q = &n->vqs[i]; 2566e790746SPaolo Bonzini 2576e790746SPaolo Bonzini if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { 2586e790746SPaolo Bonzini queue_status = 0; 2596e790746SPaolo Bonzini } else { 2606e790746SPaolo Bonzini queue_status = status; 2616e790746SPaolo Bonzini } 26238705bb5SFam Zheng queue_started = 26338705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 26438705bb5SFam Zheng 26538705bb5SFam Zheng if (queue_started) { 26638705bb5SFam Zheng qemu_flush_queued_packets(ncs); 26738705bb5SFam Zheng } 2686e790746SPaolo Bonzini 2696e790746SPaolo Bonzini if (!q->tx_waiting) { 2706e790746SPaolo Bonzini continue; 2716e790746SPaolo Bonzini } 2726e790746SPaolo Bonzini 27338705bb5SFam Zheng if (queue_started) { 2746e790746SPaolo Bonzini if (q->tx_timer) { 275bc72ad67SAlex Bligh timer_mod(q->tx_timer, 276bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 2776e790746SPaolo Bonzini } else { 2786e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 2796e790746SPaolo Bonzini } 2806e790746SPaolo Bonzini } else { 2816e790746SPaolo Bonzini if (q->tx_timer) { 282bc72ad67SAlex Bligh timer_del(q->tx_timer); 2836e790746SPaolo Bonzini } else { 2846e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 2856e790746SPaolo Bonzini } 286283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 287283e2c2aSYuri Benditovich (queue_status & VIRTIO_CONFIG_S_DRIVER_OK)) { 288283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 289283e2c2aSYuri Benditovich * and disabled notification */ 290283e2c2aSYuri Benditovich q->tx_waiting = 0; 291283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 292283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 293283e2c2aSYuri Benditovich } 2946e790746SPaolo Bonzini } 2956e790746SPaolo Bonzini } 2966e790746SPaolo Bonzini } 2976e790746SPaolo Bonzini 2986e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 2996e790746SPaolo Bonzini { 3006e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 30117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 3026e790746SPaolo Bonzini uint16_t old_status = n->status; 3036e790746SPaolo Bonzini 3046e790746SPaolo Bonzini if (nc->link_down) 3056e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 3066e790746SPaolo Bonzini else 3076e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 3086e790746SPaolo Bonzini 3096e790746SPaolo Bonzini if (n->status != old_status) 31017a0ca55SKONRAD Frederic virtio_notify_config(vdev); 3116e790746SPaolo Bonzini 31217a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 3136e790746SPaolo Bonzini } 3146e790746SPaolo Bonzini 315b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 316b1be4280SAmos Kong { 317b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 318b1be4280SAmos Kong 319b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 32096e35046SAmos Kong gchar *path = object_get_canonical_path(OBJECT(n->qdev)); 32106150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 32206150279SWenchao Xia n->netclient_name, path, &error_abort); 32396e35046SAmos Kong g_free(path); 324b1be4280SAmos Kong 325b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 326b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 327b1be4280SAmos Kong } 328b1be4280SAmos Kong } 329b1be4280SAmos Kong 330f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 331f7bc8ef8SAmos Kong { 332f7bc8ef8SAmos Kong intList *list, *entry; 333f7bc8ef8SAmos Kong int i, j; 334f7bc8ef8SAmos Kong 335f7bc8ef8SAmos Kong list = NULL; 336f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 337f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 338f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 339f7bc8ef8SAmos Kong entry = g_malloc0(sizeof(*entry)); 340f7bc8ef8SAmos Kong entry->value = (i << 5) + j; 341f7bc8ef8SAmos Kong entry->next = list; 342f7bc8ef8SAmos Kong list = entry; 343f7bc8ef8SAmos Kong } 344f7bc8ef8SAmos Kong } 345f7bc8ef8SAmos Kong } 346f7bc8ef8SAmos Kong 347f7bc8ef8SAmos Kong return list; 348f7bc8ef8SAmos Kong } 349f7bc8ef8SAmos Kong 350b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 351b1be4280SAmos Kong { 352b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 353f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 354b1be4280SAmos Kong RxFilterInfo *info; 355b1be4280SAmos Kong strList *str_list, *entry; 356f7bc8ef8SAmos Kong int i; 357b1be4280SAmos Kong 358b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 359b1be4280SAmos Kong info->name = g_strdup(nc->name); 360b1be4280SAmos Kong info->promiscuous = n->promisc; 361b1be4280SAmos Kong 362b1be4280SAmos Kong if (n->nouni) { 363b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 364b1be4280SAmos Kong } else if (n->alluni) { 365b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 366b1be4280SAmos Kong } else { 367b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 368b1be4280SAmos Kong } 369b1be4280SAmos Kong 370b1be4280SAmos Kong if (n->nomulti) { 371b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 372b1be4280SAmos Kong } else if (n->allmulti) { 373b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 374b1be4280SAmos Kong } else { 375b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 376b1be4280SAmos Kong } 377b1be4280SAmos Kong 378b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 379b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 380b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 381b1be4280SAmos Kong 382b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 383b1be4280SAmos Kong 384b1be4280SAmos Kong str_list = NULL; 385b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 386b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 387b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 388b1be4280SAmos Kong entry->next = str_list; 389b1be4280SAmos Kong str_list = entry; 390b1be4280SAmos Kong } 391b1be4280SAmos Kong info->unicast_table = str_list; 392b1be4280SAmos Kong 393b1be4280SAmos Kong str_list = NULL; 394b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 395b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 396b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 397b1be4280SAmos Kong entry->next = str_list; 398b1be4280SAmos Kong str_list = entry; 399b1be4280SAmos Kong } 400b1be4280SAmos Kong info->multicast_table = str_list; 401f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 402b1be4280SAmos Kong 40395129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 404f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 405f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 406f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 407f7bc8ef8SAmos Kong } else { 408f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 409b1be4280SAmos Kong } 410b1be4280SAmos Kong 411b1be4280SAmos Kong /* enable event notification after query */ 412b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 413b1be4280SAmos Kong 414b1be4280SAmos Kong return info; 415b1be4280SAmos Kong } 416b1be4280SAmos Kong 4176e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 4186e790746SPaolo Bonzini { 41917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 4206e790746SPaolo Bonzini 4216e790746SPaolo Bonzini /* Reset back to compatibility mode */ 4226e790746SPaolo Bonzini n->promisc = 1; 4236e790746SPaolo Bonzini n->allmulti = 0; 4246e790746SPaolo Bonzini n->alluni = 0; 4256e790746SPaolo Bonzini n->nomulti = 0; 4266e790746SPaolo Bonzini n->nouni = 0; 4276e790746SPaolo Bonzini n->nobcast = 0; 4286e790746SPaolo Bonzini /* multiqueue is disabled by default */ 4296e790746SPaolo Bonzini n->curr_queues = 1; 430f57fcf70SJason Wang timer_del(n->announce_timer); 431f57fcf70SJason Wang n->announce_counter = 0; 432f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 4336e790746SPaolo Bonzini 4346e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 4356e790746SPaolo Bonzini n->mac_table.in_use = 0; 4366e790746SPaolo Bonzini n->mac_table.first_multi = 0; 4376e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 4386e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 4396e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 4406e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 441702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 4426e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 4436e790746SPaolo Bonzini } 4446e790746SPaolo Bonzini 4456e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 4466e790746SPaolo Bonzini { 4476e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 4486e790746SPaolo Bonzini if (!nc->peer) { 4496e790746SPaolo Bonzini return; 4506e790746SPaolo Bonzini } 4516e790746SPaolo Bonzini 452d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 4536e790746SPaolo Bonzini } 4546e790746SPaolo Bonzini 4556e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 4566e790746SPaolo Bonzini { 4576e790746SPaolo Bonzini return n->has_vnet_hdr; 4586e790746SPaolo Bonzini } 4596e790746SPaolo Bonzini 4606e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 4616e790746SPaolo Bonzini { 4626e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 4636e790746SPaolo Bonzini return 0; 4646e790746SPaolo Bonzini 465d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 4666e790746SPaolo Bonzini 4676e790746SPaolo Bonzini return n->has_ufo; 4686e790746SPaolo Bonzini } 4696e790746SPaolo Bonzini 470bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 471bb9d17f8SCornelia Huck int version_1) 4726e790746SPaolo Bonzini { 4736e790746SPaolo Bonzini int i; 4746e790746SPaolo Bonzini NetClientState *nc; 4756e790746SPaolo Bonzini 4766e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 4776e790746SPaolo Bonzini 478bb9d17f8SCornelia Huck if (version_1) { 479bb9d17f8SCornelia Huck n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 480bb9d17f8SCornelia Huck } else { 4816e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 482bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 483bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 484bb9d17f8SCornelia Huck } 4856e790746SPaolo Bonzini 4866e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 4876e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 4886e790746SPaolo Bonzini 4896e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 490d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 491d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 4926e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 4936e790746SPaolo Bonzini } 4946e790746SPaolo Bonzini } 4956e790746SPaolo Bonzini } 4966e790746SPaolo Bonzini 4976e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 4986e790746SPaolo Bonzini { 4996e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 5006e790746SPaolo Bonzini 5016e790746SPaolo Bonzini if (!nc->peer) { 5026e790746SPaolo Bonzini return 0; 5036e790746SPaolo Bonzini } 5046e790746SPaolo Bonzini 505f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 5067263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 5077263a0adSChangchun Ouyang } 5087263a0adSChangchun Ouyang 509f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 5106e790746SPaolo Bonzini return 0; 5116e790746SPaolo Bonzini } 5126e790746SPaolo Bonzini 5131074b879SJason Wang if (n->max_queues == 1) { 5141074b879SJason Wang return 0; 5151074b879SJason Wang } 5161074b879SJason Wang 5176e790746SPaolo Bonzini return tap_enable(nc->peer); 5186e790746SPaolo Bonzini } 5196e790746SPaolo Bonzini 5206e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 5216e790746SPaolo Bonzini { 5226e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 5236e790746SPaolo Bonzini 5246e790746SPaolo Bonzini if (!nc->peer) { 5256e790746SPaolo Bonzini return 0; 5266e790746SPaolo Bonzini } 5276e790746SPaolo Bonzini 528f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 5297263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 5307263a0adSChangchun Ouyang } 5317263a0adSChangchun Ouyang 532f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 5336e790746SPaolo Bonzini return 0; 5346e790746SPaolo Bonzini } 5356e790746SPaolo Bonzini 5366e790746SPaolo Bonzini return tap_disable(nc->peer); 5376e790746SPaolo Bonzini } 5386e790746SPaolo Bonzini 5396e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n) 5406e790746SPaolo Bonzini { 5416e790746SPaolo Bonzini int i; 542ddfa83eaSJoel Stanley int r; 5436e790746SPaolo Bonzini 54468b5f314SYuri Benditovich if (n->nic->peer_deleted) { 54568b5f314SYuri Benditovich return; 54668b5f314SYuri Benditovich } 54768b5f314SYuri Benditovich 5486e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 5496e790746SPaolo Bonzini if (i < n->curr_queues) { 550ddfa83eaSJoel Stanley r = peer_attach(n, i); 551ddfa83eaSJoel Stanley assert(!r); 5526e790746SPaolo Bonzini } else { 553ddfa83eaSJoel Stanley r = peer_detach(n, i); 554ddfa83eaSJoel Stanley assert(!r); 5556e790746SPaolo Bonzini } 5566e790746SPaolo Bonzini } 5576e790746SPaolo Bonzini } 5586e790746SPaolo Bonzini 559ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 5606e790746SPaolo Bonzini 5619d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 5629d5b731dSJason Wang Error **errp) 5636e790746SPaolo Bonzini { 56417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 5656e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5666e790746SPaolo Bonzini 567da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 568da3e8a23SShannon Zhao features |= n->host_features; 569da3e8a23SShannon Zhao 5700cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 5716e790746SPaolo Bonzini 5726e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 5730cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 5740cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 5750cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 5760cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 5776e790746SPaolo Bonzini 5780cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 5790cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 5800cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 5810cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 5826e790746SPaolo Bonzini } 5836e790746SPaolo Bonzini 5846e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 5850cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 5860cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 5876e790746SPaolo Bonzini } 5886e790746SPaolo Bonzini 589ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 5906e790746SPaolo Bonzini return features; 5916e790746SPaolo Bonzini } 592ed8b4afeSNikolay Nikolaev return vhost_net_get_features(get_vhost_net(nc->peer), features); 5936e790746SPaolo Bonzini } 5946e790746SPaolo Bonzini 595019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 5966e790746SPaolo Bonzini { 597019a3edbSGerd Hoffmann uint64_t features = 0; 5986e790746SPaolo Bonzini 5996e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 6006e790746SPaolo Bonzini * but also these: */ 6010cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 6020cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 6030cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 6040cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 6050cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 6066e790746SPaolo Bonzini 6076e790746SPaolo Bonzini return features; 6086e790746SPaolo Bonzini } 6096e790746SPaolo Bonzini 610644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 611644c9858SDmitry Fleytman { 612ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 613644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 614644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 615644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 616644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 617644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 618644c9858SDmitry Fleytman } 619644c9858SDmitry Fleytman 620644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 621644c9858SDmitry Fleytman { 622644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 623644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 624644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 625644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 626644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 627644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 628644c9858SDmitry Fleytman 629644c9858SDmitry Fleytman return guest_offloads_mask & features; 630644c9858SDmitry Fleytman } 631644c9858SDmitry Fleytman 632644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 633644c9858SDmitry Fleytman { 634644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 635644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 636644c9858SDmitry Fleytman } 637644c9858SDmitry Fleytman 638d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 6396e790746SPaolo Bonzini { 64017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 6416e790746SPaolo Bonzini int i; 6426e790746SPaolo Bonzini 643ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 64495129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 6456e790746SPaolo Bonzini 646ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 64795129d6fSCornelia Huck virtio_has_feature(features, 648bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 64995129d6fSCornelia Huck virtio_has_feature(features, 650bb9d17f8SCornelia Huck VIRTIO_F_VERSION_1)); 6516e790746SPaolo Bonzini 6526e790746SPaolo Bonzini if (n->has_vnet_hdr) { 653644c9858SDmitry Fleytman n->curr_guest_offloads = 654644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 655644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 6566e790746SPaolo Bonzini } 6576e790746SPaolo Bonzini 6586e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 6596e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 6606e790746SPaolo Bonzini 661ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 6626e790746SPaolo Bonzini continue; 6636e790746SPaolo Bonzini } 664ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 6656e790746SPaolo Bonzini } 6660b1eaa88SStefan Fritsch 66795129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 6680b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 6690b1eaa88SStefan Fritsch } else { 6700b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 6710b1eaa88SStefan Fritsch } 6726e790746SPaolo Bonzini } 6736e790746SPaolo Bonzini 6746e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 6756e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 6766e790746SPaolo Bonzini { 6776e790746SPaolo Bonzini uint8_t on; 6786e790746SPaolo Bonzini size_t s; 679b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 6806e790746SPaolo Bonzini 6816e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 6826e790746SPaolo Bonzini if (s != sizeof(on)) { 6836e790746SPaolo Bonzini return VIRTIO_NET_ERR; 6846e790746SPaolo Bonzini } 6856e790746SPaolo Bonzini 6866e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 6876e790746SPaolo Bonzini n->promisc = on; 6886e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 6896e790746SPaolo Bonzini n->allmulti = on; 6906e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 6916e790746SPaolo Bonzini n->alluni = on; 6926e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 6936e790746SPaolo Bonzini n->nomulti = on; 6946e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 6956e790746SPaolo Bonzini n->nouni = on; 6966e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 6976e790746SPaolo Bonzini n->nobcast = on; 6986e790746SPaolo Bonzini } else { 6996e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7006e790746SPaolo Bonzini } 7016e790746SPaolo Bonzini 702b1be4280SAmos Kong rxfilter_notify(nc); 703b1be4280SAmos Kong 7046e790746SPaolo Bonzini return VIRTIO_NET_OK; 7056e790746SPaolo Bonzini } 7066e790746SPaolo Bonzini 707644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 708644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 709644c9858SDmitry Fleytman { 710644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 711644c9858SDmitry Fleytman uint64_t offloads; 712644c9858SDmitry Fleytman size_t s; 713644c9858SDmitry Fleytman 71495129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 715644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 716644c9858SDmitry Fleytman } 717644c9858SDmitry Fleytman 718644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 719644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 720644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 721644c9858SDmitry Fleytman } 722644c9858SDmitry Fleytman 723644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 724644c9858SDmitry Fleytman uint64_t supported_offloads; 725644c9858SDmitry Fleytman 726644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 727644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 728644c9858SDmitry Fleytman } 729644c9858SDmitry Fleytman 730644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 731644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 732644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 733644c9858SDmitry Fleytman } 734644c9858SDmitry Fleytman 735644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 736644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 737644c9858SDmitry Fleytman 738644c9858SDmitry Fleytman return VIRTIO_NET_OK; 739644c9858SDmitry Fleytman } else { 740644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 741644c9858SDmitry Fleytman } 742644c9858SDmitry Fleytman } 743644c9858SDmitry Fleytman 7446e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 7456e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 7466e790746SPaolo Bonzini { 7471399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 7486e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 7496e790746SPaolo Bonzini size_t s; 750b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 7516e790746SPaolo Bonzini 7526e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 7536e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 7546e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7556e790746SPaolo Bonzini } 7566e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 7576e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 7586e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 759b1be4280SAmos Kong rxfilter_notify(nc); 760b1be4280SAmos Kong 7616e790746SPaolo Bonzini return VIRTIO_NET_OK; 7626e790746SPaolo Bonzini } 7636e790746SPaolo Bonzini 7646e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 7656e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7666e790746SPaolo Bonzini } 7676e790746SPaolo Bonzini 768cae2e556SAmos Kong int in_use = 0; 769cae2e556SAmos Kong int first_multi = 0; 770cae2e556SAmos Kong uint8_t uni_overflow = 0; 771cae2e556SAmos Kong uint8_t multi_overflow = 0; 772cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 7736e790746SPaolo Bonzini 7746e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 7756e790746SPaolo Bonzini sizeof(mac_data.entries)); 7761399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 7776e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 778b1be4280SAmos Kong goto error; 7796e790746SPaolo Bonzini } 7806e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 7816e790746SPaolo Bonzini 7826e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 783b1be4280SAmos Kong goto error; 7846e790746SPaolo Bonzini } 7856e790746SPaolo Bonzini 7866e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 787cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 7886e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 7896e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 790b1be4280SAmos Kong goto error; 7916e790746SPaolo Bonzini } 792cae2e556SAmos Kong in_use += mac_data.entries; 7936e790746SPaolo Bonzini } else { 794cae2e556SAmos Kong uni_overflow = 1; 7956e790746SPaolo Bonzini } 7966e790746SPaolo Bonzini 7976e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 7986e790746SPaolo Bonzini 799cae2e556SAmos Kong first_multi = in_use; 8006e790746SPaolo Bonzini 8016e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 8026e790746SPaolo Bonzini sizeof(mac_data.entries)); 8031399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 8046e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 805b1be4280SAmos Kong goto error; 8066e790746SPaolo Bonzini } 8076e790746SPaolo Bonzini 8086e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 8096e790746SPaolo Bonzini 8106e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 811b1be4280SAmos Kong goto error; 8126e790746SPaolo Bonzini } 8136e790746SPaolo Bonzini 814edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 815cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 8166e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 8176e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 818b1be4280SAmos Kong goto error; 8196e790746SPaolo Bonzini } 820cae2e556SAmos Kong in_use += mac_data.entries; 8216e790746SPaolo Bonzini } else { 822cae2e556SAmos Kong multi_overflow = 1; 8236e790746SPaolo Bonzini } 8246e790746SPaolo Bonzini 825cae2e556SAmos Kong n->mac_table.in_use = in_use; 826cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 827cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 828cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 829cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 830cae2e556SAmos Kong g_free(macs); 831b1be4280SAmos Kong rxfilter_notify(nc); 832b1be4280SAmos Kong 8336e790746SPaolo Bonzini return VIRTIO_NET_OK; 834b1be4280SAmos Kong 835b1be4280SAmos Kong error: 836cae2e556SAmos Kong g_free(macs); 837b1be4280SAmos Kong return VIRTIO_NET_ERR; 8386e790746SPaolo Bonzini } 8396e790746SPaolo Bonzini 8406e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 8416e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 8426e790746SPaolo Bonzini { 8431399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 8446e790746SPaolo Bonzini uint16_t vid; 8456e790746SPaolo Bonzini size_t s; 846b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 8476e790746SPaolo Bonzini 8486e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 8491399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 8506e790746SPaolo Bonzini if (s != sizeof(vid)) { 8516e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8526e790746SPaolo Bonzini } 8536e790746SPaolo Bonzini 8546e790746SPaolo Bonzini if (vid >= MAX_VLAN) 8556e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8566e790746SPaolo Bonzini 8576e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 8586e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 8596e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 8606e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 8616e790746SPaolo Bonzini else 8626e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8636e790746SPaolo Bonzini 864b1be4280SAmos Kong rxfilter_notify(nc); 865b1be4280SAmos Kong 8666e790746SPaolo Bonzini return VIRTIO_NET_OK; 8676e790746SPaolo Bonzini } 8686e790746SPaolo Bonzini 869f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 870f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 871f57fcf70SJason Wang { 872f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 873f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 874f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 875f57fcf70SJason Wang if (n->announce_counter) { 876f57fcf70SJason Wang timer_mod(n->announce_timer, 877f57fcf70SJason Wang qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 878f57fcf70SJason Wang self_announce_delay(n->announce_counter)); 879f57fcf70SJason Wang } 880f57fcf70SJason Wang return VIRTIO_NET_OK; 881f57fcf70SJason Wang } else { 882f57fcf70SJason Wang return VIRTIO_NET_ERR; 883f57fcf70SJason Wang } 884f57fcf70SJason Wang } 885f57fcf70SJason Wang 8866e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 8876e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 8886e790746SPaolo Bonzini { 88917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 8906e790746SPaolo Bonzini struct virtio_net_ctrl_mq mq; 8916e790746SPaolo Bonzini size_t s; 8926e790746SPaolo Bonzini uint16_t queues; 8936e790746SPaolo Bonzini 8946e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 8956e790746SPaolo Bonzini if (s != sizeof(mq)) { 8966e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8976e790746SPaolo Bonzini } 8986e790746SPaolo Bonzini 8996e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 9006e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9016e790746SPaolo Bonzini } 9026e790746SPaolo Bonzini 9031399c60dSRusty Russell queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 9046e790746SPaolo Bonzini 9056e790746SPaolo Bonzini if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 9066e790746SPaolo Bonzini queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 9076e790746SPaolo Bonzini queues > n->max_queues || 9086e790746SPaolo Bonzini !n->multiqueue) { 9096e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9106e790746SPaolo Bonzini } 9116e790746SPaolo Bonzini 9126e790746SPaolo Bonzini n->curr_queues = queues; 9136e790746SPaolo Bonzini /* stop the backend before changing the number of queues to avoid handling a 9146e790746SPaolo Bonzini * disabled queue */ 91517a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 9166e790746SPaolo Bonzini virtio_net_set_queues(n); 9176e790746SPaolo Bonzini 9186e790746SPaolo Bonzini return VIRTIO_NET_OK; 9196e790746SPaolo Bonzini } 920ba7eadb5SGreg Kurz 9216e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 9226e790746SPaolo Bonzini { 92317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9246e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 9256e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 92651b19ebeSPaolo Bonzini VirtQueueElement *elem; 9276e790746SPaolo Bonzini size_t s; 928771b6ed3SJason Wang struct iovec *iov, *iov2; 9296e790746SPaolo Bonzini unsigned int iov_cnt; 9306e790746SPaolo Bonzini 93151b19ebeSPaolo Bonzini for (;;) { 93251b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 93351b19ebeSPaolo Bonzini if (!elem) { 93451b19ebeSPaolo Bonzini break; 93551b19ebeSPaolo Bonzini } 93651b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 93751b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 938ba7eadb5SGreg Kurz virtio_error(vdev, "virtio-net ctrl missing headers"); 939ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 940ba7eadb5SGreg Kurz g_free(elem); 941ba7eadb5SGreg Kurz break; 9426e790746SPaolo Bonzini } 9436e790746SPaolo Bonzini 94451b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 94551b19ebeSPaolo Bonzini iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num); 9466e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 9476e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 9486e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 9496e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 9506e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 9516e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 9526e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 9536e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 9546e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 9556e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 956f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 957f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 9586e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 9596e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 960644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 961644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 9626e790746SPaolo Bonzini } 9636e790746SPaolo Bonzini 96451b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 9656e790746SPaolo Bonzini assert(s == sizeof(status)); 9666e790746SPaolo Bonzini 96751b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 9686e790746SPaolo Bonzini virtio_notify(vdev, vq); 969771b6ed3SJason Wang g_free(iov2); 97051b19ebeSPaolo Bonzini g_free(elem); 9716e790746SPaolo Bonzini } 9726e790746SPaolo Bonzini } 9736e790746SPaolo Bonzini 9746e790746SPaolo Bonzini /* RX */ 9756e790746SPaolo Bonzini 9766e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 9776e790746SPaolo Bonzini { 97817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9796e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 9806e790746SPaolo Bonzini 9816e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 9826e790746SPaolo Bonzini } 9836e790746SPaolo Bonzini 9846e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc) 9856e790746SPaolo Bonzini { 9866e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 98717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 9886e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 9896e790746SPaolo Bonzini 99017a0ca55SKONRAD Frederic if (!vdev->vm_running) { 9916e790746SPaolo Bonzini return 0; 9926e790746SPaolo Bonzini } 9936e790746SPaolo Bonzini 9946e790746SPaolo Bonzini if (nc->queue_index >= n->curr_queues) { 9956e790746SPaolo Bonzini return 0; 9966e790746SPaolo Bonzini } 9976e790746SPaolo Bonzini 9986e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 99917a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 10006e790746SPaolo Bonzini return 0; 10016e790746SPaolo Bonzini } 10026e790746SPaolo Bonzini 10036e790746SPaolo Bonzini return 1; 10046e790746SPaolo Bonzini } 10056e790746SPaolo Bonzini 10066e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 10076e790746SPaolo Bonzini { 10086e790746SPaolo Bonzini VirtIONet *n = q->n; 10096e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 10106e790746SPaolo Bonzini (n->mergeable_rx_bufs && 10116e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 10126e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 10136e790746SPaolo Bonzini 10146e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 10156e790746SPaolo Bonzini * available after the above check but before notification was 10166e790746SPaolo Bonzini * enabled, check for available buffers again. 10176e790746SPaolo Bonzini */ 10186e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 10196e790746SPaolo Bonzini (n->mergeable_rx_bufs && 10206e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 10216e790746SPaolo Bonzini return 0; 10226e790746SPaolo Bonzini } 10236e790746SPaolo Bonzini } 10246e790746SPaolo Bonzini 10256e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 10266e790746SPaolo Bonzini return 1; 10276e790746SPaolo Bonzini } 10286e790746SPaolo Bonzini 10291399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1030032a74a1SCédric Le Goater { 10311399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 10321399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 10331399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 10341399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1035032a74a1SCédric Le Goater } 1036032a74a1SCédric Le Goater 10376e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 10386e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 10396e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 10406e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 10416e790746SPaolo Bonzini * dhclient yet. 10426e790746SPaolo Bonzini * 10436e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 10446e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 10456e790746SPaolo Bonzini * kernels. 10466e790746SPaolo Bonzini * 10476e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 10486e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 10496e790746SPaolo Bonzini * cache. 10506e790746SPaolo Bonzini */ 10516e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 10526e790746SPaolo Bonzini uint8_t *buf, size_t size) 10536e790746SPaolo Bonzini { 10546e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 10556e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 10566e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 10576e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 10586e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 10596e790746SPaolo Bonzini net_checksum_calculate(buf, size); 10606e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 10616e790746SPaolo Bonzini } 10626e790746SPaolo Bonzini } 10636e790746SPaolo Bonzini 10646e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 10656e790746SPaolo Bonzini const void *buf, size_t size) 10666e790746SPaolo Bonzini { 10676e790746SPaolo Bonzini if (n->has_vnet_hdr) { 10686e790746SPaolo Bonzini /* FIXME this cast is evil */ 10696e790746SPaolo Bonzini void *wbuf = (void *)buf; 10706e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 10716e790746SPaolo Bonzini size - n->host_hdr_len); 10721bfa316cSGreg Kurz 10731bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 10741399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 10751bfa316cSGreg Kurz } 10766e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 10776e790746SPaolo Bonzini } else { 10786e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 10796e790746SPaolo Bonzini .flags = 0, 10806e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 10816e790746SPaolo Bonzini }; 10826e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 10836e790746SPaolo Bonzini } 10846e790746SPaolo Bonzini } 10856e790746SPaolo Bonzini 10866e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 10876e790746SPaolo Bonzini { 10886e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 10896e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 10906e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 10916e790746SPaolo Bonzini int i; 10926e790746SPaolo Bonzini 10936e790746SPaolo Bonzini if (n->promisc) 10946e790746SPaolo Bonzini return 1; 10956e790746SPaolo Bonzini 10966e790746SPaolo Bonzini ptr += n->host_hdr_len; 10976e790746SPaolo Bonzini 10986e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 10997542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 11006e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 11016e790746SPaolo Bonzini return 0; 11026e790746SPaolo Bonzini } 11036e790746SPaolo Bonzini 11046e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 11056e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 11066e790746SPaolo Bonzini return !n->nobcast; 11076e790746SPaolo Bonzini } else if (n->nomulti) { 11086e790746SPaolo Bonzini return 0; 11096e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 11106e790746SPaolo Bonzini return 1; 11116e790746SPaolo Bonzini } 11126e790746SPaolo Bonzini 11136e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 11146e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 11156e790746SPaolo Bonzini return 1; 11166e790746SPaolo Bonzini } 11176e790746SPaolo Bonzini } 11186e790746SPaolo Bonzini } else { // unicast 11196e790746SPaolo Bonzini if (n->nouni) { 11206e790746SPaolo Bonzini return 0; 11216e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 11226e790746SPaolo Bonzini return 1; 11236e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 11246e790746SPaolo Bonzini return 1; 11256e790746SPaolo Bonzini } 11266e790746SPaolo Bonzini 11276e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 11286e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 11296e790746SPaolo Bonzini return 1; 11306e790746SPaolo Bonzini } 11316e790746SPaolo Bonzini } 11326e790746SPaolo Bonzini } 11336e790746SPaolo Bonzini 11346e790746SPaolo Bonzini return 0; 11356e790746SPaolo Bonzini } 11366e790746SPaolo Bonzini 113797cd965cSPaolo Bonzini static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 113897cd965cSPaolo Bonzini size_t size) 11396e790746SPaolo Bonzini { 11406e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 11416e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 114217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 11436e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 11446e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 11456e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 11466e790746SPaolo Bonzini size_t offset, i, guest_offset; 11476e790746SPaolo Bonzini 11486e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 11496e790746SPaolo Bonzini return -1; 11506e790746SPaolo Bonzini } 11516e790746SPaolo Bonzini 11526e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 11536e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 11546e790746SPaolo Bonzini return 0; 11556e790746SPaolo Bonzini } 11566e790746SPaolo Bonzini 11576e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 11586e790746SPaolo Bonzini return size; 11596e790746SPaolo Bonzini 11606e790746SPaolo Bonzini offset = i = 0; 11616e790746SPaolo Bonzini 11626e790746SPaolo Bonzini while (offset < size) { 116351b19ebeSPaolo Bonzini VirtQueueElement *elem; 11646e790746SPaolo Bonzini int len, total; 116551b19ebeSPaolo Bonzini const struct iovec *sg; 11666e790746SPaolo Bonzini 11676e790746SPaolo Bonzini total = 0; 11686e790746SPaolo Bonzini 116951b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 117051b19ebeSPaolo Bonzini if (!elem) { 1171ba10b9c0SGreg Kurz if (i) { 1172ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 11736e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1174019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1175019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 11766e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1177019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1178019a3edbSGerd Hoffmann vdev->guest_features); 1179ba10b9c0SGreg Kurz } 1180ba10b9c0SGreg Kurz return -1; 11816e790746SPaolo Bonzini } 11826e790746SPaolo Bonzini 118351b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1184ba10b9c0SGreg Kurz virtio_error(vdev, 1185ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1186ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1187ba10b9c0SGreg Kurz g_free(elem); 1188ba10b9c0SGreg Kurz return -1; 11896e790746SPaolo Bonzini } 11906e790746SPaolo Bonzini 119151b19ebeSPaolo Bonzini sg = elem->in_sg; 11926e790746SPaolo Bonzini if (i == 0) { 11936e790746SPaolo Bonzini assert(offset == 0); 11946e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 11956e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 119651b19ebeSPaolo Bonzini sg, elem->in_num, 11976e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 11986e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 11996e790746SPaolo Bonzini } 12006e790746SPaolo Bonzini 120151b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 12026e790746SPaolo Bonzini offset = n->host_hdr_len; 12036e790746SPaolo Bonzini total += n->guest_hdr_len; 12046e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 12056e790746SPaolo Bonzini } else { 12066e790746SPaolo Bonzini guest_offset = 0; 12076e790746SPaolo Bonzini } 12086e790746SPaolo Bonzini 12096e790746SPaolo Bonzini /* copy in packet. ugh */ 121051b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 12116e790746SPaolo Bonzini buf + offset, size - offset); 12126e790746SPaolo Bonzini total += len; 12136e790746SPaolo Bonzini offset += len; 12146e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 12156e790746SPaolo Bonzini * must have consumed the complete packet. 12166e790746SPaolo Bonzini * Otherwise, drop it. */ 12176e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 121827e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 121951b19ebeSPaolo Bonzini g_free(elem); 12206e790746SPaolo Bonzini return size; 12216e790746SPaolo Bonzini } 12226e790746SPaolo Bonzini 12236e790746SPaolo Bonzini /* signal other side */ 122451b19ebeSPaolo Bonzini virtqueue_fill(q->rx_vq, elem, total, i++); 122551b19ebeSPaolo Bonzini g_free(elem); 12266e790746SPaolo Bonzini } 12276e790746SPaolo Bonzini 12286e790746SPaolo Bonzini if (mhdr_cnt) { 12291399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 12306e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 12316e790746SPaolo Bonzini 0, 12326e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 12336e790746SPaolo Bonzini } 12346e790746SPaolo Bonzini 12356e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 123617a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 12376e790746SPaolo Bonzini 12386e790746SPaolo Bonzini return size; 12396e790746SPaolo Bonzini } 12406e790746SPaolo Bonzini 124197cd965cSPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 124297cd965cSPaolo Bonzini size_t size) 124397cd965cSPaolo Bonzini { 124497cd965cSPaolo Bonzini ssize_t r; 124597cd965cSPaolo Bonzini 124697cd965cSPaolo Bonzini rcu_read_lock(); 124797cd965cSPaolo Bonzini r = virtio_net_receive_rcu(nc, buf, size); 124897cd965cSPaolo Bonzini rcu_read_unlock(); 124997cd965cSPaolo Bonzini return r; 125097cd965cSPaolo Bonzini } 125197cd965cSPaolo Bonzini 12526e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 12536e790746SPaolo Bonzini 12546e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 12556e790746SPaolo Bonzini { 12566e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 12576e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 125817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 12596e790746SPaolo Bonzini 126051b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 126117a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 12626e790746SPaolo Bonzini 126351b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 126451b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 12656e790746SPaolo Bonzini 12666e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 12676e790746SPaolo Bonzini virtio_net_flush_tx(q); 12686e790746SPaolo Bonzini } 12696e790746SPaolo Bonzini 12706e790746SPaolo Bonzini /* TX */ 12716e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 12726e790746SPaolo Bonzini { 12736e790746SPaolo Bonzini VirtIONet *n = q->n; 127417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 127551b19ebeSPaolo Bonzini VirtQueueElement *elem; 12766e790746SPaolo Bonzini int32_t num_packets = 0; 12776e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 127817a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 12796e790746SPaolo Bonzini return num_packets; 12806e790746SPaolo Bonzini } 12816e790746SPaolo Bonzini 128251b19ebeSPaolo Bonzini if (q->async_tx.elem) { 12836e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 12846e790746SPaolo Bonzini return num_packets; 12856e790746SPaolo Bonzini } 12866e790746SPaolo Bonzini 128751b19ebeSPaolo Bonzini for (;;) { 1288bd89dd98SJason Wang ssize_t ret; 128951b19ebeSPaolo Bonzini unsigned int out_num; 129051b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 1291feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 12926e790746SPaolo Bonzini 129351b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 129451b19ebeSPaolo Bonzini if (!elem) { 129551b19ebeSPaolo Bonzini break; 129651b19ebeSPaolo Bonzini } 129751b19ebeSPaolo Bonzini 129851b19ebeSPaolo Bonzini out_num = elem->out_num; 129951b19ebeSPaolo Bonzini out_sg = elem->out_sg; 13006e790746SPaolo Bonzini if (out_num < 1) { 1301fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 1302fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 1303fa5e56c2SGreg Kurz g_free(elem); 1304fa5e56c2SGreg Kurz return -EINVAL; 13056e790746SPaolo Bonzini } 13066e790746SPaolo Bonzini 1307032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 1308feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 1309feb93f36SJason Wang n->guest_hdr_len) { 1310fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 1311fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 1312fa5e56c2SGreg Kurz g_free(elem); 1313fa5e56c2SGreg Kurz return -EINVAL; 1314032a74a1SCédric Le Goater } 13151bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1316feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 1317feb93f36SJason Wang sg2[0].iov_base = &mhdr; 1318feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 1319feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 1320feb93f36SJason Wang out_sg, out_num, 1321feb93f36SJason Wang n->guest_hdr_len, -1); 1322feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 1323feb93f36SJason Wang goto drop; 1324032a74a1SCédric Le Goater } 1325feb93f36SJason Wang out_num += 1; 1326feb93f36SJason Wang out_sg = sg2; 1327feb93f36SJason Wang } 1328feb93f36SJason Wang } 13296e790746SPaolo Bonzini /* 13306e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 13316e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 13326e790746SPaolo Bonzini * that host is interested in. 13336e790746SPaolo Bonzini */ 13346e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 13356e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 13366e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 13376e790746SPaolo Bonzini out_sg, out_num, 13386e790746SPaolo Bonzini 0, n->host_hdr_len); 13396e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 13406e790746SPaolo Bonzini out_sg, out_num, 13416e790746SPaolo Bonzini n->guest_hdr_len, -1); 13426e790746SPaolo Bonzini out_num = sg_num; 13436e790746SPaolo Bonzini out_sg = sg; 13446e790746SPaolo Bonzini } 13456e790746SPaolo Bonzini 13466e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 13476e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 13486e790746SPaolo Bonzini if (ret == 0) { 13496e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 13506e790746SPaolo Bonzini q->async_tx.elem = elem; 13516e790746SPaolo Bonzini return -EBUSY; 13526e790746SPaolo Bonzini } 13536e790746SPaolo Bonzini 1354feb93f36SJason Wang drop: 135551b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 135617a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 135751b19ebeSPaolo Bonzini g_free(elem); 13586e790746SPaolo Bonzini 13596e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 13606e790746SPaolo Bonzini break; 13616e790746SPaolo Bonzini } 13626e790746SPaolo Bonzini } 13636e790746SPaolo Bonzini return num_packets; 13646e790746SPaolo Bonzini } 13656e790746SPaolo Bonzini 13666e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 13676e790746SPaolo Bonzini { 136817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 13696e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 13706e790746SPaolo Bonzini 1371283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 1372283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 1373283e2c2aSYuri Benditovich return; 1374283e2c2aSYuri Benditovich } 1375283e2c2aSYuri Benditovich 13766e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 137717a0ca55SKONRAD Frederic if (!vdev->vm_running) { 13786e790746SPaolo Bonzini q->tx_waiting = 1; 13796e790746SPaolo Bonzini return; 13806e790746SPaolo Bonzini } 13816e790746SPaolo Bonzini 13826e790746SPaolo Bonzini if (q->tx_waiting) { 13836e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 1384bc72ad67SAlex Bligh timer_del(q->tx_timer); 13856e790746SPaolo Bonzini q->tx_waiting = 0; 1386fa5e56c2SGreg Kurz if (virtio_net_flush_tx(q) == -EINVAL) { 1387fa5e56c2SGreg Kurz return; 1388fa5e56c2SGreg Kurz } 13896e790746SPaolo Bonzini } else { 1390bc72ad67SAlex Bligh timer_mod(q->tx_timer, 1391bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 13926e790746SPaolo Bonzini q->tx_waiting = 1; 13936e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 13946e790746SPaolo Bonzini } 13956e790746SPaolo Bonzini } 13966e790746SPaolo Bonzini 13976e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 13986e790746SPaolo Bonzini { 139917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14006e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 14016e790746SPaolo Bonzini 1402283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 1403283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 1404283e2c2aSYuri Benditovich return; 1405283e2c2aSYuri Benditovich } 1406283e2c2aSYuri Benditovich 14076e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 14086e790746SPaolo Bonzini return; 14096e790746SPaolo Bonzini } 14106e790746SPaolo Bonzini q->tx_waiting = 1; 14116e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 141217a0ca55SKONRAD Frederic if (!vdev->vm_running) { 14136e790746SPaolo Bonzini return; 14146e790746SPaolo Bonzini } 14156e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 14166e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 14176e790746SPaolo Bonzini } 14186e790746SPaolo Bonzini 14196e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 14206e790746SPaolo Bonzini { 14216e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 14226e790746SPaolo Bonzini VirtIONet *n = q->n; 142317a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1424e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1425e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1426e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1427e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1428e8bcf842SMichael S. Tsirkin return; 1429e8bcf842SMichael S. Tsirkin } 14306e790746SPaolo Bonzini 14316e790746SPaolo Bonzini q->tx_waiting = 0; 14326e790746SPaolo Bonzini 14336e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 143417a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 14356e790746SPaolo Bonzini return; 143617a0ca55SKONRAD Frederic } 14376e790746SPaolo Bonzini 14386e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 14396e790746SPaolo Bonzini virtio_net_flush_tx(q); 14406e790746SPaolo Bonzini } 14416e790746SPaolo Bonzini 14426e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 14436e790746SPaolo Bonzini { 14446e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 14456e790746SPaolo Bonzini VirtIONet *n = q->n; 144617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 14476e790746SPaolo Bonzini int32_t ret; 14486e790746SPaolo Bonzini 1449e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1450e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1451e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1452e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1453e8bcf842SMichael S. Tsirkin return; 1454e8bcf842SMichael S. Tsirkin } 14556e790746SPaolo Bonzini 14566e790746SPaolo Bonzini q->tx_waiting = 0; 14576e790746SPaolo Bonzini 14586e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 145917a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 14606e790746SPaolo Bonzini return; 146117a0ca55SKONRAD Frederic } 14626e790746SPaolo Bonzini 14636e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 1464fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 1465fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 1466fa5e56c2SGreg Kurz * broken */ 14676e790746SPaolo Bonzini } 14686e790746SPaolo Bonzini 14696e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 14706e790746SPaolo Bonzini * more coming and immediately reschedule */ 14716e790746SPaolo Bonzini if (ret >= n->tx_burst) { 14726e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 14736e790746SPaolo Bonzini q->tx_waiting = 1; 14746e790746SPaolo Bonzini return; 14756e790746SPaolo Bonzini } 14766e790746SPaolo Bonzini 14776e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 14786e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 14796e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 14806e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 1481fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 1482fa5e56c2SGreg Kurz if (ret == -EINVAL) { 1483fa5e56c2SGreg Kurz return; 1484fa5e56c2SGreg Kurz } else if (ret > 0) { 14856e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 14866e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 14876e790746SPaolo Bonzini q->tx_waiting = 1; 14886e790746SPaolo Bonzini } 14896e790746SPaolo Bonzini } 14906e790746SPaolo Bonzini 1491f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 1492f9d6dbf0SWen Congyang { 1493f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1494f9d6dbf0SWen Congyang 14951c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 14961c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 1497f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 1498f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 1499f9d6dbf0SWen Congyang virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer); 1500f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 1501f9d6dbf0SWen Congyang virtio_net_tx_timer, 1502f9d6dbf0SWen Congyang &n->vqs[index]); 1503f9d6dbf0SWen Congyang } else { 1504f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 1505f9d6dbf0SWen Congyang virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh); 1506f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 1507f9d6dbf0SWen Congyang } 1508f9d6dbf0SWen Congyang 1509f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 1510f9d6dbf0SWen Congyang n->vqs[index].n = n; 1511f9d6dbf0SWen Congyang } 1512f9d6dbf0SWen Congyang 1513f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 1514f9d6dbf0SWen Congyang { 1515f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1516f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 1517f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 1518f9d6dbf0SWen Congyang 1519f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 1520f9d6dbf0SWen Congyang 1521f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 1522f9d6dbf0SWen Congyang if (q->tx_timer) { 1523f9d6dbf0SWen Congyang timer_del(q->tx_timer); 1524f9d6dbf0SWen Congyang timer_free(q->tx_timer); 1525*f989c30cSYunjian Wang q->tx_timer = NULL; 1526f9d6dbf0SWen Congyang } else { 1527f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 1528*f989c30cSYunjian Wang q->tx_bh = NULL; 1529f9d6dbf0SWen Congyang } 1530*f989c30cSYunjian Wang q->tx_waiting = 0; 1531f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 1532f9d6dbf0SWen Congyang } 1533f9d6dbf0SWen Congyang 1534f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues) 1535f9d6dbf0SWen Congyang { 1536f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1537f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 1538f9d6dbf0SWen Congyang int new_num_queues = new_max_queues * 2 + 1; 1539f9d6dbf0SWen Congyang int i; 1540f9d6dbf0SWen Congyang 1541f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 1542f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 1543f9d6dbf0SWen Congyang 1544f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 1545f9d6dbf0SWen Congyang return; 1546f9d6dbf0SWen Congyang } 1547f9d6dbf0SWen Congyang 1548f9d6dbf0SWen Congyang /* 1549f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 1550f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 1551f9d6dbf0SWen Congyang * and then we only enter one of the following too loops. 1552f9d6dbf0SWen Congyang */ 1553f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 1554f9d6dbf0SWen Congyang 1555f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 1556f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 1557f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 1558f9d6dbf0SWen Congyang } 1559f9d6dbf0SWen Congyang 1560f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 1561f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 1562f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 1563f9d6dbf0SWen Congyang } 1564f9d6dbf0SWen Congyang 1565f9d6dbf0SWen Congyang /* add ctrl_vq last */ 1566f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 1567f9d6dbf0SWen Congyang } 1568f9d6dbf0SWen Congyang 1569ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 15706e790746SPaolo Bonzini { 1571f9d6dbf0SWen Congyang int max = multiqueue ? n->max_queues : 1; 1572f9d6dbf0SWen Congyang 15736e790746SPaolo Bonzini n->multiqueue = multiqueue; 1574f9d6dbf0SWen Congyang virtio_net_change_num_queues(n, max); 15756e790746SPaolo Bonzini 15766e790746SPaolo Bonzini virtio_net_set_queues(n); 15776e790746SPaolo Bonzini } 15786e790746SPaolo Bonzini 1579982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 1580037dab2fSGreg Kurz { 1581982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 1582982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 1583037dab2fSGreg Kurz int i, link_down; 1584037dab2fSGreg Kurz 1585982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 158695129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 158795129d6fSCornelia Huck VIRTIO_F_VERSION_1)); 15886e790746SPaolo Bonzini 15896e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 1590982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 15916e790746SPaolo Bonzini n->mac_table.in_use = 0; 15926e790746SPaolo Bonzini } 15936e790746SPaolo Bonzini 1594982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 15956c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 15966c666823SMichael S. Tsirkin } 15976c666823SMichael S. Tsirkin 15986c666823SMichael S. Tsirkin if (peer_has_vnet_hdr(n)) { 15996c666823SMichael S. Tsirkin virtio_net_apply_guest_offloads(n); 16006c666823SMichael S. Tsirkin } 16016c666823SMichael S. Tsirkin 16026e790746SPaolo Bonzini virtio_net_set_queues(n); 16036e790746SPaolo Bonzini 16046e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 16056e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 16066e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 16076e790746SPaolo Bonzini break; 16086e790746SPaolo Bonzini } 16096e790746SPaolo Bonzini } 16106e790746SPaolo Bonzini n->mac_table.first_multi = i; 16116e790746SPaolo Bonzini 16126e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 16136e790746SPaolo Bonzini * to link status bit in n->status */ 16146e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 16156e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 16166e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 16176e790746SPaolo Bonzini } 16186e790746SPaolo Bonzini 16196c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 16206c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 16216c666823SMichael S. Tsirkin n->announce_counter = SELF_ANNOUNCE_ROUNDS; 16226c666823SMichael S. Tsirkin timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)); 16236c666823SMichael S. Tsirkin } 16246c666823SMichael S. Tsirkin 16256e790746SPaolo Bonzini return 0; 16266e790746SPaolo Bonzini } 16276e790746SPaolo Bonzini 1628982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 1629982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 1630982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 1631982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1632982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 1633982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1634982b78c5SDr. David Alan Gilbert }, 1635982b78c5SDr. David Alan Gilbert }; 1636982b78c5SDr. David Alan Gilbert 1637982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id) 1638982b78c5SDr. David Alan Gilbert { 1639982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->max_queues > 1; 1640982b78c5SDr. David Alan Gilbert } 1641982b78c5SDr. David Alan Gilbert 1642982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 1643982b78c5SDr. David Alan Gilbert { 1644982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 1645982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 1646982b78c5SDr. David Alan Gilbert } 1647982b78c5SDr. David Alan Gilbert 1648982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 1649982b78c5SDr. David Alan Gilbert { 1650982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 1651982b78c5SDr. David Alan Gilbert } 1652982b78c5SDr. David Alan Gilbert 1653982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 1654982b78c5SDr. David Alan Gilbert { 1655982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 1656982b78c5SDr. David Alan Gilbert } 1657982b78c5SDr. David Alan Gilbert 1658982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 1659982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 1660982b78c5SDr. David Alan Gilbert */ 1661982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 1662982b78c5SDr. David Alan Gilbert VirtIONet *parent; 1663982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 1664982b78c5SDr. David Alan Gilbert uint16_t curr_queues_1; 1665982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 1666982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 1667982b78c5SDr. David Alan Gilbert }; 1668982b78c5SDr. David Alan Gilbert 1669982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 1670982b78c5SDr. David Alan Gilbert * the 1st entry in the queues and only if there's more than one 1671982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 1672982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 1673982b78c5SDr. David Alan Gilbert */ 1674982b78c5SDr. David Alan Gilbert 1675982b78c5SDr. David Alan Gilbert static void virtio_net_tx_waiting_pre_save(void *opaque) 1676982b78c5SDr. David Alan Gilbert { 1677982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1678982b78c5SDr. David Alan Gilbert 1679982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 1680982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = tmp->parent->curr_queues - 1; 1681982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues == 0) { 1682982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = 0; 1683982b78c5SDr. David Alan Gilbert } 1684982b78c5SDr. David Alan Gilbert } 1685982b78c5SDr. David Alan Gilbert 1686982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 1687982b78c5SDr. David Alan Gilbert { 1688982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1689982b78c5SDr. David Alan Gilbert 1690982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 1691982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 1692982b78c5SDr. David Alan Gilbert 1693982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues > tmp->parent->max_queues) { 1694982b78c5SDr. David Alan Gilbert error_report("virtio-net: curr_queues %x > max_queues %x", 1695982b78c5SDr. David Alan Gilbert tmp->parent->curr_queues, tmp->parent->max_queues); 1696982b78c5SDr. David Alan Gilbert 1697982b78c5SDr. David Alan Gilbert return -EINVAL; 1698982b78c5SDr. David Alan Gilbert } 1699982b78c5SDr. David Alan Gilbert 1700982b78c5SDr. David Alan Gilbert return 0; /* all good */ 1701982b78c5SDr. David Alan Gilbert } 1702982b78c5SDr. David Alan Gilbert 1703982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 1704982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 1705982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 1706982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 1707982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1708982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 1709982b78c5SDr. David Alan Gilbert curr_queues_1, 1710982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 1711982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 1712982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1713982b78c5SDr. David Alan Gilbert }, 1714982b78c5SDr. David Alan Gilbert }; 1715982b78c5SDr. David Alan Gilbert 1716982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 1717982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 1718982b78c5SDr. David Alan Gilbert */ 1719982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 1720982b78c5SDr. David Alan Gilbert { 1721982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1722982b78c5SDr. David Alan Gilbert 1723982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 1724982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 1725982b78c5SDr. David Alan Gilbert return -EINVAL; 1726982b78c5SDr. David Alan Gilbert } 1727982b78c5SDr. David Alan Gilbert 1728982b78c5SDr. David Alan Gilbert return 0; 1729982b78c5SDr. David Alan Gilbert } 1730982b78c5SDr. David Alan Gilbert 1731982b78c5SDr. David Alan Gilbert static void virtio_net_ufo_pre_save(void *opaque) 1732982b78c5SDr. David Alan Gilbert { 1733982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1734982b78c5SDr. David Alan Gilbert 1735982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 1736982b78c5SDr. David Alan Gilbert } 1737982b78c5SDr. David Alan Gilbert 1738982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 1739982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 1740982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 1741982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 1742982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1743982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 1744982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1745982b78c5SDr. David Alan Gilbert }, 1746982b78c5SDr. David Alan Gilbert }; 1747982b78c5SDr. David Alan Gilbert 1748982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 1749982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 1750982b78c5SDr. David Alan Gilbert */ 1751982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 1752982b78c5SDr. David Alan Gilbert { 1753982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1754982b78c5SDr. David Alan Gilbert 1755982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 1756982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 1757982b78c5SDr. David Alan Gilbert return -EINVAL; 1758982b78c5SDr. David Alan Gilbert } 1759982b78c5SDr. David Alan Gilbert 1760982b78c5SDr. David Alan Gilbert return 0; 1761982b78c5SDr. David Alan Gilbert } 1762982b78c5SDr. David Alan Gilbert 1763982b78c5SDr. David Alan Gilbert static void virtio_net_vnet_pre_save(void *opaque) 1764982b78c5SDr. David Alan Gilbert { 1765982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1766982b78c5SDr. David Alan Gilbert 1767982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 1768982b78c5SDr. David Alan Gilbert } 1769982b78c5SDr. David Alan Gilbert 1770982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 1771982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 1772982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 1773982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 1774982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1775982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 1776982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1777982b78c5SDr. David Alan Gilbert }, 1778982b78c5SDr. David Alan Gilbert }; 1779982b78c5SDr. David Alan Gilbert 1780982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 1781982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 1782982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 1783982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 1784982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 1785982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1786982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 1787982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 1788982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 1789982b78c5SDr. David Alan Gilbert VirtIONetQueue), 1790982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 1791982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 1792982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 1793982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 1794982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 1795982b78c5SDr. David Alan Gilbert 1796982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 1797982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 1798982b78c5SDr. David Alan Gilbert * sets flags in this case. 1799982b78c5SDr. David Alan Gilbert */ 1800982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 1801982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 1802982b78c5SDr. David Alan Gilbert ETH_ALEN), 1803982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 1804982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 1805982b78c5SDr. David Alan Gilbert 1806982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 1807982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 1808982b78c5SDr. David Alan Gilbert * but based on the uint. 1809982b78c5SDr. David Alan Gilbert */ 1810982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 1811982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1812982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 1813982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 1814982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 1815982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 1816982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 1817982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 1818982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 1819982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1820982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 1821982b78c5SDr. David Alan Gilbert VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0, 1822982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 1823982b78c5SDr. David Alan Gilbert VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1), 1824982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1825982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 1826982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 1827982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 1828982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1829982b78c5SDr. David Alan Gilbert }, 1830982b78c5SDr. David Alan Gilbert }; 1831982b78c5SDr. David Alan Gilbert 18326e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 1833f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 18346e790746SPaolo Bonzini .size = sizeof(NICState), 18356e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 18366e790746SPaolo Bonzini .receive = virtio_net_receive, 18376e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 1838b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 18396e790746SPaolo Bonzini }; 18406e790746SPaolo Bonzini 18416e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 18426e790746SPaolo Bonzini { 184317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 18446e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 18456e790746SPaolo Bonzini assert(n->vhost_started); 1846ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 18476e790746SPaolo Bonzini } 18486e790746SPaolo Bonzini 18496e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 18506e790746SPaolo Bonzini bool mask) 18516e790746SPaolo Bonzini { 185217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 18536e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 18546e790746SPaolo Bonzini assert(n->vhost_started); 1855ed8b4afeSNikolay Nikolaev vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 18566e790746SPaolo Bonzini vdev, idx, mask); 18576e790746SPaolo Bonzini } 18586e790746SPaolo Bonzini 1859019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 18606e790746SPaolo Bonzini { 18616e790746SPaolo Bonzini int i, config_size = 0; 18620cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 1863a93e599dSMaxime Coquelin 18646e790746SPaolo Bonzini for (i = 0; feature_sizes[i].flags != 0; i++) { 18656e790746SPaolo Bonzini if (host_features & feature_sizes[i].flags) { 18666e790746SPaolo Bonzini config_size = MAX(feature_sizes[i].end, config_size); 18676e790746SPaolo Bonzini } 18686e790746SPaolo Bonzini } 186917ec5a86SKONRAD Frederic n->config_size = config_size; 187017ec5a86SKONRAD Frederic } 18716e790746SPaolo Bonzini 18728a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 18738a253ec2SKONRAD Frederic const char *type) 18748a253ec2SKONRAD Frederic { 18758a253ec2SKONRAD Frederic /* 18768a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 18778a253ec2SKONRAD Frederic */ 18788a253ec2SKONRAD Frederic assert(type != NULL); 18798a253ec2SKONRAD Frederic 18808a253ec2SKONRAD Frederic g_free(n->netclient_name); 18818a253ec2SKONRAD Frederic g_free(n->netclient_type); 18828a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 18838a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 18848a253ec2SKONRAD Frederic } 18858a253ec2SKONRAD Frederic 1886e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 188717ec5a86SKONRAD Frederic { 1888e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1889284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 1890284a32f0SAndreas Färber NetClientState *nc; 18911773d9eeSKONRAD Frederic int i; 189217ec5a86SKONRAD Frederic 1893a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 1894a93e599dSMaxime Coquelin n->host_features |= (0x1 << VIRTIO_NET_F_MTU); 1895a93e599dSMaxime Coquelin } 1896a93e599dSMaxime Coquelin 1897da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 1898284a32f0SAndreas Färber virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); 189917ec5a86SKONRAD Frederic 19001c0fbfa3SMichael S. Tsirkin /* 19011c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 19021c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 19031c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 19041c0fbfa3SMichael S. Tsirkin */ 19051c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 19061c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 19071c0fbfa3SMichael S. Tsirkin (n->net_conf.rx_queue_size & (n->net_conf.rx_queue_size - 1))) { 19081c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 19091c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 19101c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 19111c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 19121c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 19131c0fbfa3SMichael S. Tsirkin return; 19141c0fbfa3SMichael S. Tsirkin } 19151c0fbfa3SMichael S. Tsirkin 1916575a1c0eSJiri Pirko n->max_queues = MAX(n->nic_conf.peers.queues, 1); 191787b3bd1cSJason Wang if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) { 19187e0e736eSJason Wang error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 1919631b22eaSStefan Weil "must be a positive integer less than %d.", 192087b3bd1cSJason Wang n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2); 19217e0e736eSJason Wang virtio_cleanup(vdev); 19227e0e736eSJason Wang return; 19237e0e736eSJason Wang } 19246e790746SPaolo Bonzini n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); 19256e790746SPaolo Bonzini n->curr_queues = 1; 19261773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 19276e790746SPaolo Bonzini 19281773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 19291773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 19306e790746SPaolo Bonzini error_report("virtio-net: " 19316e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 19321773d9eeSKONRAD Frederic n->net_conf.tx); 19336e790746SPaolo Bonzini error_report("Defaulting to \"bh\""); 19346e790746SPaolo Bonzini } 19356e790746SPaolo Bonzini 1936da51a335SJason Wang for (i = 0; i < n->max_queues; i++) { 1937f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 1938da51a335SJason Wang } 1939da51a335SJason Wang 194017a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 19411773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 19421773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 19436e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 1944f57fcf70SJason Wang n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, 1945f57fcf70SJason Wang virtio_net_announce_timer, n); 19466e790746SPaolo Bonzini 19478a253ec2SKONRAD Frederic if (n->netclient_type) { 19488a253ec2SKONRAD Frederic /* 19498a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 19508a253ec2SKONRAD Frederic */ 19518a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 19528a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 19538a253ec2SKONRAD Frederic } else { 19541773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 1955284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 19568a253ec2SKONRAD Frederic } 19578a253ec2SKONRAD Frederic 19586e790746SPaolo Bonzini peer_test_vnet_hdr(n); 19596e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 19606e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 1961d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 19626e790746SPaolo Bonzini } 19636e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 19646e790746SPaolo Bonzini } else { 19656e790746SPaolo Bonzini n->host_hdr_len = 0; 19666e790746SPaolo Bonzini } 19676e790746SPaolo Bonzini 19681773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 19696e790746SPaolo Bonzini 19706e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 19711773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 1972bb9d17f8SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 0, 0); 19736e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 19746e790746SPaolo Bonzini 19756e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 19766e790746SPaolo Bonzini 19776e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 19786e790746SPaolo Bonzini 1979b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 1980b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 1981b1be4280SAmos Kong 1982284a32f0SAndreas Färber n->qdev = dev; 198317ec5a86SKONRAD Frederic } 198417ec5a86SKONRAD Frederic 1985306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp) 198617ec5a86SKONRAD Frederic { 1987306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1988306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 1989f9d6dbf0SWen Congyang int i, max_queues; 199017ec5a86SKONRAD Frederic 199117ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 199217ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 199317ec5a86SKONRAD Frederic 19948a253ec2SKONRAD Frederic g_free(n->netclient_name); 19958a253ec2SKONRAD Frederic n->netclient_name = NULL; 19968a253ec2SKONRAD Frederic g_free(n->netclient_type); 19978a253ec2SKONRAD Frederic n->netclient_type = NULL; 19988a253ec2SKONRAD Frederic 199917ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 200017ec5a86SKONRAD Frederic g_free(n->vlans); 200117ec5a86SKONRAD Frederic 2002f9d6dbf0SWen Congyang max_queues = n->multiqueue ? n->max_queues : 1; 2003f9d6dbf0SWen Congyang for (i = 0; i < max_queues; i++) { 2004f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 200517ec5a86SKONRAD Frederic } 200617ec5a86SKONRAD Frederic 2007f57fcf70SJason Wang timer_del(n->announce_timer); 2008f57fcf70SJason Wang timer_free(n->announce_timer); 200917ec5a86SKONRAD Frederic g_free(n->vqs); 201017ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 20116a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 201217ec5a86SKONRAD Frederic } 201317ec5a86SKONRAD Frederic 201417ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 201517ec5a86SKONRAD Frederic { 201617ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 201717ec5a86SKONRAD Frederic 201817ec5a86SKONRAD Frederic /* 201917ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 202017ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 202117ec5a86SKONRAD Frederic */ 202217ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 2023aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 2024aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 2025aa4197c3SGonglei DEVICE(n), NULL); 202617ec5a86SKONRAD Frederic } 202717ec5a86SKONRAD Frederic 20284d45dcfbSHalil Pasic static void virtio_net_pre_save(void *opaque) 20294d45dcfbSHalil Pasic { 20304d45dcfbSHalil Pasic VirtIONet *n = opaque; 20314d45dcfbSHalil Pasic 20324d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 20334d45dcfbSHalil Pasic * it might keep writing to memory. */ 20344d45dcfbSHalil Pasic assert(!n->vhost_started); 20354d45dcfbSHalil Pasic } 20364d45dcfbSHalil Pasic 20374d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 20384d45dcfbSHalil Pasic .name = "virtio-net", 20394d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 20404d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 20414d45dcfbSHalil Pasic .fields = (VMStateField[]) { 20424d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 20434d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 20444d45dcfbSHalil Pasic }, 20454d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 20464d45dcfbSHalil Pasic }; 2047290c2428SDr. David Alan Gilbert 204817ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 204987108bb2SShannon Zhao DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true), 205087108bb2SShannon Zhao DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features, 205187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 205287108bb2SShannon Zhao DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 205387108bb2SShannon Zhao DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features, 205487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 205587108bb2SShannon Zhao DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features, 205687108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 205787108bb2SShannon Zhao DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features, 205887108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 205987108bb2SShannon Zhao DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features, 206087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 206187108bb2SShannon Zhao DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features, 206287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 206387108bb2SShannon Zhao DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features, 206487108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 206587108bb2SShannon Zhao DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features, 206687108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 206787108bb2SShannon Zhao DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features, 206887108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 206987108bb2SShannon Zhao DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features, 207087108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 207187108bb2SShannon Zhao DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features, 207287108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 207387108bb2SShannon Zhao DEFINE_PROP_BIT("status", VirtIONet, host_features, 207487108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 207587108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features, 207687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 207787108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features, 207887108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 207987108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features, 208087108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 208187108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features, 208287108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 208387108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features, 208487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 208587108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features, 208687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 208787108bb2SShannon Zhao DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 208817ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 208917ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 209017ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 209117ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 209217ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 20931c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 20941c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 2095a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 209617ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 209717ec5a86SKONRAD Frederic }; 209817ec5a86SKONRAD Frederic 209917ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 210017ec5a86SKONRAD Frederic { 210117ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 210217ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 2103e6f746b3SAndreas Färber 210417ec5a86SKONRAD Frederic dc->props = virtio_net_properties; 2105290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 2106125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 2107e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 2108306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 210917ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 211017ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 211117ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 211217ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 211317ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 211417ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 211517ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 211617ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 211717ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 21182a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 2119982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 212017ec5a86SKONRAD Frederic } 212117ec5a86SKONRAD Frederic 212217ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 212317ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 212417ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 212517ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 212617ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 212717ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 212817ec5a86SKONRAD Frederic }; 212917ec5a86SKONRAD Frederic 213017ec5a86SKONRAD Frederic static void virtio_register_types(void) 213117ec5a86SKONRAD Frederic { 213217ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 213317ec5a86SKONRAD Frederic } 213417ec5a86SKONRAD Frederic 213517ec5a86SKONRAD Frederic type_init(virtio_register_types) 2136