xref: /openbmc/qemu/hw/net/net_rx_pkt.h (revision 2b74dd91)
1 /*
2  * QEMU RX packets abstraction
3  *
4  * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
5  *
6  * Developed by Daynix Computing LTD (http://www.daynix.com)
7  *
8  * Authors:
9  * Dmitry Fleytman <dmitry@daynix.com>
10  * Tamir Shomer <tamirs@daynix.com>
11  * Yan Vugenfirer <yan@daynix.com>
12  *
13  * This work is licensed under the terms of the GNU GPL, version 2 or later.
14  * See the COPYING file in the top-level directory.
15  *
16  */
17 
18 #ifndef NET_RX_PKT_H
19 #define NET_RX_PKT_H
20 
21 #include "net/eth.h"
22 
23 /* defines to enable packet dump functions */
24 /*#define NET_RX_PKT_DEBUG*/
25 
26 struct NetRxPkt;
27 
28 /**
29  * Clean all rx packet resources
30  *
31  * @pkt:            packet
32  *
33  */
34 void net_rx_pkt_uninit(struct NetRxPkt *pkt);
35 
36 /**
37  * Init function for rx packet functionality
38  *
39  * @pkt:            packet pointer
40  *
41  */
42 void net_rx_pkt_init(struct NetRxPkt **pkt);
43 
44 /**
45  * returns total length of data attached to rx context
46  *
47  * @pkt:            packet
48  *
49  * Return:  nothing
50  *
51  */
52 size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt);
53 
54 /**
55  * parse and set packet analysis results
56  *
57  * @pkt:            packet
58  * @iov:            received data scatter-gather list
59  * @iovcnt:         number of elements in iov
60  * @iovoff:         data start offset in the iov
61  *
62  */
63 void net_rx_pkt_set_protocols(struct NetRxPkt *pkt,
64                               const struct iovec *iov, size_t iovcnt,
65                               size_t iovoff);
66 
67 /**
68  * fetches packet analysis results
69  *
70  * @pkt:            packet
71  * @hasip4:          whether the packet has an IPv4 header
72  * @hasip6:          whether the packet has an IPv6 header
73  * @l4hdr_proto:     protocol of L4 header
74  *
75  */
76 void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
77                                  bool *hasip4, bool *hasip6,
78                                  EthL4HdrProto *l4hdr_proto);
79 
80 /**
81 * fetches L4 header offset
82 *
83 * @pkt:            packet
84 *
85 */
86 size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt);
87 
88 /**
89 * fetches L5 header offset
90 *
91 * @pkt:            packet
92 *
93 */
94 size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt);
95 
96 /**
97  * fetches IP6 header analysis results
98  *
99  * Return:  pointer to analysis results structure which is stored in internal
100  *          packet area.
101  *
102  */
103 eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt);
104 
105 /**
106  * fetches IP4 header analysis results
107  *
108  * Return:  pointer to analysis results structure which is stored in internal
109  *          packet area.
110  *
111  */
112 eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt);
113 
114 typedef enum {
115     NetPktRssIpV4,
116     NetPktRssIpV4Tcp,
117     NetPktRssIpV6Tcp,
118     NetPktRssIpV6,
119     NetPktRssIpV6Ex,
120     NetPktRssIpV6TcpEx,
121     NetPktRssIpV4Udp,
122     NetPktRssIpV6Udp,
123     NetPktRssIpV6UdpEx,
124 } NetRxPktRssType;
125 
126 /**
127 * calculates RSS hash for packet
128 *
129 * @pkt:            packet
130 * @type:           RSS hash type
131 *
132 * Return:  Toeplitz RSS hash.
133 *
134 */
135 uint32_t
136 net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
137                          NetRxPktRssType type,
138                          uint8_t *key);
139 
140 /**
141 * fetches IP identification for the packet
142 *
143 * @pkt:            packet
144 *
145 */
146 uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt);
147 
148 /**
149 * check if given packet is a TCP ACK packet
150 *
151 * @pkt:            packet
152 *
153 */
154 bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt);
155 
156 /**
157 * check if given packet contains TCP data
158 *
159 * @pkt:            packet
160 *
161 */
162 bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt);
163 
164 /**
165  * returns virtio header stored in rx context
166  *
167  * @pkt:            packet
168  * @ret:            virtio header
169  *
170  */
171 struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt);
172 
173 /**
174  * returns packet type
175  *
176  * @pkt:            packet
177  * @ret:            packet type
178  *
179  */
180 eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt);
181 
182 /**
183  * returns vlan tag
184  *
185  * @pkt:            packet
186  * @ret:            VLAN tag
187  *
188  */
189 uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt);
190 
191 /**
192  * tells whether vlan was stripped from the packet
193  *
194  * @pkt:            packet
195  * @ret:            VLAN stripped sign
196  *
197  */
198 bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt);
199 
200 /**
201 * attach scatter-gather data to rx packet
202 *
203 * @pkt:            packet
204 * @iov:            received data scatter-gather list
205 * @iovcnt          number of elements in iov
206 * @iovoff          data start offset in the iov
207 * @strip_vlan:     should the module strip vlan from data
208 *
209 */
210 void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
211                                 const struct iovec *iov,
212                                 int iovcnt, size_t iovoff,
213                                 bool strip_vlan);
214 
215 /**
216 * attach scatter-gather data to rx packet
217 *
218 * @pkt:              packet
219 * @iov:              received data scatter-gather list
220 * @iovcnt:           number of elements in iov
221 * @iovoff:           data start offset in the iov
222 * @strip_vlan_index: index of Q tag if it is to be stripped. negative otherwise.
223 * @vet:              VLAN tag Ethernet type
224 * @vet_ext:          outer VLAN tag Ethernet type
225 *
226 */
227 void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
228                                 const struct iovec *iov, int iovcnt,
229                                 size_t iovoff, int strip_vlan_index,
230                                 uint16_t vet, uint16_t vet_ext);
231 
232 /**
233  * attach data to rx packet
234  *
235  * @pkt:            packet
236  * @data:           pointer to the data buffer
237  * @len:            data length
238  * @strip_vlan:     should the module strip vlan from data
239  *
240  */
241 static inline void
242 net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
243                           size_t len, bool strip_vlan)
244 {
245     const struct iovec iov = {
246         .iov_base = (void *) data,
247         .iov_len = len
248     };
249 
250     net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
251 }
252 
253 /**
254  * returns io vector that holds the attached data
255  *
256  * @pkt:            packet
257  * @ret:            pointer to IOVec
258  *
259  */
260 struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
261 
262 /**
263  * prints rx packet data if debug is enabled
264  *
265  * @pkt:            packet
266  *
267  */
268 void net_rx_pkt_dump(struct NetRxPkt *pkt);
269 
270 /**
271  * copy passed vhdr data to packet context
272  *
273  * @pkt:            packet
274  * @vhdr:           VHDR buffer
275  *
276  */
277 void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
278     struct virtio_net_hdr *vhdr);
279 
280 /**
281 * copy passed vhdr data to packet context
282 *
283 * @pkt:            packet
284 * @iov:            VHDR iov
285 * @iovcnt:         VHDR iov array size
286 *
287 */
288 void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
289     const struct iovec *iov, int iovcnt);
290 
291 /**
292  * unset vhdr data from packet context
293  *
294  * @pkt:            packet
295  *
296  */
297 void net_rx_pkt_unset_vhdr(struct NetRxPkt *pkt);
298 
299 /**
300  * save packet type in packet context
301  *
302  * @pkt:            packet
303  * @packet_type:    the packet type
304  *
305  */
306 void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
307     eth_pkt_types_e packet_type);
308 
309 /**
310 * validate TCP/UDP checksum of the packet
311 *
312 * @pkt:            packet
313 * @csum_valid:     checksum validation result
314 * @ret:            true if validation was performed, false in case packet is
315 *                  not TCP/UDP or checksum validation is not possible
316 *
317 */
318 bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
319 
320 /**
321 * validate IPv4 checksum of the packet
322 *
323 * @pkt:            packet
324 * @csum_valid:     checksum validation result
325 * @ret:            true if validation was performed, false in case packet is
326 *                  not TCP/UDP or checksum validation is not possible
327 *
328 */
329 bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
330 
331 /**
332 * fix IPv4 checksum of the packet
333 *
334 * @pkt:            packet
335 * @ret:            true if checksum was fixed, false in case packet is
336 *                  not TCP/UDP or checksum correction is not possible
337 *
338 */
339 bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
340 
341 #endif
342