net: fix unneeded replacement of TCP checksum 0
[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                 sum += *((const uint8_t *)u16_buf);
144
145         return sum;
146 }
147
148 /**
149  * @internal Reduce a sum to the non-complemented checksum.
150  * Helper routine for the rte_raw_cksum().
151  *
152  * @param sum
153  *   Value of the sum.
154  * @return
155  *   The non-complemented checksum.
156  */
157 static inline uint16_t
158 __rte_raw_cksum_reduce(uint32_t sum)
159 {
160         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
161         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
162         return (uint16_t)sum;
163 }
164
165 /**
166  * Process the non-complemented checksum of a buffer.
167  *
168  * @param buf
169  *   Pointer to the buffer.
170  * @param len
171  *   Length of the buffer.
172  * @return
173  *   The non-complemented checksum.
174  */
175 static inline uint16_t
176 rte_raw_cksum(const void *buf, size_t len)
177 {
178         uint32_t sum;
179
180         sum = __rte_raw_cksum(buf, len, 0);
181         return __rte_raw_cksum_reduce(sum);
182 }
183
184 /**
185  * Compute the raw (non complemented) checksum of a packet.
186  *
187  * @param m
188  *   The pointer to the mbuf.
189  * @param off
190  *   The offset in bytes to start the checksum.
191  * @param len
192  *   The length in bytes of the data to checksum.
193  * @param cksum
194  *   A pointer to the checksum, filled on success.
195  * @return
196  *   0 on success, -1 on error (bad length or offset).
197  */
198 static inline int
199 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
200         uint16_t *cksum)
201 {
202         const struct rte_mbuf *seg;
203         const char *buf;
204         uint32_t sum, tmp;
205         uint32_t seglen, done;
206
207         /* easy case: all data in the first segment */
208         if (off + len <= rte_pktmbuf_data_len(m)) {
209                 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
210                                 const char *, off), len);
211                 return 0;
212         }
213
214         if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
215                 return -1; /* invalid params, return a dummy value */
216
217         /* else browse the segment to find offset */
218         seglen = 0;
219         for (seg = m; seg != NULL; seg = seg->next) {
220                 seglen = rte_pktmbuf_data_len(seg);
221                 if (off < seglen)
222                         break;
223                 off -= seglen;
224         }
225         seglen -= off;
226         buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
227         if (seglen >= len) {
228                 /* all in one segment */
229                 *cksum = rte_raw_cksum(buf, len);
230                 return 0;
231         }
232
233         /* hard case: process checksum of several segments */
234         sum = 0;
235         done = 0;
236         for (;;) {
237                 tmp = __rte_raw_cksum(buf, seglen, 0);
238                 if (done & 1)
239                         tmp = rte_bswap16((uint16_t)tmp);
240                 sum += tmp;
241                 done += seglen;
242                 if (done == len)
243                         break;
244                 seg = seg->next;
245                 buf = rte_pktmbuf_mtod(seg, const char *);
246                 seglen = rte_pktmbuf_data_len(seg);
247                 if (seglen > len - done)
248                         seglen = len - done;
249         }
250
251         *cksum = __rte_raw_cksum_reduce(sum);
252         return 0;
253 }
254
255 /**
256  * Process the IPv4 checksum of an IPv4 header.
257  *
258  * The checksum field must be set to 0 by the caller.
259  *
260  * @param ipv4_hdr
261  *   The pointer to the contiguous IPv4 header.
262  * @return
263  *   The complemented checksum to set in the IP packet.
264  */
265 static inline uint16_t
266 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
267 {
268         uint16_t cksum;
269         cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
270         return (uint16_t)~cksum;
271 }
272
273 /**
274  * Process the pseudo-header checksum of an IPv4 header.
275  *
276  * The checksum field must be set to 0 by the caller.
277  *
278  * Depending on the ol_flags, the pseudo-header checksum expected by the
279  * drivers is not the same. For instance, when TSO is enabled, the IP
280  * payload length must not be included in the packet.
281  *
282  * When ol_flags is 0, it computes the standard pseudo-header checksum.
283  *
284  * @param ipv4_hdr
285  *   The pointer to the contiguous IPv4 header.
286  * @param ol_flags
287  *   The ol_flags of the associated mbuf.
288  * @return
289  *   The non-complemented checksum to set in the L4 header.
290  */
291 static inline uint16_t
292 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
293 {
294         struct ipv4_psd_header {
295                 uint32_t src_addr; /* IP address of source host. */
296                 uint32_t dst_addr; /* IP address of destination host. */
297                 uint8_t  zero;     /* zero. */
298                 uint8_t  proto;    /* L4 protocol type. */
299                 uint16_t len;      /* L4 length. */
300         } psd_hdr;
301
302         psd_hdr.src_addr = ipv4_hdr->src_addr;
303         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
304         psd_hdr.zero = 0;
305         psd_hdr.proto = ipv4_hdr->next_proto_id;
306         if (ol_flags & PKT_TX_TCP_SEG) {
307                 psd_hdr.len = 0;
308         } else {
309                 psd_hdr.len = rte_cpu_to_be_16(
310                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
311                                 - sizeof(struct rte_ipv4_hdr)));
312         }
313         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
314 }
315
316 /**
317  * Process the IPv4 UDP or TCP checksum.
318  *
319  * The IPv4 header should not contains options. The IP and layer 4
320  * checksum must be set to 0 in the packet by the caller.
321  *
322  * @param ipv4_hdr
323  *   The pointer to the contiguous IPv4 header.
324  * @param l4_hdr
325  *   The pointer to the beginning of the L4 header.
326  * @return
327  *   The complemented checksum to set in the IP packet.
328  */
329 static inline uint16_t
330 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
331 {
332         uint32_t cksum;
333         uint32_t l3_len, l4_len;
334
335         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
336         if (l3_len < sizeof(struct rte_ipv4_hdr))
337                 return 0;
338
339         l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
340
341         cksum = rte_raw_cksum(l4_hdr, l4_len);
342         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
343
344         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
345         cksum = (~cksum) & 0xffff;
346         /*
347          * Per RFC 768:If the computed checksum is zero for UDP,
348          * it is transmitted as all ones
349          * (the equivalent in one's complement arithmetic).
350          */
351         if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
352                 cksum = 0xffff;
353
354         return (uint16_t)cksum;
355 }
356
357 /**
358  * IPv6 Header
359  */
360 struct rte_ipv6_hdr {
361         rte_be32_t vtc_flow;    /**< IP version, traffic class & flow label. */
362         rte_be16_t payload_len; /**< IP packet length - includes header size */
363         uint8_t  proto;         /**< Protocol, next header. */
364         uint8_t  hop_limits;    /**< Hop limits. */
365         uint8_t  src_addr[16];  /**< IP address of source host. */
366         uint8_t  dst_addr[16];  /**< IP address of destination host(s). */
367 } __rte_packed;
368
369 /* IPv6 vtc_flow: IPv / TC / flow_label */
370 #define RTE_IPV6_HDR_FL_SHIFT 0
371 #define RTE_IPV6_HDR_TC_SHIFT 20
372 #define RTE_IPV6_HDR_FL_MASK    ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
373 #define RTE_IPV6_HDR_TC_MASK    (0xff << RTE_IPV6_HDR_TC_SHIFT)
374 #define RTE_IPV6_HDR_DSCP_MASK  (0xfc << RTE_IPV6_HDR_TC_SHIFT)
375 #define RTE_IPV6_HDR_ECN_MASK   (0x03 << RTE_IPV6_HDR_TC_SHIFT)
376 #define RTE_IPV6_HDR_ECN_CE     RTE_IPV6_HDR_ECN_MASK
377
378 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
379
380 /**
381  * Process the pseudo-header checksum of an IPv6 header.
382  *
383  * Depending on the ol_flags, the pseudo-header checksum expected by the
384  * drivers is not the same. For instance, when TSO is enabled, the IPv6
385  * payload length must not be included in the packet.
386  *
387  * When ol_flags is 0, it computes the standard pseudo-header checksum.
388  *
389  * @param ipv6_hdr
390  *   The pointer to the contiguous IPv6 header.
391  * @param ol_flags
392  *   The ol_flags of the associated mbuf.
393  * @return
394  *   The non-complemented checksum to set in the L4 header.
395  */
396 static inline uint16_t
397 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
398 {
399         uint32_t sum;
400         struct {
401                 rte_be32_t len;   /* L4 length. */
402                 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
403         } psd_hdr;
404
405         psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
406         if (ol_flags & PKT_TX_TCP_SEG) {
407                 psd_hdr.len = 0;
408         } else {
409                 psd_hdr.len = ipv6_hdr->payload_len;
410         }
411
412         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
413                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
414                 0);
415         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
416         return __rte_raw_cksum_reduce(sum);
417 }
418
419 /**
420  * Process the IPv6 UDP or TCP checksum.
421  *
422  * The IPv4 header should not contains options. The layer 4 checksum
423  * must be set to 0 in the packet by the caller.
424  *
425  * @param ipv6_hdr
426  *   The pointer to the contiguous IPv6 header.
427  * @param l4_hdr
428  *   The pointer to the beginning of the L4 header.
429  * @return
430  *   The complemented checksum to set in the IP packet.
431  */
432 static inline uint16_t
433 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
434 {
435         uint32_t cksum;
436         uint32_t l4_len;
437
438         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
439
440         cksum = rte_raw_cksum(l4_hdr, l4_len);
441         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
442
443         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
444         cksum = (~cksum) & 0xffff;
445         /*
446          * Per RFC 768: If the computed checksum is zero for UDP,
447          * it is transmitted as all ones
448          * (the equivalent in one's complement arithmetic).
449          */
450         if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
451                 cksum = 0xffff;
452
453         return (uint16_t)cksum;
454 }
455
456 /* IPv6 fragmentation header size */
457 #define RTE_IPV6_FRAG_HDR_SIZE 8
458
459 /**
460  * Parse next IPv6 header extension
461  *
462  * This function checks if proto number is an IPv6 extensions and parses its
463  * data if so, providing information on next header and extension length.
464  *
465  * @param p
466  *   Pointer to an extension raw data.
467  * @param proto
468  *   Protocol number extracted from the "next header" field from
469  *   the IPv6 header or the previous extension.
470  * @param ext_len
471  *   Extension data length.
472  * @return
473  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
474  */
475 __rte_experimental
476 static inline int
477 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
478 {
479         int next_proto;
480
481         switch (proto) {
482         case IPPROTO_AH:
483                 next_proto = *p++;
484                 *ext_len = (*p + 2) * sizeof(uint32_t);
485                 break;
486
487         case IPPROTO_HOPOPTS:
488         case IPPROTO_ROUTING:
489         case IPPROTO_DSTOPTS:
490                 next_proto = *p++;
491                 *ext_len = (*p + 1) * sizeof(uint64_t);
492                 break;
493
494         case IPPROTO_FRAGMENT:
495                 next_proto = *p;
496                 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
497                 break;
498
499         default:
500                 return -EINVAL;
501         }
502
503         return next_proto;
504 }
505
506 #ifdef __cplusplus
507 }
508 #endif
509
510 #endif /* _RTE_IP_H_ */