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
;
60 uint8_t multi_overflow
;
69 * - we could suppress RX interrupt if we were so inclined.
72 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
74 return (VirtIONet
*)vdev
;
77 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
79 VirtIONet
*n
= to_virtio_net(vdev
);
80 struct virtio_net_config netcfg
;
82 stw_p(&netcfg
.status
, n
->status
);
83 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
84 memcpy(config
, &netcfg
, sizeof(netcfg
));
87 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
89 VirtIONet
*n
= to_virtio_net(vdev
);
90 struct virtio_net_config netcfg
;
92 memcpy(&netcfg
, config
, sizeof(netcfg
));
94 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
95 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
96 qemu_format_nic_info_str(&n
->nic
->nc
, n
->mac
);
100 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
102 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
103 (n
->status
& VIRTIO_NET_S_LINK_UP
) && n
->vdev
.vm_running
;
106 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
108 if (!n
->nic
->nc
.peer
) {
111 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
115 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
118 if (!!n
->vhost_started
== virtio_net_started(n
, status
) &&
119 !n
->nic
->nc
.peer
->link_down
) {
122 if (!n
->vhost_started
) {
124 if (!vhost_net_query(tap_get_vhost_net(n
->nic
->nc
.peer
), &n
->vdev
)) {
127 r
= vhost_net_start(tap_get_vhost_net(n
->nic
->nc
.peer
), &n
->vdev
);
129 error_report("unable to start vhost net: %d: "
130 "falling back on userspace virtio", -r
);
132 n
->vhost_started
= 1;
135 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), &n
->vdev
);
136 n
->vhost_started
= 0;
140 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
142 VirtIONet
*n
= to_virtio_net(vdev
);
144 virtio_net_vhost_status(n
, status
);
146 if (!n
->tx_waiting
) {
150 if (virtio_net_started(n
, status
) && !n
->vhost_started
) {
152 qemu_mod_timer(n
->tx_timer
,
153 qemu_get_clock_ns(vm_clock
) + n
->tx_timeout
);
155 qemu_bh_schedule(n
->tx_bh
);
159 qemu_del_timer(n
->tx_timer
);
161 qemu_bh_cancel(n
->tx_bh
);
166 static void virtio_net_set_link_status(VLANClientState
*nc
)
168 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
169 uint16_t old_status
= n
->status
;
172 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
174 n
->status
|= VIRTIO_NET_S_LINK_UP
;
176 if (n
->status
!= old_status
)
177 virtio_notify_config(&n
->vdev
);
179 virtio_net_set_status(&n
->vdev
, n
->vdev
.status
);
182 static void virtio_net_reset(VirtIODevice
*vdev
)
184 VirtIONet
*n
= to_virtio_net(vdev
);
186 /* Reset back to compatibility mode */
194 /* Flush any MAC and VLAN filter table state */
195 n
->mac_table
.in_use
= 0;
196 n
->mac_table
.first_multi
= 0;
197 n
->mac_table
.multi_overflow
= 0;
198 n
->mac_table
.uni_overflow
= 0;
199 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
200 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
203 static int peer_has_vnet_hdr(VirtIONet
*n
)
205 if (!n
->nic
->nc
.peer
)
208 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
)
211 n
->has_vnet_hdr
= tap_has_vnet_hdr(n
->nic
->nc
.peer
);
213 return n
->has_vnet_hdr
;
216 static int peer_has_ufo(VirtIONet
*n
)
218 if (!peer_has_vnet_hdr(n
))
221 n
->has_ufo
= tap_has_ufo(n
->nic
->nc
.peer
);
226 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
, uint32_t features
)
228 VirtIONet
*n
= to_virtio_net(vdev
);
230 features
|= (1 << VIRTIO_NET_F_MAC
);
232 if (peer_has_vnet_hdr(n
)) {
233 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
235 features
&= ~(0x1 << VIRTIO_NET_F_CSUM
);
236 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO4
);
237 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO6
);
238 features
&= ~(0x1 << VIRTIO_NET_F_HOST_ECN
);
240 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM
);
241 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4
);
242 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6
);
243 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_ECN
);
246 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
247 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_UFO
);
248 features
&= ~(0x1 << VIRTIO_NET_F_HOST_UFO
);
251 if (!n
->nic
->nc
.peer
||
252 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
255 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
258 return vhost_net_get_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
261 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
263 uint32_t features
= 0;
265 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
267 features
|= (1 << VIRTIO_NET_F_MAC
);
268 features
|= (1 << VIRTIO_NET_F_CSUM
);
269 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
270 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
271 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
276 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
278 VirtIONet
*n
= to_virtio_net(vdev
);
280 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
282 if (n
->has_vnet_hdr
) {
283 tap_set_offload(n
->nic
->nc
.peer
,
284 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
285 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
286 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
287 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
288 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
290 if (!n
->nic
->nc
.peer
||
291 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
294 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
297 vhost_net_ack_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
300 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
301 VirtQueueElement
*elem
)
305 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
306 error_report("virtio-net ctrl invalid rx mode command");
310 on
= ldub_p(elem
->out_sg
[1].iov_base
);
312 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
314 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
316 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
318 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
320 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
322 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
325 return VIRTIO_NET_ERR
;
327 return VIRTIO_NET_OK
;
330 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
331 VirtQueueElement
*elem
)
333 struct virtio_net_ctrl_mac mac_data
;
335 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
336 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
337 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
338 return VIRTIO_NET_ERR
;
340 n
->mac_table
.in_use
= 0;
341 n
->mac_table
.first_multi
= 0;
342 n
->mac_table
.uni_overflow
= 0;
343 n
->mac_table
.multi_overflow
= 0;
344 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
346 mac_data
.entries
= ldl_p(elem
->out_sg
[1].iov_base
);
348 if (sizeof(mac_data
.entries
) +
349 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
350 return VIRTIO_NET_ERR
;
352 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
353 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
354 mac_data
.entries
* ETH_ALEN
);
355 n
->mac_table
.in_use
+= mac_data
.entries
;
357 n
->mac_table
.uni_overflow
= 1;
360 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
362 mac_data
.entries
= ldl_p(elem
->out_sg
[2].iov_base
);
364 if (sizeof(mac_data
.entries
) +
365 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
366 return VIRTIO_NET_ERR
;
368 if (mac_data
.entries
) {
369 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
370 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
371 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
372 mac_data
.entries
* ETH_ALEN
);
373 n
->mac_table
.in_use
+= mac_data
.entries
;
375 n
->mac_table
.multi_overflow
= 1;
379 return VIRTIO_NET_OK
;
382 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
383 VirtQueueElement
*elem
)
387 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
388 error_report("virtio-net ctrl invalid vlan command");
389 return VIRTIO_NET_ERR
;
392 vid
= lduw_p(elem
->out_sg
[1].iov_base
);
395 return VIRTIO_NET_ERR
;
397 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
398 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
399 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
400 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
402 return VIRTIO_NET_ERR
;
404 return VIRTIO_NET_OK
;
407 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
409 VirtIONet
*n
= to_virtio_net(vdev
);
410 struct virtio_net_ctrl_hdr ctrl
;
411 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
412 VirtQueueElement elem
;
414 while (virtqueue_pop(vq
, &elem
)) {
415 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
416 error_report("virtio-net ctrl missing headers");
420 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
421 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
422 error_report("virtio-net ctrl header not in correct element");
426 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
427 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
429 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
430 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
431 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
432 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
433 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
434 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
436 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
438 virtqueue_push(vq
, &elem
, sizeof(status
));
439 virtio_notify(vdev
, vq
);
445 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
447 VirtIONet
*n
= to_virtio_net(vdev
);
449 qemu_flush_queued_packets(&n
->nic
->nc
);
451 /* We now have RX buffers, signal to the IO thread to break out of the
452 * select to re-poll the tap file descriptor */
456 static int virtio_net_can_receive(VLANClientState
*nc
)
458 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
459 if (!n
->vdev
.vm_running
) {
463 if (!virtio_queue_ready(n
->rx_vq
) ||
464 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
470 static int virtio_net_has_buffers(VirtIONet
*n
, int bufsize
)
472 if (virtio_queue_empty(n
->rx_vq
) ||
473 (n
->mergeable_rx_bufs
&&
474 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
475 virtio_queue_set_notification(n
->rx_vq
, 1);
477 /* To avoid a race condition where the guest has made some buffers
478 * available after the above check but before notification was
479 * enabled, check for available buffers again.
481 if (virtio_queue_empty(n
->rx_vq
) ||
482 (n
->mergeable_rx_bufs
&&
483 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0)))
487 virtio_queue_set_notification(n
->rx_vq
, 0);
491 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
492 * it never finds out that the packets don't have valid checksums. This
493 * causes dhclient to get upset. Fedora's carried a patch for ages to
494 * fix this with Xen but it hasn't appeared in an upstream release of
497 * To avoid breaking existing guests, we catch udp packets and add
498 * checksums. This is terrible but it's better than hacking the guest
501 * N.B. if we introduce a zero-copy API, this operation is no longer free so
502 * we should provide a mechanism to disable it to avoid polluting the host
505 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
506 const uint8_t *buf
, size_t size
)
508 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
509 (size
> 27 && size
< 1500) && /* normal sized MTU */
510 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
511 (buf
[23] == 17) && /* ip.protocol == UDP */
512 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
513 /* FIXME this cast is evil */
514 net_checksum_calculate((uint8_t *)buf
, size
);
515 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
519 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
520 const void *buf
, size_t size
, size_t hdr_len
)
522 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
526 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
528 if (n
->has_vnet_hdr
) {
529 memcpy(hdr
, buf
, sizeof(*hdr
));
530 offset
= sizeof(*hdr
);
531 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
534 /* We only ever receive a struct virtio_net_hdr from the tapfd,
535 * but we may be passing along a larger header to the guest.
537 iov
[0].iov_base
+= hdr_len
;
538 iov
[0].iov_len
-= hdr_len
;
543 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
545 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
546 static const uint8_t vlan
[] = {0x81, 0x00};
547 uint8_t *ptr
= (uint8_t *)buf
;
553 if (n
->has_vnet_hdr
) {
554 ptr
+= sizeof(struct virtio_net_hdr
);
557 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
558 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
559 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
563 if (ptr
[0] & 1) { // multicast
564 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
566 } else if (n
->nomulti
) {
568 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
572 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
573 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
580 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
582 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
586 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
587 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
596 static ssize_t
virtio_net_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
598 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
599 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
600 size_t guest_hdr_len
, offset
, i
, host_hdr_len
;
602 if (!virtio_net_can_receive(&n
->nic
->nc
))
605 /* hdr_len refers to the header we supply to the guest */
606 guest_hdr_len
= n
->mergeable_rx_bufs
?
607 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
610 host_hdr_len
= n
->has_vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
611 if (!virtio_net_has_buffers(n
, size
+ guest_hdr_len
- host_hdr_len
))
614 if (!receive_filter(n
, buf
, size
))
619 while (offset
< size
) {
620 VirtQueueElement elem
;
622 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
626 if (virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
629 error_report("virtio-net unexpected empty queue: "
630 "i %zd mergeable %d offset %zd, size %zd, "
631 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
632 i
, n
->mergeable_rx_bufs
, offset
, size
,
633 guest_hdr_len
, host_hdr_len
, n
->vdev
.guest_features
);
637 if (elem
.in_num
< 1) {
638 error_report("virtio-net receive queue contains no in buffers");
642 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= guest_hdr_len
) {
643 error_report("virtio-net header not in first element");
647 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
650 if (n
->mergeable_rx_bufs
)
651 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
653 offset
+= receive_header(n
, sg
, elem
.in_num
,
654 buf
+ offset
, size
- offset
, guest_hdr_len
);
655 total
+= guest_hdr_len
;
658 /* copy in packet. ugh */
659 len
= iov_from_buf(sg
, elem
.in_num
, 0,
660 buf
+ offset
, size
- offset
);
663 /* If buffers can't be merged, at this point we
664 * must have consumed the complete packet.
665 * Otherwise, drop it. */
666 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
668 error_report("virtio-net truncated non-mergeable packet: "
669 "i %zd mergeable %d offset %zd, size %zd, "
670 "guest hdr len %zd, host hdr len %zd",
671 i
, n
->mergeable_rx_bufs
,
672 offset
, size
, guest_hdr_len
, host_hdr_len
);
677 /* signal other side */
678 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
682 stw_p(&mhdr
->num_buffers
, i
);
685 virtqueue_flush(n
->rx_vq
, i
);
686 virtio_notify(&n
->vdev
, n
->rx_vq
);
691 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
693 static void virtio_net_tx_complete(VLANClientState
*nc
, ssize_t len
)
695 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
697 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
698 virtio_notify(&n
->vdev
, n
->tx_vq
);
700 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
702 virtio_queue_set_notification(n
->tx_vq
, 1);
703 virtio_net_flush_tx(n
, n
->tx_vq
);
707 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
709 VirtQueueElement elem
;
710 int32_t num_packets
= 0;
711 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
715 assert(n
->vdev
.vm_running
);
717 if (n
->async_tx
.elem
.out_num
) {
718 virtio_queue_set_notification(n
->tx_vq
, 0);
722 while (virtqueue_pop(vq
, &elem
)) {
723 ssize_t ret
, len
= 0;
724 unsigned int out_num
= elem
.out_num
;
725 struct iovec
*out_sg
= &elem
.out_sg
[0];
728 /* hdr_len refers to the header received from the guest */
729 hdr_len
= n
->mergeable_rx_bufs
?
730 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
731 sizeof(struct virtio_net_hdr
);
733 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
734 error_report("virtio-net header not in first element");
738 /* ignore the header if GSO is not supported */
739 if (!n
->has_vnet_hdr
) {
743 } else if (n
->mergeable_rx_bufs
) {
744 /* tapfd expects a struct virtio_net_hdr */
745 hdr_len
-= sizeof(struct virtio_net_hdr
);
746 out_sg
->iov_len
-= hdr_len
;
750 ret
= qemu_sendv_packet_async(&n
->nic
->nc
, out_sg
, out_num
,
751 virtio_net_tx_complete
);
753 virtio_queue_set_notification(n
->tx_vq
, 0);
754 n
->async_tx
.elem
= elem
;
755 n
->async_tx
.len
= len
;
761 virtqueue_push(vq
, &elem
, len
);
762 virtio_notify(&n
->vdev
, vq
);
764 if (++num_packets
>= n
->tx_burst
) {
771 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
773 VirtIONet
*n
= to_virtio_net(vdev
);
775 /* This happens when device was stopped but VCPU wasn't. */
776 if (!n
->vdev
.vm_running
) {
782 virtio_queue_set_notification(vq
, 1);
783 qemu_del_timer(n
->tx_timer
);
785 virtio_net_flush_tx(n
, vq
);
787 qemu_mod_timer(n
->tx_timer
,
788 qemu_get_clock_ns(vm_clock
) + n
->tx_timeout
);
790 virtio_queue_set_notification(vq
, 0);
794 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
796 VirtIONet
*n
= to_virtio_net(vdev
);
798 if (unlikely(n
->tx_waiting
)) {
802 /* This happens when device was stopped but VCPU wasn't. */
803 if (!n
->vdev
.vm_running
) {
806 virtio_queue_set_notification(vq
, 0);
807 qemu_bh_schedule(n
->tx_bh
);
810 static void virtio_net_tx_timer(void *opaque
)
812 VirtIONet
*n
= opaque
;
813 assert(n
->vdev
.vm_running
);
817 /* Just in case the driver is not ready on more */
818 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
821 virtio_queue_set_notification(n
->tx_vq
, 1);
822 virtio_net_flush_tx(n
, n
->tx_vq
);
825 static void virtio_net_tx_bh(void *opaque
)
827 VirtIONet
*n
= opaque
;
830 assert(n
->vdev
.vm_running
);
834 /* Just in case the driver is not ready on more */
835 if (unlikely(!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)))
838 ret
= virtio_net_flush_tx(n
, n
->tx_vq
);
840 return; /* Notification re-enable handled by tx_complete */
843 /* If we flush a full burst of packets, assume there are
844 * more coming and immediately reschedule */
845 if (ret
>= n
->tx_burst
) {
846 qemu_bh_schedule(n
->tx_bh
);
851 /* If less than a full burst, re-enable notification and flush
852 * anything that may have come in while we weren't looking. If
853 * we find something, assume the guest is still active and reschedule */
854 virtio_queue_set_notification(n
->tx_vq
, 1);
855 if (virtio_net_flush_tx(n
, n
->tx_vq
) > 0) {
856 virtio_queue_set_notification(n
->tx_vq
, 0);
857 qemu_bh_schedule(n
->tx_bh
);
862 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
864 VirtIONet
*n
= opaque
;
866 /* At this point, backend must be stopped, otherwise
867 * it might keep writing to memory. */
868 assert(!n
->vhost_started
);
869 virtio_save(&n
->vdev
, f
);
871 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
872 qemu_put_be32(f
, n
->tx_waiting
);
873 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
874 qemu_put_be16(f
, n
->status
);
875 qemu_put_byte(f
, n
->promisc
);
876 qemu_put_byte(f
, n
->allmulti
);
877 qemu_put_be32(f
, n
->mac_table
.in_use
);
878 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
879 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
880 qemu_put_be32(f
, n
->has_vnet_hdr
);
881 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
882 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
883 qemu_put_byte(f
, n
->alluni
);
884 qemu_put_byte(f
, n
->nomulti
);
885 qemu_put_byte(f
, n
->nouni
);
886 qemu_put_byte(f
, n
->nobcast
);
887 qemu_put_byte(f
, n
->has_ufo
);
890 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
892 VirtIONet
*n
= opaque
;
896 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
899 ret
= virtio_load(&n
->vdev
, f
);
904 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
905 n
->tx_waiting
= qemu_get_be32(f
);
906 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
909 n
->status
= qemu_get_be16(f
);
911 if (version_id
>= 4) {
912 if (version_id
< 8) {
913 n
->promisc
= qemu_get_be32(f
);
914 n
->allmulti
= qemu_get_be32(f
);
916 n
->promisc
= qemu_get_byte(f
);
917 n
->allmulti
= qemu_get_byte(f
);
921 if (version_id
>= 5) {
922 n
->mac_table
.in_use
= qemu_get_be32(f
);
923 /* MAC_TABLE_ENTRIES may be different from the saved image */
924 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
925 qemu_get_buffer(f
, n
->mac_table
.macs
,
926 n
->mac_table
.in_use
* ETH_ALEN
);
927 } else if (n
->mac_table
.in_use
) {
928 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
929 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
930 n
->mac_table
.in_use
= 0;
935 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
937 if (version_id
>= 7) {
938 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
939 error_report("virtio-net: saved image requires vnet_hdr=on");
943 if (n
->has_vnet_hdr
) {
944 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
945 tap_set_offload(n
->nic
->nc
.peer
,
946 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
947 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
948 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
949 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
950 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
954 if (version_id
>= 9) {
955 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
956 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
959 if (version_id
>= 10) {
960 n
->alluni
= qemu_get_byte(f
);
961 n
->nomulti
= qemu_get_byte(f
);
962 n
->nouni
= qemu_get_byte(f
);
963 n
->nobcast
= qemu_get_byte(f
);
966 if (version_id
>= 11) {
967 if (qemu_get_byte(f
) && !peer_has_ufo(n
)) {
968 error_report("virtio-net: saved image requires TUN_F_UFO support");
973 /* Find the first multicast entry in the saved MAC filter */
974 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
975 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
979 n
->mac_table
.first_multi
= i
;
983 static void virtio_net_cleanup(VLANClientState
*nc
)
985 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
990 static NetClientInfo net_virtio_info
= {
991 .type
= NET_CLIENT_TYPE_NIC
,
992 .size
= sizeof(NICState
),
993 .can_receive
= virtio_net_can_receive
,
994 .receive
= virtio_net_receive
,
995 .cleanup
= virtio_net_cleanup
,
996 .link_status_changed
= virtio_net_set_link_status
,
999 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
,
1000 virtio_net_conf
*net
)
1004 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
1005 sizeof(struct virtio_net_config
),
1008 n
->vdev
.get_config
= virtio_net_get_config
;
1009 n
->vdev
.set_config
= virtio_net_set_config
;
1010 n
->vdev
.get_features
= virtio_net_get_features
;
1011 n
->vdev
.set_features
= virtio_net_set_features
;
1012 n
->vdev
.bad_features
= virtio_net_bad_features
;
1013 n
->vdev
.reset
= virtio_net_reset
;
1014 n
->vdev
.set_status
= virtio_net_set_status
;
1015 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
1017 if (net
->tx
&& strcmp(net
->tx
, "timer") && strcmp(net
->tx
, "bh")) {
1018 error_report("virtio-net: "
1019 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1021 error_report("Defaulting to \"bh\"");
1024 if (net
->tx
&& !strcmp(net
->tx
, "timer")) {
1025 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx_timer
);
1026 n
->tx_timer
= qemu_new_timer_ns(vm_clock
, virtio_net_tx_timer
, n
);
1027 n
->tx_timeout
= net
->txtimer
;
1029 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx_bh
);
1030 n
->tx_bh
= qemu_bh_new(virtio_net_tx_bh
, n
);
1032 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
1033 qemu_macaddr_default_if_unset(&conf
->macaddr
);
1034 memcpy(&n
->mac
[0], &conf
->macaddr
, sizeof(n
->mac
));
1035 n
->status
= VIRTIO_NET_S_LINK_UP
;
1037 n
->nic
= qemu_new_nic(&net_virtio_info
, conf
, object_get_typename(OBJECT(dev
)), dev
->id
, n
);
1039 qemu_format_nic_info_str(&n
->nic
->nc
, conf
->macaddr
.a
);
1042 n
->tx_burst
= net
->txburst
;
1043 n
->mergeable_rx_bufs
= 0;
1044 n
->promisc
= 1; /* for compatibility */
1046 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1048 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
1051 register_savevm(dev
, "virtio-net", -1, VIRTIO_NET_VM_VERSION
,
1052 virtio_net_save
, virtio_net_load
, n
);
1054 add_boot_device_path(conf
->bootindex
, dev
, "/ethernet-phy@0");
1059 void virtio_net_exit(VirtIODevice
*vdev
)
1061 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
1063 /* This will stop vhost backend if appropriate. */
1064 virtio_net_set_status(vdev
, 0);
1066 qemu_purge_queued_packets(&n
->nic
->nc
);
1068 unregister_savevm(n
->qdev
, "virtio-net", n
);
1070 g_free(n
->mac_table
.macs
);
1074 qemu_del_timer(n
->tx_timer
);
1075 qemu_free_timer(n
->tx_timer
);
1077 qemu_bh_delete(n
->tx_bh
);
1080 qemu_del_vlan_client(&n
->nic
->nc
);
1081 virtio_cleanup(&n
->vdev
);