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
];
42 uint32_t has_vnet_hdr
;
45 VirtQueueElement elem
;
48 int mergeable_rx_bufs
;
55 uint8_t vhost_started
;
56 VMChangeStateEntry
*vmstate
;
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 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 void virtio_net_set_link_status(VLANClientState
*nc
)
102 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
103 uint16_t old_status
= n
->status
;
106 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
108 n
->status
|= VIRTIO_NET_S_LINK_UP
;
110 if (n
->status
!= old_status
)
111 virtio_notify_config(&n
->vdev
);
114 static void virtio_net_reset(VirtIODevice
*vdev
)
116 VirtIONet
*n
= to_virtio_net(vdev
);
118 /* Reset back to compatibility mode */
125 if (n
->vhost_started
) {
126 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
127 n
->vhost_started
= 0;
130 /* Flush any MAC and VLAN filter table state */
131 n
->mac_table
.in_use
= 0;
132 n
->mac_table
.first_multi
= 0;
133 n
->mac_table
.multi_overflow
= 0;
134 n
->mac_table
.uni_overflow
= 0;
135 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
136 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
139 static int peer_has_vnet_hdr(VirtIONet
*n
)
141 if (!n
->nic
->nc
.peer
)
144 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
)
147 n
->has_vnet_hdr
= tap_has_vnet_hdr(n
->nic
->nc
.peer
);
149 return n
->has_vnet_hdr
;
152 static int peer_has_ufo(VirtIONet
*n
)
154 if (!peer_has_vnet_hdr(n
))
157 n
->has_ufo
= tap_has_ufo(n
->nic
->nc
.peer
);
162 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
, uint32_t features
)
164 VirtIONet
*n
= to_virtio_net(vdev
);
166 features
|= (1 << VIRTIO_NET_F_MAC
);
168 if (peer_has_vnet_hdr(n
)) {
169 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
171 features
&= ~(0x1 << VIRTIO_NET_F_CSUM
);
172 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO4
);
173 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO6
);
174 features
&= ~(0x1 << VIRTIO_NET_F_HOST_ECN
);
176 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM
);
177 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4
);
178 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6
);
179 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_ECN
);
182 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
183 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_UFO
);
184 features
&= ~(0x1 << VIRTIO_NET_F_HOST_UFO
);
187 if (!n
->nic
->nc
.peer
||
188 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
191 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
194 return vhost_net_get_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
197 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
199 uint32_t features
= 0;
201 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
203 features
|= (1 << VIRTIO_NET_F_MAC
);
204 features
|= (1 << VIRTIO_NET_F_CSUM
);
205 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
206 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
207 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
212 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
214 VirtIONet
*n
= to_virtio_net(vdev
);
216 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
218 if (n
->has_vnet_hdr
) {
219 tap_set_offload(n
->nic
->nc
.peer
,
220 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
221 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
222 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
223 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
224 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
226 if (!n
->nic
->nc
.peer
||
227 n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
230 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
233 vhost_net_ack_features(tap_get_vhost_net(n
->nic
->nc
.peer
), features
);
236 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
237 VirtQueueElement
*elem
)
241 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(on
)) {
242 fprintf(stderr
, "virtio-net ctrl invalid rx mode command\n");
246 on
= ldub_p(elem
->out_sg
[1].iov_base
);
248 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
250 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
252 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLUNI
)
254 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOMULTI
)
256 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOUNI
)
258 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_NOBCAST
)
261 return VIRTIO_NET_ERR
;
263 return VIRTIO_NET_OK
;
266 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
267 VirtQueueElement
*elem
)
269 struct virtio_net_ctrl_mac mac_data
;
271 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
|| elem
->out_num
!= 3 ||
272 elem
->out_sg
[1].iov_len
< sizeof(mac_data
) ||
273 elem
->out_sg
[2].iov_len
< sizeof(mac_data
))
274 return VIRTIO_NET_ERR
;
276 n
->mac_table
.in_use
= 0;
277 n
->mac_table
.first_multi
= 0;
278 n
->mac_table
.uni_overflow
= 0;
279 n
->mac_table
.multi_overflow
= 0;
280 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
282 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
284 if (sizeof(mac_data
.entries
) +
285 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
286 return VIRTIO_NET_ERR
;
288 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
289 memcpy(n
->mac_table
.macs
, elem
->out_sg
[1].iov_base
+ sizeof(mac_data
),
290 mac_data
.entries
* ETH_ALEN
);
291 n
->mac_table
.in_use
+= mac_data
.entries
;
293 n
->mac_table
.uni_overflow
= 1;
296 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
298 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
300 if (sizeof(mac_data
.entries
) +
301 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
302 return VIRTIO_NET_ERR
;
304 if (mac_data
.entries
) {
305 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
306 memcpy(n
->mac_table
.macs
+ (n
->mac_table
.in_use
* ETH_ALEN
),
307 elem
->out_sg
[2].iov_base
+ sizeof(mac_data
),
308 mac_data
.entries
* ETH_ALEN
);
309 n
->mac_table
.in_use
+= mac_data
.entries
;
311 n
->mac_table
.multi_overflow
= 1;
315 return VIRTIO_NET_OK
;
318 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
319 VirtQueueElement
*elem
)
323 if (elem
->out_num
!= 2 || elem
->out_sg
[1].iov_len
!= sizeof(vid
)) {
324 fprintf(stderr
, "virtio-net ctrl invalid vlan command\n");
325 return VIRTIO_NET_ERR
;
328 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
331 return VIRTIO_NET_ERR
;
333 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
334 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
335 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
336 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
338 return VIRTIO_NET_ERR
;
340 return VIRTIO_NET_OK
;
343 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
345 VirtIONet
*n
= to_virtio_net(vdev
);
346 struct virtio_net_ctrl_hdr ctrl
;
347 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
348 VirtQueueElement elem
;
350 while (virtqueue_pop(vq
, &elem
)) {
351 if ((elem
.in_num
< 1) || (elem
.out_num
< 1)) {
352 fprintf(stderr
, "virtio-net ctrl missing headers\n");
356 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
357 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
358 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
362 ctrl
.class = ldub_p(elem
.out_sg
[0].iov_base
);
363 ctrl
.cmd
= ldub_p(elem
.out_sg
[0].iov_base
+ sizeof(ctrl
.class));
365 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
366 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
367 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
368 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
369 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
370 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
372 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
374 virtqueue_push(vq
, &elem
, sizeof(status
));
375 virtio_notify(vdev
, vq
);
381 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
383 VirtIONet
*n
= to_virtio_net(vdev
);
385 qemu_flush_queued_packets(&n
->nic
->nc
);
387 /* We now have RX buffers, signal to the IO thread to break out of the
388 * select to re-poll the tap file descriptor */
392 static int virtio_net_can_receive(VLANClientState
*nc
)
394 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
396 if (!virtio_queue_ready(n
->rx_vq
) ||
397 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
403 static int virtio_net_has_buffers(VirtIONet
*n
, int bufsize
)
405 if (virtio_queue_empty(n
->rx_vq
) ||
406 (n
->mergeable_rx_bufs
&&
407 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0))) {
408 virtio_queue_set_notification(n
->rx_vq
, 1);
410 /* To avoid a race condition where the guest has made some buffers
411 * available after the above check but before notification was
412 * enabled, check for available buffers again.
414 if (virtio_queue_empty(n
->rx_vq
) ||
415 (n
->mergeable_rx_bufs
&&
416 !virtqueue_avail_bytes(n
->rx_vq
, bufsize
, 0)))
420 virtio_queue_set_notification(n
->rx_vq
, 0);
424 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
425 * it never finds out that the packets don't have valid checksums. This
426 * causes dhclient to get upset. Fedora's carried a patch for ages to
427 * fix this with Xen but it hasn't appeared in an upstream release of
430 * To avoid breaking existing guests, we catch udp packets and add
431 * checksums. This is terrible but it's better than hacking the guest
434 * N.B. if we introduce a zero-copy API, this operation is no longer free so
435 * we should provide a mechanism to disable it to avoid polluting the host
438 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
439 const uint8_t *buf
, size_t size
)
441 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
442 (size
> 27 && size
< 1500) && /* normal sized MTU */
443 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
444 (buf
[23] == 17) && /* ip.protocol == UDP */
445 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
446 /* FIXME this cast is evil */
447 net_checksum_calculate((uint8_t *)buf
, size
);
448 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
452 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
453 const void *buf
, size_t size
, size_t hdr_len
)
455 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
459 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
461 if (n
->has_vnet_hdr
) {
462 memcpy(hdr
, buf
, sizeof(*hdr
));
463 offset
= sizeof(*hdr
);
464 work_around_broken_dhclient(hdr
, buf
+ offset
, size
- offset
);
467 /* We only ever receive a struct virtio_net_hdr from the tapfd,
468 * but we may be passing along a larger header to the guest.
470 iov
[0].iov_base
+= hdr_len
;
471 iov
[0].iov_len
-= hdr_len
;
476 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
478 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
479 static const uint8_t vlan
[] = {0x81, 0x00};
480 uint8_t *ptr
= (uint8_t *)buf
;
486 if (n
->has_vnet_hdr
) {
487 ptr
+= sizeof(struct virtio_net_hdr
);
490 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
491 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
492 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
496 if (ptr
[0] & 1) { // multicast
497 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
499 } else if (n
->nomulti
) {
501 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
505 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
506 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
513 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
515 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
519 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
520 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
529 static ssize_t
virtio_net_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
531 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
532 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
533 size_t guest_hdr_len
, offset
, i
, host_hdr_len
;
535 if (!virtio_net_can_receive(&n
->nic
->nc
))
538 /* hdr_len refers to the header we supply to the guest */
539 guest_hdr_len
= n
->mergeable_rx_bufs
?
540 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
543 host_hdr_len
= n
->has_vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
544 if (!virtio_net_has_buffers(n
, size
+ guest_hdr_len
- host_hdr_len
))
547 if (!receive_filter(n
, buf
, size
))
552 while (offset
< size
) {
553 VirtQueueElement elem
;
555 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
559 if (virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
562 fprintf(stderr
, "virtio-net unexpected empty queue: "
563 "i %zd mergeable %d offset %zd, size %zd, "
564 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
565 i
, n
->mergeable_rx_bufs
, offset
, size
,
566 guest_hdr_len
, host_hdr_len
, n
->vdev
.guest_features
);
570 if (elem
.in_num
< 1) {
571 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
575 if (!n
->mergeable_rx_bufs
&& elem
.in_sg
[0].iov_len
!= guest_hdr_len
) {
576 fprintf(stderr
, "virtio-net header not in first element\n");
580 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
583 if (n
->mergeable_rx_bufs
)
584 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
586 offset
+= receive_header(n
, sg
, elem
.in_num
,
587 buf
+ offset
, size
- offset
, guest_hdr_len
);
588 total
+= guest_hdr_len
;
591 /* copy in packet. ugh */
592 len
= iov_from_buf(sg
, elem
.in_num
,
593 buf
+ offset
, size
- offset
);
596 /* If buffers can't be merged, at this point we
597 * must have consumed the complete packet.
598 * Otherwise, drop it. */
599 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
601 fprintf(stderr
, "virtio-net truncated non-mergeable packet: "
603 "i %zd mergeable %d offset %zd, size %zd, "
604 "guest hdr len %zd, host hdr len %zd\n",
605 i
, n
->mergeable_rx_bufs
,
606 offset
, size
, guest_hdr_len
, host_hdr_len
);
611 /* signal other side */
612 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
616 mhdr
->num_buffers
= i
;
618 virtqueue_flush(n
->rx_vq
, i
);
619 virtio_notify(&n
->vdev
, n
->rx_vq
);
624 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
);
626 static void virtio_net_tx_complete(VLANClientState
*nc
, ssize_t len
)
628 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
630 virtqueue_push(n
->tx_vq
, &n
->async_tx
.elem
, n
->async_tx
.len
);
631 virtio_notify(&n
->vdev
, n
->tx_vq
);
633 n
->async_tx
.elem
.out_num
= n
->async_tx
.len
= 0;
635 virtio_queue_set_notification(n
->tx_vq
, 1);
636 virtio_net_flush_tx(n
, n
->tx_vq
);
640 static int32_t virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
642 VirtQueueElement elem
;
643 int32_t num_packets
= 0;
645 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
649 if (n
->async_tx
.elem
.out_num
) {
650 virtio_queue_set_notification(n
->tx_vq
, 0);
654 while (virtqueue_pop(vq
, &elem
)) {
655 ssize_t ret
, len
= 0;
656 unsigned int out_num
= elem
.out_num
;
657 struct iovec
*out_sg
= &elem
.out_sg
[0];
660 /* hdr_len refers to the header received from the guest */
661 hdr_len
= n
->mergeable_rx_bufs
?
662 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
663 sizeof(struct virtio_net_hdr
);
665 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
666 fprintf(stderr
, "virtio-net header not in first element\n");
670 /* ignore the header if GSO is not supported */
671 if (!n
->has_vnet_hdr
) {
675 } else if (n
->mergeable_rx_bufs
) {
676 /* tapfd expects a struct virtio_net_hdr */
677 hdr_len
-= sizeof(struct virtio_net_hdr
);
678 out_sg
->iov_len
-= hdr_len
;
682 ret
= qemu_sendv_packet_async(&n
->nic
->nc
, out_sg
, out_num
,
683 virtio_net_tx_complete
);
685 virtio_queue_set_notification(n
->tx_vq
, 0);
686 n
->async_tx
.elem
= elem
;
687 n
->async_tx
.len
= len
;
693 virtqueue_push(vq
, &elem
, len
);
694 virtio_notify(&n
->vdev
, vq
);
696 if (++num_packets
>= n
->tx_burst
) {
703 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
705 VirtIONet
*n
= to_virtio_net(vdev
);
708 virtio_queue_set_notification(vq
, 1);
709 qemu_del_timer(n
->tx_timer
);
711 virtio_net_flush_tx(n
, vq
);
713 qemu_mod_timer(n
->tx_timer
,
714 qemu_get_clock(vm_clock
) + n
->tx_timeout
);
716 virtio_queue_set_notification(vq
, 0);
720 static void virtio_net_tx_timer(void *opaque
)
722 VirtIONet
*n
= opaque
;
726 /* Just in case the driver is not ready on more */
727 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
730 virtio_queue_set_notification(n
->tx_vq
, 1);
731 virtio_net_flush_tx(n
, n
->tx_vq
);
734 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
736 VirtIONet
*n
= opaque
;
738 if (n
->vhost_started
) {
739 /* TODO: should we really stop the backend?
740 * If we don't, it might keep writing to memory. */
741 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), &n
->vdev
);
742 n
->vhost_started
= 0;
744 virtio_save(&n
->vdev
, f
);
746 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
747 qemu_put_be32(f
, n
->tx_waiting
);
748 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
749 qemu_put_be16(f
, n
->status
);
750 qemu_put_byte(f
, n
->promisc
);
751 qemu_put_byte(f
, n
->allmulti
);
752 qemu_put_be32(f
, n
->mac_table
.in_use
);
753 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
754 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
755 qemu_put_be32(f
, n
->has_vnet_hdr
);
756 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
757 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
758 qemu_put_byte(f
, n
->alluni
);
759 qemu_put_byte(f
, n
->nomulti
);
760 qemu_put_byte(f
, n
->nouni
);
761 qemu_put_byte(f
, n
->nobcast
);
762 qemu_put_byte(f
, n
->has_ufo
);
765 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
767 VirtIONet
*n
= opaque
;
770 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
773 virtio_load(&n
->vdev
, f
);
775 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
776 n
->tx_waiting
= qemu_get_be32(f
);
777 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
780 n
->status
= qemu_get_be16(f
);
782 if (version_id
>= 4) {
783 if (version_id
< 8) {
784 n
->promisc
= qemu_get_be32(f
);
785 n
->allmulti
= qemu_get_be32(f
);
787 n
->promisc
= qemu_get_byte(f
);
788 n
->allmulti
= qemu_get_byte(f
);
792 if (version_id
>= 5) {
793 n
->mac_table
.in_use
= qemu_get_be32(f
);
794 /* MAC_TABLE_ENTRIES may be different from the saved image */
795 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
796 qemu_get_buffer(f
, n
->mac_table
.macs
,
797 n
->mac_table
.in_use
* ETH_ALEN
);
798 } else if (n
->mac_table
.in_use
) {
799 qemu_fseek(f
, n
->mac_table
.in_use
* ETH_ALEN
, SEEK_CUR
);
800 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
801 n
->mac_table
.in_use
= 0;
806 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
808 if (version_id
>= 7) {
809 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
810 error_report("virtio-net: saved image requires vnet_hdr=on");
814 if (n
->has_vnet_hdr
) {
815 tap_using_vnet_hdr(n
->nic
->nc
.peer
, 1);
816 tap_set_offload(n
->nic
->nc
.peer
,
817 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
818 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
819 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
820 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
821 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
825 if (version_id
>= 9) {
826 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
827 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
830 if (version_id
>= 10) {
831 n
->alluni
= qemu_get_byte(f
);
832 n
->nomulti
= qemu_get_byte(f
);
833 n
->nouni
= qemu_get_byte(f
);
834 n
->nobcast
= qemu_get_byte(f
);
837 if (version_id
>= 11) {
838 if (qemu_get_byte(f
) && !peer_has_ufo(n
)) {
839 error_report("virtio-net: saved image requires TUN_F_UFO support");
844 /* Find the first multicast entry in the saved MAC filter */
845 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
846 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
850 n
->mac_table
.first_multi
= i
;
853 qemu_mod_timer(n
->tx_timer
,
854 qemu_get_clock(vm_clock
) + n
->tx_timeout
);
859 static void virtio_net_cleanup(VLANClientState
*nc
)
861 VirtIONet
*n
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
866 static NetClientInfo net_virtio_info
= {
867 .type
= NET_CLIENT_TYPE_NIC
,
868 .size
= sizeof(NICState
),
869 .can_receive
= virtio_net_can_receive
,
870 .receive
= virtio_net_receive
,
871 .cleanup
= virtio_net_cleanup
,
872 .link_status_changed
= virtio_net_set_link_status
,
875 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
877 VirtIONet
*n
= to_virtio_net(vdev
);
878 if (!n
->nic
->nc
.peer
) {
881 if (n
->nic
->nc
.peer
->info
->type
!= NET_CLIENT_TYPE_TAP
) {
885 if (!tap_get_vhost_net(n
->nic
->nc
.peer
)) {
888 if (!!n
->vhost_started
== !!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
891 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
892 int r
= vhost_net_start(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
894 fprintf(stderr
, "unable to start vhost net: %d: "
895 "falling back on userspace virtio\n", -r
);
897 n
->vhost_started
= 1;
900 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
901 n
->vhost_started
= 0;
905 static void virtio_net_vmstate_change(void *opaque
, int running
, int reason
)
907 VirtIONet
*n
= opaque
;
908 uint8_t status
= running
? VIRTIO_CONFIG_S_DRIVER_OK
: 0;
909 /* This is called when vm is started/stopped,
910 * it will start/stop vhost backend if * appropriate
911 * e.g. after migration. */
912 virtio_net_set_status(&n
->vdev
, n
->vdev
.status
& status
);
915 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
,
916 virtio_net_conf
*net
)
920 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
921 sizeof(struct virtio_net_config
),
924 n
->vdev
.get_config
= virtio_net_get_config
;
925 n
->vdev
.set_config
= virtio_net_set_config
;
926 n
->vdev
.get_features
= virtio_net_get_features
;
927 n
->vdev
.set_features
= virtio_net_set_features
;
928 n
->vdev
.bad_features
= virtio_net_bad_features
;
929 n
->vdev
.reset
= virtio_net_reset
;
930 n
->vdev
.set_status
= virtio_net_set_status
;
931 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
932 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_tx
);
933 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
934 qemu_macaddr_default_if_unset(&conf
->macaddr
);
935 memcpy(&n
->mac
[0], &conf
->macaddr
, sizeof(n
->mac
));
936 n
->status
= VIRTIO_NET_S_LINK_UP
;
938 n
->nic
= qemu_new_nic(&net_virtio_info
, conf
, dev
->info
->name
, dev
->id
, n
);
940 qemu_format_nic_info_str(&n
->nic
->nc
, conf
->macaddr
.a
);
942 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
944 n
->tx_timeout
= net
->txtimer
;
945 n
->tx_burst
= net
->txburst
;
946 n
->mergeable_rx_bufs
= 0;
947 n
->promisc
= 1; /* for compatibility */
949 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
951 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
954 register_savevm(dev
, "virtio-net", -1, VIRTIO_NET_VM_VERSION
,
955 virtio_net_save
, virtio_net_load
, n
);
956 n
->vmstate
= qemu_add_vm_change_state_handler(virtio_net_vmstate_change
, n
);
961 void virtio_net_exit(VirtIODevice
*vdev
)
963 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
964 qemu_del_vm_change_state_handler(n
->vmstate
);
966 if (n
->vhost_started
) {
967 vhost_net_stop(tap_get_vhost_net(n
->nic
->nc
.peer
), vdev
);
970 qemu_purge_queued_packets(&n
->nic
->nc
);
972 unregister_savevm(n
->qdev
, "virtio-net", n
);
974 qemu_free(n
->mac_table
.macs
);
977 qemu_del_timer(n
->tx_timer
);
978 qemu_free_timer(n
->tx_timer
);
980 virtio_cleanup(&n
->vdev
);
981 qemu_del_vlan_client(&n
->nic
->nc
);