aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorScott Hutton <schutton@cisco.com>2024-05-03 07:40:42 -0700
committerDamjan Marion <dmarion@0xa5.net>2024-05-14 07:53:35 +0000
commit182d8b2ddba098f99029ed82e44960f3dfd18987 (patch)
tree5e6207f795dba2e2be150c1b3ca4c150106e5c3a /src/plugins
parent9850d301b0dc87cadf05999cf210f1f7b731a327 (diff)
sr: move srmpls to a plugin
Move sr_mpls folder under vnet to the plugin folder, update cmake configuration and header paths, and add plugin.c to register plugin. JIRA: VPP-2054 Type: improvement Change-Id: I1ad6f287f67eb0c35588c339bcd51218fadf5f8e Signed-off-by: Scott Hutton <schutton@cisco.com>
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/srmpls/CMakeLists.txt30
-rw-r--r--src/plugins/srmpls/FEATURE.yaml9
-rw-r--r--src/plugins/srmpls/dir.dox22
-rw-r--r--src/plugins/srmpls/plugin.c26
-rw-r--r--src/plugins/srmpls/sr_doc.rst215
-rw-r--r--src/plugins/srmpls/sr_mpls.api124
-rw-r--r--src/plugins/srmpls/sr_mpls.h177
-rw-r--r--src/plugins/srmpls/sr_mpls_api.c257
-rw-r--r--src/plugins/srmpls/sr_mpls_policy.c903
-rw-r--r--src/plugins/srmpls/sr_mpls_steering.c897
-rw-r--r--src/plugins/srmpls/sr_mpls_test.c174
11 files changed, 2834 insertions, 0 deletions
diff --git a/src/plugins/srmpls/CMakeLists.txt b/src/plugins/srmpls/CMakeLists.txt
new file mode 100644
index 00000000000..25905d31e1b
--- /dev/null
+++ b/src/plugins/srmpls/CMakeLists.txt
@@ -0,0 +1,30 @@
+# Copyright (c) 2024 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.
+
+add_vpp_plugin(srmpls
+ SOURCES
+ sr_mpls_policy.c
+ sr_mpls_steering.c
+ sr_mpls_api.c
+ plugin.c
+
+ INSTALL_HEADERS
+ sr_mpls.h
+
+ API_FILES
+ sr_mpls.api
+
+ # This might need to be VAT_AUTO_TEST? Not documented
+ API_TEST_SOURCES
+ sr_mpls_test.c
+)
diff --git a/src/plugins/srmpls/FEATURE.yaml b/src/plugins/srmpls/FEATURE.yaml
new file mode 100644
index 00000000000..c5b958224c7
--- /dev/null
+++ b/src/plugins/srmpls/FEATURE.yaml
@@ -0,0 +1,9 @@
+---
+name: Segment Routing for MPLS
+maintainer: Pablo Camarillo <pcamaril@cisco.com>
+features:
+ - SR Policy support
+ - Automated steering (SR steering based on NextHop/Color)
+description: "SR-MPLS"
+state: production
+properties: [API, CLI, MULTITHREAD]
diff --git a/src/plugins/srmpls/dir.dox b/src/plugins/srmpls/dir.dox
new file mode 100644
index 00000000000..76ec1d6a41b
--- /dev/null
+++ b/src/plugins/srmpls/dir.dox
@@ -0,0 +1,22 @@
+/*
+ *
+ * Copyright (c) 2013 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.
+ */
+/**
+ @dir
+ @brief Segment Routing MPLS code
+
+ An implementation of Segment Routing for the MPLS dataplane.
+
+*/ \ No newline at end of file
diff --git a/src/plugins/srmpls/plugin.c b/src/plugins/srmpls/plugin.c
new file mode 100644
index 00000000000..af87607764f
--- /dev/null
+++ b/src/plugins/srmpls/plugin.c
@@ -0,0 +1,26 @@
+/*
+ * plugin.c: srmpls
+ *
+ * Copyright (c) 2024 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 <vlib/vlib.h>
+#include <vnet/plugin/plugin.h>
+#include <vpp/app/version.h>
+
+// register a plugin
+VLIB_PLUGIN_REGISTER () = {
+ .version = VPP_BUILD_VER,
+ .description = "Segment Routing for MPLS plugin",
+};
diff --git a/src/plugins/srmpls/sr_doc.rst b/src/plugins/srmpls/sr_doc.rst
new file mode 100644
index 00000000000..ed847fa0d42
--- /dev/null
+++ b/src/plugins/srmpls/sr_doc.rst
@@ -0,0 +1,215 @@
+.. _srmpls_doc:
+
+SR-MPLS: Segment Routing for MPLS
+=================================
+
+This is a memo intended to contain documentation of the VPP SR-MPLS
+implementation. Everything that is not directly obvious should come
+here. For any feedback on content that should be explained please
+mailto:pcamaril@cisco.com
+
+Segment Routing
+---------------
+
+Segment routing is a network technology focused on addressing the
+limitations of existing IP and Multiprotocol Label Switching (MPLS)
+networks in terms of simplicity, scale, and ease of operation. It is a
+foundation for application engineered routing as it prepares the
+networks for new business models where applications can control the
+network behavior.
+
+Segment routing seeks the right balance between distributed intelligence
+and centralized optimization and programming. It was built for the
+software-defined networking (SDN) era.
+
+Segment routing enhances packet forwarding behavior by enabling a
+network to transport unicast packets through a specific forwarding path,
+different from the normal path that a packet usually takes (IGP shortest
+path or BGP best path). This capability benefits many use cases, and one
+can build those specific paths based on application requirements.
+
+Segment routing uses the source routing paradigm. A node, usually a
+router but also a switch, a trusted server, or a virtual forwarder
+running on a hypervisor, steers a packet through an ordered list of
+instructions, called segments. A segment can represent any instruction,
+topological or service-based. A segment can have a local semantic to a
+segment-routing node or global within a segment-routing network. Segment
+routing allows an operator to enforce a flow through any topological
+path and service chain while maintaining per-flow state only at the
+ingress node to the segment-routing network. Segment routing also
+supports equal-cost multipath (ECMP) by design.
+
+Segment routing can operate with either an MPLS or an IPv6 data plane.
+All the currently available MPLS services, such as Layer 3 VPN (L3VPN),
+L2VPN (Virtual Private Wire Service [VPWS], Virtual Private LAN Services
+[VPLS], Ethernet VPN [E-VPN], and Provider Backbone Bridging Ethernet
+VPN [PBB-EVPN]), can run on top of a segment-routing transport network.
+
+**The implementation of Segment Routing in VPP covers both the IPv6 data
+plane (SRv6) as well as the MPLS data plane (SR-MPLS). This page
+contains the SR-MPLS documentation.**
+
+Segment Routing terminology
+---------------------------
+
+- SegmentID (SID): is an MPLS label.
+- Segment List (SL) (SID List): is the sequence of SIDs that the packet
+ will traverse.
+- SR Policy: is a set of candidate paths (SID list+weight). An SR
+ policy is uniquely identified by its Binding SID and associated with
+ a weighted set of Segment Lists. In case several SID lists are
+ defined, traffic steered into the policy is unevenly load-balanced
+ among them according to their respective weights.
+- BindingSID: a BindingSID is a SID (only one) associated one-one with
+ an SR Policy. If a packet arrives with MPLS label corresponding to a
+ BindingSID, then the SR policy will be applied to such packet.
+ (BindingSID is popped first.)
+
+SR-MPLS features in VPP
+-----------------------
+
+The SR-MPLS implementation is focused on the SR policies, as well on its
+steering. Others SR-MPLS features, such as for example AdjSIDs, can be
+achieved using the regular VPP MPLS implementation.
+
+The Segment Routing Policy
+(*draft-filsfils-spring-segment-routing-policy*) defines SR Policies.
+
+Creating a SR Policy
+--------------------
+
+An SR Policy is defined by a Binding SID and a weighted set of Segment
+Lists.
+
+A new SR policy is created with a first SID list using:
+
+::
+
+ sr mpls policy add bsid 40001 next 16001 next 16002 next 16003 (weight 5)
+
+- The weight parameter is only used if more than one SID list is
+ associated with the policy.
+
+An SR policy is deleted with:
+
+::
+
+ sr mpls policy del bsid 40001
+
+The existing SR policies are listed with:
+
+::
+
+ show sr mpls policies
+
+Adding/Removing SID Lists from an SR policy
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+An additional SID list is associated with an existing SR policy with:
+
+::
+
+ sr mpls policy mod bsid 40001 add sl next 16001 next 16002 next 16003 (weight 3)
+
+Conversely, a SID list can be removed from an SR policy with:
+
+::
+
+ sr mpls policy mod bsid 4001 del sl index 1
+
+Note that this CLI cannot be used to remove the last SID list of a
+policy. Instead the SR policy delete CLI must be used.
+
+The weight of a SID list can also be modified with:
+
+::
+
+ sr mpls policy mod bsid 40001 mod sl index 1 weight 4
+
+SR Policies: Spray policies
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Spray policies are a specific type of SR policies where the packet is
+replicated on all the SID lists, rather than load-balanced among them.
+
+SID list weights are ignored with this type of policies.
+
+A Spray policy is instantiated by appending the keyword **spray** to a
+regular SR-MPLS policy command, as in:
+
+::
+
+ sr mpls policy add bsid 40002 next 16001 next 16002 next 16003 spray
+
+Spray policies are used for removing multicast state from a network core
+domain, and instead send a linear unicast copy to every access node. The
+last SID in each list accesses the multicast tree within the access
+node.
+
+Steering packets into a SR Policy
+---------------------------------
+
+Segment Routing supports three methods of steering traffic into an SR
+policy.
+
+Local steering
+~~~~~~~~~~~~~~
+
+In this variant incoming packets match a routing policy which directs
+them on a local SR policy.
+
+In order to achieve this behavior the user needs to create an ‘sr
+steering policy via sr policy bsid’.
+
+::
+
+ sr mpls steer l3 2001::/64 via sr policy bsid 40001
+ sr mpls steer l3 2001::/64 via sr policy bsid 40001 fib-table 3
+ sr mpls steer l3 10.0.0.0/16 via sr policy bsid 40001
+ sr mpls steer l3 10.0.0.0/16 via sr policy bsid 40001 vpn-label 500
+
+Remote steering
+~~~~~~~~~~~~~~~
+
+In this variant incoming packets have an active SID matching a local
+BSID at the head-end.
+
+In order to achieve this behavior the packets should simply arrive with
+an active SID equal to the Binding SID of a locally instantiated SR
+policy.
+
+Automated steering
+~~~~~~~~~~~~~~~~~~
+
+In this variant incoming packets match a BGP/Service route which
+recurses on the BSID of a local policy.
+
+In order to achieve this behavior the user first needs to color the SR
+policies. He can do so by using the CLI:
+
+::
+
+ sr mpls policy te bsid xxxxx endpoint x.x.x.x color 12341234
+
+Notice that an SR policy can have a single endpoint and a single color.
+Notice that the *endpoint* value is an IP46 address and the color a u32.
+
+Then, for any BGP/Service route the user has to use the API to steer
+prefixes:
+
+::
+
+ sr steer l3 2001::/64 via next-hop 2001::1 color 1234 co 2
+ sr steer l3 2001::/64 via next-hop 2001::1 color 1234 co 2 vpn-label 500
+
+Notice that *co* refers to the CO-bits (values [0|1|2|3]).
+
+Notice also that a given prefix might be steered over several colors
+(same next-hop and same co-bit value). In order to add new colors just
+execute the API several times (or with the del parameter to delete the
+color).
+
+This variant is meant to be used in conjunction with a control plane
+agent that uses the underlying binary API bindings of
+*sr_mpls_steering_policy_add*/*sr_mpls_steering_policy_del* for any BGP
+service route received.
diff --git a/src/plugins/srmpls/sr_mpls.api b/src/plugins/srmpls/sr_mpls.api
new file mode 100644
index 00000000000..742f135d493
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls.api
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2015-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.
+ */
+
+option version = "3.0.0";
+
+import "vnet/interface_types.api";
+import "vnet/ip/ip_types.api";
+import "vnet/srv6/sr_types.api";
+
+/** \brief MPLS SR policy add
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param bsid - is the bindingSID of the SR Policy. MPLS label (20bit)
+ @param weight - is the weight of the sid list. optional.
+ @param is_spray - is the type of the SR policy. (0.Default // 1.Spray)
+ @param segments - vector of labels (20bit) composing the segment list
+*/
+autoreply define sr_mpls_policy_add
+{
+ u32 client_index;
+ u32 context;
+ u32 bsid;
+ u32 weight;
+ bool is_spray;
+ u8 n_segments;
+ u32 segments[n_segments];
+};
+
+/** \brief MPLS SR policy modification
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param bsid is the bindingSID of the SR Policy. MPLS label (20bit)
+ @param sr_policy_index is the index of the SR policy
+ @param fib_table is the VRF where to install the FIB entry for the BSID
+ @param operation is the operation to perform (among the top ones)
+ @param segments is a vector of MPLS labels composing the segment list
+ @param sl_index is the index of the Segment List to modify/delete
+ @param weight is the weight of the sid list. optional.
+ @param is_encap Mode. Encapsulation or SRH insertion.
+*/
+autoreply define sr_mpls_policy_mod
+{
+ u32 client_index;
+ u32 context;
+ u32 bsid;
+ vl_api_sr_policy_op_t operation;
+ u32 sl_index;
+ u32 weight;
+ u8 n_segments;
+ u32 segments[n_segments];
+};
+
+/** \brief MPLS SR policy deletion
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param bsid is the bindingSID of the SR Policy. MPLS label (20bit)
+*/
+autoreply define sr_mpls_policy_del
+{
+ u32 client_index;
+ u32 context;
+ u32 bsid;
+};
+
+/** \brief MPLS SR steering add/del
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param is_del
+ @param bsid - is the bindingSID of the SR Policy (~0 is no bsid)
+ @param table_id - is the VRF where to install the FIB entry for the BSID
+ @param prefix - is the IPv4/v6 address for L3 traffic type.
+ @param mask_width - is the mask for L3 traffic type
+ @param next_hop - describes the next_hop (in case no BSID)
+ @param color - describes the color
+ @param co_bits - are the CO_bits of the steering policy
+ @param vpn_label - is an additonal last VPN label. (~0 is no label)
+*/
+autoreply define sr_mpls_steering_add_del
+{
+ u32 client_index;
+ u32 context;
+ bool is_del[default = false];
+ u32 bsid;
+ u32 table_id;
+ vl_api_prefix_t prefix;
+ u32 mask_width;
+ vl_api_address_t next_hop;
+ u32 color;
+ u8 co_bits;
+ u32 vpn_label;
+};
+
+/** \brief MPLS SR steering add/del
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param bsid is the bindingSID of the SR Policy
+ @param endpoint is the endpoint of the SR policy
+ @param color is the color of the sr policy
+*/
+autoreply define sr_mpls_policy_assign_endpoint_color
+{
+ u32 client_index;
+ u32 context;
+ u32 bsid;
+ vl_api_address_t endpoint;
+ u32 color;
+};
+
+/*
+ * fd.io coding-style-patch-verification: ON Local Variables: eval:
+ * (c-set-style "gnu") End:
+ */
diff --git a/src/plugins/srmpls/sr_mpls.h b/src/plugins/srmpls/sr_mpls.h
new file mode 100644
index 00000000000..a8f9494428f
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls.h
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2015 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.
+ */
+
+/**
+ * @file
+ * @brief Segment Routing MPLS data structures definitions
+ *
+ */
+
+#ifndef included_vnet_srmpls_h
+#define included_vnet_srmpls_h
+
+#include <vnet/vnet.h>
+#include <vnet/mpls/packet.h>
+#include <vnet/fib/mpls_fib.h>
+#include <vnet/ip/ip.h>
+#include <vnet/ip/lookup.h>
+#include <vnet/dpo/dpo.h>
+#include <vnet/dpo/replicate_dpo.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+/* SR policy types */
+#define SR_POLICY_TYPE_DEFAULT 0
+#define SR_POLICY_TYPE_SPRAY 1
+
+#define SR_SEGMENT_LIST_WEIGHT_DEFAULT 1
+
+#define SR_STEER_IPV4 4
+#define SR_STEER_IPV6 6
+
+#define SR_TE_CO_BITS_00 0
+#define SR_TE_CO_BITS_01 1
+#define SR_TE_CO_BITS_10 2
+#define SR_TE_CO_BITS_11 3
+
+/**
+ * @brief SR Segment List (SID list)
+ */
+typedef struct
+{
+ /* SIDs (key) */
+ mpls_label_t *segments;
+
+ /* SID list weight (wECMP / UCMP) */
+ u32 weight;
+
+} mpls_sr_sl_t;
+
+typedef struct
+{
+ u32 *segments_lists; /**< Pool of SID lists indexes */
+
+ mpls_label_t bsid; /**< BindingSID (key) */
+
+ u8 type; /**< Type (default is 0) */
+ /* SR Policy specific DPO */
+ /* IF Type = DEFAULT Then Load-Balancer DPO among SID lists */
+ /* IF Type = SPRAY then Spray DPO with all SID lists */
+
+ ip46_address_t endpoint; /**< Optional NH for SR TE */
+ u8 endpoint_type;
+ u32 color; /**< Optional color for SR TE */
+} mpls_sr_policy_t;
+
+/**
+ * @brief Steering db key
+ *
+ * L3 is IPv4/IPv6 + mask
+ */
+typedef struct
+{
+ ip46_address_t prefix; /**< IP address of the prefix */
+ u32 mask_width; /**< Mask width of the prefix */
+ u32 fib_table; /**< VRF of the prefix */
+ u8 traffic_type; /**< Traffic type (IPv4, IPv6, L2) */
+ u8 padding[3];
+} sr_mpls_steering_key_t;
+
+typedef struct
+{
+ sr_mpls_steering_key_t classify; /**< Traffic classification */
+ mpls_label_t bsid; /**< SR Policy index */
+ ip46_address_t next_hop; /**< SR TE NH */
+ char nh_type;
+ u32 *color; /**< Vector of SR TE colors */
+ char co_bits; /**< Color-Only bits */
+ mpls_label_t vpn_label;
+} mpls_sr_steering_policy_t;
+
+/**
+ * @brief Segment Routing main datastructure
+ */
+typedef struct
+{
+ /* SR SID lists */
+ mpls_sr_sl_t *sid_lists;
+
+ /* SR MPLS policies */
+ mpls_sr_policy_t *sr_policies;
+
+ /* Hash table mapping BindingSID to SR MPLS policy */
+ uword *sr_policies_index_hash;
+
+ /* Pool of SR steer policies instances */
+ mpls_sr_steering_policy_t *steer_policies;
+
+ /* MHash table mapping steering rules to SR steer instance */
+ mhash_t sr_steer_policies_hash;
+
+ /** SR TE **/
+ /* Hash table mapping (Color->Endpoint->BSID) for SR policies */
+ mhash_t sr_policies_c2e2eclabel_hash;
+ /* SR TE (internal) fib table (Endpoint, Color) */
+ u32 fib_table_EC;
+ /* Pool of (Endpoint, Color) hidden labels */
+ u32 *ec_labels;
+
+ /* convenience */
+ vlib_main_t *vlib_main;
+ vnet_main_t *vnet_main;
+} mpls_sr_main_t;
+
+extern mpls_sr_main_t sr_mpls_main;
+
+extern int
+sr_mpls_policy_add (mpls_label_t bsid, mpls_label_t * segments,
+ u8 behavior, u32 weight);
+
+extern int
+sr_mpls_policy_mod (mpls_label_t bsid, u8 operation,
+ mpls_label_t * segments, u32 sl_index, u32 weight);
+
+extern int sr_mpls_policy_del (mpls_label_t bsid);
+
+extern int
+sr_mpls_policy_assign_endpoint_color (mpls_label_t bsid,
+ ip46_address_t * endpoint,
+ u8 endpoint_type, u32 color);
+
+extern int
+sr_mpls_steering_policy_add (mpls_label_t bsid, u32 table_id,
+ ip46_address_t * prefix, u32 mask_width,
+ u8 traffic_type, ip46_address_t * next_hop,
+ u8 nh_type, u32 color, char co_bits,
+ mpls_label_t vpn_label);
+
+extern int
+sr_mpls_steering_policy_del (ip46_address_t * prefix,
+ u32 mask_width, u8 traffic_type, u32 table_id,
+ u32 color);
+
+extern u32 find_or_create_internal_label (ip46_address_t endpoint, u32 color);
+
+extern void internal_label_lock (ip46_address_t endpoint, u32 color);
+
+extern void internal_label_unlock (ip46_address_t endpoint, u32 color);
+
+#endif /* included_vnet_sr_mpls_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables: eval: (c-set-style "gnu") End:
+ */
diff --git a/src/plugins/srmpls/sr_mpls_api.c b/src/plugins/srmpls/sr_mpls_api.c
new file mode 100644
index 00000000000..3e89017dbc1
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls_api.c
@@ -0,0 +1,257 @@
+/*
+ * ------------------------------------------------------------------
+ * sr_api.c - ipv6 segment routing api
+ *
+ * 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/vnet.h>
+#include "sr_mpls.h"
+#include <vlibmemory/api.h>
+
+#include <vnet/interface.h>
+#include <vnet/api_errno.h>
+#include <vnet/feature/feature.h>
+#include <vnet/ip/ip_types_api.h>
+
+#include <vnet/format_fns.h>
+#include <plugins/srmpls/sr_mpls.api_enum.h>
+#include <plugins/srmpls/sr_mpls.api_types.h>
+
+#define vl_api_version(n, v) static u32 api_version = v;
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_api_version
+
+#define vl_endianfun
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_endianfun
+
+#define vl_calcsizefun
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_calcsizefun
+
+#define vl_printfun
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_printfun
+
+#define vl_msg_name_crc_list
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_msg_name_crc_list
+
+#define REPLY_MSG_ID_BASE msg_id_base
+#include <vlibapi/api_helper_macros.h>
+
+#define foreach_vpe_api_msg \
+_(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \
+_(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
+_(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
+
+static u16 msg_id_base;
+
+static void
+vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
+{
+ vl_api_sr_mpls_policy_add_reply_t *rmp;
+
+ mpls_label_t *segments = 0, *seg;
+ mpls_label_t this_address = 0;
+
+ int i;
+ for (i = 0; i < mp->n_segments; i++)
+ {
+ vec_add2 (segments, seg, 1);
+ this_address = ntohl (mp->segments[i]);
+ clib_memcpy (seg, &this_address, sizeof (this_address));
+ }
+
+ int rv = 0;
+ rv = sr_mpls_policy_add (ntohl (mp->bsid),
+ segments, mp->is_spray, ntohl (mp->weight));
+ vec_free (segments);
+
+ REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
+}
+
+static void
+vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
+{
+ vl_api_sr_mpls_policy_mod_reply_t *rmp;
+
+ mpls_label_t *segments = 0, *seg;
+ mpls_label_t this_address = 0;
+
+ int i;
+ for (i = 0; i < mp->n_segments; i++)
+ {
+ vec_add2 (segments, seg, 1);
+ this_address = ntohl (mp->segments[i]);
+ clib_memcpy (seg, &this_address, sizeof (this_address));
+ }
+
+ int rv = 0;
+ rv = sr_mpls_policy_mod (ntohl (mp->bsid),
+ ntohl (mp->operation), segments,
+ ntohl (mp->sl_index), ntohl (mp->weight));
+ vec_free (segments);
+
+ REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
+}
+
+static void
+vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
+{
+ vl_api_sr_mpls_policy_del_reply_t *rmp;
+ int rv = 0;
+ rv = sr_mpls_policy_del (ntohl (mp->bsid));
+
+ REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
+}
+
+static void vl_api_sr_mpls_steering_add_del_t_handler
+ (vl_api_sr_mpls_steering_add_del_t * mp)
+{
+ vl_api_sr_mpls_steering_add_del_reply_t *rmp;
+ fib_prefix_t prefix;
+ ip46_address_t next_hop;
+ clib_memset (&prefix, 0, sizeof (ip46_address_t));
+
+ ip_prefix_decode (&mp->prefix, &prefix);
+ ip_address_decode (&mp->next_hop, &next_hop);
+
+ int rv = 0;
+ if (mp->is_del)
+ rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
+ prefix.fp_len,
+ ip46_address_is_ip4 (&prefix.fp_addr) ?
+ SR_STEER_IPV4 : SR_STEER_IPV6,
+ ntohl (mp->table_id),
+ ntohl (mp->color));
+ else
+ rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
+ ntohl (mp->table_id),
+ &prefix.fp_addr,
+ prefix.fp_len,
+ ip46_address_is_ip4 (&prefix.fp_addr) ?
+ SR_STEER_IPV4 : SR_STEER_IPV6,
+ &next_hop,
+ ip46_address_is_ip4 (&next_hop) ?
+ SR_STEER_IPV4 : SR_STEER_IPV6,
+ ntohl (mp->color), mp->co_bits,
+ ntohl (mp->vpn_label));
+
+ REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
+}
+
+static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
+ (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
+{
+ vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
+ int rv = 0;
+
+ ip46_address_t endpoint;
+ clib_memset (&endpoint, 0, sizeof (ip46_address_t));
+ ip_address_decode (&mp->endpoint, &endpoint);
+
+ rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
+ &endpoint,
+ ip46_address_is_ip4 (&endpoint) ?
+ SR_STEER_IPV4 : SR_STEER_IPV6,
+ ntohl (mp->color));
+
+ REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
+}
+
+static void
+setup_message_id_table (api_main_t * am)
+{
+#define _(id, n, crc) \
+ vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE);
+ foreach_vl_msg_name_crc_sr_mpls;
+#undef _
+}
+
+static clib_error_t *
+sr_mpls_api_hookup (vlib_main_t * vm)
+{
+ api_main_t *am = vlibapi_get_main ();
+
+ u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0);
+ REPLY_MSG_ID_BASE =
+ vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST);
+ vec_free (name);
+
+#define _(N, n) \
+ vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
+ .id = REPLY_MSG_ID_BASE + VL_API_##N, \
+ .name = #n, \
+ .handler = vl_api_##n##_t_handler, \
+ .endian = vl_api_##n##_t_endian, \
+ .format_fn = vl_api_##n##_t_format, \
+ .size = sizeof (vl_api_##n##_t), \
+ .traced = 1, \
+ .tojson = vl_api_##n##_t_tojson, \
+ .fromjson = vl_api_##n##_t_fromjson, \
+ .calc_size = vl_api_##n##_t_calc_size, \
+ });
+ foreach_vpe_api_msg;
+#undef _
+
+ /*
+ * Manually register the sr policy add msg, so we trace enough bytes
+ * to capture a typical segment list
+ */
+ vl_msg_api_config (&(vl_msg_api_msg_config_t){
+ .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD,
+ .name = "sr_mpls_policy_add",
+ .handler = vl_api_sr_mpls_policy_add_t_handler,
+ .endian = vl_api_sr_mpls_policy_add_t_endian,
+ .format_fn = vl_api_sr_mpls_policy_add_t_format,
+ .size = 256,
+ .traced = 1,
+ .tojson = vl_api_sr_mpls_policy_add_t_tojson,
+ .fromjson = vl_api_sr_mpls_policy_add_t_fromjson,
+ .calc_size = vl_api_sr_mpls_policy_add_t_calc_size,
+ });
+ /*
+ * Manually register the sr policy mod msg, so we trace enough bytes
+ * to capture a typical segment list
+ */
+ vl_msg_api_config (&(vl_msg_api_msg_config_t){
+ .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD,
+ .name = "sr_mpls_policy_mod",
+ .handler = vl_api_sr_mpls_policy_mod_t_handler,
+ .endian = vl_api_sr_mpls_policy_mod_t_endian,
+ .format_fn = vl_api_sr_mpls_policy_mod_t_format,
+ .size = 256,
+ .traced = 1,
+ .tojson = vl_api_sr_mpls_policy_mod_t_tojson,
+ .fromjson = vl_api_sr_mpls_policy_mod_t_fromjson,
+ .calc_size = vl_api_sr_mpls_policy_mod_t_calc_size,
+ });
+
+ /*
+ * Set up the (msg_name, crc, message-id) table
+ */
+ setup_message_id_table (am);
+
+ return 0;
+}
+
+VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables: eval: (c-set-style "gnu") End:
+ */
diff --git a/src/plugins/srmpls/sr_mpls_policy.c b/src/plugins/srmpls/sr_mpls_policy.c
new file mode 100644
index 00000000000..af24acd8cf6
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls_policy.c
@@ -0,0 +1,903 @@
+/*
+ * sr_mpls_policy.c: SR-MPLS policies
+ *
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief SR MPLS policy creation and application
+ *
+ * Create an SR policy.
+ * An SR policy can be either of 'default' type or 'spray' type
+ * An SR policy has attached a list of SID lists.
+ * In case the SR policy is a default one it will load balance among them.
+ * An SR policy has associated a BindingSID.
+ * In case any packet arrives with MPLS_label == BindingSID then the SR policy
+ * associated to such bindingSID will be applied to such packet.
+ * Also, a BSID can be associated with a (Next-Hop, Color)
+ *
+ */
+
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include "sr_mpls.h"
+#include <vnet/fib/mpls_fib.h>
+#include <vnet/dpo/dpo.h>
+#include <vnet/ip/ip.h>
+
+#include <vppinfra/error.h>
+#include <vppinfra/elog.h>
+
+mpls_sr_main_t sr_mpls_main;
+
+/*************************** SR LB helper functions **************************/
+/**
+ * @brief Creates a Segment List and adds it to an SR policy
+ *
+ * Creates a Segment List and adds it to the SR policy. Notice that the SL are
+ * not necessarily unique. Hence there might be two Segment List within the
+ * same SR Policy with exactly the same segments and same weight.
+ *
+ * @param sr_policy is the SR policy where the SL will be added
+ * @param sl is a vector of IPv6 addresses composing the Segment List
+ * @param weight is the weight of the SegmentList (for load-balancing purposes)
+ * @param is_encap represents the mode (SRH insertion vs Encapsulation)
+ *
+ * @return pointer to the just created segment list
+ */
+static inline mpls_sr_sl_t *
+create_sl (mpls_sr_policy_t * sr_policy, mpls_label_t * sl, u32 weight)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_sl_t *segment_list;
+ u32 ii;
+
+ pool_get (sm->sid_lists, segment_list);
+ clib_memset (segment_list, 0, sizeof (*segment_list));
+
+ vec_add1 (sr_policy->segments_lists, segment_list - sm->sid_lists);
+
+ /* Fill in segment list */
+ segment_list->weight =
+ (weight != (u32) ~ 0 ? weight : SR_SEGMENT_LIST_WEIGHT_DEFAULT);
+ segment_list->segments = vec_dup (sl);
+
+ mpls_eos_bit_t eos;
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = segment_list->weight,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_label_stack = NULL,
+ .frp_local_label = sl[0],
+ };
+
+ if (vec_len (sl) > 1)
+ {
+ vec_validate (path.frp_label_stack, vec_len (sl) - 2);
+ for (ii = 1; ii < vec_len (sl); ii++)
+ {
+ path.frp_label_stack[ii - 1].fml_value = sl[ii];
+ }
+ }
+ else
+ {
+ /*
+ * add an impliciet NULL label to allow non-eos recursion
+ */
+ fib_mpls_label_t lbl = {
+ .fml_value = MPLS_IETF_IMPLICIT_NULL_LABEL,
+ };
+ vec_add1 (path.frp_label_stack, lbl);
+ }
+
+ fib_route_path_t *paths = NULL;
+ vec_add1 (paths, path);
+
+ fib_prefix_t pfx = {
+ .fp_len = 21,
+ .fp_proto = FIB_PROTOCOL_MPLS,
+ .fp_label = sr_policy->bsid,
+ .fp_eos = eos,
+ .fp_payload_proto = DPO_PROTO_MPLS,
+ };
+
+ fib_table_entry_path_add2 (0,
+ &pfx,
+ FIB_SOURCE_SR,
+ (sr_policy->type == SR_POLICY_TYPE_DEFAULT ?
+ FIB_ENTRY_FLAG_NONE :
+ FIB_ENTRY_FLAG_MULTICAST), paths);
+ vec_free (paths);
+ }
+
+ return segment_list;
+}
+
+/******************************* SR rewrite API *******************************/
+/*
+ * Three functions for handling sr policies: -> sr_mpls_policy_add ->
+ * sr_mpls_policy_del -> sr_mpls_policy_mod All of them are API. CLI function
+ * on sr_policy_command_fn
+ */
+
+/**
+ * @brief Create a new SR policy
+ *
+ * @param bsid is the bindingSID of the SR Policy
+ * @param segments is a vector of MPLS labels composing the segment list
+ * @param behavior is the behavior of the SR policy. (default//spray)
+ * @param fib_table is the VRF where to install the FIB entry for the BSID
+ * @param weight is the weight of this specific SID list
+ *
+ * @return 0 if correct, else error
+ */
+int
+sr_mpls_policy_add (mpls_label_t bsid, mpls_label_t * segments,
+ u8 behavior, u32 weight)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_policy_t *sr_policy = 0;
+ uword *p;
+
+ if (!sm->sr_policies_index_hash)
+ sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
+
+ /* MPLS SR policies cannot be created unless the MPLS table is present */
+ if (~0 == fib_table_find (FIB_PROTOCOL_MPLS, MPLS_FIB_DEFAULT_TABLE_ID))
+ return (VNET_API_ERROR_NO_SUCH_TABLE);
+
+ /* Search for existing keys (BSID) */
+ p = hash_get (sm->sr_policies_index_hash, bsid);
+ if (p)
+ {
+ /* Add SR policy that already exists; complain */
+ return -12;
+ }
+ /* Add an SR policy object */
+ pool_get (sm->sr_policies, sr_policy);
+ clib_memset (sr_policy, 0, sizeof (*sr_policy));
+
+ /* the first policy needs to lock the MPLS table so it doesn't
+ * disappear with policies in it */
+ if (1 == pool_elts (sm->sr_policies))
+ fib_table_find_or_create_and_lock (FIB_PROTOCOL_MPLS,
+ MPLS_FIB_DEFAULT_TABLE_ID,
+ FIB_SOURCE_SR);
+ sr_policy->bsid = bsid;
+ sr_policy->type = behavior;
+ sr_policy->endpoint_type = 0;
+ ip6_address_set_zero (&sr_policy->endpoint.ip6);
+ sr_policy->color = (u32) ~ 0;
+
+ /* Copy the key */
+ hash_set (sm->sr_policies_index_hash, bsid, sr_policy - sm->sr_policies);
+
+ /* Create a segment list and add the index to the SR policy */
+ create_sl (sr_policy, segments, weight);
+
+ return 0;
+}
+
+/**
+ * @brief Delete a SR policy
+ *
+ * @param bsid is the bindingSID of the SR Policy
+ * @param index is the index of the SR policy
+ *
+ * @return 0 if correct, else error
+ */
+int
+sr_mpls_policy_del (mpls_label_t bsid)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_policy_t *sr_policy = 0;
+ mpls_sr_sl_t *segment_list;
+ mpls_eos_bit_t eos;
+ u32 *sl_index;
+ uword *p;
+
+ 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;
+
+ /* Clean SID Lists */
+ vec_foreach (sl_index, sr_policy->segments_lists)
+ {
+ segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
+
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = segment_list->weight,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_local_label = segment_list->segments[0],
+ };
+
+ vec_add (path.frp_label_stack, segment_list + 1,
+ vec_len (segment_list) - 1);
+
+ fib_route_path_t *paths = NULL;
+ vec_add1 (paths, path);
+
+ /* remove each of the MPLS routes */
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ fib_prefix_t pfx = {
+ .fp_len = 21,
+ .fp_proto = FIB_PROTOCOL_MPLS,
+ .fp_label = sr_policy->bsid,
+ .fp_eos = eos,
+ .fp_payload_proto = DPO_PROTO_MPLS,
+ };
+
+ fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
+ }
+ vec_free (paths);
+ vec_free (segment_list->segments);
+ pool_put_index (sm->sid_lists, *sl_index);
+ }
+
+ /* If there is still traces of TE, make sure locks are released */
+ if (sr_policy->endpoint_type != 0 && sr_policy->color != (u32) ~ 0)
+ {
+ sr_mpls_policy_assign_endpoint_color (bsid, NULL, 0, (u32) ~ 0);
+ }
+
+ /* Remove SR policy entry */
+ hash_unset (sm->sr_policies_index_hash, sr_policy->bsid);
+ pool_put (sm->sr_policies, sr_policy);
+
+ if (0 == pool_elts (sm->sr_policies))
+ fib_table_unlock (MPLS_FIB_DEFAULT_TABLE_ID,
+ FIB_PROTOCOL_MPLS, FIB_SOURCE_SR);
+
+ return 0;
+}
+
+/**
+ * @brief Modify an existing SR policy
+ *
+ * The possible modifications are adding a new Segment List, modifying an
+ * existing Segment List (modify the weight only) and delete a given
+ * Segment List from the SR Policy.
+ *
+ * @param bsid is the bindingSID of the SR Policy
+ * @param fib_table is the VRF where to install the FIB entry for the BSID
+ * @param operation is the operation to perform (among the top ones)
+ * @param segments is a vector of IPv6 address composing the segment list
+ * @param sl_index is the index of the Segment List to modify/delete
+ * @param weight is the weight of the sid list. optional.
+ *
+ * @return 0 ok, >0 index of SL, <0 error
+ */
+int
+sr_mpls_policy_mod (mpls_label_t bsid, u8 operation,
+ mpls_label_t * segments, u32 sl_index, u32 weight)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_policy_t *sr_policy = 0;
+ mpls_sr_sl_t *segment_list;
+ u32 *sl_index_iterate;
+ uword *p;
+
+ 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 (operation == 1)
+ { /* Add SR List to an existing SR policy */
+ /* Create the new SL */
+ segment_list = create_sl (sr_policy, segments, weight);
+ return segment_list - sm->sid_lists;
+ }
+ else if (operation == 2)
+ { /* Delete SR List from an existing SR
+ * policy */
+ /* Check that currently there are more than one SID list */
+ if (vec_len (sr_policy->segments_lists) == 1)
+ return -21;
+
+ /*
+ * Check that the SR list does exist and is assigned to the
+ * sr policy
+ */
+ vec_foreach (sl_index_iterate, sr_policy->segments_lists)
+ if (*sl_index_iterate == sl_index)
+ break;
+
+ if (*sl_index_iterate != sl_index)
+ return -22;
+
+ /* Remove the lucky SR list that is being kicked out */
+ segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
+
+ mpls_eos_bit_t eos;
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = segment_list->weight,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_local_label = segment_list->segments[0],
+ };
+
+ vec_add (path.frp_label_stack, segment_list + 1,
+ vec_len (segment_list) - 1);
+
+ fib_route_path_t *paths = NULL;
+ vec_add1 (paths, path);
+
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ fib_prefix_t pfx = {
+ .fp_len = 21,
+ .fp_proto = FIB_PROTOCOL_MPLS,
+ .fp_label = sr_policy->bsid,
+ .fp_eos = eos,
+ .fp_payload_proto = DPO_PROTO_MPLS,
+ };
+
+ fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
+ }
+
+ vec_free (paths);
+ vec_free (segment_list->segments);
+ pool_put_index (sm->sid_lists, sl_index);
+ vec_del1 (sr_policy->segments_lists,
+ sl_index_iterate - sr_policy->segments_lists);
+ }
+ else if (operation == 3)
+ { /* Modify the weight of an existing
+ * SR List */
+ /* Find the corresponding SL */
+ vec_foreach (sl_index_iterate, sr_policy->segments_lists)
+ if (*sl_index_iterate == sl_index)
+ break;
+
+ if (*sl_index_iterate != sl_index)
+ return -32;
+
+ /* Change the weight */
+ segment_list = pool_elt_at_index (sm->sid_lists, sl_index);
+
+ /* Update LB */
+ mpls_eos_bit_t eos;
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = segment_list->weight,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_local_label = segment_list->segments[0],
+ };
+
+ vec_add (path.frp_label_stack, segment_list + 1,
+ vec_len (segment_list) - 1);
+
+ fib_route_path_t *paths = NULL;
+ vec_add1 (paths, path);
+
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ fib_prefix_t pfx = {
+ .fp_len = 21,
+ .fp_proto = FIB_PROTOCOL_MPLS,
+ .fp_label = sr_policy->bsid,
+ .fp_eos = eos,
+ .fp_payload_proto = DPO_PROTO_MPLS,
+ };
+
+ fib_table_entry_path_remove2 (0, &pfx, FIB_SOURCE_SR, paths);
+ }
+
+ segment_list->weight = weight;
+
+ path.frp_weight = segment_list->weight;
+
+ vec_free (paths);
+ paths = NULL;
+ vec_add1 (paths, path);
+
+ FOR_EACH_MPLS_EOS_BIT (eos)
+ {
+ fib_prefix_t pfx = {
+ .fp_len = 21,
+ .fp_proto = FIB_PROTOCOL_MPLS,
+ .fp_label = sr_policy->bsid,
+ .fp_eos = eos,
+ .fp_payload_proto = DPO_PROTO_MPLS,
+ };
+
+ fib_table_entry_path_add2 (0,
+ &pfx,
+ FIB_SOURCE_SR,
+ (sr_policy->type ==
+ SR_POLICY_TYPE_DEFAULT ?
+ FIB_ENTRY_FLAG_NONE :
+ FIB_ENTRY_FLAG_MULTICAST), paths);
+ }
+ }
+ return 0;
+}
+
+/**
+ * @brief CLI for 'sr mpls policies' command family
+ */
+static clib_error_t *
+sr_mpls_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ int rv = -1;
+ char is_del = 0, is_add = 0, is_mod = 0;
+ char policy_set = 0;
+ mpls_label_t bsid, next_label;
+ u32 sl_index = (u32) ~ 0;
+ u32 weight = (u32) ~ 0;
+ mpls_label_t *segments = 0;
+ u8 operation = 0;
+ u8 is_spray = 0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (!is_add && !is_mod && !is_del && unformat (input, "add"))
+ is_add = 1;
+ else if (!is_add && !is_mod && !is_del && unformat (input, "del"))
+ is_del = 1;
+ else if (!is_add && !is_mod && !is_del && unformat (input, "mod"))
+ is_mod = 1;
+ else if (!policy_set
+ && unformat (input, "bsid %U", unformat_mpls_unicast_label,
+ &bsid))
+ policy_set = 1;
+ else if (unformat (input, "weight %d", &weight));
+ else if (unformat
+ (input, "next %U", unformat_mpls_unicast_label, &next_label))
+ {
+ vec_add (segments, &next_label, 1);
+ }
+ else if (unformat (input, "add sl"))
+ operation = 1;
+ else if (unformat (input, "del sl index %d", &sl_index))
+ operation = 2;
+ else if (unformat (input, "mod sl index %d", &sl_index))
+ operation = 3;
+ else if (unformat (input, "spray"))
+ is_spray = 1;
+ else
+ break;
+ }
+
+ if (!is_add && !is_mod && !is_del)
+ return clib_error_return (0, "Incorrect CLI");
+
+ if (!policy_set)
+ return clib_error_return (0, "No SR policy BSID or index specified");
+
+ if (is_add)
+ {
+ if (vec_len (segments) == 0)
+ return clib_error_return (0, "No Segment List specified");
+
+ rv = sr_mpls_policy_add (bsid, segments,
+ (is_spray ? SR_POLICY_TYPE_SPRAY :
+ SR_POLICY_TYPE_DEFAULT), weight);
+ vec_free (segments);
+ }
+ else if (is_del)
+ rv = sr_mpls_policy_del (bsid);
+ else if (is_mod)
+ {
+ if (!operation)
+ return clib_error_return (0, "No SL modification specified");
+ if (operation != 1 && sl_index == (u32) ~ 0)
+ return clib_error_return (0, "No Segment List index specified");
+ if (operation == 1 && vec_len (segments) == 0)
+ 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_mpls_policy_mod (bsid, operation, segments, sl_index, weight);
+ vec_free (segments);
+ }
+ switch (rv)
+ {
+ case 0:
+ break;
+ case 1:
+ return 0;
+ case -12:
+ return clib_error_return (0,
+ "There is already a FIB entry for the BindingSID address.\n"
+ "The SR policy could not be created.");
+ case -21:
+ return clib_error_return (0,
+ "The selected SR policy only contains ONE segment list. "
+ "Please remove the SR policy instead");
+ case -22:
+ return clib_error_return (0,
+ "Could not delete the segment list. "
+ "It is not associated with that SR policy.");
+ case -23:
+ return clib_error_return (0,
+ "Could not delete the segment list. "
+ "It is not associated with that SR policy.");
+ case -32:
+ return clib_error_return (0,
+ "Could not modify the segment list. "
+ "The given SL is not associated with such SR policy.");
+ case VNET_API_ERROR_NO_SUCH_TABLE:
+ return clib_error_return (0, "the Default MPLS table is not present");
+ default:
+ return clib_error_return (0, "BUG: sr policy returns %d", rv);
+ }
+ return 0;
+}
+
+VLIB_CLI_COMMAND(sr_mpls_policy_command, static)=
+{
+ .path = "sr mpls policy",
+ .short_help = "sr mpls policy [add||del||mod] bsid 2999 "
+ "next 10 next 20 next 30 (weight 1) (spray)",
+ .long_help = "TBD.\n",
+ .function = sr_mpls_policy_command_fn,
+};
+
+/**
+ * @brief CLI to display onscreen all the SR MPLS policies
+ */
+static clib_error_t *
+show_sr_mpls_policies_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_sl_t *segment_list = 0;
+ mpls_sr_policy_t *sr_policy = 0;
+ mpls_sr_policy_t **vec_policies = 0;
+ mpls_label_t *label;
+ u32 *sl_index;
+ u8 *s;
+ int i = 0;
+
+ vlib_cli_output (vm, "SR MPLS policies:");
+
+ pool_foreach (sr_policy, sm->sr_policies) {
+ vec_add1(vec_policies, sr_policy);
+ }
+
+ vec_foreach_index (i, vec_policies)
+ {
+ sr_policy = vec_policies[i];
+ vlib_cli_output (vm, "[%u].-\tBSID: %U",
+ (u32) (sr_policy - sm->sr_policies),
+ format_mpls_unicast_label, sr_policy->bsid);
+ switch (sr_policy->endpoint_type)
+ {
+ case SR_STEER_IPV6:
+ vlib_cli_output (vm, "\tEndpoint: %U", format_ip6_address,
+ &sr_policy->endpoint.ip6);
+ vlib_cli_output (vm, "\tColor: %u", sr_policy->color);
+ break;
+ case SR_STEER_IPV4:
+ vlib_cli_output (vm, "\tEndpoint: %U", format_ip4_address,
+ &sr_policy->endpoint.ip4);
+ vlib_cli_output (vm, "\tColor: %u", sr_policy->color);
+ break;
+ default:
+ vlib_cli_output (vm, "\tTE disabled");
+ }
+ vlib_cli_output (vm, "\tType: %s",
+ (sr_policy->type ==
+ SR_POLICY_TYPE_DEFAULT ? "Default" : "Spray"));
+ vlib_cli_output (vm, "\tSegment Lists:");
+ vec_foreach (sl_index, sr_policy->segments_lists)
+ {
+ s = NULL;
+ segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
+ s = format (s, "\t[%u].- ", *sl_index);
+ s = format (s, "< ");
+ vec_foreach (label, segment_list->segments)
+ {
+ s = format (s, "%U, ", format_mpls_unicast_label, *label);
+ }
+ s = format (s, "\b\b > ");
+ vlib_cli_output (vm, " %s", s);
+ }
+ vlib_cli_output (vm, "-----------");
+ }
+ vec_free (vec_policies);
+ return 0;
+}
+
+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,
+};
+
+/**
+ * @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 */
+ ALWAYS_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;
+
+ clib_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;
+}
+
+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,
+};
+
+/********************* SR MPLS Policy initialization ***********************/
+/**
+ * @brief SR MPLS Policy initialization
+ */
+clib_error_t *
+sr_mpls_policy_rewrite_init (vlib_main_t * vm)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+
+ /* Init memory for sr policy keys (bsid <-> ip6_address_t) */
+ sm->sr_policies_index_hash = NULL;
+ sm->sr_policies_c2e2eclabel_hash.hash = NULL;
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (sr_mpls_policy_rewrite_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables: eval: (c-set-style "gnu") End:
+ */
diff --git a/src/plugins/srmpls/sr_mpls_steering.c b/src/plugins/srmpls/sr_mpls_steering.c
new file mode 100644
index 00000000000..24c8b0e2d9f
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls_steering.c
@@ -0,0 +1,897 @@
+/*
+ * sr_steering.c: ipv6 segment routing steering into SR policy
+ *
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief Packet steering into SR-MPLS Policies
+ *
+ * This file is in charge of handling the FIB appropiatly to steer packets
+ * through SR Policies as defined in 'sr_mpls_policy.c'. Notice that here
+ * we are only doing steering. SR policy application is done in
+ * sr_policy_rewrite.c
+ *
+ * Supports:
+ * - Steering of IPv6 traffic Destination Address based through BSID
+ * - Steering of IPv4 traffic Destination Address based through BSID
+ * - Steering of IPv4 and IPv6 traffic through N,C (SR CP)
+ */
+
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include "sr_mpls.h"
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/fib/mpls_fib.h>
+
+#include <vppinfra/error.h>
+#include <vppinfra/elog.h>
+
+#define SRMPLS_TE_OFFSET 50
+
+/**
+ * @brief function to sort the colors in descending order
+ */
+int
+sort_color_descent (const u32 * x, u32 * y)
+{
+ return *y - *x;
+}
+
+/********************* Internal (NH, C) labels *******************************/
+/**
+ * @brief find the corresponding label for (endpoint, color) and lock it
+ * endpoint might be NULL or ANY
+ * NULL = 0, ANY=~0
+ */
+u32
+find_or_create_internal_label (ip46_address_t endpoint, u32 color)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ uword *color_table, *result_label;
+
+ if (!sm->sr_policies_c2e2eclabel_hash.hash)
+ mhash_init (&sm->sr_policies_c2e2eclabel_hash, sizeof (mhash_t),
+ sizeof (u32));
+
+ color_table = mhash_get (&sm->sr_policies_c2e2eclabel_hash, &color);
+ if (!color_table)
+ {
+ mhash_t color_t;
+ clib_memset (&color_t, 0, sizeof (mhash_t));
+ mhash_init (&color_t, sizeof (u32), sizeof (ip46_address_t));
+ mhash_set_mem (&sm->sr_policies_c2e2eclabel_hash, &color,
+ (uword *) & color_t, NULL);
+ color_table = mhash_get (&sm->sr_policies_c2e2eclabel_hash, &color);
+ }
+
+ result_label = mhash_get ((mhash_t *) color_table, &endpoint);
+
+ if (result_label)
+ return (u32) * result_label;
+
+ /* Create and set a new internal label */
+ u32 *new_internal_label = 0;
+ pool_get (sm->ec_labels, new_internal_label);
+ *new_internal_label = 0;
+ mhash_set ((mhash_t *) color_table, &endpoint,
+ (new_internal_label - sm->ec_labels) + SRMPLS_TE_OFFSET, NULL);
+
+ return (new_internal_label - sm->ec_labels) + SRMPLS_TE_OFFSET;
+}
+
+always_inline void
+internal_label_lock_co (ip46_address_t endpoint, u32 color, char co_bits)
+{
+ ip46_address_t zero, any;
+ ip46_address_reset (&zero);
+ any.as_u64[0] = any.as_u64[1] = (u64) ~ 0;
+ switch (co_bits)
+ {
+ case SR_TE_CO_BITS_10:
+ internal_label_lock (endpoint, color);
+ internal_label_lock (zero, color);
+ internal_label_lock (any, color);
+ break;
+ case SR_TE_CO_BITS_01:
+ internal_label_lock (endpoint, color);
+ internal_label_lock (zero, color);
+ break;
+ case SR_TE_CO_BITS_00:
+ case SR_TE_CO_BITS_11:
+ internal_label_lock (endpoint, color);
+ break;
+ }
+}
+
+/**
+ * @brief lock the label for (NH, C)
+ * endpoint might be NULL or ANY
+ * NULL = 0, ANY=~0
+ */
+void
+internal_label_lock (ip46_address_t endpoint, u32 color)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ uword *color_table, *result_label;
+
+ if (!sm->sr_policies_c2e2eclabel_hash.hash)
+ return;
+
+ color_table = mhash_get (&sm->sr_policies_c2e2eclabel_hash, &color);
+ if (!color_table)
+ return;
+
+ result_label = mhash_get ((mhash_t *) color_table, &endpoint);
+
+ if (!result_label)
+ return;
+
+ /* Lock it */
+ u32 *label_lock =
+ pool_elt_at_index (sm->ec_labels, *result_label - SRMPLS_TE_OFFSET);
+ (*label_lock)++;
+}
+
+
+always_inline void
+internal_label_unlock_co (ip46_address_t endpoint, u32 color, char co_bits)
+{
+ ip46_address_t zero, any;
+ ip46_address_reset (&zero);
+ any.as_u64[0] = any.as_u64[1] = (u64) ~ 0;
+ switch (co_bits)
+ {
+ case SR_TE_CO_BITS_10:
+ internal_label_unlock (endpoint, color);
+ internal_label_unlock (zero, color);
+ internal_label_unlock (any, color);
+ break;
+ case SR_TE_CO_BITS_01:
+ internal_label_unlock (endpoint, color);
+ internal_label_unlock (zero, color);
+ break;
+ case SR_TE_CO_BITS_00:
+ case SR_TE_CO_BITS_11:
+ internal_label_unlock (endpoint, color);
+ break;
+ }
+}
+
+/**
+ * @brief Release lock on label for (endpoint, color)
+ * endpoint might be NULL or ANY
+ * NULL = 0, ANY=~0
+ */
+void
+internal_label_unlock (ip46_address_t endpoint, u32 color)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ uword *color_table, *result_label;
+
+ if (!sm->sr_policies_c2e2eclabel_hash.hash)
+ return;
+
+ color_table = mhash_get (&sm->sr_policies_c2e2eclabel_hash, &color);
+ if (!color_table)
+ return;
+
+ result_label = mhash_get ((mhash_t *) color_table, &endpoint);
+
+ if (!result_label)
+ return;
+
+ u32 *label_lock =
+ pool_elt_at_index (sm->ec_labels, *result_label - SRMPLS_TE_OFFSET);
+ (*label_lock)--;
+
+ if (*label_lock == 0)
+ {
+ pool_put (sm->ec_labels, label_lock);
+ mhash_unset ((mhash_t *) color_table, &endpoint, NULL);
+ if (mhash_elts ((mhash_t *) color_table) == 0)
+ {
+ mhash_free ((mhash_t *) color_table);
+ mhash_unset (&sm->sr_policies_c2e2eclabel_hash, &color, NULL);
+ if (mhash_elts (&sm->sr_policies_c2e2eclabel_hash) == 0)
+ {
+ mhash_free (&sm->sr_policies_c2e2eclabel_hash);
+ sm->sr_policies_c2e2eclabel_hash.hash = NULL;
+ fib_table_unlock (sm->fib_table_EC, FIB_PROTOCOL_MPLS,
+ FIB_SOURCE_SR);
+ sm->fib_table_EC = (u32) ~ 0;
+ }
+ }
+ }
+}
+
+/********************* steering computation *********************************/
+/**
+ * @brief function to update the FIB
+ */
+void
+compute_sr_te_automated_steering_fib_entry (mpls_sr_steering_policy_t *
+ steer_pl)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ fib_prefix_t pfx = { 0 };
+
+ u32 *internal_labels = 0;
+ ip46_address_t zero, any;
+ ip46_address_reset (&zero);
+ any.as_u64[0] = any.as_u64[1] = (u64) ~ 0;
+
+ u32 *color_i = NULL;
+ vec_foreach (color_i, steer_pl->color)
+ {
+ switch (steer_pl->co_bits)
+ {
+ case SR_TE_CO_BITS_10:
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (steer_pl->next_hop,
+ *color_i));
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (zero, *color_i));
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (any, *color_i));
+ break;
+ case SR_TE_CO_BITS_01:
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (steer_pl->next_hop,
+ *color_i));
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (zero, *color_i));
+ break;
+ case SR_TE_CO_BITS_00:
+ case SR_TE_CO_BITS_11:
+ vec_add1 (internal_labels,
+ find_or_create_internal_label (steer_pl->next_hop,
+ *color_i));
+ break;
+ }
+ }
+
+ /* Does hidden FIB already exist? */
+ 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);
+ }
+
+ /* Add the corresponding FIB entries */
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_eos = MPLS_EOS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = sm->fib_table_EC,
+ .frp_weight = 1,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_label_stack = 0
+ };
+ fib_route_path_t *paths = NULL;
+
+ if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP6;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip6 = steer_pl->classify.prefix.ip6;
+ }
+ else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP4;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip4 = steer_pl->classify.prefix.ip4;
+ }
+
+ if (steer_pl->vpn_label != (u32) ~ 0)
+ {
+ fib_mpls_label_t fml = {
+ .fml_value = steer_pl->vpn_label,
+ };
+ vec_add1 (path.frp_label_stack, fml);
+ path.frp_eos = MPLS_NON_EOS;
+ }
+
+ u32 label_i;
+ vec_foreach_index (label_i, internal_labels)
+ {
+ path.frp_local_label = internal_labels[label_i];
+ path.frp_preference = label_i;
+ vec_add1 (paths, path);
+ }
+
+ /* Finally we must add to FIB IGP to N */
+ clib_memcpy (&path.frp_addr, &steer_pl->next_hop,
+ sizeof (steer_pl->next_hop));
+ path.frp_preference = vec_len (internal_labels);
+ path.frp_label_stack = NULL;
+
+ if (steer_pl->nh_type == SR_STEER_IPV6)
+ {
+ path.frp_proto = DPO_PROTO_IP6;
+ path.frp_fib_index =
+ fib_table_find (FIB_PROTOCOL_IP6,
+ (steer_pl->classify.fib_table !=
+ (u32) ~ 0 ? steer_pl->classify.fib_table : 0));
+ }
+ else if (steer_pl->nh_type == SR_STEER_IPV4)
+ {
+ path.frp_proto = DPO_PROTO_IP4;
+ path.frp_fib_index =
+ fib_table_find (FIB_PROTOCOL_IP4,
+ (steer_pl->classify.fib_table !=
+ (u32) ~ 0 ? steer_pl->classify.fib_table : 0));
+ }
+
+ vec_add1 (paths, path);
+ if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
+ fib_table_entry_update (fib_table_find
+ (FIB_PROTOCOL_IP6,
+ (steer_pl->classify.fib_table !=
+ (u32) ~ 0 ? steer_pl->classify.fib_table : 0)),
+ &pfx, FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+ else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
+ fib_table_entry_update (fib_table_find
+ (FIB_PROTOCOL_IP4,
+ (steer_pl->classify.fib_table !=
+ (u32) ~ 0 ? steer_pl->classify.fib_table : 0)),
+ &pfx, FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+
+ vec_free (paths);
+ paths = NULL;
+}
+
+/**
+ * @brief Steer traffic L3 traffic through a given SR-MPLS policy
+ *
+ * @param is_del
+ * @param bsid is the bindingSID of the SR Policy (alt to sr_policy_index)
+ * @param sr_policy is the index of the SR Policy (alt to bsid)
+ * @param table_id is the VRF where to install the FIB entry for the BSID
+ * @param prefix is the IPv4/v6 address for L3 traffic type
+ * @param mask_width is the mask for L3 traffic type
+ * @param traffic_type describes the type of traffic
+ * @param next_hop SR TE Next-Hop
+ * @param nh_type is the AF of Next-Hop
+ * @param color SR TE color
+ * @param co_bits SR TE color-only bits
+ *
+ * @return 0 if correct, else error
+ */
+int
+sr_mpls_steering_policy_add (mpls_label_t bsid, u32 table_id,
+ ip46_address_t * prefix, u32 mask_width,
+ u8 traffic_type, ip46_address_t * next_hop,
+ u8 nh_type, u32 color, char co_bits,
+ mpls_label_t vpn_label)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ sr_mpls_steering_key_t key;
+ mpls_sr_steering_policy_t *steer_pl;
+ fib_prefix_t pfx = { 0 };
+
+ mpls_sr_policy_t *sr_policy = 0;
+ uword *p = 0;
+
+ clib_memset (&key, 0, sizeof (sr_mpls_steering_key_t));
+
+ if (traffic_type != SR_STEER_IPV4 && traffic_type != SR_STEER_IPV6)
+ return -1;
+
+ /* Compute the steer policy key */
+ key.prefix.as_u64[0] = prefix->as_u64[0];
+ key.prefix.as_u64[1] = prefix->as_u64[1];
+ key.mask_width = mask_width;
+ key.fib_table = (table_id != (u32) ~ 0 ? table_id : 0);
+ key.traffic_type = traffic_type;
+
+ /*
+ * Search for steering policy. If already exists we are adding a new
+ * color.
+ */
+ if (!sm->sr_steer_policies_hash.hash)
+ mhash_init (&sm->sr_steer_policies_hash, sizeof (uword),
+ sizeof (sr_mpls_steering_key_t));
+
+ p = mhash_get (&sm->sr_steer_policies_hash, &key);
+ if (p)
+ {
+ steer_pl = pool_elt_at_index (sm->steer_policies, p[0]);
+ if (steer_pl->bsid != (u32) ~ 0)
+ return -1; //Means we are rewritting the steering. Not allowed.
+
+ /* Means we are adding a color. Check that NH match. */
+ if (ip46_address_cmp (&steer_pl->next_hop, next_hop))
+ return -2;
+ if (vec_search (steer_pl->color, color) != ~0)
+ return -3;
+ if (steer_pl->co_bits != co_bits)
+ return -4; /* CO colors should be the same */
+ if (steer_pl->vpn_label != vpn_label)
+ return -5; /* VPN label should be the same */
+
+ /* Remove the steering and ReDo it */
+ vec_add1 (steer_pl->color, color);
+ vec_sort_with_function (steer_pl->color, sort_color_descent);
+ compute_sr_te_automated_steering_fib_entry (steer_pl);
+ internal_label_lock_co (steer_pl->next_hop, color, steer_pl->co_bits);
+ return 0;
+ }
+
+ /* Create a new steering policy */
+ pool_get (sm->steer_policies, steer_pl);
+ clib_memset (steer_pl, 0, sizeof (*steer_pl));
+ clib_memcpy (&steer_pl->classify.prefix, prefix, sizeof (ip46_address_t));
+ clib_memcpy (&steer_pl->next_hop, next_hop, sizeof (ip46_address_t));
+ steer_pl->nh_type = nh_type;
+ steer_pl->co_bits = co_bits;
+ steer_pl->classify.mask_width = mask_width;
+ steer_pl->classify.fib_table = (table_id != (u32) ~ 0 ? table_id : 0);
+ steer_pl->classify.traffic_type = traffic_type;
+ steer_pl->color = NULL;
+ steer_pl->vpn_label = vpn_label;
+
+ /* Create and store key */
+ mhash_set (&sm->sr_steer_policies_hash, &key, steer_pl - sm->steer_policies,
+ NULL);
+
+ /* Local steering */
+ if (bsid != (u32) ~ 0)
+ {
+ if (!sm->sr_policies_index_hash)
+ sm->sr_policies_index_hash = hash_create (0, sizeof (mpls_label_t));
+ steer_pl->bsid = bsid;
+ p = hash_get (sm->sr_policies_index_hash, bsid);
+ if (!p)
+ return -1;
+ sr_policy = pool_elt_at_index (sm->sr_policies, p[0]);
+
+ fib_route_path_t path = {
+ .frp_proto = DPO_PROTO_MPLS,
+ .frp_local_label = sr_policy->bsid,
+ .frp_eos = MPLS_EOS,
+ .frp_sw_if_index = ~0,
+ .frp_fib_index = 0,
+ .frp_weight = 1,
+ .frp_flags = FIB_ROUTE_PATH_FLAG_NONE,
+ .frp_label_stack = 0
+ };
+ fib_route_path_t *paths = NULL;
+
+ if (steer_pl->vpn_label != (u32) ~ 0)
+ {
+ fib_mpls_label_t fml = {
+ .fml_value = steer_pl->vpn_label,
+ };
+ vec_add1 (path.frp_label_stack, fml);
+ }
+
+ /* FIB API calls - Recursive route through the BindingSID */
+ if (traffic_type == SR_STEER_IPV6)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP6;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip6 = steer_pl->classify.prefix.ip6;
+ path.frp_fib_index = 0;
+ path.frp_preference = 0;
+ vec_add1 (paths, path);
+ fib_table_entry_path_add2 (fib_table_find
+ (FIB_PROTOCOL_IP6,
+ (table_id != (u32) ~ 0 ? table_id : 0)),
+ &pfx, FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+ vec_free (paths);
+ }
+ else if (traffic_type == SR_STEER_IPV4)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP4;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip4 = steer_pl->classify.prefix.ip4;
+ path.frp_fib_index = 0;
+ path.frp_preference = 0;
+ vec_add1 (paths, path);
+ fib_table_entry_path_add2 (fib_table_find
+ (FIB_PROTOCOL_IP4,
+ (table_id != (u32) ~ 0 ? table_id : 0)),
+ &pfx, FIB_SOURCE_SR,
+ FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT, paths);
+ vec_free (paths);
+ }
+ }
+ /* Automated steering */
+ else
+ {
+ steer_pl->bsid = (u32) ~ 0;
+ vec_add1 (steer_pl->color, color);
+ compute_sr_te_automated_steering_fib_entry (steer_pl);
+ internal_label_lock_co (steer_pl->next_hop, color, steer_pl->co_bits);
+ }
+ return 0;
+}
+
+/**
+ * @brief Delete steering rule for an SR-MPLS policy
+ *
+ * @param is_del
+ * @param bsid is the bindingSID of the SR Policy (alt to sr_policy_index)
+ * @param sr_policy is the index of the SR Policy (alt to bsid)
+ * @param table_id is the VRF where to install the FIB entry for the BSID
+ * @param prefix is the IPv4/v6 address for L3 traffic type
+ * @param mask_width is the mask for L3 traffic type
+ * @param traffic_type describes the type of traffic
+ * @param next_hop SR TE Next-HOP
+ * @param nh_type is the AF of Next-Hop
+ * @param color SR TE color
+ *
+ * @return 0 if correct, else error
+ */
+int
+sr_mpls_steering_policy_del (ip46_address_t * prefix, u32 mask_width,
+ u8 traffic_type, u32 table_id, u32 color)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ sr_mpls_steering_key_t key;
+ mpls_sr_steering_policy_t *steer_pl;
+ fib_prefix_t pfx = { 0 };
+ uword *p = 0;
+
+ clib_memset (&key, 0, sizeof (sr_mpls_steering_key_t));
+
+ /* Compute the steer policy key */
+ if (traffic_type != SR_STEER_IPV4 && traffic_type != SR_STEER_IPV6)
+ return -1;
+
+ key.prefix.as_u64[0] = prefix->as_u64[0];
+ key.prefix.as_u64[1] = prefix->as_u64[1];
+ key.mask_width = mask_width;
+ key.fib_table = (table_id != (u32) ~ 0 ? table_id : 0);
+ key.traffic_type = traffic_type;
+
+ if (!sm->sr_steer_policies_hash.hash)
+ mhash_init (&sm->sr_steer_policies_hash, sizeof (uword),
+ sizeof (sr_mpls_steering_key_t));
+
+ /* Search for the item */
+ p = mhash_get (&sm->sr_steer_policies_hash, &key);
+
+ if (!p)
+ return -1;
+
+ /* Retrieve Steer Policy function */
+ steer_pl = pool_elt_at_index (sm->steer_policies, p[0]);
+
+ if (steer_pl->bsid == (u32) ~ 0)
+ {
+ /* Remove the color from the color vector */
+ vec_del1 (steer_pl->color, vec_search (steer_pl->color, color));
+
+ if (vec_len (steer_pl->color))
+ {
+ /* Reorder Colors */
+ vec_sort_with_function (steer_pl->color, sort_color_descent);
+ compute_sr_te_automated_steering_fib_entry (steer_pl);
+ /* Remove all the locks for this ones... */
+ internal_label_unlock_co (steer_pl->next_hop, color,
+ steer_pl->co_bits);
+ return 0;
+ }
+ else
+ {
+ vec_free (steer_pl->color);
+ /* Remove FIB entry */
+ if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP6;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip6 = steer_pl->classify.prefix.ip6;
+ fib_table_entry_delete (fib_table_find
+ (FIB_PROTOCOL_IP6,
+ steer_pl->classify.fib_table), &pfx,
+ FIB_SOURCE_SR);
+ }
+ else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP4;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip4 = steer_pl->classify.prefix.ip4;
+ fib_table_entry_delete (fib_table_find
+ (FIB_PROTOCOL_IP4,
+ steer_pl->classify.fib_table), &pfx,
+ FIB_SOURCE_SR);
+ }
+ /* Remove all the locks for this ones... */
+ internal_label_unlock_co (steer_pl->next_hop, color,
+ steer_pl->co_bits);
+ }
+ }
+ else //Remove by BSID
+ {
+ if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP6;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip6 = steer_pl->classify.prefix.ip6;
+ fib_table_entry_delete (fib_table_find
+ (FIB_PROTOCOL_IP6,
+ steer_pl->classify.fib_table), &pfx,
+ FIB_SOURCE_SR);
+ }
+ else if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
+ {
+ pfx.fp_proto = FIB_PROTOCOL_IP4;
+ pfx.fp_len = steer_pl->classify.mask_width;
+ pfx.fp_addr.ip4 = steer_pl->classify.prefix.ip4;
+ fib_table_entry_delete (fib_table_find
+ (FIB_PROTOCOL_IP4,
+ steer_pl->classify.fib_table), &pfx,
+ FIB_SOURCE_SR);
+ }
+ }
+ /* Delete SR steering policy entry */
+ pool_put (sm->steer_policies, steer_pl);
+ mhash_unset (&sm->sr_steer_policies_hash, &key, NULL);
+ if (mhash_elts (&sm->sr_steer_policies_hash) == 0)
+ {
+ mhash_free (&sm->sr_steer_policies_hash);
+ sm->sr_steer_policies_hash.hash = NULL;
+ }
+ return 0;
+}
+
+static clib_error_t *
+sr_mpls_steer_policy_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ int is_del = 0;
+
+ ip46_address_t prefix, nh;
+ u32 dst_mask_width = 0;
+ u8 traffic_type = 0;
+ u8 nh_type = 0;
+ u32 fib_table = (u32) ~ 0, color = (u32) ~ 0;
+ u32 co_bits = 0;
+
+ mpls_label_t bsid, vpn_label = (u32) ~ 0;
+
+ u8 sr_policy_set = 0;
+
+ clib_memset (&prefix, 0, sizeof (ip46_address_t));
+ clib_memset (&nh, 0, sizeof (ip46_address_t));
+
+ int rv;
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "del"))
+ is_del = 1;
+ else if (!traffic_type
+ && unformat (input, "l3 %U/%d", unformat_ip6_address,
+ &prefix.ip6, &dst_mask_width))
+ traffic_type = SR_STEER_IPV6;
+ else if (!traffic_type
+ && unformat (input, "l3 %U/%d", unformat_ip4_address,
+ &prefix.ip4, &dst_mask_width))
+ traffic_type = SR_STEER_IPV4;
+ else if (!sr_policy_set
+ && unformat (input, "via sr policy bsid %U",
+ unformat_mpls_unicast_label, &bsid))
+ sr_policy_set = 1;
+ else if (!sr_policy_set
+ && unformat (input, "via next-hop %U color %d co %d",
+ unformat_ip4_address, &nh.ip4, &color, &co_bits))
+ {
+ sr_policy_set = 1;
+ nh_type = SR_STEER_IPV4;
+ }
+ else if (!sr_policy_set
+ && unformat (input, "via next-hop %U color %d co %d",
+ unformat_ip6_address, &nh.ip6, &color, &co_bits))
+ {
+ sr_policy_set = 1;
+ nh_type = SR_STEER_IPV6;
+ }
+ else if (fib_table == (u32) ~ 0
+ && unformat (input, "fib-table %d", &fib_table));
+ else if (unformat (input, "vpn-label %U",
+ unformat_mpls_unicast_label, &vpn_label));
+ else
+ break;
+ }
+
+ if (!traffic_type)
+ return clib_error_return (0, "No L3 traffic specified");
+ if (!sr_policy_set)
+ return clib_error_return (0, "No SR policy specified");
+
+ /* Make sure that the prefixes are clean */
+ if (traffic_type == SR_STEER_IPV4)
+ {
+ u32 mask =
+ (dst_mask_width ? (0xFFFFFFFFu >> (32 - dst_mask_width)) : 0);
+ prefix.ip4.as_u32 &= mask;
+ }
+ else if (traffic_type == SR_STEER_IPV6)
+ {
+ ip6_address_t mask;
+ ip6_address_mask_from_width (&mask, dst_mask_width);
+ ip6_address_mask (&prefix.ip6, &mask);
+ }
+
+ if (nh_type)
+ bsid = (u32) ~ 0;
+
+ if (is_del)
+ rv =
+ sr_mpls_steering_policy_del (&prefix, dst_mask_width,
+ traffic_type, fib_table, color);
+
+ else
+ rv =
+ sr_mpls_steering_policy_add (bsid, fib_table, &prefix, dst_mask_width,
+ traffic_type, &nh, nh_type, color, co_bits,
+ vpn_label);
+
+ switch (rv)
+ {
+ case 0:
+ break;
+ case 1:
+ return 0;
+ case -1:
+ return clib_error_return (0, "Incorrect API usage.");
+ case -2:
+ return clib_error_return (0, "The Next-Hop does not match.");
+ case -3:
+ return clib_error_return (0, "The color already exists.");
+ case -4:
+ return clib_error_return (0, "The co-bits do not match.");
+ case -5:
+ return clib_error_return (0, "The VPN-labels do not match.");
+ default:
+ return clib_error_return (0, "BUG: sr steer policy returns %d", rv);
+ }
+ return 0;
+}
+
+VLIB_CLI_COMMAND(sr_mpls_steer_policy_command, static)=
+{
+ .path = "sr mpls steer",
+ .short_help = "sr mpls steer (del) l3 <ip_addr/mask> "
+ "via [sr policy bsid <mpls_label> || next-hop <ip46_addr> color <u32> co <0|1|2|3> ](fib-table <fib_table_index>)(vpn-label 500)",
+ .long_help =
+ "\tSteer L3 traffic through an existing SR policy.\n"
+ "\tExamples:\n"
+ "\t\tsr steer l3 2001::/64 via sr_policy bsid 29999\n"
+ "\t\tsr steer del l3 2001::/64 via sr_policy bsid 29999\n"
+ "\t\tsr steer l3 2001::/64 via next-hop 1.1.1.1 color 1234 co 0\n"
+ "\t\tsr steer l3 2001::/64 via next-hop 2001::1 color 1234 co 2 vpn-label 500\n",
+ .function = sr_mpls_steer_policy_command_fn,
+};
+
+static clib_error_t *
+show_sr_mpls_steering_policies_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+ mpls_sr_steering_policy_t **steer_policies = 0;
+ mpls_sr_steering_policy_t *steer_pl;
+
+ int i;
+
+ vlib_cli_output (vm, "SR MPLS steering policies:");
+ pool_foreach (steer_pl, sm->steer_policies) {
+ vec_add1(steer_policies, steer_pl);
+ }
+ for (i = 0; i < vec_len (steer_policies); i++)
+ {
+ vlib_cli_output (vm, "==========================");
+ steer_pl = steer_policies[i];
+ if (steer_pl->classify.traffic_type == SR_STEER_IPV4)
+ {
+ vlib_cli_output (vm, "Prefix: %U/%d via:",
+ format_ip4_address,
+ &steer_pl->classify.prefix.ip4,
+ steer_pl->classify.mask_width);
+ }
+ else if (steer_pl->classify.traffic_type == SR_STEER_IPV6)
+ {
+ vlib_cli_output (vm, "Prefix: %U/%d via:",
+ format_ip6_address,
+ &steer_pl->classify.prefix.ip6,
+ steer_pl->classify.mask_width);
+ }
+
+ if (steer_pl->bsid != (u32) ~ 0)
+ {
+ vlib_cli_output (vm, "· BSID %U",
+ format_mpls_unicast_label, steer_pl->bsid);
+ }
+ else
+ {
+ if (steer_pl->nh_type == SR_STEER_IPV4)
+ {
+ vlib_cli_output (vm, "· Next-hop %U",
+ format_ip4_address, &steer_pl->next_hop.ip4);
+ }
+ else if (steer_pl->nh_type == SR_STEER_IPV6)
+ {
+ vlib_cli_output (vm, "· Next-hop %U",
+ format_ip6_address, &steer_pl->next_hop.ip6);
+ }
+
+ u32 *color_i = 0;
+ u8 *s = NULL;
+ s = format (s, "[ ");
+ vec_foreach (color_i, steer_pl->color)
+ {
+ s = format (s, "%d, ", *color_i);
+ }
+ s = format (s, "\b\b ]");
+ vlib_cli_output (vm, "· Color %s", s);
+
+ switch (steer_pl->co_bits)
+ {
+ case SR_TE_CO_BITS_00:
+ vlib_cli_output (vm, "· CO-bits: 00");
+ break;
+ case SR_TE_CO_BITS_01:
+ vlib_cli_output (vm, "· CO-bits: 01");
+ break;
+ case SR_TE_CO_BITS_10:
+ vlib_cli_output (vm, "· CO-bits: 10");
+ break;
+ case SR_TE_CO_BITS_11:
+ vlib_cli_output (vm, "· CO-bits: 11");
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+VLIB_CLI_COMMAND(show_sr_mpls_steering_policies_command, static)=
+{
+ .path = "show sr mpls steering policies",
+ .short_help = "show sr mpls steering policies",
+ .function = show_sr_mpls_steering_policies_command_fn,
+};
+
+clib_error_t *
+sr_mpls_steering_init (vlib_main_t * vm)
+{
+ mpls_sr_main_t *sm = &sr_mpls_main;
+
+ /* Init memory for function keys */
+ sm->sr_steer_policies_hash.hash = NULL;
+
+ sm->fib_table_EC = (u32) ~ 0;
+ sm->ec_labels = 0;
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION(sr_mpls_steering_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables: eval: (c-set-style "gnu") End:
+ */
diff --git a/src/plugins/srmpls/sr_mpls_test.c b/src/plugins/srmpls/sr_mpls_test.c
new file mode 100644
index 00000000000..7aff4c32b06
--- /dev/null
+++ b/src/plugins/srmpls/sr_mpls_test.c
@@ -0,0 +1,174 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2021 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 <vat/vat.h>
+#include <vlibapi/api.h>
+#include <vlibmemory/api.h>
+#include <vppinfra/error.h>
+#include <vpp/api/types.h>
+
+#define __plugin_msg_base sr_mpls_test_main.msg_id_base
+#include <vlibapi/vat_helper_macros.h>
+
+/* Declare message IDs */
+#include <vnet/format_fns.h>
+#include <plugins/srmpls/sr_mpls.api_enum.h>
+#include <plugins/srmpls/sr_mpls.api_types.h>
+
+#define vl_endianfun /* define message structures */
+#include <plugins/srmpls/sr_mpls.api.h>
+#undef vl_endianfun
+
+typedef struct
+{
+ /* API message ID base */
+ u16 msg_id_base;
+ u32 ping_id;
+ vat_main_t *vat_main;
+} sr_mpls_test_main_t;
+
+static sr_mpls_test_main_t sr_mpls_test_main;
+
+static int
+api_sr_mpls_policy_mod (vat_main_t *vam)
+{
+ return -1;
+}
+
+static int
+api_sr_mpls_steering_add_del (vat_main_t *vam)
+{
+ return -1;
+}
+
+static int
+api_sr_mpls_policy_assign_endpoint_color (vat_main_t *vam)
+{
+ return -1;
+}
+
+static int
+api_sr_mpls_policy_add (vat_main_t *vam)
+{
+ unformat_input_t *i = vam->input;
+ vl_api_sr_mpls_policy_add_t *mp;
+ u32 bsid = 0;
+ u32 weight = 1;
+ u8 type = 0;
+ u8 n_segments = 0;
+ u32 sid;
+ u32 *segments = NULL;
+ int ret;
+
+ /* Parse args required to build the message */
+ while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (i, "bsid %d", &bsid))
+ ;
+ else if (unformat (i, "weight %d", &weight))
+ ;
+ else if (unformat (i, "spray"))
+ type = 1;
+ else if (unformat (i, "next %d", &sid))
+ {
+ n_segments += 1;
+ vec_add1 (segments, htonl (sid));
+ }
+ else
+ {
+ clib_warning ("parse error '%U'", format_unformat_error, i);
+ return -99;
+ }
+ }
+
+ if (bsid == 0)
+ {
+ errmsg ("bsid not set");
+ return -99;
+ }
+
+ if (n_segments == 0)
+ {
+ errmsg ("no sid in segment stack");
+ return -99;
+ }
+
+ /* Construct the API message */
+ M2 (SR_MPLS_POLICY_ADD, mp, sizeof (u32) * n_segments);
+
+ mp->bsid = htonl (bsid);
+ mp->weight = htonl (weight);
+ mp->is_spray = type;
+ mp->n_segments = n_segments;
+ memcpy (mp->segments, segments, sizeof (u32) * n_segments);
+ vec_free (segments);
+
+ /* send it... */
+ S (mp);
+
+ /* Wait for a reply... */
+ W (ret);
+ return ret;
+}
+
+static int
+api_sr_mpls_policy_del (vat_main_t *vam)
+{
+ unformat_input_t *i = vam->input;
+ vl_api_sr_mpls_policy_del_t *mp;
+ u32 bsid = 0;
+ int ret;
+
+ /* Parse args required to build the message */
+ while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (i, "bsid %d", &bsid))
+ ;
+ else
+ {
+ clib_warning ("parse error '%U'", format_unformat_error, i);
+ return -99;
+ }
+ }
+
+ if (bsid == 0)
+ {
+ errmsg ("bsid not set");
+ return -99;
+ }
+
+ /* Construct the API message */
+ M (SR_MPLS_POLICY_DEL, mp);
+
+ mp->bsid = htonl (bsid);
+
+ /* send it... */
+ S (mp);
+
+ /* Wait for a reply... */
+ W (ret);
+ return ret;
+}
+
+#include <plugins/srmpls/sr_mpls.api_test.c>
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */