73ea3f6ac1f31ef34fcaa8344a9c203712bd9120
[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 #define SID_CREATE_IFACE_FEATURE_ERROR  -1
28 #define SID_CREATE_INVALID_IFACE_TYPE   -3
29 #define SID_CREATE_INVALID_IFACE_INDEX  -4
30 #define SID_CREATE_INVALID_ADJ_INDEX    -5
31
32 unsigned char function_name[] = "SRv6-AD-plugin";
33 unsigned char keyword_str[] = "End.AD";
34 unsigned char def_str[] =
35   "Endpoint with dynamic proxy to SR-unaware appliance";
36 unsigned char params_str[] = "nh <next-hop> oif <iface-out> iif <iface-in>";
37
38 srv6_ad_main_t srv6_ad_main;
39
40 /*****************************************/
41 /* SRv6 LocalSID instantiation and removal functions */
42 static int
43 srv6_ad_localsid_creation_fn (ip6_sr_localsid_t * localsid)
44 {
45   ip6_sr_main_t *srm = &sr_main;
46   srv6_ad_main_t *sm = &srv6_ad_main;
47   srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
48   u32 localsid_index = localsid - srm->localsids;
49
50   /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
51
52   /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
53   adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
54   if (ls_mem->inner_type != AD_TYPE_L2)
55     {
56       if (ls_mem->inner_type == AD_TYPE_IP4)
57         nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
58                                             VNET_LINK_IP4, &ls_mem->nh_addr,
59                                             ls_mem->sw_if_index_out);
60       else if (ls_mem->inner_type == AD_TYPE_IP6)
61         nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
62                                             VNET_LINK_IP6, &ls_mem->nh_addr,
63                                             ls_mem->sw_if_index_out);
64       if (nh_adj_index == ADJ_INDEX_INVALID)
65         {
66           clib_mem_free (ls_mem);
67           return SID_CREATE_INVALID_ADJ_INDEX;
68         }
69     }
70
71   ls_mem->nh_adj = nh_adj_index;
72
73
74   /* Step 2: Prepare inbound policy for packets returning from the VNF */
75
76   /* Sanitise the SW_IF_INDEX */
77   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
78                           ls_mem->sw_if_index_in))
79     {
80       adj_unlock (ls_mem->nh_adj);
81       clib_mem_free (ls_mem);
82       return SID_CREATE_INVALID_IFACE_INDEX;
83     }
84
85   vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
86                                                    ls_mem->sw_if_index_in);
87   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
88     {
89       adj_unlock (ls_mem->nh_adj);
90       clib_mem_free (ls_mem);
91       return SID_CREATE_INVALID_IFACE_TYPE;
92     }
93
94   if (ls_mem->inner_type == AD_TYPE_L2)
95     {
96       /* Enable End.AD2 rewrite node for this interface */
97       int ret =
98         vnet_feature_enable_disable ("device-input", "srv6-ad2-rewrite",
99                                      ls_mem->sw_if_index_in, 1, 0, 0);
100       if (ret != 0)
101         {
102           clib_mem_free (ls_mem);
103           return SID_CREATE_IFACE_FEATURE_ERROR;
104         }
105
106       /* Set interface in promiscuous mode */
107       vnet_main_t *vnm = vnet_get_main ();
108       ethernet_set_flags (vnm, ls_mem->sw_if_index_in,
109                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
110
111       /* Associate local SID index to this interface (resize vector if needed) */
112       if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid2))
113         {
114           vec_resize (sm->sw_iface_localsid2,
115                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
116                        - vec_len (sm->sw_iface_localsid2)));
117         }
118       sm->sw_iface_localsid2[ls_mem->sw_if_index_in] = localsid_index;
119     }
120   else if (ls_mem->inner_type == AD_TYPE_IP4)
121     {
122       /* Enable End.AD4 rewrite node for this interface */
123       int ret =
124         vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
125                                      ls_mem->sw_if_index_in, 1, 0, 0);
126       if (ret != 0)
127         {
128           adj_unlock (ls_mem->nh_adj);
129           clib_mem_free (ls_mem);
130           return SID_CREATE_IFACE_FEATURE_ERROR;
131         }
132
133       /* Associate local SID index to this interface (resize vector if needed) */
134       if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid4))
135         {
136           vec_resize (sm->sw_iface_localsid4,
137                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
138                        - vec_len (sm->sw_iface_localsid4)));
139         }
140       sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
141     }
142   else if (ls_mem->inner_type == AD_TYPE_IP6)
143     {
144       /* Enable End.AD6 rewrite node for this interface */
145       int ret =
146         vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
147                                      ls_mem->sw_if_index_in, 1, 0, 0);
148       if (ret != 0)
149         {
150           adj_unlock (ls_mem->nh_adj);
151           clib_mem_free (ls_mem);
152           return SID_CREATE_IFACE_FEATURE_ERROR;
153         }
154
155       /* Associate local SID index to this interface (resize vector if needed) */
156       if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid6))
157         {
158           vec_resize (sm->sw_iface_localsid6,
159                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
160                        - vec_len (sm->sw_iface_localsid6)));
161         }
162       sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
163     }
164
165   ls_mem->rw_len = 0;
166
167   /* Step 3: Initialize rewrite counters */
168   srv6_ad_localsid_t **ls_p;
169   pool_get (sm->sids, ls_p);
170   *ls_p = ls_mem;
171   ls_mem->index = ls_p - sm->sids;
172
173   vlib_validate_combined_counter (&(sm->valid_counters), ls_mem->index);
174   vlib_validate_combined_counter (&(sm->invalid_counters), ls_mem->index);
175
176   vlib_zero_combined_counter (&(sm->valid_counters), ls_mem->index);
177   vlib_zero_combined_counter (&(sm->invalid_counters), ls_mem->index);
178
179   return 0;
180 }
181
182 static int
183 srv6_ad_localsid_removal_fn (ip6_sr_localsid_t * localsid)
184 {
185   srv6_ad_main_t *sm = &srv6_ad_main;
186   srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
187
188   if (ls_mem->inner_type == AD_TYPE_L2)
189     {
190       /* Disable End.AD2 rewrite node for this interface */
191       int ret =
192         vnet_feature_enable_disable ("device-input", "srv6-ad2-rewrite",
193                                      ls_mem->sw_if_index_in, 0, 0, 0);
194       if (ret != 0)
195         return -1;
196
197       /* Disable promiscuous mode on the interface */
198       vnet_main_t *vnm = vnet_get_main ();
199       ethernet_set_flags (vnm, ls_mem->sw_if_index_in, 0);
200
201       /* Remove local SID index from interface table */
202       sm->sw_iface_localsid2[ls_mem->sw_if_index_in] = ~(u32) 0;
203     }
204   else if (ls_mem->inner_type == AD_TYPE_IP4)
205     {
206       /* Disable End.AD4 rewrite node for this interface */
207       int ret =
208         vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
209                                      ls_mem->sw_if_index_in, 0, 0, 0);
210       if (ret != 0)
211         return -1;
212
213       /* Remove local SID pointer from interface table */
214       sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
215     }
216   else if (ls_mem->inner_type == AD_TYPE_IP6)
217     {
218       /* Disable End.AD6 rewrite node for this interface */
219       int ret =
220         vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
221                                      ls_mem->sw_if_index_in, 0, 0, 0);
222       if (ret != 0)
223         return -1;
224
225       /* Remove local SID pointer from interface table */
226       sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
227     }
228
229
230   /* Unlock (OIF, NHOP) adjacency */
231   adj_unlock (ls_mem->nh_adj);
232
233   /* Delete SID entry */
234   pool_put (sm->sids, pool_elt_at_index (sm->sids, ls_mem->index));
235
236   /* Clean up local SID memory */
237   vec_free (ls_mem->rewrite);
238   clib_mem_free (localsid->plugin_mem);
239
240   return 0;
241 }
242
243 /**********************************/
244 /* SRv6 LocalSID format functions */
245 /*
246  * Prints nicely the parameters of a localsid
247  * Example: print "Table 5"
248  */
249 u8 *
250 format_srv6_ad_localsid (u8 * s, va_list * args)
251 {
252   srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
253
254   vnet_main_t *vnm = vnet_get_main ();
255   srv6_ad_main_t *sm = &srv6_ad_main;
256
257   if (ls_mem->inner_type == AD_TYPE_IP4)
258     {
259       s =
260         format (s, "Next-hop:\t%U\n\t", format_ip4_address,
261                 &ls_mem->nh_addr.ip4);
262     }
263   else if (ls_mem->inner_type == AD_TYPE_IP6)
264     {
265       s =
266         format (s, "Next-hop:\t%U\n\t", format_ip6_address,
267                 &ls_mem->nh_addr.ip6);
268     }
269
270   s = format (s, "Outgoing iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
271               ls_mem->sw_if_index_out);
272   s = format (s, "\tIncoming iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
273               ls_mem->sw_if_index_in);
274
275   vlib_counter_t valid, invalid;
276   vlib_get_combined_counter (&(sm->valid_counters), ls_mem->index, &valid);
277   vlib_get_combined_counter (&(sm->invalid_counters), ls_mem->index,
278                              &invalid);
279   s = format (s, "\tGood rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
280               valid.packets, valid.bytes);
281   s = format (s, "\tBad rewrite traffic:  \t[%Ld packets : %Ld bytes]\n",
282               invalid.packets, invalid.bytes);
283
284   return s;
285 }
286
287 /*
288  * Process the parameters of a localsid
289  * Example: process from:
290  * sr localsid address cafe::1 behavior new_srv6_localsid 5
291  * everything from behavior on... so in this case 'new_srv6_localsid 5'
292  * Notice that it MUST match the keyword_str and params_str defined above.
293  */
294 uword
295 unformat_srv6_ad_localsid (unformat_input_t * input, va_list * args)
296 {
297   void **plugin_mem_p = va_arg (*args, void **);
298   srv6_ad_localsid_t *ls_mem;
299
300   vnet_main_t *vnm = vnet_get_main ();
301
302   u8 inner_type = AD_TYPE_L2;
303   ip46_address_t nh_addr;
304   u32 sw_if_index_out;
305   u32 sw_if_index_in;
306
307   u8 params = 0;
308 #define PARAM_AD_NH   (1 << 0)
309 #define PARAM_AD_OIF  (1 << 1)
310 #define PARAM_AD_IIF  (1 << 2)
311
312   if (!unformat (input, "end.ad"))
313     return 0;
314
315   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
316     {
317       if (!(params & PARAM_AD_NH) && unformat (input, "nh %U",
318                                                unformat_ip4_address,
319                                                &nh_addr.ip4))
320         {
321           inner_type = AD_TYPE_IP4;
322           params |= PARAM_AD_NH;
323         }
324       if (!(params & PARAM_AD_NH) && unformat (input, "nh %U",
325                                                unformat_ip6_address,
326                                                &nh_addr.ip6))
327         {
328           inner_type = AD_TYPE_IP6;
329           params |= PARAM_AD_NH;
330         }
331       else if (!(params & PARAM_AD_OIF) && unformat (input, "oif %U",
332                                                      unformat_vnet_sw_interface,
333                                                      vnm, &sw_if_index_out))
334         {
335           params |= PARAM_AD_OIF;
336         }
337       else if (!(params & PARAM_AD_IIF) && unformat (input, "iif %U",
338                                                      unformat_vnet_sw_interface,
339                                                      vnm, &sw_if_index_in))
340         {
341           params |= PARAM_AD_IIF;
342         }
343       else
344         {
345           break;
346         }
347     }
348
349   /* Make sure that all parameters are supplied */
350   u8 params_chk = (PARAM_AD_OIF | PARAM_AD_IIF);
351   if ((params & params_chk) != params_chk)
352     {
353       return 0;
354     }
355
356   /* Allocate and initialize memory block for local SID parameters */
357   ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
358   clib_memset (ls_mem, 0, sizeof *ls_mem);
359   *plugin_mem_p = ls_mem;
360
361   /* Set local SID parameters */
362   ls_mem->inner_type = inner_type;
363   if (inner_type == AD_TYPE_IP4)
364     ls_mem->nh_addr.ip4 = nh_addr.ip4;
365   else if (inner_type == AD_TYPE_IP6)
366     ls_mem->nh_addr.ip6 = nh_addr.ip6;
367   ls_mem->sw_if_index_out = sw_if_index_out;
368   ls_mem->sw_if_index_in = sw_if_index_in;
369
370   return 1;
371 }
372
373 /*************************/
374 /* SRv6 LocalSID FIB DPO */
375 static u8 *
376 format_srv6_ad_dpo (u8 * s, va_list * args)
377 {
378   index_t index = va_arg (*args, index_t);
379   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
380
381   return (format (s, "SR: dynamic_proxy_index:[%u]", index));
382 }
383
384 void
385 srv6_ad_dpo_lock (dpo_id_t * dpo)
386 {
387 }
388
389 void
390 srv6_ad_dpo_unlock (dpo_id_t * dpo)
391 {
392 }
393
394 const static dpo_vft_t srv6_ad_vft = {
395   .dv_lock = srv6_ad_dpo_lock,
396   .dv_unlock = srv6_ad_dpo_unlock,
397   .dv_format = format_srv6_ad_dpo,
398 };
399
400 const static char *const srv6_ad_ip6_nodes[] = {
401   "srv6-ad-localsid",
402   NULL,
403 };
404
405 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
406   [DPO_PROTO_IP6] = srv6_ad_ip6_nodes,
407 };
408
409 /**********************/
410 static clib_error_t *
411 srv6_ad_init (vlib_main_t * vm)
412 {
413   srv6_ad_main_t *sm = &srv6_ad_main;
414   int rv = 0;
415
416   sm->vlib_main = vm;
417   sm->vnet_main = vnet_get_main ();
418
419   /* Create DPO */
420   sm->srv6_ad_dpo_type = dpo_register_new_type (&srv6_ad_vft, srv6_ad_nodes);
421
422   /* Register SRv6 LocalSID */
423   rv = sr_localsid_register_function (vm,
424                                       function_name,
425                                       keyword_str,
426                                       def_str,
427                                       params_str,
428                                       &sm->srv6_ad_dpo_type,
429                                       format_srv6_ad_localsid,
430                                       unformat_srv6_ad_localsid,
431                                       srv6_ad_localsid_creation_fn,
432                                       srv6_ad_localsid_removal_fn);
433   if (rv < 0)
434     clib_error_return (0, "SRv6 LocalSID function could not be registered.");
435   else
436     sm->srv6_localsid_behavior_id = rv;
437
438   return 0;
439 }
440
441 /* *INDENT-OFF* */
442 VNET_FEATURE_INIT (srv6_ad2_rewrite, static) =
443 {
444   .arc_name = "device-input",
445   .node_name = "srv6-ad2-rewrite",
446   .runs_before = VNET_FEATURES ("ethernet-input"),
447 };
448
449 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
450 {
451   .arc_name = "ip4-unicast",
452   .node_name = "srv6-ad4-rewrite",
453   .runs_before = 0,
454 };
455
456 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
457 {
458   .arc_name = "ip6-unicast",
459   .node_name = "srv6-ad6-rewrite",
460   .runs_before = 0,
461 };
462
463 VLIB_INIT_FUNCTION (srv6_ad_init);
464
465 VLIB_PLUGIN_REGISTER () = {
466   .version = VPP_BUILD_VER,
467   .description = "Dynamic Segment Routing for IPv6 (SRv6) Proxy",
468 };
469 /* *INDENT-ON* */
470
471 /*
472 * fd.io coding-style-patch-verification: ON
473 *
474 * Local Variables:
475 * eval: (c-set-style "gnu")
476 * End:
477 */