diff options
author | Neale Ranns <nranns@cisco.com> | 2019-12-04 06:11:00 +0000 |
---|---|---|
committer | Neale Ranns <neale@graphiant.com> | 2021-03-16 12:12:23 +0000 |
commit | 976b259be2ce9725f1d6756c14ff81069634a396 (patch) | |
tree | f4117b32e9230e5c313474a158dbfa847943b99d /src/vnet/ip | |
parent | 400ce717ac043ceec404103dbaa87dd17d92feb7 (diff) |
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 <nranns@cisco.com>
Signed-off-by: Alexander Chernavin <achernavin@netgate.com>
Change-Id: I5158b4fa1ebe87381ff8707bb173217f56ea274a
Diffstat (limited to 'src/vnet/ip')
-rw-r--r-- | src/vnet/ip/ip.api | 62 | ||||
-rw-r--r-- | src/vnet/ip/ip_api.c | 218 |
2 files changed, 276 insertions, 4 deletions
diff --git a/src/vnet/ip/ip.api b/src/vnet/ip/ip.api index f49fc16bc1d..c8d4c397182 100644 --- a/src/vnet/ip/ip.api +++ b/src/vnet/ip/ip.api @@ -20,7 +20,7 @@ called through a shared memory interface. */ -option version = "3.0.3"; +option version = "3.1.0"; import "vnet/interface_types.api"; import "vnet/fib/fib_types.api"; @@ -135,6 +135,8 @@ define ip_table_details @param stats_index The index of the route in the stats segment @param prefix the prefix for the route @param n_paths The number of paths the route has + @param src The entity adding the route. either 0 for default + or a value returned from fib_source_sdd. @param paths The paths of the route */ typedef ip_route @@ -145,6 +147,15 @@ typedef ip_route u8 n_paths; vl_api_fib_path_t paths[n_paths]; }; +typedef ip_route_v2 +{ + u32 table_id; + u32 stats_index; + vl_api_prefix_t prefix; + u8 n_paths; + u8 src; + vl_api_fib_path_t paths[n_paths]; +}; /** \brief Add / del route request @param client_index - opaque cookie to identify the sender @@ -163,15 +174,33 @@ define ip_route_add_del bool is_multipath; vl_api_ip_route_t route; }; +define ip_route_add_del_v2 +{ + option in_progress; + u32 client_index; + u32 context; + bool is_add [default=true]; + bool is_multipath; + vl_api_ip_route_v2_t route; +}; define ip_route_add_del_reply { u32 context; i32 retval; u32 stats_index; }; +define ip_route_add_del_v2_reply +{ + option in_progress; + u32 context; + i32 retval; + u32 stats_index; +}; /** \brief Dump IP routes from a table @param client_index - opaque cookie to identify the sender + @param src The entity adding the route. either 0 for default + or a value returned from fib_source_sdd. @param table - The table from which to dump routes (ony ID an AF are needed) */ define ip_route_dump @@ -180,6 +209,15 @@ define ip_route_dump u32 context; vl_api_ip_table_t table; }; +define ip_route_v2_dump +{ + option in_progress; + u32 client_index; + u32 context; + /* vl_api_fib_source_t src; */ + u8 src; + vl_api_ip_table_t table; +}; /** \brief IP FIB table entry response @param route The route entry in the table @@ -189,6 +227,12 @@ define ip_route_details u32 context; vl_api_ip_route_t route; }; +define ip_route_v2_details +{ + option in_progress; + u32 context; + vl_api_ip_route_v2_t route; +}; /** \brief Lookup IP route from a table @param client_index - opaque cookie to identify the sender @@ -204,6 +248,15 @@ define ip_route_lookup u8 exact; vl_api_prefix_t prefix; }; +define ip_route_lookup_v2 +{ + option in_progress; + u32 client_index; + u32 context; + u32 table_id; + u8 exact; + vl_api_prefix_t prefix; +}; /** \brief IP FIB table lookup response @param retval - return code of the lookup @@ -215,6 +268,13 @@ define ip_route_lookup_reply i32 retval; vl_api_ip_route_t route; }; +define ip_route_lookup_v2_reply +{ + option in_progress; + u32 context; + i32 retval; + vl_api_ip_route_v2_t route; +}; /** \brief Set the ip flow hash config for a fib request @param client_index - opaque cookie to identify the sender diff --git a/src/vnet/ip/ip_api.c b/src/vnet/ip/ip_api.c index 5b87f7cc86a..91b12e5896a 100644 --- a/src/vnet/ip/ip_api.c +++ b/src/vnet/ip/ip_api.c @@ -72,6 +72,7 @@ _ (SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable) \ _ (IP_TABLE_DUMP, ip_table_dump) \ _ (IP_ROUTE_DUMP, ip_route_dump) \ + _ (IP_ROUTE_V2_DUMP, ip_route_v2_dump) \ _ (IP_MTABLE_DUMP, ip_mtable_dump) \ _ (IP_MROUTE_DUMP, ip_mroute_dump) \ _ (IP_MROUTE_ADD_DEL, ip_mroute_add_del) \ @@ -83,7 +84,9 @@ _ (IP_TABLE_REPLACE_END, ip_table_replace_end) \ _ (IP_TABLE_FLUSH, ip_table_flush) \ _ (IP_ROUTE_ADD_DEL, ip_route_add_del) \ + _ (IP_ROUTE_ADD_DEL_V2, ip_route_add_del_v2) \ _ (IP_ROUTE_LOOKUP, ip_route_lookup) \ + _ (IP_ROUTE_LOOKUP_V2, ip_route_lookup_v2) \ _ (IP_TABLE_ADD_DEL, ip_table_add_del) \ _ (IP_PUNT_POLICE, ip_punt_police) \ _ (IP_PUNT_REDIRECT, ip_punt_redirect) \ @@ -235,6 +238,47 @@ send_ip_route_details (vpe_api_main_t * am, vec_free (rpaths); } +static void +send_ip_route_v2_details (vpe_api_main_t *am, vl_api_registration_t *reg, + u32 context, fib_node_index_t fib_entry_index) +{ + fib_route_path_t *rpaths, *rpath; + vl_api_ip_route_v2_details_t *mp; + const fib_prefix_t *pfx; + vl_api_fib_path_t *fp; + int path_count; + + rpaths = NULL; + pfx = fib_entry_get_prefix (fib_entry_index); + rpaths = fib_entry_encode (fib_entry_index); + + path_count = vec_len (rpaths); + mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp)); + if (!mp) + return; + clib_memset (mp, 0, sizeof (*mp)); + mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_V2_DETAILS); + mp->context = context; + + ip_prefix_encode (pfx, &mp->route.prefix); + mp->route.table_id = htonl (fib_table_get_table_id ( + fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto)); + mp->route.n_paths = path_count; + mp->route.src = fib_entry_get_best_source (fib_entry_index); + mp->route.stats_index = htonl (fib_table_entry_get_stats_index ( + fib_entry_get_fib_index (fib_entry_index), pfx)); + + fp = mp->route.paths; + vec_foreach (rpath, rpaths) + { + fib_api_path_encode (rpath, fp); + fp++; + } + + vl_api_send_msg (reg, (u8 *) mp); + vec_free (rpaths); +} + typedef struct apt_ip6_fib_show_ctx_t_ { fib_node_index_t *entries; @@ -274,6 +318,45 @@ vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp) } static void +vl_api_ip_route_v2_dump_t_handler (vl_api_ip_route_v2_dump_t *mp) +{ + vpe_api_main_t *am = &vpe_api_main; + fib_node_index_t *fib_entry_index; + vl_api_registration_t *reg; + fib_protocol_t fproto; + fib_source_t src; + u32 fib_index; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + vl_api_ip_fib_dump_walk_ctx_t ctx = { + .feis = NULL, + }; + + fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4); + fib_index = fib_table_find (fproto, ntohl (mp->table.table_id)); + src = mp->src; + + if (INDEX_INVALID == fib_index) + return; + + if (src) + fib_table_walk_w_src (fib_index, fproto, src, vl_api_ip_fib_dump_walk, + &ctx); + else + fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx); + + vec_foreach (fib_entry_index, ctx.feis) + { + send_ip_route_v2_details (am, reg, mp->context, *fib_entry_index); + } + + vec_free (ctx.feis); +} + +static void send_ip_mtable_details (vl_api_registration_t * reg, u32 context, const mfib_table_t * mfib_table) { @@ -584,9 +667,60 @@ ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index) goto out; } - rv = fib_api_route_add_del (mp->is_add, - mp->is_multipath, - fib_index, &pfx, entry_flags, rpaths); + rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx, + FIB_SOURCE_API, entry_flags, rpaths); + + if (mp->is_add && 0 == rv) + *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx); + +out: + vec_free (rpaths); + + return (rv); +} + +static int +ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp, + u32 *stats_index) +{ + fib_route_path_t *rpaths = NULL, *rpath; + fib_entry_flag_t entry_flags; + vl_api_fib_path_t *apath; + fib_source_t src; + fib_prefix_t pfx; + u32 fib_index; + int rv, ii; + + entry_flags = FIB_ENTRY_FLAG_NONE; + ip_prefix_decode (&mp->route.prefix, &pfx); + + rv = fib_api_table_id_decode (pfx.fp_proto, ntohl (mp->route.table_id), + &fib_index); + if (0 != rv) + goto out; + + if (0 != mp->route.n_paths) + vec_validate (rpaths, mp->route.n_paths - 1); + + for (ii = 0; ii < mp->route.n_paths; ii++) + { + apath = &mp->route.paths[ii]; + rpath = &rpaths[ii]; + + rv = fib_api_path_decode (apath, rpath); + + if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) && + (~0 == rpath->frp_sw_if_index)) + entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL); + + if (0 != rv) + goto out; + } + + src = (0 == mp->route.src ? FIB_SOURCE_API : mp->route.src); + + rv = fib_api_route_add_del (mp->is_add, mp->is_multipath, fib_index, &pfx, + src, entry_flags, rpaths); if (mp->is_add && 0 == rv) *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx); @@ -615,6 +749,23 @@ vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp) } void +vl_api_ip_route_add_del_v2_t_handler (vl_api_ip_route_add_del_v2_t *mp) +{ + vl_api_ip_route_add_del_v2_reply_t *rmp; + u32 stats_index = ~0; + int rv; + + rv = ip_route_add_del_v2_t_handler (mp, &stats_index); + + /* clang-format off */ + REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_V2_REPLY, + ({ + rmp->stats_index = htonl (stats_index); + })) + /* clang-format on */ +} + +void vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp) { vl_api_ip_route_lookup_reply_t *rmp = NULL; @@ -671,6 +822,65 @@ vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp) } void +vl_api_ip_route_lookup_v2_t_handler (vl_api_ip_route_lookup_v2_t *mp) +{ + vl_api_ip_route_lookup_v2_reply_t *rmp = NULL; + fib_route_path_t *rpaths = NULL, *rpath; + const fib_prefix_t *pfx = NULL; + fib_prefix_t lookup; + vl_api_fib_path_t *fp; + fib_node_index_t fib_entry_index; + u32 fib_index; + int npaths = 0; + fib_source_t src = 0; + int rv; + + ip_prefix_decode (&mp->prefix, &lookup); + rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id), + &fib_index); + if (PREDICT_TRUE (!rv)) + { + if (mp->exact) + fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup); + else + fib_entry_index = fib_table_lookup (fib_index, &lookup); + if (fib_entry_index == FIB_NODE_INDEX_INVALID) + rv = VNET_API_ERROR_NO_SUCH_ENTRY; + else + { + pfx = fib_entry_get_prefix (fib_entry_index); + rpaths = fib_entry_encode (fib_entry_index); + npaths = vec_len (rpaths); + src = fib_entry_get_best_source (fib_entry_index); + } + } + + /* clang-format off */ + REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_V2_REPLY, + npaths * sizeof (*fp), + ({ + if (!rv) + { + ip_prefix_encode (pfx, &rmp->route.prefix); + rmp->route.table_id = mp->table_id; + rmp->route.n_paths = npaths; + rmp->route.src = src; + rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx); + rmp->route.stats_index = htonl (rmp->route.stats_index); + + fp = rmp->route.paths; + vec_foreach (rpath, rpaths) + { + fib_api_path_encode (rpath, fp); + fp++; + } + } + })); + /* clang-format on */ + vec_free (rpaths); +} + +void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api, const u8 * name) { @@ -1797,6 +2007,8 @@ ip_api_hookup (vlib_main_t * vm) */ am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1; am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1; + am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2] = 1; + am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_V2_REPLY] = 1; /* * Set up the (msg_name, crc, message-id) table |