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