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 #define VIRTIO_NET_VM_VERSION 10
26 #define MAC_TABLE_ENTRIES 64
27 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
29 typedef struct VirtIONet
32 uint8_t mac
[ETH_ALEN
];
41 VirtQueueElement elem
;
44 int mergeable_rx_bufs
;
54 uint8_t multi_overflow
;
62 * - we could suppress RX interrupt if we were so inclined.
65 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
67 return (VirtIONet
*)vdev
;
70 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
72 VirtIONet
*n
= to_virtio_net(vdev
);
73 struct virtio_net_config netcfg
;
75 netcfg
.status
= n
->status
;
76 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
77 memcpy(config
, &netcfg
, sizeof(netcfg
));
80 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
82 VirtIONet
*n
= to_virtio_net(vdev
);
83 struct virtio_net_config netcfg
;
85 memcpy(&netcfg
, config
, sizeof(netcfg
));
87 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
88 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
89 qemu_format_nic_info_str(n
->vc
, n
->mac
);
93 static void virtio_net_set_link_status(VLANClientState
*vc
)
95 VirtIONet
*n
= vc
->opaque
;
96 uint16_t old_status
= n
->status
;
99 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
101 n
->status
|= VIRTIO_NET_S_LINK_UP
;
103 if (n
->status
!= old_status
)
104 virtio_notify_config(&n
->vdev
);
107 static void virtio_net_reset(VirtIODevice
*vdev
)
109 VirtIONet
*n
= to_virtio_net(vdev
);
111 /* Reset back to compatibility mode */
119 /* Flush any MAC and VLAN filter table state */
120 n
->mac_table
.in_use
= 0;
121 n
->mac_table
.first_multi
= 0;
122 n
->mac_table
.multi_overflow
= 0;
123 n
->mac_table
.uni_overflow
= 0;
124 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
125 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
128 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
)
130 uint32_t features
= (1 << VIRTIO_NET_F_MAC
) |
131 (1 << VIRTIO_NET_F_MRG_RXBUF
) |
132 (1 << VIRTIO_NET_F_STATUS
) |
133 (1 << VIRTIO_NET_F_CTRL_VQ
) |
134 (1 << VIRTIO_NET_F_CTRL_RX
) |
135 (1 << VIRTIO_NET_F_CTRL_VLAN
) |
136 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA
);
139 VirtIONet
*n
= to_virtio_net(vdev
);
140 VLANClientState
*host
= QTAILQ_FIRST(&n
->vc
->vlan
->clients
);
142 if (tap_has_vnet_hdr(host
)) {
143 tap_using_vnet_hdr(host
, 1);
144 features
|= (1 << VIRTIO_NET_F_CSUM
);
145 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
146 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
147 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
148 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
149 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
150 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
151 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
152 features
|= (1 << VIRTIO_NET_F_MRG_RXBUF
);
153 if (tap_has_ufo(host
)) {
154 features
|= (1 << VIRTIO_NET_F_GUEST_UFO
);
155 features
|= (1 << VIRTIO_NET_F_HOST_UFO
);
163 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
165 uint32_t features
= 0;
167 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
169 features
|= (1 << VIRTIO_NET_F_MAC
);
170 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
171 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
172 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
173 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
175 return features
& virtio_net_get_features(vdev
);
178 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
180 VirtIONet
*n
= to_virtio_net(vdev
);
182 VLANClientState
*host
= QTAILQ_FIRST(&n
->vc
->vlan
->clients
);
185 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
188 if (!tap_has_vnet_hdr(host
) || !host
->set_offload
)
191 host
->set_offload(host
,
192 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
193 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
194 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
195 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
196 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
200 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
201 VirtQueueElement
*elem
)
205 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
206 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
210 on
= ldub_p(elem
->out_sg
[1].iov_base
);
212 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
214 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
216 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
218 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
220 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
222 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
225 return VIRTIO_NET_ERR
;
227 return VIRTIO_NET_OK
;
230 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
231 VirtQueueElement
*elem
)
233 struct virtio_net_ctrl_mac mac_data
;
235 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
236 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
237 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
238 return VIRTIO_NET_ERR
;
240 n
->mac_table
.in_use
= 0;
241 n
->mac_table
.first_multi
= 0;
242 n
->mac_table
.uni_overflow
= 0;
243 n
->mac_table
.multi_overflow
= 0;
244 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
246 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
248 if (sizeof(mac_data
.entries
) +
249 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
250 return VIRTIO_NET_ERR
;
252 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
253 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
254 mac_data
.entries
* ETH_ALEN
);
255 n
->mac_table
.in_use
+= mac_data
.entries
;
257 n
->mac_table
.uni_overflow
= 1;
260 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
262 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
264 if (sizeof(mac_data
.entries
) +
265 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
266 return VIRTIO_NET_ERR
;
268 if (mac_data
.entries
) {
269 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
270 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
271 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
272 mac_data
.entries
* ETH_ALEN
);
273 n
->mac_table
.in_use
+= mac_data
.entries
;
275 n
->mac_table
.multi_overflow
= 1;
279 return VIRTIO_NET_OK
;
282 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
283 VirtQueueElement
*elem
)
287 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
288 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
289 return VIRTIO_NET_ERR
;
292 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
295 return VIRTIO_NET_ERR
;
297 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
298 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
299 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
300 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
302 return VIRTIO_NET_ERR
;
304 return VIRTIO_NET_OK
;
307 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
309 VirtIONet
*n
= to_virtio_net(vdev
);
310 struct virtio_net_ctrl_hdr ctrl
;
311 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
312 VirtQueueElement elem
;
314 while (virtqueue_pop(vq
, &elem
)) {
315 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
316 fprintf(stderr
, "virtio-net ctrl missing headers\n");
320 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
321 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
322 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
326 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
327 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
329 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
330 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
331 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
332 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
333 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
334 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
336 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
338 virtqueue_push(vq
, &elem
, sizeof(status
));
339 virtio_notify(vdev
, vq
);
345 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
347 VirtIONet
*n
= to_virtio_net(vdev
);
349 qemu_flush_queued_packets(n
->vc
);
351 /* We now have RX buffers, signal to the IO thread to break out of the
352 * select to re-poll the tap file descriptor */
356 static int do_virtio_net_can_receive(VirtIONet
*n
, int bufsize
)
358 if (!virtio_queue_ready(n
->rx_vq
) ||
359 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
362 if (virtio_queue_empty(n
->rx_vq
) ||
363 (n
->mergeable_rx_bufs
&&
364 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
365 virtio_queue_set_notification(n
->rx_vq
, 1);
369 virtio_queue_set_notification(n
->rx_vq
, 0);
373 static int virtio_net_can_receive(VLANClientState
*vc
)
375 VirtIONet
*n
= vc
->opaque
;
377 return do_virtio_net_can_receive(n
, VIRTIO_NET_MAX_BUFSIZE
);
381 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
382 * it never finds out that the packets don't have valid checksums. This
383 * causes dhclient to get upset. Fedora's carried a patch for ages to
384 * fix this with Xen but it hasn't appeared in an upstream release of
387 * To avoid breaking existing guests, we catch udp packets and add
388 * checksums. This is terrible but it's better than hacking the guest
391 * N.B. if we introduce a zero-copy API, this operation is no longer free so
392 * we should provide a mechanism to disable it to avoid polluting the host
395 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
396 const uint8_t *buf
, size_t size
)
398 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
399 (size
> 27 && size
< 1500) && /* normal sized MTU */
400 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
401 (buf
[23] == 17) && /* ip.protocol == UDP */
402 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
403 /* FIXME this cast is evil */
404 net_checksum_calculate((uint8_t *)buf
, size
);
405 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
410 static int iov_fill(struct iovec
*iov
, int iovcnt
, const void *buf
, int count
)
415 while (offset
< count
&& i
< iovcnt
) {
416 int len
= MIN(iov
[i
].iov_len
, count
- offset
);
417 memcpy(iov
[i
].iov_base
, buf
+ offset
, len
);
425 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
426 const void *buf
, size_t size
, size_t hdr_len
, int raw
)
428 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
432 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
435 if (tap_has_vnet_hdr(QTAILQ_FIRST(&n
->vc
->vlan
->clients
))) {
437 memcpy(hdr
, buf
, sizeof(*hdr
));
439 memset(hdr
, 0, sizeof(*hdr
));
441 offset
= sizeof(*hdr
);
442 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
446 /* We only ever receive a struct virtio_net_hdr from the tapfd,
447 * but we may be passing along a larger header to the guest.
449 iov
[0].iov_base
+= hdr_len
;
450 iov
[0].iov_len
-= hdr_len
;
455 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
457 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
458 static const uint8_t vlan
[] = {0x81, 0x00};
459 uint8_t *ptr
= (uint8_t *)buf
;
466 if (tap_has_vnet_hdr(QTAILQ_FIRST(&n
->vc
->vlan
->clients
)))
467 ptr
+= sizeof(struct virtio_net_hdr
);
470 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
471 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
472 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
476 if (ptr
[0] & 1) { // multicast
477 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
479 } else if (n
->nomulti
) {
481 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
485 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
486 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
493 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
495 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
499 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
500 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
509 static ssize_t
virtio_net_receive2(VLANClientState
*vc
, const uint8_t *buf
, size_t size
, int raw
)
511 VirtIONet
*n
= vc
->opaque
;
512 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
513 size_t hdr_len
, offset
, i
;
515 if (!do_virtio_net_can_receive(n
, size
))
518 if (!receive_filter(n
, buf
, size
))
521 /* hdr_len refers to the header we supply to the guest */
522 hdr_len
= n
->mergeable_rx_bufs
?
523 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
527 while (offset
< size
) {
528 VirtQueueElement elem
;
530 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
534 if ((i
!= 0 && !n
->mergeable_rx_bufs
) ||
535 virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
538 fprintf(stderr
, "virtio-net truncating packet\n");
542 if (elem
.in_num
< 1) {
543 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
547 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= hdr_len
) {
548 fprintf(stderr
, "virtio-net header not in first element\n");
552 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
555 if (n
->mergeable_rx_bufs
)
556 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
558 offset
+= receive_header(n
, sg
, elem
.in_num
,
559 buf
+ offset
, size
- offset
, hdr_len
, raw
);
563 /* copy in packet. ugh */
564 len
= iov_fill(sg
, elem
.in_num
,
565 buf
+ offset
, size
- offset
);
568 /* signal other side */
569 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
575 mhdr
->num_buffers
= i
;
577 virtqueue_flush(n
->rx_vq
, i
);
578 virtio_notify(&n
->vdev
, n
->rx_vq
);
583 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
585 static void virtio_net_tx_complete(VLANClientState
*vc
, ssize_t len
)
587 VirtIONet
*n
= vc
->opaque
;
589 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
590 virtio_notify(&n
->vdev
, n
->tx_vq
);
592 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
594 virtio_queue_set_notification(n
->tx_vq
, 1);
595 virtio_net_flush_tx(n
, n
->tx_vq
);
598 static ssize_t
virtio_net_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
600 return virtio_net_receive2(vc
, buf
, size
, 0);
603 static ssize_t
virtio_net_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
605 return virtio_net_receive2(vc
, buf
, size
, 1);
609 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
611 VirtQueueElement elem
;
613 int has_vnet_hdr
= tap_has_vnet_hdr(QTAILQ_FIRST(&n
->vc
->vlan
->clients
));
615 int has_vnet_hdr
= 0;
618 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
621 if (n
->async_tx
.elem
.out_num
) {
622 virtio_queue_set_notification(n
->tx_vq
, 0);
626 while (virtqueue_pop(vq
, &elem
)) {
627 ssize_t ret
, len
= 0;
628 unsigned int out_num
= elem
.out_num
;
629 struct iovec
*out_sg
= &elem
.out_sg
[0];
632 /* hdr_len refers to the header received from the guest */
633 hdr_len
= n
->mergeable_rx_bufs
?
634 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
635 sizeof(struct virtio_net_hdr
);
637 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
638 fprintf(stderr
, "virtio-net header not in first element\n");
642 /* ignore the header if GSO is not supported */
647 } else if (n
->mergeable_rx_bufs
) {
648 /* tapfd expects a struct virtio_net_hdr */
649 hdr_len
-= sizeof(struct virtio_net_hdr
);
650 out_sg
->iov_len
-= hdr_len
;
654 ret
= qemu_sendv_packet_async(n
->vc
, out_sg
, out_num
,
655 virtio_net_tx_complete
);
657 virtio_queue_set_notification(n
->tx_vq
, 0);
658 n
->async_tx
.elem
= elem
;
659 n
->async_tx
.len
= len
;
665 virtqueue_push(vq
, &elem
, len
);
666 virtio_notify(&n
->vdev
, vq
);
670 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
672 VirtIONet
*n
= to_virtio_net(vdev
);
674 if (n
->tx_timer_active
) {
675 virtio_queue_set_notification(vq
, 1);
676 qemu_del_timer(n
->tx_timer
);
677 n
->tx_timer_active
= 0;
678 virtio_net_flush_tx(n
, vq
);
680 qemu_mod_timer(n
->tx_timer
,
681 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
682 n
->tx_timer_active
= 1;
683 virtio_queue_set_notification(vq
, 0);
687 static void virtio_net_tx_timer(void *opaque
)
689 VirtIONet
*n
= opaque
;
691 n
->tx_timer_active
= 0;
693 /* Just in case the driver is not ready on more */
694 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
697 virtio_queue_set_notification(n
->tx_vq
, 1);
698 virtio_net_flush_tx(n
, n
->tx_vq
);
701 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
703 VirtIONet
*n
= opaque
;
705 virtio_save(&n
->vdev
, f
);
707 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
708 qemu_put_be32(f
, n
->tx_timer_active
);
709 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
710 qemu_put_be16(f
, n
->status
);
711 qemu_put_byte(f
, n
->promisc
);
712 qemu_put_byte(f
, n
->allmulti
);
713 qemu_put_be32(f
, n
->mac_table
.in_use
);
714 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
715 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
718 qemu_put_be32(f
, tap_has_vnet_hdr(QTAILQ_FIRST(&n
->vc
->vlan
->clients
)));
723 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
724 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
725 qemu_put_byte(f
, n
->alluni
);
726 qemu_put_byte(f
, n
->nomulti
);
727 qemu_put_byte(f
, n
->nouni
);
728 qemu_put_byte(f
, n
->nobcast
);
731 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
733 VirtIONet
*n
= opaque
;
736 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
739 virtio_load(&n
->vdev
, f
);
741 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
742 n
->tx_timer_active
= qemu_get_be32(f
);
743 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
746 n
->status
= qemu_get_be16(f
);
748 if (version_id
>= 4) {
749 if (version_id
< 8) {
750 n
->promisc
= qemu_get_be32(f
);
751 n
->allmulti
= qemu_get_be32(f
);
753 n
->promisc
= qemu_get_byte(f
);
754 n
->allmulti
= qemu_get_byte(f
);
758 if (version_id
>= 5) {
759 n
->mac_table
.in_use
= qemu_get_be32(f
);
760 /* MAC_TABLE_ENTRIES may be different from the saved image */
761 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
762 qemu_get_buffer(f
, n
->mac_table
.macs
,
763 n
->mac_table
.in_use
* ETH_ALEN
);
764 } else if (n
->mac_table
.in_use
) {
765 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
766 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
767 n
->mac_table
.in_use
= 0;
772 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
774 if (version_id
>= 7 && qemu_get_be32(f
)) {
776 tap_using_vnet_hdr(QTAILQ_FIRST(&n
->vc
->vlan
->clients
), 1);
779 "virtio-net: saved image requires vnet header support\n");
784 if (version_id
>= 9) {
785 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
786 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
789 if (version_id
>= 10) {
790 n
->alluni
= qemu_get_byte(f
);
791 n
->nomulti
= qemu_get_byte(f
);
792 n
->nouni
= qemu_get_byte(f
);
793 n
->nobcast
= qemu_get_byte(f
);
796 /* Find the first multicast entry in the saved MAC filter */
797 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
798 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
802 n
->mac_table
.first_multi
= i
;
804 if (n
->tx_timer_active
) {
805 qemu_mod_timer(n
->tx_timer
,
806 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
812 static void virtio_net_cleanup(VLANClientState
*vc
)
814 VirtIONet
*n
= vc
->opaque
;
819 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
)
822 static int virtio_net_id
;
824 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
825 sizeof(struct virtio_net_config
),
828 n
->vdev
.get_config
= virtio_net_get_config
;
829 n
->vdev
.set_config
= virtio_net_set_config
;
830 n
->vdev
.get_features
= virtio_net_get_features
;
831 n
->vdev
.set_features
= virtio_net_set_features
;
832 n
->vdev
.bad_features
= virtio_net_bad_features
;
833 n
->vdev
.reset
= virtio_net_reset
;
834 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
835 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx
);
836 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
837 qemu_macaddr_default_if_unset(&conf
->macaddr
);
838 n
->status
= VIRTIO_NET_S_LINK_UP
;
839 n
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_NIC
, conf
->vlan
, conf
->peer
,
840 dev
->info
->name
, dev
->id
,
841 virtio_net_can_receive
,
842 virtio_net_receive
, NULL
,
843 virtio_net_cleanup
, n
);
844 n
->vc
->link_status_changed
= virtio_net_set_link_status
;
845 n
->vc
->receive_raw
= virtio_net_receive_raw
;
847 qemu_format_nic_info_str(n
->vc
, conf
->macaddr
.a
);
849 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
850 n
->tx_timer_active
= 0;
851 n
->mergeable_rx_bufs
= 0;
852 n
->promisc
= 1; /* for compatibility */
854 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
856 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
858 register_savevm("virtio-net", virtio_net_id
++, VIRTIO_NET_VM_VERSION
,
859 virtio_net_save
, virtio_net_load
, n
);
864 void virtio_net_exit(VirtIODevice
*vdev
)
866 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
868 qemu_purge_queued_packets(n
->vc
);
870 unregister_savevm("virtio-net", n
);
872 qemu_free(n
->mac_table
.macs
);
875 qemu_del_timer(n
->tx_timer
);
876 qemu_free_timer(n
->tx_timer
);
878 virtio_cleanup(&n
->vdev
);
879 qemu_del_vlan_client(n
->vc
);