mbuf: generic support for TCP segmentation offload
[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
82 #include <rte_memcpy.h>
83 #include <rte_byteorder.h>
84 #include <rte_mbuf.h>
85
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89
90 /**
91  * IPv4 Header
92  */
93 struct ipv4_hdr {
94         uint8_t  version_ihl;           /**< version and header length */
95         uint8_t  type_of_service;       /**< type of service */
96         uint16_t total_length;          /**< length of packet */
97         uint16_t packet_id;             /**< packet ID */
98         uint16_t fragment_offset;       /**< fragmentation offset */
99         uint8_t  time_to_live;          /**< time to live */
100         uint8_t  next_proto_id;         /**< protocol ID */
101         uint16_t hdr_checksum;          /**< header checksum */
102         uint32_t src_addr;              /**< source address */
103         uint32_t dst_addr;              /**< destination address */
104 } __attribute__((__packed__));
105
106 /** Create IPv4 address */
107 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
108                                            (((b) & 0xff) << 16) | \
109                                            (((c) & 0xff) << 8)  | \
110                                            ((d) & 0xff))
111
112 /* Fragment Offset * Flags. */
113 #define IPV4_HDR_DF_SHIFT       14
114 #define IPV4_HDR_MF_SHIFT       13
115 #define IPV4_HDR_FO_SHIFT       3
116
117 #define IPV4_HDR_DF_FLAG        (1 << IPV4_HDR_DF_SHIFT)
118 #define IPV4_HDR_MF_FLAG        (1 << IPV4_HDR_MF_SHIFT)
119
120 #define IPV4_HDR_OFFSET_MASK    ((1 << IPV4_HDR_MF_SHIFT) - 1)
121
122 #define IPV4_HDR_OFFSET_UNITS   8
123
124 /* IPv4 protocols */
125 #define IPPROTO_IP         0  /**< dummy for IP */
126 #define IPPROTO_HOPOPTS    0  /**< IP6 hop-by-hop options */
127 #define IPPROTO_ICMP       1  /**< control message protocol */
128 #define IPPROTO_IGMP       2  /**< group mgmt protocol */
129 #define IPPROTO_GGP        3  /**< gateway^2 (deprecated) */
130 #define IPPROTO_IPV4       4  /**< IPv4 encapsulation */
131 #define IPPROTO_TCP        6  /**< tcp */
132 #define IPPROTO_ST         7  /**< Stream protocol II */
133 #define IPPROTO_EGP        8  /**< exterior gateway protocol */
134 #define IPPROTO_PIGP       9  /**< private interior gateway */
135 #define IPPROTO_RCCMON    10  /**< BBN RCC Monitoring */
136 #define IPPROTO_NVPII     11  /**< network voice protocol*/
137 #define IPPROTO_PUP       12  /**< pup */
138 #define IPPROTO_ARGUS     13  /**< Argus */
139 #define IPPROTO_EMCON     14  /**< EMCON */
140 #define IPPROTO_XNET      15  /**< Cross Net Debugger */
141 #define IPPROTO_CHAOS     16  /**< Chaos*/
142 #define IPPROTO_UDP       17  /**< user datagram protocol */
143 #define IPPROTO_MUX       18  /**< Multiplexing */
144 #define IPPROTO_MEAS      19  /**< DCN Measurement Subsystems */
145 #define IPPROTO_HMP       20  /**< Host Monitoring */
146 #define IPPROTO_PRM       21  /**< Packet Radio Measurement */
147 #define IPPROTO_IDP       22  /**< xns idp */
148 #define IPPROTO_TRUNK1    23  /**< Trunk-1 */
149 #define IPPROTO_TRUNK2    24  /**< Trunk-2 */
150 #define IPPROTO_LEAF1     25  /**< Leaf-1 */
151 #define IPPROTO_LEAF2     26  /**< Leaf-2 */
152 #define IPPROTO_RDP       27  /**< Reliable Data */
153 #define IPPROTO_IRTP      28  /**< Reliable Transaction */
154 #define IPPROTO_TP        29  /**< tp-4 w/ class negotiation */
155 #define IPPROTO_BLT       30  /**< Bulk Data Transfer */
156 #define IPPROTO_NSP       31  /**< Network Services */
157 #define IPPROTO_INP       32  /**< Merit Internodal */
158 #define IPPROTO_SEP       33  /**< Sequential Exchange */
159 #define IPPROTO_3PC       34  /**< Third Party Connect */
160 #define IPPROTO_IDPR      35  /**< InterDomain Policy Routing */
161 #define IPPROTO_XTP       36  /**< XTP */
162 #define IPPROTO_DDP       37  /**< Datagram Delivery */
163 #define IPPROTO_CMTP      38  /**< Control Message Transport */
164 #define IPPROTO_TPXX      39  /**< TP++ Transport */
165 #define IPPROTO_IL        40  /**< IL transport protocol */
166 #define IPPROTO_IPV6      41  /**< IP6 header */
167 #define IPPROTO_SDRP      42  /**< Source Demand Routing */
168 #define IPPROTO_ROUTING   43  /**< IP6 routing header */
169 #define IPPROTO_FRAGMENT  44  /**< IP6 fragmentation header */
170 #define IPPROTO_IDRP      45  /**< InterDomain Routing*/
171 #define IPPROTO_RSVP      46  /**< resource reservation */
172 #define IPPROTO_GRE       47  /**< General Routing Encap. */
173 #define IPPROTO_MHRP      48  /**< Mobile Host Routing */
174 #define IPPROTO_BHA       49  /**< BHA */
175 #define IPPROTO_ESP       50  /**< IP6 Encap Sec. Payload */
176 #define IPPROTO_AH        51  /**< IP6 Auth Header */
177 #define IPPROTO_INLSP     52  /**< Integ. Net Layer Security */
178 #define IPPROTO_SWIPE     53  /**< IP with encryption */
179 #define IPPROTO_NHRP      54  /**< Next Hop Resolution */
180 /* 55-57: Unassigned */
181 #define IPPROTO_ICMPV6    58  /**< ICMP6 */
182 #define IPPROTO_NONE      59  /**< IP6 no next header */
183 #define IPPROTO_DSTOPTS   60  /**< IP6 destination option */
184 #define IPPROTO_AHIP      61  /**< any host internal protocol */
185 #define IPPROTO_CFTP      62  /**< CFTP */
186 #define IPPROTO_HELLO     63  /**< "hello" routing protocol */
187 #define IPPROTO_SATEXPAK  64  /**< SATNET/Backroom EXPAK */
188 #define IPPROTO_KRYPTOLAN 65  /**< Kryptolan */
189 #define IPPROTO_RVD       66  /**< Remote Virtual Disk */
190 #define IPPROTO_IPPC      67  /**< Pluribus Packet Core */
191 #define IPPROTO_ADFS      68  /**< Any distributed FS */
192 #define IPPROTO_SATMON    69  /**< Satnet Monitoring */
193 #define IPPROTO_VISA      70  /**< VISA Protocol */
194 #define IPPROTO_IPCV      71  /**< Packet Core Utility */
195 #define IPPROTO_CPNX      72  /**< Comp. Prot. Net. Executive */
196 #define IPPROTO_CPHB      73  /**< Comp. Prot. HeartBeat */
197 #define IPPROTO_WSN       74  /**< Wang Span Network */
198 #define IPPROTO_PVP       75  /**< Packet Video Protocol */
199 #define IPPROTO_BRSATMON  76  /**< BackRoom SATNET Monitoring */
200 #define IPPROTO_ND        77  /**< Sun net disk proto (temp.) */
201 #define IPPROTO_WBMON     78  /**< WIDEBAND Monitoring */
202 #define IPPROTO_WBEXPAK   79  /**< WIDEBAND EXPAK */
203 #define IPPROTO_EON       80  /**< ISO cnlp */
204 #define IPPROTO_VMTP      81  /**< VMTP */
205 #define IPPROTO_SVMTP     82  /**< Secure VMTP */
206 #define IPPROTO_VINES     83  /**< Banyon VINES */
207 #define IPPROTO_TTP       84  /**< TTP */
208 #define IPPROTO_IGP       85  /**< NSFNET-IGP */
209 #define IPPROTO_DGP       86  /**< dissimilar gateway prot. */
210 #define IPPROTO_TCF       87  /**< TCF */
211 #define IPPROTO_IGRP      88  /**< Cisco/GXS IGRP */
212 #define IPPROTO_OSPFIGP   89  /**< OSPFIGP */
213 #define IPPROTO_SRPC      90  /**< Strite RPC protocol */
214 #define IPPROTO_LARP      91  /**< Locus Address Resoloution */
215 #define IPPROTO_MTP       92  /**< Multicast Transport */
216 #define IPPROTO_AX25      93  /**< AX.25 Frames */
217 #define IPPROTO_IPEIP     94  /**< IP encapsulated in IP */
218 #define IPPROTO_MICP      95  /**< Mobile Int.ing control */
219 #define IPPROTO_SCCSP     96  /**< Semaphore Comm. security */
220 #define IPPROTO_ETHERIP   97  /**< Ethernet IP encapsulation */
221 #define IPPROTO_ENCAP     98  /**< encapsulation header */
222 #define IPPROTO_APES      99  /**< any private encr. scheme */
223 #define IPPROTO_GMTP     100  /**< GMTP */
224 #define IPPROTO_IPCOMP   108  /**< payload compression (IPComp) */
225 /* 101-254: Partly Unassigned */
226 #define IPPROTO_PIM      103  /**< Protocol Independent Mcast */
227 #define IPPROTO_PGM      113  /**< PGM */
228 #define IPPROTO_SCTP     132  /**< Stream Control Transport Protocol */
229 /* 255: Reserved */
230 /* BSD Private, local use, namespace incursion */
231 #define IPPROTO_DIVERT   254  /**< divert pseudo-protocol */
232 #define IPPROTO_RAW      255  /**< raw IP packet */
233 #define IPPROTO_MAX      256  /**< maximum protocol number */
234
235 /*
236  * IPv4 address types
237  */
238 #define IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
239 #define IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
240 #define IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
241 #define IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
242 #define IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
243 #define IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
244
245 /*
246  * IPv4 Multicast-related macros
247  */
248 #define IPV4_MIN_MCAST  IPv4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
249 #define IPV4_MAX_MCAST  IPv4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
250
251 #define IS_IPV4_MCAST(x) \
252         ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
253
254 /**
255  * Process the non-complemented checksum of a buffer.
256  *
257  * @param buf
258  *   Pointer to the buffer.
259  * @param len
260  *   Length of the buffer.
261  * @return
262  *   The non-complemented checksum.
263  */
264 static inline uint16_t
265 rte_raw_cksum(const char *buf, size_t len)
266 {
267         const uint16_t *u16 = (const uint16_t *)buf;
268         uint32_t sum = 0;
269
270         while (len >= (sizeof(*u16) * 4)) {
271                 sum += u16[0];
272                 sum += u16[1];
273                 sum += u16[2];
274                 sum += u16[3];
275                 len -= sizeof(*u16) * 4;
276                 u16 += 4;
277         }
278         while (len >= sizeof(*u16)) {
279                 sum += *u16;
280                 len -= sizeof(*u16);
281                 u16 += 1;
282         }
283
284         /* if length is in odd bytes */
285         if (len == 1)
286                 sum += *((const uint8_t *)u16);
287
288         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
289         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
290         return (uint16_t)sum;
291 }
292
293 /**
294  * Process the IPv4 checksum of an IPv4 header.
295  *
296  * The checksum field must be set to 0 by the caller.
297  *
298  * @param ipv4_hdr
299  *   The pointer to the contiguous IPv4 header.
300  * @return
301  *   The complemented checksum to set in the IP packet.
302  */
303 static inline uint16_t
304 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
305 {
306         uint16_t cksum;
307         cksum = rte_raw_cksum((const char *)ipv4_hdr, sizeof(struct ipv4_hdr));
308         return ((cksum == 0xffff) ? cksum : ~cksum);
309 }
310
311 /**
312  * Process the pseudo-header checksum of an IPv4 header.
313  *
314  * The checksum field must be set to 0 by the caller.
315  *
316  * Depending on the ol_flags, the pseudo-header checksum expected by the
317  * drivers is not the same. For instance, when TSO is enabled, the IP
318  * payload length must not be included in the packet.
319  *
320  * When ol_flags is 0, it computes the standard pseudo-header checksum.
321  *
322  * @param ipv4_hdr
323  *   The pointer to the contiguous IPv4 header.
324  * @param ol_flags
325  *   The ol_flags of the associated mbuf.
326  * @return
327  *   The non-complemented checksum to set in the L4 header.
328  */
329 static inline uint16_t
330 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
331 {
332         struct ipv4_psd_header {
333                 uint32_t src_addr; /* IP address of source host. */
334                 uint32_t dst_addr; /* IP address of destination host. */
335                 uint8_t  zero;     /* zero. */
336                 uint8_t  proto;    /* L4 protocol type. */
337                 uint16_t len;      /* L4 length. */
338         } psd_hdr;
339
340         psd_hdr.src_addr = ipv4_hdr->src_addr;
341         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
342         psd_hdr.zero = 0;
343         psd_hdr.proto = ipv4_hdr->next_proto_id;
344         if (ol_flags & PKT_TX_TCP_SEG) {
345                 psd_hdr.len = 0;
346         } else {
347                 psd_hdr.len = rte_cpu_to_be_16(
348                         (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
349                                 - sizeof(struct ipv4_hdr)));
350         }
351         return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
352 }
353
354 /**
355  * Process the IPv4 UDP or TCP checksum.
356  *
357  * The IPv4 header should not contains options. The IP and layer 4
358  * checksum must be set to 0 in the packet by the caller.
359  *
360  * @param ipv4_hdr
361  *   The pointer to the contiguous IPv4 header.
362  * @param l4_hdr
363  *   The pointer to the beginning of the L4 header.
364  * @return
365  *   The complemented checksum to set in the IP packet.
366  */
367 static inline uint16_t
368 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
369 {
370         uint32_t cksum;
371         uint32_t l4_len;
372
373         l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
374                 sizeof(struct ipv4_hdr);
375
376         cksum = rte_raw_cksum(l4_hdr, l4_len);
377         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
378
379         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
380         cksum = (~cksum) & 0xffff;
381         if (cksum == 0)
382                 cksum = 0xffff;
383
384         return cksum;
385 }
386
387 /**
388  * IPv6 Header
389  */
390 struct ipv6_hdr {
391         uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
392         uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
393         uint8_t  proto;        /**< Protocol, next header. */
394         uint8_t  hop_limits;   /**< Hop limits. */
395         uint8_t  src_addr[16]; /**< IP address of source host. */
396         uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
397 } __attribute__((__packed__));
398
399 /**
400  * Process the pseudo-header checksum of an IPv6 header.
401  *
402  * Depending on the ol_flags, the pseudo-header checksum expected by the
403  * drivers is not the same. For instance, when TSO is enabled, the IPv6
404  * payload length must not be included in the packet.
405  *
406  * When ol_flags is 0, it computes the standard pseudo-header checksum.
407  *
408  * @param ipv6_hdr
409  *   The pointer to the contiguous IPv6 header.
410  * @param ol_flags
411  *   The ol_flags of the associated mbuf.
412  * @return
413  *   The non-complemented checksum to set in the L4 header.
414  */
415 static inline uint16_t
416 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
417 {
418         struct ipv6_psd_header {
419                 uint8_t src_addr[16]; /* IP address of source host. */
420                 uint8_t dst_addr[16]; /* IP address of destination host. */
421                 uint32_t len;         /* L4 length. */
422                 uint32_t proto;       /* L4 protocol - top 3 bytes must be zero */
423         } psd_hdr;
424
425         rte_memcpy(&psd_hdr.src_addr, ipv6_hdr->src_addr,
426                 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr));
427         psd_hdr.proto = (ipv6_hdr->proto << 24);
428         if (ol_flags & PKT_TX_TCP_SEG) {
429                 psd_hdr.len = 0;
430         } else {
431                 psd_hdr.len = ipv6_hdr->payload_len;
432         }
433
434         return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
435 }
436
437 /**
438  * Process the IPv6 UDP or TCP checksum.
439  *
440  * The IPv4 header should not contains options. The layer 4 checksum
441  * must be set to 0 in the packet by the caller.
442  *
443  * @param ipv6_hdr
444  *   The pointer to the contiguous IPv6 header.
445  * @param l4_hdr
446  *   The pointer to the beginning of the L4 header.
447  * @return
448  *   The complemented checksum to set in the IP packet.
449  */
450 static inline uint16_t
451 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
452 {
453         uint32_t cksum;
454         uint32_t l4_len;
455
456         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
457
458         cksum = rte_raw_cksum(l4_hdr, l4_len);
459         cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
460
461         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
462         cksum = (~cksum) & 0xffff;
463         if (cksum == 0)
464                 cksum = 0xffff;
465
466         return cksum;
467 }
468
469 #ifdef __cplusplus
470 }
471 #endif
472
473 #endif /* _RTE_IP_H_ */