net: add rte prefix to IP defines
[dpdk.git] / lib / librte_net / rte_ip.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 1982, 1986, 1990, 1993
3  *      The Regents of the University of California.
4  * Copyright(c) 2010-2014 Intel Corporation.
5  * Copyright(c) 2014 6WIND S.A.
6  * All rights reserved.
7  */
8
9 #ifndef _RTE_IP_H_
10 #define _RTE_IP_H_
11
12 /**
13  * @file
14  *
15  * IP-related defines
16  */
17
18 #include <stdint.h>
19 #include <netinet/in.h>
20
21 #include <rte_byteorder.h>
22 #include <rte_mbuf.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 /**
29  * IPv4 Header
30  */
31 struct rte_ipv4_hdr {
32         uint8_t  version_ihl;           /**< version and header length */
33         uint8_t  type_of_service;       /**< type of service */
34         uint16_t total_length;          /**< length of packet */
35         uint16_t packet_id;             /**< packet ID */
36         uint16_t fragment_offset;       /**< fragmentation offset */
37         uint8_t  time_to_live;          /**< time to live */
38         uint8_t  next_proto_id;         /**< protocol ID */
39         uint16_t hdr_checksum;          /**< header checksum */
40         uint32_t src_addr;              /**< source address */
41         uint32_t dst_addr;              /**< destination address */
42 } __attribute__((__packed__));
43
44 /** Create IPv4 address */
45 #define RTE_IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
46                                            (((b) & 0xff) << 16) | \
47                                            (((c) & 0xff) << 8)  | \
48                                            ((d) & 0xff))
49
50 /** Maximal IPv4 packet length (including a header) */
51 #define RTE_IPV4_MAX_PKT_LEN        65535
52
53 /** Internet header length mask for version_ihl field */
54 #define RTE_IPV4_HDR_IHL_MASK   (0x0f)
55 /**
56  * Internet header length field multiplier (IHL field specifies overall header
57  * length in number of 4-byte words)
58  */
59 #define RTE_IPV4_IHL_MULTIPLIER (4)
60
61 /* Fragment Offset * Flags. */
62 #define RTE_IPV4_HDR_DF_SHIFT   14
63 #define RTE_IPV4_HDR_MF_SHIFT   13
64 #define RTE_IPV4_HDR_FO_SHIFT   3
65
66 #define RTE_IPV4_HDR_DF_FLAG    (1 << RTE_IPV4_HDR_DF_SHIFT)
67 #define RTE_IPV4_HDR_MF_FLAG    (1 << RTE_IPV4_HDR_MF_SHIFT)
68
69 #define RTE_IPV4_HDR_OFFSET_MASK        ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
70
71 #define RTE_IPV4_HDR_OFFSET_UNITS       8
72
73 /*
74  * IPv4 address types
75  */
76 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
77 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
78 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
79 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
80 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
81 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
82
83 /*
84  * IPv4 Multicast-related macros
85  */
86 #define RTE_IPV4_MIN_MCAST \
87         RTE_IPv4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
88 #define RTE_IPV4_MAX_MCAST \
89         RTE_IPv4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
90
91 #define RTE_IS_IPV4_MCAST(x) \
92         ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
93         /**< check if IPv4 address is multicast */
94
95 /**
96  * @internal Calculate a sum of all words in the buffer.
97  * Helper routine for the rte_raw_cksum().
98  *
99  * @param buf
100  *   Pointer to the buffer.
101  * @param len
102  *   Length of the buffer.
103  * @param sum
104  *   Initial value of the sum.
105  * @return
106  *   sum += Sum of all words in the buffer.
107  */
108 static inline uint32_t
109 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
110 {
111         /* workaround gcc strict-aliasing warning */
112         uintptr_t ptr = (uintptr_t)buf;
113         typedef uint16_t __attribute__((__may_alias__)) u16_p;
114         const u16_p *u16_buf = (const u16_p *)ptr;
115
116         while (len >= (sizeof(*u16_buf) * 4)) {
117                 sum += u16_buf[0];
118                 sum += u16_buf[1];
119                 sum += u16_buf[2];
120                 sum += u16_buf[3];
121                 len -= sizeof(*u16_buf) * 4;
122                 u16_buf += 4;
123         }
124         while (len >= sizeof(*u16_buf)) {
125                 sum += *u16_buf;
126                 len -= sizeof(*u16_buf);
127                 u16_buf += 1;
128         }
129
130         /* if length is in odd bytes */
131         if (len == 1)
132                 sum += *((const uint8_t *)u16_buf);
133
134         return sum;
135 }
136
137 /**
138  * @internal Reduce a sum to the non-complemented checksum.
139  * Helper routine for the rte_raw_cksum().
140  *
141  * @param sum
142  *   Value of the sum.
143  * @return
144  *   The non-complemented checksum.
145  */
146 static inline uint16_t
147 __rte_raw_cksum_reduce(uint32_t sum)
148 {
149         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
150         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
151         return (uint16_t)sum;
152 }
153
154 /**
155  * Process the non-complemented checksum of a buffer.
156  *
157  * @param buf
158  *   Pointer to the buffer.
159  * @param len
160  *   Length of the buffer.
161  * @return
162  *   The non-complemented checksum.
163  */
164 static inline uint16_t
165 rte_raw_cksum(const void *buf, size_t len)
166 {
167         uint32_t sum;
168
169         sum = __rte_raw_cksum(buf, len, 0);
170         return __rte_raw_cksum_reduce(sum);
171 }
172
173 /**
174  * Compute the raw (non complemented) checksum of a packet.
175  *
176  * @param m
177  *   The pointer to the mbuf.
178  * @param off
179  *   The offset in bytes to start the checksum.
180  * @param len
181  *   The length in bytes of the data to checksum.
182  * @param cksum
183  *   A pointer to the checksum, filled on success.
184  * @return
185  *   0 on success, -1 on error (bad length or offset).
186  */
187 static inline int
188 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
189         uint16_t *cksum)
190 {
191         const struct rte_mbuf *seg;
192         const char *buf;
193         uint32_t sum, tmp;
194         uint32_t seglen, done;
195
196         /* easy case: all data in the first segment */
197         if (off + len <= rte_pktmbuf_data_len(m)) {
198                 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
199                                 const char *, off), len);
200                 return 0;
201         }
202
203         if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
204                 return -1; /* invalid params, return a dummy value */
205
206         /* else browse the segment to find offset */
207         seglen = 0;
208         for (seg = m; seg != NULL; seg = seg->next) {
209                 seglen = rte_pktmbuf_data_len(seg);
210                 if (off < seglen)
211                         break;
212                 off -= seglen;
213         }
214         seglen -= off;
215         buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
216         if (seglen >= len) {
217                 /* all in one segment */
218                 *cksum = rte_raw_cksum(buf, len);
219                 return 0;
220         }
221
222         /* hard case: process checksum of several segments */
223         sum = 0;
224         done = 0;
225         for (;;) {
226                 tmp = __rte_raw_cksum(buf, seglen, 0);
227                 if (done & 1)
228                         tmp = rte_bswap16((uint16_t)tmp);
229                 sum += tmp;
230                 done += seglen;
231                 if (done == len)
232                         break;
233                 seg = seg->next;
234                 buf = rte_pktmbuf_mtod(seg, const char *);
235                 seglen = rte_pktmbuf_data_len(seg);
236                 if (seglen > len - done)
237                         seglen = len - done;
238         }
239
240         *cksum = __rte_raw_cksum_reduce(sum);
241         return 0;
242 }
243
244 /**
245  * Process the IPv4 checksum of an IPv4 header.
246  *
247  * The checksum field must be set to 0 by the caller.
248  *
249  * @param ipv4_hdr
250  *   The pointer to the contiguous IPv4 header.
251  * @return
252  *   The complemented checksum to set in the IP packet.
253  */
254 static inline uint16_t
255 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
256 {
257         uint16_t cksum;
258         cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
259         return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
260 }
261
262 /**
263  * Process the pseudo-header checksum of an IPv4 header.
264  *
265  * The checksum field must be set to 0 by the caller.
266  *
267  * Depending on the ol_flags, the pseudo-header checksum expected by the
268  * drivers is not the same. For instance, when TSO is enabled, the IP
269  * payload length must not be included in the packet.
270  *
271  * When ol_flags is 0, it computes the standard pseudo-header checksum.
272  *
273  * @param ipv4_hdr
274  *   The pointer to the contiguous IPv4 header.
275  * @param ol_flags
276  *   The ol_flags of the associated mbuf.
277  * @return
278  *   The non-complemented checksum to set in the L4 header.
279  */
280 static inline uint16_t
281 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
282 {
283         struct ipv4_psd_header {
284                 uint32_t src_addr; /* IP address of source host. */
285                 uint32_t dst_addr; /* IP address of destination host. */
286                 uint8_t  zero;     /* zero. */
287                 uint8_t  proto;    /* L4 protocol type. */
288                 uint16_t len;      /* L4 length. */
289         } psd_hdr;
290
291         psd_hdr.src_addr = ipv4_hdr->src_addr;
292         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
293         psd_hdr.zero = 0;
294         psd_hdr.proto = ipv4_hdr->next_proto_id;
295         if (ol_flags & PKT_TX_TCP_SEG) {
296                 psd_hdr.len = 0;
297         } else {
298                 psd_hdr.len = rte_cpu_to_be_16(
299                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
300                                 - sizeof(struct rte_ipv4_hdr)));
301         }
302         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
303 }
304
305 /**
306  * Process the IPv4 UDP or TCP checksum.
307  *
308  * The IPv4 header should not contains options. The IP and layer 4
309  * checksum must be set to 0 in the packet by the caller.
310  *
311  * @param ipv4_hdr
312  *   The pointer to the contiguous IPv4 header.
313  * @param l4_hdr
314  *   The pointer to the beginning of the L4 header.
315  * @return
316  *   The complemented checksum to set in the IP packet
317  *   or 0 on error
318  */
319 static inline uint16_t
320 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
321 {
322         uint32_t cksum;
323         uint32_t l3_len, l4_len;
324
325         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
326         if (l3_len < sizeof(struct rte_ipv4_hdr))
327                 return 0;
328
329         l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
330
331         cksum = rte_raw_cksum(l4_hdr, l4_len);
332         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
333
334         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
335         cksum = (~cksum) & 0xffff;
336         if (cksum == 0)
337                 cksum = 0xffff;
338
339         return (uint16_t)cksum;
340 }
341
342 /**
343  * IPv6 Header
344  */
345 struct rte_ipv6_hdr {
346         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
347         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
348         uint8_t  proto;        /**< Protocol, next header. */
349         uint8_t  hop_limits;   /**< Hop limits. */
350         uint8_t  src_addr[16]; /**< IP address of source host. */
351         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
352 } __attribute__((__packed__));
353
354 /* IPv6 vtc_flow: IPv / TC / flow_label */
355 #define RTE_IPV6_HDR_FL_SHIFT 0
356 #define RTE_IPV6_HDR_TC_SHIFT 20
357 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
358 #define RTE_IPV6_HDR_TC_MASK (0xf << RTE_IPV6_HDR_TC_SHIFT)
359
360 /**
361  * Process the pseudo-header checksum of an IPv6 header.
362  *
363  * Depending on the ol_flags, the pseudo-header checksum expected by the
364  * drivers is not the same. For instance, when TSO is enabled, the IPv6
365  * payload length must not be included in the packet.
366  *
367  * When ol_flags is 0, it computes the standard pseudo-header checksum.
368  *
369  * @param ipv6_hdr
370  *   The pointer to the contiguous IPv6 header.
371  * @param ol_flags
372  *   The ol_flags of the associated mbuf.
373  * @return
374  *   The non-complemented checksum to set in the L4 header.
375  */
376 static inline uint16_t
377 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
378 {
379         uint32_t sum;
380         struct {
381                 uint32_t len;   /* L4 length. */
382                 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
383         } psd_hdr;
384
385         psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
386         if (ol_flags & PKT_TX_TCP_SEG) {
387                 psd_hdr.len = 0;
388         } else {
389                 psd_hdr.len = ipv6_hdr->payload_len;
390         }
391
392         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
393                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
394                 0);
395         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
396         return __rte_raw_cksum_reduce(sum);
397 }
398
399 /**
400  * Process the IPv6 UDP or TCP checksum.
401  *
402  * The IPv4 header should not contains options. The layer 4 checksum
403  * must be set to 0 in the packet by the caller.
404  *
405  * @param ipv6_hdr
406  *   The pointer to the contiguous IPv6 header.
407  * @param l4_hdr
408  *   The pointer to the beginning of the L4 header.
409  * @return
410  *   The complemented checksum to set in the IP packet.
411  */
412 static inline uint16_t
413 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
414 {
415         uint32_t cksum;
416         uint32_t l4_len;
417
418         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
419
420         cksum = rte_raw_cksum(l4_hdr, l4_len);
421         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
422
423         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
424         cksum = (~cksum) & 0xffff;
425         if (cksum == 0)
426                 cksum = 0xffff;
427
428         return (uint16_t)cksum;
429 }
430
431 #ifdef __cplusplus
432 }
433 #endif
434
435 #endif /* _RTE_IP_H_ */