]> git.proxmox.com Git - qemu.git/blame - hw/virtio-net.c
net: add return value to packet receive handler
[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
14#include "virtio.h"
15#include "net.h"
16#include "qemu-timer.h"
17#include "virtio-net.h"
18
f21c0ed9 19#define VIRTIO_NET_VM_VERSION 6
b6503ed9
AL
20
21#define MAC_TABLE_ENTRIES 32
f21c0ed9 22#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 23
fbe78f4f
AL
24typedef struct VirtIONet
25{
26 VirtIODevice vdev;
79674068 27 uint8_t mac[ETH_ALEN];
554c97dd 28 uint16_t status;
fbe78f4f
AL
29 VirtQueue *rx_vq;
30 VirtQueue *tx_vq;
3d11d36c 31 VirtQueue *ctrl_vq;
fbe78f4f
AL
32 VLANClientState *vc;
33 QEMUTimer *tx_timer;
34 int tx_timer_active;
35 int mergeable_rx_bufs;
002437cd
AL
36 int promisc;
37 int allmulti;
b6503ed9
AL
38 struct {
39 int in_use;
40 uint8_t *macs;
41 } mac_table;
f21c0ed9 42 uint32_t *vlans;
fbe78f4f
AL
43} VirtIONet;
44
45/* TODO
46 * - we could suppress RX interrupt if we were so inclined.
47 */
48
49static VirtIONet *to_virtio_net(VirtIODevice *vdev)
50{
51 return (VirtIONet *)vdev;
52}
53
0f03eca6 54static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f
AL
55{
56 VirtIONet *n = to_virtio_net(vdev);
57 struct virtio_net_config netcfg;
58
554c97dd 59 netcfg.status = n->status;
79674068 60 memcpy(netcfg.mac, n->mac, ETH_ALEN);
fbe78f4f
AL
61 memcpy(config, &netcfg, sizeof(netcfg));
62}
63
0f03eca6
AL
64static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
65{
66 VirtIONet *n = to_virtio_net(vdev);
67 struct virtio_net_config netcfg;
68
69 memcpy(&netcfg, config, sizeof(netcfg));
70
79674068
AL
71 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
72 memcpy(n->mac, netcfg.mac, ETH_ALEN);
0f03eca6
AL
73 qemu_format_nic_info_str(n->vc, n->mac);
74 }
75}
76
554c97dd
AL
77static void virtio_net_set_link_status(VLANClientState *vc)
78{
79 VirtIONet *n = vc->opaque;
80 uint16_t old_status = n->status;
81
82 if (vc->link_down)
83 n->status &= ~VIRTIO_NET_S_LINK_UP;
84 else
85 n->status |= VIRTIO_NET_S_LINK_UP;
86
87 if (n->status != old_status)
88 virtio_notify_config(&n->vdev);
89}
90
002437cd
AL
91static void virtio_net_reset(VirtIODevice *vdev)
92{
93 VirtIONet *n = to_virtio_net(vdev);
94
95 /* Reset back to compatibility mode */
96 n->promisc = 1;
97 n->allmulti = 0;
b6503ed9 98
f21c0ed9 99 /* Flush any MAC and VLAN filter table state */
b6503ed9
AL
100 n->mac_table.in_use = 0;
101 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
f21c0ed9 102 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
103}
104
fbe78f4f
AL
105static uint32_t virtio_net_get_features(VirtIODevice *vdev)
106{
3d11d36c
AL
107 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
108 (1 << VIRTIO_NET_F_STATUS) |
b6503ed9 109 (1 << VIRTIO_NET_F_CTRL_VQ) |
f21c0ed9
AL
110 (1 << VIRTIO_NET_F_CTRL_RX) |
111 (1 << VIRTIO_NET_F_CTRL_VLAN);
fbe78f4f
AL
112
113 return features;
114}
115
8eca6b1b
AL
116static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
117{
118 uint32_t features = 0;
119
120 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
121 * but also these: */
122 features |= (1 << VIRTIO_NET_F_MAC);
123 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
124 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
125 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
126 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
127
128 return features & virtio_net_get_features(vdev);
129}
130
fbe78f4f
AL
131static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
132{
133 VirtIONet *n = to_virtio_net(vdev);
134
135 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
136}
137
002437cd
AL
138static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
139 VirtQueueElement *elem)
140{
141 uint8_t on;
142
143 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
144 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
145 exit(1);
146 }
147
148 on = ldub_p(elem->out_sg[1].iov_base);
149
150 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
151 n->promisc = on;
152 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
153 n->allmulti = on;
154 else
155 return VIRTIO_NET_ERR;
156
157 return VIRTIO_NET_OK;
158}
159
b6503ed9
AL
160static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
161 VirtQueueElement *elem)
162{
163 struct virtio_net_ctrl_mac mac_data;
164
165 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
166 elem->out_sg[1].iov_len < sizeof(mac_data) ||
167 elem->out_sg[2].iov_len < sizeof(mac_data))
168 return VIRTIO_NET_ERR;
169
170 n->mac_table.in_use = 0;
171 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
172
173 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
174
175 if (sizeof(mac_data.entries) +
176 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
177 return VIRTIO_NET_ERR;
178
179 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
180 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
181 mac_data.entries * ETH_ALEN);
182 n->mac_table.in_use += mac_data.entries;
183 } else {
184 n->promisc = 1;
185 return VIRTIO_NET_OK;
186 }
187
188 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
189
190 if (sizeof(mac_data.entries) +
191 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
192 return VIRTIO_NET_ERR;
193
194 if (mac_data.entries) {
195 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
196 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
197 elem->out_sg[2].iov_base + sizeof(mac_data),
198 mac_data.entries * ETH_ALEN);
199 n->mac_table.in_use += mac_data.entries;
200 } else
201 n->allmulti = 1;
202 }
203
204 return VIRTIO_NET_OK;
205}
206
f21c0ed9
AL
207static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
208 VirtQueueElement *elem)
209{
210 uint16_t vid;
211
212 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
213 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
214 return VIRTIO_NET_ERR;
215 }
216
217 vid = lduw_le_p(elem->out_sg[1].iov_base);
218
219 if (vid >= MAX_VLAN)
220 return VIRTIO_NET_ERR;
221
222 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
223 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
224 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
225 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
226 else
227 return VIRTIO_NET_ERR;
228
229 return VIRTIO_NET_OK;
230}
231
3d11d36c
AL
232static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
233{
002437cd 234 VirtIONet *n = to_virtio_net(vdev);
3d11d36c
AL
235 struct virtio_net_ctrl_hdr ctrl;
236 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
237 VirtQueueElement elem;
238
239 while (virtqueue_pop(vq, &elem)) {
240 if ((elem.in_num < 1) || (elem.out_num < 1)) {
241 fprintf(stderr, "virtio-net ctrl missing headers\n");
242 exit(1);
243 }
244
245 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
c6bb9a32 246 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
3d11d36c
AL
247 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
248 exit(1);
249 }
250
251 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
252 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
253
002437cd
AL
254 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
255 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
b6503ed9
AL
256 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
257 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
f21c0ed9
AL
258 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
259 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
002437cd 260
3d11d36c
AL
261 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
262
263 virtqueue_push(vq, &elem, sizeof(status));
264 virtio_notify(vdev, vq);
265 }
266}
267
fbe78f4f
AL
268/* RX */
269
270static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
271{
272}
273
274static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
275{
276 if (!virtio_queue_ready(n->rx_vq) ||
277 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
278 return 0;
279
280 if (virtio_queue_empty(n->rx_vq) ||
281 (n->mergeable_rx_bufs &&
282 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
283 virtio_queue_set_notification(n->rx_vq, 1);
284 return 0;
285 }
286
287 virtio_queue_set_notification(n->rx_vq, 0);
288 return 1;
289}
290
e3f5ec2b 291static int virtio_net_can_receive(VLANClientState *vc)
fbe78f4f 292{
e3f5ec2b 293 VirtIONet *n = vc->opaque;
fbe78f4f
AL
294
295 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
296}
297
298static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
299{
300 int offset, i;
301
302 offset = i = 0;
303 while (offset < count && i < iovcnt) {
304 int len = MIN(iov[i].iov_len, count - offset);
305 memcpy(iov[i].iov_base, buf + offset, len);
306 offset += len;
307 i++;
308 }
309
310 return offset;
311}
312
313static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
4689f4b3 314 const void *buf, size_t size, size_t hdr_len)
fbe78f4f 315{
3f4cb3d3 316 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
fbe78f4f
AL
317 int offset = 0;
318
319 hdr->flags = 0;
320 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
321
322 /* We only ever receive a struct virtio_net_hdr from the tapfd,
323 * but we may be passing along a larger header to the guest.
324 */
325 iov[0].iov_base += hdr_len;
326 iov[0].iov_len -= hdr_len;
327
328 return offset;
329}
330
3831ab20
AL
331static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
332{
333 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 334 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 335 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 336 int i;
3831ab20
AL
337
338 if (n->promisc)
339 return 1;
340
f21c0ed9
AL
341 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
342 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
343 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
344 return 0;
345 }
346
3831ab20
AL
347 if ((ptr[0] & 1) && n->allmulti)
348 return 1;
349
350 if (!memcmp(ptr, bcast, sizeof(bcast)))
351 return 1;
352
353 if (!memcmp(ptr, n->mac, ETH_ALEN))
354 return 1;
355
b6503ed9
AL
356 for (i = 0; i < n->mac_table.in_use; i++) {
357 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
358 return 1;
359 }
360
3831ab20
AL
361 return 0;
362}
363
4f1c942b 364static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
fbe78f4f 365{
e3f5ec2b 366 VirtIONet *n = vc->opaque;
fbe78f4f 367 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
4689f4b3 368 size_t hdr_len, offset, i;
fbe78f4f
AL
369
370 if (!do_virtio_net_can_receive(n, size))
4f1c942b 371 return -1;
fbe78f4f 372
3831ab20 373 if (!receive_filter(n, buf, size))
4f1c942b 374 return size;
3831ab20 375
fbe78f4f
AL
376 /* hdr_len refers to the header we supply to the guest */
377 hdr_len = n->mergeable_rx_bufs ?
378 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
379
380 offset = i = 0;
381
382 while (offset < size) {
383 VirtQueueElement elem;
384 int len, total;
385 struct iovec sg[VIRTQUEUE_MAX_SIZE];
386
387 len = total = 0;
388
389 if ((i != 0 && !n->mergeable_rx_bufs) ||
390 virtqueue_pop(n->rx_vq, &elem) == 0) {
391 if (i == 0)
4f1c942b 392 return -1;
fbe78f4f
AL
393 fprintf(stderr, "virtio-net truncating packet\n");
394 exit(1);
395 }
396
397 if (elem.in_num < 1) {
398 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
399 exit(1);
400 }
401
402 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
403 fprintf(stderr, "virtio-net header not in first element\n");
404 exit(1);
405 }
406
407 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
408
409 if (i == 0) {
410 if (n->mergeable_rx_bufs)
411 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
412
413 offset += receive_header(n, sg, elem.in_num,
414 buf + offset, size - offset, hdr_len);
415 total += hdr_len;
416 }
417
418 /* copy in packet. ugh */
419 len = iov_fill(sg, elem.in_num,
420 buf + offset, size - offset);
421 total += len;
422
423 /* signal other side */
424 virtqueue_fill(n->rx_vq, &elem, total, i++);
425
426 offset += len;
427 }
428
429 if (mhdr)
430 mhdr->num_buffers = i;
431
432 virtqueue_flush(n->rx_vq, i);
433 virtio_notify(&n->vdev, n->rx_vq);
4f1c942b
MM
434
435 return size;
fbe78f4f
AL
436}
437
438/* TX */
439static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
440{
441 VirtQueueElement elem;
442 int has_vnet_hdr = 0;
443
444 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
445 return;
446
447 while (virtqueue_pop(vq, &elem)) {
448 ssize_t len = 0;
449 unsigned int out_num = elem.out_num;
450 struct iovec *out_sg = &elem.out_sg[0];
451 unsigned hdr_len;
452
453 /* hdr_len refers to the header received from the guest */
454 hdr_len = n->mergeable_rx_bufs ?
455 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
456 sizeof(struct virtio_net_hdr);
457
458 if (out_num < 1 || out_sg->iov_len != hdr_len) {
459 fprintf(stderr, "virtio-net header not in first element\n");
460 exit(1);
461 }
462
463 /* ignore the header if GSO is not supported */
464 if (!has_vnet_hdr) {
465 out_num--;
466 out_sg++;
467 len += hdr_len;
468 } else if (n->mergeable_rx_bufs) {
469 /* tapfd expects a struct virtio_net_hdr */
470 hdr_len -= sizeof(struct virtio_net_hdr);
471 out_sg->iov_len -= hdr_len;
472 len += hdr_len;
473 }
474
475 len += qemu_sendv_packet(n->vc, out_sg, out_num);
476
477 virtqueue_push(vq, &elem, len);
478 virtio_notify(&n->vdev, vq);
479 }
480}
481
482static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
483{
484 VirtIONet *n = to_virtio_net(vdev);
485
486 if (n->tx_timer_active) {
487 virtio_queue_set_notification(vq, 1);
488 qemu_del_timer(n->tx_timer);
489 n->tx_timer_active = 0;
490 virtio_net_flush_tx(n, vq);
491 } else {
492 qemu_mod_timer(n->tx_timer,
493 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
494 n->tx_timer_active = 1;
495 virtio_queue_set_notification(vq, 0);
496 }
497}
498
499static void virtio_net_tx_timer(void *opaque)
500{
501 VirtIONet *n = opaque;
502
503 n->tx_timer_active = 0;
504
505 /* Just in case the driver is not ready on more */
506 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
507 return;
508
509 virtio_queue_set_notification(n->tx_vq, 1);
510 virtio_net_flush_tx(n, n->tx_vq);
511}
512
513static void virtio_net_save(QEMUFile *f, void *opaque)
514{
515 VirtIONet *n = opaque;
516
517 virtio_save(&n->vdev, f);
518
79674068 519 qemu_put_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 520 qemu_put_be32(f, n->tx_timer_active);
e46cb38f 521 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 522 qemu_put_be16(f, n->status);
002437cd
AL
523 qemu_put_be32(f, n->promisc);
524 qemu_put_be32(f, n->allmulti);
b6503ed9
AL
525 qemu_put_be32(f, n->mac_table.in_use);
526 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 527 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
fbe78f4f
AL
528}
529
530static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
531{
532 VirtIONet *n = opaque;
533
9d6271b8 534 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
535 return -EINVAL;
536
537 virtio_load(&n->vdev, f);
538
79674068 539 qemu_get_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 540 n->tx_timer_active = qemu_get_be32(f);
e46cb38f 541 n->mergeable_rx_bufs = qemu_get_be32(f);
fbe78f4f 542
9d6271b8
AL
543 if (version_id >= 3)
544 n->status = qemu_get_be16(f);
545
002437cd
AL
546 if (version_id >= 4) {
547 n->promisc = qemu_get_be32(f);
548 n->allmulti = qemu_get_be32(f);
549 }
550
b6503ed9
AL
551 if (version_id >= 5) {
552 n->mac_table.in_use = qemu_get_be32(f);
553 /* MAC_TABLE_ENTRIES may be different from the saved image */
554 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
555 qemu_get_buffer(f, n->mac_table.macs,
556 n->mac_table.in_use * ETH_ALEN);
557 } else if (n->mac_table.in_use) {
558 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
559 n->promisc = 1;
560 n->mac_table.in_use = 0;
561 }
562 }
563
f21c0ed9
AL
564 if (version_id >= 6)
565 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
566
fbe78f4f
AL
567 if (n->tx_timer_active) {
568 qemu_mod_timer(n->tx_timer,
569 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
570 }
571
572 return 0;
573}
574
b946a153
AL
575static void virtio_net_cleanup(VLANClientState *vc)
576{
577 VirtIONet *n = vc->opaque;
578
579 unregister_savevm("virtio-net", n);
580
581 qemu_free(n->mac_table.macs);
582 qemu_free(n->vlans);
583
584 qemu_del_timer(n->tx_timer);
585 qemu_free_timer(n->tx_timer);
586
587 virtio_cleanup(&n->vdev);
588}
589
53c25cea 590VirtIODevice *virtio_net_init(DeviceState *dev)
fbe78f4f
AL
591{
592 VirtIONet *n;
593 static int virtio_net_id;
594
53c25cea
PB
595 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
596 sizeof(struct virtio_net_config),
597 sizeof(VirtIONet));
fbe78f4f 598
0f03eca6
AL
599 n->vdev.get_config = virtio_net_get_config;
600 n->vdev.set_config = virtio_net_set_config;
fbe78f4f
AL
601 n->vdev.get_features = virtio_net_get_features;
602 n->vdev.set_features = virtio_net_set_features;
8eca6b1b 603 n->vdev.bad_features = virtio_net_bad_features;
002437cd 604 n->vdev.reset = virtio_net_reset;
fbe78f4f
AL
605 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
606 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
3d11d36c 607 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
53c25cea 608 qdev_get_macaddr(dev, n->mac);
554c97dd 609 n->status = VIRTIO_NET_S_LINK_UP;
53c25cea 610 n->vc = qdev_get_vlan_client(dev,
b946a153 611 virtio_net_can_receive,
463af534 612 virtio_net_receive, NULL,
b946a153 613 virtio_net_cleanup, n);
554c97dd 614 n->vc->link_status_changed = virtio_net_set_link_status;
fbe78f4f 615
96d5e201
AL
616 qemu_format_nic_info_str(n->vc, n->mac);
617
fbe78f4f
AL
618 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
619 n->tx_timer_active = 0;
620 n->mergeable_rx_bufs = 0;
002437cd 621 n->promisc = 1; /* for compatibility */
fbe78f4f 622
b6503ed9 623 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 624
f21c0ed9 625 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
f21c0ed9 626
9d6271b8 627 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
fbe78f4f 628 virtio_net_save, virtio_net_load, n);
cf21e106 629
53c25cea 630 return &n->vdev;
cf21e106 631}