2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/atomic.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/module.h"
20 #include "hw/virtio/virtio.h"
22 #include "net/checksum.h"
24 #include "qemu/error-report.h"
25 #include "qemu/timer.h"
26 #include "qemu/option.h"
27 #include "qemu/option_int.h"
28 #include "qemu/config-file.h"
29 #include "qobject/qdict.h"
30 #include "hw/virtio/virtio-net.h"
31 #include "net/vhost_net.h"
32 #include "net/announce.h"
33 #include "hw/virtio/virtio-bus.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-events-net.h"
36 #include "hw/qdev-properties.h"
37 #include "qapi/qapi-types-migration.h"
38 #include "qapi/qapi-events-migration.h"
39 #include "hw/virtio/virtio-access.h"
40 #include "migration/misc.h"
41 #include "standard-headers/linux/ethtool.h"
42 #include "system/system.h"
43 #include "system/replay.h"
45 #include "monitor/qdev.h"
46 #include "monitor/monitor.h"
47 #include "hw/pci/pci_device.h"
48 #include "net_rx_pkt.h"
49 #include "hw/virtio/vhost.h"
50 #include "system/qtest.h"
52 #define VIRTIO_NET_VM_VERSION 11
54 /* previously fixed value */
55 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
56 #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
58 /* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */
59 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
60 #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
62 #define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */
64 #define VIRTIO_NET_TCP_FLAG 0x3F
65 #define VIRTIO_NET_TCP_HDR_LENGTH 0xF000
67 /* IPv4 max payload, 16 bits in the header */
68 #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
69 #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
71 /* header length value in ip header without option */
72 #define VIRTIO_NET_IP4_HEADER_LENGTH 5
74 #define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */
75 #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
77 /* Purge coalesced packets timer interval, This value affects the performance
78 a lot, and should be tuned carefully, '300000'(300us) is the recommended
79 value to pass the WHQL test, '50000' can gain 2x netperf throughput with
81 #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
83 #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
84 VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
85 VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
86 VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
87 VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
88 VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
89 VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
90 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
91 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
93 static const VirtIOFeature feature_sizes
[] = {
94 {.flags
= 1ULL << VIRTIO_NET_F_MAC
,
95 .end
= endof(struct virtio_net_config
, mac
)},
96 {.flags
= 1ULL << VIRTIO_NET_F_STATUS
,
97 .end
= endof(struct virtio_net_config
, status
)},
98 {.flags
= 1ULL << VIRTIO_NET_F_MQ
,
99 .end
= endof(struct virtio_net_config
, max_virtqueue_pairs
)},
100 {.flags
= 1ULL << VIRTIO_NET_F_MTU
,
101 .end
= endof(struct virtio_net_config
, mtu
)},
102 {.flags
= 1ULL << VIRTIO_NET_F_SPEED_DUPLEX
,
103 .end
= endof(struct virtio_net_config
, duplex
)},
104 {.flags
= (1ULL << VIRTIO_NET_F_RSS
) | (1ULL << VIRTIO_NET_F_HASH_REPORT
),
105 .end
= endof(struct virtio_net_config
, supported_hash_types
)},
109 static const VirtIOConfigSizeParams cfg_size_params
= {
110 .min_size
= endof(struct virtio_net_config
, mac
),
111 .max_size
= sizeof(struct virtio_net_config
),
112 .feature_sizes
= feature_sizes
115 static VirtIONetQueue
*virtio_net_get_subqueue(NetClientState
*nc
)
117 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
119 return &n
->vqs
[nc
->queue_index
];
122 static int vq2q(int queue_index
)
124 return queue_index
/ 2;
127 static void flush_or_purge_queued_packets(NetClientState
*nc
)
133 qemu_flush_or_purge_queued_packets(nc
->peer
, true);
134 assert(!virtio_net_get_subqueue(nc
)->async_tx
.elem
);
138 * - we could suppress RX interrupt if we were so inclined.
141 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
143 VirtIONet
*n
= VIRTIO_NET(vdev
);
144 struct virtio_net_config netcfg
;
145 NetClientState
*nc
= qemu_get_queue(n
->nic
);
146 static const MACAddr zero
= { .a
= { 0, 0, 0, 0, 0, 0 } };
149 memset(&netcfg
, 0 , sizeof(struct virtio_net_config
));
150 virtio_stw_p(vdev
, &netcfg
.status
, n
->status
);
151 virtio_stw_p(vdev
, &netcfg
.max_virtqueue_pairs
, n
->max_queue_pairs
);
152 virtio_stw_p(vdev
, &netcfg
.mtu
, n
->net_conf
.mtu
);
153 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
154 virtio_stl_p(vdev
, &netcfg
.speed
, n
->net_conf
.speed
);
155 netcfg
.duplex
= n
->net_conf
.duplex
;
156 netcfg
.rss_max_key_size
= VIRTIO_NET_RSS_MAX_KEY_SIZE
;
157 virtio_stw_p(vdev
, &netcfg
.rss_max_indirection_table_length
,
158 virtio_host_has_feature(vdev
, VIRTIO_NET_F_RSS
) ?
159 VIRTIO_NET_RSS_MAX_TABLE_LEN
: 1);
160 virtio_stl_p(vdev
, &netcfg
.supported_hash_types
,
161 VIRTIO_NET_RSS_SUPPORTED_HASHES
);
162 memcpy(config
, &netcfg
, n
->config_size
);
165 * Is this VDPA? No peer means not VDPA: there's no way to
166 * disconnect/reconnect a VDPA peer.
168 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
169 ret
= vhost_net_get_config(get_vhost_net(nc
->peer
), (uint8_t *)&netcfg
,
176 * Some NIC/kernel combinations present 0 as the mac address. As that
177 * is not a legal address, try to proceed with the address from the
178 * QEMU command line in the hope that the address has been configured
179 * correctly elsewhere - just not reported by the device.
181 if (memcmp(&netcfg
.mac
, &zero
, sizeof(zero
)) == 0) {
182 info_report("Zero hardware mac address detected. Ignoring.");
183 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
186 netcfg
.status
|= virtio_tswap16(vdev
,
187 n
->status
& VIRTIO_NET_S_ANNOUNCE
);
188 memcpy(config
, &netcfg
, n
->config_size
);
192 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
194 VirtIONet
*n
= VIRTIO_NET(vdev
);
195 struct virtio_net_config netcfg
= {};
196 NetClientState
*nc
= qemu_get_queue(n
->nic
);
198 memcpy(&netcfg
, config
, n
->config_size
);
200 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_MAC_ADDR
) &&
201 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
202 memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
203 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
204 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
208 * Is this VDPA? No peer means not VDPA: there's no way to
209 * disconnect/reconnect a VDPA peer.
211 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
212 vhost_net_set_config(get_vhost_net(nc
->peer
),
213 (uint8_t *)&netcfg
, 0, n
->config_size
,
214 VHOST_SET_CONFIG_TYPE_FRONTEND
);
218 static bool virtio_net_started(VirtIONet
*n
, uint8_t status
)
220 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
221 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
222 (n
->status
& VIRTIO_NET_S_LINK_UP
) && vdev
->vm_running
;
225 static void virtio_net_announce_notify(VirtIONet
*net
)
227 VirtIODevice
*vdev
= VIRTIO_DEVICE(net
);
228 trace_virtio_net_announce_notify();
230 net
->status
|= VIRTIO_NET_S_ANNOUNCE
;
231 virtio_notify_config(vdev
);
234 static void virtio_net_announce_timer(void *opaque
)
236 VirtIONet
*n
= opaque
;
237 trace_virtio_net_announce_timer(n
->announce_timer
.round
);
239 n
->announce_timer
.round
--;
240 virtio_net_announce_notify(n
);
243 static void virtio_net_announce(NetClientState
*nc
)
245 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
246 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
249 * Make sure the virtio migration announcement timer isn't running
250 * If it is, let it trigger announcement so that we do not cause
253 if (n
->announce_timer
.round
) {
257 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
258 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
259 virtio_net_announce_notify(n
);
263 static void virtio_net_vhost_status(VirtIONet
*n
, uint8_t status
)
265 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
266 NetClientState
*nc
= qemu_get_queue(n
->nic
);
267 int queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
268 int cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
269 n
->max_ncs
- n
->max_queue_pairs
: 0;
271 if (!get_vhost_net(nc
->peer
)) {
275 if ((virtio_net_started(n
, status
) && !nc
->peer
->link_down
) ==
276 !!n
->vhost_started
) {
279 if (!n
->vhost_started
) {
282 if (n
->needs_vnet_hdr_swap
) {
283 error_report("backend does not support %s vnet headers; "
284 "falling back on userspace virtio",
285 virtio_is_big_endian(vdev
) ? "BE" : "LE");
289 /* Any packets outstanding? Purge them to avoid touching rings
290 * when vhost is running.
292 for (i
= 0; i
< queue_pairs
; i
++) {
293 NetClientState
*qnc
= qemu_get_subqueue(n
->nic
, i
);
295 /* Purge both directions: TX and RX. */
296 qemu_net_queue_purge(qnc
->peer
->incoming_queue
, qnc
);
297 qemu_net_queue_purge(qnc
->incoming_queue
, qnc
->peer
);
300 if (virtio_has_feature(vdev
->guest_features
, VIRTIO_NET_F_MTU
)) {
301 r
= vhost_net_set_mtu(get_vhost_net(nc
->peer
), n
->net_conf
.mtu
);
303 error_report("%uBytes MTU not supported by the backend",
310 n
->vhost_started
= 1;
311 r
= vhost_net_start(vdev
, n
->nic
->ncs
, queue_pairs
, cvq
);
313 error_report("unable to start vhost net: %d: "
314 "falling back on userspace virtio", -r
);
315 n
->vhost_started
= 0;
318 vhost_net_stop(vdev
, n
->nic
->ncs
, queue_pairs
, cvq
);
319 n
->vhost_started
= 0;
323 static int virtio_net_set_vnet_endian_one(VirtIODevice
*vdev
,
324 NetClientState
*peer
,
327 if (virtio_is_big_endian(vdev
)) {
328 return qemu_set_vnet_be(peer
, enable
);
330 return qemu_set_vnet_le(peer
, enable
);
334 static bool virtio_net_set_vnet_endian(VirtIODevice
*vdev
, NetClientState
*ncs
,
335 int queue_pairs
, bool enable
)
339 for (i
= 0; i
< queue_pairs
; i
++) {
340 if (virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, enable
) < 0 &&
343 virtio_net_set_vnet_endian_one(vdev
, ncs
[i
].peer
, false);
353 static void virtio_net_vnet_endian_status(VirtIONet
*n
, uint8_t status
)
355 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
356 int queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
358 if (virtio_net_started(n
, status
)) {
359 /* Before using the device, we tell the network backend about the
360 * endianness to use when parsing vnet headers. If the backend
361 * can't do it, we fallback onto fixing the headers in the core
364 n
->needs_vnet_hdr_swap
= n
->has_vnet_hdr
&&
365 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
,
367 } else if (virtio_net_started(n
, vdev
->status
)) {
368 /* After using the device, we need to reset the network backend to
369 * the default (guest native endianness), otherwise the guest may
370 * lose network connectivity if it is rebooted into a different
373 virtio_net_set_vnet_endian(vdev
, n
->nic
->ncs
, queue_pairs
, false);
377 static void virtio_net_drop_tx_queue_data(VirtIODevice
*vdev
, VirtQueue
*vq
)
379 unsigned int dropped
= virtqueue_drop_all(vq
);
381 virtio_notify(vdev
, vq
);
385 static int virtio_net_set_status(struct VirtIODevice
*vdev
, uint8_t status
)
387 VirtIONet
*n
= VIRTIO_NET(vdev
);
390 uint8_t queue_status
;
392 virtio_net_vnet_endian_status(n
, status
);
393 virtio_net_vhost_status(n
, status
);
395 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
396 NetClientState
*ncs
= qemu_get_subqueue(n
->nic
, i
);
400 if ((!n
->multiqueue
&& i
!= 0) || i
>= n
->curr_queue_pairs
) {
403 queue_status
= status
;
406 virtio_net_started(n
, queue_status
) && !n
->vhost_started
;
409 qemu_flush_queued_packets(ncs
);
412 if (!q
->tx_waiting
) {
418 timer_mod(q
->tx_timer
,
419 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
421 replay_bh_schedule_event(q
->tx_bh
);
425 timer_del(q
->tx_timer
);
427 qemu_bh_cancel(q
->tx_bh
);
429 if ((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0 &&
430 (queue_status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
432 /* if tx is waiting we are likely have some packets in tx queue
433 * and disabled notification */
435 virtio_queue_set_notification(q
->tx_vq
, 1);
436 virtio_net_drop_tx_queue_data(vdev
, q
->tx_vq
);
443 static void virtio_net_set_link_status(NetClientState
*nc
)
445 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
446 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
447 uint16_t old_status
= n
->status
;
450 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
452 n
->status
|= VIRTIO_NET_S_LINK_UP
;
454 if (n
->status
!= old_status
)
455 virtio_notify_config(vdev
);
457 virtio_net_set_status(vdev
, vdev
->status
);
460 static void rxfilter_notify(NetClientState
*nc
)
462 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
464 if (nc
->rxfilter_notify_enabled
) {
465 char *path
= object_get_canonical_path(OBJECT(n
->qdev
));
466 qapi_event_send_nic_rx_filter_changed(n
->netclient_name
, path
);
469 /* disable event notification to avoid events flooding */
470 nc
->rxfilter_notify_enabled
= 0;
474 static intList
*get_vlan_table(VirtIONet
*n
)
480 for (i
= 0; i
< MAX_VLAN
>> 5; i
++) {
481 for (j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
482 if (n
->vlans
[i
] & (1U << j
)) {
483 QAPI_LIST_PREPEND(list
, (i
<< 5) + j
);
491 static RxFilterInfo
*virtio_net_query_rxfilter(NetClientState
*nc
)
493 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
494 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
499 info
= g_malloc0(sizeof(*info
));
500 info
->name
= g_strdup(nc
->name
);
501 info
->promiscuous
= n
->promisc
;
504 info
->unicast
= RX_STATE_NONE
;
505 } else if (n
->alluni
) {
506 info
->unicast
= RX_STATE_ALL
;
508 info
->unicast
= RX_STATE_NORMAL
;
512 info
->multicast
= RX_STATE_NONE
;
513 } else if (n
->allmulti
) {
514 info
->multicast
= RX_STATE_ALL
;
516 info
->multicast
= RX_STATE_NORMAL
;
519 info
->broadcast_allowed
= n
->nobcast
;
520 info
->multicast_overflow
= n
->mac_table
.multi_overflow
;
521 info
->unicast_overflow
= n
->mac_table
.uni_overflow
;
523 info
->main_mac
= qemu_mac_strdup_printf(n
->mac
);
526 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
527 QAPI_LIST_PREPEND(str_list
,
528 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
530 info
->unicast_table
= str_list
;
533 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
534 QAPI_LIST_PREPEND(str_list
,
535 qemu_mac_strdup_printf(n
->mac_table
.macs
+ i
* ETH_ALEN
));
537 info
->multicast_table
= str_list
;
538 info
->vlan_table
= get_vlan_table(n
);
540 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VLAN
)) {
541 info
->vlan
= RX_STATE_ALL
;
542 } else if (!info
->vlan_table
) {
543 info
->vlan
= RX_STATE_NONE
;
545 info
->vlan
= RX_STATE_NORMAL
;
548 /* enable event notification after query */
549 nc
->rxfilter_notify_enabled
= 1;
554 static void virtio_net_queue_reset(VirtIODevice
*vdev
, uint32_t queue_index
)
556 VirtIONet
*n
= VIRTIO_NET(vdev
);
559 /* validate queue_index and skip for cvq */
560 if (queue_index
>= n
->max_queue_pairs
* 2) {
564 nc
= qemu_get_subqueue(n
->nic
, vq2q(queue_index
));
570 if (get_vhost_net(nc
->peer
) &&
571 nc
->peer
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
572 vhost_net_virtqueue_reset(vdev
, nc
, queue_index
);
575 flush_or_purge_queued_packets(nc
);
578 static void virtio_net_queue_enable(VirtIODevice
*vdev
, uint32_t queue_index
)
580 VirtIONet
*n
= VIRTIO_NET(vdev
);
584 /* validate queue_index and skip for cvq */
585 if (queue_index
>= n
->max_queue_pairs
* 2) {
589 nc
= qemu_get_subqueue(n
->nic
, vq2q(queue_index
));
591 if (!nc
->peer
|| !vdev
->vhost_started
) {
595 if (get_vhost_net(nc
->peer
) &&
596 nc
->peer
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
597 r
= vhost_net_virtqueue_restart(vdev
, nc
, queue_index
);
599 error_report("unable to restart vhost net virtqueue: %d, "
600 "when resetting the queue", queue_index
);
605 static void peer_test_vnet_hdr(VirtIONet
*n
)
607 NetClientState
*nc
= qemu_get_queue(n
->nic
);
612 n
->has_vnet_hdr
= qemu_has_vnet_hdr(nc
->peer
);
615 static int peer_has_vnet_hdr(VirtIONet
*n
)
617 return n
->has_vnet_hdr
;
620 static int peer_has_ufo(VirtIONet
*n
)
622 if (!peer_has_vnet_hdr(n
))
625 n
->has_ufo
= qemu_has_ufo(qemu_get_queue(n
->nic
)->peer
);
630 static int peer_has_uso(VirtIONet
*n
)
632 if (!peer_has_vnet_hdr(n
)) {
636 return qemu_has_uso(qemu_get_queue(n
->nic
)->peer
);
639 static void virtio_net_set_mrg_rx_bufs(VirtIONet
*n
, int mergeable_rx_bufs
,
640 int version_1
, int hash_report
)
645 n
->mergeable_rx_bufs
= mergeable_rx_bufs
;
648 n
->guest_hdr_len
= hash_report
?
649 sizeof(struct virtio_net_hdr_v1_hash
) :
650 sizeof(struct virtio_net_hdr_mrg_rxbuf
);
651 n
->rss_data
.populate_hash
= !!hash_report
;
653 n
->guest_hdr_len
= n
->mergeable_rx_bufs
?
654 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
655 sizeof(struct virtio_net_hdr
);
656 n
->rss_data
.populate_hash
= false;
659 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
660 nc
= qemu_get_subqueue(n
->nic
, i
);
662 if (peer_has_vnet_hdr(n
) &&
663 qemu_has_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
)) {
664 qemu_set_vnet_hdr_len(nc
->peer
, n
->guest_hdr_len
);
665 n
->host_hdr_len
= n
->guest_hdr_len
;
670 static int virtio_net_max_tx_queue_size(VirtIONet
*n
)
672 NetClientState
*peer
= n
->nic_conf
.peers
.ncs
[0];
675 * Backends other than vhost-user or vhost-vdpa don't support max queue
679 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
682 switch(peer
->info
->type
) {
683 case NET_CLIENT_DRIVER_VHOST_USER
:
684 case NET_CLIENT_DRIVER_VHOST_VDPA
:
685 return VIRTQUEUE_MAX_SIZE
;
687 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
;
691 static int peer_attach(VirtIONet
*n
, int index
)
693 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
699 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
700 vhost_set_vring_enable(nc
->peer
, 1);
703 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
707 if (n
->max_queue_pairs
== 1) {
711 return tap_enable(nc
->peer
);
714 static int peer_detach(VirtIONet
*n
, int index
)
716 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
722 if (nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
723 vhost_set_vring_enable(nc
->peer
, 0);
726 if (nc
->peer
->info
->type
!= NET_CLIENT_DRIVER_TAP
) {
730 return tap_disable(nc
->peer
);
733 static void virtio_net_set_queue_pairs(VirtIONet
*n
)
738 if (n
->nic
->peer_deleted
) {
742 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
743 if (i
< n
->curr_queue_pairs
) {
744 r
= peer_attach(n
, i
);
747 r
= peer_detach(n
, i
);
753 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
);
755 static uint64_t virtio_net_get_features(VirtIODevice
*vdev
, uint64_t features
,
758 VirtIONet
*n
= VIRTIO_NET(vdev
);
759 NetClientState
*nc
= qemu_get_queue(n
->nic
);
761 /* Firstly sync all virtio-net possible supported features */
762 features
|= n
->host_features
;
764 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
766 if (!peer_has_vnet_hdr(n
)) {
767 virtio_clear_feature(&features
, VIRTIO_NET_F_CSUM
);
768 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
769 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
770 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
772 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_CSUM
);
773 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO4
);
774 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_TSO6
);
775 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ECN
);
777 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_USO
);
778 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO4
);
779 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO6
);
781 virtio_clear_feature(&features
, VIRTIO_NET_F_HASH_REPORT
);
784 if (!peer_has_vnet_hdr(n
) || !peer_has_ufo(n
)) {
785 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_UFO
);
786 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_UFO
);
789 if (!peer_has_uso(n
)) {
790 virtio_clear_feature(&features
, VIRTIO_NET_F_HOST_USO
);
791 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO4
);
792 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_USO6
);
795 if (!get_vhost_net(nc
->peer
)) {
799 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
800 virtio_clear_feature(&features
, VIRTIO_NET_F_RSS
);
802 features
= vhost_net_get_features(get_vhost_net(nc
->peer
), features
);
803 vdev
->backend_features
= features
;
805 if (n
->mtu_bypass_backend
&&
806 (n
->host_features
& 1ULL << VIRTIO_NET_F_MTU
)) {
807 features
|= (1ULL << VIRTIO_NET_F_MTU
);
811 * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
812 * enabled. This happens in the vDPA case.
814 * Make sure the feature set is not incoherent, as the driver could refuse
817 * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
818 * helping guest to notify the new location with vDPA devices that does not
821 if (!virtio_has_feature(vdev
->backend_features
, VIRTIO_NET_F_CTRL_VQ
)) {
822 virtio_clear_feature(&features
, VIRTIO_NET_F_GUEST_ANNOUNCE
);
828 static uint64_t virtio_net_bad_features(VirtIODevice
*vdev
)
830 uint64_t features
= 0;
832 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
834 virtio_add_feature(&features
, VIRTIO_NET_F_MAC
);
835 virtio_add_feature(&features
, VIRTIO_NET_F_CSUM
);
836 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO4
);
837 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_TSO6
);
838 virtio_add_feature(&features
, VIRTIO_NET_F_HOST_ECN
);
843 static void virtio_net_apply_guest_offloads(VirtIONet
*n
)
845 qemu_set_offload(qemu_get_queue(n
->nic
)->peer
,
846 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_CSUM
)),
847 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO4
)),
848 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_TSO6
)),
849 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_ECN
)),
850 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_UFO
)),
851 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_USO4
)),
852 !!(n
->curr_guest_offloads
& (1ULL << VIRTIO_NET_F_GUEST_USO6
)));
855 static uint64_t virtio_net_guest_offloads_by_features(uint64_t features
)
857 static const uint64_t guest_offloads_mask
=
858 (1ULL << VIRTIO_NET_F_GUEST_CSUM
) |
859 (1ULL << VIRTIO_NET_F_GUEST_TSO4
) |
860 (1ULL << VIRTIO_NET_F_GUEST_TSO6
) |
861 (1ULL << VIRTIO_NET_F_GUEST_ECN
) |
862 (1ULL << VIRTIO_NET_F_GUEST_UFO
) |
863 (1ULL << VIRTIO_NET_F_GUEST_USO4
) |
864 (1ULL << VIRTIO_NET_F_GUEST_USO6
);
866 return guest_offloads_mask
& features
;
869 uint64_t virtio_net_supported_guest_offloads(const VirtIONet
*n
)
871 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
872 return virtio_net_guest_offloads_by_features(vdev
->guest_features
);
881 * Set the failover primary device
883 * @opaque: FailoverId to setup
884 * @opts: opts for device we are handling
885 * @errp: returns an error if this function fails
887 static int failover_set_primary(DeviceState
*dev
, void *opaque
)
889 FailoverDevice
*fdev
= opaque
;
890 PCIDevice
*pci_dev
= (PCIDevice
*)
891 object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
);
897 if (!g_strcmp0(pci_dev
->failover_pair_id
, fdev
->n
->netclient_name
)) {
906 * Find the primary device for this failover virtio-net
908 * @n: VirtIONet device
909 * @errp: returns an error if this function fails
911 static DeviceState
*failover_find_primary_device(VirtIONet
*n
)
913 FailoverDevice fdev
= {
917 qbus_walk_children(sysbus_get_default(), failover_set_primary
, NULL
,
922 static void failover_add_primary(VirtIONet
*n
, Error
**errp
)
925 DeviceState
*dev
= failover_find_primary_device(n
);
931 if (!n
->primary_opts
) {
932 error_setg(errp
, "Primary device not found");
933 error_append_hint(errp
, "Virtio-net failover will not work. Make "
934 "sure primary device has parameter"
935 " failover_pair_id=%s\n", n
->netclient_name
);
939 dev
= qdev_device_add_from_qdict(n
->primary_opts
,
940 n
->primary_opts_from_json
,
943 qobject_unref(n
->primary_opts
);
944 n
->primary_opts
= NULL
;
946 object_unref(OBJECT(dev
));
948 error_propagate(errp
, err
);
951 static void virtio_net_set_features(VirtIODevice
*vdev
, uint64_t features
)
953 VirtIONet
*n
= VIRTIO_NET(vdev
);
957 if (n
->mtu_bypass_backend
&&
958 !virtio_has_feature(vdev
->backend_features
, VIRTIO_NET_F_MTU
)) {
959 features
&= ~(1ULL << VIRTIO_NET_F_MTU
);
962 virtio_net_set_multiqueue(n
,
963 virtio_has_feature(features
, VIRTIO_NET_F_RSS
) ||
964 virtio_has_feature(features
, VIRTIO_NET_F_MQ
));
966 virtio_net_set_mrg_rx_bufs(n
,
967 virtio_has_feature(features
,
968 VIRTIO_NET_F_MRG_RXBUF
),
969 virtio_has_feature(features
,
971 virtio_has_feature(features
,
972 VIRTIO_NET_F_HASH_REPORT
));
974 n
->rsc4_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
975 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO4
);
976 n
->rsc6_enabled
= virtio_has_feature(features
, VIRTIO_NET_F_RSC_EXT
) &&
977 virtio_has_feature(features
, VIRTIO_NET_F_GUEST_TSO6
);
978 n
->rss_data
.redirect
= virtio_has_feature(features
, VIRTIO_NET_F_RSS
);
980 if (n
->has_vnet_hdr
) {
981 n
->curr_guest_offloads
=
982 virtio_net_guest_offloads_by_features(features
);
983 virtio_net_apply_guest_offloads(n
);
986 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
987 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, i
);
989 if (!get_vhost_net(nc
->peer
)) {
992 vhost_net_ack_features(get_vhost_net(nc
->peer
), features
);
995 * keep acked_features in NetVhostUserState up-to-date so it
996 * can't miss any features configured by guest virtio driver.
998 vhost_net_save_acked_features(nc
->peer
);
1001 if (!virtio_has_feature(features
, VIRTIO_NET_F_CTRL_VLAN
)) {
1002 memset(n
->vlans
, 0xff, MAX_VLAN
>> 3);
1005 if (virtio_has_feature(features
, VIRTIO_NET_F_STANDBY
)) {
1006 qapi_event_send_failover_negotiated(n
->netclient_name
);
1007 qatomic_set(&n
->failover_primary_hidden
, false);
1008 failover_add_primary(n
, &err
);
1010 if (!qtest_enabled()) {
1011 warn_report_err(err
);
1019 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
1020 struct iovec
*iov
, unsigned int iov_cnt
)
1024 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1026 s
= iov_to_buf(iov
, iov_cnt
, 0, &on
, sizeof(on
));
1027 if (s
!= sizeof(on
)) {
1028 return VIRTIO_NET_ERR
;
1031 if (cmd
== VIRTIO_NET_CTRL_RX_PROMISC
) {
1033 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLMULTI
) {
1035 } else if (cmd
== VIRTIO_NET_CTRL_RX_ALLUNI
) {
1037 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOMULTI
) {
1039 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOUNI
) {
1041 } else if (cmd
== VIRTIO_NET_CTRL_RX_NOBCAST
) {
1044 return VIRTIO_NET_ERR
;
1047 rxfilter_notify(nc
);
1049 return VIRTIO_NET_OK
;
1052 static int virtio_net_handle_offloads(VirtIONet
*n
, uint8_t cmd
,
1053 struct iovec
*iov
, unsigned int iov_cnt
)
1055 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1059 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
1060 return VIRTIO_NET_ERR
;
1063 s
= iov_to_buf(iov
, iov_cnt
, 0, &offloads
, sizeof(offloads
));
1064 if (s
!= sizeof(offloads
)) {
1065 return VIRTIO_NET_ERR
;
1068 if (cmd
== VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
) {
1069 uint64_t supported_offloads
;
1071 offloads
= virtio_ldq_p(vdev
, &offloads
);
1073 if (!n
->has_vnet_hdr
) {
1074 return VIRTIO_NET_ERR
;
1077 n
->rsc4_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1078 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO4
);
1079 n
->rsc6_enabled
= virtio_has_feature(offloads
, VIRTIO_NET_F_RSC_EXT
) &&
1080 virtio_has_feature(offloads
, VIRTIO_NET_F_GUEST_TSO6
);
1081 virtio_clear_feature(&offloads
, VIRTIO_NET_F_RSC_EXT
);
1083 supported_offloads
= virtio_net_supported_guest_offloads(n
);
1084 if (offloads
& ~supported_offloads
) {
1085 return VIRTIO_NET_ERR
;
1088 n
->curr_guest_offloads
= offloads
;
1089 virtio_net_apply_guest_offloads(n
);
1091 return VIRTIO_NET_OK
;
1093 return VIRTIO_NET_ERR
;
1097 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
1098 struct iovec
*iov
, unsigned int iov_cnt
)
1100 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1101 struct virtio_net_ctrl_mac mac_data
;
1103 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1105 if (cmd
== VIRTIO_NET_CTRL_MAC_ADDR_SET
) {
1106 if (iov_size(iov
, iov_cnt
) != sizeof(n
->mac
)) {
1107 return VIRTIO_NET_ERR
;
1109 s
= iov_to_buf(iov
, iov_cnt
, 0, &n
->mac
, sizeof(n
->mac
));
1110 assert(s
== sizeof(n
->mac
));
1111 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
1112 rxfilter_notify(nc
);
1114 return VIRTIO_NET_OK
;
1117 if (cmd
!= VIRTIO_NET_CTRL_MAC_TABLE_SET
) {
1118 return VIRTIO_NET_ERR
;
1122 int first_multi
= 0;
1123 uint8_t uni_overflow
= 0;
1124 uint8_t multi_overflow
= 0;
1125 uint8_t *macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
1127 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1128 sizeof(mac_data
.entries
));
1129 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1130 if (s
!= sizeof(mac_data
.entries
)) {
1133 iov_discard_front(&iov
, &iov_cnt
, s
);
1135 if (mac_data
.entries
* ETH_ALEN
> iov_size(iov
, iov_cnt
)) {
1139 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
) {
1140 s
= iov_to_buf(iov
, iov_cnt
, 0, macs
,
1141 mac_data
.entries
* ETH_ALEN
);
1142 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1145 in_use
+= mac_data
.entries
;
1150 iov_discard_front(&iov
, &iov_cnt
, mac_data
.entries
* ETH_ALEN
);
1152 first_multi
= in_use
;
1154 s
= iov_to_buf(iov
, iov_cnt
, 0, &mac_data
.entries
,
1155 sizeof(mac_data
.entries
));
1156 mac_data
.entries
= virtio_ldl_p(vdev
, &mac_data
.entries
);
1157 if (s
!= sizeof(mac_data
.entries
)) {
1161 iov_discard_front(&iov
, &iov_cnt
, s
);
1163 if (mac_data
.entries
* ETH_ALEN
!= iov_size(iov
, iov_cnt
)) {
1167 if (mac_data
.entries
<= MAC_TABLE_ENTRIES
- in_use
) {
1168 s
= iov_to_buf(iov
, iov_cnt
, 0, &macs
[in_use
* ETH_ALEN
],
1169 mac_data
.entries
* ETH_ALEN
);
1170 if (s
!= mac_data
.entries
* ETH_ALEN
) {
1173 in_use
+= mac_data
.entries
;
1178 n
->mac_table
.in_use
= in_use
;
1179 n
->mac_table
.first_multi
= first_multi
;
1180 n
->mac_table
.uni_overflow
= uni_overflow
;
1181 n
->mac_table
.multi_overflow
= multi_overflow
;
1182 memcpy(n
->mac_table
.macs
, macs
, MAC_TABLE_ENTRIES
* ETH_ALEN
);
1184 rxfilter_notify(nc
);
1186 return VIRTIO_NET_OK
;
1190 return VIRTIO_NET_ERR
;
1193 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
1194 struct iovec
*iov
, unsigned int iov_cnt
)
1196 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1199 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1201 s
= iov_to_buf(iov
, iov_cnt
, 0, &vid
, sizeof(vid
));
1202 vid
= virtio_lduw_p(vdev
, &vid
);
1203 if (s
!= sizeof(vid
)) {
1204 return VIRTIO_NET_ERR
;
1207 if (vid
>= MAX_VLAN
)
1208 return VIRTIO_NET_ERR
;
1210 if (cmd
== VIRTIO_NET_CTRL_VLAN_ADD
)
1211 n
->vlans
[vid
>> 5] |= (1U << (vid
& 0x1f));
1212 else if (cmd
== VIRTIO_NET_CTRL_VLAN_DEL
)
1213 n
->vlans
[vid
>> 5] &= ~(1U << (vid
& 0x1f));
1215 return VIRTIO_NET_ERR
;
1217 rxfilter_notify(nc
);
1219 return VIRTIO_NET_OK
;
1222 static int virtio_net_handle_announce(VirtIONet
*n
, uint8_t cmd
,
1223 struct iovec
*iov
, unsigned int iov_cnt
)
1225 trace_virtio_net_handle_announce(n
->announce_timer
.round
);
1226 if (cmd
== VIRTIO_NET_CTRL_ANNOUNCE_ACK
&&
1227 n
->status
& VIRTIO_NET_S_ANNOUNCE
) {
1228 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
1229 if (n
->announce_timer
.round
) {
1230 qemu_announce_timer_step(&n
->announce_timer
);
1232 return VIRTIO_NET_OK
;
1234 return VIRTIO_NET_ERR
;
1238 static bool virtio_net_attach_ebpf_to_backend(NICState
*nic
, int prog_fd
)
1240 NetClientState
*nc
= qemu_get_peer(qemu_get_queue(nic
), 0);
1241 if (nc
== NULL
|| nc
->info
->set_steering_ebpf
== NULL
) {
1245 trace_virtio_net_rss_attach_ebpf(nic
, prog_fd
);
1246 return nc
->info
->set_steering_ebpf(nc
, prog_fd
);
1249 static void rss_data_to_rss_config(struct VirtioNetRssData
*data
,
1250 struct EBPFRSSConfig
*config
)
1252 config
->redirect
= data
->redirect
;
1253 config
->populate_hash
= data
->populate_hash
;
1254 config
->hash_types
= data
->hash_types
;
1255 config
->indirections_len
= data
->indirections_len
;
1256 config
->default_queue
= data
->default_queue
;
1259 static bool virtio_net_attach_ebpf_rss(VirtIONet
*n
)
1261 struct EBPFRSSConfig config
= {};
1263 if (!ebpf_rss_is_loaded(&n
->ebpf_rss
)) {
1267 rss_data_to_rss_config(&n
->rss_data
, &config
);
1269 if (!ebpf_rss_set_all(&n
->ebpf_rss
, &config
,
1270 n
->rss_data
.indirections_table
, n
->rss_data
.key
,
1275 if (!virtio_net_attach_ebpf_to_backend(n
->nic
, n
->ebpf_rss
.program_fd
)) {
1282 static void virtio_net_detach_ebpf_rss(VirtIONet
*n
)
1284 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1287 static void virtio_net_commit_rss_config(VirtIONet
*n
)
1289 if (n
->rss_data
.enabled
) {
1290 n
->rss_data
.enabled_software_rss
= n
->rss_data
.populate_hash
;
1291 if (n
->rss_data
.populate_hash
) {
1292 virtio_net_detach_ebpf_rss(n
);
1293 } else if (!virtio_net_attach_ebpf_rss(n
)) {
1294 if (get_vhost_net(qemu_get_queue(n
->nic
)->peer
)) {
1295 warn_report("Can't load eBPF RSS for vhost");
1297 warn_report("Can't load eBPF RSS - fallback to software RSS");
1298 n
->rss_data
.enabled_software_rss
= true;
1302 trace_virtio_net_rss_enable(n
,
1303 n
->rss_data
.hash_types
,
1304 n
->rss_data
.indirections_len
,
1305 sizeof(n
->rss_data
.key
));
1307 virtio_net_detach_ebpf_rss(n
);
1308 trace_virtio_net_rss_disable(n
);
1312 static void virtio_net_disable_rss(VirtIONet
*n
)
1314 if (!n
->rss_data
.enabled
) {
1318 n
->rss_data
.enabled
= false;
1319 virtio_net_commit_rss_config(n
);
1322 static bool virtio_net_load_ebpf_fds(VirtIONet
*n
, Error
**errp
)
1324 int fds
[EBPF_RSS_MAX_FDS
] = { [0 ... EBPF_RSS_MAX_FDS
- 1] = -1};
1328 if (n
->nr_ebpf_rss_fds
!= EBPF_RSS_MAX_FDS
) {
1329 error_setg(errp
, "Expected %d file descriptors but got %d",
1330 EBPF_RSS_MAX_FDS
, n
->nr_ebpf_rss_fds
);
1334 for (i
= 0; i
< n
->nr_ebpf_rss_fds
; i
++) {
1335 fds
[i
] = monitor_fd_param(monitor_cur(), n
->ebpf_rss_fds
[i
], errp
);
1342 ret
= ebpf_rss_load_fds(&n
->ebpf_rss
, fds
[0], fds
[1], fds
[2], fds
[3], errp
);
1346 for (i
= 0; i
< n
->nr_ebpf_rss_fds
&& fds
[i
] != -1; i
++) {
1354 static bool virtio_net_load_ebpf(VirtIONet
*n
, Error
**errp
)
1356 if (!virtio_net_attach_ebpf_to_backend(n
->nic
, -1)) {
1360 trace_virtio_net_rss_load(n
, n
->nr_ebpf_rss_fds
, n
->ebpf_rss_fds
);
1363 * If user explicitly gave QEMU RSS FDs to use, then
1364 * failing to use them must be considered a fatal
1365 * error. If no RSS FDs were provided, QEMU is trying
1366 * eBPF on a "best effort" basis only, so report a
1367 * warning and allow fallback to software RSS.
1369 if (n
->ebpf_rss_fds
) {
1370 return virtio_net_load_ebpf_fds(n
, errp
);
1373 ebpf_rss_load(&n
->ebpf_rss
, &error_warn
);
1377 static void virtio_net_unload_ebpf(VirtIONet
*n
)
1379 virtio_net_attach_ebpf_to_backend(n
->nic
, -1);
1380 ebpf_rss_unload(&n
->ebpf_rss
);
1383 static uint16_t virtio_net_handle_rss(VirtIONet
*n
,
1385 unsigned int iov_cnt
,
1388 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1389 struct virtio_net_rss_config cfg
;
1390 size_t s
, offset
= 0, size_get
;
1391 uint16_t queue_pairs
, i
;
1396 const char *err_msg
= "";
1397 uint32_t err_value
= 0;
1399 if (do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_RSS
)) {
1400 err_msg
= "RSS is not negotiated";
1403 if (!do_rss
&& !virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_HASH_REPORT
)) {
1404 err_msg
= "Hash report is not negotiated";
1407 size_get
= offsetof(struct virtio_net_rss_config
, indirection_table
);
1408 s
= iov_to_buf(iov
, iov_cnt
, offset
, &cfg
, size_get
);
1409 if (s
!= size_get
) {
1410 err_msg
= "Short command buffer";
1411 err_value
= (uint32_t)s
;
1414 n
->rss_data
.hash_types
= virtio_ldl_p(vdev
, &cfg
.hash_types
);
1415 n
->rss_data
.indirections_len
=
1416 virtio_lduw_p(vdev
, &cfg
.indirection_table_mask
);
1418 n
->rss_data
.indirections_len
= 0;
1420 if (n
->rss_data
.indirections_len
>= VIRTIO_NET_RSS_MAX_TABLE_LEN
) {
1421 err_msg
= "Too large indirection table";
1422 err_value
= n
->rss_data
.indirections_len
;
1425 n
->rss_data
.indirections_len
++;
1426 if (!is_power_of_2(n
->rss_data
.indirections_len
)) {
1427 err_msg
= "Invalid size of indirection table";
1428 err_value
= n
->rss_data
.indirections_len
;
1431 n
->rss_data
.default_queue
= do_rss
?
1432 virtio_lduw_p(vdev
, &cfg
.unclassified_queue
) : 0;
1433 if (n
->rss_data
.default_queue
>= n
->max_queue_pairs
) {
1434 err_msg
= "Invalid default queue";
1435 err_value
= n
->rss_data
.default_queue
;
1439 size_get
= sizeof(uint16_t) * n
->rss_data
.indirections_len
;
1440 g_free(n
->rss_data
.indirections_table
);
1441 n
->rss_data
.indirections_table
= g_malloc(size_get
);
1442 if (!n
->rss_data
.indirections_table
) {
1443 err_msg
= "Can't allocate indirections table";
1444 err_value
= n
->rss_data
.indirections_len
;
1447 s
= iov_to_buf(iov
, iov_cnt
, offset
,
1448 n
->rss_data
.indirections_table
, size_get
);
1449 if (s
!= size_get
) {
1450 err_msg
= "Short indirection table buffer";
1451 err_value
= (uint32_t)s
;
1454 for (i
= 0; i
< n
->rss_data
.indirections_len
; ++i
) {
1455 uint16_t val
= n
->rss_data
.indirections_table
[i
];
1456 n
->rss_data
.indirections_table
[i
] = virtio_lduw_p(vdev
, &val
);
1459 size_get
= sizeof(temp
);
1460 s
= iov_to_buf(iov
, iov_cnt
, offset
, &temp
, size_get
);
1461 if (s
!= size_get
) {
1462 err_msg
= "Can't get queue_pairs";
1463 err_value
= (uint32_t)s
;
1466 queue_pairs
= do_rss
? virtio_lduw_p(vdev
, &temp
.us
) : n
->curr_queue_pairs
;
1467 if (queue_pairs
== 0 || queue_pairs
> n
->max_queue_pairs
) {
1468 err_msg
= "Invalid number of queue_pairs";
1469 err_value
= queue_pairs
;
1472 if (temp
.b
> VIRTIO_NET_RSS_MAX_KEY_SIZE
) {
1473 err_msg
= "Invalid key size";
1477 if (!temp
.b
&& n
->rss_data
.hash_types
) {
1478 err_msg
= "No key provided";
1482 if (!temp
.b
&& !n
->rss_data
.hash_types
) {
1483 virtio_net_disable_rss(n
);
1488 s
= iov_to_buf(iov
, iov_cnt
, offset
, n
->rss_data
.key
, size_get
);
1489 if (s
!= size_get
) {
1490 err_msg
= "Can get key buffer";
1491 err_value
= (uint32_t)s
;
1494 n
->rss_data
.enabled
= true;
1495 virtio_net_commit_rss_config(n
);
1498 trace_virtio_net_rss_error(n
, err_msg
, err_value
);
1499 virtio_net_disable_rss(n
);
1503 static int virtio_net_handle_mq(VirtIONet
*n
, uint8_t cmd
,
1504 struct iovec
*iov
, unsigned int iov_cnt
)
1506 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1507 uint16_t queue_pairs
;
1508 NetClientState
*nc
= qemu_get_queue(n
->nic
);
1510 virtio_net_disable_rss(n
);
1511 if (cmd
== VIRTIO_NET_CTRL_MQ_HASH_CONFIG
) {
1512 queue_pairs
= virtio_net_handle_rss(n
, iov
, iov_cnt
, false);
1513 return queue_pairs
? VIRTIO_NET_OK
: VIRTIO_NET_ERR
;
1515 if (cmd
== VIRTIO_NET_CTRL_MQ_RSS_CONFIG
) {
1516 queue_pairs
= virtio_net_handle_rss(n
, iov
, iov_cnt
, true);
1517 } else if (cmd
== VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
) {
1518 struct virtio_net_ctrl_mq mq
;
1520 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_MQ
)) {
1521 return VIRTIO_NET_ERR
;
1523 s
= iov_to_buf(iov
, iov_cnt
, 0, &mq
, sizeof(mq
));
1524 if (s
!= sizeof(mq
)) {
1525 return VIRTIO_NET_ERR
;
1527 queue_pairs
= virtio_lduw_p(vdev
, &mq
.virtqueue_pairs
);
1530 return VIRTIO_NET_ERR
;
1533 if (queue_pairs
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
1534 queue_pairs
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
1535 queue_pairs
> n
->max_queue_pairs
||
1537 return VIRTIO_NET_ERR
;
1540 n
->curr_queue_pairs
= queue_pairs
;
1541 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
1543 * Avoid updating the backend for a vdpa device: We're only interested
1544 * in updating the device model queues.
1546 return VIRTIO_NET_OK
;
1548 /* stop the backend before changing the number of queue_pairs to avoid handling a
1550 virtio_net_set_status(vdev
, vdev
->status
);
1551 virtio_net_set_queue_pairs(n
);
1553 return VIRTIO_NET_OK
;
1556 size_t virtio_net_handle_ctrl_iov(VirtIODevice
*vdev
,
1557 const struct iovec
*in_sg
, unsigned in_num
,
1558 const struct iovec
*out_sg
,
1561 VirtIONet
*n
= VIRTIO_NET(vdev
);
1562 struct virtio_net_ctrl_hdr ctrl
;
1563 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1565 struct iovec
*iov
, *iov2
;
1567 if (iov_size(in_sg
, in_num
) < sizeof(status
) ||
1568 iov_size(out_sg
, out_num
) < sizeof(ctrl
)) {
1569 virtio_error(vdev
, "virtio-net ctrl missing headers");
1573 iov2
= iov
= g_memdup2(out_sg
, sizeof(struct iovec
) * out_num
);
1574 s
= iov_to_buf(iov
, out_num
, 0, &ctrl
, sizeof(ctrl
));
1575 iov_discard_front(&iov
, &out_num
, sizeof(ctrl
));
1576 if (s
!= sizeof(ctrl
)) {
1577 status
= VIRTIO_NET_ERR
;
1578 } else if (ctrl
.class == VIRTIO_NET_CTRL_RX
) {
1579 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, iov
, out_num
);
1580 } else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
) {
1581 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, iov
, out_num
);
1582 } else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
) {
1583 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, iov
, out_num
);
1584 } else if (ctrl
.class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1585 status
= virtio_net_handle_announce(n
, ctrl
.cmd
, iov
, out_num
);
1586 } else if (ctrl
.class == VIRTIO_NET_CTRL_MQ
) {
1587 status
= virtio_net_handle_mq(n
, ctrl
.cmd
, iov
, out_num
);
1588 } else if (ctrl
.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS
) {
1589 status
= virtio_net_handle_offloads(n
, ctrl
.cmd
, iov
, out_num
);
1592 s
= iov_from_buf(in_sg
, in_num
, 0, &status
, sizeof(status
));
1593 assert(s
== sizeof(status
));
1596 return sizeof(status
);
1599 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
1601 VirtQueueElement
*elem
;
1605 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
1610 written
= virtio_net_handle_ctrl_iov(vdev
, elem
->in_sg
, elem
->in_num
,
1611 elem
->out_sg
, elem
->out_num
);
1613 virtqueue_push(vq
, elem
, written
);
1614 virtio_notify(vdev
, vq
);
1617 virtqueue_detach_element(vq
, elem
, 0);
1626 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
1628 VirtIONet
*n
= VIRTIO_NET(vdev
);
1629 int queue_index
= vq2q(virtio_get_queue_index(vq
));
1631 qemu_flush_queued_packets(qemu_get_subqueue(n
->nic
, queue_index
));
1634 static bool virtio_net_can_receive(NetClientState
*nc
)
1636 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1637 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1638 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
1640 if (!vdev
->vm_running
) {
1644 if (nc
->queue_index
>= n
->curr_queue_pairs
) {
1648 if (!virtio_queue_ready(q
->rx_vq
) ||
1649 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1656 static int virtio_net_has_buffers(VirtIONetQueue
*q
, int bufsize
)
1659 unsigned int in_bytes
;
1660 VirtIONet
*n
= q
->n
;
1662 while (virtio_queue_empty(q
->rx_vq
) || n
->mergeable_rx_bufs
) {
1663 opaque
= virtqueue_get_avail_bytes(q
->rx_vq
, &in_bytes
, NULL
,
1665 /* Buffer is enough, disable notifiaction */
1666 if (bufsize
<= in_bytes
) {
1670 if (virtio_queue_enable_notification_and_check(q
->rx_vq
, opaque
)) {
1671 /* Guest has added some buffers, try again */
1678 virtio_queue_set_notification(q
->rx_vq
, 0);
1683 static void virtio_net_hdr_swap(VirtIODevice
*vdev
, struct virtio_net_hdr
*hdr
)
1685 virtio_tswap16s(vdev
, &hdr
->hdr_len
);
1686 virtio_tswap16s(vdev
, &hdr
->gso_size
);
1687 virtio_tswap16s(vdev
, &hdr
->csum_start
);
1688 virtio_tswap16s(vdev
, &hdr
->csum_offset
);
1691 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1692 * it never finds out that the packets don't have valid checksums. This
1693 * causes dhclient to get upset. Fedora's carried a patch for ages to
1694 * fix this with Xen but it hasn't appeared in an upstream release of
1697 * To avoid breaking existing guests, we catch udp packets and add
1698 * checksums. This is terrible but it's better than hacking the guest
1701 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1702 * we should provide a mechanism to disable it to avoid polluting the host
1705 static void work_around_broken_dhclient(struct virtio_net_hdr
*hdr
,
1706 uint8_t *buf
, size_t size
)
1708 size_t csum_size
= ETH_HLEN
+ sizeof(struct ip_header
) +
1709 sizeof(struct udp_header
);
1711 if ((hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) && /* missing csum */
1712 (size
>= csum_size
&& size
< 1500) && /* normal sized MTU */
1713 (buf
[12] == 0x08 && buf
[13] == 0x00) && /* ethertype == IPv4 */
1714 (buf
[23] == 17) && /* ip.protocol == UDP */
1715 (buf
[34] == 0 && buf
[35] == 67)) { /* udp.srcport == bootps */
1716 net_checksum_calculate(buf
, size
, CSUM_UDP
);
1717 hdr
->flags
&= ~VIRTIO_NET_HDR_F_NEEDS_CSUM
;
1721 static void receive_header(VirtIONet
*n
, const struct iovec
*iov
, int iov_cnt
,
1722 const void *buf
, size_t size
)
1724 if (n
->has_vnet_hdr
) {
1725 /* FIXME this cast is evil */
1726 void *wbuf
= (void *)buf
;
1727 work_around_broken_dhclient(wbuf
, wbuf
+ n
->host_hdr_len
,
1728 size
- n
->host_hdr_len
);
1730 if (n
->needs_vnet_hdr_swap
) {
1731 virtio_net_hdr_swap(VIRTIO_DEVICE(n
), wbuf
);
1733 iov_from_buf(iov
, iov_cnt
, 0, buf
, sizeof(struct virtio_net_hdr
));
1735 struct virtio_net_hdr hdr
= {
1737 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
1739 iov_from_buf(iov
, iov_cnt
, 0, &hdr
, sizeof hdr
);
1743 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
1745 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1746 static const uint8_t vlan
[] = {0x81, 0x00};
1747 uint8_t *ptr
= (uint8_t *)buf
;
1753 ptr
+= n
->host_hdr_len
;
1755 if (!memcmp(&ptr
[12], vlan
, sizeof(vlan
))) {
1756 int vid
= lduw_be_p(ptr
+ 14) & 0xfff;
1757 if (!(n
->vlans
[vid
>> 5] & (1U << (vid
& 0x1f))))
1761 if (ptr
[0] & 1) { // multicast
1762 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
1764 } else if (n
->nomulti
) {
1766 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
1770 for (i
= n
->mac_table
.first_multi
; i
< n
->mac_table
.in_use
; i
++) {
1771 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1778 } else if (n
->alluni
|| n
->mac_table
.uni_overflow
) {
1780 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
1784 for (i
= 0; i
< n
->mac_table
.first_multi
; i
++) {
1785 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
)) {
1794 static uint8_t virtio_net_get_hash_type(bool hasip4
,
1796 EthL4HdrProto l4hdr_proto
,
1800 switch (l4hdr_proto
) {
1801 case ETH_L4_HDR_PROTO_TCP
:
1802 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCPv4
) {
1803 return NetPktRssIpV4Tcp
;
1807 case ETH_L4_HDR_PROTO_UDP
:
1808 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDPv4
) {
1809 return NetPktRssIpV4Udp
;
1817 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IPv4
) {
1818 return NetPktRssIpV4
;
1820 } else if (hasip6
) {
1821 switch (l4hdr_proto
) {
1822 case ETH_L4_HDR_PROTO_TCP
:
1823 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCP_EX
) {
1824 return NetPktRssIpV6TcpEx
;
1826 if (types
& VIRTIO_NET_RSS_HASH_TYPE_TCPv6
) {
1827 return NetPktRssIpV6Tcp
;
1831 case ETH_L4_HDR_PROTO_UDP
:
1832 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDP_EX
) {
1833 return NetPktRssIpV6UdpEx
;
1835 if (types
& VIRTIO_NET_RSS_HASH_TYPE_UDPv6
) {
1836 return NetPktRssIpV6Udp
;
1844 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IP_EX
) {
1845 return NetPktRssIpV6Ex
;
1847 if (types
& VIRTIO_NET_RSS_HASH_TYPE_IPv6
) {
1848 return NetPktRssIpV6
;
1854 static int virtio_net_process_rss(NetClientState
*nc
, const uint8_t *buf
,
1856 struct virtio_net_hdr_v1_hash
*hdr
)
1858 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1859 unsigned int index
= nc
->queue_index
, new_index
= index
;
1860 struct NetRxPkt
*pkt
= n
->rx_pkt
;
1861 uint8_t net_hash_type
;
1863 bool hasip4
, hasip6
;
1864 EthL4HdrProto l4hdr_proto
;
1865 static const uint8_t reports
[NetPktRssIpV6UdpEx
+ 1] = {
1866 VIRTIO_NET_HASH_REPORT_IPv4
,
1867 VIRTIO_NET_HASH_REPORT_TCPv4
,
1868 VIRTIO_NET_HASH_REPORT_TCPv6
,
1869 VIRTIO_NET_HASH_REPORT_IPv6
,
1870 VIRTIO_NET_HASH_REPORT_IPv6_EX
,
1871 VIRTIO_NET_HASH_REPORT_TCPv6_EX
,
1872 VIRTIO_NET_HASH_REPORT_UDPv4
,
1873 VIRTIO_NET_HASH_REPORT_UDPv6
,
1874 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1876 struct iovec iov
= {
1877 .iov_base
= (void *)buf
,
1881 net_rx_pkt_set_protocols(pkt
, &iov
, 1, n
->host_hdr_len
);
1882 net_rx_pkt_get_protocols(pkt
, &hasip4
, &hasip6
, &l4hdr_proto
);
1883 net_hash_type
= virtio_net_get_hash_type(hasip4
, hasip6
, l4hdr_proto
,
1884 n
->rss_data
.hash_types
);
1885 if (net_hash_type
> NetPktRssIpV6UdpEx
) {
1886 if (n
->rss_data
.populate_hash
) {
1887 hdr
->hash_value
= VIRTIO_NET_HASH_REPORT_NONE
;
1888 hdr
->hash_report
= 0;
1890 return n
->rss_data
.redirect
? n
->rss_data
.default_queue
: -1;
1893 hash
= net_rx_pkt_calc_rss_hash(pkt
, net_hash_type
, n
->rss_data
.key
);
1895 if (n
->rss_data
.populate_hash
) {
1896 hdr
->hash_value
= hash
;
1897 hdr
->hash_report
= reports
[net_hash_type
];
1900 if (n
->rss_data
.redirect
) {
1901 new_index
= hash
& (n
->rss_data
.indirections_len
- 1);
1902 new_index
= n
->rss_data
.indirections_table
[new_index
];
1905 return (index
== new_index
) ? -1 : new_index
;
1908 static ssize_t
virtio_net_receive_rcu(NetClientState
*nc
, const uint8_t *buf
,
1911 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
1913 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
1914 QEMU_UNINITIALIZED VirtQueueElement
*elems
[VIRTQUEUE_MAX_SIZE
];
1915 QEMU_UNINITIALIZED
size_t lens
[VIRTQUEUE_MAX_SIZE
];
1916 QEMU_UNINITIALIZED
struct iovec mhdr_sg
[VIRTQUEUE_MAX_SIZE
];
1917 struct virtio_net_hdr_v1_hash extra_hdr
;
1918 unsigned mhdr_cnt
= 0;
1919 size_t offset
, i
, guest_offset
, j
;
1922 memset(&extra_hdr
, 0, sizeof(extra_hdr
));
1924 if (n
->rss_data
.enabled
&& n
->rss_data
.enabled_software_rss
) {
1925 int index
= virtio_net_process_rss(nc
, buf
, size
, &extra_hdr
);
1927 nc
= qemu_get_subqueue(n
->nic
, index
% n
->curr_queue_pairs
);
1931 if (!virtio_net_can_receive(nc
)) {
1935 q
= virtio_net_get_subqueue(nc
);
1937 /* hdr_len refers to the header we supply to the guest */
1938 if (!virtio_net_has_buffers(q
, size
+ n
->guest_hdr_len
- n
->host_hdr_len
)) {
1942 if (!receive_filter(n
, buf
, size
))
1947 while (offset
< size
) {
1948 VirtQueueElement
*elem
;
1950 const struct iovec
*sg
;
1954 if (i
== VIRTQUEUE_MAX_SIZE
) {
1955 virtio_error(vdev
, "virtio-net unexpected long buffer chain");
1960 elem
= virtqueue_pop(q
->rx_vq
, sizeof(VirtQueueElement
));
1963 virtio_error(vdev
, "virtio-net unexpected empty queue: "
1964 "i %zd mergeable %d offset %zd, size %zd, "
1965 "guest hdr len %zd, host hdr len %zd "
1966 "guest features 0x%" PRIx64
,
1967 i
, n
->mergeable_rx_bufs
, offset
, size
,
1968 n
->guest_hdr_len
, n
->host_hdr_len
,
1969 vdev
->guest_features
);
1975 if (elem
->in_num
< 1) {
1977 "virtio-net receive queue contains no in buffers");
1978 virtqueue_detach_element(q
->rx_vq
, elem
, 0);
1986 assert(offset
== 0);
1987 if (n
->mergeable_rx_bufs
) {
1988 mhdr_cnt
= iov_copy(mhdr_sg
, ARRAY_SIZE(mhdr_sg
),
1990 offsetof(typeof(extra_hdr
), hdr
.num_buffers
),
1991 sizeof(extra_hdr
.hdr
.num_buffers
));
1993 extra_hdr
.hdr
.num_buffers
= cpu_to_le16(1);
1996 receive_header(n
, sg
, elem
->in_num
, buf
, size
);
1997 if (n
->rss_data
.populate_hash
) {
1998 offset
= offsetof(typeof(extra_hdr
), hash_value
);
1999 iov_from_buf(sg
, elem
->in_num
, offset
,
2000 (char *)&extra_hdr
+ offset
,
2001 sizeof(extra_hdr
.hash_value
) +
2002 sizeof(extra_hdr
.hash_report
));
2004 offset
= n
->host_hdr_len
;
2005 total
+= n
->guest_hdr_len
;
2006 guest_offset
= n
->guest_hdr_len
;
2011 /* copy in packet. ugh */
2012 len
= iov_from_buf(sg
, elem
->in_num
, guest_offset
,
2013 buf
+ offset
, size
- offset
);
2016 /* If buffers can't be merged, at this point we
2017 * must have consumed the complete packet.
2018 * Otherwise, drop it. */
2019 if (!n
->mergeable_rx_bufs
&& offset
< size
) {
2020 virtqueue_unpop(q
->rx_vq
, elem
, total
);
2032 virtio_stw_p(vdev
, &extra_hdr
.hdr
.num_buffers
, i
);
2033 iov_from_buf(mhdr_sg
, mhdr_cnt
,
2035 &extra_hdr
.hdr
.num_buffers
,
2036 sizeof extra_hdr
.hdr
.num_buffers
);
2039 for (j
= 0; j
< i
; j
++) {
2040 /* signal other side */
2041 virtqueue_fill(q
->rx_vq
, elems
[j
], lens
[j
], j
);
2045 virtqueue_flush(q
->rx_vq
, i
);
2046 virtio_notify(vdev
, q
->rx_vq
);
2051 for (j
= 0; j
< i
; j
++) {
2052 virtqueue_detach_element(q
->rx_vq
, elems
[j
], lens
[j
]);
2059 static ssize_t
virtio_net_do_receive(NetClientState
*nc
, const uint8_t *buf
,
2062 RCU_READ_LOCK_GUARD();
2064 return virtio_net_receive_rcu(nc
, buf
, size
);
2068 * Accessors to read and write the IP packet data length field. This
2069 * is a potentially unaligned network-byte-order 16 bit unsigned integer
2070 * pointed to by unit->ip_len.
2072 static uint16_t read_unit_ip_len(VirtioNetRscUnit
*unit
)
2074 return lduw_be_p(unit
->ip_plen
);
2077 static void write_unit_ip_len(VirtioNetRscUnit
*unit
, uint16_t l
)
2079 stw_be_p(unit
->ip_plen
, l
);
2082 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain
*chain
,
2084 VirtioNetRscUnit
*unit
)
2087 struct ip_header
*ip
;
2089 ip
= (struct ip_header
*)(buf
+ chain
->n
->guest_hdr_len
2090 + sizeof(struct eth_header
));
2091 unit
->ip
= (void *)ip
;
2092 ip_hdrlen
= (ip
->ip_ver_len
& 0xF) << 2;
2093 unit
->ip_plen
= &ip
->ip_len
;
2094 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
) + ip_hdrlen
);
2095 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
2096 unit
->payload
= read_unit_ip_len(unit
) - ip_hdrlen
- unit
->tcp_hdrlen
;
2099 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain
*chain
,
2101 VirtioNetRscUnit
*unit
)
2103 struct ip6_header
*ip6
;
2105 ip6
= (struct ip6_header
*)(buf
+ chain
->n
->guest_hdr_len
2106 + sizeof(struct eth_header
));
2108 unit
->ip_plen
= &(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
2109 unit
->tcp
= (struct tcp_header
*)(((uint8_t *)unit
->ip
)
2110 + sizeof(struct ip6_header
));
2111 unit
->tcp_hdrlen
= (htons(unit
->tcp
->th_offset_flags
) & 0xF000) >> 10;
2113 /* There is a difference between payload length in ipv4 and v6,
2114 ip header is excluded in ipv6 */
2115 unit
->payload
= read_unit_ip_len(unit
) - unit
->tcp_hdrlen
;
2118 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain
*chain
,
2119 VirtioNetRscSeg
*seg
)
2122 struct virtio_net_hdr_v1
*h
;
2124 h
= (struct virtio_net_hdr_v1
*)seg
->buf
;
2126 h
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
2128 if (seg
->is_coalesced
) {
2129 h
->rsc
.segments
= seg
->packets
;
2130 h
->rsc
.dup_acks
= seg
->dup_ack
;
2131 h
->flags
= VIRTIO_NET_HDR_F_RSC_INFO
;
2132 if (chain
->proto
== ETH_P_IP
) {
2133 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2135 h
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2139 ret
= virtio_net_do_receive(seg
->nc
, seg
->buf
, seg
->size
);
2140 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
2147 static void virtio_net_rsc_purge(void *opq
)
2149 VirtioNetRscSeg
*seg
, *rn
;
2150 VirtioNetRscChain
*chain
= (VirtioNetRscChain
*)opq
;
2152 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn
) {
2153 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2154 chain
->stat
.purge_failed
++;
2159 chain
->stat
.timer
++;
2160 if (!QTAILQ_EMPTY(&chain
->buffers
)) {
2161 timer_mod(chain
->drain_timer
,
2162 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + chain
->n
->rsc_timeout
);
2166 static void virtio_net_rsc_cleanup(VirtIONet
*n
)
2168 VirtioNetRscChain
*chain
, *rn_chain
;
2169 VirtioNetRscSeg
*seg
, *rn_seg
;
2171 QTAILQ_FOREACH_SAFE(chain
, &n
->rsc_chains
, next
, rn_chain
) {
2172 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, rn_seg
) {
2173 QTAILQ_REMOVE(&chain
->buffers
, seg
, next
);
2178 timer_free(chain
->drain_timer
);
2179 QTAILQ_REMOVE(&n
->rsc_chains
, chain
, next
);
2184 static void virtio_net_rsc_cache_buf(VirtioNetRscChain
*chain
,
2186 const uint8_t *buf
, size_t size
)
2189 VirtioNetRscSeg
*seg
;
2191 hdr_len
= chain
->n
->guest_hdr_len
;
2192 seg
= g_new(VirtioNetRscSeg
, 1);
2193 seg
->buf
= g_malloc(hdr_len
+ sizeof(struct eth_header
)
2194 + sizeof(struct ip6_header
) + VIRTIO_NET_MAX_TCP_PAYLOAD
);
2195 memcpy(seg
->buf
, buf
, size
);
2199 seg
->is_coalesced
= 0;
2202 QTAILQ_INSERT_TAIL(&chain
->buffers
, seg
, next
);
2203 chain
->stat
.cache
++;
2205 switch (chain
->proto
) {
2207 virtio_net_rsc_extract_unit4(chain
, seg
->buf
, &seg
->unit
);
2210 virtio_net_rsc_extract_unit6(chain
, seg
->buf
, &seg
->unit
);
2213 g_assert_not_reached();
2217 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain
*chain
,
2218 VirtioNetRscSeg
*seg
,
2220 struct tcp_header
*n_tcp
,
2221 struct tcp_header
*o_tcp
)
2223 uint32_t nack
, oack
;
2224 uint16_t nwin
, owin
;
2226 nack
= htonl(n_tcp
->th_ack
);
2227 nwin
= htons(n_tcp
->th_win
);
2228 oack
= htonl(o_tcp
->th_ack
);
2229 owin
= htons(o_tcp
->th_win
);
2231 if ((nack
- oack
) >= VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2232 chain
->stat
.ack_out_of_win
++;
2234 } else if (nack
== oack
) {
2235 /* duplicated ack or window probe */
2237 /* duplicated ack, add dup ack count due to whql test up to 1 */
2238 chain
->stat
.dup_ack
++;
2241 /* Coalesce window update */
2242 o_tcp
->th_win
= n_tcp
->th_win
;
2243 chain
->stat
.win_update
++;
2244 return RSC_COALESCE
;
2247 /* pure ack, go to 'C', finalize*/
2248 chain
->stat
.pure_ack
++;
2253 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain
*chain
,
2254 VirtioNetRscSeg
*seg
,
2256 VirtioNetRscUnit
*n_unit
)
2260 uint32_t nseq
, oseq
;
2261 VirtioNetRscUnit
*o_unit
;
2263 o_unit
= &seg
->unit
;
2264 o_ip_len
= read_unit_ip_len(o_unit
);
2265 nseq
= htonl(n_unit
->tcp
->th_seq
);
2266 oseq
= htonl(o_unit
->tcp
->th_seq
);
2268 /* out of order or retransmitted. */
2269 if ((nseq
- oseq
) > VIRTIO_NET_MAX_TCP_PAYLOAD
) {
2270 chain
->stat
.data_out_of_win
++;
2274 data
= ((uint8_t *)n_unit
->tcp
) + n_unit
->tcp_hdrlen
;
2276 if ((o_unit
->payload
== 0) && n_unit
->payload
) {
2277 /* From no payload to payload, normal case, not a dup ack or etc */
2278 chain
->stat
.data_after_pure_ack
++;
2281 return virtio_net_rsc_handle_ack(chain
, seg
, buf
,
2282 n_unit
->tcp
, o_unit
->tcp
);
2284 } else if ((nseq
- oseq
) != o_unit
->payload
) {
2285 /* Not a consistent packet, out of order */
2286 chain
->stat
.data_out_of_order
++;
2290 if ((o_ip_len
+ n_unit
->payload
) > chain
->max_payload
) {
2291 chain
->stat
.over_size
++;
2295 /* Here comes the right data, the payload length in v4/v6 is different,
2296 so use the field value to update and record the new data len */
2297 o_unit
->payload
+= n_unit
->payload
; /* update new data len */
2299 /* update field in ip header */
2300 write_unit_ip_len(o_unit
, o_ip_len
+ n_unit
->payload
);
2302 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
2303 for windows guest, while this may change the behavior for linux
2304 guest (only if it uses RSC feature). */
2305 o_unit
->tcp
->th_offset_flags
= n_unit
->tcp
->th_offset_flags
;
2307 o_unit
->tcp
->th_ack
= n_unit
->tcp
->th_ack
;
2308 o_unit
->tcp
->th_win
= n_unit
->tcp
->th_win
;
2310 memmove(seg
->buf
+ seg
->size
, data
, n_unit
->payload
);
2311 seg
->size
+= n_unit
->payload
;
2313 chain
->stat
.coalesced
++;
2314 return RSC_COALESCE
;
2318 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain
*chain
,
2319 VirtioNetRscSeg
*seg
,
2320 const uint8_t *buf
, size_t size
,
2321 VirtioNetRscUnit
*unit
)
2323 struct ip_header
*ip1
, *ip2
;
2325 ip1
= (struct ip_header
*)(unit
->ip
);
2326 ip2
= (struct ip_header
*)(seg
->unit
.ip
);
2327 if ((ip1
->ip_src
^ ip2
->ip_src
) || (ip1
->ip_dst
^ ip2
->ip_dst
)
2328 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2329 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2330 chain
->stat
.no_match
++;
2331 return RSC_NO_MATCH
;
2334 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2337 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain
*chain
,
2338 VirtioNetRscSeg
*seg
,
2339 const uint8_t *buf
, size_t size
,
2340 VirtioNetRscUnit
*unit
)
2342 struct ip6_header
*ip1
, *ip2
;
2344 ip1
= (struct ip6_header
*)(unit
->ip
);
2345 ip2
= (struct ip6_header
*)(seg
->unit
.ip
);
2346 if (memcmp(&ip1
->ip6_src
, &ip2
->ip6_src
, sizeof(struct in6_address
))
2347 || memcmp(&ip1
->ip6_dst
, &ip2
->ip6_dst
, sizeof(struct in6_address
))
2348 || (unit
->tcp
->th_sport
^ seg
->unit
.tcp
->th_sport
)
2349 || (unit
->tcp
->th_dport
^ seg
->unit
.tcp
->th_dport
)) {
2350 chain
->stat
.no_match
++;
2351 return RSC_NO_MATCH
;
2354 return virtio_net_rsc_coalesce_data(chain
, seg
, buf
, unit
);
2357 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2358 * to prevent out of order */
2359 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain
*chain
,
2360 struct tcp_header
*tcp
)
2365 tcp_flag
= htons(tcp
->th_offset_flags
);
2366 tcp_hdr
= (tcp_flag
& VIRTIO_NET_TCP_HDR_LENGTH
) >> 10;
2367 tcp_flag
&= VIRTIO_NET_TCP_FLAG
;
2368 if (tcp_flag
& TH_SYN
) {
2369 chain
->stat
.tcp_syn
++;
2373 if (tcp_flag
& (TH_FIN
| TH_URG
| TH_RST
| TH_ECE
| TH_CWR
)) {
2374 chain
->stat
.tcp_ctrl_drain
++;
2378 if (tcp_hdr
> sizeof(struct tcp_header
)) {
2379 chain
->stat
.tcp_all_opt
++;
2383 return RSC_CANDIDATE
;
2386 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain
*chain
,
2388 const uint8_t *buf
, size_t size
,
2389 VirtioNetRscUnit
*unit
)
2392 VirtioNetRscSeg
*seg
, *nseg
;
2394 if (QTAILQ_EMPTY(&chain
->buffers
)) {
2395 chain
->stat
.empty_cache
++;
2396 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2397 timer_mod(chain
->drain_timer
,
2398 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + chain
->n
->rsc_timeout
);
2402 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2403 if (chain
->proto
== ETH_P_IP
) {
2404 ret
= virtio_net_rsc_coalesce4(chain
, seg
, buf
, size
, unit
);
2406 ret
= virtio_net_rsc_coalesce6(chain
, seg
, buf
, size
, unit
);
2409 if (ret
== RSC_FINAL
) {
2410 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2412 chain
->stat
.final_failed
++;
2416 /* Send current packet */
2417 return virtio_net_do_receive(nc
, buf
, size
);
2418 } else if (ret
== RSC_NO_MATCH
) {
2421 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2422 seg
->is_coalesced
= 1;
2427 chain
->stat
.no_match_cache
++;
2428 virtio_net_rsc_cache_buf(chain
, nc
, buf
, size
);
2432 /* Drain a connection data, this is to avoid out of order segments */
2433 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain
*chain
,
2435 const uint8_t *buf
, size_t size
,
2436 uint16_t ip_start
, uint16_t ip_size
,
2439 VirtioNetRscSeg
*seg
, *nseg
;
2440 uint32_t ppair1
, ppair2
;
2442 ppair1
= *(uint32_t *)(buf
+ tcp_port
);
2443 QTAILQ_FOREACH_SAFE(seg
, &chain
->buffers
, next
, nseg
) {
2444 ppair2
= *(uint32_t *)(seg
->buf
+ tcp_port
);
2445 if (memcmp(buf
+ ip_start
, seg
->buf
+ ip_start
, ip_size
)
2446 || (ppair1
!= ppair2
)) {
2449 if (virtio_net_rsc_drain_seg(chain
, seg
) == 0) {
2450 chain
->stat
.drain_failed
++;
2456 return virtio_net_do_receive(nc
, buf
, size
);
2459 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain
*chain
,
2460 struct ip_header
*ip
,
2461 const uint8_t *buf
, size_t size
)
2465 /* Not an ipv4 packet */
2466 if (((ip
->ip_ver_len
& 0xF0) >> 4) != IP_HEADER_VERSION_4
) {
2467 chain
->stat
.ip_option
++;
2471 /* Don't handle packets with ip option */
2472 if ((ip
->ip_ver_len
& 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH
) {
2473 chain
->stat
.ip_option
++;
2477 if (ip
->ip_p
!= IPPROTO_TCP
) {
2478 chain
->stat
.bypass_not_tcp
++;
2482 /* Don't handle packets with ip fragment */
2483 if (!(htons(ip
->ip_off
) & IP_DF
)) {
2484 chain
->stat
.ip_frag
++;
2488 /* Don't handle packets with ecn flag */
2489 if (IPTOS_ECN(ip
->ip_tos
)) {
2490 chain
->stat
.ip_ecn
++;
2494 ip_len
= htons(ip
->ip_len
);
2495 if (ip_len
< (sizeof(struct ip_header
) + sizeof(struct tcp_header
))
2496 || ip_len
> (size
- chain
->n
->guest_hdr_len
-
2497 sizeof(struct eth_header
))) {
2498 chain
->stat
.ip_hacked
++;
2502 return RSC_CANDIDATE
;
2505 static size_t virtio_net_rsc_receive4(VirtioNetRscChain
*chain
,
2507 const uint8_t *buf
, size_t size
)
2511 VirtioNetRscUnit unit
;
2513 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2515 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
)
2516 + sizeof(struct tcp_header
))) {
2517 chain
->stat
.bypass_not_tcp
++;
2518 return virtio_net_do_receive(nc
, buf
, size
);
2521 virtio_net_rsc_extract_unit4(chain
, buf
, &unit
);
2522 if (virtio_net_rsc_sanity_check4(chain
, unit
.ip
, buf
, size
)
2524 return virtio_net_do_receive(nc
, buf
, size
);
2527 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2528 if (ret
== RSC_BYPASS
) {
2529 return virtio_net_do_receive(nc
, buf
, size
);
2530 } else if (ret
== RSC_FINAL
) {
2531 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2532 ((hdr_len
+ sizeof(struct eth_header
)) + 12),
2533 VIRTIO_NET_IP4_ADDR_SIZE
,
2534 hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip_header
));
2537 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2540 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain
*chain
,
2541 struct ip6_header
*ip6
,
2542 const uint8_t *buf
, size_t size
)
2546 if (((ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_flow
& 0xF0) >> 4)
2547 != IP_HEADER_VERSION_6
) {
2551 /* Both option and protocol is checked in this */
2552 if (ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_nxt
!= IPPROTO_TCP
) {
2553 chain
->stat
.bypass_not_tcp
++;
2557 ip_len
= htons(ip6
->ip6_ctlun
.ip6_un1
.ip6_un1_plen
);
2558 if (ip_len
< sizeof(struct tcp_header
) ||
2559 ip_len
> (size
- chain
->n
->guest_hdr_len
- sizeof(struct eth_header
)
2560 - sizeof(struct ip6_header
))) {
2561 chain
->stat
.ip_hacked
++;
2565 /* Don't handle packets with ecn flag */
2566 if (IP6_ECN(ip6
->ip6_ctlun
.ip6_un3
.ip6_un3_ecn
)) {
2567 chain
->stat
.ip_ecn
++;
2571 return RSC_CANDIDATE
;
2574 static size_t virtio_net_rsc_receive6(void *opq
, NetClientState
*nc
,
2575 const uint8_t *buf
, size_t size
)
2579 VirtioNetRscChain
*chain
;
2580 VirtioNetRscUnit unit
;
2583 hdr_len
= ((VirtIONet
*)(chain
->n
))->guest_hdr_len
;
2585 if (size
< (hdr_len
+ sizeof(struct eth_header
) + sizeof(struct ip6_header
)
2586 + sizeof(tcp_header
))) {
2587 return virtio_net_do_receive(nc
, buf
, size
);
2590 virtio_net_rsc_extract_unit6(chain
, buf
, &unit
);
2591 if (RSC_CANDIDATE
!= virtio_net_rsc_sanity_check6(chain
,
2592 unit
.ip
, buf
, size
)) {
2593 return virtio_net_do_receive(nc
, buf
, size
);
2596 ret
= virtio_net_rsc_tcp_ctrl_check(chain
, unit
.tcp
);
2597 if (ret
== RSC_BYPASS
) {
2598 return virtio_net_do_receive(nc
, buf
, size
);
2599 } else if (ret
== RSC_FINAL
) {
2600 return virtio_net_rsc_drain_flow(chain
, nc
, buf
, size
,
2601 ((hdr_len
+ sizeof(struct eth_header
)) + 8),
2602 VIRTIO_NET_IP6_ADDR_SIZE
,
2603 hdr_len
+ sizeof(struct eth_header
)
2604 + sizeof(struct ip6_header
));
2607 return virtio_net_rsc_do_coalesce(chain
, nc
, buf
, size
, &unit
);
2610 static VirtioNetRscChain
*virtio_net_rsc_lookup_chain(VirtIONet
*n
,
2614 VirtioNetRscChain
*chain
;
2616 if ((proto
!= (uint16_t)ETH_P_IP
) && (proto
!= (uint16_t)ETH_P_IPV6
)) {
2620 QTAILQ_FOREACH(chain
, &n
->rsc_chains
, next
) {
2621 if (chain
->proto
== proto
) {
2626 chain
= g_malloc(sizeof(*chain
));
2628 chain
->proto
= proto
;
2629 if (proto
== (uint16_t)ETH_P_IP
) {
2630 chain
->max_payload
= VIRTIO_NET_MAX_IP4_PAYLOAD
;
2631 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
2633 chain
->max_payload
= VIRTIO_NET_MAX_IP6_PAYLOAD
;
2634 chain
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
2636 chain
->drain_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2637 virtio_net_rsc_purge
, chain
);
2638 memset(&chain
->stat
, 0, sizeof(chain
->stat
));
2640 QTAILQ_INIT(&chain
->buffers
);
2641 QTAILQ_INSERT_TAIL(&n
->rsc_chains
, chain
, next
);
2646 static ssize_t
virtio_net_rsc_receive(NetClientState
*nc
,
2651 VirtioNetRscChain
*chain
;
2652 struct eth_header
*eth
;
2655 n
= qemu_get_nic_opaque(nc
);
2656 if (size
< (n
->host_hdr_len
+ sizeof(struct eth_header
))) {
2657 return virtio_net_do_receive(nc
, buf
, size
);
2660 eth
= (struct eth_header
*)(buf
+ n
->guest_hdr_len
);
2661 proto
= htons(eth
->h_proto
);
2663 chain
= virtio_net_rsc_lookup_chain(n
, nc
, proto
);
2665 chain
->stat
.received
++;
2666 if (proto
== (uint16_t)ETH_P_IP
&& n
->rsc4_enabled
) {
2667 return virtio_net_rsc_receive4(chain
, nc
, buf
, size
);
2668 } else if (proto
== (uint16_t)ETH_P_IPV6
&& n
->rsc6_enabled
) {
2669 return virtio_net_rsc_receive6(chain
, nc
, buf
, size
);
2672 return virtio_net_do_receive(nc
, buf
, size
);
2675 static ssize_t
virtio_net_receive(NetClientState
*nc
, const uint8_t *buf
,
2678 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2679 if ((n
->rsc4_enabled
|| n
->rsc6_enabled
)) {
2680 return virtio_net_rsc_receive(nc
, buf
, size
);
2682 return virtio_net_do_receive(nc
, buf
, size
);
2686 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
);
2688 static void virtio_net_tx_complete(NetClientState
*nc
, ssize_t len
)
2690 VirtIONet
*n
= qemu_get_nic_opaque(nc
);
2691 VirtIONetQueue
*q
= virtio_net_get_subqueue(nc
);
2692 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2695 virtqueue_push(q
->tx_vq
, q
->async_tx
.elem
, 0);
2696 virtio_notify(vdev
, q
->tx_vq
);
2698 g_free(q
->async_tx
.elem
);
2699 q
->async_tx
.elem
= NULL
;
2701 virtio_queue_set_notification(q
->tx_vq
, 1);
2702 ret
= virtio_net_flush_tx(q
);
2703 if (ret
>= n
->tx_burst
) {
2705 * the flush has been stopped by tx_burst
2706 * we will not receive notification for the
2707 * remainining part, so re-schedule
2709 virtio_queue_set_notification(q
->tx_vq
, 0);
2711 replay_bh_schedule_event(q
->tx_bh
);
2713 timer_mod(q
->tx_timer
,
2714 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2721 static int32_t virtio_net_flush_tx(VirtIONetQueue
*q
)
2723 VirtIONet
*n
= q
->n
;
2724 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2725 VirtQueueElement
*elem
;
2726 int32_t num_packets
= 0;
2727 int queue_index
= vq2q(virtio_get_queue_index(q
->tx_vq
));
2728 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2732 if (q
->async_tx
.elem
) {
2733 virtio_queue_set_notification(q
->tx_vq
, 0);
2739 unsigned int out_num
;
2740 struct iovec sg
[VIRTQUEUE_MAX_SIZE
], sg2
[VIRTQUEUE_MAX_SIZE
+ 1], *out_sg
;
2741 struct virtio_net_hdr vhdr
;
2743 elem
= virtqueue_pop(q
->tx_vq
, sizeof(VirtQueueElement
));
2748 out_num
= elem
->out_num
;
2749 out_sg
= elem
->out_sg
;
2751 virtio_error(vdev
, "virtio-net header not in first element");
2755 if (n
->needs_vnet_hdr_swap
) {
2756 if (iov_to_buf(out_sg
, out_num
, 0, &vhdr
, sizeof(vhdr
)) <
2758 virtio_error(vdev
, "virtio-net header incorrect");
2761 virtio_net_hdr_swap(vdev
, &vhdr
);
2762 sg2
[0].iov_base
= &vhdr
;
2763 sg2
[0].iov_len
= sizeof(vhdr
);
2764 out_num
= iov_copy(&sg2
[1], ARRAY_SIZE(sg2
) - 1, out_sg
, out_num
,
2766 if (out_num
== VIRTQUEUE_MAX_SIZE
) {
2773 * If host wants to see the guest header as is, we can
2774 * pass it on unchanged. Otherwise, copy just the parts
2775 * that host is interested in.
2777 assert(n
->host_hdr_len
<= n
->guest_hdr_len
);
2778 if (n
->host_hdr_len
!= n
->guest_hdr_len
) {
2779 if (iov_size(out_sg
, out_num
) < n
->guest_hdr_len
) {
2780 virtio_error(vdev
, "virtio-net header is invalid");
2783 unsigned sg_num
= iov_copy(sg
, ARRAY_SIZE(sg
),
2785 0, n
->host_hdr_len
);
2786 sg_num
+= iov_copy(sg
+ sg_num
, ARRAY_SIZE(sg
) - sg_num
,
2788 n
->guest_hdr_len
, -1);
2793 virtio_error(vdev
, "virtio-net nothing to send");
2798 ret
= qemu_sendv_packet_async(qemu_get_subqueue(n
->nic
, queue_index
),
2799 out_sg
, out_num
, virtio_net_tx_complete
);
2801 virtio_queue_set_notification(q
->tx_vq
, 0);
2802 q
->async_tx
.elem
= elem
;
2807 virtqueue_push(q
->tx_vq
, elem
, 0);
2808 virtio_notify(vdev
, q
->tx_vq
);
2811 if (++num_packets
>= n
->tx_burst
) {
2818 virtqueue_detach_element(q
->tx_vq
, elem
, 0);
2823 static void virtio_net_tx_timer(void *opaque
);
2825 static void virtio_net_handle_tx_timer(VirtIODevice
*vdev
, VirtQueue
*vq
)
2827 VirtIONet
*n
= VIRTIO_NET(vdev
);
2828 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2830 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2831 virtio_net_drop_tx_queue_data(vdev
, vq
);
2835 /* This happens when device was stopped but VCPU wasn't. */
2836 if (!vdev
->vm_running
) {
2841 if (q
->tx_waiting
) {
2842 /* We already have queued packets, immediately flush */
2843 timer_del(q
->tx_timer
);
2844 virtio_net_tx_timer(q
);
2846 /* re-arm timer to flush it (and more) on next tick */
2847 timer_mod(q
->tx_timer
,
2848 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2850 virtio_queue_set_notification(vq
, 0);
2854 static void virtio_net_handle_tx_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
2856 VirtIONet
*n
= VIRTIO_NET(vdev
);
2857 VirtIONetQueue
*q
= &n
->vqs
[vq2q(virtio_get_queue_index(vq
))];
2859 if (unlikely(n
->vhost_started
)) {
2863 if (unlikely((n
->status
& VIRTIO_NET_S_LINK_UP
) == 0)) {
2864 virtio_net_drop_tx_queue_data(vdev
, vq
);
2868 if (unlikely(q
->tx_waiting
)) {
2872 /* This happens when device was stopped but VCPU wasn't. */
2873 if (!vdev
->vm_running
) {
2876 virtio_queue_set_notification(vq
, 0);
2877 replay_bh_schedule_event(q
->tx_bh
);
2880 static void virtio_net_tx_timer(void *opaque
)
2882 VirtIONetQueue
*q
= opaque
;
2883 VirtIONet
*n
= q
->n
;
2884 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2887 /* This happens when device was stopped but BH wasn't. */
2888 if (!vdev
->vm_running
) {
2889 /* Make sure tx waiting is set, so we'll run when restarted. */
2890 assert(q
->tx_waiting
);
2896 /* Just in case the driver is not ready on more */
2897 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2901 ret
= virtio_net_flush_tx(q
);
2902 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2906 * If we flush a full burst of packets, assume there are
2907 * more coming and immediately rearm
2909 if (ret
>= n
->tx_burst
) {
2911 timer_mod(q
->tx_timer
,
2912 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2916 * If less than a full burst, re-enable notification and flush
2917 * anything that may have come in while we weren't looking. If
2918 * we find something, assume the guest is still active and rearm
2920 virtio_queue_set_notification(q
->tx_vq
, 1);
2921 ret
= virtio_net_flush_tx(q
);
2923 virtio_queue_set_notification(q
->tx_vq
, 0);
2925 timer_mod(q
->tx_timer
,
2926 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + n
->tx_timeout
);
2930 static void virtio_net_tx_bh(void *opaque
)
2932 VirtIONetQueue
*q
= opaque
;
2933 VirtIONet
*n
= q
->n
;
2934 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2937 /* This happens when device was stopped but BH wasn't. */
2938 if (!vdev
->vm_running
) {
2939 /* Make sure tx waiting is set, so we'll run when restarted. */
2940 assert(q
->tx_waiting
);
2946 /* Just in case the driver is not ready on more */
2947 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
2951 ret
= virtio_net_flush_tx(q
);
2952 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2953 return; /* Notification re-enable handled by tx_complete or device
2957 /* If we flush a full burst of packets, assume there are
2958 * more coming and immediately reschedule */
2959 if (ret
>= n
->tx_burst
) {
2960 replay_bh_schedule_event(q
->tx_bh
);
2965 /* If less than a full burst, re-enable notification and flush
2966 * anything that may have come in while we weren't looking. If
2967 * we find something, assume the guest is still active and reschedule */
2968 virtio_queue_set_notification(q
->tx_vq
, 1);
2969 ret
= virtio_net_flush_tx(q
);
2970 if (ret
== -EINVAL
) {
2972 } else if (ret
> 0) {
2973 virtio_queue_set_notification(q
->tx_vq
, 0);
2974 replay_bh_schedule_event(q
->tx_bh
);
2979 static void virtio_net_add_queue(VirtIONet
*n
, int index
)
2981 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
2983 n
->vqs
[index
].rx_vq
= virtio_add_queue(vdev
, n
->net_conf
.rx_queue_size
,
2984 virtio_net_handle_rx
);
2986 if (n
->net_conf
.tx
&& !strcmp(n
->net_conf
.tx
, "timer")) {
2987 n
->vqs
[index
].tx_vq
=
2988 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2989 virtio_net_handle_tx_timer
);
2990 n
->vqs
[index
].tx_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2991 virtio_net_tx_timer
,
2994 n
->vqs
[index
].tx_vq
=
2995 virtio_add_queue(vdev
, n
->net_conf
.tx_queue_size
,
2996 virtio_net_handle_tx_bh
);
2997 n
->vqs
[index
].tx_bh
= qemu_bh_new_guarded(virtio_net_tx_bh
, &n
->vqs
[index
],
2998 &DEVICE(vdev
)->mem_reentrancy_guard
);
3001 n
->vqs
[index
].tx_waiting
= 0;
3002 n
->vqs
[index
].n
= n
;
3005 static void virtio_net_del_queue(VirtIONet
*n
, int index
)
3007 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3008 VirtIONetQueue
*q
= &n
->vqs
[index
];
3009 NetClientState
*nc
= qemu_get_subqueue(n
->nic
, index
);
3011 qemu_purge_queued_packets(nc
);
3013 virtio_del_queue(vdev
, index
* 2);
3015 timer_free(q
->tx_timer
);
3018 qemu_bh_delete(q
->tx_bh
);
3022 virtio_del_queue(vdev
, index
* 2 + 1);
3025 static void virtio_net_change_num_queue_pairs(VirtIONet
*n
, int new_max_queue_pairs
)
3027 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3028 int old_num_queues
= virtio_get_num_queues(vdev
);
3029 int new_num_queues
= new_max_queue_pairs
* 2 + 1;
3032 assert(old_num_queues
>= 3);
3033 assert(old_num_queues
% 2 == 1);
3035 if (old_num_queues
== new_num_queues
) {
3040 * We always need to remove and add ctrl vq if
3041 * old_num_queues != new_num_queues. Remove ctrl_vq first,
3042 * and then we only enter one of the following two loops.
3044 virtio_del_queue(vdev
, old_num_queues
- 1);
3046 for (i
= new_num_queues
- 1; i
< old_num_queues
- 1; i
+= 2) {
3047 /* new_num_queues < old_num_queues */
3048 virtio_net_del_queue(n
, i
/ 2);
3051 for (i
= old_num_queues
- 1; i
< new_num_queues
- 1; i
+= 2) {
3052 /* new_num_queues > old_num_queues */
3053 virtio_net_add_queue(n
, i
/ 2);
3056 /* add ctrl_vq last */
3057 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
3060 static void virtio_net_set_multiqueue(VirtIONet
*n
, int multiqueue
)
3062 int max
= multiqueue
? n
->max_queue_pairs
: 1;
3064 n
->multiqueue
= multiqueue
;
3065 virtio_net_change_num_queue_pairs(n
, max
);
3067 virtio_net_set_queue_pairs(n
);
3070 static int virtio_net_pre_load_queues(VirtIODevice
*vdev
)
3072 virtio_net_set_multiqueue(VIRTIO_NET(vdev
),
3073 virtio_has_feature(vdev
->guest_features
, VIRTIO_NET_F_RSS
) ||
3074 virtio_has_feature(vdev
->guest_features
, VIRTIO_NET_F_MQ
));
3079 static int virtio_net_post_load_device(void *opaque
, int version_id
)
3081 VirtIONet
*n
= opaque
;
3082 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3085 trace_virtio_net_post_load_device();
3086 virtio_net_set_mrg_rx_bufs(n
, n
->mergeable_rx_bufs
,
3087 virtio_vdev_has_feature(vdev
,
3088 VIRTIO_F_VERSION_1
),
3089 virtio_vdev_has_feature(vdev
,
3090 VIRTIO_NET_F_HASH_REPORT
));
3092 /* MAC_TABLE_ENTRIES may be different from the saved image */
3093 if (n
->mac_table
.in_use
> MAC_TABLE_ENTRIES
) {
3094 n
->mac_table
.in_use
= 0;
3097 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
3098 n
->curr_guest_offloads
= virtio_net_supported_guest_offloads(n
);
3102 * curr_guest_offloads will be later overwritten by the
3103 * virtio_set_features_nocheck call done from the virtio_load.
3104 * Here we make sure it is preserved and restored accordingly
3105 * in the virtio_net_post_load_virtio callback.
3107 n
->saved_guest_offloads
= n
->curr_guest_offloads
;
3109 virtio_net_set_queue_pairs(n
);
3111 /* Find the first multicast entry in the saved MAC filter */
3112 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
3113 if (n
->mac_table
.macs
[i
* ETH_ALEN
] & 1) {
3117 n
->mac_table
.first_multi
= i
;
3119 /* nc.link_down can't be migrated, so infer link_down according
3120 * to link status bit in n->status */
3121 link_down
= (n
->status
& VIRTIO_NET_S_LINK_UP
) == 0;
3122 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3123 qemu_get_subqueue(n
->nic
, i
)->link_down
= link_down
;
3126 if (virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_GUEST_ANNOUNCE
) &&
3127 virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3128 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
3130 virtio_net_announce_timer
, n
);
3131 if (n
->announce_timer
.round
) {
3132 timer_mod(n
->announce_timer
.tm
,
3133 qemu_clock_get_ms(n
->announce_timer
.type
));
3135 qemu_announce_timer_del(&n
->announce_timer
, false);
3139 virtio_net_commit_rss_config(n
);
3143 static int virtio_net_post_load_virtio(VirtIODevice
*vdev
)
3145 VirtIONet
*n
= VIRTIO_NET(vdev
);
3147 * The actual needed state is now in saved_guest_offloads,
3148 * see virtio_net_post_load_device for detail.
3149 * Restore it back and apply the desired offloads.
3151 n
->curr_guest_offloads
= n
->saved_guest_offloads
;
3152 if (peer_has_vnet_hdr(n
)) {
3153 virtio_net_apply_guest_offloads(n
);
3159 /* tx_waiting field of a VirtIONetQueue */
3160 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting
= {
3161 .name
= "virtio-net-queue-tx_waiting",
3162 .fields
= (const VMStateField
[]) {
3163 VMSTATE_UINT32(tx_waiting
, VirtIONetQueue
),
3164 VMSTATE_END_OF_LIST()
3168 static bool max_queue_pairs_gt_1(void *opaque
, int version_id
)
3170 return VIRTIO_NET(opaque
)->max_queue_pairs
> 1;
3173 static bool has_ctrl_guest_offloads(void *opaque
, int version_id
)
3175 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque
),
3176 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
);
3179 static bool mac_table_fits(void *opaque
, int version_id
)
3181 return VIRTIO_NET(opaque
)->mac_table
.in_use
<= MAC_TABLE_ENTRIES
;
3184 static bool mac_table_doesnt_fit(void *opaque
, int version_id
)
3186 return !mac_table_fits(opaque
, version_id
);
3189 /* This temporary type is shared by all the WITH_TMP methods
3190 * although only some fields are used by each.
3192 struct VirtIONetMigTmp
{
3194 VirtIONetQueue
*vqs_1
;
3195 uint16_t curr_queue_pairs_1
;
3197 uint32_t has_vnet_hdr
;
3200 /* The 2nd and subsequent tx_waiting flags are loaded later than
3201 * the 1st entry in the queue_pairs and only if there's more than one
3202 * entry. We use the tmp mechanism to calculate a temporary
3203 * pointer and count and also validate the count.
3206 static int virtio_net_tx_waiting_pre_save(void *opaque
)
3208 struct VirtIONetMigTmp
*tmp
= opaque
;
3210 tmp
->vqs_1
= tmp
->parent
->vqs
+ 1;
3211 tmp
->curr_queue_pairs_1
= tmp
->parent
->curr_queue_pairs
- 1;
3212 if (tmp
->parent
->curr_queue_pairs
== 0) {
3213 tmp
->curr_queue_pairs_1
= 0;
3219 static int virtio_net_tx_waiting_pre_load(void *opaque
)
3221 struct VirtIONetMigTmp
*tmp
= opaque
;
3223 /* Reuse the pointer setup from save */
3224 virtio_net_tx_waiting_pre_save(opaque
);
3226 if (tmp
->parent
->curr_queue_pairs
> tmp
->parent
->max_queue_pairs
) {
3227 error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3228 tmp
->parent
->curr_queue_pairs
, tmp
->parent
->max_queue_pairs
);
3233 return 0; /* all good */
3236 static const VMStateDescription vmstate_virtio_net_tx_waiting
= {
3237 .name
= "virtio-net-tx_waiting",
3238 .pre_load
= virtio_net_tx_waiting_pre_load
,
3239 .pre_save
= virtio_net_tx_waiting_pre_save
,
3240 .fields
= (const VMStateField
[]) {
3241 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1
, struct VirtIONetMigTmp
,
3243 vmstate_virtio_net_queue_tx_waiting
,
3244 struct VirtIONetQueue
),
3245 VMSTATE_END_OF_LIST()
3249 /* the 'has_ufo' flag is just tested; if the incoming stream has the
3250 * flag set we need to check that we have it
3252 static int virtio_net_ufo_post_load(void *opaque
, int version_id
)
3254 struct VirtIONetMigTmp
*tmp
= opaque
;
3256 if (tmp
->has_ufo
&& !peer_has_ufo(tmp
->parent
)) {
3257 error_report("virtio-net: saved image requires TUN_F_UFO support");
3264 static int virtio_net_ufo_pre_save(void *opaque
)
3266 struct VirtIONetMigTmp
*tmp
= opaque
;
3268 tmp
->has_ufo
= tmp
->parent
->has_ufo
;
3273 static const VMStateDescription vmstate_virtio_net_has_ufo
= {
3274 .name
= "virtio-net-ufo",
3275 .post_load
= virtio_net_ufo_post_load
,
3276 .pre_save
= virtio_net_ufo_pre_save
,
3277 .fields
= (const VMStateField
[]) {
3278 VMSTATE_UINT8(has_ufo
, struct VirtIONetMigTmp
),
3279 VMSTATE_END_OF_LIST()
3283 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3284 * flag set we need to check that we have it
3286 static int virtio_net_vnet_post_load(void *opaque
, int version_id
)
3288 struct VirtIONetMigTmp
*tmp
= opaque
;
3290 if (tmp
->has_vnet_hdr
&& !peer_has_vnet_hdr(tmp
->parent
)) {
3291 error_report("virtio-net: saved image requires vnet_hdr=on");
3298 static int virtio_net_vnet_pre_save(void *opaque
)
3300 struct VirtIONetMigTmp
*tmp
= opaque
;
3302 tmp
->has_vnet_hdr
= tmp
->parent
->has_vnet_hdr
;
3307 static const VMStateDescription vmstate_virtio_net_has_vnet
= {
3308 .name
= "virtio-net-vnet",
3309 .post_load
= virtio_net_vnet_post_load
,
3310 .pre_save
= virtio_net_vnet_pre_save
,
3311 .fields
= (const VMStateField
[]) {
3312 VMSTATE_UINT32(has_vnet_hdr
, struct VirtIONetMigTmp
),
3313 VMSTATE_END_OF_LIST()
3317 static bool virtio_net_rss_needed(void *opaque
)
3319 return VIRTIO_NET(opaque
)->rss_data
.enabled
;
3322 static const VMStateDescription vmstate_virtio_net_rss
= {
3323 .name
= "virtio-net-device/rss",
3325 .minimum_version_id
= 1,
3326 .needed
= virtio_net_rss_needed
,
3327 .fields
= (const VMStateField
[]) {
3328 VMSTATE_BOOL(rss_data
.enabled
, VirtIONet
),
3329 VMSTATE_BOOL(rss_data
.redirect
, VirtIONet
),
3330 VMSTATE_BOOL(rss_data
.populate_hash
, VirtIONet
),
3331 VMSTATE_UINT32(rss_data
.hash_types
, VirtIONet
),
3332 VMSTATE_UINT16(rss_data
.indirections_len
, VirtIONet
),
3333 VMSTATE_UINT16(rss_data
.default_queue
, VirtIONet
),
3334 VMSTATE_UINT8_ARRAY(rss_data
.key
, VirtIONet
,
3335 VIRTIO_NET_RSS_MAX_KEY_SIZE
),
3336 VMSTATE_VARRAY_UINT16_ALLOC(rss_data
.indirections_table
, VirtIONet
,
3337 rss_data
.indirections_len
, 0,
3338 vmstate_info_uint16
, uint16_t),
3339 VMSTATE_END_OF_LIST()
3343 static struct vhost_dev
*virtio_net_get_vhost(VirtIODevice
*vdev
)
3345 VirtIONet
*n
= VIRTIO_NET(vdev
);
3347 struct vhost_net
*net
;
3353 nc
= qemu_get_queue(n
->nic
);
3358 net
= get_vhost_net(nc
->peer
);
3366 static int vhost_user_net_save_state(QEMUFile
*f
, void *pv
, size_t size
,
3367 const VMStateField
*field
,
3371 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3372 struct vhost_dev
*vhdev
;
3373 Error
*local_error
= NULL
;
3376 vhdev
= virtio_net_get_vhost(vdev
);
3377 if (vhdev
== NULL
) {
3378 error_reportf_err(local_error
,
3379 "Error getting vhost back-end of %s device %s: ",
3380 vdev
->name
, vdev
->parent_obj
.canonical_path
);
3384 ret
= vhost_save_backend_state(vhdev
, f
, &local_error
);
3386 error_reportf_err(local_error
,
3387 "Error saving back-end state of %s device %s: ",
3388 vdev
->name
, vdev
->parent_obj
.canonical_path
);
3395 static int vhost_user_net_load_state(QEMUFile
*f
, void *pv
, size_t size
,
3396 const VMStateField
*field
)
3399 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3400 struct vhost_dev
*vhdev
;
3401 Error
*local_error
= NULL
;
3404 vhdev
= virtio_net_get_vhost(vdev
);
3405 if (vhdev
== NULL
) {
3406 error_reportf_err(local_error
,
3407 "Error getting vhost back-end of %s device %s: ",
3408 vdev
->name
, vdev
->parent_obj
.canonical_path
);
3412 ret
= vhost_load_backend_state(vhdev
, f
, &local_error
);
3414 error_reportf_err(local_error
,
3415 "Error loading back-end state of %s device %s: ",
3416 vdev
->name
, vdev
->parent_obj
.canonical_path
);
3423 static bool vhost_user_net_is_internal_migration(void *opaque
)
3425 VirtIONet
*n
= opaque
;
3426 VirtIODevice
*vdev
= VIRTIO_DEVICE(n
);
3427 struct vhost_dev
*vhdev
;
3429 vhdev
= virtio_net_get_vhost(vdev
);
3430 if (vhdev
== NULL
) {
3434 return vhost_supports_device_state(vhdev
);
3437 static const VMStateDescription vhost_user_net_backend_state
= {
3438 .name
= "virtio-net-device/backend",
3440 .needed
= vhost_user_net_is_internal_migration
,
3441 .fields
= (const VMStateField
[]) {
3444 .info
= &(const VMStateInfo
) {
3445 .name
= "virtio-net vhost-user backend state",
3446 .get
= vhost_user_net_load_state
,
3447 .put
= vhost_user_net_save_state
,
3450 VMSTATE_END_OF_LIST()
3454 static const VMStateDescription vmstate_virtio_net_device
= {
3455 .name
= "virtio-net-device",
3456 .version_id
= VIRTIO_NET_VM_VERSION
,
3457 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
3458 .post_load
= virtio_net_post_load_device
,
3459 .fields
= (const VMStateField
[]) {
3460 VMSTATE_UINT8_ARRAY(mac
, VirtIONet
, ETH_ALEN
),
3461 VMSTATE_STRUCT_POINTER(vqs
, VirtIONet
,
3462 vmstate_virtio_net_queue_tx_waiting
,
3464 VMSTATE_UINT32(mergeable_rx_bufs
, VirtIONet
),
3465 VMSTATE_UINT16(status
, VirtIONet
),
3466 VMSTATE_UINT8(promisc
, VirtIONet
),
3467 VMSTATE_UINT8(allmulti
, VirtIONet
),
3468 VMSTATE_UINT32(mac_table
.in_use
, VirtIONet
),
3470 /* Guarded pair: If it fits we load it, else we throw it away
3471 * - can happen if source has a larger MAC table.; post-load
3472 * sets flags in this case.
3474 VMSTATE_VBUFFER_MULTIPLY(mac_table
.macs
, VirtIONet
,
3475 0, mac_table_fits
, mac_table
.in_use
,
3477 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet
, mac_table_doesnt_fit
, 0,
3478 mac_table
.in_use
, ETH_ALEN
),
3480 /* Note: This is an array of uint32's that's always been saved as a
3481 * buffer; hold onto your endiannesses; it's actually used as a bitmap
3482 * but based on the uint.
3484 VMSTATE_BUFFER_POINTER_UNSAFE(vlans
, VirtIONet
, 0, MAX_VLAN
>> 3),
3485 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3486 vmstate_virtio_net_has_vnet
),
3487 VMSTATE_UINT8(mac_table
.multi_overflow
, VirtIONet
),
3488 VMSTATE_UINT8(mac_table
.uni_overflow
, VirtIONet
),
3489 VMSTATE_UINT8(alluni
, VirtIONet
),
3490 VMSTATE_UINT8(nomulti
, VirtIONet
),
3491 VMSTATE_UINT8(nouni
, VirtIONet
),
3492 VMSTATE_UINT8(nobcast
, VirtIONet
),
3493 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3494 vmstate_virtio_net_has_ufo
),
3495 VMSTATE_SINGLE_TEST(max_queue_pairs
, VirtIONet
, max_queue_pairs_gt_1
, 0,
3496 vmstate_info_uint16_equal
, uint16_t),
3497 VMSTATE_UINT16_TEST(curr_queue_pairs
, VirtIONet
, max_queue_pairs_gt_1
),
3498 VMSTATE_WITH_TMP(VirtIONet
, struct VirtIONetMigTmp
,
3499 vmstate_virtio_net_tx_waiting
),
3500 VMSTATE_UINT64_TEST(curr_guest_offloads
, VirtIONet
,
3501 has_ctrl_guest_offloads
),
3502 VMSTATE_END_OF_LIST()
3504 .subsections
= (const VMStateDescription
* const []) {
3505 &vmstate_virtio_net_rss
,
3506 &vhost_user_net_backend_state
,
3511 static NetClientInfo net_virtio_info
= {
3512 .type
= NET_CLIENT_DRIVER_NIC
,
3513 .size
= sizeof(NICState
),
3514 .can_receive
= virtio_net_can_receive
,
3515 .receive
= virtio_net_receive
,
3516 .link_status_changed
= virtio_net_set_link_status
,
3517 .query_rx_filter
= virtio_net_query_rxfilter
,
3518 .announce
= virtio_net_announce
,
3521 static bool virtio_net_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
3523 VirtIONet
*n
= VIRTIO_NET(vdev
);
3525 assert(n
->vhost_started
);
3526 if (!n
->multiqueue
&& idx
== 2) {
3527 /* Must guard against invalid features and bogus queue index
3528 * from being set by malicious guest, or penetrated through
3529 * buggy migration stream.
3531 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3532 qemu_log_mask(LOG_GUEST_ERROR
,
3533 "%s: bogus vq index ignored\n", __func__
);
3536 nc
= qemu_get_subqueue(n
->nic
, n
->max_queue_pairs
);
3538 nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3541 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
3542 * as the macro of configure interrupt's IDX, If this driver does not
3543 * support, the function will return false
3546 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
3547 return vhost_net_config_pending(get_vhost_net(nc
->peer
));
3549 return vhost_net_virtqueue_pending(get_vhost_net(nc
->peer
), idx
);
3552 static void virtio_net_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
3555 VirtIONet
*n
= VIRTIO_NET(vdev
);
3557 assert(n
->vhost_started
);
3558 if (!n
->multiqueue
&& idx
== 2) {
3559 /* Must guard against invalid features and bogus queue index
3560 * from being set by malicious guest, or penetrated through
3561 * buggy migration stream.
3563 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
3564 qemu_log_mask(LOG_GUEST_ERROR
,
3565 "%s: bogus vq index ignored\n", __func__
);
3568 nc
= qemu_get_subqueue(n
->nic
, n
->max_queue_pairs
);
3570 nc
= qemu_get_subqueue(n
->nic
, vq2q(idx
));
3573 *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
3574 * as the macro of configure interrupt's IDX, If this driver does not
3575 * support, the function will return
3578 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
3579 vhost_net_config_mask(get_vhost_net(nc
->peer
), vdev
, mask
);
3582 vhost_net_virtqueue_mask(get_vhost_net(nc
->peer
), vdev
, idx
, mask
);
3585 static void virtio_net_set_config_size(VirtIONet
*n
, uint64_t host_features
)
3587 virtio_add_feature(&host_features
, VIRTIO_NET_F_MAC
);
3589 n
->config_size
= virtio_get_config_size(&cfg_size_params
, host_features
);
3592 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,
3596 * The name can be NULL, the netclient name will be type.x.
3598 assert(type
!= NULL
);
3600 g_free(n
->netclient_name
);
3601 g_free(n
->netclient_type
);
3602 n
->netclient_name
= g_strdup(name
);
3603 n
->netclient_type
= g_strdup(type
);
3606 static bool failover_unplug_primary(VirtIONet
*n
, DeviceState
*dev
)
3608 HotplugHandler
*hotplug_ctrl
;
3612 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3614 pci_dev
= PCI_DEVICE(dev
);
3615 pci_dev
->partially_hotplugged
= true;
3616 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &err
);
3618 error_report_err(err
);
3627 static bool failover_replug_primary(VirtIONet
*n
, DeviceState
*dev
,
3631 HotplugHandler
*hotplug_ctrl
;
3632 PCIDevice
*pdev
= PCI_DEVICE(dev
);
3633 BusState
*primary_bus
;
3635 if (!pdev
->partially_hotplugged
) {
3638 primary_bus
= dev
->parent_bus
;
3640 error_setg(errp
, "virtio_net: couldn't find primary bus");
3643 qdev_set_parent_bus(dev
, primary_bus
, &error_abort
);
3644 qatomic_set(&n
->failover_primary_hidden
, false);
3645 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
3647 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &err
);
3651 hotplug_handler_plug(hotplug_ctrl
, dev
, &err
);
3653 pdev
->partially_hotplugged
= false;
3656 error_propagate(errp
, err
);
3660 static void virtio_net_handle_migration_primary(VirtIONet
*n
, MigrationEvent
*e
)
3662 bool should_be_hidden
;
3664 DeviceState
*dev
= failover_find_primary_device(n
);
3670 should_be_hidden
= qatomic_read(&n
->failover_primary_hidden
);
3672 if (e
->type
== MIG_EVENT_PRECOPY_SETUP
&& !should_be_hidden
) {
3673 if (failover_unplug_primary(n
, dev
)) {
3674 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
3675 qapi_event_send_unplug_primary(dev
->id
);
3676 qatomic_set(&n
->failover_primary_hidden
, true);
3678 warn_report("couldn't unplug primary device");
3680 } else if (e
->type
== MIG_EVENT_PRECOPY_FAILED
) {
3681 /* We already unplugged the device let's plug it back */
3682 if (!failover_replug_primary(n
, dev
, &err
)) {
3684 error_report_err(err
);
3690 static int virtio_net_migration_state_notifier(NotifierWithReturn
*notifier
,
3691 MigrationEvent
*e
, Error
**errp
)
3693 VirtIONet
*n
= container_of(notifier
, VirtIONet
, migration_state
);
3694 virtio_net_handle_migration_primary(n
, e
);
3698 static bool failover_hide_primary_device(DeviceListener
*listener
,
3699 const QDict
*device_opts
,
3703 VirtIONet
*n
= container_of(listener
, VirtIONet
, primary_listener
);
3704 const char *standby_id
;
3710 if (!qdict_haskey(device_opts
, "failover_pair_id")) {
3714 if (!qdict_haskey(device_opts
, "id")) {
3715 error_setg(errp
, "Device with failover_pair_id needs to have id");
3719 standby_id
= qdict_get_str(device_opts
, "failover_pair_id");
3720 if (g_strcmp0(standby_id
, n
->netclient_name
) != 0) {
3725 * The hide helper can be called several times for a given device.
3726 * Check there is only one primary for a virtio-net device but
3727 * don't duplicate the qdict several times if it's called for the same
3730 if (n
->primary_opts
) {
3731 const char *old
, *new;
3732 /* devices with failover_pair_id always have an id */
3733 old
= qdict_get_str(n
->primary_opts
, "id");
3734 new = qdict_get_str(device_opts
, "id");
3735 if (strcmp(old
, new) != 0) {
3736 error_setg(errp
, "Cannot attach more than one primary device to "
3737 "'%s': '%s' and '%s'", n
->netclient_name
, old
, new);
3741 n
->primary_opts
= qdict_clone_shallow(device_opts
);
3742 n
->primary_opts_from_json
= from_json
;
3745 /* failover_primary_hidden is set during feature negotiation */
3746 return qatomic_read(&n
->failover_primary_hidden
);
3749 static void virtio_net_device_realize(DeviceState
*dev
, Error
**errp
)
3751 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3752 VirtIONet
*n
= VIRTIO_NET(dev
);
3756 if (n
->net_conf
.mtu
) {
3757 n
->host_features
|= (1ULL << VIRTIO_NET_F_MTU
);
3760 if (n
->net_conf
.duplex_str
) {
3761 if (strncmp(n
->net_conf
.duplex_str
, "half", 5) == 0) {
3762 n
->net_conf
.duplex
= DUPLEX_HALF
;
3763 } else if (strncmp(n
->net_conf
.duplex_str
, "full", 5) == 0) {
3764 n
->net_conf
.duplex
= DUPLEX_FULL
;
3766 error_setg(errp
, "'duplex' must be 'half' or 'full'");
3769 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3771 n
->net_conf
.duplex
= DUPLEX_UNKNOWN
;
3774 if (n
->net_conf
.speed
< SPEED_UNKNOWN
) {
3775 error_setg(errp
, "'speed' must be between 0 and INT_MAX");
3778 if (n
->net_conf
.speed
>= 0) {
3779 n
->host_features
|= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX
);
3783 n
->primary_listener
.hide_device
= failover_hide_primary_device
;
3784 qatomic_set(&n
->failover_primary_hidden
, true);
3785 device_listener_register(&n
->primary_listener
);
3786 migration_add_notifier(&n
->migration_state
,
3787 virtio_net_migration_state_notifier
);
3788 n
->host_features
|= (1ULL << VIRTIO_NET_F_STANDBY
);
3791 virtio_net_set_config_size(n
, n
->host_features
);
3792 virtio_init(vdev
, VIRTIO_ID_NET
, n
->config_size
);
3795 * We set a lower limit on RX queue size to what it always was.
3796 * Guests that want a smaller ring can always resize it without
3797 * help from us (using virtio 1 and up).
3799 if (n
->net_conf
.rx_queue_size
< VIRTIO_NET_RX_QUEUE_MIN_SIZE
||
3800 n
->net_conf
.rx_queue_size
> VIRTQUEUE_MAX_SIZE
||
3801 !is_power_of_2(n
->net_conf
.rx_queue_size
)) {
3802 error_setg(errp
, "Invalid rx_queue_size (= %" PRIu16
"), "
3803 "must be a power of 2 between %d and %d.",
3804 n
->net_conf
.rx_queue_size
, VIRTIO_NET_RX_QUEUE_MIN_SIZE
,
3805 VIRTQUEUE_MAX_SIZE
);
3806 virtio_cleanup(vdev
);
3810 if (n
->net_conf
.tx_queue_size
< VIRTIO_NET_TX_QUEUE_MIN_SIZE
||
3811 n
->net_conf
.tx_queue_size
> virtio_net_max_tx_queue_size(n
) ||
3812 !is_power_of_2(n
->net_conf
.tx_queue_size
)) {
3813 error_setg(errp
, "Invalid tx_queue_size (= %" PRIu16
"), "
3814 "must be a power of 2 between %d and %d",
3815 n
->net_conf
.tx_queue_size
, VIRTIO_NET_TX_QUEUE_MIN_SIZE
,
3816 virtio_net_max_tx_queue_size(n
));
3817 virtio_cleanup(vdev
);
3821 n
->max_ncs
= MAX(n
->nic_conf
.peers
.queues
, 1);
3824 * Figure out the datapath queue pairs since the backend could
3825 * provide control queue via peers as well.
3827 if (n
->nic_conf
.peers
.queues
) {
3828 for (i
= 0; i
< n
->max_ncs
; i
++) {
3829 if (n
->nic_conf
.peers
.ncs
[i
]->is_datapath
) {
3830 ++n
->max_queue_pairs
;
3834 n
->max_queue_pairs
= MAX(n
->max_queue_pairs
, 1);
3836 if (n
->max_queue_pairs
* 2 + 1 > VIRTIO_QUEUE_MAX
) {
3837 error_setg(errp
, "Invalid number of queue pairs (= %" PRIu32
"), "
3838 "must be a positive integer less than %d.",
3839 n
->max_queue_pairs
, (VIRTIO_QUEUE_MAX
- 1) / 2);
3840 virtio_cleanup(vdev
);
3843 n
->vqs
= g_new0(VirtIONetQueue
, n
->max_queue_pairs
);
3844 n
->curr_queue_pairs
= 1;
3845 n
->tx_timeout
= n
->net_conf
.txtimer
;
3847 if (n
->net_conf
.tx
&& strcmp(n
->net_conf
.tx
, "timer")
3848 && strcmp(n
->net_conf
.tx
, "bh")) {
3849 warn_report("virtio-net: "
3850 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3852 error_printf("Defaulting to \"bh\"");
3855 n
->net_conf
.tx_queue_size
= MIN(virtio_net_max_tx_queue_size(n
),
3856 n
->net_conf
.tx_queue_size
);
3858 virtio_net_add_queue(n
, 0);
3860 n
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_net_handle_ctrl
);
3861 qemu_macaddr_default_if_unset(&n
->nic_conf
.macaddr
);
3862 memcpy(&n
->mac
[0], &n
->nic_conf
.macaddr
, sizeof(n
->mac
));
3863 n
->status
= VIRTIO_NET_S_LINK_UP
;
3864 qemu_announce_timer_reset(&n
->announce_timer
, migrate_announce_params(),
3866 virtio_net_announce_timer
, n
);
3867 n
->announce_timer
.round
= 0;
3869 if (n
->netclient_type
) {
3871 * Happen when virtio_net_set_netclient_name has been called.
3873 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3874 n
->netclient_type
, n
->netclient_name
,
3875 &dev
->mem_reentrancy_guard
, n
);
3877 n
->nic
= qemu_new_nic(&net_virtio_info
, &n
->nic_conf
,
3878 object_get_typename(OBJECT(dev
)), dev
->id
,
3879 &dev
->mem_reentrancy_guard
, n
);
3882 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3883 n
->nic
->ncs
[i
].do_not_pad
= true;
3886 peer_test_vnet_hdr(n
);
3887 if (peer_has_vnet_hdr(n
)) {
3888 n
->host_hdr_len
= sizeof(struct virtio_net_hdr
);
3890 n
->host_hdr_len
= 0;
3893 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->nic_conf
.macaddr
.a
);
3895 n
->vqs
[0].tx_waiting
= 0;
3896 n
->tx_burst
= n
->net_conf
.txburst
;
3897 virtio_net_set_mrg_rx_bufs(n
, 0, 0, 0);
3898 n
->promisc
= 1; /* for compatibility */
3900 n
->mac_table
.macs
= g_malloc0(MAC_TABLE_ENTRIES
* ETH_ALEN
);
3902 n
->vlans
= g_malloc0(MAX_VLAN
>> 3);
3904 nc
= qemu_get_queue(n
->nic
);
3905 nc
->rxfilter_notify_enabled
= 1;
3907 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
3908 struct virtio_net_config netcfg
= {};
3909 memcpy(&netcfg
.mac
, &n
->nic_conf
.macaddr
, ETH_ALEN
);
3910 vhost_net_set_config(get_vhost_net(nc
->peer
),
3911 (uint8_t *)&netcfg
, 0, ETH_ALEN
, VHOST_SET_CONFIG_TYPE_FRONTEND
);
3913 QTAILQ_INIT(&n
->rsc_chains
);
3916 net_rx_pkt_init(&n
->rx_pkt
);
3918 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3919 virtio_net_load_ebpf(n
, errp
);
3923 static void virtio_net_device_unrealize(DeviceState
*dev
)
3925 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3926 VirtIONet
*n
= VIRTIO_NET(dev
);
3927 int i
, max_queue_pairs
;
3929 if (virtio_has_feature(n
->host_features
, VIRTIO_NET_F_RSS
)) {
3930 virtio_net_unload_ebpf(n
);
3933 /* This will stop vhost backend if appropriate. */
3934 virtio_net_set_status(vdev
, 0);
3936 g_free(n
->netclient_name
);
3937 n
->netclient_name
= NULL
;
3938 g_free(n
->netclient_type
);
3939 n
->netclient_type
= NULL
;
3941 g_free(n
->mac_table
.macs
);
3945 qobject_unref(n
->primary_opts
);
3946 device_listener_unregister(&n
->primary_listener
);
3947 migration_remove_notifier(&n
->migration_state
);
3949 assert(n
->primary_opts
== NULL
);
3952 max_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
3953 for (i
= 0; i
< max_queue_pairs
; i
++) {
3954 virtio_net_del_queue(n
, i
);
3956 /* delete also control vq */
3957 virtio_del_queue(vdev
, max_queue_pairs
* 2);
3958 qemu_announce_timer_del(&n
->announce_timer
, false);
3960 qemu_del_nic(n
->nic
);
3961 virtio_net_rsc_cleanup(n
);
3962 g_free(n
->rss_data
.indirections_table
);
3963 net_rx_pkt_uninit(n
->rx_pkt
);
3964 virtio_cleanup(vdev
);
3967 static void virtio_net_reset(VirtIODevice
*vdev
)
3969 VirtIONet
*n
= VIRTIO_NET(vdev
);
3972 /* Reset back to compatibility mode */
3979 /* multiqueue is disabled by default */
3980 n
->curr_queue_pairs
= 1;
3981 timer_del(n
->announce_timer
.tm
);
3982 n
->announce_timer
.round
= 0;
3983 n
->status
&= ~VIRTIO_NET_S_ANNOUNCE
;
3985 /* Flush any MAC and VLAN filter table state */
3986 n
->mac_table
.in_use
= 0;
3987 n
->mac_table
.first_multi
= 0;
3988 n
->mac_table
.multi_overflow
= 0;
3989 n
->mac_table
.uni_overflow
= 0;
3990 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
3991 memcpy(&n
->mac
[0], &n
->nic
->conf
->macaddr
, sizeof(n
->mac
));
3992 qemu_format_nic_info_str(qemu_get_queue(n
->nic
), n
->mac
);
3993 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
3995 /* Flush any async TX */
3996 for (i
= 0; i
< n
->max_queue_pairs
; i
++) {
3997 flush_or_purge_queued_packets(qemu_get_subqueue(n
->nic
, i
));
4000 virtio_net_disable_rss(n
);
4003 static void virtio_net_instance_init(Object
*obj
)
4005 VirtIONet
*n
= VIRTIO_NET(obj
);
4008 * The default config_size is sizeof(struct virtio_net_config).
4009 * Can be overridden with virtio_net_set_config_size.
4011 n
->config_size
= sizeof(struct virtio_net_config
);
4012 device_add_bootindex_property(obj
, &n
->nic_conf
.bootindex
,
4013 "bootindex", "/ethernet-phy@0",
4016 ebpf_rss_init(&n
->ebpf_rss
);
4019 static int virtio_net_pre_save(void *opaque
)
4021 VirtIONet
*n
= opaque
;
4023 /* At this point, backend must be stopped, otherwise
4024 * it might keep writing to memory. */
4025 assert(!n
->vhost_started
);
4030 static bool primary_unplug_pending(void *opaque
)
4032 DeviceState
*dev
= opaque
;
4033 DeviceState
*primary
;
4034 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
4035 VirtIONet
*n
= VIRTIO_NET(vdev
);
4037 if (!virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_STANDBY
)) {
4040 primary
= failover_find_primary_device(n
);
4041 return primary
? primary
->pending_deleted_event
: false;
4044 static bool dev_unplug_pending(void *opaque
)
4046 DeviceState
*dev
= opaque
;
4047 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
4049 return vdc
->primary_unplug_pending(dev
);
4052 static const VMStateDescription vmstate_virtio_net
= {
4053 .name
= "virtio-net",
4054 .minimum_version_id
= VIRTIO_NET_VM_VERSION
,
4055 .version_id
= VIRTIO_NET_VM_VERSION
,
4056 .fields
= (const VMStateField
[]) {
4057 VMSTATE_VIRTIO_DEVICE
,
4058 VMSTATE_END_OF_LIST()
4060 .pre_save
= virtio_net_pre_save
,
4061 .dev_unplug_pending
= dev_unplug_pending
,
4064 static const Property virtio_net_properties
[] = {
4065 DEFINE_PROP_BIT64("csum", VirtIONet
, host_features
,
4066 VIRTIO_NET_F_CSUM
, true),
4067 DEFINE_PROP_BIT64("guest_csum", VirtIONet
, host_features
,
4068 VIRTIO_NET_F_GUEST_CSUM
, true),
4069 DEFINE_PROP_BIT64("gso", VirtIONet
, host_features
, VIRTIO_NET_F_GSO
, true),
4070 DEFINE_PROP_BIT64("guest_tso4", VirtIONet
, host_features
,
4071 VIRTIO_NET_F_GUEST_TSO4
, true),
4072 DEFINE_PROP_BIT64("guest_tso6", VirtIONet
, host_features
,
4073 VIRTIO_NET_F_GUEST_TSO6
, true),
4074 DEFINE_PROP_BIT64("guest_ecn", VirtIONet
, host_features
,
4075 VIRTIO_NET_F_GUEST_ECN
, true),
4076 DEFINE_PROP_BIT64("guest_ufo", VirtIONet
, host_features
,
4077 VIRTIO_NET_F_GUEST_UFO
, true),
4078 DEFINE_PROP_BIT64("guest_announce", VirtIONet
, host_features
,
4079 VIRTIO_NET_F_GUEST_ANNOUNCE
, true),
4080 DEFINE_PROP_BIT64("host_tso4", VirtIONet
, host_features
,
4081 VIRTIO_NET_F_HOST_TSO4
, true),
4082 DEFINE_PROP_BIT64("host_tso6", VirtIONet
, host_features
,
4083 VIRTIO_NET_F_HOST_TSO6
, true),
4084 DEFINE_PROP_BIT64("host_ecn", VirtIONet
, host_features
,
4085 VIRTIO_NET_F_HOST_ECN
, true),
4086 DEFINE_PROP_BIT64("host_ufo", VirtIONet
, host_features
,
4087 VIRTIO_NET_F_HOST_UFO
, true),
4088 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet
, host_features
,
4089 VIRTIO_NET_F_MRG_RXBUF
, true),
4090 DEFINE_PROP_BIT64("status", VirtIONet
, host_features
,
4091 VIRTIO_NET_F_STATUS
, true),
4092 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet
, host_features
,
4093 VIRTIO_NET_F_CTRL_VQ
, true),
4094 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet
, host_features
,
4095 VIRTIO_NET_F_CTRL_RX
, true),
4096 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet
, host_features
,
4097 VIRTIO_NET_F_CTRL_VLAN
, true),
4098 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet
, host_features
,
4099 VIRTIO_NET_F_CTRL_RX_EXTRA
, true),
4100 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet
, host_features
,
4101 VIRTIO_NET_F_CTRL_MAC_ADDR
, true),
4102 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet
, host_features
,
4103 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
, true),
4104 DEFINE_PROP_BIT64("mq", VirtIONet
, host_features
, VIRTIO_NET_F_MQ
, false),
4105 DEFINE_PROP_BIT64("rss", VirtIONet
, host_features
,
4106 VIRTIO_NET_F_RSS
, false),
4107 DEFINE_PROP_BIT64("hash", VirtIONet
, host_features
,
4108 VIRTIO_NET_F_HASH_REPORT
, false),
4109 DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet
, nr_ebpf_rss_fds
,
4110 ebpf_rss_fds
, qdev_prop_string
, char*),
4111 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet
, host_features
,
4112 VIRTIO_NET_F_RSC_EXT
, false),
4113 DEFINE_PROP_UINT32("rsc_interval", VirtIONet
, rsc_timeout
,
4114 VIRTIO_NET_RSC_DEFAULT_INTERVAL
),
4115 DEFINE_NIC_PROPERTIES(VirtIONet
, nic_conf
),
4116 DEFINE_PROP_UINT32("x-txtimer", VirtIONet
, net_conf
.txtimer
,
4118 DEFINE_PROP_INT32("x-txburst", VirtIONet
, net_conf
.txburst
, TX_BURST
),
4119 DEFINE_PROP_STRING("tx", VirtIONet
, net_conf
.tx
),
4120 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet
, net_conf
.rx_queue_size
,
4121 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
),
4122 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet
, net_conf
.tx_queue_size
,
4123 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
),
4124 DEFINE_PROP_UINT16("host_mtu", VirtIONet
, net_conf
.mtu
, 0),
4125 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet
, mtu_bypass_backend
,
4127 DEFINE_PROP_INT32("speed", VirtIONet
, net_conf
.speed
, SPEED_UNKNOWN
),
4128 DEFINE_PROP_STRING("duplex", VirtIONet
, net_conf
.duplex_str
),
4129 DEFINE_PROP_BOOL("failover", VirtIONet
, failover
, false),
4130 DEFINE_PROP_BIT64("guest_uso4", VirtIONet
, host_features
,
4131 VIRTIO_NET_F_GUEST_USO4
, true),
4132 DEFINE_PROP_BIT64("guest_uso6", VirtIONet
, host_features
,
4133 VIRTIO_NET_F_GUEST_USO6
, true),
4134 DEFINE_PROP_BIT64("host_uso", VirtIONet
, host_features
,
4135 VIRTIO_NET_F_HOST_USO
, true),
4138 static void virtio_net_class_init(ObjectClass
*klass
, const void *data
)
4140 DeviceClass
*dc
= DEVICE_CLASS(klass
);
4141 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
4143 device_class_set_props(dc
, virtio_net_properties
);
4144 dc
->vmsd
= &vmstate_virtio_net
;
4145 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
4146 vdc
->realize
= virtio_net_device_realize
;
4147 vdc
->unrealize
= virtio_net_device_unrealize
;
4148 vdc
->get_config
= virtio_net_get_config
;
4149 vdc
->set_config
= virtio_net_set_config
;
4150 vdc
->get_features
= virtio_net_get_features
;
4151 vdc
->set_features
= virtio_net_set_features
;
4152 vdc
->bad_features
= virtio_net_bad_features
;
4153 vdc
->reset
= virtio_net_reset
;
4154 vdc
->queue_reset
= virtio_net_queue_reset
;
4155 vdc
->queue_enable
= virtio_net_queue_enable
;
4156 vdc
->set_status
= virtio_net_set_status
;
4157 vdc
->guest_notifier_mask
= virtio_net_guest_notifier_mask
;
4158 vdc
->guest_notifier_pending
= virtio_net_guest_notifier_pending
;
4159 vdc
->legacy_features
|= (0x1 << VIRTIO_NET_F_GSO
);
4160 vdc
->pre_load_queues
= virtio_net_pre_load_queues
;
4161 vdc
->post_load
= virtio_net_post_load_virtio
;
4162 vdc
->vmsd
= &vmstate_virtio_net_device
;
4163 vdc
->primary_unplug_pending
= primary_unplug_pending
;
4164 vdc
->get_vhost
= virtio_net_get_vhost
;
4165 vdc
->toggle_device_iotlb
= vhost_toggle_device_iotlb
;
4168 static const TypeInfo virtio_net_info
= {
4169 .name
= TYPE_VIRTIO_NET
,
4170 .parent
= TYPE_VIRTIO_DEVICE
,
4171 .instance_size
= sizeof(VirtIONet
),
4172 .instance_init
= virtio_net_instance_init
,
4173 .class_init
= virtio_net_class_init
,
4176 static void virtio_register_types(void)
4178 type_register_static(&virtio_net_info
);
4181 type_init(virtio_register_types
)