blob: 161f11445f25dd8462df8019a1ec8002d358ae37 [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;
aliguorib6503ed2009-02-05 22:36:28 +000057 struct {
58 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060059 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060060 uint8_t multi_overflow;
61 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000062 uint8_t *macs;
63 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000064 uint32_t *vlans;
Alex Williamson01657c82010-06-25 11:09:28 -060065 DeviceState *qdev;
aliguorifbe78f42008-12-17 19:13:11 +000066} VirtIONet;
67
68/* TODO
69 * - we could suppress RX interrupt if we were so inclined.
70 */
71
72static VirtIONet *to_virtio_net(VirtIODevice *vdev)
73{
74 return (VirtIONet *)vdev;
75}
76
aliguori0f03eca2009-02-05 22:36:08 +000077static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000078{
79 VirtIONet *n = to_virtio_net(vdev);
80 struct virtio_net_config netcfg;
81
Aurelien Jarno44b15bc2011-01-25 11:55:14 +010082 netcfg.status = lduw_p(&n->status);
aliguori79674062009-02-05 22:36:12 +000083 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000084 memcpy(config, &netcfg, sizeof(netcfg));
85}
86
aliguori0f03eca2009-02-05 22:36:08 +000087static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
88{
89 VirtIONet *n = to_virtio_net(vdev);
90 struct virtio_net_config netcfg;
91
92 memcpy(&netcfg, config, sizeof(netcfg));
93
aliguori79674062009-02-05 22:36:12 +000094 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
95 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000096 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000097 }
98}
99
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200100static bool virtio_net_started(VirtIONet *n, uint8_t status)
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200101{
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200102 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200103 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200104}
105
106static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
107{
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200108 if (!n->nic->nc.peer) {
109 return;
110 }
111 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
112 return;
113 }
114
115 if (!tap_get_vhost_net(n->nic->nc.peer)) {
116 return;
117 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200118 if (!!n->vhost_started == virtio_net_started(n, status)) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200119 return;
120 }
121 if (!n->vhost_started) {
122 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
123 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000124 error_report("unable to start vhost net: %d: "
125 "falling back on userspace virtio", -r);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200126 } else {
127 n->vhost_started = 1;
128 }
129 } else {
130 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
131 n->vhost_started = 0;
132 }
133}
134
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200135static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
136{
137 VirtIONet *n = to_virtio_net(vdev);
138
139 virtio_net_vhost_status(n, status);
140
141 if (!n->tx_waiting) {
142 return;
143 }
144
145 if (virtio_net_started(n, status) && !n->vhost_started) {
146 if (n->tx_timer) {
147 qemu_mod_timer(n->tx_timer,
148 qemu_get_clock(vm_clock) + n->tx_timeout);
149 } else {
150 qemu_bh_schedule(n->tx_bh);
151 }
152 } else {
153 if (n->tx_timer) {
154 qemu_del_timer(n->tx_timer);
155 } else {
156 qemu_bh_cancel(n->tx_bh);
157 }
158 }
159}
160
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000161static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000162{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000163 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000164 uint16_t old_status = n->status;
165
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000166 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000167 n->status &= ~VIRTIO_NET_S_LINK_UP;
168 else
169 n->status |= VIRTIO_NET_S_LINK_UP;
170
171 if (n->status != old_status)
172 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200173
174 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000175}
176
aliguori002437c2009-02-05 22:36:20 +0000177static void virtio_net_reset(VirtIODevice *vdev)
178{
179 VirtIONet *n = to_virtio_net(vdev);
180
181 /* Reset back to compatibility mode */
182 n->promisc = 1;
183 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600184 n->alluni = 0;
185 n->nomulti = 0;
186 n->nouni = 0;
187 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000188
aliguorif21c0ed2009-02-05 22:36:32 +0000189 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000190 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600191 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600192 n->mac_table.multi_overflow = 0;
193 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000194 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000195 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000196}
197
Mark McLoughlin3a330132009-10-22 17:43:45 +0100198static int peer_has_vnet_hdr(VirtIONet *n)
199{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000200 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100201 return 0;
202
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000203 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100204 return 0;
205
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000206 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100207
208 return n->has_vnet_hdr;
209}
210
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100211static int peer_has_ufo(VirtIONet *n)
212{
213 if (!peer_has_vnet_hdr(n))
214 return 0;
215
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000216 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100217
218 return n->has_ufo;
219}
220
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200221static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000222{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100223 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000224
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200225 features |= (1 << VIRTIO_NET_F_MAC);
226
Mark McLoughlin3a330132009-10-22 17:43:45 +0100227 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000228 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200229 } else {
230 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
231 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
232 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
233 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100234
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200235 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
236 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
237 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
238 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
239 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100240
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200241 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
242 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
243 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100244 }
245
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200246 if (!n->nic->nc.peer ||
247 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
248 return features;
249 }
250 if (!tap_get_vhost_net(n->nic->nc.peer)) {
251 return features;
252 }
253 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000254}
255
aliguori8eca6b12009-04-05 17:40:08 +0000256static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
257{
258 uint32_t features = 0;
259
260 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
261 * but also these: */
262 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500263 features |= (1 << VIRTIO_NET_F_CSUM);
264 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
265 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
266 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000267
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200268 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000269}
270
aliguorifbe78f42008-12-17 19:13:11 +0000271static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
272{
273 VirtIONet *n = to_virtio_net(vdev);
274
275 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100276
277 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000278 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100279 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
280 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
281 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100282 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
283 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100284 }
David L Stevensdc14a392010-03-31 21:20:31 +0300285 if (!n->nic->nc.peer ||
286 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
287 return;
288 }
289 if (!tap_get_vhost_net(n->nic->nc.peer)) {
290 return;
291 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300292 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000293}
294
aliguori002437c2009-02-05 22:36:20 +0000295static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
296 VirtQueueElement *elem)
297{
298 uint8_t on;
299
300 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000301 error_report("virtio-net ctrl invalid rx mode command");
aliguori002437c2009-02-05 22:36:20 +0000302 exit(1);
303 }
304
305 on = ldub_p(elem->out_sg[1].iov_base);
306
307 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
308 n->promisc = on;
309 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
310 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600311 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
312 n->alluni = on;
313 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
314 n->nomulti = on;
315 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
316 n->nouni = on;
317 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
318 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000319 else
320 return VIRTIO_NET_ERR;
321
322 return VIRTIO_NET_OK;
323}
324
aliguorib6503ed2009-02-05 22:36:28 +0000325static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
326 VirtQueueElement *elem)
327{
328 struct virtio_net_ctrl_mac mac_data;
329
330 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
331 elem->out_sg[1].iov_len < sizeof(mac_data) ||
332 elem->out_sg[2].iov_len < sizeof(mac_data))
333 return VIRTIO_NET_ERR;
334
335 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600336 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600337 n->mac_table.uni_overflow = 0;
338 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000339 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
340
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100341 mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000342
343 if (sizeof(mac_data.entries) +
344 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
345 return VIRTIO_NET_ERR;
346
347 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
348 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
349 mac_data.entries * ETH_ALEN);
350 n->mac_table.in_use += mac_data.entries;
351 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600352 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000353 }
354
Alex Williamson2d9aba32009-06-05 14:47:13 -0600355 n->mac_table.first_multi = n->mac_table.in_use;
356
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100357 mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000358
359 if (sizeof(mac_data.entries) +
360 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
361 return VIRTIO_NET_ERR;
362
363 if (mac_data.entries) {
364 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
365 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
366 elem->out_sg[2].iov_base + sizeof(mac_data),
367 mac_data.entries * ETH_ALEN);
368 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600369 } else {
370 n->mac_table.multi_overflow = 1;
371 }
aliguorib6503ed2009-02-05 22:36:28 +0000372 }
373
374 return VIRTIO_NET_OK;
375}
376
aliguorif21c0ed2009-02-05 22:36:32 +0000377static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
378 VirtQueueElement *elem)
379{
380 uint16_t vid;
381
382 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000383 error_report("virtio-net ctrl invalid vlan command");
aliguorif21c0ed2009-02-05 22:36:32 +0000384 return VIRTIO_NET_ERR;
385 }
386
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100387 vid = lduw_p(elem->out_sg[1].iov_base);
aliguorif21c0ed2009-02-05 22:36:32 +0000388
389 if (vid >= MAX_VLAN)
390 return VIRTIO_NET_ERR;
391
392 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
393 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
394 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
395 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
396 else
397 return VIRTIO_NET_ERR;
398
399 return VIRTIO_NET_OK;
400}
401
aliguori3d11d362009-02-05 22:36:16 +0000402static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
403{
aliguori002437c2009-02-05 22:36:20 +0000404 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000405 struct virtio_net_ctrl_hdr ctrl;
406 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
407 VirtQueueElement elem;
408
409 while (virtqueue_pop(vq, &elem)) {
410 if ((elem.in_num < 1) || (elem.out_num < 1)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000411 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000412 exit(1);
413 }
414
415 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000416 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000417 error_report("virtio-net ctrl header not in correct element");
aliguori3d11d362009-02-05 22:36:16 +0000418 exit(1);
419 }
420
421 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
422 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
423
aliguori002437c2009-02-05 22:36:20 +0000424 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
425 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000426 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
427 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000428 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
429 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000430
aliguori3d11d362009-02-05 22:36:16 +0000431 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
432
433 virtqueue_push(vq, &elem, sizeof(status));
434 virtio_notify(vdev, vq);
435 }
436}
437
aliguorifbe78f42008-12-17 19:13:11 +0000438/* RX */
439
440static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
441{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100442 VirtIONet *n = to_virtio_net(vdev);
443
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000444 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400445
446 /* We now have RX buffers, signal to the IO thread to break out of the
447 * select to re-poll the tap file descriptor */
448 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000449}
450
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000451static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000452{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000453 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200454 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200455 return 0;
456 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000457
aliguorifbe78f42008-12-17 19:13:11 +0000458 if (!virtio_queue_ready(n->rx_vq) ||
459 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
460 return 0;
461
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000462 return 1;
463}
464
465static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
466{
aliguorifbe78f42008-12-17 19:13:11 +0000467 if (virtio_queue_empty(n->rx_vq) ||
468 (n->mergeable_rx_bufs &&
469 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
470 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600471
472 /* To avoid a race condition where the guest has made some buffers
473 * available after the above check but before notification was
474 * enabled, check for available buffers again.
475 */
476 if (virtio_queue_empty(n->rx_vq) ||
477 (n->mergeable_rx_bufs &&
478 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
479 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000480 }
481
482 virtio_queue_set_notification(n->rx_vq, 0);
483 return 1;
484}
485
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100486/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
487 * it never finds out that the packets don't have valid checksums. This
488 * causes dhclient to get upset. Fedora's carried a patch for ages to
489 * fix this with Xen but it hasn't appeared in an upstream release of
490 * dhclient yet.
491 *
492 * To avoid breaking existing guests, we catch udp packets and add
493 * checksums. This is terrible but it's better than hacking the guest
494 * kernels.
495 *
496 * N.B. if we introduce a zero-copy API, this operation is no longer free so
497 * we should provide a mechanism to disable it to avoid polluting the host
498 * cache.
499 */
500static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
501 const uint8_t *buf, size_t size)
502{
503 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
504 (size > 27 && size < 1500) && /* normal sized MTU */
505 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
506 (buf[23] == 17) && /* ip.protocol == UDP */
507 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
508 /* FIXME this cast is evil */
509 net_checksum_calculate((uint8_t *)buf, size);
510 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
511 }
512}
513
aliguorifbe78f42008-12-17 19:13:11 +0000514static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000515 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000516{
blueswir13f4cb3d2009-04-13 16:31:01 +0000517 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000518 int offset = 0;
519
520 hdr->flags = 0;
521 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
522
Mark McLoughlin3a330132009-10-22 17:43:45 +0100523 if (n->has_vnet_hdr) {
524 memcpy(hdr, buf, sizeof(*hdr));
525 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100526 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100527 }
528
aliguorifbe78f42008-12-17 19:13:11 +0000529 /* We only ever receive a struct virtio_net_hdr from the tapfd,
530 * but we may be passing along a larger header to the guest.
531 */
532 iov[0].iov_base += hdr_len;
533 iov[0].iov_len -= hdr_len;
534
535 return offset;
536}
537
aliguori3831ab22009-02-05 22:36:24 +0000538static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
539{
540 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000541 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000542 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000543 int i;
aliguori3831ab22009-02-05 22:36:24 +0000544
545 if (n->promisc)
546 return 1;
547
Mark McLoughlin3a330132009-10-22 17:43:45 +0100548 if (n->has_vnet_hdr) {
549 ptr += sizeof(struct virtio_net_hdr);
550 }
551
aliguorif21c0ed2009-02-05 22:36:32 +0000552 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
553 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
554 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
555 return 0;
556 }
557
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600558 if (ptr[0] & 1) { // multicast
559 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600560 return !n->nobcast;
561 } else if (n->nomulti) {
562 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600563 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600564 return 1;
565 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600566
567 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
568 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
569 return 1;
570 }
571 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600572 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600573 if (n->nouni) {
574 return 0;
575 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600576 return 1;
577 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600578 return 1;
579 }
aliguori3831ab22009-02-05 22:36:24 +0000580
Alex Williamson2d9aba32009-06-05 14:47:13 -0600581 for (i = 0; i < n->mac_table.first_multi; i++) {
582 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
583 return 1;
584 }
585 }
aliguorib6503ed2009-02-05 22:36:28 +0000586 }
587
aliguori3831ab22009-02-05 22:36:24 +0000588 return 0;
589}
590
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000591static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000592{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000593 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000594 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300595 size_t guest_hdr_len, offset, i, host_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000596
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000597 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000598 return -1;
599
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300600 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300601 guest_hdr_len = n->mergeable_rx_bufs ?
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300602 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
603
604
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300605 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
606 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100607 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000608
aliguori3831ab22009-02-05 22:36:24 +0000609 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100610 return size;
aliguori3831ab22009-02-05 22:36:24 +0000611
aliguorifbe78f42008-12-17 19:13:11 +0000612 offset = i = 0;
613
614 while (offset < size) {
615 VirtQueueElement elem;
616 int len, total;
617 struct iovec sg[VIRTQUEUE_MAX_SIZE];
618
Amit Shah22c253d2010-01-13 16:24:43 +0530619 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000620
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300621 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000622 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100623 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000624 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300625 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000626 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300627 i, n->mergeable_rx_bufs, offset, size,
628 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000629 exit(1);
630 }
631
632 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000633 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000634 exit(1);
635 }
636
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300637 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000638 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000639 exit(1);
640 }
641
642 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
643
644 if (i == 0) {
645 if (n->mergeable_rx_bufs)
646 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
647
648 offset += receive_header(n, sg, elem.in_num,
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300649 buf + offset, size - offset, guest_hdr_len);
650 total += guest_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000651 }
652
653 /* copy in packet. ugh */
Amit Shahe4d56392010-04-27 18:04:05 +0530654 len = iov_from_buf(sg, elem.in_num,
655 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000656 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300657 offset += len;
658 /* If buffers can't be merged, at this point we
659 * must have consumed the complete packet.
660 * Otherwise, drop it. */
661 if (!n->mergeable_rx_bufs && offset < size) {
662#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000663 error_report("virtio-net truncated non-mergeable packet: "
664 "i %zd mergeable %d offset %zd, size %zd, "
665 "guest hdr len %zd, host hdr len %zd",
666 i, n->mergeable_rx_bufs,
667 offset, size, guest_hdr_len, host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300668#endif
669 return size;
670 }
aliguorifbe78f42008-12-17 19:13:11 +0000671
672 /* signal other side */
673 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000674 }
675
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100676 if (mhdr) {
677 mhdr->num_buffers = lduw_p(&i);
678 }
aliguorifbe78f42008-12-17 19:13:11 +0000679
680 virtqueue_flush(n->rx_vq, i);
681 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100682
683 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000684}
685
Alex Williamsone3f30482010-09-02 09:00:57 -0600686static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100687
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000688static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100689{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000690 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100691
692 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
693 virtio_notify(&n->vdev, n->tx_vq);
694
695 n->async_tx.elem.out_num = n->async_tx.len = 0;
696
697 virtio_queue_set_notification(n->tx_vq, 1);
698 virtio_net_flush_tx(n, n->tx_vq);
699}
700
aliguorifbe78f42008-12-17 19:13:11 +0000701/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600702static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000703{
704 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600705 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600706 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
707 return num_packets;
708 }
aliguorifbe78f42008-12-17 19:13:11 +0000709
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200710 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200711
Mark McLoughlin62433752009-06-18 18:21:36 +0100712 if (n->async_tx.elem.out_num) {
713 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600714 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100715 }
716
aliguorifbe78f42008-12-17 19:13:11 +0000717 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100718 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000719 unsigned int out_num = elem.out_num;
720 struct iovec *out_sg = &elem.out_sg[0];
721 unsigned hdr_len;
722
723 /* hdr_len refers to the header received from the guest */
724 hdr_len = n->mergeable_rx_bufs ?
725 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
726 sizeof(struct virtio_net_hdr);
727
728 if (out_num < 1 || out_sg->iov_len != hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000729 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000730 exit(1);
731 }
732
733 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100734 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000735 out_num--;
736 out_sg++;
737 len += hdr_len;
738 } else if (n->mergeable_rx_bufs) {
739 /* tapfd expects a struct virtio_net_hdr */
740 hdr_len -= sizeof(struct virtio_net_hdr);
741 out_sg->iov_len -= hdr_len;
742 len += hdr_len;
743 }
744
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000745 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100746 virtio_net_tx_complete);
747 if (ret == 0) {
748 virtio_queue_set_notification(n->tx_vq, 0);
749 n->async_tx.elem = elem;
750 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600751 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100752 }
753
754 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000755
756 virtqueue_push(vq, &elem, len);
757 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600758
759 if (++num_packets >= n->tx_burst) {
760 break;
761 }
aliguorifbe78f42008-12-17 19:13:11 +0000762 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600763 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000764}
765
Alex Williamsona697a332010-09-02 09:01:10 -0600766static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000767{
768 VirtIONet *n = to_virtio_net(vdev);
769
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200770 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200771 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200772 n->tx_waiting = 1;
773 return;
774 }
775
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600776 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000777 virtio_queue_set_notification(vq, 1);
778 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600779 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000780 virtio_net_flush_tx(n, vq);
781 } else {
782 qemu_mod_timer(n->tx_timer,
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600783 qemu_get_clock(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600784 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000785 virtio_queue_set_notification(vq, 0);
786 }
787}
788
Alex Williamsona697a332010-09-02 09:01:10 -0600789static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
790{
791 VirtIONet *n = to_virtio_net(vdev);
792
793 if (unlikely(n->tx_waiting)) {
794 return;
795 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200796 n->tx_waiting = 1;
797 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200798 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200799 return;
800 }
Alex Williamsona697a332010-09-02 09:01:10 -0600801 virtio_queue_set_notification(vq, 0);
802 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600803}
804
aliguorifbe78f42008-12-17 19:13:11 +0000805static void virtio_net_tx_timer(void *opaque)
806{
807 VirtIONet *n = opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200808 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000809
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600810 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000811
812 /* Just in case the driver is not ready on more */
813 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
814 return;
815
816 virtio_queue_set_notification(n->tx_vq, 1);
817 virtio_net_flush_tx(n, n->tx_vq);
818}
819
Alex Williamsona697a332010-09-02 09:01:10 -0600820static void virtio_net_tx_bh(void *opaque)
821{
822 VirtIONet *n = opaque;
823 int32_t ret;
824
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200825 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200826
Alex Williamsona697a332010-09-02 09:01:10 -0600827 n->tx_waiting = 0;
828
829 /* Just in case the driver is not ready on more */
830 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
831 return;
832
833 ret = virtio_net_flush_tx(n, n->tx_vq);
834 if (ret == -EBUSY) {
835 return; /* Notification re-enable handled by tx_complete */
836 }
837
838 /* If we flush a full burst of packets, assume there are
839 * more coming and immediately reschedule */
840 if (ret >= n->tx_burst) {
841 qemu_bh_schedule(n->tx_bh);
842 n->tx_waiting = 1;
843 return;
844 }
845
846 /* If less than a full burst, re-enable notification and flush
847 * anything that may have come in while we weren't looking. If
848 * we find something, assume the guest is still active and reschedule */
849 virtio_queue_set_notification(n->tx_vq, 1);
850 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
851 virtio_queue_set_notification(n->tx_vq, 0);
852 qemu_bh_schedule(n->tx_bh);
853 n->tx_waiting = 1;
854 }
855}
856
aliguorifbe78f42008-12-17 19:13:11 +0000857static void virtio_net_save(QEMUFile *f, void *opaque)
858{
859 VirtIONet *n = opaque;
860
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200861 /* At this point, backend must be stopped, otherwise
862 * it might keep writing to memory. */
863 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000864 virtio_save(&n->vdev, f);
865
aliguori79674062009-02-05 22:36:12 +0000866 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600867 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000868 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000869 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600870 qemu_put_byte(f, n->promisc);
871 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000872 qemu_put_be32(f, n->mac_table.in_use);
873 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000874 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100875 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600876 qemu_put_byte(f, n->mac_table.multi_overflow);
877 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600878 qemu_put_byte(f, n->alluni);
879 qemu_put_byte(f, n->nomulti);
880 qemu_put_byte(f, n->nouni);
881 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100882 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000883}
884
885static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
886{
887 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600888 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000889
aliguori9d6271b2009-02-05 22:36:04 +0000890 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000891 return -EINVAL;
892
893 virtio_load(&n->vdev, f);
894
aliguori79674062009-02-05 22:36:12 +0000895 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600896 n->tx_waiting = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000897 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000898
aliguori9d6271b2009-02-05 22:36:04 +0000899 if (version_id >= 3)
900 n->status = qemu_get_be16(f);
901
aliguori002437c2009-02-05 22:36:20 +0000902 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600903 if (version_id < 8) {
904 n->promisc = qemu_get_be32(f);
905 n->allmulti = qemu_get_be32(f);
906 } else {
907 n->promisc = qemu_get_byte(f);
908 n->allmulti = qemu_get_byte(f);
909 }
aliguori002437c2009-02-05 22:36:20 +0000910 }
911
aliguorib6503ed2009-02-05 22:36:28 +0000912 if (version_id >= 5) {
913 n->mac_table.in_use = qemu_get_be32(f);
914 /* MAC_TABLE_ENTRIES may be different from the saved image */
915 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
916 qemu_get_buffer(f, n->mac_table.macs,
917 n->mac_table.in_use * ETH_ALEN);
918 } else if (n->mac_table.in_use) {
919 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600920 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000921 n->mac_table.in_use = 0;
922 }
923 }
924
aliguorif21c0ed2009-02-05 22:36:32 +0000925 if (version_id >= 6)
926 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
927
Mark McLoughlin3a330132009-10-22 17:43:45 +0100928 if (version_id >= 7) {
929 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100930 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100931 return -1;
932 }
933
934 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000935 tap_using_vnet_hdr(n->nic->nc.peer, 1);
936 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200937 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
938 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
939 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
940 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
941 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100942 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600943 }
944
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600945 if (version_id >= 9) {
946 n->mac_table.multi_overflow = qemu_get_byte(f);
947 n->mac_table.uni_overflow = qemu_get_byte(f);
948 }
949
Alex Williamson015cb162009-06-05 14:47:18 -0600950 if (version_id >= 10) {
951 n->alluni = qemu_get_byte(f);
952 n->nomulti = qemu_get_byte(f);
953 n->nouni = qemu_get_byte(f);
954 n->nobcast = qemu_get_byte(f);
955 }
956
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100957 if (version_id >= 11) {
958 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100959 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100960 return -1;
961 }
962 }
963
Alex Williamson2d9aba32009-06-05 14:47:13 -0600964 /* Find the first multicast entry in the saved MAC filter */
965 for (i = 0; i < n->mac_table.in_use; i++) {
966 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
967 break;
968 }
969 }
970 n->mac_table.first_multi = i;
aliguorifbe78f42008-12-17 19:13:11 +0000971 return 0;
972}
973
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000974static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000975{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000976 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000977
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000978 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000979}
980
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000981static NetClientInfo net_virtio_info = {
982 .type = NET_CLIENT_TYPE_NIC,
983 .size = sizeof(NICState),
984 .can_receive = virtio_net_can_receive,
985 .receive = virtio_net_receive,
986 .cleanup = virtio_net_cleanup,
987 .link_status_changed = virtio_net_set_link_status,
988};
989
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600990VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
991 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +0000992{
993 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +0000994
Paul Brook53c25ce2009-05-18 14:51:59 +0100995 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
996 sizeof(struct virtio_net_config),
997 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000998
aliguori0f03eca2009-02-05 22:36:08 +0000999 n->vdev.get_config = virtio_net_get_config;
1000 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001001 n->vdev.get_features = virtio_net_get_features;
1002 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001003 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001004 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001005 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +00001006 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001007
1008 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001009 error_report("virtio-net: "
1010 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1011 net->tx);
1012 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001013 }
1014
1015 if (net->tx && !strcmp(net->tx, "timer")) {
1016 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1017 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
1018 n->tx_timeout = net->txtimer;
1019 } else {
1020 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1021 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1022 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001023 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001024 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001025 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001026 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001027
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001028 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1029
1030 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001031
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001032 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001033 n->tx_burst = net->txburst;
aliguorifbe78f42008-12-17 19:13:11 +00001034 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +00001035 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001036
aliguorib6503ed2009-02-05 22:36:28 +00001037 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001038
aliguorif21c0ed2009-02-05 22:36:32 +00001039 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001040
Alex Williamson01657c82010-06-25 11:09:28 -06001041 n->qdev = dev;
1042 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001043 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001044
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001045 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1046
Paul Brook53c25ce2009-05-18 14:51:59 +01001047 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001048}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001049
1050void virtio_net_exit(VirtIODevice *vdev)
1051{
1052 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001053
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001054 /* This will stop vhost backend if appropriate. */
1055 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001056
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001057 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001058
Alex Williamson01657c82010-06-25 11:09:28 -06001059 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001060
1061 qemu_free(n->mac_table.macs);
1062 qemu_free(n->vlans);
1063
Alex Williamsona697a332010-09-02 09:01:10 -06001064 if (n->tx_timer) {
1065 qemu_del_timer(n->tx_timer);
1066 qemu_free_timer(n->tx_timer);
1067 } else {
1068 qemu_bh_delete(n->tx_bh);
1069 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001070
1071 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001072 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001073}