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