Evolving SRv6 (Segment Routing for IPv6)
[vpp.git] / src / vnet / sr / sr.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /**
17  * @file
18  * @brief Segment Routing data structures definitions
19  *
20  */
21
22 #ifndef included_vnet_sr_h
23 #define included_vnet_sr_h
24
25 #include <vnet/vnet.h>
26 #include <vnet/sr/sr_packet.h>
27 #include <vnet/ip/ip6_packet.h>
28 #include <vnet/ethernet/ethernet.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define IPv6_DEFAULT_HEADER_LENGTH 40
34 #define IPv6_DEFAULT_HOP_LIMIT 64
35 #define IPv6_DEFAULT_MAX_MASK_WIDTH 128
36
37 #define SR_BEHAVIOR_END 1
38 #define SR_BEHAVIOR_X 2
39 #define SR_BEHAVIOR_D_FIRST 3   /* Unused. Separator in between regular and D */
40 #define SR_BEHAVIOR_DX2 4
41 #define SR_BEHAVIOR_DX6 5
42 #define SR_BEHAVIOR_DX4 6
43 #define SR_BEHAVIOR_DT6 7
44 #define SR_BEHAVIOR_DT4 8
45 #define SR_BEHAVIOR_LAST 9      /* Must always be the last one */
46
47 #define SR_STEER_L2 2
48 #define SR_STEER_IPV4 4
49 #define SR_STEER_IPV6 6
50
51 #define SR_FUNCTION_SIZE 4
52 #define SR_ARGUMENT_SIZE 4
53
54 #define SR_SEGMENT_LIST_WEIGHT_DEFAULT 1
55
56 /**
57  * @brief SR Segment List (SID list)
58  */
59 typedef struct
60 {
61   ip6_address_t *segments;              /**< SIDs (key) */
62
63   u32 weight;                                           /**< SID list weight (wECMP / UCMP) */
64
65   u8 *rewrite;                                  /**< Precomputed rewrite header */
66   u8 *rewrite_bsid;                             /**< Precomputed rewrite header for bindingSID */
67
68   dpo_id_t bsid_dpo;                            /**< DPO for Encaps/Insert for BSID */
69   dpo_id_t ip6_dpo;                             /**< DPO for Encaps/Insert IPv6 */
70   dpo_id_t ip4_dpo;                             /**< DPO for Encaps IPv6 */
71 } ip6_sr_sl_t;
72
73 /* SR policy types */
74 #define SR_POLICY_TYPE_DEFAULT 0
75 #define SR_POLICY_TYPE_SPRAY 1
76 /**
77  * @brief SR Policy
78  */
79 typedef struct
80 {
81   u32 *segments_lists;          /**< SID lists indexes (vector) */
82
83   ip6_address_t bsid;                   /**< BindingSID (key) */
84
85   u8 type;                                      /**< Type (default is 0) */
86
87   /* SR Policy specific DPO                                                                               */
88   /* IF Type = DEFAULT Then Load Balancer DPO among SID lists     */
89   /* IF Type = SPRAY then Spray DPO with all SID lists                    */
90   dpo_id_t bsid_dpo;                    /**< SR Policy specific DPO - BSID */
91   dpo_id_t ip4_dpo;                     /**< SR Policy specific DPO - IPv6 */
92   dpo_id_t ip6_dpo;                     /**< SR Policy specific DPO - IPv4 */
93
94   u32 fib_table;                        /**< FIB table */
95
96   u8 is_encap;                          /**< Mode (0 is SRH insert, 1 Encaps) */
97 } ip6_sr_policy_t;
98
99 /**
100  * @brief SR LocalSID
101  */
102 typedef struct
103 {
104   ip6_address_t localsid;               /**< LocalSID IPv6 address */
105
106   char end_psp;                                 /**< Combined with End.PSP? */
107
108   u16 behavior;                                 /**< Behavior associated to this localsid */
109
110   union
111   {
112     u32 sw_if_index;                            /**< xconnect only */
113     u32 vrf_index;                              /**< vrf only */
114   };
115
116   u32 fib_table;                                /**< FIB table where localsid is registered */
117
118   u32 vlan_index;                               /**< VLAN tag (not an index) */
119
120   ip46_address_t next_hop;              /**< Next_hop for xconnect usage only */
121
122   u32 nh_adj;                                           /**< Next_adj for xconnect usage only */
123
124   void *plugin_mem;                             /**< Memory to be used by the plugin callback functions */
125 } ip6_sr_localsid_t;
126
127 typedef int (sr_plugin_callback_t) (ip6_sr_localsid_t * localsid);
128
129 /**
130  * @brief SR LocalSID behavior registration
131  */
132 typedef struct
133 {
134   u16 sr_localsid_function_number;                      /**< SR LocalSID plugin function (>SR_BEHAVIOR_LAST) */
135
136   u8 *function_name;                                                    /**< Function name. (key). */
137
138   u8 *keyword_str;                                                      /**< Behavior keyword (i.e. End.X) */
139
140   u8 *def_str;                                                          /**< Behavior definition (i.e. Endpoint with cross-connect) */
141
142   u8 *params_str;                                                       /**< Behavior parameters (i.e. <oif> <IP46next_hop>) */
143
144   dpo_type_t dpo;                                                       /**< DPO type registration */
145
146   format_function_t *ls_format;                         /**< LocalSID format function */
147
148   unformat_function_t *ls_unformat;                     /**< LocalSID unformat function */
149
150   sr_plugin_callback_t *creation;                       /**< Function within plugin that will be called after localsid creation*/
151
152   sr_plugin_callback_t *removal;                        /**< Function within plugin that will be called before localsid removal */
153 } sr_localsid_fn_registration_t;
154
155 /**
156  * @brief Steering db key
157  *
158  * L3 is IPv4/IPv6 + mask
159  * L2 is sf_if_index + vlan
160  */
161 typedef struct
162 {
163   union
164   {
165     struct
166     {
167       ip46_address_t prefix;                    /**< IP address of the prefix */
168       u32 mask_width;                                   /**< Mask width of the prefix */
169       u32 fib_table;                                    /**< VRF of the prefix */
170     } l3;
171     struct
172     {
173       u32 sw_if_index;                                  /**< Incoming software interface */
174     } l2;
175   };
176   u8 traffic_type;                                      /**< Traffic type (IPv4, IPv6, L2) */
177 } sr_steering_key_t;
178
179 typedef struct
180 {
181   sr_steering_key_t classify;           /**< Traffic classification */
182   u32 sr_policy;                                        /**< SR Policy index */
183 } ip6_sr_steering_policy_t;
184
185 /**
186  * @brief Segment Routing main datastructure
187  */
188 typedef struct
189 {
190   /* ip6-lookup next index for imposition FIB entries */
191   u32 ip6_lookup_sr_next_index;
192
193   /* ip6-replicate next index for multicast tunnel */
194   u32 ip6_lookup_sr_spray_index;
195
196   /* IP4-lookup -> SR rewrite next index */
197   u32 ip4_lookup_sr_policy_rewrite_encaps_index;
198   u32 ip4_lookup_sr_policy_rewrite_insert_index;
199
200   /* IP6-lookup -> SR rewrite next index */
201   u32 ip6_lookup_sr_policy_rewrite_encaps_index;
202   u32 ip6_lookup_sr_policy_rewrite_insert_index;
203
204   /* L2-input -> SR rewrite next index */
205   u32 l2_sr_policy_rewrite_index;
206
207   /* IP6-lookup -> SR LocalSID (SR End processing) index */
208   u32 ip6_lookup_sr_localsid_index;
209
210   /* SR SID lists */
211   ip6_sr_sl_t *sid_lists;
212
213   /* SR policies */
214   ip6_sr_policy_t *sr_policies;
215
216   /* Find an SR policy by its BindingSID */
217   ip6_address_t *sr_policy_index_by_key;
218
219   /* Pool of SR localsid instances */
220   ip6_sr_localsid_t *localsids;
221
222   /* Find a SR localsid instance based on its functionID */
223   ip6_address_t *localsids_index_by_key;
224
225   /* Pool of SR steer policies instances */
226   ip6_sr_steering_policy_t *steer_policies;
227
228   /* Find a steer policy based on its classifier */
229   sr_steering_key_t *steer_policies_index_by_key;
230
231   /* L2 steering ifaces - sr_policies */
232   u32 *sw_iface_sr_policies;
233
234   /* Spray DPO */
235   dpo_type_t sr_pr_spray_dpo_type;
236
237   /* Plugin functions */
238   sr_localsid_fn_registration_t *plugin_functions;
239
240   /* Find plugin function by name */
241   uword *plugin_functions_by_key;
242
243   /* Counters */
244   vlib_combined_counter_main_t sr_ls_valid_counters;
245   vlib_combined_counter_main_t sr_ls_invalid_counters;
246
247   /* SR Policies FIBs */
248   u32 fib_table_ip6;
249   u32 fib_table_ip4;
250
251   /* convenience */
252   vlib_main_t *vlib_main;
253   vnet_main_t *vnet_main;
254 } ip6_sr_main_t;
255
256 ip6_sr_main_t sr_main;
257
258 extern vlib_node_registration_t sr_policy_rewrite_encaps_node;
259 extern vlib_node_registration_t sr_policy_rewrite_insert_node;
260 extern vlib_node_registration_t sr_localsid_node;
261 extern vlib_node_registration_t sr_localsid_d_node;
262
263 void sr_dpo_lock (dpo_id_t * dpo);
264 void sr_dpo_unlock (dpo_id_t * dpo);
265
266 int sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
267                                    u8 * keyword_str, u8 * def_str,
268                                    u8 * params_str, dpo_type_t * dpo,
269                                    format_function_t * ls_format,
270                                    unformat_function_t * ls_unformat,
271                                    sr_plugin_callback_t * creation_fn,
272                                    sr_plugin_callback_t * removal_fn);
273
274 int
275 sr_policy_add (ip6_address_t * bsid, ip6_address_t * segments,
276                u32 weight, u8 behavior, u32 fib_table, u8 is_encap);
277 int
278 sr_policy_mod (ip6_address_t * bsid, u32 index, u32 fib_table,
279                u8 operation, ip6_address_t * segments, u32 sl_index,
280                u32 weight);
281 int sr_policy_del (ip6_address_t * bsid, u32 index);
282
283 int sr_cli_localsid (char is_del, ip6_address_t * localsid_addr,
284                      char end_psp, u8 behavior, u32 sw_if_index,
285                      u32 vlan_index, u32 fib_table, ip46_address_t * nh_addr,
286                      void *ls_plugin_mem);
287
288 int
289 sr_steering_policy (int is_del, ip6_address_t * bsid, u32 sr_policy_index,
290                     u32 table_id, ip46_address_t * prefix, u32 mask_width,
291                     u32 sw_if_index, u8 traffic_type);
292
293 #endif /* included_vnet_sr_h */
294
295 /*
296  * fd.io coding-style-patch-verification: ON
297  *
298  * Local Variables:
299  * eval: (c-set-style "gnu")
300  * End:
301  */