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