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 #include "qemu/osdep.h"
15 #include "qemu/atomic.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/module.h"
20 #include "hw/virtio/virtio.h"
22 #include "net/checksum.h"
24 #include "qemu/error-report.h"
25 #include "qemu/timer.h"
26 #include "qemu/option.h"
27 #include "qemu/option_int.h"
28 #include "qemu/config-file.h"
29 #include "qapi/qmp/qdict.h"
30 #include "hw/virtio/virtio-net.h"
31 #include "net/vhost_net.h"
32 #include "net/announce.h"
33 #include "hw/virtio/virtio-bus.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-events-net.h"
36 #include "hw/qdev-properties.h"
37 #include "qapi/qapi-types-migration.h"
38 #include "qapi/qapi-events-migration.h"
39 #include "hw/virtio/virtio-access.h"
40 #include "migration/misc.h"
41 #include "standard-headers/linux/ethtool.h"
42 #include "sysemu/sysemu.h"
43 #include "sysemu/replay.h"
45 #include "monitor/qdev.h"
46 #include "monitor/monitor.h"
47 #include "hw/pci/pci_device.h"
48 #include "net_rx_pkt.h"
49 #include "hw/virtio/vhost.h"
50 #include "sysemu/qtest.h"
52 #define VIRTIO_NET_VM_VERSION 11
54 /* previously fixed value */
55 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
56 #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
58 /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */
59 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
60 #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
62 #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */
64 #define VIRTIO_NET_TCP_FLAG 0x3F
65 #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000
67 /* IPv4 max payload, 16 bits in the header */
68 #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
69 #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
71 /* header length value in ip header without option */
72 #define VIRTIO_NET_IP4_HEADER_LENGTH 5
74 #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */
75 #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
77 /* Purge coalesced packets timer interval, This value affects the performance
78 a lot, and should be tuned carefully, '300000'(300us) is the recommended
79 value to pass the WHQL test, '50000' can gain 2x netperf throughput with
81 #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
83 #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
84 VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
85 VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
86 VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
87 VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
88 VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
89 VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
90 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
91 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
93 static const VirtIOFeature feature_sizes
[] = {
94 {.flags
= 1ULL << VIRTIO_NET_F_MAC
,
95 .end
= endof(struct virtio_net_config
, mac
)},
96 {.flags
= 1ULL << VIRTIO_NET_F_STATUS
,
97 .end
= endof(struct virtio_net_config
, status
)},
98 {.flags
= 1ULL << VIRTIO_NET_F_MQ
,
99 .end
= endof(struct virtio_net_config
, max_virtqueue_pairs
)},
100 {.flags
= 1ULL << VIRTIO_NET_F_MTU
,
101 .end
= endof(struct virtio_net_config
, mtu
)},
102 {.flags
= 1ULL << VIRTIO_NET_F_SPEED_DUPLEX
,
103 .end
= endof(struct virtio_net_config
, duplex
)},
104 {.flags
= (1ULL << VIRTIO_NET_F_RSS
) | (1ULL << VIRTIO_NET_F_HASH_REPORT
),
105 .end
= endof(struct virtio_net_config
, supported_hash_types
)},
109 static const VirtIOConfigSizeParams cfg_size_params
= {
110 .min_size
= endof(struct virtio_net_config
, mac
),
111 .max_size
= sizeof(struct virtio_net_config
),
112 .feature_sizes
= feature_sizes
115 static VirtIONetQueue
*virtio_net_get_subqueue(NetClientState
*nc
)
117 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
119 return &n
->vqs
[nc
->queue_index
];
122 static int vq2q(int queue_index
)
124 return queue_index
/ 2;
127 static void flush_or_purge_queued_packets(NetClientState
*nc
)
133 qemu_flush_or_purge_queued_packets(nc
->peer
, true);
134 assert(!virtio_net_get_subqueue(nc
)->async_tx
.elem
);
138 * - we could suppress RX interrupt if we were so inclined.
141 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
143 VirtIONet
*n
= VIRTIO_NET(vdev
);
144 struct virtio_net_config netcfg
;
145 NetClientState
*nc
= qemu_get_queue(n
->nic
);
146 static const MACAddr zero
= { .a
= { 0, 0, 0, 0, 0, 0 } };
149 memset(&netcfg
, 0 , sizeof(struct virtio_net_config
));
150 virtio_stw_p(vdev
, &netcfg
.status
, n
->status
);
151 virtio_stw_p(vdev
, &netcfg
.max_virtqueue_pairs
, n
->max_queue_pairs
);
152 virtio_stw_p(vdev
, &netcfg
.mtu
, n
->net_conf
.mtu
);
153 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
154 virtio_stl_p(vdev
, &netcfg
.speed
, n
->net_conf
.speed
);
155 netcfg
.duplex
= n
->net_conf
.duplex
;
156 netcfg
.rss_max_key_size
= VIRTIO_NET_RSS_MAX_KEY_SIZE
;
157 virtio_stw_p(vdev
, &netcfg
.rss_max_indirection_table_length
,
158 virtio_host_has_feature(vdev
, VIRTIO_NET_F_RSS
) ?
159 VIRTIO_NET_RSS_MAX_TABLE_LEN
: 1);
160 virtio_stl_p(vdev
, &netcfg
.supported_hash_types
,
161 VIRTIO_NET_RSS_SUPPORTED_HASHES
);
162 memcpy(config
, &netcfg
, n
->config_size
);
165 * Is this VDPA? No peer means not VDPA: there's no way to
166 * disconnect/reconnect a VDPA peer.
168 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
169 ret
= vhost_net_get_config(get_vhost_net(nc
->peer
), (uint8_t *)&netcfg
,
176 * Some NIC/kernel combinations present 0 as the mac address. As that
177 * is not a legal address, try to proceed with the address from the
178 * QEMU command line in the hope that the address has been configured
179 * correctly elsewhere - just not reported by the device.
181 if (memcmp(&netcfg
.mac
, &zero
, sizeof(zero
)) == 0) {
182 info_report("Zero hardware mac address detected. Ignoring.");
183 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
186 netcfg
.status
|= virtio_tswap16(vdev
,
187 n
->status
& VIRTIO_NET_S_ANNOUNCE
);
188 memcpy(config
, &netcfg
, n
->config_size
);
192 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
194 VirtIONet
*n
= VIRTIO_NET(vdev
);
195 struct virtio_net_config netcfg
= {};
196 NetClientState
*nc
= qemu_get_queue(n
->nic
);
198 memcpy(&netcfg
, config
, n
->config_size
);
200 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_MAC_ADDR
) &&
201 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
202 memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
203 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
204 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
208 * Is this VDPA? No peer means not VDPA: there's no way to
209 * disconnect/reconnect a VDPA peer.
211 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
212 vhost_net_set_config(get_vhost_net(nc
->peer
),
213 (uint8_t *)&netcfg
, 0, n
->config_size
,
214 VHOST_SET_CONFIG_TYPE_FRONTEND
);
218 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
220 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
221 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
222 (n
->status
& VIRTIO_NET_S_LINK_UP
) && vdev
->vm_running
;
225 static void virtio_net_announce_notify(VirtIONet
*net
)
227 VirtIODevice
*vdev
= VIRTIO_DEVICE(net
);
228 trace_virtio_net_announce_notify();
230 net
->status
|= VIRTIO_NET_S_ANNOUNCE
;
231 virtio_notify_config(vdev
);
234 static void virtio_net_announce_timer(void *opaque
)
236 VirtIONet
*n
= opaque
;
237 trace_virtio_net_announce_timer(n
->announce_timer
.round
);
239 n
->announce_timer
.round
--;
240 virtio_net_announce_notify(n
);
243 static void virtio_net_announce(NetClientState
*nc
)
245 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
246 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
249 * Make sure the virtio migration announcement timer isn't running
250 * If it is, let it trigger announcement so that we do not cause
253 if (n
->announce_timer
.round
) {
257 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
258 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
259 virtio_net_announce_notify(n
);
263 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
265 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
266 NetClientState
*nc
= qemu_get_queue(n
->nic
);
267 int queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
268 int cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
269 n
->max_ncs
- n
->max_queue_pairs
: 0;
271 if (!get_vhost_net(nc
->peer
)) {
275 if ((virtio_net_started(n
, status
) && !nc
->peer
->link_down
) ==
276 !!n
->vhost_started
) {
279 if (!n
->vhost_started
) {
282 if (n
->needs_vnet_hdr_swap
) {
283 error_report("backend does not support %s vnet headers; "
284 "falling back on userspace virtio",
285 virtio_is_big_endian(vdev
) ? "BE" : "LE");
289 /* Any packets outstanding? Purge them to avoid touching rings
290 * when vhost is running.
292 for (i
= 0; i
< queue_pairs
; i
++) {
293 NetClientState
*qnc
= qemu_get_subqueue(n
->nic
, i
);
295 /* Purge both directions: TX and RX. */
296 qemu_net_queue_purge(qnc
->peer
->incoming_queue
, qnc
);
297 qemu_net_queue_purge(qnc
->incoming_queue
, qnc
->peer
);
300 if (virtio_has_feature(vdev
->guest_features
, VIRTIO_NET_F_MTU
)) {
301 r
= vhost_net_set_mtu(get_vhost_net(nc
->peer
), n
->net_conf
.mtu
);
303 error_report("%uBytes MTU not supported by the backend",
310 n
->vhost_started
= 1;
311 r
= vhost_net_start(vdev
, n
->nic
->ncs
, queue_pairs
, cvq
);
313 error_report("unable to start vhost net: %d: "
314 "falling back on userspace virtio", -r
);
315 n
->vhost_started
= 0;
318 vhost_net_stop(vdev
, n
->nic
->ncs
, queue_pairs
, cvq
);
319 n
->vhost_started
= 0;
323 static int virtio_net_set_vnet_endian_one(VirtIODevice
*vdev
,
324 NetClientState
*peer
,
327 if (virtio_is_big_endian(vdev
)) {
328 return qemu_set_vnet_be(peer
, enable
);
330 return qemu_set_vnet_le(peer
, enable
);
334 static bool virtio_net_set_vnet_endian(VirtIODevice
*vdev
, NetClientState
*ncs
,
335 int queue_pairs
, bool enable
)
339 for (i
= 0; i
< queue_pairs
; i
++) {
340 if (virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, enable
) < 0 &&
343 virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, false);
353 static void virtio_net_vnet_endian_status(VirtIONet
*n
, uint8_t status
)
355 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
356 int queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
358 if (virtio_net_started(n
, status
)) {
359 /* Before using the device, we tell the network backend about the
360 * endianness to use when parsing vnet headers. If the backend
361 * can't do it, we fallback onto fixing the headers in the core
364 n
->needs_vnet_hdr_swap
= n
->has_vnet_hdr
&&
365 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
,
367 } else if (virtio_net_started(n
, vdev
->status
)) {
368 /* After using the device, we need to reset the network backend to
369 * the default (guest native endianness), otherwise the guest may
370 * lose network connectivity if it is rebooted into a different
373 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
, queue_pairs
, false);
377 static void virtio_net_drop_tx_queue_data(VirtIODevice
*vdev
, VirtQueue
*vq
)
379 unsigned int dropped
= virtqueue_drop_all(vq
);
381 virtio_notify(vdev
, vq
);
385 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
387 VirtIONet
*n
= VIRTIO_NET(vdev
);
390 uint8_t queue_status
;
392 virtio_net_vnet_endian_status(n
, status
);
393 virtio_net_vhost_status(n
, status
);
395 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
396 NetClientState
*ncs
= qemu_get_subqueue(n
->nic
, i
);
400 if ((!n
->multiqueue
&& i
!= 0) || i
>= n
->curr_queue_pairs
) {
403 queue_status
= status
;
406 virtio_net_started(n
, queue_status
) && !n
->vhost_started
;
409 qemu_flush_queued_packets(ncs
);
412 if (!q
->tx_waiting
) {
418 timer_mod(q
->tx_timer
,
419 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
421 replay_bh_schedule_event(q
->tx_bh
);
425 timer_del(q
->tx_timer
);
427 qemu_bh_cancel(q
->tx_bh
);
429 if ((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0 &&
430 (queue_status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
432 /* if tx is waiting we are likely have some packets in tx queue
433 * and disabled notification */
435 virtio_queue_set_notification(q
->tx_vq
, 1);
436 virtio_net_drop_tx_queue_data(vdev
, q
->tx_vq
);
442 static void virtio_net_set_link_status(NetClientState
*nc
)
444 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
445 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
446 uint16_t old_status
= n
->status
;
449 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
451 n
->status
|= VIRTIO_NET_S_LINK_UP
;
453 if (n
->status
!= old_status
)
454 virtio_notify_config(vdev
);
456 virtio_net_set_status(vdev
, vdev
->status
);
459 static void rxfilter_notify(NetClientState
*nc
)
461 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
463 if (nc
->rxfilter_notify_enabled
) {
464 char *path
= object_get_canonical_path(OBJECT(n
->qdev
));
465 qapi_event_send_nic_rx_filter_changed(n
->netclient_name
, path
);
468 /* disable event notification to avoid events flooding */
469 nc
->rxfilter_notify_enabled
= 0;
473 static intList
*get_vlan_table(VirtIONet
*n
)
479 for (i
= 0; i
< MAX_VLAN
>> 5; i
++) {
480 for (j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
481 if (n
->vlans
[i
] & (1U << j
)) {
482 QAPI_LIST_PREPEND(list
, (i
<< 5) + j
);
490 static RxFilterInfo
*virtio_net_query_rxfilter(NetClientState
*nc
)
492 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
493 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
498 info
= g_malloc0(sizeof(*info
));
499 info
->name
= g_strdup(nc
->name
);
500 info
->promiscuous
= n
->promisc
;
503 info
->unicast
= RX_STATE_NONE
;
504 } else if (n
->alluni
) {
505 info
->unicast
= RX_STATE_ALL
;
507 info
->unicast
= RX_STATE_NORMAL
;
511 info
->multicast
= RX_STATE_NONE
;
512 } else if (n
->allmulti
) {
513 info
->multicast
= RX_STATE_ALL
;
515 info
->multicast
= RX_STATE_NORMAL
;
518 info
->broadcast_allowed
= n
->nobcast
;
519 info
->multicast_overflow
= n
->mac_table
.multi_overflow
;
520 info
->unicast_overflow
= n
->mac_table
.uni_overflow
;
522 info
->main_mac
= qemu_mac_strdup_printf(n
->mac
);
525 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
526 QAPI_LIST_PREPEND(str_list
,
527 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
529 info
->unicast_table
= str_list
;
532 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
533 QAPI_LIST_PREPEND(str_list
,
534 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
536 info
->multicast_table
= str_list
;
537 info
->vlan_table
= get_vlan_table(n
);
539 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VLAN
)) {
540 info
->vlan
= RX_STATE_ALL
;
541 } else if (!info
->vlan_table
) {
542 info
->vlan
= RX_STATE_NONE
;
544 info
->vlan
= RX_STATE_NORMAL
;
547 /* enable event notification after query */
548 nc
->rxfilter_notify_enabled
= 1;
553 static void virtio_net_queue_reset(VirtIODevice
*vdev
, uint32_t queue_index
)
555 VirtIONet
*n
= VIRTIO_NET(vdev
);
558 /* validate queue_index and skip for cvq */
559 if (queue_index
>= n
->max_queue_pairs
* 2) {
563 nc
= qemu_get_subqueue(n
->nic
, vq2q(queue_index
));
569 if (get_vhost_net(nc
->peer
) &&
570 nc
->peer
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
571 vhost_net_virtqueue_reset(vdev
, nc
, queue_index
);
574 flush_or_purge_queued_packets(nc
);
577 static void virtio_net_queue_enable(VirtIODevice
*vdev
, uint32_t queue_index
)
579 VirtIONet
*n
= VIRTIO_NET(vdev
);
583 /* validate queue_index and skip for cvq */
584 if (queue_index
>= n
->max_queue_pairs
* 2) {
588 nc
= qemu_get_subqueue(n
->nic
, vq2q(queue_index
));
590 if (!nc
->peer
|| !vdev
->vhost_started
) {
594 if (get_vhost_net(nc
->peer
) &&
595 nc
->peer
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
596 r
= vhost_net_virtqueue_restart(vdev
, nc
, queue_index
);
598 error_report("unable to restart vhost net virtqueue: %d, "
599 "when resetting the queue", queue_index
);
604 static void peer_test_vnet_hdr(VirtIONet
*n
)
606 NetClientState
*nc
= qemu_get_queue(n
->nic
);
611 n
->has_vnet_hdr
= qemu_has_vnet_hdr(nc
->peer
);
614 static int peer_has_vnet_hdr(VirtIONet
*n
)
616 return n
->has_vnet_hdr
;
619 static int peer_has_ufo(VirtIONet
*n
)
621 if (!peer_has_vnet_hdr(n
))
624 n
->has_ufo
= qemu_has_ufo(qemu_get_queue(n
->nic
)->peer
);
629 static int peer_has_uso(VirtIONet
*n
)
631 if (!peer_has_vnet_hdr(n
)) {
635 return qemu_has_uso(qemu_get_queue(n
->nic
)->peer
);
638 static void virtio_net_set_mrg_rx_bufs(VirtIONet
*n
, int mergeable_rx_bufs
,
639 int version_1
, int hash_report
)
644 n
->mergeable_rx_bufs
= mergeable_rx_bufs
;
647 n
->guest_hdr_len
= hash_report
?
648 sizeof(struct virtio_net_hdr_v1_hash
) :
649 sizeof(struct virtio_net_hdr_mrg_rxbuf
);
650 n
->rss_data
.populate_hash
= !!hash_report
;
652 n
->guest_hdr_len
= n
->mergeable_rx_bufs
?
653 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
654 sizeof(struct virtio_net_hdr
);
655 n
->rss_data
.populate_hash
= false;
658 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
659 nc
= qemu_get_subqueue(n
->nic
, i
);
661 if (peer_has_vnet_hdr(n
) &&
662 qemu_has_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
)) {
663 qemu_set_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
);
664 n
->host_hdr_len
= n
->guest_hdr_len
;
669 static int virtio_net_max_tx_queue_size(VirtIONet
*n
)
671 NetClientState
*peer
= n
->nic_conf
.peers
.ncs
[0];
674 * Backends other than vhost-user or vhost-vdpa don't support max queue
678 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
681 switch(peer
->info
->type
) {
682 case NET_CLIENT_DRIVER_VHOST_USER
:
683 case NET_CLIENT_DRIVER_VHOST_VDPA
:
684 return VIRTQUEUE_MAX_SIZE
;
686 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
690 static int peer_attach(VirtIONet
*n
, int index
)
692 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
698 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
699 vhost_set_vring_enable(nc
->peer
, 1);
702 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
706 if (n
->max_queue_pairs
== 1) {
710 return tap_enable(nc
->peer
);
713 static int peer_detach(VirtIONet
*n
, int index
)
715 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
721 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
722 vhost_set_vring_enable(nc
->peer
, 0);
725 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
729 return tap_disable(nc
->peer
);
732 static void virtio_net_set_queue_pairs(VirtIONet
*n
)
737 if (n
->nic
->peer_deleted
) {
741 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
742 if (i
< n
->curr_queue_pairs
) {
743 r
= peer_attach(n
, i
);
746 r
= peer_detach(n
, i
);
752 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
);
754 static uint64_t virtio_net_get_features(VirtIODevice
*vdev
, uint64_t features
,
757 VirtIONet
*n
= VIRTIO_NET(vdev
);
758 NetClientState
*nc
= qemu_get_queue(n
->nic
);
760 /* Firstly sync all virtio-net possible supported features */
761 features
|= n
->host_features
;
763 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
765 if (!peer_has_vnet_hdr(n
)) {
766 virtio_clear_feature(&features
, VIRTIO_NET_F_CSUM
);
767 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
768 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
769 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
771 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_CSUM
);
772 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO4
);
773 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO6
);
774 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ECN
);
776 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_USO
);
777 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO4
);
778 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO6
);
780 virtio_clear_feature(&features
, VIRTIO_NET_F_HASH_REPORT
);
783 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
784 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_UFO
);
785 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_UFO
);
788 if (!peer_has_uso(n
)) {
789 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_USO
);
790 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO4
);
791 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO6
);
794 if (!get_vhost_net(nc
->peer
)) {
798 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
799 virtio_clear_feature(&features
, VIRTIO_NET_F_RSS
);
801 features
= vhost_net_get_features(get_vhost_net(nc
->peer
), features
);
802 vdev
->backend_features
= features
;
804 if (n
->mtu_bypass_backend
&&
805 (n
->host_features
& 1ULL << VIRTIO_NET_F_MTU
)) {
806 features
|= (1ULL << VIRTIO_NET_F_MTU
);
810 * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
811 * enabled. This happens in the vDPA case.
813 * Make sure the feature set is not incoherent, as the driver could refuse
816 * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
817 * helping guest to notify the new location with vDPA devices that does not
820 if (!virtio_has_feature(vdev
->backend_features
, VIRTIO_NET_F_CTRL_VQ
)) {
821 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ANNOUNCE
);
827 static uint64_t virtio_net_bad_features(VirtIODevice
*vdev
)
829 uint64_t features
= 0;
831 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
833 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
834 virtio_add_feature(&features
, VIRTIO_NET_F_CSUM
);
835 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
836 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
837 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
842 static void virtio_net_apply_guest_offloads(VirtIONet
*n
)
844 qemu_set_offload(qemu_get_queue(n
->nic
)->peer
,
845 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_CSUM
)),
846 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO4
)),
847 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO6
)),
848 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_ECN
)),
849 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_UFO
)),
850 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_USO4
)),
851 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_USO6
)));
854 static uint64_t virtio_net_guest_offloads_by_features(uint64_t features
)
856 static const uint64_t guest_offloads_mask
=
857 (1ULL << VIRTIO_NET_F_GUEST_CSUM
) |
858 (1ULL << VIRTIO_NET_F_GUEST_TSO4
) |
859 (1ULL << VIRTIO_NET_F_GUEST_TSO6
) |
860 (1ULL << VIRTIO_NET_F_GUEST_ECN
) |
861 (1ULL << VIRTIO_NET_F_GUEST_UFO
) |
862 (1ULL << VIRTIO_NET_F_GUEST_USO4
) |
863 (1ULL << VIRTIO_NET_F_GUEST_USO6
);
865 return guest_offloads_mask
& features
;
868 uint64_t virtio_net_supported_guest_offloads(const VirtIONet
*n
)
870 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
871 return virtio_net_guest_offloads_by_features(vdev
->guest_features
);
880 * Set the failover primary device
882 * @opaque: FailoverId to setup
883 * @opts: opts for device we are handling
884 * @errp: returns an error if this function fails
886 static int failover_set_primary(DeviceState
*dev
, void *opaque
)
888 FailoverDevice
*fdev
= opaque
;
889 PCIDevice
*pci_dev
= (PCIDevice
*)
890 object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
);
896 if (!g_strcmp0(pci_dev
->failover_pair_id
, fdev
->n
->netclient_name
)) {
905 * Find the primary device for this failover virtio-net
907 * @n: VirtIONet device
908 * @errp: returns an error if this function fails
910 static DeviceState
*failover_find_primary_device(VirtIONet
*n
)
912 FailoverDevice fdev
= {
916 qbus_walk_children(sysbus_get_default(), failover_set_primary
, NULL
,
921 static void failover_add_primary(VirtIONet
*n
, Error
**errp
)
924 DeviceState
*dev
= failover_find_primary_device(n
);
930 if (!n
->primary_opts
) {
931 error_setg(errp
, "Primary device not found");
932 error_append_hint(errp
, "Virtio-net failover will not work. Make "
933 "sure primary device has parameter"
934 " failover_pair_id=%s\n", n
->netclient_name
);
938 dev
= qdev_device_add_from_qdict(n
->primary_opts
,
939 n
->primary_opts_from_json
,
942 qobject_unref(n
->primary_opts
);
943 n
->primary_opts
= NULL
;
945 object_unref(OBJECT(dev
));
947 error_propagate(errp
, err
);
950 static void virtio_net_set_features(VirtIODevice
*vdev
, uint64_t features
)
952 VirtIONet
*n
= VIRTIO_NET(vdev
);
956 if (n
->mtu_bypass_backend
&&
957 !virtio_has_feature(vdev
->backend_features
, VIRTIO_NET_F_MTU
)) {
958 features
&= ~(1ULL << VIRTIO_NET_F_MTU
);
961 virtio_net_set_multiqueue(n
,
962 virtio_has_feature(features
, VIRTIO_NET_F_RSS
) ||
963 virtio_has_feature(features
, VIRTIO_NET_F_MQ
));
965 virtio_net_set_mrg_rx_bufs(n
,
966 virtio_has_feature(features
,
967 VIRTIO_NET_F_MRG_RXBUF
),
968 virtio_has_feature(features
,
970 virtio_has_feature(features
,
971 VIRTIO_NET_F_HASH_REPORT
));
973 n
->rsc4_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
974 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO4
);
975 n
->rsc6_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
976 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO6
);
977 n
->rss_data
.redirect
= virtio_has_feature(features
, VIRTIO_NET_F_RSS
);
979 if (n
->has_vnet_hdr
) {
980 n
->curr_guest_offloads
=
981 virtio_net_guest_offloads_by_features(features
);
982 virtio_net_apply_guest_offloads(n
);
985 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
986 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
988 if (!get_vhost_net(nc
->peer
)) {
991 vhost_net_ack_features(get_vhost_net(nc
->peer
), features
);
994 * keep acked_features in NetVhostUserState up-to-date so it
995 * can't miss any features configured by guest virtio driver.
997 vhost_net_save_acked_features(nc
->peer
);
1000 if (!virtio_has_feature(features
, VIRTIO_NET_F_CTRL_VLAN
)) {
1001 memset(n
->vlans
, 0xff, MAX_VLAN
>> 3);
1004 if (virtio_has_feature(features
, VIRTIO_NET_F_STANDBY
)) {
1005 qapi_event_send_failover_negotiated(n
->netclient_name
);
1006 qatomic_set(&n
->failover_primary_hidden
, false);
1007 failover_add_primary(n
, &err
);
1009 if (!qtest_enabled()) {
1010 warn_report_err(err
);
1018 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
1019 struct iovec
*iov
, unsigned int iov_cnt
)
1023 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1025 s
= iov_to_buf(iov
, iov_cnt
, 0, &on
, sizeof(on
));
1026 if (s
!= sizeof(on
)) {
1027 return VIRTIO_NET_ERR
;
1030 if (cmd
== VIRTIO_NET_CTRL_RX_PROMISC
) {
1032 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLMULTI
) {
1034 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLUNI
) {
1036 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOMULTI
) {
1038 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOUNI
) {
1040 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOBCAST
) {
1043 return VIRTIO_NET_ERR
;
1046 rxfilter_notify(nc
);
1048 return VIRTIO_NET_OK
;
1051 static int virtio_net_handle_offloads(VirtIONet
*n
, uint8_t cmd
,
1052 struct iovec
*iov
, unsigned int iov_cnt
)
1054 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1058 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
1059 return VIRTIO_NET_ERR
;
1062 s
= iov_to_buf(iov
, iov_cnt
, 0, &offloads
, sizeof(offloads
));
1063 if (s
!= sizeof(offloads
)) {
1064 return VIRTIO_NET_ERR
;
1067 if (cmd
== VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
) {
1068 uint64_t supported_offloads
;
1070 offloads
= virtio_ldq_p(vdev
, &offloads
);
1072 if (!n
->has_vnet_hdr
) {
1073 return VIRTIO_NET_ERR
;
1076 n
->rsc4_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1077 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO4
);
1078 n
->rsc6_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1079 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO6
);
1080 virtio_clear_feature(&offloads
, VIRTIO_NET_F_RSC_EXT
);
1082 supported_offloads
= virtio_net_supported_guest_offloads(n
);
1083 if (offloads
& ~supported_offloads
) {
1084 return VIRTIO_NET_ERR
;
1087 n
->curr_guest_offloads
= offloads
;
1088 virtio_net_apply_guest_offloads(n
);
1090 return VIRTIO_NET_OK
;
1092 return VIRTIO_NET_ERR
;
1096 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
1097 struct iovec
*iov
, unsigned int iov_cnt
)
1099 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1100 struct virtio_net_ctrl_mac mac_data
;
1102 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1104 if (cmd
== VIRTIO_NET_CTRL_MAC_ADDR_SET
) {
1105 if (iov_size(iov
, iov_cnt
) != sizeof(n
->mac
)) {
1106 return VIRTIO_NET_ERR
;
1108 s
= iov_to_buf(iov
, iov_cnt
, 0, &n
->mac
, sizeof(n
->mac
));
1109 assert(s
== sizeof(n
->mac
));
1110 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
1111 rxfilter_notify(nc
);
1113 return VIRTIO_NET_OK
;
1116 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
) {
1117 return VIRTIO_NET_ERR
;
1121 int first_multi
= 0;
1122 uint8_t uni_overflow
= 0;
1123 uint8_t multi_overflow
= 0;
1124 uint8_t *macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1126 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1127 sizeof(mac_data
.entries
));
1128 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1129 if (s
!= sizeof(mac_data
.entries
)) {
1132 iov_discard_front(&iov
, &iov_cnt
, s
);
1134 if (mac_data
.entries
* ETH_ALEN
> iov_size(iov
, iov_cnt
)) {
1138 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
1139 s
= iov_to_buf(iov
, iov_cnt
, 0, macs
,
1140 mac_data
.entries
* ETH_ALEN
);
1141 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1144 in_use
+= mac_data
.entries
;
1149 iov_discard_front(&iov
, &iov_cnt
, mac_data
.entries
* ETH_ALEN
);
1151 first_multi
= in_use
;
1153 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1154 sizeof(mac_data
.entries
));
1155 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1156 if (s
!= sizeof(mac_data
.entries
)) {
1160 iov_discard_front(&iov
, &iov_cnt
, s
);
1162 if (mac_data
.entries
* ETH_ALEN
!= iov_size(iov
, iov_cnt
)) {
1166 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
- in_use
) {
1167 s
= iov_to_buf(iov
, iov_cnt
, 0, &macs
[in_use
* ETH_ALEN
],
1168 mac_data
.entries
* ETH_ALEN
);
1169 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1172 in_use
+= mac_data
.entries
;
1177 n
->mac_table
.in_use
= in_use
;
1178 n
->mac_table
.first_multi
= first_multi
;
1179 n
->mac_table
.uni_overflow
= uni_overflow
;
1180 n
->mac_table
.multi_overflow
= multi_overflow
;
1181 memcpy(n
->mac_table
.macs
, macs
, MAC_TABLE_ENTRIES
* ETH_ALEN
);
1183 rxfilter_notify(nc
);
1185 return VIRTIO_NET_OK
;
1189 return VIRTIO_NET_ERR
;
1192 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
1193 struct iovec
*iov
, unsigned int iov_cnt
)
1195 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1198 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1200 s
= iov_to_buf(iov
, iov_cnt
, 0, &vid
, sizeof(vid
));
1201 vid
= virtio_lduw_p(vdev
, &vid
);
1202 if (s
!= sizeof(vid
)) {
1203 return VIRTIO_NET_ERR
;
1206 if (vid
>= MAX_VLAN
)
1207 return VIRTIO_NET_ERR
;
1209 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
1210 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
1211 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
1212 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
1214 return VIRTIO_NET_ERR
;
1216 rxfilter_notify(nc
);
1218 return VIRTIO_NET_OK
;
1221 static int virtio_net_handle_announce(VirtIONet
*n
, uint8_t cmd
,
1222 struct iovec
*iov
, unsigned int iov_cnt
)
1224 trace_virtio_net_handle_announce(n
->announce_timer
.round
);
1225 if (cmd
== VIRTIO_NET_CTRL_ANNOUNCE_ACK
&&
1226 n
->status
& VIRTIO_NET_S_ANNOUNCE
) {
1227 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
1228 if (n
->announce_timer
.round
) {
1229 qemu_announce_timer_step(&n
->announce_timer
);
1231 return VIRTIO_NET_OK
;
1233 return VIRTIO_NET_ERR
;
1237 static bool virtio_net_attach_ebpf_to_backend(NICState
*nic
, int prog_fd
)
1239 NetClientState
*nc
= qemu_get_peer(qemu_get_queue(nic
), 0);
1240 if (nc
== NULL
|| nc
->info
->set_steering_ebpf
== NULL
) {
1244 return nc
->info
->set_steering_ebpf(nc
, prog_fd
);
1247 static void rss_data_to_rss_config(struct VirtioNetRssData
*data
,
1248 struct EBPFRSSConfig
*config
)
1250 config
->redirect
= data
->redirect
;
1251 config
->populate_hash
= data
->populate_hash
;
1252 config
->hash_types
= data
->hash_types
;
1253 config
->indirections_len
= data
->indirections_len
;
1254 config
->default_queue
= data
->default_queue
;
1257 static bool virtio_net_attach_epbf_rss(VirtIONet
*n
)
1259 struct EBPFRSSConfig config
= {};
1261 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
1265 rss_data_to_rss_config(&n
->rss_data
, &config
);
1267 if (!ebpf_rss_set_all(&n
->ebpf_rss
, &config
,
1268 n
->rss_data
.indirections_table
, n
->rss_data
.key
)) {
1272 if (!virtio_net_attach_ebpf_to_backend(n
->nic
, n
->ebpf_rss
.program_fd
)) {
1279 static void virtio_net_detach_epbf_rss(VirtIONet
*n
)
1281 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1284 static void virtio_net_commit_rss_config(VirtIONet
*n
)
1286 if (n
->rss_data
.enabled
) {
1287 n
->rss_data
.enabled_software_rss
= n
->rss_data
.populate_hash
;
1288 if (n
->rss_data
.populate_hash
) {
1289 virtio_net_detach_epbf_rss(n
);
1290 } else if (!virtio_net_attach_epbf_rss(n
)) {
1291 if (get_vhost_net(qemu_get_queue(n
->nic
)->peer
)) {
1292 warn_report("Can't load eBPF RSS for vhost");
1294 warn_report("Can't load eBPF RSS - fallback to software RSS");
1295 n
->rss_data
.enabled_software_rss
= true;
1299 trace_virtio_net_rss_enable(n
->rss_data
.hash_types
,
1300 n
->rss_data
.indirections_len
,
1301 sizeof(n
->rss_data
.key
));
1303 virtio_net_detach_epbf_rss(n
);
1304 trace_virtio_net_rss_disable();
1308 static void virtio_net_disable_rss(VirtIONet
*n
)
1310 if (!n
->rss_data
.enabled
) {
1314 n
->rss_data
.enabled
= false;
1315 virtio_net_commit_rss_config(n
);
1318 static bool virtio_net_load_ebpf_fds(VirtIONet
*n
)
1320 int fds
[EBPF_RSS_MAX_FDS
] = { [0 ... EBPF_RSS_MAX_FDS
- 1] = -1};
1324 if (n
->nr_ebpf_rss_fds
!= EBPF_RSS_MAX_FDS
) {
1325 warn_report("Expected %d file descriptors but got %d",
1326 EBPF_RSS_MAX_FDS
, n
->nr_ebpf_rss_fds
);
1330 for (i
= 0; i
< n
->nr_ebpf_rss_fds
; i
++) {
1331 fds
[i
] = monitor_fd_param(monitor_cur(), n
->ebpf_rss_fds
[i
],
1339 ret
= ebpf_rss_load_fds(&n
->ebpf_rss
, fds
[0], fds
[1], fds
[2], fds
[3]);
1343 for (i
= 0; i
< n
->nr_ebpf_rss_fds
&& fds
[i
] != -1; i
++) {
1351 static bool virtio_net_load_ebpf(VirtIONet
*n
)
1355 if (virtio_net_attach_ebpf_to_backend(n
->nic
, -1)) {
1356 if (!(n
->ebpf_rss_fds
&& virtio_net_load_ebpf_fds(n
))) {
1357 ret
= ebpf_rss_load(&n
->ebpf_rss
);
1364 static void virtio_net_unload_ebpf(VirtIONet
*n
)
1366 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1367 ebpf_rss_unload(&n
->ebpf_rss
);
1370 static uint16_t virtio_net_handle_rss(VirtIONet
*n
,
1372 unsigned int iov_cnt
,
1375 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1376 struct virtio_net_rss_config cfg
;
1377 size_t s
, offset
= 0, size_get
;
1378 uint16_t queue_pairs
, i
;
1383 const char *err_msg
= "";
1384 uint32_t err_value
= 0;
1386 if (do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_RSS
)) {
1387 err_msg
= "RSS is not negotiated";
1390 if (!do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_HASH_REPORT
)) {
1391 err_msg
= "Hash report is not negotiated";
1394 size_get
= offsetof(struct virtio_net_rss_config
, indirection_table
);
1395 s
= iov_to_buf(iov
, iov_cnt
, offset
, &cfg
, size_get
);
1396 if (s
!= size_get
) {
1397 err_msg
= "Short command buffer";
1398 err_value
= (uint32_t)s
;
1401 n
->rss_data
.hash_types
= virtio_ldl_p(vdev
, &cfg
.hash_types
);
1402 n
->rss_data
.indirections_len
=
1403 virtio_lduw_p(vdev
, &cfg
.indirection_table_mask
);
1404 n
->rss_data
.indirections_len
++;
1406 n
->rss_data
.indirections_len
= 1;
1408 if (!is_power_of_2(n
->rss_data
.indirections_len
)) {
1409 err_msg
= "Invalid size of indirection table";
1410 err_value
= n
->rss_data
.indirections_len
;
1413 if (n
->rss_data
.indirections_len
> VIRTIO_NET_RSS_MAX_TABLE_LEN
) {
1414 err_msg
= "Too large indirection table";
1415 err_value
= n
->rss_data
.indirections_len
;
1418 n
->rss_data
.default_queue
= do_rss
?
1419 virtio_lduw_p(vdev
, &cfg
.unclassified_queue
) : 0;
1420 if (n
->rss_data
.default_queue
>= n
->max_queue_pairs
) {
1421 err_msg
= "Invalid default queue";
1422 err_value
= n
->rss_data
.default_queue
;
1426 size_get
= sizeof(uint16_t) * n
->rss_data
.indirections_len
;
1427 g_free(n
->rss_data
.indirections_table
);
1428 n
->rss_data
.indirections_table
= g_malloc(size_get
);
1429 if (!n
->rss_data
.indirections_table
) {
1430 err_msg
= "Can't allocate indirections table";
1431 err_value
= n
->rss_data
.indirections_len
;
1434 s
= iov_to_buf(iov
, iov_cnt
, offset
,
1435 n
->rss_data
.indirections_table
, size_get
);
1436 if (s
!= size_get
) {
1437 err_msg
= "Short indirection table buffer";
1438 err_value
= (uint32_t)s
;
1441 for (i
= 0; i
< n
->rss_data
.indirections_len
; ++i
) {
1442 uint16_t val
= n
->rss_data
.indirections_table
[i
];
1443 n
->rss_data
.indirections_table
[i
] = virtio_lduw_p(vdev
, &val
);
1446 size_get
= sizeof(temp
);
1447 s
= iov_to_buf(iov
, iov_cnt
, offset
, &temp
, size_get
);
1448 if (s
!= size_get
) {
1449 err_msg
= "Can't get queue_pairs";
1450 err_value
= (uint32_t)s
;
1453 queue_pairs
= do_rss
? virtio_lduw_p(vdev
, &temp
.us
) : n
->curr_queue_pairs
;
1454 if (queue_pairs
== 0 || queue_pairs
> n
->max_queue_pairs
) {
1455 err_msg
= "Invalid number of queue_pairs";
1456 err_value
= queue_pairs
;
1459 if (temp
.b
> VIRTIO_NET_RSS_MAX_KEY_SIZE
) {
1460 err_msg
= "Invalid key size";
1464 if (!temp
.b
&& n
->rss_data
.hash_types
) {
1465 err_msg
= "No key provided";
1469 if (!temp
.b
&& !n
->rss_data
.hash_types
) {
1470 virtio_net_disable_rss(n
);
1475 s
= iov_to_buf(iov
, iov_cnt
, offset
, n
->rss_data
.key
, size_get
);
1476 if (s
!= size_get
) {
1477 err_msg
= "Can get key buffer";
1478 err_value
= (uint32_t)s
;
1481 n
->rss_data
.enabled
= true;
1482 virtio_net_commit_rss_config(n
);
1485 trace_virtio_net_rss_error(err_msg
, err_value
);
1486 virtio_net_disable_rss(n
);
1490 static int virtio_net_handle_mq(VirtIONet
*n
, uint8_t cmd
,
1491 struct iovec
*iov
, unsigned int iov_cnt
)
1493 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1494 uint16_t queue_pairs
;
1495 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1497 virtio_net_disable_rss(n
);
1498 if (cmd
== VIRTIO_NET_CTRL_MQ_HASH_CONFIG
) {
1499 queue_pairs
= virtio_net_handle_rss(n
, iov
, iov_cnt
, false);
1500 return queue_pairs
? VIRTIO_NET_OK
: VIRTIO_NET_ERR
;
1502 if (cmd
== VIRTIO_NET_CTRL_MQ_RSS_CONFIG
) {
1503 queue_pairs
= virtio_net_handle_rss(n
, iov
, iov_cnt
, true);
1504 } else if (cmd
== VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
) {
1505 struct virtio_net_ctrl_mq mq
;
1507 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_MQ
)) {
1508 return VIRTIO_NET_ERR
;
1510 s
= iov_to_buf(iov
, iov_cnt
, 0, &mq
, sizeof(mq
));
1511 if (s
!= sizeof(mq
)) {
1512 return VIRTIO_NET_ERR
;
1514 queue_pairs
= virtio_lduw_p(vdev
, &mq
.virtqueue_pairs
);
1517 return VIRTIO_NET_ERR
;
1520 if (queue_pairs
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
1521 queue_pairs
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
1522 queue_pairs
> n
->max_queue_pairs
||
1524 return VIRTIO_NET_ERR
;
1527 n
->curr_queue_pairs
= queue_pairs
;
1528 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
1530 * Avoid updating the backend for a vdpa device: We're only interested
1531 * in updating the device model queues.
1533 return VIRTIO_NET_OK
;
1535 /* stop the backend before changing the number of queue_pairs to avoid handling a
1537 virtio_net_set_status(vdev
, vdev
->status
);
1538 virtio_net_set_queue_pairs(n
);
1540 return VIRTIO_NET_OK
;
1543 size_t virtio_net_handle_ctrl_iov(VirtIODevice
*vdev
,
1544 const struct iovec
*in_sg
, unsigned in_num
,
1545 const struct iovec
*out_sg
,
1548 VirtIONet
*n
= VIRTIO_NET(vdev
);
1549 struct virtio_net_ctrl_hdr ctrl
;
1550 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1552 struct iovec
*iov
, *iov2
;
1554 if (iov_size(in_sg
, in_num
) < sizeof(status
) ||
1555 iov_size(out_sg
, out_num
) < sizeof(ctrl
)) {
1556 virtio_error(vdev
, "virtio-net ctrl missing headers");
1560 iov2
= iov
= g_memdup2(out_sg
, sizeof(struct iovec
) * out_num
);
1561 s
= iov_to_buf(iov
, out_num
, 0, &ctrl
, sizeof(ctrl
));
1562 iov_discard_front(&iov
, &out_num
, sizeof(ctrl
));
1563 if (s
!= sizeof(ctrl
)) {
1564 status
= VIRTIO_NET_ERR
;
1565 } else if (ctrl
.class == VIRTIO_NET_CTRL_RX
) {
1566 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, iov
, out_num
);
1567 } else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
) {
1568 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, iov
, out_num
);
1569 } else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
) {
1570 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, iov
, out_num
);
1571 } else if (ctrl
.class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1572 status
= virtio_net_handle_announce(n
, ctrl
.cmd
, iov
, out_num
);
1573 } else if (ctrl
.class == VIRTIO_NET_CTRL_MQ
) {
1574 status
= virtio_net_handle_mq(n
, ctrl
.cmd
, iov
, out_num
);
1575 } else if (ctrl
.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS
) {
1576 status
= virtio_net_handle_offloads(n
, ctrl
.cmd
, iov
, out_num
);
1579 s
= iov_from_buf(in_sg
, in_num
, 0, &status
, sizeof(status
));
1580 assert(s
== sizeof(status
));
1583 return sizeof(status
);
1586 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
1588 VirtQueueElement
*elem
;
1592 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
1597 written
= virtio_net_handle_ctrl_iov(vdev
, elem
->in_sg
, elem
->in_num
,
1598 elem
->out_sg
, elem
->out_num
);
1600 virtqueue_push(vq
, elem
, written
);
1601 virtio_notify(vdev
, vq
);
1604 virtqueue_detach_element(vq
, elem
, 0);
1613 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
1615 VirtIONet
*n
= VIRTIO_NET(vdev
);
1616 int queue_index
= vq2q(virtio_get_queue_index(vq
));
1618 qemu_flush_queued_packets(qemu_get_subqueue(n
->nic
, queue_index
));
1621 static bool virtio_net_can_receive(NetClientState
*nc
)
1623 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1624 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1625 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1627 if (!vdev
->vm_running
) {
1631 if (nc
->queue_index
>= n
->curr_queue_pairs
) {
1635 if (!virtio_queue_ready(q
->rx_vq
) ||
1636 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1643 static int virtio_net_has_buffers(VirtIONetQueue
*q
, int bufsize
)
1646 unsigned int in_bytes
;
1647 VirtIONet
*n
= q
->n
;
1649 while (virtio_queue_empty(q
->rx_vq
) || n
->mergeable_rx_bufs
) {
1650 opaque
= virtqueue_get_avail_bytes(q
->rx_vq
, &in_bytes
, NULL
,
1652 /* Buffer is enough, disable notifiaction */
1653 if (bufsize
<= in_bytes
) {
1657 if (virtio_queue_enable_notification_and_check(q
->rx_vq
, opaque
)) {
1658 /* Guest has added some buffers, try again */
1665 virtio_queue_set_notification(q
->rx_vq
, 0);
1670 static void virtio_net_hdr_swap(VirtIODevice
*vdev
, struct virtio_net_hdr
*hdr
)
1672 virtio_tswap16s(vdev
, &hdr
->hdr_len
);
1673 virtio_tswap16s(vdev
, &hdr
->gso_size
);
1674 virtio_tswap16s(vdev
, &hdr
->csum_start
);
1675 virtio_tswap16s(vdev
, &hdr
->csum_offset
);
1678 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1679 * it never finds out that the packets don't have valid checksums. This
1680 * causes dhclient to get upset. Fedora's carried a patch for ages to
1681 * fix this with Xen but it hasn't appeared in an upstream release of
1684 * To avoid breaking existing guests, we catch udp packets and add
1685 * checksums. This is terrible but it's better than hacking the guest
1688 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1689 * we should provide a mechanism to disable it to avoid polluting the host
1692 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
1693 uint8_t *buf
, size_t size
)
1695 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
1696 (size
> 27 && size
< 1500) && /* normal sized MTU */
1697 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
1698 (buf
[23] == 17) && /* ip.protocol == UDP */
1699 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
1700 net_checksum_calculate(buf
, size
, CSUM_UDP
);
1701 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1705 static void receive_header(VirtIONet
*n
, const struct iovec
*iov
, int iov_cnt
,
1706 const void *buf
, size_t size
)
1708 if (n
->has_vnet_hdr
) {
1709 /* FIXME this cast is evil */
1710 void *wbuf
= (void *)buf
;
1711 work_around_broken_dhclient(wbuf
, wbuf
+ n
->host_hdr_len
,
1712 size
- n
->host_hdr_len
);
1714 if (n
->needs_vnet_hdr_swap
) {
1715 virtio_net_hdr_swap(VIRTIO_DEVICE(n
), wbuf
);
1717 iov_from_buf(iov
, iov_cnt
, 0, buf
, sizeof(struct virtio_net_hdr
));
1719 struct virtio_net_hdr hdr
= {
1721 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
1723 iov_from_buf(iov
, iov_cnt
, 0, &hdr
, sizeof hdr
);
1727 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
1729 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1730 static const uint8_t vlan
[] = {0x81, 0x00};
1731 uint8_t *ptr
= (uint8_t *)buf
;
1737 ptr
+= n
->host_hdr_len
;
1739 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
1740 int vid
= lduw_be_p(ptr
+ 14) & 0xfff;
1741 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
1745 if (ptr
[0] & 1) { // multicast
1746 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
1748 } else if (n
->nomulti
) {
1750 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
1754 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
1755 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1762 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
1764 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
1768 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
1769 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1778 static uint8_t virtio_net_get_hash_type(bool hasip4
,
1780 EthL4HdrProto l4hdr_proto
,
1784 switch (l4hdr_proto
) {
1785 case ETH_L4_HDR_PROTO_TCP
:
1786 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCPv4
) {
1787 return NetPktRssIpV4Tcp
;
1791 case ETH_L4_HDR_PROTO_UDP
:
1792 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDPv4
) {
1793 return NetPktRssIpV4Udp
;
1801 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IPv4
) {
1802 return NetPktRssIpV4
;
1804 } else if (hasip6
) {
1805 switch (l4hdr_proto
) {
1806 case ETH_L4_HDR_PROTO_TCP
:
1807 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCP_EX
) {
1808 return NetPktRssIpV6TcpEx
;
1810 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCPv6
) {
1811 return NetPktRssIpV6Tcp
;
1815 case ETH_L4_HDR_PROTO_UDP
:
1816 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDP_EX
) {
1817 return NetPktRssIpV6UdpEx
;
1819 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDPv6
) {
1820 return NetPktRssIpV6Udp
;
1828 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IP_EX
) {
1829 return NetPktRssIpV6Ex
;
1831 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IPv6
) {
1832 return NetPktRssIpV6
;
1838 static int virtio_net_process_rss(NetClientState
*nc
, const uint8_t *buf
,
1840 struct virtio_net_hdr_v1_hash
*hdr
)
1842 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1843 unsigned int index
= nc
->queue_index
, new_index
= index
;
1844 struct NetRxPkt
*pkt
= n
->rx_pkt
;
1845 uint8_t net_hash_type
;
1847 bool hasip4
, hasip6
;
1848 EthL4HdrProto l4hdr_proto
;
1849 static const uint8_t reports
[NetPktRssIpV6UdpEx
+ 1] = {
1850 VIRTIO_NET_HASH_REPORT_IPv4
,
1851 VIRTIO_NET_HASH_REPORT_TCPv4
,
1852 VIRTIO_NET_HASH_REPORT_TCPv6
,
1853 VIRTIO_NET_HASH_REPORT_IPv6
,
1854 VIRTIO_NET_HASH_REPORT_IPv6_EX
,
1855 VIRTIO_NET_HASH_REPORT_TCPv6_EX
,
1856 VIRTIO_NET_HASH_REPORT_UDPv4
,
1857 VIRTIO_NET_HASH_REPORT_UDPv6
,
1858 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1860 struct iovec iov
= {
1861 .iov_base
= (void *)buf
,
1865 net_rx_pkt_set_protocols(pkt
, &iov
, 1, n
->host_hdr_len
);
1866 net_rx_pkt_get_protocols(pkt
, &hasip4
, &hasip6
, &l4hdr_proto
);
1867 net_hash_type
= virtio_net_get_hash_type(hasip4
, hasip6
, l4hdr_proto
,
1868 n
->rss_data
.hash_types
);
1869 if (net_hash_type
> NetPktRssIpV6UdpEx
) {
1870 if (n
->rss_data
.populate_hash
) {
1871 hdr
->hash_value
= VIRTIO_NET_HASH_REPORT_NONE
;
1872 hdr
->hash_report
= 0;
1874 return n
->rss_data
.redirect
? n
->rss_data
.default_queue
: -1;
1877 hash
= net_rx_pkt_calc_rss_hash(pkt
, net_hash_type
, n
->rss_data
.key
);
1879 if (n
->rss_data
.populate_hash
) {
1880 hdr
->hash_value
= hash
;
1881 hdr
->hash_report
= reports
[net_hash_type
];
1884 if (n
->rss_data
.redirect
) {
1885 new_index
= hash
& (n
->rss_data
.indirections_len
- 1);
1886 new_index
= n
->rss_data
.indirections_table
[new_index
];
1889 return (index
== new_index
) ? -1 : new_index
;
1892 static ssize_t
virtio_net_receive_rcu(NetClientState
*nc
, const uint8_t *buf
,
1893 size_t size
, bool no_rss
)
1895 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1896 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1897 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1898 VirtQueueElement
*elems
[VIRTQUEUE_MAX_SIZE
];
1899 size_t lens
[VIRTQUEUE_MAX_SIZE
];
1900 struct iovec mhdr_sg
[VIRTQUEUE_MAX_SIZE
];
1901 struct virtio_net_hdr_v1_hash extra_hdr
;
1902 unsigned mhdr_cnt
= 0;
1903 size_t offset
, i
, guest_offset
, j
;
1906 if (!virtio_net_can_receive(nc
)) {
1910 if (!no_rss
&& n
->rss_data
.enabled
&& n
->rss_data
.enabled_software_rss
) {
1911 int index
= virtio_net_process_rss(nc
, buf
, size
, &extra_hdr
);
1913 NetClientState
*nc2
=
1914 qemu_get_subqueue(n
->nic
, index
% n
->curr_queue_pairs
);
1915 return virtio_net_receive_rcu(nc2
, buf
, size
, true);
1919 /* hdr_len refers to the header we supply to the guest */
1920 if (!virtio_net_has_buffers(q
, size
+ n
->guest_hdr_len
- n
->host_hdr_len
)) {
1924 if (!receive_filter(n
, buf
, size
))
1929 while (offset
< size
) {
1930 VirtQueueElement
*elem
;
1932 const struct iovec
*sg
;
1936 if (i
== VIRTQUEUE_MAX_SIZE
) {
1937 virtio_error(vdev
, "virtio-net unexpected long buffer chain");
1942 elem
= virtqueue_pop(q
->rx_vq
, sizeof(VirtQueueElement
));
1945 virtio_error(vdev
, "virtio-net unexpected empty queue: "
1946 "i %zd mergeable %d offset %zd, size %zd, "
1947 "guest hdr len %zd, host hdr len %zd "
1948 "guest features 0x%" PRIx64
,
1949 i
, n
->mergeable_rx_bufs
, offset
, size
,
1950 n
->guest_hdr_len
, n
->host_hdr_len
,
1951 vdev
->guest_features
);
1957 if (elem
->in_num
< 1) {
1959 "virtio-net receive queue contains no in buffers");
1960 virtqueue_detach_element(q
->rx_vq
, elem
, 0);
1968 assert(offset
== 0);
1969 if (n
->mergeable_rx_bufs
) {
1970 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
1972 offsetof(typeof(extra_hdr
), hdr
.num_buffers
),
1973 sizeof(extra_hdr
.hdr
.num_buffers
));
1976 receive_header(n
, sg
, elem
->in_num
, buf
, size
);
1977 if (n
->rss_data
.populate_hash
) {
1978 offset
= offsetof(typeof(extra_hdr
), hash_value
);
1979 iov_from_buf(sg
, elem
->in_num
, offset
,
1980 (char *)&extra_hdr
+ offset
,
1981 sizeof(extra_hdr
.hash_value
) +
1982 sizeof(extra_hdr
.hash_report
));
1984 offset
= n
->host_hdr_len
;
1985 total
+= n
->guest_hdr_len
;
1986 guest_offset
= n
->guest_hdr_len
;
1991 /* copy in packet. ugh */
1992 len
= iov_from_buf(sg
, elem
->in_num
, guest_offset
,
1993 buf
+ offset
, size
- offset
);
1996 /* If buffers can't be merged, at this point we
1997 * must have consumed the complete packet.
1998 * Otherwise, drop it. */
1999 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
2000 virtqueue_unpop(q
->rx_vq
, elem
, total
);
2012 virtio_stw_p(vdev
, &extra_hdr
.hdr
.num_buffers
, i
);
2013 iov_from_buf(mhdr_sg
, mhdr_cnt
,
2015 &extra_hdr
.hdr
.num_buffers
,
2016 sizeof extra_hdr
.hdr
.num_buffers
);
2019 for (j
= 0; j
< i
; j
++) {
2020 /* signal other side */
2021 virtqueue_fill(q
->rx_vq
, elems
[j
], lens
[j
], j
);
2025 virtqueue_flush(q
->rx_vq
, i
);
2026 virtio_notify(vdev
, q
->rx_vq
);
2031 for (j
= 0; j
< i
; j
++) {
2032 virtqueue_detach_element(q
->rx_vq
, elems
[j
], lens
[j
]);
2039 static ssize_t
virtio_net_do_receive(NetClientState
*nc
, const uint8_t *buf
,
2042 RCU_READ_LOCK_GUARD();
2044 return virtio_net_receive_rcu(nc
, buf
, size
, false);
2047 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain
*chain
,
2049 VirtioNetRscUnit
*unit
)
2052 struct ip_header
*ip
;
2054 ip
= (struct ip_header
*)(buf
+ chain
->n
->guest_hdr_len
2055 + sizeof(struct eth_header
));
2056 unit
->ip
= (void *)ip
;
2057 ip_hdrlen
= (ip
->ip_ver_len
& 0xF) << 2;
2058 unit
->ip_plen
= &ip
->ip_len
;
2059 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
) + ip_hdrlen
);
2060 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
2061 unit
->payload
= htons(*unit
->ip_plen
) - ip_hdrlen
- unit
->tcp_hdrlen
;
2064 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain
*chain
,
2066 VirtioNetRscUnit
*unit
)
2068 struct ip6_header
*ip6
;
2070 ip6
= (struct ip6_header
*)(buf
+ chain
->n
->guest_hdr_len
2071 + sizeof(struct eth_header
));
2073 unit
->ip_plen
= &(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
2074 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
)
2075 + sizeof(struct ip6_header
));
2076 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
2078 /* There is a difference between payload length in ipv4 and v6,
2079 ip header is excluded in ipv6 */
2080 unit
->payload
= htons(*unit
->ip_plen
) - unit
->tcp_hdrlen
;
2083 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain
*chain
,
2084 VirtioNetRscSeg
*seg
)
2087 struct virtio_net_hdr_v1
*h
;
2089 h
= (struct virtio_net_hdr_v1
*)seg
->buf
;
2091 h
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
2093 if (seg
->is_coalesced
) {
2094 h
->rsc
.segments
= seg
->packets
;
2095 h
->rsc
.dup_acks
= seg
->dup_ack
;
2096 h
->flags
= VIRTIO_NET_HDR_F_RSC_INFO
;
2097 if (chain
->proto
== ETH_P_IP
) {
2098 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2100 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2104 ret
= virtio_net_do_receive(seg
->nc
, seg
->buf
, seg
->size
);
2105 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
2112 static void virtio_net_rsc_purge(void *opq
)
2114 VirtioNetRscSeg
*seg
, *rn
;
2115 VirtioNetRscChain
*chain
= (VirtioNetRscChain
*)opq
;
2117 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn
) {
2118 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2119 chain
->stat
.purge_failed
++;
2124 chain
->stat
.timer
++;
2125 if (!QTAILQ_EMPTY(&chain
->buffers
)) {
2126 timer_mod(chain
->drain_timer
,
2127 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + chain
->n
->rsc_timeout
);
2131 static void virtio_net_rsc_cleanup(VirtIONet
*n
)
2133 VirtioNetRscChain
*chain
, *rn_chain
;
2134 VirtioNetRscSeg
*seg
, *rn_seg
;
2136 QTAILQ_FOREACH_SAFE(chain
, &n
->rsc_chains
, next
, rn_chain
) {
2137 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn_seg
) {
2138 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
2143 timer_free(chain
->drain_timer
);
2144 QTAILQ_REMOVE(&n
->rsc_chains
, chain
, next
);
2149 static void virtio_net_rsc_cache_buf(VirtioNetRscChain
*chain
,
2151 const uint8_t *buf
, size_t size
)
2154 VirtioNetRscSeg
*seg
;
2156 hdr_len
= chain
->n
->guest_hdr_len
;
2157 seg
= g_new(VirtioNetRscSeg
, 1);
2158 seg
->buf
= g_malloc(hdr_len
+ sizeof(struct eth_header
)
2159 + sizeof(struct ip6_header
) + VIRTIO_NET_MAX_TCP_PAYLOAD
);
2160 memcpy(seg
->buf
, buf
, size
);
2164 seg
->is_coalesced
= 0;
2167 QTAILQ_INSERT_TAIL(&chain
->buffers
, seg
, next
);
2168 chain
->stat
.cache
++;
2170 switch (chain
->proto
) {
2172 virtio_net_rsc_extract_unit4(chain
, seg
->buf
, &seg
->unit
);
2175 virtio_net_rsc_extract_unit6(chain
, seg
->buf
, &seg
->unit
);
2178 g_assert_not_reached();
2182 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain
*chain
,
2183 VirtioNetRscSeg
*seg
,
2185 struct tcp_header
*n_tcp
,
2186 struct tcp_header
*o_tcp
)
2188 uint32_t nack
, oack
;
2189 uint16_t nwin
, owin
;
2191 nack
= htonl(n_tcp
->th_ack
);
2192 nwin
= htons(n_tcp
->th_win
);
2193 oack
= htonl(o_tcp
->th_ack
);
2194 owin
= htons(o_tcp
->th_win
);
2196 if ((nack
- oack
) >= VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2197 chain
->stat
.ack_out_of_win
++;
2199 } else if (nack
== oack
) {
2200 /* duplicated ack or window probe */
2202 /* duplicated ack, add dup ack count due to whql test up to 1 */
2203 chain
->stat
.dup_ack
++;
2206 /* Coalesce window update */
2207 o_tcp
->th_win
= n_tcp
->th_win
;
2208 chain
->stat
.win_update
++;
2209 return RSC_COALESCE
;
2212 /* pure ack, go to 'C', finalize*/
2213 chain
->stat
.pure_ack
++;
2218 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain
*chain
,
2219 VirtioNetRscSeg
*seg
,
2221 VirtioNetRscUnit
*n_unit
)
2225 uint32_t nseq
, oseq
;
2226 VirtioNetRscUnit
*o_unit
;
2228 o_unit
= &seg
->unit
;
2229 o_ip_len
= htons(*o_unit
->ip_plen
);
2230 nseq
= htonl(n_unit
->tcp
->th_seq
);
2231 oseq
= htonl(o_unit
->tcp
->th_seq
);
2233 /* out of order or retransmitted. */
2234 if ((nseq
- oseq
) > VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2235 chain
->stat
.data_out_of_win
++;
2239 data
= ((uint8_t *)n_unit
->tcp
) + n_unit
->tcp_hdrlen
;
2241 if ((o_unit
->payload
== 0) && n_unit
->payload
) {
2242 /* From no payload to payload, normal case, not a dup ack or etc */
2243 chain
->stat
.data_after_pure_ack
++;
2246 return virtio_net_rsc_handle_ack(chain
, seg
, buf
,
2247 n_unit
->tcp
, o_unit
->tcp
);
2249 } else if ((nseq
- oseq
) != o_unit
->payload
) {
2250 /* Not a consistent packet, out of order */
2251 chain
->stat
.data_out_of_order
++;
2255 if ((o_ip_len
+ n_unit
->payload
) > chain
->max_payload
) {
2256 chain
->stat
.over_size
++;
2260 /* Here comes the right data, the payload length in v4/v6 is different,
2261 so use the field value to update and record the new data len */
2262 o_unit
->payload
+= n_unit
->payload
; /* update new data len */
2264 /* update field in ip header */
2265 *o_unit
->ip_plen
= htons(o_ip_len
+ n_unit
->payload
);
2267 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
2268 for windows guest, while this may change the behavior for linux
2269 guest (only if it uses RSC feature). */
2270 o_unit
->tcp
->th_offset_flags
= n_unit
->tcp
->th_offset_flags
;
2272 o_unit
->tcp
->th_ack
= n_unit
->tcp
->th_ack
;
2273 o_unit
->tcp
->th_win
= n_unit
->tcp
->th_win
;
2275 memmove(seg
->buf
+ seg
->size
, data
, n_unit
->payload
);
2276 seg
->size
+= n_unit
->payload
;
2278 chain
->stat
.coalesced
++;
2279 return RSC_COALESCE
;
2283 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain
*chain
,
2284 VirtioNetRscSeg
*seg
,
2285 const uint8_t *buf
, size_t size
,
2286 VirtioNetRscUnit
*unit
)
2288 struct ip_header
*ip1
, *ip2
;
2290 ip1
= (struct ip_header
*)(unit
->ip
);
2291 ip2
= (struct ip_header
*)(seg
->unit
.ip
);
2292 if ((ip1
->ip_src
^ ip2
->ip_src
) || (ip1
->ip_dst
^ ip2
->ip_dst
)
2293 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2294 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2295 chain
->stat
.no_match
++;
2296 return RSC_NO_MATCH
;
2299 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2302 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain
*chain
,
2303 VirtioNetRscSeg
*seg
,
2304 const uint8_t *buf
, size_t size
,
2305 VirtioNetRscUnit
*unit
)
2307 struct ip6_header
*ip1
, *ip2
;
2309 ip1
= (struct ip6_header
*)(unit
->ip
);
2310 ip2
= (struct ip6_header
*)(seg
->unit
.ip
);
2311 if (memcmp(&ip1
->ip6_src
, &ip2
->ip6_src
, sizeof(struct in6_address
))
2312 || memcmp(&ip1
->ip6_dst
, &ip2
->ip6_dst
, sizeof(struct in6_address
))
2313 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2314 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2315 chain
->stat
.no_match
++;
2316 return RSC_NO_MATCH
;
2319 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2322 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2323 * to prevent out of order */
2324 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain
*chain
,
2325 struct tcp_header
*tcp
)
2330 tcp_flag
= htons(tcp
->th_offset_flags
);
2331 tcp_hdr
= (tcp_flag
& VIRTIO_NET_TCP_HDR_LENGTH
) >> 10;
2332 tcp_flag
&= VIRTIO_NET_TCP_FLAG
;
2333 if (tcp_flag
& TH_SYN
) {
2334 chain
->stat
.tcp_syn
++;
2338 if (tcp_flag
& (TH_FIN
| TH_URG
| TH_RST
| TH_ECE
| TH_CWR
)) {
2339 chain
->stat
.tcp_ctrl_drain
++;
2343 if (tcp_hdr
> sizeof(struct tcp_header
)) {
2344 chain
->stat
.tcp_all_opt
++;
2348 return RSC_CANDIDATE
;
2351 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain
*chain
,
2353 const uint8_t *buf
, size_t size
,
2354 VirtioNetRscUnit
*unit
)
2357 VirtioNetRscSeg
*seg
, *nseg
;
2359 if (QTAILQ_EMPTY(&chain
->buffers
)) {
2360 chain
->stat
.empty_cache
++;
2361 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2362 timer_mod(chain
->drain_timer
,
2363 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + chain
->n
->rsc_timeout
);
2367 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2368 if (chain
->proto
== ETH_P_IP
) {
2369 ret
= virtio_net_rsc_coalesce4(chain
, seg
, buf
, size
, unit
);
2371 ret
= virtio_net_rsc_coalesce6(chain
, seg
, buf
, size
, unit
);
2374 if (ret
== RSC_FINAL
) {
2375 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2377 chain
->stat
.final_failed
++;
2381 /* Send current packet */
2382 return virtio_net_do_receive(nc
, buf
, size
);
2383 } else if (ret
== RSC_NO_MATCH
) {
2386 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2387 seg
->is_coalesced
= 1;
2392 chain
->stat
.no_match_cache
++;
2393 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2397 /* Drain a connection data, this is to avoid out of order segments */
2398 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain
*chain
,
2400 const uint8_t *buf
, size_t size
,
2401 uint16_t ip_start
, uint16_t ip_size
,
2404 VirtioNetRscSeg
*seg
, *nseg
;
2405 uint32_t ppair1
, ppair2
;
2407 ppair1
= *(uint32_t *)(buf
+ tcp_port
);
2408 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2409 ppair2
= *(uint32_t *)(seg
->buf
+ tcp_port
);
2410 if (memcmp(buf
+ ip_start
, seg
->buf
+ ip_start
, ip_size
)
2411 || (ppair1
!= ppair2
)) {
2414 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2415 chain
->stat
.drain_failed
++;
2421 return virtio_net_do_receive(nc
, buf
, size
);
2424 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain
*chain
,
2425 struct ip_header
*ip
,
2426 const uint8_t *buf
, size_t size
)
2430 /* Not an ipv4 packet */
2431 if (((ip
->ip_ver_len
& 0xF0) >> 4) != IP_HEADER_VERSION_4
) {
2432 chain
->stat
.ip_option
++;
2436 /* Don't handle packets with ip option */
2437 if ((ip
->ip_ver_len
& 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH
) {
2438 chain
->stat
.ip_option
++;
2442 if (ip
->ip_p
!= IPPROTO_TCP
) {
2443 chain
->stat
.bypass_not_tcp
++;
2447 /* Don't handle packets with ip fragment */
2448 if (!(htons(ip
->ip_off
) & IP_DF
)) {
2449 chain
->stat
.ip_frag
++;
2453 /* Don't handle packets with ecn flag */
2454 if (IPTOS_ECN(ip
->ip_tos
)) {
2455 chain
->stat
.ip_ecn
++;
2459 ip_len
= htons(ip
->ip_len
);
2460 if (ip_len
< (sizeof(struct ip_header
) + sizeof(struct tcp_header
))
2461 || ip_len
> (size
- chain
->n
->guest_hdr_len
-
2462 sizeof(struct eth_header
))) {
2463 chain
->stat
.ip_hacked
++;
2467 return RSC_CANDIDATE
;
2470 static size_t virtio_net_rsc_receive4(VirtioNetRscChain
*chain
,
2472 const uint8_t *buf
, size_t size
)
2476 VirtioNetRscUnit unit
;
2478 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2480 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
)
2481 + sizeof(struct tcp_header
))) {
2482 chain
->stat
.bypass_not_tcp
++;
2483 return virtio_net_do_receive(nc
, buf
, size
);
2486 virtio_net_rsc_extract_unit4(chain
, buf
, &unit
);
2487 if (virtio_net_rsc_sanity_check4(chain
, unit
.ip
, buf
, size
)
2489 return virtio_net_do_receive(nc
, buf
, size
);
2492 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2493 if (ret
== RSC_BYPASS
) {
2494 return virtio_net_do_receive(nc
, buf
, size
);
2495 } else if (ret
== RSC_FINAL
) {
2496 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2497 ((hdr_len
+ sizeof(struct eth_header
)) + 12),
2498 VIRTIO_NET_IP4_ADDR_SIZE
,
2499 hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
));
2502 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2505 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain
*chain
,
2506 struct ip6_header
*ip6
,
2507 const uint8_t *buf
, size_t size
)
2511 if (((ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_flow
& 0xF0) >> 4)
2512 != IP_HEADER_VERSION_6
) {
2516 /* Both option and protocol is checked in this */
2517 if (ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_nxt
!= IPPROTO_TCP
) {
2518 chain
->stat
.bypass_not_tcp
++;
2522 ip_len
= htons(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
2523 if (ip_len
< sizeof(struct tcp_header
) ||
2524 ip_len
> (size
- chain
->n
->guest_hdr_len
- sizeof(struct eth_header
)
2525 - sizeof(struct ip6_header
))) {
2526 chain
->stat
.ip_hacked
++;
2530 /* Don't handle packets with ecn flag */
2531 if (IP6_ECN(ip6
->ip6_ctlun
.ip6_un3
.ip6_un3_ecn
)) {
2532 chain
->stat
.ip_ecn
++;
2536 return RSC_CANDIDATE
;
2539 static size_t virtio_net_rsc_receive6(void *opq
, NetClientState
*nc
,
2540 const uint8_t *buf
, size_t size
)
2544 VirtioNetRscChain
*chain
;
2545 VirtioNetRscUnit unit
;
2548 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2550 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip6_header
)
2551 + sizeof(tcp_header
))) {
2552 return virtio_net_do_receive(nc
, buf
, size
);
2555 virtio_net_rsc_extract_unit6(chain
, buf
, &unit
);
2556 if (RSC_CANDIDATE
!= virtio_net_rsc_sanity_check6(chain
,
2557 unit
.ip
, buf
, size
)) {
2558 return virtio_net_do_receive(nc
, buf
, size
);
2561 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2562 if (ret
== RSC_BYPASS
) {
2563 return virtio_net_do_receive(nc
, buf
, size
);
2564 } else if (ret
== RSC_FINAL
) {
2565 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2566 ((hdr_len
+ sizeof(struct eth_header
)) + 8),
2567 VIRTIO_NET_IP6_ADDR_SIZE
,
2568 hdr_len
+ sizeof(struct eth_header
)
2569 + sizeof(struct ip6_header
));
2572 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2575 static VirtioNetRscChain
*virtio_net_rsc_lookup_chain(VirtIONet
*n
,
2579 VirtioNetRscChain
*chain
;
2581 if ((proto
!= (uint16_t)ETH_P_IP
) && (proto
!= (uint16_t)ETH_P_IPV6
)) {
2585 QTAILQ_FOREACH(chain
, &n
->rsc_chains
, next
) {
2586 if (chain
->proto
== proto
) {
2591 chain
= g_malloc(sizeof(*chain
));
2593 chain
->proto
= proto
;
2594 if (proto
== (uint16_t)ETH_P_IP
) {
2595 chain
->max_payload
= VIRTIO_NET_MAX_IP4_PAYLOAD
;
2596 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2598 chain
->max_payload
= VIRTIO_NET_MAX_IP6_PAYLOAD
;
2599 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2601 chain
->drain_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2602 virtio_net_rsc_purge
, chain
);
2603 memset(&chain
->stat
, 0, sizeof(chain
->stat
));
2605 QTAILQ_INIT(&chain
->buffers
);
2606 QTAILQ_INSERT_TAIL(&n
->rsc_chains
, chain
, next
);
2611 static ssize_t
virtio_net_rsc_receive(NetClientState
*nc
,
2616 VirtioNetRscChain
*chain
;
2617 struct eth_header
*eth
;
2620 n
= qemu_get_nic_opaque(nc
);
2621 if (size
< (n
->host_hdr_len
+ sizeof(struct eth_header
))) {
2622 return virtio_net_do_receive(nc
, buf
, size
);
2625 eth
= (struct eth_header
*)(buf
+ n
->guest_hdr_len
);
2626 proto
= htons(eth
->h_proto
);
2628 chain
= virtio_net_rsc_lookup_chain(n
, nc
, proto
);
2630 chain
->stat
.received
++;
2631 if (proto
== (uint16_t)ETH_P_IP
&& n
->rsc4_enabled
) {
2632 return virtio_net_rsc_receive4(chain
, nc
, buf
, size
);
2633 } else if (proto
== (uint16_t)ETH_P_IPV6
&& n
->rsc6_enabled
) {
2634 return virtio_net_rsc_receive6(chain
, nc
, buf
, size
);
2637 return virtio_net_do_receive(nc
, buf
, size
);
2640 static ssize_t
virtio_net_receive(NetClientState
*nc
, const uint8_t *buf
,
2643 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2644 if ((n
->rsc4_enabled
|| n
->rsc6_enabled
)) {
2645 return virtio_net_rsc_receive(nc
, buf
, size
);
2647 return virtio_net_do_receive(nc
, buf
, size
);
2651 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
);
2653 static void virtio_net_tx_complete(NetClientState
*nc
, ssize_t len
)
2655 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2656 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
2657 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2660 virtqueue_push(q
->tx_vq
, q
->async_tx
.elem
, 0);
2661 virtio_notify(vdev
, q
->tx_vq
);
2663 g_free(q
->async_tx
.elem
);
2664 q
->async_tx
.elem
= NULL
;
2666 virtio_queue_set_notification(q
->tx_vq
, 1);
2667 ret
= virtio_net_flush_tx(q
);
2668 if (ret
>= n
->tx_burst
) {
2670 * the flush has been stopped by tx_burst
2671 * we will not receive notification for the
2672 * remainining part, so re-schedule
2674 virtio_queue_set_notification(q
->tx_vq
, 0);
2676 replay_bh_schedule_event(q
->tx_bh
);
2678 timer_mod(q
->tx_timer
,
2679 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2686 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
)
2688 VirtIONet
*n
= q
->n
;
2689 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2690 VirtQueueElement
*elem
;
2691 int32_t num_packets
= 0;
2692 int queue_index
= vq2q(virtio_get_queue_index(q
->tx_vq
));
2693 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2697 if (q
->async_tx
.elem
) {
2698 virtio_queue_set_notification(q
->tx_vq
, 0);
2704 unsigned int out_num
;
2705 struct iovec sg
[VIRTQUEUE_MAX_SIZE
], sg2
[VIRTQUEUE_MAX_SIZE
+ 1], *out_sg
;
2706 struct virtio_net_hdr vhdr
;
2708 elem
= virtqueue_pop(q
->tx_vq
, sizeof(VirtQueueElement
));
2713 out_num
= elem
->out_num
;
2714 out_sg
= elem
->out_sg
;
2716 virtio_error(vdev
, "virtio-net header not in first element");
2720 if (n
->needs_vnet_hdr_swap
) {
2721 if (iov_to_buf(out_sg
, out_num
, 0, &vhdr
, sizeof(vhdr
)) <
2723 virtio_error(vdev
, "virtio-net header incorrect");
2726 virtio_net_hdr_swap(vdev
, &vhdr
);
2727 sg2
[0].iov_base
= &vhdr
;
2728 sg2
[0].iov_len
= sizeof(vhdr
);
2729 out_num
= iov_copy(&sg2
[1], ARRAY_SIZE(sg2
) - 1, out_sg
, out_num
,
2731 if (out_num
== VIRTQUEUE_MAX_SIZE
) {
2738 * If host wants to see the guest header as is, we can
2739 * pass it on unchanged. Otherwise, copy just the parts
2740 * that host is interested in.
2742 assert(n
->host_hdr_len
<= n
->guest_hdr_len
);
2743 if (n
->host_hdr_len
!= n
->guest_hdr_len
) {
2744 if (iov_size(out_sg
, out_num
) < n
->guest_hdr_len
) {
2745 virtio_error(vdev
, "virtio-net header is invalid");
2748 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
2750 0, n
->host_hdr_len
);
2751 sg_num
+= iov_copy(sg
+ sg_num
, ARRAY_SIZE(sg
) - sg_num
,
2753 n
->guest_hdr_len
, -1);
2758 virtio_error(vdev
, "virtio-net nothing to send");
2763 ret
= qemu_sendv_packet_async(qemu_get_subqueue(n
->nic
, queue_index
),
2764 out_sg
, out_num
, virtio_net_tx_complete
);
2766 virtio_queue_set_notification(q
->tx_vq
, 0);
2767 q
->async_tx
.elem
= elem
;
2772 virtqueue_push(q
->tx_vq
, elem
, 0);
2773 virtio_notify(vdev
, q
->tx_vq
);
2776 if (++num_packets
>= n
->tx_burst
) {
2783 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
2788 static void virtio_net_tx_timer(void *opaque
);
2790 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
2792 VirtIONet
*n
= VIRTIO_NET(vdev
);
2793 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2795 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2796 virtio_net_drop_tx_queue_data(vdev
, vq
);
2800 /* This happens when device was stopped but VCPU wasn't. */
2801 if (!vdev
->vm_running
) {
2806 if (q
->tx_waiting
) {
2807 /* We already have queued packets, immediately flush */
2808 timer_del(q
->tx_timer
);
2809 virtio_net_tx_timer(q
);
2811 /* re-arm timer to flush it (and more) on next tick */
2812 timer_mod(q
->tx_timer
,
2813 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2815 virtio_queue_set_notification(vq
, 0);
2819 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
2821 VirtIONet
*n
= VIRTIO_NET(vdev
);
2822 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2824 if (unlikely(n
->vhost_started
)) {
2828 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2829 virtio_net_drop_tx_queue_data(vdev
, vq
);
2833 if (unlikely(q
->tx_waiting
)) {
2837 /* This happens when device was stopped but VCPU wasn't. */
2838 if (!vdev
->vm_running
) {
2841 virtio_queue_set_notification(vq
, 0);
2842 replay_bh_schedule_event(q
->tx_bh
);
2845 static void virtio_net_tx_timer(void *opaque
)
2847 VirtIONetQueue
*q
= opaque
;
2848 VirtIONet
*n
= q
->n
;
2849 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2852 /* This happens when device was stopped but BH wasn't. */
2853 if (!vdev
->vm_running
) {
2854 /* Make sure tx waiting is set, so we'll run when restarted. */
2855 assert(q
->tx_waiting
);
2861 /* Just in case the driver is not ready on more */
2862 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2866 ret
= virtio_net_flush_tx(q
);
2867 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2871 * If we flush a full burst of packets, assume there are
2872 * more coming and immediately rearm
2874 if (ret
>= n
->tx_burst
) {
2876 timer_mod(q
->tx_timer
,
2877 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2881 * If less than a full burst, re-enable notification and flush
2882 * anything that may have come in while we weren't looking. If
2883 * we find something, assume the guest is still active and rearm
2885 virtio_queue_set_notification(q
->tx_vq
, 1);
2886 ret
= virtio_net_flush_tx(q
);
2888 virtio_queue_set_notification(q
->tx_vq
, 0);
2890 timer_mod(q
->tx_timer
,
2891 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2895 static void virtio_net_tx_bh(void *opaque
)
2897 VirtIONetQueue
*q
= opaque
;
2898 VirtIONet
*n
= q
->n
;
2899 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2902 /* This happens when device was stopped but BH wasn't. */
2903 if (!vdev
->vm_running
) {
2904 /* Make sure tx waiting is set, so we'll run when restarted. */
2905 assert(q
->tx_waiting
);
2911 /* Just in case the driver is not ready on more */
2912 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
2916 ret
= virtio_net_flush_tx(q
);
2917 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2918 return; /* Notification re-enable handled by tx_complete or device
2922 /* If we flush a full burst of packets, assume there are
2923 * more coming and immediately reschedule */
2924 if (ret
>= n
->tx_burst
) {
2925 replay_bh_schedule_event(q
->tx_bh
);
2930 /* If less than a full burst, re-enable notification and flush
2931 * anything that may have come in while we weren't looking. If
2932 * we find something, assume the guest is still active and reschedule */
2933 virtio_queue_set_notification(q
->tx_vq
, 1);
2934 ret
= virtio_net_flush_tx(q
);
2935 if (ret
== -EINVAL
) {
2937 } else if (ret
> 0) {
2938 virtio_queue_set_notification(q
->tx_vq
, 0);
2939 replay_bh_schedule_event(q
->tx_bh
);
2944 static void virtio_net_add_queue(VirtIONet
*n
, int index
)
2946 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2948 n
->vqs
[index
].rx_vq
= virtio_add_queue(vdev
, n
->net_conf
.rx_queue_size
,
2949 virtio_net_handle_rx
);
2951 if (n
->net_conf
.tx
&& !strcmp(n
->net_conf
.tx
, "timer")) {
2952 n
->vqs
[index
].tx_vq
=
2953 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2954 virtio_net_handle_tx_timer
);
2955 n
->vqs
[index
].tx_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2956 virtio_net_tx_timer
,
2959 n
->vqs
[index
].tx_vq
=
2960 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2961 virtio_net_handle_tx_bh
);
2962 n
->vqs
[index
].tx_bh
= qemu_bh_new_guarded(virtio_net_tx_bh
, &n
->vqs
[index
],
2963 &DEVICE(vdev
)->mem_reentrancy_guard
);
2966 n
->vqs
[index
].tx_waiting
= 0;
2967 n
->vqs
[index
].n
= n
;
2970 static void virtio_net_del_queue(VirtIONet
*n
, int index
)
2972 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2973 VirtIONetQueue
*q
= &n
->vqs
[index
];
2974 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
2976 qemu_purge_queued_packets(nc
);
2978 virtio_del_queue(vdev
, index
* 2);
2980 timer_free(q
->tx_timer
);
2983 qemu_bh_delete(q
->tx_bh
);
2987 virtio_del_queue(vdev
, index
* 2 + 1);
2990 static void virtio_net_change_num_queue_pairs(VirtIONet
*n
, int new_max_queue_pairs
)
2992 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2993 int old_num_queues
= virtio_get_num_queues(vdev
);
2994 int new_num_queues
= new_max_queue_pairs
* 2 + 1;
2997 assert(old_num_queues
>= 3);
2998 assert(old_num_queues
% 2 == 1);
3000 if (old_num_queues
== new_num_queues
) {
3005 * We always need to remove and add ctrl vq if
3006 * old_num_queues != new_num_queues. Remove ctrl_vq first,
3007 * and then we only enter one of the following two loops.
3009 virtio_del_queue(vdev
, old_num_queues
- 1);
3011 for (i
= new_num_queues
- 1; i
< old_num_queues
- 1; i
+= 2) {
3012 /* new_num_queues < old_num_queues */
3013 virtio_net_del_queue(n
, i
/ 2);
3016 for (i
= old_num_queues
- 1; i
< new_num_queues
- 1; i
+= 2) {
3017 /* new_num_queues > old_num_queues */
3018 virtio_net_add_queue(n
, i
/ 2);
3021 /* add ctrl_vq last */
3022 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
3025 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
)
3027 int max
= multiqueue
? n
->max_queue_pairs
: 1;
3029 n
->multiqueue
= multiqueue
;
3030 virtio_net_change_num_queue_pairs(n
, max
);
3032 virtio_net_set_queue_pairs(n
);
3035 static int virtio_net_post_load_device(void *opaque
, int version_id
)
3037 VirtIONet
*n
= opaque
;
3038 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3041 trace_virtio_net_post_load_device();
3042 virtio_net_set_mrg_rx_bufs(n
, n
->mergeable_rx_bufs
,
3043 virtio_vdev_has_feature(vdev
,
3044 VIRTIO_F_VERSION_1
),
3045 virtio_vdev_has_feature(vdev
,
3046 VIRTIO_NET_F_HASH_REPORT
));
3048 /* MAC_TABLE_ENTRIES may be different from the saved image */
3049 if (n
->mac_table
.in_use
> MAC_TABLE_ENTRIES
) {
3050 n
->mac_table
.in_use
= 0;
3053 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
3054 n
->curr_guest_offloads
= virtio_net_supported_guest_offloads(n
);
3058 * curr_guest_offloads will be later overwritten by the
3059 * virtio_set_features_nocheck call done from the virtio_load.
3060 * Here we make sure it is preserved and restored accordingly
3061 * in the virtio_net_post_load_virtio callback.
3063 n
->saved_guest_offloads
= n
->curr_guest_offloads
;
3065 virtio_net_set_queue_pairs(n
);
3067 /* Find the first multicast entry in the saved MAC filter */
3068 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
3069 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
3073 n
->mac_table
.first_multi
= i
;
3075 /* nc.link_down can't be migrated, so infer link_down according
3076 * to link status bit in n->status */
3077 link_down
= (n
->status
& VIRTIO_NET_S_LINK_UP
) == 0;
3078 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3079 qemu_get_subqueue(n
->nic
, i
)->link_down
= link_down
;
3082 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
3083 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3084 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
3086 virtio_net_announce_timer
, n
);
3087 if (n
->announce_timer
.round
) {
3088 timer_mod(n
->announce_timer
.tm
,
3089 qemu_clock_get_ms(n
->announce_timer
.type
));
3091 qemu_announce_timer_del(&n
->announce_timer
, false);
3095 virtio_net_commit_rss_config(n
);
3099 static int virtio_net_post_load_virtio(VirtIODevice
*vdev
)
3101 VirtIONet
*n
= VIRTIO_NET(vdev
);
3103 * The actual needed state is now in saved_guest_offloads,
3104 * see virtio_net_post_load_device for detail.
3105 * Restore it back and apply the desired offloads.
3107 n
->curr_guest_offloads
= n
->saved_guest_offloads
;
3108 if (peer_has_vnet_hdr(n
)) {
3109 virtio_net_apply_guest_offloads(n
);
3115 /* tx_waiting field of a VirtIONetQueue */
3116 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting
= {
3117 .name
= "virtio-net-queue-tx_waiting",
3118 .fields
= (const VMStateField
[]) {
3119 VMSTATE_UINT32(tx_waiting
, VirtIONetQueue
),
3120 VMSTATE_END_OF_LIST()
3124 static bool max_queue_pairs_gt_1(void *opaque
, int version_id
)
3126 return VIRTIO_NET(opaque
)->max_queue_pairs
> 1;
3129 static bool has_ctrl_guest_offloads(void *opaque
, int version_id
)
3131 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque
),
3132 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
);
3135 static bool mac_table_fits(void *opaque
, int version_id
)
3137 return VIRTIO_NET(opaque
)->mac_table
.in_use
<= MAC_TABLE_ENTRIES
;
3140 static bool mac_table_doesnt_fit(void *opaque
, int version_id
)
3142 return !mac_table_fits(opaque
, version_id
);
3145 /* This temporary type is shared by all the WITH_TMP methods
3146 * although only some fields are used by each.
3148 struct VirtIONetMigTmp
{
3150 VirtIONetQueue
*vqs_1
;
3151 uint16_t curr_queue_pairs_1
;
3153 uint32_t has_vnet_hdr
;
3156 /* The 2nd and subsequent tx_waiting flags are loaded later than
3157 * the 1st entry in the queue_pairs and only if there's more than one
3158 * entry. We use the tmp mechanism to calculate a temporary
3159 * pointer and count and also validate the count.
3162 static int virtio_net_tx_waiting_pre_save(void *opaque
)
3164 struct VirtIONetMigTmp
*tmp
= opaque
;
3166 tmp
->vqs_1
= tmp
->parent
->vqs
+ 1;
3167 tmp
->curr_queue_pairs_1
= tmp
->parent
->curr_queue_pairs
- 1;
3168 if (tmp
->parent
->curr_queue_pairs
== 0) {
3169 tmp
->curr_queue_pairs_1
= 0;
3175 static int virtio_net_tx_waiting_pre_load(void *opaque
)
3177 struct VirtIONetMigTmp
*tmp
= opaque
;
3179 /* Reuse the pointer setup from save */
3180 virtio_net_tx_waiting_pre_save(opaque
);
3182 if (tmp
->parent
->curr_queue_pairs
> tmp
->parent
->max_queue_pairs
) {
3183 error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3184 tmp
->parent
->curr_queue_pairs
, tmp
->parent
->max_queue_pairs
);
3189 return 0; /* all good */
3192 static const VMStateDescription vmstate_virtio_net_tx_waiting
= {
3193 .name
= "virtio-net-tx_waiting",
3194 .pre_load
= virtio_net_tx_waiting_pre_load
,
3195 .pre_save
= virtio_net_tx_waiting_pre_save
,
3196 .fields
= (const VMStateField
[]) {
3197 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1
, struct VirtIONetMigTmp
,
3199 vmstate_virtio_net_queue_tx_waiting
,
3200 struct VirtIONetQueue
),
3201 VMSTATE_END_OF_LIST()
3205 /* the 'has_ufo' flag is just tested; if the incoming stream has the
3206 * flag set we need to check that we have it
3208 static int virtio_net_ufo_post_load(void *opaque
, int version_id
)
3210 struct VirtIONetMigTmp
*tmp
= opaque
;
3212 if (tmp
->has_ufo
&& !peer_has_ufo(tmp
->parent
)) {
3213 error_report("virtio-net: saved image requires TUN_F_UFO support");
3220 static int virtio_net_ufo_pre_save(void *opaque
)
3222 struct VirtIONetMigTmp
*tmp
= opaque
;
3224 tmp
->has_ufo
= tmp
->parent
->has_ufo
;
3229 static const VMStateDescription vmstate_virtio_net_has_ufo
= {
3230 .name
= "virtio-net-ufo",
3231 .post_load
= virtio_net_ufo_post_load
,
3232 .pre_save
= virtio_net_ufo_pre_save
,
3233 .fields
= (const VMStateField
[]) {
3234 VMSTATE_UINT8(has_ufo
, struct VirtIONetMigTmp
),
3235 VMSTATE_END_OF_LIST()
3239 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3240 * flag set we need to check that we have it
3242 static int virtio_net_vnet_post_load(void *opaque
, int version_id
)
3244 struct VirtIONetMigTmp
*tmp
= opaque
;
3246 if (tmp
->has_vnet_hdr
&& !peer_has_vnet_hdr(tmp
->parent
)) {
3247 error_report("virtio-net: saved image requires vnet_hdr=on");
3254 static int virtio_net_vnet_pre_save(void *opaque
)
3256 struct VirtIONetMigTmp
*tmp
= opaque
;
3258 tmp
->has_vnet_hdr
= tmp
->parent
->has_vnet_hdr
;
3263 static const VMStateDescription vmstate_virtio_net_has_vnet
= {
3264 .name
= "virtio-net-vnet",
3265 .post_load
= virtio_net_vnet_post_load
,
3266 .pre_save
= virtio_net_vnet_pre_save
,
3267 .fields
= (const VMStateField
[]) {
3268 VMSTATE_UINT32(has_vnet_hdr
, struct VirtIONetMigTmp
),
3269 VMSTATE_END_OF_LIST()
3273 static bool virtio_net_rss_needed(void *opaque
)
3275 return VIRTIO_NET(opaque
)->rss_data
.enabled
;
3278 static const VMStateDescription vmstate_virtio_net_rss
= {
3279 .name
= "virtio-net-device/rss",
3281 .minimum_version_id
= 1,
3282 .needed
= virtio_net_rss_needed
,
3283 .fields
= (const VMStateField
[]) {
3284 VMSTATE_BOOL(rss_data
.enabled
, VirtIONet
),
3285 VMSTATE_BOOL(rss_data
.redirect
, VirtIONet
),
3286 VMSTATE_BOOL(rss_data
.populate_hash
, VirtIONet
),
3287 VMSTATE_UINT32(rss_data
.hash_types
, VirtIONet
),
3288 VMSTATE_UINT16(rss_data
.indirections_len
, VirtIONet
),
3289 VMSTATE_UINT16(rss_data
.default_queue
, VirtIONet
),
3290 VMSTATE_UINT8_ARRAY(rss_data
.key
, VirtIONet
,
3291 VIRTIO_NET_RSS_MAX_KEY_SIZE
),
3292 VMSTATE_VARRAY_UINT16_ALLOC(rss_data
.indirections_table
, VirtIONet
,
3293 rss_data
.indirections_len
, 0,
3294 vmstate_info_uint16
, uint16_t),
3295 VMSTATE_END_OF_LIST()
3299 static const VMStateDescription vmstate_virtio_net_device
= {
3300 .name
= "virtio-net-device",
3301 .version_id
= VIRTIO_NET_VM_VERSION
,
3302 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
3303 .post_load
= virtio_net_post_load_device
,
3304 .fields
= (const VMStateField
[]) {
3305 VMSTATE_UINT8_ARRAY(mac
, VirtIONet
, ETH_ALEN
),
3306 VMSTATE_STRUCT_POINTER(vqs
, VirtIONet
,
3307 vmstate_virtio_net_queue_tx_waiting
,
3309 VMSTATE_UINT32(mergeable_rx_bufs
, VirtIONet
),
3310 VMSTATE_UINT16(status
, VirtIONet
),
3311 VMSTATE_UINT8(promisc
, VirtIONet
),
3312 VMSTATE_UINT8(allmulti
, VirtIONet
),
3313 VMSTATE_UINT32(mac_table
.in_use
, VirtIONet
),
3315 /* Guarded pair: If it fits we load it, else we throw it away
3316 * - can happen if source has a larger MAC table.; post-load
3317 * sets flags in this case.
3319 VMSTATE_VBUFFER_MULTIPLY(mac_table
.macs
, VirtIONet
,
3320 0, mac_table_fits
, mac_table
.in_use
,
3322 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet
, mac_table_doesnt_fit
, 0,
3323 mac_table
.in_use
, ETH_ALEN
),
3325 /* Note: This is an array of uint32's that's always been saved as a
3326 * buffer; hold onto your endiannesses; it's actually used as a bitmap
3327 * but based on the uint.
3329 VMSTATE_BUFFER_POINTER_UNSAFE(vlans
, VirtIONet
, 0, MAX_VLAN
>> 3),
3330 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3331 vmstate_virtio_net_has_vnet
),
3332 VMSTATE_UINT8(mac_table
.multi_overflow
, VirtIONet
),
3333 VMSTATE_UINT8(mac_table
.uni_overflow
, VirtIONet
),
3334 VMSTATE_UINT8(alluni
, VirtIONet
),
3335 VMSTATE_UINT8(nomulti
, VirtIONet
),
3336 VMSTATE_UINT8(nouni
, VirtIONet
),
3337 VMSTATE_UINT8(nobcast
, VirtIONet
),
3338 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3339 vmstate_virtio_net_has_ufo
),
3340 VMSTATE_SINGLE_TEST(max_queue_pairs
, VirtIONet
, max_queue_pairs_gt_1
, 0,
3341 vmstate_info_uint16_equal
, uint16_t),
3342 VMSTATE_UINT16_TEST(curr_queue_pairs
, VirtIONet
, max_queue_pairs_gt_1
),
3343 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3344 vmstate_virtio_net_tx_waiting
),
3345 VMSTATE_UINT64_TEST(curr_guest_offloads
, VirtIONet
,
3346 has_ctrl_guest_offloads
),
3347 VMSTATE_END_OF_LIST()
3349 .subsections
= (const VMStateDescription
* const []) {
3350 &vmstate_virtio_net_rss
,
3355 static NetClientInfo net_virtio_info
= {
3356 .type
= NET_CLIENT_DRIVER_NIC
,
3357 .size
= sizeof(NICState
),
3358 .can_receive
= virtio_net_can_receive
,
3359 .receive
= virtio_net_receive
,
3360 .link_status_changed
= virtio_net_set_link_status
,
3361 .query_rx_filter
= virtio_net_query_rxfilter
,
3362 .announce
= virtio_net_announce
,
3365 static bool virtio_net_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
3367 VirtIONet
*n
= VIRTIO_NET(vdev
);
3369 assert(n
->vhost_started
);
3370 if (!n
->multiqueue
&& idx
== 2) {
3371 /* Must guard against invalid features and bogus queue index
3372 * from being set by malicious guest, or penetrated through
3373 * buggy migration stream.
3375 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3376 qemu_log_mask(LOG_GUEST_ERROR
,
3377 "%s: bogus vq index ignored\n", __func__
);
3380 nc
= qemu_get_subqueue(n
->nic
, n
->max_queue_pairs
);
3382 nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3385 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
3386 * as the macro of configure interrupt's IDX, If this driver does not
3387 * support, the function will return false
3390 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
3391 return vhost_net_config_pending(get_vhost_net(nc
->peer
));
3393 return vhost_net_virtqueue_pending(get_vhost_net(nc
->peer
), idx
);
3396 static void virtio_net_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
3399 VirtIONet
*n
= VIRTIO_NET(vdev
);
3401 assert(n
->vhost_started
);
3402 if (!n
->multiqueue
&& idx
== 2) {
3403 /* Must guard against invalid features and bogus queue index
3404 * from being set by malicious guest, or penetrated through
3405 * buggy migration stream.
3407 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3408 qemu_log_mask(LOG_GUEST_ERROR
,
3409 "%s: bogus vq index ignored\n", __func__
);
3412 nc
= qemu_get_subqueue(n
->nic
, n
->max_queue_pairs
);
3414 nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3417 *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
3418 * as the macro of configure interrupt's IDX, If this driver does not
3419 * support, the function will return
3422 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
3423 vhost_net_config_mask(get_vhost_net(nc
->peer
), vdev
, mask
);
3426 vhost_net_virtqueue_mask(get_vhost_net(nc
->peer
), vdev
, idx
, mask
);
3429 static void virtio_net_set_config_size(VirtIONet
*n
, uint64_t host_features
)
3431 virtio_add_feature(&host_features
, VIRTIO_NET_F_MAC
);
3433 n
->config_size
= virtio_get_config_size(&cfg_size_params
, host_features
);
3436 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,
3440 * The name can be NULL, the netclient name will be type.x.
3442 assert(type
!= NULL
);
3444 g_free(n
->netclient_name
);
3445 g_free(n
->netclient_type
);
3446 n
->netclient_name
= g_strdup(name
);
3447 n
->netclient_type
= g_strdup(type
);
3450 static bool failover_unplug_primary(VirtIONet
*n
, DeviceState
*dev
)
3452 HotplugHandler
*hotplug_ctrl
;
3456 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3458 pci_dev
= PCI_DEVICE(dev
);
3459 pci_dev
->partially_hotplugged
= true;
3460 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &err
);
3462 error_report_err(err
);
3471 static bool failover_replug_primary(VirtIONet
*n
, DeviceState
*dev
,
3475 HotplugHandler
*hotplug_ctrl
;
3476 PCIDevice
*pdev
= PCI_DEVICE(dev
);
3477 BusState
*primary_bus
;
3479 if (!pdev
->partially_hotplugged
) {
3482 primary_bus
= dev
->parent_bus
;
3484 error_setg(errp
, "virtio_net: couldn't find primary bus");
3487 qdev_set_parent_bus(dev
, primary_bus
, &error_abort
);
3488 qatomic_set(&n
->failover_primary_hidden
, false);
3489 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3491 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &err
);
3495 hotplug_handler_plug(hotplug_ctrl
, dev
, &err
);
3497 pdev
->partially_hotplugged
= false;
3500 error_propagate(errp
, err
);
3504 static void virtio_net_handle_migration_primary(VirtIONet
*n
, MigrationEvent
*e
)
3506 bool should_be_hidden
;
3508 DeviceState
*dev
= failover_find_primary_device(n
);
3514 should_be_hidden
= qatomic_read(&n
->failover_primary_hidden
);
3516 if (e
->type
== MIG_EVENT_PRECOPY_SETUP
&& !should_be_hidden
) {
3517 if (failover_unplug_primary(n
, dev
)) {
3518 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
3519 qapi_event_send_unplug_primary(dev
->id
);
3520 qatomic_set(&n
->failover_primary_hidden
, true);
3522 warn_report("couldn't unplug primary device");
3524 } else if (e
->type
== MIG_EVENT_PRECOPY_FAILED
) {
3525 /* We already unplugged the device let's plug it back */
3526 if (!failover_replug_primary(n
, dev
, &err
)) {
3528 error_report_err(err
);
3534 static int virtio_net_migration_state_notifier(NotifierWithReturn
*notifier
,
3535 MigrationEvent
*e
, Error
**errp
)
3537 VirtIONet
*n
= container_of(notifier
, VirtIONet
, migration_state
);
3538 virtio_net_handle_migration_primary(n
, e
);
3542 static bool failover_hide_primary_device(DeviceListener
*listener
,
3543 const QDict
*device_opts
,
3547 VirtIONet
*n
= container_of(listener
, VirtIONet
, primary_listener
);
3548 const char *standby_id
;
3554 if (!qdict_haskey(device_opts
, "failover_pair_id")) {
3558 if (!qdict_haskey(device_opts
, "id")) {
3559 error_setg(errp
, "Device with failover_pair_id needs to have id");
3563 standby_id
= qdict_get_str(device_opts
, "failover_pair_id");
3564 if (g_strcmp0(standby_id
, n
->netclient_name
) != 0) {
3569 * The hide helper can be called several times for a given device.
3570 * Check there is only one primary for a virtio-net device but
3571 * don't duplicate the qdict several times if it's called for the same
3574 if (n
->primary_opts
) {
3575 const char *old
, *new;
3576 /* devices with failover_pair_id always have an id */
3577 old
= qdict_get_str(n
->primary_opts
, "id");
3578 new = qdict_get_str(device_opts
, "id");
3579 if (strcmp(old
, new) != 0) {
3580 error_setg(errp
, "Cannot attach more than one primary device to "
3581 "'%s': '%s' and '%s'", n
->netclient_name
, old
, new);
3585 n
->primary_opts
= qdict_clone_shallow(device_opts
);
3586 n
->primary_opts_from_json
= from_json
;
3589 /* failover_primary_hidden is set during feature negotiation */
3590 return qatomic_read(&n
->failover_primary_hidden
);
3593 static void virtio_net_device_realize(DeviceState
*dev
, Error
**errp
)
3595 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3596 VirtIONet
*n
= VIRTIO_NET(dev
);
3600 if (n
->net_conf
.mtu
) {
3601 n
->host_features
|= (1ULL << VIRTIO_NET_F_MTU
);
3604 if (n
->net_conf
.duplex_str
) {
3605 if (strncmp(n
->net_conf
.duplex_str
, "half", 5) == 0) {
3606 n
->net_conf
.duplex
= DUPLEX_HALF
;
3607 } else if (strncmp(n
->net_conf
.duplex_str
, "full", 5) == 0) {
3608 n
->net_conf
.duplex
= DUPLEX_FULL
;
3610 error_setg(errp
, "'duplex' must be 'half' or 'full'");
3613 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3615 n
->net_conf
.duplex
= DUPLEX_UNKNOWN
;
3618 if (n
->net_conf
.speed
< SPEED_UNKNOWN
) {
3619 error_setg(errp
, "'speed' must be between 0 and INT_MAX");
3622 if (n
->net_conf
.speed
>= 0) {
3623 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3627 n
->primary_listener
.hide_device
= failover_hide_primary_device
;
3628 qatomic_set(&n
->failover_primary_hidden
, true);
3629 device_listener_register(&n
->primary_listener
);
3630 migration_add_notifier(&n
->migration_state
,
3631 virtio_net_migration_state_notifier
);
3632 n
->host_features
|= (1ULL << VIRTIO_NET_F_STANDBY
);
3635 virtio_net_set_config_size(n
, n
->host_features
);
3636 virtio_init(vdev
, VIRTIO_ID_NET
, n
->config_size
);
3639 * We set a lower limit on RX queue size to what it always was.
3640 * Guests that want a smaller ring can always resize it without
3641 * help from us (using virtio 1 and up).
3643 if (n
->net_conf
.rx_queue_size
< VIRTIO_NET_RX_QUEUE_MIN_SIZE
||
3644 n
->net_conf
.rx_queue_size
> VIRTQUEUE_MAX_SIZE
||
3645 !is_power_of_2(n
->net_conf
.rx_queue_size
)) {
3646 error_setg(errp
, "Invalid rx_queue_size (= %" PRIu16
"), "
3647 "must be a power of 2 between %d and %d.",
3648 n
->net_conf
.rx_queue_size
, VIRTIO_NET_RX_QUEUE_MIN_SIZE
,
3649 VIRTQUEUE_MAX_SIZE
);
3650 virtio_cleanup(vdev
);
3654 if (n
->net_conf
.tx_queue_size
< VIRTIO_NET_TX_QUEUE_MIN_SIZE
||
3655 n
->net_conf
.tx_queue_size
> virtio_net_max_tx_queue_size(n
) ||
3656 !is_power_of_2(n
->net_conf
.tx_queue_size
)) {
3657 error_setg(errp
, "Invalid tx_queue_size (= %" PRIu16
"), "
3658 "must be a power of 2 between %d and %d",
3659 n
->net_conf
.tx_queue_size
, VIRTIO_NET_TX_QUEUE_MIN_SIZE
,
3660 virtio_net_max_tx_queue_size(n
));
3661 virtio_cleanup(vdev
);
3665 n
->max_ncs
= MAX(n
->nic_conf
.peers
.queues
, 1);
3668 * Figure out the datapath queue pairs since the backend could
3669 * provide control queue via peers as well.
3671 if (n
->nic_conf
.peers
.queues
) {
3672 for (i
= 0; i
< n
->max_ncs
; i
++) {
3673 if (n
->nic_conf
.peers
.ncs
[i
]->is_datapath
) {
3674 ++n
->max_queue_pairs
;
3678 n
->max_queue_pairs
= MAX(n
->max_queue_pairs
, 1);
3680 if (n
->max_queue_pairs
* 2 + 1 > VIRTIO_QUEUE_MAX
) {
3681 error_setg(errp
, "Invalid number of queue pairs (= %" PRIu32
"), "
3682 "must be a positive integer less than %d.",
3683 n
->max_queue_pairs
, (VIRTIO_QUEUE_MAX
- 1) / 2);
3684 virtio_cleanup(vdev
);
3687 n
->vqs
= g_new0(VirtIONetQueue
, n
->max_queue_pairs
);
3688 n
->curr_queue_pairs
= 1;
3689 n
->tx_timeout
= n
->net_conf
.txtimer
;
3691 if (n
->net_conf
.tx
&& strcmp(n
->net_conf
.tx
, "timer")
3692 && strcmp(n
->net_conf
.tx
, "bh")) {
3693 warn_report("virtio-net: "
3694 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3696 error_printf("Defaulting to \"bh\"");
3699 n
->net_conf
.tx_queue_size
= MIN(virtio_net_max_tx_queue_size(n
),
3700 n
->net_conf
.tx_queue_size
);
3702 virtio_net_add_queue(n
, 0);
3704 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
3705 qemu_macaddr_default_if_unset(&n
->nic_conf
.macaddr
);
3706 memcpy(&n
->mac
[0], &n
->nic_conf
.macaddr
, sizeof(n
->mac
));
3707 n
->status
= VIRTIO_NET_S_LINK_UP
;
3708 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
3710 virtio_net_announce_timer
, n
);
3711 n
->announce_timer
.round
= 0;
3713 if (n
->netclient_type
) {
3715 * Happen when virtio_net_set_netclient_name has been called.
3717 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3718 n
->netclient_type
, n
->netclient_name
,
3719 &dev
->mem_reentrancy_guard
, n
);
3721 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3722 object_get_typename(OBJECT(dev
)), dev
->id
,
3723 &dev
->mem_reentrancy_guard
, n
);
3726 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3727 n
->nic
->ncs
[i
].do_not_pad
= true;
3730 peer_test_vnet_hdr(n
);
3731 if (peer_has_vnet_hdr(n
)) {
3732 n
->host_hdr_len
= sizeof(struct virtio_net_hdr
);
3734 n
->host_hdr_len
= 0;
3737 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->nic_conf
.macaddr
.a
);
3739 n
->vqs
[0].tx_waiting
= 0;
3740 n
->tx_burst
= n
->net_conf
.txburst
;
3741 virtio_net_set_mrg_rx_bufs(n
, 0, 0, 0);
3742 n
->promisc
= 1; /* for compatibility */
3744 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
3746 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
3748 nc
= qemu_get_queue(n
->nic
);
3749 nc
->rxfilter_notify_enabled
= 1;
3751 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
3752 struct virtio_net_config netcfg
= {};
3753 memcpy(&netcfg
.mac
, &n
->nic_conf
.macaddr
, ETH_ALEN
);
3754 vhost_net_set_config(get_vhost_net(nc
->peer
),
3755 (uint8_t *)&netcfg
, 0, ETH_ALEN
, VHOST_SET_CONFIG_TYPE_FRONTEND
);
3757 QTAILQ_INIT(&n
->rsc_chains
);
3760 net_rx_pkt_init(&n
->rx_pkt
);
3762 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3763 virtio_net_load_ebpf(n
);
3767 static void virtio_net_device_unrealize(DeviceState
*dev
)
3769 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3770 VirtIONet
*n
= VIRTIO_NET(dev
);
3771 int i
, max_queue_pairs
;
3773 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3774 virtio_net_unload_ebpf(n
);
3777 /* This will stop vhost backend if appropriate. */
3778 virtio_net_set_status(vdev
, 0);
3780 g_free(n
->netclient_name
);
3781 n
->netclient_name
= NULL
;
3782 g_free(n
->netclient_type
);
3783 n
->netclient_type
= NULL
;
3785 g_free(n
->mac_table
.macs
);
3789 qobject_unref(n
->primary_opts
);
3790 device_listener_unregister(&n
->primary_listener
);
3791 migration_remove_notifier(&n
->migration_state
);
3793 assert(n
->primary_opts
== NULL
);
3796 max_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
3797 for (i
= 0; i
< max_queue_pairs
; i
++) {
3798 virtio_net_del_queue(n
, i
);
3800 /* delete also control vq */
3801 virtio_del_queue(vdev
, max_queue_pairs
* 2);
3802 qemu_announce_timer_del(&n
->announce_timer
, false);
3804 qemu_del_nic(n
->nic
);
3805 virtio_net_rsc_cleanup(n
);
3806 g_free(n
->rss_data
.indirections_table
);
3807 net_rx_pkt_uninit(n
->rx_pkt
);
3808 virtio_cleanup(vdev
);
3811 static void virtio_net_reset(VirtIODevice
*vdev
)
3813 VirtIONet
*n
= VIRTIO_NET(vdev
);
3816 /* Reset back to compatibility mode */
3823 /* multiqueue is disabled by default */
3824 n
->curr_queue_pairs
= 1;
3825 timer_del(n
->announce_timer
.tm
);
3826 n
->announce_timer
.round
= 0;
3827 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
3829 /* Flush any MAC and VLAN filter table state */
3830 n
->mac_table
.in_use
= 0;
3831 n
->mac_table
.first_multi
= 0;
3832 n
->mac_table
.multi_overflow
= 0;
3833 n
->mac_table
.uni_overflow
= 0;
3834 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
3835 memcpy(&n
->mac
[0], &n
->nic
->conf
->macaddr
, sizeof(n
->mac
));
3836 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
3837 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
3839 /* Flush any async TX */
3840 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3841 flush_or_purge_queued_packets(qemu_get_subqueue(n
->nic
, i
));
3844 virtio_net_disable_rss(n
);
3847 static void virtio_net_instance_init(Object
*obj
)
3849 VirtIONet
*n
= VIRTIO_NET(obj
);
3852 * The default config_size is sizeof(struct virtio_net_config).
3853 * Can be overridden with virtio_net_set_config_size.
3855 n
->config_size
= sizeof(struct virtio_net_config
);
3856 device_add_bootindex_property(obj
, &n
->nic_conf
.bootindex
,
3857 "bootindex", "/ethernet-phy@0",
3860 ebpf_rss_init(&n
->ebpf_rss
);
3863 static int virtio_net_pre_save(void *opaque
)
3865 VirtIONet
*n
= opaque
;
3867 /* At this point, backend must be stopped, otherwise
3868 * it might keep writing to memory. */
3869 assert(!n
->vhost_started
);
3874 static bool primary_unplug_pending(void *opaque
)
3876 DeviceState
*dev
= opaque
;
3877 DeviceState
*primary
;
3878 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3879 VirtIONet
*n
= VIRTIO_NET(vdev
);
3881 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_STANDBY
)) {
3884 primary
= failover_find_primary_device(n
);
3885 return primary
? primary
->pending_deleted_event
: false;
3888 static bool dev_unplug_pending(void *opaque
)
3890 DeviceState
*dev
= opaque
;
3891 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3893 return vdc
->primary_unplug_pending(dev
);
3896 static struct vhost_dev
*virtio_net_get_vhost(VirtIODevice
*vdev
)
3898 VirtIONet
*n
= VIRTIO_NET(vdev
);
3900 struct vhost_net
*net
;
3906 nc
= qemu_get_queue(n
->nic
);
3911 net
= get_vhost_net(nc
->peer
);
3919 static const VMStateDescription vmstate_virtio_net
= {
3920 .name
= "virtio-net",
3921 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
3922 .version_id
= VIRTIO_NET_VM_VERSION
,
3923 .fields
= (const VMStateField
[]) {
3924 VMSTATE_VIRTIO_DEVICE
,
3925 VMSTATE_END_OF_LIST()
3927 .pre_save
= virtio_net_pre_save
,
3928 .dev_unplug_pending
= dev_unplug_pending
,
3931 static Property virtio_net_properties
[] = {
3932 DEFINE_PROP_BIT64("csum", VirtIONet
, host_features
,
3933 VIRTIO_NET_F_CSUM
, true),
3934 DEFINE_PROP_BIT64("guest_csum", VirtIONet
, host_features
,
3935 VIRTIO_NET_F_GUEST_CSUM
, true),
3936 DEFINE_PROP_BIT64("gso", VirtIONet
, host_features
, VIRTIO_NET_F_GSO
, true),
3937 DEFINE_PROP_BIT64("guest_tso4", VirtIONet
, host_features
,
3938 VIRTIO_NET_F_GUEST_TSO4
, true),
3939 DEFINE_PROP_BIT64("guest_tso6", VirtIONet
, host_features
,
3940 VIRTIO_NET_F_GUEST_TSO6
, true),
3941 DEFINE_PROP_BIT64("guest_ecn", VirtIONet
, host_features
,
3942 VIRTIO_NET_F_GUEST_ECN
, true),
3943 DEFINE_PROP_BIT64("guest_ufo", VirtIONet
, host_features
,
3944 VIRTIO_NET_F_GUEST_UFO
, true),
3945 DEFINE_PROP_BIT64("guest_announce", VirtIONet
, host_features
,
3946 VIRTIO_NET_F_GUEST_ANNOUNCE
, true),
3947 DEFINE_PROP_BIT64("host_tso4", VirtIONet
, host_features
,
3948 VIRTIO_NET_F_HOST_TSO4
, true),
3949 DEFINE_PROP_BIT64("host_tso6", VirtIONet
, host_features
,
3950 VIRTIO_NET_F_HOST_TSO6
, true),
3951 DEFINE_PROP_BIT64("host_ecn", VirtIONet
, host_features
,
3952 VIRTIO_NET_F_HOST_ECN
, true),
3953 DEFINE_PROP_BIT64("host_ufo", VirtIONet
, host_features
,
3954 VIRTIO_NET_F_HOST_UFO
, true),
3955 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet
, host_features
,
3956 VIRTIO_NET_F_MRG_RXBUF
, true),
3957 DEFINE_PROP_BIT64("status", VirtIONet
, host_features
,
3958 VIRTIO_NET_F_STATUS
, true),
3959 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet
, host_features
,
3960 VIRTIO_NET_F_CTRL_VQ
, true),
3961 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet
, host_features
,
3962 VIRTIO_NET_F_CTRL_RX
, true),
3963 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet
, host_features
,
3964 VIRTIO_NET_F_CTRL_VLAN
, true),
3965 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet
, host_features
,
3966 VIRTIO_NET_F_CTRL_RX_EXTRA
, true),
3967 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet
, host_features
,
3968 VIRTIO_NET_F_CTRL_MAC_ADDR
, true),
3969 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet
, host_features
,
3970 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
, true),
3971 DEFINE_PROP_BIT64("mq", VirtIONet
, host_features
, VIRTIO_NET_F_MQ
, false),
3972 DEFINE_PROP_BIT64("rss", VirtIONet
, host_features
,
3973 VIRTIO_NET_F_RSS
, false),
3974 DEFINE_PROP_BIT64("hash", VirtIONet
, host_features
,
3975 VIRTIO_NET_F_HASH_REPORT
, false),
3976 DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet
, nr_ebpf_rss_fds
,
3977 ebpf_rss_fds
, qdev_prop_string
, char*),
3978 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet
, host_features
,
3979 VIRTIO_NET_F_RSC_EXT
, false),
3980 DEFINE_PROP_UINT32("rsc_interval", VirtIONet
, rsc_timeout
,
3981 VIRTIO_NET_RSC_DEFAULT_INTERVAL
),
3982 DEFINE_NIC_PROPERTIES(VirtIONet
, nic_conf
),
3983 DEFINE_PROP_UINT32("x-txtimer", VirtIONet
, net_conf
.txtimer
,
3985 DEFINE_PROP_INT32("x-txburst", VirtIONet
, net_conf
.txburst
, TX_BURST
),
3986 DEFINE_PROP_STRING("tx", VirtIONet
, net_conf
.tx
),
3987 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet
, net_conf
.rx_queue_size
,
3988 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
),
3989 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet
, net_conf
.tx_queue_size
,
3990 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
),
3991 DEFINE_PROP_UINT16("host_mtu", VirtIONet
, net_conf
.mtu
, 0),
3992 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet
, mtu_bypass_backend
,
3994 DEFINE_PROP_INT32("speed", VirtIONet
, net_conf
.speed
, SPEED_UNKNOWN
),
3995 DEFINE_PROP_STRING("duplex", VirtIONet
, net_conf
.duplex_str
),
3996 DEFINE_PROP_BOOL("failover", VirtIONet
, failover
, false),
3997 DEFINE_PROP_BIT64("guest_uso4", VirtIONet
, host_features
,
3998 VIRTIO_NET_F_GUEST_USO4
, true),
3999 DEFINE_PROP_BIT64("guest_uso6", VirtIONet
, host_features
,
4000 VIRTIO_NET_F_GUEST_USO6
, true),
4001 DEFINE_PROP_BIT64("host_uso", VirtIONet
, host_features
,
4002 VIRTIO_NET_F_HOST_USO
, true),
4003 DEFINE_PROP_END_OF_LIST(),
4006 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
4008 DeviceClass
*dc
= DEVICE_CLASS(klass
);
4009 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
4011 device_class_set_props(dc
, virtio_net_properties
);
4012 dc
->vmsd
= &vmstate_virtio_net
;
4013 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
4014 vdc
->realize
= virtio_net_device_realize
;
4015 vdc
->unrealize
= virtio_net_device_unrealize
;
4016 vdc
->get_config
= virtio_net_get_config
;
4017 vdc
->set_config
= virtio_net_set_config
;
4018 vdc
->get_features
= virtio_net_get_features
;
4019 vdc
->set_features
= virtio_net_set_features
;
4020 vdc
->bad_features
= virtio_net_bad_features
;
4021 vdc
->reset
= virtio_net_reset
;
4022 vdc
->queue_reset
= virtio_net_queue_reset
;
4023 vdc
->queue_enable
= virtio_net_queue_enable
;
4024 vdc
->set_status
= virtio_net_set_status
;
4025 vdc
->guest_notifier_mask
= virtio_net_guest_notifier_mask
;
4026 vdc
->guest_notifier_pending
= virtio_net_guest_notifier_pending
;
4027 vdc
->legacy_features
|= (0x1 << VIRTIO_NET_F_GSO
);
4028 vdc
->post_load
= virtio_net_post_load_virtio
;
4029 vdc
->vmsd
= &vmstate_virtio_net_device
;
4030 vdc
->primary_unplug_pending
= primary_unplug_pending
;
4031 vdc
->get_vhost
= virtio_net_get_vhost
;
4032 vdc
->toggle_device_iotlb
= vhost_toggle_device_iotlb
;
4035 static const TypeInfo virtio_net_info
= {
4036 .name
= TYPE_VIRTIO_NET
,
4037 .parent
= TYPE_VIRTIO_DEVICE
,
4038 .instance_size
= sizeof(VirtIONet
),
4039 .instance_init
= virtio_net_instance_init
,
4040 .class_init
= virtio_net_class_init
,
4043 static void virtio_register_types(void)
4045 type_register_static(&virtio_net_info
);
4048 type_init(virtio_register_types
)