1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef QEMU_VIRTIO_NET_H
15#define QEMU_VIRTIO_NET_H
16
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"
23
24#include "ebpf/ebpf_rss.h"
25
26#define TYPE_VIRTIO_NET "virtio-net-device"
27OBJECT_DECLARE_SIMPLE_TYPE(VirtIONet, VIRTIO_NET)
28
29#define TX_TIMER_INTERVAL 150000
30
31
32
33
34
35
36#define TX_BURST 256
37
38
39#define MAC_TABLE_ENTRIES 64
40
41typedef struct virtio_net_conf
42{
43 uint32_t txtimer;
44 int32_t txburst;
45 char *tx;
46 uint16_t rx_queue_size;
47 uint16_t tx_queue_size;
48 uint16_t mtu;
49 int32_t speed;
50 char *duplex_str;
51 uint8_t duplex;
52 char *primary_id_str;
53} virtio_net_conf;
54
55
56typedef enum {
57 RSC_COALESCE,
58 RSC_FINAL,
59 RSC_NO_MATCH,
60 RSC_BYPASS,
61 RSC_CANDIDATE
62} CoalesceStatus;
63
64typedef struct VirtioNetRscStat {
65 uint32_t received;
66 uint32_t coalesced;
67 uint32_t over_size;
68 uint32_t cache;
69 uint32_t empty_cache;
70 uint32_t no_match_cache;
71 uint32_t win_update;
72 uint32_t no_match;
73 uint32_t tcp_syn;
74 uint32_t tcp_ctrl_drain;
75 uint32_t dup_ack;
76 uint32_t dup_ack1;
77 uint32_t dup_ack2;
78 uint32_t pure_ack;
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;
84 uint32_t tcp_option;
85 uint32_t tcp_all_opt;
86 uint32_t ip_frag;
87 uint32_t ip_ecn;
88 uint32_t ip_hacked;
89 uint32_t ip_option;
90 uint32_t purge_failed;
91 uint32_t drain_failed;
92 uint32_t final_failed;
93 int64_t timer;
94} VirtioNetRscStat;
95
96
97typedef struct VirtioNetRscUnit {
98 void *ip;
99 uint16_t *ip_plen;
100 struct tcp_header *tcp;
101 uint16_t tcp_hdrlen;
102 uint16_t payload;
103} VirtioNetRscUnit;
104
105
106typedef struct VirtioNetRscSeg {
107 QTAILQ_ENTRY(VirtioNetRscSeg) next;
108 void *buf;
109 size_t size;
110 uint16_t packets;
111 uint16_t dup_ack;
112 bool is_coalesced;
113 VirtioNetRscUnit unit;
114 NetClientState *nc;
115} VirtioNetRscSeg;
116
117
118
119typedef struct VirtioNetRscChain {
120 QTAILQ_ENTRY(VirtioNetRscChain) next;
121 VirtIONet *n;
122 uint16_t proto;
123 uint8_t gso_type;
124 uint16_t max_payload;
125 QEMUTimer *drain_timer;
126 QTAILQ_HEAD(, VirtioNetRscSeg) buffers;
127 VirtioNetRscStat stat;
128} VirtioNetRscChain;
129
130
131#define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
132
133#define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
134#define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
135
136typedef struct VirtioNetRssData {
137 bool enabled;
138 bool enabled_software_rss;
139 bool redirect;
140 bool populate_hash;
141 uint32_t hash_types;
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;
146} VirtioNetRssData;
147
148typedef struct VirtIONetQueue {
149 VirtQueue *rx_vq;
150 VirtQueue *tx_vq;
151 QEMUTimer *tx_timer;
152 QEMUBH *tx_bh;
153 uint32_t tx_waiting;
154 struct {
155 VirtQueueElement *elem;
156 } async_tx;
157 struct VirtIONet *n;
158} VirtIONetQueue;
159
160struct VirtIONet {
161 VirtIODevice parent_obj;
162 uint8_t mac[ETH_ALEN];
163 uint16_t status;
164 VirtIONetQueue *vqs;
165 VirtQueue *ctrl_vq;
166 NICState *nic;
167
168
169 QTAILQ_HEAD(, VirtioNetRscChain) rsc_chains;
170 uint32_t tx_timeout;
171 int32_t tx_burst;
172 uint32_t has_vnet_hdr;
173 size_t host_hdr_len;
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;
179 uint8_t has_ufo;
180 uint32_t mergeable_rx_bufs;
181 uint8_t promisc;
182 uint8_t allmulti;
183 uint8_t alluni;
184 uint8_t nomulti;
185 uint8_t nouni;
186 uint8_t nobcast;
187 uint8_t vhost_started;
188 struct {
189 uint32_t in_use;
190 uint32_t first_multi;
191 uint8_t multi_overflow;
192 uint8_t uni_overflow;
193 uint8_t *macs;
194 } mac_table;
195 uint32_t *vlans;
196 virtio_net_conf net_conf;
197 NICConf nic_conf;
198 DeviceState *qdev;
199 int multiqueue;
200 uint16_t max_queue_pairs;
201 uint16_t curr_queue_pairs;
202 uint16_t max_ncs;
203 size_t config_size;
204 char *netclient_name;
205 char *netclient_type;
206 uint64_t curr_guest_offloads;
207
208 uint64_t saved_guest_offloads;
209 AnnounceTimer announce_timer;
210 bool needs_vnet_hdr_swap;
211 bool mtu_bypass_backend;
212
213 bool failover_primary_hidden;
214 bool failover;
215 DeviceListener primary_listener;
216 QDict *primary_opts;
217 bool primary_opts_from_json;
218 Notifier migration_state;
219 VirtioNetRssData rss_data;
220 struct NetRxPkt *rx_pkt;
221 struct EBPFRSSContext ebpf_rss;
222};
223
224size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
225 const struct iovec *in_sg, unsigned in_num,
226 const struct iovec *out_sg,
227 unsigned out_num);
228void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
229 const char *type);
230
231#endif
232