2 * teib.h: next-hop resolution
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <vnet/teib/teib.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/adj/adj_midchain.h>
22 #include <vnet/ip/ip6_ll_table.h>
24 typedef struct teib_key_t_
26 ip46_address_t tk_peer;
28 fib_protocol_t tk_proto;
29 } __clib_packed teib_key_t;
38 static uword *teib_db[FIB_PROTOCOL_IP_MAX];
39 static teib_entry_t *teib_pool;
40 static teib_vft_t *teib_vfts;
41 static vlib_log_class_t teib_logger;
43 #define TEIB_NOTIFY(_te, _fn) { \
45 vec_foreach(_vft, teib_vfts) { \
52 #define TEIB_DBG(...) \
53 vlib_log_debug (teib_logger, __VA_ARGS__);
55 #define TEIB_INFO(...) \
56 vlib_log_notice (teib_logger, __VA_ARGS__);
58 #define TEIB_TE_DBG(_te, _fmt, _args...) \
59 vlib_log_debug (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
60 #define TEIB_TE_INFO(_te, _fmt, _args...) \
61 vlib_log_notice (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
64 teib_entry_get_sw_if_index (const teib_entry_t * te)
66 return (te->te_key->tk_sw_if_index);
70 teib_entry_get_proto (const teib_entry_t * te)
72 return (te->te_key->tk_proto);
76 teib_entry_get_fib_index (const teib_entry_t * te)
78 return (te->te_fib_index);
81 const ip46_address_t *
82 teib_entry_get_peer (const teib_entry_t * te)
84 return (&te->te_key->tk_peer);
88 teib_entry_get_nh (const teib_entry_t * te)
94 teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai)
96 adj_midchain_delegate_stack (ai, te->te_fib_index, &te->te_nh);
100 teib_entry_get (index_t tei)
102 return pool_elt_at_index (teib_pool, tei);
106 teib_entry_find (u32 sw_if_index,
107 fib_protocol_t fproto, const ip46_address_t * peer)
112 .tk_sw_if_index = sw_if_index,
116 p = hash_get_mem (teib_db[fproto], &nk);
119 return teib_entry_get (p[0]);
125 teib_adj_fib_add (fib_protocol_t fproto,
126 const ip46_address_t * ip, u32 sw_if_index, u32 fib_index)
128 if (FIB_PROTOCOL_IP6 == fproto &&
129 ip6_address_is_link_local_unicast (&ip->ip6))
131 ip6_ll_prefix_t pfx = {
133 .ilp_sw_if_index = sw_if_index,
135 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
140 .fp_len = (FIB_PROTOCOL_IP4 == fproto ? 32 : 128),
144 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
145 FIB_ENTRY_FLAG_ATTACHED,
146 fib_proto_to_dpo (pfx.fp_proto),
149 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
152 if (1 == hash_elts (teib_db[pfx.fp_proto]))
153 fib_table_lock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
158 teib_adj_fib_remove (fib_protocol_t fproto,
159 ip46_address_t * ip, u32 sw_if_index, u32 fib_index)
161 if (FIB_PROTOCOL_IP6 == fproto &&
162 ip6_address_is_link_local_unicast (&ip->ip6))
164 ip6_ll_prefix_t pfx = {
166 .ilp_sw_if_index = sw_if_index,
168 ip6_ll_table_entry_delete (&pfx);
173 .fp_len = (FIB_PROTOCOL_IP4 == fproto ? 32 : 128),
178 fib_table_entry_path_remove (fib_index, &pfx, FIB_SOURCE_ADJ,
179 fib_proto_to_dpo (pfx.fp_proto),
182 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
184 if (0 == hash_elts (teib_db[pfx.fp_proto]))
185 fib_table_unlock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
190 teib_entry_add (u32 sw_if_index,
191 fib_protocol_t fproto,
192 const ip46_address_t * peer,
193 u32 nh_table_id, const ip46_address_t * nh)
195 fib_protocol_t nh_proto;
200 nh_proto = (ip46_address_is_ip4 (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
202 fib_index = fib_table_find (nh_proto, nh_table_id);
206 return (VNET_API_ERROR_NO_SUCH_FIB);
209 te = teib_entry_find (sw_if_index, fproto, peer);
216 .tk_sw_if_index = sw_if_index,
221 fib_index = fib_table_get_index_for_sw_if_index (fproto, sw_if_index);
223 pool_get_zero (teib_pool, te);
225 tei = te - teib_pool;
226 te->te_key = clib_mem_alloc (sizeof (*te->te_key));
227 clib_memcpy (te->te_key, &nk, sizeof (*te->te_key));
229 ip46_address_copy (&te->te_nh.fp_addr, nh);
230 te->te_nh.fp_proto = fproto;
231 te->te_nh.fp_len = (te->te_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128);
232 te->te_fib_index = fib_index;
234 hash_set_mem (teib_db[fproto], te->te_key, tei);
236 /* we how have a /32 in the overlay, add an adj-fib */
237 teib_adj_fib_add (te->te_key->tk_proto,
238 &te->te_key->tk_peer, sw_if_index, fib_index);
240 TEIB_NOTIFY (te, nv_added);
241 TEIB_TE_INFO (te, "created");
245 TEIB_TE_INFO (te, "exists");
246 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
252 teib_entry_del (u32 sw_if_index,
253 fib_protocol_t fproto, const ip46_address_t * peer)
257 te = teib_entry_find (sw_if_index, fproto, peer);
261 TEIB_TE_INFO (te, "removed");
265 fib_index = fib_table_get_index_for_sw_if_index (fproto, sw_if_index);
267 teib_adj_fib_remove (te->te_key->tk_proto,
268 &te->te_key->tk_peer, sw_if_index, fib_index);
270 hash_unset_mem (teib_db[fproto], te->te_key);
272 TEIB_NOTIFY (te, nv_deleted);
274 clib_mem_free (te->te_key);
275 pool_put (teib_pool, te);
279 TEIB_INFO ("no such entry: %U, %U, %U",
280 format_vnet_sw_if_index_name,
281 vnet_get_main (), sw_if_index,
282 format_fib_protocol, fproto,
283 format_ip46_address, peer, IP46_TYPE_ANY);
284 return (VNET_API_ERROR_NO_SUCH_ENTRY);
290 format_teib_entry (u8 * s, va_list * args)
292 index_t tei = va_arg (*args, index_t);
293 vnet_main_t *vnm = vnet_get_main ();
296 te = teib_entry_get (tei);
298 s = format (s, "[%d] ", tei);
299 s = format (s, "%U:", format_vnet_sw_if_index_name,
300 vnm, te->te_key->tk_sw_if_index);
301 s = format (s, " %U:", format_fib_protocol, te->te_key->tk_proto);
302 s = format (s, "%U", format_ip46_address,
303 &te->te_key->tk_peer, IP46_TYPE_ANY);
304 s = format (s, " via [%d]:%U",
305 fib_table_get_table_id (te->te_fib_index, te->te_nh.fp_proto),
306 format_fib_prefix, &te->te_nh);
312 teib_walk (teib_walk_cb_t fn, void *ctx)
317 pool_foreach_index(tei, teib_pool,
325 teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx)
330 pool_foreach_index(tei, teib_pool,
332 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)))
339 teib_walk_itf_proto (u32 sw_if_index,
340 fib_protocol_t fproto, teib_walk_cb_t fn, void *ctx)
345 pool_foreach_index(tei, teib_pool,
347 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)) &&
348 fproto == teib_entry_get_proto(teib_entry_get(tei)))
354 typedef struct teib_table_bind_ctx_t_
358 } teib_table_bind_ctx_t;
361 teib_walk_table_bind (index_t tei, void *arg)
363 teib_table_bind_ctx_t *ctx = arg;
366 te = teib_entry_get (tei);
368 TEIB_TE_INFO (te, "bind: %d -> %d", ctx->old_fib_index, ctx->new_fib_index);
370 teib_adj_fib_remove (te->te_key->tk_proto,
371 &te->te_key->tk_peer,
372 te->te_key->tk_sw_if_index, ctx->old_fib_index);
373 teib_adj_fib_add (te->te_key->tk_proto,
374 &te->te_key->tk_peer,
375 te->te_key->tk_sw_if_index, ctx->new_fib_index);
377 return (WALK_CONTINUE);
381 teib_table_bind_v4 (ip4_main_t * im,
383 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
385 teib_table_bind_ctx_t ctx = {
386 .old_fib_index = old_fib_index,
387 .new_fib_index = new_fib_index,
390 teib_walk_itf_proto (sw_if_index,
391 FIB_PROTOCOL_IP4, teib_walk_table_bind, &ctx);
395 teib_table_bind_v6 (ip6_main_t * im,
397 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
399 teib_table_bind_ctx_t ctx = {
400 .old_fib_index = old_fib_index,
401 .new_fib_index = new_fib_index,
404 teib_walk_itf_proto (sw_if_index,
405 FIB_PROTOCOL_IP6, teib_walk_table_bind, &ctx);
409 teib_register (const teib_vft_t * vft)
411 vec_add1 (teib_vfts, *vft);
414 static clib_error_t *
415 teib_init (vlib_main_t * vm)
417 fib_protocol_t fproto;
419 FOR_EACH_FIB_IP_PROTOCOL (fproto)
420 teib_db[fproto] = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32));
422 ip4_table_bind_callback_t cb4 = {
423 .function = teib_table_bind_v4,
425 vec_add1 (ip4_main.table_bind_callbacks, cb4);
427 ip6_table_bind_callback_t cb6 = {
428 .function = teib_table_bind_v6,
430 vec_add1 (ip6_main.table_bind_callbacks, cb6);
432 teib_logger = vlib_log_register_class ("teib", "teib");
437 VLIB_INIT_FUNCTION (teib_init);
440 * fd.io coding-style-patch-verification: ON
443 * eval: (c-set-style "gnu")