blob: acb3cec7cbbde944f13a89fbf95455b4c42ef53e [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
14#include "virtio.h"
15#include "net.h"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010016#include "net/checksum.h"
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010017#include "net/tap.h"
Markus Armbruster2f792012010-02-18 16:24:31 +010018#include "qemu-error.h"
aliguorifbe78f42008-12-17 19:13:11 +000019#include "qemu-timer.h"
20#include "virtio-net.h"
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020021#include "vhost_net.h"
aliguorifbe78f42008-12-17 19:13:11 +000022
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010023#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000024
Alex Williamson4ffb17f2009-06-05 14:47:23 -060025#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000026#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000027
aliguorifbe78f42008-12-17 19:13:11 +000028typedef struct VirtIONet
29{
30 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000031 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000032 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000033 VirtQueue *rx_vq;
34 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000035 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000036 NICState *nic;
aliguorifbe78f42008-12-17 19:13:11 +000037 QEMUTimer *tx_timer;
38 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010039 uint32_t has_vnet_hdr;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010040 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010041 struct {
42 VirtQueueElement elem;
43 ssize_t len;
44 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000045 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060046 uint8_t promisc;
47 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060048 uint8_t alluni;
49 uint8_t nomulti;
50 uint8_t nouni;
51 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020052 uint8_t vhost_started;
53 VMChangeStateEntry *vmstate;
aliguorib6503ed2009-02-05 22:36:28 +000054 struct {
55 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060056 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060057 uint8_t multi_overflow;
58 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000059 uint8_t *macs;
60 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000061 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000062} VirtIONet;
63
64/* TODO
65 * - we could suppress RX interrupt if we were so inclined.
66 */
67
68static VirtIONet *to_virtio_net(VirtIODevice *vdev)
69{
70 return (VirtIONet *)vdev;
71}
72
aliguori0f03eca2009-02-05 22:36:08 +000073static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000074{
75 VirtIONet *n = to_virtio_net(vdev);
76 struct virtio_net_config netcfg;
77
aliguori554c97d2009-01-08 19:46:33 +000078 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000079 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000080 memcpy(config, &netcfg, sizeof(netcfg));
81}
82
aliguori0f03eca2009-02-05 22:36:08 +000083static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
84{
85 VirtIONet *n = to_virtio_net(vdev);
86 struct virtio_net_config netcfg;
87
88 memcpy(&netcfg, config, sizeof(netcfg));
89
aliguori79674062009-02-05 22:36:12 +000090 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
91 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000092 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000093 }
94}
95
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000096static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +000097{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000098 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +000099 uint16_t old_status = n->status;
100
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000101 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000102 n->status &= ~VIRTIO_NET_S_LINK_UP;
103 else
104 n->status |= VIRTIO_NET_S_LINK_UP;
105
106 if (n->status != old_status)
107 virtio_notify_config(&n->vdev);
108}
109
aliguori002437c2009-02-05 22:36:20 +0000110static void virtio_net_reset(VirtIODevice *vdev)
111{
112 VirtIONet *n = to_virtio_net(vdev);
113
114 /* Reset back to compatibility mode */
115 n->promisc = 1;
116 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600117 n->alluni = 0;
118 n->nomulti = 0;
119 n->nouni = 0;
120 n->nobcast = 0;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200121 if (n->vhost_started) {
122 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
123 n->vhost_started = 0;
124 }
aliguorib6503ed2009-02-05 22:36:28 +0000125
aliguorif21c0ed2009-02-05 22:36:32 +0000126 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000127 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600128 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600129 n->mac_table.multi_overflow = 0;
130 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000131 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000132 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000133}
134
Mark McLoughlin3a330132009-10-22 17:43:45 +0100135static int peer_has_vnet_hdr(VirtIONet *n)
136{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000137 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100138 return 0;
139
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000140 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100141 return 0;
142
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000143 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100144
145 return n->has_vnet_hdr;
146}
147
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100148static int peer_has_ufo(VirtIONet *n)
149{
150 if (!peer_has_vnet_hdr(n))
151 return 0;
152
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000153 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100154
155 return n->has_ufo;
156}
157
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200158static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000159{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100160 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000161
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200162 features |= (1 << VIRTIO_NET_F_MAC);
163
Mark McLoughlin3a330132009-10-22 17:43:45 +0100164 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000165 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200166 } else {
167 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
168 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
169 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
170 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100171
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200172 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
173 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
174 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
175 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
176 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100177
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200178 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
179 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
180 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100181 }
182
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200183 if (!n->nic->nc.peer ||
184 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
185 return features;
186 }
187 if (!tap_get_vhost_net(n->nic->nc.peer)) {
188 return features;
189 }
190 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000191}
192
aliguori8eca6b12009-04-05 17:40:08 +0000193static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
194{
195 uint32_t features = 0;
196
197 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
198 * but also these: */
199 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500200 features |= (1 << VIRTIO_NET_F_CSUM);
201 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
202 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
203 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000204
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200205 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000206}
207
aliguorifbe78f42008-12-17 19:13:11 +0000208static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
209{
210 VirtIONet *n = to_virtio_net(vdev);
211
212 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100213
214 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000215 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100216 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
217 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
218 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100219 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
220 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100221 }
David L Stevensdc14a392010-03-31 21:20:31 +0300222 if (!n->nic->nc.peer ||
223 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
224 return;
225 }
226 if (!tap_get_vhost_net(n->nic->nc.peer)) {
227 return;
228 }
229 return vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000230}
231
aliguori002437c2009-02-05 22:36:20 +0000232static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
233 VirtQueueElement *elem)
234{
235 uint8_t on;
236
237 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
238 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
239 exit(1);
240 }
241
242 on = ldub_p(elem->out_sg[1].iov_base);
243
244 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
245 n->promisc = on;
246 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
247 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600248 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
249 n->alluni = on;
250 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
251 n->nomulti = on;
252 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
253 n->nouni = on;
254 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
255 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000256 else
257 return VIRTIO_NET_ERR;
258
259 return VIRTIO_NET_OK;
260}
261
aliguorib6503ed2009-02-05 22:36:28 +0000262static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
263 VirtQueueElement *elem)
264{
265 struct virtio_net_ctrl_mac mac_data;
266
267 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
268 elem->out_sg[1].iov_len < sizeof(mac_data) ||
269 elem->out_sg[2].iov_len < sizeof(mac_data))
270 return VIRTIO_NET_ERR;
271
272 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600273 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600274 n->mac_table.uni_overflow = 0;
275 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000276 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
277
278 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
279
280 if (sizeof(mac_data.entries) +
281 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
282 return VIRTIO_NET_ERR;
283
284 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
285 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
286 mac_data.entries * ETH_ALEN);
287 n->mac_table.in_use += mac_data.entries;
288 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600289 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000290 }
291
Alex Williamson2d9aba32009-06-05 14:47:13 -0600292 n->mac_table.first_multi = n->mac_table.in_use;
293
aliguorib6503ed2009-02-05 22:36:28 +0000294 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
295
296 if (sizeof(mac_data.entries) +
297 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
298 return VIRTIO_NET_ERR;
299
300 if (mac_data.entries) {
301 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
302 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
303 elem->out_sg[2].iov_base + sizeof(mac_data),
304 mac_data.entries * ETH_ALEN);
305 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600306 } else {
307 n->mac_table.multi_overflow = 1;
308 }
aliguorib6503ed2009-02-05 22:36:28 +0000309 }
310
311 return VIRTIO_NET_OK;
312}
313
aliguorif21c0ed2009-02-05 22:36:32 +0000314static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
315 VirtQueueElement *elem)
316{
317 uint16_t vid;
318
319 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
320 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
321 return VIRTIO_NET_ERR;
322 }
323
324 vid = lduw_le_p(elem->out_sg[1].iov_base);
325
326 if (vid >= MAX_VLAN)
327 return VIRTIO_NET_ERR;
328
329 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
330 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
331 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
332 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
333 else
334 return VIRTIO_NET_ERR;
335
336 return VIRTIO_NET_OK;
337}
338
aliguori3d11d362009-02-05 22:36:16 +0000339static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
340{
aliguori002437c2009-02-05 22:36:20 +0000341 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000342 struct virtio_net_ctrl_hdr ctrl;
343 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
344 VirtQueueElement elem;
345
346 while (virtqueue_pop(vq, &elem)) {
347 if ((elem.in_num < 1) || (elem.out_num < 1)) {
348 fprintf(stderr, "virtio-net ctrl missing headers\n");
349 exit(1);
350 }
351
352 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000353 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000354 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
355 exit(1);
356 }
357
358 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
359 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
360
aliguori002437c2009-02-05 22:36:20 +0000361 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
362 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000363 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
364 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000365 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
366 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000367
aliguori3d11d362009-02-05 22:36:16 +0000368 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
369
370 virtqueue_push(vq, &elem, sizeof(status));
371 virtio_notify(vdev, vq);
372 }
373}
374
aliguorifbe78f42008-12-17 19:13:11 +0000375/* RX */
376
377static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
378{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100379 VirtIONet *n = to_virtio_net(vdev);
380
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000381 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400382
383 /* We now have RX buffers, signal to the IO thread to break out of the
384 * select to re-poll the tap file descriptor */
385 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000386}
387
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000388static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000389{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000390 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000391
aliguorifbe78f42008-12-17 19:13:11 +0000392 if (!virtio_queue_ready(n->rx_vq) ||
393 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
394 return 0;
395
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000396 return 1;
397}
398
399static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
400{
aliguorifbe78f42008-12-17 19:13:11 +0000401 if (virtio_queue_empty(n->rx_vq) ||
402 (n->mergeable_rx_bufs &&
403 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
404 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600405
406 /* To avoid a race condition where the guest has made some buffers
407 * available after the above check but before notification was
408 * enabled, check for available buffers again.
409 */
410 if (virtio_queue_empty(n->rx_vq) ||
411 (n->mergeable_rx_bufs &&
412 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
413 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000414 }
415
416 virtio_queue_set_notification(n->rx_vq, 0);
417 return 1;
418}
419
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100420/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
421 * it never finds out that the packets don't have valid checksums. This
422 * causes dhclient to get upset. Fedora's carried a patch for ages to
423 * fix this with Xen but it hasn't appeared in an upstream release of
424 * dhclient yet.
425 *
426 * To avoid breaking existing guests, we catch udp packets and add
427 * checksums. This is terrible but it's better than hacking the guest
428 * kernels.
429 *
430 * N.B. if we introduce a zero-copy API, this operation is no longer free so
431 * we should provide a mechanism to disable it to avoid polluting the host
432 * cache.
433 */
434static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
435 const uint8_t *buf, size_t size)
436{
437 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
438 (size > 27 && size < 1500) && /* normal sized MTU */
439 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
440 (buf[23] == 17) && /* ip.protocol == UDP */
441 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
442 /* FIXME this cast is evil */
443 net_checksum_calculate((uint8_t *)buf, size);
444 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
445 }
446}
447
aliguorifbe78f42008-12-17 19:13:11 +0000448static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
449{
450 int offset, i;
451
452 offset = i = 0;
453 while (offset < count && i < iovcnt) {
454 int len = MIN(iov[i].iov_len, count - offset);
455 memcpy(iov[i].iov_base, buf + offset, len);
456 offset += len;
457 i++;
458 }
459
460 return offset;
461}
462
463static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000464 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000465{
blueswir13f4cb3d2009-04-13 16:31:01 +0000466 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000467 int offset = 0;
468
469 hdr->flags = 0;
470 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
471
Mark McLoughlin3a330132009-10-22 17:43:45 +0100472 if (n->has_vnet_hdr) {
473 memcpy(hdr, buf, sizeof(*hdr));
474 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100475 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100476 }
477
aliguorifbe78f42008-12-17 19:13:11 +0000478 /* We only ever receive a struct virtio_net_hdr from the tapfd,
479 * but we may be passing along a larger header to the guest.
480 */
481 iov[0].iov_base += hdr_len;
482 iov[0].iov_len -= hdr_len;
483
484 return offset;
485}
486
aliguori3831ab22009-02-05 22:36:24 +0000487static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
488{
489 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000490 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000491 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000492 int i;
aliguori3831ab22009-02-05 22:36:24 +0000493
494 if (n->promisc)
495 return 1;
496
Mark McLoughlin3a330132009-10-22 17:43:45 +0100497 if (n->has_vnet_hdr) {
498 ptr += sizeof(struct virtio_net_hdr);
499 }
500
aliguorif21c0ed2009-02-05 22:36:32 +0000501 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
502 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
503 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
504 return 0;
505 }
506
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600507 if (ptr[0] & 1) { // multicast
508 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600509 return !n->nobcast;
510 } else if (n->nomulti) {
511 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600512 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600513 return 1;
514 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600515
516 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
517 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
518 return 1;
519 }
520 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600521 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600522 if (n->nouni) {
523 return 0;
524 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600525 return 1;
526 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600527 return 1;
528 }
aliguori3831ab22009-02-05 22:36:24 +0000529
Alex Williamson2d9aba32009-06-05 14:47:13 -0600530 for (i = 0; i < n->mac_table.first_multi; i++) {
531 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
532 return 1;
533 }
534 }
aliguorib6503ed2009-02-05 22:36:28 +0000535 }
536
aliguori3831ab22009-02-05 22:36:24 +0000537 return 0;
538}
539
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000540static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000541{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000542 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000543 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000544 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000545
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000546 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000547 return -1;
548
549 if (!virtio_net_has_buffers(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100550 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000551
aliguori3831ab22009-02-05 22:36:24 +0000552 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100553 return size;
aliguori3831ab22009-02-05 22:36:24 +0000554
aliguorifbe78f42008-12-17 19:13:11 +0000555 /* hdr_len refers to the header we supply to the guest */
556 hdr_len = n->mergeable_rx_bufs ?
557 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
558
559 offset = i = 0;
560
561 while (offset < size) {
562 VirtQueueElement elem;
563 int len, total;
564 struct iovec sg[VIRTQUEUE_MAX_SIZE];
565
Amit Shah22c253d2010-01-13 16:24:43 +0530566 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000567
568 if ((i != 0 && !n->mergeable_rx_bufs) ||
569 virtqueue_pop(n->rx_vq, &elem) == 0) {
570 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100571 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000572 fprintf(stderr, "virtio-net truncating packet\n");
573 exit(1);
574 }
575
576 if (elem.in_num < 1) {
577 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
578 exit(1);
579 }
580
581 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
582 fprintf(stderr, "virtio-net header not in first element\n");
583 exit(1);
584 }
585
586 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
587
588 if (i == 0) {
589 if (n->mergeable_rx_bufs)
590 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
591
592 offset += receive_header(n, sg, elem.in_num,
593 buf + offset, size - offset, hdr_len);
594 total += hdr_len;
595 }
596
597 /* copy in packet. ugh */
598 len = iov_fill(sg, elem.in_num,
599 buf + offset, size - offset);
600 total += len;
601
602 /* signal other side */
603 virtqueue_fill(n->rx_vq, &elem, total, i++);
604
605 offset += len;
606 }
607
608 if (mhdr)
609 mhdr->num_buffers = i;
610
611 virtqueue_flush(n->rx_vq, i);
612 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100613
614 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000615}
616
Mark McLoughlin62433752009-06-18 18:21:36 +0100617static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
618
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000619static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100620{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000621 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100622
623 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
624 virtio_notify(&n->vdev, n->tx_vq);
625
626 n->async_tx.elem.out_num = n->async_tx.len = 0;
627
628 virtio_queue_set_notification(n->tx_vq, 1);
629 virtio_net_flush_tx(n, n->tx_vq);
630}
631
aliguorifbe78f42008-12-17 19:13:11 +0000632/* TX */
633static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
634{
635 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000636
637 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
638 return;
639
Mark McLoughlin62433752009-06-18 18:21:36 +0100640 if (n->async_tx.elem.out_num) {
641 virtio_queue_set_notification(n->tx_vq, 0);
642 return;
643 }
644
aliguorifbe78f42008-12-17 19:13:11 +0000645 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100646 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000647 unsigned int out_num = elem.out_num;
648 struct iovec *out_sg = &elem.out_sg[0];
649 unsigned hdr_len;
650
651 /* hdr_len refers to the header received from the guest */
652 hdr_len = n->mergeable_rx_bufs ?
653 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
654 sizeof(struct virtio_net_hdr);
655
656 if (out_num < 1 || out_sg->iov_len != hdr_len) {
657 fprintf(stderr, "virtio-net header not in first element\n");
658 exit(1);
659 }
660
661 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100662 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000663 out_num--;
664 out_sg++;
665 len += hdr_len;
666 } else if (n->mergeable_rx_bufs) {
667 /* tapfd expects a struct virtio_net_hdr */
668 hdr_len -= sizeof(struct virtio_net_hdr);
669 out_sg->iov_len -= hdr_len;
670 len += hdr_len;
671 }
672
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000673 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100674 virtio_net_tx_complete);
675 if (ret == 0) {
676 virtio_queue_set_notification(n->tx_vq, 0);
677 n->async_tx.elem = elem;
678 n->async_tx.len = len;
679 return;
680 }
681
682 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000683
684 virtqueue_push(vq, &elem, len);
685 virtio_notify(&n->vdev, vq);
686 }
687}
688
689static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
690{
691 VirtIONet *n = to_virtio_net(vdev);
692
693 if (n->tx_timer_active) {
694 virtio_queue_set_notification(vq, 1);
695 qemu_del_timer(n->tx_timer);
696 n->tx_timer_active = 0;
697 virtio_net_flush_tx(n, vq);
698 } else {
699 qemu_mod_timer(n->tx_timer,
700 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
701 n->tx_timer_active = 1;
702 virtio_queue_set_notification(vq, 0);
703 }
704}
705
706static void virtio_net_tx_timer(void *opaque)
707{
708 VirtIONet *n = opaque;
709
710 n->tx_timer_active = 0;
711
712 /* Just in case the driver is not ready on more */
713 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
714 return;
715
716 virtio_queue_set_notification(n->tx_vq, 1);
717 virtio_net_flush_tx(n, n->tx_vq);
718}
719
720static void virtio_net_save(QEMUFile *f, void *opaque)
721{
722 VirtIONet *n = opaque;
723
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200724 if (n->vhost_started) {
725 /* TODO: should we really stop the backend?
726 * If we don't, it might keep writing to memory. */
727 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
728 n->vhost_started = 0;
729 }
aliguorifbe78f42008-12-17 19:13:11 +0000730 virtio_save(&n->vdev, f);
731
aliguori79674062009-02-05 22:36:12 +0000732 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000733 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000734 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000735 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600736 qemu_put_byte(f, n->promisc);
737 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000738 qemu_put_be32(f, n->mac_table.in_use);
739 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000740 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100741 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600742 qemu_put_byte(f, n->mac_table.multi_overflow);
743 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600744 qemu_put_byte(f, n->alluni);
745 qemu_put_byte(f, n->nomulti);
746 qemu_put_byte(f, n->nouni);
747 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100748 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000749}
750
751static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
752{
753 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600754 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000755
aliguori9d6271b2009-02-05 22:36:04 +0000756 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000757 return -EINVAL;
758
759 virtio_load(&n->vdev, f);
760
aliguori79674062009-02-05 22:36:12 +0000761 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000762 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000763 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000764
aliguori9d6271b2009-02-05 22:36:04 +0000765 if (version_id >= 3)
766 n->status = qemu_get_be16(f);
767
aliguori002437c2009-02-05 22:36:20 +0000768 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600769 if (version_id < 8) {
770 n->promisc = qemu_get_be32(f);
771 n->allmulti = qemu_get_be32(f);
772 } else {
773 n->promisc = qemu_get_byte(f);
774 n->allmulti = qemu_get_byte(f);
775 }
aliguori002437c2009-02-05 22:36:20 +0000776 }
777
aliguorib6503ed2009-02-05 22:36:28 +0000778 if (version_id >= 5) {
779 n->mac_table.in_use = qemu_get_be32(f);
780 /* MAC_TABLE_ENTRIES may be different from the saved image */
781 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
782 qemu_get_buffer(f, n->mac_table.macs,
783 n->mac_table.in_use * ETH_ALEN);
784 } else if (n->mac_table.in_use) {
785 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600786 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000787 n->mac_table.in_use = 0;
788 }
789 }
790
aliguorif21c0ed2009-02-05 22:36:32 +0000791 if (version_id >= 6)
792 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
793
Mark McLoughlin3a330132009-10-22 17:43:45 +0100794 if (version_id >= 7) {
795 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100796 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100797 return -1;
798 }
799
800 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000801 tap_using_vnet_hdr(n->nic->nc.peer, 1);
802 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200803 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
804 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
805 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
806 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
807 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100808 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600809 }
810
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600811 if (version_id >= 9) {
812 n->mac_table.multi_overflow = qemu_get_byte(f);
813 n->mac_table.uni_overflow = qemu_get_byte(f);
814 }
815
Alex Williamson015cb162009-06-05 14:47:18 -0600816 if (version_id >= 10) {
817 n->alluni = qemu_get_byte(f);
818 n->nomulti = qemu_get_byte(f);
819 n->nouni = qemu_get_byte(f);
820 n->nobcast = qemu_get_byte(f);
821 }
822
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100823 if (version_id >= 11) {
824 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100825 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100826 return -1;
827 }
828 }
829
Alex Williamson2d9aba32009-06-05 14:47:13 -0600830 /* Find the first multicast entry in the saved MAC filter */
831 for (i = 0; i < n->mac_table.in_use; i++) {
832 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
833 break;
834 }
835 }
836 n->mac_table.first_multi = i;
837
aliguorifbe78f42008-12-17 19:13:11 +0000838 if (n->tx_timer_active) {
839 qemu_mod_timer(n->tx_timer,
840 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
841 }
aliguorifbe78f42008-12-17 19:13:11 +0000842 return 0;
843}
844
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000845static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000846{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000847 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000848
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000849 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000850}
851
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000852static NetClientInfo net_virtio_info = {
853 .type = NET_CLIENT_TYPE_NIC,
854 .size = sizeof(NICState),
855 .can_receive = virtio_net_can_receive,
856 .receive = virtio_net_receive,
857 .cleanup = virtio_net_cleanup,
858 .link_status_changed = virtio_net_set_link_status,
859};
860
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200861static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
862{
863 VirtIONet *n = to_virtio_net(vdev);
864 if (!n->nic->nc.peer) {
865 return;
866 }
867 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
868 return;
869 }
870
871 if (!tap_get_vhost_net(n->nic->nc.peer)) {
872 return;
873 }
874 if (!!n->vhost_started == !!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
875 return;
876 }
877 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
878 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), vdev);
879 if (r < 0) {
880 fprintf(stderr, "unable to start vhost net: %d: "
881 "falling back on userspace virtio\n", -r);
882 } else {
883 n->vhost_started = 1;
884 }
885 } else {
886 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
887 n->vhost_started = 0;
888 }
889}
890
891static void virtio_net_vmstate_change(void *opaque, int running, int reason)
892{
893 VirtIONet *n = opaque;
894 if (!running) {
895 return;
896 }
897 /* This is called when vm is started, it will start vhost backend if
898 * appropriate e.g. after migration. */
899 virtio_net_set_status(&n->vdev, n->vdev.status);
900}
901
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200902VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000903{
904 VirtIONet *n;
905 static int virtio_net_id;
906
Paul Brook53c25ce2009-05-18 14:51:59 +0100907 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
908 sizeof(struct virtio_net_config),
909 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000910
aliguori0f03eca2009-02-05 22:36:08 +0000911 n->vdev.get_config = virtio_net_get_config;
912 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000913 n->vdev.get_features = virtio_net_get_features;
914 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000915 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000916 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200917 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +0000918 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
919 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600920 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200921 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +0000922 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +0000923 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +0000924
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000925 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
926
927 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000928
aliguorifbe78f42008-12-17 19:13:11 +0000929 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
930 n->tx_timer_active = 0;
931 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000932 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000933
aliguorib6503ed2009-02-05 22:36:28 +0000934 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000935
aliguorif21c0ed2009-02-05 22:36:32 +0000936 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000937
aliguori9d6271b2009-02-05 22:36:04 +0000938 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000939 virtio_net_save, virtio_net_load, n);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200940 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100941
Paul Brook53c25ce2009-05-18 14:51:59 +0100942 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100943}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200944
945void virtio_net_exit(VirtIODevice *vdev)
946{
947 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200948 qemu_del_vm_change_state_handler(n->vmstate);
949
950 if (n->vhost_started) {
951 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
952 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200953
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000954 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200955
956 unregister_savevm("virtio-net", n);
957
958 qemu_free(n->mac_table.macs);
959 qemu_free(n->vlans);
960
961 qemu_del_timer(n->tx_timer);
962 qemu_free_timer(n->tx_timer);
963
964 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000965 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200966}