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