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