blob: 58819612bec0e376f205799c8f3ac3b431cde49c [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. 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) &&
105 (n->status & VIRTIO_NET_S_LINK_UP) && n->vm_running;
106}
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 }
113 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
114 return;
115 }
116
117 if (!tap_get_vhost_net(n->nic->nc.peer)) {
118 return;
119 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200120 if (!!n->vhost_started == virtio_net_started(n, status)) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200121 return;
122 }
123 if (!n->vhost_started) {
124 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
125 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000126 error_report("unable to start vhost net: %d: "
127 "falling back on userspace virtio", -r);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200128 } else {
129 n->vhost_started = 1;
130 }
131 } else {
132 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
133 n->vhost_started = 0;
134 }
135}
136
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200137static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
138{
139 VirtIONet *n = to_virtio_net(vdev);
140
141 virtio_net_vhost_status(n, status);
142
143 if (!n->tx_waiting) {
144 return;
145 }
146
147 if (virtio_net_started(n, status) && !n->vhost_started) {
148 if (n->tx_timer) {
149 qemu_mod_timer(n->tx_timer,
150 qemu_get_clock(vm_clock) + n->tx_timeout);
151 } else {
152 qemu_bh_schedule(n->tx_bh);
153 }
154 } else {
155 if (n->tx_timer) {
156 qemu_del_timer(n->tx_timer);
157 } else {
158 qemu_bh_cancel(n->tx_bh);
159 }
160 }
161}
162
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000163static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000164{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000165 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000166 uint16_t old_status = n->status;
167
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000168 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000169 n->status &= ~VIRTIO_NET_S_LINK_UP;
170 else
171 n->status |= VIRTIO_NET_S_LINK_UP;
172
173 if (n->status != old_status)
174 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200175
176 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000177}
178
aliguori002437c2009-02-05 22:36:20 +0000179static void virtio_net_reset(VirtIODevice *vdev)
180{
181 VirtIONet *n = to_virtio_net(vdev);
182
183 /* Reset back to compatibility mode */
184 n->promisc = 1;
185 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600186 n->alluni = 0;
187 n->nomulti = 0;
188 n->nouni = 0;
189 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000190
aliguorif21c0ed2009-02-05 22:36:32 +0000191 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000192 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600193 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600194 n->mac_table.multi_overflow = 0;
195 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000196 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000197 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000198}
199
Mark McLoughlin3a330132009-10-22 17:43:45 +0100200static int peer_has_vnet_hdr(VirtIONet *n)
201{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000202 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100203 return 0;
204
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000205 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100206 return 0;
207
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000208 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100209
210 return n->has_vnet_hdr;
211}
212
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100213static int peer_has_ufo(VirtIONet *n)
214{
215 if (!peer_has_vnet_hdr(n))
216 return 0;
217
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000218 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100219
220 return n->has_ufo;
221}
222
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200223static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000224{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100225 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000226
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200227 features |= (1 << VIRTIO_NET_F_MAC);
228
Mark McLoughlin3a330132009-10-22 17:43:45 +0100229 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000230 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200231 } else {
232 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
233 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
234 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
235 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100236
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200237 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
238 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
239 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
240 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
241 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100242
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200243 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
244 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
245 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100246 }
247
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200248 if (!n->nic->nc.peer ||
249 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
250 return features;
251 }
252 if (!tap_get_vhost_net(n->nic->nc.peer)) {
253 return features;
254 }
255 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000256}
257
aliguori8eca6b12009-04-05 17:40:08 +0000258static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
259{
260 uint32_t features = 0;
261
262 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
263 * but also these: */
264 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500265 features |= (1 << VIRTIO_NET_F_CSUM);
266 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
267 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
268 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000269
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200270 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000271}
272
aliguorifbe78f42008-12-17 19:13:11 +0000273static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
274{
275 VirtIONet *n = to_virtio_net(vdev);
276
277 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100278
279 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000280 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100281 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
282 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
283 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100284 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
285 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100286 }
David L Stevensdc14a392010-03-31 21:20:31 +0300287 if (!n->nic->nc.peer ||
288 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
289 return;
290 }
291 if (!tap_get_vhost_net(n->nic->nc.peer)) {
292 return;
293 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300294 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000295}
296
aliguori002437c2009-02-05 22:36:20 +0000297static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
298 VirtQueueElement *elem)
299{
300 uint8_t on;
301
302 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000303 error_report("virtio-net ctrl invalid rx mode command");
aliguori002437c2009-02-05 22:36:20 +0000304 exit(1);
305 }
306
307 on = ldub_p(elem->out_sg[1].iov_base);
308
309 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
310 n->promisc = on;
311 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
312 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600313 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
314 n->alluni = on;
315 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
316 n->nomulti = on;
317 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
318 n->nouni = on;
319 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
320 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000321 else
322 return VIRTIO_NET_ERR;
323
324 return VIRTIO_NET_OK;
325}
326
aliguorib6503ed2009-02-05 22:36:28 +0000327static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
328 VirtQueueElement *elem)
329{
330 struct virtio_net_ctrl_mac mac_data;
331
332 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
333 elem->out_sg[1].iov_len < sizeof(mac_data) ||
334 elem->out_sg[2].iov_len < sizeof(mac_data))
335 return VIRTIO_NET_ERR;
336
337 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600338 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600339 n->mac_table.uni_overflow = 0;
340 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000341 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
342
343 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
344
345 if (sizeof(mac_data.entries) +
346 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
347 return VIRTIO_NET_ERR;
348
349 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
350 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
351 mac_data.entries * ETH_ALEN);
352 n->mac_table.in_use += mac_data.entries;
353 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600354 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000355 }
356
Alex Williamson2d9aba32009-06-05 14:47:13 -0600357 n->mac_table.first_multi = n->mac_table.in_use;
358
aliguorib6503ed2009-02-05 22:36:28 +0000359 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
360
361 if (sizeof(mac_data.entries) +
362 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
363 return VIRTIO_NET_ERR;
364
365 if (mac_data.entries) {
366 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
367 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
368 elem->out_sg[2].iov_base + sizeof(mac_data),
369 mac_data.entries * ETH_ALEN);
370 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600371 } else {
372 n->mac_table.multi_overflow = 1;
373 }
aliguorib6503ed2009-02-05 22:36:28 +0000374 }
375
376 return VIRTIO_NET_OK;
377}
378
aliguorif21c0ed2009-02-05 22:36:32 +0000379static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
380 VirtQueueElement *elem)
381{
382 uint16_t vid;
383
384 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000385 error_report("virtio-net ctrl invalid vlan command");
aliguorif21c0ed2009-02-05 22:36:32 +0000386 return VIRTIO_NET_ERR;
387 }
388
389 vid = lduw_le_p(elem->out_sg[1].iov_base);
390
391 if (vid >= MAX_VLAN)
392 return VIRTIO_NET_ERR;
393
394 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
395 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
396 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
397 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
398 else
399 return VIRTIO_NET_ERR;
400
401 return VIRTIO_NET_OK;
402}
403
aliguori3d11d362009-02-05 22:36:16 +0000404static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
405{
aliguori002437c2009-02-05 22:36:20 +0000406 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000407 struct virtio_net_ctrl_hdr ctrl;
408 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
409 VirtQueueElement elem;
410
411 while (virtqueue_pop(vq, &elem)) {
412 if ((elem.in_num < 1) || (elem.out_num < 1)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000413 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000414 exit(1);
415 }
416
417 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000418 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000419 error_report("virtio-net ctrl header not in correct element");
aliguori3d11d362009-02-05 22:36:16 +0000420 exit(1);
421 }
422
423 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
424 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
425
aliguori002437c2009-02-05 22:36:20 +0000426 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
427 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000428 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
429 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000430 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
431 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000432
aliguori3d11d362009-02-05 22:36:16 +0000433 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
434
435 virtqueue_push(vq, &elem, sizeof(status));
436 virtio_notify(vdev, vq);
437 }
438}
439
aliguorifbe78f42008-12-17 19:13:11 +0000440/* RX */
441
442static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
443{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100444 VirtIONet *n = to_virtio_net(vdev);
445
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000446 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400447
448 /* We now have RX buffers, signal to the IO thread to break out of the
449 * select to re-poll the tap file descriptor */
450 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000451}
452
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000453static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000454{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000455 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200456 if (!n->vm_running) {
457 return 0;
458 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000459
aliguorifbe78f42008-12-17 19:13:11 +0000460 if (!virtio_queue_ready(n->rx_vq) ||
461 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
462 return 0;
463
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000464 return 1;
465}
466
467static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
468{
aliguorifbe78f42008-12-17 19:13:11 +0000469 if (virtio_queue_empty(n->rx_vq) ||
470 (n->mergeable_rx_bufs &&
471 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
472 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600473
474 /* To avoid a race condition where the guest has made some buffers
475 * available after the above check but before notification was
476 * enabled, check for available buffers again.
477 */
478 if (virtio_queue_empty(n->rx_vq) ||
479 (n->mergeable_rx_bufs &&
480 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
481 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000482 }
483
484 virtio_queue_set_notification(n->rx_vq, 0);
485 return 1;
486}
487
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100488/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
489 * it never finds out that the packets don't have valid checksums. This
490 * causes dhclient to get upset. Fedora's carried a patch for ages to
491 * fix this with Xen but it hasn't appeared in an upstream release of
492 * dhclient yet.
493 *
494 * To avoid breaking existing guests, we catch udp packets and add
495 * checksums. This is terrible but it's better than hacking the guest
496 * kernels.
497 *
498 * N.B. if we introduce a zero-copy API, this operation is no longer free so
499 * we should provide a mechanism to disable it to avoid polluting the host
500 * cache.
501 */
502static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
503 const uint8_t *buf, size_t size)
504{
505 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
506 (size > 27 && size < 1500) && /* normal sized MTU */
507 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
508 (buf[23] == 17) && /* ip.protocol == UDP */
509 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
510 /* FIXME this cast is evil */
511 net_checksum_calculate((uint8_t *)buf, size);
512 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
513 }
514}
515
aliguorifbe78f42008-12-17 19:13:11 +0000516static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000517 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000518{
blueswir13f4cb3d2009-04-13 16:31:01 +0000519 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000520 int offset = 0;
521
522 hdr->flags = 0;
523 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
524
Mark McLoughlin3a330132009-10-22 17:43:45 +0100525 if (n->has_vnet_hdr) {
526 memcpy(hdr, buf, sizeof(*hdr));
527 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100528 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100529 }
530
aliguorifbe78f42008-12-17 19:13:11 +0000531 /* We only ever receive a struct virtio_net_hdr from the tapfd,
532 * but we may be passing along a larger header to the guest.
533 */
534 iov[0].iov_base += hdr_len;
535 iov[0].iov_len -= hdr_len;
536
537 return offset;
538}
539
aliguori3831ab22009-02-05 22:36:24 +0000540static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
541{
542 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000543 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000544 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000545 int i;
aliguori3831ab22009-02-05 22:36:24 +0000546
547 if (n->promisc)
548 return 1;
549
Mark McLoughlin3a330132009-10-22 17:43:45 +0100550 if (n->has_vnet_hdr) {
551 ptr += sizeof(struct virtio_net_hdr);
552 }
553
aliguorif21c0ed2009-02-05 22:36:32 +0000554 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
555 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
556 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
557 return 0;
558 }
559
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600560 if (ptr[0] & 1) { // multicast
561 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600562 return !n->nobcast;
563 } else if (n->nomulti) {
564 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600565 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600566 return 1;
567 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600568
569 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
570 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
571 return 1;
572 }
573 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600574 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600575 if (n->nouni) {
576 return 0;
577 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600578 return 1;
579 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600580 return 1;
581 }
aliguori3831ab22009-02-05 22:36:24 +0000582
Alex Williamson2d9aba32009-06-05 14:47:13 -0600583 for (i = 0; i < n->mac_table.first_multi; i++) {
584 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
585 return 1;
586 }
587 }
aliguorib6503ed2009-02-05 22:36:28 +0000588 }
589
aliguori3831ab22009-02-05 22:36:24 +0000590 return 0;
591}
592
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000593static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000594{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000595 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000596 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300597 size_t guest_hdr_len, offset, i, host_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000598
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000599 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000600 return -1;
601
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300602 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300603 guest_hdr_len = n->mergeable_rx_bufs ?
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300604 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
605
606
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300607 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
608 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100609 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000610
aliguori3831ab22009-02-05 22:36:24 +0000611 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100612 return size;
aliguori3831ab22009-02-05 22:36:24 +0000613
aliguorifbe78f42008-12-17 19:13:11 +0000614 offset = i = 0;
615
616 while (offset < size) {
617 VirtQueueElement elem;
618 int len, total;
619 struct iovec sg[VIRTQUEUE_MAX_SIZE];
620
Amit Shah22c253d2010-01-13 16:24:43 +0530621 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000622
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300623 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000624 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100625 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000626 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300627 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000628 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300629 i, n->mergeable_rx_bufs, offset, size,
630 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000631 exit(1);
632 }
633
634 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000635 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000636 exit(1);
637 }
638
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300639 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000640 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000641 exit(1);
642 }
643
644 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
645
646 if (i == 0) {
647 if (n->mergeable_rx_bufs)
648 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
649
650 offset += receive_header(n, sg, elem.in_num,
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300651 buf + offset, size - offset, guest_hdr_len);
652 total += guest_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000653 }
654
655 /* copy in packet. ugh */
Amit Shahe4d56392010-04-27 18:04:05 +0530656 len = iov_from_buf(sg, elem.in_num,
657 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000658 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300659 offset += len;
660 /* If buffers can't be merged, at this point we
661 * must have consumed the complete packet.
662 * Otherwise, drop it. */
663 if (!n->mergeable_rx_bufs && offset < size) {
664#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000665 error_report("virtio-net truncated non-mergeable packet: "
666 "i %zd mergeable %d offset %zd, size %zd, "
667 "guest hdr len %zd, host hdr len %zd",
668 i, n->mergeable_rx_bufs,
669 offset, size, guest_hdr_len, host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300670#endif
671 return size;
672 }
aliguorifbe78f42008-12-17 19:13:11 +0000673
674 /* signal other side */
675 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000676 }
677
678 if (mhdr)
679 mhdr->num_buffers = i;
680
681 virtqueue_flush(n->rx_vq, i);
682 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100683
684 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000685}
686
Alex Williamsone3f30482010-09-02 09:00:57 -0600687static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100688
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000689static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100690{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000691 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100692
693 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
694 virtio_notify(&n->vdev, n->tx_vq);
695
696 n->async_tx.elem.out_num = n->async_tx.len = 0;
697
698 virtio_queue_set_notification(n->tx_vq, 1);
699 virtio_net_flush_tx(n, n->tx_vq);
700}
701
aliguorifbe78f42008-12-17 19:13:11 +0000702/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600703static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000704{
705 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600706 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600707 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
708 return num_packets;
709 }
aliguorifbe78f42008-12-17 19:13:11 +0000710
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200711 assert(n->vm_running);
712
Mark McLoughlin62433752009-06-18 18:21:36 +0100713 if (n->async_tx.elem.out_num) {
714 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600715 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100716 }
717
aliguorifbe78f42008-12-17 19:13:11 +0000718 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100719 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000720 unsigned int out_num = elem.out_num;
721 struct iovec *out_sg = &elem.out_sg[0];
722 unsigned hdr_len;
723
724 /* hdr_len refers to the header received from the guest */
725 hdr_len = n->mergeable_rx_bufs ?
726 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
727 sizeof(struct virtio_net_hdr);
728
729 if (out_num < 1 || out_sg->iov_len != hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000730 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000731 exit(1);
732 }
733
734 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100735 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000736 out_num--;
737 out_sg++;
738 len += hdr_len;
739 } else if (n->mergeable_rx_bufs) {
740 /* tapfd expects a struct virtio_net_hdr */
741 hdr_len -= sizeof(struct virtio_net_hdr);
742 out_sg->iov_len -= hdr_len;
743 len += hdr_len;
744 }
745
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000746 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100747 virtio_net_tx_complete);
748 if (ret == 0) {
749 virtio_queue_set_notification(n->tx_vq, 0);
750 n->async_tx.elem = elem;
751 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600752 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100753 }
754
755 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000756
757 virtqueue_push(vq, &elem, len);
758 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600759
760 if (++num_packets >= n->tx_burst) {
761 break;
762 }
aliguorifbe78f42008-12-17 19:13:11 +0000763 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600764 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000765}
766
Alex Williamsona697a332010-09-02 09:01:10 -0600767static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000768{
769 VirtIONet *n = to_virtio_net(vdev);
770
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200771 /* This happens when device was stopped but VCPU wasn't. */
772 if (!n->vm_running) {
773 n->tx_waiting = 1;
774 return;
775 }
776
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600777 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000778 virtio_queue_set_notification(vq, 1);
779 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600780 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000781 virtio_net_flush_tx(n, vq);
782 } else {
783 qemu_mod_timer(n->tx_timer,
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600784 qemu_get_clock(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600785 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000786 virtio_queue_set_notification(vq, 0);
787 }
788}
789
Alex Williamsona697a332010-09-02 09:01:10 -0600790static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
791{
792 VirtIONet *n = to_virtio_net(vdev);
793
794 if (unlikely(n->tx_waiting)) {
795 return;
796 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200797 n->tx_waiting = 1;
798 /* This happens when device was stopped but VCPU wasn't. */
799 if (!n->vm_running) {
800 return;
801 }
Alex Williamsona697a332010-09-02 09:01:10 -0600802 virtio_queue_set_notification(vq, 0);
803 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600804}
805
aliguorifbe78f42008-12-17 19:13:11 +0000806static void virtio_net_tx_timer(void *opaque)
807{
808 VirtIONet *n = opaque;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200809 assert(n->vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000810
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600811 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000812
813 /* Just in case the driver is not ready on more */
814 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
815 return;
816
817 virtio_queue_set_notification(n->tx_vq, 1);
818 virtio_net_flush_tx(n, n->tx_vq);
819}
820
Alex Williamsona697a332010-09-02 09:01:10 -0600821static void virtio_net_tx_bh(void *opaque)
822{
823 VirtIONet *n = opaque;
824 int32_t ret;
825
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200826 assert(n->vm_running);
827
Alex Williamsona697a332010-09-02 09:01:10 -0600828 n->tx_waiting = 0;
829
830 /* Just in case the driver is not ready on more */
831 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
832 return;
833
834 ret = virtio_net_flush_tx(n, n->tx_vq);
835 if (ret == -EBUSY) {
836 return; /* Notification re-enable handled by tx_complete */
837 }
838
839 /* If we flush a full burst of packets, assume there are
840 * more coming and immediately reschedule */
841 if (ret >= n->tx_burst) {
842 qemu_bh_schedule(n->tx_bh);
843 n->tx_waiting = 1;
844 return;
845 }
846
847 /* If less than a full burst, re-enable notification and flush
848 * anything that may have come in while we weren't looking. If
849 * we find something, assume the guest is still active and reschedule */
850 virtio_queue_set_notification(n->tx_vq, 1);
851 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
852 virtio_queue_set_notification(n->tx_vq, 0);
853 qemu_bh_schedule(n->tx_bh);
854 n->tx_waiting = 1;
855 }
856}
857
aliguorifbe78f42008-12-17 19:13:11 +0000858static void virtio_net_save(QEMUFile *f, void *opaque)
859{
860 VirtIONet *n = opaque;
861
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200862 /* At this point, backend must be stopped, otherwise
863 * it might keep writing to memory. */
864 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000865 virtio_save(&n->vdev, f);
866
aliguori79674062009-02-05 22:36:12 +0000867 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600868 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000869 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000870 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600871 qemu_put_byte(f, n->promisc);
872 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000873 qemu_put_be32(f, n->mac_table.in_use);
874 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000875 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100876 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600877 qemu_put_byte(f, n->mac_table.multi_overflow);
878 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600879 qemu_put_byte(f, n->alluni);
880 qemu_put_byte(f, n->nomulti);
881 qemu_put_byte(f, n->nouni);
882 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100883 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000884}
885
886static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
887{
888 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600889 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000890
aliguori9d6271b2009-02-05 22:36:04 +0000891 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000892 return -EINVAL;
893
894 virtio_load(&n->vdev, f);
895
aliguori79674062009-02-05 22:36:12 +0000896 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600897 n->tx_waiting = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000898 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000899
aliguori9d6271b2009-02-05 22:36:04 +0000900 if (version_id >= 3)
901 n->status = qemu_get_be16(f);
902
aliguori002437c2009-02-05 22:36:20 +0000903 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600904 if (version_id < 8) {
905 n->promisc = qemu_get_be32(f);
906 n->allmulti = qemu_get_be32(f);
907 } else {
908 n->promisc = qemu_get_byte(f);
909 n->allmulti = qemu_get_byte(f);
910 }
aliguori002437c2009-02-05 22:36:20 +0000911 }
912
aliguorib6503ed2009-02-05 22:36:28 +0000913 if (version_id >= 5) {
914 n->mac_table.in_use = qemu_get_be32(f);
915 /* MAC_TABLE_ENTRIES may be different from the saved image */
916 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
917 qemu_get_buffer(f, n->mac_table.macs,
918 n->mac_table.in_use * ETH_ALEN);
919 } else if (n->mac_table.in_use) {
920 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600921 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000922 n->mac_table.in_use = 0;
923 }
924 }
925
aliguorif21c0ed2009-02-05 22:36:32 +0000926 if (version_id >= 6)
927 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
928
Mark McLoughlin3a330132009-10-22 17:43:45 +0100929 if (version_id >= 7) {
930 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100931 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100932 return -1;
933 }
934
935 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000936 tap_using_vnet_hdr(n->nic->nc.peer, 1);
937 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200938 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
939 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
940 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
941 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
942 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100943 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600944 }
945
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600946 if (version_id >= 9) {
947 n->mac_table.multi_overflow = qemu_get_byte(f);
948 n->mac_table.uni_overflow = qemu_get_byte(f);
949 }
950
Alex Williamson015cb162009-06-05 14:47:18 -0600951 if (version_id >= 10) {
952 n->alluni = qemu_get_byte(f);
953 n->nomulti = qemu_get_byte(f);
954 n->nouni = qemu_get_byte(f);
955 n->nobcast = qemu_get_byte(f);
956 }
957
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100958 if (version_id >= 11) {
959 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100960 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100961 return -1;
962 }
963 }
964
Alex Williamson2d9aba32009-06-05 14:47:13 -0600965 /* Find the first multicast entry in the saved MAC filter */
966 for (i = 0; i < n->mac_table.in_use; i++) {
967 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
968 break;
969 }
970 }
971 n->mac_table.first_multi = i;
aliguorifbe78f42008-12-17 19:13:11 +0000972 return 0;
973}
974
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000975static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000976{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000977 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000978
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000979 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000980}
981
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000982static NetClientInfo net_virtio_info = {
983 .type = NET_CLIENT_TYPE_NIC,
984 .size = sizeof(NICState),
985 .can_receive = virtio_net_can_receive,
986 .receive = virtio_net_receive,
987 .cleanup = virtio_net_cleanup,
988 .link_status_changed = virtio_net_set_link_status,
989};
990
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200991static void virtio_net_vmstate_change(void *opaque, int running, int reason)
992{
993 VirtIONet *n = opaque;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200994 n->vm_running = running;
Michael S. Tsirkin7f974482010-06-02 11:40:54 +0300995 /* This is called when vm is started/stopped,
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200996 * it will start/stop vhost backend if appropriate
Michael S. Tsirkin7f974482010-06-02 11:40:54 +0300997 * e.g. after migration. */
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200998 virtio_net_set_status(&n->vdev, n->vdev.status);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200999}
1000
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001001VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1002 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001003{
1004 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001005
Paul Brook53c25ce2009-05-18 14:51:59 +01001006 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1007 sizeof(struct virtio_net_config),
1008 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001009
aliguori0f03eca2009-02-05 22:36:08 +00001010 n->vdev.get_config = virtio_net_get_config;
1011 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001012 n->vdev.get_features = virtio_net_get_features;
1013 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001014 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001015 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001016 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +00001017 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001018
1019 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001020 error_report("virtio-net: "
1021 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1022 net->tx);
1023 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001024 }
1025
1026 if (net->tx && !strcmp(net->tx, "timer")) {
1027 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1028 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
1029 n->tx_timeout = net->txtimer;
1030 } else {
1031 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1032 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1033 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001034 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001035 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001036 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001037 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001038
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001039 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1040
1041 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001042
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001043 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001044 n->tx_burst = net->txburst;
aliguorifbe78f42008-12-17 19:13:11 +00001045 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +00001046 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001047
aliguorib6503ed2009-02-05 22:36:28 +00001048 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001049
aliguorif21c0ed2009-02-05 22:36:32 +00001050 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001051
Alex Williamson01657c82010-06-25 11:09:28 -06001052 n->qdev = dev;
1053 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001054 virtio_net_save, virtio_net_load, n);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001055 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001056
Paul Brook53c25ce2009-05-18 14:51:59 +01001057 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001058}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001059
1060void virtio_net_exit(VirtIODevice *vdev)
1061{
1062 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001063 qemu_del_vm_change_state_handler(n->vmstate);
1064
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001065 /* This will stop vhost backend if appropriate. */
1066 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001067
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001068 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001069
Alex Williamson01657c82010-06-25 11:09:28 -06001070 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001071
1072 qemu_free(n->mac_table.macs);
1073 qemu_free(n->vlans);
1074
Alex Williamsona697a332010-09-02 09:01:10 -06001075 if (n->tx_timer) {
1076 qemu_del_timer(n->tx_timer);
1077 qemu_free_timer(n->tx_timer);
1078 } else {
1079 qemu_bh_delete(n->tx_bh);
1080 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001081
1082 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001083 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001084}