]> git.proxmox.com Git - qemu.git/blame - hw/virtio-net.c
virtio-net: use safe iov operations for rx
[qemu.git] / hw / virtio-net.c
CommitLineData
fbe78f4f
AL
1/*
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
e4d5639d 14#include "iov.h"
fbe78f4f
AL
15#include "virtio.h"
16#include "net.h"
7200ac3c 17#include "net/checksum.h"
a8ed73f7 18#include "net/tap.h"
2f792016 19#include "qemu-error.h"
fbe78f4f
AL
20#include "qemu-timer.h"
21#include "virtio-net.h"
9bc6304c 22#include "vhost_net.h"
fbe78f4f 23
0ce0e8f4 24#define VIRTIO_NET_VM_VERSION 11
b6503ed9 25
4ffb17f5 26#define MAC_TABLE_ENTRIES 64
f21c0ed9 27#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 28
fbe78f4f
AL
29typedef struct VirtIONet
30{
31 VirtIODevice vdev;
79674068 32 uint8_t mac[ETH_ALEN];
554c97dd 33 uint16_t status;
fbe78f4f
AL
34 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
3d11d36c 36 VirtQueue *ctrl_vq;
eb6b6c12 37 NICState *nic;
fbe78f4f 38 QEMUTimer *tx_timer;
a697a334 39 QEMUBH *tx_bh;
f0c07c7c 40 uint32_t tx_timeout;
e3f30488 41 int32_t tx_burst;
4b4b8d36 42 int tx_waiting;
3a330134 43 uint32_t has_vnet_hdr;
e35e23f6
MT
44 size_t host_hdr_len;
45 size_t guest_hdr_len;
0ce0e8f4 46 uint8_t has_ufo;
6243375f
MM
47 struct {
48 VirtQueueElement elem;
49 ssize_t len;
50 } async_tx;
fbe78f4f 51 int mergeable_rx_bufs;
f10c592e
AW
52 uint8_t promisc;
53 uint8_t allmulti;
015cb166
AW
54 uint8_t alluni;
55 uint8_t nomulti;
56 uint8_t nouni;
57 uint8_t nobcast;
9bc6304c 58 uint8_t vhost_started;
b6503ed9
AL
59 struct {
60 int in_use;
2d9aba39 61 int first_multi;
8fd2a2f1
AW
62 uint8_t multi_overflow;
63 uint8_t uni_overflow;
b6503ed9
AL
64 uint8_t *macs;
65 } mac_table;
f21c0ed9 66 uint32_t *vlans;
01657c86 67 DeviceState *qdev;
fbe78f4f
AL
68} 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
0f03eca6 79static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f
AL
80{
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
b46d97f2 84 stw_p(&netcfg.status, n->status);
79674068 85 memcpy(netcfg.mac, n->mac, ETH_ALEN);
fbe78f4f
AL
86 memcpy(config, &netcfg, sizeof(netcfg));
87}
88
0f03eca6
AL
89static 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
79674068
AL
96 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
97 memcpy(n->mac, netcfg.mac, ETH_ALEN);
eb6b6c12 98 qemu_format_nic_info_str(&n->nic->nc, n->mac);
0f03eca6
AL
99 }
100}
101
783e7706
MT
102static bool virtio_net_started(VirtIONet *n, uint8_t status)
103{
104 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
85cf2a8d 105 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
783e7706
MT
106}
107
108static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
afbaa7b4 109{
afbaa7b4
MT
110 if (!n->nic->nc.peer) {
111 return;
112 }
2be64a68 113 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
afbaa7b4
MT
114 return;
115 }
116
117 if (!tap_get_vhost_net(n->nic->nc.peer)) {
118 return;
119 }
32993698
MT
120 if (!!n->vhost_started == virtio_net_started(n, status) &&
121 !n->nic->nc.peer->link_down) {
afbaa7b4
MT
122 return;
123 }
124 if (!n->vhost_started) {
5430a28f 125 int r;
126 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
127 return;
128 }
129 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
afbaa7b4 130 if (r < 0) {
e7b43f7e
SH
131 error_report("unable to start vhost net: %d: "
132 "falling back on userspace virtio", -r);
afbaa7b4
MT
133 } else {
134 n->vhost_started = 1;
135 }
136 } else {
137 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
138 n->vhost_started = 0;
139 }
140}
141
783e7706
MT
142static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
143{
144 VirtIONet *n = to_virtio_net(vdev);
145
146 virtio_net_vhost_status(n, status);
147
148 if (!n->tx_waiting) {
149 return;
150 }
151
152 if (virtio_net_started(n, status) && !n->vhost_started) {
153 if (n->tx_timer) {
154 qemu_mod_timer(n->tx_timer,
74475455 155 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
783e7706
MT
156 } else {
157 qemu_bh_schedule(n->tx_bh);
158 }
159 } else {
160 if (n->tx_timer) {
161 qemu_del_timer(n->tx_timer);
162 } else {
163 qemu_bh_cancel(n->tx_bh);
164 }
165 }
166}
167
4e68f7a0 168static void virtio_net_set_link_status(NetClientState *nc)
554c97dd 169{
eb6b6c12 170 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
554c97dd
AL
171 uint16_t old_status = n->status;
172
eb6b6c12 173 if (nc->link_down)
554c97dd
AL
174 n->status &= ~VIRTIO_NET_S_LINK_UP;
175 else
176 n->status |= VIRTIO_NET_S_LINK_UP;
177
178 if (n->status != old_status)
179 virtio_notify_config(&n->vdev);
afbaa7b4
MT
180
181 virtio_net_set_status(&n->vdev, n->vdev.status);
554c97dd
AL
182}
183
002437cd
AL
184static void virtio_net_reset(VirtIODevice *vdev)
185{
186 VirtIONet *n = to_virtio_net(vdev);
187
188 /* Reset back to compatibility mode */
189 n->promisc = 1;
190 n->allmulti = 0;
015cb166
AW
191 n->alluni = 0;
192 n->nomulti = 0;
193 n->nouni = 0;
194 n->nobcast = 0;
b6503ed9 195
f21c0ed9 196 /* Flush any MAC and VLAN filter table state */
b6503ed9 197 n->mac_table.in_use = 0;
2d9aba39 198 n->mac_table.first_multi = 0;
8fd2a2f1
AW
199 n->mac_table.multi_overflow = 0;
200 n->mac_table.uni_overflow = 0;
b6503ed9 201 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
f21c0ed9 202 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
203}
204
3a330134
MM
205static int peer_has_vnet_hdr(VirtIONet *n)
206{
eb6b6c12 207 if (!n->nic->nc.peer)
3a330134
MM
208 return 0;
209
2be64a68 210 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
3a330134
MM
211 return 0;
212
eb6b6c12 213 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
3a330134
MM
214
215 return n->has_vnet_hdr;
216}
217
0ce0e8f4
MM
218static int peer_has_ufo(VirtIONet *n)
219{
220 if (!peer_has_vnet_hdr(n))
221 return 0;
222
eb6b6c12 223 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
0ce0e8f4
MM
224
225 return n->has_ufo;
226}
227
8172539d 228static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
fbe78f4f 229{
3a330134 230 VirtIONet *n = to_virtio_net(vdev);
fbe78f4f 231
c9f79a3f
MT
232 features |= (1 << VIRTIO_NET_F_MAC);
233
3a330134 234 if (peer_has_vnet_hdr(n)) {
eb6b6c12 235 tap_using_vnet_hdr(n->nic->nc.peer, 1);
e35e23f6 236 n->host_hdr_len = sizeof(struct virtio_net_hdr);
8172539d
MT
237 } else {
238 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
239 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
240 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
241 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
242
243 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
244 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
245 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
246 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
247 }
3a330134 248
8172539d
MT
249 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
250 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
251 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
3a330134
MM
252 }
253
9bc6304c 254 if (!n->nic->nc.peer ||
2be64a68 255 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
9bc6304c
MT
256 return features;
257 }
258 if (!tap_get_vhost_net(n->nic->nc.peer)) {
259 return features;
260 }
261 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
fbe78f4f
AL
262}
263
8eca6b1b
AL
264static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
265{
266 uint32_t features = 0;
267
268 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
269 * but also these: */
270 features |= (1 << VIRTIO_NET_F_MAC);
184bd048
DK
271 features |= (1 << VIRTIO_NET_F_CSUM);
272 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
273 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
274 features |= (1 << VIRTIO_NET_F_HOST_ECN);
8eca6b1b 275
8172539d 276 return features;
8eca6b1b
AL
277}
278
fbe78f4f
AL
279static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
280{
281 VirtIONet *n = to_virtio_net(vdev);
282
283 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
e35e23f6
MT
284 n->guest_hdr_len = n->mergeable_rx_bufs ?
285 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
f5436dd9
MM
286
287 if (n->has_vnet_hdr) {
eb6b6c12 288 tap_set_offload(n->nic->nc.peer,
f5436dd9
MM
289 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
290 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
291 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
6c9f58ba
SS
292 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
293 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
f5436dd9 294 }
dc14a397 295 if (!n->nic->nc.peer ||
2be64a68 296 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
dc14a397
DS
297 return;
298 }
299 if (!tap_get_vhost_net(n->nic->nc.peer)) {
300 return;
301 }
57c3229b 302 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
fbe78f4f
AL
303}
304
002437cd
AL
305static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
306 VirtQueueElement *elem)
307{
308 uint8_t on;
309
310 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
e7b43f7e 311 error_report("virtio-net ctrl invalid rx mode command");
002437cd
AL
312 exit(1);
313 }
314
315 on = ldub_p(elem->out_sg[1].iov_base);
316
317 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
318 n->promisc = on;
319 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
320 n->allmulti = on;
015cb166
AW
321 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
322 n->alluni = on;
323 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
324 n->nomulti = on;
325 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
326 n->nouni = on;
327 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
328 n->nobcast = on;
002437cd
AL
329 else
330 return VIRTIO_NET_ERR;
331
332 return VIRTIO_NET_OK;
333}
334
b6503ed9
AL
335static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
336 VirtQueueElement *elem)
337{
338 struct virtio_net_ctrl_mac mac_data;
339
340 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
341 elem->out_sg[1].iov_len < sizeof(mac_data) ||
342 elem->out_sg[2].iov_len < sizeof(mac_data))
343 return VIRTIO_NET_ERR;
344
345 n->mac_table.in_use = 0;
2d9aba39 346 n->mac_table.first_multi = 0;
8fd2a2f1
AW
347 n->mac_table.uni_overflow = 0;
348 n->mac_table.multi_overflow = 0;
b6503ed9
AL
349 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
350
44b15bc5 351 mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
b6503ed9
AL
352
353 if (sizeof(mac_data.entries) +
354 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
355 return VIRTIO_NET_ERR;
356
357 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
358 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
359 mac_data.entries * ETH_ALEN);
360 n->mac_table.in_use += mac_data.entries;
361 } else {
8fd2a2f1 362 n->mac_table.uni_overflow = 1;
b6503ed9
AL
363 }
364
2d9aba39
AW
365 n->mac_table.first_multi = n->mac_table.in_use;
366
44b15bc5 367 mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
b6503ed9
AL
368
369 if (sizeof(mac_data.entries) +
370 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
371 return VIRTIO_NET_ERR;
372
373 if (mac_data.entries) {
374 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
375 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
376 elem->out_sg[2].iov_base + sizeof(mac_data),
377 mac_data.entries * ETH_ALEN);
378 n->mac_table.in_use += mac_data.entries;
8fd2a2f1
AW
379 } else {
380 n->mac_table.multi_overflow = 1;
381 }
b6503ed9
AL
382 }
383
384 return VIRTIO_NET_OK;
385}
386
f21c0ed9
AL
387static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
388 VirtQueueElement *elem)
389{
390 uint16_t vid;
391
392 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
e7b43f7e 393 error_report("virtio-net ctrl invalid vlan command");
f21c0ed9
AL
394 return VIRTIO_NET_ERR;
395 }
396
44b15bc5 397 vid = lduw_p(elem->out_sg[1].iov_base);
f21c0ed9
AL
398
399 if (vid >= MAX_VLAN)
400 return VIRTIO_NET_ERR;
401
402 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
403 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
404 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
405 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
406 else
407 return VIRTIO_NET_ERR;
408
409 return VIRTIO_NET_OK;
410}
411
3d11d36c
AL
412static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
413{
002437cd 414 VirtIONet *n = to_virtio_net(vdev);
3d11d36c
AL
415 struct virtio_net_ctrl_hdr ctrl;
416 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
417 VirtQueueElement elem;
418
419 while (virtqueue_pop(vq, &elem)) {
420 if ((elem.in_num < 1) || (elem.out_num < 1)) {
e7b43f7e 421 error_report("virtio-net ctrl missing headers");
3d11d36c
AL
422 exit(1);
423 }
424
425 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
c6bb9a32 426 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
e7b43f7e 427 error_report("virtio-net ctrl header not in correct element");
3d11d36c
AL
428 exit(1);
429 }
430
431 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
432 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
433
002437cd
AL
434 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
435 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
b6503ed9
AL
436 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
437 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
f21c0ed9
AL
438 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
439 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
002437cd 440
3d11d36c
AL
441 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
442
443 virtqueue_push(vq, &elem, sizeof(status));
444 virtio_notify(vdev, vq);
445 }
446}
447
fbe78f4f
AL
448/* RX */
449
450static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
451{
8aeff62d
MM
452 VirtIONet *n = to_virtio_net(vdev);
453
eb6b6c12 454 qemu_flush_queued_packets(&n->nic->nc);
fbe78f4f
AL
455}
456
4e68f7a0 457static int virtio_net_can_receive(NetClientState *nc)
fbe78f4f 458{
eb6b6c12 459 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
85cf2a8d 460 if (!n->vdev.vm_running) {
95477323
MT
461 return 0;
462 }
cdd5cc12 463
fbe78f4f
AL
464 if (!virtio_queue_ready(n->rx_vq) ||
465 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
466 return 0;
467
cdd5cc12
MM
468 return 1;
469}
470
471static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
472{
fbe78f4f
AL
473 if (virtio_queue_empty(n->rx_vq) ||
474 (n->mergeable_rx_bufs &&
475 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
476 virtio_queue_set_notification(n->rx_vq, 1);
06b12970
TL
477
478 /* To avoid a race condition where the guest has made some buffers
479 * available after the above check but before notification was
480 * enabled, check for available buffers again.
481 */
482 if (virtio_queue_empty(n->rx_vq) ||
483 (n->mergeable_rx_bufs &&
484 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
485 return 0;
fbe78f4f
AL
486 }
487
488 virtio_queue_set_notification(n->rx_vq, 0);
489 return 1;
490}
491
1d41b0c1
AL
492/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
493 * it never finds out that the packets don't have valid checksums. This
494 * causes dhclient to get upset. Fedora's carried a patch for ages to
495 * fix this with Xen but it hasn't appeared in an upstream release of
496 * dhclient yet.
497 *
498 * To avoid breaking existing guests, we catch udp packets and add
499 * checksums. This is terrible but it's better than hacking the guest
500 * kernels.
501 *
502 * N.B. if we introduce a zero-copy API, this operation is no longer free so
503 * we should provide a mechanism to disable it to avoid polluting the host
504 * cache.
505 */
506static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
22cc84db 507 uint8_t *buf, size_t size)
1d41b0c1
AL
508{
509 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
510 (size > 27 && size < 1500) && /* normal sized MTU */
511 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
512 (buf[23] == 17) && /* ip.protocol == UDP */
513 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
22cc84db 514 net_checksum_calculate(buf, size);
1d41b0c1
AL
515 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
516 }
517}
518
22cc84db
MT
519static int receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
520 const void *buf, size_t size)
fbe78f4f 521{
fbe78f4f
AL
522 int offset = 0;
523
3a330134 524 if (n->has_vnet_hdr) {
22cc84db
MT
525 /* FIXME this cast is evil */
526 void *wbuf = (void *)buf;
527 work_around_broken_dhclient(wbuf, wbuf + offset, size - offset);
528 offset = sizeof(struct virtio_net_hdr);
529 iov_from_buf(iov, iov_cnt, 0, buf, offset);
530 } else {
531 struct virtio_net_hdr hdr = {
532 .flags = 0,
533 .gso_type = VIRTIO_NET_HDR_GSO_NONE
534 };
535 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
3a330134
MM
536 }
537
fbe78f4f
AL
538 return offset;
539}
540
3831ab20
AL
541static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
542{
543 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 544 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 545 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 546 int i;
3831ab20
AL
547
548 if (n->promisc)
549 return 1;
550
3a330134
MM
551 if (n->has_vnet_hdr) {
552 ptr += sizeof(struct virtio_net_hdr);
553 }
554
f21c0ed9
AL
555 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
556 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
557 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
558 return 0;
559 }
560
bbe2f399
AW
561 if (ptr[0] & 1) { // multicast
562 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
563 return !n->nobcast;
564 } else if (n->nomulti) {
565 return 0;
8fd2a2f1 566 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
567 return 1;
568 }
2d9aba39
AW
569
570 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
571 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
572 return 1;
573 }
574 }
bbe2f399 575 } else { // unicast
015cb166
AW
576 if (n->nouni) {
577 return 0;
578 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
579 return 1;
580 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
581 return 1;
582 }
3831ab20 583
2d9aba39
AW
584 for (i = 0; i < n->mac_table.first_multi; i++) {
585 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
586 return 1;
587 }
588 }
b6503ed9
AL
589 }
590
3831ab20
AL
591 return 0;
592}
593
4e68f7a0 594static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
fbe78f4f 595{
eb6b6c12 596 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
63c58728
MT
597 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
598 struct virtio_net_hdr_mrg_rxbuf mhdr;
599 unsigned mhdr_cnt = 0;
22cc84db 600 size_t offset, i, guest_offset;
fbe78f4f 601
eb6b6c12 602 if (!virtio_net_can_receive(&n->nic->nc))
cdd5cc12
MM
603 return -1;
604
940cda94 605 /* hdr_len refers to the header we supply to the guest */
e35e23f6 606 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
8aeff62d 607 return 0;
fbe78f4f 608
3831ab20 609 if (!receive_filter(n, buf, size))
4f1c942b 610 return size;
3831ab20 611
fbe78f4f
AL
612 offset = i = 0;
613
614 while (offset < size) {
615 VirtQueueElement elem;
616 int len, total;
22cc84db 617 const struct iovec *sg = elem.in_sg;
fbe78f4f 618
22c253d9 619 total = 0;
fbe78f4f 620
279a4253 621 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
fbe78f4f 622 if (i == 0)
4f1c942b 623 return -1;
e7b43f7e 624 error_report("virtio-net unexpected empty queue: "
279a4253 625 "i %zd mergeable %d offset %zd, size %zd, "
e7b43f7e 626 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
279a4253 627 i, n->mergeable_rx_bufs, offset, size,
e35e23f6 628 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
fbe78f4f
AL
629 exit(1);
630 }
631
632 if (elem.in_num < 1) {
e7b43f7e 633 error_report("virtio-net receive queue contains no in buffers");
fbe78f4f
AL
634 exit(1);
635 }
636
fbe78f4f 637 if (i == 0) {
63c58728
MT
638 if (n->mergeable_rx_bufs) {
639 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
640 sg, elem.in_num,
641 offsetof(typeof(mhdr), num_buffers),
642 sizeof(mhdr.num_buffers));
643 }
fbe78f4f
AL
644
645 offset += receive_header(n, sg, elem.in_num,
22cc84db 646 buf + offset, size - offset);
e35e23f6 647 total += n->guest_hdr_len;
22cc84db
MT
648 guest_offset = n->guest_hdr_len;
649 } else {
650 guest_offset = 0;
fbe78f4f
AL
651 }
652
653 /* copy in packet. ugh */
22cc84db 654 len = iov_from_buf(sg, elem.in_num, guest_offset,
dcf6f5e1 655 buf + offset, size - offset);
fbe78f4f 656 total += len;
279a4253
MT
657 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
e7b43f7e
SH
663 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,
e35e23f6 667 offset, size, n->guest_hdr_len, n->host_hdr_len);
279a4253
MT
668#endif
669 return size;
670 }
fbe78f4f
AL
671
672 /* signal other side */
673 virtqueue_fill(n->rx_vq, &elem, total, i++);
fbe78f4f
AL
674 }
675
63c58728
MT
676 if (mhdr_cnt) {
677 stw_p(&mhdr.num_buffers, i);
678 iov_from_buf(mhdr_sg, mhdr_cnt,
679 0,
680 &mhdr.num_buffers, sizeof mhdr.num_buffers);
44b15bc5 681 }
fbe78f4f
AL
682
683 virtqueue_flush(n->rx_vq, i);
684 virtio_notify(&n->vdev, n->rx_vq);
4f1c942b
MM
685
686 return size;
fbe78f4f
AL
687}
688
e3f30488 689static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
6243375f 690
4e68f7a0 691static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
6243375f 692{
eb6b6c12 693 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
6243375f 694
40bad8f3 695 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
6243375f
MM
696 virtio_notify(&n->vdev, n->tx_vq);
697
698 n->async_tx.elem.out_num = n->async_tx.len = 0;
699
700 virtio_queue_set_notification(n->tx_vq, 1);
701 virtio_net_flush_tx(n, n->tx_vq);
702}
703
fbe78f4f 704/* TX */
e3f30488 705static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
fbe78f4f
AL
706{
707 VirtQueueElement elem;
e3f30488 708 int32_t num_packets = 0;
e3f30488
AW
709 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
710 return num_packets;
711 }
fbe78f4f 712
85cf2a8d 713 assert(n->vdev.vm_running);
783e7706 714
6243375f
MM
715 if (n->async_tx.elem.out_num) {
716 virtio_queue_set_notification(n->tx_vq, 0);
e3f30488 717 return num_packets;
6243375f
MM
718 }
719
fbe78f4f 720 while (virtqueue_pop(vq, &elem)) {
6243375f 721 ssize_t ret, len = 0;
fbe78f4f
AL
722 unsigned int out_num = elem.out_num;
723 struct iovec *out_sg = &elem.out_sg[0];
724 unsigned hdr_len;
725
726 /* hdr_len refers to the header received from the guest */
727 hdr_len = n->mergeable_rx_bufs ?
728 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
729 sizeof(struct virtio_net_hdr);
730
731 if (out_num < 1 || out_sg->iov_len != hdr_len) {
e7b43f7e 732 error_report("virtio-net header not in first element");
fbe78f4f
AL
733 exit(1);
734 }
735
736 /* ignore the header if GSO is not supported */
3a330134 737 if (!n->has_vnet_hdr) {
fbe78f4f
AL
738 out_num--;
739 out_sg++;
740 len += hdr_len;
741 } else if (n->mergeable_rx_bufs) {
742 /* tapfd expects a struct virtio_net_hdr */
743 hdr_len -= sizeof(struct virtio_net_hdr);
744 out_sg->iov_len -= hdr_len;
745 len += hdr_len;
746 }
747
eb6b6c12 748 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
6243375f
MM
749 virtio_net_tx_complete);
750 if (ret == 0) {
751 virtio_queue_set_notification(n->tx_vq, 0);
752 n->async_tx.elem = elem;
753 n->async_tx.len = len;
e3f30488 754 return -EBUSY;
6243375f
MM
755 }
756
757 len += ret;
fbe78f4f 758
40bad8f3 759 virtqueue_push(vq, &elem, 0);
fbe78f4f 760 virtio_notify(&n->vdev, vq);
e3f30488
AW
761
762 if (++num_packets >= n->tx_burst) {
763 break;
764 }
fbe78f4f 765 }
e3f30488 766 return num_packets;
fbe78f4f
AL
767}
768
a697a334 769static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
fbe78f4f
AL
770{
771 VirtIONet *n = to_virtio_net(vdev);
772
783e7706 773 /* This happens when device was stopped but VCPU wasn't. */
85cf2a8d 774 if (!n->vdev.vm_running) {
783e7706
MT
775 n->tx_waiting = 1;
776 return;
777 }
778
4b4b8d36 779 if (n->tx_waiting) {
fbe78f4f
AL
780 virtio_queue_set_notification(vq, 1);
781 qemu_del_timer(n->tx_timer);
4b4b8d36 782 n->tx_waiting = 0;
fbe78f4f
AL
783 virtio_net_flush_tx(n, vq);
784 } else {
785 qemu_mod_timer(n->tx_timer,
74475455 786 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
4b4b8d36 787 n->tx_waiting = 1;
fbe78f4f
AL
788 virtio_queue_set_notification(vq, 0);
789 }
790}
791
a697a334
AW
792static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
793{
794 VirtIONet *n = to_virtio_net(vdev);
795
796 if (unlikely(n->tx_waiting)) {
797 return;
798 }
783e7706
MT
799 n->tx_waiting = 1;
800 /* This happens when device was stopped but VCPU wasn't. */
85cf2a8d 801 if (!n->vdev.vm_running) {
783e7706
MT
802 return;
803 }
a697a334
AW
804 virtio_queue_set_notification(vq, 0);
805 qemu_bh_schedule(n->tx_bh);
a697a334
AW
806}
807
fbe78f4f
AL
808static void virtio_net_tx_timer(void *opaque)
809{
810 VirtIONet *n = opaque;
85cf2a8d 811 assert(n->vdev.vm_running);
fbe78f4f 812
4b4b8d36 813 n->tx_waiting = 0;
fbe78f4f
AL
814
815 /* Just in case the driver is not ready on more */
816 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
817 return;
818
819 virtio_queue_set_notification(n->tx_vq, 1);
820 virtio_net_flush_tx(n, n->tx_vq);
821}
822
a697a334
AW
823static void virtio_net_tx_bh(void *opaque)
824{
825 VirtIONet *n = opaque;
826 int32_t ret;
827
85cf2a8d 828 assert(n->vdev.vm_running);
783e7706 829
a697a334
AW
830 n->tx_waiting = 0;
831
832 /* Just in case the driver is not ready on more */
833 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
834 return;
835
836 ret = virtio_net_flush_tx(n, n->tx_vq);
837 if (ret == -EBUSY) {
838 return; /* Notification re-enable handled by tx_complete */
839 }
840
841 /* If we flush a full burst of packets, assume there are
842 * more coming and immediately reschedule */
843 if (ret >= n->tx_burst) {
844 qemu_bh_schedule(n->tx_bh);
845 n->tx_waiting = 1;
846 return;
847 }
848
849 /* If less than a full burst, re-enable notification and flush
850 * anything that may have come in while we weren't looking. If
851 * we find something, assume the guest is still active and reschedule */
852 virtio_queue_set_notification(n->tx_vq, 1);
853 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
854 virtio_queue_set_notification(n->tx_vq, 0);
855 qemu_bh_schedule(n->tx_bh);
856 n->tx_waiting = 1;
857 }
858}
859
fbe78f4f
AL
860static void virtio_net_save(QEMUFile *f, void *opaque)
861{
862 VirtIONet *n = opaque;
863
afbaa7b4
MT
864 /* At this point, backend must be stopped, otherwise
865 * it might keep writing to memory. */
866 assert(!n->vhost_started);
fbe78f4f
AL
867 virtio_save(&n->vdev, f);
868
79674068 869 qemu_put_buffer(f, n->mac, ETH_ALEN);
4b4b8d36 870 qemu_put_be32(f, n->tx_waiting);
e46cb38f 871 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 872 qemu_put_be16(f, n->status);
f10c592e
AW
873 qemu_put_byte(f, n->promisc);
874 qemu_put_byte(f, n->allmulti);
b6503ed9
AL
875 qemu_put_be32(f, n->mac_table.in_use);
876 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 877 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
3a330134 878 qemu_put_be32(f, n->has_vnet_hdr);
8fd2a2f1
AW
879 qemu_put_byte(f, n->mac_table.multi_overflow);
880 qemu_put_byte(f, n->mac_table.uni_overflow);
015cb166
AW
881 qemu_put_byte(f, n->alluni);
882 qemu_put_byte(f, n->nomulti);
883 qemu_put_byte(f, n->nouni);
884 qemu_put_byte(f, n->nobcast);
0ce0e8f4 885 qemu_put_byte(f, n->has_ufo);
fbe78f4f
AL
886}
887
888static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
889{
890 VirtIONet *n = opaque;
2d9aba39 891 int i;
2a633c46 892 int ret;
fbe78f4f 893
9d6271b8 894 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
895 return -EINVAL;
896
2a633c46
OW
897 ret = virtio_load(&n->vdev, f);
898 if (ret) {
899 return ret;
900 }
fbe78f4f 901
79674068 902 qemu_get_buffer(f, n->mac, ETH_ALEN);
4b4b8d36 903 n->tx_waiting = qemu_get_be32(f);
e46cb38f 904 n->mergeable_rx_bufs = qemu_get_be32(f);
e35e23f6
MT
905 n->guest_hdr_len = n->mergeable_rx_bufs ?
906 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
fbe78f4f 907
9d6271b8
AL
908 if (version_id >= 3)
909 n->status = qemu_get_be16(f);
910
002437cd 911 if (version_id >= 4) {
f10c592e
AW
912 if (version_id < 8) {
913 n->promisc = qemu_get_be32(f);
914 n->allmulti = qemu_get_be32(f);
915 } else {
916 n->promisc = qemu_get_byte(f);
917 n->allmulti = qemu_get_byte(f);
918 }
002437cd
AL
919 }
920
b6503ed9
AL
921 if (version_id >= 5) {
922 n->mac_table.in_use = qemu_get_be32(f);
923 /* MAC_TABLE_ENTRIES may be different from the saved image */
924 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
925 qemu_get_buffer(f, n->mac_table.macs,
926 n->mac_table.in_use * ETH_ALEN);
927 } else if (n->mac_table.in_use) {
e398d61b
JQ
928 uint8_t *buf = g_malloc0(n->mac_table.in_use);
929 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
930 g_free(buf);
8fd2a2f1 931 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
b6503ed9
AL
932 n->mac_table.in_use = 0;
933 }
934 }
935
f21c0ed9
AL
936 if (version_id >= 6)
937 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
938
3a330134
MM
939 if (version_id >= 7) {
940 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1ecda02b 941 error_report("virtio-net: saved image requires vnet_hdr=on");
3a330134
MM
942 return -1;
943 }
944
945 if (n->has_vnet_hdr) {
eb6b6c12 946 tap_using_vnet_hdr(n->nic->nc.peer, 1);
e35e23f6 947 n->host_hdr_len = sizeof(struct virtio_net_hdr);
eb6b6c12 948 tap_set_offload(n->nic->nc.peer,
704a76fc
MT
949 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
950 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
951 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
952 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
953 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
3a330134 954 }
6c042c16
AW
955 }
956
8fd2a2f1
AW
957 if (version_id >= 9) {
958 n->mac_table.multi_overflow = qemu_get_byte(f);
959 n->mac_table.uni_overflow = qemu_get_byte(f);
960 }
961
015cb166
AW
962 if (version_id >= 10) {
963 n->alluni = qemu_get_byte(f);
964 n->nomulti = qemu_get_byte(f);
965 n->nouni = qemu_get_byte(f);
966 n->nobcast = qemu_get_byte(f);
967 }
968
0ce0e8f4
MM
969 if (version_id >= 11) {
970 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1ecda02b 971 error_report("virtio-net: saved image requires TUN_F_UFO support");
0ce0e8f4
MM
972 return -1;
973 }
974 }
975
2d9aba39
AW
976 /* Find the first multicast entry in the saved MAC filter */
977 for (i = 0; i < n->mac_table.in_use; i++) {
978 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
979 break;
980 }
981 }
982 n->mac_table.first_multi = i;
98991481
AK
983
984 /* nc.link_down can't be migrated, so infer link_down according
985 * to link status bit in n->status */
986 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
987
fbe78f4f
AL
988 return 0;
989}
990
4e68f7a0 991static void virtio_net_cleanup(NetClientState *nc)
b946a153 992{
eb6b6c12 993 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
b946a153 994
eb6b6c12 995 n->nic = NULL;
b946a153
AL
996}
997
eb6b6c12 998static NetClientInfo net_virtio_info = {
2be64a68 999 .type = NET_CLIENT_OPTIONS_KIND_NIC,
eb6b6c12
MM
1000 .size = sizeof(NICState),
1001 .can_receive = virtio_net_can_receive,
1002 .receive = virtio_net_receive,
1003 .cleanup = virtio_net_cleanup,
1004 .link_status_changed = virtio_net_set_link_status,
1005};
1006
f0c07c7c
AW
1007VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1008 virtio_net_conf *net)
fbe78f4f
AL
1009{
1010 VirtIONet *n;
fbe78f4f 1011
53c25cea
PB
1012 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1013 sizeof(struct virtio_net_config),
1014 sizeof(VirtIONet));
fbe78f4f 1015
0f03eca6
AL
1016 n->vdev.get_config = virtio_net_get_config;
1017 n->vdev.set_config = virtio_net_set_config;
fbe78f4f
AL
1018 n->vdev.get_features = virtio_net_get_features;
1019 n->vdev.set_features = virtio_net_set_features;
8eca6b1b 1020 n->vdev.bad_features = virtio_net_bad_features;
002437cd 1021 n->vdev.reset = virtio_net_reset;
9bc6304c 1022 n->vdev.set_status = virtio_net_set_status;
fbe78f4f 1023 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
a697a334
AW
1024
1025 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
e7b43f7e
SH
1026 error_report("virtio-net: "
1027 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1028 net->tx);
1029 error_report("Defaulting to \"bh\"");
a697a334
AW
1030 }
1031
1032 if (net->tx && !strcmp(net->tx, "timer")) {
1033 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
74475455 1034 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
a697a334
AW
1035 n->tx_timeout = net->txtimer;
1036 } else {
1037 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1038 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1039 }
4ffb17f5 1040 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
97b15621 1041 qemu_macaddr_default_if_unset(&conf->macaddr);
3cbe04c4 1042 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
554c97dd 1043 n->status = VIRTIO_NET_S_LINK_UP;
fbe78f4f 1044
f79f2bfc 1045 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
eb6b6c12
MM
1046
1047 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
96d5e201 1048
4b4b8d36 1049 n->tx_waiting = 0;
e3f30488 1050 n->tx_burst = net->txburst;
fbe78f4f 1051 n->mergeable_rx_bufs = 0;
e35e23f6 1052 n->guest_hdr_len = sizeof(struct virtio_net_hdr);
002437cd 1053 n->promisc = 1; /* for compatibility */
fbe78f4f 1054
7267c094 1055 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 1056
7267c094 1057 n->vlans = g_malloc0(MAX_VLAN >> 3);
f21c0ed9 1058
01657c86
AW
1059 n->qdev = dev;
1060 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
fbe78f4f 1061 virtio_net_save, virtio_net_load, n);
cf21e106 1062
1ca4d09a
GN
1063 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1064
53c25cea 1065 return &n->vdev;
cf21e106 1066}
97b15621
GH
1067
1068void virtio_net_exit(VirtIODevice *vdev)
1069{
1070 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
9bc6304c 1071
afbaa7b4
MT
1072 /* This will stop vhost backend if appropriate. */
1073 virtio_net_set_status(vdev, 0);
97b15621 1074
eb6b6c12 1075 qemu_purge_queued_packets(&n->nic->nc);
97b15621 1076
01657c86 1077 unregister_savevm(n->qdev, "virtio-net", n);
97b15621 1078
7267c094
AL
1079 g_free(n->mac_table.macs);
1080 g_free(n->vlans);
97b15621 1081
a697a334
AW
1082 if (n->tx_timer) {
1083 qemu_del_timer(n->tx_timer);
1084 qemu_free_timer(n->tx_timer);
1085 } else {
1086 qemu_bh_delete(n->tx_bh);
1087 }
97b15621 1088
b20c6b9e 1089 qemu_del_net_client(&n->nic->nc);
b52dfd71 1090 virtio_cleanup(&n->vdev);
97b15621 1091}