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