net: add definition for DSCP and ECN masks
[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 /* Type of Service fields */
62 #define RTE_IPV4_HDR_DSCP_MASK  (0xfc)
63 #define RTE_IPV4_HDR_ECN_MASK   (0x03)
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 \
91         RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
92 #define RTE_IPV4_MAX_MCAST \
93         RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
94
95 #define RTE_IS_IPV4_MCAST(x) \
96         ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
97         /**< check if IPv4 address is multicast */
98
99 /**
100  * @internal Calculate a sum of all words in the buffer.
101  * Helper routine for the rte_raw_cksum().
102  *
103  * @param buf
104  *   Pointer to the buffer.
105  * @param len
106  *   Length of the buffer.
107  * @param sum
108  *   Initial value of the sum.
109  * @return
110  *   sum += Sum of all words in the buffer.
111  */
112 static inline uint32_t
113 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
114 {
115         /* workaround gcc strict-aliasing warning */
116         uintptr_t ptr = (uintptr_t)buf;
117         typedef uint16_t __attribute__((__may_alias__)) u16_p;
118         const u16_p *u16_buf = (const u16_p *)ptr;
119
120         while (len >= (sizeof(*u16_buf) * 4)) {
121                 sum += u16_buf[0];
122                 sum += u16_buf[1];
123                 sum += u16_buf[2];
124                 sum += u16_buf[3];
125                 len -= sizeof(*u16_buf) * 4;
126                 u16_buf += 4;
127         }
128         while (len >= sizeof(*u16_buf)) {
129                 sum += *u16_buf;
130                 len -= sizeof(*u16_buf);
131                 u16_buf += 1;
132         }
133
134         /* if length is in odd bytes */
135         if (len == 1)
136                 sum += *((const uint8_t *)u16_buf);
137
138         return sum;
139 }
140
141 /**
142  * @internal Reduce a sum to the non-complemented checksum.
143  * Helper routine for the rte_raw_cksum().
144  *
145  * @param sum
146  *   Value of the sum.
147  * @return
148  *   The non-complemented checksum.
149  */
150 static inline uint16_t
151 __rte_raw_cksum_reduce(uint32_t sum)
152 {
153         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
154         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
155         return (uint16_t)sum;
156 }
157
158 /**
159  * Process the non-complemented checksum of a buffer.
160  *
161  * @param buf
162  *   Pointer to the buffer.
163  * @param len
164  *   Length of the buffer.
165  * @return
166  *   The non-complemented checksum.
167  */
168 static inline uint16_t
169 rte_raw_cksum(const void *buf, size_t len)
170 {
171         uint32_t sum;
172
173         sum = __rte_raw_cksum(buf, len, 0);
174         return __rte_raw_cksum_reduce(sum);
175 }
176
177 /**
178  * Compute the raw (non complemented) checksum of a packet.
179  *
180  * @param m
181  *   The pointer to the mbuf.
182  * @param off
183  *   The offset in bytes to start the checksum.
184  * @param len
185  *   The length in bytes of the data to checksum.
186  * @param cksum
187  *   A pointer to the checksum, filled on success.
188  * @return
189  *   0 on success, -1 on error (bad length or offset).
190  */
191 static inline int
192 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
193         uint16_t *cksum)
194 {
195         const struct rte_mbuf *seg;
196         const char *buf;
197         uint32_t sum, tmp;
198         uint32_t seglen, done;
199
200         /* easy case: all data in the first segment */
201         if (off + len <= rte_pktmbuf_data_len(m)) {
202                 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
203                                 const char *, off), len);
204                 return 0;
205         }
206
207         if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
208                 return -1; /* invalid params, return a dummy value */
209
210         /* else browse the segment to find offset */
211         seglen = 0;
212         for (seg = m; seg != NULL; seg = seg->next) {
213                 seglen = rte_pktmbuf_data_len(seg);
214                 if (off < seglen)
215                         break;
216                 off -= seglen;
217         }
218         seglen -= off;
219         buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
220         if (seglen >= len) {
221                 /* all in one segment */
222                 *cksum = rte_raw_cksum(buf, len);
223                 return 0;
224         }
225
226         /* hard case: process checksum of several segments */
227         sum = 0;
228         done = 0;
229         for (;;) {
230                 tmp = __rte_raw_cksum(buf, seglen, 0);
231                 if (done & 1)
232                         tmp = rte_bswap16((uint16_t)tmp);
233                 sum += tmp;
234                 done += seglen;
235                 if (done == len)
236                         break;
237                 seg = seg->next;
238                 buf = rte_pktmbuf_mtod(seg, const char *);
239                 seglen = rte_pktmbuf_data_len(seg);
240                 if (seglen > len - done)
241                         seglen = len - done;
242         }
243
244         *cksum = __rte_raw_cksum_reduce(sum);
245         return 0;
246 }
247
248 /**
249  * Process the IPv4 checksum of an IPv4 header.
250  *
251  * The checksum field must be set to 0 by the caller.
252  *
253  * @param ipv4_hdr
254  *   The pointer to the contiguous IPv4 header.
255  * @return
256  *   The complemented checksum to set in the IP packet.
257  */
258 static inline uint16_t
259 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
260 {
261         uint16_t cksum;
262         cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
263         return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
264 }
265
266 /**
267  * Process the pseudo-header checksum of an IPv4 header.
268  *
269  * The checksum field must be set to 0 by the caller.
270  *
271  * Depending on the ol_flags, the pseudo-header checksum expected by the
272  * drivers is not the same. For instance, when TSO is enabled, the IP
273  * payload length must not be included in the packet.
274  *
275  * When ol_flags is 0, it computes the standard pseudo-header checksum.
276  *
277  * @param ipv4_hdr
278  *   The pointer to the contiguous IPv4 header.
279  * @param ol_flags
280  *   The ol_flags of the associated mbuf.
281  * @return
282  *   The non-complemented checksum to set in the L4 header.
283  */
284 static inline uint16_t
285 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
286 {
287         struct ipv4_psd_header {
288                 uint32_t src_addr; /* IP address of source host. */
289                 uint32_t dst_addr; /* IP address of destination host. */
290                 uint8_t  zero;     /* zero. */
291                 uint8_t  proto;    /* L4 protocol type. */
292                 uint16_t len;      /* L4 length. */
293         } psd_hdr;
294
295         psd_hdr.src_addr = ipv4_hdr->src_addr;
296         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
297         psd_hdr.zero = 0;
298         psd_hdr.proto = ipv4_hdr->next_proto_id;
299         if (ol_flags & PKT_TX_TCP_SEG) {
300                 psd_hdr.len = 0;
301         } else {
302                 psd_hdr.len = rte_cpu_to_be_16(
303                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
304                                 - sizeof(struct rte_ipv4_hdr)));
305         }
306         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
307 }
308
309 /**
310  * Process the IPv4 UDP or TCP checksum.
311  *
312  * The IPv4 header should not contains options. The IP and layer 4
313  * checksum must be set to 0 in the packet by the caller.
314  *
315  * @param ipv4_hdr
316  *   The pointer to the contiguous IPv4 header.
317  * @param l4_hdr
318  *   The pointer to the beginning of the L4 header.
319  * @return
320  *   The complemented checksum to set in the IP packet
321  *   or 0 on error
322  */
323 static inline uint16_t
324 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
325 {
326         uint32_t cksum;
327         uint32_t l3_len, l4_len;
328
329         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
330         if (l3_len < sizeof(struct rte_ipv4_hdr))
331                 return 0;
332
333         l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
334
335         cksum = rte_raw_cksum(l4_hdr, l4_len);
336         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
337
338         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
339         cksum = (~cksum) & 0xffff;
340         if (cksum == 0)
341                 cksum = 0xffff;
342
343         return (uint16_t)cksum;
344 }
345
346 /**
347  * IPv6 Header
348  */
349 struct rte_ipv6_hdr {
350         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
351         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
352         uint8_t  proto;        /**< Protocol, next header. */
353         uint8_t  hop_limits;   /**< Hop limits. */
354         uint8_t  src_addr[16]; /**< IP address of source host. */
355         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
356 } __attribute__((__packed__));
357
358 /* IPv6 vtc_flow: IPv / TC / flow_label */
359 #define RTE_IPV6_HDR_FL_SHIFT 0
360 #define RTE_IPV6_HDR_TC_SHIFT 20
361 #define RTE_IPV6_HDR_FL_MASK    ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
362 #define RTE_IPV6_HDR_TC_MASK    (0xff << RTE_IPV6_HDR_TC_SHIFT)
363 #define RTE_IPV6_HDR_DSCP_MASK  (0xfc << RTE_IPV6_HDR_TC_SHIFT)
364 #define RTE_IPV6_HDR_ECN_MASK   (0x03 << RTE_IPV6_HDR_TC_SHIFT)
365
366 /**
367  * Process the pseudo-header checksum of an IPv6 header.
368  *
369  * Depending on the ol_flags, the pseudo-header checksum expected by the
370  * drivers is not the same. For instance, when TSO is enabled, the IPv6
371  * payload length must not be included in the packet.
372  *
373  * When ol_flags is 0, it computes the standard pseudo-header checksum.
374  *
375  * @param ipv6_hdr
376  *   The pointer to the contiguous IPv6 header.
377  * @param ol_flags
378  *   The ol_flags of the associated mbuf.
379  * @return
380  *   The non-complemented checksum to set in the L4 header.
381  */
382 static inline uint16_t
383 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
384 {
385         uint32_t sum;
386         struct {
387                 uint32_t len;   /* L4 length. */
388                 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
389         } psd_hdr;
390
391         psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
392         if (ol_flags & PKT_TX_TCP_SEG) {
393                 psd_hdr.len = 0;
394         } else {
395                 psd_hdr.len = ipv6_hdr->payload_len;
396         }
397
398         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
399                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
400                 0);
401         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
402         return __rte_raw_cksum_reduce(sum);
403 }
404
405 /**
406  * Process the IPv6 UDP or TCP checksum.
407  *
408  * The IPv4 header should not contains options. The layer 4 checksum
409  * must be set to 0 in the packet by the caller.
410  *
411  * @param ipv6_hdr
412  *   The pointer to the contiguous IPv6 header.
413  * @param l4_hdr
414  *   The pointer to the beginning of the L4 header.
415  * @return
416  *   The complemented checksum to set in the IP packet.
417  */
418 static inline uint16_t
419 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
420 {
421         uint32_t cksum;
422         uint32_t l4_len;
423
424         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
425
426         cksum = rte_raw_cksum(l4_hdr, l4_len);
427         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
428
429         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
430         cksum = (~cksum) & 0xffff;
431         if (cksum == 0)
432                 cksum = 0xffff;
433
434         return (uint16_t)cksum;
435 }
436
437 #ifdef __cplusplus
438 }
439 #endif
440
441 #endif /* _RTE_IP_H_ */