srv6-as: Adding rewrite counters
[vpp.git] / src / plugins / srv6-as / as.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  * as.c - SRv6 Static Proxy (AS) 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-as/as.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-AS-plugin";
33 unsigned char keyword_str[] = "End.AS";
34 unsigned char def_str[] =
35   "Endpoint with static proxy to SR-unaware appliance";
36 unsigned char params_str[] =
37   "nh <next-hop> oif <iface-out> iif <iface-in> src <src-addr> next <sid> [next <sid> ...]";
38
39
40 static inline u8 *
41 prepare_rewrite (ip6_address_t src_addr, ip6_address_t * sid_list,
42                  u8 protocol)
43 {
44   u8 *rewrite_str = NULL;
45   u32 rewrite_len = IPv6_DEFAULT_HEADER_LENGTH;
46
47   u8 num_sids = vec_len (sid_list);
48   u32 sr_hdr_len = 0;
49
50   if (num_sids > 1)
51     {
52       sr_hdr_len =
53         sizeof (ip6_sr_header_t) + num_sids * sizeof (ip6_address_t);
54       rewrite_len += sr_hdr_len;
55     }
56
57   vec_validate (rewrite_str, rewrite_len - 1);
58
59   /* Fill IP header */
60   ip6_header_t *iph = (ip6_header_t *) rewrite_str;
61   iph->ip_version_traffic_class_and_flow_label =
62     clib_host_to_net_u32 (0 | ((6 & 0xF) << 28));
63   iph->src_address = src_addr;
64   iph->dst_address = sid_list[0];
65   iph->payload_length = sr_hdr_len;
66   iph->hop_limit = IPv6_DEFAULT_HOP_LIMIT;
67
68   if (num_sids > 1)
69     {
70       /* Set Next Header value to Routing Extension */
71       iph->protocol = IP_PROTOCOL_IPV6_ROUTE;
72
73       /* Fill SR header */
74       ip6_sr_header_t *srh = (ip6_sr_header_t *) (iph + 1);
75       srh->protocol = protocol;
76       srh->length = sr_hdr_len / 8 - 1;
77       srh->type = ROUTING_HEADER_TYPE_SR;
78       srh->segments_left = num_sids - 1;
79       srh->first_segment = num_sids - 1;
80       srh->flags = 0x00;
81       srh->reserved = 0x00;
82
83       /* Fill segment list */
84       ip6_address_t *this_address;
85       ip6_address_t *addrp = srh->segments + srh->first_segment;
86       vec_foreach (this_address, sid_list)
87       {
88         *addrp = *this_address;
89         addrp--;
90       }
91     }
92   else
93     {
94       /* Set Next Header value to inner protocol */
95       iph->protocol = protocol;
96     }
97
98   return rewrite_str;
99 }
100
101 static inline void
102 free_ls_mem (srv6_as_localsid_t * ls_mem)
103 {
104   vec_free (ls_mem->rewrite);
105   vec_free (ls_mem->sid_list);
106   clib_mem_free (ls_mem);
107 }
108
109
110 /*****************************************/
111 /* SRv6 LocalSID instantiation and removal functions */
112 static int
113 srv6_as_localsid_creation_fn (ip6_sr_localsid_t * localsid)
114 {
115   ip6_sr_main_t *srm = &sr_main;
116   srv6_as_main_t *sm = &srv6_as_main;
117   srv6_as_localsid_t *ls_mem = localsid->plugin_mem;
118   u32 localsid_index = localsid - srm->localsids;
119
120   /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
121
122   /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
123   adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
124   if (ls_mem->ip_version == DA_IP4)
125     nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
126                                         VNET_LINK_IP4, &ls_mem->nh_addr,
127                                         ls_mem->sw_if_index_out);
128   else if (ls_mem->ip_version == DA_IP6)
129     nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
130                                         VNET_LINK_IP6, &ls_mem->nh_addr,
131                                         ls_mem->sw_if_index_out);
132   if (nh_adj_index == ADJ_INDEX_INVALID)
133     {
134       free_ls_mem (ls_mem);
135       return SID_CREATE_INVALID_ADJ_INDEX;
136     }
137
138   ls_mem->nh_adj = nh_adj_index;
139
140
141   /* Step 2: Prepare inbound policy for packets returning from the VNF */
142
143   /* Make sure the provided incoming interface index is valid */
144   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
145                           ls_mem->sw_if_index_in))
146     {
147       adj_unlock (ls_mem->nh_adj);
148       free_ls_mem (ls_mem);
149       return SID_CREATE_INVALID_IFACE_INDEX;
150     }
151
152   /* Retrieve associated interface structure */
153   vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
154                                                    ls_mem->sw_if_index_in);
155   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
156     {
157       adj_unlock (ls_mem->nh_adj);
158       free_ls_mem (ls_mem);
159       return SID_CREATE_INVALID_IFACE_TYPE;
160     }
161
162   if (ls_mem->ip_version == DA_IP4)
163     {
164       /* Enable End.AS4 rewrite node for this interface */
165       int ret =
166         vnet_feature_enable_disable ("ip4-unicast", "srv6-as4-rewrite",
167                                      ls_mem->sw_if_index_in, 1, 0, 0);
168       if (ret != 0)
169         {
170           adj_unlock (ls_mem->nh_adj);
171           free_ls_mem (ls_mem);
172           return SID_CREATE_IFACE_FEATURE_ERROR;
173         }
174
175       /* Prepare rewrite string */
176       ls_mem->rewrite = prepare_rewrite (ls_mem->src_addr, ls_mem->sid_list,
177                                          IP_PROTOCOL_IP_IN_IP);
178
179       /* Associate local SID index to this interface (resize vector if needed) */
180       if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid4))
181         {
182           vec_resize (sm->sw_iface_localsid4,
183                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
184                        - vec_len (sm->sw_iface_localsid4)));
185         }
186       sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
187     }
188   else if (ls_mem->ip_version == DA_IP6)
189     {
190       /* Enable End.AS6 rewrite node for this interface */
191       int ret =
192         vnet_feature_enable_disable ("ip6-unicast", "srv6-as6-rewrite",
193                                      ls_mem->sw_if_index_in, 1, 0, 0);
194       if (ret != 0)
195         {
196           adj_unlock (ls_mem->nh_adj);
197           free_ls_mem (ls_mem);
198           return SID_CREATE_IFACE_FEATURE_ERROR;
199         }
200
201       /* Prepare rewrite string */
202       ls_mem->rewrite = prepare_rewrite (ls_mem->src_addr, ls_mem->sid_list,
203                                          IP_PROTOCOL_IPV6);
204
205       /* Associate local SID index to this interface (resize vector if needed) */
206       if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid6))
207         {
208           vec_resize (sm->sw_iface_localsid6,
209                       (pool_len (sm->vnet_main->interface_main.sw_interfaces)
210                        - vec_len (sm->sw_iface_localsid6)));
211         }
212       sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
213     }
214
215   /* Step 3: Initialize rewrite counters */
216   srv6_as_localsid_t **ls_p;
217   pool_get (sm->sids, ls_p);
218   *ls_p = ls_mem;
219   ls_mem->index = ls_p - sm->sids;
220
221   vlib_validate_combined_counter (&(sm->valid_counters), ls_mem->index);
222   vlib_validate_combined_counter (&(sm->invalid_counters), ls_mem->index);
223
224   vlib_zero_combined_counter (&(sm->valid_counters), ls_mem->index);
225   vlib_zero_combined_counter (&(sm->invalid_counters), ls_mem->index);
226
227   return 0;
228 }
229
230 static int
231 srv6_as_localsid_removal_fn (ip6_sr_localsid_t * localsid)
232 {
233   srv6_as_main_t *sm = &srv6_as_main;
234   srv6_as_localsid_t *ls_mem = localsid->plugin_mem;
235
236   if (ls_mem->ip_version == DA_IP4)
237     {
238       /* Disable End.AS4 rewrite node for this interface */
239       int ret;
240       ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-as4-rewrite",
241                                          ls_mem->sw_if_index_in, 0, 0, 0);
242       if (ret != 0)
243         return -1;
244
245       /* Remove local SID index from interface table */
246       sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
247     }
248   else if (ls_mem->ip_version == DA_IP6)
249     {
250       /* Disable End.AS6 rewrite node for this interface */
251       int ret;
252       ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-as6-rewrite",
253                                          ls_mem->sw_if_index_in, 0, 0, 0);
254       if (ret != 0)
255         return -1;
256
257       /* Remove local SID index from interface table */
258       sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
259     }
260
261
262   /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
263   adj_unlock (ls_mem->nh_adj);
264
265   /* Delete SID entry */
266   pool_put (sm->sids, pool_elt_at_index (sm->sids, ls_mem->index));
267
268   /* Clean up local SID memory */
269   free_ls_mem (ls_mem);
270
271   return 0;
272 }
273
274 /**********************************/
275 /* SRv6 LocalSID format functions */
276 /*
277  * Prints nicely the parameters of a localsid
278  * Example: print "Table 5"
279  */
280 u8 *
281 format_srv6_as_localsid (u8 * s, va_list * args)
282 {
283   srv6_as_localsid_t *ls_mem = va_arg (*args, void *);
284
285   vnet_main_t *vnm = vnet_get_main ();
286   srv6_as_main_t *sm = &srv6_as_main;
287
288   if (ls_mem->ip_version == DA_IP4)
289     {
290       s =
291         format (s, "Next-hop:\t%U\n", format_ip4_address,
292                 &ls_mem->nh_addr.ip4);
293     }
294   else
295     {
296       s =
297         format (s, "Next-hop:\t%U\n", format_ip6_address,
298                 &ls_mem->nh_addr.ip6);
299     }
300
301   s = format (s, "\tOutgoing iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
302               ls_mem->sw_if_index_out);
303   s = format (s, "\tIncoming iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
304               ls_mem->sw_if_index_in);
305   s = format (s, "\tSource address:\t%U\n", format_ip6_address,
306               &ls_mem->src_addr);
307
308   s = format (s, "\tSegment list:\t< ");
309   ip6_address_t *addr;
310   vec_foreach (addr, ls_mem->sid_list)
311   {
312     s = format (s, "%U, ", format_ip6_address, addr);
313   }
314   s = format (s, "\b\b >\n");
315
316   vlib_counter_t valid, invalid;
317   vlib_get_combined_counter (&(sm->valid_counters), ls_mem->index, &valid);
318   vlib_get_combined_counter (&(sm->invalid_counters), ls_mem->index,
319                              &invalid);
320   s =
321     format (s, "\tGood rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
322             valid.packets, valid.bytes);
323   s =
324     format (s, "\tBad rewrite traffic:  \t[%Ld packets : %Ld bytes]\n",
325             invalid.packets, invalid.bytes);
326
327   return s;
328 }
329
330 /*
331  * Process the parameters of a localsid
332  * Example: process from:
333  * sr localsid address cafe::1 behavior new_srv6_localsid 5
334  * everything from behavior on... so in this case 'new_srv6_localsid 5'
335  * Notice that it MUST match the keyword_str and params_str defined above.
336  */
337 uword
338 unformat_srv6_as_localsid (unformat_input_t * input, va_list * args)
339 {
340   void **plugin_mem_p = va_arg (*args, void **);
341   srv6_as_localsid_t *ls_mem;
342
343   vnet_main_t *vnm = vnet_get_main ();
344
345   u8 ip_version = 0;
346   ip46_address_t nh_addr;
347   u32 sw_if_index_out;
348   u32 sw_if_index_in;
349   ip6_address_t src_addr;
350   ip6_address_t next_sid;
351   ip6_address_t *sid_list = NULL;
352
353   u8 params = 0;
354 #define PARAM_AS_NH   (1 << 0)
355 #define PARAM_AS_OIF  (1 << 1)
356 #define PARAM_AS_IIF  (1 << 2)
357 #define PARAM_AS_SRC  (1 << 3)
358
359   if (!unformat (input, "end.as"))
360     return 0;
361
362   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
363     {
364       if (!(params & PARAM_AS_NH) && unformat (input, "nh %U",
365                                                unformat_ip4_address,
366                                                &nh_addr.ip4))
367         {
368           ip_version = DA_IP4;
369           params |= PARAM_AS_NH;
370         }
371       if (!(params & PARAM_AS_NH) && unformat (input, "nh %U",
372                                                unformat_ip6_address,
373                                                &nh_addr.ip6))
374         {
375           ip_version = DA_IP6;
376           params |= PARAM_AS_NH;
377         }
378       else if (!(params & PARAM_AS_OIF) && unformat (input, "oif %U",
379                                                      unformat_vnet_sw_interface,
380                                                      vnm, &sw_if_index_out))
381         {
382           params |= PARAM_AS_OIF;
383         }
384       else if (!(params & PARAM_AS_IIF) && unformat (input, "iif %U",
385                                                      unformat_vnet_sw_interface,
386                                                      vnm, &sw_if_index_in))
387         {
388           params |= PARAM_AS_IIF;
389         }
390       else if (!(params & PARAM_AS_SRC) && unformat (input, "src %U",
391                                                      unformat_ip6_address,
392                                                      &src_addr))
393         {
394           params |= PARAM_AS_SRC;
395         }
396       else if (unformat (input, "next %U", unformat_ip6_address, &next_sid))
397         {
398           vec_add1 (sid_list, next_sid);
399         }
400       else
401         {
402           break;
403         }
404     }
405
406   /* Make sure that all parameters are supplied */
407   u8 params_chk = (PARAM_AS_NH | PARAM_AS_OIF | PARAM_AS_IIF | PARAM_AS_SRC);
408   if ((params & params_chk) != params_chk || sid_list == NULL)
409     {
410       vec_free (sid_list);
411       return 0;
412     }
413
414   /* Allocate and initialize memory block for local SID parameters */
415   ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
416   memset (ls_mem, 0, sizeof *ls_mem);
417   *plugin_mem_p = ls_mem;
418
419   /* Set local SID parameters */
420   ls_mem->ip_version = ip_version;
421   if (ip_version == DA_IP4)
422     ls_mem->nh_addr.ip4 = nh_addr.ip4;
423   else
424     ls_mem->nh_addr.ip6 = nh_addr.ip6;
425   ls_mem->sw_if_index_out = sw_if_index_out;
426   ls_mem->sw_if_index_in = sw_if_index_in;
427   ls_mem->src_addr = src_addr;
428   ls_mem->sid_list = sid_list;
429
430   return 1;
431 }
432
433 /*************************/
434 /* SRv6 LocalSID FIB DPO */
435 static u8 *
436 format_srv6_as_dpo (u8 * s, va_list * args)
437 {
438   index_t index = va_arg (*args, index_t);
439   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
440
441   return (format (s, "SR: static_proxy_index:[%u]", index));
442 }
443
444 void
445 srv6_as_dpo_lock (dpo_id_t * dpo)
446 {
447 }
448
449 void
450 srv6_as_dpo_unlock (dpo_id_t * dpo)
451 {
452 }
453
454 const static dpo_vft_t srv6_as_vft = {
455   .dv_lock = srv6_as_dpo_lock,
456   .dv_unlock = srv6_as_dpo_unlock,
457   .dv_format = format_srv6_as_dpo,
458 };
459
460 const static char *const srv6_as_ip6_nodes[] = {
461   "srv6-as-localsid",
462   NULL,
463 };
464
465 const static char *const *const srv6_as_nodes[DPO_PROTO_NUM] = {
466   [DPO_PROTO_IP6] = srv6_as_ip6_nodes,
467 };
468
469 /**********************/
470 static clib_error_t *
471 srv6_as_init (vlib_main_t * vm)
472 {
473   srv6_as_main_t *sm = &srv6_as_main;
474   int rv = 0;
475
476   sm->vlib_main = vm;
477   sm->vnet_main = vnet_get_main ();
478
479   /* Create DPO */
480   sm->srv6_as_dpo_type = dpo_register_new_type (&srv6_as_vft, srv6_as_nodes);
481
482   /* Register SRv6 LocalSID */
483   rv = sr_localsid_register_function (vm,
484                                       function_name,
485                                       keyword_str,
486                                       def_str,
487                                       params_str,
488                                       &sm->srv6_as_dpo_type,
489                                       format_srv6_as_localsid,
490                                       unformat_srv6_as_localsid,
491                                       srv6_as_localsid_creation_fn,
492                                       srv6_as_localsid_removal_fn);
493   if (rv < 0)
494     clib_error_return (0, "SRv6 LocalSID function could not be registered.");
495   else
496     sm->srv6_localsid_behavior_id = rv;
497
498   return 0;
499 }
500
501 /* *INDENT-OFF* */
502 VNET_FEATURE_INIT (srv6_as4_rewrite, static) =
503 {
504   .arc_name = "ip4-unicast",
505   .node_name = "srv6-as4-rewrite",
506   .runs_before = 0,
507 };
508
509 VNET_FEATURE_INIT (srv6_as6_rewrite, static) =
510 {
511   .arc_name = "ip6-unicast",
512   .node_name = "srv6-as6-rewrite",
513   .runs_before = 0,
514 };
515
516 VLIB_INIT_FUNCTION (srv6_as_init);
517
518 VLIB_PLUGIN_REGISTER () = {
519   .version = VPP_BUILD_VER,
520   .description = "Static SRv6 proxy",
521 };
522 /* *INDENT-ON* */
523
524 /*
525 * fd.io coding-style-patch-verification: ON
526 *
527 * Local Variables:
528 * eval: (c-set-style "gnu")
529 * End:
530 */