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.
14 #define VMSTATE_VIRTIO_DEVICE_USE_NEW
16 #include "qemu/osdep.h"
18 #include "hw/virtio/virtio.h"
20 #include "net/checksum.h"
22 #include "qemu/error-report.h"
23 #include "qemu/timer.h"
24 #include "hw/virtio/virtio-net.h"
25 #include "net/vhost_net.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "qapi/qmp/qjson.h"
28 #include "qapi-event.h"
29 #include "hw/virtio/virtio-access.h"
31 #define VIRTIO_NET_VM_VERSION 11
33 #define MAC_TABLE_ENTRIES 64
34 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
36 /* previously fixed value */
37 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
38 /* for now, only allow larger queues; with virtio-1, guest can downsize */
39 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
42 * Calculate the number of bytes up to and including the given 'field' of
45 #define endof(container, field) \
46 (offsetof(container, field) + sizeof(((container *)0)->field))
48 typedef struct VirtIOFeature
{
53 static VirtIOFeature feature_sizes
[] = {
54 {.flags
= 1 << VIRTIO_NET_F_MAC
,
55 .end
= endof(struct virtio_net_config
, mac
)},
56 {.flags
= 1 << VIRTIO_NET_F_STATUS
,
57 .end
= endof(struct virtio_net_config
, status
)},
58 {.flags
= 1 << VIRTIO_NET_F_MQ
,
59 .end
= endof(struct virtio_net_config
, max_virtqueue_pairs
)},
63 static VirtIONetQueue
*virtio_net_get_subqueue(NetClientState
*nc
)
65 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
67 return &n
->vqs
[nc
->queue_index
];
70 static int vq2q(int queue_index
)
72 return queue_index
/ 2;
76 * - we could suppress RX interrupt if we were so inclined.
79 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
81 VirtIONet
*n
= VIRTIO_NET(vdev
);
82 struct virtio_net_config netcfg
;
84 virtio_stw_p(vdev
, &netcfg
.status
, n
->status
);
85 virtio_stw_p(vdev
, &netcfg
.max_virtqueue_pairs
, n
->max_queues
);
86 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
87 memcpy(config
, &netcfg
, n
->config_size
);
90 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
92 VirtIONet
*n
= VIRTIO_NET(vdev
);
93 struct virtio_net_config netcfg
= {};
95 memcpy(&netcfg
, config
, n
->config_size
);
97 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_MAC_ADDR
) &&
98 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
99 memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
100 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
101 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
105 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
107 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
108 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
109 (n
->status
& VIRTIO_NET_S_LINK_UP
) && vdev
->vm_running
;
112 static void virtio_net_announce_timer(void *opaque
)
114 VirtIONet
*n
= opaque
;
115 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
117 n
->announce_counter
--;
118 n
->status
|= VIRTIO_NET_S_ANNOUNCE
;
119 virtio_notify_config(vdev
);
122 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
124 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
125 NetClientState
*nc
= qemu_get_queue(n
->nic
);
126 int queues
= n
->multiqueue
? n
->max_queues
: 1;
128 if (!get_vhost_net(nc
->peer
)) {
132 if ((virtio_net_started(n
, status
) && !nc
->peer
->link_down
) ==
133 !!n
->vhost_started
) {
136 if (!n
->vhost_started
) {
139 if (n
->needs_vnet_hdr_swap
) {
140 error_report("backend does not support %s vnet headers; "
141 "falling back on userspace virtio",
142 virtio_is_big_endian(vdev
) ? "BE" : "LE");
146 /* Any packets outstanding? Purge them to avoid touching rings
147 * when vhost is running.
149 for (i
= 0; i
< queues
; i
++) {
150 NetClientState
*qnc
= qemu_get_subqueue(n
->nic
, i
);
152 /* Purge both directions: TX and RX. */
153 qemu_net_queue_purge(qnc
->peer
->incoming_queue
, qnc
);
154 qemu_net_queue_purge(qnc
->incoming_queue
, qnc
->peer
);
157 n
->vhost_started
= 1;
158 r
= vhost_net_start(vdev
, n
->nic
->ncs
, queues
);
160 error_report("unable to start vhost net: %d: "
161 "falling back on userspace virtio", -r
);
162 n
->vhost_started
= 0;
165 vhost_net_stop(vdev
, n
->nic
->ncs
, queues
);
166 n
->vhost_started
= 0;
170 static int virtio_net_set_vnet_endian_one(VirtIODevice
*vdev
,
171 NetClientState
*peer
,
174 if (virtio_is_big_endian(vdev
)) {
175 return qemu_set_vnet_be(peer
, enable
);
177 return qemu_set_vnet_le(peer
, enable
);
181 static bool virtio_net_set_vnet_endian(VirtIODevice
*vdev
, NetClientState
*ncs
,
182 int queues
, bool enable
)
186 for (i
= 0; i
< queues
; i
++) {
187 if (virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, enable
) < 0 &&
190 virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, false);
200 static void virtio_net_vnet_endian_status(VirtIONet
*n
, uint8_t status
)
202 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
203 int queues
= n
->multiqueue
? n
->max_queues
: 1;
205 if (virtio_net_started(n
, status
)) {
206 /* Before using the device, we tell the network backend about the
207 * endianness to use when parsing vnet headers. If the backend
208 * can't do it, we fallback onto fixing the headers in the core
211 n
->needs_vnet_hdr_swap
= virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
,
213 } else if (virtio_net_started(n
, vdev
->status
)) {
214 /* After using the device, we need to reset the network backend to
215 * the default (guest native endianness), otherwise the guest may
216 * lose network connectivity if it is rebooted into a different
219 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
, queues
, false);
223 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
225 VirtIONet
*n
= VIRTIO_NET(vdev
);
228 uint8_t queue_status
;
230 virtio_net_vnet_endian_status(n
, status
);
231 virtio_net_vhost_status(n
, status
);
233 for (i
= 0; i
< n
->max_queues
; i
++) {
234 NetClientState
*ncs
= qemu_get_subqueue(n
->nic
, i
);
238 if ((!n
->multiqueue
&& i
!= 0) || i
>= n
->curr_queues
) {
241 queue_status
= status
;
244 virtio_net_started(n
, queue_status
) && !n
->vhost_started
;
247 qemu_flush_queued_packets(ncs
);
250 if (!q
->tx_waiting
) {
256 timer_mod(q
->tx_timer
,
257 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
259 qemu_bh_schedule(q
->tx_bh
);
263 timer_del(q
->tx_timer
);
265 qemu_bh_cancel(q
->tx_bh
);
271 static void virtio_net_set_link_status(NetClientState
*nc
)
273 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
274 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
275 uint16_t old_status
= n
->status
;
278 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
280 n
->status
|= VIRTIO_NET_S_LINK_UP
;
282 if (n
->status
!= old_status
)
283 virtio_notify_config(vdev
);
285 virtio_net_set_status(vdev
, vdev
->status
);
288 static void rxfilter_notify(NetClientState
*nc
)
290 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
292 if (nc
->rxfilter_notify_enabled
) {
293 gchar
*path
= object_get_canonical_path(OBJECT(n
->qdev
));
294 qapi_event_send_nic_rx_filter_changed(!!n
->netclient_name
,
295 n
->netclient_name
, path
, &error_abort
);
298 /* disable event notification to avoid events flooding */
299 nc
->rxfilter_notify_enabled
= 0;
303 static intList
*get_vlan_table(VirtIONet
*n
)
305 intList
*list
, *entry
;
309 for (i
= 0; i
< MAX_VLAN
>> 5; i
++) {
310 for (j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
311 if (n
->vlans
[i
] & (1U << j
)) {
312 entry
= g_malloc0(sizeof(*entry
));
313 entry
->value
= (i
<< 5) + j
;
323 static RxFilterInfo
*virtio_net_query_rxfilter(NetClientState
*nc
)
325 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
326 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
328 strList
*str_list
, *entry
;
331 info
= g_malloc0(sizeof(*info
));
332 info
->name
= g_strdup(nc
->name
);
333 info
->promiscuous
= n
->promisc
;
336 info
->unicast
= RX_STATE_NONE
;
337 } else if (n
->alluni
) {
338 info
->unicast
= RX_STATE_ALL
;
340 info
->unicast
= RX_STATE_NORMAL
;
344 info
->multicast
= RX_STATE_NONE
;
345 } else if (n
->allmulti
) {
346 info
->multicast
= RX_STATE_ALL
;
348 info
->multicast
= RX_STATE_NORMAL
;
351 info
->broadcast_allowed
= n
->nobcast
;
352 info
->multicast_overflow
= n
->mac_table
.multi_overflow
;
353 info
->unicast_overflow
= n
->mac_table
.uni_overflow
;
355 info
->main_mac
= qemu_mac_strdup_printf(n
->mac
);
358 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
359 entry
= g_malloc0(sizeof(*entry
));
360 entry
->value
= qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
);
361 entry
->next
= str_list
;
364 info
->unicast_table
= str_list
;
367 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
368 entry
= g_malloc0(sizeof(*entry
));
369 entry
->value
= qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
);
370 entry
->next
= str_list
;
373 info
->multicast_table
= str_list
;
374 info
->vlan_table
= get_vlan_table(n
);
376 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VLAN
)) {
377 info
->vlan
= RX_STATE_ALL
;
378 } else if (!info
->vlan_table
) {
379 info
->vlan
= RX_STATE_NONE
;
381 info
->vlan
= RX_STATE_NORMAL
;
384 /* enable event notification after query */
385 nc
->rxfilter_notify_enabled
= 1;
390 static void virtio_net_reset(VirtIODevice
*vdev
)
392 VirtIONet
*n
= VIRTIO_NET(vdev
);
394 /* Reset back to compatibility mode */
401 /* multiqueue is disabled by default */
403 timer_del(n
->announce_timer
);
404 n
->announce_counter
= 0;
405 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
407 /* Flush any MAC and VLAN filter table state */
408 n
->mac_table
.in_use
= 0;
409 n
->mac_table
.first_multi
= 0;
410 n
->mac_table
.multi_overflow
= 0;
411 n
->mac_table
.uni_overflow
= 0;
412 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
413 memcpy(&n
->mac
[0], &n
->nic
->conf
->macaddr
, sizeof(n
->mac
));
414 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
415 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
418 static void peer_test_vnet_hdr(VirtIONet
*n
)
420 NetClientState
*nc
= qemu_get_queue(n
->nic
);
425 n
->has_vnet_hdr
= qemu_has_vnet_hdr(nc
->peer
);
428 static int peer_has_vnet_hdr(VirtIONet
*n
)
430 return n
->has_vnet_hdr
;
433 static int peer_has_ufo(VirtIONet
*n
)
435 if (!peer_has_vnet_hdr(n
))
438 n
->has_ufo
= qemu_has_ufo(qemu_get_queue(n
->nic
)->peer
);
443 static void virtio_net_set_mrg_rx_bufs(VirtIONet
*n
, int mergeable_rx_bufs
,
449 n
->mergeable_rx_bufs
= mergeable_rx_bufs
;
452 n
->guest_hdr_len
= sizeof(struct virtio_net_hdr_mrg_rxbuf
);
454 n
->guest_hdr_len
= n
->mergeable_rx_bufs
?
455 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
456 sizeof(struct virtio_net_hdr
);
459 for (i
= 0; i
< n
->max_queues
; i
++) {
460 nc
= qemu_get_subqueue(n
->nic
, i
);
462 if (peer_has_vnet_hdr(n
) &&
463 qemu_has_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
)) {
464 qemu_set_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
);
465 n
->host_hdr_len
= n
->guest_hdr_len
;
470 static int peer_attach(VirtIONet
*n
, int index
)
472 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
478 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
479 vhost_set_vring_enable(nc
->peer
, 1);
482 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
486 return tap_enable(nc
->peer
);
489 static int peer_detach(VirtIONet
*n
, int index
)
491 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
497 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
498 vhost_set_vring_enable(nc
->peer
, 0);
501 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
505 return tap_disable(nc
->peer
);
508 static void virtio_net_set_queues(VirtIONet
*n
)
513 for (i
= 0; i
< n
->max_queues
; i
++) {
514 if (i
< n
->curr_queues
) {
515 r
= peer_attach(n
, i
);
518 r
= peer_detach(n
, i
);
524 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
);
526 static uint64_t virtio_net_get_features(VirtIODevice
*vdev
, uint64_t features
,
529 VirtIONet
*n
= VIRTIO_NET(vdev
);
530 NetClientState
*nc
= qemu_get_queue(n
->nic
);
532 /* Firstly sync all virtio-net possible supported features */
533 features
|= n
->host_features
;
535 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
537 if (!peer_has_vnet_hdr(n
)) {
538 virtio_clear_feature(&features
, VIRTIO_NET_F_CSUM
);
539 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
540 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
541 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
543 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_CSUM
);
544 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO4
);
545 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO6
);
546 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ECN
);
549 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
550 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_UFO
);
551 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_UFO
);
554 if (!get_vhost_net(nc
->peer
)) {
557 return vhost_net_get_features(get_vhost_net(nc
->peer
), features
);
560 static uint64_t virtio_net_bad_features(VirtIODevice
*vdev
)
562 uint64_t features
= 0;
564 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
566 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
567 virtio_add_feature(&features
, VIRTIO_NET_F_CSUM
);
568 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
569 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
570 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
575 static void virtio_net_apply_guest_offloads(VirtIONet
*n
)
577 qemu_set_offload(qemu_get_queue(n
->nic
)->peer
,
578 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_CSUM
)),
579 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO4
)),
580 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO6
)),
581 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_ECN
)),
582 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_UFO
)));
585 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features
)
587 static const uint64_t guest_offloads_mask
=
588 (1ULL << VIRTIO_NET_F_GUEST_CSUM
) |
589 (1ULL << VIRTIO_NET_F_GUEST_TSO4
) |
590 (1ULL << VIRTIO_NET_F_GUEST_TSO6
) |
591 (1ULL << VIRTIO_NET_F_GUEST_ECN
) |
592 (1ULL << VIRTIO_NET_F_GUEST_UFO
);
594 return guest_offloads_mask
& features
;
597 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet
*n
)
599 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
600 return virtio_net_guest_offloads_by_features(vdev
->guest_features
);
603 static void virtio_net_set_features(VirtIODevice
*vdev
, uint64_t features
)
605 VirtIONet
*n
= VIRTIO_NET(vdev
);
608 virtio_net_set_multiqueue(n
,
609 virtio_has_feature(features
, VIRTIO_NET_F_MQ
));
611 virtio_net_set_mrg_rx_bufs(n
,
612 virtio_has_feature(features
,
613 VIRTIO_NET_F_MRG_RXBUF
),
614 virtio_has_feature(features
,
615 VIRTIO_F_VERSION_1
));
617 if (n
->has_vnet_hdr
) {
618 n
->curr_guest_offloads
=
619 virtio_net_guest_offloads_by_features(features
);
620 virtio_net_apply_guest_offloads(n
);
623 for (i
= 0; i
< n
->max_queues
; i
++) {
624 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
626 if (!get_vhost_net(nc
->peer
)) {
629 vhost_net_ack_features(get_vhost_net(nc
->peer
), features
);
632 if (virtio_has_feature(features
, VIRTIO_NET_F_CTRL_VLAN
)) {
633 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
635 memset(n
->vlans
, 0xff, MAX_VLAN
>> 3);
639 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
640 struct iovec
*iov
, unsigned int iov_cnt
)
644 NetClientState
*nc
= qemu_get_queue(n
->nic
);
646 s
= iov_to_buf(iov
, iov_cnt
, 0, &on
, sizeof(on
));
647 if (s
!= sizeof(on
)) {
648 return VIRTIO_NET_ERR
;
651 if (cmd
== VIRTIO_NET_CTRL_RX_PROMISC
) {
653 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLMULTI
) {
655 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLUNI
) {
657 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOMULTI
) {
659 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOUNI
) {
661 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOBCAST
) {
664 return VIRTIO_NET_ERR
;
669 return VIRTIO_NET_OK
;
672 static int virtio_net_handle_offloads(VirtIONet
*n
, uint8_t cmd
,
673 struct iovec
*iov
, unsigned int iov_cnt
)
675 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
679 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
680 return VIRTIO_NET_ERR
;
683 s
= iov_to_buf(iov
, iov_cnt
, 0, &offloads
, sizeof(offloads
));
684 if (s
!= sizeof(offloads
)) {
685 return VIRTIO_NET_ERR
;
688 if (cmd
== VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
) {
689 uint64_t supported_offloads
;
691 if (!n
->has_vnet_hdr
) {
692 return VIRTIO_NET_ERR
;
695 supported_offloads
= virtio_net_supported_guest_offloads(n
);
696 if (offloads
& ~supported_offloads
) {
697 return VIRTIO_NET_ERR
;
700 n
->curr_guest_offloads
= offloads
;
701 virtio_net_apply_guest_offloads(n
);
703 return VIRTIO_NET_OK
;
705 return VIRTIO_NET_ERR
;
709 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
710 struct iovec
*iov
, unsigned int iov_cnt
)
712 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
713 struct virtio_net_ctrl_mac mac_data
;
715 NetClientState
*nc
= qemu_get_queue(n
->nic
);
717 if (cmd
== VIRTIO_NET_CTRL_MAC_ADDR_SET
) {
718 if (iov_size(iov
, iov_cnt
) != sizeof(n
->mac
)) {
719 return VIRTIO_NET_ERR
;
721 s
= iov_to_buf(iov
, iov_cnt
, 0, &n
->mac
, sizeof(n
->mac
));
722 assert(s
== sizeof(n
->mac
));
723 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
726 return VIRTIO_NET_OK
;
729 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
) {
730 return VIRTIO_NET_ERR
;
735 uint8_t uni_overflow
= 0;
736 uint8_t multi_overflow
= 0;
737 uint8_t *macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
739 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
740 sizeof(mac_data
.entries
));
741 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
742 if (s
!= sizeof(mac_data
.entries
)) {
745 iov_discard_front(&iov
, &iov_cnt
, s
);
747 if (mac_data
.entries
* ETH_ALEN
> iov_size(iov
, iov_cnt
)) {
751 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
752 s
= iov_to_buf(iov
, iov_cnt
, 0, macs
,
753 mac_data
.entries
* ETH_ALEN
);
754 if (s
!= mac_data
.entries
* ETH_ALEN
) {
757 in_use
+= mac_data
.entries
;
762 iov_discard_front(&iov
, &iov_cnt
, mac_data
.entries
* ETH_ALEN
);
764 first_multi
= in_use
;
766 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
767 sizeof(mac_data
.entries
));
768 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
769 if (s
!= sizeof(mac_data
.entries
)) {
773 iov_discard_front(&iov
, &iov_cnt
, s
);
775 if (mac_data
.entries
* ETH_ALEN
!= iov_size(iov
, iov_cnt
)) {
779 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
- in_use
) {
780 s
= iov_to_buf(iov
, iov_cnt
, 0, &macs
[in_use
* ETH_ALEN
],
781 mac_data
.entries
* ETH_ALEN
);
782 if (s
!= mac_data
.entries
* ETH_ALEN
) {
785 in_use
+= mac_data
.entries
;
790 n
->mac_table
.in_use
= in_use
;
791 n
->mac_table
.first_multi
= first_multi
;
792 n
->mac_table
.uni_overflow
= uni_overflow
;
793 n
->mac_table
.multi_overflow
= multi_overflow
;
794 memcpy(n
->mac_table
.macs
, macs
, MAC_TABLE_ENTRIES
* ETH_ALEN
);
798 return VIRTIO_NET_OK
;
802 return VIRTIO_NET_ERR
;
805 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
806 struct iovec
*iov
, unsigned int iov_cnt
)
808 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
811 NetClientState
*nc
= qemu_get_queue(n
->nic
);
813 s
= iov_to_buf(iov
, iov_cnt
, 0, &vid
, sizeof(vid
));
814 vid
= virtio_lduw_p(vdev
, &vid
);
815 if (s
!= sizeof(vid
)) {
816 return VIRTIO_NET_ERR
;
820 return VIRTIO_NET_ERR
;
822 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
823 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
824 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
825 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
827 return VIRTIO_NET_ERR
;
831 return VIRTIO_NET_OK
;
834 static int virtio_net_handle_announce(VirtIONet
*n
, uint8_t cmd
,
835 struct iovec
*iov
, unsigned int iov_cnt
)
837 if (cmd
== VIRTIO_NET_CTRL_ANNOUNCE_ACK
&&
838 n
->status
& VIRTIO_NET_S_ANNOUNCE
) {
839 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
840 if (n
->announce_counter
) {
841 timer_mod(n
->announce_timer
,
842 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
843 self_announce_delay(n
->announce_counter
));
845 return VIRTIO_NET_OK
;
847 return VIRTIO_NET_ERR
;
851 static int virtio_net_handle_mq(VirtIONet
*n
, uint8_t cmd
,
852 struct iovec
*iov
, unsigned int iov_cnt
)
854 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
855 struct virtio_net_ctrl_mq mq
;
859 s
= iov_to_buf(iov
, iov_cnt
, 0, &mq
, sizeof(mq
));
860 if (s
!= sizeof(mq
)) {
861 return VIRTIO_NET_ERR
;
864 if (cmd
!= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
) {
865 return VIRTIO_NET_ERR
;
868 queues
= virtio_lduw_p(vdev
, &mq
.virtqueue_pairs
);
870 if (queues
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
871 queues
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
872 queues
> n
->max_queues
||
874 return VIRTIO_NET_ERR
;
877 n
->curr_queues
= queues
;
878 /* stop the backend before changing the number of queues to avoid handling a
880 virtio_net_set_status(vdev
, vdev
->status
);
881 virtio_net_set_queues(n
);
883 return VIRTIO_NET_OK
;
886 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
888 VirtIONet
*n
= VIRTIO_NET(vdev
);
889 struct virtio_net_ctrl_hdr ctrl
;
890 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
891 VirtQueueElement
*elem
;
893 struct iovec
*iov
, *iov2
;
894 unsigned int iov_cnt
;
897 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
901 if (iov_size(elem
->in_sg
, elem
->in_num
) < sizeof(status
) ||
902 iov_size(elem
->out_sg
, elem
->out_num
) < sizeof(ctrl
)) {
903 virtio_error(vdev
, "virtio-net ctrl missing headers");
904 virtqueue_detach_element(vq
, elem
, 0);
909 iov_cnt
= elem
->out_num
;
910 iov2
= iov
= g_memdup(elem
->out_sg
, sizeof(struct iovec
) * elem
->out_num
);
911 s
= iov_to_buf(iov
, iov_cnt
, 0, &ctrl
, sizeof(ctrl
));
912 iov_discard_front(&iov
, &iov_cnt
, sizeof(ctrl
));
913 if (s
!= sizeof(ctrl
)) {
914 status
= VIRTIO_NET_ERR
;
915 } else if (ctrl
.class == VIRTIO_NET_CTRL_RX
) {
916 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, iov
, iov_cnt
);
917 } else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
) {
918 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, iov
, iov_cnt
);
919 } else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
) {
920 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, iov
, iov_cnt
);
921 } else if (ctrl
.class == VIRTIO_NET_CTRL_ANNOUNCE
) {
922 status
= virtio_net_handle_announce(n
, ctrl
.cmd
, iov
, iov_cnt
);
923 } else if (ctrl
.class == VIRTIO_NET_CTRL_MQ
) {
924 status
= virtio_net_handle_mq(n
, ctrl
.cmd
, iov
, iov_cnt
);
925 } else if (ctrl
.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS
) {
926 status
= virtio_net_handle_offloads(n
, ctrl
.cmd
, iov
, iov_cnt
);
929 s
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
, sizeof(status
));
930 assert(s
== sizeof(status
));
932 virtqueue_push(vq
, elem
, sizeof(status
));
933 virtio_notify(vdev
, vq
);
941 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
943 VirtIONet
*n
= VIRTIO_NET(vdev
);
944 int queue_index
= vq2q(virtio_get_queue_index(vq
));
946 qemu_flush_queued_packets(qemu_get_subqueue(n
->nic
, queue_index
));
949 static int virtio_net_can_receive(NetClientState
*nc
)
951 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
952 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
953 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
955 if (!vdev
->vm_running
) {
959 if (nc
->queue_index
>= n
->curr_queues
) {
963 if (!virtio_queue_ready(q
->rx_vq
) ||
964 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
971 static int virtio_net_has_buffers(VirtIONetQueue
*q
, int bufsize
)
974 if (virtio_queue_empty(q
->rx_vq
) ||
975 (n
->mergeable_rx_bufs
&&
976 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
977 virtio_queue_set_notification(q
->rx_vq
, 1);
979 /* To avoid a race condition where the guest has made some buffers
980 * available after the above check but before notification was
981 * enabled, check for available buffers again.
983 if (virtio_queue_empty(q
->rx_vq
) ||
984 (n
->mergeable_rx_bufs
&&
985 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
990 virtio_queue_set_notification(q
->rx_vq
, 0);
994 static void virtio_net_hdr_swap(VirtIODevice
*vdev
, struct virtio_net_hdr
*hdr
)
996 virtio_tswap16s(vdev
, &hdr
->hdr_len
);
997 virtio_tswap16s(vdev
, &hdr
->gso_size
);
998 virtio_tswap16s(vdev
, &hdr
->csum_start
);
999 virtio_tswap16s(vdev
, &hdr
->csum_offset
);
1002 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1003 * it never finds out that the packets don't have valid checksums. This
1004 * causes dhclient to get upset. Fedora's carried a patch for ages to
1005 * fix this with Xen but it hasn't appeared in an upstream release of
1008 * To avoid breaking existing guests, we catch udp packets and add
1009 * checksums. This is terrible but it's better than hacking the guest
1012 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1013 * we should provide a mechanism to disable it to avoid polluting the host
1016 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
1017 uint8_t *buf
, size_t size
)
1019 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
1020 (size
> 27 && size
< 1500) && /* normal sized MTU */
1021 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
1022 (buf
[23] == 17) && /* ip.protocol == UDP */
1023 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
1024 net_checksum_calculate(buf
, size
);
1025 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1029 static void receive_header(VirtIONet
*n
, const struct iovec
*iov
, int iov_cnt
,
1030 const void *buf
, size_t size
)
1032 if (n
->has_vnet_hdr
) {
1033 /* FIXME this cast is evil */
1034 void *wbuf
= (void *)buf
;
1035 work_around_broken_dhclient(wbuf
, wbuf
+ n
->host_hdr_len
,
1036 size
- n
->host_hdr_len
);
1038 if (n
->needs_vnet_hdr_swap
) {
1039 virtio_net_hdr_swap(VIRTIO_DEVICE(n
), wbuf
);
1041 iov_from_buf(iov
, iov_cnt
, 0, buf
, sizeof(struct virtio_net_hdr
));
1043 struct virtio_net_hdr hdr
= {
1045 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
1047 iov_from_buf(iov
, iov_cnt
, 0, &hdr
, sizeof hdr
);
1051 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
1053 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1054 static const uint8_t vlan
[] = {0x81, 0x00};
1055 uint8_t *ptr
= (uint8_t *)buf
;
1061 ptr
+= n
->host_hdr_len
;
1063 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
1064 int vid
= lduw_be_p(ptr
+ 14) & 0xfff;
1065 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
1069 if (ptr
[0] & 1) { // multicast
1070 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
1072 } else if (n
->nomulti
) {
1074 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
1078 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
1079 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1086 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
1088 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
1092 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
1093 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1102 static ssize_t
virtio_net_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
1104 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1105 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1106 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1107 struct iovec mhdr_sg
[VIRTQUEUE_MAX_SIZE
];
1108 struct virtio_net_hdr_mrg_rxbuf mhdr
;
1109 unsigned mhdr_cnt
= 0;
1110 size_t offset
, i
, guest_offset
;
1112 if (!virtio_net_can_receive(nc
)) {
1116 /* hdr_len refers to the header we supply to the guest */
1117 if (!virtio_net_has_buffers(q
, size
+ n
->guest_hdr_len
- n
->host_hdr_len
)) {
1121 if (!receive_filter(n
, buf
, size
))
1126 while (offset
< size
) {
1127 VirtQueueElement
*elem
;
1129 const struct iovec
*sg
;
1133 elem
= virtqueue_pop(q
->rx_vq
, sizeof(VirtQueueElement
));
1136 virtio_error(vdev
, "virtio-net unexpected empty queue: "
1137 "i %zd mergeable %d offset %zd, size %zd, "
1138 "guest hdr len %zd, host hdr len %zd "
1139 "guest features 0x%" PRIx64
,
1140 i
, n
->mergeable_rx_bufs
, offset
, size
,
1141 n
->guest_hdr_len
, n
->host_hdr_len
,
1142 vdev
->guest_features
);
1147 if (elem
->in_num
< 1) {
1149 "virtio-net receive queue contains no in buffers");
1150 virtqueue_detach_element(q
->rx_vq
, elem
, 0);
1157 assert(offset
== 0);
1158 if (n
->mergeable_rx_bufs
) {
1159 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
1161 offsetof(typeof(mhdr
), num_buffers
),
1162 sizeof(mhdr
.num_buffers
));
1165 receive_header(n
, sg
, elem
->in_num
, buf
, size
);
1166 offset
= n
->host_hdr_len
;
1167 total
+= n
->guest_hdr_len
;
1168 guest_offset
= n
->guest_hdr_len
;
1173 /* copy in packet. ugh */
1174 len
= iov_from_buf(sg
, elem
->in_num
, guest_offset
,
1175 buf
+ offset
, size
- offset
);
1178 /* If buffers can't be merged, at this point we
1179 * must have consumed the complete packet.
1180 * Otherwise, drop it. */
1181 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
1182 virtqueue_discard(q
->rx_vq
, elem
, total
);
1187 /* signal other side */
1188 virtqueue_fill(q
->rx_vq
, elem
, total
, i
++);
1193 virtio_stw_p(vdev
, &mhdr
.num_buffers
, i
);
1194 iov_from_buf(mhdr_sg
, mhdr_cnt
,
1196 &mhdr
.num_buffers
, sizeof mhdr
.num_buffers
);
1199 virtqueue_flush(q
->rx_vq
, i
);
1200 virtio_notify(vdev
, q
->rx_vq
);
1205 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
);
1207 static void virtio_net_tx_complete(NetClientState
*nc
, ssize_t len
)
1209 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1210 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1211 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1213 virtqueue_push(q
->tx_vq
, q
->async_tx
.elem
, 0);
1214 virtio_notify(vdev
, q
->tx_vq
);
1216 g_free(q
->async_tx
.elem
);
1217 q
->async_tx
.elem
= NULL
;
1219 virtio_queue_set_notification(q
->tx_vq
, 1);
1220 virtio_net_flush_tx(q
);
1224 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
)
1226 VirtIONet
*n
= q
->n
;
1227 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1228 VirtQueueElement
*elem
;
1229 int32_t num_packets
= 0;
1230 int queue_index
= vq2q(virtio_get_queue_index(q
->tx_vq
));
1231 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1235 if (q
->async_tx
.elem
) {
1236 virtio_queue_set_notification(q
->tx_vq
, 0);
1242 unsigned int out_num
;
1243 struct iovec sg
[VIRTQUEUE_MAX_SIZE
], sg2
[VIRTQUEUE_MAX_SIZE
+ 1], *out_sg
;
1244 struct virtio_net_hdr_mrg_rxbuf mhdr
;
1246 elem
= virtqueue_pop(q
->tx_vq
, sizeof(VirtQueueElement
));
1251 out_num
= elem
->out_num
;
1252 out_sg
= elem
->out_sg
;
1254 virtio_error(vdev
, "virtio-net header not in first element");
1255 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
1260 if (n
->has_vnet_hdr
) {
1261 if (iov_to_buf(out_sg
, out_num
, 0, &mhdr
, n
->guest_hdr_len
) <
1263 virtio_error(vdev
, "virtio-net header incorrect");
1264 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
1268 if (n
->needs_vnet_hdr_swap
) {
1269 virtio_net_hdr_swap(vdev
, (void *) &mhdr
);
1270 sg2
[0].iov_base
= &mhdr
;
1271 sg2
[0].iov_len
= n
->guest_hdr_len
;
1272 out_num
= iov_copy(&sg2
[1], ARRAY_SIZE(sg2
) - 1,
1274 n
->guest_hdr_len
, -1);
1275 if (out_num
== VIRTQUEUE_MAX_SIZE
) {
1283 * If host wants to see the guest header as is, we can
1284 * pass it on unchanged. Otherwise, copy just the parts
1285 * that host is interested in.
1287 assert(n
->host_hdr_len
<= n
->guest_hdr_len
);
1288 if (n
->host_hdr_len
!= n
->guest_hdr_len
) {
1289 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
1291 0, n
->host_hdr_len
);
1292 sg_num
+= iov_copy(sg
+ sg_num
, ARRAY_SIZE(sg
) - sg_num
,
1294 n
->guest_hdr_len
, -1);
1299 ret
= qemu_sendv_packet_async(qemu_get_subqueue(n
->nic
, queue_index
),
1300 out_sg
, out_num
, virtio_net_tx_complete
);
1302 virtio_queue_set_notification(q
->tx_vq
, 0);
1303 q
->async_tx
.elem
= elem
;
1308 virtqueue_push(q
->tx_vq
, elem
, 0);
1309 virtio_notify(vdev
, q
->tx_vq
);
1312 if (++num_packets
>= n
->tx_burst
) {
1319 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
1321 VirtIONet
*n
= VIRTIO_NET(vdev
);
1322 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
1324 /* This happens when device was stopped but VCPU wasn't. */
1325 if (!vdev
->vm_running
) {
1330 if (q
->tx_waiting
) {
1331 virtio_queue_set_notification(vq
, 1);
1332 timer_del(q
->tx_timer
);
1334 if (virtio_net_flush_tx(q
) == -EINVAL
) {
1338 timer_mod(q
->tx_timer
,
1339 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
1341 virtio_queue_set_notification(vq
, 0);
1345 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
1347 VirtIONet
*n
= VIRTIO_NET(vdev
);
1348 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
1350 if (unlikely(q
->tx_waiting
)) {
1354 /* This happens when device was stopped but VCPU wasn't. */
1355 if (!vdev
->vm_running
) {
1358 virtio_queue_set_notification(vq
, 0);
1359 qemu_bh_schedule(q
->tx_bh
);
1362 static void virtio_net_tx_timer(void *opaque
)
1364 VirtIONetQueue
*q
= opaque
;
1365 VirtIONet
*n
= q
->n
;
1366 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1367 /* This happens when device was stopped but BH wasn't. */
1368 if (!vdev
->vm_running
) {
1369 /* Make sure tx waiting is set, so we'll run when restarted. */
1370 assert(q
->tx_waiting
);
1376 /* Just in case the driver is not ready on more */
1377 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1381 virtio_queue_set_notification(q
->tx_vq
, 1);
1382 virtio_net_flush_tx(q
);
1385 static void virtio_net_tx_bh(void *opaque
)
1387 VirtIONetQueue
*q
= opaque
;
1388 VirtIONet
*n
= q
->n
;
1389 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1392 /* This happens when device was stopped but BH wasn't. */
1393 if (!vdev
->vm_running
) {
1394 /* Make sure tx waiting is set, so we'll run when restarted. */
1395 assert(q
->tx_waiting
);
1401 /* Just in case the driver is not ready on more */
1402 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
1406 ret
= virtio_net_flush_tx(q
);
1407 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
1408 return; /* Notification re-enable handled by tx_complete or device
1412 /* If we flush a full burst of packets, assume there are
1413 * more coming and immediately reschedule */
1414 if (ret
>= n
->tx_burst
) {
1415 qemu_bh_schedule(q
->tx_bh
);
1420 /* If less than a full burst, re-enable notification and flush
1421 * anything that may have come in while we weren't looking. If
1422 * we find something, assume the guest is still active and reschedule */
1423 virtio_queue_set_notification(q
->tx_vq
, 1);
1424 ret
= virtio_net_flush_tx(q
);
1425 if (ret
== -EINVAL
) {
1427 } else if (ret
> 0) {
1428 virtio_queue_set_notification(q
->tx_vq
, 0);
1429 qemu_bh_schedule(q
->tx_bh
);
1434 static void virtio_net_add_queue(VirtIONet
*n
, int index
)
1436 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1438 n
->vqs
[index
].rx_vq
= virtio_add_queue(vdev
, n
->net_conf
.rx_queue_size
,
1439 virtio_net_handle_rx
);
1440 if (n
->net_conf
.tx
&& !strcmp(n
->net_conf
.tx
, "timer")) {
1441 n
->vqs
[index
].tx_vq
=
1442 virtio_add_queue(vdev
, 256, virtio_net_handle_tx_timer
);
1443 n
->vqs
[index
].tx_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
1444 virtio_net_tx_timer
,
1447 n
->vqs
[index
].tx_vq
=
1448 virtio_add_queue(vdev
, 256, virtio_net_handle_tx_bh
);
1449 n
->vqs
[index
].tx_bh
= qemu_bh_new(virtio_net_tx_bh
, &n
->vqs
[index
]);
1452 n
->vqs
[index
].tx_waiting
= 0;
1453 n
->vqs
[index
].n
= n
;
1456 static void virtio_net_del_queue(VirtIONet
*n
, int index
)
1458 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1459 VirtIONetQueue
*q
= &n
->vqs
[index
];
1460 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
1462 qemu_purge_queued_packets(nc
);
1464 virtio_del_queue(vdev
, index
* 2);
1466 timer_del(q
->tx_timer
);
1467 timer_free(q
->tx_timer
);
1469 qemu_bh_delete(q
->tx_bh
);
1471 virtio_del_queue(vdev
, index
* 2 + 1);
1474 static void virtio_net_change_num_queues(VirtIONet
*n
, int new_max_queues
)
1476 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1477 int old_num_queues
= virtio_get_num_queues(vdev
);
1478 int new_num_queues
= new_max_queues
* 2 + 1;
1481 assert(old_num_queues
>= 3);
1482 assert(old_num_queues
% 2 == 1);
1484 if (old_num_queues
== new_num_queues
) {
1489 * We always need to remove and add ctrl vq if
1490 * old_num_queues != new_num_queues. Remove ctrl_vq first,
1491 * and then we only enter one of the following too loops.
1493 virtio_del_queue(vdev
, old_num_queues
- 1);
1495 for (i
= new_num_queues
- 1; i
< old_num_queues
- 1; i
+= 2) {
1496 /* new_num_queues < old_num_queues */
1497 virtio_net_del_queue(n
, i
/ 2);
1500 for (i
= old_num_queues
- 1; i
< new_num_queues
- 1; i
+= 2) {
1501 /* new_num_queues > old_num_queues */
1502 virtio_net_add_queue(n
, i
/ 2);
1505 /* add ctrl_vq last */
1506 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
1509 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
)
1511 int max
= multiqueue
? n
->max_queues
: 1;
1513 n
->multiqueue
= multiqueue
;
1514 virtio_net_change_num_queues(n
, max
);
1516 virtio_net_set_queues(n
);
1519 static void virtio_net_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
1521 VirtIONet
*n
= VIRTIO_NET(vdev
);
1524 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
1525 qemu_put_be32(f
, n
->vqs
[0].tx_waiting
);
1526 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
1527 qemu_put_be16(f
, n
->status
);
1528 qemu_put_byte(f
, n
->promisc
);
1529 qemu_put_byte(f
, n
->allmulti
);
1530 qemu_put_be32(f
, n
->mac_table
.in_use
);
1531 qemu_put_buffer(f
, n
->mac_table
.macs
, n
->mac_table
.in_use
* ETH_ALEN
);
1532 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
1533 qemu_put_be32(f
, n
->has_vnet_hdr
);
1534 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
1535 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
1536 qemu_put_byte(f
, n
->alluni
);
1537 qemu_put_byte(f
, n
->nomulti
);
1538 qemu_put_byte(f
, n
->nouni
);
1539 qemu_put_byte(f
, n
->nobcast
);
1540 qemu_put_byte(f
, n
->has_ufo
);
1541 if (n
->max_queues
> 1) {
1542 qemu_put_be16(f
, n
->max_queues
);
1543 qemu_put_be16(f
, n
->curr_queues
);
1544 for (i
= 1; i
< n
->curr_queues
; i
++) {
1545 qemu_put_be32(f
, n
->vqs
[i
].tx_waiting
);
1549 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
1550 qemu_put_be64(f
, n
->curr_guest_offloads
);
1554 static int virtio_net_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
1557 VirtIONet
*n
= VIRTIO_NET(vdev
);
1560 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
1561 n
->vqs
[0].tx_waiting
= qemu_get_be32(f
);
1563 virtio_net_set_mrg_rx_bufs(n
, qemu_get_be32(f
),
1564 virtio_vdev_has_feature(vdev
,
1565 VIRTIO_F_VERSION_1
));
1567 n
->status
= qemu_get_be16(f
);
1569 n
->promisc
= qemu_get_byte(f
);
1570 n
->allmulti
= qemu_get_byte(f
);
1572 n
->mac_table
.in_use
= qemu_get_be32(f
);
1573 /* MAC_TABLE_ENTRIES may be different from the saved image */
1574 if (n
->mac_table
.in_use
<= MAC_TABLE_ENTRIES
) {
1575 qemu_get_buffer(f
, n
->mac_table
.macs
,
1576 n
->mac_table
.in_use
* ETH_ALEN
);
1580 /* Overflow detected - can happen if source has a larger MAC table.
1581 * We simply set overflow flag so there's no need to maintain the
1582 * table of addresses, discard them all.
1583 * Note: 64 bit math to avoid integer overflow.
1585 for (i
= 0; i
< (int64_t)n
->mac_table
.in_use
* ETH_ALEN
; ++i
) {
1588 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
1589 n
->mac_table
.in_use
= 0;
1592 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
1594 if (qemu_get_be32(f
) && !peer_has_vnet_hdr(n
)) {
1595 error_report("virtio-net: saved image requires vnet_hdr=on");
1599 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
1600 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
1602 n
->alluni
= qemu_get_byte(f
);
1603 n
->nomulti
= qemu_get_byte(f
);
1604 n
->nouni
= qemu_get_byte(f
);
1605 n
->nobcast
= qemu_get_byte(f
);
1607 if (qemu_get_byte(f
) && !peer_has_ufo(n
)) {
1608 error_report("virtio-net: saved image requires TUN_F_UFO support");
1612 if (n
->max_queues
> 1) {
1613 if (n
->max_queues
!= qemu_get_be16(f
)) {
1614 error_report("virtio-net: different max_queues ");
1618 n
->curr_queues
= qemu_get_be16(f
);
1619 if (n
->curr_queues
> n
->max_queues
) {
1620 error_report("virtio-net: curr_queues %x > max_queues %x",
1621 n
->curr_queues
, n
->max_queues
);
1624 for (i
= 1; i
< n
->curr_queues
; i
++) {
1625 n
->vqs
[i
].tx_waiting
= qemu_get_be32(f
);
1629 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
1630 n
->curr_guest_offloads
= qemu_get_be64(f
);
1632 n
->curr_guest_offloads
= virtio_net_supported_guest_offloads(n
);
1635 if (peer_has_vnet_hdr(n
)) {
1636 virtio_net_apply_guest_offloads(n
);
1639 virtio_net_set_queues(n
);
1641 /* Find the first multicast entry in the saved MAC filter */
1642 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
1643 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
1647 n
->mac_table
.first_multi
= i
;
1649 /* nc.link_down can't be migrated, so infer link_down according
1650 * to link status bit in n->status */
1651 link_down
= (n
->status
& VIRTIO_NET_S_LINK_UP
) == 0;
1652 for (i
= 0; i
< n
->max_queues
; i
++) {
1653 qemu_get_subqueue(n
->nic
, i
)->link_down
= link_down
;
1656 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
1657 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
1658 n
->announce_counter
= SELF_ANNOUNCE_ROUNDS
;
1659 timer_mod(n
->announce_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
));
1665 static NetClientInfo net_virtio_info
= {
1666 .type
= NET_CLIENT_DRIVER_NIC
,
1667 .size
= sizeof(NICState
),
1668 .can_receive
= virtio_net_can_receive
,
1669 .receive
= virtio_net_receive
,
1670 .link_status_changed
= virtio_net_set_link_status
,
1671 .query_rx_filter
= virtio_net_query_rxfilter
,
1674 static bool virtio_net_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
1676 VirtIONet
*n
= VIRTIO_NET(vdev
);
1677 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
1678 assert(n
->vhost_started
);
1679 return vhost_net_virtqueue_pending(get_vhost_net(nc
->peer
), idx
);
1682 static void virtio_net_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
1685 VirtIONet
*n
= VIRTIO_NET(vdev
);
1686 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
1687 assert(n
->vhost_started
);
1688 vhost_net_virtqueue_mask(get_vhost_net(nc
->peer
),
1692 static void virtio_net_set_config_size(VirtIONet
*n
, uint64_t host_features
)
1694 int i
, config_size
= 0;
1695 virtio_add_feature(&host_features
, VIRTIO_NET_F_MAC
);
1696 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
1697 if (host_features
& feature_sizes
[i
].flags
) {
1698 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
1701 n
->config_size
= config_size
;
1704 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,
1708 * The name can be NULL, the netclient name will be type.x.
1710 assert(type
!= NULL
);
1712 g_free(n
->netclient_name
);
1713 g_free(n
->netclient_type
);
1714 n
->netclient_name
= g_strdup(name
);
1715 n
->netclient_type
= g_strdup(type
);
1718 static void virtio_net_device_realize(DeviceState
*dev
, Error
**errp
)
1720 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1721 VirtIONet
*n
= VIRTIO_NET(dev
);
1725 virtio_net_set_config_size(n
, n
->host_features
);
1726 virtio_init(vdev
, "virtio-net", VIRTIO_ID_NET
, n
->config_size
);
1729 * We set a lower limit on RX queue size to what it always was.
1730 * Guests that want a smaller ring can always resize it without
1731 * help from us (using virtio 1 and up).
1733 if (n
->net_conf
.rx_queue_size
< VIRTIO_NET_RX_QUEUE_MIN_SIZE
||
1734 n
->net_conf
.rx_queue_size
> VIRTQUEUE_MAX_SIZE
||
1735 (n
->net_conf
.rx_queue_size
& (n
->net_conf
.rx_queue_size
- 1))) {
1736 error_setg(errp
, "Invalid rx_queue_size (= %" PRIu16
"), "
1737 "must be a power of 2 between %d and %d.",
1738 n
->net_conf
.rx_queue_size
, VIRTIO_NET_RX_QUEUE_MIN_SIZE
,
1739 VIRTQUEUE_MAX_SIZE
);
1740 virtio_cleanup(vdev
);
1744 n
->max_queues
= MAX(n
->nic_conf
.peers
.queues
, 1);
1745 if (n
->max_queues
* 2 + 1 > VIRTIO_QUEUE_MAX
) {
1746 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
1747 "must be a positive integer less than %d.",
1748 n
->max_queues
, (VIRTIO_QUEUE_MAX
- 1) / 2);
1749 virtio_cleanup(vdev
);
1752 n
->vqs
= g_malloc0(sizeof(VirtIONetQueue
) * n
->max_queues
);
1754 n
->tx_timeout
= n
->net_conf
.txtimer
;
1756 if (n
->net_conf
.tx
&& strcmp(n
->net_conf
.tx
, "timer")
1757 && strcmp(n
->net_conf
.tx
, "bh")) {
1758 error_report("virtio-net: "
1759 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1761 error_report("Defaulting to \"bh\"");
1764 for (i
= 0; i
< n
->max_queues
; i
++) {
1765 virtio_net_add_queue(n
, i
);
1768 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
1769 qemu_macaddr_default_if_unset(&n
->nic_conf
.macaddr
);
1770 memcpy(&n
->mac
[0], &n
->nic_conf
.macaddr
, sizeof(n
->mac
));
1771 n
->status
= VIRTIO_NET_S_LINK_UP
;
1772 n
->announce_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
1773 virtio_net_announce_timer
, n
);
1775 if (n
->netclient_type
) {
1777 * Happen when virtio_net_set_netclient_name has been called.
1779 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
1780 n
->netclient_type
, n
->netclient_name
, n
);
1782 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
1783 object_get_typename(OBJECT(dev
)), dev
->id
, n
);
1786 peer_test_vnet_hdr(n
);
1787 if (peer_has_vnet_hdr(n
)) {
1788 for (i
= 0; i
< n
->max_queues
; i
++) {
1789 qemu_using_vnet_hdr(qemu_get_subqueue(n
->nic
, i
)->peer
, true);
1791 n
->host_hdr_len
= sizeof(struct virtio_net_hdr
);
1793 n
->host_hdr_len
= 0;
1796 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->nic_conf
.macaddr
.a
);
1798 n
->vqs
[0].tx_waiting
= 0;
1799 n
->tx_burst
= n
->net_conf
.txburst
;
1800 virtio_net_set_mrg_rx_bufs(n
, 0, 0);
1801 n
->promisc
= 1; /* for compatibility */
1803 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1805 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
1807 nc
= qemu_get_queue(n
->nic
);
1808 nc
->rxfilter_notify_enabled
= 1;
1813 static void virtio_net_device_unrealize(DeviceState
*dev
, Error
**errp
)
1815 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1816 VirtIONet
*n
= VIRTIO_NET(dev
);
1819 /* This will stop vhost backend if appropriate. */
1820 virtio_net_set_status(vdev
, 0);
1822 g_free(n
->netclient_name
);
1823 n
->netclient_name
= NULL
;
1824 g_free(n
->netclient_type
);
1825 n
->netclient_type
= NULL
;
1827 g_free(n
->mac_table
.macs
);
1830 max_queues
= n
->multiqueue
? n
->max_queues
: 1;
1831 for (i
= 0; i
< max_queues
; i
++) {
1832 virtio_net_del_queue(n
, i
);
1835 timer_del(n
->announce_timer
);
1836 timer_free(n
->announce_timer
);
1838 qemu_del_nic(n
->nic
);
1839 virtio_cleanup(vdev
);
1842 static void virtio_net_instance_init(Object
*obj
)
1844 VirtIONet
*n
= VIRTIO_NET(obj
);
1847 * The default config_size is sizeof(struct virtio_net_config).
1848 * Can be overriden with virtio_net_set_config_size.
1850 n
->config_size
= sizeof(struct virtio_net_config
);
1851 device_add_bootindex_property(obj
, &n
->nic_conf
.bootindex
,
1852 "bootindex", "/ethernet-phy@0",
1856 static void virtio_net_pre_save(void *opaque
)
1858 VirtIONet
*n
= opaque
;
1860 /* At this point, backend must be stopped, otherwise
1861 * it might keep writing to memory. */
1862 assert(!n
->vhost_started
);
1865 static const VMStateDescription vmstate_virtio_net
= {
1866 .name
= "virtio-net",
1867 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
1868 .version_id
= VIRTIO_NET_VM_VERSION
,
1869 .fields
= (VMStateField
[]) {
1870 VMSTATE_VIRTIO_DEVICE
,
1871 VMSTATE_END_OF_LIST()
1873 .pre_save
= virtio_net_pre_save
,
1876 static Property virtio_net_properties
[] = {
1877 DEFINE_PROP_BIT("csum", VirtIONet
, host_features
, VIRTIO_NET_F_CSUM
, true),
1878 DEFINE_PROP_BIT("guest_csum", VirtIONet
, host_features
,
1879 VIRTIO_NET_F_GUEST_CSUM
, true),
1880 DEFINE_PROP_BIT("gso", VirtIONet
, host_features
, VIRTIO_NET_F_GSO
, true),
1881 DEFINE_PROP_BIT("guest_tso4", VirtIONet
, host_features
,
1882 VIRTIO_NET_F_GUEST_TSO4
, true),
1883 DEFINE_PROP_BIT("guest_tso6", VirtIONet
, host_features
,
1884 VIRTIO_NET_F_GUEST_TSO6
, true),
1885 DEFINE_PROP_BIT("guest_ecn", VirtIONet
, host_features
,
1886 VIRTIO_NET_F_GUEST_ECN
, true),
1887 DEFINE_PROP_BIT("guest_ufo", VirtIONet
, host_features
,
1888 VIRTIO_NET_F_GUEST_UFO
, true),
1889 DEFINE_PROP_BIT("guest_announce", VirtIONet
, host_features
,
1890 VIRTIO_NET_F_GUEST_ANNOUNCE
, true),
1891 DEFINE_PROP_BIT("host_tso4", VirtIONet
, host_features
,
1892 VIRTIO_NET_F_HOST_TSO4
, true),
1893 DEFINE_PROP_BIT("host_tso6", VirtIONet
, host_features
,
1894 VIRTIO_NET_F_HOST_TSO6
, true),
1895 DEFINE_PROP_BIT("host_ecn", VirtIONet
, host_features
,
1896 VIRTIO_NET_F_HOST_ECN
, true),
1897 DEFINE_PROP_BIT("host_ufo", VirtIONet
, host_features
,
1898 VIRTIO_NET_F_HOST_UFO
, true),
1899 DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet
, host_features
,
1900 VIRTIO_NET_F_MRG_RXBUF
, true),
1901 DEFINE_PROP_BIT("status", VirtIONet
, host_features
,
1902 VIRTIO_NET_F_STATUS
, true),
1903 DEFINE_PROP_BIT("ctrl_vq", VirtIONet
, host_features
,
1904 VIRTIO_NET_F_CTRL_VQ
, true),
1905 DEFINE_PROP_BIT("ctrl_rx", VirtIONet
, host_features
,
1906 VIRTIO_NET_F_CTRL_RX
, true),
1907 DEFINE_PROP_BIT("ctrl_vlan", VirtIONet
, host_features
,
1908 VIRTIO_NET_F_CTRL_VLAN
, true),
1909 DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet
, host_features
,
1910 VIRTIO_NET_F_CTRL_RX_EXTRA
, true),
1911 DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet
, host_features
,
1912 VIRTIO_NET_F_CTRL_MAC_ADDR
, true),
1913 DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet
, host_features
,
1914 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
, true),
1915 DEFINE_PROP_BIT("mq", VirtIONet
, host_features
, VIRTIO_NET_F_MQ
, false),
1916 DEFINE_NIC_PROPERTIES(VirtIONet
, nic_conf
),
1917 DEFINE_PROP_UINT32("x-txtimer", VirtIONet
, net_conf
.txtimer
,
1919 DEFINE_PROP_INT32("x-txburst", VirtIONet
, net_conf
.txburst
, TX_BURST
),
1920 DEFINE_PROP_STRING("tx", VirtIONet
, net_conf
.tx
),
1921 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet
, net_conf
.rx_queue_size
,
1922 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
),
1923 DEFINE_PROP_END_OF_LIST(),
1926 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
1928 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1929 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1931 dc
->props
= virtio_net_properties
;
1932 dc
->vmsd
= &vmstate_virtio_net
;
1933 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1934 vdc
->realize
= virtio_net_device_realize
;
1935 vdc
->unrealize
= virtio_net_device_unrealize
;
1936 vdc
->get_config
= virtio_net_get_config
;
1937 vdc
->set_config
= virtio_net_set_config
;
1938 vdc
->get_features
= virtio_net_get_features
;
1939 vdc
->set_features
= virtio_net_set_features
;
1940 vdc
->bad_features
= virtio_net_bad_features
;
1941 vdc
->reset
= virtio_net_reset
;
1942 vdc
->set_status
= virtio_net_set_status
;
1943 vdc
->guest_notifier_mask
= virtio_net_guest_notifier_mask
;
1944 vdc
->guest_notifier_pending
= virtio_net_guest_notifier_pending
;
1945 vdc
->load
= virtio_net_load_device
;
1946 vdc
->save
= virtio_net_save_device
;
1949 static const TypeInfo virtio_net_info
= {
1950 .name
= TYPE_VIRTIO_NET
,
1951 .parent
= TYPE_VIRTIO_DEVICE
,
1952 .instance_size
= sizeof(VirtIONet
),
1953 .instance_init
= virtio_net_instance_init
,
1954 .class_init
= virtio_net_class_init
,
1957 static void virtio_register_types(void)
1959 type_register_static(&virtio_net_info
);
1962 type_init(virtio_register_types
)