A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / ip / lookup.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_lookup.h: ip (4 or 6) lookup structures, adjacencies, ...
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 /**
41  * @file
42  * Definitions for all things IP (v4|v6) unicast and multicast lookup related.
43  *
44  * - Adjacency definitions and registration.
45  * - Callbacks on route add.
46  * - Callbacks on interface address change.
47  */
48 #ifndef included_ip_lookup_h
49 #define included_ip_lookup_h
50
51 #include <vnet/vnet.h>
52 #include <vlib/buffer.h>
53 #include <vnet/ip/ip4_packet.h>
54 #include <vnet/ip/ip6_packet.h>
55 #include <vnet/fib/fib_node.h>
56 #include <vnet/dpo/dpo.h>
57
58 /** @brief Common (IP4/IP6) next index stored in adjacency. */
59 typedef enum {
60   /** Adjacency to drop this packet. */
61   IP_LOOKUP_NEXT_DROP,
62   /** Adjacency to punt this packet. */
63   IP_LOOKUP_NEXT_PUNT,
64
65   /** This packet is for one of our own IP addresses. */
66   IP_LOOKUP_NEXT_LOCAL,
67
68   /** This packet matches an "incomplete adjacency" and packets
69      need to be passed to ARP to find rewrite string for
70      this destination. */
71   IP_LOOKUP_NEXT_ARP,
72
73   /** This packet matches an "interface route" and packets
74      need to be passed to ARP to find rewrite string for
75      this destination. */
76   IP_LOOKUP_NEXT_GLEAN,
77
78   /** This packet is to be rewritten and forwarded to the next
79      processing node.  This is typically the output interface but
80      might be another node for further output processing. */
81   IP_LOOKUP_NEXT_REWRITE,
82
83   /** This packets follow a load-balance */
84   IP_LOOKUP_NEXT_LOAD_BALANCE,
85
86   /** This packets follow a mid-chain adjacency */
87   IP_LOOKUP_NEXT_MIDCHAIN,
88
89   /** This packets needs to go to ICMP error */
90   IP_LOOKUP_NEXT_ICMP_ERROR,
91
92   IP_LOOKUP_N_NEXT,
93 } ip_lookup_next_t;
94
95 typedef enum {
96   IP4_LOOKUP_N_NEXT = IP_LOOKUP_N_NEXT,
97 } ip4_lookup_next_t;
98
99 typedef enum {
100   /* Hop-by-hop header handling */
101   IP6_LOOKUP_NEXT_HOP_BY_HOP = IP_LOOKUP_N_NEXT,
102   IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP,
103   IP6_LOOKUP_NEXT_POP_HOP_BY_HOP,
104   IP6_LOOKUP_N_NEXT,
105 } ip6_lookup_next_t;
106
107 #define IP4_LOOKUP_NEXT_NODES {                                 \
108     [IP_LOOKUP_NEXT_DROP] = "ip4-drop",                         \
109     [IP_LOOKUP_NEXT_PUNT] = "ip4-punt",                         \
110     [IP_LOOKUP_NEXT_LOCAL] = "ip4-local",                       \
111     [IP_LOOKUP_NEXT_ARP] = "ip4-arp",                           \
112     [IP_LOOKUP_NEXT_GLEAN] = "ip4-glean",                       \
113     [IP_LOOKUP_NEXT_REWRITE] = "ip4-rewrite-transit",           \
114     [IP_LOOKUP_NEXT_MIDCHAIN] = "ip4-midchain",                 \
115     [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip4-load-balance",         \
116     [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip4-icmp-error",             \
117 }
118
119 #define IP6_LOOKUP_NEXT_NODES {                                 \
120     [IP_LOOKUP_NEXT_DROP] = "ip6-drop",                         \
121     [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",                         \
122     [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",                       \
123     [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",             \
124     [IP_LOOKUP_NEXT_GLEAN] = "ip6-glean",                       \
125     [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",                   \
126     [IP_LOOKUP_NEXT_MIDCHAIN] = "ip6-midchain",                 \
127     [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip6-load-balance",         \
128     [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip6-icmp-error",             \
129     [IP6_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",            \
130     [IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop",    \
131     [IP6_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop",    \
132 }
133
134 /** Flow hash configuration */
135 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
136 #define IP_FLOW_HASH_DST_ADDR (1<<1)
137 #define IP_FLOW_HASH_PROTO (1<<2)
138 #define IP_FLOW_HASH_SRC_PORT (1<<3)
139 #define IP_FLOW_HASH_DST_PORT (1<<4)
140 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
141
142 /** Default: 5-tuple without the "reverse" bit */
143 #define IP_FLOW_HASH_DEFAULT (0x1F)
144
145 #define foreach_flow_hash_bit                   \
146 _(src, IP_FLOW_HASH_SRC_ADDR)                   \
147 _(dst, IP_FLOW_HASH_DST_ADDR)                   \
148 _(sport, IP_FLOW_HASH_SRC_PORT)                 \
149 _(dport, IP_FLOW_HASH_DST_PORT)                 \
150 _(proto, IP_FLOW_HASH_PROTO)                    \
151 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)
152
153 /**
154  * A flow hash configuration is a mask of the flow hash options
155  */
156 typedef u32 flow_hash_config_t;
157
158 #define IP_ADJACENCY_OPAQUE_SZ 16
159 /** @brief IP unicast adjacency.
160     @note cache aligned.
161 */
162 typedef struct {
163   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
164   /* Handle for this adjacency in adjacency heap. */
165   u32 heap_handle;
166
167   /** Number of adjecencies in block.  Greater than 1 means multipath;
168      otherwise equal to 1. */
169   u16 n_adj;
170
171   /** Next hop after ip4-lookup. */
172   union {
173     ip_lookup_next_t lookup_next_index : 16;
174     u16 lookup_next_index_as_int;
175   };
176
177   /** Interface address index for this local/arp adjacency. */
178   u32 if_address_index;
179
180   /** Force re-lookup in a different FIB. ~0 => normal behavior */
181   u16 mcast_group_index;  
182
183   /** Highest possible perf subgraph arc interposition, e.g. for ip6 ioam */
184   u16 saved_lookup_next_index;
185
186   /*
187    * link/ether-type
188    */
189   u8 ia_link;
190   u8 ia_nh_proto;
191
192   union {
193     union {
194         /**
195          * IP_LOOKUP_NEXT_ARP/IP_LOOKUP_NEXT_REWRITE
196          *
197          * neighbour adjacency sub-type;
198          */
199         struct {
200             ip46_address_t next_hop;
201         } nbr;
202         /**
203          * IP_LOOKUP_NEXT_MIDCHAIN
204          *
205          * A nbr adj that is also recursive. Think tunnels.
206          * A nbr adj can transition to be of type MDICHAIN
207          * so be sure to leave the two structs with the next_hop
208          * fields aligned.
209          */
210         struct {
211             /**
212              * The recursive next-hop
213              */
214             ip46_address_t next_hop;
215             /**
216              * The node index of the tunnel's post rewrite/TX function.
217              */
218             u32 tx_function_node;
219             /**
220              * The next DPO to use
221              */
222             dpo_id_t next_dpo;
223         } midchain;
224         /**
225          * IP_LOOKUP_NEXT_GLEAN
226          *
227          * Glean the address to ARP for from the packet's destination
228          */
229         struct {
230             ip46_address_t receive_addr;
231         } glean;
232     } sub_type;
233     u16 opaque[IP_ADJACENCY_OPAQUE_SZ];
234   };
235
236   /** @brief Special format function for this adjacency.
237    * Specifically good for cases which use the entire rewrite
238    * for their own purposes. Can easily reduce to a u16 or a u8 if/when
239    * the first cache line reads "full" on the free space gas gauge.
240    */
241   u32 special_adjacency_format_function_index;  /* 0 is invalid */
242
243   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
244
245   /* Rewrite in second/third cache lines */
246   vnet_declare_rewrite (VLIB_BUFFER_PRE_DATA_SIZE);
247
248     /*
249      * member not accessed in the data plane are relgated to the
250      * remaining cachelines
251      */
252     fib_node_t ia_node;
253 } ip_adjacency_t;
254
255 _Static_assert((STRUCT_OFFSET_OF(ip_adjacency_t, cacheline0) == 0),
256                "IP adjacency cachline 0 is not offset");
257 _Static_assert((STRUCT_OFFSET_OF(ip_adjacency_t, cacheline1) ==
258                 CLIB_CACHE_LINE_BYTES),
259                "IP adjacency cachline 1 is more than one cachline size offset");
260
261 /* An all zeros address */
262 extern const ip46_address_t zero_addr;
263
264 /* Index into adjacency table. */
265 typedef u32 ip_adjacency_index_t;
266
267 typedef struct {
268   /* Adjacency index of first index in block. */
269   u32 adj_index;
270   
271   /* Power of 2 size of adjacency block. */
272   u32 n_adj_in_block;
273
274   /* Number of prefixes that point to this adjacency. */
275   u32 reference_count;
276
277   /* Normalized next hops are saved for stats/display purposes */
278   struct {
279     /* Number of hops in the multipath. */
280     u32 count;
281
282     /* Offset into next hop heap for this block. */
283     u32 heap_offset;
284
285     /* Heap handle used to for example free block when we're done with it. */
286     u32 heap_handle;
287   } normalized_next_hops;
288 } ip_multipath_adjacency_t;
289
290 /* IP multicast adjacency. */
291 typedef struct {
292   /* Handle for this adjacency in adjacency heap. */
293   u32 heap_handle;
294
295   /* Number of adjecencies in block. */
296   u32 n_adj;
297
298   /* Rewrite string. */
299   vnet_declare_rewrite (64 - 2*sizeof(u32));
300 } ip_multicast_rewrite_t;
301
302 typedef struct {
303   /* ip4-multicast-rewrite next index. */
304   u32 next_index;
305
306   u8 n_rewrite_bytes;
307
308   u8 rewrite_string[64 - 1*sizeof(u32) - 1*sizeof(u8)];
309 } ip_multicast_rewrite_string_t;
310
311 typedef struct {
312   ip_multicast_rewrite_t * rewrite_heap;
313
314   ip_multicast_rewrite_string_t * rewrite_strings;
315
316   /* Negative rewrite string index; >= 0 sw_if_index.
317      Sorted.  Used to hash. */
318   i32 ** adjacency_id_vector;
319
320   uword * adjacency_by_id_vector;
321 } ip_multicast_lookup_main_t;
322
323 typedef struct {
324   /* Key for mhash; in fact, just a byte offset into mhash key vector. */
325   u32 address_key;
326
327   /* Interface which has this address. */
328   u32 sw_if_index;
329
330   /* Adjacency for neighbor probe (ARP) for this interface address. */
331   u32 neighbor_probe_adj_index;
332
333   /* Address (prefix) length for this interface. */
334   u16 address_length;
335
336   /* Will be used for something eventually.  Primary vs. secondary? */
337   u16 flags;
338
339   /* Next and previous pointers for doubly linked list of
340      addresses per software interface. */
341   u32 next_this_sw_interface;
342   u32 prev_this_sw_interface;
343 } ip_interface_address_t;
344
345 typedef enum {
346   IP_LOCAL_NEXT_DROP,
347   IP_LOCAL_NEXT_PUNT,
348   IP_LOCAL_NEXT_UDP_LOOKUP,
349   IP_LOCAL_NEXT_ICMP,
350   IP_LOCAL_N_NEXT,
351 } ip_local_next_t;
352
353 struct ip_lookup_main_t;
354
355 typedef void (* ip_add_del_adjacency_callback_t) (struct ip_lookup_main_t * lm,
356                                                   u32 adj_index,
357                                                   ip_adjacency_t * adj,
358                                                   u32 is_del);
359
360 typedef struct {
361   vnet_config_main_t config_main;
362
363   u32 * config_index_by_sw_if_index;
364 } ip_config_main_t;
365
366 /**
367  * This structure is used to dynamically register a custom adjacency
368  * for ip lookup.
369  * Typically used with
370  *  VNET_IP4_REGISTER_ADJACENCY or
371  *  VNET_IP6_REGISTER_ADJACENCY macros.
372  */
373 typedef struct ip_adj_register_struct {
374   /** Name of the node for this registered adjacency. */
375   char *node_name;
376
377   /** Formatting function for the adjacency.
378    * Variadic arguments given to the function are:
379    * - struct ip_lookup_main_t *
380    * - ip_adjacency_t *adj
381    */
382   format_function_t *fn;
383
384   /**
385    * When the adjacency is registered, the ip-lookup next index will
386    * be written where this pointer points.
387    */
388   u32 *next_index;
389
390   struct ip_adj_register_struct *next;
391 } ip_adj_register_t;
392
393 typedef struct ip_lookup_main_t {
394   /* Adjacency heap. */
395   ip_adjacency_t * adjacency_heap;
396
397   /** load-balance  packet/byte counters indexed by LB index. */
398   vlib_combined_counter_main_t load_balance_counters;
399
400   /** any-tx-feature-enabled interface bitmap */
401   uword * tx_sw_if_has_ip_output_features;
402
403   /** count of enabled features, per sw_if_index, to maintain bitmap */
404   i16 * tx_feature_count_by_sw_if_index;
405
406   /** Pool of addresses that are assigned to interfaces. */
407   ip_interface_address_t * if_address_pool;
408
409   /** Hash table mapping address to index in interface address pool. */
410   mhash_t address_to_if_address_index;
411
412   /** Head of doubly linked list of interface addresses for each software interface.
413      ~0 means this interface has no address. */
414   u32 * if_address_pool_index_by_sw_if_index;
415
416   /** First table index to use for this interface, ~0 => none */
417   u32 * classify_table_index_by_sw_if_index;
418
419   /** rx unicast, multicast, tx interface/feature configuration. */
420   ip_config_main_t feature_config_mains[VNET_N_IP_FEAT];
421
422   /** Number of bytes in a fib result.  Must be at least
423      sizeof (uword).  First word is always adjacency index. */
424   u32 fib_result_n_bytes, fib_result_n_words;
425
426   format_function_t * format_fib_result;
427
428   /** 1 for ip6; 0 for ip4. */
429   u32 is_ip6;
430
431   /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
432   format_function_t * format_address_and_length;
433
434   /** Special adjacency format functions */
435   format_function_t ** special_adjacency_format_functions;
436
437   /** Table mapping ip protocol to ip[46]-local node next index. */
438   u8 local_next_by_ip_protocol[256];
439
440   /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
441   u8 builtin_protocol_by_ip_protocol[256];
442
443   /** Registered adjacencies */
444   ip_adj_register_t *registered_adjacencies;
445 } ip_lookup_main_t;
446
447 always_inline ip_adjacency_t *
448 ip_get_adjacency (ip_lookup_main_t * lm,
449                   u32 adj_index)
450 {
451   ip_adjacency_t * adj;
452
453   adj = vec_elt_at_index (lm->adjacency_heap, adj_index);
454
455   ASSERT (adj->heap_handle != ~0);
456
457   return adj;
458 }
459
460 #define ip_prefetch_adjacency(lm,adj_index,type)                \
461 do {                                                            \
462   ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index);   \
463   CLIB_PREFETCH (_adj, sizeof (_adj[0]), type);                 \
464 } while (0)
465
466 /* Create new block of given number of contiguous adjacencies. */
467 ip_adjacency_t *
468 ip_add_adjacency (ip_lookup_main_t * lm,
469                   ip_adjacency_t * adj,
470                   u32 n_adj,
471                   u32 * adj_index_result);
472
473 clib_error_t *
474 ip_interface_address_add_del (ip_lookup_main_t * lm,
475                               u32 sw_if_index,
476                               void * address,
477                               u32 address_length,
478                               u32 is_del,
479                               u32 * result_index);
480
481 u8 *
482 format_ip_flow_hash_config (u8 * s, va_list * args);
483
484 always_inline ip_interface_address_t *
485 ip_get_interface_address (ip_lookup_main_t * lm, void * addr_fib)
486 {
487   uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
488   return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
489 }
490
491 u32
492 fib_table_id_find_fib_index (fib_protocol_t proto,
493                              u32 table_id);
494
495 always_inline void *
496 ip_interface_address_get_address (ip_lookup_main_t * lm, ip_interface_address_t * a)
497 { return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key); }
498
499 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
500 do {                                                                    \
501     vnet_main_t *_vnm = vnet_get_main();                                     \
502     u32 _sw_if_index = sw_if_index;                                     \
503     vnet_sw_interface_t *_swif;                                         \
504     _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
505                                                                         \
506     /*                                                                  \
507      * Loop => honor unnumbered interface addressing.                   \
508      */                                                                 \
509     if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)       \
510       _sw_if_index = _swif->unnumbered_sw_if_index;                     \
511     u32 _ia =                                                           \
512       (vec_len((lm)->if_address_pool_index_by_sw_if_index)              \
513        > (_sw_if_index))                                                \
514         ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index,          \
515                    (_sw_if_index)) : (u32)~0;                           \
516     ip_interface_address_t * _a;                                        \
517     while (_ia != ~0)                                                   \
518     {                                                                   \
519         _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
520         _ia = _a->next_this_sw_interface;                               \
521         (a) = _a;                                                       \
522         body;                                                           \
523     }                                                                   \
524 } while (0)
525
526 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
527
528 #endif /* included_ip_lookup_h */