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/adj/adj.h>
57 #include <vnet/dpo/dpo.h>
58 #include <vnet/feature/feature.h>
60 /** Flow hash configuration */
61 #define IP_FLOW_HASH_SRC_ADDR (1<<0)
62 #define IP_FLOW_HASH_DST_ADDR (1<<1)
63 #define IP_FLOW_HASH_PROTO (1<<2)
64 #define IP_FLOW_HASH_SRC_PORT (1<<3)
65 #define IP_FLOW_HASH_DST_PORT (1<<4)
66 #define IP_FLOW_HASH_REVERSE_SRC_DST (1<<5)
67 #define IP_FLOW_HASH_SYMMETRIC (1<<6)
69 /** Default: 5-tuple without the "reverse" bit */
70 #define IP_FLOW_HASH_DEFAULT (0x1F)
72 #define foreach_flow_hash_bit \
73 _(src, IP_FLOW_HASH_SRC_ADDR) \
74 _(dst, IP_FLOW_HASH_DST_ADDR) \
75 _(sport, IP_FLOW_HASH_SRC_PORT) \
76 _(dport, IP_FLOW_HASH_DST_PORT) \
77 _(proto, IP_FLOW_HASH_PROTO) \
78 _(reverse, IP_FLOW_HASH_REVERSE_SRC_DST) \
79 _(symmetric, IP_FLOW_HASH_SYMMETRIC)
82 * A flow hash configuration is a mask of the flow hash options
84 typedef u32 flow_hash_config_t;
86 /* An all zeros address */
87 extern const ip46_address_t zero_addr;
92 /* Key for mhash; in fact, just a byte offset into mhash key vector. */
95 /* Interface which has this address. */
98 /* Address (prefix) length for this interface. */
101 /* Will be used for something eventually. Primary vs. secondary? */
104 /* Next and previous pointers for doubly linked list of
105 addresses per software interface. */
106 u32 next_this_sw_interface;
107 u32 prev_this_sw_interface;
108 } ip_interface_address_t;
114 IP_LOCAL_NEXT_UDP_LOOKUP,
116 IP_LOCAL_NEXT_REASSEMBLY,
120 struct ip_lookup_main_t;
122 typedef struct ip_lookup_main_t
124 /** Pool of addresses that are assigned to interfaces. */
125 ip_interface_address_t *if_address_pool;
127 /** Hash table mapping address to index in interface address pool. */
128 mhash_t address_to_if_address_index;
130 /** Head of doubly linked list of interface addresses for each software interface.
131 ~0 means this interface has no address. */
132 u32 *if_address_pool_index_by_sw_if_index;
134 /** First table index to use for this interface, ~0 => none */
135 u32 *classify_table_index_by_sw_if_index;
137 /** Feature arc indices */
138 u8 mcast_feature_arc_index;
139 u8 ucast_feature_arc_index;
140 u8 output_feature_arc_index;
142 /** Number of bytes in a fib result. Must be at least
143 sizeof (uword). First word is always adjacency index. */
144 u32 fib_result_n_bytes, fib_result_n_words;
146 /** 1 for ip6; 0 for ip4. */
149 /** Either format_ip4_address_and_length or format_ip6_address_and_length. */
150 format_function_t *format_address_and_length;
152 /** Table mapping ip protocol to ip[46]-local node next index. */
153 u8 local_next_by_ip_protocol[256];
155 /** IP_BUILTIN_PROTOCOL_{TCP,UDP,ICMP,OTHER} by protocol in IP header. */
156 u8 builtin_protocol_by_ip_protocol[256];
159 clib_error_t *ip_interface_address_add_del (ip_lookup_main_t * lm,
163 u32 is_del, u32 * result_index);
165 u8 *format_ip_flow_hash_config (u8 * s, va_list * args);
167 always_inline ip_interface_address_t *
168 ip_get_interface_address (ip_lookup_main_t * lm, void *addr_fib)
170 uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib);
171 return p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0;
175 ip_interface_address_get_address (ip_lookup_main_t * lm,
176 ip_interface_address_t * a)
178 return mhash_key_to_mem (&lm->address_to_if_address_index, a->address_key);
182 #define foreach_ip_interface_address(lm,a,sw_if_index,loop,body) \
184 vnet_main_t *_vnm = vnet_get_main(); \
185 u32 _sw_if_index = sw_if_index; \
186 vnet_sw_interface_t *_swif; \
187 _swif = vnet_get_sw_interface (_vnm, _sw_if_index); \
190 * Loop => honor unnumbered interface addressing. \
192 if (_swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) \
195 _sw_if_index = _swif->unnumbered_sw_if_index; \
197 /* the interface is unnumbered, by the caller does not want \
198 * unnumbered interfaces considered/honoured */ \
201 u32 _ia = ((vec_len((lm)->if_address_pool_index_by_sw_if_index) \
202 > (_sw_if_index)) ? \
203 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, \
206 ip_interface_address_t * _a; \
209 _a = pool_elt_at_index ((lm)->if_address_pool, _ia); \
210 _ia = _a->next_this_sw_interface; \
218 ip_lookup_set_buffer_fib_index (u32 * fib_index_by_sw_if_index,
222 vnet_buffer (b)->ip.fib_index =
223 vec_elt (fib_index_by_sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_RX]);
224 vnet_buffer (b)->ip.fib_index =
225 ((vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
226 vnet_buffer (b)->ip.fib_index :
227 vnet_buffer (b)->sw_if_index[VLIB_TX]);
231 typedef struct _vnet_ip_container_proxy_args
236 } vnet_ip_container_proxy_args_t;
238 clib_error_t *vnet_ip_container_proxy_add_del (vnet_ip_container_proxy_args_t
241 typedef int (*ip_container_proxy_cb_t) (const fib_prefix_t * pfx,
242 u32 sw_if_index, void *ctx);
243 void ip_container_proxy_walk (ip_container_proxy_cb_t cb, void *ctx);
245 void ip_lookup_init (ip_lookup_main_t * lm, u32 ip_lookup_node_index);
247 #endif /* included_ip_lookup_h */
249 * fd.io coding-style-patch-verification: ON
252 * eval: (c-set-style "gnu")