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