8382d0fac7a03ad71566593123636467693bf7bf
[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 <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22
23 #include <rte_byteorder.h>
24 #include <rte_mbuf.h>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 /**
31  * IPv4 Header
32  */
33 struct rte_ipv4_hdr {
34         uint8_t  version_ihl;           /**< version and header length */
35         uint8_t  type_of_service;       /**< type of service */
36         rte_be16_t total_length;        /**< length of packet */
37         rte_be16_t packet_id;           /**< packet ID */
38         rte_be16_t fragment_offset;     /**< fragmentation offset */
39         uint8_t  time_to_live;          /**< time to live */
40         uint8_t  next_proto_id;         /**< protocol ID */
41         rte_be16_t hdr_checksum;        /**< header checksum */
42         rte_be32_t src_addr;            /**< source address */
43         rte_be32_t dst_addr;            /**< destination address */
44 } __rte_packed;
45
46 /** Create IPv4 address */
47 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
48                                            (((b) & 0xff) << 16) | \
49                                            (((c) & 0xff) << 8)  | \
50                                            ((d) & 0xff))
51
52 /** Maximal IPv4 packet length (including a header) */
53 #define RTE_IPV4_MAX_PKT_LEN        65535
54
55 /** Internet header length mask for version_ihl field */
56 #define RTE_IPV4_HDR_IHL_MASK   (0x0f)
57 /**
58  * Internet header length field multiplier (IHL field specifies overall header
59  * length in number of 4-byte words)
60  */
61 #define RTE_IPV4_IHL_MULTIPLIER (4)
62
63 /* Type of Service fields */
64 #define RTE_IPV4_HDR_DSCP_MASK  (0xfc)
65 #define RTE_IPV4_HDR_ECN_MASK   (0x03)
66 #define RTE_IPV4_HDR_ECN_CE     RTE_IPV4_HDR_ECN_MASK
67
68 /* Fragment Offset * Flags. */
69 #define RTE_IPV4_HDR_DF_SHIFT   14
70 #define RTE_IPV4_HDR_MF_SHIFT   13
71 #define RTE_IPV4_HDR_FO_SHIFT   3
72
73 #define RTE_IPV4_HDR_DF_FLAG    (1 << RTE_IPV4_HDR_DF_SHIFT)
74 #define RTE_IPV4_HDR_MF_FLAG    (1 << RTE_IPV4_HDR_MF_SHIFT)
75
76 #define RTE_IPV4_HDR_OFFSET_MASK        ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
77
78 #define RTE_IPV4_HDR_OFFSET_UNITS       8
79
80 /*
81  * IPv4 address types
82  */
83 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
84 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
85 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
86 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
87 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
88 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
89
90 /*
91  * IPv4 Multicast-related macros
92  */
93 #define RTE_IPV4_MIN_MCAST \
94         RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
95 #define RTE_IPV4_MAX_MCAST \
96         RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
97
98 #define RTE_IS_IPV4_MCAST(x) \
99         ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
100         /**< check if IPv4 address is multicast */
101
102 /* IPv4 default fields values */
103 #define RTE_IPV4_MIN_IHL    (0x5)
104 #define RTE_IPV4_VHL_DEF    ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
105
106 /**
107  * @internal Calculate a sum of all words in the buffer.
108  * Helper routine for the rte_raw_cksum().
109  *
110  * @param buf
111  *   Pointer to the buffer.
112  * @param len
113  *   Length of the buffer.
114  * @param sum
115  *   Initial value of the sum.
116  * @return
117  *   sum += Sum of all words in the buffer.
118  */
119 static inline uint32_t
120 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
121 {
122         /* workaround gcc strict-aliasing warning */
123         uintptr_t ptr = (uintptr_t)buf;
124         typedef uint16_t __attribute__((__may_alias__)) u16_p;
125         const u16_p *u16_buf = (const u16_p *)ptr;
126
127         while (len >= (sizeof(*u16_buf) * 4)) {
128                 sum += u16_buf[0];
129                 sum += u16_buf[1];
130                 sum += u16_buf[2];
131                 sum += u16_buf[3];
132                 len -= sizeof(*u16_buf) * 4;
133                 u16_buf += 4;
134         }
135         while (len >= sizeof(*u16_buf)) {
136                 sum += *u16_buf;
137                 len -= sizeof(*u16_buf);
138                 u16_buf += 1;
139         }
140
141         /* if length is in odd bytes */
142         if (len == 1) {
143                 uint16_t left = 0;
144                 *(uint8_t *)&left = *(const uint8_t *)u16_buf;
145                 sum += left;
146         }
147
148         return sum;
149 }
150
151 /**
152  * @internal Reduce a sum to the non-complemented checksum.
153  * Helper routine for the rte_raw_cksum().
154  *
155  * @param sum
156  *   Value of the sum.
157  * @return
158  *   The non-complemented checksum.
159  */
160 static inline uint16_t
161 __rte_raw_cksum_reduce(uint32_t sum)
162 {
163         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
164         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
165         return (uint16_t)sum;
166 }
167
168 /**
169  * Process the non-complemented checksum of a buffer.
170  *
171  * @param buf
172  *   Pointer to the buffer.
173  * @param len
174  *   Length of the buffer.
175  * @return
176  *   The non-complemented checksum.
177  */
178 static inline uint16_t
179 rte_raw_cksum(const void *buf, size_t len)
180 {
181         uint32_t sum;
182
183         sum = __rte_raw_cksum(buf, len, 0);
184         return __rte_raw_cksum_reduce(sum);
185 }
186
187 /**
188  * Compute the raw (non complemented) checksum of a packet.
189  *
190  * @param m
191  *   The pointer to the mbuf.
192  * @param off
193  *   The offset in bytes to start the checksum.
194  * @param len
195  *   The length in bytes of the data to checksum.
196  * @param cksum
197  *   A pointer to the checksum, filled on success.
198  * @return
199  *   0 on success, -1 on error (bad length or offset).
200  */
201 static inline int
202 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
203         uint16_t *cksum)
204 {
205         const struct rte_mbuf *seg;
206         const char *buf;
207         uint32_t sum, tmp;
208         uint32_t seglen, done;
209
210         /* easy case: all data in the first segment */
211         if (off + len <= rte_pktmbuf_data_len(m)) {
212                 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
213                                 const char *, off), len);
214                 return 0;
215         }
216
217         if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
218                 return -1; /* invalid params, return a dummy value */
219
220         /* else browse the segment to find offset */
221         seglen = 0;
222         for (seg = m; seg != NULL; seg = seg->next) {
223                 seglen = rte_pktmbuf_data_len(seg);
224                 if (off < seglen)
225                         break;
226                 off -= seglen;
227         }
228         RTE_ASSERT(seg != NULL);
229         if (seg == NULL)
230                 return -1;
231         seglen -= off;
232         buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
233         if (seglen >= len) {
234                 /* all in one segment */
235                 *cksum = rte_raw_cksum(buf, len);
236                 return 0;
237         }
238
239         /* hard case: process checksum of several segments */
240         sum = 0;
241         done = 0;
242         for (;;) {
243                 tmp = __rte_raw_cksum(buf, seglen, 0);
244                 if (done & 1)
245                         tmp = rte_bswap16((uint16_t)tmp);
246                 sum += tmp;
247                 done += seglen;
248                 if (done == len)
249                         break;
250                 seg = seg->next;
251                 buf = rte_pktmbuf_mtod(seg, const char *);
252                 seglen = rte_pktmbuf_data_len(seg);
253                 if (seglen > len - done)
254                         seglen = len - done;
255         }
256
257         *cksum = __rte_raw_cksum_reduce(sum);
258         return 0;
259 }
260
261 /**
262  * Process the IPv4 checksum of an IPv4 header.
263  *
264  * The checksum field must be set to 0 by the caller.
265  *
266  * @param ipv4_hdr
267  *   The pointer to the contiguous IPv4 header.
268  * @return
269  *   The complemented checksum to set in the IP packet.
270  */
271 static inline uint16_t
272 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
273 {
274         uint16_t cksum;
275         cksum = rte_raw_cksum(ipv4_hdr, (ipv4_hdr->version_ihl & 0xf) * 4);
276         return (uint16_t)~cksum;
277 }
278
279 /**
280  * Process the pseudo-header checksum of an IPv4 header.
281  *
282  * The checksum field must be set to 0 by the caller.
283  *
284  * Depending on the ol_flags, the pseudo-header checksum expected by the
285  * drivers is not the same. For instance, when TSO is enabled, the IP
286  * payload length must not be included in the packet.
287  *
288  * When ol_flags is 0, it computes the standard pseudo-header checksum.
289  *
290  * @param ipv4_hdr
291  *   The pointer to the contiguous IPv4 header.
292  * @param ol_flags
293  *   The ol_flags of the associated mbuf.
294  * @return
295  *   The non-complemented checksum to set in the L4 header.
296  */
297 static inline uint16_t
298 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
299 {
300         struct ipv4_psd_header {
301                 uint32_t src_addr; /* IP address of source host. */
302                 uint32_t dst_addr; /* IP address of destination host. */
303                 uint8_t  zero;     /* zero. */
304                 uint8_t  proto;    /* L4 protocol type. */
305                 uint16_t len;      /* L4 length. */
306         } psd_hdr;
307
308         uint32_t l3_len;
309         uint8_t ip_hdr_len;
310
311         psd_hdr.src_addr = ipv4_hdr->src_addr;
312         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
313         psd_hdr.zero = 0;
314         psd_hdr.proto = ipv4_hdr->next_proto_id;
315         if (ol_flags & PKT_TX_TCP_SEG) {
316                 psd_hdr.len = 0;
317         } else {
318                 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
319                 ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
320                 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len - ip_hdr_len));
321         }
322         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
323 }
324
325 /**
326  * Process the IPv4 UDP or TCP checksum.
327  *
328  * The IP and layer 4 checksum must be set to 0 in the packet by
329  * the caller.
330  *
331  * @param ipv4_hdr
332  *   The pointer to the contiguous IPv4 header.
333  * @param l4_hdr
334  *   The pointer to the beginning of the L4 header.
335  * @return
336  *   The complemented checksum to set in the IP packet.
337  */
338 static inline uint16_t
339 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
340 {
341         uint32_t cksum;
342         uint32_t l3_len, l4_len;
343         uint8_t ip_hdr_len;
344
345         ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
346         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
347         if (l3_len < ip_hdr_len)
348                 return 0;
349
350         l4_len = l3_len - ip_hdr_len;
351
352         cksum = rte_raw_cksum(l4_hdr, l4_len);
353         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
354
355         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
356         cksum = (~cksum) & 0xffff;
357         /*
358          * Per RFC 768:If the computed checksum is zero for UDP,
359          * it is transmitted as all ones
360          * (the equivalent in one's complement arithmetic).
361          */
362         if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
363                 cksum = 0xffff;
364
365         return (uint16_t)cksum;
366 }
367
368 /**
369  * IPv6 Header
370  */
371 struct rte_ipv6_hdr {
372         rte_be32_t vtc_flow;    /**< IP version, traffic class & flow label. */
373         rte_be16_t payload_len; /**< IP packet length - includes header size */
374         uint8_t  proto;         /**< Protocol, next header. */
375         uint8_t  hop_limits;    /**< Hop limits. */
376         uint8_t  src_addr[16];  /**< IP address of source host. */
377         uint8_t  dst_addr[16];  /**< IP address of destination host(s). */
378 } __rte_packed;
379
380 /* IPv6 vtc_flow: IPv / TC / flow_label */
381 #define RTE_IPV6_HDR_FL_SHIFT 0
382 #define RTE_IPV6_HDR_TC_SHIFT 20
383 #define RTE_IPV6_HDR_FL_MASK    ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
384 #define RTE_IPV6_HDR_TC_MASK    (0xff << RTE_IPV6_HDR_TC_SHIFT)
385 #define RTE_IPV6_HDR_DSCP_MASK  (0xfc << RTE_IPV6_HDR_TC_SHIFT)
386 #define RTE_IPV6_HDR_ECN_MASK   (0x03 << RTE_IPV6_HDR_TC_SHIFT)
387 #define RTE_IPV6_HDR_ECN_CE     RTE_IPV6_HDR_ECN_MASK
388
389 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
390
391 /**
392  * Process the pseudo-header checksum of an IPv6 header.
393  *
394  * Depending on the ol_flags, the pseudo-header checksum expected by the
395  * drivers is not the same. For instance, when TSO is enabled, the IPv6
396  * payload length must not be included in the packet.
397  *
398  * When ol_flags is 0, it computes the standard pseudo-header checksum.
399  *
400  * @param ipv6_hdr
401  *   The pointer to the contiguous IPv6 header.
402  * @param ol_flags
403  *   The ol_flags of the associated mbuf.
404  * @return
405  *   The non-complemented checksum to set in the L4 header.
406  */
407 static inline uint16_t
408 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
409 {
410         uint32_t sum;
411         struct {
412                 rte_be32_t len;   /* L4 length. */
413                 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
414         } psd_hdr;
415
416         psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
417         if (ol_flags & PKT_TX_TCP_SEG) {
418                 psd_hdr.len = 0;
419         } else {
420                 psd_hdr.len = ipv6_hdr->payload_len;
421         }
422
423         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
424                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
425                 0);
426         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
427         return __rte_raw_cksum_reduce(sum);
428 }
429
430 /**
431  * Process the IPv6 UDP or TCP checksum.
432  *
433  * The IPv4 header should not contains options. The layer 4 checksum
434  * must be set to 0 in the packet by the caller.
435  *
436  * @param ipv6_hdr
437  *   The pointer to the contiguous IPv6 header.
438  * @param l4_hdr
439  *   The pointer to the beginning of the L4 header.
440  * @return
441  *   The complemented checksum to set in the IP packet.
442  */
443 static inline uint16_t
444 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
445 {
446         uint32_t cksum;
447         uint32_t l4_len;
448
449         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
450
451         cksum = rte_raw_cksum(l4_hdr, l4_len);
452         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
453
454         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
455         cksum = (~cksum) & 0xffff;
456         /*
457          * Per RFC 768: If the computed checksum is zero for UDP,
458          * it is transmitted as all ones
459          * (the equivalent in one's complement arithmetic).
460          */
461         if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
462                 cksum = 0xffff;
463
464         return (uint16_t)cksum;
465 }
466
467 /* IPv6 fragmentation header size */
468 #define RTE_IPV6_FRAG_HDR_SIZE 8
469
470 /**
471  * Parse next IPv6 header extension
472  *
473  * This function checks if proto number is an IPv6 extensions and parses its
474  * data if so, providing information on next header and extension length.
475  *
476  * @param p
477  *   Pointer to an extension raw data.
478  * @param proto
479  *   Protocol number extracted from the "next header" field from
480  *   the IPv6 header or the previous extension.
481  * @param ext_len
482  *   Extension data length.
483  * @return
484  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
485  */
486 __rte_experimental
487 static inline int
488 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
489 {
490         int next_proto;
491
492         switch (proto) {
493         case IPPROTO_AH:
494                 next_proto = *p++;
495                 *ext_len = (*p + 2) * sizeof(uint32_t);
496                 break;
497
498         case IPPROTO_HOPOPTS:
499         case IPPROTO_ROUTING:
500         case IPPROTO_DSTOPTS:
501                 next_proto = *p++;
502                 *ext_len = (*p + 1) * sizeof(uint64_t);
503                 break;
504
505         case IPPROTO_FRAGMENT:
506                 next_proto = *p;
507                 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
508                 break;
509
510         default:
511                 return -EINVAL;
512         }
513
514         return next_proto;
515 }
516
517 #ifdef __cplusplus
518 }
519 #endif
520
521 #endif /* _RTE_IP_H_ */