xref: /qemu/hw/net/net_rx_pkt.h (revision aac8f89dba870bb263d05bb84c3d64e0204be092)
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  * @data:           pointer to the data buffer to be parsed
59  * @len:            data length
60  *
61  */
62 void net_rx_pkt_set_protocols(struct NetRxPkt *pkt, const void *data,
63                               size_t len);
64 
65 /**
66  * fetches packet analysis results
67  *
68  * @pkt:            packet
69  * @isip4:          whether the packet given is IPv4
70  * @isip6:          whether the packet given is IPv6
71  * @isudp:          whether the packet given is UDP
72  * @istcp:          whether the packet given is TCP
73  *
74  */
75 void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
76                                  bool *isip4, bool *isip6,
77                                  bool *isudp, bool *istcp);
78 
79 /**
80 * fetches L3 header offset
81 *
82 * @pkt:            packet
83 *
84 */
85 size_t net_rx_pkt_get_l3_hdr_offset(struct NetRxPkt *pkt);
86 
87 /**
88 * fetches L4 header offset
89 *
90 * @pkt:            packet
91 *
92 */
93 size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt);
94 
95 /**
96 * fetches L5 header offset
97 *
98 * @pkt:            packet
99 *
100 */
101 size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt);
102 
103 /**
104  * fetches IP6 header analysis results
105  *
106  * Return:  pointer to analysis results structure which is stored in internal
107  *          packet area.
108  *
109  */
110 eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt);
111 
112 /**
113  * fetches IP4 header analysis results
114  *
115  * Return:  pointer to analysis results structure which is stored in internal
116  *          packet area.
117  *
118  */
119 eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt);
120 
121 /**
122  * fetches L4 header analysis results
123  *
124  * Return:  pointer to analysis results structure which is stored in internal
125  *          packet area.
126  *
127  */
128 eth_l4_hdr_info *net_rx_pkt_get_l4_info(struct NetRxPkt *pkt);
129 
130 typedef enum {
131     NetPktRssIpV4,
132     NetPktRssIpV4Tcp,
133     NetPktRssIpV6Tcp,
134     NetPktRssIpV6,
135     NetPktRssIpV6Ex,
136     NetPktRssIpV6TcpEx,
137     NetPktRssIpV4Udp,
138     NetPktRssIpV6Udp,
139     NetPktRssIpV6UdpEx,
140 } NetRxPktRssType;
141 
142 /**
143 * calculates RSS hash for packet
144 *
145 * @pkt:            packet
146 * @type:           RSS hash type
147 *
148 * Return:  Toeplitz RSS hash.
149 *
150 */
151 uint32_t
152 net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
153                          NetRxPktRssType type,
154                          uint8_t *key);
155 
156 /**
157 * fetches IP identification for the packet
158 *
159 * @pkt:            packet
160 *
161 */
162 uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt);
163 
164 /**
165 * check if given packet is a TCP ACK packet
166 *
167 * @pkt:            packet
168 *
169 */
170 bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt);
171 
172 /**
173 * check if given packet contains TCP data
174 *
175 * @pkt:            packet
176 *
177 */
178 bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt);
179 
180 /**
181  * returns virtio header stored in rx context
182  *
183  * @pkt:            packet
184  * @ret:            virtio header
185  *
186  */
187 struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt);
188 
189 /**
190  * returns packet type
191  *
192  * @pkt:            packet
193  * @ret:            packet type
194  *
195  */
196 eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt);
197 
198 /**
199  * returns vlan tag
200  *
201  * @pkt:            packet
202  * @ret:            VLAN tag
203  *
204  */
205 uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt);
206 
207 /**
208  * tells whether vlan was stripped from the packet
209  *
210  * @pkt:            packet
211  * @ret:            VLAN stripped sign
212  *
213  */
214 bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt);
215 
216 /**
217 * attach scatter-gather data to rx packet
218 *
219 * @pkt:            packet
220 * @iov:            received data scatter-gather list
221 * @iovcnt          number of elements in iov
222 * @iovoff          data start offset in the iov
223 * @strip_vlan:     should the module strip vlan from data
224 *
225 */
226 void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
227                                 const struct iovec *iov,
228                                 int iovcnt, size_t iovoff,
229                                 bool strip_vlan);
230 
231 /**
232 * attach scatter-gather data to rx packet
233 *
234 * @pkt:            packet
235 * @iov:            received data scatter-gather list
236 * @iovcnt          number of elements in iov
237 * @iovoff          data start offset in the iov
238 * @strip_vlan:     should the module strip vlan from data
239 * @vet:            VLAN tag Ethernet type
240 *
241 */
242 void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
243                                    const struct iovec *iov, int iovcnt,
244                                    size_t iovoff, bool strip_vlan,
245                                    uint16_t vet);
246 
247 /**
248  * attach data to rx packet
249  *
250  * @pkt:            packet
251  * @data:           pointer to the data buffer
252  * @len:            data length
253  * @strip_vlan:     should the module strip vlan from data
254  *
255  */
256 static inline void
257 net_rx_pkt_attach_data(struct NetRxPkt *pkt, const void *data,
258                           size_t len, bool strip_vlan)
259 {
260     const struct iovec iov = {
261         .iov_base = (void *) data,
262         .iov_len = len
263     };
264 
265     net_rx_pkt_attach_iovec(pkt, &iov, 1, 0, strip_vlan);
266 }
267 
268 /**
269  * returns io vector that holds the attached data
270  *
271  * @pkt:            packet
272  * @ret:            pointer to IOVec
273  *
274  */
275 struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt);
276 
277 /**
278 * returns io vector length that holds the attached data
279 *
280 * @pkt:            packet
281 * @ret:            IOVec length
282 *
283 */
284 uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt);
285 
286 /**
287  * prints rx packet data if debug is enabled
288  *
289  * @pkt:            packet
290  *
291  */
292 void net_rx_pkt_dump(struct NetRxPkt *pkt);
293 
294 /**
295  * copy passed vhdr data to packet context
296  *
297  * @pkt:            packet
298  * @vhdr:           VHDR buffer
299  *
300  */
301 void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
302     struct virtio_net_hdr *vhdr);
303 
304 /**
305 * copy passed vhdr data to packet context
306 *
307 * @pkt:            packet
308 * @iov:            VHDR iov
309 * @iovcnt:         VHDR iov array size
310 *
311 */
312 void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
313     const struct iovec *iov, int iovcnt);
314 
315 /**
316  * save packet type in packet context
317  *
318  * @pkt:            packet
319  * @packet_type:    the packet type
320  *
321  */
322 void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
323     eth_pkt_types_e packet_type);
324 
325 /**
326 * validate TCP/UDP checksum of the packet
327 *
328 * @pkt:            packet
329 * @csum_valid:     checksum validation result
330 * @ret:            true if validation was performed, false in case packet is
331 *                  not TCP/UDP or checksum validation is not possible
332 *
333 */
334 bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid);
335 
336 /**
337 * validate IPv4 checksum of the packet
338 *
339 * @pkt:            packet
340 * @csum_valid:     checksum validation result
341 * @ret:            true if validation was performed, false in case packet is
342 *                  not TCP/UDP or checksum validation is not possible
343 *
344 */
345 bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid);
346 
347 /**
348 * fix IPv4 checksum of the packet
349 *
350 * @pkt:            packet
351 * @ret:            true if checksum was fixed, false in case packet is
352 *                  not TCP/UDP or checksum correction is not possible
353 *
354 */
355 bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt);
356 
357 #endif
358