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