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