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