16e790746SPaolo Bonzini /* 26e790746SPaolo Bonzini * Virtio Network Device 36e790746SPaolo Bonzini * 46e790746SPaolo Bonzini * Copyright IBM, Corp. 2007 56e790746SPaolo Bonzini * 66e790746SPaolo Bonzini * Authors: 76e790746SPaolo Bonzini * Anthony Liguori <aliguori@us.ibm.com> 86e790746SPaolo Bonzini * 96e790746SPaolo Bonzini * This work is licensed under the terms of the GNU GPL, version 2. See 106e790746SPaolo Bonzini * the COPYING file in the top-level directory. 116e790746SPaolo Bonzini * 126e790746SPaolo Bonzini */ 136e790746SPaolo Bonzini 146e790746SPaolo Bonzini #include "qemu/iov.h" 156e790746SPaolo Bonzini #include "hw/virtio/virtio.h" 166e790746SPaolo Bonzini #include "net/net.h" 176e790746SPaolo Bonzini #include "net/checksum.h" 186e790746SPaolo Bonzini #include "net/tap.h" 196e790746SPaolo Bonzini #include "qemu/error-report.h" 206e790746SPaolo Bonzini #include "qemu/timer.h" 216e790746SPaolo Bonzini #include "hw/virtio/virtio-net.h" 226e790746SPaolo Bonzini #include "net/vhost_net.h" 2317ec5a86SKONRAD Frederic #include "hw/virtio/virtio-bus.h" 246e790746SPaolo Bonzini 256e790746SPaolo Bonzini #define VIRTIO_NET_VM_VERSION 11 266e790746SPaolo Bonzini 276e790746SPaolo Bonzini #define MAC_TABLE_ENTRIES 64 286e790746SPaolo Bonzini #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 296e790746SPaolo Bonzini 306e790746SPaolo Bonzini /* 316e790746SPaolo Bonzini * Calculate the number of bytes up to and including the given 'field' of 326e790746SPaolo Bonzini * 'container'. 336e790746SPaolo Bonzini */ 346e790746SPaolo Bonzini #define endof(container, field) \ 356e790746SPaolo Bonzini (offsetof(container, field) + sizeof(((container *)0)->field)) 366e790746SPaolo Bonzini 376e790746SPaolo Bonzini typedef struct VirtIOFeature { 386e790746SPaolo Bonzini uint32_t flags; 396e790746SPaolo Bonzini size_t end; 406e790746SPaolo Bonzini } VirtIOFeature; 416e790746SPaolo Bonzini 426e790746SPaolo Bonzini static VirtIOFeature feature_sizes[] = { 436e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MAC, 446e790746SPaolo Bonzini .end = endof(struct virtio_net_config, mac)}, 456e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_STATUS, 466e790746SPaolo Bonzini .end = endof(struct virtio_net_config, status)}, 476e790746SPaolo Bonzini {.flags = 1 << VIRTIO_NET_F_MQ, 486e790746SPaolo Bonzini .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, 496e790746SPaolo Bonzini {} 506e790746SPaolo Bonzini }; 516e790746SPaolo Bonzini 526e790746SPaolo Bonzini static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) 536e790746SPaolo Bonzini { 546e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 556e790746SPaolo Bonzini 566e790746SPaolo Bonzini return &n->vqs[nc->queue_index]; 576e790746SPaolo Bonzini } 586e790746SPaolo Bonzini 596e790746SPaolo Bonzini static int vq2q(int queue_index) 606e790746SPaolo Bonzini { 616e790746SPaolo Bonzini return queue_index / 2; 626e790746SPaolo Bonzini } 636e790746SPaolo Bonzini 646e790746SPaolo Bonzini /* TODO 656e790746SPaolo Bonzini * - we could suppress RX interrupt if we were so inclined. 666e790746SPaolo Bonzini */ 676e790746SPaolo Bonzini 686e790746SPaolo Bonzini static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) 696e790746SPaolo Bonzini { 7017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 716e790746SPaolo Bonzini struct virtio_net_config netcfg; 726e790746SPaolo Bonzini 736e790746SPaolo Bonzini stw_p(&netcfg.status, n->status); 746e790746SPaolo Bonzini stw_p(&netcfg.max_virtqueue_pairs, n->max_queues); 756e790746SPaolo Bonzini memcpy(netcfg.mac, n->mac, ETH_ALEN); 766e790746SPaolo Bonzini memcpy(config, &netcfg, n->config_size); 776e790746SPaolo Bonzini } 786e790746SPaolo Bonzini 796e790746SPaolo Bonzini static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) 806e790746SPaolo Bonzini { 8117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 826e790746SPaolo Bonzini struct virtio_net_config netcfg = {}; 836e790746SPaolo Bonzini 846e790746SPaolo Bonzini memcpy(&netcfg, config, n->config_size); 856e790746SPaolo Bonzini 8617a0ca55SKONRAD Frederic if (!(vdev->guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) && 876e790746SPaolo Bonzini memcmp(netcfg.mac, n->mac, ETH_ALEN)) { 886e790746SPaolo Bonzini memcpy(n->mac, netcfg.mac, ETH_ALEN); 896e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 906e790746SPaolo Bonzini } 916e790746SPaolo Bonzini } 926e790746SPaolo Bonzini 936e790746SPaolo Bonzini static bool virtio_net_started(VirtIONet *n, uint8_t status) 946e790746SPaolo Bonzini { 9517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 966e790746SPaolo Bonzini return (status & VIRTIO_CONFIG_S_DRIVER_OK) && 9717a0ca55SKONRAD Frederic (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running; 986e790746SPaolo Bonzini } 996e790746SPaolo Bonzini 1006e790746SPaolo Bonzini static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) 1016e790746SPaolo Bonzini { 10217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1036e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 1046e790746SPaolo Bonzini int queues = n->multiqueue ? n->max_queues : 1; 1056e790746SPaolo Bonzini 1066e790746SPaolo Bonzini if (!nc->peer) { 1076e790746SPaolo Bonzini return; 1086e790746SPaolo Bonzini } 1096e790746SPaolo Bonzini if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 1106e790746SPaolo Bonzini return; 1116e790746SPaolo Bonzini } 1126e790746SPaolo Bonzini 1136e790746SPaolo Bonzini if (!tap_get_vhost_net(nc->peer)) { 1146e790746SPaolo Bonzini return; 1156e790746SPaolo Bonzini } 1166e790746SPaolo Bonzini 1176e790746SPaolo Bonzini if (!!n->vhost_started == virtio_net_started(n, status) && 1186e790746SPaolo Bonzini !nc->peer->link_down) { 1196e790746SPaolo Bonzini return; 1206e790746SPaolo Bonzini } 1216e790746SPaolo Bonzini if (!n->vhost_started) { 1226e790746SPaolo Bonzini int r; 12317a0ca55SKONRAD Frederic if (!vhost_net_query(tap_get_vhost_net(nc->peer), vdev)) { 1246e790746SPaolo Bonzini return; 1256e790746SPaolo Bonzini } 1266e790746SPaolo Bonzini n->vhost_started = 1; 12717a0ca55SKONRAD Frederic r = vhost_net_start(vdev, n->nic->ncs, queues); 1286e790746SPaolo Bonzini if (r < 0) { 1296e790746SPaolo Bonzini error_report("unable to start vhost net: %d: " 1306e790746SPaolo Bonzini "falling back on userspace virtio", -r); 1316e790746SPaolo Bonzini n->vhost_started = 0; 1326e790746SPaolo Bonzini } 1336e790746SPaolo Bonzini } else { 13417a0ca55SKONRAD Frederic vhost_net_stop(vdev, n->nic->ncs, queues); 1356e790746SPaolo Bonzini n->vhost_started = 0; 1366e790746SPaolo Bonzini } 1376e790746SPaolo Bonzini } 1386e790746SPaolo Bonzini 1396e790746SPaolo Bonzini static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) 1406e790746SPaolo Bonzini { 14117a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1426e790746SPaolo Bonzini VirtIONetQueue *q; 1436e790746SPaolo Bonzini int i; 1446e790746SPaolo Bonzini uint8_t queue_status; 1456e790746SPaolo Bonzini 1466e790746SPaolo Bonzini virtio_net_vhost_status(n, status); 1476e790746SPaolo Bonzini 1486e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 1496e790746SPaolo Bonzini q = &n->vqs[i]; 1506e790746SPaolo Bonzini 1516e790746SPaolo Bonzini if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { 1526e790746SPaolo Bonzini queue_status = 0; 1536e790746SPaolo Bonzini } else { 1546e790746SPaolo Bonzini queue_status = status; 1556e790746SPaolo Bonzini } 1566e790746SPaolo Bonzini 1576e790746SPaolo Bonzini if (!q->tx_waiting) { 1586e790746SPaolo Bonzini continue; 1596e790746SPaolo Bonzini } 1606e790746SPaolo Bonzini 1616e790746SPaolo Bonzini if (virtio_net_started(n, queue_status) && !n->vhost_started) { 1626e790746SPaolo Bonzini if (q->tx_timer) { 1636e790746SPaolo Bonzini qemu_mod_timer(q->tx_timer, 1646e790746SPaolo Bonzini qemu_get_clock_ns(vm_clock) + n->tx_timeout); 1656e790746SPaolo Bonzini } else { 1666e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 1676e790746SPaolo Bonzini } 1686e790746SPaolo Bonzini } else { 1696e790746SPaolo Bonzini if (q->tx_timer) { 1706e790746SPaolo Bonzini qemu_del_timer(q->tx_timer); 1716e790746SPaolo Bonzini } else { 1726e790746SPaolo Bonzini qemu_bh_cancel(q->tx_bh); 1736e790746SPaolo Bonzini } 1746e790746SPaolo Bonzini } 1756e790746SPaolo Bonzini } 1766e790746SPaolo Bonzini } 1776e790746SPaolo Bonzini 1786e790746SPaolo Bonzini static void virtio_net_set_link_status(NetClientState *nc) 1796e790746SPaolo Bonzini { 1806e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 18117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 1826e790746SPaolo Bonzini uint16_t old_status = n->status; 1836e790746SPaolo Bonzini 1846e790746SPaolo Bonzini if (nc->link_down) 1856e790746SPaolo Bonzini n->status &= ~VIRTIO_NET_S_LINK_UP; 1866e790746SPaolo Bonzini else 1876e790746SPaolo Bonzini n->status |= VIRTIO_NET_S_LINK_UP; 1886e790746SPaolo Bonzini 1896e790746SPaolo Bonzini if (n->status != old_status) 19017a0ca55SKONRAD Frederic virtio_notify_config(vdev); 1916e790746SPaolo Bonzini 19217a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 1936e790746SPaolo Bonzini } 1946e790746SPaolo Bonzini 1956e790746SPaolo Bonzini static void virtio_net_reset(VirtIODevice *vdev) 1966e790746SPaolo Bonzini { 19717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 1986e790746SPaolo Bonzini 1996e790746SPaolo Bonzini /* Reset back to compatibility mode */ 2006e790746SPaolo Bonzini n->promisc = 1; 2016e790746SPaolo Bonzini n->allmulti = 0; 2026e790746SPaolo Bonzini n->alluni = 0; 2036e790746SPaolo Bonzini n->nomulti = 0; 2046e790746SPaolo Bonzini n->nouni = 0; 2056e790746SPaolo Bonzini n->nobcast = 0; 2066e790746SPaolo Bonzini /* multiqueue is disabled by default */ 2076e790746SPaolo Bonzini n->curr_queues = 1; 2086e790746SPaolo Bonzini 2096e790746SPaolo Bonzini /* Flush any MAC and VLAN filter table state */ 2106e790746SPaolo Bonzini n->mac_table.in_use = 0; 2116e790746SPaolo Bonzini n->mac_table.first_multi = 0; 2126e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 2136e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 2146e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 2156e790746SPaolo Bonzini memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); 2166e790746SPaolo Bonzini memset(n->vlans, 0, MAX_VLAN >> 3); 2176e790746SPaolo Bonzini } 2186e790746SPaolo Bonzini 2196e790746SPaolo Bonzini static void peer_test_vnet_hdr(VirtIONet *n) 2206e790746SPaolo Bonzini { 2216e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 2226e790746SPaolo Bonzini if (!nc->peer) { 2236e790746SPaolo Bonzini return; 2246e790746SPaolo Bonzini } 2256e790746SPaolo Bonzini 2266e790746SPaolo Bonzini if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 2276e790746SPaolo Bonzini return; 2286e790746SPaolo Bonzini } 2296e790746SPaolo Bonzini 2306e790746SPaolo Bonzini n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer); 2316e790746SPaolo Bonzini } 2326e790746SPaolo Bonzini 2336e790746SPaolo Bonzini static int peer_has_vnet_hdr(VirtIONet *n) 2346e790746SPaolo Bonzini { 2356e790746SPaolo Bonzini return n->has_vnet_hdr; 2366e790746SPaolo Bonzini } 2376e790746SPaolo Bonzini 2386e790746SPaolo Bonzini static int peer_has_ufo(VirtIONet *n) 2396e790746SPaolo Bonzini { 2406e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) 2416e790746SPaolo Bonzini return 0; 2426e790746SPaolo Bonzini 2436e790746SPaolo Bonzini n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer); 2446e790746SPaolo Bonzini 2456e790746SPaolo Bonzini return n->has_ufo; 2466e790746SPaolo Bonzini } 2476e790746SPaolo Bonzini 2486e790746SPaolo Bonzini static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs) 2496e790746SPaolo Bonzini { 2506e790746SPaolo Bonzini int i; 2516e790746SPaolo Bonzini NetClientState *nc; 2526e790746SPaolo Bonzini 2536e790746SPaolo Bonzini n->mergeable_rx_bufs = mergeable_rx_bufs; 2546e790746SPaolo Bonzini 2556e790746SPaolo Bonzini n->guest_hdr_len = n->mergeable_rx_bufs ? 2566e790746SPaolo Bonzini sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); 2576e790746SPaolo Bonzini 2586e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 2596e790746SPaolo Bonzini nc = qemu_get_subqueue(n->nic, i); 2606e790746SPaolo Bonzini 2616e790746SPaolo Bonzini if (peer_has_vnet_hdr(n) && 2626e790746SPaolo Bonzini tap_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { 2636e790746SPaolo Bonzini tap_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); 2646e790746SPaolo Bonzini n->host_hdr_len = n->guest_hdr_len; 2656e790746SPaolo Bonzini } 2666e790746SPaolo Bonzini } 2676e790746SPaolo Bonzini } 2686e790746SPaolo Bonzini 2696e790746SPaolo Bonzini static int peer_attach(VirtIONet *n, int index) 2706e790746SPaolo Bonzini { 2716e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 2726e790746SPaolo Bonzini 2736e790746SPaolo Bonzini if (!nc->peer) { 2746e790746SPaolo Bonzini return 0; 2756e790746SPaolo Bonzini } 2766e790746SPaolo Bonzini 2776e790746SPaolo Bonzini if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 2786e790746SPaolo Bonzini return 0; 2796e790746SPaolo Bonzini } 2806e790746SPaolo Bonzini 2816e790746SPaolo Bonzini return tap_enable(nc->peer); 2826e790746SPaolo Bonzini } 2836e790746SPaolo Bonzini 2846e790746SPaolo Bonzini static int peer_detach(VirtIONet *n, int index) 2856e790746SPaolo Bonzini { 2866e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, index); 2876e790746SPaolo Bonzini 2886e790746SPaolo Bonzini if (!nc->peer) { 2896e790746SPaolo Bonzini return 0; 2906e790746SPaolo Bonzini } 2916e790746SPaolo Bonzini 2926e790746SPaolo Bonzini if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 2936e790746SPaolo Bonzini return 0; 2946e790746SPaolo Bonzini } 2956e790746SPaolo Bonzini 2966e790746SPaolo Bonzini return tap_disable(nc->peer); 2976e790746SPaolo Bonzini } 2986e790746SPaolo Bonzini 2996e790746SPaolo Bonzini static void virtio_net_set_queues(VirtIONet *n) 3006e790746SPaolo Bonzini { 3016e790746SPaolo Bonzini int i; 3026e790746SPaolo Bonzini 3036e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 3046e790746SPaolo Bonzini if (i < n->curr_queues) { 3056e790746SPaolo Bonzini assert(!peer_attach(n, i)); 3066e790746SPaolo Bonzini } else { 3076e790746SPaolo Bonzini assert(!peer_detach(n, i)); 3086e790746SPaolo Bonzini } 3096e790746SPaolo Bonzini } 3106e790746SPaolo Bonzini } 3116e790746SPaolo Bonzini 312*ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); 3136e790746SPaolo Bonzini 3146e790746SPaolo Bonzini static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) 3156e790746SPaolo Bonzini { 31617a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3176e790746SPaolo Bonzini NetClientState *nc = qemu_get_queue(n->nic); 3186e790746SPaolo Bonzini 3196e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_MAC); 3206e790746SPaolo Bonzini 3216e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n)) { 3226e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_CSUM); 3236e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4); 3246e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6); 3256e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN); 3266e790746SPaolo Bonzini 3276e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM); 3286e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4); 3296e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6); 3306e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN); 3316e790746SPaolo Bonzini } 3326e790746SPaolo Bonzini 3336e790746SPaolo Bonzini if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { 3346e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO); 3356e790746SPaolo Bonzini features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO); 3366e790746SPaolo Bonzini } 3376e790746SPaolo Bonzini 3386e790746SPaolo Bonzini if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 3396e790746SPaolo Bonzini return features; 3406e790746SPaolo Bonzini } 3416e790746SPaolo Bonzini if (!tap_get_vhost_net(nc->peer)) { 3426e790746SPaolo Bonzini return features; 3436e790746SPaolo Bonzini } 3446e790746SPaolo Bonzini return vhost_net_get_features(tap_get_vhost_net(nc->peer), features); 3456e790746SPaolo Bonzini } 3466e790746SPaolo Bonzini 3476e790746SPaolo Bonzini static uint32_t virtio_net_bad_features(VirtIODevice *vdev) 3486e790746SPaolo Bonzini { 3496e790746SPaolo Bonzini uint32_t features = 0; 3506e790746SPaolo Bonzini 3516e790746SPaolo Bonzini /* Linux kernel 2.6.25. It understood MAC (as everyone must), 3526e790746SPaolo Bonzini * but also these: */ 3536e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_MAC); 3546e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_CSUM); 3556e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_HOST_TSO4); 3566e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_HOST_TSO6); 3576e790746SPaolo Bonzini features |= (1 << VIRTIO_NET_F_HOST_ECN); 3586e790746SPaolo Bonzini 3596e790746SPaolo Bonzini return features; 3606e790746SPaolo Bonzini } 3616e790746SPaolo Bonzini 3626e790746SPaolo Bonzini static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) 3636e790746SPaolo Bonzini { 36417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 3656e790746SPaolo Bonzini int i; 3666e790746SPaolo Bonzini 367*ec57db16SJason Wang virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ))); 3686e790746SPaolo Bonzini 3696e790746SPaolo Bonzini virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF))); 3706e790746SPaolo Bonzini 3716e790746SPaolo Bonzini if (n->has_vnet_hdr) { 3726e790746SPaolo Bonzini tap_set_offload(qemu_get_subqueue(n->nic, 0)->peer, 3736e790746SPaolo Bonzini (features >> VIRTIO_NET_F_GUEST_CSUM) & 1, 3746e790746SPaolo Bonzini (features >> VIRTIO_NET_F_GUEST_TSO4) & 1, 3756e790746SPaolo Bonzini (features >> VIRTIO_NET_F_GUEST_TSO6) & 1, 3766e790746SPaolo Bonzini (features >> VIRTIO_NET_F_GUEST_ECN) & 1, 3776e790746SPaolo Bonzini (features >> VIRTIO_NET_F_GUEST_UFO) & 1); 3786e790746SPaolo Bonzini } 3796e790746SPaolo Bonzini 3806e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 3816e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, i); 3826e790746SPaolo Bonzini 3836e790746SPaolo Bonzini if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { 3846e790746SPaolo Bonzini continue; 3856e790746SPaolo Bonzini } 3866e790746SPaolo Bonzini if (!tap_get_vhost_net(nc->peer)) { 3876e790746SPaolo Bonzini continue; 3886e790746SPaolo Bonzini } 3896e790746SPaolo Bonzini vhost_net_ack_features(tap_get_vhost_net(nc->peer), features); 3906e790746SPaolo Bonzini } 3916e790746SPaolo Bonzini } 3926e790746SPaolo Bonzini 3936e790746SPaolo Bonzini static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, 3946e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 3956e790746SPaolo Bonzini { 3966e790746SPaolo Bonzini uint8_t on; 3976e790746SPaolo Bonzini size_t s; 3986e790746SPaolo Bonzini 3996e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); 4006e790746SPaolo Bonzini if (s != sizeof(on)) { 4016e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4026e790746SPaolo Bonzini } 4036e790746SPaolo Bonzini 4046e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { 4056e790746SPaolo Bonzini n->promisc = on; 4066e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { 4076e790746SPaolo Bonzini n->allmulti = on; 4086e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { 4096e790746SPaolo Bonzini n->alluni = on; 4106e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { 4116e790746SPaolo Bonzini n->nomulti = on; 4126e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { 4136e790746SPaolo Bonzini n->nouni = on; 4146e790746SPaolo Bonzini } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { 4156e790746SPaolo Bonzini n->nobcast = on; 4166e790746SPaolo Bonzini } else { 4176e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4186e790746SPaolo Bonzini } 4196e790746SPaolo Bonzini 4206e790746SPaolo Bonzini return VIRTIO_NET_OK; 4216e790746SPaolo Bonzini } 4226e790746SPaolo Bonzini 4236e790746SPaolo Bonzini static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, 4246e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 4256e790746SPaolo Bonzini { 4266e790746SPaolo Bonzini struct virtio_net_ctrl_mac mac_data; 4276e790746SPaolo Bonzini size_t s; 4286e790746SPaolo Bonzini 4296e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { 4306e790746SPaolo Bonzini if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { 4316e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4326e790746SPaolo Bonzini } 4336e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); 4346e790746SPaolo Bonzini assert(s == sizeof(n->mac)); 4356e790746SPaolo Bonzini qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 4366e790746SPaolo Bonzini return VIRTIO_NET_OK; 4376e790746SPaolo Bonzini } 4386e790746SPaolo Bonzini 4396e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { 4406e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4416e790746SPaolo Bonzini } 4426e790746SPaolo Bonzini 4436e790746SPaolo Bonzini n->mac_table.in_use = 0; 4446e790746SPaolo Bonzini n->mac_table.first_multi = 0; 4456e790746SPaolo Bonzini n->mac_table.uni_overflow = 0; 4466e790746SPaolo Bonzini n->mac_table.multi_overflow = 0; 4476e790746SPaolo Bonzini memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); 4486e790746SPaolo Bonzini 4496e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 4506e790746SPaolo Bonzini sizeof(mac_data.entries)); 4516e790746SPaolo Bonzini mac_data.entries = ldl_p(&mac_data.entries); 4526e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 4536e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4546e790746SPaolo Bonzini } 4556e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 4566e790746SPaolo Bonzini 4576e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { 4586e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4596e790746SPaolo Bonzini } 4606e790746SPaolo Bonzini 4616e790746SPaolo Bonzini if (mac_data.entries <= MAC_TABLE_ENTRIES) { 4626e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, 4636e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 4646e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 4656e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4666e790746SPaolo Bonzini } 4676e790746SPaolo Bonzini n->mac_table.in_use += mac_data.entries; 4686e790746SPaolo Bonzini } else { 4696e790746SPaolo Bonzini n->mac_table.uni_overflow = 1; 4706e790746SPaolo Bonzini } 4716e790746SPaolo Bonzini 4726e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); 4736e790746SPaolo Bonzini 4746e790746SPaolo Bonzini n->mac_table.first_multi = n->mac_table.in_use; 4756e790746SPaolo Bonzini 4766e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, 4776e790746SPaolo Bonzini sizeof(mac_data.entries)); 4786e790746SPaolo Bonzini mac_data.entries = ldl_p(&mac_data.entries); 4796e790746SPaolo Bonzini if (s != sizeof(mac_data.entries)) { 4806e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4816e790746SPaolo Bonzini } 4826e790746SPaolo Bonzini 4836e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, s); 4846e790746SPaolo Bonzini 4856e790746SPaolo Bonzini if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { 4866e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4876e790746SPaolo Bonzini } 4886e790746SPaolo Bonzini 4896e790746SPaolo Bonzini if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { 4906e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, 4916e790746SPaolo Bonzini mac_data.entries * ETH_ALEN); 4926e790746SPaolo Bonzini if (s != mac_data.entries * ETH_ALEN) { 4936e790746SPaolo Bonzini return VIRTIO_NET_ERR; 4946e790746SPaolo Bonzini } 4956e790746SPaolo Bonzini n->mac_table.in_use += mac_data.entries; 4966e790746SPaolo Bonzini } else { 4976e790746SPaolo Bonzini n->mac_table.multi_overflow = 1; 4986e790746SPaolo Bonzini } 4996e790746SPaolo Bonzini 5006e790746SPaolo Bonzini return VIRTIO_NET_OK; 5016e790746SPaolo Bonzini } 5026e790746SPaolo Bonzini 5036e790746SPaolo Bonzini static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, 5046e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 5056e790746SPaolo Bonzini { 5066e790746SPaolo Bonzini uint16_t vid; 5076e790746SPaolo Bonzini size_t s; 5086e790746SPaolo Bonzini 5096e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); 5106e790746SPaolo Bonzini vid = lduw_p(&vid); 5116e790746SPaolo Bonzini if (s != sizeof(vid)) { 5126e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5136e790746SPaolo Bonzini } 5146e790746SPaolo Bonzini 5156e790746SPaolo Bonzini if (vid >= MAX_VLAN) 5166e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5176e790746SPaolo Bonzini 5186e790746SPaolo Bonzini if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) 5196e790746SPaolo Bonzini n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); 5206e790746SPaolo Bonzini else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) 5216e790746SPaolo Bonzini n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); 5226e790746SPaolo Bonzini else 5236e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5246e790746SPaolo Bonzini 5256e790746SPaolo Bonzini return VIRTIO_NET_OK; 5266e790746SPaolo Bonzini } 5276e790746SPaolo Bonzini 5286e790746SPaolo Bonzini static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, 5296e790746SPaolo Bonzini struct iovec *iov, unsigned int iov_cnt) 5306e790746SPaolo Bonzini { 53117a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 5326e790746SPaolo Bonzini struct virtio_net_ctrl_mq mq; 5336e790746SPaolo Bonzini size_t s; 5346e790746SPaolo Bonzini uint16_t queues; 5356e790746SPaolo Bonzini 5366e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); 5376e790746SPaolo Bonzini if (s != sizeof(mq)) { 5386e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5396e790746SPaolo Bonzini } 5406e790746SPaolo Bonzini 5416e790746SPaolo Bonzini if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 5426e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5436e790746SPaolo Bonzini } 5446e790746SPaolo Bonzini 5456e790746SPaolo Bonzini queues = lduw_p(&mq.virtqueue_pairs); 5466e790746SPaolo Bonzini 5476e790746SPaolo Bonzini if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 5486e790746SPaolo Bonzini queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 5496e790746SPaolo Bonzini queues > n->max_queues || 5506e790746SPaolo Bonzini !n->multiqueue) { 5516e790746SPaolo Bonzini return VIRTIO_NET_ERR; 5526e790746SPaolo Bonzini } 5536e790746SPaolo Bonzini 5546e790746SPaolo Bonzini n->curr_queues = queues; 5556e790746SPaolo Bonzini /* stop the backend before changing the number of queues to avoid handling a 5566e790746SPaolo Bonzini * disabled queue */ 55717a0ca55SKONRAD Frederic virtio_net_set_status(vdev, vdev->status); 5586e790746SPaolo Bonzini virtio_net_set_queues(n); 5596e790746SPaolo Bonzini 5606e790746SPaolo Bonzini return VIRTIO_NET_OK; 5616e790746SPaolo Bonzini } 5626e790746SPaolo Bonzini static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 5636e790746SPaolo Bonzini { 56417a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 5656e790746SPaolo Bonzini struct virtio_net_ctrl_hdr ctrl; 5666e790746SPaolo Bonzini virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 5676e790746SPaolo Bonzini VirtQueueElement elem; 5686e790746SPaolo Bonzini size_t s; 5696e790746SPaolo Bonzini struct iovec *iov; 5706e790746SPaolo Bonzini unsigned int iov_cnt; 5716e790746SPaolo Bonzini 5726e790746SPaolo Bonzini while (virtqueue_pop(vq, &elem)) { 5736e790746SPaolo Bonzini if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) || 5746e790746SPaolo Bonzini iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) { 5756e790746SPaolo Bonzini error_report("virtio-net ctrl missing headers"); 5766e790746SPaolo Bonzini exit(1); 5776e790746SPaolo Bonzini } 5786e790746SPaolo Bonzini 5796e790746SPaolo Bonzini iov = elem.out_sg; 5806e790746SPaolo Bonzini iov_cnt = elem.out_num; 5816e790746SPaolo Bonzini s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); 5826e790746SPaolo Bonzini iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); 5836e790746SPaolo Bonzini if (s != sizeof(ctrl)) { 5846e790746SPaolo Bonzini status = VIRTIO_NET_ERR; 5856e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { 5866e790746SPaolo Bonzini status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); 5876e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { 5886e790746SPaolo Bonzini status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); 5896e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { 5906e790746SPaolo Bonzini status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); 5916e790746SPaolo Bonzini } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { 5926e790746SPaolo Bonzini status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); 5936e790746SPaolo Bonzini } 5946e790746SPaolo Bonzini 5956e790746SPaolo Bonzini s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status)); 5966e790746SPaolo Bonzini assert(s == sizeof(status)); 5976e790746SPaolo Bonzini 5986e790746SPaolo Bonzini virtqueue_push(vq, &elem, sizeof(status)); 5996e790746SPaolo Bonzini virtio_notify(vdev, vq); 6006e790746SPaolo Bonzini } 6016e790746SPaolo Bonzini } 6026e790746SPaolo Bonzini 6036e790746SPaolo Bonzini /* RX */ 6046e790746SPaolo Bonzini 6056e790746SPaolo Bonzini static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) 6066e790746SPaolo Bonzini { 60717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 6086e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(vq)); 6096e790746SPaolo Bonzini 6106e790746SPaolo Bonzini qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); 6116e790746SPaolo Bonzini } 6126e790746SPaolo Bonzini 6136e790746SPaolo Bonzini static int virtio_net_can_receive(NetClientState *nc) 6146e790746SPaolo Bonzini { 6156e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 61617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 6176e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 6186e790746SPaolo Bonzini 61917a0ca55SKONRAD Frederic if (!vdev->vm_running) { 6206e790746SPaolo Bonzini return 0; 6216e790746SPaolo Bonzini } 6226e790746SPaolo Bonzini 6236e790746SPaolo Bonzini if (nc->queue_index >= n->curr_queues) { 6246e790746SPaolo Bonzini return 0; 6256e790746SPaolo Bonzini } 6266e790746SPaolo Bonzini 6276e790746SPaolo Bonzini if (!virtio_queue_ready(q->rx_vq) || 62817a0ca55SKONRAD Frederic !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 6296e790746SPaolo Bonzini return 0; 6306e790746SPaolo Bonzini } 6316e790746SPaolo Bonzini 6326e790746SPaolo Bonzini return 1; 6336e790746SPaolo Bonzini } 6346e790746SPaolo Bonzini 6356e790746SPaolo Bonzini static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) 6366e790746SPaolo Bonzini { 6376e790746SPaolo Bonzini VirtIONet *n = q->n; 6386e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 6396e790746SPaolo Bonzini (n->mergeable_rx_bufs && 6406e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 6416e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 1); 6426e790746SPaolo Bonzini 6436e790746SPaolo Bonzini /* To avoid a race condition where the guest has made some buffers 6446e790746SPaolo Bonzini * available after the above check but before notification was 6456e790746SPaolo Bonzini * enabled, check for available buffers again. 6466e790746SPaolo Bonzini */ 6476e790746SPaolo Bonzini if (virtio_queue_empty(q->rx_vq) || 6486e790746SPaolo Bonzini (n->mergeable_rx_bufs && 6496e790746SPaolo Bonzini !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { 6506e790746SPaolo Bonzini return 0; 6516e790746SPaolo Bonzini } 6526e790746SPaolo Bonzini } 6536e790746SPaolo Bonzini 6546e790746SPaolo Bonzini virtio_queue_set_notification(q->rx_vq, 0); 6556e790746SPaolo Bonzini return 1; 6566e790746SPaolo Bonzini } 6576e790746SPaolo Bonzini 6586e790746SPaolo Bonzini /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so 6596e790746SPaolo Bonzini * it never finds out that the packets don't have valid checksums. This 6606e790746SPaolo Bonzini * causes dhclient to get upset. Fedora's carried a patch for ages to 6616e790746SPaolo Bonzini * fix this with Xen but it hasn't appeared in an upstream release of 6626e790746SPaolo Bonzini * dhclient yet. 6636e790746SPaolo Bonzini * 6646e790746SPaolo Bonzini * To avoid breaking existing guests, we catch udp packets and add 6656e790746SPaolo Bonzini * checksums. This is terrible but it's better than hacking the guest 6666e790746SPaolo Bonzini * kernels. 6676e790746SPaolo Bonzini * 6686e790746SPaolo Bonzini * N.B. if we introduce a zero-copy API, this operation is no longer free so 6696e790746SPaolo Bonzini * we should provide a mechanism to disable it to avoid polluting the host 6706e790746SPaolo Bonzini * cache. 6716e790746SPaolo Bonzini */ 6726e790746SPaolo Bonzini static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, 6736e790746SPaolo Bonzini uint8_t *buf, size_t size) 6746e790746SPaolo Bonzini { 6756e790746SPaolo Bonzini if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ 6766e790746SPaolo Bonzini (size > 27 && size < 1500) && /* normal sized MTU */ 6776e790746SPaolo Bonzini (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ 6786e790746SPaolo Bonzini (buf[23] == 17) && /* ip.protocol == UDP */ 6796e790746SPaolo Bonzini (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ 6806e790746SPaolo Bonzini net_checksum_calculate(buf, size); 6816e790746SPaolo Bonzini hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; 6826e790746SPaolo Bonzini } 6836e790746SPaolo Bonzini } 6846e790746SPaolo Bonzini 6856e790746SPaolo Bonzini static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, 6866e790746SPaolo Bonzini const void *buf, size_t size) 6876e790746SPaolo Bonzini { 6886e790746SPaolo Bonzini if (n->has_vnet_hdr) { 6896e790746SPaolo Bonzini /* FIXME this cast is evil */ 6906e790746SPaolo Bonzini void *wbuf = (void *)buf; 6916e790746SPaolo Bonzini work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, 6926e790746SPaolo Bonzini size - n->host_hdr_len); 6936e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); 6946e790746SPaolo Bonzini } else { 6956e790746SPaolo Bonzini struct virtio_net_hdr hdr = { 6966e790746SPaolo Bonzini .flags = 0, 6976e790746SPaolo Bonzini .gso_type = VIRTIO_NET_HDR_GSO_NONE 6986e790746SPaolo Bonzini }; 6996e790746SPaolo Bonzini iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); 7006e790746SPaolo Bonzini } 7016e790746SPaolo Bonzini } 7026e790746SPaolo Bonzini 7036e790746SPaolo Bonzini static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) 7046e790746SPaolo Bonzini { 7056e790746SPaolo Bonzini static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 7066e790746SPaolo Bonzini static const uint8_t vlan[] = {0x81, 0x00}; 7076e790746SPaolo Bonzini uint8_t *ptr = (uint8_t *)buf; 7086e790746SPaolo Bonzini int i; 7096e790746SPaolo Bonzini 7106e790746SPaolo Bonzini if (n->promisc) 7116e790746SPaolo Bonzini return 1; 7126e790746SPaolo Bonzini 7136e790746SPaolo Bonzini ptr += n->host_hdr_len; 7146e790746SPaolo Bonzini 7156e790746SPaolo Bonzini if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { 7166e790746SPaolo Bonzini int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; 7176e790746SPaolo Bonzini if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) 7186e790746SPaolo Bonzini return 0; 7196e790746SPaolo Bonzini } 7206e790746SPaolo Bonzini 7216e790746SPaolo Bonzini if (ptr[0] & 1) { // multicast 7226e790746SPaolo Bonzini if (!memcmp(ptr, bcast, sizeof(bcast))) { 7236e790746SPaolo Bonzini return !n->nobcast; 7246e790746SPaolo Bonzini } else if (n->nomulti) { 7256e790746SPaolo Bonzini return 0; 7266e790746SPaolo Bonzini } else if (n->allmulti || n->mac_table.multi_overflow) { 7276e790746SPaolo Bonzini return 1; 7286e790746SPaolo Bonzini } 7296e790746SPaolo Bonzini 7306e790746SPaolo Bonzini for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { 7316e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 7326e790746SPaolo Bonzini return 1; 7336e790746SPaolo Bonzini } 7346e790746SPaolo Bonzini } 7356e790746SPaolo Bonzini } else { // unicast 7366e790746SPaolo Bonzini if (n->nouni) { 7376e790746SPaolo Bonzini return 0; 7386e790746SPaolo Bonzini } else if (n->alluni || n->mac_table.uni_overflow) { 7396e790746SPaolo Bonzini return 1; 7406e790746SPaolo Bonzini } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { 7416e790746SPaolo Bonzini return 1; 7426e790746SPaolo Bonzini } 7436e790746SPaolo Bonzini 7446e790746SPaolo Bonzini for (i = 0; i < n->mac_table.first_multi; i++) { 7456e790746SPaolo Bonzini if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { 7466e790746SPaolo Bonzini return 1; 7476e790746SPaolo Bonzini } 7486e790746SPaolo Bonzini } 7496e790746SPaolo Bonzini } 7506e790746SPaolo Bonzini 7516e790746SPaolo Bonzini return 0; 7526e790746SPaolo Bonzini } 7536e790746SPaolo Bonzini 7546e790746SPaolo Bonzini static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size) 7556e790746SPaolo Bonzini { 7566e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 7576e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 75817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 7596e790746SPaolo Bonzini struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; 7606e790746SPaolo Bonzini struct virtio_net_hdr_mrg_rxbuf mhdr; 7616e790746SPaolo Bonzini unsigned mhdr_cnt = 0; 7626e790746SPaolo Bonzini size_t offset, i, guest_offset; 7636e790746SPaolo Bonzini 7646e790746SPaolo Bonzini if (!virtio_net_can_receive(nc)) { 7656e790746SPaolo Bonzini return -1; 7666e790746SPaolo Bonzini } 7676e790746SPaolo Bonzini 7686e790746SPaolo Bonzini /* hdr_len refers to the header we supply to the guest */ 7696e790746SPaolo Bonzini if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { 7706e790746SPaolo Bonzini return 0; 7716e790746SPaolo Bonzini } 7726e790746SPaolo Bonzini 7736e790746SPaolo Bonzini if (!receive_filter(n, buf, size)) 7746e790746SPaolo Bonzini return size; 7756e790746SPaolo Bonzini 7766e790746SPaolo Bonzini offset = i = 0; 7776e790746SPaolo Bonzini 7786e790746SPaolo Bonzini while (offset < size) { 7796e790746SPaolo Bonzini VirtQueueElement elem; 7806e790746SPaolo Bonzini int len, total; 7816e790746SPaolo Bonzini const struct iovec *sg = elem.in_sg; 7826e790746SPaolo Bonzini 7836e790746SPaolo Bonzini total = 0; 7846e790746SPaolo Bonzini 7856e790746SPaolo Bonzini if (virtqueue_pop(q->rx_vq, &elem) == 0) { 7866e790746SPaolo Bonzini if (i == 0) 7876e790746SPaolo Bonzini return -1; 7886e790746SPaolo Bonzini error_report("virtio-net unexpected empty queue: " 7896e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 7906e790746SPaolo Bonzini "guest hdr len %zd, host hdr len %zd guest features 0x%x", 7916e790746SPaolo Bonzini i, n->mergeable_rx_bufs, offset, size, 79217a0ca55SKONRAD Frederic n->guest_hdr_len, n->host_hdr_len, vdev->guest_features); 7936e790746SPaolo Bonzini exit(1); 7946e790746SPaolo Bonzini } 7956e790746SPaolo Bonzini 7966e790746SPaolo Bonzini if (elem.in_num < 1) { 7976e790746SPaolo Bonzini error_report("virtio-net receive queue contains no in buffers"); 7986e790746SPaolo Bonzini exit(1); 7996e790746SPaolo Bonzini } 8006e790746SPaolo Bonzini 8016e790746SPaolo Bonzini if (i == 0) { 8026e790746SPaolo Bonzini assert(offset == 0); 8036e790746SPaolo Bonzini if (n->mergeable_rx_bufs) { 8046e790746SPaolo Bonzini mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), 8056e790746SPaolo Bonzini sg, elem.in_num, 8066e790746SPaolo Bonzini offsetof(typeof(mhdr), num_buffers), 8076e790746SPaolo Bonzini sizeof(mhdr.num_buffers)); 8086e790746SPaolo Bonzini } 8096e790746SPaolo Bonzini 8106e790746SPaolo Bonzini receive_header(n, sg, elem.in_num, buf, size); 8116e790746SPaolo Bonzini offset = n->host_hdr_len; 8126e790746SPaolo Bonzini total += n->guest_hdr_len; 8136e790746SPaolo Bonzini guest_offset = n->guest_hdr_len; 8146e790746SPaolo Bonzini } else { 8156e790746SPaolo Bonzini guest_offset = 0; 8166e790746SPaolo Bonzini } 8176e790746SPaolo Bonzini 8186e790746SPaolo Bonzini /* copy in packet. ugh */ 8196e790746SPaolo Bonzini len = iov_from_buf(sg, elem.in_num, guest_offset, 8206e790746SPaolo Bonzini buf + offset, size - offset); 8216e790746SPaolo Bonzini total += len; 8226e790746SPaolo Bonzini offset += len; 8236e790746SPaolo Bonzini /* If buffers can't be merged, at this point we 8246e790746SPaolo Bonzini * must have consumed the complete packet. 8256e790746SPaolo Bonzini * Otherwise, drop it. */ 8266e790746SPaolo Bonzini if (!n->mergeable_rx_bufs && offset < size) { 8276e790746SPaolo Bonzini #if 0 8286e790746SPaolo Bonzini error_report("virtio-net truncated non-mergeable packet: " 8296e790746SPaolo Bonzini "i %zd mergeable %d offset %zd, size %zd, " 8306e790746SPaolo Bonzini "guest hdr len %zd, host hdr len %zd", 8316e790746SPaolo Bonzini i, n->mergeable_rx_bufs, 8326e790746SPaolo Bonzini offset, size, n->guest_hdr_len, n->host_hdr_len); 8336e790746SPaolo Bonzini #endif 8346e790746SPaolo Bonzini return size; 8356e790746SPaolo Bonzini } 8366e790746SPaolo Bonzini 8376e790746SPaolo Bonzini /* signal other side */ 8386e790746SPaolo Bonzini virtqueue_fill(q->rx_vq, &elem, total, i++); 8396e790746SPaolo Bonzini } 8406e790746SPaolo Bonzini 8416e790746SPaolo Bonzini if (mhdr_cnt) { 8426e790746SPaolo Bonzini stw_p(&mhdr.num_buffers, i); 8436e790746SPaolo Bonzini iov_from_buf(mhdr_sg, mhdr_cnt, 8446e790746SPaolo Bonzini 0, 8456e790746SPaolo Bonzini &mhdr.num_buffers, sizeof mhdr.num_buffers); 8466e790746SPaolo Bonzini } 8476e790746SPaolo Bonzini 8486e790746SPaolo Bonzini virtqueue_flush(q->rx_vq, i); 84917a0ca55SKONRAD Frederic virtio_notify(vdev, q->rx_vq); 8506e790746SPaolo Bonzini 8516e790746SPaolo Bonzini return size; 8526e790746SPaolo Bonzini } 8536e790746SPaolo Bonzini 8546e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q); 8556e790746SPaolo Bonzini 8566e790746SPaolo Bonzini static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) 8576e790746SPaolo Bonzini { 8586e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 8596e790746SPaolo Bonzini VirtIONetQueue *q = virtio_net_get_subqueue(nc); 86017a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 8616e790746SPaolo Bonzini 8626e790746SPaolo Bonzini virtqueue_push(q->tx_vq, &q->async_tx.elem, 0); 86317a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 8646e790746SPaolo Bonzini 8656e790746SPaolo Bonzini q->async_tx.elem.out_num = q->async_tx.len = 0; 8666e790746SPaolo Bonzini 8676e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 8686e790746SPaolo Bonzini virtio_net_flush_tx(q); 8696e790746SPaolo Bonzini } 8706e790746SPaolo Bonzini 8716e790746SPaolo Bonzini /* TX */ 8726e790746SPaolo Bonzini static int32_t virtio_net_flush_tx(VirtIONetQueue *q) 8736e790746SPaolo Bonzini { 8746e790746SPaolo Bonzini VirtIONet *n = q->n; 87517a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 8766e790746SPaolo Bonzini VirtQueueElement elem; 8776e790746SPaolo Bonzini int32_t num_packets = 0; 8786e790746SPaolo Bonzini int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); 87917a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 8806e790746SPaolo Bonzini return num_packets; 8816e790746SPaolo Bonzini } 8826e790746SPaolo Bonzini 88317a0ca55SKONRAD Frederic assert(vdev->vm_running); 8846e790746SPaolo Bonzini 8856e790746SPaolo Bonzini if (q->async_tx.elem.out_num) { 8866e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 8876e790746SPaolo Bonzini return num_packets; 8886e790746SPaolo Bonzini } 8896e790746SPaolo Bonzini 8906e790746SPaolo Bonzini while (virtqueue_pop(q->tx_vq, &elem)) { 8916e790746SPaolo Bonzini ssize_t ret, len; 8926e790746SPaolo Bonzini unsigned int out_num = elem.out_num; 8936e790746SPaolo Bonzini struct iovec *out_sg = &elem.out_sg[0]; 8946e790746SPaolo Bonzini struct iovec sg[VIRTQUEUE_MAX_SIZE]; 8956e790746SPaolo Bonzini 8966e790746SPaolo Bonzini if (out_num < 1) { 8976e790746SPaolo Bonzini error_report("virtio-net header not in first element"); 8986e790746SPaolo Bonzini exit(1); 8996e790746SPaolo Bonzini } 9006e790746SPaolo Bonzini 9016e790746SPaolo Bonzini /* 9026e790746SPaolo Bonzini * If host wants to see the guest header as is, we can 9036e790746SPaolo Bonzini * pass it on unchanged. Otherwise, copy just the parts 9046e790746SPaolo Bonzini * that host is interested in. 9056e790746SPaolo Bonzini */ 9066e790746SPaolo Bonzini assert(n->host_hdr_len <= n->guest_hdr_len); 9076e790746SPaolo Bonzini if (n->host_hdr_len != n->guest_hdr_len) { 9086e790746SPaolo Bonzini unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), 9096e790746SPaolo Bonzini out_sg, out_num, 9106e790746SPaolo Bonzini 0, n->host_hdr_len); 9116e790746SPaolo Bonzini sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, 9126e790746SPaolo Bonzini out_sg, out_num, 9136e790746SPaolo Bonzini n->guest_hdr_len, -1); 9146e790746SPaolo Bonzini out_num = sg_num; 9156e790746SPaolo Bonzini out_sg = sg; 9166e790746SPaolo Bonzini } 9176e790746SPaolo Bonzini 9186e790746SPaolo Bonzini len = n->guest_hdr_len; 9196e790746SPaolo Bonzini 9206e790746SPaolo Bonzini ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), 9216e790746SPaolo Bonzini out_sg, out_num, virtio_net_tx_complete); 9226e790746SPaolo Bonzini if (ret == 0) { 9236e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 9246e790746SPaolo Bonzini q->async_tx.elem = elem; 9256e790746SPaolo Bonzini q->async_tx.len = len; 9266e790746SPaolo Bonzini return -EBUSY; 9276e790746SPaolo Bonzini } 9286e790746SPaolo Bonzini 9296e790746SPaolo Bonzini len += ret; 9306e790746SPaolo Bonzini 9316e790746SPaolo Bonzini virtqueue_push(q->tx_vq, &elem, 0); 93217a0ca55SKONRAD Frederic virtio_notify(vdev, q->tx_vq); 9336e790746SPaolo Bonzini 9346e790746SPaolo Bonzini if (++num_packets >= n->tx_burst) { 9356e790746SPaolo Bonzini break; 9366e790746SPaolo Bonzini } 9376e790746SPaolo Bonzini } 9386e790746SPaolo Bonzini return num_packets; 9396e790746SPaolo Bonzini } 9406e790746SPaolo Bonzini 9416e790746SPaolo Bonzini static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) 9426e790746SPaolo Bonzini { 94317a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9446e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 9456e790746SPaolo Bonzini 9466e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 94717a0ca55SKONRAD Frederic if (!vdev->vm_running) { 9486e790746SPaolo Bonzini q->tx_waiting = 1; 9496e790746SPaolo Bonzini return; 9506e790746SPaolo Bonzini } 9516e790746SPaolo Bonzini 9526e790746SPaolo Bonzini if (q->tx_waiting) { 9536e790746SPaolo Bonzini virtio_queue_set_notification(vq, 1); 9546e790746SPaolo Bonzini qemu_del_timer(q->tx_timer); 9556e790746SPaolo Bonzini q->tx_waiting = 0; 9566e790746SPaolo Bonzini virtio_net_flush_tx(q); 9576e790746SPaolo Bonzini } else { 9586e790746SPaolo Bonzini qemu_mod_timer(q->tx_timer, 9596e790746SPaolo Bonzini qemu_get_clock_ns(vm_clock) + n->tx_timeout); 9606e790746SPaolo Bonzini q->tx_waiting = 1; 9616e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 9626e790746SPaolo Bonzini } 9636e790746SPaolo Bonzini } 9646e790746SPaolo Bonzini 9656e790746SPaolo Bonzini static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) 9666e790746SPaolo Bonzini { 96717a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 9686e790746SPaolo Bonzini VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; 9696e790746SPaolo Bonzini 9706e790746SPaolo Bonzini if (unlikely(q->tx_waiting)) { 9716e790746SPaolo Bonzini return; 9726e790746SPaolo Bonzini } 9736e790746SPaolo Bonzini q->tx_waiting = 1; 9746e790746SPaolo Bonzini /* This happens when device was stopped but VCPU wasn't. */ 97517a0ca55SKONRAD Frederic if (!vdev->vm_running) { 9766e790746SPaolo Bonzini return; 9776e790746SPaolo Bonzini } 9786e790746SPaolo Bonzini virtio_queue_set_notification(vq, 0); 9796e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 9806e790746SPaolo Bonzini } 9816e790746SPaolo Bonzini 9826e790746SPaolo Bonzini static void virtio_net_tx_timer(void *opaque) 9836e790746SPaolo Bonzini { 9846e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 9856e790746SPaolo Bonzini VirtIONet *n = q->n; 98617a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 98717a0ca55SKONRAD Frederic assert(vdev->vm_running); 9886e790746SPaolo Bonzini 9896e790746SPaolo Bonzini q->tx_waiting = 0; 9906e790746SPaolo Bonzini 9916e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 99217a0ca55SKONRAD Frederic if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 9936e790746SPaolo Bonzini return; 99417a0ca55SKONRAD Frederic } 9956e790746SPaolo Bonzini 9966e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 9976e790746SPaolo Bonzini virtio_net_flush_tx(q); 9986e790746SPaolo Bonzini } 9996e790746SPaolo Bonzini 10006e790746SPaolo Bonzini static void virtio_net_tx_bh(void *opaque) 10016e790746SPaolo Bonzini { 10026e790746SPaolo Bonzini VirtIONetQueue *q = opaque; 10036e790746SPaolo Bonzini VirtIONet *n = q->n; 100417a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 10056e790746SPaolo Bonzini int32_t ret; 10066e790746SPaolo Bonzini 100717a0ca55SKONRAD Frederic assert(vdev->vm_running); 10086e790746SPaolo Bonzini 10096e790746SPaolo Bonzini q->tx_waiting = 0; 10106e790746SPaolo Bonzini 10116e790746SPaolo Bonzini /* Just in case the driver is not ready on more */ 101217a0ca55SKONRAD Frederic if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { 10136e790746SPaolo Bonzini return; 101417a0ca55SKONRAD Frederic } 10156e790746SPaolo Bonzini 10166e790746SPaolo Bonzini ret = virtio_net_flush_tx(q); 10176e790746SPaolo Bonzini if (ret == -EBUSY) { 10186e790746SPaolo Bonzini return; /* Notification re-enable handled by tx_complete */ 10196e790746SPaolo Bonzini } 10206e790746SPaolo Bonzini 10216e790746SPaolo Bonzini /* If we flush a full burst of packets, assume there are 10226e790746SPaolo Bonzini * more coming and immediately reschedule */ 10236e790746SPaolo Bonzini if (ret >= n->tx_burst) { 10246e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 10256e790746SPaolo Bonzini q->tx_waiting = 1; 10266e790746SPaolo Bonzini return; 10276e790746SPaolo Bonzini } 10286e790746SPaolo Bonzini 10296e790746SPaolo Bonzini /* If less than a full burst, re-enable notification and flush 10306e790746SPaolo Bonzini * anything that may have come in while we weren't looking. If 10316e790746SPaolo Bonzini * we find something, assume the guest is still active and reschedule */ 10326e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 1); 10336e790746SPaolo Bonzini if (virtio_net_flush_tx(q) > 0) { 10346e790746SPaolo Bonzini virtio_queue_set_notification(q->tx_vq, 0); 10356e790746SPaolo Bonzini qemu_bh_schedule(q->tx_bh); 10366e790746SPaolo Bonzini q->tx_waiting = 1; 10376e790746SPaolo Bonzini } 10386e790746SPaolo Bonzini } 10396e790746SPaolo Bonzini 1040*ec57db16SJason Wang static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) 10416e790746SPaolo Bonzini { 104217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 10436e790746SPaolo Bonzini int i, max = multiqueue ? n->max_queues : 1; 10446e790746SPaolo Bonzini 10456e790746SPaolo Bonzini n->multiqueue = multiqueue; 10466e790746SPaolo Bonzini 10476e790746SPaolo Bonzini for (i = 2; i <= n->max_queues * 2 + 1; i++) { 10486e790746SPaolo Bonzini virtio_del_queue(vdev, i); 10496e790746SPaolo Bonzini } 10506e790746SPaolo Bonzini 10516e790746SPaolo Bonzini for (i = 1; i < max; i++) { 10526e790746SPaolo Bonzini n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); 10536e790746SPaolo Bonzini if (n->vqs[i].tx_timer) { 10546e790746SPaolo Bonzini n->vqs[i].tx_vq = 10556e790746SPaolo Bonzini virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer); 10566e790746SPaolo Bonzini n->vqs[i].tx_timer = qemu_new_timer_ns(vm_clock, 10576e790746SPaolo Bonzini virtio_net_tx_timer, 10586e790746SPaolo Bonzini &n->vqs[i]); 10596e790746SPaolo Bonzini } else { 10606e790746SPaolo Bonzini n->vqs[i].tx_vq = 10616e790746SPaolo Bonzini virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh); 10626e790746SPaolo Bonzini n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]); 10636e790746SPaolo Bonzini } 10646e790746SPaolo Bonzini 10656e790746SPaolo Bonzini n->vqs[i].tx_waiting = 0; 10666e790746SPaolo Bonzini n->vqs[i].n = n; 10676e790746SPaolo Bonzini } 10686e790746SPaolo Bonzini 1069*ec57db16SJason Wang /* Note: Minux Guests (version 3.2.1) use ctrl vq but don't ack 1070*ec57db16SJason Wang * VIRTIO_NET_F_CTRL_VQ. Create ctrl vq unconditionally to avoid 1071*ec57db16SJason Wang * breaking them. 1072*ec57db16SJason Wang */ 10736e790746SPaolo Bonzini n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 10746e790746SPaolo Bonzini 10756e790746SPaolo Bonzini virtio_net_set_queues(n); 10766e790746SPaolo Bonzini } 10776e790746SPaolo Bonzini 10786e790746SPaolo Bonzini static void virtio_net_save(QEMUFile *f, void *opaque) 10796e790746SPaolo Bonzini { 10806e790746SPaolo Bonzini int i; 10816e790746SPaolo Bonzini VirtIONet *n = opaque; 108217a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 10836e790746SPaolo Bonzini 10846e790746SPaolo Bonzini /* At this point, backend must be stopped, otherwise 10856e790746SPaolo Bonzini * it might keep writing to memory. */ 10866e790746SPaolo Bonzini assert(!n->vhost_started); 108717a0ca55SKONRAD Frederic virtio_save(vdev, f); 10886e790746SPaolo Bonzini 10896e790746SPaolo Bonzini qemu_put_buffer(f, n->mac, ETH_ALEN); 10906e790746SPaolo Bonzini qemu_put_be32(f, n->vqs[0].tx_waiting); 10916e790746SPaolo Bonzini qemu_put_be32(f, n->mergeable_rx_bufs); 10926e790746SPaolo Bonzini qemu_put_be16(f, n->status); 10936e790746SPaolo Bonzini qemu_put_byte(f, n->promisc); 10946e790746SPaolo Bonzini qemu_put_byte(f, n->allmulti); 10956e790746SPaolo Bonzini qemu_put_be32(f, n->mac_table.in_use); 10966e790746SPaolo Bonzini qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); 10976e790746SPaolo Bonzini qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); 10986e790746SPaolo Bonzini qemu_put_be32(f, n->has_vnet_hdr); 10996e790746SPaolo Bonzini qemu_put_byte(f, n->mac_table.multi_overflow); 11006e790746SPaolo Bonzini qemu_put_byte(f, n->mac_table.uni_overflow); 11016e790746SPaolo Bonzini qemu_put_byte(f, n->alluni); 11026e790746SPaolo Bonzini qemu_put_byte(f, n->nomulti); 11036e790746SPaolo Bonzini qemu_put_byte(f, n->nouni); 11046e790746SPaolo Bonzini qemu_put_byte(f, n->nobcast); 11056e790746SPaolo Bonzini qemu_put_byte(f, n->has_ufo); 11066e790746SPaolo Bonzini if (n->max_queues > 1) { 11076e790746SPaolo Bonzini qemu_put_be16(f, n->max_queues); 11086e790746SPaolo Bonzini qemu_put_be16(f, n->curr_queues); 11096e790746SPaolo Bonzini for (i = 1; i < n->curr_queues; i++) { 11106e790746SPaolo Bonzini qemu_put_be32(f, n->vqs[i].tx_waiting); 11116e790746SPaolo Bonzini } 11126e790746SPaolo Bonzini } 11136e790746SPaolo Bonzini } 11146e790746SPaolo Bonzini 11156e790746SPaolo Bonzini static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) 11166e790746SPaolo Bonzini { 11176e790746SPaolo Bonzini VirtIONet *n = opaque; 111817a0ca55SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(n); 11196e790746SPaolo Bonzini int ret, i, link_down; 11206e790746SPaolo Bonzini 11216e790746SPaolo Bonzini if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) 11226e790746SPaolo Bonzini return -EINVAL; 11236e790746SPaolo Bonzini 112417a0ca55SKONRAD Frederic ret = virtio_load(vdev, f); 11256e790746SPaolo Bonzini if (ret) { 11266e790746SPaolo Bonzini return ret; 11276e790746SPaolo Bonzini } 11286e790746SPaolo Bonzini 11296e790746SPaolo Bonzini qemu_get_buffer(f, n->mac, ETH_ALEN); 11306e790746SPaolo Bonzini n->vqs[0].tx_waiting = qemu_get_be32(f); 11316e790746SPaolo Bonzini 11326e790746SPaolo Bonzini virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f)); 11336e790746SPaolo Bonzini 11346e790746SPaolo Bonzini if (version_id >= 3) 11356e790746SPaolo Bonzini n->status = qemu_get_be16(f); 11366e790746SPaolo Bonzini 11376e790746SPaolo Bonzini if (version_id >= 4) { 11386e790746SPaolo Bonzini if (version_id < 8) { 11396e790746SPaolo Bonzini n->promisc = qemu_get_be32(f); 11406e790746SPaolo Bonzini n->allmulti = qemu_get_be32(f); 11416e790746SPaolo Bonzini } else { 11426e790746SPaolo Bonzini n->promisc = qemu_get_byte(f); 11436e790746SPaolo Bonzini n->allmulti = qemu_get_byte(f); 11446e790746SPaolo Bonzini } 11456e790746SPaolo Bonzini } 11466e790746SPaolo Bonzini 11476e790746SPaolo Bonzini if (version_id >= 5) { 11486e790746SPaolo Bonzini n->mac_table.in_use = qemu_get_be32(f); 11496e790746SPaolo Bonzini /* MAC_TABLE_ENTRIES may be different from the saved image */ 11506e790746SPaolo Bonzini if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { 11516e790746SPaolo Bonzini qemu_get_buffer(f, n->mac_table.macs, 11526e790746SPaolo Bonzini n->mac_table.in_use * ETH_ALEN); 11536e790746SPaolo Bonzini } else if (n->mac_table.in_use) { 11546e790746SPaolo Bonzini uint8_t *buf = g_malloc0(n->mac_table.in_use); 11556e790746SPaolo Bonzini qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN); 11566e790746SPaolo Bonzini g_free(buf); 11576e790746SPaolo Bonzini n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; 11586e790746SPaolo Bonzini n->mac_table.in_use = 0; 11596e790746SPaolo Bonzini } 11606e790746SPaolo Bonzini } 11616e790746SPaolo Bonzini 11626e790746SPaolo Bonzini if (version_id >= 6) 11636e790746SPaolo Bonzini qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); 11646e790746SPaolo Bonzini 11656e790746SPaolo Bonzini if (version_id >= 7) { 11666e790746SPaolo Bonzini if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { 11676e790746SPaolo Bonzini error_report("virtio-net: saved image requires vnet_hdr=on"); 11686e790746SPaolo Bonzini return -1; 11696e790746SPaolo Bonzini } 11706e790746SPaolo Bonzini 11716e790746SPaolo Bonzini if (n->has_vnet_hdr) { 11726e790746SPaolo Bonzini tap_set_offload(qemu_get_queue(n->nic)->peer, 117317a0ca55SKONRAD Frederic (vdev->guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1, 117417a0ca55SKONRAD Frederic (vdev->guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1, 117517a0ca55SKONRAD Frederic (vdev->guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1, 117617a0ca55SKONRAD Frederic (vdev->guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1, 117717a0ca55SKONRAD Frederic (vdev->guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1); 11786e790746SPaolo Bonzini } 11796e790746SPaolo Bonzini } 11806e790746SPaolo Bonzini 11816e790746SPaolo Bonzini if (version_id >= 9) { 11826e790746SPaolo Bonzini n->mac_table.multi_overflow = qemu_get_byte(f); 11836e790746SPaolo Bonzini n->mac_table.uni_overflow = qemu_get_byte(f); 11846e790746SPaolo Bonzini } 11856e790746SPaolo Bonzini 11866e790746SPaolo Bonzini if (version_id >= 10) { 11876e790746SPaolo Bonzini n->alluni = qemu_get_byte(f); 11886e790746SPaolo Bonzini n->nomulti = qemu_get_byte(f); 11896e790746SPaolo Bonzini n->nouni = qemu_get_byte(f); 11906e790746SPaolo Bonzini n->nobcast = qemu_get_byte(f); 11916e790746SPaolo Bonzini } 11926e790746SPaolo Bonzini 11936e790746SPaolo Bonzini if (version_id >= 11) { 11946e790746SPaolo Bonzini if (qemu_get_byte(f) && !peer_has_ufo(n)) { 11956e790746SPaolo Bonzini error_report("virtio-net: saved image requires TUN_F_UFO support"); 11966e790746SPaolo Bonzini return -1; 11976e790746SPaolo Bonzini } 11986e790746SPaolo Bonzini } 11996e790746SPaolo Bonzini 12006e790746SPaolo Bonzini if (n->max_queues > 1) { 12016e790746SPaolo Bonzini if (n->max_queues != qemu_get_be16(f)) { 12026e790746SPaolo Bonzini error_report("virtio-net: different max_queues "); 12036e790746SPaolo Bonzini return -1; 12046e790746SPaolo Bonzini } 12056e790746SPaolo Bonzini 12066e790746SPaolo Bonzini n->curr_queues = qemu_get_be16(f); 12076e790746SPaolo Bonzini for (i = 1; i < n->curr_queues; i++) { 12086e790746SPaolo Bonzini n->vqs[i].tx_waiting = qemu_get_be32(f); 12096e790746SPaolo Bonzini } 12106e790746SPaolo Bonzini } 12116e790746SPaolo Bonzini 12126e790746SPaolo Bonzini virtio_net_set_queues(n); 12136e790746SPaolo Bonzini 12146e790746SPaolo Bonzini /* Find the first multicast entry in the saved MAC filter */ 12156e790746SPaolo Bonzini for (i = 0; i < n->mac_table.in_use; i++) { 12166e790746SPaolo Bonzini if (n->mac_table.macs[i * ETH_ALEN] & 1) { 12176e790746SPaolo Bonzini break; 12186e790746SPaolo Bonzini } 12196e790746SPaolo Bonzini } 12206e790746SPaolo Bonzini n->mac_table.first_multi = i; 12216e790746SPaolo Bonzini 12226e790746SPaolo Bonzini /* nc.link_down can't be migrated, so infer link_down according 12236e790746SPaolo Bonzini * to link status bit in n->status */ 12246e790746SPaolo Bonzini link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; 12256e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 12266e790746SPaolo Bonzini qemu_get_subqueue(n->nic, i)->link_down = link_down; 12276e790746SPaolo Bonzini } 12286e790746SPaolo Bonzini 12296e790746SPaolo Bonzini return 0; 12306e790746SPaolo Bonzini } 12316e790746SPaolo Bonzini 12326e790746SPaolo Bonzini static void virtio_net_cleanup(NetClientState *nc) 12336e790746SPaolo Bonzini { 12346e790746SPaolo Bonzini VirtIONet *n = qemu_get_nic_opaque(nc); 12356e790746SPaolo Bonzini 12366e790746SPaolo Bonzini n->nic = NULL; 12376e790746SPaolo Bonzini } 12386e790746SPaolo Bonzini 12396e790746SPaolo Bonzini static NetClientInfo net_virtio_info = { 12406e790746SPaolo Bonzini .type = NET_CLIENT_OPTIONS_KIND_NIC, 12416e790746SPaolo Bonzini .size = sizeof(NICState), 12426e790746SPaolo Bonzini .can_receive = virtio_net_can_receive, 12436e790746SPaolo Bonzini .receive = virtio_net_receive, 12446e790746SPaolo Bonzini .cleanup = virtio_net_cleanup, 12456e790746SPaolo Bonzini .link_status_changed = virtio_net_set_link_status, 12466e790746SPaolo Bonzini }; 12476e790746SPaolo Bonzini 12486e790746SPaolo Bonzini static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) 12496e790746SPaolo Bonzini { 125017a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 12516e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 12526e790746SPaolo Bonzini assert(n->vhost_started); 12536e790746SPaolo Bonzini return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx); 12546e790746SPaolo Bonzini } 12556e790746SPaolo Bonzini 12566e790746SPaolo Bonzini static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, 12576e790746SPaolo Bonzini bool mask) 12586e790746SPaolo Bonzini { 125917a0ca55SKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 12606e790746SPaolo Bonzini NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); 12616e790746SPaolo Bonzini assert(n->vhost_started); 12626e790746SPaolo Bonzini vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer), 12636e790746SPaolo Bonzini vdev, idx, mask); 12646e790746SPaolo Bonzini } 12656e790746SPaolo Bonzini 126617ec5a86SKONRAD Frederic void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features) 12676e790746SPaolo Bonzini { 12686e790746SPaolo Bonzini int i, config_size = 0; 12696e790746SPaolo Bonzini for (i = 0; feature_sizes[i].flags != 0; i++) { 12706e790746SPaolo Bonzini if (host_features & feature_sizes[i].flags) { 12716e790746SPaolo Bonzini config_size = MAX(feature_sizes[i].end, config_size); 12726e790746SPaolo Bonzini } 12736e790746SPaolo Bonzini } 127417ec5a86SKONRAD Frederic n->config_size = config_size; 127517ec5a86SKONRAD Frederic } 12766e790746SPaolo Bonzini 12771773d9eeSKONRAD Frederic static int virtio_net_device_init(VirtIODevice *vdev) 127817ec5a86SKONRAD Frederic { 12791773d9eeSKONRAD Frederic int i; 128017ec5a86SKONRAD Frederic 12811773d9eeSKONRAD Frederic DeviceState *qdev = DEVICE(vdev); 12821773d9eeSKONRAD Frederic VirtIONet *n = VIRTIO_NET(vdev); 12831773d9eeSKONRAD Frederic 128417ec5a86SKONRAD Frederic virtio_init(VIRTIO_DEVICE(n), "virtio-net", VIRTIO_ID_NET, 128517ec5a86SKONRAD Frederic n->config_size); 128617ec5a86SKONRAD Frederic 12871773d9eeSKONRAD Frederic n->max_queues = MAX(n->nic_conf.queues, 1); 12886e790746SPaolo Bonzini n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); 128917a0ca55SKONRAD Frederic n->vqs[0].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); 12906e790746SPaolo Bonzini n->curr_queues = 1; 12916e790746SPaolo Bonzini n->vqs[0].n = n; 12921773d9eeSKONRAD Frederic n->tx_timeout = n->net_conf.txtimer; 12936e790746SPaolo Bonzini 12941773d9eeSKONRAD Frederic if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") 12951773d9eeSKONRAD Frederic && strcmp(n->net_conf.tx, "bh")) { 12966e790746SPaolo Bonzini error_report("virtio-net: " 12976e790746SPaolo Bonzini "Unknown option tx=%s, valid options: \"timer\" \"bh\"", 12981773d9eeSKONRAD Frederic n->net_conf.tx); 12996e790746SPaolo Bonzini error_report("Defaulting to \"bh\""); 13006e790746SPaolo Bonzini } 13016e790746SPaolo Bonzini 13021773d9eeSKONRAD Frederic if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { 130317a0ca55SKONRAD Frederic n->vqs[0].tx_vq = virtio_add_queue(vdev, 256, 13046e790746SPaolo Bonzini virtio_net_handle_tx_timer); 13056e790746SPaolo Bonzini n->vqs[0].tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, 13066e790746SPaolo Bonzini &n->vqs[0]); 13076e790746SPaolo Bonzini } else { 130817a0ca55SKONRAD Frederic n->vqs[0].tx_vq = virtio_add_queue(vdev, 256, 13096e790746SPaolo Bonzini virtio_net_handle_tx_bh); 13106e790746SPaolo Bonzini n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]); 13116e790746SPaolo Bonzini } 131217a0ca55SKONRAD Frederic n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); 13131773d9eeSKONRAD Frederic qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); 13141773d9eeSKONRAD Frederic memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); 13156e790746SPaolo Bonzini n->status = VIRTIO_NET_S_LINK_UP; 13166e790746SPaolo Bonzini 13171773d9eeSKONRAD Frederic n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, 13181773d9eeSKONRAD Frederic object_get_typename(OBJECT(qdev)), qdev->id, n); 13196e790746SPaolo Bonzini peer_test_vnet_hdr(n); 13206e790746SPaolo Bonzini if (peer_has_vnet_hdr(n)) { 13216e790746SPaolo Bonzini for (i = 0; i < n->max_queues; i++) { 13226e790746SPaolo Bonzini tap_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); 13236e790746SPaolo Bonzini } 13246e790746SPaolo Bonzini n->host_hdr_len = sizeof(struct virtio_net_hdr); 13256e790746SPaolo Bonzini } else { 13266e790746SPaolo Bonzini n->host_hdr_len = 0; 13276e790746SPaolo Bonzini } 13286e790746SPaolo Bonzini 13291773d9eeSKONRAD Frederic qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); 13306e790746SPaolo Bonzini 13316e790746SPaolo Bonzini n->vqs[0].tx_waiting = 0; 13321773d9eeSKONRAD Frederic n->tx_burst = n->net_conf.txburst; 13336e790746SPaolo Bonzini virtio_net_set_mrg_rx_bufs(n, 0); 13346e790746SPaolo Bonzini n->promisc = 1; /* for compatibility */ 13356e790746SPaolo Bonzini 13366e790746SPaolo Bonzini n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); 13376e790746SPaolo Bonzini 13386e790746SPaolo Bonzini n->vlans = g_malloc0(MAX_VLAN >> 3); 13396e790746SPaolo Bonzini 13401773d9eeSKONRAD Frederic n->qdev = qdev; 13411773d9eeSKONRAD Frederic register_savevm(qdev, "virtio-net", -1, VIRTIO_NET_VM_VERSION, 13426e790746SPaolo Bonzini virtio_net_save, virtio_net_load, n); 13436e790746SPaolo Bonzini 13441773d9eeSKONRAD Frederic add_boot_device_path(n->nic_conf.bootindex, qdev, "/ethernet-phy@0"); 134517ec5a86SKONRAD Frederic return 0; 134617ec5a86SKONRAD Frederic } 134717ec5a86SKONRAD Frederic 134817ec5a86SKONRAD Frederic static int virtio_net_device_exit(DeviceState *qdev) 134917ec5a86SKONRAD Frederic { 135017ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(qdev); 135117ec5a86SKONRAD Frederic VirtIODevice *vdev = VIRTIO_DEVICE(qdev); 135217ec5a86SKONRAD Frederic int i; 135317ec5a86SKONRAD Frederic 135417ec5a86SKONRAD Frederic /* This will stop vhost backend if appropriate. */ 135517ec5a86SKONRAD Frederic virtio_net_set_status(vdev, 0); 135617ec5a86SKONRAD Frederic 135717ec5a86SKONRAD Frederic unregister_savevm(qdev, "virtio-net", n); 135817ec5a86SKONRAD Frederic 135917ec5a86SKONRAD Frederic g_free(n->mac_table.macs); 136017ec5a86SKONRAD Frederic g_free(n->vlans); 136117ec5a86SKONRAD Frederic 136217ec5a86SKONRAD Frederic for (i = 0; i < n->max_queues; i++) { 136317ec5a86SKONRAD Frederic VirtIONetQueue *q = &n->vqs[i]; 136417ec5a86SKONRAD Frederic NetClientState *nc = qemu_get_subqueue(n->nic, i); 136517ec5a86SKONRAD Frederic 136617ec5a86SKONRAD Frederic qemu_purge_queued_packets(nc); 136717ec5a86SKONRAD Frederic 136817ec5a86SKONRAD Frederic if (q->tx_timer) { 136917ec5a86SKONRAD Frederic qemu_del_timer(q->tx_timer); 137017ec5a86SKONRAD Frederic qemu_free_timer(q->tx_timer); 137117ec5a86SKONRAD Frederic } else { 137217ec5a86SKONRAD Frederic qemu_bh_delete(q->tx_bh); 137317ec5a86SKONRAD Frederic } 137417ec5a86SKONRAD Frederic } 137517ec5a86SKONRAD Frederic 137617ec5a86SKONRAD Frederic g_free(n->vqs); 137717ec5a86SKONRAD Frederic qemu_del_nic(n->nic); 13786a1a8cc7SKONRAD Frederic virtio_cleanup(vdev); 137917ec5a86SKONRAD Frederic 138017ec5a86SKONRAD Frederic return 0; 138117ec5a86SKONRAD Frederic } 138217ec5a86SKONRAD Frederic 138317ec5a86SKONRAD Frederic static void virtio_net_instance_init(Object *obj) 138417ec5a86SKONRAD Frederic { 138517ec5a86SKONRAD Frederic VirtIONet *n = VIRTIO_NET(obj); 138617ec5a86SKONRAD Frederic 138717ec5a86SKONRAD Frederic /* 138817ec5a86SKONRAD Frederic * The default config_size is sizeof(struct virtio_net_config). 138917ec5a86SKONRAD Frederic * Can be overriden with virtio_net_set_config_size. 139017ec5a86SKONRAD Frederic */ 139117ec5a86SKONRAD Frederic n->config_size = sizeof(struct virtio_net_config); 139217ec5a86SKONRAD Frederic } 139317ec5a86SKONRAD Frederic 139417ec5a86SKONRAD Frederic static Property virtio_net_properties[] = { 139517ec5a86SKONRAD Frederic DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), 139617ec5a86SKONRAD Frederic DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, 139717ec5a86SKONRAD Frederic TX_TIMER_INTERVAL), 139817ec5a86SKONRAD Frederic DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), 139917ec5a86SKONRAD Frederic DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), 140017ec5a86SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 140117ec5a86SKONRAD Frederic }; 140217ec5a86SKONRAD Frederic 140317ec5a86SKONRAD Frederic static void virtio_net_class_init(ObjectClass *klass, void *data) 140417ec5a86SKONRAD Frederic { 140517ec5a86SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 140617ec5a86SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 140717ec5a86SKONRAD Frederic dc->exit = virtio_net_device_exit; 140817ec5a86SKONRAD Frederic dc->props = virtio_net_properties; 140917ec5a86SKONRAD Frederic vdc->init = virtio_net_device_init; 141017ec5a86SKONRAD Frederic vdc->get_config = virtio_net_get_config; 141117ec5a86SKONRAD Frederic vdc->set_config = virtio_net_set_config; 141217ec5a86SKONRAD Frederic vdc->get_features = virtio_net_get_features; 141317ec5a86SKONRAD Frederic vdc->set_features = virtio_net_set_features; 141417ec5a86SKONRAD Frederic vdc->bad_features = virtio_net_bad_features; 141517ec5a86SKONRAD Frederic vdc->reset = virtio_net_reset; 141617ec5a86SKONRAD Frederic vdc->set_status = virtio_net_set_status; 141717ec5a86SKONRAD Frederic vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; 141817ec5a86SKONRAD Frederic vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; 141917ec5a86SKONRAD Frederic } 142017ec5a86SKONRAD Frederic 142117ec5a86SKONRAD Frederic static const TypeInfo virtio_net_info = { 142217ec5a86SKONRAD Frederic .name = TYPE_VIRTIO_NET, 142317ec5a86SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 142417ec5a86SKONRAD Frederic .instance_size = sizeof(VirtIONet), 142517ec5a86SKONRAD Frederic .instance_init = virtio_net_instance_init, 142617ec5a86SKONRAD Frederic .class_init = virtio_net_class_init, 142717ec5a86SKONRAD Frederic }; 142817ec5a86SKONRAD Frederic 142917ec5a86SKONRAD Frederic static void virtio_register_types(void) 143017ec5a86SKONRAD Frederic { 143117ec5a86SKONRAD Frederic type_register_static(&virtio_net_info); 143217ec5a86SKONRAD Frederic } 143317ec5a86SKONRAD Frederic 143417ec5a86SKONRAD Frederic type_init(virtio_register_types) 1435