Added ICMP4 error node.
[vpp.git] / vnet / vnet / ip / ip.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.h: ip generic (4 or 6) main
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_main_h
41 #define included_ip_main_h
42
43 #include <vppinfra/hash.h>
44 #include <vppinfra/heap.h>              /* adjacency heap */
45
46 #include <vnet/vnet.h>
47
48 #include <vnet/ip/format.h>
49 #include <vnet/ip/ip_packet.h>
50 #include <vnet/ip/lookup.h>
51
52 #include <vnet/ip/tcp_packet.h>
53 #include <vnet/ip/udp_packet.h>
54 #include <vnet/ip/icmp46_packet.h>
55
56 #include <vnet/ip/ip4.h>
57 #include <vnet/ip/ip4_error.h>
58 #include <vnet/ip/ip4_packet.h>
59 #include <vnet/ip/icmp4.h>
60
61 #include <vnet/ip/ip6.h>
62 #include <vnet/ip/ip6_packet.h>
63 #include <vnet/ip/ip6_error.h>
64 #include <vnet/ip/icmp6.h>
65
66 #include <vnet/ip/tcp.h>
67
68 #if DPDK > 0
69 #include <vnet/devices/dpdk/dpdk.h>
70 #endif
71
72 #include <vnet/classify/vnet_classify.h>
73
74 typedef union {
75   ip4_address_t ip4;
76   ip6_address_t ip6;
77 } ip46_address_t;
78
79 /* Per protocol info. */
80 typedef struct {
81   /* Protocol name (also used as hash key). */
82   u8 * name;
83
84   /* Protocol number. */
85   ip_protocol_t protocol;
86
87   /* Format function for this IP protocol. */
88   format_function_t * format_header;
89
90   /* Parser for header. */
91   unformat_function_t * unformat_header;
92
93   /* Parser for per-protocol matches. */
94   unformat_function_t * unformat_match;
95
96   /* Parser for packet generator edits for this protocol. */
97   unformat_function_t * unformat_pg_edit;
98 } ip_protocol_info_t;
99
100 /* Per TCP/UDP port info. */
101 typedef struct {
102   /* Port name (used as hash key). */
103   u8 * name;
104
105   /* UDP/TCP port number in network byte order. */
106   u16 port;
107
108   /* Port specific format function. */
109   format_function_t * format_header;
110
111   /* Parser for packet generator edits for this protocol. */
112   unformat_function_t * unformat_pg_edit;
113 } tcp_udp_port_info_t;
114
115 typedef struct {
116   /* Per IP protocol info. */
117   ip_protocol_info_t * protocol_infos;
118
119   /* Protocol info index hashed by 8 bit IP protocol. */
120   uword * protocol_info_by_protocol;
121
122   /* Hash table mapping IP protocol name (see protocols.def)
123      to protocol number. */
124   uword * protocol_info_by_name;
125
126   /* Per TCP/UDP port info. */
127   tcp_udp_port_info_t * port_infos;
128
129   /* Hash table from network-byte-order port to port info index. */
130   uword * port_info_by_port;
131
132   /* Hash table mapping TCP/UDP name to port info index. */
133   uword * port_info_by_name;
134 } ip_main_t;
135
136 extern ip_main_t ip_main;
137
138 clib_error_t *
139 ip_main_init (vlib_main_t * vm);
140
141 static inline ip_protocol_info_t *
142 ip_get_protocol_info (ip_main_t * im, u32 protocol)
143 {
144   uword * p;
145
146   p = hash_get (im->protocol_info_by_protocol, protocol);
147   return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
148 }
149
150 static inline tcp_udp_port_info_t *
151 ip_get_tcp_udp_port_info (ip_main_t * im, u32 port)
152 {
153   uword * p;
154
155   p = hash_get (im->port_info_by_port, port);
156   return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
157 }
158       
159 always_inline ip_csum_t
160 ip_incremental_checksum_buffer (vlib_main_t * vm, vlib_buffer_t * first_buffer,
161                                 u32 first_buffer_offset,
162                                 u32 n_bytes_to_checksum,
163                                 ip_csum_t sum)
164 #if DPDK > 0
165 {
166   u32 n_bytes_left = n_bytes_to_checksum;
167   struct rte_mbuf * mb = ((struct rte_mbuf *)first_buffer)-1;
168   u8 nb_segs = mb->nb_segs;
169   ASSERT(mb->data_len >= first_buffer_offset);
170   void * h;
171   u32 n;
172   
173   n = clib_min (n_bytes_left, mb->data_len);
174   h = vlib_buffer_get_current (first_buffer) + first_buffer_offset;
175   while (n_bytes_left)
176     {
177       sum = ip_incremental_checksum (sum, h, n);
178       n_bytes_left -= n;
179       nb_segs--;
180       mb = mb->next;
181       if ((nb_segs == 0) || (mb == 0))
182         break;
183
184       n = clib_min (n_bytes_left, mb->data_len);
185       h = rte_ctrlmbuf_data(mb);
186     }
187  
188   ASSERT(n_bytes_left == 0);
189   ASSERT(nb_segs == 0);
190   return sum;
191 }
192 #else
193 {
194   vlib_buffer_t * b = first_buffer;
195   u32 n_bytes_left = n_bytes_to_checksum;
196   ASSERT (b->current_length >= first_buffer_offset);
197   void * h;
198   u32 n;
199
200   n = clib_min (n_bytes_left, b->current_length);
201   h = vlib_buffer_get_current (b) + first_buffer_offset;
202   sum = ip_incremental_checksum (sum, h, n);
203   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
204     {
205       while (1)
206         {
207           n_bytes_left -= n;
208           if (n_bytes_left == 0)
209             break;
210           b = vlib_get_buffer (vm, b->next_buffer);
211           n = clib_min (n_bytes_left, b->current_length);
212           h = vlib_buffer_get_current (b);
213           sum = ip_incremental_checksum (sum, h, n);
214         }
215     }
216
217   return sum;
218 }
219 #endif /* DPDK */
220
221 void ip_del_all_interface_addresses (vlib_main_t *vm, u32 sw_if_index);
222
223 #endif /* included_ip_main_h */