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"
19 #define VIRTIO_NET_VM_VERSION 10
21 #define MAC_TABLE_ENTRIES 64
22 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
24 typedef struct VirtIONet
27 uint8_t mac
[ETH_ALEN
];
35 uint32_t has_vnet_hdr
;
37 VirtQueueElement elem
;
40 int mergeable_rx_bufs
;
50 uint8_t multi_overflow
;
58 * - we could suppress RX interrupt if we were so inclined.
61 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
63 return (VirtIONet
*)vdev
;
66 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
68 VirtIONet
*n
= to_virtio_net(vdev
);
69 struct virtio_net_config netcfg
;
71 netcfg
.status
= n
->status
;
72 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
73 memcpy(config
, &netcfg
, sizeof(netcfg
));
76 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
78 VirtIONet
*n
= to_virtio_net(vdev
);
79 struct virtio_net_config netcfg
;
81 memcpy(&netcfg
, config
, sizeof(netcfg
));
83 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
84 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
85 qemu_format_nic_info_str(n
->vc
, n
->mac
);
89 static void virtio_net_set_link_status(VLANClientState
*vc
)
91 VirtIONet
*n
= vc
->opaque
;
92 uint16_t old_status
= n
->status
;
95 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
97 n
->status
|= VIRTIO_NET_S_LINK_UP
;
99 if (n
->status
!= old_status
)
100 virtio_notify_config(&n
->vdev
);
103 static void virtio_net_reset(VirtIODevice
*vdev
)
105 VirtIONet
*n
= to_virtio_net(vdev
);
107 /* Reset back to compatibility mode */
115 /* Flush any MAC and VLAN filter table state */
116 n
->mac_table
.in_use
= 0;
117 n
->mac_table
.first_multi
= 0;
118 n
->mac_table
.multi_overflow
= 0;
119 n
->mac_table
.uni_overflow
= 0;
120 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
121 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
124 static int peer_has_vnet_hdr(VirtIONet
*n
)
129 if (n
->vc
->peer
->type
!= NET_CLIENT_TYPE_TAP
)
132 n
->has_vnet_hdr
= tap_has_vnet_hdr(n
->vc
->peer
);
134 return n
->has_vnet_hdr
;
137 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
)
139 VirtIONet
*n
= to_virtio_net(vdev
);
140 uint32_t features
= (1 << VIRTIO_NET_F_MAC
) |
141 (1 << VIRTIO_NET_F_MRG_RXBUF
) |
142 (1 << VIRTIO_NET_F_STATUS
) |
143 (1 << VIRTIO_NET_F_CTRL_VQ
) |
144 (1 << VIRTIO_NET_F_CTRL_RX
) |
145 (1 << VIRTIO_NET_F_CTRL_VLAN
) |
146 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA
);
148 if (peer_has_vnet_hdr(n
)) {
149 tap_using_vnet_hdr(n
->vc
->peer
, 1);
151 features
|= (1 << VIRTIO_NET_F_CSUM
);
152 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
153 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
154 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
156 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
157 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
158 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
159 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
161 if (tap_has_ufo(n
->vc
->peer
)) {
162 features
|= (1 << VIRTIO_NET_F_GUEST_UFO
);
163 features
|= (1 << VIRTIO_NET_F_HOST_UFO
);
170 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
172 uint32_t features
= 0;
174 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
176 features
|= (1 << VIRTIO_NET_F_MAC
);
177 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
178 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
179 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
180 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
182 return features
& virtio_net_get_features(vdev
);
185 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
187 VirtIONet
*n
= to_virtio_net(vdev
);
188 VLANClientState
*host
= n
->vc
->peer
;
190 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
192 if (!n
->has_vnet_hdr
|| !host
->set_offload
)
195 host
->set_offload(host
,
196 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
197 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
198 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
199 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
200 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
203 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
204 VirtQueueElement
*elem
)
208 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
209 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
213 on
= ldub_p(elem
->out_sg
[1].iov_base
);
215 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
217 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
219 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
221 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
223 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
225 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
228 return VIRTIO_NET_ERR
;
230 return VIRTIO_NET_OK
;
233 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
234 VirtQueueElement
*elem
)
236 struct virtio_net_ctrl_mac mac_data
;
238 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
239 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
240 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
241 return VIRTIO_NET_ERR
;
243 n
->mac_table
.in_use
= 0;
244 n
->mac_table
.first_multi
= 0;
245 n
->mac_table
.uni_overflow
= 0;
246 n
->mac_table
.multi_overflow
= 0;
247 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
249 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
251 if (sizeof(mac_data
.entries
) +
252 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
253 return VIRTIO_NET_ERR
;
255 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
256 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
257 mac_data
.entries
* ETH_ALEN
);
258 n
->mac_table
.in_use
+= mac_data
.entries
;
260 n
->mac_table
.uni_overflow
= 1;
263 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
265 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
267 if (sizeof(mac_data
.entries
) +
268 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
269 return VIRTIO_NET_ERR
;
271 if (mac_data
.entries
) {
272 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
273 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
274 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
275 mac_data
.entries
* ETH_ALEN
);
276 n
->mac_table
.in_use
+= mac_data
.entries
;
278 n
->mac_table
.multi_overflow
= 1;
282 return VIRTIO_NET_OK
;
285 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
286 VirtQueueElement
*elem
)
290 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
291 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
292 return VIRTIO_NET_ERR
;
295 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
298 return VIRTIO_NET_ERR
;
300 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
301 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
302 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
303 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
305 return VIRTIO_NET_ERR
;
307 return VIRTIO_NET_OK
;
310 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
312 VirtIONet
*n
= to_virtio_net(vdev
);
313 struct virtio_net_ctrl_hdr ctrl
;
314 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
315 VirtQueueElement elem
;
317 while (virtqueue_pop(vq
, &elem
)) {
318 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
319 fprintf(stderr
, "virtio-net ctrl missing headers\n");
323 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
324 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
325 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
329 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
330 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
332 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
333 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
334 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
335 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
336 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
337 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
339 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
341 virtqueue_push(vq
, &elem
, sizeof(status
));
342 virtio_notify(vdev
, vq
);
348 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
350 VirtIONet
*n
= to_virtio_net(vdev
);
352 qemu_flush_queued_packets(n
->vc
);
354 /* We now have RX buffers, signal to the IO thread to break out of the
355 * select to re-poll the tap file descriptor */
359 static int do_virtio_net_can_receive(VirtIONet
*n
, int bufsize
)
361 if (!virtio_queue_ready(n
->rx_vq
) ||
362 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
365 if (virtio_queue_empty(n
->rx_vq
) ||
366 (n
->mergeable_rx_bufs
&&
367 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
368 virtio_queue_set_notification(n
->rx_vq
, 1);
372 virtio_queue_set_notification(n
->rx_vq
, 0);
376 static int virtio_net_can_receive(VLANClientState
*vc
)
378 VirtIONet
*n
= vc
->opaque
;
380 return do_virtio_net_can_receive(n
, VIRTIO_NET_MAX_BUFSIZE
);
383 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
384 * it never finds out that the packets don't have valid checksums. This
385 * causes dhclient to get upset. Fedora's carried a patch for ages to
386 * fix this with Xen but it hasn't appeared in an upstream release of
389 * To avoid breaking existing guests, we catch udp packets and add
390 * checksums. This is terrible but it's better than hacking the guest
393 * N.B. if we introduce a zero-copy API, this operation is no longer free so
394 * we should provide a mechanism to disable it to avoid polluting the host
397 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
398 const uint8_t *buf
, size_t size
)
400 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
401 (size
> 27 && size
< 1500) && /* normal sized MTU */
402 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
403 (buf
[23] == 17) && /* ip.protocol == UDP */
404 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
405 /* FIXME this cast is evil */
406 net_checksum_calculate((uint8_t *)buf
, size
);
407 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
411 static int iov_fill(struct iovec
*iov
, int iovcnt
, const void *buf
, int count
)
416 while (offset
< count
&& i
< iovcnt
) {
417 int len
= MIN(iov
[i
].iov_len
, count
- offset
);
418 memcpy(iov
[i
].iov_base
, buf
+ offset
, len
);
426 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
427 const void *buf
, size_t size
, size_t hdr_len
)
429 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
433 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
435 if (n
->has_vnet_hdr
) {
436 memcpy(hdr
, buf
, sizeof(*hdr
));
437 offset
= sizeof(*hdr
);
438 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
441 /* We only ever receive a struct virtio_net_hdr from the tapfd,
442 * but we may be passing along a larger header to the guest.
444 iov
[0].iov_base
+= hdr_len
;
445 iov
[0].iov_len
-= hdr_len
;
450 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
452 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
453 static const uint8_t vlan
[] = {0x81, 0x00};
454 uint8_t *ptr
= (uint8_t *)buf
;
460 if (n
->has_vnet_hdr
) {
461 ptr
+= sizeof(struct virtio_net_hdr
);
464 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
465 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
466 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
470 if (ptr
[0] & 1) { // multicast
471 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
473 } else if (n
->nomulti
) {
475 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
479 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
480 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
487 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
489 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
493 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
494 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
503 static ssize_t
virtio_net_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
505 VirtIONet
*n
= vc
->opaque
;
506 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
507 size_t hdr_len
, offset
, i
;
509 if (!do_virtio_net_can_receive(n
, size
))
512 if (!receive_filter(n
, buf
, size
))
515 /* hdr_len refers to the header we supply to the guest */
516 hdr_len
= n
->mergeable_rx_bufs
?
517 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
521 while (offset
< size
) {
522 VirtQueueElement elem
;
524 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
528 if ((i
!= 0 && !n
->mergeable_rx_bufs
) ||
529 virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
532 fprintf(stderr
, "virtio-net truncating packet\n");
536 if (elem
.in_num
< 1) {
537 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
541 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= hdr_len
) {
542 fprintf(stderr
, "virtio-net header not in first element\n");
546 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
549 if (n
->mergeable_rx_bufs
)
550 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
552 offset
+= receive_header(n
, sg
, elem
.in_num
,
553 buf
+ offset
, size
- offset
, hdr_len
);
557 /* copy in packet. ugh */
558 len
= iov_fill(sg
, elem
.in_num
,
559 buf
+ offset
, size
- offset
);
562 /* signal other side */
563 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
569 mhdr
->num_buffers
= i
;
571 virtqueue_flush(n
->rx_vq
, i
);
572 virtio_notify(&n
->vdev
, n
->rx_vq
);
577 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
579 static void virtio_net_tx_complete(VLANClientState
*vc
, ssize_t len
)
581 VirtIONet
*n
= vc
->opaque
;
583 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
584 virtio_notify(&n
->vdev
, n
->tx_vq
);
586 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
588 virtio_queue_set_notification(n
->tx_vq
, 1);
589 virtio_net_flush_tx(n
, n
->tx_vq
);
593 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
595 VirtQueueElement elem
;
597 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
600 if (n
->async_tx
.elem
.out_num
) {
601 virtio_queue_set_notification(n
->tx_vq
, 0);
605 while (virtqueue_pop(vq
, &elem
)) {
606 ssize_t ret
, len
= 0;
607 unsigned int out_num
= elem
.out_num
;
608 struct iovec
*out_sg
= &elem
.out_sg
[0];
611 /* hdr_len refers to the header received from the guest */
612 hdr_len
= n
->mergeable_rx_bufs
?
613 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
614 sizeof(struct virtio_net_hdr
);
616 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
617 fprintf(stderr
, "virtio-net header not in first element\n");
621 /* ignore the header if GSO is not supported */
622 if (!n
->has_vnet_hdr
) {
626 } else if (n
->mergeable_rx_bufs
) {
627 /* tapfd expects a struct virtio_net_hdr */
628 hdr_len
-= sizeof(struct virtio_net_hdr
);
629 out_sg
->iov_len
-= hdr_len
;
633 ret
= qemu_sendv_packet_async(n
->vc
, out_sg
, out_num
,
634 virtio_net_tx_complete
);
636 virtio_queue_set_notification(n
->tx_vq
, 0);
637 n
->async_tx
.elem
= elem
;
638 n
->async_tx
.len
= len
;
644 virtqueue_push(vq
, &elem
, len
);
645 virtio_notify(&n
->vdev
, vq
);
649 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
651 VirtIONet
*n
= to_virtio_net(vdev
);
653 if (n
->tx_timer_active
) {
654 virtio_queue_set_notification(vq
, 1);
655 qemu_del_timer(n
->tx_timer
);
656 n
->tx_timer_active
= 0;
657 virtio_net_flush_tx(n
, vq
);
659 qemu_mod_timer(n
->tx_timer
,
660 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
661 n
->tx_timer_active
= 1;
662 virtio_queue_set_notification(vq
, 0);
666 static void virtio_net_tx_timer(void *opaque
)
668 VirtIONet
*n
= opaque
;
670 n
->tx_timer_active
= 0;
672 /* Just in case the driver is not ready on more */
673 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
676 virtio_queue_set_notification(n
->tx_vq
, 1);
677 virtio_net_flush_tx(n
, n
->tx_vq
);
680 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
682 VirtIONet
*n
= opaque
;
684 virtio_save(&n
->vdev
, f
);
686 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
687 qemu_put_be32(f
, n
->tx_timer_active
);
688 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
689 qemu_put_be16(f
, n
->status
);
690 qemu_put_byte(f
, n
->promisc
);
691 qemu_put_byte(f
, n
->allmulti
);
692 qemu_put_be32(f
, n
->mac_table
.in_use
);
693 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
694 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
695 qemu_put_be32(f
, n
->has_vnet_hdr
);
696 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
697 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
698 qemu_put_byte(f
, n
->alluni
);
699 qemu_put_byte(f
, n
->nomulti
);
700 qemu_put_byte(f
, n
->nouni
);
701 qemu_put_byte(f
, n
->nobcast
);
704 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
706 VirtIONet
*n
= opaque
;
709 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
712 virtio_load(&n
->vdev
, f
);
714 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
715 n
->tx_timer_active
= qemu_get_be32(f
);
716 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
719 n
->status
= qemu_get_be16(f
);
721 if (version_id
>= 4) {
722 if (version_id
< 8) {
723 n
->promisc
= qemu_get_be32(f
);
724 n
->allmulti
= qemu_get_be32(f
);
726 n
->promisc
= qemu_get_byte(f
);
727 n
->allmulti
= qemu_get_byte(f
);
731 if (version_id
>= 5) {
732 n
->mac_table
.in_use
= qemu_get_be32(f
);
733 /* MAC_TABLE_ENTRIES may be different from the saved image */
734 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
735 qemu_get_buffer(f
, n
->mac_table
.macs
,
736 n
->mac_table
.in_use
* ETH_ALEN
);
737 } else if (n
->mac_table
.in_use
) {
738 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
739 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
740 n
->mac_table
.in_use
= 0;
745 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
747 if (version_id
>= 7) {
748 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
749 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
753 if (n
->has_vnet_hdr
) {
754 tap_using_vnet_hdr(n
->vc
->peer
, 1);
758 if (version_id
>= 9) {
759 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
760 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
763 if (version_id
>= 10) {
764 n
->alluni
= qemu_get_byte(f
);
765 n
->nomulti
= qemu_get_byte(f
);
766 n
->nouni
= qemu_get_byte(f
);
767 n
->nobcast
= qemu_get_byte(f
);
770 /* Find the first multicast entry in the saved MAC filter */
771 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
772 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
776 n
->mac_table
.first_multi
= i
;
778 if (n
->tx_timer_active
) {
779 qemu_mod_timer(n
->tx_timer
,
780 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
786 static void virtio_net_cleanup(VLANClientState
*vc
)
788 VirtIONet
*n
= vc
->opaque
;
793 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
)
796 static int virtio_net_id
;
798 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
799 sizeof(struct virtio_net_config
),
802 n
->vdev
.get_config
= virtio_net_get_config
;
803 n
->vdev
.set_config
= virtio_net_set_config
;
804 n
->vdev
.get_features
= virtio_net_get_features
;
805 n
->vdev
.set_features
= virtio_net_set_features
;
806 n
->vdev
.bad_features
= virtio_net_bad_features
;
807 n
->vdev
.reset
= virtio_net_reset
;
808 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
809 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx
);
810 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
811 qemu_macaddr_default_if_unset(&conf
->macaddr
);
812 n
->status
= VIRTIO_NET_S_LINK_UP
;
813 n
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_NIC
, conf
->vlan
, conf
->peer
,
814 dev
->info
->name
, dev
->id
,
815 virtio_net_can_receive
,
816 virtio_net_receive
, NULL
, NULL
,
817 virtio_net_cleanup
, n
);
818 n
->vc
->link_status_changed
= virtio_net_set_link_status
;
820 qemu_format_nic_info_str(n
->vc
, conf
->macaddr
.a
);
822 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
823 n
->tx_timer_active
= 0;
824 n
->mergeable_rx_bufs
= 0;
825 n
->promisc
= 1; /* for compatibility */
827 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
829 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
831 register_savevm("virtio-net", virtio_net_id
++, VIRTIO_NET_VM_VERSION
,
832 virtio_net_save
, virtio_net_load
, n
);
837 void virtio_net_exit(VirtIODevice
*vdev
)
839 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
841 qemu_purge_queued_packets(n
->vc
);
843 unregister_savevm("virtio-net", n
);
845 qemu_free(n
->mac_table
.macs
);
848 qemu_del_timer(n
->tx_timer
);
849 qemu_free_timer(n
->tx_timer
);
851 virtio_cleanup(&n
->vdev
);
852 qemu_del_vlan_client(n
->vc
);