From 976b259be2ce9725f1d6756c14ff81069634a396 Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Wed, 4 Dec 2019 06:11:00 +0000 Subject: fib: Allow the creation of new source on the API Type: feature an client can dump the existing sources, examine their priorities, then define thier own source. Usefull if a client wants to distingusih between say, static, ospf, bgp, etc routes it has added over the API. Signed-off-by: Neale Ranns Signed-off-by: Alexander Chernavin Change-Id: I5158b4fa1ebe87381ff8707bb173217f56ea274a --- src/vnet/fib/fib.api | 52 ++++++++++++++++++++ src/vnet/fib/fib_api.c | 119 +++++++++++++++++++++++++++++++++++++-------- src/vnet/fib/fib_api.h | 1 + src/vnet/fib/fib_table.c | 36 ++++++++++++++ src/vnet/fib/fib_table.h | 11 +++++ src/vnet/fib/fib_types.api | 2 +- 6 files changed, 201 insertions(+), 20 deletions(-) create mode 100644 src/vnet/fib/fib.api (limited to 'src/vnet/fib') diff --git a/src/vnet/fib/fib.api b/src/vnet/fib/fib.api new file mode 100644 index 00000000000..ad83b7402f8 --- /dev/null +++ b/src/vnet/fib/fib.api @@ -0,0 +1,52 @@ +/* Hey Emacs use -*- mode: C -*- */ +/* + * Copyright (c) 2018 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. + */ + +option version = "1.0.0"; + +import "vnet/fib/fib_types.api"; + +typedef fib_source +{ + u8 priority; + u8 id; + string name[64]; +}; + +define fib_source_add +{ + u32 client_index; + u32 context; + vl_api_fib_source_t src; +}; + +define fib_source_add_reply +{ + u32 context; + i32 retval; + u8 id; +}; + +define fib_source_dump +{ + u32 client_index; + u32 context; +}; + +define fib_source_details +{ + u32 context; + vl_api_fib_source_t src; +}; diff --git a/src/vnet/fib/fib_api.c b/src/vnet/fib/fib_api.c index d626ae24502..0254c551411 100644 --- a/src/vnet/fib/fib_api.c +++ b/src/vnet/fib/fib_api.c @@ -22,23 +22,13 @@ #include #include #include +#include -#include - -#define vl_typedefs /* define message structures */ -#include -#undef vl_typedefs - -#define vl_endianfun /* define message structures */ -#include -#undef vl_endianfun - -/* instantiate all the print functions we know about */ -#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) -#define vl_printfun -#include -#undef vl_printfun +#include +#include +static u16 fib_base_msg_id; +#define REPLY_MSG_ID_BASE fib_base_msg_id #include int @@ -461,6 +451,7 @@ fib_api_route_add_del (u8 is_add, u8 is_multipath, u32 fib_index, const fib_prefix_t * prefix, + fib_source_t src, fib_entry_flag_t entry_flags, fib_route_path_t *rpaths) { @@ -473,13 +464,13 @@ fib_api_route_add_del (u8 is_add, if (is_add) fib_table_entry_path_add2 (fib_index, prefix, - FIB_SOURCE_API, + src, entry_flags, rpaths); else fib_table_entry_path_remove2 (fib_index, prefix, - FIB_SOURCE_API, + src, rpaths); } else @@ -492,7 +483,7 @@ fib_api_route_add_del (u8 is_add, /* path replacement */ fib_table_entry_update (fib_index, prefix, - FIB_SOURCE_API, + src, entry_flags, rpaths); } @@ -500,7 +491,7 @@ fib_api_route_add_del (u8 is_add, /* entry delete */ fib_table_entry_delete (fib_index, prefix, - FIB_SOURCE_API); + src); } return (0); @@ -569,3 +560,93 @@ fib_proto_to_api_address_family (fib_protocol_t fproto) ASSERT(0); return (ADDRESS_IP4); } + +void +vl_api_fib_source_add_t_handler (vl_api_fib_source_add_t * mp) +{ + vl_api_fib_source_add_reply_t *rmp; + fib_source_t src; + int rv = 0; + u8 *name; + + name = format (0, "%s", mp->src.name); + vec_add1 (name, 0); + + src = fib_source_allocate((const char *)name, + mp->src.priority, + FIB_SOURCE_BH_API); + + vec_free(name); + + REPLY_MACRO2 (VL_API_FIB_SOURCE_ADD_REPLY, + ({ + rmp->id = src; + })); +} + +typedef struct fib_source_dump_ctx_t_ +{ + vl_api_registration_t * reg; + u32 context; +} fib_source_dump_ctx_t; + +static walk_rc_t +send_fib_source (fib_source_t id, + const char *name, + fib_source_priority_t prio, + fib_source_behaviour_t bh, + void *data) +{ + vl_api_fib_source_details_t *mp; + fib_source_dump_ctx_t *ctx; + + ctx = data; + mp = vl_msg_api_alloc_zero (sizeof (*mp)); + if (!mp) + return WALK_STOP; + + mp->_vl_msg_id = ntohs (VL_API_FIB_SOURCE_DETAILS + REPLY_MSG_ID_BASE); + mp->context = ctx->context; + + mp->src.priority = prio; + mp->src.id = id; + clib_memcpy(mp->src.name, name, + clib_min(strlen(name), ARRAY_LEN(mp->src.name))); + + vl_api_send_msg (ctx->reg, (u8 *) mp); + + return (WALK_CONTINUE); +} + +void +vl_api_fib_source_dump_t_handler (vl_api_fib_source_dump_t * mp) +{ + vl_api_registration_t *reg; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + fib_source_dump_ctx_t ctx = { + .reg = reg, + .context = mp->context, + }; + + fib_source_walk(send_fib_source, &ctx); +} + + +#include + +static clib_error_t * +fib_api_hookup (vlib_main_t * vm) +{ + /* + * Set up the (msg_name, crc, message-id) table + */ + fib_base_msg_id = setup_message_id_table (); + + return (NULL); +} + +VLIB_API_INIT_FUNCTION (fib_api_hookup); diff --git a/src/vnet/fib/fib_api.h b/src/vnet/fib/fib_api.h index caa993b78d0..7fd7d16cb33 100644 --- a/src/vnet/fib/fib_api.h +++ b/src/vnet/fib/fib_api.h @@ -45,6 +45,7 @@ extern int fib_api_route_add_del (u8 is_add, u8 is_multipath, u32 fib_index, const fib_prefix_t * prefix, + fib_source_t src, fib_entry_flag_t entry_flags, fib_route_path_t *rpaths); diff --git a/src/vnet/fib/fib_table.c b/src/vnet/fib/fib_table.c index e71e6c36bfb..eaeee5bb921 100644 --- a/src/vnet/fib/fib_table.c +++ b/src/vnet/fib/fib_table.c @@ -1255,6 +1255,42 @@ fib_table_walk (u32 fib_index, } } +typedef struct fib_table_walk_w_src_ctx_t_ +{ + fib_table_walk_fn_t fn; + void *data; + fib_source_t src; +} fib_table_walk_w_src_cxt_t; + +static fib_table_walk_rc_t +fib_table_walk_w_src_cb (fib_node_index_t fei, + void *arg) +{ + fib_table_walk_w_src_cxt_t *ctx = arg; + + if (ctx->src == fib_entry_get_best_source(fei)) + { + return (ctx->fn(fei, ctx->data)); + } + return (FIB_TABLE_WALK_CONTINUE); +} + +void +fib_table_walk_w_src (u32 fib_index, + fib_protocol_t proto, + fib_source_t src, + fib_table_walk_fn_t fn, + void *data) +{ + fib_table_walk_w_src_cxt_t ctx = { + .fn = fn, + .src = src, + .data = data, + }; + + fib_table_walk(fib_index, proto, fib_table_walk_w_src_cb, &ctx); +} + void fib_table_sub_tree_walk (u32 fib_index, fib_protocol_t proto, diff --git a/src/vnet/fib/fib_table.h b/src/vnet/fib/fib_table.h index 201170707a8..11137e173cf 100644 --- a/src/vnet/fib/fib_table.h +++ b/src/vnet/fib/fib_table.h @@ -940,6 +940,17 @@ extern void fib_table_walk(u32 fib_index, fib_table_walk_fn_t fn, void *ctx); +/** + * @brief Walk all entries in a FIB table + * N.B: This is NOT safe to deletes. If you need to delete walk the whole + * table and store elements in a vector, then delete the elements + */ +extern void fib_table_walk_w_src(u32 fib_index, + fib_protocol_t proto, + fib_source_t src, + fib_table_walk_fn_t fn, + void *ctx); + /** * @brief Walk all entries in a sub-tree FIB table. The 'root' paraneter * is the prefix at the root of the sub-tree. diff --git a/src/vnet/fib/fib_types.api b/src/vnet/fib/fib_types.api index 4a5cea79064..c5fbcf8fc29 100644 --- a/src/vnet/fib/fib_types.api +++ b/src/vnet/fib/fib_types.api @@ -14,7 +14,7 @@ * limitations under the License. */ -option version = "2.0.0"; +option version = "2.0.1"; import "vnet/ip/ip_types.api"; /** \brief MPLS label -- cgit 1.2.3-korg