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