16e790746SPaolo Bonzini /* 26e790746SPaolo Bonzini * Virtio Network Device 36e790746SPaolo Bonzini * 46e790746SPaolo Bonzini * Copyright IBM, Corp. 2007 56e790746SPaolo Bonzini * 66e790746SPaolo Bonzini * Authors: 76e790746SPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 86e790746SPaolo Bonzini * 96e790746SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 106e790746SPaolo Bonzini * the COPYING file in the top-level directory. 116e790746SPaolo Bonzini * 126e790746SPaolo Bonzini */ 136e790746SPaolo Bonzini 149b8bfe21SPeter Maydell #include "qemu/osdep.h" 156e790746SPaolo Bonzini #include "qemu/iov.h" 166e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 176e790746SPaolo Bonzini #include "net/net.h" 186e790746SPaolo Bonzini #include "net/checksum.h" 196e790746SPaolo Bonzini #include "net/tap.h" 206e790746SPaolo Bonzini #include "qemu/error-report.h" 216e790746SPaolo Bonzini #include "qemu/timer.h" 226e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 236e790746SPaolo Bonzini #include "net/vhost_net.h" 2417ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 25b1be4280SAmos Kong #include "qapi/qmp/qjson.h" 2606150279SWenchao Xia #include "qapi-event.h" 271399c60dSRusty Russell #include "hw/virtio/virtio-access.h" 286e790746SPaolo Bonzini 296e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 306e790746SPaolo Bonzini 316e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 326e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 336e790746SPaolo Bonzini 346e790746SPaolo Bonzini /* 356e790746SPaolo Bonzini * Calculate the number of bytes up to and including the given 'field' of 366e790746SPaolo Bonzini * 'container'. 376e790746SPaolo Bonzini */ 386e790746SPaolo Bonzini #define endof(container, field) \ 396e790746SPaolo Bonzini (offsetof(container, field) + sizeof(((container *)0)->field)) 406e790746SPaolo Bonzini 416e790746SPaolo Bonzini typedef struct VirtIOFeature { 426e790746SPaolo Bonzini uint32_t flags; 436e790746SPaolo Bonzini size_t end; 446e790746SPaolo Bonzini } VirtIOFeature; 456e790746SPaolo Bonzini 466e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = { 476e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MAC, 486e790746SPaolo Bonzini .end = endof(struct virtio_net_config, mac)}, 496e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_STATUS, 506e790746SPaolo Bonzini .end = endof(struct virtio_net_config, status)}, 516e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MQ, 526e790746SPaolo Bonzini .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 536e790746SPaolo Bonzini {} 546e790746SPaolo Bonzini }; 556e790746SPaolo Bonzini 566e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 576e790746SPaolo Bonzini { 586e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 596e790746SPaolo Bonzini 606e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 616e790746SPaolo Bonzini } 626e790746SPaolo Bonzini 636e790746SPaolo Bonzini static int vq2q(int queue_index) 646e790746SPaolo Bonzini { 656e790746SPaolo Bonzini return queue_index / 2; 666e790746SPaolo Bonzini } 676e790746SPaolo Bonzini 686e790746SPaolo Bonzini /* TODO 696e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 706e790746SPaolo Bonzini */ 716e790746SPaolo Bonzini 726e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 736e790746SPaolo Bonzini { 7417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 756e790746SPaolo Bonzini struct virtio_net_config netcfg; 766e790746SPaolo Bonzini 771399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.status, n->status); 781399c60dSRusty Russell virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues); 796e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 806e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 816e790746SPaolo Bonzini } 826e790746SPaolo Bonzini 836e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 846e790746SPaolo Bonzini { 8517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 866e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 876e790746SPaolo Bonzini 886e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 896e790746SPaolo Bonzini 9095129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) && 9195129d6fSCornelia Huck !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) && 926e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 936e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 946e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 956e790746SPaolo Bonzini } 966e790746SPaolo Bonzini } 976e790746SPaolo Bonzini 986e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 996e790746SPaolo Bonzini { 10017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1016e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 10217a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 1036e790746SPaolo Bonzini } 1046e790746SPaolo Bonzini 105f57fcf70SJason Wang static void virtio_net_announce_timer(void *opaque) 106f57fcf70SJason Wang { 107f57fcf70SJason Wang VirtIONet *n = opaque; 108f57fcf70SJason Wang VirtIODevice *vdev = VIRTIO_DEVICE(n); 109f57fcf70SJason Wang 110f57fcf70SJason Wang n->announce_counter--; 111f57fcf70SJason Wang n->status |= VIRTIO_NET_S_ANNOUNCE; 112f57fcf70SJason Wang virtio_notify_config(vdev); 113f57fcf70SJason Wang } 114f57fcf70SJason Wang 1156e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 1166e790746SPaolo Bonzini { 11717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1186e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 1196e790746SPaolo Bonzini int queues = n->multiqueue ? n->max_queues : 1; 1206e790746SPaolo Bonzini 121ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 1226e790746SPaolo Bonzini return; 1236e790746SPaolo Bonzini } 1246e790746SPaolo Bonzini 1258c1ac475SRadim Krčmář if ((virtio_net_started(n, status) && !nc->peer->link_down) == 1268c1ac475SRadim Krčmář !!n->vhost_started) { 1276e790746SPaolo Bonzini return; 1286e790746SPaolo Bonzini } 1296e790746SPaolo Bonzini if (!n->vhost_started) { 130086abc1cSMichael S. Tsirkin int r, i; 131086abc1cSMichael S. Tsirkin 1321bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1331bfa316cSGreg Kurz error_report("backend does not support %s vnet headers; " 1341bfa316cSGreg Kurz "falling back on userspace virtio", 1351bfa316cSGreg Kurz virtio_is_big_endian(vdev) ? "BE" : "LE"); 1361bfa316cSGreg Kurz return; 1371bfa316cSGreg Kurz } 1381bfa316cSGreg Kurz 139086abc1cSMichael S. Tsirkin /* Any packets outstanding? Purge them to avoid touching rings 140086abc1cSMichael S. Tsirkin * when vhost is running. 141086abc1cSMichael S. Tsirkin */ 142086abc1cSMichael S. Tsirkin for (i = 0; i < queues; i++) { 143086abc1cSMichael S. Tsirkin NetClientState *qnc = qemu_get_subqueue(n->nic, i); 144086abc1cSMichael S. Tsirkin 145086abc1cSMichael S. Tsirkin /* Purge both directions: TX and RX. */ 146086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->peer->incoming_queue, qnc); 147086abc1cSMichael S. Tsirkin qemu_net_queue_purge(qnc->incoming_queue, qnc->peer); 148086abc1cSMichael S. Tsirkin } 149086abc1cSMichael S. Tsirkin 1506e790746SPaolo Bonzini n->vhost_started = 1; 15117a0ca55SKONRAD Frederic r = vhost_net_start(vdev, n->nic->ncs, queues); 1526e790746SPaolo Bonzini if (r < 0) { 1536e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 1546e790746SPaolo Bonzini "falling back on userspace virtio", -r); 1556e790746SPaolo Bonzini n->vhost_started = 0; 1566e790746SPaolo Bonzini } 1576e790746SPaolo Bonzini } else { 15817a0ca55SKONRAD Frederic vhost_net_stop(vdev, n->nic->ncs, queues); 1596e790746SPaolo Bonzini n->vhost_started = 0; 1606e790746SPaolo Bonzini } 1616e790746SPaolo Bonzini } 1626e790746SPaolo Bonzini 1631bfa316cSGreg Kurz static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev, 1641bfa316cSGreg Kurz NetClientState *peer, 1651bfa316cSGreg Kurz bool enable) 1661bfa316cSGreg Kurz { 1671bfa316cSGreg Kurz if (virtio_is_big_endian(vdev)) { 1681bfa316cSGreg Kurz return qemu_set_vnet_be(peer, enable); 1691bfa316cSGreg Kurz } else { 1701bfa316cSGreg Kurz return qemu_set_vnet_le(peer, enable); 1711bfa316cSGreg Kurz } 1721bfa316cSGreg Kurz } 1731bfa316cSGreg Kurz 1741bfa316cSGreg Kurz static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs, 1751bfa316cSGreg Kurz int queues, bool enable) 1761bfa316cSGreg Kurz { 1771bfa316cSGreg Kurz int i; 1781bfa316cSGreg Kurz 1791bfa316cSGreg Kurz for (i = 0; i < queues; i++) { 1801bfa316cSGreg Kurz if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 && 1811bfa316cSGreg Kurz enable) { 1821bfa316cSGreg Kurz while (--i >= 0) { 1831bfa316cSGreg Kurz virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false); 1841bfa316cSGreg Kurz } 1851bfa316cSGreg Kurz 1861bfa316cSGreg Kurz return true; 1871bfa316cSGreg Kurz } 1881bfa316cSGreg Kurz } 1891bfa316cSGreg Kurz 1901bfa316cSGreg Kurz return false; 1911bfa316cSGreg Kurz } 1921bfa316cSGreg Kurz 1931bfa316cSGreg Kurz static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status) 1941bfa316cSGreg Kurz { 1951bfa316cSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(n); 1961bfa316cSGreg Kurz int queues = n->multiqueue ? n->max_queues : 1; 1971bfa316cSGreg Kurz 1981bfa316cSGreg Kurz if (virtio_net_started(n, status)) { 1991bfa316cSGreg Kurz /* Before using the device, we tell the network backend about the 2001bfa316cSGreg Kurz * endianness to use when parsing vnet headers. If the backend 2011bfa316cSGreg Kurz * can't do it, we fallback onto fixing the headers in the core 2021bfa316cSGreg Kurz * virtio-net code. 2031bfa316cSGreg Kurz */ 2041bfa316cSGreg Kurz n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs, 2051bfa316cSGreg Kurz queues, true); 2061bfa316cSGreg Kurz } else if (virtio_net_started(n, vdev->status)) { 2071bfa316cSGreg Kurz /* After using the device, we need to reset the network backend to 2081bfa316cSGreg Kurz * the default (guest native endianness), otherwise the guest may 2091bfa316cSGreg Kurz * lose network connectivity if it is rebooted into a different 2101bfa316cSGreg Kurz * endianness. 2111bfa316cSGreg Kurz */ 2121bfa316cSGreg Kurz virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false); 2131bfa316cSGreg Kurz } 2141bfa316cSGreg Kurz } 2151bfa316cSGreg Kurz 2166e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 2176e790746SPaolo Bonzini { 21817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 2196e790746SPaolo Bonzini VirtIONetQueue *q; 2206e790746SPaolo Bonzini int i; 2216e790746SPaolo Bonzini uint8_t queue_status; 2226e790746SPaolo Bonzini 2231bfa316cSGreg Kurz virtio_net_vnet_endian_status(n, status); 2246e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 2256e790746SPaolo Bonzini 2266e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 22738705bb5SFam Zheng NetClientState *ncs = qemu_get_subqueue(n->nic, i); 22838705bb5SFam Zheng bool queue_started; 2296e790746SPaolo Bonzini q = &n->vqs[i]; 2306e790746SPaolo Bonzini 2316e790746SPaolo Bonzini if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { 2326e790746SPaolo Bonzini queue_status = 0; 2336e790746SPaolo Bonzini } else { 2346e790746SPaolo Bonzini queue_status = status; 2356e790746SPaolo Bonzini } 23638705bb5SFam Zheng queue_started = 23738705bb5SFam Zheng virtio_net_started(n, queue_status) && !n->vhost_started; 23838705bb5SFam Zheng 23938705bb5SFam Zheng if (queue_started) { 24038705bb5SFam Zheng qemu_flush_queued_packets(ncs); 24138705bb5SFam Zheng } 2426e790746SPaolo Bonzini 2436e790746SPaolo Bonzini if (!q->tx_waiting) { 2446e790746SPaolo Bonzini continue; 2456e790746SPaolo Bonzini } 2466e790746SPaolo Bonzini 24738705bb5SFam Zheng if (queue_started) { 2486e790746SPaolo Bonzini if (q->tx_timer) { 249bc72ad67SAlex Bligh timer_mod(q->tx_timer, 250bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 2516e790746SPaolo Bonzini } else { 2526e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 2536e790746SPaolo Bonzini } 2546e790746SPaolo Bonzini } else { 2556e790746SPaolo Bonzini if (q->tx_timer) { 256bc72ad67SAlex Bligh timer_del(q->tx_timer); 2576e790746SPaolo Bonzini } else { 2586e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 2596e790746SPaolo Bonzini } 2606e790746SPaolo Bonzini } 2616e790746SPaolo Bonzini } 2626e790746SPaolo Bonzini } 2636e790746SPaolo Bonzini 2646e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 2656e790746SPaolo Bonzini { 2666e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 26717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 2686e790746SPaolo Bonzini uint16_t old_status = n->status; 2696e790746SPaolo Bonzini 2706e790746SPaolo Bonzini if (nc->link_down) 2716e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 2726e790746SPaolo Bonzini else 2736e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 2746e790746SPaolo Bonzini 2756e790746SPaolo Bonzini if (n->status != old_status) 27617a0ca55SKONRAD Frederic virtio_notify_config(vdev); 2776e790746SPaolo Bonzini 27817a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 2796e790746SPaolo Bonzini } 2806e790746SPaolo Bonzini 281b1be4280SAmos Kong static void rxfilter_notify(NetClientState *nc) 282b1be4280SAmos Kong { 283b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 284b1be4280SAmos Kong 285b1be4280SAmos Kong if (nc->rxfilter_notify_enabled) { 28696e35046SAmos Kong gchar *path = object_get_canonical_path(OBJECT(n->qdev)); 28706150279SWenchao Xia qapi_event_send_nic_rx_filter_changed(!!n->netclient_name, 28806150279SWenchao Xia n->netclient_name, path, &error_abort); 28996e35046SAmos Kong g_free(path); 290b1be4280SAmos Kong 291b1be4280SAmos Kong /* disable event notification to avoid events flooding */ 292b1be4280SAmos Kong nc->rxfilter_notify_enabled = 0; 293b1be4280SAmos Kong } 294b1be4280SAmos Kong } 295b1be4280SAmos Kong 296f7bc8ef8SAmos Kong static intList *get_vlan_table(VirtIONet *n) 297f7bc8ef8SAmos Kong { 298f7bc8ef8SAmos Kong intList *list, *entry; 299f7bc8ef8SAmos Kong int i, j; 300f7bc8ef8SAmos Kong 301f7bc8ef8SAmos Kong list = NULL; 302f7bc8ef8SAmos Kong for (i = 0; i < MAX_VLAN >> 5; i++) { 303f7bc8ef8SAmos Kong for (j = 0; n->vlans[i] && j <= 0x1f; j++) { 304f7bc8ef8SAmos Kong if (n->vlans[i] & (1U << j)) { 305f7bc8ef8SAmos Kong entry = g_malloc0(sizeof(*entry)); 306f7bc8ef8SAmos Kong entry->value = (i << 5) + j; 307f7bc8ef8SAmos Kong entry->next = list; 308f7bc8ef8SAmos Kong list = entry; 309f7bc8ef8SAmos Kong } 310f7bc8ef8SAmos Kong } 311f7bc8ef8SAmos Kong } 312f7bc8ef8SAmos Kong 313f7bc8ef8SAmos Kong return list; 314f7bc8ef8SAmos Kong } 315f7bc8ef8SAmos Kong 316b1be4280SAmos Kong static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc) 317b1be4280SAmos Kong { 318b1be4280SAmos Kong VirtIONet *n = qemu_get_nic_opaque(nc); 319f7bc8ef8SAmos Kong VirtIODevice *vdev = VIRTIO_DEVICE(n); 320b1be4280SAmos Kong RxFilterInfo *info; 321b1be4280SAmos Kong strList *str_list, *entry; 322f7bc8ef8SAmos Kong int i; 323b1be4280SAmos Kong 324b1be4280SAmos Kong info = g_malloc0(sizeof(*info)); 325b1be4280SAmos Kong info->name = g_strdup(nc->name); 326b1be4280SAmos Kong info->promiscuous = n->promisc; 327b1be4280SAmos Kong 328b1be4280SAmos Kong if (n->nouni) { 329b1be4280SAmos Kong info->unicast = RX_STATE_NONE; 330b1be4280SAmos Kong } else if (n->alluni) { 331b1be4280SAmos Kong info->unicast = RX_STATE_ALL; 332b1be4280SAmos Kong } else { 333b1be4280SAmos Kong info->unicast = RX_STATE_NORMAL; 334b1be4280SAmos Kong } 335b1be4280SAmos Kong 336b1be4280SAmos Kong if (n->nomulti) { 337b1be4280SAmos Kong info->multicast = RX_STATE_NONE; 338b1be4280SAmos Kong } else if (n->allmulti) { 339b1be4280SAmos Kong info->multicast = RX_STATE_ALL; 340b1be4280SAmos Kong } else { 341b1be4280SAmos Kong info->multicast = RX_STATE_NORMAL; 342b1be4280SAmos Kong } 343b1be4280SAmos Kong 344b1be4280SAmos Kong info->broadcast_allowed = n->nobcast; 345b1be4280SAmos Kong info->multicast_overflow = n->mac_table.multi_overflow; 346b1be4280SAmos Kong info->unicast_overflow = n->mac_table.uni_overflow; 347b1be4280SAmos Kong 348b0575ba4SScott Feldman info->main_mac = qemu_mac_strdup_printf(n->mac); 349b1be4280SAmos Kong 350b1be4280SAmos Kong str_list = NULL; 351b1be4280SAmos Kong for (i = 0; i < n->mac_table.first_multi; i++) { 352b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 353b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 354b1be4280SAmos Kong entry->next = str_list; 355b1be4280SAmos Kong str_list = entry; 356b1be4280SAmos Kong } 357b1be4280SAmos Kong info->unicast_table = str_list; 358b1be4280SAmos Kong 359b1be4280SAmos Kong str_list = NULL; 360b1be4280SAmos Kong for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 361b1be4280SAmos Kong entry = g_malloc0(sizeof(*entry)); 362b0575ba4SScott Feldman entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN); 363b1be4280SAmos Kong entry->next = str_list; 364b1be4280SAmos Kong str_list = entry; 365b1be4280SAmos Kong } 366b1be4280SAmos Kong info->multicast_table = str_list; 367f7bc8ef8SAmos Kong info->vlan_table = get_vlan_table(n); 368b1be4280SAmos Kong 36995129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) { 370f7bc8ef8SAmos Kong info->vlan = RX_STATE_ALL; 371f7bc8ef8SAmos Kong } else if (!info->vlan_table) { 372f7bc8ef8SAmos Kong info->vlan = RX_STATE_NONE; 373f7bc8ef8SAmos Kong } else { 374f7bc8ef8SAmos Kong info->vlan = RX_STATE_NORMAL; 375b1be4280SAmos Kong } 376b1be4280SAmos Kong 377b1be4280SAmos Kong /* enable event notification after query */ 378b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 379b1be4280SAmos Kong 380b1be4280SAmos Kong return info; 381b1be4280SAmos Kong } 382b1be4280SAmos Kong 3836e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 3846e790746SPaolo Bonzini { 38517a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3866e790746SPaolo Bonzini 3876e790746SPaolo Bonzini /* Reset back to compatibility mode */ 3886e790746SPaolo Bonzini n->promisc = 1; 3896e790746SPaolo Bonzini n->allmulti = 0; 3906e790746SPaolo Bonzini n->alluni = 0; 3916e790746SPaolo Bonzini n->nomulti = 0; 3926e790746SPaolo Bonzini n->nouni = 0; 3936e790746SPaolo Bonzini n->nobcast = 0; 3946e790746SPaolo Bonzini /* multiqueue is disabled by default */ 3956e790746SPaolo Bonzini n->curr_queues = 1; 396f57fcf70SJason Wang timer_del(n->announce_timer); 397f57fcf70SJason Wang n->announce_counter = 0; 398f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 3996e790746SPaolo Bonzini 4006e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 4016e790746SPaolo Bonzini n->mac_table.in_use = 0; 4026e790746SPaolo Bonzini n->mac_table.first_multi = 0; 4036e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 4046e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 4056e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 4066e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 407702d66a8SMichael S. Tsirkin qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 4086e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 4096e790746SPaolo Bonzini } 4106e790746SPaolo Bonzini 4116e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 4126e790746SPaolo Bonzini { 4136e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 4146e790746SPaolo Bonzini if (!nc->peer) { 4156e790746SPaolo Bonzini return; 4166e790746SPaolo Bonzini } 4176e790746SPaolo Bonzini 418d6085e3aSStefan Hajnoczi n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer); 4196e790746SPaolo Bonzini } 4206e790746SPaolo Bonzini 4216e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 4226e790746SPaolo Bonzini { 4236e790746SPaolo Bonzini return n->has_vnet_hdr; 4246e790746SPaolo Bonzini } 4256e790746SPaolo Bonzini 4266e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 4276e790746SPaolo Bonzini { 4286e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 4296e790746SPaolo Bonzini return 0; 4306e790746SPaolo Bonzini 431d6085e3aSStefan Hajnoczi n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer); 4326e790746SPaolo Bonzini 4336e790746SPaolo Bonzini return n->has_ufo; 4346e790746SPaolo Bonzini } 4356e790746SPaolo Bonzini 436bb9d17f8SCornelia Huck static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs, 437bb9d17f8SCornelia Huck int version_1) 4386e790746SPaolo Bonzini { 4396e790746SPaolo Bonzini int i; 4406e790746SPaolo Bonzini NetClientState *nc; 4416e790746SPaolo Bonzini 4426e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 4436e790746SPaolo Bonzini 444bb9d17f8SCornelia Huck if (version_1) { 445bb9d17f8SCornelia Huck n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 446bb9d17f8SCornelia Huck } else { 4476e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 448bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr_mrg_rxbuf) : 449bb9d17f8SCornelia Huck sizeof(struct virtio_net_hdr); 450bb9d17f8SCornelia Huck } 4516e790746SPaolo Bonzini 4526e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 4536e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 4546e790746SPaolo Bonzini 4556e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 456d6085e3aSStefan Hajnoczi qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 457d6085e3aSStefan Hajnoczi qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 4586e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 4596e790746SPaolo Bonzini } 4606e790746SPaolo Bonzini } 4616e790746SPaolo Bonzini } 4626e790746SPaolo Bonzini 4636e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 4646e790746SPaolo Bonzini { 4656e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 4666e790746SPaolo Bonzini 4676e790746SPaolo Bonzini if (!nc->peer) { 4686e790746SPaolo Bonzini return 0; 4696e790746SPaolo Bonzini } 4706e790746SPaolo Bonzini 471*f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 4727263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 1); 4737263a0adSChangchun Ouyang } 4747263a0adSChangchun Ouyang 475*f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 4766e790746SPaolo Bonzini return 0; 4776e790746SPaolo Bonzini } 4786e790746SPaolo Bonzini 4796e790746SPaolo Bonzini return tap_enable(nc->peer); 4806e790746SPaolo Bonzini } 4816e790746SPaolo Bonzini 4826e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 4836e790746SPaolo Bonzini { 4846e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 4856e790746SPaolo Bonzini 4866e790746SPaolo Bonzini if (!nc->peer) { 4876e790746SPaolo Bonzini return 0; 4886e790746SPaolo Bonzini } 4896e790746SPaolo Bonzini 490*f394b2e2SEric Blake if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 4917263a0adSChangchun Ouyang vhost_set_vring_enable(nc->peer, 0); 4927263a0adSChangchun Ouyang } 4937263a0adSChangchun Ouyang 494*f394b2e2SEric Blake if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) { 4956e790746SPaolo Bonzini return 0; 4966e790746SPaolo Bonzini } 4976e790746SPaolo Bonzini 4986e790746SPaolo Bonzini return tap_disable(nc->peer); 4996e790746SPaolo Bonzini } 5006e790746SPaolo Bonzini 5016e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n) 5026e790746SPaolo Bonzini { 5036e790746SPaolo Bonzini int i; 504ddfa83eaSJoel Stanley int r; 5056e790746SPaolo Bonzini 5066e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 5076e790746SPaolo Bonzini if (i < n->curr_queues) { 508ddfa83eaSJoel Stanley r = peer_attach(n, i); 509ddfa83eaSJoel Stanley assert(!r); 5106e790746SPaolo Bonzini } else { 511ddfa83eaSJoel Stanley r = peer_detach(n, i); 512ddfa83eaSJoel Stanley assert(!r); 5136e790746SPaolo Bonzini } 5146e790746SPaolo Bonzini } 5156e790746SPaolo Bonzini } 5166e790746SPaolo Bonzini 517ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 5186e790746SPaolo Bonzini 5199d5b731dSJason Wang static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, 5209d5b731dSJason Wang Error **errp) 5216e790746SPaolo Bonzini { 52217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 5236e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 5246e790746SPaolo Bonzini 525da3e8a23SShannon Zhao /* Firstly sync all virtio-net possible supported features */ 526da3e8a23SShannon Zhao features |= n->host_features; 527da3e8a23SShannon Zhao 5280cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 5296e790746SPaolo Bonzini 5306e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 5310cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_CSUM); 5320cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4); 5330cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6); 5340cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN); 5356e790746SPaolo Bonzini 5360cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM); 5370cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4); 5380cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6); 5390cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN); 5406e790746SPaolo Bonzini } 5416e790746SPaolo Bonzini 5426e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 5430cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO); 5440cd09c3aSCornelia Huck virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO); 5456e790746SPaolo Bonzini } 5466e790746SPaolo Bonzini 547ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 5486e790746SPaolo Bonzini return features; 5496e790746SPaolo Bonzini } 550ed8b4afeSNikolay Nikolaev return vhost_net_get_features(get_vhost_net(nc->peer), features); 5516e790746SPaolo Bonzini } 5526e790746SPaolo Bonzini 553019a3edbSGerd Hoffmann static uint64_t virtio_net_bad_features(VirtIODevice *vdev) 5546e790746SPaolo Bonzini { 555019a3edbSGerd Hoffmann uint64_t features = 0; 5566e790746SPaolo Bonzini 5576e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 5586e790746SPaolo Bonzini * but also these: */ 5590cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_MAC); 5600cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_CSUM); 5610cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4); 5620cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6); 5630cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN); 5646e790746SPaolo Bonzini 5656e790746SPaolo Bonzini return features; 5666e790746SPaolo Bonzini } 5676e790746SPaolo Bonzini 568644c9858SDmitry Fleytman static void virtio_net_apply_guest_offloads(VirtIONet *n) 569644c9858SDmitry Fleytman { 570ad37bb3bSStefan Hajnoczi qemu_set_offload(qemu_get_queue(n->nic)->peer, 571644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), 572644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), 573644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), 574644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), 575644c9858SDmitry Fleytman !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); 576644c9858SDmitry Fleytman } 577644c9858SDmitry Fleytman 578644c9858SDmitry Fleytman static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) 579644c9858SDmitry Fleytman { 580644c9858SDmitry Fleytman static const uint64_t guest_offloads_mask = 581644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_CSUM) | 582644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 583644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_TSO6) | 584644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_ECN) | 585644c9858SDmitry Fleytman (1ULL << VIRTIO_NET_F_GUEST_UFO); 586644c9858SDmitry Fleytman 587644c9858SDmitry Fleytman return guest_offloads_mask & features; 588644c9858SDmitry Fleytman } 589644c9858SDmitry Fleytman 590644c9858SDmitry Fleytman static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) 591644c9858SDmitry Fleytman { 592644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 593644c9858SDmitry Fleytman return virtio_net_guest_offloads_by_features(vdev->guest_features); 594644c9858SDmitry Fleytman } 595644c9858SDmitry Fleytman 596d5aaa1b0SGerd Hoffmann static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features) 5976e790746SPaolo Bonzini { 59817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 5996e790746SPaolo Bonzini int i; 6006e790746SPaolo Bonzini 601ef546f12SCornelia Huck virtio_net_set_multiqueue(n, 60295129d6fSCornelia Huck virtio_has_feature(features, VIRTIO_NET_F_MQ)); 6036e790746SPaolo Bonzini 604ef546f12SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 60595129d6fSCornelia Huck virtio_has_feature(features, 606bb9d17f8SCornelia Huck VIRTIO_NET_F_MRG_RXBUF), 60795129d6fSCornelia Huck virtio_has_feature(features, 608bb9d17f8SCornelia Huck VIRTIO_F_VERSION_1)); 6096e790746SPaolo Bonzini 6106e790746SPaolo Bonzini if (n->has_vnet_hdr) { 611644c9858SDmitry Fleytman n->curr_guest_offloads = 612644c9858SDmitry Fleytman virtio_net_guest_offloads_by_features(features); 613644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 6146e790746SPaolo Bonzini } 6156e790746SPaolo Bonzini 6166e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 6176e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 6186e790746SPaolo Bonzini 619ed8b4afeSNikolay Nikolaev if (!get_vhost_net(nc->peer)) { 6206e790746SPaolo Bonzini continue; 6216e790746SPaolo Bonzini } 622ed8b4afeSNikolay Nikolaev vhost_net_ack_features(get_vhost_net(nc->peer), features); 6236e790746SPaolo Bonzini } 6240b1eaa88SStefan Fritsch 62595129d6fSCornelia Huck if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) { 6260b1eaa88SStefan Fritsch memset(n->vlans, 0, MAX_VLAN >> 3); 6270b1eaa88SStefan Fritsch } else { 6280b1eaa88SStefan Fritsch memset(n->vlans, 0xff, MAX_VLAN >> 3); 6290b1eaa88SStefan Fritsch } 6306e790746SPaolo Bonzini } 6316e790746SPaolo Bonzini 6326e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 6336e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 6346e790746SPaolo Bonzini { 6356e790746SPaolo Bonzini uint8_t on; 6366e790746SPaolo Bonzini size_t s; 637b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 6386e790746SPaolo Bonzini 6396e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 6406e790746SPaolo Bonzini if (s != sizeof(on)) { 6416e790746SPaolo Bonzini return VIRTIO_NET_ERR; 6426e790746SPaolo Bonzini } 6436e790746SPaolo Bonzini 6446e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 6456e790746SPaolo Bonzini n->promisc = on; 6466e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 6476e790746SPaolo Bonzini n->allmulti = on; 6486e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 6496e790746SPaolo Bonzini n->alluni = on; 6506e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 6516e790746SPaolo Bonzini n->nomulti = on; 6526e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 6536e790746SPaolo Bonzini n->nouni = on; 6546e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 6556e790746SPaolo Bonzini n->nobcast = on; 6566e790746SPaolo Bonzini } else { 6576e790746SPaolo Bonzini return VIRTIO_NET_ERR; 6586e790746SPaolo Bonzini } 6596e790746SPaolo Bonzini 660b1be4280SAmos Kong rxfilter_notify(nc); 661b1be4280SAmos Kong 6626e790746SPaolo Bonzini return VIRTIO_NET_OK; 6636e790746SPaolo Bonzini } 6646e790746SPaolo Bonzini 665644c9858SDmitry Fleytman static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, 666644c9858SDmitry Fleytman struct iovec *iov, unsigned int iov_cnt) 667644c9858SDmitry Fleytman { 668644c9858SDmitry Fleytman VirtIODevice *vdev = VIRTIO_DEVICE(n); 669644c9858SDmitry Fleytman uint64_t offloads; 670644c9858SDmitry Fleytman size_t s; 671644c9858SDmitry Fleytman 67295129d6fSCornelia Huck if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 673644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 674644c9858SDmitry Fleytman } 675644c9858SDmitry Fleytman 676644c9858SDmitry Fleytman s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); 677644c9858SDmitry Fleytman if (s != sizeof(offloads)) { 678644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 679644c9858SDmitry Fleytman } 680644c9858SDmitry Fleytman 681644c9858SDmitry Fleytman if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { 682644c9858SDmitry Fleytman uint64_t supported_offloads; 683644c9858SDmitry Fleytman 684644c9858SDmitry Fleytman if (!n->has_vnet_hdr) { 685644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 686644c9858SDmitry Fleytman } 687644c9858SDmitry Fleytman 688644c9858SDmitry Fleytman supported_offloads = virtio_net_supported_guest_offloads(n); 689644c9858SDmitry Fleytman if (offloads & ~supported_offloads) { 690644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 691644c9858SDmitry Fleytman } 692644c9858SDmitry Fleytman 693644c9858SDmitry Fleytman n->curr_guest_offloads = offloads; 694644c9858SDmitry Fleytman virtio_net_apply_guest_offloads(n); 695644c9858SDmitry Fleytman 696644c9858SDmitry Fleytman return VIRTIO_NET_OK; 697644c9858SDmitry Fleytman } else { 698644c9858SDmitry Fleytman return VIRTIO_NET_ERR; 699644c9858SDmitry Fleytman } 700644c9858SDmitry Fleytman } 701644c9858SDmitry Fleytman 7026e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 7036e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 7046e790746SPaolo Bonzini { 7051399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 7066e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 7076e790746SPaolo Bonzini size_t s; 708b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 7096e790746SPaolo Bonzini 7106e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 7116e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 7126e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7136e790746SPaolo Bonzini } 7146e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 7156e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 7166e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 717b1be4280SAmos Kong rxfilter_notify(nc); 718b1be4280SAmos Kong 7196e790746SPaolo Bonzini return VIRTIO_NET_OK; 7206e790746SPaolo Bonzini } 7216e790746SPaolo Bonzini 7226e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 7236e790746SPaolo Bonzini return VIRTIO_NET_ERR; 7246e790746SPaolo Bonzini } 7256e790746SPaolo Bonzini 726cae2e556SAmos Kong int in_use = 0; 727cae2e556SAmos Kong int first_multi = 0; 728cae2e556SAmos Kong uint8_t uni_overflow = 0; 729cae2e556SAmos Kong uint8_t multi_overflow = 0; 730cae2e556SAmos Kong uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 7316e790746SPaolo Bonzini 7326e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 7336e790746SPaolo Bonzini sizeof(mac_data.entries)); 7341399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 7356e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 736b1be4280SAmos Kong goto error; 7376e790746SPaolo Bonzini } 7386e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 7396e790746SPaolo Bonzini 7406e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 741b1be4280SAmos Kong goto error; 7426e790746SPaolo Bonzini } 7436e790746SPaolo Bonzini 7446e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 745cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, macs, 7466e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 7476e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 748b1be4280SAmos Kong goto error; 7496e790746SPaolo Bonzini } 750cae2e556SAmos Kong in_use += mac_data.entries; 7516e790746SPaolo Bonzini } else { 752cae2e556SAmos Kong uni_overflow = 1; 7536e790746SPaolo Bonzini } 7546e790746SPaolo Bonzini 7556e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 7566e790746SPaolo Bonzini 757cae2e556SAmos Kong first_multi = in_use; 7586e790746SPaolo Bonzini 7596e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 7606e790746SPaolo Bonzini sizeof(mac_data.entries)); 7611399c60dSRusty Russell mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries); 7626e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 763b1be4280SAmos Kong goto error; 7646e790746SPaolo Bonzini } 7656e790746SPaolo Bonzini 7666e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 7676e790746SPaolo Bonzini 7686e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 769b1be4280SAmos Kong goto error; 7706e790746SPaolo Bonzini } 7716e790746SPaolo Bonzini 772edc24385SMichael S. Tsirkin if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) { 773cae2e556SAmos Kong s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN], 7746e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 7756e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 776b1be4280SAmos Kong goto error; 7776e790746SPaolo Bonzini } 778cae2e556SAmos Kong in_use += mac_data.entries; 7796e790746SPaolo Bonzini } else { 780cae2e556SAmos Kong multi_overflow = 1; 7816e790746SPaolo Bonzini } 7826e790746SPaolo Bonzini 783cae2e556SAmos Kong n->mac_table.in_use = in_use; 784cae2e556SAmos Kong n->mac_table.first_multi = first_multi; 785cae2e556SAmos Kong n->mac_table.uni_overflow = uni_overflow; 786cae2e556SAmos Kong n->mac_table.multi_overflow = multi_overflow; 787cae2e556SAmos Kong memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN); 788cae2e556SAmos Kong g_free(macs); 789b1be4280SAmos Kong rxfilter_notify(nc); 790b1be4280SAmos Kong 7916e790746SPaolo Bonzini return VIRTIO_NET_OK; 792b1be4280SAmos Kong 793b1be4280SAmos Kong error: 794cae2e556SAmos Kong g_free(macs); 795b1be4280SAmos Kong return VIRTIO_NET_ERR; 7966e790746SPaolo Bonzini } 7976e790746SPaolo Bonzini 7986e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 7996e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 8006e790746SPaolo Bonzini { 8011399c60dSRusty Russell VirtIODevice *vdev = VIRTIO_DEVICE(n); 8026e790746SPaolo Bonzini uint16_t vid; 8036e790746SPaolo Bonzini size_t s; 804b1be4280SAmos Kong NetClientState *nc = qemu_get_queue(n->nic); 8056e790746SPaolo Bonzini 8066e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 8071399c60dSRusty Russell vid = virtio_lduw_p(vdev, &vid); 8086e790746SPaolo Bonzini if (s != sizeof(vid)) { 8096e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8106e790746SPaolo Bonzini } 8116e790746SPaolo Bonzini 8126e790746SPaolo Bonzini if (vid >= MAX_VLAN) 8136e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8146e790746SPaolo Bonzini 8156e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 8166e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 8176e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 8186e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 8196e790746SPaolo Bonzini else 8206e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8216e790746SPaolo Bonzini 822b1be4280SAmos Kong rxfilter_notify(nc); 823b1be4280SAmos Kong 8246e790746SPaolo Bonzini return VIRTIO_NET_OK; 8256e790746SPaolo Bonzini } 8266e790746SPaolo Bonzini 827f57fcf70SJason Wang static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd, 828f57fcf70SJason Wang struct iovec *iov, unsigned int iov_cnt) 829f57fcf70SJason Wang { 830f57fcf70SJason Wang if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK && 831f57fcf70SJason Wang n->status & VIRTIO_NET_S_ANNOUNCE) { 832f57fcf70SJason Wang n->status &= ~VIRTIO_NET_S_ANNOUNCE; 833f57fcf70SJason Wang if (n->announce_counter) { 834f57fcf70SJason Wang timer_mod(n->announce_timer, 835f57fcf70SJason Wang qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 836f57fcf70SJason Wang self_announce_delay(n->announce_counter)); 837f57fcf70SJason Wang } 838f57fcf70SJason Wang return VIRTIO_NET_OK; 839f57fcf70SJason Wang } else { 840f57fcf70SJason Wang return VIRTIO_NET_ERR; 841f57fcf70SJason Wang } 842f57fcf70SJason Wang } 843f57fcf70SJason Wang 8446e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 8456e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 8466e790746SPaolo Bonzini { 84717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 8486e790746SPaolo Bonzini struct virtio_net_ctrl_mq mq; 8496e790746SPaolo Bonzini size_t s; 8506e790746SPaolo Bonzini uint16_t queues; 8516e790746SPaolo Bonzini 8526e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 8536e790746SPaolo Bonzini if (s != sizeof(mq)) { 8546e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8556e790746SPaolo Bonzini } 8566e790746SPaolo Bonzini 8576e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 8586e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8596e790746SPaolo Bonzini } 8606e790746SPaolo Bonzini 8611399c60dSRusty Russell queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs); 8626e790746SPaolo Bonzini 8636e790746SPaolo Bonzini if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 8646e790746SPaolo Bonzini queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 8656e790746SPaolo Bonzini queues > n->max_queues || 8666e790746SPaolo Bonzini !n->multiqueue) { 8676e790746SPaolo Bonzini return VIRTIO_NET_ERR; 8686e790746SPaolo Bonzini } 8696e790746SPaolo Bonzini 8706e790746SPaolo Bonzini n->curr_queues = queues; 8716e790746SPaolo Bonzini /* stop the backend before changing the number of queues to avoid handling a 8726e790746SPaolo Bonzini * disabled queue */ 87317a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 8746e790746SPaolo Bonzini virtio_net_set_queues(n); 8756e790746SPaolo Bonzini 8766e790746SPaolo Bonzini return VIRTIO_NET_OK; 8776e790746SPaolo Bonzini } 8786e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 8796e790746SPaolo Bonzini { 88017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 8816e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 8826e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 88351b19ebeSPaolo Bonzini VirtQueueElement *elem; 8846e790746SPaolo Bonzini size_t s; 885771b6ed3SJason Wang struct iovec *iov, *iov2; 8866e790746SPaolo Bonzini unsigned int iov_cnt; 8876e790746SPaolo Bonzini 88851b19ebeSPaolo Bonzini for (;;) { 88951b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 89051b19ebeSPaolo Bonzini if (!elem) { 89151b19ebeSPaolo Bonzini break; 89251b19ebeSPaolo Bonzini } 89351b19ebeSPaolo Bonzini if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) || 89451b19ebeSPaolo Bonzini iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) { 8956e790746SPaolo Bonzini error_report("virtio-net ctrl missing headers"); 8966e790746SPaolo Bonzini exit(1); 8976e790746SPaolo Bonzini } 8986e790746SPaolo Bonzini 89951b19ebeSPaolo Bonzini iov_cnt = elem->out_num; 90051b19ebeSPaolo Bonzini iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num); 9016e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 9026e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 9036e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 9046e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 9056e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 9066e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 9076e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 9086e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 9096e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 9106e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 911f57fcf70SJason Wang } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) { 912f57fcf70SJason Wang status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt); 9136e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 9146e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 915644c9858SDmitry Fleytman } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { 916644c9858SDmitry Fleytman status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); 9176e790746SPaolo Bonzini } 9186e790746SPaolo Bonzini 91951b19ebeSPaolo Bonzini s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status)); 9206e790746SPaolo Bonzini assert(s == sizeof(status)); 9216e790746SPaolo Bonzini 92251b19ebeSPaolo Bonzini virtqueue_push(vq, elem, sizeof(status)); 9236e790746SPaolo Bonzini virtio_notify(vdev, vq); 924771b6ed3SJason Wang g_free(iov2); 92551b19ebeSPaolo Bonzini g_free(elem); 9266e790746SPaolo Bonzini } 9276e790746SPaolo Bonzini } 9286e790746SPaolo Bonzini 9296e790746SPaolo Bonzini /* RX */ 9306e790746SPaolo Bonzini 9316e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 9326e790746SPaolo Bonzini { 93317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9346e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 9356e790746SPaolo Bonzini 9366e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 9376e790746SPaolo Bonzini } 9386e790746SPaolo Bonzini 9396e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc) 9406e790746SPaolo Bonzini { 9416e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 94217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 9436e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 9446e790746SPaolo Bonzini 94517a0ca55SKONRAD Frederic if (!vdev->vm_running) { 9466e790746SPaolo Bonzini return 0; 9476e790746SPaolo Bonzini } 9486e790746SPaolo Bonzini 9496e790746SPaolo Bonzini if (nc->queue_index >= n->curr_queues) { 9506e790746SPaolo Bonzini return 0; 9516e790746SPaolo Bonzini } 9526e790746SPaolo Bonzini 9536e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 95417a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 9556e790746SPaolo Bonzini return 0; 9566e790746SPaolo Bonzini } 9576e790746SPaolo Bonzini 9586e790746SPaolo Bonzini return 1; 9596e790746SPaolo Bonzini } 9606e790746SPaolo Bonzini 9616e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 9626e790746SPaolo Bonzini { 9636e790746SPaolo Bonzini VirtIONet *n = q->n; 9646e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 9656e790746SPaolo Bonzini (n->mergeable_rx_bufs && 9666e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 9676e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 9686e790746SPaolo Bonzini 9696e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 9706e790746SPaolo Bonzini * available after the above check but before notification was 9716e790746SPaolo Bonzini * enabled, check for available buffers again. 9726e790746SPaolo Bonzini */ 9736e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 9746e790746SPaolo Bonzini (n->mergeable_rx_bufs && 9756e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 9766e790746SPaolo Bonzini return 0; 9776e790746SPaolo Bonzini } 9786e790746SPaolo Bonzini } 9796e790746SPaolo Bonzini 9806e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 9816e790746SPaolo Bonzini return 1; 9826e790746SPaolo Bonzini } 9836e790746SPaolo Bonzini 9841399c60dSRusty Russell static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr) 985032a74a1SCédric Le Goater { 9861399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->hdr_len); 9871399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->gso_size); 9881399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_start); 9891399c60dSRusty Russell virtio_tswap16s(vdev, &hdr->csum_offset); 990032a74a1SCédric Le Goater } 991032a74a1SCédric Le Goater 9926e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 9936e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 9946e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 9956e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 9966e790746SPaolo Bonzini * dhclient yet. 9976e790746SPaolo Bonzini * 9986e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 9996e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 10006e790746SPaolo Bonzini * kernels. 10016e790746SPaolo Bonzini * 10026e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 10036e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 10046e790746SPaolo Bonzini * cache. 10056e790746SPaolo Bonzini */ 10066e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 10076e790746SPaolo Bonzini uint8_t *buf, size_t size) 10086e790746SPaolo Bonzini { 10096e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 10106e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 10116e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 10126e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 10136e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 10146e790746SPaolo Bonzini net_checksum_calculate(buf, size); 10156e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 10166e790746SPaolo Bonzini } 10176e790746SPaolo Bonzini } 10186e790746SPaolo Bonzini 10196e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 10206e790746SPaolo Bonzini const void *buf, size_t size) 10216e790746SPaolo Bonzini { 10226e790746SPaolo Bonzini if (n->has_vnet_hdr) { 10236e790746SPaolo Bonzini /* FIXME this cast is evil */ 10246e790746SPaolo Bonzini void *wbuf = (void *)buf; 10256e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 10266e790746SPaolo Bonzini size - n->host_hdr_len); 10271bfa316cSGreg Kurz 10281bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 10291399c60dSRusty Russell virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf); 10301bfa316cSGreg Kurz } 10316e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 10326e790746SPaolo Bonzini } else { 10336e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 10346e790746SPaolo Bonzini .flags = 0, 10356e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 10366e790746SPaolo Bonzini }; 10376e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 10386e790746SPaolo Bonzini } 10396e790746SPaolo Bonzini } 10406e790746SPaolo Bonzini 10416e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 10426e790746SPaolo Bonzini { 10436e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 10446e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 10456e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 10466e790746SPaolo Bonzini int i; 10476e790746SPaolo Bonzini 10486e790746SPaolo Bonzini if (n->promisc) 10496e790746SPaolo Bonzini return 1; 10506e790746SPaolo Bonzini 10516e790746SPaolo Bonzini ptr += n->host_hdr_len; 10526e790746SPaolo Bonzini 10536e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 10547542d3e7SPeter Maydell int vid = lduw_be_p(ptr + 14) & 0xfff; 10556e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 10566e790746SPaolo Bonzini return 0; 10576e790746SPaolo Bonzini } 10586e790746SPaolo Bonzini 10596e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 10606e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 10616e790746SPaolo Bonzini return !n->nobcast; 10626e790746SPaolo Bonzini } else if (n->nomulti) { 10636e790746SPaolo Bonzini return 0; 10646e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 10656e790746SPaolo Bonzini return 1; 10666e790746SPaolo Bonzini } 10676e790746SPaolo Bonzini 10686e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 10696e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 10706e790746SPaolo Bonzini return 1; 10716e790746SPaolo Bonzini } 10726e790746SPaolo Bonzini } 10736e790746SPaolo Bonzini } else { // unicast 10746e790746SPaolo Bonzini if (n->nouni) { 10756e790746SPaolo Bonzini return 0; 10766e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 10776e790746SPaolo Bonzini return 1; 10786e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 10796e790746SPaolo Bonzini return 1; 10806e790746SPaolo Bonzini } 10816e790746SPaolo Bonzini 10826e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 10836e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 10846e790746SPaolo Bonzini return 1; 10856e790746SPaolo Bonzini } 10866e790746SPaolo Bonzini } 10876e790746SPaolo Bonzini } 10886e790746SPaolo Bonzini 10896e790746SPaolo Bonzini return 0; 10906e790746SPaolo Bonzini } 10916e790746SPaolo Bonzini 10926e790746SPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size) 10936e790746SPaolo Bonzini { 10946e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 10956e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 109617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 10976e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 10986e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 10996e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 11006e790746SPaolo Bonzini size_t offset, i, guest_offset; 11016e790746SPaolo Bonzini 11026e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 11036e790746SPaolo Bonzini return -1; 11046e790746SPaolo Bonzini } 11056e790746SPaolo Bonzini 11066e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 11076e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 11086e790746SPaolo Bonzini return 0; 11096e790746SPaolo Bonzini } 11106e790746SPaolo Bonzini 11116e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 11126e790746SPaolo Bonzini return size; 11136e790746SPaolo Bonzini 11146e790746SPaolo Bonzini offset = i = 0; 11156e790746SPaolo Bonzini 11166e790746SPaolo Bonzini while (offset < size) { 111751b19ebeSPaolo Bonzini VirtQueueElement *elem; 11186e790746SPaolo Bonzini int len, total; 111951b19ebeSPaolo Bonzini const struct iovec *sg; 11206e790746SPaolo Bonzini 11216e790746SPaolo Bonzini total = 0; 11226e790746SPaolo Bonzini 112351b19ebeSPaolo Bonzini elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement)); 112451b19ebeSPaolo Bonzini if (!elem) { 11256e790746SPaolo Bonzini if (i == 0) 11266e790746SPaolo Bonzini return -1; 11276e790746SPaolo Bonzini error_report("virtio-net unexpected empty queue: " 11286e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 1129019a3edbSGerd Hoffmann "guest hdr len %zd, host hdr len %zd " 1130019a3edbSGerd Hoffmann "guest features 0x%" PRIx64, 11316e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 1132019a3edbSGerd Hoffmann n->guest_hdr_len, n->host_hdr_len, 1133019a3edbSGerd Hoffmann vdev->guest_features); 11346e790746SPaolo Bonzini exit(1); 11356e790746SPaolo Bonzini } 11366e790746SPaolo Bonzini 113751b19ebeSPaolo Bonzini if (elem->in_num < 1) { 11386e790746SPaolo Bonzini error_report("virtio-net receive queue contains no in buffers"); 11396e790746SPaolo Bonzini exit(1); 11406e790746SPaolo Bonzini } 11416e790746SPaolo Bonzini 114251b19ebeSPaolo Bonzini sg = elem->in_sg; 11436e790746SPaolo Bonzini if (i == 0) { 11446e790746SPaolo Bonzini assert(offset == 0); 11456e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 11466e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 114751b19ebeSPaolo Bonzini sg, elem->in_num, 11486e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 11496e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 11506e790746SPaolo Bonzini } 11516e790746SPaolo Bonzini 115251b19ebeSPaolo Bonzini receive_header(n, sg, elem->in_num, buf, size); 11536e790746SPaolo Bonzini offset = n->host_hdr_len; 11546e790746SPaolo Bonzini total += n->guest_hdr_len; 11556e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 11566e790746SPaolo Bonzini } else { 11576e790746SPaolo Bonzini guest_offset = 0; 11586e790746SPaolo Bonzini } 11596e790746SPaolo Bonzini 11606e790746SPaolo Bonzini /* copy in packet. ugh */ 116151b19ebeSPaolo Bonzini len = iov_from_buf(sg, elem->in_num, guest_offset, 11626e790746SPaolo Bonzini buf + offset, size - offset); 11636e790746SPaolo Bonzini total += len; 11646e790746SPaolo Bonzini offset += len; 11656e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 11666e790746SPaolo Bonzini * must have consumed the complete packet. 11676e790746SPaolo Bonzini * Otherwise, drop it. */ 11686e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 116951b19ebeSPaolo Bonzini virtqueue_discard(q->rx_vq, elem, total); 117051b19ebeSPaolo Bonzini g_free(elem); 11716e790746SPaolo Bonzini return size; 11726e790746SPaolo Bonzini } 11736e790746SPaolo Bonzini 11746e790746SPaolo Bonzini /* signal other side */ 117551b19ebeSPaolo Bonzini virtqueue_fill(q->rx_vq, elem, total, i++); 117651b19ebeSPaolo Bonzini g_free(elem); 11776e790746SPaolo Bonzini } 11786e790746SPaolo Bonzini 11796e790746SPaolo Bonzini if (mhdr_cnt) { 11801399c60dSRusty Russell virtio_stw_p(vdev, &mhdr.num_buffers, i); 11816e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 11826e790746SPaolo Bonzini 0, 11836e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 11846e790746SPaolo Bonzini } 11856e790746SPaolo Bonzini 11866e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 118717a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 11886e790746SPaolo Bonzini 11896e790746SPaolo Bonzini return size; 11906e790746SPaolo Bonzini } 11916e790746SPaolo Bonzini 11926e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 11936e790746SPaolo Bonzini 11946e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 11956e790746SPaolo Bonzini { 11966e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 11976e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 119817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 11996e790746SPaolo Bonzini 120051b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, q->async_tx.elem, 0); 120117a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 12026e790746SPaolo Bonzini 120351b19ebeSPaolo Bonzini g_free(q->async_tx.elem); 120451b19ebeSPaolo Bonzini q->async_tx.elem = NULL; 12056e790746SPaolo Bonzini 12066e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 12076e790746SPaolo Bonzini virtio_net_flush_tx(q); 12086e790746SPaolo Bonzini } 12096e790746SPaolo Bonzini 12106e790746SPaolo Bonzini /* TX */ 12116e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 12126e790746SPaolo Bonzini { 12136e790746SPaolo Bonzini VirtIONet *n = q->n; 121417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 121551b19ebeSPaolo Bonzini VirtQueueElement *elem; 12166e790746SPaolo Bonzini int32_t num_packets = 0; 12176e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 121817a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 12196e790746SPaolo Bonzini return num_packets; 12206e790746SPaolo Bonzini } 12216e790746SPaolo Bonzini 122251b19ebeSPaolo Bonzini if (q->async_tx.elem) { 12236e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 12246e790746SPaolo Bonzini return num_packets; 12256e790746SPaolo Bonzini } 12266e790746SPaolo Bonzini 122751b19ebeSPaolo Bonzini for (;;) { 1228bd89dd98SJason Wang ssize_t ret; 122951b19ebeSPaolo Bonzini unsigned int out_num; 123051b19ebeSPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg; 1231feb93f36SJason Wang struct virtio_net_hdr_mrg_rxbuf mhdr; 12326e790746SPaolo Bonzini 123351b19ebeSPaolo Bonzini elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement)); 123451b19ebeSPaolo Bonzini if (!elem) { 123551b19ebeSPaolo Bonzini break; 123651b19ebeSPaolo Bonzini } 123751b19ebeSPaolo Bonzini 123851b19ebeSPaolo Bonzini out_num = elem->out_num; 123951b19ebeSPaolo Bonzini out_sg = elem->out_sg; 12406e790746SPaolo Bonzini if (out_num < 1) { 12416e790746SPaolo Bonzini error_report("virtio-net header not in first element"); 12426e790746SPaolo Bonzini exit(1); 12436e790746SPaolo Bonzini } 12446e790746SPaolo Bonzini 1245032a74a1SCédric Le Goater if (n->has_vnet_hdr) { 1246feb93f36SJason Wang if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < 1247feb93f36SJason Wang n->guest_hdr_len) { 1248032a74a1SCédric Le Goater error_report("virtio-net header incorrect"); 1249032a74a1SCédric Le Goater exit(1); 1250032a74a1SCédric Le Goater } 12511bfa316cSGreg Kurz if (n->needs_vnet_hdr_swap) { 1252feb93f36SJason Wang virtio_net_hdr_swap(vdev, (void *) &mhdr); 1253feb93f36SJason Wang sg2[0].iov_base = &mhdr; 1254feb93f36SJason Wang sg2[0].iov_len = n->guest_hdr_len; 1255feb93f36SJason Wang out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, 1256feb93f36SJason Wang out_sg, out_num, 1257feb93f36SJason Wang n->guest_hdr_len, -1); 1258feb93f36SJason Wang if (out_num == VIRTQUEUE_MAX_SIZE) { 1259feb93f36SJason Wang goto drop; 1260032a74a1SCédric Le Goater } 1261feb93f36SJason Wang out_num += 1; 1262feb93f36SJason Wang out_sg = sg2; 1263feb93f36SJason Wang } 1264feb93f36SJason Wang } 12656e790746SPaolo Bonzini /* 12666e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 12676e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 12686e790746SPaolo Bonzini * that host is interested in. 12696e790746SPaolo Bonzini */ 12706e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 12716e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 12726e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 12736e790746SPaolo Bonzini out_sg, out_num, 12746e790746SPaolo Bonzini 0, n->host_hdr_len); 12756e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 12766e790746SPaolo Bonzini out_sg, out_num, 12776e790746SPaolo Bonzini n->guest_hdr_len, -1); 12786e790746SPaolo Bonzini out_num = sg_num; 12796e790746SPaolo Bonzini out_sg = sg; 12806e790746SPaolo Bonzini } 12816e790746SPaolo Bonzini 12826e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 12836e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 12846e790746SPaolo Bonzini if (ret == 0) { 12856e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 12866e790746SPaolo Bonzini q->async_tx.elem = elem; 12876e790746SPaolo Bonzini return -EBUSY; 12886e790746SPaolo Bonzini } 12896e790746SPaolo Bonzini 1290feb93f36SJason Wang drop: 129151b19ebeSPaolo Bonzini virtqueue_push(q->tx_vq, elem, 0); 129217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 129351b19ebeSPaolo Bonzini g_free(elem); 12946e790746SPaolo Bonzini 12956e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 12966e790746SPaolo Bonzini break; 12976e790746SPaolo Bonzini } 12986e790746SPaolo Bonzini } 12996e790746SPaolo Bonzini return num_packets; 13006e790746SPaolo Bonzini } 13016e790746SPaolo Bonzini 13026e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 13036e790746SPaolo Bonzini { 130417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 13056e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 13066e790746SPaolo Bonzini 13076e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 130817a0ca55SKONRAD Frederic if (!vdev->vm_running) { 13096e790746SPaolo Bonzini q->tx_waiting = 1; 13106e790746SPaolo Bonzini return; 13116e790746SPaolo Bonzini } 13126e790746SPaolo Bonzini 13136e790746SPaolo Bonzini if (q->tx_waiting) { 13146e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 1315bc72ad67SAlex Bligh timer_del(q->tx_timer); 13166e790746SPaolo Bonzini q->tx_waiting = 0; 13176e790746SPaolo Bonzini virtio_net_flush_tx(q); 13186e790746SPaolo Bonzini } else { 1319bc72ad67SAlex Bligh timer_mod(q->tx_timer, 1320bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); 13216e790746SPaolo Bonzini q->tx_waiting = 1; 13226e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 13236e790746SPaolo Bonzini } 13246e790746SPaolo Bonzini } 13256e790746SPaolo Bonzini 13266e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 13276e790746SPaolo Bonzini { 132817a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 13296e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 13306e790746SPaolo Bonzini 13316e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 13326e790746SPaolo Bonzini return; 13336e790746SPaolo Bonzini } 13346e790746SPaolo Bonzini q->tx_waiting = 1; 13356e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 133617a0ca55SKONRAD Frederic if (!vdev->vm_running) { 13376e790746SPaolo Bonzini return; 13386e790746SPaolo Bonzini } 13396e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 13406e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 13416e790746SPaolo Bonzini } 13426e790746SPaolo Bonzini 13436e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 13446e790746SPaolo Bonzini { 13456e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 13466e790746SPaolo Bonzini VirtIONet *n = q->n; 134717a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1348e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1349e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1350e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1351e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1352e8bcf842SMichael S. Tsirkin return; 1353e8bcf842SMichael S. Tsirkin } 13546e790746SPaolo Bonzini 13556e790746SPaolo Bonzini q->tx_waiting = 0; 13566e790746SPaolo Bonzini 13576e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 135817a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 13596e790746SPaolo Bonzini return; 136017a0ca55SKONRAD Frederic } 13616e790746SPaolo Bonzini 13626e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 13636e790746SPaolo Bonzini virtio_net_flush_tx(q); 13646e790746SPaolo Bonzini } 13656e790746SPaolo Bonzini 13666e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 13676e790746SPaolo Bonzini { 13686e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 13696e790746SPaolo Bonzini VirtIONet *n = q->n; 137017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 13716e790746SPaolo Bonzini int32_t ret; 13726e790746SPaolo Bonzini 1373e8bcf842SMichael S. Tsirkin /* This happens when device was stopped but BH wasn't. */ 1374e8bcf842SMichael S. Tsirkin if (!vdev->vm_running) { 1375e8bcf842SMichael S. Tsirkin /* Make sure tx waiting is set, so we'll run when restarted. */ 1376e8bcf842SMichael S. Tsirkin assert(q->tx_waiting); 1377e8bcf842SMichael S. Tsirkin return; 1378e8bcf842SMichael S. Tsirkin } 13796e790746SPaolo Bonzini 13806e790746SPaolo Bonzini q->tx_waiting = 0; 13816e790746SPaolo Bonzini 13826e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 138317a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 13846e790746SPaolo Bonzini return; 138517a0ca55SKONRAD Frederic } 13866e790746SPaolo Bonzini 13876e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 13886e790746SPaolo Bonzini if (ret == -EBUSY) { 13896e790746SPaolo Bonzini return; /* Notification re-enable handled by tx_complete */ 13906e790746SPaolo Bonzini } 13916e790746SPaolo Bonzini 13926e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 13936e790746SPaolo Bonzini * more coming and immediately reschedule */ 13946e790746SPaolo Bonzini if (ret >= n->tx_burst) { 13956e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 13966e790746SPaolo Bonzini q->tx_waiting = 1; 13976e790746SPaolo Bonzini return; 13986e790746SPaolo Bonzini } 13996e790746SPaolo Bonzini 14006e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 14016e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 14026e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 14036e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 14046e790746SPaolo Bonzini if (virtio_net_flush_tx(q) > 0) { 14056e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 14066e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 14076e790746SPaolo Bonzini q->tx_waiting = 1; 14086e790746SPaolo Bonzini } 14096e790746SPaolo Bonzini } 14106e790746SPaolo Bonzini 1411f9d6dbf0SWen Congyang static void virtio_net_add_queue(VirtIONet *n, int index) 1412f9d6dbf0SWen Congyang { 1413f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1414f9d6dbf0SWen Congyang 1415f9d6dbf0SWen Congyang n->vqs[index].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); 1416f9d6dbf0SWen Congyang if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 1417f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 1418f9d6dbf0SWen Congyang virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer); 1419f9d6dbf0SWen Congyang n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 1420f9d6dbf0SWen Congyang virtio_net_tx_timer, 1421f9d6dbf0SWen Congyang &n->vqs[index]); 1422f9d6dbf0SWen Congyang } else { 1423f9d6dbf0SWen Congyang n->vqs[index].tx_vq = 1424f9d6dbf0SWen Congyang virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh); 1425f9d6dbf0SWen Congyang n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]); 1426f9d6dbf0SWen Congyang } 1427f9d6dbf0SWen Congyang 1428f9d6dbf0SWen Congyang n->vqs[index].tx_waiting = 0; 1429f9d6dbf0SWen Congyang n->vqs[index].n = n; 1430f9d6dbf0SWen Congyang } 1431f9d6dbf0SWen Congyang 1432f9d6dbf0SWen Congyang static void virtio_net_del_queue(VirtIONet *n, int index) 1433f9d6dbf0SWen Congyang { 1434f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1435f9d6dbf0SWen Congyang VirtIONetQueue *q = &n->vqs[index]; 1436f9d6dbf0SWen Congyang NetClientState *nc = qemu_get_subqueue(n->nic, index); 1437f9d6dbf0SWen Congyang 1438f9d6dbf0SWen Congyang qemu_purge_queued_packets(nc); 1439f9d6dbf0SWen Congyang 1440f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2); 1441f9d6dbf0SWen Congyang if (q->tx_timer) { 1442f9d6dbf0SWen Congyang timer_del(q->tx_timer); 1443f9d6dbf0SWen Congyang timer_free(q->tx_timer); 1444f9d6dbf0SWen Congyang } else { 1445f9d6dbf0SWen Congyang qemu_bh_delete(q->tx_bh); 1446f9d6dbf0SWen Congyang } 1447f9d6dbf0SWen Congyang virtio_del_queue(vdev, index * 2 + 1); 1448f9d6dbf0SWen Congyang } 1449f9d6dbf0SWen Congyang 1450f9d6dbf0SWen Congyang static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues) 1451f9d6dbf0SWen Congyang { 1452f9d6dbf0SWen Congyang VirtIODevice *vdev = VIRTIO_DEVICE(n); 1453f9d6dbf0SWen Congyang int old_num_queues = virtio_get_num_queues(vdev); 1454f9d6dbf0SWen Congyang int new_num_queues = new_max_queues * 2 + 1; 1455f9d6dbf0SWen Congyang int i; 1456f9d6dbf0SWen Congyang 1457f9d6dbf0SWen Congyang assert(old_num_queues >= 3); 1458f9d6dbf0SWen Congyang assert(old_num_queues % 2 == 1); 1459f9d6dbf0SWen Congyang 1460f9d6dbf0SWen Congyang if (old_num_queues == new_num_queues) { 1461f9d6dbf0SWen Congyang return; 1462f9d6dbf0SWen Congyang } 1463f9d6dbf0SWen Congyang 1464f9d6dbf0SWen Congyang /* 1465f9d6dbf0SWen Congyang * We always need to remove and add ctrl vq if 1466f9d6dbf0SWen Congyang * old_num_queues != new_num_queues. Remove ctrl_vq first, 1467f9d6dbf0SWen Congyang * and then we only enter one of the following too loops. 1468f9d6dbf0SWen Congyang */ 1469f9d6dbf0SWen Congyang virtio_del_queue(vdev, old_num_queues - 1); 1470f9d6dbf0SWen Congyang 1471f9d6dbf0SWen Congyang for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) { 1472f9d6dbf0SWen Congyang /* new_num_queues < old_num_queues */ 1473f9d6dbf0SWen Congyang virtio_net_del_queue(n, i / 2); 1474f9d6dbf0SWen Congyang } 1475f9d6dbf0SWen Congyang 1476f9d6dbf0SWen Congyang for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) { 1477f9d6dbf0SWen Congyang /* new_num_queues > old_num_queues */ 1478f9d6dbf0SWen Congyang virtio_net_add_queue(n, i / 2); 1479f9d6dbf0SWen Congyang } 1480f9d6dbf0SWen Congyang 1481f9d6dbf0SWen Congyang /* add ctrl_vq last */ 1482f9d6dbf0SWen Congyang n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 1483f9d6dbf0SWen Congyang } 1484f9d6dbf0SWen Congyang 1485ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 14866e790746SPaolo Bonzini { 1487f9d6dbf0SWen Congyang int max = multiqueue ? n->max_queues : 1; 1488f9d6dbf0SWen Congyang 14896e790746SPaolo Bonzini n->multiqueue = multiqueue; 1490f9d6dbf0SWen Congyang virtio_net_change_num_queues(n, max); 14916e790746SPaolo Bonzini 14926e790746SPaolo Bonzini virtio_net_set_queues(n); 14936e790746SPaolo Bonzini } 14946e790746SPaolo Bonzini 14956e790746SPaolo Bonzini static void virtio_net_save(QEMUFile *f, void *opaque) 14966e790746SPaolo Bonzini { 14976e790746SPaolo Bonzini VirtIONet *n = opaque; 149817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 14996e790746SPaolo Bonzini 15006e790746SPaolo Bonzini /* At this point, backend must be stopped, otherwise 15016e790746SPaolo Bonzini * it might keep writing to memory. */ 15026e790746SPaolo Bonzini assert(!n->vhost_started); 150317a0ca55SKONRAD Frederic virtio_save(vdev, f); 1504037dab2fSGreg Kurz } 1505037dab2fSGreg Kurz 1506037dab2fSGreg Kurz static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f) 1507037dab2fSGreg Kurz { 1508037dab2fSGreg Kurz VirtIONet *n = VIRTIO_NET(vdev); 1509037dab2fSGreg Kurz int i; 15106e790746SPaolo Bonzini 15116e790746SPaolo Bonzini qemu_put_buffer(f, n->mac, ETH_ALEN); 15126e790746SPaolo Bonzini qemu_put_be32(f, n->vqs[0].tx_waiting); 15136e790746SPaolo Bonzini qemu_put_be32(f, n->mergeable_rx_bufs); 15146e790746SPaolo Bonzini qemu_put_be16(f, n->status); 15156e790746SPaolo Bonzini qemu_put_byte(f, n->promisc); 15166e790746SPaolo Bonzini qemu_put_byte(f, n->allmulti); 15176e790746SPaolo Bonzini qemu_put_be32(f, n->mac_table.in_use); 15186e790746SPaolo Bonzini qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); 15196e790746SPaolo Bonzini qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); 15206e790746SPaolo Bonzini qemu_put_be32(f, n->has_vnet_hdr); 15216e790746SPaolo Bonzini qemu_put_byte(f, n->mac_table.multi_overflow); 15226e790746SPaolo Bonzini qemu_put_byte(f, n->mac_table.uni_overflow); 15236e790746SPaolo Bonzini qemu_put_byte(f, n->alluni); 15246e790746SPaolo Bonzini qemu_put_byte(f, n->nomulti); 15256e790746SPaolo Bonzini qemu_put_byte(f, n->nouni); 15266e790746SPaolo Bonzini qemu_put_byte(f, n->nobcast); 15276e790746SPaolo Bonzini qemu_put_byte(f, n->has_ufo); 15286e790746SPaolo Bonzini if (n->max_queues > 1) { 15296e790746SPaolo Bonzini qemu_put_be16(f, n->max_queues); 15306e790746SPaolo Bonzini qemu_put_be16(f, n->curr_queues); 15316e790746SPaolo Bonzini for (i = 1; i < n->curr_queues; i++) { 15326e790746SPaolo Bonzini qemu_put_be32(f, n->vqs[i].tx_waiting); 15336e790746SPaolo Bonzini } 15346e790746SPaolo Bonzini } 1535644c9858SDmitry Fleytman 153695129d6fSCornelia Huck if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 1537644c9858SDmitry Fleytman qemu_put_be64(f, n->curr_guest_offloads); 1538644c9858SDmitry Fleytman } 15396e790746SPaolo Bonzini } 15406e790746SPaolo Bonzini 15416e790746SPaolo Bonzini static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) 15426e790746SPaolo Bonzini { 15436e790746SPaolo Bonzini VirtIONet *n = opaque; 154417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 15456e790746SPaolo Bonzini 15466e790746SPaolo Bonzini if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) 15476e790746SPaolo Bonzini return -EINVAL; 15486e790746SPaolo Bonzini 15496c666823SMichael S. Tsirkin return virtio_load(vdev, f, version_id); 15506e790746SPaolo Bonzini } 15516e790746SPaolo Bonzini 1552037dab2fSGreg Kurz static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, 1553037dab2fSGreg Kurz int version_id) 1554037dab2fSGreg Kurz { 1555037dab2fSGreg Kurz VirtIONet *n = VIRTIO_NET(vdev); 1556037dab2fSGreg Kurz int i, link_down; 1557037dab2fSGreg Kurz 15586e790746SPaolo Bonzini qemu_get_buffer(f, n->mac, ETH_ALEN); 15596e790746SPaolo Bonzini n->vqs[0].tx_waiting = qemu_get_be32(f); 15606e790746SPaolo Bonzini 1561bb9d17f8SCornelia Huck virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f), 156295129d6fSCornelia Huck virtio_vdev_has_feature(vdev, 156395129d6fSCornelia Huck VIRTIO_F_VERSION_1)); 15646e790746SPaolo Bonzini 15656e790746SPaolo Bonzini if (version_id >= 3) 15666e790746SPaolo Bonzini n->status = qemu_get_be16(f); 15676e790746SPaolo Bonzini 15686e790746SPaolo Bonzini if (version_id >= 4) { 15696e790746SPaolo Bonzini if (version_id < 8) { 15706e790746SPaolo Bonzini n->promisc = qemu_get_be32(f); 15716e790746SPaolo Bonzini n->allmulti = qemu_get_be32(f); 15726e790746SPaolo Bonzini } else { 15736e790746SPaolo Bonzini n->promisc = qemu_get_byte(f); 15746e790746SPaolo Bonzini n->allmulti = qemu_get_byte(f); 15756e790746SPaolo Bonzini } 15766e790746SPaolo Bonzini } 15776e790746SPaolo Bonzini 15786e790746SPaolo Bonzini if (version_id >= 5) { 15796e790746SPaolo Bonzini n->mac_table.in_use = qemu_get_be32(f); 15806e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 15816e790746SPaolo Bonzini if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { 15826e790746SPaolo Bonzini qemu_get_buffer(f, n->mac_table.macs, 15836e790746SPaolo Bonzini n->mac_table.in_use * ETH_ALEN); 158498f93dddSMichael S. Tsirkin } else { 158598f93dddSMichael S. Tsirkin int64_t i; 158698f93dddSMichael S. Tsirkin 158798f93dddSMichael S. Tsirkin /* Overflow detected - can happen if source has a larger MAC table. 158898f93dddSMichael S. Tsirkin * We simply set overflow flag so there's no need to maintain the 158998f93dddSMichael S. Tsirkin * table of addresses, discard them all. 159098f93dddSMichael S. Tsirkin * Note: 64 bit math to avoid integer overflow. 159198f93dddSMichael S. Tsirkin */ 159298f93dddSMichael S. Tsirkin for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) { 159398f93dddSMichael S. Tsirkin qemu_get_byte(f); 159498f93dddSMichael S. Tsirkin } 15956e790746SPaolo Bonzini n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; 15966e790746SPaolo Bonzini n->mac_table.in_use = 0; 15976e790746SPaolo Bonzini } 15986e790746SPaolo Bonzini } 15996e790746SPaolo Bonzini 16006e790746SPaolo Bonzini if (version_id >= 6) 16016e790746SPaolo Bonzini qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); 16026e790746SPaolo Bonzini 16036e790746SPaolo Bonzini if (version_id >= 7) { 16046e790746SPaolo Bonzini if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { 16056e790746SPaolo Bonzini error_report("virtio-net: saved image requires vnet_hdr=on"); 16066e790746SPaolo Bonzini return -1; 16076e790746SPaolo Bonzini } 16086e790746SPaolo Bonzini } 16096e790746SPaolo Bonzini 16106e790746SPaolo Bonzini if (version_id >= 9) { 16116e790746SPaolo Bonzini n->mac_table.multi_overflow = qemu_get_byte(f); 16126e790746SPaolo Bonzini n->mac_table.uni_overflow = qemu_get_byte(f); 16136e790746SPaolo Bonzini } 16146e790746SPaolo Bonzini 16156e790746SPaolo Bonzini if (version_id >= 10) { 16166e790746SPaolo Bonzini n->alluni = qemu_get_byte(f); 16176e790746SPaolo Bonzini n->nomulti = qemu_get_byte(f); 16186e790746SPaolo Bonzini n->nouni = qemu_get_byte(f); 16196e790746SPaolo Bonzini n->nobcast = qemu_get_byte(f); 16206e790746SPaolo Bonzini } 16216e790746SPaolo Bonzini 16226e790746SPaolo Bonzini if (version_id >= 11) { 16236e790746SPaolo Bonzini if (qemu_get_byte(f) && !peer_has_ufo(n)) { 16246e790746SPaolo Bonzini error_report("virtio-net: saved image requires TUN_F_UFO support"); 16256e790746SPaolo Bonzini return -1; 16266e790746SPaolo Bonzini } 16276e790746SPaolo Bonzini } 16286e790746SPaolo Bonzini 16296e790746SPaolo Bonzini if (n->max_queues > 1) { 16306e790746SPaolo Bonzini if (n->max_queues != qemu_get_be16(f)) { 16316e790746SPaolo Bonzini error_report("virtio-net: different max_queues "); 16326e790746SPaolo Bonzini return -1; 16336e790746SPaolo Bonzini } 16346e790746SPaolo Bonzini 16356e790746SPaolo Bonzini n->curr_queues = qemu_get_be16(f); 1636eea750a5SMichael S. Tsirkin if (n->curr_queues > n->max_queues) { 1637eea750a5SMichael S. Tsirkin error_report("virtio-net: curr_queues %x > max_queues %x", 1638eea750a5SMichael S. Tsirkin n->curr_queues, n->max_queues); 1639eea750a5SMichael S. Tsirkin return -1; 1640eea750a5SMichael S. Tsirkin } 16416e790746SPaolo Bonzini for (i = 1; i < n->curr_queues; i++) { 16426e790746SPaolo Bonzini n->vqs[i].tx_waiting = qemu_get_be32(f); 16436e790746SPaolo Bonzini } 16446e790746SPaolo Bonzini } 16456e790746SPaolo Bonzini 16466c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { 16476c666823SMichael S. Tsirkin n->curr_guest_offloads = qemu_get_be64(f); 16486c666823SMichael S. Tsirkin } else { 16496c666823SMichael S. Tsirkin n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); 16506c666823SMichael S. Tsirkin } 16516c666823SMichael S. Tsirkin 16526c666823SMichael S. Tsirkin if (peer_has_vnet_hdr(n)) { 16536c666823SMichael S. Tsirkin virtio_net_apply_guest_offloads(n); 16546c666823SMichael S. Tsirkin } 16556c666823SMichael S. Tsirkin 16566e790746SPaolo Bonzini virtio_net_set_queues(n); 16576e790746SPaolo Bonzini 16586e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 16596e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 16606e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 16616e790746SPaolo Bonzini break; 16626e790746SPaolo Bonzini } 16636e790746SPaolo Bonzini } 16646e790746SPaolo Bonzini n->mac_table.first_multi = i; 16656e790746SPaolo Bonzini 16666e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 16676e790746SPaolo Bonzini * to link status bit in n->status */ 16686e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 16696e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 16706e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 16716e790746SPaolo Bonzini } 16726e790746SPaolo Bonzini 16736c666823SMichael S. Tsirkin if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) && 16746c666823SMichael S. Tsirkin virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { 16756c666823SMichael S. Tsirkin n->announce_counter = SELF_ANNOUNCE_ROUNDS; 16766c666823SMichael S. Tsirkin timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)); 16776c666823SMichael S. Tsirkin } 16786c666823SMichael S. Tsirkin 16796e790746SPaolo Bonzini return 0; 16806e790746SPaolo Bonzini } 16816e790746SPaolo Bonzini 16826e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 1683*f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 16846e790746SPaolo Bonzini .size = sizeof(NICState), 16856e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 16866e790746SPaolo Bonzini .receive = virtio_net_receive, 16876e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 1688b1be4280SAmos Kong .query_rx_filter = virtio_net_query_rxfilter, 16896e790746SPaolo Bonzini }; 16906e790746SPaolo Bonzini 16916e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 16926e790746SPaolo Bonzini { 169317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 16946e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 16956e790746SPaolo Bonzini assert(n->vhost_started); 1696ed8b4afeSNikolay Nikolaev return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); 16976e790746SPaolo Bonzini } 16986e790746SPaolo Bonzini 16996e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 17006e790746SPaolo Bonzini bool mask) 17016e790746SPaolo Bonzini { 170217a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 17036e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 17046e790746SPaolo Bonzini assert(n->vhost_started); 1705ed8b4afeSNikolay Nikolaev vhost_net_virtqueue_mask(get_vhost_net(nc->peer), 17066e790746SPaolo Bonzini vdev, idx, mask); 17076e790746SPaolo Bonzini } 17086e790746SPaolo Bonzini 1709019a3edbSGerd Hoffmann static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features) 17106e790746SPaolo Bonzini { 17116e790746SPaolo Bonzini int i, config_size = 0; 17120cd09c3aSCornelia Huck virtio_add_feature(&host_features, VIRTIO_NET_F_MAC); 17136e790746SPaolo Bonzini for (i = 0; feature_sizes[i].flags != 0; i++) { 17146e790746SPaolo Bonzini if (host_features & feature_sizes[i].flags) { 17156e790746SPaolo Bonzini config_size = MAX(feature_sizes[i].end, config_size); 17166e790746SPaolo Bonzini } 17176e790746SPaolo Bonzini } 171817ec5a86SKONRAD Frederic n->config_size = config_size; 171917ec5a86SKONRAD Frederic } 17206e790746SPaolo Bonzini 17218a253ec2SKONRAD Frederic void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 17228a253ec2SKONRAD Frederic const char *type) 17238a253ec2SKONRAD Frederic { 17248a253ec2SKONRAD Frederic /* 17258a253ec2SKONRAD Frederic * The name can be NULL, the netclient name will be type.x. 17268a253ec2SKONRAD Frederic */ 17278a253ec2SKONRAD Frederic assert(type != NULL); 17288a253ec2SKONRAD Frederic 17298a253ec2SKONRAD Frederic g_free(n->netclient_name); 17308a253ec2SKONRAD Frederic g_free(n->netclient_type); 17318a253ec2SKONRAD Frederic n->netclient_name = g_strdup(name); 17328a253ec2SKONRAD Frederic n->netclient_type = g_strdup(type); 17338a253ec2SKONRAD Frederic } 17348a253ec2SKONRAD Frederic 1735e6f746b3SAndreas Färber static void virtio_net_device_realize(DeviceState *dev, Error **errp) 173617ec5a86SKONRAD Frederic { 1737e6f746b3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1738284a32f0SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 1739284a32f0SAndreas Färber NetClientState *nc; 17401773d9eeSKONRAD Frederic int i; 174117ec5a86SKONRAD Frederic 1742da3e8a23SShannon Zhao virtio_net_set_config_size(n, n->host_features); 1743284a32f0SAndreas Färber virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size); 174417ec5a86SKONRAD Frederic 1745575a1c0eSJiri Pirko n->max_queues = MAX(n->nic_conf.peers.queues, 1); 174687b3bd1cSJason Wang if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) { 17477e0e736eSJason Wang error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 1748631b22eaSStefan Weil "must be a positive integer less than %d.", 174987b3bd1cSJason Wang n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2); 17507e0e736eSJason Wang virtio_cleanup(vdev); 17517e0e736eSJason Wang return; 17527e0e736eSJason Wang } 17536e790746SPaolo Bonzini n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); 17546e790746SPaolo Bonzini n->curr_queues = 1; 17551773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 17566e790746SPaolo Bonzini 17571773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 17581773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 17596e790746SPaolo Bonzini error_report("virtio-net: " 17606e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 17611773d9eeSKONRAD Frederic n->net_conf.tx); 17626e790746SPaolo Bonzini error_report("Defaulting to \"bh\""); 17636e790746SPaolo Bonzini } 17646e790746SPaolo Bonzini 1765da51a335SJason Wang for (i = 0; i < n->max_queues; i++) { 1766f9d6dbf0SWen Congyang virtio_net_add_queue(n, i); 1767da51a335SJason Wang } 1768da51a335SJason Wang 176917a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 17701773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 17711773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 17726e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 1773f57fcf70SJason Wang n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, 1774f57fcf70SJason Wang virtio_net_announce_timer, n); 17756e790746SPaolo Bonzini 17768a253ec2SKONRAD Frederic if (n->netclient_type) { 17778a253ec2SKONRAD Frederic /* 17788a253ec2SKONRAD Frederic * Happen when virtio_net_set_netclient_name has been called. 17798a253ec2SKONRAD Frederic */ 17808a253ec2SKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 17818a253ec2SKONRAD Frederic n->netclient_type, n->netclient_name, n); 17828a253ec2SKONRAD Frederic } else { 17831773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 1784284a32f0SAndreas Färber object_get_typename(OBJECT(dev)), dev->id, n); 17858a253ec2SKONRAD Frederic } 17868a253ec2SKONRAD Frederic 17876e790746SPaolo Bonzini peer_test_vnet_hdr(n); 17886e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 17896e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 1790d6085e3aSStefan Hajnoczi qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 17916e790746SPaolo Bonzini } 17926e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 17936e790746SPaolo Bonzini } else { 17946e790746SPaolo Bonzini n->host_hdr_len = 0; 17956e790746SPaolo Bonzini } 17966e790746SPaolo Bonzini 17971773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 17986e790746SPaolo Bonzini 17996e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 18001773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 1801bb9d17f8SCornelia Huck virtio_net_set_mrg_rx_bufs(n, 0, 0); 18026e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 18036e790746SPaolo Bonzini 18046e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 18056e790746SPaolo Bonzini 18066e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 18076e790746SPaolo Bonzini 1808b1be4280SAmos Kong nc = qemu_get_queue(n->nic); 1809b1be4280SAmos Kong nc->rxfilter_notify_enabled = 1; 1810b1be4280SAmos Kong 1811284a32f0SAndreas Färber n->qdev = dev; 1812284a32f0SAndreas Färber register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION, 18136e790746SPaolo Bonzini virtio_net_save, virtio_net_load, n); 181417ec5a86SKONRAD Frederic } 181517ec5a86SKONRAD Frederic 1816306ec6c3SAndreas Färber static void virtio_net_device_unrealize(DeviceState *dev, Error **errp) 181717ec5a86SKONRAD Frederic { 1818306ec6c3SAndreas Färber VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1819306ec6c3SAndreas Färber VirtIONet *n = VIRTIO_NET(dev); 1820f9d6dbf0SWen Congyang int i, max_queues; 182117ec5a86SKONRAD Frederic 182217ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 182317ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 182417ec5a86SKONRAD Frederic 1825306ec6c3SAndreas Färber unregister_savevm(dev, "virtio-net", n); 182617ec5a86SKONRAD Frederic 18278a253ec2SKONRAD Frederic g_free(n->netclient_name); 18288a253ec2SKONRAD Frederic n->netclient_name = NULL; 18298a253ec2SKONRAD Frederic g_free(n->netclient_type); 18308a253ec2SKONRAD Frederic n->netclient_type = NULL; 18318a253ec2SKONRAD Frederic 183217ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 183317ec5a86SKONRAD Frederic g_free(n->vlans); 183417ec5a86SKONRAD Frederic 1835f9d6dbf0SWen Congyang max_queues = n->multiqueue ? n->max_queues : 1; 1836f9d6dbf0SWen Congyang for (i = 0; i < max_queues; i++) { 1837f9d6dbf0SWen Congyang virtio_net_del_queue(n, i); 183817ec5a86SKONRAD Frederic } 183917ec5a86SKONRAD Frederic 1840f57fcf70SJason Wang timer_del(n->announce_timer); 1841f57fcf70SJason Wang timer_free(n->announce_timer); 184217ec5a86SKONRAD Frederic g_free(n->vqs); 184317ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 18446a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 184517ec5a86SKONRAD Frederic } 184617ec5a86SKONRAD Frederic 184717ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 184817ec5a86SKONRAD Frederic { 184917ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 185017ec5a86SKONRAD Frederic 185117ec5a86SKONRAD Frederic /* 185217ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 185317ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 185417ec5a86SKONRAD Frederic */ 185517ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 1856aa4197c3SGonglei device_add_bootindex_property(obj, &n->nic_conf.bootindex, 1857aa4197c3SGonglei "bootindex", "/ethernet-phy@0", 1858aa4197c3SGonglei DEVICE(n), NULL); 185917ec5a86SKONRAD Frederic } 186017ec5a86SKONRAD Frederic 186117ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 186287108bb2SShannon Zhao DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true), 186387108bb2SShannon Zhao DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features, 186487108bb2SShannon Zhao VIRTIO_NET_F_GUEST_CSUM, true), 186587108bb2SShannon Zhao DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true), 186687108bb2SShannon Zhao DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features, 186787108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO4, true), 186887108bb2SShannon Zhao DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features, 186987108bb2SShannon Zhao VIRTIO_NET_F_GUEST_TSO6, true), 187087108bb2SShannon Zhao DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features, 187187108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ECN, true), 187287108bb2SShannon Zhao DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features, 187387108bb2SShannon Zhao VIRTIO_NET_F_GUEST_UFO, true), 187487108bb2SShannon Zhao DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features, 187587108bb2SShannon Zhao VIRTIO_NET_F_GUEST_ANNOUNCE, true), 187687108bb2SShannon Zhao DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features, 187787108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO4, true), 187887108bb2SShannon Zhao DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features, 187987108bb2SShannon Zhao VIRTIO_NET_F_HOST_TSO6, true), 188087108bb2SShannon Zhao DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features, 188187108bb2SShannon Zhao VIRTIO_NET_F_HOST_ECN, true), 188287108bb2SShannon Zhao DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features, 188387108bb2SShannon Zhao VIRTIO_NET_F_HOST_UFO, true), 188487108bb2SShannon Zhao DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features, 188587108bb2SShannon Zhao VIRTIO_NET_F_MRG_RXBUF, true), 188687108bb2SShannon Zhao DEFINE_PROP_BIT("status", VirtIONet, host_features, 188787108bb2SShannon Zhao VIRTIO_NET_F_STATUS, true), 188887108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features, 188987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VQ, true), 189087108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features, 189187108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX, true), 189287108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features, 189387108bb2SShannon Zhao VIRTIO_NET_F_CTRL_VLAN, true), 189487108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features, 189587108bb2SShannon Zhao VIRTIO_NET_F_CTRL_RX_EXTRA, true), 189687108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features, 189787108bb2SShannon Zhao VIRTIO_NET_F_CTRL_MAC_ADDR, true), 189887108bb2SShannon Zhao DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features, 189987108bb2SShannon Zhao VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), 190087108bb2SShannon Zhao DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false), 190117ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 190217ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 190317ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 190417ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 190517ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 190617ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 190717ec5a86SKONRAD Frederic }; 190817ec5a86SKONRAD Frederic 190917ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 191017ec5a86SKONRAD Frederic { 191117ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 191217ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 1913e6f746b3SAndreas Färber 191417ec5a86SKONRAD Frederic dc->props = virtio_net_properties; 1915125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1916e6f746b3SAndreas Färber vdc->realize = virtio_net_device_realize; 1917306ec6c3SAndreas Färber vdc->unrealize = virtio_net_device_unrealize; 191817ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 191917ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 192017ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 192117ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 192217ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 192317ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 192417ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 192517ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 192617ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 1927037dab2fSGreg Kurz vdc->load = virtio_net_load_device; 1928037dab2fSGreg Kurz vdc->save = virtio_net_save_device; 192917ec5a86SKONRAD Frederic } 193017ec5a86SKONRAD Frederic 193117ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 193217ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 193317ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 193417ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 193517ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 193617ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 193717ec5a86SKONRAD Frederic }; 193817ec5a86SKONRAD Frederic 193917ec5a86SKONRAD Frederic static void virtio_register_types(void) 194017ec5a86SKONRAD Frederic { 194117ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 194217ec5a86SKONRAD Frederic } 194317ec5a86SKONRAD Frederic 194417ec5a86SKONRAD Frederic type_init(virtio_register_types) 1945