1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
5 #ifndef _RTE_NET_PTYPE_H_
6 #define _RTE_NET_PTYPE_H_
17 * Structure containing header lengths associated to a packet, filled
18 * by rte_net_get_ptype().
20 struct rte_net_hdr_lens {
24 uint16_t inner_l3_len;
31 * Skip IPv6 header extensions.
33 * This function skips all IPv6 extensions, returning size of
34 * complete header including options and final protocol value.
37 * Protocol field of IPv6 header.
39 * The packet mbuf to be parsed.
41 * On input, must contain the offset to the first byte following
42 * IPv6 header, on output, contains offset to the first byte
43 * of next layer (after any IPv6 extension header)
45 * Contains 1 in output if packet is an IPv6 fragment.
47 * Protocol that follows IPv6 header.
48 * -1 if an error occurs during mbuf parsing.
51 rte_net_skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
55 * Parse an Ethernet packet to get its packet type.
57 * This function parses the network headers in mbuf data and return its
60 * If it is provided by the user, it also fills a rte_net_hdr_lens
61 * structure that contains the lengths of the parsed network
62 * headers. Each length field is valid only if the associated packet
63 * type is set. For instance, hdr_lens->l2_len is valid only if
64 * (retval & RTE_PTYPE_L2_MASK) != RTE_PTYPE_UNKNOWN.
66 * Supported packet types are:
67 * L2: Ether, Vlan, QinQ
70 * Tunnels: IPv4, IPv6, Gre, Nvgre
73 * The packet mbuf to be parsed.
75 * A pointer to a structure where the header lengths will be returned,
78 * List of layers to parse. The function will stop at the first
79 * empty layer. Examples:
80 * - To parse all known layers, use RTE_PTYPE_ALL_MASK.
81 * - To parse only L2 and L3, use RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK
83 * The packet type of the packet.
85 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
86 struct rte_net_hdr_lens *hdr_lens, uint32_t layers);
89 * Prepare pseudo header checksum
91 * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
92 * provided mbufs packet data and based on the requested offload flags.
94 * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
96 * - for TSO the IP payload length is not included in pseudo header.
98 * This function expects that used headers are in the first data segment of
99 * mbuf, are not fragmented and can be safely modified.
102 * The packet mbuf to be fixed.
104 * TX offloads flags to use with this packet.
106 * 0 if checksum is initialized properly
109 rte_net_intel_cksum_flags_prepare(struct rte_mbuf *m, uint64_t ol_flags)
111 /* Initialise ipv4_hdr to avoid false positive compiler warnings. */
112 struct rte_ipv4_hdr *ipv4_hdr = NULL;
113 struct rte_ipv6_hdr *ipv6_hdr;
114 struct rte_tcp_hdr *tcp_hdr;
115 struct rte_udp_hdr *udp_hdr;
116 uint64_t inner_l3_offset = m->l2_len;
119 * Does packet set any of available offloads?
120 * Mainly it is required to avoid fragmented headers check if
121 * no offloads are requested.
123 if (!(ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK | RTE_MBUF_F_TX_TCP_SEG |
124 RTE_MBUF_F_TX_OUTER_IP_CKSUM)))
127 if (ol_flags & (RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_OUTER_IPV6)) {
128 inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
130 * prepare outer IPv4 header checksum by setting it to 0,
131 * in order to be computed by hardware NICs.
133 if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) {
134 ipv4_hdr = rte_pktmbuf_mtod_offset(m,
135 struct rte_ipv4_hdr *, m->outer_l2_len);
136 ipv4_hdr->hdr_checksum = 0;
141 * Check if headers are fragmented.
142 * The check could be less strict depending on which offloads are
143 * requested and headers to be used, but let's keep it simple.
145 if (unlikely(rte_pktmbuf_data_len(m) <
146 inner_l3_offset + m->l3_len + m->l4_len))
149 if (ol_flags & RTE_MBUF_F_TX_IPV4) {
150 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
153 if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
154 ipv4_hdr->hdr_checksum = 0;
157 if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_UDP_CKSUM) {
158 if (ol_flags & RTE_MBUF_F_TX_IPV4) {
159 udp_hdr = (struct rte_udp_hdr *)((char *)ipv4_hdr +
161 udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
164 ipv6_hdr = rte_pktmbuf_mtod_offset(m,
165 struct rte_ipv6_hdr *, inner_l3_offset);
167 udp_hdr = rte_pktmbuf_mtod_offset(m,
168 struct rte_udp_hdr *,
169 inner_l3_offset + m->l3_len);
170 udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
173 } else if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_TCP_CKSUM ||
174 (ol_flags & RTE_MBUF_F_TX_TCP_SEG)) {
175 if (ol_flags & RTE_MBUF_F_TX_IPV4) {
176 /* non-TSO tcp or TSO */
177 tcp_hdr = (struct rte_tcp_hdr *)((char *)ipv4_hdr +
179 tcp_hdr->cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
182 ipv6_hdr = rte_pktmbuf_mtod_offset(m,
183 struct rte_ipv6_hdr *, inner_l3_offset);
184 /* non-TSO tcp or TSO */
185 tcp_hdr = rte_pktmbuf_mtod_offset(m,
186 struct rte_tcp_hdr *,
187 inner_l3_offset + m->l3_len);
188 tcp_hdr->cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
197 * Prepare pseudo header checksum
199 * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
200 * provided mbufs packet data.
202 * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
204 * - for TSO the IP payload length is not included in pseudo header.
206 * This function expects that used headers are in the first data segment of
207 * mbuf, are not fragmented and can be safely modified.
210 * The packet mbuf to be fixed.
212 * 0 if checksum is initialized properly
215 rte_net_intel_cksum_prepare(struct rte_mbuf *m)
217 return rte_net_intel_cksum_flags_prepare(m, m->ol_flags);
225 #endif /* _RTE_NET_PTYPE_H_ */