xref: /openbmc/qemu/include/hw/virtio/virtio-net.h (revision 5c5e0445)
1 /*
2  * Virtio Network Device
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
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"
27 OBJECT_DECLARE_SIMPLE_TYPE(VirtIONet, VIRTIO_NET)
28 
29 #define TX_TIMER_INTERVAL 150000 /* 150 us */
30 
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
35  * and latency. */
36 #define TX_BURST 256
37 
38 typedef struct virtio_net_conf
39 {
40     uint32_t txtimer;
41     int32_t txburst;
42     char *tx;
43     uint16_t rx_queue_size;
44     uint16_t tx_queue_size;
45     uint16_t mtu;
46     int32_t speed;
47     char *duplex_str;
48     uint8_t duplex;
49     char *primary_id_str;
50 } virtio_net_conf;
51 
52 /* Coalesced packets type & status */
53 typedef enum {
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 */
59 } CoalesceStatus;
60 
61 typedef struct VirtioNetRscStat {
62     uint32_t received;
63     uint32_t coalesced;
64     uint32_t over_size;
65     uint32_t cache;
66     uint32_t empty_cache;
67     uint32_t no_match_cache;
68     uint32_t win_update;
69     uint32_t no_match;
70     uint32_t tcp_syn;
71     uint32_t tcp_ctrl_drain;
72     uint32_t dup_ack;
73     uint32_t dup_ack1;
74     uint32_t dup_ack2;
75     uint32_t pure_ack;
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;
81     uint32_t tcp_option;
82     uint32_t tcp_all_opt;
83     uint32_t ip_frag;
84     uint32_t ip_ecn;
85     uint32_t ip_hacked;
86     uint32_t ip_option;
87     uint32_t purge_failed;
88     uint32_t drain_failed;
89     uint32_t final_failed;
90     int64_t  timer;
91 } VirtioNetRscStat;
92 
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 */
100 } VirtioNetRscUnit;
101 
102 /* Coalesced segment */
103 typedef struct VirtioNetRscSeg {
104     QTAILQ_ENTRY(VirtioNetRscSeg) next;
105     void *buf;
106     size_t size;
107     uint16_t packets;
108     uint16_t dup_ack;
109     bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
110     VirtioNetRscUnit unit;
111     NetClientState *nc;
112 } VirtioNetRscSeg;
113 
114 
115 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
116 typedef struct VirtioNetRscChain {
117     QTAILQ_ENTRY(VirtioNetRscChain) next;
118     VirtIONet *n;                            /* VirtIONet */
119     uint16_t proto;
120     uint8_t  gso_type;
121     uint16_t max_payload;
122     QEMUTimer *drain_timer;
123     QTAILQ_HEAD(, VirtioNetRscSeg) buffers;
124     VirtioNetRscStat stat;
125 } VirtioNetRscChain;
126 
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))
129 
130 #define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
131 #define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
132 
133 typedef struct VirtioNetRssData {
134     bool    enabled;
135     bool    enabled_software_rss;
136     bool    redirect;
137     bool    populate_hash;
138     uint32_t hash_types;
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;
143 } VirtioNetRssData;
144 
145 typedef struct VirtIONetQueue {
146     VirtQueue *rx_vq;
147     VirtQueue *tx_vq;
148     QEMUTimer *tx_timer;
149     QEMUBH *tx_bh;
150     uint32_t tx_waiting;
151     struct {
152         VirtQueueElement *elem;
153     } async_tx;
154     struct VirtIONet *n;
155 } VirtIONetQueue;
156 
157 struct VirtIONet {
158     VirtIODevice parent_obj;
159     uint8_t mac[ETH_ALEN];
160     uint16_t status;
161     VirtIONetQueue *vqs;
162     VirtQueue *ctrl_vq;
163     NICState *nic;
164     /* RSC Chains - temporary storage of coalesced data,
165        all these data are lost in case of migration */
166     QTAILQ_HEAD(, VirtioNetRscChain) rsc_chains;
167     uint32_t tx_timeout;
168     int32_t tx_burst;
169     uint32_t has_vnet_hdr;
170     size_t host_hdr_len;
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;
176     uint8_t has_ufo;
177     uint32_t mergeable_rx_bufs;
178     uint8_t promisc;
179     uint8_t allmulti;
180     uint8_t alluni;
181     uint8_t nomulti;
182     uint8_t nouni;
183     uint8_t nobcast;
184     uint8_t vhost_started;
185     struct {
186         uint32_t in_use;
187         uint32_t first_multi;
188         uint8_t multi_overflow;
189         uint8_t uni_overflow;
190         uint8_t *macs;
191     } mac_table;
192     uint32_t *vlans;
193     virtio_net_conf net_conf;
194     NICConf nic_conf;
195     DeviceState *qdev;
196     int multiqueue;
197     uint16_t max_queue_pairs;
198     uint16_t curr_queue_pairs;
199     uint16_t max_ncs;
200     size_t config_size;
201     char *netclient_name;
202     char *netclient_type;
203     uint64_t curr_guest_offloads;
204     /* used on saved state restore phase to preserve the curr_guest_offloads */
205     uint64_t saved_guest_offloads;
206     AnnounceTimer announce_timer;
207     bool needs_vnet_hdr_swap;
208     bool mtu_bypass_backend;
209     /* primary failover device is hidden*/
210     bool failover_primary_hidden;
211     bool failover;
212     DeviceListener primary_listener;
213     QDict *primary_opts;
214     bool primary_opts_from_json;
215     Notifier migration_state;
216     VirtioNetRssData rss_data;
217     struct NetRxPkt *rx_pkt;
218     struct EBPFRSSContext ebpf_rss;
219 };
220 
221 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
222                                    const char *type);
223 
224 #endif
225