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