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" 25e688df6bSMarkus Armbruster #include "qapi/error.h" 269af23989SMarkus Armbruster #include "qapi/qapi-events-net.h" 271399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 28f8d806c9SJuan Quintela #include "migration/misc.h" 29*9473939eSJason Baron #include "standard-headers/linux/ethtool.h" 306e790746SPaolo Bonzini 316e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 326e790746SPaolo Bonzini 336e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 346e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 356e790746SPaolo Bonzini 361c0fbfa3SMichael S. Tsirkin /* previously fixed value */ 371c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256 389b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256 399b02e161SWei Wang 401c0fbfa3SMichael S. Tsirkin /* for now, only allow larger queues; with virtio-1, guest can downsize */ 411c0fbfa3SMichael S. Tsirkin #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 429b02e161SWei Wang #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 431c0fbfa3SMichael S. Tsirkin 446e790746SPaolo Bonzini /* 456e790746SPaolo Bonzini * Calculate the number of bytes up to and including the given 'field' of 466e790746SPaolo Bonzini * 'container'. 476e790746SPaolo Bonzini */ 486e790746SPaolo Bonzini #define endof(container, field) \ 496e790746SPaolo Bonzini (offsetof(container, field) + sizeof(((container *)0)->field)) 506e790746SPaolo Bonzini 516e790746SPaolo Bonzini typedef struct VirtIOFeature { 52127833eeSJason Baron uint64_t flags; 536e790746SPaolo Bonzini size_t end; 546e790746SPaolo Bonzini } VirtIOFeature; 556e790746SPaolo Bonzini 566e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = { 57127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MAC, 586e790746SPaolo Bonzini .end = endof(struct virtio_net_config, mac)}, 59127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_STATUS, 606e790746SPaolo Bonzini .end = endof(struct virtio_net_config, status)}, 61127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MQ, 626e790746SPaolo Bonzini .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 63127833eeSJason Baron {.flags = 1ULL << VIRTIO_NET_F_MTU, 64a93e599dSMaxime Coquelin .end = endof(struct virtio_net_config, mtu)}, 65*9473939eSJason Baron {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX, 66*9473939eSJason Baron .end = endof(struct virtio_net_config, duplex)}, 676e790746SPaolo Bonzini {} 686e790746SPaolo Bonzini }; 696e790746SPaolo Bonzini 706e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 716e790746SPaolo Bonzini { 726e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 736e790746SPaolo Bonzini 746e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 756e790746SPaolo Bonzini } 766e790746SPaolo Bonzini 776e790746SPaolo Bonzini static int vq2q(int queue_index) 786e790746SPaolo Bonzini { 796e790746SPaolo Bonzini return queue_index / 2; 806e790746SPaolo Bonzini } 816e790746SPaolo Bonzini 826e790746SPaolo Bonzini /* TODO 836e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 846e790746SPaolo Bonzini */ 856e790746SPaolo Bonzini 866e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 876e790746SPaolo Bonzini { 8817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 896e790746SPaolo Bonzini struct virtio_net_config netcfg; 906e790746SPaolo Bonzini 911399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 921399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues); 93a93e599dSMaxime Coquelin virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu); 946e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 95*9473939eSJason Baron virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed); 96*9473939eSJason Baron netcfg.duplex = n->net_conf.duplex; 976e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 986e790746SPaolo Bonzini } 996e790746SPaolo Bonzini 1006e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 1016e790746SPaolo Bonzini { 10217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1036e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 1046e790746SPaolo Bonzini 1056e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 1066e790746SPaolo Bonzini 10795129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 10895129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 1096e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 1106e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 1116e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 1126e790746SPaolo Bonzini } 1136e790746SPaolo Bonzini } 1146e790746SPaolo Bonzini 1156e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 1166e790746SPaolo Bonzini { 11717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1186e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 11917a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 1206e790746SPaolo Bonzini } 1216e790746SPaolo Bonzini 122f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 123f57fcf70SJason Wang { 124f57fcf70SJason Wang VirtIONet *n = opaque; 125f57fcf70SJason Wang VirtIODevice *vdev = VIRTIO_DEVICE(n); 126f57fcf70SJason Wang 127f57fcf70SJason Wang n->announce_counter--; 128f57fcf70SJason Wang n->status |= VIRTIO_NET_S_ANNOUNCE; 129f57fcf70SJason Wang virtio_notify_config(vdev); 130f57fcf70SJason Wang } 131f57fcf70SJason Wang 1326e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 1336e790746SPaolo Bonzini { 13417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1356e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 1366e790746SPaolo Bonzini int queues = n->multiqueue ? n->max_queues : 1; 1376e790746SPaolo Bonzini 138ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 1396e790746SPaolo Bonzini return; 1406e790746SPaolo Bonzini } 1416e790746SPaolo Bonzini 1428c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 1438c1ac475SRadim Krčmář !!n->vhost_started) { 1446e790746SPaolo Bonzini return; 1456e790746SPaolo Bonzini } 1466e790746SPaolo Bonzini if (!n->vhost_started) { 147086abc1cSMichael S. Tsirkin int r, i; 148086abc1cSMichael S. Tsirkin 1491bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1501bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 1511bfa316cSGreg Kurz "falling back on userspace virtio", 1521bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 1531bfa316cSGreg Kurz return; 1541bfa316cSGreg Kurz } 1551bfa316cSGreg Kurz 156086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 157086abc1cSMichael S. Tsirkin * when vhost is running. 158086abc1cSMichael S. Tsirkin */ 159086abc1cSMichael S. Tsirkin for (i = 0; i < queues; i++) { 160086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 161086abc1cSMichael S. Tsirkin 162086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 163086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 164086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 165086abc1cSMichael S. Tsirkin } 166086abc1cSMichael S. Tsirkin 167a93e599dSMaxime Coquelin if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) { 168a93e599dSMaxime Coquelin r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu); 169a93e599dSMaxime Coquelin if (r < 0) { 170a93e599dSMaxime Coquelin error_report("%uBytes MTU not supported by the backend", 171a93e599dSMaxime Coquelin n->net_conf.mtu); 172a93e599dSMaxime Coquelin 173a93e599dSMaxime Coquelin return; 174a93e599dSMaxime Coquelin } 175a93e599dSMaxime Coquelin } 176a93e599dSMaxime Coquelin 1776e790746SPaolo Bonzini n->vhost_started = 1; 17817a0ca55SKONRAD Frederic r = vhost_net_start(vdev, n->nic->ncs, queues); 1796e790746SPaolo Bonzini if (r < 0) { 1806e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 1816e790746SPaolo Bonzini "falling back on userspace virtio", -r); 1826e790746SPaolo Bonzini n->vhost_started = 0; 1836e790746SPaolo Bonzini } 1846e790746SPaolo Bonzini } else { 18517a0ca55SKONRAD Frederic vhost_net_stop(vdev, n->nic->ncs, queues); 1866e790746SPaolo Bonzini n->vhost_started = 0; 1876e790746SPaolo Bonzini } 1886e790746SPaolo Bonzini } 1896e790746SPaolo Bonzini 1901bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 1911bfa316cSGreg Kurz NetClientState *peer, 1921bfa316cSGreg Kurz bool enable) 1931bfa316cSGreg Kurz { 1941bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 1951bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 1961bfa316cSGreg Kurz } else { 1971bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 1981bfa316cSGreg Kurz } 1991bfa316cSGreg Kurz } 2001bfa316cSGreg Kurz 2011bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 2021bfa316cSGreg Kurz int queues, bool enable) 2031bfa316cSGreg Kurz { 2041bfa316cSGreg Kurz int i; 2051bfa316cSGreg Kurz 2061bfa316cSGreg Kurz for (i = 0; i < queues; i++) { 2071bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 2081bfa316cSGreg Kurz enable) { 2091bfa316cSGreg Kurz while (--i >= 0) { 2101bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 2111bfa316cSGreg Kurz } 2121bfa316cSGreg Kurz 2131bfa316cSGreg Kurz return true; 2141bfa316cSGreg Kurz } 2151bfa316cSGreg Kurz } 2161bfa316cSGreg Kurz 2171bfa316cSGreg Kurz return false; 2181bfa316cSGreg Kurz } 2191bfa316cSGreg Kurz 2201bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 2211bfa316cSGreg Kurz { 2221bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 2231bfa316cSGreg Kurz int queues = n->multiqueue ? n->max_queues : 1; 2241bfa316cSGreg Kurz 2251bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 2261bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 2271bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 2281bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 2291bfa316cSGreg Kurz * virtio-net code. 2301bfa316cSGreg Kurz */ 2311bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 2321bfa316cSGreg Kurz queues, true); 2331bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 2341bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 2351bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 2361bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 2371bfa316cSGreg Kurz * endianness. 2381bfa316cSGreg Kurz */ 2391bfa316cSGreg Kurz virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false); 2401bfa316cSGreg Kurz } 2411bfa316cSGreg Kurz } 2421bfa316cSGreg Kurz 243283e2c2aSYuri Benditovich static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq) 244283e2c2aSYuri Benditovich { 245283e2c2aSYuri Benditovich unsigned int dropped = virtqueue_drop_all(vq); 246283e2c2aSYuri Benditovich if (dropped) { 247283e2c2aSYuri Benditovich virtio_notify(vdev, vq); 248283e2c2aSYuri Benditovich } 249283e2c2aSYuri Benditovich } 250283e2c2aSYuri Benditovich 2516e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 2526e790746SPaolo Bonzini { 25317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 2546e790746SPaolo Bonzini VirtIONetQueue *q; 2556e790746SPaolo Bonzini int i; 2566e790746SPaolo Bonzini uint8_t queue_status; 2576e790746SPaolo Bonzini 2581bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 2596e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 2606e790746SPaolo Bonzini 2616e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 26238705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 26338705bb5SFam Zheng bool queue_started; 2646e790746SPaolo Bonzini q = &n->vqs[i]; 2656e790746SPaolo Bonzini 2666e790746SPaolo Bonzini if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { 2676e790746SPaolo Bonzini queue_status = 0; 2686e790746SPaolo Bonzini } else { 2696e790746SPaolo Bonzini queue_status = status; 2706e790746SPaolo Bonzini } 27138705bb5SFam Zheng queue_started = 27238705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 27338705bb5SFam Zheng 27438705bb5SFam Zheng if (queue_started) { 27538705bb5SFam Zheng qemu_flush_queued_packets(ncs); 27638705bb5SFam Zheng } 2776e790746SPaolo Bonzini 2786e790746SPaolo Bonzini if (!q->tx_waiting) { 2796e790746SPaolo Bonzini continue; 2806e790746SPaolo Bonzini } 2816e790746SPaolo Bonzini 28238705bb5SFam Zheng if (queue_started) { 2836e790746SPaolo Bonzini if (q->tx_timer) { 284bc72ad67SAlex Bligh timer_mod(q->tx_timer, 285bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 2866e790746SPaolo Bonzini } else { 2876e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 2886e790746SPaolo Bonzini } 2896e790746SPaolo Bonzini } else { 2906e790746SPaolo Bonzini if (q->tx_timer) { 291bc72ad67SAlex Bligh timer_del(q->tx_timer); 2926e790746SPaolo Bonzini } else { 2936e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 2946e790746SPaolo Bonzini } 295283e2c2aSYuri Benditovich if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 && 29670e53e6eSJason Wang (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) && 29770e53e6eSJason Wang vdev->vm_running) { 298283e2c2aSYuri Benditovich /* if tx is waiting we are likely have some packets in tx queue 299283e2c2aSYuri Benditovich * and disabled notification */ 300283e2c2aSYuri Benditovich q->tx_waiting = 0; 301283e2c2aSYuri Benditovich virtio_queue_set_notification(q->tx_vq, 1); 302283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, q->tx_vq); 303283e2c2aSYuri Benditovich } 3046e790746SPaolo Bonzini } 3056e790746SPaolo Bonzini } 3066e790746SPaolo Bonzini } 3076e790746SPaolo Bonzini 3086e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 3096e790746SPaolo Bonzini { 3106e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 31117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 3126e790746SPaolo Bonzini uint16_t old_status = n->status; 3136e790746SPaolo Bonzini 3146e790746SPaolo Bonzini if (nc->link_down) 3156e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 3166e790746SPaolo Bonzini else 3176e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 3186e790746SPaolo Bonzini 3196e790746SPaolo Bonzini if (n->status != old_status) 32017a0ca55SKONRAD Frederic virtio_notify_config(vdev); 3216e790746SPaolo Bonzini 32217a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 3236e790746SPaolo Bonzini } 3246e790746SPaolo Bonzini 325b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 326b1be4280SAmos Kong { 327b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 328b1be4280SAmos Kong 329b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 33096e35046SAmos Kong gchar *path = object_get_canonical_path(OBJECT(n->qdev)); 33106150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 33206150279SWenchao Xia n->netclient_name, path, &error_abort); 33396e35046SAmos Kong g_free(path); 334b1be4280SAmos Kong 335b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 336b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 337b1be4280SAmos Kong } 338b1be4280SAmos Kong } 339b1be4280SAmos Kong 340f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 341f7bc8ef8SAmos Kong { 342f7bc8ef8SAmos Kong intList *list, *entry; 343f7bc8ef8SAmos Kong int i, j; 344f7bc8ef8SAmos Kong 345f7bc8ef8SAmos Kong list = NULL; 346f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 347f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 348f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 349f7bc8ef8SAmos Kong entry = g_malloc0(sizeof(*entry)); 350f7bc8ef8SAmos Kong entry->value = (i << 5) + j; 351f7bc8ef8SAmos Kong entry->next = list; 352f7bc8ef8SAmos Kong list = entry; 353f7bc8ef8SAmos Kong } 354f7bc8ef8SAmos Kong } 355f7bc8ef8SAmos Kong } 356f7bc8ef8SAmos Kong 357f7bc8ef8SAmos Kong return list; 358f7bc8ef8SAmos Kong } 359f7bc8ef8SAmos Kong 360b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 361b1be4280SAmos Kong { 362b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 363f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 364b1be4280SAmos Kong RxFilterInfo *info; 365b1be4280SAmos Kong strList *str_list, *entry; 366f7bc8ef8SAmos Kong int i; 367b1be4280SAmos Kong 368b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 369b1be4280SAmos Kong info->name = g_strdup(nc->name); 370b1be4280SAmos Kong info->promiscuous = n->promisc; 371b1be4280SAmos Kong 372b1be4280SAmos Kong if (n->nouni) { 373b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 374b1be4280SAmos Kong } else if (n->alluni) { 375b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 376b1be4280SAmos Kong } else { 377b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 378b1be4280SAmos Kong } 379b1be4280SAmos Kong 380b1be4280SAmos Kong if (n->nomulti) { 381b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 382b1be4280SAmos Kong } else if (n->allmulti) { 383b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 384b1be4280SAmos Kong } else { 385b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 386b1be4280SAmos Kong } 387b1be4280SAmos Kong 388b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 389b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 390b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 391b1be4280SAmos Kong 392b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 393b1be4280SAmos Kong 394b1be4280SAmos Kong str_list = NULL; 395b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 396b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 397b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 398b1be4280SAmos Kong entry->next = str_list; 399b1be4280SAmos Kong str_list = entry; 400b1be4280SAmos Kong } 401b1be4280SAmos Kong info->unicast_table = str_list; 402b1be4280SAmos Kong 403b1be4280SAmos Kong str_list = NULL; 404b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 405b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 406b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 407b1be4280SAmos Kong entry->next = str_list; 408b1be4280SAmos Kong str_list = entry; 409b1be4280SAmos Kong } 410b1be4280SAmos Kong info->multicast_table = str_list; 411f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 412b1be4280SAmos Kong 41395129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 414f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 415f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 416f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 417f7bc8ef8SAmos Kong } else { 418f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 419b1be4280SAmos Kong } 420b1be4280SAmos Kong 421b1be4280SAmos Kong /* enable event notification after query */ 422b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 423b1be4280SAmos Kong 424b1be4280SAmos Kong return info; 425b1be4280SAmos Kong } 426b1be4280SAmos Kong 4276e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 4286e790746SPaolo Bonzini { 42917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 4306e790746SPaolo Bonzini 4316e790746SPaolo Bonzini /* Reset back to compatibility mode */ 4326e790746SPaolo Bonzini n->promisc = 1; 4336e790746SPaolo Bonzini n->allmulti = 0; 4346e790746SPaolo Bonzini n->alluni = 0; 4356e790746SPaolo Bonzini n->nomulti = 0; 4366e790746SPaolo Bonzini n->nouni = 0; 4376e790746SPaolo Bonzini n->nobcast = 0; 4386e790746SPaolo Bonzini /* multiqueue is disabled by default */ 4396e790746SPaolo Bonzini n->curr_queues = 1; 440f57fcf70SJason Wang timer_del(n->announce_timer); 441f57fcf70SJason Wang n->announce_counter = 0; 442f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 4436e790746SPaolo Bonzini 4446e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 4456e790746SPaolo Bonzini n->mac_table.in_use = 0; 4466e790746SPaolo Bonzini n->mac_table.first_multi = 0; 4476e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 4486e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 4496e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 4506e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 451702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 4526e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 4536e790746SPaolo Bonzini } 4546e790746SPaolo Bonzini 4556e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 4566e790746SPaolo Bonzini { 4576e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 4586e790746SPaolo Bonzini if (!nc->peer) { 4596e790746SPaolo Bonzini return; 4606e790746SPaolo Bonzini } 4616e790746SPaolo Bonzini 462d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 4636e790746SPaolo Bonzini } 4646e790746SPaolo Bonzini 4656e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 4666e790746SPaolo Bonzini { 4676e790746SPaolo Bonzini return n->has_vnet_hdr; 4686e790746SPaolo Bonzini } 4696e790746SPaolo Bonzini 4706e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 4716e790746SPaolo Bonzini { 4726e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 4736e790746SPaolo Bonzini return 0; 4746e790746SPaolo Bonzini 475d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 4766e790746SPaolo Bonzini 4776e790746SPaolo Bonzini return n->has_ufo; 4786e790746SPaolo Bonzini } 4796e790746SPaolo Bonzini 480bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 481bb9d17f8SCornelia Huck int version_1) 4826e790746SPaolo Bonzini { 4836e790746SPaolo Bonzini int i; 4846e790746SPaolo Bonzini NetClientState *nc; 4856e790746SPaolo Bonzini 4866e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 4876e790746SPaolo Bonzini 488bb9d17f8SCornelia Huck if (version_1) { 489bb9d17f8SCornelia Huck n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 490bb9d17f8SCornelia Huck } else { 4916e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 492bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 493bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 494bb9d17f8SCornelia Huck } 4956e790746SPaolo Bonzini 4966e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 4976e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 4986e790746SPaolo Bonzini 4996e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 500d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 501d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 5026e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 5036e790746SPaolo Bonzini } 5046e790746SPaolo Bonzini } 5056e790746SPaolo Bonzini } 5066e790746SPaolo Bonzini 5072eef278bSMichael S. Tsirkin static int virtio_net_max_tx_queue_size(VirtIONet *n) 5082eef278bSMichael S. Tsirkin { 5092eef278bSMichael S. Tsirkin NetClientState *peer = n->nic_conf.peers.ncs[0]; 5102eef278bSMichael S. Tsirkin 5112eef278bSMichael S. Tsirkin /* 5122eef278bSMichael S. Tsirkin * Backends other than vhost-user don't support max queue size. 5132eef278bSMichael S. Tsirkin */ 5142eef278bSMichael S. Tsirkin if (!peer) { 5152eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 5162eef278bSMichael S. Tsirkin } 5172eef278bSMichael S. Tsirkin 5182eef278bSMichael S. Tsirkin if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) { 5192eef278bSMichael S. Tsirkin return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; 5202eef278bSMichael S. Tsirkin } 5212eef278bSMichael S. Tsirkin 5222eef278bSMichael S. Tsirkin return VIRTQUEUE_MAX_SIZE; 5232eef278bSMichael S. Tsirkin } 5242eef278bSMichael S. Tsirkin 5256e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 5266e790746SPaolo Bonzini { 5276e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 5286e790746SPaolo Bonzini 5296e790746SPaolo Bonzini if (!nc->peer) { 5306e790746SPaolo Bonzini return 0; 5316e790746SPaolo Bonzini } 5326e790746SPaolo Bonzini 533f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 5347263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 5357263a0adSChangchun Ouyang } 5367263a0adSChangchun Ouyang 537f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 5386e790746SPaolo Bonzini return 0; 5396e790746SPaolo Bonzini } 5406e790746SPaolo Bonzini 5411074b879SJason Wang if (n->max_queues == 1) { 5421074b879SJason Wang return 0; 5431074b879SJason Wang } 5441074b879SJason Wang 5456e790746SPaolo Bonzini return tap_enable(nc->peer); 5466e790746SPaolo Bonzini } 5476e790746SPaolo Bonzini 5486e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 5496e790746SPaolo Bonzini { 5506e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 5516e790746SPaolo Bonzini 5526e790746SPaolo Bonzini if (!nc->peer) { 5536e790746SPaolo Bonzini return 0; 5546e790746SPaolo Bonzini } 5556e790746SPaolo Bonzini 556f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 5577263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 5587263a0adSChangchun Ouyang } 5597263a0adSChangchun Ouyang 560f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 5616e790746SPaolo Bonzini return 0; 5626e790746SPaolo Bonzini } 5636e790746SPaolo Bonzini 5646e790746SPaolo Bonzini return tap_disable(nc->peer); 5656e790746SPaolo Bonzini } 5666e790746SPaolo Bonzini 5676e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n) 5686e790746SPaolo Bonzini { 5696e790746SPaolo Bonzini int i; 570ddfa83eaSJoel Stanley int r; 5716e790746SPaolo Bonzini 57268b5f314SYuri Benditovich if (n->nic->peer_deleted) { 57368b5f314SYuri Benditovich return; 57468b5f314SYuri Benditovich } 57568b5f314SYuri Benditovich 5766e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 5776e790746SPaolo Bonzini if (i < n->curr_queues) { 578ddfa83eaSJoel Stanley r = peer_attach(n, i); 579ddfa83eaSJoel Stanley assert(!r); 5806e790746SPaolo Bonzini } else { 581ddfa83eaSJoel Stanley r = peer_detach(n, i); 582ddfa83eaSJoel Stanley assert(!r); 5836e790746SPaolo Bonzini } 5846e790746SPaolo Bonzini } 5856e790746SPaolo Bonzini } 5866e790746SPaolo Bonzini 587ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 5886e790746SPaolo Bonzini 5899d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 5909d5b731dSJason Wang Error **errp) 5916e790746SPaolo Bonzini { 59217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 5936e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5946e790746SPaolo Bonzini 595da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 596da3e8a23SShannon Zhao features |= n->host_features; 597da3e8a23SShannon Zhao 5980cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 5996e790746SPaolo Bonzini 6006e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 6010cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 6020cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 6030cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 6040cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 6056e790746SPaolo Bonzini 6060cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 6070cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 6080cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 6090cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 6106e790746SPaolo Bonzini } 6116e790746SPaolo Bonzini 6126e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 6130cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 6140cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 6156e790746SPaolo Bonzini } 6166e790746SPaolo Bonzini 617ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 6186e790746SPaolo Bonzini return features; 6196e790746SPaolo Bonzini } 62075ebec11SMaxime Coquelin features = vhost_net_get_features(get_vhost_net(nc->peer), features); 62175ebec11SMaxime Coquelin vdev->backend_features = features; 62275ebec11SMaxime Coquelin 62375ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 62475ebec11SMaxime Coquelin (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) { 62575ebec11SMaxime Coquelin features |= (1ULL << VIRTIO_NET_F_MTU); 62675ebec11SMaxime Coquelin } 62775ebec11SMaxime Coquelin 62875ebec11SMaxime Coquelin return features; 6296e790746SPaolo Bonzini } 6306e790746SPaolo Bonzini 631019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 6326e790746SPaolo Bonzini { 633019a3edbSGerd Hoffmann uint64_t features = 0; 6346e790746SPaolo Bonzini 6356e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 6366e790746SPaolo Bonzini * but also these: */ 6370cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 6380cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 6390cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 6400cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 6410cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 6426e790746SPaolo Bonzini 6436e790746SPaolo Bonzini return features; 6446e790746SPaolo Bonzini } 6456e790746SPaolo Bonzini 646644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 647644c9858SDmitry Fleytman { 648ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 649644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 650644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 651644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 652644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 653644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 654644c9858SDmitry Fleytman } 655644c9858SDmitry Fleytman 656644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 657644c9858SDmitry Fleytman { 658644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 659644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 660644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 661644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 662644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 663644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 664644c9858SDmitry Fleytman 665644c9858SDmitry Fleytman return guest_offloads_mask & features; 666644c9858SDmitry Fleytman } 667644c9858SDmitry Fleytman 668644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 669644c9858SDmitry Fleytman { 670644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 671644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 672644c9858SDmitry Fleytman } 673644c9858SDmitry Fleytman 674d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 6756e790746SPaolo Bonzini { 67617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 6776e790746SPaolo Bonzini int i; 6786e790746SPaolo Bonzini 67975ebec11SMaxime Coquelin if (n->mtu_bypass_backend && 68075ebec11SMaxime Coquelin !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) { 68175ebec11SMaxime Coquelin features &= ~(1ULL << VIRTIO_NET_F_MTU); 68275ebec11SMaxime Coquelin } 68375ebec11SMaxime Coquelin 684ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 68595129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 6866e790746SPaolo Bonzini 687ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 68895129d6fSCornelia Huck virtio_has_feature(features, 689bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 69095129d6fSCornelia Huck virtio_has_feature(features, 691bb9d17f8SCornelia Huck VIRTIO_F_VERSION_1)); 6926e790746SPaolo Bonzini 6936e790746SPaolo Bonzini if (n->has_vnet_hdr) { 694644c9858SDmitry Fleytman n->curr_guest_offloads = 695644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 696644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 6976e790746SPaolo Bonzini } 6986e790746SPaolo Bonzini 6996e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 7006e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 7016e790746SPaolo Bonzini 702ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 7036e790746SPaolo Bonzini continue; 7046e790746SPaolo Bonzini } 705ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 7066e790746SPaolo Bonzini } 7070b1eaa88SStefan Fritsch 70895129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 7090b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 7100b1eaa88SStefan Fritsch } else { 7110b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 7120b1eaa88SStefan Fritsch } 7136e790746SPaolo Bonzini } 7146e790746SPaolo Bonzini 7156e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 7166e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 7176e790746SPaolo Bonzini { 7186e790746SPaolo Bonzini uint8_t on; 7196e790746SPaolo Bonzini size_t s; 720b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 7216e790746SPaolo Bonzini 7226e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 7236e790746SPaolo Bonzini if (s != sizeof(on)) { 7246e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7256e790746SPaolo Bonzini } 7266e790746SPaolo Bonzini 7276e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 7286e790746SPaolo Bonzini n->promisc = on; 7296e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 7306e790746SPaolo Bonzini n->allmulti = on; 7316e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 7326e790746SPaolo Bonzini n->alluni = on; 7336e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 7346e790746SPaolo Bonzini n->nomulti = on; 7356e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 7366e790746SPaolo Bonzini n->nouni = on; 7376e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 7386e790746SPaolo Bonzini n->nobcast = on; 7396e790746SPaolo Bonzini } else { 7406e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7416e790746SPaolo Bonzini } 7426e790746SPaolo Bonzini 743b1be4280SAmos Kong rxfilter_notify(nc); 744b1be4280SAmos Kong 7456e790746SPaolo Bonzini return VIRTIO_NET_OK; 7466e790746SPaolo Bonzini } 7476e790746SPaolo Bonzini 748644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 749644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 750644c9858SDmitry Fleytman { 751644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 752644c9858SDmitry Fleytman uint64_t offloads; 753644c9858SDmitry Fleytman size_t s; 754644c9858SDmitry Fleytman 75595129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 756644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 757644c9858SDmitry Fleytman } 758644c9858SDmitry Fleytman 759644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 760644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 761644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 762644c9858SDmitry Fleytman } 763644c9858SDmitry Fleytman 764644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 765644c9858SDmitry Fleytman uint64_t supported_offloads; 766644c9858SDmitry Fleytman 767189ae6bbSJason Wang offloads = virtio_ldq_p(vdev, &offloads); 768189ae6bbSJason Wang 769644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 770644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 771644c9858SDmitry Fleytman } 772644c9858SDmitry Fleytman 773644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 774644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 775644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 776644c9858SDmitry Fleytman } 777644c9858SDmitry Fleytman 778644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 779644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 780644c9858SDmitry Fleytman 781644c9858SDmitry Fleytman return VIRTIO_NET_OK; 782644c9858SDmitry Fleytman } else { 783644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 784644c9858SDmitry Fleytman } 785644c9858SDmitry Fleytman } 786644c9858SDmitry Fleytman 7876e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 7886e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 7896e790746SPaolo Bonzini { 7901399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 7916e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 7926e790746SPaolo Bonzini size_t s; 793b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 7946e790746SPaolo Bonzini 7956e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 7966e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 7976e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7986e790746SPaolo Bonzini } 7996e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 8006e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 8016e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 802b1be4280SAmos Kong rxfilter_notify(nc); 803b1be4280SAmos Kong 8046e790746SPaolo Bonzini return VIRTIO_NET_OK; 8056e790746SPaolo Bonzini } 8066e790746SPaolo Bonzini 8076e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 8086e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8096e790746SPaolo Bonzini } 8106e790746SPaolo Bonzini 811cae2e556SAmos Kong int in_use = 0; 812cae2e556SAmos Kong int first_multi = 0; 813cae2e556SAmos Kong uint8_t uni_overflow = 0; 814cae2e556SAmos Kong uint8_t multi_overflow = 0; 815cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 8166e790746SPaolo Bonzini 8176e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 8186e790746SPaolo Bonzini sizeof(mac_data.entries)); 8191399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 8206e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 821b1be4280SAmos Kong goto error; 8226e790746SPaolo Bonzini } 8236e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 8246e790746SPaolo Bonzini 8256e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 826b1be4280SAmos Kong goto error; 8276e790746SPaolo Bonzini } 8286e790746SPaolo Bonzini 8296e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 830cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 8316e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 8326e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 833b1be4280SAmos Kong goto error; 8346e790746SPaolo Bonzini } 835cae2e556SAmos Kong in_use += mac_data.entries; 8366e790746SPaolo Bonzini } else { 837cae2e556SAmos Kong uni_overflow = 1; 8386e790746SPaolo Bonzini } 8396e790746SPaolo Bonzini 8406e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 8416e790746SPaolo Bonzini 842cae2e556SAmos Kong first_multi = in_use; 8436e790746SPaolo Bonzini 8446e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 8456e790746SPaolo Bonzini sizeof(mac_data.entries)); 8461399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 8476e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 848b1be4280SAmos Kong goto error; 8496e790746SPaolo Bonzini } 8506e790746SPaolo Bonzini 8516e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 8526e790746SPaolo Bonzini 8536e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 854b1be4280SAmos Kong goto error; 8556e790746SPaolo Bonzini } 8566e790746SPaolo Bonzini 857edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 858cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 8596e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 8606e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 861b1be4280SAmos Kong goto error; 8626e790746SPaolo Bonzini } 863cae2e556SAmos Kong in_use += mac_data.entries; 8646e790746SPaolo Bonzini } else { 865cae2e556SAmos Kong multi_overflow = 1; 8666e790746SPaolo Bonzini } 8676e790746SPaolo Bonzini 868cae2e556SAmos Kong n->mac_table.in_use = in_use; 869cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 870cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 871cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 872cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 873cae2e556SAmos Kong g_free(macs); 874b1be4280SAmos Kong rxfilter_notify(nc); 875b1be4280SAmos Kong 8766e790746SPaolo Bonzini return VIRTIO_NET_OK; 877b1be4280SAmos Kong 878b1be4280SAmos Kong error: 879cae2e556SAmos Kong g_free(macs); 880b1be4280SAmos Kong return VIRTIO_NET_ERR; 8816e790746SPaolo Bonzini } 8826e790746SPaolo Bonzini 8836e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 8846e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 8856e790746SPaolo Bonzini { 8861399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 8876e790746SPaolo Bonzini uint16_t vid; 8886e790746SPaolo Bonzini size_t s; 889b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 8906e790746SPaolo Bonzini 8916e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 8921399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 8936e790746SPaolo Bonzini if (s != sizeof(vid)) { 8946e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8956e790746SPaolo Bonzini } 8966e790746SPaolo Bonzini 8976e790746SPaolo Bonzini if (vid >= MAX_VLAN) 8986e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8996e790746SPaolo Bonzini 9006e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 9016e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 9026e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 9036e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 9046e790746SPaolo Bonzini else 9056e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9066e790746SPaolo Bonzini 907b1be4280SAmos Kong rxfilter_notify(nc); 908b1be4280SAmos Kong 9096e790746SPaolo Bonzini return VIRTIO_NET_OK; 9106e790746SPaolo Bonzini } 9116e790746SPaolo Bonzini 912f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 913f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 914f57fcf70SJason Wang { 915f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 916f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 917f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 918f57fcf70SJason Wang if (n->announce_counter) { 919f57fcf70SJason Wang timer_mod(n->announce_timer, 920f57fcf70SJason Wang qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 921f57fcf70SJason Wang self_announce_delay(n->announce_counter)); 922f57fcf70SJason Wang } 923f57fcf70SJason Wang return VIRTIO_NET_OK; 924f57fcf70SJason Wang } else { 925f57fcf70SJason Wang return VIRTIO_NET_ERR; 926f57fcf70SJason Wang } 927f57fcf70SJason Wang } 928f57fcf70SJason Wang 9296e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 9306e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 9316e790746SPaolo Bonzini { 93217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 9336e790746SPaolo Bonzini struct virtio_net_ctrl_mq mq; 9346e790746SPaolo Bonzini size_t s; 9356e790746SPaolo Bonzini uint16_t queues; 9366e790746SPaolo Bonzini 9376e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 9386e790746SPaolo Bonzini if (s != sizeof(mq)) { 9396e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9406e790746SPaolo Bonzini } 9416e790746SPaolo Bonzini 9426e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 9436e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9446e790746SPaolo Bonzini } 9456e790746SPaolo Bonzini 9461399c60dSRusty Russell queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 9476e790746SPaolo Bonzini 9486e790746SPaolo Bonzini if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 9496e790746SPaolo Bonzini queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 9506e790746SPaolo Bonzini queues > n->max_queues || 9516e790746SPaolo Bonzini !n->multiqueue) { 9526e790746SPaolo Bonzini return VIRTIO_NET_ERR; 9536e790746SPaolo Bonzini } 9546e790746SPaolo Bonzini 9556e790746SPaolo Bonzini n->curr_queues = queues; 9566e790746SPaolo Bonzini /* stop the backend before changing the number of queues to avoid handling a 9576e790746SPaolo Bonzini * disabled queue */ 95817a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 9596e790746SPaolo Bonzini virtio_net_set_queues(n); 9606e790746SPaolo Bonzini 9616e790746SPaolo Bonzini return VIRTIO_NET_OK; 9626e790746SPaolo Bonzini } 963ba7eadb5SGreg Kurz 9646e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 9656e790746SPaolo Bonzini { 96617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9676e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 9686e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 96951b19ebeSPaolo Bonzini VirtQueueElement *elem; 9706e790746SPaolo Bonzini size_t s; 971771b6ed3SJason Wang struct iovec *iov, *iov2; 9726e790746SPaolo Bonzini unsigned int iov_cnt; 9736e790746SPaolo Bonzini 97451b19ebeSPaolo Bonzini for (;;) { 97551b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 97651b19ebeSPaolo Bonzini if (!elem) { 97751b19ebeSPaolo Bonzini break; 97851b19ebeSPaolo Bonzini } 97951b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 98051b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 981ba7eadb5SGreg Kurz virtio_error(vdev, "virtio-net ctrl missing headers"); 982ba7eadb5SGreg Kurz virtqueue_detach_element(vq, elem, 0); 983ba7eadb5SGreg Kurz g_free(elem); 984ba7eadb5SGreg Kurz break; 9856e790746SPaolo Bonzini } 9866e790746SPaolo Bonzini 98751b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 98851b19ebeSPaolo Bonzini iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num); 9896e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 9906e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 9916e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 9926e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 9936e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 9946e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 9956e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 9966e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 9976e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 9986e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 999f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 1000f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 10016e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 10026e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 1003644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 1004644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 10056e790746SPaolo Bonzini } 10066e790746SPaolo Bonzini 100751b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 10086e790746SPaolo Bonzini assert(s == sizeof(status)); 10096e790746SPaolo Bonzini 101051b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 10116e790746SPaolo Bonzini virtio_notify(vdev, vq); 1012771b6ed3SJason Wang g_free(iov2); 101351b19ebeSPaolo Bonzini g_free(elem); 10146e790746SPaolo Bonzini } 10156e790746SPaolo Bonzini } 10166e790746SPaolo Bonzini 10176e790746SPaolo Bonzini /* RX */ 10186e790746SPaolo Bonzini 10196e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 10206e790746SPaolo Bonzini { 102117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 10226e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 10236e790746SPaolo Bonzini 10246e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 10256e790746SPaolo Bonzini } 10266e790746SPaolo Bonzini 10276e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc) 10286e790746SPaolo Bonzini { 10296e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 103017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 10316e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 10326e790746SPaolo Bonzini 103317a0ca55SKONRAD Frederic if (!vdev->vm_running) { 10346e790746SPaolo Bonzini return 0; 10356e790746SPaolo Bonzini } 10366e790746SPaolo Bonzini 10376e790746SPaolo Bonzini if (nc->queue_index >= n->curr_queues) { 10386e790746SPaolo Bonzini return 0; 10396e790746SPaolo Bonzini } 10406e790746SPaolo Bonzini 10416e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 104217a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 10436e790746SPaolo Bonzini return 0; 10446e790746SPaolo Bonzini } 10456e790746SPaolo Bonzini 10466e790746SPaolo Bonzini return 1; 10476e790746SPaolo Bonzini } 10486e790746SPaolo Bonzini 10496e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 10506e790746SPaolo Bonzini { 10516e790746SPaolo Bonzini VirtIONet *n = q->n; 10526e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 10536e790746SPaolo Bonzini (n->mergeable_rx_bufs && 10546e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 10556e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 10566e790746SPaolo Bonzini 10576e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 10586e790746SPaolo Bonzini * available after the above check but before notification was 10596e790746SPaolo Bonzini * enabled, check for available buffers again. 10606e790746SPaolo Bonzini */ 10616e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 10626e790746SPaolo Bonzini (n->mergeable_rx_bufs && 10636e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 10646e790746SPaolo Bonzini return 0; 10656e790746SPaolo Bonzini } 10666e790746SPaolo Bonzini } 10676e790746SPaolo Bonzini 10686e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 10696e790746SPaolo Bonzini return 1; 10706e790746SPaolo Bonzini } 10716e790746SPaolo Bonzini 10721399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 1073032a74a1SCédric Le Goater { 10741399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 10751399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 10761399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 10771399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 1078032a74a1SCédric Le Goater } 1079032a74a1SCédric Le Goater 10806e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 10816e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 10826e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 10836e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 10846e790746SPaolo Bonzini * dhclient yet. 10856e790746SPaolo Bonzini * 10866e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 10876e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 10886e790746SPaolo Bonzini * kernels. 10896e790746SPaolo Bonzini * 10906e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 10916e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 10926e790746SPaolo Bonzini * cache. 10936e790746SPaolo Bonzini */ 10946e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 10956e790746SPaolo Bonzini uint8_t *buf, size_t size) 10966e790746SPaolo Bonzini { 10976e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 10986e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 10996e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 11006e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 11016e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 11026e790746SPaolo Bonzini net_checksum_calculate(buf, size); 11036e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 11046e790746SPaolo Bonzini } 11056e790746SPaolo Bonzini } 11066e790746SPaolo Bonzini 11076e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 11086e790746SPaolo Bonzini const void *buf, size_t size) 11096e790746SPaolo Bonzini { 11106e790746SPaolo Bonzini if (n->has_vnet_hdr) { 11116e790746SPaolo Bonzini /* FIXME this cast is evil */ 11126e790746SPaolo Bonzini void *wbuf = (void *)buf; 11136e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 11146e790746SPaolo Bonzini size - n->host_hdr_len); 11151bfa316cSGreg Kurz 11161bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 11171399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 11181bfa316cSGreg Kurz } 11196e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 11206e790746SPaolo Bonzini } else { 11216e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 11226e790746SPaolo Bonzini .flags = 0, 11236e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 11246e790746SPaolo Bonzini }; 11256e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 11266e790746SPaolo Bonzini } 11276e790746SPaolo Bonzini } 11286e790746SPaolo Bonzini 11296e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 11306e790746SPaolo Bonzini { 11316e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 11326e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 11336e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 11346e790746SPaolo Bonzini int i; 11356e790746SPaolo Bonzini 11366e790746SPaolo Bonzini if (n->promisc) 11376e790746SPaolo Bonzini return 1; 11386e790746SPaolo Bonzini 11396e790746SPaolo Bonzini ptr += n->host_hdr_len; 11406e790746SPaolo Bonzini 11416e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 11427542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 11436e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 11446e790746SPaolo Bonzini return 0; 11456e790746SPaolo Bonzini } 11466e790746SPaolo Bonzini 11476e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 11486e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 11496e790746SPaolo Bonzini return !n->nobcast; 11506e790746SPaolo Bonzini } else if (n->nomulti) { 11516e790746SPaolo Bonzini return 0; 11526e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 11536e790746SPaolo Bonzini return 1; 11546e790746SPaolo Bonzini } 11556e790746SPaolo Bonzini 11566e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 11576e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 11586e790746SPaolo Bonzini return 1; 11596e790746SPaolo Bonzini } 11606e790746SPaolo Bonzini } 11616e790746SPaolo Bonzini } else { // unicast 11626e790746SPaolo Bonzini if (n->nouni) { 11636e790746SPaolo Bonzini return 0; 11646e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 11656e790746SPaolo Bonzini return 1; 11666e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 11676e790746SPaolo Bonzini return 1; 11686e790746SPaolo Bonzini } 11696e790746SPaolo Bonzini 11706e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 11716e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 11726e790746SPaolo Bonzini return 1; 11736e790746SPaolo Bonzini } 11746e790746SPaolo Bonzini } 11756e790746SPaolo Bonzini } 11766e790746SPaolo Bonzini 11776e790746SPaolo Bonzini return 0; 11786e790746SPaolo Bonzini } 11796e790746SPaolo Bonzini 118097cd965cSPaolo Bonzini static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf, 118197cd965cSPaolo Bonzini size_t size) 11826e790746SPaolo Bonzini { 11836e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 11846e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 118517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 11866e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 11876e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 11886e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 11896e790746SPaolo Bonzini size_t offset, i, guest_offset; 11906e790746SPaolo Bonzini 11916e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 11926e790746SPaolo Bonzini return -1; 11936e790746SPaolo Bonzini } 11946e790746SPaolo Bonzini 11956e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 11966e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 11976e790746SPaolo Bonzini return 0; 11986e790746SPaolo Bonzini } 11996e790746SPaolo Bonzini 12006e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 12016e790746SPaolo Bonzini return size; 12026e790746SPaolo Bonzini 12036e790746SPaolo Bonzini offset = i = 0; 12046e790746SPaolo Bonzini 12056e790746SPaolo Bonzini while (offset < size) { 120651b19ebeSPaolo Bonzini VirtQueueElement *elem; 12076e790746SPaolo Bonzini int len, total; 120851b19ebeSPaolo Bonzini const struct iovec *sg; 12096e790746SPaolo Bonzini 12106e790746SPaolo Bonzini total = 0; 12116e790746SPaolo Bonzini 121251b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 121351b19ebeSPaolo Bonzini if (!elem) { 1214ba10b9c0SGreg Kurz if (i) { 1215ba10b9c0SGreg Kurz virtio_error(vdev, "virtio-net unexpected empty queue: " 12166e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1217019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1218019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 12196e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1220019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1221019a3edbSGerd Hoffmann vdev->guest_features); 1222ba10b9c0SGreg Kurz } 1223ba10b9c0SGreg Kurz return -1; 12246e790746SPaolo Bonzini } 12256e790746SPaolo Bonzini 122651b19ebeSPaolo Bonzini if (elem->in_num < 1) { 1227ba10b9c0SGreg Kurz virtio_error(vdev, 1228ba10b9c0SGreg Kurz "virtio-net receive queue contains no in buffers"); 1229ba10b9c0SGreg Kurz virtqueue_detach_element(q->rx_vq, elem, 0); 1230ba10b9c0SGreg Kurz g_free(elem); 1231ba10b9c0SGreg Kurz return -1; 12326e790746SPaolo Bonzini } 12336e790746SPaolo Bonzini 123451b19ebeSPaolo Bonzini sg = elem->in_sg; 12356e790746SPaolo Bonzini if (i == 0) { 12366e790746SPaolo Bonzini assert(offset == 0); 12376e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 12386e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 123951b19ebeSPaolo Bonzini sg, elem->in_num, 12406e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 12416e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 12426e790746SPaolo Bonzini } 12436e790746SPaolo Bonzini 124451b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 12456e790746SPaolo Bonzini offset = n->host_hdr_len; 12466e790746SPaolo Bonzini total += n->guest_hdr_len; 12476e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 12486e790746SPaolo Bonzini } else { 12496e790746SPaolo Bonzini guest_offset = 0; 12506e790746SPaolo Bonzini } 12516e790746SPaolo Bonzini 12526e790746SPaolo Bonzini /* copy in packet. ugh */ 125351b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 12546e790746SPaolo Bonzini buf + offset, size - offset); 12556e790746SPaolo Bonzini total += len; 12566e790746SPaolo Bonzini offset += len; 12576e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 12586e790746SPaolo Bonzini * must have consumed the complete packet. 12596e790746SPaolo Bonzini * Otherwise, drop it. */ 12606e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 126127e57efeSLadi Prosek virtqueue_unpop(q->rx_vq, elem, total); 126251b19ebeSPaolo Bonzini g_free(elem); 12636e790746SPaolo Bonzini return size; 12646e790746SPaolo Bonzini } 12656e790746SPaolo Bonzini 12666e790746SPaolo Bonzini /* signal other side */ 126751b19ebeSPaolo Bonzini virtqueue_fill(q->rx_vq, elem, total, i++); 126851b19ebeSPaolo Bonzini g_free(elem); 12696e790746SPaolo Bonzini } 12706e790746SPaolo Bonzini 12716e790746SPaolo Bonzini if (mhdr_cnt) { 12721399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 12736e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 12746e790746SPaolo Bonzini 0, 12756e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 12766e790746SPaolo Bonzini } 12776e790746SPaolo Bonzini 12786e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 127917a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 12806e790746SPaolo Bonzini 12816e790746SPaolo Bonzini return size; 12826e790746SPaolo Bonzini } 12836e790746SPaolo Bonzini 128497cd965cSPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, 128597cd965cSPaolo Bonzini size_t size) 128697cd965cSPaolo Bonzini { 128797cd965cSPaolo Bonzini ssize_t r; 128897cd965cSPaolo Bonzini 128997cd965cSPaolo Bonzini rcu_read_lock(); 129097cd965cSPaolo Bonzini r = virtio_net_receive_rcu(nc, buf, size); 129197cd965cSPaolo Bonzini rcu_read_unlock(); 129297cd965cSPaolo Bonzini return r; 129397cd965cSPaolo Bonzini } 129497cd965cSPaolo Bonzini 12956e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 12966e790746SPaolo Bonzini 12976e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 12986e790746SPaolo Bonzini { 12996e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 13006e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 130117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 13026e790746SPaolo Bonzini 130351b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 130417a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 13056e790746SPaolo Bonzini 130651b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 130751b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 13086e790746SPaolo Bonzini 13096e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 13106e790746SPaolo Bonzini virtio_net_flush_tx(q); 13116e790746SPaolo Bonzini } 13126e790746SPaolo Bonzini 13136e790746SPaolo Bonzini /* TX */ 13146e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 13156e790746SPaolo Bonzini { 13166e790746SPaolo Bonzini VirtIONet *n = q->n; 131717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 131851b19ebeSPaolo Bonzini VirtQueueElement *elem; 13196e790746SPaolo Bonzini int32_t num_packets = 0; 13206e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 132117a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 13226e790746SPaolo Bonzini return num_packets; 13236e790746SPaolo Bonzini } 13246e790746SPaolo Bonzini 132551b19ebeSPaolo Bonzini if (q->async_tx.elem) { 13266e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 13276e790746SPaolo Bonzini return num_packets; 13286e790746SPaolo Bonzini } 13296e790746SPaolo Bonzini 133051b19ebeSPaolo Bonzini for (;;) { 1331bd89dd98SJason Wang ssize_t ret; 133251b19ebeSPaolo Bonzini unsigned int out_num; 133351b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 1334feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 13356e790746SPaolo Bonzini 133651b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 133751b19ebeSPaolo Bonzini if (!elem) { 133851b19ebeSPaolo Bonzini break; 133951b19ebeSPaolo Bonzini } 134051b19ebeSPaolo Bonzini 134151b19ebeSPaolo Bonzini out_num = elem->out_num; 134251b19ebeSPaolo Bonzini out_sg = elem->out_sg; 13436e790746SPaolo Bonzini if (out_num < 1) { 1344fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header not in first element"); 1345fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 1346fa5e56c2SGreg Kurz g_free(elem); 1347fa5e56c2SGreg Kurz return -EINVAL; 13486e790746SPaolo Bonzini } 13496e790746SPaolo Bonzini 1350032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 1351feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 1352feb93f36SJason Wang n->guest_hdr_len) { 1353fa5e56c2SGreg Kurz virtio_error(vdev, "virtio-net header incorrect"); 1354fa5e56c2SGreg Kurz virtqueue_detach_element(q->tx_vq, elem, 0); 1355fa5e56c2SGreg Kurz g_free(elem); 1356fa5e56c2SGreg Kurz return -EINVAL; 1357032a74a1SCédric Le Goater } 13581bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1359feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 1360feb93f36SJason Wang sg2[0].iov_base = &mhdr; 1361feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 1362feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 1363feb93f36SJason Wang out_sg, out_num, 1364feb93f36SJason Wang n->guest_hdr_len, -1); 1365feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 1366feb93f36SJason Wang goto drop; 1367032a74a1SCédric Le Goater } 1368feb93f36SJason Wang out_num += 1; 1369feb93f36SJason Wang out_sg = sg2; 1370feb93f36SJason Wang } 1371feb93f36SJason Wang } 13726e790746SPaolo Bonzini /* 13736e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 13746e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 13756e790746SPaolo Bonzini * that host is interested in. 13766e790746SPaolo Bonzini */ 13776e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 13786e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 13796e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 13806e790746SPaolo Bonzini out_sg, out_num, 13816e790746SPaolo Bonzini 0, n->host_hdr_len); 13826e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 13836e790746SPaolo Bonzini out_sg, out_num, 13846e790746SPaolo Bonzini n->guest_hdr_len, -1); 13856e790746SPaolo Bonzini out_num = sg_num; 13866e790746SPaolo Bonzini out_sg = sg; 13876e790746SPaolo Bonzini } 13886e790746SPaolo Bonzini 13896e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 13906e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 13916e790746SPaolo Bonzini if (ret == 0) { 13926e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 13936e790746SPaolo Bonzini q->async_tx.elem = elem; 13946e790746SPaolo Bonzini return -EBUSY; 13956e790746SPaolo Bonzini } 13966e790746SPaolo Bonzini 1397feb93f36SJason Wang drop: 139851b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 139917a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 140051b19ebeSPaolo Bonzini g_free(elem); 14016e790746SPaolo Bonzini 14026e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 14036e790746SPaolo Bonzini break; 14046e790746SPaolo Bonzini } 14056e790746SPaolo Bonzini } 14066e790746SPaolo Bonzini return num_packets; 14076e790746SPaolo Bonzini } 14086e790746SPaolo Bonzini 14096e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 14106e790746SPaolo Bonzini { 141117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14126e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 14136e790746SPaolo Bonzini 1414283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 1415283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 1416283e2c2aSYuri Benditovich return; 1417283e2c2aSYuri Benditovich } 1418283e2c2aSYuri Benditovich 14196e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 142017a0ca55SKONRAD Frederic if (!vdev->vm_running) { 14216e790746SPaolo Bonzini q->tx_waiting = 1; 14226e790746SPaolo Bonzini return; 14236e790746SPaolo Bonzini } 14246e790746SPaolo Bonzini 14256e790746SPaolo Bonzini if (q->tx_waiting) { 14266e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 1427bc72ad67SAlex Bligh timer_del(q->tx_timer); 14286e790746SPaolo Bonzini q->tx_waiting = 0; 1429fa5e56c2SGreg Kurz if (virtio_net_flush_tx(q) == -EINVAL) { 1430fa5e56c2SGreg Kurz return; 1431fa5e56c2SGreg Kurz } 14326e790746SPaolo Bonzini } else { 1433bc72ad67SAlex Bligh timer_mod(q->tx_timer, 1434bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 14356e790746SPaolo Bonzini q->tx_waiting = 1; 14366e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 14376e790746SPaolo Bonzini } 14386e790746SPaolo Bonzini } 14396e790746SPaolo Bonzini 14406e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 14416e790746SPaolo Bonzini { 144217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 14436e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 14446e790746SPaolo Bonzini 1445283e2c2aSYuri Benditovich if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) { 1446283e2c2aSYuri Benditovich virtio_net_drop_tx_queue_data(vdev, vq); 1447283e2c2aSYuri Benditovich return; 1448283e2c2aSYuri Benditovich } 1449283e2c2aSYuri Benditovich 14506e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 14516e790746SPaolo Bonzini return; 14526e790746SPaolo Bonzini } 14536e790746SPaolo Bonzini q->tx_waiting = 1; 14546e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 145517a0ca55SKONRAD Frederic if (!vdev->vm_running) { 14566e790746SPaolo Bonzini return; 14576e790746SPaolo Bonzini } 14586e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 14596e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 14606e790746SPaolo Bonzini } 14616e790746SPaolo Bonzini 14626e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 14636e790746SPaolo Bonzini { 14646e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 14656e790746SPaolo Bonzini VirtIONet *n = q->n; 146617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1467e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1468e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1469e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1470e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1471e8bcf842SMichael S. Tsirkin return; 1472e8bcf842SMichael S. Tsirkin } 14736e790746SPaolo Bonzini 14746e790746SPaolo Bonzini q->tx_waiting = 0; 14756e790746SPaolo Bonzini 14766e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 147717a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 14786e790746SPaolo Bonzini return; 147917a0ca55SKONRAD Frederic } 14806e790746SPaolo Bonzini 14816e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 14826e790746SPaolo Bonzini virtio_net_flush_tx(q); 14836e790746SPaolo Bonzini } 14846e790746SPaolo Bonzini 14856e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 14866e790746SPaolo Bonzini { 14876e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 14886e790746SPaolo Bonzini VirtIONet *n = q->n; 148917a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 14906e790746SPaolo Bonzini int32_t ret; 14916e790746SPaolo Bonzini 1492e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1493e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1494e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1495e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1496e8bcf842SMichael S. Tsirkin return; 1497e8bcf842SMichael S. Tsirkin } 14986e790746SPaolo Bonzini 14996e790746SPaolo Bonzini q->tx_waiting = 0; 15006e790746SPaolo Bonzini 15016e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 150217a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 15036e790746SPaolo Bonzini return; 150417a0ca55SKONRAD Frederic } 15056e790746SPaolo Bonzini 15066e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 1507fa5e56c2SGreg Kurz if (ret == -EBUSY || ret == -EINVAL) { 1508fa5e56c2SGreg Kurz return; /* Notification re-enable handled by tx_complete or device 1509fa5e56c2SGreg Kurz * broken */ 15106e790746SPaolo Bonzini } 15116e790746SPaolo Bonzini 15126e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 15136e790746SPaolo Bonzini * more coming and immediately reschedule */ 15146e790746SPaolo Bonzini if (ret >= n->tx_burst) { 15156e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 15166e790746SPaolo Bonzini q->tx_waiting = 1; 15176e790746SPaolo Bonzini return; 15186e790746SPaolo Bonzini } 15196e790746SPaolo Bonzini 15206e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 15216e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 15226e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 15236e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 1524fa5e56c2SGreg Kurz ret = virtio_net_flush_tx(q); 1525fa5e56c2SGreg Kurz if (ret == -EINVAL) { 1526fa5e56c2SGreg Kurz return; 1527fa5e56c2SGreg Kurz } else if (ret > 0) { 15286e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 15296e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 15306e790746SPaolo Bonzini q->tx_waiting = 1; 15316e790746SPaolo Bonzini } 15326e790746SPaolo Bonzini } 15336e790746SPaolo Bonzini 1534f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 1535f9d6dbf0SWen Congyang { 1536f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1537f9d6dbf0SWen Congyang 15381c0fbfa3SMichael S. Tsirkin n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size, 15391c0fbfa3SMichael S. Tsirkin virtio_net_handle_rx); 15409b02e161SWei Wang 1541f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 1542f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 15439b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 15449b02e161SWei Wang virtio_net_handle_tx_timer); 1545f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 1546f9d6dbf0SWen Congyang virtio_net_tx_timer, 1547f9d6dbf0SWen Congyang &n->vqs[index]); 1548f9d6dbf0SWen Congyang } else { 1549f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 15509b02e161SWei Wang virtio_add_queue(vdev, n->net_conf.tx_queue_size, 15519b02e161SWei Wang virtio_net_handle_tx_bh); 1552f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 1553f9d6dbf0SWen Congyang } 1554f9d6dbf0SWen Congyang 1555f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 1556f9d6dbf0SWen Congyang n->vqs[index].n = n; 1557f9d6dbf0SWen Congyang } 1558f9d6dbf0SWen Congyang 1559f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 1560f9d6dbf0SWen Congyang { 1561f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1562f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 1563f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 1564f9d6dbf0SWen Congyang 1565f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 1566f9d6dbf0SWen Congyang 1567f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 1568f9d6dbf0SWen Congyang if (q->tx_timer) { 1569f9d6dbf0SWen Congyang timer_del(q->tx_timer); 1570f9d6dbf0SWen Congyang timer_free(q->tx_timer); 1571f989c30cSYunjian Wang q->tx_timer = NULL; 1572f9d6dbf0SWen Congyang } else { 1573f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 1574f989c30cSYunjian Wang q->tx_bh = NULL; 1575f9d6dbf0SWen Congyang } 1576f989c30cSYunjian Wang q->tx_waiting = 0; 1577f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 1578f9d6dbf0SWen Congyang } 1579f9d6dbf0SWen Congyang 1580f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues) 1581f9d6dbf0SWen Congyang { 1582f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1583f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 1584f9d6dbf0SWen Congyang int new_num_queues = new_max_queues * 2 + 1; 1585f9d6dbf0SWen Congyang int i; 1586f9d6dbf0SWen Congyang 1587f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 1588f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 1589f9d6dbf0SWen Congyang 1590f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 1591f9d6dbf0SWen Congyang return; 1592f9d6dbf0SWen Congyang } 1593f9d6dbf0SWen Congyang 1594f9d6dbf0SWen Congyang /* 1595f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 1596f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 1597f9d6dbf0SWen Congyang * and then we only enter one of the following too loops. 1598f9d6dbf0SWen Congyang */ 1599f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 1600f9d6dbf0SWen Congyang 1601f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 1602f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 1603f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 1604f9d6dbf0SWen Congyang } 1605f9d6dbf0SWen Congyang 1606f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 1607f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 1608f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 1609f9d6dbf0SWen Congyang } 1610f9d6dbf0SWen Congyang 1611f9d6dbf0SWen Congyang /* add ctrl_vq last */ 1612f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 1613f9d6dbf0SWen Congyang } 1614f9d6dbf0SWen Congyang 1615ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 16166e790746SPaolo Bonzini { 1617f9d6dbf0SWen Congyang int max = multiqueue ? n->max_queues : 1; 1618f9d6dbf0SWen Congyang 16196e790746SPaolo Bonzini n->multiqueue = multiqueue; 1620f9d6dbf0SWen Congyang virtio_net_change_num_queues(n, max); 16216e790746SPaolo Bonzini 16226e790746SPaolo Bonzini virtio_net_set_queues(n); 16236e790746SPaolo Bonzini } 16246e790746SPaolo Bonzini 1625982b78c5SDr. David Alan Gilbert static int virtio_net_post_load_device(void *opaque, int version_id) 1626037dab2fSGreg Kurz { 1627982b78c5SDr. David Alan Gilbert VirtIONet *n = opaque; 1628982b78c5SDr. David Alan Gilbert VirtIODevice *vdev = VIRTIO_DEVICE(n); 1629037dab2fSGreg Kurz int i, link_down; 1630037dab2fSGreg Kurz 1631982b78c5SDr. David Alan Gilbert virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, 163295129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 163395129d6fSCornelia Huck VIRTIO_F_VERSION_1)); 16346e790746SPaolo Bonzini 16356e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 1636982b78c5SDr. David Alan Gilbert if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { 16376e790746SPaolo Bonzini n->mac_table.in_use = 0; 16386e790746SPaolo Bonzini } 16396e790746SPaolo Bonzini 1640982b78c5SDr. David Alan Gilbert if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 16416c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 16426c666823SMichael S. Tsirkin } 16436c666823SMichael S. Tsirkin 16446c666823SMichael S. Tsirkin if (peer_has_vnet_hdr(n)) { 16456c666823SMichael S. Tsirkin virtio_net_apply_guest_offloads(n); 16466c666823SMichael S. Tsirkin } 16476c666823SMichael S. Tsirkin 16486e790746SPaolo Bonzini virtio_net_set_queues(n); 16496e790746SPaolo Bonzini 16506e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 16516e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 16526e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 16536e790746SPaolo Bonzini break; 16546e790746SPaolo Bonzini } 16556e790746SPaolo Bonzini } 16566e790746SPaolo Bonzini n->mac_table.first_multi = i; 16576e790746SPaolo Bonzini 16586e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 16596e790746SPaolo Bonzini * to link status bit in n->status */ 16606e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 16616e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 16626e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 16636e790746SPaolo Bonzini } 16646e790746SPaolo Bonzini 16656c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 16666c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 16676c666823SMichael S. Tsirkin n->announce_counter = SELF_ANNOUNCE_ROUNDS; 16686c666823SMichael S. Tsirkin timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)); 16696c666823SMichael S. Tsirkin } 16706c666823SMichael S. Tsirkin 16716e790746SPaolo Bonzini return 0; 16726e790746SPaolo Bonzini } 16736e790746SPaolo Bonzini 1674982b78c5SDr. David Alan Gilbert /* tx_waiting field of a VirtIONetQueue */ 1675982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = { 1676982b78c5SDr. David Alan Gilbert .name = "virtio-net-queue-tx_waiting", 1677982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1678982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(tx_waiting, VirtIONetQueue), 1679982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1680982b78c5SDr. David Alan Gilbert }, 1681982b78c5SDr. David Alan Gilbert }; 1682982b78c5SDr. David Alan Gilbert 1683982b78c5SDr. David Alan Gilbert static bool max_queues_gt_1(void *opaque, int version_id) 1684982b78c5SDr. David Alan Gilbert { 1685982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->max_queues > 1; 1686982b78c5SDr. David Alan Gilbert } 1687982b78c5SDr. David Alan Gilbert 1688982b78c5SDr. David Alan Gilbert static bool has_ctrl_guest_offloads(void *opaque, int version_id) 1689982b78c5SDr. David Alan Gilbert { 1690982b78c5SDr. David Alan Gilbert return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), 1691982b78c5SDr. David Alan Gilbert VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 1692982b78c5SDr. David Alan Gilbert } 1693982b78c5SDr. David Alan Gilbert 1694982b78c5SDr. David Alan Gilbert static bool mac_table_fits(void *opaque, int version_id) 1695982b78c5SDr. David Alan Gilbert { 1696982b78c5SDr. David Alan Gilbert return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES; 1697982b78c5SDr. David Alan Gilbert } 1698982b78c5SDr. David Alan Gilbert 1699982b78c5SDr. David Alan Gilbert static bool mac_table_doesnt_fit(void *opaque, int version_id) 1700982b78c5SDr. David Alan Gilbert { 1701982b78c5SDr. David Alan Gilbert return !mac_table_fits(opaque, version_id); 1702982b78c5SDr. David Alan Gilbert } 1703982b78c5SDr. David Alan Gilbert 1704982b78c5SDr. David Alan Gilbert /* This temporary type is shared by all the WITH_TMP methods 1705982b78c5SDr. David Alan Gilbert * although only some fields are used by each. 1706982b78c5SDr. David Alan Gilbert */ 1707982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp { 1708982b78c5SDr. David Alan Gilbert VirtIONet *parent; 1709982b78c5SDr. David Alan Gilbert VirtIONetQueue *vqs_1; 1710982b78c5SDr. David Alan Gilbert uint16_t curr_queues_1; 1711982b78c5SDr. David Alan Gilbert uint8_t has_ufo; 1712982b78c5SDr. David Alan Gilbert uint32_t has_vnet_hdr; 1713982b78c5SDr. David Alan Gilbert }; 1714982b78c5SDr. David Alan Gilbert 1715982b78c5SDr. David Alan Gilbert /* The 2nd and subsequent tx_waiting flags are loaded later than 1716982b78c5SDr. David Alan Gilbert * the 1st entry in the queues and only if there's more than one 1717982b78c5SDr. David Alan Gilbert * entry. We use the tmp mechanism to calculate a temporary 1718982b78c5SDr. David Alan Gilbert * pointer and count and also validate the count. 1719982b78c5SDr. David Alan Gilbert */ 1720982b78c5SDr. David Alan Gilbert 172144b1ff31SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_save(void *opaque) 1722982b78c5SDr. David Alan Gilbert { 1723982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1724982b78c5SDr. David Alan Gilbert 1725982b78c5SDr. David Alan Gilbert tmp->vqs_1 = tmp->parent->vqs + 1; 1726982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = tmp->parent->curr_queues - 1; 1727982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues == 0) { 1728982b78c5SDr. David Alan Gilbert tmp->curr_queues_1 = 0; 1729982b78c5SDr. David Alan Gilbert } 173044b1ff31SDr. David Alan Gilbert 173144b1ff31SDr. David Alan Gilbert return 0; 1732982b78c5SDr. David Alan Gilbert } 1733982b78c5SDr. David Alan Gilbert 1734982b78c5SDr. David Alan Gilbert static int virtio_net_tx_waiting_pre_load(void *opaque) 1735982b78c5SDr. David Alan Gilbert { 1736982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1737982b78c5SDr. David Alan Gilbert 1738982b78c5SDr. David Alan Gilbert /* Reuse the pointer setup from save */ 1739982b78c5SDr. David Alan Gilbert virtio_net_tx_waiting_pre_save(opaque); 1740982b78c5SDr. David Alan Gilbert 1741982b78c5SDr. David Alan Gilbert if (tmp->parent->curr_queues > tmp->parent->max_queues) { 1742982b78c5SDr. David Alan Gilbert error_report("virtio-net: curr_queues %x > max_queues %x", 1743982b78c5SDr. David Alan Gilbert tmp->parent->curr_queues, tmp->parent->max_queues); 1744982b78c5SDr. David Alan Gilbert 1745982b78c5SDr. David Alan Gilbert return -EINVAL; 1746982b78c5SDr. David Alan Gilbert } 1747982b78c5SDr. David Alan Gilbert 1748982b78c5SDr. David Alan Gilbert return 0; /* all good */ 1749982b78c5SDr. David Alan Gilbert } 1750982b78c5SDr. David Alan Gilbert 1751982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_tx_waiting = { 1752982b78c5SDr. David Alan Gilbert .name = "virtio-net-tx_waiting", 1753982b78c5SDr. David Alan Gilbert .pre_load = virtio_net_tx_waiting_pre_load, 1754982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_tx_waiting_pre_save, 1755982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1756982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, 1757982b78c5SDr. David Alan Gilbert curr_queues_1, 1758982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 1759982b78c5SDr. David Alan Gilbert struct VirtIONetQueue), 1760982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1761982b78c5SDr. David Alan Gilbert }, 1762982b78c5SDr. David Alan Gilbert }; 1763982b78c5SDr. David Alan Gilbert 1764982b78c5SDr. David Alan Gilbert /* the 'has_ufo' flag is just tested; if the incoming stream has the 1765982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 1766982b78c5SDr. David Alan Gilbert */ 1767982b78c5SDr. David Alan Gilbert static int virtio_net_ufo_post_load(void *opaque, int version_id) 1768982b78c5SDr. David Alan Gilbert { 1769982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1770982b78c5SDr. David Alan Gilbert 1771982b78c5SDr. David Alan Gilbert if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { 1772982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires TUN_F_UFO support"); 1773982b78c5SDr. David Alan Gilbert return -EINVAL; 1774982b78c5SDr. David Alan Gilbert } 1775982b78c5SDr. David Alan Gilbert 1776982b78c5SDr. David Alan Gilbert return 0; 1777982b78c5SDr. David Alan Gilbert } 1778982b78c5SDr. David Alan Gilbert 177944b1ff31SDr. David Alan Gilbert static int virtio_net_ufo_pre_save(void *opaque) 1780982b78c5SDr. David Alan Gilbert { 1781982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1782982b78c5SDr. David Alan Gilbert 1783982b78c5SDr. David Alan Gilbert tmp->has_ufo = tmp->parent->has_ufo; 178444b1ff31SDr. David Alan Gilbert 178544b1ff31SDr. David Alan Gilbert return 0; 1786982b78c5SDr. David Alan Gilbert } 1787982b78c5SDr. David Alan Gilbert 1788982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_ufo = { 1789982b78c5SDr. David Alan Gilbert .name = "virtio-net-ufo", 1790982b78c5SDr. David Alan Gilbert .post_load = virtio_net_ufo_post_load, 1791982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_ufo_pre_save, 1792982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1793982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), 1794982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1795982b78c5SDr. David Alan Gilbert }, 1796982b78c5SDr. David Alan Gilbert }; 1797982b78c5SDr. David Alan Gilbert 1798982b78c5SDr. David Alan Gilbert /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the 1799982b78c5SDr. David Alan Gilbert * flag set we need to check that we have it 1800982b78c5SDr. David Alan Gilbert */ 1801982b78c5SDr. David Alan Gilbert static int virtio_net_vnet_post_load(void *opaque, int version_id) 1802982b78c5SDr. David Alan Gilbert { 1803982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1804982b78c5SDr. David Alan Gilbert 1805982b78c5SDr. David Alan Gilbert if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { 1806982b78c5SDr. David Alan Gilbert error_report("virtio-net: saved image requires vnet_hdr=on"); 1807982b78c5SDr. David Alan Gilbert return -EINVAL; 1808982b78c5SDr. David Alan Gilbert } 1809982b78c5SDr. David Alan Gilbert 1810982b78c5SDr. David Alan Gilbert return 0; 1811982b78c5SDr. David Alan Gilbert } 1812982b78c5SDr. David Alan Gilbert 181344b1ff31SDr. David Alan Gilbert static int virtio_net_vnet_pre_save(void *opaque) 1814982b78c5SDr. David Alan Gilbert { 1815982b78c5SDr. David Alan Gilbert struct VirtIONetMigTmp *tmp = opaque; 1816982b78c5SDr. David Alan Gilbert 1817982b78c5SDr. David Alan Gilbert tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr; 181844b1ff31SDr. David Alan Gilbert 181944b1ff31SDr. David Alan Gilbert return 0; 1820982b78c5SDr. David Alan Gilbert } 1821982b78c5SDr. David Alan Gilbert 1822982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_has_vnet = { 1823982b78c5SDr. David Alan Gilbert .name = "virtio-net-vnet", 1824982b78c5SDr. David Alan Gilbert .post_load = virtio_net_vnet_post_load, 1825982b78c5SDr. David Alan Gilbert .pre_save = virtio_net_vnet_pre_save, 1826982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1827982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), 1828982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1829982b78c5SDr. David Alan Gilbert }, 1830982b78c5SDr. David Alan Gilbert }; 1831982b78c5SDr. David Alan Gilbert 1832982b78c5SDr. David Alan Gilbert static const VMStateDescription vmstate_virtio_net_device = { 1833982b78c5SDr. David Alan Gilbert .name = "virtio-net-device", 1834982b78c5SDr. David Alan Gilbert .version_id = VIRTIO_NET_VM_VERSION, 1835982b78c5SDr. David Alan Gilbert .minimum_version_id = VIRTIO_NET_VM_VERSION, 1836982b78c5SDr. David Alan Gilbert .post_load = virtio_net_post_load_device, 1837982b78c5SDr. David Alan Gilbert .fields = (VMStateField[]) { 1838982b78c5SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), 1839982b78c5SDr. David Alan Gilbert VMSTATE_STRUCT_POINTER(vqs, VirtIONet, 1840982b78c5SDr. David Alan Gilbert vmstate_virtio_net_queue_tx_waiting, 1841982b78c5SDr. David Alan Gilbert VirtIONetQueue), 1842982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), 1843982b78c5SDr. David Alan Gilbert VMSTATE_UINT16(status, VirtIONet), 1844982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(promisc, VirtIONet), 1845982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(allmulti, VirtIONet), 1846982b78c5SDr. David Alan Gilbert VMSTATE_UINT32(mac_table.in_use, VirtIONet), 1847982b78c5SDr. David Alan Gilbert 1848982b78c5SDr. David Alan Gilbert /* Guarded pair: If it fits we load it, else we throw it away 1849982b78c5SDr. David Alan Gilbert * - can happen if source has a larger MAC table.; post-load 1850982b78c5SDr. David Alan Gilbert * sets flags in this case. 1851982b78c5SDr. David Alan Gilbert */ 1852982b78c5SDr. David Alan Gilbert VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, 1853982b78c5SDr. David Alan Gilbert 0, mac_table_fits, mac_table.in_use, 1854982b78c5SDr. David Alan Gilbert ETH_ALEN), 1855982b78c5SDr. David Alan Gilbert VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, 1856982b78c5SDr. David Alan Gilbert mac_table.in_use, ETH_ALEN), 1857982b78c5SDr. David Alan Gilbert 1858982b78c5SDr. David Alan Gilbert /* Note: This is an array of uint32's that's always been saved as a 1859982b78c5SDr. David Alan Gilbert * buffer; hold onto your endiannesses; it's actually used as a bitmap 1860982b78c5SDr. David Alan Gilbert * but based on the uint. 1861982b78c5SDr. David Alan Gilbert */ 1862982b78c5SDr. David Alan Gilbert VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), 1863982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1864982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_vnet), 1865982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), 1866982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), 1867982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(alluni, VirtIONet), 1868982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nomulti, VirtIONet), 1869982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nouni, VirtIONet), 1870982b78c5SDr. David Alan Gilbert VMSTATE_UINT8(nobcast, VirtIONet), 1871982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1872982b78c5SDr. David Alan Gilbert vmstate_virtio_net_has_ufo), 1873982b78c5SDr. David Alan Gilbert VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0, 1874982b78c5SDr. David Alan Gilbert vmstate_info_uint16_equal, uint16_t), 1875982b78c5SDr. David Alan Gilbert VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1), 1876982b78c5SDr. David Alan Gilbert VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, 1877982b78c5SDr. David Alan Gilbert vmstate_virtio_net_tx_waiting), 1878982b78c5SDr. David Alan Gilbert VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, 1879982b78c5SDr. David Alan Gilbert has_ctrl_guest_offloads), 1880982b78c5SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 1881982b78c5SDr. David Alan Gilbert }, 1882982b78c5SDr. David Alan Gilbert }; 1883982b78c5SDr. David Alan Gilbert 18846e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 1885f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 18866e790746SPaolo Bonzini .size = sizeof(NICState), 18876e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 18886e790746SPaolo Bonzini .receive = virtio_net_receive, 18896e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 1890b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 18916e790746SPaolo Bonzini }; 18926e790746SPaolo Bonzini 18936e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 18946e790746SPaolo Bonzini { 189517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 18966e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 18976e790746SPaolo Bonzini assert(n->vhost_started); 1898ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 18996e790746SPaolo Bonzini } 19006e790746SPaolo Bonzini 19016e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 19026e790746SPaolo Bonzini bool mask) 19036e790746SPaolo Bonzini { 190417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 19056e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 19066e790746SPaolo Bonzini assert(n->vhost_started); 1907ed8b4afeSNikolay Nikolaev vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 19086e790746SPaolo Bonzini vdev, idx, mask); 19096e790746SPaolo Bonzini } 19106e790746SPaolo Bonzini 1911019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 19126e790746SPaolo Bonzini { 19136e790746SPaolo Bonzini int i, config_size = 0; 19140cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 1915a93e599dSMaxime Coquelin 19166e790746SPaolo Bonzini for (i = 0; feature_sizes[i].flags != 0; i++) { 19176e790746SPaolo Bonzini if (host_features & feature_sizes[i].flags) { 19186e790746SPaolo Bonzini config_size = MAX(feature_sizes[i].end, config_size); 19196e790746SPaolo Bonzini } 19206e790746SPaolo Bonzini } 192117ec5a86SKONRAD Frederic n->config_size = config_size; 192217ec5a86SKONRAD Frederic } 19236e790746SPaolo Bonzini 19248a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 19258a253ec2SKONRAD Frederic const char *type) 19268a253ec2SKONRAD Frederic { 19278a253ec2SKONRAD Frederic /* 19288a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 19298a253ec2SKONRAD Frederic */ 19308a253ec2SKONRAD Frederic assert(type != NULL); 19318a253ec2SKONRAD Frederic 19328a253ec2SKONRAD Frederic g_free(n->netclient_name); 19338a253ec2SKONRAD Frederic g_free(n->netclient_type); 19348a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 19358a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 19368a253ec2SKONRAD Frederic } 19378a253ec2SKONRAD Frederic 1938e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 193917ec5a86SKONRAD Frederic { 1940e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1941284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 1942284a32f0SAndreas Färber NetClientState *nc; 19431773d9eeSKONRAD Frederic int i; 194417ec5a86SKONRAD Frederic 1945a93e599dSMaxime Coquelin if (n->net_conf.mtu) { 1946127833eeSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_MTU); 1947a93e599dSMaxime Coquelin } 1948a93e599dSMaxime Coquelin 1949*9473939eSJason Baron if (n->net_conf.duplex_str) { 1950*9473939eSJason Baron if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) { 1951*9473939eSJason Baron n->net_conf.duplex = DUPLEX_HALF; 1952*9473939eSJason Baron } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) { 1953*9473939eSJason Baron n->net_conf.duplex = DUPLEX_FULL; 1954*9473939eSJason Baron } else { 1955*9473939eSJason Baron error_setg(errp, "'duplex' must be 'half' or 'full'"); 1956*9473939eSJason Baron } 1957*9473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 1958*9473939eSJason Baron } else { 1959*9473939eSJason Baron n->net_conf.duplex = DUPLEX_UNKNOWN; 1960*9473939eSJason Baron } 1961*9473939eSJason Baron 1962*9473939eSJason Baron if (n->net_conf.speed < SPEED_UNKNOWN) { 1963*9473939eSJason Baron error_setg(errp, "'speed' must be between 0 and INT_MAX"); 1964*9473939eSJason Baron } else if (n->net_conf.speed >= 0) { 1965*9473939eSJason Baron n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX); 1966*9473939eSJason Baron } 1967*9473939eSJason Baron 1968da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 1969284a32f0SAndreas Färber virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); 197017ec5a86SKONRAD Frederic 19711c0fbfa3SMichael S. Tsirkin /* 19721c0fbfa3SMichael S. Tsirkin * We set a lower limit on RX queue size to what it always was. 19731c0fbfa3SMichael S. Tsirkin * Guests that want a smaller ring can always resize it without 19741c0fbfa3SMichael S. Tsirkin * help from us (using virtio 1 and up). 19751c0fbfa3SMichael S. Tsirkin */ 19761c0fbfa3SMichael S. Tsirkin if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE || 19771c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE || 19785f997fd1SMichal Privoznik !is_power_of_2(n->net_conf.rx_queue_size)) { 19791c0fbfa3SMichael S. Tsirkin error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), " 19801c0fbfa3SMichael S. Tsirkin "must be a power of 2 between %d and %d.", 19811c0fbfa3SMichael S. Tsirkin n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE, 19821c0fbfa3SMichael S. Tsirkin VIRTQUEUE_MAX_SIZE); 19831c0fbfa3SMichael S. Tsirkin virtio_cleanup(vdev); 19841c0fbfa3SMichael S. Tsirkin return; 19851c0fbfa3SMichael S. Tsirkin } 19861c0fbfa3SMichael S. Tsirkin 19879b02e161SWei Wang if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE || 19889b02e161SWei Wang n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE || 19899b02e161SWei Wang !is_power_of_2(n->net_conf.tx_queue_size)) { 19909b02e161SWei Wang error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), " 19919b02e161SWei Wang "must be a power of 2 between %d and %d", 19929b02e161SWei Wang n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE, 19939b02e161SWei Wang VIRTQUEUE_MAX_SIZE); 19949b02e161SWei Wang virtio_cleanup(vdev); 19959b02e161SWei Wang return; 19969b02e161SWei Wang } 19979b02e161SWei Wang 1998575a1c0eSJiri Pirko n->max_queues = MAX(n->nic_conf.peers.queues, 1); 199987b3bd1cSJason Wang if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) { 20007e0e736eSJason Wang error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 2001631b22eaSStefan Weil "must be a positive integer less than %d.", 200287b3bd1cSJason Wang n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2); 20037e0e736eSJason Wang virtio_cleanup(vdev); 20047e0e736eSJason Wang return; 20057e0e736eSJason Wang } 20066e790746SPaolo Bonzini n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); 20076e790746SPaolo Bonzini n->curr_queues = 1; 20081773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 20096e790746SPaolo Bonzini 20101773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 20111773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 20126e790746SPaolo Bonzini error_report("virtio-net: " 20136e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 20141773d9eeSKONRAD Frederic n->net_conf.tx); 20156e790746SPaolo Bonzini error_report("Defaulting to \"bh\""); 20166e790746SPaolo Bonzini } 20176e790746SPaolo Bonzini 20182eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n), 20192eef278bSMichael S. Tsirkin n->net_conf.tx_queue_size); 20209b02e161SWei Wang 2021da51a335SJason Wang for (i = 0; i < n->max_queues; i++) { 2022f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 2023da51a335SJason Wang } 2024da51a335SJason Wang 202517a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 20261773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 20271773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 20286e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 2029f57fcf70SJason Wang n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, 2030f57fcf70SJason Wang virtio_net_announce_timer, n); 20316e790746SPaolo Bonzini 20328a253ec2SKONRAD Frederic if (n->netclient_type) { 20338a253ec2SKONRAD Frederic /* 20348a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 20358a253ec2SKONRAD Frederic */ 20368a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 20378a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 20388a253ec2SKONRAD Frederic } else { 20391773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 2040284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 20418a253ec2SKONRAD Frederic } 20428a253ec2SKONRAD Frederic 20436e790746SPaolo Bonzini peer_test_vnet_hdr(n); 20446e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 20456e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 2046d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 20476e790746SPaolo Bonzini } 20486e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 20496e790746SPaolo Bonzini } else { 20506e790746SPaolo Bonzini n->host_hdr_len = 0; 20516e790746SPaolo Bonzini } 20526e790746SPaolo Bonzini 20531773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 20546e790746SPaolo Bonzini 20556e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 20561773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 2057bb9d17f8SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 0, 0); 20586e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 20596e790746SPaolo Bonzini 20606e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 20616e790746SPaolo Bonzini 20626e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 20636e790746SPaolo Bonzini 2064b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 2065b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 2066b1be4280SAmos Kong 2067284a32f0SAndreas Färber n->qdev = dev; 206817ec5a86SKONRAD Frederic } 206917ec5a86SKONRAD Frederic 2070306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp) 207117ec5a86SKONRAD Frederic { 2072306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 2073306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 2074f9d6dbf0SWen Congyang int i, max_queues; 207517ec5a86SKONRAD Frederic 207617ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 207717ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 207817ec5a86SKONRAD Frederic 20798a253ec2SKONRAD Frederic g_free(n->netclient_name); 20808a253ec2SKONRAD Frederic n->netclient_name = NULL; 20818a253ec2SKONRAD Frederic g_free(n->netclient_type); 20828a253ec2SKONRAD Frederic n->netclient_type = NULL; 20838a253ec2SKONRAD Frederic 208417ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 208517ec5a86SKONRAD Frederic g_free(n->vlans); 208617ec5a86SKONRAD Frederic 2087f9d6dbf0SWen Congyang max_queues = n->multiqueue ? n->max_queues : 1; 2088f9d6dbf0SWen Congyang for (i = 0; i < max_queues; i++) { 2089f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 209017ec5a86SKONRAD Frederic } 209117ec5a86SKONRAD Frederic 2092f57fcf70SJason Wang timer_del(n->announce_timer); 2093f57fcf70SJason Wang timer_free(n->announce_timer); 209417ec5a86SKONRAD Frederic g_free(n->vqs); 209517ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 20966a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 209717ec5a86SKONRAD Frederic } 209817ec5a86SKONRAD Frederic 209917ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 210017ec5a86SKONRAD Frederic { 210117ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 210217ec5a86SKONRAD Frederic 210317ec5a86SKONRAD Frederic /* 210417ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 210517ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 210617ec5a86SKONRAD Frederic */ 210717ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 2108aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 2109aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 2110aa4197c3SGonglei DEVICE(n), NULL); 211117ec5a86SKONRAD Frederic } 211217ec5a86SKONRAD Frederic 211344b1ff31SDr. David Alan Gilbert static int virtio_net_pre_save(void *opaque) 21144d45dcfbSHalil Pasic { 21154d45dcfbSHalil Pasic VirtIONet *n = opaque; 21164d45dcfbSHalil Pasic 21174d45dcfbSHalil Pasic /* At this point, backend must be stopped, otherwise 21184d45dcfbSHalil Pasic * it might keep writing to memory. */ 21194d45dcfbSHalil Pasic assert(!n->vhost_started); 212044b1ff31SDr. David Alan Gilbert 212144b1ff31SDr. David Alan Gilbert return 0; 21224d45dcfbSHalil Pasic } 21234d45dcfbSHalil Pasic 21244d45dcfbSHalil Pasic static const VMStateDescription vmstate_virtio_net = { 21254d45dcfbSHalil Pasic .name = "virtio-net", 21264d45dcfbSHalil Pasic .minimum_version_id = VIRTIO_NET_VM_VERSION, 21274d45dcfbSHalil Pasic .version_id = VIRTIO_NET_VM_VERSION, 21284d45dcfbSHalil Pasic .fields = (VMStateField[]) { 21294d45dcfbSHalil Pasic VMSTATE_VIRTIO_DEVICE, 21304d45dcfbSHalil Pasic VMSTATE_END_OF_LIST() 21314d45dcfbSHalil Pasic }, 21324d45dcfbSHalil Pasic .pre_save = virtio_net_pre_save, 21334d45dcfbSHalil Pasic }; 2134290c2428SDr. David Alan Gilbert 213517ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 2136127833eeSJason Baron DEFINE_PROP_BIT64("csum", VirtIONet, host_features, 2137127833eeSJason Baron VIRTIO_NET_F_CSUM, true), 2138127833eeSJason Baron DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features, 213987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 2140127833eeSJason Baron DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 2141127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features, 214287108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 2143127833eeSJason Baron DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features, 214487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 2145127833eeSJason Baron DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features, 214687108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 2147127833eeSJason Baron DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features, 214887108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 2149127833eeSJason Baron DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features, 215087108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 2151127833eeSJason Baron DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features, 215287108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 2153127833eeSJason Baron DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features, 215487108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 2155127833eeSJason Baron DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features, 215687108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 2157127833eeSJason Baron DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features, 215887108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 2159127833eeSJason Baron DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features, 216087108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 2161127833eeSJason Baron DEFINE_PROP_BIT64("status", VirtIONet, host_features, 216287108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 2163127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features, 216487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 2165127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features, 216687108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 2167127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features, 216887108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 2169127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features, 217087108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 2171127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features, 217287108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 2173127833eeSJason Baron DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features, 217487108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 2175127833eeSJason Baron DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 217617ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 217717ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 217817ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 217917ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 218017ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 21811c0fbfa3SMichael S. Tsirkin DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size, 21821c0fbfa3SMichael S. Tsirkin VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE), 21839b02e161SWei Wang DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size, 21849b02e161SWei Wang VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE), 2185a93e599dSMaxime Coquelin DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0), 218675ebec11SMaxime Coquelin DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend, 218775ebec11SMaxime Coquelin true), 2188*9473939eSJason Baron DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN), 2189*9473939eSJason Baron DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str), 219017ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 219117ec5a86SKONRAD Frederic }; 219217ec5a86SKONRAD Frederic 219317ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 219417ec5a86SKONRAD Frederic { 219517ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 219617ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 2197e6f746b3SAndreas Färber 219817ec5a86SKONRAD Frederic dc->props = virtio_net_properties; 2199290c2428SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_net; 2200125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 2201e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 2202306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 220317ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 220417ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 220517ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 220617ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 220717ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 220817ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 220917ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 221017ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 221117ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 22122a083ffdSMichael S. Tsirkin vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO); 2213982b78c5SDr. David Alan Gilbert vdc->vmsd = &vmstate_virtio_net_device; 221417ec5a86SKONRAD Frederic } 221517ec5a86SKONRAD Frederic 221617ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 221717ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 221817ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 221917ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 222017ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 222117ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 222217ec5a86SKONRAD Frederic }; 222317ec5a86SKONRAD Frederic 222417ec5a86SKONRAD Frederic static void virtio_register_types(void) 222517ec5a86SKONRAD Frederic { 222617ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 222717ec5a86SKONRAD Frederic } 222817ec5a86SKONRAD Frederic 222917ec5a86SKONRAD Frederic type_init(virtio_register_types) 2230