Fixed bugs in SRv6 API
[vpp.git] / src / vnet / srv6 / sr_localsid.c
1 /*
2  * sr_localsid.c: ipv6 segment routing Endpoint behaviors
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * @file
20  * @brief Processing of packets with a SRH
21  *
22  * CLI to define new Segment Routing End processing functions.
23  * Graph node to support such functions.
24  *
25  * Each function associates an SRv6 segment (IPv6 address) with an specific
26  * Segment Routing function.
27  *
28  */
29
30 #include <vlib/vlib.h>
31 #include <vnet/vnet.h>
32 #include <vnet/srv6/sr.h>
33 #include <vnet/ip/ip.h>
34 #include <vnet/srv6/sr_packet.h>
35 #include <vnet/ip/ip6_packet.h>
36 #include <vnet/fib/ip6_fib.h>
37 #include <vnet/dpo/dpo.h>
38 #include <vnet/adj/adj.h>
39
40 #include <vppinfra/error.h>
41 #include <vppinfra/elog.h>
42
43 /**
44  * @brief Dynamically added SR localsid DPO type
45  */
46 static dpo_type_t sr_localsid_dpo_type;
47 static dpo_type_t sr_localsid_d_dpo_type;
48
49 /**
50  * @brief SR localsid add/del
51  *
52  * Function to add or delete SR LocalSIDs.
53  *
54  * @param is_del Boolean of whether its a delete instruction
55  * @param localsid_addr IPv6 address of the localsid
56  * @param is_decap Boolean of whether decapsulation is allowed in this function
57  * @param behavior Type of behavior (function) for this localsid
58  * @param sw_if_index Only for L2/L3 xconnect. OIF. In VRF variant the fib_table.
59  * @param vlan_index Only for L2 xconnect. Outgoing VLAN tag.
60  * @param fib_table  FIB table in which we should install the localsid entry
61  * @param nh_addr Next Hop IPv4/IPv6 address. Only for L2/L3 xconnect.
62  *
63  * @return 0 on success, error otherwise.
64  */
65 int
66 sr_cli_localsid (char is_del, ip6_address_t * localsid_addr,
67                  char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index,
68                  u32 fib_table, ip46_address_t * nh_addr, void *ls_plugin_mem)
69 {
70   ip6_sr_main_t *sm = &sr_main;
71   uword *p;
72   int rv;
73
74   ip6_sr_localsid_t *ls = 0;
75
76   dpo_id_t dpo = DPO_INVALID;
77
78   /* Search for the item */
79   p = mhash_get (&sm->sr_localsids_index_hash, localsid_addr);
80
81   if (p)
82     {
83       if (is_del)
84         {
85           /* Retrieve localsid */
86           ls = pool_elt_at_index (sm->localsids, p[0]);
87           /* Delete FIB entry */
88           fib_prefix_t pfx = {
89             .fp_proto = FIB_PROTOCOL_IP6,
90             .fp_len = 128,
91             .fp_addr = {
92                         .ip6 = *localsid_addr,
93                         }
94           };
95
96           fib_table_entry_delete (fib_table_find (FIB_PROTOCOL_IP6,
97                                                   fib_table),
98                                   &pfx, FIB_SOURCE_SR);
99
100           /* In case it is a Xconnect iface remove the (OIF, NHOP) adj */
101           if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX6
102               || ls->behavior == SR_BEHAVIOR_DX4)
103             adj_unlock (ls->nh_adj);
104
105           if (ls->behavior >= SR_BEHAVIOR_LAST)
106             {
107               sr_localsid_fn_registration_t *plugin = 0;
108               plugin = pool_elt_at_index (sm->plugin_functions,
109                                           ls->behavior - SR_BEHAVIOR_LAST);
110
111               /* Callback plugin removal function */
112               rv = plugin->removal (ls);
113             }
114
115           /* Delete localsid registry */
116           pool_put (sm->localsids, ls);
117           mhash_unset (&sm->sr_localsids_index_hash, localsid_addr, NULL);
118           return 0;
119         }
120       else                      /* create with function already existing; complain */
121         return -1;
122     }
123   else
124     /* delete; localsid does not exist; complain */
125   if (is_del)
126     return -2;
127
128   /* Check whether there exists a FIB entry with such address */
129   fib_prefix_t pfx = {
130     .fp_proto = FIB_PROTOCOL_IP6,
131     .fp_len = 128,
132   };
133
134   pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
135   pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
136
137   /* Lookup the FIB index associated to the table id provided */
138   u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, fib_table);
139   if (fib_index == ~0)
140     return -3;
141
142   /* Lookup the localsid in such FIB table */
143   fib_node_index_t fei = fib_table_lookup_exact_match (fib_index, &pfx);
144   if (FIB_NODE_INDEX_INVALID != fei)
145     return -4;                  //There is an entry for such address (the localsid addr)
146
147   /* Create a new localsid registry */
148   pool_get (sm->localsids, ls);
149   memset (ls, 0, sizeof (*ls));
150
151   clib_memcpy (&ls->localsid, localsid_addr, sizeof (ip6_address_t));
152   ls->end_psp = end_psp;
153   ls->behavior = behavior;
154   ls->nh_adj = (u32) ~ 0;
155   ls->fib_table = fib_table;
156   switch (behavior)
157     {
158     case SR_BEHAVIOR_END:
159       break;
160     case SR_BEHAVIOR_X:
161       ls->sw_if_index = sw_if_index;
162       clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
163       break;
164     case SR_BEHAVIOR_T:
165       ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
166       break;
167     case SR_BEHAVIOR_DX4:
168       ls->sw_if_index = sw_if_index;
169       clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
170       break;
171     case SR_BEHAVIOR_DX6:
172       ls->sw_if_index = sw_if_index;
173       clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
174       break;
175     case SR_BEHAVIOR_DT6:
176       ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
177       break;
178     case SR_BEHAVIOR_DT4:
179       ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP4, sw_if_index);
180       break;
181     case SR_BEHAVIOR_DX2:
182       ls->sw_if_index = sw_if_index;
183       ls->vlan_index = vlan_index;
184       break;
185     }
186
187   /* Figure out the adjacency magic for Xconnect variants */
188   if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
189       || ls->behavior == SR_BEHAVIOR_DX6)
190     {
191       adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
192
193       /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
194       if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
195         nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
196                                             nh_addr, sw_if_index);
197
198       else if (ls->behavior == SR_BEHAVIOR_DX4)
199         nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
200                                             nh_addr, sw_if_index);
201
202       /* Check for ADJ creation error. If so panic */
203       if (nh_adj_index == ADJ_INDEX_INVALID)
204         {
205           pool_put (sm->localsids, ls);
206           return -5;
207         }
208
209       ls->nh_adj = nh_adj_index;
210     }
211
212   /* Set DPO */
213   if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X)
214     dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
215   else if (ls->behavior > SR_BEHAVIOR_D_FIRST
216            && ls->behavior < SR_BEHAVIOR_LAST)
217     dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
218   else if (ls->behavior >= SR_BEHAVIOR_LAST)
219     {
220       sr_localsid_fn_registration_t *plugin = 0;
221       plugin = pool_elt_at_index (sm->plugin_functions,
222                                   ls->behavior - SR_BEHAVIOR_LAST);
223       /* Copy the unformat memory result */
224       ls->plugin_mem = ls_plugin_mem;
225       /* Callback plugin creation function */
226       rv = plugin->creation (ls);
227       if (rv)
228         {
229           pool_put (sm->localsids, ls);
230           return -6;
231         }
232       dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
233     }
234
235   /* Set hash key for searching localsid by address */
236   mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
237              NULL);
238
239   fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
240                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
241   dpo_reset (&dpo);
242
243   /* Set counter to zero */
244   vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
245                                   ls - sm->localsids);
246   vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
247                                   ls - sm->localsids);
248
249   vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
250                               ls - sm->localsids);
251   vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
252                               ls - sm->localsids);
253
254   return 0;
255 }
256
257 /**
258  * @brief SR LocalSID CLI function.
259  *
260  * @see sr_cli_localsid
261  */
262 static clib_error_t *
263 sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
264                             vlib_cli_command_t * cmd)
265 {
266   vnet_main_t *vnm = vnet_get_main ();
267   ip6_sr_main_t *sm = &sr_main;
268   u32 sw_if_index = (u32) ~ 0, vlan_index = (u32) ~ 0, fib_index = 0;
269   int is_del = 0;
270   int end_psp = 0;
271   ip6_address_t resulting_address;
272   ip46_address_t next_hop;
273   char address_set = 0;
274   char behavior = 0;
275   void *ls_plugin_mem = 0;
276
277   int rv;
278
279   memset (&resulting_address, 0, sizeof (ip6_address_t));
280   ip46_address_reset (&next_hop);
281
282   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
283     {
284       if (unformat (input, "del"))
285         is_del = 1;
286       else if (!address_set
287                && unformat (input, "address %U", unformat_ip6_address,
288                             &resulting_address))
289         address_set = 1;
290       else if (!address_set
291                && unformat (input, "addr %U", unformat_ip6_address,
292                             &resulting_address))
293         address_set = 1;
294       else if (unformat (input, "fib-table %u", &fib_index));
295       else if (vlan_index == (u32) ~ 0
296                && unformat (input, "vlan %u", &vlan_index));
297       else if (!behavior && unformat (input, "behavior"))
298         {
299           if (unformat (input, "end.x %U %U",
300                         unformat_vnet_sw_interface, vnm, &sw_if_index,
301                         unformat_ip6_address, &next_hop.ip6))
302             behavior = SR_BEHAVIOR_X;
303           else if (unformat (input, "end.t %u", &sw_if_index))
304             behavior = SR_BEHAVIOR_T;
305           else if (unformat (input, "end.dx6 %U %U",
306                              unformat_vnet_sw_interface, vnm, &sw_if_index,
307                              unformat_ip6_address, &next_hop.ip6))
308             behavior = SR_BEHAVIOR_DX6;
309           else if (unformat (input, "end.dx4 %U %U",
310                              unformat_vnet_sw_interface, vnm, &sw_if_index,
311                              unformat_ip4_address, &next_hop.ip4))
312             behavior = SR_BEHAVIOR_DX4;
313           else if (unformat (input, "end.dx2 %U",
314                              unformat_vnet_sw_interface, vnm, &sw_if_index))
315             behavior = SR_BEHAVIOR_DX2;
316           else if (unformat (input, "end.dt6 %u", &sw_if_index))
317             behavior = SR_BEHAVIOR_DT6;
318           else if (unformat (input, "end.dt4 %u", &sw_if_index))
319             behavior = SR_BEHAVIOR_DT4;
320           else
321             {
322               /* Loop over all the plugin behavior format functions */
323               sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
324               sr_localsid_fn_registration_t **plugin_it = 0;
325
326               /* Create a vector out of the plugin pool as recommended */
327         /* *INDENT-OFF* */
328         pool_foreach (plugin, sm->plugin_functions,
329         {
330           vec_add1 (vec_plugins, plugin);
331         });
332         /* *INDENT-ON* */
333
334               vec_foreach (plugin_it, vec_plugins)
335               {
336                 if (unformat
337                     (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
338                   {
339                     behavior = (*plugin_it)->sr_localsid_function_number;
340                     break;
341                   }
342               }
343             }
344
345           if (!behavior)
346             {
347               if (unformat (input, "end"))
348                 behavior = SR_BEHAVIOR_END;
349               else
350                 break;
351             }
352         }
353       else if (!end_psp && unformat (input, "psp"))
354         end_psp = 1;
355       else
356         break;
357     }
358
359   if (!behavior && end_psp)
360     behavior = SR_BEHAVIOR_END;
361
362   if (!address_set)
363     return clib_error_return (0,
364                               "Error: SRv6 LocalSID address is mandatory.");
365   if (!is_del && !behavior)
366     return clib_error_return (0,
367                               "Error: SRv6 LocalSID behavior is mandatory.");
368   if (vlan_index != (u32) ~ 0)
369     return clib_error_return (0,
370                               "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
371   if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
372     return clib_error_return (0,
373                               "Error: SRv6 PSP only compatible with End and End.X");
374
375   rv = sr_cli_localsid (is_del, &resulting_address, end_psp, behavior,
376                         sw_if_index, vlan_index, fib_index, &next_hop,
377                         ls_plugin_mem);
378
379   switch (rv)
380     {
381     case 0:
382       break;
383     case 1:
384       return 0;
385     case -1:
386       return clib_error_return (0,
387                                 "Identical localsid already exists. Requested localsid not created.");
388     case -2:
389       return clib_error_return (0,
390                                 "The requested localsid could not be deleted. SR localsid not found");
391     case -3:
392       return clib_error_return (0, "FIB table %u does not exist", fib_index);
393     case -4:
394       return clib_error_return (0, "There is already one FIB entry for the"
395                                 "requested localsid non segment routing related");
396     case -5:
397       return clib_error_return (0,
398                                 "Could not create ARP/ND entry for such next_hop. Internal error.");
399     case -6:
400       return clib_error_return (0,
401                                 "Error on the plugin based localsid creation.");
402     default:
403       return clib_error_return (0, "BUG: sr localsid returns %d", rv);
404     }
405   return 0;
406 }
407
408 /* *INDENT-OFF* */
409 VLIB_CLI_COMMAND (sr_localsid_command, static) = {
410   .path = "sr localsid",
411   .short_help = "sr localsid (del) address XX:XX::YY:YY"
412       "(fib-table 8) behavior STRING",
413   .long_help =
414     "Create SR LocalSID and binds it to a particular behavior\n"
415     "Arguments:\n"
416     "\tlocalSID IPv6_addr(128b)   LocalSID IPv6 address\n"
417     "\t(fib-table X)              Optional. VRF where to install SRv6 localsid\n"
418     "\tbehavior STRING            Specifies the behavior\n"
419     "\n\tBehaviors:\n"
420     "\tEnd\t-> Endpoint.\n"
421     "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
422     "\t\tParameters: '<iface> <ip6_next_hop>'\n"
423     "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
424     "\t\tParameters: '<iface>'\n"
425     "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
426     "\t\tParameters: '<iface> <ip6_next_hop>'\n"
427     "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
428     "\t\tParameters: '<iface> <ip4_next_hop>'\n"
429     "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
430     "\t\tParameters: '<ip6_fib_table>'\n"
431     "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
432     "\t\tParameters: '<ip4_fib_table>'\n",
433   .function = sr_cli_localsid_command_fn,
434 };
435 /* *INDENT-ON* */
436
437 /**
438  * @brief CLI function to 'show' all SR LocalSIDs on console.
439  */
440 static clib_error_t *
441 show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
442                              vlib_cli_command_t * cmd)
443 {
444   vnet_main_t *vnm = vnet_get_main ();
445   ip6_sr_main_t *sm = &sr_main;
446   ip6_sr_localsid_t **localsid_list = 0;
447   ip6_sr_localsid_t *ls;
448   int i;
449
450   vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
451   vlib_cli_output (vm, "=========================");
452   /* *INDENT-OFF* */
453   pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
454   /* *INDENT-ON* */
455   for (i = 0; i < vec_len (localsid_list); i++)
456     {
457       ls = localsid_list[i];
458       switch (ls->behavior)
459         {
460         case SR_BEHAVIOR_END:
461           vlib_cli_output (vm, "\tAddress: \t%U\n\tBehavior: \tEnd",
462                            format_ip6_address, &ls->localsid);
463           break;
464         case SR_BEHAVIOR_X:
465           vlib_cli_output (vm,
466                            "\tAddress: \t%U\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
467                            "\n\tIface:  \t%U\n\tNext hop: \t%U",
468                            format_ip6_address, &ls->localsid,
469                            format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
470                            format_ip6_address, &ls->next_hop.ip6);
471           break;
472         case SR_BEHAVIOR_T:
473           vlib_cli_output (vm,
474                            "\tAddress: \t%U\n\tBehavior: \tT (Endpoint with specific IPv6 table lookup)"
475                            "\n\tTable:  \t%u",
476                            format_ip6_address, &ls->localsid, ls->vrf_index);
477           break;
478         case SR_BEHAVIOR_DX4:
479           vlib_cli_output (vm,
480                            "\tAddress: \t%U\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
481                            "\n\tIface:  \t%U\n\tNext hop: \t%U",
482                            format_ip6_address, &ls->localsid,
483                            format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
484                            format_ip4_address, &ls->next_hop.ip4);
485           break;
486         case SR_BEHAVIOR_DX6:
487           vlib_cli_output (vm,
488                            "\tAddress: \t%U\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
489                            "\n\tIface:  \t%U\n\tNext hop: \t%U",
490                            format_ip6_address, &ls->localsid,
491                            format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
492                            format_ip6_address, &ls->next_hop.ip6);
493           break;
494         case SR_BEHAVIOR_DX2:
495           if (ls->vlan_index == (u32) ~ 0)
496             vlib_cli_output (vm,
497                              "\tAddress: \t%U\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
498                              "\n\tIface:  \t%U", format_ip6_address,
499                              &ls->localsid, format_vnet_sw_if_index_name, vnm,
500                              ls->sw_if_index);
501           else
502             vlib_cli_output (vm,
503                              "Unsupported yet. (DX2 with egress VLAN rewrite)");
504           break;
505         case SR_BEHAVIOR_DT6:
506           vlib_cli_output (vm,
507                            "\tAddress: \t%U\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
508                            "\n\tTable: %u", format_ip6_address, &ls->localsid,
509                            ls->vrf_index);
510           break;
511         case SR_BEHAVIOR_DT4:
512           vlib_cli_output (vm,
513                            "\tAddress: \t%U\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
514                            "\n\tTable: \t%u", format_ip6_address,
515                            &ls->localsid, ls->vrf_index);
516           break;
517         default:
518           if (ls->behavior >= SR_BEHAVIOR_LAST)
519             {
520               sr_localsid_fn_registration_t *plugin =
521                 pool_elt_at_index (sm->plugin_functions,
522                                    ls->behavior - SR_BEHAVIOR_LAST);
523
524               vlib_cli_output (vm, "\tAddress: \t%U\n"
525                                "\tBehavior: \t%s (%s)\n\t%U",
526                                format_ip6_address, &ls->localsid,
527                                plugin->keyword_str, plugin->def_str,
528                                plugin->ls_format, ls->plugin_mem);
529             }
530           else
531             //Should never get here...
532             vlib_cli_output (vm, "Internal error");
533           break;
534         }
535       if (ls->end_psp)
536         vlib_cli_output (vm, "\tPSP: \tTrue\n");
537
538       /* Print counters */
539       vlib_counter_t valid, invalid;
540       vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
541       vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
542       vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
543                        valid.packets, valid.bytes);
544       vlib_cli_output (vm, "\tBad traffic:  \t[%Ld packets : %Ld bytes]\n",
545                        invalid.packets, invalid.bytes);
546       vlib_cli_output (vm, "--------------------");
547     }
548   return 0;
549 }
550
551 /* *INDENT-OFF* */
552 VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
553   .path = "show sr localsids",
554   .short_help = "show sr localsids",
555   .function = show_sr_localsid_command_fn,
556 };
557 /* *INDENT-ON* */
558
559 /**
560  * @brief Function to 'clear' ALL SR localsid counters
561  */
562 static clib_error_t *
563 clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
564                                        unformat_input_t * input,
565                                        vlib_cli_command_t * cmd)
566 {
567   ip6_sr_main_t *sm = &sr_main;
568
569   vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
570   vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
571
572   return 0;
573 }
574
575 /* *INDENT-OFF* */
576 VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
577   .path = "clear sr localsid-counters",
578   .short_help = "clear sr localsid-counters",
579   .function = clear_sr_localsid_counters_command_fn,
580 };
581 /* *INDENT-ON* */
582
583 /************************ SR LocalSID graphs node ****************************/
584 /**
585  * @brief SR localsid node trace
586  */
587 typedef struct
588 {
589   ip6_address_t localsid;
590   u16 behavior;
591   u8 sr[256];
592   u8 num_segments;
593   u8 segments_left;
594 } sr_localsid_trace_t;
595
596 #define foreach_sr_localsid_error                                   \
597 _(NO_INNER_HEADER, "(SR-Error) No inner IP header")                 \
598 _(NO_MORE_SEGMENTS, "(SR-Error) No more segments")                  \
599 _(NO_SRH, "(SR-Error) No SR header")                                \
600 _(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)")       \
601 _(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)")    \
602 _(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
603
604 typedef enum
605 {
606 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
607   foreach_sr_localsid_error
608 #undef _
609     SR_LOCALSID_N_ERROR,
610 } sr_localsid_error_t;
611
612 static char *sr_localsid_error_strings[] = {
613 #define _(sym,string) string,
614   foreach_sr_localsid_error
615 #undef _
616 };
617
618 #define foreach_sr_localsid_next        \
619 _(ERROR, "error-drop")                  \
620 _(IP6_LOOKUP, "ip6-lookup")             \
621 _(IP4_LOOKUP, "ip4-lookup")             \
622 _(IP6_REWRITE, "ip6-rewrite")           \
623 _(IP4_REWRITE, "ip4-rewrite")           \
624 _(INTERFACE_OUTPUT, "interface-output")
625
626 typedef enum
627 {
628 #define _(s,n) SR_LOCALSID_NEXT_##s,
629   foreach_sr_localsid_next
630 #undef _
631     SR_LOCALSID_N_NEXT,
632 } sr_localsid_next_t;
633
634 /**
635  * @brief SR LocalSID graph node trace function
636  *
637  * @see sr_localsid
638  */
639 u8 *
640 format_sr_localsid_trace (u8 * s, va_list * args)
641 {
642   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
643   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
644   sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
645
646   s =
647     format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
648             &t->localsid);
649   switch (t->behavior)
650     {
651     case SR_BEHAVIOR_END:
652       s = format (s, "\tBehavior: End\n");
653       break;
654     case SR_BEHAVIOR_DX6:
655       s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
656       break;
657     case SR_BEHAVIOR_DX4:
658       s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
659       break;
660     case SR_BEHAVIOR_X:
661       s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
662       break;
663     case SR_BEHAVIOR_T:
664       s = format (s, "\tBehavior: IPv6 specific table lookup\n");
665       break;
666     case SR_BEHAVIOR_DT6:
667       s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
668       break;
669     case SR_BEHAVIOR_DT4:
670       s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
671       break;
672     case SR_BEHAVIOR_DX2:
673       s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
674       break;
675     default:
676       s = format (s, "\tBehavior: defined in plugin\n");        //TODO
677       break;
678     }
679   if (t->num_segments != 0xFF)
680     {
681       if (t->num_segments > 0)
682         {
683           s = format (s, "\tSegments left: %d\n", t->segments_left);
684           s = format (s, "\tSID list: [in ietf order]");
685           int i = 0;
686           for (i = 0; i < t->num_segments; i++)
687             {
688               s = format (s, "\n\t-> %U", format_ip6_address,
689                           (ip6_address_t *) & t->sr[i *
690                                                     sizeof (ip6_address_t)]);
691             }
692         }
693     }
694   return s;
695 }
696
697 /**
698  * @brief Function doing End processing.
699  */
700 static_always_inline void
701 end_srh_processing (vlib_node_runtime_t * node,
702                     vlib_buffer_t * b0,
703                     ip6_header_t * ip0,
704                     ip6_sr_header_t * sr0,
705                     ip6_sr_localsid_t * ls0,
706                     u32 * next0, u8 psp, ip6_ext_header_t * prev0)
707 {
708   ip6_address_t *new_dst0;
709
710   if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
711     {
712       if (sr0->segments_left == 1 && psp)
713         {
714           u32 new_l0, sr_len;
715           u64 *copy_dst0, *copy_src0;
716           u32 copy_len_u64s0 = 0;
717
718           ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
719           ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
720
721           /* Remove the SRH taking care of the rest of IPv6 ext header */
722           if (prev0)
723             prev0->next_hdr = sr0->protocol;
724           else
725             ip0->protocol = sr0->protocol;
726
727           sr_len = ip6_ext_header_len (sr0);
728           vlib_buffer_advance (b0, sr_len);
729           new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
730           ip0->payload_length = clib_host_to_net_u16 (new_l0);
731           copy_src0 = (u64 *) ip0;
732           copy_dst0 = copy_src0 + (sr0->length + 1);
733           /* number of 8 octet units to copy
734            * By default in absence of extension headers it is equal to length of ip6 header
735            * With extension headers it number of 8 octet units of ext headers preceding
736            * SR header
737            */
738           copy_len_u64s0 =
739             (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
740           copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
741           copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
742           copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
743           copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
744           copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
745
746           int i;
747           for (i = copy_len_u64s0 - 1; i >= 0; i--)
748             {
749               copy_dst0[i] = copy_src0[i];
750             }
751
752           if (ls0->behavior == SR_BEHAVIOR_X)
753             {
754               vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
755               *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
756             }
757           else if (ls0->behavior == SR_BEHAVIOR_T)
758             {
759               vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
760             }
761         }
762       else if (PREDICT_TRUE (sr0->segments_left > 0))
763         {
764           sr0->segments_left -= 1;
765           new_dst0 = (ip6_address_t *) (sr0->segments);
766           new_dst0 += sr0->segments_left;
767           ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
768           ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
769
770           if (ls0->behavior == SR_BEHAVIOR_X)
771             {
772               vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
773               *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
774             }
775           else if (ls0->behavior == SR_BEHAVIOR_T)
776             {
777               vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
778             }
779         }
780       else
781         {
782           *next0 = SR_LOCALSID_NEXT_ERROR;
783           b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
784         }
785     }
786   else
787     {
788       /* Error. Routing header of type != SR */
789       *next0 = SR_LOCALSID_NEXT_ERROR;
790       b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
791     }
792 }
793
794 /*
795  * @brief Function doing SRH processing for D* variants
796  */
797 static_always_inline void
798 end_decaps_srh_processing (vlib_node_runtime_t * node,
799                            vlib_buffer_t * b0,
800                            ip6_header_t * ip0,
801                            ip6_sr_header_t * sr0,
802                            ip6_sr_localsid_t * ls0, u32 * next0)
803 {
804   /* Compute the size of the IPv6 header with all Ext. headers */
805   u8 next_proto;
806   ip6_ext_header_t *next_ext_header;
807   u16 total_size = 0;
808
809   next_proto = ip0->protocol;
810   next_ext_header = (void *) (ip0 + 1);
811   total_size = sizeof (ip6_header_t);
812   while (ip6_ext_hdr (next_proto))
813     {
814       total_size += ip6_ext_header_len (next_ext_header);
815       next_proto = next_ext_header->next_hdr;
816       next_ext_header = ip6_ext_next_header (next_ext_header);
817     }
818
819   /* Ensure this is the last segment. Otherwise drop. */
820   if (sr0 && sr0->segments_left != 0)
821     {
822       *next0 = SR_LOCALSID_NEXT_ERROR;
823       b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
824       return;
825     }
826
827   switch (next_proto)
828     {
829     case IP_PROTOCOL_IPV6:
830       /* Encap-End IPv6. Pop outer IPv6 header. */
831       if (ls0->behavior == SR_BEHAVIOR_DX6)
832         {
833           vlib_buffer_advance (b0, total_size);
834           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
835           *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
836           return;
837         }
838       else if (ls0->behavior == SR_BEHAVIOR_DT6)
839         {
840           vlib_buffer_advance (b0, total_size);
841           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
842           return;
843         }
844       break;
845     case IP_PROTOCOL_IP_IN_IP:
846       /* Encap-End IPv4. Pop outer IPv6 header */
847       if (ls0->behavior == SR_BEHAVIOR_DX4)
848         {
849           vlib_buffer_advance (b0, total_size);
850           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
851           *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
852           return;
853         }
854       else if (ls0->behavior == SR_BEHAVIOR_DT4)
855         {
856           vlib_buffer_advance (b0, total_size);
857           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
858           *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
859           return;
860         }
861       break;
862     case IP_PROTOCOL_IP6_NONXT:
863       /* L2 encaps */
864       if (ls0->behavior == SR_BEHAVIOR_DX2)
865         {
866           vlib_buffer_advance (b0, total_size);
867           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
868           *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
869           return;
870         }
871       break;
872     }
873   *next0 = SR_LOCALSID_NEXT_ERROR;
874   b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
875   return;
876 }
877
878 /**
879  * @brief SR LocalSID graph node. Supports all default SR Endpoint variants with decaps
880  */
881 static uword
882 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
883                   vlib_frame_t * from_frame)
884 {
885   u32 n_left_from, next_index, *from, *to_next;
886   ip6_sr_main_t *sm = &sr_main;
887   from = vlib_frame_vector_args (from_frame);
888   n_left_from = from_frame->n_vectors;
889   next_index = node->cached_next_index;
890   u32 thread_index = vlib_get_thread_index ();
891
892   while (n_left_from > 0)
893     {
894       u32 n_left_to_next;
895       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
896
897       /* Quad - Loop */
898       while (n_left_from >= 8 && n_left_to_next >= 4)
899         {
900           u32 bi0, bi1, bi2, bi3;
901           vlib_buffer_t *b0, *b1, *b2, *b3;
902           ip6_header_t *ip0, *ip1, *ip2, *ip3;
903           ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
904           ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
905           u32 next0, next1, next2, next3;
906           next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
907           ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
908
909           /* Prefetch next iteration. */
910           {
911             vlib_buffer_t *p4, *p5, *p6, *p7;
912
913             p4 = vlib_get_buffer (vm, from[4]);
914             p5 = vlib_get_buffer (vm, from[5]);
915             p6 = vlib_get_buffer (vm, from[6]);
916             p7 = vlib_get_buffer (vm, from[7]);
917
918             /* Prefetch the buffer header and packet for the N+4 loop iteration */
919             vlib_prefetch_buffer_header (p4, LOAD);
920             vlib_prefetch_buffer_header (p5, LOAD);
921             vlib_prefetch_buffer_header (p6, LOAD);
922             vlib_prefetch_buffer_header (p7, LOAD);
923
924             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
925             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
926             CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
927             CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
928           }
929
930           to_next[0] = bi0 = from[0];
931           to_next[1] = bi1 = from[1];
932           to_next[2] = bi2 = from[2];
933           to_next[3] = bi3 = from[3];
934           from += 4;
935           to_next += 4;
936           n_left_from -= 4;
937           n_left_to_next -= 4;
938
939           b0 = vlib_get_buffer (vm, bi0);
940           b1 = vlib_get_buffer (vm, bi1);
941           b2 = vlib_get_buffer (vm, bi2);
942           b3 = vlib_get_buffer (vm, bi3);
943
944           ls0 =
945             pool_elt_at_index (sm->localsids,
946                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
947           ls1 =
948             pool_elt_at_index (sm->localsids,
949                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
950           ls2 =
951             pool_elt_at_index (sm->localsids,
952                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
953           ls3 =
954             pool_elt_at_index (sm->localsids,
955                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
956
957           ip0 = vlib_buffer_get_current (b0);
958           ip1 = vlib_buffer_get_current (b1);
959           ip2 = vlib_buffer_get_current (b2);
960           ip3 = vlib_buffer_get_current (b3);
961
962           ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
963           ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
964           ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
965           ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
966
967           end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
968           end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
969           end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
970           end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
971
972           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
973             {
974               sr_localsid_trace_t *tr =
975                 vlib_add_trace (vm, node, b0, sizeof (*tr));
976               tr->num_segments = 0;
977               clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
978                            sizeof (tr->localsid.as_u8));
979               tr->behavior = ls0->behavior;
980               if (ip0 == vlib_buffer_get_current (b0))
981                 {
982                   if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
983                       && sr0->type == ROUTING_HEADER_TYPE_SR)
984                     {
985                       clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
986                       tr->num_segments =
987                         sr0->length * 8 / sizeof (ip6_address_t);
988                       tr->segments_left = sr0->segments_left;
989                     }
990                 }
991               else
992                 tr->num_segments = 0xFF;
993             }
994
995           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
996             {
997               sr_localsid_trace_t *tr =
998                 vlib_add_trace (vm, node, b1, sizeof (*tr));
999               tr->num_segments = 0;
1000               clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1001                            sizeof (tr->localsid.as_u8));
1002               tr->behavior = ls1->behavior;
1003               if (ip1 == vlib_buffer_get_current (b1))
1004                 {
1005                   if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1006                       && sr1->type == ROUTING_HEADER_TYPE_SR)
1007                     {
1008                       clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1009                       tr->num_segments =
1010                         sr1->length * 8 / sizeof (ip6_address_t);
1011                       tr->segments_left = sr1->segments_left;
1012                     }
1013                 }
1014               else
1015                 tr->num_segments = 0xFF;
1016             }
1017
1018           if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1019             {
1020               sr_localsid_trace_t *tr =
1021                 vlib_add_trace (vm, node, b2, sizeof (*tr));
1022               tr->num_segments = 0;
1023               clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1024                            sizeof (tr->localsid.as_u8));
1025               tr->behavior = ls2->behavior;
1026               if (ip2 == vlib_buffer_get_current (b2))
1027                 {
1028                   if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1029                       && sr2->type == ROUTING_HEADER_TYPE_SR)
1030                     {
1031                       clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1032                       tr->num_segments =
1033                         sr2->length * 8 / sizeof (ip6_address_t);
1034                       tr->segments_left = sr2->segments_left;
1035                     }
1036                 }
1037               else
1038                 tr->num_segments = 0xFF;
1039             }
1040
1041           if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1042             {
1043               sr_localsid_trace_t *tr =
1044                 vlib_add_trace (vm, node, b3, sizeof (*tr));
1045               tr->num_segments = 0;
1046               clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1047                            sizeof (tr->localsid.as_u8));
1048               tr->behavior = ls3->behavior;
1049               if (ip3 == vlib_buffer_get_current (b3))
1050                 {
1051                   if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1052                       && sr3->type == ROUTING_HEADER_TYPE_SR)
1053                     {
1054                       clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1055                       tr->num_segments =
1056                         sr3->length * 8 / sizeof (ip6_address_t);
1057                       tr->segments_left = sr3->segments_left;
1058                     }
1059                 }
1060               else
1061                 tr->num_segments = 0xFF;
1062             }
1063
1064           vlib_increment_combined_counter
1065             (((next0 ==
1066                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1067               &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1068              1, vlib_buffer_length_in_chain (vm, b0));
1069
1070           vlib_increment_combined_counter
1071             (((next1 ==
1072                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1073               &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1074              1, vlib_buffer_length_in_chain (vm, b1));
1075
1076           vlib_increment_combined_counter
1077             (((next2 ==
1078                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1079               &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1080              1, vlib_buffer_length_in_chain (vm, b2));
1081
1082           vlib_increment_combined_counter
1083             (((next3 ==
1084                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1085               &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1086              1, vlib_buffer_length_in_chain (vm, b3));
1087
1088           vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1089                                            n_left_to_next, bi0, bi1, bi2, bi3,
1090                                            next0, next1, next2, next3);
1091         }
1092
1093       /* Single loop for potentially the last three packets */
1094       while (n_left_from > 0 && n_left_to_next > 0)
1095         {
1096           u32 bi0;
1097           vlib_buffer_t *b0;
1098           ip6_header_t *ip0;
1099           ip6_ext_header_t *prev0;
1100           ip6_sr_header_t *sr0;
1101           u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1102           ip6_sr_localsid_t *ls0;
1103
1104           bi0 = from[0];
1105           to_next[0] = bi0;
1106           from += 1;
1107           to_next += 1;
1108           n_left_from -= 1;
1109           n_left_to_next -= 1;
1110
1111           b0 = vlib_get_buffer (vm, bi0);
1112           ip0 = vlib_buffer_get_current (b0);
1113
1114           /* Lookup the SR End behavior based on IP DA (adj) */
1115           ls0 =
1116             pool_elt_at_index (sm->localsids,
1117                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1118
1119           /* Find SRH as well as previous header */
1120           ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1121
1122           /* SRH processing and End variants */
1123           end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1124
1125           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1126             {
1127               sr_localsid_trace_t *tr =
1128                 vlib_add_trace (vm, node, b0, sizeof (*tr));
1129               tr->num_segments = 0;
1130               clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1131                            sizeof (tr->localsid.as_u8));
1132               tr->behavior = ls0->behavior;
1133               if (ip0 == vlib_buffer_get_current (b0))
1134                 {
1135                   if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1136                       && sr0->type == ROUTING_HEADER_TYPE_SR)
1137                     {
1138                       clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1139                       tr->num_segments =
1140                         sr0->length * 8 / sizeof (ip6_address_t);
1141                       tr->segments_left = sr0->segments_left;
1142                     }
1143                 }
1144               else
1145                 tr->num_segments = 0xFF;
1146             }
1147
1148           /* Increase the counters */
1149           vlib_increment_combined_counter
1150             (((next0 ==
1151                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1152               &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1153              1, vlib_buffer_length_in_chain (vm, b0));
1154
1155           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1156                                            n_left_to_next, bi0, next0);
1157         }
1158       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1159     }
1160   return from_frame->n_vectors;
1161 }
1162
1163 /* *INDENT-OFF* */
1164 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1165   .function = sr_localsid_d_fn,
1166   .name = "sr-localsid-d",
1167   .vector_size = sizeof (u32),
1168   .format_trace = format_sr_localsid_trace,
1169   .type = VLIB_NODE_TYPE_INTERNAL,
1170   .n_errors = SR_LOCALSID_N_ERROR,
1171   .error_strings = sr_localsid_error_strings,
1172   .n_next_nodes = SR_LOCALSID_N_NEXT,
1173   .next_nodes = {
1174 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1175     foreach_sr_localsid_next
1176 #undef _
1177   },
1178 };
1179 /* *INDENT-ON* */
1180
1181 /**
1182  * @brief SR LocalSID graph node. Supports all default SR Endpoint without decaps
1183  */
1184 static uword
1185 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1186                 vlib_frame_t * from_frame)
1187 {
1188   u32 n_left_from, next_index, *from, *to_next;
1189   ip6_sr_main_t *sm = &sr_main;
1190   from = vlib_frame_vector_args (from_frame);
1191   n_left_from = from_frame->n_vectors;
1192   next_index = node->cached_next_index;
1193   u32 thread_index = vlib_get_thread_index ();
1194
1195   while (n_left_from > 0)
1196     {
1197       u32 n_left_to_next;
1198       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1199
1200       /* Quad - Loop */
1201       while (n_left_from >= 8 && n_left_to_next >= 4)
1202         {
1203           u32 bi0, bi1, bi2, bi3;
1204           vlib_buffer_t *b0, *b1, *b2, *b3;
1205           ip6_header_t *ip0, *ip1, *ip2, *ip3;
1206           ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1207           ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1208           u32 next0, next1, next2, next3;
1209           next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1210           ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1211
1212           /* Prefetch next iteration. */
1213           {
1214             vlib_buffer_t *p4, *p5, *p6, *p7;
1215
1216             p4 = vlib_get_buffer (vm, from[4]);
1217             p5 = vlib_get_buffer (vm, from[5]);
1218             p6 = vlib_get_buffer (vm, from[6]);
1219             p7 = vlib_get_buffer (vm, from[7]);
1220
1221             /* Prefetch the buffer header and packet for the N+2 loop iteration */
1222             vlib_prefetch_buffer_header (p4, LOAD);
1223             vlib_prefetch_buffer_header (p5, LOAD);
1224             vlib_prefetch_buffer_header (p6, LOAD);
1225             vlib_prefetch_buffer_header (p7, LOAD);
1226
1227             CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1228             CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1229             CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1230             CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1231           }
1232
1233           to_next[0] = bi0 = from[0];
1234           to_next[1] = bi1 = from[1];
1235           to_next[2] = bi2 = from[2];
1236           to_next[3] = bi3 = from[3];
1237           from += 4;
1238           to_next += 4;
1239           n_left_from -= 4;
1240           n_left_to_next -= 4;
1241
1242           b0 = vlib_get_buffer (vm, bi0);
1243           b1 = vlib_get_buffer (vm, bi1);
1244           b2 = vlib_get_buffer (vm, bi2);
1245           b3 = vlib_get_buffer (vm, bi3);
1246
1247           ip0 = vlib_buffer_get_current (b0);
1248           ip1 = vlib_buffer_get_current (b1);
1249           ip2 = vlib_buffer_get_current (b2);
1250           ip3 = vlib_buffer_get_current (b3);
1251
1252           ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1253           ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
1254           ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
1255           ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
1256
1257           ls0 =
1258             pool_elt_at_index (sm->localsids,
1259                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1260           ls1 =
1261             pool_elt_at_index (sm->localsids,
1262                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1263           ls2 =
1264             pool_elt_at_index (sm->localsids,
1265                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1266           ls3 =
1267             pool_elt_at_index (sm->localsids,
1268                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1269
1270           end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1271                               prev0);
1272           end_srh_processing (node, b1, ip1, sr1, ls1, &next1, ls1->end_psp,
1273                               prev1);
1274           end_srh_processing (node, b2, ip2, sr2, ls2, &next2, ls2->end_psp,
1275                               prev2);
1276           end_srh_processing (node, b3, ip3, sr3, ls3, &next3, ls3->end_psp,
1277                               prev3);
1278
1279           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1280             {
1281               sr_localsid_trace_t *tr =
1282                 vlib_add_trace (vm, node, b0, sizeof (*tr));
1283               tr->num_segments = 0;
1284               clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1285                            sizeof (tr->localsid.as_u8));
1286               tr->behavior = ls0->behavior;
1287               if (ip0 == vlib_buffer_get_current (b0))
1288                 {
1289                   if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1290                       && sr0->type == ROUTING_HEADER_TYPE_SR)
1291                     {
1292                       clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1293                       tr->num_segments =
1294                         sr0->length * 8 / sizeof (ip6_address_t);
1295                       tr->segments_left = sr0->segments_left;
1296                     }
1297                 }
1298               else
1299                 tr->num_segments = 0xFF;
1300             }
1301
1302           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1303             {
1304               sr_localsid_trace_t *tr =
1305                 vlib_add_trace (vm, node, b1, sizeof (*tr));
1306               tr->num_segments = 0;
1307               clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1308                            sizeof (tr->localsid.as_u8));
1309               tr->behavior = ls1->behavior;
1310               if (ip1 == vlib_buffer_get_current (b1))
1311                 {
1312                   if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1313                       && sr1->type == ROUTING_HEADER_TYPE_SR)
1314                     {
1315                       clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1316                       tr->num_segments =
1317                         sr1->length * 8 / sizeof (ip6_address_t);
1318                       tr->segments_left = sr1->segments_left;
1319                     }
1320                 }
1321               else
1322                 tr->num_segments = 0xFF;
1323             }
1324
1325           if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1326             {
1327               sr_localsid_trace_t *tr =
1328                 vlib_add_trace (vm, node, b2, sizeof (*tr));
1329               tr->num_segments = 0;
1330               clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1331                            sizeof (tr->localsid.as_u8));
1332               tr->behavior = ls2->behavior;
1333               if (ip2 == vlib_buffer_get_current (b2))
1334                 {
1335                   if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1336                       && sr2->type == ROUTING_HEADER_TYPE_SR)
1337                     {
1338                       clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1339                       tr->num_segments =
1340                         sr2->length * 8 / sizeof (ip6_address_t);
1341                       tr->segments_left = sr2->segments_left;
1342                     }
1343                 }
1344               else
1345                 tr->num_segments = 0xFF;
1346             }
1347
1348           if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1349             {
1350               sr_localsid_trace_t *tr =
1351                 vlib_add_trace (vm, node, b3, sizeof (*tr));
1352               tr->num_segments = 0;
1353               clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1354                            sizeof (tr->localsid.as_u8));
1355               tr->behavior = ls3->behavior;
1356               if (ip3 == vlib_buffer_get_current (b3))
1357                 {
1358                   if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1359                       && sr3->type == ROUTING_HEADER_TYPE_SR)
1360                     {
1361                       clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1362                       tr->num_segments =
1363                         sr3->length * 8 / sizeof (ip6_address_t);
1364                       tr->segments_left = sr3->segments_left;
1365                     }
1366                 }
1367               else
1368                 tr->num_segments = 0xFF;
1369             }
1370
1371           vlib_increment_combined_counter
1372             (((next0 ==
1373                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1374               &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1375              1, vlib_buffer_length_in_chain (vm, b0));
1376
1377           vlib_increment_combined_counter
1378             (((next1 ==
1379                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1380               &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1381              1, vlib_buffer_length_in_chain (vm, b1));
1382
1383           vlib_increment_combined_counter
1384             (((next2 ==
1385                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1386               &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1387              1, vlib_buffer_length_in_chain (vm, b2));
1388
1389           vlib_increment_combined_counter
1390             (((next3 ==
1391                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1392               &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1393              1, vlib_buffer_length_in_chain (vm, b3));
1394
1395           vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1396                                            n_left_to_next, bi0, bi1, bi2, bi3,
1397                                            next0, next1, next2, next3);
1398         }
1399
1400       /* Single loop for potentially the last three packets */
1401       while (n_left_from > 0 && n_left_to_next > 0)
1402         {
1403           u32 bi0;
1404           vlib_buffer_t *b0;
1405           ip6_header_t *ip0 = 0;
1406           ip6_ext_header_t *prev0;
1407           ip6_sr_header_t *sr0;
1408           u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1409           ip6_sr_localsid_t *ls0;
1410
1411           bi0 = from[0];
1412           to_next[0] = bi0;
1413           from += 1;
1414           to_next += 1;
1415           n_left_from -= 1;
1416           n_left_to_next -= 1;
1417
1418           b0 = vlib_get_buffer (vm, bi0);
1419           ip0 = vlib_buffer_get_current (b0);
1420           ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1421
1422           /* Lookup the SR End behavior based on IP DA (adj) */
1423           ls0 =
1424             pool_elt_at_index (sm->localsids,
1425                                vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1426
1427           /* SRH processing */
1428           end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1429                               prev0);
1430
1431           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1432             {
1433               sr_localsid_trace_t *tr =
1434                 vlib_add_trace (vm, node, b0, sizeof (*tr));
1435               tr->num_segments = 0;
1436               clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1437                            sizeof (tr->localsid.as_u8));
1438               tr->behavior = ls0->behavior;
1439               if (ip0 == vlib_buffer_get_current (b0))
1440                 {
1441                   if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1442                       && sr0->type == ROUTING_HEADER_TYPE_SR)
1443                     {
1444                       clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1445                       tr->num_segments =
1446                         sr0->length * 8 / sizeof (ip6_address_t);
1447                       tr->segments_left = sr0->segments_left;
1448                     }
1449                 }
1450               else
1451                 tr->num_segments = 0xFF;
1452             }
1453
1454           vlib_increment_combined_counter
1455             (((next0 ==
1456                SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1457               &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1458              1, vlib_buffer_length_in_chain (vm, b0));
1459
1460           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1461                                            n_left_to_next, bi0, next0);
1462         }
1463       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1464     }
1465   return from_frame->n_vectors;
1466 }
1467
1468 /* *INDENT-OFF* */
1469 VLIB_REGISTER_NODE (sr_localsid_node) = {
1470   .function = sr_localsid_fn,
1471   .name = "sr-localsid",
1472   .vector_size = sizeof (u32),
1473   .format_trace = format_sr_localsid_trace,
1474   .type = VLIB_NODE_TYPE_INTERNAL,
1475   .n_errors = SR_LOCALSID_N_ERROR,
1476   .error_strings = sr_localsid_error_strings,
1477   .n_next_nodes = SR_LOCALSID_N_NEXT,
1478   .next_nodes = {
1479 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1480     foreach_sr_localsid_next
1481 #undef _
1482   },
1483 };
1484 /* *INDENT-ON* */
1485
1486 static u8 *
1487 format_sr_dpo (u8 * s, va_list * args)
1488 {
1489   index_t index = va_arg (*args, index_t);
1490   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1491
1492   return (format (s, "SR: localsid_index:[%d]", index));
1493 }
1494
1495 const static dpo_vft_t sr_loc_vft = {
1496   .dv_lock = sr_dpo_lock,
1497   .dv_unlock = sr_dpo_unlock,
1498   .dv_format = format_sr_dpo,
1499 };
1500
1501 const static char *const sr_loc_ip6_nodes[] = {
1502   "sr-localsid",
1503   NULL,
1504 };
1505
1506 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1507   [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1508 };
1509
1510 const static char *const sr_loc_d_ip6_nodes[] = {
1511   "sr-localsid-d",
1512   NULL,
1513 };
1514
1515 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1516   [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1517 };
1518
1519
1520 /*************************** SR LocalSID plugins ******************************/
1521 /**
1522  * @brief SR LocalSID plugin registry
1523  */
1524 int
1525 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1526                                u8 * keyword_str, u8 * def_str,
1527                                u8 * params_str, dpo_type_t * dpo,
1528                                format_function_t * ls_format,
1529                                unformat_function_t * ls_unformat,
1530                                sr_plugin_callback_t * creation_fn,
1531                                sr_plugin_callback_t * removal_fn)
1532 {
1533   ip6_sr_main_t *sm = &sr_main;
1534   uword *p;
1535
1536   sr_localsid_fn_registration_t *plugin;
1537
1538   /* Did this function exist? If so update it */
1539   p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1540   if (p)
1541     {
1542       plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1543     }
1544   /* Else create a new one and set hash key */
1545   else
1546     {
1547       pool_get (sm->plugin_functions, plugin);
1548       hash_set_mem (sm->plugin_functions_by_key, fn_name,
1549                     plugin - sm->plugin_functions);
1550     }
1551
1552   memset (plugin, 0, sizeof (*plugin));
1553
1554   plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1555   plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1556   plugin->ls_format = ls_format;
1557   plugin->ls_unformat = ls_unformat;
1558   plugin->creation = creation_fn;
1559   plugin->removal = removal_fn;
1560   clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1561   plugin->function_name = format (0, "%s%c", fn_name, 0);
1562   plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1563   plugin->def_str = format (0, "%s%c", def_str, 0);
1564   plugin->params_str = format (0, "%s%c", params_str, 0);
1565
1566   return plugin->sr_localsid_function_number;
1567 }
1568
1569 /**
1570  * @brief CLI function to 'show' all available SR LocalSID behaviors
1571  */
1572 static clib_error_t *
1573 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1574                                        unformat_input_t * input,
1575                                        vlib_cli_command_t * cmd)
1576 {
1577   ip6_sr_main_t *sm = &sr_main;
1578   sr_localsid_fn_registration_t *plugin;
1579   sr_localsid_fn_registration_t **plugins_vec = 0;
1580   int i;
1581
1582   vlib_cli_output (vm,
1583                    "SR LocalSIDs behaviors:\n-----------------------\n\n");
1584
1585   /* *INDENT-OFF* */
1586   pool_foreach (plugin, sm->plugin_functions,
1587     ({ vec_add1 (plugins_vec, plugin); }));
1588   /* *INDENT-ON* */
1589
1590   /* Print static behaviors */
1591   vlib_cli_output (vm, "Default behaviors:\n"
1592                    "\tEnd\t-> Endpoint.\n"
1593                    "\tEnd.X\t-> Endpoint with Layer-3 cross-connect.\n"
1594                    "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1595                    "\tEnd.T\t-> Endpoint with specific IPv6 table lookup.\n"
1596                    "\t\tParameters: '<fib_table>'\n"
1597                    "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1598                    "\t\tParameters: '<iface>'\n"
1599                    "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1600                    "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1601                    "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1602                    "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1603                    "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1604                    "\t\tParameters: '<ip6_fib_table>'\n"
1605                    "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1606                    "\t\tParameters: '<ip4_fib_table>'\n");
1607   vlib_cli_output (vm, "Plugin behaviors:\n");
1608   for (i = 0; i < vec_len (plugins_vec); i++)
1609     {
1610       plugin = plugins_vec[i];
1611       vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1612                        plugin->def_str);
1613       vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1614     }
1615   return 0;
1616 }
1617
1618 /* *INDENT-OFF* */
1619 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1620   .path = "show sr localsids behaviors",
1621   .short_help = "show sr localsids behaviors",
1622   .function = show_sr_localsid_behaviors_command_fn,
1623 };
1624 /* *INDENT-ON* */
1625
1626 /**
1627  * @brief SR LocalSID initialization
1628  */
1629 clib_error_t *
1630 sr_localsids_init (vlib_main_t * vm)
1631 {
1632   /* Init memory for function keys */
1633   ip6_sr_main_t *sm = &sr_main;
1634   mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1635               sizeof (ip6_address_t));
1636   /* Init SR behaviors DPO type */
1637   sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1638   /* Init SR behaviors DPO type */
1639   sr_localsid_d_dpo_type =
1640     dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1641   /* Init memory for localsid plugins */
1642   sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1643   return 0;
1644 }
1645
1646 VLIB_INIT_FUNCTION (sr_localsids_init);
1647 /*
1648 * fd.io coding-style-patch-verification: ON
1649 *
1650 * Local Variables:
1651 * eval: (c-set-style "gnu")
1652 * End:
1653 */