blob: 4d80a2574431ff3259f6692d549225be1f9c1946 [file] [log] [blame]
aliguorifbe78f42008-12-17 19:13:11 +00001/*
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 Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/iov.h"
aliguorifbe78f42008-12-17 19:13:11 +000015#include "virtio.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020016#include "net/net.h"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010017#include "net/checksum.h"
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010018#include "net/tap.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/error-report.h"
20#include "qemu/timer.h"
aliguorifbe78f42008-12-17 19:13:11 +000021#include "virtio-net.h"
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020022#include "vhost_net.h"
aliguorifbe78f42008-12-17 19:13:11 +000023
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010024#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000025
Alex Williamson4ffb17f2009-06-05 14:47:23 -060026#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000027#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000028
aliguorifbe78f42008-12-17 19:13:11 +000029typedef struct VirtIONet
30{
31 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000032 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000033 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000034 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000036 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000037 NICState *nic;
aliguorifbe78f42008-12-17 19:13:11 +000038 QEMUTimer *tx_timer;
Alex Williamsona697a332010-09-02 09:01:10 -060039 QEMUBH *tx_bh;
Alex Williamsonf0c07c72010-09-02 09:00:50 -060040 uint32_t tx_timeout;
Alex Williamsone3f30482010-09-02 09:00:57 -060041 int32_t tx_burst;
Alex Williamson4b4b8d32010-09-02 09:01:04 -060042 int tx_waiting;
Mark McLoughlin3a330132009-10-22 17:43:45 +010043 uint32_t has_vnet_hdr;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +020044 size_t host_hdr_len;
45 size_t guest_hdr_len;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010046 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010047 struct {
48 VirtQueueElement elem;
49 ssize_t len;
50 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000051 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060052 uint8_t promisc;
53 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060054 uint8_t alluni;
55 uint8_t nomulti;
56 uint8_t nouni;
57 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020058 uint8_t vhost_started;
aliguorib6503ed2009-02-05 22:36:28 +000059 struct {
60 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060061 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060062 uint8_t multi_overflow;
63 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000064 uint8_t *macs;
65 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000066 uint32_t *vlans;
Alex Williamson01657c82010-06-25 11:09:28 -060067 DeviceState *qdev;
aliguorifbe78f42008-12-17 19:13:11 +000068} VirtIONet;
69
70/* TODO
71 * - we could suppress RX interrupt if we were so inclined.
72 */
73
74static VirtIONet *to_virtio_net(VirtIODevice *vdev)
75{
76 return (VirtIONet *)vdev;
77}
78
aliguori0f03eca2009-02-05 22:36:08 +000079static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000080{
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
Stefan Hajnoczib46d97f2011-03-03 21:42:28 +000084 stw_p(&netcfg.status, n->status);
aliguori79674062009-02-05 22:36:12 +000085 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000086 memcpy(config, &netcfg, sizeof(netcfg));
87}
88
aliguori0f03eca2009-02-05 22:36:08 +000089static 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
aliguori79674062009-02-05 22:36:12 +000096 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
97 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000098 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000099 }
100}
101
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200102static bool virtio_net_started(VirtIONet *n, uint8_t status)
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200103{
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200104 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200105 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200106}
107
108static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
109{
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200110 if (!n->nic->nc.peer) {
111 return;
112 }
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200113 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200114 return;
115 }
116
117 if (!tap_get_vhost_net(n->nic->nc.peer)) {
118 return;
119 }
Michael S. Tsirkin32993692011-02-09 18:45:09 +0200120 if (!!n->vhost_started == virtio_net_started(n, status) &&
121 !n->nic->nc.peer->link_down) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200122 return;
123 }
124 if (!n->vhost_started) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200125 int r;
126 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
127 return;
128 }
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200129 n->vhost_started = 1;
mst@redhat.com5430a282011-02-01 22:13:42 +0200130 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200131 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000132 error_report("unable to start vhost net: %d: "
133 "falling back on userspace virtio", -r);
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200134 n->vhost_started = 0;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200135 }
136 } else {
137 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
138 n->vhost_started = 0;
139 }
140}
141
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200142static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
143{
144 VirtIONet *n = to_virtio_net(vdev);
145
146 virtio_net_vhost_status(n, status);
147
148 if (!n->tx_waiting) {
149 return;
150 }
151
152 if (virtio_net_started(n, status) && !n->vhost_started) {
153 if (n->tx_timer) {
154 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100155 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200156 } else {
157 qemu_bh_schedule(n->tx_bh);
158 }
159 } else {
160 if (n->tx_timer) {
161 qemu_del_timer(n->tx_timer);
162 } else {
163 qemu_bh_cancel(n->tx_bh);
164 }
165 }
166}
167
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100168static void virtio_net_set_link_status(NetClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000169{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000170 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000171 uint16_t old_status = n->status;
172
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000173 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000174 n->status &= ~VIRTIO_NET_S_LINK_UP;
175 else
176 n->status |= VIRTIO_NET_S_LINK_UP;
177
178 if (n->status != old_status)
179 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200180
181 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000182}
183
aliguori002437c2009-02-05 22:36:20 +0000184static void virtio_net_reset(VirtIODevice *vdev)
185{
186 VirtIONet *n = to_virtio_net(vdev);
187
188 /* Reset back to compatibility mode */
189 n->promisc = 1;
190 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600191 n->alluni = 0;
192 n->nomulti = 0;
193 n->nouni = 0;
194 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000195
aliguorif21c0ed2009-02-05 22:36:32 +0000196 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000197 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600198 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600199 n->mac_table.multi_overflow = 0;
200 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000201 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
Michael S. Tsirkin41dc8a62013-01-16 11:37:40 +0200202 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
aliguorif21c0ed2009-02-05 22:36:32 +0000203 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000204}
205
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200206static void peer_test_vnet_hdr(VirtIONet *n)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100207{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000208 if (!n->nic->nc.peer)
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200209 return;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100210
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200211 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200212 return;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100213
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000214 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200215}
Mark McLoughlin3a330132009-10-22 17:43:45 +0100216
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200217static int peer_has_vnet_hdr(VirtIONet *n)
218{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100219 return n->has_vnet_hdr;
220}
221
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100222static int peer_has_ufo(VirtIONet *n)
223{
224 if (!peer_has_vnet_hdr(n))
225 return 0;
226
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000227 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100228
229 return n->has_ufo;
230}
231
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200232static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
233{
234 n->mergeable_rx_bufs = mergeable_rx_bufs;
235
236 n->guest_hdr_len = n->mergeable_rx_bufs ?
237 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
238
239 if (peer_has_vnet_hdr(n) &&
240 tap_has_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len)) {
241 tap_set_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len);
242 n->host_hdr_len = n->guest_hdr_len;
243 }
244}
245
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200246static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000247{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100248 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000249
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200250 features |= (1 << VIRTIO_NET_F_MAC);
251
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200252 if (!peer_has_vnet_hdr(n)) {
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200253 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
254 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
255 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
256 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100257
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200258 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
259 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
260 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
261 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
262 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100263
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200264 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
265 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
266 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100267 }
268
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200269 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200270 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200271 return features;
272 }
273 if (!tap_get_vhost_net(n->nic->nc.peer)) {
274 return features;
275 }
276 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000277}
278
aliguori8eca6b12009-04-05 17:40:08 +0000279static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
280{
281 uint32_t features = 0;
282
283 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
284 * but also these: */
285 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500286 features |= (1 << VIRTIO_NET_F_CSUM);
287 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
288 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
289 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000290
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200291 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000292}
293
aliguorifbe78f42008-12-17 19:13:11 +0000294static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
295{
296 VirtIONet *n = to_virtio_net(vdev);
297
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200298 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100299
300 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000301 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100302 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
303 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
304 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100305 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
306 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100307 }
David L Stevensdc14a392010-03-31 21:20:31 +0300308 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200309 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
David L Stevensdc14a392010-03-31 21:20:31 +0300310 return;
311 }
312 if (!tap_get_vhost_net(n->nic->nc.peer)) {
313 return;
314 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300315 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000316}
317
aliguori002437c2009-02-05 22:36:20 +0000318static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
319 VirtQueueElement *elem)
320{
321 uint8_t on;
322
323 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000324 error_report("virtio-net ctrl invalid rx mode command");
aliguori002437c2009-02-05 22:36:20 +0000325 exit(1);
326 }
327
328 on = ldub_p(elem->out_sg[1].iov_base);
329
330 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
331 n->promisc = on;
332 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
333 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600334 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
335 n->alluni = on;
336 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
337 n->nomulti = on;
338 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
339 n->nouni = on;
340 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
341 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000342 else
343 return VIRTIO_NET_ERR;
344
345 return VIRTIO_NET_OK;
346}
347
aliguorib6503ed2009-02-05 22:36:28 +0000348static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
349 VirtQueueElement *elem)
350{
351 struct virtio_net_ctrl_mac mac_data;
352
353 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
354 elem->out_sg[1].iov_len < sizeof(mac_data) ||
355 elem->out_sg[2].iov_len < sizeof(mac_data))
356 return VIRTIO_NET_ERR;
357
358 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600359 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600360 n->mac_table.uni_overflow = 0;
361 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000362 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
363
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100364 mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000365
366 if (sizeof(mac_data.entries) +
367 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
368 return VIRTIO_NET_ERR;
369
370 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
371 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
372 mac_data.entries * ETH_ALEN);
373 n->mac_table.in_use += mac_data.entries;
374 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600375 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000376 }
377
Alex Williamson2d9aba32009-06-05 14:47:13 -0600378 n->mac_table.first_multi = n->mac_table.in_use;
379
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100380 mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000381
382 if (sizeof(mac_data.entries) +
383 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
384 return VIRTIO_NET_ERR;
385
386 if (mac_data.entries) {
387 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
388 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
389 elem->out_sg[2].iov_base + sizeof(mac_data),
390 mac_data.entries * ETH_ALEN);
391 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600392 } else {
393 n->mac_table.multi_overflow = 1;
394 }
aliguorib6503ed2009-02-05 22:36:28 +0000395 }
396
397 return VIRTIO_NET_OK;
398}
399
aliguorif21c0ed2009-02-05 22:36:32 +0000400static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
401 VirtQueueElement *elem)
402{
403 uint16_t vid;
404
405 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000406 error_report("virtio-net ctrl invalid vlan command");
aliguorif21c0ed2009-02-05 22:36:32 +0000407 return VIRTIO_NET_ERR;
408 }
409
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100410 vid = lduw_p(elem->out_sg[1].iov_base);
aliguorif21c0ed2009-02-05 22:36:32 +0000411
412 if (vid >= MAX_VLAN)
413 return VIRTIO_NET_ERR;
414
415 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
416 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
417 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
418 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
419 else
420 return VIRTIO_NET_ERR;
421
422 return VIRTIO_NET_OK;
423}
424
aliguori3d11d362009-02-05 22:36:16 +0000425static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
426{
aliguori002437c2009-02-05 22:36:20 +0000427 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000428 struct virtio_net_ctrl_hdr ctrl;
429 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
430 VirtQueueElement elem;
431
432 while (virtqueue_pop(vq, &elem)) {
433 if ((elem.in_num < 1) || (elem.out_num < 1)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000434 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000435 exit(1);
436 }
437
438 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000439 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000440 error_report("virtio-net ctrl header not in correct element");
aliguori3d11d362009-02-05 22:36:16 +0000441 exit(1);
442 }
443
444 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
445 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
446
aliguori002437c2009-02-05 22:36:20 +0000447 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
448 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000449 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
450 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000451 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
452 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000453
aliguori3d11d362009-02-05 22:36:16 +0000454 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
455
456 virtqueue_push(vq, &elem, sizeof(status));
457 virtio_notify(vdev, vq);
458 }
459}
460
aliguorifbe78f42008-12-17 19:13:11 +0000461/* RX */
462
463static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
464{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100465 VirtIONet *n = to_virtio_net(vdev);
466
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000467 qemu_flush_queued_packets(&n->nic->nc);
aliguorifbe78f42008-12-17 19:13:11 +0000468}
469
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100470static int virtio_net_can_receive(NetClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000471{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000472 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200473 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200474 return 0;
475 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000476
aliguorifbe78f42008-12-17 19:13:11 +0000477 if (!virtio_queue_ready(n->rx_vq) ||
478 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
479 return 0;
480
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000481 return 1;
482}
483
484static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
485{
aliguorifbe78f42008-12-17 19:13:11 +0000486 if (virtio_queue_empty(n->rx_vq) ||
487 (n->mergeable_rx_bufs &&
488 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
489 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600490
491 /* To avoid a race condition where the guest has made some buffers
492 * available after the above check but before notification was
493 * enabled, check for available buffers again.
494 */
495 if (virtio_queue_empty(n->rx_vq) ||
496 (n->mergeable_rx_bufs &&
497 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
498 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000499 }
500
501 virtio_queue_set_notification(n->rx_vq, 0);
502 return 1;
503}
504
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100505/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
506 * it never finds out that the packets don't have valid checksums. This
507 * causes dhclient to get upset. Fedora's carried a patch for ages to
508 * fix this with Xen but it hasn't appeared in an upstream release of
509 * dhclient yet.
510 *
511 * To avoid breaking existing guests, we catch udp packets and add
512 * checksums. This is terrible but it's better than hacking the guest
513 * kernels.
514 *
515 * N.B. if we introduce a zero-copy API, this operation is no longer free so
516 * we should provide a mechanism to disable it to avoid polluting the host
517 * cache.
518 */
519static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200520 uint8_t *buf, size_t size)
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100521{
522 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
523 (size > 27 && size < 1500) && /* normal sized MTU */
524 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
525 (buf[23] == 17) && /* ip.protocol == UDP */
526 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200527 net_checksum_calculate(buf, size);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100528 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
529 }
530}
531
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200532static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
533 const void *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000534{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100535 if (n->has_vnet_hdr) {
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200536 /* FIXME this cast is evil */
537 void *wbuf = (void *)buf;
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200538 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
539 size - n->host_hdr_len);
540 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200541 } else {
542 struct virtio_net_hdr hdr = {
543 .flags = 0,
544 .gso_type = VIRTIO_NET_HDR_GSO_NONE
545 };
546 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100547 }
aliguorifbe78f42008-12-17 19:13:11 +0000548}
549
aliguori3831ab22009-02-05 22:36:24 +0000550static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
551{
552 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000553 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000554 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000555 int i;
aliguori3831ab22009-02-05 22:36:24 +0000556
557 if (n->promisc)
558 return 1;
559
Michael S. Tsirkine043ebc2012-09-24 16:27:27 +0200560 ptr += n->host_hdr_len;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100561
aliguorif21c0ed2009-02-05 22:36:32 +0000562 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
563 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
564 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
565 return 0;
566 }
567
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600568 if (ptr[0] & 1) { // multicast
569 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600570 return !n->nobcast;
571 } else if (n->nomulti) {
572 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600573 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600574 return 1;
575 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600576
577 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
578 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
579 return 1;
580 }
581 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600582 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600583 if (n->nouni) {
584 return 0;
585 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600586 return 1;
587 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600588 return 1;
589 }
aliguori3831ab22009-02-05 22:36:24 +0000590
Alex Williamson2d9aba32009-06-05 14:47:13 -0600591 for (i = 0; i < n->mac_table.first_multi; i++) {
592 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
593 return 1;
594 }
595 }
aliguorib6503ed2009-02-05 22:36:28 +0000596 }
597
aliguori3831ab22009-02-05 22:36:24 +0000598 return 0;
599}
600
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100601static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000602{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000603 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200604 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
605 struct virtio_net_hdr_mrg_rxbuf mhdr;
606 unsigned mhdr_cnt = 0;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200607 size_t offset, i, guest_offset;
aliguorifbe78f42008-12-17 19:13:11 +0000608
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000609 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000610 return -1;
611
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300612 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200613 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100614 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000615
aliguori3831ab22009-02-05 22:36:24 +0000616 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100617 return size;
aliguori3831ab22009-02-05 22:36:24 +0000618
aliguorifbe78f42008-12-17 19:13:11 +0000619 offset = i = 0;
620
621 while (offset < size) {
622 VirtQueueElement elem;
623 int len, total;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200624 const struct iovec *sg = elem.in_sg;
aliguorifbe78f42008-12-17 19:13:11 +0000625
Amit Shah22c253d2010-01-13 16:24:43 +0530626 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000627
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300628 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000629 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100630 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000631 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300632 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000633 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300634 i, n->mergeable_rx_bufs, offset, size,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200635 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000636 exit(1);
637 }
638
639 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000640 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000641 exit(1);
642 }
643
aliguorifbe78f42008-12-17 19:13:11 +0000644 if (i == 0) {
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200645 assert(offset == 0);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200646 if (n->mergeable_rx_bufs) {
647 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
648 sg, elem.in_num,
649 offsetof(typeof(mhdr), num_buffers),
650 sizeof(mhdr.num_buffers));
651 }
aliguorifbe78f42008-12-17 19:13:11 +0000652
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200653 receive_header(n, sg, elem.in_num, buf, size);
654 offset = n->host_hdr_len;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200655 total += n->guest_hdr_len;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200656 guest_offset = n->guest_hdr_len;
657 } else {
658 guest_offset = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000659 }
660
661 /* copy in packet. ugh */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200662 len = iov_from_buf(sg, elem.in_num, guest_offset,
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400663 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000664 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300665 offset += len;
666 /* If buffers can't be merged, at this point we
667 * must have consumed the complete packet.
668 * Otherwise, drop it. */
669 if (!n->mergeable_rx_bufs && offset < size) {
670#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000671 error_report("virtio-net truncated non-mergeable packet: "
672 "i %zd mergeable %d offset %zd, size %zd, "
673 "guest hdr len %zd, host hdr len %zd",
674 i, n->mergeable_rx_bufs,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200675 offset, size, n->guest_hdr_len, n->host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300676#endif
677 return size;
678 }
aliguorifbe78f42008-12-17 19:13:11 +0000679
680 /* signal other side */
681 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000682 }
683
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200684 if (mhdr_cnt) {
685 stw_p(&mhdr.num_buffers, i);
686 iov_from_buf(mhdr_sg, mhdr_cnt,
687 0,
688 &mhdr.num_buffers, sizeof mhdr.num_buffers);
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100689 }
aliguorifbe78f42008-12-17 19:13:11 +0000690
691 virtqueue_flush(n->rx_vq, i);
692 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100693
694 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000695}
696
Alex Williamsone3f30482010-09-02 09:00:57 -0600697static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100698
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100699static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100700{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000701 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100702
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200703 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
Mark McLoughlin62433752009-06-18 18:21:36 +0100704 virtio_notify(&n->vdev, n->tx_vq);
705
706 n->async_tx.elem.out_num = n->async_tx.len = 0;
707
708 virtio_queue_set_notification(n->tx_vq, 1);
709 virtio_net_flush_tx(n, n->tx_vq);
710}
711
aliguorifbe78f42008-12-17 19:13:11 +0000712/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600713static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000714{
715 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600716 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600717 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
718 return num_packets;
719 }
aliguorifbe78f42008-12-17 19:13:11 +0000720
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200721 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200722
Mark McLoughlin62433752009-06-18 18:21:36 +0100723 if (n->async_tx.elem.out_num) {
724 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600725 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100726 }
727
aliguorifbe78f42008-12-17 19:13:11 +0000728 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200729 ssize_t ret, len;
aliguorifbe78f42008-12-17 19:13:11 +0000730 unsigned int out_num = elem.out_num;
731 struct iovec *out_sg = &elem.out_sg[0];
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200732 struct iovec sg[VIRTQUEUE_MAX_SIZE];
aliguorifbe78f42008-12-17 19:13:11 +0000733
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200734 if (out_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000735 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000736 exit(1);
737 }
738
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200739 /*
740 * If host wants to see the guest header as is, we can
741 * pass it on unchanged. Otherwise, copy just the parts
742 * that host is interested in.
743 */
744 assert(n->host_hdr_len <= n->guest_hdr_len);
745 if (n->host_hdr_len != n->guest_hdr_len) {
746 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
747 out_sg, out_num,
748 0, n->host_hdr_len);
749 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
750 out_sg, out_num,
751 n->guest_hdr_len, -1);
752 out_num = sg_num;
753 out_sg = sg;
aliguorifbe78f42008-12-17 19:13:11 +0000754 }
755
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200756 len = n->guest_hdr_len;
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200757
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000758 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100759 virtio_net_tx_complete);
760 if (ret == 0) {
761 virtio_queue_set_notification(n->tx_vq, 0);
762 n->async_tx.elem = elem;
763 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600764 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100765 }
766
767 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000768
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200769 virtqueue_push(vq, &elem, 0);
aliguorifbe78f42008-12-17 19:13:11 +0000770 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600771
772 if (++num_packets >= n->tx_burst) {
773 break;
774 }
aliguorifbe78f42008-12-17 19:13:11 +0000775 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600776 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000777}
778
Alex Williamsona697a332010-09-02 09:01:10 -0600779static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000780{
781 VirtIONet *n = to_virtio_net(vdev);
782
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200783 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200784 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200785 n->tx_waiting = 1;
786 return;
787 }
788
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600789 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000790 virtio_queue_set_notification(vq, 1);
791 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600792 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000793 virtio_net_flush_tx(n, vq);
794 } else {
795 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100796 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600797 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000798 virtio_queue_set_notification(vq, 0);
799 }
800}
801
Alex Williamsona697a332010-09-02 09:01:10 -0600802static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
803{
804 VirtIONet *n = to_virtio_net(vdev);
805
806 if (unlikely(n->tx_waiting)) {
807 return;
808 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200809 n->tx_waiting = 1;
810 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200811 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200812 return;
813 }
Alex Williamsona697a332010-09-02 09:01:10 -0600814 virtio_queue_set_notification(vq, 0);
815 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600816}
817
aliguorifbe78f42008-12-17 19:13:11 +0000818static void virtio_net_tx_timer(void *opaque)
819{
820 VirtIONet *n = opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200821 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000822
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600823 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000824
825 /* Just in case the driver is not ready on more */
826 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
827 return;
828
829 virtio_queue_set_notification(n->tx_vq, 1);
830 virtio_net_flush_tx(n, n->tx_vq);
831}
832
Alex Williamsona697a332010-09-02 09:01:10 -0600833static void virtio_net_tx_bh(void *opaque)
834{
835 VirtIONet *n = opaque;
836 int32_t ret;
837
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200838 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200839
Alex Williamsona697a332010-09-02 09:01:10 -0600840 n->tx_waiting = 0;
841
842 /* Just in case the driver is not ready on more */
843 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
844 return;
845
846 ret = virtio_net_flush_tx(n, n->tx_vq);
847 if (ret == -EBUSY) {
848 return; /* Notification re-enable handled by tx_complete */
849 }
850
851 /* If we flush a full burst of packets, assume there are
852 * more coming and immediately reschedule */
853 if (ret >= n->tx_burst) {
854 qemu_bh_schedule(n->tx_bh);
855 n->tx_waiting = 1;
856 return;
857 }
858
859 /* If less than a full burst, re-enable notification and flush
860 * anything that may have come in while we weren't looking. If
861 * we find something, assume the guest is still active and reschedule */
862 virtio_queue_set_notification(n->tx_vq, 1);
863 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
864 virtio_queue_set_notification(n->tx_vq, 0);
865 qemu_bh_schedule(n->tx_bh);
866 n->tx_waiting = 1;
867 }
868}
869
aliguorifbe78f42008-12-17 19:13:11 +0000870static void virtio_net_save(QEMUFile *f, void *opaque)
871{
872 VirtIONet *n = opaque;
873
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200874 /* At this point, backend must be stopped, otherwise
875 * it might keep writing to memory. */
876 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000877 virtio_save(&n->vdev, f);
878
aliguori79674062009-02-05 22:36:12 +0000879 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600880 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000881 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000882 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600883 qemu_put_byte(f, n->promisc);
884 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000885 qemu_put_be32(f, n->mac_table.in_use);
886 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000887 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100888 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600889 qemu_put_byte(f, n->mac_table.multi_overflow);
890 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600891 qemu_put_byte(f, n->alluni);
892 qemu_put_byte(f, n->nomulti);
893 qemu_put_byte(f, n->nouni);
894 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100895 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000896}
897
898static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
899{
900 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600901 int i;
Orit Wassermann2a633c42012-05-16 12:21:35 +0200902 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000903
aliguori9d6271b2009-02-05 22:36:04 +0000904 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000905 return -EINVAL;
906
Orit Wassermann2a633c42012-05-16 12:21:35 +0200907 ret = virtio_load(&n->vdev, f);
908 if (ret) {
909 return ret;
910 }
aliguorifbe78f42008-12-17 19:13:11 +0000911
aliguori79674062009-02-05 22:36:12 +0000912 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600913 n->tx_waiting = qemu_get_be32(f);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200914
915 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
aliguorifbe78f42008-12-17 19:13:11 +0000916
aliguori9d6271b2009-02-05 22:36:04 +0000917 if (version_id >= 3)
918 n->status = qemu_get_be16(f);
919
aliguori002437c2009-02-05 22:36:20 +0000920 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600921 if (version_id < 8) {
922 n->promisc = qemu_get_be32(f);
923 n->allmulti = qemu_get_be32(f);
924 } else {
925 n->promisc = qemu_get_byte(f);
926 n->allmulti = qemu_get_byte(f);
927 }
aliguori002437c2009-02-05 22:36:20 +0000928 }
929
aliguorib6503ed2009-02-05 22:36:28 +0000930 if (version_id >= 5) {
931 n->mac_table.in_use = qemu_get_be32(f);
932 /* MAC_TABLE_ENTRIES may be different from the saved image */
933 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
934 qemu_get_buffer(f, n->mac_table.macs,
935 n->mac_table.in_use * ETH_ALEN);
936 } else if (n->mac_table.in_use) {
Juan Quintelae398d612012-08-29 19:03:09 +0200937 uint8_t *buf = g_malloc0(n->mac_table.in_use);
938 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
939 g_free(buf);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600940 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000941 n->mac_table.in_use = 0;
942 }
943 }
944
aliguorif21c0ed2009-02-05 22:36:32 +0000945 if (version_id >= 6)
946 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
947
Mark McLoughlin3a330132009-10-22 17:43:45 +0100948 if (version_id >= 7) {
949 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100950 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100951 return -1;
952 }
953
954 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000955 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200956 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
957 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
958 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
959 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
960 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100961 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600962 }
963
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600964 if (version_id >= 9) {
965 n->mac_table.multi_overflow = qemu_get_byte(f);
966 n->mac_table.uni_overflow = qemu_get_byte(f);
967 }
968
Alex Williamson015cb162009-06-05 14:47:18 -0600969 if (version_id >= 10) {
970 n->alluni = qemu_get_byte(f);
971 n->nomulti = qemu_get_byte(f);
972 n->nouni = qemu_get_byte(f);
973 n->nobcast = qemu_get_byte(f);
974 }
975
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100976 if (version_id >= 11) {
977 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100978 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100979 return -1;
980 }
981 }
982
Alex Williamson2d9aba32009-06-05 14:47:13 -0600983 /* Find the first multicast entry in the saved MAC filter */
984 for (i = 0; i < n->mac_table.in_use; i++) {
985 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
986 break;
987 }
988 }
989 n->mac_table.first_multi = i;
Amos Kong98991482012-09-28 10:06:02 +0800990
991 /* nc.link_down can't be migrated, so infer link_down according
992 * to link status bit in n->status */
993 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
994
aliguorifbe78f42008-12-17 19:13:11 +0000995 return 0;
996}
997
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100998static void virtio_net_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000999{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001000 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +00001001
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001002 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +00001003}
1004
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001005static NetClientInfo net_virtio_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001006 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001007 .size = sizeof(NICState),
1008 .can_receive = virtio_net_can_receive,
1009 .receive = virtio_net_receive,
1010 .cleanup = virtio_net_cleanup,
1011 .link_status_changed = virtio_net_set_link_status,
1012};
1013
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001014static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1015{
1016 VirtIONet *n = to_virtio_net(vdev);
1017 assert(n->vhost_started);
1018 return vhost_net_virtqueue_pending(tap_get_vhost_net(n->nic->nc.peer), idx);
1019}
1020
1021static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1022 bool mask)
1023{
1024 VirtIONet *n = to_virtio_net(vdev);
1025 assert(n->vhost_started);
1026 vhost_net_virtqueue_mask(tap_get_vhost_net(n->nic->nc.peer),
1027 vdev, idx, mask);
1028}
1029
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001030VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1031 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001032{
1033 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001034
Paul Brook53c25ce2009-05-18 14:51:59 +01001035 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1036 sizeof(struct virtio_net_config),
1037 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001038
aliguori0f03eca2009-02-05 22:36:08 +00001039 n->vdev.get_config = virtio_net_get_config;
1040 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001041 n->vdev.get_features = virtio_net_get_features;
1042 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001043 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001044 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001045 n->vdev.set_status = virtio_net_set_status;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001046 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1047 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
aliguorifbe78f42008-12-17 19:13:11 +00001048 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001049
1050 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001051 error_report("virtio-net: "
1052 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1053 net->tx);
1054 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001055 }
1056
1057 if (net->tx && !strcmp(net->tx, "timer")) {
1058 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
Paolo Bonzini74475452011-03-11 16:47:48 +01001059 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
Alex Williamsona697a332010-09-02 09:01:10 -06001060 n->tx_timeout = net->txtimer;
1061 } else {
1062 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1063 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1064 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001065 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001066 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001067 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001068 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001069
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001070 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001071 peer_test_vnet_hdr(n);
1072 if (peer_has_vnet_hdr(n)) {
1073 tap_using_vnet_hdr(n->nic->nc.peer, 1);
1074 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1075 } else {
1076 n->host_hdr_len = 0;
1077 }
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001078
1079 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001080
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001081 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001082 n->tx_burst = net->txburst;
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +02001083 virtio_net_set_mrg_rx_bufs(n, 0);
aliguori002437c2009-02-05 22:36:20 +00001084 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001085
Anthony Liguori7267c092011-08-20 22:09:37 -05001086 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001087
Anthony Liguori7267c092011-08-20 22:09:37 -05001088 n->vlans = g_malloc0(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001089
Alex Williamson01657c82010-06-25 11:09:28 -06001090 n->qdev = dev;
1091 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001092 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001093
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001094 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1095
Paul Brook53c25ce2009-05-18 14:51:59 +01001096 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001097}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001098
1099void virtio_net_exit(VirtIODevice *vdev)
1100{
1101 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001102
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001103 /* This will stop vhost backend if appropriate. */
1104 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001105
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001106 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001107
Alex Williamson01657c82010-06-25 11:09:28 -06001108 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001109
Anthony Liguori7267c092011-08-20 22:09:37 -05001110 g_free(n->mac_table.macs);
1111 g_free(n->vlans);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001112
Alex Williamsona697a332010-09-02 09:01:10 -06001113 if (n->tx_timer) {
1114 qemu_del_timer(n->tx_timer);
1115 qemu_free_timer(n->tx_timer);
1116 } else {
1117 qemu_bh_delete(n->tx_bh);
1118 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001119
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001120 qemu_del_net_client(&n->nic->nc);
Amit Shahb52dfd72011-07-27 14:00:31 +05301121 virtio_cleanup(&n->vdev);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001122}