DPDK  17.02.1
rte_net.h
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright 2016 6WIND S.A.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_NET_PTYPE_H_
35 #define _RTE_NET_PTYPE_H_
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #include <rte_ip.h>
42 #include <rte_udp.h>
43 #include <rte_tcp.h>
44 #include <rte_sctp.h>
45 
51  uint8_t l2_len;
52  uint8_t l3_len;
53  uint8_t l4_len;
54  uint8_t tunnel_len;
55  uint8_t inner_l2_len;
56  uint8_t inner_l3_len;
57  uint8_t inner_l4_len;
58 };
59 
91 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
92  struct rte_net_hdr_lens *hdr_lens, uint32_t layers);
93 
114 static inline int
115 rte_net_intel_cksum_flags_prepare(struct rte_mbuf *m, uint64_t ol_flags)
116 {
117  struct ipv4_hdr *ipv4_hdr;
118  struct ipv6_hdr *ipv6_hdr;
119  struct tcp_hdr *tcp_hdr;
120  struct udp_hdr *udp_hdr;
121  uint64_t inner_l3_offset = m->l2_len;
122 
123  if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) ||
124  (ol_flags & PKT_TX_OUTER_IPV6))
125  inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
126 
127  if ((ol_flags & PKT_TX_UDP_CKSUM) == PKT_TX_UDP_CKSUM) {
128  if (ol_flags & PKT_TX_IPV4) {
129  ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
130  inner_l3_offset);
131 
132  if (ol_flags & PKT_TX_IP_CKSUM)
133  ipv4_hdr->hdr_checksum = 0;
134 
135  udp_hdr = (struct udp_hdr *)((char *)ipv4_hdr +
136  m->l3_len);
137  udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
138  ol_flags);
139  } else {
140  ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
141  inner_l3_offset);
142  /* non-TSO udp */
143  udp_hdr = rte_pktmbuf_mtod_offset(m, struct udp_hdr *,
144  inner_l3_offset + m->l3_len);
145  udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
146  ol_flags);
147  }
148  } else if ((ol_flags & PKT_TX_TCP_CKSUM) ||
149  (ol_flags & PKT_TX_TCP_SEG)) {
150  if (ol_flags & PKT_TX_IPV4) {
151  ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
152  inner_l3_offset);
153 
154  if (ol_flags & PKT_TX_IP_CKSUM)
155  ipv4_hdr->hdr_checksum = 0;
156 
157  /* non-TSO tcp or TSO */
158  tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr +
159  m->l3_len);
160  tcp_hdr->cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
161  ol_flags);
162  } else {
163  ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
164  inner_l3_offset);
165  /* non-TSO tcp or TSO */
166  tcp_hdr = rte_pktmbuf_mtod_offset(m, struct tcp_hdr *,
167  inner_l3_offset + m->l3_len);
168  tcp_hdr->cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
169  ol_flags);
170  }
171  }
172 
173  return 0;
174 }
175 
194 static inline int
195 rte_net_intel_cksum_prepare(struct rte_mbuf *m)
196 {
197  return rte_net_intel_cksum_flags_prepare(m, m->ol_flags);
198 }
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 
205 #endif /* _RTE_NET_PTYPE_H_ */