2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
16 #include "qemu-timer.h"
17 #include "virtio-net.h"
24 /* Version 7 has TAP_VNET_HDR support. This is reserved in upstream QEMU to
25 * avoid future conflict.
26 * We can't assume verisons > 7 have TAP_VNET_HDR support until this is merged
29 #define VIRTIO_NET_VM_VERSION 7
31 #define MAC_TABLE_ENTRIES 32
32 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
34 typedef struct VirtIONet
37 uint8_t mac
[ETH_ALEN
];
45 int mergeable_rx_bufs
;
56 * - we could suppress RX interrupt if we were so inclined.
59 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
61 return (VirtIONet
*)vdev
;
64 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
66 VirtIONet
*n
= to_virtio_net(vdev
);
67 struct virtio_net_config netcfg
;
69 netcfg
.status
= n
->status
;
70 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
71 memcpy(config
, &netcfg
, sizeof(netcfg
));
74 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
76 VirtIONet
*n
= to_virtio_net(vdev
);
77 struct virtio_net_config netcfg
;
79 memcpy(&netcfg
, config
, sizeof(netcfg
));
81 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
82 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
83 qemu_format_nic_info_str(n
->vc
, n
->mac
);
87 static void virtio_net_set_link_status(VLANClientState
*vc
)
89 VirtIONet
*n
= vc
->opaque
;
90 uint16_t old_status
= n
->status
;
93 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
95 n
->status
|= VIRTIO_NET_S_LINK_UP
;
97 if (n
->status
!= old_status
)
98 virtio_notify_config(&n
->vdev
);
101 static void virtio_net_reset(VirtIODevice
*vdev
)
103 VirtIONet
*n
= to_virtio_net(vdev
);
105 /* Reset back to compatibility mode */
109 /* Flush any MAC and VLAN filter table state */
110 n
->mac_table
.in_use
= 0;
111 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
112 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
115 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
)
117 uint32_t features
= (1 << VIRTIO_NET_F_MAC
) |
118 (1 << VIRTIO_NET_F_STATUS
) |
119 (1 << VIRTIO_NET_F_CTRL_VQ
) |
120 (1 << VIRTIO_NET_F_CTRL_RX
) |
121 (1 << VIRTIO_NET_F_CTRL_VLAN
);
123 VirtIONet
*n
= to_virtio_net(vdev
);
124 VLANClientState
*host
= n
->vc
->vlan
->first_client
;
126 if (tap_has_vnet_hdr(host
)) {
127 tap_using_vnet_hdr(host
, 1);
128 features
|= (1 << VIRTIO_NET_F_CSUM
);
129 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
130 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
131 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
132 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
133 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
134 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
135 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
136 features
|= (1 << VIRTIO_NET_F_MRG_RXBUF
);
137 /* Kernel can't actually handle UFO in software currently. */
144 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
146 uint32_t features
= 0;
148 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
150 features
|= (1 << VIRTIO_NET_F_MAC
);
151 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
152 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
153 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
154 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
156 return features
& virtio_net_get_features(vdev
);
159 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
161 VirtIONet
*n
= to_virtio_net(vdev
);
163 VLANClientState
*host
= n
->vc
->vlan
->first_client
;
166 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
169 if (!tap_has_vnet_hdr(host
) || !host
->set_offload
)
172 host
->set_offload(host
,
173 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
174 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
175 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
176 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1);
180 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
181 VirtQueueElement
*elem
)
185 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
186 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
190 on
= ldub_p(elem
->out_sg
[1].iov_base
);
192 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
194 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
197 return VIRTIO_NET_ERR
;
199 return VIRTIO_NET_OK
;
202 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
203 VirtQueueElement
*elem
)
205 struct virtio_net_ctrl_mac mac_data
;
207 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
208 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
209 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
210 return VIRTIO_NET_ERR
;
212 n
->mac_table
.in_use
= 0;
213 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
215 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
217 if (sizeof(mac_data
.entries
) +
218 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
219 return VIRTIO_NET_ERR
;
221 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
222 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
223 mac_data
.entries
* ETH_ALEN
);
224 n
->mac_table
.in_use
+= mac_data
.entries
;
227 return VIRTIO_NET_OK
;
230 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
232 if (sizeof(mac_data
.entries
) +
233 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
234 return VIRTIO_NET_ERR
;
236 if (mac_data
.entries
) {
237 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
238 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
239 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
240 mac_data
.entries
* ETH_ALEN
);
241 n
->mac_table
.in_use
+= mac_data
.entries
;
246 return VIRTIO_NET_OK
;
249 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
250 VirtQueueElement
*elem
)
254 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
255 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
256 return VIRTIO_NET_ERR
;
259 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
262 return VIRTIO_NET_ERR
;
264 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
265 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
266 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
267 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
269 return VIRTIO_NET_ERR
;
271 return VIRTIO_NET_OK
;
274 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
276 VirtIONet
*n
= to_virtio_net(vdev
);
277 struct virtio_net_ctrl_hdr ctrl
;
278 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
279 VirtQueueElement elem
;
281 while (virtqueue_pop(vq
, &elem
)) {
282 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
283 fprintf(stderr
, "virtio-net ctrl missing headers\n");
287 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
288 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
289 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
293 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
294 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
296 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
297 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
298 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
299 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
300 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
301 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
303 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
305 virtqueue_push(vq
, &elem
, sizeof(status
));
306 virtio_notify(vdev
, vq
);
312 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
315 /* We now have RX buffers, signal to the IO thread to break out of the
316 select to re-poll the tap file descriptor */
318 qemu_kvm_notify_work();
322 static int do_virtio_net_can_receive(VirtIONet
*n
, int bufsize
)
324 if (!virtio_queue_ready(n
->rx_vq
) ||
325 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
328 if (virtio_queue_empty(n
->rx_vq
) ||
329 (n
->mergeable_rx_bufs
&&
330 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
331 virtio_queue_set_notification(n
->rx_vq
, 1);
335 virtio_queue_set_notification(n
->rx_vq
, 0);
339 static int virtio_net_can_receive(void *opaque
)
341 VirtIONet
*n
= opaque
;
343 return do_virtio_net_can_receive(n
, VIRTIO_NET_MAX_BUFSIZE
);
347 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
348 * it never finds out that the packets don't have valid checksums. This
349 * causes dhclient to get upset. Fedora's carried a patch for ages to
350 * fix this with Xen but it hasn't appeared in an upstream release of
353 * To avoid breaking existing guests, we catch udp packets and add
354 * checksums. This is terrible but it's better than hacking the guest
357 * N.B. if we introduce a zero-copy API, this operation is no longer free so
358 * we should provide a mechanism to disable it to avoid polluting the host
361 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
362 const uint8_t *buf
, size_t size
)
364 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
365 (size
> 27 && size
< 1500) && /* normal sized MTU */
366 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
367 (buf
[23] == 17) && /* ip.protocol == UDP */
368 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
369 /* FIXME this cast is evil */
370 net_checksum_calculate((uint8_t *)buf
, size
);
371 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
376 static int iov_fill(struct iovec
*iov
, int iovcnt
, const void *buf
, int count
)
381 while (offset
< count
&& i
< iovcnt
) {
382 int len
= MIN(iov
[i
].iov_len
, count
- offset
);
383 memcpy(iov
[i
].iov_base
, buf
+ offset
, len
);
391 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
392 const void *buf
, size_t size
, size_t hdr_len
)
394 struct virtio_net_hdr
*hdr
= iov
[0].iov_base
;
398 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
401 if (tap_has_vnet_hdr(n
->vc
->vlan
->first_client
)) {
402 memcpy(hdr
, buf
, sizeof(*hdr
));
403 offset
= sizeof(*hdr
);
404 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
408 /* We only ever receive a struct virtio_net_hdr from the tapfd,
409 * but we may be passing along a larger header to the guest.
411 iov
[0].iov_base
+= hdr_len
;
412 iov
[0].iov_len
-= hdr_len
;
417 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
419 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
420 static const uint8_t vlan
[] = {0x81, 0x00};
421 uint8_t *ptr
= (uint8_t *)buf
;
428 if (tap_has_vnet_hdr(n
->vc
->vlan
->first_client
))
429 ptr
+= sizeof(struct virtio_net_hdr
);
432 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
433 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
434 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
438 if ((ptr
[0] & 1) && n
->allmulti
)
441 if (!memcmp(ptr
, bcast
, sizeof(bcast
)))
444 if (!memcmp(ptr
, n
->mac
, ETH_ALEN
))
447 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
448 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
))
455 static void virtio_net_receive(void *opaque
, const uint8_t *buf
, int size
)
457 VirtIONet
*n
= opaque
;
458 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
459 size_t hdr_len
, offset
, i
;
461 if (!do_virtio_net_can_receive(n
, size
))
464 if (!receive_filter(n
, buf
, size
))
467 /* hdr_len refers to the header we supply to the guest */
468 hdr_len
= n
->mergeable_rx_bufs
?
469 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
473 while (offset
< size
) {
474 VirtQueueElement elem
;
476 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
480 if ((i
!= 0 && !n
->mergeable_rx_bufs
) ||
481 virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
484 fprintf(stderr
, "virtio-net truncating packet\n");
488 if (elem
.in_num
< 1) {
489 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
493 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= hdr_len
) {
494 fprintf(stderr
, "virtio-net header not in first element\n");
498 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
501 if (n
->mergeable_rx_bufs
)
502 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
504 offset
+= receive_header(n
, sg
, elem
.in_num
,
505 buf
+ offset
, size
- offset
, hdr_len
);
509 /* copy in packet. ugh */
510 len
= iov_fill(sg
, elem
.in_num
,
511 buf
+ offset
, size
- offset
);
514 /* signal other side */
515 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
521 mhdr
->num_buffers
= i
;
523 virtqueue_flush(n
->rx_vq
, i
);
524 virtio_notify(&n
->vdev
, n
->rx_vq
);
528 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
530 VirtQueueElement elem
;
532 int has_vnet_hdr
= tap_has_vnet_hdr(n
->vc
->vlan
->first_client
);
534 int has_vnet_hdr
= 0;
537 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
540 while (virtqueue_pop(vq
, &elem
)) {
542 unsigned int out_num
= elem
.out_num
;
543 struct iovec
*out_sg
= &elem
.out_sg
[0];
546 /* hdr_len refers to the header received from the guest */
547 hdr_len
= n
->mergeable_rx_bufs
?
548 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
549 sizeof(struct virtio_net_hdr
);
551 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
552 fprintf(stderr
, "virtio-net header not in first element\n");
556 /* ignore the header if GSO is not supported */
561 } else if (n
->mergeable_rx_bufs
) {
562 /* tapfd expects a struct virtio_net_hdr */
563 hdr_len
-= sizeof(struct virtio_net_hdr
);
564 out_sg
->iov_len
-= hdr_len
;
568 len
+= qemu_sendv_packet(n
->vc
, out_sg
, out_num
);
570 virtqueue_push(vq
, &elem
, len
);
571 virtio_notify(&n
->vdev
, vq
);
575 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
577 VirtIONet
*n
= to_virtio_net(vdev
);
579 if (n
->tx_timer_active
) {
580 virtio_queue_set_notification(vq
, 1);
581 qemu_del_timer(n
->tx_timer
);
582 n
->tx_timer_active
= 0;
583 virtio_net_flush_tx(n
, vq
);
585 qemu_mod_timer(n
->tx_timer
,
586 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
587 n
->tx_timer_active
= 1;
588 virtio_queue_set_notification(vq
, 0);
592 static void virtio_net_tx_timer(void *opaque
)
594 VirtIONet
*n
= opaque
;
596 n
->tx_timer_active
= 0;
598 /* Just in case the driver is not ready on more */
599 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
602 virtio_queue_set_notification(n
->tx_vq
, 1);
603 virtio_net_flush_tx(n
, n
->tx_vq
);
606 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
608 VirtIONet
*n
= opaque
;
610 virtio_save(&n
->vdev
, f
);
612 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
613 qemu_put_be32(f
, n
->tx_timer_active
);
614 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
615 qemu_put_be16(f
, n
->status
);
616 qemu_put_be32(f
, n
->promisc
);
617 qemu_put_be32(f
, n
->allmulti
);
618 qemu_put_be32(f
, n
->mac_table
.in_use
);
619 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
620 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
623 qemu_put_be32(f
, tap_has_vnet_hdr(n
->vc
->vlan
->first_client
));
629 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
631 VirtIONet
*n
= opaque
;
633 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
636 virtio_load(&n
->vdev
, f
);
638 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
639 n
->tx_timer_active
= qemu_get_be32(f
);
640 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
643 n
->status
= qemu_get_be16(f
);
645 if (version_id
>= 4) {
646 n
->promisc
= qemu_get_be32(f
);
647 n
->allmulti
= qemu_get_be32(f
);
650 if (version_id
>= 5) {
651 n
->mac_table
.in_use
= qemu_get_be32(f
);
652 /* MAC_TABLE_ENTRIES may be different from the saved image */
653 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
654 qemu_get_buffer(f
, n
->mac_table
.macs
,
655 n
->mac_table
.in_use
* ETH_ALEN
);
656 } else if (n
->mac_table
.in_use
) {
657 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
659 n
->mac_table
.in_use
= 0;
664 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
666 if (version_id
== 7 && qemu_get_be32(f
)) {
668 tap_using_vnet_hdr(n
->vc
->vlan
->first_client
, 1);
671 "virtio-net: saved image requires vnet header support\n");
676 if (n
->tx_timer_active
) {
677 qemu_mod_timer(n
->tx_timer
,
678 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
684 static void virtio_net_cleanup(VLANClientState
*vc
)
686 VirtIONet
*n
= vc
->opaque
;
688 unregister_savevm("virtio-net", n
);
690 qemu_free(n
->mac_table
.macs
);
693 qemu_del_timer(n
->tx_timer
);
694 qemu_free_timer(n
->tx_timer
);
696 virtio_cleanup(&n
->vdev
);
699 PCIDevice
*virtio_net_init(PCIBus
*bus
, NICInfo
*nd
, int devfn
)
702 static int virtio_net_id
;
704 n
= (VirtIONet
*)virtio_init_pci(bus
, "virtio-net",
705 PCI_VENDOR_ID_REDHAT_QUMRANET
,
706 PCI_DEVICE_ID_VIRTIO_NET
,
707 PCI_VENDOR_ID_REDHAT_QUMRANET
,
709 PCI_CLASS_NETWORK_ETHERNET
, 0x00,
710 sizeof(struct virtio_net_config
),
715 n
->vdev
.get_config
= virtio_net_get_config
;
716 n
->vdev
.set_config
= virtio_net_set_config
;
717 n
->vdev
.get_features
= virtio_net_get_features
;
718 n
->vdev
.set_features
= virtio_net_set_features
;
719 n
->vdev
.bad_features
= virtio_net_bad_features
;
720 n
->vdev
.reset
= virtio_net_reset
;
721 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
722 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx
);
723 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 16, virtio_net_handle_ctrl
);
724 memcpy(n
->mac
, nd
->macaddr
, ETH_ALEN
);
725 n
->status
= VIRTIO_NET_S_LINK_UP
;
726 n
->vc
= qemu_new_vlan_client(nd
->vlan
, nd
->model
, nd
->name
,
728 virtio_net_can_receive
,
729 virtio_net_cleanup
, n
);
730 n
->vc
->link_status_changed
= virtio_net_set_link_status
;
732 qemu_format_nic_info_str(n
->vc
, n
->mac
);
734 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
735 n
->tx_timer_active
= 0;
736 n
->mergeable_rx_bufs
= 0;
737 n
->promisc
= 1; /* for compatibility */
739 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
741 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
743 register_savevm("virtio-net", virtio_net_id
++, VIRTIO_NET_VM_VERSION
,
744 virtio_net_save
, virtio_net_load
, n
);
745 return (PCIDevice
*)n
;