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.
42 * Definitions for all things IP (v4|v6) unicast and multicast lookup related.
44 * - Adjacency definitions and registration.
45 * - Callbacks on route add.
46 * - Callbacks on interface address change.
48 #ifndef included_ip_lookup_h
49 #define included_ip_lookup_h
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 #include <vnet/rewrite.h>
60 /** @brief Common (IP4/IP6) next index stored in adjacency. */
63 /** Adjacency to drop this packet. */
65 /** Adjacency to punt this packet. */
68 /** This packet is for one of our own IP addresses. */
71 /** This packet matches an "incomplete adjacency" and packets
72 need to be passed to ARP to find rewrite string for
76 /** This packet matches an "interface route" and packets
77 need to be passed to ARP to find rewrite string for
81 /** This packet is to be rewritten and forwarded to the next
82 processing node. This is typically the output interface but
83 might be another node for further output processing. */
84 IP_LOOKUP_NEXT_REWRITE,
86 /** This packets follow a load-balance */
87 IP_LOOKUP_NEXT_LOAD_BALANCE,
89 /** This packets follow a mid-chain adjacency */
90 IP_LOOKUP_NEXT_MIDCHAIN,
92 /** This packets needs to go to ICMP error */
93 IP_LOOKUP_NEXT_ICMP_ERROR,
95 /** Multicast Adjacency. */
103 IP4_LOOKUP_N_NEXT = IP_LOOKUP_N_NEXT,
108 /* Hop-by-hop header handling */
109 IP6_LOOKUP_NEXT_HOP_BY_HOP = IP_LOOKUP_N_NEXT,
110 IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP,
111 IP6_LOOKUP_NEXT_POP_HOP_BY_HOP,
115 #define IP4_LOOKUP_NEXT_NODES { \
116 [IP_LOOKUP_NEXT_DROP] = "ip4-drop", \
117 [IP_LOOKUP_NEXT_PUNT] = "ip4-punt", \
118 [IP_LOOKUP_NEXT_LOCAL] = "ip4-local", \
119 [IP_LOOKUP_NEXT_ARP] = "ip4-arp", \
120 [IP_LOOKUP_NEXT_GLEAN] = "ip4-glean", \
121 [IP_LOOKUP_NEXT_REWRITE] = "ip4-rewrite", \
122 [IP_LOOKUP_NEXT_MCAST] = "ip4-rewrite-mcast", \
123 [IP_LOOKUP_NEXT_MIDCHAIN] = "ip4-midchain", \
124 [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip4-load-balance", \
125 [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip4-icmp-error", \
128 #define IP6_LOOKUP_NEXT_NODES { \
129 [IP_LOOKUP_NEXT_DROP] = "ip6-drop", \
130 [IP_LOOKUP_NEXT_PUNT] = "ip6-punt", \
131 [IP_LOOKUP_NEXT_LOCAL] = "ip6-local", \
132 [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor", \
133 [IP_LOOKUP_NEXT_GLEAN] = "ip6-glean", \
134 [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite", \
135 [IP_LOOKUP_NEXT_MCAST] = "ip6-rewrite-mcast", \
136 [IP_LOOKUP_NEXT_MIDCHAIN] = "ip6-midchain", \
137 [IP_LOOKUP_NEXT_LOAD_BALANCE] = "ip6-load-balance", \
138 [IP_LOOKUP_NEXT_ICMP_ERROR] = "ip6-icmp-error", \
139 [IP6_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop", \
140 [IP6_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop", \
141 [IP6_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop", \
144 /** Flow hash configuration */
145 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
146 #define IP_FLOW_HASH_DST_ADDR (1<<1)
147 #define IP_FLOW_HASH_PROTO (1<<2)
148 #define IP_FLOW_HASH_SRC_PORT (1<<3)
149 #define IP_FLOW_HASH_DST_PORT (1<<4)
150 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
152 /** Default: 5-tuple without the "reverse" bit */
153 #define IP_FLOW_HASH_DEFAULT (0x1F)
155 #define foreach_flow_hash_bit \
156 _(src, IP_FLOW_HASH_SRC_ADDR) \
157 _(dst, IP_FLOW_HASH_DST_ADDR) \
158 _(sport, IP_FLOW_HASH_SRC_PORT) \
159 _(dport, IP_FLOW_HASH_DST_PORT) \
160 _(proto, IP_FLOW_HASH_PROTO) \
161 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST)
164 * A flow hash configuration is a mask of the flow hash options
166 typedef u32 flow_hash_config_t;
171 struct ip_adjacency_t_;
174 * @brief A function type for post-rewrite fixups on midchain adjacency
176 typedef void (*adj_midchain_fixup_t) (vlib_main_t * vm,
177 struct ip_adjacency_t_ * adj,
181 * @brief Flags on an IP adjacency
183 typedef enum ip_adjacency_flags_t_
186 * Currently a sync walk is active. Used to prevent re-entrant walking
188 IP_ADJ_SYNC_WALK_ACTIVE = (1 << 0),
189 } ip_adjacency_flags_t;
191 /** @brief IP unicast adjacency.
194 typedef struct ip_adjacency_t_
196 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
198 /** Number of adjecencies in block. Greater than 1 means multipath;
199 otherwise equal to 1. */
202 /** Next hop after ip4-lookup. */
205 ip_lookup_next_t lookup_next_index:16;
206 u16 lookup_next_index_as_int;
209 /** Interface address index for this local/arp adjacency. */
210 u32 if_address_index;
221 * IP_LOOKUP_NEXT_ARP/IP_LOOKUP_NEXT_REWRITE
223 * neighbour adjacency sub-type;
227 ip46_address_t next_hop;
230 * IP_LOOKUP_NEXT_MIDCHAIN
232 * A nbr adj that is also recursive. Think tunnels.
233 * A nbr adj can transition to be of type MDICHAIN
234 * so be sure to leave the two structs with the next_hop
240 * The recursive next-hop
242 ip46_address_t next_hop;
244 * The node index of the tunnel's post rewrite/TX function.
246 u32 tx_function_node;
248 * The next DPO to use
252 * A function to perform the post-rewrite fixup
254 adj_midchain_fixup_t fixup_func;
257 * IP_LOOKUP_NEXT_GLEAN
259 * Glean the address to ARP for from the packet's destination
263 ip46_address_t receive_addr;
267 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
269 /* Rewrite in second/third cache lines */
270 vnet_declare_rewrite (VLIB_BUFFER_PRE_DATA_SIZE);
273 * member not accessed in the data plane are relgated to the
274 * remaining cachelines
279 * Flags on the adjacency
281 ip_adjacency_flags_t ia_flags;
285 STATIC_ASSERT ((STRUCT_OFFSET_OF (ip_adjacency_t, cacheline0) == 0),
286 "IP adjacency cachline 0 is not offset");
287 STATIC_ASSERT ((STRUCT_OFFSET_OF (ip_adjacency_t, cacheline1) ==
288 CLIB_CACHE_LINE_BYTES),
289 "IP adjacency cachline 1 is more than one cachline size offset");
291 /* An all zeros address */
292 extern const ip46_address_t zero_addr;
297 /* Key for mhash; in fact, just a byte offset into mhash key vector. */
300 /* Interface which has this address. */
303 /* Adjacency for neighbor probe (ARP) for this interface address. */
304 u32 neighbor_probe_adj_index;
306 /* Address (prefix) length for this interface. */
309 /* Will be used for something eventually. Primary vs. secondary? */
312 /* Next and previous pointers for doubly linked list of
313 addresses per software interface. */
314 u32 next_this_sw_interface;
315 u32 prev_this_sw_interface;
316 } ip_interface_address_t;
322 IP_LOCAL_NEXT_UDP_LOOKUP,
327 struct ip_lookup_main_t;
329 typedef struct ip_lookup_main_t
331 /* Adjacency heap. */
332 ip_adjacency_t *adjacency_heap;
334 /** load-balance packet/byte counters indexed by LB index. */
335 vlib_combined_counter_main_t load_balance_counters;
337 /** Pool of addresses that are assigned to interfaces. */
338 ip_interface_address_t *if_address_pool;
340 /** Hash table mapping address to index in interface address pool. */
341 mhash_t address_to_if_address_index;
343 /** Head of doubly linked list of interface addresses for each software interface.
344 ~0 means this interface has no address. */
345 u32 *if_address_pool_index_by_sw_if_index;
347 /** First table index to use for this interface, ~0 => none */
348 u32 *classify_table_index_by_sw_if_index;
350 /** Feature arc indices */
351 u8 mcast_feature_arc_index;
352 u8 ucast_feature_arc_index;
353 u8 output_feature_arc_index;
355 /** Number of bytes in a fib result. Must be at least
356 sizeof (uword). First word is always adjacency index. */
357 u32 fib_result_n_bytes, fib_result_n_words;
359 format_function_t *format_fib_result;
361 /** 1 for ip6; 0 for ip4. */
364 /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
365 format_function_t *format_address_and_length;
367 /** Special adjacency format functions */
368 format_function_t **special_adjacency_format_functions;
370 /** Table mapping ip protocol to ip[46]-local node next index. */
371 u8 local_next_by_ip_protocol[256];
373 /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
374 u8 builtin_protocol_by_ip_protocol[256];
377 always_inline ip_adjacency_t *
378 ip_get_adjacency (ip_lookup_main_t * lm, u32 adj_index)
382 adj = vec_elt_at_index (lm->adjacency_heap, adj_index);
387 #define ip_prefetch_adjacency(lm,adj_index,type) \
389 ip_adjacency_t * _adj = (lm)->adjacency_heap + (adj_index); \
390 CLIB_PREFETCH (_adj, sizeof (_adj[0]), type); \
393 clib_error_t *ip_interface_address_add_del (ip_lookup_main_t * lm,
397 u32 is_del, u32 * result_index);
399 u8 *format_ip_flow_hash_config (u8 * s, va_list * args);
401 always_inline ip_interface_address_t *
402 ip_get_interface_address (ip_lookup_main_t * lm, void *addr_fib)
404 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
405 return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
408 u32 fib_table_id_find_fib_index (fib_protocol_t proto, u32 table_id);
411 ip_interface_address_get_address (ip_lookup_main_t * lm,
412 ip_interface_address_t * a)
414 return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
418 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \
420 vnet_main_t *_vnm = vnet_get_main(); \
421 u32 _sw_if_index = sw_if_index; \
422 vnet_sw_interface_t *_swif; \
423 _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \
426 * Loop => honor unnumbered interface addressing. \
428 if (loop && _swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \
429 _sw_if_index = _swif->unnumbered_sw_if_index; \
431 (vec_len((lm)->if_address_pool_index_by_sw_if_index) \
433 ? vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \
434 (_sw_if_index)) : (u32)~0; \
435 ip_interface_address_t * _a; \
438 _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \
439 _ia = _a->next_this_sw_interface; \
446 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
448 #endif /* included_ip_lookup_h */
451 * fd.io coding-style-patch-verification: ON
454 * eval: (c-set-style "gnu")