Add support for installing ipv4 routes via unresolved next hop
[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 #ifndef included_ip_lookup_h
41 #define included_ip_lookup_h
42
43 #include <vnet/vnet.h>
44 #include <vlib/buffer.h>
45 #include <vnet/ip/ip4_packet.h>
46
47 /* Next index stored in adjacency. */
48 typedef enum {
49   /* Packet does not match any route in table. */
50   IP_LOOKUP_NEXT_MISS,
51
52   /* Adjacency says to drop or punt this packet. */
53   IP_LOOKUP_NEXT_DROP,
54   IP_LOOKUP_NEXT_PUNT,
55
56   /* This packet is for one of our own IP addresses. */
57   IP_LOOKUP_NEXT_LOCAL,
58
59   /* This packet matches an "interface route" and packets
60      need to be passed to ARP to find rewrite string for
61      this destination. */
62   IP_LOOKUP_NEXT_ARP,
63
64   /* This packet is to be rewritten and forwarded to the next
65      processing node.  This is typically the output interface but
66      might be another node for further output processing. */
67   IP_LOOKUP_NEXT_REWRITE,
68
69   /* This packet needs to be classified */
70   IP_LOOKUP_NEXT_CLASSIFY,
71
72   /* This packet needs to go to MAP - RFC7596, RFC7597 */
73   IP_LOOKUP_NEXT_MAP,
74
75   /* This packet needs to go to MAP with Translation - RFC7599 */
76   IP_LOOKUP_NEXT_MAP_T,
77
78   /* This packets needs to go to 6RD (RFC5969) */
79   IP_LOOKUP_NEXT_SIXRD,
80
81   /* Hop-by-hop header handling */
82   IP_LOOKUP_NEXT_HOP_BY_HOP,
83   IP_LOOKUP_NEXT_ADD_HOP_BY_HOP,
84   IP_LOOKUP_NEXT_POP_HOP_BY_HOP,
85
86   IP_LOOKUP_N_NEXT,
87 } ip_lookup_next_t;
88
89 /* Flow hash configuration */
90 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
91 #define IP_FLOW_HASH_DST_ADDR (1<<1)
92 #define IP_FLOW_HASH_PROTO (1<<2)
93 #define IP_FLOW_HASH_SRC_PORT (1<<3)
94 #define IP_FLOW_HASH_DST_PORT (1<<4)
95 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
96
97 /* Default: 5-tuple without the "reverse" bit */
98 #define IP_FLOW_HASH_DEFAULT (0x1F)
99
100 #define foreach_flow_hash_bit                   \
101 _(src, IP_FLOW_HASH_SRC_ADDR)                   \
102 _(dst, IP_FLOW_HASH_DST_ADDR)                   \
103 _(sport, IP_FLOW_HASH_SRC_PORT)                 \
104 _(dport, IP_FLOW_HASH_DST_PORT)                 \
105 _(proto, IP_FLOW_HASH_PROTO)                    \
106 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)
107
108 /* IP unicast adjacency. */
109 typedef struct {
110   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
111   /* Handle for this adjacency in adjacency heap. */
112   u32 heap_handle;
113
114   STRUCT_MARK(signature_start);
115
116   /* Interface address index for this local/arp adjacency. */
117   u32 if_address_index;
118
119   /* Number of adjecencies in block.  Greater than 1 means multipath;
120      otherwise equal to 1. */
121   u16 n_adj;
122
123   /* Next hop after ip4-lookup. */
124   union {
125     ip_lookup_next_t lookup_next_index : 16;
126     u16 lookup_next_index_as_int;
127   };
128
129   /* Force re-lookup in a different FIB. ~0 => normal behavior */
130   i16 explicit_fib_index;
131   u16 mcast_group_index;  
132
133   /* Highest possible perf subgraph arc interposition, e.g. for ip6 ioam */
134   u16 saved_lookup_next_index;
135
136   union {
137     /* IP_LOOKUP_NEXT_ARP only */
138     struct {
139       union {
140         ip4_address_t ip4;
141       } next_hop;
142       u32 next_adj_index_with_same_next_hop;
143     } arp;
144     /* IP_LOOKUP_NEXT_CLASSIFY only */
145     struct {
146       u16 table_index;
147     } classify;
148   };
149
150   STRUCT_MARK(signature_end);
151
152   /* Number of FIB entries sharing this adjacency */
153   u32 share_count;
154   /* Use this adjacency instead */
155   u32 next_adj_with_signature;
156
157   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
158
159   /* Rewrite in second/third cache lines */
160   vnet_declare_rewrite (VLIB_BUFFER_PRE_DATA_SIZE);
161 } ip_adjacency_t;
162
163 static inline uword
164 vnet_ip_adjacency_signature (ip_adjacency_t * adj)
165 {
166   uword signature = 0xfeedfaceULL;
167
168   /* Skip heap handle, sum everything up to but not including share_count */
169   signature = hash_memory64
170       (STRUCT_MARK_PTR(adj, signature_start),
171        STRUCT_OFFSET_OF(ip_adjacency_t, signature_end)
172        - STRUCT_OFFSET_OF(ip_adjacency_t, signature_start),
173        signature);
174
175   /* and the rewrite */
176   signature = hash_memory64 (&adj->rewrite_header, VLIB_BUFFER_PRE_DATA_SIZE,
177                              signature);
178   return signature;
179 }
180
181 static inline int
182 vnet_ip_adjacency_share_compare (ip_adjacency_t * a1, ip_adjacency_t *a2)
183 {
184   if (memcmp (STRUCT_MARK_PTR(a1, signature_start),
185               STRUCT_MARK_PTR(a2, signature_start),
186               STRUCT_OFFSET_OF(ip_adjacency_t, signature_end)
187               - STRUCT_OFFSET_OF(ip_adjacency_t, signature_start)))
188     return 0;
189   if (memcmp (&a1->rewrite_header, &a2->rewrite_header,
190               VLIB_BUFFER_PRE_DATA_SIZE))
191     return 0;
192   return 1;
193 }
194
195 /* Index into adjacency table. */
196 typedef u32 ip_adjacency_index_t;
197
198 typedef struct {
199   /* Directly connected next-hop adjacency index. */
200   u32 next_hop_adj_index;
201
202   /* Path weight for this adjacency. */
203   u32 weight;
204 } ip_multipath_next_hop_t;
205
206 typedef struct {
207   /* Adjacency index of first index in block. */
208   u32 adj_index;
209   
210   /* Power of 2 size of adjacency block. */
211   u32 n_adj_in_block;
212
213   /* Number of prefixes that point to this adjacency. */
214   u32 reference_count;
215
216   /* Normalized next hops are used as hash keys: they are sorted by weight
217      and weights are chosen so they add up to 1 << log2_n_adj_in_block (with
218      zero-weighted next hops being deleted).
219      Unnormalized next hops are saved so that control plane has a record of exactly
220      what the RIB told it. */
221   struct {
222     /* Number of hops in the multipath. */
223     u32 count;
224
225     /* Offset into next hop heap for this block. */
226     u32 heap_offset;
227
228     /* Heap handle used to for example free block when we're done with it. */
229     u32 heap_handle;
230   } normalized_next_hops, unnormalized_next_hops;
231 } ip_multipath_adjacency_t;
232
233 /* IP multicast adjacency. */
234 typedef struct {
235   /* Handle for this adjacency in adjacency heap. */
236   u32 heap_handle;
237
238   /* Number of adjecencies in block. */
239   u32 n_adj;
240
241   /* Rewrite string. */
242   vnet_declare_rewrite (64 - 2*sizeof(u32));
243 } ip_multicast_rewrite_t;
244
245 typedef struct {
246   /* ip4-multicast-rewrite next index. */
247   u32 next_index;
248
249   u8 n_rewrite_bytes;
250
251   u8 rewrite_string[64 - 1*sizeof(u32) - 1*sizeof(u8)];
252 } ip_multicast_rewrite_string_t;
253
254 typedef struct {
255   ip_multicast_rewrite_t * rewrite_heap;
256
257   ip_multicast_rewrite_string_t * rewrite_strings;
258
259   /* Negative rewrite string index; >= 0 sw_if_index.
260      Sorted.  Used to hash. */
261   i32 ** adjacency_id_vector;
262
263   uword * adjacency_by_id_vector;
264 } ip_multicast_lookup_main_t;
265
266 typedef struct {
267   /* Key for mhash; in fact, just a byte offset into mhash key vector. */
268   u32 address_key;
269
270   /* Interface which has this address. */
271   u32 sw_if_index;
272
273   /* Adjacency for neighbor probe (ARP) for this interface address. */
274   u32 neighbor_probe_adj_index;
275
276   /* Address (prefix) length for this interface. */
277   u16 address_length;
278
279   /* Will be used for something eventually.  Primary vs. secondary? */
280   u16 flags;
281
282   /* Next and previous pointers for doubly linked list of
283      addresses per software interface. */
284   u32 next_this_sw_interface;
285   u32 prev_this_sw_interface;
286 } ip_interface_address_t;
287
288 typedef enum {
289   IP_LOCAL_NEXT_DROP,
290   IP_LOCAL_NEXT_PUNT,
291   IP_LOCAL_NEXT_UDP_LOOKUP,
292   IP_LOCAL_NEXT_ICMP,
293   IP_LOCAL_N_NEXT,
294 } ip_local_next_t;
295
296 struct ip_lookup_main_t;
297
298 typedef void (* ip_add_del_adjacency_callback_t) (struct ip_lookup_main_t * lm,
299                                                   u32 adj_index,
300                                                   ip_adjacency_t * adj,
301                                                   u32 is_del);
302
303 typedef struct {
304   vnet_config_main_t config_main;
305
306   u32 * config_index_by_sw_if_index;
307 } ip_config_main_t;
308
309 typedef struct ip_lookup_main_t {
310   /* Adjacency heap. */
311   ip_adjacency_t * adjacency_heap;
312
313   /* Adjacency packet/byte counters indexed by adjacency index. */
314   vlib_combined_counter_main_t adjacency_counters;
315
316   /* Heap of (next hop, weight) blocks.  Sorted by next hop. */
317   ip_multipath_next_hop_t * next_hop_heap;
318
319   /* Indexed by heap_handle from ip_adjacency_t. */
320   ip_multipath_adjacency_t * multipath_adjacencies;
321
322   /* Adjacency by signature hash */
323   uword * adj_index_by_signature;
324
325   /* Temporary vectors for looking up next hops in hash. */
326   ip_multipath_next_hop_t * next_hop_hash_lookup_key;
327   ip_multipath_next_hop_t * next_hop_hash_lookup_key_normalized;
328
329   /* Hash table mapping normalized next hops and weights
330      to multipath adjacency index. */
331   uword * multipath_adjacency_by_next_hops;
332
333   u32 * adjacency_remap_table;
334   u32 n_adjacency_remaps;
335
336   /* If average error per adjacency is less than this threshold adjacency block
337      size is accepted. */
338   f64 multipath_next_hop_error_tolerance;
339
340   /* Adjacency index for routing table misses, local punts, and drops. */
341   u32 miss_adj_index, drop_adj_index, local_adj_index;
342
343   /* Miss adjacency is always first in adjacency table. */
344 #define IP_LOOKUP_MISS_ADJ_INDEX 0
345
346   ip_add_del_adjacency_callback_t * add_del_adjacency_callbacks;
347
348   /* Pool of addresses that are assigned to interfaces. */
349   ip_interface_address_t * if_address_pool;
350
351   /* Hash table mapping address to index in interface address pool. */
352   mhash_t address_to_if_address_index;
353
354   /* Head of doubly linked list of interface addresses for each software interface.
355      ~0 means this interface has no address. */
356   u32 * if_address_pool_index_by_sw_if_index;
357
358   /* First table index to use for this interface, ~0 => none */
359   u32 * classify_table_index_by_sw_if_index;
360
361   /* rx/tx interface/feature configuration. */
362   ip_config_main_t rx_config_mains[VNET_N_CAST], tx_config_main;
363
364   /* Number of bytes in a fib result.  Must be at least
365      sizeof (uword).  First word is always adjacency index. */
366   u32 fib_result_n_bytes, fib_result_n_words;
367
368   format_function_t * format_fib_result;
369
370   /* 1 for ip6; 0 for ip4. */
371   u32 is_ip6;
372
373   /* Either format_ip4_address_and_length or format_ip6_address_and_length. */
374   format_function_t * format_address_and_length;
375
376   /* Table mapping ip protocol to ip[46]-local node next index. */
377   u8 local_next_by_ip_protocol[256];
378
379   /* IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
380   u8 builtin_protocol_by_ip_protocol[256];
381 } ip_lookup_main_t;
382
383 always_inline ip_adjacency_t *
384 ip_get_adjacency (ip_lookup_main_t * lm,
385                   u32 adj_index)
386 {
387   ip_adjacency_t * adj;
388
389   adj = vec_elt_at_index (lm->adjacency_heap, adj_index);
390
391   ASSERT (adj->heap_handle != ~0);
392
393   return adj;
394 }
395
396 #define ip_prefetch_adjacency(lm,adj_index,type)                \
397 do {                                                            \
398   ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index);   \
399   CLIB_PREFETCH (_adj, sizeof (_adj[0]), type);                 \
400 } while (0)
401
402 static inline void
403 ip_register_add_del_adjacency_callback(ip_lookup_main_t * lm,
404                                        ip_add_del_adjacency_callback_t cb)
405 {
406   vec_add1(lm->add_del_adjacency_callbacks, cb);
407 }
408
409 always_inline void
410 ip_call_add_del_adjacency_callbacks (ip_lookup_main_t * lm, u32 adj_index, u32 is_del)
411 {
412   ip_adjacency_t * adj;
413   uword i;
414   adj = ip_get_adjacency (lm, adj_index);
415   for (i = 0; i < vec_len (lm->add_del_adjacency_callbacks); i++)
416     lm->add_del_adjacency_callbacks[i] (lm, adj_index, adj, is_del);
417 }
418
419 /* Create new block of given number of contiguous adjacencies. */
420 ip_adjacency_t *
421 ip_add_adjacency (ip_lookup_main_t * lm,
422                   ip_adjacency_t * adj,
423                   u32 n_adj,
424                   u32 * adj_index_result);
425
426 void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index);
427 void
428 ip_update_adjacency (ip_lookup_main_t * lm,
429                      u32 adj_index,
430                      ip_adjacency_t * copy_adj);
431
432 static inline int
433 ip_adjacency_is_multipath(ip_lookup_main_t * lm, u32 adj_index)
434 {
435   if (vec_len(lm->multipath_adjacencies) < adj_index - 1)
436     return 0;
437
438   return (lm->multipath_adjacencies[adj_index].adj_index == adj_index &&
439           lm->multipath_adjacencies[adj_index].n_adj_in_block > 0);
440 }
441
442 void
443 ip_multipath_adjacency_free (ip_lookup_main_t * lm,
444                              ip_multipath_adjacency_t * a);
445
446 u32
447 ip_multipath_adjacency_add_del_next_hop (ip_lookup_main_t * lm,
448                                          u32 is_del,
449                                          u32 old_mp_adj_index,
450                                          u32 next_hop_adj_index,
451                                          u32 next_hop_weight,
452                                          u32 * new_mp_adj_index);
453
454 clib_error_t *
455 ip_interface_address_add_del (ip_lookup_main_t * lm,
456                               u32 sw_if_index,
457                               void * address,
458                               u32 address_length,
459                               u32 is_del,
460                               u32 * result_index);
461
462 always_inline ip_interface_address_t *
463 ip_get_interface_address (ip_lookup_main_t * lm, void * addr_fib)
464 {
465   uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
466   return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
467 }
468
469 always_inline void *
470 ip_interface_address_get_address (ip_lookup_main_t * lm, ip_interface_address_t * a)
471 { return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key); }
472
473 always_inline ip_interface_address_t *
474 ip_interface_address_for_packet (ip_lookup_main_t * lm, vlib_buffer_t * b, u32 sw_if_index)
475 {
476   ip_adjacency_t * adj;
477   u32 if_address_index;
478
479   adj = ip_get_adjacency (lm, vnet_buffer (b)->ip.adj_index[VLIB_TX]);
480
481   ASSERT (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
482           || adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL);
483   if_address_index = adj->if_address_index;
484   if_address_index = (if_address_index == ~0 ?
485                       vec_elt (lm->if_address_pool_index_by_sw_if_index, sw_if_index)
486                       : if_address_index);
487
488   return pool_elt_at_index (lm->if_address_pool, if_address_index);
489 }
490
491 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body)        \
492 do {                                                                    \
493     vnet_main_t *_vnm = vnet_get_main();                                     \
494     u32 _sw_if_index = sw_if_index;                                     \
495     vnet_sw_interface_t *_swif;                                         \
496     _swif = vnet_get_sw_interface (_vnm, _sw_if_index);                 \
497                                                                         \
498     /*                                                                  \
499      * Loop => honor unnumbered interface addressing.                   \
500      */                                                                 \
501     if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)       \
502       _sw_if_index = _swif->unnumbered_sw_if_index;                     \
503     u32 _ia =                                                           \
504       (vec_len((lm)->if_address_pool_index_by_sw_if_index)              \
505        > (_sw_if_index))                                                \
506         ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index,          \
507                    (_sw_if_index)) : (u32)~0;                           \
508     ip_interface_address_t * _a;                                        \
509     while (_ia != ~0)                                                   \
510     {                                                                   \
511         _a = pool_elt_at_index ((lm)->if_address_pool, _ia);            \
512         _ia = _a->next_this_sw_interface;                               \
513         (a) = _a;                                                       \
514         body;                                                           \
515     }                                                                   \
516 } while (0)
517
518 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
519
520 #endif /* included_ip_lookup_h */