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