A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / ip / ip4.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/ip4.h: ip4 main include file
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_ip4_h
41 #define included_ip_ip4_h
42
43 #include <vnet/ip/ip4_mtrie.h>
44 #include <vnet/ip/ip4_packet.h>
45 #include <vnet/ip/lookup.h>
46 #include <vnet/ip/ip_feature_registration.h>
47
48 typedef struct ip4_fib_t {
49   /* Hash table for each prefix length mapping. */
50   uword * fib_entry_by_dst_address[33];
51
52   /* Mtrie for fast lookups.  Hash is used to maintain overlapping prefixes. */
53   ip4_fib_mtrie_t mtrie;
54
55   /* Table ID (hash key) for this FIB. */
56   u32 table_id;
57
58   /* Index into FIB vector. */
59   u32 index;
60
61   /* flow hash configuration */
62   flow_hash_config_t flow_hash_config;
63
64   /* N-tuple classifier indices */
65   u32 fwd_classify_table_index;
66   u32 rev_classify_table_index;
67
68 } ip4_fib_t;
69
70 struct ip4_main_t;
71
72 typedef void (ip4_add_del_interface_address_function_t)
73   (struct ip4_main_t * im,
74    uword opaque,
75    u32 sw_if_index,
76    ip4_address_t * address,
77    u32 address_length,
78    u32 if_address_index,
79    u32 is_del);
80
81 typedef struct {
82   ip4_add_del_interface_address_function_t * function;
83   uword function_opaque;
84 } ip4_add_del_interface_address_callback_t;
85
86 /**
87  * @brief IPv4 main type.
88  *
89  * State of IPv4 VPP processing including:
90  * - FIBs
91  * - Feature indices used in feature topological sort
92  * - Feature node run time references
93  */
94
95 typedef struct ip4_main_t {
96   ip_lookup_main_t lookup_main;
97
98   /** Vector of FIBs. */
99   struct fib_table_t_ * fibs;
100
101   u32 fib_masks[33];
102
103   /** Table index indexed by software interface. */
104   u32 * fib_index_by_sw_if_index;
105
106   /* IP4 enabled count by software interface */
107   u8 * ip_enabled_by_sw_if_index;
108
109   /** Hash table mapping table id to fib index.
110      ID space is not necessarily dense; index space is dense. */
111   uword * fib_index_by_table_id;
112
113   /** Functions to call when interface address changes. */
114   ip4_add_del_interface_address_callback_t * add_del_interface_address_callbacks;
115
116   /** Template used to generate IP4 ARP packets. */
117   vlib_packet_template_t ip4_arp_request_packet_template;
118
119   /** Feature path configuration lists */
120   vnet_ip_feature_registration_t * next_uc_feature;
121   vnet_ip_feature_registration_t * next_mc_feature;
122   vnet_ip_feature_registration_t * next_tx_feature;
123
124   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
125   u32 ip4_unicast_rx_feature_check_access;
126   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
127   u32 ip4_unicast_rx_feature_source_reachable_via_rx;
128   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
129   u32 ip4_unicast_rx_feature_source_reachable_via_any;
130   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
131   u32 ip4_unicast_rx_feature_policer_classify;
132   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
133   u32 ip4_unicast_rx_feature_ipsec;
134   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
135   u32 ip4_unicast_rx_feature_vpath;
136   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
137   u32 ip4_unicast_rx_feature_lookup;
138   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
139   u32 ip4_unicast_rx_feature_source_and_port_range_check;
140   /** Built-in unicast feature path indice, see @ref ip_feature_init_cast()  */
141   u32 ip4_unicast_rx_feature_drop;
142
143   /** Built-in multicast feature path index */
144   u32 ip4_multicast_rx_feature_vpath;
145   /** Built-in multicast feature path index */
146   u32 ip4_multicast_rx_feature_lookup;
147   /** Built-in multicast feature path indices */
148   u32 ip4_multicast_rx_feature_drop;
149
150   /** Built-in unicast feature path index, see @ref ip_feature_init_cast()  */
151   u32 ip4_unicast_tx_feature_source_and_port_range_check;
152
153   /** Built-in tx feature path index */
154   u32 ip4_tx_feature_interface_output;
155
156   /** Save results for show command */
157   char ** feature_nodes[VNET_N_IP_FEAT];
158
159   /** Seed for Jenkins hash used to compute ip4 flow hash. */
160   u32 flow_hash_seed;
161
162   /** @brief Template information for VPP generated packets */
163   struct {
164     /** TTL to use for host generated packets. */
165     u8 ttl;
166
167     /** TOS byte to use for host generated packets. */
168     u8 tos;
169
170     u8 pad[2];
171   } host_config;
172 } ip4_main_t;
173
174 /** Global ip4 main structure. */
175 extern ip4_main_t ip4_main;
176
177 #define VNET_IP4_UNICAST_FEATURE_INIT(x,...)                    \
178   __VA_ARGS__ vnet_ip_feature_registration_t uc_##x;            \
179 static void __vnet_add_feature_registration_uc_##x (void)       \
180   __attribute__((__constructor__)) ;                            \
181 static void __vnet_add_feature_registration_uc_##x (void)       \
182 {                                                               \
183   ip4_main_t * im = &ip4_main;                                  \
184   uc_##x.next = im->next_uc_feature;                            \
185   im->next_uc_feature = &uc_##x;                                \
186 }                                                               \
187 __VA_ARGS__ vnet_ip_feature_registration_t uc_##x 
188
189 #define VNET_IP4_MULTICAST_FEATURE_INIT(x,...)                  \
190   __VA_ARGS__ vnet_ip_feature_registration_t mc_##x;            \
191 static void __vnet_add_feature_registration_mc_##x (void)       \
192   __attribute__((__constructor__)) ;                            \
193 static void __vnet_add_feature_registration_mc_##x (void)       \
194 {                                                               \
195   ip4_main_t * im = &ip4_main;                                  \
196   mc_##x.next = im->next_mc_feature;                            \
197   im->next_mc_feature = &mc_##x;                                \
198 }                                                               \
199 __VA_ARGS__ vnet_ip_feature_registration_t mc_##x 
200
201 #define VNET_IP4_TX_FEATURE_INIT(x,...)                         \
202   __VA_ARGS__ vnet_ip_feature_registration_t tx_##x;            \
203 static void __vnet_add_feature_registration_tx_##x (void)       \
204   __attribute__((__constructor__)) ;                            \
205 static void __vnet_add_feature_registration_tx_##x (void)       \
206 {                                                               \
207   ip4_main_t * im = &ip4_main;                                  \
208   tx_##x.next = im->next_tx_feature;                            \
209   im->next_tx_feature = &tx_##x;                                \
210 }                                                               \
211 __VA_ARGS__ vnet_ip_feature_registration_t tx_##x 
212
213
214 /** Global ip4 input node.  Errors get attached to ip4 input node. */
215 extern vlib_node_registration_t ip4_input_node;
216 extern vlib_node_registration_t ip4_lookup_node;
217 extern vlib_node_registration_t ip4_rewrite_node;
218 extern vlib_node_registration_t ip4_rewrite_local_node;
219 extern vlib_node_registration_t ip4_arp_node;
220 extern vlib_node_registration_t ip4_glean_node;
221 extern vlib_node_registration_t ip4_midchain_node;
222
223 always_inline uword
224 ip4_destination_matches_route (const ip4_main_t * im,
225                                const ip4_address_t * key,
226                                const ip4_address_t * dest,
227                                uword dest_length)
228 { return 0 == ((key->data_u32 ^ dest->data_u32) & im->fib_masks[dest_length]); }
229
230 always_inline uword
231 ip4_destination_matches_interface (ip4_main_t * im,
232                                    ip4_address_t * key,
233                                    ip_interface_address_t * ia)
234 {
235   ip4_address_t * a = ip_interface_address_get_address (&im->lookup_main, ia);
236   return ip4_destination_matches_route (im, key, a, ia->address_length);
237 }
238
239 /* As above but allows for unaligned destinations (e.g. works right from IP header of packet). */
240 always_inline uword
241 ip4_unaligned_destination_matches_route (ip4_main_t * im,
242                                          ip4_address_t * key,
243                                          ip4_address_t * dest,
244                                          uword dest_length)
245 { return 0 == ((clib_mem_unaligned (&key->data_u32, u32) ^ dest->data_u32) & im->fib_masks[dest_length]); }
246
247 always_inline int
248 ip4_src_address_for_packet (ip_lookup_main_t * lm,
249                             u32 sw_if_index,
250                             ip4_address_t * src)
251 {
252     u32 if_add_index = 
253         lm->if_address_pool_index_by_sw_if_index[sw_if_index];
254     if (PREDICT_TRUE(if_add_index != ~0)) {
255         ip_interface_address_t *if_add = 
256             pool_elt_at_index(lm->if_address_pool, if_add_index);
257         ip4_address_t *if_ip = 
258             ip_interface_address_get_address(lm, if_add);
259         *src = *if_ip;
260         return 0;
261     }
262     else
263     {
264         ASSERT(0);
265         src->as_u32 = 0;
266     }
267     return (!0);
268 }
269
270 /* Find interface address which matches destination. */
271 always_inline ip4_address_t *
272 ip4_interface_address_matching_destination (ip4_main_t * im, ip4_address_t * dst, u32 sw_if_index,
273                                             ip_interface_address_t ** result_ia)
274 {
275   ip_lookup_main_t * lm = &im->lookup_main;
276   ip_interface_address_t * ia;
277   ip4_address_t * result = 0;
278
279   foreach_ip_interface_address (lm, ia, sw_if_index, 
280                                 1 /* honor unnumbered */,
281   ({
282     ip4_address_t * a = ip_interface_address_get_address (lm, ia);
283     if (ip4_destination_matches_route (im, dst, a, ia->address_length))
284       {
285         result = a;
286         break;
287       }
288   }));
289   if (result_ia)
290     *result_ia = result ? ia : 0;
291   return result;
292 }
293
294 ip4_address_t *
295 ip4_interface_first_address (ip4_main_t * im, u32 sw_if_index,
296                              ip_interface_address_t ** result_ia);
297
298 clib_error_t *
299 ip4_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
300                                ip4_address_t * address, u32 address_length,
301                                u32 is_del);
302
303 void
304 ip4_sw_interface_enable_disable (u32 sw_if_index,
305                                  u32 is_enable);
306
307 int ip4_address_compare (ip4_address_t * a1, ip4_address_t * a2);
308
309 /* Send an ARP request to see if given destination is reachable on given interface. */
310 clib_error_t *
311 ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index);
312
313 clib_error_t *
314 ip4_set_arp_limit (u32 arp_limit);
315
316 uword
317 ip4_udp_register_listener (vlib_main_t * vm,
318                            u16 dst_port,
319                            u32 next_node_index);
320
321 void 
322 ip4_icmp_register_type (vlib_main_t * vm, icmp4_type_t type, 
323                         u32 node_index);
324
325 u16 ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip4_header_t * ip0);
326
327 void ip4_register_protocol (u32 protocol, u32 node_index);
328
329 serialize_function_t serialize_vnet_ip4_main, unserialize_vnet_ip4_main;
330
331 int vnet_set_ip4_flow_hash (u32 table_id, flow_hash_config_t flow_hash_config);
332
333 void ip4_mtrie_init (ip4_fib_mtrie_t * m);
334
335 int vnet_set_ip4_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
336                                  u32 table_index);
337
338 /* Compute flow hash.  We'll use it to select which adjacency to use for this
339    flow.  And other things. */
340 always_inline u32
341 ip4_compute_flow_hash (const ip4_header_t * ip,
342                        flow_hash_config_t flow_hash_config)
343 {
344     tcp_header_t * tcp = (void *) (ip + 1);
345     u32 a, b, c, t1, t2;
346     uword is_tcp_udp = (ip->protocol == IP_PROTOCOL_TCP
347                         || ip->protocol == IP_PROTOCOL_UDP);
348
349     t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) 
350         ? ip->src_address.data_u32 : 0;
351     t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) 
352         ? ip->dst_address.data_u32 : 0;
353     
354     a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
355     b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
356     b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? ip->protocol : 0;
357
358     t1 = is_tcp_udp ? tcp->ports.src : 0;
359     t2 = is_tcp_udp ? tcp->ports.dst : 0;
360     
361     t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
362     t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
363
364     c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ?
365         (t1<<16) | t2 : (t2<<16) | t1;
366
367     hash_v3_mix32 (a, b, c);
368     hash_v3_finalize32 (a, b, c);
369
370     return c;
371 }
372
373 #endif /* included_ip_ip4_h */