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