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