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