+VLIB_CLI_COMMAND(show_sr_mpls_policies_command, static)=
+{
+ .path = "show sr mpls policies",
+ .short_help = "show sr mpls policies",
+ .function = show_sr_mpls_policies_command_fn,
+};
+/* *INDENT-ON* */
+
+/**
+ * @brief Update the Endpoint,Color tuple of an SR policy
+ *
+ * @param bsid is the bindingSID of the SR Policy
+ * @param endpoint represents the IP46 of the endpoint
+ * @param color represents the color (u32)
+ *
+ * To reset to NULL use ~0 as parameters.
+ *
+ * @return 0 if correct, else error
+ */
+int
+sr_mpls_policy_assign_endpoint_color (mpls_label_t bsid,
+ ip46_address_t * endpoint,
+ u8 endpoint_type, u32 color)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_policy_t *sr_policy = 0;
+ uword *endpoint_table, *p, *old_value;
+
+ ip46_address_t any;
+ any.as_u64[0] = any.as_u64[1] = (u64) ~ 0;
+
+ if (!sm->sr_policies_index_hash)
+ sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
+
+ p = hash_get (sm->sr_policies_index_hash, bsid);
+ if (p)
+ sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
+ else
+ return -1;
+
+ /* If previous Endpoint, color existed, remove (NH,C) and (ANY,C) */
+ if (sr_policy->endpoint_type)
+ {
+ endpoint_table =
+ mhash_get (&sm->sr_policies_c2e2eclabel_hash, &sr_policy->color);
+ if (!endpoint_table)
+ return -2;
+ old_value =
+ mhash_get ((mhash_t *) endpoint_table, &sr_policy->endpoint);
+
+ /* CID 180995 This should never be NULL unless the two hash tables
+ * get out of sync */
+ ASSERT (old_value != NULL);
+
+ fib_prefix_t pfx = { 0 };
+ pfx.fp_proto = FIB_PROTOCOL_MPLS;
+ pfx.fp_len = 21;
+ pfx.fp_label = (u32) * old_value;
+
+ mpls_eos_bit_t eos;
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ pfx.fp_eos = eos;
+ fib_table_entry_path_remove (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ DPO_PROTO_MPLS,
+ NULL,
+ ~0, 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
+ }
+
+ old_value = mhash_get ((mhash_t *) endpoint_table, &any);
+ pfx.fp_label = (u32) * old_value;
+
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ pfx.fp_eos = eos;
+ fib_table_entry_path_remove (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ DPO_PROTO_MPLS,
+ NULL,
+ ~0, 0, 1, FIB_ROUTE_PATH_FLAG_NONE);
+ }
+
+ /* Release the lock on (NH, Color) and (ANY, Color) */
+ internal_label_unlock (sr_policy->endpoint, sr_policy->color);
+ internal_label_unlock (any, sr_policy->color);
+
+ /* Reset the values on the SR policy */
+ sr_policy->endpoint_type = 0;
+ sr_policy->endpoint.as_u64[0] = sr_policy->endpoint.as_u64[1] =
+ (u64) ~ 0;
+ sr_policy->color = (u32) ~ 0;
+ }
+
+ if (endpoint_type)
+ {
+ sr_policy->endpoint_type = endpoint_type;
+ sr_policy->endpoint.as_u64[0] = endpoint->as_u64[0];
+ sr_policy->endpoint.as_u64[1] = endpoint->as_u64[1];
+ sr_policy->color = color;
+
+ u32 label = find_or_create_internal_label (*endpoint, color);
+ internal_label_lock (*endpoint, sr_policy->color);
+
+ /* If FIB doesnt exist, create them */
+ if (sm->fib_table_EC == (u32) ~ 0)
+ {
+ sm->fib_table_EC = fib_table_create_and_lock (FIB_PROTOCOL_MPLS,
+ FIB_SOURCE_SR,
+ "SR-MPLS Traffic Engineering (NextHop,Color)");
+
+ fib_table_flush (sm->fib_table_EC, FIB_PROTOCOL_MPLS,
+ FIB_SOURCE_SPECIAL);
+ }
+
+ fib_prefix_t pfx = { 0 };
+ pfx.fp_proto = FIB_PROTOCOL_MPLS;
+ pfx.fp_len = 21;
+
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = 1,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_label_stack = 0
+ };
+ path.frp_local_label = sr_policy->bsid;
+
+ //Add the entry to ANY,Color
+ u32 any_label = find_or_create_internal_label (any, color);
+ internal_label_lock (any, sr_policy->color);
+
+ pfx.fp_eos = MPLS_EOS;
+ path.frp_eos = MPLS_EOS;
+
+ fib_route_path_t *paths = NULL;
+ vec_add1 (paths, path);
+
+ pfx.fp_label = label;
+ fib_table_entry_update (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+
+ pfx.fp_label = any_label;
+ fib_table_entry_update (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+
+ fib_mpls_label_t fml = {
+ .fml_value = MPLS_IETF_IMPLICIT_NULL_LABEL,
+ };
+
+ vec_add1 (path.frp_label_stack, fml);
+ pfx.fp_eos = MPLS_NON_EOS;
+ path.frp_eos = MPLS_NON_EOS;
+
+ paths = NULL;
+ vec_add1 (paths, path);
+
+ pfx.fp_label = label;
+ fib_table_entry_update (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+
+ pfx.fp_label = any_label;
+ fib_table_entry_update (sm->fib_table_EC,
+ &pfx,
+ FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+ }
+ return 0;
+}
+
+/**
+ * @brief CLI to modify the Endpoint,Color of an SR policy
+ */
+static clib_error_t *
+cli_sr_mpls_policy_ec_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ ip46_address_t endpoint;
+ u32 color = (u32) ~ 0;
+ mpls_label_t bsid;
+ u8 endpoint_type = 0;
+ char clear = 0, color_set = 0, bsid_set = 0;
+
+ memset (&endpoint, 0, sizeof (ip46_address_t));
+
+ int rv;
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (!endpoint_type
+ && unformat (input, "endpoint %U", unformat_ip6_address,
+ &endpoint.ip6))
+ endpoint_type = SR_STEER_IPV6;
+ else if (!endpoint_type
+ && unformat (input, "endpoint %U", unformat_ip4_address,
+ &endpoint.ip4))
+ endpoint_type = SR_STEER_IPV4;
+ else if (!color_set && unformat (input, "color %u", &color))
+ color_set = 1;
+ else if (!bsid_set
+ && unformat (input, "bsid %U", unformat_mpls_unicast_label,
+ &bsid))
+ bsid_set = 1;
+ else if (!clear && unformat (input, "clear"))
+ clear = 1;
+ else
+ break;
+ }
+
+ if (!bsid_set)
+ return clib_error_return (0, "No BSID specified");
+ if (!endpoint_type && !clear)
+ return clib_error_return (0, "No Endpoint specified");
+ if (!color_set && !clear)
+ return clib_error_return (0, "No Color set");
+
+ /* In case its a cleanup */
+ if (clear)
+ {
+ ip6_address_set_zero (&endpoint.ip6);
+ color = (u32) ~ 0;
+ }
+ rv =
+ sr_mpls_policy_assign_endpoint_color (bsid, &endpoint, endpoint_type,
+ color);
+
+ if (rv)
+ clib_error_return (0, "Error on Endpoint,Color");
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND(cli_sr_mpls_policy_ec_command, static)=
+{
+ .path = "sr mpls policy te",
+ .short_help = "sr mpls policy te bsid xxxxx endpoint x.x.x.x color 12341234",
+ .function = cli_sr_mpls_policy_ec_command_fn,