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