diff options
Diffstat (limited to 'src/vnet/srv6')
-rwxr-xr-x | src/vnet/srv6/sr.h | 55 | ||||
-rw-r--r-- | src/vnet/srv6/sr_api.c | 5 | ||||
-rwxr-xr-x | src/vnet/srv6/sr_policy_rewrite.c | 216 |
3 files changed, 258 insertions, 18 deletions
diff --git a/src/vnet/srv6/sr.h b/src/vnet/srv6/sr.h index 11935833394..6a228c8fc23 100755 --- a/src/vnet/srv6/sr.h +++ b/src/vnet/srv6/sr.h @@ -77,6 +77,9 @@ typedef struct dpo_id_t bsid_dpo; /**< DPO for Encaps/Insert for BSID */ dpo_id_t ip6_dpo; /**< DPO for Encaps/Insert IPv6 */ dpo_id_t ip4_dpo; /**< DPO for Encaps IPv6 */ + + u16 plugin; + void *plugin_mem; } ip6_sr_sl_t; /* SR policy types */ @@ -102,8 +105,13 @@ typedef struct u32 fib_table; /**< FIB table */ u8 is_encap; /**< Mode (0 is SRH insert, 1 Encaps) */ + + u16 plugin; + void *plugin_mem; } ip6_sr_policy_t; +typedef int (sr_p_plugin_callback_t) (ip6_sr_policy_t * sr); + /** * @brief SR LocalSID */ @@ -165,6 +173,34 @@ typedef struct } sr_localsid_fn_registration_t; /** + * @brief SR Policy behavior registration + */ +typedef struct +{ + u16 sr_policy_function_number; /**< SR Policy plugin function */ + + u8 *function_name; /**< Function name. (key). */ + + u8 *keyword_str; /**< Behavior keyword (i.e. End.X) */ + + u8 *def_str; /**< Behavior definition (i.e. Endpoint with cross-connect) */ + + u8 *params_str; /**< Behavior parameters (i.e. <oif> <IP46next_hop>) */ + + u8 prefix_length; + + dpo_type_t dpo; /**< DPO type registration */ + + format_function_t *ls_format; /**< LocalSID format function */ + + unformat_function_t *ls_unformat; /**< LocalSID unformat function */ + + sr_p_plugin_callback_t *creation; /**< Function within plugin that will be called after localsid creation*/ + + sr_p_plugin_callback_t *removal; /**< Function within plugin that will be called before localsid removal */ +} sr_policy_fn_registration_t; + +/** * @brief Steering db key * * L3 is IPv4/IPv6 + mask @@ -236,6 +272,12 @@ typedef struct /* Find plugin function by name */ uword *plugin_functions_by_key; + /* Plugin functions for Policy */ + sr_policy_fn_registration_t *policy_plugin_functions; + + /* Find plugin function by name */ + uword *policy_plugin_functions_by_key; + /* Counters */ vlib_combined_counter_main_t sr_ls_valid_counters; vlib_combined_counter_main_t sr_ls_invalid_counters; @@ -270,8 +312,19 @@ sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name, sr_plugin_callback_t * removal_fn); extern int +sr_policy_register_function (vlib_main_t * vm, u8 * fn_name, + u8 * keyword_str, u8 * def_str, + u8 * params_str, u8 prefix_length, + dpo_type_t * dpo, + format_function_t * ls_format, + unformat_function_t * ls_unformat, + sr_p_plugin_callback_t * creation_fn, + sr_p_plugin_callback_t * removal_fn); + +extern int sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, - u32 weight, u8 behavior, u32 fib_table, u8 is_encap); + u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + u16 plugin, void *plugin_mem); extern int sr_policy_mod (ip6_address_t * bsid, u32 index, u32 fib_table, u8 operation, ip6_address_t * segments, u32 sl_index, u32 weight); diff --git a/src/vnet/srv6/sr_api.c b/src/vnet/srv6/sr_api.c index 940336d9859..5a7d8c5a2ea 100644 --- a/src/vnet/srv6/sr_api.c +++ b/src/vnet/srv6/sr_api.c @@ -108,13 +108,14 @@ vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp) /* * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments, - * u32 weight, u8 behavior, u32 fib_table, u8 is_encap) + * u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + * u16 behavior, void *plugin_mem) */ int rv = 0; rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr, segments, ntohl (mp->sids.weight), - mp->type, ntohl (mp->fib_table), mp->is_encap); + mp->type, ntohl (mp->fib_table), mp->is_encap, 0, NULL); vec_free (segments); REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY); diff --git a/src/vnet/srv6/sr_policy_rewrite.c b/src/vnet/srv6/sr_policy_rewrite.c index 8d11da67ddf..9c4d0ec493f 100755 --- a/src/vnet/srv6/sr_policy_rewrite.c +++ b/src/vnet/srv6/sr_policy_rewrite.c @@ -340,6 +340,7 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, { ip6_sr_main_t *sm = &sr_main; ip6_sr_sl_t *segment_list; + sr_policy_fn_registration_t *plugin = 0; pool_get (sm->sid_lists, segment_list); clib_memset (segment_list, 0, sizeof (*segment_list)); @@ -349,6 +350,7 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, /* Fill in segment list */ segment_list->weight = (weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT); + segment_list->segments = vec_dup (sl); if (is_encap) @@ -362,6 +364,18 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, segment_list->rewrite_bsid = compute_rewrite_bsid (sl); } + if (sr_policy->plugin) + { + plugin = + pool_elt_at_index (sm->policy_plugin_functions, + sr_policy->plugin - SR_BEHAVIOR_LAST); + + segment_list->plugin = sr_policy->plugin; + segment_list->plugin_mem = sr_policy->plugin_mem; + + plugin->creation (sr_policy); + } + /* Create DPO */ dpo_reset (&segment_list->bsid_dpo); dpo_reset (&segment_list->ip6_dpo); @@ -369,19 +383,41 @@ create_sl (ip6_sr_policy_t * sr_policy, ip6_address_t * sl, u32 weight, if (is_encap) { - dpo_set (&segment_list->ip6_dpo, sr_pr_encaps_dpo_type, DPO_PROTO_IP6, - segment_list - sm->sid_lists); - dpo_set (&segment_list->ip4_dpo, sr_pr_encaps_dpo_type, DPO_PROTO_IP4, - segment_list - sm->sid_lists); - dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_encaps_dpo_type, - DPO_PROTO_IP6, segment_list - sm->sid_lists); + if (!sr_policy->plugin) + { + dpo_set (&segment_list->ip6_dpo, sr_pr_encaps_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + dpo_set (&segment_list->ip4_dpo, sr_pr_encaps_dpo_type, + DPO_PROTO_IP4, segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_encaps_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + } + else + { + dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + dpo_set (&segment_list->ip4_dpo, plugin->dpo, DPO_PROTO_IP4, + segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + } } else { - dpo_set (&segment_list->ip6_dpo, sr_pr_insert_dpo_type, DPO_PROTO_IP6, - segment_list - sm->sid_lists); - dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_insert_dpo_type, - DPO_PROTO_IP6, segment_list - sm->sid_lists); + if (!sr_policy->plugin) + { + dpo_set (&segment_list->ip6_dpo, sr_pr_insert_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, sr_pr_bsid_insert_dpo_type, + DPO_PROTO_IP6, segment_list - sm->sid_lists); + } + else + { + dpo_set (&segment_list->ip6_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + dpo_set (&segment_list->bsid_dpo, plugin->dpo, DPO_PROTO_IP6, + segment_list - sm->sid_lists); + } } return segment_list; @@ -454,7 +490,6 @@ update_lb (ip6_sr_policy_t * sr_policy) } /* Create the LB path vector */ - //path_vector = vec_new(load_balance_path_t, vec_len(sr_policy->segments_lists)); vec_foreach (sl_index, sr_policy->segments_lists) { segment_list = pool_elt_at_index (sm->sid_lists, *sl_index); @@ -483,7 +518,6 @@ update_lb (ip6_sr_policy_t * sr_policy) vec_free (b_path_vector); vec_free (ip6_path_vector); vec_free (ip4_path_vector); - } /** @@ -590,7 +624,8 @@ update_replicate (ip6_sr_policy_t * sr_policy) */ int sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, - u32 weight, u8 behavior, u32 fib_table, u8 is_encap) + u32 weight, u8 behavior, u32 fib_table, u8 is_encap, + u16 plugin, void *ls_plugin_mem) { ip6_sr_main_t *sm = &sr_main; ip6_sr_policy_t *sr_policy = 0; @@ -635,6 +670,12 @@ sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments, sr_policy->fib_table = (fib_table != (u32) ~ 0 ? fib_table : 0); //Is default FIB 0 ? sr_policy->is_encap = is_encap; + if (plugin) + { + sr_policy->plugin = plugin; + sr_policy->plugin_mem = ls_plugin_mem; + } + /* Copy the key */ mhash_set (&sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies, NULL); @@ -730,6 +771,19 @@ sr_policy_del (ip6_address_t * bsid, u32 index) pool_put_index (sm->sid_lists, *sl_index); } + if (sr_policy->plugin) + { + sr_policy_fn_registration_t *plugin = 0; + + plugin = + pool_elt_at_index (sm->policy_plugin_functions, + sr_policy->plugin - SR_BEHAVIOR_LAST); + + plugin->removal (sr_policy); + sr_policy->plugin = 0; + sr_policy->plugin_mem = NULL; + } + /* Remove SR policy entry */ mhash_unset (&sm->sr_policies_index_hash, &sr_policy->bsid, NULL); pool_put (sm->sr_policies, sr_policy); @@ -863,6 +917,7 @@ static clib_error_t * sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { + ip6_sr_main_t *sm = &sr_main; int rv = -1; char is_del = 0, is_add = 0, is_mod = 0; char policy_set = 0; @@ -873,6 +928,8 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, u8 operation = 0; char is_encap = 1; char is_spray = 0; + u16 behavior = 0; + void *ls_plugin_mem = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { @@ -910,6 +967,33 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, is_encap = 0; else if (unformat (input, "spray")) is_spray = 1; + else if (!behavior && unformat (input, "behavior")) + { + sr_policy_fn_registration_t *plugin = 0, **vec_plugins = 0; + sr_policy_fn_registration_t **plugin_it = 0; + + /* *INDENT-OFF* */ + pool_foreach (plugin, sm->policy_plugin_functions, + { + vec_add1 (vec_plugins, plugin); + }); + /* *INDENT-ON* */ + + vec_foreach (plugin_it, vec_plugins) + { + if (unformat + (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem)) + { + behavior = (*plugin_it)->sr_policy_function_number; + break; + } + } + + if (!behavior) + { + return clib_error_return (0, "Invalid behavior"); + } + } else break; } @@ -922,11 +1006,20 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, if (is_add) { + if (behavior && vec_len (segments) == 0) + { + vec_add2 (segments, this_seg, 1); + clib_memset (this_seg, 0, sizeof (*this_seg)); + } + if (vec_len (segments) == 0) return clib_error_return (0, "No Segment List specified"); + rv = sr_policy_add (&bsid, segments, weight, (is_spray ? SR_POLICY_TYPE_SPRAY : - SR_POLICY_TYPE_DEFAULT), fib_table, is_encap); + SR_POLICY_TYPE_DEFAULT), fib_table, is_encap, + behavior, ls_plugin_mem); + vec_free (segments); } else if (is_del) @@ -942,10 +1035,13 @@ sr_policy_command_fn (vlib_main_t * vm, unformat_input_t * input, return clib_error_return (0, "No Segment List specified"); if (operation == 3 && weight == (u32) ~ 0) return clib_error_return (0, "No new weight for the SL specified"); + rv = sr_policy_mod ((sr_policy_index != (u32) ~ 0 ? NULL : &bsid), sr_policy_index, fib_table, operation, segments, sl_index, weight); - vec_free (segments); + + if (segments) + vec_free (segments); } switch (rv) @@ -3200,6 +3296,96 @@ VLIB_REGISTER_NODE (sr_policy_rewrite_b_encaps_node) = { }; /* *INDENT-ON* */ +/*************************** SR Policy plugins ******************************/ +/** + * @brief SR Policy plugin registry + */ +int +sr_policy_register_function (vlib_main_t * vm, u8 * fn_name, + u8 * keyword_str, u8 * def_str, + u8 * params_str, u8 prefix_length, + dpo_type_t * dpo, + format_function_t * ls_format, + unformat_function_t * ls_unformat, + sr_p_plugin_callback_t * creation_fn, + sr_p_plugin_callback_t * removal_fn) +{ + ip6_sr_main_t *sm = &sr_main; + uword *p; + + sr_policy_fn_registration_t *plugin; + + /* Did this function exist? If so update it */ + p = hash_get_mem (sm->policy_plugin_functions_by_key, fn_name); + if (p) + { + plugin = pool_elt_at_index (sm->policy_plugin_functions, p[0]); + } + /* Else create a new one and set hash key */ + else + { + pool_get (sm->policy_plugin_functions, plugin); + hash_set_mem (sm->policy_plugin_functions_by_key, fn_name, + plugin - sm->policy_plugin_functions); + } + + clib_memset (plugin, 0, sizeof (*plugin)); + + plugin->sr_policy_function_number = (plugin - sm->policy_plugin_functions); + plugin->sr_policy_function_number += SR_BEHAVIOR_LAST; + plugin->prefix_length = prefix_length; + plugin->ls_format = ls_format; + plugin->ls_unformat = ls_unformat; + plugin->creation = creation_fn; + plugin->removal = removal_fn; + clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t)); + plugin->function_name = format (0, "%s%c", fn_name, 0); + plugin->keyword_str = format (0, "%s%c", keyword_str, 0); + plugin->def_str = format (0, "%s%c", def_str, 0); + plugin->params_str = format (0, "%s%c", params_str, 0); + + return plugin->sr_policy_function_number; +} + +/** + * @brief CLI function to 'show' all available SR LocalSID behaviors + */ +static clib_error_t * +show_sr_policy_behaviors_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + ip6_sr_main_t *sm = &sr_main; + sr_policy_fn_registration_t *plugin; + sr_policy_fn_registration_t **plugins_vec = 0; + int i; + + vlib_cli_output (vm, "SR Policy behaviors:\n-----------------------\n\n"); + + /* *INDENT-OFF* */ + pool_foreach (plugin, sm->policy_plugin_functions, + ({ vec_add1 (plugins_vec, plugin); })); + /* *INDENT-ON* */ + + vlib_cli_output (vm, "Plugin behaviors:\n"); + for (i = 0; i < vec_len (plugins_vec); i++) + { + plugin = plugins_vec[i]; + vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str, + plugin->def_str); + vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str); + } + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_sr_policy_behaviors_command, static) = { + .path = "show sr policy behaviors", + .short_help = "show sr policy behaviors", + .function = show_sr_policy_behaviors_command_fn, +}; +/* *INDENT-ON* */ + /*************************** SR Segment Lists DPOs ****************************/ static u8 * format_sr_segment_list_dpo (u8 * s, va_list * args) |