Initial commit of vpp code.
[vpp.git] / vnet / vnet / ip / ip_packet.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ip/ip_packet.h: packet format common between ip4 & ip6
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_ip_packet_h
41 #define included_ip_packet_h
42
43 #include <vppinfra/byte_order.h>
44 #include <vppinfra/error.h>
45
46 typedef enum ip_protocol {
47 #define ip_protocol(n,s) IP_PROTOCOL_##s = n,
48 #include "protocols.def"
49 #undef ip_protocol
50 } ip_protocol_t;
51
52 /* TCP/UDP ports. */
53 typedef enum {
54 #define ip_port(s,n) IP_PORT_##s = n,
55 #include "ports.def"
56 #undef ip_port
57 } ip_port_t;
58
59 /* Classifies protocols into TCP, UDP, ICMP or other. */
60 typedef enum {
61   IP_BUILTIN_PROTOCOL_UDP,
62   IP_BUILTIN_PROTOCOL_TCP,
63   IP_BUILTIN_PROTOCOL_ICMP,
64   IP_BUILTIN_PROTOCOL_UNKNOWN,
65 } ip_builtin_protocol_t;
66
67 #define foreach_ip_builtin_multicast_group      \
68   _ (1, all_hosts_on_subnet)                    \
69   _ (2, all_routers_on_subnet)                  \
70   _ (4, dvmrp)                                  \
71   _ (5, ospf_all_routers)                       \
72   _ (6, ospf_designated_routers)                \
73   _ (13, pim)                                   \
74   _ (18, vrrp)                                  \
75   _ (102, hsrp)                                 \
76   _ (22, igmp_v3)
77
78 typedef enum {
79 #define _(n,f) IP_MULTICAST_GROUP_##f = n,
80   foreach_ip_builtin_multicast_group
81 #undef _
82 } ip_multicast_group_t;
83
84 /* IP checksum support. */
85
86 /* Incremental checksum update. */
87 typedef uword ip_csum_t;
88
89 always_inline ip_csum_t
90 ip_csum_with_carry (ip_csum_t sum, ip_csum_t x)
91 {
92   ip_csum_t t = sum + x;
93   return t + (t < x);
94 }
95
96 /* Update checksum changing field at even byte offset from x -> 0. */
97 always_inline ip_csum_t
98 ip_csum_add_even (ip_csum_t c, ip_csum_t x)
99 {
100   ip_csum_t d;
101
102   d = c - x;
103
104   /* Fold in carry from high bit. */
105   d -= d > c;
106
107   ASSERT (ip_csum_with_carry (d, x) == c);
108
109   return d;
110 }
111
112 /* Update checksum changing field at even byte offset from 0 -> x. */
113 always_inline ip_csum_t
114 ip_csum_sub_even (ip_csum_t c, ip_csum_t x)
115 { return ip_csum_with_carry (c, x); }
116
117 always_inline ip_csum_t
118 ip_csum_update_inline (ip_csum_t sum, ip_csum_t old, ip_csum_t new,
119                        u32 field_byte_offset, u32 field_n_bytes)
120 {
121   /* For even 1-byte fields on big-endian and odd 1-byte fields on little endian
122      we need to shift byte into place for checksum. */
123   if ((field_n_bytes % 2)
124       && (field_byte_offset % 2) == CLIB_ARCH_IS_LITTLE_ENDIAN)
125     {
126       old = old << 8;
127       new = new << 8;
128     }
129   sum = ip_csum_sub_even (sum, old);
130   sum = ip_csum_add_even (sum, new);
131   return sum;
132 }
133
134 #define ip_csum_update(sum,old,new,type,field)                  \
135   ip_csum_update_inline ((sum), (old), (new),                   \
136                          STRUCT_OFFSET_OF (type, field),        \
137                          STRUCT_SIZE_OF (type, field))
138
139 always_inline u16 ip_csum_fold (ip_csum_t c)
140 {
141   /* Reduce to 16 bits. */
142 #if uword_bits == 64
143   c = (c & (ip_csum_t) 0xffffffff) + (c >> (ip_csum_t) 32);
144   c = (c & 0xffff) + (c >> 16);
145 #endif
146
147   c = (c & 0xffff) + (c >> 16);
148   c = (c & 0xffff) + (c >> 16);
149
150   return c;
151 }
152
153 /* Copy data and checksum at the same time. */
154 ip_csum_t ip_csum_and_memcpy (ip_csum_t sum, void * dst, void * src, uword n_bytes);
155
156 always_inline u16
157 ip_csum_and_memcpy_fold (ip_csum_t sum, void * dst)
158 {
159   uword n_zero;
160   ip_csum_t * dst_even;
161
162   dst_even = uword_to_pointer
163     (pointer_to_uword (dst) &~ (sizeof (sum) - 1),
164      ip_csum_t *);
165
166   if ((n_zero = dst - (void *) dst_even))
167     {
168       u8 * d8 = dst;
169       uword i;
170
171       for (i = 0; i < n_zero; i++)
172         d8[i] = 0;
173       
174       sum = ip_csum_with_carry (sum, dst_even[0]);
175     }
176   
177   return ip_csum_fold (sum);
178 }
179
180 /* Checksum routine. */
181 ip_csum_t ip_incremental_checksum (ip_csum_t sum, void * data, uword n_bytes);
182
183 #endif /* included_ip_packet_h */