SRv6 dynamic proxy plugin
[vpp.git] / src / plugins / srv6-ad / ad.c
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  * ad.c - SRv6 Dynamic Proxy (AD) function
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <srv6-ad/ad.h>
26
27 unsigned char function_name[] = "SRv6-AD-plugin";
28 unsigned char keyword_str[] = "End.AD";
29 unsigned char def_str[] =
30   "Endpoint with dynamic proxy to SR-unaware appliance";
31 unsigned char params_str[] = "nh <next-hop> oif <iface-out> iif <iface-in>";
32
33
34 /*****************************************/
35 /* SRv6 LocalSID instantiation and removal functions */
36 static int
37 srv6_ad_localsid_creation_fn (ip6_sr_localsid_t * localsid)
38 {
39   ip6_sr_main_t *srm = &sr_main;
40   srv6_ad_main_t *sm = &srv6_ad_main;
41   srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
42   u32 localsid_index = localsid - srm->localsids;
43
44   /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
45
46   /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
47   adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
48   if (ls_mem->ip_version == DA_IP4)
49     nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
50                                         VNET_LINK_IP4, &ls_mem->nh_addr,
51                                         ls_mem->sw_if_index_out);
52   else if (ls_mem->ip_version == DA_IP6)
53     nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
54                                         VNET_LINK_IP6, &ls_mem->nh_addr,
55                                         ls_mem->sw_if_index_out);
56   if (nh_adj_index == ADJ_INDEX_INVALID)
57     return -5;
58
59   ls_mem->nh_adj = nh_adj_index;
60
61
62   /* Step 2: Prepare inbound policy for packets returning from the VNF */
63
64   /* Sanitise the SW_IF_INDEX */
65   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
66                           ls_mem->sw_if_index_in))
67     return -3;
68
69   vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
70                                                    ls_mem->sw_if_index_in);
71   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
72     return -3;
73
74   int ret = -1;
75   if (ls_mem->ip_version == DA_IP4)
76     {
77       ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
78                                          ls_mem->sw_if_index_in, 1, 0, 0);
79       if (ret != 0)
80         return -1;
81
82       /* FIB API calls - Recursive route through the BindingSID */
83       if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid4))
84         {
85           sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
86         }
87       else
88         {
89           vec_resize (sm->sw_iface_localsid4,
90                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
91                        - vec_len (sm->sw_iface_localsid4)));
92           sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
93         }
94     }
95   else if (ls_mem->ip_version == DA_IP6)
96     {
97       ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
98                                          ls_mem->sw_if_index_in, 1, 0, 0);
99       if (ret != 0)
100         return -1;
101
102       if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid6))
103         {
104           sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
105         }
106       else
107         {
108           vec_resize (sm->sw_iface_localsid6,
109                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
110                        - vec_len (sm->sw_iface_localsid6)));
111           sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
112         }
113     }
114
115   return 0;
116 }
117
118 static int
119 srv6_ad_localsid_removal_fn (ip6_sr_localsid_t * localsid)
120 {
121   srv6_ad_main_t *sm = &srv6_ad_main;
122   srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
123
124   int ret = -1;
125   if (ls_mem->ip_version == DA_IP4)
126     {
127       /* Remove hardware indirection (from sr_steering.c:137) */
128       ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
129                                          ls_mem->sw_if_index_in, 0, 0, 0);
130       if (ret != 0)
131         return -1;
132
133       /* Remove local SID pointer from interface table (from sr_steering.c:139) */
134       sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
135     }
136   else if (ls_mem->ip_version == DA_IP6)
137     {
138       /* Remove hardware indirection (from sr_steering.c:137) */
139       ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
140                                          ls_mem->sw_if_index_in, 0, 0, 0);
141       if (ret != 0)
142         return -1;
143
144       /* Remove local SID pointer from interface table (from sr_steering.c:139) */
145       sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
146     }
147
148
149   /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
150   adj_unlock (ls_mem->nh_adj);
151
152   /* Clean up local SID memory */
153   clib_mem_free (localsid->plugin_mem);
154
155   return 0;
156 }
157
158 /**********************************/
159 /* SRv6 LocalSID format functions */
160 /*
161  * Prints nicely the parameters of a localsid
162  * Example: print "Table 5"
163  */
164 u8 *
165 format_srv6_ad_localsid (u8 * s, va_list * args)
166 {
167   srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
168
169   vnet_main_t *vnm = vnet_get_main ();
170
171   if (ls_mem->ip_version == DA_IP4)
172     {
173       return (format (s,
174                       "Next-hop:\t%U\n"
175                       "\tOutgoing iface: %U\n"
176                       "\tIncoming iface: %U",
177                       format_ip4_address, &ls_mem->nh_addr.ip4,
178                       format_vnet_sw_if_index_name, vnm,
179                       ls_mem->sw_if_index_out, format_vnet_sw_if_index_name,
180                       vnm, ls_mem->sw_if_index_in));
181     }
182   else
183     {
184       return (format (s,
185                       "Next-hop:\t%U\n"
186                       "\tOutgoing iface: %U\n"
187                       "\tIncoming iface: %U",
188                       format_ip6_address, &ls_mem->nh_addr.ip6,
189                       format_vnet_sw_if_index_name, vnm,
190                       ls_mem->sw_if_index_out, format_vnet_sw_if_index_name,
191                       vnm, ls_mem->sw_if_index_in));
192     }
193 }
194
195 /*
196  * Process the parameters of a localsid
197  * Example: process from:
198  * sr localsid address cafe::1 behavior new_srv6_localsid 5
199  * everything from behavior on... so in this case 'new_srv6_localsid 5'
200  * Notice that it MUST match the keyword_str and params_str defined above.
201  */
202 uword
203 unformat_srv6_ad_localsid (unformat_input_t * input, va_list * args)
204 {
205   void **plugin_mem_p = va_arg (*args, void **);
206   srv6_ad_localsid_t *ls_mem;
207
208   vnet_main_t *vnm = vnet_get_main ();
209
210   ip46_address_t nh_addr;
211   u32 sw_if_index_out;
212   u32 sw_if_index_in;
213
214   if (unformat (input, "end.ad nh %U oif %U iif %U",
215                 unformat_ip4_address, &nh_addr.ip4,
216                 unformat_vnet_sw_interface, vnm, &sw_if_index_out,
217                 unformat_vnet_sw_interface, vnm, &sw_if_index_in))
218     {
219       /* Allocate a portion of memory */
220       ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
221
222       /* Set to zero the memory */
223       memset (ls_mem, 0, sizeof *ls_mem);
224
225       /* Our brand-new car is ready */
226       ls_mem->ip_version = DA_IP4;
227       clib_memcpy (&ls_mem->nh_addr.ip4, &nh_addr.ip4,
228                    sizeof (ip4_address_t));
229       ls_mem->sw_if_index_out = sw_if_index_out;
230       ls_mem->sw_if_index_in = sw_if_index_in;
231
232       /* Dont forget to add it to the localsid */
233       *plugin_mem_p = ls_mem;
234       return 1;
235     }
236   else if (unformat (input, "end.ad nh %U oif %U iif %U",
237                      unformat_ip6_address, &nh_addr.ip6,
238                      unformat_vnet_sw_interface, vnm, &sw_if_index_out,
239                      unformat_vnet_sw_interface, vnm, &sw_if_index_in))
240     {
241       /* Allocate a portion of memory */
242       ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
243
244       /* Set to zero the memory */
245       memset (ls_mem, 0, sizeof *ls_mem);
246
247       /* Our brand-new car is ready */
248       ls_mem->ip_version = DA_IP6;
249       clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
250                    sizeof (ip6_address_t));
251       ls_mem->sw_if_index_out = sw_if_index_out;
252       ls_mem->sw_if_index_in = sw_if_index_in;
253
254       /* Dont forget to add it to the localsid */
255       *plugin_mem_p = ls_mem;
256       return 1;
257     }
258   return 0;
259 }
260
261 /*************************/
262 /* SRv6 LocalSID FIB DPO */
263 static u8 *
264 format_srv6_ad_dpo (u8 * s, va_list * args)
265 {
266   index_t index = va_arg (*args, index_t);
267   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
268
269   return (format (s, "SR: dynamic_proxy_index:[%u]", index));
270 }
271
272 void
273 srv6_ad_dpo_lock (dpo_id_t * dpo)
274 {
275 }
276
277 void
278 srv6_ad_dpo_unlock (dpo_id_t * dpo)
279 {
280 }
281
282 const static dpo_vft_t srv6_ad_vft = {
283   .dv_lock = srv6_ad_dpo_lock,
284   .dv_unlock = srv6_ad_dpo_unlock,
285   .dv_format = format_srv6_ad_dpo,
286 };
287
288 const static char *const srv6_ad_ip6_nodes[] = {
289   "srv6-ad-localsid",
290   NULL,
291 };
292
293 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
294   [DPO_PROTO_IP6] = srv6_ad_ip6_nodes,
295 };
296
297 /**********************/
298 static clib_error_t *
299 srv6_ad_init (vlib_main_t * vm)
300 {
301   srv6_ad_main_t *sm = &srv6_ad_main;
302   int rv = 0;
303
304   sm->vlib_main = vm;
305   sm->vnet_main = vnet_get_main ();
306
307   /* Create DPO */
308   sm->srv6_ad_dpo_type = dpo_register_new_type (&srv6_ad_vft, srv6_ad_nodes);
309
310   /* Register SRv6 LocalSID */
311   rv = sr_localsid_register_function (vm,
312                                       function_name,
313                                       keyword_str,
314                                       def_str,
315                                       params_str,
316                                       &sm->srv6_ad_dpo_type,
317                                       format_srv6_ad_localsid,
318                                       unformat_srv6_ad_localsid,
319                                       srv6_ad_localsid_creation_fn,
320                                       srv6_ad_localsid_removal_fn);
321   if (rv < 0)
322     clib_error_return (0, "SRv6 LocalSID function could not be registered.");
323   else
324     sm->srv6_localsid_behavior_id = rv;
325
326   return 0;
327 }
328
329 /* *INDENT-OFF* */
330 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
331 {
332   .arc_name = "ip4-unicast",
333   .node_name = "srv6-ad4-rewrite",
334   .runs_before = 0,
335 };
336
337 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
338 {
339   .arc_name = "ip6-unicast",
340   .node_name = "srv6-ad6-rewrite",
341   .runs_before = 0,
342 };
343
344 VLIB_INIT_FUNCTION (srv6_ad_init);
345
346 VLIB_PLUGIN_REGISTER () = {
347   .version = VPP_BUILD_VER,
348   .description = "Dynamic SRv6 proxy",
349 };
350 /* *INDENT-ON* */
351
352 /*
353 * fd.io coding-style-patch-verification: ON
354 *
355 * Local Variables:
356 * eval: (c-set-style "gnu")
357 * End:
358 */