2 * teib.h: Tunnel Endpoint Information Base
4 * Copyright (c) 2020 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_
29 } __clib_packed teib_key_t;
31 STATIC_ASSERT_SIZEOF (teib_key_t, 24);
40 typedef struct teib_db_t_
42 u32 td_n_entries[N_AF];
46 static teib_db_t teib_db;
47 static teib_entry_t *teib_pool;
48 static teib_vft_t *teib_vfts;
49 static vlib_log_class_t teib_logger;
51 #define TEIB_NOTIFY(_te, _fn) { \
53 vec_foreach(_vft, teib_vfts) { \
60 #define TEIB_DBG(...) \
61 vlib_log_debug (teib_logger, __VA_ARGS__);
63 #define TEIB_INFO(...) \
64 vlib_log_notice (teib_logger, __VA_ARGS__);
66 #define TEIB_TE_DBG(_te, _fmt, _args...) \
67 vlib_log_debug (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
68 #define TEIB_TE_INFO(_te, _fmt, _args...) \
69 vlib_log_notice (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args)
72 teib_entry_get_sw_if_index (const teib_entry_t * te)
74 return (te->te_key->tk_sw_if_index);
77 static ip_address_family_t
78 teib_entry_get_af (const teib_entry_t * te)
80 return (ip_addr_version (&te->te_key->tk_peer));
84 teib_entry_get_fib_index (const teib_entry_t * te)
86 return (te->te_nh_fib_index);
90 teib_entry_get_peer (const teib_entry_t * te)
92 return (&te->te_key->tk_peer);
96 teib_entry_get_nh (const teib_entry_t * te)
102 teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai)
104 adj_midchain_delegate_stack (ai, te->te_nh_fib_index, &te->te_nh);
108 teib_entry_get (index_t tei)
110 return pool_elt_at_index (teib_pool, tei);
114 teib_entry_find (u32 sw_if_index, const ip_address_t * peer)
118 .tk_sw_if_index = sw_if_index,
122 p = hash_get_mem (teib_db.td_db, &nk);
125 return teib_entry_get (p[0]);
131 teib_entry_find_46 (u32 sw_if_index,
132 fib_protocol_t fproto, const ip46_address_t * peer)
136 ip_address_from_46 (peer, fproto, &ip);
138 return (teib_entry_find (sw_if_index, &ip));
142 teib_adj_fib_add (const ip_address_t *ip, u32 sw_if_index, u32 peer_fib_index)
144 if (AF_IP6 == ip_addr_version (ip) &&
145 ip6_address_is_link_local_unicast (&ip_addr_v6 (ip)))
147 ip6_ll_prefix_t pfx = {
148 .ilp_addr = ip_addr_v6 (ip),
149 .ilp_sw_if_index = sw_if_index,
151 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
157 ip_address_to_fib_prefix (ip, &pfx);
158 fib_table_entry_path_add (
159 peer_fib_index, &pfx, FIB_SOURCE_ADJ, FIB_ENTRY_FLAG_ATTACHED,
160 fib_proto_to_dpo (pfx.fp_proto), &pfx.fp_addr, sw_if_index, ~0, 1,
161 NULL, FIB_ROUTE_PATH_FLAG_NONE);
163 if (0 == teib_db.td_n_entries[ip_addr_version (ip)]++)
164 fib_table_lock (peer_fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
169 teib_adj_fib_remove (ip_address_t *ip, u32 sw_if_index, u32 peer_fib_index)
171 if (AF_IP6 == ip_addr_version (ip) &&
172 ip6_address_is_link_local_unicast (&ip_addr_v6 (ip)))
174 ip6_ll_prefix_t pfx = {
175 .ilp_addr = ip_addr_v6 (ip),
176 .ilp_sw_if_index = sw_if_index,
178 ip6_ll_table_entry_delete (&pfx);
184 ip_address_to_fib_prefix (ip, &pfx);
185 fib_table_entry_path_remove (
186 peer_fib_index, &pfx, FIB_SOURCE_ADJ, fib_proto_to_dpo (pfx.fp_proto),
187 &pfx.fp_addr, sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
189 if (0 == --teib_db.td_n_entries[ip_addr_version (ip)])
190 fib_table_unlock (peer_fib_index, pfx.fp_proto, FIB_SOURCE_ADJ);
195 teib_entry_add (u32 sw_if_index,
196 const ip_address_t * peer,
197 u32 nh_table_id, const ip_address_t * nh)
199 fib_protocol_t nh_proto;
201 u32 nh_fib_index, peer_fib_index;
204 nh_proto = (AF_IP4 == ip_addr_version (nh) ?
205 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
207 peer_fib_index = fib_table_get_index_for_sw_if_index (
208 ip_address_family_to_fib_proto (peer->version), sw_if_index);
209 nh_fib_index = fib_table_find (nh_proto, nh_table_id);
211 if (~0 == nh_fib_index)
213 return (VNET_API_ERROR_NO_SUCH_FIB);
216 te = teib_entry_find (sw_if_index, peer);
222 .tk_sw_if_index = sw_if_index,
226 pool_get_zero (teib_pool, te);
228 tei = te - teib_pool;
229 te->te_key = clib_mem_alloc (sizeof (*te->te_key));
230 clib_memcpy (te->te_key, &nk, sizeof (*te->te_key));
232 ip_address_to_fib_prefix (nh, &te->te_nh);
233 te->te_nh_fib_index = nh_fib_index;
235 hash_set_mem (teib_db.td_db, te->te_key, tei);
237 /* we how have a /32 in the overlay, add an adj-fib */
238 teib_adj_fib_add (&te->te_key->tk_peer, sw_if_index, peer_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, const ip_address_t * peer)
256 te = teib_entry_find (sw_if_index, peer);
260 TEIB_TE_INFO (te, "removed");
264 peer_fib_index = fib_table_get_index_for_sw_if_index (
265 ip_address_family_to_fib_proto (peer->version), sw_if_index);
267 teib_adj_fib_remove (&te->te_key->tk_peer, sw_if_index, peer_fib_index);
269 hash_unset_mem (teib_db.td_db, te->te_key);
271 TEIB_NOTIFY (te, nv_deleted);
273 clib_mem_free (te->te_key);
274 pool_put (teib_pool, te);
278 TEIB_INFO ("no such entry: %U, %U", format_vnet_sw_if_index_name,
279 vnet_get_main (), sw_if_index, format_ip_address, peer);
280 return (VNET_API_ERROR_NO_SUCH_ENTRY);
286 format_teib_entry (u8 * s, va_list * args)
288 index_t tei = va_arg (*args, index_t);
289 vnet_main_t *vnm = vnet_get_main ();
292 te = teib_entry_get (tei);
294 s = format (s, "[%d] ", tei);
295 s = format (s, "%U:", format_vnet_sw_if_index_name,
296 vnm, te->te_key->tk_sw_if_index);
297 s = format (s, "%U", format_ip_address,
298 &te->te_key->tk_peer, IP46_TYPE_ANY);
299 s = format (s, " via [%d]:%U",
300 fib_table_get_table_id (te->te_nh_fib_index, te->te_nh.fp_proto),
301 format_fib_prefix, &te->te_nh);
307 teib_walk (teib_walk_cb_t fn, void *ctx)
311 pool_foreach_index (tei, teib_pool)
318 teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx)
322 pool_foreach_index (tei, teib_pool)
324 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)))
330 teib_walk_itf_proto (u32 sw_if_index,
331 ip_address_family_t af, teib_walk_cb_t fn, void *ctx)
335 pool_foreach_index (tei, teib_pool)
337 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)) &&
338 af == teib_entry_get_af(teib_entry_get(tei)))
343 typedef struct teib_table_bind_ctx_t_
345 u32 new_peer_fib_index;
346 u32 old_peer_fib_index;
347 } teib_table_bind_ctx_t;
350 teib_walk_table_bind (index_t tei, void *arg)
352 teib_table_bind_ctx_t *ctx = arg;
355 te = teib_entry_get (tei);
357 TEIB_TE_INFO (te, "bind: %d -> %d", ctx->old_peer_fib_index,
358 ctx->new_peer_fib_index);
360 teib_adj_fib_remove (&te->te_key->tk_peer, te->te_key->tk_sw_if_index,
361 ctx->old_peer_fib_index);
362 teib_adj_fib_add (&te->te_key->tk_peer, te->te_key->tk_sw_if_index,
363 ctx->new_peer_fib_index);
365 return (WALK_CONTINUE);
369 teib_table_bind_v4 (ip4_main_t * im,
371 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
373 teib_table_bind_ctx_t ctx = {
374 .old_peer_fib_index = old_fib_index,
375 .new_peer_fib_index = new_fib_index,
378 teib_walk_itf_proto (sw_if_index, AF_IP4, teib_walk_table_bind, &ctx);
382 teib_table_bind_v6 (ip6_main_t * im,
384 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
386 teib_table_bind_ctx_t ctx = {
387 .old_peer_fib_index = old_fib_index,
388 .new_peer_fib_index = new_fib_index,
391 teib_walk_itf_proto (sw_if_index, AF_IP6, teib_walk_table_bind, &ctx);
395 teib_register (const teib_vft_t * vft)
397 vec_add1 (teib_vfts, *vft);
400 static clib_error_t *
401 teib_init (vlib_main_t * vm)
403 teib_db.td_db = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32));
405 ip4_table_bind_callback_t cb4 = {
406 .function = teib_table_bind_v4,
408 vec_add1 (ip4_main.table_bind_callbacks, cb4);
410 ip6_table_bind_callback_t cb6 = {
411 .function = teib_table_bind_v6,
413 vec_add1 (ip6_main.table_bind_callbacks, cb6);
415 teib_logger = vlib_log_register_class ("teib", "teib");
420 VLIB_INIT_FUNCTION (teib_init);
423 * fd.io coding-style-patch-verification: ON
426 * eval: (c-set-style "gnu")