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/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>
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;
75 ip6_address_t *key_copy;
77 dpo_id_t dpo = DPO_INVALID;
79 /* Search for the item */
80 p = hash_get_mem (sm->localsids_index_by_key, localsid_addr);
87 /* Retrieve localsid */
88 ls = pool_elt_at_index (sm->localsids, p[0]);
89 /* Delete FIB entry */
91 .fp_proto = FIB_PROTOCOL_IP6,
94 .ip6 = *localsid_addr,
98 fib_table_entry_delete (fib_table_id_find_fib_index
99 (FIB_PROTOCOL_IP6, fib_table), &pfx,
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);
107 if (ls->behavior >= SR_BEHAVIOR_LAST)
109 sr_localsid_fn_registration_t *plugin = 0;
110 plugin = pool_elt_at_index (sm->plugin_functions,
111 ls->behavior - SR_BEHAVIOR_LAST);
113 /* Callback plugin removal function */
114 rv = plugin->removal (ls);
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);
125 else /* create with function already existing; complain */
129 /* delete; localsid does not exist; complain */
133 /* Check whether there exists a FIB entry with such address */
135 .fp_proto = FIB_PROTOCOL_IP6,
139 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
140 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
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);
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)
152 /* Create a new localsid registry */
153 pool_get (sm->localsids, ls);
154 memset (ls, 0, sizeof (*ls));
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;
163 case SR_BEHAVIOR_END:
166 ls->sw_if_index = sw_if_index;
167 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
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));
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));
177 case SR_BEHAVIOR_DT6:
178 ls->vrf_index = sw_if_index;
180 case SR_BEHAVIOR_DX2:
181 ls->sw_if_index = sw_if_index;
182 ls->vlan_index = vlan_index;
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)
190 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
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);
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);
201 /* Check for ADJ creation error. If so panic */
202 if (nh_adj_index == ADJ_INDEX_INVALID)
204 pool_put (sm->localsids, ls);
208 ls->nh_adj = nh_adj_index;
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)
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);
228 pool_put (sm->localsids, ls);
231 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
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);
239 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
240 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
243 /* Set counter to zero */
244 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
246 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
249 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
251 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
258 * @brief SR LocalSID CLI function.
260 * @see sr_cli_localsid
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)
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;
271 ip6_address_t resulting_address;
272 ip46_address_t next_hop;
273 char address_set = 0;
275 void *ls_plugin_mem = 0;
279 memset (&resulting_address, 0, sizeof (ip6_address_t));
280 ip46_address_reset (&next_hop);
282 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
284 if (unformat (input, "del"))
286 else if (!address_set
287 && unformat (input, "address %U", unformat_ip6_address,
290 else if (!address_set
291 && unformat (input, "addr %U", unformat_ip6_address,
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"))
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;
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;
324 /* Create a vector out of the plugin pool as recommended */
326 pool_foreach (plugin, sm->plugin_functions,
328 vec_add1 (vec_plugins, plugin);
332 vec_foreach (plugin_it, vec_plugins)
335 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
337 behavior = (*plugin_it)->sr_localsid_function_number;
345 if (unformat (input, "end"))
346 behavior = SR_BEHAVIOR_END;
351 else if (!end_psp && unformat (input, "psp"))
357 if (!behavior && end_psp)
358 behavior = SR_BEHAVIOR_END;
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");
373 rv = sr_cli_localsid (is_del, &resulting_address, end_psp, behavior,
374 sw_if_index, vlan_index, fib_index, &next_hop,
384 return clib_error_return (0,
385 "Identical localsid already exists. Requested localsid not created.");
387 return clib_error_return (0,
388 "The requested localsid could not be deleted. SR localsid not found");
390 return clib_error_return (0, "FIB table %u does not exist", fib_index);
392 return clib_error_return (0, "There is already one FIB entry for the"
393 "requested localsid non segment routing related");
395 return clib_error_return (0,
396 "Could not create ARP/ND entry for such next_hop. Internal error.");
398 return clib_error_return (0,
399 "Error on the plugin based localsid creation.");
401 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
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",
412 "Create SR LocalSID and binds it to a particular behavior\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"
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,
436 * @brief CLI function to 'show' all SR LocalSIDs on console.
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)
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;
448 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
449 vlib_cli_output (vm, "=========================");
451 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
453 for (i = 0; i < vec_len (localsid_list); i++)
455 ls = localsid_list[i];
456 switch (ls->behavior)
458 case SR_BEHAVIOR_END:
459 vlib_cli_output (vm, "\tAddress: \t%U\n\tBehavior: \tEnd",
460 format_ip6_address, &ls->localsid);
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);
470 case SR_BEHAVIOR_DX4:
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);
478 case SR_BEHAVIOR_DX6:
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);
486 case SR_BEHAVIOR_DX2:
487 if (ls->vlan_index == (u32) ~ 0)
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,
495 "Unsupported yet. (DX2 with egress VLAN rewrite)");
497 case SR_BEHAVIOR_DT6:
499 "\tAddress: \t%U\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
500 "\n\tTable: %u", format_ip6_address, &ls->localsid,
503 case SR_BEHAVIOR_DT4:
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);
510 if (ls->behavior >= SR_BEHAVIOR_LAST)
512 sr_localsid_fn_registration_t *plugin =
513 pool_elt_at_index (sm->plugin_functions,
514 ls->behavior - SR_BEHAVIOR_LAST);
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);
523 //Should never get here...
524 vlib_cli_output (vm, "Internal error");
528 vlib_cli_output (vm, "\tPSP: \tTrue\n");
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, "--------------------");
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,
552 * @brief Function to 'clear' ALL SR localsid counters
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)
559 ip6_sr_main_t *sm = &sr_main;
561 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
562 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
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,
575 /************************ SR LocalSID graphs node ****************************/
577 * @brief SR localsid node trace
582 ip6_address_t src, out_dst;
586 //With SRv6 header update include flags here.
587 } sr_localsid_trace_t;
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")
599 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
600 foreach_sr_localsid_error
603 } sr_localsid_error_t;
605 static char *sr_localsid_error_strings[] = {
606 #define _(sym,string) string,
607 foreach_sr_localsid_error
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")
621 #define _(s,n) SR_LOCALSID_NEXT_##s,
622 foreach_sr_localsid_next
625 } sr_localsid_next_t;
628 * @brief SR LocalSID graph node trace function
633 format_sr_localsid_trace (u8 * s, va_list * args)
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 *);
640 ip6_sr_localsid_t *ls =
641 pool_elt_at_index (sm->localsids, t->localsid_index);
644 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
646 switch (ls->behavior)
648 case SR_BEHAVIOR_END:
649 s = format (s, "\tBehavior: End\n");
651 case SR_BEHAVIOR_DX6:
652 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
654 case SR_BEHAVIOR_DX4:
655 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
658 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
660 case SR_BEHAVIOR_DT6:
661 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
663 case SR_BEHAVIOR_DT4:
664 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
666 case SR_BEHAVIOR_DX2:
667 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
670 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
673 if (t->num_segments != 0xFF)
675 if (t->num_segments > 0)
677 s = format (s, "\tSegments left: %d\n", t->num_segments);
678 s = format (s, "\tSID list: [in ietf order]");
680 for (i = 0; i < t->num_segments; i++)
682 s = format (s, "\n\t-> %U", format_ip6_address,
683 (ip6_address_t *) & t->sr[i *
684 sizeof (ip6_address_t)]);
692 * @brief Function doing End processing.
694 static_always_inline void
695 end_srh_processing (vlib_node_runtime_t * node,
698 ip6_sr_header_t * sr0,
699 ip6_sr_localsid_t * ls0, u32 * next0)
701 ip6_address_t *new_dst0;
703 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
705 if (PREDICT_TRUE (sr0->segments_left != 0))
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];
713 if (ls0->behavior == SR_BEHAVIOR_X)
715 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
716 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
721 *next0 = SR_LOCALSID_NEXT_ERROR;
722 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
727 /* Error. Routing header of type != SR */
728 *next0 = SR_LOCALSID_NEXT_ERROR;
729 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
734 * @brief Function doing SRH processing for D* variants
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,
741 ip6_sr_header_t * sr0,
742 ip6_sr_localsid_t * ls0, u32 * next0)
744 /* Compute the size of the IPv6 header with all Ext. headers */
746 ip6_ext_header_t *next_ext_header;
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))
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);
759 /* Ensure this is the last segment. Otherwise drop. */
760 if (sr0 && sr0->segments_left != 0)
762 *next0 = SR_LOCALSID_NEXT_ERROR;
763 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
769 case IP_PROTOCOL_IPV6:
770 /* Encap-End IPv6. Pop outer IPv6 header. */
771 if (ls0->behavior == SR_BEHAVIOR_DX6)
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;
778 else if (ls0->behavior == SR_BEHAVIOR_DT6)
780 vlib_buffer_advance (b0, total_size);
781 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->fib_table;
785 case IP_PROTOCOL_IP_IN_IP:
786 /* Encap-End IPv4. Pop outer IPv6 header */
787 if (ls0->behavior == SR_BEHAVIOR_DX4)
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;
794 else if (ls0->behavior == SR_BEHAVIOR_DT4)
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;
802 case IP_PROTOCOL_IP6_NONXT:
804 if (ls0->behavior == SR_BEHAVIOR_DX2)
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;
813 *next0 = SR_LOCALSID_NEXT_ERROR;
814 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
819 * @brief Function doing End processing with PSP
821 static_always_inline void
822 end_psp_srh_processing (vlib_node_runtime_t * node,
825 ip6_ext_header_t * prev0,
826 ip6_sr_header_t * sr0,
827 ip6_sr_localsid_t * ls0, u32 * next0)
831 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
833 if (PREDICT_TRUE (sr0->segments_left == 1))
835 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
836 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
838 /* Remove the SRH taking care of the rest of IPv6 ext header */
840 prev0->next_hdr = sr0->protocol;
842 ip0->protocol = sr0->protocol;
844 sr_len = ip6_ext_header_len (sr0);
845 vlib_buffer_advance (b0, sr_len);
846 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
847 ip0->payload_length = clib_host_to_net_u16 (new_l0);
848 clib_memcpy ((void *) ip0 + sr_len, ip0,
849 (void *) sr0 - (void *) ip0);
851 if (ls0->behavior == SR_BEHAVIOR_X)
853 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
854 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
859 /* Error. Routing header of type != SR */
860 *next0 = SR_LOCALSID_NEXT_ERROR;
861 b0->error = node->errors[SR_LOCALSID_ERROR_NO_PSP];
865 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
868 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
869 vlib_frame_t * from_frame)
871 u32 n_left_from, next_index, *from, *to_next;
872 ip6_sr_main_t *sm = &sr_main;
873 from = vlib_frame_vector_args (from_frame);
874 n_left_from = from_frame->n_vectors;
875 next_index = node->cached_next_index;
876 u32 cpu_index = os_get_cpu_number ();
878 while (n_left_from > 0)
881 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
884 while (n_left_from >= 8 && n_left_to_next >= 4)
886 u32 bi0, bi1, bi2, bi3;
887 vlib_buffer_t *b0, *b1, *b2, *b3;
888 ip6_header_t *ip0, *ip1, *ip2, *ip3;
889 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
890 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
891 u32 next0, next1, next2, next3;
892 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
893 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
895 /* Prefetch next iteration. */
897 vlib_buffer_t *p4, *p5, *p6, *p7;
899 p4 = vlib_get_buffer (vm, from[4]);
900 p5 = vlib_get_buffer (vm, from[5]);
901 p6 = vlib_get_buffer (vm, from[6]);
902 p7 = vlib_get_buffer (vm, from[7]);
904 /* Prefetch the buffer header and packet for the N+4 loop iteration */
905 vlib_prefetch_buffer_header (p4, LOAD);
906 vlib_prefetch_buffer_header (p5, LOAD);
907 vlib_prefetch_buffer_header (p6, LOAD);
908 vlib_prefetch_buffer_header (p7, LOAD);
910 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
911 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
912 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
913 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
916 to_next[0] = bi0 = from[0];
917 to_next[1] = bi1 = from[1];
918 to_next[2] = bi2 = from[2];
919 to_next[3] = bi3 = from[3];
925 b0 = vlib_get_buffer (vm, bi0);
926 b1 = vlib_get_buffer (vm, bi1);
927 b2 = vlib_get_buffer (vm, bi2);
928 b3 = vlib_get_buffer (vm, bi3);
931 pool_elt_at_index (sm->localsids,
932 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
934 pool_elt_at_index (sm->localsids,
935 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
937 pool_elt_at_index (sm->localsids,
938 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
940 pool_elt_at_index (sm->localsids,
941 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
943 ip0 = vlib_buffer_get_current (b0);
944 ip1 = vlib_buffer_get_current (b1);
945 ip2 = vlib_buffer_get_current (b2);
946 ip3 = vlib_buffer_get_current (b3);
948 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
949 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
950 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
951 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
953 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
954 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
955 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
956 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
960 vlib_increment_combined_counter
962 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
963 &(sm->sr_ls_valid_counters)), cpu_index, ls0 - sm->localsids, 1,
964 vlib_buffer_length_in_chain (vm, b0));
966 vlib_increment_combined_counter
968 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
969 &(sm->sr_ls_valid_counters)), cpu_index, ls1 - sm->localsids, 1,
970 vlib_buffer_length_in_chain (vm, b1));
972 vlib_increment_combined_counter
974 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
975 &(sm->sr_ls_valid_counters)), cpu_index, ls2 - sm->localsids, 1,
976 vlib_buffer_length_in_chain (vm, b2));
978 vlib_increment_combined_counter
980 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
981 &(sm->sr_ls_valid_counters)), cpu_index, ls3 - sm->localsids, 1,
982 vlib_buffer_length_in_chain (vm, b3));
984 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
985 n_left_to_next, bi0, bi1, bi2, bi3,
986 next0, next1, next2, next3);
989 /* Single loop for potentially the last three packets */
990 while (n_left_from > 0 && n_left_to_next > 0)
995 ip6_ext_header_t *prev0;
996 ip6_sr_header_t *sr0;
997 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
998 ip6_sr_localsid_t *ls0;
1005 n_left_to_next -= 1;
1007 b0 = vlib_get_buffer (vm, bi0);
1008 ip0 = vlib_buffer_get_current (b0);
1010 /* Lookup the SR End behavior based on IP DA (adj) */
1012 pool_elt_at_index (sm->localsids,
1013 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1015 /* Find SRH as well as previous header */
1016 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1018 /* SRH processing and End variants */
1019 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1021 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1023 sr_localsid_trace_t *tr =
1024 vlib_add_trace (vm, node, b0, sizeof (*tr));
1025 tr->num_segments = 0;
1026 tr->localsid_index = ls0 - sm->localsids;
1028 if (ip0 == vlib_buffer_get_current (b0))
1030 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1031 sizeof (tr->src.as_u8));
1032 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1033 sizeof (tr->out_dst.as_u8));
1034 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1035 && sr0->type == ROUTING_HEADER_TYPE_SR)
1037 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1039 sr0->length * 8 / sizeof (ip6_address_t);
1040 tr->segments_left = sr0->segments_left;
1044 tr->num_segments = 0xFF;
1047 /* Increase the counters */
1048 vlib_increment_combined_counter
1050 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1051 &(sm->sr_ls_valid_counters)), cpu_index, ls0 - sm->localsids, 1,
1052 vlib_buffer_length_in_chain (vm, b0));
1054 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1055 n_left_to_next, bi0, next0);
1057 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1059 return from_frame->n_vectors;
1063 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1064 .function = sr_localsid_d_fn,
1065 .name = "sr-localsid-d",
1066 .vector_size = sizeof (u32),
1067 .format_trace = format_sr_localsid_trace,
1068 .type = VLIB_NODE_TYPE_INTERNAL,
1069 .n_errors = SR_LOCALSID_N_ERROR,
1070 .error_strings = sr_localsid_error_strings,
1071 .n_next_nodes = SR_LOCALSID_N_NEXT,
1073 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1074 foreach_sr_localsid_next
1081 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
1084 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1085 vlib_frame_t * from_frame)
1087 u32 n_left_from, next_index, *from, *to_next;
1088 ip6_sr_main_t *sm = &sr_main;
1089 from = vlib_frame_vector_args (from_frame);
1090 n_left_from = from_frame->n_vectors;
1091 next_index = node->cached_next_index;
1092 u32 cpu_index = os_get_cpu_number ();
1094 while (n_left_from > 0)
1097 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1100 while (n_left_from >= 8 && n_left_to_next >= 4)
1102 u32 bi0, bi1, bi2, bi3;
1103 vlib_buffer_t *b0, *b1, *b2, *b3;
1104 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1105 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1106 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1107 u32 next0, next1, next2, next3;
1108 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1109 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1111 /* Prefetch next iteration. */
1113 vlib_buffer_t *p4, *p5, *p6, *p7;
1115 p4 = vlib_get_buffer (vm, from[4]);
1116 p5 = vlib_get_buffer (vm, from[5]);
1117 p6 = vlib_get_buffer (vm, from[6]);
1118 p7 = vlib_get_buffer (vm, from[7]);
1120 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1121 vlib_prefetch_buffer_header (p4, LOAD);
1122 vlib_prefetch_buffer_header (p5, LOAD);
1123 vlib_prefetch_buffer_header (p6, LOAD);
1124 vlib_prefetch_buffer_header (p7, LOAD);
1126 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1127 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1128 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1129 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1132 to_next[0] = bi0 = from[0];
1133 to_next[1] = bi1 = from[1];
1134 to_next[2] = bi2 = from[2];
1135 to_next[3] = bi3 = from[3];
1139 n_left_to_next -= 4;
1141 b0 = vlib_get_buffer (vm, bi0);
1142 b1 = vlib_get_buffer (vm, bi1);
1143 b2 = vlib_get_buffer (vm, bi2);
1144 b3 = vlib_get_buffer (vm, bi3);
1146 ip0 = vlib_buffer_get_current (b0);
1147 ip1 = vlib_buffer_get_current (b1);
1148 ip2 = vlib_buffer_get_current (b2);
1149 ip3 = vlib_buffer_get_current (b3);
1151 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1152 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
1153 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
1154 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
1157 pool_elt_at_index (sm->localsids,
1158 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1160 pool_elt_at_index (sm->localsids,
1161 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1163 pool_elt_at_index (sm->localsids,
1164 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1166 pool_elt_at_index (sm->localsids,
1167 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1170 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1172 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1175 end_psp_srh_processing (node, b1, ip1, prev1, sr1, ls1, &next1);
1177 end_srh_processing (node, b1, ip1, sr1, ls1, &next1);
1180 end_psp_srh_processing (node, b2, ip2, prev2, sr2, ls2, &next2);
1182 end_srh_processing (node, b2, ip2, sr2, ls2, &next2);
1185 end_psp_srh_processing (node, b3, ip3, prev3, sr3, ls3, &next3);
1187 end_srh_processing (node, b3, ip3, sr3, ls3, &next3);
1189 //TODO: proper trace.
1191 vlib_increment_combined_counter
1193 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1194 &(sm->sr_ls_valid_counters)), cpu_index, ls0 - sm->localsids, 1,
1195 vlib_buffer_length_in_chain (vm, b0));
1197 vlib_increment_combined_counter
1199 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1200 &(sm->sr_ls_valid_counters)), cpu_index, ls1 - sm->localsids, 1,
1201 vlib_buffer_length_in_chain (vm, b1));
1203 vlib_increment_combined_counter
1205 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1206 &(sm->sr_ls_valid_counters)), cpu_index, ls2 - sm->localsids, 1,
1207 vlib_buffer_length_in_chain (vm, b2));
1209 vlib_increment_combined_counter
1211 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1212 &(sm->sr_ls_valid_counters)), cpu_index, ls3 - sm->localsids, 1,
1213 vlib_buffer_length_in_chain (vm, b3));
1215 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1216 n_left_to_next, bi0, bi1, bi2, bi3,
1217 next0, next1, next2, next3);
1220 /* Single loop for potentially the last three packets */
1221 while (n_left_from > 0 && n_left_to_next > 0)
1225 ip6_header_t *ip0 = 0;
1226 ip6_ext_header_t *prev0;
1227 ip6_sr_header_t *sr0;
1228 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1229 ip6_sr_localsid_t *ls0;
1236 n_left_to_next -= 1;
1238 b0 = vlib_get_buffer (vm, bi0);
1239 ip0 = vlib_buffer_get_current (b0);
1240 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1242 /* Lookup the SR End behavior based on IP DA (adj) */
1244 pool_elt_at_index (sm->localsids,
1245 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1247 /* SRH processing */
1249 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1251 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1253 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1255 sr_localsid_trace_t *tr =
1256 vlib_add_trace (vm, node, b0, sizeof (*tr));
1257 tr->num_segments = 0;
1258 tr->localsid_index = ls0 - sm->localsids;
1260 if (ip0 == vlib_buffer_get_current (b0))
1262 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1263 sizeof (tr->src.as_u8));
1264 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1265 sizeof (tr->out_dst.as_u8));
1266 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1267 && sr0->type == ROUTING_HEADER_TYPE_SR)
1269 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1271 sr0->length * 8 / sizeof (ip6_address_t);
1272 tr->segments_left = sr0->segments_left;
1277 tr->num_segments = 0xFF;
1281 vlib_increment_combined_counter
1283 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1284 &(sm->sr_ls_valid_counters)), cpu_index, ls0 - sm->localsids, 1,
1285 vlib_buffer_length_in_chain (vm, b0));
1287 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1288 n_left_to_next, bi0, next0);
1290 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1292 return from_frame->n_vectors;
1296 VLIB_REGISTER_NODE (sr_localsid_node) = {
1297 .function = sr_localsid_fn,
1298 .name = "sr-localsid",
1299 .vector_size = sizeof (u32),
1300 .format_trace = format_sr_localsid_trace,
1301 .type = VLIB_NODE_TYPE_INTERNAL,
1302 .n_errors = SR_LOCALSID_N_ERROR,
1303 .error_strings = sr_localsid_error_strings,
1304 .n_next_nodes = SR_LOCALSID_N_NEXT,
1306 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1307 foreach_sr_localsid_next
1314 format_sr_dpo (u8 * s, va_list * args)
1316 index_t index = va_arg (*args, index_t);
1317 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1319 return (format (s, "SR: localsid_index:[%d]", index));
1322 const static dpo_vft_t sr_loc_vft = {
1323 .dv_lock = sr_dpo_lock,
1324 .dv_unlock = sr_dpo_unlock,
1325 .dv_format = format_sr_dpo,
1328 const static char *const sr_loc_ip6_nodes[] = {
1333 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1334 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1337 const static char *const sr_loc_d_ip6_nodes[] = {
1342 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1343 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1347 /*************************** SR LocalSID plugins ******************************/
1349 * @brief SR LocalSID plugin registry
1352 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1353 u8 * keyword_str, u8 * def_str,
1354 u8 * params_str, dpo_type_t * dpo,
1355 format_function_t * ls_format,
1356 unformat_function_t * ls_unformat,
1357 sr_plugin_callback_t * creation_fn,
1358 sr_plugin_callback_t * removal_fn)
1360 ip6_sr_main_t *sm = &sr_main;
1363 sr_localsid_fn_registration_t *plugin;
1365 /* Did this function exist? If so update it */
1366 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1369 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1371 /* Else create a new one and set hash key */
1374 pool_get (sm->plugin_functions, plugin);
1375 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1376 plugin - sm->plugin_functions);
1379 memset (plugin, 0, sizeof (*plugin));
1381 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1382 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1383 plugin->ls_format = ls_format;
1384 plugin->ls_unformat = ls_unformat;
1385 plugin->creation = creation_fn;
1386 plugin->removal = removal_fn;
1387 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1388 plugin->function_name = format (0, "%s%c", fn_name, 0);
1389 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1390 plugin->def_str = format (0, "%s%c", def_str, 0);
1391 plugin->params_str = format (0, "%s%c", params_str, 0);
1393 return plugin->sr_localsid_function_number;
1397 * @brief CLI function to 'show' all available SR LocalSID behaviors
1399 static clib_error_t *
1400 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1401 unformat_input_t * input,
1402 vlib_cli_command_t * cmd)
1404 ip6_sr_main_t *sm = &sr_main;
1405 sr_localsid_fn_registration_t *plugin;
1406 sr_localsid_fn_registration_t **plugins_vec = 0;
1409 vlib_cli_output (vm,
1410 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1413 pool_foreach (plugin, sm->plugin_functions,
1414 ({ vec_add1 (plugins_vec, plugin); }));
1417 /* Print static behaviors */
1418 vlib_cli_output (vm, "Default behaviors:\n"
1419 "\tEnd\t-> Endpoint.\n"
1420 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
1421 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1422 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1423 "\t\tParameters: '<iface>'\n"
1424 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1425 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1426 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1427 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1428 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1429 "\t\tParameters: '<ip6_fib_table>'\n"
1430 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1431 "\t\tParameters: '<ip4_fib_table>'\n");
1432 vlib_cli_output (vm, "Plugin behaviors:\n");
1433 for (i = 0; i < vec_len (plugins_vec); i++)
1435 plugin = plugins_vec[i];
1436 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1438 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1444 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1445 .path = "show sr localsids behaviors",
1446 .short_help = "show sr localsids behaviors",
1447 .function = show_sr_localsid_behaviors_command_fn,
1452 * @brief SR LocalSID initialization
1455 sr_localsids_init (vlib_main_t * vm)
1457 /* Init memory for function keys */
1458 ip6_sr_main_t *sm = &sr_main;
1459 sm->localsids_index_by_key =
1460 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1461 /* Init SR behaviors DPO type */
1462 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1463 /* Init SR behaviors DPO type */
1464 sr_localsid_d_dpo_type =
1465 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1466 /* Init memory for localsid plugins */
1467 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1471 VLIB_INIT_FUNCTION (sr_localsids_init);
1473 * fd.io coding-style-patch-verification: ON
1476 * eval: (c-set-style "gnu")