summaryrefslogtreecommitdiffstats
path: root/src/vnet/nhrp/nhrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vnet/nhrp/nhrp.c')
-rw-r--r--src/vnet/nhrp/nhrp.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/vnet/nhrp/nhrp.c b/src/vnet/nhrp/nhrp.c
new file mode 100644
index 00000000000..6a616ba273d
--- /dev/null
+++ b/src/vnet/nhrp/nhrp.c
@@ -0,0 +1,196 @@
+/*
+ * nhrp.h: next-hop resolution
+ *
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include <vnet/nhrp/nhrp.h>
+#include <vnet/fib/fib_table.h>
+#include <vnet/adj/adj_midchain.h>
+
+static uword *nhrp_db;
+static nhrp_entry_t *nhrp_pool;
+
+void
+nhrp_entry_adj_stack (const nhrp_entry_t * ne, adj_index_t ai)
+{
+ adj_midchain_delegate_stack (ai, ne->ne_fib_index, &ne->ne_nh);
+}
+
+static adj_walk_rc_t
+nhrp_entry_add_adj_walk (adj_index_t ai, void *ctx)
+{
+ nhrp_entry_adj_stack (ctx, ai);
+
+ return (ADJ_WALK_RC_CONTINUE);
+}
+
+static adj_walk_rc_t
+nhrp_entry_del_adj_walk (adj_index_t ai, void *ctx)
+{
+ adj_midchain_delegate_unstack (ai);
+
+ return (ADJ_WALK_RC_CONTINUE);
+}
+
+nhrp_entry_t *
+nhrp_entry_get (index_t nei)
+{
+ return pool_elt_at_index (nhrp_pool, nei);
+}
+
+nhrp_entry_t *
+nhrp_entry_find (u32 sw_if_index, const ip46_address_t * peer)
+{
+ nhrp_key_t nk = {
+ .nk_peer = *peer,
+ .nk_sw_if_index = sw_if_index,
+ };
+ uword *p;
+
+ p = hash_get_mem (nhrp_db, &nk);
+
+ if (NULL != p)
+ return nhrp_entry_get (p[0]);
+
+ return (NULL);
+}
+
+int
+nhrp_entry_add (u32 sw_if_index,
+ const ip46_address_t * peer,
+ u32 nh_table_id, const ip46_address_t * nh)
+{
+ fib_protocol_t fproto;
+ nhrp_entry_t *ne;
+ u32 fib_index;
+ index_t nei;
+
+ fproto = (ip46_address_is_ip4 (nh) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
+
+ fib_index = fib_table_find (fproto, nh_table_id);
+
+ if (~0 == fib_index)
+ {
+ return (VNET_API_ERROR_NO_SUCH_FIB);
+ }
+
+ ne = nhrp_entry_find (sw_if_index, peer);
+
+ if (NULL == ne)
+ {
+ nhrp_key_t nk = {
+ .nk_peer = *peer,
+ .nk_sw_if_index = sw_if_index,
+ };
+ nhrp_entry_t *ne;
+
+ pool_get_zero (nhrp_pool, ne);
+
+ nei = ne - nhrp_pool;
+ ne->ne_key = clib_mem_alloc (sizeof (*ne->ne_key));
+ clib_memcpy (ne->ne_key, &nk, sizeof (*ne->ne_key));
+
+ ip46_address_copy (&ne->ne_nh.fp_addr, nh);
+ ne->ne_nh.fp_proto = fproto;
+ ne->ne_nh.fp_len = (ne->ne_nh.fp_proto == FIB_PROTOCOL_IP4 ? 32 : 128);
+ ne->ne_fib_index = fib_index;
+
+ hash_set_mem (nhrp_db, ne->ne_key, nei);
+
+ adj_nbr_walk_nh (sw_if_index,
+ ne->ne_nh.fp_proto,
+ &ne->ne_key->nk_peer, nhrp_entry_add_adj_walk, ne);
+ }
+ else
+ return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
+
+ return 0;
+}
+
+int
+nhrp_entry_del (u32 sw_if_index, const ip46_address_t * peer)
+{
+ nhrp_entry_t *ne;
+
+ ne = nhrp_entry_find (sw_if_index, peer);
+
+ if (ne != NULL)
+ {
+ hash_unset_mem (nhrp_db, ne->ne_key);
+
+ adj_nbr_walk_nh (sw_if_index,
+ ne->ne_nh.fp_proto,
+ &ne->ne_key->nk_peer, nhrp_entry_del_adj_walk, ne);
+
+ clib_mem_free (ne->ne_key);
+ pool_put (nhrp_pool, ne);
+ }
+ else
+ return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
+
+ return 0;
+}
+
+u8 *
+format_nhrp_entry (u8 * s, va_list * args)
+{
+ index_t nei = va_arg (*args, index_t);
+ vnet_main_t *vnm = vnet_get_main ();
+ nhrp_entry_t *ne;
+
+ ne = nhrp_entry_get (nei);
+
+ s = format (s, "[%d] ", nei);
+ s = format (s, "%U:%U ", format_vnet_sw_if_index_name,
+ vnm, ne->ne_key->nk_sw_if_index,
+ format_ip46_address, &ne->ne_key->nk_peer, IP46_TYPE_ANY);
+ s = format (s, "via %d:%U",
+ fib_table_get_table_id (ne->ne_fib_index, ne->ne_nh.fp_proto),
+ format_fib_prefix, &ne->ne_nh);
+
+ return (s);
+}
+
+void
+nhrp_walk (nhrp_walk_cb_t fn, void *ctx)
+{
+ index_t nei;
+
+ /* *INDENT-OFF* */
+ pool_foreach_index(nei, nhrp_pool,
+ ({
+ fn(nei, ctx);
+ }));
+ /* *INDENT-ON* */
+}
+
+static clib_error_t *
+nhrp_init (vlib_main_t * vm)
+{
+ nhrp_db = hash_create_mem (0, sizeof (nhrp_key_t), sizeof (u32));
+
+ return (NULL);
+}
+
+VLIB_INIT_FUNCTION (nhrp_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */