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"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
19 #include "hw/virtio/virtio.h"
21 #include "net/checksum.h"
23 #include "qemu/error-report.h"
24 #include "qemu/timer.h"
25 #include "qemu/option.h"
26 #include "qemu/option_int.h"
27 #include "qemu/config-file.h"
28 #include "qapi/qmp/qdict.h"
29 #include "hw/virtio/virtio-net.h"
30 #include "net/vhost_net.h"
31 #include "net/announce.h"
32 #include "hw/virtio/virtio-bus.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-net.h"
35 #include "hw/qdev-properties.h"
36 #include "qapi/qapi-types-migration.h"
37 #include "qapi/qapi-events-migration.h"
38 #include "hw/virtio/virtio-access.h"
39 #include "migration/misc.h"
40 #include "standard-headers/linux/ethtool.h"
41 #include "sysemu/sysemu.h"
43 #include "monitor/qdev.h"
44 #include "hw/pci/pci.h"
45 #include "net_rx_pkt.h"
46 #include "hw/virtio/vhost.h"
48 #define VIRTIO_NET_VM_VERSION 11
50 #define MAC_TABLE_ENTRIES 64
51 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
53 /* previously fixed value */
54 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
55 #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
57 /* for now, only allow larger queues; with virtio-1, guest can downsize */
58 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
59 #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
61 #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */
63 #define VIRTIO_NET_TCP_FLAG 0x3F
64 #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000
66 /* IPv4 max payload, 16 bits in the header */
67 #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
68 #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
70 /* header length value in ip header without option */
71 #define VIRTIO_NET_IP4_HEADER_LENGTH 5
73 #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */
74 #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
76 /* Purge coalesced packets timer interval, This value affects the performance
77 a lot, and should be tuned carefully, '300000'(300us) is the recommended
78 value to pass the WHQL test, '50000' can gain 2x netperf throughput with
80 #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
82 #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
83 VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
84 VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
85 VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
86 VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
87 VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
88 VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
89 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
90 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
92 static const VirtIOFeature feature_sizes
[] = {
93 {.flags
= 1ULL << VIRTIO_NET_F_MAC
,
94 .end
= endof(struct virtio_net_config
, mac
)},
95 {.flags
= 1ULL << VIRTIO_NET_F_STATUS
,
96 .end
= endof(struct virtio_net_config
, status
)},
97 {.flags
= 1ULL << VIRTIO_NET_F_MQ
,
98 .end
= endof(struct virtio_net_config
, max_virtqueue_pairs
)},
99 {.flags
= 1ULL << VIRTIO_NET_F_MTU
,
100 .end
= endof(struct virtio_net_config
, mtu
)},
101 {.flags
= 1ULL << VIRTIO_NET_F_SPEED_DUPLEX
,
102 .end
= endof(struct virtio_net_config
, duplex
)},
103 {.flags
= (1ULL << VIRTIO_NET_F_RSS
) | (1ULL << VIRTIO_NET_F_HASH_REPORT
),
104 .end
= endof(struct virtio_net_config
, supported_hash_types
)},
108 static VirtIONetQueue
*virtio_net_get_subqueue(NetClientState
*nc
)
110 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
112 return &n
->vqs
[nc
->queue_index
];
115 static int vq2q(int queue_index
)
117 return queue_index
/ 2;
121 * - we could suppress RX interrupt if we were so inclined.
124 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
126 VirtIONet
*n
= VIRTIO_NET(vdev
);
127 struct virtio_net_config netcfg
;
128 NetClientState
*nc
= qemu_get_queue(n
->nic
);
129 static const MACAddr zero
= { .a
= { 0, 0, 0, 0, 0, 0 } };
132 memset(&netcfg
, 0 , sizeof(struct virtio_net_config
));
133 virtio_stw_p(vdev
, &netcfg
.status
, n
->status
);
134 virtio_stw_p(vdev
, &netcfg
.max_virtqueue_pairs
, n
->max_queues
);
135 virtio_stw_p(vdev
, &netcfg
.mtu
, n
->net_conf
.mtu
);
136 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
137 virtio_stl_p(vdev
, &netcfg
.speed
, n
->net_conf
.speed
);
138 netcfg
.duplex
= n
->net_conf
.duplex
;
139 netcfg
.rss_max_key_size
= VIRTIO_NET_RSS_MAX_KEY_SIZE
;
140 virtio_stw_p(vdev
, &netcfg
.rss_max_indirection_table_length
,
141 virtio_host_has_feature(vdev
, VIRTIO_NET_F_RSS
) ?
142 VIRTIO_NET_RSS_MAX_TABLE_LEN
: 1);
143 virtio_stl_p(vdev
, &netcfg
.supported_hash_types
,
144 VIRTIO_NET_RSS_SUPPORTED_HASHES
);
145 memcpy(config
, &netcfg
, n
->config_size
);
148 * Is this VDPA? No peer means not VDPA: there's no way to
149 * disconnect/reconnect a VDPA peer.
151 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
152 ret
= vhost_net_get_config(get_vhost_net(nc
->peer
), (uint8_t *)&netcfg
,
156 * Some NIC/kernel combinations present 0 as the mac address. As
157 * that is not a legal address, try to proceed with the
158 * address from the QEMU command line in the hope that the
159 * address has been configured correctly elsewhere - just not
160 * reported by the device.
162 if (memcmp(&netcfg
.mac
, &zero
, sizeof(zero
)) == 0) {
163 info_report("Zero hardware mac address detected. Ignoring.");
164 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
166 memcpy(config
, &netcfg
, n
->config_size
);
171 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
173 VirtIONet
*n
= VIRTIO_NET(vdev
);
174 struct virtio_net_config netcfg
= {};
175 NetClientState
*nc
= qemu_get_queue(n
->nic
);
177 memcpy(&netcfg
, config
, n
->config_size
);
179 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_MAC_ADDR
) &&
180 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
181 memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
182 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
183 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
187 * Is this VDPA? No peer means not VDPA: there's no way to
188 * disconnect/reconnect a VDPA peer.
190 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
191 vhost_net_set_config(get_vhost_net(nc
->peer
),
192 (uint8_t *)&netcfg
, 0, n
->config_size
,
193 VHOST_SET_CONFIG_TYPE_MASTER
);
197 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
199 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
200 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
201 (n
->status
& VIRTIO_NET_S_LINK_UP
) && vdev
->vm_running
;
204 static void virtio_net_announce_notify(VirtIONet
*net
)
206 VirtIODevice
*vdev
= VIRTIO_DEVICE(net
);
207 trace_virtio_net_announce_notify();
209 net
->status
|= VIRTIO_NET_S_ANNOUNCE
;
210 virtio_notify_config(vdev
);
213 static void virtio_net_announce_timer(void *opaque
)
215 VirtIONet
*n
= opaque
;
216 trace_virtio_net_announce_timer(n
->announce_timer
.round
);
218 n
->announce_timer
.round
--;
219 virtio_net_announce_notify(n
);
222 static void virtio_net_announce(NetClientState
*nc
)
224 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
225 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
228 * Make sure the virtio migration announcement timer isn't running
229 * If it is, let it trigger announcement so that we do not cause
232 if (n
->announce_timer
.round
) {
236 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
237 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
238 virtio_net_announce_notify(n
);
242 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
244 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
245 NetClientState
*nc
= qemu_get_queue(n
->nic
);
246 int queues
= n
->multiqueue
? n
->max_queues
: 1;
248 if (!get_vhost_net(nc
->peer
)) {
252 if ((virtio_net_started(n
, status
) && !nc
->peer
->link_down
) ==
253 !!n
->vhost_started
) {
256 if (!n
->vhost_started
) {
259 if (n
->needs_vnet_hdr_swap
) {
260 error_report("backend does not support %s vnet headers; "
261 "falling back on userspace virtio",
262 virtio_is_big_endian(vdev
) ? "BE" : "LE");
266 /* Any packets outstanding? Purge them to avoid touching rings
267 * when vhost is running.
269 for (i
= 0; i
< queues
; i
++) {
270 NetClientState
*qnc
= qemu_get_subqueue(n
->nic
, i
);
272 /* Purge both directions: TX and RX. */
273 qemu_net_queue_purge(qnc
->peer
->incoming_queue
, qnc
);
274 qemu_net_queue_purge(qnc
->incoming_queue
, qnc
->peer
);
277 if (virtio_has_feature(vdev
->guest_features
, VIRTIO_NET_F_MTU
)) {
278 r
= vhost_net_set_mtu(get_vhost_net(nc
->peer
), n
->net_conf
.mtu
);
280 error_report("%uBytes MTU not supported by the backend",
287 n
->vhost_started
= 1;
288 r
= vhost_net_start(vdev
, n
->nic
->ncs
, queues
);
290 error_report("unable to start vhost net: %d: "
291 "falling back on userspace virtio", -r
);
292 n
->vhost_started
= 0;
295 vhost_net_stop(vdev
, n
->nic
->ncs
, queues
);
296 n
->vhost_started
= 0;
300 static int virtio_net_set_vnet_endian_one(VirtIODevice
*vdev
,
301 NetClientState
*peer
,
304 if (virtio_is_big_endian(vdev
)) {
305 return qemu_set_vnet_be(peer
, enable
);
307 return qemu_set_vnet_le(peer
, enable
);
311 static bool virtio_net_set_vnet_endian(VirtIODevice
*vdev
, NetClientState
*ncs
,
312 int queues
, bool enable
)
316 for (i
= 0; i
< queues
; i
++) {
317 if (virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, enable
) < 0 &&
320 virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, false);
330 static void virtio_net_vnet_endian_status(VirtIONet
*n
, uint8_t status
)
332 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
333 int queues
= n
->multiqueue
? n
->max_queues
: 1;
335 if (virtio_net_started(n
, status
)) {
336 /* Before using the device, we tell the network backend about the
337 * endianness to use when parsing vnet headers. If the backend
338 * can't do it, we fallback onto fixing the headers in the core
341 n
->needs_vnet_hdr_swap
= virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
,
343 } else if (virtio_net_started(n
, vdev
->status
)) {
344 /* After using the device, we need to reset the network backend to
345 * the default (guest native endianness), otherwise the guest may
346 * lose network connectivity if it is rebooted into a different
349 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
, queues
, false);
353 static void virtio_net_drop_tx_queue_data(VirtIODevice
*vdev
, VirtQueue
*vq
)
355 unsigned int dropped
= virtqueue_drop_all(vq
);
357 virtio_notify(vdev
, vq
);
361 static void virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
363 VirtIONet
*n
= VIRTIO_NET(vdev
);
366 uint8_t queue_status
;
368 virtio_net_vnet_endian_status(n
, status
);
369 virtio_net_vhost_status(n
, status
);
371 for (i
= 0; i
< n
->max_queues
; i
++) {
372 NetClientState
*ncs
= qemu_get_subqueue(n
->nic
, i
);
376 if ((!n
->multiqueue
&& i
!= 0) || i
>= n
->curr_queues
) {
379 queue_status
= status
;
382 virtio_net_started(n
, queue_status
) && !n
->vhost_started
;
385 qemu_flush_queued_packets(ncs
);
388 if (!q
->tx_waiting
) {
394 timer_mod(q
->tx_timer
,
395 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
397 qemu_bh_schedule(q
->tx_bh
);
401 timer_del(q
->tx_timer
);
403 qemu_bh_cancel(q
->tx_bh
);
405 if ((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0 &&
406 (queue_status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
408 /* if tx is waiting we are likely have some packets in tx queue
409 * and disabled notification */
411 virtio_queue_set_notification(q
->tx_vq
, 1);
412 virtio_net_drop_tx_queue_data(vdev
, q
->tx_vq
);
418 static void virtio_net_set_link_status(NetClientState
*nc
)
420 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
421 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
422 uint16_t old_status
= n
->status
;
425 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
427 n
->status
|= VIRTIO_NET_S_LINK_UP
;
429 if (n
->status
!= old_status
)
430 virtio_notify_config(vdev
);
432 virtio_net_set_status(vdev
, vdev
->status
);
435 static void rxfilter_notify(NetClientState
*nc
)
437 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
439 if (nc
->rxfilter_notify_enabled
) {
440 char *path
= object_get_canonical_path(OBJECT(n
->qdev
));
441 qapi_event_send_nic_rx_filter_changed(!!n
->netclient_name
,
442 n
->netclient_name
, path
);
445 /* disable event notification to avoid events flooding */
446 nc
->rxfilter_notify_enabled
= 0;
450 static intList
*get_vlan_table(VirtIONet
*n
)
456 for (i
= 0; i
< MAX_VLAN
>> 5; i
++) {
457 for (j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
458 if (n
->vlans
[i
] & (1U << j
)) {
459 QAPI_LIST_PREPEND(list
, (i
<< 5) + j
);
467 static RxFilterInfo
*virtio_net_query_rxfilter(NetClientState
*nc
)
469 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
470 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
475 info
= g_malloc0(sizeof(*info
));
476 info
->name
= g_strdup(nc
->name
);
477 info
->promiscuous
= n
->promisc
;
480 info
->unicast
= RX_STATE_NONE
;
481 } else if (n
->alluni
) {
482 info
->unicast
= RX_STATE_ALL
;
484 info
->unicast
= RX_STATE_NORMAL
;
488 info
->multicast
= RX_STATE_NONE
;
489 } else if (n
->allmulti
) {
490 info
->multicast
= RX_STATE_ALL
;
492 info
->multicast
= RX_STATE_NORMAL
;
495 info
->broadcast_allowed
= n
->nobcast
;
496 info
->multicast_overflow
= n
->mac_table
.multi_overflow
;
497 info
->unicast_overflow
= n
->mac_table
.uni_overflow
;
499 info
->main_mac
= qemu_mac_strdup_printf(n
->mac
);
502 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
503 QAPI_LIST_PREPEND(str_list
,
504 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
506 info
->unicast_table
= str_list
;
509 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
510 QAPI_LIST_PREPEND(str_list
,
511 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
513 info
->multicast_table
= str_list
;
514 info
->vlan_table
= get_vlan_table(n
);
516 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VLAN
)) {
517 info
->vlan
= RX_STATE_ALL
;
518 } else if (!info
->vlan_table
) {
519 info
->vlan
= RX_STATE_NONE
;
521 info
->vlan
= RX_STATE_NORMAL
;
524 /* enable event notification after query */
525 nc
->rxfilter_notify_enabled
= 1;
530 static void virtio_net_reset(VirtIODevice
*vdev
)
532 VirtIONet
*n
= VIRTIO_NET(vdev
);
535 /* Reset back to compatibility mode */
542 /* multiqueue is disabled by default */
544 timer_del(n
->announce_timer
.tm
);
545 n
->announce_timer
.round
= 0;
546 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
548 /* Flush any MAC and VLAN filter table state */
549 n
->mac_table
.in_use
= 0;
550 n
->mac_table
.first_multi
= 0;
551 n
->mac_table
.multi_overflow
= 0;
552 n
->mac_table
.uni_overflow
= 0;
553 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
554 memcpy(&n
->mac
[0], &n
->nic
->conf
->macaddr
, sizeof(n
->mac
));
555 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
556 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
558 /* Flush any async TX */
559 for (i
= 0; i
< n
->max_queues
; i
++) {
560 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
563 qemu_flush_or_purge_queued_packets(nc
->peer
, true);
564 assert(!virtio_net_get_subqueue(nc
)->async_tx
.elem
);
569 static void peer_test_vnet_hdr(VirtIONet
*n
)
571 NetClientState
*nc
= qemu_get_queue(n
->nic
);
576 n
->has_vnet_hdr
= qemu_has_vnet_hdr(nc
->peer
);
579 static int peer_has_vnet_hdr(VirtIONet
*n
)
581 return n
->has_vnet_hdr
;
584 static int peer_has_ufo(VirtIONet
*n
)
586 if (!peer_has_vnet_hdr(n
))
589 n
->has_ufo
= qemu_has_ufo(qemu_get_queue(n
->nic
)->peer
);
594 static void virtio_net_set_mrg_rx_bufs(VirtIONet
*n
, int mergeable_rx_bufs
,
595 int version_1
, int hash_report
)
600 n
->mergeable_rx_bufs
= mergeable_rx_bufs
;
603 n
->guest_hdr_len
= hash_report
?
604 sizeof(struct virtio_net_hdr_v1_hash
) :
605 sizeof(struct virtio_net_hdr_mrg_rxbuf
);
606 n
->rss_data
.populate_hash
= !!hash_report
;
608 n
->guest_hdr_len
= n
->mergeable_rx_bufs
?
609 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
610 sizeof(struct virtio_net_hdr
);
613 for (i
= 0; i
< n
->max_queues
; i
++) {
614 nc
= qemu_get_subqueue(n
->nic
, i
);
616 if (peer_has_vnet_hdr(n
) &&
617 qemu_has_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
)) {
618 qemu_set_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
);
619 n
->host_hdr_len
= n
->guest_hdr_len
;
624 static int virtio_net_max_tx_queue_size(VirtIONet
*n
)
626 NetClientState
*peer
= n
->nic_conf
.peers
.ncs
[0];
629 * Backends other than vhost-user don't support max queue size.
632 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
635 if (peer
->info
->type
!= NET_CLIENT_DRIVER_VHOST_USER
) {
636 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
639 return VIRTQUEUE_MAX_SIZE
;
642 static int peer_attach(VirtIONet
*n
, int index
)
644 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
650 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
651 vhost_set_vring_enable(nc
->peer
, 1);
654 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
658 if (n
->max_queues
== 1) {
662 return tap_enable(nc
->peer
);
665 static int peer_detach(VirtIONet
*n
, int index
)
667 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
673 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
674 vhost_set_vring_enable(nc
->peer
, 0);
677 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
681 return tap_disable(nc
->peer
);
684 static void virtio_net_set_queues(VirtIONet
*n
)
689 if (n
->nic
->peer_deleted
) {
693 for (i
= 0; i
< n
->max_queues
; i
++) {
694 if (i
< n
->curr_queues
) {
695 r
= peer_attach(n
, i
);
698 r
= peer_detach(n
, i
);
704 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
);
706 static uint64_t virtio_net_get_features(VirtIODevice
*vdev
, uint64_t features
,
709 VirtIONet
*n
= VIRTIO_NET(vdev
);
710 NetClientState
*nc
= qemu_get_queue(n
->nic
);
712 /* Firstly sync all virtio-net possible supported features */
713 features
|= n
->host_features
;
715 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
717 if (!peer_has_vnet_hdr(n
)) {
718 virtio_clear_feature(&features
, VIRTIO_NET_F_CSUM
);
719 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
720 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
721 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
723 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_CSUM
);
724 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO4
);
725 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO6
);
726 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ECN
);
728 virtio_clear_feature(&features
, VIRTIO_NET_F_HASH_REPORT
);
731 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
732 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_UFO
);
733 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_UFO
);
736 if (!get_vhost_net(nc
->peer
)) {
740 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
741 virtio_clear_feature(&features
, VIRTIO_NET_F_RSS
);
743 features
= vhost_net_get_features(get_vhost_net(nc
->peer
), features
);
744 vdev
->backend_features
= features
;
746 if (n
->mtu_bypass_backend
&&
747 (n
->host_features
& 1ULL << VIRTIO_NET_F_MTU
)) {
748 features
|= (1ULL << VIRTIO_NET_F_MTU
);
754 static uint64_t virtio_net_bad_features(VirtIODevice
*vdev
)
756 uint64_t features
= 0;
758 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
760 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
761 virtio_add_feature(&features
, VIRTIO_NET_F_CSUM
);
762 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
763 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
764 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
769 static void virtio_net_apply_guest_offloads(VirtIONet
*n
)
771 qemu_set_offload(qemu_get_queue(n
->nic
)->peer
,
772 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_CSUM
)),
773 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO4
)),
774 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO6
)),
775 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_ECN
)),
776 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_UFO
)));
779 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features
)
781 static const uint64_t guest_offloads_mask
=
782 (1ULL << VIRTIO_NET_F_GUEST_CSUM
) |
783 (1ULL << VIRTIO_NET_F_GUEST_TSO4
) |
784 (1ULL << VIRTIO_NET_F_GUEST_TSO6
) |
785 (1ULL << VIRTIO_NET_F_GUEST_ECN
) |
786 (1ULL << VIRTIO_NET_F_GUEST_UFO
);
788 return guest_offloads_mask
& features
;
791 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet
*n
)
793 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
794 return virtio_net_guest_offloads_by_features(vdev
->guest_features
);
803 * Set the id of the failover primary device
805 * @opaque: FailoverId to setup
806 * @opts: opts for device we are handling
807 * @errp: returns an error if this function fails
809 static int failover_set_primary(void *opaque
, QemuOpts
*opts
, Error
**errp
)
811 FailoverId
*fid
= opaque
;
812 const char *standby_id
= qemu_opt_get(opts
, "failover_pair_id");
814 if (g_strcmp0(standby_id
, fid
->n
->netclient_name
) == 0) {
815 fid
->id
= g_strdup(opts
->id
);
823 * Find the primary device id for this failover virtio-net
825 * @n: VirtIONet device
826 * @errp: returns an error if this function fails
828 static char *failover_find_primary_device_id(VirtIONet
*n
)
834 if (!qemu_opts_foreach(qemu_find_opts("device"),
835 failover_set_primary
, &fid
, &err
)) {
842 * Find the primary device for this failover virtio-net
844 * @n: VirtIONet device
845 * @errp: returns an error if this function fails
847 static DeviceState
*failover_find_primary_device(VirtIONet
*n
)
849 char *id
= failover_find_primary_device_id(n
);
855 return qdev_find_recursive(sysbus_get_default(), id
);
858 static void failover_add_primary(VirtIONet
*n
, Error
**errp
)
863 DeviceState
*dev
= failover_find_primary_device(n
);
869 id
= failover_find_primary_device_id(n
);
871 error_setg(errp
, "Primary device not found");
872 error_append_hint(errp
, "Virtio-net failover will not work. Make "
873 "sure primary device has parameter"
874 " failover_pair_id=%s\n", n
->netclient_name
);
877 opts
= qemu_opts_find(qemu_find_opts("device"), id
);
878 g_assert(opts
); /* cannot be NULL because id was found using opts list */
879 dev
= qdev_device_add(opts
, &err
);
883 object_unref(OBJECT(dev
));
885 error_propagate(errp
, err
);
888 static void virtio_net_set_features(VirtIODevice
*vdev
, uint64_t features
)
890 VirtIONet
*n
= VIRTIO_NET(vdev
);
894 if (n
->mtu_bypass_backend
&&
895 !virtio_has_feature(vdev
->backend_features
, VIRTIO_NET_F_MTU
)) {
896 features
&= ~(1ULL << VIRTIO_NET_F_MTU
);
899 virtio_net_set_multiqueue(n
,
900 virtio_has_feature(features
, VIRTIO_NET_F_RSS
) ||
901 virtio_has_feature(features
, VIRTIO_NET_F_MQ
));
903 virtio_net_set_mrg_rx_bufs(n
,
904 virtio_has_feature(features
,
905 VIRTIO_NET_F_MRG_RXBUF
),
906 virtio_has_feature(features
,
908 virtio_has_feature(features
,
909 VIRTIO_NET_F_HASH_REPORT
));
911 n
->rsc4_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
912 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO4
);
913 n
->rsc6_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
914 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO6
);
915 n
->rss_data
.redirect
= virtio_has_feature(features
, VIRTIO_NET_F_RSS
);
917 if (n
->has_vnet_hdr
) {
918 n
->curr_guest_offloads
=
919 virtio_net_guest_offloads_by_features(features
);
920 virtio_net_apply_guest_offloads(n
);
923 for (i
= 0; i
< n
->max_queues
; i
++) {
924 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
926 if (!get_vhost_net(nc
->peer
)) {
929 vhost_net_ack_features(get_vhost_net(nc
->peer
), features
);
932 if (virtio_has_feature(features
, VIRTIO_NET_F_CTRL_VLAN
)) {
933 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
935 memset(n
->vlans
, 0xff, MAX_VLAN
>> 3);
938 if (virtio_has_feature(features
, VIRTIO_NET_F_STANDBY
)) {
939 qapi_event_send_failover_negotiated(n
->netclient_name
);
940 qatomic_set(&n
->failover_primary_hidden
, false);
941 failover_add_primary(n
, &err
);
943 warn_report_err(err
);
948 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
949 struct iovec
*iov
, unsigned int iov_cnt
)
953 NetClientState
*nc
= qemu_get_queue(n
->nic
);
955 s
= iov_to_buf(iov
, iov_cnt
, 0, &on
, sizeof(on
));
956 if (s
!= sizeof(on
)) {
957 return VIRTIO_NET_ERR
;
960 if (cmd
== VIRTIO_NET_CTRL_RX_PROMISC
) {
962 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLMULTI
) {
964 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLUNI
) {
966 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOMULTI
) {
968 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOUNI
) {
970 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOBCAST
) {
973 return VIRTIO_NET_ERR
;
978 return VIRTIO_NET_OK
;
981 static int virtio_net_handle_offloads(VirtIONet
*n
, uint8_t cmd
,
982 struct iovec
*iov
, unsigned int iov_cnt
)
984 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
988 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
989 return VIRTIO_NET_ERR
;
992 s
= iov_to_buf(iov
, iov_cnt
, 0, &offloads
, sizeof(offloads
));
993 if (s
!= sizeof(offloads
)) {
994 return VIRTIO_NET_ERR
;
997 if (cmd
== VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
) {
998 uint64_t supported_offloads
;
1000 offloads
= virtio_ldq_p(vdev
, &offloads
);
1002 if (!n
->has_vnet_hdr
) {
1003 return VIRTIO_NET_ERR
;
1006 n
->rsc4_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1007 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO4
);
1008 n
->rsc6_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1009 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO6
);
1010 virtio_clear_feature(&offloads
, VIRTIO_NET_F_RSC_EXT
);
1012 supported_offloads
= virtio_net_supported_guest_offloads(n
);
1013 if (offloads
& ~supported_offloads
) {
1014 return VIRTIO_NET_ERR
;
1017 n
->curr_guest_offloads
= offloads
;
1018 virtio_net_apply_guest_offloads(n
);
1020 return VIRTIO_NET_OK
;
1022 return VIRTIO_NET_ERR
;
1026 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
1027 struct iovec
*iov
, unsigned int iov_cnt
)
1029 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1030 struct virtio_net_ctrl_mac mac_data
;
1032 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1034 if (cmd
== VIRTIO_NET_CTRL_MAC_ADDR_SET
) {
1035 if (iov_size(iov
, iov_cnt
) != sizeof(n
->mac
)) {
1036 return VIRTIO_NET_ERR
;
1038 s
= iov_to_buf(iov
, iov_cnt
, 0, &n
->mac
, sizeof(n
->mac
));
1039 assert(s
== sizeof(n
->mac
));
1040 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
1041 rxfilter_notify(nc
);
1043 return VIRTIO_NET_OK
;
1046 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
) {
1047 return VIRTIO_NET_ERR
;
1051 int first_multi
= 0;
1052 uint8_t uni_overflow
= 0;
1053 uint8_t multi_overflow
= 0;
1054 uint8_t *macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1056 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1057 sizeof(mac_data
.entries
));
1058 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1059 if (s
!= sizeof(mac_data
.entries
)) {
1062 iov_discard_front(&iov
, &iov_cnt
, s
);
1064 if (mac_data
.entries
* ETH_ALEN
> iov_size(iov
, iov_cnt
)) {
1068 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
1069 s
= iov_to_buf(iov
, iov_cnt
, 0, macs
,
1070 mac_data
.entries
* ETH_ALEN
);
1071 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1074 in_use
+= mac_data
.entries
;
1079 iov_discard_front(&iov
, &iov_cnt
, mac_data
.entries
* ETH_ALEN
);
1081 first_multi
= in_use
;
1083 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1084 sizeof(mac_data
.entries
));
1085 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1086 if (s
!= sizeof(mac_data
.entries
)) {
1090 iov_discard_front(&iov
, &iov_cnt
, s
);
1092 if (mac_data
.entries
* ETH_ALEN
!= iov_size(iov
, iov_cnt
)) {
1096 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
- in_use
) {
1097 s
= iov_to_buf(iov
, iov_cnt
, 0, &macs
[in_use
* ETH_ALEN
],
1098 mac_data
.entries
* ETH_ALEN
);
1099 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1102 in_use
+= mac_data
.entries
;
1107 n
->mac_table
.in_use
= in_use
;
1108 n
->mac_table
.first_multi
= first_multi
;
1109 n
->mac_table
.uni_overflow
= uni_overflow
;
1110 n
->mac_table
.multi_overflow
= multi_overflow
;
1111 memcpy(n
->mac_table
.macs
, macs
, MAC_TABLE_ENTRIES
* ETH_ALEN
);
1113 rxfilter_notify(nc
);
1115 return VIRTIO_NET_OK
;
1119 return VIRTIO_NET_ERR
;
1122 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
1123 struct iovec
*iov
, unsigned int iov_cnt
)
1125 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1128 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1130 s
= iov_to_buf(iov
, iov_cnt
, 0, &vid
, sizeof(vid
));
1131 vid
= virtio_lduw_p(vdev
, &vid
);
1132 if (s
!= sizeof(vid
)) {
1133 return VIRTIO_NET_ERR
;
1136 if (vid
>= MAX_VLAN
)
1137 return VIRTIO_NET_ERR
;
1139 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
1140 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
1141 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
1142 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
1144 return VIRTIO_NET_ERR
;
1146 rxfilter_notify(nc
);
1148 return VIRTIO_NET_OK
;
1151 static int virtio_net_handle_announce(VirtIONet
*n
, uint8_t cmd
,
1152 struct iovec
*iov
, unsigned int iov_cnt
)
1154 trace_virtio_net_handle_announce(n
->announce_timer
.round
);
1155 if (cmd
== VIRTIO_NET_CTRL_ANNOUNCE_ACK
&&
1156 n
->status
& VIRTIO_NET_S_ANNOUNCE
) {
1157 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
1158 if (n
->announce_timer
.round
) {
1159 qemu_announce_timer_step(&n
->announce_timer
);
1161 return VIRTIO_NET_OK
;
1163 return VIRTIO_NET_ERR
;
1167 static void virtio_net_detach_epbf_rss(VirtIONet
*n
);
1169 static void virtio_net_disable_rss(VirtIONet
*n
)
1171 if (n
->rss_data
.enabled
) {
1172 trace_virtio_net_rss_disable();
1174 n
->rss_data
.enabled
= false;
1176 virtio_net_detach_epbf_rss(n
);
1179 static bool virtio_net_attach_ebpf_to_backend(NICState
*nic
, int prog_fd
)
1181 NetClientState
*nc
= qemu_get_peer(qemu_get_queue(nic
), 0);
1182 if (nc
== NULL
|| nc
->info
->set_steering_ebpf
== NULL
) {
1186 return nc
->info
->set_steering_ebpf(nc
, prog_fd
);
1189 static void rss_data_to_rss_config(struct VirtioNetRssData
*data
,
1190 struct EBPFRSSConfig
*config
)
1192 config
->redirect
= data
->redirect
;
1193 config
->populate_hash
= data
->populate_hash
;
1194 config
->hash_types
= data
->hash_types
;
1195 config
->indirections_len
= data
->indirections_len
;
1196 config
->default_queue
= data
->default_queue
;
1199 static bool virtio_net_attach_epbf_rss(VirtIONet
*n
)
1201 struct EBPFRSSConfig config
= {};
1203 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
1207 rss_data_to_rss_config(&n
->rss_data
, &config
);
1209 if (!ebpf_rss_set_all(&n
->ebpf_rss
, &config
,
1210 n
->rss_data
.indirections_table
, n
->rss_data
.key
)) {
1214 if (!virtio_net_attach_ebpf_to_backend(n
->nic
, n
->ebpf_rss
.program_fd
)) {
1221 static void virtio_net_detach_epbf_rss(VirtIONet
*n
)
1223 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1226 static bool virtio_net_load_ebpf(VirtIONet
*n
)
1228 if (!virtio_net_attach_ebpf_to_backend(n
->nic
, -1)) {
1229 /* backend does't support steering ebpf */
1233 return ebpf_rss_load(&n
->ebpf_rss
);
1236 static void virtio_net_unload_ebpf(VirtIONet
*n
)
1238 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1239 ebpf_rss_unload(&n
->ebpf_rss
);
1242 static uint16_t virtio_net_handle_rss(VirtIONet
*n
,
1244 unsigned int iov_cnt
,
1247 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1248 struct virtio_net_rss_config cfg
;
1249 size_t s
, offset
= 0, size_get
;
1255 const char *err_msg
= "";
1256 uint32_t err_value
= 0;
1258 if (do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_RSS
)) {
1259 err_msg
= "RSS is not negotiated";
1262 if (!do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_HASH_REPORT
)) {
1263 err_msg
= "Hash report is not negotiated";
1266 size_get
= offsetof(struct virtio_net_rss_config
, indirection_table
);
1267 s
= iov_to_buf(iov
, iov_cnt
, offset
, &cfg
, size_get
);
1268 if (s
!= size_get
) {
1269 err_msg
= "Short command buffer";
1270 err_value
= (uint32_t)s
;
1273 n
->rss_data
.hash_types
= virtio_ldl_p(vdev
, &cfg
.hash_types
);
1274 n
->rss_data
.indirections_len
=
1275 virtio_lduw_p(vdev
, &cfg
.indirection_table_mask
);
1276 n
->rss_data
.indirections_len
++;
1278 n
->rss_data
.indirections_len
= 1;
1280 if (!is_power_of_2(n
->rss_data
.indirections_len
)) {
1281 err_msg
= "Invalid size of indirection table";
1282 err_value
= n
->rss_data
.indirections_len
;
1285 if (n
->rss_data
.indirections_len
> VIRTIO_NET_RSS_MAX_TABLE_LEN
) {
1286 err_msg
= "Too large indirection table";
1287 err_value
= n
->rss_data
.indirections_len
;
1290 n
->rss_data
.default_queue
= do_rss
?
1291 virtio_lduw_p(vdev
, &cfg
.unclassified_queue
) : 0;
1292 if (n
->rss_data
.default_queue
>= n
->max_queues
) {
1293 err_msg
= "Invalid default queue";
1294 err_value
= n
->rss_data
.default_queue
;
1298 size_get
= sizeof(uint16_t) * n
->rss_data
.indirections_len
;
1299 g_free(n
->rss_data
.indirections_table
);
1300 n
->rss_data
.indirections_table
= g_malloc(size_get
);
1301 if (!n
->rss_data
.indirections_table
) {
1302 err_msg
= "Can't allocate indirections table";
1303 err_value
= n
->rss_data
.indirections_len
;
1306 s
= iov_to_buf(iov
, iov_cnt
, offset
,
1307 n
->rss_data
.indirections_table
, size_get
);
1308 if (s
!= size_get
) {
1309 err_msg
= "Short indirection table buffer";
1310 err_value
= (uint32_t)s
;
1313 for (i
= 0; i
< n
->rss_data
.indirections_len
; ++i
) {
1314 uint16_t val
= n
->rss_data
.indirections_table
[i
];
1315 n
->rss_data
.indirections_table
[i
] = virtio_lduw_p(vdev
, &val
);
1318 size_get
= sizeof(temp
);
1319 s
= iov_to_buf(iov
, iov_cnt
, offset
, &temp
, size_get
);
1320 if (s
!= size_get
) {
1321 err_msg
= "Can't get queues";
1322 err_value
= (uint32_t)s
;
1325 queues
= do_rss
? virtio_lduw_p(vdev
, &temp
.us
) : n
->curr_queues
;
1326 if (queues
== 0 || queues
> n
->max_queues
) {
1327 err_msg
= "Invalid number of queues";
1331 if (temp
.b
> VIRTIO_NET_RSS_MAX_KEY_SIZE
) {
1332 err_msg
= "Invalid key size";
1336 if (!temp
.b
&& n
->rss_data
.hash_types
) {
1337 err_msg
= "No key provided";
1341 if (!temp
.b
&& !n
->rss_data
.hash_types
) {
1342 virtio_net_disable_rss(n
);
1347 s
= iov_to_buf(iov
, iov_cnt
, offset
, n
->rss_data
.key
, size_get
);
1348 if (s
!= size_get
) {
1349 err_msg
= "Can get key buffer";
1350 err_value
= (uint32_t)s
;
1353 n
->rss_data
.enabled
= true;
1355 if (!n
->rss_data
.populate_hash
) {
1356 if (!virtio_net_attach_epbf_rss(n
)) {
1357 /* EBPF must be loaded for vhost */
1358 if (get_vhost_net(qemu_get_queue(n
->nic
)->peer
)) {
1359 warn_report("Can't load eBPF RSS for vhost");
1362 /* fallback to software RSS */
1363 warn_report("Can't load eBPF RSS - fallback to software RSS");
1364 n
->rss_data
.enabled_software_rss
= true;
1367 /* use software RSS for hash populating */
1368 /* and detach eBPF if was loaded before */
1369 virtio_net_detach_epbf_rss(n
);
1370 n
->rss_data
.enabled_software_rss
= true;
1373 trace_virtio_net_rss_enable(n
->rss_data
.hash_types
,
1374 n
->rss_data
.indirections_len
,
1378 trace_virtio_net_rss_error(err_msg
, err_value
);
1379 virtio_net_disable_rss(n
);
1383 static int virtio_net_handle_mq(VirtIONet
*n
, uint8_t cmd
,
1384 struct iovec
*iov
, unsigned int iov_cnt
)
1386 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1389 virtio_net_disable_rss(n
);
1390 if (cmd
== VIRTIO_NET_CTRL_MQ_HASH_CONFIG
) {
1391 queues
= virtio_net_handle_rss(n
, iov
, iov_cnt
, false);
1392 return queues
? VIRTIO_NET_OK
: VIRTIO_NET_ERR
;
1394 if (cmd
== VIRTIO_NET_CTRL_MQ_RSS_CONFIG
) {
1395 queues
= virtio_net_handle_rss(n
, iov
, iov_cnt
, true);
1396 } else if (cmd
== VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
) {
1397 struct virtio_net_ctrl_mq mq
;
1399 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_MQ
)) {
1400 return VIRTIO_NET_ERR
;
1402 s
= iov_to_buf(iov
, iov_cnt
, 0, &mq
, sizeof(mq
));
1403 if (s
!= sizeof(mq
)) {
1404 return VIRTIO_NET_ERR
;
1406 queues
= virtio_lduw_p(vdev
, &mq
.virtqueue_pairs
);
1409 return VIRTIO_NET_ERR
;
1412 if (queues
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
1413 queues
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
1414 queues
> n
->max_queues
||
1416 return VIRTIO_NET_ERR
;
1419 n
->curr_queues
= queues
;
1420 /* stop the backend before changing the number of queues to avoid handling a
1422 virtio_net_set_status(vdev
, vdev
->status
);
1423 virtio_net_set_queues(n
);
1425 return VIRTIO_NET_OK
;
1428 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
1430 VirtIONet
*n
= VIRTIO_NET(vdev
);
1431 struct virtio_net_ctrl_hdr ctrl
;
1432 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1433 VirtQueueElement
*elem
;
1435 struct iovec
*iov
, *iov2
;
1436 unsigned int iov_cnt
;
1439 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
1443 if (iov_size(elem
->in_sg
, elem
->in_num
) < sizeof(status
) ||
1444 iov_size(elem
->out_sg
, elem
->out_num
) < sizeof(ctrl
)) {
1445 virtio_error(vdev
, "virtio-net ctrl missing headers");
1446 virtqueue_detach_element(vq
, elem
, 0);
1451 iov_cnt
= elem
->out_num
;
1452 iov2
= iov
= g_memdup(elem
->out_sg
, sizeof(struct iovec
) * elem
->out_num
);
1453 s
= iov_to_buf(iov
, iov_cnt
, 0, &ctrl
, sizeof(ctrl
));
1454 iov_discard_front(&iov
, &iov_cnt
, sizeof(ctrl
));
1455 if (s
!= sizeof(ctrl
)) {
1456 status
= VIRTIO_NET_ERR
;
1457 } else if (ctrl
.class == VIRTIO_NET_CTRL_RX
) {
1458 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, iov
, iov_cnt
);
1459 } else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
) {
1460 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, iov
, iov_cnt
);
1461 } else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
) {
1462 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, iov
, iov_cnt
);
1463 } else if (ctrl
.class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1464 status
= virtio_net_handle_announce(n
, ctrl
.cmd
, iov
, iov_cnt
);
1465 } else if (ctrl
.class == VIRTIO_NET_CTRL_MQ
) {
1466 status
= virtio_net_handle_mq(n
, ctrl
.cmd
, iov
, iov_cnt
);
1467 } else if (ctrl
.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS
) {
1468 status
= virtio_net_handle_offloads(n
, ctrl
.cmd
, iov
, iov_cnt
);
1471 s
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
, sizeof(status
));
1472 assert(s
== sizeof(status
));
1474 virtqueue_push(vq
, elem
, sizeof(status
));
1475 virtio_notify(vdev
, vq
);
1483 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
1485 VirtIONet
*n
= VIRTIO_NET(vdev
);
1486 int queue_index
= vq2q(virtio_get_queue_index(vq
));
1488 qemu_flush_queued_packets(qemu_get_subqueue(n
->nic
, queue_index
));
1491 static bool virtio_net_can_receive(NetClientState
*nc
)
1493 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1494 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1495 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1497 if (!vdev
->vm_running
) {
1501 if (nc
->queue_index
>= n
->curr_queues
) {
1505 if (!virtio_queue_ready(q
->rx_vq
) ||
1506 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1513 static int virtio_net_has_buffers(VirtIONetQueue
*q
, int bufsize
)
1515 VirtIONet
*n
= q
->n
;
1516 if (virtio_queue_empty(q
->rx_vq
) ||
1517 (n
->mergeable_rx_bufs
&&
1518 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
1519 virtio_queue_set_notification(q
->rx_vq
, 1);
1521 /* To avoid a race condition where the guest has made some buffers
1522 * available after the above check but before notification was
1523 * enabled, check for available buffers again.
1525 if (virtio_queue_empty(q
->rx_vq
) ||
1526 (n
->mergeable_rx_bufs
&&
1527 !virtqueue_avail_bytes(q
->rx_vq
, bufsize
, 0))) {
1532 virtio_queue_set_notification(q
->rx_vq
, 0);
1536 static void virtio_net_hdr_swap(VirtIODevice
*vdev
, struct virtio_net_hdr
*hdr
)
1538 virtio_tswap16s(vdev
, &hdr
->hdr_len
);
1539 virtio_tswap16s(vdev
, &hdr
->gso_size
);
1540 virtio_tswap16s(vdev
, &hdr
->csum_start
);
1541 virtio_tswap16s(vdev
, &hdr
->csum_offset
);
1544 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1545 * it never finds out that the packets don't have valid checksums. This
1546 * causes dhclient to get upset. Fedora's carried a patch for ages to
1547 * fix this with Xen but it hasn't appeared in an upstream release of
1550 * To avoid breaking existing guests, we catch udp packets and add
1551 * checksums. This is terrible but it's better than hacking the guest
1554 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1555 * we should provide a mechanism to disable it to avoid polluting the host
1558 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
1559 uint8_t *buf
, size_t size
)
1561 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
1562 (size
> 27 && size
< 1500) && /* normal sized MTU */
1563 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
1564 (buf
[23] == 17) && /* ip.protocol == UDP */
1565 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
1566 net_checksum_calculate(buf
, size
, CSUM_UDP
);
1567 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1571 static void receive_header(VirtIONet
*n
, const struct iovec
*iov
, int iov_cnt
,
1572 const void *buf
, size_t size
)
1574 if (n
->has_vnet_hdr
) {
1575 /* FIXME this cast is evil */
1576 void *wbuf
= (void *)buf
;
1577 work_around_broken_dhclient(wbuf
, wbuf
+ n
->host_hdr_len
,
1578 size
- n
->host_hdr_len
);
1580 if (n
->needs_vnet_hdr_swap
) {
1581 virtio_net_hdr_swap(VIRTIO_DEVICE(n
), wbuf
);
1583 iov_from_buf(iov
, iov_cnt
, 0, buf
, sizeof(struct virtio_net_hdr
));
1585 struct virtio_net_hdr hdr
= {
1587 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
1589 iov_from_buf(iov
, iov_cnt
, 0, &hdr
, sizeof hdr
);
1593 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
1595 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1596 static const uint8_t vlan
[] = {0x81, 0x00};
1597 uint8_t *ptr
= (uint8_t *)buf
;
1603 ptr
+= n
->host_hdr_len
;
1605 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
1606 int vid
= lduw_be_p(ptr
+ 14) & 0xfff;
1607 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
1611 if (ptr
[0] & 1) { // multicast
1612 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
1614 } else if (n
->nomulti
) {
1616 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
1620 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
1621 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1628 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
1630 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
1634 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
1635 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1644 static uint8_t virtio_net_get_hash_type(bool isip4
,
1651 if (istcp
&& (types
& VIRTIO_NET_RSS_HASH_TYPE_TCPv4
)) {
1652 return NetPktRssIpV4Tcp
;
1654 if (isudp
&& (types
& VIRTIO_NET_RSS_HASH_TYPE_UDPv4
)) {
1655 return NetPktRssIpV4Udp
;
1657 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IPv4
) {
1658 return NetPktRssIpV4
;
1661 uint32_t mask
= VIRTIO_NET_RSS_HASH_TYPE_TCP_EX
|
1662 VIRTIO_NET_RSS_HASH_TYPE_TCPv6
;
1664 if (istcp
&& (types
& mask
)) {
1665 return (types
& VIRTIO_NET_RSS_HASH_TYPE_TCP_EX
) ?
1666 NetPktRssIpV6TcpEx
: NetPktRssIpV6Tcp
;
1668 mask
= VIRTIO_NET_RSS_HASH_TYPE_UDP_EX
| VIRTIO_NET_RSS_HASH_TYPE_UDPv6
;
1669 if (isudp
&& (types
& mask
)) {
1670 return (types
& VIRTIO_NET_RSS_HASH_TYPE_UDP_EX
) ?
1671 NetPktRssIpV6UdpEx
: NetPktRssIpV6Udp
;
1673 mask
= VIRTIO_NET_RSS_HASH_TYPE_IP_EX
| VIRTIO_NET_RSS_HASH_TYPE_IPv6
;
1675 return (types
& VIRTIO_NET_RSS_HASH_TYPE_IP_EX
) ?
1676 NetPktRssIpV6Ex
: NetPktRssIpV6
;
1682 static void virtio_set_packet_hash(const uint8_t *buf
, uint8_t report
,
1685 struct virtio_net_hdr_v1_hash
*hdr
= (void *)buf
;
1686 hdr
->hash_value
= hash
;
1687 hdr
->hash_report
= report
;
1690 static int virtio_net_process_rss(NetClientState
*nc
, const uint8_t *buf
,
1693 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1694 unsigned int index
= nc
->queue_index
, new_index
= index
;
1695 struct NetRxPkt
*pkt
= n
->rx_pkt
;
1696 uint8_t net_hash_type
;
1698 bool isip4
, isip6
, isudp
, istcp
;
1699 static const uint8_t reports
[NetPktRssIpV6UdpEx
+ 1] = {
1700 VIRTIO_NET_HASH_REPORT_IPv4
,
1701 VIRTIO_NET_HASH_REPORT_TCPv4
,
1702 VIRTIO_NET_HASH_REPORT_TCPv6
,
1703 VIRTIO_NET_HASH_REPORT_IPv6
,
1704 VIRTIO_NET_HASH_REPORT_IPv6_EX
,
1705 VIRTIO_NET_HASH_REPORT_TCPv6_EX
,
1706 VIRTIO_NET_HASH_REPORT_UDPv4
,
1707 VIRTIO_NET_HASH_REPORT_UDPv6
,
1708 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1711 net_rx_pkt_set_protocols(pkt
, buf
+ n
->host_hdr_len
,
1712 size
- n
->host_hdr_len
);
1713 net_rx_pkt_get_protocols(pkt
, &isip4
, &isip6
, &isudp
, &istcp
);
1714 if (isip4
&& (net_rx_pkt_get_ip4_info(pkt
)->fragment
)) {
1715 istcp
= isudp
= false;
1717 if (isip6
&& (net_rx_pkt_get_ip6_info(pkt
)->fragment
)) {
1718 istcp
= isudp
= false;
1720 net_hash_type
= virtio_net_get_hash_type(isip4
, isip6
, isudp
, istcp
,
1721 n
->rss_data
.hash_types
);
1722 if (net_hash_type
> NetPktRssIpV6UdpEx
) {
1723 if (n
->rss_data
.populate_hash
) {
1724 virtio_set_packet_hash(buf
, VIRTIO_NET_HASH_REPORT_NONE
, 0);
1726 return n
->rss_data
.redirect
? n
->rss_data
.default_queue
: -1;
1729 hash
= net_rx_pkt_calc_rss_hash(pkt
, net_hash_type
, n
->rss_data
.key
);
1731 if (n
->rss_data
.populate_hash
) {
1732 virtio_set_packet_hash(buf
, reports
[net_hash_type
], hash
);
1735 if (n
->rss_data
.redirect
) {
1736 new_index
= hash
& (n
->rss_data
.indirections_len
- 1);
1737 new_index
= n
->rss_data
.indirections_table
[new_index
];
1740 return (index
== new_index
) ? -1 : new_index
;
1743 static ssize_t
virtio_net_receive_rcu(NetClientState
*nc
, const uint8_t *buf
,
1744 size_t size
, bool no_rss
)
1746 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1747 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1748 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1749 VirtQueueElement
*elems
[VIRTQUEUE_MAX_SIZE
];
1750 size_t lens
[VIRTQUEUE_MAX_SIZE
];
1751 struct iovec mhdr_sg
[VIRTQUEUE_MAX_SIZE
];
1752 struct virtio_net_hdr_mrg_rxbuf mhdr
;
1753 unsigned mhdr_cnt
= 0;
1754 size_t offset
, i
, guest_offset
, j
;
1757 if (!virtio_net_can_receive(nc
)) {
1761 if (!no_rss
&& n
->rss_data
.enabled
&& n
->rss_data
.enabled_software_rss
) {
1762 int index
= virtio_net_process_rss(nc
, buf
, size
);
1764 NetClientState
*nc2
= qemu_get_subqueue(n
->nic
, index
);
1765 return virtio_net_receive_rcu(nc2
, buf
, size
, true);
1769 /* hdr_len refers to the header we supply to the guest */
1770 if (!virtio_net_has_buffers(q
, size
+ n
->guest_hdr_len
- n
->host_hdr_len
)) {
1774 if (!receive_filter(n
, buf
, size
))
1779 while (offset
< size
) {
1780 VirtQueueElement
*elem
;
1782 const struct iovec
*sg
;
1786 if (i
== VIRTQUEUE_MAX_SIZE
) {
1787 virtio_error(vdev
, "virtio-net unexpected long buffer chain");
1792 elem
= virtqueue_pop(q
->rx_vq
, sizeof(VirtQueueElement
));
1795 virtio_error(vdev
, "virtio-net unexpected empty queue: "
1796 "i %zd mergeable %d offset %zd, size %zd, "
1797 "guest hdr len %zd, host hdr len %zd "
1798 "guest features 0x%" PRIx64
,
1799 i
, n
->mergeable_rx_bufs
, offset
, size
,
1800 n
->guest_hdr_len
, n
->host_hdr_len
,
1801 vdev
->guest_features
);
1807 if (elem
->in_num
< 1) {
1809 "virtio-net receive queue contains no in buffers");
1810 virtqueue_detach_element(q
->rx_vq
, elem
, 0);
1818 assert(offset
== 0);
1819 if (n
->mergeable_rx_bufs
) {
1820 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
1822 offsetof(typeof(mhdr
), num_buffers
),
1823 sizeof(mhdr
.num_buffers
));
1826 receive_header(n
, sg
, elem
->in_num
, buf
, size
);
1827 if (n
->rss_data
.populate_hash
) {
1828 offset
= sizeof(mhdr
);
1829 iov_from_buf(sg
, elem
->in_num
, offset
,
1830 buf
+ offset
, n
->host_hdr_len
- sizeof(mhdr
));
1832 offset
= n
->host_hdr_len
;
1833 total
+= n
->guest_hdr_len
;
1834 guest_offset
= n
->guest_hdr_len
;
1839 /* copy in packet. ugh */
1840 len
= iov_from_buf(sg
, elem
->in_num
, guest_offset
,
1841 buf
+ offset
, size
- offset
);
1844 /* If buffers can't be merged, at this point we
1845 * must have consumed the complete packet.
1846 * Otherwise, drop it. */
1847 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
1848 virtqueue_unpop(q
->rx_vq
, elem
, total
);
1860 virtio_stw_p(vdev
, &mhdr
.num_buffers
, i
);
1861 iov_from_buf(mhdr_sg
, mhdr_cnt
,
1863 &mhdr
.num_buffers
, sizeof mhdr
.num_buffers
);
1866 for (j
= 0; j
< i
; j
++) {
1867 /* signal other side */
1868 virtqueue_fill(q
->rx_vq
, elems
[j
], lens
[j
], j
);
1872 virtqueue_flush(q
->rx_vq
, i
);
1873 virtio_notify(vdev
, q
->rx_vq
);
1878 for (j
= 0; j
< i
; j
++) {
1885 static ssize_t
virtio_net_do_receive(NetClientState
*nc
, const uint8_t *buf
,
1888 RCU_READ_LOCK_GUARD();
1890 return virtio_net_receive_rcu(nc
, buf
, size
, false);
1893 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain
*chain
,
1895 VirtioNetRscUnit
*unit
)
1898 struct ip_header
*ip
;
1900 ip
= (struct ip_header
*)(buf
+ chain
->n
->guest_hdr_len
1901 + sizeof(struct eth_header
));
1902 unit
->ip
= (void *)ip
;
1903 ip_hdrlen
= (ip
->ip_ver_len
& 0xF) << 2;
1904 unit
->ip_plen
= &ip
->ip_len
;
1905 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
) + ip_hdrlen
);
1906 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
1907 unit
->payload
= htons(*unit
->ip_plen
) - ip_hdrlen
- unit
->tcp_hdrlen
;
1910 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain
*chain
,
1912 VirtioNetRscUnit
*unit
)
1914 struct ip6_header
*ip6
;
1916 ip6
= (struct ip6_header
*)(buf
+ chain
->n
->guest_hdr_len
1917 + sizeof(struct eth_header
));
1919 unit
->ip_plen
= &(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
1920 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
)
1921 + sizeof(struct ip6_header
));
1922 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
1924 /* There is a difference between payload lenght in ipv4 and v6,
1925 ip header is excluded in ipv6 */
1926 unit
->payload
= htons(*unit
->ip_plen
) - unit
->tcp_hdrlen
;
1929 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain
*chain
,
1930 VirtioNetRscSeg
*seg
)
1933 struct virtio_net_hdr_v1
*h
;
1935 h
= (struct virtio_net_hdr_v1
*)seg
->buf
;
1937 h
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
1939 if (seg
->is_coalesced
) {
1940 h
->rsc
.segments
= seg
->packets
;
1941 h
->rsc
.dup_acks
= seg
->dup_ack
;
1942 h
->flags
= VIRTIO_NET_HDR_F_RSC_INFO
;
1943 if (chain
->proto
== ETH_P_IP
) {
1944 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
1946 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
1950 ret
= virtio_net_do_receive(seg
->nc
, seg
->buf
, seg
->size
);
1951 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
1958 static void virtio_net_rsc_purge(void *opq
)
1960 VirtioNetRscSeg
*seg
, *rn
;
1961 VirtioNetRscChain
*chain
= (VirtioNetRscChain
*)opq
;
1963 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn
) {
1964 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
1965 chain
->stat
.purge_failed
++;
1970 chain
->stat
.timer
++;
1971 if (!QTAILQ_EMPTY(&chain
->buffers
)) {
1972 timer_mod(chain
->drain_timer
,
1973 qemu_clock_get_ns(QEMU_CLOCK_HOST
) + chain
->n
->rsc_timeout
);
1977 static void virtio_net_rsc_cleanup(VirtIONet
*n
)
1979 VirtioNetRscChain
*chain
, *rn_chain
;
1980 VirtioNetRscSeg
*seg
, *rn_seg
;
1982 QTAILQ_FOREACH_SAFE(chain
, &n
->rsc_chains
, next
, rn_chain
) {
1983 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn_seg
) {
1984 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
1989 timer_free(chain
->drain_timer
);
1990 QTAILQ_REMOVE(&n
->rsc_chains
, chain
, next
);
1995 static void virtio_net_rsc_cache_buf(VirtioNetRscChain
*chain
,
1997 const uint8_t *buf
, size_t size
)
2000 VirtioNetRscSeg
*seg
;
2002 hdr_len
= chain
->n
->guest_hdr_len
;
2003 seg
= g_malloc(sizeof(VirtioNetRscSeg
));
2004 seg
->buf
= g_malloc(hdr_len
+ sizeof(struct eth_header
)
2005 + sizeof(struct ip6_header
) + VIRTIO_NET_MAX_TCP_PAYLOAD
);
2006 memcpy(seg
->buf
, buf
, size
);
2010 seg
->is_coalesced
= 0;
2013 QTAILQ_INSERT_TAIL(&chain
->buffers
, seg
, next
);
2014 chain
->stat
.cache
++;
2016 switch (chain
->proto
) {
2018 virtio_net_rsc_extract_unit4(chain
, seg
->buf
, &seg
->unit
);
2021 virtio_net_rsc_extract_unit6(chain
, seg
->buf
, &seg
->unit
);
2024 g_assert_not_reached();
2028 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain
*chain
,
2029 VirtioNetRscSeg
*seg
,
2031 struct tcp_header
*n_tcp
,
2032 struct tcp_header
*o_tcp
)
2034 uint32_t nack
, oack
;
2035 uint16_t nwin
, owin
;
2037 nack
= htonl(n_tcp
->th_ack
);
2038 nwin
= htons(n_tcp
->th_win
);
2039 oack
= htonl(o_tcp
->th_ack
);
2040 owin
= htons(o_tcp
->th_win
);
2042 if ((nack
- oack
) >= VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2043 chain
->stat
.ack_out_of_win
++;
2045 } else if (nack
== oack
) {
2046 /* duplicated ack or window probe */
2048 /* duplicated ack, add dup ack count due to whql test up to 1 */
2049 chain
->stat
.dup_ack
++;
2052 /* Coalesce window update */
2053 o_tcp
->th_win
= n_tcp
->th_win
;
2054 chain
->stat
.win_update
++;
2055 return RSC_COALESCE
;
2058 /* pure ack, go to 'C', finalize*/
2059 chain
->stat
.pure_ack
++;
2064 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain
*chain
,
2065 VirtioNetRscSeg
*seg
,
2067 VirtioNetRscUnit
*n_unit
)
2071 uint32_t nseq
, oseq
;
2072 VirtioNetRscUnit
*o_unit
;
2074 o_unit
= &seg
->unit
;
2075 o_ip_len
= htons(*o_unit
->ip_plen
);
2076 nseq
= htonl(n_unit
->tcp
->th_seq
);
2077 oseq
= htonl(o_unit
->tcp
->th_seq
);
2079 /* out of order or retransmitted. */
2080 if ((nseq
- oseq
) > VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2081 chain
->stat
.data_out_of_win
++;
2085 data
= ((uint8_t *)n_unit
->tcp
) + n_unit
->tcp_hdrlen
;
2087 if ((o_unit
->payload
== 0) && n_unit
->payload
) {
2088 /* From no payload to payload, normal case, not a dup ack or etc */
2089 chain
->stat
.data_after_pure_ack
++;
2092 return virtio_net_rsc_handle_ack(chain
, seg
, buf
,
2093 n_unit
->tcp
, o_unit
->tcp
);
2095 } else if ((nseq
- oseq
) != o_unit
->payload
) {
2096 /* Not a consistent packet, out of order */
2097 chain
->stat
.data_out_of_order
++;
2101 if ((o_ip_len
+ n_unit
->payload
) > chain
->max_payload
) {
2102 chain
->stat
.over_size
++;
2106 /* Here comes the right data, the payload length in v4/v6 is different,
2107 so use the field value to update and record the new data len */
2108 o_unit
->payload
+= n_unit
->payload
; /* update new data len */
2110 /* update field in ip header */
2111 *o_unit
->ip_plen
= htons(o_ip_len
+ n_unit
->payload
);
2113 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
2114 for windows guest, while this may change the behavior for linux
2115 guest (only if it uses RSC feature). */
2116 o_unit
->tcp
->th_offset_flags
= n_unit
->tcp
->th_offset_flags
;
2118 o_unit
->tcp
->th_ack
= n_unit
->tcp
->th_ack
;
2119 o_unit
->tcp
->th_win
= n_unit
->tcp
->th_win
;
2121 memmove(seg
->buf
+ seg
->size
, data
, n_unit
->payload
);
2122 seg
->size
+= n_unit
->payload
;
2124 chain
->stat
.coalesced
++;
2125 return RSC_COALESCE
;
2129 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain
*chain
,
2130 VirtioNetRscSeg
*seg
,
2131 const uint8_t *buf
, size_t size
,
2132 VirtioNetRscUnit
*unit
)
2134 struct ip_header
*ip1
, *ip2
;
2136 ip1
= (struct ip_header
*)(unit
->ip
);
2137 ip2
= (struct ip_header
*)(seg
->unit
.ip
);
2138 if ((ip1
->ip_src
^ ip2
->ip_src
) || (ip1
->ip_dst
^ ip2
->ip_dst
)
2139 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2140 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2141 chain
->stat
.no_match
++;
2142 return RSC_NO_MATCH
;
2145 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2148 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain
*chain
,
2149 VirtioNetRscSeg
*seg
,
2150 const uint8_t *buf
, size_t size
,
2151 VirtioNetRscUnit
*unit
)
2153 struct ip6_header
*ip1
, *ip2
;
2155 ip1
= (struct ip6_header
*)(unit
->ip
);
2156 ip2
= (struct ip6_header
*)(seg
->unit
.ip
);
2157 if (memcmp(&ip1
->ip6_src
, &ip2
->ip6_src
, sizeof(struct in6_address
))
2158 || memcmp(&ip1
->ip6_dst
, &ip2
->ip6_dst
, sizeof(struct in6_address
))
2159 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2160 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2161 chain
->stat
.no_match
++;
2162 return RSC_NO_MATCH
;
2165 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2168 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2169 * to prevent out of order */
2170 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain
*chain
,
2171 struct tcp_header
*tcp
)
2176 tcp_flag
= htons(tcp
->th_offset_flags
);
2177 tcp_hdr
= (tcp_flag
& VIRTIO_NET_TCP_HDR_LENGTH
) >> 10;
2178 tcp_flag
&= VIRTIO_NET_TCP_FLAG
;
2179 if (tcp_flag
& TH_SYN
) {
2180 chain
->stat
.tcp_syn
++;
2184 if (tcp_flag
& (TH_FIN
| TH_URG
| TH_RST
| TH_ECE
| TH_CWR
)) {
2185 chain
->stat
.tcp_ctrl_drain
++;
2189 if (tcp_hdr
> sizeof(struct tcp_header
)) {
2190 chain
->stat
.tcp_all_opt
++;
2194 return RSC_CANDIDATE
;
2197 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain
*chain
,
2199 const uint8_t *buf
, size_t size
,
2200 VirtioNetRscUnit
*unit
)
2203 VirtioNetRscSeg
*seg
, *nseg
;
2205 if (QTAILQ_EMPTY(&chain
->buffers
)) {
2206 chain
->stat
.empty_cache
++;
2207 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2208 timer_mod(chain
->drain_timer
,
2209 qemu_clock_get_ns(QEMU_CLOCK_HOST
) + chain
->n
->rsc_timeout
);
2213 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2214 if (chain
->proto
== ETH_P_IP
) {
2215 ret
= virtio_net_rsc_coalesce4(chain
, seg
, buf
, size
, unit
);
2217 ret
= virtio_net_rsc_coalesce6(chain
, seg
, buf
, size
, unit
);
2220 if (ret
== RSC_FINAL
) {
2221 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2223 chain
->stat
.final_failed
++;
2227 /* Send current packet */
2228 return virtio_net_do_receive(nc
, buf
, size
);
2229 } else if (ret
== RSC_NO_MATCH
) {
2232 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2233 seg
->is_coalesced
= 1;
2238 chain
->stat
.no_match_cache
++;
2239 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2243 /* Drain a connection data, this is to avoid out of order segments */
2244 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain
*chain
,
2246 const uint8_t *buf
, size_t size
,
2247 uint16_t ip_start
, uint16_t ip_size
,
2250 VirtioNetRscSeg
*seg
, *nseg
;
2251 uint32_t ppair1
, ppair2
;
2253 ppair1
= *(uint32_t *)(buf
+ tcp_port
);
2254 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2255 ppair2
= *(uint32_t *)(seg
->buf
+ tcp_port
);
2256 if (memcmp(buf
+ ip_start
, seg
->buf
+ ip_start
, ip_size
)
2257 || (ppair1
!= ppair2
)) {
2260 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2261 chain
->stat
.drain_failed
++;
2267 return virtio_net_do_receive(nc
, buf
, size
);
2270 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain
*chain
,
2271 struct ip_header
*ip
,
2272 const uint8_t *buf
, size_t size
)
2276 /* Not an ipv4 packet */
2277 if (((ip
->ip_ver_len
& 0xF0) >> 4) != IP_HEADER_VERSION_4
) {
2278 chain
->stat
.ip_option
++;
2282 /* Don't handle packets with ip option */
2283 if ((ip
->ip_ver_len
& 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH
) {
2284 chain
->stat
.ip_option
++;
2288 if (ip
->ip_p
!= IPPROTO_TCP
) {
2289 chain
->stat
.bypass_not_tcp
++;
2293 /* Don't handle packets with ip fragment */
2294 if (!(htons(ip
->ip_off
) & IP_DF
)) {
2295 chain
->stat
.ip_frag
++;
2299 /* Don't handle packets with ecn flag */
2300 if (IPTOS_ECN(ip
->ip_tos
)) {
2301 chain
->stat
.ip_ecn
++;
2305 ip_len
= htons(ip
->ip_len
);
2306 if (ip_len
< (sizeof(struct ip_header
) + sizeof(struct tcp_header
))
2307 || ip_len
> (size
- chain
->n
->guest_hdr_len
-
2308 sizeof(struct eth_header
))) {
2309 chain
->stat
.ip_hacked
++;
2313 return RSC_CANDIDATE
;
2316 static size_t virtio_net_rsc_receive4(VirtioNetRscChain
*chain
,
2318 const uint8_t *buf
, size_t size
)
2322 VirtioNetRscUnit unit
;
2324 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2326 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
)
2327 + sizeof(struct tcp_header
))) {
2328 chain
->stat
.bypass_not_tcp
++;
2329 return virtio_net_do_receive(nc
, buf
, size
);
2332 virtio_net_rsc_extract_unit4(chain
, buf
, &unit
);
2333 if (virtio_net_rsc_sanity_check4(chain
, unit
.ip
, buf
, size
)
2335 return virtio_net_do_receive(nc
, buf
, size
);
2338 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2339 if (ret
== RSC_BYPASS
) {
2340 return virtio_net_do_receive(nc
, buf
, size
);
2341 } else if (ret
== RSC_FINAL
) {
2342 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2343 ((hdr_len
+ sizeof(struct eth_header
)) + 12),
2344 VIRTIO_NET_IP4_ADDR_SIZE
,
2345 hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
));
2348 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2351 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain
*chain
,
2352 struct ip6_header
*ip6
,
2353 const uint8_t *buf
, size_t size
)
2357 if (((ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_flow
& 0xF0) >> 4)
2358 != IP_HEADER_VERSION_6
) {
2362 /* Both option and protocol is checked in this */
2363 if (ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_nxt
!= IPPROTO_TCP
) {
2364 chain
->stat
.bypass_not_tcp
++;
2368 ip_len
= htons(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
2369 if (ip_len
< sizeof(struct tcp_header
) ||
2370 ip_len
> (size
- chain
->n
->guest_hdr_len
- sizeof(struct eth_header
)
2371 - sizeof(struct ip6_header
))) {
2372 chain
->stat
.ip_hacked
++;
2376 /* Don't handle packets with ecn flag */
2377 if (IP6_ECN(ip6
->ip6_ctlun
.ip6_un3
.ip6_un3_ecn
)) {
2378 chain
->stat
.ip_ecn
++;
2382 return RSC_CANDIDATE
;
2385 static size_t virtio_net_rsc_receive6(void *opq
, NetClientState
*nc
,
2386 const uint8_t *buf
, size_t size
)
2390 VirtioNetRscChain
*chain
;
2391 VirtioNetRscUnit unit
;
2393 chain
= (VirtioNetRscChain
*)opq
;
2394 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2396 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip6_header
)
2397 + sizeof(tcp_header
))) {
2398 return virtio_net_do_receive(nc
, buf
, size
);
2401 virtio_net_rsc_extract_unit6(chain
, buf
, &unit
);
2402 if (RSC_CANDIDATE
!= virtio_net_rsc_sanity_check6(chain
,
2403 unit
.ip
, buf
, size
)) {
2404 return virtio_net_do_receive(nc
, buf
, size
);
2407 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2408 if (ret
== RSC_BYPASS
) {
2409 return virtio_net_do_receive(nc
, buf
, size
);
2410 } else if (ret
== RSC_FINAL
) {
2411 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2412 ((hdr_len
+ sizeof(struct eth_header
)) + 8),
2413 VIRTIO_NET_IP6_ADDR_SIZE
,
2414 hdr_len
+ sizeof(struct eth_header
)
2415 + sizeof(struct ip6_header
));
2418 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2421 static VirtioNetRscChain
*virtio_net_rsc_lookup_chain(VirtIONet
*n
,
2425 VirtioNetRscChain
*chain
;
2427 if ((proto
!= (uint16_t)ETH_P_IP
) && (proto
!= (uint16_t)ETH_P_IPV6
)) {
2431 QTAILQ_FOREACH(chain
, &n
->rsc_chains
, next
) {
2432 if (chain
->proto
== proto
) {
2437 chain
= g_malloc(sizeof(*chain
));
2439 chain
->proto
= proto
;
2440 if (proto
== (uint16_t)ETH_P_IP
) {
2441 chain
->max_payload
= VIRTIO_NET_MAX_IP4_PAYLOAD
;
2442 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2444 chain
->max_payload
= VIRTIO_NET_MAX_IP6_PAYLOAD
;
2445 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2447 chain
->drain_timer
= timer_new_ns(QEMU_CLOCK_HOST
,
2448 virtio_net_rsc_purge
, chain
);
2449 memset(&chain
->stat
, 0, sizeof(chain
->stat
));
2451 QTAILQ_INIT(&chain
->buffers
);
2452 QTAILQ_INSERT_TAIL(&n
->rsc_chains
, chain
, next
);
2457 static ssize_t
virtio_net_rsc_receive(NetClientState
*nc
,
2462 VirtioNetRscChain
*chain
;
2463 struct eth_header
*eth
;
2466 n
= qemu_get_nic_opaque(nc
);
2467 if (size
< (n
->host_hdr_len
+ sizeof(struct eth_header
))) {
2468 return virtio_net_do_receive(nc
, buf
, size
);
2471 eth
= (struct eth_header
*)(buf
+ n
->guest_hdr_len
);
2472 proto
= htons(eth
->h_proto
);
2474 chain
= virtio_net_rsc_lookup_chain(n
, nc
, proto
);
2476 chain
->stat
.received
++;
2477 if (proto
== (uint16_t)ETH_P_IP
&& n
->rsc4_enabled
) {
2478 return virtio_net_rsc_receive4(chain
, nc
, buf
, size
);
2479 } else if (proto
== (uint16_t)ETH_P_IPV6
&& n
->rsc6_enabled
) {
2480 return virtio_net_rsc_receive6(chain
, nc
, buf
, size
);
2483 return virtio_net_do_receive(nc
, buf
, size
);
2486 static ssize_t
virtio_net_receive(NetClientState
*nc
, const uint8_t *buf
,
2489 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2490 if ((n
->rsc4_enabled
|| n
->rsc6_enabled
)) {
2491 return virtio_net_rsc_receive(nc
, buf
, size
);
2493 return virtio_net_do_receive(nc
, buf
, size
);
2497 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
);
2499 static void virtio_net_tx_complete(NetClientState
*nc
, ssize_t len
)
2501 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2502 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
2503 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2505 virtqueue_push(q
->tx_vq
, q
->async_tx
.elem
, 0);
2506 virtio_notify(vdev
, q
->tx_vq
);
2508 g_free(q
->async_tx
.elem
);
2509 q
->async_tx
.elem
= NULL
;
2511 virtio_queue_set_notification(q
->tx_vq
, 1);
2512 virtio_net_flush_tx(q
);
2516 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
)
2518 VirtIONet
*n
= q
->n
;
2519 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2520 VirtQueueElement
*elem
;
2521 int32_t num_packets
= 0;
2522 int queue_index
= vq2q(virtio_get_queue_index(q
->tx_vq
));
2523 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2527 if (q
->async_tx
.elem
) {
2528 virtio_queue_set_notification(q
->tx_vq
, 0);
2534 unsigned int out_num
;
2535 struct iovec sg
[VIRTQUEUE_MAX_SIZE
], sg2
[VIRTQUEUE_MAX_SIZE
+ 1], *out_sg
;
2536 struct virtio_net_hdr_mrg_rxbuf mhdr
;
2538 elem
= virtqueue_pop(q
->tx_vq
, sizeof(VirtQueueElement
));
2543 out_num
= elem
->out_num
;
2544 out_sg
= elem
->out_sg
;
2546 virtio_error(vdev
, "virtio-net header not in first element");
2547 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
2552 if (n
->has_vnet_hdr
) {
2553 if (iov_to_buf(out_sg
, out_num
, 0, &mhdr
, n
->guest_hdr_len
) <
2555 virtio_error(vdev
, "virtio-net header incorrect");
2556 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
2560 if (n
->needs_vnet_hdr_swap
) {
2561 virtio_net_hdr_swap(vdev
, (void *) &mhdr
);
2562 sg2
[0].iov_base
= &mhdr
;
2563 sg2
[0].iov_len
= n
->guest_hdr_len
;
2564 out_num
= iov_copy(&sg2
[1], ARRAY_SIZE(sg2
) - 1,
2566 n
->guest_hdr_len
, -1);
2567 if (out_num
== VIRTQUEUE_MAX_SIZE
) {
2575 * If host wants to see the guest header as is, we can
2576 * pass it on unchanged. Otherwise, copy just the parts
2577 * that host is interested in.
2579 assert(n
->host_hdr_len
<= n
->guest_hdr_len
);
2580 if (n
->host_hdr_len
!= n
->guest_hdr_len
) {
2581 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
2583 0, n
->host_hdr_len
);
2584 sg_num
+= iov_copy(sg
+ sg_num
, ARRAY_SIZE(sg
) - sg_num
,
2586 n
->guest_hdr_len
, -1);
2591 ret
= qemu_sendv_packet_async(qemu_get_subqueue(n
->nic
, queue_index
),
2592 out_sg
, out_num
, virtio_net_tx_complete
);
2594 virtio_queue_set_notification(q
->tx_vq
, 0);
2595 q
->async_tx
.elem
= elem
;
2600 virtqueue_push(q
->tx_vq
, elem
, 0);
2601 virtio_notify(vdev
, q
->tx_vq
);
2604 if (++num_packets
>= n
->tx_burst
) {
2611 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
2613 VirtIONet
*n
= VIRTIO_NET(vdev
);
2614 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2616 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2617 virtio_net_drop_tx_queue_data(vdev
, vq
);
2621 /* This happens when device was stopped but VCPU wasn't. */
2622 if (!vdev
->vm_running
) {
2627 if (q
->tx_waiting
) {
2628 virtio_queue_set_notification(vq
, 1);
2629 timer_del(q
->tx_timer
);
2631 if (virtio_net_flush_tx(q
) == -EINVAL
) {
2635 timer_mod(q
->tx_timer
,
2636 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2638 virtio_queue_set_notification(vq
, 0);
2642 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
2644 VirtIONet
*n
= VIRTIO_NET(vdev
);
2645 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2647 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2648 virtio_net_drop_tx_queue_data(vdev
, vq
);
2652 if (unlikely(q
->tx_waiting
)) {
2656 /* This happens when device was stopped but VCPU wasn't. */
2657 if (!vdev
->vm_running
) {
2660 virtio_queue_set_notification(vq
, 0);
2661 qemu_bh_schedule(q
->tx_bh
);
2664 static void virtio_net_tx_timer(void *opaque
)
2666 VirtIONetQueue
*q
= opaque
;
2667 VirtIONet
*n
= q
->n
;
2668 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2669 /* This happens when device was stopped but BH wasn't. */
2670 if (!vdev
->vm_running
) {
2671 /* Make sure tx waiting is set, so we'll run when restarted. */
2672 assert(q
->tx_waiting
);
2678 /* Just in case the driver is not ready on more */
2679 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2683 virtio_queue_set_notification(q
->tx_vq
, 1);
2684 virtio_net_flush_tx(q
);
2687 static void virtio_net_tx_bh(void *opaque
)
2689 VirtIONetQueue
*q
= opaque
;
2690 VirtIONet
*n
= q
->n
;
2691 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2694 /* This happens when device was stopped but BH wasn't. */
2695 if (!vdev
->vm_running
) {
2696 /* Make sure tx waiting is set, so we'll run when restarted. */
2697 assert(q
->tx_waiting
);
2703 /* Just in case the driver is not ready on more */
2704 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
2708 ret
= virtio_net_flush_tx(q
);
2709 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2710 return; /* Notification re-enable handled by tx_complete or device
2714 /* If we flush a full burst of packets, assume there are
2715 * more coming and immediately reschedule */
2716 if (ret
>= n
->tx_burst
) {
2717 qemu_bh_schedule(q
->tx_bh
);
2722 /* If less than a full burst, re-enable notification and flush
2723 * anything that may have come in while we weren't looking. If
2724 * we find something, assume the guest is still active and reschedule */
2725 virtio_queue_set_notification(q
->tx_vq
, 1);
2726 ret
= virtio_net_flush_tx(q
);
2727 if (ret
== -EINVAL
) {
2729 } else if (ret
> 0) {
2730 virtio_queue_set_notification(q
->tx_vq
, 0);
2731 qemu_bh_schedule(q
->tx_bh
);
2736 static void virtio_net_add_queue(VirtIONet
*n
, int index
)
2738 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2740 n
->vqs
[index
].rx_vq
= virtio_add_queue(vdev
, n
->net_conf
.rx_queue_size
,
2741 virtio_net_handle_rx
);
2743 if (n
->net_conf
.tx
&& !strcmp(n
->net_conf
.tx
, "timer")) {
2744 n
->vqs
[index
].tx_vq
=
2745 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2746 virtio_net_handle_tx_timer
);
2747 n
->vqs
[index
].tx_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2748 virtio_net_tx_timer
,
2751 n
->vqs
[index
].tx_vq
=
2752 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2753 virtio_net_handle_tx_bh
);
2754 n
->vqs
[index
].tx_bh
= qemu_bh_new(virtio_net_tx_bh
, &n
->vqs
[index
]);
2757 n
->vqs
[index
].tx_waiting
= 0;
2758 n
->vqs
[index
].n
= n
;
2761 static void virtio_net_del_queue(VirtIONet
*n
, int index
)
2763 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2764 VirtIONetQueue
*q
= &n
->vqs
[index
];
2765 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
2767 qemu_purge_queued_packets(nc
);
2769 virtio_del_queue(vdev
, index
* 2);
2771 timer_free(q
->tx_timer
);
2774 qemu_bh_delete(q
->tx_bh
);
2778 virtio_del_queue(vdev
, index
* 2 + 1);
2781 static void virtio_net_change_num_queues(VirtIONet
*n
, int new_max_queues
)
2783 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2784 int old_num_queues
= virtio_get_num_queues(vdev
);
2785 int new_num_queues
= new_max_queues
* 2 + 1;
2788 assert(old_num_queues
>= 3);
2789 assert(old_num_queues
% 2 == 1);
2791 if (old_num_queues
== new_num_queues
) {
2796 * We always need to remove and add ctrl vq if
2797 * old_num_queues != new_num_queues. Remove ctrl_vq first,
2798 * and then we only enter one of the following two loops.
2800 virtio_del_queue(vdev
, old_num_queues
- 1);
2802 for (i
= new_num_queues
- 1; i
< old_num_queues
- 1; i
+= 2) {
2803 /* new_num_queues < old_num_queues */
2804 virtio_net_del_queue(n
, i
/ 2);
2807 for (i
= old_num_queues
- 1; i
< new_num_queues
- 1; i
+= 2) {
2808 /* new_num_queues > old_num_queues */
2809 virtio_net_add_queue(n
, i
/ 2);
2812 /* add ctrl_vq last */
2813 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
2816 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
)
2818 int max
= multiqueue
? n
->max_queues
: 1;
2820 n
->multiqueue
= multiqueue
;
2821 virtio_net_change_num_queues(n
, max
);
2823 virtio_net_set_queues(n
);
2826 static int virtio_net_post_load_device(void *opaque
, int version_id
)
2828 VirtIONet
*n
= opaque
;
2829 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2832 trace_virtio_net_post_load_device();
2833 virtio_net_set_mrg_rx_bufs(n
, n
->mergeable_rx_bufs
,
2834 virtio_vdev_has_feature(vdev
,
2835 VIRTIO_F_VERSION_1
),
2836 virtio_vdev_has_feature(vdev
,
2837 VIRTIO_NET_F_HASH_REPORT
));
2839 /* MAC_TABLE_ENTRIES may be different from the saved image */
2840 if (n
->mac_table
.in_use
> MAC_TABLE_ENTRIES
) {
2841 n
->mac_table
.in_use
= 0;
2844 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
2845 n
->curr_guest_offloads
= virtio_net_supported_guest_offloads(n
);
2849 * curr_guest_offloads will be later overwritten by the
2850 * virtio_set_features_nocheck call done from the virtio_load.
2851 * Here we make sure it is preserved and restored accordingly
2852 * in the virtio_net_post_load_virtio callback.
2854 n
->saved_guest_offloads
= n
->curr_guest_offloads
;
2856 virtio_net_set_queues(n
);
2858 /* Find the first multicast entry in the saved MAC filter */
2859 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
2860 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
2864 n
->mac_table
.first_multi
= i
;
2866 /* nc.link_down can't be migrated, so infer link_down according
2867 * to link status bit in n->status */
2868 link_down
= (n
->status
& VIRTIO_NET_S_LINK_UP
) == 0;
2869 for (i
= 0; i
< n
->max_queues
; i
++) {
2870 qemu_get_subqueue(n
->nic
, i
)->link_down
= link_down
;
2873 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
2874 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
2875 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
2877 virtio_net_announce_timer
, n
);
2878 if (n
->announce_timer
.round
) {
2879 timer_mod(n
->announce_timer
.tm
,
2880 qemu_clock_get_ms(n
->announce_timer
.type
));
2882 qemu_announce_timer_del(&n
->announce_timer
, false);
2886 if (n
->rss_data
.enabled
) {
2887 n
->rss_data
.enabled_software_rss
= n
->rss_data
.populate_hash
;
2888 if (!n
->rss_data
.populate_hash
) {
2889 if (!virtio_net_attach_epbf_rss(n
)) {
2890 if (get_vhost_net(qemu_get_queue(n
->nic
)->peer
)) {
2891 warn_report("Can't post-load eBPF RSS for vhost");
2893 warn_report("Can't post-load eBPF RSS - "
2894 "fallback to software RSS");
2895 n
->rss_data
.enabled_software_rss
= true;
2900 trace_virtio_net_rss_enable(n
->rss_data
.hash_types
,
2901 n
->rss_data
.indirections_len
,
2902 sizeof(n
->rss_data
.key
));
2904 trace_virtio_net_rss_disable();
2909 static int virtio_net_post_load_virtio(VirtIODevice
*vdev
)
2911 VirtIONet
*n
= VIRTIO_NET(vdev
);
2913 * The actual needed state is now in saved_guest_offloads,
2914 * see virtio_net_post_load_device for detail.
2915 * Restore it back and apply the desired offloads.
2917 n
->curr_guest_offloads
= n
->saved_guest_offloads
;
2918 if (peer_has_vnet_hdr(n
)) {
2919 virtio_net_apply_guest_offloads(n
);
2925 /* tx_waiting field of a VirtIONetQueue */
2926 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting
= {
2927 .name
= "virtio-net-queue-tx_waiting",
2928 .fields
= (VMStateField
[]) {
2929 VMSTATE_UINT32(tx_waiting
, VirtIONetQueue
),
2930 VMSTATE_END_OF_LIST()
2934 static bool max_queues_gt_1(void *opaque
, int version_id
)
2936 return VIRTIO_NET(opaque
)->max_queues
> 1;
2939 static bool has_ctrl_guest_offloads(void *opaque
, int version_id
)
2941 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque
),
2942 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
);
2945 static bool mac_table_fits(void *opaque
, int version_id
)
2947 return VIRTIO_NET(opaque
)->mac_table
.in_use
<= MAC_TABLE_ENTRIES
;
2950 static bool mac_table_doesnt_fit(void *opaque
, int version_id
)
2952 return !mac_table_fits(opaque
, version_id
);
2955 /* This temporary type is shared by all the WITH_TMP methods
2956 * although only some fields are used by each.
2958 struct VirtIONetMigTmp
{
2960 VirtIONetQueue
*vqs_1
;
2961 uint16_t curr_queues_1
;
2963 uint32_t has_vnet_hdr
;
2966 /* The 2nd and subsequent tx_waiting flags are loaded later than
2967 * the 1st entry in the queues and only if there's more than one
2968 * entry. We use the tmp mechanism to calculate a temporary
2969 * pointer and count and also validate the count.
2972 static int virtio_net_tx_waiting_pre_save(void *opaque
)
2974 struct VirtIONetMigTmp
*tmp
= opaque
;
2976 tmp
->vqs_1
= tmp
->parent
->vqs
+ 1;
2977 tmp
->curr_queues_1
= tmp
->parent
->curr_queues
- 1;
2978 if (tmp
->parent
->curr_queues
== 0) {
2979 tmp
->curr_queues_1
= 0;
2985 static int virtio_net_tx_waiting_pre_load(void *opaque
)
2987 struct VirtIONetMigTmp
*tmp
= opaque
;
2989 /* Reuse the pointer setup from save */
2990 virtio_net_tx_waiting_pre_save(opaque
);
2992 if (tmp
->parent
->curr_queues
> tmp
->parent
->max_queues
) {
2993 error_report("virtio-net: curr_queues %x > max_queues %x",
2994 tmp
->parent
->curr_queues
, tmp
->parent
->max_queues
);
2999 return 0; /* all good */
3002 static const VMStateDescription vmstate_virtio_net_tx_waiting
= {
3003 .name
= "virtio-net-tx_waiting",
3004 .pre_load
= virtio_net_tx_waiting_pre_load
,
3005 .pre_save
= virtio_net_tx_waiting_pre_save
,
3006 .fields
= (VMStateField
[]) {
3007 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1
, struct VirtIONetMigTmp
,
3009 vmstate_virtio_net_queue_tx_waiting
,
3010 struct VirtIONetQueue
),
3011 VMSTATE_END_OF_LIST()
3015 /* the 'has_ufo' flag is just tested; if the incoming stream has the
3016 * flag set we need to check that we have it
3018 static int virtio_net_ufo_post_load(void *opaque
, int version_id
)
3020 struct VirtIONetMigTmp
*tmp
= opaque
;
3022 if (tmp
->has_ufo
&& !peer_has_ufo(tmp
->parent
)) {
3023 error_report("virtio-net: saved image requires TUN_F_UFO support");
3030 static int virtio_net_ufo_pre_save(void *opaque
)
3032 struct VirtIONetMigTmp
*tmp
= opaque
;
3034 tmp
->has_ufo
= tmp
->parent
->has_ufo
;
3039 static const VMStateDescription vmstate_virtio_net_has_ufo
= {
3040 .name
= "virtio-net-ufo",
3041 .post_load
= virtio_net_ufo_post_load
,
3042 .pre_save
= virtio_net_ufo_pre_save
,
3043 .fields
= (VMStateField
[]) {
3044 VMSTATE_UINT8(has_ufo
, struct VirtIONetMigTmp
),
3045 VMSTATE_END_OF_LIST()
3049 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3050 * flag set we need to check that we have it
3052 static int virtio_net_vnet_post_load(void *opaque
, int version_id
)
3054 struct VirtIONetMigTmp
*tmp
= opaque
;
3056 if (tmp
->has_vnet_hdr
&& !peer_has_vnet_hdr(tmp
->parent
)) {
3057 error_report("virtio-net: saved image requires vnet_hdr=on");
3064 static int virtio_net_vnet_pre_save(void *opaque
)
3066 struct VirtIONetMigTmp
*tmp
= opaque
;
3068 tmp
->has_vnet_hdr
= tmp
->parent
->has_vnet_hdr
;
3073 static const VMStateDescription vmstate_virtio_net_has_vnet
= {
3074 .name
= "virtio-net-vnet",
3075 .post_load
= virtio_net_vnet_post_load
,
3076 .pre_save
= virtio_net_vnet_pre_save
,
3077 .fields
= (VMStateField
[]) {
3078 VMSTATE_UINT32(has_vnet_hdr
, struct VirtIONetMigTmp
),
3079 VMSTATE_END_OF_LIST()
3083 static bool virtio_net_rss_needed(void *opaque
)
3085 return VIRTIO_NET(opaque
)->rss_data
.enabled
;
3088 static const VMStateDescription vmstate_virtio_net_rss
= {
3089 .name
= "virtio-net-device/rss",
3091 .minimum_version_id
= 1,
3092 .needed
= virtio_net_rss_needed
,
3093 .fields
= (VMStateField
[]) {
3094 VMSTATE_BOOL(rss_data
.enabled
, VirtIONet
),
3095 VMSTATE_BOOL(rss_data
.redirect
, VirtIONet
),
3096 VMSTATE_BOOL(rss_data
.populate_hash
, VirtIONet
),
3097 VMSTATE_UINT32(rss_data
.hash_types
, VirtIONet
),
3098 VMSTATE_UINT16(rss_data
.indirections_len
, VirtIONet
),
3099 VMSTATE_UINT16(rss_data
.default_queue
, VirtIONet
),
3100 VMSTATE_UINT8_ARRAY(rss_data
.key
, VirtIONet
,
3101 VIRTIO_NET_RSS_MAX_KEY_SIZE
),
3102 VMSTATE_VARRAY_UINT16_ALLOC(rss_data
.indirections_table
, VirtIONet
,
3103 rss_data
.indirections_len
, 0,
3104 vmstate_info_uint16
, uint16_t),
3105 VMSTATE_END_OF_LIST()
3109 static const VMStateDescription vmstate_virtio_net_device
= {
3110 .name
= "virtio-net-device",
3111 .version_id
= VIRTIO_NET_VM_VERSION
,
3112 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
3113 .post_load
= virtio_net_post_load_device
,
3114 .fields
= (VMStateField
[]) {
3115 VMSTATE_UINT8_ARRAY(mac
, VirtIONet
, ETH_ALEN
),
3116 VMSTATE_STRUCT_POINTER(vqs
, VirtIONet
,
3117 vmstate_virtio_net_queue_tx_waiting
,
3119 VMSTATE_UINT32(mergeable_rx_bufs
, VirtIONet
),
3120 VMSTATE_UINT16(status
, VirtIONet
),
3121 VMSTATE_UINT8(promisc
, VirtIONet
),
3122 VMSTATE_UINT8(allmulti
, VirtIONet
),
3123 VMSTATE_UINT32(mac_table
.in_use
, VirtIONet
),
3125 /* Guarded pair: If it fits we load it, else we throw it away
3126 * - can happen if source has a larger MAC table.; post-load
3127 * sets flags in this case.
3129 VMSTATE_VBUFFER_MULTIPLY(mac_table
.macs
, VirtIONet
,
3130 0, mac_table_fits
, mac_table
.in_use
,
3132 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet
, mac_table_doesnt_fit
, 0,
3133 mac_table
.in_use
, ETH_ALEN
),
3135 /* Note: This is an array of uint32's that's always been saved as a
3136 * buffer; hold onto your endiannesses; it's actually used as a bitmap
3137 * but based on the uint.
3139 VMSTATE_BUFFER_POINTER_UNSAFE(vlans
, VirtIONet
, 0, MAX_VLAN
>> 3),
3140 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3141 vmstate_virtio_net_has_vnet
),
3142 VMSTATE_UINT8(mac_table
.multi_overflow
, VirtIONet
),
3143 VMSTATE_UINT8(mac_table
.uni_overflow
, VirtIONet
),
3144 VMSTATE_UINT8(alluni
, VirtIONet
),
3145 VMSTATE_UINT8(nomulti
, VirtIONet
),
3146 VMSTATE_UINT8(nouni
, VirtIONet
),
3147 VMSTATE_UINT8(nobcast
, VirtIONet
),
3148 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3149 vmstate_virtio_net_has_ufo
),
3150 VMSTATE_SINGLE_TEST(max_queues
, VirtIONet
, max_queues_gt_1
, 0,
3151 vmstate_info_uint16_equal
, uint16_t),
3152 VMSTATE_UINT16_TEST(curr_queues
, VirtIONet
, max_queues_gt_1
),
3153 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3154 vmstate_virtio_net_tx_waiting
),
3155 VMSTATE_UINT64_TEST(curr_guest_offloads
, VirtIONet
,
3156 has_ctrl_guest_offloads
),
3157 VMSTATE_END_OF_LIST()
3159 .subsections
= (const VMStateDescription
* []) {
3160 &vmstate_virtio_net_rss
,
3165 static NetClientInfo net_virtio_info
= {
3166 .type
= NET_CLIENT_DRIVER_NIC
,
3167 .size
= sizeof(NICState
),
3168 .can_receive
= virtio_net_can_receive
,
3169 .receive
= virtio_net_receive
,
3170 .link_status_changed
= virtio_net_set_link_status
,
3171 .query_rx_filter
= virtio_net_query_rxfilter
,
3172 .announce
= virtio_net_announce
,
3175 static bool virtio_net_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
3177 VirtIONet
*n
= VIRTIO_NET(vdev
);
3178 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3179 assert(n
->vhost_started
);
3180 return vhost_net_virtqueue_pending(get_vhost_net(nc
->peer
), idx
);
3183 static void virtio_net_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
3186 VirtIONet
*n
= VIRTIO_NET(vdev
);
3187 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3188 assert(n
->vhost_started
);
3189 vhost_net_virtqueue_mask(get_vhost_net(nc
->peer
),
3193 static void virtio_net_set_config_size(VirtIONet
*n
, uint64_t host_features
)
3195 virtio_add_feature(&host_features
, VIRTIO_NET_F_MAC
);
3197 n
->config_size
= virtio_feature_get_config_size(feature_sizes
,
3201 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,
3205 * The name can be NULL, the netclient name will be type.x.
3207 assert(type
!= NULL
);
3209 g_free(n
->netclient_name
);
3210 g_free(n
->netclient_type
);
3211 n
->netclient_name
= g_strdup(name
);
3212 n
->netclient_type
= g_strdup(type
);
3215 static bool failover_unplug_primary(VirtIONet
*n
, DeviceState
*dev
)
3217 HotplugHandler
*hotplug_ctrl
;
3221 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3223 pci_dev
= PCI_DEVICE(dev
);
3224 pci_dev
->partially_hotplugged
= true;
3225 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &err
);
3227 error_report_err(err
);
3236 static bool failover_replug_primary(VirtIONet
*n
, DeviceState
*dev
,
3240 HotplugHandler
*hotplug_ctrl
;
3241 PCIDevice
*pdev
= PCI_DEVICE(dev
);
3242 BusState
*primary_bus
;
3244 if (!pdev
->partially_hotplugged
) {
3247 primary_bus
= dev
->parent_bus
;
3249 error_setg(errp
, "virtio_net: couldn't find primary bus");
3252 qdev_set_parent_bus(dev
, primary_bus
, &error_abort
);
3253 qatomic_set(&n
->failover_primary_hidden
, false);
3254 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3256 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &err
);
3260 hotplug_handler_plug(hotplug_ctrl
, dev
, &err
);
3262 pdev
->partially_hotplugged
= false;
3265 error_propagate(errp
, err
);
3269 static void virtio_net_handle_migration_primary(VirtIONet
*n
, MigrationState
*s
)
3271 bool should_be_hidden
;
3273 DeviceState
*dev
= failover_find_primary_device(n
);
3279 should_be_hidden
= qatomic_read(&n
->failover_primary_hidden
);
3281 if (migration_in_setup(s
) && !should_be_hidden
) {
3282 if (failover_unplug_primary(n
, dev
)) {
3283 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
3284 qapi_event_send_unplug_primary(dev
->id
);
3285 qatomic_set(&n
->failover_primary_hidden
, true);
3287 warn_report("couldn't unplug primary device");
3289 } else if (migration_has_failed(s
)) {
3290 /* We already unplugged the device let's plug it back */
3291 if (!failover_replug_primary(n
, dev
, &err
)) {
3293 error_report_err(err
);
3299 static void virtio_net_migration_state_notifier(Notifier
*notifier
, void *data
)
3301 MigrationState
*s
= data
;
3302 VirtIONet
*n
= container_of(notifier
, VirtIONet
, migration_state
);
3303 virtio_net_handle_migration_primary(n
, s
);
3306 static bool failover_hide_primary_device(DeviceListener
*listener
,
3307 QemuOpts
*device_opts
)
3309 VirtIONet
*n
= container_of(listener
, VirtIONet
, primary_listener
);
3310 const char *standby_id
;
3315 standby_id
= qemu_opt_get(device_opts
, "failover_pair_id");
3316 if (g_strcmp0(standby_id
, n
->netclient_name
) != 0) {
3320 /* failover_primary_hidden is set during feature negotiation */
3321 return qatomic_read(&n
->failover_primary_hidden
);
3324 static void virtio_net_device_realize(DeviceState
*dev
, Error
**errp
)
3326 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3327 VirtIONet
*n
= VIRTIO_NET(dev
);
3331 if (n
->net_conf
.mtu
) {
3332 n
->host_features
|= (1ULL << VIRTIO_NET_F_MTU
);
3335 if (n
->net_conf
.duplex_str
) {
3336 if (strncmp(n
->net_conf
.duplex_str
, "half", 5) == 0) {
3337 n
->net_conf
.duplex
= DUPLEX_HALF
;
3338 } else if (strncmp(n
->net_conf
.duplex_str
, "full", 5) == 0) {
3339 n
->net_conf
.duplex
= DUPLEX_FULL
;
3341 error_setg(errp
, "'duplex' must be 'half' or 'full'");
3344 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3346 n
->net_conf
.duplex
= DUPLEX_UNKNOWN
;
3349 if (n
->net_conf
.speed
< SPEED_UNKNOWN
) {
3350 error_setg(errp
, "'speed' must be between 0 and INT_MAX");
3353 if (n
->net_conf
.speed
>= 0) {
3354 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3358 n
->primary_listener
.hide_device
= failover_hide_primary_device
;
3359 qatomic_set(&n
->failover_primary_hidden
, true);
3360 device_listener_register(&n
->primary_listener
);
3361 n
->migration_state
.notify
= virtio_net_migration_state_notifier
;
3362 add_migration_state_change_notifier(&n
->migration_state
);
3363 n
->host_features
|= (1ULL << VIRTIO_NET_F_STANDBY
);
3366 virtio_net_set_config_size(n
, n
->host_features
);
3367 virtio_init(vdev
, "virtio-net", VIRTIO_ID_NET
, n
->config_size
);
3370 * We set a lower limit on RX queue size to what it always was.
3371 * Guests that want a smaller ring can always resize it without
3372 * help from us (using virtio 1 and up).
3374 if (n
->net_conf
.rx_queue_size
< VIRTIO_NET_RX_QUEUE_MIN_SIZE
||
3375 n
->net_conf
.rx_queue_size
> VIRTQUEUE_MAX_SIZE
||
3376 !is_power_of_2(n
->net_conf
.rx_queue_size
)) {
3377 error_setg(errp
, "Invalid rx_queue_size (= %" PRIu16
"), "
3378 "must be a power of 2 between %d and %d.",
3379 n
->net_conf
.rx_queue_size
, VIRTIO_NET_RX_QUEUE_MIN_SIZE
,
3380 VIRTQUEUE_MAX_SIZE
);
3381 virtio_cleanup(vdev
);
3385 if (n
->net_conf
.tx_queue_size
< VIRTIO_NET_TX_QUEUE_MIN_SIZE
||
3386 n
->net_conf
.tx_queue_size
> VIRTQUEUE_MAX_SIZE
||
3387 !is_power_of_2(n
->net_conf
.tx_queue_size
)) {
3388 error_setg(errp
, "Invalid tx_queue_size (= %" PRIu16
"), "
3389 "must be a power of 2 between %d and %d",
3390 n
->net_conf
.tx_queue_size
, VIRTIO_NET_TX_QUEUE_MIN_SIZE
,
3391 VIRTQUEUE_MAX_SIZE
);
3392 virtio_cleanup(vdev
);
3396 n
->max_queues
= MAX(n
->nic_conf
.peers
.queues
, 1);
3397 if (n
->max_queues
* 2 + 1 > VIRTIO_QUEUE_MAX
) {
3398 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
3399 "must be a positive integer less than %d.",
3400 n
->max_queues
, (VIRTIO_QUEUE_MAX
- 1) / 2);
3401 virtio_cleanup(vdev
);
3404 n
->vqs
= g_malloc0(sizeof(VirtIONetQueue
) * n
->max_queues
);
3406 n
->tx_timeout
= n
->net_conf
.txtimer
;
3408 if (n
->net_conf
.tx
&& strcmp(n
->net_conf
.tx
, "timer")
3409 && strcmp(n
->net_conf
.tx
, "bh")) {
3410 warn_report("virtio-net: "
3411 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3413 error_printf("Defaulting to \"bh\"");
3416 n
->net_conf
.tx_queue_size
= MIN(virtio_net_max_tx_queue_size(n
),
3417 n
->net_conf
.tx_queue_size
);
3419 for (i
= 0; i
< n
->max_queues
; i
++) {
3420 virtio_net_add_queue(n
, i
);
3423 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
3424 qemu_macaddr_default_if_unset(&n
->nic_conf
.macaddr
);
3425 memcpy(&n
->mac
[0], &n
->nic_conf
.macaddr
, sizeof(n
->mac
));
3426 n
->status
= VIRTIO_NET_S_LINK_UP
;
3427 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
3429 virtio_net_announce_timer
, n
);
3430 n
->announce_timer
.round
= 0;
3432 if (n
->netclient_type
) {
3434 * Happen when virtio_net_set_netclient_name has been called.
3436 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3437 n
->netclient_type
, n
->netclient_name
, n
);
3439 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3440 object_get_typename(OBJECT(dev
)), dev
->id
, n
);
3443 for (i
= 0; i
< n
->max_queues
; i
++) {
3444 n
->nic
->ncs
[i
].do_not_pad
= true;
3447 peer_test_vnet_hdr(n
);
3448 if (peer_has_vnet_hdr(n
)) {
3449 for (i
= 0; i
< n
->max_queues
; i
++) {
3450 qemu_using_vnet_hdr(qemu_get_subqueue(n
->nic
, i
)->peer
, true);
3452 n
->host_hdr_len
= sizeof(struct virtio_net_hdr
);
3454 n
->host_hdr_len
= 0;
3457 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->nic_conf
.macaddr
.a
);
3459 n
->vqs
[0].tx_waiting
= 0;
3460 n
->tx_burst
= n
->net_conf
.txburst
;
3461 virtio_net_set_mrg_rx_bufs(n
, 0, 0, 0);
3462 n
->promisc
= 1; /* for compatibility */
3464 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
3466 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
3468 nc
= qemu_get_queue(n
->nic
);
3469 nc
->rxfilter_notify_enabled
= 1;
3471 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
3472 struct virtio_net_config netcfg
= {};
3473 memcpy(&netcfg
.mac
, &n
->nic_conf
.macaddr
, ETH_ALEN
);
3474 vhost_net_set_config(get_vhost_net(nc
->peer
),
3475 (uint8_t *)&netcfg
, 0, ETH_ALEN
, VHOST_SET_CONFIG_TYPE_MASTER
);
3477 QTAILQ_INIT(&n
->rsc_chains
);
3480 net_rx_pkt_init(&n
->rx_pkt
, false);
3482 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3483 virtio_net_load_ebpf(n
);
3487 static void virtio_net_device_unrealize(DeviceState
*dev
)
3489 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3490 VirtIONet
*n
= VIRTIO_NET(dev
);
3493 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3494 virtio_net_unload_ebpf(n
);
3497 /* This will stop vhost backend if appropriate. */
3498 virtio_net_set_status(vdev
, 0);
3500 g_free(n
->netclient_name
);
3501 n
->netclient_name
= NULL
;
3502 g_free(n
->netclient_type
);
3503 n
->netclient_type
= NULL
;
3505 g_free(n
->mac_table
.macs
);
3509 device_listener_unregister(&n
->primary_listener
);
3510 remove_migration_state_change_notifier(&n
->migration_state
);
3513 max_queues
= n
->multiqueue
? n
->max_queues
: 1;
3514 for (i
= 0; i
< max_queues
; i
++) {
3515 virtio_net_del_queue(n
, i
);
3517 /* delete also control vq */
3518 virtio_del_queue(vdev
, max_queues
* 2);
3519 qemu_announce_timer_del(&n
->announce_timer
, false);
3521 qemu_del_nic(n
->nic
);
3522 virtio_net_rsc_cleanup(n
);
3523 g_free(n
->rss_data
.indirections_table
);
3524 net_rx_pkt_uninit(n
->rx_pkt
);
3525 virtio_cleanup(vdev
);
3528 static void virtio_net_instance_init(Object
*obj
)
3530 VirtIONet
*n
= VIRTIO_NET(obj
);
3533 * The default config_size is sizeof(struct virtio_net_config).
3534 * Can be overriden with virtio_net_set_config_size.
3536 n
->config_size
= sizeof(struct virtio_net_config
);
3537 device_add_bootindex_property(obj
, &n
->nic_conf
.bootindex
,
3538 "bootindex", "/ethernet-phy@0",
3541 ebpf_rss_init(&n
->ebpf_rss
);
3544 static int virtio_net_pre_save(void *opaque
)
3546 VirtIONet
*n
= opaque
;
3548 /* At this point, backend must be stopped, otherwise
3549 * it might keep writing to memory. */
3550 assert(!n
->vhost_started
);
3555 static bool primary_unplug_pending(void *opaque
)
3557 DeviceState
*dev
= opaque
;
3558 DeviceState
*primary
;
3559 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3560 VirtIONet
*n
= VIRTIO_NET(vdev
);
3562 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_STANDBY
)) {
3565 primary
= failover_find_primary_device(n
);
3566 return primary
? primary
->pending_deleted_event
: false;
3569 static bool dev_unplug_pending(void *opaque
)
3571 DeviceState
*dev
= opaque
;
3572 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3574 return vdc
->primary_unplug_pending(dev
);
3577 static const VMStateDescription vmstate_virtio_net
= {
3578 .name
= "virtio-net",
3579 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
3580 .version_id
= VIRTIO_NET_VM_VERSION
,
3581 .fields
= (VMStateField
[]) {
3582 VMSTATE_VIRTIO_DEVICE
,
3583 VMSTATE_END_OF_LIST()
3585 .pre_save
= virtio_net_pre_save
,
3586 .dev_unplug_pending
= dev_unplug_pending
,
3589 static Property virtio_net_properties
[] = {
3590 DEFINE_PROP_BIT64("csum", VirtIONet
, host_features
,
3591 VIRTIO_NET_F_CSUM
, true),
3592 DEFINE_PROP_BIT64("guest_csum", VirtIONet
, host_features
,
3593 VIRTIO_NET_F_GUEST_CSUM
, true),
3594 DEFINE_PROP_BIT64("gso", VirtIONet
, host_features
, VIRTIO_NET_F_GSO
, true),
3595 DEFINE_PROP_BIT64("guest_tso4", VirtIONet
, host_features
,
3596 VIRTIO_NET_F_GUEST_TSO4
, true),
3597 DEFINE_PROP_BIT64("guest_tso6", VirtIONet
, host_features
,
3598 VIRTIO_NET_F_GUEST_TSO6
, true),
3599 DEFINE_PROP_BIT64("guest_ecn", VirtIONet
, host_features
,
3600 VIRTIO_NET_F_GUEST_ECN
, true),
3601 DEFINE_PROP_BIT64("guest_ufo", VirtIONet
, host_features
,
3602 VIRTIO_NET_F_GUEST_UFO
, true),
3603 DEFINE_PROP_BIT64("guest_announce", VirtIONet
, host_features
,
3604 VIRTIO_NET_F_GUEST_ANNOUNCE
, true),
3605 DEFINE_PROP_BIT64("host_tso4", VirtIONet
, host_features
,
3606 VIRTIO_NET_F_HOST_TSO4
, true),
3607 DEFINE_PROP_BIT64("host_tso6", VirtIONet
, host_features
,
3608 VIRTIO_NET_F_HOST_TSO6
, true),
3609 DEFINE_PROP_BIT64("host_ecn", VirtIONet
, host_features
,
3610 VIRTIO_NET_F_HOST_ECN
, true),
3611 DEFINE_PROP_BIT64("host_ufo", VirtIONet
, host_features
,
3612 VIRTIO_NET_F_HOST_UFO
, true),
3613 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet
, host_features
,
3614 VIRTIO_NET_F_MRG_RXBUF
, true),
3615 DEFINE_PROP_BIT64("status", VirtIONet
, host_features
,
3616 VIRTIO_NET_F_STATUS
, true),
3617 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet
, host_features
,
3618 VIRTIO_NET_F_CTRL_VQ
, true),
3619 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet
, host_features
,
3620 VIRTIO_NET_F_CTRL_RX
, true),
3621 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet
, host_features
,
3622 VIRTIO_NET_F_CTRL_VLAN
, true),
3623 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet
, host_features
,
3624 VIRTIO_NET_F_CTRL_RX_EXTRA
, true),
3625 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet
, host_features
,
3626 VIRTIO_NET_F_CTRL_MAC_ADDR
, true),
3627 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet
, host_features
,
3628 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
, true),
3629 DEFINE_PROP_BIT64("mq", VirtIONet
, host_features
, VIRTIO_NET_F_MQ
, false),
3630 DEFINE_PROP_BIT64("rss", VirtIONet
, host_features
,
3631 VIRTIO_NET_F_RSS
, false),
3632 DEFINE_PROP_BIT64("hash", VirtIONet
, host_features
,
3633 VIRTIO_NET_F_HASH_REPORT
, false),
3634 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet
, host_features
,
3635 VIRTIO_NET_F_RSC_EXT
, false),
3636 DEFINE_PROP_UINT32("rsc_interval", VirtIONet
, rsc_timeout
,
3637 VIRTIO_NET_RSC_DEFAULT_INTERVAL
),
3638 DEFINE_NIC_PROPERTIES(VirtIONet
, nic_conf
),
3639 DEFINE_PROP_UINT32("x-txtimer", VirtIONet
, net_conf
.txtimer
,
3641 DEFINE_PROP_INT32("x-txburst", VirtIONet
, net_conf
.txburst
, TX_BURST
),
3642 DEFINE_PROP_STRING("tx", VirtIONet
, net_conf
.tx
),
3643 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet
, net_conf
.rx_queue_size
,
3644 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
),
3645 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet
, net_conf
.tx_queue_size
,
3646 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
),
3647 DEFINE_PROP_UINT16("host_mtu", VirtIONet
, net_conf
.mtu
, 0),
3648 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet
, mtu_bypass_backend
,
3650 DEFINE_PROP_INT32("speed", VirtIONet
, net_conf
.speed
, SPEED_UNKNOWN
),
3651 DEFINE_PROP_STRING("duplex", VirtIONet
, net_conf
.duplex_str
),
3652 DEFINE_PROP_BOOL("failover", VirtIONet
, failover
, false),
3653 DEFINE_PROP_END_OF_LIST(),
3656 static void virtio_net_class_init(ObjectClass
*klass
, void *data
)
3658 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3659 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
3661 device_class_set_props(dc
, virtio_net_properties
);
3662 dc
->vmsd
= &vmstate_virtio_net
;
3663 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
3664 vdc
->realize
= virtio_net_device_realize
;
3665 vdc
->unrealize
= virtio_net_device_unrealize
;
3666 vdc
->get_config
= virtio_net_get_config
;
3667 vdc
->set_config
= virtio_net_set_config
;
3668 vdc
->get_features
= virtio_net_get_features
;
3669 vdc
->set_features
= virtio_net_set_features
;
3670 vdc
->bad_features
= virtio_net_bad_features
;
3671 vdc
->reset
= virtio_net_reset
;
3672 vdc
->set_status
= virtio_net_set_status
;
3673 vdc
->guest_notifier_mask
= virtio_net_guest_notifier_mask
;
3674 vdc
->guest_notifier_pending
= virtio_net_guest_notifier_pending
;
3675 vdc
->legacy_features
|= (0x1 << VIRTIO_NET_F_GSO
);
3676 vdc
->post_load
= virtio_net_post_load_virtio
;
3677 vdc
->vmsd
= &vmstate_virtio_net_device
;
3678 vdc
->primary_unplug_pending
= primary_unplug_pending
;
3681 static const TypeInfo virtio_net_info
= {
3682 .name
= TYPE_VIRTIO_NET
,
3683 .parent
= TYPE_VIRTIO_DEVICE
,
3684 .instance_size
= sizeof(VirtIONet
),
3685 .instance_init
= virtio_net_instance_init
,
3686 .class_init
= virtio_net_class_init
,
3689 static void virtio_register_types(void)
3691 type_register_static(&virtio_net_info
);
3694 type_init(virtio_register_types
)