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 typedef struct virtio_net_conf
43 uint16_t rx_queue_size
;
44 uint16_t tx_queue_size
;
52 /* Coalesced packets type & status */
54 RSC_COALESCE
, /* Data been coalesced */
55 RSC_FINAL
, /* Will terminate current connection */
56 RSC_NO_MATCH
, /* No matched in the buffer pool */
57 RSC_BYPASS
, /* Packet to be bypass, not tcp, tcp ctrl, etc */
58 RSC_CANDIDATE
/* Data want to be coalesced */
61 typedef struct VirtioNetRscStat
{
67 uint32_t no_match_cache
;
71 uint32_t tcp_ctrl_drain
;
76 uint32_t ack_out_of_win
;
77 uint32_t data_out_of_win
;
78 uint32_t data_out_of_order
;
79 uint32_t data_after_pure_ack
;
80 uint32_t bypass_not_tcp
;
87 uint32_t purge_failed
;
88 uint32_t drain_failed
;
89 uint32_t final_failed
;
93 /* Rsc unit general info used to checking if can coalescing */
94 typedef struct VirtioNetRscUnit
{
95 void *ip
; /* ip header */
96 uint16_t *ip_plen
; /* data len pointer in ip header field */
97 struct tcp_header
*tcp
; /* tcp header */
98 uint16_t tcp_hdrlen
; /* tcp header len */
99 uint16_t payload
; /* pure payload without virtio/eth/ip/tcp */
102 /* Coalesced segment */
103 typedef struct VirtioNetRscSeg
{
104 QTAILQ_ENTRY(VirtioNetRscSeg
) next
;
109 bool is_coalesced
; /* need recal ipv4 header checksum, mark here */
110 VirtioNetRscUnit unit
;
115 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
116 typedef struct VirtioNetRscChain
{
117 QTAILQ_ENTRY(VirtioNetRscChain
) next
;
118 VirtIONet
*n
; /* VirtIONet */
121 uint16_t max_payload
;
122 QEMUTimer
*drain_timer
;
123 QTAILQ_HEAD(, VirtioNetRscSeg
) buffers
;
124 VirtioNetRscStat stat
;
127 /* Maximum packet size we can receive from tap device: header + 64k */
128 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
130 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
131 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
133 typedef struct VirtioNetRssData
{
135 bool enabled_software_rss
;
139 uint8_t key
[VIRTIO_NET_RSS_MAX_KEY_SIZE
];
140 uint16_t indirections_len
;
141 uint16_t *indirections_table
;
142 uint16_t default_queue
;
145 typedef struct VirtIONetQueue
{
152 VirtQueueElement
*elem
;
158 VirtIODevice parent_obj
;
159 uint8_t mac
[ETH_ALEN
];
164 /* RSC Chains - temporary storage of coalesced data,
165 all these data are lost in case of migration */
166 QTAILQ_HEAD(, VirtioNetRscChain
) rsc_chains
;
169 uint32_t has_vnet_hdr
;
171 size_t guest_hdr_len
;
172 uint64_t host_features
;
173 uint32_t rsc_timeout
;
174 uint8_t rsc4_enabled
;
175 uint8_t rsc6_enabled
;
177 uint32_t mergeable_rx_bufs
;
184 uint8_t vhost_started
;
187 uint32_t first_multi
;
188 uint8_t multi_overflow
;
189 uint8_t uni_overflow
;
193 virtio_net_conf net_conf
;
198 uint16_t curr_queues
;
200 char *netclient_name
;
201 char *netclient_type
;
202 uint64_t curr_guest_offloads
;
203 /* used on saved state restore phase to preserve the curr_guest_offloads */
204 uint64_t saved_guest_offloads
;
205 AnnounceTimer announce_timer
;
206 bool needs_vnet_hdr_swap
;
207 bool mtu_bypass_backend
;
208 /* primary failover device is hidden*/
209 bool failover_primary_hidden
;
211 DeviceListener primary_listener
;
213 bool primary_opts_from_json
;
214 Notifier migration_state
;
215 VirtioNetRssData rss_data
;
216 struct NetRxPkt
*rx_pkt
;
217 struct EBPFRSSContext ebpf_rss
;
220 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,