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.
17 #include "net/checksum.h"
19 #include "qemu-error.h"
20 #include "qemu-timer.h"
21 #include "virtio-net.h"
22 #include "vhost_net.h"
24 #define VIRTIO_NET_VM_VERSION 11
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
];
43 uint32_t has_vnet_hdr
;
46 VirtQueueElement elem
;
49 int mergeable_rx_bufs
;
56 uint8_t vhost_started
;
57 VMChangeStateEntry
*vmstate
;
61 uint8_t multi_overflow
;
70 * - we could suppress RX interrupt if we were so inclined.
73 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
75 return (VirtIONet
*)vdev
;
78 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
80 VirtIONet
*n
= to_virtio_net(vdev
);
81 struct virtio_net_config netcfg
;
83 netcfg
.status
= n
->status
;
84 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
85 memcpy(config
, &netcfg
, sizeof(netcfg
));
88 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
90 VirtIONet
*n
= to_virtio_net(vdev
);
91 struct virtio_net_config netcfg
;
93 memcpy(&netcfg
, config
, sizeof(netcfg
));
95 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
96 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
97 qemu_format_nic_info_str(&n
->nic
->nc
, n
->mac
);
101 static void virtio_net_set_link_status(VLANClientState
*nc
)
103 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
104 uint16_t old_status
= n
->status
;
107 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
109 n
->status
|= VIRTIO_NET_S_LINK_UP
;
111 if (n
->status
!= old_status
)
112 virtio_notify_config(&n
->vdev
);
115 static void virtio_net_reset(VirtIODevice
*vdev
)
117 VirtIONet
*n
= to_virtio_net(vdev
);
119 /* Reset back to compatibility mode */
126 if (n
->vhost_started
) {
127 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
128 n
->vhost_started
= 0;
131 /* Flush any MAC and VLAN filter table state */
132 n
->mac_table
.in_use
= 0;
133 n
->mac_table
.first_multi
= 0;
134 n
->mac_table
.multi_overflow
= 0;
135 n
->mac_table
.uni_overflow
= 0;
136 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
137 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
140 static int peer_has_vnet_hdr(VirtIONet
*n
)
142 if (!n
->nic
->nc
.peer
)
145 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
)
148 n
->has_vnet_hdr
= tap_has_vnet_hdr(n
->nic
->nc
.peer
);
150 return n
->has_vnet_hdr
;
153 static int peer_has_ufo(VirtIONet
*n
)
155 if (!peer_has_vnet_hdr(n
))
158 n
->has_ufo
= tap_has_ufo(n
->nic
->nc
.peer
);
163 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
, uint32_t features
)
165 VirtIONet
*n
= to_virtio_net(vdev
);
167 features
|= (1 << VIRTIO_NET_F_MAC
);
169 if (peer_has_vnet_hdr(n
)) {
170 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
172 features
&= ~(0x1 << VIRTIO_NET_F_CSUM
);
173 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO4
);
174 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO6
);
175 features
&= ~(0x1 << VIRTIO_NET_F_HOST_ECN
);
177 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM
);
178 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4
);
179 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6
);
180 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_ECN
);
183 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
184 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_UFO
);
185 features
&= ~(0x1 << VIRTIO_NET_F_HOST_UFO
);
188 if (!n
->nic
->nc
.peer
||
189 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
192 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
195 return vhost_net_get_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
198 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
200 uint32_t features
= 0;
202 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
204 features
|= (1 << VIRTIO_NET_F_MAC
);
205 features
|= (1 << VIRTIO_NET_F_CSUM
);
206 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
207 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
208 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
213 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
215 VirtIONet
*n
= to_virtio_net(vdev
);
217 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
219 if (n
->has_vnet_hdr
) {
220 tap_set_offload(n
->nic
->nc
.peer
,
221 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
222 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
223 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
224 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
225 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
227 if (!n
->nic
->nc
.peer
||
228 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
231 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
234 vhost_net_ack_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
237 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
238 VirtQueueElement
*elem
)
242 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
243 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
247 on
= ldub_p(elem
->out_sg
[1].iov_base
);
249 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
251 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
253 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
255 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
257 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
259 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
262 return VIRTIO_NET_ERR
;
264 return VIRTIO_NET_OK
;
267 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
268 VirtQueueElement
*elem
)
270 struct virtio_net_ctrl_mac mac_data
;
272 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
273 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
274 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
275 return VIRTIO_NET_ERR
;
277 n
->mac_table
.in_use
= 0;
278 n
->mac_table
.first_multi
= 0;
279 n
->mac_table
.uni_overflow
= 0;
280 n
->mac_table
.multi_overflow
= 0;
281 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
283 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
285 if (sizeof(mac_data
.entries
) +
286 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
287 return VIRTIO_NET_ERR
;
289 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
290 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
291 mac_data
.entries
* ETH_ALEN
);
292 n
->mac_table
.in_use
+= mac_data
.entries
;
294 n
->mac_table
.uni_overflow
= 1;
297 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
299 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
301 if (sizeof(mac_data
.entries
) +
302 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
303 return VIRTIO_NET_ERR
;
305 if (mac_data
.entries
) {
306 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
307 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
308 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
309 mac_data
.entries
* ETH_ALEN
);
310 n
->mac_table
.in_use
+= mac_data
.entries
;
312 n
->mac_table
.multi_overflow
= 1;
316 return VIRTIO_NET_OK
;
319 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
320 VirtQueueElement
*elem
)
324 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
325 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
326 return VIRTIO_NET_ERR
;
329 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
332 return VIRTIO_NET_ERR
;
334 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
335 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
336 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
337 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
339 return VIRTIO_NET_ERR
;
341 return VIRTIO_NET_OK
;
344 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
346 VirtIONet
*n
= to_virtio_net(vdev
);
347 struct virtio_net_ctrl_hdr ctrl
;
348 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
349 VirtQueueElement elem
;
351 while (virtqueue_pop(vq
, &elem
)) {
352 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
353 fprintf(stderr
, "virtio-net ctrl missing headers\n");
357 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
358 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
359 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
363 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
364 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
366 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
367 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
368 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
369 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
370 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
371 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
373 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
375 virtqueue_push(vq
, &elem
, sizeof(status
));
376 virtio_notify(vdev
, vq
);
382 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
384 VirtIONet
*n
= to_virtio_net(vdev
);
386 qemu_flush_queued_packets(&n
->nic
->nc
);
388 /* We now have RX buffers, signal to the IO thread to break out of the
389 * select to re-poll the tap file descriptor */
393 static int virtio_net_can_receive(VLANClientState
*nc
)
395 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
397 if (!virtio_queue_ready(n
->rx_vq
) ||
398 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
404 static int virtio_net_has_buffers(VirtIONet
*n
, int bufsize
)
406 if (virtio_queue_empty(n
->rx_vq
) ||
407 (n
->mergeable_rx_bufs
&&
408 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
409 virtio_queue_set_notification(n
->rx_vq
, 1);
411 /* To avoid a race condition where the guest has made some buffers
412 * available after the above check but before notification was
413 * enabled, check for available buffers again.
415 if (virtio_queue_empty(n
->rx_vq
) ||
416 (n
->mergeable_rx_bufs
&&
417 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0)))
421 virtio_queue_set_notification(n
->rx_vq
, 0);
425 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
426 * it never finds out that the packets don't have valid checksums. This
427 * causes dhclient to get upset. Fedora's carried a patch for ages to
428 * fix this with Xen but it hasn't appeared in an upstream release of
431 * To avoid breaking existing guests, we catch udp packets and add
432 * checksums. This is terrible but it's better than hacking the guest
435 * N.B. if we introduce a zero-copy API, this operation is no longer free so
436 * we should provide a mechanism to disable it to avoid polluting the host
439 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
440 const uint8_t *buf
, size_t size
)
442 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
443 (size
> 27 && size
< 1500) && /* normal sized MTU */
444 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
445 (buf
[23] == 17) && /* ip.protocol == UDP */
446 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
447 /* FIXME this cast is evil */
448 net_checksum_calculate((uint8_t *)buf
, size
);
449 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
453 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
454 const void *buf
, size_t size
, size_t hdr_len
)
456 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
460 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
462 if (n
->has_vnet_hdr
) {
463 memcpy(hdr
, buf
, sizeof(*hdr
));
464 offset
= sizeof(*hdr
);
465 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
468 /* We only ever receive a struct virtio_net_hdr from the tapfd,
469 * but we may be passing along a larger header to the guest.
471 iov
[0].iov_base
+= hdr_len
;
472 iov
[0].iov_len
-= hdr_len
;
477 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
479 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
480 static const uint8_t vlan
[] = {0x81, 0x00};
481 uint8_t *ptr
= (uint8_t *)buf
;
487 if (n
->has_vnet_hdr
) {
488 ptr
+= sizeof(struct virtio_net_hdr
);
491 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
492 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
493 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
497 if (ptr
[0] & 1) { // multicast
498 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
500 } else if (n
->nomulti
) {
502 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
506 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
507 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
514 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
516 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
520 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
521 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
530 static ssize_t
virtio_net_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
532 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
533 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
534 size_t guest_hdr_len
, offset
, i
, host_hdr_len
;
536 if (!virtio_net_can_receive(&n
->nic
->nc
))
539 /* hdr_len refers to the header we supply to the guest */
540 guest_hdr_len
= n
->mergeable_rx_bufs
?
541 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
544 host_hdr_len
= n
->has_vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
545 if (!virtio_net_has_buffers(n
, size
+ guest_hdr_len
- host_hdr_len
))
548 if (!receive_filter(n
, buf
, size
))
553 while (offset
< size
) {
554 VirtQueueElement elem
;
556 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
560 if (virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
563 fprintf(stderr
, "virtio-net unexpected empty queue: "
564 "i %zd mergeable %d offset %zd, size %zd, "
565 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
566 i
, n
->mergeable_rx_bufs
, offset
, size
,
567 guest_hdr_len
, host_hdr_len
, n
->vdev
.guest_features
);
571 if (elem
.in_num
< 1) {
572 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
576 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= guest_hdr_len
) {
577 fprintf(stderr
, "virtio-net header not in first element\n");
581 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
584 if (n
->mergeable_rx_bufs
)
585 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
587 offset
+= receive_header(n
, sg
, elem
.in_num
,
588 buf
+ offset
, size
- offset
, guest_hdr_len
);
589 total
+= guest_hdr_len
;
592 /* copy in packet. ugh */
593 len
= iov_from_buf(sg
, elem
.in_num
,
594 buf
+ offset
, size
- offset
);
597 /* If buffers can't be merged, at this point we
598 * must have consumed the complete packet.
599 * Otherwise, drop it. */
600 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
602 fprintf(stderr
, "virtio-net truncated non-mergeable packet: "
604 "i %zd mergeable %d offset %zd, size %zd, "
605 "guest hdr len %zd, host hdr len %zd\n",
606 i
, n
->mergeable_rx_bufs
,
607 offset
, size
, guest_hdr_len
, host_hdr_len
);
612 /* signal other side */
613 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
617 mhdr
->num_buffers
= i
;
619 virtqueue_flush(n
->rx_vq
, i
);
620 virtio_notify(&n
->vdev
, n
->rx_vq
);
625 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
627 static void virtio_net_tx_complete(VLANClientState
*nc
, ssize_t len
)
629 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
631 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
632 virtio_notify(&n
->vdev
, n
->tx_vq
);
634 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
636 virtio_queue_set_notification(n
->tx_vq
, 1);
637 virtio_net_flush_tx(n
, n
->tx_vq
);
641 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
643 VirtQueueElement elem
;
644 int32_t num_packets
= 0;
646 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
650 if (n
->async_tx
.elem
.out_num
) {
651 virtio_queue_set_notification(n
->tx_vq
, 0);
655 while (virtqueue_pop(vq
, &elem
)) {
656 ssize_t ret
, len
= 0;
657 unsigned int out_num
= elem
.out_num
;
658 struct iovec
*out_sg
= &elem
.out_sg
[0];
661 /* hdr_len refers to the header received from the guest */
662 hdr_len
= n
->mergeable_rx_bufs
?
663 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
664 sizeof(struct virtio_net_hdr
);
666 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
667 fprintf(stderr
, "virtio-net header not in first element\n");
671 /* ignore the header if GSO is not supported */
672 if (!n
->has_vnet_hdr
) {
676 } else if (n
->mergeable_rx_bufs
) {
677 /* tapfd expects a struct virtio_net_hdr */
678 hdr_len
-= sizeof(struct virtio_net_hdr
);
679 out_sg
->iov_len
-= hdr_len
;
683 ret
= qemu_sendv_packet_async(&n
->nic
->nc
, out_sg
, out_num
,
684 virtio_net_tx_complete
);
686 virtio_queue_set_notification(n
->tx_vq
, 0);
687 n
->async_tx
.elem
= elem
;
688 n
->async_tx
.len
= len
;
694 virtqueue_push(vq
, &elem
, len
);
695 virtio_notify(&n
->vdev
, vq
);
697 if (++num_packets
>= n
->tx_burst
) {
704 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
706 VirtIONet
*n
= to_virtio_net(vdev
);
709 virtio_queue_set_notification(vq
, 1);
710 qemu_del_timer(n
->tx_timer
);
712 virtio_net_flush_tx(n
, vq
);
714 qemu_mod_timer(n
->tx_timer
,
715 qemu_get_clock(vm_clock
) + n
->tx_timeout
);
717 virtio_queue_set_notification(vq
, 0);
721 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
723 VirtIONet
*n
= to_virtio_net(vdev
);
725 if (unlikely(n
->tx_waiting
)) {
728 virtio_queue_set_notification(vq
, 0);
729 qemu_bh_schedule(n
->tx_bh
);
733 static void virtio_net_tx_timer(void *opaque
)
735 VirtIONet
*n
= opaque
;
739 /* Just in case the driver is not ready on more */
740 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
743 virtio_queue_set_notification(n
->tx_vq
, 1);
744 virtio_net_flush_tx(n
, n
->tx_vq
);
747 static void virtio_net_tx_bh(void *opaque
)
749 VirtIONet
*n
= opaque
;
754 /* Just in case the driver is not ready on more */
755 if (unlikely(!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)))
758 ret
= virtio_net_flush_tx(n
, n
->tx_vq
);
760 return; /* Notification re-enable handled by tx_complete */
763 /* If we flush a full burst of packets, assume there are
764 * more coming and immediately reschedule */
765 if (ret
>= n
->tx_burst
) {
766 qemu_bh_schedule(n
->tx_bh
);
771 /* If less than a full burst, re-enable notification and flush
772 * anything that may have come in while we weren't looking. If
773 * we find something, assume the guest is still active and reschedule */
774 virtio_queue_set_notification(n
->tx_vq
, 1);
775 if (virtio_net_flush_tx(n
, n
->tx_vq
) > 0) {
776 virtio_queue_set_notification(n
->tx_vq
, 0);
777 qemu_bh_schedule(n
->tx_bh
);
782 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
784 VirtIONet
*n
= opaque
;
786 if (n
->vhost_started
) {
787 /* TODO: should we really stop the backend?
788 * If we don't, it might keep writing to memory. */
789 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), &n
->vdev
);
790 n
->vhost_started
= 0;
792 virtio_save(&n
->vdev
, f
);
794 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
795 qemu_put_be32(f
, n
->tx_waiting
);
796 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
797 qemu_put_be16(f
, n
->status
);
798 qemu_put_byte(f
, n
->promisc
);
799 qemu_put_byte(f
, n
->allmulti
);
800 qemu_put_be32(f
, n
->mac_table
.in_use
);
801 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
802 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
803 qemu_put_be32(f
, n
->has_vnet_hdr
);
804 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
805 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
806 qemu_put_byte(f
, n
->alluni
);
807 qemu_put_byte(f
, n
->nomulti
);
808 qemu_put_byte(f
, n
->nouni
);
809 qemu_put_byte(f
, n
->nobcast
);
810 qemu_put_byte(f
, n
->has_ufo
);
813 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
815 VirtIONet
*n
= opaque
;
818 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
821 virtio_load(&n
->vdev
, f
);
823 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
824 n
->tx_waiting
= qemu_get_be32(f
);
825 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
828 n
->status
= qemu_get_be16(f
);
830 if (version_id
>= 4) {
831 if (version_id
< 8) {
832 n
->promisc
= qemu_get_be32(f
);
833 n
->allmulti
= qemu_get_be32(f
);
835 n
->promisc
= qemu_get_byte(f
);
836 n
->allmulti
= qemu_get_byte(f
);
840 if (version_id
>= 5) {
841 n
->mac_table
.in_use
= qemu_get_be32(f
);
842 /* MAC_TABLE_ENTRIES may be different from the saved image */
843 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
844 qemu_get_buffer(f
, n
->mac_table
.macs
,
845 n
->mac_table
.in_use
* ETH_ALEN
);
846 } else if (n
->mac_table
.in_use
) {
847 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
848 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
849 n
->mac_table
.in_use
= 0;
854 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
856 if (version_id
>= 7) {
857 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
858 error_report("virtio-net: saved image requires vnet_hdr=on");
862 if (n
->has_vnet_hdr
) {
863 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
864 tap_set_offload(n
->nic
->nc
.peer
,
865 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
866 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
867 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
868 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
869 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
873 if (version_id
>= 9) {
874 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
875 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
878 if (version_id
>= 10) {
879 n
->alluni
= qemu_get_byte(f
);
880 n
->nomulti
= qemu_get_byte(f
);
881 n
->nouni
= qemu_get_byte(f
);
882 n
->nobcast
= qemu_get_byte(f
);
885 if (version_id
>= 11) {
886 if (qemu_get_byte(f
) && !peer_has_ufo(n
)) {
887 error_report("virtio-net: saved image requires TUN_F_UFO support");
892 /* Find the first multicast entry in the saved MAC filter */
893 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
894 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
898 n
->mac_table
.first_multi
= i
;
902 qemu_mod_timer(n
->tx_timer
,
903 qemu_get_clock(vm_clock
) + n
->tx_timeout
);
905 qemu_bh_schedule(n
->tx_bh
);
911 static void virtio_net_cleanup(VLANClientState
*nc
)
913 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
918 static NetClientInfo net_virtio_info
= {
919 .type
= NET_CLIENT_TYPE_NIC
,
920 .size
= sizeof(NICState
),
921 .can_receive
= virtio_net_can_receive
,
922 .receive
= virtio_net_receive
,
923 .cleanup
= virtio_net_cleanup
,
924 .link_status_changed
= virtio_net_set_link_status
,
927 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
929 VirtIONet
*n
= to_virtio_net(vdev
);
930 if (!n
->nic
->nc
.peer
) {
933 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
937 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
940 if (!!n
->vhost_started
== !!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
943 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
944 int r
= vhost_net_start(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
946 fprintf(stderr
, "unable to start vhost net: %d: "
947 "falling back on userspace virtio\n", -r
);
949 n
->vhost_started
= 1;
952 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
953 n
->vhost_started
= 0;
957 static void virtio_net_vmstate_change(void *opaque
, int running
, int reason
)
959 VirtIONet
*n
= opaque
;
960 uint8_t status
= running
? VIRTIO_CONFIG_S_DRIVER_OK
: 0;
961 /* This is called when vm is started/stopped,
962 * it will start/stop vhost backend if * appropriate
963 * e.g. after migration. */
964 virtio_net_set_status(&n
->vdev
, n
->vdev
.status
& status
);
967 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
,
968 virtio_net_conf
*net
)
972 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
973 sizeof(struct virtio_net_config
),
976 n
->vdev
.get_config
= virtio_net_get_config
;
977 n
->vdev
.set_config
= virtio_net_set_config
;
978 n
->vdev
.get_features
= virtio_net_get_features
;
979 n
->vdev
.set_features
= virtio_net_set_features
;
980 n
->vdev
.bad_features
= virtio_net_bad_features
;
981 n
->vdev
.reset
= virtio_net_reset
;
982 n
->vdev
.set_status
= virtio_net_set_status
;
983 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
985 if (net
->tx
&& strcmp(net
->tx
, "timer") && strcmp(net
->tx
, "bh")) {
986 fprintf(stderr
, "virtio-net: "
987 "Unknown option tx=%s, valid options: \"timer\" \"bh\"\n",
989 fprintf(stderr
, "Defaulting to \"bh\"\n");
992 if (net
->tx
&& !strcmp(net
->tx
, "timer")) {
993 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx_timer
);
994 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
995 n
->tx_timeout
= net
->txtimer
;
997 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx_bh
);
998 n
->tx_bh
= qemu_bh_new(virtio_net_tx_bh
, n
);
1000 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
1001 qemu_macaddr_default_if_unset(&conf
->macaddr
);
1002 memcpy(&n
->mac
[0], &conf
->macaddr
, sizeof(n
->mac
));
1003 n
->status
= VIRTIO_NET_S_LINK_UP
;
1005 n
->nic
= qemu_new_nic(&net_virtio_info
, conf
, dev
->info
->name
, dev
->id
, n
);
1007 qemu_format_nic_info_str(&n
->nic
->nc
, conf
->macaddr
.a
);
1010 n
->tx_burst
= net
->txburst
;
1011 n
->mergeable_rx_bufs
= 0;
1012 n
->promisc
= 1; /* for compatibility */
1014 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1016 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
1019 register_savevm(dev
, "virtio-net", -1, VIRTIO_NET_VM_VERSION
,
1020 virtio_net_save
, virtio_net_load
, n
);
1021 n
->vmstate
= qemu_add_vm_change_state_handler(virtio_net_vmstate_change
, n
);
1026 void virtio_net_exit(VirtIODevice
*vdev
)
1028 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
1029 qemu_del_vm_change_state_handler(n
->vmstate
);
1031 if (n
->vhost_started
) {
1032 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
1035 qemu_purge_queued_packets(&n
->nic
->nc
);
1037 unregister_savevm(n
->qdev
, "virtio-net", n
);
1039 qemu_free(n
->mac_table
.macs
);
1040 qemu_free(n
->vlans
);
1043 qemu_del_timer(n
->tx_timer
);
1044 qemu_free_timer(n
->tx_timer
);
1046 qemu_bh_delete(n
->tx_bh
);
1049 virtio_cleanup(&n
->vdev
);
1050 qemu_del_vlan_client(&n
->nic
->nc
);