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 #ifndef QEMU_VIRTIO_NET_H
15 #define QEMU_VIRTIO_NET_H
17 #include "qemu/units.h"
18 #include "standard-headers/linux/virtio_net.h"
19 #include "hw/virtio/virtio.h"
20 #include "net/announce.h"
21 #include "qemu/option_int.h"
22 #include "qom/object.h"
24 #include "ebpf/ebpf_rss.h"
26 #define TYPE_VIRTIO_NET "virtio-net-device"
27 OBJECT_DECLARE_SIMPLE_TYPE(VirtIONet
, VIRTIO_NET
)
29 #define TX_TIMER_INTERVAL 150000 /* 150 us */
31 /* Limit the number of packets that can be sent via a single flush
32 * of the TX queue. This gives us a guaranteed exit condition and
33 * ensures fairness in the io path. 256 conveniently matches the
34 * length of the TX queue and shows a good balance of performance
38 /* Maximum VIRTIO_NET_CTRL_MAC_TABLE_SET unicast + multicast entries. */
39 #define MAC_TABLE_ENTRIES 64
41 typedef struct virtio_net_conf
46 uint16_t rx_queue_size
;
47 uint16_t tx_queue_size
;
55 /* Coalesced packets type & status */
57 RSC_COALESCE
, /* Data been coalesced */
58 RSC_FINAL
, /* Will terminate current connection */
59 RSC_NO_MATCH
, /* No matched in the buffer pool */
60 RSC_BYPASS
, /* Packet to be bypass, not tcp, tcp ctrl, etc */
61 RSC_CANDIDATE
/* Data want to be coalesced */
64 typedef struct VirtioNetRscStat
{
70 uint32_t no_match_cache
;
74 uint32_t tcp_ctrl_drain
;
79 uint32_t ack_out_of_win
;
80 uint32_t data_out_of_win
;
81 uint32_t data_out_of_order
;
82 uint32_t data_after_pure_ack
;
83 uint32_t bypass_not_tcp
;
90 uint32_t purge_failed
;
91 uint32_t drain_failed
;
92 uint32_t final_failed
;
96 /* Rsc unit general info used to checking if can coalescing */
97 typedef struct VirtioNetRscUnit
{
98 void *ip
; /* ip header */
99 uint16_t *ip_plen
; /* data len pointer in ip header field */
100 struct tcp_header
*tcp
; /* tcp header */
101 uint16_t tcp_hdrlen
; /* tcp header len */
102 uint16_t payload
; /* pure payload without virtio/eth/ip/tcp */
105 /* Coalesced segment */
106 typedef struct VirtioNetRscSeg
{
107 QTAILQ_ENTRY(VirtioNetRscSeg
) next
;
112 bool is_coalesced
; /* need recal ipv4 header checksum, mark here */
113 VirtioNetRscUnit unit
;
118 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
119 typedef struct VirtioNetRscChain
{
120 QTAILQ_ENTRY(VirtioNetRscChain
) next
;
121 VirtIONet
*n
; /* VirtIONet */
124 uint16_t max_payload
;
125 QEMUTimer
*drain_timer
;
126 QTAILQ_HEAD(, VirtioNetRscSeg
) buffers
;
127 VirtioNetRscStat stat
;
130 /* Maximum packet size we can receive from tap device: header + 64k */
131 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
133 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
134 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
136 typedef struct VirtioNetRssData
{
138 bool enabled_software_rss
;
142 uint8_t key
[VIRTIO_NET_RSS_MAX_KEY_SIZE
];
143 uint16_t indirections_len
;
144 uint16_t *indirections_table
;
145 uint16_t default_queue
;
148 typedef struct VirtIONetQueue
{
155 VirtQueueElement
*elem
;
161 VirtIODevice parent_obj
;
162 uint8_t mac
[ETH_ALEN
];
167 /* RSC Chains - temporary storage of coalesced data,
168 all these data are lost in case of migration */
169 QTAILQ_HEAD(, VirtioNetRscChain
) rsc_chains
;
172 uint32_t has_vnet_hdr
;
174 size_t guest_hdr_len
;
175 uint64_t host_features
;
176 uint32_t rsc_timeout
;
177 uint8_t rsc4_enabled
;
178 uint8_t rsc6_enabled
;
180 uint32_t mergeable_rx_bufs
;
187 uint8_t vhost_started
;
190 uint32_t first_multi
;
191 uint8_t multi_overflow
;
192 uint8_t uni_overflow
;
196 virtio_net_conf net_conf
;
200 uint16_t max_queue_pairs
;
201 uint16_t curr_queue_pairs
;
204 char *netclient_name
;
205 char *netclient_type
;
206 uint64_t curr_guest_offloads
;
207 /* used on saved state restore phase to preserve the curr_guest_offloads */
208 uint64_t saved_guest_offloads
;
209 AnnounceTimer announce_timer
;
210 bool needs_vnet_hdr_swap
;
211 bool mtu_bypass_backend
;
212 /* primary failover device is hidden*/
213 bool failover_primary_hidden
;
215 DeviceListener primary_listener
;
217 bool primary_opts_from_json
;
218 Notifier migration_state
;
219 VirtioNetRssData rss_data
;
220 struct NetRxPkt
*rx_pkt
;
221 struct EBPFRSSContext ebpf_rss
;
224 size_t virtio_net_handle_ctrl_iov(VirtIODevice
*vdev
,
225 const struct iovec
*in_sg
, unsigned in_num
,
226 const struct iovec
*out_sg
,
228 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,