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
42 * The maximum number of VLANs in the VLAN filter table
43 * added by VIRTIO_NET_CTRL_VLAN_ADD
45 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
47 typedef struct virtio_net_conf
52 uint16_t rx_queue_size
;
53 uint16_t tx_queue_size
;
61 /* Coalesced packets type & status */
63 RSC_COALESCE
, /* Data been coalesced */
64 RSC_FINAL
, /* Will terminate current connection */
65 RSC_NO_MATCH
, /* No matched in the buffer pool */
66 RSC_BYPASS
, /* Packet to be bypass, not tcp, tcp ctrl, etc */
67 RSC_CANDIDATE
/* Data want to be coalesced */
70 typedef struct VirtioNetRscStat
{
76 uint32_t no_match_cache
;
80 uint32_t tcp_ctrl_drain
;
85 uint32_t ack_out_of_win
;
86 uint32_t data_out_of_win
;
87 uint32_t data_out_of_order
;
88 uint32_t data_after_pure_ack
;
89 uint32_t bypass_not_tcp
;
96 uint32_t purge_failed
;
97 uint32_t drain_failed
;
98 uint32_t final_failed
;
102 /* Rsc unit general info used to checking if can coalescing */
103 typedef struct VirtioNetRscUnit
{
104 void *ip
; /* ip header */
105 uint16_t *ip_plen
; /* data len pointer in ip header field */
106 struct tcp_header
*tcp
; /* tcp header */
107 uint16_t tcp_hdrlen
; /* tcp header len */
108 uint16_t payload
; /* pure payload without virtio/eth/ip/tcp */
111 /* Coalesced segment */
112 typedef struct VirtioNetRscSeg
{
113 QTAILQ_ENTRY(VirtioNetRscSeg
) next
;
118 bool is_coalesced
; /* need recall ipv4 header checksum, mark here */
119 VirtioNetRscUnit unit
;
124 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
125 typedef struct VirtioNetRscChain
{
126 QTAILQ_ENTRY(VirtioNetRscChain
) next
;
127 VirtIONet
*n
; /* VirtIONet */
130 uint16_t max_payload
;
131 QEMUTimer
*drain_timer
;
132 QTAILQ_HEAD(, VirtioNetRscSeg
) buffers
;
133 VirtioNetRscStat stat
;
136 /* Maximum packet size we can receive from tap device: header + 64k */
137 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
139 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
140 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
142 typedef struct VirtioNetRssData
{
144 bool enabled_software_rss
;
148 uint8_t key
[VIRTIO_NET_RSS_MAX_KEY_SIZE
];
149 uint16_t indirections_len
;
150 uint16_t *indirections_table
;
151 uint16_t default_queue
;
154 typedef struct VirtIONetQueue
{
161 VirtQueueElement
*elem
;
167 VirtIODevice parent_obj
;
168 uint8_t mac
[ETH_ALEN
];
173 /* RSC Chains - temporary storage of coalesced data,
174 all these data are lost in case of migration */
175 QTAILQ_HEAD(, VirtioNetRscChain
) rsc_chains
;
178 uint32_t has_vnet_hdr
;
180 size_t guest_hdr_len
;
181 uint64_t host_features
;
182 uint32_t rsc_timeout
;
183 uint8_t rsc4_enabled
;
184 uint8_t rsc6_enabled
;
186 uint32_t mergeable_rx_bufs
;
193 uint8_t vhost_started
;
196 uint32_t first_multi
;
197 uint8_t multi_overflow
;
198 uint8_t uni_overflow
;
202 virtio_net_conf net_conf
;
206 uint16_t max_queue_pairs
;
207 uint16_t curr_queue_pairs
;
210 char *netclient_name
;
211 char *netclient_type
;
212 uint64_t curr_guest_offloads
;
213 /* used on saved state restore phase to preserve the curr_guest_offloads */
214 uint64_t saved_guest_offloads
;
215 AnnounceTimer announce_timer
;
216 bool needs_vnet_hdr_swap
;
217 bool mtu_bypass_backend
;
218 /* primary failover device is hidden*/
219 bool failover_primary_hidden
;
221 DeviceListener primary_listener
;
223 bool primary_opts_from_json
;
224 NotifierWithReturn migration_state
;
225 VirtioNetRssData rss_data
;
226 struct NetRxPkt
*rx_pkt
;
227 struct EBPFRSSContext ebpf_rss
;
228 uint32_t nr_ebpf_rss_fds
;
232 size_t virtio_net_handle_ctrl_iov(VirtIODevice
*vdev
,
233 const struct iovec
*in_sg
, unsigned in_num
,
234 const struct iovec
*out_sg
,
236 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,
238 uint64_t virtio_net_supported_guest_offloads(const VirtIONet
*n
);