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