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