aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Virtio Network Device |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2007 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 14 | #include "qemu/iov.h" |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 15 | #include "virtio.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 16 | #include "net/net.h" |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 17 | #include "net/checksum.h" |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 18 | #include "net/tap.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/error-report.h" |
| 20 | #include "qemu/timer.h" |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 21 | #include "virtio-net.h" |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 22 | #include "vhost_net.h" |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 23 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 24 | #define VIRTIO_NET_VM_VERSION 11 |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 25 | |
Alex Williamson | 4ffb17f | 2009-06-05 14:47:23 -0600 | [diff] [blame] | 26 | #define MAC_TABLE_ENTRIES 64 |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 27 | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 28 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 29 | typedef struct VirtIONetQueue { |
| 30 | VirtQueue *rx_vq; |
| 31 | VirtQueue *tx_vq; |
| 32 | QEMUTimer *tx_timer; |
| 33 | QEMUBH *tx_bh; |
| 34 | int tx_waiting; |
| 35 | struct { |
| 36 | VirtQueueElement elem; |
| 37 | ssize_t len; |
| 38 | } async_tx; |
| 39 | struct VirtIONet *n; |
| 40 | } VirtIONetQueue; |
| 41 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 42 | typedef struct VirtIONet |
| 43 | { |
| 44 | VirtIODevice vdev; |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 45 | uint8_t mac[ETH_ALEN]; |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 46 | uint16_t status; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 47 | VirtIONetQueue vq; |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 48 | VirtQueue *ctrl_vq; |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 49 | NICState *nic; |
Alex Williamson | f0c07c7 | 2010-09-02 09:00:50 -0600 | [diff] [blame] | 50 | uint32_t tx_timeout; |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 51 | int32_t tx_burst; |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 52 | uint32_t has_vnet_hdr; |
Michael S. Tsirkin | e35e23f | 2012-09-24 12:12:25 +0200 | [diff] [blame] | 53 | size_t host_hdr_len; |
| 54 | size_t guest_hdr_len; |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 55 | uint8_t has_ufo; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 56 | int mergeable_rx_bufs; |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 57 | uint8_t promisc; |
| 58 | uint8_t allmulti; |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 59 | uint8_t alluni; |
| 60 | uint8_t nomulti; |
| 61 | uint8_t nouni; |
| 62 | uint8_t nobcast; |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 63 | uint8_t vhost_started; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 64 | struct { |
| 65 | int in_use; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 66 | int first_multi; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 67 | uint8_t multi_overflow; |
| 68 | uint8_t uni_overflow; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 69 | uint8_t *macs; |
| 70 | } mac_table; |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 71 | uint32_t *vlans; |
Alex Williamson | 01657c8 | 2010-06-25 11:09:28 -0600 | [diff] [blame] | 72 | DeviceState *qdev; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 73 | } VirtIONet; |
| 74 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 75 | static VirtIONetQueue *virtio_net_get_queue(NetClientState *nc) |
| 76 | { |
| 77 | VirtIONet *n = qemu_get_nic_opaque(nc); |
| 78 | |
| 79 | return &n->vq; |
| 80 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 81 | /* TODO |
| 82 | * - we could suppress RX interrupt if we were so inclined. |
| 83 | */ |
| 84 | |
| 85 | static VirtIONet *to_virtio_net(VirtIODevice *vdev) |
| 86 | { |
| 87 | return (VirtIONet *)vdev; |
| 88 | } |
| 89 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 90 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 91 | { |
| 92 | VirtIONet *n = to_virtio_net(vdev); |
| 93 | struct virtio_net_config netcfg; |
| 94 | |
Stefan Hajnoczi | b46d97f | 2011-03-03 21:42:28 +0000 | [diff] [blame] | 95 | stw_p(&netcfg.status, n->status); |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 96 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 97 | memcpy(config, &netcfg, sizeof(netcfg)); |
| 98 | } |
| 99 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 100 | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 101 | { |
| 102 | VirtIONet *n = to_virtio_net(vdev); |
| 103 | struct virtio_net_config netcfg; |
| 104 | |
| 105 | memcpy(&netcfg, config, sizeof(netcfg)); |
| 106 | |
Amos Kong | c1943a3 | 2013-01-22 23:44:45 +0800 | [diff] [blame] | 107 | if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) && |
| 108 | memcmp(netcfg.mac, n->mac, ETH_ALEN)) { |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 109 | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 110 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 114 | static bool virtio_net_started(VirtIONet *n, uint8_t status) |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 115 | { |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 116 | return (status & VIRTIO_CONFIG_S_DRIVER_OK) && |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 117 | (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running; |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) |
| 121 | { |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 122 | NetClientState *nc = qemu_get_queue(n->nic); |
| 123 | |
| 124 | if (!nc->peer) { |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 125 | return; |
| 126 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 127 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 131 | if (!tap_get_vhost_net(nc->peer)) { |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 132 | return; |
| 133 | } |
Michael S. Tsirkin | 3299369 | 2011-02-09 18:45:09 +0200 | [diff] [blame] | 134 | if (!!n->vhost_started == virtio_net_started(n, status) && |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 135 | !nc->peer->link_down) { |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | if (!n->vhost_started) { |
mst@redhat.com | 5430a28 | 2011-02-01 22:13:42 +0200 | [diff] [blame] | 139 | int r; |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 140 | if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) { |
mst@redhat.com | 5430a28 | 2011-02-01 22:13:42 +0200 | [diff] [blame] | 141 | return; |
| 142 | } |
Michael S. Tsirkin | 1830b80 | 2012-12-25 17:38:59 +0200 | [diff] [blame] | 143 | n->vhost_started = 1; |
Jason Wang | a9f98bb | 2013-01-30 19:12:35 +0800 | [diff] [blame] | 144 | r = vhost_net_start(&n->vdev, nc, 1); |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 145 | if (r < 0) { |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 146 | error_report("unable to start vhost net: %d: " |
| 147 | "falling back on userspace virtio", -r); |
Michael S. Tsirkin | 1830b80 | 2012-12-25 17:38:59 +0200 | [diff] [blame] | 148 | n->vhost_started = 0; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 149 | } else { |
| 150 | n->vhost_started = 1; |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 151 | } |
| 152 | } else { |
Jason Wang | a9f98bb | 2013-01-30 19:12:35 +0800 | [diff] [blame] | 153 | vhost_net_stop(&n->vdev, nc, 1); |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 154 | n->vhost_started = 0; |
| 155 | } |
| 156 | } |
| 157 | |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 158 | static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) |
| 159 | { |
| 160 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 161 | VirtIONetQueue *q = &n->vq; |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 162 | |
| 163 | virtio_net_vhost_status(n, status); |
| 164 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 165 | if (!q->tx_waiting) { |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (virtio_net_started(n, status) && !n->vhost_started) { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 170 | if (q->tx_timer) { |
| 171 | qemu_mod_timer(q->tx_timer, |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 172 | qemu_get_clock_ns(vm_clock) + n->tx_timeout); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 173 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 174 | qemu_bh_schedule(q->tx_bh); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 175 | } |
| 176 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 177 | if (q->tx_timer) { |
| 178 | qemu_del_timer(q->tx_timer); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 179 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 180 | qemu_bh_cancel(q->tx_bh); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 185 | static void virtio_net_set_link_status(NetClientState *nc) |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 186 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 187 | VirtIONet *n = qemu_get_nic_opaque(nc); |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 188 | uint16_t old_status = n->status; |
| 189 | |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 190 | if (nc->link_down) |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 191 | n->status &= ~VIRTIO_NET_S_LINK_UP; |
| 192 | else |
| 193 | n->status |= VIRTIO_NET_S_LINK_UP; |
| 194 | |
| 195 | if (n->status != old_status) |
| 196 | virtio_notify_config(&n->vdev); |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 197 | |
| 198 | virtio_net_set_status(&n->vdev, n->vdev.status); |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 199 | } |
| 200 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 201 | static void virtio_net_reset(VirtIODevice *vdev) |
| 202 | { |
| 203 | VirtIONet *n = to_virtio_net(vdev); |
| 204 | |
| 205 | /* Reset back to compatibility mode */ |
| 206 | n->promisc = 1; |
| 207 | n->allmulti = 0; |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 208 | n->alluni = 0; |
| 209 | n->nomulti = 0; |
| 210 | n->nouni = 0; |
| 211 | n->nobcast = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 212 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 213 | /* Flush any MAC and VLAN filter table state */ |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 214 | n->mac_table.in_use = 0; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 215 | n->mac_table.first_multi = 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 216 | n->mac_table.multi_overflow = 0; |
| 217 | n->mac_table.uni_overflow = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 218 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
Michael S. Tsirkin | 41dc8a6 | 2013-01-16 11:37:40 +0200 | [diff] [blame] | 219 | memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 220 | memset(n->vlans, 0, MAX_VLAN >> 3); |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 223 | static void peer_test_vnet_hdr(VirtIONet *n) |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 224 | { |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 225 | NetClientState *nc = qemu_get_queue(n->nic); |
| 226 | if (!nc->peer) { |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 227 | return; |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 228 | } |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 229 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 230 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 231 | return; |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 232 | } |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 233 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 234 | n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer); |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 235 | } |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 236 | |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 237 | static int peer_has_vnet_hdr(VirtIONet *n) |
| 238 | { |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 239 | return n->has_vnet_hdr; |
| 240 | } |
| 241 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 242 | static int peer_has_ufo(VirtIONet *n) |
| 243 | { |
| 244 | if (!peer_has_vnet_hdr(n)) |
| 245 | return 0; |
| 246 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 247 | n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer); |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 248 | |
| 249 | return n->has_ufo; |
| 250 | } |
| 251 | |
Michael S. Tsirkin | ff3a806 | 2012-09-24 21:05:03 +0200 | [diff] [blame] | 252 | static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs) |
| 253 | { |
| 254 | n->mergeable_rx_bufs = mergeable_rx_bufs; |
| 255 | |
| 256 | n->guest_hdr_len = n->mergeable_rx_bufs ? |
| 257 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); |
| 258 | |
| 259 | if (peer_has_vnet_hdr(n) && |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 260 | tap_has_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len)) { |
| 261 | tap_set_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len); |
Michael S. Tsirkin | ff3a806 | 2012-09-24 21:05:03 +0200 | [diff] [blame] | 262 | n->host_hdr_len = n->guest_hdr_len; |
| 263 | } |
| 264 | } |
| 265 | |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 266 | static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 267 | { |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 268 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 269 | NetClientState *nc = qemu_get_queue(n->nic); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 270 | |
Michael S. Tsirkin | c9f79a3 | 2010-01-12 20:50:17 +0200 | [diff] [blame] | 271 | features |= (1 << VIRTIO_NET_F_MAC); |
| 272 | |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 273 | if (!peer_has_vnet_hdr(n)) { |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 274 | features &= ~(0x1 << VIRTIO_NET_F_CSUM); |
| 275 | features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4); |
| 276 | features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6); |
| 277 | features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 278 | |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 279 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM); |
| 280 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4); |
| 281 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6); |
| 282 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN); |
| 283 | } |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 284 | |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 285 | if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { |
| 286 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO); |
| 287 | features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 290 | if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 291 | return features; |
| 292 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 293 | if (!tap_get_vhost_net(nc->peer)) { |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 294 | return features; |
| 295 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 296 | return vhost_net_get_features(tap_get_vhost_net(nc->peer), features); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 297 | } |
| 298 | |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 299 | static uint32_t virtio_net_bad_features(VirtIODevice *vdev) |
| 300 | { |
| 301 | uint32_t features = 0; |
| 302 | |
| 303 | /* Linux kernel 2.6.25. It understood MAC (as everyone must), |
| 304 | * but also these: */ |
| 305 | features |= (1 << VIRTIO_NET_F_MAC); |
Dustin Kirkland | 184bd04 | 2009-10-29 10:34:15 -0500 | [diff] [blame] | 306 | features |= (1 << VIRTIO_NET_F_CSUM); |
| 307 | features |= (1 << VIRTIO_NET_F_HOST_TSO4); |
| 308 | features |= (1 << VIRTIO_NET_F_HOST_TSO6); |
| 309 | features |= (1 << VIRTIO_NET_F_HOST_ECN); |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 310 | |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 311 | return features; |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 312 | } |
| 313 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 314 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
| 315 | { |
| 316 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 317 | NetClientState *nc = qemu_get_queue(n->nic); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 318 | |
Michael S. Tsirkin | ff3a806 | 2012-09-24 21:05:03 +0200 | [diff] [blame] | 319 | virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF))); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 320 | |
| 321 | if (n->has_vnet_hdr) { |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 322 | tap_set_offload(nc->peer, |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 323 | (features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
| 324 | (features >> VIRTIO_NET_F_GUEST_TSO4) & 1, |
| 325 | (features >> VIRTIO_NET_F_GUEST_TSO6) & 1, |
Sridhar Samudrala | 6c9f58b | 2009-10-22 17:43:49 +0100 | [diff] [blame] | 326 | (features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
| 327 | (features >> VIRTIO_NET_F_GUEST_UFO) & 1); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 328 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 329 | if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
David L Stevens | dc14a39 | 2010-03-31 21:20:31 +0300 | [diff] [blame] | 330 | return; |
| 331 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 332 | if (!tap_get_vhost_net(nc->peer)) { |
David L Stevens | dc14a39 | 2010-03-31 21:20:31 +0300 | [diff] [blame] | 333 | return; |
| 334 | } |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 335 | vhost_net_ack_features(tap_get_vhost_net(nc->peer), features); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 336 | } |
| 337 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 338 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 339 | struct iovec *iov, unsigned int iov_cnt) |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 340 | { |
| 341 | uint8_t on; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 342 | size_t s; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 343 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 344 | s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); |
| 345 | if (s != sizeof(on)) { |
| 346 | return VIRTIO_NET_ERR; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 349 | if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 350 | n->promisc = on; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 351 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 352 | n->allmulti = on; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 353 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 354 | n->alluni = on; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 355 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 356 | n->nomulti = on; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 357 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 358 | n->nouni = on; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 359 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 360 | n->nobcast = on; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 361 | } else { |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 362 | return VIRTIO_NET_ERR; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 363 | } |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 364 | |
| 365 | return VIRTIO_NET_OK; |
| 366 | } |
| 367 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 368 | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 369 | struct iovec *iov, unsigned int iov_cnt) |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 370 | { |
| 371 | struct virtio_net_ctrl_mac mac_data; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 372 | size_t s; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 373 | |
Amos Kong | c1943a3 | 2013-01-22 23:44:45 +0800 | [diff] [blame] | 374 | if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { |
| 375 | if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { |
| 376 | return VIRTIO_NET_ERR; |
| 377 | } |
| 378 | s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); |
| 379 | assert(s == sizeof(n->mac)); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 380 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); |
Amos Kong | c1943a3 | 2013-01-22 23:44:45 +0800 | [diff] [blame] | 381 | return VIRTIO_NET_OK; |
| 382 | } |
| 383 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 384 | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 385 | return VIRTIO_NET_ERR; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 386 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 387 | |
| 388 | n->mac_table.in_use = 0; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 389 | n->mac_table.first_multi = 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 390 | n->mac_table.uni_overflow = 0; |
| 391 | n->mac_table.multi_overflow = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 392 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
| 393 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 394 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
| 395 | sizeof(mac_data.entries)); |
| 396 | mac_data.entries = ldl_p(&mac_data.entries); |
| 397 | if (s != sizeof(mac_data.entries)) { |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 398 | return VIRTIO_NET_ERR; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 399 | } |
| 400 | iov_discard_front(&iov, &iov_cnt, s); |
| 401 | |
| 402 | if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { |
| 403 | return VIRTIO_NET_ERR; |
| 404 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 405 | |
| 406 | if (mac_data.entries <= MAC_TABLE_ENTRIES) { |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 407 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, |
| 408 | mac_data.entries * ETH_ALEN); |
| 409 | if (s != mac_data.entries * ETH_ALEN) { |
| 410 | return VIRTIO_NET_ERR; |
| 411 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 412 | n->mac_table.in_use += mac_data.entries; |
| 413 | } else { |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 414 | n->mac_table.uni_overflow = 1; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 417 | iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); |
| 418 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 419 | n->mac_table.first_multi = n->mac_table.in_use; |
| 420 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 421 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
| 422 | sizeof(mac_data.entries)); |
| 423 | mac_data.entries = ldl_p(&mac_data.entries); |
| 424 | if (s != sizeof(mac_data.entries)) { |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 425 | return VIRTIO_NET_ERR; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 426 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 427 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 428 | iov_discard_front(&iov, &iov_cnt, s); |
| 429 | |
| 430 | if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { |
| 431 | return VIRTIO_NET_ERR; |
| 432 | } |
| 433 | |
| 434 | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { |
| 435 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, |
| 436 | mac_data.entries * ETH_ALEN); |
| 437 | if (s != mac_data.entries * ETH_ALEN) { |
| 438 | return VIRTIO_NET_ERR; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 439 | } |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 440 | n->mac_table.in_use += mac_data.entries; |
| 441 | } else { |
| 442 | n->mac_table.multi_overflow = 1; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return VIRTIO_NET_OK; |
| 446 | } |
| 447 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 448 | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 449 | struct iovec *iov, unsigned int iov_cnt) |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 450 | { |
| 451 | uint16_t vid; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 452 | size_t s; |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 453 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 454 | s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); |
| 455 | vid = lduw_p(&vid); |
| 456 | if (s != sizeof(vid)) { |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 457 | return VIRTIO_NET_ERR; |
| 458 | } |
| 459 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 460 | if (vid >= MAX_VLAN) |
| 461 | return VIRTIO_NET_ERR; |
| 462 | |
| 463 | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) |
| 464 | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); |
| 465 | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) |
| 466 | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); |
| 467 | else |
| 468 | return VIRTIO_NET_ERR; |
| 469 | |
| 470 | return VIRTIO_NET_OK; |
| 471 | } |
| 472 | |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 473 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
| 474 | { |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 475 | VirtIONet *n = to_virtio_net(vdev); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 476 | struct virtio_net_ctrl_hdr ctrl; |
| 477 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 478 | VirtQueueElement elem; |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 479 | size_t s; |
| 480 | struct iovec *iov; |
| 481 | unsigned int iov_cnt; |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 482 | |
| 483 | while (virtqueue_pop(vq, &elem)) { |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 484 | if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) || |
| 485 | iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) { |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 486 | error_report("virtio-net ctrl missing headers"); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 487 | exit(1); |
| 488 | } |
| 489 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 490 | iov = elem.out_sg; |
| 491 | iov_cnt = elem.out_num; |
| 492 | s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); |
| 493 | iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); |
| 494 | if (s != sizeof(ctrl)) { |
| 495 | status = VIRTIO_NET_ERR; |
Amos Kong | dd23454 | 2013-01-22 23:44:46 +0800 | [diff] [blame] | 496 | } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 497 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); |
| 498 | } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { |
| 499 | status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); |
| 500 | } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { |
| 501 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Michael S. Tsirkin | 921ac5d | 2013-01-22 23:44:44 +0800 | [diff] [blame] | 504 | s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status)); |
| 505 | assert(s == sizeof(status)); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 506 | |
| 507 | virtqueue_push(vq, &elem, sizeof(status)); |
| 508 | virtio_notify(vdev, vq); |
| 509 | } |
| 510 | } |
| 511 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 512 | /* RX */ |
| 513 | |
| 514 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) |
| 515 | { |
Mark McLoughlin | 8aeff62 | 2009-04-29 13:40:02 +0100 | [diff] [blame] | 516 | VirtIONet *n = to_virtio_net(vdev); |
| 517 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 518 | qemu_flush_queued_packets(qemu_get_queue(n->nic)); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 521 | static int virtio_net_can_receive(NetClientState *nc) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 522 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 523 | VirtIONet *n = qemu_get_nic_opaque(nc); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 524 | VirtIONetQueue *q = virtio_net_get_queue(nc); |
| 525 | |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 526 | if (!n->vdev.vm_running) { |
Michael S. Tsirkin | 9547732 | 2010-11-22 19:52:19 +0200 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Mark McLoughlin | cdd5cc1 | 2009-10-27 18:16:38 +0000 | [diff] [blame] | 529 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 530 | if (!virtio_queue_ready(q->rx_vq) || |
| 531 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 532 | return 0; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 533 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 534 | |
Mark McLoughlin | cdd5cc1 | 2009-10-27 18:16:38 +0000 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 538 | static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) |
Mark McLoughlin | cdd5cc1 | 2009-10-27 18:16:38 +0000 | [diff] [blame] | 539 | { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 540 | VirtIONet *n = q->n; |
| 541 | if (virtio_queue_empty(q->rx_vq) || |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 542 | (n->mergeable_rx_bufs && |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 543 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
| 544 | virtio_queue_set_notification(q->rx_vq, 1); |
Tom Lendacky | 06b1297 | 2010-02-08 10:10:01 -0600 | [diff] [blame] | 545 | |
| 546 | /* To avoid a race condition where the guest has made some buffers |
| 547 | * available after the above check but before notification was |
| 548 | * enabled, check for available buffers again. |
| 549 | */ |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 550 | if (virtio_queue_empty(q->rx_vq) || |
Tom Lendacky | 06b1297 | 2010-02-08 10:10:01 -0600 | [diff] [blame] | 551 | (n->mergeable_rx_bufs && |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 552 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
Tom Lendacky | 06b1297 | 2010-02-08 10:10:01 -0600 | [diff] [blame] | 553 | return 0; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 554 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 557 | virtio_queue_set_notification(q->rx_vq, 0); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 558 | return 1; |
| 559 | } |
| 560 | |
Anthony Liguori | 1d41b0c | 2009-10-22 17:43:48 +0100 | [diff] [blame] | 561 | /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so |
| 562 | * it never finds out that the packets don't have valid checksums. This |
| 563 | * causes dhclient to get upset. Fedora's carried a patch for ages to |
| 564 | * fix this with Xen but it hasn't appeared in an upstream release of |
| 565 | * dhclient yet. |
| 566 | * |
| 567 | * To avoid breaking existing guests, we catch udp packets and add |
| 568 | * checksums. This is terrible but it's better than hacking the guest |
| 569 | * kernels. |
| 570 | * |
| 571 | * N.B. if we introduce a zero-copy API, this operation is no longer free so |
| 572 | * we should provide a mechanism to disable it to avoid polluting the host |
| 573 | * cache. |
| 574 | */ |
| 575 | static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 576 | uint8_t *buf, size_t size) |
Anthony Liguori | 1d41b0c | 2009-10-22 17:43:48 +0100 | [diff] [blame] | 577 | { |
| 578 | if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ |
| 579 | (size > 27 && size < 1500) && /* normal sized MTU */ |
| 580 | (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ |
| 581 | (buf[23] == 17) && /* ip.protocol == UDP */ |
| 582 | (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 583 | net_checksum_calculate(buf, size); |
Anthony Liguori | 1d41b0c | 2009-10-22 17:43:48 +0100 | [diff] [blame] | 584 | hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 585 | } |
| 586 | } |
| 587 | |
Michael S. Tsirkin | 280598b | 2012-09-24 13:24:17 +0200 | [diff] [blame] | 588 | static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, |
| 589 | const void *buf, size_t size) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 590 | { |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 591 | if (n->has_vnet_hdr) { |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 592 | /* FIXME this cast is evil */ |
| 593 | void *wbuf = (void *)buf; |
Michael S. Tsirkin | 280598b | 2012-09-24 13:24:17 +0200 | [diff] [blame] | 594 | work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, |
| 595 | size - n->host_hdr_len); |
| 596 | iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 597 | } else { |
| 598 | struct virtio_net_hdr hdr = { |
| 599 | .flags = 0, |
| 600 | .gso_type = VIRTIO_NET_HDR_GSO_NONE |
| 601 | }; |
| 602 | iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 603 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 604 | } |
| 605 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 606 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
| 607 | { |
| 608 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 609 | static const uint8_t vlan[] = {0x81, 0x00}; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 610 | uint8_t *ptr = (uint8_t *)buf; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 611 | int i; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 612 | |
| 613 | if (n->promisc) |
| 614 | return 1; |
| 615 | |
Michael S. Tsirkin | e043ebc | 2012-09-24 16:27:27 +0200 | [diff] [blame] | 616 | ptr += n->host_hdr_len; |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 617 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 618 | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
| 619 | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; |
| 620 | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) |
| 621 | return 0; |
| 622 | } |
| 623 | |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 624 | if (ptr[0] & 1) { // multicast |
| 625 | if (!memcmp(ptr, bcast, sizeof(bcast))) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 626 | return !n->nobcast; |
| 627 | } else if (n->nomulti) { |
| 628 | return 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 629 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 630 | return 1; |
| 631 | } |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 632 | |
| 633 | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { |
| 634 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { |
| 635 | return 1; |
| 636 | } |
| 637 | } |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 638 | } else { // unicast |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 639 | if (n->nouni) { |
| 640 | return 0; |
| 641 | } else if (n->alluni || n->mac_table.uni_overflow) { |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 642 | return 1; |
| 643 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 644 | return 1; |
| 645 | } |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 646 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 647 | for (i = 0; i < n->mac_table.first_multi; i++) { |
| 648 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { |
| 649 | return 1; |
| 650 | } |
| 651 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 652 | } |
| 653 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 657 | static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 658 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 659 | VirtIONet *n = qemu_get_nic_opaque(nc); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 660 | VirtIONetQueue *q = virtio_net_get_queue(nc); |
Michael S. Tsirkin | 63c5872 | 2012-09-24 13:17:13 +0200 | [diff] [blame] | 661 | struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; |
| 662 | struct virtio_net_hdr_mrg_rxbuf mhdr; |
| 663 | unsigned mhdr_cnt = 0; |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 664 | size_t offset, i, guest_offset; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 665 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 666 | if (!virtio_net_can_receive(qemu_get_queue(n->nic))) { |
Mark McLoughlin | cdd5cc1 | 2009-10-27 18:16:38 +0000 | [diff] [blame] | 667 | return -1; |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 668 | } |
Mark McLoughlin | cdd5cc1 | 2009-10-27 18:16:38 +0000 | [diff] [blame] | 669 | |
Michael S. Tsirkin | 940cda9 | 2010-06-06 18:53:10 +0300 | [diff] [blame] | 670 | /* hdr_len refers to the header we supply to the guest */ |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 671 | if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { |
Mark McLoughlin | 8aeff62 | 2009-04-29 13:40:02 +0100 | [diff] [blame] | 672 | return 0; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 673 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 674 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 675 | if (!receive_filter(n, buf, size)) |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 676 | return size; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 677 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 678 | offset = i = 0; |
| 679 | |
| 680 | while (offset < size) { |
| 681 | VirtQueueElement elem; |
| 682 | int len, total; |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 683 | const struct iovec *sg = elem.in_sg; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 684 | |
Amit Shah | 22c253d | 2010-01-13 16:24:43 +0530 | [diff] [blame] | 685 | total = 0; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 686 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 687 | if (virtqueue_pop(q->rx_vq, &elem) == 0) { |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 688 | if (i == 0) |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 689 | return -1; |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 690 | error_report("virtio-net unexpected empty queue: " |
Michael S. Tsirkin | 279a425 | 2010-06-22 16:22:49 +0300 | [diff] [blame] | 691 | "i %zd mergeable %d offset %zd, size %zd, " |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 692 | "guest hdr len %zd, host hdr len %zd guest features 0x%x", |
Michael S. Tsirkin | 279a425 | 2010-06-22 16:22:49 +0300 | [diff] [blame] | 693 | i, n->mergeable_rx_bufs, offset, size, |
Michael S. Tsirkin | e35e23f | 2012-09-24 12:12:25 +0200 | [diff] [blame] | 694 | n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 695 | exit(1); |
| 696 | } |
| 697 | |
| 698 | if (elem.in_num < 1) { |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 699 | error_report("virtio-net receive queue contains no in buffers"); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 700 | exit(1); |
| 701 | } |
| 702 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 703 | if (i == 0) { |
Michael S. Tsirkin | c8d28e7 | 2012-09-24 13:26:55 +0200 | [diff] [blame] | 704 | assert(offset == 0); |
Michael S. Tsirkin | 63c5872 | 2012-09-24 13:17:13 +0200 | [diff] [blame] | 705 | if (n->mergeable_rx_bufs) { |
| 706 | mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), |
| 707 | sg, elem.in_num, |
| 708 | offsetof(typeof(mhdr), num_buffers), |
| 709 | sizeof(mhdr.num_buffers)); |
| 710 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 711 | |
Michael S. Tsirkin | c8d28e7 | 2012-09-24 13:26:55 +0200 | [diff] [blame] | 712 | receive_header(n, sg, elem.in_num, buf, size); |
| 713 | offset = n->host_hdr_len; |
Michael S. Tsirkin | e35e23f | 2012-09-24 12:12:25 +0200 | [diff] [blame] | 714 | total += n->guest_hdr_len; |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 715 | guest_offset = n->guest_hdr_len; |
| 716 | } else { |
| 717 | guest_offset = 0; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* copy in packet. ugh */ |
Michael S. Tsirkin | 22cc84d | 2012-09-24 13:14:16 +0200 | [diff] [blame] | 721 | len = iov_from_buf(sg, elem.in_num, guest_offset, |
Michael Tokarev | dcf6f5e | 2012-03-11 18:05:12 +0400 | [diff] [blame] | 722 | buf + offset, size - offset); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 723 | total += len; |
Michael S. Tsirkin | 279a425 | 2010-06-22 16:22:49 +0300 | [diff] [blame] | 724 | offset += len; |
| 725 | /* If buffers can't be merged, at this point we |
| 726 | * must have consumed the complete packet. |
| 727 | * Otherwise, drop it. */ |
| 728 | if (!n->mergeable_rx_bufs && offset < size) { |
| 729 | #if 0 |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 730 | error_report("virtio-net truncated non-mergeable packet: " |
| 731 | "i %zd mergeable %d offset %zd, size %zd, " |
| 732 | "guest hdr len %zd, host hdr len %zd", |
| 733 | i, n->mergeable_rx_bufs, |
Michael S. Tsirkin | e35e23f | 2012-09-24 12:12:25 +0200 | [diff] [blame] | 734 | offset, size, n->guest_hdr_len, n->host_hdr_len); |
Michael S. Tsirkin | 279a425 | 2010-06-22 16:22:49 +0300 | [diff] [blame] | 735 | #endif |
| 736 | return size; |
| 737 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 738 | |
| 739 | /* signal other side */ |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 740 | virtqueue_fill(q->rx_vq, &elem, total, i++); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Michael S. Tsirkin | 63c5872 | 2012-09-24 13:17:13 +0200 | [diff] [blame] | 743 | if (mhdr_cnt) { |
| 744 | stw_p(&mhdr.num_buffers, i); |
| 745 | iov_from_buf(mhdr_sg, mhdr_cnt, |
| 746 | 0, |
| 747 | &mhdr.num_buffers, sizeof mhdr.num_buffers); |
Aurelien Jarno | 44b15bc | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 748 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 749 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 750 | virtqueue_flush(q->rx_vq, i); |
| 751 | virtio_notify(&n->vdev, q->rx_vq); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 752 | |
| 753 | return size; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 756 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q); |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 757 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 758 | static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 759 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 760 | VirtIONet *n = qemu_get_nic_opaque(nc); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 761 | VirtIONetQueue *q = virtio_net_get_queue(nc); |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 762 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 763 | virtqueue_push(q->tx_vq, &q->async_tx.elem, 0); |
| 764 | virtio_notify(&n->vdev, q->tx_vq); |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 765 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 766 | q->async_tx.elem.out_num = q->async_tx.len = 0; |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 767 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 768 | virtio_queue_set_notification(q->tx_vq, 1); |
| 769 | virtio_net_flush_tx(q); |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 770 | } |
| 771 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 772 | /* TX */ |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 773 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 774 | { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 775 | VirtIONet *n = q->n; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 776 | VirtQueueElement elem; |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 777 | int32_t num_packets = 0; |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 778 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
| 779 | return num_packets; |
| 780 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 781 | |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 782 | assert(n->vdev.vm_running); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 783 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 784 | if (q->async_tx.elem.out_num) { |
| 785 | virtio_queue_set_notification(q->tx_vq, 0); |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 786 | return num_packets; |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 789 | while (virtqueue_pop(q->tx_vq, &elem)) { |
Michael S. Tsirkin | 14761f9 | 2012-09-24 14:52:28 +0200 | [diff] [blame] | 790 | ssize_t ret, len; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 791 | unsigned int out_num = elem.out_num; |
| 792 | struct iovec *out_sg = &elem.out_sg[0]; |
Michael S. Tsirkin | 14761f9 | 2012-09-24 14:52:28 +0200 | [diff] [blame] | 793 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 794 | |
Michael S. Tsirkin | 7b80d08 | 2012-09-24 14:54:44 +0200 | [diff] [blame] | 795 | if (out_num < 1) { |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 796 | error_report("virtio-net header not in first element"); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 797 | exit(1); |
| 798 | } |
| 799 | |
Michael S. Tsirkin | 14761f9 | 2012-09-24 14:52:28 +0200 | [diff] [blame] | 800 | /* |
| 801 | * If host wants to see the guest header as is, we can |
| 802 | * pass it on unchanged. Otherwise, copy just the parts |
| 803 | * that host is interested in. |
| 804 | */ |
| 805 | assert(n->host_hdr_len <= n->guest_hdr_len); |
| 806 | if (n->host_hdr_len != n->guest_hdr_len) { |
| 807 | unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), |
| 808 | out_sg, out_num, |
| 809 | 0, n->host_hdr_len); |
| 810 | sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, |
| 811 | out_sg, out_num, |
| 812 | n->guest_hdr_len, -1); |
| 813 | out_num = sg_num; |
| 814 | out_sg = sg; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Michael S. Tsirkin | 7b80d08 | 2012-09-24 14:54:44 +0200 | [diff] [blame] | 817 | len = n->guest_hdr_len; |
Michael S. Tsirkin | 14761f9 | 2012-09-24 14:52:28 +0200 | [diff] [blame] | 818 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 819 | ret = qemu_sendv_packet_async(qemu_get_queue(n->nic), out_sg, out_num, |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 820 | virtio_net_tx_complete); |
| 821 | if (ret == 0) { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 822 | virtio_queue_set_notification(q->tx_vq, 0); |
| 823 | q->async_tx.elem = elem; |
| 824 | q->async_tx.len = len; |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 825 | return -EBUSY; |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | len += ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 829 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 830 | virtqueue_push(q->tx_vq, &elem, 0); |
| 831 | virtio_notify(&n->vdev, q->tx_vq); |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 832 | |
| 833 | if (++num_packets >= n->tx_burst) { |
| 834 | break; |
| 835 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 836 | } |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 837 | return num_packets; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 840 | static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 841 | { |
| 842 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 843 | VirtIONetQueue *q = &n->vq; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 844 | |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 845 | /* This happens when device was stopped but VCPU wasn't. */ |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 846 | if (!n->vdev.vm_running) { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 847 | q->tx_waiting = 1; |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 851 | if (q->tx_waiting) { |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 852 | virtio_queue_set_notification(vq, 1); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 853 | qemu_del_timer(q->tx_timer); |
| 854 | q->tx_waiting = 0; |
| 855 | virtio_net_flush_tx(q); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 856 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 857 | qemu_mod_timer(q->tx_timer, |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 858 | qemu_get_clock_ns(vm_clock) + n->tx_timeout); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 859 | q->tx_waiting = 1; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 860 | virtio_queue_set_notification(vq, 0); |
| 861 | } |
| 862 | } |
| 863 | |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 864 | static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) |
| 865 | { |
| 866 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 867 | VirtIONetQueue *q = &n->vq; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 868 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 869 | if (unlikely(q->tx_waiting)) { |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 870 | return; |
| 871 | } |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 872 | q->tx_waiting = 1; |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 873 | /* This happens when device was stopped but VCPU wasn't. */ |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 874 | if (!n->vdev.vm_running) { |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 875 | return; |
| 876 | } |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 877 | virtio_queue_set_notification(vq, 0); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 878 | qemu_bh_schedule(q->tx_bh); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 879 | } |
| 880 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 881 | static void virtio_net_tx_timer(void *opaque) |
| 882 | { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 883 | VirtIONetQueue *q = opaque; |
| 884 | VirtIONet *n = q->n; |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 885 | assert(n->vdev.vm_running); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 886 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 887 | q->tx_waiting = 0; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 888 | |
| 889 | /* Just in case the driver is not ready on more */ |
| 890 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
| 891 | return; |
| 892 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 893 | virtio_queue_set_notification(q->tx_vq, 1); |
| 894 | virtio_net_flush_tx(q); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 897 | static void virtio_net_tx_bh(void *opaque) |
| 898 | { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 899 | VirtIONetQueue *q = opaque; |
| 900 | VirtIONet *n = q->n; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 901 | int32_t ret; |
| 902 | |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 903 | assert(n->vdev.vm_running); |
Michael S. Tsirkin | 783e770 | 2010-11-22 19:52:30 +0200 | [diff] [blame] | 904 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 905 | q->tx_waiting = 0; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 906 | |
| 907 | /* Just in case the driver is not ready on more */ |
| 908 | if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))) |
| 909 | return; |
| 910 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 911 | ret = virtio_net_flush_tx(q); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 912 | if (ret == -EBUSY) { |
| 913 | return; /* Notification re-enable handled by tx_complete */ |
| 914 | } |
| 915 | |
| 916 | /* If we flush a full burst of packets, assume there are |
| 917 | * more coming and immediately reschedule */ |
| 918 | if (ret >= n->tx_burst) { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 919 | qemu_bh_schedule(q->tx_bh); |
| 920 | q->tx_waiting = 1; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 921 | return; |
| 922 | } |
| 923 | |
| 924 | /* If less than a full burst, re-enable notification and flush |
| 925 | * anything that may have come in while we weren't looking. If |
| 926 | * we find something, assume the guest is still active and reschedule */ |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 927 | virtio_queue_set_notification(q->tx_vq, 1); |
| 928 | if (virtio_net_flush_tx(q) > 0) { |
| 929 | virtio_queue_set_notification(q->tx_vq, 0); |
| 930 | qemu_bh_schedule(q->tx_bh); |
| 931 | q->tx_waiting = 1; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 935 | static void virtio_net_save(QEMUFile *f, void *opaque) |
| 936 | { |
| 937 | VirtIONet *n = opaque; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 938 | VirtIONetQueue *q = &n->vq; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 939 | |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 940 | /* At this point, backend must be stopped, otherwise |
| 941 | * it might keep writing to memory. */ |
| 942 | assert(!n->vhost_started); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 943 | virtio_save(&n->vdev, f); |
| 944 | |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 945 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 946 | qemu_put_be32(f, q->tx_waiting); |
aliguori | e46cb38 | 2009-01-07 17:50:45 +0000 | [diff] [blame] | 947 | qemu_put_be32(f, n->mergeable_rx_bufs); |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 948 | qemu_put_be16(f, n->status); |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 949 | qemu_put_byte(f, n->promisc); |
| 950 | qemu_put_byte(f, n->allmulti); |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 951 | qemu_put_be32(f, n->mac_table.in_use); |
| 952 | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 953 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 954 | qemu_put_be32(f, n->has_vnet_hdr); |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 955 | qemu_put_byte(f, n->mac_table.multi_overflow); |
| 956 | qemu_put_byte(f, n->mac_table.uni_overflow); |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 957 | qemu_put_byte(f, n->alluni); |
| 958 | qemu_put_byte(f, n->nomulti); |
| 959 | qemu_put_byte(f, n->nouni); |
| 960 | qemu_put_byte(f, n->nobcast); |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 961 | qemu_put_byte(f, n->has_ufo); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) |
| 965 | { |
| 966 | VirtIONet *n = opaque; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 967 | VirtIONetQueue *q = &n->vq; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 968 | int i; |
Orit Wassermann | 2a633c4 | 2012-05-16 12:21:35 +0200 | [diff] [blame] | 969 | int ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 970 | |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 971 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 972 | return -EINVAL; |
| 973 | |
Orit Wassermann | 2a633c4 | 2012-05-16 12:21:35 +0200 | [diff] [blame] | 974 | ret = virtio_load(&n->vdev, f); |
| 975 | if (ret) { |
| 976 | return ret; |
| 977 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 978 | |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 979 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 980 | q->tx_waiting = qemu_get_be32(f); |
Michael S. Tsirkin | ff3a806 | 2012-09-24 21:05:03 +0200 | [diff] [blame] | 981 | |
| 982 | virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f)); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 983 | |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 984 | if (version_id >= 3) |
| 985 | n->status = qemu_get_be16(f); |
| 986 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 987 | if (version_id >= 4) { |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 988 | if (version_id < 8) { |
| 989 | n->promisc = qemu_get_be32(f); |
| 990 | n->allmulti = qemu_get_be32(f); |
| 991 | } else { |
| 992 | n->promisc = qemu_get_byte(f); |
| 993 | n->allmulti = qemu_get_byte(f); |
| 994 | } |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 995 | } |
| 996 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 997 | if (version_id >= 5) { |
| 998 | n->mac_table.in_use = qemu_get_be32(f); |
| 999 | /* MAC_TABLE_ENTRIES may be different from the saved image */ |
| 1000 | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { |
| 1001 | qemu_get_buffer(f, n->mac_table.macs, |
| 1002 | n->mac_table.in_use * ETH_ALEN); |
| 1003 | } else if (n->mac_table.in_use) { |
Juan Quintela | e398d61 | 2012-08-29 19:03:09 +0200 | [diff] [blame] | 1004 | uint8_t *buf = g_malloc0(n->mac_table.in_use); |
| 1005 | qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN); |
| 1006 | g_free(buf); |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 1007 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 1008 | n->mac_table.in_use = 0; |
| 1009 | } |
| 1010 | } |
| 1011 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 1012 | if (version_id >= 6) |
| 1013 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
| 1014 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 1015 | if (version_id >= 7) { |
| 1016 | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1017 | error_report("virtio-net: saved image requires vnet_hdr=on"); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | if (n->has_vnet_hdr) { |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1022 | tap_set_offload(qemu_get_queue(n->nic)->peer, |
Michael S. Tsirkin | 704a76f | 2010-01-10 13:52:47 +0200 | [diff] [blame] | 1023 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
| 1024 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1, |
| 1025 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1, |
| 1026 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
| 1027 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 1028 | } |
Alex Williamson | 6c042c1 | 2009-06-05 14:46:52 -0600 | [diff] [blame] | 1029 | } |
| 1030 | |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 1031 | if (version_id >= 9) { |
| 1032 | n->mac_table.multi_overflow = qemu_get_byte(f); |
| 1033 | n->mac_table.uni_overflow = qemu_get_byte(f); |
| 1034 | } |
| 1035 | |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 1036 | if (version_id >= 10) { |
| 1037 | n->alluni = qemu_get_byte(f); |
| 1038 | n->nomulti = qemu_get_byte(f); |
| 1039 | n->nouni = qemu_get_byte(f); |
| 1040 | n->nobcast = qemu_get_byte(f); |
| 1041 | } |
| 1042 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 1043 | if (version_id >= 11) { |
| 1044 | if (qemu_get_byte(f) && !peer_has_ufo(n)) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 1045 | error_report("virtio-net: saved image requires TUN_F_UFO support"); |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
| 1048 | } |
| 1049 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 1050 | /* Find the first multicast entry in the saved MAC filter */ |
| 1051 | for (i = 0; i < n->mac_table.in_use; i++) { |
| 1052 | if (n->mac_table.macs[i * ETH_ALEN] & 1) { |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | n->mac_table.first_multi = i; |
Amos Kong | 9899148 | 2012-09-28 10:06:02 +0800 | [diff] [blame] | 1057 | |
| 1058 | /* nc.link_down can't be migrated, so infer link_down according |
| 1059 | * to link status bit in n->status */ |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1060 | qemu_get_queue(n->nic)->link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; |
Amos Kong | 9899148 | 2012-09-28 10:06:02 +0800 | [diff] [blame] | 1061 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
| 1064 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 1065 | static void virtio_net_cleanup(NetClientState *nc) |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1066 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 1067 | VirtIONet *n = qemu_get_nic_opaque(nc); |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1068 | |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 1069 | n->nic = NULL; |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 1072 | static NetClientInfo net_virtio_info = { |
Laszlo Ersek | 2be64a6 | 2012-07-17 16:17:12 +0200 | [diff] [blame] | 1073 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 1074 | .size = sizeof(NICState), |
| 1075 | .can_receive = virtio_net_can_receive, |
| 1076 | .receive = virtio_net_receive, |
| 1077 | .cleanup = virtio_net_cleanup, |
| 1078 | .link_status_changed = virtio_net_set_link_status, |
| 1079 | }; |
| 1080 | |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1081 | static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) |
| 1082 | { |
| 1083 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1084 | NetClientState *nc = qemu_get_queue(n->nic); |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1085 | assert(n->vhost_started); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1086 | return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx); |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, |
| 1090 | bool mask) |
| 1091 | { |
| 1092 | VirtIONet *n = to_virtio_net(vdev); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1093 | NetClientState *nc = qemu_get_queue(n->nic); |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1094 | assert(n->vhost_started); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1095 | vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer), |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1096 | vdev, idx, mask); |
| 1097 | } |
| 1098 | |
Alex Williamson | f0c07c7 | 2010-09-02 09:00:50 -0600 | [diff] [blame] | 1099 | VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf, |
| 1100 | virtio_net_conf *net) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1101 | { |
| 1102 | VirtIONet *n; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1103 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 1104 | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, |
| 1105 | sizeof(struct virtio_net_config), |
| 1106 | sizeof(VirtIONet)); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1107 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 1108 | n->vdev.get_config = virtio_net_get_config; |
| 1109 | n->vdev.set_config = virtio_net_set_config; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1110 | n->vdev.get_features = virtio_net_get_features; |
| 1111 | n->vdev.set_features = virtio_net_set_features; |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 1112 | n->vdev.bad_features = virtio_net_bad_features; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 1113 | n->vdev.reset = virtio_net_reset; |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 1114 | n->vdev.set_status = virtio_net_set_status; |
Michael S. Tsirkin | f56a124 | 2012-12-24 17:37:01 +0200 | [diff] [blame] | 1115 | n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask; |
| 1116 | n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending; |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1117 | n->vq.rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
| 1118 | n->vq.n = n; |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1119 | |
| 1120 | if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) { |
Stefan Hajnoczi | e7b43f7 | 2010-11-15 20:44:37 +0000 | [diff] [blame] | 1121 | error_report("virtio-net: " |
| 1122 | "Unknown option tx=%s, valid options: \"timer\" \"bh\"", |
| 1123 | net->tx); |
| 1124 | error_report("Defaulting to \"bh\""); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | if (net->tx && !strcmp(net->tx, "timer")) { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1128 | n->vq.tx_vq = virtio_add_queue(&n->vdev, 256, |
| 1129 | virtio_net_handle_tx_timer); |
| 1130 | n->vq.tx_timer = qemu_new_timer_ns(vm_clock, |
| 1131 | virtio_net_tx_timer, &n->vq); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1132 | n->tx_timeout = net->txtimer; |
| 1133 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1134 | n->vq.tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh); |
| 1135 | n->vq.tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vq); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1136 | } |
Alex Williamson | 4ffb17f | 2009-06-05 14:47:23 -0600 | [diff] [blame] | 1137 | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1138 | qemu_macaddr_default_if_unset(&conf->macaddr); |
Mark McLoughlin | 3cbe04c | 2009-10-28 14:07:23 +0000 | [diff] [blame] | 1139 | memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac)); |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 1140 | n->status = VIRTIO_NET_S_LINK_UP; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1141 | |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 1142 | n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n); |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 1143 | peer_test_vnet_hdr(n); |
| 1144 | if (peer_has_vnet_hdr(n)) { |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1145 | tap_using_vnet_hdr(qemu_get_queue(n->nic)->peer, true); |
Michael S. Tsirkin | 6e371ab | 2012-09-24 17:04:21 +0200 | [diff] [blame] | 1146 | n->host_hdr_len = sizeof(struct virtio_net_hdr); |
| 1147 | } else { |
| 1148 | n->host_hdr_len = 0; |
| 1149 | } |
Mark McLoughlin | eb6b6c1 | 2009-11-25 18:49:11 +0000 | [diff] [blame] | 1150 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1151 | qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a); |
aliguori | 96d5e20 | 2009-01-07 17:47:15 +0000 | [diff] [blame] | 1152 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1153 | n->vq.tx_waiting = 0; |
Alex Williamson | e3f3048 | 2010-09-02 09:00:57 -0600 | [diff] [blame] | 1154 | n->tx_burst = net->txburst; |
Michael S. Tsirkin | ff3a806 | 2012-09-24 21:05:03 +0200 | [diff] [blame] | 1155 | virtio_net_set_mrg_rx_bufs(n, 0); |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 1156 | n->promisc = 1; /* for compatibility */ |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1157 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1158 | n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 1159 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1160 | n->vlans = g_malloc0(MAX_VLAN >> 3); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 1161 | |
Alex Williamson | 01657c8 | 2010-06-25 11:09:28 -0600 | [diff] [blame] | 1162 | n->qdev = dev; |
| 1163 | register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION, |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 1164 | virtio_net_save, virtio_net_load, n); |
Paul Brook | cf21e10 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 1165 | |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 1166 | add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0"); |
| 1167 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 1168 | return &n->vdev; |
Paul Brook | cf21e10 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 1169 | } |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1170 | |
| 1171 | void virtio_net_exit(VirtIODevice *vdev) |
| 1172 | { |
| 1173 | VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev); |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1174 | VirtIONetQueue *q = &n->vq; |
Michael S. Tsirkin | 9bc6304 | 2010-03-17 13:08:42 +0200 | [diff] [blame] | 1175 | |
Michael S. Tsirkin | afbaa7b | 2010-09-27 18:41:30 +0200 | [diff] [blame] | 1176 | /* This will stop vhost backend if appropriate. */ |
| 1177 | virtio_net_set_status(vdev, 0); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1178 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 1179 | qemu_purge_queued_packets(qemu_get_queue(n->nic)); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1180 | |
Alex Williamson | 01657c8 | 2010-06-25 11:09:28 -0600 | [diff] [blame] | 1181 | unregister_savevm(n->qdev, "virtio-net", n); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1182 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1183 | g_free(n->mac_table.macs); |
| 1184 | g_free(n->vlans); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1185 | |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1186 | if (q->tx_timer) { |
| 1187 | qemu_del_timer(q->tx_timer); |
| 1188 | qemu_free_timer(q->tx_timer); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1189 | } else { |
Jason Wang | 0c87e93 | 2013-01-30 19:12:38 +0800 | [diff] [blame^] | 1190 | qemu_bh_delete(q->tx_bh); |
Alex Williamson | a697a33 | 2010-09-02 09:01:10 -0600 | [diff] [blame] | 1191 | } |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1192 | |
Jason Wang | 948ecf2 | 2013-01-30 19:12:24 +0800 | [diff] [blame] | 1193 | qemu_del_nic(n->nic); |
Amit Shah | b52dfd7 | 2011-07-27 14:00:31 +0530 | [diff] [blame] | 1194 | virtio_cleanup(&n->vdev); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 1195 | } |