Branch data Line data Source code
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 : : #include <rte_ip.h>
9 : : #include <rte_udp.h>
10 : : #include <rte_tcp.h>
11 : :
12 : : #ifdef __cplusplus
13 : : extern "C" {
14 : : #endif
15 : :
16 : : /**
17 : : * Structure containing header lengths associated to a packet, filled
18 : : * by rte_net_get_ptype().
19 : : */
20 : : struct rte_net_hdr_lens {
21 : : uint8_t l2_len;
22 : : /* Outer_L4_len + ... + inner L2_len for tunneling pkt. */
23 : : uint8_t inner_l2_len;
24 : : uint16_t l3_len;
25 : : uint16_t inner_l3_len;
26 : : /* Protocol header of tunnel packets */
27 : : uint16_t tunnel_len;
28 : : uint8_t l4_len;
29 : : uint8_t inner_l4_len;
30 : : };
31 : :
32 : : /**
33 : : * Skip IPv6 header extensions.
34 : : *
35 : : * This function skips all IPv6 extensions, returning size of
36 : : * complete header including options and final protocol value.
37 : : *
38 : : * @param proto
39 : : * Protocol field of IPv6 header.
40 : : * @param m
41 : : * The packet mbuf to be parsed.
42 : : * @param off
43 : : * On input, must contain the offset to the first byte following
44 : : * IPv6 header, on output, contains offset to the first byte
45 : : * of next layer (after any IPv6 extension header)
46 : : * @param frag
47 : : * Contains 1 in output if packet is an IPv6 fragment.
48 : : * @return
49 : : * Protocol that follows IPv6 header.
50 : : * -1 if an error occurs during mbuf parsing.
51 : : */
52 : : int
53 : : rte_net_skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
54 : : int *frag);
55 : :
56 : : /**
57 : : * Parse an Ethernet packet to get its packet type.
58 : : *
59 : : * This function parses the network headers in mbuf data and return its
60 : : * packet type.
61 : : *
62 : : * If it is provided by the user, it also fills a rte_net_hdr_lens
63 : : * structure that contains the lengths of the parsed network
64 : : * headers. Each length field is valid only if the associated packet
65 : : * type is set. For instance, hdr_lens->l2_len is valid only if
66 : : * (retval & RTE_PTYPE_L2_MASK) != RTE_PTYPE_UNKNOWN.
67 : : *
68 : : * Supported packet types are:
69 : : * L2: Ether, Vlan, QinQ
70 : : * L3: IPv4, IPv6
71 : : * L4: TCP, UDP, SCTP
72 : : * Tunnels: IPv4, IPv6, Gre, Nvgre
73 : : *
74 : : * @param m
75 : : * The packet mbuf to be parsed.
76 : : * @param hdr_lens
77 : : * A pointer to a structure where the header lengths will be returned,
78 : : * or NULL.
79 : : * @param layers
80 : : * List of layers to parse. The function will stop at the first
81 : : * empty layer. Examples:
82 : : * - To parse all known layers, use RTE_PTYPE_ALL_MASK.
83 : : * - To parse only L2 and L3, use RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK
84 : : * @return
85 : : * The packet type of the packet.
86 : : */
87 : : uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
88 : : struct rte_net_hdr_lens *hdr_lens, uint32_t layers);
89 : :
90 : : /**
91 : : * Prepare pseudo header checksum
92 : : *
93 : : * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
94 : : * provided mbufs packet data and based on the requested offload flags.
95 : : *
96 : : * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
97 : : * in packet data,
98 : : * - for TSO the IP payload length is not included in pseudo header.
99 : : *
100 : : * This function expects that used headers are in the first data segment of
101 : : * mbuf, are not fragmented and can be safely modified.
102 : : *
103 : : * @param m
104 : : * The packet mbuf to be fixed.
105 : : * @param ol_flags
106 : : * TX offloads flags to use with this packet.
107 : : * @return
108 : : * 0 if checksum is initialized properly
109 : : */
110 : : static inline int
111 : 0 : rte_net_intel_cksum_flags_prepare(struct rte_mbuf *m, uint64_t ol_flags)
112 : : {
113 : : const uint64_t inner_requests = RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK |
114 : : RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_UDP_SEG;
115 : : const uint64_t outer_requests = RTE_MBUF_F_TX_OUTER_IP_CKSUM |
116 : : RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
117 : : /* Initialise ipv4_hdr to avoid false positive compiler warnings. */
118 : : struct rte_ipv4_hdr *ipv4_hdr = NULL;
119 : : struct rte_ipv6_hdr *ipv6_hdr;
120 : : struct rte_tcp_hdr *tcp_hdr;
121 : : struct rte_udp_hdr *udp_hdr;
122 : 0 : uint64_t inner_l3_offset = m->l2_len;
123 : :
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 [ # # ]: 0 : if (!(ol_flags & (inner_requests | outer_requests)))
130 : : return 0;
131 : :
132 [ # # ]: 0 : if (ol_flags & (RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_OUTER_IPV6)) {
133 : 0 : inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
134 : : /*
135 : : * prepare outer IPv4 header checksum by setting it to 0,
136 : : * in order to be computed by hardware NICs.
137 : : */
138 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) {
139 : 0 : ipv4_hdr = rte_pktmbuf_mtod_offset(m,
140 : : struct rte_ipv4_hdr *, m->outer_l2_len);
141 : 0 : ipv4_hdr->hdr_checksum = 0;
142 : : }
143 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM || ol_flags & inner_requests) {
144 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_OUTER_IPV4) {
145 : 0 : ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
146 : : m->outer_l2_len);
147 : 0 : udp_hdr = (struct rte_udp_hdr *)((char *)ipv4_hdr +
148 : 0 : m->outer_l3_len);
149 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM)
150 : 0 : udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
151 : : m->ol_flags);
152 [ # # ]: 0 : else if (ipv4_hdr->next_proto_id == IPPROTO_UDP)
153 : 0 : udp_hdr->dgram_cksum = 0;
154 : : } else {
155 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
156 : : m->outer_l2_len);
157 : 0 : udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
158 : : m->outer_l2_len + m->outer_l3_len);
159 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM)
160 : 0 : udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
161 : : m->ol_flags);
162 [ # # ]: 0 : else if (ipv6_hdr->proto == IPPROTO_UDP)
163 : 0 : udp_hdr->dgram_cksum = 0;
164 : : }
165 : : }
166 : : }
167 : :
168 : : /*
169 : : * Check if headers are fragmented.
170 : : * The check could be less strict depending on which offloads are
171 : : * requested and headers to be used, but let's keep it simple.
172 : : */
173 [ # # ]: 0 : if (unlikely(rte_pktmbuf_data_len(m) <
174 : : inner_l3_offset + m->l3_len + m->l4_len))
175 : : return -ENOTSUP;
176 : :
177 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_IPV4) {
178 : 0 : ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
179 : : inner_l3_offset);
180 : :
181 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
182 : 0 : ipv4_hdr->hdr_checksum = 0;
183 : : }
184 : :
185 [ # # ]: 0 : if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_UDP_CKSUM ||
186 [ # # ]: 0 : (ol_flags & RTE_MBUF_F_TX_UDP_SEG)) {
187 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_IPV4) {
188 : 0 : udp_hdr = (struct rte_udp_hdr *)((char *)ipv4_hdr +
189 : 0 : m->l3_len);
190 : 0 : udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
191 : : ol_flags);
192 : : } else {
193 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(m,
194 : : struct rte_ipv6_hdr *, inner_l3_offset);
195 : : /* non-TSO udp */
196 : 0 : udp_hdr = rte_pktmbuf_mtod_offset(m,
197 : : struct rte_udp_hdr *,
198 : : inner_l3_offset + m->l3_len);
199 : 0 : udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
200 : : ol_flags);
201 : : }
202 [ # # ]: 0 : } else if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_TCP_CKSUM ||
203 [ # # ]: 0 : (ol_flags & RTE_MBUF_F_TX_TCP_SEG)) {
204 [ # # ]: 0 : if (ol_flags & RTE_MBUF_F_TX_IPV4) {
205 : : /* non-TSO tcp or TSO */
206 : 0 : tcp_hdr = (struct rte_tcp_hdr *)((char *)ipv4_hdr +
207 : 0 : m->l3_len);
208 : 0 : tcp_hdr->cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
209 : : ol_flags);
210 : : } else {
211 : 0 : ipv6_hdr = rte_pktmbuf_mtod_offset(m,
212 : : struct rte_ipv6_hdr *, inner_l3_offset);
213 : : /* non-TSO tcp or TSO */
214 : 0 : tcp_hdr = rte_pktmbuf_mtod_offset(m,
215 : : struct rte_tcp_hdr *,
216 : : inner_l3_offset + m->l3_len);
217 : 0 : tcp_hdr->cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
218 : : ol_flags);
219 : : }
220 : : }
221 : :
222 : : return 0;
223 : : }
224 : :
225 : : /**
226 : : * Prepare pseudo header checksum
227 : : *
228 : : * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
229 : : * provided mbufs packet data.
230 : : *
231 : : * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
232 : : * in packet data,
233 : : * - for TSO the IP payload length is not included in pseudo header.
234 : : *
235 : : * This function expects that used headers are in the first data segment of
236 : : * mbuf, are not fragmented and can be safely modified.
237 : : *
238 : : * @param m
239 : : * The packet mbuf to be fixed.
240 : : * @return
241 : : * 0 if checksum is initialized properly
242 : : */
243 : : static inline int
244 : 0 : rte_net_intel_cksum_prepare(struct rte_mbuf *m)
245 : : {
246 : 0 : return rte_net_intel_cksum_flags_prepare(m, m->ol_flags);
247 : : }
248 : :
249 : : #ifdef __cplusplus
250 : : }
251 : : #endif
252 : :
253 : :
254 : : #endif /* _RTE_NET_PTYPE_H_ */
|