net: use SPDX tags
[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  * Parse an Ethernet packet to get its packet type.
33  *
34  * This function parses the network headers in mbuf data and return its
35  * packet type.
36  *
37  * If it is provided by the user, it also fills a rte_net_hdr_lens
38  * structure that contains the lengths of the parsed network
39  * headers. Each length field is valid only if the associated packet
40  * type is set. For instance, hdr_lens->l2_len is valid only if
41  * (retval & RTE_PTYPE_L2_MASK) != RTE_PTYPE_UNKNOWN.
42  *
43  * Supported packet types are:
44  *   L2: Ether, Vlan, QinQ
45  *   L3: IPv4, IPv6
46  *   L4: TCP, UDP, SCTP
47  *   Tunnels: IPv4, IPv6, Gre, Nvgre
48  *
49  * @param m
50  *   The packet mbuf to be parsed.
51  * @param hdr_lens
52  *   A pointer to a structure where the header lengths will be returned,
53  *   or NULL.
54  * @param layers
55  *   List of layers to parse. The function will stop at the first
56  *   empty layer. Examples:
57  *   - To parse all known layers, use RTE_PTYPE_ALL_MASK.
58  *   - To parse only L2 and L3, use RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK
59  * @return
60  *   The packet type of the packet.
61  */
62 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
63         struct rte_net_hdr_lens *hdr_lens, uint32_t layers);
64
65 /**
66  * Prepare pseudo header checksum
67  *
68  * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
69  * provided mbufs packet data and based on the requested offload flags.
70  *
71  * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
72  *   in packet data,
73  * - for TSO the IP payload length is not included in pseudo header.
74  *
75  * This function expects that used headers are in the first data segment of
76  * mbuf, are not fragmented and can be safely modified.
77  *
78  * @param m
79  *   The packet mbuf to be fixed.
80  * @param ol_flags
81  *   TX offloads flags to use with this packet.
82  * @return
83  *   0 if checksum is initialized properly
84  */
85 static inline int
86 rte_net_intel_cksum_flags_prepare(struct rte_mbuf *m, uint64_t ol_flags)
87 {
88         struct ipv4_hdr *ipv4_hdr;
89         struct ipv6_hdr *ipv6_hdr;
90         struct tcp_hdr *tcp_hdr;
91         struct udp_hdr *udp_hdr;
92         uint64_t inner_l3_offset = m->l2_len;
93
94         if ((ol_flags & PKT_TX_OUTER_IP_CKSUM) ||
95                 (ol_flags & PKT_TX_OUTER_IPV6))
96                 inner_l3_offset += m->outer_l2_len + m->outer_l3_len;
97
98         if ((ol_flags & PKT_TX_UDP_CKSUM) == PKT_TX_UDP_CKSUM) {
99                 if (ol_flags & PKT_TX_IPV4) {
100                         ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
101                                         inner_l3_offset);
102
103                         if (ol_flags & PKT_TX_IP_CKSUM)
104                                 ipv4_hdr->hdr_checksum = 0;
105
106                         udp_hdr = (struct udp_hdr *)((char *)ipv4_hdr +
107                                         m->l3_len);
108                         udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
109                                         ol_flags);
110                 } else {
111                         ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
112                                         inner_l3_offset);
113                         /* non-TSO udp */
114                         udp_hdr = rte_pktmbuf_mtod_offset(m, struct udp_hdr *,
115                                         inner_l3_offset + m->l3_len);
116                         udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
117                                         ol_flags);
118                 }
119         } else if ((ol_flags & PKT_TX_TCP_CKSUM) ||
120                         (ol_flags & PKT_TX_TCP_SEG)) {
121                 if (ol_flags & PKT_TX_IPV4) {
122                         ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
123                                         inner_l3_offset);
124
125                         if (ol_flags & PKT_TX_IP_CKSUM)
126                                 ipv4_hdr->hdr_checksum = 0;
127
128                         /* non-TSO tcp or TSO */
129                         tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr +
130                                         m->l3_len);
131                         tcp_hdr->cksum = rte_ipv4_phdr_cksum(ipv4_hdr,
132                                         ol_flags);
133                 } else {
134                         ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
135                                         inner_l3_offset);
136                         /* non-TSO tcp or TSO */
137                         tcp_hdr = rte_pktmbuf_mtod_offset(m, struct tcp_hdr *,
138                                         inner_l3_offset + m->l3_len);
139                         tcp_hdr->cksum = rte_ipv6_phdr_cksum(ipv6_hdr,
140                                         ol_flags);
141                 }
142         }
143
144         return 0;
145 }
146
147 /**
148  * Prepare pseudo header checksum
149  *
150  * This function prepares pseudo header checksum for TSO and non-TSO tcp/udp in
151  * provided mbufs packet data.
152  *
153  * - for non-TSO tcp/udp packets full pseudo-header checksum is counted and set
154  *   in packet data,
155  * - for TSO the IP payload length is not included in pseudo header.
156  *
157  * This function expects that used headers are in the first data segment of
158  * mbuf, are not fragmented and can be safely modified.
159  *
160  * @param m
161  *   The packet mbuf to be fixed.
162  * @return
163  *   0 if checksum is initialized properly
164  */
165 static inline int
166 rte_net_intel_cksum_prepare(struct rte_mbuf *m)
167 {
168         return rte_net_intel_cksum_flags_prepare(m, m->ol_flags);
169 }
170
171 #ifdef __cplusplus
172 }
173 #endif
174
175
176 #endif /* _RTE_NET_PTYPE_H_ */