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.
15 #include "hw/virtio/virtio.h"
17 #include "net/checksum.h"
19 #include "qemu/error-report.h"
20 #include "qemu/timer.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "net/vhost_net.h"
23 #include "hw/virtio/virtio-bus.h"
25 #define VIRTIO_NET_VM_VERSION 11
27 #define MAC_TABLE_ENTRIES 64
28 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
31 * Calculate the number of bytes up to and including the given 'field' of
34 #define endof(container, field) \
35 (offsetof(container, field) + sizeof(((container *)0)->field))
37 typedef struct VirtIOFeature
{
42 static VirtIOFeature feature_sizes
[] = {
43 {.flags
= 1 << VIRTIO_NET_F_MAC
,
44 .end
= endof(struct virtio_net_config
, mac
)},
45 {.flags
= 1 << VIRTIO_NET_F_STATUS
,
46 .end
= endof(struct virtio_net_config
, status
)},
47 {.flags
= 1 << VIRTIO_NET_F_MQ
,
48 .end
= endof(struct virtio_net_config
, max_virtqueue_pairs
)},
52 static VirtIONetQueue
*virtio_net_get_subqueue(NetClientState
*nc
)
54 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
56 return &n
->vqs
[nc
->queue_index
];
59 static int vq2q(int queue_index
)
61 return queue_index
/ 2;
65 * - we could suppress RX interrupt if we were so inclined.
69 * Moving to QOM later in this serie.
71 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
73 return (VirtIONet
*)vdev
;
76 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
78 VirtIONet
*n
= to_virtio_net(vdev
);
79 struct virtio_net_config netcfg
;
81 stw_p(&netcfg
.status
, n
->status
);
82 stw_p(&netcfg
.max_virtqueue_pairs
, n
->max_queues
);
83 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
84 memcpy(config
, &netcfg
, n
->config_size
);
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
, n
->config_size
);
94 if (!(n
->vdev
.guest_features
>> VIRTIO_NET_F_CTRL_MAC_ADDR
& 1) &&
95 memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
96 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
97 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
101 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
103 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
104 (n
->status
& VIRTIO_NET_S_LINK_UP
) && n
->vdev
.vm_running
;
107 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
109 NetClientState
*nc
= qemu_get_queue(n
->nic
);
110 int queues
= n
->multiqueue
? n
->max_queues
: 1;
115 if (nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
119 if (!tap_get_vhost_net(nc
->peer
)) {
123 if (!!n
->vhost_started
== virtio_net_started(n
, status
) &&
124 !nc
->peer
->link_down
) {
127 if (!n
->vhost_started
) {
129 if (!vhost_net_query(tap_get_vhost_net(nc
->peer
), &n
->vdev
)) {
132 n
->vhost_started
= 1;
133 r
= vhost_net_start(&n
->vdev
, n
->nic
->ncs
, queues
);
135 error_report("unable to start vhost net: %d: "
136 "falling back on userspace virtio", -r
);
137 n
->vhost_started
= 0;
140 vhost_net_stop(&n
->vdev
, n
->nic
->ncs
, queues
);
141 n
->vhost_started
= 0;
145 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
147 VirtIONet
*n
= to_virtio_net(vdev
);
150 uint8_t queue_status
;
152 virtio_net_vhost_status(n
, status
);
154 for (i
= 0; i
< n
->max_queues
; i
++) {
157 if ((!n
->multiqueue
&& i
!= 0) || i
>= n
->curr_queues
) {
160 queue_status
= status
;
163 if (!q
->tx_waiting
) {
167 if (virtio_net_started(n
, queue_status
) && !n
->vhost_started
) {
169 qemu_mod_timer(q
->tx_timer
,
170 qemu_get_clock_ns(vm_clock
) + n
->tx_timeout
);
172 qemu_bh_schedule(q
->tx_bh
);
176 qemu_del_timer(q
->tx_timer
);
178 qemu_bh_cancel(q
->tx_bh
);
184 static void virtio_net_set_link_status(NetClientState
*nc
)
186 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
187 uint16_t old_status
= n
->status
;
190 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
192 n
->status
|= VIRTIO_NET_S_LINK_UP
;
194 if (n
->status
!= old_status
)
195 virtio_notify_config(&n
->vdev
);
197 virtio_net_set_status(&n
->vdev
, n
->vdev
.status
);
200 static void virtio_net_reset(VirtIODevice
*vdev
)
202 VirtIONet
*n
= to_virtio_net(vdev
);
204 /* Reset back to compatibility mode */
211 /* multiqueue is disabled by default */
214 /* Flush any MAC and VLAN filter table state */
215 n
->mac_table
.in_use
= 0;
216 n
->mac_table
.first_multi
= 0;
217 n
->mac_table
.multi_overflow
= 0;
218 n
->mac_table
.uni_overflow
= 0;
219 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
220 memcpy(&n
->mac
[0], &n
->nic
->conf
->macaddr
, sizeof(n
->mac
));
221 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
224 static void peer_test_vnet_hdr(VirtIONet
*n
)
226 NetClientState
*nc
= qemu_get_queue(n
->nic
);
231 if (nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
235 n
->has_vnet_hdr
= tap_has_vnet_hdr(nc
->peer
);
238 static int peer_has_vnet_hdr(VirtIONet
*n
)
240 return n
->has_vnet_hdr
;
243 static int peer_has_ufo(VirtIONet
*n
)
245 if (!peer_has_vnet_hdr(n
))
248 n
->has_ufo
= tap_has_ufo(qemu_get_queue(n
->nic
)->peer
);
253 static void virtio_net_set_mrg_rx_bufs(VirtIONet
*n
, int mergeable_rx_bufs
)
258 n
->mergeable_rx_bufs
= mergeable_rx_bufs
;
260 n
->guest_hdr_len
= n
->mergeable_rx_bufs
?
261 sizeof(struct virtio_net_hdr_mrg_rxbuf
) : sizeof(struct virtio_net_hdr
);
263 for (i
= 0; i
< n
->max_queues
; i
++) {
264 nc
= qemu_get_subqueue(n
->nic
, i
);
266 if (peer_has_vnet_hdr(n
) &&
267 tap_has_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
)) {
268 tap_set_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
);
269 n
->host_hdr_len
= n
->guest_hdr_len
;
274 static int peer_attach(VirtIONet
*n
, int index
)
276 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
282 if (nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
286 return tap_enable(nc
->peer
);
289 static int peer_detach(VirtIONet
*n
, int index
)
291 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
297 if (nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
301 return tap_disable(nc
->peer
);
304 static void virtio_net_set_queues(VirtIONet
*n
)
308 for (i
= 0; i
< n
->max_queues
; i
++) {
309 if (i
< n
->curr_queues
) {
310 assert(!peer_attach(n
, i
));
312 assert(!peer_detach(n
, i
));
317 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
, int ctrl
);
319 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
, uint32_t features
)
321 VirtIONet
*n
= to_virtio_net(vdev
);
322 NetClientState
*nc
= qemu_get_queue(n
->nic
);
324 features
|= (1 << VIRTIO_NET_F_MAC
);
326 if (!peer_has_vnet_hdr(n
)) {
327 features
&= ~(0x1 << VIRTIO_NET_F_CSUM
);
328 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO4
);
329 features
&= ~(0x1 << VIRTIO_NET_F_HOST_TSO6
);
330 features
&= ~(0x1 << VIRTIO_NET_F_HOST_ECN
);
332 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM
);
333 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4
);
334 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6
);
335 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_ECN
);
338 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
339 features
&= ~(0x1 << VIRTIO_NET_F_GUEST_UFO
);
340 features
&= ~(0x1 << VIRTIO_NET_F_HOST_UFO
);
343 if (!nc
->peer
|| nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
346 if (!tap_get_vhost_net(nc
->peer
)) {
349 return vhost_net_get_features(tap_get_vhost_net(nc
->peer
), features
);
352 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
354 uint32_t features
= 0;
356 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
358 features
|= (1 << VIRTIO_NET_F_MAC
);
359 features
|= (1 << VIRTIO_NET_F_CSUM
);
360 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
361 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
362 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
367 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
369 VirtIONet
*n
= to_virtio_net(vdev
);
372 virtio_net_set_multiqueue(n
, !!(features
& (1 << VIRTIO_NET_F_MQ
)),
373 !!(features
& (1 << VIRTIO_NET_F_CTRL_VQ
)));
375 virtio_net_set_mrg_rx_bufs(n
, !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
)));
377 if (n
->has_vnet_hdr
) {
378 tap_set_offload(qemu_get_subqueue(n
->nic
, 0)->peer
,
379 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
380 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
381 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
382 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
383 (features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
386 for (i
= 0; i
< n
->max_queues
; i
++) {
387 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
389 if (!nc
->peer
|| nc
->peer
->info
->type
!= NET_CLIENT_OPTIONS_KIND_TAP
) {
392 if (!tap_get_vhost_net(nc
->peer
)) {
395 vhost_net_ack_features(tap_get_vhost_net(nc
->peer
), features
);
399 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
400 struct iovec
*iov
, unsigned int iov_cnt
)
405 s
= iov_to_buf(iov
, iov_cnt
, 0, &on
, sizeof(on
));
406 if (s
!= sizeof(on
)) {
407 return VIRTIO_NET_ERR
;
410 if (cmd
== VIRTIO_NET_CTRL_RX_PROMISC
) {
412 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLMULTI
) {
414 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLUNI
) {
416 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOMULTI
) {
418 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOUNI
) {
420 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOBCAST
) {
423 return VIRTIO_NET_ERR
;
426 return VIRTIO_NET_OK
;
429 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
430 struct iovec
*iov
, unsigned int iov_cnt
)
432 struct virtio_net_ctrl_mac mac_data
;
435 if (cmd
== VIRTIO_NET_CTRL_MAC_ADDR_SET
) {
436 if (iov_size(iov
, iov_cnt
) != sizeof(n
->mac
)) {
437 return VIRTIO_NET_ERR
;
439 s
= iov_to_buf(iov
, iov_cnt
, 0, &n
->mac
, sizeof(n
->mac
));
440 assert(s
== sizeof(n
->mac
));
441 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
442 return VIRTIO_NET_OK
;
445 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
) {
446 return VIRTIO_NET_ERR
;
449 n
->mac_table
.in_use
= 0;
450 n
->mac_table
.first_multi
= 0;
451 n
->mac_table
.uni_overflow
= 0;
452 n
->mac_table
.multi_overflow
= 0;
453 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
455 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
456 sizeof(mac_data
.entries
));
457 mac_data
.entries
= ldl_p(&mac_data
.entries
);
458 if (s
!= sizeof(mac_data
.entries
)) {
459 return VIRTIO_NET_ERR
;
461 iov_discard_front(&iov
, &iov_cnt
, s
);
463 if (mac_data
.entries
* ETH_ALEN
> iov_size(iov
, iov_cnt
)) {
464 return VIRTIO_NET_ERR
;
467 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
468 s
= iov_to_buf(iov
, iov_cnt
, 0, n
->mac_table
.macs
,
469 mac_data
.entries
* ETH_ALEN
);
470 if (s
!= mac_data
.entries
* ETH_ALEN
) {
471 return VIRTIO_NET_ERR
;
473 n
->mac_table
.in_use
+= mac_data
.entries
;
475 n
->mac_table
.uni_overflow
= 1;
478 iov_discard_front(&iov
, &iov_cnt
, mac_data
.entries
* ETH_ALEN
);
480 n
->mac_table
.first_multi
= n
->mac_table
.in_use
;
482 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
483 sizeof(mac_data
.entries
));
484 mac_data
.entries
= ldl_p(&mac_data
.entries
);
485 if (s
!= sizeof(mac_data
.entries
)) {
486 return VIRTIO_NET_ERR
;
489 iov_discard_front(&iov
, &iov_cnt
, s
);
491 if (mac_data
.entries
* ETH_ALEN
!= iov_size(iov
, iov_cnt
)) {
492 return VIRTIO_NET_ERR
;
495 if (n
->mac_table
.in_use
+ mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
496 s
= iov_to_buf(iov
, iov_cnt
, 0, n
->mac_table
.macs
,
497 mac_data
.entries
* ETH_ALEN
);
498 if (s
!= mac_data
.entries
* ETH_ALEN
) {
499 return VIRTIO_NET_ERR
;
501 n
->mac_table
.in_use
+= mac_data
.entries
;
503 n
->mac_table
.multi_overflow
= 1;
506 return VIRTIO_NET_OK
;
509 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
510 struct iovec
*iov
, unsigned int iov_cnt
)
515 s
= iov_to_buf(iov
, iov_cnt
, 0, &vid
, sizeof(vid
));
517 if (s
!= sizeof(vid
)) {
518 return VIRTIO_NET_ERR
;
522 return VIRTIO_NET_ERR
;
524 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
525 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
526 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
527 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
529 return VIRTIO_NET_ERR
;
531 return VIRTIO_NET_OK
;
534 static int virtio_net_handle_mq(VirtIONet
*n
, uint8_t cmd
,
535 struct iovec
*iov
, unsigned int iov_cnt
)
537 struct virtio_net_ctrl_mq mq
;
541 s
= iov_to_buf(iov
, iov_cnt
, 0, &mq
, sizeof(mq
));
542 if (s
!= sizeof(mq
)) {
543 return VIRTIO_NET_ERR
;
546 if (cmd
!= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
) {
547 return VIRTIO_NET_ERR
;
550 queues
= lduw_p(&mq
.virtqueue_pairs
);
552 if (queues
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
553 queues
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
554 queues
> n
->max_queues
||
556 return VIRTIO_NET_ERR
;
559 n
->curr_queues
= queues
;
560 /* stop the backend before changing the number of queues to avoid handling a
562 virtio_net_set_status(&n
->vdev
, n
->vdev
.status
);
563 virtio_net_set_queues(n
);
565 return VIRTIO_NET_OK
;
567 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
569 VirtIONet
*n
= to_virtio_net(vdev
);
570 struct virtio_net_ctrl_hdr ctrl
;
571 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
572 VirtQueueElement elem
;
575 unsigned int iov_cnt
;
577 while (virtqueue_pop(vq
, &elem
)) {
578 if (iov_size(elem
.in_sg
, elem
.in_num
) < sizeof(status
) ||
579 iov_size(elem
.out_sg
, elem
.out_num
) < sizeof(ctrl
)) {
580 error_report("virtio-net ctrl missing headers");
585 iov_cnt
= elem
.out_num
;
586 s
= iov_to_buf(iov
, iov_cnt
, 0, &ctrl
, sizeof(ctrl
));
587 iov_discard_front(&iov
, &iov_cnt
, sizeof(ctrl
));
588 if (s
!= sizeof(ctrl
)) {
589 status
= VIRTIO_NET_ERR
;
590 } else if (ctrl
.class == VIRTIO_NET_CTRL_RX
) {
591 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, iov
, iov_cnt
);
592 } else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
) {
593 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, iov
, iov_cnt
);
594 } else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
) {
595 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, iov
, iov_cnt
);
596 } else if (ctrl
.class == VIRTIO_NET_CTRL_MQ
) {
597 status
= virtio_net_handle_mq(n
, ctrl
.cmd
, iov
, iov_cnt
);
600 s
= iov_from_buf(elem
.in_sg
, elem
.in_num
, 0, &status
, sizeof(status
));
601 assert(s
== sizeof(status
));
603 virtqueue_push(vq
, &elem
, sizeof(status
));
604 virtio_notify(vdev
, vq
);
610 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
612 VirtIONet
*n
= to_virtio_net(vdev
);
613 int queue_index
= vq2q(virtio_get_queue_index(vq
));
615 qemu_flush_queued_packets(qemu_get_subqueue(n
->nic
, queue_index
));
618 static int virtio_net_can_receive(NetClientState
*nc
)
620 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
621 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
623 if (!n
->vdev
.vm_running
) {
627 if (nc
->queue_index
>= n
->curr_queues
) {
631 if (!virtio_queue_ready(q
->rx_vq
) ||
632 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
639 static int virtio_net_has_buffers(VirtIONetQueue
*q
, int bufsize
)
642 if (virtio_queue_empty(q
->rx_vq
) ||
643 (n
->mergeable_rx_bufs
&&
644 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
645 virtio_queue_set_notification(q
->rx_vq
, 1);
647 /* To avoid a race condition where the guest has made some buffers
648 * available after the above check but before notification was
649 * enabled, check for available buffers again.
651 if (virtio_queue_empty(q
->rx_vq
) ||
652 (n
->mergeable_rx_bufs
&&
653 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
658 virtio_queue_set_notification(q
->rx_vq
, 0);
662 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
663 * it never finds out that the packets don't have valid checksums. This
664 * causes dhclient to get upset. Fedora's carried a patch for ages to
665 * fix this with Xen but it hasn't appeared in an upstream release of
668 * To avoid breaking existing guests, we catch udp packets and add
669 * checksums. This is terrible but it's better than hacking the guest
672 * N.B. if we introduce a zero-copy API, this operation is no longer free so
673 * we should provide a mechanism to disable it to avoid polluting the host
676 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
677 uint8_t *buf
, size_t size
)
679 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
680 (size
> 27 && size
< 1500) && /* normal sized MTU */
681 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
682 (buf
[23] == 17) && /* ip.protocol == UDP */
683 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
684 net_checksum_calculate(buf
, size
);
685 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
689 static void receive_header(VirtIONet
*n
, const struct iovec
*iov
, int iov_cnt
,
690 const void *buf
, size_t size
)
692 if (n
->has_vnet_hdr
) {
693 /* FIXME this cast is evil */
694 void *wbuf
= (void *)buf
;
695 work_around_broken_dhclient(wbuf
, wbuf
+ n
->host_hdr_len
,
696 size
- n
->host_hdr_len
);
697 iov_from_buf(iov
, iov_cnt
, 0, buf
, sizeof(struct virtio_net_hdr
));
699 struct virtio_net_hdr hdr
= {
701 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
703 iov_from_buf(iov
, iov_cnt
, 0, &hdr
, sizeof hdr
);
707 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
709 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
710 static const uint8_t vlan
[] = {0x81, 0x00};
711 uint8_t *ptr
= (uint8_t *)buf
;
717 ptr
+= n
->host_hdr_len
;
719 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
720 int vid
= be16_to_cpup((uint16_t *)(ptr
+ 14)) & 0xfff;
721 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
725 if (ptr
[0] & 1) { // multicast
726 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
728 } else if (n
->nomulti
) {
730 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
734 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
735 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
742 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
744 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
748 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
749 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
758 static ssize_t
virtio_net_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
760 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
761 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
762 struct iovec mhdr_sg
[VIRTQUEUE_MAX_SIZE
];
763 struct virtio_net_hdr_mrg_rxbuf mhdr
;
764 unsigned mhdr_cnt
= 0;
765 size_t offset
, i
, guest_offset
;
767 if (!virtio_net_can_receive(nc
)) {
771 /* hdr_len refers to the header we supply to the guest */
772 if (!virtio_net_has_buffers(q
, size
+ n
->guest_hdr_len
- n
->host_hdr_len
)) {
776 if (!receive_filter(n
, buf
, size
))
781 while (offset
< size
) {
782 VirtQueueElement elem
;
784 const struct iovec
*sg
= elem
.in_sg
;
788 if (virtqueue_pop(q
->rx_vq
, &elem
) == 0) {
791 error_report("virtio-net unexpected empty queue: "
792 "i %zd mergeable %d offset %zd, size %zd, "
793 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
794 i
, n
->mergeable_rx_bufs
, offset
, size
,
795 n
->guest_hdr_len
, n
->host_hdr_len
, n
->vdev
.guest_features
);
799 if (elem
.in_num
< 1) {
800 error_report("virtio-net receive queue contains no in buffers");
806 if (n
->mergeable_rx_bufs
) {
807 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
809 offsetof(typeof(mhdr
), num_buffers
),
810 sizeof(mhdr
.num_buffers
));
813 receive_header(n
, sg
, elem
.in_num
, buf
, size
);
814 offset
= n
->host_hdr_len
;
815 total
+= n
->guest_hdr_len
;
816 guest_offset
= n
->guest_hdr_len
;
821 /* copy in packet. ugh */
822 len
= iov_from_buf(sg
, elem
.in_num
, guest_offset
,
823 buf
+ offset
, size
- offset
);
826 /* If buffers can't be merged, at this point we
827 * must have consumed the complete packet.
828 * Otherwise, drop it. */
829 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
831 error_report("virtio-net truncated non-mergeable packet: "
832 "i %zd mergeable %d offset %zd, size %zd, "
833 "guest hdr len %zd, host hdr len %zd",
834 i
, n
->mergeable_rx_bufs
,
835 offset
, size
, n
->guest_hdr_len
, n
->host_hdr_len
);
840 /* signal other side */
841 virtqueue_fill(q
->rx_vq
, &elem
, total
, i
++);
845 stw_p(&mhdr
.num_buffers
, i
);
846 iov_from_buf(mhdr_sg
, mhdr_cnt
,
848 &mhdr
.num_buffers
, sizeof mhdr
.num_buffers
);
851 virtqueue_flush(q
->rx_vq
, i
);
852 virtio_notify(&n
->vdev
, q
->rx_vq
);
857 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
);
859 static void virtio_net_tx_complete(NetClientState
*nc
, ssize_t len
)
861 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
862 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
864 virtqueue_push(q
->tx_vq
, &q
->async_tx
.elem
, 0);
865 virtio_notify(&n
->vdev
, q
->tx_vq
);
867 q
->async_tx
.elem
.out_num
= q
->async_tx
.len
= 0;
869 virtio_queue_set_notification(q
->tx_vq
, 1);
870 virtio_net_flush_tx(q
);
874 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
)
877 VirtQueueElement elem
;
878 int32_t num_packets
= 0;
879 int queue_index
= vq2q(virtio_get_queue_index(q
->tx_vq
));
880 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
884 assert(n
->vdev
.vm_running
);
886 if (q
->async_tx
.elem
.out_num
) {
887 virtio_queue_set_notification(q
->tx_vq
, 0);
891 while (virtqueue_pop(q
->tx_vq
, &elem
)) {
893 unsigned int out_num
= elem
.out_num
;
894 struct iovec
*out_sg
= &elem
.out_sg
[0];
895 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
898 error_report("virtio-net header not in first element");
903 * If host wants to see the guest header as is, we can
904 * pass it on unchanged. Otherwise, copy just the parts
905 * that host is interested in.
907 assert(n
->host_hdr_len
<= n
->guest_hdr_len
);
908 if (n
->host_hdr_len
!= n
->guest_hdr_len
) {
909 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
912 sg_num
+= iov_copy(sg
+ sg_num
, ARRAY_SIZE(sg
) - sg_num
,
914 n
->guest_hdr_len
, -1);
919 len
= n
->guest_hdr_len
;
921 ret
= qemu_sendv_packet_async(qemu_get_subqueue(n
->nic
, queue_index
),
922 out_sg
, out_num
, virtio_net_tx_complete
);
924 virtio_queue_set_notification(q
->tx_vq
, 0);
925 q
->async_tx
.elem
= elem
;
926 q
->async_tx
.len
= len
;
932 virtqueue_push(q
->tx_vq
, &elem
, 0);
933 virtio_notify(&n
->vdev
, q
->tx_vq
);
935 if (++num_packets
>= n
->tx_burst
) {
942 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
944 VirtIONet
*n
= to_virtio_net(vdev
);
945 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
947 /* This happens when device was stopped but VCPU wasn't. */
948 if (!n
->vdev
.vm_running
) {
954 virtio_queue_set_notification(vq
, 1);
955 qemu_del_timer(q
->tx_timer
);
957 virtio_net_flush_tx(q
);
959 qemu_mod_timer(q
->tx_timer
,
960 qemu_get_clock_ns(vm_clock
) + n
->tx_timeout
);
962 virtio_queue_set_notification(vq
, 0);
966 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
968 VirtIONet
*n
= to_virtio_net(vdev
);
969 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
971 if (unlikely(q
->tx_waiting
)) {
975 /* This happens when device was stopped but VCPU wasn't. */
976 if (!n
->vdev
.vm_running
) {
979 virtio_queue_set_notification(vq
, 0);
980 qemu_bh_schedule(q
->tx_bh
);
983 static void virtio_net_tx_timer(void *opaque
)
985 VirtIONetQueue
*q
= opaque
;
987 assert(n
->vdev
.vm_running
);
991 /* Just in case the driver is not ready on more */
992 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
995 virtio_queue_set_notification(q
->tx_vq
, 1);
996 virtio_net_flush_tx(q
);
999 static void virtio_net_tx_bh(void *opaque
)
1001 VirtIONetQueue
*q
= opaque
;
1002 VirtIONet
*n
= q
->n
;
1005 assert(n
->vdev
.vm_running
);
1009 /* Just in case the driver is not ready on more */
1010 if (unlikely(!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)))
1013 ret
= virtio_net_flush_tx(q
);
1014 if (ret
== -EBUSY
) {
1015 return; /* Notification re-enable handled by tx_complete */
1018 /* If we flush a full burst of packets, assume there are
1019 * more coming and immediately reschedule */
1020 if (ret
>= n
->tx_burst
) {
1021 qemu_bh_schedule(q
->tx_bh
);
1026 /* If less than a full burst, re-enable notification and flush
1027 * anything that may have come in while we weren't looking. If
1028 * we find something, assume the guest is still active and reschedule */
1029 virtio_queue_set_notification(q
->tx_vq
, 1);
1030 if (virtio_net_flush_tx(q
) > 0) {
1031 virtio_queue_set_notification(q
->tx_vq
, 0);
1032 qemu_bh_schedule(q
->tx_bh
);
1037 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
, int ctrl
)
1039 VirtIODevice
*vdev
= &n
->vdev
;
1040 int i
, max
= multiqueue
? n
->max_queues
: 1;
1042 n
->multiqueue
= multiqueue
;
1044 for (i
= 2; i
<= n
->max_queues
* 2 + 1; i
++) {
1045 virtio_del_queue(vdev
, i
);
1048 for (i
= 1; i
< max
; i
++) {
1049 n
->vqs
[i
].rx_vq
= virtio_add_queue(vdev
, 256, virtio_net_handle_rx
);
1050 if (n
->vqs
[i
].tx_timer
) {
1052 virtio_add_queue(vdev
, 256, virtio_net_handle_tx_timer
);
1053 n
->vqs
[i
].tx_timer
= qemu_new_timer_ns(vm_clock
,
1054 virtio_net_tx_timer
,
1058 virtio_add_queue(vdev
, 256, virtio_net_handle_tx_bh
);
1059 n
->vqs
[i
].tx_bh
= qemu_bh_new(virtio_net_tx_bh
, &n
->vqs
[i
]);
1062 n
->vqs
[i
].tx_waiting
= 0;
1067 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
1070 virtio_net_set_queues(n
);
1073 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
1076 VirtIONet
*n
= opaque
;
1078 /* At this point, backend must be stopped, otherwise
1079 * it might keep writing to memory. */
1080 assert(!n
->vhost_started
);
1081 virtio_save(&n
->vdev
, f
);
1083 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
1084 qemu_put_be32(f
, n
->vqs
[0].tx_waiting
);
1085 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
1086 qemu_put_be16(f
, n
->status
);
1087 qemu_put_byte(f
, n
->promisc
);
1088 qemu_put_byte(f
, n
->allmulti
);
1089 qemu_put_be32(f
, n
->mac_table
.in_use
);
1090 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
1091 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
1092 qemu_put_be32(f
, n
->has_vnet_hdr
);
1093 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
1094 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
1095 qemu_put_byte(f
, n
->alluni
);
1096 qemu_put_byte(f
, n
->nomulti
);
1097 qemu_put_byte(f
, n
->nouni
);
1098 qemu_put_byte(f
, n
->nobcast
);
1099 qemu_put_byte(f
, n
->has_ufo
);
1100 if (n
->max_queues
> 1) {
1101 qemu_put_be16(f
, n
->max_queues
);
1102 qemu_put_be16(f
, n
->curr_queues
);
1103 for (i
= 1; i
< n
->curr_queues
; i
++) {
1104 qemu_put_be32(f
, n
->vqs
[i
].tx_waiting
);
1109 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
1111 VirtIONet
*n
= opaque
;
1112 int ret
, i
, link_down
;
1114 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
1117 ret
= virtio_load(&n
->vdev
, f
);
1122 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
1123 n
->vqs
[0].tx_waiting
= qemu_get_be32(f
);
1125 virtio_net_set_mrg_rx_bufs(n
, qemu_get_be32(f
));
1127 if (version_id
>= 3)
1128 n
->status
= qemu_get_be16(f
);
1130 if (version_id
>= 4) {
1131 if (version_id
< 8) {
1132 n
->promisc
= qemu_get_be32(f
);
1133 n
->allmulti
= qemu_get_be32(f
);
1135 n
->promisc
= qemu_get_byte(f
);
1136 n
->allmulti
= qemu_get_byte(f
);
1140 if (version_id
>= 5) {
1141 n
->mac_table
.in_use
= qemu_get_be32(f
);
1142 /* MAC_TABLE_ENTRIES may be different from the saved image */
1143 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
1144 qemu_get_buffer(f
, n
->mac_table
.macs
,
1145 n
->mac_table
.in_use
* ETH_ALEN
);
1146 } else if (n
->mac_table
.in_use
) {
1147 uint8_t *buf
= g_malloc0(n
->mac_table
.in_use
);
1148 qemu_get_buffer(f
, buf
, n
->mac_table
.in_use
* ETH_ALEN
);
1150 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
1151 n
->mac_table
.in_use
= 0;
1155 if (version_id
>= 6)
1156 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
1158 if (version_id
>= 7) {
1159 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
1160 error_report("virtio-net: saved image requires vnet_hdr=on");
1164 if (n
->has_vnet_hdr
) {
1165 tap_set_offload(qemu_get_queue(n
->nic
)->peer
,
1166 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
1167 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
1168 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
1169 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_ECN
) & 1,
1170 (n
->vdev
.guest_features
>> VIRTIO_NET_F_GUEST_UFO
) & 1);
1174 if (version_id
>= 9) {
1175 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
1176 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
1179 if (version_id
>= 10) {
1180 n
->alluni
= qemu_get_byte(f
);
1181 n
->nomulti
= qemu_get_byte(f
);
1182 n
->nouni
= qemu_get_byte(f
);
1183 n
->nobcast
= qemu_get_byte(f
);
1186 if (version_id
>= 11) {
1187 if (qemu_get_byte(f
) && !peer_has_ufo(n
)) {
1188 error_report("virtio-net: saved image requires TUN_F_UFO support");
1193 if (n
->max_queues
> 1) {
1194 if (n
->max_queues
!= qemu_get_be16(f
)) {
1195 error_report("virtio-net: different max_queues ");
1199 n
->curr_queues
= qemu_get_be16(f
);
1200 for (i
= 1; i
< n
->curr_queues
; i
++) {
1201 n
->vqs
[i
].tx_waiting
= qemu_get_be32(f
);
1205 virtio_net_set_queues(n
);
1207 /* Find the first multicast entry in the saved MAC filter */
1208 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
1209 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
1213 n
->mac_table
.first_multi
= i
;
1215 /* nc.link_down can't be migrated, so infer link_down according
1216 * to link status bit in n->status */
1217 link_down
= (n
->status
& VIRTIO_NET_S_LINK_UP
) == 0;
1218 for (i
= 0; i
< n
->max_queues
; i
++) {
1219 qemu_get_subqueue(n
->nic
, i
)->link_down
= link_down
;
1225 static void virtio_net_cleanup(NetClientState
*nc
)
1227 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1232 static NetClientInfo net_virtio_info
= {
1233 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
1234 .size
= sizeof(NICState
),
1235 .can_receive
= virtio_net_can_receive
,
1236 .receive
= virtio_net_receive
,
1237 .cleanup
= virtio_net_cleanup
,
1238 .link_status_changed
= virtio_net_set_link_status
,
1241 static bool virtio_net_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
1243 VirtIONet
*n
= to_virtio_net(vdev
);
1244 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
1245 assert(n
->vhost_started
);
1246 return vhost_net_virtqueue_pending(tap_get_vhost_net(nc
->peer
), idx
);
1249 static void virtio_net_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
1252 VirtIONet
*n
= to_virtio_net(vdev
);
1253 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
1254 assert(n
->vhost_started
);
1255 vhost_net_virtqueue_mask(tap_get_vhost_net(nc
->peer
),
1259 void virtio_net_set_config_size(VirtIONet
*n
, uint32_t host_features
)
1261 int i
, config_size
= 0;
1262 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
1263 if (host_features
& feature_sizes
[i
].flags
) {
1264 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
1267 n
->config_size
= config_size
;
1270 static VirtIODevice
*virtio_net_common_init(DeviceState
*dev
, NICConf
*conf
,
1271 virtio_net_conf
*net
,
1272 uint32_t host_features
,
1276 int i
, config_size
= 0;
1279 * We have two cases here: the old virtio-net-pci device, and the
1280 * refactored virtio-net.
1283 /* virtio-net-pci */
1284 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
1285 if (host_features
& feature_sizes
[i
].flags
) {
1286 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
1289 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
1290 config_size
, sizeof(VirtIONet
));
1291 n
->config_size
= config_size
;
1294 virtio_init(VIRTIO_DEVICE(n
), "virtio-net", VIRTIO_ID_NET
,
1298 n
->vdev
.get_config
= virtio_net_get_config
;
1299 n
->vdev
.set_config
= virtio_net_set_config
;
1300 n
->vdev
.get_features
= virtio_net_get_features
;
1301 n
->vdev
.set_features
= virtio_net_set_features
;
1302 n
->vdev
.bad_features
= virtio_net_bad_features
;
1303 n
->vdev
.reset
= virtio_net_reset
;
1304 n
->vdev
.set_status
= virtio_net_set_status
;
1305 n
->vdev
.guest_notifier_mask
= virtio_net_guest_notifier_mask
;
1306 n
->vdev
.guest_notifier_pending
= virtio_net_guest_notifier_pending
;
1307 n
->max_queues
= MAX(conf
->queues
, 1);
1308 n
->vqs
= g_malloc0(sizeof(VirtIONetQueue
) * n
->max_queues
);
1309 n
->vqs
[0].rx_vq
= virtio_add_queue(&n
->vdev
, 256, virtio_net_handle_rx
);
1312 n
->tx_timeout
= net
->txtimer
;
1314 if (net
->tx
&& strcmp(net
->tx
, "timer") && strcmp(net
->tx
, "bh")) {
1315 error_report("virtio-net: "
1316 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1318 error_report("Defaulting to \"bh\"");
1321 if (net
->tx
&& !strcmp(net
->tx
, "timer")) {
1322 n
->vqs
[0].tx_vq
= virtio_add_queue(&n
->vdev
, 256,
1323 virtio_net_handle_tx_timer
);
1324 n
->vqs
[0].tx_timer
= qemu_new_timer_ns(vm_clock
, virtio_net_tx_timer
,
1327 n
->vqs
[0].tx_vq
= virtio_add_queue(&n
->vdev
, 256,
1328 virtio_net_handle_tx_bh
);
1329 n
->vqs
[0].tx_bh
= qemu_bh_new(virtio_net_tx_bh
, &n
->vqs
[0]);
1331 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 64, virtio_net_handle_ctrl
);
1332 qemu_macaddr_default_if_unset(&conf
->macaddr
);
1333 memcpy(&n
->mac
[0], &conf
->macaddr
, sizeof(n
->mac
));
1334 n
->status
= VIRTIO_NET_S_LINK_UP
;
1336 n
->nic
= qemu_new_nic(&net_virtio_info
, conf
, object_get_typename(OBJECT(dev
)), dev
->id
, n
);
1337 peer_test_vnet_hdr(n
);
1338 if (peer_has_vnet_hdr(n
)) {
1339 for (i
= 0; i
< n
->max_queues
; i
++) {
1340 tap_using_vnet_hdr(qemu_get_subqueue(n
->nic
, i
)->peer
, true);
1342 n
->host_hdr_len
= sizeof(struct virtio_net_hdr
);
1344 n
->host_hdr_len
= 0;
1347 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), conf
->macaddr
.a
);
1349 n
->vqs
[0].tx_waiting
= 0;
1350 n
->tx_burst
= net
->txburst
;
1351 virtio_net_set_mrg_rx_bufs(n
, 0);
1352 n
->promisc
= 1; /* for compatibility */
1354 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1356 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
1359 register_savevm(dev
, "virtio-net", -1, VIRTIO_NET_VM_VERSION
,
1360 virtio_net_save
, virtio_net_load
, n
);
1362 add_boot_device_path(conf
->bootindex
, dev
, "/ethernet-phy@0");
1367 VirtIODevice
*virtio_net_init(DeviceState
*dev
, NICConf
*conf
,
1368 virtio_net_conf
*net
, uint32_t host_features
)
1370 VirtIONet
*n
= NULL
;
1371 return virtio_net_common_init(dev
, conf
, net
, host_features
, &n
);
1374 void virtio_net_exit(VirtIODevice
*vdev
)
1376 VirtIONet
*n
= DO_UPCAST(VirtIONet
, vdev
, vdev
);
1379 /* This will stop vhost backend if appropriate. */
1380 virtio_net_set_status(vdev
, 0);
1382 unregister_savevm(n
->qdev
, "virtio-net", n
);
1384 g_free(n
->mac_table
.macs
);
1387 for (i
= 0; i
< n
->max_queues
; i
++) {
1388 VirtIONetQueue
*q
= &n
->vqs
[i
];
1389 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
1391 qemu_purge_queued_packets(nc
);
1394 qemu_del_timer(q
->tx_timer
);
1395 qemu_free_timer(q
->tx_timer
);
1397 qemu_bh_delete(q
->tx_bh
);
1402 qemu_del_nic(n
->nic
);
1403 virtio_cleanup(&n
->vdev
);
1406 static int virtio_net_device_init(VirtIODevice
*vdev
)
1408 DeviceState
*qdev
= DEVICE(vdev
);
1409 VirtIONet
*n
= VIRTIO_NET(vdev
);
1412 * Initially, the new VirtIONet device will have a config size =
1413 * sizeof(struct config), because we can't get host_features here.
1415 if (virtio_net_common_init(qdev
, &(n
->nic_conf
),
1416 &(n
->net_conf
), 0, &n
) == NULL
) {
1422 static int virtio_net_device_exit(DeviceState
*qdev
)
1424 VirtIONet
*n
= VIRTIO_NET(qdev
);
1425 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
1428 /* This will stop vhost backend if appropriate. */
1429 virtio_net_set_status(vdev
, 0);
1431 unregister_savevm(qdev
, "virtio-net", n
);
1433 g_free(n
->mac_table
.macs
);
1436 for (i
= 0; i
< n
->max_queues
; i
++) {
1437 VirtIONetQueue
*q
= &n
->vqs
[i
];
1438 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
1440 qemu_purge_queued_packets(nc
);
1443 qemu_del_timer(q
->tx_timer
);
1444 qemu_free_timer(q
->tx_timer
);
1446 qemu_bh_delete(q
->tx_bh
);
1451 qemu_del_nic(n
->nic
);
1452 virtio_common_cleanup(&n
->vdev
);
1457 static void virtio_net_instance_init(Object
*obj
)
1459 VirtIONet
*n
= VIRTIO_NET(obj
);
1462 * The default config_size is sizeof(struct virtio_net_config).
1463 * Can be overriden with virtio_net_set_config_size.
1465 n
->config_size
= sizeof(struct virtio_net_config
);
1468 static Property virtio_net_properties
[] = {
1469 DEFINE_NIC_PROPERTIES(VirtIONet
, nic_conf
),
1470 DEFINE_PROP_UINT32("x-txtimer", VirtIONet
, net_conf
.txtimer
,
1472 DEFINE_PROP_INT32("x-txburst", VirtIONet
, net_conf
.txburst
, TX_BURST
),
1473 DEFINE_PROP_STRING("tx", VirtIONet
, net_conf
.tx
),
1474 DEFINE_PROP_END_OF_LIST(),
1477 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
1479 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1480 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1481 dc
->exit
= virtio_net_device_exit
;
1482 dc
->props
= virtio_net_properties
;
1483 vdc
->init
= virtio_net_device_init
;
1484 vdc
->get_config
= virtio_net_get_config
;
1485 vdc
->set_config
= virtio_net_set_config
;
1486 vdc
->get_features
= virtio_net_get_features
;
1487 vdc
->set_features
= virtio_net_set_features
;
1488 vdc
->bad_features
= virtio_net_bad_features
;
1489 vdc
->reset
= virtio_net_reset
;
1490 vdc
->set_status
= virtio_net_set_status
;
1491 vdc
->guest_notifier_mask
= virtio_net_guest_notifier_mask
;
1492 vdc
->guest_notifier_pending
= virtio_net_guest_notifier_pending
;
1495 static const TypeInfo virtio_net_info
= {
1496 .name
= TYPE_VIRTIO_NET
,
1497 .parent
= TYPE_VIRTIO_DEVICE
,
1498 .instance_size
= sizeof(VirtIONet
),
1499 .instance_init
= virtio_net_instance_init
,
1500 .class_init
= virtio_net_class_init
,
1503 static void virtio_register_types(void)
1505 type_register_static(&virtio_net_info
);
1508 type_init(virtio_register_types
)