blob: 7e1688cf69dc5272fd84a27aaf031ce3ce1c6134 [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
Amit Shahe4d56392010-04-27 18:04:05 +053014#include "iov.h"
aliguorifbe78f42008-12-17 19:13:11 +000015#include "virtio.h"
16#include "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"
Markus Armbruster2f792012010-02-18 16:24:31 +010019#include "qemu-error.h"
aliguorifbe78f42008-12-17 19:13:11 +000020#include "qemu-timer.h"
21#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;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010044 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010045 struct {
46 VirtQueueElement elem;
47 ssize_t len;
48 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000049 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060050 uint8_t promisc;
51 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060052 uint8_t alluni;
53 uint8_t nomulti;
54 uint8_t nouni;
55 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020056 uint8_t vhost_started;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +020057 bool vm_running;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020058 VMChangeStateEntry *vmstate;
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
aliguori554c97d2009-01-08 19:46:33 +000084 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. Tsirkinafbaa7b2010-09-27 18:41:30 +0200102static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
103{
104 VirtIONet *n = to_virtio_net(vdev);
105 if (!n->nic->nc.peer) {
106 return;
107 }
108 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
109 return;
110 }
111
112 if (!tap_get_vhost_net(n->nic->nc.peer)) {
113 return;
114 }
115 if (!!n->vhost_started == ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
116 (n->status & VIRTIO_NET_S_LINK_UP) &&
117 n->vm_running)) {
118 return;
119 }
120 if (!n->vhost_started) {
121 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
122 if (r < 0) {
123 fprintf(stderr, "unable to start vhost net: %d: "
124 "falling back on userspace virtio\n", -r);
125 } else {
126 n->vhost_started = 1;
127 }
128 } else {
129 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
130 n->vhost_started = 0;
131 }
132}
133
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000134static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000135{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000136 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000137 uint16_t old_status = n->status;
138
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000139 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000140 n->status &= ~VIRTIO_NET_S_LINK_UP;
141 else
142 n->status |= VIRTIO_NET_S_LINK_UP;
143
144 if (n->status != old_status)
145 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200146
147 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000148}
149
aliguori002437c2009-02-05 22:36:20 +0000150static void virtio_net_reset(VirtIODevice *vdev)
151{
152 VirtIONet *n = to_virtio_net(vdev);
153
154 /* Reset back to compatibility mode */
155 n->promisc = 1;
156 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600157 n->alluni = 0;
158 n->nomulti = 0;
159 n->nouni = 0;
160 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000161
aliguorif21c0ed2009-02-05 22:36:32 +0000162 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000163 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600164 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600165 n->mac_table.multi_overflow = 0;
166 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000167 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000168 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000169}
170
Mark McLoughlin3a330132009-10-22 17:43:45 +0100171static int peer_has_vnet_hdr(VirtIONet *n)
172{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000173 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100174 return 0;
175
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000176 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100177 return 0;
178
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000179 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100180
181 return n->has_vnet_hdr;
182}
183
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100184static int peer_has_ufo(VirtIONet *n)
185{
186 if (!peer_has_vnet_hdr(n))
187 return 0;
188
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000189 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100190
191 return n->has_ufo;
192}
193
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200194static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000195{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100196 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000197
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200198 features |= (1 << VIRTIO_NET_F_MAC);
199
Mark McLoughlin3a330132009-10-22 17:43:45 +0100200 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000201 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200202 } else {
203 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
204 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
205 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
206 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100207
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200208 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
209 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
210 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
211 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
212 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100213
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200214 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
215 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
216 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100217 }
218
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200219 if (!n->nic->nc.peer ||
220 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
221 return features;
222 }
223 if (!tap_get_vhost_net(n->nic->nc.peer)) {
224 return features;
225 }
226 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000227}
228
aliguori8eca6b12009-04-05 17:40:08 +0000229static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
230{
231 uint32_t features = 0;
232
233 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
234 * but also these: */
235 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500236 features |= (1 << VIRTIO_NET_F_CSUM);
237 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
238 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
239 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000240
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200241 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000242}
243
aliguorifbe78f42008-12-17 19:13:11 +0000244static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
245{
246 VirtIONet *n = to_virtio_net(vdev);
247
248 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100249
250 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000251 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100252 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
253 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
254 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100255 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
256 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100257 }
David L Stevensdc14a392010-03-31 21:20:31 +0300258 if (!n->nic->nc.peer ||
259 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
260 return;
261 }
262 if (!tap_get_vhost_net(n->nic->nc.peer)) {
263 return;
264 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300265 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000266}
267
aliguori002437c2009-02-05 22:36:20 +0000268static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
269 VirtQueueElement *elem)
270{
271 uint8_t on;
272
273 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
274 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
275 exit(1);
276 }
277
278 on = ldub_p(elem->out_sg[1].iov_base);
279
280 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
281 n->promisc = on;
282 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
283 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600284 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
285 n->alluni = on;
286 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
287 n->nomulti = on;
288 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
289 n->nouni = on;
290 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
291 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000292 else
293 return VIRTIO_NET_ERR;
294
295 return VIRTIO_NET_OK;
296}
297
aliguorib6503ed2009-02-05 22:36:28 +0000298static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
299 VirtQueueElement *elem)
300{
301 struct virtio_net_ctrl_mac mac_data;
302
303 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
304 elem->out_sg[1].iov_len < sizeof(mac_data) ||
305 elem->out_sg[2].iov_len < sizeof(mac_data))
306 return VIRTIO_NET_ERR;
307
308 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600309 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600310 n->mac_table.uni_overflow = 0;
311 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000312 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
313
314 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
315
316 if (sizeof(mac_data.entries) +
317 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
318 return VIRTIO_NET_ERR;
319
320 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
321 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
322 mac_data.entries * ETH_ALEN);
323 n->mac_table.in_use += mac_data.entries;
324 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600325 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000326 }
327
Alex Williamson2d9aba32009-06-05 14:47:13 -0600328 n->mac_table.first_multi = n->mac_table.in_use;
329
aliguorib6503ed2009-02-05 22:36:28 +0000330 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
331
332 if (sizeof(mac_data.entries) +
333 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
334 return VIRTIO_NET_ERR;
335
336 if (mac_data.entries) {
337 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
338 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
339 elem->out_sg[2].iov_base + sizeof(mac_data),
340 mac_data.entries * ETH_ALEN);
341 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600342 } else {
343 n->mac_table.multi_overflow = 1;
344 }
aliguorib6503ed2009-02-05 22:36:28 +0000345 }
346
347 return VIRTIO_NET_OK;
348}
349
aliguorif21c0ed2009-02-05 22:36:32 +0000350static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
351 VirtQueueElement *elem)
352{
353 uint16_t vid;
354
355 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
356 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
357 return VIRTIO_NET_ERR;
358 }
359
360 vid = lduw_le_p(elem->out_sg[1].iov_base);
361
362 if (vid >= MAX_VLAN)
363 return VIRTIO_NET_ERR;
364
365 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
366 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
367 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
368 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
369 else
370 return VIRTIO_NET_ERR;
371
372 return VIRTIO_NET_OK;
373}
374
aliguori3d11d362009-02-05 22:36:16 +0000375static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
376{
aliguori002437c2009-02-05 22:36:20 +0000377 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000378 struct virtio_net_ctrl_hdr ctrl;
379 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
380 VirtQueueElement elem;
381
382 while (virtqueue_pop(vq, &elem)) {
383 if ((elem.in_num < 1) || (elem.out_num < 1)) {
384 fprintf(stderr, "virtio-net ctrl missing headers\n");
385 exit(1);
386 }
387
388 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000389 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000390 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
391 exit(1);
392 }
393
394 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
395 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
396
aliguori002437c2009-02-05 22:36:20 +0000397 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
398 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000399 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
400 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000401 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
402 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000403
aliguori3d11d362009-02-05 22:36:16 +0000404 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
405
406 virtqueue_push(vq, &elem, sizeof(status));
407 virtio_notify(vdev, vq);
408 }
409}
410
aliguorifbe78f42008-12-17 19:13:11 +0000411/* RX */
412
413static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
414{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100415 VirtIONet *n = to_virtio_net(vdev);
416
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000417 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400418
419 /* We now have RX buffers, signal to the IO thread to break out of the
420 * select to re-poll the tap file descriptor */
421 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000422}
423
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000424static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000425{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000426 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000427
aliguorifbe78f42008-12-17 19:13:11 +0000428 if (!virtio_queue_ready(n->rx_vq) ||
429 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
430 return 0;
431
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000432 return 1;
433}
434
435static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
436{
aliguorifbe78f42008-12-17 19:13:11 +0000437 if (virtio_queue_empty(n->rx_vq) ||
438 (n->mergeable_rx_bufs &&
439 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
440 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600441
442 /* To avoid a race condition where the guest has made some buffers
443 * available after the above check but before notification was
444 * enabled, check for available buffers again.
445 */
446 if (virtio_queue_empty(n->rx_vq) ||
447 (n->mergeable_rx_bufs &&
448 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
449 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000450 }
451
452 virtio_queue_set_notification(n->rx_vq, 0);
453 return 1;
454}
455
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100456/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
457 * it never finds out that the packets don't have valid checksums. This
458 * causes dhclient to get upset. Fedora's carried a patch for ages to
459 * fix this with Xen but it hasn't appeared in an upstream release of
460 * dhclient yet.
461 *
462 * To avoid breaking existing guests, we catch udp packets and add
463 * checksums. This is terrible but it's better than hacking the guest
464 * kernels.
465 *
466 * N.B. if we introduce a zero-copy API, this operation is no longer free so
467 * we should provide a mechanism to disable it to avoid polluting the host
468 * cache.
469 */
470static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
471 const uint8_t *buf, size_t size)
472{
473 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
474 (size > 27 && size < 1500) && /* normal sized MTU */
475 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
476 (buf[23] == 17) && /* ip.protocol == UDP */
477 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
478 /* FIXME this cast is evil */
479 net_checksum_calculate((uint8_t *)buf, size);
480 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
481 }
482}
483
aliguorifbe78f42008-12-17 19:13:11 +0000484static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000485 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000486{
blueswir13f4cb3d2009-04-13 16:31:01 +0000487 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000488 int offset = 0;
489
490 hdr->flags = 0;
491 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
492
Mark McLoughlin3a330132009-10-22 17:43:45 +0100493 if (n->has_vnet_hdr) {
494 memcpy(hdr, buf, sizeof(*hdr));
495 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100496 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100497 }
498
aliguorifbe78f42008-12-17 19:13:11 +0000499 /* We only ever receive a struct virtio_net_hdr from the tapfd,
500 * but we may be passing along a larger header to the guest.
501 */
502 iov[0].iov_base += hdr_len;
503 iov[0].iov_len -= hdr_len;
504
505 return offset;
506}
507
aliguori3831ab22009-02-05 22:36:24 +0000508static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
509{
510 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000511 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000512 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000513 int i;
aliguori3831ab22009-02-05 22:36:24 +0000514
515 if (n->promisc)
516 return 1;
517
Mark McLoughlin3a330132009-10-22 17:43:45 +0100518 if (n->has_vnet_hdr) {
519 ptr += sizeof(struct virtio_net_hdr);
520 }
521
aliguorif21c0ed2009-02-05 22:36:32 +0000522 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
523 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
524 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
525 return 0;
526 }
527
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600528 if (ptr[0] & 1) { // multicast
529 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600530 return !n->nobcast;
531 } else if (n->nomulti) {
532 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600533 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600534 return 1;
535 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600536
537 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
538 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
539 return 1;
540 }
541 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600542 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600543 if (n->nouni) {
544 return 0;
545 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600546 return 1;
547 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600548 return 1;
549 }
aliguori3831ab22009-02-05 22:36:24 +0000550
Alex Williamson2d9aba32009-06-05 14:47:13 -0600551 for (i = 0; i < n->mac_table.first_multi; i++) {
552 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
553 return 1;
554 }
555 }
aliguorib6503ed2009-02-05 22:36:28 +0000556 }
557
aliguori3831ab22009-02-05 22:36:24 +0000558 return 0;
559}
560
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000561static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000562{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000563 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000564 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300565 size_t guest_hdr_len, offset, i, host_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000566
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000567 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000568 return -1;
569
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300570 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300571 guest_hdr_len = n->mergeable_rx_bufs ?
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300572 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
573
574
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300575 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
576 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100577 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000578
aliguori3831ab22009-02-05 22:36:24 +0000579 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100580 return size;
aliguori3831ab22009-02-05 22:36:24 +0000581
aliguorifbe78f42008-12-17 19:13:11 +0000582 offset = i = 0;
583
584 while (offset < size) {
585 VirtQueueElement elem;
586 int len, total;
587 struct iovec sg[VIRTQUEUE_MAX_SIZE];
588
Amit Shah22c253d2010-01-13 16:24:43 +0530589 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000590
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300591 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000592 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100593 return -1;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300594 fprintf(stderr, "virtio-net unexpected empty queue: "
595 "i %zd mergeable %d offset %zd, size %zd, "
596 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
597 i, n->mergeable_rx_bufs, offset, size,
598 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000599 exit(1);
600 }
601
602 if (elem.in_num < 1) {
603 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
604 exit(1);
605 }
606
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300607 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
aliguorifbe78f42008-12-17 19:13:11 +0000608 fprintf(stderr, "virtio-net header not in first element\n");
609 exit(1);
610 }
611
612 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
613
614 if (i == 0) {
615 if (n->mergeable_rx_bufs)
616 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
617
618 offset += receive_header(n, sg, elem.in_num,
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300619 buf + offset, size - offset, guest_hdr_len);
620 total += guest_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000621 }
622
623 /* copy in packet. ugh */
Amit Shahe4d56392010-04-27 18:04:05 +0530624 len = iov_from_buf(sg, elem.in_num,
625 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000626 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300627 offset += len;
628 /* If buffers can't be merged, at this point we
629 * must have consumed the complete packet.
630 * Otherwise, drop it. */
631 if (!n->mergeable_rx_bufs && offset < size) {
632#if 0
633 fprintf(stderr, "virtio-net truncated non-mergeable packet: "
634
635 "i %zd mergeable %d offset %zd, size %zd, "
636 "guest hdr len %zd, host hdr len %zd\n",
637 i, n->mergeable_rx_bufs,
638 offset, size, guest_hdr_len, host_hdr_len);
639#endif
640 return size;
641 }
aliguorifbe78f42008-12-17 19:13:11 +0000642
643 /* signal other side */
644 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000645 }
646
647 if (mhdr)
648 mhdr->num_buffers = i;
649
650 virtqueue_flush(n->rx_vq, i);
651 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100652
653 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000654}
655
Alex Williamsone3f30482010-09-02 09:00:57 -0600656static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100657
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000658static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100659{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000660 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100661
662 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
663 virtio_notify(&n->vdev, n->tx_vq);
664
665 n->async_tx.elem.out_num = n->async_tx.len = 0;
666
667 virtio_queue_set_notification(n->tx_vq, 1);
668 virtio_net_flush_tx(n, n->tx_vq);
669}
670
aliguorifbe78f42008-12-17 19:13:11 +0000671/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600672static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000673{
674 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600675 int32_t num_packets = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000676
Alex Williamsone3f30482010-09-02 09:00:57 -0600677 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
678 return num_packets;
679 }
aliguorifbe78f42008-12-17 19:13:11 +0000680
Mark McLoughlin62433752009-06-18 18:21:36 +0100681 if (n->async_tx.elem.out_num) {
682 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600683 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100684 }
685
aliguorifbe78f42008-12-17 19:13:11 +0000686 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100687 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000688 unsigned int out_num = elem.out_num;
689 struct iovec *out_sg = &elem.out_sg[0];
690 unsigned hdr_len;
691
692 /* hdr_len refers to the header received from the guest */
693 hdr_len = n->mergeable_rx_bufs ?
694 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
695 sizeof(struct virtio_net_hdr);
696
697 if (out_num < 1 || out_sg->iov_len != hdr_len) {
698 fprintf(stderr, "virtio-net header not in first element\n");
699 exit(1);
700 }
701
702 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100703 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000704 out_num--;
705 out_sg++;
706 len += hdr_len;
707 } else if (n->mergeable_rx_bufs) {
708 /* tapfd expects a struct virtio_net_hdr */
709 hdr_len -= sizeof(struct virtio_net_hdr);
710 out_sg->iov_len -= hdr_len;
711 len += hdr_len;
712 }
713
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000714 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100715 virtio_net_tx_complete);
716 if (ret == 0) {
717 virtio_queue_set_notification(n->tx_vq, 0);
718 n->async_tx.elem = elem;
719 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600720 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100721 }
722
723 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000724
725 virtqueue_push(vq, &elem, len);
726 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600727
728 if (++num_packets >= n->tx_burst) {
729 break;
730 }
aliguorifbe78f42008-12-17 19:13:11 +0000731 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600732 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000733}
734
Alex Williamsona697a332010-09-02 09:01:10 -0600735static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000736{
737 VirtIONet *n = to_virtio_net(vdev);
738
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600739 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000740 virtio_queue_set_notification(vq, 1);
741 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600742 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000743 virtio_net_flush_tx(n, vq);
744 } else {
745 qemu_mod_timer(n->tx_timer,
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600746 qemu_get_clock(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600747 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000748 virtio_queue_set_notification(vq, 0);
749 }
750}
751
Alex Williamsona697a332010-09-02 09:01:10 -0600752static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
753{
754 VirtIONet *n = to_virtio_net(vdev);
755
756 if (unlikely(n->tx_waiting)) {
757 return;
758 }
759 virtio_queue_set_notification(vq, 0);
760 qemu_bh_schedule(n->tx_bh);
761 n->tx_waiting = 1;
762}
763
aliguorifbe78f42008-12-17 19:13:11 +0000764static void virtio_net_tx_timer(void *opaque)
765{
766 VirtIONet *n = opaque;
767
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600768 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000769
770 /* Just in case the driver is not ready on more */
771 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
772 return;
773
774 virtio_queue_set_notification(n->tx_vq, 1);
775 virtio_net_flush_tx(n, n->tx_vq);
776}
777
Alex Williamsona697a332010-09-02 09:01:10 -0600778static void virtio_net_tx_bh(void *opaque)
779{
780 VirtIONet *n = opaque;
781 int32_t ret;
782
783 n->tx_waiting = 0;
784
785 /* Just in case the driver is not ready on more */
786 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
787 return;
788
789 ret = virtio_net_flush_tx(n, n->tx_vq);
790 if (ret == -EBUSY) {
791 return; /* Notification re-enable handled by tx_complete */
792 }
793
794 /* If we flush a full burst of packets, assume there are
795 * more coming and immediately reschedule */
796 if (ret >= n->tx_burst) {
797 qemu_bh_schedule(n->tx_bh);
798 n->tx_waiting = 1;
799 return;
800 }
801
802 /* If less than a full burst, re-enable notification and flush
803 * anything that may have come in while we weren't looking. If
804 * we find something, assume the guest is still active and reschedule */
805 virtio_queue_set_notification(n->tx_vq, 1);
806 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
807 virtio_queue_set_notification(n->tx_vq, 0);
808 qemu_bh_schedule(n->tx_bh);
809 n->tx_waiting = 1;
810 }
811}
812
aliguorifbe78f42008-12-17 19:13:11 +0000813static void virtio_net_save(QEMUFile *f, void *opaque)
814{
815 VirtIONet *n = opaque;
816
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200817 /* At this point, backend must be stopped, otherwise
818 * it might keep writing to memory. */
819 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000820 virtio_save(&n->vdev, f);
821
aliguori79674062009-02-05 22:36:12 +0000822 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600823 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000824 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000825 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600826 qemu_put_byte(f, n->promisc);
827 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000828 qemu_put_be32(f, n->mac_table.in_use);
829 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000830 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100831 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600832 qemu_put_byte(f, n->mac_table.multi_overflow);
833 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600834 qemu_put_byte(f, n->alluni);
835 qemu_put_byte(f, n->nomulti);
836 qemu_put_byte(f, n->nouni);
837 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100838 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000839}
840
841static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
842{
843 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600844 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000845
aliguori9d6271b2009-02-05 22:36:04 +0000846 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000847 return -EINVAL;
848
849 virtio_load(&n->vdev, f);
850
aliguori79674062009-02-05 22:36:12 +0000851 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600852 n->tx_waiting = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000853 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000854
aliguori9d6271b2009-02-05 22:36:04 +0000855 if (version_id >= 3)
856 n->status = qemu_get_be16(f);
857
aliguori002437c2009-02-05 22:36:20 +0000858 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600859 if (version_id < 8) {
860 n->promisc = qemu_get_be32(f);
861 n->allmulti = qemu_get_be32(f);
862 } else {
863 n->promisc = qemu_get_byte(f);
864 n->allmulti = qemu_get_byte(f);
865 }
aliguori002437c2009-02-05 22:36:20 +0000866 }
867
aliguorib6503ed2009-02-05 22:36:28 +0000868 if (version_id >= 5) {
869 n->mac_table.in_use = qemu_get_be32(f);
870 /* MAC_TABLE_ENTRIES may be different from the saved image */
871 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
872 qemu_get_buffer(f, n->mac_table.macs,
873 n->mac_table.in_use * ETH_ALEN);
874 } else if (n->mac_table.in_use) {
875 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600876 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000877 n->mac_table.in_use = 0;
878 }
879 }
880
aliguorif21c0ed2009-02-05 22:36:32 +0000881 if (version_id >= 6)
882 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
883
Mark McLoughlin3a330132009-10-22 17:43:45 +0100884 if (version_id >= 7) {
885 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100886 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100887 return -1;
888 }
889
890 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000891 tap_using_vnet_hdr(n->nic->nc.peer, 1);
892 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200893 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
894 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
895 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
896 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
897 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100898 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600899 }
900
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600901 if (version_id >= 9) {
902 n->mac_table.multi_overflow = qemu_get_byte(f);
903 n->mac_table.uni_overflow = qemu_get_byte(f);
904 }
905
Alex Williamson015cb162009-06-05 14:47:18 -0600906 if (version_id >= 10) {
907 n->alluni = qemu_get_byte(f);
908 n->nomulti = qemu_get_byte(f);
909 n->nouni = qemu_get_byte(f);
910 n->nobcast = qemu_get_byte(f);
911 }
912
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100913 if (version_id >= 11) {
914 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100915 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100916 return -1;
917 }
918 }
919
Alex Williamson2d9aba32009-06-05 14:47:13 -0600920 /* Find the first multicast entry in the saved MAC filter */
921 for (i = 0; i < n->mac_table.in_use; i++) {
922 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
923 break;
924 }
925 }
926 n->mac_table.first_multi = i;
927
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600928 if (n->tx_waiting) {
Alex Williamsona697a332010-09-02 09:01:10 -0600929 if (n->tx_timer) {
930 qemu_mod_timer(n->tx_timer,
931 qemu_get_clock(vm_clock) + n->tx_timeout);
932 } else {
933 qemu_bh_schedule(n->tx_bh);
934 }
aliguorifbe78f42008-12-17 19:13:11 +0000935 }
aliguorifbe78f42008-12-17 19:13:11 +0000936 return 0;
937}
938
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000939static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000940{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000941 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000942
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000943 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000944}
945
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000946static NetClientInfo net_virtio_info = {
947 .type = NET_CLIENT_TYPE_NIC,
948 .size = sizeof(NICState),
949 .can_receive = virtio_net_can_receive,
950 .receive = virtio_net_receive,
951 .cleanup = virtio_net_cleanup,
952 .link_status_changed = virtio_net_set_link_status,
953};
954
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200955static void virtio_net_vmstate_change(void *opaque, int running, int reason)
956{
957 VirtIONet *n = opaque;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200958 n->vm_running = running;
Michael S. Tsirkin7f974482010-06-02 11:40:54 +0300959 /* This is called when vm is started/stopped,
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200960 * it will start/stop vhost backend if appropriate
Michael S. Tsirkin7f974482010-06-02 11:40:54 +0300961 * e.g. after migration. */
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200962 virtio_net_set_status(&n->vdev, n->vdev.status);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200963}
964
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600965VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
966 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +0000967{
968 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +0000969
Paul Brook53c25ce2009-05-18 14:51:59 +0100970 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
971 sizeof(struct virtio_net_config),
972 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000973
aliguori0f03eca2009-02-05 22:36:08 +0000974 n->vdev.get_config = virtio_net_get_config;
975 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000976 n->vdev.get_features = virtio_net_get_features;
977 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000978 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000979 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200980 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +0000981 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -0600982
983 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
984 fprintf(stderr, "virtio-net: "
985 "Unknown option tx=%s, valid options: \"timer\" \"bh\"\n",
986 net->tx);
987 fprintf(stderr, "Defaulting to \"bh\"\n");
988 }
989
990 if (net->tx && !strcmp(net->tx, "timer")) {
991 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
992 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
993 n->tx_timeout = net->txtimer;
994 } else {
995 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
996 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
997 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600998 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200999 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001000 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001001 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001002
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001003 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1004
1005 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001006
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001007 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001008 n->tx_burst = net->txburst;
aliguorifbe78f42008-12-17 19:13:11 +00001009 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +00001010 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001011
aliguorib6503ed2009-02-05 22:36:28 +00001012 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001013
aliguorif21c0ed2009-02-05 22:36:32 +00001014 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001015
Alex Williamson01657c82010-06-25 11:09:28 -06001016 n->qdev = dev;
1017 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001018 virtio_net_save, virtio_net_load, n);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001019 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001020
Paul Brook53c25ce2009-05-18 14:51:59 +01001021 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001022}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001023
1024void virtio_net_exit(VirtIODevice *vdev)
1025{
1026 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001027 qemu_del_vm_change_state_handler(n->vmstate);
1028
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001029 /* This will stop vhost backend if appropriate. */
1030 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001031
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001032 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001033
Alex Williamson01657c82010-06-25 11:09:28 -06001034 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001035
1036 qemu_free(n->mac_table.macs);
1037 qemu_free(n->vlans);
1038
Alex Williamsona697a332010-09-02 09:01:10 -06001039 if (n->tx_timer) {
1040 qemu_del_timer(n->tx_timer);
1041 qemu_free_timer(n->tx_timer);
1042 } else {
1043 qemu_bh_delete(n->tx_bh);
1044 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001045
1046 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001047 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001048}