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