net: fix how L4 checksum choice is tested
[dpdk.git] / lib / librte_net / rte_net.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  */
4
5 #ifndef _RTE_NET_PTYPE_H_
6 #define _RTE_NET_PTYPE_H_
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 #include <rte_ip.h>
13 #include <rte_udp.h>
14 #include <rte_tcp.h>
15 #include <rte_sctp.h>
16
17 /**
18  * Structure containing header lengths associated to a packet, filled
19  * by rte_net_get_ptype().
20  */
21 struct rte_net_hdr_lens {
22         uint8_t l2_len;
23         uint8_t l3_len;
24         uint8_t l4_len;
25         uint8_t tunnel_len;
26         uint8_t inner_l2_len;
27         uint8_t inner_l3_len;
28         uint8_t inner_l4_len;
29 };
30
31 /**
32  * Skip IPv6 header extensions.
33  *
34  * This function skips all IPv6 extensions, returning size of
35  * complete header including options and final protocol value.
36  *
37  * @warning
38  * @b EXPERIMENTAL: this API may change without prior notice
39  *
40  * @param proto
41  *   Protocol field of IPv6 header.
42  * @param m
43  *   The packet mbuf to be parsed.
44  * @param off
45  *   On input, must contain the offset to the first byte following
46  *   IPv6 header, on output, contains offset to the first byte
47  *   of next layer (after any IPv6 extension header)
48  * @param frag
49  *   Contains 1 in output if packet is an IPv6 fragment.
50  * @return
51  *   Protocol that follows IPv6 header.
52  *   -1 if an error occurs during mbuf parsing.
53  */
54 __rte_experimental
55 int
56 rte_net_skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
57         int *frag);
58
59 /**
60  * Parse an Ethernet packet to get its packet type.
61  *
62  * This function parses the network headers in mbuf data and return its
63  * packet type.
64  *
65  * If it is provided by the user, it also fills a rte_net_hdr_lens
66  * structure that contains the lengths of the parsed network
67  * headers. Each length field is valid only if the associated packet
68  * type is set. For instance, hdr_lens->l2_len is valid only if
69  * (retval & RTE_PTYPE_L2_MASK) != RTE_PTYPE_UNKNOWN.
70  *
71  * Supported packet types are:
72  *   L2: Ether, Vlan, QinQ
73  *   L3: IPv4, IPv6
74  *   L4: TCP, UDP, SCTP
75  *   Tunnels: IPv4, IPv6, Gre, Nvgre
76  *
77  * @param m
78  *   The packet mbuf to be parsed.
79  * @param hdr_lens
80  *   A pointer to a structure where the header lengths will be returned,
81  *   or NULL.
82  * @param layers
83  *   List of layers to parse. The function will stop at the first
84  *   empty layer. Examples:
85  *   - To parse all known layers, use RTE_PTYPE_ALL_MASK.
86  *   - To parse only L2 and L3, use RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK
87  * @return
88  *   The packet type of the packet.
89  */
90 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
91         struct rte_net_hdr_lens *hdr_lens, uint32_t layers);
92
93 /**
94  * Prepare pseudo header checksum
95  *
96  * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
97  * provided mbufs packet data and based on the requested offload flags.
98  *
99  * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
100  *   in packet data,
101  * - for TSO the IP payload length is not included in pseudo header.
102  *
103  * This function expects that used headers are in the first data segment of
104  * mbuf, are not fragmented and can be safely modified.
105  *
106  * @param m
107  *   The packet mbuf to be fixed.
108  * @param ol_flags
109  *   TX offloads flags to use with this packet.
110  * @return
111  *   0 if checksum is initialized properly
112  */
113 static inline int
114 rte_net_intel_cksum_flags_prepare(struct rte_mbuf *m, uint64_t ol_flags)
115 {
116         /* Initialise ipv4_hdr to avoid false positive compiler warnings. */
117         struct rte_ipv4_hdr *ipv4_hdr = NULL;
118         struct rte_ipv6_hdr *ipv6_hdr;
119         struct rte_tcp_hdr *tcp_hdr;
120         struct rte_udp_hdr *udp_hdr;
121         uint64_t inner_l3_offset = m->l2_len;
122
123 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
124         /*
125          * Does packet set any of available offloads?
126          * Mainly it is required to avoid fragmented headers check if
127          * no offloads are requested.
128          */
129         if (!(ol_flags & PKT_TX_OFFLOAD_MASK))
130                 return 0;
131 #endif
132
133         if (ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6))
134                 inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
135
136 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
137         /*
138          * Check if headers are fragmented.
139          * The check could be less strict depending on which offloads are
140          * requested and headers to be used, but let's keep it simple.
141          */
142         if (unlikely(rte_pktmbuf_data_len(m) <
143                      inner_l3_offset + m->l3_len + m->l4_len))
144                 return -ENOTSUP;
145 #endif
146
147         if (ol_flags & PKT_TX_IPV4) {
148                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
149                                 inner_l3_offset);
150
151                 if (ol_flags & PKT_TX_IP_CKSUM)
152                         ipv4_hdr->hdr_checksum = 0;
153         }
154
155         if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM) {
156                 if (ol_flags & PKT_TX_IPV4) {
157                         udp_hdr = (struct rte_udp_hdr *)((char *)ipv4_hdr +
158                                         m->l3_len);
159                         udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
160                                         ol_flags);
161                 } else {
162                         ipv6_hdr = rte_pktmbuf_mtod_offset(m,
163                                 struct rte_ipv6_hdr *, inner_l3_offset);
164                         /* non-TSO udp */
165                         udp_hdr = rte_pktmbuf_mtod_offset(m,
166                                         struct rte_udp_hdr *,
167                                         inner_l3_offset + m->l3_len);
168                         udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
169                                         ol_flags);
170                 }
171         } else if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_TCP_CKSUM ||
172                         (ol_flags & PKT_TX_TCP_SEG)) {
173                 if (ol_flags & PKT_TX_IPV4) {
174                         /* non-TSO tcp or TSO */
175                         tcp_hdr = (struct rte_tcp_hdr *)((char *)ipv4_hdr +
176                                         m->l3_len);
177                         tcp_hdr->cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
178                                         ol_flags);
179                 } else {
180                         ipv6_hdr = rte_pktmbuf_mtod_offset(m,
181                                 struct rte_ipv6_hdr *, inner_l3_offset);
182                         /* non-TSO tcp or TSO */
183                         tcp_hdr = rte_pktmbuf_mtod_offset(m,
184                                         struct rte_tcp_hdr *,
185                                         inner_l3_offset + m->l3_len);
186                         tcp_hdr->cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
187                                         ol_flags);
188                 }
189         }
190
191         return 0;
192 }
193
194 /**
195  * Prepare pseudo header checksum
196  *
197  * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
198  * provided mbufs packet data.
199  *
200  * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
201  *   in packet data,
202  * - for TSO the IP payload length is not included in pseudo header.
203  *
204  * This function expects that used headers are in the first data segment of
205  * mbuf, are not fragmented and can be safely modified.
206  *
207  * @param m
208  *   The packet mbuf to be fixed.
209  * @return
210  *   0 if checksum is initialized properly
211  */
212 static inline int
213 rte_net_intel_cksum_prepare(struct rte_mbuf *m)
214 {
215         return rte_net_intel_cksum_flags_prepare(m, m->ol_flags);
216 }
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222
223 #endif /* _RTE_NET_PTYPE_H_ */