2 * sr_localsid.c: ipv6 segment routing Endpoint behaviors
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief Processing of packets with a SRH
22 * CLI to define new Segment Routing End processing functions.
23 * Graph node to support such functions.
25 * Each function associates an SRv6 segment (IPv6 address) with an specific
26 * Segment Routing function.
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>
40 #include <vppinfra/error.h>
41 #include <vppinfra/elog.h>
44 * @brief Dynamically added SR localsid DPO type
46 static dpo_type_t sr_localsid_dpo_type;
47 static dpo_type_t sr_localsid_d_dpo_type;
50 * @brief SR localsid add/del
52 * Function to add or delete SR LocalSIDs.
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.
63 * @return 0 on success, error otherwise.
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)
70 ip6_sr_main_t *sm = &sr_main;
74 ip6_sr_localsid_t *ls = 0;
76 dpo_id_t dpo = DPO_INVALID;
78 /* Search for the item */
79 p = mhash_get (&sm->sr_localsids_index_hash, localsid_addr);
85 /* Retrieve localsid */
86 ls = pool_elt_at_index (sm->localsids, p[0]);
87 /* Delete FIB entry */
89 .fp_proto = FIB_PROTOCOL_IP6,
92 .ip6 = *localsid_addr,
96 fib_table_entry_delete (fib_table_find (FIB_PROTOCOL_IP6,
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);
105 if (ls->behavior >= SR_BEHAVIOR_LAST)
107 sr_localsid_fn_registration_t *plugin = 0;
108 plugin = pool_elt_at_index (sm->plugin_functions,
109 ls->behavior - SR_BEHAVIOR_LAST);
111 /* Callback plugin removal function */
112 rv = plugin->removal (ls);
115 /* Delete localsid registry */
116 pool_put (sm->localsids, ls);
117 mhash_unset (&sm->sr_localsids_index_hash, localsid_addr, NULL);
120 else /* create with function already existing; complain */
124 /* delete; localsid does not exist; complain */
128 /* Check whether there exists a FIB entry with such address */
130 .fp_proto = FIB_PROTOCOL_IP6,
134 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
135 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
137 /* Lookup the FIB index associated to the table id provided */
138 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, fib_table);
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)
147 /* Create a new localsid registry */
148 pool_get (sm->localsids, ls);
149 memset (ls, 0, sizeof (*ls));
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;
158 case SR_BEHAVIOR_END:
161 ls->sw_if_index = sw_if_index;
162 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
164 case SR_BEHAVIOR_DX4:
165 ls->sw_if_index = sw_if_index;
166 clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
168 case SR_BEHAVIOR_DX6:
169 ls->sw_if_index = sw_if_index;
170 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
172 case SR_BEHAVIOR_DT6:
173 ls->vrf_index = sw_if_index;
175 case SR_BEHAVIOR_DX2:
176 ls->sw_if_index = sw_if_index;
177 ls->vlan_index = vlan_index;
181 /* Figure out the adjacency magic for Xconnect variants */
182 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
183 || ls->behavior == SR_BEHAVIOR_DX6)
185 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
187 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
188 if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
189 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
190 nh_addr, sw_if_index);
192 else if (ls->behavior == SR_BEHAVIOR_DX4)
193 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
194 nh_addr, sw_if_index);
196 /* Check for ADJ creation error. If so panic */
197 if (nh_adj_index == ADJ_INDEX_INVALID)
199 pool_put (sm->localsids, ls);
203 ls->nh_adj = nh_adj_index;
207 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X)
208 dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
209 else if (ls->behavior > SR_BEHAVIOR_D_FIRST
210 && ls->behavior < SR_BEHAVIOR_LAST)
211 dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
212 else if (ls->behavior >= SR_BEHAVIOR_LAST)
214 sr_localsid_fn_registration_t *plugin = 0;
215 plugin = pool_elt_at_index (sm->plugin_functions,
216 ls->behavior - SR_BEHAVIOR_LAST);
217 /* Copy the unformat memory result */
218 ls->plugin_mem = ls_plugin_mem;
219 /* Callback plugin creation function */
220 rv = plugin->creation (ls);
223 pool_put (sm->localsids, ls);
226 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
229 /* Set hash key for searching localsid by address */
230 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
233 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
234 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
237 /* Set counter to zero */
238 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
240 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
243 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
245 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
252 * @brief SR LocalSID CLI function.
254 * @see sr_cli_localsid
256 static clib_error_t *
257 sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
258 vlib_cli_command_t * cmd)
260 vnet_main_t *vnm = vnet_get_main ();
261 ip6_sr_main_t *sm = &sr_main;
262 u32 sw_if_index = (u32) ~ 0, vlan_index = (u32) ~ 0, fib_index = 0;
265 ip6_address_t resulting_address;
266 ip46_address_t next_hop;
267 char address_set = 0;
269 void *ls_plugin_mem = 0;
273 memset (&resulting_address, 0, sizeof (ip6_address_t));
274 ip46_address_reset (&next_hop);
276 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
278 if (unformat (input, "del"))
280 else if (!address_set
281 && unformat (input, "address %U", unformat_ip6_address,
284 else if (!address_set
285 && unformat (input, "addr %U", unformat_ip6_address,
288 else if (unformat (input, "fib-table %u", &fib_index));
289 else if (vlan_index == (u32) ~ 0
290 && unformat (input, "vlan %u", &vlan_index));
291 else if (!behavior && unformat (input, "behavior"))
293 if (unformat (input, "end.x %U %U",
294 unformat_vnet_sw_interface, vnm, &sw_if_index,
295 unformat_ip6_address, &next_hop.ip6))
296 behavior = SR_BEHAVIOR_X;
297 else if (unformat (input, "end.dx6 %U %U",
298 unformat_vnet_sw_interface, vnm, &sw_if_index,
299 unformat_ip6_address, &next_hop.ip6))
300 behavior = SR_BEHAVIOR_DX6;
301 else if (unformat (input, "end.dx4 %U %U",
302 unformat_vnet_sw_interface, vnm, &sw_if_index,
303 unformat_ip4_address, &next_hop.ip4))
304 behavior = SR_BEHAVIOR_DX4;
305 else if (unformat (input, "end.dx2 %U",
306 unformat_vnet_sw_interface, vnm, &sw_if_index))
307 behavior = SR_BEHAVIOR_DX2;
308 else if (unformat (input, "end.dt6 %u", &sw_if_index))
309 behavior = SR_BEHAVIOR_DT6;
310 else if (unformat (input, "end.dt4 %u", &sw_if_index))
311 behavior = SR_BEHAVIOR_DT4;
314 /* Loop over all the plugin behavior format functions */
315 sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
316 sr_localsid_fn_registration_t **plugin_it = 0;
318 /* Create a vector out of the plugin pool as recommended */
320 pool_foreach (plugin, sm->plugin_functions,
322 vec_add1 (vec_plugins, plugin);
326 vec_foreach (plugin_it, vec_plugins)
329 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
331 behavior = (*plugin_it)->sr_localsid_function_number;
339 if (unformat (input, "end"))
340 behavior = SR_BEHAVIOR_END;
345 else if (!end_psp && unformat (input, "psp"))
351 if (!behavior && end_psp)
352 behavior = SR_BEHAVIOR_END;
355 return clib_error_return (0,
356 "Error: SRv6 LocalSID address is mandatory.");
357 if (!is_del && !behavior)
358 return clib_error_return (0,
359 "Error: SRv6 LocalSID behavior is mandatory.");
360 if (vlan_index != (u32) ~ 0)
361 return clib_error_return (0,
362 "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
363 if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
364 return clib_error_return (0,
365 "Error: SRv6 PSP only compatible with End and End.X");
367 rv = sr_cli_localsid (is_del, &resulting_address, end_psp, behavior,
368 sw_if_index, vlan_index, fib_index, &next_hop,
378 return clib_error_return (0,
379 "Identical localsid already exists. Requested localsid not created.");
381 return clib_error_return (0,
382 "The requested localsid could not be deleted. SR localsid not found");
384 return clib_error_return (0, "FIB table %u does not exist", fib_index);
386 return clib_error_return (0, "There is already one FIB entry for the"
387 "requested localsid non segment routing related");
389 return clib_error_return (0,
390 "Could not create ARP/ND entry for such next_hop. Internal error.");
392 return clib_error_return (0,
393 "Error on the plugin based localsid creation.");
395 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
401 VLIB_CLI_COMMAND (sr_localsid_command, static) = {
402 .path = "sr localsid",
403 .short_help = "sr localsid (del) address XX:XX::YY:YY"
404 "(fib-table 8) behavior STRING",
406 "Create SR LocalSID and binds it to a particular behavior\n"
408 "\tlocalSID IPv6_addr(128b) LocalSID IPv6 address\n"
409 "\t(fib-table X) Optional. VRF where to install SRv6 localsid\n"
410 "\tbehavior STRING Specifies the behavior\n"
412 "\tEnd\t-> Endpoint.\n"
413 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
414 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
415 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
416 "\t\tParameters: '<iface>'\n"
417 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
418 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
419 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
420 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
421 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
422 "\t\tParameters: '<ip6_fib_table>'\n"
423 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
424 "\t\tParameters: '<ip4_fib_table>'\n",
425 .function = sr_cli_localsid_command_fn,
430 * @brief CLI function to 'show' all SR LocalSIDs on console.
432 static clib_error_t *
433 show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
434 vlib_cli_command_t * cmd)
436 vnet_main_t *vnm = vnet_get_main ();
437 ip6_sr_main_t *sm = &sr_main;
438 ip6_sr_localsid_t **localsid_list = 0;
439 ip6_sr_localsid_t *ls;
442 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
443 vlib_cli_output (vm, "=========================");
445 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
447 for (i = 0; i < vec_len (localsid_list); i++)
449 ls = localsid_list[i];
450 switch (ls->behavior)
452 case SR_BEHAVIOR_END:
453 vlib_cli_output (vm, "\tAddress: \t%U\n\tBehavior: \tEnd",
454 format_ip6_address, &ls->localsid);
458 "\tAddress: \t%U\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
459 "\n\tIface: \t%U\n\tNext hop: \t%U",
460 format_ip6_address, &ls->localsid,
461 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
462 format_ip6_address, &ls->next_hop.ip6);
464 case SR_BEHAVIOR_DX4:
466 "\tAddress: \t%U\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
467 "\n\tIface: \t%U\n\tNext hop: \t%U",
468 format_ip6_address, &ls->localsid,
469 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
470 format_ip4_address, &ls->next_hop.ip4);
472 case SR_BEHAVIOR_DX6:
474 "\tAddress: \t%U\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
475 "\n\tIface: \t%U\n\tNext hop: \t%U",
476 format_ip6_address, &ls->localsid,
477 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
478 format_ip6_address, &ls->next_hop.ip6);
480 case SR_BEHAVIOR_DX2:
481 if (ls->vlan_index == (u32) ~ 0)
483 "\tAddress: \t%U\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
484 "\n\tIface: \t%U", format_ip6_address,
485 &ls->localsid, format_vnet_sw_if_index_name, vnm,
489 "Unsupported yet. (DX2 with egress VLAN rewrite)");
491 case SR_BEHAVIOR_DT6:
493 "\tAddress: \t%U\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
494 "\n\tTable: %u", format_ip6_address, &ls->localsid,
497 case SR_BEHAVIOR_DT4:
499 "\tAddress: \t%U\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
500 "\n\tTable: \t%u", format_ip6_address,
501 &ls->localsid, ls->fib_table);
504 if (ls->behavior >= SR_BEHAVIOR_LAST)
506 sr_localsid_fn_registration_t *plugin =
507 pool_elt_at_index (sm->plugin_functions,
508 ls->behavior - SR_BEHAVIOR_LAST);
510 vlib_cli_output (vm, "\tAddress: \t%U\n"
511 "\tBehavior: \t%s (%s)\n\t%U",
512 format_ip6_address, &ls->localsid,
513 plugin->keyword_str, plugin->def_str,
514 plugin->ls_format, ls->plugin_mem);
517 //Should never get here...
518 vlib_cli_output (vm, "Internal error");
522 vlib_cli_output (vm, "\tPSP: \tTrue\n");
525 vlib_counter_t valid, invalid;
526 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
527 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
528 vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
529 valid.packets, valid.bytes);
530 vlib_cli_output (vm, "\tBad traffic: \t[%Ld packets : %Ld bytes]\n",
531 invalid.packets, invalid.bytes);
532 vlib_cli_output (vm, "--------------------");
538 VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
539 .path = "show sr localsids",
540 .short_help = "show sr localsids",
541 .function = show_sr_localsid_command_fn,
546 * @brief Function to 'clear' ALL SR localsid counters
548 static clib_error_t *
549 clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
550 unformat_input_t * input,
551 vlib_cli_command_t * cmd)
553 ip6_sr_main_t *sm = &sr_main;
555 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
556 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
562 VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
563 .path = "clear sr localsid counters",
564 .short_help = "clear sr localsid counters",
565 .function = clear_sr_localsid_counters_command_fn,
569 /************************ SR LocalSID graphs node ****************************/
571 * @brief SR localsid node trace
576 ip6_address_t src, out_dst;
580 //With SRv6 header update include flags here.
581 } sr_localsid_trace_t;
583 #define foreach_sr_localsid_error \
584 _(NO_INNER_HEADER, "(SR-Error) No inner IP header") \
585 _(NO_MORE_SEGMENTS, "(SR-Error) No more segments") \
586 _(NO_SRH, "(SR-Error) No SR header") \
587 _(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)") \
588 _(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)") \
589 _(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
593 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
594 foreach_sr_localsid_error
597 } sr_localsid_error_t;
599 static char *sr_localsid_error_strings[] = {
600 #define _(sym,string) string,
601 foreach_sr_localsid_error
605 #define foreach_sr_localsid_next \
606 _(ERROR, "error-drop") \
607 _(IP6_LOOKUP, "ip6-lookup") \
608 _(IP4_LOOKUP, "ip4-lookup") \
609 _(IP6_REWRITE, "ip6-rewrite") \
610 _(IP4_REWRITE, "ip4-rewrite") \
611 _(INTERFACE_OUTPUT, "interface-output")
615 #define _(s,n) SR_LOCALSID_NEXT_##s,
616 foreach_sr_localsid_next
619 } sr_localsid_next_t;
622 * @brief SR LocalSID graph node trace function
627 format_sr_localsid_trace (u8 * s, va_list * args)
629 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
630 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
631 ip6_sr_main_t *sm = &sr_main;
632 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
634 ip6_sr_localsid_t *ls =
635 pool_elt_at_index (sm->localsids, t->localsid_index);
638 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
640 switch (ls->behavior)
642 case SR_BEHAVIOR_END:
643 s = format (s, "\tBehavior: End\n");
645 case SR_BEHAVIOR_DX6:
646 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
648 case SR_BEHAVIOR_DX4:
649 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
652 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
654 case SR_BEHAVIOR_DT6:
655 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
657 case SR_BEHAVIOR_DT4:
658 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
660 case SR_BEHAVIOR_DX2:
661 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
664 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
667 if (t->num_segments != 0xFF)
669 if (t->num_segments > 0)
671 s = format (s, "\tSegments left: %d\n", t->num_segments);
672 s = format (s, "\tSID list: [in ietf order]");
674 for (i = 0; i < t->num_segments; i++)
676 s = format (s, "\n\t-> %U", format_ip6_address,
677 (ip6_address_t *) & t->sr[i *
678 sizeof (ip6_address_t)]);
686 * @brief Function doing End processing.
688 static_always_inline void
689 end_srh_processing (vlib_node_runtime_t * node,
692 ip6_sr_header_t * sr0,
693 ip6_sr_localsid_t * ls0, u32 * next0)
695 ip6_address_t *new_dst0;
697 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
699 if (PREDICT_TRUE (sr0->segments_left != 0))
701 sr0->segments_left -= 1;
702 new_dst0 = (ip6_address_t *) (sr0->segments);
703 new_dst0 += sr0->segments_left;
704 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
705 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
707 if (ls0->behavior == SR_BEHAVIOR_X)
709 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
710 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
715 *next0 = SR_LOCALSID_NEXT_ERROR;
716 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
721 /* Error. Routing header of type != SR */
722 *next0 = SR_LOCALSID_NEXT_ERROR;
723 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
728 * @brief Function doing SRH processing for D* variants
730 //FixME. I must crosscheck that next_proto matches the localsid
731 static_always_inline void
732 end_decaps_srh_processing (vlib_node_runtime_t * node,
735 ip6_sr_header_t * sr0,
736 ip6_sr_localsid_t * ls0, u32 * next0)
738 /* Compute the size of the IPv6 header with all Ext. headers */
740 ip6_ext_header_t *next_ext_header;
743 next_proto = ip0->protocol;
744 next_ext_header = (void *) (ip0 + 1);
745 total_size = sizeof (ip6_header_t);
746 while (ip6_ext_hdr (next_proto))
748 total_size += ip6_ext_header_len (next_ext_header);
749 next_proto = next_ext_header->next_hdr;
750 next_ext_header = ip6_ext_next_header (next_ext_header);
753 /* Ensure this is the last segment. Otherwise drop. */
754 if (sr0 && sr0->segments_left != 0)
756 *next0 = SR_LOCALSID_NEXT_ERROR;
757 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
763 case IP_PROTOCOL_IPV6:
764 /* Encap-End IPv6. Pop outer IPv6 header. */
765 if (ls0->behavior == SR_BEHAVIOR_DX6)
767 vlib_buffer_advance (b0, total_size);
768 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
769 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
772 else if (ls0->behavior == SR_BEHAVIOR_DT6)
774 vlib_buffer_advance (b0, total_size);
775 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->fib_table;
779 case IP_PROTOCOL_IP_IN_IP:
780 /* Encap-End IPv4. Pop outer IPv6 header */
781 if (ls0->behavior == SR_BEHAVIOR_DX4)
783 vlib_buffer_advance (b0, total_size);
784 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
785 *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
788 else if (ls0->behavior == SR_BEHAVIOR_DT4)
790 vlib_buffer_advance (b0, total_size);
791 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->fib_table;
792 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
796 case IP_PROTOCOL_IP6_NONXT:
798 if (ls0->behavior == SR_BEHAVIOR_DX2)
800 vlib_buffer_advance (b0, total_size);
801 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
802 *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
807 *next0 = SR_LOCALSID_NEXT_ERROR;
808 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
813 * @brief Function doing End processing with PSP
815 static_always_inline void
816 end_psp_srh_processing (vlib_node_runtime_t * node,
819 ip6_ext_header_t * prev0,
820 ip6_sr_header_t * sr0,
821 ip6_sr_localsid_t * ls0, u32 * next0)
824 u64 *copy_dst0, *copy_src0;
825 u32 copy_len_u64s0 = 0;
828 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
830 if (PREDICT_TRUE (sr0->segments_left == 1))
832 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
833 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
835 /* Remove the SRH taking care of the rest of IPv6 ext header */
837 prev0->next_hdr = sr0->protocol;
839 ip0->protocol = sr0->protocol;
841 sr_len = ip6_ext_header_len (sr0);
842 vlib_buffer_advance (b0, sr_len);
843 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
844 ip0->payload_length = clib_host_to_net_u16 (new_l0);
845 copy_src0 = (u64 *) ip0;
846 copy_dst0 = copy_src0 + (sr0->length + 1);
847 /* number of 8 octet units to copy
848 * By default in absence of extension headers it is equal to length of ip6 header
849 * With extension headers it number of 8 octet units of ext headers preceding
853 (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
854 copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
855 copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
856 copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
857 copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
858 copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
860 for (i = copy_len_u64s0 - 1; i >= 0; i--)
862 copy_dst0[i] = copy_src0[i];
865 if (ls0->behavior == SR_BEHAVIOR_X)
867 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
868 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
873 /* Error. Routing header of type != SR */
874 *next0 = SR_LOCALSID_NEXT_ERROR;
875 b0->error = node->errors[SR_LOCALSID_ERROR_NO_PSP];
879 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
882 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
883 vlib_frame_t * from_frame)
885 u32 n_left_from, next_index, *from, *to_next;
886 ip6_sr_main_t *sm = &sr_main;
887 from = vlib_frame_vector_args (from_frame);
888 n_left_from = from_frame->n_vectors;
889 next_index = node->cached_next_index;
890 u32 thread_index = vlib_get_thread_index ();
892 while (n_left_from > 0)
895 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
898 while (n_left_from >= 8 && n_left_to_next >= 4)
900 u32 bi0, bi1, bi2, bi3;
901 vlib_buffer_t *b0, *b1, *b2, *b3;
902 ip6_header_t *ip0, *ip1, *ip2, *ip3;
903 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
904 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
905 u32 next0, next1, next2, next3;
906 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
907 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
909 /* Prefetch next iteration. */
911 vlib_buffer_t *p4, *p5, *p6, *p7;
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]);
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);
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);
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];
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);
945 pool_elt_at_index (sm->localsids,
946 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
948 pool_elt_at_index (sm->localsids,
949 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
951 pool_elt_at_index (sm->localsids,
952 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
954 pool_elt_at_index (sm->localsids,
955 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
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);
962 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
963 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
964 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
965 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
967 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
968 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
969 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
970 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
974 vlib_increment_combined_counter
976 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
977 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
978 1, vlib_buffer_length_in_chain (vm, b0));
980 vlib_increment_combined_counter
982 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
983 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
984 1, vlib_buffer_length_in_chain (vm, b1));
986 vlib_increment_combined_counter
988 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
989 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
990 1, vlib_buffer_length_in_chain (vm, b2));
992 vlib_increment_combined_counter
994 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
995 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
996 1, vlib_buffer_length_in_chain (vm, b3));
998 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
999 n_left_to_next, bi0, bi1, bi2, bi3,
1000 next0, next1, next2, next3);
1003 /* Single loop for potentially the last three packets */
1004 while (n_left_from > 0 && n_left_to_next > 0)
1009 ip6_ext_header_t *prev0;
1010 ip6_sr_header_t *sr0;
1011 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1012 ip6_sr_localsid_t *ls0;
1019 n_left_to_next -= 1;
1021 b0 = vlib_get_buffer (vm, bi0);
1022 ip0 = vlib_buffer_get_current (b0);
1024 /* Lookup the SR End behavior based on IP DA (adj) */
1026 pool_elt_at_index (sm->localsids,
1027 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1029 /* Find SRH as well as previous header */
1030 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1032 /* SRH processing and End variants */
1033 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1035 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1037 sr_localsid_trace_t *tr =
1038 vlib_add_trace (vm, node, b0, sizeof (*tr));
1039 tr->num_segments = 0;
1040 tr->localsid_index = ls0 - sm->localsids;
1042 if (ip0 == vlib_buffer_get_current (b0))
1044 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1045 sizeof (tr->src.as_u8));
1046 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1047 sizeof (tr->out_dst.as_u8));
1048 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1049 && sr0->type == ROUTING_HEADER_TYPE_SR)
1051 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1053 sr0->length * 8 / sizeof (ip6_address_t);
1054 tr->segments_left = sr0->segments_left;
1058 tr->num_segments = 0xFF;
1061 /* Increase the counters */
1062 vlib_increment_combined_counter
1064 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1065 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1066 1, vlib_buffer_length_in_chain (vm, b0));
1068 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1069 n_left_to_next, bi0, next0);
1071 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1073 return from_frame->n_vectors;
1077 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1078 .function = sr_localsid_d_fn,
1079 .name = "sr-localsid-d",
1080 .vector_size = sizeof (u32),
1081 .format_trace = format_sr_localsid_trace,
1082 .type = VLIB_NODE_TYPE_INTERNAL,
1083 .n_errors = SR_LOCALSID_N_ERROR,
1084 .error_strings = sr_localsid_error_strings,
1085 .n_next_nodes = SR_LOCALSID_N_NEXT,
1087 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1088 foreach_sr_localsid_next
1095 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
1098 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1099 vlib_frame_t * from_frame)
1101 u32 n_left_from, next_index, *from, *to_next;
1102 ip6_sr_main_t *sm = &sr_main;
1103 from = vlib_frame_vector_args (from_frame);
1104 n_left_from = from_frame->n_vectors;
1105 next_index = node->cached_next_index;
1106 u32 thread_index = vlib_get_thread_index ();
1108 while (n_left_from > 0)
1111 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1114 while (n_left_from >= 8 && n_left_to_next >= 4)
1116 u32 bi0, bi1, bi2, bi3;
1117 vlib_buffer_t *b0, *b1, *b2, *b3;
1118 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1119 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1120 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1121 u32 next0, next1, next2, next3;
1122 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1123 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1125 /* Prefetch next iteration. */
1127 vlib_buffer_t *p4, *p5, *p6, *p7;
1129 p4 = vlib_get_buffer (vm, from[4]);
1130 p5 = vlib_get_buffer (vm, from[5]);
1131 p6 = vlib_get_buffer (vm, from[6]);
1132 p7 = vlib_get_buffer (vm, from[7]);
1134 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1135 vlib_prefetch_buffer_header (p4, LOAD);
1136 vlib_prefetch_buffer_header (p5, LOAD);
1137 vlib_prefetch_buffer_header (p6, LOAD);
1138 vlib_prefetch_buffer_header (p7, LOAD);
1140 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1141 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1142 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1143 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1146 to_next[0] = bi0 = from[0];
1147 to_next[1] = bi1 = from[1];
1148 to_next[2] = bi2 = from[2];
1149 to_next[3] = bi3 = from[3];
1153 n_left_to_next -= 4;
1155 b0 = vlib_get_buffer (vm, bi0);
1156 b1 = vlib_get_buffer (vm, bi1);
1157 b2 = vlib_get_buffer (vm, bi2);
1158 b3 = vlib_get_buffer (vm, bi3);
1160 ip0 = vlib_buffer_get_current (b0);
1161 ip1 = vlib_buffer_get_current (b1);
1162 ip2 = vlib_buffer_get_current (b2);
1163 ip3 = vlib_buffer_get_current (b3);
1165 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1166 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
1167 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
1168 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
1171 pool_elt_at_index (sm->localsids,
1172 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1174 pool_elt_at_index (sm->localsids,
1175 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1177 pool_elt_at_index (sm->localsids,
1178 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1180 pool_elt_at_index (sm->localsids,
1181 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1184 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1186 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1189 end_psp_srh_processing (node, b1, ip1, prev1, sr1, ls1, &next1);
1191 end_srh_processing (node, b1, ip1, sr1, ls1, &next1);
1194 end_psp_srh_processing (node, b2, ip2, prev2, sr2, ls2, &next2);
1196 end_srh_processing (node, b2, ip2, sr2, ls2, &next2);
1199 end_psp_srh_processing (node, b3, ip3, prev3, sr3, ls3, &next3);
1201 end_srh_processing (node, b3, ip3, sr3, ls3, &next3);
1203 //TODO: proper trace.
1205 vlib_increment_combined_counter
1207 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1208 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1209 1, vlib_buffer_length_in_chain (vm, b0));
1211 vlib_increment_combined_counter
1213 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1214 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1215 1, vlib_buffer_length_in_chain (vm, b1));
1217 vlib_increment_combined_counter
1219 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1220 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1221 1, vlib_buffer_length_in_chain (vm, b2));
1223 vlib_increment_combined_counter
1225 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1226 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1227 1, vlib_buffer_length_in_chain (vm, b3));
1229 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1230 n_left_to_next, bi0, bi1, bi2, bi3,
1231 next0, next1, next2, next3);
1234 /* Single loop for potentially the last three packets */
1235 while (n_left_from > 0 && n_left_to_next > 0)
1239 ip6_header_t *ip0 = 0;
1240 ip6_ext_header_t *prev0;
1241 ip6_sr_header_t *sr0;
1242 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1243 ip6_sr_localsid_t *ls0;
1250 n_left_to_next -= 1;
1252 b0 = vlib_get_buffer (vm, bi0);
1253 ip0 = vlib_buffer_get_current (b0);
1254 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1256 /* Lookup the SR End behavior based on IP DA (adj) */
1258 pool_elt_at_index (sm->localsids,
1259 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1261 /* SRH processing */
1263 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1265 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1267 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1269 sr_localsid_trace_t *tr =
1270 vlib_add_trace (vm, node, b0, sizeof (*tr));
1271 tr->num_segments = 0;
1272 tr->localsid_index = ls0 - sm->localsids;
1274 if (ip0 == vlib_buffer_get_current (b0))
1276 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1277 sizeof (tr->src.as_u8));
1278 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1279 sizeof (tr->out_dst.as_u8));
1280 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1281 && sr0->type == ROUTING_HEADER_TYPE_SR)
1283 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1285 sr0->length * 8 / sizeof (ip6_address_t);
1286 tr->segments_left = sr0->segments_left;
1291 tr->num_segments = 0xFF;
1295 vlib_increment_combined_counter
1297 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1298 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1299 1, vlib_buffer_length_in_chain (vm, b0));
1301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1302 n_left_to_next, bi0, next0);
1304 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1306 return from_frame->n_vectors;
1310 VLIB_REGISTER_NODE (sr_localsid_node) = {
1311 .function = sr_localsid_fn,
1312 .name = "sr-localsid",
1313 .vector_size = sizeof (u32),
1314 .format_trace = format_sr_localsid_trace,
1315 .type = VLIB_NODE_TYPE_INTERNAL,
1316 .n_errors = SR_LOCALSID_N_ERROR,
1317 .error_strings = sr_localsid_error_strings,
1318 .n_next_nodes = SR_LOCALSID_N_NEXT,
1320 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1321 foreach_sr_localsid_next
1328 format_sr_dpo (u8 * s, va_list * args)
1330 index_t index = va_arg (*args, index_t);
1331 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1333 return (format (s, "SR: localsid_index:[%d]", index));
1336 const static dpo_vft_t sr_loc_vft = {
1337 .dv_lock = sr_dpo_lock,
1338 .dv_unlock = sr_dpo_unlock,
1339 .dv_format = format_sr_dpo,
1342 const static char *const sr_loc_ip6_nodes[] = {
1347 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1348 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1351 const static char *const sr_loc_d_ip6_nodes[] = {
1356 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1357 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1361 /*************************** SR LocalSID plugins ******************************/
1363 * @brief SR LocalSID plugin registry
1366 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1367 u8 * keyword_str, u8 * def_str,
1368 u8 * params_str, dpo_type_t * dpo,
1369 format_function_t * ls_format,
1370 unformat_function_t * ls_unformat,
1371 sr_plugin_callback_t * creation_fn,
1372 sr_plugin_callback_t * removal_fn)
1374 ip6_sr_main_t *sm = &sr_main;
1377 sr_localsid_fn_registration_t *plugin;
1379 /* Did this function exist? If so update it */
1380 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1383 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1385 /* Else create a new one and set hash key */
1388 pool_get (sm->plugin_functions, plugin);
1389 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1390 plugin - sm->plugin_functions);
1393 memset (plugin, 0, sizeof (*plugin));
1395 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1396 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1397 plugin->ls_format = ls_format;
1398 plugin->ls_unformat = ls_unformat;
1399 plugin->creation = creation_fn;
1400 plugin->removal = removal_fn;
1401 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1402 plugin->function_name = format (0, "%s%c", fn_name, 0);
1403 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1404 plugin->def_str = format (0, "%s%c", def_str, 0);
1405 plugin->params_str = format (0, "%s%c", params_str, 0);
1407 return plugin->sr_localsid_function_number;
1411 * @brief CLI function to 'show' all available SR LocalSID behaviors
1413 static clib_error_t *
1414 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1415 unformat_input_t * input,
1416 vlib_cli_command_t * cmd)
1418 ip6_sr_main_t *sm = &sr_main;
1419 sr_localsid_fn_registration_t *plugin;
1420 sr_localsid_fn_registration_t **plugins_vec = 0;
1423 vlib_cli_output (vm,
1424 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1427 pool_foreach (plugin, sm->plugin_functions,
1428 ({ vec_add1 (plugins_vec, plugin); }));
1431 /* Print static behaviors */
1432 vlib_cli_output (vm, "Default behaviors:\n"
1433 "\tEnd\t-> Endpoint.\n"
1434 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
1435 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1436 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1437 "\t\tParameters: '<iface>'\n"
1438 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1439 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1440 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1441 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1442 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1443 "\t\tParameters: '<ip6_fib_table>'\n"
1444 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1445 "\t\tParameters: '<ip4_fib_table>'\n");
1446 vlib_cli_output (vm, "Plugin behaviors:\n");
1447 for (i = 0; i < vec_len (plugins_vec); i++)
1449 plugin = plugins_vec[i];
1450 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1452 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1458 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1459 .path = "show sr localsids behaviors",
1460 .short_help = "show sr localsids behaviors",
1461 .function = show_sr_localsid_behaviors_command_fn,
1466 * @brief SR LocalSID initialization
1469 sr_localsids_init (vlib_main_t * vm)
1471 /* Init memory for function keys */
1472 ip6_sr_main_t *sm = &sr_main;
1473 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1474 sizeof (ip6_address_t));
1475 /* Init SR behaviors DPO type */
1476 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1477 /* Init SR behaviors DPO type */
1478 sr_localsid_d_dpo_type =
1479 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1480 /* Init memory for localsid plugins */
1481 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1485 VLIB_INIT_FUNCTION (sr_localsids_init);
1487 * fd.io coding-style-patch-verification: ON
1490 * eval: (c-set-style "gnu")