2 * nhrp.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/nhrp/nhrp.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/adj/adj_midchain.h>
23 typedef struct nhrp_key_t_
25 ip46_address_t nk_peer;
36 static uword *nhrp_db;
37 static nhrp_entry_t *nhrp_pool;
38 static nhrp_vft_t *nhrp_vfts;
40 #define NHRP_NOTIFY(_ne, _fn) { \
42 vec_foreach(_vft, nhrp_vfts) { \
50 nhrp_entry_get_sw_if_index (const nhrp_entry_t * ne)
52 return (ne->ne_key->nk_sw_if_index);
56 nhrp_entry_get_fib_index (const nhrp_entry_t * ne)
58 return (ne->ne_fib_index);
61 const ip46_address_t *
62 nhrp_entry_get_peer (const nhrp_entry_t * ne)
64 return (&ne->ne_key->nk_peer);
68 nhrp_entry_get_nh (const nhrp_entry_t * ne)
74 nhrp_entry_adj_stack (const nhrp_entry_t * ne, adj_index_t ai)
76 adj_midchain_delegate_stack (ai, ne->ne_fib_index, &ne->ne_nh);
80 nhrp_entry_get (index_t nei)
82 return pool_elt_at_index (nhrp_pool, nei);
86 nhrp_entry_find (u32 sw_if_index, const ip46_address_t * peer)
90 .nk_sw_if_index = sw_if_index,
94 p = hash_get_mem (nhrp_db, &nk);
97 return nhrp_entry_get (p[0]);
103 nhrp_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 ne = nhrp_entry_find (sw_if_index, peer);
127 .nk_sw_if_index = sw_if_index,
131 pool_get_zero (nhrp_pool, ne);
133 nei = ne - nhrp_pool;
134 ne->ne_key = clib_mem_alloc (sizeof (*ne->ne_key));
135 clib_memcpy (ne->ne_key, &nk, sizeof (*ne->ne_key));
137 ip46_address_copy (&ne->ne_nh.fp_addr, nh);
138 ne->ne_nh.fp_proto = fproto;
139 ne->ne_nh.fp_len = (ne->ne_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128);
140 ne->ne_fib_index = fib_index;
142 hash_set_mem (nhrp_db, ne->ne_key, nei);
144 NHRP_NOTIFY (ne, nv_added);
147 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
153 nhrp_entry_del (u32 sw_if_index, const ip46_address_t * peer)
157 ne = nhrp_entry_find (sw_if_index, peer);
161 hash_unset_mem (nhrp_db, ne->ne_key);
163 NHRP_NOTIFY (ne, nv_deleted);
165 clib_mem_free (ne->ne_key);
166 pool_put (nhrp_pool, ne);
169 return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
175 format_nhrp_entry (u8 * s, va_list * args)
177 index_t nei = va_arg (*args, index_t);
178 vnet_main_t *vnm = vnet_get_main ();
181 ne = nhrp_entry_get (nei);
183 s = format (s, "[%d] ", nei);
184 s = format (s, "%U:", format_vnet_sw_if_index_name,
185 vnm, ne->ne_key->nk_sw_if_index);
186 s = format (s, " %U", format_ip46_address,
187 &ne->ne_key->nk_peer, IP46_TYPE_ANY);
188 s = format (s, " via [%d]:%U",
189 fib_table_get_table_id (ne->ne_fib_index, ne->ne_nh.fp_proto),
190 format_fib_prefix, &ne->ne_nh);
196 nhrp_walk (nhrp_walk_cb_t fn, void *ctx)
201 pool_foreach_index(nei, nhrp_pool,
209 nhrp_walk_itf (u32 sw_if_index, nhrp_walk_cb_t fn, void *ctx)
214 pool_foreach_index(nei, nhrp_pool,
216 if (sw_if_index == nhrp_entry_get_sw_if_index(nhrp_entry_get(nei)))
223 nhrp_register (const nhrp_vft_t * vft)
225 vec_add1 (nhrp_vfts, *vft);
228 static clib_error_t *
229 nhrp_init (vlib_main_t * vm)
231 nhrp_db = hash_create_mem (0, sizeof (nhrp_key_t), sizeof (u32));
236 VLIB_INIT_FUNCTION (nhrp_init);
239 * fd.io coding-style-patch-verification: ON
242 * eval: (c-set-style "gnu")