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 | |
| 14 | #include "virtio.h" |
| 15 | #include "net.h" |
| 16 | #include "qemu-timer.h" |
| 17 | #include "virtio-net.h" |
| 18 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 19 | #define VIRTIO_NET_VM_VERSION 11 |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 20 | |
Alex Williamson | 4ffb17f | 2009-06-05 14:47:23 -0600 | [diff] [blame] | 21 | #define MAC_TABLE_ENTRIES 64 |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 22 | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 23 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 24 | typedef struct VirtIONet |
| 25 | { |
| 26 | VirtIODevice vdev; |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 27 | uint8_t mac[ETH_ALEN]; |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 28 | uint16_t status; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 29 | VirtQueue *rx_vq; |
| 30 | VirtQueue *tx_vq; |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 31 | VirtQueue *ctrl_vq; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 32 | VLANClientState *vc; |
| 33 | QEMUTimer *tx_timer; |
| 34 | int tx_timer_active; |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 35 | uint32_t has_vnet_hdr; |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 36 | uint8_t has_ufo; |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 37 | struct { |
| 38 | VirtQueueElement elem; |
| 39 | ssize_t len; |
| 40 | } async_tx; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 41 | int mergeable_rx_bufs; |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 42 | uint8_t promisc; |
| 43 | uint8_t allmulti; |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 44 | uint8_t alluni; |
| 45 | uint8_t nomulti; |
| 46 | uint8_t nouni; |
| 47 | uint8_t nobcast; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 48 | struct { |
| 49 | int in_use; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 50 | int first_multi; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 51 | uint8_t multi_overflow; |
| 52 | uint8_t uni_overflow; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 53 | uint8_t *macs; |
| 54 | } mac_table; |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 55 | uint32_t *vlans; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 56 | } VirtIONet; |
| 57 | |
| 58 | /* TODO |
| 59 | * - we could suppress RX interrupt if we were so inclined. |
| 60 | */ |
| 61 | |
| 62 | static VirtIONet *to_virtio_net(VirtIODevice *vdev) |
| 63 | { |
| 64 | return (VirtIONet *)vdev; |
| 65 | } |
| 66 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 67 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 68 | { |
| 69 | VirtIONet *n = to_virtio_net(vdev); |
| 70 | struct virtio_net_config netcfg; |
| 71 | |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 72 | netcfg.status = n->status; |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 73 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 74 | memcpy(config, &netcfg, sizeof(netcfg)); |
| 75 | } |
| 76 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 77 | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 78 | { |
| 79 | VirtIONet *n = to_virtio_net(vdev); |
| 80 | struct virtio_net_config netcfg; |
| 81 | |
| 82 | memcpy(&netcfg, config, sizeof(netcfg)); |
| 83 | |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 84 | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) { |
| 85 | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 86 | qemu_format_nic_info_str(n->vc, n->mac); |
| 87 | } |
| 88 | } |
| 89 | |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 90 | static void virtio_net_set_link_status(VLANClientState *vc) |
| 91 | { |
| 92 | VirtIONet *n = vc->opaque; |
| 93 | uint16_t old_status = n->status; |
| 94 | |
| 95 | if (vc->link_down) |
| 96 | n->status &= ~VIRTIO_NET_S_LINK_UP; |
| 97 | else |
| 98 | n->status |= VIRTIO_NET_S_LINK_UP; |
| 99 | |
| 100 | if (n->status != old_status) |
| 101 | virtio_notify_config(&n->vdev); |
| 102 | } |
| 103 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 104 | static void virtio_net_reset(VirtIODevice *vdev) |
| 105 | { |
| 106 | VirtIONet *n = to_virtio_net(vdev); |
| 107 | |
| 108 | /* Reset back to compatibility mode */ |
| 109 | n->promisc = 1; |
| 110 | n->allmulti = 0; |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 111 | n->alluni = 0; |
| 112 | n->nomulti = 0; |
| 113 | n->nouni = 0; |
| 114 | n->nobcast = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 115 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 116 | /* Flush any MAC and VLAN filter table state */ |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 117 | n->mac_table.in_use = 0; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 118 | n->mac_table.first_multi = 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 119 | n->mac_table.multi_overflow = 0; |
| 120 | n->mac_table.uni_overflow = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 121 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 122 | memset(n->vlans, 0, MAX_VLAN >> 3); |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 125 | static int peer_has_vnet_hdr(VirtIONet *n) |
| 126 | { |
| 127 | if (!n->vc->peer) |
| 128 | return 0; |
| 129 | |
| 130 | if (n->vc->peer->type != NET_CLIENT_TYPE_TAP) |
| 131 | return 0; |
| 132 | |
| 133 | n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer); |
| 134 | |
| 135 | return n->has_vnet_hdr; |
| 136 | } |
| 137 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 138 | static int peer_has_ufo(VirtIONet *n) |
| 139 | { |
| 140 | if (!peer_has_vnet_hdr(n)) |
| 141 | return 0; |
| 142 | |
| 143 | n->has_ufo = tap_has_ufo(n->vc->peer); |
| 144 | |
| 145 | return n->has_ufo; |
| 146 | } |
| 147 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 148 | static uint32_t virtio_net_get_features(VirtIODevice *vdev) |
| 149 | { |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 150 | VirtIONet *n = to_virtio_net(vdev); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 151 | uint32_t features = (1 << VIRTIO_NET_F_MAC) | |
Mark McLoughlin | e16044e | 2009-06-18 11:31:07 +0100 | [diff] [blame] | 152 | (1 << VIRTIO_NET_F_MRG_RXBUF) | |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 153 | (1 << VIRTIO_NET_F_STATUS) | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 154 | (1 << VIRTIO_NET_F_CTRL_VQ) | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 155 | (1 << VIRTIO_NET_F_CTRL_RX) | |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 156 | (1 << VIRTIO_NET_F_CTRL_VLAN) | |
| 157 | (1 << VIRTIO_NET_F_CTRL_RX_EXTRA); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 158 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 159 | if (peer_has_vnet_hdr(n)) { |
| 160 | tap_using_vnet_hdr(n->vc->peer, 1); |
| 161 | |
| 162 | features |= (1 << VIRTIO_NET_F_CSUM); |
| 163 | features |= (1 << VIRTIO_NET_F_HOST_TSO4); |
| 164 | features |= (1 << VIRTIO_NET_F_HOST_TSO6); |
| 165 | features |= (1 << VIRTIO_NET_F_HOST_ECN); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 166 | |
| 167 | features |= (1 << VIRTIO_NET_F_GUEST_CSUM); |
| 168 | features |= (1 << VIRTIO_NET_F_GUEST_TSO4); |
| 169 | features |= (1 << VIRTIO_NET_F_GUEST_TSO6); |
| 170 | features |= (1 << VIRTIO_NET_F_GUEST_ECN); |
Sridhar Samudrala | 6c9f58b | 2009-10-22 17:43:49 +0100 | [diff] [blame] | 171 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 172 | if (peer_has_ufo(n)) { |
Sridhar Samudrala | 6c9f58b | 2009-10-22 17:43:49 +0100 | [diff] [blame] | 173 | features |= (1 << VIRTIO_NET_F_GUEST_UFO); |
| 174 | features |= (1 << VIRTIO_NET_F_HOST_UFO); |
| 175 | } |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 176 | } |
| 177 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 178 | return features; |
| 179 | } |
| 180 | |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 181 | static uint32_t virtio_net_bad_features(VirtIODevice *vdev) |
| 182 | { |
| 183 | uint32_t features = 0; |
| 184 | |
| 185 | /* Linux kernel 2.6.25. It understood MAC (as everyone must), |
| 186 | * but also these: */ |
| 187 | features |= (1 << VIRTIO_NET_F_MAC); |
| 188 | features |= (1 << VIRTIO_NET_F_GUEST_CSUM); |
| 189 | features |= (1 << VIRTIO_NET_F_GUEST_TSO4); |
| 190 | features |= (1 << VIRTIO_NET_F_GUEST_TSO6); |
| 191 | features |= (1 << VIRTIO_NET_F_GUEST_ECN); |
| 192 | |
| 193 | return features & virtio_net_get_features(vdev); |
| 194 | } |
| 195 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 196 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
| 197 | { |
| 198 | VirtIONet *n = to_virtio_net(vdev); |
| 199 | |
| 200 | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 201 | |
| 202 | if (n->has_vnet_hdr) { |
| 203 | tap_set_offload(n->vc->peer, |
| 204 | (features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
| 205 | (features >> VIRTIO_NET_F_GUEST_TSO4) & 1, |
| 206 | (features >> VIRTIO_NET_F_GUEST_TSO6) & 1, |
Sridhar Samudrala | 6c9f58b | 2009-10-22 17:43:49 +0100 | [diff] [blame] | 207 | (features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
| 208 | (features >> VIRTIO_NET_F_GUEST_UFO) & 1); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 209 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 212 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
| 213 | VirtQueueElement *elem) |
| 214 | { |
| 215 | uint8_t on; |
| 216 | |
| 217 | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { |
| 218 | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n"); |
| 219 | exit(1); |
| 220 | } |
| 221 | |
| 222 | on = ldub_p(elem->out_sg[1].iov_base); |
| 223 | |
| 224 | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC) |
| 225 | n->promisc = on; |
| 226 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) |
| 227 | n->allmulti = on; |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 228 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI) |
| 229 | n->alluni = on; |
| 230 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI) |
| 231 | n->nomulti = on; |
| 232 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI) |
| 233 | n->nouni = on; |
| 234 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST) |
| 235 | n->nobcast = on; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 236 | else |
| 237 | return VIRTIO_NET_ERR; |
| 238 | |
| 239 | return VIRTIO_NET_OK; |
| 240 | } |
| 241 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 242 | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
| 243 | VirtQueueElement *elem) |
| 244 | { |
| 245 | struct virtio_net_ctrl_mac mac_data; |
| 246 | |
| 247 | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || |
| 248 | elem->out_sg[1].iov_len < sizeof(mac_data) || |
| 249 | elem->out_sg[2].iov_len < sizeof(mac_data)) |
| 250 | return VIRTIO_NET_ERR; |
| 251 | |
| 252 | n->mac_table.in_use = 0; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 253 | n->mac_table.first_multi = 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 254 | n->mac_table.uni_overflow = 0; |
| 255 | n->mac_table.multi_overflow = 0; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 256 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
| 257 | |
| 258 | mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base); |
| 259 | |
| 260 | if (sizeof(mac_data.entries) + |
| 261 | (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len) |
| 262 | return VIRTIO_NET_ERR; |
| 263 | |
| 264 | if (mac_data.entries <= MAC_TABLE_ENTRIES) { |
| 265 | memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data), |
| 266 | mac_data.entries * ETH_ALEN); |
| 267 | n->mac_table.in_use += mac_data.entries; |
| 268 | } else { |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 269 | n->mac_table.uni_overflow = 1; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 272 | n->mac_table.first_multi = n->mac_table.in_use; |
| 273 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 274 | mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base); |
| 275 | |
| 276 | if (sizeof(mac_data.entries) + |
| 277 | (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len) |
| 278 | return VIRTIO_NET_ERR; |
| 279 | |
| 280 | if (mac_data.entries) { |
| 281 | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { |
| 282 | memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), |
| 283 | elem->out_sg[2].iov_base + sizeof(mac_data), |
| 284 | mac_data.entries * ETH_ALEN); |
| 285 | n->mac_table.in_use += mac_data.entries; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 286 | } else { |
| 287 | n->mac_table.multi_overflow = 1; |
| 288 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return VIRTIO_NET_OK; |
| 292 | } |
| 293 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 294 | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
| 295 | VirtQueueElement *elem) |
| 296 | { |
| 297 | uint16_t vid; |
| 298 | |
| 299 | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { |
| 300 | fprintf(stderr, "virtio-net ctrl invalid vlan command\n"); |
| 301 | return VIRTIO_NET_ERR; |
| 302 | } |
| 303 | |
| 304 | vid = lduw_le_p(elem->out_sg[1].iov_base); |
| 305 | |
| 306 | if (vid >= MAX_VLAN) |
| 307 | return VIRTIO_NET_ERR; |
| 308 | |
| 309 | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) |
| 310 | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); |
| 311 | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) |
| 312 | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); |
| 313 | else |
| 314 | return VIRTIO_NET_ERR; |
| 315 | |
| 316 | return VIRTIO_NET_OK; |
| 317 | } |
| 318 | |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 319 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
| 320 | { |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 321 | VirtIONet *n = to_virtio_net(vdev); |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 322 | struct virtio_net_ctrl_hdr ctrl; |
| 323 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 324 | VirtQueueElement elem; |
| 325 | |
| 326 | while (virtqueue_pop(vq, &elem)) { |
| 327 | if ((elem.in_num < 1) || (elem.out_num < 1)) { |
| 328 | fprintf(stderr, "virtio-net ctrl missing headers\n"); |
| 329 | exit(1); |
| 330 | } |
| 331 | |
| 332 | if (elem.out_sg[0].iov_len < sizeof(ctrl) || |
aliguori | c6bb9a3 | 2009-03-13 15:04:02 +0000 | [diff] [blame] | 333 | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 334 | fprintf(stderr, "virtio-net ctrl header not in correct element\n"); |
| 335 | exit(1); |
| 336 | } |
| 337 | |
| 338 | ctrl.class = ldub_p(elem.out_sg[0].iov_base); |
| 339 | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); |
| 340 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 341 | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE) |
| 342 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 343 | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
| 344 | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 345 | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
| 346 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 347 | |
aliguori | 3d11d36 | 2009-02-05 22:36:16 +0000 | [diff] [blame] | 348 | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status); |
| 349 | |
| 350 | virtqueue_push(vq, &elem, sizeof(status)); |
| 351 | virtio_notify(vdev, vq); |
| 352 | } |
| 353 | } |
| 354 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 355 | /* RX */ |
| 356 | |
| 357 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) |
| 358 | { |
Mark McLoughlin | 8aeff62 | 2009-04-29 13:40:02 +0100 | [diff] [blame] | 359 | VirtIONet *n = to_virtio_net(vdev); |
| 360 | |
| 361 | qemu_flush_queued_packets(n->vc); |
Glauber Costa | a61d1f6 | 2009-07-20 13:07:41 -0400 | [diff] [blame] | 362 | |
| 363 | /* We now have RX buffers, signal to the IO thread to break out of the |
| 364 | * select to re-poll the tap file descriptor */ |
| 365 | qemu_notify_event(); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int do_virtio_net_can_receive(VirtIONet *n, int bufsize) |
| 369 | { |
| 370 | if (!virtio_queue_ready(n->rx_vq) || |
| 371 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
| 372 | return 0; |
| 373 | |
| 374 | if (virtio_queue_empty(n->rx_vq) || |
| 375 | (n->mergeable_rx_bufs && |
| 376 | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) { |
| 377 | virtio_queue_set_notification(n->rx_vq, 1); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | virtio_queue_set_notification(n->rx_vq, 0); |
| 382 | return 1; |
| 383 | } |
| 384 | |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 385 | static int virtio_net_can_receive(VLANClientState *vc) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 386 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 387 | VirtIONet *n = vc->opaque; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 388 | |
| 389 | return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE); |
| 390 | } |
| 391 | |
Anthony Liguori | 1d41b0c | 2009-10-22 17:43:48 +0100 | [diff] [blame] | 392 | /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so |
| 393 | * it never finds out that the packets don't have valid checksums. This |
| 394 | * causes dhclient to get upset. Fedora's carried a patch for ages to |
| 395 | * fix this with Xen but it hasn't appeared in an upstream release of |
| 396 | * dhclient yet. |
| 397 | * |
| 398 | * To avoid breaking existing guests, we catch udp packets and add |
| 399 | * checksums. This is terrible but it's better than hacking the guest |
| 400 | * kernels. |
| 401 | * |
| 402 | * N.B. if we introduce a zero-copy API, this operation is no longer free so |
| 403 | * we should provide a mechanism to disable it to avoid polluting the host |
| 404 | * cache. |
| 405 | */ |
| 406 | static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, |
| 407 | const uint8_t *buf, size_t size) |
| 408 | { |
| 409 | if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ |
| 410 | (size > 27 && size < 1500) && /* normal sized MTU */ |
| 411 | (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ |
| 412 | (buf[23] == 17) && /* ip.protocol == UDP */ |
| 413 | (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ |
| 414 | /* FIXME this cast is evil */ |
| 415 | net_checksum_calculate((uint8_t *)buf, size); |
| 416 | hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 417 | } |
| 418 | } |
| 419 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 420 | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) |
| 421 | { |
| 422 | int offset, i; |
| 423 | |
| 424 | offset = i = 0; |
| 425 | while (offset < count && i < iovcnt) { |
| 426 | int len = MIN(iov[i].iov_len, count - offset); |
| 427 | memcpy(iov[i].iov_base, buf + offset, len); |
| 428 | offset += len; |
| 429 | i++; |
| 430 | } |
| 431 | |
| 432 | return offset; |
| 433 | } |
| 434 | |
| 435 | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, |
aliguori | 4689f4b | 2008-12-17 19:45:40 +0000 | [diff] [blame] | 436 | const void *buf, size_t size, size_t hdr_len) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 437 | { |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 438 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 439 | int offset = 0; |
| 440 | |
| 441 | hdr->flags = 0; |
| 442 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 443 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 444 | if (n->has_vnet_hdr) { |
| 445 | memcpy(hdr, buf, sizeof(*hdr)); |
| 446 | offset = sizeof(*hdr); |
Anthony Liguori | 1d41b0c | 2009-10-22 17:43:48 +0100 | [diff] [blame] | 447 | work_around_broken_dhclient(hdr, buf + offset, size - offset); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 448 | } |
| 449 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 450 | /* We only ever receive a struct virtio_net_hdr from the tapfd, |
| 451 | * but we may be passing along a larger header to the guest. |
| 452 | */ |
| 453 | iov[0].iov_base += hdr_len; |
| 454 | iov[0].iov_len -= hdr_len; |
| 455 | |
| 456 | return offset; |
| 457 | } |
| 458 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 459 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
| 460 | { |
| 461 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 462 | static const uint8_t vlan[] = {0x81, 0x00}; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 463 | uint8_t *ptr = (uint8_t *)buf; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 464 | int i; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 465 | |
| 466 | if (n->promisc) |
| 467 | return 1; |
| 468 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 469 | if (n->has_vnet_hdr) { |
| 470 | ptr += sizeof(struct virtio_net_hdr); |
| 471 | } |
| 472 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 473 | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
| 474 | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; |
| 475 | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) |
| 476 | return 0; |
| 477 | } |
| 478 | |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 479 | if (ptr[0] & 1) { // multicast |
| 480 | if (!memcmp(ptr, bcast, sizeof(bcast))) { |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 481 | return !n->nobcast; |
| 482 | } else if (n->nomulti) { |
| 483 | return 0; |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 484 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 485 | return 1; |
| 486 | } |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 487 | |
| 488 | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { |
| 489 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { |
| 490 | return 1; |
| 491 | } |
| 492 | } |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 493 | } else { // unicast |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 494 | if (n->nouni) { |
| 495 | return 0; |
| 496 | } else if (n->alluni || n->mac_table.uni_overflow) { |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 497 | return 1; |
| 498 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { |
Alex Williamson | bbe2f39 | 2009-06-05 14:47:02 -0600 | [diff] [blame] | 499 | return 1; |
| 500 | } |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 501 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 502 | for (i = 0; i < n->mac_table.first_multi; i++) { |
| 503 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { |
| 504 | return 1; |
| 505 | } |
| 506 | } |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 507 | } |
| 508 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 512 | static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 513 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 514 | VirtIONet *n = vc->opaque; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 515 | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
aliguori | 4689f4b | 2008-12-17 19:45:40 +0000 | [diff] [blame] | 516 | size_t hdr_len, offset, i; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 517 | |
| 518 | if (!do_virtio_net_can_receive(n, size)) |
Mark McLoughlin | 8aeff62 | 2009-04-29 13:40:02 +0100 | [diff] [blame] | 519 | return 0; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 520 | |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 521 | if (!receive_filter(n, buf, size)) |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 522 | return size; |
aliguori | 3831ab2 | 2009-02-05 22:36:24 +0000 | [diff] [blame] | 523 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 524 | /* hdr_len refers to the header we supply to the guest */ |
| 525 | hdr_len = n->mergeable_rx_bufs ? |
| 526 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); |
| 527 | |
| 528 | offset = i = 0; |
| 529 | |
| 530 | while (offset < size) { |
| 531 | VirtQueueElement elem; |
| 532 | int len, total; |
| 533 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; |
| 534 | |
| 535 | len = total = 0; |
| 536 | |
| 537 | if ((i != 0 && !n->mergeable_rx_bufs) || |
| 538 | virtqueue_pop(n->rx_vq, &elem) == 0) { |
| 539 | if (i == 0) |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 540 | return -1; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 541 | fprintf(stderr, "virtio-net truncating packet\n"); |
| 542 | exit(1); |
| 543 | } |
| 544 | |
| 545 | if (elem.in_num < 1) { |
| 546 | fprintf(stderr, "virtio-net receive queue contains no in buffers\n"); |
| 547 | exit(1); |
| 548 | } |
| 549 | |
| 550 | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { |
| 551 | fprintf(stderr, "virtio-net header not in first element\n"); |
| 552 | exit(1); |
| 553 | } |
| 554 | |
| 555 | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); |
| 556 | |
| 557 | if (i == 0) { |
| 558 | if (n->mergeable_rx_bufs) |
| 559 | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; |
| 560 | |
| 561 | offset += receive_header(n, sg, elem.in_num, |
| 562 | buf + offset, size - offset, hdr_len); |
| 563 | total += hdr_len; |
| 564 | } |
| 565 | |
| 566 | /* copy in packet. ugh */ |
| 567 | len = iov_fill(sg, elem.in_num, |
| 568 | buf + offset, size - offset); |
| 569 | total += len; |
| 570 | |
| 571 | /* signal other side */ |
| 572 | virtqueue_fill(n->rx_vq, &elem, total, i++); |
| 573 | |
| 574 | offset += len; |
| 575 | } |
| 576 | |
| 577 | if (mhdr) |
| 578 | mhdr->num_buffers = i; |
| 579 | |
| 580 | virtqueue_flush(n->rx_vq, i); |
| 581 | virtio_notify(&n->vdev, n->rx_vq); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 582 | |
| 583 | return size; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 586 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq); |
| 587 | |
| 588 | static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len) |
| 589 | { |
| 590 | VirtIONet *n = vc->opaque; |
| 591 | |
| 592 | virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len); |
| 593 | virtio_notify(&n->vdev, n->tx_vq); |
| 594 | |
| 595 | n->async_tx.elem.out_num = n->async_tx.len = 0; |
| 596 | |
| 597 | virtio_queue_set_notification(n->tx_vq, 1); |
| 598 | virtio_net_flush_tx(n, n->tx_vq); |
| 599 | } |
| 600 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 601 | /* TX */ |
| 602 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) |
| 603 | { |
| 604 | VirtQueueElement elem; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 605 | |
| 606 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
| 607 | return; |
| 608 | |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 609 | if (n->async_tx.elem.out_num) { |
| 610 | virtio_queue_set_notification(n->tx_vq, 0); |
| 611 | return; |
| 612 | } |
| 613 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 614 | while (virtqueue_pop(vq, &elem)) { |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 615 | ssize_t ret, len = 0; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 616 | unsigned int out_num = elem.out_num; |
| 617 | struct iovec *out_sg = &elem.out_sg[0]; |
| 618 | unsigned hdr_len; |
| 619 | |
| 620 | /* hdr_len refers to the header received from the guest */ |
| 621 | hdr_len = n->mergeable_rx_bufs ? |
| 622 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
| 623 | sizeof(struct virtio_net_hdr); |
| 624 | |
| 625 | if (out_num < 1 || out_sg->iov_len != hdr_len) { |
| 626 | fprintf(stderr, "virtio-net header not in first element\n"); |
| 627 | exit(1); |
| 628 | } |
| 629 | |
| 630 | /* ignore the header if GSO is not supported */ |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 631 | if (!n->has_vnet_hdr) { |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 632 | out_num--; |
| 633 | out_sg++; |
| 634 | len += hdr_len; |
| 635 | } else if (n->mergeable_rx_bufs) { |
| 636 | /* tapfd expects a struct virtio_net_hdr */ |
| 637 | hdr_len -= sizeof(struct virtio_net_hdr); |
| 638 | out_sg->iov_len -= hdr_len; |
| 639 | len += hdr_len; |
| 640 | } |
| 641 | |
Mark McLoughlin | 6243375 | 2009-06-18 18:21:36 +0100 | [diff] [blame] | 642 | ret = qemu_sendv_packet_async(n->vc, out_sg, out_num, |
| 643 | virtio_net_tx_complete); |
| 644 | if (ret == 0) { |
| 645 | virtio_queue_set_notification(n->tx_vq, 0); |
| 646 | n->async_tx.elem = elem; |
| 647 | n->async_tx.len = len; |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | len += ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 652 | |
| 653 | virtqueue_push(vq, &elem, len); |
| 654 | virtio_notify(&n->vdev, vq); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) |
| 659 | { |
| 660 | VirtIONet *n = to_virtio_net(vdev); |
| 661 | |
| 662 | if (n->tx_timer_active) { |
| 663 | virtio_queue_set_notification(vq, 1); |
| 664 | qemu_del_timer(n->tx_timer); |
| 665 | n->tx_timer_active = 0; |
| 666 | virtio_net_flush_tx(n, vq); |
| 667 | } else { |
| 668 | qemu_mod_timer(n->tx_timer, |
| 669 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
| 670 | n->tx_timer_active = 1; |
| 671 | virtio_queue_set_notification(vq, 0); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | static void virtio_net_tx_timer(void *opaque) |
| 676 | { |
| 677 | VirtIONet *n = opaque; |
| 678 | |
| 679 | n->tx_timer_active = 0; |
| 680 | |
| 681 | /* Just in case the driver is not ready on more */ |
| 682 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
| 683 | return; |
| 684 | |
| 685 | virtio_queue_set_notification(n->tx_vq, 1); |
| 686 | virtio_net_flush_tx(n, n->tx_vq); |
| 687 | } |
| 688 | |
| 689 | static void virtio_net_save(QEMUFile *f, void *opaque) |
| 690 | { |
| 691 | VirtIONet *n = opaque; |
| 692 | |
| 693 | virtio_save(&n->vdev, f); |
| 694 | |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 695 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 696 | qemu_put_be32(f, n->tx_timer_active); |
aliguori | e46cb38 | 2009-01-07 17:50:45 +0000 | [diff] [blame] | 697 | qemu_put_be32(f, n->mergeable_rx_bufs); |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 698 | qemu_put_be16(f, n->status); |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 699 | qemu_put_byte(f, n->promisc); |
| 700 | qemu_put_byte(f, n->allmulti); |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 701 | qemu_put_be32(f, n->mac_table.in_use); |
| 702 | 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] | 703 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 704 | qemu_put_be32(f, n->has_vnet_hdr); |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 705 | qemu_put_byte(f, n->mac_table.multi_overflow); |
| 706 | qemu_put_byte(f, n->mac_table.uni_overflow); |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 707 | qemu_put_byte(f, n->alluni); |
| 708 | qemu_put_byte(f, n->nomulti); |
| 709 | qemu_put_byte(f, n->nouni); |
| 710 | qemu_put_byte(f, n->nobcast); |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 711 | qemu_put_byte(f, n->has_ufo); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) |
| 715 | { |
| 716 | VirtIONet *n = opaque; |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 717 | int i; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 718 | |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 719 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 720 | return -EINVAL; |
| 721 | |
| 722 | virtio_load(&n->vdev, f); |
| 723 | |
aliguori | 7967406 | 2009-02-05 22:36:12 +0000 | [diff] [blame] | 724 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 725 | n->tx_timer_active = qemu_get_be32(f); |
aliguori | e46cb38 | 2009-01-07 17:50:45 +0000 | [diff] [blame] | 726 | n->mergeable_rx_bufs = qemu_get_be32(f); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 727 | |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 728 | if (version_id >= 3) |
| 729 | n->status = qemu_get_be16(f); |
| 730 | |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 731 | if (version_id >= 4) { |
Alex Williamson | f10c592 | 2009-06-05 14:46:57 -0600 | [diff] [blame] | 732 | if (version_id < 8) { |
| 733 | n->promisc = qemu_get_be32(f); |
| 734 | n->allmulti = qemu_get_be32(f); |
| 735 | } else { |
| 736 | n->promisc = qemu_get_byte(f); |
| 737 | n->allmulti = qemu_get_byte(f); |
| 738 | } |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 739 | } |
| 740 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 741 | if (version_id >= 5) { |
| 742 | n->mac_table.in_use = qemu_get_be32(f); |
| 743 | /* MAC_TABLE_ENTRIES may be different from the saved image */ |
| 744 | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { |
| 745 | qemu_get_buffer(f, n->mac_table.macs, |
| 746 | n->mac_table.in_use * ETH_ALEN); |
| 747 | } else if (n->mac_table.in_use) { |
| 748 | qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 749 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 750 | n->mac_table.in_use = 0; |
| 751 | } |
| 752 | } |
| 753 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 754 | if (version_id >= 6) |
| 755 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
| 756 | |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 757 | if (version_id >= 7) { |
| 758 | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { |
| 759 | qemu_error("virtio-net: saved image requires vnet_hdr=on\n"); |
| 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | if (n->has_vnet_hdr) { |
| 764 | tap_using_vnet_hdr(n->vc->peer, 1); |
Mark McLoughlin | f5436dd | 2009-10-22 17:43:47 +0100 | [diff] [blame] | 765 | tap_set_offload(n->vc->peer, |
| 766 | (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
| 767 | (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1, |
| 768 | (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1, |
Sridhar Samudrala | 6c9f58b | 2009-10-22 17:43:49 +0100 | [diff] [blame] | 769 | (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
| 770 | (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1); |
Mark McLoughlin | 3a33013 | 2009-10-22 17:43:45 +0100 | [diff] [blame] | 771 | } |
Alex Williamson | 6c042c1 | 2009-06-05 14:46:52 -0600 | [diff] [blame] | 772 | } |
| 773 | |
Alex Williamson | 8fd2a2f | 2009-06-05 14:47:08 -0600 | [diff] [blame] | 774 | if (version_id >= 9) { |
| 775 | n->mac_table.multi_overflow = qemu_get_byte(f); |
| 776 | n->mac_table.uni_overflow = qemu_get_byte(f); |
| 777 | } |
| 778 | |
Alex Williamson | 015cb16 | 2009-06-05 14:47:18 -0600 | [diff] [blame] | 779 | if (version_id >= 10) { |
| 780 | n->alluni = qemu_get_byte(f); |
| 781 | n->nomulti = qemu_get_byte(f); |
| 782 | n->nouni = qemu_get_byte(f); |
| 783 | n->nobcast = qemu_get_byte(f); |
| 784 | } |
| 785 | |
Mark McLoughlin | 0ce0e8f | 2009-10-22 17:43:50 +0100 | [diff] [blame^] | 786 | if (version_id >= 11) { |
| 787 | if (qemu_get_byte(f) && !peer_has_ufo(n)) { |
| 788 | qemu_error("virtio-net: saved image requires TUN_F_UFO support\n"); |
| 789 | return -1; |
| 790 | } |
| 791 | } |
| 792 | |
Alex Williamson | 2d9aba3 | 2009-06-05 14:47:13 -0600 | [diff] [blame] | 793 | /* Find the first multicast entry in the saved MAC filter */ |
| 794 | for (i = 0; i < n->mac_table.in_use; i++) { |
| 795 | if (n->mac_table.macs[i * ETH_ALEN] & 1) { |
| 796 | break; |
| 797 | } |
| 798 | } |
| 799 | n->mac_table.first_multi = i; |
| 800 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 801 | if (n->tx_timer_active) { |
| 802 | qemu_mod_timer(n->tx_timer, |
| 803 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
| 804 | } |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 809 | static void virtio_net_cleanup(VLANClientState *vc) |
| 810 | { |
| 811 | VirtIONet *n = vc->opaque; |
| 812 | |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 813 | n->vc = NULL; |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 816 | VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 817 | { |
| 818 | VirtIONet *n; |
| 819 | static int virtio_net_id; |
| 820 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 821 | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, |
| 822 | sizeof(struct virtio_net_config), |
| 823 | sizeof(VirtIONet)); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 824 | |
aliguori | 0f03eca | 2009-02-05 22:36:08 +0000 | [diff] [blame] | 825 | n->vdev.get_config = virtio_net_get_config; |
| 826 | n->vdev.set_config = virtio_net_set_config; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 827 | n->vdev.get_features = virtio_net_get_features; |
| 828 | n->vdev.set_features = virtio_net_set_features; |
aliguori | 8eca6b1 | 2009-04-05 17:40:08 +0000 | [diff] [blame] | 829 | n->vdev.bad_features = virtio_net_bad_features; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 830 | n->vdev.reset = virtio_net_reset; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 831 | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
| 832 | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx); |
Alex Williamson | 4ffb17f | 2009-06-05 14:47:23 -0600 | [diff] [blame] | 833 | 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] | 834 | qemu_macaddr_default_if_unset(&conf->macaddr); |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 835 | n->status = VIRTIO_NET_S_LINK_UP; |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 836 | n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer, |
| 837 | dev->info->name, dev->id, |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 838 | virtio_net_can_receive, |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 839 | virtio_net_receive, NULL, NULL, |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 840 | virtio_net_cleanup, n); |
aliguori | 554c97d | 2009-01-08 19:46:33 +0000 | [diff] [blame] | 841 | n->vc->link_status_changed = virtio_net_set_link_status; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 842 | |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 843 | qemu_format_nic_info_str(n->vc, conf->macaddr.a); |
aliguori | 96d5e20 | 2009-01-07 17:47:15 +0000 | [diff] [blame] | 844 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 845 | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
| 846 | n->tx_timer_active = 0; |
| 847 | n->mergeable_rx_bufs = 0; |
aliguori | 002437c | 2009-02-05 22:36:20 +0000 | [diff] [blame] | 848 | n->promisc = 1; /* for compatibility */ |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 849 | |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 850 | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
aliguori | b6503ed | 2009-02-05 22:36:28 +0000 | [diff] [blame] | 851 | |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 852 | n->vlans = qemu_mallocz(MAX_VLAN >> 3); |
aliguori | f21c0ed | 2009-02-05 22:36:32 +0000 | [diff] [blame] | 853 | |
aliguori | 9d6271b | 2009-02-05 22:36:04 +0000 | [diff] [blame] | 854 | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 855 | virtio_net_save, virtio_net_load, n); |
Paul Brook | cf21e10 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 856 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 857 | return &n->vdev; |
Paul Brook | cf21e10 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 858 | } |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 859 | |
| 860 | void virtio_net_exit(VirtIODevice *vdev) |
| 861 | { |
| 862 | VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev); |
| 863 | |
| 864 | qemu_purge_queued_packets(n->vc); |
| 865 | |
| 866 | unregister_savevm("virtio-net", n); |
| 867 | |
| 868 | qemu_free(n->mac_table.macs); |
| 869 | qemu_free(n->vlans); |
| 870 | |
| 871 | qemu_del_timer(n->tx_timer); |
| 872 | qemu_free_timer(n->tx_timer); |
| 873 | |
| 874 | virtio_cleanup(&n->vdev); |
| 875 | qemu_del_vlan_client(n->vc); |
| 876 | } |