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