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