xref: /openbmc/qemu/include/hw/virtio/virtio-net.h (revision f7160f32)
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 
23 #define TYPE_VIRTIO_NET "virtio-net-device"
24 #define VIRTIO_NET(obj) \
25         OBJECT_CHECK(VirtIONet, (obj), TYPE_VIRTIO_NET)
26 
27 #define TX_TIMER_INTERVAL 150000 /* 150 us */
28 
29 /* Limit the number of packets that can be sent via a single flush
30  * of the TX queue.  This gives us a guaranteed exit condition and
31  * ensures fairness in the io path.  256 conveniently matches the
32  * length of the TX queue and shows a good balance of performance
33  * and latency. */
34 #define TX_BURST 256
35 
36 typedef struct virtio_net_conf
37 {
38     uint32_t txtimer;
39     int32_t txburst;
40     char *tx;
41     uint16_t rx_queue_size;
42     uint16_t tx_queue_size;
43     uint16_t mtu;
44     int32_t speed;
45     char *duplex_str;
46     uint8_t duplex;
47     char *primary_id_str;
48 } virtio_net_conf;
49 
50 /* Coalesced packets type & status */
51 typedef enum {
52     RSC_COALESCE,           /* Data been coalesced */
53     RSC_FINAL,              /* Will terminate current connection */
54     RSC_NO_MATCH,           /* No matched in the buffer pool */
55     RSC_BYPASS,             /* Packet to be bypass, not tcp, tcp ctrl, etc */
56     RSC_CANDIDATE                /* Data want to be coalesced */
57 } CoalesceStatus;
58 
59 typedef struct VirtioNetRscStat {
60     uint32_t received;
61     uint32_t coalesced;
62     uint32_t over_size;
63     uint32_t cache;
64     uint32_t empty_cache;
65     uint32_t no_match_cache;
66     uint32_t win_update;
67     uint32_t no_match;
68     uint32_t tcp_syn;
69     uint32_t tcp_ctrl_drain;
70     uint32_t dup_ack;
71     uint32_t dup_ack1;
72     uint32_t dup_ack2;
73     uint32_t pure_ack;
74     uint32_t ack_out_of_win;
75     uint32_t data_out_of_win;
76     uint32_t data_out_of_order;
77     uint32_t data_after_pure_ack;
78     uint32_t bypass_not_tcp;
79     uint32_t tcp_option;
80     uint32_t tcp_all_opt;
81     uint32_t ip_frag;
82     uint32_t ip_ecn;
83     uint32_t ip_hacked;
84     uint32_t ip_option;
85     uint32_t purge_failed;
86     uint32_t drain_failed;
87     uint32_t final_failed;
88     int64_t  timer;
89 } VirtioNetRscStat;
90 
91 /* Rsc unit general info used to checking if can coalescing */
92 typedef struct VirtioNetRscUnit {
93     void *ip;   /* ip header */
94     uint16_t *ip_plen;      /* data len pointer in ip header field */
95     struct tcp_header *tcp; /* tcp header */
96     uint16_t tcp_hdrlen;    /* tcp header len */
97     uint16_t payload;       /* pure payload without virtio/eth/ip/tcp */
98 } VirtioNetRscUnit;
99 
100 /* Coalesced segment */
101 typedef struct VirtioNetRscSeg {
102     QTAILQ_ENTRY(VirtioNetRscSeg) next;
103     void *buf;
104     size_t size;
105     uint16_t packets;
106     uint16_t dup_ack;
107     bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
108     VirtioNetRscUnit unit;
109     NetClientState *nc;
110 } VirtioNetRscSeg;
111 
112 typedef struct VirtIONet VirtIONet;
113 
114 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
115 typedef struct VirtioNetRscChain {
116     QTAILQ_ENTRY(VirtioNetRscChain) next;
117     VirtIONet *n;                            /* VirtIONet */
118     uint16_t proto;
119     uint8_t  gso_type;
120     uint16_t max_payload;
121     QEMUTimer *drain_timer;
122     QTAILQ_HEAD(, VirtioNetRscSeg) buffers;
123     VirtioNetRscStat stat;
124 } VirtioNetRscChain;
125 
126 /* Maximum packet size we can receive from tap device: header + 64k */
127 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
128 
129 #define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
130 #define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
131 
132 typedef struct VirtioNetRssData {
133     bool    enabled;
134     bool    redirect;
135     bool    populate_hash;
136     uint32_t hash_types;
137     uint8_t key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
138     uint16_t indirections_len;
139     uint16_t *indirections_table;
140     uint16_t default_queue;
141 } VirtioNetRssData;
142 
143 typedef struct VirtIONetQueue {
144     VirtQueue *rx_vq;
145     VirtQueue *tx_vq;
146     QEMUTimer *tx_timer;
147     QEMUBH *tx_bh;
148     uint32_t tx_waiting;
149     struct {
150         VirtQueueElement *elem;
151     } async_tx;
152     struct VirtIONet *n;
153 } VirtIONetQueue;
154 
155 struct VirtIONet {
156     VirtIODevice parent_obj;
157     uint8_t mac[ETH_ALEN];
158     uint16_t status;
159     VirtIONetQueue *vqs;
160     VirtQueue *ctrl_vq;
161     NICState *nic;
162     /* RSC Chains - temporary storage of coalesced data,
163        all these data are lost in case of migration */
164     QTAILQ_HEAD(, VirtioNetRscChain) rsc_chains;
165     uint32_t tx_timeout;
166     int32_t tx_burst;
167     uint32_t has_vnet_hdr;
168     size_t host_hdr_len;
169     size_t guest_hdr_len;
170     uint64_t host_features;
171     uint32_t rsc_timeout;
172     uint8_t rsc4_enabled;
173     uint8_t rsc6_enabled;
174     uint8_t has_ufo;
175     uint32_t mergeable_rx_bufs;
176     uint8_t promisc;
177     uint8_t allmulti;
178     uint8_t alluni;
179     uint8_t nomulti;
180     uint8_t nouni;
181     uint8_t nobcast;
182     uint8_t vhost_started;
183     struct {
184         uint32_t in_use;
185         uint32_t first_multi;
186         uint8_t multi_overflow;
187         uint8_t uni_overflow;
188         uint8_t *macs;
189     } mac_table;
190     uint32_t *vlans;
191     virtio_net_conf net_conf;
192     NICConf nic_conf;
193     DeviceState *qdev;
194     int multiqueue;
195     uint16_t max_queues;
196     uint16_t curr_queues;
197     size_t config_size;
198     char *netclient_name;
199     char *netclient_type;
200     uint64_t curr_guest_offloads;
201     /* used on saved state restore phase to preserve the curr_guest_offloads */
202     uint64_t saved_guest_offloads;
203     AnnounceTimer announce_timer;
204     bool needs_vnet_hdr_swap;
205     bool mtu_bypass_backend;
206     QemuOpts *primary_device_opts;
207     QDict *primary_device_dict;
208     DeviceState *primary_dev;
209     BusState *primary_bus;
210     char *primary_device_id;
211     char *standby_id;
212     bool primary_should_be_hidden;
213     bool failover;
214     DeviceListener primary_listener;
215     Notifier migration_state;
216     VirtioNetRssData rss_data;
217     struct NetRxPkt *rx_pkt;
218 };
219 
220 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
221                                    const char *type);
222 
223 #endif
224