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>
23 typedef struct teib_key_t_
25 ip46_address_t tk_peer;
36 static uword *teib_db;
37 static teib_entry_t *teib_pool;
38 static teib_vft_t *teib_vfts;
40 #define TEIB_NOTIFY(_te, _fn) { \
42 vec_foreach(_vft, teib_vfts) { \
50 teib_entry_get_sw_if_index (const teib_entry_t * te)
52 return (te->te_key->tk_sw_if_index);
56 teib_entry_get_fib_index (const teib_entry_t * te)
58 return (te->te_fib_index);
61 const ip46_address_t *
62 teib_entry_get_peer (const teib_entry_t * te)
64 return (&te->te_key->tk_peer);
68 teib_entry_get_nh (const teib_entry_t * te)
74 teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai)
76 adj_midchain_delegate_stack (ai, te->te_fib_index, &te->te_nh);
80 teib_entry_get (index_t tei)
82 return pool_elt_at_index (teib_pool, tei);
86 teib_entry_find (u32 sw_if_index, const ip46_address_t * peer)
90 .tk_sw_if_index = sw_if_index,
94 p = hash_get_mem (teib_db, &nk);
97 return teib_entry_get (p[0]);
103 teib_entry_add (u32 sw_if_index,
104 const ip46_address_t * peer,
105 u32 nh_table_id, const ip46_address_t * nh)
107 fib_protocol_t fproto;
112 fproto = (ip46_address_is_ip4 (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
114 fib_index = fib_table_find (fproto, nh_table_id);
118 return (VNET_API_ERROR_NO_SUCH_FIB);
121 te = teib_entry_find (sw_if_index, peer);
127 .tk_sw_if_index = sw_if_index,
131 pool_get_zero (teib_pool, te);
133 tei = te - teib_pool;
134 te->te_key = clib_mem_alloc (sizeof (*te->te_key));
135 clib_memcpy (te->te_key, &nk, sizeof (*te->te_key));
137 ip46_address_copy (&te->te_nh.fp_addr, nh);
138 te->te_nh.fp_proto = fproto;
139 te->te_nh.fp_len = (te->te_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128);
140 te->te_fib_index = fib_index;
142 hash_set_mem (teib_db, te->te_key, tei);
144 TEIB_NOTIFY (te, nv_added);
147 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
153 teib_entry_del (u32 sw_if_index, const ip46_address_t * peer)
157 te = teib_entry_find (sw_if_index, peer);
161 hash_unset_mem (teib_db, te->te_key);
163 TEIB_NOTIFY (te, nv_deleted);
165 clib_mem_free (te->te_key);
166 pool_put (teib_pool, te);
169 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
175 format_teib_entry (u8 * s, va_list * args)
177 index_t tei = va_arg (*args, index_t);
178 vnet_main_t *vnm = vnet_get_main ();
181 te = teib_entry_get (tei);
183 s = format (s, "[%d] ", tei);
184 s = format (s, "%U:", format_vnet_sw_if_index_name,
185 vnm, te->te_key->tk_sw_if_index);
186 s = format (s, " %U", format_ip46_address,
187 &te->te_key->tk_peer, IP46_TYPE_ANY);
188 s = format (s, " via [%d]:%U",
189 fib_table_get_table_id (te->te_fib_index, te->te_nh.fp_proto),
190 format_fib_prefix, &te->te_nh);
196 teib_walk (teib_walk_cb_t fn, void *ctx)
201 pool_foreach_index(tei, teib_pool,
209 teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx)
214 pool_foreach_index(tei, teib_pool,
216 if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)))
223 teib_register (const teib_vft_t * vft)
225 vec_add1 (teib_vfts, *vft);
228 static clib_error_t *
229 teib_init (vlib_main_t * vm)
231 teib_db = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32));
236 VLIB_INIT_FUNCTION (teib_init);
239 * fd.io coding-style-patch-verification: ON
242 * eval: (c-set-style "gnu")