bond: fix vlan flag interpretation
[dpdk.git] / lib / librte_net / rte_ip.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 1982, 1986, 1990, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by the University of
50  *      California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *      @(#)in.h        8.3 (Berkeley) 1/3/94
68  * $FreeBSD: src/sys/netinet/in.h,v 1.82 2003/10/25 09:37:10 ume Exp $
69  */
70
71 #ifndef _RTE_IP_H_
72 #define _RTE_IP_H_
73
74 /**
75  * @file
76  *
77  * IP-related defines
78  */
79
80 #include <stdint.h>
81 #include <netinet/in.h>
82
83 #include <rte_memcpy.h>
84 #include <rte_byteorder.h>
85 #include <rte_mbuf.h>
86
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90
91 /**
92  * IPv4 Header
93  */
94 struct ipv4_hdr {
95         uint8_t  version_ihl;           /**< version and header length */
96         uint8_t  type_of_service;       /**< type of service */
97         uint16_t total_length;          /**< length of packet */
98         uint16_t packet_id;             /**< packet ID */
99         uint16_t fragment_offset;       /**< fragmentation offset */
100         uint8_t  time_to_live;          /**< time to live */
101         uint8_t  next_proto_id;         /**< protocol ID */
102         uint16_t hdr_checksum;          /**< header checksum */
103         uint32_t src_addr;              /**< source address */
104         uint32_t dst_addr;              /**< destination address */
105 } __attribute__((__packed__));
106
107 /** Create IPv4 address */
108 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
109                                            (((b) & 0xff) << 16) | \
110                                            (((c) & 0xff) << 8)  | \
111                                            ((d) & 0xff))
112
113 /** Internet header length mask for version_ihl field */
114 #define IPV4_HDR_IHL_MASK       (0x0f)
115 /**
116  * Internet header length field multiplier (IHL field specifies overall header
117  * length in number of 4-byte words)
118  */
119 #define IPV4_IHL_MULTIPLIER     (4)
120
121 /* Fragment Offset * Flags. */
122 #define IPV4_HDR_DF_SHIFT       14
123 #define IPV4_HDR_MF_SHIFT       13
124 #define IPV4_HDR_FO_SHIFT       3
125
126 #define IPV4_HDR_DF_FLAG        (1 << IPV4_HDR_DF_SHIFT)
127 #define IPV4_HDR_MF_FLAG        (1 << IPV4_HDR_MF_SHIFT)
128
129 #define IPV4_HDR_OFFSET_MASK    ((1 << IPV4_HDR_MF_SHIFT) - 1)
130
131 #define IPV4_HDR_OFFSET_UNITS   8
132
133 /*
134  * IPv4 address types
135  */
136 #define IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
137 #define IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
138 #define IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
139 #define IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
140 #define IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
141 #define IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
142
143 /*
144  * IPv4 Multicast-related macros
145  */
146 #define IPV4_MIN_MCAST  IPv4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
147 #define IPV4_MAX_MCAST  IPv4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
148
149 #define IS_IPV4_MCAST(x) \
150         ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
151
152 /**
153  * @internal Calculate a sum of all words in the buffer.
154  * Helper routine for the rte_raw_cksum().
155  *
156  * @param buf
157  *   Pointer to the buffer.
158  * @param len
159  *   Length of the buffer.
160  * @param sum
161  *   Initial value of the sum.
162  * @return
163  *   sum += Sum of all words in the buffer.
164  */
165 static inline uint32_t
166 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
167 {
168         /* workaround gcc strict-aliasing warning */
169         uintptr_t ptr = (uintptr_t)buf;
170         const uint16_t *u16 = (const uint16_t *)ptr;
171
172         while (len >= (sizeof(*u16) * 4)) {
173                 sum += u16[0];
174                 sum += u16[1];
175                 sum += u16[2];
176                 sum += u16[3];
177                 len -= sizeof(*u16) * 4;
178                 u16 += 4;
179         }
180         while (len >= sizeof(*u16)) {
181                 sum += *u16;
182                 len -= sizeof(*u16);
183                 u16 += 1;
184         }
185
186         /* if length is in odd bytes */
187         if (len == 1)
188                 sum += *((const uint8_t *)u16);
189
190         return sum;
191 }
192
193 /**
194  * @internal Reduce a sum to the non-complemented checksum.
195  * Helper routine for the rte_raw_cksum().
196  *
197  * @param sum
198  *   Value of the sum.
199  * @return
200  *   The non-complemented checksum.
201  */
202 static inline uint16_t
203 __rte_raw_cksum_reduce(uint32_t sum)
204 {
205         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
206         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
207         return (uint16_t)sum;
208 }
209
210 /**
211  * Process the non-complemented checksum of a buffer.
212  *
213  * @param buf
214  *   Pointer to the buffer.
215  * @param len
216  *   Length of the buffer.
217  * @return
218  *   The non-complemented checksum.
219  */
220 static inline uint16_t
221 rte_raw_cksum(const void *buf, size_t len)
222 {
223         uint32_t sum;
224
225         sum = __rte_raw_cksum(buf, len, 0);
226         return __rte_raw_cksum_reduce(sum);
227 }
228
229 /**
230  * Process the IPv4 checksum of an IPv4 header.
231  *
232  * The checksum field must be set to 0 by the caller.
233  *
234  * @param ipv4_hdr
235  *   The pointer to the contiguous IPv4 header.
236  * @return
237  *   The complemented checksum to set in the IP packet.
238  */
239 static inline uint16_t
240 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
241 {
242         uint16_t cksum;
243         cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
244         return ((cksum == 0xffff) ? cksum : ~cksum);
245 }
246
247 /**
248  * Process the pseudo-header checksum of an IPv4 header.
249  *
250  * The checksum field must be set to 0 by the caller.
251  *
252  * Depending on the ol_flags, the pseudo-header checksum expected by the
253  * drivers is not the same. For instance, when TSO is enabled, the IP
254  * payload length must not be included in the packet.
255  *
256  * When ol_flags is 0, it computes the standard pseudo-header checksum.
257  *
258  * @param ipv4_hdr
259  *   The pointer to the contiguous IPv4 header.
260  * @param ol_flags
261  *   The ol_flags of the associated mbuf.
262  * @return
263  *   The non-complemented checksum to set in the L4 header.
264  */
265 static inline uint16_t
266 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
267 {
268         struct ipv4_psd_header {
269                 uint32_t src_addr; /* IP address of source host. */
270                 uint32_t dst_addr; /* IP address of destination host. */
271                 uint8_t  zero;     /* zero. */
272                 uint8_t  proto;    /* L4 protocol type. */
273                 uint16_t len;      /* L4 length. */
274         } psd_hdr;
275
276         psd_hdr.src_addr = ipv4_hdr->src_addr;
277         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
278         psd_hdr.zero = 0;
279         psd_hdr.proto = ipv4_hdr->next_proto_id;
280         if (ol_flags & PKT_TX_TCP_SEG) {
281                 psd_hdr.len = 0;
282         } else {
283                 psd_hdr.len = rte_cpu_to_be_16(
284                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
285                                 - sizeof(struct ipv4_hdr)));
286         }
287         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
288 }
289
290 /**
291  * Process the IPv4 UDP or TCP checksum.
292  *
293  * The IPv4 header should not contains options. The IP and layer 4
294  * checksum must be set to 0 in the packet by the caller.
295  *
296  * @param ipv4_hdr
297  *   The pointer to the contiguous IPv4 header.
298  * @param l4_hdr
299  *   The pointer to the beginning of the L4 header.
300  * @return
301  *   The complemented checksum to set in the IP packet.
302  */
303 static inline uint16_t
304 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
305 {
306         uint32_t cksum;
307         uint32_t l4_len;
308
309         l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
310                 sizeof(struct ipv4_hdr);
311
312         cksum = rte_raw_cksum(l4_hdr, l4_len);
313         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
314
315         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
316         cksum = (~cksum) & 0xffff;
317         if (cksum == 0)
318                 cksum = 0xffff;
319
320         return cksum;
321 }
322
323 /**
324  * IPv6 Header
325  */
326 struct ipv6_hdr {
327         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
328         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
329         uint8_t  proto;        /**< Protocol, next header. */
330         uint8_t  hop_limits;   /**< Hop limits. */
331         uint8_t  src_addr[16]; /**< IP address of source host. */
332         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
333 } __attribute__((__packed__));
334
335 /**
336  * Process the pseudo-header checksum of an IPv6 header.
337  *
338  * Depending on the ol_flags, the pseudo-header checksum expected by the
339  * drivers is not the same. For instance, when TSO is enabled, the IPv6
340  * payload length must not be included in the packet.
341  *
342  * When ol_flags is 0, it computes the standard pseudo-header checksum.
343  *
344  * @param ipv6_hdr
345  *   The pointer to the contiguous IPv6 header.
346  * @param ol_flags
347  *   The ol_flags of the associated mbuf.
348  * @return
349  *   The non-complemented checksum to set in the L4 header.
350  */
351 static inline uint16_t
352 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
353 {
354         uint32_t sum;
355         struct {
356                 uint32_t len;   /* L4 length. */
357                 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
358         } psd_hdr;
359
360         psd_hdr.proto = (ipv6_hdr->proto << 24);
361         if (ol_flags & PKT_TX_TCP_SEG) {
362                 psd_hdr.len = 0;
363         } else {
364                 psd_hdr.len = ipv6_hdr->payload_len;
365         }
366
367         sum = __rte_raw_cksum(ipv6_hdr->src_addr,
368                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
369                 0);
370         sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
371         return __rte_raw_cksum_reduce(sum);
372 }
373
374 /**
375  * Process the IPv6 UDP or TCP checksum.
376  *
377  * The IPv4 header should not contains options. The layer 4 checksum
378  * must be set to 0 in the packet by the caller.
379  *
380  * @param ipv6_hdr
381  *   The pointer to the contiguous IPv6 header.
382  * @param l4_hdr
383  *   The pointer to the beginning of the L4 header.
384  * @return
385  *   The complemented checksum to set in the IP packet.
386  */
387 static inline uint16_t
388 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
389 {
390         uint32_t cksum;
391         uint32_t l4_len;
392
393         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
394
395         cksum = rte_raw_cksum(l4_hdr, l4_len);
396         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
397
398         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
399         cksum = (~cksum) & 0xffff;
400         if (cksum == 0)
401                 cksum = 0xffff;
402
403         return cksum;
404 }
405
406 #ifdef __cplusplus
407 }
408 #endif
409
410 #endif /* _RTE_IP_H_ */