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
= n
->vc
->vlan
->first_client
;
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 /* Kernel can't actually handle UFO in software currently. */
160 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
162 uint32_t features
= 0;
164 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
166 features
|= (1 << VIRTIO_NET_F_MAC
);
167 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
168 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
169 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
170 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
172 return features
& virtio_net_get_features(vdev
);
175 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
177 VirtIONet
*n
= to_virtio_net(vdev
);
179 VLANClientState
*host
= n
->vc
->vlan
->first_client
;
182 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
185 if (!tap_has_vnet_hdr(host
) || !host
->set_offload
)
188 host
->set_offload(host
,
189 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
190 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
191 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
192 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1);
196 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
197 VirtQueueElement
*elem
)
201 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
202 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
206 on
= ldub_p(elem
->out_sg
[1].iov_base
);
208 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
210 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
212 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
214 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
216 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
218 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
221 return VIRTIO_NET_ERR
;
223 return VIRTIO_NET_OK
;
226 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
227 VirtQueueElement
*elem
)
229 struct virtio_net_ctrl_mac mac_data
;
231 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
232 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
233 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
234 return VIRTIO_NET_ERR
;
236 n
->mac_table
.in_use
= 0;
237 n
->mac_table
.first_multi
= 0;
238 n
->mac_table
.uni_overflow
= 0;
239 n
->mac_table
.multi_overflow
= 0;
240 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
242 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
244 if (sizeof(mac_data
.entries
) +
245 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
246 return VIRTIO_NET_ERR
;
248 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
249 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
250 mac_data
.entries
* ETH_ALEN
);
251 n
->mac_table
.in_use
+= mac_data
.entries
;
253 n
->mac_table
.uni_overflow
= 1;
256 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
258 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
260 if (sizeof(mac_data
.entries
) +
261 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
262 return VIRTIO_NET_ERR
;
264 if (mac_data
.entries
) {
265 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
266 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
267 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
268 mac_data
.entries
* ETH_ALEN
);
269 n
->mac_table
.in_use
+= mac_data
.entries
;
271 n
->mac_table
.multi_overflow
= 1;
275 return VIRTIO_NET_OK
;
278 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
279 VirtQueueElement
*elem
)
283 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
284 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
285 return VIRTIO_NET_ERR
;
288 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
291 return VIRTIO_NET_ERR
;
293 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
294 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
295 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
296 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
298 return VIRTIO_NET_ERR
;
300 return VIRTIO_NET_OK
;
303 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
305 VirtIONet
*n
= to_virtio_net(vdev
);
306 struct virtio_net_ctrl_hdr ctrl
;
307 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
308 VirtQueueElement elem
;
310 while (virtqueue_pop(vq
, &elem
)) {
311 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
312 fprintf(stderr
, "virtio-net ctrl missing headers\n");
316 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
317 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
318 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
322 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
323 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
325 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
326 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
327 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
328 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
329 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
330 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
332 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
334 virtqueue_push(vq
, &elem
, sizeof(status
));
335 virtio_notify(vdev
, vq
);
341 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
343 VirtIONet
*n
= to_virtio_net(vdev
);
345 qemu_flush_queued_packets(n
->vc
);
347 /* We now have RX buffers, signal to the IO thread to break out of the
348 * select to re-poll the tap file descriptor */
352 static int do_virtio_net_can_receive(VirtIONet
*n
, int bufsize
)
354 if (!virtio_queue_ready(n
->rx_vq
) ||
355 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
358 if (virtio_queue_empty(n
->rx_vq
) ||
359 (n
->mergeable_rx_bufs
&&
360 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
361 virtio_queue_set_notification(n
->rx_vq
, 1);
365 virtio_queue_set_notification(n
->rx_vq
, 0);
369 static int virtio_net_can_receive(VLANClientState
*vc
)
371 VirtIONet
*n
= vc
->opaque
;
373 return do_virtio_net_can_receive(n
, VIRTIO_NET_MAX_BUFSIZE
);
377 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
378 * it never finds out that the packets don't have valid checksums. This
379 * causes dhclient to get upset. Fedora's carried a patch for ages to
380 * fix this with Xen but it hasn't appeared in an upstream release of
383 * To avoid breaking existing guests, we catch udp packets and add
384 * checksums. This is terrible but it's better than hacking the guest
387 * N.B. if we introduce a zero-copy API, this operation is no longer free so
388 * we should provide a mechanism to disable it to avoid polluting the host
391 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
392 const uint8_t *buf
, size_t size
)
394 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
395 (size
> 27 && size
< 1500) && /* normal sized MTU */
396 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
397 (buf
[23] == 17) && /* ip.protocol == UDP */
398 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
399 /* FIXME this cast is evil */
400 net_checksum_calculate((uint8_t *)buf
, size
);
401 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
406 static int iov_fill(struct iovec
*iov
, int iovcnt
, const void *buf
, int count
)
411 while (offset
< count
&& i
< iovcnt
) {
412 int len
= MIN(iov
[i
].iov_len
, count
- offset
);
413 memcpy(iov
[i
].iov_base
, buf
+ offset
, len
);
421 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
422 const void *buf
, size_t size
, size_t hdr_len
, int raw
)
424 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
428 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
431 if (tap_has_vnet_hdr(n
->vc
->vlan
->first_client
)) {
433 memcpy(hdr
, buf
, sizeof(*hdr
));
435 memset(hdr
, 0, sizeof(*hdr
));
437 offset
= sizeof(*hdr
);
438 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
442 /* We only ever receive a struct virtio_net_hdr from the tapfd,
443 * but we may be passing along a larger header to the guest.
445 iov
[0].iov_base
+= hdr_len
;
446 iov
[0].iov_len
-= hdr_len
;
451 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
453 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
454 static const uint8_t vlan
[] = {0x81, 0x00};
455 uint8_t *ptr
= (uint8_t *)buf
;
462 if (tap_has_vnet_hdr(n
->vc
->vlan
->first_client
))
463 ptr
+= sizeof(struct virtio_net_hdr
);
466 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
467 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
468 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
472 if (ptr
[0] & 1) { // multicast
473 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
475 } else if (n
->nomulti
) {
477 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
481 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
482 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
489 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
491 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
495 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
496 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
505 static ssize_t
virtio_net_receive2(VLANClientState
*vc
, const uint8_t *buf
, size_t size
, int raw
)
507 VirtIONet
*n
= vc
->opaque
;
508 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
509 size_t hdr_len
, offset
, i
;
511 if (!do_virtio_net_can_receive(n
, size
))
514 if (!receive_filter(n
, buf
, size
))
517 /* hdr_len refers to the header we supply to the guest */
518 hdr_len
= n
->mergeable_rx_bufs
?
519 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
523 while (offset
< size
) {
524 VirtQueueElement elem
;
526 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
530 if ((i
!= 0 && !n
->mergeable_rx_bufs
) ||
531 virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
534 fprintf(stderr
, "virtio-net truncating packet\n");
538 if (elem
.in_num
< 1) {
539 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
543 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= hdr_len
) {
544 fprintf(stderr
, "virtio-net header not in first element\n");
548 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
551 if (n
->mergeable_rx_bufs
)
552 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
554 offset
+= receive_header(n
, sg
, elem
.in_num
,
555 buf
+ offset
, size
- offset
, hdr_len
, raw
);
559 /* copy in packet. ugh */
560 len
= iov_fill(sg
, elem
.in_num
,
561 buf
+ offset
, size
- offset
);
564 /* signal other side */
565 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
571 mhdr
->num_buffers
= i
;
573 virtqueue_flush(n
->rx_vq
, i
);
574 virtio_notify(&n
->vdev
, n
->rx_vq
);
579 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
581 static void virtio_net_tx_complete(VLANClientState
*vc
, ssize_t len
)
583 VirtIONet
*n
= vc
->opaque
;
585 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
586 virtio_notify(&n
->vdev
, n
->tx_vq
);
588 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
590 virtio_queue_set_notification(n
->tx_vq
, 1);
591 virtio_net_flush_tx(n
, n
->tx_vq
);
594 static ssize_t
virtio_net_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
596 return virtio_net_receive2(vc
, buf
, size
, 0);
599 static ssize_t
virtio_net_receive_raw(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
601 return virtio_net_receive2(vc
, buf
, size
, 1);
605 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
607 VirtQueueElement elem
;
609 int has_vnet_hdr
= tap_has_vnet_hdr(n
->vc
->vlan
->first_client
);
611 int has_vnet_hdr
= 0;
614 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
617 if (n
->async_tx
.elem
.out_num
) {
618 virtio_queue_set_notification(n
->tx_vq
, 0);
622 while (virtqueue_pop(vq
, &elem
)) {
623 ssize_t ret
, len
= 0;
624 unsigned int out_num
= elem
.out_num
;
625 struct iovec
*out_sg
= &elem
.out_sg
[0];
628 /* hdr_len refers to the header received from the guest */
629 hdr_len
= n
->mergeable_rx_bufs
?
630 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
631 sizeof(struct virtio_net_hdr
);
633 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
634 fprintf(stderr
, "virtio-net header not in first element\n");
638 /* ignore the header if GSO is not supported */
643 } else if (n
->mergeable_rx_bufs
) {
644 /* tapfd expects a struct virtio_net_hdr */
645 hdr_len
-= sizeof(struct virtio_net_hdr
);
646 out_sg
->iov_len
-= hdr_len
;
650 ret
= qemu_sendv_packet_async(n
->vc
, out_sg
, out_num
,
651 virtio_net_tx_complete
);
653 virtio_queue_set_notification(n
->tx_vq
, 0);
654 n
->async_tx
.elem
= elem
;
655 n
->async_tx
.len
= len
;
661 virtqueue_push(vq
, &elem
, len
);
662 virtio_notify(&n
->vdev
, vq
);
666 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
668 VirtIONet
*n
= to_virtio_net(vdev
);
670 if (n
->tx_timer_active
) {
671 virtio_queue_set_notification(vq
, 1);
672 qemu_del_timer(n
->tx_timer
);
673 n
->tx_timer_active
= 0;
674 virtio_net_flush_tx(n
, vq
);
676 qemu_mod_timer(n
->tx_timer
,
677 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
678 n
->tx_timer_active
= 1;
679 virtio_queue_set_notification(vq
, 0);
683 static void virtio_net_tx_timer(void *opaque
)
685 VirtIONet
*n
= opaque
;
687 n
->tx_timer_active
= 0;
689 /* Just in case the driver is not ready on more */
690 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
693 virtio_queue_set_notification(n
->tx_vq
, 1);
694 virtio_net_flush_tx(n
, n
->tx_vq
);
697 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
699 VirtIONet
*n
= opaque
;
701 virtio_save(&n
->vdev
, f
);
703 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
704 qemu_put_be32(f
, n
->tx_timer_active
);
705 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
706 qemu_put_be16(f
, n
->status
);
707 qemu_put_byte(f
, n
->promisc
);
708 qemu_put_byte(f
, n
->allmulti
);
709 qemu_put_be32(f
, n
->mac_table
.in_use
);
710 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
711 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
714 qemu_put_be32(f
, tap_has_vnet_hdr(n
->vc
->vlan
->first_client
));
719 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
720 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
721 qemu_put_byte(f
, n
->alluni
);
722 qemu_put_byte(f
, n
->nomulti
);
723 qemu_put_byte(f
, n
->nouni
);
724 qemu_put_byte(f
, n
->nobcast
);
727 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
729 VirtIONet
*n
= opaque
;
732 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
735 virtio_load(&n
->vdev
, f
);
737 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
738 n
->tx_timer_active
= qemu_get_be32(f
);
739 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
742 n
->status
= qemu_get_be16(f
);
744 if (version_id
>= 4) {
745 if (version_id
< 8) {
746 n
->promisc
= qemu_get_be32(f
);
747 n
->allmulti
= qemu_get_be32(f
);
749 n
->promisc
= qemu_get_byte(f
);
750 n
->allmulti
= qemu_get_byte(f
);
754 if (version_id
>= 5) {
755 n
->mac_table
.in_use
= qemu_get_be32(f
);
756 /* MAC_TABLE_ENTRIES may be different from the saved image */
757 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
758 qemu_get_buffer(f
, n
->mac_table
.macs
,
759 n
->mac_table
.in_use
* ETH_ALEN
);
760 } else if (n
->mac_table
.in_use
) {
761 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
762 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
763 n
->mac_table
.in_use
= 0;
768 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
770 if (version_id
>= 7 && qemu_get_be32(f
)) {
772 tap_using_vnet_hdr(n
->vc
->vlan
->first_client
, 1);
775 "virtio-net: saved image requires vnet header support\n");
780 if (version_id
>= 9) {
781 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
782 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
785 if (version_id
>= 10) {
786 n
->alluni
= qemu_get_byte(f
);
787 n
->nomulti
= qemu_get_byte(f
);
788 n
->nouni
= qemu_get_byte(f
);
789 n
->nobcast
= qemu_get_byte(f
);
792 /* Find the first multicast entry in the saved MAC filter */
793 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
794 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
798 n
->mac_table
.first_multi
= i
;
800 if (n
->tx_timer_active
) {
801 qemu_mod_timer(n
->tx_timer
,
802 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
808 static void virtio_net_cleanup(VLANClientState
*vc
)
810 VirtIONet
*n
= vc
->opaque
;
812 qemu_purge_queued_packets(vc
);
814 unregister_savevm("virtio-net", n
);
816 qemu_free(n
->mac_table
.macs
);
819 qemu_del_timer(n
->tx_timer
);
820 qemu_free_timer(n
->tx_timer
);
822 virtio_cleanup(&n
->vdev
);
825 VirtIODevice
*virtio_net_init(DeviceState
*dev
)
828 static int virtio_net_id
;
830 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
831 sizeof(struct virtio_net_config
),
834 n
->vdev
.get_config
= virtio_net_get_config
;
835 n
->vdev
.set_config
= virtio_net_set_config
;
836 n
->vdev
.get_features
= virtio_net_get_features
;
837 n
->vdev
.set_features
= virtio_net_set_features
;
838 n
->vdev
.bad_features
= virtio_net_bad_features
;
839 n
->vdev
.reset
= virtio_net_reset
;
840 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
841 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx
);
842 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
843 qdev_get_macaddr(dev
, n
->mac
);
844 n
->status
= VIRTIO_NET_S_LINK_UP
;
845 n
->vc
= qdev_get_vlan_client(dev
,
846 virtio_net_can_receive
,
847 virtio_net_receive
, NULL
,
848 virtio_net_cleanup
, n
);
849 n
->vc
->link_status_changed
= virtio_net_set_link_status
;
850 n
->vc
->receive_raw
= virtio_net_receive_raw
;
852 qemu_format_nic_info_str(n
->vc
, n
->mac
);
854 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
855 n
->tx_timer_active
= 0;
856 n
->mergeable_rx_bufs
= 0;
857 n
->promisc
= 1; /* for compatibility */
859 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
861 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
862 if (dev
->nd
->nvectors
== NIC_NVECTORS_UNSPECIFIED
)
863 n
->vdev
.nvectors
= 3;
865 n
->vdev
.nvectors
= dev
->nd
->nvectors
;
867 register_savevm("virtio-net", virtio_net_id
++, VIRTIO_NET_VM_VERSION
,
868 virtio_net_save
, virtio_net_load
, n
);