net: fix build with gcc 4.4.7 and strict aliasing
[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 /* Fragment Offset * Flags. */
114 #define IPV4_HDR_DF_SHIFT       14
115 #define IPV4_HDR_MF_SHIFT       13
116 #define IPV4_HDR_FO_SHIFT       3
117
118 #define IPV4_HDR_DF_FLAG        (1 << IPV4_HDR_DF_SHIFT)
119 #define IPV4_HDR_MF_FLAG        (1 << IPV4_HDR_MF_SHIFT)
120
121 #define IPV4_HDR_OFFSET_MASK    ((1 << IPV4_HDR_MF_SHIFT) - 1)
122
123 #define IPV4_HDR_OFFSET_UNITS   8
124
125 /*
126  * IPv4 address types
127  */
128 #define IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
129 #define IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
130 #define IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
131 #define IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
132 #define IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
133 #define IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
134
135 /*
136  * IPv4 Multicast-related macros
137  */
138 #define IPV4_MIN_MCAST  IPv4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
139 #define IPV4_MAX_MCAST  IPv4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
140
141 #define IS_IPV4_MCAST(x) \
142         ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
143
144 /**
145  * Process the non-complemented checksum of a buffer.
146  *
147  * @param buf
148  *   Pointer to the buffer.
149  * @param len
150  *   Length of the buffer.
151  * @return
152  *   The non-complemented checksum.
153  */
154 static inline uint16_t
155 rte_raw_cksum(const char *buf, size_t len)
156 {
157         /* workaround gcc strict-aliasing warning */
158         uintptr_t ptr = (uintptr_t)buf;
159         const uint16_t *u16 = (const uint16_t *)ptr;
160         uint32_t sum = 0;
161
162         while (len >= (sizeof(*u16) * 4)) {
163                 sum += u16[0];
164                 sum += u16[1];
165                 sum += u16[2];
166                 sum += u16[3];
167                 len -= sizeof(*u16) * 4;
168                 u16 += 4;
169         }
170         while (len >= sizeof(*u16)) {
171                 sum += *u16;
172                 len -= sizeof(*u16);
173                 u16 += 1;
174         }
175
176         /* if length is in odd bytes */
177         if (len == 1)
178                 sum += *((const uint8_t *)u16);
179
180         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
181         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
182         return (uint16_t)sum;
183 }
184
185 /**
186  * Process the IPv4 checksum of an IPv4 header.
187  *
188  * The checksum field must be set to 0 by the caller.
189  *
190  * @param ipv4_hdr
191  *   The pointer to the contiguous IPv4 header.
192  * @return
193  *   The complemented checksum to set in the IP packet.
194  */
195 static inline uint16_t
196 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
197 {
198         uint16_t cksum;
199         cksum = rte_raw_cksum((const char *)ipv4_hdr, sizeof(struct ipv4_hdr));
200         return ((cksum == 0xffff) ? cksum : ~cksum);
201 }
202
203 /**
204  * Process the pseudo-header checksum of an IPv4 header.
205  *
206  * The checksum field must be set to 0 by the caller.
207  *
208  * Depending on the ol_flags, the pseudo-header checksum expected by the
209  * drivers is not the same. For instance, when TSO is enabled, the IP
210  * payload length must not be included in the packet.
211  *
212  * When ol_flags is 0, it computes the standard pseudo-header checksum.
213  *
214  * @param ipv4_hdr
215  *   The pointer to the contiguous IPv4 header.
216  * @param ol_flags
217  *   The ol_flags of the associated mbuf.
218  * @return
219  *   The non-complemented checksum to set in the L4 header.
220  */
221 static inline uint16_t
222 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
223 {
224         struct ipv4_psd_header {
225                 uint32_t src_addr; /* IP address of source host. */
226                 uint32_t dst_addr; /* IP address of destination host. */
227                 uint8_t  zero;     /* zero. */
228                 uint8_t  proto;    /* L4 protocol type. */
229                 uint16_t len;      /* L4 length. */
230         } psd_hdr;
231
232         psd_hdr.src_addr = ipv4_hdr->src_addr;
233         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
234         psd_hdr.zero = 0;
235         psd_hdr.proto = ipv4_hdr->next_proto_id;
236         if (ol_flags & PKT_TX_TCP_SEG) {
237                 psd_hdr.len = 0;
238         } else {
239                 psd_hdr.len = rte_cpu_to_be_16(
240                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
241                                 - sizeof(struct ipv4_hdr)));
242         }
243         return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
244 }
245
246 /**
247  * Process the IPv4 UDP or TCP checksum.
248  *
249  * The IPv4 header should not contains options. The IP and layer 4
250  * checksum must be set to 0 in the packet by the caller.
251  *
252  * @param ipv4_hdr
253  *   The pointer to the contiguous IPv4 header.
254  * @param l4_hdr
255  *   The pointer to the beginning of the L4 header.
256  * @return
257  *   The complemented checksum to set in the IP packet.
258  */
259 static inline uint16_t
260 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
261 {
262         uint32_t cksum;
263         uint32_t l4_len;
264
265         l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
266                 sizeof(struct ipv4_hdr);
267
268         cksum = rte_raw_cksum(l4_hdr, l4_len);
269         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
270
271         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
272         cksum = (~cksum) & 0xffff;
273         if (cksum == 0)
274                 cksum = 0xffff;
275
276         return cksum;
277 }
278
279 /**
280  * IPv6 Header
281  */
282 struct ipv6_hdr {
283         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
284         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
285         uint8_t  proto;        /**< Protocol, next header. */
286         uint8_t  hop_limits;   /**< Hop limits. */
287         uint8_t  src_addr[16]; /**< IP address of source host. */
288         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
289 } __attribute__((__packed__));
290
291 /**
292  * Process the pseudo-header checksum of an IPv6 header.
293  *
294  * Depending on the ol_flags, the pseudo-header checksum expected by the
295  * drivers is not the same. For instance, when TSO is enabled, the IPv6
296  * payload length must not be included in the packet.
297  *
298  * When ol_flags is 0, it computes the standard pseudo-header checksum.
299  *
300  * @param ipv6_hdr
301  *   The pointer to the contiguous IPv6 header.
302  * @param ol_flags
303  *   The ol_flags of the associated mbuf.
304  * @return
305  *   The non-complemented checksum to set in the L4 header.
306  */
307 static inline uint16_t
308 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
309 {
310         struct ipv6_psd_header {
311                 uint8_t src_addr[16]; /* IP address of source host. */
312                 uint8_t dst_addr[16]; /* IP address of destination host. */
313                 uint32_t len;         /* L4 length. */
314                 uint32_t proto;       /* L4 protocol - top 3 bytes must be zero */
315         } psd_hdr;
316
317         rte_memcpy(&psd_hdr.src_addr, ipv6_hdr->src_addr,
318                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr));
319         psd_hdr.proto = (ipv6_hdr->proto << 24);
320         if (ol_flags & PKT_TX_TCP_SEG) {
321                 psd_hdr.len = 0;
322         } else {
323                 psd_hdr.len = ipv6_hdr->payload_len;
324         }
325
326         return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
327 }
328
329 /**
330  * Process the IPv6 UDP or TCP checksum.
331  *
332  * The IPv4 header should not contains options. The layer 4 checksum
333  * must be set to 0 in the packet by the caller.
334  *
335  * @param ipv6_hdr
336  *   The pointer to the contiguous IPv6 header.
337  * @param l4_hdr
338  *   The pointer to the beginning of the L4 header.
339  * @return
340  *   The complemented checksum to set in the IP packet.
341  */
342 static inline uint16_t
343 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
344 {
345         uint32_t cksum;
346         uint32_t l4_len;
347
348         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
349
350         cksum = rte_raw_cksum(l4_hdr, l4_len);
351         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
352
353         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
354         cksum = (~cksum) & 0xffff;
355         if (cksum == 0)
356                 cksum = 0xffff;
357
358         return cksum;
359 }
360
361 #ifdef __cplusplus
362 }
363 #endif
364
365 #endif /* _RTE_IP_H_ */