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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip/ip_lookup.h: ip (4 or 6) lookup structures, adjacencies, ...
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_ip_lookup_h
41 #define included_ip_lookup_h
43 #include <vnet/vnet.h>
44 #include <vlib/buffer.h>
46 /* Next index stored in adjacency. */
48 /* Packet does not match any route in table. */
51 /* Adjacency says to drop or punt this packet. */
55 /* This packet is for one of our own IP addresses. */
58 /* This packet matches an "interface route" and packets
59 need to be passed to ARP to find rewrite string for
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,
68 /* This packet needs to be classified */
69 IP_LOOKUP_NEXT_CLASSIFY,
71 /* This packet needs to go to MAP - RFC7596, RFC7597 */
74 /* This packet needs to go to MAP with Translation - RFC7599 */
77 /* This packets needs to go to 6RD (RFC5969) */
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,
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)
96 /* Default: 5-tuple without the "reverse" bit */
97 #define IP_FLOW_HASH_DEFAULT (0x1F)
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)
107 /* IP unicast adjacency. */
109 /* Handle for this adjacency in adjacency heap. */
112 /* Interface address index for this local/arp adjacency. */
113 u32 if_address_index;
115 /* Number of adjecencies in block. Greater than 1 means multipath;
116 otherwise equal to 1. */
119 /* Next hop after ip4-lookup. */
121 ip_lookup_next_t lookup_next_index : 16;
122 u16 lookup_next_index_as_int;
125 /* Force re-lookup in a different FIB. ~0 => normal behavior */
126 i16 explicit_fib_index;
127 u16 mcast_group_index;
129 /* When classifying, start here */
130 u16 classify_table_index;
131 /* Highest possible perf subgraph arc interposition, e.g. for ip6 ioam */
132 u16 saved_lookup_next_index;
134 vnet_declare_rewrite (VLIB_BUFFER_PRE_DATA_SIZE - 5*sizeof(u32));
137 /* Index into adjacency table. */
138 typedef u32 ip_adjacency_index_t;
141 /* Directly connected next-hop adjacency index. */
142 u32 next_hop_adj_index;
144 /* Path weight for this adjacency. */
146 } ip_multipath_next_hop_t;
149 /* Adjacency index of first index in block. */
152 /* Power of 2 size of adjacency block. */
155 /* Number of prefixes that point to this adjacency. */
158 /* Normalized next hops are used as hash keys: they are sorted by weight
159 and weights are chosen so they add up to 1 << log2_n_adj_in_block (with
160 zero-weighted next hops being deleted).
161 Unnormalized next hops are saved so that control plane has a record of exactly
162 what the RIB told it. */
164 /* Number of hops in the multipath. */
167 /* Offset into next hop heap for this block. */
170 /* Heap handle used to for example free block when we're done with it. */
172 } normalized_next_hops, unnormalized_next_hops;
173 } ip_multipath_adjacency_t;
175 /* IP multicast adjacency. */
177 /* Handle for this adjacency in adjacency heap. */
180 /* Number of adjecencies in block. */
183 /* Rewrite string. */
184 vnet_declare_rewrite (64 - 2*sizeof(u32));
185 } ip_multicast_rewrite_t;
188 /* ip4-multicast-rewrite next index. */
193 u8 rewrite_string[64 - 1*sizeof(u32) - 1*sizeof(u8)];
194 } ip_multicast_rewrite_string_t;
197 ip_multicast_rewrite_t * rewrite_heap;
199 ip_multicast_rewrite_string_t * rewrite_strings;
201 /* Negative rewrite string index; >= 0 sw_if_index.
202 Sorted. Used to hash. */
203 i32 ** adjacency_id_vector;
205 uword * adjacency_by_id_vector;
206 } ip_multicast_lookup_main_t;
209 /* Key for mhash; in fact, just a byte offset into mhash key vector. */
212 /* Interface which has this address. */
215 /* Adjacency for neighbor probe (ARP) for this interface address. */
216 u32 neighbor_probe_adj_index;
218 /* Address (prefix) length for this interface. */
221 /* Will be used for something eventually. Primary vs. secondary? */
224 /* Next and previous pointers for doubly linked list of
225 addresses per software interface. */
226 u32 next_this_sw_interface;
227 u32 prev_this_sw_interface;
228 } ip_interface_address_t;
233 // IP_LOCAL_NEXT_TCP_LOOKUP,
234 IP_LOCAL_NEXT_UDP_LOOKUP,
239 struct ip_lookup_main_t;
241 typedef void (* ip_add_del_adjacency_callback_t) (struct ip_lookup_main_t * lm,
243 ip_adjacency_t * adj,
247 vnet_config_main_t config_main;
249 u32 * config_index_by_sw_if_index;
252 typedef struct ip_lookup_main_t {
253 /* Adjacency heap. */
254 ip_adjacency_t * adjacency_heap;
256 /* Adjacency packet/byte counters indexed by adjacency index. */
257 vlib_combined_counter_main_t adjacency_counters;
259 /* Heap of (next hop, weight) blocks. Sorted by next hop. */
260 ip_multipath_next_hop_t * next_hop_heap;
262 /* Indexed by heap_handle from ip_adjacency_t. */
263 ip_multipath_adjacency_t * multipath_adjacencies;
265 /* Temporary vectors for looking up next hops in hash. */
266 ip_multipath_next_hop_t * next_hop_hash_lookup_key;
267 ip_multipath_next_hop_t * next_hop_hash_lookup_key_normalized;
269 /* Hash table mapping normalized next hops and weights
270 to multipath adjacency index. */
271 uword * multipath_adjacency_by_next_hops;
273 u32 * adjacency_remap_table;
274 u32 n_adjacency_remaps;
276 /* If average error per adjacency is less than this threshold adjacency block
278 f64 multipath_next_hop_error_tolerance;
280 /* Adjacency index for routing table misses, local punts, and drops. */
281 u32 miss_adj_index, drop_adj_index, local_adj_index;
283 /* Miss adjacency is always first in adjacency table. */
284 #define IP_LOOKUP_MISS_ADJ_INDEX 0
286 ip_add_del_adjacency_callback_t * add_del_adjacency_callbacks;
288 /* Pool of addresses that are assigned to interfaces. */
289 ip_interface_address_t * if_address_pool;
291 /* Hash table mapping address to index in interface address pool. */
292 mhash_t address_to_if_address_index;
294 /* Head of doubly linked list of interface addresses for each software interface.
295 ~0 means this interface has no address. */
296 u32 * if_address_pool_index_by_sw_if_index;
298 /* First table index to use for this interface, ~0 => none */
299 u32 * classify_table_index_by_sw_if_index;
301 /* rx/tx interface/feature configuration. */
302 ip_config_main_t rx_config_mains[VNET_N_CAST], tx_config_main;
304 /* Number of bytes in a fib result. Must be at least
305 sizeof (uword). First word is always adjacency index. */
306 u32 fib_result_n_bytes, fib_result_n_words;
308 format_function_t * format_fib_result;
310 /* 1 for ip6; 0 for ip4. */
313 /* Either format_ip4_address_and_length or format_ip6_address_and_length. */
314 format_function_t * format_address_and_length;
316 /* Table mapping ip protocol to ip[46]-local node next index. */
317 u8 local_next_by_ip_protocol[256];
319 /* IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
320 u8 builtin_protocol_by_ip_protocol[256];
323 always_inline ip_adjacency_t *
324 ip_get_adjacency (ip_lookup_main_t * lm,
327 ip_adjacency_t * adj;
329 adj = heap_elt_at_index (lm->adjacency_heap, adj_index);
331 ASSERT (! heap_is_free_handle (lm->adjacency_heap, adj->heap_handle));
336 #define ip_prefetch_adjacency(lm,adj_index,type) \
338 ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index); \
339 CLIB_PREFETCH (_adj, sizeof (_adj[0]), type); \
343 ip_call_add_del_adjacency_callbacks (ip_lookup_main_t * lm, u32 adj_index, u32 is_del)
345 ip_adjacency_t * adj;
347 adj = ip_get_adjacency (lm, adj_index);
348 for (i = 0; i < vec_len (lm->add_del_adjacency_callbacks); i++)
349 lm->add_del_adjacency_callbacks[i] (lm, adj_index, adj, is_del);
352 /* Create new block of given number of contiguous adjacencies. */
354 ip_add_adjacency (ip_lookup_main_t * lm,
355 ip_adjacency_t * adj,
357 u32 * adj_index_result);
359 void ip_del_adjacency (ip_lookup_main_t * lm, u32 adj_index);
362 ip_multipath_adjacency_free (ip_lookup_main_t * lm,
363 ip_multipath_adjacency_t * a);
366 ip_multipath_adjacency_add_del_next_hop (ip_lookup_main_t * lm,
368 u32 old_mp_adj_index,
369 u32 next_hop_adj_index,
371 u32 * new_mp_adj_index);
374 ip_interface_address_add_del (ip_lookup_main_t * lm,
381 always_inline ip_interface_address_t *
382 ip_get_interface_address (ip_lookup_main_t * lm, void * addr_fib)
384 uword * p = mhash_get (&lm->address_to_if_address_index, addr_fib);
385 return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
389 ip_interface_address_get_address (ip_lookup_main_t * lm, ip_interface_address_t * a)
390 { return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key); }
392 always_inline ip_interface_address_t *
393 ip_interface_address_for_packet (ip_lookup_main_t * lm, vlib_buffer_t * b, u32 sw_if_index)
395 ip_adjacency_t * adj;
396 u32 if_address_index;
398 adj = ip_get_adjacency (lm, vnet_buffer (b)->ip.adj_index[VLIB_TX]);
400 ASSERT (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
401 || adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL);
402 if_address_index = adj->if_address_index;
403 if_address_index = (if_address_index == ~0 ?
404 vec_elt (lm->if_address_pool_index_by_sw_if_index, sw_if_index)
407 return pool_elt_at_index (lm->if_address_pool, if_address_index);
410 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \
412 vnet_main_t *_vnm = vnet_get_main(); \
413 u32 _sw_if_index = sw_if_index; \
414 vnet_sw_interface_t *_swif; \
415 _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \
418 * Loop => honor unnumbered interface addressing. \
420 if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \
421 _sw_if_index = _swif->unnumbered_sw_if_index; \
423 (vec_len((lm)->if_address_pool_index_by_sw_if_index) \
425 ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \
426 (_sw_if_index)) : (u32)~0; \
427 ip_interface_address_t * _a; \
430 _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \
431 _ia = _a->next_this_sw_interface; \
437 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
439 serialize_function_t serialize_ip_lookup_main, unserialize_ip_lookup_main;
440 serialize_function_t serialize_vec_ip_adjacency, unserialize_vec_ip_adjacency;
442 #endif /* included_ip_lookup_h */