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